very minor code police. also fix a possible but unlikely missed cpu_boost(false)
[Rockbox.git] / apps / plugins / wormlet.c
blob54690735883a91e5caa3e9a750b77817c83485d9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
22 #include "configfile.h"
23 #include "helper.h"
25 PLUGIN_HEADER
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 */
40 #define MAX_WORMS 3
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
61 #define REMOTE
62 #define MULTIPLAYER
63 #endif
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
224 #else
225 #error No keymap defined!
226 #endif
228 #ifdef HAVE_TOUCHPAD
229 #ifndef BTN_DIR_UP
230 #define BTN_DIR_UP BUTTON_TOPMIDDLE
231 #endif
232 #ifndef BTN_DIR_DOWN
233 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
234 #endif
235 #ifndef BTN_DIR_LEFT
236 #define BTN_DIR_LEFT BUTTON_MIDLEFT
237 #endif
238 #ifndef BTN_DIR_RIGHT
239 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
240 #endif
241 #ifndef BTN_STARTPAUSE
242 #define BTN_STARTPAUSE BUTTON_CENTER
243 #endif
244 #ifndef BTN_QUIT
245 #define BTN_QUIT BUTTON_TOPLEFT
246 #endif
247 #ifndef BTN_STOPRESET
248 #define BTN_STOPRESET BUTTON_TOPRIGHT
250 #endif
251 #ifndef PLAYERS_TEXT
252 #define PLAYERS_TEXT "Up/Down"
253 #endif
254 #ifndef WORMS_TEXT
255 #define WORMS_TEXT "Left/Right"
256 #endif
257 #endif
260 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
261 #define FOOD_SIZE 3
262 #define ARGH_SIZE 4
263 #define SPEED 14
264 #define MAX_WORM_SEGMENTS 128
265 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
266 #define FOOD_SIZE 3
267 #define ARGH_SIZE 4
268 #define SPEED 14
269 #define MAX_WORM_SEGMENTS 128
270 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
271 #define FOOD_SIZE 3
272 #define ARGH_SIZE 4
273 #define SPEED 12
274 #define MAX_WORM_SEGMENTS 128
275 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
276 #define FOOD_SIZE 4
277 #define ARGH_SIZE 5
278 #define SPEED 10
279 #define MAX_WORM_SEGMENTS 128
280 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
281 #define FOOD_SIZE 4
282 #define ARGH_SIZE 5
283 #define SPEED 9
284 #define MAX_WORM_SEGMENTS 128
285 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
286 #define FOOD_SIZE 4
287 #define ARGH_SIZE 5
288 #define SPEED 8
289 #define MAX_WORM_SEGMENTS 256
290 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
291 #define FOOD_SIZE 4
292 #define ARGH_SIZE 5
293 #define SPEED 6
294 #define MAX_WORM_SEGMENTS 256
295 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
296 #define FOOD_SIZE 5
297 #define ARGH_SIZE 6
298 #define SPEED 4
299 #define MAX_WORM_SEGMENTS 512
300 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
301 #define FOOD_SIZE 5
302 #define ARGH_SIZE 6
303 #define SPEED 4
304 #define MAX_WORM_SEGMENTS 512
305 #elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240)
306 #define FOOD_SIZE 7
307 #define ARGH_SIZE 8
308 #define SPEED 4
309 #define MAX_WORM_SEGMENTS 512
310 #elif (LCD_WIDTH == 240) && (LCD_HEIGHT == 320)
311 #define FOOD_SIZE 7
312 #define ARGH_SIZE 8
313 #define SPEED 4
314 #define MAX_WORM_SEGMENTS 512
315 #endif
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)
323 #endif
326 * All the properties that a worm has.
328 static struct worm {
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
346 of the worm */
347 void (*fetch_worm_direction)(struct worm *w);
348 } worms[MAX_WORMS];
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 */
382 #ifdef DEBUG_WORMLET
383 /* just a buffer used for debug output */
384 static char debugout[15];
385 #endif
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. */
405 #define WEST 0
406 #define NORTH 1
407 #define EAST 2
408 #define SOUTH 3
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
418 control a worm */
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}
441 #ifdef DEBUG_WORMLET
442 static void set_debug_out(char *str){
443 strcpy(debugout, str);
445 #endif
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) {
456 int retVal ;
457 if (w->dirx == 0) {
458 if (w->diry == 1) {
459 retVal = SOUTH;
460 } else {
461 retVal = NORTH;
463 } else {
464 if (w->dirx == 1) {
465 retVal = EAST;
466 } else {
467 retVal = WEST;
470 return retVal;
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) {
483 switch (dir) {
484 case WEST:
485 w->dirx = -1;
486 w->diry = 0;
487 break;
488 case NORTH:
489 w->dirx = 0;
490 w->diry = - 1;
491 break;
492 case EAST:
493 w->dirx = 1;
494 w->diry = 0;
495 break;
496 case SOUTH:
497 w->dirx = 0;
498 w->diry = 1;
499 break;
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 */
513 if (retVal < 0) {
514 retVal = w->head + MAX_WORM_SEGMENTS - w->tail;
517 return retVal;
521 * Returns the score the specified worm. The score is the length
522 * of the worm.
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) {
528 int retval = 0;
529 int length = get_worm_array_length(w);
530 int i;
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);
547 } else {
548 minimum = MIN(startx, endx);
549 maximum = MAX(startx, endx);
551 retval += abs(maximum - minimum);
553 return retval;
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) {
571 bool retval = false;
572 int simple, simplemin, simplemax;
573 int compa, compb, compmin, compmax;
574 int temp;
575 if (startx == endx) {
576 simple = startx;
577 simplemin = x;
578 simplemax = x + width;
580 compa = starty;
581 compb = endy;
582 compmin = y;
583 compmax = y + height;
584 } else {
585 simple = starty;
586 simplemin = y;
587 simplemax = y + height;
589 compa = startx;
590 compb = endx;
591 compmin = x;
592 compmax = x + width;
595 temp = compa;
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)) {
603 retval = true;
606 return retval;
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) {
619 bool retval = false;
622 /* get_worm_array_length is expensive -> buffer the value */
623 int wormLength = get_worm_array_length(w);
624 int i;
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);
641 return retval;
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
651 * foodIndex.
653 static bool specific_food_collision(int foodIndex, int x, int y) {
654 bool retVal = false;
655 if (x >= foodx[foodIndex] &&
656 x < foodx[foodIndex] + food_size &&
657 y >= foody[foodIndex] &&
658 y < foody[foodIndex] + food_size) {
660 retVal = true;
662 return retVal;
666 * Returns the index of the food that is at the
667 * given coordinates. If no food is at the coordinates
668 * -1 is returned.
669 * @return int -1 <= value < MAX_FOOD
671 static int food_collision(int x, int y) {
672 int i = 0;
673 int retVal = -1;
674 for (i = 0; i < MAX_FOOD; i++) {
675 if (specific_food_collision(i, x, y)) {
676 retVal = i;
677 break;
680 return retVal;
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
690 * arghIndex.
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 )
699 return true;
702 return false;
706 * Returns the index of the argh that is at the
707 * given coordinates. If no argh is at the coordinates
708 * -1 is returned.
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) {
714 int i = 0;
715 int retVal = -1;
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)) {
720 retVal = i;
721 break;
724 return retVal;
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)
735 bool retVal = false;
737 retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex],
738 food_size - 1, food_size - 1);
740 return retVal;
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
750 * moves.
752 static bool worm_argh_collision_in_moves(struct worm *w, int argh_idx, int moves){
753 bool retVal = false;
754 int x1, y1, x2, y2;
755 x1 = w->x[w->head];
756 y1 = w->y[w->head];
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);
763 return retVal;
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)
774 bool retVal = false;
776 retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex],
777 argh_size - 1, argh_size - 1);
779 return retVal;
783 * Find new coordinates for the food stored in foodx[index], foody[index]
784 * that don't collide with any other food or argh
785 * @param int index
786 * Ensure that 0 <= index < MAX_FOOD.
788 static void make_food(int index) {
790 int x = 0;
791 int y = 0;
792 bool collisionDetected = false;
793 int i;
795 do {
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.
806 collisionDetected =
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 */
817 foodx[index] = x;
818 foody[index] = y;
820 /* now test wether we accidently hit the worm with food ;) */
821 i = 0;
822 for (i = 0; i < worm_count && !collisionDetected; i++) {
823 collisionDetected |= worm_food_collision(&worms[i], index);
826 while (collisionDetected);
827 return;
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);
857 #endif
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);
868 #endif
872 * Find new coordinates for the argh stored in arghx[index], arghy[index]
873 * that don't collide with any other food or argh.
874 * @param int index
875 * Ensure that 0 <= index < argh_count < MAX_ARGH.
877 static void make_argh(int index)
879 int x = -1;
880 int y = -1;
881 bool collisionDetected = false;
882 int i;
884 do {
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.
895 collisionDetected =
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 */
906 arghx[index] = x;
907 arghy[index] = y;
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,
913 MIN_ARGH_DIST);
916 while (collisionDetected);
917 return;
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);
931 #endif
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);
937 #endif
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 */
953 w->head = 1;
954 w->tail = 0;
956 w->x[w->head] = x + 1;
957 w->y[w->head] = y;
959 w->x[w->tail] = x;
960 w->y[w->tail] = y;
962 /* set the initial direction the worm creeps to */
963 w->dirx = 1;
964 w->diry = 0;
966 w->growing = INITIAL_WORM_LENGTH - 1;
967 w->alive = true;
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.
975 * The value of
976 * the direction is read from player1_dir.
977 * @param struct worm *w - The worm of which the direction
978 * is altered.
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.
988 * The value of
989 * the direction is read from player2_dir.
990 * @param struct worm *w - The worm of which the direction
991 * is altered.
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.
1002 * The value of
1003 * the direction is read from player3_dir.
1004 * @param struct worm *w - The worm of which the direction
1005 * is altered.
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)
1017 int i;
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);
1027 player1_dir = EAST;
1028 player2_dir = EAST;
1029 player3_dir = EAST;
1031 if (players > 0) {
1032 worms[0].fetch_worm_direction = human_player1;
1035 if (players > 1) {
1036 if (use_remote) {
1037 worms[1].fetch_worm_direction = remote_player;
1038 } else {
1039 worms[1].fetch_worm_direction = human_player2;
1043 if (players > 2) {
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++) {
1053 make_food(i);
1054 draw_food(i);
1055 make_argh(i);
1056 draw_argh(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 */
1066 rb->lcd_update();
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)
1079 if (w->alive) {
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 */
1088 int olddirx;
1089 int olddiry;
1090 if (headx == preheadx) {
1091 olddirx = 0;
1092 olddiry = (heady > preheady) ? 1 : -1;
1093 } else {
1094 olddiry = 0;
1095 olddirx = (headx > preheadx) ? 1 : -1;
1098 /* olddir == dir?
1099 a change of direction means a new segment
1100 has been opened */
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 */
1114 w->growing--;
1117 /* if the worm isn't growing the tail has to be dragged */
1118 else {
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;
1129 } else {
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
1136 must be freed */
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);
1156 #endif
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 */
1167 x = w->x[w->tail] ;
1168 y = w->y[w->tail] ;
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);
1175 #endif
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)
1188 int retVal = -1;
1190 /* get_worm_array_length is expensive -> buffer the value */
1191 int wormLength = get_worm_array_length(w);
1192 int i;
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;
1206 if (samey) {
1207 min = w->x[linestart];
1208 max = w->x[lineend];
1209 test = x;
1210 } else {
1211 min = w->y[linestart];
1212 max = w->y[lineend];
1213 test = y;
1216 tmp = min;
1217 min = MIN(min, max);
1218 max = MAX(tmp, max);
1220 if (min <= test && test <= max) {
1221 retVal = lineend;
1225 return retVal;
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) {
1237 w->growing += 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;
1258 int i;
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)){
1263 retVal = &worms[i];
1267 return retVal;
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))
1283 retVal = true;
1285 return retVal;
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
1295 * field of worms.
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);
1301 return retVal;
1305 * Checks and returns wether the head of the w
1306 * is colliding with something currently.
1307 * @return int One of the values:
1308 * COLLISION_NONE
1309 * COLLISION_w
1310 * COLLISION_FOOD
1311 * COLLISION_ARGH
1312 * COLLISION_FIELD
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;
1330 return retVal;
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
1336 * foody arrays.
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;
1344 int deltax = 0;
1345 int deltay = 0;
1346 int foodindex;
1347 for (foodindex = 0; foodindex < MAX_FOOD; foodindex++) {
1348 int distance;
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;
1360 return nearestfood;
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];
1379 if (w->dirx == 0) {
1380 infront = (w->diry * deltay) > 0;
1381 } else {
1382 infront = (w->dirx * deltax) > 0;
1384 return infront;
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);
1398 if (!retVal) {
1399 retVal = (argh_collision(x, y) != -1);
1402 if (!retVal) {
1403 retVal = (worm_collision(w, x, y) != NULL);
1405 return retVal;
1409 * This function
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
1414 * worm.
1415 * @param struct worm *w - The worm of which the direction
1416 * is altered.
1418 static void virtual_player(struct worm *w) {
1419 bool isright;
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 */
1436 if (infront) {
1437 if (isright) {
1438 plana = olddir;
1439 planb = (olddir + 1) % 4;
1440 planc = (olddir + 3) % 4;
1441 } else {
1442 plana = olddir;
1443 planb = (olddir + 3) % 4;
1444 planc = (olddir + 1) % 4;
1446 } else {
1447 if (isright) {
1448 plana = (olddir + 1) % 4;
1449 planb = olddir;
1450 planc = (olddir + 3) % 4;
1451 } else {
1452 plana = (olddir + 3) % 4;
1453 planb = olddir;
1454 planc = (olddir + 1) % 4;
1458 /* test for collision */
1459 set_worm_dir(w, plana);
1460 if (will_worm_collide(w)){
1462 /* plan b */
1463 set_worm_dir(w, planb);
1465 /* test for collision */
1466 if (will_worm_collide(w)) {
1468 /* plan c */
1469 set_worm_dir(w, planc);
1475 * prints out the score board with all the status information
1476 * about the game.
1478 static void score_board(void)
1480 char buf[15];
1481 char* buf2 = NULL;
1482 int i;
1483 int y = 0;
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]);
1490 /* high score */
1491 if (worms[i].fetch_worm_direction != virtual_player){
1492 if (highscore < score) {
1493 highscore = score;
1497 /* length */
1498 rb->snprintf(buf, sizeof (buf),"Len:%d", score);
1500 /* worm state */
1501 switch (check_collision(&worms[i])) {
1502 case COLLISION_NONE:
1503 if (worms[i].growing > 0)
1504 buf2 = "Growing";
1505 else {
1506 if (worms[i].alive)
1507 buf2 = "Hungry";
1508 else
1509 buf2 = "Wormed";
1511 break;
1513 case COLLISION_WORM:
1514 buf2 = "Wormed";
1515 break;
1517 case COLLISION_FOOD:
1518 buf2 = "Growing";
1519 break;
1521 case COLLISION_ARGH:
1522 buf2 = "Argh";
1523 break;
1525 case COLLISION_FIELD:
1526 buf2 = "Crashed";
1527 break;
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);
1538 y += 19;
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);
1543 #else
1544 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout);
1545 #endif
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)
1556 int index = -1;
1558 w->alive &= !field_collision(w);
1560 if (w->alive) {
1562 /* check if food was eaten */
1563 index = food_collision(w->x[w->head], w->y[w->head]);
1564 if (index != -1){
1565 int i;
1567 clear_food(index);
1568 make_food(index);
1569 draw_food(index);
1571 for (i = 0; i < arghs_per_food; i++) {
1572 argh_count++;
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);
1581 draw_worm(w);
1584 /* check if argh was eaten */
1585 else {
1586 index = argh_collision(w->x[w->head], w->y[w->head]);
1587 if (index != -1) {
1588 w->alive = false;
1590 else {
1591 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) {
1592 w->alive = false;
1597 return !w->alive;
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)
1608 int button = 0;
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;
1616 long max_cycle = 0;
1617 char buf[20];
1618 #endif
1620 /* initialize the board and so on */
1621 init_wormlet();
1623 cycle_start = *rb->current_tick;
1624 /* change the direction of the worm */
1625 while (!wormDead)
1627 int i;
1628 long cycle_duration=0;
1630 #ifdef HAS_BUTTON_HOLD
1631 if (rb->button_hold())
1632 paused = true;
1633 #endif
1635 switch (button) {
1636 case BTN_STARTPAUSE:
1637 paused = !paused;
1638 break;
1639 case BTN_STOPRESET:
1640 if (paused)
1641 return 1; /* restart game */
1642 else
1643 paused = true;
1644 break;
1645 #ifdef BTN_RC_QUIT
1646 case BTN_RC_QUIT:
1647 #endif
1648 case BTN_QUIT:
1649 return 2; /* back to menu */
1650 break;
1652 if (!paused)
1654 switch (button) {
1655 case BTN_DIR_UP:
1656 if (players == 1 && !use_remote) {
1657 player1_dir = NORTH;
1659 break;
1661 case BTN_DIR_DOWN:
1662 if (players == 1 && !use_remote) {
1663 player1_dir = SOUTH;
1665 break;
1667 case BTN_DIR_LEFT:
1668 if (players != 1 || use_remote) {
1669 player1_dir = (player1_dir + 3) % 4;
1670 } else {
1671 player1_dir = WEST;
1673 break;
1675 case BTN_DIR_RIGHT:
1676 if (players != 1 || use_remote) {
1677 player1_dir = (player1_dir + 1) % 4;
1678 } else {
1679 player1_dir = EAST;
1681 break;
1683 #ifdef MULTIPLAYER
1684 case BTN_PLAYER2_DIR1:
1685 player2_dir = (player2_dir + 3) % 4;
1686 break;
1688 case BTN_PLAYER2_DIR2:
1689 player2_dir = (player2_dir + 1) % 4;
1690 break;
1691 #endif
1693 #ifdef REMOTE
1694 case BTN_RC_UP:
1695 player3_dir = (player3_dir + 1) % 4;
1696 break;
1698 case BTN_RC_DOWN:
1699 player3_dir = (player3_dir + 3) % 4;
1700 break;
1701 #endif
1705 for (i = 0; i < worm_count; i++) {
1706 worms[i].fetch_worm_direction(&worms[i]);
1709 wormDead = true;
1710 for (i = 0; i < worm_count; i++){
1711 struct worm *w = &worms[i];
1712 move_worm(w);
1713 wormDead &= process_collisions(w);
1714 draw_worm(w);
1716 score_board();
1717 rb->lcd_update();
1718 if (button == BTN_STOPRESET) {
1719 wormDead = true;
1722 /* here the wormlet game cycle ends
1723 thus the current tick is stored
1724 as end time */
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) {
1736 max_cycle = 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);
1744 set_debug_out(buf);
1745 #endif
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;
1767 int i;
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]);
1783 foodx[0] = 15;
1784 foody[0] = 12;
1785 for (foody[0] = 20; foody[0] > 0; foody[0] --) {
1786 char buf[20];
1787 bool collision;
1788 draw_worm(&worms[0]);
1789 draw_food(0);
1790 collision = worm_food_collision(&worms[0], 0);
1791 if (collision) {
1792 collision_count++;
1794 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1795 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1796 rb->lcd_update();
1798 if (collision_count != food_size) {
1799 rb->button_get(true);
1803 foody[0] = 15;
1804 for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) {
1805 char buf[20];
1806 bool collision;
1807 draw_worm(&worms[0]);
1808 draw_food(0);
1809 collision = worm_food_collision(&worms[0], 0);
1810 if (collision) {
1811 collision_count ++;
1813 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1814 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1815 rb->lcd_update();
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){
1824 int x, y;
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) {
1829 retVal = true;
1833 return retVal;
1836 static void test_worm_argh_collision(void) {
1837 int i;
1838 int dir;
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]);
1851 arghx[0] = 12;
1852 for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){
1853 char buf[20];
1854 bool collision;
1855 draw_argh(0);
1856 collision = worm_argh_collision(&worms[0], 0);
1857 if (collision) {
1858 collision_count ++;
1860 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1861 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1862 rb->lcd_update();
1864 if (collision_count != argh_size * 2) {
1865 rb->button_get(true);
1868 arghy[0] = 12;
1869 for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){
1870 char buf[20];
1871 bool collision;
1872 draw_argh(0);
1873 collision = worm_argh_collision(&worms[0], 0);
1874 if (collision) {
1875 collision_count ++;
1877 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1878 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1879 rb->lcd_update();
1881 if (collision_count != argh_size * 4) {
1882 rb->button_get(true);
1886 static int testline_in_rect(void) {
1887 int testfailed = -1;
1889 int rx = 10;
1890 int ry = 15;
1891 int rw = 20;
1892 int rh = 25;
1894 /* Test 1 */
1895 int x1 = 12;
1896 int y1 = 8;
1897 int x2 = 12;
1898 int y2 = 42;
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);
1904 rb->lcd_update();
1905 rb->lcd_putsxy(0, 0, "failed 1");
1906 rb->button_get(true);
1907 testfailed = 1;
1910 /* test 2 */
1911 y2 = 20;
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");
1917 rb->lcd_update();
1918 rb->button_get(true);
1919 testfailed = 2;
1922 /* test 3 */
1923 y1 = 30;
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");
1929 rb->lcd_update();
1930 rb->button_get(true);
1931 testfailed = 3;
1934 /* test 4 */
1935 y2 = 45;
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");
1941 rb->lcd_update();
1942 rb->button_get(true);
1943 testfailed = 4;
1946 /* test 5 */
1947 y1 = 50;
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");
1953 rb->lcd_update();
1954 rb->button_get(true);
1955 testfailed = 5;
1958 /* test 6 */
1959 y1 = 5;
1960 y2 = 7;
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");
1966 rb->lcd_update();
1967 rb->button_get(true);
1968 testfailed = 6;
1971 /* test 7 */
1972 x1 = 8;
1973 y1 = 20;
1974 x2 = 35;
1975 y2 = 20;
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");
1981 rb->lcd_update();
1982 rb->button_get(true);
1983 testfailed = 7;
1986 /* test 8 */
1987 x2 = 12;
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");
1993 rb->lcd_update();
1994 rb->button_get(true);
1995 testfailed = 8;
1998 /* test 9 */
1999 x1 = 25;
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");
2005 rb->lcd_update();
2006 rb->button_get(true);
2007 testfailed = 9;
2010 /* test 10 */
2011 x2 = 37;
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");
2017 rb->lcd_update();
2018 rb->button_get(true);
2019 testfailed = 10;
2022 /* test 11 */
2023 x1 = 42;
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");
2029 rb->lcd_update();
2030 rb->button_get(true);
2031 testfailed = 11;
2034 /* test 12 */
2035 x1 = 5;
2036 x2 = 7;
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");
2042 rb->lcd_update();
2043 rb->button_get(true);
2044 testfailed = 12;
2047 /* test 13 */
2048 rx = 9;
2049 ry = 15;
2050 rw = food_size;
2051 rh = food_size;
2053 x1 = 10;
2054 y1 = 10;
2055 x2 = 10;
2056 y2 = 20;
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");
2062 rb->lcd_update();
2063 rb->button_get(true);
2064 testfailed = 13;
2067 /* test 14 */
2068 rx = 9;
2069 ry = 15;
2070 rw = 4;
2071 rh = 4;
2073 x1 = 10;
2074 y1 = 10;
2075 x2 = 10;
2076 y2 = 19;
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");
2082 rb->lcd_update();
2083 rb->button_get(true);
2084 testfailed = 14;
2087 rb->lcd_clear_display();
2089 return testfailed;
2093 * Just a test routine to test wether specific_worm_collision might work properly
2095 static int test_specific_worm_collision(void) {
2096 int collisions = 0;
2097 int dir;
2098 int x = 0;
2099 int y = 0;
2100 char buf[20];
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++) {
2106 int i;
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) {
2119 collisions ++;
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);
2124 rb->lcd_update();
2127 if (collisions != 21) {
2128 rb->button_get(true);
2130 return collisions;
2133 static void test_make_argh(void){
2134 int dir;
2135 int seed = 0;
2136 int hit = 0;
2137 int failures = 0;
2138 int last_failures = 0;
2139 int i, worm_idx;
2140 rb->lcd_clear_display();
2141 worm_count = 3;
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]);
2160 rb->lcd_update();
2162 for (seed = 0; hit < 20; seed += 2) {
2163 char buf[20];
2164 int x, y;
2165 rb->srand(seed);
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)) {
2171 int tries = 0;
2172 rb->srand(seed);
2174 tries = make_argh(0);
2175 if ((x == arghx[0] && y == arghy[0]) || tries < 2) {
2176 failures ++;
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);
2181 rb->lcd_update();
2182 rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y, argh_size, argh_size);
2183 rb->lcd_update();
2184 draw_argh(0);
2185 rb->lcd_update();
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;
2193 hit ++;
2199 static void test_worm_argh_collision_in_moves(void) {
2200 int hit_count = 0;
2201 int i;
2202 rb->lcd_clear_display();
2203 init_worm(&worms[0], 10, 20);
2205 arghx[0] = 20;
2206 arghy[0] = 18;
2207 draw_argh(0);
2209 set_worm_dir(&worms[0], EAST);
2210 for (i = 0; i < 20; i++) {
2211 char buf[20];
2212 move_worm(&worms[0]);
2213 draw_worm(&worms[0]);
2214 if (worm_argh_collision_in_moves(&worms[0], 0, 5)){
2215 hit_count ++;
2217 rb->snprintf(buf, sizeof buf, "in 5 moves hits: %d", hit_count);
2218 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2219 rb->lcd_update();
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)
2239 bool ret;
2240 static const struct opt_items num_worms_option[3] = {
2241 { "1", -1 },
2242 { "2", -1 },
2243 { "3", -1 },
2246 ret = rb->set_option("Number Of Worms", &worm_count,INT, num_worms_option, 3, NULL);
2247 if (worm_count < players) {
2248 worm_count=players;
2250 return ret;
2254 Sets the number of human players
2256 bool set_worm_num_players(void)
2258 bool ret;
2259 static const struct opt_items num_players_option[4] = {
2260 { "0", -1 },
2261 { "1", -1 },
2262 { "2", -1 },
2263 { "3", -1 }
2266 ret = rb->set_option("Number of Players", &players, INT,num_players_option , 4, NULL);
2267 if (players > worm_count) {
2268 worm_count = players;
2270 if (players > 2) {
2271 use_remote = true;
2273 return ret;
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] = {
2282 { "0", -1 },
2283 { "1", -1 },
2284 { "2", -1 },
2285 { "3", -1 },
2286 { "4", -1 },
2287 { "5", -1 },
2288 { "6", -1 },
2289 { "7", -1 },
2290 { "8", -1 },
2291 { "9", -1 },
2292 { "10", -1 }
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] = {
2304 { "0", -1 },
2305 { "1", -1 },
2306 { "2", -1 },
2307 { "3", -1 },
2308 { "4", -1 },
2309 { "5", -1 },
2310 { "6", -1 },
2311 { "7", -1 },
2312 { "8", -1 },
2313 { "9", -1 },
2314 { "10", -1 }
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] = {
2325 { "0", -1 },
2326 { "1", -1 },
2327 { "2", -1 },
2328 { "3", -1 },
2329 { "4", -1 },
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)
2339 bool ret;
2340 static const struct opt_items speed_option[19] = {
2341 { "0", -1 },
2342 { "1", -1 },
2343 { "2", -1 },
2344 { "3", -1 },
2345 { "4", -1 },
2346 { "5", -1 },
2347 { "6", -1 },
2348 { "7", -1 },
2349 { "8", -1 },
2350 { "9", -1 },
2351 { "10", -1 },
2352 { "11", -1 },
2353 { "12", -1 },
2354 { "13", -1 },
2355 { "14", -1 },
2356 { "15", -1 },
2357 { "16", -1 },
2358 { "17", -1 },
2359 { "18", -1 },
2363 speed = 20 - speed;
2364 ret = rb->set_option("Worm Speed", &speed,INT,speed_option , 19, NULL);
2365 speed = 20 - speed;
2366 return ret;
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 },
2391 if (players > 1) {
2392 rb->set_option("Control Style",&use_remote,INT, key1_option, 2, NULL);
2393 } else {
2394 if (players > 0) {
2395 rb->set_option("Control Style",&use_remote,INT, key2_option, 2, NULL);
2397 else {
2398 rb->set_option("Control Style",&use_remote,INT, key3_option, 1, NULL);
2401 return false;
2404 void default_settings(void)
2406 arghs_per_food = ARGHS_PER_FOOD;
2407 argh_size = ARGH_SIZE;
2408 food_size = FOOD_SIZE;
2409 speed = SPEED;
2410 worm_food = WORM_PER_FOOD;
2411 players = 1;
2412 worm_count = MAX_WORMS;
2413 use_remote = false;
2414 return;
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)
2435 case 2:
2436 /* Turn on backlight timeout (revert to settings) */
2437 backlight_use_settings(rb); /* backlight control in lib/helper.c */
2438 return false;
2439 break;
2441 return false;
2444 /* End of settings/changes etc */
2447 * Main entry point
2449 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
2451 int result;
2452 int menu_quit = 0;
2453 int new_setting;
2455 (void)(parameter);
2456 rb = api;
2458 default_settings();
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),
2468 SETTINGS_VERSION);
2471 #ifdef HAVE_LCD_COLOR
2472 rb->lcd_set_foreground(COLOR_FG);
2473 rb->lcd_set_background(COLOR_BG);
2474 #endif
2476 #if LCD_DEPTH > 1
2477 rb->lcd_set_backdrop(NULL);
2478 #endif
2480 #ifdef DEBUG_WORMLET
2481 testline_in_rect();
2482 test_worm_argh_collision_in_moves();
2483 test_make_argh();
2484 test_worm_food_collision();
2485 test_worm_argh_collision();
2486 test_specific_worm_collision();
2487 #endif
2489 /* Setup screen */
2491 static const struct opt_items noyes[2] = {
2492 { "No", -1 },
2493 { "Yes", -1 },
2496 static const struct opt_items num_worms_option[3] = {
2497 { "1", -1 },
2498 { "2", -1 },
2499 { "3", -1 }
2502 #ifdef MULTIPLAYER
2503 static const struct opt_items num_players_option[4] = {
2504 #else
2505 static const struct opt_items num_players_option[2] = {
2506 #endif
2507 { "0", -1 },
2508 { "1", -1 }
2509 #ifdef MULTIPLAYER
2510 ,{ "2", -1 },
2511 { "3", -1 }
2512 #endif
2515 static const struct opt_items argh_size_option[9] = {
2516 { "2", -1 },
2517 { "3", -1 },
2518 { "4", -1 },
2519 { "5", -1 },
2520 { "6", -1 },
2521 { "7", -1 },
2522 { "8", -1 },
2523 { "9", -1 },
2524 { "10", -1 }
2527 static const struct opt_items food_size_option[9] = {
2528 { "2", -1 },
2529 { "3", -1 },
2530 { "4", -1 },
2531 { "5", -1 },
2532 { "6", -1 },
2533 { "7", -1 },
2534 { "8", -1 },
2535 { "9", -1 },
2536 { "10", -1 }
2539 static const struct opt_items worm_food_option[16] = {
2540 { "0", -1 },
2541 { "1", -1 },
2542 { "2", -1 },
2543 { "3", -1 },
2544 { "4", -1 },
2545 { "5", -1 },
2546 { "6", -1 },
2547 { "7", -1 },
2548 { "8", -1 },
2549 { "9", -1 },
2550 { "10", -1 },
2551 { "11", -1 },
2552 { "12", -1 },
2553 { "13", -1 },
2554 { "14", -1 },
2555 { "15", -1 }
2558 static const struct opt_items argh_food_option[9] = {
2559 { "0", -1 },
2560 { "1", -1 },
2561 { "2", -1 },
2562 { "3", -1 },
2563 { "4", -1 },
2564 { "5", -1 },
2565 { "6", -1 },
2566 { "7", -1 },
2567 { "8", -1 }
2570 static const struct opt_items speed_option[21] = {
2571 { "0", -1 },
2572 { "1", -1 },
2573 { "2", -1 },
2574 { "3", -1 },
2575 { "4", -1 },
2576 { "5", -1 },
2577 { "6", -1 },
2578 { "7", -1 },
2579 { "8", -1 },
2580 { "9", -1 },
2581 { "10", -1 },
2582 { "11", -1 },
2583 { "12", -1 },
2584 { "13", -1 },
2585 { "14", -1 },
2586 { "15", -1 },
2587 { "16", -1 },
2588 { "17", -1 },
2589 { "18", -1 },
2590 { "19", -1 },
2591 { "20", -1 }
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 }
2603 #ifdef REMOTE
2604 static const struct opt_items remote_option[2] = {
2605 { "Remote Control", -1 },
2606 { "No Rem. Control", -1 }
2608 #else
2609 static const struct opt_items key2_option[1] = {
2610 { "2 Key Control", -1 }
2612 #endif
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",
2622 "Quit");
2624 rb->button_clear_queue();
2626 while (!menu_quit) {
2627 switch(rb->do_menu(&menu, &result, NULL, false))
2629 case 0:
2630 rb->lcd_setfont(FONT_SYSFIXED);
2631 launch_wormlet();
2632 break;
2633 case 1:
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;
2641 break;
2642 case 2:
2643 new_setting = players;
2644 #ifdef MULTIPLAYER
2645 rb->set_option("Number of Players", &new_setting, INT, num_players_option , 4, NULL);
2646 #else
2647 rb->set_option("Number of Players", &new_setting, INT, num_players_option , 2, NULL);
2648 #endif
2649 if (new_setting != players)
2650 players = new_setting;
2651 if (players > worm_count) {
2652 worm_count = players;
2654 if (players > 2) {
2655 use_remote = true;
2657 break;
2658 case 3:
2659 new_setting = use_remote;
2660 switch(players) {
2661 case 0:
2662 rb->set_option("Control Style",&new_setting,INT, nokey_option, 1, NULL);
2663 break;
2664 case 1:
2665 rb->set_option("Control Style",&new_setting,INT, key24_option, 2, NULL);
2666 break;
2667 case 2:
2668 #ifdef REMOTE
2669 rb->set_option("Control Style",&new_setting,INT, remote_option, 2, NULL);
2670 #else
2671 rb->set_option("Control Style",&new_setting,INT, key2_option, 1, NULL);
2672 #endif
2673 break;
2674 case 3:
2675 rb->set_option("Control Style",&new_setting,INT, remoteonly_option, 1, NULL);
2676 break;
2678 if (new_setting != use_remote)
2679 use_remote = new_setting;
2680 break;
2681 case 4:
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;
2686 break;
2687 case 5:
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;
2694 break;
2695 case 6:
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;
2700 break;
2701 case 7:
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;
2706 break;
2707 case 8:
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;
2712 break;
2713 case 9:
2714 new_setting = 0;
2715 rb->set_option("Reset Settings?", &new_setting,INT, noyes , 2, NULL);
2716 if (new_setting == 1)
2717 default_settings();
2718 break;
2719 default:
2720 menu_quit=1;
2721 break;
2725 configfile_save(SETTINGS_FILENAME, config,
2726 sizeof(config)/sizeof(*config),
2727 SETTINGS_VERSION);
2729 return PLUGIN_OK;