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 #define PLAYERS_TEXT "UP/DN"
67 #define WORMS_TEXT "L/R"
68 #define KEY_CONTROL_TEXT "F1"
70 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
71 #define BTN_DIR_UP BUTTON_UP
72 #define BTN_DIR_DOWN BUTTON_DOWN
73 #define BTN_DIR_LEFT BUTTON_LEFT
74 #define BTN_DIR_RIGHT BUTTON_RIGHT
75 #define BTN_PLAYER2_DIR1 BUTTON_F2
76 #define BTN_PLAYER2_DIR2 BUTTON_F3
77 #define BTN_STARTPAUSE BUTTON_SELECT
78 #define BTN_QUIT BUTTON_OFF
79 #define BTN_STOPRESET BUTTON_ON
80 #define BTN_TOGGLE_KEYS BUTTON_F1
82 #define PLAYERS_TEXT "UP/DN"
83 #define WORMS_TEXT "L/R"
84 #define KEY_CONTROL_TEXT "F1"
86 #elif (CONFIG_KEYPAD == ONDIO_PAD)
87 #define BTN_DIR_UP BUTTON_UP
88 #define BTN_DIR_DOWN BUTTON_DOWN
89 #define BTN_DIR_LEFT BUTTON_LEFT
90 #define BTN_DIR_RIGHT BUTTON_RIGHT
91 #define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL)
92 #define BTN_QUIT (BUTTON_OFF|BUTTON_REL)
93 #define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU)
95 #define PLAYERS_TEXT "UP/DN"
96 #define WORMS_TEXT "L/R"
98 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
99 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
101 #define BTN_DIR_UP BUTTON_MENU
102 #define BTN_DIR_DOWN BUTTON_PLAY
103 #define BTN_DIR_LEFT BUTTON_LEFT
104 #define BTN_DIR_RIGHT BUTTON_RIGHT
105 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
106 #define BTN_QUIT (BUTTON_SELECT|BUTTON_MENU)
107 #define BTN_STOPRESET (BUTTON_SELECT|BUTTON_PLAY)
109 #define PLAYERS_TEXT "Menu/Play"
110 #define WORMS_TEXT "Left/Right"
112 #elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
114 #define BTN_DIR_UP BUTTON_UP
115 #define BTN_DIR_DOWN BUTTON_DOWN
116 #define BTN_DIR_LEFT BUTTON_LEFT
117 #define BTN_DIR_RIGHT BUTTON_RIGHT
118 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
119 #define BTN_QUIT BUTTON_OFF
120 #define BTN_STOPRESET BUTTON_ON
122 #define BTN_RC_QUIT BUTTON_RC_STOP
124 #define PLAYERS_TEXT "Up/Down"
125 #define WORMS_TEXT "Left/Right"
127 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
129 #define BTN_DIR_UP BUTTON_UP
130 #define BTN_DIR_DOWN BUTTON_DOWN
131 #define BTN_DIR_LEFT BUTTON_LEFT
132 #define BTN_DIR_RIGHT BUTTON_RIGHT
133 #define BTN_STARTPAUSE BUTTON_PLAY
134 #define BTN_QUIT BUTTON_POWER
135 #define BTN_STOPRESET BUTTON_REC
137 #define PLAYERS_TEXT "Up/Down"
138 #define WORMS_TEXT "Left/Right"
140 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
142 #define BTN_DIR_UP BUTTON_UP
143 #define BTN_DIR_DOWN BUTTON_DOWN
144 #define BTN_DIR_LEFT BUTTON_LEFT
145 #define BTN_DIR_RIGHT BUTTON_RIGHT
146 #define BTN_STARTPAUSE BUTTON_SELECT
147 #define BTN_QUIT BUTTON_POWER
148 #define BTN_STOPRESET BUTTON_A
150 #define PLAYERS_TEXT "Up/Down"
151 #define WORMS_TEXT "Left/Right"
154 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
155 (CONFIG_KEYPAD == SANSA_C200_PAD)
157 #define BTN_DIR_UP BUTTON_UP
158 #define BTN_DIR_DOWN BUTTON_DOWN
159 #define BTN_DIR_LEFT BUTTON_LEFT
160 #define BTN_DIR_RIGHT BUTTON_RIGHT
161 #define BTN_STARTPAUSE BUTTON_SELECT
162 #define BTN_QUIT BUTTON_POWER
163 #define BTN_STOPRESET BUTTON_REC
165 #define PLAYERS_TEXT "Up/Down"
166 #define WORMS_TEXT "Left/Right"
168 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
170 #define BTN_DIR_UP BUTTON_UP
171 #define BTN_DIR_DOWN BUTTON_DOWN
172 #define BTN_DIR_LEFT BUTTON_LEFT
173 #define BTN_DIR_RIGHT BUTTON_RIGHT
174 #define BTN_STARTPAUSE BUTTON_SELECT
175 #define BTN_QUIT BUTTON_POWER
176 #define BTN_STOPRESET BUTTON_HOME
178 #define PLAYERS_TEXT "Up/Down"
179 #define WORMS_TEXT "Left/Right"
181 #elif (CONFIG_KEYPAD == SANSA_FUZE_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_HOME|BUTTON_REPEAT)
189 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
191 #define PLAYERS_TEXT "Up/Down"
192 #define WORMS_TEXT "Left/Right"
194 #elif (CONFIG_KEYPAD == SANSA_M200_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 | BUTTON_REL)
201 #define BTN_QUIT BUTTON_POWER
202 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
204 #define PLAYERS_TEXT "Up/Down"
205 #define WORMS_TEXT "Left/Right"
207 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
209 #define BTN_DIR_UP BUTTON_SCROLL_UP
210 #define BTN_DIR_DOWN BUTTON_SCROLL_DOWN
211 #define BTN_DIR_LEFT BUTTON_LEFT
212 #define BTN_DIR_RIGHT BUTTON_RIGHT
213 #define BTN_STARTPAUSE BUTTON_PLAY
214 #define BTN_QUIT BUTTON_POWER
215 #define BTN_STOPRESET BUTTON_REW
217 #define PLAYERS_TEXT "Up/Down"
218 #define WORMS_TEXT "Left/Right"
220 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
222 #define BTN_DIR_UP BUTTON_UP
223 #define BTN_DIR_DOWN BUTTON_DOWN
224 #define BTN_DIR_LEFT BUTTON_LEFT
225 #define BTN_DIR_RIGHT BUTTON_RIGHT
226 #define BTN_STARTPAUSE BUTTON_SELECT
227 #define BTN_QUIT BUTTON_BACK
228 #define BTN_STOPRESET BUTTON_MENU
230 #define PLAYERS_TEXT "Up/Down"
231 #define WORMS_TEXT "Left/Right"
233 #elif (CONFIG_KEYPAD == MROBE100_PAD)
235 #define BTN_DIR_UP BUTTON_UP
236 #define BTN_DIR_DOWN BUTTON_DOWN
237 #define BTN_DIR_LEFT BUTTON_LEFT
238 #define BTN_DIR_RIGHT BUTTON_RIGHT
239 #define BTN_STARTPAUSE BUTTON_SELECT
240 #define BTN_QUIT BUTTON_POWER
241 #define BTN_STOPRESET BUTTON_DISPLAY
243 #define PLAYERS_TEXT "Up/Down"
244 #define WORMS_TEXT "Left/Right"
246 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
248 #define BTN_DIR_UP BUTTON_RC_VOL_UP
249 #define BTN_DIR_DOWN BUTTON_RC_VOL_DOWN
250 #define BTN_DIR_LEFT BUTTON_RC_REW
251 #define BTN_DIR_RIGHT BUTTON_RC_FF
252 #define BTN_STARTPAUSE BUTTON_RC_PLAY
253 #define BTN_QUIT BUTTON_RC_REC
254 #define BTN_STOPRESET BUTTON_RC_MODE
256 #define PLAYERS_TEXT "VOL UP/DN"
257 #define WORMS_TEXT "REW/FF"
259 #elif (CONFIG_KEYPAD == COWOND2_PAD)
261 #define BTN_QUIT BUTTON_POWER
263 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
265 #define BTN_DIR_UP BUTTON_UP
266 #define BTN_DIR_DOWN BUTTON_DOWN
267 #define BTN_DIR_LEFT BUTTON_LEFT
268 #define BTN_DIR_RIGHT BUTTON_RIGHT
269 #define BTN_STARTPAUSE BUTTON_PLAY
270 #define BTN_QUIT BUTTON_BACK
271 #define BTN_STOPRESET BUTTON_MENU
273 #define PLAYERS_TEXT "Up/Down"
274 #define WORMS_TEXT "Left/Right"
276 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
278 #define BTN_DIR_UP BUTTON_UP
279 #define BTN_DIR_DOWN BUTTON_DOWN
280 #define BTN_DIR_LEFT BUTTON_LEFT
281 #define BTN_DIR_RIGHT BUTTON_RIGHT
282 #define BTN_STARTPAUSE BUTTON_MENU
283 #define BTN_QUIT BUTTON_POWER
284 #define BTN_STOPRESET BUTTON_VIEW
286 #define PLAYERS_TEXT "Up/Down"
287 #define WORMS_TEXT "Left/Right"
289 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || CONFIG_KEYPAD == MROBE500_PAD
291 #define BTN_QUIT BUTTON_POWER
294 #error No keymap defined!
297 #ifdef HAVE_TOUCHSCREEN
299 #define BTN_DIR_UP BUTTON_TOPMIDDLE
302 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
305 #define BTN_DIR_LEFT BUTTON_MIDLEFT
307 #ifndef BTN_DIR_RIGHT
308 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
310 #ifndef BTN_STARTPAUSE
311 #define BTN_STARTPAUSE BUTTON_CENTER
314 #define BTN_QUIT BUTTON_TOPLEFT
316 #ifndef BTN_STOPRESET
317 #define BTN_STOPRESET BUTTON_TOPRIGHT
321 #define PLAYERS_TEXT "Up/Down"
324 #define WORMS_TEXT "Left/Right"
329 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
333 #define MAX_WORM_SEGMENTS 128
334 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 64)
338 #define MAX_WORM_SEGMENTS 128
339 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
343 #define MAX_WORM_SEGMENTS 128
344 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
348 #define MAX_WORM_SEGMENTS 128
349 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
353 #define MAX_WORM_SEGMENTS 128
354 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
358 #define MAX_WORM_SEGMENTS 128
359 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
363 #define MAX_WORM_SEGMENTS 256
364 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
368 #define MAX_WORM_SEGMENTS 256
369 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
373 #define MAX_WORM_SEGMENTS 512
374 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
378 #define MAX_WORM_SEGMENTS 512
379 #elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240)
383 #define MAX_WORM_SEGMENTS 512
384 #elif (LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400))
388 #define MAX_WORM_SEGMENTS 512
391 #ifdef HAVE_LCD_COLOR
392 #define COLOR_WORM LCD_RGBPACK(80, 40, 0)
393 #define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
394 #define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
395 #define COLOR_FG LCD_RGBPACK(0, 0, 0)
396 #define COLOR_BG LCD_RGBPACK(181, 199, 231)
400 * All the properties that a worm has.
403 /* The worm is stored in a ring of xy coordinates */
404 int x
[MAX_WORM_SEGMENTS
];
405 int y
[MAX_WORM_SEGMENTS
];
407 int head
; /* index of the head within the buffer */
408 int tail
; /* index of the tail within the buffer */
409 int growing
; /* number of cyles the worm still keeps growing */
410 bool alive
; /* the worms living state */
412 /* direction vector in which the worm moves */
413 int dirx
; /* only values -1 0 1 allowed */
414 int diry
; /* only values -1 0 1 allowed */
416 /* this method is used to fetch the direction the user
417 has selected. It can be one of the values
418 human_player1, human_player2, remote_player, virtual_player.
419 All these values are fuctions, that can change the direction
421 void (*fetch_worm_direction
)(struct worm
*w
);
424 /* stores the highscore - besides it was scored by a virtual player */
425 static int highscore
;
427 #define MAX_FOOD 5 /* maximal number of food items */
429 /* The arrays store the food coordinates */
430 static int foodx
[MAX_FOOD
];
431 static int foody
[MAX_FOOD
];
433 #define MAX_ARGH 100 /* maximal number of argh items */
434 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
436 /* The arrays store the argh coordinates */
437 static int arghx
[MAX_ARGH
];
438 static int arghy
[MAX_ARGH
];
440 /* the number of arghs that are currently in use */
441 static int argh_count
;
443 /* the number of arghs per food, settable by user */
444 static int arghs_per_food
= ARGHS_PER_FOOD
;
445 /* the size of the argh, settable by user */
446 static int argh_size
= ARGH_SIZE
;
447 /* the size of the food, settable by user */
448 static int food_size
= FOOD_SIZE
;
449 /* the speed of the worm, settable by user */
450 static int speed
= SPEED
;
451 /* the amount a worm grows by eating a food, settable by user */
452 static int worm_food
= WORM_PER_FOOD
;
454 /* End additional variables */
457 /* just a buffer used for debug output */
458 static char debugout
[15];
461 /* the number of active worms (dead or alive) */
462 static int worm_count
= MAX_WORMS
;
464 /* in multiplayer mode: en- / disables the remote worm control
465 in singleplayer mode: toggles 4 / 2 button worm control */
466 static bool use_remote
= false;
468 /* return values of check_collision */
469 #define COLLISION_NONE 0
470 #define COLLISION_WORM 1
471 #define COLLISION_FOOD 2
472 #define COLLISION_ARGH 3
473 #define COLLISION_FIELD 4
475 /* constants for use as directions.
476 Note that the values are ordered clockwise.
477 Thus increasing / decreasing the values
478 is equivalent to right / left turns. */
484 /* direction of human player 1 */
485 static int player1_dir
= EAST
;
486 /* direction of human player 2 */
487 static int player2_dir
= EAST
;
488 /* direction of human player 3 */
489 static int player3_dir
= EAST
;
491 /* the number of (human) players that currently
493 static int players
= 1;
495 #define SETTINGS_VERSION 1
496 #define SETTINGS_MIN_VERSION 1
497 #define SETTINGS_FILENAME "wormlet.cfg"
499 static struct configdata config
[] =
501 {TYPE_INT
, 0, 1024, { .int_p
= &highscore
}, "highscore", NULL
},
502 {TYPE_INT
, 0, 15, { .int_p
= &arghs_per_food
}, "arghs per food", NULL
},
503 {TYPE_INT
, 0, 15, { .int_p
= &argh_size
}, "argh size", NULL
},
504 {TYPE_INT
, 0, 15, { .int_p
= &food_size
}, "food size", NULL
},
505 {TYPE_INT
, 0, 3, { .int_p
= &players
}, "players", NULL
},
506 {TYPE_INT
, 0, 3, { .int_p
= &worm_count
}, "worms", NULL
},
507 {TYPE_INT
, 0, 20, { .int_p
= &speed
}, "speed", NULL
},
508 {TYPE_INT
, 0, 15, { .int_p
= &worm_food
}, "Worm Growth Per Food", NULL
}
512 static void set_debug_out(char *str
){
513 strcpy(debugout
, str
);
518 * Returns the direction id in which the worm
519 * currently is creeping.
520 * @param struct worm *w The worm that is to be investigated.
521 * w Must not be null.
522 * @return int A value 0 <= value < 4
523 * Note the predefined constants NORTH, SOUTH, EAST, WEST
525 static int get_worm_dir(struct worm
*w
) {
544 * Set the direction of the specified worm with a direction id.
545 * Increasing the value by 1 means to turn the worm direction
546 * to right by 90 degree.
547 * @param struct worm *w The worm that is to be altered. w Must not be null.
548 * @param int dir The new direction in which the worm is to creep.
549 * dir must be 0 <= dir < 4. Use predefined constants
550 * NORTH, SOUTH, EAST, WEST
552 static void set_worm_dir(struct worm
*w
, int dir
) {
574 * Returns the current length of the worm array. This
575 * is also a value for the number of bends that are in the worm.
576 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
578 static int get_worm_array_length(struct worm
*w
) {
579 /* initial simple calculation will be overwritten if wrong. */
580 int retVal
= w
->head
- w
->tail
;
582 /* if the worm 'crosses' the boundaries of the ringbuffer */
584 retVal
= w
->head
+ MAX_WORM_SEGMENTS
- w
->tail
;
591 * Returns the score the specified worm. The score is the length
593 * @param struct worm *w The worm that is to be investigated.
594 * w must not be null.
595 * @return int The length of the worm (>= 0).
597 static int get_score(struct worm
*w
) {
599 int length
= get_worm_array_length(w
);
601 for (i
= 0; i
< length
; i
++) {
603 /* The iteration iterates the length of the worm.
604 Here's the conversion to the true indices within the worm arrays. */
605 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
606 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
607 int startx
= w
->x
[linestart
];
608 int starty
= w
->y
[linestart
];
609 int endx
= w
->x
[lineend
];
610 int endy
= w
->y
[lineend
];
612 int minimum
, maximum
;
614 if (startx
== endx
) {
615 minimum
= MIN(starty
, endy
);
616 maximum
= MAX(starty
, endy
);
618 minimum
= MIN(startx
, endx
);
619 maximum
= MAX(startx
, endx
);
621 retval
+= abs(maximum
- minimum
);
627 * Determines wether the line specified by startx, starty, endx, endy intersects
628 * the rectangle specified by x, y, width, height. Note that the line must be exactly
629 * horizontal or vertical (startx == endx or starty == endy).
630 * @param int startx The x coordinate of the start point of the line.
631 * @param int starty The y coordinate of the start point of the line.
632 * @param int endx The x coordinate of the end point of the line.
633 * @param int endy The y coordinate of the end point of the line.
634 * @param int x The x coordinate of the top left corner of the rectangle.
635 * @param int y The y coordinate of the top left corner of the rectangle.
636 * @param int width The width of the rectangle.
637 * @param int height The height of the rectangle.
638 * @return bool Returns true if the specified line intersects with the recangle.
640 static bool line_in_rect(int startx
, int starty
, int endx
, int endy
, int x
, int y
, int width
, int height
) {
642 int simple
, simplemin
, simplemax
;
643 int compa
, compb
, compmin
, compmax
;
645 if (startx
== endx
) {
648 simplemax
= x
+ width
;
653 compmax
= y
+ height
;
657 simplemax
= y
+ height
;
666 compa
= MIN(compa
, compb
);
667 compb
= MAX(temp
, compb
);
669 if (simplemin
<= simple
&& simple
<= simplemax
) {
670 if ((compmin
<= compa
&& compa
<= compmax
) ||
671 (compmin
<= compb
&& compb
<= compmax
) ||
672 (compa
<= compmin
&& compb
>= compmax
)) {
680 * Tests wether the specified worm intersects with the rect.
681 * @param struct worm *w The worm to be investigated
682 * @param int x The x coordinate of the top left corner of the rect
683 * @param int y The y coordinate of the top left corner of the rect
684 * @param int widht The width of the rect
685 * @param int height The height of the rect
686 * @return bool Returns true if the worm intersects with the rect
688 static bool worm_in_rect(struct worm
*w
, int x
, int y
, int width
, int height
) {
692 /* get_worm_array_length is expensive -> buffer the value */
693 int wormLength
= get_worm_array_length(w
);
696 /* test each entry that is part of the worm */
697 for (i
= 0; i
< wormLength
&& retval
== false; i
++) {
699 /* The iteration iterates the length of the worm.
700 Here's the conversion to the true indices within the worm arrays. */
701 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
702 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
703 int startx
= w
->x
[linestart
];
704 int starty
= w
->y
[linestart
];
705 int endx
= w
->x
[lineend
];
706 int endy
= w
->y
[lineend
];
708 retval
= line_in_rect(startx
, starty
, endx
, endy
, x
, y
, width
, height
);
715 * Checks wether a specific food in the food arrays is at the
716 * specified coordinates.
717 * @param int foodIndex The index of the food in the food arrays
718 * @param int x the x coordinate.
719 * @param int y the y coordinate.
720 * @return Returns true if the coordinate hits the food specified by
723 static bool specific_food_collision(int foodIndex
, int x
, int y
) {
725 if (x
>= foodx
[foodIndex
] &&
726 x
< foodx
[foodIndex
] + food_size
&&
727 y
>= foody
[foodIndex
] &&
728 y
< foody
[foodIndex
] + food_size
) {
736 * Returns the index of the food that is at the
737 * given coordinates. If no food is at the coordinates
739 * @return int -1 <= value < MAX_FOOD
741 static int food_collision(int x
, int y
) {
744 for (i
= 0; i
< MAX_FOOD
; i
++) {
745 if (specific_food_collision(i
, x
, y
)) {
754 * Checks wether a specific argh in the argh arrays is at the
755 * specified coordinates.
756 * @param int arghIndex The index of the argh in the argh arrays
757 * @param int x the x coordinate.
758 * @param int y the y coordinate.
759 * @return Returns true if the coordinate hits the argh specified by
762 static bool specific_argh_collision(int arghIndex
, int x
, int y
) {
764 if ( x
>= arghx
[arghIndex
] &&
765 y
>= arghy
[arghIndex
] &&
766 x
< arghx
[arghIndex
] + argh_size
&&
767 y
< arghy
[arghIndex
] + argh_size
)
776 * Returns the index of the argh that is at the
777 * given coordinates. If no argh is at the coordinates
779 * @param int x The x coordinate.
780 * @param int y The y coordinate.
781 * @return int -1 <= value < argh_count <= MAX_ARGH
783 static int argh_collision(int x
, int y
) {
787 /* search for the argh that has the specified coords */
788 for (i
= 0; i
< argh_count
; i
++) {
789 if (specific_argh_collision(i
, x
, y
)) {
798 * Checks wether the worm collides with the food at the specfied food-arrays.
799 * @param int foodIndex The index of the food in the arrays. Ensure the value is
800 * 0 <= foodIndex <= MAX_FOOD
801 * @return Returns true if the worm collides with the specified food.
803 static bool worm_food_collision(struct worm
*w
, int foodIndex
)
807 retVal
= worm_in_rect(w
, foodx
[foodIndex
], foody
[foodIndex
],
808 food_size
- 1, food_size
- 1);
814 * Returns true if the worm hits the argh within the next moves (unless
815 * the worm changes it's direction).
816 * @param struct worm *w - The worm to investigate
817 * @param int argh_idx - The index of the argh
818 * @param int moves - The number of moves that are considered.
819 * @return Returns false if the specified argh is not hit within the next
822 static bool worm_argh_collision_in_moves(struct worm
*w
, int argh_idx
, int moves
){
828 x2
= w
->x
[w
->head
] + moves
* w
->dirx
;
829 y2
= w
->y
[w
->head
] + moves
* w
->diry
;
831 retVal
= line_in_rect(x1
, y1
, x2
, y2
, arghx
[argh_idx
], arghy
[argh_idx
],
832 argh_size
, argh_size
);
837 * Checks wether the worm collides with the argh at the specfied argh-arrays.
838 * @param int arghIndex The index of the argh in the arrays.
839 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
840 * @return Returns true if the worm collides with the specified argh.
842 static bool worm_argh_collision(struct worm
*w
, int arghIndex
)
846 retVal
= worm_in_rect(w
, arghx
[arghIndex
], arghy
[arghIndex
],
847 argh_size
- 1, argh_size
- 1);
853 * Find new coordinates for the food stored in foodx[index], foody[index]
854 * that don't collide with any other food or argh
856 * Ensure that 0 <= index < MAX_FOOD.
858 static void make_food(int index
) {
862 bool collisionDetected
= false;
866 /* make coordinates for a new food so that
867 the entire food lies within the FIELD */
868 x
= rb
->rand() % (FIELD_RECT_WIDTH
- food_size
);
869 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- food_size
);
871 /* Ensure that the new food doesn't collide with any
872 existing foods or arghs.
873 If one or more corners of the new food hit any existing
874 argh or food a collision is detected.
877 food_collision(x
, y
) >= 0 ||
878 food_collision(x
, y
+ food_size
- 1) >= 0 ||
879 food_collision(x
+ food_size
- 1, y
) >= 0 ||
880 food_collision(x
+ food_size
- 1, y
+ food_size
- 1) >= 0 ||
881 argh_collision(x
, y
) >= 0 ||
882 argh_collision(x
, y
+ food_size
- 1) >= 0 ||
883 argh_collision(x
+ food_size
- 1, y
) >= 0 ||
884 argh_collision(x
+ food_size
- 1, y
+ food_size
- 1) >= 0;
886 /* use coordinates for further testing */
890 /* now test wether we accidently hit the worm with food ;) */
892 for (i
= 0; i
< worm_count
&& !collisionDetected
; i
++) {
893 collisionDetected
|= worm_food_collision(&worms
[i
], index
);
896 while (collisionDetected
);
901 * Clears a food from the lcd buffer.
902 * @param int index The index of the food arrays under which
903 * the coordinates of the desired food can be found. Ensure
904 * that the value is 0 <= index <= MAX_FOOD.
906 static void clear_food(int index
)
908 /* remove the old food from the screen */
909 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
910 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
,
911 foody
[index
] + FIELD_RECT_Y
,
912 food_size
, food_size
);
913 rb
->lcd_set_drawmode(DRMODE_SOLID
);
917 * Draws a food in the lcd buffer.
918 * @param int index The index of the food arrays under which
919 * the coordinates of the desired food can be found. Ensure
920 * that the value is 0 <= index <= MAX_FOOD.
922 static void draw_food(int index
)
924 /* draw the food object */
925 #ifdef HAVE_LCD_COLOR
926 rb
->lcd_set_foreground(COLOR_FOOD
);
928 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
,
929 foody
[index
] + FIELD_RECT_Y
,
930 food_size
, food_size
);
931 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
932 rb
->lcd_fillrect(foodx
[index
] + FIELD_RECT_X
+ 1,
933 foody
[index
] + FIELD_RECT_Y
+ 1,
934 food_size
- 2, food_size
- 2);
935 rb
->lcd_set_drawmode(DRMODE_SOLID
);
936 #ifdef HAVE_LCD_COLOR
937 rb
->lcd_set_foreground(COLOR_FG
);
942 * Find new coordinates for the argh stored in arghx[index], arghy[index]
943 * that don't collide with any other food or argh.
945 * Ensure that 0 <= index < argh_count < MAX_ARGH.
947 static void make_argh(int index
)
951 bool collisionDetected
= false;
955 /* make coordinates for a new argh so that
956 the entire food lies within the FIELD */
957 x
= rb
->rand() % (FIELD_RECT_WIDTH
- argh_size
);
958 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- argh_size
);
960 /* Ensure that the new argh doesn't intersect with any
961 existing foods or arghs.
962 If one or more corners of the new argh hit any existing
963 argh or food an intersection is detected.
966 food_collision(x
, y
) >= 0 ||
967 food_collision(x
, y
+ argh_size
- 1) >= 0 ||
968 food_collision(x
+ argh_size
- 1, y
) >= 0 ||
969 food_collision(x
+ argh_size
- 1, y
+ argh_size
- 1) >= 0 ||
970 argh_collision(x
, y
) >= 0 ||
971 argh_collision(x
, y
+ argh_size
- 1) >= 0 ||
972 argh_collision(x
+ argh_size
- 1, y
) >= 0 ||
973 argh_collision(x
+ argh_size
- 1, y
+ argh_size
- 1) >= 0;
975 /* use the candidate coordinates to make a real argh */
979 /* now test wether we accidently hit the worm with argh ;) */
980 for (i
= 0; i
< worm_count
&& !collisionDetected
; i
++) {
981 collisionDetected
|= worm_argh_collision(&worms
[i
], index
);
982 collisionDetected
|= worm_argh_collision_in_moves(&worms
[i
], index
,
986 while (collisionDetected
);
991 * Draws an argh in the lcd buffer.
992 * @param int index The index of the argh arrays under which
993 * the coordinates of the desired argh can be found. Ensure
994 * that the value is 0 <= index < argh_count <= MAX_ARGH.
996 static void draw_argh(int index
)
998 /* draw the new argh */
999 #ifdef HAVE_LCD_COLOR
1000 rb
->lcd_set_foreground(COLOR_ARGH
);
1002 rb
->lcd_fillrect(arghx
[index
] + FIELD_RECT_X
,
1003 arghy
[index
] + FIELD_RECT_Y
,
1004 argh_size
, argh_size
);
1005 #ifdef HAVE_LCD_COLOR
1006 rb
->lcd_set_foreground(COLOR_FG
);
1010 static void virtual_player(struct worm
*w
);
1012 * Initialzes the specified worm with INITIAL_WORM_LENGTH
1013 * and the tail at the specified position. The worm will
1014 * be initialized alive and creeping EAST.
1015 * @param struct worm *w The worm that is to be initialized
1016 * @param int x The x coordinate at which the tail of the worm starts.
1017 * x must be 0 <= x < FIELD_RECT_WIDTH.
1018 * @param int y The y coordinate at which the tail of the worm starts
1019 * y must be 0 <= y < FIELD_RECT_WIDTH.
1021 static void init_worm(struct worm
*w
, int x
, int y
){
1022 /* initialize the worm size */
1026 w
->x
[w
->head
] = x
+ 1;
1032 /* set the initial direction the worm creeps to */
1036 w
->growing
= INITIAL_WORM_LENGTH
- 1;
1038 w
->fetch_worm_direction
= virtual_player
;
1042 * Writes the direction that was stored for
1043 * human player 1 into the specified worm. This function
1044 * may be used to be stored in worm.fetch_worm_direction.
1046 * the direction is read from player1_dir.
1047 * @param struct worm *w - The worm of which the direction
1050 static void human_player1(struct worm
*w
) {
1051 set_worm_dir(w
, player1_dir
);
1055 * Writes the direction that was stored for
1056 * human player 2 into the specified worm. This function
1057 * may be used to be stored in worm.fetch_worm_direction.
1059 * the direction is read from player2_dir.
1060 * @param struct worm *w - The worm of which the direction
1063 static void human_player2(struct worm
*w
) {
1064 set_worm_dir(w
, player2_dir
);
1068 * Writes the direction that was stored for
1069 * human player using a remote control
1070 * into the specified worm. This function
1071 * may be used to be stored in worm.fetch_worm_direction.
1073 * the direction is read from player3_dir.
1074 * @param struct worm *w - The worm of which the direction
1077 static void remote_player(struct worm
*w
) {
1078 set_worm_dir(w
, player3_dir
);
1082 * Initializes the worm-, food- and argh-arrays, draws a frame,
1083 * makes some food and argh and display all that stuff.
1085 static void init_wormlet(void)
1089 for (i
= 0; i
< worm_count
; i
++) {
1090 /* Initialize all the worm coordinates to center. */
1091 int x
= (int)(FIELD_RECT_WIDTH
/ 2);
1092 int y
= (int)((FIELD_RECT_HEIGHT
- 20)/ 2) + i
* 10;
1094 init_worm(&worms
[i
], x
, y
);
1102 worms
[0].fetch_worm_direction
= human_player1
;
1107 worms
[1].fetch_worm_direction
= remote_player
;
1109 worms
[1].fetch_worm_direction
= human_player2
;
1114 worms
[2].fetch_worm_direction
= human_player2
;
1117 /* Needed when the game is restarted using BTN_STOPRESET */
1118 rb
->lcd_clear_display();
1120 /* make and display some food and argh */
1121 argh_count
= MAX_FOOD
;
1122 for (i
= 0; i
< MAX_FOOD
; i
++) {
1129 /* draw the game field */
1130 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
1131 rb
->lcd_fillrect(0, 0, FIELD_RECT_WIDTH
+ 2, FIELD_RECT_HEIGHT
+ 2);
1132 rb
->lcd_fillrect(1, 1, FIELD_RECT_WIDTH
, FIELD_RECT_HEIGHT
);
1133 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1135 /* make everything visible */
1141 * Move the worm one step further if it is alive.
1142 * The direction in which the worm moves is taken from dirx and diry.
1143 * move_worm decreases growing if > 0. While the worm is growing the tail
1144 * is left untouched.
1145 * @param struct worm *w The worm to move. w must not be NULL.
1147 static void move_worm(struct worm
*w
)
1150 /* determine the head point and its precessor */
1151 int headx
= w
->x
[w
->head
];
1152 int heady
= w
->y
[w
->head
];
1153 int prehead
= (w
->head
+ MAX_WORM_SEGMENTS
- 1) % MAX_WORM_SEGMENTS
;
1154 int preheadx
= w
->x
[prehead
];
1155 int preheady
= w
->y
[prehead
];
1157 /* determine the old direction */
1160 if (headx
== preheadx
) {
1162 olddiry
= (heady
> preheady
) ? 1 : -1;
1165 olddirx
= (headx
> preheadx
) ? 1 : -1;
1169 a change of direction means a new segment
1171 if (olddirx
!= w
->dirx
||
1172 olddiry
!= w
->diry
) {
1173 w
->head
= (w
->head
+ 1) % MAX_WORM_SEGMENTS
;
1176 /* new head position */
1177 w
->x
[w
->head
] = headx
+ w
->dirx
;
1178 w
->y
[w
->head
] = heady
+ w
->diry
;
1181 /* while the worm is growing no tail procession is necessary */
1182 if (w
->growing
> 0) {
1183 /* update the worms grow state */
1187 /* if the worm isn't growing the tail has to be dragged */
1189 /* index of the end of the tail segment */
1190 int tail_segment_end
= (w
->tail
+ 1) % MAX_WORM_SEGMENTS
;
1192 /* drag the end of the tail */
1193 /* only one coordinate has to be altered. Here it is
1194 determined which one */
1195 int dir
= 0; /* specifies wether the coord has to be in- or decreased */
1196 if (w
->x
[w
->tail
] == w
->x
[tail_segment_end
]) {
1197 dir
= (w
->y
[w
->tail
] - w
->y
[tail_segment_end
] < 0) ? 1 : -1;
1198 w
->y
[w
->tail
] += dir
;
1200 dir
= (w
->x
[w
->tail
] - w
->x
[tail_segment_end
] < 0) ? 1 : -1;
1201 w
->x
[w
->tail
] += dir
;
1204 /* when the tail has been dragged so far that it meets
1205 the next segment start the tail segment is obsolete and
1207 if (w
->x
[w
->tail
] == w
->x
[tail_segment_end
] &&
1208 w
->y
[w
->tail
] == w
->y
[tail_segment_end
]){
1210 /* drop the last tail point */
1211 w
->tail
= tail_segment_end
;
1218 * Draws the head and clears the tail of the worm in
1219 * the display buffer. lcd_update() is NOT called thus
1220 * the caller has to take care that the buffer is displayed.
1222 static void draw_worm(struct worm
*w
)
1224 #ifdef HAVE_LCD_COLOR
1225 rb
->lcd_set_foreground(COLOR_WORM
);
1227 /* draw the new head */
1228 int x
= w
->x
[w
->head
];
1229 int y
= w
->y
[w
->head
];
1230 if (x
>= 0 && x
< FIELD_RECT_WIDTH
&& y
>= 0 && y
< FIELD_RECT_HEIGHT
) {
1231 rb
->lcd_drawpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
1234 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1236 /* clear the space behind the worm */
1239 if (x
>= 0 && x
< FIELD_RECT_WIDTH
&& y
>= 0 && y
< FIELD_RECT_HEIGHT
) {
1240 rb
->lcd_drawpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
1242 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1243 #ifdef HAVE_LCD_COLOR
1244 rb
->lcd_set_foreground(COLOR_FG
);
1249 * Checks wether the coordinate is part of the worm. Returns
1250 * true if any part of the worm was hit - including the head.
1251 * @param x int The x coordinate
1252 * @param y int The y coordinate
1253 * @return int The index of the worm arrays that contain x, y.
1254 * Returns -1 if the coordinates are not part of the worm.
1256 static int specific_worm_collision(struct worm
*w
, int x
, int y
)
1260 /* get_worm_array_length is expensive -> buffer the value */
1261 int wormLength
= get_worm_array_length(w
);
1264 /* test each entry that is part of the worm */
1265 for (i
= 0; i
< wormLength
&& retVal
== -1; i
++) {
1267 /* The iteration iterates the length of the worm.
1268 Here's the conversion to the true indices within the worm arrays. */
1269 int linestart
= (w
->tail
+ i
) % MAX_WORM_SEGMENTS
;
1270 int lineend
= (linestart
+ 1) % MAX_WORM_SEGMENTS
;
1271 bool samex
= (w
->x
[linestart
] == x
) && (w
->x
[lineend
] == x
);
1272 bool samey
= (w
->y
[linestart
] == y
) && (w
->y
[lineend
] == y
);
1273 if (samex
|| samey
){
1274 int test
, min
, max
, tmp
;
1277 min
= w
->x
[linestart
];
1278 max
= w
->x
[lineend
];
1281 min
= w
->y
[linestart
];
1282 max
= w
->y
[lineend
];
1287 min
= MIN(min
, max
);
1288 max
= MAX(tmp
, max
);
1290 if (min
<= test
&& test
<= max
) {
1299 * Increases the length of the specified worm by marking
1300 * that it may grow by len pixels. Note that the worm has
1301 * to move to make the growing happen.
1302 * @param worm *w The worm that is to be altered.
1303 * @param int len A positive value specifying the amount of
1304 * pixels the worm may grow.
1306 static void add_growing(struct worm
*w
, int len
) {
1311 * Determins the worm that is at the coordinates x, y. The parameter
1312 * w is a switch parameter that changes the functionality of worm_collision.
1313 * If w is specified and x,y hits the head of w NULL is returned.
1314 * This is a useful way to determine wether the head of w hits
1315 * any worm but including itself but excluding its own head.
1316 * (It hits always its own head ;))
1317 * If w is set to NULL worm_collision returns any worm including all heads
1318 * that is at position of x,y.
1319 * @param struct worm *w The worm of which the head should be excluded in
1320 * the test. w may be set to NULL.
1321 * @param int x The x coordinate that is checked
1322 * @param int y The y coordinate that is checkec
1323 * @return struct worm* The worm that has been hit by x,y. If no worm
1324 * was at the position NULL is returned.
1326 static struct worm
* worm_collision(struct worm
*w
, int x
, int y
){
1327 struct worm
*retVal
= NULL
;
1329 for (i
= 0; (i
< worm_count
) && (retVal
== NULL
); i
++) {
1330 int collision_at
= specific_worm_collision(&worms
[i
], x
, y
);
1331 if (collision_at
!= -1) {
1332 if (!(w
== &worms
[i
] && collision_at
== w
->head
)){
1341 * Returns true if the head of the worm just has
1342 * crossed the field boundaries.
1343 * @return bool true if the worm just has wrapped.
1345 static bool field_collision(struct worm
*w
)
1347 bool retVal
= false;
1348 if ((w
->x
[w
->head
] >= FIELD_RECT_WIDTH
) ||
1349 (w
->y
[w
->head
] >= FIELD_RECT_HEIGHT
) ||
1350 (w
->x
[w
->head
] < 0) ||
1351 (w
->y
[w
->head
] < 0))
1360 * Returns true if the specified coordinates are within the
1361 * field specified by the FIELD_RECT_XXX constants.
1362 * @param int x The x coordinate of the point that is investigated
1363 * @param int y The y coordinate of the point that is investigated
1364 * @return bool Returns false if x,y specifies a point outside the
1367 static bool is_in_field_rect(int x
, int y
) {
1368 bool retVal
= false;
1369 retVal
= (x
>= 0 && x
< FIELD_RECT_WIDTH
&&
1370 y
>= 0 && y
< FIELD_RECT_HEIGHT
);
1375 * Checks and returns wether the head of the w
1376 * is colliding with something currently.
1377 * @return int One of the values:
1384 static int check_collision(struct worm
*w
)
1386 int retVal
= COLLISION_NONE
;
1388 if (worm_collision(w
, w
->x
[w
->head
], w
->y
[w
->head
]) != NULL
)
1389 retVal
= COLLISION_WORM
;
1391 if (food_collision(w
->x
[w
->head
], w
->y
[w
->head
]) >= 0)
1392 retVal
= COLLISION_FOOD
;
1394 if (argh_collision(w
->x
[w
->head
], w
->y
[w
->head
]) >= 0)
1395 retVal
= COLLISION_ARGH
;
1397 if (field_collision(w
))
1398 retVal
= COLLISION_FIELD
;
1404 * Returns the index of the food that is closest to the point
1405 * specified by x, y. This index may be used in the foodx and
1407 * @param int x The x coordinate of the point
1408 * @param int y The y coordinate of the point
1409 * @return int A value usable as index in foodx and foody.
1411 static int get_nearest_food(int x
, int y
){
1412 int nearestfood
= 0;
1413 int olddistance
= FIELD_RECT_WIDTH
+ FIELD_RECT_HEIGHT
;
1417 for (foodindex
= 0; foodindex
< MAX_FOOD
; foodindex
++) {
1419 deltax
= foodx
[foodindex
] - x
;
1420 deltay
= foody
[foodindex
] - y
;
1421 deltax
= deltax
> 0 ? deltax
: deltax
* (-1);
1422 deltay
= deltay
> 0 ? deltay
: deltay
* (-1);
1423 distance
= deltax
+ deltay
;
1425 if (distance
< olddistance
) {
1426 olddistance
= distance
;
1427 nearestfood
= foodindex
;
1434 * Returns wether the specified position is next to the worm
1435 * and in the direction the worm looks. Use this method to
1436 * test wether this position would be hit with the next move of
1437 * the worm unless the worm changes its direction.
1438 * @param struct worm *w - The worm to be investigated
1439 * @param int x - The x coordinate of the position to test.
1440 * @param int y - The y coordinate of the position to test.
1441 * @return Returns true if the worm will hit the position unless
1442 * it change its direction before the next move.
1444 static bool is_in_front_of_worm(struct worm
*w
, int x
, int y
) {
1445 bool infront
= false;
1446 int deltax
= x
- w
->x
[w
->head
];
1447 int deltay
= y
- w
->y
[w
->head
];
1450 infront
= (w
->diry
* deltay
) > 0;
1452 infront
= (w
->dirx
* deltax
) > 0;
1458 * Returns true if the worm will collide with the next move unless
1459 * it changes its direction.
1460 * @param struct worm *w - The worm to be investigated.
1461 * @return Returns true if the worm will collide with the next move
1462 * unless it changes its direction.
1464 static bool will_worm_collide(struct worm
*w
) {
1465 int x
= w
->x
[w
->head
] + w
->dirx
;
1466 int y
= w
->y
[w
->head
] + w
->diry
;
1467 bool retVal
= !is_in_field_rect(x
, y
);
1469 retVal
= (argh_collision(x
, y
) != -1);
1473 retVal
= (worm_collision(w
, x
, y
) != NULL
);
1480 * may be used to be stored in worm.fetch_worm_direction for
1481 * worms that are not controlled by humans but by artificial stupidity.
1482 * A direction is searched that doesn't lead to collision but to the nearest
1483 * food - but not very intelligent. The direction is written to the specified
1485 * @param struct worm *w - The worm of which the direction
1488 static void virtual_player(struct worm
*w
) {
1490 int plana
, planb
, planc
;
1491 /* find the next lunch */
1492 int nearestfood
= get_nearest_food(w
->x
[w
->head
], w
->y
[w
->head
]);
1494 /* determine in which direction it is */
1496 /* in front of me? */
1497 bool infront
= is_in_front_of_worm(w
, foodx
[nearestfood
], foody
[nearestfood
]);
1499 /* left right of me? */
1500 int olddir
= get_worm_dir(w
);
1501 set_worm_dir(w
, (olddir
+ 1) % 4);
1502 isright
= is_in_front_of_worm(w
, foodx
[nearestfood
], foody
[nearestfood
]);
1503 set_worm_dir(w
, olddir
);
1505 /* detect situation, set strategy */
1509 planb
= (olddir
+ 1) % 4;
1510 planc
= (olddir
+ 3) % 4;
1513 planb
= (olddir
+ 3) % 4;
1514 planc
= (olddir
+ 1) % 4;
1518 plana
= (olddir
+ 1) % 4;
1520 planc
= (olddir
+ 3) % 4;
1522 plana
= (olddir
+ 3) % 4;
1524 planc
= (olddir
+ 1) % 4;
1528 /* test for collision */
1529 set_worm_dir(w
, plana
);
1530 if (will_worm_collide(w
)){
1533 set_worm_dir(w
, planb
);
1535 /* test for collision */
1536 if (will_worm_collide(w
)) {
1539 set_worm_dir(w
, planc
);
1545 * prints out the score board with all the status information
1548 static void score_board(void)
1554 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1555 rb
->lcd_fillrect(FIELD_RECT_WIDTH
+ 2, 0, LCD_WIDTH
- FIELD_RECT_WIDTH
- 2, LCD_HEIGHT
);
1556 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1557 for (i
= 0; i
< worm_count
; i
++) {
1558 int score
= get_score(&worms
[i
]);
1561 if (worms
[i
].fetch_worm_direction
!= virtual_player
){
1562 if (highscore
< score
) {
1568 rb
->snprintf(buf
, sizeof (buf
),"Len:%d", score
);
1571 switch (check_collision(&worms
[i
])) {
1572 case COLLISION_NONE
:
1573 if (worms
[i
].growing
> 0)
1583 case COLLISION_WORM
:
1587 case COLLISION_FOOD
:
1591 case COLLISION_ARGH
:
1595 case COLLISION_FIELD
:
1599 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, y
, buf
);
1600 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, y
+8, buf2
);
1602 if (!worms
[i
].alive
){
1603 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
1604 rb
->lcd_fillrect(FIELD_RECT_WIDTH
+ 2, y
,
1605 LCD_WIDTH
- FIELD_RECT_WIDTH
- 2, 17);
1606 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1610 rb
->snprintf(buf
, sizeof(buf
), "Hs: %d", highscore
);
1611 #ifndef DEBUG_WORMLET
1612 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, LCD_HEIGHT
- 8, buf
);
1614 rb
->lcd_putsxy(FIELD_RECT_WIDTH
+ 3, LCD_HEIGHT
- 8, debugout
);
1619 * Checks for collisions of the worm and its environment and
1620 * takes appropriate actions like growing the worm or killing it.
1621 * @return bool Returns true if the worm is dead. Returns
1622 * false if the worm is healthy, up and creeping.
1624 static bool process_collisions(struct worm
*w
)
1628 w
->alive
&= !field_collision(w
);
1632 /* check if food was eaten */
1633 index
= food_collision(w
->x
[w
->head
], w
->y
[w
->head
]);
1641 for (i
= 0; i
< arghs_per_food
; i
++) {
1643 if (argh_count
> MAX_ARGH
)
1644 argh_count
= MAX_ARGH
;
1645 make_argh(argh_count
- 1);
1646 draw_argh(argh_count
- 1);
1649 add_growing(w
, worm_food
);
1654 /* check if argh was eaten */
1656 index
= argh_collision(w
->x
[w
->head
], w
->y
[w
->head
]);
1661 if (worm_collision(w
, w
->x
[w
->head
], w
->y
[w
->head
]) != NULL
) {
1671 * The main loop of the game.
1672 * @return bool Returns true if the game ended
1673 * with a dead worm. Returns false if the user
1674 * aborted the game manually.
1676 static int run(void)
1679 int wormDead
= false;
1680 bool paused
= false;
1682 /* ticks are counted to compensate speed variations */
1683 long cycle_start
= 0, cycle_end
= 0;
1684 #ifdef DEBUG_WORMLET
1685 int ticks_to_max_cycle_reset
= 20;
1690 /* initialize the board and so on */
1693 cycle_start
= *rb
->current_tick
;
1694 /* change the direction of the worm */
1698 long cycle_duration
=0;
1700 #ifdef HAS_BUTTON_HOLD
1701 if (rb
->button_hold())
1706 case BTN_STARTPAUSE
:
1711 return 1; /* restart game */
1719 return 2; /* back to menu */
1726 if (players
== 1 && !use_remote
) {
1727 player1_dir
= NORTH
;
1732 if (players
== 1 && !use_remote
) {
1733 player1_dir
= SOUTH
;
1738 if (players
!= 1 || use_remote
) {
1739 player1_dir
= (player1_dir
+ 3) % 4;
1746 if (players
!= 1 || use_remote
) {
1747 player1_dir
= (player1_dir
+ 1) % 4;
1754 case BTN_PLAYER2_DIR1
:
1755 player2_dir
= (player2_dir
+ 3) % 4;
1758 case BTN_PLAYER2_DIR2
:
1759 player2_dir
= (player2_dir
+ 1) % 4;
1765 player3_dir
= (player3_dir
+ 1) % 4;
1769 player3_dir
= (player3_dir
+ 3) % 4;
1775 for (i
= 0; i
< worm_count
; i
++) {
1776 worms
[i
].fetch_worm_direction(&worms
[i
]);
1780 for (i
= 0; i
< worm_count
; i
++){
1781 struct worm
*w
= &worms
[i
];
1783 wormDead
&= process_collisions(w
);
1788 if (button
== BTN_STOPRESET
) {
1792 /* here the wormlet game cycle ends
1793 thus the current tick is stored
1795 cycle_end
= *rb
->current_tick
;
1797 /* The duration of the game cycle */
1798 cycle_duration
= cycle_end
- cycle_start
;
1799 cycle_duration
= MAX(0, cycle_duration
);
1800 cycle_duration
= MIN(speed
-1, cycle_duration
);
1803 #ifdef DEBUG_WORMLET
1804 ticks_to_max_cycle_reset
--;
1805 if (ticks_to_max_cycle_reset
<= 0) {
1809 if (max_cycle
< cycle_duration
) {
1810 max_cycle
= cycle_duration
;
1811 ticks_to_max_cycle_reset
= 20;
1813 rb
->snprintf(buf
, sizeof buf
, "ticks %d", max_cycle
);
1817 /* adjust the number of ticks to wait for a button.
1818 This ensures that a complete game cycle including
1819 user input runs in constant time */
1820 button
= rb
->button_get_w_tmo(speed
- cycle_duration
);
1821 cycle_start
= *rb
->current_tick
;
1824 rb
->splash(HZ
*2, "Game Over!");
1826 return 2; /* back to menu */
1829 #ifdef DEBUG_WORMLET
1832 * Just a test routine that checks that worm_food_collision works
1833 * in some typical situations.
1835 static void test_worm_food_collision(void) {
1836 int collision_count
= 0;
1838 rb
->lcd_clear_display();
1839 init_worm(&worms
[0], 10, 10);
1840 add_growing(&worms
[0], 10);
1841 set_worm_dir(&worms
[0], EAST
);
1842 for (i
= 0; i
< 10; i
++) {
1843 move_worm(&worms
[0]);
1844 draw_worm(&worms
[0]);
1847 set_worm_dir(&worms
[0], SOUTH
);
1848 for (i
= 0; i
< 10; i
++) {
1849 move_worm(&worms
[0]);
1850 draw_worm(&worms
[0]);
1855 for (foody
[0] = 20; foody
[0] > 0; foody
[0] --) {
1858 draw_worm(&worms
[0]);
1860 collision
= worm_food_collision(&worms
[0], 0);
1864 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1865 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1868 if (collision_count
!= food_size
) {
1869 rb
->button_get(true);
1874 for (foodx
[0] = 30; foodx
[0] > 0; foodx
[0] --) {
1877 draw_worm(&worms
[0]);
1879 collision
= worm_food_collision(&worms
[0], 0);
1883 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1884 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1887 if (collision_count
!= food_size
* 2) {
1888 rb
->button_get(true);
1893 static bool expensive_worm_in_rect(struct worm
*w
, int rx
, int ry
, int rw
, int rh
){
1895 bool retVal
= false;
1896 for (x
= rx
; x
< rx
+ rw
; x
++){
1897 for (y
= ry
; y
< ry
+ rh
; y
++) {
1898 if (specific_worm_collision(w
, x
, y
) != -1) {
1906 static void test_worm_argh_collision(void) {
1909 int collision_count
= 0;
1910 rb
->lcd_clear_display();
1911 init_worm(&worms
[0], 10, 10);
1912 add_growing(&worms
[0], 40);
1913 for (dir
= 0; dir
< 4; dir
++) {
1914 set_worm_dir(&worms
[0], (EAST
+ dir
) % 4);
1915 for (i
= 0; i
< 10; i
++) {
1916 move_worm(&worms
[0]);
1917 draw_worm(&worms
[0]);
1922 for (arghy
[0] = 0; arghy
[0] < FIELD_RECT_HEIGHT
- argh_size
; arghy
[0]++){
1926 collision
= worm_argh_collision(&worms
[0], 0);
1930 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1931 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1934 if (collision_count
!= argh_size
* 2) {
1935 rb
->button_get(true);
1939 for (arghx
[0] = 0; arghx
[0] < FIELD_RECT_HEIGHT
- argh_size
; arghx
[0]++){
1943 collision
= worm_argh_collision(&worms
[0], 0);
1947 rb
->snprintf(buf
, sizeof buf
, "collisions: %d", collision_count
);
1948 rb
->lcd_putsxy(0, LCD_HEIGHT
-8, buf
);
1951 if (collision_count
!= argh_size
* 4) {
1952 rb
->button_get(true);
1956 static int testline_in_rect(void) {
1957 int testfailed
= -1;
1970 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1971 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1972 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1973 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1975 rb
->lcd_putsxy(0, 0, "failed 1");
1976 rb
->button_get(true);
1982 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1983 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1984 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1985 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1986 rb
->lcd_putsxy(0, 0, "failed 2");
1988 rb
->button_get(true);
1994 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
1995 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
1996 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
1997 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
1998 rb
->lcd_putsxy(0, 0, "failed 3");
2000 rb
->button_get(true);
2006 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2007 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2008 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2009 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2010 rb
->lcd_putsxy(0, 0, "failed 4");
2012 rb
->button_get(true);
2018 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2019 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2020 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2021 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2022 rb
->lcd_putsxy(0, 0, "failed 5");
2024 rb
->button_get(true);
2031 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2032 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2033 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2034 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2035 rb
->lcd_putsxy(0, 0, "failed 6");
2037 rb
->button_get(true);
2046 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2047 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2048 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2049 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2050 rb
->lcd_putsxy(0, 0, "failed 7");
2052 rb
->button_get(true);
2058 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2059 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2060 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2061 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2062 rb
->lcd_putsxy(0, 0, "failed 8");
2064 rb
->button_get(true);
2070 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2071 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2072 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2073 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2074 rb
->lcd_putsxy(0, 0, "failed 9");
2076 rb
->button_get(true);
2082 if (!line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2083 !line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2084 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2085 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2086 rb
->lcd_putsxy(0, 0, "failed 10");
2088 rb
->button_get(true);
2094 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2095 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2096 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2097 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2098 rb
->lcd_putsxy(0, 0, "failed 11");
2100 rb
->button_get(true);
2107 if (line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) ||
2108 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
)) {
2109 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2110 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2111 rb
->lcd_putsxy(0, 0, "failed 12");
2113 rb
->button_get(true);
2127 if (!(line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2128 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
))) {
2129 rb
->lcd_drawrect(rx
, ry
, rw
, rh
);
2130 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2131 rb
->lcd_putsxy(0, 0, "failed 13");
2133 rb
->button_get(true);
2147 if (!(line_in_rect(x1
, y1
, x2
, y2
, rx
, ry
, rw
, rh
) &&
2148 line_in_rect(x2
, y2
, x1
, y1
, rx
, ry
, rw
, rh
))) {
2149 rb
->lcd_drawline(x1
, y1
, x2
, y2
);
2150 rb
->lcd_invertrect(rx
, ry
, rw
, rh
);
2151 rb
->lcd_putsxy(0, 0, "failed 14");
2153 rb
->button_get(true);
2157 rb
->lcd_clear_display();
2163 * Just a test routine to test wether specific_worm_collision might work properly
2165 static int test_specific_worm_collision(void) {
2171 rb
->lcd_clear_display();
2172 init_worm(&worms
[0], 10, 20);
2173 add_growing(&worms
[0], 20 - INITIAL_WORM_LENGTH
);
2175 for (dir
= EAST
; dir
< EAST
+ 4; dir
++) {
2177 set_worm_dir(&worms
[0], dir
% 4);
2178 for (i
= 0; i
< 5; i
++) {
2179 if (!(dir
% 4 == NORTH
&& i
== 9)) {
2180 move_worm(&worms
[0]);
2181 draw_worm(&worms
[0]);
2186 for (y
= 15; y
< 30; y
++){
2187 for (x
= 5; x
< 20; x
++) {
2188 if (specific_worm_collision(&worms
[0], x
, y
) != -1) {
2191 rb
->lcd_invertpixel(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
);
2192 rb
->snprintf(buf
, sizeof buf
, "collisions %d", collisions
);
2193 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2197 if (collisions
!= 21) {
2198 rb
->button_get(true);
2203 static void test_make_argh(void){
2208 int last_failures
= 0;
2210 rb
->lcd_clear_display();
2213 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++) {
2214 init_worm(&worms
[worm_idx
], 10 + worm_idx
* 20, 20);
2215 add_growing(&worms
[worm_idx
], 40 - INITIAL_WORM_LENGTH
);
2218 for (dir
= EAST
; dir
< EAST
+ 4; dir
++) {
2219 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++) {
2220 set_worm_dir(&worms
[worm_idx
], dir
% 4);
2221 for (i
= 0; i
< 10; i
++) {
2222 if (!(dir
% 4 == NORTH
&& i
== 9)) {
2223 move_worm(&worms
[worm_idx
]);
2224 draw_worm(&worms
[worm_idx
]);
2232 for (seed
= 0; hit
< 20; seed
+= 2) {
2236 x
= rb
->rand() % (FIELD_RECT_WIDTH
- argh_size
);
2237 y
= rb
->rand() % (FIELD_RECT_HEIGHT
- argh_size
);
2239 for (worm_idx
= 0; worm_idx
< worm_count
; worm_idx
++){
2240 if (expensive_worm_in_rect(&worms
[worm_idx
], x
, y
, argh_size
, argh_size
)) {
2244 tries
= make_argh(0);
2245 if ((x
== arghx
[0] && y
== arghy
[0]) || tries
< 2) {
2249 rb
->snprintf(buf
, sizeof buf
, "(%d;%d) fail%d try%d", x
, y
, failures
, tries
);
2250 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2252 rb
->lcd_invertrect(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
, argh_size
, argh_size
);
2256 rb
->lcd_invertrect(x
+ FIELD_RECT_X
, y
+ FIELD_RECT_Y
, argh_size
, argh_size
);
2257 rb
->lcd_clearrect(arghx
[0] + FIELD_RECT_X
, arghy
[0] + FIELD_RECT_Y
, argh_size
, argh_size
);
2259 if (failures
> last_failures
) {
2260 rb
->button_get(true);
2262 last_failures
= failures
;
2269 static void test_worm_argh_collision_in_moves(void) {
2272 rb
->lcd_clear_display();
2273 init_worm(&worms
[0], 10, 20);
2279 set_worm_dir(&worms
[0], EAST
);
2280 for (i
= 0; i
< 20; i
++) {
2282 move_worm(&worms
[0]);
2283 draw_worm(&worms
[0]);
2284 if (worm_argh_collision_in_moves(&worms
[0], 0, 5)){
2287 rb
->snprintf(buf
, sizeof buf
, "in 5 moves hits: %d", hit_count
);
2288 rb
->lcd_putsxy(0, LCD_HEIGHT
- 8, buf
);
2291 if (hit_count
!= argh_size
+ 5) {
2292 rb
->button_get(true);
2295 #endif /* DEBUG_WORMLET */
2297 extern bool use_old_rect
;
2300 * These are additional functions required to set various wormlet settings
2301 * and to enable saving of settings and high score
2305 Sets the total number of worms, both human and machine
2307 bool set_worm_num_worms(void)
2310 static const struct opt_items num_worms_option
[3] = {
2316 ret
= rb
->set_option("Number Of Worms", &worm_count
,INT
, num_worms_option
, 3, NULL
);
2317 if (worm_count
< players
) {
2324 Sets the number of human players
2326 bool set_worm_num_players(void)
2329 static const struct opt_items num_players_option
[4] = {
2336 ret
= rb
->set_option("Number of Players", &players
, INT
,num_players_option
, 4, NULL
);
2337 if (players
> worm_count
) {
2338 worm_count
= players
;
2347 Sets the size of each argh block created
2349 bool set_worm_argh_size(void)
2351 static const struct opt_items argh_size_option
[11] = {
2365 return rb
->set_option("Argh Size", &argh_size
,INT
,argh_size_option
, 11, NULL
);
2369 Sets the amount a worm grows per food
2371 bool set_worm_food(void)
2373 static const struct opt_items worm_food_option
[11] = {
2386 return rb
->set_option("Worm Growth Per Food", &worm_food
,INT
,worm_food_option
, 11, NULL
);
2390 Sets the number of arghs created per food
2392 bool set_argh_per_food(void)
2394 static const struct opt_items argh_food_option
[5] = {
2401 return rb
->set_option("Arghs Per Food", &arghs_per_food
,INT
,argh_food_option
, 5, NULL
);
2405 Sets the number of ticks per move
2407 bool set_worm_speed(void)
2410 static const struct opt_items speed_option
[19] = {
2434 ret
= rb
->set_option("Worm Speed", &speed
,INT
,speed_option
, 19, NULL
);
2440 Sets the control style, which depends on the number of players,
2441 remote control existing, etc
2442 bool set_bool_options(char* string, bool* variable,
2443 char* yes_str, char* no_str, void (*function)(bool))
2445 bool set_worm_control_style(void)
2447 static const struct opt_items key1_option
[2] = {
2448 { "Remote Control", -1 },
2449 { "No Rem. Control", -1 },
2452 static const struct opt_items key2_option
[2] = {
2453 { "2 Key Control", -1 },
2454 { "4 Key COntrol", -1 },
2457 static const struct opt_items key3_option
[1] = {
2458 { "Out of Control", -1 },
2462 rb
->set_option("Control Style",&use_remote
,INT
, key1_option
, 2, NULL
);
2465 rb
->set_option("Control Style",&use_remote
,INT
, key2_option
, 2, NULL
);
2468 rb
->set_option("Control Style",&use_remote
,INT
, key3_option
, 1, NULL
);
2474 void default_settings(void)
2476 arghs_per_food
= ARGHS_PER_FOOD
;
2477 argh_size
= ARGH_SIZE
;
2478 food_size
= FOOD_SIZE
;
2480 worm_food
= WORM_PER_FOOD
;
2482 worm_count
= MAX_WORMS
;
2488 Launches the wormlet game
2490 bool launch_wormlet(void)
2492 int game_result
= 1;
2494 rb
->lcd_clear_display();
2496 /* Turn off backlight timeout */
2497 backlight_force_on(); /* backlight control in lib/helper.c */
2499 /* start the game */
2500 while (game_result
== 1)
2501 game_result
= run();
2503 switch (game_result
)
2506 /* Turn on backlight timeout (revert to settings) */
2507 backlight_use_settings(); /* backlight control in lib/helper.c */
2514 /* End of settings/changes etc */
2519 enum plugin_status
plugin_start(const void* parameter
)
2528 if (configfile_load(SETTINGS_FILENAME
, config
,
2529 sizeof(config
)/sizeof(*config
),
2530 SETTINGS_MIN_VERSION
) < 0)
2532 /* If the loading failed, save a new config file (as the disk is
2533 already spinning) */
2534 configfile_save(SETTINGS_FILENAME
, config
,
2535 sizeof(config
)/sizeof(*config
),
2539 #ifdef HAVE_LCD_COLOR
2540 rb
->lcd_set_foreground(COLOR_FG
);
2541 rb
->lcd_set_background(COLOR_BG
);
2545 rb
->lcd_set_backdrop(NULL
);
2548 #ifdef DEBUG_WORMLET
2550 test_worm_argh_collision_in_moves();
2552 test_worm_food_collision();
2553 test_worm_argh_collision();
2554 test_specific_worm_collision();
2559 static const struct opt_items noyes
[2] = {
2564 static const struct opt_items num_worms_option
[3] = {
2571 static const struct opt_items num_players_option
[4] = {
2573 static const struct opt_items num_players_option
[2] = {
2583 static const struct opt_items argh_size_option
[9] = {
2595 static const struct opt_items food_size_option
[9] = {
2607 static const struct opt_items worm_food_option
[16] = {
2626 static const struct opt_items argh_food_option
[9] = {
2638 static const struct opt_items speed_option
[21] = {
2662 static const struct opt_items remoteonly_option
[1] = {
2663 { "Remote Control", -1 }
2666 static const struct opt_items key24_option
[2] = {
2667 { "4 Key Control", -1 },
2668 { "2 Key Control", -1 }
2672 static const struct opt_items remote_option
[2] = {
2673 { "Remote Control", -1 },
2674 { "No Rem. Control", -1 }
2677 static const struct opt_items key2_option
[1] = {
2678 { "2 Key Control", -1 }
2682 static const struct opt_items nokey_option
[1] = {
2683 { "Out of Control", -1 }
2686 MENUITEM_STRINGLIST(menu
, "Wormlet Menu", NULL
, "Play Wormlet!",
2687 "Number of Worms", "Number of Players", "Control Style",
2688 "Worm Growth Per Food","Worm Speed","Arghs Per Food",
2689 "Argh Size","Food Size","Revert to Default Settings",
2690 "Playback Control", "Quit");
2692 rb
->button_clear_queue();
2694 while (!menu_quit
) {
2695 switch(rb
->do_menu(&menu
, &result
, NULL
, false))
2698 rb
->lcd_setfont(FONT_SYSFIXED
);
2702 new_setting
= worm_count
- 1;
2703 rb
->set_option("Number of Worms", &new_setting
, INT
, num_worms_option
, 3, NULL
);
2704 if (new_setting
!= worm_count
)
2705 worm_count
= new_setting
+ 1;
2706 if (worm_count
< players
) {
2707 worm_count
= players
;
2711 new_setting
= players
;
2713 rb
->set_option("Number of Players", &new_setting
, INT
, num_players_option
, 4, NULL
);
2715 rb
->set_option("Number of Players", &new_setting
, INT
, num_players_option
, 2, NULL
);
2717 if (new_setting
!= players
)
2718 players
= new_setting
;
2719 if (players
> worm_count
) {
2720 worm_count
= players
;
2727 new_setting
= use_remote
;
2730 rb
->set_option("Control Style",&new_setting
,INT
, nokey_option
, 1, NULL
);
2733 rb
->set_option("Control Style",&new_setting
,INT
, key24_option
, 2, NULL
);
2737 rb
->set_option("Control Style",&new_setting
,INT
, remote_option
, 2, NULL
);
2739 rb
->set_option("Control Style",&new_setting
,INT
, key2_option
, 1, NULL
);
2743 rb
->set_option("Control Style",&new_setting
,INT
, remoteonly_option
, 1, NULL
);
2746 if (new_setting
!= use_remote
)
2747 use_remote
= new_setting
;
2750 new_setting
= worm_food
;
2751 rb
->set_option("Worm Growth Per Food", &new_setting
,INT
,worm_food_option
, 16, NULL
);
2752 if (new_setting
!= worm_food
)
2753 worm_food
= new_setting
;
2756 new_setting
= speed
;
2757 new_setting
= 20 - new_setting
;
2758 rb
->set_option("Worm Speed", &new_setting
,INT
,speed_option
, 21, NULL
);
2759 new_setting
= 20 - new_setting
;
2760 if (new_setting
!= speed
)
2761 speed
= new_setting
;
2764 new_setting
= arghs_per_food
;
2765 rb
->set_option("Arghs Per Food", &new_setting
,INT
,argh_food_option
, 9, NULL
);
2766 if (new_setting
!= arghs_per_food
)
2767 arghs_per_food
= new_setting
;
2770 new_setting
= argh_size
-2;
2771 rb
->set_option("Argh Size", &new_setting
,INT
,argh_size_option
, 9, NULL
);
2772 if (new_setting
!= argh_size
)
2773 argh_size
= new_setting
+2;
2776 new_setting
= food_size
-2;
2777 rb
->set_option("Food Size", &new_setting
,INT
,food_size_option
, 9, NULL
);
2778 if (new_setting
!= food_size
)
2779 food_size
= new_setting
+2;
2783 rb
->set_option("Reset Settings?", &new_setting
,INT
, noyes
, 2, NULL
);
2784 if (new_setting
== 1)
2788 playback_control(NULL
);
2796 configfile_save(SETTINGS_FILENAME
, config
,
2797 sizeof(config
)/sizeof(*config
),