1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Philipp Pertermann
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 ****************************************************************************/
22 #include "lib/configfile.h"
23 #include "lib/helper.h"
24 #include "lib/playback_control.h"
28 /* size of the field the worm lives in */
29 #define FIELD_RECT_X 1
30 #define FIELD_RECT_Y 1
31 #define FIELD_RECT_WIDTH (LCD_WIDTH - 45)
32 #define FIELD_RECT_HEIGHT (LCD_HEIGHT - 2)
34 /* when the game starts */
35 #define INITIAL_WORM_LENGTH 10
37 /* num of pixel the worm grows per eaten food */
38 #define WORM_PER_FOOD 7
40 /* num of worms creeping in the FIELD */
43 /* minimal distance between a worm and an argh
44 when a new argh is made */
45 #define MIN_ARGH_DIST 5
47 #if (CONFIG_KEYPAD == RECORDER_PAD)
48 #define BTN_DIR_UP BUTTON_UP
49 #define BTN_DIR_DOWN BUTTON_DOWN
50 #define BTN_DIR_LEFT BUTTON_LEFT
51 #define BTN_DIR_RIGHT BUTTON_RIGHT
52 #define BTN_PLAYER2_DIR1 BUTTON_F2
53 #define BTN_PLAYER2_DIR2 BUTTON_F3
54 #define BTN_STARTPAUSE BUTTON_PLAY
55 #define BTN_QUIT BUTTON_OFF
56 #define BTN_STOPRESET BUTTON_ON
57 #define BTN_TOGGLE_KEYS BUTTON_F1
59 #if BUTTON_REMOTE != 0
60 #define BTN_RC_UP BUTTON_RC_VOL_UP
61 #define BTN_RC_DOWN BUTTON_RC_VOL_DOWN
66 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
67 #define BTN_DIR_UP BUTTON_UP
68 #define BTN_DIR_DOWN BUTTON_DOWN
69 #define BTN_DIR_LEFT BUTTON_LEFT
70 #define BTN_DIR_RIGHT BUTTON_RIGHT
71 #define BTN_PLAYER2_DIR1 BUTTON_F2
72 #define BTN_PLAYER2_DIR2 BUTTON_F3
73 #define BTN_STARTPAUSE BUTTON_SELECT
74 #define BTN_QUIT BUTTON_OFF
75 #define BTN_STOPRESET BUTTON_ON
76 #define BTN_TOGGLE_KEYS BUTTON_F1
78 #elif (CONFIG_KEYPAD == ONDIO_PAD)
79 #define BTN_DIR_UP BUTTON_UP
80 #define BTN_DIR_DOWN BUTTON_DOWN
81 #define BTN_DIR_LEFT BUTTON_LEFT
82 #define BTN_DIR_RIGHT BUTTON_RIGHT
83 #define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL)
84 #define BTN_QUIT (BUTTON_OFF|BUTTON_REL)
85 #define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU)
87 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
88 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
90 #define BTN_DIR_UP BUTTON_MENU
91 #define BTN_DIR_DOWN BUTTON_PLAY
92 #define BTN_DIR_LEFT BUTTON_LEFT
93 #define BTN_DIR_RIGHT BUTTON_RIGHT
94 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
95 #define BTN_QUIT (BUTTON_SELECT|BUTTON_MENU)
96 #define BTN_STOPRESET (BUTTON_SELECT|BUTTON_PLAY)
98 #elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
100 #define BTN_DIR_UP BUTTON_UP
101 #define BTN_DIR_DOWN BUTTON_DOWN
102 #define BTN_DIR_LEFT BUTTON_LEFT
103 #define BTN_DIR_RIGHT BUTTON_RIGHT
104 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
105 #define BTN_QUIT BUTTON_OFF
106 #define BTN_STOPRESET BUTTON_ON
108 #define BTN_RC_QUIT BUTTON_RC_STOP
110 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
112 #define BTN_DIR_UP BUTTON_UP
113 #define BTN_DIR_DOWN BUTTON_DOWN
114 #define BTN_DIR_LEFT BUTTON_LEFT
115 #define BTN_DIR_RIGHT BUTTON_RIGHT
116 #define BTN_STARTPAUSE BUTTON_PLAY
117 #define BTN_QUIT BUTTON_POWER
118 #define BTN_STOPRESET BUTTON_REC
120 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
122 #define BTN_DIR_UP BUTTON_UP
123 #define BTN_DIR_DOWN BUTTON_DOWN
124 #define BTN_DIR_LEFT BUTTON_LEFT
125 #define BTN_DIR_RIGHT BUTTON_RIGHT
126 #define BTN_STARTPAUSE BUTTON_SELECT
127 #define BTN_QUIT BUTTON_POWER
128 #define BTN_STOPRESET BUTTON_A
130 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
131 (CONFIG_KEYPAD == SANSA_C200_PAD)
133 #define BTN_DIR_UP BUTTON_UP
134 #define BTN_DIR_DOWN BUTTON_DOWN
135 #define BTN_DIR_LEFT BUTTON_LEFT
136 #define BTN_DIR_RIGHT BUTTON_RIGHT
137 #define BTN_STARTPAUSE BUTTON_SELECT
138 #define BTN_QUIT BUTTON_POWER
139 #define BTN_STOPRESET BUTTON_REC
141 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
143 #define BTN_DIR_UP BUTTON_UP
144 #define BTN_DIR_DOWN BUTTON_DOWN
145 #define BTN_DIR_LEFT BUTTON_LEFT
146 #define BTN_DIR_RIGHT BUTTON_RIGHT
147 #define BTN_STARTPAUSE BUTTON_SELECT
148 #define BTN_QUIT BUTTON_POWER
149 #define BTN_STOPRESET BUTTON_HOME
151 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
153 #define BTN_DIR_UP BUTTON_UP
154 #define BTN_DIR_DOWN BUTTON_DOWN
155 #define BTN_DIR_LEFT BUTTON_LEFT
156 #define BTN_DIR_RIGHT BUTTON_RIGHT
157 #define BTN_STARTPAUSE BUTTON_SELECT
158 #define BTN_QUIT (BUTTON_HOME|BUTTON_REPEAT)
159 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
161 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
163 #define BTN_DIR_UP BUTTON_UP
164 #define BTN_DIR_DOWN BUTTON_DOWN
165 #define BTN_DIR_LEFT BUTTON_LEFT
166 #define BTN_DIR_RIGHT BUTTON_RIGHT
167 #define BTN_STARTPAUSE (BUTTON_SELECT | BUTTON_REL)
168 #define BTN_QUIT BUTTON_POWER
169 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
171 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
173 #define BTN_DIR_UP BUTTON_SCROLL_UP
174 #define BTN_DIR_DOWN BUTTON_SCROLL_DOWN
175 #define BTN_DIR_LEFT BUTTON_LEFT
176 #define BTN_DIR_RIGHT BUTTON_RIGHT
177 #define BTN_STARTPAUSE BUTTON_PLAY
178 #define BTN_QUIT BUTTON_POWER
179 #define BTN_STOPRESET BUTTON_REW
181 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
183 #define BTN_DIR_UP BUTTON_UP
184 #define BTN_DIR_DOWN BUTTON_DOWN
185 #define BTN_DIR_LEFT BUTTON_LEFT
186 #define BTN_DIR_RIGHT BUTTON_RIGHT
187 #define BTN_STARTPAUSE BUTTON_SELECT
188 #define BTN_QUIT BUTTON_BACK
189 #define BTN_STOPRESET BUTTON_MENU
191 #elif (CONFIG_KEYPAD == MROBE100_PAD)
193 #define BTN_DIR_UP BUTTON_UP
194 #define BTN_DIR_DOWN BUTTON_DOWN
195 #define BTN_DIR_LEFT BUTTON_LEFT
196 #define BTN_DIR_RIGHT BUTTON_RIGHT
197 #define BTN_STARTPAUSE BUTTON_SELECT
198 #define BTN_QUIT BUTTON_POWER
199 #define BTN_STOPRESET BUTTON_DISPLAY
201 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
203 #define BTN_DIR_UP BUTTON_RC_VOL_UP
204 #define BTN_DIR_DOWN BUTTON_RC_VOL_DOWN
205 #define BTN_DIR_LEFT BUTTON_RC_REW
206 #define BTN_DIR_RIGHT BUTTON_RC_FF
207 #define BTN_STARTPAUSE BUTTON_RC_PLAY
208 #define BTN_QUIT BUTTON_RC_REC
209 #define BTN_STOPRESET BUTTON_RC_MODE
211 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
213 #define BTN_QUIT BUTTON_POWER
215 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
217 #define BTN_DIR_UP BUTTON_UP
218 #define BTN_DIR_DOWN BUTTON_DOWN
219 #define BTN_DIR_LEFT BUTTON_LEFT
220 #define BTN_DIR_RIGHT BUTTON_RIGHT
221 #define BTN_STARTPAUSE BUTTON_PLAY
222 #define BTN_QUIT BUTTON_BACK
223 #define BTN_STOPRESET BUTTON_MENU
225 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
227 #define BTN_DIR_UP BUTTON_UP
228 #define BTN_DIR_DOWN BUTTON_DOWN
229 #define BTN_DIR_LEFT BUTTON_LEFT
230 #define BTN_DIR_RIGHT BUTTON_RIGHT
231 #define BTN_STARTPAUSE BUTTON_MENU
232 #define BTN_QUIT BUTTON_POWER
233 #define BTN_STOPRESET BUTTON_VIEW
235 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
237 #define BTN_DIR_UP BUTTON_UP
238 #define BTN_DIR_DOWN BUTTON_DOWN
239 #define BTN_DIR_LEFT BUTTON_PREV
240 #define BTN_DIR_RIGHT BUTTON_RIGHT
241 #define BTN_STARTPAUSE BUTTON_MENU
242 #define BTN_QUIT BUTTON_POWER
243 #define BTN_STOPRESET BUTTON_RIGHT
245 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
246 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
247 CONFIG_KEYPAD == MROBE500_PAD
249 #define BTN_QUIT BUTTON_POWER
251 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
253 #define BTN_DIR_UP BUTTON_UP
254 #define BTN_DIR_DOWN BUTTON_DOWN
255 #define BTN_DIR_LEFT BUTTON_LEFT
256 #define BTN_DIR_RIGHT BUTTON_RIGHT
257 #define BTN_STARTPAUSE BUTTON_PLAY
258 #define BTN_QUIT BUTTON_FFWD
259 #define BTN_STOPRESET BUTTON_REW
261 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
263 #define BTN_DIR_UP BUTTON_UP
264 #define BTN_DIR_DOWN BUTTON_DOWN
265 #define BTN_DIR_LEFT BUTTON_PREV
266 #define BTN_DIR_RIGHT BUTTON_NEXT
267 #define BTN_STARTPAUSE BUTTON_PLAY
268 #define BTN_QUIT BUTTON_REC
269 #define BTN_STOPRESET BUTTON_CANCEL
272 #error No keymap defined!
275 #ifdef HAVE_TOUCHSCREEN
277 #define BTN_DIR_UP BUTTON_TOPMIDDLE
280 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
283 #define BTN_DIR_LEFT BUTTON_MIDLEFT
285 #ifndef BTN_DIR_RIGHT
286 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
288 #ifndef BTN_STARTPAUSE
289 #define BTN_STARTPAUSE BUTTON_CENTER
292 #define BTN_QUIT BUTTON_TOPLEFT
294 #ifndef BTN_STOPRESET
295 #define BTN_STOPRESET BUTTON_TOPRIGHT
299 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
303 #define MAX_WORM_SEGMENTS 128
304 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 64)
308 #define MAX_WORM_SEGMENTS 128
309 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
313 #define MAX_WORM_SEGMENTS 128
314 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
318 #define MAX_WORM_SEGMENTS 128
319 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
323 #define MAX_WORM_SEGMENTS 128
324 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
328 #define MAX_WORM_SEGMENTS 128
329 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
333 #define MAX_WORM_SEGMENTS 256
334 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
338 #define MAX_WORM_SEGMENTS 256
339 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
343 #define MAX_WORM_SEGMENTS 512
344 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
348 #define MAX_WORM_SEGMENTS 512
349 #elif ((LCD_WIDTH == 320) && (LCD_HEIGHT == 240)) || \
350 ((LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400)))
354 #define MAX_WORM_SEGMENTS 512
355 #elif ((LCD_WIDTH == 640) && (LCD_HEIGHT == 480)) || \
356 ((LCD_WIDTH == 480) && (LCD_HEIGHT == 640))
360 #define MAX_WORM_SEGMENTS 512
363 #ifdef HAVE_LCD_COLOR
364 #define COLOR_WORM LCD_RGBPACK(80, 40, 0)
365 #define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
366 #define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
367 #define COLOR_FG LCD_RGBPACK(0, 0, 0)
368 #define COLOR_BG LCD_RGBPACK(181, 199, 231)
371 #define CHECK_SQUARE_COLLISION(x1,y1,s1,x2,y2,s2) (x1+s1>x2)&&(x2+s2>x1)&&(y1+s1>y2)&&(y2+s2>y1)
374 * All the properties that a worm has.
377 /* The worm is stored in a ring of xy coordinates */
378 int x
[MAX_WORM_SEGMENTS
];
379 int y
[MAX_WORM_SEGMENTS
];
381 int head
; /* index of the head within the buffer */
382 int tail
; /* index of the tail within the buffer */
383 int growing
; /* number of cyles the worm still keeps growing */
384 bool alive
; /* the worms living state */
386 /* direction vector in which the worm moves */
387 int dirx
; /* only values -1 0 1 allowed */
388 int diry
; /* only values -1 0 1 allowed */
390 /* this method is used to fetch the direction the user
391 has selected. It can be one of the values
392 human_player1, human_player2, remote_player, virtual_player.
393 All these values are fuctions, that can change the direction
395 void (*fetch_worm_direction
)(struct worm
*w
);
398 /* stores the highscore - besides it was scored by a virtual player */
399 static int highscore
;
401 #define MAX_FOOD 5 /* maximal number of food items */
403 /* The arrays store the food coordinates */
404 static int foodx
[MAX_FOOD
];
405 static int foody
[MAX_FOOD
];
407 #define MAX_ARGH 100 /* maximal number of argh items */
408 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
410 /* The arrays store the argh coordinates */
411 static int arghx
[MAX_ARGH
];
412 static int arghy
[MAX_ARGH
];
414 /* the number of arghs that are currently in use */
415 static int argh_count
;
417 /* the number of arghs per food, settable by user */
418 static int arghs_per_food
= ARGHS_PER_FOOD
;
419 /* the size of the argh, settable by user */
420 static int argh_size
= ARGH_SIZE
;
421 /* the size of the food, settable by user */
422 static int food_size
= FOOD_SIZE
;
423 /* the speed of the worm, settable by user */
424 static int speed
= SPEED
;
425 /* the amount a worm grows by eating a food, settable by user */
426 static int worm_food
= WORM_PER_FOOD
;
428 /* End additional variables */
431 /* just a buffer used for debug output */
432 static char debugout
[15];
435 /* the number of active worms (dead or alive) */
436 static int worm_count
= MAX_WORMS
;
438 /* in multiplayer mode: en- / disables the remote worm control
439 in singleplayer mode: toggles 4 / 2 button worm control */
440 static bool use_remote
= false;
442 /* return values of check_collision */
443 #define COLLISION_NONE 0
444 #define COLLISION_WORM 1
445 #define COLLISION_FOOD 2
446 #define COLLISION_ARGH 3
447 #define COLLISION_FIELD 4
449 /* constants for use as directions.
450 Note that the values are ordered clockwise.
451 Thus increasing / decreasing the values
452 is equivalent to right / left turns. */
458 /* direction of human player 1 */
459 static int player1_dir
= EAST
;
460 /* direction of human player 2 */
461 static int player2_dir
= EAST
;
462 /* direction of human player 3 */
463 static int player3_dir
= EAST
;
465 /* the number of (human) players that currently
467 static int players
= 1;
469 #define SETTINGS_VERSION 1
470 #define SETTINGS_MIN_VERSION 1
471 #define SETTINGS_FILENAME "wormlet.cfg"
473 static struct configdata config
[] =
475 {TYPE_INT
, 0, 1024, { .int_p
= &highscore
}, "highscore", NULL
},
476 {TYPE_INT
, 0, 15, { .int_p
= &arghs_per_food
}, "arghs per food", NULL
},
477 {TYPE_INT
, 0, 15, { .int_p
= &argh_size
}, "argh size", NULL
},
478 {TYPE_INT
, 0, 15, { .int_p
= &food_size
}, "food size", NULL
},
479 {TYPE_INT
, 0, 3, { .int_p
= &players
}, "players", NULL
},
480 {TYPE_INT
, 0, 3, { .int_p
= &worm_count
}, "worms", NULL
},
481 {TYPE_INT
, 0, 20, { .int_p
= &speed
}, "speed", NULL
},
482 {TYPE_INT
, 0, 15, { .int_p
= &worm_food
}, "Worm Growth Per Food", NULL
}
486 static void set_debug_out(char *str
){
487 strcpy(debugout
, str
);
492 * Returns the direction id in which the worm
493 * currently is creeping.
494 * @param struct worm *w The worm that is to be investigated.
495 * w Must not be null.
496 * @return int A value 0 <= value < 4
497 * Note the predefined constants NORTH, SOUTH, EAST, WEST
499 static int get_worm_dir(struct worm
*w
)
519 * Set the direction of the specified worm with a direction id.
520 * Increasing the value by 1 means to turn the worm direction
521 * to right by 90 degree.
522 * @param struct worm *w The worm that is to be altered. w Must not be null.
523 * @param int dir The new direction in which the worm is to creep.
524 * dir must be 0 <= dir < 4. Use predefined constants
525 * NORTH, SOUTH, EAST, WEST
527 static void set_worm_dir(struct worm
*w
, int dir
)
550 * Returns the current length of the worm array. This
551 * is also a value for the number of bends that are in the worm.
552 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
554 static int get_worm_array_length(struct worm
*w
)
556 /* initial simple calculation will be overwritten if wrong. */
557 int retVal
= w
->head
- w
->tail
;
559 /* if the worm 'crosses' the boundaries of the ringbuffer */
561 retVal
= w
->head
+ MAX_WORM_SEGMENTS
- w
->tail
;
568 * Returns the score the specified worm. The score is the length
570 * @param struct worm *w The worm that is to be investigated.
571 * w must not be null.
572 * @return int The length of the worm (>= 0).
574 static int get_score(struct worm
*w
)
577 int length
= get_worm_array_length(w
);
579 for (i
= 0; i
< length
; i
++) {
581 /* The iteration iterates the length of the worm.
582 Here's the conversion to the true indices within the worm arrays. */
583 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
584 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
585 int startx
= w
->x
[linestart
];
586 int starty
= w
->y
[linestart
];
587 int endx
= w
->x
[lineend
];
588 int endy
= w
->y
[lineend
];
590 int minimum
, maximum
;
592 if (startx
== endx
) {
593 minimum
= MIN(starty
, endy
);
594 maximum
= MAX(starty
, endy
);
596 minimum
= MIN(startx
, endx
);
597 maximum
= MAX(startx
, endx
);
599 retval
+= abs(maximum
- minimum
);
605 * Determines wether the line specified by startx, starty, endx, endy intersects
606 * the rectangle specified by x, y, width, height. Note that the line must be exactly
607 * horizontal or vertical (startx == endx or starty == endy).
608 * @param int startx The x coordinate of the start point of the line.
609 * @param int starty The y coordinate of the start point of the line.
610 * @param int endx The x coordinate of the end point of the line.
611 * @param int endy The y coordinate of the end point of the line.
612 * @param int x The x coordinate of the top left corner of the rectangle.
613 * @param int y The y coordinate of the top left corner of the rectangle.
614 * @param int width The width of the rectangle.
615 * @param int height The height of the rectangle.
616 * @return bool Returns true if the specified line intersects with the recangle.
618 static bool line_in_rect(int startx
, int starty
, int endx
, int endy
,
619 int x
, int y
, int width
, int height
)
622 int simple
, simplemin
, simplemax
;
623 int compa
, compb
, compmin
, compmax
;
625 if (startx
== endx
) {
628 simplemax
= x
+ width
;
633 compmax
= y
+ height
;
637 simplemax
= y
+ height
;
646 compa
= MIN(compa
, compb
);
647 compb
= MAX(temp
, compb
);
649 if (simplemin
<= simple
&& simple
<= simplemax
) {
650 if ((compmin
<= compa
&& compa
<= compmax
) ||
651 (compmin
<= compb
&& compb
<= compmax
) ||
652 (compa
<= compmin
&& compb
>= compmax
)) {
660 * Tests wether the specified worm intersects with the rect.
661 * @param struct worm *w The worm to be investigated
662 * @param int x The x coordinate of the top left corner of the rect
663 * @param int y The y coordinate of the top left corner of the rect
664 * @param int widht The width of the rect
665 * @param int height The height of the rect
666 * @return bool Returns true if the worm intersects with the rect
668 static bool worm_in_rect(struct worm
*w
, int x
, int y
, int width
, int height
)
673 /* get_worm_array_length is expensive -> buffer the value */
674 int wormLength
= get_worm_array_length(w
);
677 /* test each entry that is part of the worm */
678 for (i
= 0; i
< wormLength
&& retval
== false; i
++) {
680 /* The iteration iterates the length of the worm.
681 Here's the conversion to the true indices within the worm arrays. */
682 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
683 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
684 int startx
= w
->x
[linestart
];
685 int starty
= w
->y
[linestart
];
686 int endx
= w
->x
[lineend
];
687 int endy
= w
->y
[lineend
];
689 retval
= line_in_rect(startx
, starty
, endx
, endy
, x
, y
, width
, height
);
696 * Checks wether a specific food in the food arrays is at the
697 * specified coordinates.
698 * @param int foodIndex The index of the food in the food arrays
699 * @param int x the x coordinate.
700 * @param int y the y coordinate.
701 * @return Returns true if the coordinate hits the food specified by
704 static bool specific_food_collision(int foodIndex
, int x
, int y
)
707 if (x
>= foodx
[foodIndex
] &&
708 x
< foodx
[foodIndex
] + food_size
&&
709 y
>= foody
[foodIndex
] &&
710 y
< foody
[foodIndex
] + food_size
) {
718 * Returns the index of the food that is at the
719 * given coordinates. If no food is at the coordinates
721 * @return int -1 <= value < MAX_FOOD
723 static int food_collision(int x
, int y
)
727 for (i
= 0; i
< MAX_FOOD
; i
++) {
728 if (specific_food_collision(i
, x
, y
)) {
737 * Checks wether a specific argh in the argh arrays is at the
738 * specified coordinates.
739 * @param int arghIndex The index of the argh in the argh arrays
740 * @param int x the x coordinate.
741 * @param int y the y coordinate.
742 * @return Returns true if the coordinate hits the argh specified by
745 static bool specific_argh_collision(int arghIndex
, int x
, int y
)
747 if ( x
>= arghx
[arghIndex
] &&
748 y
>= arghy
[arghIndex
] &&
749 x
< arghx
[arghIndex
] + argh_size
&&
750 y
< arghy
[arghIndex
] + argh_size
)
759 * Returns the index of the argh that is at the
760 * given coordinates. If no argh is at the coordinates
762 * @param int x The x coordinate.
763 * @param int y The y coordinate.
764 * @return int -1 <= value < argh_count <= MAX_ARGH
766 static int argh_collision(int x
, int y
)
771 /* search for the argh that has the specified coords */
772 for (i
= 0; i
< argh_count
; i
++) {
773 if (specific_argh_collision(i
, x
, y
)) {
782 * Checks wether the worm collides with the food at the specfied food-arrays.
783 * @param int foodIndex The index of the food in the arrays. Ensure the value is
784 * 0 <= foodIndex <= MAX_FOOD
785 * @return Returns true if the worm collides with the specified food.
787 static bool worm_food_collision(struct worm
*w
, int foodIndex
)
791 retVal
= worm_in_rect(w
, foodx
[foodIndex
], foody
[foodIndex
],
792 food_size
- 1, food_size
- 1);
798 * Returns true if the worm hits the argh within the next moves (unless
799 * the worm changes it's direction).
800 * @param struct worm *w - The worm to investigate
801 * @param int argh_idx - The index of the argh
802 * @param int moves - The number of moves that are considered.
803 * @return Returns false if the specified argh is not hit within the next
806 static bool worm_argh_collision_in_moves(struct worm
*w
, int argh_idx
, int moves
)
813 x2
= w
->x
[w
->head
] + moves
* w
->dirx
;
814 y2
= w
->y
[w
->head
] + moves
* w
->diry
;
816 retVal
= line_in_rect(x1
, y1
, x2
, y2
, arghx
[argh_idx
], arghy
[argh_idx
],
817 argh_size
, argh_size
);
822 * Checks wether the worm collides with the argh at the specfied argh-arrays.
823 * @param int arghIndex The index of the argh in the arrays.
824 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
825 * @return Returns true if the worm collides with the specified argh.
827 static bool worm_argh_collision(struct worm
*w
, int arghIndex
)
831 retVal
= worm_in_rect(w
, arghx
[arghIndex
], arghy
[arghIndex
],
832 argh_size
- 1, argh_size
- 1);
838 * Find new coordinates for the food stored in foodx[index], foody[index]
839 * that don't collide with any other food or argh
841 * Ensure that 0 <= index < MAX_FOOD.
843 static void make_food(int index
)
847 bool collisionDetected
= false;
851 /* make coordinates for a new food so that
852 the entire food lies within the FIELD */
853 x
= rb
->rand() % (FIELD_RECT_WIDTH
- food_size
);
854 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- food_size
);
855 collisionDetected
= false;
856 /* Ensure that the new food doesn't collide with any
857 existing foods or arghs.
858 If the new food hit any existing
859 argh or food a collision is detected.
862 for (i
=0; i
<MAX_FOOD
&& !collisionDetected
; i
++) {
863 collisionDetected
= CHECK_SQUARE_COLLISION(x
,y
,food_size
,foodx
[i
],foody
[i
],food_size
);
865 for (i
=0; i
<argh_count
&& !collisionDetected
; i
++) {
866 collisionDetected
= CHECK_SQUARE_COLLISION(x
,y
,food_size
,arghx
[i
],arghy
[i
],argh_size
);
869 /* use coordinates for further testing */
873 /* now test wether we accidently hit the worm with food ;) */
875 for (i
= 0; i
< worm_count
&& !collisionDetected
; i
++) {
877 collisionDetected
= worm_food_collision(&worms
[i
], index
);
881 while (collisionDetected
);
886 * Clears a food from the lcd buffer.
887 * @param int index The index of the food arrays under which
888 * the coordinates of the desired food can be found. Ensure
889 * that the value is 0 <= index <= MAX_FOOD.
891 static void clear_food(int index
)
893 /* remove the old food from the screen */
894 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
895 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
,
896 foody
[index
] + FIELD_RECT_Y
,
897 food_size
, food_size
);
898 rb
->lcd_set_drawmode(DRMODE_SOLID
);
902 * Draws a food in the lcd buffer.
903 * @param int index The index of the food arrays under which
904 * the coordinates of the desired food can be found. Ensure
905 * that the value is 0 <= index <= MAX_FOOD.
907 static void draw_food(int index
)
909 /* draw the food object */
910 #ifdef HAVE_LCD_COLOR
911 rb
->lcd_set_foreground(COLOR_FOOD
);
913 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
,
914 foody
[index
] + FIELD_RECT_Y
,
915 food_size
, food_size
);
916 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
917 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
+ 1,
918 foody
[index
] + FIELD_RECT_Y
+ 1,
919 food_size
- 2, food_size
- 2);
920 rb
->lcd_set_drawmode(DRMODE_SOLID
);
921 #ifdef HAVE_LCD_COLOR
922 rb
->lcd_set_foreground(COLOR_FG
);
927 * Find new coordinates for the argh stored in arghx[index], arghy[index]
928 * that don't collide with any other food or argh.
930 * Ensure that 0 <= index < argh_count < MAX_ARGH.
932 static void make_argh(int index
)
936 bool collisionDetected
= false;
940 /* make coordinates for a new argh so that
941 the entire food lies within the FIELD */
942 x
= rb
->rand() % (FIELD_RECT_WIDTH
- argh_size
);
943 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- argh_size
);
944 collisionDetected
= false;
945 /* Ensure that the new argh doesn't intersect with any
946 existing foods or arghs.
947 If the new argh hit any existing
948 argh or food an intersection is detected.
951 for (i
=0; i
<MAX_FOOD
&& !collisionDetected
; i
++) {
952 collisionDetected
= CHECK_SQUARE_COLLISION(x
,y
,argh_size
,foodx
[i
],foody
[i
],food_size
);
954 for (i
=0; i
<argh_count
&& !collisionDetected
; i
++) {
955 collisionDetected
= CHECK_SQUARE_COLLISION(x
,y
,argh_size
,arghx
[i
],arghy
[i
],argh_size
);
958 /* use the candidate coordinates to make a real argh */
962 /* now test wether we accidently hit the worm with argh ;) */
963 for (i
= 0; i
< worm_count
&& !collisionDetected
; i
++) {
964 collisionDetected
|= worm_argh_collision(&worms
[i
], index
);
965 collisionDetected
|= worm_argh_collision_in_moves(&worms
[i
], index
,
969 while (collisionDetected
);
974 * Draws an argh in the lcd buffer.
975 * @param int index The index of the argh arrays under which
976 * the coordinates of the desired argh can be found. Ensure
977 * that the value is 0 <= index < argh_count <= MAX_ARGH.
979 static void draw_argh(int index
)
981 /* draw the new argh */
982 #ifdef HAVE_LCD_COLOR
983 rb
->lcd_set_foreground(COLOR_ARGH
);
985 rb
->lcd_fillrect(arghx
[index
] + FIELD_RECT_X
,
986 arghy
[index
] + FIELD_RECT_Y
,
987 argh_size
, argh_size
);
988 #ifdef HAVE_LCD_COLOR
989 rb
->lcd_set_foreground(COLOR_FG
);
993 static void virtual_player(struct worm
*w
);
995 * Initialzes the specified worm with INITIAL_WORM_LENGTH
996 * and the tail at the specified position. The worm will
997 * be initialized alive and creeping EAST.
998 * @param struct worm *w The worm that is to be initialized
999 * @param int x The x coordinate at which the tail of the worm starts.
1000 * x must be 0 <= x < FIELD_RECT_WIDTH.
1001 * @param int y The y coordinate at which the tail of the worm starts
1002 * y must be 0 <= y < FIELD_RECT_WIDTH.
1004 static void init_worm(struct worm
*w
, int x
, int y
)
1006 /* initialize the worm size */
1010 w
->x
[w
->head
] = x
+ 1;
1016 /* set the initial direction the worm creeps to */
1020 w
->growing
= INITIAL_WORM_LENGTH
- 1;
1022 w
->fetch_worm_direction
= virtual_player
;
1026 * Writes the direction that was stored for
1027 * human player 1 into the specified worm. This function
1028 * may be used to be stored in worm.fetch_worm_direction.
1030 * the direction is read from player1_dir.
1031 * @param struct worm *w - The worm of which the direction
1034 static void human_player1(struct worm
*w
) {
1035 set_worm_dir(w
, player1_dir
);
1039 * Writes the direction that was stored for
1040 * human player 2 into the specified worm. This function
1041 * may be used to be stored in worm.fetch_worm_direction.
1043 * the direction is read from player2_dir.
1044 * @param struct worm *w - The worm of which the direction
1047 static void human_player2(struct worm
*w
) {
1048 set_worm_dir(w
, player2_dir
);
1052 * Writes the direction that was stored for
1053 * human player using a remote control
1054 * into the specified worm. This function
1055 * may be used to be stored in worm.fetch_worm_direction.
1057 * the direction is read from player3_dir.
1058 * @param struct worm *w - The worm of which the direction
1061 static void remote_player(struct worm
*w
) {
1062 set_worm_dir(w
, player3_dir
);
1066 * Initializes the worm-, food- and argh-arrays, draws a frame,
1067 * makes some food and argh and display all that stuff.
1069 static void init_wormlet(void)
1073 for (i
= 0; i
< worm_count
; i
++) {
1074 /* Initialize all the worm coordinates to center. */
1075 int x
= (int)(FIELD_RECT_WIDTH
/ 2);
1076 int y
= (int)((FIELD_RECT_HEIGHT
- 20)/ 2) + i
* 10;
1078 init_worm(&worms
[i
], x
, y
);
1086 worms
[0].fetch_worm_direction
= human_player1
;
1091 worms
[1].fetch_worm_direction
= remote_player
;
1093 worms
[1].fetch_worm_direction
= human_player2
;
1098 worms
[2].fetch_worm_direction
= human_player2
;
1101 /* Needed when the game is restarted using BTN_STOPRESET */
1102 rb
->lcd_clear_display();
1104 /* make and display some food and argh */
1105 argh_count
= MAX_FOOD
;
1106 for (i
= 0; i
< MAX_FOOD
; i
++) {
1113 /* draw the game field */
1114 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
1115 rb
->lcd_fillrect(0, 0, FIELD_RECT_WIDTH
+ 2, FIELD_RECT_HEIGHT
+ 2);
1116 rb
->lcd_fillrect(1, 1, FIELD_RECT_WIDTH
, FIELD_RECT_HEIGHT
);
1117 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1119 /* make everything visible */
1125 * Move the worm one step further if it is alive.
1126 * The direction in which the worm moves is taken from dirx and diry.
1127 * move_worm decreases growing if > 0. While the worm is growing the tail
1128 * is left untouched.
1129 * @param struct worm *w The worm to move. w must not be NULL.
1131 static void move_worm(struct worm
*w
)
1134 /* determine the head point and its precessor */
1135 int headx
= w
->x
[w
->head
];
1136 int heady
= w
->y
[w
->head
];
1137 int prehead
= (w
->head
+ MAX_WORM_SEGMENTS
- 1) % MAX_WORM_SEGMENTS
;
1138 int preheadx
= w
->x
[prehead
];
1139 int preheady
= w
->y
[prehead
];
1141 /* determine the old direction */
1144 if (headx
== preheadx
) {
1146 olddiry
= (heady
> preheady
) ? 1 : -1;
1149 olddirx
= (headx
> preheadx
) ? 1 : -1;
1153 a change of direction means a new segment
1155 if (olddirx
!= w
->dirx
||
1156 olddiry
!= w
->diry
) {
1157 w
->head
= (w
->head
+ 1) % MAX_WORM_SEGMENTS
;
1160 /* new head position */
1161 w
->x
[w
->head
] = headx
+ w
->dirx
;
1162 w
->y
[w
->head
] = heady
+ w
->diry
;
1165 /* while the worm is growing no tail procession is necessary */
1166 if (w
->growing
> 0) {
1167 /* update the worms grow state */
1171 /* if the worm isn't growing the tail has to be dragged */
1173 /* index of the end of the tail segment */
1174 int tail_segment_end
= (w
->tail
+ 1) % MAX_WORM_SEGMENTS
;
1176 /* drag the end of the tail */
1177 /* only one coordinate has to be altered. Here it is
1178 determined which one */
1179 int dir
= 0; /* specifies wether the coord has to be in- or decreased */
1180 if (w
->x
[w
->tail
] == w
->x
[tail_segment_end
]) {
1181 dir
= (w
->y
[w
->tail
] - w
->y
[tail_segment_end
] < 0) ? 1 : -1;
1182 w
->y
[w
->tail
] += dir
;
1184 dir
= (w
->x
[w
->tail
] - w
->x
[tail_segment_end
] < 0) ? 1 : -1;
1185 w
->x
[w
->tail
] += dir
;
1188 /* when the tail has been dragged so far that it meets
1189 the next segment start the tail segment is obsolete and
1191 if (w
->x
[w
->tail
] == w
->x
[tail_segment_end
] &&
1192 w
->y
[w
->tail
] == w
->y
[tail_segment_end
]){
1194 /* drop the last tail point */
1195 w
->tail
= tail_segment_end
;
1202 * Draws the head and clears the tail of the worm in
1203 * the display buffer. lcd_update() is NOT called thus
1204 * the caller has to take care that the buffer is displayed.
1206 static void draw_worm(struct worm
*w
)
1208 /* draw the new head */
1209 int x
= w
->x
[w
->head
];
1210 int y
= w
->y
[w
->head
];
1211 #ifdef HAVE_LCD_COLOR
1212 rb
->lcd_set_foreground(COLOR_WORM
);
1214 if (x
>= 0 && x
< FIELD_RECT_WIDTH
&& y
>= 0 && y
< FIELD_RECT_HEIGHT
) {
1215 rb
->lcd_drawpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
1218 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1220 /* clear the space behind the worm */
1223 if (x
>= 0 && x
< FIELD_RECT_WIDTH
&& y
>= 0 && y
< FIELD_RECT_HEIGHT
) {
1224 rb
->lcd_drawpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
1226 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1227 #ifdef HAVE_LCD_COLOR
1228 rb
->lcd_set_foreground(COLOR_FG
);
1233 * Checks wether the coordinate is part of the worm. Returns
1234 * true if any part of the worm was hit - including the head.
1235 * @param x int The x coordinate
1236 * @param y int The y coordinate
1237 * @return int The index of the worm arrays that contain x, y.
1238 * Returns -1 if the coordinates are not part of the worm.
1240 static int specific_worm_collision(struct worm
*w
, int x
, int y
)
1244 /* get_worm_array_length is expensive -> buffer the value */
1245 int wormLength
= get_worm_array_length(w
);
1248 /* test each entry that is part of the worm */
1249 for (i
= 0; i
< wormLength
&& retVal
== -1; i
++) {
1251 /* The iteration iterates the length of the worm.
1252 Here's the conversion to the true indices within the worm arrays. */
1253 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
1254 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
1255 bool samex
= (w
->x
[linestart
] == x
) && (w
->x
[lineend
] == x
);
1256 bool samey
= (w
->y
[linestart
] == y
) && (w
->y
[lineend
] == y
);
1257 if (samex
|| samey
){
1258 int test
, min
, max
, tmp
;
1261 min
= w
->x
[linestart
];
1262 max
= w
->x
[lineend
];
1265 min
= w
->y
[linestart
];
1266 max
= w
->y
[lineend
];
1271 min
= MIN(min
, max
);
1272 max
= MAX(tmp
, max
);
1274 if (min
<= test
&& test
<= max
) {
1283 * Increases the length of the specified worm by marking
1284 * that it may grow by len pixels. Note that the worm has
1285 * to move to make the growing happen.
1286 * @param worm *w The worm that is to be altered.
1287 * @param int len A positive value specifying the amount of
1288 * pixels the worm may grow.
1290 static void add_growing(struct worm
*w
, int len
) {
1295 * Determins the worm that is at the coordinates x, y. The parameter
1296 * w is a switch parameter that changes the functionality of worm_collision.
1297 * If w is specified and x,y hits the head of w NULL is returned.
1298 * This is a useful way to determine wether the head of w hits
1299 * any worm but including itself but excluding its own head.
1300 * (It hits always its own head ;))
1301 * If w is set to NULL worm_collision returns any worm including all heads
1302 * that is at position of x,y.
1303 * @param struct worm *w The worm of which the head should be excluded in
1304 * the test. w may be set to NULL.
1305 * @param int x The x coordinate that is checked
1306 * @param int y The y coordinate that is checkec
1307 * @return struct worm* The worm that has been hit by x,y. If no worm
1308 * was at the position NULL is returned.
1310 static struct worm
* worm_collision(struct worm
*w
, int x
, int y
)
1312 struct worm
*retVal
= NULL
;
1314 for (i
= 0; (i
< worm_count
) && (retVal
== NULL
); i
++) {
1315 int collision_at
= specific_worm_collision(&worms
[i
], x
, y
);
1316 if (collision_at
!= -1) {
1317 if (!(w
== &worms
[i
] && collision_at
== w
->head
)){
1326 * Returns true if the head of the worm just has
1327 * crossed the field boundaries.
1328 * @return bool true if the worm just has wrapped.
1330 static bool field_collision(struct worm
*w
)
1332 bool retVal
= false;
1333 if ((w
->x
[w
->head
] >= FIELD_RECT_WIDTH
) ||
1334 (w
->y
[w
->head
] >= FIELD_RECT_HEIGHT
) ||
1335 (w
->x
[w
->head
] < 0) ||
1336 (w
->y
[w
->head
] < 0))
1345 * Returns true if the specified coordinates are within the
1346 * field specified by the FIELD_RECT_XXX constants.
1347 * @param int x The x coordinate of the point that is investigated
1348 * @param int y The y coordinate of the point that is investigated
1349 * @return bool Returns false if x,y specifies a point outside the
1352 static bool is_in_field_rect(int x
, int y
)
1354 bool retVal
= false;
1355 retVal
= (x
>= 0 && x
< FIELD_RECT_WIDTH
&&
1356 y
>= 0 && y
< FIELD_RECT_HEIGHT
);
1361 * Checks and returns wether the head of the w
1362 * is colliding with something currently.
1363 * @return int One of the values:
1370 static int check_collision(struct worm
*w
)
1372 int retVal
= COLLISION_NONE
;
1374 if (worm_collision(w
, w
->x
[w
->head
], w
->y
[w
->head
]) != NULL
)
1375 retVal
= COLLISION_WORM
;
1377 if (food_collision(w
->x
[w
->head
], w
->y
[w
->head
]) >= 0)
1378 retVal
= COLLISION_FOOD
;
1380 if (argh_collision(w
->x
[w
->head
], w
->y
[w
->head
]) >= 0)
1381 retVal
= COLLISION_ARGH
;
1383 if (field_collision(w
))
1384 retVal
= COLLISION_FIELD
;
1390 * Returns the index of the food that is closest to the point
1391 * specified by x, y. This index may be used in the foodx and
1393 * @param int x The x coordinate of the point
1394 * @param int y The y coordinate of the point
1395 * @return int A value usable as index in foodx and foody.
1397 static int get_nearest_food(int x
, int y
)
1399 int nearestfood
= 0;
1400 int olddistance
= FIELD_RECT_WIDTH
+ FIELD_RECT_HEIGHT
;
1404 for (foodindex
= 0; foodindex
< MAX_FOOD
; foodindex
++) {
1406 deltax
= foodx
[foodindex
] - x
;
1407 deltay
= foody
[foodindex
] - y
;
1408 deltax
= deltax
> 0 ? deltax
: deltax
* (-1);
1409 deltay
= deltay
> 0 ? deltay
: deltay
* (-1);
1410 distance
= deltax
+ deltay
;
1412 if (distance
< olddistance
) {
1413 olddistance
= distance
;
1414 nearestfood
= foodindex
;
1421 * Returns wether the specified position is next to the worm
1422 * and in the direction the worm looks. Use this method to
1423 * test wether this position would be hit with the next move of
1424 * the worm unless the worm changes its direction.
1425 * @param struct worm *w - The worm to be investigated
1426 * @param int x - The x coordinate of the position to test.
1427 * @param int y - The y coordinate of the position to test.
1428 * @return Returns true if the worm will hit the position unless
1429 * it change its direction before the next move.
1431 static bool is_in_front_of_worm(struct worm
*w
, int x
, int y
)
1433 bool infront
= false;
1434 int deltax
= x
- w
->x
[w
->head
];
1435 int deltay
= y
- w
->y
[w
->head
];
1438 infront
= (w
->diry
* deltay
) > 0;
1440 infront
= (w
->dirx
* deltax
) > 0;
1446 * Returns true if the worm will collide with the next move unless
1447 * it changes its direction.
1448 * @param struct worm *w - The worm to be investigated.
1449 * @return Returns true if the worm will collide with the next move
1450 * unless it changes its direction.
1452 static bool will_worm_collide(struct worm
*w
)
1454 int x
= w
->x
[w
->head
] + w
->dirx
;
1455 int y
= w
->y
[w
->head
] + w
->diry
;
1456 bool retVal
= !is_in_field_rect(x
, y
);
1458 retVal
= (argh_collision(x
, y
) != -1);
1462 retVal
= (worm_collision(w
, x
, y
) != NULL
);
1469 * may be used to be stored in worm.fetch_worm_direction for
1470 * worms that are not controlled by humans but by artificial stupidity.
1471 * A direction is searched that doesn't lead to collision but to the nearest
1472 * food - but not very intelligent. The direction is written to the specified
1474 * @param struct worm *w - The worm of which the direction
1477 static void virtual_player(struct worm
*w
)
1480 int plana
, planb
, planc
;
1481 /* find the next lunch */
1482 int nearestfood
= get_nearest_food(w
->x
[w
->head
], w
->y
[w
->head
]);
1484 /* determine in which direction it is */
1486 /* in front of me? */
1487 bool infront
= is_in_front_of_worm(w
, foodx
[nearestfood
], foody
[nearestfood
]);
1489 /* left right of me? */
1490 int olddir
= get_worm_dir(w
);
1491 set_worm_dir(w
, (olddir
+ 1) % 4);
1492 isright
= is_in_front_of_worm(w
, foodx
[nearestfood
], foody
[nearestfood
]);
1493 set_worm_dir(w
, olddir
);
1495 /* detect situation, set strategy */
1499 planb
= (olddir
+ 1) % 4;
1500 planc
= (olddir
+ 3) % 4;
1503 planb
= (olddir
+ 3) % 4;
1504 planc
= (olddir
+ 1) % 4;
1508 plana
= (olddir
+ 1) % 4;
1510 planc
= (olddir
+ 3) % 4;
1512 plana
= (olddir
+ 3) % 4;
1514 planc
= (olddir
+ 1) % 4;
1518 /* test for collision */
1519 set_worm_dir(w
, plana
);
1520 if (will_worm_collide(w
)){
1523 set_worm_dir(w
, planb
);
1525 /* test for collision */
1526 if (will_worm_collide(w
)) {
1529 set_worm_dir(w
, planc
);
1535 * prints out the score board with all the status information
1538 static void score_board(void)
1544 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1545 rb
->lcd_fillrect(FIELD_RECT_WIDTH
+ 2, 0,
1546 LCD_WIDTH
- FIELD_RECT_WIDTH
- 2, LCD_HEIGHT
);
1547 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1548 for (i
= 0; i
< worm_count
; i
++) {
1549 int score
= get_score(&worms
[i
]);
1552 if (worms
[i
].fetch_worm_direction
!= virtual_player
){
1553 if (highscore
< score
) {
1559 rb
->snprintf(buf
, sizeof (buf
),"Len:%d", score
);
1562 switch (check_collision(&worms
[i
])) {
1563 case COLLISION_NONE
:
1564 if (worms
[i
].growing
> 0)
1574 case COLLISION_WORM
:
1578 case COLLISION_FOOD
:
1582 case COLLISION_ARGH
:
1586 case COLLISION_FIELD
:
1590 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, y
, buf
);
1591 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, y
+8, buf2
);
1593 if (!worms
[i
].alive
){
1594 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
1595 rb
->lcd_fillrect(FIELD_RECT_WIDTH
+ 2, y
,
1596 LCD_WIDTH
- FIELD_RECT_WIDTH
- 2, 17);
1597 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1601 rb
->snprintf(buf
, sizeof(buf
), "Hs: %d", highscore
);
1602 #ifndef DEBUG_WORMLET
1603 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, LCD_HEIGHT
- 8, buf
);
1605 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, LCD_HEIGHT
- 8, debugout
);
1610 * Checks for collisions of the worm and its environment and
1611 * takes appropriate actions like growing the worm or killing it.
1612 * @return bool Returns true if the worm is dead. Returns
1613 * false if the worm is healthy, up and creeping.
1615 static bool process_collisions(struct worm
*w
)
1619 w
->alive
&= !field_collision(w
);
1623 /* check if food was eaten */
1624 index
= food_collision(w
->x
[w
->head
], w
->y
[w
->head
]);
1632 for (i
= 0; i
< arghs_per_food
; i
++) {
1634 if (argh_count
> MAX_ARGH
)
1635 argh_count
= MAX_ARGH
;
1636 make_argh(argh_count
- 1);
1637 draw_argh(argh_count
- 1);
1640 add_growing(w
, worm_food
);
1645 /* check if argh was eaten */
1647 index
= argh_collision(w
->x
[w
->head
], w
->y
[w
->head
]);
1652 if (worm_collision(w
, w
->x
[w
->head
], w
->y
[w
->head
]) != NULL
) {
1662 * The main loop of the game.
1663 * @return bool Returns true if the game ended
1664 * with a dead worm. Returns false if the user
1665 * aborted the game manually.
1667 static int run(void)
1670 int wormDead
= false;
1671 bool paused
= false;
1673 /* ticks are counted to compensate speed variations */
1674 long cycle_start
= 0, cycle_end
= 0;
1675 #ifdef DEBUG_WORMLET
1676 int ticks_to_max_cycle_reset
= 20;
1681 /* initialize the board and so on */
1684 cycle_start
= *rb
->current_tick
;
1685 /* change the direction of the worm */
1689 long cycle_duration
=0;
1691 #ifdef HAS_BUTTON_HOLD
1692 if (rb
->button_hold())
1697 case BTN_STARTPAUSE
:
1702 return 1; /* restart game */
1710 return 2; /* back to menu */
1717 if (players
== 1 && !use_remote
) {
1718 player1_dir
= NORTH
;
1723 if (players
== 1 && !use_remote
) {
1724 player1_dir
= SOUTH
;
1729 if (players
!= 1 || use_remote
) {
1730 player1_dir
= (player1_dir
+ 3) % 4;
1737 if (players
!= 1 || use_remote
) {
1738 player1_dir
= (player1_dir
+ 1) % 4;
1745 case BTN_PLAYER2_DIR1
:
1746 player2_dir
= (player2_dir
+ 3) % 4;
1749 case BTN_PLAYER2_DIR2
:
1750 player2_dir
= (player2_dir
+ 1) % 4;
1756 player3_dir
= (player3_dir
+ 1) % 4;
1760 player3_dir
= (player3_dir
+ 3) % 4;
1766 for (i
= 0; i
< worm_count
; i
++) {
1767 worms
[i
].fetch_worm_direction(&worms
[i
]);
1771 for (i
= 0; i
< worm_count
; i
++){
1772 struct worm
*w
= &worms
[i
];
1774 wormDead
&= process_collisions(w
);
1779 if (button
== BTN_STOPRESET
) {
1783 /* here the wormlet game cycle ends
1784 thus the current tick is stored
1786 cycle_end
= *rb
->current_tick
;
1788 /* The duration of the game cycle */
1789 cycle_duration
= cycle_end
- cycle_start
;
1790 cycle_duration
= MAX(0, cycle_duration
);
1791 cycle_duration
= MIN(speed
-1, cycle_duration
);
1794 #ifdef DEBUG_WORMLET
1795 ticks_to_max_cycle_reset
--;
1796 if (ticks_to_max_cycle_reset
<= 0) {
1800 if (max_cycle
< cycle_duration
) {
1801 max_cycle
= cycle_duration
;
1802 ticks_to_max_cycle_reset
= 20;
1804 rb
->snprintf(buf
, sizeof buf
, "ticks %d", max_cycle
);
1808 /* adjust the number of ticks to wait for a button.
1809 This ensures that a complete game cycle including
1810 user input runs in constant time */
1811 button
= rb
->button_get_w_tmo(speed
- cycle_duration
);
1812 cycle_start
= *rb
->current_tick
;
1815 rb
->splash(HZ
*2, "Game Over!");
1817 return 2; /* back to menu */
1820 #ifdef DEBUG_WORMLET
1823 * Just a test routine that checks that worm_food_collision works
1824 * in some typical situations.
1826 static void test_worm_food_collision(void)
1828 int collision_count
= 0;
1830 rb
->lcd_clear_display();
1831 init_worm(&worms
[0], 10, 10);
1832 add_growing(&worms
[0], 10);
1833 set_worm_dir(&worms
[0], EAST
);
1834 for (i
= 0; i
< 10; i
++) {
1835 move_worm(&worms
[0]);
1836 draw_worm(&worms
[0]);
1839 set_worm_dir(&worms
[0], SOUTH
);
1840 for (i
= 0; i
< 10; i
++) {
1841 move_worm(&worms
[0]);
1842 draw_worm(&worms
[0]);
1847 for (foody
[0] = 20; foody
[0] > 0; foody
[0] --) {
1850 draw_worm(&worms
[0]);
1852 collision
= worm_food_collision(&worms
[0], 0);
1856 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1857 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1860 if (collision_count
!= food_size
) {
1861 rb
->button_get(true);
1866 for (foodx
[0] = 30; foodx
[0] > 0; foodx
[0] --) {
1869 draw_worm(&worms
[0]);
1871 collision
= worm_food_collision(&worms
[0], 0);
1875 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1876 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1879 if (collision_count
!= food_size
* 2) {
1880 rb
->button_get(true);
1885 static bool expensive_worm_in_rect(struct worm
*w
, int rx
, int ry
, int rw
, int rh
)
1888 bool retVal
= false;
1889 for (x
= rx
; x
< rx
+ rw
; x
++){
1890 for (y
= ry
; y
< ry
+ rh
; y
++) {
1891 if (specific_worm_collision(w
, x
, y
) != -1) {
1899 static void test_worm_argh_collision(void)
1903 int collision_count
= 0;
1904 rb
->lcd_clear_display();
1905 init_worm(&worms
[0], 10, 10);
1906 add_growing(&worms
[0], 40);
1907 for (dir
= 0; dir
< 4; dir
++) {
1908 set_worm_dir(&worms
[0], (EAST
+ dir
) % 4);
1909 for (i
= 0; i
< 10; i
++) {
1910 move_worm(&worms
[0]);
1911 draw_worm(&worms
[0]);
1916 for (arghy
[0] = 0; arghy
[0] < FIELD_RECT_HEIGHT
- argh_size
; arghy
[0]++){
1920 collision
= worm_argh_collision(&worms
[0], 0);
1924 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1925 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1928 if (collision_count
!= argh_size
* 2) {
1929 rb
->button_get(true);
1933 for (arghx
[0] = 0; arghx
[0] < FIELD_RECT_HEIGHT
- argh_size
; arghx
[0]++){
1937 collision
= worm_argh_collision(&worms
[0], 0);
1941 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1942 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1945 if (collision_count
!= argh_size
* 4) {
1946 rb
->button_get(true);
1950 static int testline_in_rect(void)
1952 int testfailed
= -1;
1965 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1966 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1967 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1968 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1970 rb
->lcd_putsxy(0, 0, "failed 1");
1971 rb
->button_get(true);
1977 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1978 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1979 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1980 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1981 rb
->lcd_putsxy(0, 0, "failed 2");
1983 rb
->button_get(true);
1989 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1990 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1991 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1992 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1993 rb
->lcd_putsxy(0, 0, "failed 3");
1995 rb
->button_get(true);
2001 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2002 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2003 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2004 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2005 rb
->lcd_putsxy(0, 0, "failed 4");
2007 rb
->button_get(true);
2013 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2014 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2015 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2016 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2017 rb
->lcd_putsxy(0, 0, "failed 5");
2019 rb
->button_get(true);
2026 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2027 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2028 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2029 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2030 rb
->lcd_putsxy(0, 0, "failed 6");
2032 rb
->button_get(true);
2041 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2042 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2043 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2044 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2045 rb
->lcd_putsxy(0, 0, "failed 7");
2047 rb
->button_get(true);
2053 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2054 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2055 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2056 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2057 rb
->lcd_putsxy(0, 0, "failed 8");
2059 rb
->button_get(true);
2065 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2066 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2067 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2068 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2069 rb
->lcd_putsxy(0, 0, "failed 9");
2071 rb
->button_get(true);
2077 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2078 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2079 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2080 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2081 rb
->lcd_putsxy(0, 0, "failed 10");
2083 rb
->button_get(true);
2089 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2090 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2091 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2092 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2093 rb
->lcd_putsxy(0, 0, "failed 11");
2095 rb
->button_get(true);
2102 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2103 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2104 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2105 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2106 rb
->lcd_putsxy(0, 0, "failed 12");
2108 rb
->button_get(true);
2122 if (!(line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2123 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
))) {
2124 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2125 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2126 rb
->lcd_putsxy(0, 0, "failed 13");
2128 rb
->button_get(true);
2142 if (!(line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2143 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
))) {
2144 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2145 rb
->lcd_invertrect(rx
, ry
, rw
, rh
);
2146 rb
->lcd_putsxy(0, 0, "failed 14");
2148 rb
->button_get(true);
2152 rb
->lcd_clear_display();
2158 * Just a test routine to test wether specific_worm_collision might work properly
2160 static int test_specific_worm_collision(void)
2167 rb
->lcd_clear_display();
2168 init_worm(&worms
[0], 10, 20);
2169 add_growing(&worms
[0], 20 - INITIAL_WORM_LENGTH
);
2171 for (dir
= EAST
; dir
< EAST
+ 4; dir
++) {
2173 set_worm_dir(&worms
[0], dir
% 4);
2174 for (i
= 0; i
< 5; i
++) {
2175 if (!(dir
% 4 == NORTH
&& i
== 9)) {
2176 move_worm(&worms
[0]);
2177 draw_worm(&worms
[0]);
2182 for (y
= 15; y
< 30; y
++){
2183 for (x
= 5; x
< 20; x
++) {
2184 if (specific_worm_collision(&worms
[0], x
, y
) != -1) {
2187 rb
->lcd_invertpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
2188 rb
->snprintf(buf
, sizeof buf
, "collisions %d", collisions
);
2189 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2193 if (collisions
!= 21) {
2194 rb
->button_get(true);
2199 static void test_make_argh(void)
2205 int last_failures
= 0;
2207 rb
->lcd_clear_display();
2210 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++) {
2211 init_worm(&worms
[worm_idx
], 10 + worm_idx
* 20, 20);
2212 add_growing(&worms
[worm_idx
], 40 - INITIAL_WORM_LENGTH
);
2215 for (dir
= EAST
; dir
< EAST
+ 4; dir
++) {
2216 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++) {
2217 set_worm_dir(&worms
[worm_idx
], dir
% 4);
2218 for (i
= 0; i
< 10; i
++) {
2219 if (!(dir
% 4 == NORTH
&& i
== 9)) {
2220 move_worm(&worms
[worm_idx
]);
2221 draw_worm(&worms
[worm_idx
]);
2229 for (seed
= 0; hit
< 20; seed
+= 2) {
2233 x
= rb
->rand() % (FIELD_RECT_WIDTH
- argh_size
);
2234 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- argh_size
);
2236 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++){
2237 if (expensive_worm_in_rect(&worms
[worm_idx
], x
, y
, argh_size
, argh_size
)) {
2241 tries
= make_argh(0);
2242 if ((x
== arghx
[0] && y
== arghy
[0]) || tries
< 2) {
2246 rb
->snprintf(buf
, sizeof buf
, "(%d;%d) fail%d try%d",
2247 x
, y
, failures
, tries
);
2248 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2250 rb
->lcd_invertrect(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
,
2251 argh_size
, argh_size
);
2255 rb
->lcd_invertrect(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
,
2256 argh_size
, argh_size
);
2257 rb
->lcd_clearrect(arghx
[0] + FIELD_RECT_X
, arghy
[0] + FIELD_RECT_Y
,
2258 argh_size
, argh_size
);
2260 if (failures
> last_failures
) {
2261 rb
->button_get(true);
2263 last_failures
= failures
;
2270 static void test_worm_argh_collision_in_moves(void) {
2273 rb
->lcd_clear_display();
2274 init_worm(&worms
[0], 10, 20);
2280 set_worm_dir(&worms
[0], EAST
);
2281 for (i
= 0; i
< 20; i
++) {
2283 move_worm(&worms
[0]);
2284 draw_worm(&worms
[0]);
2285 if (worm_argh_collision_in_moves(&worms
[0], 0, 5)){
2288 rb
->snprintf(buf
, sizeof buf
, "in 5 moves hits: %d", hit_count
);
2289 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2292 if (hit_count
!= argh_size
+ 5) {
2293 rb
->button_get(true);
2296 #endif /* DEBUG_WORMLET */
2299 * Reverts default settings
2301 static void default_settings(void)
2303 arghs_per_food
= ARGHS_PER_FOOD
;
2304 argh_size
= ARGH_SIZE
;
2305 food_size
= FOOD_SIZE
;
2307 worm_food
= WORM_PER_FOOD
;
2309 worm_count
= MAX_WORMS
;
2315 * Launches the wormlet game
2317 static bool launch_wormlet(void)
2319 int game_result
= 1;
2321 rb
->lcd_clear_display();
2323 /* Turn off backlight timeout */
2324 backlight_force_on(); /* backlight control in lib/helper.c */
2326 /* start the game */
2327 while (game_result
== 1)
2328 game_result
= run();
2330 switch (game_result
)
2333 /* Turn on backlight timeout (revert to settings) */
2334 backlight_use_settings(); /* backlight control in lib/helper.c */
2344 enum plugin_status
plugin_start(const void* parameter
)
2353 if (configfile_load(SETTINGS_FILENAME
, config
,
2354 sizeof(config
)/sizeof(*config
),
2355 SETTINGS_MIN_VERSION
) < 0)
2357 /* If the loading failed, save a new config file (as the disk is
2358 already spinning) */
2359 configfile_save(SETTINGS_FILENAME
, config
,
2360 sizeof(config
)/sizeof(*config
),
2364 #ifdef HAVE_LCD_COLOR
2365 rb
->lcd_set_foreground(COLOR_FG
);
2366 rb
->lcd_set_background(COLOR_BG
);
2370 rb
->lcd_set_backdrop(NULL
);
2373 #ifdef DEBUG_WORMLET
2375 test_worm_argh_collision_in_moves();
2377 test_worm_food_collision();
2378 test_worm_argh_collision();
2379 test_specific_worm_collision();
2384 static const struct opt_items noyes
[2] = {
2389 static const struct opt_items remoteonly_option
[1] = {
2390 { "Remote Control", -1 }
2393 static const struct opt_items key24_option
[2] = {
2394 { "4 Key Control", -1 },
2395 { "2 Key Control", -1 }
2399 static const struct opt_items remote_option
[2] = {
2400 { "Remote Control", -1 },
2401 { "No Rem. Control", -1 }
2404 static const struct opt_items key2_option
[1] = {
2405 { "2 Key Control", -1 }
2409 static const struct opt_items nokey_option
[1] = {
2410 { "Out of Control", -1 }
2413 MENUITEM_STRINGLIST(menu
, "Wormlet Menu", NULL
, "Play Wormlet!",
2414 "Number of Worms", "Number of Players", "Control Style",
2415 "Worm Growth Per Food","Worm Speed","Arghs Per Food",
2416 "Argh Size","Food Size","Revert to Default Settings",
2417 "Playback Control", "Quit");
2419 rb
->button_clear_queue();
2421 while (!menu_quit
) {
2422 switch(rb
->do_menu(&menu
, &result
, NULL
, false))
2425 rb
->lcd_setfont(FONT_SYSFIXED
);
2429 rb
->set_int("Number of Worms", "", UNIT_INT
, &worm_count
, NULL
,
2431 if (worm_count
< players
) {
2432 worm_count
= players
;
2437 rb
->set_int("Number of Players", "", UNIT_INT
, &players
, NULL
,
2440 rb
->set_int("Number of Players", "", UNIT_INT
, &players
, NULL
,
2443 if (players
> worm_count
) {
2444 worm_count
= players
;
2453 rb
->set_option("Control Style",&use_remote
,INT
,
2454 nokey_option
, 1, NULL
);
2457 rb
->set_option("Control Style",&use_remote
,INT
,
2458 key24_option
, 2, NULL
);
2462 rb
->set_option("Control Style",&use_remote
,INT
,
2463 remote_option
, 2, NULL
);
2465 rb
->set_option("Control Style",&use_remote
,INT
,
2466 key2_option
, 1, NULL
);
2470 rb
->set_option("Control Style",&use_remote
,INT
,
2471 remoteonly_option
, 1, NULL
);
2476 rb
->set_int("Worm Growth Per Food", "", UNIT_INT
, &worm_food
,
2477 NULL
, 1, 0, 15, NULL
);
2480 new_setting
= 20 - speed
;
2481 rb
->set_int("Worm Speed", "", UNIT_INT
, &new_setting
,
2482 NULL
, 1, 0, 20, NULL
);
2483 speed
= 20 - new_setting
;
2486 rb
->set_int("Arghs Per Food", "", UNIT_INT
, &arghs_per_food
,
2487 NULL
, 1, 0, 8, NULL
);
2490 rb
->set_int("Argh Size", "", UNIT_INT
, &argh_size
,
2491 NULL
, 1, 2, 10, NULL
);
2494 rb
->set_int("Food Size", "", UNIT_INT
, &food_size
,
2495 NULL
, 1, 2, 10, NULL
);
2499 rb
->set_option("Reset Settings?", &new_setting
, INT
, noyes
, 2, NULL
);
2500 if (new_setting
== 1)
2504 playback_control(NULL
);
2512 configfile_save(SETTINGS_FILENAME
, config
,
2513 sizeof(config
)/sizeof(*config
),