Fix FS#11007: Lua didn't parse negative numbers correct when reading from files
[kugel-rb.git] / apps / plugins / wormlet.c
blob5a897be086adc4ab753b55c478259b0c6ddfac6a
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 #else
262 #error No keymap defined!
263 #endif
265 #ifdef HAVE_TOUCHSCREEN
266 #ifndef BTN_DIR_UP
267 #define BTN_DIR_UP BUTTON_TOPMIDDLE
268 #endif
269 #ifndef BTN_DIR_DOWN
270 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
271 #endif
272 #ifndef BTN_DIR_LEFT
273 #define BTN_DIR_LEFT BUTTON_MIDLEFT
274 #endif
275 #ifndef BTN_DIR_RIGHT
276 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
277 #endif
278 #ifndef BTN_STARTPAUSE
279 #define BTN_STARTPAUSE BUTTON_CENTER
280 #endif
281 #ifndef BTN_QUIT
282 #define BTN_QUIT BUTTON_TOPLEFT
283 #endif
284 #ifndef BTN_STOPRESET
285 #define BTN_STOPRESET BUTTON_TOPRIGHT
286 #endif
287 #endif
289 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
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 == 64)
295 #define FOOD_SIZE 3
296 #define ARGH_SIZE 4
297 #define SPEED 14
298 #define MAX_WORM_SEGMENTS 128
299 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
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 == 96)
305 #define FOOD_SIZE 3
306 #define ARGH_SIZE 4
307 #define SPEED 12
308 #define MAX_WORM_SEGMENTS 128
309 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
310 #define FOOD_SIZE 4
311 #define ARGH_SIZE 5
312 #define SPEED 10
313 #define MAX_WORM_SEGMENTS 128
314 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
315 #define FOOD_SIZE 4
316 #define ARGH_SIZE 5
317 #define SPEED 9
318 #define MAX_WORM_SEGMENTS 128
319 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
320 #define FOOD_SIZE 4
321 #define ARGH_SIZE 5
322 #define SPEED 8
323 #define MAX_WORM_SEGMENTS 256
324 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
325 #define FOOD_SIZE 4
326 #define ARGH_SIZE 5
327 #define SPEED 6
328 #define MAX_WORM_SEGMENTS 256
329 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
330 #define FOOD_SIZE 5
331 #define ARGH_SIZE 6
332 #define SPEED 4
333 #define MAX_WORM_SEGMENTS 512
334 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
335 #define FOOD_SIZE 5
336 #define ARGH_SIZE 6
337 #define SPEED 4
338 #define MAX_WORM_SEGMENTS 512
339 #elif ((LCD_WIDTH == 320) && (LCD_HEIGHT == 240)) || \
340 ((LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400)))
341 #define FOOD_SIZE 7
342 #define ARGH_SIZE 8
343 #define SPEED 4
344 #define MAX_WORM_SEGMENTS 512
345 #elif ((LCD_WIDTH == 640) && (LCD_HEIGHT == 480)) || \
346 ((LCD_WIDTH == 480) && (LCD_HEIGHT == 640))
347 #define FOOD_SIZE 14
348 #define ARGH_SIZE 16
349 #define SPEED 4
350 #define MAX_WORM_SEGMENTS 512
351 #endif
353 #ifdef HAVE_LCD_COLOR
354 #define COLOR_WORM LCD_RGBPACK(80, 40, 0)
355 #define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
356 #define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
357 #define COLOR_FG LCD_RGBPACK(0, 0, 0)
358 #define COLOR_BG LCD_RGBPACK(181, 199, 231)
359 #endif
361 #define CHECK_SQUARE_COLLISION(x1,y1,s1,x2,y2,s2) (x1+s1>x2)&&(x2+s2>x1)&&(y1+s1>y2)&&(y2+s2>y1)
364 * All the properties that a worm has.
366 static struct worm {
367 /* The worm is stored in a ring of xy coordinates */
368 int x[MAX_WORM_SEGMENTS];
369 int y[MAX_WORM_SEGMENTS];
371 int head; /* index of the head within the buffer */
372 int tail; /* index of the tail within the buffer */
373 int growing; /* number of cyles the worm still keeps growing */
374 bool alive; /* the worms living state */
376 /* direction vector in which the worm moves */
377 int dirx; /* only values -1 0 1 allowed */
378 int diry; /* only values -1 0 1 allowed */
380 /* this method is used to fetch the direction the user
381 has selected. It can be one of the values
382 human_player1, human_player2, remote_player, virtual_player.
383 All these values are fuctions, that can change the direction
384 of the worm */
385 void (*fetch_worm_direction)(struct worm *w);
386 } worms[MAX_WORMS];
388 /* stores the highscore - besides it was scored by a virtual player */
389 static int highscore;
391 #define MAX_FOOD 5 /* maximal number of food items */
393 /* The arrays store the food coordinates */
394 static int foodx[MAX_FOOD];
395 static int foody[MAX_FOOD];
397 #define MAX_ARGH 100 /* maximal number of argh items */
398 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
400 /* The arrays store the argh coordinates */
401 static int arghx[MAX_ARGH];
402 static int arghy[MAX_ARGH];
404 /* the number of arghs that are currently in use */
405 static int argh_count;
407 /* the number of arghs per food, settable by user */
408 static int arghs_per_food = ARGHS_PER_FOOD;
409 /* the size of the argh, settable by user */
410 static int argh_size = ARGH_SIZE;
411 /* the size of the food, settable by user */
412 static int food_size = FOOD_SIZE;
413 /* the speed of the worm, settable by user */
414 static int speed = SPEED;
415 /* the amount a worm grows by eating a food, settable by user */
416 static int worm_food = WORM_PER_FOOD;
418 /* End additional variables */
420 #ifdef DEBUG_WORMLET
421 /* just a buffer used for debug output */
422 static char debugout[15];
423 #endif
425 /* the number of active worms (dead or alive) */
426 static int worm_count = MAX_WORMS;
428 /* in multiplayer mode: en- / disables the remote worm control
429 in singleplayer mode: toggles 4 / 2 button worm control */
430 static bool use_remote = false;
432 /* return values of check_collision */
433 #define COLLISION_NONE 0
434 #define COLLISION_WORM 1
435 #define COLLISION_FOOD 2
436 #define COLLISION_ARGH 3
437 #define COLLISION_FIELD 4
439 /* constants for use as directions.
440 Note that the values are ordered clockwise.
441 Thus increasing / decreasing the values
442 is equivalent to right / left turns. */
443 #define WEST 0
444 #define NORTH 1
445 #define EAST 2
446 #define SOUTH 3
448 /* direction of human player 1 */
449 static int player1_dir = EAST;
450 /* direction of human player 2 */
451 static int player2_dir = EAST;
452 /* direction of human player 3 */
453 static int player3_dir = EAST;
455 /* the number of (human) players that currently
456 control a worm */
457 static int players = 1;
459 #define SETTINGS_VERSION 1
460 #define SETTINGS_MIN_VERSION 1
461 #define SETTINGS_FILENAME "wormlet.cfg"
463 static struct configdata config[] =
465 {TYPE_INT, 0, 1024, { .int_p = &highscore }, "highscore", NULL},
466 {TYPE_INT, 0, 15, { .int_p = &arghs_per_food }, "arghs per food", NULL},
467 {TYPE_INT, 0, 15, { .int_p = &argh_size }, "argh size", NULL},
468 {TYPE_INT, 0, 15, { .int_p = &food_size }, "food size", NULL},
469 {TYPE_INT, 0, 3, { .int_p = &players }, "players", NULL},
470 {TYPE_INT, 0, 3, { .int_p = &worm_count }, "worms", NULL},
471 {TYPE_INT, 0, 20, { .int_p = &speed }, "speed", NULL},
472 {TYPE_INT, 0, 15, { .int_p = &worm_food }, "Worm Growth Per Food", NULL}
475 #ifdef DEBUG_WORMLET
476 static void set_debug_out(char *str){
477 strcpy(debugout, str);
479 #endif
482 * Returns the direction id in which the worm
483 * currently is creeping.
484 * @param struct worm *w The worm that is to be investigated.
485 * w Must not be null.
486 * @return int A value 0 <= value < 4
487 * Note the predefined constants NORTH, SOUTH, EAST, WEST
489 static int get_worm_dir(struct worm *w)
491 int retVal ;
492 if (w->dirx == 0) {
493 if (w->diry == 1) {
494 retVal = SOUTH;
495 } else {
496 retVal = NORTH;
498 } else {
499 if (w->dirx == 1) {
500 retVal = EAST;
501 } else {
502 retVal = WEST;
505 return retVal;
509 * Set the direction of the specified worm with a direction id.
510 * Increasing the value by 1 means to turn the worm direction
511 * to right by 90 degree.
512 * @param struct worm *w The worm that is to be altered. w Must not be null.
513 * @param int dir The new direction in which the worm is to creep.
514 * dir must be 0 <= dir < 4. Use predefined constants
515 * NORTH, SOUTH, EAST, WEST
517 static void set_worm_dir(struct worm *w, int dir)
519 switch (dir) {
520 case WEST:
521 w->dirx = -1;
522 w->diry = 0;
523 break;
524 case NORTH:
525 w->dirx = 0;
526 w->diry = - 1;
527 break;
528 case EAST:
529 w->dirx = 1;
530 w->diry = 0;
531 break;
532 case SOUTH:
533 w->dirx = 0;
534 w->diry = 1;
535 break;
540 * Returns the current length of the worm array. This
541 * is also a value for the number of bends that are in the worm.
542 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
544 static int get_worm_array_length(struct worm *w)
546 /* initial simple calculation will be overwritten if wrong. */
547 int retVal = w->head - w->tail;
549 /* if the worm 'crosses' the boundaries of the ringbuffer */
550 if (retVal < 0) {
551 retVal = w->head + MAX_WORM_SEGMENTS - w->tail;
554 return retVal;
558 * Returns the score the specified worm. The score is the length
559 * of the worm.
560 * @param struct worm *w The worm that is to be investigated.
561 * w must not be null.
562 * @return int The length of the worm (>= 0).
564 static int get_score(struct worm *w)
566 int retval = 0;
567 int length = get_worm_array_length(w);
568 int i;
569 for (i = 0; i < length; i++) {
571 /* The iteration iterates the length of the worm.
572 Here's the conversion to the true indices within the worm arrays. */
573 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
574 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
575 int startx = w->x[linestart];
576 int starty = w->y[linestart];
577 int endx = w->x[lineend];
578 int endy = w->y[lineend];
580 int minimum, maximum;
582 if (startx == endx) {
583 minimum = MIN(starty, endy);
584 maximum = MAX(starty, endy);
585 } else {
586 minimum = MIN(startx, endx);
587 maximum = MAX(startx, endx);
589 retval += abs(maximum - minimum);
591 return retval;
595 * Determines wether the line specified by startx, starty, endx, endy intersects
596 * the rectangle specified by x, y, width, height. Note that the line must be exactly
597 * horizontal or vertical (startx == endx or starty == endy).
598 * @param int startx The x coordinate of the start point of the line.
599 * @param int starty The y coordinate of the start point of the line.
600 * @param int endx The x coordinate of the end point of the line.
601 * @param int endy The y coordinate of the end point of the line.
602 * @param int x The x coordinate of the top left corner of the rectangle.
603 * @param int y The y coordinate of the top left corner of the rectangle.
604 * @param int width The width of the rectangle.
605 * @param int height The height of the rectangle.
606 * @return bool Returns true if the specified line intersects with the recangle.
608 static bool line_in_rect(int startx, int starty, int endx, int endy,
609 int x, int y, int width, int height)
611 bool retval = false;
612 int simple, simplemin, simplemax;
613 int compa, compb, compmin, compmax;
614 int temp;
615 if (startx == endx) {
616 simple = startx;
617 simplemin = x;
618 simplemax = x + width;
620 compa = starty;
621 compb = endy;
622 compmin = y;
623 compmax = y + height;
624 } else {
625 simple = starty;
626 simplemin = y;
627 simplemax = y + height;
629 compa = startx;
630 compb = endx;
631 compmin = x;
632 compmax = x + width;
635 temp = compa;
636 compa = MIN(compa, compb);
637 compb = MAX(temp, compb);
639 if (simplemin <= simple && simple <= simplemax) {
640 if ((compmin <= compa && compa <= compmax) ||
641 (compmin <= compb && compb <= compmax) ||
642 (compa <= compmin && compb >= compmax)) {
643 retval = true;
646 return retval;
650 * Tests wether the specified worm intersects with the rect.
651 * @param struct worm *w The worm to be investigated
652 * @param int x The x coordinate of the top left corner of the rect
653 * @param int y The y coordinate of the top left corner of the rect
654 * @param int widht The width of the rect
655 * @param int height The height of the rect
656 * @return bool Returns true if the worm intersects with the rect
658 static bool worm_in_rect(struct worm *w, int x, int y, int width, int height)
660 bool retval = false;
663 /* get_worm_array_length is expensive -> buffer the value */
664 int wormLength = get_worm_array_length(w);
665 int i;
667 /* test each entry that is part of the worm */
668 for (i = 0; i < wormLength && retval == false; i++) {
670 /* The iteration iterates the length of the worm.
671 Here's the conversion to the true indices within the worm arrays. */
672 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
673 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
674 int startx = w->x[linestart];
675 int starty = w->y[linestart];
676 int endx = w->x[lineend];
677 int endy = w->y[lineend];
679 retval = line_in_rect(startx, starty, endx, endy, x, y, width, height);
682 return retval;
686 * Checks wether a specific food in the food arrays is at the
687 * specified coordinates.
688 * @param int foodIndex The index of the food in the food arrays
689 * @param int x the x coordinate.
690 * @param int y the y coordinate.
691 * @return Returns true if the coordinate hits the food specified by
692 * foodIndex.
694 static bool specific_food_collision(int foodIndex, int x, int y)
696 bool retVal = false;
697 if (x >= foodx[foodIndex] &&
698 x < foodx[foodIndex] + food_size &&
699 y >= foody[foodIndex] &&
700 y < foody[foodIndex] + food_size) {
702 retVal = true;
704 return retVal;
708 * Returns the index of the food that is at the
709 * given coordinates. If no food is at the coordinates
710 * -1 is returned.
711 * @return int -1 <= value < MAX_FOOD
713 static int food_collision(int x, int y)
715 int i = 0;
716 int retVal = -1;
717 for (i = 0; i < MAX_FOOD; i++) {
718 if (specific_food_collision(i, x, y)) {
719 retVal = i;
720 break;
723 return retVal;
727 * Checks wether a specific argh in the argh arrays is at the
728 * specified coordinates.
729 * @param int arghIndex The index of the argh in the argh arrays
730 * @param int x the x coordinate.
731 * @param int y the y coordinate.
732 * @return Returns true if the coordinate hits the argh specified by
733 * arghIndex.
735 static bool specific_argh_collision(int arghIndex, int x, int y)
737 if ( x >= arghx[arghIndex] &&
738 y >= arghy[arghIndex] &&
739 x < arghx[arghIndex] + argh_size &&
740 y < arghy[arghIndex] + argh_size )
742 return true;
745 return false;
749 * Returns the index of the argh that is at the
750 * given coordinates. If no argh is at the coordinates
751 * -1 is returned.
752 * @param int x The x coordinate.
753 * @param int y The y coordinate.
754 * @return int -1 <= value < argh_count <= MAX_ARGH
756 static int argh_collision(int x, int y)
758 int i = 0;
759 int retVal = -1;
761 /* search for the argh that has the specified coords */
762 for (i = 0; i < argh_count; i++) {
763 if (specific_argh_collision(i, x, y)) {
764 retVal = i;
765 break;
768 return retVal;
772 * Checks wether the worm collides with the food at the specfied food-arrays.
773 * @param int foodIndex The index of the food in the arrays. Ensure the value is
774 * 0 <= foodIndex <= MAX_FOOD
775 * @return Returns true if the worm collides with the specified food.
777 static bool worm_food_collision(struct worm *w, int foodIndex)
779 bool retVal = false;
781 retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex],
782 food_size - 1, food_size - 1);
784 return retVal;
788 * Returns true if the worm hits the argh within the next moves (unless
789 * the worm changes it's direction).
790 * @param struct worm *w - The worm to investigate
791 * @param int argh_idx - The index of the argh
792 * @param int moves - The number of moves that are considered.
793 * @return Returns false if the specified argh is not hit within the next
794 * moves.
796 static bool worm_argh_collision_in_moves(struct worm *w, int argh_idx, int moves)
798 bool retVal = false;
799 int x1, y1, x2, y2;
800 x1 = w->x[w->head];
801 y1 = w->y[w->head];
803 x2 = w->x[w->head] + moves * w->dirx;
804 y2 = w->y[w->head] + moves * w->diry;
806 retVal = line_in_rect(x1, y1, x2, y2, arghx[argh_idx], arghy[argh_idx],
807 argh_size, argh_size);
808 return retVal;
812 * Checks wether the worm collides with the argh at the specfied argh-arrays.
813 * @param int arghIndex The index of the argh in the arrays.
814 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
815 * @return Returns true if the worm collides with the specified argh.
817 static bool worm_argh_collision(struct worm *w, int arghIndex)
819 bool retVal = false;
821 retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex],
822 argh_size - 1, argh_size - 1);
824 return retVal;
828 * Find new coordinates for the food stored in foodx[index], foody[index]
829 * that don't collide with any other food or argh
830 * @param int index
831 * Ensure that 0 <= index < MAX_FOOD.
833 static void make_food(int index)
835 int x = 0;
836 int y = 0;
837 bool collisionDetected = false;
838 int i;
840 do {
841 /* make coordinates for a new food so that
842 the entire food lies within the FIELD */
843 x = rb->rand() % (FIELD_RECT_WIDTH - food_size);
844 y = rb->rand() % (FIELD_RECT_HEIGHT - food_size);
845 collisionDetected = false;
846 /* Ensure that the new food doesn't collide with any
847 existing foods or arghs.
848 If the new food hit any existing
849 argh or food a collision is detected.
852 for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
853 collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,foodx[i],foody[i],food_size);
855 for (i=0; i<argh_count && !collisionDetected; i++) {
856 collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,arghx[i],arghy[i],argh_size);
859 /* use coordinates for further testing */
860 foodx[index] = x;
861 foody[index] = y;
863 /* now test wether we accidently hit the worm with food ;) */
864 i = 0;
865 for (i = 0; i < worm_count && !collisionDetected; i++) {
867 collisionDetected = worm_food_collision(&worms[i], index);
871 while (collisionDetected);
872 return;
876 * Clears a food from the lcd buffer.
877 * @param int index The index of the food arrays under which
878 * the coordinates of the desired food can be found. Ensure
879 * that the value is 0 <= index <= MAX_FOOD.
881 static void clear_food(int index)
883 /* remove the old food from the screen */
884 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
885 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
886 foody[index] + FIELD_RECT_Y,
887 food_size, food_size);
888 rb->lcd_set_drawmode(DRMODE_SOLID);
892 * Draws a food in the lcd buffer.
893 * @param int index The index of the food arrays under which
894 * the coordinates of the desired food can be found. Ensure
895 * that the value is 0 <= index <= MAX_FOOD.
897 static void draw_food(int index)
899 /* draw the food object */
900 #ifdef HAVE_LCD_COLOR
901 rb->lcd_set_foreground(COLOR_FOOD);
902 #endif
903 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
904 foody[index] + FIELD_RECT_Y,
905 food_size, food_size);
906 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
907 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X + 1,
908 foody[index] + FIELD_RECT_Y + 1,
909 food_size - 2, food_size - 2);
910 rb->lcd_set_drawmode(DRMODE_SOLID);
911 #ifdef HAVE_LCD_COLOR
912 rb->lcd_set_foreground(COLOR_FG);
913 #endif
917 * Find new coordinates for the argh stored in arghx[index], arghy[index]
918 * that don't collide with any other food or argh.
919 * @param int index
920 * Ensure that 0 <= index < argh_count < MAX_ARGH.
922 static void make_argh(int index)
924 int x = -1;
925 int y = -1;
926 bool collisionDetected = false;
927 int i;
929 do {
930 /* make coordinates for a new argh so that
931 the entire food lies within the FIELD */
932 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
933 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
934 collisionDetected = false;
935 /* Ensure that the new argh doesn't intersect with any
936 existing foods or arghs.
937 If the new argh hit any existing
938 argh or food an intersection is detected.
941 for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
942 collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,foodx[i],foody[i],food_size);
944 for (i=0; i<argh_count && !collisionDetected; i++) {
945 collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,arghx[i],arghy[i],argh_size);
948 /* use the candidate coordinates to make a real argh */
949 arghx[index] = x;
950 arghy[index] = y;
952 /* now test wether we accidently hit the worm with argh ;) */
953 for (i = 0; i < worm_count && !collisionDetected; i++) {
954 collisionDetected |= worm_argh_collision(&worms[i], index);
955 collisionDetected |= worm_argh_collision_in_moves(&worms[i], index,
956 MIN_ARGH_DIST);
959 while (collisionDetected);
960 return;
964 * Draws an argh in the lcd buffer.
965 * @param int index The index of the argh arrays under which
966 * the coordinates of the desired argh can be found. Ensure
967 * that the value is 0 <= index < argh_count <= MAX_ARGH.
969 static void draw_argh(int index)
971 /* draw the new argh */
972 #ifdef HAVE_LCD_COLOR
973 rb->lcd_set_foreground(COLOR_ARGH);
974 #endif
975 rb->lcd_fillrect(arghx[index] + FIELD_RECT_X,
976 arghy[index] + FIELD_RECT_Y,
977 argh_size, argh_size);
978 #ifdef HAVE_LCD_COLOR
979 rb->lcd_set_foreground(COLOR_FG);
980 #endif
983 static void virtual_player(struct worm *w);
985 * Initialzes the specified worm with INITIAL_WORM_LENGTH
986 * and the tail at the specified position. The worm will
987 * be initialized alive and creeping EAST.
988 * @param struct worm *w The worm that is to be initialized
989 * @param int x The x coordinate at which the tail of the worm starts.
990 * x must be 0 <= x < FIELD_RECT_WIDTH.
991 * @param int y The y coordinate at which the tail of the worm starts
992 * y must be 0 <= y < FIELD_RECT_WIDTH.
994 static void init_worm(struct worm *w, int x, int y)
996 /* initialize the worm size */
997 w->head = 1;
998 w->tail = 0;
1000 w->x[w->head] = x + 1;
1001 w->y[w->head] = y;
1003 w->x[w->tail] = x;
1004 w->y[w->tail] = y;
1006 /* set the initial direction the worm creeps to */
1007 w->dirx = 1;
1008 w->diry = 0;
1010 w->growing = INITIAL_WORM_LENGTH - 1;
1011 w->alive = true;
1012 w->fetch_worm_direction = virtual_player;
1016 * Writes the direction that was stored for
1017 * human player 1 into the specified worm. This function
1018 * may be used to be stored in worm.fetch_worm_direction.
1019 * The value of
1020 * the direction is read from player1_dir.
1021 * @param struct worm *w - The worm of which the direction
1022 * is altered.
1024 static void human_player1(struct worm *w) {
1025 set_worm_dir(w, player1_dir);
1029 * Writes the direction that was stored for
1030 * human player 2 into the specified worm. This function
1031 * may be used to be stored in worm.fetch_worm_direction.
1032 * The value of
1033 * the direction is read from player2_dir.
1034 * @param struct worm *w - The worm of which the direction
1035 * is altered.
1037 static void human_player2(struct worm *w) {
1038 set_worm_dir(w, player2_dir);
1042 * Writes the direction that was stored for
1043 * human player using a remote control
1044 * into the specified worm. This function
1045 * may be used to be stored in worm.fetch_worm_direction.
1046 * The value of
1047 * the direction is read from player3_dir.
1048 * @param struct worm *w - The worm of which the direction
1049 * is altered.
1051 static void remote_player(struct worm *w) {
1052 set_worm_dir(w, player3_dir);
1056 * Initializes the worm-, food- and argh-arrays, draws a frame,
1057 * makes some food and argh and display all that stuff.
1059 static void init_wormlet(void)
1061 int i;
1063 for (i = 0; i< worm_count; i++) {
1064 /* Initialize all the worm coordinates to center. */
1065 int x = (int)(FIELD_RECT_WIDTH / 2);
1066 int y = (int)((FIELD_RECT_HEIGHT - 20)/ 2) + i * 10;
1068 init_worm(&worms[i], x, y);
1071 player1_dir = EAST;
1072 player2_dir = EAST;
1073 player3_dir = EAST;
1075 if (players > 0) {
1076 worms[0].fetch_worm_direction = human_player1;
1079 if (players > 1) {
1080 if (use_remote) {
1081 worms[1].fetch_worm_direction = remote_player;
1082 } else {
1083 worms[1].fetch_worm_direction = human_player2;
1087 if (players > 2) {
1088 worms[2].fetch_worm_direction = human_player2;
1091 /* Needed when the game is restarted using BTN_STOPRESET */
1092 rb->lcd_clear_display();
1094 /* make and display some food and argh */
1095 argh_count = MAX_FOOD;
1096 for (i = 0; i < MAX_FOOD; i++) {
1097 make_food(i);
1098 draw_food(i);
1099 make_argh(i);
1100 draw_argh(i);
1103 /* draw the game field */
1104 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1105 rb->lcd_fillrect(0, 0, FIELD_RECT_WIDTH + 2, FIELD_RECT_HEIGHT + 2);
1106 rb->lcd_fillrect(1, 1, FIELD_RECT_WIDTH, FIELD_RECT_HEIGHT);
1107 rb->lcd_set_drawmode(DRMODE_SOLID);
1109 /* make everything visible */
1110 rb->lcd_update();
1115 * Move the worm one step further if it is alive.
1116 * The direction in which the worm moves is taken from dirx and diry.
1117 * move_worm decreases growing if > 0. While the worm is growing the tail
1118 * is left untouched.
1119 * @param struct worm *w The worm to move. w must not be NULL.
1121 static void move_worm(struct worm *w)
1123 if (w->alive) {
1124 /* determine the head point and its precessor */
1125 int headx = w->x[w->head];
1126 int heady = w->y[w->head];
1127 int prehead = (w->head + MAX_WORM_SEGMENTS - 1) % MAX_WORM_SEGMENTS;
1128 int preheadx = w->x[prehead];
1129 int preheady = w->y[prehead];
1131 /* determine the old direction */
1132 int olddirx;
1133 int olddiry;
1134 if (headx == preheadx) {
1135 olddirx = 0;
1136 olddiry = (heady > preheady) ? 1 : -1;
1137 } else {
1138 olddiry = 0;
1139 olddirx = (headx > preheadx) ? 1 : -1;
1142 /* olddir == dir?
1143 a change of direction means a new segment
1144 has been opened */
1145 if (olddirx != w->dirx ||
1146 olddiry != w->diry) {
1147 w->head = (w->head + 1) % MAX_WORM_SEGMENTS;
1150 /* new head position */
1151 w->x[w->head] = headx + w->dirx;
1152 w->y[w->head] = heady + w->diry;
1155 /* while the worm is growing no tail procession is necessary */
1156 if (w->growing > 0) {
1157 /* update the worms grow state */
1158 w->growing--;
1161 /* if the worm isn't growing the tail has to be dragged */
1162 else {
1163 /* index of the end of the tail segment */
1164 int tail_segment_end = (w->tail + 1) % MAX_WORM_SEGMENTS;
1166 /* drag the end of the tail */
1167 /* only one coordinate has to be altered. Here it is
1168 determined which one */
1169 int dir = 0; /* specifies wether the coord has to be in- or decreased */
1170 if (w->x[w->tail] == w->x[tail_segment_end]) {
1171 dir = (w->y[w->tail] - w->y[tail_segment_end] < 0) ? 1 : -1;
1172 w->y[w->tail] += dir;
1173 } else {
1174 dir = (w->x[w->tail] - w->x[tail_segment_end] < 0) ? 1 : -1;
1175 w->x[w->tail] += dir;
1178 /* when the tail has been dragged so far that it meets
1179 the next segment start the tail segment is obsolete and
1180 must be freed */
1181 if (w->x[w->tail] == w->x[tail_segment_end] &&
1182 w->y[w->tail] == w->y[tail_segment_end]){
1184 /* drop the last tail point */
1185 w->tail = tail_segment_end;
1192 * Draws the head and clears the tail of the worm in
1193 * the display buffer. lcd_update() is NOT called thus
1194 * the caller has to take care that the buffer is displayed.
1196 static void draw_worm(struct worm *w)
1198 /* draw the new head */
1199 int x = w->x[w->head];
1200 int y = w->y[w->head];
1201 #ifdef HAVE_LCD_COLOR
1202 rb->lcd_set_foreground(COLOR_WORM);
1203 #endif
1204 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1205 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1208 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1210 /* clear the space behind the worm */
1211 x = w->x[w->tail] ;
1212 y = w->y[w->tail] ;
1213 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1214 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1216 rb->lcd_set_drawmode(DRMODE_SOLID);
1217 #ifdef HAVE_LCD_COLOR
1218 rb->lcd_set_foreground(COLOR_FG);
1219 #endif
1223 * Checks wether the coordinate is part of the worm. Returns
1224 * true if any part of the worm was hit - including the head.
1225 * @param x int The x coordinate
1226 * @param y int The y coordinate
1227 * @return int The index of the worm arrays that contain x, y.
1228 * Returns -1 if the coordinates are not part of the worm.
1230 static int specific_worm_collision(struct worm *w, int x, int y)
1232 int retVal = -1;
1234 /* get_worm_array_length is expensive -> buffer the value */
1235 int wormLength = get_worm_array_length(w);
1236 int i;
1238 /* test each entry that is part of the worm */
1239 for (i = 0; i < wormLength && retVal == -1; i++) {
1241 /* The iteration iterates the length of the worm.
1242 Here's the conversion to the true indices within the worm arrays. */
1243 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
1244 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
1245 bool samex = (w->x[linestart] == x) && (w->x[lineend] == x);
1246 bool samey = (w->y[linestart] == y) && (w->y[lineend] == y);
1247 if (samex || samey){
1248 int test, min, max, tmp;
1250 if (samey) {
1251 min = w->x[linestart];
1252 max = w->x[lineend];
1253 test = x;
1254 } else {
1255 min = w->y[linestart];
1256 max = w->y[lineend];
1257 test = y;
1260 tmp = min;
1261 min = MIN(min, max);
1262 max = MAX(tmp, max);
1264 if (min <= test && test <= max) {
1265 retVal = lineend;
1269 return retVal;
1273 * Increases the length of the specified worm by marking
1274 * that it may grow by len pixels. Note that the worm has
1275 * to move to make the growing happen.
1276 * @param worm *w The worm that is to be altered.
1277 * @param int len A positive value specifying the amount of
1278 * pixels the worm may grow.
1280 static void add_growing(struct worm *w, int len) {
1281 w->growing += len;
1285 * Determins the worm that is at the coordinates x, y. The parameter
1286 * w is a switch parameter that changes the functionality of worm_collision.
1287 * If w is specified and x,y hits the head of w NULL is returned.
1288 * This is a useful way to determine wether the head of w hits
1289 * any worm but including itself but excluding its own head.
1290 * (It hits always its own head ;))
1291 * If w is set to NULL worm_collision returns any worm including all heads
1292 * that is at position of x,y.
1293 * @param struct worm *w The worm of which the head should be excluded in
1294 * the test. w may be set to NULL.
1295 * @param int x The x coordinate that is checked
1296 * @param int y The y coordinate that is checkec
1297 * @return struct worm* The worm that has been hit by x,y. If no worm
1298 * was at the position NULL is returned.
1300 static struct worm* worm_collision(struct worm *w, int x, int y)
1302 struct worm *retVal = NULL;
1303 int i;
1304 for (i = 0; (i < worm_count) && (retVal == NULL); i++) {
1305 int collision_at = specific_worm_collision(&worms[i], x, y);
1306 if (collision_at != -1) {
1307 if (!(w == &worms[i] && collision_at == w->head)){
1308 retVal = &worms[i];
1312 return retVal;
1316 * Returns true if the head of the worm just has
1317 * crossed the field boundaries.
1318 * @return bool true if the worm just has wrapped.
1320 static bool field_collision(struct worm *w)
1322 bool retVal = false;
1323 if ((w->x[w->head] >= FIELD_RECT_WIDTH) ||
1324 (w->y[w->head] >= FIELD_RECT_HEIGHT) ||
1325 (w->x[w->head] < 0) ||
1326 (w->y[w->head] < 0))
1328 retVal = true;
1330 return retVal;
1335 * Returns true if the specified coordinates are within the
1336 * field specified by the FIELD_RECT_XXX constants.
1337 * @param int x The x coordinate of the point that is investigated
1338 * @param int y The y coordinate of the point that is investigated
1339 * @return bool Returns false if x,y specifies a point outside the
1340 * field of worms.
1342 static bool is_in_field_rect(int x, int y)
1344 bool retVal = false;
1345 retVal = (x >= 0 && x < FIELD_RECT_WIDTH &&
1346 y >= 0 && y < FIELD_RECT_HEIGHT);
1347 return retVal;
1351 * Checks and returns wether the head of the w
1352 * is colliding with something currently.
1353 * @return int One of the values:
1354 * COLLISION_NONE
1355 * COLLISION_w
1356 * COLLISION_FOOD
1357 * COLLISION_ARGH
1358 * COLLISION_FIELD
1360 static int check_collision(struct worm *w)
1362 int retVal = COLLISION_NONE;
1364 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL)
1365 retVal = COLLISION_WORM;
1367 if (food_collision(w->x[w->head], w->y[w->head]) >= 0)
1368 retVal = COLLISION_FOOD;
1370 if (argh_collision(w->x[w->head], w->y[w->head]) >= 0)
1371 retVal = COLLISION_ARGH;
1373 if (field_collision(w))
1374 retVal = COLLISION_FIELD;
1376 return retVal;
1380 * Returns the index of the food that is closest to the point
1381 * specified by x, y. This index may be used in the foodx and
1382 * foody arrays.
1383 * @param int x The x coordinate of the point
1384 * @param int y The y coordinate of the point
1385 * @return int A value usable as index in foodx and foody.
1387 static int get_nearest_food(int x, int y)
1389 int nearestfood = 0;
1390 int olddistance = FIELD_RECT_WIDTH + FIELD_RECT_HEIGHT;
1391 int deltax = 0;
1392 int deltay = 0;
1393 int foodindex;
1394 for (foodindex = 0; foodindex < MAX_FOOD; foodindex++) {
1395 int distance;
1396 deltax = foodx[foodindex] - x;
1397 deltay = foody[foodindex] - y;
1398 deltax = deltax > 0 ? deltax : deltax * (-1);
1399 deltay = deltay > 0 ? deltay : deltay * (-1);
1400 distance = deltax + deltay;
1402 if (distance < olddistance) {
1403 olddistance = distance;
1404 nearestfood = foodindex;
1407 return nearestfood;
1411 * Returns wether the specified position is next to the worm
1412 * and in the direction the worm looks. Use this method to
1413 * test wether this position would be hit with the next move of
1414 * the worm unless the worm changes its direction.
1415 * @param struct worm *w - The worm to be investigated
1416 * @param int x - The x coordinate of the position to test.
1417 * @param int y - The y coordinate of the position to test.
1418 * @return Returns true if the worm will hit the position unless
1419 * it change its direction before the next move.
1421 static bool is_in_front_of_worm(struct worm *w, int x, int y)
1423 bool infront = false;
1424 int deltax = x - w->x[w->head];
1425 int deltay = y - w->y[w->head];
1427 if (w->dirx == 0) {
1428 infront = (w->diry * deltay) > 0;
1429 } else {
1430 infront = (w->dirx * deltax) > 0;
1432 return infront;
1436 * Returns true if the worm will collide with the next move unless
1437 * it changes its direction.
1438 * @param struct worm *w - The worm to be investigated.
1439 * @return Returns true if the worm will collide with the next move
1440 * unless it changes its direction.
1442 static bool will_worm_collide(struct worm *w)
1444 int x = w->x[w->head] + w->dirx;
1445 int y = w->y[w->head] + w->diry;
1446 bool retVal = !is_in_field_rect(x, y);
1447 if (!retVal) {
1448 retVal = (argh_collision(x, y) != -1);
1451 if (!retVal) {
1452 retVal = (worm_collision(w, x, y) != NULL);
1454 return retVal;
1458 * This function
1459 * may be used to be stored in worm.fetch_worm_direction for
1460 * worms that are not controlled by humans but by artificial stupidity.
1461 * A direction is searched that doesn't lead to collision but to the nearest
1462 * food - but not very intelligent. The direction is written to the specified
1463 * worm.
1464 * @param struct worm *w - The worm of which the direction
1465 * is altered.
1467 static void virtual_player(struct worm *w)
1469 bool isright;
1470 int plana, planb, planc;
1471 /* find the next lunch */
1472 int nearestfood = get_nearest_food(w->x[w->head], w->y[w->head]);
1474 /* determine in which direction it is */
1476 /* in front of me? */
1477 bool infront = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1479 /* left right of me? */
1480 int olddir = get_worm_dir(w);
1481 set_worm_dir(w, (olddir + 1) % 4);
1482 isright = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1483 set_worm_dir(w, olddir);
1485 /* detect situation, set strategy */
1486 if (infront) {
1487 if (isright) {
1488 plana = olddir;
1489 planb = (olddir + 1) % 4;
1490 planc = (olddir + 3) % 4;
1491 } else {
1492 plana = olddir;
1493 planb = (olddir + 3) % 4;
1494 planc = (olddir + 1) % 4;
1496 } else {
1497 if (isright) {
1498 plana = (olddir + 1) % 4;
1499 planb = olddir;
1500 planc = (olddir + 3) % 4;
1501 } else {
1502 plana = (olddir + 3) % 4;
1503 planb = olddir;
1504 planc = (olddir + 1) % 4;
1508 /* test for collision */
1509 set_worm_dir(w, plana);
1510 if (will_worm_collide(w)){
1512 /* plan b */
1513 set_worm_dir(w, planb);
1515 /* test for collision */
1516 if (will_worm_collide(w)) {
1518 /* plan c */
1519 set_worm_dir(w, planc);
1525 * prints out the score board with all the status information
1526 * about the game.
1528 static void score_board(void)
1530 char buf[15];
1531 char* buf2 = NULL;
1532 int i;
1533 int y = 0;
1534 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1535 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, 0,
1536 LCD_WIDTH - FIELD_RECT_WIDTH - 2, LCD_HEIGHT);
1537 rb->lcd_set_drawmode(DRMODE_SOLID);
1538 for (i = 0; i < worm_count; i++) {
1539 int score = get_score(&worms[i]);
1541 /* high score */
1542 if (worms[i].fetch_worm_direction != virtual_player){
1543 if (highscore < score) {
1544 highscore = score;
1548 /* length */
1549 rb->snprintf(buf, sizeof (buf),"Len:%d", score);
1551 /* worm state */
1552 switch (check_collision(&worms[i])) {
1553 case COLLISION_NONE:
1554 if (worms[i].growing > 0)
1555 buf2 = "Growing";
1556 else {
1557 if (worms[i].alive)
1558 buf2 = "Hungry";
1559 else
1560 buf2 = "Wormed";
1562 break;
1564 case COLLISION_WORM:
1565 buf2 = "Wormed";
1566 break;
1568 case COLLISION_FOOD:
1569 buf2 = "Growing";
1570 break;
1572 case COLLISION_ARGH:
1573 buf2 = "Argh";
1574 break;
1576 case COLLISION_FIELD:
1577 buf2 = "Crashed";
1578 break;
1580 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y , buf);
1581 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y+8, buf2);
1583 if (!worms[i].alive){
1584 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1585 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, y,
1586 LCD_WIDTH - FIELD_RECT_WIDTH - 2, 17);
1587 rb->lcd_set_drawmode(DRMODE_SOLID);
1589 y += 19;
1591 rb->snprintf(buf , sizeof(buf), "Hs: %d", highscore);
1592 #ifndef DEBUG_WORMLET
1593 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, buf);
1594 #else
1595 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout);
1596 #endif
1600 * Checks for collisions of the worm and its environment and
1601 * takes appropriate actions like growing the worm or killing it.
1602 * @return bool Returns true if the worm is dead. Returns
1603 * false if the worm is healthy, up and creeping.
1605 static bool process_collisions(struct worm *w)
1607 int index = -1;
1609 w->alive &= !field_collision(w);
1611 if (w->alive) {
1613 /* check if food was eaten */
1614 index = food_collision(w->x[w->head], w->y[w->head]);
1615 if (index != -1){
1616 int i;
1618 clear_food(index);
1619 make_food(index);
1620 draw_food(index);
1622 for (i = 0; i < arghs_per_food; i++) {
1623 argh_count++;
1624 if (argh_count > MAX_ARGH)
1625 argh_count = MAX_ARGH;
1626 make_argh(argh_count - 1);
1627 draw_argh(argh_count - 1);
1630 add_growing(w, worm_food);
1632 draw_worm(w);
1635 /* check if argh was eaten */
1636 else {
1637 index = argh_collision(w->x[w->head], w->y[w->head]);
1638 if (index != -1) {
1639 w->alive = false;
1641 else {
1642 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) {
1643 w->alive = false;
1648 return !w->alive;
1652 * The main loop of the game.
1653 * @return bool Returns true if the game ended
1654 * with a dead worm. Returns false if the user
1655 * aborted the game manually.
1657 static int run(void)
1659 int button = 0;
1660 int wormDead = false;
1661 bool paused = false;
1663 /* ticks are counted to compensate speed variations */
1664 long cycle_start = 0, cycle_end = 0;
1665 #ifdef DEBUG_WORMLET
1666 int ticks_to_max_cycle_reset = 20;
1667 long max_cycle = 0;
1668 char buf[20];
1669 #endif
1671 /* initialize the board and so on */
1672 init_wormlet();
1674 cycle_start = *rb->current_tick;
1675 /* change the direction of the worm */
1676 while (!wormDead)
1678 int i;
1679 long cycle_duration=0;
1681 #ifdef HAS_BUTTON_HOLD
1682 if (rb->button_hold())
1683 paused = true;
1684 #endif
1686 switch (button) {
1687 case BTN_STARTPAUSE:
1688 paused = !paused;
1689 break;
1690 case BTN_STOPRESET:
1691 if (paused)
1692 return 1; /* restart game */
1693 else
1694 paused = true;
1695 break;
1696 #ifdef BTN_RC_QUIT
1697 case BTN_RC_QUIT:
1698 #endif
1699 case BTN_QUIT:
1700 return 2; /* back to menu */
1701 break;
1703 if (!paused)
1705 switch (button) {
1706 case BTN_DIR_UP:
1707 if (players == 1 && !use_remote) {
1708 player1_dir = NORTH;
1710 break;
1712 case BTN_DIR_DOWN:
1713 if (players == 1 && !use_remote) {
1714 player1_dir = SOUTH;
1716 break;
1718 case BTN_DIR_LEFT:
1719 if (players != 1 || use_remote) {
1720 player1_dir = (player1_dir + 3) % 4;
1721 } else {
1722 player1_dir = WEST;
1724 break;
1726 case BTN_DIR_RIGHT:
1727 if (players != 1 || use_remote) {
1728 player1_dir = (player1_dir + 1) % 4;
1729 } else {
1730 player1_dir = EAST;
1732 break;
1734 #ifdef MULTIPLAYER
1735 case BTN_PLAYER2_DIR1:
1736 player2_dir = (player2_dir + 3) % 4;
1737 break;
1739 case BTN_PLAYER2_DIR2:
1740 player2_dir = (player2_dir + 1) % 4;
1741 break;
1742 #endif
1744 #ifdef REMOTE
1745 case BTN_RC_UP:
1746 player3_dir = (player3_dir + 1) % 4;
1747 break;
1749 case BTN_RC_DOWN:
1750 player3_dir = (player3_dir + 3) % 4;
1751 break;
1752 #endif
1756 for (i = 0; i < worm_count; i++) {
1757 worms[i].fetch_worm_direction(&worms[i]);
1760 wormDead = true;
1761 for (i = 0; i < worm_count; i++){
1762 struct worm *w = &worms[i];
1763 move_worm(w);
1764 wormDead &= process_collisions(w);
1765 draw_worm(w);
1767 score_board();
1768 rb->lcd_update();
1769 if (button == BTN_STOPRESET) {
1770 wormDead = true;
1773 /* here the wormlet game cycle ends
1774 thus the current tick is stored
1775 as end time */
1776 cycle_end = *rb->current_tick;
1778 /* The duration of the game cycle */
1779 cycle_duration = cycle_end - cycle_start;
1780 cycle_duration = MAX(0, cycle_duration);
1781 cycle_duration = MIN(speed -1, cycle_duration);
1784 #ifdef DEBUG_WORMLET
1785 ticks_to_max_cycle_reset--;
1786 if (ticks_to_max_cycle_reset <= 0) {
1787 max_cycle = 0;
1790 if (max_cycle < cycle_duration) {
1791 max_cycle = cycle_duration;
1792 ticks_to_max_cycle_reset = 20;
1794 rb->snprintf(buf, sizeof buf, "ticks %d", max_cycle);
1795 set_debug_out(buf);
1796 #endif
1798 /* adjust the number of ticks to wait for a button.
1799 This ensures that a complete game cycle including
1800 user input runs in constant time */
1801 button = rb->button_get_w_tmo(speed - cycle_duration);
1802 cycle_start = *rb->current_tick;
1805 rb->splash(HZ*2, "Game Over!");
1807 return 2; /* back to menu */
1810 #ifdef DEBUG_WORMLET
1813 * Just a test routine that checks that worm_food_collision works
1814 * in some typical situations.
1816 static void test_worm_food_collision(void)
1818 int collision_count = 0;
1819 int i;
1820 rb->lcd_clear_display();
1821 init_worm(&worms[0], 10, 10);
1822 add_growing(&worms[0], 10);
1823 set_worm_dir(&worms[0], EAST);
1824 for (i = 0; i < 10; i++) {
1825 move_worm(&worms[0]);
1826 draw_worm(&worms[0]);
1829 set_worm_dir(&worms[0], SOUTH);
1830 for (i = 0; i < 10; i++) {
1831 move_worm(&worms[0]);
1832 draw_worm(&worms[0]);
1835 foodx[0] = 15;
1836 foody[0] = 12;
1837 for (foody[0] = 20; foody[0] > 0; foody[0] --) {
1838 char buf[20];
1839 bool collision;
1840 draw_worm(&worms[0]);
1841 draw_food(0);
1842 collision = worm_food_collision(&worms[0], 0);
1843 if (collision) {
1844 collision_count++;
1846 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1847 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1848 rb->lcd_update();
1850 if (collision_count != food_size) {
1851 rb->button_get(true);
1855 foody[0] = 15;
1856 for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) {
1857 char buf[20];
1858 bool collision;
1859 draw_worm(&worms[0]);
1860 draw_food(0);
1861 collision = worm_food_collision(&worms[0], 0);
1862 if (collision) {
1863 collision_count ++;
1865 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1866 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1867 rb->lcd_update();
1869 if (collision_count != food_size * 2) {
1870 rb->button_get(true);
1875 static bool expensive_worm_in_rect(struct worm *w, int rx, int ry, int rw, int rh)
1877 int x, y;
1878 bool retVal = false;
1879 for (x = rx; x < rx + rw; x++){
1880 for (y = ry; y < ry + rh; y++) {
1881 if (specific_worm_collision(w, x, y) != -1) {
1882 retVal = true;
1886 return retVal;
1889 static void test_worm_argh_collision(void)
1891 int i;
1892 int dir;
1893 int collision_count = 0;
1894 rb->lcd_clear_display();
1895 init_worm(&worms[0], 10, 10);
1896 add_growing(&worms[0], 40);
1897 for (dir = 0; dir < 4; dir++) {
1898 set_worm_dir(&worms[0], (EAST + dir) % 4);
1899 for (i = 0; i < 10; i++) {
1900 move_worm(&worms[0]);
1901 draw_worm(&worms[0]);
1905 arghx[0] = 12;
1906 for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){
1907 char buf[20];
1908 bool collision;
1909 draw_argh(0);
1910 collision = worm_argh_collision(&worms[0], 0);
1911 if (collision) {
1912 collision_count ++;
1914 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1915 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1916 rb->lcd_update();
1918 if (collision_count != argh_size * 2) {
1919 rb->button_get(true);
1922 arghy[0] = 12;
1923 for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){
1924 char buf[20];
1925 bool collision;
1926 draw_argh(0);
1927 collision = worm_argh_collision(&worms[0], 0);
1928 if (collision) {
1929 collision_count ++;
1931 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1932 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1933 rb->lcd_update();
1935 if (collision_count != argh_size * 4) {
1936 rb->button_get(true);
1940 static int testline_in_rect(void)
1942 int testfailed = -1;
1944 int rx = 10;
1945 int ry = 15;
1946 int rw = 20;
1947 int rh = 25;
1949 /* Test 1 */
1950 int x1 = 12;
1951 int y1 = 8;
1952 int x2 = 12;
1953 int y2 = 42;
1955 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1956 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1957 rb->lcd_drawrect(rx, ry, rw, rh);
1958 rb->lcd_drawline(x1, y1, x2, y2);
1959 rb->lcd_update();
1960 rb->lcd_putsxy(0, 0, "failed 1");
1961 rb->button_get(true);
1962 testfailed = 1;
1965 /* test 2 */
1966 y2 = 20;
1967 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1968 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1969 rb->lcd_drawrect(rx, ry, rw, rh);
1970 rb->lcd_drawline(x1, y1, x2, y2);
1971 rb->lcd_putsxy(0, 0, "failed 2");
1972 rb->lcd_update();
1973 rb->button_get(true);
1974 testfailed = 2;
1977 /* test 3 */
1978 y1 = 30;
1979 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1980 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1981 rb->lcd_drawrect(rx, ry, rw, rh);
1982 rb->lcd_drawline(x1, y1, x2, y2);
1983 rb->lcd_putsxy(0, 0, "failed 3");
1984 rb->lcd_update();
1985 rb->button_get(true);
1986 testfailed = 3;
1989 /* test 4 */
1990 y2 = 45;
1991 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1992 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1993 rb->lcd_drawrect(rx, ry, rw, rh);
1994 rb->lcd_drawline(x1, y1, x2, y2);
1995 rb->lcd_putsxy(0, 0, "failed 4");
1996 rb->lcd_update();
1997 rb->button_get(true);
1998 testfailed = 4;
2001 /* test 5 */
2002 y1 = 50;
2003 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2004 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2005 rb->lcd_drawrect(rx, ry, rw, rh);
2006 rb->lcd_drawline(x1, y1, x2, y2);
2007 rb->lcd_putsxy(0, 0, "failed 5");
2008 rb->lcd_update();
2009 rb->button_get(true);
2010 testfailed = 5;
2013 /* test 6 */
2014 y1 = 5;
2015 y2 = 7;
2016 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2017 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2018 rb->lcd_drawrect(rx, ry, rw, rh);
2019 rb->lcd_drawline(x1, y1, x2, y2);
2020 rb->lcd_putsxy(0, 0, "failed 6");
2021 rb->lcd_update();
2022 rb->button_get(true);
2023 testfailed = 6;
2026 /* test 7 */
2027 x1 = 8;
2028 y1 = 20;
2029 x2 = 35;
2030 y2 = 20;
2031 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2032 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2033 rb->lcd_drawrect(rx, ry, rw, rh);
2034 rb->lcd_drawline(x1, y1, x2, y2);
2035 rb->lcd_putsxy(0, 0, "failed 7");
2036 rb->lcd_update();
2037 rb->button_get(true);
2038 testfailed = 7;
2041 /* test 8 */
2042 x2 = 12;
2043 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2044 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2045 rb->lcd_drawrect(rx, ry, rw, rh);
2046 rb->lcd_drawline(x1, y1, x2, y2);
2047 rb->lcd_putsxy(0, 0, "failed 8");
2048 rb->lcd_update();
2049 rb->button_get(true);
2050 testfailed = 8;
2053 /* test 9 */
2054 x1 = 25;
2055 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2056 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2057 rb->lcd_drawrect(rx, ry, rw, rh);
2058 rb->lcd_drawline(x1, y1, x2, y2);
2059 rb->lcd_putsxy(0, 0, "failed 9");
2060 rb->lcd_update();
2061 rb->button_get(true);
2062 testfailed = 9;
2065 /* test 10 */
2066 x2 = 37;
2067 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2068 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2069 rb->lcd_drawrect(rx, ry, rw, rh);
2070 rb->lcd_drawline(x1, y1, x2, y2);
2071 rb->lcd_putsxy(0, 0, "failed 10");
2072 rb->lcd_update();
2073 rb->button_get(true);
2074 testfailed = 10;
2077 /* test 11 */
2078 x1 = 42;
2079 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2080 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2081 rb->lcd_drawrect(rx, ry, rw, rh);
2082 rb->lcd_drawline(x1, y1, x2, y2);
2083 rb->lcd_putsxy(0, 0, "failed 11");
2084 rb->lcd_update();
2085 rb->button_get(true);
2086 testfailed = 11;
2089 /* test 12 */
2090 x1 = 5;
2091 x2 = 7;
2092 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2093 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2094 rb->lcd_drawrect(rx, ry, rw, rh);
2095 rb->lcd_drawline(x1, y1, x2, y2);
2096 rb->lcd_putsxy(0, 0, "failed 12");
2097 rb->lcd_update();
2098 rb->button_get(true);
2099 testfailed = 12;
2102 /* test 13 */
2103 rx = 9;
2104 ry = 15;
2105 rw = food_size;
2106 rh = food_size;
2108 x1 = 10;
2109 y1 = 10;
2110 x2 = 10;
2111 y2 = 20;
2112 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2113 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2114 rb->lcd_drawrect(rx, ry, rw, rh);
2115 rb->lcd_drawline(x1, y1, x2, y2);
2116 rb->lcd_putsxy(0, 0, "failed 13");
2117 rb->lcd_update();
2118 rb->button_get(true);
2119 testfailed = 13;
2122 /* test 14 */
2123 rx = 9;
2124 ry = 15;
2125 rw = 4;
2126 rh = 4;
2128 x1 = 10;
2129 y1 = 10;
2130 x2 = 10;
2131 y2 = 19;
2132 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2133 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2134 rb->lcd_drawline(x1, y1, x2, y2);
2135 rb->lcd_invertrect(rx, ry, rw, rh);
2136 rb->lcd_putsxy(0, 0, "failed 14");
2137 rb->lcd_update();
2138 rb->button_get(true);
2139 testfailed = 14;
2142 rb->lcd_clear_display();
2144 return testfailed;
2148 * Just a test routine to test wether specific_worm_collision might work properly
2150 static int test_specific_worm_collision(void)
2152 int collisions = 0;
2153 int dir;
2154 int x = 0;
2155 int y = 0;
2156 char buf[20];
2157 rb->lcd_clear_display();
2158 init_worm(&worms[0], 10, 20);
2159 add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH);
2161 for (dir = EAST; dir < EAST + 4; dir++) {
2162 int i;
2163 set_worm_dir(&worms[0], dir % 4);
2164 for (i = 0; i < 5; i++) {
2165 if (!(dir % 4 == NORTH && i == 9)) {
2166 move_worm(&worms[0]);
2167 draw_worm(&worms[0]);
2172 for (y = 15; y < 30; y ++){
2173 for (x = 5; x < 20; x++) {
2174 if (specific_worm_collision(&worms[0], x, y) != -1) {
2175 collisions ++;
2177 rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
2178 rb->snprintf(buf, sizeof buf, "collisions %d", collisions);
2179 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2180 rb->lcd_update();
2183 if (collisions != 21) {
2184 rb->button_get(true);
2186 return collisions;
2189 static void test_make_argh(void)
2191 int dir;
2192 int seed = 0;
2193 int hit = 0;
2194 int failures = 0;
2195 int last_failures = 0;
2196 int i, worm_idx;
2197 rb->lcd_clear_display();
2198 worm_count = 3;
2200 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2201 init_worm(&worms[worm_idx], 10 + worm_idx * 20, 20);
2202 add_growing(&worms[worm_idx], 40 - INITIAL_WORM_LENGTH);
2205 for (dir = EAST; dir < EAST + 4; dir++) {
2206 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2207 set_worm_dir(&worms[worm_idx], dir % 4);
2208 for (i = 0; i < 10; i++) {
2209 if (!(dir % 4 == NORTH && i == 9)) {
2210 move_worm(&worms[worm_idx]);
2211 draw_worm(&worms[worm_idx]);
2217 rb->lcd_update();
2219 for (seed = 0; hit < 20; seed += 2) {
2220 char buf[20];
2221 int x, y;
2222 rb->srand(seed);
2223 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
2224 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
2226 for (worm_idx = 0; worm_idx < worm_count; worm_idx++){
2227 if (expensive_worm_in_rect(&worms[worm_idx], x, y, argh_size, argh_size)) {
2228 int tries = 0;
2229 rb->srand(seed);
2231 tries = make_argh(0);
2232 if ((x == arghx[0] && y == arghy[0]) || tries < 2) {
2233 failures ++;
2236 rb->snprintf(buf, sizeof buf, "(%d;%d) fail%d try%d",
2237 x, y, failures, tries);
2238 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2239 rb->lcd_update();
2240 rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y,
2241 argh_size, argh_size);
2242 rb->lcd_update();
2243 draw_argh(0);
2244 rb->lcd_update();
2245 rb->lcd_invertrect(x + FIELD_RECT_X, y + FIELD_RECT_Y,
2246 argh_size, argh_size);
2247 rb->lcd_clearrect(arghx[0] + FIELD_RECT_X, arghy[0] + FIELD_RECT_Y,
2248 argh_size, argh_size);
2250 if (failures > last_failures) {
2251 rb->button_get(true);
2253 last_failures = failures;
2254 hit ++;
2260 static void test_worm_argh_collision_in_moves(void) {
2261 int hit_count = 0;
2262 int i;
2263 rb->lcd_clear_display();
2264 init_worm(&worms[0], 10, 20);
2266 arghx[0] = 20;
2267 arghy[0] = 18;
2268 draw_argh(0);
2270 set_worm_dir(&worms[0], EAST);
2271 for (i = 0; i < 20; i++) {
2272 char buf[20];
2273 move_worm(&worms[0]);
2274 draw_worm(&worms[0]);
2275 if (worm_argh_collision_in_moves(&worms[0], 0, 5)){
2276 hit_count ++;
2278 rb->snprintf(buf, sizeof buf, "in 5 moves hits: %d", hit_count);
2279 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2280 rb->lcd_update();
2282 if (hit_count != argh_size + 5) {
2283 rb->button_get(true);
2286 #endif /* DEBUG_WORMLET */
2289 * Reverts default settings
2291 static void default_settings(void)
2293 arghs_per_food = ARGHS_PER_FOOD;
2294 argh_size = ARGH_SIZE;
2295 food_size = FOOD_SIZE;
2296 speed = SPEED;
2297 worm_food = WORM_PER_FOOD;
2298 players = 1;
2299 worm_count = MAX_WORMS;
2300 use_remote = false;
2301 return;
2305 * Launches the wormlet game
2307 static bool launch_wormlet(void)
2309 int game_result = 1;
2311 rb->lcd_clear_display();
2313 /* Turn off backlight timeout */
2314 backlight_force_on(); /* backlight control in lib/helper.c */
2316 /* start the game */
2317 while (game_result == 1)
2318 game_result = run();
2320 switch (game_result)
2322 case 2:
2323 /* Turn on backlight timeout (revert to settings) */
2324 backlight_use_settings(); /* backlight control in lib/helper.c */
2325 return false;
2326 break;
2328 return false;
2332 * Main entry point
2334 enum plugin_status plugin_start(const void* parameter)
2336 int result;
2337 int menu_quit = 0;
2338 int new_setting;
2340 (void)(parameter);
2342 default_settings();
2343 if (configfile_load(SETTINGS_FILENAME, config,
2344 sizeof(config)/sizeof(*config),
2345 SETTINGS_MIN_VERSION ) < 0)
2347 /* If the loading failed, save a new config file (as the disk is
2348 already spinning) */
2349 configfile_save(SETTINGS_FILENAME, config,
2350 sizeof(config)/sizeof(*config),
2351 SETTINGS_VERSION);
2354 #ifdef HAVE_LCD_COLOR
2355 rb->lcd_set_foreground(COLOR_FG);
2356 rb->lcd_set_background(COLOR_BG);
2357 #endif
2359 #if LCD_DEPTH > 1
2360 rb->lcd_set_backdrop(NULL);
2361 #endif
2363 #ifdef DEBUG_WORMLET
2364 testline_in_rect();
2365 test_worm_argh_collision_in_moves();
2366 test_make_argh();
2367 test_worm_food_collision();
2368 test_worm_argh_collision();
2369 test_specific_worm_collision();
2370 #endif
2372 /* Setup screen */
2374 static const struct opt_items noyes[2] = {
2375 { "No", -1 },
2376 { "Yes", -1 },
2379 static const struct opt_items remoteonly_option[1] = {
2380 { "Remote Control", -1 }
2383 static const struct opt_items key24_option[2] = {
2384 { "4 Key Control", -1 },
2385 { "2 Key Control", -1 }
2388 #ifdef REMOTE
2389 static const struct opt_items remote_option[2] = {
2390 { "Remote Control", -1 },
2391 { "No Rem. Control", -1 }
2393 #else
2394 static const struct opt_items key2_option[1] = {
2395 { "2 Key Control", -1 }
2397 #endif
2399 static const struct opt_items nokey_option[1] = {
2400 { "Out of Control", -1 }
2403 MENUITEM_STRINGLIST(menu, "Wormlet Menu", NULL, "Play Wormlet!",
2404 "Number of Worms", "Number of Players", "Control Style",
2405 "Worm Growth Per Food","Worm Speed","Arghs Per Food",
2406 "Argh Size","Food Size","Revert to Default Settings",
2407 "Playback Control", "Quit");
2409 rb->button_clear_queue();
2411 while (!menu_quit) {
2412 switch(rb->do_menu(&menu, &result, NULL, false))
2414 case 0:
2415 rb->lcd_setfont(FONT_SYSFIXED);
2416 launch_wormlet();
2417 break;
2418 case 1:
2419 rb->set_int("Number of Worms", "", UNIT_INT, &worm_count, NULL,
2420 1, 1, 3, NULL);
2421 if (worm_count < players) {
2422 worm_count = players;
2424 break;
2425 case 2:
2426 #ifdef MULTIPLAYER
2427 rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
2428 1, 0, 4, NULL);
2429 #else
2430 rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
2431 1, 0, 2, NULL);
2432 #endif
2433 if (players > worm_count) {
2434 worm_count = players;
2436 if (players > 2) {
2437 use_remote = true;
2439 break;
2440 case 3:
2441 switch(players) {
2442 case 0:
2443 rb->set_option("Control Style",&use_remote,INT,
2444 nokey_option, 1, NULL);
2445 break;
2446 case 1:
2447 rb->set_option("Control Style",&use_remote,INT,
2448 key24_option, 2, NULL);
2449 break;
2450 case 2:
2451 #ifdef REMOTE
2452 rb->set_option("Control Style",&use_remote,INT,
2453 remote_option, 2, NULL);
2454 #else
2455 rb->set_option("Control Style",&use_remote,INT,
2456 key2_option, 1, NULL);
2457 #endif
2458 break;
2459 case 3:
2460 rb->set_option("Control Style",&use_remote,INT,
2461 remoteonly_option, 1, NULL);
2462 break;
2464 break;
2465 case 4:
2466 rb->set_int("Worm Growth Per Food", "", UNIT_INT, &worm_food,
2467 NULL, 1, 0, 15, NULL);
2468 break;
2469 case 5:
2470 new_setting = 20 - speed;
2471 rb->set_int("Worm Speed", "", UNIT_INT, &new_setting,
2472 NULL, 1, 0, 20, NULL);
2473 speed = 20 - new_setting;
2474 break;
2475 case 6:
2476 rb->set_int("Arghs Per Food", "", UNIT_INT, &arghs_per_food,
2477 NULL, 1, 0, 8, NULL);
2478 break;
2479 case 7:
2480 rb->set_int("Argh Size", "", UNIT_INT, &argh_size,
2481 NULL, 1, 2, 10, NULL);
2482 break;
2483 case 8:
2484 rb->set_int("Food Size", "", UNIT_INT, &food_size,
2485 NULL, 1, 2, 10, NULL);
2486 break;
2487 case 9:
2488 new_setting = 0;
2489 rb->set_option("Reset Settings?", &new_setting, INT, noyes , 2, NULL);
2490 if (new_setting == 1)
2491 default_settings();
2492 break;
2493 case 10:
2494 playback_control(NULL);
2495 break;
2496 default:
2497 menu_quit=1;
2498 break;
2502 configfile_save(SETTINGS_FILENAME, config,
2503 sizeof(config)/sizeof(*config),
2504 SETTINGS_VERSION);
2506 return PLUGIN_OK;