Packard Bell Vibe 500: More plugin keymaps (second portion).
[kugel-rb.git] / apps / plugins / wormlet.c
blob3324d6c507297336da5ea9776c32c5ad2dcf5b72
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 == COWON_D2_PAD)
213 #define BTN_QUIT BUTTON_POWER
215 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
217 #define BTN_DIR_UP BUTTON_UP
218 #define BTN_DIR_DOWN BUTTON_DOWN
219 #define BTN_DIR_LEFT BUTTON_LEFT
220 #define BTN_DIR_RIGHT BUTTON_RIGHT
221 #define BTN_STARTPAUSE BUTTON_PLAY
222 #define BTN_QUIT BUTTON_BACK
223 #define BTN_STOPRESET BUTTON_MENU
225 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
227 #define BTN_DIR_UP BUTTON_UP
228 #define BTN_DIR_DOWN BUTTON_DOWN
229 #define BTN_DIR_LEFT BUTTON_LEFT
230 #define BTN_DIR_RIGHT BUTTON_RIGHT
231 #define BTN_STARTPAUSE BUTTON_MENU
232 #define BTN_QUIT BUTTON_POWER
233 #define BTN_STOPRESET BUTTON_VIEW
235 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
237 #define BTN_DIR_UP BUTTON_UP
238 #define BTN_DIR_DOWN BUTTON_DOWN
239 #define BTN_DIR_LEFT BUTTON_PREV
240 #define BTN_DIR_RIGHT BUTTON_RIGHT
241 #define BTN_STARTPAUSE BUTTON_MENU
242 #define BTN_QUIT BUTTON_POWER
243 #define BTN_STOPRESET BUTTON_RIGHT
245 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
246 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
247 CONFIG_KEYPAD == MROBE500_PAD
249 #define BTN_QUIT BUTTON_POWER
251 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
253 #define BTN_DIR_UP BUTTON_UP
254 #define BTN_DIR_DOWN BUTTON_DOWN
255 #define BTN_DIR_LEFT BUTTON_LEFT
256 #define BTN_DIR_RIGHT BUTTON_RIGHT
257 #define BTN_STARTPAUSE BUTTON_PLAY
258 #define BTN_QUIT BUTTON_FFWD
259 #define BTN_STOPRESET BUTTON_REW
261 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
263 #define BTN_DIR_UP BUTTON_UP
264 #define BTN_DIR_DOWN BUTTON_DOWN
265 #define BTN_DIR_LEFT BUTTON_PREV
266 #define BTN_DIR_RIGHT BUTTON_NEXT
267 #define BTN_STARTPAUSE BUTTON_PLAY
268 #define BTN_QUIT BUTTON_REC
269 #define BTN_STOPRESET BUTTON_CANCEL
271 #else
272 #error No keymap defined!
273 #endif
275 #ifdef HAVE_TOUCHSCREEN
276 #ifndef BTN_DIR_UP
277 #define BTN_DIR_UP BUTTON_TOPMIDDLE
278 #endif
279 #ifndef BTN_DIR_DOWN
280 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
281 #endif
282 #ifndef BTN_DIR_LEFT
283 #define BTN_DIR_LEFT BUTTON_MIDLEFT
284 #endif
285 #ifndef BTN_DIR_RIGHT
286 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
287 #endif
288 #ifndef BTN_STARTPAUSE
289 #define BTN_STARTPAUSE BUTTON_CENTER
290 #endif
291 #ifndef BTN_QUIT
292 #define BTN_QUIT BUTTON_TOPLEFT
293 #endif
294 #ifndef BTN_STOPRESET
295 #define BTN_STOPRESET BUTTON_TOPRIGHT
296 #endif
297 #endif
299 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
300 #define FOOD_SIZE 3
301 #define ARGH_SIZE 4
302 #define SPEED 14
303 #define MAX_WORM_SEGMENTS 128
304 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 64)
305 #define FOOD_SIZE 3
306 #define ARGH_SIZE 4
307 #define SPEED 14
308 #define MAX_WORM_SEGMENTS 128
309 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
310 #define FOOD_SIZE 3
311 #define ARGH_SIZE 4
312 #define SPEED 14
313 #define MAX_WORM_SEGMENTS 128
314 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
315 #define FOOD_SIZE 3
316 #define ARGH_SIZE 4
317 #define SPEED 12
318 #define MAX_WORM_SEGMENTS 128
319 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
320 #define FOOD_SIZE 4
321 #define ARGH_SIZE 5
322 #define SPEED 10
323 #define MAX_WORM_SEGMENTS 128
324 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
325 #define FOOD_SIZE 4
326 #define ARGH_SIZE 5
327 #define SPEED 9
328 #define MAX_WORM_SEGMENTS 128
329 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
330 #define FOOD_SIZE 4
331 #define ARGH_SIZE 5
332 #define SPEED 8
333 #define MAX_WORM_SEGMENTS 256
334 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
335 #define FOOD_SIZE 4
336 #define ARGH_SIZE 5
337 #define SPEED 6
338 #define MAX_WORM_SEGMENTS 256
339 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
340 #define FOOD_SIZE 5
341 #define ARGH_SIZE 6
342 #define SPEED 4
343 #define MAX_WORM_SEGMENTS 512
344 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
345 #define FOOD_SIZE 5
346 #define ARGH_SIZE 6
347 #define SPEED 4
348 #define MAX_WORM_SEGMENTS 512
349 #elif ((LCD_WIDTH == 320) && (LCD_HEIGHT == 240)) || \
350 ((LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400)))
351 #define FOOD_SIZE 7
352 #define ARGH_SIZE 8
353 #define SPEED 4
354 #define MAX_WORM_SEGMENTS 512
355 #elif ((LCD_WIDTH == 640) && (LCD_HEIGHT == 480)) || \
356 ((LCD_WIDTH == 480) && (LCD_HEIGHT == 640))
357 #define FOOD_SIZE 14
358 #define ARGH_SIZE 16
359 #define SPEED 4
360 #define MAX_WORM_SEGMENTS 512
361 #endif
363 #ifdef HAVE_LCD_COLOR
364 #define COLOR_WORM LCD_RGBPACK(80, 40, 0)
365 #define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
366 #define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
367 #define COLOR_FG LCD_RGBPACK(0, 0, 0)
368 #define COLOR_BG LCD_RGBPACK(181, 199, 231)
369 #endif
371 #define CHECK_SQUARE_COLLISION(x1,y1,s1,x2,y2,s2) (x1+s1>x2)&&(x2+s2>x1)&&(y1+s1>y2)&&(y2+s2>y1)
374 * All the properties that a worm has.
376 static struct worm {
377 /* The worm is stored in a ring of xy coordinates */
378 int x[MAX_WORM_SEGMENTS];
379 int y[MAX_WORM_SEGMENTS];
381 int head; /* index of the head within the buffer */
382 int tail; /* index of the tail within the buffer */
383 int growing; /* number of cyles the worm still keeps growing */
384 bool alive; /* the worms living state */
386 /* direction vector in which the worm moves */
387 int dirx; /* only values -1 0 1 allowed */
388 int diry; /* only values -1 0 1 allowed */
390 /* this method is used to fetch the direction the user
391 has selected. It can be one of the values
392 human_player1, human_player2, remote_player, virtual_player.
393 All these values are fuctions, that can change the direction
394 of the worm */
395 void (*fetch_worm_direction)(struct worm *w);
396 } worms[MAX_WORMS];
398 /* stores the highscore - besides it was scored by a virtual player */
399 static int highscore;
401 #define MAX_FOOD 5 /* maximal number of food items */
403 /* The arrays store the food coordinates */
404 static int foodx[MAX_FOOD];
405 static int foody[MAX_FOOD];
407 #define MAX_ARGH 100 /* maximal number of argh items */
408 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
410 /* The arrays store the argh coordinates */
411 static int arghx[MAX_ARGH];
412 static int arghy[MAX_ARGH];
414 /* the number of arghs that are currently in use */
415 static int argh_count;
417 /* the number of arghs per food, settable by user */
418 static int arghs_per_food = ARGHS_PER_FOOD;
419 /* the size of the argh, settable by user */
420 static int argh_size = ARGH_SIZE;
421 /* the size of the food, settable by user */
422 static int food_size = FOOD_SIZE;
423 /* the speed of the worm, settable by user */
424 static int speed = SPEED;
425 /* the amount a worm grows by eating a food, settable by user */
426 static int worm_food = WORM_PER_FOOD;
428 /* End additional variables */
430 #ifdef DEBUG_WORMLET
431 /* just a buffer used for debug output */
432 static char debugout[15];
433 #endif
435 /* the number of active worms (dead or alive) */
436 static int worm_count = MAX_WORMS;
438 /* in multiplayer mode: en- / disables the remote worm control
439 in singleplayer mode: toggles 4 / 2 button worm control */
440 static bool use_remote = false;
442 /* return values of check_collision */
443 #define COLLISION_NONE 0
444 #define COLLISION_WORM 1
445 #define COLLISION_FOOD 2
446 #define COLLISION_ARGH 3
447 #define COLLISION_FIELD 4
449 /* constants for use as directions.
450 Note that the values are ordered clockwise.
451 Thus increasing / decreasing the values
452 is equivalent to right / left turns. */
453 #define WEST 0
454 #define NORTH 1
455 #define EAST 2
456 #define SOUTH 3
458 /* direction of human player 1 */
459 static int player1_dir = EAST;
460 /* direction of human player 2 */
461 static int player2_dir = EAST;
462 /* direction of human player 3 */
463 static int player3_dir = EAST;
465 /* the number of (human) players that currently
466 control a worm */
467 static int players = 1;
469 #define SETTINGS_VERSION 1
470 #define SETTINGS_MIN_VERSION 1
471 #define SETTINGS_FILENAME "wormlet.cfg"
473 static struct configdata config[] =
475 {TYPE_INT, 0, 1024, { .int_p = &highscore }, "highscore", NULL},
476 {TYPE_INT, 0, 15, { .int_p = &arghs_per_food }, "arghs per food", NULL},
477 {TYPE_INT, 0, 15, { .int_p = &argh_size }, "argh size", NULL},
478 {TYPE_INT, 0, 15, { .int_p = &food_size }, "food size", NULL},
479 {TYPE_INT, 0, 3, { .int_p = &players }, "players", NULL},
480 {TYPE_INT, 0, 3, { .int_p = &worm_count }, "worms", NULL},
481 {TYPE_INT, 0, 20, { .int_p = &speed }, "speed", NULL},
482 {TYPE_INT, 0, 15, { .int_p = &worm_food }, "Worm Growth Per Food", NULL}
485 #ifdef DEBUG_WORMLET
486 static void set_debug_out(char *str){
487 strcpy(debugout, str);
489 #endif
492 * Returns the direction id in which the worm
493 * currently is creeping.
494 * @param struct worm *w The worm that is to be investigated.
495 * w Must not be null.
496 * @return int A value 0 <= value < 4
497 * Note the predefined constants NORTH, SOUTH, EAST, WEST
499 static int get_worm_dir(struct worm *w)
501 int retVal ;
502 if (w->dirx == 0) {
503 if (w->diry == 1) {
504 retVal = SOUTH;
505 } else {
506 retVal = NORTH;
508 } else {
509 if (w->dirx == 1) {
510 retVal = EAST;
511 } else {
512 retVal = WEST;
515 return retVal;
519 * Set the direction of the specified worm with a direction id.
520 * Increasing the value by 1 means to turn the worm direction
521 * to right by 90 degree.
522 * @param struct worm *w The worm that is to be altered. w Must not be null.
523 * @param int dir The new direction in which the worm is to creep.
524 * dir must be 0 <= dir < 4. Use predefined constants
525 * NORTH, SOUTH, EAST, WEST
527 static void set_worm_dir(struct worm *w, int dir)
529 switch (dir) {
530 case WEST:
531 w->dirx = -1;
532 w->diry = 0;
533 break;
534 case NORTH:
535 w->dirx = 0;
536 w->diry = - 1;
537 break;
538 case EAST:
539 w->dirx = 1;
540 w->diry = 0;
541 break;
542 case SOUTH:
543 w->dirx = 0;
544 w->diry = 1;
545 break;
550 * Returns the current length of the worm array. This
551 * is also a value for the number of bends that are in the worm.
552 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
554 static int get_worm_array_length(struct worm *w)
556 /* initial simple calculation will be overwritten if wrong. */
557 int retVal = w->head - w->tail;
559 /* if the worm 'crosses' the boundaries of the ringbuffer */
560 if (retVal < 0) {
561 retVal = w->head + MAX_WORM_SEGMENTS - w->tail;
564 return retVal;
568 * Returns the score the specified worm. The score is the length
569 * of the worm.
570 * @param struct worm *w The worm that is to be investigated.
571 * w must not be null.
572 * @return int The length of the worm (>= 0).
574 static int get_score(struct worm *w)
576 int retval = 0;
577 int length = get_worm_array_length(w);
578 int i;
579 for (i = 0; i < length; i++) {
581 /* The iteration iterates the length of the worm.
582 Here's the conversion to the true indices within the worm arrays. */
583 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
584 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
585 int startx = w->x[linestart];
586 int starty = w->y[linestart];
587 int endx = w->x[lineend];
588 int endy = w->y[lineend];
590 int minimum, maximum;
592 if (startx == endx) {
593 minimum = MIN(starty, endy);
594 maximum = MAX(starty, endy);
595 } else {
596 minimum = MIN(startx, endx);
597 maximum = MAX(startx, endx);
599 retval += abs(maximum - minimum);
601 return retval;
605 * Determines wether the line specified by startx, starty, endx, endy intersects
606 * the rectangle specified by x, y, width, height. Note that the line must be exactly
607 * horizontal or vertical (startx == endx or starty == endy).
608 * @param int startx The x coordinate of the start point of the line.
609 * @param int starty The y coordinate of the start point of the line.
610 * @param int endx The x coordinate of the end point of the line.
611 * @param int endy The y coordinate of the end point of the line.
612 * @param int x The x coordinate of the top left corner of the rectangle.
613 * @param int y The y coordinate of the top left corner of the rectangle.
614 * @param int width The width of the rectangle.
615 * @param int height The height of the rectangle.
616 * @return bool Returns true if the specified line intersects with the recangle.
618 static bool line_in_rect(int startx, int starty, int endx, int endy,
619 int x, int y, int width, int height)
621 bool retval = false;
622 int simple, simplemin, simplemax;
623 int compa, compb, compmin, compmax;
624 int temp;
625 if (startx == endx) {
626 simple = startx;
627 simplemin = x;
628 simplemax = x + width;
630 compa = starty;
631 compb = endy;
632 compmin = y;
633 compmax = y + height;
634 } else {
635 simple = starty;
636 simplemin = y;
637 simplemax = y + height;
639 compa = startx;
640 compb = endx;
641 compmin = x;
642 compmax = x + width;
645 temp = compa;
646 compa = MIN(compa, compb);
647 compb = MAX(temp, compb);
649 if (simplemin <= simple && simple <= simplemax) {
650 if ((compmin <= compa && compa <= compmax) ||
651 (compmin <= compb && compb <= compmax) ||
652 (compa <= compmin && compb >= compmax)) {
653 retval = true;
656 return retval;
660 * Tests wether the specified worm intersects with the rect.
661 * @param struct worm *w The worm to be investigated
662 * @param int x The x coordinate of the top left corner of the rect
663 * @param int y The y coordinate of the top left corner of the rect
664 * @param int widht The width of the rect
665 * @param int height The height of the rect
666 * @return bool Returns true if the worm intersects with the rect
668 static bool worm_in_rect(struct worm *w, int x, int y, int width, int height)
670 bool retval = false;
673 /* get_worm_array_length is expensive -> buffer the value */
674 int wormLength = get_worm_array_length(w);
675 int i;
677 /* test each entry that is part of the worm */
678 for (i = 0; i < wormLength && retval == false; i++) {
680 /* The iteration iterates the length of the worm.
681 Here's the conversion to the true indices within the worm arrays. */
682 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
683 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
684 int startx = w->x[linestart];
685 int starty = w->y[linestart];
686 int endx = w->x[lineend];
687 int endy = w->y[lineend];
689 retval = line_in_rect(startx, starty, endx, endy, x, y, width, height);
692 return retval;
696 * Checks wether a specific food in the food arrays is at the
697 * specified coordinates.
698 * @param int foodIndex The index of the food in the food arrays
699 * @param int x the x coordinate.
700 * @param int y the y coordinate.
701 * @return Returns true if the coordinate hits the food specified by
702 * foodIndex.
704 static bool specific_food_collision(int foodIndex, int x, int y)
706 bool retVal = false;
707 if (x >= foodx[foodIndex] &&
708 x < foodx[foodIndex] + food_size &&
709 y >= foody[foodIndex] &&
710 y < foody[foodIndex] + food_size) {
712 retVal = true;
714 return retVal;
718 * Returns the index of the food that is at the
719 * given coordinates. If no food is at the coordinates
720 * -1 is returned.
721 * @return int -1 <= value < MAX_FOOD
723 static int food_collision(int x, int y)
725 int i = 0;
726 int retVal = -1;
727 for (i = 0; i < MAX_FOOD; i++) {
728 if (specific_food_collision(i, x, y)) {
729 retVal = i;
730 break;
733 return retVal;
737 * Checks wether a specific argh in the argh arrays is at the
738 * specified coordinates.
739 * @param int arghIndex The index of the argh in the argh arrays
740 * @param int x the x coordinate.
741 * @param int y the y coordinate.
742 * @return Returns true if the coordinate hits the argh specified by
743 * arghIndex.
745 static bool specific_argh_collision(int arghIndex, int x, int y)
747 if ( x >= arghx[arghIndex] &&
748 y >= arghy[arghIndex] &&
749 x < arghx[arghIndex] + argh_size &&
750 y < arghy[arghIndex] + argh_size )
752 return true;
755 return false;
759 * Returns the index of the argh that is at the
760 * given coordinates. If no argh is at the coordinates
761 * -1 is returned.
762 * @param int x The x coordinate.
763 * @param int y The y coordinate.
764 * @return int -1 <= value < argh_count <= MAX_ARGH
766 static int argh_collision(int x, int y)
768 int i = 0;
769 int retVal = -1;
771 /* search for the argh that has the specified coords */
772 for (i = 0; i < argh_count; i++) {
773 if (specific_argh_collision(i, x, y)) {
774 retVal = i;
775 break;
778 return retVal;
782 * Checks wether the worm collides with the food at the specfied food-arrays.
783 * @param int foodIndex The index of the food in the arrays. Ensure the value is
784 * 0 <= foodIndex <= MAX_FOOD
785 * @return Returns true if the worm collides with the specified food.
787 static bool worm_food_collision(struct worm *w, int foodIndex)
789 bool retVal = false;
791 retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex],
792 food_size - 1, food_size - 1);
794 return retVal;
798 * Returns true if the worm hits the argh within the next moves (unless
799 * the worm changes it's direction).
800 * @param struct worm *w - The worm to investigate
801 * @param int argh_idx - The index of the argh
802 * @param int moves - The number of moves that are considered.
803 * @return Returns false if the specified argh is not hit within the next
804 * moves.
806 static bool worm_argh_collision_in_moves(struct worm *w, int argh_idx, int moves)
808 bool retVal = false;
809 int x1, y1, x2, y2;
810 x1 = w->x[w->head];
811 y1 = w->y[w->head];
813 x2 = w->x[w->head] + moves * w->dirx;
814 y2 = w->y[w->head] + moves * w->diry;
816 retVal = line_in_rect(x1, y1, x2, y2, arghx[argh_idx], arghy[argh_idx],
817 argh_size, argh_size);
818 return retVal;
822 * Checks wether the worm collides with the argh at the specfied argh-arrays.
823 * @param int arghIndex The index of the argh in the arrays.
824 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
825 * @return Returns true if the worm collides with the specified argh.
827 static bool worm_argh_collision(struct worm *w, int arghIndex)
829 bool retVal = false;
831 retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex],
832 argh_size - 1, argh_size - 1);
834 return retVal;
838 * Find new coordinates for the food stored in foodx[index], foody[index]
839 * that don't collide with any other food or argh
840 * @param int index
841 * Ensure that 0 <= index < MAX_FOOD.
843 static void make_food(int index)
845 int x = 0;
846 int y = 0;
847 bool collisionDetected = false;
848 int i;
850 do {
851 /* make coordinates for a new food so that
852 the entire food lies within the FIELD */
853 x = rb->rand() % (FIELD_RECT_WIDTH - food_size);
854 y = rb->rand() % (FIELD_RECT_HEIGHT - food_size);
855 collisionDetected = false;
856 /* Ensure that the new food doesn't collide with any
857 existing foods or arghs.
858 If the new food hit any existing
859 argh or food a collision is detected.
862 for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
863 collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,foodx[i],foody[i],food_size);
865 for (i=0; i<argh_count && !collisionDetected; i++) {
866 collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,arghx[i],arghy[i],argh_size);
869 /* use coordinates for further testing */
870 foodx[index] = x;
871 foody[index] = y;
873 /* now test wether we accidently hit the worm with food ;) */
874 i = 0;
875 for (i = 0; i < worm_count && !collisionDetected; i++) {
877 collisionDetected = worm_food_collision(&worms[i], index);
881 while (collisionDetected);
882 return;
886 * Clears a food from the lcd buffer.
887 * @param int index The index of the food arrays under which
888 * the coordinates of the desired food can be found. Ensure
889 * that the value is 0 <= index <= MAX_FOOD.
891 static void clear_food(int index)
893 /* remove the old food from the screen */
894 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
895 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
896 foody[index] + FIELD_RECT_Y,
897 food_size, food_size);
898 rb->lcd_set_drawmode(DRMODE_SOLID);
902 * Draws a food in the lcd buffer.
903 * @param int index The index of the food arrays under which
904 * the coordinates of the desired food can be found. Ensure
905 * that the value is 0 <= index <= MAX_FOOD.
907 static void draw_food(int index)
909 /* draw the food object */
910 #ifdef HAVE_LCD_COLOR
911 rb->lcd_set_foreground(COLOR_FOOD);
912 #endif
913 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
914 foody[index] + FIELD_RECT_Y,
915 food_size, food_size);
916 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
917 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X + 1,
918 foody[index] + FIELD_RECT_Y + 1,
919 food_size - 2, food_size - 2);
920 rb->lcd_set_drawmode(DRMODE_SOLID);
921 #ifdef HAVE_LCD_COLOR
922 rb->lcd_set_foreground(COLOR_FG);
923 #endif
927 * Find new coordinates for the argh stored in arghx[index], arghy[index]
928 * that don't collide with any other food or argh.
929 * @param int index
930 * Ensure that 0 <= index < argh_count < MAX_ARGH.
932 static void make_argh(int index)
934 int x = -1;
935 int y = -1;
936 bool collisionDetected = false;
937 int i;
939 do {
940 /* make coordinates for a new argh so that
941 the entire food lies within the FIELD */
942 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
943 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
944 collisionDetected = false;
945 /* Ensure that the new argh doesn't intersect with any
946 existing foods or arghs.
947 If the new argh hit any existing
948 argh or food an intersection is detected.
951 for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
952 collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,foodx[i],foody[i],food_size);
954 for (i=0; i<argh_count && !collisionDetected; i++) {
955 collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,arghx[i],arghy[i],argh_size);
958 /* use the candidate coordinates to make a real argh */
959 arghx[index] = x;
960 arghy[index] = y;
962 /* now test wether we accidently hit the worm with argh ;) */
963 for (i = 0; i < worm_count && !collisionDetected; i++) {
964 collisionDetected |= worm_argh_collision(&worms[i], index);
965 collisionDetected |= worm_argh_collision_in_moves(&worms[i], index,
966 MIN_ARGH_DIST);
969 while (collisionDetected);
970 return;
974 * Draws an argh in the lcd buffer.
975 * @param int index The index of the argh arrays under which
976 * the coordinates of the desired argh can be found. Ensure
977 * that the value is 0 <= index < argh_count <= MAX_ARGH.
979 static void draw_argh(int index)
981 /* draw the new argh */
982 #ifdef HAVE_LCD_COLOR
983 rb->lcd_set_foreground(COLOR_ARGH);
984 #endif
985 rb->lcd_fillrect(arghx[index] + FIELD_RECT_X,
986 arghy[index] + FIELD_RECT_Y,
987 argh_size, argh_size);
988 #ifdef HAVE_LCD_COLOR
989 rb->lcd_set_foreground(COLOR_FG);
990 #endif
993 static void virtual_player(struct worm *w);
995 * Initialzes the specified worm with INITIAL_WORM_LENGTH
996 * and the tail at the specified position. The worm will
997 * be initialized alive and creeping EAST.
998 * @param struct worm *w The worm that is to be initialized
999 * @param int x The x coordinate at which the tail of the worm starts.
1000 * x must be 0 <= x < FIELD_RECT_WIDTH.
1001 * @param int y The y coordinate at which the tail of the worm starts
1002 * y must be 0 <= y < FIELD_RECT_WIDTH.
1004 static void init_worm(struct worm *w, int x, int y)
1006 /* initialize the worm size */
1007 w->head = 1;
1008 w->tail = 0;
1010 w->x[w->head] = x + 1;
1011 w->y[w->head] = y;
1013 w->x[w->tail] = x;
1014 w->y[w->tail] = y;
1016 /* set the initial direction the worm creeps to */
1017 w->dirx = 1;
1018 w->diry = 0;
1020 w->growing = INITIAL_WORM_LENGTH - 1;
1021 w->alive = true;
1022 w->fetch_worm_direction = virtual_player;
1026 * Writes the direction that was stored for
1027 * human player 1 into the specified worm. This function
1028 * may be used to be stored in worm.fetch_worm_direction.
1029 * The value of
1030 * the direction is read from player1_dir.
1031 * @param struct worm *w - The worm of which the direction
1032 * is altered.
1034 static void human_player1(struct worm *w) {
1035 set_worm_dir(w, player1_dir);
1039 * Writes the direction that was stored for
1040 * human player 2 into the specified worm. This function
1041 * may be used to be stored in worm.fetch_worm_direction.
1042 * The value of
1043 * the direction is read from player2_dir.
1044 * @param struct worm *w - The worm of which the direction
1045 * is altered.
1047 static void human_player2(struct worm *w) {
1048 set_worm_dir(w, player2_dir);
1052 * Writes the direction that was stored for
1053 * human player using a remote control
1054 * into the specified worm. This function
1055 * may be used to be stored in worm.fetch_worm_direction.
1056 * The value of
1057 * the direction is read from player3_dir.
1058 * @param struct worm *w - The worm of which the direction
1059 * is altered.
1061 static void remote_player(struct worm *w) {
1062 set_worm_dir(w, player3_dir);
1066 * Initializes the worm-, food- and argh-arrays, draws a frame,
1067 * makes some food and argh and display all that stuff.
1069 static void init_wormlet(void)
1071 int i;
1073 for (i = 0; i< worm_count; i++) {
1074 /* Initialize all the worm coordinates to center. */
1075 int x = (int)(FIELD_RECT_WIDTH / 2);
1076 int y = (int)((FIELD_RECT_HEIGHT - 20)/ 2) + i * 10;
1078 init_worm(&worms[i], x, y);
1081 player1_dir = EAST;
1082 player2_dir = EAST;
1083 player3_dir = EAST;
1085 if (players > 0) {
1086 worms[0].fetch_worm_direction = human_player1;
1089 if (players > 1) {
1090 if (use_remote) {
1091 worms[1].fetch_worm_direction = remote_player;
1092 } else {
1093 worms[1].fetch_worm_direction = human_player2;
1097 if (players > 2) {
1098 worms[2].fetch_worm_direction = human_player2;
1101 /* Needed when the game is restarted using BTN_STOPRESET */
1102 rb->lcd_clear_display();
1104 /* make and display some food and argh */
1105 argh_count = MAX_FOOD;
1106 for (i = 0; i < MAX_FOOD; i++) {
1107 make_food(i);
1108 draw_food(i);
1109 make_argh(i);
1110 draw_argh(i);
1113 /* draw the game field */
1114 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1115 rb->lcd_fillrect(0, 0, FIELD_RECT_WIDTH + 2, FIELD_RECT_HEIGHT + 2);
1116 rb->lcd_fillrect(1, 1, FIELD_RECT_WIDTH, FIELD_RECT_HEIGHT);
1117 rb->lcd_set_drawmode(DRMODE_SOLID);
1119 /* make everything visible */
1120 rb->lcd_update();
1125 * Move the worm one step further if it is alive.
1126 * The direction in which the worm moves is taken from dirx and diry.
1127 * move_worm decreases growing if > 0. While the worm is growing the tail
1128 * is left untouched.
1129 * @param struct worm *w The worm to move. w must not be NULL.
1131 static void move_worm(struct worm *w)
1133 if (w->alive) {
1134 /* determine the head point and its precessor */
1135 int headx = w->x[w->head];
1136 int heady = w->y[w->head];
1137 int prehead = (w->head + MAX_WORM_SEGMENTS - 1) % MAX_WORM_SEGMENTS;
1138 int preheadx = w->x[prehead];
1139 int preheady = w->y[prehead];
1141 /* determine the old direction */
1142 int olddirx;
1143 int olddiry;
1144 if (headx == preheadx) {
1145 olddirx = 0;
1146 olddiry = (heady > preheady) ? 1 : -1;
1147 } else {
1148 olddiry = 0;
1149 olddirx = (headx > preheadx) ? 1 : -1;
1152 /* olddir == dir?
1153 a change of direction means a new segment
1154 has been opened */
1155 if (olddirx != w->dirx ||
1156 olddiry != w->diry) {
1157 w->head = (w->head + 1) % MAX_WORM_SEGMENTS;
1160 /* new head position */
1161 w->x[w->head] = headx + w->dirx;
1162 w->y[w->head] = heady + w->diry;
1165 /* while the worm is growing no tail procession is necessary */
1166 if (w->growing > 0) {
1167 /* update the worms grow state */
1168 w->growing--;
1171 /* if the worm isn't growing the tail has to be dragged */
1172 else {
1173 /* index of the end of the tail segment */
1174 int tail_segment_end = (w->tail + 1) % MAX_WORM_SEGMENTS;
1176 /* drag the end of the tail */
1177 /* only one coordinate has to be altered. Here it is
1178 determined which one */
1179 int dir = 0; /* specifies wether the coord has to be in- or decreased */
1180 if (w->x[w->tail] == w->x[tail_segment_end]) {
1181 dir = (w->y[w->tail] - w->y[tail_segment_end] < 0) ? 1 : -1;
1182 w->y[w->tail] += dir;
1183 } else {
1184 dir = (w->x[w->tail] - w->x[tail_segment_end] < 0) ? 1 : -1;
1185 w->x[w->tail] += dir;
1188 /* when the tail has been dragged so far that it meets
1189 the next segment start the tail segment is obsolete and
1190 must be freed */
1191 if (w->x[w->tail] == w->x[tail_segment_end] &&
1192 w->y[w->tail] == w->y[tail_segment_end]){
1194 /* drop the last tail point */
1195 w->tail = tail_segment_end;
1202 * Draws the head and clears the tail of the worm in
1203 * the display buffer. lcd_update() is NOT called thus
1204 * the caller has to take care that the buffer is displayed.
1206 static void draw_worm(struct worm *w)
1208 /* draw the new head */
1209 int x = w->x[w->head];
1210 int y = w->y[w->head];
1211 #ifdef HAVE_LCD_COLOR
1212 rb->lcd_set_foreground(COLOR_WORM);
1213 #endif
1214 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1215 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1218 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1220 /* clear the space behind the worm */
1221 x = w->x[w->tail] ;
1222 y = w->y[w->tail] ;
1223 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1224 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1226 rb->lcd_set_drawmode(DRMODE_SOLID);
1227 #ifdef HAVE_LCD_COLOR
1228 rb->lcd_set_foreground(COLOR_FG);
1229 #endif
1233 * Checks wether the coordinate is part of the worm. Returns
1234 * true if any part of the worm was hit - including the head.
1235 * @param x int The x coordinate
1236 * @param y int The y coordinate
1237 * @return int The index of the worm arrays that contain x, y.
1238 * Returns -1 if the coordinates are not part of the worm.
1240 static int specific_worm_collision(struct worm *w, int x, int y)
1242 int retVal = -1;
1244 /* get_worm_array_length is expensive -> buffer the value */
1245 int wormLength = get_worm_array_length(w);
1246 int i;
1248 /* test each entry that is part of the worm */
1249 for (i = 0; i < wormLength && retVal == -1; i++) {
1251 /* The iteration iterates the length of the worm.
1252 Here's the conversion to the true indices within the worm arrays. */
1253 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
1254 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
1255 bool samex = (w->x[linestart] == x) && (w->x[lineend] == x);
1256 bool samey = (w->y[linestart] == y) && (w->y[lineend] == y);
1257 if (samex || samey){
1258 int test, min, max, tmp;
1260 if (samey) {
1261 min = w->x[linestart];
1262 max = w->x[lineend];
1263 test = x;
1264 } else {
1265 min = w->y[linestart];
1266 max = w->y[lineend];
1267 test = y;
1270 tmp = min;
1271 min = MIN(min, max);
1272 max = MAX(tmp, max);
1274 if (min <= test && test <= max) {
1275 retVal = lineend;
1279 return retVal;
1283 * Increases the length of the specified worm by marking
1284 * that it may grow by len pixels. Note that the worm has
1285 * to move to make the growing happen.
1286 * @param worm *w The worm that is to be altered.
1287 * @param int len A positive value specifying the amount of
1288 * pixels the worm may grow.
1290 static void add_growing(struct worm *w, int len) {
1291 w->growing += len;
1295 * Determins the worm that is at the coordinates x, y. The parameter
1296 * w is a switch parameter that changes the functionality of worm_collision.
1297 * If w is specified and x,y hits the head of w NULL is returned.
1298 * This is a useful way to determine wether the head of w hits
1299 * any worm but including itself but excluding its own head.
1300 * (It hits always its own head ;))
1301 * If w is set to NULL worm_collision returns any worm including all heads
1302 * that is at position of x,y.
1303 * @param struct worm *w The worm of which the head should be excluded in
1304 * the test. w may be set to NULL.
1305 * @param int x The x coordinate that is checked
1306 * @param int y The y coordinate that is checkec
1307 * @return struct worm* The worm that has been hit by x,y. If no worm
1308 * was at the position NULL is returned.
1310 static struct worm* worm_collision(struct worm *w, int x, int y)
1312 struct worm *retVal = NULL;
1313 int i;
1314 for (i = 0; (i < worm_count) && (retVal == NULL); i++) {
1315 int collision_at = specific_worm_collision(&worms[i], x, y);
1316 if (collision_at != -1) {
1317 if (!(w == &worms[i] && collision_at == w->head)){
1318 retVal = &worms[i];
1322 return retVal;
1326 * Returns true if the head of the worm just has
1327 * crossed the field boundaries.
1328 * @return bool true if the worm just has wrapped.
1330 static bool field_collision(struct worm *w)
1332 bool retVal = false;
1333 if ((w->x[w->head] >= FIELD_RECT_WIDTH) ||
1334 (w->y[w->head] >= FIELD_RECT_HEIGHT) ||
1335 (w->x[w->head] < 0) ||
1336 (w->y[w->head] < 0))
1338 retVal = true;
1340 return retVal;
1345 * Returns true if the specified coordinates are within the
1346 * field specified by the FIELD_RECT_XXX constants.
1347 * @param int x The x coordinate of the point that is investigated
1348 * @param int y The y coordinate of the point that is investigated
1349 * @return bool Returns false if x,y specifies a point outside the
1350 * field of worms.
1352 static bool is_in_field_rect(int x, int y)
1354 bool retVal = false;
1355 retVal = (x >= 0 && x < FIELD_RECT_WIDTH &&
1356 y >= 0 && y < FIELD_RECT_HEIGHT);
1357 return retVal;
1361 * Checks and returns wether the head of the w
1362 * is colliding with something currently.
1363 * @return int One of the values:
1364 * COLLISION_NONE
1365 * COLLISION_w
1366 * COLLISION_FOOD
1367 * COLLISION_ARGH
1368 * COLLISION_FIELD
1370 static int check_collision(struct worm *w)
1372 int retVal = COLLISION_NONE;
1374 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL)
1375 retVal = COLLISION_WORM;
1377 if (food_collision(w->x[w->head], w->y[w->head]) >= 0)
1378 retVal = COLLISION_FOOD;
1380 if (argh_collision(w->x[w->head], w->y[w->head]) >= 0)
1381 retVal = COLLISION_ARGH;
1383 if (field_collision(w))
1384 retVal = COLLISION_FIELD;
1386 return retVal;
1390 * Returns the index of the food that is closest to the point
1391 * specified by x, y. This index may be used in the foodx and
1392 * foody arrays.
1393 * @param int x The x coordinate of the point
1394 * @param int y The y coordinate of the point
1395 * @return int A value usable as index in foodx and foody.
1397 static int get_nearest_food(int x, int y)
1399 int nearestfood = 0;
1400 int olddistance = FIELD_RECT_WIDTH + FIELD_RECT_HEIGHT;
1401 int deltax = 0;
1402 int deltay = 0;
1403 int foodindex;
1404 for (foodindex = 0; foodindex < MAX_FOOD; foodindex++) {
1405 int distance;
1406 deltax = foodx[foodindex] - x;
1407 deltay = foody[foodindex] - y;
1408 deltax = deltax > 0 ? deltax : deltax * (-1);
1409 deltay = deltay > 0 ? deltay : deltay * (-1);
1410 distance = deltax + deltay;
1412 if (distance < olddistance) {
1413 olddistance = distance;
1414 nearestfood = foodindex;
1417 return nearestfood;
1421 * Returns wether the specified position is next to the worm
1422 * and in the direction the worm looks. Use this method to
1423 * test wether this position would be hit with the next move of
1424 * the worm unless the worm changes its direction.
1425 * @param struct worm *w - The worm to be investigated
1426 * @param int x - The x coordinate of the position to test.
1427 * @param int y - The y coordinate of the position to test.
1428 * @return Returns true if the worm will hit the position unless
1429 * it change its direction before the next move.
1431 static bool is_in_front_of_worm(struct worm *w, int x, int y)
1433 bool infront = false;
1434 int deltax = x - w->x[w->head];
1435 int deltay = y - w->y[w->head];
1437 if (w->dirx == 0) {
1438 infront = (w->diry * deltay) > 0;
1439 } else {
1440 infront = (w->dirx * deltax) > 0;
1442 return infront;
1446 * Returns true if the worm will collide with the next move unless
1447 * it changes its direction.
1448 * @param struct worm *w - The worm to be investigated.
1449 * @return Returns true if the worm will collide with the next move
1450 * unless it changes its direction.
1452 static bool will_worm_collide(struct worm *w)
1454 int x = w->x[w->head] + w->dirx;
1455 int y = w->y[w->head] + w->diry;
1456 bool retVal = !is_in_field_rect(x, y);
1457 if (!retVal) {
1458 retVal = (argh_collision(x, y) != -1);
1461 if (!retVal) {
1462 retVal = (worm_collision(w, x, y) != NULL);
1464 return retVal;
1468 * This function
1469 * may be used to be stored in worm.fetch_worm_direction for
1470 * worms that are not controlled by humans but by artificial stupidity.
1471 * A direction is searched that doesn't lead to collision but to the nearest
1472 * food - but not very intelligent. The direction is written to the specified
1473 * worm.
1474 * @param struct worm *w - The worm of which the direction
1475 * is altered.
1477 static void virtual_player(struct worm *w)
1479 bool isright;
1480 int plana, planb, planc;
1481 /* find the next lunch */
1482 int nearestfood = get_nearest_food(w->x[w->head], w->y[w->head]);
1484 /* determine in which direction it is */
1486 /* in front of me? */
1487 bool infront = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1489 /* left right of me? */
1490 int olddir = get_worm_dir(w);
1491 set_worm_dir(w, (olddir + 1) % 4);
1492 isright = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1493 set_worm_dir(w, olddir);
1495 /* detect situation, set strategy */
1496 if (infront) {
1497 if (isright) {
1498 plana = olddir;
1499 planb = (olddir + 1) % 4;
1500 planc = (olddir + 3) % 4;
1501 } else {
1502 plana = olddir;
1503 planb = (olddir + 3) % 4;
1504 planc = (olddir + 1) % 4;
1506 } else {
1507 if (isright) {
1508 plana = (olddir + 1) % 4;
1509 planb = olddir;
1510 planc = (olddir + 3) % 4;
1511 } else {
1512 plana = (olddir + 3) % 4;
1513 planb = olddir;
1514 planc = (olddir + 1) % 4;
1518 /* test for collision */
1519 set_worm_dir(w, plana);
1520 if (will_worm_collide(w)){
1522 /* plan b */
1523 set_worm_dir(w, planb);
1525 /* test for collision */
1526 if (will_worm_collide(w)) {
1528 /* plan c */
1529 set_worm_dir(w, planc);
1535 * prints out the score board with all the status information
1536 * about the game.
1538 static void score_board(void)
1540 char buf[15];
1541 char* buf2 = NULL;
1542 int i;
1543 int y = 0;
1544 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1545 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, 0,
1546 LCD_WIDTH - FIELD_RECT_WIDTH - 2, LCD_HEIGHT);
1547 rb->lcd_set_drawmode(DRMODE_SOLID);
1548 for (i = 0; i < worm_count; i++) {
1549 int score = get_score(&worms[i]);
1551 /* high score */
1552 if (worms[i].fetch_worm_direction != virtual_player){
1553 if (highscore < score) {
1554 highscore = score;
1558 /* length */
1559 rb->snprintf(buf, sizeof (buf),"Len:%d", score);
1561 /* worm state */
1562 switch (check_collision(&worms[i])) {
1563 case COLLISION_NONE:
1564 if (worms[i].growing > 0)
1565 buf2 = "Growing";
1566 else {
1567 if (worms[i].alive)
1568 buf2 = "Hungry";
1569 else
1570 buf2 = "Wormed";
1572 break;
1574 case COLLISION_WORM:
1575 buf2 = "Wormed";
1576 break;
1578 case COLLISION_FOOD:
1579 buf2 = "Growing";
1580 break;
1582 case COLLISION_ARGH:
1583 buf2 = "Argh";
1584 break;
1586 case COLLISION_FIELD:
1587 buf2 = "Crashed";
1588 break;
1590 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y , buf);
1591 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y+8, buf2);
1593 if (!worms[i].alive){
1594 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1595 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, y,
1596 LCD_WIDTH - FIELD_RECT_WIDTH - 2, 17);
1597 rb->lcd_set_drawmode(DRMODE_SOLID);
1599 y += 19;
1601 rb->snprintf(buf , sizeof(buf), "Hs: %d", highscore);
1602 #ifndef DEBUG_WORMLET
1603 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, buf);
1604 #else
1605 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout);
1606 #endif
1610 * Checks for collisions of the worm and its environment and
1611 * takes appropriate actions like growing the worm or killing it.
1612 * @return bool Returns true if the worm is dead. Returns
1613 * false if the worm is healthy, up and creeping.
1615 static bool process_collisions(struct worm *w)
1617 int index = -1;
1619 w->alive &= !field_collision(w);
1621 if (w->alive) {
1623 /* check if food was eaten */
1624 index = food_collision(w->x[w->head], w->y[w->head]);
1625 if (index != -1){
1626 int i;
1628 clear_food(index);
1629 make_food(index);
1630 draw_food(index);
1632 for (i = 0; i < arghs_per_food; i++) {
1633 argh_count++;
1634 if (argh_count > MAX_ARGH)
1635 argh_count = MAX_ARGH;
1636 make_argh(argh_count - 1);
1637 draw_argh(argh_count - 1);
1640 add_growing(w, worm_food);
1642 draw_worm(w);
1645 /* check if argh was eaten */
1646 else {
1647 index = argh_collision(w->x[w->head], w->y[w->head]);
1648 if (index != -1) {
1649 w->alive = false;
1651 else {
1652 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) {
1653 w->alive = false;
1658 return !w->alive;
1662 * The main loop of the game.
1663 * @return bool Returns true if the game ended
1664 * with a dead worm. Returns false if the user
1665 * aborted the game manually.
1667 static int run(void)
1669 int button = 0;
1670 int wormDead = false;
1671 bool paused = false;
1673 /* ticks are counted to compensate speed variations */
1674 long cycle_start = 0, cycle_end = 0;
1675 #ifdef DEBUG_WORMLET
1676 int ticks_to_max_cycle_reset = 20;
1677 long max_cycle = 0;
1678 char buf[20];
1679 #endif
1681 /* initialize the board and so on */
1682 init_wormlet();
1684 cycle_start = *rb->current_tick;
1685 /* change the direction of the worm */
1686 while (!wormDead)
1688 int i;
1689 long cycle_duration=0;
1691 #ifdef HAS_BUTTON_HOLD
1692 if (rb->button_hold())
1693 paused = true;
1694 #endif
1696 switch (button) {
1697 case BTN_STARTPAUSE:
1698 paused = !paused;
1699 break;
1700 case BTN_STOPRESET:
1701 if (paused)
1702 return 1; /* restart game */
1703 else
1704 paused = true;
1705 break;
1706 #ifdef BTN_RC_QUIT
1707 case BTN_RC_QUIT:
1708 #endif
1709 case BTN_QUIT:
1710 return 2; /* back to menu */
1711 break;
1713 if (!paused)
1715 switch (button) {
1716 case BTN_DIR_UP:
1717 if (players == 1 && !use_remote) {
1718 player1_dir = NORTH;
1720 break;
1722 case BTN_DIR_DOWN:
1723 if (players == 1 && !use_remote) {
1724 player1_dir = SOUTH;
1726 break;
1728 case BTN_DIR_LEFT:
1729 if (players != 1 || use_remote) {
1730 player1_dir = (player1_dir + 3) % 4;
1731 } else {
1732 player1_dir = WEST;
1734 break;
1736 case BTN_DIR_RIGHT:
1737 if (players != 1 || use_remote) {
1738 player1_dir = (player1_dir + 1) % 4;
1739 } else {
1740 player1_dir = EAST;
1742 break;
1744 #ifdef MULTIPLAYER
1745 case BTN_PLAYER2_DIR1:
1746 player2_dir = (player2_dir + 3) % 4;
1747 break;
1749 case BTN_PLAYER2_DIR2:
1750 player2_dir = (player2_dir + 1) % 4;
1751 break;
1752 #endif
1754 #ifdef REMOTE
1755 case BTN_RC_UP:
1756 player3_dir = (player3_dir + 1) % 4;
1757 break;
1759 case BTN_RC_DOWN:
1760 player3_dir = (player3_dir + 3) % 4;
1761 break;
1762 #endif
1766 for (i = 0; i < worm_count; i++) {
1767 worms[i].fetch_worm_direction(&worms[i]);
1770 wormDead = true;
1771 for (i = 0; i < worm_count; i++){
1772 struct worm *w = &worms[i];
1773 move_worm(w);
1774 wormDead &= process_collisions(w);
1775 draw_worm(w);
1777 score_board();
1778 rb->lcd_update();
1779 if (button == BTN_STOPRESET) {
1780 wormDead = true;
1783 /* here the wormlet game cycle ends
1784 thus the current tick is stored
1785 as end time */
1786 cycle_end = *rb->current_tick;
1788 /* The duration of the game cycle */
1789 cycle_duration = cycle_end - cycle_start;
1790 cycle_duration = MAX(0, cycle_duration);
1791 cycle_duration = MIN(speed -1, cycle_duration);
1794 #ifdef DEBUG_WORMLET
1795 ticks_to_max_cycle_reset--;
1796 if (ticks_to_max_cycle_reset <= 0) {
1797 max_cycle = 0;
1800 if (max_cycle < cycle_duration) {
1801 max_cycle = cycle_duration;
1802 ticks_to_max_cycle_reset = 20;
1804 rb->snprintf(buf, sizeof buf, "ticks %d", max_cycle);
1805 set_debug_out(buf);
1806 #endif
1808 /* adjust the number of ticks to wait for a button.
1809 This ensures that a complete game cycle including
1810 user input runs in constant time */
1811 button = rb->button_get_w_tmo(speed - cycle_duration);
1812 cycle_start = *rb->current_tick;
1815 rb->splash(HZ*2, "Game Over!");
1817 return 2; /* back to menu */
1820 #ifdef DEBUG_WORMLET
1823 * Just a test routine that checks that worm_food_collision works
1824 * in some typical situations.
1826 static void test_worm_food_collision(void)
1828 int collision_count = 0;
1829 int i;
1830 rb->lcd_clear_display();
1831 init_worm(&worms[0], 10, 10);
1832 add_growing(&worms[0], 10);
1833 set_worm_dir(&worms[0], EAST);
1834 for (i = 0; i < 10; i++) {
1835 move_worm(&worms[0]);
1836 draw_worm(&worms[0]);
1839 set_worm_dir(&worms[0], SOUTH);
1840 for (i = 0; i < 10; i++) {
1841 move_worm(&worms[0]);
1842 draw_worm(&worms[0]);
1845 foodx[0] = 15;
1846 foody[0] = 12;
1847 for (foody[0] = 20; foody[0] > 0; foody[0] --) {
1848 char buf[20];
1849 bool collision;
1850 draw_worm(&worms[0]);
1851 draw_food(0);
1852 collision = worm_food_collision(&worms[0], 0);
1853 if (collision) {
1854 collision_count++;
1856 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1857 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1858 rb->lcd_update();
1860 if (collision_count != food_size) {
1861 rb->button_get(true);
1865 foody[0] = 15;
1866 for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) {
1867 char buf[20];
1868 bool collision;
1869 draw_worm(&worms[0]);
1870 draw_food(0);
1871 collision = worm_food_collision(&worms[0], 0);
1872 if (collision) {
1873 collision_count ++;
1875 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1876 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1877 rb->lcd_update();
1879 if (collision_count != food_size * 2) {
1880 rb->button_get(true);
1885 static bool expensive_worm_in_rect(struct worm *w, int rx, int ry, int rw, int rh)
1887 int x, y;
1888 bool retVal = false;
1889 for (x = rx; x < rx + rw; x++){
1890 for (y = ry; y < ry + rh; y++) {
1891 if (specific_worm_collision(w, x, y) != -1) {
1892 retVal = true;
1896 return retVal;
1899 static void test_worm_argh_collision(void)
1901 int i;
1902 int dir;
1903 int collision_count = 0;
1904 rb->lcd_clear_display();
1905 init_worm(&worms[0], 10, 10);
1906 add_growing(&worms[0], 40);
1907 for (dir = 0; dir < 4; dir++) {
1908 set_worm_dir(&worms[0], (EAST + dir) % 4);
1909 for (i = 0; i < 10; i++) {
1910 move_worm(&worms[0]);
1911 draw_worm(&worms[0]);
1915 arghx[0] = 12;
1916 for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){
1917 char buf[20];
1918 bool collision;
1919 draw_argh(0);
1920 collision = worm_argh_collision(&worms[0], 0);
1921 if (collision) {
1922 collision_count ++;
1924 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1925 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1926 rb->lcd_update();
1928 if (collision_count != argh_size * 2) {
1929 rb->button_get(true);
1932 arghy[0] = 12;
1933 for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){
1934 char buf[20];
1935 bool collision;
1936 draw_argh(0);
1937 collision = worm_argh_collision(&worms[0], 0);
1938 if (collision) {
1939 collision_count ++;
1941 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1942 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1943 rb->lcd_update();
1945 if (collision_count != argh_size * 4) {
1946 rb->button_get(true);
1950 static int testline_in_rect(void)
1952 int testfailed = -1;
1954 int rx = 10;
1955 int ry = 15;
1956 int rw = 20;
1957 int rh = 25;
1959 /* Test 1 */
1960 int x1 = 12;
1961 int y1 = 8;
1962 int x2 = 12;
1963 int y2 = 42;
1965 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1966 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1967 rb->lcd_drawrect(rx, ry, rw, rh);
1968 rb->lcd_drawline(x1, y1, x2, y2);
1969 rb->lcd_update();
1970 rb->lcd_putsxy(0, 0, "failed 1");
1971 rb->button_get(true);
1972 testfailed = 1;
1975 /* test 2 */
1976 y2 = 20;
1977 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1978 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1979 rb->lcd_drawrect(rx, ry, rw, rh);
1980 rb->lcd_drawline(x1, y1, x2, y2);
1981 rb->lcd_putsxy(0, 0, "failed 2");
1982 rb->lcd_update();
1983 rb->button_get(true);
1984 testfailed = 2;
1987 /* test 3 */
1988 y1 = 30;
1989 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1990 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1991 rb->lcd_drawrect(rx, ry, rw, rh);
1992 rb->lcd_drawline(x1, y1, x2, y2);
1993 rb->lcd_putsxy(0, 0, "failed 3");
1994 rb->lcd_update();
1995 rb->button_get(true);
1996 testfailed = 3;
1999 /* test 4 */
2000 y2 = 45;
2001 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2002 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2003 rb->lcd_drawrect(rx, ry, rw, rh);
2004 rb->lcd_drawline(x1, y1, x2, y2);
2005 rb->lcd_putsxy(0, 0, "failed 4");
2006 rb->lcd_update();
2007 rb->button_get(true);
2008 testfailed = 4;
2011 /* test 5 */
2012 y1 = 50;
2013 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2014 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2015 rb->lcd_drawrect(rx, ry, rw, rh);
2016 rb->lcd_drawline(x1, y1, x2, y2);
2017 rb->lcd_putsxy(0, 0, "failed 5");
2018 rb->lcd_update();
2019 rb->button_get(true);
2020 testfailed = 5;
2023 /* test 6 */
2024 y1 = 5;
2025 y2 = 7;
2026 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2027 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2028 rb->lcd_drawrect(rx, ry, rw, rh);
2029 rb->lcd_drawline(x1, y1, x2, y2);
2030 rb->lcd_putsxy(0, 0, "failed 6");
2031 rb->lcd_update();
2032 rb->button_get(true);
2033 testfailed = 6;
2036 /* test 7 */
2037 x1 = 8;
2038 y1 = 20;
2039 x2 = 35;
2040 y2 = 20;
2041 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2042 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2043 rb->lcd_drawrect(rx, ry, rw, rh);
2044 rb->lcd_drawline(x1, y1, x2, y2);
2045 rb->lcd_putsxy(0, 0, "failed 7");
2046 rb->lcd_update();
2047 rb->button_get(true);
2048 testfailed = 7;
2051 /* test 8 */
2052 x2 = 12;
2053 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2054 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2055 rb->lcd_drawrect(rx, ry, rw, rh);
2056 rb->lcd_drawline(x1, y1, x2, y2);
2057 rb->lcd_putsxy(0, 0, "failed 8");
2058 rb->lcd_update();
2059 rb->button_get(true);
2060 testfailed = 8;
2063 /* test 9 */
2064 x1 = 25;
2065 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2066 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2067 rb->lcd_drawrect(rx, ry, rw, rh);
2068 rb->lcd_drawline(x1, y1, x2, y2);
2069 rb->lcd_putsxy(0, 0, "failed 9");
2070 rb->lcd_update();
2071 rb->button_get(true);
2072 testfailed = 9;
2075 /* test 10 */
2076 x2 = 37;
2077 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2078 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2079 rb->lcd_drawrect(rx, ry, rw, rh);
2080 rb->lcd_drawline(x1, y1, x2, y2);
2081 rb->lcd_putsxy(0, 0, "failed 10");
2082 rb->lcd_update();
2083 rb->button_get(true);
2084 testfailed = 10;
2087 /* test 11 */
2088 x1 = 42;
2089 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2090 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2091 rb->lcd_drawrect(rx, ry, rw, rh);
2092 rb->lcd_drawline(x1, y1, x2, y2);
2093 rb->lcd_putsxy(0, 0, "failed 11");
2094 rb->lcd_update();
2095 rb->button_get(true);
2096 testfailed = 11;
2099 /* test 12 */
2100 x1 = 5;
2101 x2 = 7;
2102 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2103 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2104 rb->lcd_drawrect(rx, ry, rw, rh);
2105 rb->lcd_drawline(x1, y1, x2, y2);
2106 rb->lcd_putsxy(0, 0, "failed 12");
2107 rb->lcd_update();
2108 rb->button_get(true);
2109 testfailed = 12;
2112 /* test 13 */
2113 rx = 9;
2114 ry = 15;
2115 rw = food_size;
2116 rh = food_size;
2118 x1 = 10;
2119 y1 = 10;
2120 x2 = 10;
2121 y2 = 20;
2122 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2123 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2124 rb->lcd_drawrect(rx, ry, rw, rh);
2125 rb->lcd_drawline(x1, y1, x2, y2);
2126 rb->lcd_putsxy(0, 0, "failed 13");
2127 rb->lcd_update();
2128 rb->button_get(true);
2129 testfailed = 13;
2132 /* test 14 */
2133 rx = 9;
2134 ry = 15;
2135 rw = 4;
2136 rh = 4;
2138 x1 = 10;
2139 y1 = 10;
2140 x2 = 10;
2141 y2 = 19;
2142 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2143 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2144 rb->lcd_drawline(x1, y1, x2, y2);
2145 rb->lcd_invertrect(rx, ry, rw, rh);
2146 rb->lcd_putsxy(0, 0, "failed 14");
2147 rb->lcd_update();
2148 rb->button_get(true);
2149 testfailed = 14;
2152 rb->lcd_clear_display();
2154 return testfailed;
2158 * Just a test routine to test wether specific_worm_collision might work properly
2160 static int test_specific_worm_collision(void)
2162 int collisions = 0;
2163 int dir;
2164 int x = 0;
2165 int y = 0;
2166 char buf[20];
2167 rb->lcd_clear_display();
2168 init_worm(&worms[0], 10, 20);
2169 add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH);
2171 for (dir = EAST; dir < EAST + 4; dir++) {
2172 int i;
2173 set_worm_dir(&worms[0], dir % 4);
2174 for (i = 0; i < 5; i++) {
2175 if (!(dir % 4 == NORTH && i == 9)) {
2176 move_worm(&worms[0]);
2177 draw_worm(&worms[0]);
2182 for (y = 15; y < 30; y ++){
2183 for (x = 5; x < 20; x++) {
2184 if (specific_worm_collision(&worms[0], x, y) != -1) {
2185 collisions ++;
2187 rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
2188 rb->snprintf(buf, sizeof buf, "collisions %d", collisions);
2189 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2190 rb->lcd_update();
2193 if (collisions != 21) {
2194 rb->button_get(true);
2196 return collisions;
2199 static void test_make_argh(void)
2201 int dir;
2202 int seed = 0;
2203 int hit = 0;
2204 int failures = 0;
2205 int last_failures = 0;
2206 int i, worm_idx;
2207 rb->lcd_clear_display();
2208 worm_count = 3;
2210 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2211 init_worm(&worms[worm_idx], 10 + worm_idx * 20, 20);
2212 add_growing(&worms[worm_idx], 40 - INITIAL_WORM_LENGTH);
2215 for (dir = EAST; dir < EAST + 4; dir++) {
2216 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2217 set_worm_dir(&worms[worm_idx], dir % 4);
2218 for (i = 0; i < 10; i++) {
2219 if (!(dir % 4 == NORTH && i == 9)) {
2220 move_worm(&worms[worm_idx]);
2221 draw_worm(&worms[worm_idx]);
2227 rb->lcd_update();
2229 for (seed = 0; hit < 20; seed += 2) {
2230 char buf[20];
2231 int x, y;
2232 rb->srand(seed);
2233 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
2234 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
2236 for (worm_idx = 0; worm_idx < worm_count; worm_idx++){
2237 if (expensive_worm_in_rect(&worms[worm_idx], x, y, argh_size, argh_size)) {
2238 int tries = 0;
2239 rb->srand(seed);
2241 tries = make_argh(0);
2242 if ((x == arghx[0] && y == arghy[0]) || tries < 2) {
2243 failures ++;
2246 rb->snprintf(buf, sizeof buf, "(%d;%d) fail%d try%d",
2247 x, y, failures, tries);
2248 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2249 rb->lcd_update();
2250 rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y,
2251 argh_size, argh_size);
2252 rb->lcd_update();
2253 draw_argh(0);
2254 rb->lcd_update();
2255 rb->lcd_invertrect(x + FIELD_RECT_X, y + FIELD_RECT_Y,
2256 argh_size, argh_size);
2257 rb->lcd_clearrect(arghx[0] + FIELD_RECT_X, arghy[0] + FIELD_RECT_Y,
2258 argh_size, argh_size);
2260 if (failures > last_failures) {
2261 rb->button_get(true);
2263 last_failures = failures;
2264 hit ++;
2270 static void test_worm_argh_collision_in_moves(void) {
2271 int hit_count = 0;
2272 int i;
2273 rb->lcd_clear_display();
2274 init_worm(&worms[0], 10, 20);
2276 arghx[0] = 20;
2277 arghy[0] = 18;
2278 draw_argh(0);
2280 set_worm_dir(&worms[0], EAST);
2281 for (i = 0; i < 20; i++) {
2282 char buf[20];
2283 move_worm(&worms[0]);
2284 draw_worm(&worms[0]);
2285 if (worm_argh_collision_in_moves(&worms[0], 0, 5)){
2286 hit_count ++;
2288 rb->snprintf(buf, sizeof buf, "in 5 moves hits: %d", hit_count);
2289 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2290 rb->lcd_update();
2292 if (hit_count != argh_size + 5) {
2293 rb->button_get(true);
2296 #endif /* DEBUG_WORMLET */
2299 * Reverts default settings
2301 static void default_settings(void)
2303 arghs_per_food = ARGHS_PER_FOOD;
2304 argh_size = ARGH_SIZE;
2305 food_size = FOOD_SIZE;
2306 speed = SPEED;
2307 worm_food = WORM_PER_FOOD;
2308 players = 1;
2309 worm_count = MAX_WORMS;
2310 use_remote = false;
2311 return;
2315 * Launches the wormlet game
2317 static bool launch_wormlet(void)
2319 int game_result = 1;
2321 rb->lcd_clear_display();
2323 /* Turn off backlight timeout */
2324 backlight_force_on(); /* backlight control in lib/helper.c */
2326 /* start the game */
2327 while (game_result == 1)
2328 game_result = run();
2330 switch (game_result)
2332 case 2:
2333 /* Turn on backlight timeout (revert to settings) */
2334 backlight_use_settings(); /* backlight control in lib/helper.c */
2335 return false;
2336 break;
2338 return false;
2342 * Main entry point
2344 enum plugin_status plugin_start(const void* parameter)
2346 int result;
2347 int menu_quit = 0;
2348 int new_setting;
2350 (void)(parameter);
2352 default_settings();
2353 if (configfile_load(SETTINGS_FILENAME, config,
2354 sizeof(config)/sizeof(*config),
2355 SETTINGS_MIN_VERSION ) < 0)
2357 /* If the loading failed, save a new config file (as the disk is
2358 already spinning) */
2359 configfile_save(SETTINGS_FILENAME, config,
2360 sizeof(config)/sizeof(*config),
2361 SETTINGS_VERSION);
2364 #ifdef HAVE_LCD_COLOR
2365 rb->lcd_set_foreground(COLOR_FG);
2366 rb->lcd_set_background(COLOR_BG);
2367 #endif
2369 #if LCD_DEPTH > 1
2370 rb->lcd_set_backdrop(NULL);
2371 #endif
2373 #ifdef DEBUG_WORMLET
2374 testline_in_rect();
2375 test_worm_argh_collision_in_moves();
2376 test_make_argh();
2377 test_worm_food_collision();
2378 test_worm_argh_collision();
2379 test_specific_worm_collision();
2380 #endif
2382 /* Setup screen */
2384 static const struct opt_items noyes[2] = {
2385 { "No", -1 },
2386 { "Yes", -1 },
2389 static const struct opt_items remoteonly_option[1] = {
2390 { "Remote Control", -1 }
2393 static const struct opt_items key24_option[2] = {
2394 { "4 Key Control", -1 },
2395 { "2 Key Control", -1 }
2398 #ifdef REMOTE
2399 static const struct opt_items remote_option[2] = {
2400 { "Remote Control", -1 },
2401 { "No Rem. Control", -1 }
2403 #else
2404 static const struct opt_items key2_option[1] = {
2405 { "2 Key Control", -1 }
2407 #endif
2409 static const struct opt_items nokey_option[1] = {
2410 { "Out of Control", -1 }
2413 MENUITEM_STRINGLIST(menu, "Wormlet Menu", NULL, "Play Wormlet!",
2414 "Number of Worms", "Number of Players", "Control Style",
2415 "Worm Growth Per Food","Worm Speed","Arghs Per Food",
2416 "Argh Size","Food Size","Revert to Default Settings",
2417 "Playback Control", "Quit");
2419 rb->button_clear_queue();
2421 while (!menu_quit) {
2422 switch(rb->do_menu(&menu, &result, NULL, false))
2424 case 0:
2425 rb->lcd_setfont(FONT_SYSFIXED);
2426 launch_wormlet();
2427 break;
2428 case 1:
2429 rb->set_int("Number of Worms", "", UNIT_INT, &worm_count, NULL,
2430 1, 1, 3, NULL);
2431 if (worm_count < players) {
2432 worm_count = players;
2434 break;
2435 case 2:
2436 #ifdef MULTIPLAYER
2437 rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
2438 1, 0, 4, NULL);
2439 #else
2440 rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
2441 1, 0, 2, NULL);
2442 #endif
2443 if (players > worm_count) {
2444 worm_count = players;
2446 if (players > 2) {
2447 use_remote = true;
2449 break;
2450 case 3:
2451 switch(players) {
2452 case 0:
2453 rb->set_option("Control Style",&use_remote,INT,
2454 nokey_option, 1, NULL);
2455 break;
2456 case 1:
2457 rb->set_option("Control Style",&use_remote,INT,
2458 key24_option, 2, NULL);
2459 break;
2460 case 2:
2461 #ifdef REMOTE
2462 rb->set_option("Control Style",&use_remote,INT,
2463 remote_option, 2, NULL);
2464 #else
2465 rb->set_option("Control Style",&use_remote,INT,
2466 key2_option, 1, NULL);
2467 #endif
2468 break;
2469 case 3:
2470 rb->set_option("Control Style",&use_remote,INT,
2471 remoteonly_option, 1, NULL);
2472 break;
2474 break;
2475 case 4:
2476 rb->set_int("Worm Growth Per Food", "", UNIT_INT, &worm_food,
2477 NULL, 1, 0, 15, NULL);
2478 break;
2479 case 5:
2480 new_setting = 20 - speed;
2481 rb->set_int("Worm Speed", "", UNIT_INT, &new_setting,
2482 NULL, 1, 0, 20, NULL);
2483 speed = 20 - new_setting;
2484 break;
2485 case 6:
2486 rb->set_int("Arghs Per Food", "", UNIT_INT, &arghs_per_food,
2487 NULL, 1, 0, 8, NULL);
2488 break;
2489 case 7:
2490 rb->set_int("Argh Size", "", UNIT_INT, &argh_size,
2491 NULL, 1, 2, 10, NULL);
2492 break;
2493 case 8:
2494 rb->set_int("Food Size", "", UNIT_INT, &food_size,
2495 NULL, 1, 2, 10, NULL);
2496 break;
2497 case 9:
2498 new_setting = 0;
2499 rb->set_option("Reset Settings?", &new_setting, INT, noyes , 2, NULL);
2500 if (new_setting == 1)
2501 default_settings();
2502 break;
2503 case 10:
2504 playback_control(NULL);
2505 break;
2506 default:
2507 menu_quit=1;
2508 break;
2512 configfile_save(SETTINGS_FILENAME, config,
2513 sizeof(config)/sizeof(*config),
2514 SETTINGS_VERSION);
2516 return PLUGIN_OK;