Bump plugin API version. This should have been done in r24587. Also, because the...
[kugel-rb.git] / apps / plugins / invadrox.c
blobfcd1c2d950bf99c41a44fcaf85bef82a4c5f00e0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Albert Veli
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* Improvised creds goes to:
24 * - Anders Clausen for ingeniously inventing the name Invadrox.
25 * - Linus Nielsen-Feltzing for patiently answering n00b questions.
28 #include "plugin.h"
29 #include "lib/highscore.h"
30 #include "lib/helper.h"
32 /* bitmaps */
33 #include "pluginbitmaps/invadrox_background.h"
35 /* get dimensions for later use from the bitmaps */
36 #include "pluginbitmaps/invadrox_aliens.h"
37 #include "pluginbitmaps/invadrox_ships.h"
38 #include "pluginbitmaps/invadrox_bombs.h"
39 #include "pluginbitmaps/invadrox_alien_explode.h"
40 #include "pluginbitmaps/invadrox_shield.h"
41 #include "pluginbitmaps/invadrox_ufo.h"
42 #include "pluginbitmaps/invadrox_ufo_explode.h"
43 #include "pluginbitmaps/invadrox_numbers.h"
44 #include "pluginbitmaps/invadrox_fire.h"
45 #define ALIEN_WIDTH (BMPWIDTH_invadrox_aliens/2)
46 #define ALIEN_HEIGHT (BMPHEIGHT_invadrox_aliens/3)
47 #define SHIP_WIDTH BMPWIDTH_invadrox_ships
48 #define SHIP_HEIGHT (BMPHEIGHT_invadrox_ships/3)
49 #define BOMB_WIDTH (BMPWIDTH_invadrox_bombs/3)
50 #define BOMB_HEIGHT (BMPHEIGHT_invadrox_bombs/6)
51 #define ALIEN_EXPLODE_WIDTH BMPWIDTH_invadrox_alien_explode
52 #define ALIEN_EXPLODE_HEIGHT BMPHEIGHT_invadrox_alien_explode
53 #define SHIELD_WIDTH BMPWIDTH_invadrox_shield
54 #define SHIELD_HEIGHT BMPHEIGHT_invadrox_shield
55 #define UFO_WIDTH BMPWIDTH_invadrox_ufo
56 #define UFO_HEIGHT BMPHEIGHT_invadrox_ufo
57 #define UFO_EXPLODE_WIDTH BMPWIDTH_invadrox_ufo_explode
58 #define UFO_EXPLODE_HEIGHT BMPHEIGHT_invadrox_ufo_explode
59 #define NUMBERS_WIDTH (BMPWIDTH_invadrox_numbers/10)
60 #define FONT_HEIGHT BMPHEIGHT_invadrox_numbers
61 #define FIRE_WIDTH BMPWIDTH_invadrox_fire
62 #define FIRE_HEIGHT BMPHEIGHT_invadrox_fire
64 PLUGIN_HEADER
66 /* Original graphics is only 1bpp so it should be portable
67 * to most targets. But for now, only support the simple ones.
69 #ifndef HAVE_LCD_BITMAP
70 #error INVADROX: Unsupported LCD
71 #endif
73 #if (LCD_DEPTH < 2)
74 #error INVADROX: Unsupported LCD
75 #endif
77 /* #define DEBUG */
78 #ifdef DEBUG
79 #define DBG(format, arg...) { DEBUGF("%s: " format, __FUNCTION__, ## arg); }
80 #else
81 #define DBG(format, arg...) {}
82 #endif
84 #if CONFIG_KEYPAD == IRIVER_H100_PAD
86 #define QUIT BUTTON_OFF
87 #define LEFT BUTTON_LEFT
88 #define RIGHT BUTTON_RIGHT
89 #define FIRE BUTTON_ON
91 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
93 #define QUIT BUTTON_OFF
94 #define LEFT BUTTON_LEFT
95 #define RIGHT BUTTON_RIGHT
96 #define FIRE BUTTON_SELECT
98 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
100 #define QUIT BUTTON_POWER
101 #define LEFT BUTTON_LEFT
102 #define RIGHT BUTTON_RIGHT
103 #define FIRE BUTTON_PLAY
105 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
106 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
107 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
109 #define QUIT BUTTON_MENU
110 #define LEFT BUTTON_LEFT
111 #define RIGHT BUTTON_RIGHT
112 #define FIRE BUTTON_SELECT
114 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
116 #define QUIT BUTTON_POWER
117 #define LEFT BUTTON_LEFT
118 #define RIGHT BUTTON_RIGHT
119 #define FIRE BUTTON_SELECT
121 #elif CONFIG_KEYPAD == GIGABEAT_PAD
123 #define QUIT BUTTON_POWER
124 #define LEFT BUTTON_LEFT
125 #define RIGHT BUTTON_RIGHT
126 #define FIRE BUTTON_SELECT
128 #elif CONFIG_KEYPAD == SANSA_E200_PAD
130 #define QUIT BUTTON_POWER
131 #define LEFT BUTTON_LEFT
132 #define RIGHT BUTTON_RIGHT
133 #define FIRE BUTTON_SELECT
135 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
137 #define QUIT (BUTTON_HOME|BUTTON_REPEAT)
138 #define LEFT BUTTON_LEFT
139 #define RIGHT BUTTON_RIGHT
140 #define FIRE BUTTON_SELECT
142 #elif CONFIG_KEYPAD == TATUNG_TPJ1022_PAD
144 /* TODO: Figure out which buttons to use for Tatung Elio TPJ-1022 */
145 #define QUIT BUTTON_AB
146 #define LEFT BUTTON_LEFT
147 #define RIGHT BUTTON_RIGHT
148 #define FIRE BUTTON_MENU
150 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
152 #define QUIT BUTTON_BACK
153 #define LEFT BUTTON_LEFT
154 #define RIGHT BUTTON_RIGHT
155 #define FIRE BUTTON_SELECT
157 #elif CONFIG_KEYPAD == COWON_D2_PAD
159 #define QUIT BUTTON_POWER
160 #define LEFT BUTTON_MINUS
161 #define RIGHT BUTTON_PLUS
162 #define FIRE BUTTON_MENU
164 #elif CONFIG_KEYPAD == IAUDIO67_PAD
166 #define QUIT BUTTON_POWER
167 #define LEFT BUTTON_LEFT
168 #define RIGHT BUTTON_RIGHT
169 #define FIRE BUTTON_PLAY
171 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
173 #define QUIT BUTTON_BACK
174 #define LEFT BUTTON_LEFT
175 #define RIGHT BUTTON_RIGHT
176 #define FIRE BUTTON_SELECT
178 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
180 #define QUIT BUTTON_POWER
181 #define LEFT BUTTON_PREV
182 #define RIGHT BUTTON_NEXT
183 #define FIRE BUTTON_PLAY
185 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
186 CONFIG_KEYPAD == ONDAVX777_PAD || \
187 CONFIG_KEYPAD == MROBE500_PAD
189 #define QUIT BUTTON_POWER
191 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
193 #define QUIT BUTTON_REC
194 #define LEFT BUTTON_LEFT
195 #define RIGHT BUTTON_RIGHT
196 #define FIRE BUTTON_PLAY
198 #else
199 #error INVADROX: Unsupported keypad
200 #endif
202 #ifndef RC_QUIT
203 #define RC_QUIT 0
204 #endif
206 #ifdef HAVE_TOUCHSCREEN
208 #ifndef QUIT
209 #define QUIT 0
210 #endif
211 #ifndef LEFT
212 #define LEFT 0
213 #endif
214 #ifndef RIGHT
215 #define RIGHT 0
216 #endif
217 #ifndef FIRE
218 #define FIRE 0
219 #endif
221 #define TOUCHSCREEN_QUIT BUTTON_TOPLEFT
222 #define TOUCHSCREEN_LEFT (BUTTON_MIDLEFT | BUTTON_BOTTOMLEFT)
223 #define TOUCHSCREEN_RIGHT (BUTTON_MIDRIGHT | BUTTON_BOTTOMRIGHT)
224 #define TOUCHSCREEN_FIRE (BUTTON_CENTER | BUTTON_BOTTOMMIDDLE)
226 #define ACTION_QUIT (QUIT | TOUCHSCREEN_QUIT | RC_QUIT)
227 #define ACTION_LEFT (LEFT | TOUCHSCREEN_LEFT)
228 #define ACTION_RIGHT (RIGHT | TOUCHSCREEN_RIGHT)
229 #define ACTION_FIRE (FIRE | TOUCHSCREEN_FIRE)
231 #else /* HAVE_TOUCHSCREEN */
233 #define ACTION_QUIT (QUIT | RC_QUIT)
234 #define ACTION_LEFT LEFT
235 #define ACTION_RIGHT RIGHT
236 #define ACTION_FIRE FIRE
238 #endif
240 #ifndef UNUSED
241 #define UNUSED __attribute__ ((unused))
242 #endif
244 /* Defines common to all models */
245 #define UFO_Y (SCORENUM_Y + FONT_HEIGHT + ALIEN_HEIGHT)
246 #define PLAYFIELD_Y (LCD_HEIGHT - SHIP_HEIGHT - 2)
247 #define PLAYFIELD_WIDTH (LCD_WIDTH - 2 * PLAYFIELD_X)
248 #define LEVEL_X (LCD_WIDTH - PLAYFIELD_X - LIVES_X - 2 * NUMBERS_WIDTH - 3 * NUM_SPACING)
249 #define SHIP_MIN_X (PLAYFIELD_X + PLAYFIELD_WIDTH / 5 - SHIELD_WIDTH / 2 - SHIP_WIDTH)
250 #define SHIP_MAX_X (PLAYFIELD_X + 4 * PLAYFIELD_WIDTH / 5 + SHIELD_WIDTH / 2)
251 /* SCORE_Y = 0 for most targets. Gigabeat redefines it later. */
252 #define SCORE_Y 0
253 #define MAX_LIVES 8
256 /* m:robe 500 defines */
257 #if ((LCD_WIDTH == 640) && (LCD_HEIGHT == 480)) || \
258 ((LCD_WIDTH == 480) && (LCD_HEIGHT == 640))
260 /* Original arcade game size 224x240, 1bpp with
261 * red overlay at top and green overlay at bottom.
263 * M:Robe 500: 640x480x16
264 * ======================
267 #define ARCADISH_GRAPHICS
268 #define PLAYFIELD_X 48
269 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
270 #define ALIEN_START_Y (UFO_Y + ALIEN_HEIGHT)
271 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
272 #define SCORENUM_Y (SCORE_Y + FONT_HEIGHT + 2)
273 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
274 #define SHIELD_Y (PLAYFIELD_Y - 5 * SHIP_HEIGHT)
275 #define LIVES_X 10
276 #define MAX_Y 18
278 /* iPod Video defines */
279 #elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240)
281 /* Original arcade game size 224x240, 1bpp with
282 * red overlay at top and green overlay at bottom.
284 * iPod Video: 320x240x16
285 * ======================
286 * X: 48p padding at left/right gives 224p playfield in middle.
287 * 10p "border" gives 204p actual playfield. UFO use full 224p.
288 * Y: Use full 240p.
290 * MAX_X = (204 - 12) / 2 - 1 = 95
292 * Y: Score text 7 0
293 * Space 10 7
294 * Score 7 17
295 * Space 8 24
296 * 3 Ufo 7 32
297 * 2 Space Aliens start at 32 + 3 * 8 = 56
298 * 0 aliens 9*8 56 -
299 * space ~7*8 128 | 18.75 aliens space between
300 * shield 2*8 182 | first alien and ship.
301 * space 8 198 | MAX_Y = 18
302 * ship 8 206 -
303 * space 2*8 214
304 * hline 1 230 - PLAYFIELD_Y
305 * bottom border 10 240
306 * Lives and Level goes inside bottom border
309 #define ARCADISH_GRAPHICS
310 #define PLAYFIELD_X 48
311 #define SHIP_Y (PLAYFIELD_Y - 3 * SHIP_HEIGHT)
312 #define ALIEN_START_Y (UFO_Y + 3 * ALIEN_HEIGHT)
313 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
314 #define SCORENUM_Y SCORE_Y + (2 * (FONT_HEIGHT + 1) + 1)
315 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
316 #define SHIELD_Y (PLAYFIELD_Y - 6 * SHIP_HEIGHT)
317 #define LIVES_X 10
318 #define MAX_Y 18
320 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
322 /* Sandisk Sansa e200: 176x220x16
323 * ==============================
324 * X: No padding. 8p border -> 160p playfield.
326 * 8p Aliens with 3p spacing -> 88 + 30 = 118p aliens block.
327 * (160 - 118) / 2 = 21 rounds for whole block (more than original)
328 * MAX_X = (160 - 8) / 2 - 1 = 75 rounds for single alien (less than original)
330 * LOGO 70 0
331 * Score text 5 70
332 * Space 5 75
333 * Y Score 5 80
334 * Space 10 85
335 * 2 Ufo 5 95
336 * 2 Space 10 100
337 * 0 aliens 9*5 110 -
338 * space ~7*5 155 | 18.6 aliens space between
339 * shield 2*5 188 | first alien and ship.
340 * space 5 198 | MAX_Y = 18
341 * ship 5 203 -
342 * space 5 208
343 * hline 1 213 PLAYFIELD_Y
344 * bottom border 6
345 * LCD_HEIGHT 220
346 * Lives and Level goes inside bottom border
349 #define SMALL_GRAPHICS
350 #define PLAYFIELD_X 0
351 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
352 #define SHIELD_Y (SHIP_Y - SHIP_HEIGHT - SHIELD_HEIGHT)
353 #define ALIEN_START_Y (UFO_Y + 3 * SHIP_HEIGHT)
354 /* Redefine SCORE_Y */
355 #undef SCORE_Y
356 #define SCORE_Y 70
357 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
358 #define SCORENUM_Y (SCORE_Y + 2 * FONT_HEIGHT)
359 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
360 #define LIVES_X 8
361 #define MAX_Y 18
364 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
366 /* iPod Nano: 176x132x16
367 * ======================
368 * X: No padding. 8p border -> 160p playfield.
370 * LIVES_X 8
371 * ALIEN_WIDTH 8
372 * ALIEN_HEIGHT 5
373 * ALIEN_SPACING 3
374 * SHIP_WIDTH 10
375 * SHIP_HEIGHT 5
376 * FONT_HEIGHT 5
377 * UFO_WIDTH 10
378 * UFO_HEIGHT 5
379 * SHIELD_WIDTH 15
380 * SHIELD_HEIGHT 10
381 * MAX_X 75
382 * MAX_Y = 18
383 * ALIEN_START_Y (UFO_Y + 12)
385 * 8p Aliens with 3p spacing -> 88 + 30 = 118p aliens block.
386 * (160 - 118) / 2 = 21 rounds for whole block (more than original)
387 * MAX_X = (160 - 8) / 2 - 1 = 75 rounds for single alien (less than original)
389 * Y: Scoreline 5 0 (combine scoretext and numbers on same line)
390 * Space 5 5
391 * 1 Ufo 5 10
392 * 3 Space 7 15
393 * 2 aliens 9*5 22 -
394 * space ~7*5 67 | Just above 18 aliens space between
395 * shield 2*5 100 | first alien and ship.
396 * space 5 110 | MAX_Y = 18
397 * ship 5 115 -
398 * space 5 120
399 * hline 1 125 PLAYFIELD_Y
400 * bottom border 6 126
401 * LCD_HEIGHT 131
402 * Lives and Level goes inside bottom border
405 #define SMALL_GRAPHICS
406 #define PLAYFIELD_X 0
407 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
408 #define ALIEN_START_Y (UFO_Y + 12)
409 #define SCORENUM_X (PLAYFIELD_X + 6 * NUMBERS_WIDTH + 5 * NUM_SPACING)
410 #define SCORENUM_Y SCORE_Y
411 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 4 * NUMBERS_WIDTH - 3 * NUM_SPACING)
412 #define SHIELD_Y (SHIP_Y - SHIP_HEIGHT - SHIELD_HEIGHT)
413 #define LIVES_X 8
414 #define MAX_Y 18
416 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
418 /* iAudio X5, iRiver H10 20Gb, iPod 3g/4g, H100, M5: 160x128
419 * =========================================================
420 * X: No padding. No border -> 160p playfield.
422 * LIVES_X 0
423 * ALIEN_WIDTH 8
424 * ALIEN_HEIGHT 5
425 * ALIEN_SPACING 3
426 * SHIP_WIDTH 10
427 * SHIP_HEIGHT 5
428 * FONT_HEIGHT 5
429 * UFO_WIDTH 10
430 * UFO_HEIGHT 5
431 * SHIELD_WIDTH 15
432 * SHIELD_HEIGHT 10
433 * MAX_X 75
434 * MAX_Y = 18
435 * ALIEN_START_Y (UFO_Y + 10)
437 * 8p Aliens with 3p spacing -> 88 + 30 = 118p aliens block.
438 * (160 - 118) / 2 = 21 rounds for whole block (more than original)
439 * MAX_X = (160 - 8) / 2 - 1 = 75 rounds for single alien (less than original)
441 * Y: Scoreline 5 0 (combine scoretext and numbers on same line)
442 * Space 5 5
443 * 1 Ufo 5 10
444 * 2 Space 5 15
445 * 8 aliens 9*5 20 -
446 * space ~6*5 65 | Just above 18 aliens space between
447 * shield 2*5 96 | first alien and ship.
448 * space 5 106 | MAX_Y = 18
449 * ship 5 111 -
450 * space 5 116
451 * hline 1 121 PLAYFIELD_Y
452 * bottom border 6 122
453 * LCD_HEIGHT 128
454 * Lives and Level goes inside bottom border
457 #define SMALL_GRAPHICS
458 #define PLAYFIELD_X 0
459 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
460 #define ALIEN_START_Y (UFO_Y + 10)
461 #define SCORENUM_X (PLAYFIELD_X + 6 * NUMBERS_WIDTH + 5 * NUM_SPACING)
462 #define SCORENUM_Y SCORE_Y
463 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 4 * NUMBERS_WIDTH - 3 * NUM_SPACING)
464 #define SHIELD_Y (SHIP_Y - SHIP_HEIGHT - SHIELD_HEIGHT)
465 #define LIVES_X 0
466 #define MAX_Y 18
469 #elif (LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400))
471 /* Gigabeat: 240x320x16
472 * ======================
473 * X: 8p padding at left/right gives 224p playfield in middle.
474 * 10p "border" gives 204p actual playfield. UFO use full 224p.
475 * Y: Use bottom 240p for playfield and top 80 pixels for logo.
477 * MAX_X = (204 - 12) / 2 - 1 = 95
479 * Y: Score text 7 0 + 80
480 * Space 10 7 + 80
481 * Score 7 17 + 80
482 * Space 8 24 + 80
483 * 3 Ufo 7 32 + 80
484 * 2 Space Aliens start at 32 + 3 * 8 = 56
485 * 0 aliens 9*8 56 -
486 * space ~7*8 128 | 18.75 aliens space between
487 * shield 2*8 182 | first alien and ship.
488 * space 8 198 | MAX_Y = 18
489 * ship 8 206 -
490 * space 2*8 214
491 * hline 1 230 310 - PLAYFIELD_Y
492 * bottom border 10 240 320
493 * Lives and Level goes inside bottom border
496 #define ARCADISH_GRAPHICS
497 #define PLAYFIELD_X 8
498 #define SHIP_Y (PLAYFIELD_Y - 3 * SHIP_HEIGHT)
499 #define ALIEN_START_Y (UFO_Y + 3 * ALIEN_HEIGHT)
500 /* Redefine SCORE_Y */
501 #undef SCORE_Y
502 #define SCORE_Y 80
503 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
504 #define SCORENUM_Y SCORE_Y + (2 * (FONT_HEIGHT + 1) + 1)
505 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
506 #define SHIELD_Y (PLAYFIELD_Y - 6 * SHIP_HEIGHT)
507 #define LIVES_X 10
508 #define MAX_Y 18
510 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
512 /* TPJ1022, H300, iPod Color: 220x176x16
513 * ============================
514 * X: 0p padding at left/right gives 220p playfield in middle.
515 * 8p "border" gives 204p actual playfield. UFO use full 220p.
516 * Y: Use full 176p for playfield.
518 * MAX_X = (204 - 12) / 2 - 1 = 95
520 * Y: Score text 7 0
521 * Space 8 7
522 * 1 Ufo 7 15
523 * 7 Space Aliens start at 15 + 3 * 8 = 56
524 * 6 aliens 9*8 25 -
525 * space ~7*8 103 | 15.6 aliens space between
526 * shield 2*8 126 | first alien and ship.
527 * space 8 142 | MAX_Y = 15
528 * ship 8 150 -
529 * space 8 158
530 * hline 1 166 - PLAYFIELD_Y
531 * bottom border 10 176
532 * Lives and Level goes inside bottom border
535 #define ARCADISH_GRAPHICS
536 #define PLAYFIELD_X 0
537 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
538 #define ALIEN_START_Y (UFO_Y + 10)
539 #define SCORENUM_Y SCORE_Y
540 #define SCORENUM_X (PLAYFIELD_X + 6 * NUMBERS_WIDTH + 6 * NUM_SPACING)
541 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 4 * NUMBERS_WIDTH - 3 * NUM_SPACING)
542 #define SHIELD_Y (PLAYFIELD_Y - 5 * SHIP_HEIGHT)
543 #define LIVES_X 8
544 #define MAX_Y 15
547 #else
548 #error INVADROX: Unsupported LCD type
549 #endif
551 #define MAX_X ((LCD_WIDTH-LIVES_X*2-PLAYFIELD_X*2 - ALIEN_WIDTH)/2 - 1)
553 /* Defines common to each "graphic type" */
554 #ifdef ARCADISH_GRAPHICS
556 #define SHOT_HEIGHT 5
557 #define ALIEN_SPACING 4
558 #define ALIEN_SPEED 2
559 #define UFO_SPEED 1
560 #define NUM_SPACING 3
561 #define FIRE_SPEED 8
562 #define BOMB_SPEED 3
563 #define ALIENS 11
565 #elif defined SMALL_GRAPHICS
567 #define SHOT_HEIGHT 4
568 #define ALIEN_SPACING 3
569 #define ALIEN_SPEED 2
570 #define UFO_SPEED 1
571 #define NUM_SPACING 2
572 #define FIRE_SPEED 6
573 #define BOMB_SPEED 2
574 #define ALIENS 11
576 #else
577 #error Graphic type not defined
578 #endif
581 /* Colors */
582 #if (LCD_DEPTH >= 8)
583 #define SLIME_GREEN LCD_RGBPACK(31, 254, 31)
584 #define UFO_RED LCD_RGBPACK(254, 31, 31)
585 #elif (LCD_DEPTH == 2)
586 #define SLIME_GREEN LCD_LIGHTGRAY
587 #define UFO_RED LCD_LIGHTGRAY
588 #else
589 #error LCD type not implemented yet
590 #endif
592 /* Alien states */
593 #define DEAD 0
594 #define ALIVE 1
595 #define BOMBER 2
597 /* Fire/bomb/ufo states */
598 #define S_IDLE 0
599 #define S_ACTIVE 1
600 #define S_SHOWSCORE 2
601 #define S_EXPLODE -9
603 /* Fire/bomb targets */
604 #define TARGET_TOP 0
605 #define TARGET_SHIELD 1
606 #define TARGET_SHIP 2
607 #define TARGET_BOTTOM 3
608 #define TARGET_UFO 4
610 #define HISCOREFILE PLUGIN_GAMES_DIR "/invadrox.high"
613 /* The time (in ms) for one iteration through the game loop - decrease this
614 * to speed up the game - note that current_tick is (currently) only accurate
615 * to 10ms.
617 #define CYCLETIME 40
620 /* Physical x is at PLAYFIELD_X + LIVES_X + x * ALIEN_SPEED
621 * Physical y is at y * ALIEN_HEIGHT
623 struct alien {
624 int x; /* x-coordinate (0 - 95) */
625 int y; /* y-coordinate (0 - 18) */
626 unsigned char type; /* 0 (Kang), 1 (Kodos), 2 (Serak) */
627 unsigned char state; /* Dead, alive or bomber */
630 /* Aliens box 5 rows * ALIENS aliens in each row */
631 struct alien aliens[5 * ALIENS];
633 #define MAX_BOMBS 4
634 struct bomb {
635 int x, y;
636 unsigned char type;
637 unsigned char frame; /* Current animation frame */
638 unsigned char frames; /* Number of frames in animation */
639 unsigned char target; /* Remember target during explosion frames */
640 int state; /* 0 (IDLE) = inactive, 1 (FIRE) or negative, exploding */
642 struct bomb bombs[MAX_BOMBS];
643 /* Increase max_bombs at higher levels */
644 int max_bombs;
646 /* Raw framebuffer value of shield/ship green color */
647 fb_data screen_green, screen_white;
649 /* For optimization, precalculate startoffset of each scanline */
650 unsigned int ytab[LCD_HEIGHT];
652 int lives = 2;
653 int score = 0;
654 int scores[3] = { 30, 20, 10 };
655 int level = 0;
656 struct highscore hiscore;
657 bool game_over = false;
658 int ship_x, old_ship_x, ship_dir, ship_acc, max_ship_speed;
659 int ship_frame, ship_frame_counter;
660 bool ship_hit;
661 int fire, fire_target, fire_x, fire_y;
662 int curr_alien, aliens_paralyzed, gamespeed;
663 int ufo_state, ufo_x;
664 bool level_finished;
665 bool aliens_down, aliens_right, hit_left_border, hit_right_border;
668 /* No standard get_pixel function yet, use this hack instead */
669 #if (LCD_DEPTH >= 8)
671 #if defined(LCD_STRIDEFORMAT) && LCD_STRIDEFORMAT == VERTICAL_STRIDE
672 inline fb_data get_pixel(int x, int y)
674 return rb->lcd_framebuffer[x*LCD_HEIGHT+y];
676 #else
677 inline fb_data get_pixel(int x, int y)
679 return rb->lcd_framebuffer[ytab[y] + x];
681 #endif
683 #elif (LCD_DEPTH == 2)
685 #if (LCD_PIXELFORMAT == HORIZONTAL_PACKING)
686 static const unsigned char shifts[4] = {
687 6, 4, 2, 0
689 /* Horizontal packing */
690 inline fb_data get_pixel(int x, int y)
692 return (rb->lcd_framebuffer[ytab[y] + (x >> 2)] >> shifts[x & 3]) & 3;
694 #else
695 /* Vertical packing */
696 static const unsigned char shifts[4] = {
697 0, 2, 4, 6
699 inline fb_data get_pixel(int x, int y)
701 return (rb->lcd_framebuffer[ytab[y] + x] >> shifts[y & 3]) & 3;
703 #endif /* Horizontal/Vertical packing */
705 #else
706 #error get_pixel: pixelformat not implemented yet
707 #endif
710 /* Draw "digits" least significant digits of num at (x,y) */
711 void draw_number(int x, int y, int num, int digits)
713 int i;
714 int d;
716 for (i = digits - 1; i >= 0; i--) {
717 d = num % 10;
718 num = num / 10;
719 rb->lcd_bitmap_part(invadrox_numbers, d * NUMBERS_WIDTH, 0,
720 STRIDE( SCREEN_MAIN,
721 BMPWIDTH_invadrox_numbers,
722 BMPHEIGHT_invadrox_numbers),
723 x + i * (NUMBERS_WIDTH + NUM_SPACING), y,
724 NUMBERS_WIDTH, FONT_HEIGHT);
726 /* Update lcd */
727 rb->lcd_update_rect(x, y, 4 * NUMBERS_WIDTH + 3 * NUM_SPACING, FONT_HEIGHT);
731 inline void draw_score(void)
733 draw_number(SCORENUM_X, SCORENUM_Y, score, 4);
734 if (score > hiscore.score) {
735 /* Draw new hiscore (same as score) */
736 draw_number(HISCORENUM_X, SCORENUM_Y, score, 4);
741 void draw_level(void)
743 draw_number(LEVEL_X + 2 * NUM_SPACING, PLAYFIELD_Y + 2, level, 2);
747 void draw_lives(void)
749 int i;
750 /* Lives num */
751 rb->lcd_bitmap_part(invadrox_numbers, lives * NUMBERS_WIDTH, 0,
752 STRIDE( SCREEN_MAIN,
753 BMPWIDTH_invadrox_numbers,
754 BMPHEIGHT_invadrox_numbers),
755 PLAYFIELD_X + LIVES_X, PLAYFIELD_Y + 2,
756 NUMBERS_WIDTH, FONT_HEIGHT);
758 /* Ships */
759 for (i = 0; i < (lives - 1); i++) {
760 rb->lcd_bitmap_part(invadrox_ships, 0, 0,
761 STRIDE( SCREEN_MAIN,
762 BMPWIDTH_invadrox_ships,
763 BMPHEIGHT_invadrox_ships),
764 PLAYFIELD_X + LIVES_X + SHIP_WIDTH + i * (SHIP_WIDTH + NUM_SPACING),
765 PLAYFIELD_Y + 1, SHIP_WIDTH, SHIP_HEIGHT);
768 /* Erase ship to the right (if less than MAX_LIVES) */
769 if (lives < MAX_LIVES) {
770 rb->lcd_fillrect(PLAYFIELD_X + LIVES_X + SHIP_WIDTH + i * (SHIP_WIDTH + NUM_SPACING),
771 PLAYFIELD_Y + 1, SHIP_WIDTH, SHIP_HEIGHT);
773 /* Update lives (and level) part of screen */
774 rb->lcd_update_rect(PLAYFIELD_X + LIVES_X, PLAYFIELD_Y + 1,
775 PLAYFIELD_WIDTH - 2 * LIVES_X, MAX(FONT_HEIGHT + 1, SHIP_HEIGHT + 1));
779 inline void draw_aliens(void)
781 int i;
783 for (i = 0; i < 5 * ALIENS; i++) {
784 rb->lcd_bitmap_part(invadrox_aliens, aliens[i].x & 1 ? ALIEN_WIDTH : 0,
785 aliens[i].type * ALIEN_HEIGHT,
786 STRIDE( SCREEN_MAIN,
787 BMPWIDTH_invadrox_aliens,
788 BMPHEIGHT_invadrox_aliens),
789 PLAYFIELD_X + LIVES_X + aliens[i].x * ALIEN_SPEED,
790 ALIEN_START_Y + aliens[i].y * ALIEN_HEIGHT,
791 ALIEN_WIDTH, ALIEN_HEIGHT);
796 /* Return false if there is no next alive alien (round is over) */
797 inline bool next_alien(void)
799 bool ret = true;
801 do {
802 curr_alien++;
803 if (curr_alien % ALIENS == 0) {
804 /* End of this row. Move up one row. */
805 curr_alien -= 2 * ALIENS;
806 if (curr_alien < 0) {
807 /* No more aliens in this round. */
808 curr_alien = 4 * ALIENS;
809 ret = false;
812 } while (aliens[curr_alien].state == DEAD && ret);
814 if (!ret) {
815 /* No more alive aliens. Round finished. */
816 if (hit_right_border) {
817 if (hit_left_border) {
818 DBG("ERROR: both left and right borders are set (%d)\n", curr_alien);
820 /* Move down-left next round */
821 aliens_right = false;
822 aliens_down = true;
823 hit_right_border = false;
824 } else if (hit_left_border) {
825 /* Move down-right next round */
826 aliens_right = true;
827 aliens_down = true;
828 hit_left_border = false;
829 } else {
830 /* Not left nor right. Set down to false. */
831 aliens_down = false;
835 return ret;
839 /* All aliens have been moved.
840 * Set curr_alien to first alive.
841 * Return false if no-one is left alive.
843 bool first_alien(void)
845 int i, y;
847 for (y = 4; y >= 0; y--) {
848 for (i = y * ALIENS; i < (y + 1) * ALIENS; i++) {
849 if (aliens[i].state != DEAD) {
850 curr_alien = i;
851 return true;
856 /* All aliens dead. */
857 level_finished = true;
859 return false;
863 bool move_aliens(void)
865 int x, y, old_x, old_y;
867 /* Move current alien (curr_alien is pointing to a living alien) */
869 old_x = aliens[curr_alien].x;
870 old_y = aliens[curr_alien].y;
872 if (aliens_down) {
873 aliens[curr_alien].y++;
874 if (aliens[curr_alien].y == MAX_Y) {
875 /* Alien is at bottom. Game Over. */
876 DBG("Alien %d is at bottom. Game Over.\n", curr_alien);
877 game_over = true;
878 return false;
882 if (aliens_right) {
883 /* Moving right */
884 if (aliens[curr_alien].x < MAX_X) {
885 aliens[curr_alien].x++;
888 /* Now, after move, check if we hit the right border. */
889 if (aliens[curr_alien].x == MAX_X) {
890 hit_right_border = true;
893 } else {
894 /* Moving left */
895 if (aliens[curr_alien].x > 0) {
896 aliens[curr_alien].x--;
899 /* Now, after move, check if we hit the left border. */
900 if (aliens[curr_alien].x == 0) {
901 hit_left_border = true;
905 /* Erase old position */
906 x = PLAYFIELD_X + LIVES_X + old_x * ALIEN_SPEED;
907 y = ALIEN_START_Y + old_y * ALIEN_HEIGHT;
908 if (aliens[curr_alien].y != old_y) {
909 /* Moved in y-dir. Erase whole alien. */
910 rb->lcd_fillrect(x, y, ALIEN_WIDTH, ALIEN_HEIGHT);
911 } else {
912 if (aliens_right) {
913 /* Erase left edge */
914 rb->lcd_fillrect(x, y, ALIEN_SPEED, ALIEN_HEIGHT);
915 } else {
916 /* Erase right edge */
917 x += ALIEN_WIDTH - ALIEN_SPEED;
918 rb->lcd_fillrect(x, y, ALIEN_SPEED, ALIEN_HEIGHT);
922 /* Draw alien at new pos */
923 x = PLAYFIELD_X + LIVES_X + aliens[curr_alien].x * ALIEN_SPEED;
924 y = ALIEN_START_Y + aliens[curr_alien].y * ALIEN_HEIGHT;
925 rb->lcd_bitmap_part(invadrox_aliens,
926 aliens[curr_alien].x & 1 ? ALIEN_WIDTH : 0,
927 aliens[curr_alien].type * ALIEN_HEIGHT,
928 STRIDE( SCREEN_MAIN,
929 BMPWIDTH_invadrox_aliens,
930 BMPHEIGHT_invadrox_aliens),
931 x, y, ALIEN_WIDTH, ALIEN_HEIGHT);
933 if (!next_alien()) {
934 /* Round finished. Set curr_alien to first alive from bottom. */
935 if (!first_alien()) {
936 /* Should never happen. Taken care of in move_fire(). */
937 return false;
939 /* TODO: Play next background sound */
942 return true;
946 inline void draw_ship(void)
948 /* Erase old ship */
949 if (old_ship_x < ship_x) {
950 /* Move right. Erase leftmost part of ship. */
951 rb->lcd_fillrect(old_ship_x, SHIP_Y, ship_x - old_ship_x, SHIP_HEIGHT);
952 } else if (old_ship_x > ship_x) {
953 /* Move left. Erase rightmost part of ship. */
954 rb->lcd_fillrect(ship_x + SHIP_WIDTH, SHIP_Y, old_ship_x - ship_x, SHIP_HEIGHT);
957 /* Draw ship */
958 rb->lcd_bitmap_part(invadrox_ships, 0, ship_frame * SHIP_HEIGHT,
959 STRIDE( SCREEN_MAIN,
960 BMPWIDTH_invadrox_ships,
961 BMPHEIGHT_invadrox_ships),
962 ship_x, SHIP_Y, SHIP_WIDTH, SHIP_HEIGHT);
963 if (ship_hit) {
964 /* Alternate between frame 1 and 2 during hit */
965 ship_frame_counter++;
966 if (ship_frame_counter > 2) {
967 ship_frame_counter = 0;
968 ship_frame++;
969 if (ship_frame > 2) {
970 ship_frame = 1;
975 /* Save ship_x for next time */
976 old_ship_x = ship_x;
980 inline void fire_alpha(int xc, int yc, fb_data color)
982 int oldmode = rb->lcd_get_drawmode();
984 rb->lcd_set_foreground(color);
985 rb->lcd_set_drawmode(DRMODE_FG);
987 rb->lcd_mono_bitmap(invadrox_fire, xc - (FIRE_WIDTH/2), yc, FIRE_WIDTH, FIRE_HEIGHT);
989 rb->lcd_set_foreground(LCD_BLACK);
990 rb->lcd_set_drawmode(oldmode);
994 void move_fire(void)
996 bool hit_green = false;
997 bool hit_white = false;
998 int i, j;
999 static int exploding_alien = -1;
1000 fb_data pix;
1002 if (fire == S_IDLE) {
1003 return;
1006 /* Alien hit. Wait until explosion is finished. */
1007 if (aliens_paralyzed < 0) {
1008 aliens_paralyzed++;
1009 if (aliens_paralyzed == 0) {
1010 /* Erase exploding_alien */
1011 rb->lcd_fillrect(PLAYFIELD_X + LIVES_X + aliens[exploding_alien].x * ALIEN_SPEED,
1012 ALIEN_START_Y + aliens[exploding_alien].y * ALIEN_HEIGHT,
1013 ALIEN_EXPLODE_WIDTH, ALIEN_HEIGHT);
1014 fire = S_IDLE;
1015 /* Special case. We killed curr_alien. */
1016 if (exploding_alien == curr_alien) {
1017 if (!next_alien()) {
1018 /* Round finished. Set curr_alien to first alive from bottom. */
1019 first_alien();
1023 return;
1026 if (fire == S_ACTIVE) {
1028 /* Erase */
1029 rb->lcd_vline(fire_x, fire_y, fire_y + SHOT_HEIGHT);
1031 /* Check top */
1032 if (fire_y <= SCORENUM_Y + FONT_HEIGHT + 4) {
1034 /* TODO: Play explode sound */
1036 fire = S_EXPLODE;
1037 fire_target = TARGET_TOP;
1038 fire_alpha(fire_x, fire_y, UFO_RED);
1039 return;
1042 /* Move */
1043 fire_y -= FIRE_SPEED;
1045 /* Hit UFO? */
1046 if (ufo_state == S_ACTIVE) {
1047 if ((ABS(ufo_x + UFO_WIDTH / 2 - fire_x) <= UFO_WIDTH / 2) &&
1048 (fire_y <= UFO_Y + UFO_HEIGHT)) {
1049 ufo_state = S_EXPLODE;
1050 fire = S_EXPLODE;
1051 fire_target = TARGET_UFO;
1052 /* Center explosion */
1053 ufo_x -= (UFO_EXPLODE_WIDTH - UFO_WIDTH) / 2;
1054 rb->lcd_bitmap(invadrox_ufo_explode, ufo_x, UFO_Y - 1,
1055 UFO_EXPLODE_WIDTH, UFO_EXPLODE_HEIGHT);
1056 return;
1060 /* Hit bomb? (check position, not pixel value) */
1061 for (i = 0; i < max_bombs; i++) {
1062 if (bombs[i].state == S_ACTIVE) {
1063 /* Count as hit if within BOMB_WIDTH pixels */
1064 if ((ABS(bombs[i].x - fire_x) < BOMB_WIDTH) &&
1065 (fire_y - bombs[i].y < BOMB_HEIGHT)) {
1066 /* Erase bomb */
1067 rb->lcd_fillrect(bombs[i].x, bombs[i].y, BOMB_WIDTH, BOMB_HEIGHT);
1068 bombs[i].state = S_IDLE;
1069 /* Explode ship fire */
1070 fire = S_EXPLODE;
1071 fire_target = TARGET_SHIELD;
1072 fire_alpha(fire_x, fire_y, LCD_WHITE);
1073 return;
1078 /* Check for hit*/
1079 for (i = FIRE_SPEED; i >= 0; i--) {
1080 pix = get_pixel(fire_x, fire_y + i);
1081 if(pix == screen_white) {
1082 hit_white = true;
1083 fire_y += i;
1084 break;
1086 if(pix == screen_green) {
1087 hit_green = true;
1088 fire_y += i;
1089 break;
1093 if (hit_green) {
1094 /* Hit shield */
1096 /* TODO: Play explode sound */
1098 fire = S_EXPLODE;
1099 fire_target = TARGET_SHIELD;
1100 /* Center explosion around hit pixel */
1101 fire_y -= FIRE_HEIGHT / 2;
1102 fire_alpha(fire_x, fire_y, SLIME_GREEN);
1103 return;
1106 if (hit_white) {
1108 /* Hit alien? */
1109 for (i = 0; i < 5 * ALIENS; i++) {
1110 if (aliens[i].state != DEAD &&
1111 (ABS(fire_x - (PLAYFIELD_X + LIVES_X + aliens[i].x * ALIEN_SPEED +
1112 ALIEN_WIDTH / 2)) <= ALIEN_WIDTH / 2) &&
1113 (ABS(fire_y - (ALIEN_START_Y + aliens[i].y * ALIEN_HEIGHT +
1114 ALIEN_HEIGHT / 2)) <= ALIEN_HEIGHT / 2)) {
1116 /* TODO: play alien hit sound */
1118 if (aliens[i].state == BOMBER) {
1119 /* Set (possible) alien above to bomber */
1120 for (j = i - ALIENS; j >= 0; j -= ALIENS) {
1121 if (aliens[j].state != DEAD) {
1122 /* printf("New bomber (%d, %d)\n", j % ALIENS, j / ALIENS); */
1123 aliens[j].state = BOMBER;
1124 break;
1128 aliens[i].state = DEAD;
1129 exploding_alien = i;
1130 score += scores[aliens[i].type];
1131 draw_score();
1132 /* Update score part of screen */
1133 rb->lcd_update_rect(SCORENUM_X, SCORENUM_Y,
1134 PLAYFIELD_WIDTH - 2 * NUMBERS_WIDTH, FONT_HEIGHT);
1136 /* Paralyze aliens S_EXPLODE frames */
1137 aliens_paralyzed = S_EXPLODE;
1138 rb->lcd_bitmap(invadrox_alien_explode,
1139 PLAYFIELD_X + LIVES_X + aliens[i].x * ALIEN_SPEED,
1140 ALIEN_START_Y + aliens[i].y * ALIEN_HEIGHT,
1141 ALIEN_EXPLODE_WIDTH, ALIEN_EXPLODE_HEIGHT);
1142 /* Since alien is 1 pixel taller than explosion sprite, erase bottom line */
1143 rb->lcd_hline(PLAYFIELD_X + LIVES_X + aliens[i].x * ALIEN_SPEED,
1144 PLAYFIELD_X + LIVES_X + aliens[i].x * ALIEN_SPEED + ALIEN_WIDTH,
1145 ALIEN_START_Y + (aliens[i].y + 1) * ALIEN_HEIGHT - 1);
1146 return;
1151 /* Draw shot */
1152 rb->lcd_set_foreground(LCD_WHITE);
1153 rb->lcd_vline(fire_x, fire_y, fire_y + SHOT_HEIGHT);
1154 rb->lcd_set_foreground(LCD_BLACK);
1155 } else if (fire < S_IDLE) {
1156 /* Count up towards S_IDLE, then erase explosion */
1157 fire++;
1158 if (fire == S_IDLE) {
1159 /* Erase explosion */
1160 if (fire_target == TARGET_TOP) {
1161 rb->lcd_fillrect(fire_x - (FIRE_WIDTH / 2), fire_y, FIRE_WIDTH, FIRE_HEIGHT);
1162 } else if (fire_target == TARGET_SHIELD) {
1163 /* Draw explosion with black pixels */
1164 fire_alpha(fire_x, fire_y, LCD_BLACK);
1171 /* Return a BOMBER alien */
1172 inline int random_bomber(void)
1174 int i, col;
1176 /* TODO: Weigh higher probability near ship */
1177 col = rb->rand() % ALIENS;
1178 for (i = col + 4 * ALIENS; i >= 0; i -= ALIENS) {
1179 if (aliens[i].state == BOMBER) {
1180 return i;
1184 /* No BOMBER found in this col */
1186 for (i = 0; i < 5 * ALIENS; i++) {
1187 if (aliens[i].state == BOMBER) {
1188 return i;
1192 /* No BOMBER found at all (error?) */
1194 return -1;
1198 inline void draw_bomb(int i)
1200 rb->lcd_bitmap_part(invadrox_bombs, bombs[i].type * BOMB_WIDTH,
1201 bombs[i].frame * BOMB_HEIGHT,
1202 STRIDE( SCREEN_MAIN,
1203 BMPWIDTH_invadrox_bombs,
1204 BMPHEIGHT_invadrox_bombs),
1205 bombs[i].x, bombs[i].y,
1206 BOMB_WIDTH, BOMB_HEIGHT);
1207 /* Advance frame */
1208 bombs[i].frame++;
1209 if (bombs[i].frame == bombs[i].frames) {
1210 bombs[i].frame = 0;
1215 void move_bombs(void)
1217 int i, j, bomber;
1218 bool abort;
1220 for (i = 0; i < max_bombs; i++) {
1222 switch (bombs[i].state) {
1224 case S_IDLE:
1225 if (ship_hit) {
1226 continue;
1228 bomber = random_bomber();
1229 if (bomber < 0) {
1230 DBG("ERROR: No bomber available\n");
1231 continue;
1233 /* x, y */
1234 bombs[i].x = PLAYFIELD_X + LIVES_X + aliens[bomber].x * ALIEN_SPEED + ALIEN_WIDTH / 2;
1235 bombs[i].y = ALIEN_START_Y + (aliens[bomber].y + 1) * ALIEN_HEIGHT;
1237 /* Check for duplets in x and y direction */
1238 abort = false;
1239 for (j = i - 1; j >= 0; j--) {
1240 if ((bombs[j].state == S_ACTIVE) &&
1241 ((bombs[i].x == bombs[j].x) || (bombs[i].y == bombs[j].y))) {
1242 abort = true;
1243 break;
1246 if (abort) {
1247 /* Skip this one, continue with next bomb */
1248 /* printf("Bomb %d duplet of %d\n", i, j); */
1249 continue;
1252 /* Passed, set type */
1253 bombs[i].type = rb->rand() % 3;
1254 bombs[i].frame = 0;
1255 if (bombs[i].type == 0) {
1256 bombs[i].frames = 3;
1257 } else if (bombs[i].type == 1) {
1258 bombs[i].frames = 4;
1259 } else {
1260 bombs[i].frames = 6;
1263 /* Bombs away */
1264 bombs[i].state = S_ACTIVE;
1265 draw_bomb(i);
1266 continue;
1268 break;
1270 case S_ACTIVE:
1271 /* Erase old position */
1272 rb->lcd_fillrect(bombs[i].x, bombs[i].y, BOMB_WIDTH, BOMB_HEIGHT);
1274 /* Move */
1275 bombs[i].y += BOMB_SPEED;
1277 /* Check if bottom hit */
1278 if (bombs[i].y + BOMB_HEIGHT >= PLAYFIELD_Y) {
1279 bombs[i].y = PLAYFIELD_Y - FIRE_HEIGHT + 1;
1280 fire_alpha(bombs[i].x, bombs[i].y, LCD_WHITE);
1281 bombs[i].state = S_EXPLODE;
1282 bombs[i].target = TARGET_BOTTOM;
1283 break;
1286 /* Check for green (ship or shield) */
1287 for (j = BOMB_HEIGHT; j >= BOMB_HEIGHT - BOMB_SPEED; j--) {
1288 bombs[i].target = 0;
1289 if(get_pixel(bombs[i].x + BOMB_WIDTH / 2, bombs[i].y + j) == screen_green) {
1290 /* Move to hit pixel */
1291 bombs[i].x += BOMB_WIDTH / 2;
1292 bombs[i].y += j;
1294 /* Check if ship is hit */
1295 if (bombs[i].y > SHIELD_Y + SHIELD_HEIGHT && bombs[i].y < PLAYFIELD_Y) {
1297 /* TODO: play ship hit sound */
1299 ship_hit = true;
1300 ship_frame = 1;
1301 ship_frame_counter = 0;
1302 bombs[i].state = S_EXPLODE * 4;
1303 bombs[i].target = TARGET_SHIP;
1304 rb->lcd_bitmap_part(invadrox_ships, 0, 1 * SHIP_HEIGHT,
1305 STRIDE( SCREEN_MAIN,
1306 BMPWIDTH_invadrox_ships,
1307 BMPHEIGHT_invadrox_ships),
1308 ship_x, SHIP_Y,
1309 SHIP_WIDTH, SHIP_HEIGHT);
1310 break;
1312 /* Shield hit */
1313 bombs[i].state = S_EXPLODE;
1314 bombs[i].target = TARGET_SHIELD;
1315 /* Center explosion around hit pixel in shield */
1316 bombs[i].y -= FIRE_HEIGHT / 2;
1317 fire_alpha(bombs[i].x, bombs[i].y, SLIME_GREEN);
1318 break;
1322 if (bombs[i].target != 0) {
1323 /* Hit ship or shield, continue */
1324 continue;
1327 draw_bomb(i);
1328 break;
1330 default:
1331 /* If we get here state should be < 0, exploding */
1332 bombs[i].state++;
1333 if (bombs[i].state == S_IDLE) {
1334 if (ship_hit) {
1335 /* Erase explosion */
1336 rb->lcd_fillrect(ship_x, SHIP_Y, SHIP_WIDTH, SHIP_HEIGHT);
1337 rb->lcd_update_rect(ship_x, SHIP_Y, SHIP_WIDTH, SHIP_HEIGHT);
1338 ship_hit = false;
1339 ship_frame = 0;
1340 ship_x = PLAYFIELD_X + 2 * LIVES_X;
1341 lives--;
1342 if (lives == 0) {
1343 game_over = true;
1344 return;
1346 draw_lives();
1347 /* Sleep 1s to give player time to examine lives left */
1348 rb->sleep(HZ);
1350 /* Erase explosion (even if ship hit, might be another bomb) */
1351 fire_alpha(bombs[i].x, bombs[i].y, LCD_BLACK);
1353 break;
1359 inline void move_ship(void)
1361 ship_dir += ship_acc;
1362 if (ship_dir > max_ship_speed) {
1363 ship_dir = max_ship_speed;
1365 if (ship_dir < -max_ship_speed) {
1366 ship_dir = -max_ship_speed;
1368 ship_x += ship_dir;
1369 if (ship_x < SHIP_MIN_X) {
1370 ship_x = SHIP_MIN_X;
1372 if (ship_x > SHIP_MAX_X) {
1373 ship_x = SHIP_MAX_X;
1376 draw_ship();
1380 /* Unidentified Flying Object */
1381 void move_ufo(void)
1383 static int ufo_speed;
1384 static int counter;
1385 int mystery_score;
1387 switch (ufo_state) {
1389 case S_IDLE:
1391 if (rb->rand() % 500 == 0) {
1392 /* Uh-oh, it's time to launch a mystery UFO */
1394 /* TODO: Play UFO sound */
1396 if (rb->rand() % 2) {
1397 ufo_speed = UFO_SPEED;
1398 ufo_x = PLAYFIELD_X;
1399 } else {
1400 ufo_speed = -UFO_SPEED;
1401 ufo_x = LCD_WIDTH - PLAYFIELD_X - UFO_WIDTH;
1403 ufo_state = S_ACTIVE;
1404 /* UFO will be drawn next frame */
1406 break;
1408 case S_ACTIVE:
1409 /* Erase old pos */
1410 rb->lcd_fillrect(ufo_x, UFO_Y, UFO_WIDTH, UFO_HEIGHT);
1411 /* Move */
1412 ufo_x += ufo_speed;
1413 /* Check bounds */
1414 if (ufo_x < PLAYFIELD_X || ufo_x > LCD_WIDTH - PLAYFIELD_X - UFO_WIDTH) {
1415 ufo_state = S_IDLE;
1416 break;
1418 /* Draw new pos */
1419 rb->lcd_bitmap(invadrox_ufo, ufo_x, UFO_Y, UFO_WIDTH, UFO_HEIGHT);
1420 break;
1422 case S_SHOWSCORE:
1423 counter++;
1424 if (counter == S_IDLE) {
1425 /* Erase mystery number */
1426 rb->lcd_fillrect(ufo_x, UFO_Y, 3 * NUMBERS_WIDTH + 2 * NUM_SPACING, FONT_HEIGHT);
1427 ufo_state = S_IDLE;
1429 break;
1431 default:
1432 /* Exploding */
1433 ufo_state++;
1434 if (ufo_state == S_IDLE) {
1435 /* Erase explosion */
1436 rb->lcd_fillrect(ufo_x, UFO_Y - 1, UFO_EXPLODE_WIDTH, UFO_EXPLODE_HEIGHT);
1437 ufo_state = S_SHOWSCORE;
1438 counter = S_EXPLODE * 4;
1439 /* Draw mystery_score, sleep, increase score and continue */
1440 mystery_score = 50 + (rb->rand() % 6) * 50;
1441 if (mystery_score < 100) {
1442 draw_number(ufo_x, UFO_Y, mystery_score, 2);
1443 } else {
1444 draw_number(ufo_x, UFO_Y, mystery_score, 3);
1446 score += mystery_score;
1447 draw_score();
1449 break;
1454 void draw_background(void)
1457 rb->lcd_bitmap(invadrox_background, 0, 0, LCD_WIDTH, LCD_HEIGHT);
1458 rb->lcd_update();
1462 void new_level(void)
1464 int i;
1466 draw_background();
1467 /* Give an extra life for each new level */
1468 if (lives < MAX_LIVES) {
1469 lives++;
1471 draw_lives();
1473 /* Score */
1474 draw_score();
1475 draw_number(HISCORENUM_X, SCORENUM_Y, hiscore.score, 4);
1477 level++;
1478 draw_level();
1479 level_finished = false;
1481 ufo_state = S_IDLE;
1483 /* Init alien positions and states */
1484 for (i = 0; i < 4 * ALIENS; i++) {
1485 aliens[i].x = 0 + (i % ALIENS) * ((ALIEN_WIDTH + ALIEN_SPACING) / ALIEN_SPEED);
1486 aliens[i].y = 2 * (i / ALIENS);
1487 aliens[i].state = ALIVE;
1489 /* Last row, bombers */
1490 for (i = 4 * ALIENS; i < 5 * ALIENS; i++) {
1491 aliens[i].x = 0 + (i % ALIENS) * ((ALIEN_WIDTH + ALIEN_SPACING) / ALIEN_SPEED);
1492 aliens[i].y = 2 * (i / ALIENS);
1493 aliens[i].state = BOMBER;
1496 /* Init bombs to inactive (S_IDLE) */
1497 for (i = 0; i < MAX_BOMBS; i++) {
1498 bombs[i].state = S_IDLE;
1501 /* Start aliens closer to earth from level 2 */
1502 for (i = 0; i < 5 * ALIENS; i++) {
1503 if (level < 6) {
1504 aliens[i].y += level - 1;
1505 } else {
1506 aliens[i].y += 5;
1510 /* Max concurrent bombs */
1511 max_bombs = 1;
1513 gamespeed = 2;
1515 if (level > 1) {
1516 max_bombs++;
1519 /* Increase speed */
1520 if (level > 2) {
1521 gamespeed++;
1524 if (level > 3) {
1525 max_bombs++;
1528 /* Increase speed more */
1529 if (level > 4) {
1530 gamespeed++;
1533 if (level > 5) {
1534 max_bombs++;
1537 /* 4 shields */
1538 for (i = 1; i <= 4; i++) {
1539 rb->lcd_bitmap(invadrox_shield,
1540 PLAYFIELD_X + i * PLAYFIELD_WIDTH / 5 - SHIELD_WIDTH / 2,
1541 SHIELD_Y, SHIELD_WIDTH, SHIELD_HEIGHT);
1544 /* Bottom line */
1545 rb->lcd_set_foreground(SLIME_GREEN);
1546 rb->lcd_hline(PLAYFIELD_X, LCD_WIDTH - PLAYFIELD_X, PLAYFIELD_Y);
1547 /* Restore foreground to black (for fast erase later). */
1548 rb->lcd_set_foreground(LCD_BLACK);
1550 ship_x = PLAYFIELD_X + 2 * LIVES_X;
1551 if (level == 1) {
1552 old_ship_x = ship_x;
1554 ship_dir = 0;
1555 ship_acc = 0;
1556 ship_frame = 0;
1557 ship_hit = false;
1558 fire = S_IDLE;
1559 /* Start moving the bottom row left to right */
1560 curr_alien = 4 * ALIENS;
1561 aliens_paralyzed = 0;
1562 aliens_right = true;
1563 aliens_down = false;
1564 hit_left_border = false;
1565 hit_right_border = false;
1566 /* TODO: Change max_ship_speed to 3 at higher levels */
1567 max_ship_speed = 2;
1569 draw_aliens();
1571 rb->lcd_update();
1575 void init_invadrox(void)
1577 int i;
1579 /* Seed random number generator with a "random" number */
1580 rb->srand(rb->get_time()->tm_sec + rb->get_time()->tm_min * 60);
1582 /* Precalculate start of each scanline */
1583 for (i = 0; i < LCD_HEIGHT; i++) {
1584 #if (LCD_DEPTH >= 8)
1585 ytab[i] = i * LCD_WIDTH;
1586 #elif (LCD_DEPTH == 2) && (LCD_PIXELFORMAT == HORIZONTAL_PACKING)
1587 ytab[i] = i * (LCD_WIDTH / 4);
1588 #elif (LCD_DEPTH == 2) && (LCD_PIXELFORMAT == VERTICAL_PACKING)
1589 ytab[i] = (i / 4) * LCD_WIDTH;
1590 #else
1591 #error pixelformat not implemented yet
1592 #endif
1595 rb->lcd_set_background(LCD_BLACK);
1596 rb->lcd_set_foreground(LCD_BLACK);
1598 if (highscore_load(HISCOREFILE, &hiscore, 1) < 0) {
1599 /* Init hiscore to 0 */
1600 rb->strlcpy(hiscore.name, "Invader", sizeof(hiscore.name));
1601 hiscore.score = 0;
1602 hiscore.level = 1;
1605 /* Init alien types in aliens array */
1606 for (i = 0; i < 1 * ALIENS; i++) {
1607 aliens[i].type = 0; /* Kang */
1609 for (; i < 3 * ALIENS; i++) {
1610 aliens[i].type = 1; /* Kodos */
1612 for (; i < 5 * ALIENS; i++) {
1613 aliens[i].type = 2; /* Serak */
1617 /* Save screen white color */
1618 rb->lcd_set_foreground(LCD_WHITE);
1619 rb->lcd_drawpixel(0, 0);
1620 rb->lcd_update_rect(0, 0, 1, 1);
1621 screen_white = get_pixel(0, 0);
1623 /* Save screen green color */
1624 rb->lcd_set_foreground(SLIME_GREEN);
1625 rb->lcd_drawpixel(0, 0);
1626 rb->lcd_update_rect(0, 0, 1, 1);
1627 screen_green = get_pixel(0, 0);
1629 /* Restore black foreground */
1630 rb->lcd_set_foreground(LCD_BLACK);
1632 new_level();
1634 /* Flash score at start */
1635 for (i = 0; i < 5; i++) {
1636 rb->lcd_fillrect(SCORENUM_X, SCORENUM_Y,
1637 4 * NUMBERS_WIDTH + 3 * NUM_SPACING,
1638 FONT_HEIGHT);
1639 rb->lcd_update_rect(SCORENUM_X, SCORENUM_Y,
1640 4 * NUMBERS_WIDTH + 3 * NUM_SPACING,
1641 FONT_HEIGHT);
1642 rb->sleep(HZ / 10);
1643 draw_number(SCORENUM_X, SCORENUM_Y, score, 4);
1644 rb->sleep(HZ / 10);
1649 inline bool handle_buttons(void)
1651 static unsigned int oldbuttonstate = 0;
1653 unsigned int released, pressed, newbuttonstate;
1655 if (ship_hit) {
1656 /* Don't allow ship movement during explosion */
1657 newbuttonstate = 0;
1658 } else {
1659 newbuttonstate = rb->button_status();
1661 if(newbuttonstate == oldbuttonstate) {
1662 if (newbuttonstate == 0) {
1663 /* No button pressed. Stop ship. */
1664 ship_acc = 0;
1665 if (ship_dir > 0) {
1666 ship_dir--;
1668 if (ship_dir < 0) {
1669 ship_dir++;
1672 /* return false; */
1673 goto check_usb;
1675 released = ~newbuttonstate & oldbuttonstate;
1676 pressed = newbuttonstate & ~oldbuttonstate;
1677 oldbuttonstate = newbuttonstate;
1678 if (pressed) {
1679 if (pressed & ACTION_LEFT) {
1680 if (ship_acc > -1) {
1681 ship_acc--;
1684 if (pressed & ACTION_RIGHT) {
1685 if (ship_acc < 1) {
1686 ship_acc++;
1689 if (pressed & ACTION_FIRE) {
1690 if (fire == S_IDLE) {
1691 /* Fire shot */
1692 fire_x = ship_x + SHIP_WIDTH / 2;
1693 fire_y = SHIP_Y - SHOT_HEIGHT;
1694 fire = S_ACTIVE;
1695 /* TODO: play fire sound */
1698 if (pressed & ACTION_QUIT) {
1699 rb->splash(HZ * 1, "Quit");
1700 return true;
1703 if (released) {
1704 if ((released & ACTION_LEFT)) {
1705 if (ship_acc < 1) {
1706 ship_acc++;
1709 if ((released & ACTION_RIGHT)) {
1710 if (ship_acc > -1) {
1711 ship_acc--;
1716 check_usb:
1718 /* Quit if USB is connected */
1719 if (rb->button_get(false) == SYS_USB_CONNECTED) {
1720 return true;
1723 return false;
1727 void game_loop(void)
1729 int i, end;
1731 /* Print dimensions (just for debugging) */
1732 DBG("%03dx%03dx%02d\n", LCD_WIDTH, LCD_HEIGHT, LCD_DEPTH);
1734 /* Init */
1735 init_invadrox();
1737 while (1) {
1738 /* Convert CYCLETIME (in ms) to HZ */
1739 end = *rb->current_tick + (CYCLETIME * HZ) / 1000;
1741 if (handle_buttons()) {
1742 return;
1745 /* Animate */
1746 move_ship();
1747 move_fire();
1749 /* Check if level is finished (marked by move_fire) */
1750 if (level_finished) {
1751 /* TODO: Play level finished sound */
1752 new_level();
1755 move_ufo();
1757 /* Move aliens */
1758 if (!aliens_paralyzed && !ship_hit) {
1759 for (i = 0; i < gamespeed; i++) {
1760 if (!move_aliens()) {
1761 if (game_over) {
1762 return;
1768 /* Move alien bombs */
1769 move_bombs();
1770 if (game_over) {
1771 return;
1774 /* Update "playfield" rect */
1775 rb->lcd_update_rect(PLAYFIELD_X, SCORENUM_Y + FONT_HEIGHT,
1776 PLAYFIELD_WIDTH,
1777 PLAYFIELD_Y + 1 - SCORENUM_Y - FONT_HEIGHT);
1779 /* Wait until next frame */
1780 DBG("%ld (%d)\n", end - *rb->current_tick, (CYCLETIME * HZ) / 1000);
1781 if (TIME_BEFORE(*rb->current_tick, end)) {
1782 rb->sleep(end - *rb->current_tick);
1783 } else {
1784 rb->yield();
1787 } /* end while */
1791 /* this is the plugin entry point */
1792 enum plugin_status plugin_start(UNUSED const void* parameter)
1794 rb->lcd_setfont(FONT_SYSFIXED);
1795 /* Turn off backlight timeout */
1796 backlight_force_on(); /* backlight control in lib/helper.c */
1798 /* now go ahead and have fun! */
1799 game_loop();
1801 /* Game Over. */
1802 /* TODO: Play game over sound */
1803 rb->splash(HZ * 2, "Game Over");
1804 if (score > hiscore.score) {
1805 /* Save new hiscore */
1806 highscore_update(score, level, "Invader", &hiscore, 1);
1807 highscore_save(HISCOREFILE, &hiscore, 1);
1810 /* Restore user's original backlight setting */
1811 rb->lcd_setfont(FONT_UI);
1812 /* Turn on backlight timeout (revert to settings) */
1813 backlight_use_settings(); /* backlight control in lib/helper.c */
1815 return PLUGIN_OK;
1821 * GNU Emacs settings: Kernighan & Richie coding style with
1822 * 4 spaces indent and no tabs.
1823 * Local Variables:
1824 * c-file-style: "k&r"
1825 * c-basic-offset: 4
1826 * indent-tabs-mode: nil
1827 * End: