fix wrong score recording.
[kugel-rb.git] / apps / plugins / wormlet.c
blob4cc3d5cec13d17260b594e5971e85141777023d5
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 #define PLAYERS_TEXT "UP/DN"
67 #define WORMS_TEXT "L/R"
68 #define KEY_CONTROL_TEXT "F1"
70 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
71 #define BTN_DIR_UP BUTTON_UP
72 #define BTN_DIR_DOWN BUTTON_DOWN
73 #define BTN_DIR_LEFT BUTTON_LEFT
74 #define BTN_DIR_RIGHT BUTTON_RIGHT
75 #define BTN_PLAYER2_DIR1 BUTTON_F2
76 #define BTN_PLAYER2_DIR2 BUTTON_F3
77 #define BTN_STARTPAUSE BUTTON_SELECT
78 #define BTN_QUIT BUTTON_OFF
79 #define BTN_STOPRESET BUTTON_ON
80 #define BTN_TOGGLE_KEYS BUTTON_F1
82 #define PLAYERS_TEXT "UP/DN"
83 #define WORMS_TEXT "L/R"
84 #define KEY_CONTROL_TEXT "F1"
86 #elif (CONFIG_KEYPAD == ONDIO_PAD)
87 #define BTN_DIR_UP BUTTON_UP
88 #define BTN_DIR_DOWN BUTTON_DOWN
89 #define BTN_DIR_LEFT BUTTON_LEFT
90 #define BTN_DIR_RIGHT BUTTON_RIGHT
91 #define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL)
92 #define BTN_QUIT (BUTTON_OFF|BUTTON_REL)
93 #define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU)
95 #define PLAYERS_TEXT "UP/DN"
96 #define WORMS_TEXT "L/R"
98 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
99 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
101 #define BTN_DIR_UP BUTTON_MENU
102 #define BTN_DIR_DOWN BUTTON_PLAY
103 #define BTN_DIR_LEFT BUTTON_LEFT
104 #define BTN_DIR_RIGHT BUTTON_RIGHT
105 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
106 #define BTN_QUIT (BUTTON_SELECT|BUTTON_MENU)
107 #define BTN_STOPRESET (BUTTON_SELECT|BUTTON_PLAY)
109 #define PLAYERS_TEXT "Menu/Play"
110 #define WORMS_TEXT "Left/Right"
112 #elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
114 #define BTN_DIR_UP BUTTON_UP
115 #define BTN_DIR_DOWN BUTTON_DOWN
116 #define BTN_DIR_LEFT BUTTON_LEFT
117 #define BTN_DIR_RIGHT BUTTON_RIGHT
118 #define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
119 #define BTN_QUIT BUTTON_OFF
120 #define BTN_STOPRESET BUTTON_ON
122 #define BTN_RC_QUIT BUTTON_RC_STOP
124 #define PLAYERS_TEXT "Up/Down"
125 #define WORMS_TEXT "Left/Right"
127 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
129 #define BTN_DIR_UP BUTTON_UP
130 #define BTN_DIR_DOWN BUTTON_DOWN
131 #define BTN_DIR_LEFT BUTTON_LEFT
132 #define BTN_DIR_RIGHT BUTTON_RIGHT
133 #define BTN_STARTPAUSE BUTTON_PLAY
134 #define BTN_QUIT BUTTON_POWER
135 #define BTN_STOPRESET BUTTON_REC
137 #define PLAYERS_TEXT "Up/Down"
138 #define WORMS_TEXT "Left/Right"
140 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
142 #define BTN_DIR_UP BUTTON_UP
143 #define BTN_DIR_DOWN BUTTON_DOWN
144 #define BTN_DIR_LEFT BUTTON_LEFT
145 #define BTN_DIR_RIGHT BUTTON_RIGHT
146 #define BTN_STARTPAUSE BUTTON_SELECT
147 #define BTN_QUIT BUTTON_POWER
148 #define BTN_STOPRESET BUTTON_A
150 #define PLAYERS_TEXT "Up/Down"
151 #define WORMS_TEXT "Left/Right"
154 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
155 (CONFIG_KEYPAD == SANSA_C200_PAD)
157 #define BTN_DIR_UP BUTTON_UP
158 #define BTN_DIR_DOWN BUTTON_DOWN
159 #define BTN_DIR_LEFT BUTTON_LEFT
160 #define BTN_DIR_RIGHT BUTTON_RIGHT
161 #define BTN_STARTPAUSE BUTTON_SELECT
162 #define BTN_QUIT BUTTON_POWER
163 #define BTN_STOPRESET BUTTON_REC
165 #define PLAYERS_TEXT "Up/Down"
166 #define WORMS_TEXT "Left/Right"
168 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
170 #define BTN_DIR_UP BUTTON_UP
171 #define BTN_DIR_DOWN BUTTON_DOWN
172 #define BTN_DIR_LEFT BUTTON_LEFT
173 #define BTN_DIR_RIGHT BUTTON_RIGHT
174 #define BTN_STARTPAUSE BUTTON_SELECT
175 #define BTN_QUIT BUTTON_POWER
176 #define BTN_STOPRESET BUTTON_HOME
178 #define PLAYERS_TEXT "Up/Down"
179 #define WORMS_TEXT "Left/Right"
181 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
183 #define BTN_DIR_UP BUTTON_UP
184 #define BTN_DIR_DOWN BUTTON_DOWN
185 #define BTN_DIR_LEFT BUTTON_LEFT
186 #define BTN_DIR_RIGHT BUTTON_RIGHT
187 #define BTN_STARTPAUSE BUTTON_SELECT
188 #define BTN_QUIT (BUTTON_HOME|BUTTON_REPEAT)
189 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
191 #define PLAYERS_TEXT "Up/Down"
192 #define WORMS_TEXT "Left/Right"
194 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
196 #define BTN_DIR_UP BUTTON_UP
197 #define BTN_DIR_DOWN BUTTON_DOWN
198 #define BTN_DIR_LEFT BUTTON_LEFT
199 #define BTN_DIR_RIGHT BUTTON_RIGHT
200 #define BTN_STARTPAUSE (BUTTON_SELECT | BUTTON_REL)
201 #define BTN_QUIT BUTTON_POWER
202 #define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
204 #define PLAYERS_TEXT "Up/Down"
205 #define WORMS_TEXT "Left/Right"
207 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
209 #define BTN_DIR_UP BUTTON_SCROLL_UP
210 #define BTN_DIR_DOWN BUTTON_SCROLL_DOWN
211 #define BTN_DIR_LEFT BUTTON_LEFT
212 #define BTN_DIR_RIGHT BUTTON_RIGHT
213 #define BTN_STARTPAUSE BUTTON_PLAY
214 #define BTN_QUIT BUTTON_POWER
215 #define BTN_STOPRESET BUTTON_REW
217 #define PLAYERS_TEXT "Up/Down"
218 #define WORMS_TEXT "Left/Right"
220 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
222 #define BTN_DIR_UP BUTTON_UP
223 #define BTN_DIR_DOWN BUTTON_DOWN
224 #define BTN_DIR_LEFT BUTTON_LEFT
225 #define BTN_DIR_RIGHT BUTTON_RIGHT
226 #define BTN_STARTPAUSE BUTTON_SELECT
227 #define BTN_QUIT BUTTON_BACK
228 #define BTN_STOPRESET BUTTON_MENU
230 #define PLAYERS_TEXT "Up/Down"
231 #define WORMS_TEXT "Left/Right"
233 #elif (CONFIG_KEYPAD == MROBE100_PAD)
235 #define BTN_DIR_UP BUTTON_UP
236 #define BTN_DIR_DOWN BUTTON_DOWN
237 #define BTN_DIR_LEFT BUTTON_LEFT
238 #define BTN_DIR_RIGHT BUTTON_RIGHT
239 #define BTN_STARTPAUSE BUTTON_SELECT
240 #define BTN_QUIT BUTTON_POWER
241 #define BTN_STOPRESET BUTTON_DISPLAY
243 #define PLAYERS_TEXT "Up/Down"
244 #define WORMS_TEXT "Left/Right"
246 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
248 #define BTN_DIR_UP BUTTON_RC_VOL_UP
249 #define BTN_DIR_DOWN BUTTON_RC_VOL_DOWN
250 #define BTN_DIR_LEFT BUTTON_RC_REW
251 #define BTN_DIR_RIGHT BUTTON_RC_FF
252 #define BTN_STARTPAUSE BUTTON_RC_PLAY
253 #define BTN_QUIT BUTTON_RC_REC
254 #define BTN_STOPRESET BUTTON_RC_MODE
256 #define PLAYERS_TEXT "VOL UP/DN"
257 #define WORMS_TEXT "REW/FF"
259 #elif (CONFIG_KEYPAD == COWOND2_PAD)
261 #define BTN_QUIT BUTTON_POWER
263 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
265 #define BTN_DIR_UP BUTTON_UP
266 #define BTN_DIR_DOWN BUTTON_DOWN
267 #define BTN_DIR_LEFT BUTTON_LEFT
268 #define BTN_DIR_RIGHT BUTTON_RIGHT
269 #define BTN_STARTPAUSE BUTTON_PLAY
270 #define BTN_QUIT BUTTON_BACK
271 #define BTN_STOPRESET BUTTON_MENU
273 #define PLAYERS_TEXT "Up/Down"
274 #define WORMS_TEXT "Left/Right"
276 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
278 #define BTN_DIR_UP BUTTON_UP
279 #define BTN_DIR_DOWN BUTTON_DOWN
280 #define BTN_DIR_LEFT BUTTON_LEFT
281 #define BTN_DIR_RIGHT BUTTON_RIGHT
282 #define BTN_STARTPAUSE BUTTON_MENU
283 #define BTN_QUIT BUTTON_POWER
284 #define BTN_STOPRESET BUTTON_VIEW
286 #define PLAYERS_TEXT "Up/Down"
287 #define WORMS_TEXT "Left/Right"
289 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || CONFIG_KEYPAD == MROBE500_PAD
291 #define BTN_QUIT BUTTON_POWER
293 #else
294 #error No keymap defined!
295 #endif
297 #ifdef HAVE_TOUCHSCREEN
298 #ifndef BTN_DIR_UP
299 #define BTN_DIR_UP BUTTON_TOPMIDDLE
300 #endif
301 #ifndef BTN_DIR_DOWN
302 #define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
303 #endif
304 #ifndef BTN_DIR_LEFT
305 #define BTN_DIR_LEFT BUTTON_MIDLEFT
306 #endif
307 #ifndef BTN_DIR_RIGHT
308 #define BTN_DIR_RIGHT BUTTON_MIDRIGHT
309 #endif
310 #ifndef BTN_STARTPAUSE
311 #define BTN_STARTPAUSE BUTTON_CENTER
312 #endif
313 #ifndef BTN_QUIT
314 #define BTN_QUIT BUTTON_TOPLEFT
315 #endif
316 #ifndef BTN_STOPRESET
317 #define BTN_STOPRESET BUTTON_TOPRIGHT
319 #endif
320 #ifndef PLAYERS_TEXT
321 #define PLAYERS_TEXT "Up/Down"
322 #endif
323 #ifndef WORMS_TEXT
324 #define WORMS_TEXT "Left/Right"
325 #endif
326 #endif
329 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
330 #define FOOD_SIZE 3
331 #define ARGH_SIZE 4
332 #define SPEED 14
333 #define MAX_WORM_SEGMENTS 128
334 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 64)
335 #define FOOD_SIZE 3
336 #define ARGH_SIZE 4
337 #define SPEED 14
338 #define MAX_WORM_SEGMENTS 128
339 #elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
340 #define FOOD_SIZE 3
341 #define ARGH_SIZE 4
342 #define SPEED 14
343 #define MAX_WORM_SEGMENTS 128
344 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
345 #define FOOD_SIZE 3
346 #define ARGH_SIZE 4
347 #define SPEED 12
348 #define MAX_WORM_SEGMENTS 128
349 #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
350 #define FOOD_SIZE 4
351 #define ARGH_SIZE 5
352 #define SPEED 10
353 #define MAX_WORM_SEGMENTS 128
354 #elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
355 #define FOOD_SIZE 4
356 #define ARGH_SIZE 5
357 #define SPEED 9
358 #define MAX_WORM_SEGMENTS 128
359 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
360 #define FOOD_SIZE 4
361 #define ARGH_SIZE 5
362 #define SPEED 8
363 #define MAX_WORM_SEGMENTS 256
364 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
365 #define FOOD_SIZE 4
366 #define ARGH_SIZE 5
367 #define SPEED 6
368 #define MAX_WORM_SEGMENTS 256
369 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
370 #define FOOD_SIZE 5
371 #define ARGH_SIZE 6
372 #define SPEED 4
373 #define MAX_WORM_SEGMENTS 512
374 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
375 #define FOOD_SIZE 5
376 #define ARGH_SIZE 6
377 #define SPEED 4
378 #define MAX_WORM_SEGMENTS 512
379 #elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240)
380 #define FOOD_SIZE 7
381 #define ARGH_SIZE 8
382 #define SPEED 4
383 #define MAX_WORM_SEGMENTS 512
384 #elif (LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400))
385 #define FOOD_SIZE 7
386 #define ARGH_SIZE 8
387 #define SPEED 4
388 #define MAX_WORM_SEGMENTS 512
389 #endif
391 #ifdef HAVE_LCD_COLOR
392 #define COLOR_WORM LCD_RGBPACK(80, 40, 0)
393 #define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
394 #define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
395 #define COLOR_FG LCD_RGBPACK(0, 0, 0)
396 #define COLOR_BG LCD_RGBPACK(181, 199, 231)
397 #endif
400 * All the properties that a worm has.
402 static struct worm {
403 /* The worm is stored in a ring of xy coordinates */
404 int x[MAX_WORM_SEGMENTS];
405 int y[MAX_WORM_SEGMENTS];
407 int head; /* index of the head within the buffer */
408 int tail; /* index of the tail within the buffer */
409 int growing; /* number of cyles the worm still keeps growing */
410 bool alive; /* the worms living state */
412 /* direction vector in which the worm moves */
413 int dirx; /* only values -1 0 1 allowed */
414 int diry; /* only values -1 0 1 allowed */
416 /* this method is used to fetch the direction the user
417 has selected. It can be one of the values
418 human_player1, human_player2, remote_player, virtual_player.
419 All these values are fuctions, that can change the direction
420 of the worm */
421 void (*fetch_worm_direction)(struct worm *w);
422 } worms[MAX_WORMS];
424 /* stores the highscore - besides it was scored by a virtual player */
425 static int highscore;
427 #define MAX_FOOD 5 /* maximal number of food items */
429 /* The arrays store the food coordinates */
430 static int foodx[MAX_FOOD];
431 static int foody[MAX_FOOD];
433 #define MAX_ARGH 100 /* maximal number of argh items */
434 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
436 /* The arrays store the argh coordinates */
437 static int arghx[MAX_ARGH];
438 static int arghy[MAX_ARGH];
440 /* the number of arghs that are currently in use */
441 static int argh_count;
443 /* the number of arghs per food, settable by user */
444 static int arghs_per_food = ARGHS_PER_FOOD;
445 /* the size of the argh, settable by user */
446 static int argh_size = ARGH_SIZE;
447 /* the size of the food, settable by user */
448 static int food_size = FOOD_SIZE;
449 /* the speed of the worm, settable by user */
450 static int speed = SPEED;
451 /* the amount a worm grows by eating a food, settable by user */
452 static int worm_food = WORM_PER_FOOD;
454 /* End additional variables */
456 #ifdef DEBUG_WORMLET
457 /* just a buffer used for debug output */
458 static char debugout[15];
459 #endif
461 /* the number of active worms (dead or alive) */
462 static int worm_count = MAX_WORMS;
464 /* in multiplayer mode: en- / disables the remote worm control
465 in singleplayer mode: toggles 4 / 2 button worm control */
466 static bool use_remote = false;
468 /* return values of check_collision */
469 #define COLLISION_NONE 0
470 #define COLLISION_WORM 1
471 #define COLLISION_FOOD 2
472 #define COLLISION_ARGH 3
473 #define COLLISION_FIELD 4
475 /* constants for use as directions.
476 Note that the values are ordered clockwise.
477 Thus increasing / decreasing the values
478 is equivalent to right / left turns. */
479 #define WEST 0
480 #define NORTH 1
481 #define EAST 2
482 #define SOUTH 3
484 /* direction of human player 1 */
485 static int player1_dir = EAST;
486 /* direction of human player 2 */
487 static int player2_dir = EAST;
488 /* direction of human player 3 */
489 static int player3_dir = EAST;
491 /* the number of (human) players that currently
492 control a worm */
493 static int players = 1;
495 #define SETTINGS_VERSION 1
496 #define SETTINGS_MIN_VERSION 1
497 #define SETTINGS_FILENAME "wormlet.cfg"
499 static struct configdata config[] =
501 {TYPE_INT, 0, 1024, { .int_p = &highscore }, "highscore", NULL},
502 {TYPE_INT, 0, 15, { .int_p = &arghs_per_food }, "arghs per food", NULL},
503 {TYPE_INT, 0, 15, { .int_p = &argh_size }, "argh size", NULL},
504 {TYPE_INT, 0, 15, { .int_p = &food_size }, "food size", NULL},
505 {TYPE_INT, 0, 3, { .int_p = &players }, "players", NULL},
506 {TYPE_INT, 0, 3, { .int_p = &worm_count }, "worms", NULL},
507 {TYPE_INT, 0, 20, { .int_p = &speed }, "speed", NULL},
508 {TYPE_INT, 0, 15, { .int_p = &worm_food }, "Worm Growth Per Food", NULL}
511 #ifdef DEBUG_WORMLET
512 static void set_debug_out(char *str){
513 strcpy(debugout, str);
515 #endif
518 * Returns the direction id in which the worm
519 * currently is creeping.
520 * @param struct worm *w The worm that is to be investigated.
521 * w Must not be null.
522 * @return int A value 0 <= value < 4
523 * Note the predefined constants NORTH, SOUTH, EAST, WEST
525 static int get_worm_dir(struct worm *w) {
526 int retVal ;
527 if (w->dirx == 0) {
528 if (w->diry == 1) {
529 retVal = SOUTH;
530 } else {
531 retVal = NORTH;
533 } else {
534 if (w->dirx == 1) {
535 retVal = EAST;
536 } else {
537 retVal = WEST;
540 return retVal;
544 * Set the direction of the specified worm with a direction id.
545 * Increasing the value by 1 means to turn the worm direction
546 * to right by 90 degree.
547 * @param struct worm *w The worm that is to be altered. w Must not be null.
548 * @param int dir The new direction in which the worm is to creep.
549 * dir must be 0 <= dir < 4. Use predefined constants
550 * NORTH, SOUTH, EAST, WEST
552 static void set_worm_dir(struct worm *w, int dir) {
553 switch (dir) {
554 case WEST:
555 w->dirx = -1;
556 w->diry = 0;
557 break;
558 case NORTH:
559 w->dirx = 0;
560 w->diry = - 1;
561 break;
562 case EAST:
563 w->dirx = 1;
564 w->diry = 0;
565 break;
566 case SOUTH:
567 w->dirx = 0;
568 w->diry = 1;
569 break;
574 * Returns the current length of the worm array. This
575 * is also a value for the number of bends that are in the worm.
576 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
578 static int get_worm_array_length(struct worm *w) {
579 /* initial simple calculation will be overwritten if wrong. */
580 int retVal = w->head - w->tail;
582 /* if the worm 'crosses' the boundaries of the ringbuffer */
583 if (retVal < 0) {
584 retVal = w->head + MAX_WORM_SEGMENTS - w->tail;
587 return retVal;
591 * Returns the score the specified worm. The score is the length
592 * of the worm.
593 * @param struct worm *w The worm that is to be investigated.
594 * w must not be null.
595 * @return int The length of the worm (>= 0).
597 static int get_score(struct worm *w) {
598 int retval = 0;
599 int length = get_worm_array_length(w);
600 int i;
601 for (i = 0; i < length; i++) {
603 /* The iteration iterates the length of the worm.
604 Here's the conversion to the true indices within the worm arrays. */
605 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
606 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
607 int startx = w->x[linestart];
608 int starty = w->y[linestart];
609 int endx = w->x[lineend];
610 int endy = w->y[lineend];
612 int minimum, maximum;
614 if (startx == endx) {
615 minimum = MIN(starty, endy);
616 maximum = MAX(starty, endy);
617 } else {
618 minimum = MIN(startx, endx);
619 maximum = MAX(startx, endx);
621 retval += abs(maximum - minimum);
623 return retval;
627 * Determines wether the line specified by startx, starty, endx, endy intersects
628 * the rectangle specified by x, y, width, height. Note that the line must be exactly
629 * horizontal or vertical (startx == endx or starty == endy).
630 * @param int startx The x coordinate of the start point of the line.
631 * @param int starty The y coordinate of the start point of the line.
632 * @param int endx The x coordinate of the end point of the line.
633 * @param int endy The y coordinate of the end point of the line.
634 * @param int x The x coordinate of the top left corner of the rectangle.
635 * @param int y The y coordinate of the top left corner of the rectangle.
636 * @param int width The width of the rectangle.
637 * @param int height The height of the rectangle.
638 * @return bool Returns true if the specified line intersects with the recangle.
640 static bool line_in_rect(int startx, int starty, int endx, int endy, int x, int y, int width, int height) {
641 bool retval = false;
642 int simple, simplemin, simplemax;
643 int compa, compb, compmin, compmax;
644 int temp;
645 if (startx == endx) {
646 simple = startx;
647 simplemin = x;
648 simplemax = x + width;
650 compa = starty;
651 compb = endy;
652 compmin = y;
653 compmax = y + height;
654 } else {
655 simple = starty;
656 simplemin = y;
657 simplemax = y + height;
659 compa = startx;
660 compb = endx;
661 compmin = x;
662 compmax = x + width;
665 temp = compa;
666 compa = MIN(compa, compb);
667 compb = MAX(temp, compb);
669 if (simplemin <= simple && simple <= simplemax) {
670 if ((compmin <= compa && compa <= compmax) ||
671 (compmin <= compb && compb <= compmax) ||
672 (compa <= compmin && compb >= compmax)) {
673 retval = true;
676 return retval;
680 * Tests wether the specified worm intersects with the rect.
681 * @param struct worm *w The worm to be investigated
682 * @param int x The x coordinate of the top left corner of the rect
683 * @param int y The y coordinate of the top left corner of the rect
684 * @param int widht The width of the rect
685 * @param int height The height of the rect
686 * @return bool Returns true if the worm intersects with the rect
688 static bool worm_in_rect(struct worm *w, int x, int y, int width, int height) {
689 bool retval = false;
692 /* get_worm_array_length is expensive -> buffer the value */
693 int wormLength = get_worm_array_length(w);
694 int i;
696 /* test each entry that is part of the worm */
697 for (i = 0; i < wormLength && retval == false; i++) {
699 /* The iteration iterates the length of the worm.
700 Here's the conversion to the true indices within the worm arrays. */
701 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
702 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
703 int startx = w->x[linestart];
704 int starty = w->y[linestart];
705 int endx = w->x[lineend];
706 int endy = w->y[lineend];
708 retval = line_in_rect(startx, starty, endx, endy, x, y, width, height);
711 return retval;
715 * Checks wether a specific food in the food arrays is at the
716 * specified coordinates.
717 * @param int foodIndex The index of the food in the food arrays
718 * @param int x the x coordinate.
719 * @param int y the y coordinate.
720 * @return Returns true if the coordinate hits the food specified by
721 * foodIndex.
723 static bool specific_food_collision(int foodIndex, int x, int y) {
724 bool retVal = false;
725 if (x >= foodx[foodIndex] &&
726 x < foodx[foodIndex] + food_size &&
727 y >= foody[foodIndex] &&
728 y < foody[foodIndex] + food_size) {
730 retVal = true;
732 return retVal;
736 * Returns the index of the food that is at the
737 * given coordinates. If no food is at the coordinates
738 * -1 is returned.
739 * @return int -1 <= value < MAX_FOOD
741 static int food_collision(int x, int y) {
742 int i = 0;
743 int retVal = -1;
744 for (i = 0; i < MAX_FOOD; i++) {
745 if (specific_food_collision(i, x, y)) {
746 retVal = i;
747 break;
750 return retVal;
754 * Checks wether a specific argh in the argh arrays is at the
755 * specified coordinates.
756 * @param int arghIndex The index of the argh in the argh arrays
757 * @param int x the x coordinate.
758 * @param int y the y coordinate.
759 * @return Returns true if the coordinate hits the argh specified by
760 * arghIndex.
762 static bool specific_argh_collision(int arghIndex, int x, int y) {
764 if ( x >= arghx[arghIndex] &&
765 y >= arghy[arghIndex] &&
766 x < arghx[arghIndex] + argh_size &&
767 y < arghy[arghIndex] + argh_size )
769 return true;
772 return false;
776 * Returns the index of the argh that is at the
777 * given coordinates. If no argh is at the coordinates
778 * -1 is returned.
779 * @param int x The x coordinate.
780 * @param int y The y coordinate.
781 * @return int -1 <= value < argh_count <= MAX_ARGH
783 static int argh_collision(int x, int y) {
784 int i = 0;
785 int retVal = -1;
787 /* search for the argh that has the specified coords */
788 for (i = 0; i < argh_count; i++) {
789 if (specific_argh_collision(i, x, y)) {
790 retVal = i;
791 break;
794 return retVal;
798 * Checks wether the worm collides with the food at the specfied food-arrays.
799 * @param int foodIndex The index of the food in the arrays. Ensure the value is
800 * 0 <= foodIndex <= MAX_FOOD
801 * @return Returns true if the worm collides with the specified food.
803 static bool worm_food_collision(struct worm *w, int foodIndex)
805 bool retVal = false;
807 retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex],
808 food_size - 1, food_size - 1);
810 return retVal;
814 * Returns true if the worm hits the argh within the next moves (unless
815 * the worm changes it's direction).
816 * @param struct worm *w - The worm to investigate
817 * @param int argh_idx - The index of the argh
818 * @param int moves - The number of moves that are considered.
819 * @return Returns false if the specified argh is not hit within the next
820 * moves.
822 static bool worm_argh_collision_in_moves(struct worm *w, int argh_idx, int moves){
823 bool retVal = false;
824 int x1, y1, x2, y2;
825 x1 = w->x[w->head];
826 y1 = w->y[w->head];
828 x2 = w->x[w->head] + moves * w->dirx;
829 y2 = w->y[w->head] + moves * w->diry;
831 retVal = line_in_rect(x1, y1, x2, y2, arghx[argh_idx], arghy[argh_idx],
832 argh_size, argh_size);
833 return retVal;
837 * Checks wether the worm collides with the argh at the specfied argh-arrays.
838 * @param int arghIndex The index of the argh in the arrays.
839 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
840 * @return Returns true if the worm collides with the specified argh.
842 static bool worm_argh_collision(struct worm *w, int arghIndex)
844 bool retVal = false;
846 retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex],
847 argh_size - 1, argh_size - 1);
849 return retVal;
853 * Find new coordinates for the food stored in foodx[index], foody[index]
854 * that don't collide with any other food or argh
855 * @param int index
856 * Ensure that 0 <= index < MAX_FOOD.
858 static void make_food(int index) {
860 int x = 0;
861 int y = 0;
862 bool collisionDetected = false;
863 int i;
865 do {
866 /* make coordinates for a new food so that
867 the entire food lies within the FIELD */
868 x = rb->rand() % (FIELD_RECT_WIDTH - food_size);
869 y = rb->rand() % (FIELD_RECT_HEIGHT - food_size);
871 /* Ensure that the new food doesn't collide with any
872 existing foods or arghs.
873 If one or more corners of the new food hit any existing
874 argh or food a collision is detected.
876 collisionDetected =
877 food_collision(x , y ) >= 0 ||
878 food_collision(x , y + food_size - 1) >= 0 ||
879 food_collision(x + food_size - 1, y ) >= 0 ||
880 food_collision(x + food_size - 1, y + food_size - 1) >= 0 ||
881 argh_collision(x , y ) >= 0 ||
882 argh_collision(x , y + food_size - 1) >= 0 ||
883 argh_collision(x + food_size - 1, y ) >= 0 ||
884 argh_collision(x + food_size - 1, y + food_size - 1) >= 0;
886 /* use coordinates for further testing */
887 foodx[index] = x;
888 foody[index] = y;
890 /* now test wether we accidently hit the worm with food ;) */
891 i = 0;
892 for (i = 0; i < worm_count && !collisionDetected; i++) {
893 collisionDetected |= worm_food_collision(&worms[i], index);
896 while (collisionDetected);
897 return;
901 * Clears a food from the lcd buffer.
902 * @param int index The index of the food arrays under which
903 * the coordinates of the desired food can be found. Ensure
904 * that the value is 0 <= index <= MAX_FOOD.
906 static void clear_food(int index)
908 /* remove the old food from the screen */
909 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
910 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
911 foody[index] + FIELD_RECT_Y,
912 food_size, food_size);
913 rb->lcd_set_drawmode(DRMODE_SOLID);
917 * Draws a food in the lcd buffer.
918 * @param int index The index of the food arrays under which
919 * the coordinates of the desired food can be found. Ensure
920 * that the value is 0 <= index <= MAX_FOOD.
922 static void draw_food(int index)
924 /* draw the food object */
925 #ifdef HAVE_LCD_COLOR
926 rb->lcd_set_foreground(COLOR_FOOD);
927 #endif
928 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
929 foody[index] + FIELD_RECT_Y,
930 food_size, food_size);
931 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
932 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X + 1,
933 foody[index] + FIELD_RECT_Y + 1,
934 food_size - 2, food_size - 2);
935 rb->lcd_set_drawmode(DRMODE_SOLID);
936 #ifdef HAVE_LCD_COLOR
937 rb->lcd_set_foreground(COLOR_FG);
938 #endif
942 * Find new coordinates for the argh stored in arghx[index], arghy[index]
943 * that don't collide with any other food or argh.
944 * @param int index
945 * Ensure that 0 <= index < argh_count < MAX_ARGH.
947 static void make_argh(int index)
949 int x = -1;
950 int y = -1;
951 bool collisionDetected = false;
952 int i;
954 do {
955 /* make coordinates for a new argh so that
956 the entire food lies within the FIELD */
957 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
958 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
960 /* Ensure that the new argh doesn't intersect with any
961 existing foods or arghs.
962 If one or more corners of the new argh hit any existing
963 argh or food an intersection is detected.
965 collisionDetected =
966 food_collision(x , y ) >= 0 ||
967 food_collision(x , y + argh_size - 1) >= 0 ||
968 food_collision(x + argh_size - 1, y ) >= 0 ||
969 food_collision(x + argh_size - 1, y + argh_size - 1) >= 0 ||
970 argh_collision(x , y ) >= 0 ||
971 argh_collision(x , y + argh_size - 1) >= 0 ||
972 argh_collision(x + argh_size - 1, y ) >= 0 ||
973 argh_collision(x + argh_size - 1, y + argh_size - 1) >= 0;
975 /* use the candidate coordinates to make a real argh */
976 arghx[index] = x;
977 arghy[index] = y;
979 /* now test wether we accidently hit the worm with argh ;) */
980 for (i = 0; i < worm_count && !collisionDetected; i++) {
981 collisionDetected |= worm_argh_collision(&worms[i], index);
982 collisionDetected |= worm_argh_collision_in_moves(&worms[i], index,
983 MIN_ARGH_DIST);
986 while (collisionDetected);
987 return;
991 * Draws an argh in the lcd buffer.
992 * @param int index The index of the argh arrays under which
993 * the coordinates of the desired argh can be found. Ensure
994 * that the value is 0 <= index < argh_count <= MAX_ARGH.
996 static void draw_argh(int index)
998 /* draw the new argh */
999 #ifdef HAVE_LCD_COLOR
1000 rb->lcd_set_foreground(COLOR_ARGH);
1001 #endif
1002 rb->lcd_fillrect(arghx[index] + FIELD_RECT_X,
1003 arghy[index] + FIELD_RECT_Y,
1004 argh_size, argh_size);
1005 #ifdef HAVE_LCD_COLOR
1006 rb->lcd_set_foreground(COLOR_FG);
1007 #endif
1010 static void virtual_player(struct worm *w);
1012 * Initialzes the specified worm with INITIAL_WORM_LENGTH
1013 * and the tail at the specified position. The worm will
1014 * be initialized alive and creeping EAST.
1015 * @param struct worm *w The worm that is to be initialized
1016 * @param int x The x coordinate at which the tail of the worm starts.
1017 * x must be 0 <= x < FIELD_RECT_WIDTH.
1018 * @param int y The y coordinate at which the tail of the worm starts
1019 * y must be 0 <= y < FIELD_RECT_WIDTH.
1021 static void init_worm(struct worm *w, int x, int y){
1022 /* initialize the worm size */
1023 w->head = 1;
1024 w->tail = 0;
1026 w->x[w->head] = x + 1;
1027 w->y[w->head] = y;
1029 w->x[w->tail] = x;
1030 w->y[w->tail] = y;
1032 /* set the initial direction the worm creeps to */
1033 w->dirx = 1;
1034 w->diry = 0;
1036 w->growing = INITIAL_WORM_LENGTH - 1;
1037 w->alive = true;
1038 w->fetch_worm_direction = virtual_player;
1042 * Writes the direction that was stored for
1043 * human player 1 into the specified worm. This function
1044 * may be used to be stored in worm.fetch_worm_direction.
1045 * The value of
1046 * the direction is read from player1_dir.
1047 * @param struct worm *w - The worm of which the direction
1048 * is altered.
1050 static void human_player1(struct worm *w) {
1051 set_worm_dir(w, player1_dir);
1055 * Writes the direction that was stored for
1056 * human player 2 into the specified worm. This function
1057 * may be used to be stored in worm.fetch_worm_direction.
1058 * The value of
1059 * the direction is read from player2_dir.
1060 * @param struct worm *w - The worm of which the direction
1061 * is altered.
1063 static void human_player2(struct worm *w) {
1064 set_worm_dir(w, player2_dir);
1068 * Writes the direction that was stored for
1069 * human player using a remote control
1070 * into the specified worm. This function
1071 * may be used to be stored in worm.fetch_worm_direction.
1072 * The value of
1073 * the direction is read from player3_dir.
1074 * @param struct worm *w - The worm of which the direction
1075 * is altered.
1077 static void remote_player(struct worm *w) {
1078 set_worm_dir(w, player3_dir);
1082 * Initializes the worm-, food- and argh-arrays, draws a frame,
1083 * makes some food and argh and display all that stuff.
1085 static void init_wormlet(void)
1087 int i;
1089 for (i = 0; i< worm_count; i++) {
1090 /* Initialize all the worm coordinates to center. */
1091 int x = (int)(FIELD_RECT_WIDTH / 2);
1092 int y = (int)((FIELD_RECT_HEIGHT - 20)/ 2) + i * 10;
1094 init_worm(&worms[i], x, y);
1097 player1_dir = EAST;
1098 player2_dir = EAST;
1099 player3_dir = EAST;
1101 if (players > 0) {
1102 worms[0].fetch_worm_direction = human_player1;
1105 if (players > 1) {
1106 if (use_remote) {
1107 worms[1].fetch_worm_direction = remote_player;
1108 } else {
1109 worms[1].fetch_worm_direction = human_player2;
1113 if (players > 2) {
1114 worms[2].fetch_worm_direction = human_player2;
1117 /* Needed when the game is restarted using BTN_STOPRESET */
1118 rb->lcd_clear_display();
1120 /* make and display some food and argh */
1121 argh_count = MAX_FOOD;
1122 for (i = 0; i < MAX_FOOD; i++) {
1123 make_food(i);
1124 draw_food(i);
1125 make_argh(i);
1126 draw_argh(i);
1129 /* draw the game field */
1130 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1131 rb->lcd_fillrect(0, 0, FIELD_RECT_WIDTH + 2, FIELD_RECT_HEIGHT + 2);
1132 rb->lcd_fillrect(1, 1, FIELD_RECT_WIDTH, FIELD_RECT_HEIGHT);
1133 rb->lcd_set_drawmode(DRMODE_SOLID);
1135 /* make everything visible */
1136 rb->lcd_update();
1141 * Move the worm one step further if it is alive.
1142 * The direction in which the worm moves is taken from dirx and diry.
1143 * move_worm decreases growing if > 0. While the worm is growing the tail
1144 * is left untouched.
1145 * @param struct worm *w The worm to move. w must not be NULL.
1147 static void move_worm(struct worm *w)
1149 if (w->alive) {
1150 /* determine the head point and its precessor */
1151 int headx = w->x[w->head];
1152 int heady = w->y[w->head];
1153 int prehead = (w->head + MAX_WORM_SEGMENTS - 1) % MAX_WORM_SEGMENTS;
1154 int preheadx = w->x[prehead];
1155 int preheady = w->y[prehead];
1157 /* determine the old direction */
1158 int olddirx;
1159 int olddiry;
1160 if (headx == preheadx) {
1161 olddirx = 0;
1162 olddiry = (heady > preheady) ? 1 : -1;
1163 } else {
1164 olddiry = 0;
1165 olddirx = (headx > preheadx) ? 1 : -1;
1168 /* olddir == dir?
1169 a change of direction means a new segment
1170 has been opened */
1171 if (olddirx != w->dirx ||
1172 olddiry != w->diry) {
1173 w->head = (w->head + 1) % MAX_WORM_SEGMENTS;
1176 /* new head position */
1177 w->x[w->head] = headx + w->dirx;
1178 w->y[w->head] = heady + w->diry;
1181 /* while the worm is growing no tail procession is necessary */
1182 if (w->growing > 0) {
1183 /* update the worms grow state */
1184 w->growing--;
1187 /* if the worm isn't growing the tail has to be dragged */
1188 else {
1189 /* index of the end of the tail segment */
1190 int tail_segment_end = (w->tail + 1) % MAX_WORM_SEGMENTS;
1192 /* drag the end of the tail */
1193 /* only one coordinate has to be altered. Here it is
1194 determined which one */
1195 int dir = 0; /* specifies wether the coord has to be in- or decreased */
1196 if (w->x[w->tail] == w->x[tail_segment_end]) {
1197 dir = (w->y[w->tail] - w->y[tail_segment_end] < 0) ? 1 : -1;
1198 w->y[w->tail] += dir;
1199 } else {
1200 dir = (w->x[w->tail] - w->x[tail_segment_end] < 0) ? 1 : -1;
1201 w->x[w->tail] += dir;
1204 /* when the tail has been dragged so far that it meets
1205 the next segment start the tail segment is obsolete and
1206 must be freed */
1207 if (w->x[w->tail] == w->x[tail_segment_end] &&
1208 w->y[w->tail] == w->y[tail_segment_end]){
1210 /* drop the last tail point */
1211 w->tail = tail_segment_end;
1218 * Draws the head and clears the tail of the worm in
1219 * the display buffer. lcd_update() is NOT called thus
1220 * the caller has to take care that the buffer is displayed.
1222 static void draw_worm(struct worm *w)
1224 #ifdef HAVE_LCD_COLOR
1225 rb->lcd_set_foreground(COLOR_WORM);
1226 #endif
1227 /* draw the new head */
1228 int x = w->x[w->head];
1229 int y = w->y[w->head];
1230 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1231 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1234 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1236 /* clear the space behind the worm */
1237 x = w->x[w->tail] ;
1238 y = w->y[w->tail] ;
1239 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
1240 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
1242 rb->lcd_set_drawmode(DRMODE_SOLID);
1243 #ifdef HAVE_LCD_COLOR
1244 rb->lcd_set_foreground(COLOR_FG);
1245 #endif
1249 * Checks wether the coordinate is part of the worm. Returns
1250 * true if any part of the worm was hit - including the head.
1251 * @param x int The x coordinate
1252 * @param y int The y coordinate
1253 * @return int The index of the worm arrays that contain x, y.
1254 * Returns -1 if the coordinates are not part of the worm.
1256 static int specific_worm_collision(struct worm *w, int x, int y)
1258 int retVal = -1;
1260 /* get_worm_array_length is expensive -> buffer the value */
1261 int wormLength = get_worm_array_length(w);
1262 int i;
1264 /* test each entry that is part of the worm */
1265 for (i = 0; i < wormLength && retVal == -1; i++) {
1267 /* The iteration iterates the length of the worm.
1268 Here's the conversion to the true indices within the worm arrays. */
1269 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
1270 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
1271 bool samex = (w->x[linestart] == x) && (w->x[lineend] == x);
1272 bool samey = (w->y[linestart] == y) && (w->y[lineend] == y);
1273 if (samex || samey){
1274 int test, min, max, tmp;
1276 if (samey) {
1277 min = w->x[linestart];
1278 max = w->x[lineend];
1279 test = x;
1280 } else {
1281 min = w->y[linestart];
1282 max = w->y[lineend];
1283 test = y;
1286 tmp = min;
1287 min = MIN(min, max);
1288 max = MAX(tmp, max);
1290 if (min <= test && test <= max) {
1291 retVal = lineend;
1295 return retVal;
1299 * Increases the length of the specified worm by marking
1300 * that it may grow by len pixels. Note that the worm has
1301 * to move to make the growing happen.
1302 * @param worm *w The worm that is to be altered.
1303 * @param int len A positive value specifying the amount of
1304 * pixels the worm may grow.
1306 static void add_growing(struct worm *w, int len) {
1307 w->growing += len;
1311 * Determins the worm that is at the coordinates x, y. The parameter
1312 * w is a switch parameter that changes the functionality of worm_collision.
1313 * If w is specified and x,y hits the head of w NULL is returned.
1314 * This is a useful way to determine wether the head of w hits
1315 * any worm but including itself but excluding its own head.
1316 * (It hits always its own head ;))
1317 * If w is set to NULL worm_collision returns any worm including all heads
1318 * that is at position of x,y.
1319 * @param struct worm *w The worm of which the head should be excluded in
1320 * the test. w may be set to NULL.
1321 * @param int x The x coordinate that is checked
1322 * @param int y The y coordinate that is checkec
1323 * @return struct worm* The worm that has been hit by x,y. If no worm
1324 * was at the position NULL is returned.
1326 static struct worm* worm_collision(struct worm *w, int x, int y){
1327 struct worm *retVal = NULL;
1328 int i;
1329 for (i = 0; (i < worm_count) && (retVal == NULL); i++) {
1330 int collision_at = specific_worm_collision(&worms[i], x, y);
1331 if (collision_at != -1) {
1332 if (!(w == &worms[i] && collision_at == w->head)){
1333 retVal = &worms[i];
1337 return retVal;
1341 * Returns true if the head of the worm just has
1342 * crossed the field boundaries.
1343 * @return bool true if the worm just has wrapped.
1345 static bool field_collision(struct worm *w)
1347 bool retVal = false;
1348 if ((w->x[w->head] >= FIELD_RECT_WIDTH) ||
1349 (w->y[w->head] >= FIELD_RECT_HEIGHT) ||
1350 (w->x[w->head] < 0) ||
1351 (w->y[w->head] < 0))
1353 retVal = true;
1355 return retVal;
1360 * Returns true if the specified coordinates are within the
1361 * field specified by the FIELD_RECT_XXX constants.
1362 * @param int x The x coordinate of the point that is investigated
1363 * @param int y The y coordinate of the point that is investigated
1364 * @return bool Returns false if x,y specifies a point outside the
1365 * field of worms.
1367 static bool is_in_field_rect(int x, int y) {
1368 bool retVal = false;
1369 retVal = (x >= 0 && x < FIELD_RECT_WIDTH &&
1370 y >= 0 && y < FIELD_RECT_HEIGHT);
1371 return retVal;
1375 * Checks and returns wether the head of the w
1376 * is colliding with something currently.
1377 * @return int One of the values:
1378 * COLLISION_NONE
1379 * COLLISION_w
1380 * COLLISION_FOOD
1381 * COLLISION_ARGH
1382 * COLLISION_FIELD
1384 static int check_collision(struct worm *w)
1386 int retVal = COLLISION_NONE;
1388 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL)
1389 retVal = COLLISION_WORM;
1391 if (food_collision(w->x[w->head], w->y[w->head]) >= 0)
1392 retVal = COLLISION_FOOD;
1394 if (argh_collision(w->x[w->head], w->y[w->head]) >= 0)
1395 retVal = COLLISION_ARGH;
1397 if (field_collision(w))
1398 retVal = COLLISION_FIELD;
1400 return retVal;
1404 * Returns the index of the food that is closest to the point
1405 * specified by x, y. This index may be used in the foodx and
1406 * foody arrays.
1407 * @param int x The x coordinate of the point
1408 * @param int y The y coordinate of the point
1409 * @return int A value usable as index in foodx and foody.
1411 static int get_nearest_food(int x, int y){
1412 int nearestfood = 0;
1413 int olddistance = FIELD_RECT_WIDTH + FIELD_RECT_HEIGHT;
1414 int deltax = 0;
1415 int deltay = 0;
1416 int foodindex;
1417 for (foodindex = 0; foodindex < MAX_FOOD; foodindex++) {
1418 int distance;
1419 deltax = foodx[foodindex] - x;
1420 deltay = foody[foodindex] - y;
1421 deltax = deltax > 0 ? deltax : deltax * (-1);
1422 deltay = deltay > 0 ? deltay : deltay * (-1);
1423 distance = deltax + deltay;
1425 if (distance < olddistance) {
1426 olddistance = distance;
1427 nearestfood = foodindex;
1430 return nearestfood;
1434 * Returns wether the specified position is next to the worm
1435 * and in the direction the worm looks. Use this method to
1436 * test wether this position would be hit with the next move of
1437 * the worm unless the worm changes its direction.
1438 * @param struct worm *w - The worm to be investigated
1439 * @param int x - The x coordinate of the position to test.
1440 * @param int y - The y coordinate of the position to test.
1441 * @return Returns true if the worm will hit the position unless
1442 * it change its direction before the next move.
1444 static bool is_in_front_of_worm(struct worm *w, int x, int y) {
1445 bool infront = false;
1446 int deltax = x - w->x[w->head];
1447 int deltay = y - w->y[w->head];
1449 if (w->dirx == 0) {
1450 infront = (w->diry * deltay) > 0;
1451 } else {
1452 infront = (w->dirx * deltax) > 0;
1454 return infront;
1458 * Returns true if the worm will collide with the next move unless
1459 * it changes its direction.
1460 * @param struct worm *w - The worm to be investigated.
1461 * @return Returns true if the worm will collide with the next move
1462 * unless it changes its direction.
1464 static bool will_worm_collide(struct worm *w) {
1465 int x = w->x[w->head] + w->dirx;
1466 int y = w->y[w->head] + w->diry;
1467 bool retVal = !is_in_field_rect(x, y);
1468 if (!retVal) {
1469 retVal = (argh_collision(x, y) != -1);
1472 if (!retVal) {
1473 retVal = (worm_collision(w, x, y) != NULL);
1475 return retVal;
1479 * This function
1480 * may be used to be stored in worm.fetch_worm_direction for
1481 * worms that are not controlled by humans but by artificial stupidity.
1482 * A direction is searched that doesn't lead to collision but to the nearest
1483 * food - but not very intelligent. The direction is written to the specified
1484 * worm.
1485 * @param struct worm *w - The worm of which the direction
1486 * is altered.
1488 static void virtual_player(struct worm *w) {
1489 bool isright;
1490 int plana, planb, planc;
1491 /* find the next lunch */
1492 int nearestfood = get_nearest_food(w->x[w->head], w->y[w->head]);
1494 /* determine in which direction it is */
1496 /* in front of me? */
1497 bool infront = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1499 /* left right of me? */
1500 int olddir = get_worm_dir(w);
1501 set_worm_dir(w, (olddir + 1) % 4);
1502 isright = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1503 set_worm_dir(w, olddir);
1505 /* detect situation, set strategy */
1506 if (infront) {
1507 if (isright) {
1508 plana = olddir;
1509 planb = (olddir + 1) % 4;
1510 planc = (olddir + 3) % 4;
1511 } else {
1512 plana = olddir;
1513 planb = (olddir + 3) % 4;
1514 planc = (olddir + 1) % 4;
1516 } else {
1517 if (isright) {
1518 plana = (olddir + 1) % 4;
1519 planb = olddir;
1520 planc = (olddir + 3) % 4;
1521 } else {
1522 plana = (olddir + 3) % 4;
1523 planb = olddir;
1524 planc = (olddir + 1) % 4;
1528 /* test for collision */
1529 set_worm_dir(w, plana);
1530 if (will_worm_collide(w)){
1532 /* plan b */
1533 set_worm_dir(w, planb);
1535 /* test for collision */
1536 if (will_worm_collide(w)) {
1538 /* plan c */
1539 set_worm_dir(w, planc);
1545 * prints out the score board with all the status information
1546 * about the game.
1548 static void score_board(void)
1550 char buf[15];
1551 char* buf2 = NULL;
1552 int i;
1553 int y = 0;
1554 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1555 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, 0, LCD_WIDTH - FIELD_RECT_WIDTH - 2, LCD_HEIGHT);
1556 rb->lcd_set_drawmode(DRMODE_SOLID);
1557 for (i = 0; i < worm_count; i++) {
1558 int score = get_score(&worms[i]);
1560 /* high score */
1561 if (worms[i].fetch_worm_direction != virtual_player){
1562 if (highscore < score) {
1563 highscore = score;
1567 /* length */
1568 rb->snprintf(buf, sizeof (buf),"Len:%d", score);
1570 /* worm state */
1571 switch (check_collision(&worms[i])) {
1572 case COLLISION_NONE:
1573 if (worms[i].growing > 0)
1574 buf2 = "Growing";
1575 else {
1576 if (worms[i].alive)
1577 buf2 = "Hungry";
1578 else
1579 buf2 = "Wormed";
1581 break;
1583 case COLLISION_WORM:
1584 buf2 = "Wormed";
1585 break;
1587 case COLLISION_FOOD:
1588 buf2 = "Growing";
1589 break;
1591 case COLLISION_ARGH:
1592 buf2 = "Argh";
1593 break;
1595 case COLLISION_FIELD:
1596 buf2 = "Crashed";
1597 break;
1599 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y , buf);
1600 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y+8, buf2);
1602 if (!worms[i].alive){
1603 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1604 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, y,
1605 LCD_WIDTH - FIELD_RECT_WIDTH - 2, 17);
1606 rb->lcd_set_drawmode(DRMODE_SOLID);
1608 y += 19;
1610 rb->snprintf(buf , sizeof(buf), "Hs: %d", highscore);
1611 #ifndef DEBUG_WORMLET
1612 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, buf);
1613 #else
1614 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout);
1615 #endif
1619 * Checks for collisions of the worm and its environment and
1620 * takes appropriate actions like growing the worm or killing it.
1621 * @return bool Returns true if the worm is dead. Returns
1622 * false if the worm is healthy, up and creeping.
1624 static bool process_collisions(struct worm *w)
1626 int index = -1;
1628 w->alive &= !field_collision(w);
1630 if (w->alive) {
1632 /* check if food was eaten */
1633 index = food_collision(w->x[w->head], w->y[w->head]);
1634 if (index != -1){
1635 int i;
1637 clear_food(index);
1638 make_food(index);
1639 draw_food(index);
1641 for (i = 0; i < arghs_per_food; i++) {
1642 argh_count++;
1643 if (argh_count > MAX_ARGH)
1644 argh_count = MAX_ARGH;
1645 make_argh(argh_count - 1);
1646 draw_argh(argh_count - 1);
1649 add_growing(w, worm_food);
1651 draw_worm(w);
1654 /* check if argh was eaten */
1655 else {
1656 index = argh_collision(w->x[w->head], w->y[w->head]);
1657 if (index != -1) {
1658 w->alive = false;
1660 else {
1661 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) {
1662 w->alive = false;
1667 return !w->alive;
1671 * The main loop of the game.
1672 * @return bool Returns true if the game ended
1673 * with a dead worm. Returns false if the user
1674 * aborted the game manually.
1676 static int run(void)
1678 int button = 0;
1679 int wormDead = false;
1680 bool paused = false;
1682 /* ticks are counted to compensate speed variations */
1683 long cycle_start = 0, cycle_end = 0;
1684 #ifdef DEBUG_WORMLET
1685 int ticks_to_max_cycle_reset = 20;
1686 long max_cycle = 0;
1687 char buf[20];
1688 #endif
1690 /* initialize the board and so on */
1691 init_wormlet();
1693 cycle_start = *rb->current_tick;
1694 /* change the direction of the worm */
1695 while (!wormDead)
1697 int i;
1698 long cycle_duration=0;
1700 #ifdef HAS_BUTTON_HOLD
1701 if (rb->button_hold())
1702 paused = true;
1703 #endif
1705 switch (button) {
1706 case BTN_STARTPAUSE:
1707 paused = !paused;
1708 break;
1709 case BTN_STOPRESET:
1710 if (paused)
1711 return 1; /* restart game */
1712 else
1713 paused = true;
1714 break;
1715 #ifdef BTN_RC_QUIT
1716 case BTN_RC_QUIT:
1717 #endif
1718 case BTN_QUIT:
1719 return 2; /* back to menu */
1720 break;
1722 if (!paused)
1724 switch (button) {
1725 case BTN_DIR_UP:
1726 if (players == 1 && !use_remote) {
1727 player1_dir = NORTH;
1729 break;
1731 case BTN_DIR_DOWN:
1732 if (players == 1 && !use_remote) {
1733 player1_dir = SOUTH;
1735 break;
1737 case BTN_DIR_LEFT:
1738 if (players != 1 || use_remote) {
1739 player1_dir = (player1_dir + 3) % 4;
1740 } else {
1741 player1_dir = WEST;
1743 break;
1745 case BTN_DIR_RIGHT:
1746 if (players != 1 || use_remote) {
1747 player1_dir = (player1_dir + 1) % 4;
1748 } else {
1749 player1_dir = EAST;
1751 break;
1753 #ifdef MULTIPLAYER
1754 case BTN_PLAYER2_DIR1:
1755 player2_dir = (player2_dir + 3) % 4;
1756 break;
1758 case BTN_PLAYER2_DIR2:
1759 player2_dir = (player2_dir + 1) % 4;
1760 break;
1761 #endif
1763 #ifdef REMOTE
1764 case BTN_RC_UP:
1765 player3_dir = (player3_dir + 1) % 4;
1766 break;
1768 case BTN_RC_DOWN:
1769 player3_dir = (player3_dir + 3) % 4;
1770 break;
1771 #endif
1775 for (i = 0; i < worm_count; i++) {
1776 worms[i].fetch_worm_direction(&worms[i]);
1779 wormDead = true;
1780 for (i = 0; i < worm_count; i++){
1781 struct worm *w = &worms[i];
1782 move_worm(w);
1783 wormDead &= process_collisions(w);
1784 draw_worm(w);
1786 score_board();
1787 rb->lcd_update();
1788 if (button == BTN_STOPRESET) {
1789 wormDead = true;
1792 /* here the wormlet game cycle ends
1793 thus the current tick is stored
1794 as end time */
1795 cycle_end = *rb->current_tick;
1797 /* The duration of the game cycle */
1798 cycle_duration = cycle_end - cycle_start;
1799 cycle_duration = MAX(0, cycle_duration);
1800 cycle_duration = MIN(speed -1, cycle_duration);
1803 #ifdef DEBUG_WORMLET
1804 ticks_to_max_cycle_reset--;
1805 if (ticks_to_max_cycle_reset <= 0) {
1806 max_cycle = 0;
1809 if (max_cycle < cycle_duration) {
1810 max_cycle = cycle_duration;
1811 ticks_to_max_cycle_reset = 20;
1813 rb->snprintf(buf, sizeof buf, "ticks %d", max_cycle);
1814 set_debug_out(buf);
1815 #endif
1817 /* adjust the number of ticks to wait for a button.
1818 This ensures that a complete game cycle including
1819 user input runs in constant time */
1820 button = rb->button_get_w_tmo(speed - cycle_duration);
1821 cycle_start = *rb->current_tick;
1824 rb->splash(HZ*2, "Game Over!");
1826 return 2; /* back to menu */
1829 #ifdef DEBUG_WORMLET
1832 * Just a test routine that checks that worm_food_collision works
1833 * in some typical situations.
1835 static void test_worm_food_collision(void) {
1836 int collision_count = 0;
1837 int i;
1838 rb->lcd_clear_display();
1839 init_worm(&worms[0], 10, 10);
1840 add_growing(&worms[0], 10);
1841 set_worm_dir(&worms[0], EAST);
1842 for (i = 0; i < 10; i++) {
1843 move_worm(&worms[0]);
1844 draw_worm(&worms[0]);
1847 set_worm_dir(&worms[0], SOUTH);
1848 for (i = 0; i < 10; i++) {
1849 move_worm(&worms[0]);
1850 draw_worm(&worms[0]);
1853 foodx[0] = 15;
1854 foody[0] = 12;
1855 for (foody[0] = 20; foody[0] > 0; foody[0] --) {
1856 char buf[20];
1857 bool collision;
1858 draw_worm(&worms[0]);
1859 draw_food(0);
1860 collision = worm_food_collision(&worms[0], 0);
1861 if (collision) {
1862 collision_count++;
1864 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1865 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1866 rb->lcd_update();
1868 if (collision_count != food_size) {
1869 rb->button_get(true);
1873 foody[0] = 15;
1874 for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) {
1875 char buf[20];
1876 bool collision;
1877 draw_worm(&worms[0]);
1878 draw_food(0);
1879 collision = worm_food_collision(&worms[0], 0);
1880 if (collision) {
1881 collision_count ++;
1883 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1884 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1885 rb->lcd_update();
1887 if (collision_count != food_size * 2) {
1888 rb->button_get(true);
1893 static bool expensive_worm_in_rect(struct worm *w, int rx, int ry, int rw, int rh){
1894 int x, y;
1895 bool retVal = false;
1896 for (x = rx; x < rx + rw; x++){
1897 for (y = ry; y < ry + rh; y++) {
1898 if (specific_worm_collision(w, x, y) != -1) {
1899 retVal = true;
1903 return retVal;
1906 static void test_worm_argh_collision(void) {
1907 int i;
1908 int dir;
1909 int collision_count = 0;
1910 rb->lcd_clear_display();
1911 init_worm(&worms[0], 10, 10);
1912 add_growing(&worms[0], 40);
1913 for (dir = 0; dir < 4; dir++) {
1914 set_worm_dir(&worms[0], (EAST + dir) % 4);
1915 for (i = 0; i < 10; i++) {
1916 move_worm(&worms[0]);
1917 draw_worm(&worms[0]);
1921 arghx[0] = 12;
1922 for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){
1923 char buf[20];
1924 bool collision;
1925 draw_argh(0);
1926 collision = worm_argh_collision(&worms[0], 0);
1927 if (collision) {
1928 collision_count ++;
1930 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1931 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1932 rb->lcd_update();
1934 if (collision_count != argh_size * 2) {
1935 rb->button_get(true);
1938 arghy[0] = 12;
1939 for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){
1940 char buf[20];
1941 bool collision;
1942 draw_argh(0);
1943 collision = worm_argh_collision(&worms[0], 0);
1944 if (collision) {
1945 collision_count ++;
1947 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1948 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1949 rb->lcd_update();
1951 if (collision_count != argh_size * 4) {
1952 rb->button_get(true);
1956 static int testline_in_rect(void) {
1957 int testfailed = -1;
1959 int rx = 10;
1960 int ry = 15;
1961 int rw = 20;
1962 int rh = 25;
1964 /* Test 1 */
1965 int x1 = 12;
1966 int y1 = 8;
1967 int x2 = 12;
1968 int y2 = 42;
1970 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1971 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1972 rb->lcd_drawrect(rx, ry, rw, rh);
1973 rb->lcd_drawline(x1, y1, x2, y2);
1974 rb->lcd_update();
1975 rb->lcd_putsxy(0, 0, "failed 1");
1976 rb->button_get(true);
1977 testfailed = 1;
1980 /* test 2 */
1981 y2 = 20;
1982 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1983 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1984 rb->lcd_drawrect(rx, ry, rw, rh);
1985 rb->lcd_drawline(x1, y1, x2, y2);
1986 rb->lcd_putsxy(0, 0, "failed 2");
1987 rb->lcd_update();
1988 rb->button_get(true);
1989 testfailed = 2;
1992 /* test 3 */
1993 y1 = 30;
1994 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1995 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
1996 rb->lcd_drawrect(rx, ry, rw, rh);
1997 rb->lcd_drawline(x1, y1, x2, y2);
1998 rb->lcd_putsxy(0, 0, "failed 3");
1999 rb->lcd_update();
2000 rb->button_get(true);
2001 testfailed = 3;
2004 /* test 4 */
2005 y2 = 45;
2006 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2007 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2008 rb->lcd_drawrect(rx, ry, rw, rh);
2009 rb->lcd_drawline(x1, y1, x2, y2);
2010 rb->lcd_putsxy(0, 0, "failed 4");
2011 rb->lcd_update();
2012 rb->button_get(true);
2013 testfailed = 4;
2016 /* test 5 */
2017 y1 = 50;
2018 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2019 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2020 rb->lcd_drawrect(rx, ry, rw, rh);
2021 rb->lcd_drawline(x1, y1, x2, y2);
2022 rb->lcd_putsxy(0, 0, "failed 5");
2023 rb->lcd_update();
2024 rb->button_get(true);
2025 testfailed = 5;
2028 /* test 6 */
2029 y1 = 5;
2030 y2 = 7;
2031 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2032 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2033 rb->lcd_drawrect(rx, ry, rw, rh);
2034 rb->lcd_drawline(x1, y1, x2, y2);
2035 rb->lcd_putsxy(0, 0, "failed 6");
2036 rb->lcd_update();
2037 rb->button_get(true);
2038 testfailed = 6;
2041 /* test 7 */
2042 x1 = 8;
2043 y1 = 20;
2044 x2 = 35;
2045 y2 = 20;
2046 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2047 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2048 rb->lcd_drawrect(rx, ry, rw, rh);
2049 rb->lcd_drawline(x1, y1, x2, y2);
2050 rb->lcd_putsxy(0, 0, "failed 7");
2051 rb->lcd_update();
2052 rb->button_get(true);
2053 testfailed = 7;
2056 /* test 8 */
2057 x2 = 12;
2058 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2059 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2060 rb->lcd_drawrect(rx, ry, rw, rh);
2061 rb->lcd_drawline(x1, y1, x2, y2);
2062 rb->lcd_putsxy(0, 0, "failed 8");
2063 rb->lcd_update();
2064 rb->button_get(true);
2065 testfailed = 8;
2068 /* test 9 */
2069 x1 = 25;
2070 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2071 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2072 rb->lcd_drawrect(rx, ry, rw, rh);
2073 rb->lcd_drawline(x1, y1, x2, y2);
2074 rb->lcd_putsxy(0, 0, "failed 9");
2075 rb->lcd_update();
2076 rb->button_get(true);
2077 testfailed = 9;
2080 /* test 10 */
2081 x2 = 37;
2082 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2083 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2084 rb->lcd_drawrect(rx, ry, rw, rh);
2085 rb->lcd_drawline(x1, y1, x2, y2);
2086 rb->lcd_putsxy(0, 0, "failed 10");
2087 rb->lcd_update();
2088 rb->button_get(true);
2089 testfailed = 10;
2092 /* test 11 */
2093 x1 = 42;
2094 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2095 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2096 rb->lcd_drawrect(rx, ry, rw, rh);
2097 rb->lcd_drawline(x1, y1, x2, y2);
2098 rb->lcd_putsxy(0, 0, "failed 11");
2099 rb->lcd_update();
2100 rb->button_get(true);
2101 testfailed = 11;
2104 /* test 12 */
2105 x1 = 5;
2106 x2 = 7;
2107 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
2108 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
2109 rb->lcd_drawrect(rx, ry, rw, rh);
2110 rb->lcd_drawline(x1, y1, x2, y2);
2111 rb->lcd_putsxy(0, 0, "failed 12");
2112 rb->lcd_update();
2113 rb->button_get(true);
2114 testfailed = 12;
2117 /* test 13 */
2118 rx = 9;
2119 ry = 15;
2120 rw = food_size;
2121 rh = food_size;
2123 x1 = 10;
2124 y1 = 10;
2125 x2 = 10;
2126 y2 = 20;
2127 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2128 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2129 rb->lcd_drawrect(rx, ry, rw, rh);
2130 rb->lcd_drawline(x1, y1, x2, y2);
2131 rb->lcd_putsxy(0, 0, "failed 13");
2132 rb->lcd_update();
2133 rb->button_get(true);
2134 testfailed = 13;
2137 /* test 14 */
2138 rx = 9;
2139 ry = 15;
2140 rw = 4;
2141 rh = 4;
2143 x1 = 10;
2144 y1 = 10;
2145 x2 = 10;
2146 y2 = 19;
2147 if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
2148 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
2149 rb->lcd_drawline(x1, y1, x2, y2);
2150 rb->lcd_invertrect(rx, ry, rw, rh);
2151 rb->lcd_putsxy(0, 0, "failed 14");
2152 rb->lcd_update();
2153 rb->button_get(true);
2154 testfailed = 14;
2157 rb->lcd_clear_display();
2159 return testfailed;
2163 * Just a test routine to test wether specific_worm_collision might work properly
2165 static int test_specific_worm_collision(void) {
2166 int collisions = 0;
2167 int dir;
2168 int x = 0;
2169 int y = 0;
2170 char buf[20];
2171 rb->lcd_clear_display();
2172 init_worm(&worms[0], 10, 20);
2173 add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH);
2175 for (dir = EAST; dir < EAST + 4; dir++) {
2176 int i;
2177 set_worm_dir(&worms[0], dir % 4);
2178 for (i = 0; i < 5; i++) {
2179 if (!(dir % 4 == NORTH && i == 9)) {
2180 move_worm(&worms[0]);
2181 draw_worm(&worms[0]);
2186 for (y = 15; y < 30; y ++){
2187 for (x = 5; x < 20; x++) {
2188 if (specific_worm_collision(&worms[0], x, y) != -1) {
2189 collisions ++;
2191 rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
2192 rb->snprintf(buf, sizeof buf, "collisions %d", collisions);
2193 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2194 rb->lcd_update();
2197 if (collisions != 21) {
2198 rb->button_get(true);
2200 return collisions;
2203 static void test_make_argh(void){
2204 int dir;
2205 int seed = 0;
2206 int hit = 0;
2207 int failures = 0;
2208 int last_failures = 0;
2209 int i, worm_idx;
2210 rb->lcd_clear_display();
2211 worm_count = 3;
2213 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2214 init_worm(&worms[worm_idx], 10 + worm_idx * 20, 20);
2215 add_growing(&worms[worm_idx], 40 - INITIAL_WORM_LENGTH);
2218 for (dir = EAST; dir < EAST + 4; dir++) {
2219 for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
2220 set_worm_dir(&worms[worm_idx], dir % 4);
2221 for (i = 0; i < 10; i++) {
2222 if (!(dir % 4 == NORTH && i == 9)) {
2223 move_worm(&worms[worm_idx]);
2224 draw_worm(&worms[worm_idx]);
2230 rb->lcd_update();
2232 for (seed = 0; hit < 20; seed += 2) {
2233 char buf[20];
2234 int x, y;
2235 rb->srand(seed);
2236 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
2237 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
2239 for (worm_idx = 0; worm_idx < worm_count; worm_idx++){
2240 if (expensive_worm_in_rect(&worms[worm_idx], x, y, argh_size, argh_size)) {
2241 int tries = 0;
2242 rb->srand(seed);
2244 tries = make_argh(0);
2245 if ((x == arghx[0] && y == arghy[0]) || tries < 2) {
2246 failures ++;
2249 rb->snprintf(buf, sizeof buf, "(%d;%d) fail%d try%d", x, y, failures, tries);
2250 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2251 rb->lcd_update();
2252 rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y, argh_size, argh_size);
2253 rb->lcd_update();
2254 draw_argh(0);
2255 rb->lcd_update();
2256 rb->lcd_invertrect(x + FIELD_RECT_X, y + FIELD_RECT_Y, argh_size, argh_size);
2257 rb->lcd_clearrect(arghx[0] + FIELD_RECT_X, arghy[0] + FIELD_RECT_Y, argh_size, argh_size);
2259 if (failures > last_failures) {
2260 rb->button_get(true);
2262 last_failures = failures;
2263 hit ++;
2269 static void test_worm_argh_collision_in_moves(void) {
2270 int hit_count = 0;
2271 int i;
2272 rb->lcd_clear_display();
2273 init_worm(&worms[0], 10, 20);
2275 arghx[0] = 20;
2276 arghy[0] = 18;
2277 draw_argh(0);
2279 set_worm_dir(&worms[0], EAST);
2280 for (i = 0; i < 20; i++) {
2281 char buf[20];
2282 move_worm(&worms[0]);
2283 draw_worm(&worms[0]);
2284 if (worm_argh_collision_in_moves(&worms[0], 0, 5)){
2285 hit_count ++;
2287 rb->snprintf(buf, sizeof buf, "in 5 moves hits: %d", hit_count);
2288 rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf);
2289 rb->lcd_update();
2291 if (hit_count != argh_size + 5) {
2292 rb->button_get(true);
2295 #endif /* DEBUG_WORMLET */
2297 extern bool use_old_rect;
2300 * These are additional functions required to set various wormlet settings
2301 * and to enable saving of settings and high score
2305 Sets the total number of worms, both human and machine
2307 bool set_worm_num_worms(void)
2309 bool ret;
2310 static const struct opt_items num_worms_option[3] = {
2311 { "1", -1 },
2312 { "2", -1 },
2313 { "3", -1 },
2316 ret = rb->set_option("Number Of Worms", &worm_count,INT, num_worms_option, 3, NULL);
2317 if (worm_count < players) {
2318 worm_count=players;
2320 return ret;
2324 Sets the number of human players
2326 bool set_worm_num_players(void)
2328 bool ret;
2329 static const struct opt_items num_players_option[4] = {
2330 { "0", -1 },
2331 { "1", -1 },
2332 { "2", -1 },
2333 { "3", -1 }
2336 ret = rb->set_option("Number of Players", &players, INT,num_players_option , 4, NULL);
2337 if (players > worm_count) {
2338 worm_count = players;
2340 if (players > 2) {
2341 use_remote = true;
2343 return ret;
2347 Sets the size of each argh block created
2349 bool set_worm_argh_size(void)
2351 static const struct opt_items argh_size_option[11] = {
2352 { "0", -1 },
2353 { "1", -1 },
2354 { "2", -1 },
2355 { "3", -1 },
2356 { "4", -1 },
2357 { "5", -1 },
2358 { "6", -1 },
2359 { "7", -1 },
2360 { "8", -1 },
2361 { "9", -1 },
2362 { "10", -1 }
2365 return rb->set_option("Argh Size", &argh_size,INT,argh_size_option , 11, NULL);
2369 Sets the amount a worm grows per food
2371 bool set_worm_food(void)
2373 static const struct opt_items worm_food_option[11] = {
2374 { "0", -1 },
2375 { "1", -1 },
2376 { "2", -1 },
2377 { "3", -1 },
2378 { "4", -1 },
2379 { "5", -1 },
2380 { "6", -1 },
2381 { "7", -1 },
2382 { "8", -1 },
2383 { "9", -1 },
2384 { "10", -1 }
2386 return rb->set_option("Worm Growth Per Food", &worm_food,INT,worm_food_option , 11, NULL);
2390 Sets the number of arghs created per food
2392 bool set_argh_per_food(void)
2394 static const struct opt_items argh_food_option[5] = {
2395 { "0", -1 },
2396 { "1", -1 },
2397 { "2", -1 },
2398 { "3", -1 },
2399 { "4", -1 },
2401 return rb->set_option("Arghs Per Food", &arghs_per_food,INT,argh_food_option , 5, NULL);
2405 Sets the number of ticks per move
2407 bool set_worm_speed(void)
2409 bool ret;
2410 static const struct opt_items speed_option[19] = {
2411 { "0", -1 },
2412 { "1", -1 },
2413 { "2", -1 },
2414 { "3", -1 },
2415 { "4", -1 },
2416 { "5", -1 },
2417 { "6", -1 },
2418 { "7", -1 },
2419 { "8", -1 },
2420 { "9", -1 },
2421 { "10", -1 },
2422 { "11", -1 },
2423 { "12", -1 },
2424 { "13", -1 },
2425 { "14", -1 },
2426 { "15", -1 },
2427 { "16", -1 },
2428 { "17", -1 },
2429 { "18", -1 },
2433 speed = 20 - speed;
2434 ret = rb->set_option("Worm Speed", &speed,INT,speed_option , 19, NULL);
2435 speed = 20 - speed;
2436 return ret;
2440 Sets the control style, which depends on the number of players,
2441 remote control existing, etc
2442 bool set_bool_options(char* string, bool* variable,
2443 char* yes_str, char* no_str, void (*function)(bool))
2445 bool set_worm_control_style(void)
2447 static const struct opt_items key1_option[2] = {
2448 { "Remote Control", -1 },
2449 { "No Rem. Control", -1 },
2452 static const struct opt_items key2_option[2] = {
2453 { "2 Key Control", -1 },
2454 { "4 Key COntrol", -1 },
2457 static const struct opt_items key3_option[1] = {
2458 { "Out of Control", -1 },
2461 if (players > 1) {
2462 rb->set_option("Control Style",&use_remote,INT, key1_option, 2, NULL);
2463 } else {
2464 if (players > 0) {
2465 rb->set_option("Control Style",&use_remote,INT, key2_option, 2, NULL);
2467 else {
2468 rb->set_option("Control Style",&use_remote,INT, key3_option, 1, NULL);
2471 return false;
2474 void default_settings(void)
2476 arghs_per_food = ARGHS_PER_FOOD;
2477 argh_size = ARGH_SIZE;
2478 food_size = FOOD_SIZE;
2479 speed = SPEED;
2480 worm_food = WORM_PER_FOOD;
2481 players = 1;
2482 worm_count = MAX_WORMS;
2483 use_remote = false;
2484 return;
2488 Launches the wormlet game
2490 bool launch_wormlet(void)
2492 int game_result = 1;
2494 rb->lcd_clear_display();
2496 /* Turn off backlight timeout */
2497 backlight_force_on(); /* backlight control in lib/helper.c */
2499 /* start the game */
2500 while (game_result == 1)
2501 game_result = run();
2503 switch (game_result)
2505 case 2:
2506 /* Turn on backlight timeout (revert to settings) */
2507 backlight_use_settings(); /* backlight control in lib/helper.c */
2508 return false;
2509 break;
2511 return false;
2514 /* End of settings/changes etc */
2517 * Main entry point
2519 enum plugin_status plugin_start(const void* parameter)
2521 int result;
2522 int menu_quit = 0;
2523 int new_setting;
2525 (void)(parameter);
2527 default_settings();
2528 if (configfile_load(SETTINGS_FILENAME, config,
2529 sizeof(config)/sizeof(*config),
2530 SETTINGS_MIN_VERSION ) < 0)
2532 /* If the loading failed, save a new config file (as the disk is
2533 already spinning) */
2534 configfile_save(SETTINGS_FILENAME, config,
2535 sizeof(config)/sizeof(*config),
2536 SETTINGS_VERSION);
2539 #ifdef HAVE_LCD_COLOR
2540 rb->lcd_set_foreground(COLOR_FG);
2541 rb->lcd_set_background(COLOR_BG);
2542 #endif
2544 #if LCD_DEPTH > 1
2545 rb->lcd_set_backdrop(NULL);
2546 #endif
2548 #ifdef DEBUG_WORMLET
2549 testline_in_rect();
2550 test_worm_argh_collision_in_moves();
2551 test_make_argh();
2552 test_worm_food_collision();
2553 test_worm_argh_collision();
2554 test_specific_worm_collision();
2555 #endif
2557 /* Setup screen */
2559 static const struct opt_items noyes[2] = {
2560 { "No", -1 },
2561 { "Yes", -1 },
2564 static const struct opt_items num_worms_option[3] = {
2565 { "1", -1 },
2566 { "2", -1 },
2567 { "3", -1 }
2570 #ifdef MULTIPLAYER
2571 static const struct opt_items num_players_option[4] = {
2572 #else
2573 static const struct opt_items num_players_option[2] = {
2574 #endif
2575 { "0", -1 },
2576 { "1", -1 }
2577 #ifdef MULTIPLAYER
2578 ,{ "2", -1 },
2579 { "3", -1 }
2580 #endif
2583 static const struct opt_items argh_size_option[9] = {
2584 { "2", -1 },
2585 { "3", -1 },
2586 { "4", -1 },
2587 { "5", -1 },
2588 { "6", -1 },
2589 { "7", -1 },
2590 { "8", -1 },
2591 { "9", -1 },
2592 { "10", -1 }
2595 static const struct opt_items food_size_option[9] = {
2596 { "2", -1 },
2597 { "3", -1 },
2598 { "4", -1 },
2599 { "5", -1 },
2600 { "6", -1 },
2601 { "7", -1 },
2602 { "8", -1 },
2603 { "9", -1 },
2604 { "10", -1 }
2607 static const struct opt_items worm_food_option[16] = {
2608 { "0", -1 },
2609 { "1", -1 },
2610 { "2", -1 },
2611 { "3", -1 },
2612 { "4", -1 },
2613 { "5", -1 },
2614 { "6", -1 },
2615 { "7", -1 },
2616 { "8", -1 },
2617 { "9", -1 },
2618 { "10", -1 },
2619 { "11", -1 },
2620 { "12", -1 },
2621 { "13", -1 },
2622 { "14", -1 },
2623 { "15", -1 }
2626 static const struct opt_items argh_food_option[9] = {
2627 { "0", -1 },
2628 { "1", -1 },
2629 { "2", -1 },
2630 { "3", -1 },
2631 { "4", -1 },
2632 { "5", -1 },
2633 { "6", -1 },
2634 { "7", -1 },
2635 { "8", -1 }
2638 static const struct opt_items speed_option[21] = {
2639 { "0", -1 },
2640 { "1", -1 },
2641 { "2", -1 },
2642 { "3", -1 },
2643 { "4", -1 },
2644 { "5", -1 },
2645 { "6", -1 },
2646 { "7", -1 },
2647 { "8", -1 },
2648 { "9", -1 },
2649 { "10", -1 },
2650 { "11", -1 },
2651 { "12", -1 },
2652 { "13", -1 },
2653 { "14", -1 },
2654 { "15", -1 },
2655 { "16", -1 },
2656 { "17", -1 },
2657 { "18", -1 },
2658 { "19", -1 },
2659 { "20", -1 }
2662 static const struct opt_items remoteonly_option[1] = {
2663 { "Remote Control", -1 }
2666 static const struct opt_items key24_option[2] = {
2667 { "4 Key Control", -1 },
2668 { "2 Key Control", -1 }
2671 #ifdef REMOTE
2672 static const struct opt_items remote_option[2] = {
2673 { "Remote Control", -1 },
2674 { "No Rem. Control", -1 }
2676 #else
2677 static const struct opt_items key2_option[1] = {
2678 { "2 Key Control", -1 }
2680 #endif
2682 static const struct opt_items nokey_option[1] = {
2683 { "Out of Control", -1 }
2686 MENUITEM_STRINGLIST(menu, "Wormlet Menu", NULL, "Play Wormlet!",
2687 "Number of Worms", "Number of Players", "Control Style",
2688 "Worm Growth Per Food","Worm Speed","Arghs Per Food",
2689 "Argh Size","Food Size","Revert to Default Settings",
2690 "Playback Control", "Quit");
2692 rb->button_clear_queue();
2694 while (!menu_quit) {
2695 switch(rb->do_menu(&menu, &result, NULL, false))
2697 case 0:
2698 rb->lcd_setfont(FONT_SYSFIXED);
2699 launch_wormlet();
2700 break;
2701 case 1:
2702 new_setting = worm_count - 1;
2703 rb->set_option("Number of Worms", &new_setting, INT, num_worms_option, 3, NULL);
2704 if (new_setting != worm_count)
2705 worm_count = new_setting + 1;
2706 if (worm_count < players) {
2707 worm_count = players;
2709 break;
2710 case 2:
2711 new_setting = players;
2712 #ifdef MULTIPLAYER
2713 rb->set_option("Number of Players", &new_setting, INT, num_players_option , 4, NULL);
2714 #else
2715 rb->set_option("Number of Players", &new_setting, INT, num_players_option , 2, NULL);
2716 #endif
2717 if (new_setting != players)
2718 players = new_setting;
2719 if (players > worm_count) {
2720 worm_count = players;
2722 if (players > 2) {
2723 use_remote = true;
2725 break;
2726 case 3:
2727 new_setting = use_remote;
2728 switch(players) {
2729 case 0:
2730 rb->set_option("Control Style",&new_setting,INT, nokey_option, 1, NULL);
2731 break;
2732 case 1:
2733 rb->set_option("Control Style",&new_setting,INT, key24_option, 2, NULL);
2734 break;
2735 case 2:
2736 #ifdef REMOTE
2737 rb->set_option("Control Style",&new_setting,INT, remote_option, 2, NULL);
2738 #else
2739 rb->set_option("Control Style",&new_setting,INT, key2_option, 1, NULL);
2740 #endif
2741 break;
2742 case 3:
2743 rb->set_option("Control Style",&new_setting,INT, remoteonly_option, 1, NULL);
2744 break;
2746 if (new_setting != use_remote)
2747 use_remote = new_setting;
2748 break;
2749 case 4:
2750 new_setting = worm_food;
2751 rb->set_option("Worm Growth Per Food", &new_setting,INT,worm_food_option , 16, NULL);
2752 if (new_setting != worm_food)
2753 worm_food = new_setting;
2754 break;
2755 case 5:
2756 new_setting = speed;
2757 new_setting = 20 - new_setting;
2758 rb->set_option("Worm Speed", &new_setting,INT,speed_option , 21, NULL);
2759 new_setting = 20 - new_setting;
2760 if (new_setting != speed)
2761 speed = new_setting;
2762 break;
2763 case 6:
2764 new_setting = arghs_per_food;
2765 rb->set_option("Arghs Per Food", &new_setting,INT,argh_food_option , 9, NULL);
2766 if (new_setting != arghs_per_food)
2767 arghs_per_food = new_setting;
2768 break;
2769 case 7:
2770 new_setting = argh_size-2;
2771 rb->set_option("Argh Size", &new_setting,INT,argh_size_option , 9, NULL);
2772 if (new_setting != argh_size)
2773 argh_size = new_setting+2;
2774 break;
2775 case 8:
2776 new_setting = food_size-2;
2777 rb->set_option("Food Size", &new_setting,INT,food_size_option, 9, NULL);
2778 if (new_setting != food_size)
2779 food_size = new_setting+2;
2780 break;
2781 case 9:
2782 new_setting = 0;
2783 rb->set_option("Reset Settings?", &new_setting,INT, noyes , 2, NULL);
2784 if (new_setting == 1)
2785 default_settings();
2786 break;
2787 case 10:
2788 playback_control(NULL);
2789 break;
2790 default:
2791 menu_quit=1;
2792 break;
2796 configfile_save(SETTINGS_FILENAME, config,
2797 sizeof(config)/sizeof(*config),
2798 SETTINGS_VERSION);
2800 return PLUGIN_OK;