Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 Philipp Pertermann |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 21 | #include "plugin.h" |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 22 | #include "configfile.h" |
Peter D'Hoye | cb53e3c | 2007-08-15 12:42:09 +0000 | [diff] [blame] | 23 | #include "helper.h" |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 24 | |
Jens Arnold | a36b1d4 | 2006-01-15 18:20:18 +0000 | [diff] [blame] | 25 | PLUGIN_HEADER |
| 26 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 27 | /* size of the field the worm lives in */ |
| 28 | #define FIELD_RECT_X 1 |
| 29 | #define FIELD_RECT_Y 1 |
Daniel Stenberg | 17b3bea | 2002-09-27 07:13:02 +0000 | [diff] [blame] | 30 | #define FIELD_RECT_WIDTH (LCD_WIDTH - 45) |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 31 | #define FIELD_RECT_HEIGHT (LCD_HEIGHT - 2) |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 32 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 33 | /* when the game starts */ |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 34 | #define INITIAL_WORM_LENGTH 10 |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* num of pixel the worm grows per eaten food */ |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 37 | #define WORM_PER_FOOD 7 |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 38 | |
| 39 | /* num of worms creeping in the FIELD */ |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 40 | #define MAX_WORMS 3 |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 41 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 42 | /* minimal distance between a worm and an argh |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 43 | when a new argh is made */ |
| 44 | #define MIN_ARGH_DIST 5 |
| 45 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 46 | #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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 53 | #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 Arnold | 0e6dd7e | 2006-11-27 02:16:32 +0000 | [diff] [blame] | 58 | #if BUTTON_REMOTE != 0 |
| 59 | #define BTN_RC_UP BUTTON_RC_VOL_UP |
| 60 | #define BTN_RC_DOWN BUTTON_RC_VOL_DOWN |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 61 | #define REMOTE |
| 62 | #define MULTIPLAYER |
Jens Arnold | 0e6dd7e | 2006-11-27 02:16:32 +0000 | [diff] [blame] | 63 | #endif |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 64 | |
| 65 | #define PLAYERS_TEXT "UP/DN" |
| 66 | #define WORMS_TEXT "L/R" |
| 67 | #define KEY_CONTROL_TEXT "F1" |
| 68 | |
Dave Chapman | d64e626 | 2007-01-14 13:48:09 +0000 | [diff] [blame] | 69 | #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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 85 | #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 Roberts | 0b8766f | 2006-03-13 03:31:02 +0000 | [diff] [blame] | 90 | #define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL) |
| 91 | #define BTN_QUIT (BUTTON_OFF|BUTTON_REL) |
| 92 | #define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU) |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 93 | |
| 94 | #define PLAYERS_TEXT "UP/DN" |
| 95 | #define WORMS_TEXT "L/R" |
| 96 | |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 97 | #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \ |
| 98 | (CONFIG_KEYPAD == IPOD_1G2G_PAD) |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 99 | |
| 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 Roberts | 0b8766f | 2006-03-13 03:31:02 +0000 | [diff] [blame] | 111 | #elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD) |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 112 | |
| 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 Ferrare | f5c319b | 2006-06-30 17:03:01 +0000 | [diff] [blame] | 121 | #define BTN_RC_QUIT BUTTON_RC_STOP |
| 122 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 123 | #define PLAYERS_TEXT "Up/Down" |
| 124 | #define WORMS_TEXT "Left/Right" |
| 125 | |
Jens Arnold | 85a226d | 2007-03-16 23:02:39 +0000 | [diff] [blame] | 126 | #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD) |
Marcoen Hirschberg | 7db4270 | 2006-05-23 19:58:37 +0000 | [diff] [blame] | 127 | |
| 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 Hirschberg | 9ad02de | 2006-05-22 16:28:19 +0000 | [diff] [blame] | 139 | #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 Hirschberg | a7168fe | 2007-05-19 23:38:09 +0000 | [diff] [blame] | 146 | #define BTN_QUIT BUTTON_POWER |
| 147 | #define BTN_STOPRESET BUTTON_A |
Marcoen Hirschberg | 9ad02de | 2006-05-22 16:28:19 +0000 | [diff] [blame] | 148 | |
| 149 | #define PLAYERS_TEXT "Up/Down" |
| 150 | #define WORMS_TEXT "Left/Right" |
| 151 | |
Barry Wardell | 24f4a2a | 2006-10-26 13:38:09 +0000 | [diff] [blame] | 152 | |
Marianne Arnold | 12ddb8e | 2007-09-20 10:49:48 +0000 | [diff] [blame] | 153 | #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \ |
| 154 | (CONFIG_KEYPAD == SANSA_C200_PAD) |
Barry Wardell | 24f4a2a | 2006-10-26 13:38:09 +0000 | [diff] [blame] | 155 | |
| 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 Stenberg | 1e88be5 | 2006-08-03 20:17:25 +0000 | [diff] [blame] | 168 | #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 Robertson | 8215b34 | 2008-02-17 12:23:02 +0000 | [diff] [blame] | 181 | #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 Kukla | d6c8b57 | 2008-03-01 22:55:09 +0000 | [diff] [blame] | 194 | #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 Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 207 | #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 Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 220 | #elif (CONFIG_KEYPAD == COWOND2_PAD) |
| 221 | |
Rob Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 222 | #define BTN_QUIT BUTTON_POWER |
Rob Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 223 | |
Robert Kukla | d6c8b57 | 2008-03-01 22:55:09 +0000 | [diff] [blame] | 224 | #else |
| 225 | #error No keymap defined! |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 226 | #endif |
| 227 | |
Rob Purchase | 297e050 | 2008-04-27 15:30:19 +0000 | [diff] [blame] | 228 | #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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 260 | #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64) |
| 261 | #define FOOD_SIZE 3 |
| 262 | #define ARGH_SIZE 4 |
| 263 | #define SPEED 14 |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 264 | #define MAX_WORM_SEGMENTS 128 |
Marianne Arnold | 12ddb8e | 2007-09-20 10:49:48 +0000 | [diff] [blame] | 265 | #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 Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 270 | #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 Arnold | d3feb78 | 2006-03-30 17:29:21 +0000 | [diff] [blame] | 275 | #elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110) |
| 276 | #define FOOD_SIZE 4 |
| 277 | #define ARGH_SIZE 5 |
| 278 | #define SPEED 10 |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 279 | #define MAX_WORM_SEGMENTS 128 |
Tom Ross | 0edd86d | 2007-01-18 00:25:57 +0000 | [diff] [blame] | 280 | #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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 285 | #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128) |
| 286 | #define FOOD_SIZE 4 |
| 287 | #define ARGH_SIZE 5 |
| 288 | #define SPEED 8 |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 289 | #define MAX_WORM_SEGMENTS 256 |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 290 | #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132) |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 291 | #define FOOD_SIZE 4 |
| 292 | #define ARGH_SIZE 5 |
| 293 | #define SPEED 6 |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 294 | #define MAX_WORM_SEGMENTS 256 |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 295 | #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176) |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 296 | #define FOOD_SIZE 5 |
| 297 | #define ARGH_SIZE 6 |
| 298 | #define SPEED 4 |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 299 | #define MAX_WORM_SEGMENTS 512 |
Barry Wardell | 41ee9e3 | 2007-01-15 20:40:48 +0000 | [diff] [blame] | 300 | #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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 305 | #elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240) |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 306 | #define FOOD_SIZE 7 |
| 307 | #define ARGH_SIZE 8 |
| 308 | #define SPEED 4 |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 309 | #define MAX_WORM_SEGMENTS 512 |
Marcoen Hirschberg | 9ad02de | 2006-05-22 16:28:19 +0000 | [diff] [blame] | 310 | #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 Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 315 | #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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 323 | #endif |
| 324 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 325 | /** |
| 326 | * All the properties that a worm has. |
| 327 | */ |
Björn Stenberg | c3fd67c | 2002-12-18 14:57:45 +0000 | [diff] [blame] | 328 | static struct worm { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 329 | /* The worm is stored in a ring of xy coordinates */ |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 330 | int x[MAX_WORM_SEGMENTS]; |
| 331 | int y[MAX_WORM_SEGMENTS]; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 332 | |
| 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 344 | human_player1, human_player2, remote_player, virtual_player. |
| 345 | All these values are fuctions, that can change the direction |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 346 | 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 */ |
| 351 | static int highscore; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 352 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 353 | #define MAX_FOOD 5 /* maximal number of food items */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 354 | |
| 355 | /* The arrays store the food coordinates */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 356 | static int foodx[MAX_FOOD]; |
| 357 | static int foody[MAX_FOOD]; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 358 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 359 | #define MAX_ARGH 100 /* maximal number of argh items */ |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 360 | #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 361 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 362 | /* The arrays store the argh coordinates */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 363 | static int arghx[MAX_ARGH]; |
| 364 | static int arghy[MAX_ARGH]; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 365 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 366 | /* the number of arghs that are currently in use */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 367 | static int argh_count; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 368 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 369 | /* the number of arghs per food, settable by user */ |
| 370 | static int arghs_per_food = ARGHS_PER_FOOD; |
| 371 | /* the size of the argh, settable by user */ |
| 372 | static int argh_size = ARGH_SIZE; |
| 373 | /* the size of the food, settable by user */ |
| 374 | static int food_size = FOOD_SIZE; |
| 375 | /* the speed of the worm, settable by user */ |
| 376 | static int speed = SPEED; |
| 377 | /* the amount a worm grows by eating a food, settable by user */ |
| 378 | static int worm_food = WORM_PER_FOOD; |
| 379 | |
| 380 | /* End additional variables */ |
| 381 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 382 | #ifdef DEBUG_WORMLET |
| 383 | /* just a buffer used for debug output */ |
| 384 | static char debugout[15]; |
| 385 | #endif |
| 386 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 387 | /* the number of active worms (dead or alive) */ |
| 388 | static 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 */ |
| 392 | static bool use_remote = false; |
| 393 | |
| 394 | /* return values of check_collision */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 395 | #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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 401 | /* 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 411 | static int player1_dir = EAST; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 412 | /* direction of human player 2 */ |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 413 | static int player2_dir = EAST; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 414 | /* direction of human player 3 */ |
| 415 | static int player3_dir = EAST; |
| 416 | |
| 417 | /* the number of (human) players that currently |
| 418 | control a worm */ |
| 419 | static int players = 1; |
| 420 | |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 421 | /* the rockbox plugin api */ |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 422 | static const struct plugin_api* rb; |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 423 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 424 | #define SETTINGS_VERSION 1 |
| 425 | #define SETTINGS_MIN_VERSION 1 |
| 426 | #define SETTINGS_FILENAME "wormlet.cfg" |
| 427 | |
| 428 | static 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 441 | #ifdef DEBUG_WORMLET |
| 442 | static void set_debug_out(char *str){ |
| 443 | strcpy(debugout, str); |
| 444 | } |
| 445 | #endif |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 446 | |
| 447 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 448 | * Returns the direction id in which the worm |
| 449 | * currently is creeping. |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 450 | * @param struct worm *w The worm that is to be investigated. |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 451 | * w Must not be null. |
| 452 | * @return int A value 0 <= value < 4 |
| 453 | * Note the predefined constants NORTH, SOUTH, EAST, WEST |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 454 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 455 | static 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 479 | * dir must be 0 <= dir < 4. Use predefined constants |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 480 | * NORTH, SOUTH, EAST, WEST |
| 481 | */ |
| 482 | static 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 | */ |
| 508 | static int get_worm_array_length(struct worm *w) { |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 509 | /* initial simple calculation will be overwritten if wrong. */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 510 | int retVal = w->head - w->tail; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 511 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 512 | /* if the worm 'crosses' the boundaries of the ringbuffer */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 513 | if (retVal < 0) { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 514 | retVal = w->head + MAX_WORM_SEGMENTS - w->tail; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return retVal; |
| 518 | } |
| 519 | |
| 520 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 521 | * 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 | */ |
| 527 | static 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 559 | * horizontal or vertical (startx == endx or starty == endy). |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 560 | * @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 | */ |
| 570 | static 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 Linenberg | 380ca88 | 2002-09-17 12:39:47 +0000 | [diff] [blame] | 599 | if (simplemin <= simple && simple <= simplemax) { |
| 600 | if ((compmin <= compa && compa <= compmax) || |
| 601 | (compmin <= compb && compb <= compmax) || |
| 602 | (compa <= compmin && compb >= compmax)) { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 603 | retval = true; |
| 604 | } |
| 605 | } |
| 606 | return retval; |
| 607 | } |
| 608 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 609 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 610 | * 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 | */ |
| 618 | static 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 645 | * 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 653 | static bool specific_food_collision(int foodIndex, int x, int y) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 654 | bool retVal = false; |
| 655 | if (x >= foodx[foodIndex] && |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 656 | x < foodx[foodIndex] + food_size && |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 657 | y >= foody[foodIndex] && |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 658 | y < foody[foodIndex] + food_size) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 659 | |
| 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 671 | static int food_collision(int x, int y) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 672 | int i = 0; |
| 673 | int retVal = -1; |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 674 | for (i = 0; i < MAX_FOOD; i++) { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 675 | if (specific_food_collision(i, x, y)) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 676 | retVal = i; |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 677 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 678 | } |
| 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 692 | static bool specific_argh_collision(int arghIndex, int x, int y) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 693 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 694 | if ( x >= arghx[arghIndex] && |
| 695 | y >= arghy[arghIndex] && |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 696 | x < arghx[arghIndex] + argh_size && |
| 697 | y < arghy[arghIndex] + argh_size ) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 698 | { |
| 699 | return true; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 700 | } |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 701 | |
| 702 | return false; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 703 | } |
| 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 711 | * @return int -1 <= value < argh_count <= MAX_ARGH |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 712 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 713 | static int argh_collision(int x, int y) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 714 | int i = 0; |
| 715 | int retVal = -1; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 716 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 717 | /* search for the argh that has the specified coords */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 718 | for (i = 0; i < argh_count; i++) { |
| 719 | if (specific_argh_collision(i, x, y)) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 720 | retVal = i; |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 721 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 722 | } |
| 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 733 | static bool worm_food_collision(struct worm *w, int foodIndex) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 734 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 735 | bool retVal = false; |
| 736 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 737 | retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex], |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 738 | food_size - 1, food_size - 1); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 739 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 740 | return retVal; |
| 741 | } |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 742 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 743 | /** |
| 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 747 | * @param int argh_idx - The index of the argh |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 748 | * @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 | */ |
| 752 | static 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 757 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 758 | x2 = w->x[w->head] + moves * w->dirx; |
| 759 | y2 = w->y[w->head] + moves * w->diry; |
| 760 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 761 | retVal = line_in_rect(x1, y1, x2, y2, arghx[argh_idx], arghy[argh_idx], |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 762 | argh_size, argh_size); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 763 | return retVal; |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Checks wether the worm collides with the argh at the specfied argh-arrays. |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 768 | * @param int arghIndex The index of the argh in the arrays. |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 769 | * Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 770 | * @return Returns true if the worm collides with the specified argh. |
| 771 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 772 | static bool worm_argh_collision(struct worm *w, int arghIndex) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 773 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 774 | bool retVal = false; |
| 775 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 776 | retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex], |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 777 | argh_size - 1, argh_size - 1); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 778 | |
| 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 785 | * @param int index |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 786 | * Ensure that 0 <= index < MAX_FOOD. |
| 787 | */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 788 | static void make_food(int index) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 789 | |
| 790 | int x = 0; |
| 791 | int y = 0; |
| 792 | bool collisionDetected = false; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 793 | int i; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 794 | |
| 795 | do { |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 796 | /* make coordinates for a new food so that |
| 797 | the entire food lies within the FIELD */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 798 | x = rb->rand() % (FIELD_RECT_WIDTH - food_size); |
| 799 | y = rb->rand() % (FIELD_RECT_HEIGHT - food_size); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 800 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 801 | /* 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 806 | collisionDetected = |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 807 | food_collision(x , y ) >= 0 || |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 808 | 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 811 | argh_collision(x , y ) >= 0 || |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 812 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 815 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 816 | /* use coordinates for further testing */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 817 | foodx[index] = x; |
| 818 | foody[index] = y; |
| 819 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 820 | /* now test wether we accidently hit the worm with food ;) */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 821 | i = 0; |
| 822 | for (i = 0; i < worm_count && !collisionDetected; i++) { |
| 823 | collisionDetected |= worm_food_collision(&worms[i], index); |
| 824 | } |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 825 | } |
| 826 | while (collisionDetected); |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 827 | return; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Clears a food from the lcd buffer. |
| 832 | * @param int index The index of the food arrays under which |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 833 | * the coordinates of the desired food can be found. Ensure |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 834 | * that the value is 0 <= index <= MAX_FOOD. |
| 835 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 836 | static void clear_food(int index) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 837 | { |
| 838 | /* remove the old food from the screen */ |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 839 | rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); |
| 840 | rb->lcd_fillrect(foodx[index] + FIELD_RECT_X, |
| 841 | foody[index] + FIELD_RECT_Y, |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 842 | food_size, food_size); |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 843 | rb->lcd_set_drawmode(DRMODE_SOLID); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /** |
| 847 | * Draws a food in the lcd buffer. |
| 848 | * @param int index The index of the food arrays under which |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 849 | * the coordinates of the desired food can be found. Ensure |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 850 | * that the value is 0 <= index <= MAX_FOOD. |
| 851 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 852 | static void draw_food(int index) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 853 | { |
| 854 | /* draw the food object */ |
Zakk Roberts | e8a0506 | 2006-03-13 07:05:15 +0000 | [diff] [blame] | 855 | #ifdef HAVE_LCD_COLOR |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 856 | rb->lcd_set_foreground(COLOR_FOOD); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 857 | #endif |
| 858 | rb->lcd_fillrect(foodx[index] + FIELD_RECT_X, |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 859 | foody[index] + FIELD_RECT_Y, |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 860 | food_size, food_size); |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 861 | 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 Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 864 | food_size - 2, food_size - 2); |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 865 | rb->lcd_set_drawmode(DRMODE_SOLID); |
Zakk Roberts | e8a0506 | 2006-03-13 07:05:15 +0000 | [diff] [blame] | 866 | #ifdef HAVE_LCD_COLOR |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 867 | rb->lcd_set_foreground(COLOR_FG); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 868 | #endif |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 869 | } |
| 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 874 | * @param int index |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 875 | * Ensure that 0 <= index < argh_count < MAX_ARGH. |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 876 | */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 877 | static void make_argh(int index) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 878 | { |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 879 | int x = -1; |
| 880 | int y = -1; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 881 | bool collisionDetected = false; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 882 | int i; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 883 | |
| 884 | do { |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 885 | /* make coordinates for a new argh so that |
| 886 | the entire food lies within the FIELD */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 887 | x = rb->rand() % (FIELD_RECT_WIDTH - argh_size); |
| 888 | y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 889 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 890 | /* 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 895 | collisionDetected = |
| 896 | food_collision(x , y ) >= 0 || |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 897 | 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 900 | argh_collision(x , y ) >= 0 || |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 901 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 904 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 905 | /* use the candidate coordinates to make a real argh */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 906 | arghx[index] = x; |
| 907 | arghy[index] = y; |
| 908 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 909 | /* now test wether we accidently hit the worm with argh ;) */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 910 | for (i = 0; i < worm_count && !collisionDetected; i++) { |
| 911 | collisionDetected |= worm_argh_collision(&worms[i], index); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 912 | collisionDetected |= worm_argh_collision_in_moves(&worms[i], index, |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 913 | MIN_ARGH_DIST); |
| 914 | } |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 915 | } |
| 916 | while (collisionDetected); |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 917 | return; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Draws an argh in the lcd buffer. |
| 922 | * @param int index The index of the argh arrays under which |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 923 | * the coordinates of the desired argh can be found. Ensure |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 924 | * that the value is 0 <= index < argh_count <= MAX_ARGH. |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 925 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 926 | static void draw_argh(int index) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 927 | { |
| 928 | /* draw the new argh */ |
Zakk Roberts | e8a0506 | 2006-03-13 07:05:15 +0000 | [diff] [blame] | 929 | #ifdef HAVE_LCD_COLOR |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 930 | rb->lcd_set_foreground(COLOR_ARGH); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 931 | #endif |
| 932 | rb->lcd_fillrect(arghx[index] + FIELD_RECT_X, |
| 933 | arghy[index] + FIELD_RECT_Y, |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 934 | argh_size, argh_size); |
Zakk Roberts | e8a0506 | 2006-03-13 07:05:15 +0000 | [diff] [blame] | 935 | #ifdef HAVE_LCD_COLOR |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 936 | rb->lcd_set_foreground(COLOR_FG); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 937 | #endif |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 940 | static 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 | */ |
| 951 | static 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 971 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 972 | * Writes the direction that was stored for |
| 973 | * human player 1 into the specified worm. This function |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 974 | * may be used to be stored in worm.fetch_worm_direction. |
| 975 | * The value of |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 976 | * the direction is read from player1_dir. |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 977 | * @param struct worm *w - The worm of which the direction |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 978 | * is altered. |
| 979 | */ |
| 980 | static void human_player1(struct worm *w) { |
| 981 | set_worm_dir(w, player1_dir); |
| 982 | } |
| 983 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 984 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 985 | * Writes the direction that was stored for |
| 986 | * human player 2 into the specified worm. This function |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 987 | * may be used to be stored in worm.fetch_worm_direction. |
| 988 | * The value of |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 989 | * the direction is read from player2_dir. |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 990 | * @param struct worm *w - The worm of which the direction |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 991 | * is altered. |
| 992 | */ |
| 993 | static void human_player2(struct worm *w) { |
| 994 | set_worm_dir(w, player2_dir); |
| 995 | } |
| 996 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 997 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 998 | * Writes the direction that was stored for |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 999 | * human player using a remote control |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1000 | * into the specified worm. This function |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1001 | * may be used to be stored in worm.fetch_worm_direction. |
| 1002 | * The value of |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1003 | * the direction is read from player3_dir. |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1004 | * @param struct worm *w - The worm of which the direction |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1005 | * is altered. |
| 1006 | */ |
| 1007 | static void remote_player(struct worm *w) { |
| 1008 | set_worm_dir(w, player3_dir); |
| 1009 | } |
| 1010 | |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1011 | /** |
| 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1015 | static void init_wormlet(void) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1016 | { |
| 1017 | int i; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1018 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1019 | 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 Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1046 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1047 | /* Needed when the game is restarted using BTN_STOPRESET */ |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1048 | rb->lcd_clear_display(); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1049 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1050 | /* make and display some food and argh */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1051 | argh_count = MAX_FOOD; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1052 | for (i = 0; i < MAX_FOOD; i++) { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1053 | make_food(i); |
| 1054 | draw_food(i); |
| 1055 | make_argh(i); |
| 1056 | draw_argh(i); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1059 | /* draw the game field */ |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 1060 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1064 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1065 | /* make everything visible */ |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1066 | rb->lcd_update(); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1069 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1070 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1071 | * Move the worm one step further if it is alive. |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1072 | * The direction in which the worm moves is taken from dirx and diry. |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1073 | * 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1076 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1077 | static void move_worm(struct worm *w) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1078 | { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1079 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1086 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1087 | /* 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1097 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1098 | /* olddir == dir? |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1099 | a change of direction means a new segment |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1100 | has been opened */ |
| 1101 | if (olddirx != w->dirx || |
| 1102 | olddiry != w->diry) { |
| 1103 | w->head = (w->head + 1) % MAX_WORM_SEGMENTS; |
| 1104 | } |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1105 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1106 | /* new head position */ |
| 1107 | w->x[w->head] = headx + w->dirx; |
| 1108 | w->y[w->head] = heady + w->diry; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1109 | |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1110 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1111 | /* while the worm is growing no tail procession is necessary */ |
| 1112 | if (w->growing > 0) { |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1113 | /* update the worms grow state */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1114 | w->growing--; |
| 1115 | } |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1116 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1117 | /* if the worm isn't growing the tail has to be dragged */ |
| 1118 | else { |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1119 | /* index of the end of the tail segment */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1120 | int tail_segment_end = (w->tail + 1) % MAX_WORM_SEGMENTS; |
| 1121 | |
| 1122 | /* drag the end of the tail */ |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1123 | /* only one coordinate has to be altered. Here it is |
| 1124 | determined which one */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1125 | 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1139 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1140 | /* drop the last tail point */ |
| 1141 | w->tail = tail_segment_end; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /** |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1148 | * Draws the head and clears the tail of the worm in |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1149 | * the display buffer. lcd_update() is NOT called thus |
| 1150 | * the caller has to take care that the buffer is displayed. |
| 1151 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1152 | static void draw_worm(struct worm *w) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1153 | { |
Zakk Roberts | e8a0506 | 2006-03-13 07:05:15 +0000 | [diff] [blame] | 1154 | #ifdef HAVE_LCD_COLOR |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1155 | rb->lcd_set_foreground(COLOR_WORM); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1156 | #endif |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1157 | /* draw the new head */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1158 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1161 | rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1162 | } |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1163 | |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 1164 | rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); |
| 1165 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1166 | /* clear the space behind the worm */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1167 | 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 Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 1170 | rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1171 | } |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 1172 | rb->lcd_set_drawmode(DRMODE_SOLID); |
Zakk Roberts | e8a0506 | 2006-03-13 07:05:15 +0000 | [diff] [blame] | 1173 | #ifdef HAVE_LCD_COLOR |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1174 | rb->lcd_set_foreground(COLOR_FG); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1175 | #endif |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1179 | * Checks wether the coordinate is part of the worm. Returns |
| 1180 | * true if any part of the worm was hit - including the head. |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1181 | * @param x int The x coordinate |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1182 | * @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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1185 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1186 | static int specific_worm_collision(struct worm *w, int x, int y) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1187 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1188 | int retVal = -1; |
| 1189 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1190 | /* get_worm_array_length is expensive -> buffer the value */ |
| 1191 | int wormLength = get_worm_array_length(w); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1192 | int i; |
| 1193 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1194 | /* test each entry that is part of the worm */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1195 | for (i = 0; i < wormLength && retVal == -1; i++) { |
| 1196 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1197 | /* The iteration iterates the length of the worm. |
| 1198 | Here's the conversion to the true indices within the worm arrays. */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1199 | 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1209 | test = x; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1210 | } 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1229 | * Increases the length of the specified worm by marking |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1230 | * 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 | */ |
| 1236 | static 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1244 | * 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1246 | * (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 | */ |
| 1256 | static 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1266 | } |
| 1267 | return retVal; |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * Returns true if the head of the worm just has |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1272 | * crossed the field boundaries. |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1273 | * @return bool true if the worm just has wrapped. |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1274 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1275 | static bool field_collision(struct worm *w) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1276 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1277 | bool retVal = false; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1278 | 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 Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1282 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1283 | retVal = true; |
| 1284 | } |
| 1285 | return retVal; |
| 1286 | } |
| 1287 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1288 | |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1289 | /** |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1290 | * Returns true if the specified coordinates are within the |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1291 | * 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1294 | * @return bool Returns false if x,y specifies a point outside the |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1295 | * field of worms. |
| 1296 | */ |
| 1297 | static 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1306 | * is colliding with something currently. |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1307 | * @return int One of the values: |
| 1308 | * COLLISION_NONE |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1309 | * COLLISION_w |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1310 | * COLLISION_FOOD |
| 1311 | * COLLISION_ARGH |
| 1312 | * COLLISION_FIELD |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1313 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1314 | static int check_collision(struct worm *w) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1315 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1316 | int retVal = COLLISION_NONE; |
| 1317 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1318 | if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1319 | retVal = COLLISION_WORM; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1320 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1321 | if (food_collision(w->x[w->head], w->y[w->head]) >= 0) |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1322 | retVal = COLLISION_FOOD; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1323 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1324 | if (argh_collision(w->x[w->head], w->y[w->head]) >= 0) |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1325 | retVal = COLLISION_ARGH; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1326 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1327 | if (field_collision(w)) |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1328 | retVal = COLLISION_FIELD; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1329 | |
| 1330 | return retVal; |
| 1331 | } |
| 1332 | |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1333 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1334 | * Returns the index of the food that is closest to the point |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1335 | * specified by x, y. This index may be used in the foodx and |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1336 | * 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 | */ |
| 1341 | static 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1363 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1364 | * Returns wether the specified position is next to the worm |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1365 | * 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1367 | * 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 | */ |
| 1374 | static 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 | */ |
| 1394 | static 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1401 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1402 | if (!retVal) { |
| 1403 | retVal = (worm_collision(w, x, y) != NULL); |
| 1404 | } |
| 1405 | return retVal; |
| 1406 | } |
| 1407 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1408 | /** |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1409 | * This function |
| 1410 | * may be used to be stored in worm.fetch_worm_direction for |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1411 | * worms that are not controlled by humans but by artificial stupidity. |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1412 | * 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1415 | * @param struct worm *w - The worm of which the direction |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1416 | * is altered. |
| 1417 | */ |
| 1418 | static 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1476 | * about the game. |
| 1477 | */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1478 | static void score_board(void) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1479 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1480 | char buf[15]; |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1481 | char* buf2 = NULL; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1482 | int i; |
| 1483 | int y = 0; |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 1484 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1487 | for (i = 0; i < worm_count; i++) { |
| 1488 | int score = get_score(&worms[i]); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1489 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1490 | /* high score */ |
| 1491 | if (worms[i].fetch_worm_direction != virtual_player){ |
| 1492 | if (highscore < score) { |
| 1493 | highscore = score; |
| 1494 | } |
| 1495 | } |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1496 | |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1497 | /* length */ |
| 1498 | rb->snprintf(buf, sizeof (buf),"Len:%d", score); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1499 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1500 | /* worm state */ |
| 1501 | switch (check_collision(&worms[i])) { |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1502 | case COLLISION_NONE: |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1503 | if (worms[i].growing > 0) |
| 1504 | buf2 = "Growing"; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1505 | else { |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1506 | if (worms[i].alive) |
| 1507 | buf2 = "Hungry"; |
| 1508 | else |
| 1509 | buf2 = "Wormed"; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1510 | } |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1511 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1512 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1513 | case COLLISION_WORM: |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1514 | buf2 = "Wormed"; |
| 1515 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1516 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1517 | case COLLISION_FOOD: |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1518 | buf2 = "Growing"; |
| 1519 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1520 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1521 | case COLLISION_ARGH: |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1522 | buf2 = "Argh"; |
| 1523 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1524 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1525 | case COLLISION_FIELD: |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1526 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1531 | |
| 1532 | if (!worms[i].alive){ |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 1533 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1537 | } |
| 1538 | y += 19; |
| 1539 | } |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1540 | rb->snprintf(buf , sizeof(buf), "Hs: %d", highscore); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1541 | #ifndef DEBUG_WORMLET |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1542 | rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, buf); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1543 | #else |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1544 | rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1545 | #endif |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1546 | } |
| 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1554 | static bool process_collisions(struct worm *w) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1555 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1556 | int index = -1; |
| 1557 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1558 | w->alive &= !field_collision(w); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1559 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1560 | if (w->alive) { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1561 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1562 | /* check if food was eaten */ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1563 | index = food_collision(w->x[w->head], w->y[w->head]); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1564 | if (index != -1){ |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1565 | int i; |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1566 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1567 | clear_food(index); |
| 1568 | make_food(index); |
| 1569 | draw_food(index); |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1570 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1571 | for (i = 0; i < arghs_per_food; i++) { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1572 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1579 | add_growing(w, worm_food); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1580 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1581 | draw_worm(w); |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1584 | /* check if argh was eaten */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1585 | else { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1586 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1597 | return !w->alive; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1598 | } |
| 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 Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1605 | */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1606 | static int run(void) |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1607 | { |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1608 | int button = 0; |
| 1609 | int wormDead = false; |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1610 | bool paused = false; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1611 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1612 | /* 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1619 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1620 | /* initialize the board and so on */ |
| 1621 | init_wormlet(); |
| 1622 | |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1623 | cycle_start = *rb->current_tick; |
Björn Stenberg | e67958b | 2002-08-26 09:21:59 +0000 | [diff] [blame] | 1624 | /* change the direction of the worm */ |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1625 | while (!wormDead) |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1626 | { |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1627 | int i; |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1628 | long cycle_duration=0; |
| 1629 | |
Shachar Liberman | 5360df9 | 2006-07-30 03:10:09 +0000 | [diff] [blame] | 1630 | #ifdef HAS_BUTTON_HOLD |
| 1631 | if (rb->button_hold()) |
| 1632 | paused = true; |
| 1633 | #endif |
| 1634 | |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1635 | switch (button) { |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1636 | case BTN_STARTPAUSE: |
| 1637 | paused = !paused; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1638 | break; |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1639 | case BTN_STOPRESET: |
| 1640 | if (paused) |
| 1641 | return 1; /* restart game */ |
| 1642 | else |
| 1643 | paused = true; |
| 1644 | break; |
Kevin Ferrare | 0e027bd | 2006-06-30 16:43:47 +0000 | [diff] [blame] | 1645 | #ifdef BTN_RC_QUIT |
| 1646 | case BTN_RC_QUIT: |
| 1647 | #endif |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1648 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1660 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1661 | case BTN_DIR_DOWN: |
| 1662 | if (players == 1 && !use_remote) { |
| 1663 | player1_dir = SOUTH; |
| 1664 | } |
| 1665 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1666 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1667 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1674 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1675 | 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 Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1682 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1683 | #ifdef MULTIPLAYER |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1684 | case BTN_PLAYER2_DIR1: |
| 1685 | player2_dir = (player2_dir + 3) % 4; |
| 1686 | break; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1687 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1688 | case BTN_PLAYER2_DIR2: |
| 1689 | player2_dir = (player2_dir + 1) % 4; |
| 1690 | break; |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1691 | #endif |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1692 | |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1693 | #ifdef REMOTE |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1694 | case BTN_RC_UP: |
| 1695 | player3_dir = (player3_dir + 1) % 4; |
| 1696 | break; |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1697 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1698 | case BTN_RC_DOWN: |
| 1699 | player3_dir = (player3_dir + 3) % 4; |
| 1700 | break; |
Zakk Roberts | c091a77 | 2006-03-13 03:13:05 +0000 | [diff] [blame] | 1701 | #endif |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1702 | } |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1703 | |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1704 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1705 | for (i = 0; i < worm_count; i++) { |
| 1706 | worms[i].fetch_worm_direction(&worms[i]); |
| 1707 | } |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1708 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1709 | wormDead = true; |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1710 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1721 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1722 | /* 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1726 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1727 | /* 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1731 | |
| 1732 | |
| 1733 | #ifdef DEBUG_WORMLET |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1734 | ticks_to_max_cycle_reset--; |
| 1735 | if (ticks_to_max_cycle_reset <= 0) { |
| 1736 | max_cycle = 0; |
| 1737 | } |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1738 | |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1739 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1745 | #endif |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1746 | } |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1747 | /* 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 Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1750 | button = rb->button_get_w_tmo(speed - cycle_duration); |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1751 | cycle_start = *rb->current_tick; |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1752 | } |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1753 | |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 1754 | rb->splash(HZ*2, "Game Over!"); |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1755 | |
| 1756 | return 2; /* back to menu */ |
Eric Linenberg | b0a3984 | 2002-08-26 03:32:39 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1759 | #ifdef DEBUG_WORMLET |
| 1760 | |
| 1761 | /** |
| 1762 | * Just a test routine that checks that worm_food_collision works |
| 1763 | * in some typical situations. |
| 1764 | */ |
| 1765 | static void test_worm_food_collision(void) { |
| 1766 | int collision_count = 0; |
| 1767 | int i; |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1768 | rb->lcd_clear_display(); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1769 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1794 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); |
| 1795 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); |
| 1796 | rb->lcd_update(); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1797 | } |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1798 | if (collision_count != food_size) { |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1799 | rb->button_get(true); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1800 | } |
| 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1813 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); |
| 1814 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); |
| 1815 | rb->lcd_update(); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1816 | } |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1817 | if (collision_count != food_size * 2) { |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1818 | rb->button_get(true); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | } |
| 1822 | |
Eric Linenberg | 380ca88 | 2002-09-17 12:39:47 +0000 | [diff] [blame] | 1823 | static 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1836 | static void test_worm_argh_collision(void) { |
| 1837 | int i; |
| 1838 | int dir; |
| 1839 | int collision_count = 0; |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1840 | rb->lcd_clear_display(); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1841 | 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 Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1852 | for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1853 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1860 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); |
| 1861 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); |
| 1862 | rb->lcd_update(); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1863 | } |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1864 | if (collision_count != argh_size * 2) { |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1865 | rb->button_get(true); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | arghy[0] = 12; |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1869 | for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){ |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1870 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1877 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); |
| 1878 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); |
| 1879 | rb->lcd_update(); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1880 | } |
Zakk Roberts | 02e97d5 | 2006-05-22 06:56:39 +0000 | [diff] [blame] | 1881 | if (collision_count != argh_size * 4) { |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1882 | rb->button_get(true); |
Björn Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | static 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1902 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1907 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1914 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1919 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1926 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1931 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1938 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1943 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1950 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1955 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1963 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1968 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1978 | 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 Stenberg | 50053d1 | 2002-09-09 23:30:16 +0000 | [diff] [blame] | 1983 | 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 Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1990 | rb->lcd_drawrect(rx, ry, rw, rh); |
<