Correct battery conditional and disk access icon
[kugel-rb.git] / apps / plugins / wormlet.c
blob3f67e6185790cc0e8d4a3ce775e340ccc0c8df1b
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 "lib/configfile.h"
23 #include "lib/helper.h"
24 #include "lib/playback_control.h"
26 PLUGIN_HEADER
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 */
41 #define MAX_WORMS 3
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
62 #define REMOTE
63 #define MULTIPLAYER
64 #endif
66 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
67 #define BTN_DIR_UP BUTTON_UP
68 #define BTN_DIR_DOWN BUTTON_DOWN
69 #define BTN_DIR_LEFT BUTTON_LEFT
70 #define BTN_DIR_RIGHT BUTTON_RIGHT
71 #define BTN_PLAYER2_DIR1 BUTTON_F2
72 #define BTN_PLAYER2_DIR2 BUTTON_F3
73 #define BTN_STARTPAUSE BUTTON_SELECT
74 #define BTN_QUIT BUTTON_OFF
75 #define BTN_STOPRESET BUTTON_ON
76 #define BTN_TOGGLE_KEYS BUTTON_F1
78 #elif (CONFIG_KEYPAD == ONDIO_PAD)
79 #define BTN_DIR_UP BUTTON_UP
80 #define BTN_DIR_DOWN BUTTON_DOWN
81 #define BTN_DIR_LEFT BUTTON_LEFT
82 #define BTN_DIR_RIGHT BUTTON_RIGHT
83 #define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL)
84 #define BTN_QUIT (BUTTON_OFF|BUTTON_REL)
85 #define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU)
87 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
88 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
90 #define BTN_DIR_UP BUTTON_MENU
91 #define BTN_DIR_DOWN BUTTON_PLAY
92 #define BTN_DIR_LEFT BUTTON_LEFT
93 #define BTN_DIR_RIGHT BUTTON_RIGHT
94 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
95 #define BTN_QUIT (BUTTON_SELECT|BUTTON_MENU)
96 #define BTN_STOPRESET (BUTTON_SELECT|BUTTON_PLAY)
98 #elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
100 #define BTN_DIR_UP BUTTON_UP
101 #define BTN_DIR_DOWN BUTTON_DOWN
102 #define BTN_DIR_LEFT BUTTON_LEFT
103 #define BTN_DIR_RIGHT BUTTON_RIGHT
104 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
105 #define BTN_QUIT BUTTON_OFF
106 #define BTN_STOPRESET BUTTON_ON
108 #define BTN_RC_QUIT BUTTON_RC_STOP
110 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
112 #define BTN_DIR_UP BUTTON_UP
113 #define BTN_DIR_DOWN BUTTON_DOWN
114 #define BTN_DIR_LEFT BUTTON_LEFT
115 #define BTN_DIR_RIGHT BUTTON_RIGHT
116 #define BTN_STARTPAUSE BUTTON_PLAY
117 #define BTN_QUIT BUTTON_POWER
118 #define BTN_STOPRESET BUTTON_REC
120 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
122 #define BTN_DIR_UP BUTTON_UP
123 #define BTN_DIR_DOWN BUTTON_DOWN
124 #define BTN_DIR_LEFT BUTTON_LEFT
125 #define BTN_DIR_RIGHT BUTTON_RIGHT
126 #define BTN_STARTPAUSE BUTTON_SELECT
127 #define BTN_QUIT BUTTON_POWER
128 #define BTN_STOPRESET BUTTON_A
130 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
131 (CONFIG_KEYPAD == SANSA_C200_PAD)
133 #define BTN_DIR_UP BUTTON_UP
134 #define BTN_DIR_DOWN BUTTON_DOWN
135 #define BTN_DIR_LEFT BUTTON_LEFT
136 #define BTN_DIR_RIGHT BUTTON_RIGHT
137 #define BTN_STARTPAUSE BUTTON_SELECT
138 #define BTN_QUIT BUTTON_POWER
139 #define BTN_STOPRESET BUTTON_REC
141 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
143 #define BTN_DIR_UP BUTTON_UP
144 #define BTN_DIR_DOWN BUTTON_DOWN
145 #define BTN_DIR_LEFT BUTTON_LEFT
146 #define BTN_DIR_RIGHT BUTTON_RIGHT
147 #define BTN_STARTPAUSE BUTTON_SELECT
148 #define BTN_QUIT BUTTON_POWER
149 #define BTN_STOPRESET BUTTON_HOME
151 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
153 #define BTN_DIR_UP BUTTON_UP
154 #define BTN_DIR_DOWN BUTTON_DOWN
155 #define BTN_DIR_LEFT BUTTON_LEFT
156 #define BTN_DIR_RIGHT BUTTON_RIGHT
157 #define BTN_STARTPAUSE BUTTON_SELECT
158 #define BTN_QUIT (BUTTON_HOME|BUTTON_REPEAT)
159 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
161 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
163 #define BTN_DIR_UP BUTTON_UP
164 #define BTN_DIR_DOWN BUTTON_DOWN
165 #define BTN_DIR_LEFT BUTTON_LEFT
166 #define BTN_DIR_RIGHT BUTTON_RIGHT
167 #define BTN_STARTPAUSE (BUTTON_SELECT | BUTTON_REL)
168 #define BTN_QUIT BUTTON_POWER
169 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
171 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
173 #define BTN_DIR_UP BUTTON_SCROLL_UP
174 #define BTN_DIR_DOWN BUTTON_SCROLL_DOWN
175 #define BTN_DIR_LEFT BUTTON_LEFT
176 #define BTN_DIR_RIGHT BUTTON_RIGHT
177 #define BTN_STARTPAUSE BUTTON_PLAY
178 #define BTN_QUIT BUTTON_POWER
179 #define BTN_STOPRESET BUTTON_REW
181 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
183 #define BTN_DIR_UP BUTTON_UP
184 #define BTN_DIR_DOWN BUTTON_DOWN
185 #define BTN_DIR_LEFT BUTTON_LEFT
186 #define BTN_DIR_RIGHT BUTTON_RIGHT
187 #define BTN_STARTPAUSE BUTTON_SELECT
188 #define BTN_QUIT BUTTON_BACK
189 #define BTN_STOPRESET BUTTON_MENU
191 #elif (CONFIG_KEYPAD == MROBE100_PAD)
193 #define BTN_DIR_UP BUTTON_UP
194 #define BTN_DIR_DOWN BUTTON_DOWN
195 #define BTN_DIR_LEFT BUTTON_LEFT
196 #define BTN_DIR_RIGHT BUTTON_RIGHT
197 #define BTN_STARTPAUSE BUTTON_SELECT
198 #define BTN_QUIT BUTTON_POWER
199 #define BTN_STOPRESET BUTTON_DISPLAY
201 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
203 #define BTN_DIR_UP BUTTON_RC_VOL_UP
204 #define BTN_DIR_DOWN BUTTON_RC_VOL_DOWN
205 #define BTN_DIR_LEFT BUTTON_RC_REW
206 #define BTN_DIR_RIGHT BUTTON_RC_FF
207 #define BTN_STARTPAUSE BUTTON_RC_PLAY
208 #define BTN_QUIT BUTTON_RC_REC
209 #define BTN_STOPRESET BUTTON_RC_MODE
211 #elif (CONFIG_KEYPAD == COWOND2_PAD)
213 #define BTN_QUIT BUTTON_POWER
215 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
217 #define BTN_DIR_UP BUTTON_UP
218 #define BTN_DIR_DOWN BUTTON_DOWN
219 #define BTN_DIR_LEFT BUTTON_LEFT
220 #define BTN_DIR_RIGHT BUTTON_RIGHT
221 #define BTN_STARTPAUSE BUTTON_PLAY
222 #define BTN_QUIT BUTTON_BACK
223 #define BTN_STOPRESET BUTTON_MENU
225 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
227 #define BTN_DIR_UP BUTTON_UP
228 #define BTN_DIR_DOWN BUTTON_DOWN
229 #define BTN_DIR_LEFT BUTTON_LEFT
230 #define BTN_DIR_RIGHT BUTTON_RIGHT
231 #define BTN_STARTPAUSE BUTTON_MENU
232 #define BTN_QUIT BUTTON_POWER
233 #define BTN_STOPRESET BUTTON_VIEW
235 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
236 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
237 CONFIG_KEYPAD == MROBE500_PAD
239 #define BTN_QUIT BUTTON_POWER
241 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
243 #define BTN_DIR_UP BUTTON_UP
244 #define BTN_DIR_DOWN BUTTON_DOWN
245 #define BTN_DIR_LEFT BUTTON_LEFT
246 #define BTN_DIR_RIGHT BUTTON_RIGHT
247 #define BTN_STARTPAUSE BUTTON_PLAY
248 #define BTN_QUIT BUTTON_FFWD
249 #define BTN_STOPRESET BUTTON_REW
251 #else
252 #error No keymap defined!
253 #endif
255 #ifdef HAVE_TOUCHSCREEN
256 #ifndef BTN_DIR_UP
257 #define BTN_DIR_UP BUTTON_TOPMIDDLE
258 #endif
259 #ifndef BTN_DIR_DOWN
260 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
261 #endif
262 #ifndef BTN_DIR_LEFT
263 #define BTN_DIR_LEFT BUTTON_MIDLEFT
264 #endif
265 #ifndef BTN_DIR_RIGHT
266 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
267 #endif
268 #ifndef BTN_STARTPAUSE
269 #define BTN_STARTPAUSE BUTTON_CENTER
270 #endif
271 #ifndef BTN_QUIT
272 #define BTN_QUIT BUTTON_TOPLEFT
273 #endif
274 #ifndef BTN_STOPRESET
275 #define BTN_STOPRESET BUTTON_TOPRIGHT
276 #endif
277 #endif
279 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
280 #define FOOD_SIZE 3
281 #define ARGH_SIZE 4
282 #define SPEED 14
283 #define MAX_WORM_SEGMENTS 128
284 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 64)
285 #define FOOD_SIZE 3
286 #define ARGH_SIZE 4
287 #define SPEED 14
288 #define MAX_WORM_SEGMENTS 128
289 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
290 #define FOOD_SIZE 3
291 #define ARGH_SIZE 4
292 #define SPEED 14
293 #define MAX_WORM_SEGMENTS 128
294 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
295 #define FOOD_SIZE 3
296 #define ARGH_SIZE 4
297 #define SPEED 12
298 #define MAX_WORM_SEGMENTS 128
299 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
300 #define FOOD_SIZE 4
301 #define ARGH_SIZE 5
302 #define SPEED 10
303 #define MAX_WORM_SEGMENTS 128
304 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
305 #define FOOD_SIZE 4
306 #define ARGH_SIZE 5
307 #define SPEED 9
308 #define MAX_WORM_SEGMENTS 128
309 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
310 #define FOOD_SIZE 4
311 #define ARGH_SIZE 5
312 #define SPEED 8
313 #define MAX_WORM_SEGMENTS 256
314 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
315 #define FOOD_SIZE 4
316 #define ARGH_SIZE 5
317 #define SPEED 6
318 #define MAX_WORM_SEGMENTS 256
319 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
320 #define FOOD_SIZE 5
321 #define ARGH_SIZE 6
322 #define SPEED 4
323 #define MAX_WORM_SEGMENTS 512
324 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
325 #define FOOD_SIZE 5
326 #define ARGH_SIZE 6
327 #define SPEED 4
328 #define MAX_WORM_SEGMENTS 512
329 #elif ((LCD_WIDTH == 320) && (LCD_HEIGHT == 240)) || \
330 ((LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400)))
331 #define FOOD_SIZE 7
332 #define ARGH_SIZE 8
333 #define SPEED 4
334 #define MAX_WORM_SEGMENTS 512
335 #elif ((LCD_WIDTH == 640) && (LCD_HEIGHT == 480)) || \
336 ((LCD_WIDTH == 480) && (LCD_HEIGHT == 640))
337 #define FOOD_SIZE 14
338 #define ARGH_SIZE 16
339 #define SPEED 4
340 #define MAX_WORM_SEGMENTS 512
341 #endif
343 #ifdef HAVE_LCD_COLOR
344 #define COLOR_WORM LCD_RGBPACK(80, 40, 0)
345 #define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
346 #define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
347 #define COLOR_FG LCD_RGBPACK(0, 0, 0)
348 #define COLOR_BG LCD_RGBPACK(181, 199, 231)
349 #endif
351 #define CHECK_SQUARE_COLLISION(x1,y1,s1,x2,y2,s2) (x1+s1>x2)&&(x2+s2>x1)&&(y1+s1>y2)&&(y2+s2>y1)
354 * All the properties that a worm has.
356 static struct worm {
357 /* The worm is stored in a ring of xy coordinates */
358 int x[MAX_WORM_SEGMENTS];
359 int y[MAX_WORM_SEGMENTS];
361 int head; /* index of the head within the buffer */
362 int tail; /* index of the tail within the buffer */
363 int growing; /* number of cyles the worm still keeps growing */
364 bool alive; /* the worms living state */
366 /* direction vector in which the worm moves */
367 int dirx; /* only values -1 0 1 allowed */
368 int diry; /* only values -1 0 1 allowed */
370 /* this method is used to fetch the direction the user
371 has selected. It can be one of the values
372 human_player1, human_player2, remote_player, virtual_player.
373 All these values are fuctions, that can change the direction
374 of the worm */
375 void (*fetch_worm_direction)(struct worm *w);
376 } worms[MAX_WORMS];
378 /* stores the highscore - besides it was scored by a virtual player */
379 static int highscore;
381 #define MAX_FOOD 5 /* maximal number of food items */
383 /* The arrays store the food coordinates */
384 static int foodx[MAX_FOOD];
385 static int foody[MAX_FOOD];
387 #define MAX_ARGH 100 /* maximal number of argh items */
388 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
390 /* The arrays store the argh coordinates */
391 static int arghx[MAX_ARGH];
392 static int arghy[MAX_ARGH];
394 /* the number of arghs that are currently in use */
395 static int argh_count;
397 /* the number of arghs per food, settable by user */
398 static int arghs_per_food = ARGHS_PER_FOOD;
399 /* the size of the argh, settable by user */
400 static int argh_size = ARGH_SIZE;
401 /* the size of the food, settable by user */
402 static int food_size = FOOD_SIZE;
403 /* the speed of the worm, settable by user */
404 static int speed = SPEED;
405 /* the amount a worm grows by eating a food, settable by user */
406 static int worm_food = WORM_PER_FOOD;
408 /* End additional variables */
410 #ifdef DEBUG_WORMLET
411 /* just a buffer used for debug output */
412 static char debugout[15];
413 #endif
415 /* the number of active worms (dead or alive) */
416 static int worm_count = MAX_WORMS;
418 /* in multiplayer mode: en- / disables the remote worm control
419 in singleplayer mode: toggles 4 / 2 button worm control */
420 static bool use_remote = false;
422 /* return values of check_collision */
423 #define COLLISION_NONE 0
424 #define COLLISION_WORM 1
425 #define COLLISION_FOOD 2
426 #define COLLISION_ARGH 3
427 #define COLLISION_FIELD 4
429 /* constants for use as directions.
430 Note that the values are ordered clockwise.
431 Thus increasing / decreasing the values
432 is equivalent to right / left turns. */
433 #define WEST 0
434 #define NORTH 1
435 #define EAST 2
436 #define SOUTH 3
438 /* direction of human player 1 */
439 static int player1_dir = EAST;
440 /* direction of human player 2 */
441 static int player2_dir = EAST;
442 /* direction of human player 3 */
443 static int player3_dir = EAST;
445 /* the number of (human) players that currently
446 control a worm */
447 static int players = 1;
449 #define SETTINGS_VERSION 1
450 #define SETTINGS_MIN_VERSION 1
451 #define SETTINGS_FILENAME "wormlet.cfg"
453 static struct configdata config[] =
455 {TYPE_INT, 0, 1024, { .int_p = &highscore }, "highscore", NULL},
456 {TYPE_INT, 0, 15, { .int_p = &arghs_per_food }, "arghs per food", NULL},
457 {TYPE_INT, 0, 15, { .int_p = &argh_size }, "argh size", NULL},
458 {TYPE_INT, 0, 15, { .int_p = &food_size }, "food size", NULL},
459 {TYPE_INT, 0, 3, { .int_p = &players }, "players", NULL},
460 {TYPE_INT, 0, 3, { .int_p = &worm_count }, "worms", NULL},
461 {TYPE_INT, 0, 20, { .int_p = &speed }, "speed", NULL},
462 {TYPE_INT, 0, 15, { .int_p = &worm_food }, "Worm Growth Per Food", NULL}
465 #ifdef DEBUG_WORMLET
466 static void set_debug_out(char *str){
467 strcpy(debugout, str);
469 #endif
472 * Returns the direction id in which the worm
473 * currently is creeping.
474 * @param struct worm *w The worm that is to be investigated.
475 * w Must not be null.
476 * @return int A value 0 <= value < 4
477 * Note the predefined constants NORTH, SOUTH, EAST, WEST
479 static int get_worm_dir(struct worm *w)
481 int retVal ;
482 if (w->dirx == 0) {
483 if (w->diry == 1) {
484 retVal = SOUTH;
485 } else {
486 retVal = NORTH;
488 } else {
489 if (w->dirx == 1) {
490 retVal = EAST;
491 } else {
492 retVal = WEST;
495 return retVal;
499 * Set the direction of the specified worm with a direction id.
500 * Increasing the value by 1 means to turn the worm direction
501 * to right by 90 degree.
502 * @param struct worm *w The worm that is to be altered. w Must not be null.
503 * @param int dir The new direction in which the worm is to creep.
504 * dir must be 0 <= dir < 4. Use predefined constants
505 * NORTH, SOUTH, EAST, WEST
507 static void set_worm_dir(struct worm *w, int dir)
509 switch (dir) {
510 case WEST:
511 w->dirx = -1;
512 w->diry = 0;
513 break;
514 case NORTH:
515 w->dirx = 0;
516 w->diry = - 1;
517 break;
518 case EAST:
519 w->dirx = 1;
520 w->diry = 0;
521 break;
522 case SOUTH:
523 w->dirx = 0;
524 w->diry = 1;
525 break;
530 * Returns the current length of the worm array. This
531 * is also a value for the number of bends that are in the worm.
532 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
534 static int get_worm_array_length(struct worm *w)
536 /* initial simple calculation will be overwritten if wrong. */
537 int retVal = w->head - w->tail;
539 /* if the worm 'crosses' the boundaries of the ringbuffer */
540 if (retVal < 0) {
541 retVal = w->head + MAX_WORM_SEGMENTS - w->tail;
544 return retVal;
548 * Returns the score the specified worm. The score is the length
549 * of the worm.
550 * @param struct worm *w The worm that is to be investigated.
551 * w must not be null.
552 * @return int The length of the worm (>= 0).
554 static int get_score(struct worm *w)
556 int retval = 0;
557 int length = get_worm_array_length(w);
558 int i;
559 for (i = 0; i < length; i++) {
561 /* The iteration iterates the length of the worm.
562 Here's the conversion to the true indices within the worm arrays. */
563 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
564 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
565 int startx = w->x[linestart];
566 int starty = w->y[linestart];
567 int endx = w->x[lineend];
568 int endy = w->y[lineend];
570 int minimum, maximum;
572 if (startx == endx) {
573 minimum = MIN(starty, endy);
574 maximum = MAX(starty, endy);
575 } else {
576 minimum = MIN(startx, endx);
577 maximum = MAX(startx, endx);
579 retval += abs(maximum - minimum);
581 return retval;
585 * Determines wether the line specified by startx, starty, endx, endy intersects
586 * the rectangle specified by x, y, width, height. Note that the line must be exactly
587 * horizontal or vertical (startx == endx or starty == endy).
588 * @param int startx The x coordinate of the start point of the line.
589 * @param int starty The y coordinate of the start point of the line.
590 * @param int endx The x coordinate of the end point of the line.
591 * @param int endy The y coordinate of the end point of the line.
592 * @param int x The x coordinate of the top left corner of the rectangle.
593 * @param int y The y coordinate of the top left corner of the rectangle.
594 * @param int width The width of the rectangle.
595 * @param int height The height of the rectangle.
596 * @return bool Returns true if the specified line intersects with the recangle.
598 static bool line_in_rect(int startx, int starty, int endx, int endy,
599 int x, int y, int width, int height)
601 bool retval = false;
602 int simple, simplemin, simplemax;
603 int compa, compb, compmin, compmax;
604 int temp;
605 if (startx == endx) {
606 simple = startx;
607 simplemin = x;
608 simplemax = x + width;
610 compa = starty;
611 compb = endy;
612 compmin = y;
613 compmax = y + height;
614 } else {
615 simple = starty;
616 simplemin = y;
617 simplemax = y + height;
619 compa = startx;
620 compb = endx;
621 compmin = x;
622 compmax = x + width;
625 temp = compa;
626 compa = MIN(compa, compb);
627 compb = MAX(temp, compb);
629 if (simplemin <= simple && simple <= simplemax) {
630 if ((compmin <= compa && compa <= compmax) ||
631 (compmin <= compb && compb <= compmax) ||
632 (compa <= compmin && compb >= compmax)) {
633 retval = true;
636 return retval;
640 * Tests wether the specified worm intersects with the rect.
641 * @param struct worm *w The worm to be investigated
642 * @param int x The x coordinate of the top left corner of the rect
643 * @param int y The y coordinate of the top left corner of the rect
644 * @param int widht The width of the rect
645 * @param int height The height of the rect
646 * @return bool Returns true if the worm intersects with the rect
648 static bool worm_in_rect(struct worm *w, int x, int y, int width, int height)
650 bool retval = false;
653 /* get_worm_array_length is expensive -> buffer the value */
654 int wormLength = get_worm_array_length(w);
655 int i;
657 /* test each entry that is part of the worm */
658 for (i = 0; i < wormLength && retval == false; i++) {
660 /* The iteration iterates the length of the worm.
661 Here's the conversion to the true indices within the worm arrays. */
662 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
663 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
664 int startx = w->x[linestart];
665 int starty = w->y[linestart];
666 int endx = w->x[lineend];
667 int endy = w->y[lineend];
669 retval = line_in_rect(startx, starty, endx, endy, x, y, width, height);
672 return retval;
676 * Checks wether a specific food in the food arrays is at the
677 * specified coordinates.
678 * @param int foodIndex The index of the food in the food arrays
679 * @param int x the x coordinate.
680 * @param int y the y coordinate.
681 * @return Returns true if the coordinate hits the food specified by
682 * foodIndex.
684 static bool specific_food_collision(int foodIndex, int x, int y)
686 bool retVal = false;
687 if (x >= foodx[foodIndex] &&
688 x < foodx[foodIndex] + food_size &&
689 y >= foody[foodIndex] &&
690 y < foody[foodIndex] + food_size) {
692 retVal = true;
694 return retVal;
698 * Returns the index of the food that is at the
699 * given coordinates. If no food is at the coordinates
700 * -1 is returned.
701 * @return int -1 <= value < MAX_FOOD
703 static int food_collision(int x, int y)
705 int i = 0;
706 int retVal = -1;
707 for (i = 0; i < MAX_FOOD; i++) {
708 if (specific_food_collision(i, x, y)) {
709 retVal = i;
710 break;
713 return retVal;
717 * Checks wether a specific argh in the argh arrays is at the
718 * specified coordinates.
719 * @param int arghIndex The index of the argh in the argh arrays
720 * @param int x the x coordinate.
721 * @param int y the y coordinate.
722 * @return Returns true if the coordinate hits the argh specified by
723 * arghIndex.
725 static bool specific_argh_collision(int arghIndex, int x, int y)
727 if ( x >= arghx[arghIndex] &&
728 y >= arghy[arghIndex] &&
729 x < arghx[arghIndex] + argh_size &&
730 y < arghy[arghIndex] + argh_size )
732 return true;
735 return false;
739 * Returns the index of the argh that is at the
740 * given coordinates. If no argh is at the coordinates
741 * -1 is returned.
742 * @param int x The x coordinate.
743 * @param int y The y coordinate.
744 * @return int -1 <= value < argh_count <= MAX_ARGH
746 static int argh_collision(int x, int y)
748 int i = 0;
749 int retVal = -1;
751 /* search for the argh that has the specified coords */
752 for (i = 0; i < argh_count; i++) {
753 if (specific_argh_collision(i, x, y)) {
754 retVal = i;
755 break;
758 return retVal;
762 * Checks wether the worm collides with the food at the specfied food-arrays.
763 * @param int foodIndex The index of the food in the arrays. Ensure the value is
764 * 0 <= foodIndex <= MAX_FOOD
765 * @return Returns true if the worm collides with the specified food.
767 static bool worm_food_collision(struct worm *w, int foodIndex)
769 bool retVal = false;
771 retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex],
772 food_size - 1, food_size - 1);
774 return retVal;
778 * Returns true if the worm hits the argh within the next moves (unless
779 * the worm changes it's direction).
780 * @param struct worm *w - The worm to investigate
781 * @param int argh_idx - The index of the argh
782 * @param int moves - The number of moves that are considered.
783 * @return Returns false if the specified argh is not hit within the next
784 * moves.
786 static bool worm_argh_collision_in_moves(struct worm *w, int argh_idx, int moves)
788 bool retVal = false;
789 int x1, y1, x2, y2;
790 x1 = w->x[w->head];
791 y1 = w->y[w->head];
793 x2 = w->x[w->head] + moves * w->dirx;
794 y2 = w->y[w->head] + moves * w->diry;
796 retVal = line_in_rect(x1, y1, x2, y2, arghx[argh_idx], arghy[argh_idx],
797 argh_size, argh_size);
798 return retVal;
802 * Checks wether the worm collides with the argh at the specfied argh-arrays.
803 * @param int arghIndex The index of the argh in the arrays.
804 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
805 * @return Returns true if the worm collides with the specified argh.
807 static bool worm_argh_collision(struct worm *w, int arghIndex)
809 bool retVal = false;
811 retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex],
812 argh_size - 1, argh_size - 1);
814 return retVal;
818 * Find new coordinates for the food stored in foodx[index], foody[index]
819 * that don't collide with any other food or argh
820 * @param int index
821 * Ensure that 0 <= index < MAX_FOOD.
823 static void make_food(int index)
825 int x = 0;
826 int y = 0;
827 bool collisionDetected = false;
828 int i;
830 do {
831 /* make coordinates for a new food so that
832 the entire food lies within the FIELD */
833 x = rb->rand() % (FIELD_RECT_WIDTH - food_size);
834 y = rb->rand() % (FIELD_RECT_HEIGHT - food_size);
835 collisionDetected = false;
836 /* Ensure that the new food doesn't collide with any
837 existing foods or arghs.
838 If the new food hit any existing
839 argh or food a collision is detected.
842 for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
843 collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,foodx[i],foody[i],food_size);
845 for (i=0; i<argh_count && !collisionDetected; i++) {
846 collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,arghx[i],arghy[i],argh_size);
849 /* use coordinates for further testing */
850 foodx[index] = x;
851 foody[index] = y;
853 /* now test wether we accidently hit the worm with food ;) */
854 i = 0;
855 for (i = 0; i < worm_count && !collisionDetected; i++) {
857 collisionDetected = worm_food_collision(&worms[i], index);
861 while (collisionDetected);
862 return;
866 * Clears a food from the lcd buffer.
867 * @param int index The index of the food arrays under which
868 * the coordinates of the desired food can be found. Ensure
869 * that the value is 0 <= index <= MAX_FOOD.
871 static void clear_food(int index)
873 /* remove the old food from the screen */
874 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
875 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
876 foody[index] + FIELD_RECT_Y,
877 food_size, food_size);
878 rb->lcd_set_drawmode(DRMODE_SOLID);
882 * Draws a food in the lcd buffer.
883 * @param int index The index of the food arrays under which
884 * the coordinates of the desired food can be found. Ensure
885 * that the value is 0 <= index <= MAX_FOOD.
887 static void draw_food(int index)
889 /* draw the food object */
890 #ifdef HAVE_LCD_COLOR
891 rb->lcd_set_foreground(COLOR_FOOD);
892 #endif
893 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
894 foody[index] + FIELD_RECT_Y,
895 food_size, food_size);
896 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
897 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X + 1,
898 foody[index] + FIELD_RECT_Y + 1,
899 food_size - 2, food_size - 2);
900 rb->lcd_set_drawmode(DRMODE_SOLID);
901 #ifdef HAVE_LCD_COLOR
902 rb->lcd_set_foreground(COLOR_FG);
903 #endif
907 * Find new coordinates for the argh stored in arghx[index], arghy[index]
908 * that don't collide with any other food or argh.
909 * @param int index
910 * Ensure that 0 <= index < argh_count < MAX_ARGH.
912 static void make_argh(int index)
914 int x = -1;
915 int y = -1;
916 bool collisionDetected = false;
917 int i;
919 do {
920 /* make coordinates for a new argh so that
921 the entire food lies within the FIELD */
922 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
923 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
924 collisionDetected = false;
925 /* Ensure that the new argh doesn't intersect with any
926 existing foods or arghs.
927 If the new argh hit any existing
928 argh or food an intersection is detected.
931 for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
932 collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,foodx[i],foody[i],food_size);
934 for (i=0; i<argh_count && !collisionDetected; i++) {
935 collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,arghx[i],arghy[i],argh_size);
938 /* use the candidate coordinates to make a real argh */
939 arghx[index] = x;
940 arghy[index] = y;
942 /* now test wether we accidently hit the worm with argh ;) */
943 for (i = 0; i < worm_count && !collisionDetected; i++) {
944 collisionDetected |= worm_argh_collision(&worms[i], index);
945 collisionDetected |= worm_argh_collision_in_moves(&worms[i], index,
946 MIN_ARGH_DIST);
949 while (collisionDetected);
950 return;
954 * Draws an argh in the lcd buffer.
955 * @param int index The index of the argh arrays under which
956 * the coordinates of the desired argh can be found. Ensure
957 * that the value is 0 <= index < argh_count <= MAX_ARGH.
959 static void draw_argh(int index)
961 /* draw the new argh */
962 #ifdef HAVE_LCD_COLOR
963 rb->lcd_set_foreground(COLOR_ARGH);
964 #endif
965 rb->lcd_fillrect(arghx[index] + FIELD_RECT_X,
966 arghy[index] + FIELD_RECT_Y,
967 argh_size, argh_size);
968 #ifdef HAVE_LCD_COLOR
969 rb->lcd_set_foreground(COLOR_FG);
970 #endif
973 static void virtual_player(struct worm *w);
975 * Initialzes the specified worm with INITIAL_WORM_LENGTH
976 * and the tail at the specified position. The worm will
977 * be initialized alive and creeping EAST.
978 * @param struct worm *w The worm that is to be initialized
979 * @param int x The x coordinate at which the tail of the worm starts.
980 * x must be 0 <= x < FIELD_RECT_WIDTH.
981 * @param int y The y coordinate at which the tail of the worm starts
982 * y must be 0 <= y < FIELD_RECT_WIDTH.
984 static void init_worm(struct worm *w, int x, int y)
986 /* initialize the worm size */
987 w->head = 1;
988 w->tail = 0;
990 w->x[w->head] = x + 1;
991 w->y[w->head] = y;
993 w->x[w->tail] = x;
994 w->y[w->tail] = y;
996 /* set the initial direction the worm creeps to */
997 w->dirx = 1;
998 w->diry = 0;
1000 w->growing = INITIAL_WORM_LENGTH - 1;
1001 w->alive = true;
1002 w->fetch_worm_direction = virtual_player;
1006 * Writes the direction that was stored for
1007 * human player 1 into the specified worm. This function
1008 * may be used to be stored in worm.fetch_worm_direction.
1009 * The value of
1010 * the direction is read from player1_dir.
1011 * @param struct worm *w - The worm of which the direction
1012 * is altered.
1014 static void human_player1(struct worm *w) {
1015 set_worm_dir(w, player1_dir);
1019 * Writes the direction that was stored for
1020 * human player 2 into the specified worm. This function
1021 * may be used to be stored in worm.fetch_worm_direction.
1022 * The value of
1023 * the direction is read from player2_dir.
1024 * @param struct worm *w - The worm of which the direction
1025 * is altered.
1027 static void human_player2(struct worm *w) {
1028 set_worm_dir(w, player2_dir);
1032 * Writes the direction that was stored for
1033 * human player using a remote control
1034 * into the specified worm. This function
1035 * may be used to be stored in worm.fetch_worm_direction.
1036 * The value of
1037 * the direction is read from player3_dir.
1038 * @param struct worm *w - The worm of which the direction
1039 * is altered.
1041 static void remote_player(struct worm *w) {
1042 set_worm_dir(w, player3_dir);
1046 * Initializes the worm-, food- and argh-arrays, draws a frame,
1047 * makes some food and argh and display all that stuff.
1049 static void init_wormlet(void)
1051 int i;
1053 for (i = 0; i< worm_count; i++) {
1054 /* Initialize all the worm coordinates to center. */
1055 int x = (int)(FIELD_RECT_WIDTH / 2);
1056 int y = (int)((FIELD_RECT_HEIGHT - 20)/ 2) + i * 10;
1058 init_worm(&worms[i], x, y);
1061 player1_dir = EAST;
1062 player2_dir = EAST;
1063 player3_dir = EAST;
1065 if (players > 0) {
1066 worms[0].fetch_worm_direction = human_player1;
1069 if (players > 1) {
1070 if (use_remote) {
1071 worms[1].fetch_worm_direction = remote_player;
1072 } else {
1073 worms[1].fetch_worm_direction = human_player2;
1077 if (players > 2) {
1078 worms[2].fetch_worm_direction = human_player2;
1081 /* Needed when the game is restarted using BTN_STOPRESET */
1082 rb->lcd_clear_display();
1084 /* make and display some food and argh */
1085 argh_count = MAX_FOOD;
1086 for (i = 0; i < MAX_FOOD; i++) {
1087 make_food(i);
1088 draw_food(i);
1089 make_argh(i);
1090 draw_argh(i);
1093 /* draw the game field */
1094 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1095 rb->lcd_fillrect(0, 0, FIELD_RECT_WIDTH + 2, FIELD_RECT_HEIGHT + 2);
1096 rb->lcd_fillrect(1, 1, FIELD_RECT_WIDTH, FIELD_RECT_HEIGHT);
1097 rb->lcd_set_drawmode(DRMODE_SOLID);
1099 /* make everything visible */
1100 rb->lcd_update();
1105 * Move the worm one step further if it is alive.
1106 * The direction in which the worm moves is taken from dirx and diry.
1107 * move_worm decreases growing if > 0. While the worm is growing the tail
1108 * is left untouched.
1109 * @param struct worm *w The worm to move. w must not be NULL.
1111 static void move_worm(struct worm *w)
1113 if (w->alive) {
1114 /* determine the head point and its precessor */
1115 int headx = w->x[w->head];
1116 int heady = w->y[w->head];
1117 int prehead = (w->head + MAX_WORM_SEGMENTS - 1) % MAX_WORM_SEGMENTS;
1118 int preheadx = w->x[prehead];
1119 int preheady = w->y[prehead];
1121 /* determine the old direction */
1122 int olddirx;
1123 int olddiry;
1124 if (headx == preheadx) {
1125 olddirx = 0;
1126 olddiry = (heady > preheady) ? 1 : -1;
1127 } else {
1128 olddiry = 0;
1129 olddirx = (headx > preheadx) ? 1 : -1;
1132 /* olddir == dir?
1133 a change of direction means a new segment
1134 has been opened */
1135 if (olddirx != w->dirx ||
1136 olddiry != w->diry) {
1137 w->head = (w->head + 1) % MAX_WORM_SEGMENTS;
1140 /* new head position */
1141 w->x[w->head] = headx + w->dirx;
1142 w->y[w->head] = heady + w->diry;
1145 /* while the worm is growing no tail procession is necessary */
1146 if (w->growing > 0) {
1147 /* update the worms grow state */
1148 w->growing--;
1151 /* if the worm isn't growing the tail has to be dragged */
1152 else {
1153 /* index of the end of the tail segment */
1154 int tail_segment_end = (w->tail + 1) % MAX_WORM_SEGMENTS;
1156 /* drag the end of the tail */
1157 /* only one coordinate has to be altered. Here it is
1158 determined which one */
1159 int dir = 0; /* specifies wether the coord has to be in- or decreased */
1160 if (w->x[w->tail] == w->x[tail_segment_end]) {
1161 dir = (w->y[w->tail] - w->y[tail_segment_end] < 0) ? 1 : -1;
1162 w->y[w->tail] += dir;
1163 } else {
1164 dir = (w->x[w->tail] - w->x[tail_segment_end] < 0) ? 1 : -1;
1165 w->x[w->tail] += dir;
1168 /* when the tail has been dragged so far that it meets
1169 the next segment start the tail segment is obsolete and
1170 must be freed */
1171 if (w->x[w->tail] == w->x[tail_segment_end] &&
1172 w->y[w->tail] == w->y[tail_segment_end]){
1174 /* drop the last tail point */
1175 w->tail = tail_segment_end;
1182 * Draws the head and clears the tail of the worm in
1183 * the display buffer. lcd_update() is NOT called thus
1184 * the caller has to take care that the buffer is displayed.
1186 static void draw_worm(struct worm *w)
1188 /* draw the new head */
1189 int x = w->x[w->head];
1190 int y = w->y[w->head];
1191 #ifdef HAVE_LCD_COLOR
1192 rb->lcd_set_foreground(COLOR_WORM);
1193 #endif
1194 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1195 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1198 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1200 /* clear the space behind the worm */
1201 x = w->x[w->tail] ;
1202 y = w->y[w->tail] ;
1203 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1204 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1206 rb->lcd_set_drawmode(DRMODE_SOLID);
1207 #ifdef HAVE_LCD_COLOR
1208 rb->lcd_set_foreground(COLOR_FG);
1209 #endif
1213 * Checks wether the coordinate is part of the worm. Returns
1214 * true if any part of the worm was hit - including the head.
1215 * @param x int The x coordinate
1216 * @param y int The y coordinate
1217 * @return int The index of the worm arrays that contain x, y.
1218 * Returns -1 if the coordinates are not part of the worm.
1220 static int specific_worm_collision(struct worm *w, int x, int y)
1222 int retVal = -1;
1224 /* get_worm_array_length is expensive -> buffer the value */
1225 int wormLength = get_worm_array_length(w);
1226 int i;
1228 /* test each entry that is part of the worm */
1229 for (i = 0; i < wormLength && retVal == -1; i++) {
1231 /* The iteration iterates the length of the worm.
1232 Here's the conversion to the true indices within the worm arrays. */
1233 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
1234 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
1235 bool samex = (w->x[linestart] == x) && (w->x[lineend] == x);
1236 bool samey = (w->y[linestart] == y) && (w->y[lineend] == y);
1237 if (samex || samey){
1238 int test, min, max, tmp;
1240 if (samey) {
1241 min = w->x[linestart];
1242 max = w->x[lineend];
1243 test = x;
1244 } else {
1245 min = w->y[linestart];
1246 max = w->y[lineend];
1247 test = y;
1250 tmp = min;
1251 min = MIN(min, max);
1252 max = MAX(tmp, max);
1254 if (min <= test && test <= max) {
1255 retVal = lineend;
1259 return retVal;
1263 * Increases the length of the specified worm by marking
1264 * that it may grow by len pixels. Note that the worm has
1265 * to move to make the growing happen.
1266 * @param worm *w The worm that is to be altered.
1267 * @param int len A positive value specifying the amount of
1268 * pixels the worm may grow.
1270 static void add_growing(struct worm *w, int len) {
1271 w->growing += len;
1275 * Determins the worm that is at the coordinates x, y. The parameter
1276 * w is a switch parameter that changes the functionality of worm_collision.
1277 * If w is specified and x,y hits the head of w NULL is returned.
1278 * This is a useful way to determine wether the head of w hits
1279 * any worm but including itself but excluding its own head.
1280 * (It hits always its own head ;))
1281 * If w is set to NULL worm_collision returns any worm including all heads
1282 * that is at position of x,y.
1283 * @param struct worm *w The worm of which the head should be excluded in
1284 * the test. w may be set to NULL.
1285 * @param int x The x coordinate that is checked
1286 * @param int y The y coordinate that is checkec
1287 * @return struct worm* The worm that has been hit by x,y. If no worm
1288 * was at the position NULL is returned.
1290 static struct worm* worm_collision(struct worm *w, int x, int y)
1292 struct worm *retVal = NULL;
1293 int i;
1294 for (i = 0; (i < worm_count) && (retVal == NULL); i++) {
1295 int collision_at = specific_worm_collision(&worms[i], x, y);
1296 if (collision_at != -1) {
1297 if (!(w == &worms[i] && collision_at == w->head)){
1298 retVal = &worms[i];
1302 return retVal;
1306 * Returns true if the head of the worm just has
1307 * crossed the field boundaries.
1308 * @return bool true if the worm just has wrapped.
1310 static bool field_collision(struct worm *w)
1312 bool retVal = false;
1313 if ((w->x[w->head] >= FIELD_RECT_WIDTH) ||
1314 (w->y[w->head] >= FIELD_RECT_HEIGHT) ||
1315 (w->x[w->head] < 0) ||
1316 (w->y[w->head] < 0))
1318 retVal = true;
1320 return retVal;
1325 * Returns true if the specified coordinates are within the
1326 * field specified by the FIELD_RECT_XXX constants.
1327 * @param int x The x coordinate of the point that is investigated
1328 * @param int y The y coordinate of the point that is investigated
1329 * @return bool Returns false if x,y specifies a point outside the
1330 * field of worms.
1332 static bool is_in_field_rect(int x, int y)
1334 bool retVal = false;
1335 retVal = (x >= 0 && x < FIELD_RECT_WIDTH &&
1336 y >= 0 && y < FIELD_RECT_HEIGHT);
1337 return retVal;
1341 * Checks and returns wether the head of the w
1342 * is colliding with something currently.
1343 * @return int One of the values:
1344 * COLLISION_NONE
1345 * COLLISION_w
1346 * COLLISION_FOOD
1347 * COLLISION_ARGH
1348 * COLLISION_FIELD
1350 static int check_collision(struct worm *w)
1352 int retVal = COLLISION_NONE;
1354 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL)
1355 retVal = COLLISION_WORM;
1357 if (food_collision(w->x[w->head], w->y[w->head]) >= 0)
1358 retVal = COLLISION_FOOD;
1360 if (argh_collision(w->x[w->head], w->y[w->head]) >= 0)
1361 retVal = COLLISION_ARGH;
1363 if (field_collision(w))
1364 retVal = COLLISION_FIELD;
1366 return retVal;
1370 * Returns the index of the food that is closest to the point
1371 * specified by x, y. This index may be used in the foodx and
1372 * foody arrays.
1373 * @param int x The x coordinate of the point
1374 * @param int y The y coordinate of the point
1375 * @return int A value usable as index in foodx and foody.
1377 static int get_nearest_food(int x, int y)
1379 int nearestfood = 0;
1380 int olddistance = FIELD_RECT_WIDTH + FIELD_RECT_HEIGHT;
1381 int deltax = 0;
1382 int deltay = 0;
1383 int foodindex;
1384 for (foodindex = 0; foodindex < MAX_FOOD; foodindex++) {
1385 int distance;
1386 deltax = foodx[foodindex] - x;
1387 deltay = foody[foodindex] - y;
1388 deltax = deltax > 0 ? deltax : deltax * (-1);
1389 deltay = deltay > 0 ? deltay : deltay * (-1);
1390 distance = deltax + deltay;
1392 if (distance < olddistance) {
1393 olddistance = distance;
1394 nearestfood = foodindex;
1397 return nearestfood;
1401 * Returns wether the specified position is next to the worm
1402 * and in the direction the worm looks. Use this method to
1403 * test wether this position would be hit with the next move of
1404 * the worm unless the worm changes its direction.
1405 * @param struct worm *w - The worm to be investigated
1406 * @param int x - The x coordinate of the position to test.
1407 * @param int y - The y coordinate of the position to test.
1408 * @return Returns true if the worm will hit the position unless
1409 * it change its direction before the next move.
1411 static bool is_in_front_of_worm(struct worm *w, int x, int y)
1413 bool infront = false;
1414 int deltax = x - w->x[w->head];
1415 int deltay = y - w->y[w->head];
1417 if (w->dirx == 0) {
1418 infront = (w->diry * deltay) > 0;
1419 } else {
1420 infront = (w->dirx * deltax) > 0;
1422 return infront;
1426 * Returns true if the worm will collide with the next move unless
1427 * it changes its direction.
1428 * @param struct worm *w - The worm to be investigated.
1429 * @return Returns true if the worm will collide with the next move
1430 * unless it changes its direction.
1432 static bool will_worm_collide(struct worm *w)
1434 int x = w->x[w->head] + w->dirx;
1435 int y = w->y[w->head] + w->diry;
1436 bool retVal = !is_in_field_rect(x, y);
1437 if (!retVal) {
1438 retVal = (argh_collision(x, y) != -1);
1441 if (!retVal) {
1442 retVal = (worm_collision(w, x, y) != NULL);
1444 return retVal;
1448 * This function
1449 * may be used to be stored in worm.fetch_worm_direction for
1450 * worms that are not controlled by humans but by artificial stupidity.
1451 * A direction is searched that doesn't lead to collision but to the nearest
1452 * food - but not very intelligent. The direction is written to the specified
1453 * worm.
1454 * @param struct worm *w - The worm of which the direction
1455 * is altered.
1457 static void virtual_player(struct worm *w)
1459 bool isright;
1460 int plana, planb, planc;
1461 /* find the next lunch */
1462 int nearestfood = get_nearest_food(w->x[w->head], w->y[w->head]);
1464 /* determine in which direction it is */
1466 /* in front of me? */
1467 bool infront = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1469 /* left right of me? */
1470 int olddir = get_worm_dir(w);
1471 set_worm_dir(w, (olddir + 1) % 4);
1472 isright = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1473 set_worm_dir(w, olddir);
1475 /* detect situation, set strategy */
1476 if (infront) {
1477 if (isright) {
1478 plana = olddir;
1479 planb = (olddir + 1) % 4;
1480 planc = (olddir + 3) % 4;
1481 } else {
1482 plana = olddir;
1483 planb = (olddir + 3) % 4;
1484 planc = (olddir + 1) % 4;
1486 } else {
1487 if (isright) {
1488 plana = (olddir + 1) % 4;
1489 planb = olddir;
1490 planc = (olddir + 3) % 4;
1491 } else {
1492 plana = (olddir + 3) % 4;
1493 planb = olddir;
1494 planc = (olddir + 1) % 4;
1498 /* test for collision */
1499 set_worm_dir(w, plana);
1500 if (will_worm_collide(w)){
1502 /* plan b */
1503 set_worm_dir(w, planb);
1505 /* test for collision */
1506 if (will_worm_collide(w)) {
1508 /* plan c */
1509 set_worm_dir(w, planc);
1515 * prints out the score board with all the status information
1516 * about the game.
1518 static void score_board(void)
1520 char buf[15];
1521 char* buf2 = NULL;
1522 int i;
1523 int y = 0;
1524 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1525 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, 0,
1526 LCD_WIDTH - FIELD_RECT_WIDTH - 2, LCD_HEIGHT);
1527 rb->lcd_set_drawmode(DRMODE_SOLID);
1528 for (i = 0; i < worm_count; i++) {
1529 int score = get_score(&worms[i]);
1531 /* high score */
1532 if (worms[i].fetch_worm_direction != virtual_player){
1533 if (highscore < score) {
1534 highscore = score;
1538 /* length */
1539 rb->snprintf(buf, sizeof (buf),"Len:%d", score);
1541 /* worm state */
1542 switch (check_collision(&worms[i])) {
1543 case COLLISION_NONE:
1544 if (worms[i].growing > 0)
1545 buf2 = "Growing";
1546 else {
1547 if (worms[i].alive)
1548 buf2 = "Hungry";
1549 else
1550 buf2 = "Wormed";
1552 break;
1554 case COLLISION_WORM:
1555 buf2 = "Wormed";
1556 break;
1558 case COLLISION_FOOD:
1559 buf2 = "Growing";
1560 break;
1562 case COLLISION_ARGH:
1563 buf2 = "Argh";
1564 break;
1566 case COLLISION_FIELD:
1567 buf2 = "Crashed";
1568 break;
1570 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y , buf);
1571 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y+8, buf2);
1573 if (!worms[i].alive){
1574 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1575 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, y,
1576 LCD_WIDTH - FIELD_RECT_WIDTH - 2, 17);
1577 rb->lcd_set_drawmode(DRMODE_SOLID);
1579 y += 19;
1581 rb->snprintf(buf , sizeof(buf), "Hs: %d", highscore);
1582 #ifndef DEBUG_WORMLET
1583 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, buf);
1584 #else
1585 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout);
1586 #endif
1590 * Checks for collisions of the worm and its environment and
1591 * takes appropriate actions like growing the worm or killing it.
1592 * @return bool Returns true if the worm is dead. Returns
1593 * false if the worm is healthy, up and creeping.
1595 static bool process_collisions(struct worm *w)
1597 int index = -1;
1599 w->alive &= !field_collision(w);
1601 if (w->alive) {
1603 /* check if food was eaten */
1604 index = food_collision(w->x[w->head], w->y[w->head]);
1605 if (index != -1){
1606 int i;
1608 clear_food(index);
1609 make_food(index);
1610 draw_food(index);
1612 for (i = 0; i < arghs_per_food; i++) {
1613 argh_count++;
1614 if (argh_count > MAX_ARGH)
1615 argh_count = MAX_ARGH;
1616 make_argh(argh_count - 1);
1617 draw_argh(argh_count - 1);
1620 add_growing(w, worm_food);
1622 draw_worm(w);
1625 /* check if argh was eaten */
1626 else {
1627 index = argh_collision(w->x[w->head], w->y[w->head]);
1628 if (index != -1) {
1629 w->alive = false;
1631 else {
1632 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) {
1633 w->alive = false;
1638 return !w->alive;
1642 * The main loop of the game.
1643 * @return bool Returns true if the game ended
1644 * with a dead worm. Returns false if the user
1645 * aborted the game manually.
1647 static int run(void)
1649 int button = 0;
1650 int wormDead = false;
1651 bool paused = false;
1653 /* ticks are counted to compensate speed variations */
1654 long cycle_start = 0, cycle_end = 0;
1655 #ifdef DEBUG_WORMLET
1656 int ticks_to_max_cycle_reset = 20;
1657 long max_cycle = 0;
1658 char buf[20];
1659 #endif
1661 /* initialize the board and so on */
1662 init_wormlet();
1664 cycle_start = *rb->current_tick;
1665 /* change the direction of the worm */
1666 while (!wormDead)
1668 int i;
1669 long cycle_duration=0;
1671 #ifdef HAS_BUTTON_HOLD
1672 if (rb->button_hold())
1673 paused = true;
1674 #endif
1676 switch (button) {
1677 case BTN_STARTPAUSE:
1678 paused = !paused;
1679 break;
1680 case BTN_STOPRESET:
1681 if (paused)
1682 return 1; /* restart game */
1683 else
1684 paused = true;
1685 break;
1686 #ifdef BTN_RC_QUIT
1687 case BTN_RC_QUIT:
1688 #endif
1689 case BTN_QUIT:
1690 return 2; /* back to menu */
1691 break;
1693 if (!paused)
1695 switch (button) {
1696 case BTN_DIR_UP:
1697 if (players == 1 && !use_remote) {
1698 player1_dir = NORTH;
1700 break;
1702 case BTN_DIR_DOWN:
1703 if (players == 1 && !use_remote) {
1704 player1_dir = SOUTH;
1706 break;
1708 case BTN_DIR_LEFT:
1709 if (players != 1 || use_remote) {
1710 player1_dir = (player1_dir + 3) % 4;
1711 } else {
1712 player1_dir = WEST;
1714 break;
1716 case BTN_DIR_RIGHT:
1717 if (players != 1 || use_remote) {
1718 player1_dir = (player1_dir + 1) % 4;
1719 } else {
1720 player1_dir = EAST;
1722 break;
1724 #ifdef MULTIPLAYER
1725 case BTN_PLAYER2_DIR1:
1726 player2_dir = (player2_dir + 3) % 4;
1727 break;
1729 case BTN_PLAYER2_DIR2:
1730 player2_dir = (player2_dir + 1) % 4;
1731 break;
1732 #endif
1734 #ifdef REMOTE
1735 case BTN_RC_UP:
1736 player3_dir = (player3_dir + 1) % 4;
1737 break;
1739 case BTN_RC_DOWN:
1740 player3_dir = (player3_dir + 3) % 4;
1741 break;
1742 #endif
1746 for (i = 0; i < worm_count; i++) {
1747 worms[i].fetch_worm_direction(&worms[i]);
1750 wormDead = true;
1751 for (i = 0; i < worm_count; i++){
1752 struct worm *w = &worms[i];
1753 move_worm(w);
1754 wormDead &= process_collisions(w);
1755 draw_worm(w);
1757 score_board();
1758 rb->lcd_update();
1759 if (button == BTN_STOPRESET) {
1760 wormDead = true;
1763 /* here the wormlet game cycle ends
1764 thus the current tick is stored
1765 as end time */
1766 cycle_end = *rb->current_tick;
1768 /* The duration of the game cycle */
1769 cycle_duration = cycle_end - cycle_start;
1770 cycle_duration = MAX(0, cycle_duration);
1771 cycle_duration = MIN(speed -1, cycle_duration);
1774 #ifdef DEBUG_WORMLET
1775 ticks_to_max_cycle_reset--;
1776 if (ticks_to_max_cycle_reset <= 0) {
1777 max_cycle = 0;
1780 if (max_cycle < cycle_duration) {
1781 max_cycle = cycle_duration;
1782 ticks_to_max_cycle_reset = 20;
1784 rb->snprintf(buf, sizeof buf, "ticks %d", max_cycle);
1785 set_debug_out(buf);
1786 #endif
1788 /* adjust the number of ticks to wait for a button.
1789 This ensures that a complete game cycle including
1790 user input runs in constant time */
1791 button = rb->button_get_w_tmo(speed - cycle_duration);
1792 cycle_start = *rb->current_tick;
1795 rb->splash(HZ*2, "Game Over!");
1797 return 2; /* back to menu */
1800 #ifdef DEBUG_WORMLET
1803 * Just a test routine that checks that worm_food_collision works
1804 * in some typical situations.
1806 static void test_worm_food_collision(void)
1808 int collision_count = 0;
1809 int i;
1810 rb->lcd_clear_display();
1811 init_worm(&worms[0], 10, 10);
1812 add_growing(&worms[0], 10);
1813 set_worm_dir(&worms[0], EAST);
1814 for (i = 0; i < 10; i++) {
1815 move_worm(&worms[0]);
1816 draw_worm(&worms[0]);
1819 set_worm_dir(&worms[0], SOUTH);
1820 for (i = 0; i < 10; i++) {
1821 move_worm(&worms[0]);
1822 draw_worm(&worms[0]);
1825 foodx[0] = 15;
1826 foody[0] = 12;
1827 for (foody[0] = 20; foody[0] > 0; foody[0] --) {
1828 char buf[20];
1829 bool collision;
1830 draw_worm(&worms[0]);
1831 draw_food(0);
1832 collision = worm_food_collision(&worms[0], 0);
1833 if (collision) {
1834 collision_count++;
1836 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1837 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1838 rb->lcd_update();
1840 if (collision_count != food_size) {
1841 rb->button_get(true);
1845 foody[0] = 15;
1846 for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) {
1847 char buf[20];
1848 bool collision;
1849 draw_worm(&worms[0]);
1850 draw_food(0);
1851 collision = worm_food_collision(&worms[0], 0);
1852 if (collision) {
1853 collision_count ++;
1855 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1856 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1857 rb->lcd_update();
1859 if (collision_count != food_size * 2) {
1860 rb->button_get(true);
1865 static bool expensive_worm_in_rect(struct worm *w, int rx, int ry, int rw, int rh)
1867 int x, y;
1868 bool retVal = false;
1869 for (x = rx; x < rx + rw; x++){
1870 for (y = ry; y < ry + rh; y++) {
1871 if (specific_worm_collision(w, x, y) != -1) {
1872 retVal = true;
1876 return retVal;
1879 static void test_worm_argh_collision(void)
1881 int i;
1882 int dir;
1883 int collision_count = 0;
1884 rb->lcd_clear_display();
1885 init_worm(&worms[0], 10, 10);
1886 add_growing(&worms[0], 40);
1887 for (dir = 0; dir < 4; dir++) {
1888 set_worm_dir(&worms[0], (EAST + dir) % 4);
1889 for (i = 0; i < 10; i++) {
1890 move_worm(&worms[0]);
1891 draw_worm(&worms[0]);
1895 arghx[0] = 12;
1896 for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){
1897 char buf[20];
1898 bool collision;
1899 draw_argh(0);
1900 collision = worm_argh_collision(&worms[0], 0);
1901 if (collision) {
1902 collision_count ++;
1904 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1905 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1906 rb->lcd_update();
1908 if (collision_count != argh_size * 2) {
1909 rb->button_get(true);
1912 arghy[0] = 12;
1913 for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){
1914 char buf[20];
1915 bool collision;
1916 draw_argh(0);
1917 collision = worm_argh_collision(&worms[0], 0);
1918 if (collision) {
1919 collision_count ++;
1921 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1922 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1923 rb->lcd_update();
1925 if (collision_count != argh_size * 4) {
1926 rb->button_get(true);
1930 static int testline_in_rect(void)
1932 int testfailed = -1;
1934 int rx = 10;
1935 int ry = 15;
1936 int rw = 20;
1937 int rh = 25;
1939 /* Test 1 */
1940 int x1 = 12;
1941 int y1 = 8;
1942 int x2 = 12;
1943 int y2 = 42;
1945 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1946 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1947 rb->lcd_drawrect(rx, ry, rw, rh);
1948 rb->lcd_drawline(x1, y1, x2, y2);
1949 rb->lcd_update();
1950 rb->lcd_putsxy(0, 0, "failed 1");
1951 rb->button_get(true);
1952 testfailed = 1;
1955 /* test 2 */
1956 y2 = 20;
1957 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1958 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1959 rb->lcd_drawrect(rx, ry, rw, rh);
1960 rb->lcd_drawline(x1, y1, x2, y2);
1961 rb->lcd_putsxy(0, 0, "failed 2");
1962 rb->lcd_update();
1963 rb->button_get(true);
1964 testfailed = 2;
1967 /* test 3 */
1968 y1 = 30;
1969 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1970 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1971 rb->lcd_drawrect(rx, ry, rw, rh);
1972 rb->lcd_drawline(x1, y1, x2, y2);
1973 rb->lcd_putsxy(0, 0, "failed 3");
1974 rb->lcd_update();
1975 rb->button_get(true);
1976 testfailed = 3;
1979 /* test 4 */
1980 y2 = 45;
1981 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1982 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1983 rb->lcd_drawrect(rx, ry, rw, rh);
1984 rb->lcd_drawline(x1, y1, x2, y2);
1985 rb->lcd_putsxy(0, 0, "failed 4");
1986 rb->lcd_update();
1987 rb->button_get(true);
1988 testfailed = 4;
1991 /* test 5 */
1992 y1 = 50;
1993 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
1994 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1995 rb->lcd_drawrect(rx, ry, rw, rh);
1996 rb->lcd_drawline(x1, y1, x2, y2);
1997 rb->lcd_putsxy(0, 0, "failed 5");
1998 rb->lcd_update();
1999 rb->button_get(true);
2000 testfailed = 5;
2003 /* test 6 */
2004 y1 = 5;
2005 y2 = 7;
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 6");
2011 rb->lcd_update();
2012 rb->button_get(true);
2013 testfailed = 6;
2016 /* test 7 */
2017 x1 = 8;
2018 y1 = 20;
2019 x2 = 35;
2020 y2 = 20;
2021 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2022 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2023 rb->lcd_drawrect(rx, ry, rw, rh);
2024 rb->lcd_drawline(x1, y1, x2, y2);
2025 rb->lcd_putsxy(0, 0, "failed 7");
2026 rb->lcd_update();
2027 rb->button_get(true);
2028 testfailed = 7;
2031 /* test 8 */
2032 x2 = 12;
2033 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2034 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2035 rb->lcd_drawrect(rx, ry, rw, rh);
2036 rb->lcd_drawline(x1, y1, x2, y2);
2037 rb->lcd_putsxy(0, 0, "failed 8");
2038 rb->lcd_update();
2039 rb->button_get(true);
2040 testfailed = 8;
2043 /* test 9 */
2044 x1 = 25;
2045 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2046 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2047 rb->lcd_drawrect(rx, ry, rw, rh);
2048 rb->lcd_drawline(x1, y1, x2, y2);
2049 rb->lcd_putsxy(0, 0, "failed 9");
2050 rb->lcd_update();
2051 rb->button_get(true);
2052 testfailed = 9;
2055 /* test 10 */
2056 x2 = 37;
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 10");
2062 rb->lcd_update();
2063 rb->button_get(true);
2064 testfailed = 10;
2067 /* test 11 */
2068 x1 = 42;
2069 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2070 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2071 rb->lcd_drawrect(rx, ry, rw, rh);
2072 rb->lcd_drawline(x1, y1, x2, y2);
2073 rb->lcd_putsxy(0, 0, "failed 11");
2074 rb->lcd_update();
2075 rb->button_get(true);
2076 testfailed = 11;
2079 /* test 12 */
2080 x1 = 5;
2081 x2 = 7;
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 12");
2087 rb->lcd_update();
2088 rb->button_get(true);
2089 testfailed = 12;
2092 /* test 13 */
2093 rx = 9;
2094 ry = 15;
2095 rw = food_size;
2096 rh = food_size;
2098 x1 = 10;
2099 y1 = 10;
2100 x2 = 10;
2101 y2 = 20;
2102 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2103 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2104 rb->lcd_drawrect(rx, ry, rw, rh);
2105 rb->lcd_drawline(x1, y1, x2, y2);
2106 rb->lcd_putsxy(0, 0, "failed 13");
2107 rb->lcd_update();
2108 rb->button_get(true);
2109 testfailed = 13;
2112 /* test 14 */
2113 rx = 9;
2114 ry = 15;
2115 rw = 4;
2116 rh = 4;
2118 x1 = 10;
2119 y1 = 10;
2120 x2 = 10;
2121 y2 = 19;
2122 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2123 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2124 rb->lcd_drawline(x1, y1, x2, y2);
2125 rb->lcd_invertrect(rx, ry, rw, rh);
2126 rb->lcd_putsxy(0, 0, "failed 14");
2127 rb->lcd_update();
2128 rb->button_get(true);
2129 testfailed = 14;
2132 rb->lcd_clear_display();
2134 return testfailed;
2138 * Just a test routine to test wether specific_worm_collision might work properly
2140 static int test_specific_worm_collision(void)
2142 int collisions = 0;
2143 int dir;
2144 int x = 0;
2145 int y = 0;
2146 char buf[20];
2147 rb->lcd_clear_display();
2148 init_worm(&worms[0], 10, 20);
2149 add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH);
2151 for (dir = EAST; dir < EAST + 4; dir++) {
2152 int i;
2153 set_worm_dir(&worms[0], dir % 4);
2154 for (i = 0; i < 5; i++) {
2155 if (!(dir % 4 == NORTH && i == 9)) {
2156 move_worm(&worms[0]);
2157 draw_worm(&worms[0]);
2162 for (y = 15; y < 30; y ++){
2163 for (x = 5; x < 20; x++) {
2164 if (specific_worm_collision(&worms[0], x, y) != -1) {
2165 collisions ++;
2167 rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
2168 rb->snprintf(buf, sizeof buf, "collisions %d", collisions);
2169 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2170 rb->lcd_update();
2173 if (collisions != 21) {
2174 rb->button_get(true);
2176 return collisions;
2179 static void test_make_argh(void)
2181 int dir;
2182 int seed = 0;
2183 int hit = 0;
2184 int failures = 0;
2185 int last_failures = 0;
2186 int i, worm_idx;
2187 rb->lcd_clear_display();
2188 worm_count = 3;
2190 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2191 init_worm(&worms[worm_idx], 10 + worm_idx * 20, 20);
2192 add_growing(&worms[worm_idx], 40 - INITIAL_WORM_LENGTH);
2195 for (dir = EAST; dir < EAST + 4; dir++) {
2196 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2197 set_worm_dir(&worms[worm_idx], dir % 4);
2198 for (i = 0; i < 10; i++) {
2199 if (!(dir % 4 == NORTH && i == 9)) {
2200 move_worm(&worms[worm_idx]);
2201 draw_worm(&worms[worm_idx]);
2207 rb->lcd_update();
2209 for (seed = 0; hit < 20; seed += 2) {
2210 char buf[20];
2211 int x, y;
2212 rb->srand(seed);
2213 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
2214 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
2216 for (worm_idx = 0; worm_idx < worm_count; worm_idx++){
2217 if (expensive_worm_in_rect(&worms[worm_idx], x, y, argh_size, argh_size)) {
2218 int tries = 0;
2219 rb->srand(seed);
2221 tries = make_argh(0);
2222 if ((x == arghx[0] && y == arghy[0]) || tries < 2) {
2223 failures ++;
2226 rb->snprintf(buf, sizeof buf, "(%d;%d) fail%d try%d",
2227 x, y, failures, tries);
2228 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2229 rb->lcd_update();
2230 rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y,
2231 argh_size, argh_size);
2232 rb->lcd_update();
2233 draw_argh(0);
2234 rb->lcd_update();
2235 rb->lcd_invertrect(x + FIELD_RECT_X, y + FIELD_RECT_Y,
2236 argh_size, argh_size);
2237 rb->lcd_clearrect(arghx[0] + FIELD_RECT_X, arghy[0] + FIELD_RECT_Y,
2238 argh_size, argh_size);
2240 if (failures > last_failures) {
2241 rb->button_get(true);
2243 last_failures = failures;
2244 hit ++;
2250 static void test_worm_argh_collision_in_moves(void) {
2251 int hit_count = 0;
2252 int i;
2253 rb->lcd_clear_display();
2254 init_worm(&worms[0], 10, 20);
2256 arghx[0] = 20;
2257 arghy[0] = 18;
2258 draw_argh(0);
2260 set_worm_dir(&worms[0], EAST);
2261 for (i = 0; i < 20; i++) {
2262 char buf[20];
2263 move_worm(&worms[0]);
2264 draw_worm(&worms[0]);
2265 if (worm_argh_collision_in_moves(&worms[0], 0, 5)){
2266 hit_count ++;
2268 rb->snprintf(buf, sizeof buf, "in 5 moves hits: %d", hit_count);
2269 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2270 rb->lcd_update();
2272 if (hit_count != argh_size + 5) {
2273 rb->button_get(true);
2276 #endif /* DEBUG_WORMLET */
2279 * Reverts default settings
2281 static void default_settings(void)
2283 arghs_per_food = ARGHS_PER_FOOD;
2284 argh_size = ARGH_SIZE;
2285 food_size = FOOD_SIZE;
2286 speed = SPEED;
2287 worm_food = WORM_PER_FOOD;
2288 players = 1;
2289 worm_count = MAX_WORMS;
2290 use_remote = false;
2291 return;
2295 * Launches the wormlet game
2297 static bool launch_wormlet(void)
2299 int game_result = 1;
2301 rb->lcd_clear_display();
2303 /* Turn off backlight timeout */
2304 backlight_force_on(); /* backlight control in lib/helper.c */
2306 /* start the game */
2307 while (game_result == 1)
2308 game_result = run();
2310 switch (game_result)
2312 case 2:
2313 /* Turn on backlight timeout (revert to settings) */
2314 backlight_use_settings(); /* backlight control in lib/helper.c */
2315 return false;
2316 break;
2318 return false;
2322 * Main entry point
2324 enum plugin_status plugin_start(const void* parameter)
2326 int result;
2327 int menu_quit = 0;
2328 int new_setting;
2330 (void)(parameter);
2332 default_settings();
2333 if (configfile_load(SETTINGS_FILENAME, config,
2334 sizeof(config)/sizeof(*config),
2335 SETTINGS_MIN_VERSION ) < 0)
2337 /* If the loading failed, save a new config file (as the disk is
2338 already spinning) */
2339 configfile_save(SETTINGS_FILENAME, config,
2340 sizeof(config)/sizeof(*config),
2341 SETTINGS_VERSION);
2344 #ifdef HAVE_LCD_COLOR
2345 rb->lcd_set_foreground(COLOR_FG);
2346 rb->lcd_set_background(COLOR_BG);
2347 #endif
2349 #if LCD_DEPTH > 1
2350 rb->lcd_set_backdrop(NULL);
2351 #endif
2353 #ifdef DEBUG_WORMLET
2354 testline_in_rect();
2355 test_worm_argh_collision_in_moves();
2356 test_make_argh();
2357 test_worm_food_collision();
2358 test_worm_argh_collision();
2359 test_specific_worm_collision();
2360 #endif
2362 /* Setup screen */
2364 static const struct opt_items noyes[2] = {
2365 { "No", -1 },
2366 { "Yes", -1 },
2369 static const struct opt_items remoteonly_option[1] = {
2370 { "Remote Control", -1 }
2373 static const struct opt_items key24_option[2] = {
2374 { "4 Key Control", -1 },
2375 { "2 Key Control", -1 }
2378 #ifdef REMOTE
2379 static const struct opt_items remote_option[2] = {
2380 { "Remote Control", -1 },
2381 { "No Rem. Control", -1 }
2383 #else
2384 static const struct opt_items key2_option[1] = {
2385 { "2 Key Control", -1 }
2387 #endif
2389 static const struct opt_items nokey_option[1] = {
2390 { "Out of Control", -1 }
2393 MENUITEM_STRINGLIST(menu, "Wormlet Menu", NULL, "Play Wormlet!",
2394 "Number of Worms", "Number of Players", "Control Style",
2395 "Worm Growth Per Food","Worm Speed","Arghs Per Food",
2396 "Argh Size","Food Size","Revert to Default Settings",
2397 "Playback Control", "Quit");
2399 rb->button_clear_queue();
2401 while (!menu_quit) {
2402 switch(rb->do_menu(&menu, &result, NULL, false))
2404 case 0:
2405 rb->lcd_setfont(FONT_SYSFIXED);
2406 launch_wormlet();
2407 break;
2408 case 1:
2409 rb->set_int("Number of Worms", "", UNIT_INT, &worm_count, NULL,
2410 1, 1, 3, NULL);
2411 if (worm_count < players) {
2412 worm_count = players;
2414 break;
2415 case 2:
2416 #ifdef MULTIPLAYER
2417 rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
2418 1, 0, 4, NULL);
2419 #else
2420 rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
2421 1, 0, 2, NULL);
2422 #endif
2423 if (players > worm_count) {
2424 worm_count = players;
2426 if (players > 2) {
2427 use_remote = true;
2429 break;
2430 case 3:
2431 switch(players) {
2432 case 0:
2433 rb->set_option("Control Style",&use_remote,INT,
2434 nokey_option, 1, NULL);
2435 break;
2436 case 1:
2437 rb->set_option("Control Style",&use_remote,INT,
2438 key24_option, 2, NULL);
2439 break;
2440 case 2:
2441 #ifdef REMOTE
2442 rb->set_option("Control Style",&use_remote,INT,
2443 remote_option, 2, NULL);
2444 #else
2445 rb->set_option("Control Style",&use_remote,INT,
2446 key2_option, 1, NULL);
2447 #endif
2448 break;
2449 case 3:
2450 rb->set_option("Control Style",&use_remote,INT,
2451 remoteonly_option, 1, NULL);
2452 break;
2454 break;
2455 case 4:
2456 rb->set_int("Worm Growth Per Food", "", UNIT_INT, &worm_food,
2457 NULL, 1, 0, 15, NULL);
2458 break;
2459 case 5:
2460 new_setting = 20 - speed;
2461 rb->set_int("Worm Speed", "", UNIT_INT, &new_setting,
2462 NULL, 1, 0, 20, NULL);
2463 speed = 20 - new_setting;
2464 break;
2465 case 6:
2466 rb->set_int("Arghs Per Food", "", UNIT_INT, &arghs_per_food,
2467 NULL, 1, 0, 8, NULL);
2468 break;
2469 case 7:
2470 rb->set_int("Argh Size", "", UNIT_INT, &argh_size,
2471 NULL, 1, 2, 10, NULL);
2472 break;
2473 case 8:
2474 rb->set_int("Food Size", "", UNIT_INT, &food_size,
2475 NULL, 1, 2, 10, NULL);
2476 break;
2477 case 9:
2478 new_setting = 0;
2479 rb->set_option("Reset Settings?", &new_setting, INT, noyes , 2, NULL);
2480 if (new_setting == 1)
2481 default_settings();
2482 break;
2483 case 10:
2484 playback_control(NULL);
2485 break;
2486 default:
2487 menu_quit=1;
2488 break;
2492 configfile_save(SETTINGS_FILENAME, config,
2493 sizeof(config)/sizeof(*config),
2494 SETTINGS_VERSION);
2496 return PLUGIN_OK;