summaryrefslogtreecommitdiff
path: root/apps/codecs/libspeex/ltp.c
blob: 9f10bc09d85a5399f0cc489804098da4e8d6b91a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/* Copyright (C) 2002-2006 Jean-Marc Valin 
   File: ltp.c
   Long-Term Prediction functions

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config-speex.h"
#endif

#include <math.h>
#include "ltp.h"
#include "stack_alloc.h"
#include "filters.h"
#include "speex/speex_bits.h"
#include "math_approx.h"
#include "os_support.h"

#ifndef NULL
#define NULL 0
#endif


#ifdef _USE_SSE
#include "ltp_sse.h"
#elif defined (ARM4_ASM) || defined(ARM5E_ASM)
#include "ltp_arm4.h"
#elif defined (COLDFIRE_ASM)
#define OVERRIDE_INNER_PROD
#elif defined (BFIN_ASM)
#include "ltp_bfin.h"
#endif

#ifndef OVERRIDE_INNER_PROD
spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)
{
   spx_word32_t sum=0;
   len >>= 2;
   while(len--)
   {
      spx_word32_t part=0;
      part = MAC16_16(part,*x++,*y++);
      part = MAC16_16(part,*x++,*y++);
      part = MAC16_16(part,*x++,*y++);
      part = MAC16_16(part,*x++,*y++);
      /* HINT: If you had a 40-bit accumulator, you could shift only at the end */
      sum = ADD32(sum,SHR32(part,6));
   }
   return sum;
}
#endif

#ifndef SPEEX_DISABLE_ENCODER
#ifndef OVERRIDE_PITCH_XCORR
#if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */
void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)
{
   int i,j;
   for (i=0;i<nb_pitch;i+=4)
   {
      /* Compute correlation*/
      /*corr[nb_pitch-1-i]=inner_prod(x, _y+i, len);*/
      spx_word32_t sum1=0;
      spx_word32_t sum2=0;
      spx_word32_t sum3=0;
      spx_word32_t sum4=0;
      const spx_word16_t *y = _y+i;
      const spx_word16_t *x = _x;
      spx_word16_t y0, y1, y2, y3;
      /*y0=y[0];y1=y[1];y2=y[2];y3=y[3];*/
      y0=*y++;
      y1=*y++;
      y2=*y++;
      y3=*y++;
      for (j=0;j<len;j+=4)
      {
         spx_word32_t part1;
         spx_word32_t part2;
         spx_word32_t part3;
         spx_word32_t part4;
         part1 = MULT16_16(*x,y0);
         part2 = MULT16_16(*x,y1);
         part3 = MULT16_16(*x,y2);
         part4 = MULT16_16(*x,y3);
         x++;
         y0=*y++;
         part1 = MAC16_16(part1,*x,y1);
         part2 = MAC16_16(part2,*x,y2);
         part3 = MAC16_16(part3,*x,y3);
         part4 = MAC16_16(part4,*x,y0);
         x++;
         y1=*y++;
         part1 = MAC16_16(part1,*x,y2);
         part2 = MAC16_16(part2,*x,y3);
         part3 = MAC16_16(part3,*x,y0);
         part4 = MAC16_16(part4,*x,y1);
         x++;
         y2=*y++;
         part1 = MAC16_16(part1,*x,y3);
         part2 = MAC16_16(part2,*x,y0);
         part3 = MAC16_16(part3,*x,y1);
         part4 = MAC16_16(part4,*x,y2);
         x++;
         y3=*y++;
         
         sum1 = ADD32(sum1,SHR32(part1,6));
         sum2 = ADD32(sum2,SHR32(part2,6));
         sum3 = ADD32(sum3,SHR32(part3,6));
         sum4 = ADD32(sum4,SHR32(part4,6));
      }
      corr[nb_pitch-1-i]=sum1;
      corr[nb_pitch-2-i]=sum2;
      corr[nb_pitch-3-i]=sum3;
      corr[nb_pitch-4-i]=sum4;
   }

}
#else
void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)
{
   int i;
   for (i=0;i<nb_pitch;i++)
   {
      /* Compute correlation*/
      corr[nb_pitch-1-i]=inner_prod(_x, _y+i, len);
   }

}
#endif
#endif

#ifndef OVERRIDE_COMPUTE_PITCH_ERROR
static inline spx_word32_t compute_pitch_error(spx_word16_t *C, spx_word16_t *g, spx_word16_t pitch_control)
{
   spx_word32_t sum = 0;
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[0],pitch_control),C[0]));
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[1],pitch_control),C[1]));
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[2],pitch_control),C[2]));
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[1]),C[3]));
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[1]),C[4]));
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[0]),C[5]));
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[0]),C[6]));
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[1],g[1]),C[7]));
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[2]),C[8]));
   return sum;
}
#endif

#ifndef OVERRIDE_OPEN_LOOP_NBEST_PITCH
void open_loop_nbest_pitch(spx_word16_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack)
{
   int i,j,k;
   VARDECL(spx_word32_t *best_score);
   VARDECL(spx_word32_t *best_ener);
   spx_word32_t e0;
   VARDECL(spx_word32_t *corr);
#ifdef FIXED_POINT
   /* In fixed-point, we need only one (temporary) array of 32-bit values and two (corr16, ener16) 
      arrays for (normalized) 16-bit values */
   VARDECL(spx_word16_t *corr16);
   VARDECL(spx_word16_t *ener16);
   spx_word32_t *energy;
   int cshift=0, eshift=0;
   int scaledown = 0;
   ALLOC(corr16, end-start+1, spx_word16_t);
   ALLOC(ener16, end-start+1, spx_word16_t);
   ALLOC(corr, end-start+1, spx_word32_t);
   energy = corr;
#else
   /* In floating-point, we need to float arrays and no normalized copies */
   VARDECL(spx_word32_t *energy);
   spx_word16_t *corr16;
   spx_word16_t *ener16;
   ALLOC(energy, end-start+2, spx_word32_t);
   ALLOC(corr, end-start+1, spx_word32_t);
   corr16 = corr;
   ener16 = energy;
#endif
   
   ALLOC(best_score, N, spx_word32_t);
   ALLOC(best_ener, N, spx_word32_t);
   for (i=0;i<N;i++)
   {
        best_score[i]=-1;
        best_ener[i]=0;
        pitch[i]=start;
   }
   
#ifdef FIXED_POINT
   for (i=-end;i<len;i++)
   {
      if (ABS16(sw[i])>16383)
      {
         scaledown=1;
         break;
      }
   }
   /* If the weighted input is close to saturation, then we scale it down */
   if (scaledown)
   {
      for (i=-end;i<len;i++)
      {
         sw[i]=SHR16(sw[i],1);
      }
   }      
#endif
   energy[0]=inner_prod(sw-start, sw-start, len);
   e0=inner_prod(sw, sw, len);
   for (i=start;i<end;i++)
   {
      /* Update energy for next pitch*/
      energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw[-i-1],sw[-i-1]),6)), SHR32(MULT16_16(sw[-i+len-1],sw[-i+len-1]),6));
      if (energy[i-start+1] < 0)
         energy[i-start+1] = 0;
   }
   
#ifdef FIXED_POINT
   eshift = normalize16(energy, ener16, 32766, end-start+1);
#endif
   
   /* In fixed-point, this actually overrites the energy array (aliased to corr) */
   pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack);
   
#ifdef FIXED_POINT
   /* Normalize to 180 so we can square it and it still fits in 16 bits */
   cshift = normalize16(corr, corr16, 180, end-start+1);
   /* If we scaled weighted input down, we need to scale it up again (OK, so we've just lost the LSB, who cares?) */
   if (scaledown)
   {
      for (i=-end;i<len;i++)
      {
         sw[i]=SHL16(sw[i],1);
      }
   }      
#endif

   /* Search for the best pitch prediction gain */
   for (i=start;i<=end;i++)
   {
      spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]);
      /* Instead of dividing the tmp by the energy, we multiply on the other side */
      if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start])))
      {
         /* We can safely put it last and then check */
         best_score[N-1]=tmp;
         best_ener[N-1]=ener16[i-start]+1;
         pitch[N-1]=i;
         /* Check if it comes in front of others */
         for (j=0;j<N-1;j++)
         {
            if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start])))
            {
               for (k=N-1;k>j;k--)
               {
                  best_score[k]=best_score[k-1];
                  best_ener[k]=best_ener[k-1];
                  pitch[k]=pitch[k-1];
               }
               best_score[j]=tmp;
               best_ener[j]=ener16[i-start]+1;
               pitch[j]=i;
               break;
            }
         }
      }
   }
   
   /* Compute open-loop gain if necessary */
   if (gain)
   {
      for (j=0;j<N;j++)
      {
         spx_word16_t g;
         i=pitch[j];
         g = DIV32(SHL32(EXTEND32(corr16[i-start]),cshift), 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(SHL32(EXTEND32(ener16[i-start]),eshift))),6));
         /* FIXME: g = max(g,corr/energy) */
         if (g<0)
            g = 0;
         gain[j]=g;
      }
   }


}
#endif

#ifndef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ
static int pitch_gain_search_3tap_vq(
  const signed char *gain_cdbk,
  int                gain_cdbk_size,
  spx_word16_t      *C16,
  spx_word16_t       max_gain
)
{
  const signed char *ptr=gain_cdbk;
  int                best_cdbk=0;
  spx_word32_t       best_sum=-VERY_LARGE32;
  spx_word32_t       sum=0;
  spx_word16_t       g[3];
  spx_word16_t       pitch_control=64;
  spx_word16_t       gain_sum;
  int                i;

  for (i=0;i<gain_cdbk_size;i++) {
         
    ptr = gain_cdbk+4*i;
    g[0]=ADD16((spx_word16_t)ptr[0],32);
    g[1]=ADD16((spx_word16_t)ptr[1],32);
    g[2]=ADD16((spx_word16_t)ptr[2],32);
    gain_sum = (spx_word16_t)ptr[3];
         
    sum = compute_pitch_error(C16, g, pitch_control);
         
    if (sum>best_sum && gain_sum<=max_gain) {
      best_sum=sum;
      best_cdbk=i;
    }
  }

  return best_cdbk;
}
#endif

/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
static spx_word32_t pitch_gain_search_3tap(
const spx_word16_t target[],       /* Target vector */
const spx_coef_t ak[],          /* LPCs for this subframe */
const spx_coef_t awk1[],        /* Weighted LPCs #1 for this subframe */
const spx_coef_t awk2[],        /* Weighted LPCs #2 for this subframe */
spx_sig_t exc[],                /* Excitation */
const signed char *gain_cdbk,
int gain_cdbk_size,
int   pitch,                    /* Pitch value */
int   p,                        /* Number of LPC coeffs */
int   nsf,                      /* Number of samples in subframe */
SpeexBits *bits,
char *stack,
const spx_word16_t *exc2,
const spx_word16_t *r,
spx_word16_t *new_target,
int  *cdbk_index,
int plc_tuning,
spx_word32_t cumul_gain,
int scaledown
)
{
   int i,j;
   VARDECL(spx_word16_t *tmp1);
   VARDECL(spx_word16_t *e);
   spx_word16_t *x[3];
   spx_word32_t corr[3];
   spx_word32_t A[3][3];
   spx_word16_t gain[3];
   spx_word32_t err;
   spx_word16_t max_gain=128;
   int          best_cdbk=0;

   ALLOC(tmp1, 3*nsf, spx_word16_t);
   ALLOC(e, nsf, spx_word16_t);

   if (cumul_gain > 262144)
      max_gain = 31;
   
   x[0]=tmp1;
   x[1]=tmp1+nsf;
   x[2]=tmp1+2*nsf;
   
   for (j=0;j<nsf;j++)
      new_target[j] = target[j];

   {
      VARDECL(spx_mem_t *mm);
      int pp=pitch-1;
      ALLOC(mm, p, spx_mem_t);
      for (j=0;j<nsf;j++)
      {
         if (j-pp<0)
            e[j]=exc2[j-pp];
         else if (j-pp-pitch<0)
            e[j]=exc2[j-pp-pitch];
         else
            e[j]=0;
      }
#ifdef FIXED_POINT
      /* Scale target and excitation down if needed (avoiding overflow) */
      if (scaledown)
      {
         for (j=0;j<nsf;j++)
            e[j] = SHR16(e[j],1);
         for (j=0;j<nsf;j++)
            new_target[j] = SHR16(new_target[j],1);
      }
#endif
      for (j=0;j<p;j++)
         mm[j] = 0;
      iir_mem16(e, ak, e, nsf, p, mm, stack);
      for (j=0;j<p;j++)
         mm[j] = 0;
      filter_mem16(e, awk1, awk2, e, nsf, p, mm, stack);
      for (j=0;j<nsf;j++)
         x[2][j] = e[j];
   }
   for (i=1;i>=0;i--)
   {
      spx_word16_t e0=exc2[-pitch-1+i];
#ifdef FIXED_POINT
      /* Scale excitation down if needed (avoiding overflow) */
      if (scaledown)
         e0 = SHR16(e0,1);
#endif
      x[i][0]=MULT16_16_Q14(r[0], e0);
      for (j=0;j<nsf-1;j++)
         x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0));
   }

   for (i=0;i<3;i++)
      corr[i]=inner_prod(x[i],new_target,nsf);
   for (i=0;i<3;i++)
      for (j=0;j<=i;j++)
         A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);

   {
      spx_word32_t C[9];
#ifdef FIXED_POINT
      spx_word16_t C16[9];
#else
      spx_word16_t *C16=C;
#endif      
      C[0]=corr[2];
      C[1]=corr[1];
      C[2]=corr[0];
      C[3]=A[1][2];
      C[4]=A[0][1];
      C[5]=A[0][2];      
      C[6]=A[2][2];
      C[7]=A[1][1];
      C[8]=A[0][0];
      
      /*plc_tuning *= 2;*/
      if (plc_tuning<2)
         plc_tuning=2;
      if (plc_tuning>30)
         plc_tuning=30;
#ifdef FIXED_POINT
      C[0] = SHL32(C[0],1);
      C[1] = SHL32(C[1],1);
      C[2] = SHL32(C[2],1);
      C[3] = SHL32(C[3],1);
      C[4] = SHL32(C[4],1);
      C[5] = SHL32(C[5],1);
      C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]);
      C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]);
      C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]);
      normalize16(C, C16, 32767, 9);
