blob: 54690735883a91e5caa3e9a750b77817c83485d9 [file] [log] [blame]
<
Eric Linenbergb0a39842002-08-26 03:32:39 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Philipp Pertermann
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Eric Linenbergb0a39842002-08-26 03:32:39 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Björn Stenbergba371fb2003-06-29 16:33:04 +000021#include "plugin.h"
Zakk Roberts02e97d52006-05-22 06:56:39 +000022#include "configfile.h"
Peter D'Hoyecb53e3c2007-08-15 12:42:09 +000023#include "helper.h"
Eric Linenbergb0a39842002-08-26 03:32:39 +000024
Jens Arnolda36b1d42006-01-15 18:20:18 +000025PLUGIN_HEADER
26
Björn Stenberg50053d12002-09-09 23:30:16 +000027/* size of the field the worm lives in */
28#define FIELD_RECT_X 1
29#define FIELD_RECT_Y 1
Daniel Stenberg17b3bea2002-09-27 07:13:02 +000030#define FIELD_RECT_WIDTH (LCD_WIDTH - 45)
Björn Stenberg50053d12002-09-09 23:30:16 +000031#define FIELD_RECT_HEIGHT (LCD_HEIGHT - 2)
Eric Linenbergb0a39842002-08-26 03:32:39 +000032
Björn Stenberg50053d12002-09-09 23:30:16 +000033/* when the game starts */
Zakk Robertsc091a772006-03-13 03:13:05 +000034#define INITIAL_WORM_LENGTH 10
Björn Stenberg50053d12002-09-09 23:30:16 +000035
36/* num of pixel the worm grows per eaten food */
Zakk Robertsc091a772006-03-13 03:13:05 +000037#define WORM_PER_FOOD 7
Björn Stenberg50053d12002-09-09 23:30:16 +000038
39/* num of worms creeping in the FIELD */
Zakk Robertsc091a772006-03-13 03:13:05 +000040#define MAX_WORMS 3
Björn Stenberg50053d12002-09-09 23:30:16 +000041
Zakk Robertsc091a772006-03-13 03:13:05 +000042/* minimal distance between a worm and an argh
Björn Stenberg50053d12002-09-09 23:30:16 +000043 when a new argh is made */
44#define MIN_ARGH_DIST 5
45
Zakk Robertsc091a772006-03-13 03:13:05 +000046#if (CONFIG_KEYPAD == RECORDER_PAD)
47#define BTN_DIR_UP BUTTON_UP
48#define BTN_DIR_DOWN BUTTON_DOWN
49#define BTN_DIR_LEFT BUTTON_LEFT
50#define BTN_DIR_RIGHT BUTTON_RIGHT
51#define BTN_PLAYER2_DIR1 BUTTON_F2
52#define BTN_PLAYER2_DIR2 BUTTON_F3
Zakk Robertsc091a772006-03-13 03:13:05 +000053#define BTN_STARTPAUSE BUTTON_PLAY
54#define BTN_QUIT BUTTON_OFF
55#define BTN_STOPRESET BUTTON_ON
56#define BTN_TOGGLE_KEYS BUTTON_F1
57
Jens Arnold0e6dd7e2006-11-27 02:16:32 +000058#if BUTTON_REMOTE != 0
59#define BTN_RC_UP BUTTON_RC_VOL_UP
60#define BTN_RC_DOWN BUTTON_RC_VOL_DOWN
Zakk Robertsc091a772006-03-13 03:13:05 +000061#define REMOTE
62#define MULTIPLAYER
Jens Arnold0e6dd7e2006-11-27 02:16:32 +000063#endif
Zakk Robertsc091a772006-03-13 03:13:05 +000064
65#define PLAYERS_TEXT "UP/DN"
66#define WORMS_TEXT "L/R"
67#define KEY_CONTROL_TEXT "F1"
68
Dave Chapmand64e6262007-01-14 13:48:09 +000069#elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
70#define BTN_DIR_UP BUTTON_UP
71#define BTN_DIR_DOWN BUTTON_DOWN
72#define BTN_DIR_LEFT BUTTON_LEFT
73#define BTN_DIR_RIGHT BUTTON_RIGHT
74#define BTN_PLAYER2_DIR1 BUTTON_F2
75#define BTN_PLAYER2_DIR2 BUTTON_F3
76#define BTN_STARTPAUSE BUTTON_SELECT
77#define BTN_QUIT BUTTON_OFF
78#define BTN_STOPRESET BUTTON_ON
79#define BTN_TOGGLE_KEYS BUTTON_F1
80
81#define PLAYERS_TEXT "UP/DN"
82#define WORMS_TEXT "L/R"
83#define KEY_CONTROL_TEXT "F1"
84
Zakk Robertsc091a772006-03-13 03:13:05 +000085#elif (CONFIG_KEYPAD == ONDIO_PAD)
86#define BTN_DIR_UP BUTTON_UP
87#define BTN_DIR_DOWN BUTTON_DOWN
88#define BTN_DIR_LEFT BUTTON_LEFT
89#define BTN_DIR_RIGHT BUTTON_RIGHT
Zakk Roberts0b8766f2006-03-13 03:31:02 +000090#define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL)
91#define BTN_QUIT (BUTTON_OFF|BUTTON_REL)
92#define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU)
Zakk Robertsc091a772006-03-13 03:13:05 +000093
94#define PLAYERS_TEXT "UP/DN"
95#define WORMS_TEXT "L/R"
96
Jens Arnoldb7013222007-07-27 09:57:27 +000097#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
98 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
Zakk Robertsc091a772006-03-13 03:13:05 +000099
100#define BTN_DIR_UP BUTTON_MENU
101#define BTN_DIR_DOWN BUTTON_PLAY
102#define BTN_DIR_LEFT BUTTON_LEFT
103#define BTN_DIR_RIGHT BUTTON_RIGHT
104#define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
105#define BTN_QUIT (BUTTON_SELECT|BUTTON_MENU)
106#define BTN_STOPRESET (BUTTON_SELECT|BUTTON_PLAY)
107
108#define PLAYERS_TEXT "Menu/Play"
109#define WORMS_TEXT "Left/Right"
110
Zakk Roberts0b8766f2006-03-13 03:31:02 +0000111#elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
Zakk Robertsc091a772006-03-13 03:13:05 +0000112
113#define BTN_DIR_UP BUTTON_UP
114#define BTN_DIR_DOWN BUTTON_DOWN
115#define BTN_DIR_LEFT BUTTON_LEFT
116#define BTN_DIR_RIGHT BUTTON_RIGHT
117#define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
118#define BTN_QUIT BUTTON_OFF
119#define BTN_STOPRESET BUTTON_ON
120
Kevin Ferraref5c319b2006-06-30 17:03:01 +0000121#define BTN_RC_QUIT BUTTON_RC_STOP
122
Zakk Robertsc091a772006-03-13 03:13:05 +0000123#define PLAYERS_TEXT "Up/Down"
124#define WORMS_TEXT "Left/Right"
125
Jens Arnold85a226d2007-03-16 23:02:39 +0000126#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
Marcoen Hirschberg7db42702006-05-23 19:58:37 +0000127
128#define BTN_DIR_UP BUTTON_UP
129#define BTN_DIR_DOWN BUTTON_DOWN
130#define BTN_DIR_LEFT BUTTON_LEFT
131#define BTN_DIR_RIGHT BUTTON_RIGHT
132#define BTN_STARTPAUSE BUTTON_PLAY
133#define BTN_QUIT BUTTON_POWER
134#define BTN_STOPRESET BUTTON_REC
135
136#define PLAYERS_TEXT "Up/Down"
137#define WORMS_TEXT "Left/Right"
138
Marcoen Hirschberg9ad02de2006-05-22 16:28:19 +0000139#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
140
141#define BTN_DIR_UP BUTTON_UP
142#define BTN_DIR_DOWN BUTTON_DOWN
143#define BTN_DIR_LEFT BUTTON_LEFT
144#define BTN_DIR_RIGHT BUTTON_RIGHT
145#define BTN_STARTPAUSE BUTTON_SELECT
Marcoen Hirschberga7168fe2007-05-19 23:38:09 +0000146#define BTN_QUIT BUTTON_POWER
147#define BTN_STOPRESET BUTTON_A
Marcoen Hirschberg9ad02de2006-05-22 16:28:19 +0000148
149#define PLAYERS_TEXT "Up/Down"
150#define WORMS_TEXT "Left/Right"
151
Barry Wardell24f4a2a2006-10-26 13:38:09 +0000152
Marianne Arnold12ddb8e2007-09-20 10:49:48 +0000153#elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
154(CONFIG_KEYPAD == SANSA_C200_PAD)
Barry Wardell24f4a2a2006-10-26 13:38:09 +0000155
156#define BTN_DIR_UP BUTTON_UP
157#define BTN_DIR_DOWN BUTTON_DOWN
158#define BTN_DIR_LEFT BUTTON_LEFT
159#define BTN_DIR_RIGHT BUTTON_RIGHT
160#define BTN_STARTPAUSE BUTTON_SELECT
161#define BTN_QUIT BUTTON_POWER
162#define BTN_STOPRESET BUTTON_REC
163
164#define PLAYERS_TEXT "Up/Down"
165#define WORMS_TEXT "Left/Right"
166
167
Daniel Stenberg1e88be52006-08-03 20:17:25 +0000168#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
169
170#define BTN_DIR_UP BUTTON_SCROLL_UP
171#define BTN_DIR_DOWN BUTTON_SCROLL_DOWN
172#define BTN_DIR_LEFT BUTTON_LEFT
173#define BTN_DIR_RIGHT BUTTON_RIGHT
174#define BTN_STARTPAUSE BUTTON_PLAY
175#define BTN_QUIT BUTTON_POWER
176#define BTN_STOPRESET BUTTON_REW
177
178#define PLAYERS_TEXT "Up/Down"
179#define WORMS_TEXT "Left/Right"
180
Will Robertson8215b342008-02-17 12:23:02 +0000181#elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
182
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
190
191#define PLAYERS_TEXT "Up/Down"
192#define WORMS_TEXT "Left/Right"
193
Robert Kuklad6c8b572008-03-01 22:55:09 +0000194#elif (CONFIG_KEYPAD == MROBE100_PAD)
195
196#define BTN_DIR_UP BUTTON_UP
197#define BTN_DIR_DOWN BUTTON_DOWN
198#define BTN_DIR_LEFT BUTTON_LEFT
199#define BTN_DIR_RIGHT BUTTON_RIGHT
200#define BTN_STARTPAUSE BUTTON_SELECT
201#define BTN_QUIT BUTTON_POWER
202#define BTN_STOPRESET BUTTON_DISPLAY
203
204#define PLAYERS_TEXT "Up/Down"
205#define WORMS_TEXT "Left/Right"
206
Jens Arnold29361ab2008-03-22 10:24:28 +0000207#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
208
209#define BTN_DIR_UP BUTTON_RC_VOL_UP
210#define BTN_DIR_DOWN BUTTON_RC_VOL_DOWN
211#define BTN_DIR_LEFT BUTTON_RC_REW
212#define BTN_DIR_RIGHT BUTTON_RC_FF
213#define BTN_STARTPAUSE BUTTON_RC_PLAY
214#define BTN_QUIT BUTTON_RC_REC
215#define BTN_STOPRESET BUTTON_RC_MODE
216
217#define PLAYERS_TEXT "VOL UP/DN"
218#define WORMS_TEXT "REW/FF"
219
Rob Purchase554d7ed2008-03-22 22:03:34 +0000220#elif (CONFIG_KEYPAD == COWOND2_PAD)
221
Rob Purchase554d7ed2008-03-22 22:03:34 +0000222#define BTN_QUIT BUTTON_POWER
Rob Purchase554d7ed2008-03-22 22:03:34 +0000223
Robert Kuklad6c8b572008-03-01 22:55:09 +0000224#else
225#error No keymap defined!
Zakk Robertsc091a772006-03-13 03:13:05 +0000226#endif
227
Rob Purchase297e0502008-04-27 15:30:19 +0000228#ifdef HAVE_TOUCHPAD
229#ifndef BTN_DIR_UP
230#define BTN_DIR_UP BUTTON_TOPMIDDLE
231#endif
232#ifndef BTN_DIR_DOWN
233#define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
234#endif
235#ifndef BTN_DIR_LEFT
236#define BTN_DIR_LEFT BUTTON_MIDLEFT
237#endif
238#ifndef BTN_DIR_RIGHT
239#define BTN_DIR_RIGHT BUTTON_MIDRIGHT
240#endif
241#ifndef BTN_STARTPAUSE
242#define BTN_STARTPAUSE BUTTON_CENTER
243#endif
244#ifndef BTN_QUIT
245#define BTN_QUIT BUTTON_TOPLEFT
246#endif
247#ifndef BTN_STOPRESET
248#define BTN_STOPRESET BUTTON_TOPRIGHT
249
250#endif
251#ifndef PLAYERS_TEXT
252#define PLAYERS_TEXT "Up/Down"
253#endif
254#ifndef WORMS_TEXT
255#define WORMS_TEXT "Left/Right"
256#endif
257#endif
258
259
Zakk Robertsc091a772006-03-13 03:13:05 +0000260#if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
261#define FOOD_SIZE 3
262#define ARGH_SIZE 4
263#define SPEED 14
Zakk Roberts02e97d52006-05-22 06:56:39 +0000264#define MAX_WORM_SEGMENTS 128
Marianne Arnold12ddb8e2007-09-20 10:49:48 +0000265#elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
266#define FOOD_SIZE 3
267#define ARGH_SIZE 4
268#define SPEED 14
269#define MAX_WORM_SEGMENTS 128
Jens Arnold29361ab2008-03-22 10:24:28 +0000270#elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
271#define FOOD_SIZE 3
272#define ARGH_SIZE 4
273#define SPEED 12
274#define MAX_WORM_SEGMENTS 128
Jens Arnoldd3feb782006-03-30 17:29:21 +0000275#elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
276#define FOOD_SIZE 4
277#define ARGH_SIZE 5
278#define SPEED 10
Zakk Roberts02e97d52006-05-22 06:56:39 +0000279#define MAX_WORM_SEGMENTS 128
Tom Ross0edd86d2007-01-18 00:25:57 +0000280#elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
281#define FOOD_SIZE 4
282#define ARGH_SIZE 5
283#define SPEED 9
284#define MAX_WORM_SEGMENTS 128
Zakk Robertsc091a772006-03-13 03:13:05 +0000285#elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
286#define FOOD_SIZE 4
287#define ARGH_SIZE 5
288#define SPEED 8
Zakk Roberts02e97d52006-05-22 06:56:39 +0000289#define MAX_WORM_SEGMENTS 256
Zakk Robertsc091a772006-03-13 03:13:05 +0000290#elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
Zakk Robertsc091a772006-03-13 03:13:05 +0000291#define FOOD_SIZE 4
292#define ARGH_SIZE 5
293#define SPEED 6
Zakk Roberts02e97d52006-05-22 06:56:39 +0000294#define MAX_WORM_SEGMENTS 256
Zakk Robertsc091a772006-03-13 03:13:05 +0000295#elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
Zakk Robertsc091a772006-03-13 03:13:05 +0000296#define FOOD_SIZE 5
297#define ARGH_SIZE 6
298#define SPEED 4
Zakk Roberts02e97d52006-05-22 06:56:39 +0000299#define MAX_WORM_SEGMENTS 512
Barry Wardell41ee9e32007-01-15 20:40:48 +0000300#elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
301#define FOOD_SIZE 5
302#define ARGH_SIZE 6
303#define SPEED 4
304#define MAX_WORM_SEGMENTS 512
Zakk Robertsc091a772006-03-13 03:13:05 +0000305#elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240)
Zakk Robertsc091a772006-03-13 03:13:05 +0000306#define FOOD_SIZE 7
307#define ARGH_SIZE 8
308#define SPEED 4
Zakk Roberts02e97d52006-05-22 06:56:39 +0000309#define MAX_WORM_SEGMENTS 512
Marcoen Hirschberg9ad02de2006-05-22 16:28:19 +0000310#elif (LCD_WIDTH == 240) && (LCD_HEIGHT == 320)
311#define FOOD_SIZE 7
312#define ARGH_SIZE 8
313#define SPEED 4
314#define MAX_WORM_SEGMENTS 512
Zakk Roberts02e97d52006-05-22 06:56:39 +0000315#endif
316
317#ifdef HAVE_LCD_COLOR
318#define COLOR_WORM LCD_RGBPACK(80, 40, 0)
319#define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
320#define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
321#define COLOR_FG LCD_RGBPACK(0, 0, 0)
322#define COLOR_BG LCD_RGBPACK(181, 199, 231)
Zakk Robertsc091a772006-03-13 03:13:05 +0000323#endif
324
Björn Stenberg50053d12002-09-09 23:30:16 +0000325/**
326 * All the properties that a worm has.
327 */
Björn Stenbergc3fd67c2002-12-18 14:57:45 +0000328static struct worm {
Björn Stenberg50053d12002-09-09 23:30:16 +0000329 /* The worm is stored in a ring of xy coordinates */
Zakk Robertsc091a772006-03-13 03:13:05 +0000330 int x[MAX_WORM_SEGMENTS];
331 int y[MAX_WORM_SEGMENTS];
Björn Stenberg50053d12002-09-09 23:30:16 +0000332
333 int head; /* index of the head within the buffer */
334 int tail; /* index of the tail within the buffer */
335 int growing; /* number of cyles the worm still keeps growing */
336 bool alive; /* the worms living state */
337
338 /* direction vector in which the worm moves */
339 int dirx; /* only values -1 0 1 allowed */
340 int diry; /* only values -1 0 1 allowed */
341
342 /* this method is used to fetch the direction the user
343 has selected. It can be one of the values
Zakk Robertsc091a772006-03-13 03:13:05 +0000344 human_player1, human_player2, remote_player, virtual_player.
345 All these values are fuctions, that can change the direction
Björn Stenberg50053d12002-09-09 23:30:16 +0000346 of the worm */
347 void (*fetch_worm_direction)(struct worm *w);
348} worms[MAX_WORMS];
349
350/* stores the highscore - besides it was scored by a virtual player */
351static int highscore;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000352
Zakk Robertsc091a772006-03-13 03:13:05 +0000353#define MAX_FOOD 5 /* maximal number of food items */
Björn Stenberg50053d12002-09-09 23:30:16 +0000354
355/* The arrays store the food coordinates */
Zakk Roberts02e97d52006-05-22 06:56:39 +0000356static int foodx[MAX_FOOD];
357static int foody[MAX_FOOD];
Eric Linenbergb0a39842002-08-26 03:32:39 +0000358
Zakk Robertsc091a772006-03-13 03:13:05 +0000359#define MAX_ARGH 100 /* maximal number of argh items */
Björn Stenberge67958b2002-08-26 09:21:59 +0000360#define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
Eric Linenbergb0a39842002-08-26 03:32:39 +0000361
Björn Stenberg50053d12002-09-09 23:30:16 +0000362/* The arrays store the argh coordinates */
Zakk Roberts02e97d52006-05-22 06:56:39 +0000363static int arghx[MAX_ARGH];
364static int arghy[MAX_ARGH];
Eric Linenbergb0a39842002-08-26 03:32:39 +0000365
Zakk Robertsc091a772006-03-13 03:13:05 +0000366/* the number of arghs that are currently in use */
Björn Stenberg50053d12002-09-09 23:30:16 +0000367static int argh_count;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000368
Zakk Roberts02e97d52006-05-22 06:56:39 +0000369/* the number of arghs per food, settable by user */
370static int arghs_per_food = ARGHS_PER_FOOD;
371/* the size of the argh, settable by user */
372static int argh_size = ARGH_SIZE;
373/* the size of the food, settable by user */
374static int food_size = FOOD_SIZE;
375/* the speed of the worm, settable by user */
376static int speed = SPEED;
377/* the amount a worm grows by eating a food, settable by user */
378static int worm_food = WORM_PER_FOOD;
379
380/* End additional variables */
381
Björn Stenberg50053d12002-09-09 23:30:16 +0000382#ifdef DEBUG_WORMLET
383/* just a buffer used for debug output */
384static char debugout[15];
385#endif
386
Björn Stenberg50053d12002-09-09 23:30:16 +0000387/* the number of active worms (dead or alive) */
388static int worm_count = MAX_WORMS;
389
390/* in multiplayer mode: en- / disables the remote worm control
391 in singleplayer mode: toggles 4 / 2 button worm control */
392static bool use_remote = false;
393
394/* return values of check_collision */
Eric Linenbergb0a39842002-08-26 03:32:39 +0000395#define COLLISION_NONE 0
396#define COLLISION_WORM 1
397#define COLLISION_FOOD 2
398#define COLLISION_ARGH 3
399#define COLLISION_FIELD 4
400
Björn Stenberg50053d12002-09-09 23:30:16 +0000401/* constants for use as directions.
402 Note that the values are ordered clockwise.
403 Thus increasing / decreasing the values
404 is equivalent to right / left turns. */
405#define WEST 0
406#define NORTH 1
407#define EAST 2
408#define SOUTH 3
409
410/* direction of human player 1 */
Zakk Robertsc091a772006-03-13 03:13:05 +0000411static int player1_dir = EAST;
Björn Stenberg50053d12002-09-09 23:30:16 +0000412/* direction of human player 2 */
Zakk Robertsc091a772006-03-13 03:13:05 +0000413static int player2_dir = EAST;
Björn Stenberg50053d12002-09-09 23:30:16 +0000414/* direction of human player 3 */
415static int player3_dir = EAST;
416
417/* the number of (human) players that currently
418 control a worm */
419static int players = 1;
420
Björn Stenbergba371fb2003-06-29 16:33:04 +0000421/* the rockbox plugin api */
Steve Bavin65265772008-05-13 09:57:56 +0000422static const struct plugin_api* rb;
Björn Stenbergba371fb2003-06-29 16:33:04 +0000423
Zakk Roberts02e97d52006-05-22 06:56:39 +0000424#define SETTINGS_VERSION 1
425#define SETTINGS_MIN_VERSION 1
426#define SETTINGS_FILENAME "wormlet.cfg"
427
428static struct configdata config[] =
429{
430 {TYPE_INT, 0, 1024, &highscore, "highscore", NULL, NULL},
431 {TYPE_INT, 0, 15, &arghs_per_food, "arghs per food", NULL, NULL},
432 {TYPE_INT, 0, 15, &argh_size, "argh size", NULL, NULL},
433 {TYPE_INT, 0, 15, &food_size, "food size", NULL, NULL},
434 {TYPE_INT, 0, 3, &players, "players", NULL, NULL},
435 {TYPE_INT, 0, 3, &worm_count, "worms", NULL, NULL},
436 {TYPE_INT, 0, 20, &speed, "speed", NULL, NULL},
437 {TYPE_INT, 0, 15, &worm_food, "Worm Growth Per Food", NULL, NULL}//,
438 //{TYPE_INT, 0, 3, &use_remote, "use remote", NULL, NULL}
439};
440
Björn Stenberg50053d12002-09-09 23:30:16 +0000441#ifdef DEBUG_WORMLET
442static void set_debug_out(char *str){
443 strcpy(debugout, str);
444}
445#endif
Eric Linenbergb0a39842002-08-26 03:32:39 +0000446
447/**
Björn Stenberg50053d12002-09-09 23:30:16 +0000448 * Returns the direction id in which the worm
449 * currently is creeping.
Zakk Robertsc091a772006-03-13 03:13:05 +0000450 * @param struct worm *w The worm that is to be investigated.
Björn Stenberg50053d12002-09-09 23:30:16 +0000451 * w Must not be null.
452 * @return int A value 0 <= value < 4
453 * Note the predefined constants NORTH, SOUTH, EAST, WEST
Eric Linenbergb0a39842002-08-26 03:32:39 +0000454 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000455static int get_worm_dir(struct worm *w) {
456 int retVal ;
457 if (w->dirx == 0) {
458 if (w->diry == 1) {
459 retVal = SOUTH;
460 } else {
461 retVal = NORTH;
462 }
463 } else {
464 if (w->dirx == 1) {
465 retVal = EAST;
466 } else {
467 retVal = WEST;
468 }
469 }
470 return retVal;
471}
472
473/**
474 * Set the direction of the specified worm with a direction id.
475 * Increasing the value by 1 means to turn the worm direction
476 * to right by 90 degree.
477 * @param struct worm *w The worm that is to be altered. w Must not be null.
478 * @param int dir The new direction in which the worm is to creep.
Zakk Robertsc091a772006-03-13 03:13:05 +0000479 * dir must be 0 <= dir < 4. Use predefined constants
Björn Stenberg50053d12002-09-09 23:30:16 +0000480 * NORTH, SOUTH, EAST, WEST
481 */
482static void set_worm_dir(struct worm *w, int dir) {
483 switch (dir) {
484 case WEST:
485 w->dirx = -1;
486 w->diry = 0;
487 break;
488 case NORTH:
489 w->dirx = 0;
490 w->diry = - 1;
491 break;
492 case EAST:
493 w->dirx = 1;
494 w->diry = 0;
495 break;
496 case SOUTH:
497 w->dirx = 0;
498 w->diry = 1;
499 break;
500 }
501}
502
503/**
504 * Returns the current length of the worm array. This
505 * is also a value for the number of bends that are in the worm.
506 * @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
507 */
508static int get_worm_array_length(struct worm *w) {
Björn Stenberge67958b2002-08-26 09:21:59 +0000509 /* initial simple calculation will be overwritten if wrong. */
Björn Stenberg50053d12002-09-09 23:30:16 +0000510 int retVal = w->head - w->tail;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000511
Björn Stenberge67958b2002-08-26 09:21:59 +0000512 /* if the worm 'crosses' the boundaries of the ringbuffer */
Eric Linenbergb0a39842002-08-26 03:32:39 +0000513 if (retVal < 0) {
Björn Stenberg50053d12002-09-09 23:30:16 +0000514 retVal = w->head + MAX_WORM_SEGMENTS - w->tail;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000515 }
516
517 return retVal;
518}
519
520/**
Björn Stenberg50053d12002-09-09 23:30:16 +0000521 * Returns the score the specified worm. The score is the length
522 * of the worm.
523 * @param struct worm *w The worm that is to be investigated.
524 * w must not be null.
525 * @return int The length of the worm (>= 0).
526 */
527static int get_score(struct worm *w) {
528 int retval = 0;
529 int length = get_worm_array_length(w);
530 int i;
531 for (i = 0; i < length; i++) {
532
533 /* The iteration iterates the length of the worm.
534 Here's the conversion to the true indices within the worm arrays. */
535 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
536 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
537 int startx = w->x[linestart];
538 int starty = w->y[linestart];
539 int endx = w->x[lineend];
540 int endy = w->y[lineend];
541
542 int minimum, maximum;
543
544 if (startx == endx) {
545 minimum = MIN(starty, endy);
546 maximum = MAX(starty, endy);
547 } else {
548 minimum = MIN(startx, endx);
549 maximum = MAX(startx, endx);
550 }
551 retval += abs(maximum - minimum);
552 }
553 return retval;
554}
555
556/**
557 * Determines wether the line specified by startx, starty, endx, endy intersects
558 * the rectangle specified by x, y, width, height. Note that the line must be exactly
Zakk Robertsc091a772006-03-13 03:13:05 +0000559 * horizontal or vertical (startx == endx or starty == endy).
Björn Stenberg50053d12002-09-09 23:30:16 +0000560 * @param int startx The x coordinate of the start point of the line.
561 * @param int starty The y coordinate of the start point of the line.
562 * @param int endx The x coordinate of the end point of the line.
563 * @param int endy The y coordinate of the end point of the line.
564 * @param int x The x coordinate of the top left corner of the rectangle.
565 * @param int y The y coordinate of the top left corner of the rectangle.
566 * @param int width The width of the rectangle.
567 * @param int height The height of the rectangle.
568 * @return bool Returns true if the specified line intersects with the recangle.
569 */
570static bool line_in_rect(int startx, int starty, int endx, int endy, int x, int y, int width, int height) {
571 bool retval = false;
572 int simple, simplemin, simplemax;
573 int compa, compb, compmin, compmax;
574 int temp;
575 if (startx == endx) {
576 simple = startx;
577 simplemin = x;
578 simplemax = x + width;
579
580 compa = starty;
581 compb = endy;
582 compmin = y;
583 compmax = y + height;
584 } else {
585 simple = starty;
586 simplemin = y;
587 simplemax = y + height;
588
589 compa = startx;
590 compb = endx;
591 compmin = x;
592 compmax = x + width;
593 };
594
595 temp = compa;
596 compa = MIN(compa, compb);
597 compb = MAX(temp, compb);
598
Eric Linenberg380ca882002-09-17 12:39:47 +0000599 if (simplemin <= simple && simple <= simplemax) {
600 if ((compmin <= compa && compa <= compmax) ||
601 (compmin <= compb && compb <= compmax) ||
602 (compa <= compmin && compb >= compmax)) {
Björn Stenberg50053d12002-09-09 23:30:16 +0000603 retval = true;
604 }
605 }
606 return retval;
607}
608
Zakk Robertsc091a772006-03-13 03:13:05 +0000609/**
Björn Stenberg50053d12002-09-09 23:30:16 +0000610 * Tests wether the specified worm intersects with the rect.
611 * @param struct worm *w The worm to be investigated
612 * @param int x The x coordinate of the top left corner of the rect
613 * @param int y The y coordinate of the top left corner of the rect
614 * @param int widht The width of the rect
615 * @param int height The height of the rect
616 * @return bool Returns true if the worm intersects with the rect
617 */
618static bool worm_in_rect(struct worm *w, int x, int y, int width, int height) {
619 bool retval = false;
620
621
622 /* get_worm_array_length is expensive -> buffer the value */
623 int wormLength = get_worm_array_length(w);
624 int i;
625
626 /* test each entry that is part of the worm */
627 for (i = 0; i < wormLength && retval == false; i++) {
628
629 /* The iteration iterates the length of the worm.
630 Here's the conversion to the true indices within the worm arrays. */
631 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
632 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
633 int startx = w->x[linestart];
634 int starty = w->y[linestart];
635 int endx = w->x[lineend];
636 int endy = w->y[lineend];
637
638 retval = line_in_rect(startx, starty, endx, endy, x, y, width, height);
639 }
640
641 return retval;
642}
643
644/**
Eric Linenbergb0a39842002-08-26 03:32:39 +0000645 * Checks wether a specific food in the food arrays is at the
646 * specified coordinates.
647 * @param int foodIndex The index of the food in the food arrays
648 * @param int x the x coordinate.
649 * @param int y the y coordinate.
650 * @return Returns true if the coordinate hits the food specified by
651 * foodIndex.
652 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000653static bool specific_food_collision(int foodIndex, int x, int y) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000654 bool retVal = false;
655 if (x >= foodx[foodIndex] &&
Zakk Roberts02e97d52006-05-22 06:56:39 +0000656 x < foodx[foodIndex] + food_size &&
Eric Linenbergb0a39842002-08-26 03:32:39 +0000657 y >= foody[foodIndex] &&
Zakk Roberts02e97d52006-05-22 06:56:39 +0000658 y < foody[foodIndex] + food_size) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000659
660 retVal = true;
661 }
662 return retVal;
663}
664
665/**
666 * Returns the index of the food that is at the
667 * given coordinates. If no food is at the coordinates
668 * -1 is returned.
669 * @return int -1 <= value < MAX_FOOD
670 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000671static int food_collision(int x, int y) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000672 int i = 0;
673 int retVal = -1;
Björn Stenberge67958b2002-08-26 09:21:59 +0000674 for (i = 0; i < MAX_FOOD; i++) {
Björn Stenberg50053d12002-09-09 23:30:16 +0000675 if (specific_food_collision(i, x, y)) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000676 retVal = i;
Björn Stenberge67958b2002-08-26 09:21:59 +0000677 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000678 }
679 }
680 return retVal;
681}
682
683/**
684 * Checks wether a specific argh in the argh arrays is at the
685 * specified coordinates.
686 * @param int arghIndex The index of the argh in the argh arrays
687 * @param int x the x coordinate.
688 * @param int y the y coordinate.
689 * @return Returns true if the coordinate hits the argh specified by
690 * arghIndex.
691 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000692static bool specific_argh_collision(int arghIndex, int x, int y) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000693
Björn Stenberge67958b2002-08-26 09:21:59 +0000694 if ( x >= arghx[arghIndex] &&
695 y >= arghy[arghIndex] &&
Zakk Roberts02e97d52006-05-22 06:56:39 +0000696 x < arghx[arghIndex] + argh_size &&
697 y < arghy[arghIndex] + argh_size )
Björn Stenberge67958b2002-08-26 09:21:59 +0000698 {
699 return true;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000700 }
Björn Stenberge67958b2002-08-26 09:21:59 +0000701
702 return false;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000703}
704
705/**
706 * Returns the index of the argh that is at the
707 * given coordinates. If no argh is at the coordinates
708 * -1 is returned.
709 * @param int x The x coordinate.
710 * @param int y The y coordinate.
Björn Stenberg50053d12002-09-09 23:30:16 +0000711 * @return int -1 <= value < argh_count <= MAX_ARGH
Eric Linenbergb0a39842002-08-26 03:32:39 +0000712 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000713static int argh_collision(int x, int y) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000714 int i = 0;
715 int retVal = -1;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000716
Björn Stenberge67958b2002-08-26 09:21:59 +0000717 /* search for the argh that has the specified coords */
Björn Stenberg50053d12002-09-09 23:30:16 +0000718 for (i = 0; i < argh_count; i++) {
719 if (specific_argh_collision(i, x, y)) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000720 retVal = i;
Björn Stenberge67958b2002-08-26 09:21:59 +0000721 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000722 }
723 }
724 return retVal;
725}
726
727/**
728 * Checks wether the worm collides with the food at the specfied food-arrays.
729 * @param int foodIndex The index of the food in the arrays. Ensure the value is
730 * 0 <= foodIndex <= MAX_FOOD
731 * @return Returns true if the worm collides with the specified food.
732 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000733static bool worm_food_collision(struct worm *w, int foodIndex)
Björn Stenberge67958b2002-08-26 09:21:59 +0000734{
Eric Linenbergb0a39842002-08-26 03:32:39 +0000735 bool retVal = false;
736
Zakk Robertsc091a772006-03-13 03:13:05 +0000737 retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex],
Zakk Roberts02e97d52006-05-22 06:56:39 +0000738 food_size - 1, food_size - 1);
Eric Linenbergb0a39842002-08-26 03:32:39 +0000739
Björn Stenberg50053d12002-09-09 23:30:16 +0000740 return retVal;
741}
Eric Linenbergb0a39842002-08-26 03:32:39 +0000742
Björn Stenberg50053d12002-09-09 23:30:16 +0000743/**
744 * Returns true if the worm hits the argh within the next moves (unless
745 * the worm changes it's direction).
746 * @param struct worm *w - The worm to investigate
Zakk Robertsc091a772006-03-13 03:13:05 +0000747 * @param int argh_idx - The index of the argh
Björn Stenberg50053d12002-09-09 23:30:16 +0000748 * @param int moves - The number of moves that are considered.
749 * @return Returns false if the specified argh is not hit within the next
750 * moves.
751 */
752static bool worm_argh_collision_in_moves(struct worm *w, int argh_idx, int moves){
753 bool retVal = false;
754 int x1, y1, x2, y2;
755 x1 = w->x[w->head];
756 y1 = w->y[w->head];
Eric Linenbergb0a39842002-08-26 03:32:39 +0000757
Björn Stenberg50053d12002-09-09 23:30:16 +0000758 x2 = w->x[w->head] + moves * w->dirx;
759 y2 = w->y[w->head] + moves * w->diry;
760
Zakk Robertsc091a772006-03-13 03:13:05 +0000761 retVal = line_in_rect(x1, y1, x2, y2, arghx[argh_idx], arghy[argh_idx],
Zakk Roberts02e97d52006-05-22 06:56:39 +0000762 argh_size, argh_size);
Eric Linenbergb0a39842002-08-26 03:32:39 +0000763 return retVal;
764}
765
766/**
767 * Checks wether the worm collides with the argh at the specfied argh-arrays.
Björn Stenberge67958b2002-08-26 09:21:59 +0000768 * @param int arghIndex The index of the argh in the arrays.
Björn Stenberg50053d12002-09-09 23:30:16 +0000769 * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
Eric Linenbergb0a39842002-08-26 03:32:39 +0000770 * @return Returns true if the worm collides with the specified argh.
771 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000772static bool worm_argh_collision(struct worm *w, int arghIndex)
Björn Stenberge67958b2002-08-26 09:21:59 +0000773{
Eric Linenbergb0a39842002-08-26 03:32:39 +0000774 bool retVal = false;
775
Zakk Robertsc091a772006-03-13 03:13:05 +0000776 retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex],
Zakk Roberts02e97d52006-05-22 06:56:39 +0000777 argh_size - 1, argh_size - 1);
Eric Linenbergb0a39842002-08-26 03:32:39 +0000778
779 return retVal;
780}
781
782/**
783 * Find new coordinates for the food stored in foodx[index], foody[index]
784 * that don't collide with any other food or argh
Zakk Robertsc091a772006-03-13 03:13:05 +0000785 * @param int index
Eric Linenbergb0a39842002-08-26 03:32:39 +0000786 * Ensure that 0 <= index < MAX_FOOD.
787 */
Zakk Roberts02e97d52006-05-22 06:56:39 +0000788static void make_food(int index) {
Eric Linenbergb0a39842002-08-26 03:32:39 +0000789
790 int x = 0;
791 int y = 0;
792 bool collisionDetected = false;
Björn Stenberg50053d12002-09-09 23:30:16 +0000793 int i;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000794
795 do {
Björn Stenberge67958b2002-08-26 09:21:59 +0000796 /* make coordinates for a new food so that
797 the entire food lies within the FIELD */
Zakk Roberts02e97d52006-05-22 06:56:39 +0000798 x = rb->rand() % (FIELD_RECT_WIDTH - food_size);
799 y = rb->rand() % (FIELD_RECT_HEIGHT - food_size);
Eric Linenbergb0a39842002-08-26 03:32:39 +0000800
Björn Stenberge67958b2002-08-26 09:21:59 +0000801 /* Ensure that the new food doesn't collide with any
802 existing foods or arghs.
803 If one or more corners of the new food hit any existing
804 argh or food a collision is detected.
805 */
Zakk Robertsc091a772006-03-13 03:13:05 +0000806 collisionDetected =
Björn Stenberg50053d12002-09-09 23:30:16 +0000807 food_collision(x , y ) >= 0 ||
Zakk Roberts02e97d52006-05-22 06:56:39 +0000808 food_collision(x , y + food_size - 1) >= 0 ||
809 food_collision(x + food_size - 1, y ) >= 0 ||
810 food_collision(x + food_size - 1, y + food_size - 1) >= 0 ||
Zakk Robertsc091a772006-03-13 03:13:05 +0000811 argh_collision(x , y ) >= 0 ||
Zakk Roberts02e97d52006-05-22 06:56:39 +0000812 argh_collision(x , y + food_size - 1) >= 0 ||
813 argh_collision(x + food_size - 1, y ) >= 0 ||
814 argh_collision(x + food_size - 1, y + food_size - 1) >= 0;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000815
Björn Stenberge67958b2002-08-26 09:21:59 +0000816 /* use coordinates for further testing */
Eric Linenbergb0a39842002-08-26 03:32:39 +0000817 foodx[index] = x;
818 foody[index] = y;
819
Björn Stenberge67958b2002-08-26 09:21:59 +0000820 /* now test wether we accidently hit the worm with food ;) */
Björn Stenberg50053d12002-09-09 23:30:16 +0000821 i = 0;
822 for (i = 0; i < worm_count && !collisionDetected; i++) {
823 collisionDetected |= worm_food_collision(&worms[i], index);
824 }
Eric Linenbergb0a39842002-08-26 03:32:39 +0000825 }
826 while (collisionDetected);
Zakk Roberts02e97d52006-05-22 06:56:39 +0000827 return;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000828}
829
830/**
831 * Clears a food from the lcd buffer.
832 * @param int index The index of the food arrays under which
Zakk Robertsc091a772006-03-13 03:13:05 +0000833 * the coordinates of the desired food can be found. Ensure
Eric Linenbergb0a39842002-08-26 03:32:39 +0000834 * that the value is 0 <= index <= MAX_FOOD.
835 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000836static void clear_food(int index)
Björn Stenberge67958b2002-08-26 09:21:59 +0000837{
838 /* remove the old food from the screen */
Jens Arnold04daef12005-06-24 22:33:21 +0000839 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
840 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
841 foody[index] + FIELD_RECT_Y,
Zakk Roberts02e97d52006-05-22 06:56:39 +0000842 food_size, food_size);
Jens Arnold04daef12005-06-24 22:33:21 +0000843 rb->lcd_set_drawmode(DRMODE_SOLID);
Eric Linenbergb0a39842002-08-26 03:32:39 +0000844}
845
846/**
847 * Draws a food in the lcd buffer.
848 * @param int index The index of the food arrays under which
Zakk Robertsc091a772006-03-13 03:13:05 +0000849 * the coordinates of the desired food can be found. Ensure
Eric Linenbergb0a39842002-08-26 03:32:39 +0000850 * that the value is 0 <= index <= MAX_FOOD.
851 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000852static void draw_food(int index)
Björn Stenberge67958b2002-08-26 09:21:59 +0000853{
854 /* draw the food object */
Zakk Robertse8a05062006-03-13 07:05:15 +0000855#ifdef HAVE_LCD_COLOR
Zakk Roberts02e97d52006-05-22 06:56:39 +0000856 rb->lcd_set_foreground(COLOR_FOOD);
Zakk Robertsc091a772006-03-13 03:13:05 +0000857#endif
858 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
Jens Arnold04daef12005-06-24 22:33:21 +0000859 foody[index] + FIELD_RECT_Y,
Zakk Roberts02e97d52006-05-22 06:56:39 +0000860 food_size, food_size);
Jens Arnold04daef12005-06-24 22:33:21 +0000861 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
862 rb->lcd_fillrect(foodx[index] + FIELD_RECT_X + 1,
863 foody[index] + FIELD_RECT_Y + 1,
Zakk Roberts02e97d52006-05-22 06:56:39 +0000864 food_size - 2, food_size - 2);
Jens Arnold04daef12005-06-24 22:33:21 +0000865 rb->lcd_set_drawmode(DRMODE_SOLID);
Zakk Robertse8a05062006-03-13 07:05:15 +0000866#ifdef HAVE_LCD_COLOR
Zakk Roberts02e97d52006-05-22 06:56:39 +0000867 rb->lcd_set_foreground(COLOR_FG);
Zakk Robertsc091a772006-03-13 03:13:05 +0000868#endif
Eric Linenbergb0a39842002-08-26 03:32:39 +0000869}
870
871/**
872 * Find new coordinates for the argh stored in arghx[index], arghy[index]
873 * that don't collide with any other food or argh.
Zakk Robertsc091a772006-03-13 03:13:05 +0000874 * @param int index
Björn Stenberg50053d12002-09-09 23:30:16 +0000875 * Ensure that 0 <= index < argh_count < MAX_ARGH.
Eric Linenbergb0a39842002-08-26 03:32:39 +0000876 */
Zakk Roberts02e97d52006-05-22 06:56:39 +0000877static void make_argh(int index)
Björn Stenberge67958b2002-08-26 09:21:59 +0000878{
Zakk Robertsc091a772006-03-13 03:13:05 +0000879 int x = -1;
880 int y = -1;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000881 bool collisionDetected = false;
Björn Stenberg50053d12002-09-09 23:30:16 +0000882 int i;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000883
884 do {
Björn Stenberge67958b2002-08-26 09:21:59 +0000885 /* make coordinates for a new argh so that
886 the entire food lies within the FIELD */
Zakk Roberts02e97d52006-05-22 06:56:39 +0000887 x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
888 y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
Björn Stenberg50053d12002-09-09 23:30:16 +0000889
Björn Stenberge67958b2002-08-26 09:21:59 +0000890 /* Ensure that the new argh doesn't intersect with any
891 existing foods or arghs.
892 If one or more corners of the new argh hit any existing
893 argh or food an intersection is detected.
894 */
Zakk Robertsc091a772006-03-13 03:13:05 +0000895 collisionDetected =
896 food_collision(x , y ) >= 0 ||
Zakk Roberts02e97d52006-05-22 06:56:39 +0000897 food_collision(x , y + argh_size - 1) >= 0 ||
898 food_collision(x + argh_size - 1, y ) >= 0 ||
899 food_collision(x + argh_size - 1, y + argh_size - 1) >= 0 ||
Zakk Robertsc091a772006-03-13 03:13:05 +0000900 argh_collision(x , y ) >= 0 ||
Zakk Roberts02e97d52006-05-22 06:56:39 +0000901 argh_collision(x , y + argh_size - 1) >= 0 ||
902 argh_collision(x + argh_size - 1, y ) >= 0 ||
903 argh_collision(x + argh_size - 1, y + argh_size - 1) >= 0;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000904
Björn Stenberge67958b2002-08-26 09:21:59 +0000905 /* use the candidate coordinates to make a real argh */
Eric Linenbergb0a39842002-08-26 03:32:39 +0000906 arghx[index] = x;
907 arghy[index] = y;
908
Björn Stenberge67958b2002-08-26 09:21:59 +0000909 /* now test wether we accidently hit the worm with argh ;) */
Björn Stenberg50053d12002-09-09 23:30:16 +0000910 for (i = 0; i < worm_count && !collisionDetected; i++) {
911 collisionDetected |= worm_argh_collision(&worms[i], index);
Zakk Robertsc091a772006-03-13 03:13:05 +0000912 collisionDetected |= worm_argh_collision_in_moves(&worms[i], index,
Björn Stenberg50053d12002-09-09 23:30:16 +0000913 MIN_ARGH_DIST);
914 }
Eric Linenbergb0a39842002-08-26 03:32:39 +0000915 }
916 while (collisionDetected);
Zakk Roberts02e97d52006-05-22 06:56:39 +0000917 return;
Eric Linenbergb0a39842002-08-26 03:32:39 +0000918}
919
920/**
921 * Draws an argh in the lcd buffer.
922 * @param int index The index of the argh arrays under which
Zakk Robertsc091a772006-03-13 03:13:05 +0000923 * the coordinates of the desired argh can be found. Ensure
Björn Stenberg50053d12002-09-09 23:30:16 +0000924 * that the value is 0 <= index < argh_count <= MAX_ARGH.
Eric Linenbergb0a39842002-08-26 03:32:39 +0000925 */
Björn Stenberg50053d12002-09-09 23:30:16 +0000926static void draw_argh(int index)
Björn Stenberge67958b2002-08-26 09:21:59 +0000927{
928 /* draw the new argh */
Zakk Robertse8a05062006-03-13 07:05:15 +0000929#ifdef HAVE_LCD_COLOR
Zakk Roberts02e97d52006-05-22 06:56:39 +0000930 rb->lcd_set_foreground(COLOR_ARGH);
Zakk Robertsc091a772006-03-13 03:13:05 +0000931#endif
932 rb->lcd_fillrect(arghx[index] + FIELD_RECT_X,
933 arghy[index] + FIELD_RECT_Y,
Zakk Roberts02e97d52006-05-22 06:56:39 +0000934 argh_size, argh_size);
Zakk Robertse8a05062006-03-13 07:05:15 +0000935#ifdef HAVE_LCD_COLOR
Zakk Roberts02e97d52006-05-22 06:56:39 +0000936 rb->lcd_set_foreground(COLOR_FG);
Zakk Robertsc091a772006-03-13 03:13:05 +0000937#endif
Eric Linenbergb0a39842002-08-26 03:32:39 +0000938}
939
Björn Stenberg50053d12002-09-09 23:30:16 +0000940static void virtual_player(struct worm *w);
941/**
942 * Initialzes the specified worm with INITIAL_WORM_LENGTH
943 * and the tail at the specified position. The worm will
944 * be initialized alive and creeping EAST.
945 * @param struct worm *w The worm that is to be initialized
946 * @param int x The x coordinate at which the tail of the worm starts.
947 * x must be 0 <= x < FIELD_RECT_WIDTH.
948 * @param int y The y coordinate at which the tail of the worm starts
949 * y must be 0 <= y < FIELD_RECT_WIDTH.
950 */
951static void init_worm(struct worm *w, int x, int y){
952 /* initialize the worm size */
953 w->head = 1;
954 w->tail = 0;
955
956 w->x[w->head] = x + 1;
957 w->y[w->head] = y;
958
959 w->x[w->tail] = x;
960 w->y[w->tail] = y;
961
962 /* set the initial direction the worm creeps to */
963 w->dirx = 1;
964 w->diry = 0;
965
966 w->growing = INITIAL_WORM_LENGTH - 1;
967 w->alive = true;
968 w->fetch_worm_direction = virtual_player;
969}
970
Zakk Robertsc091a772006-03-13 03:13:05 +0000971/**
Björn Stenberg50053d12002-09-09 23:30:16 +0000972 * Writes the direction that was stored for
973 * human player 1 into the specified worm. This function
Zakk Robertsc091a772006-03-13 03:13:05 +0000974 * may be used to be stored in worm.fetch_worm_direction.
975 * The value of
Björn Stenberg50053d12002-09-09 23:30:16 +0000976 * the direction is read from player1_dir.
Zakk Robertsc091a772006-03-13 03:13:05 +0000977 * @param struct worm *w - The worm of which the direction
Björn Stenberg50053d12002-09-09 23:30:16 +0000978 * is altered.
979 */
980static void human_player1(struct worm *w) {
981 set_worm_dir(w, player1_dir);
982}
983
Zakk Robertsc091a772006-03-13 03:13:05 +0000984/**
Björn Stenberg50053d12002-09-09 23:30:16 +0000985 * Writes the direction that was stored for
986 * human player 2 into the specified worm. This function
Zakk Robertsc091a772006-03-13 03:13:05 +0000987 * may be used to be stored in worm.fetch_worm_direction.
988 * The value of
Björn Stenberg50053d12002-09-09 23:30:16 +0000989 * the direction is read from player2_dir.
Zakk Robertsc091a772006-03-13 03:13:05 +0000990 * @param struct worm *w - The worm of which the direction
Björn Stenberg50053d12002-09-09 23:30:16 +0000991 * is altered.
992 */
993static void human_player2(struct worm *w) {
994 set_worm_dir(w, player2_dir);
995}
996
Zakk Robertsc091a772006-03-13 03:13:05 +0000997/**
Björn Stenberg50053d12002-09-09 23:30:16 +0000998 * Writes the direction that was stored for
Zakk Robertsc091a772006-03-13 03:13:05 +0000999 * human player using a remote control
Björn Stenberg50053d12002-09-09 23:30:16 +00001000 * into the specified worm. This function
Zakk Robertsc091a772006-03-13 03:13:05 +00001001 * may be used to be stored in worm.fetch_worm_direction.
1002 * The value of
Björn Stenberg50053d12002-09-09 23:30:16 +00001003 * the direction is read from player3_dir.
Zakk Robertsc091a772006-03-13 03:13:05 +00001004 * @param struct worm *w - The worm of which the direction
Björn Stenberg50053d12002-09-09 23:30:16 +00001005 * is altered.
1006 */
1007static void remote_player(struct worm *w) {
1008 set_worm_dir(w, player3_dir);
1009}
1010
Eric Linenbergb0a39842002-08-26 03:32:39 +00001011/**
1012 * Initializes the worm-, food- and argh-arrays, draws a frame,
1013 * makes some food and argh and display all that stuff.
1014 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001015static void init_wormlet(void)
Björn Stenberge67958b2002-08-26 09:21:59 +00001016{
1017 int i;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001018
Björn Stenberg50053d12002-09-09 23:30:16 +00001019 for (i = 0; i< worm_count; i++) {
1020 /* Initialize all the worm coordinates to center. */
1021 int x = (int)(FIELD_RECT_WIDTH / 2);
1022 int y = (int)((FIELD_RECT_HEIGHT - 20)/ 2) + i * 10;
1023
1024 init_worm(&worms[i], x, y);
1025 }
1026
1027 player1_dir = EAST;
1028 player2_dir = EAST;
1029 player3_dir = EAST;
1030
1031 if (players > 0) {
1032 worms[0].fetch_worm_direction = human_player1;
1033 }
1034
1035 if (players > 1) {
1036 if (use_remote) {
1037 worms[1].fetch_worm_direction = remote_player;
1038 } else {
1039 worms[1].fetch_worm_direction = human_player2;
1040 }
1041 }
1042
1043 if (players > 2) {
1044 worms[2].fetch_worm_direction = human_player2;
1045 }
Björn Stenberge67958b2002-08-26 09:21:59 +00001046
Zakk Robertsc091a772006-03-13 03:13:05 +00001047 /* Needed when the game is restarted using BTN_STOPRESET */
Björn Stenbergba371fb2003-06-29 16:33:04 +00001048 rb->lcd_clear_display();
Eric Linenbergb0a39842002-08-26 03:32:39 +00001049
Björn Stenberge67958b2002-08-26 09:21:59 +00001050 /* make and display some food and argh */
Björn Stenberg50053d12002-09-09 23:30:16 +00001051 argh_count = MAX_FOOD;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001052 for (i = 0; i < MAX_FOOD; i++) {
Björn Stenberg50053d12002-09-09 23:30:16 +00001053 make_food(i);
1054 draw_food(i);
1055 make_argh(i);
1056 draw_argh(i);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001057 }
1058
Björn Stenberge67958b2002-08-26 09:21:59 +00001059 /* draw the game field */
Jens Arnold04daef12005-06-24 22:33:21 +00001060 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1061 rb->lcd_fillrect(0, 0, FIELD_RECT_WIDTH + 2, FIELD_RECT_HEIGHT + 2);
1062 rb->lcd_fillrect(1, 1, FIELD_RECT_WIDTH, FIELD_RECT_HEIGHT);
1063 rb->lcd_set_drawmode(DRMODE_SOLID);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001064
Björn Stenberge67958b2002-08-26 09:21:59 +00001065 /* make everything visible */
Björn Stenbergba371fb2003-06-29 16:33:04 +00001066 rb->lcd_update();
Eric Linenbergb0a39842002-08-26 03:32:39 +00001067}
1068
Björn Stenberg50053d12002-09-09 23:30:16 +00001069
Zakk Robertsc091a772006-03-13 03:13:05 +00001070/**
Björn Stenberg50053d12002-09-09 23:30:16 +00001071 * Move the worm one step further if it is alive.
Zakk Robertsc091a772006-03-13 03:13:05 +00001072 * The direction in which the worm moves is taken from dirx and diry.
Björn Stenberg50053d12002-09-09 23:30:16 +00001073 * move_worm decreases growing if > 0. While the worm is growing the tail
1074 * is left untouched.
1075 * @param struct worm *w The worm to move. w must not be NULL.
Eric Linenbergb0a39842002-08-26 03:32:39 +00001076 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001077static void move_worm(struct worm *w)
Björn Stenberge67958b2002-08-26 09:21:59 +00001078{
Björn Stenberg50053d12002-09-09 23:30:16 +00001079 if (w->alive) {
1080 /* determine the head point and its precessor */
1081 int headx = w->x[w->head];
1082 int heady = w->y[w->head];
1083 int prehead = (w->head + MAX_WORM_SEGMENTS - 1) % MAX_WORM_SEGMENTS;
1084 int preheadx = w->x[prehead];
1085 int preheady = w->y[prehead];
Eric Linenbergb0a39842002-08-26 03:32:39 +00001086
Björn Stenberg50053d12002-09-09 23:30:16 +00001087 /* determine the old direction */
1088 int olddirx;
1089 int olddiry;
1090 if (headx == preheadx) {
1091 olddirx = 0;
1092 olddiry = (heady > preheady) ? 1 : -1;
1093 } else {
1094 olddiry = 0;
1095 olddirx = (headx > preheadx) ? 1 : -1;
1096 }
Eric Linenbergb0a39842002-08-26 03:32:39 +00001097
Björn Stenberg50053d12002-09-09 23:30:16 +00001098 /* olddir == dir?
Zakk Robertsc091a772006-03-13 03:13:05 +00001099 a change of direction means a new segment
Björn Stenberg50053d12002-09-09 23:30:16 +00001100 has been opened */
1101 if (olddirx != w->dirx ||
1102 olddiry != w->diry) {
1103 w->head = (w->head + 1) % MAX_WORM_SEGMENTS;
1104 }
Eric Linenbergb0a39842002-08-26 03:32:39 +00001105
Björn Stenberg50053d12002-09-09 23:30:16 +00001106 /* new head position */
1107 w->x[w->head] = headx + w->dirx;
1108 w->y[w->head] = heady + w->diry;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001109
Eric Linenbergb0a39842002-08-26 03:32:39 +00001110
Björn Stenberg50053d12002-09-09 23:30:16 +00001111 /* while the worm is growing no tail procession is necessary */
1112 if (w->growing > 0) {
Björn Stenberge67958b2002-08-26 09:21:59 +00001113 /* update the worms grow state */
Björn Stenberg50053d12002-09-09 23:30:16 +00001114 w->growing--;
1115 }
Zakk Robertsc091a772006-03-13 03:13:05 +00001116
Björn Stenberg50053d12002-09-09 23:30:16 +00001117 /* if the worm isn't growing the tail has to be dragged */
1118 else {
Zakk Robertsc091a772006-03-13 03:13:05 +00001119 /* index of the end of the tail segment */
Björn Stenberg50053d12002-09-09 23:30:16 +00001120 int tail_segment_end = (w->tail + 1) % MAX_WORM_SEGMENTS;
1121
1122 /* drag the end of the tail */
Zakk Robertsc091a772006-03-13 03:13:05 +00001123 /* only one coordinate has to be altered. Here it is
1124 determined which one */
Björn Stenberg50053d12002-09-09 23:30:16 +00001125 int dir = 0; /* specifies wether the coord has to be in- or decreased */
1126 if (w->x[w->tail] == w->x[tail_segment_end]) {
1127 dir = (w->y[w->tail] - w->y[tail_segment_end] < 0) ? 1 : -1;
1128 w->y[w->tail] += dir;
1129 } else {
1130 dir = (w->x[w->tail] - w->x[tail_segment_end] < 0) ? 1 : -1;
1131 w->x[w->tail] += dir;
1132 }
1133
1134 /* when the tail has been dragged so far that it meets
1135 the next segment start the tail segment is obsolete and
1136 must be freed */
1137 if (w->x[w->tail] == w->x[tail_segment_end] &&
1138 w->y[w->tail] == w->y[tail_segment_end]){
Zakk Robertsc091a772006-03-13 03:13:05 +00001139
Björn Stenberg50053d12002-09-09 23:30:16 +00001140 /* drop the last tail point */
1141 w->tail = tail_segment_end;
1142 }
1143 }
1144 }
Eric Linenbergb0a39842002-08-26 03:32:39 +00001145}
1146
1147/**
Zakk Robertsc091a772006-03-13 03:13:05 +00001148 * Draws the head and clears the tail of the worm in
Eric Linenbergb0a39842002-08-26 03:32:39 +00001149 * the display buffer. lcd_update() is NOT called thus
1150 * the caller has to take care that the buffer is displayed.
1151 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001152static void draw_worm(struct worm *w)
Björn Stenberge67958b2002-08-26 09:21:59 +00001153{
Zakk Robertse8a05062006-03-13 07:05:15 +00001154#ifdef HAVE_LCD_COLOR
Zakk Roberts02e97d52006-05-22 06:56:39 +00001155 rb->lcd_set_foreground(COLOR_WORM);
Zakk Robertsc091a772006-03-13 03:13:05 +00001156#endif
Björn Stenberge67958b2002-08-26 09:21:59 +00001157 /* draw the new head */
Björn Stenberg50053d12002-09-09 23:30:16 +00001158 int x = w->x[w->head];
1159 int y = w->y[w->head];
1160 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001161 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
Björn Stenberg50053d12002-09-09 23:30:16 +00001162 }
Eric Linenbergb0a39842002-08-26 03:32:39 +00001163
Jens Arnold04daef12005-06-24 22:33:21 +00001164 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1165
Björn Stenberge67958b2002-08-26 09:21:59 +00001166 /* clear the space behind the worm */
Björn Stenberg50053d12002-09-09 23:30:16 +00001167 x = w->x[w->tail] ;
1168 y = w->y[w->tail] ;
1169 if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
Jens Arnold04daef12005-06-24 22:33:21 +00001170 rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
Björn Stenberg50053d12002-09-09 23:30:16 +00001171 }
Jens Arnold04daef12005-06-24 22:33:21 +00001172 rb->lcd_set_drawmode(DRMODE_SOLID);
Zakk Robertse8a05062006-03-13 07:05:15 +00001173#ifdef HAVE_LCD_COLOR
Zakk Roberts02e97d52006-05-22 06:56:39 +00001174 rb->lcd_set_foreground(COLOR_FG);
Zakk Robertsc091a772006-03-13 03:13:05 +00001175#endif
Eric Linenbergb0a39842002-08-26 03:32:39 +00001176}
1177
1178/**
Björn Stenberg50053d12002-09-09 23:30:16 +00001179 * Checks wether the coordinate is part of the worm. Returns
1180 * true if any part of the worm was hit - including the head.
Zakk Robertsc091a772006-03-13 03:13:05 +00001181 * @param x int The x coordinate
Eric Linenbergb0a39842002-08-26 03:32:39 +00001182 * @param y int The y coordinate
1183 * @return int The index of the worm arrays that contain x, y.
1184 * Returns -1 if the coordinates are not part of the worm.
Zakk Robertsc091a772006-03-13 03:13:05 +00001185 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001186static int specific_worm_collision(struct worm *w, int x, int y)
Björn Stenberge67958b2002-08-26 09:21:59 +00001187{
Eric Linenbergb0a39842002-08-26 03:32:39 +00001188 int retVal = -1;
1189
Björn Stenberg50053d12002-09-09 23:30:16 +00001190 /* get_worm_array_length is expensive -> buffer the value */
1191 int wormLength = get_worm_array_length(w);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001192 int i;
1193
Björn Stenberge67958b2002-08-26 09:21:59 +00001194 /* test each entry that is part of the worm */
Eric Linenbergb0a39842002-08-26 03:32:39 +00001195 for (i = 0; i < wormLength && retVal == -1; i++) {
1196
Björn Stenberge67958b2002-08-26 09:21:59 +00001197 /* The iteration iterates the length of the worm.
1198 Here's the conversion to the true indices within the worm arrays. */
Björn Stenberg50053d12002-09-09 23:30:16 +00001199 int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
1200 int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
1201 bool samex = (w->x[linestart] == x) && (w->x[lineend] == x);
1202 bool samey = (w->y[linestart] == y) && (w->y[lineend] == y);
1203 if (samex || samey){
1204 int test, min, max, tmp;
1205
1206 if (samey) {
1207 min = w->x[linestart];
1208 max = w->x[lineend];
Zakk Robertsc091a772006-03-13 03:13:05 +00001209 test = x;
Björn Stenberg50053d12002-09-09 23:30:16 +00001210 } else {
1211 min = w->y[linestart];
1212 max = w->y[lineend];
1213 test = y;
1214 }
1215
1216 tmp = min;
1217 min = MIN(min, max);
1218 max = MAX(tmp, max);
1219
1220 if (min <= test && test <= max) {
1221 retVal = lineend;
1222 }
1223 }
1224 }
1225 return retVal;
1226}
1227
1228/**
Zakk Robertsc091a772006-03-13 03:13:05 +00001229 * Increases the length of the specified worm by marking
Björn Stenberg50053d12002-09-09 23:30:16 +00001230 * that it may grow by len pixels. Note that the worm has
1231 * to move to make the growing happen.
1232 * @param worm *w The worm that is to be altered.
1233 * @param int len A positive value specifying the amount of
1234 * pixels the worm may grow.
1235 */
1236static void add_growing(struct worm *w, int len) {
1237 w->growing += len;
1238}
1239
1240/**
1241 * Determins the worm that is at the coordinates x, y. The parameter
1242 * w is a switch parameter that changes the functionality of worm_collision.
1243 * If w is specified and x,y hits the head of w NULL is returned.
Zakk Robertsc091a772006-03-13 03:13:05 +00001244 * This is a useful way to determine wether the head of w hits
1245 * any worm but including itself but excluding its own head.
Björn Stenberg50053d12002-09-09 23:30:16 +00001246 * (It hits always its own head ;))
1247 * If w is set to NULL worm_collision returns any worm including all heads
1248 * that is at position of x,y.
1249 * @param struct worm *w The worm of which the head should be excluded in
1250 * the test. w may be set to NULL.
1251 * @param int x The x coordinate that is checked
1252 * @param int y The y coordinate that is checkec
1253 * @return struct worm* The worm that has been hit by x,y. If no worm
1254 * was at the position NULL is returned.
1255 */
1256static struct worm* worm_collision(struct worm *w, int x, int y){
1257 struct worm *retVal = NULL;
1258 int i;
1259 for (i = 0; (i < worm_count) && (retVal == NULL); i++) {
1260 int collision_at = specific_worm_collision(&worms[i], x, y);
1261 if (collision_at != -1) {
1262 if (!(w == &worms[i] && collision_at == w->head)){
1263 retVal = &worms[i];
1264 }
1265 }
Eric Linenbergb0a39842002-08-26 03:32:39 +00001266 }
1267 return retVal;
1268}
1269
1270/**
1271 * Returns true if the head of the worm just has
Zakk Robertsc091a772006-03-13 03:13:05 +00001272 * crossed the field boundaries.
Eric Linenbergb0a39842002-08-26 03:32:39 +00001273 * @return bool true if the worm just has wrapped.
Zakk Robertsc091a772006-03-13 03:13:05 +00001274 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001275static bool field_collision(struct worm *w)
Björn Stenberge67958b2002-08-26 09:21:59 +00001276{
Eric Linenbergb0a39842002-08-26 03:32:39 +00001277 bool retVal = false;
Björn Stenberg50053d12002-09-09 23:30:16 +00001278 if ((w->x[w->head] >= FIELD_RECT_WIDTH) ||
1279 (w->y[w->head] >= FIELD_RECT_HEIGHT) ||
1280 (w->x[w->head] < 0) ||
1281 (w->y[w->head] < 0))
Björn Stenberge67958b2002-08-26 09:21:59 +00001282 {
Eric Linenbergb0a39842002-08-26 03:32:39 +00001283 retVal = true;
1284 }
1285 return retVal;
1286}
1287
Björn Stenberg50053d12002-09-09 23:30:16 +00001288
Eric Linenbergb0a39842002-08-26 03:32:39 +00001289/**
Zakk Robertsc091a772006-03-13 03:13:05 +00001290 * Returns true if the specified coordinates are within the
Björn Stenberg50053d12002-09-09 23:30:16 +00001291 * field specified by the FIELD_RECT_XXX constants.
1292 * @param int x The x coordinate of the point that is investigated
1293 * @param int y The y coordinate of the point that is investigated
Zakk Robertsc091a772006-03-13 03:13:05 +00001294 * @return bool Returns false if x,y specifies a point outside the
Björn Stenberg50053d12002-09-09 23:30:16 +00001295 * field of worms.
1296 */
1297static bool is_in_field_rect(int x, int y) {
1298 bool retVal = false;
1299 retVal = (x >= 0 && x < FIELD_RECT_WIDTH &&
1300 y >= 0 && y < FIELD_RECT_HEIGHT);
1301 return retVal;
1302}
1303
1304/**
1305 * Checks and returns wether the head of the w
Eric Linenbergb0a39842002-08-26 03:32:39 +00001306 * is colliding with something currently.
Björn Stenberge67958b2002-08-26 09:21:59 +00001307 * @return int One of the values:
1308 * COLLISION_NONE
Björn Stenberg50053d12002-09-09 23:30:16 +00001309 * COLLISION_w
Björn Stenberge67958b2002-08-26 09:21:59 +00001310 * COLLISION_FOOD
1311 * COLLISION_ARGH
1312 * COLLISION_FIELD
Eric Linenbergb0a39842002-08-26 03:32:39 +00001313 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001314static int check_collision(struct worm *w)
Björn Stenberge67958b2002-08-26 09:21:59 +00001315{
Eric Linenbergb0a39842002-08-26 03:32:39 +00001316 int retVal = COLLISION_NONE;
1317
Björn Stenberg50053d12002-09-09 23:30:16 +00001318 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL)
Eric Linenbergb0a39842002-08-26 03:32:39 +00001319 retVal = COLLISION_WORM;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001320
Björn Stenberg50053d12002-09-09 23:30:16 +00001321 if (food_collision(w->x[w->head], w->y[w->head]) >= 0)
Eric Linenbergb0a39842002-08-26 03:32:39 +00001322 retVal = COLLISION_FOOD;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001323
Björn Stenberg50053d12002-09-09 23:30:16 +00001324 if (argh_collision(w->x[w->head], w->y[w->head]) >= 0)
Eric Linenbergb0a39842002-08-26 03:32:39 +00001325 retVal = COLLISION_ARGH;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001326
Björn Stenberg50053d12002-09-09 23:30:16 +00001327 if (field_collision(w))
Eric Linenbergb0a39842002-08-26 03:32:39 +00001328 retVal = COLLISION_FIELD;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001329
1330 return retVal;
1331}
1332
Eric Linenbergb0a39842002-08-26 03:32:39 +00001333/**
Björn Stenberg50053d12002-09-09 23:30:16 +00001334 * Returns the index of the food that is closest to the point
Zakk Robertsc091a772006-03-13 03:13:05 +00001335 * specified by x, y. This index may be used in the foodx and
Björn Stenberg50053d12002-09-09 23:30:16 +00001336 * foody arrays.
1337 * @param int x The x coordinate of the point
1338 * @param int y The y coordinate of the point
1339 * @return int A value usable as index in foodx and foody.
1340 */
1341static int get_nearest_food(int x, int y){
1342 int nearestfood = 0;
1343 int olddistance = FIELD_RECT_WIDTH + FIELD_RECT_HEIGHT;
1344 int deltax = 0;
1345 int deltay = 0;
1346 int foodindex;
1347 for (foodindex = 0; foodindex < MAX_FOOD; foodindex++) {
1348 int distance;
1349 deltax = foodx[foodindex] - x;
1350 deltay = foody[foodindex] - y;
1351 deltax = deltax > 0 ? deltax : deltax * (-1);
1352 deltay = deltay > 0 ? deltay : deltay * (-1);
1353 distance = deltax + deltay;
1354
1355 if (distance < olddistance) {
1356 olddistance = distance;
1357 nearestfood = foodindex;
1358 }
1359 }
1360 return nearestfood;
1361}
1362
Zakk Robertsc091a772006-03-13 03:13:05 +00001363/**
Björn Stenberg50053d12002-09-09 23:30:16 +00001364 * Returns wether the specified position is next to the worm
Zakk Robertsc091a772006-03-13 03:13:05 +00001365 * and in the direction the worm looks. Use this method to
1366 * test wether this position would be hit with the next move of
Björn Stenberg50053d12002-09-09 23:30:16 +00001367 * the worm unless the worm changes its direction.
1368 * @param struct worm *w - The worm to be investigated
1369 * @param int x - The x coordinate of the position to test.
1370 * @param int y - The y coordinate of the position to test.
1371 * @return Returns true if the worm will hit the position unless
1372 * it change its direction before the next move.
1373 */
1374static bool is_in_front_of_worm(struct worm *w, int x, int y) {
1375 bool infront = false;
1376 int deltax = x - w->x[w->head];
1377 int deltay = y - w->y[w->head];
1378
1379 if (w->dirx == 0) {
1380 infront = (w->diry * deltay) > 0;
1381 } else {
1382 infront = (w->dirx * deltax) > 0;
1383 }
1384 return infront;
1385}
1386
1387/**
1388 * Returns true if the worm will collide with the next move unless
1389 * it changes its direction.
1390 * @param struct worm *w - The worm to be investigated.
1391 * @return Returns true if the worm will collide with the next move
1392 * unless it changes its direction.
1393 */
1394static bool will_worm_collide(struct worm *w) {
1395 int x = w->x[w->head] + w->dirx;
1396 int y = w->y[w->head] + w->diry;
1397 bool retVal = !is_in_field_rect(x, y);
1398 if (!retVal) {
1399 retVal = (argh_collision(x, y) != -1);
1400 }
Zakk Robertsc091a772006-03-13 03:13:05 +00001401
Björn Stenberg50053d12002-09-09 23:30:16 +00001402 if (!retVal) {
1403 retVal = (worm_collision(w, x, y) != NULL);
1404 }
1405 return retVal;
1406}
1407
Zakk Robertsc091a772006-03-13 03:13:05 +00001408/**
Björn Stenberg50053d12002-09-09 23:30:16 +00001409 * This function
1410 * may be used to be stored in worm.fetch_worm_direction for
Zakk Robertsc091a772006-03-13 03:13:05 +00001411 * worms that are not controlled by humans but by artificial stupidity.
Björn Stenberg50053d12002-09-09 23:30:16 +00001412 * A direction is searched that doesn't lead to collision but to the nearest
1413 * food - but not very intelligent. The direction is written to the specified
1414 * worm.
Zakk Robertsc091a772006-03-13 03:13:05 +00001415 * @param struct worm *w - The worm of which the direction
Björn Stenberg50053d12002-09-09 23:30:16 +00001416 * is altered.
1417 */
1418static void virtual_player(struct worm *w) {
1419 bool isright;
1420 int plana, planb, planc;
1421 /* find the next lunch */
1422 int nearestfood = get_nearest_food(w->x[w->head], w->y[w->head]);
1423
1424 /* determine in which direction it is */
1425
1426 /* in front of me? */
1427 bool infront = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1428
1429 /* left right of me? */
1430 int olddir = get_worm_dir(w);
1431 set_worm_dir(w, (olddir + 1) % 4);
1432 isright = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
1433 set_worm_dir(w, olddir);
1434
1435 /* detect situation, set strategy */
1436 if (infront) {
1437 if (isright) {
1438 plana = olddir;
1439 planb = (olddir + 1) % 4;
1440 planc = (olddir + 3) % 4;
1441 } else {
1442 plana = olddir;
1443 planb = (olddir + 3) % 4;
1444 planc = (olddir + 1) % 4;
1445 }
1446 } else {
1447 if (isright) {
1448 plana = (olddir + 1) % 4;
1449 planb = olddir;
1450 planc = (olddir + 3) % 4;
1451 } else {
1452 plana = (olddir + 3) % 4;
1453 planb = olddir;
1454 planc = (olddir + 1) % 4;
1455 }
1456 }
1457
1458 /* test for collision */
1459 set_worm_dir(w, plana);
1460 if (will_worm_collide(w)){
1461
1462 /* plan b */
1463 set_worm_dir(w, planb);
1464
1465 /* test for collision */
1466 if (will_worm_collide(w)) {
1467
1468 /* plan c */
1469 set_worm_dir(w, planc);
1470 }
1471 }
1472}
1473
1474/**
1475 * prints out the score board with all the status information
Eric Linenbergb0a39842002-08-26 03:32:39 +00001476 * about the game.
1477 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001478static void score_board(void)
Björn Stenberge67958b2002-08-26 09:21:59 +00001479{
Eric Linenbergb0a39842002-08-26 03:32:39 +00001480 char buf[15];
Björn Stenbergba371fb2003-06-29 16:33:04 +00001481 char* buf2 = NULL;
Björn Stenberg50053d12002-09-09 23:30:16 +00001482 int i;
1483 int y = 0;
Jens Arnold04daef12005-06-24 22:33:21 +00001484 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1485 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, 0, LCD_WIDTH - FIELD_RECT_WIDTH - 2, LCD_HEIGHT);
1486 rb->lcd_set_drawmode(DRMODE_SOLID);
Björn Stenberg50053d12002-09-09 23:30:16 +00001487 for (i = 0; i < worm_count; i++) {
1488 int score = get_score(&worms[i]);
Zakk Robertsc091a772006-03-13 03:13:05 +00001489
Björn Stenberg50053d12002-09-09 23:30:16 +00001490 /* high score */
1491 if (worms[i].fetch_worm_direction != virtual_player){
1492 if (highscore < score) {
1493 highscore = score;
1494 }
1495 }
Eric Linenbergb0a39842002-08-26 03:32:39 +00001496
Björn Stenbergba371fb2003-06-29 16:33:04 +00001497 /* length */
1498 rb->snprintf(buf, sizeof (buf),"Len:%d", score);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001499
Björn Stenberg50053d12002-09-09 23:30:16 +00001500 /* worm state */
1501 switch (check_collision(&worms[i])) {
Zakk Robertsc091a772006-03-13 03:13:05 +00001502 case COLLISION_NONE:
Björn Stenbergba371fb2003-06-29 16:33:04 +00001503 if (worms[i].growing > 0)
1504 buf2 = "Growing";
Björn Stenberg50053d12002-09-09 23:30:16 +00001505 else {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001506 if (worms[i].alive)
1507 buf2 = "Hungry";
1508 else
1509 buf2 = "Wormed";
Björn Stenberg50053d12002-09-09 23:30:16 +00001510 }
Björn Stenbergba371fb2003-06-29 16:33:04 +00001511 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001512
Zakk Robertsc091a772006-03-13 03:13:05 +00001513 case COLLISION_WORM:
Björn Stenbergba371fb2003-06-29 16:33:04 +00001514 buf2 = "Wormed";
1515 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001516
Zakk Robertsc091a772006-03-13 03:13:05 +00001517 case COLLISION_FOOD:
Björn Stenbergba371fb2003-06-29 16:33:04 +00001518 buf2 = "Growing";
1519 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001520
Zakk Robertsc091a772006-03-13 03:13:05 +00001521 case COLLISION_ARGH:
Björn Stenbergba371fb2003-06-29 16:33:04 +00001522 buf2 = "Argh";
1523 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001524
Zakk Robertsc091a772006-03-13 03:13:05 +00001525 case COLLISION_FIELD:
Björn Stenbergba371fb2003-06-29 16:33:04 +00001526 buf2 = "Crashed";
1527 break;
1528 }
1529 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y , buf);
1530 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y+8, buf2);
Björn Stenberg50053d12002-09-09 23:30:16 +00001531
1532 if (!worms[i].alive){
Jens Arnold04daef12005-06-24 22:33:21 +00001533 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1534 rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, y,
1535 LCD_WIDTH - FIELD_RECT_WIDTH - 2, 17);
1536 rb->lcd_set_drawmode(DRMODE_SOLID);
Björn Stenberg50053d12002-09-09 23:30:16 +00001537 }
1538 y += 19;
1539 }
Björn Stenbergba371fb2003-06-29 16:33:04 +00001540 rb->snprintf(buf , sizeof(buf), "Hs: %d", highscore);
Björn Stenberg50053d12002-09-09 23:30:16 +00001541#ifndef DEBUG_WORMLET
Björn Stenbergba371fb2003-06-29 16:33:04 +00001542 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, buf);
Björn Stenberg50053d12002-09-09 23:30:16 +00001543#else
Björn Stenbergba371fb2003-06-29 16:33:04 +00001544 rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout);
Björn Stenberg50053d12002-09-09 23:30:16 +00001545#endif
Eric Linenbergb0a39842002-08-26 03:32:39 +00001546}
1547
1548/**
1549 * Checks for collisions of the worm and its environment and
1550 * takes appropriate actions like growing the worm or killing it.
1551 * @return bool Returns true if the worm is dead. Returns
1552 * false if the worm is healthy, up and creeping.
1553 */
Björn Stenberg50053d12002-09-09 23:30:16 +00001554static bool process_collisions(struct worm *w)
Björn Stenberge67958b2002-08-26 09:21:59 +00001555{
Eric Linenbergb0a39842002-08-26 03:32:39 +00001556 int index = -1;
1557
Björn Stenberg50053d12002-09-09 23:30:16 +00001558 w->alive &= !field_collision(w);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001559
Björn Stenberg50053d12002-09-09 23:30:16 +00001560 if (w->alive) {
Eric Linenbergb0a39842002-08-26 03:32:39 +00001561
Björn Stenberge67958b2002-08-26 09:21:59 +00001562 /* check if food was eaten */
Björn Stenberg50053d12002-09-09 23:30:16 +00001563 index = food_collision(w->x[w->head], w->y[w->head]);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001564 if (index != -1){
Björn Stenberge67958b2002-08-26 09:21:59 +00001565 int i;
Zakk Robertsc091a772006-03-13 03:13:05 +00001566
Björn Stenberg50053d12002-09-09 23:30:16 +00001567 clear_food(index);
1568 make_food(index);
1569 draw_food(index);
Zakk Robertsc091a772006-03-13 03:13:05 +00001570
Zakk Roberts02e97d52006-05-22 06:56:39 +00001571 for (i = 0; i < arghs_per_food; i++) {
Björn Stenberg50053d12002-09-09 23:30:16 +00001572 argh_count++;
1573 if (argh_count > MAX_ARGH)
1574 argh_count = MAX_ARGH;
1575 make_argh(argh_count - 1);
1576 draw_argh(argh_count - 1);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001577 }
1578
Zakk Roberts02e97d52006-05-22 06:56:39 +00001579 add_growing(w, worm_food);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001580
Björn Stenberg50053d12002-09-09 23:30:16 +00001581 draw_worm(w);
Eric Linenbergb0a39842002-08-26 03:32:39 +00001582 }
1583
Björn Stenberge67958b2002-08-26 09:21:59 +00001584 /* check if argh was eaten */
Eric Linenbergb0a39842002-08-26 03:32:39 +00001585 else {
Björn Stenberg50053d12002-09-09 23:30:16 +00001586 index = argh_collision(w->x[w->head], w->y[w->head]);
1587 if (index != -1) {
1588 w->alive = false;
1589 }
1590 else {
1591 if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) {
1592 w->alive = false;
1593 }
1594 }
Eric Linenbergb0a39842002-08-26 03:32:39 +00001595 }
1596 }
Björn Stenberg50053d12002-09-09 23:30:16 +00001597 return !w->alive;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001598}
1599
1600/**
1601 * The main loop of the game.
1602 * @return bool Returns true if the game ended
1603 * with a dead worm. Returns false if the user
1604 * aborted the game manually.
Zakk Robertsc091a772006-03-13 03:13:05 +00001605 */
Zakk Roberts02e97d52006-05-22 06:56:39 +00001606static int run(void)
Björn Stenberge67958b2002-08-26 09:21:59 +00001607{
Eric Linenbergb0a39842002-08-26 03:32:39 +00001608 int button = 0;
1609 int wormDead = false;
Zakk Roberts02e97d52006-05-22 06:56:39 +00001610 bool paused = false;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001611
Björn Stenberg50053d12002-09-09 23:30:16 +00001612 /* ticks are counted to compensate speed variations */
1613 long cycle_start = 0, cycle_end = 0;
1614#ifdef DEBUG_WORMLET
1615 int ticks_to_max_cycle_reset = 20;
1616 long max_cycle = 0;
1617 char buf[20];
1618#endif
Eric Linenbergb0a39842002-08-26 03:32:39 +00001619
Björn Stenberg50053d12002-09-09 23:30:16 +00001620 /* initialize the board and so on */
1621 init_wormlet();
1622
Björn Stenbergba371fb2003-06-29 16:33:04 +00001623 cycle_start = *rb->current_tick;
Björn Stenberge67958b2002-08-26 09:21:59 +00001624 /* change the direction of the worm */
Zakk Roberts02e97d52006-05-22 06:56:39 +00001625 while (!wormDead)
Eric Linenbergb0a39842002-08-26 03:32:39 +00001626 {
Björn Stenberg50053d12002-09-09 23:30:16 +00001627 int i;
Zakk Roberts02e97d52006-05-22 06:56:39 +00001628 long cycle_duration=0;
1629
Shachar Liberman5360df92006-07-30 03:10:09 +00001630#ifdef HAS_BUTTON_HOLD
1631 if (rb->button_hold())
1632 paused = true;
1633#endif
1634
Eric Linenbergb0a39842002-08-26 03:32:39 +00001635 switch (button) {
Zakk Roberts02e97d52006-05-22 06:56:39 +00001636 case BTN_STARTPAUSE:
1637 paused = !paused;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001638 break;
Zakk Roberts02e97d52006-05-22 06:56:39 +00001639 case BTN_STOPRESET:
1640 if (paused)
1641 return 1; /* restart game */
1642 else
1643 paused = true;
1644 break;
Kevin Ferrare0e027bd2006-06-30 16:43:47 +00001645#ifdef BTN_RC_QUIT
1646 case BTN_RC_QUIT:
1647#endif
Zakk Roberts02e97d52006-05-22 06:56:39 +00001648 case BTN_QUIT:
1649 return 2; /* back to menu */
1650 break;
1651 }
1652 if (!paused)
1653 {
1654 switch (button) {
1655 case BTN_DIR_UP:
1656 if (players == 1 && !use_remote) {
1657 player1_dir = NORTH;
1658 }
1659 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001660
Zakk Roberts02e97d52006-05-22 06:56:39 +00001661 case BTN_DIR_DOWN:
1662 if (players == 1 && !use_remote) {
1663 player1_dir = SOUTH;
1664 }
1665 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001666
Zakk Roberts02e97d52006-05-22 06:56:39 +00001667 case BTN_DIR_LEFT:
1668 if (players != 1 || use_remote) {
1669 player1_dir = (player1_dir + 3) % 4;
1670 } else {
1671 player1_dir = WEST;
1672 }
1673 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001674
Zakk Roberts02e97d52006-05-22 06:56:39 +00001675 case BTN_DIR_RIGHT:
1676 if (players != 1 || use_remote) {
1677 player1_dir = (player1_dir + 1) % 4;
1678 } else {
1679 player1_dir = EAST;
1680 }
1681 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001682
Zakk Robertsc091a772006-03-13 03:13:05 +00001683#ifdef MULTIPLAYER
Zakk Roberts02e97d52006-05-22 06:56:39 +00001684 case BTN_PLAYER2_DIR1:
1685 player2_dir = (player2_dir + 3) % 4;
1686 break;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001687
Zakk Roberts02e97d52006-05-22 06:56:39 +00001688 case BTN_PLAYER2_DIR2:
1689 player2_dir = (player2_dir + 1) % 4;
1690 break;
Zakk Robertsc091a772006-03-13 03:13:05 +00001691#endif
Björn Stenberg50053d12002-09-09 23:30:16 +00001692
Zakk Robertsc091a772006-03-13 03:13:05 +00001693#ifdef REMOTE
Zakk Roberts02e97d52006-05-22 06:56:39 +00001694 case BTN_RC_UP:
1695 player3_dir = (player3_dir + 1) % 4;
1696 break;
Björn Stenberg50053d12002-09-09 23:30:16 +00001697
Zakk Roberts02e97d52006-05-22 06:56:39 +00001698 case BTN_RC_DOWN:
1699 player3_dir = (player3_dir + 3) % 4;
1700 break;
Zakk Robertsc091a772006-03-13 03:13:05 +00001701#endif
Zakk Roberts02e97d52006-05-22 06:56:39 +00001702 }
Björn Stenberg50053d12002-09-09 23:30:16 +00001703
Eric Linenbergb0a39842002-08-26 03:32:39 +00001704
Zakk Roberts02e97d52006-05-22 06:56:39 +00001705 for (i = 0; i < worm_count; i++) {
1706 worms[i].fetch_worm_direction(&worms[i]);
1707 }
Björn Stenberg50053d12002-09-09 23:30:16 +00001708
Björn Stenberg50053d12002-09-09 23:30:16 +00001709 wormDead = true;
Zakk Roberts02e97d52006-05-22 06:56:39 +00001710 for (i = 0; i < worm_count; i++){
1711 struct worm *w = &worms[i];
1712 move_worm(w);
1713 wormDead &= process_collisions(w);
1714 draw_worm(w);
1715 }
1716 score_board();
1717 rb->lcd_update();
1718 if (button == BTN_STOPRESET) {
1719 wormDead = true;
1720 }
Björn Stenberg50053d12002-09-09 23:30:16 +00001721
Zakk Roberts02e97d52006-05-22 06:56:39 +00001722 /* here the wormlet game cycle ends
1723 thus the current tick is stored
1724 as end time */
1725 cycle_end = *rb->current_tick;
Björn Stenberg50053d12002-09-09 23:30:16 +00001726
Zakk Roberts02e97d52006-05-22 06:56:39 +00001727 /* The duration of the game cycle */
1728 cycle_duration = cycle_end - cycle_start;
1729 cycle_duration = MAX(0, cycle_duration);
1730 cycle_duration = MIN(speed -1, cycle_duration);
Björn Stenberg50053d12002-09-09 23:30:16 +00001731
1732
1733#ifdef DEBUG_WORMLET
Zakk Roberts02e97d52006-05-22 06:56:39 +00001734 ticks_to_max_cycle_reset--;
1735 if (ticks_to_max_cycle_reset <= 0) {
1736 max_cycle = 0;
1737 }
Björn Stenberg50053d12002-09-09 23:30:16 +00001738
Zakk Roberts02e97d52006-05-22 06:56:39 +00001739 if (max_cycle < cycle_duration) {
1740 max_cycle = cycle_duration;
1741 ticks_to_max_cycle_reset = 20;
1742 }
1743 rb->snprintf(buf, sizeof buf, "ticks %d", max_cycle);
1744 set_debug_out(buf);
Björn Stenberg50053d12002-09-09 23:30:16 +00001745#endif
Zakk Roberts02e97d52006-05-22 06:56:39 +00001746 }
Björn Stenberg50053d12002-09-09 23:30:16 +00001747 /* adjust the number of ticks to wait for a button.
1748 This ensures that a complete game cycle including
1749 user input runs in constant time */
Zakk Roberts02e97d52006-05-22 06:56:39 +00001750 button = rb->button_get_w_tmo(speed - cycle_duration);
Björn Stenbergba371fb2003-06-29 16:33:04 +00001751 cycle_start = *rb->current_tick;
Eric Linenbergb0a39842002-08-26 03:32:39 +00001752 }
Zakk Roberts02e97d52006-05-22 06:56:39 +00001753
Jens Arnold4d6374c2007-03-16 21:56:08 +00001754 rb->splash(HZ*2, "Game Over!");
Zakk Roberts02e97d52006-05-22 06:56:39 +00001755
1756 return 2; /* back to menu */
Eric Linenbergb0a39842002-08-26 03:32:39 +00001757}
1758
Björn Stenberg50053d12002-09-09 23:30:16 +00001759#ifdef DEBUG_WORMLET
1760
1761/**
1762 * Just a test routine that checks that worm_food_collision works
1763 * in some typical situations.
1764 */
1765static void test_worm_food_collision(void) {
1766 int collision_count = 0;
1767 int i;
Björn Stenbergba371fb2003-06-29 16:33:04 +00001768 rb->lcd_clear_display();
Björn Stenberg50053d12002-09-09 23:30:16 +00001769 init_worm(&worms[0], 10, 10);
1770 add_growing(&worms[0], 10);
1771 set_worm_dir(&worms[0], EAST);
1772 for (i = 0; i < 10; i++) {
1773 move_worm(&worms[0]);
1774 draw_worm(&worms[0]);
1775 }
1776
1777 set_worm_dir(&worms[0], SOUTH);
1778 for (i = 0; i < 10; i++) {
1779 move_worm(&worms[0]);
1780 draw_worm(&worms[0]);
1781 }
1782
1783 foodx[0] = 15;
1784 foody[0] = 12;
1785 for (foody[0] = 20; foody[0] > 0; foody[0] --) {
1786 char buf[20];
1787 bool collision;
1788 draw_worm(&worms[0]);
1789 draw_food(0);
1790 collision = worm_food_collision(&worms[0], 0);
1791 if (collision) {
1792 collision_count++;
1793 }
Björn Stenbergba371fb2003-06-29 16:33:04 +00001794 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1795 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1796 rb->lcd_update();
Björn Stenberg50053d12002-09-09 23:30:16 +00001797 }
Zakk Roberts02e97d52006-05-22 06:56:39 +00001798 if (collision_count != food_size) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001799 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001800 }
1801
1802
1803 foody[0] = 15;
1804 for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) {
1805 char buf[20];
1806 bool collision;
1807 draw_worm(&worms[0]);
1808 draw_food(0);
1809 collision = worm_food_collision(&worms[0], 0);
1810 if (collision) {
1811 collision_count ++;
1812 }
Björn Stenbergba371fb2003-06-29 16:33:04 +00001813 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1814 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1815 rb->lcd_update();
Björn Stenberg50053d12002-09-09 23:30:16 +00001816 }
Zakk Roberts02e97d52006-05-22 06:56:39 +00001817 if (collision_count != food_size * 2) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001818 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001819 }
1820
1821}
1822
Eric Linenberg380ca882002-09-17 12:39:47 +00001823static bool expensive_worm_in_rect(struct worm *w, int rx, int ry, int rw, int rh){
1824 int x, y;
1825 bool retVal = false;
1826 for (x = rx; x < rx + rw; x++){
1827 for (y = ry; y < ry + rh; y++) {
1828 if (specific_worm_collision(w, x, y) != -1) {
1829 retVal = true;
1830 }
1831 }
1832 }
1833 return retVal;
1834}
1835
Björn Stenberg50053d12002-09-09 23:30:16 +00001836static void test_worm_argh_collision(void) {
1837 int i;
1838 int dir;
1839 int collision_count = 0;
Björn Stenbergba371fb2003-06-29 16:33:04 +00001840 rb->lcd_clear_display();
Björn Stenberg50053d12002-09-09 23:30:16 +00001841 init_worm(&worms[0], 10, 10);
1842 add_growing(&worms[0], 40);
1843 for (dir = 0; dir < 4; dir++) {
1844 set_worm_dir(&worms[0], (EAST + dir) % 4);
1845 for (i = 0; i < 10; i++) {
1846 move_worm(&worms[0]);
1847 draw_worm(&worms[0]);
1848 }
1849 }
1850
1851 arghx[0] = 12;
Zakk Roberts02e97d52006-05-22 06:56:39 +00001852 for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){
Björn Stenberg50053d12002-09-09 23:30:16 +00001853 char buf[20];
1854 bool collision;
1855 draw_argh(0);
1856 collision = worm_argh_collision(&worms[0], 0);
1857 if (collision) {
1858 collision_count ++;
1859 }
Björn Stenbergba371fb2003-06-29 16:33:04 +00001860 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1861 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1862 rb->lcd_update();
Björn Stenberg50053d12002-09-09 23:30:16 +00001863 }
Zakk Roberts02e97d52006-05-22 06:56:39 +00001864 if (collision_count != argh_size * 2) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001865 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001866 }
1867
1868 arghy[0] = 12;
Zakk Roberts02e97d52006-05-22 06:56:39 +00001869 for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){
Björn Stenberg50053d12002-09-09 23:30:16 +00001870 char buf[20];
1871 bool collision;
1872 draw_argh(0);
1873 collision = worm_argh_collision(&worms[0], 0);
1874 if (collision) {
1875 collision_count ++;
1876 }
Björn Stenbergba371fb2003-06-29 16:33:04 +00001877 rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count);
1878 rb->lcd_putsxy(0, LCD_HEIGHT -8, buf);
1879 rb->lcd_update();
Björn Stenberg50053d12002-09-09 23:30:16 +00001880 }
Zakk Roberts02e97d52006-05-22 06:56:39 +00001881 if (collision_count != argh_size * 4) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001882 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001883 }
1884}
1885
1886static int testline_in_rect(void) {
1887 int testfailed = -1;
1888
1889 int rx = 10;
1890 int ry = 15;
1891 int rw = 20;
1892 int rh = 25;
1893
1894 /* Test 1 */
1895 int x1 = 12;
1896 int y1 = 8;
1897 int x2 = 12;
1898 int y2 = 42;
1899
1900 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1901 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001902 rb->lcd_drawrect(rx, ry, rw, rh);
1903 rb->lcd_drawline(x1, y1, x2, y2);
1904 rb->lcd_update();
1905 rb->lcd_putsxy(0, 0, "failed 1");
1906 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001907 testfailed = 1;
1908 }
1909
1910 /* test 2 */
1911 y2 = 20;
1912 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1913 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001914 rb->lcd_drawrect(rx, ry, rw, rh);
1915 rb->lcd_drawline(x1, y1, x2, y2);
1916 rb->lcd_putsxy(0, 0, "failed 2");
1917 rb->lcd_update();
1918 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001919 testfailed = 2;
1920 }
1921
1922 /* test 3 */
1923 y1 = 30;
1924 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1925 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001926 rb->lcd_drawrect(rx, ry, rw, rh);
1927 rb->lcd_drawline(x1, y1, x2, y2);
1928 rb->lcd_putsxy(0, 0, "failed 3");
1929 rb->lcd_update();
1930 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001931 testfailed = 3;
1932 }
1933
1934 /* test 4 */
1935 y2 = 45;
1936 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1937 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001938 rb->lcd_drawrect(rx, ry, rw, rh);
1939 rb->lcd_drawline(x1, y1, x2, y2);
1940 rb->lcd_putsxy(0, 0, "failed 4");
1941 rb->lcd_update();
1942 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001943 testfailed = 4;
1944 }
1945
1946 /* test 5 */
1947 y1 = 50;
1948 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
1949 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001950 rb->lcd_drawrect(rx, ry, rw, rh);
1951 rb->lcd_drawline(x1, y1, x2, y2);
1952 rb->lcd_putsxy(0, 0, "failed 5");
1953 rb->lcd_update();
1954 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001955 testfailed = 5;
1956 }
1957
1958 /* test 6 */
1959 y1 = 5;
1960 y2 = 7;
1961 if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
1962 line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001963 rb->lcd_drawrect(rx, ry, rw, rh);
1964 rb->lcd_drawline(x1, y1, x2, y2);
1965 rb->lcd_putsxy(0, 0, "failed 6");
1966 rb->lcd_update();
1967 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001968 testfailed = 6;
1969 }
1970
1971 /* test 7 */
1972 x1 = 8;
1973 y1 = 20;
1974 x2 = 35;
1975 y2 = 20;
1976 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1977 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001978 rb->lcd_drawrect(rx, ry, rw, rh);
1979 rb->lcd_drawline(x1, y1, x2, y2);
1980 rb->lcd_putsxy(0, 0, "failed 7");
1981 rb->lcd_update();
1982 rb->button_get(true);
Björn Stenberg50053d12002-09-09 23:30:16 +00001983 testfailed = 7;
1984 }
1985
1986 /* test 8 */
1987 x2 = 12;
1988 if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
1989 !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
Björn Stenbergba371fb2003-06-29 16:33:04 +00001990 rb->lcd_drawrect(rx, ry, rw, rh);