replace weaponshop sprite of M203 with MG42
[rofl0r-openDOW.git] / spriteview.c
blob2ecf4a3922ff0fdcb7ffb55f559875204dbaa734
1 #include "../lib/include/timelib.h"
2 #include "../lib/include/macros.h"
3 #include "../lib/include/sblist.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <stdbool.h>
8 #include <assert.h>
9 #include "vec2f.h"
10 #include "anim.h"
11 #include "gameobj.h"
12 #include "video.h"
13 #include "direction.h"
14 #include "weapon.h"
15 #include "palpic.h"
16 #include "sdl_rgb.h"
17 #include "audio.h"
18 #include "muzzle_tab.h"
19 #include "spritemaps.h"
20 #include "enemy.h"
21 #include "font.h"
22 #include "maps.h"
23 #include "mapsprites.h"
24 #include "walls.h"
25 #include "music.h"
26 #include "spawnmaps.h"
28 #include <SDL/SDL.h>
30 #ifndef IN_KDEVELOP_PARSER
31 #include "../lib/include/bitarray.h"
33 #endif
35 enum mousebutton {
36 MB_LEFT = 0,
37 MB_RIGHT,
40 // 1 if button down, 0 if not, >1 to count ms pressed
41 unsigned long mousebutton_down[] = {
42 [MB_LEFT] = 0,
43 [MB_RIGHT] = 0,
46 //RcB: LINK "-lSDL"
47 #if 0
48 static void get_last_move_event(SDL_Event* e) {
49 #define numpeek 32
50 SDL_Event peek[numpeek];
51 SDL_Event* last_event = NULL;
52 int i, results;
53 results = SDL_PeepEvents(peek, numpeek, SDL_PEEKEVENT, (uint32_t) ~0);
54 if(results == -1) return;
55 for(i = 0; i < results; i++) {
56 if(peek[i].type == SDL_MOUSEMOTION)
57 last_event = &peek[i];
58 else
59 break;
61 if(last_event) {
62 *e = *last_event;
63 SDL_PeepEvents(peek, i + 1, SDL_GETEVENT, (uint32_t) ~0);
65 #undef numpeek
67 #endif
69 static vec2f get_sprite_center(const struct palpic *p) {
70 assert(p->spritecount);
71 vec2f res;
72 res.x = palpic_getspritewidth(p) * SCALE / 2;
73 res.y = palpic_getspriteheight(p) * SCALE / 2;
74 return res;
77 static int player_ids[2];
78 static int player_kills[2];
79 static int player_score[2];
80 static int player_cash[2];
81 static enum weapon_id player_weapons[2][WP_MAX];
82 static int weapon_count[2];
83 static enum weapon_id weapon_active[2]; // index into player_weapons[playerno]
84 static int player_ammo[2][AMMO_MAX];
85 static enum weapon_id get_active_weapon_id(int player_no);
86 static void switch_anim(int obj_id, int aid);
87 static vec2f get_vel_from_direction(enum direction dir, float speed);
88 static vec2f get_vel_from_direction16(enum direction16 dir, float speed);
89 // used by game_tick
90 static sblist go_player_bullets;
91 static sblist go_enemy_bullets;
92 static sblist go_explosions;
93 static sblist go_enemy_explosions;
94 static sblist go_enemies;
95 static sblist go_players;
96 static sblist go_flames;
97 static sblist go_enemy_flames;
98 static sblist go_rockets;
99 static sblist go_grenades;
100 static sblist go_enemy_grenades;
101 static sblist go_vehicles;
102 static sblist go_mines;
103 static sblist go_turrets;
104 static sblist go_bunkers;
105 static sblist go_boss;
106 static sblist go_crosshair;
107 static sblist go_muzzleflash;
108 static sblist go_blood;
109 static void add_pbullet(uint8_t bullet_id) {
110 sblist_add(&go_player_bullets, &bullet_id);
112 static void add_ebullet(uint8_t bullet_id) {
113 sblist_add(&go_enemy_bullets, &bullet_id);
115 static void add_player(uint8_t player_id) {
116 sblist_add(&go_players, &player_id);
118 static void add_enemy(uint8_t enem_id) {
119 sblist_add(&go_enemies, &enem_id);
121 static void add_explosion(uint8_t expl_id) {
122 sblist_add(&go_explosions, &expl_id);
124 static void add_enemy_explosion(uint8_t expl_id) {
125 sblist_add(&go_enemy_explosions, &expl_id);
127 static void add_flame(uint8_t id) {
128 sblist_add(&go_flames, &id);
130 static void add_grenade(uint8_t id) {
131 sblist_add(&go_grenades, &id);
133 static void add_enemy_grenade(uint8_t id) {
134 sblist_add(&go_enemy_grenades, &id);
136 static void add_rocket(uint8_t id) {
137 sblist_add(&go_rockets, &id);
139 static void add_vehicle(uint8_t id) {
140 sblist_add(&go_vehicles, &id);
142 static void add_mine(uint8_t id) {
143 sblist_add(&go_mines, &id);
145 static void add_turret(uint8_t id) {
146 sblist_add(&go_turrets, &id);
148 static void add_bunker(uint8_t id) {
149 sblist_add(&go_bunkers, &id);
151 static void add_boss(uint8_t id) {
152 sblist_add(&go_boss, &id);
154 static void add_crosshair(uint8_t id) {
155 sblist_add(&go_crosshair, &id);
157 static void add_muzzleflash(uint8_t id) {
158 sblist_add(&go_muzzleflash, &id);
160 static void add_blood(uint8_t id) {
161 sblist_add(&go_blood, &id);
163 static void add_enemy_flame(uint8_t id) {
164 sblist_add(&go_enemy_flames, &id);
166 static void golist_remove(sblist *l, uint8_t objid) {
167 size_t i;
168 uint8_t *itemid;
169 sblist_iter_counter2(l, i, itemid) {
170 if(*itemid == objid) {
171 sblist_delete(l, i);
172 return;
177 static int get_next_anim_frame(enum animation_id aid, anim_step curr) {
178 if(curr == ANIM_STEP_INIT) return animations[aid].first;
179 curr++;
180 if(curr > animations[aid].last) return animations[aid].first;
181 return curr;
184 #define SCREEN_MIN_X 64*SCALE
185 #define SCREEN_MAX_X VMODE_W - 64*SCALE
186 #define SCREEN_MIN_Y 0
187 #define SCREEN_MAX_Y 200*SCALE
189 static void draw_status_bar(void) {
190 enum weapon_id wid = get_active_weapon_id(0);
191 int x, y;
192 sdl_rgb_t *ptr = (sdl_rgb_t *) video.mem;
193 unsigned pitch = video.pitch/4;
194 for(y = SCREEN_MAX_Y; y < VMODE_H; y++)
195 for (x = SCREEN_MIN_X; x < SCREEN_MAX_X; x++)
196 ptr[y*pitch + x] = SRGB_BLACK;
198 blit_sprite(((320 / 2) - (59 / 2)) * SCALE, (200 + (40/2) - (16/2)) * SCALE,
199 &video, SCALE, spritemaps[SI_WEAPONS], wid, 0);
201 char buf[16];
202 snprintf(buf, 16, "%.6u", player_score[0]);
203 font_print(SCREEN_MIN_X + 8, SCREEN_MAX_Y + 8, buf, 6, 1 * SCALE, PRGB(255,255,255));
206 enum map_index current_map = MI_VIETNAM;
207 const struct map *map;
208 const struct map_screen* map_scr;
209 const struct palpic *map_bg;
210 const struct palpic *map_fg;
211 const mapscreen_index *map_bonus_indices;
212 const struct map_fglayer *map_bonus_scr;
214 int mapscreen_yoff, mapscreen_xoff;
215 struct { int x,y; } mapsquare;
216 enum map_scrolldir mapscrolldir;
217 unsigned map_spawn_screen_index;
218 unsigned map_spawn_line;
219 unsigned map_spawn_current;
221 static const int fps = 64;
223 static void init_map(enum map_index mapindex) {
224 map = maps[mapindex];
225 map_scr = map_screens[mapindex];
226 map_bg = map_bg_sprites[map->maptype];
227 map_fg = map_fg_sprites[map->maptype];
228 map_bonus_scr = map_bonus_screens[mapindex];
229 map_bonus_indices = map_bonus_layer_indices[mapindex];
230 mapscreen_yoff = 0;
231 mapscreen_xoff = 0;
232 mapsquare.x = 5;
233 mapsquare.y = 26;
234 mapscrolldir = MS_UP;
235 map_spawn_screen_index = 0;
236 map_spawn_line = 0;
237 map_spawn_current = 0;
240 static mapscreen_index get_bonus_layer_index(mapscreen_index screen) {
241 unsigned i;
242 for (i = 0; i < map->bonuslayer_count; i++)
243 if(map_bonus_indices[i] == screen) return i;
244 return MAPSCREEN_BLOCKED;
247 static mapscreen_index screen_to_mapscreen(int *x, int *y) {
248 *x = ((int) *x - SCREEN_MIN_X) / SCALE;
249 *y = ((int) *y - SCREEN_MIN_Y) / SCALE;
250 int yscr = (*y + mapscreen_yoff) / 192;
251 *y = (*y + mapscreen_yoff) - yscr*192;
252 int xscr = (*x + mapscreen_xoff) / 192;
253 *x = (*x + mapscreen_xoff) - xscr*192;
254 return map->screen_map[mapsquare.y + yscr][mapsquare.x + xscr];
257 static enum walltype is_wall(vec2f *pos) {
258 int x = pos->x;
259 int y = pos->y;
260 mapscreen_index scr_idx = screen_to_mapscreen(&x, &y);
261 /* can happen when a bullet goes partially off-screen */
262 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
263 uint8_t spriteno = map_scr[scr_idx].fg.fg[y/16][x/16];
264 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
265 scr_idx = get_bonus_layer_index(scr_idx);
266 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
267 spriteno = map_bonus_scr[scr_idx].fg[y/16][x/16];
268 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
269 return WT_NONE;
272 static void draw_map() {
273 int y, x, my, mx;
274 unsigned map_off = 192-mapscreen_yoff;
275 unsigned vis_x = 192-mapscreen_xoff;
276 int x_iter_max16 = 192/16;
277 int x_iter_max64 = 192/64;
278 unsigned x_iter_start64 = x_iter_max64 - (vis_x / 64 + !!(vis_x % 64));
279 unsigned x_iter_start16 = x_iter_max16 - (vis_x / 16 + !!(vis_x % 16));
281 int x_screen_start64 = -(!!(vis_x%64)*64-(vis_x%64));
282 int x_screen_start16 = -(!!(vis_x%16)*16-(vis_x%16));
284 unsigned x_screen_iter;
285 for(x_screen_iter = 0; x_screen_iter <= !!mapscreen_xoff; x_screen_iter++) {
286 mapscreen_index bonus_layer;
287 if(x_screen_iter) {
288 x_screen_start16 = x_screen_start16+(x_iter_max16-x_iter_start16)*16;
289 x_screen_start64 = x_screen_start64+(x_iter_max64-x_iter_start64)*64;
290 x_iter_max16 = mapscreen_xoff / 16 + !!(mapscreen_xoff % 16);
291 x_iter_max64 = mapscreen_xoff / 64 + !!(mapscreen_xoff % 64);
292 x_iter_start16 = 0;
293 x_iter_start64 = 0;
294 assert(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter] != MAPSCREEN_BLOCKED);
296 uint8_t spriteno;
298 for(my = 6-map_off/32-!!(map_off%32), y = SCREEN_MIN_Y + (!!(map_off%32)*32-(map_off%32))*-SCALE; my < 6; my++, y+=32*SCALE)
299 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
300 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
301 blit_sprite(x, y, &video,
302 SCALE, map_bg, spriteno, 0);
304 for(my = 12-map_off/16-!!(map_off%16), y = SCREEN_MIN_Y + (!!(map_off%16)*16-(map_off%16))*-SCALE; my < 12; my++, y+=16*SCALE)
305 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
306 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
307 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
309 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]);
310 if(bonus_layer != MAPSCREEN_BLOCKED) {
311 for(my = 12-map_off/16-!!(map_off%16), y = SCREEN_MIN_Y + (!!(map_off%16)*16-(map_off%16))*-SCALE; my < 12; my++, y+=16*SCALE)
312 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
313 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
314 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
319 int yleft = 200-map_off;
320 if(yleft > 192) yleft = 192;
321 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/32+!!(yleft%32); my++, y+=32*SCALE)
322 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
323 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
324 blit_sprite(x, y, &video, SCALE, map_bg, spriteno, 0);
326 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
327 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
328 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
329 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
332 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]);
333 if(bonus_layer != MAPSCREEN_BLOCKED) {
334 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
335 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
336 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
337 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
341 /* this is never triggered when mapscreen_xoff != 0 */
342 if(mapscreen_yoff > 192 - 8) {
343 for(mx = 0, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < 3; mx++, x += 64*SCALE)
344 blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video,
345 SCALE, map_bg, map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].bg.bg[0][mx], 0);
346 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
347 spriteno = map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].fg.fg[0][mx];
348 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
350 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+2][mapsquare.x]);
351 if(bonus_layer != MAPSCREEN_BLOCKED) {
352 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
353 spriteno = map_bonus_scr[bonus_layer].fg[0][mx];
354 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
361 #define VSCROLL_TRESHOLD (200-74)
362 #define HSCROLLR_TRESHOLD (54-6)
363 #define HSCROLLL_TRESHOLD (192-(78+3))
364 static int scroll_needed() {
365 struct gameobj *player = &objs[player_ids[0]];
366 if((mapscrolldir == MS_UP && player->pos.y - SCREEN_MIN_Y < VSCROLL_TRESHOLD*SCALE) ||
367 (mapscrolldir == MS_RIGHT && player->pos.x - SCREEN_MIN_X > HSCROLLR_TRESHOLD*SCALE) ||
368 (mapscrolldir == MS_LEFT && player->pos.x - SCREEN_MIN_X < HSCROLLL_TRESHOLD*SCALE))
369 return 1;
370 return 0;
373 static void scroll_gameobjs(int scroll_step) {
374 if(!scroll_step) return;
375 unsigned i, avail = obj_count;
376 for(i = 0; i < OBJ_MAX && avail; i++) {
377 if(!obj_slot_used[i]) continue;
378 avail--;
379 if(objs[i].objtype == OBJ_CROSSHAIR) continue;
381 if(mapscrolldir == MS_UP)
382 objs[i].pos.y += scroll_step*SCALE;
383 else if(mapscrolldir == MS_LEFT)
384 objs[i].pos.x += scroll_step*SCALE;
385 else if(mapscrolldir == MS_RIGHT)
386 objs[i].pos.x -= scroll_step*SCALE;
390 static void next_screen() {
391 map_spawn_screen_index++;
392 map_spawn_line = 0;
393 map_spawn_current = 0;
396 static int init_enemy(const struct enemy_spawn *spawn);
397 static void handle_spawns(unsigned scrollstep) {
398 assert(scrollstep <= 192);
399 unsigned i;
400 const struct enemy_spawn_screen *spawn_map = spawn_maps[current_map];
401 if(!spawn_map[map_spawn_screen_index].spawns) goto done;
402 for(i = 0; i < scrollstep; i++) {
403 while(map_spawn_current < spawn_map[map_spawn_screen_index].num_spawns &&
404 map_spawn_line+i >= spawn_map[map_spawn_screen_index].spawns[map_spawn_current].scroll_line) {
405 init_enemy(&spawn_map[map_spawn_screen_index].spawns[map_spawn_current]);
406 map_spawn_current++;
409 done:
410 map_spawn_line += scrollstep;
413 static int scroll_map() {
414 int ret = 0;
415 int scroll_step = 1;
416 if(scroll_needed()) {
417 if(mapscrolldir == MS_UP) {
418 mapscreen_yoff -= scroll_step;
419 if(mapscreen_yoff < 0) {
420 mapsquare.y--;
421 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
422 scroll_step = -mapscreen_yoff;
423 mapscreen_yoff = 0;
424 mapsquare.y++;
425 scroll_gameobjs(scroll_step);
426 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
427 mapscrolldir = MS_LEFT;
428 } else {
429 mapscrolldir = MS_RIGHT;
430 next_screen();
432 scroll_step = 0;
433 } else {
434 next_screen();
435 mapscreen_yoff += 192;
438 handle_objs:;
439 handle_spawns(scroll_step);
440 scroll_gameobjs(scroll_step);
441 ret = 1;
442 } else if(mapscrolldir == MS_LEFT) {
443 mapscreen_xoff -= scroll_step;
444 if(mapscreen_xoff < 0) {
445 mapsquare.x--;
446 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
447 scroll_step = -mapscreen_xoff;
448 mapscreen_xoff = 0;
449 mapscreen_yoff = 0;
450 mapsquare.x++;
451 scroll_gameobjs(scroll_step);
452 mapscrolldir = MS_UP;
453 scroll_step = 0;
454 } else {
455 mapscreen_xoff += 192;
456 next_screen();
459 goto handle_objs;
460 } else if(mapscrolldir == MS_RIGHT) {
461 mapscreen_xoff += scroll_step;
462 if(mapscreen_xoff >= 192) {
463 mapsquare.x++;
464 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
465 scroll_step = mapscreen_xoff - 192;
466 mapscreen_xoff = 0;
467 mapscreen_yoff = 0;
468 scroll_gameobjs(scroll_step);
469 mapscrolldir = MS_UP;
470 scroll_step = 0;
471 } else {
472 next_screen();
473 mapscreen_xoff -= 192;
476 goto handle_objs;
479 return ret;
482 static void init_player_once(int player_no) {
483 player_score[player_no] = 0;
484 player_cash[player_no] = 10000;
487 static int init_player(int player_no) {
488 assert(player_no == 0 || player_no == 1);
489 int pid = gameobj_alloc();
490 gameobj_init(pid, &VEC( SCREEN_MIN_X, SCREEN_MAX_Y - (25 * SCALE) ), &VEC( 0, 0 ),
491 SI_PLAYERS, player_no == 0 ? ANIM_P1_MOVE_N : ANIM_P2_MOVE_N, player_no == 0 ? OBJ_P1 : OBJ_P2);
492 if(pid == -1) return -1;
493 player_ids[player_no] = pid;
494 player_weapons[player_no][0] = WP_COLT45;
495 weapon_count[player_no] = 1;
496 weapon_active[player_no] = 0;
498 // FIXME: remove this once the weapon shop is implemented
499 size_t i = 0;
500 for(; i < AMMO_MAX; i++)
501 player_ammo[player_no][i] = 50000;
503 add_player(pid);
504 return pid;
507 static vec2f *mousepos;
508 static int init_crosshair() {
509 int id = gameobj_alloc();
510 gameobj_init(id, &VEC(VMODE_W/2, VMODE_H/2), &VEC(0,0), SI_CROSSHAIR, ANIM_CROSSHAIR, OBJ_CROSSHAIR);
511 if(id == -1) return -1;
512 mousepos = &objs[id].pos;
513 return id;
516 static int init_blood(vec2f *pos) {
517 int id = gameobj_alloc();
518 gameobj_init(id, pos, &VEC(0,0), SI_MISC, ANIM_BLOOD, OBJ_BLOOD);
519 gameobj_init_bulletdata(id, 4);
520 return id;
523 static int init_bullet(vec2f *pos, vec2f *vel, int steps) {
524 int id = gameobj_alloc();
525 gameobj_init(id, pos, vel, SI_BULLET, ANIM_BULLET, OBJ_BULLET);
526 gameobj_init_bulletdata(id, steps);
527 return id;
530 static int init_grenade(vec2f *pos, vec2f *vel, int steps) {
531 int id = gameobj_alloc();
532 gameobj_init(id, pos, vel, SI_GRENADE, ANIM_GRENADE_SMALL, OBJ_GRENADE);
533 gameobj_init_bulletdata(id, steps);
534 return id;
537 static int init_grenade_explosion(vec2f *pos, int from_enemy) {
538 const int ticks_per_anim_frame = 4;
539 const int expl_anim_frames = 11;
540 vec2f grenade_center = get_sprite_center(spritemaps[SI_GRENADE_EXPLOSION]);
541 vec2f mypos = vecsub(pos, &grenade_center);
542 int id = gameobj_alloc();
543 if(id == -1) return -1;
544 gameobj_init(id, &mypos, &VEC(0,0), SI_GRENADE_EXPLOSION, ANIM_GRENADE_EXPLOSION, OBJ_GRENADE_EXPLOSION);
545 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
546 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
547 if(!from_enemy) add_explosion(id);
548 else add_enemy_explosion(id);
549 return id;
552 static int init_big_explosion(vec2f *pos) {
553 const int ticks_per_anim_frame = 8;
554 const int expl_anim_frames = 5;
555 vec2f rocket_center = get_sprite_center(spritemaps[SI_BIG_EXPLOSION]);
556 vec2f mypos = vecsub(pos, &rocket_center);
557 int id = gameobj_alloc();
558 if(id == -1) return -1;
559 gameobj_init(id, &mypos, &VEC(0,0), SI_BIG_EXPLOSION, ANIM_BIG_EXPLOSION, OBJ_BIG_EXPLOSION);
560 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
561 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
562 add_explosion(id);
563 return id;
566 static int init_rocket_explosion(vec2f *pos) {
567 vec2f ax = vecadd(pos, &VEC(-15*SCALE, 9*SCALE));
568 vec2f bx = vecadd(pos, &VEC(1*SCALE, -6*SCALE));
569 vec2f cx = vecadd(pos, &VEC(-8*SCALE, -8*SCALE));
570 vec2f dx = vecadd(pos, &VEC(8*SCALE, 8*SCALE));
571 int ret = 0;
572 ret += init_grenade_explosion(&ax, 0) != -1;
573 ret += init_grenade_explosion(&bx, 0) != -1;
574 ret += init_big_explosion(&cx) != -1;
575 ret += init_big_explosion(&dx) != -1;
576 return ret;
579 static int init_flame(vec2f *pos, vec2f *vel, int steps) {
580 int id = gameobj_alloc();
581 if(id == -1) return -1;
582 gameobj_init(id, pos, vel, SI_FLAME, ANIM_FLAME, OBJ_FLAME);
583 gameobj_init_bulletdata(id, steps);
584 return id;
587 static int init_player_flame(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
588 static const vec2f flame_origin[] = {
589 [DIR_O] = { 4.0, 8.0 },
590 [DIR_NO] = { 5.0, 11.0 },
591 [DIR_N] = { 7.5, 12.0 },
592 [DIR_NW] = { 10.0, 11.0 },
593 [DIR_W] = { 11.0, 8.0 },
594 [DIR_SW] = { 10.0, 5.0 },
595 [DIR_S] = { 7.5, 3.0 },
596 [DIR_SO] = { 4.0, 4.0 },
598 vec2f mypos = *pos;
599 mypos.x -= flame_origin[dir].x * SCALE;
600 mypos.y -= flame_origin[dir].y * SCALE;
601 return init_flame(&mypos, vel, steps);
604 static int init_rocket(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
605 static const vec2f rocket_origin[] = {
606 [DIR_N] = { 1.0, 10.0 },
607 [DIR_S] = { 1.0, 0.0 },
608 [DIR_O] = { 0.0, 1.0 },
609 [DIR_W] = { 10.0, 1.0 },
610 [DIR_NO] = { 0.0, 7.0 },
611 [DIR_SO] = { 0.0, 0.0 },
612 [DIR_SW] = { 7.0, 0.0 },
613 [DIR_NW] = { 7.0, 7.0 },
615 static const enum animation_id rocket_anim[] = {
616 [DIR_N] = ANIM_ROCKET_N,
617 [DIR_S] = ANIM_ROCKET_S,
618 [DIR_O] = ANIM_ROCKET_O,
619 [DIR_W] = ANIM_ROCKET_W,
620 [DIR_NO] = ANIM_ROCKET_NO,
621 [DIR_SO] = ANIM_ROCKET_SO,
622 [DIR_SW] = ANIM_ROCKET_SW,
623 [DIR_NW] = ANIM_ROCKET_NW,
625 vec2f mypos = *pos;
626 mypos.x -= rocket_origin[dir].x * SCALE;
627 mypos.y -= rocket_origin[dir].y * SCALE;
628 int id = gameobj_alloc();
629 if(id == -1) return -1;
630 gameobj_init(id, &mypos, vel, SI_ROCKET, rocket_anim[dir], OBJ_ROCKET);
631 gameobj_init_bulletdata(id, steps);
632 add_rocket(id);
633 return id;
637 static const struct enemy_route* get_enemy_current_route(int curr_step, const struct enemy_spawn *spawn) {
638 int i = ENEMY_MAX_ROUTE -1;
639 for(; i >= 0; i--)
640 if(spawn->route[i].shape != ES_INVALID &&
641 curr_step >= spawn->route[i].start_step)
642 return &spawn->route[i];
643 return 0;
646 static vec2f get_enemy_vel(int curr_step, const struct enemy_spawn *spawn) {
647 const struct enemy_route *route = get_enemy_current_route(curr_step, spawn);
648 return get_vel_from_direction16(route->dir, (float)route->vel/8.f);
651 static const enum animation_id enemy_animation_lut[] = {
652 [ES_SOLDIER1_DOWN] = ANIM_ENEMY_GUNNER_DOWN,
653 [ES_SOLDIER1_RIGHT] = ANIM_ENEMY_GUNNER_RIGHT,
654 [ES_SOLDIER1_LEFT] = ANIM_ENEMY_GUNNER_LEFT,
655 [ES_SOLDIER2_DOWN] = ANIM_ENEMY_BOMBER_DOWN,
656 [ES_SOLDIER2_RIGHT] = ANIM_ENEMY_BOMBER_RIGHT,
657 [ES_SOLDIER2_LEFT] = ANIM_ENEMY_BOMBER_LEFT,
658 [ES_JEEP] = ANIM_JEEP,
659 [ES_TANK_SMALL] = ANIM_TANK_SMALL,
660 [ES_TANK_BIG] = ANIM_TANK_BIG,
661 [ES_TRANSPORTER] = ANIM_TRANSPORTER,
662 [ES_GUNTURRET_MOVABLE_MACHINE] = ANIM_GUNTURRET_MOVABLE_MACHINE_S,
663 [ES_GUNTURRET_MOVABLE_MAN] = ANIM_GUNTURRET_MOVABLE_MAN_S,
664 [ES_MINE_FLAT] = ANIM_MINE_FLAT,
665 [ES_MINE_CROSS] = ANIM_MINE_CROSSED,
666 [ES_FLAMETURRET] = ANIM_FLAMETURRET,
667 [ES_GUNTURRET_FIXED_SOUTH] = ANIM_GUNTURRET_FIXED_SOUTH,
668 [ES_GUNTURRET_FIXED_NORTH] = ANIM_GUNTURRET_FIXED_NORTH,
669 [ES_BUNKER_1] = ANIM_BUNKER1,
670 [ES_BUNKER_2] = ANIM_BUNKER2,
671 [ES_BUNKER_3] = ANIM_BUNKER3,
672 [ES_BUNKER_4] = ANIM_BUNKER4,
673 [ES_BUNKER_5] = ANIM_BUNKER5,
676 static int init_enemy(const struct enemy_spawn *spawn) {
677 const enum objtype enemy_soldier_objtype_lut[] = {
678 [0] = OBJ_ENEMY_SHOOTER,
679 [1] = OBJ_ENEMY_BOMBER
681 const enum objtype enemy_objtype_lut[] = {
682 [ES_JEEP] = OBJ_JEEP,
683 [ES_TANK_SMALL] = OBJ_TANK_SMALL,
684 [ES_TANK_BIG] = OBJ_TANK_BIG,
685 [ES_TRANSPORTER] = OBJ_TRANSPORTER,
686 [ES_GUNTURRET_MOVABLE_MACHINE] = OBJ_GUNTURRET_MOVABLE_MACHINE,
687 [ES_GUNTURRET_MOVABLE_MAN] = OBJ_GUNTURRET_MOVABLE_MAN,
688 [ES_MINE_FLAT] = OBJ_MINE_FLAT,
689 [ES_MINE_CROSS] = OBJ_MINE_CROSSED,
690 [ES_FLAMETURRET] = OBJ_FLAMETURRET,
691 [ES_GUNTURRET_FIXED_SOUTH] = OBJ_GUNTURRET_FIXED_SOUTH,
692 [ES_GUNTURRET_FIXED_NORTH] = OBJ_GUNTURRET_FIXED_NORTH,
693 [ES_BUNKER_1] = OBJ_BUNKER1,
694 [ES_BUNKER_2] = OBJ_BUNKER2,
695 [ES_BUNKER_3] = OBJ_BUNKER3,
696 [ES_BUNKER_4] = OBJ_BUNKER4,
697 [ES_BUNKER_5] = OBJ_BUNKER5,
698 [ES_BOSS] = OBJ_BOSS,
700 const enum animation_id boss_animation_lut[] = {
701 [0] = ANIM_BOSS1,
702 [1] = ANIM_BOSS2,
703 [2] = ANIM_BOSS3,
704 [3] = ANIM_BOSS4,
705 [4] = ANIM_BOSS5,
706 [5] = ANIM_BOSS6,
707 [6] = ANIM_BOSS7,
708 [7] = ANIM_BOSS8,
709 [8] = ANIM_BOSS9,
710 [9] = ANIM_BOSS10,
711 [10] = ANIM_BOSS11,
712 [11] = ANIM_BOSS12,
714 const enum sprite_index enemy_soldier_sprite_lut[] = {
715 [ET_ASIAN] = SI_ENEMY_ASIAN,
716 [ET_WESTERN] = SI_ENEMY_WESTERN,
718 const enum sprite_index enemy_sprite_lut[] = {
719 [ES_JEEP] = SI_VEHICLES_SMALL,
720 [ES_TANK_SMALL] = SI_VEHICLES_MEDIUM,
721 [ES_TANK_BIG] = SI_VEHICLES_BIG,
722 [ES_TRANSPORTER] = SI_VEHICLES_BIG,
723 [ES_BUNKER_1] = SI_BUNKERS,
724 [ES_BUNKER_2] = SI_BUNKERS,
725 [ES_BUNKER_3] = SI_BUNKERS,
726 [ES_BUNKER_4] = SI_BUNKERS,
727 [ES_BUNKER_5] = SI_BUNKERS,
728 [ES_GUNTURRET_MOVABLE_MACHINE] = SI_GUNTURRET,
729 [ES_GUNTURRET_MOVABLE_MAN] = SI_GUNTURRET,
730 [ES_MINE_FLAT] = SI_MINES,
731 [ES_MINE_CROSS] = SI_MINES,
732 [ES_FLAMETURRET] = SI_MINES,
733 [ES_GUNTURRET_FIXED_SOUTH] = SI_MINES,
734 [ES_GUNTURRET_FIXED_NORTH] = SI_MINES,
735 [ES_BOSS] = SI_BOSSES,
738 vec2f spawnpos = VEC(SCREEN_MIN_X + spawn->x*SCALE, SCREEN_MIN_Y + spawn->y*SCALE);
739 int id = gameobj_alloc();
740 if(id == -1) return -1;
741 const struct enemy_route* route_curr = get_enemy_current_route(0, spawn);
742 vec2f vel = get_enemy_vel(0, spawn);
744 int is_soldier = route_curr->shape <= ES_SOLDIER2_RIGHT;
745 int is_boss = route_curr->shape == ES_BOSS;
746 enum sprite_index spriteid;
747 enum objtype objid;
748 enum animation_id animid;
749 if(is_soldier) {
750 spriteid = enemy_soldier_sprite_lut[map->enemy_type];
751 objid = enemy_soldier_objtype_lut[spawn->weapon];
752 } else {
753 spriteid = enemy_sprite_lut[route_curr->shape];
754 objid = enemy_objtype_lut[route_curr->shape];
756 if(is_boss) animid = boss_animation_lut[map->boss_id];
757 else animid = enemy_animation_lut[route_curr->shape];
759 gameobj_init(id, &spawnpos, &vel, spriteid, animid, objid);
760 objs[id].objspecific.enemy.curr_step = 0;
761 objs[id].objspecific.enemy.spawn = spawn;
762 switch(objid) {
763 case OBJ_BOSS:
764 add_boss(id);
765 break;
766 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
767 case OBJ_BUNKER4: case OBJ_BUNKER5:
768 add_bunker(id);
769 break;
770 case OBJ_GUNTURRET_MOVABLE_MACHINE:
771 case OBJ_GUNTURRET_MOVABLE_MAN:
772 case OBJ_FLAMETURRET: case OBJ_GUNTURRET_FIXED_NORTH:
773 case OBJ_GUNTURRET_FIXED_SOUTH:
774 add_turret(id);
775 break;
776 case OBJ_MINE_CROSSED: case OBJ_MINE_FLAT:
777 add_mine(id);
778 break;
779 case OBJ_JEEP: case OBJ_TRANSPORTER:
780 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
781 add_vehicle(id);
782 break;
783 default:
784 add_enemy(id);
786 return id;
789 static void remove_enemy(int id) {
790 enum objtype objid = objs[id].objtype;
791 switch(objid) {
792 case OBJ_JEEP: case OBJ_TRANSPORTER:
793 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
794 golist_remove(&go_vehicles, id);
795 break;
796 default:
797 golist_remove(&go_enemies, id);
799 gameobj_free(id);
802 static int enemy_fires(struct enemy *e) {
803 int i;
804 for(i = 0; i < ENEMY_MAX_SHOT; i++)
805 if(e->curr_step == e->spawn->shots[i]) return 1;
806 return 0;
809 static enum animation_id get_flash_animation_from_direction(enum direction dir) {
810 #define ANIMF(dir, anim) [dir] = anim
811 static const enum animation_id dir_to_anim[] = {
812 ANIMF(DIR_O, ANIM_FLASH_O),
813 ANIMF(DIR_NO, ANIM_FLASH_NO),
814 ANIMF(DIR_N, ANIM_FLASH_N),
815 ANIMF(DIR_NW, ANIM_FLASH_NW),
816 ANIMF(DIR_W, ANIM_FLASH_W),
817 ANIMF(DIR_SW, ANIM_FLASH_SW),
818 ANIMF(DIR_S, ANIM_FLASH_S),
819 ANIMF(DIR_SO, ANIM_FLASH_SO),
821 #undef ANIMF
822 return dir_to_anim[dir];
825 static int init_flash(vec2f *pos, enum direction dir) {
826 int id = gameobj_alloc();
827 gameobj_init(id, pos, &VEC(0, 0), SI_FLASH, get_flash_animation_from_direction(dir), OBJ_FLASH);
828 gameobj_init_bulletdata(id, 2);
829 return id;
832 static vec2f get_gameobj_pos(int obj_id) {
833 return objs[obj_id].pos;
836 static vec2f get_gameobj_center(int obj_id) {
837 vec2f res = objs[obj_id].pos;
838 vec2f add = get_sprite_center(spritemaps[objs[obj_id].spritemap_id]);
839 return vecadd(&res, &add);
842 static enum direction get_direction_from_vec(vec2f *vel);
843 static enum animation_id get_anim_from_direction(enum direction dir, int player, int throwing);
845 static enum direction16 get_direction16_from_direction(enum direction dir) {
846 static const enum direction16 dir16_transl_tab[] = {
847 [DIR_N] = DIR16_N, [DIR_NO] = DIR16_NO, [DIR_O] = DIR16_O, [DIR_SO] = DIR16_SO,
848 [DIR_S] = DIR16_S, [DIR_SW] = DIR16_SW, [DIR_W] = DIR16_W, [DIR_NW] = DIR16_NW,
850 assert(dir != DIR_INVALID);
851 return dir16_transl_tab[dir];
854 static enum weapon_id get_active_weapon_id(int player_no) {
855 return player_weapons[player_no][weapon_active[player_no]];
858 static const struct weapon* get_active_weapon(int player_no) {
859 return &weapons[get_active_weapon_id(player_no)];
861 static enum direction16 get_shotdirection_from_enemy(int curr_step, const struct enemy_spawn *spawn) {
862 const struct enemy_route* r = get_enemy_current_route(curr_step, spawn);
863 switch(r->shape) {
864 case ES_SOLDIER1_DOWN: case ES_SOLDIER2_DOWN:
865 return DIR16_S;
866 case ES_SOLDIER1_LEFT: case ES_SOLDIER2_LEFT:
867 return DIR16_W;
868 case ES_SOLDIER1_RIGHT: case ES_SOLDIER2_RIGHT:
869 return DIR16_O;
870 default:
871 assert(0);
875 static void enemy_fire_bullet(enum direction16 dir, int steps, enum enemy_weapon wpn, vec2f *pos) {
876 vec2f vel = get_vel_from_direction16(dir, 1.75);
877 int id;
878 if(wpn == EW_GUN) {
879 id = init_bullet(pos, &vel, steps);
880 if(id != -1) add_ebullet(id);
881 } else if (wpn == EW_GRENADE) {
882 id = init_grenade(pos, &vel, steps);
883 if(id != -1) add_enemy_grenade(id);
884 } else {
885 id = init_flame(pos, &vel, steps);
886 if(id != -1) add_enemy_flame(id);
890 static int get_crosshair_id(void) {
891 assert(sblist_getsize(&go_crosshair));
892 uint8_t *id = sblist_get(&go_crosshair, 0);
893 return *id;
896 static void fire_bullet(int player_no) {
897 int id;
898 const struct weapon *pw = get_active_weapon(player_no);
899 if(player_ammo[player_no][pw->ammo] == 0) return;
900 vec2f from = get_gameobj_center(player_ids[player_no]);
901 //get_anim_from_vel(0, objs[player].
902 vec2f to = get_gameobj_center(get_crosshair_id());
903 to.x += 4*SCALE - rand()%8*SCALE;
904 to.y += 4*SCALE - rand()%8*SCALE;
905 vec2f vel = velocity(&from, &to);
906 enum direction dir = get_direction_from_vec(&vel);
907 if(dir != DIR_INVALID) {
908 enum animation_id aid = get_anim_from_direction(dir, player_no, pw->ammo == AMMO_GRENADE);
909 if(aid != ANIM_INVALID) switch_anim(player_ids[player_no], aid);
910 anim_step curranim = objs[player_ids[player_no]].anim_curr;
911 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[player_ids[player_no]].animid, ANIM_STEP_INIT);
912 vec2f muzzle = muzzle_tab[curranim];
914 from = get_gameobj_pos(player_ids[player_no]);
915 from.x += muzzle.x * SCALE;
916 from.y += muzzle.y * SCALE;
918 if(pw->flags & WF_MUZZLEFLASH) {
919 static const vec2f flash_start[] = {
920 [DIR_O] = { 0.0, 1.0 },
921 [DIR_NO] = { 0.5, 6.0 },
922 [DIR_N] = { 1.0, 7.5 },
923 [DIR_NW] = { 6.0, 6.0 },
924 [DIR_W] = { 7.5, 1.0 },
925 [DIR_SW] = { 4.5, 0.0 },
926 [DIR_S] = { 1.0, 0.0 },
927 [DIR_SO] = { 0.0, 0.0 },
929 vec2f ffrom = from;
930 ffrom.x -= flash_start[dir].x * SCALE;
931 ffrom.y -= flash_start[dir].y * SCALE;
932 id = init_flash(&ffrom, dir);
933 if(id != -1) add_muzzleflash(id);
935 vel = velocity(&from, &to);
937 float dist = veclength(&vel);
938 float speed = pw->bullet_speed * SCALE;
939 const uint16_t range_tab[] = {0, 80, 66, 80, 118, 118, 118, 118, 118, 118,
940 200, 200, 240, 240, 240, 240, 240, 240, 240, 240, 320 };
941 float range = range_tab[pw->range] * SCALE;
942 if(dist > range)
943 dist = range;
944 float steps = dist / speed;
945 float deg = atan2(vel.y, vel.x);
946 vel.x = cos(deg) * speed;
947 vel.y = sin(deg) * speed;
948 switch(pw->shot) {
949 case ST_LAUNCHER:
950 id = init_rocket(dir, &from, &vel, steps);
951 break;
952 case ST_BULLET:
953 id = init_bullet(&from, &vel, steps);
954 if(id != -1) add_pbullet(id);
955 break;
956 case ST_FLAMES:
957 id = init_player_flame(dir, &from, &vel, steps);
958 if(id != -1) add_flame(id);
959 break;
960 case ST_GRENADE:
961 id = init_grenade(&from, &vel, steps);
962 add_grenade(id);
963 break;
964 default:
965 abort();
967 player_ammo[player_no][pw->ammo]--;
968 const WAVE_HEADER_COMPLETE *wf = wavesounds[pw->sound];
969 if(id != -1 && pw->sound != WS_NONE)
970 audio_play_wave_resource(wf);
973 static void init_game_objs() {
974 sblist_init(&go_crosshair, 1, 4);
975 sblist_init(&go_players, 1, 4);
976 sblist_init(&go_muzzleflash, 1, 4);
977 sblist_init(&go_player_bullets, 1, 32);
978 sblist_init(&go_flames, 1, 32);
979 sblist_init(&go_enemy_bullets, 1, 32);
980 sblist_init(&go_explosions, 1, 16);
981 sblist_init(&go_enemy_explosions, 1, 16);
982 sblist_init(&go_grenades, 1, 16);
983 sblist_init(&go_enemy_grenades, 1, 16);
984 sblist_init(&go_rockets, 1, 8);
985 sblist_init(&go_enemies, 1, 32);
986 sblist_init(&go_vehicles, 1, 4);
987 sblist_init(&go_mines, 1, 4);
988 sblist_init(&go_turrets, 1, 8);
989 sblist_init(&go_bunkers, 1, 4);
990 sblist_init(&go_boss, 1, 4);
991 sblist_init(&go_blood, 1, 16);
992 sblist_init(&go_enemy_flames, 1, 16);
993 init_player(0);
994 add_crosshair(init_crosshair());
995 init_map(current_map);
998 static int point_in_mask(vec2f *point, int obj_id) {
999 vec2f pos_in_pic = VEC((point->x - objs[obj_id].pos.x) / SCALE,
1000 (point->y - objs[obj_id].pos.y) / SCALE);
1001 const struct palpic *p = spritemaps[objs[obj_id].spritemap_id];
1002 unsigned h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
1003 if(pos_in_pic.x < 0 || pos_in_pic.y < 0 || pos_in_pic.x > w || pos_in_pic.y > h) return 0;
1004 assert(objs[obj_id].anim_curr != ANIM_STEP_INIT);
1005 anim_step curranim = objs[obj_id].anim_curr;
1006 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[obj_id].animid, ANIM_STEP_INIT);
1007 uint8_t *data = palpic_getspritedata(p, curranim);
1008 if(data[(unsigned) pos_in_pic.y * w + (unsigned) pos_in_pic.x] != 0) return 1;
1009 return 0;
1012 static enum animation_id get_die_anim(unsigned id) {
1013 switch(objs[id].objtype) {
1014 case OBJ_P1:
1015 return ANIM_P1_DIE;
1016 case OBJ_P2:
1017 return ANIM_P2_DIE;
1018 case OBJ_JEEP:
1019 return ANIM_JEEP_DESTROYED;
1020 case OBJ_TANK_SMALL:
1021 return ANIM_TANK_SMALL_DESTROYED;
1022 case OBJ_TANK_BIG:
1023 return ANIM_TANK_BIG_DESTROYED;
1024 case OBJ_TRANSPORTER:
1025 return ANIM_TRANSPORTER_DESTROYED;
1026 case OBJ_ENEMY_BOMBER:
1027 return ANIM_ENEMY_BOMBER_DIE;
1028 case OBJ_ENEMY_SHOOTER:
1029 return ANIM_ENEMY_GUNNER_DIE;
1030 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
1031 case OBJ_BUNKER4: case OBJ_BUNKER5:
1032 return ANIM_BUNKER_DESTROYED;
1033 case OBJ_GUNTURRET_MOVABLE_MACHINE:
1034 return ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED;
1035 case OBJ_GUNTURRET_MOVABLE_MAN:
1036 return ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED;
1037 default:
1038 return ANIM_INVALID;
1042 /* remove bullets that have reached their maximum number of steps */
1043 static int remove_bullets(sblist *list) {
1044 int res = 0;
1045 uint8_t *item_id;
1046 ssize_t li;
1047 sblist_iter_counter2s(list, li, item_id) {
1048 struct gameobj *bullet = &objs[*item_id];
1049 if(bullet->objspecific.bullet.step_curr >= bullet->objspecific.bullet.step_max) {
1050 gameobj_free(*item_id);
1051 sblist_delete(list, li);
1052 li--;
1053 } else {
1054 bullet->objspecific.bullet.step_curr++;
1056 res = 1;
1058 return res;
1061 static int remove_explosives(sblist *list) {
1062 int res = 0;
1063 uint8_t *item_id;
1064 ssize_t li;
1065 sblist_iter_counter2s(list, li, item_id) {
1066 struct gameobj *go = &objs[*item_id];
1067 if(go->objspecific.bullet.step_curr >= go->objspecific.bullet.step_max) {
1068 if(go->objtype == OBJ_GRENADE) init_grenade_explosion(&go->pos, list == &go_enemy_grenades);
1069 else init_rocket_explosion(&go->pos);
1070 gameobj_free(*item_id);
1071 sblist_delete(list, li);
1072 li--;
1073 } else {
1074 go->objspecific.bullet.step_curr++;
1075 if(go->objtype == OBJ_GRENADE) {
1076 if(go->objspecific.bullet.step_curr >= 32) go->animid = ANIM_GRENADE_SMALL;
1077 else if(go->objspecific.bullet.step_curr >= 8) go->animid = ANIM_GRENADE_BIG;
1080 res = 1;
1082 return res;
1085 static int remove_offscreen_objects(sblist *list) {
1086 int res = 0;
1087 uint8_t *item_id;
1088 ssize_t li;
1089 sblist_iter_counter2s(list, li, item_id) {
1090 assert(obj_slot_used[*item_id]);
1091 struct gameobj *go = &objs[*item_id];
1092 assert((int) go->spritemap_id < SI_MAX);
1093 const struct palpic *p = spritemaps[go->spritemap_id];
1094 assert(p->spritecount);
1095 int h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
1096 if(go->pos.x < SCREEN_MIN_X-w*SCALE || go->pos.x > SCREEN_MAX_X ||
1097 go->pos.y < SCREEN_MIN_Y-h*SCALE || go->pos.y > SCREEN_MAX_Y) {
1098 res = 1;
1099 dprintf(2, "offscreen: removed gameobj %d\n", (int) *item_id);
1100 gameobj_free(*item_id);
1101 sblist_delete(list, li);
1102 li--;
1105 return res;
1108 static int get_sprite_number(int id) {
1109 struct gameobj *o = &objs[id];
1110 return o->anim_curr == ANIM_STEP_INIT ? get_next_anim_frame(o->animid, o->anim_curr) : o->anim_curr;
1113 static int is_death_anim(enum animation_id anim) {
1114 switch(anim) {
1115 case ANIM_ENEMY_BOMBER_DIE:
1116 case ANIM_ENEMY_GUNNER_DIE:
1117 case ANIM_ENEMY_BURNT:
1118 case ANIM_P1_DIE:
1119 case ANIM_P2_DIE:
1120 case ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED:
1121 case ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED:
1122 case ANIM_BUNKER_DESTROYED:
1123 case ANIM_JEEP_DESTROYED:
1124 case ANIM_TANK_BIG_DESTROYED:
1125 case ANIM_TANK_SMALL_DESTROYED:
1126 case ANIM_TRANSPORTER_DESTROYED:
1127 return 1;
1128 default:
1129 return 0;
1133 static int overlap(int id1, int id2) {
1134 struct gameobj *o1 = &objs[id1];
1135 struct gameobj *o2 = &objs[id2];
1136 const struct palpic* p1 = spritemaps[o1->spritemap_id];
1137 const struct palpic* p2 = spritemaps[o2->spritemap_id];
1139 vec2f vec = velocity(&o1->pos, &o2->pos);
1140 float mh = o1->pos.y < o2->pos.y ? palpic_getspriteheight(p1) : palpic_getspriteheight(p2);
1141 float mw = o1->pos.x < o2->pos.x ? palpic_getspritewidth(p1) : palpic_getspritewidth(p2);
1142 mh*=SCALE, mw*=SCALE;
1143 return fabs(vec.x) < mw && fabs(vec.y) < mh;
1146 static int masks_collide(int id1, int id2) {
1147 struct gameobj *a = &objs[id1];
1148 struct gameobj *b = &objs[id2];
1149 const struct palpic* pa = spritemaps[a->spritemap_id];
1150 const struct palpic* pb = spritemaps[b->spritemap_id];
1151 int ax = a->pos.x/SCALE, ay = a->pos.y/SCALE,
1152 aw = palpic_getspritewidth(pa), ah = palpic_getspriteheight(pa);
1153 int bx = b->pos.x/SCALE, by = b->pos.y/SCALE,
1154 bw = palpic_getspritewidth(pb), bh = palpic_getspriteheight(pb);
1156 /* x: normalized start position */
1157 int xx = MIN(ax, bx), xy = MIN(ay, by);
1158 /* normalize coordinates to 0 */
1159 ax -= xx, ay -= xy, bx -= xx, by -= xy;
1160 /* o: upper left point of the overlapping region */
1161 int ox = MAX(ax, bx), oy = MAX(ay, by);
1162 /* e: lower right point of the overlapping region */
1163 int ex = MIN(ax+aw, bx+bw), ey = MIN(ay+ah, by+bh);
1164 /* vector o -> e */
1165 int oex = ex - ox, oey = ey - oy;
1166 /* (s)tart and (e)nd coordinates of the overlapping regions in both rects */
1167 int asx = ox - ax, asy = oy - ay;
1168 int bsx = ox - bx, bsy = oy - by;
1169 int aex = asx + oex, bex = bsx + oex;
1170 int aey = asy + oey, bey = bsy + oey;
1171 assert(asx >= 0); assert(asy >= 0);
1172 assert(bsx >= 0); assert(bsy >= 0);
1173 if(!aex || !bex || !aey || !bey) return 0;
1174 assert(aex > 0); assert(bex > 0);
1175 assert(aey > 0); assert(bey > 0);
1176 int xa, ya, xb, yb;
1177 const uint8_t *da = palpic_getspritedata(pa, get_sprite_number(id1)),
1178 *db = palpic_getspritedata(pb, get_sprite_number(id2));
1179 assert(da); assert(db);
1180 for(ya = asy, yb = bsy; ya < aey && yb < bey; ya++, yb++)
1181 for(xa = asx, xb = bsx; xa < aex && xb < bex ; xa++, xb++)
1182 if(da[ya*aw+xa] && db[yb*bw+xb]) return 1;
1183 return 0;
1187 // removes bullets and other objects if they collide. return 1 if anything happened
1188 static int hit_bullets(sblist *bullet_list, sblist *target_list) {
1189 uint8_t *bullet_id;
1190 ssize_t li;
1191 int res = 0;
1192 enum bulletsubtype {
1193 BS_BULLET = 0,
1194 BS_FLAME = 1,
1195 BS_GRENADE_EXPL = 2,
1196 BS_BIG_EXPL = 3,
1197 BS_TOUCH,
1198 } bullet_subtybe = BS_BULLET;
1200 sblist_iter_counter2s(bullet_list, li, bullet_id) {
1201 struct gameobj *bullet = &objs[*bullet_id];
1202 if(bullet->objtype == OBJ_FLAME) bullet_subtybe = BS_FLAME;
1203 else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1204 /* grenade kills only in the explosion, not in the smoke phase */
1205 if(bullet->objspecific.bullet.step_curr > 22) continue;
1206 bullet_subtybe = BS_GRENADE_EXPL;
1207 } else if(bullet->objtype == OBJ_BIG_EXPLOSION) {
1208 bullet_subtybe = BS_BIG_EXPL;
1209 } else if(bullet_list == &go_vehicles || bullet_list == &go_enemies ||
1210 bullet_list == &go_boss || bullet_list == &go_mines) {
1211 bullet_subtybe = BS_TOUCH;
1214 vec2f bullet_center = get_gameobj_center(*bullet_id);
1215 if(bullet_list == &go_player_bullets || bullet_list == &go_flames) {
1216 if(is_wall(&bullet_center) == WT_SOLID) goto remove_bullet;
1219 const float bullet_radius[] = { [BS_BULLET] = 1.f, [BS_FLAME] = 6.f,
1220 [BS_GRENADE_EXPL] = 16.f, [BS_BIG_EXPL] = 19.f, [BS_TOUCH] = 8.f, };
1222 size_t lj;
1223 uint8_t *target_id;
1224 sblist_iter_counter2(target_list, lj, target_id) {
1225 struct gameobj *target = &objs[*target_id];
1226 if(is_death_anim(target->animid) && target_list != &go_vehicles) continue;
1227 if(bullet_subtybe == BS_TOUCH &&
1228 overlap(*target_id, *bullet_id) &&
1229 masks_collide(*target_id, *bullet_id)) {
1230 dprintf(2, "hit2\n");
1231 if(bullet_list == &go_boss) return 2;
1232 goto hit;
1235 vec2f temp = get_gameobj_center(*target_id);
1236 float dist1 = vecdist(&bullet_center, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1237 vec2f newpos = vecadd(&bullet_center, &bullet->vel);
1238 float dist2 = vecdist(&newpos, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1240 unsigned w = palpic_getspritewidth(spritemaps[target->spritemap_id]),
1241 h = palpic_getspriteheight(spritemaps[target->spritemap_id]);
1242 float longest_side_div2 = ((float) MAX(h, w) / 2) * SCALE;
1243 if(dist1 < 1.f*SCALE || dist2 < 1.f*SCALE) { dprintf(2, "hit1\n"); goto hit; }
1244 if(dist1 < longest_side_div2 || dist2 < longest_side_div2) {
1245 vec2f velquarter = VEC(bullet->vel.x * 0.25, bullet->vel.y * 0.25);
1246 vec2f point = bullet_center;
1247 size_t k;
1248 for(k = 0; k < 4; k++) {
1249 if(point_in_mask(&point, *target_id)) {
1250 hit:
1252 if(bullet_list == &go_player_bullets) {
1253 if(target_list == &go_vehicles) {
1254 audio_play_wave_resource(wavesounds[WS_DROPSHOT]);
1255 goto remove_bullet;
1257 player_score[0] += 50;
1258 player_kills[0]++;
1259 } else if (bullet_list == &go_rockets) {
1260 init_rocket_explosion(&target->pos);
1261 goto remove_bullet;
1262 } else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1263 // grenade explosion has no effect on vehicles and bunkers.
1264 if(target_list == &go_vehicles || target_list == &go_bunkers)
1265 goto next_bullet;
1266 } else if(bullet->objtype == OBJ_MINE_CROSSED || bullet->objtype == OBJ_MINE_FLAT) {
1267 init_big_explosion(&target->pos);
1268 goto remove_bullet;
1270 enum animation_id death_anim = (bullet_subtybe == BS_FLAME && target_list == &go_enemies) ?
1271 ANIM_ENEMY_BURNT : get_die_anim(*target_id);
1272 if(death_anim == ANIM_INVALID) {
1273 gameobj_free(*target_id);
1274 sblist_delete(target_list, lj);
1275 lj--;
1276 goto next_target;
1278 switch_anim(*target_id, death_anim);
1279 target->vel = VEC(0,0);
1280 if(target->objtype == OBJ_ENEMY_BOMBER || target->objtype == OBJ_ENEMY_SHOOTER) {
1281 const enum wavesound_id wid[] = { WS_SCREAM, WS_SCREAM2 };
1282 audio_play_wave_resource(wavesounds[wid[rand()%2]]);
1283 if(bullet_subtybe == BS_BULLET) {
1284 vec2f bloodpos = vecadd(&target->pos, &VEC(0, (2+rand()%7)*SCALE));
1285 /* original displays at 0,8 but i prefer a random effect */
1286 //vec2f bloodpos = vecadd(&target->pos, &VEC(0, 8*SCALE));
1287 int id = init_blood(&bloodpos);
1288 if(id != -1) add_blood(id);
1291 if(bullet_subtybe == BS_BULLET) {
1292 remove_bullet:
1293 gameobj_free(*bullet_id);
1294 sblist_delete(bullet_list, li);
1295 li--;
1296 goto next_bullet;
1297 } else break;
1299 point = vecadd(&point, &velquarter);
1302 next_target:;
1304 next_bullet:
1305 res = 1;
1307 return res;
1310 uint32_t tickcounter;
1312 static int advance_animations(void) {
1313 size_t i, obj_visited;
1314 int res = 0;
1315 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1316 if(!obj_slot_used[i]) continue;
1317 struct gameobj *go = &objs[i];
1318 if(go->anim_curr == ANIM_STEP_INIT || (go->vel.x != 0 || go->vel.y != 0) || (go->objtype != OBJ_P1 && go->objtype != OBJ_P2) || is_death_anim(go->animid)) {
1319 unsigned anim_delay = go->objtype == OBJ_BIG_EXPLOSION ? 8 : 4;
1320 if(go->anim_curr == ANIM_STEP_INIT || tickcounter % anim_delay == go->anim_frame) {
1321 anim_step last_anim = go->anim_curr;
1322 go->anim_curr = get_next_anim_frame(go->animid, go->anim_curr);
1323 if(last_anim != go->anim_curr) res = 1;
1326 obj_visited++;
1328 return res;
1331 static void switch_enemy_shape(int objid, const struct enemy_route* r) {
1332 switch_anim(objid, enemy_animation_lut[r->shape]);
1335 static void draw_golist(sblist *list) {
1336 size_t i;
1337 uint8_t *itemid;
1338 sblist_iter_counter2(list, i, itemid) {
1339 assert(obj_slot_used[*itemid]);
1340 struct gameobj *o = &objs[*itemid];
1341 const prgb *palette;
1342 switch(o->objtype) {
1343 case OBJ_BLOOD:
1344 // original blood color is bb5511 but that is hardly visible
1345 // palette = (prgb[]) {PRGB(0,0,0), PRGB(0xbb, 0x55, 0x11)};
1346 palette = (const prgb[]) {PRGB(0,0,0), PRGB(0xff, 0x0, 0x0)};
1347 break;
1348 case OBJ_ENEMY_BOMBER: case OBJ_ENEMY_SHOOTER:
1349 palette = map->enemy_palette;
1350 break;
1351 default:
1352 palette = 0;
1354 blit_sprite(o->pos.x, o->pos.y, &video,
1355 SCALE, spritemaps[o->spritemap_id],
1356 get_sprite_number(*itemid),
1357 palette);
1361 static void draw_gameobjs(void) {
1362 draw_golist(&go_mines);
1363 draw_golist(&go_turrets);
1364 draw_golist(&go_bunkers);
1365 draw_golist(&go_vehicles);
1366 draw_golist(&go_enemies);
1367 draw_golist(&go_blood);
1368 draw_golist(&go_enemy_bullets);
1369 draw_golist(&go_boss);
1370 draw_golist(&go_rockets);
1371 draw_golist(&go_players);
1372 draw_golist(&go_player_bullets);
1373 draw_golist(&go_enemy_explosions);
1374 draw_golist(&go_flames);
1375 draw_golist(&go_enemy_flames);
1376 draw_golist(&go_explosions);
1377 draw_golist(&go_grenades);
1378 draw_golist(&go_enemy_grenades);
1379 draw_golist(&go_crosshair);
1380 draw_golist(&go_muzzleflash);
1383 static void process_soldiers(void) {
1384 uint8_t *itemid;
1385 sblist_iter(&go_enemies, itemid) {
1386 struct gameobj *go = &objs[*itemid];
1387 assert(go->objtype == OBJ_ENEMY_SHOOTER || go->objtype == OBJ_ENEMY_BOMBER);
1388 if (tickcounter % 4 == go->anim_frame) {
1389 const struct enemy_route *rc = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1390 if(rc->vel) {
1391 go->objspecific.enemy.curr_step++;
1392 if(enemy_fires(&go->objspecific.enemy)) {
1393 vec2f from = get_gameobj_center(*itemid);
1394 enum direction16 dir = get_shotdirection_from_enemy(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1395 enemy_fire_bullet(dir, 41, go->objspecific.enemy.spawn->weapon, &from);
1397 const struct enemy_route *rn = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1398 if(rn->shape != rc->shape) switch_enemy_shape(*itemid, rn);
1401 if(!is_death_anim(go->animid)) go->vel = get_enemy_vel(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1402 else go->vel = VEC(0, 0);
1406 static enum animation_id get_turret_anim_from_dir(int objid, enum direction dir) {
1407 static const enum animation_id man_anim[] = {
1408 [DIR_N] = ANIM_GUNTURRET_MOVABLE_MAN_N,
1409 [DIR_NO] = ANIM_GUNTURRET_MOVABLE_MAN_NO,
1410 [DIR_O] = ANIM_GUNTURRET_MOVABLE_MAN_O,
1411 [DIR_SO] = ANIM_GUNTURRET_MOVABLE_MAN_SO,
1412 [DIR_S] = ANIM_GUNTURRET_MOVABLE_MAN_S,
1413 [DIR_SW] = ANIM_GUNTURRET_MOVABLE_MAN_SW,
1414 [DIR_W] = ANIM_GUNTURRET_MOVABLE_MAN_W,
1415 [DIR_NW] = ANIM_GUNTURRET_MOVABLE_MAN_NW,
1417 static const enum animation_id mach_anim[] = {
1418 [DIR_N] = ANIM_GUNTURRET_MOVABLE_MACHINE_N,
1419 [DIR_NO] = ANIM_GUNTURRET_MOVABLE_MACHINE_NO,
1420 [DIR_O] = ANIM_GUNTURRET_MOVABLE_MACHINE_O,
1421 [DIR_SO] = ANIM_GUNTURRET_MOVABLE_MACHINE_SO,
1422 [DIR_S] = ANIM_GUNTURRET_MOVABLE_MACHINE_S,
1423 [DIR_SW] = ANIM_GUNTURRET_MOVABLE_MACHINE_SW,
1424 [DIR_W] = ANIM_GUNTURRET_MOVABLE_MACHINE_W,
1425 [DIR_NW] = ANIM_GUNTURRET_MOVABLE_MACHINE_NW,
1427 const enum animation_id *lut = objs[objid].objtype == OBJ_GUNTURRET_MOVABLE_MAN ? man_anim : mach_anim;
1428 return lut[dir];
1431 static int process_turrets(sblist* list) {
1432 int res = 0;
1433 uint8_t *itemid;
1434 sblist_iter(list, itemid) {
1435 struct gameobj *go = &objs[*itemid];
1436 if(is_death_anim(go->animid)) continue;
1437 enum direction16 dir = DIR16_S;
1438 enum enemy_weapon ew = EW_GUN;
1439 int steps = 92;
1440 vec2f from;
1441 switch(go->objtype) {
1442 case OBJ_GUNTURRET_FIXED_NORTH:
1443 dir = DIR16_N;
1444 case OBJ_GUNTURRET_FIXED_SOUTH:
1445 from = get_gameobj_center(*itemid);
1446 shottest:
1447 if(rand()%8 == 0) {
1448 shot:
1449 enemy_fire_bullet(dir, steps, ew, &from);
1450 res = 1;
1452 break;
1453 case OBJ_FLAMETURRET:
1454 ew = EW_FLAME;
1455 steps = 48;
1457 if(objs[player_ids[0]].pos.y <= (192-25)*SCALE/2) dir = DIR16_N;
1458 from = go->pos;
1459 from.y += (dir == DIR16_N ? -8*SCALE : 8*SCALE);
1461 /* abusing curr_step variable to save position in a burst of flames */
1462 if(go->objspecific.enemy.curr_step && go->objspecific.enemy.curr_step < 16)
1463 goto do_flame;
1464 else if(go->objspecific.enemy.curr_step)
1465 go->objspecific.enemy.curr_step = 0;
1467 if(objs[player_ids[0]].pos.x < go->pos.x-32*SCALE || objs[player_ids[0]].pos.x > go->pos.x + (16+32)*SCALE)
1468 break;
1469 if(rand()%8 == 0) {
1470 do_flame:
1471 go->objspecific.enemy.curr_step++;
1472 goto shot;
1474 break;
1475 case OBJ_GUNTURRET_MOVABLE_MAN:
1476 case OBJ_GUNTURRET_MOVABLE_MACHINE: {
1477 from = get_gameobj_center(*itemid);
1478 vec2f p_center = get_gameobj_center(player_ids[0]);
1479 vec2f vel = velocity(&from, &p_center);
1480 enum direction dir8 = get_direction_from_vec(&vel);
1481 dir = get_direction16_from_direction(dir8);
1482 switch_anim(*itemid, get_turret_anim_from_dir(*itemid, dir8));
1483 goto shottest;
1484 break;
1486 case OBJ_TANK_SMALL:
1487 case OBJ_BUNKER1:
1488 case OBJ_BUNKER2:
1489 case OBJ_BUNKER3:
1490 case OBJ_BUNKER4:
1491 case OBJ_BUNKER5: {
1492 const vec2f *bunkerpos;
1493 const enum direction16 *bunkerdir;
1494 unsigned count, sec;
1495 static const enum direction16 bunker5_dir[] = {
1496 [0] = DIR16_N, [1] = DIR16_NO, [2] = DIR16_O, [3] = DIR16_SO,
1497 [4] = DIR16_S, [5] = DIR16_SW, [6] = DIR16_W, [7] = DIR16_NW,
1499 static const enum direction16 bunker1_dir[] = {
1500 [0] = DIR16_NNW, [1] = DIR16_NNO, [2] = DIR16_NO, [3] = DIR16_O,
1501 [4] = DIR16_SO, [5] = DIR16_SSO, [6] = DIR16_SSW, [7] = DIR16_SW,
1502 [8] = DIR16_W, [9] = DIR16_NW,
1504 static const vec2f bunker5_pos[] = {
1505 [0] = VEC(9,-8), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(22,10),
1506 [4] = VEC(9,16), [5] = VEC(-5,13), [6] = VEC(-7,2), [7] = VEC(-5,-6),
1508 static const vec2f bunker2_pos[] = {
1509 [0] = VEC(15,-2), [1] = VEC(27,3), [2] = VEC(31,12), [3] = VEC(28,23),
1510 [4] = VEC(15,27), [5] = VEC(2,23), [6] = VEC(0,12), [7] = VEC(4,2),
1512 static const vec2f bunker4_pos[] = {
1513 [0] = VEC(13,0), [1] = VEC(25,1), [2] = VEC(29,10), [3] = VEC(26,21),
1514 [4] = VEC(13,25), [5] = VEC(0,21), [6] = VEC(-2,10), [7] = VEC(2,0),
1516 static const vec2f bunker1_pos[] = {
1517 [0] = VEC(6,-6), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(26,12),
1518 [4] = VEC(26,16), [5] = VEC(20,24), [6] = VEC(6,24), [7] = VEC(0,20),
1519 [8] = VEC(0,12), [9] = VEC(0,2),
1521 static const enum direction16 tank_dir[] = {
1522 [0] = DIR16_WSW, [1] = DIR16_OSO, [2] = DIR16_WSW, [3] = DIR16_OSO,
1523 [4] = DIR16_WSW, [5] = DIR16_OSO, [6] = DIR16_SSW, [7] = DIR16_SSO,
1525 static const vec2f tank_pos[] = {
1526 [0] = VEC(10,8), [1] = VEC(38,8), [2] = VEC(10,20), [3] = VEC(38,20),
1527 [4] = VEC(10,32), [5] = VEC(38,32), [6] = VEC(16,40), [7] = VEC(32,40),
1529 unsigned b, dist = 28;
1530 switch(go->objtype) {
1531 case OBJ_BUNKER1:
1532 ew = EW_GRENADE;
1533 bunkerpos = bunker1_pos;
1534 bunkerdir = bunker1_dir;
1535 b = 0;
1536 count = 10;
1537 sec = 2;
1538 break;
1539 case OBJ_TANK_SMALL:
1540 if(tickcounter % 8) break;
1541 ew = EW_GUN;
1542 dist = 128;
1543 bunkerpos = tank_pos;
1544 bunkerdir = tank_dir;
1545 // FIXME: the last 2 shoots (6+7) should be fired at the same time
1546 goto bunker_circle_shoot;
1547 case OBJ_BUNKER2:
1548 if(tickcounter % 8) break;
1549 ew = EW_GUN;
1550 dist = 128;
1551 bunkerpos = bunker2_pos;
1552 bunkerdir = bunker5_dir;
1553 goto bunker_circle_shoot;
1554 case OBJ_BUNKER4:
1555 if(tickcounter % /*(fps*3.5)/8*/ 24) break;
1556 ew = EW_GRENADE;
1557 dist = 32;
1558 bunkerpos = bunker4_pos;
1559 bunkerdir = bunker5_dir;
1560 bunker_circle_shoot:;
1561 b = go->objspecific.enemy.curr_step;
1562 count = b+1;
1563 if(++go->objspecific.enemy.curr_step >= 8) go->objspecific.enemy.curr_step = 0;
1564 goto bunkerloop;
1565 case OBJ_BUNKER3:
1566 ew = EW_GUN;
1567 bunkerpos = bunker2_pos;
1568 bunkerdir = bunker5_dir;
1569 b = 0;
1570 count = 8;
1571 dist = 128;
1572 sec = 1;
1573 break;
1574 case OBJ_BUNKER5:
1575 ew = EW_FLAME;
1576 bunkerpos = bunker5_pos;
1577 bunkerdir = bunker5_dir;
1578 b = 0;
1579 count = 8;
1580 sec = 3;
1581 break;
1582 default:;
1584 if(tickcounter > (uint32_t) go->objspecific.enemy.curr_step + fps*sec) {
1585 go->objspecific.enemy.curr_step = tickcounter;
1586 bunkerloop:;
1587 for(; b < count; b++) {
1588 from = go->pos;
1589 from.x += bunkerpos[b].x*SCALE;
1590 from.y += bunkerpos[b].y*SCALE;
1591 enemy_fire_bullet(bunkerdir[b], dist, ew, &from);
1593 res = 1;
1595 break;
1597 default:;
1600 return res;
1603 static int move_gameobjs(void) {
1604 int res = 0;
1605 size_t i, obj_visited;
1606 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1607 if(obj_slot_used[i]) {
1608 struct gameobj *go = &objs[i];
1609 obj_visited++;
1610 if(go->anim_curr == ANIM_STEP_INIT) res = 1;
1611 if(go->vel.x != 0 || go->vel.y != 0) {
1612 vec2f oldpos = go->pos;
1613 go->pos.x += go->vel.x;
1614 go->pos.y += go->vel.y;
1616 if(go->objtype == OBJ_P1 || go->objtype == OBJ_P2) {
1617 if(go->pos.y < SCREEN_MIN_Y) go->pos.y = SCREEN_MIN_Y;
1618 else if(go->pos.y+25*SCALE > SCREEN_MAX_Y) go->pos.y = SCREEN_MAX_Y-25*SCALE;
1619 if(go->pos.x < SCREEN_MIN_X) go->pos.x = SCREEN_MIN_X;
1620 else if(go->pos.x+32*SCALE > SCREEN_MAX_X) go->pos.x = SCREEN_MAX_X-32*SCALE;
1621 vec2f center = get_sprite_center(spritemaps[go->spritemap_id]);
1622 center = vecadd(&center, &go->pos);
1623 if(is_wall(&center)) {
1624 go->pos = oldpos;
1625 go->vel = VEC(0,0);
1629 res = 1;
1631 if((go->objtype == OBJ_ENEMY_BOMBER || go->objtype == OBJ_ENEMY_SHOOTER) &&
1632 go->anim_curr == animations[go->animid].last &&
1633 (go->animid == ANIM_ENEMY_BOMBER_DIE ||
1634 go->animid == ANIM_ENEMY_GUNNER_DIE ||
1635 go->animid == ANIM_ENEMY_BURNT)) {
1636 dprintf(2, "removed enemy from %.2f,%.2f\n", go->pos.x, go->pos.y);
1637 gameobj_free(i);
1638 golist_remove(&go_enemies, i);
1639 res = 1;
1640 continue;
1645 return res;
1648 static void game_update_caption(void) {
1649 char buf [128];
1650 snprintf(buf, 128, "objs: %d, map x,y %d/%d, index %d, xoff %d, yoff %d, spawnscreen %d, line %d", (int) obj_count,
1651 (int)mapsquare.x, (int)mapsquare.y, (int)map->screen_map[mapsquare.y][mapsquare.x],
1652 (int)mapscreen_xoff, (int)mapscreen_yoff, (int)map_spawn_screen_index, (int) map_spawn_line);
1653 SDL_WM_SetCaption(buf, 0);
1655 static void(*update_caption)(void) = game_update_caption;
1657 /* returns 1 if level finished */
1658 static int game_tick(int force_redraw) {
1659 int need_redraw = force_redraw;
1660 if(mousebutton_down[MB_LEFT] > 1) {
1661 const int player_no = 0;
1662 const struct weapon *pw = get_active_weapon(player_no);
1663 //if(get_active_weapon_id(player_no) == WP_M134) __asm__("int3");
1664 if (pw->flags & WF_AUTOMATIC) {
1665 float shots_per_second = pw->rpm / 60.f;
1666 float shotinterval = fps / shots_per_second;
1667 if((int)((float)mousebutton_down[MB_LEFT] / shotinterval) != (int)((float)(mousebutton_down[MB_LEFT]-1) / shotinterval))
1668 fire_bullet(player_no);
1672 if(advance_animations()) need_redraw = 1;
1673 if(hit_bullets(&go_player_bullets, &go_enemies)) need_redraw = 1;
1674 if(hit_bullets(&go_player_bullets, &go_vehicles)) need_redraw = 1;
1675 if(hit_bullets(&go_flames, &go_enemies)) need_redraw = 1;
1676 if(hit_bullets(&go_enemy_flames, &go_enemies)) need_redraw = 1;
1677 if(hit_bullets(&go_rockets, &go_enemies)) need_redraw = 1;
1678 if(hit_bullets(&go_rockets, &go_vehicles)) need_redraw = 1;
1679 if(hit_bullets(&go_explosions, &go_enemies)) need_redraw = 1;
1680 if(hit_bullets(&go_explosions, &go_vehicles)) need_redraw = 1;
1681 if(hit_bullets(&go_explosions, &go_mines)) need_redraw = 1;
1682 if(hit_bullets(&go_explosions, &go_turrets)) need_redraw = 1;
1683 if(hit_bullets(&go_explosions, &go_bunkers)) need_redraw = 1;
1684 if(hit_bullets(&go_explosions, &go_players)) need_redraw = 1;
1685 if(hit_bullets(&go_enemy_explosions, &go_players)) need_redraw = 1;
1686 if(hit_bullets(&go_enemy_bullets, &go_players)) need_redraw = 1;
1687 if(hit_bullets(&go_enemy_flames, &go_players)) need_redraw = 1;
1688 if(hit_bullets(&go_vehicles, &go_players)) need_redraw = 1;
1689 if(hit_bullets(&go_enemies, &go_players)) need_redraw = 1;
1690 if(hit_bullets(&go_mines, &go_players)) need_redraw = 1;
1692 int ret, level_finished = 0;
1693 if((ret = hit_bullets(&go_boss, &go_players)) == 2) level_finished = 1;
1694 else if(ret == 1) need_redraw = 1;
1696 if(remove_bullets(&go_player_bullets)) need_redraw = 1;
1697 if(remove_bullets(&go_flames)) need_redraw = 1;
1698 if(remove_bullets(&go_explosions)) need_redraw = 1;
1699 if(remove_bullets(&go_enemy_explosions)) need_redraw = 1;
1700 if(remove_bullets(&go_enemy_bullets)) need_redraw = 1;
1701 if(remove_bullets(&go_enemy_flames)) need_redraw = 1;
1702 if(remove_bullets(&go_muzzleflash)) need_redraw = 1;
1703 if(remove_bullets(&go_blood)) need_redraw = 1;
1704 if(remove_explosives(&go_grenades)) need_redraw = 1;
1705 if(remove_explosives(&go_enemy_grenades)) need_redraw = 1;
1706 if(remove_explosives(&go_rockets)) need_redraw = 1;
1707 if(tickcounter % 2 == 0 && scroll_map()) need_redraw = 1;
1709 process_soldiers();
1710 if(tickcounter % 4 == 0 && process_turrets(&go_turrets)) need_redraw = 1;
1711 if(tickcounter % 4 == 0 && process_turrets(&go_bunkers)) need_redraw = 1;
1712 if(tickcounter % 4 == 0 && process_turrets(&go_vehicles)) need_redraw = 1;
1714 if(move_gameobjs()) need_redraw = 1;
1716 if(remove_offscreen_objects(&go_enemies)) need_redraw = 1;
1717 if(remove_offscreen_objects(&go_vehicles)) need_redraw = 1;
1718 if(remove_offscreen_objects(&go_mines)) need_redraw = 1;
1719 if(remove_offscreen_objects(&go_turrets)) need_redraw = 1;
1720 if(remove_offscreen_objects(&go_bunkers)) need_redraw = 1;
1722 long ms_used = 0;
1723 struct timeval timer;
1724 gettimestamp(&timer);
1725 if(need_redraw) {
1726 draw_map();
1727 draw_gameobjs();
1728 draw_status_bar();
1729 video_update_region(SCREEN_MIN_X ,SCREEN_MIN_Y , SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1732 ms_used = mspassed(&timer);
1733 //if(ms_used) printf("repaint took: ms_used %ld\n", ms_used);
1734 int res = audio_process();
1735 if(res == -1) music_restart();
1736 ms_used = mspassed(&timer);
1737 //if(ms_used) printf("audio processed: %d, ms_used %ld\n", res, ms_used);
1739 long sleepms = 1000/fps - ms_used;
1740 if(sleepms >= 0) SDL_Delay(sleepms);
1741 if(mousebutton_down[MB_LEFT]) mousebutton_down[MB_LEFT]++;
1743 tickcounter++;
1744 update_caption();
1746 return level_finished;
1749 enum cursor {
1750 c_down = 0,
1751 c_up,
1752 c_left,
1753 c_right,
1756 static enum cursor cursor_lut[] = {
1757 [SDLK_UP] = c_up,
1758 [SDLK_DOWN] = c_down,
1759 [SDLK_LEFT] = c_left,
1760 [SDLK_RIGHT] = c_right,
1761 [SDLK_w] = c_up,
1762 [SDLK_a] = c_left,
1763 [SDLK_s] = c_down,
1764 [SDLK_d] = c_right,
1767 static char cursors_pressed[] = {
1768 [c_up] = 0,
1769 [c_down] = 0,
1770 [c_left] = 0,
1771 [c_right] = 0,
1774 static vec2f get_vel_from_direction(enum direction dir, float speed) {
1775 #define VELLUT(a, b, c) [a] = VEC(b, c)
1776 static const vec2f vel_lut[] = {
1777 VELLUT(DIR_O, 1, 0),
1778 VELLUT(DIR_NO, 0.7071067769704655, -0.7071067769704655),
1779 VELLUT(DIR_N, 0, -1),
1780 VELLUT(DIR_NW, -0.7071067769704655, -0.7071067769704655),
1781 VELLUT(DIR_W, -1, 0),
1782 VELLUT(DIR_SW, -0.7071067769704655, 0.7071067769704655),
1783 VELLUT(DIR_S, 0, 1),
1784 VELLUT(DIR_SO, 0.7071067769704655, 0.7071067769704655),
1786 #undef VELLUT
1787 vec2f v = vel_lut[dir];
1788 v.x *= speed * SCALE;
1789 v.y *= speed * SCALE;
1790 return v;
1793 static vec2f get_vel_from_direction16(enum direction16 dir, float speed) {
1794 #define VELLUT(a, b, c) [a] = VEC(b, c)
1795 #define ANK90 0.7071067769704655
1796 #define GK90 ANK90
1797 #define ANK45 0.9238795042037964
1798 #define GK45 0.3826834261417389
1799 static const vec2f vel_lut[] = {
1800 VELLUT(DIR16_O, 1, 0),
1801 VELLUT(DIR16_ONO, ANK45, -GK45),
1802 VELLUT(DIR16_NO, ANK90, -ANK90),
1803 VELLUT(DIR16_NNO, GK45, -ANK45),
1804 VELLUT(DIR16_N, 0, -1),
1805 VELLUT(DIR16_NNW, -GK45, -ANK45),
1806 VELLUT(DIR16_NW, -ANK90, -ANK90),
1807 VELLUT(DIR16_WNW, -ANK45, -GK45),
1808 VELLUT(DIR16_W, -1, 0),
1809 VELLUT(DIR16_WSW, -ANK45, GK45),
1810 VELLUT(DIR16_SW, -ANK90, ANK90),
1811 VELLUT(DIR16_SSW, -GK45, ANK45),
1812 VELLUT(DIR16_S, 0, 1),
1813 VELLUT(DIR16_SSO, GK45, ANK45),
1814 VELLUT(DIR16_SO, ANK90, ANK90),
1815 VELLUT(DIR16_OSO, ANK45, GK45),
1817 #undef VELLUT
1818 vec2f v = vel_lut[dir];
1819 v.x *= speed * SCALE;
1820 v.y *= speed * SCALE;
1821 return v;
1825 static enum direction get_direction_from_vec(vec2f *vel) {
1826 float deg_org, deg = atan2(vel->y, vel->x);
1827 deg_org = deg;
1828 if(deg < 0) deg *= -1.f;
1829 else if(deg > 0) deg = M_PI + (M_PI - deg); // normalize atan2 result to scale from 0 to 2 pi
1830 int hexadrant = (int)(deg / ((1.0/16.0f)*2*M_PI));
1831 assert(hexadrant >= 0 && hexadrant < 16);
1832 static const enum direction rad_lut[] = {
1833 DIR_O,
1834 DIR_NO, DIR_NO,
1835 DIR_N, DIR_N,
1836 DIR_NW, DIR_NW,
1837 DIR_W, DIR_W,
1838 DIR_SW, DIR_SW,
1839 DIR_S, DIR_S,
1840 DIR_SO, DIR_SO,
1841 DIR_O
1843 return rad_lut[hexadrant];
1846 static enum direction get_direction_from_cursor(void) {
1847 enum direction dir = DIR_INVALID;
1848 if(cursors_pressed[c_up]) {
1849 if(cursors_pressed[c_left]) dir = DIR_NW;
1850 else if(cursors_pressed[c_right]) dir = DIR_NO;
1851 else dir = DIR_N;
1852 } else if (cursors_pressed[c_down]) {
1853 if(cursors_pressed[c_left]) dir = DIR_SW;
1854 else if(cursors_pressed[c_right]) dir = DIR_SO;
1855 else dir = DIR_S;
1856 } else if (cursors_pressed[c_left]) {
1857 dir = DIR_W;
1858 } else if (cursors_pressed[c_right]) {
1859 dir = DIR_O;
1861 return dir;
1864 static enum animation_id get_anim_from_direction(enum direction dir, int player_no, int throwing) {
1865 #define DIRMAP(a, b) [a] = b
1866 if(!throwing) {
1867 static const enum animation_id dir_map_p1[] = {
1868 DIRMAP(DIR_N, ANIM_P1_MOVE_N),
1869 DIRMAP(DIR_NW, ANIM_P1_MOVE_NW),
1870 DIRMAP(DIR_W, ANIM_P1_MOVE_W),
1871 DIRMAP(DIR_SW, ANIM_P1_MOVE_SW),
1872 DIRMAP(DIR_S, ANIM_P1_MOVE_S),
1873 DIRMAP(DIR_SO, ANIM_P1_MOVE_SO),
1874 DIRMAP(DIR_O, ANIM_P1_MOVE_O),
1875 DIRMAP(DIR_NO, ANIM_P1_MOVE_NO),
1877 static const enum animation_id dir_map_p2[] = {
1878 DIRMAP(DIR_N, ANIM_P2_MOVE_N),
1879 DIRMAP(DIR_NW, ANIM_P2_MOVE_NW),
1880 DIRMAP(DIR_W, ANIM_P2_MOVE_W),
1881 DIRMAP(DIR_SW, ANIM_P2_MOVE_SW),
1882 DIRMAP(DIR_S, ANIM_P2_MOVE_S),
1883 DIRMAP(DIR_SO, ANIM_P2_MOVE_SO),
1884 DIRMAP(DIR_O, ANIM_P2_MOVE_O),
1885 DIRMAP(DIR_NO, ANIM_P2_MOVE_NO),
1887 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1 : dir_map_p2;
1888 return dir_map[dir];
1889 } else {
1890 static const enum animation_id dir_map_p1_g[] = {
1891 DIRMAP(DIR_N, ANIM_P1_THROW_N),
1892 DIRMAP(DIR_NW, ANIM_P1_THROW_NW),
1893 DIRMAP(DIR_W, ANIM_P1_THROW_W),
1894 DIRMAP(DIR_SW, ANIM_P1_THROW_SW),
1895 DIRMAP(DIR_S, ANIM_P1_THROW_S),
1896 DIRMAP(DIR_SO, ANIM_P1_THROW_SO),
1897 DIRMAP(DIR_O, ANIM_P1_THROW_O),
1898 DIRMAP(DIR_NO, ANIM_P1_THROW_NO),
1900 static const enum animation_id dir_map_p2_g[] = {
1901 DIRMAP(DIR_N, ANIM_P2_THROW_N),
1902 DIRMAP(DIR_NW, ANIM_P2_THROW_NW),
1903 DIRMAP(DIR_W, ANIM_P2_THROW_W),
1904 DIRMAP(DIR_SW, ANIM_P2_THROW_SW),
1905 DIRMAP(DIR_S, ANIM_P2_THROW_S),
1906 DIRMAP(DIR_SO, ANIM_P2_THROW_SO),
1907 DIRMAP(DIR_O, ANIM_P2_THROW_O),
1908 DIRMAP(DIR_NO, ANIM_P2_THROW_NO),
1910 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1_g : dir_map_p2_g;
1911 return dir_map[dir];
1913 #undef DIRMAP
1916 #if 0
1917 static enum animation_id get_anim_from_cursor(void) {
1918 enum direction dir = get_direction_from_cursor();
1919 if(dir == DIR_INVALID) return ANIM_INVALID;
1920 return get_anim_from_direction(dir, 0, 0);
1922 /* playerno is either 0 or 1, not player_id! */
1923 static enum animation_id get_anim_from_vel(int player_no, vec2f *vel) {
1924 enum direction dir = get_direction_from_vec(vel);
1925 if(dir == DIR_INVALID) return ANIM_INVALID;
1926 return get_anim_from_direction(dir, player_no, 0);
1928 #endif
1930 static void switch_anim(int obj_id, int aid) {
1931 if(objs[obj_id].animid == aid) return;
1932 gameobj_start_anim(obj_id, aid);
1935 enum map_index choose_mission(uint8_t* completed);
1936 //RcB: DEP "mission_select.c"
1937 #include "enemytag.c"
1939 static uint8_t mission_completed[MI_MAX];
1941 static void game_delay(int s) {
1942 /* wait for s frames while continuing to play music */
1943 int x = s;
1944 while(x){
1945 audio_process();
1946 SDL_Delay(1000/fps);
1947 x--;
1948 tickcounter++;
1952 static void finish_level(void) {
1953 music_play(TUNE_TITLE);
1954 video_darken_screen();
1955 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1956 #define STRLSZ(x) (x), sizeof(x)-1
1957 font_print(SCREEN_MIN_X+40*SCALE, SCREEN_MIN_Y+13*SCALE, STRLSZ("congratulations"), SCALE, PRGB(255,255,255));
1958 font_print(SCREEN_MIN_X+24*SCALE, SCREEN_MIN_Y+24*SCALE, STRLSZ("you have completed"), SCALE, PRGB(255,255,255));
1959 font_print(SCREEN_MIN_X+48*SCALE, SCREEN_MIN_Y+35*SCALE, STRLSZ("your mission"), SCALE, PRGB(255,255,255));
1960 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1961 game_delay(2*fps);
1962 uint32_t bgbuf0[8*SCALE*((95-40)*SCALE)];
1963 video_save_rect(SCREEN_MIN_X+40*SCALE,SCREEN_MIN_Y+65*SCALE,(95-40)*SCALE,8*SCALE,bgbuf0);
1965 font_print(SCREEN_MIN_X+40*SCALE, SCREEN_MIN_Y+65*SCALE, STRLSZ("mission bonus"), SCALE, PRGB(255,255,255));
1967 font_print(SCREEN_MIN_X+32*SCALE, SCREEN_MIN_Y+89*SCALE, STRLSZ("1."), SCALE, PRGB(255,255,255));
1968 uint32_t bgbuf[8*SCALE*((159-64)*SCALE)];
1969 video_save_rect(SCREEN_MIN_X+64*SCALE,SCREEN_MIN_Y+89*SCALE,(159-64)*SCALE,8*SCALE,bgbuf);
1971 char buf[16];
1972 int dollars = player_cash[0];
1973 snprintf(buf, sizeof buf, " $%.6d", dollars);
1974 font_print(SCREEN_MIN_X+64*SCALE, SCREEN_MIN_Y+89*SCALE, buf, 12, SCALE, PRGB(255,255,255));
1975 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1976 game_delay(1*fps);
1978 // TODO: play mission bonus sound
1979 dollars += maps[current_map]->rewardk*500;
1980 video_restore_rect(SCREEN_MIN_X+64*SCALE,SCREEN_MIN_Y+89*SCALE,(159-64)*SCALE,8*SCALE,bgbuf);
1981 snprintf(buf, sizeof buf, " $%.6d", dollars);
1982 font_print(SCREEN_MIN_X+64*SCALE, SCREEN_MIN_Y+89*SCALE, buf, 12, SCALE, PRGB(255,255,255));
1983 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1984 game_delay(1*fps);
1987 video_restore_rect(SCREEN_MIN_X+40*SCALE,SCREEN_MIN_Y+65*SCALE,(95-40)*SCALE,8*SCALE,bgbuf0);
1988 font_print(SCREEN_MIN_X+40*SCALE, SCREEN_MIN_Y+65*SCALE, STRLSZ("revenge bonus"), SCALE, PRGB(255,255,255));
1990 snprintf(buf, sizeof buf, "%.4d $%.6d", player_kills[0], dollars);
1991 font_print(SCREEN_MIN_X+64*SCALE, SCREEN_MIN_Y+89*SCALE, buf, 12, SCALE, PRGB(255,255,255));
1992 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1993 game_delay(1*fps);
1994 tickcounter = 0;
1995 while(player_kills[0]) {
1996 dollars+=10;
1997 player_kills[0]--;
1998 video_restore_rect(SCREEN_MIN_X+64*SCALE,SCREEN_MIN_Y+89*SCALE,(159-64)*SCALE,8*SCALE,bgbuf);
1999 snprintf(buf, sizeof buf, "%.4d $%.6d", player_kills[0], dollars);
2000 font_print(SCREEN_MIN_X+64*SCALE, SCREEN_MIN_Y+89*SCALE, buf, 12, SCALE, PRGB(255,255,255));
2001 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
2002 if(tickcounter % 4 == 0) audio_play_wave_resource(wavesounds[WS_COUNTDOWN]);
2003 // FIXME: when no music is played, sound does not play either... maybe add "empty song"?
2004 game_delay(1);
2006 music_play(TUNE_LEVEL_FINISHED);
2007 game_delay(3*fps);
2009 mission_completed[current_map] = 1;
2010 player_cash[0] = dollars;
2013 #include "weapon_shop.c"
2015 int main() {
2016 video_init();
2017 clear_screen();
2018 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2019 SDL_EnableKeyRepeat(100, 20);
2021 audio_init();
2022 init_player_once(0);
2024 mission_select:
2026 SDL_ShowCursor(1);
2028 /* background music for mission selection screen */
2029 music_play(TUNE_MAP);
2031 if((current_map = choose_mission(mission_completed)) == MI_INVALID) goto dun_goofed;
2033 player_cash[0] += maps[current_map]->rewardk*500;
2035 weapon_shop();
2037 music_play(TUNE_FIGHTING);
2039 SDL_ShowCursor(0);
2041 int startx = 10;
2042 int starty = 10;
2044 //redraw(surface, startx, starty);
2045 const float player_speed = 1.25f;
2046 const struct palpic* spritemap = spritemaps[SI_PLAYERS];
2047 struct { int *target; int dir; int max;} moves[] = {
2048 [c_up] = {&starty, SCALE * -1, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
2049 [c_down] = {&starty, SCALE, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
2050 [c_left] = {&startx, SCALE * -1, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
2051 [c_right] = {&startx, SCALE, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
2054 init_game_objs();
2055 int player_no = 0;
2056 int player_id = player_ids[player_no];
2058 game_tick(1);
2060 SDL_Event sdl_event;
2061 while(1) {
2062 unsigned need_redraw = 0;
2063 int weapon_inc = 0;
2064 //if(tickcounter>64) { player_kills[0] = 150; finish_level();}
2065 while (SDL_PollEvent(&sdl_event)) {
2066 need_redraw = 0;
2067 switch (sdl_event.type) {
2068 case SDL_MOUSEMOTION:
2069 if((int)mousepos->x != sdl_event.motion.x || (int)mousepos->y != sdl_event.motion.y)
2070 need_redraw = 1;
2071 mousepos->x = sdl_event.motion.x;
2072 mousepos->y = sdl_event.motion.y;
2073 break;
2074 case SDL_MOUSEBUTTONDOWN:
2075 mousepos->x = sdl_event.button.x;
2076 mousepos->y = sdl_event.button.y;
2077 mousebutton_down[MB_LEFT] = 1;
2078 fire_bullet(player_no);
2079 break;
2080 case SDL_MOUSEBUTTONUP:
2081 mousepos->x = sdl_event.button.x;
2082 mousepos->y = sdl_event.button.y;
2083 mousebutton_down[MB_LEFT] = 0;
2084 break;
2085 case SDL_QUIT:
2086 dun_goofed:
2087 video_cleanup();
2088 return 0;
2089 case SDL_KEYDOWN:
2090 switch(sdl_event.key.keysym.sym) {
2091 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
2092 case SDLK_UP:
2093 case SDLK_DOWN:
2094 case SDLK_RIGHT:
2095 case SDLK_LEFT:
2096 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 1;
2097 check_anim: {
2098 enum direction dir = get_direction_from_cursor();
2099 if(dir != DIR_INVALID) {
2100 if(!mousebutton_down[MB_LEFT]) {
2101 // change animation according to cursors,
2102 // unless we're in automatic fire mode.
2103 enum animation_id aid = get_anim_from_direction(dir, player_no, 0);
2104 if(aid != ANIM_INVALID) switch_anim(player_id, aid);
2106 objs[player_id].vel = get_vel_from_direction(dir, player_speed);
2107 } else {
2108 objs[player_id].vel = VEC(0,0);
2111 break;
2112 case SDLK_RETURN:
2113 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
2114 (sdl_event.key.keysym.mod & KMOD_RALT)) {
2115 toggle_fullscreen();
2116 SDL_Delay(1);
2117 game_tick(1);
2118 need_redraw = 1;
2120 break;
2121 case SDLK_KP_PLUS:
2122 weapon_inc = 1;
2123 goto toggle_weapon;
2124 case SDLK_KP_MINUS:
2125 if((int)player_weapons[player_no][0] == 0)
2126 weapon_inc = WP_MAX-1;
2127 else weapon_inc = -1;
2128 toggle_weapon:
2129 player_weapons[player_no][0] += weapon_inc;
2130 if(player_weapons[player_no][0] == WP_INVALID)
2131 player_weapons[player_no][0] = 0;
2132 printf("%s\n", weapon_name(player_weapons[player_no][0]));
2133 need_redraw = 1;
2134 break;
2135 default:
2136 break;
2138 break;
2139 case SDL_KEYUP:
2140 switch(sdl_event.key.keysym.sym) {
2141 case SDLK_e:
2142 enemy_tag_loop();
2143 update_caption = game_update_caption;
2144 break;
2145 case SDLK_c:
2146 clear_screen();
2147 video_update();
2148 need_redraw = 1;
2149 break;
2150 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
2151 case SDLK_UP:
2152 case SDLK_DOWN:
2153 case SDLK_RIGHT:
2154 case SDLK_LEFT:
2155 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 0;
2156 goto check_anim;
2157 case SDLK_ESCAPE:
2158 goto dun_goofed;
2159 default:
2160 break;
2162 default:
2163 break;
2166 unsigned i;
2167 for (i = 0; i < ARRAY_SIZE(cursors_pressed); i++) {
2168 if(cursors_pressed[i]) {
2169 *moves[i].target += moves[i].dir;
2170 if(*moves[i].target < 0) *moves[i].target = 0;
2171 if(*moves[i].target > moves[i].max) *moves[i].target = moves[i].max;
2172 need_redraw = 1;
2175 if(game_tick(need_redraw)) {
2176 /* completed map */
2177 finish_level();
2178 goto mission_select;
2182 return 0;