#else
      C[6]*=.5*(1+.02*plc_tuning);
      C[7]*=.5*(1+.02*plc_tuning);
      C[8]*=.5*(1+.02*plc_tuning);
#endif

      best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain);

#ifdef FIXED_POINT
      gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]);
      gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]);
      gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]);
      /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
#else
      gain[0] = 0.015625*gain_cdbk[best_cdbk*4]  + .5;
      gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5;
      gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5;
#endif
      *cdbk_index=best_cdbk;
   }

   SPEEX_MEMSET(exc, 0, nsf);
   for (i=0;i<3;i++)
   {
      int j;
      int tmp1, tmp3;
      int pp=pitch+1-i;
      tmp1=nsf;
      if (tmp1>pp)
         tmp1=pp;
      for (j=0;j<tmp1;j++)
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]);
      tmp3=nsf;
      if (tmp3>pp+pitch)
         tmp3=pp+pitch;
      for (j=tmp1;j<tmp3;j++)
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]);
   }
   for (i=0;i<nsf;i++)
   {
      spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])),
                            MULT16_16(gain[2],x[0][i]));
      new_target[i] = SUB16(new_target[i], EXTRACT16(PSHR32(tmp,6)));
   }
   err = inner_prod(new_target, new_target, nsf);

   return err;
}

/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
int pitch_search_3tap(
spx_word16_t target[],                 /* Target vector */
spx_word16_t *sw,
spx_coef_t ak[],                     /* LPCs for this subframe */
spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */
spx_coef_t awk2[],                   /* Weighted LPCs #2 for this subframe */
spx_sig_t exc[],                    /* Excitation */
const void *par,
int   start,                    /* Smallest pitch value allowed */
int   end,                      /* Largest pitch value allowed */
spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */
int   p,                        /* Number of LPC coeffs */
int   nsf,                      /* Number of samples in subframe */
SpeexBits *bits,
char *stack,
spx_word16_t *exc2,
spx_word16_t *r,
int complexity,
int cdbk_offset,
int plc_tuning,
spx_word32_t *cumul_gain
)
{
   int i;
   int cdbk_index, pitch=0, best_gain_index=0;
   VARDECL(spx_sig_t *best_exc);
   VARDECL(spx_word16_t *new_target);
   VARDECL(spx_word16_t *best_target);
   int best_pitch=0;
   spx_word32_t err, best_err=-1;
   int N;
   const ltp_params *params;
   const signed char *gain_cdbk;
   int   gain_cdbk_size;
   int scaledown=0;
         
   VARDECL(int *nbest);
   
   params = (const ltp_params*) par;
   gain_cdbk_size = 1<<params->gain_bits;
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
   
   N=complexity;
   if (N>10)
      N=10;
   if (N<1)
      N=1;

   ALLOC(nbest, N, int);
   params = (const ltp_params*) par;

   if (end<start)
   {
      speex_bits_pack(bits, 0, params->pitch_bits);
      speex_bits_pack(bits, 0, params->gain_bits);
      SPEEX_MEMSET(exc, 0, nsf);
      return start;
   }
   
#ifdef FIXED_POINT
   /* Check if we need to scale everything down in the pitch search to avoid overflows */
   for (i=0;i<nsf;i++)
   {
      if (ABS16(target[i])>16383)
      {
         scaledown=1;
         break;
      }
   }
   for (i=-end;i<nsf;i++)
   {
      if (ABS16(exc2[i])>16383)
      {
         scaledown=1;
         break;
      }
   }
#endif
   if (N>end-start+1)
      N=end-start+1;
   if (end != start)
      open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack);
   else
      nbest[0] = start;
   
   ALLOC(best_exc, nsf, spx_sig_t);
   ALLOC(new_target, nsf, spx_word16_t);
   ALLOC(best_target, nsf, spx_word16_t);
   
   for (i=0;i<N;i++)
   {
      pitch=nbest[i];
      SPEEX_MEMSET(exc, 0, nsf);
      err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf,
                                 bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain, scaledown);
      if (err<best_err || best_err<0)
      {
         SPEEX_COPY(best_exc, exc, nsf);
         SPEEX_COPY(best_target, new_target, nsf);
         best_err=err;
         best_pitch=pitch;
         best_gain_index=cdbk_index;
      }
   }
   /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/
   speex_bits_pack(bits, best_pitch-start, params->pitch_bits);
   speex_bits_pack(bits, best_gain_index, params->gain_bits);
#ifdef FIXED_POINT
   *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain));
#else
   *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3];
#endif
   /*printf ("%f\n", cumul_gain);*/
   /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/
   SPEEX_COPY(exc, best_exc, nsf);
   SPEEX_COPY(target, best_target, nsf);
#ifdef FIXED_POINT
   /* Scale target back up if needed */
   if (scaledown)
   {
      for (i=0;i<nsf;i++)
         target[i]=SHL16(target[i],1);
   }
#endif
   return pitch;
}
#endif /* SPEEX_DISABLE_ENCODER */

void pitch_unquant_3tap(
spx_word16_t exc[],             /* Input excitation */
spx_word32_t exc_out[],         /* Output excitation */
int   start,                    /* Smallest pitch value allowed */
int   end,                      /* Largest pitch value allowed */
spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */
const void *par,
int   nsf,                      /* Number of samples in subframe */
int *pitch_val,
spx_word16_t *gain_val,
SpeexBits *bits,
char *stack,
int count_lost,
int subframe_offset,
spx_word16_t last_pitch_gain,
int cdbk_offset
)
{
    (void)end;
    (void)pitch_coef;
    (void)stack;
   int i;
   int pitch;
   int gain_index;
   spx_word16_t gain[3];
   const signed char *gain_cdbk;
   int gain_cdbk_size;
   const ltp_params *params;

   params = (const ltp_params*) par;
   gain_cdbk_size = 1<<params->gain_bits;
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;

   pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
   pitch += start;
   gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
   /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
#ifdef FIXED_POINT
   gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]);
   gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]);
   gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]);
#else
   gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5;
   gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5;
   gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5;
#endif

   if (count_lost && pitch > subframe_offset)
   {
      spx_word16_t gain_sum;
      if (1) {
#ifdef FIXED_POINT
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1);
         if (tmp>62)
            tmp=62;
#else
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain;
         if (tmp>.95)
            tmp=.95;
#endif
         gain_sum = gain_3tap_to_1tap(gain);

         if (gain_sum > tmp)
         {
            spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum);
            for (i=0;i<3;i++)
               gain[i]=MULT16_16_Q14(fact,gain[i]);
         }

      }

   }

   *pitch_val = pitch;
   gain_val[0]=gain[0];
   gain_val[1]=gain[1];
   gain_val[2]=gain[2];
   gain[0] = SHL16(gain[0],7);
   gain[1] = SHL16(gain[1],7);
   gain[2] = SHL16(gain[2],7);
   SPEEX_MEMSET(exc_out, 0, nsf);
   for (i=0;i<3;i++)
   {
      int j;
      int tmp1, tmp3;
      int pp=pitch+1-i;
      tmp1=nsf;
      if (tmp1>pp)
         tmp1=pp;
      for (j=0;j<tmp1;j++)
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]);
      tmp3=nsf;
      if (tmp3>pp+pitch)
         tmp3=pp+pitch;
      for (j=tmp1;j<tmp3;j++)
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]);
   }
   /*for (i=0;i<nsf;i++)
   exc[i]=PSHR32(exc32[i],13);*/
}


#ifndef SPEEX_DISABLE_ENCODER
/** Forced pitch delay and gain */
int forced_pitch_quant(
spx_word16_t target[],                 /* Target vector */
spx_word16_t *sw,
spx_coef_t ak[],                     /* LPCs for this subframe */
spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */
spx_coef_t awk2[],                   /* Weighted LPCs #2 for this subframe */
spx_sig_t exc[],                    /* Excitation */
const void *par,
int   start,                    /* Smallest pitch value allowed */
int   end,                      /* Largest pitch value allowed */
spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */
int   p,                        /* Number of LPC coeffs */
int   nsf,                      /* Number of samples in subframe */
SpeexBits *bits,
char *stack,
spx_word16_t *exc2,
spx_word16_t *r,
int complexity,
int cdbk_offset,
int plc_tuning,
spx_word32_t *cumul_gain
)
{
   int i;
   VARDECL(spx_word16_t *res);
   ALLOC(res, nsf, spx_word16_t);
#ifdef FIXED_POINT
   if (pitch_coef>63)
      pitch_coef=63;
#else
   if (pitch_coef>.99)
      pitch_coef=.99;
#endif
   for (i=0;i<nsf&&i<start;i++)
   {
      exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]);
   }
   for (;i<nsf;i++)
   {
      exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]);
   }
   for (i=0;i<nsf;i++)
      res[i] = EXTRACT16(PSHR32(exc[i], SIG_SHIFT-1));
   syn_percep_zero16(res, ak, awk1, awk2, res, nsf, p, stack);
   for (i=0;i<nsf;i++)
      target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),EXTEND32(res[i])),32700));
   return start;
}
#endif /* SPEEX_DISABLE_ENCODER */

