Bump plugin API version. This should have been done in r24587. Also, because the...
[kugel-rb.git] / apps / plugins / xobox.c
blob81e0e6ae1585d36c77461f278db0e6e21957ed75
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Eli Sherer
11 * 2007 Antoine Cellerier
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "plugin.h"
24 #include "lib/helper.h"
25 #include "lib/playback_control.h"
27 PLUGIN_HEADER
29 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
31 #define QUIT BUTTON_OFF
32 #define LEFT BUTTON_LEFT
33 #define RIGHT BUTTON_RIGHT
34 #define PAUSE BUTTON_MODE
35 #define UP BUTTON_UP
36 #define DOWN BUTTON_DOWN
38 #define RC_QUIT BUTTON_RC_STOP
40 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
42 #define QUIT BUTTON_OFF
43 #define LEFT BUTTON_LEFT
44 #define RIGHT BUTTON_RIGHT
45 #define PAUSE BUTTON_ON
46 #define UP BUTTON_UP
47 #define DOWN BUTTON_DOWN
49 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
50 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
51 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
53 #define QUIT (BUTTON_SELECT | BUTTON_MENU)
54 #define LEFT BUTTON_LEFT
55 #define RIGHT BUTTON_RIGHT
56 #define PAUSE BUTTON_SELECT
57 #define MENU_UP BUTTON_SCROLL_FWD
58 #define MENU_DOWN BUTTON_SCROLL_BACK
59 #define UP BUTTON_MENU
60 #define DOWN BUTTON_PLAY
62 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
64 #define QUIT BUTTON_POWER
65 #define LEFT BUTTON_LEFT
66 #define RIGHT BUTTON_RIGHT
67 #define UP BUTTON_UP
68 #define DOWN BUTTON_DOWN
69 #define PAUSE BUTTON_PLAY
71 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
73 #define QUIT BUTTON_POWER
74 #define LEFT BUTTON_LEFT
75 #define RIGHT BUTTON_RIGHT
76 #define UP BUTTON_UP
77 #define DOWN BUTTON_DOWN
78 #define PAUSE BUTTON_A
80 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
81 (CONFIG_KEYPAD == SANSA_C200_PAD)
83 #define QUIT BUTTON_POWER
84 #define LEFT BUTTON_LEFT
85 #define RIGHT BUTTON_RIGHT
86 #define UP BUTTON_UP
87 #define DOWN BUTTON_DOWN
88 #define PAUSE BUTTON_REC
90 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
92 #define QUIT BUTTON_POWER
93 #define LEFT BUTTON_LEFT
94 #define RIGHT BUTTON_RIGHT
95 #define UP BUTTON_UP
96 #define DOWN BUTTON_DOWN
97 #define PAUSE BUTTON_HOME
99 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
101 #define QUIT (BUTTON_HOME|BUTTON_REPEAT)
102 #define LEFT BUTTON_LEFT
103 #define RIGHT BUTTON_RIGHT
104 #define UP BUTTON_UP
105 #define DOWN BUTTON_DOWN
106 #define PAUSE BUTTON_SELECT
108 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
110 #define QUIT BUTTON_POWER
111 #define LEFT BUTTON_LEFT
112 #define RIGHT BUTTON_RIGHT
113 #define UP BUTTON_UP
114 #define DOWN BUTTON_DOWN
115 #define PAUSE BUTTON_SELECT
117 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
119 #define QUIT BUTTON_POWER
120 #define LEFT BUTTON_LEFT
121 #define RIGHT BUTTON_RIGHT
122 #define UP BUTTON_SCROLL_UP
123 #define DOWN BUTTON_SCROLL_DOWN
124 #define PAUSE BUTTON_PLAY
126 #elif CONFIG_KEYPAD == RECORDER_PAD
128 #define QUIT BUTTON_OFF
129 #define LEFT BUTTON_LEFT
130 #define RIGHT BUTTON_RIGHT
131 #define DOWN BUTTON_DOWN
132 #define UP BUTTON_UP
133 #define PAUSE BUTTON_PLAY
135 #elif CONFIG_KEYPAD == ONDIO_PAD
137 #define QUIT BUTTON_OFF
138 #define LEFT BUTTON_LEFT
139 #define RIGHT BUTTON_RIGHT
140 #define DOWN BUTTON_DOWN
141 #define UP BUTTON_UP
142 #define PAUSE BUTTON_MENU
144 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
146 #define QUIT BUTTON_BACK
147 #define LEFT BUTTON_LEFT
148 #define RIGHT BUTTON_RIGHT
149 #define UP BUTTON_UP
150 #define DOWN BUTTON_DOWN
151 #define PAUSE BUTTON_PLAY
153 #elif (CONFIG_KEYPAD == MROBE100_PAD)
155 #define QUIT BUTTON_POWER
156 #define LEFT BUTTON_LEFT
157 #define RIGHT BUTTON_RIGHT
158 #define UP BUTTON_UP
159 #define DOWN BUTTON_DOWN
160 #define PAUSE BUTTON_DISPLAY
162 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
164 #define QUIT BUTTON_RC_REC
165 #define LEFT BUTTON_RC_REW
166 #define RIGHT BUTTON_RC_FF
167 #define UP BUTTON_RC_VOL_UP
168 #define DOWN BUTTON_RC_VOL_DOWN
169 #define PAUSE BUTTON_RC_PLAY
171 #elif CONFIG_KEYPAD == COWON_D2_PAD
173 #define QUIT BUTTON_POWER
175 #elif CONFIG_KEYPAD == IAUDIO67_PAD
177 #define QUIT BUTTON_POWER
178 #define LEFT BUTTON_LEFT
179 #define RIGHT BUTTON_RIGHT
180 #define UP BUTTON_STOP
181 #define DOWN BUTTON_PLAY
182 #define PAUSE BUTTON_MENU
184 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
186 #define QUIT BUTTON_BACK
187 #define LEFT BUTTON_LEFT
188 #define RIGHT BUTTON_RIGHT
189 #define UP BUTTON_UP
190 #define DOWN BUTTON_DOWN
191 #define PAUSE BUTTON_PLAY
193 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
195 #define QUIT BUTTON_POWER
196 #define LEFT BUTTON_LEFT
197 #define RIGHT BUTTON_RIGHT
198 #define UP BUTTON_UP
199 #define DOWN BUTTON_DOWN
200 #define PAUSE BUTTON_VIEW
202 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
204 #define QUIT BUTTON_POWER
205 #define LEFT BUTTON_PREV
206 #define RIGHT BUTTON_NEXT
207 #define UP BUTTON_UP
208 #define DOWN BUTTON_DOWN
209 #define PAUSE BUTTON_MENU
211 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
212 CONFIG_KEYPAD == ONDAVX777_PAD || \
213 CONFIG_KEYPAD == MROBE500_PAD
215 #define QUIT BUTTON_POWER
217 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
219 #define QUIT BUTTON_PLAY
220 #define LEFT BUTTON_LEFT
221 #define RIGHT BUTTON_RIGHT
222 #define UP BUTTON_UP
223 #define DOWN BUTTON_DOWN
224 #define PAUSE BUTTON_FFWD
226 #else
227 #error No keymap defined!
228 #endif
230 #ifdef HAVE_TOUCHSCREEN
231 #ifndef QUIT
232 #define QUIT BUTTON_TOPLEFT
233 #endif
234 #ifndef LEFT
235 #define LEFT BUTTON_MIDLEFT
236 #endif
237 #ifndef RIGHT
238 #define RIGHT BUTTON_MIDRIGHT
239 #endif
240 #ifndef UP
241 #define UP BUTTON_TOPMIDDLE
242 #endif
243 #ifndef DOWN
244 #define DOWN BUTTON_BOTTOMMIDDLE
245 #endif
246 #ifndef PAUSE
247 #define PAUSE BUTTON_CENTER
248 #endif
249 #endif
251 #define MOVE_NO 0 /* player movement */
252 #define MOVE_UP 1 /* 1 */
253 #define MOVE_DN 2 /* 3 0 4 */
254 #define MOVE_LT 3 /* 2 */
255 #define MOVE_RT 4
257 /* ball movement (12 ways) */
258 /* UUL UR */
259 /* UL UR */
260 /* ULL . URR */
261 /* DLL DRR */
262 /* DL DR */
263 /* DDL DDR */
265 #define DIR_UU (1<<7)
266 #define DIR_U (1<<6)
267 #define DIR_RR (1<<5)
268 #define DIR_R (1<<4)
269 #define DIR_DD (1<<3)
270 #define DIR_D (1<<2)
271 #define DIR_LL (1<<1)
272 #define DIR_L (1<<0)
274 #define MOVE_UUR ( DIR_UU | DIR_R )
275 #define MOVE_UR ( DIR_U | DIR_R )
276 #define MOVE_URR ( DIR_U | DIR_RR )
277 #define MOVE_DRR ( DIR_D | DIR_RR )
278 #define MOVE_DR ( DIR_D | DIR_R )
279 #define MOVE_DDR ( DIR_DD | DIR_R )
280 #define MOVE_DDL ( DIR_DD | DIR_L )
281 #define MOVE_DL ( DIR_D | DIR_L )
282 #define MOVE_DLL ( DIR_D | DIR_LL )
283 #define MOVE_ULL ( DIR_U | DIR_LL )
284 #define MOVE_UL ( DIR_U | DIR_L )
285 #define MOVE_UUL ( DIR_UU | DIR_L )
287 #if (LCD_WIDTH>112) && (LCD_HEIGHT>64)
288 # define CUBE_SIZE 8 /* 8x22=176 */
289 # define pos(a) ((a)>>3)
290 #else
291 # define CUBE_SIZE 4
292 # define pos(a) ((a)>>2)
293 #endif
295 #define STARTING_QIXES 2
296 #define MAX_LEVEL 10
297 #define MAX_QIXES MAX_LEVEL+STARTING_QIXES
298 #define BOARD_W ((int)(LCD_WIDTH/CUBE_SIZE))
299 #define BOARD_H ((int)(LCD_HEIGHT/CUBE_SIZE))
300 #define BOARD_X (LCD_WIDTH-BOARD_W*CUBE_SIZE)/2
301 #define BOARD_Y (LCD_HEIGHT-BOARD_H*CUBE_SIZE)/2
303 #ifdef HAVE_LCD_COLOR
304 #define CLR_RED LCD_RGBPACK(255,0,0) /* used to imply danger */
305 #define CLR_LTBLUE LCD_RGBPACK(125, 145, 180) /* used for frame and filling */
306 #define PLR_COL LCD_WHITE /* color used for the player */
307 #elif LCD_DEPTH>=2
308 #define CLR_RED LCD_DARKGRAY /* used to imply danger */
309 #define CLR_LTBLUE LCD_LIGHTGRAY /* used for frame and filling */
310 #define PLR_COL LCD_BLACK /* color used for the player */
311 #endif
313 #if LCD_DEPTH>=2
314 #define EMPTIED LCD_BLACK /* empty spot */
315 #define FILLED CLR_LTBLUE /* filled spot */
316 #define TRAIL CLR_RED /* the red trail of the player */
317 #define QIX LCD_WHITE
318 #else
319 #define EMPTIED 0
320 #define FILLED 1
321 #define TRAIL 2
322 #define QIX 3
323 #endif
324 #define UNCHECKED 0
325 #define CHECKED 1
326 #define PAINTED -1
327 #define PIC_QIX 0
328 #define PIC_PLAYER 1
330 /* The time (in ms) for one iteration through the game loop - decrease this
331 to speed up the game - note that current_tick is (currently) only accurate
332 to 10ms.
334 static int speed = 6; /* CYCLETIME = (11-speed)*10 ms */
335 static int difficulty = 75; /* Percentage of screen that needs to be filled
336 * in order to win the game */
338 static bool quit = false;
340 static unsigned int board[BOARD_H][BOARD_W];
341 static int testboard[BOARD_H][BOARD_W];
343 #if CUBE_SIZE == 8
345 00011000 0x18 - 11100111 0xe7
346 00111100 0x3c - 11100111 0xe7
347 01111110 0x7e - 11000011 0xc3
348 11111111 0xff - 00000000 0x00
349 11111111 0xff - 00000000 0x00
350 01111110 0x7e - 11000011 0xc3
351 00111100 0x3c - 11100111 0xe7
352 00011000 0x18 - 11100111 0xe7
354 const unsigned char pics[2][8] = {
355 {0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18}, /* Alien (QIX) */
356 {0xe7, 0xe7, 0xc3, 0x00, 0x00, 0xc3, 0xe7, 0xe7} /* Player (XONIX) */
358 #elif CUBE_SIZE == 4
360 0110 0x6 - 1001 0x9
361 1111 0xf - 0110 0x6
362 1111 0xf - 0110 0x6
363 0110 0x6 - 1001 0x9
365 const unsigned char pics[2][4] = {
366 {0x6, 0xf, 0xf, 0x6}, /* Alien (QIX) */
367 {0x9, 0x6, 0x6, 0x9} /* Player (XONIX) */
369 #else
370 #error Incorrect CUBE_SIZE value.
371 #endif
373 static struct qix
375 int velocity; /* velocity */
376 int x, y; /* position on screen */
377 int angle; /* angle */
378 } qixes[MAX_QIXES]; /* black_qix */
380 static struct splayer
382 int i, j; /* position on board */
383 int move, score, level, lives;
384 bool drawing;
385 bool gameover;
386 } player;
388 static int percentage_cache;
390 /*************************** STACK STUFF **********************/
392 /* the stack */
393 #define STACK_SIZE (2*BOARD_W*BOARD_H)
394 static struct pos
396 int x, y; /* position on board */
397 } stack[STACK_SIZE];
398 static int stackPointer;
400 static inline bool pop (struct pos *p)
402 if (stackPointer > 0) {
403 p->x = stack[stackPointer].x;
404 p->y = stack[stackPointer].y;
405 stackPointer--;
406 return true;
407 } else
408 return false; /* SE */
411 static inline bool push (struct pos *p)
413 if (stackPointer < STACK_SIZE - 1) {
414 stackPointer++;
415 stack[stackPointer].x = p->x;
416 stack[stackPointer].y = p->y;
417 return true;
418 } else
419 return false; /* SOF */
422 static inline void emptyStack (void)
424 stackPointer = 0;
427 /*********************** END OF STACK STUFF *********************/
429 /* calculate the new x coordinate of the ball according to angle and speed */
430 static inline int get_newx (int x, int len, int deg)
432 if (deg & DIR_R)
433 return x + len;
434 else if (deg & DIR_L)
435 return x - len;
436 else if (deg & DIR_RR)
437 return x + len * 2;
438 else /* (def & DIR_LL) */
439 return x - len * 2;
442 /* calculate the new y coordinate of the ball according to angle and speed */
443 static inline int get_newy (int y, int len, int deg)
445 if (deg & DIR_D)
446 return y + len;
447 else if (deg & DIR_U)
448 return y - len;
449 else if (deg & DIR_DD)
450 return y + len * 2;
451 else /* (deg & DIR_UU) */
452 return y - len * 2;
455 /* make random function get it's value from the device ticker */
456 static inline void randomize (void)
458 rb->srand (*rb->current_tick);
461 /* get a random number between 0 and range-1 */
462 static int t_rand (int range)
464 return rb->rand () % range;
467 /* initializes the test help board */
468 static void init_testboard (void)
470 int j; /* testboard */
471 for (j = 0; j < BOARD_H; j++)
472 /* UNCHEKED == (int)0 */
473 rb->memset( testboard[j], 0, BOARD_W * sizeof( int ) );
476 /* initializes the game board on with the player,qix's and black qix */
477 static void init_board (void)
479 int i, j;
480 for (j = 0; j < BOARD_H; j++)
481 for (i = 0; i < BOARD_W; i++) { /* make a nice cyan frame */
482 if ((i == 0) || (j <= 1) || (i == BOARD_W - 1)
483 || (j >= BOARD_H - 2))
484 board[j][i] = FILLED;
485 else
486 board[j][i] = EMPTIED;
489 /* (level+2) is the number of qixes */
490 for (j = 0; j < player.level + STARTING_QIXES; j++) {
491 qixes[j].velocity = t_rand (2) + 1; /* 1 or 2 pix-per-sec */
493 /* not on frame */
494 qixes[j].x = CUBE_SIZE*2 + 2*t_rand (((BOARD_W-4)*CUBE_SIZE)/2);
495 qixes[j].y = CUBE_SIZE*2 + 2*t_rand (((BOARD_H-4)*CUBE_SIZE)/2);
497 const int angle_table[] = {
498 MOVE_UUR, MOVE_UR, MOVE_URR, MOVE_DRR, MOVE_DR, MOVE_DDR,
499 MOVE_UUL, MOVE_UL, MOVE_ULL, MOVE_DLL, MOVE_DL, MOVE_DDL };
500 qixes[j].angle = angle_table[t_rand (12)];
501 #if CUBE_SIZE == 4
502 /* Work arround a nasty bug. FIXME */
503 if( qixes[j].angle & (DIR_LL|DIR_RR|DIR_UU|DIR_DD) )
504 qixes[j].velocity = 1;
505 #endif
507 /*black_qix.velocity=1;
508 black_qix.x=BOARD_X+(BOARD_W*CUBE_SIZE)/2-CUBE_SIZE/2;
509 black_qix.y=BOARD_Y+(BOARD_H*CUBE_SIZE)-CUBE_SIZE-CUBE_SIZE/2;
510 black_qix.angle=MOVE_UR; */
511 player.move = MOVE_NO;
512 player.drawing = false;
513 player.i = BOARD_W / 2;
514 player.j = 1;
516 percentage_cache = 0;
519 /* calculates the percentage of the screen filling */
520 static int percentage (void)
522 int i, j, filled = 0;
523 for (j = 2; j < BOARD_H - 2; j++)
524 for (i = 1; i < BOARD_W - 1; i++)
525 if (board[j][i] == FILLED)
526 filled++;
527 return (filled * 100) / ((BOARD_W - 2) * (BOARD_H - 4));
530 /* draw the board on with all the game figures */
531 static void refresh_board (void)
533 int i, j;
534 char str[25];
536 #if LCD_DEPTH>=2
537 rb->lcd_set_background (LCD_BLACK);
538 #endif
539 rb->lcd_clear_display ();
540 for (j = 0; j < BOARD_H; j++)
542 unsigned last_color = board[j][0];
543 int last_i = 0;
544 for (i = 1; i < BOARD_W; i++) {
545 if( last_color != board[j][i] )
547 #if LCD_DEPTH>=2
548 rb->lcd_set_foreground (last_color);
549 #else
550 if (last_color != EMPTIED)
551 #endif
552 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
553 BOARD_Y + CUBE_SIZE * j,
554 CUBE_SIZE * (i - last_i), CUBE_SIZE );
555 last_color = board[j][i];
556 last_i = i;
559 #if LCD_DEPTH>=2
560 rb->lcd_set_foreground (last_color);
561 #else
562 if (last_color != EMPTIED)
563 #endif
564 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
565 BOARD_Y + CUBE_SIZE * j,
566 CUBE_SIZE * (i - last_i), CUBE_SIZE);
569 #if LCD_DEPTH>=2
570 rb->lcd_set_foreground (LCD_BLACK);
571 rb->lcd_set_background (CLR_LTBLUE);
572 #else
573 rb->lcd_set_drawmode (DRMODE_COMPLEMENT);
574 #endif
575 rb->snprintf (str, sizeof (str), "Level %d", player.level + 1);
576 rb->lcd_putsxy (BOARD_X, BOARD_Y, str);
577 rb->snprintf (str, sizeof (str), "%d%%", percentage_cache);
578 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 24, BOARD_Y, str);
579 rb->snprintf (str, sizeof (str), "Score: %d", player.score);
580 rb->lcd_putsxy (BOARD_X, BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
581 rb->snprintf (str, sizeof (str),
582 (player.lives != 1) ? "%d Lives" : "%d Life", player.lives);
583 #if LCD_DEPTH>=2
584 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 60,
585 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
586 #else
587 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 40,
588 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
589 #endif
591 #if LCD_DEPTH>=2
592 rb->lcd_set_foreground (PLR_COL);
593 rb->lcd_set_background (board[player.j][player.i]);
594 #endif
595 rb->lcd_mono_bitmap (pics[PIC_PLAYER], player.i * CUBE_SIZE + BOARD_X,
596 player.j * CUBE_SIZE + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
598 #if LCD_DEPTH>=2
599 rb->lcd_set_background (EMPTIED);
600 rb->lcd_set_foreground (LCD_WHITE);
601 rb->lcd_set_drawmode (DRMODE_FG);
602 #else
603 rb->lcd_set_drawmode (DRMODE_FG);
604 #endif
605 for (j = 0; j < player.level + STARTING_QIXES; j++)
606 rb->lcd_mono_bitmap (pics[PIC_QIX], qixes[j].x + BOARD_X,
607 qixes[j].y + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
608 #if LCD_DEPTH>=2
609 rb->lcd_set_foreground (LCD_BLACK);
610 #endif
611 rb->lcd_set_drawmode (DRMODE_SOLID);
613 rb->lcd_update ();
616 static inline int infested_area (int i, int j, int v)
618 struct pos p;
619 p.x = i;
620 p.y = j;
621 emptyStack ();
622 if (!push (&p))
623 return -1;
624 while (pop (&p)) {
625 if (testboard[p.y][p.x] == v) continue;
626 if (testboard[p.y][p.x] > UNCHECKED)
627 return 1; /* This area was previously flagged as infested */
628 testboard[p.y][p.x] = v;
629 if (board[p.y][p.x] == QIX)
630 return 1; /* Infested area */
632 struct pos p1 = { p.x+1, p.y };
633 if ((p1.x < BOARD_W)
634 && (board[p1.y][p1.x] != FILLED)
635 && (!push (&p1)))
636 return -1;
639 struct pos p1 = { p.x-1, p.y };
640 if ((p1.x >= 0)
641 && (board[p1.y][p1.x] != FILLED)
642 && (!push (&p1)))
643 return -1;
646 struct pos p1 = { p.x, p.y+1 };
647 if ((p1.y < BOARD_H)
648 && (board[p1.y][p1.x] != FILLED)
649 && (!push (&p1)))
650 return -1;
653 struct pos p1 = { p.x, p.y-1 };
654 if ((p1.y >= 0)
655 && (board[p1.y][p1.x] != FILLED)
656 && (!push (&p1)))
657 return -1;
660 return 0;
663 static inline int fill_area (int i, int j)
665 struct pos p;
666 p.x = i;
667 p.y = j;
668 int v = testboard[p.y][p.x];
669 emptyStack ();
670 if (!push (&p))
671 return -1;
672 while (pop (&p)) {
673 board[p.y][p.x] = FILLED;
674 testboard[p.y][p.x] = PAINTED;
676 struct pos p1 = { p.x+1, p.y };
677 if ((p1.x < BOARD_W)
678 && (testboard[p1.y][p1.x] == v)
679 && (!push (&p1)))
680 return -1;
683 struct pos p1 = { p.x-1, p.y };
684 if ((p1.x >= 0)
685 && (testboard[p1.y][p1.x] == v)
686 && (!push (&p1)))
687 return -1;
690 struct pos p1 = { p.x, p.y+1 };
691 if ((p1.y < BOARD_H)
692 && (testboard[p1.y][p1.x] == v)
693 && (!push (&p1)))
694 return -1;
697 struct pos p1 = { p.x, p.y-1 };
698 if ((p1.y >= 0)
699 && (testboard[p1.y][p1.x] == v)
700 && (!push (&p1)))
701 return -1;
704 return 0;
708 /* take care of stuff after xonix has landed on a filled spot */
709 static void complete_trail (int fill)
711 int i, j, ret;
712 for (j = 0; j < BOARD_H; j++) {
713 for (i = 0; i < BOARD_W; i++) {
714 if (board[j][i] == TRAIL) {
715 if (fill)
716 board[j][i] = FILLED;
717 else
718 board[j][i] = EMPTIED;
723 if (fill) {
724 int v = CHECKED;
725 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
726 board[pos(qixes[i].y - BOARD_Y)]
727 [pos(qixes[i].x - BOARD_X)] = QIX;
729 init_testboard();
730 for (j = 1; j < BOARD_H - 1; j++) {
731 for (i = 0; i < BOARD_W - 0; i++) {
732 if (board[j][i] != FILLED) {
733 ret = infested_area (i, j, v);
734 if (ret < 0 || ( ret == 0 && fill_area (i, j) ) )
735 quit = true;
736 v++;
741 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
742 board[pos(qixes[i].y - BOARD_Y)]
743 [pos(qixes[i].x - BOARD_X)] = EMPTIED;
744 percentage_cache = percentage();
747 rb->button_clear_queue();
750 /* returns the color the real pixel(x,y) on the lcd is pointing at */
751 static inline unsigned int getpixel (int x, int y)
753 const int a = pos (x - BOARD_X), b = pos (y - BOARD_Y);
754 if ((a > 0) && (a < BOARD_W) && (b > 0) && (b < BOARD_H)) /* if inside board */
755 return board[b][a];
756 else
757 return FILLED;
760 /* returns the color the ball on (newx,newy) is heading at *----*
761 checks the four edge points of the square if 1st of all | |
762 are a trail (cause it's a lose life situation) and 2nd | |
763 if it's filled so it needs to bounce. *____*
765 static inline unsigned int next_hit (int newx, int newy)
767 if ((getpixel (newx, newy) == TRAIL)
768 || (getpixel (newx, newy + CUBE_SIZE - 1) == TRAIL)
769 || (getpixel (newx + CUBE_SIZE - 1, newy) == TRAIL)
770 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) == TRAIL))
771 return TRAIL;
772 else if ((getpixel (newx, newy) == FILLED)
773 || (getpixel (newx, newy + CUBE_SIZE - 1) == FILLED)
774 || (getpixel (newx + CUBE_SIZE - 1, newy) == FILLED)
775 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) ==
776 FILLED))
777 return FILLED;
778 else
779 return EMPTIED;
782 static void die (void)
784 player.lives--;
785 if (player.lives == 0)
786 player.gameover = true;
787 else {
788 refresh_board ();
789 rb->splash (HZ, "Crash!");
790 complete_trail (false);
791 player.move = MOVE_NO;
792 player.drawing = false;
793 player.i = BOARD_W / 2;
794 player.j = 1;
798 /* returns true if the (side) of the block -***-
799 starting from (newx,newy) has any filled pixels * *
800 -***-
802 static inline bool line_check_lt (int newx, int newy)
804 return getpixel (newx, newy + CUBE_SIZE/2-1) == FILLED
805 && getpixel (newx, newy + CUBE_SIZE/2 ) == FILLED;
807 static inline bool line_check_rt (int newx, int newy)
809 return getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2-1) == FILLED
810 && getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2 ) == FILLED;
812 static inline bool line_check_up (int newx, int newy)
814 return getpixel (newx + CUBE_SIZE/2-1, newy) == FILLED
815 && getpixel (newx + CUBE_SIZE/2 , newy) == FILLED;
817 static inline bool line_check_dn (int newx, int newy)
819 return getpixel (newx + CUBE_SIZE/2-1, newy + CUBE_SIZE-1) == FILLED
820 && getpixel (newx + CUBE_SIZE/2 , newy + CUBE_SIZE-1) == FILLED;
823 static inline void move_qix (struct qix *q)
825 int newx, newy;
826 newx = get_newx (q->x, q->velocity, q->angle);
827 newy = get_newy (q->y, q->velocity, q->angle);
828 switch (next_hit (newx, newy))
830 case EMPTIED:
831 q->x = newx;
832 q->y = newy;
833 break;
834 case FILLED:
836 const int a = q->angle;
837 q->angle =
838 ((a&(DIR_UU|DIR_U))
839 ? (line_check_up (newx, newy) ? ((a&(DIR_UU|DIR_U))>>4)
840 : (a&(DIR_UU|DIR_U)))
841 : 0)
843 ((a&(DIR_RR|DIR_R))
844 ? (line_check_rt (newx, newy) ? ((a&(DIR_RR|DIR_R))>>4)
845 : (a&(DIR_RR|DIR_R)))
846 : 0)
848 ((a&(DIR_DD|DIR_D))
849 ? (line_check_dn (newx, newy) ? ((a&(DIR_DD|DIR_D))<<4)
850 : (a&(DIR_DD|DIR_D)))
851 : 0)
853 ((a&(DIR_LL|DIR_L))
854 ? (line_check_lt (newx, newy) ? ((a&(DIR_LL|DIR_L))<<4)
855 : (a&(DIR_LL|DIR_L)))
856 : 0);
857 q->x = get_newx (q->x, q->velocity, q->angle);
858 q->y = get_newy (q->y, q->velocity, q->angle);
859 break;
861 case TRAIL:
862 die();
863 break;
867 /* move the board forward timewise */
868 static inline void move_board (void)
870 int j, newi, newj;
872 for (j = 0; j < player.level + STARTING_QIXES; j++)
873 move_qix (&qixes[j]);
874 /* move_qix(&black_qix,true); */
875 if (player.move) {
876 newi = player.i;
877 newj = player.j;
878 switch (player.move) {
879 case MOVE_UP:
880 if (player.j > 1)
881 newj--;
882 break;
883 case MOVE_DN:
884 if (player.j < BOARD_H - 2)
885 newj++;
886 break;
887 case MOVE_LT:
888 if (player.i > 0)
889 newi--;
890 break;
891 case MOVE_RT:
892 if (player.i < BOARD_W - 1)
893 newi++;
894 break;
895 default:
896 break;
899 if ((player.drawing) && (board[newj][newi] == EMPTIED)) /* continue drawing */
900 board[newj][newi] = TRAIL;
901 else if ((player.drawing) && (board[newj][newi] == FILLED)) { /* finish drawing */
902 player.move = MOVE_NO; /* stop moving */
903 player.drawing = false;
904 complete_trail (true);
905 } else if ((board[player.j][player.i] == FILLED)
906 && (board[newj][newi] == EMPTIED)) {
907 /* start drawing */
908 player.drawing = true;
909 board[newj][newi] = TRAIL;
910 /* if the block after next is empty and we're moving onto filled, stop */
911 } else if ((board[newj][newi] == FILLED)
912 && (board[newj + newj-player.j][newi + newi-player.i] == EMPTIED)) {
913 player.move = MOVE_NO;
915 player.i = newi;
916 player.j = newj;
918 if (percentage_cache >= difficulty) { /* finished level */
919 refresh_board ();
920 rb->splashf (HZ * 2, "Level %d finished", player.level+1);
921 player.score += percentage_cache;
922 if (player.level < MAX_LEVEL)
923 player.level++;
924 init_board ();
925 refresh_board ();
926 rb->splash (HZ * 2, "Ready?");
930 /* init game's variables */
931 static void init_game (void)
933 player.level = 0;
934 player.score = 0;
935 player.lives = 3;
936 player.gameover = false;
937 player.drawing = false;
938 init_board ();
939 refresh_board ();
940 rb->splash (HZ * 2, "Ready?");
943 /* the main menu */
944 static bool _ingame;
945 static int xobox_menu_cb(int action, const struct menu_item_ex *this_item)
947 if(action == ACTION_REQUEST_MENUITEM
948 && !_ingame && ((intptr_t)this_item)==0)
949 return ACTION_EXIT_MENUITEM;
950 return action;
953 static int xobox_menu(bool ingame)
955 rb->button_clear_queue();
957 int selection = 0;
958 MENUITEM_STRINGLIST(main_menu, "Xobox Menu", xobox_menu_cb,
959 "Resume Game", "Start New Game",
960 "Speed", "Difficulty",
961 "Playback Control", "Quit");
962 _ingame = ingame;
964 while (true) {
965 switch (rb->do_menu(&main_menu, &selection, NULL, false)) {
966 case 0:
967 return 0;
968 case 1:
969 init_game ();
970 return 0;
971 case 2:
972 rb->set_int ("Speed", "", UNIT_INT, &speed, NULL, 1, 1, 10, NULL);
973 break;
974 case 3:
975 rb->set_int ("Difficulty", "", UNIT_INT, &difficulty, NULL,
976 5, 50, 95, NULL);
977 break;
978 case 4:
979 playback_control(NULL);
980 break;
981 case 5:
982 return 1;
983 case MENU_ATTACHED_USB:
984 return 1;
985 default:
986 break;
991 /* general keypad handler loop */
992 static int xobox_loop (void)
994 int button = 0;
995 bool pause = false;
996 int end;
998 if (xobox_menu(false)) {
999 return PLUGIN_OK;
1002 while (!quit) {
1003 end = *rb->current_tick + ((11-speed)*HZ)/100;
1005 #ifdef HAS_BUTTON_HOLD
1006 if (rb->button_hold()) {
1007 pause = true;
1008 rb->splash (HZ, "Paused");
1010 #endif
1012 button = rb->button_get_w_tmo (1);
1013 switch (button) {
1014 case UP:
1015 case UP|BUTTON_REPEAT:
1016 player.move = MOVE_UP;
1017 break;
1018 case DOWN:
1019 case DOWN|BUTTON_REPEAT:
1020 player.move = MOVE_DN;
1021 break;
1022 case LEFT:
1023 case LEFT|BUTTON_REPEAT:
1024 player.move = MOVE_LT;
1025 break;
1026 case RIGHT:
1027 case RIGHT|BUTTON_REPEAT:
1028 player.move = MOVE_RT;
1029 break;
1030 case PAUSE:
1031 pause = !pause;
1032 if (pause)
1033 rb->splash (HZ, "Paused");
1034 break;
1035 case QUIT:
1036 if (!pause) {
1037 if (xobox_menu(true)) {
1038 quit = true;
1041 break;
1042 default:
1043 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
1044 return PLUGIN_USB_CONNECTED;
1045 break;
1047 if (!pause) {
1048 move_board ();
1049 refresh_board ();
1051 if (player.gameover) {
1052 rb->splash (HZ, "Game Over!");
1053 if (xobox_menu(false)) {
1054 quit = true;
1058 if (TIME_BEFORE(*rb->current_tick, end))
1059 rb->sleep (end - *rb->current_tick);
1060 else
1061 rb->yield ();
1063 } /* end while */
1064 return PLUGIN_OK; /* for no warnings on compiling */
1067 /* plugin main procedure */
1068 enum plugin_status plugin_start (const void *parameter)
1070 int ret = PLUGIN_OK;
1072 (void) parameter;
1074 rb->lcd_setfont (FONT_SYSFIXED);
1075 #if LCD_DEPTH>=2
1076 rb->lcd_set_backdrop(NULL);
1077 #endif
1079 /* Turn off backlight timeout */
1080 backlight_force_on(); /* backlight control in lib/helper.c */
1082 randomize ();
1083 ret = xobox_loop ();
1085 /* Turn on backlight timeout (revert to settings) */
1086 backlight_use_settings(); /* backlight control in lib/helper.c */
1087 rb->lcd_setfont (FONT_UI);
1089 return ret;