new skin token: %cx - 24 hour time format enabled in the setting.. e.g %?cx<24 hour...
[kugel-rb.git] / apps / plugins / snake.c
blob87b68d2055d1ba014886d33c9ee3b052dde312ef
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Itai Shaked
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 Snake!
25 by Itai Shaked
27 ok, a little explanation -
28 board holds the snake and apple position - 1+ - snake body (the number
29 represents the age [1 is the snake's head]).
30 -1 is an apple, and 0 is a clear spot.
31 dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
35 #include "plugin.h"
36 #ifdef HAVE_LCD_BITMAP
37 #include "lib/configfile.h"
38 #include "lib/playback_control.h"
40 PLUGIN_HEADER
42 /* variable button definitions */
43 #if CONFIG_KEYPAD == RECORDER_PAD
44 #define SNAKE_QUIT BUTTON_OFF
45 #define SNAKE_LEFT BUTTON_LEFT
46 #define SNAKE_RIGHT BUTTON_RIGHT
47 #define SNAKE_UP BUTTON_UP
48 #define SNAKE_DOWN BUTTON_DOWN
49 #define SNAKE_PLAYPAUSE BUTTON_PLAY
51 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
52 #define SNAKE_QUIT BUTTON_OFF
53 #define SNAKE_LEFT BUTTON_LEFT
54 #define SNAKE_RIGHT BUTTON_RIGHT
55 #define SNAKE_UP BUTTON_UP
56 #define SNAKE_DOWN BUTTON_DOWN
57 #define SNAKE_PLAYPAUSE BUTTON_SELECT
59 #elif CONFIG_KEYPAD == ONDIO_PAD
60 #define SNAKE_QUIT BUTTON_OFF
61 #define SNAKE_LEFT BUTTON_LEFT
62 #define SNAKE_RIGHT BUTTON_RIGHT
63 #define SNAKE_UP BUTTON_UP
64 #define SNAKE_DOWN BUTTON_DOWN
65 #define SNAKE_PLAYPAUSE BUTTON_MENU
67 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
68 (CONFIG_KEYPAD == IRIVER_H300_PAD)
69 #define SNAKE_QUIT BUTTON_OFF
70 #define SNAKE_LEFT BUTTON_LEFT
71 #define SNAKE_RIGHT BUTTON_RIGHT
72 #define SNAKE_UP BUTTON_UP
73 #define SNAKE_DOWN BUTTON_DOWN
74 #define SNAKE_PLAYPAUSE BUTTON_ON
76 #define SNAKE_RC_QUIT BUTTON_RC_STOP
78 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
79 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
80 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
81 #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU)
82 #define SNAKE_LEFT BUTTON_LEFT
83 #define SNAKE_RIGHT BUTTON_RIGHT
84 #define SNAKE_UP BUTTON_MENU
85 #define SNAKE_DOWN BUTTON_PLAY
86 #define SNAKE_PLAYPAUSE BUTTON_SELECT
88 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
89 #define SNAKE_QUIT BUTTON_POWER
90 #define SNAKE_LEFT BUTTON_LEFT
91 #define SNAKE_RIGHT BUTTON_RIGHT
92 #define SNAKE_UP BUTTON_UP
93 #define SNAKE_DOWN BUTTON_DOWN
94 #define SNAKE_PLAYPAUSE BUTTON_PLAY
96 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
97 #define SNAKE_QUIT BUTTON_POWER
98 #define SNAKE_LEFT BUTTON_LEFT
99 #define SNAKE_RIGHT BUTTON_RIGHT
100 #define SNAKE_UP BUTTON_UP
101 #define SNAKE_DOWN BUTTON_DOWN
102 #define SNAKE_PLAYPAUSE BUTTON_SELECT
104 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
105 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
106 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
107 (CONFIG_KEYPAD == SANSA_M200_PAD)
108 #define SNAKE_QUIT BUTTON_POWER
109 #define SNAKE_LEFT BUTTON_LEFT
110 #define SNAKE_RIGHT BUTTON_RIGHT
111 #define SNAKE_UP BUTTON_UP
112 #define SNAKE_DOWN BUTTON_DOWN
113 #define SNAKE_PLAYPAUSE BUTTON_SELECT
115 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
116 #define SNAKE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
117 #define SNAKE_LEFT BUTTON_LEFT
118 #define SNAKE_RIGHT BUTTON_RIGHT
119 #define SNAKE_UP BUTTON_UP
120 #define SNAKE_DOWN BUTTON_DOWN
121 #define SNAKE_PLAYPAUSE BUTTON_SELECT
123 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
124 #define SNAKE_QUIT BUTTON_POWER
125 #define SNAKE_LEFT BUTTON_LEFT
126 #define SNAKE_RIGHT BUTTON_RIGHT
127 #define SNAKE_UP BUTTON_SCROLL_UP
128 #define SNAKE_DOWN BUTTON_SCROLL_DOWN
129 #define SNAKE_PLAYPAUSE BUTTON_PLAY
131 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
132 #define SNAKE_QUIT BUTTON_BACK
133 #define SNAKE_LEFT BUTTON_LEFT
134 #define SNAKE_RIGHT BUTTON_RIGHT
135 #define SNAKE_UP BUTTON_UP
136 #define SNAKE_DOWN BUTTON_DOWN
137 #define SNAKE_PLAYPAUSE BUTTON_SELECT
139 #elif (CONFIG_KEYPAD == MROBE100_PAD)
140 #define SNAKE_QUIT BUTTON_POWER
141 #define SNAKE_LEFT BUTTON_LEFT
142 #define SNAKE_RIGHT BUTTON_RIGHT
143 #define SNAKE_UP BUTTON_UP
144 #define SNAKE_DOWN BUTTON_DOWN
145 #define SNAKE_PLAYPAUSE BUTTON_SELECT
147 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
148 #define SNAKE_QUIT BUTTON_RC_REC
149 #define SNAKE_LEFT BUTTON_RC_REW
150 #define SNAKE_RIGHT BUTTON_RC_FF
151 #define SNAKE_UP BUTTON_RC_VOL_UP
152 #define SNAKE_DOWN BUTTON_RC_VOL_DOWN
153 #define SNAKE_PLAYPAUSE BUTTON_RC_PLAY
155 #define SNAKE_RC_QUIT BUTTON_REC
157 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
158 #define SNAKE_QUIT BUTTON_BACK
159 #define SNAKE_LEFT BUTTON_LEFT
160 #define SNAKE_RIGHT BUTTON_RIGHT
161 #define SNAKE_UP BUTTON_UP
162 #define SNAKE_DOWN BUTTON_DOWN
163 #define SNAKE_PLAYPAUSE BUTTON_PLAY
165 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
166 #define SNAKE_QUIT BUTTON_POWER
167 #define SNAKE_LEFT BUTTON_LEFT
168 #define SNAKE_RIGHT BUTTON_RIGHT
169 #define SNAKE_UP BUTTON_UP
170 #define SNAKE_DOWN BUTTON_DOWN
171 #define SNAKE_PLAYPAUSE BUTTON_MENU
173 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
174 #define SNAKE_QUIT BUTTON_REC
175 #define SNAKE_LEFT BUTTON_LEFT
176 #define SNAKE_RIGHT BUTTON_RIGHT
177 #define SNAKE_UP BUTTON_UP
178 #define SNAKE_DOWN BUTTON_DOWN
179 #define SNAKE_PLAYPAUSE BUTTON_PLAY
181 #elif CONFIG_KEYPAD == MROBE500_PAD
182 #define SNAKE_QUIT BUTTON_POWER
183 #define SNAKE_RC_QUIT BUTTON_RC_DOWN
185 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
186 #define SNAKE_QUIT BUTTON_POWER
188 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
189 #define SNAKE_QUIT BUTTON_POWER
191 #elif CONFIG_KEYPAD == COWOND2_PAD
192 #define SNAKE_QUIT BUTTON_POWER
194 #else
195 #error No keymap defined!
196 #endif
198 #ifdef HAVE_TOUCHSCREEN
199 #ifndef SNAKE_QUIT
200 #define SNAKE_QUIT BUTTON_TOPLEFT
201 #endif
202 #ifndef SNAKE_LEFT
203 #define SNAKE_LEFT BUTTON_MIDLEFT
204 #endif
205 #ifndef SNAKE_RIGHT
206 #define SNAKE_RIGHT BUTTON_MIDRIGHT
207 #endif
208 #ifndef SNAKE_UP
209 #define SNAKE_UP BUTTON_TOPMIDDLE
210 #endif
211 #ifndef SNAKE_DOWN
212 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
213 #endif
214 #ifndef SNAKE_PLAYPAUSE
215 #define SNAKE_PLAYPAUSE BUTTON_CENTER
216 #endif
217 #endif
219 #define BOARD_WIDTH (LCD_WIDTH/4)
220 #define BOARD_HEIGHT (LCD_HEIGHT/4)
222 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
223 static unsigned int score,hiscore=0,level=1;
224 static int dir;
225 static bool apple, dead;
227 #define CONFIG_FILE_NAME "snake.cfg"
228 static struct configdata config[] = {
229 {TYPE_INT, 0, 10, { .int_p = &level }, "level", NULL},
230 {TYPE_INT, 0, 10000, { .int_p = &hiscore }, "hiscore", NULL},
233 static void snake_die (void)
235 char pscore[17];
236 rb->lcd_clear_display();
237 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
238 rb->lcd_puts(0,0,"Oops...");
239 rb->lcd_puts(0,1, pscore);
240 if (score>hiscore) {
241 hiscore=score;
242 rb->lcd_puts(0,2,"New High Score!");
244 else {
245 rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore);
246 rb->lcd_puts(0,2,pscore);
248 rb->lcd_update();
249 rb->sleep(3*HZ);
250 dead=true;
253 static void snake_colission (short x, short y)
255 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0) {
256 snake_die();
257 return;
259 switch (board[x][y]) {
260 case 0:
261 break;
262 case -1:
263 snakelength+=2;
264 score+=level;
265 apple=false;
266 break;
267 default:
268 snake_die();
269 break;
273 static void snake_move_head (short x, short y)
275 switch (dir) {
276 case 0:
277 y-=1;
278 break;
279 case 1:
280 x+=1;
281 break;
282 case 2:
283 y+=1;
284 break;
285 case 3:
286 x-=1;
287 break;
289 snake_colission (x,y);
290 if (dead)
291 return;
292 board[x][y]=1;
293 rb->lcd_fillrect(x*4,y*4,4,4);
296 static void snake_redraw (void)
298 short x,y;
299 rb->lcd_clear_display();
300 for (x=0; x<BOARD_WIDTH; x++) {
301 for (y=0; y<BOARD_HEIGHT; y++) {
302 switch (board[x][y]) {
303 case -1:
304 rb->lcd_fillrect((x*4)+1,y*4,2,4);
305 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
306 break;
307 case 0:
308 break;
309 default:
310 rb->lcd_fillrect(x*4,y*4,4,4);
311 break;
315 rb->lcd_update();
318 static void snake_frame (void)
320 short x,y,head=0;
321 for (x=0; x<BOARD_WIDTH; x++) {
322 for (y=0; y<BOARD_HEIGHT; y++) {
323 switch (board[x][y]) {
324 case 1:
325 if (!head) {
326 snake_move_head(x,y);
327 if (dead)
328 return;
329 board[x][y]++;
330 head=1;
332 break;
333 case 0:
334 break;
335 case -1:
336 break;
337 default:
338 if (board[x][y]==snakelength) {
339 board[x][y]=0;
340 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
341 rb->lcd_fillrect(x*4,y*4,4,4);
342 rb->lcd_set_drawmode(DRMODE_SOLID);
344 else
345 board[x][y]++;
346 break;
350 rb->lcd_update();
353 static void snake_game_init(void) {
354 short x,y;
355 rb->lcd_clear_display();
357 for (x=0; x<BOARD_WIDTH; x++) {
358 for (y=0; y<BOARD_HEIGHT; y++) {
359 board[x][y]=0;
362 apple=false;
363 dead=false;
364 snakelength=4;
365 score=0;
366 board[11][7]=1;
369 static void snake_choose_level(void)
371 rb->set_int("Snake Speed", "", UNIT_INT, &level, NULL, 1, 1, 9, NULL);
374 static bool _ingame;
375 static int snake_menu_cb(int action, const struct menu_item_ex *this_item)
377 if(action == ACTION_REQUEST_MENUITEM
378 && !_ingame && ((intptr_t)this_item)==0)
379 return ACTION_EXIT_MENUITEM;
380 return action;
383 static int snake_game_menu(bool ingame)
385 rb->button_clear_queue();
386 int choice = 0;
388 _ingame = ingame;
390 MENUITEM_STRINGLIST(main_menu,"Snake Menu",snake_menu_cb,
391 "Resume Game",
392 "Start New Game",
393 "Snake Speed",
394 "High Score",
395 "Playback Control",
396 "Quit");
398 while (true) {
399 choice = rb->do_menu(&main_menu, &choice, NULL, false);
400 switch (choice) {
401 case 0:
402 snake_redraw();
403 return 0;
404 case 1:
405 snake_game_init();
406 return 0;
407 case 2:
408 snake_choose_level();
409 break;
410 case 3:
411 rb->splashf(HZ*2, "High Score: %d", hiscore);
412 break;
413 case 4:
414 playback_control(NULL);
415 break;
416 case 5:
417 return 1;
418 case MENU_ATTACHED_USB:
419 return 1;
420 default:
421 break;
426 static int snake_game_loop (void) {
427 int button;
428 short x,y;
429 bool pause = false;
431 if (snake_game_menu(false)==1)
432 return 1;
434 while (true) {
435 if (!pause) {
436 snake_frame();
437 if (dead) {
438 if (snake_game_menu(false)==1)
439 return 1;
441 if (!apple) {
442 do {
443 x=rb->rand() % BOARD_WIDTH;
444 y=rb->rand() % BOARD_HEIGHT;
445 } while (board[x][y]);
446 apple=true;
447 board[x][y]=-1;
448 rb->lcd_fillrect((x*4)+1,y*4,2,4);
449 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
452 rb->sleep(HZ/level);
455 button=rb->button_get(false);
457 #ifdef HAS_BUTTON_HOLD
458 if (rb->button_hold()) {
459 pause = true;
460 rb->splash (HZ, "Paused");
462 #endif
463 switch (button) {
464 case SNAKE_UP:
465 if (dir!=2) dir=0;
466 break;
467 case SNAKE_RIGHT:
468 if (dir!=3) dir=1;
469 break;
470 case SNAKE_DOWN:
471 if (dir!=0) dir=2;
472 break;
473 case SNAKE_LEFT:
474 if (dir!=1) dir=3;
475 break;
476 case SNAKE_PLAYPAUSE:
477 pause = !pause;
478 if (pause)
479 rb->splash (HZ, "Paused");
480 else
481 snake_redraw();
482 break;
483 #ifdef SNAKE_RC_QUIT
484 case SNAKE_RC_QUIT:
485 #endif
486 case SNAKE_QUIT:
487 pause = false;
488 if (snake_game_menu(true)==1)
489 return 1;
490 break;
491 default:
492 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
493 return PLUGIN_USB_CONNECTED;
494 break;
499 enum plugin_status plugin_start(const void* parameter)
501 (void)parameter;
503 configfile_load(CONFIG_FILE_NAME,config,2,0);
504 rb->lcd_clear_display();
505 snake_game_loop();
506 configfile_save(CONFIG_FILE_NAME,config,2,0);
507 return PLUGIN_OK;
509 #endif