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 "configfile.h"
27 /* size of the field the worm lives in */
28 #define FIELD_RECT_X 1
29 #define FIELD_RECT_Y 1
30 #define FIELD_RECT_WIDTH (LCD_WIDTH - 45)
31 #define FIELD_RECT_HEIGHT (LCD_HEIGHT - 2)
33 /* when the game starts */
34 #define INITIAL_WORM_LENGTH 10
36 /* num of pixel the worm grows per eaten food */
37 #define WORM_PER_FOOD 7
39 /* num of worms creeping in the FIELD */
42 /* minimal distance between a worm and an argh
43 when a new argh is made */
44 #define MIN_ARGH_DIST 5
46 #if (CONFIG_KEYPAD == RECORDER_PAD)
47 #define BTN_DIR_UP BUTTON_UP
48 #define BTN_DIR_DOWN BUTTON_DOWN
49 #define BTN_DIR_LEFT BUTTON_LEFT
50 #define BTN_DIR_RIGHT BUTTON_RIGHT
51 #define BTN_PLAYER2_DIR1 BUTTON_F2
52 #define BTN_PLAYER2_DIR2 BUTTON_F3
53 #define BTN_STARTPAUSE BUTTON_PLAY
54 #define BTN_QUIT BUTTON_OFF
55 #define BTN_STOPRESET BUTTON_ON
56 #define BTN_TOGGLE_KEYS BUTTON_F1
58 #if BUTTON_REMOTE != 0
59 #define BTN_RC_UP BUTTON_RC_VOL_UP
60 #define BTN_RC_DOWN BUTTON_RC_VOL_DOWN
65 #define PLAYERS_TEXT "UP/DN"
66 #define WORMS_TEXT "L/R"
67 #define KEY_CONTROL_TEXT "F1"
69 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
70 #define BTN_DIR_UP BUTTON_UP
71 #define BTN_DIR_DOWN BUTTON_DOWN
72 #define BTN_DIR_LEFT BUTTON_LEFT
73 #define BTN_DIR_RIGHT BUTTON_RIGHT
74 #define BTN_PLAYER2_DIR1 BUTTON_F2
75 #define BTN_PLAYER2_DIR2 BUTTON_F3
76 #define BTN_STARTPAUSE BUTTON_SELECT
77 #define BTN_QUIT BUTTON_OFF
78 #define BTN_STOPRESET BUTTON_ON
79 #define BTN_TOGGLE_KEYS BUTTON_F1
81 #define PLAYERS_TEXT "UP/DN"
82 #define WORMS_TEXT "L/R"
83 #define KEY_CONTROL_TEXT "F1"
85 #elif (CONFIG_KEYPAD == ONDIO_PAD)
86 #define BTN_DIR_UP BUTTON_UP
87 #define BTN_DIR_DOWN BUTTON_DOWN
88 #define BTN_DIR_LEFT BUTTON_LEFT
89 #define BTN_DIR_RIGHT BUTTON_RIGHT
90 #define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL)
91 #define BTN_QUIT (BUTTON_OFF|BUTTON_REL)
92 #define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU)
94 #define PLAYERS_TEXT "UP/DN"
95 #define WORMS_TEXT "L/R"
97 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
98 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
100 #define BTN_DIR_UP BUTTON_MENU
101 #define BTN_DIR_DOWN BUTTON_PLAY
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_SELECT|BUTTON_MENU)
106 #define BTN_STOPRESET (BUTTON_SELECT|BUTTON_PLAY)
108 #define PLAYERS_TEXT "Menu/Play"
109 #define WORMS_TEXT "Left/Right"
111 #elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
113 #define BTN_DIR_UP BUTTON_UP
114 #define BTN_DIR_DOWN BUTTON_DOWN
115 #define BTN_DIR_LEFT BUTTON_LEFT
116 #define BTN_DIR_RIGHT BUTTON_RIGHT
117 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
118 #define BTN_QUIT BUTTON_OFF
119 #define BTN_STOPRESET BUTTON_ON
121 #define BTN_RC_QUIT BUTTON_RC_STOP
123 #define PLAYERS_TEXT "Up/Down"
124 #define WORMS_TEXT "Left/Right"
126 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
128 #define BTN_DIR_UP BUTTON_UP
129 #define BTN_DIR_DOWN BUTTON_DOWN
130 #define BTN_DIR_LEFT BUTTON_LEFT
131 #define BTN_DIR_RIGHT BUTTON_RIGHT
132 #define BTN_STARTPAUSE BUTTON_PLAY
133 #define BTN_QUIT BUTTON_POWER
134 #define BTN_STOPRESET BUTTON_REC
136 #define PLAYERS_TEXT "Up/Down"
137 #define WORMS_TEXT "Left/Right"
139 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
141 #define BTN_DIR_UP BUTTON_UP
142 #define BTN_DIR_DOWN BUTTON_DOWN
143 #define BTN_DIR_LEFT BUTTON_LEFT
144 #define BTN_DIR_RIGHT BUTTON_RIGHT
145 #define BTN_STARTPAUSE BUTTON_SELECT
146 #define BTN_QUIT BUTTON_POWER
147 #define BTN_STOPRESET BUTTON_A
149 #define PLAYERS_TEXT "Up/Down"
150 #define WORMS_TEXT "Left/Right"
153 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
154 (CONFIG_KEYPAD == SANSA_C200_PAD)
156 #define BTN_DIR_UP BUTTON_UP
157 #define BTN_DIR_DOWN BUTTON_DOWN
158 #define BTN_DIR_LEFT BUTTON_LEFT
159 #define BTN_DIR_RIGHT BUTTON_RIGHT
160 #define BTN_STARTPAUSE BUTTON_SELECT
161 #define BTN_QUIT BUTTON_POWER
162 #define BTN_STOPRESET BUTTON_REC
164 #define PLAYERS_TEXT "Up/Down"
165 #define WORMS_TEXT "Left/Right"
168 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
170 #define BTN_DIR_UP BUTTON_SCROLL_UP
171 #define BTN_DIR_DOWN BUTTON_SCROLL_DOWN
172 #define BTN_DIR_LEFT BUTTON_LEFT
173 #define BTN_DIR_RIGHT BUTTON_RIGHT
174 #define BTN_STARTPAUSE BUTTON_PLAY
175 #define BTN_QUIT BUTTON_POWER
176 #define BTN_STOPRESET BUTTON_REW
178 #define PLAYERS_TEXT "Up/Down"
179 #define WORMS_TEXT "Left/Right"
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 #define PLAYERS_TEXT "Up/Down"
192 #define WORMS_TEXT "Left/Right"
194 #elif (CONFIG_KEYPAD == MROBE100_PAD)
196 #define BTN_DIR_UP BUTTON_UP
197 #define BTN_DIR_DOWN BUTTON_DOWN
198 #define BTN_DIR_LEFT BUTTON_LEFT
199 #define BTN_DIR_RIGHT BUTTON_RIGHT
200 #define BTN_STARTPAUSE BUTTON_SELECT
201 #define BTN_QUIT BUTTON_POWER
202 #define BTN_STOPRESET BUTTON_DISPLAY
204 #define PLAYERS_TEXT "Up/Down"
205 #define WORMS_TEXT "Left/Right"
207 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
209 #define BTN_DIR_UP BUTTON_RC_VOL_UP
210 #define BTN_DIR_DOWN BUTTON_RC_VOL_DOWN
211 #define BTN_DIR_LEFT BUTTON_RC_REW
212 #define BTN_DIR_RIGHT BUTTON_RC_FF
213 #define BTN_STARTPAUSE BUTTON_RC_PLAY
214 #define BTN_QUIT BUTTON_RC_REC
215 #define BTN_STOPRESET BUTTON_RC_MODE
217 #define PLAYERS_TEXT "VOL UP/DN"
218 #define WORMS_TEXT "REW/FF"
220 #elif (CONFIG_KEYPAD == COWOND2_PAD)
222 #define BTN_QUIT BUTTON_POWER
225 #error No keymap defined!
228 #ifdef HAVE_TOUCHSCREEN
230 #define BTN_DIR_UP BUTTON_TOPMIDDLE
233 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
236 #define BTN_DIR_LEFT BUTTON_MIDLEFT
238 #ifndef BTN_DIR_RIGHT
239 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
241 #ifndef BTN_STARTPAUSE
242 #define BTN_STARTPAUSE BUTTON_CENTER
245 #define BTN_QUIT BUTTON_TOPLEFT
247 #ifndef BTN_STOPRESET
248 #define BTN_STOPRESET BUTTON_TOPRIGHT
252 #define PLAYERS_TEXT "Up/Down"
255 #define WORMS_TEXT "Left/Right"
260 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
264 #define MAX_WORM_SEGMENTS 128
265 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
269 #define MAX_WORM_SEGMENTS 128
270 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
274 #define MAX_WORM_SEGMENTS 128
275 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
279 #define MAX_WORM_SEGMENTS 128
280 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
284 #define MAX_WORM_SEGMENTS 128
285 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
289 #define MAX_WORM_SEGMENTS 256
290 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
294 #define MAX_WORM_SEGMENTS 256
295 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
299 #define MAX_WORM_SEGMENTS 512
300 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
304 #define MAX_WORM_SEGMENTS 512
305 #elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240)
309 #define MAX_WORM_SEGMENTS 512
310 #elif (LCD_WIDTH == 240) && (LCD_HEIGHT == 320)
314 #define MAX_WORM_SEGMENTS 512
317 #ifdef HAVE_LCD_COLOR
318 #define COLOR_WORM LCD_RGBPACK(80, 40, 0)
319 #define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
320 #define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
321 #define COLOR_FG LCD_RGBPACK(0, 0, 0)
322 #define COLOR_BG LCD_RGBPACK(181, 199, 231)
326 * All the properties that a worm has.
329 /* The worm is stored in a ring of xy coordinates */
330 int x
[MAX_WORM_SEGMENTS
];
331 int y
[MAX_WORM_SEGMENTS
];
333 int head
; /* index of the head within the buffer */
334 int tail
; /* index of the tail within the buffer */
335 int growing
; /* number of cyles the worm still keeps growing */
336 bool alive
; /* the worms living state */
338 /* direction vector in which the worm moves */
339 int dirx
; /* only values -1 0 1 allowed */
340 int diry
; /* only values -1 0 1 allowed */
342 /* this method is used to fetch the direction the user
343 has selected. It can be one of the values
344 human_player1, human_player2, remote_player, virtual_player.
345 All these values are fuctions, that can change the direction
347 void (*fetch_worm_direction
)(struct worm
*w
);
350 /* stores the highscore - besides it was scored by a virtual player */
351 static int highscore
;
353 #define MAX_FOOD 5 /* maximal number of food items */
355 /* The arrays store the food coordinates */
356 static int foodx
[MAX_FOOD
];
357 static int foody
[MAX_FOOD
];
359 #define MAX_ARGH 100 /* maximal number of argh items */
360 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
362 /* The arrays store the argh coordinates */
363 static int arghx
[MAX_ARGH
];
364 static int arghy
[MAX_ARGH
];
366 /* the number of arghs that are currently in use */
367 static int argh_count
;
369 /* the number of arghs per food, settable by user */
370 static int arghs_per_food
= ARGHS_PER_FOOD
;
371 /* the size of the argh, settable by user */
372 static int argh_size
= ARGH_SIZE
;
373 /* the size of the food, settable by user */
374 static int food_size
= FOOD_SIZE
;
375 /* the speed of the worm, settable by user */
376 static int speed
= SPEED
;
377 /* the amount a worm grows by eating a food, settable by user */
378 static int worm_food
= WORM_PER_FOOD
;
380 /* End additional variables */
383 /* just a buffer used for debug output */
384 static char debugout
[15];
387 /* the number of active worms (dead or alive) */
388 static int worm_count
= MAX_WORMS
;
390 /* in multiplayer mode: en- / disables the remote worm control
391 in singleplayer mode: toggles 4 / 2 button worm control */
392 static bool use_remote
= false;
394 /* return values of check_collision */
395 #define COLLISION_NONE 0
396 #define COLLISION_WORM 1
397 #define COLLISION_FOOD 2
398 #define COLLISION_ARGH 3
399 #define COLLISION_FIELD 4
401 /* constants for use as directions.
402 Note that the values are ordered clockwise.
403 Thus increasing / decreasing the values
404 is equivalent to right / left turns. */
410 /* direction of human player 1 */
411 static int player1_dir
= EAST
;
412 /* direction of human player 2 */
413 static int player2_dir
= EAST
;
414 /* direction of human player 3 */
415 static int player3_dir
= EAST
;
417 /* the number of (human) players that currently
419 static int players
= 1;
421 /* the rockbox plugin api */
422 static const struct plugin_api
* rb
;
424 #define SETTINGS_VERSION 1
425 #define SETTINGS_MIN_VERSION 1
426 #define SETTINGS_FILENAME "wormlet.cfg"
428 static struct configdata config
[] =
430 {TYPE_INT
, 0, 1024, &highscore
, "highscore", NULL
, NULL
},
431 {TYPE_INT
, 0, 15, &arghs_per_food
, "arghs per food", NULL
, NULL
},
432 {TYPE_INT
, 0, 15, &argh_size
, "argh size", NULL
, NULL
},
433 {TYPE_INT
, 0, 15, &food_size
, "food size", NULL
, NULL
},
434 {TYPE_INT
, 0, 3, &players
, "players", NULL
, NULL
},
435 {TYPE_INT
, 0, 3, &worm_count
, "worms", NULL
, NULL
},
436 {TYPE_INT
, 0, 20, &speed
, "speed", NULL
, NULL
},
437 {TYPE_INT
, 0, 15, &worm_food
, "Worm Growth Per Food", NULL
, NULL
}//,
438 //{TYPE_INT, 0, 3, &use_remote, "use remote", NULL, NULL}
442 static void set_debug_out(char *str
){
443 strcpy(debugout
, str
);
448 * Returns the direction id in which the worm
449 * currently is creeping.
450 * @param struct worm *w The worm that is to be investigated.
451 * w Must not be null.
452 * @return int A value 0 <= value < 4
453 * Note the predefined constants NORTH, SOUTH, EAST, WEST
455 static int get_worm_dir(struct worm
*w
) {
474 * Set the direction of the specified worm with a direction id.
475 * Increasing the value by 1 means to turn the worm direction
476 * to right by 90 degree.
477 * @param struct worm *w The worm that is to be altered. w Must not be null.
478 * @param int dir The new direction in which the worm is to creep.
479 * dir must be 0 <= dir < 4. Use predefined constants
480 * NORTH, SOUTH, EAST, WEST
482 static void set_worm_dir(struct worm
*w
, int dir
) {
504 * Returns the current length of the worm array. This
505 * is also a value for the number of bends that are in the worm.
506 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
508 static int get_worm_array_length(struct worm
*w
) {
509 /* initial simple calculation will be overwritten if wrong. */
510 int retVal
= w
->head
- w
->tail
;
512 /* if the worm 'crosses' the boundaries of the ringbuffer */
514 retVal
= w
->head
+ MAX_WORM_SEGMENTS
- w
->tail
;
521 * Returns the score the specified worm. The score is the length
523 * @param struct worm *w The worm that is to be investigated.
524 * w must not be null.
525 * @return int The length of the worm (>= 0).
527 static int get_score(struct worm
*w
) {
529 int length
= get_worm_array_length(w
);
531 for (i
= 0; i
< length
; i
++) {
533 /* The iteration iterates the length of the worm.
534 Here's the conversion to the true indices within the worm arrays. */
535 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
536 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
537 int startx
= w
->x
[linestart
];
538 int starty
= w
->y
[linestart
];
539 int endx
= w
->x
[lineend
];
540 int endy
= w
->y
[lineend
];
542 int minimum
, maximum
;
544 if (startx
== endx
) {
545 minimum
= MIN(starty
, endy
);
546 maximum
= MAX(starty
, endy
);
548 minimum
= MIN(startx
, endx
);
549 maximum
= MAX(startx
, endx
);
551 retval
+= abs(maximum
- minimum
);
557 * Determines wether the line specified by startx, starty, endx, endy intersects
558 * the rectangle specified by x, y, width, height. Note that the line must be exactly
559 * horizontal or vertical (startx == endx or starty == endy).
560 * @param int startx The x coordinate of the start point of the line.
561 * @param int starty The y coordinate of the start point of the line.
562 * @param int endx The x coordinate of the end point of the line.
563 * @param int endy The y coordinate of the end point of the line.
564 * @param int x The x coordinate of the top left corner of the rectangle.
565 * @param int y The y coordinate of the top left corner of the rectangle.
566 * @param int width The width of the rectangle.
567 * @param int height The height of the rectangle.
568 * @return bool Returns true if the specified line intersects with the recangle.
570 static bool line_in_rect(int startx
, int starty
, int endx
, int endy
, int x
, int y
, int width
, int height
) {
572 int simple
, simplemin
, simplemax
;
573 int compa
, compb
, compmin
, compmax
;
575 if (startx
== endx
) {
578 simplemax
= x
+ width
;
583 compmax
= y
+ height
;
587 simplemax
= y
+ height
;
596 compa
= MIN(compa
, compb
);
597 compb
= MAX(temp
, compb
);
599 if (simplemin
<= simple
&& simple
<= simplemax
) {
600 if ((compmin
<= compa
&& compa
<= compmax
) ||
601 (compmin
<= compb
&& compb
<= compmax
) ||
602 (compa
<= compmin
&& compb
>= compmax
)) {
610 * Tests wether the specified worm intersects with the rect.
611 * @param struct worm *w The worm to be investigated
612 * @param int x The x coordinate of the top left corner of the rect
613 * @param int y The y coordinate of the top left corner of the rect
614 * @param int widht The width of the rect
615 * @param int height The height of the rect
616 * @return bool Returns true if the worm intersects with the rect
618 static bool worm_in_rect(struct worm
*w
, int x
, int y
, int width
, int height
) {
622 /* get_worm_array_length is expensive -> buffer the value */
623 int wormLength
= get_worm_array_length(w
);
626 /* test each entry that is part of the worm */
627 for (i
= 0; i
< wormLength
&& retval
== false; i
++) {
629 /* The iteration iterates the length of the worm.
630 Here's the conversion to the true indices within the worm arrays. */
631 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
632 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
633 int startx
= w
->x
[linestart
];
634 int starty
= w
->y
[linestart
];
635 int endx
= w
->x
[lineend
];
636 int endy
= w
->y
[lineend
];
638 retval
= line_in_rect(startx
, starty
, endx
, endy
, x
, y
, width
, height
);
645 * Checks wether a specific food in the food arrays is at the
646 * specified coordinates.
647 * @param int foodIndex The index of the food in the food arrays
648 * @param int x the x coordinate.
649 * @param int y the y coordinate.
650 * @return Returns true if the coordinate hits the food specified by
653 static bool specific_food_collision(int foodIndex
, int x
, int y
) {
655 if (x
>= foodx
[foodIndex
] &&
656 x
< foodx
[foodIndex
] + food_size
&&
657 y
>= foody
[foodIndex
] &&
658 y
< foody
[foodIndex
] + food_size
) {
666 * Returns the index of the food that is at the
667 * given coordinates. If no food is at the coordinates
669 * @return int -1 <= value < MAX_FOOD
671 static int food_collision(int x
, int y
) {
674 for (i
= 0; i
< MAX_FOOD
; i
++) {
675 if (specific_food_collision(i
, x
, y
)) {
684 * Checks wether a specific argh in the argh arrays is at the
685 * specified coordinates.
686 * @param int arghIndex The index of the argh in the argh arrays
687 * @param int x the x coordinate.
688 * @param int y the y coordinate.
689 * @return Returns true if the coordinate hits the argh specified by
692 static bool specific_argh_collision(int arghIndex
, int x
, int y
) {
694 if ( x
>= arghx
[arghIndex
] &&
695 y
>= arghy
[arghIndex
] &&
696 x
< arghx
[arghIndex
] + argh_size
&&
697 y
< arghy
[arghIndex
] + argh_size
)
706 * Returns the index of the argh that is at the
707 * given coordinates. If no argh is at the coordinates
709 * @param int x The x coordinate.
710 * @param int y The y coordinate.
711 * @return int -1 <= value < argh_count <= MAX_ARGH
713 static int argh_collision(int x
, int y
) {
717 /* search for the argh that has the specified coords */
718 for (i
= 0; i
< argh_count
; i
++) {
719 if (specific_argh_collision(i
, x
, y
)) {
728 * Checks wether the worm collides with the food at the specfied food-arrays.
729 * @param int foodIndex The index of the food in the arrays. Ensure the value is
730 * 0 <= foodIndex <= MAX_FOOD
731 * @return Returns true if the worm collides with the specified food.
733 static bool worm_food_collision(struct worm
*w
, int foodIndex
)
737 retVal
= worm_in_rect(w
, foodx
[foodIndex
], foody
[foodIndex
],
738 food_size
- 1, food_size
- 1);
744 * Returns true if the worm hits the argh within the next moves (unless
745 * the worm changes it's direction).
746 * @param struct worm *w - The worm to investigate
747 * @param int argh_idx - The index of the argh
748 * @param int moves - The number of moves that are considered.
749 * @return Returns false if the specified argh is not hit within the next
752 static bool worm_argh_collision_in_moves(struct worm
*w
, int argh_idx
, int moves
){
758 x2
= w
->x
[w
->head
] + moves
* w
->dirx
;
759 y2
= w
->y
[w
->head
] + moves
* w
->diry
;
761 retVal
= line_in_rect(x1
, y1
, x2
, y2
, arghx
[argh_idx
], arghy
[argh_idx
],
762 argh_size
, argh_size
);
767 * Checks wether the worm collides with the argh at the specfied argh-arrays.
768 * @param int arghIndex The index of the argh in the arrays.
769 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
770 * @return Returns true if the worm collides with the specified argh.
772 static bool worm_argh_collision(struct worm
*w
, int arghIndex
)
776 retVal
= worm_in_rect(w
, arghx
[arghIndex
], arghy
[arghIndex
],
777 argh_size
- 1, argh_size
- 1);
783 * Find new coordinates for the food stored in foodx[index], foody[index]
784 * that don't collide with any other food or argh
786 * Ensure that 0 <= index < MAX_FOOD.
788 static void make_food(int index
) {
792 bool collisionDetected
= false;
796 /* make coordinates for a new food so that
797 the entire food lies within the FIELD */
798 x
= rb
->rand() % (FIELD_RECT_WIDTH
- food_size
);
799 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- food_size
);
801 /* Ensure that the new food doesn't collide with any
802 existing foods or arghs.
803 If one or more corners of the new food hit any existing
804 argh or food a collision is detected.
807 food_collision(x
, y
) >= 0 ||
808 food_collision(x
, y
+ food_size
- 1) >= 0 ||
809 food_collision(x
+ food_size
- 1, y
) >= 0 ||
810 food_collision(x
+ food_size
- 1, y
+ food_size
- 1) >= 0 ||
811 argh_collision(x
, y
) >= 0 ||
812 argh_collision(x
, y
+ food_size
- 1) >= 0 ||
813 argh_collision(x
+ food_size
- 1, y
) >= 0 ||
814 argh_collision(x
+ food_size
- 1, y
+ food_size
- 1) >= 0;
816 /* use coordinates for further testing */
820 /* now test wether we accidently hit the worm with food ;) */
822 for (i
= 0; i
< worm_count
&& !collisionDetected
; i
++) {
823 collisionDetected
|= worm_food_collision(&worms
[i
], index
);
826 while (collisionDetected
);
831 * Clears a food from the lcd buffer.
832 * @param int index The index of the food arrays under which
833 * the coordinates of the desired food can be found. Ensure
834 * that the value is 0 <= index <= MAX_FOOD.
836 static void clear_food(int index
)
838 /* remove the old food from the screen */
839 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
840 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
,
841 foody
[index
] + FIELD_RECT_Y
,
842 food_size
, food_size
);
843 rb
->lcd_set_drawmode(DRMODE_SOLID
);
847 * Draws a food in the lcd buffer.
848 * @param int index The index of the food arrays under which
849 * the coordinates of the desired food can be found. Ensure
850 * that the value is 0 <= index <= MAX_FOOD.
852 static void draw_food(int index
)
854 /* draw the food object */
855 #ifdef HAVE_LCD_COLOR
856 rb
->lcd_set_foreground(COLOR_FOOD
);
858 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
,
859 foody
[index
] + FIELD_RECT_Y
,
860 food_size
, food_size
);
861 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
862 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
+ 1,
863 foody
[index
] + FIELD_RECT_Y
+ 1,
864 food_size
- 2, food_size
- 2);
865 rb
->lcd_set_drawmode(DRMODE_SOLID
);
866 #ifdef HAVE_LCD_COLOR
867 rb
->lcd_set_foreground(COLOR_FG
);
872 * Find new coordinates for the argh stored in arghx[index], arghy[index]
873 * that don't collide with any other food or argh.
875 * Ensure that 0 <= index < argh_count < MAX_ARGH.
877 static void make_argh(int index
)
881 bool collisionDetected
= false;
885 /* make coordinates for a new argh so that
886 the entire food lies within the FIELD */
887 x
= rb
->rand() % (FIELD_RECT_WIDTH
- argh_size
);
888 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- argh_size
);
890 /* Ensure that the new argh doesn't intersect with any
891 existing foods or arghs.
892 If one or more corners of the new argh hit any existing
893 argh or food an intersection is detected.
896 food_collision(x
, y
) >= 0 ||
897 food_collision(x
, y
+ argh_size
- 1) >= 0 ||
898 food_collision(x
+ argh_size
- 1, y
) >= 0 ||
899 food_collision(x
+ argh_size
- 1, y
+ argh_size
- 1) >= 0 ||
900 argh_collision(x
, y
) >= 0 ||
901 argh_collision(x
, y
+ argh_size
- 1) >= 0 ||
902 argh_collision(x
+ argh_size
- 1, y
) >= 0 ||
903 argh_collision(x
+ argh_size
- 1, y
+ argh_size
- 1) >= 0;
905 /* use the candidate coordinates to make a real argh */
909 /* now test wether we accidently hit the worm with argh ;) */
910 for (i
= 0; i
< worm_count
&& !collisionDetected
; i
++) {
911 collisionDetected
|= worm_argh_collision(&worms
[i
], index
);
912 collisionDetected
|= worm_argh_collision_in_moves(&worms
[i
], index
,
916 while (collisionDetected
);
921 * Draws an argh in the lcd buffer.
922 * @param int index The index of the argh arrays under which
923 * the coordinates of the desired argh can be found. Ensure
924 * that the value is 0 <= index < argh_count <= MAX_ARGH.
926 static void draw_argh(int index
)
928 /* draw the new argh */
929 #ifdef HAVE_LCD_COLOR
930 rb
->lcd_set_foreground(COLOR_ARGH
);
932 rb
->lcd_fillrect(arghx
[index
] + FIELD_RECT_X
,
933 arghy
[index
] + FIELD_RECT_Y
,
934 argh_size
, argh_size
);
935 #ifdef HAVE_LCD_COLOR
936 rb
->lcd_set_foreground(COLOR_FG
);
940 static void virtual_player(struct worm
*w
);
942 * Initialzes the specified worm with INITIAL_WORM_LENGTH
943 * and the tail at the specified position. The worm will
944 * be initialized alive and creeping EAST.
945 * @param struct worm *w The worm that is to be initialized
946 * @param int x The x coordinate at which the tail of the worm starts.
947 * x must be 0 <= x < FIELD_RECT_WIDTH.
948 * @param int y The y coordinate at which the tail of the worm starts
949 * y must be 0 <= y < FIELD_RECT_WIDTH.
951 static void init_worm(struct worm
*w
, int x
, int y
){
952 /* initialize the worm size */
956 w
->x
[w
->head
] = x
+ 1;
962 /* set the initial direction the worm creeps to */
966 w
->growing
= INITIAL_WORM_LENGTH
- 1;
968 w
->fetch_worm_direction
= virtual_player
;
972 * Writes the direction that was stored for
973 * human player 1 into the specified worm. This function
974 * may be used to be stored in worm.fetch_worm_direction.
976 * the direction is read from player1_dir.
977 * @param struct worm *w - The worm of which the direction
980 static void human_player1(struct worm
*w
) {
981 set_worm_dir(w
, player1_dir
);
985 * Writes the direction that was stored for
986 * human player 2 into the specified worm. This function
987 * may be used to be stored in worm.fetch_worm_direction.
989 * the direction is read from player2_dir.
990 * @param struct worm *w - The worm of which the direction
993 static void human_player2(struct worm
*w
) {
994 set_worm_dir(w
, player2_dir
);
998 * Writes the direction that was stored for
999 * human player using a remote control
1000 * into the specified worm. This function
1001 * may be used to be stored in worm.fetch_worm_direction.
1003 * the direction is read from player3_dir.
1004 * @param struct worm *w - The worm of which the direction
1007 static void remote_player(struct worm
*w
) {
1008 set_worm_dir(w
, player3_dir
);
1012 * Initializes the worm-, food- and argh-arrays, draws a frame,
1013 * makes some food and argh and display all that stuff.
1015 static void init_wormlet(void)
1019 for (i
= 0; i
< worm_count
; i
++) {
1020 /* Initialize all the worm coordinates to center. */
1021 int x
= (int)(FIELD_RECT_WIDTH
/ 2);
1022 int y
= (int)((FIELD_RECT_HEIGHT
- 20)/ 2) + i
* 10;
1024 init_worm(&worms
[i
], x
, y
);
1032 worms
[0].fetch_worm_direction
= human_player1
;
1037 worms
[1].fetch_worm_direction
= remote_player
;
1039 worms
[1].fetch_worm_direction
= human_player2
;
1044 worms
[2].fetch_worm_direction
= human_player2
;
1047 /* Needed when the game is restarted using BTN_STOPRESET */
1048 rb
->lcd_clear_display();
1050 /* make and display some food and argh */
1051 argh_count
= MAX_FOOD
;
1052 for (i
= 0; i
< MAX_FOOD
; i
++) {
1059 /* draw the game field */
1060 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
1061 rb
->lcd_fillrect(0, 0, FIELD_RECT_WIDTH
+ 2, FIELD_RECT_HEIGHT
+ 2);
1062 rb
->lcd_fillrect(1, 1, FIELD_RECT_WIDTH
, FIELD_RECT_HEIGHT
);
1063 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1065 /* make everything visible */
1071 * Move the worm one step further if it is alive.
1072 * The direction in which the worm moves is taken from dirx and diry.
1073 * move_worm decreases growing if > 0. While the worm is growing the tail
1074 * is left untouched.
1075 * @param struct worm *w The worm to move. w must not be NULL.
1077 static void move_worm(struct worm
*w
)
1080 /* determine the head point and its precessor */
1081 int headx
= w
->x
[w
->head
];
1082 int heady
= w
->y
[w
->head
];
1083 int prehead
= (w
->head
+ MAX_WORM_SEGMENTS
- 1) % MAX_WORM_SEGMENTS
;
1084 int preheadx
= w
->x
[prehead
];
1085 int preheady
= w
->y
[prehead
];
1087 /* determine the old direction */
1090 if (headx
== preheadx
) {
1092 olddiry
= (heady
> preheady
) ? 1 : -1;
1095 olddirx
= (headx
> preheadx
) ? 1 : -1;
1099 a change of direction means a new segment
1101 if (olddirx
!= w
->dirx
||
1102 olddiry
!= w
->diry
) {
1103 w
->head
= (w
->head
+ 1) % MAX_WORM_SEGMENTS
;
1106 /* new head position */
1107 w
->x
[w
->head
] = headx
+ w
->dirx
;
1108 w
->y
[w
->head
] = heady
+ w
->diry
;
1111 /* while the worm is growing no tail procession is necessary */
1112 if (w
->growing
> 0) {
1113 /* update the worms grow state */
1117 /* if the worm isn't growing the tail has to be dragged */
1119 /* index of the end of the tail segment */
1120 int tail_segment_end
= (w
->tail
+ 1) % MAX_WORM_SEGMENTS
;
1122 /* drag the end of the tail */
1123 /* only one coordinate has to be altered. Here it is
1124 determined which one */
1125 int dir
= 0; /* specifies wether the coord has to be in- or decreased */
1126 if (w
->x
[w
->tail
] == w
->x
[tail_segment_end
]) {
1127 dir
= (w
->y
[w
->tail
] - w
->y
[tail_segment_end
] < 0) ? 1 : -1;
1128 w
->y
[w
->tail
] += dir
;
1130 dir
= (w
->x
[w
->tail
] - w
->x
[tail_segment_end
] < 0) ? 1 : -1;
1131 w
->x
[w
->tail
] += dir
;
1134 /* when the tail has been dragged so far that it meets
1135 the next segment start the tail segment is obsolete and
1137 if (w
->x
[w
->tail
] == w
->x
[tail_segment_end
] &&
1138 w
->y
[w
->tail
] == w
->y
[tail_segment_end
]){
1140 /* drop the last tail point */
1141 w
->tail
= tail_segment_end
;
1148 * Draws the head and clears the tail of the worm in
1149 * the display buffer. lcd_update() is NOT called thus
1150 * the caller has to take care that the buffer is displayed.
1152 static void draw_worm(struct worm
*w
)
1154 #ifdef HAVE_LCD_COLOR
1155 rb
->lcd_set_foreground(COLOR_WORM
);
1157 /* draw the new head */
1158 int x
= w
->x
[w
->head
];
1159 int y
= w
->y
[w
->head
];
1160 if (x
>= 0 && x
< FIELD_RECT_WIDTH
&& y
>= 0 && y
< FIELD_RECT_HEIGHT
) {
1161 rb
->lcd_drawpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
1164 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1166 /* clear the space behind the worm */
1169 if (x
>= 0 && x
< FIELD_RECT_WIDTH
&& y
>= 0 && y
< FIELD_RECT_HEIGHT
) {
1170 rb
->lcd_drawpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
1172 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1173 #ifdef HAVE_LCD_COLOR
1174 rb
->lcd_set_foreground(COLOR_FG
);
1179 * Checks wether the coordinate is part of the worm. Returns
1180 * true if any part of the worm was hit - including the head.
1181 * @param x int The x coordinate
1182 * @param y int The y coordinate
1183 * @return int The index of the worm arrays that contain x, y.
1184 * Returns -1 if the coordinates are not part of the worm.
1186 static int specific_worm_collision(struct worm
*w
, int x
, int y
)
1190 /* get_worm_array_length is expensive -> buffer the value */
1191 int wormLength
= get_worm_array_length(w
);
1194 /* test each entry that is part of the worm */
1195 for (i
= 0; i
< wormLength
&& retVal
== -1; i
++) {
1197 /* The iteration iterates the length of the worm.
1198 Here's the conversion to the true indices within the worm arrays. */
1199 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
1200 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
1201 bool samex
= (w
->x
[linestart
] == x
) && (w
->x
[lineend
] == x
);
1202 bool samey
= (w
->y
[linestart
] == y
) && (w
->y
[lineend
] == y
);
1203 if (samex
|| samey
){
1204 int test
, min
, max
, tmp
;
1207 min
= w
->x
[linestart
];
1208 max
= w
->x
[lineend
];
1211 min
= w
->y
[linestart
];
1212 max
= w
->y
[lineend
];
1217 min
= MIN(min
, max
);
1218 max
= MAX(tmp
, max
);
1220 if (min
<= test
&& test
<= max
) {
1229 * Increases the length of the specified worm by marking
1230 * that it may grow by len pixels. Note that the worm has
1231 * to move to make the growing happen.
1232 * @param worm *w The worm that is to be altered.
1233 * @param int len A positive value specifying the amount of
1234 * pixels the worm may grow.
1236 static void add_growing(struct worm
*w
, int len
) {
1241 * Determins the worm that is at the coordinates x, y. The parameter
1242 * w is a switch parameter that changes the functionality of worm_collision.
1243 * If w is specified and x,y hits the head of w NULL is returned.
1244 * This is a useful way to determine wether the head of w hits
1245 * any worm but including itself but excluding its own head.
1246 * (It hits always its own head ;))
1247 * If w is set to NULL worm_collision returns any worm including all heads
1248 * that is at position of x,y.
1249 * @param struct worm *w The worm of which the head should be excluded in
1250 * the test. w may be set to NULL.
1251 * @param int x The x coordinate that is checked
1252 * @param int y The y coordinate that is checkec
1253 * @return struct worm* The worm that has been hit by x,y. If no worm
1254 * was at the position NULL is returned.
1256 static struct worm
* worm_collision(struct worm
*w
, int x
, int y
){
1257 struct worm
*retVal
= NULL
;
1259 for (i
= 0; (i
< worm_count
) && (retVal
== NULL
); i
++) {
1260 int collision_at
= specific_worm_collision(&worms
[i
], x
, y
);
1261 if (collision_at
!= -1) {
1262 if (!(w
== &worms
[i
] && collision_at
== w
->head
)){
1271 * Returns true if the head of the worm just has
1272 * crossed the field boundaries.
1273 * @return bool true if the worm just has wrapped.
1275 static bool field_collision(struct worm
*w
)
1277 bool retVal
= false;
1278 if ((w
->x
[w
->head
] >= FIELD_RECT_WIDTH
) ||
1279 (w
->y
[w
->head
] >= FIELD_RECT_HEIGHT
) ||
1280 (w
->x
[w
->head
] < 0) ||
1281 (w
->y
[w
->head
] < 0))
1290 * Returns true if the specified coordinates are within the
1291 * field specified by the FIELD_RECT_XXX constants.
1292 * @param int x The x coordinate of the point that is investigated
1293 * @param int y The y coordinate of the point that is investigated
1294 * @return bool Returns false if x,y specifies a point outside the
1297 static bool is_in_field_rect(int x
, int y
) {
1298 bool retVal
= false;
1299 retVal
= (x
>= 0 && x
< FIELD_RECT_WIDTH
&&
1300 y
>= 0 && y
< FIELD_RECT_HEIGHT
);
1305 * Checks and returns wether the head of the w
1306 * is colliding with something currently.
1307 * @return int One of the values:
1314 static int check_collision(struct worm
*w
)
1316 int retVal
= COLLISION_NONE
;
1318 if (worm_collision(w
, w
->x
[w
->head
], w
->y
[w
->head
]) != NULL
)
1319 retVal
= COLLISION_WORM
;
1321 if (food_collision(w
->x
[w
->head
], w
->y
[w
->head
]) >= 0)
1322 retVal
= COLLISION_FOOD
;
1324 if (argh_collision(w
->x
[w
->head
], w
->y
[w
->head
]) >= 0)
1325 retVal
= COLLISION_ARGH
;
1327 if (field_collision(w
))
1328 retVal
= COLLISION_FIELD
;
1334 * Returns the index of the food that is closest to the point
1335 * specified by x, y. This index may be used in the foodx and
1337 * @param int x The x coordinate of the point
1338 * @param int y The y coordinate of the point
1339 * @return int A value usable as index in foodx and foody.
1341 static int get_nearest_food(int x
, int y
){
1342 int nearestfood
= 0;
1343 int olddistance
= FIELD_RECT_WIDTH
+ FIELD_RECT_HEIGHT
;
1347 for (foodindex
= 0; foodindex
< MAX_FOOD
; foodindex
++) {
1349 deltax
= foodx
[foodindex
] - x
;
1350 deltay
= foody
[foodindex
] - y
;
1351 deltax
= deltax
> 0 ? deltax
: deltax
* (-1);
1352 deltay
= deltay
> 0 ? deltay
: deltay
* (-1);
1353 distance
= deltax
+ deltay
;
1355 if (distance
< olddistance
) {
1356 olddistance
= distance
;
1357 nearestfood
= foodindex
;
1364 * Returns wether the specified position is next to the worm
1365 * and in the direction the worm looks. Use this method to
1366 * test wether this position would be hit with the next move of
1367 * the worm unless the worm changes its direction.
1368 * @param struct worm *w - The worm to be investigated
1369 * @param int x - The x coordinate of the position to test.
1370 * @param int y - The y coordinate of the position to test.
1371 * @return Returns true if the worm will hit the position unless
1372 * it change its direction before the next move.
1374 static bool is_in_front_of_worm(struct worm
*w
, int x
, int y
) {
1375 bool infront
= false;
1376 int deltax
= x
- w
->x
[w
->head
];
1377 int deltay
= y
- w
->y
[w
->head
];
1380 infront
= (w
->diry
* deltay
) > 0;
1382 infront
= (w
->dirx
* deltax
) > 0;
1388 * Returns true if the worm will collide with the next move unless
1389 * it changes its direction.
1390 * @param struct worm *w - The worm to be investigated.
1391 * @return Returns true if the worm will collide with the next move
1392 * unless it changes its direction.
1394 static bool will_worm_collide(struct worm
*w
) {
1395 int x
= w
->x
[w
->head
] + w
->dirx
;
1396 int y
= w
->y
[w
->head
] + w
->diry
;
1397 bool retVal
= !is_in_field_rect(x
, y
);
1399 retVal
= (argh_collision(x
, y
) != -1);
1403 retVal
= (worm_collision(w
, x
, y
) != NULL
);
1410 * may be used to be stored in worm.fetch_worm_direction for
1411 * worms that are not controlled by humans but by artificial stupidity.
1412 * A direction is searched that doesn't lead to collision but to the nearest
1413 * food - but not very intelligent. The direction is written to the specified
1415 * @param struct worm *w - The worm of which the direction
1418 static void virtual_player(struct worm
*w
) {
1420 int plana
, planb
, planc
;
1421 /* find the next lunch */
1422 int nearestfood
= get_nearest_food(w
->x
[w
->head
], w
->y
[w
->head
]);
1424 /* determine in which direction it is */
1426 /* in front of me? */
1427 bool infront
= is_in_front_of_worm(w
, foodx
[nearestfood
], foody
[nearestfood
]);
1429 /* left right of me? */
1430 int olddir
= get_worm_dir(w
);
1431 set_worm_dir(w
, (olddir
+ 1) % 4);
1432 isright
= is_in_front_of_worm(w
, foodx
[nearestfood
], foody
[nearestfood
]);
1433 set_worm_dir(w
, olddir
);
1435 /* detect situation, set strategy */
1439 planb
= (olddir
+ 1) % 4;
1440 planc
= (olddir
+ 3) % 4;
1443 planb
= (olddir
+ 3) % 4;
1444 planc
= (olddir
+ 1) % 4;
1448 plana
= (olddir
+ 1) % 4;
1450 planc
= (olddir
+ 3) % 4;
1452 plana
= (olddir
+ 3) % 4;
1454 planc
= (olddir
+ 1) % 4;
1458 /* test for collision */
1459 set_worm_dir(w
, plana
);
1460 if (will_worm_collide(w
)){
1463 set_worm_dir(w
, planb
);
1465 /* test for collision */
1466 if (will_worm_collide(w
)) {
1469 set_worm_dir(w
, planc
);
1475 * prints out the score board with all the status information
1478 static void score_board(void)
1484 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1485 rb
->lcd_fillrect(FIELD_RECT_WIDTH
+ 2, 0, LCD_WIDTH
- FIELD_RECT_WIDTH
- 2, LCD_HEIGHT
);
1486 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1487 for (i
= 0; i
< worm_count
; i
++) {
1488 int score
= get_score(&worms
[i
]);
1491 if (worms
[i
].fetch_worm_direction
!= virtual_player
){
1492 if (highscore
< score
) {
1498 rb
->snprintf(buf
, sizeof (buf
),"Len:%d", score
);
1501 switch (check_collision(&worms
[i
])) {
1502 case COLLISION_NONE
:
1503 if (worms
[i
].growing
> 0)
1513 case COLLISION_WORM
:
1517 case COLLISION_FOOD
:
1521 case COLLISION_ARGH
:
1525 case COLLISION_FIELD
:
1529 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, y
, buf
);
1530 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, y
+8, buf2
);
1532 if (!worms
[i
].alive
){
1533 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
1534 rb
->lcd_fillrect(FIELD_RECT_WIDTH
+ 2, y
,
1535 LCD_WIDTH
- FIELD_RECT_WIDTH
- 2, 17);
1536 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1540 rb
->snprintf(buf
, sizeof(buf
), "Hs: %d", highscore
);
1541 #ifndef DEBUG_WORMLET
1542 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, LCD_HEIGHT
- 8, buf
);
1544 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, LCD_HEIGHT
- 8, debugout
);
1549 * Checks for collisions of the worm and its environment and
1550 * takes appropriate actions like growing the worm or killing it.
1551 * @return bool Returns true if the worm is dead. Returns
1552 * false if the worm is healthy, up and creeping.
1554 static bool process_collisions(struct worm
*w
)
1558 w
->alive
&= !field_collision(w
);
1562 /* check if food was eaten */
1563 index
= food_collision(w
->x
[w
->head
], w
->y
[w
->head
]);
1571 for (i
= 0; i
< arghs_per_food
; i
++) {
1573 if (argh_count
> MAX_ARGH
)
1574 argh_count
= MAX_ARGH
;
1575 make_argh(argh_count
- 1);
1576 draw_argh(argh_count
- 1);
1579 add_growing(w
, worm_food
);
1584 /* check if argh was eaten */
1586 index
= argh_collision(w
->x
[w
->head
], w
->y
[w
->head
]);
1591 if (worm_collision(w
, w
->x
[w
->head
], w
->y
[w
->head
]) != NULL
) {
1601 * The main loop of the game.
1602 * @return bool Returns true if the game ended
1603 * with a dead worm. Returns false if the user
1604 * aborted the game manually.
1606 static int run(void)
1609 int wormDead
= false;
1610 bool paused
= false;
1612 /* ticks are counted to compensate speed variations */
1613 long cycle_start
= 0, cycle_end
= 0;
1614 #ifdef DEBUG_WORMLET
1615 int ticks_to_max_cycle_reset
= 20;
1620 /* initialize the board and so on */
1623 cycle_start
= *rb
->current_tick
;
1624 /* change the direction of the worm */
1628 long cycle_duration
=0;
1630 #ifdef HAS_BUTTON_HOLD
1631 if (rb
->button_hold())
1636 case BTN_STARTPAUSE
:
1641 return 1; /* restart game */
1649 return 2; /* back to menu */
1656 if (players
== 1 && !use_remote
) {
1657 player1_dir
= NORTH
;
1662 if (players
== 1 && !use_remote
) {
1663 player1_dir
= SOUTH
;
1668 if (players
!= 1 || use_remote
) {
1669 player1_dir
= (player1_dir
+ 3) % 4;
1676 if (players
!= 1 || use_remote
) {
1677 player1_dir
= (player1_dir
+ 1) % 4;
1684 case BTN_PLAYER2_DIR1
:
1685 player2_dir
= (player2_dir
+ 3) % 4;
1688 case BTN_PLAYER2_DIR2
:
1689 player2_dir
= (player2_dir
+ 1) % 4;
1695 player3_dir
= (player3_dir
+ 1) % 4;
1699 player3_dir
= (player3_dir
+ 3) % 4;
1705 for (i
= 0; i
< worm_count
; i
++) {
1706 worms
[i
].fetch_worm_direction(&worms
[i
]);
1710 for (i
= 0; i
< worm_count
; i
++){
1711 struct worm
*w
= &worms
[i
];
1713 wormDead
&= process_collisions(w
);
1718 if (button
== BTN_STOPRESET
) {
1722 /* here the wormlet game cycle ends
1723 thus the current tick is stored
1725 cycle_end
= *rb
->current_tick
;
1727 /* The duration of the game cycle */
1728 cycle_duration
= cycle_end
- cycle_start
;
1729 cycle_duration
= MAX(0, cycle_duration
);
1730 cycle_duration
= MIN(speed
-1, cycle_duration
);
1733 #ifdef DEBUG_WORMLET
1734 ticks_to_max_cycle_reset
--;
1735 if (ticks_to_max_cycle_reset
<= 0) {
1739 if (max_cycle
< cycle_duration
) {
1740 max_cycle
= cycle_duration
;
1741 ticks_to_max_cycle_reset
= 20;
1743 rb
->snprintf(buf
, sizeof buf
, "ticks %d", max_cycle
);
1747 /* adjust the number of ticks to wait for a button.
1748 This ensures that a complete game cycle including
1749 user input runs in constant time */
1750 button
= rb
->button_get_w_tmo(speed
- cycle_duration
);
1751 cycle_start
= *rb
->current_tick
;
1754 rb
->splash(HZ
*2, "Game Over!");
1756 return 2; /* back to menu */
1759 #ifdef DEBUG_WORMLET
1762 * Just a test routine that checks that worm_food_collision works
1763 * in some typical situations.
1765 static void test_worm_food_collision(void) {
1766 int collision_count
= 0;
1768 rb
->lcd_clear_display();
1769 init_worm(&worms
[0], 10, 10);
1770 add_growing(&worms
[0], 10);
1771 set_worm_dir(&worms
[0], EAST
);
1772 for (i
= 0; i
< 10; i
++) {
1773 move_worm(&worms
[0]);
1774 draw_worm(&worms
[0]);
1777 set_worm_dir(&worms
[0], SOUTH
);
1778 for (i
= 0; i
< 10; i
++) {
1779 move_worm(&worms
[0]);
1780 draw_worm(&worms
[0]);
1785 for (foody
[0] = 20; foody
[0] > 0; foody
[0] --) {
1788 draw_worm(&worms
[0]);
1790 collision
= worm_food_collision(&worms
[0], 0);
1794 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1795 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1798 if (collision_count
!= food_size
) {
1799 rb
->button_get(true);
1804 for (foodx
[0] = 30; foodx
[0] > 0; foodx
[0] --) {
1807 draw_worm(&worms
[0]);
1809 collision
= worm_food_collision(&worms
[0], 0);
1813 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1814 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1817 if (collision_count
!= food_size
* 2) {
1818 rb
->button_get(true);
1823 static bool expensive_worm_in_rect(struct worm
*w
, int rx
, int ry
, int rw
, int rh
){
1825 bool retVal
= false;
1826 for (x
= rx
; x
< rx
+ rw
; x
++){
1827 for (y
= ry
; y
< ry
+ rh
; y
++) {
1828 if (specific_worm_collision(w
, x
, y
) != -1) {
1836 static void test_worm_argh_collision(void) {
1839 int collision_count
= 0;
1840 rb
->lcd_clear_display();
1841 init_worm(&worms
[0], 10, 10);
1842 add_growing(&worms
[0], 40);
1843 for (dir
= 0; dir
< 4; dir
++) {
1844 set_worm_dir(&worms
[0], (EAST
+ dir
) % 4);
1845 for (i
= 0; i
< 10; i
++) {
1846 move_worm(&worms
[0]);
1847 draw_worm(&worms
[0]);
1852 for (arghy
[0] = 0; arghy
[0] < FIELD_RECT_HEIGHT
- argh_size
; arghy
[0]++){
1856 collision
= worm_argh_collision(&worms
[0], 0);
1860 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1861 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1864 if (collision_count
!= argh_size
* 2) {
1865 rb
->button_get(true);
1869 for (arghx
[0] = 0; arghx
[0] < FIELD_RECT_HEIGHT
- argh_size
; arghx
[0]++){
1873 collision
= worm_argh_collision(&worms
[0], 0);
1877 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1878 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1881 if (collision_count
!= argh_size
* 4) {
1882 rb
->button_get(true);
1886 static int testline_in_rect(void) {
1887 int testfailed
= -1;
1900 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1901 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1902 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1903 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1905 rb
->lcd_putsxy(0, 0, "failed 1");
1906 rb
->button_get(true);
1912 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1913 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1914 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1915 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1916 rb
->lcd_putsxy(0, 0, "failed 2");
1918 rb
->button_get(true);
1924 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1925 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1926 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1927 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1928 rb
->lcd_putsxy(0, 0, "failed 3");
1930 rb
->button_get(true);
1936 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1937 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1938 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1939 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1940 rb
->lcd_putsxy(0, 0, "failed 4");
1942 rb
->button_get(true);
1948 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
1949 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1950 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1951 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1952 rb
->lcd_putsxy(0, 0, "failed 5");
1954 rb
->button_get(true);
1961 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
1962 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1963 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1964 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1965 rb
->lcd_putsxy(0, 0, "failed 6");
1967 rb
->button_get(true);
1976 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1977 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1978 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1979 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1980 rb
->lcd_putsxy(0, 0, "failed 7");
1982 rb
->button_get(true);
1988 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1989 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1990 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1991 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1992 rb
->lcd_putsxy(0, 0, "failed 8");
1994 rb
->button_get(true);
2000 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2001 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2002 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2003 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2004 rb
->lcd_putsxy(0, 0, "failed 9");
2006 rb
->button_get(true);
2012 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2013 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2014 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2015 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2016 rb
->lcd_putsxy(0, 0, "failed 10");
2018 rb
->button_get(true);
2024 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2025 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2026 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2027 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2028 rb
->lcd_putsxy(0, 0, "failed 11");
2030 rb
->button_get(true);
2037 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2038 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2039 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2040 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2041 rb
->lcd_putsxy(0, 0, "failed 12");
2043 rb
->button_get(true);
2057 if (!(line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2058 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
))) {
2059 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2060 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2061 rb
->lcd_putsxy(0, 0, "failed 13");
2063 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_drawline(x1
, y1
, x2
, y2
);
2080 rb
->lcd_invertrect(rx
, ry
, rw
, rh
);
2081 rb
->lcd_putsxy(0, 0, "failed 14");
2083 rb
->button_get(true);
2087 rb
->lcd_clear_display();
2093 * Just a test routine to test wether specific_worm_collision might work properly
2095 static int test_specific_worm_collision(void) {
2101 rb
->lcd_clear_display();
2102 init_worm(&worms
[0], 10, 20);
2103 add_growing(&worms
[0], 20 - INITIAL_WORM_LENGTH
);
2105 for (dir
= EAST
; dir
< EAST
+ 4; dir
++) {
2107 set_worm_dir(&worms
[0], dir
% 4);
2108 for (i
= 0; i
< 5; i
++) {
2109 if (!(dir
% 4 == NORTH
&& i
== 9)) {
2110 move_worm(&worms
[0]);
2111 draw_worm(&worms
[0]);
2116 for (y
= 15; y
< 30; y
++){
2117 for (x
= 5; x
< 20; x
++) {
2118 if (specific_worm_collision(&worms
[0], x
, y
) != -1) {
2121 rb
->lcd_invertpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
2122 rb
->snprintf(buf
, sizeof buf
, "collisions %d", collisions
);
2123 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2127 if (collisions
!= 21) {
2128 rb
->button_get(true);
2133 static void test_make_argh(void){
2138 int last_failures
= 0;
2140 rb
->lcd_clear_display();
2143 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++) {
2144 init_worm(&worms
[worm_idx
], 10 + worm_idx
* 20, 20);
2145 add_growing(&worms
[worm_idx
], 40 - INITIAL_WORM_LENGTH
);
2148 for (dir
= EAST
; dir
< EAST
+ 4; dir
++) {
2149 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++) {
2150 set_worm_dir(&worms
[worm_idx
], dir
% 4);
2151 for (i
= 0; i
< 10; i
++) {
2152 if (!(dir
% 4 == NORTH
&& i
== 9)) {
2153 move_worm(&worms
[worm_idx
]);
2154 draw_worm(&worms
[worm_idx
]);
2162 for (seed
= 0; hit
< 20; seed
+= 2) {
2166 x
= rb
->rand() % (FIELD_RECT_WIDTH
- argh_size
);
2167 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- argh_size
);
2169 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++){
2170 if (expensive_worm_in_rect(&worms
[worm_idx
], x
, y
, argh_size
, argh_size
)) {
2174 tries
= make_argh(0);
2175 if ((x
== arghx
[0] && y
== arghy
[0]) || tries
< 2) {
2179 rb
->snprintf(buf
, sizeof buf
, "(%d;%d) fail%d try%d", x
, y
, failures
, tries
);
2180 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2182 rb
->lcd_invertrect(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
, argh_size
, argh_size
);
2186 rb
->lcd_invertrect(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
, argh_size
, argh_size
);
2187 rb
->lcd_clearrect(arghx
[0] + FIELD_RECT_X
, arghy
[0] + FIELD_RECT_Y
, argh_size
, argh_size
);
2189 if (failures
> last_failures
) {
2190 rb
->button_get(true);
2192 last_failures
= failures
;
2199 static void test_worm_argh_collision_in_moves(void) {
2202 rb
->lcd_clear_display();
2203 init_worm(&worms
[0], 10, 20);
2209 set_worm_dir(&worms
[0], EAST
);
2210 for (i
= 0; i
< 20; i
++) {
2212 move_worm(&worms
[0]);
2213 draw_worm(&worms
[0]);
2214 if (worm_argh_collision_in_moves(&worms
[0], 0, 5)){
2217 rb
->snprintf(buf
, sizeof buf
, "in 5 moves hits: %d", hit_count
);
2218 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2221 if (hit_count
!= argh_size
+ 5) {
2222 rb
->button_get(true);
2225 #endif /* DEBUG_WORMLET */
2227 extern bool use_old_rect
;
2230 * These are additional functions required to set various wormlet settings
2231 * and to enable saving of settings and high score
2235 Sets the total number of worms, both human and machine
2237 bool set_worm_num_worms(void)
2240 static const struct opt_items num_worms_option
[3] = {
2246 ret
= rb
->set_option("Number Of Worms", &worm_count
,INT
, num_worms_option
, 3, NULL
);
2247 if (worm_count
< players
) {
2254 Sets the number of human players
2256 bool set_worm_num_players(void)
2259 static const struct opt_items num_players_option
[4] = {
2266 ret
= rb
->set_option("Number of Players", &players
, INT
,num_players_option
, 4, NULL
);
2267 if (players
> worm_count
) {
2268 worm_count
= players
;
2277 Sets the size of each argh block created
2279 bool set_worm_argh_size(void)
2281 static const struct opt_items argh_size_option
[11] = {
2295 return rb
->set_option("Argh Size", &argh_size
,INT
,argh_size_option
, 11, NULL
);
2299 Sets the amount a worm grows per food
2301 bool set_worm_food(void)
2303 static const struct opt_items worm_food_option
[11] = {
2316 return rb
->set_option("Worm Growth Per Food", &worm_food
,INT
,worm_food_option
, 11, NULL
);
2320 Sets the number of arghs created per food
2322 bool set_argh_per_food(void)
2324 static const struct opt_items argh_food_option
[5] = {
2331 return rb
->set_option("Arghs Per Food", &arghs_per_food
,INT
,argh_food_option
, 5, NULL
);
2335 Sets the number of ticks per move
2337 bool set_worm_speed(void)
2340 static const struct opt_items speed_option
[19] = {
2364 ret
= rb
->set_option("Worm Speed", &speed
,INT
,speed_option
, 19, NULL
);
2370 Sets the control style, which depends on the number of players,
2371 remote control existing, etc
2372 bool set_bool_options(char* string, bool* variable,
2373 char* yes_str, char* no_str, void (*function)(bool))
2375 bool set_worm_control_style(void)
2377 static const struct opt_items key1_option
[2] = {
2378 { "Remote Control", -1 },
2379 { "No Rem. Control", -1 },
2382 static const struct opt_items key2_option
[2] = {
2383 { "2 Key Control", -1 },
2384 { "4 Key COntrol", -1 },
2387 static const struct opt_items key3_option
[1] = {
2388 { "Out of Control", -1 },
2392 rb
->set_option("Control Style",&use_remote
,INT
, key1_option
, 2, NULL
);
2395 rb
->set_option("Control Style",&use_remote
,INT
, key2_option
, 2, NULL
);
2398 rb
->set_option("Control Style",&use_remote
,INT
, key3_option
, 1, NULL
);
2404 void default_settings(void)
2406 arghs_per_food
= ARGHS_PER_FOOD
;
2407 argh_size
= ARGH_SIZE
;
2408 food_size
= FOOD_SIZE
;
2410 worm_food
= WORM_PER_FOOD
;
2412 worm_count
= MAX_WORMS
;
2418 Launches the wormlet game
2420 bool launch_wormlet(void)
2422 int game_result
= 1;
2424 rb
->lcd_clear_display();
2426 /* Turn off backlight timeout */
2427 backlight_force_on(rb
); /* backlight control in lib/helper.c */
2429 /* start the game */
2430 while (game_result
== 1)
2431 game_result
= run();
2433 switch (game_result
)
2436 /* Turn on backlight timeout (revert to settings) */
2437 backlight_use_settings(rb
); /* backlight control in lib/helper.c */
2444 /* End of settings/changes etc */
2449 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
2459 configfile_init(rb
);
2460 if (configfile_load(SETTINGS_FILENAME
, config
,
2461 sizeof(config
)/sizeof(*config
),
2462 SETTINGS_MIN_VERSION
) < 0)
2464 /* If the loading failed, save a new config file (as the disk is
2465 already spinning) */
2466 configfile_save(SETTINGS_FILENAME
, config
,
2467 sizeof(config
)/sizeof(*config
),
2471 #ifdef HAVE_LCD_COLOR
2472 rb
->lcd_set_foreground(COLOR_FG
);
2473 rb
->lcd_set_background(COLOR_BG
);
2477 rb
->lcd_set_backdrop(NULL
);
2480 #ifdef DEBUG_WORMLET
2482 test_worm_argh_collision_in_moves();
2484 test_worm_food_collision();
2485 test_worm_argh_collision();
2486 test_specific_worm_collision();
2491 static const struct opt_items noyes
[2] = {
2496 static const struct opt_items num_worms_option
[3] = {
2503 static const struct opt_items num_players_option
[4] = {
2505 static const struct opt_items num_players_option
[2] = {
2515 static const struct opt_items argh_size_option
[9] = {
2527 static const struct opt_items food_size_option
[9] = {
2539 static const struct opt_items worm_food_option
[16] = {
2558 static const struct opt_items argh_food_option
[9] = {
2570 static const struct opt_items speed_option
[21] = {
2594 static const struct opt_items remoteonly_option
[1] = {
2595 { "Remote Control", -1 }
2598 static const struct opt_items key24_option
[2] = {
2599 { "4 Key Control", -1 },
2600 { "2 Key Control", -1 }
2604 static const struct opt_items remote_option
[2] = {
2605 { "Remote Control", -1 },
2606 { "No Rem. Control", -1 }
2609 static const struct opt_items key2_option
[1] = {
2610 { "2 Key Control", -1 }
2614 static const struct opt_items nokey_option
[1] = {
2615 { "Out of Control", -1 }
2618 MENUITEM_STRINGLIST(menu
, "Wormlet Menu", NULL
, "Play Wormlet!",
2619 "Number of Worms", "Number of Players", "Control Style",
2620 "Worm Growth Per Food","Worm Speed","Arghs Per Food",
2621 "Argh Size","Food Size","Revert to Default Settings",
2624 rb
->button_clear_queue();
2626 while (!menu_quit
) {
2627 switch(rb
->do_menu(&menu
, &result
, NULL
, false))
2630 rb
->lcd_setfont(FONT_SYSFIXED
);
2634 new_setting
= worm_count
- 1;
2635 rb
->set_option("Number of Worms", &new_setting
, INT
, num_worms_option
, 3, NULL
);
2636 if (new_setting
!= worm_count
)
2637 worm_count
= new_setting
+ 1;
2638 if (worm_count
< players
) {
2639 worm_count
= players
;
2643 new_setting
= players
;
2645 rb
->set_option("Number of Players", &new_setting
, INT
, num_players_option
, 4, NULL
);
2647 rb
->set_option("Number of Players", &new_setting
, INT
, num_players_option
, 2, NULL
);
2649 if (new_setting
!= players
)
2650 players
= new_setting
;
2651 if (players
> worm_count
) {
2652 worm_count
= players
;
2659 new_setting
= use_remote
;
2662 rb
->set_option("Control Style",&new_setting
,INT
, nokey_option
, 1, NULL
);
2665 rb
->set_option("Control Style",&new_setting
,INT
, key24_option
, 2, NULL
);
2669 rb
->set_option("Control Style",&new_setting
,INT
, remote_option
, 2, NULL
);
2671 rb
->set_option("Control Style",&new_setting
,INT
, key2_option
, 1, NULL
);
2675 rb
->set_option("Control Style",&new_setting
,INT
, remoteonly_option
, 1, NULL
);
2678 if (new_setting
!= use_remote
)
2679 use_remote
= new_setting
;
2682 new_setting
= worm_food
;
2683 rb
->set_option("Worm Growth Per Food", &new_setting
,INT
,worm_food_option
, 16, NULL
);
2684 if (new_setting
!= worm_food
)
2685 worm_food
= new_setting
;
2688 new_setting
= speed
;
2689 new_setting
= 20 - new_setting
;
2690 rb
->set_option("Worm Speed", &new_setting
,INT
,speed_option
, 21, NULL
);
2691 new_setting
= 20 - new_setting
;
2692 if (new_setting
!= speed
)
2693 speed
= new_setting
;
2696 new_setting
= arghs_per_food
;
2697 rb
->set_option("Arghs Per Food", &new_setting
,INT
,argh_food_option
, 9, NULL
);
2698 if (new_setting
!= arghs_per_food
)
2699 arghs_per_food
= new_setting
;
2702 new_setting
= argh_size
-2;
2703 rb
->set_option("Argh Size", &new_setting
,INT
,argh_size_option
, 9, NULL
);
2704 if (new_setting
!= argh_size
)
2705 argh_size
= new_setting
+2;
2708 new_setting
= food_size
-2;
2709 rb
->set_option("Food Size", &new_setting
,INT
,food_size_option
, 9, NULL
);
2710 if (new_setting
!= food_size
)
2711 food_size
= new_setting
+2;
2715 rb
->set_option("Reset Settings?", &new_setting
,INT
, noyes
, 2, NULL
);
2716 if (new_setting
== 1)
2725 configfile_save(SETTINGS_FILENAME
, config
,
2726 sizeof(config
)/sizeof(*config
),