/** Unquantize forced pitch delay and gain */
void forced_pitch_unquant(
spx_word16_t exc[],             /* Input excitation */
spx_word32_t exc_out[],         /* Output excitation */
int   start,                    /* Smallest pitch value allowed */
int   end,                      /* Largest pitch value allowed */
spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */
const void *par,
int   nsf,                      /* Number of samples in subframe */
int *pitch_val,
spx_word16_t *gain_val,
SpeexBits *bits,
char *stack,
int count_lost,
int subframe_offset,
spx_word16_t last_pitch_gain,
int cdbk_offset
)
{
    (void)end;
    (void)par;
    (void)bits;
    (void)stack;
    (void)count_lost;
    (void)subframe_offset;
    (void)last_pitch_gain;
    (void)cdbk_offset;
   int i;
#ifdef FIXED_POINT
   if (pitch_coef>63)
      pitch_coef=63;
#else
   if (pitch_coef>.99)
      pitch_coef=.99;
#endif
   for (i=0;i<nsf;i++)
   {
      exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7));
      exc[i] = EXTRACT16(PSHR32(exc_out[i],13));
   }
   *pitch_val = start;
   gain_val[0]=gain_val[2]=0;
   gain_val[1] = pitch_coef;
}
="hl num">1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 2, -1, 1, -1, 1, -1, 2}, { 1, 2, -1, 2, 1, -1, 1, -1}, { 1, -1, 1, -1, 2, -1, 2, -1}, { 2, 1, -1, 1, 2, -1, 2, -1}, {-1, 2, -1, 2, -1, 2, -1, 2}, { 1, 2, -1, 2, 1, -1, 1, -1}, { 1, -1, 1, -1, 2, -1, 1, -1}, { 2, 2, -1, 1, 1, -1, 2, -1}, {-1, 2, -1, 1, -1, 1, -1, 1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 7, 7, -1, -1, 5, 5, -1}, { 1, -1, -1, -1, -1, -1, 4, -1}, { 2, 1, -1, -1, -1, -1, 4, 3}, { 2, -1, -1, -1, -1, -1, 3, -1}, { 1, 2, -1, -1, -1, -1, 3, 4}, { 1, -1, -1, -1, -1, -1, 4, -1}, { 7, 1, -1, -1, -1, -1, 4, 5}, { 7, 7, -1, -1, -1, 5, 5, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 7, 7, -1, -1, -1, -1, 5, 5}, { 1, 5, -1, -1, -1, 7, 4, -1}, { 2, 1, -1, -1, -1, -1, 4, 3}, { 2, -1, -1, -1, -1, -1, 3, -1}, { 1, 5, -1, -1, -1, -1, 7, 4}, { 1, -1, -1, -1, -1, -1, 4, -1}, { 7, 1, -1, -1, -1, -1, 4, 5}, { 7, 5, -1, -1, -1, 7, 5, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 0, 0, -1, -1, -1}, {-1, -1, 5, 0, 1, -1, -1, -1}, {-1, -1, 3, 5, 1, 6, -1, -1}, {-1, 4, 3, 2, 6, 2, -1, -1}, {-1, 7, 4, 7, 2, 2, 4, -1}, { 6, 7, 7, 3, 3, 4, 3, -1}, { 1, 6, 1, 1, 1, 3, 3, 5}, { 1, 1, -1, -1, -1, -1, 5, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 0, -1, -1, 0, -1, -1}, {-1, 3, 3, -1, 3, 3, -1, -1}, {-1, 0, 2, 0, 0, 2, 0, -1}, {-1, 3, 3, -1, 3, 3, -1, -1}, {-1, -1, 0, -1, -1, 0, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 1, 1, -1, -1, -1}, {-1, -1, 2, 2, 2, -1, -1, -1}, {-1, -1, 3, 3, 3, 3, -1, -1}, {-1, 4, 4, 4, 4, 4, -1, -1}, {-1, 5, 5, 5, 5, 5, 5, -1}, {-1, -1, -1, 6, -1, -1, -1, -1}, {-1, -1, -1, 7, 7, -1, -1, -1}, {-1, -1, -1, 0, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 2, 5, -1, -1, -1}, {-1, 4, 3, -1, -1, -1, -1, -1}, { 6, 7, -1, 5, 2, -1, -1, -1}, {-1, -1, -1, -1, 3, 4, -1, -1}, {-1, -1, -1, 2, 5, -1, 7, 6}, {-1, 4, 3, -1, -1, -1, -1, -1}, { 6, 7, -1, 5, 2, -1, -1, -1}, {-1, -1, -1, -1, 3, 4, -1, -1}, {-1, -1, -1, -1, -1, -1, 7, 6}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 5, 5, -1, -1, -1}, {-1, -1, -1, 3, -1, -1, -1, -1}, {-1, -1, -1, 1, -1, -1, -1, -1}, {-1, -1, -1, 7, -1, -1, -1, -1}, {-1, -1, -1, 2, -1, -1, -1, -1}, {-1, -1, -1, 4, -1, -1, -1, -1}, {-1, -1, -1, 5, -1, -1, -1, -1}, {-1, -1, -1, 3, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 0, 1, -1, -1, -1}, {-1, -1, 0, 2, 7, 7, -1, -1}, {-1, -1, -1, 0, 1, 7, -1, -1}, {-1, 0, 0, 0, 0, -1, -1, -1}, {-1, 0, 0, 0, 1, 1, -1, -1}, { 0, 0, 0, 1, 1, 1, -1, -1}, {-1, 0, 0, 1, 1, 1, -1, -1}, {-1, 0, 0, 0, 7, 7, -1, -1}, {-1, -1, 7, 7, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 1, -1, -1, -1, -1, -1, -1}, { 1, -1, -1, -1, -1, -1, -1, -1}, {-1, 2, 3, 4, 7, 6, 5, -1}, {-1, -1, -1, -1, -1, -1, 1, -1}, {-1, -1, -1, -1, -1, -1, 1, -1}, {-1, 2, 3, 4, 7, 6, -1, -1}, {-1, 1, -1, -1, -1, -1, -1, -1}, { 1, -1, -1, -1, -1, -1, -1, -1}, {-1, 2, 3, 4, 7, 6, 5, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 6, -1, -1, -1, -1, -1, -1}, { 5, -1, -1, -1, -1, -1, -1, -1}, { 2, 3, 4, 7, 6, 5, 2, 3}, {-1, -1, -1, -1, -1, -1, 4, -1}, {-1, -1, -1, -1, -1, -1, 7, -1}, {-1, 4, 3, 2, 5, 6, -1, -1}, {-1, 7, -1, -1, -1, -1, -1, -1}, { 6, -1, -1, -1, -1, -1, -1, -1}, { 5, 2, 3, 4, 7, 6, 5, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 3, 2, 1, 0, 0, 1, 2, 3}, { 3, 2, 1, 0, 1, 2, 3, -1}, { 4, 3, 2, 1, 1, 2, 3, 4}, { 4, 3, 2, 1, 2, 3, 4, -1}, { 5, 4, 3, 2, 2, 3, 4, 5}, { 5, 4, 3, 2, 3, 4, 5, -1}, { 6, 5, 4, 3, 3, 4, 5, 6}, { 6, 5, 4, 3, 4, 5, 6, -1}, { 7, 6, 5, 4, 4, 5, 6, 7}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 5, 5, -1, -1, -1}, {-1, -1, -1, 3, -1, -1, -1, -1}, {-1, -1, -1, 2, 4, -1, -1, -1}, {-1, -1, -1, 6, -1, -1, -1, -1}, {-1, -1, -1, 2, 4, -1, -1, -1}, {-1, 2, -1, 5, -1, 4, -1, -1}, { 1, 0, 1, 0, 1, 0, 1, 0}, { 3, -1, 3, -1, 2, -1, 6, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, -1, 1, -1, -1, -1}, { 7, 4, 3, 5, -1, -1, -1, -1}, { 6, -1, -1, 1, -1, -1, -1, -1}, {-1, -1, -1, 5, 3, 4, 7, -1}, { 6, -1, -1, -1, 1, -1, -1, 6}, { 7, 4, 3, 5, -1, -1, -1, -1}, {-1, -1, -1, 1, -1, -1, -1, 6}, {-1, -1, -1, 5, 3, 4, 7, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, -1, 7, 3, 6, -1}, {-1, -1, 3, 7, 3, 6, 3, -1}, {-1, -1, 5, 7, 3, 6, 3, -1}, {-1, 6, 7, 3, 6, 7, -1, -1}, {-1, 7, 7, 3, 6, 1, -1, -1}, { 3, 7, 3, 6, 3, -1, -1, -1}, { 5, 6, 2, 7, 1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 5, -1, -1, -1, -1, -1, -1, 5}, { 5, -1, 6, 6, 6, -1, 5, -1}, {-1, 5, 4, -1, -1, 4, 5, -1}, {-1, 3, -1, -1, -1, 3, -1, -1}, {-1, 6, 0, -1, -1, 0, 6, -1}, {-1, 3, -1, -1, -1, 3, -1, -1}, {-1, -1, 4, -1, -1, 4, -1, -1}, {-1, -1, 6, 6, 6, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 7, 0, -1, -1, 0, 7, -1}, { 7, -1, 0, -1, 0, -1, 7, -1}, { 7, 1, -1, 0, 0, -1, 1, 7}, { 7, 1, 2, 0, 2, 1, 7, -1}, { 7, 6, 3, 2, 2, 3, 6, 7}, { 7, -1, 3, 2, 3, -1, 7, -1}, {-1, 7, 7, 3, 3, 7, 7, -1}, {-1, -1, -1, 3, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 3, -1, 1, -1, 7, -1, 6}, { 5, -1, 7, -1, 7, -1, 6, -1}, { 6, -1, 0, -1, 5, -1, 3, -1}, {-1, 2, -1, 1, -1, 5, -1, -1}, {-1, 4, -1, 3, -1, 4, -1, -1}, { 2, -1, 3, -1, 2, -1, -1, -1}, {-1, -1, 4, -1, 6, -1, -1, -1}, {-1, -1, -1, 5, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, -1, 1, -1, -1, -1}, {-1, -1, -1, -1, 3, -1, -1, -1}, { 6, 1, 3, 1, 2, 1, 4, 1}, {-1, -1, -1, -1, 6, -1, -1, -1}, {-1, -1, -1, 4, 1, -1, -1, -1}, {-1, -1, 1, -1, 3, -1, -1, -1}, {-1, -1, -1, 2, 1, -1, -1, -1}, {-1, -1, -1, -1, 4, -1, -1, -1}, {-1, -1, -1, 6, 1, -1, -1, -1}, {-1, -1, -1, 6, -1, -1, -1, -1}}, {{-1, -1, -1, 5, 4, -1, -1, -1}, {-1, -1, 4, 1, 0, -1, -1, -1}, {-1, -1, -1, 2, 3, -1, -1, -1}, {-1, 1, 4, -1, 2, 2, -1, -1}, {-1, 3, 1, 2, 5, 1, 4, -1}, {-1, 4, 2, -1, 0, 4, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, -1, 1, -1, -1, -1}, {-1, -1, -1, 1, -1, -1, -1, -1}, {-1, 2, -1, -1, 1, -1, 5, -1}, { 5, -1, -1, 1, -1, -1, 0, -1}, {-1, 6, -1, -1, 1, -1, 4, -1}, {-1, 0, -1, 1, -1, 5, -1, -1}, {-1, -1, 5, 5, 0, 1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 6, 3, -1, -1, -1}, {-1, -1, 3, 2, 6, -1, -1, -1}, {-1, -1, 2, 6, 3, 2, -1, -1}, {-1, 6, 3, 2, 6, 3, -1, -1}, {-1, 3, 2, 6, 3, 2, 6, -1}, { 2, 6, 3, 2, 6, 3, 2, -1}, { 6, 3, 2, 6, 3, 2, 6, 3}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 6, 6, 6, 6, 6, 6, 6, 6}, { 4, -1, -1, -1, -1, -1, -1, -1}, {-1, 3, 2, 5, 7, 6, 4, 3}, {-1, 5, -1, -1, -1, -1, -1, -1}, {-1, -1, 7, 6, 4, 3, 2, 5}, {-1, -1, 4, -1, -1, -1, -1, -1}, {-1, -1, -1, 3, 2, 5, 7, 6}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 1, -1, 7, -1, -1, 6, -1, 2}, { 6, -1, 1, -1, 6, 1, 3, -1}, {-1, 4, -1, 7, 2, -1, 7, -1}, { 2, 7, -1, -1, -1, 4, -1, -1}, { 6, -1, 3, 5, 0, 2, -1, 7}, { 1, -1, -1, -1, -1, -1, 1, -1}, {-1, 1, 4, 5, 7, 5, 1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 6, 6, 6, -1, -1, 6, 6, 6}, {-1, -1, 6, -1, 6, -1, -1, -1}, {-1, -1, 2, 3, 3, 2, -1, -1}, {-1, 3, -1, 5, -1, 3, -1, -1}, {-1, -1, 5, 3, 3, 5, -1, -1}, {-1, -1, 6, 1, 6, -1, -1, -1}, {-1, 4, 2, -1, -1, 2, 4, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 5, 5, -1, -1, -1}, {-1, -1, 5, -1, -1, -1, -1, -1}, {-1, 3, 4, 6, 6, -1, -1, 5}, { 3, 3, 4, 6, 5, -1, 5, -1}, { 3, 2, 3, 6, 6, 5, 5, -1}, { 3, 3, 4, 6, 5, -1, 5, -1}, {-1, 3, 4, 6, 6, -1, -1, 5}, {-1, -1, 5, -1, -1, -1, -1, -1}, {-1, -1, -1, 5, 5, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 1, -1, -1, -1, -1, -1, -1, 1}, { 1, -1, 2, 2, 2, -1, 1, -1}, {-1, 1, 2, 3, 3, 2, 1, -1}, { 6, 2, 3, -1, 3, 2, 6, -1}, { 6, 2, 3, -1, -1, 3, 2, 6}, { 6, 2, 3, -1, 3, 2, 6, -1}, { 3, 3, 3, 7, 7, 3, 3, 3}, { 0, 5, 0, 2, 0, 5, 0, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 7, 7, 7, -1, -1, -1}, {-1, 7, 2, 2, 7, -1, -1, -1}, {-1, 7, 5, 5, 5, 7, -1, -1}, { 7, 7, 7, 7, 7, 7, -1, -1}, {-1, -1, 6, -1, 6, -1, -1, -1}, {-1, 6, -1, -1, 6, -1, -1, -1}, {-1, 6, 4, 4, -1, 6, 4, 4}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 3, 3, -1, 3, 3, 3, -1}, { 3, 7, 5, 4, 6, 5, 3, -1}, { 1, 3, 3, 3, -1, 3, 3, 1}, { 2, 1, 2, 1, 2, 1, 2, -1}, { 1, 3, 3, -1, 3, 3, 3, 1}, { 3, 5, 6, 4, 5, 7, 3, -1}, { 2, 3, 3, 3, -1, 3, 3, 2}, { 1, 1, 2, 2, 2, 1, 1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 6, 5, -1, -1, -1, -1, -1}, { 3, 1, 3, -1, -1, -1, -1, -1}, {-1, 5, 6, -1, -1, -1, -1, -1}, {-1, -1, 5, 3, -1, -1, -1, -1}, {-1, -1, 6, 1, 6, -1, -1, -1}, {-1, -1, 3, 5, -1, -1, -1, -1}, {-1, -1, -1, -1, 3, 6, -1, -1}, {-1, -1, -1, 5, 6, 5, -1, -1}, {-1, -1, -1, -1, 6, 3, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 6, 3, 7, 4, 5, 1, 6, 3}, { 5, 1, 6, 3, 7, 4, 5, -1}, { 6, 3, 7, 4, 5, 1, 6, 3}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, -1, -1, -1, 4, 4}, {-1, -1, 7, 7, 7, 4, 4, -1}, {-1, -1, -1, -1, -1, -1, 4, 4}, {-1, 1, -1, -1, -1, 7, -1, -1}, {-1, 1, 1, -1, -1, 7, -1, -1}, { 3, 3, 3, -1, 7, -1, -1, -1}, { 3, -1, 2, 3, 3, 3, -1, 3}, {-1, 2, -1, 3, -1, 3, 3, -1}, {-1, 2, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 4, -1, -1, -1, -1, -1}, {-1, 7, 4, -1, -1, -1, -1, -1}, {-1, -1, 7, 4, -1, -1, -1, -1}, {-1, 4, 7, 4, -1, -1, -1, -1}, { 1, 1, 1, 1, 1, 1, 1, -1}, { 1, 2, 1, 2, 1, 1, -1, -1}, { 2, 2, 2, 2, 2, 2, 2, 2}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 0, -1, -1, -1, -1, -1, -1, 6}, { 6, 1, 4, 3, 7, 5, 0, -1}, { 0, -1, -1, -1, -1, -1, -1, 6}, { 6, 1, 4, 3, 7, 5, 0, -1}, { 0, -1, -1, -1, -1, -1, -1, 6}, { 6, 1, 4, 3, 7, 5, 0, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 3, 3, 4, 6, 6, 4, 3, 3}, { 0, 3, 4, 6, 4, 3, 1, -1}, { 5, 1, 3, 4, 4, 3, 0, 1}, { 0, 1, 3, 4, 3, 1, 0, -1}, { 2, 1, 6, 3, 3, 0, 0, 1}, { 0, 3, 4, 3, 6, 1, 5, -1}, { 6, 1, 2, 6, 4, 0, 0, 2}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 6, 6, -1, -1, -1, -1, 4, 4}, { 4, 0, -1, -1, -1, 3, 6, -1}, { 0, 6, -1, -1, -1, -1, 4, 2}, { 7, -1, -1, -1, -1, -1, 7, -1}, { 4, 4, -1, -1, -1, -1, 5, 6}, { 6, 4, 7, 7, 5, 6, 4, -1}, {-1, 7, 6, 4, 6, 4, 7, -1}, {-1, 0, -1, 7, -1, 7, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 5, -1, -1, -1, -1, 4, -1}, {-1, 5, -1, -1, -1, 4, -1, -1}, {-1, -1, 5, 6, 6, 4, -1, -1}, {-1, -1, 2, -1, 2, -1, -1, -1}, { 0, 0, 6, -1, -1, 6, 1, 1}, {-1, -1, 2, -1, 2, -1, -1, -1}, {-1, -1, 7, 6, 6, 3, -1, -1}, {-1, 7, -1, -1, -1, 3, -1, -1}, {-1, 7, -1, -1, -1, -1, 3, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 6, -1, -1, -1, -1, 2, -1}, { 1, 7, 1, 1, 1, 3, 1, -1}, {-1, -1, 4, 1, 1, 4, -1, -1}, {-1, 1, 3, 1, 7, 1, -1, -1}, {-1, -1, -1, 2, 6, -1, -1, -1}, {-1, -1, 1, 5, 1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 7, 7, 7, 7, 7, 7, 7, 7}, { 7, -1, -1, -1, -1, -1, 7, -1}, { 7, -1, -1, 2, 0, 5, 2, 2}, { 7, -1, -1, -1, 0, 3, 6, -1}, { 7, -1, -1, -1, -1, -1, 4, 0}, { 5, 5, -1, -1, -1, -1, -1, -1}, { 4, 3, 6, 2, -1, -1, -1, -1}, { 0, 2, 0, 4, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 1, -1, -1, 1, -1, -1}, {-1, 4, -1, -1, 5, -1, -1, -1}, {-1, 7, -1, -1, 1, 1, 1, -1}, { 6, -1, -1, -1, -1, 7, -1, -1}, { 1, 1, 1, 1, -1, 4, -1, -1}, {-1, -1, 5, -1, -1, -1, -1, -1}, {-1, -1, 0, -1, -1, -1, -1, -1}, {-1, 3, -1, -1, -1, -1, -1, -1}, {-1, 1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 7, 7, -1, -1, 7, 7, -1}, { 6, -1, 4, -1, 4, -1, 6, -1}, { 5, -1, -1, 3, 3, -1, -1, 5}, { 6, -1, -1, -1, -1, -1, 6, -1}, {-1, 7, -1, -1, -1, -1, 7, -1}, {-1, 4, -1, -1, -1, 4, -1, -1}, {-1, -1, 3, -1, -1, 3, -1, -1}, {-1, -1, 2, -1, 2, -1, -1, -1}, {-1, -1, -1, 5, 5, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 0, 0, -1, -1, 0, 0, -1}, { 7, 4, 6, 6, 6, 4, 3, -1}, { 5, 6, 6, 6, 2, 6, 6, 3}, { 7, 4, 6, 6, 6, 4, 3, -1}, {-1, 0, 0, -1, -1, 0, 0, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, -1, -1, 7, 7, 7}, {-1, -1, -1, -1, 2, 7, 7, -1}, {-1, 0, 7, 7, 7, -1, 7, 7}, { 6, 7, 7, 7, -1, -1, -1, -1}, { 6, -1, -1, -1, 7, 7, 7, 7}, { 6, -1, -1, -1, -1, -1, -1, -1}, { 4, 2, 2, 2, 4, -1, 3, -1}, { 4, 4, 4, 4, 3, 3, 3, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 4, -1, -1, 7, -1, 6, -1, 7}, { 7, 6, 7, -1, -1, 7, 4, -1}, {-1, -1, 7, -1, -1, 7, -1, -1}, {-1, 0, 0, 0, 0, 0, 3, -1}, {-1, -1, 0, 2, 2, 0, 6, 4}, {-1, -1, 0, 0, 0, 1, 3, -1}, {-1, -1, -1, 0, 0, -1, 3, 4}, {-1, -1, -1, 6, -1, 5, 6, -1}, {-1, -1, -1, -1, -1, -1, 1, 0}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 5, -1, -1, -1, -1, 5, -1}, { 0, -1, -1, 0, -1, -1, 0, -1}, { 0, 0, 0, 2, 2, 0, 0, 0}, { 0, -1, -1, 0, -1, -1, 0, -1}, {-1, 7, -1, 3, -1, -1, 7, -1}, {-1, -1, 3, 6, -1, -1, -1, -1}, {-1, -1, -1, 6, -1, -1, -1, -1}, {-1, 3, 6, -1, -1, -1, -1, -1}, {-1, 3, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 6, 5, -1, -1, -1}, {-1, -1, 2, 6, 3, -1, -1, -1}, {-1, -1, 5, 4, 7, 1, -1, -1}, {-1, 6, 2, 2, 3, 4, -1, -1}, {-1, -1, 3, 7, 3, 6, -1, -1}, {-1, -1, 1, 3, 2, -1, -1, -1}, {-1, -1, -1, 4, 5, -1, -1, -1}, {-1, -1, -1, 4, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 7, 7, -1, 2, 2, -1, 6, 6}, { 6, -1, -1, 6, -1, -1, 3, -1}, { 2, -1, -1, 1, -1, -1, 2, -1}, { 5, -1, -1, 3, -1, -1, 2, -1}, { 1, -1, -1, 2, -1, -1, 1, -1}, { 5, -1, -1, 2, -1, -1, 2, -1}, { 6, -1, -1, 1, -1, -1, 7, -1}, { 5, -1, -1, 5, -1, -1, 4, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 6, 6, -1, -1, -1}, {-1, 0, 4, 4, 4, 0, -1, -1}, {-1, -1, -1, 6, 6, -1, -1, -1}, {-1, -1, 2, 7, 2, -1, -1, -1}, {-1, -1, -1, 6, 6, -1, -1, -1}, {-1, 0, 5, 5, 5, 0, -1, -1}, {-1, -1, -1, 3, 3, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 4, 1, 3, -1, -1, -1}, {-1, 1, -1, -1, 1, -1, -1, -1}, {-1, -1, 4, 1, 3, 4, 1, -1}, {-1, 1, 3, 4, -1, -1, 4, -1}, {-1, 3, -1, -1, 3, 4, 1, -1}, {-1, 1, 3, 4, 1, 3, -1, -1}, {-1, -1, 4, 1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 6, 4, -1, 3, 2, 5, -1}, { 0, -1, -1, -1, -1, -1, 1, -1}, {-1, 2, 3, 5, -1, 4, 6, -1}, { 0, -1, -1, -1, -1, -1, 1, -1}, {-1, 4, 6, -1, 2, 5, 3, -1}, { 0, -1, -1, -1, -1, -1, 1, -1}, {-1, 5, 2, 3, -1, 4, 6, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 6, 6, -1, -1, -1}, {-1, -1, 7, 6, 4, -1, -1, -1}, {-1, 2, 1, 7, 4, 1, 3, -1}, { 2, 1, 1, 1, 1, 1, 3, -1}, {-1, 2, 2, 2, 3, 3, 3, -1}, {-1, -1, -1, 5, -1, -1, -1, -1}, {-1, -1, -1, 2, 3, -1, -1, -1}, {-1, -1, -1, 5, -1, -1, -1, -1}, {-1, -1, 2, 2, 3, 3, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 4, -1, 5, -1, -1, 3, -1, 6}, { 2, -1, 3, -1, 2, -1, 4, -1}, { 4, -1, -1, 1, 0, -1, -1, 6}, { 6, -1, 2, 3, 5, -1, 4, -1}, { 4, -1, -1, 0, 1, -1, -1, 6}, { 2, -1, 5, -1, 3, -1, 4, -1}, { 4, -1, 3, -1, -1, 2, -1, 6}, { 6, -1, -1, -1, -1, -1, 4, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 2, 6, 0, 5, 5, 1, 3, 4}, { 1, -1, -1, 2, -1, -1, 0, -1}, { 4, -1, -1, 3, 6, -1, -1, 2}, {-1, -1, -1, 0, -1, -1, -1, -1}, {-1, -1, -1, 1, 4, -1, -1, -1}, {-1, -1, -1, 2, -1, -1, -1, -1}, {-1, -1, -1, 6, 3, -1, -1, -1}, {-1, -1, -1, 5, -1, -1, -1, -1}, {-1, -1, -1, 4, 1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, -1, 5, 1, 1, 3}, { 0, 5, 1, 0, 5, 3, 3, -1}, { 5, 1, 0, 5, 1, 0, 5, 1}, { 0, 5, 1, 0, 5, 1, 6, -1}, {-1, -1, -1, -1, 1, 6, 5, 1}, {-1, -1, -1, -1, 5, 1, 6, -1}, {-1, -1, -1, -1, 1, 0, 5, 1}, {-1, -1, -1, -1, 5, 1, 0, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 0, 7, 3, -1, -1, 2, 2}, {-1, 0, 7, 3, -1, -1, 2, -1}, {-1, 0, 7, 3, -1, -1, 2, 2}, {-1, 0, 7, 3, -1, 3, 1, -1}, {-1, 0, 7, 3, -1, 6, 4, 5}, {-1, 0, 7, 3, -1, 7, 0, -1}, {-1, 0, 7, 3, -1, 2, 3, 4}, {-1, 0, 7, 3, -1, 5, 6, -1}, {-1, -1, -1, -1, -1, 7, 0, 1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 7, 7, 7, 7, -1}, { 3, 4, 5, -1, -1, -1, 7, -1}, { 2, -1, -1, -1, -1, -1, -1, 3}, { 7, -1, -1, -1, -1, -1, 4, -1}, { 7, -1, -1, -1, 3, 4, 5, 6}, { 7, -1, -1, 2, 0, 1, 2, -1}, { 6, -1, -1, -1, 3, 4, 5, 6}, { 0, 1, -1, -1, -1, -1, -1, -1}, { 2, 3, 4, -1, -1, -1, -1, -1}, { 5, 6, 0, -1, -1, -1, -1, -1}}, {{-1, 7, -1, -1, -1, -1, 2, -1}, { 1, 1, -1, -1, -1, 3, 3, -1}, {-1, 2, -1, -1, -1, -1, 4, -1}, { 3, 3, -1, -1, -1, 5, 5, -1}, {-1, 4, -1, -1, -1, -1, 6, -1}, { 5, 5, -1, -1, -1, 1, 1, -1}, {-1, 6, -1, -1, -1, -1, 7, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 4, -1, -1, -1, -1, 4, -1}, { 2, -1, -1, 1, -1, -1, 2, -1}, { 5, -1, -1, 0, 0, -1, -1, 5}, { 5, -1, -1, 1, -1, -1, 6, -1}, {-1, 4, 2, 7, 7, 5, 4, -1}, {-1, -1, -1, 6, -1, -1, -1, -1}, {-1, -1, -1, 3, 3, -1, -1, -1}, {-1, -1, -1, 7, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 1, -1, -1, 2, 3, 4, -1}, { 2, -1, -1, 3, 0, 4, -1, -1}, { 4, -1, -1, 2, 3, 1, -1, -1}, { 3, -1, 4, 3, 0, -1, -1, -1}, { 4, -1, -1, 2, 5, 1, -1, -1}, { 3, -1, 4, 5, 0, 4, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 2, -1, -1, 1, 1, -1, -1, 2}, { 2, -1, 3, 3, 3, -1, 2, -1}, {-1, 2, -1, 4, 4, -1, 2, -1}, {-1, 7, 7, 0, 7, 7, -1, -1}, {-1, -1, -1, 4, 4, -1, -1, -1}, {-1, -1, 5, 7, 5, -1, -1, -1}, { 6, 3, 2, 6, 4, 2, 3, 6}, { 5, -1, -1, -1, -1, -1, 1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 4, 2, 3, 5, 7, 1, 3, 6}, { 1, -1, -1, 1, -1, -1, 1, -1}, { 3, 0, 1, 3, 2, 4, 3, 5}, { 4, -1, -1, 4, -1, -1, 4, -1}, {-1, 5, -1, -1, 5, -1, -1, 5}, { 0, 3, 2, 0, 4, 5, 0, -1}, {-1, 6, -1, -1, 6, -1, -1, 6}, { 7, -1, -1, 7, -1, -1, 7, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 5, 4, -1, 1, 1, -1, -1}, { 5, -1, 4, 1, -1, 1, -1, -1}, { 0, -1, -1, -1, -1, -1, 0, -1}, { 0, 6, 4, -1, -1, 4, 2, -1}, {-1, 4, 3, 5, 2, 6, 3, 6}, {-1, 2, 6, -1, -1, 5, 4, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 6, 6, -1, -1, -1}, {-1, -1, 5, 5, 4, -1, -1, -1}, {-1, -1, 1, 6, 6, 4, -1, -1}, {-1, 1, 7, 2, 5, 3, -1, -1}, {-1, 2, 7, 2, 1, 5, 3, -1}, { 2, 1, 3, 1, 4, 2, 7, -1}, {-1, 3, 1, 3, 4, 2, 7, -1}, {-1, 3, 5, 5, 6, 6, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 7, 3, -1, -1, -1, -1}, {-1, 1, 7, 6, -1, -1, -1, -1}, {-1, 3, 7, 5, 1, 5, -1, -1}, { 7, 7, 0, 2, 4, 0, 4, -1}, { 7, 1, 4, 6, 5, 6, 5, 7}, { 1, 7, 7, 1, 7, 7, 1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 1, -1, -1, 1, -1, -1}, {-1, 5, 6, 1, 5, 6, -1, -1}, {-1, 1, 1, 2, 2, 1, 1, -1}, { 4, 7, 1, 0, 1, 7, 4, -1}, {-1, 3, 7, 5, 7, 5, 3, -1}, {-1, 1, 1, 1, 1, 1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 4, -1, -1, -1, 5, -1, -1, 4}, { 6, 6, 7, 6, -1, 4, 5, -1}, { 4, 2, 7, 5, 2, 2, 6, 4}, {-1, -1, 4, 1, -1, 5, 2, -1}, {-1, 5, 2, 7, 7, -1, 7, 4}, { 4, 6, 5, 4, -1, 4, 2, -1}, {-1, -1, -1, 4, -1, 4, 1, -1}, { 0, 0, 0, 5, -1, -1, -1, -1}, {-1, -1, -1, -1, 0, 0, 0, 0}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 1, -1, -1, -1, 0, 0, -1, -1}, { 2, -1, -1, 0, 1, 0, -1, -1}, { 3, -1, -1, 0, 2, 2, 0, -1}, { 4, -1, 0, 1, 1, 1, 0, -1}, { 5, -1, -1, 0, 4, 4, 0, -1}, { 6, -1, -1, 4, 4, 4, -1, -1}, { 7, -1, -1, -1, 4, 4, -1, -1}, {-1, -1, -1, 0, 1, 0, -1, -1}, {-1, -1, -1, 0, 1, 1, 0, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 3, -1, -1, 1, 7, -1}, {-1, 7, 4, -1, -1, 4, 3, -1}, { 1, -1, -1, 0, 2, 0, -1, -1}, { 5, 4, -1, 3, -1, -1, -1, -1}, { 4, -1, 3, 6, 1, 1, 6, -1}, {-1, 1, -1, -1, 4, -1, 1, -1}, {-1, 7, 5, -1, -1, -1, 3, -1}, {-1, -1, 3, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 1, -1, -1, -1, 1, -1, -1, -1}, { 2, -1, -1, -1, 2, -1, -1, -1}, {-1, 3, -1, -1, 3, 3, -1, -1}, {-1, 4, -1, 4, -1, 4, -1, -1}, {-1, 5, -1, -1, 5, 5, -1, -1}, { 6, -1, -1, 7, 1, 7, -1, -1}, { 7, -1, -1, -1, 6, 6, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 2, -1, -1, 6, -1, 2, 5, 1}, { 5, -1, 4, -1, 4, -1, 4, -1}, { 6, -1, -1, 3, -1, -1, -1, 3}, { 4, 2, 0, -1, -1, -1, 5, -1}, {-1, -1, -1, 6, -1, 3, 6, -1}, {-1, -1, 5, -1, 5, -1, -1, -1}, {-1, -1, -1, 3, -1, 4, 2, 5}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 6, -1, -1, -1, 4, -1, -1, 3}, { 0, 3, -1, -1, 6, -1, 0, -1}, {-1, -1, 7, -1, 1, -1, 3, -1}, { 7, -1, 4, 7, -1, 2, -1, -1}, { 5, 2, 3, 2, 1, 6, -1, 3}, {-1, -1, 0, 4, 3, 5, 4, -1}, {-1, 7, 6, -1, -1, 0, -1, -1}, { 4, 3, -1, -1, -1, 4, 2, -1}, { 0, -1, -1, -1, -1, -1, 6, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 6, 1, 2, 5, 1, 6, 3, 0}, {-1, -1, -1, -1, -1, -1, 4, -1}, { 0, 5, 2, 7, 1, 6, 2, -1}, { 3, -1, -1, -1, -1, -1, -1, -1}, { 6, 7, 6, 4, 0, 5, 2, 6}, {-1, -1, -1, -1, -1, -1, 1, -1}, { 6, 1, 4, 0, 6, 2, 3, -1}, { 0, -1, -1, -1, -1, -1, -1, -1}, {-1, 0, 4, 5, 3, 7, 6, 0}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 0, 1, -1, -1, -1}, {-1, -1, 0, 7, 0, -1, -1, -1}, {-1, -1, 1, 2, 2, 0, -1, -1}, {-1, 0, 7, 0, 7, 0, -1, -1}, {-1, 6, -1, 7, 7, -1, 6, -1}, { 4, 1, 6, 6, 6, 4, 1, -1}, {-1, 5, -1, 7, 7, -1, 5, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 5, 6, -1, -1, -1}, {-1, -1, 3, 3, 3, -1, -1, -1}, {-1, -1, 7, 5, 3, 7, -1, -1}, {-1, 3, -1, 6, -1, 3, -1, -1}, { 2, -1, -1, 3, 7, -1, -1, 1}, { 2, 2, -1, 3, -1, 1, 1, -1}, {-1, 0, 2, 5, 6, 1, 0, -1}, {-1, -1, -1, 3, -1, -1, -1, -1}, {-1, -1, -1, 3, 7, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 6, -1, -1, -1, -1, 2, -1}, {-1, 2, 6, 0, 6, 0, -1, -1}, {-1, 0, -1, -1, -1, -1, -1, -1}, { 6, -1, -1, -1, -1, -1, -1, -1}, {-1, 3, 3, 2, 0, 6, 0, 0}, {-1, 6, -1, -1, -1, -1, 0, -1}, {-1, -1, -1, 6, 0, 2, 6, -1}, {-1, 2, 0, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 0, 7, -1, -1, -1, -1, -1, -1}, { 1, 5, -1, -1, -1, -1, -1, -1}, { 7, 2, 5, -1, -1, -1, -1, -1}, { 6, 3, 4, -1, -1, -1, -1, -1}, { 5, 5, 4, 4, -1, -1, -1, -1}, { 3, 3, 5, 3, -1, -1, -1, -1}, { 1, 2, 2, 5, 3, -1, -1, -1}, { 1, 0, 0, 7, 6, -1, -1, -1}, { 3, 3, 5, 5, 7, 6, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 2, 6, 6, 2, -1, -1}, {-1, 2, 1, 1, 0, 2, -1, -1}, {-1, 2, 3, 2, 2, 0, 2, -1}, { 2, 3, 2, 5, 2, 7, 2, -1}, { 2, 4, 2, 5, 2, 7, 2, 0}, { 2, 4, 2, 6, 6, 2, 0, -1}, {-1, 2, 5, 2, 2, 2, 7, 2}, {-1, 2, 5, 6, 6, 7, 2, -1}, {-1, -1, 2, 2, 2, 2, 2, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 0, -1, -1, 0, -1, -1}, { 1, 0, 0, 1, 0, 0, 1, -1}, { 1, 7, 7, 5, 5, 7, 7, 1}, { 3, 2, -1, 2, -1, 2, 3, -1}, { 3, 7, -1, 6, 6, -1, 7, 3}, { 7, -1, -1, 6, -1, -1, 7, -1}, { 4, 4, 5, -1, -1, 5, 4, 4}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 6, 3, -1, -1, 3, 6, -1}, { 6, -1, 2, -1, 2, -1, 6, -1}, { 2, -1, 0, 1, 1, 0, -1, 2}, { 5, 0, -1, 7, -1, 0, 5, -1}, {-1, 5, -1, 6, 6, -1, 5, -1}, { 7, 1, 4, -1, 4, 1, 7, -1}, { 7, -1, 4, -1, -1, 4, -1, 7}, { 2, 0, -1, -1, -1, 0, 2, -1}, {-1, 2, -1, -1, -1, -1, 2, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 6, 1, -1, -1, -1, -1, 4, 0}, { 2, 7, 5, 5, 5, 7, 3, -1}, { 6, 1, -1, -1, -1, -1, 4, 0}, { 2, 5, 7, 7, 7, 5, 3, -1}, { 6, 1, -1, -1, -1, -1, 4, 0}, { 2, 0, 6, 6, 6, 0, 3, -1}, { 6, 1, -1, -1, -1, -1, 4, 0}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 5, -1, -1, 1, 1, -1, -1, 5}, { 5, -1, 4, -1, 4, -1, 5, -1}, {-1, 2, 4, -1, -1, 4, 2, -1}, { 7, 2, -1, -1, -1, 2, 7, -1}, { 0, -1, 0, 4, 4, 0, -1, 0}, { 7, 2, -1, -1, -1, 2, 7, -1}, {-1, 2, 3, -1, -1, 3, 2, -1}, { 5, -1, 3, -1, 3, -1, 5, -1}, { 5, -1, -1, 6, 6, -1, -1, 5}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 2, 2, -1, -1, -1, -1, 5, 5}, { 5, -1, -1, -1, -1, -1, 2, -1}, { 5, -1, -1, -1, -1, -1, -1, 2}, { 1, -1, 1, 5, 1, -1, 3, -1}, { 5, 2, 5, 3, 1, 2, 5, 2}, { 2, 0, 5, -1, 2, 0, 5, -1}, {-1, 3, 7, -1, -1, 3, 7, -1}, {-1, -1, 2, 0, 5, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 0, 6, 5, 2, 3, 4, 1, 7}, {-1, -1, -1, -1, 1, -1, -1, -1}, {-1, -1, -1, 1, 1, -1, -1, -1}, {-1, -1, 1, -1, -1, -1, -1, -1}, { 7, 1, 4, 3, 2, 5, 6, 0}, {-1, -1, -1, -1, 1, -1, -1, -1}, {-1, -1, -1, 1, 1, -1, -1, -1}, {-1, -1, 1, -1, -1, -1, -1, -1}, { 0, 6, 5, 2, 3, 4, 1, 7}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, 1, -1, -1, 1, -1, -1}, {-1, 2, 4, -1, 2, 4, -1, -1}, {-1, 2, 3, 6, 5, 3, 2, -1}, {-1, 6, 5, -1, 6, 5, -1, -1}, {-1, -1, -1, 7, 7, -1, -1, -1}, {-1, -1, -1, 7, -1, -1, -1, -1}, { 1, -1, -1, 7, 7, -1, -1, 3}, { 2, -1, -1, 7, -1, -1, 2, -1}, {-1, 3, 4, 5, 6, 4, 1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 1, -1, -1, 2, 2, -1, -1, 2}, { 1, 3, 7, 3, 7, 4, 2, -1}, {-1, 1, 6, -1, -1, 6, 2, -1}, { 6, -1, 7, 3, 7, -1, 6, -1}, {-1, 4, 2, -1, -1, 1, 3, -1}, {-1, -1, 2, 6, 1, -1, -1, -1}, {-1, 4, 3, 3, 4, 4, 3, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, -1, -1, 5, 6, -1, -1, -1}, {-1, -1, -1, 3, -1, -1, -1, -1}, {-1, -1, -1, 1, 2, -1, -1, -1}, {-1, -1, -1, 4, -1, -1, -1, -1}, {-1, -1, -1, 5, 7, -1, -1, -1}, {-1, -1, -1, 2, -1, -1, -1, -1}, { 6, 5, 4, 3, 2, 1, 7, 5}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{-1, 0, -1, 1, -1, 2, -1, -1}, {-1, 4, -1, 5, -1, 6, -1, -1}, {-1, 7, -1, 0, -1, 2, -1, -1}, {-1, 6, -1, 3, -1, 6, -1, -1}, {-1, 1, -1, 1, -1, 2, -1, -1}, {-1, 3, -1, 5, -1, 0, -1, -1}, {-1, 2, -1, 4, -1, 6, -1, -1}, {-1, 3, -1, 6, -1, 7, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 1, 1, 2, 2, 3, 3, 4, 4}, { 5, 5, 6, 7, 6, 5, 5, -1}, { 6, 4, 3, 3, 2, 2, 1, 6}, { 4, 6, 5, 7, 6, 3, 1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 7, 4, -1, 1, 2, -1, 4, 7}, { 5, 5, -1, 2, -1, 4, 4, -1}, {-1, 5, -1, 7, 7, -1, 4, -1}, { 1, 0, 6, 7, 6, 0, 2, -1}, {-1, 2, -1, 5, 3, -1, 1, -1}, { 1, 1, -1, -1, -1, 2, 2, -1}, { 6, 1, 4, -1, -1, 4, 2, 6}, { 5, 3, -1, -1, -1, 3, 5, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 1, 5, 1, 0, 0, 1, 5, 1}, { 1, 2, 5, -1, 5, 2, 1, -1}, { 3, 6, 1, 2, 2, 1, 6, 3}, { 4, 3, 4, -1, 4, 3, 4, -1}, { 3, 4, 6, 5, 5, 6, 4, 3}, { 0, 2, 3, -1, 3, 2, 0, -1}, { 2, 3, 1, 5, 5, 1, 3, 2}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}}, {{ 3, 0, 2, 7, 5, 7, 6, 5}, { 6, -1, 1, -1, 2, -1, 1, -1}, {-1, 6, 4, 0, 3, 4, 5, -1}, {-1, 5, -1, 1, -1, 4, -1, -1}, {-1, 7, 3, 5, 6, 5, 3, -1}, { 1, -1, 2, -1, 4, -1, 2, -1}, { 6, 4, 4, 6, 6, 5, 5, 1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}} }; /* the tile struct * type is the bubble number 0-7 * fallx is the x axis movement for the falling bubble * fallvel is the initial upward velocity for the falling bubble * ingroup denotes a bubble that is part of a group to be removed * anchored denotes a bubble that is anchored to the ceiling */ struct tile { int type; int fallx; int fallvel; bool ingroup; bool anchored; bool delete; }; /* the game context struct * score is the current score * level is the current level * angle is the current cannon direction * shots is the number of shots fired since last compression * compress is the height of the compressor * onboardcnt is the number of unique bubbles on the playing board * onboard is the unique bubbles on the playing board * nextinq is the pointer to the next bubble in the firing queue * queue is the circular buffer of bubbles to be fired * elapsedlvl is level elapsed time in 1/100s of seconds * elapsedshot is the shot elapsed time in 1/100s of seconds * startedshot is when the current shot began * playboard is the game playing board */ struct game_context { unsigned int score; unsigned int level; int angle; int shots; int compress; int onboardcnt; int onboard[NUM_BUBBLES]; int nextinq; int queue[NUM_QUEUE]; long elapsedlvl; long elapsedshot; long startedshot; struct tile playboard[BB_HEIGHT][BB_WIDTH]; }; struct highscore highscores[NUM_SCORES]; /* used to denote available resume info */ static bool resume = false; static unsigned int highlevel = 0; /* the highest level beaten */ static unsigned int last_highlevel = 0; static void bubbles_init(struct game_context* bb); static bool bubbles_nextlevel(struct game_context* bb); static void bubbles_getonboard(struct game_context* bb); static void bubbles_drawboard(struct game_context* bb); static int bubbles_fire(struct game_context* bb); static bool bubbles_collision(struct game_context* bb, int y, int x, int nearrow, int nearcol); static bool bubbles_ingroup(struct game_context* bb, int row, int col); static int bubbles_searchgroup(struct game_context* bb, int row, int col); static int bubbles_remove(struct game_context* bb); static void bubbles_anchored(struct game_context* bb, int row, int col); static int bubbles_fall(struct game_context* bb); static int bubbles_checklevel(struct game_context* bb); static void bubbles_recordscore(struct game_context* bb); static bool bubbles_loadgame(struct game_context* bb); static void bubbles_savegame(struct game_context* bb); static inline void bubbles_setcolors(void); static void bubbles_callback(void* param); static int bubbles_handlebuttons(struct game_context* bb, bool animblock, int timeout); static int bubbles(struct game_context* bb); /***************************************************************************** * bubbles_init() initializes bubbles data structures. ******************************************************************************/ static void bubbles_init(struct game_context* bb) { bubbles_setcolors(); /* seed the rand generator */ rb->srand(*rb->current_tick); /* check for resumed game */ if (resume) { resume = false; return; } bb->score = 0; bubbles_nextlevel(bb); } /***************************************************************************** * bubbles_nextlevel() sets up the game for the next level, returns false if * there are no more levels. ******************************************************************************/ static bool bubbles_nextlevel(struct game_context* bb) { int i, j, pos; bb->level++; /* check if there are no more levels */ if(bb->level > NUM_LEVELS) return false; /* save highest level */ if (bb->level-1 > highlevel) highlevel = bb->level-1; /* set up the play board */ rb->memset(bb->playboard, 0, sizeof(bb->playboard)); for(i=0; i<BB_LEVEL_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { pos = (int)level[bb->level-1][i][j]; if(pos >=0 && pos < NUM_BUBBLES) { bb->playboard[i][j].type = pos; } else { bb->playboard[i][j].type = -1; } } } for(i=BB_LEVEL_HEIGHT; i<BB_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { bb->playboard[i][j].type = -1; } } /* fill first bubbles in shot queue */ bubbles_getonboard(bb); for(i=0; i<NUM_QUEUE; i++) { bb->queue[i] = bb->onboard[rb->rand()%bb->onboardcnt]; } bb->angle = 0; bb->shots = 0; bb->compress = 0; bb->nextinq = 0; bb->elapsedlvl = 0; bb->elapsedshot = 0; return true; } /***************************************************************************** * bubbles_getonboard() determines which bubble types are on the play board. ******************************************************************************/ static void bubbles_getonboard(struct game_context* bb) { int i, j, k; bool found; bb->onboardcnt = 0; rb->memset(bb->onboard, -1, sizeof(bb->onboard)); for(i=0; i<BB_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[i][j].type >= 0) { found = false; for(k=0; k<bb->onboardcnt; k++) { if(bb->playboard[i][j].type == bb->onboard[k]) { found = true; break; } } if(!found) { bb->onboard[bb->onboardcnt] = bb->playboard[i][j].type; bb->onboardcnt++; } if(bb->onboardcnt == NUM_BUBBLES) return; } } } } /***************************************************************************** * bubbles_drawboard() draws the game board to the buffer but does not update * the lcd. ******************************************************************************/ static void bubbles_drawboard(struct game_context* bb) { int i, j; int w1, w2, h; int colmax, indent; int tipx, tipy; bool evenline = false; char *level = "Level"; char *score = "Score"; char *next = "Next"; char *hurry = "HURRY!"; char str[11]; /* clear screen */ rb->lcd_clear_display(); int font = rb->screens[SCREEN_MAIN]->getfont(); h = rb->font_get(font)->height + 1; /* draw background */ #ifdef HAVE_LCD_COLOR rb->lcd_bitmap(bubbles_background, 0, 0, LCD_WIDTH, LCD_HEIGHT); #endif /* display play board */ for(i=0; i<BB_HEIGHT; i++) { colmax = BB_WIDTH; if(evenline) { colmax--; indent = ROW_INDENT; } else { indent = 0; } evenline = !evenline; for(j=0; j<colmax; j++) { if(bb->playboard[i][j].type >= 0 && !bb->playboard[i][j].delete) { rb->lcd_bitmap_part(bubbles_emblem, 0, EMBLEM_HEIGHT*bb->playboard[i][j].type, EMBLEM_WIDTH, XOFS+indent+BUBBLE_WIDTH*j+(BUBBLE_WIDTH-EMBLEM_WIDTH)/2, YOFS+ROW_HEIGHT*i+(BUBBLE_HEIGHT-EMBLEM_HEIGHT)/2+bb->compress*ROW_HEIGHT, EMBLEM_WIDTH, EMBLEM_HEIGHT); rb->lcd_set_drawmode(DRMODE_FG); rb->lcd_mono_bitmap((const unsigned char *)bubbles_bubble, XOFS+indent+BUBBLE_WIDTH*j, YOFS+ROW_HEIGHT*i+bb->compress*ROW_HEIGHT, BUBBLE_WIDTH, BUBBLE_HEIGHT); rb->lcd_set_drawmode(DRMODE_SOLID); } } } /* display bubble to be shot */ rb->lcd_bitmap_part(bubbles_emblem, 0, EMBLEM_HEIGHT*bb->queue[bb->nextinq], EMBLEM_WIDTH, SHOTX+(BUBBLE_WIDTH-EMBLEM_WIDTH)/2, SHOTY+(BUBBLE_HEIGHT-EMBLEM_HEIGHT)/2, EMBLEM_WIDTH, EMBLEM_HEIGHT); rb->lcd_set_drawmode(DRMODE_FG); rb->lcd_mono_bitmap((const unsigned char *)bubbles_bubble, SHOTX, SHOTY, BUBBLE_WIDTH, BUBBLE_HEIGHT); rb->lcd_set_drawmode(DRMODE_SOLID); /* display next bubble to be shot */ #ifndef NEXT_BB_X rb->lcd_bitmap_part(bubbles_emblem, 0, EMBLEM_HEIGHT*bb->queue[(bb->nextinq+1)%NUM_QUEUE], EMBLEM_WIDTH, XOFS/2-BUBBLE_WIDTH/2+(BUBBLE_WIDTH-EMBLEM_WIDTH)/2, SHOTY+(BUBBLE_HEIGHT-EMBLEM_HEIGHT)/2, EMBLEM_WIDTH, EMBLEM_HEIGHT); rb->lcd_set_drawmode(DRMODE_FG); rb->lcd_mono_bitmap((const unsigned char *)bubbles_bubble, XOFS/2-BUBBLE_WIDTH/2, SHOTY, BUBBLE_WIDTH, BUBBLE_HEIGHT); rb->lcd_set_drawmode(DRMODE_SOLID); #else rb->lcd_bitmap_part(bubbles_emblem, 0, EMBLEM_HEIGHT*bb->queue[(bb->nextinq+1)%NUM_QUEUE], EMBLEM_WIDTH, NEXT_BB_X + NEXT_BB_WIDTH/2-BUBBLE_WIDTH/2+(BUBBLE_WIDTH-EMBLEM_WIDTH)/2, NEXT_BB_Y + (BUBBLE_HEIGHT-EMBLEM_HEIGHT)/2 + h, EMBLEM_WIDTH, EMBLEM_HEIGHT); rb->lcd_set_drawmode(DRMODE_FG); rb->lcd_mono_bitmap((const unsigned char *)bubbles_bubble, NEXT_BB_X + NEXT_BB_WIDTH/2-BUBBLE_WIDTH/2, NEXT_BB_Y + h, BUBBLE_WIDTH, BUBBLE_HEIGHT); rb->lcd_set_drawmode(DRMODE_SOLID); #endif /* draw bounding lines */ #ifndef HAVE_LCD_COLOR rb->lcd_vline(XOFS-1, 0, LCD_HEIGHT); rb->lcd_vline(XOFS+BUBBLE_WIDTH*BB_WIDTH, 0, LCD_HEIGHT); #endif rb->lcd_hline(XOFS, XOFS+BUBBLE_WIDTH*BB_WIDTH-1, YOFS+bb->compress*ROW_HEIGHT-1); rb->lcd_hline(XOFS, XOFS+BUBBLE_WIDTH*BB_WIDTH-1, YOFS+ROW_HEIGHT*(BB_HEIGHT-2)+BUBBLE_HEIGHT); /* draw arrow */ tipx = SHOTX+BUBBLE_WIDTH/2+(((fp14_sin(bb->angle)>>4)*BUBBLE_WIDTH*3/2)>>10); tipy = SHOTY+BUBBLE_HEIGHT/2-(((fp14_cos(bb->angle)>>4)*BUBBLE_HEIGHT*3/2)>>10); rb->lcd_drawline(SHOTX+BUBBLE_WIDTH/2+(((fp14_sin(bb->angle)>>4)*BUBBLE_WIDTH/2)>>10), SHOTY+BUBBLE_HEIGHT/2-(((fp14_cos(bb->angle)>>4)*BUBBLE_HEIGHT/2)>>10), tipx, tipy); xlcd_filltriangle(tipx, tipy, tipx+(((fp14_sin(bb->angle-135)>>4)*BUBBLE_WIDTH/3)>>10), tipy-(((fp14_cos(bb->angle-135)>>4)*BUBBLE_HEIGHT/3)>>10), tipx+(((fp14_sin(bb->angle+135)>>4)*BUBBLE_WIDTH/3)>>10), tipy-(((fp14_cos(bb->angle+135)>>4)*BUBBLE_HEIGHT/3)>>10)); /* draw text */ rb->snprintf(str, 4, "%d", bb->level); rb->lcd_getstringsize(level, &w1, NULL); rb->lcd_getstringsize(str, &w2, NULL); #ifndef LEVEL_TXT_X rb->lcd_putsxy(XOFS/2-w1/2, 2, level); rb->lcd_putsxy(XOFS/2-w2/2, 2+h, str); #else rb->lcd_putsxy(LEVEL_TXT_X+(LEVEL_TXT_WIDTH/2-w1/2), LEVEL_TXT_Y, level); rb->lcd_putsxy(LEVEL_TXT_X+(LEVEL_TXT_WIDTH/2-w2/2), LEVEL_TXT_Y+h, str); #endif rb->snprintf(str, 10, "%d", bb->score); rb->lcd_getstringsize(score, &w1,NULL); rb->lcd_getstringsize(str, &w2, NULL); #ifndef SCORE_TXT_X rb->lcd_putsxy(XOFS/2-w1/2, 29, score); rb->lcd_putsxy(XOFS/2-w2/2, 29+h, str); #else rb->lcd_putsxy(SCORE_TXT_X+(SCORE_TXT_WIDTH/2-w1/2), SCORE_TXT_Y, score); rb->lcd_putsxy(SCORE_TXT_X+(SCORE_TXT_WIDTH/2-w2/2), SCORE_TXT_Y+h, str); #endif rb->lcd_getstringsize(next, &w1, NULL); #ifndef NEXT_BB_X rb->lcd_putsxy(XOFS/2-w1/2, SHOTY-h, next); #else rb->lcd_putsxy(NEXT_BB_X+(NEXT_BB_WIDTH/2-w1/2), NEXT_BB_Y, next); #endif if(bb->elapsedshot >= (MAX_SHOTTIME*7)/10) { rb->lcd_getstringsize(hurry, &w1, &h); rb->lcd_putsxy(LCD_WIDTH/2-w1/2, LCD_HEIGHT/2-h/2, hurry); } } /***************************************************************************** * bubbles_fire() fires the current bubble, reloads the cannon, attaches * bubble to playboard, removes appropriate bubbles, and advances the * the compressor. ******************************************************************************/ static int bubbles_fire(struct game_context* bb) { int bubblecur; long shotxinc, shotyinc; long shotxofs, shotyofs; int shotxdirec = 1; long tempxofs, tempyofs; int nearrow, nearcol; int lastrow = BB_HEIGHT-1; int lastcol = (BB_WIDTH-1)/2; int buttonres; long lasttick, currenttick; /* get current bubble */ bubblecur = bb->queue[bb->nextinq]; shotxinc = ((fp14_sin(bb->angle)>>4)*BUBBLE_WIDTH)/3; shotyinc = ((-1*(fp14_cos(bb->angle)>>4))*BUBBLE_HEIGHT)/3; shotxofs = shotyofs = 0; /* advance the queue */ bb->queue[bb->nextinq] = bb->onboard[rb->rand()%bb->onboardcnt]; bb->nextinq = (bb->nextinq+1)%NUM_QUEUE; bubbles_drawboard(bb); rb->lcd_update_rect(0, 0, XOFS, LCD_HEIGHT); /* move the bubble across the play board */ lasttick = *rb->current_tick; while(true) { /* move the bubble one step */ shotyofs += shotyinc; shotxofs += shotxinc*shotxdirec; /* check for bounce off sides */ if(SHOTX+(shotxofs>>10) < XOFS) { shotxofs += 2*((XOFS<<10)-(((SHOTX)<<10)+shotxofs)); shotxdirec *= -1; } else if(SHOTX+(shotxofs>>10) > XOFS+(BB_WIDTH-1)*BUBBLE_WIDTH) { shotxofs -= 2*((((SHOTX)<<10)+shotxofs)- ((XOFS<<10)+(((BB_WIDTH-1)*BUBBLE_WIDTH)<<10))); shotxdirec *= -1; } tempxofs = shotxofs>>10; tempyofs = shotyofs>>10; /* display shot */ bubbles_drawboard(bb); rb->lcd_bitmap_part(bubbles_emblem, 0, EMBLEM_HEIGHT*bubblecur, EMBLEM_WIDTH, SHOTX+tempxofs+(BUBBLE_WIDTH-EMBLEM_WIDTH)/2, SHOTY+tempyofs+(BUBBLE_HEIGHT-EMBLEM_HEIGHT)/2, EMBLEM_WIDTH, EMBLEM_HEIGHT); rb->lcd_set_drawmode(DRMODE_FG); rb->lcd_mono_bitmap((const unsigned char *)bubbles_bubble, SHOTX+tempxofs, SHOTY+tempyofs, BUBBLE_WIDTH, BUBBLE_HEIGHT); rb->lcd_set_drawmode(DRMODE_SOLID); rb->lcd_update_rect(XOFS, 0, BB_WIDTH*BUBBLE_WIDTH, LCD_HEIGHT); /* find nearest position */ nearrow = ((SHOTY+tempyofs)- (YOFS+bb->compress*ROW_HEIGHT)+ (ROW_HEIGHT/2))/ROW_HEIGHT; if(nearrow >= BB_HEIGHT) nearrow = BB_HEIGHT-1; if(nearrow%2) { /* odd row */ nearcol = ((SHOTX+tempxofs)- (XOFS+ROW_INDENT)+ (BUBBLE_WIDTH/2))/BUBBLE_WIDTH; if(nearcol >= BB_WIDTH-1) nearcol = BB_WIDTH-2; } else { /* even row */ nearcol = ((SHOTX+tempxofs)-XOFS+(BUBBLE_WIDTH/2))/BUBBLE_WIDTH; if(nearcol >= BB_WIDTH) nearcol = BB_WIDTH-1; } if(nearcol < 0) nearcol = 0; /* if nearest position is occupied attach to last position */ if(bb->playboard[nearrow][nearcol].type >= 0) { bb->playboard[lastrow][lastcol].type = bubblecur; break; } /* save last position */ lastrow = nearrow; lastcol = nearcol; /* if collision with neighbor then attach shot */ if(bubbles_collision(bb, YOFS+SHOTY+tempyofs, SHOTX+tempxofs, nearrow, nearcol)) { bb->playboard[nearrow][nearcol].type = bubblecur; break; } /* if at top then attach shot to the ceiling */ if(nearrow == 0 && SHOTY+tempyofs <= YOFS+bb->compress*ROW_HEIGHT) { bb->playboard[nearrow][nearcol].type = bubblecur; break; } /* handle button events */ buttonres = bubbles_handlebuttons(bb, true, 0); if(buttonres != BB_NONE) return buttonres; /* framerate limiting */ currenttick = *rb->current_tick; if(currenttick-lasttick < HZ/MAX_FPS) { rb->sleep((HZ/MAX_FPS)-(currenttick-lasttick)); } else { rb->yield(); } lasttick = currenttick; } bubbles_drawboard(bb); rb->lcd_update(); /* clear appropriate bubbles from playing board */ if(bubbles_ingroup(bb, lastrow, lastcol)) { buttonres = bubbles_remove(bb); if(buttonres != BB_NONE) return buttonres; } /* update shots and compress amount */ bb->shots++; if(bb->shots >= NUM_COMPRESS) { bb->shots = 0; bb->compress++; } return BB_NONE; } /***************************************************************************** * bubbles_collision() determines if a fired bubble has collided with another * bubble. ******************************************************************************/ static bool bubbles_collision(struct game_context* bb, int y, int x, int nearrow, int nearcol) { int nx, ny; int adj = nearrow%2; /* check neighbors */ if(nearcol-1 >= 0) { if(bb->playboard[nearrow][nearcol-1].type >= 0) { nx = XOFS+(nearrow%2 ? ROW_INDENT : 0)+BUBBLE_WIDTH*(nearcol-1); ny = YOFS+ROW_HEIGHT*nearrow+bb->compress*ROW_HEIGHT; if((x-nx)*(x-nx)+(y-ny)*(y-ny) < MIN_DISTANCE) return true; } } if(nearcol-1+adj >= 0) { if(nearrow-1 >= 0) { if(bb->playboard[nearrow-1][nearcol-1+adj].type >= 0) { nx = XOFS+((nearrow-1)%2 ? ROW_INDENT : 0)+ BUBBLE_WIDTH*(nearcol-1+adj); ny = YOFS+ROW_HEIGHT*(nearrow-1)+bb->compress*ROW_HEIGHT; if((x-nx)*(x-nx)+(y-ny)*(y-ny) < MIN_DISTANCE) return true; } } if(nearrow+1 < BB_HEIGHT) { if(bb->playboard[nearrow+1][nearcol-1+adj].type >= 0) { nx = XOFS+((nearrow+1)%2 ? ROW_INDENT : 0)+ BUBBLE_WIDTH*(nearcol-1+adj); ny = YOFS+ROW_HEIGHT*(nearrow+1)+bb->compress*ROW_HEIGHT; if((x-nx)*(x-nx)+(y-ny)*(y-ny) < MIN_DISTANCE) return true; } } } if(nearcol+adj >= 0) { if(nearrow-1 >= 0) { if(bb->playboard[nearrow-1][nearcol+adj].type >= 0) { nx = XOFS+((nearrow-1)%2 ? ROW_INDENT : 0)+ BUBBLE_WIDTH*(nearcol+adj); ny = YOFS+ROW_HEIGHT*(nearrow-1)+bb->compress*ROW_HEIGHT; if((x-nx)*(x-nx)+(y-ny)*(y-ny) < MIN_DISTANCE) return true; } } if(nearrow+1 < BB_HEIGHT) { if(bb->playboard[nearrow+1][nearcol+adj].type >= 0) { nx = XOFS+((nearrow+1)%2 ? ROW_INDENT : 0)+ BUBBLE_WIDTH*(nearcol+adj); ny = YOFS+ROW_HEIGHT*(nearrow+1)+bb->compress*ROW_HEIGHT; if((x-nx)*(x-nx)+(y-ny)*(y-ny) < MIN_DISTANCE) return true; } } } if(nearcol+1 < BB_WIDTH-adj) { if(bb->playboard[nearrow][nearcol+1].type >= 0) { nx = XOFS+(nearrow%2 ? ROW_INDENT : 0)+BUBBLE_WIDTH*(nearcol+1); ny = YOFS+ROW_HEIGHT*nearrow+bb->compress*ROW_HEIGHT; if((x-nx)*(x-nx)+(y-ny)*(y-ny) < MIN_DISTANCE) return true; } } return false; } /***************************************************************************** * bubbles_ingroup() marks all bubbles that form the current group. ******************************************************************************/ static bool bubbles_ingroup(struct game_context* bb, int row, int col) { int i, j; int count; count = bubbles_searchgroup(bb, row, col); /* unmark group if too small */ if(count < 3) { for(i=0; i<BB_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { bb->playboard[i][j].ingroup = false; } } return false; } return true; } /***************************************************************************** * bubbles_searchgroup() return the size of the group of bubbles of the same * type that the current bubble belongs to. ******************************************************************************/ static int bubbles_searchgroup(struct game_context* bb, int row, int col) { int i, adj; int myrow, mycol, mytype; int count = 0; struct coord { int row; int col; } search[(2*BB_WIDTH-1)*(BB_HEIGHT/2)]; /* search initial bubble */ bb->playboard[row][col].ingroup = true; search[count].row = row; search[count].col = col; count++; /* breadth-first search neighbors */ for(i=0; i<count; i++) { myrow = search[i].row; mycol = search[i].col; mytype = bb->playboard[myrow][mycol].type; adj = myrow%2; if(mycol-1 >= 0) { if(bb->playboard[myrow][mycol-1].type == mytype && !bb->playboard[myrow][mycol-1].ingroup) { bb->playboard[myrow][mycol-1].ingroup = true; search[count].row = myrow; search[count].col = mycol-1; count++; } } if(mycol-1+adj >= 0) { if(myrow-1 >= 0) { if(bb->playboard[myrow-1][mycol-1+adj].type == mytype && !bb->playboard[myrow-1][mycol-1+adj].ingroup) { bb->playboard[myrow-1][mycol-1+adj].ingroup = true; search[count].row = myrow-1; search[count].col = mycol-1+adj; count++; } } if(myrow+1 < BB_HEIGHT) { if(bb->playboard[myrow+1][mycol-1+adj].type == mytype && !bb->playboard[myrow+1][mycol-1+adj].ingroup) { bb->playboard[myrow+1][mycol-1+adj].ingroup = true; search[count].row = myrow+1; search[count].col = mycol-1+adj; count++; } } } if(mycol+adj >= 0) { if(myrow-1 >= 0) { if(bb->playboard[myrow-1][mycol+adj].type == mytype && !bb->playboard[myrow-1][mycol+adj].ingroup) { bb->playboard[myrow-1][mycol+adj].ingroup = true; search[count].row = myrow-1; search[count].col = mycol+adj; count++; } } if(myrow+1 < BB_HEIGHT) { if(bb->playboard[myrow+1][mycol+adj].type == mytype && !bb->playboard[myrow+1][mycol+adj].ingroup) { bb->playboard[myrow+1][mycol+adj].ingroup = true; search[count].row = myrow+1; search[count].col = mycol+adj; count++; } } } if(mycol+1 < BB_WIDTH-adj) { if(bb->playboard[myrow][mycol+1].type == mytype && !bb->playboard[myrow][mycol+1].ingroup) { bb->playboard[myrow][mycol+1].ingroup = true; search[count].row = myrow; search[count].col = mycol+1; count++; } } } return count; } /***************************************************************************** * bubbles_remove() removes all bubbles in the current group and all * unanchored bubbles from the play board. ******************************************************************************/ static int bubbles_remove(struct game_context* bb) { int i, j; int buttonres; /* determine all anchored bubbles */ for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[0][j].type >= 0 && !bb->playboard[0][j].ingroup) { bubbles_anchored(bb, 0, j); } } /* mark bubbles to be deleted */ for(i=0; i<BB_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[i][j].type >= 0 && (!bb->playboard[i][j].anchored || bb->playboard[i][j].ingroup)) { bb->playboard[i][j].delete = true; } } } /* animate falling bubbles */ buttonres = bubbles_fall(bb); if(buttonres != BB_NONE) return buttonres; /* remove bubbles */ for(i=0; i<BB_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[i][j].delete) { bb->playboard[i][j].ingroup = false; bb->playboard[i][j].type = -1; bb->playboard[i][j].delete = false; } else { bb->playboard[i][j].anchored = false; } } } bubbles_getonboard(bb); return BB_NONE; } /***************************************************************************** * bubbles_anchored() marks all bubbles that are anchored in some way to the * current bubble. ******************************************************************************/ static void bubbles_anchored(struct game_context* bb, int row, int col) { int i, adj; int myrow, mycol, mytype; int count = 0; struct coord { int row; int col; } search[(2*BB_WIDTH-1)*(BB_HEIGHT/2)]; /* search initial bubble */ bb->playboard[row][col].anchored = true; search[count].row = row; search[count].col = col; count++; /* breadth-first search neighbors */ for(i=0; i<count; i++) { myrow = search[i].row; mycol = search[i].col; mytype = bb->playboard[myrow][mycol].type; adj = myrow%2; if(mycol-1 >= 0) { if(bb->playboard[myrow][mycol-1].type >= 0 && !bb->playboard[myrow][mycol-1].ingroup && !bb->playboard[myrow][mycol-1].anchored) { bb->playboard[myrow][mycol-1].anchored = true; search[count].row = myrow; search[count].col = mycol-1; count++; } } if(mycol-1+adj >= 0) { if(myrow-1 >= 0) { if(bb->playboard[myrow-1][mycol-1+adj].type >= 0 && !bb->playboard[myrow-1][mycol-1+adj].ingroup && !bb->playboard[myrow-1][mycol-1+adj].anchored) { bb->playboard[myrow-1][mycol-1+adj].anchored = true; search[count].row = myrow-1; search[count].col = mycol-1+adj; count++; } } if(myrow+1 < BB_HEIGHT) { if(bb->playboard[myrow+1][mycol-1+adj].type >= 0 && !bb->playboard[myrow+1][mycol-1+adj].ingroup && !bb->playboard[myrow+1][mycol-1+adj].anchored) { bb->playboard[myrow+1][mycol-1+adj].anchored = true; search[count].row = myrow+1; search[count].col = mycol-1+adj; count++; } } } if(mycol+adj >= 0) { if(myrow-1 >= 0) { if(bb->playboard[myrow-1][mycol+adj].type >= 0 && !bb->playboard[myrow-1][mycol+adj].ingroup && !bb->playboard[myrow-1][mycol+adj].anchored) { bb->playboard[myrow-1][mycol+adj].anchored = true; search[count].row = myrow-1; search[count].col = mycol+adj; count++; } } if(myrow+1 < BB_HEIGHT) { if(bb->playboard[myrow+1][mycol+adj].type >= 0 && !bb->playboard[myrow+1][mycol+adj].ingroup && !bb->playboard[myrow+1][mycol+adj].anchored) { bb->playboard[myrow+1][mycol+adj].anchored = true; search[count].row = myrow+1; search[count].col = mycol+adj; count++; } } } if(mycol+1 < BB_WIDTH-adj) { if(bb->playboard[myrow][mycol+1].type >= 0 && !bb->playboard[myrow][mycol+1].ingroup && !bb->playboard[myrow][mycol+1].anchored) { bb->playboard[myrow][mycol+1].anchored = true; search[count].row = myrow; search[count].col = mycol+1; count++; } } } } /***************************************************************************** * bubbles_fall() makes removed bubbles fall from the screen. ******************************************************************************/ static int bubbles_fall(struct game_context* bb) { int i, j; int count; int indent; int xofs, yofs; int buttonres; bool onscreen; long lasttick, currenttick; /* give all falling bubbles an x axis movement */ for(i=0; i<BB_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[i][j].delete) { bb->playboard[i][j].fallx = rb->rand()%25 - 12; bb->playboard[i][j].fallvel = rb->rand()%5 + 6; } } } /* draw bubbles falling off the screen * follows y=x^2-8x scaled to bubble size */ lasttick = *rb->current_tick; for(count=1; ;count++) { onscreen = false; bubbles_drawboard(bb); for(i=0; i<BB_HEIGHT; i++) { for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[i][j].delete) { indent = (i%2 ? ROW_INDENT : 0); xofs = ((bb->playboard[i][j].fallx*count)*BUBBLE_WIDTH)/48; yofs = ((count*count - bb->playboard[i][j].fallvel*count)* BUBBLE_HEIGHT)/20; /* draw bubble if it is still on the screen */ if(YOFS+ROW_HEIGHT*i+bb->compress*ROW_HEIGHT+yofs <= LCD_HEIGHT) { onscreen = true; rb->lcd_bitmap_part(bubbles_emblem, 0, EMBLEM_HEIGHT*bb->playboard[i][j].type, EMBLEM_WIDTH, XOFS+indent+BUBBLE_WIDTH*j+ (BUBBLE_WIDTH-EMBLEM_WIDTH)/2+xofs, YOFS+ROW_HEIGHT*i+(BUBBLE_HEIGHT-EMBLEM_HEIGHT)/2+ bb->compress*ROW_HEIGHT+yofs, EMBLEM_WIDTH, EMBLEM_HEIGHT); rb->lcd_set_drawmode(DRMODE_FG); rb->lcd_mono_bitmap( (const unsigned char *)bubbles_bubble, XOFS+indent+BUBBLE_WIDTH*j+xofs, YOFS+ROW_HEIGHT*i+bb->compress*ROW_HEIGHT+yofs, BUBBLE_WIDTH, BUBBLE_HEIGHT); rb->lcd_set_drawmode(DRMODE_SOLID); } } } } rb->lcd_update(); /* break out if all bubbles are off the screen */ if(!onscreen) break; /* handle button events */ buttonres = bubbles_handlebuttons(bb, true, 0); if(buttonres != BB_NONE) return buttonres; /* framerate limiting */ currenttick = *rb->current_tick; if(currenttick-lasttick < HZ/MAX_FPS) { rb->sleep((HZ/MAX_FPS)-(currenttick-lasttick)); } else { rb->yield(); } lasttick = currenttick; } return BB_NONE; } /***************************************************************************** * bubbles_checklevel() checks the state of the playboard for a win or loss. ******************************************************************************/ static int bubbles_checklevel(struct game_context* bb) { int i, j; int points; char str[13]; bubbles_drawboard(bb); rb->lcd_update(); /* check for bubbles below cut off point */ for(i=0; i<=bb->compress; i++) { for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[BB_HEIGHT-1-i][j].type >= 0) return BB_LOSE; } } /* check for bubbles above cut off point */ for(i=0; i<BB_HEIGHT-1-bb->compress; i++) { for(j=0; j<BB_WIDTH; j++) { if(bb->playboard[i][j].type >= 0) return BB_NONE; } } /* level complete, record score */ points = 100 - bb->elapsedlvl/100; if(points > 0) { bb->score += points; } else { points = 0; } rb->snprintf(str, 12, "%d points", points); rb->splash(HZ, str); /* advance to the next level */ if(!bubbles_nextlevel(bb)) { return BB_WIN; } bubbles_drawboard(bb); rb->lcd_update(); rb->snprintf(str, 12, "Level %d", bb->level); rb->splash(HZ, str); bubbles_drawboard(bb); rb->lcd_update(); return BB_NONE; } /***************************************************************************** * bubbles_recordscore() inserts a high score into the high scores list and * returns the high score position. ******************************************************************************/ static void bubbles_recordscore(struct game_context* bb) { int position; position = highscore_update(bb->score, bb->level-1, "", highscores, NUM_SCORES); if (position==0) rb->splash(HZ*2, "New High Score"); if (position != -1) highscore_show(position, highscores, NUM_SCORES, true); } /***************************************************************************** * bubbles_loaddata() loads highest level beaten. ******************************************************************************/ static void bubbles_loaddata(void) { int fd; last_highlevel = highlevel = 0; /* open data file */ fd = rb->open(DATA_FILE, O_RDONLY); if (fd < 0) return; /* read in saved game */ if (rb->read(fd, &highlevel, sizeof(highlevel)) < (long)sizeof(highlevel)) { highlevel = 0; } if (highlevel >= NUM_LEVELS) highlevel = NUM_LEVELS-1; last_highlevel = highlevel; rb->close(fd); } /***************************************************************************** * bubbles_savedata() saves the current game state. ******************************************************************************/ static void bubbles_savedata(void) { int fd; if (last_highlevel >= highlevel) /* no need to save */ return; fd = rb->open(DATA_FILE, O_WRONLY|O_CREAT); if (fd < 0) return; rb->write(fd, &highlevel, sizeof(highlevel)); rb->close(fd); } /***************************************************************************** * bubbles_loadgame() loads the saved game and returns load success. ******************************************************************************/ static bool bubbles_loadgame(struct game_context* bb) { int fd; bool ret = true; /* open game file */ fd = rb->open(SAVE_FILE, O_RDONLY); if(fd < 0) return false; /* read in saved game */ if(rb->read(fd, bb, sizeof(struct game_context)) < (long)sizeof(struct game_context)) { ret = false; } rb->close(fd); /* delete saved file */ rb->remove(SAVE_FILE); return ret; } /***************************************************************************** * bubbles_savegame() saves the current game state. ******************************************************************************/ static void bubbles_savegame(struct game_context* bb) { int fd; if (!resume) /* nothing to save */ return; /* write out the game state to the save file */ fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT); if (fd < 0) { rb->splash(HZ/2, "Failed to save game"); return; } if (rb->write(fd, bb, sizeof(struct game_context)) <= 0) { rb->splash(HZ/2, "Failed to save game"); } rb->close(fd); } /***************************************************************************** * bubbles_setcolors() set the foreground and background colors. ******************************************************************************/ static inline void bubbles_setcolors(void) { #ifdef HAVE_LCD_COLOR rb->lcd_set_background(LCD_RGBPACK(181,181,222)); rb->lcd_set_foreground(LCD_BLACK); #endif } /***************************************************************************** * bubbles_callback() is the default event handler callback which is called * on usb connect and shutdown. ******************************************************************************/ static void bubbles_callback(void* param) { (void) param; highscore_save(SCORE_FILE, highscores, NUM_SCORES); } /***************************************************************************** * bubbles_handlebuttons() handles button events during a game. ******************************************************************************/ static int bubbles_handlebuttons(struct game_context* bb, bool animblock, int timeout) { int button; int buttonres; long start; const struct button_mapping *plugin_contexts[] #if (CONFIG_KEYPAD != SANSA_E200_PAD) && \ (CONFIG_KEYPAD != SANSA_FUZE_PAD) = {generic_left_right_fire,generic_actions}; #else = {generic_directions,generic_actions}; #endif if (timeout < 0) timeout = 0; button = pluginlib_getaction(timeout,plugin_contexts,2); #if defined(HAS_BUTTON_HOLD) && !defined(HAVE_REMOTE_LCD_AS_MAIN) /* FIXME: Should probably check remote hold here */ if (rb->button_hold()) button = BUBBLES_START; #endif switch(button){ case BUBBLES_LEFT_REP: if(bb->angle > MIN_ANGLE) bb->angle -= ANGLE_STEP_REP; case BUBBLES_LEFT: /* change angle to the left */ if(bb->angle > MIN_ANGLE) bb->angle -= ANGLE_STEP; break; case BUBBLES_RIGHT_REP: if(bb->angle < MAX_ANGLE) bb->angle += ANGLE_STEP_REP; case BUBBLES_RIGHT: /* change angle to the right */ if(bb->angle < MAX_ANGLE) bb->angle += ANGLE_STEP; break; case BUBBLES_SELECT: /* fire the shot */ if(!animblock) { bb->elapsedlvl += bb->elapsedshot; bb->elapsedshot = 0; buttonres = bubbles_fire(bb); if(buttonres != BB_NONE) return buttonres; buttonres = bubbles_checklevel(bb); if(buttonres != BB_NONE) return buttonres; bb->startedshot = *rb->current_tick; } break; case BUBBLES_START: /* pause the game */ start = *rb->current_tick; rb->splash(0, "Paused"); while(pluginlib_getaction(TIMEOUT_BLOCK,plugin_contexts,2) != (BUBBLES_START)); bb->startedshot += *rb->current_tick-start; bubbles_drawboard(bb); rb->lcd_update(); break; case BUBBLES_RESUME: /* save and end the game */ case BUBBLES_QUIT: /* end the game */ if(!animblock) { resume = true; return BB_END; } break; case ACTION_UNKNOWN: case ACTION_NONE: /* no button pressed */ break; default: if(rb->default_event_handler_ex(button, bubbles_callback, (void*) bb) == SYS_USB_CONNECTED) return BB_USB; break; } return BB_NONE; } /***************************************************************************** * bubbles_menu() is the initial menu at the start of the game. ******************************************************************************/ static int bubbles_menu(struct game_context* bb) { static unsigned int startlevel = 0; int selected = resume?0:1; bool startgame = false; MENUITEM_STRINGLIST(menu,"Bubbles Menu",NULL, "Resume Game", "Start New Game", "Level", "High Scores", "Playback Control", "Quit", "Save and Quit"); while(!startgame){ switch (rb->do_menu(&menu, &selected, NULL, false)) { case 0: /* resume game */ if (!resume) { rb->splash(HZ/2, "Nothing to resume"); break; } else { startgame = true; } break; case 1: /* new game */ bb->level = startlevel; startgame = true; resume = false; break; case 2: /* choose level */ startlevel++; rb->set_int("Choose start level", "", UNIT_INT, &startlevel, NULL, 1, 1, highlevel+1, NULL); startlevel--; break; case 3: /* High scores */ highscore_show(NUM_SCORES, highscores, NUM_SCORES, true); break; case 4: /* Playback Control */ playback_control(NULL); break; case 5: /* quit */ return BB_QUIT; case 6: /* save and quit */ return BB_SAVE_AND_QUIT; case MENU_ATTACHED_USB: bubbles_callback(bb); return BB_USB; } } return 0; } /***************************************************************************** * bubbles() is the main game subroutine, it returns the final game status. ******************************************************************************/ static int bubbles(struct game_context* bb) { int buttonres; long timeout; /******************** * menu * ********************/ buttonres = bubbles_menu(bb); if(buttonres != 0) return buttonres; /******************** * init * ********************/ bubbles_init(bb); bubbles_drawboard(bb); rb->lcd_update(); /********************** * play * **********************/ bb->startedshot = *rb->current_tick; while(true) { /* refresh the board */ bubbles_drawboard(bb); rb->lcd_update(); /* manange idle framerate */ bb->elapsedshot = *rb->current_tick-bb->startedshot; if(MAX_SHOTTIME-bb->elapsedshot < HZ/2) { timeout = MAX_SHOTTIME-bb->elapsedshot; } else { timeout = HZ/2; } /* handle button events */ buttonres = bubbles_handlebuttons(bb, false, timeout); if(buttonres != BB_NONE) return buttonres; /* handle timing */ bb->elapsedshot = *rb->current_tick-bb->startedshot; if(bb->elapsedshot > MAX_SHOTTIME) { bb->elapsedlvl += bb->elapsedshot;