make mission end screen more accurate
[rofl0r-openDOW.git] / spriteview.c
blobfdbf49a95526a3d9df0d1e5ced189207d0147ff6
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"
32 #include "weapon_sprites.c"
34 #endif
36 enum mousebutton {
37 MB_LEFT = 0,
38 MB_RIGHT,
41 // 1 if button down, 0 if not, >1 to count ms pressed
42 unsigned long mousebutton_down[] = {
43 [MB_LEFT] = 0,
44 [MB_RIGHT] = 0,
47 //RcB: LINK "-lSDL"
48 #if 0
49 static void get_last_move_event(SDL_Event* e) {
50 #define numpeek 32
51 SDL_Event peek[numpeek];
52 SDL_Event* last_event = NULL;
53 int i, results;
54 results = SDL_PeepEvents(peek, numpeek, SDL_PEEKEVENT, (uint32_t) ~0);
55 if(results == -1) return;
56 for(i = 0; i < results; i++) {
57 if(peek[i].type == SDL_MOUSEMOTION)
58 last_event = &peek[i];
59 else
60 break;
62 if(last_event) {
63 *e = *last_event;
64 SDL_PeepEvents(peek, i + 1, SDL_GETEVENT, (uint32_t) ~0);
66 #undef numpeek
68 #endif
70 static vec2f get_sprite_center(const struct palpic *p) {
71 assert(p->spritecount);
72 vec2f res;
73 res.x = palpic_getspritewidth(p) * SCALE / 2;
74 res.y = palpic_getspriteheight(p) * SCALE / 2;
75 return res;
78 static int player_ids[2];
79 static int player_kills[2];
80 static enum weapon_id player_weapons[2][WP_MAX];
81 static int weapon_count[2];
82 static enum weapon_id weapon_active[2]; // index into player_weapons[playerno]
83 static int player_ammo[2][AMMO_MAX];
84 static enum weapon_id get_active_weapon_id(int player_no);
85 static void switch_anim(int obj_id, int aid);
86 static vec2f get_vel_from_direction(enum direction dir, float speed);
87 static vec2f get_vel_from_direction16(enum direction16 dir, float speed);
88 // used by game_tick
89 static sblist go_player_bullets;
90 static sblist go_enemy_bullets;
91 static sblist go_explosions;
92 static sblist go_enemy_explosions;
93 static sblist go_enemies;
94 static sblist go_players;
95 static sblist go_flames;
96 static sblist go_enemy_flames;
97 static sblist go_rockets;
98 static sblist go_grenades;
99 static sblist go_enemy_grenades;
100 static sblist go_vehicles;
101 static sblist go_mines;
102 static sblist go_turrets;
103 static sblist go_bunkers;
104 static sblist go_boss;
105 static sblist go_crosshair;
106 static sblist go_muzzleflash;
107 static sblist go_blood;
108 static void add_pbullet(uint8_t bullet_id) {
109 sblist_add(&go_player_bullets, &bullet_id);
111 static void add_ebullet(uint8_t bullet_id) {
112 sblist_add(&go_enemy_bullets, &bullet_id);
114 static void add_player(uint8_t player_id) {
115 sblist_add(&go_players, &player_id);
117 static void add_enemy(uint8_t enem_id) {
118 sblist_add(&go_enemies, &enem_id);
120 static void add_explosion(uint8_t expl_id) {
121 sblist_add(&go_explosions, &expl_id);
123 static void add_enemy_explosion(uint8_t expl_id) {
124 sblist_add(&go_enemy_explosions, &expl_id);
126 static void add_flame(uint8_t id) {
127 sblist_add(&go_flames, &id);
129 static void add_grenade(uint8_t id) {
130 sblist_add(&go_grenades, &id);
132 static void add_enemy_grenade(uint8_t id) {
133 sblist_add(&go_enemy_grenades, &id);
135 static void add_rocket(uint8_t id) {
136 sblist_add(&go_rockets, &id);
138 static void add_vehicle(uint8_t id) {
139 sblist_add(&go_vehicles, &id);
141 static void add_mine(uint8_t id) {
142 sblist_add(&go_mines, &id);
144 static void add_turret(uint8_t id) {
145 sblist_add(&go_turrets, &id);
147 static void add_bunker(uint8_t id) {
148 sblist_add(&go_bunkers, &id);
150 static void add_boss(uint8_t id) {
151 sblist_add(&go_boss, &id);
153 static void add_crosshair(uint8_t id) {
154 sblist_add(&go_crosshair, &id);
156 static void add_muzzleflash(uint8_t id) {
157 sblist_add(&go_muzzleflash, &id);
159 static void add_blood(uint8_t id) {
160 sblist_add(&go_blood, &id);
162 static void add_enemy_flame(uint8_t id) {
163 sblist_add(&go_enemy_flames, &id);
165 static void golist_remove(sblist *l, uint8_t objid) {
166 size_t i;
167 uint8_t *itemid;
168 sblist_iter_counter2(l, i, itemid) {
169 if(*itemid == objid) {
170 sblist_delete(l, i);
171 return;
176 static int get_next_anim_frame(enum animation_id aid, anim_step curr) {
177 if(curr == ANIM_STEP_INIT) return animations[aid].first;
178 curr++;
179 if(curr > animations[aid].last) return animations[aid].first;
180 return curr;
183 #define SCREEN_MIN_X 64*SCALE
184 #define SCREEN_MAX_X VMODE_W - 64*SCALE
185 #define SCREEN_MIN_Y 0
186 #define SCREEN_MAX_Y 200*SCALE
188 static void draw_status_bar(void) {
189 enum weapon_id wid = get_active_weapon_id(0);
190 int x, y;
191 sdl_rgb_t *ptr = (sdl_rgb_t *) video.mem;
192 unsigned pitch = video.pitch/4;
193 for(y = SCREEN_MAX_Y; y < VMODE_H; y++)
194 for (x = SCREEN_MIN_X; x < SCREEN_MAX_X; x++)
195 ptr[y*pitch + x] = SRGB_BLACK;
197 blit_sprite(((320 / 2) - (59 / 2)) * SCALE, (200 + (40/2) - (16/2)) * SCALE,
198 &video, SCALE, &weapon_sprites.header, wid, 0);
200 char buf[16];
201 snprintf(buf, 16, "%.6u", objs[player_ids[0]].objspecific.playerdata.score);
202 font_print(SCREEN_MIN_X + 8, SCREEN_MAX_Y + 8, buf, 6, 1 * SCALE, PRGB(255,255,255));
205 enum map_index current_map = MI_VIETNAM;
206 const struct map *map;
207 const struct map_screen* map_scr;
208 const struct palpic *map_bg;
209 const struct palpic *map_fg;
210 const mapscreen_index *map_bonus_indices;
211 const struct map_fglayer *map_bonus_scr;
213 int mapscreen_yoff, mapscreen_xoff;
214 struct { int x,y; } mapsquare;
215 enum map_scrolldir mapscrolldir;
216 unsigned map_spawn_screen_index;
217 unsigned map_spawn_line;
218 unsigned map_spawn_current;
220 static const int fps = 64;
222 static void init_map(enum map_index mapindex) {
223 map = maps[mapindex];
224 map_scr = map_screens[mapindex];
225 map_bg = map_bg_sprites[map->maptype];
226 map_fg = map_fg_sprites[map->maptype];
227 map_bonus_scr = map_bonus_screens[mapindex];
228 map_bonus_indices = map_bonus_layer_indices[mapindex];
229 mapscreen_yoff = 0;
230 mapscreen_xoff = 0;
231 mapsquare.x = 5;
232 mapsquare.y = 26;
233 mapscrolldir = MS_UP;
234 map_spawn_screen_index = 0;
235 map_spawn_line = 0;
236 map_spawn_current = 0;
239 static mapscreen_index get_bonus_layer_index(mapscreen_index screen) {
240 unsigned i;
241 for (i = 0; i < map->bonuslayer_count; i++)
242 if(map_bonus_indices[i] == screen) return i;
243 return MAPSCREEN_BLOCKED;
246 static mapscreen_index screen_to_mapscreen(int *x, int *y) {
247 *x = ((int) *x - SCREEN_MIN_X) / SCALE;
248 *y = ((int) *y - SCREEN_MIN_Y) / SCALE;
249 int yscr = (*y + mapscreen_yoff) / 192;
250 *y = (*y + mapscreen_yoff) - yscr*192;
251 int xscr = (*x + mapscreen_xoff) / 192;
252 *x = (*x + mapscreen_xoff) - xscr*192;
253 return map->screen_map[mapsquare.y + yscr][mapsquare.x + xscr];
256 static enum walltype is_wall(vec2f *pos) {
257 int x = pos->x;
258 int y = pos->y;
259 mapscreen_index scr_idx = screen_to_mapscreen(&x, &y);
260 /* can happen when a bullet goes partially off-screen */
261 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
262 uint8_t spriteno = map_scr[scr_idx].fg.fg[y/16][x/16];
263 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
264 scr_idx = get_bonus_layer_index(scr_idx);
265 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
266 spriteno = map_bonus_scr[scr_idx].fg[y/16][x/16];
267 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
268 return WT_NONE;
271 static void draw_map() {
272 int y, x, my, mx;
273 unsigned map_off = 192-mapscreen_yoff;
274 unsigned vis_x = 192-mapscreen_xoff;
275 int x_iter_max16 = 192/16;
276 int x_iter_max64 = 192/64;
277 unsigned x_iter_start64 = x_iter_max64 - (vis_x / 64 + !!(vis_x % 64));
278 unsigned x_iter_start16 = x_iter_max16 - (vis_x / 16 + !!(vis_x % 16));
280 int x_screen_start64 = -(!!(vis_x%64)*64-(vis_x%64));
281 int x_screen_start16 = -(!!(vis_x%16)*16-(vis_x%16));
283 unsigned x_screen_iter;
284 for(x_screen_iter = 0; x_screen_iter <= !!mapscreen_xoff; x_screen_iter++) {
285 mapscreen_index bonus_layer;
286 if(x_screen_iter) {
287 x_screen_start16 = x_screen_start16+(x_iter_max16-x_iter_start16)*16;
288 x_screen_start64 = x_screen_start64+(x_iter_max64-x_iter_start64)*64;
289 x_iter_max16 = mapscreen_xoff / 16 + !!(mapscreen_xoff % 16);
290 x_iter_max64 = mapscreen_xoff / 64 + !!(mapscreen_xoff % 64);
291 x_iter_start16 = 0;
292 x_iter_start64 = 0;
293 assert(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter] != MAPSCREEN_BLOCKED);
295 uint8_t spriteno;
297 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)
298 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
299 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
300 blit_sprite(x, y, &video,
301 SCALE, map_bg, spriteno, 0);
303 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)
304 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
305 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
306 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
308 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]);
309 if(bonus_layer != MAPSCREEN_BLOCKED) {
310 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)
311 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
312 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
313 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
318 int yleft = 200-map_off;
319 if(yleft > 192) yleft = 192;
320 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/32+!!(yleft%32); my++, y+=32*SCALE)
321 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
322 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
323 blit_sprite(x, y, &video, SCALE, map_bg, spriteno, 0);
325 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
326 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
327 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
328 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
331 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]);
332 if(bonus_layer != MAPSCREEN_BLOCKED) {
333 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
334 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
335 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
336 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
340 /* this is never triggered when mapscreen_xoff != 0 */
341 if(mapscreen_yoff > 192 - 8) {
342 for(mx = 0, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < 3; mx++, x += 64*SCALE)
343 blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video,
344 SCALE, map_bg, map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].bg.bg[0][mx], 0);
345 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
346 spriteno = map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].fg.fg[0][mx];
347 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
349 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+2][mapsquare.x]);
350 if(bonus_layer != MAPSCREEN_BLOCKED) {
351 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
352 spriteno = map_bonus_scr[bonus_layer].fg[0][mx];
353 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
360 #define VSCROLL_TRESHOLD (200-74)
361 #define HSCROLLR_TRESHOLD (54-6)
362 #define HSCROLLL_TRESHOLD (192-(78+3))
363 static int scroll_needed() {
364 struct gameobj *player = &objs[player_ids[0]];
365 if((mapscrolldir == MS_UP && player->pos.y - SCREEN_MIN_Y < VSCROLL_TRESHOLD*SCALE) ||
366 (mapscrolldir == MS_RIGHT && player->pos.x - SCREEN_MIN_X > HSCROLLR_TRESHOLD*SCALE) ||
367 (mapscrolldir == MS_LEFT && player->pos.x - SCREEN_MIN_X < HSCROLLL_TRESHOLD*SCALE))
368 return 1;
369 return 0;
372 static void scroll_gameobjs(int scroll_step) {
373 if(!scroll_step) return;
374 unsigned i, avail = obj_count;
375 for(i = 0; i < OBJ_MAX && avail; i++) {
376 if(!obj_slot_used[i]) continue;
377 avail--;
378 if(objs[i].objtype == OBJ_CROSSHAIR) continue;
380 if(mapscrolldir == MS_UP)
381 objs[i].pos.y += scroll_step*SCALE;
382 else if(mapscrolldir == MS_LEFT)
383 objs[i].pos.x += scroll_step*SCALE;
384 else if(mapscrolldir == MS_RIGHT)
385 objs[i].pos.x -= scroll_step*SCALE;
389 static void next_screen() {
390 map_spawn_screen_index++;
391 map_spawn_line = 0;
392 map_spawn_current = 0;
395 static int init_enemy(const struct enemy_spawn *spawn);
396 static void handle_spawns(unsigned scrollstep) {
397 assert(scrollstep <= 192);
398 unsigned i;
399 const struct enemy_spawn_screen *spawn_map = spawn_maps[current_map];
400 if(!spawn_map[map_spawn_screen_index].spawns) goto done;
401 for(i = 0; i < scrollstep; i++) {
402 while(map_spawn_current < spawn_map[map_spawn_screen_index].num_spawns &&
403 map_spawn_line+i >= spawn_map[map_spawn_screen_index].spawns[map_spawn_current].scroll_line) {
404 init_enemy(&spawn_map[map_spawn_screen_index].spawns[map_spawn_current]);
405 map_spawn_current++;
408 done:
409 map_spawn_line += scrollstep;
412 static int scroll_map() {
413 int ret = 0;
414 int scroll_step = 1;
415 if(scroll_needed()) {
416 if(mapscrolldir == MS_UP) {
417 mapscreen_yoff -= scroll_step;
418 if(mapscreen_yoff < 0) {
419 mapsquare.y--;
420 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
421 scroll_step = -mapscreen_yoff;
422 mapscreen_yoff = 0;
423 mapsquare.y++;
424 scroll_gameobjs(scroll_step);
425 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
426 mapscrolldir = MS_LEFT;
427 } else {
428 mapscrolldir = MS_RIGHT;
429 next_screen();
431 scroll_step = 0;
432 } else {
433 next_screen();
434 mapscreen_yoff += 192;
437 handle_objs:;
438 handle_spawns(scroll_step);
439 scroll_gameobjs(scroll_step);
440 ret = 1;
441 } else if(mapscrolldir == MS_LEFT) {
442 mapscreen_xoff -= scroll_step;
443 if(mapscreen_xoff < 0) {
444 mapsquare.x--;
445 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
446 scroll_step = -mapscreen_xoff;
447 mapscreen_xoff = 0;
448 mapscreen_yoff = 0;
449 mapsquare.x++;
450 scroll_gameobjs(scroll_step);
451 mapscrolldir = MS_UP;
452 scroll_step = 0;
453 } else {
454 mapscreen_xoff += 192;
455 next_screen();
458 goto handle_objs;
459 } else if(mapscrolldir == MS_RIGHT) {
460 mapscreen_xoff += scroll_step;
461 if(mapscreen_xoff >= 192) {
462 mapsquare.x++;
463 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
464 scroll_step = mapscreen_xoff - 192;
465 mapscreen_xoff = 0;
466 mapscreen_yoff = 0;
467 scroll_gameobjs(scroll_step);
468 mapscrolldir = MS_UP;
469 scroll_step = 0;
470 } else {
471 next_screen();
472 mapscreen_xoff -= 192;
475 goto handle_objs;
478 return ret;
481 static int init_player(int player_no) {
482 assert(player_no == 0 || player_no == 1);
483 int pid = gameobj_alloc();
484 gameobj_init(pid, &VEC( SCREEN_MIN_X, SCREEN_MAX_Y - (25 * SCALE) ), &VEC( 0, 0 ),
485 SI_PLAYERS, player_no == 0 ? ANIM_P1_MOVE_N : ANIM_P2_MOVE_N, player_no == 0 ? OBJ_P1 : OBJ_P2);
486 if(pid == -1) return -1;
487 player_ids[player_no] = pid;
488 objs[pid].objspecific.playerdata.score = 0;
489 player_weapons[player_no][0] = WP_COLT45;
490 weapon_count[player_no] = 1;
491 weapon_active[player_no] = 0;
492 size_t i = 0;
493 for(; i < AMMO_MAX; i++)
494 player_ammo[player_no][i] = 50000;
495 add_player(pid);
496 return pid;
499 static vec2f *mousepos;
500 static int init_crosshair() {
501 int id = gameobj_alloc();
502 gameobj_init(id, &VEC(VMODE_W/2, VMODE_H/2), &VEC(0,0), SI_CROSSHAIR, ANIM_CROSSHAIR, OBJ_CROSSHAIR);
503 if(id == -1) return -1;
504 mousepos = &objs[id].pos;
505 return id;
508 static int init_blood(vec2f *pos) {
509 int id = gameobj_alloc();
510 gameobj_init(id, pos, &VEC(0,0), SI_MISC, ANIM_BLOOD, OBJ_BLOOD);
511 gameobj_init_bulletdata(id, 4);
512 return id;
515 static int init_bullet(vec2f *pos, vec2f *vel, int steps) {
516 int id = gameobj_alloc();
517 gameobj_init(id, pos, vel, SI_BULLET, ANIM_BULLET, OBJ_BULLET);
518 gameobj_init_bulletdata(id, steps);
519 return id;
522 static int init_grenade(vec2f *pos, vec2f *vel, int steps) {
523 int id = gameobj_alloc();
524 gameobj_init(id, pos, vel, SI_GRENADE, ANIM_GRENADE_SMALL, OBJ_GRENADE);
525 gameobj_init_bulletdata(id, steps);
526 return id;
529 static int init_grenade_explosion(vec2f *pos, int from_enemy) {
530 const int ticks_per_anim_frame = 4;
531 const int expl_anim_frames = 11;
532 vec2f grenade_center = get_sprite_center(spritemaps[SI_GRENADE_EXPLOSION]);
533 vec2f mypos = vecsub(pos, &grenade_center);
534 int id = gameobj_alloc();
535 if(id == -1) return -1;
536 gameobj_init(id, &mypos, &VEC(0,0), SI_GRENADE_EXPLOSION, ANIM_GRENADE_EXPLOSION, OBJ_GRENADE_EXPLOSION);
537 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
538 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
539 if(!from_enemy) add_explosion(id);
540 else add_enemy_explosion(id);
541 return id;
544 static int init_big_explosion(vec2f *pos) {
545 const int ticks_per_anim_frame = 8;
546 const int expl_anim_frames = 5;
547 vec2f rocket_center = get_sprite_center(spritemaps[SI_BIG_EXPLOSION]);
548 vec2f mypos = vecsub(pos, &rocket_center);
549 int id = gameobj_alloc();
550 if(id == -1) return -1;
551 gameobj_init(id, &mypos, &VEC(0,0), SI_BIG_EXPLOSION, ANIM_BIG_EXPLOSION, OBJ_BIG_EXPLOSION);
552 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
553 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
554 add_explosion(id);
555 return id;
558 static int init_rocket_explosion(vec2f *pos) {
559 vec2f ax = vecadd(pos, &VEC(-15*SCALE, 9*SCALE));
560 vec2f bx = vecadd(pos, &VEC(1*SCALE, -6*SCALE));
561 vec2f cx = vecadd(pos, &VEC(-8*SCALE, -8*SCALE));
562 vec2f dx = vecadd(pos, &VEC(8*SCALE, 8*SCALE));
563 int ret = 0;
564 ret += init_grenade_explosion(&ax, 0) != -1;
565 ret += init_grenade_explosion(&bx, 0) != -1;
566 ret += init_big_explosion(&cx) != -1;
567 ret += init_big_explosion(&dx) != -1;
568 return ret;
571 static int init_flame(vec2f *pos, vec2f *vel, int steps) {
572 int id = gameobj_alloc();
573 if(id == -1) return -1;
574 gameobj_init(id, pos, vel, SI_FLAME, ANIM_FLAME, OBJ_FLAME);
575 gameobj_init_bulletdata(id, steps);
576 return id;
579 static int init_player_flame(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
580 static const vec2f flame_origin[] = {
581 [DIR_O] = { 4.0, 8.0 },
582 [DIR_NO] = { 5.0, 11.0 },
583 [DIR_N] = { 7.5, 12.0 },
584 [DIR_NW] = { 10.0, 11.0 },
585 [DIR_W] = { 11.0, 8.0 },
586 [DIR_SW] = { 10.0, 5.0 },
587 [DIR_S] = { 7.5, 3.0 },
588 [DIR_SO] = { 4.0, 4.0 },
590 vec2f mypos = *pos;
591 mypos.x -= flame_origin[dir].x * SCALE;
592 mypos.y -= flame_origin[dir].y * SCALE;
593 return init_flame(&mypos, vel, steps);
596 static int init_rocket(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
597 static const vec2f rocket_origin[] = {
598 [DIR_N] = { 1.0, 10.0 },
599 [DIR_S] = { 1.0, 0.0 },
600 [DIR_O] = { 0.0, 1.0 },
601 [DIR_W] = { 10.0, 1.0 },
602 [DIR_NO] = { 0.0, 7.0 },
603 [DIR_SO] = { 0.0, 0.0 },
604 [DIR_SW] = { 7.0, 0.0 },
605 [DIR_NW] = { 7.0, 7.0 },
607 static const enum animation_id rocket_anim[] = {
608 [DIR_N] = ANIM_ROCKET_N,
609 [DIR_S] = ANIM_ROCKET_S,
610 [DIR_O] = ANIM_ROCKET_O,
611 [DIR_W] = ANIM_ROCKET_W,
612 [DIR_NO] = ANIM_ROCKET_NO,
613 [DIR_SO] = ANIM_ROCKET_SO,
614 [DIR_SW] = ANIM_ROCKET_SW,
615 [DIR_NW] = ANIM_ROCKET_NW,
617 vec2f mypos = *pos;
618 mypos.x -= rocket_origin[dir].x * SCALE;
619 mypos.y -= rocket_origin[dir].y * SCALE;
620 int id = gameobj_alloc();
621 if(id == -1) return -1;
622 gameobj_init(id, &mypos, vel, SI_ROCKET, rocket_anim[dir], OBJ_ROCKET);
623 gameobj_init_bulletdata(id, steps);
624 add_rocket(id);
625 return id;
629 static const struct enemy_route* get_enemy_current_route(int curr_step, const struct enemy_spawn *spawn) {
630 int i = ENEMY_MAX_ROUTE -1;
631 for(; i >= 0; i--)
632 if(spawn->route[i].shape != ES_INVALID &&
633 curr_step >= spawn->route[i].start_step)
634 return &spawn->route[i];
635 return 0;
638 static vec2f get_enemy_vel(int curr_step, const struct enemy_spawn *spawn) {
639 const struct enemy_route *route = get_enemy_current_route(curr_step, spawn);
640 return get_vel_from_direction16(route->dir, (float)route->vel/8.f);
643 static const enum animation_id enemy_animation_lut[] = {
644 [ES_SOLDIER1_DOWN] = ANIM_ENEMY_GUNNER_DOWN,
645 [ES_SOLDIER1_RIGHT] = ANIM_ENEMY_GUNNER_RIGHT,
646 [ES_SOLDIER1_LEFT] = ANIM_ENEMY_GUNNER_LEFT,
647 [ES_SOLDIER2_DOWN] = ANIM_ENEMY_BOMBER_DOWN,
648 [ES_SOLDIER2_RIGHT] = ANIM_ENEMY_BOMBER_RIGHT,
649 [ES_SOLDIER2_LEFT] = ANIM_ENEMY_BOMBER_LEFT,
650 [ES_JEEP] = ANIM_JEEP,
651 [ES_TANK_SMALL] = ANIM_TANK_SMALL,
652 [ES_TANK_BIG] = ANIM_TANK_BIG,
653 [ES_TRANSPORTER] = ANIM_TRANSPORTER,
654 [ES_GUNTURRET_MOVABLE_MACHINE] = ANIM_GUNTURRET_MOVABLE_MACHINE_S,
655 [ES_GUNTURRET_MOVABLE_MAN] = ANIM_GUNTURRET_MOVABLE_MAN_S,
656 [ES_MINE_FLAT] = ANIM_MINE_FLAT,
657 [ES_MINE_CROSS] = ANIM_MINE_CROSSED,
658 [ES_FLAMETURRET] = ANIM_FLAMETURRET,
659 [ES_GUNTURRET_FIXED_SOUTH] = ANIM_GUNTURRET_FIXED_SOUTH,
660 [ES_GUNTURRET_FIXED_NORTH] = ANIM_GUNTURRET_FIXED_NORTH,
661 [ES_BUNKER_1] = ANIM_BUNKER1,
662 [ES_BUNKER_2] = ANIM_BUNKER2,
663 [ES_BUNKER_3] = ANIM_BUNKER3,
664 [ES_BUNKER_4] = ANIM_BUNKER4,
665 [ES_BUNKER_5] = ANIM_BUNKER5,
668 static int init_enemy(const struct enemy_spawn *spawn) {
669 const enum objtype enemy_soldier_objtype_lut[] = {
670 [0] = OBJ_ENEMY_SHOOTER,
671 [1] = OBJ_ENEMY_BOMBER
673 const enum objtype enemy_objtype_lut[] = {
674 [ES_JEEP] = OBJ_JEEP,
675 [ES_TANK_SMALL] = OBJ_TANK_SMALL,
676 [ES_TANK_BIG] = OBJ_TANK_BIG,
677 [ES_TRANSPORTER] = OBJ_TRANSPORTER,
678 [ES_GUNTURRET_MOVABLE_MACHINE] = OBJ_GUNTURRET_MOVABLE_MACHINE,
679 [ES_GUNTURRET_MOVABLE_MAN] = OBJ_GUNTURRET_MOVABLE_MAN,
680 [ES_MINE_FLAT] = OBJ_MINE_FLAT,
681 [ES_MINE_CROSS] = OBJ_MINE_CROSSED,
682 [ES_FLAMETURRET] = OBJ_FLAMETURRET,
683 [ES_GUNTURRET_FIXED_SOUTH] = OBJ_GUNTURRET_FIXED_SOUTH,
684 [ES_GUNTURRET_FIXED_NORTH] = OBJ_GUNTURRET_FIXED_NORTH,
685 [ES_BUNKER_1] = OBJ_BUNKER1,
686 [ES_BUNKER_2] = OBJ_BUNKER2,
687 [ES_BUNKER_3] = OBJ_BUNKER3,
688 [ES_BUNKER_4] = OBJ_BUNKER4,
689 [ES_BUNKER_5] = OBJ_BUNKER5,
690 [ES_BOSS] = OBJ_BOSS,
692 const enum animation_id boss_animation_lut[] = {
693 [0] = ANIM_BOSS1,
694 [1] = ANIM_BOSS2,
695 [2] = ANIM_BOSS3,
696 [3] = ANIM_BOSS4,
697 [4] = ANIM_BOSS5,
698 [5] = ANIM_BOSS6,
699 [6] = ANIM_BOSS7,
700 [7] = ANIM_BOSS8,
701 [8] = ANIM_BOSS9,
702 [9] = ANIM_BOSS10,
703 [10] = ANIM_BOSS11,
704 [11] = ANIM_BOSS12,
706 const enum sprite_index enemy_soldier_sprite_lut[] = {
707 [ET_ASIAN] = SI_ENEMY_ASIAN,
708 [ET_WESTERN] = SI_ENEMY_WESTERN,
710 const enum sprite_index enemy_sprite_lut[] = {
711 [ES_JEEP] = SI_VEHICLES_SMALL,
712 [ES_TANK_SMALL] = SI_VEHICLES_MEDIUM,
713 [ES_TANK_BIG] = SI_VEHICLES_BIG,
714 [ES_TRANSPORTER] = SI_VEHICLES_BIG,
715 [ES_BUNKER_1] = SI_BUNKERS,
716 [ES_BUNKER_2] = SI_BUNKERS,
717 [ES_BUNKER_3] = SI_BUNKERS,
718 [ES_BUNKER_4] = SI_BUNKERS,
719 [ES_BUNKER_5] = SI_BUNKERS,
720 [ES_GUNTURRET_MOVABLE_MACHINE] = SI_GUNTURRET,
721 [ES_GUNTURRET_MOVABLE_MAN] = SI_GUNTURRET,
722 [ES_MINE_FLAT] = SI_MINES,
723 [ES_MINE_CROSS] = SI_MINES,
724 [ES_FLAMETURRET] = SI_MINES,
725 [ES_GUNTURRET_FIXED_SOUTH] = SI_MINES,
726 [ES_GUNTURRET_FIXED_NORTH] = SI_MINES,
727 [ES_BOSS] = SI_BOSSES,
730 vec2f spawnpos = VEC(SCREEN_MIN_X + spawn->x*SCALE, SCREEN_MIN_Y + spawn->y*SCALE);
731 int id = gameobj_alloc();
732 if(id == -1) return -1;
733 const struct enemy_route* route_curr = get_enemy_current_route(0, spawn);
734 vec2f vel = get_enemy_vel(0, spawn);
736 int is_soldier = route_curr->shape <= ES_SOLDIER2_RIGHT;
737 int is_boss = route_curr->shape == ES_BOSS;
738 enum sprite_index spriteid;
739 enum objtype objid;
740 enum animation_id animid;
741 if(is_soldier) {
742 spriteid = enemy_soldier_sprite_lut[map->enemy_type];
743 objid = enemy_soldier_objtype_lut[spawn->weapon];
744 } else {
745 spriteid = enemy_sprite_lut[route_curr->shape];
746 objid = enemy_objtype_lut[route_curr->shape];
748 if(is_boss) animid = boss_animation_lut[map->boss_id];
749 else animid = enemy_animation_lut[route_curr->shape];
751 gameobj_init(id, &spawnpos, &vel, spriteid, animid, objid);
752 objs[id].objspecific.enemy.curr_step = 0;
753 objs[id].objspecific.enemy.spawn = spawn;
754 switch(objid) {
755 case OBJ_BOSS:
756 add_boss(id);
757 break;
758 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
759 case OBJ_BUNKER4: case OBJ_BUNKER5:
760 add_bunker(id);
761 break;
762 case OBJ_GUNTURRET_MOVABLE_MACHINE:
763 case OBJ_GUNTURRET_MOVABLE_MAN:
764 case OBJ_FLAMETURRET: case OBJ_GUNTURRET_FIXED_NORTH:
765 case OBJ_GUNTURRET_FIXED_SOUTH:
766 add_turret(id);
767 break;
768 case OBJ_MINE_CROSSED: case OBJ_MINE_FLAT:
769 add_mine(id);
770 break;
771 case OBJ_JEEP: case OBJ_TRANSPORTER:
772 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
773 add_vehicle(id);
774 break;
775 default:
776 add_enemy(id);
778 return id;
781 static void remove_enemy(int id) {
782 enum objtype objid = objs[id].objtype;
783 switch(objid) {
784 case OBJ_JEEP: case OBJ_TRANSPORTER:
785 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
786 golist_remove(&go_vehicles, id);
787 break;
788 default:
789 golist_remove(&go_enemies, id);
791 gameobj_free(id);
794 static int enemy_fires(struct enemy *e) {
795 int i;
796 for(i = 0; i < ENEMY_MAX_SHOT; i++)
797 if(e->curr_step == e->spawn->shots[i]) return 1;
798 return 0;
801 static enum animation_id get_flash_animation_from_direction(enum direction dir) {
802 #define ANIMF(dir, anim) [dir] = anim
803 static const enum animation_id dir_to_anim[] = {
804 ANIMF(DIR_O, ANIM_FLASH_O),
805 ANIMF(DIR_NO, ANIM_FLASH_NO),
806 ANIMF(DIR_N, ANIM_FLASH_N),
807 ANIMF(DIR_NW, ANIM_FLASH_NW),
808 ANIMF(DIR_W, ANIM_FLASH_W),
809 ANIMF(DIR_SW, ANIM_FLASH_SW),
810 ANIMF(DIR_S, ANIM_FLASH_S),
811 ANIMF(DIR_SO, ANIM_FLASH_SO),
813 #undef ANIMF
814 return dir_to_anim[dir];
817 static int init_flash(vec2f *pos, enum direction dir) {
818 int id = gameobj_alloc();
819 gameobj_init(id, pos, &VEC(0, 0), SI_FLASH, get_flash_animation_from_direction(dir), OBJ_FLASH);
820 gameobj_init_bulletdata(id, 2);
821 return id;
824 static vec2f get_gameobj_pos(int obj_id) {
825 return objs[obj_id].pos;
828 static vec2f get_gameobj_center(int obj_id) {
829 vec2f res = objs[obj_id].pos;
830 vec2f add = get_sprite_center(spritemaps[objs[obj_id].spritemap_id]);
831 return vecadd(&res, &add);
834 static enum direction get_direction_from_vec(vec2f *vel);
835 static enum animation_id get_anim_from_direction(enum direction dir, int player, int throwing);
837 static enum direction16 get_direction16_from_direction(enum direction dir) {
838 static const enum direction16 dir16_transl_tab[] = {
839 [DIR_N] = DIR16_N, [DIR_NO] = DIR16_NO, [DIR_O] = DIR16_O, [DIR_SO] = DIR16_SO,
840 [DIR_S] = DIR16_S, [DIR_SW] = DIR16_SW, [DIR_W] = DIR16_W, [DIR_NW] = DIR16_NW,
842 assert(dir != DIR_INVALID);
843 return dir16_transl_tab[dir];
846 static enum weapon_id get_active_weapon_id(int player_no) {
847 return player_weapons[player_no][weapon_active[player_no]];
850 static const struct weapon* get_active_weapon(int player_no) {
851 return &weapons[get_active_weapon_id(player_no)];
853 static enum direction16 get_shotdirection_from_enemy(int curr_step, const struct enemy_spawn *spawn) {
854 const struct enemy_route* r = get_enemy_current_route(curr_step, spawn);
855 switch(r->shape) {
856 case ES_SOLDIER1_DOWN: case ES_SOLDIER2_DOWN:
857 return DIR16_S;
858 case ES_SOLDIER1_LEFT: case ES_SOLDIER2_LEFT:
859 return DIR16_W;
860 case ES_SOLDIER1_RIGHT: case ES_SOLDIER2_RIGHT:
861 return DIR16_O;
862 default:
863 assert(0);
867 static void enemy_fire_bullet(enum direction16 dir, int steps, enum enemy_weapon wpn, vec2f *pos) {
868 vec2f vel = get_vel_from_direction16(dir, 1.75);
869 int id;
870 if(wpn == EW_GUN) {
871 id = init_bullet(pos, &vel, steps);
872 if(id != -1) add_ebullet(id);
873 } else if (wpn == EW_GRENADE) {
874 id = init_grenade(pos, &vel, steps);
875 if(id != -1) add_enemy_grenade(id);
876 } else {
877 id = init_flame(pos, &vel, steps);
878 if(id != -1) add_enemy_flame(id);
882 static int get_crosshair_id(void) {
883 assert(sblist_getsize(&go_crosshair));
884 uint8_t *id = sblist_get(&go_crosshair, 0);
885 return *id;
888 static void fire_bullet(int player_no) {
889 int id;
890 const struct weapon *pw = get_active_weapon(player_no);
891 if(player_ammo[player_no][pw->ammo] == 0) return;
892 vec2f from = get_gameobj_center(player_ids[player_no]);
893 //get_anim_from_vel(0, objs[player].
894 vec2f to = get_gameobj_center(get_crosshair_id());
895 to.x += 4*SCALE - rand()%8*SCALE;
896 to.y += 4*SCALE - rand()%8*SCALE;
897 vec2f vel = velocity(&from, &to);
898 enum direction dir = get_direction_from_vec(&vel);
899 if(dir != DIR_INVALID) {
900 enum animation_id aid = get_anim_from_direction(dir, player_no, pw->ammo == AMMO_GRENADE);
901 if(aid != ANIM_INVALID) switch_anim(player_ids[player_no], aid);
902 anim_step curranim = objs[player_ids[player_no]].anim_curr;
903 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[player_ids[player_no]].animid, ANIM_STEP_INIT);
904 vec2f muzzle = muzzle_tab[curranim];
906 from = get_gameobj_pos(player_ids[player_no]);
907 from.x += muzzle.x * SCALE;
908 from.y += muzzle.y * SCALE;
910 if(pw->flags & WF_MUZZLEFLASH) {
911 static const vec2f flash_start[] = {
912 [DIR_O] = { 0.0, 1.0 },
913 [DIR_NO] = { 0.5, 6.0 },
914 [DIR_N] = { 1.0, 7.5 },
915 [DIR_NW] = { 6.0, 6.0 },
916 [DIR_W] = { 7.5, 1.0 },
917 [DIR_SW] = { 4.5, 0.0 },
918 [DIR_S] = { 1.0, 0.0 },
919 [DIR_SO] = { 0.0, 0.0 },
921 vec2f ffrom = from;
922 ffrom.x -= flash_start[dir].x * SCALE;
923 ffrom.y -= flash_start[dir].y * SCALE;
924 id = init_flash(&ffrom, dir);
925 if(id != -1) add_muzzleflash(id);
927 vel = velocity(&from, &to);
929 float dist = veclength(&vel);
930 float speed = pw->bullet_speed * SCALE;
931 const uint16_t range_tab[] = {0, 80, 66, 80, 118, 118, 118, 118, 118, 118,
932 200, 200, 240, 240, 240, 240, 240, 240, 240, 240, 320 };
933 float range = range_tab[pw->range] * SCALE;
934 if(dist > range)
935 dist = range;
936 float steps = dist / speed;
937 float deg = atan2(vel.y, vel.x);
938 vel.x = cos(deg) * speed;
939 vel.y = sin(deg) * speed;
940 switch(pw->shot) {
941 case ST_LAUNCHER:
942 id = init_rocket(dir, &from, &vel, steps);
943 break;
944 case ST_BULLET:
945 id = init_bullet(&from, &vel, steps);
946 if(id != -1) add_pbullet(id);
947 break;
948 case ST_FLAMES:
949 id = init_player_flame(dir, &from, &vel, steps);
950 if(id != -1) add_flame(id);
951 break;
952 case ST_GRENADE:
953 id = init_grenade(&from, &vel, steps);
954 add_grenade(id);
955 break;
956 default:
957 abort();
959 player_ammo[player_no][pw->ammo]--;
960 const WAVE_HEADER_COMPLETE *wf = wavesounds[pw->sound];
961 if(id != -1 && pw->sound != WS_NONE)
962 audio_play_wave_resource(wf);
965 static void init_game_objs() {
966 sblist_init(&go_crosshair, 1, 4);
967 sblist_init(&go_players, 1, 4);
968 sblist_init(&go_muzzleflash, 1, 4);
969 sblist_init(&go_player_bullets, 1, 32);
970 sblist_init(&go_flames, 1, 32);
971 sblist_init(&go_enemy_bullets, 1, 32);
972 sblist_init(&go_explosions, 1, 16);
973 sblist_init(&go_enemy_explosions, 1, 16);
974 sblist_init(&go_grenades, 1, 16);
975 sblist_init(&go_enemy_grenades, 1, 16);
976 sblist_init(&go_rockets, 1, 8);
977 sblist_init(&go_enemies, 1, 32);
978 sblist_init(&go_vehicles, 1, 4);
979 sblist_init(&go_mines, 1, 4);
980 sblist_init(&go_turrets, 1, 8);
981 sblist_init(&go_bunkers, 1, 4);
982 sblist_init(&go_boss, 1, 4);
983 sblist_init(&go_blood, 1, 16);
984 sblist_init(&go_enemy_flames, 1, 16);
985 init_player(0);
986 add_crosshair(init_crosshair());
987 init_map(current_map);
990 static int point_in_mask(vec2f *point, int obj_id) {
991 vec2f pos_in_pic = VEC((point->x - objs[obj_id].pos.x) / SCALE,
992 (point->y - objs[obj_id].pos.y) / SCALE);
993 const struct palpic *p = spritemaps[objs[obj_id].spritemap_id];
994 unsigned h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
995 if(pos_in_pic.x < 0 || pos_in_pic.y < 0 || pos_in_pic.x > w || pos_in_pic.y > h) return 0;
996 assert(objs[obj_id].anim_curr != ANIM_STEP_INIT);
997 anim_step curranim = objs[obj_id].anim_curr;
998 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[obj_id].animid, ANIM_STEP_INIT);
999 uint8_t *data = palpic_getspritedata(p, curranim);
1000 if(data[(unsigned) pos_in_pic.y * w + (unsigned) pos_in_pic.x] != 0) return 1;
1001 return 0;
1004 static enum animation_id get_die_anim(unsigned id) {
1005 switch(objs[id].objtype) {
1006 case OBJ_P1:
1007 return ANIM_P1_DIE;
1008 case OBJ_P2:
1009 return ANIM_P2_DIE;
1010 case OBJ_JEEP:
1011 return ANIM_JEEP_DESTROYED;
1012 case OBJ_TANK_SMALL:
1013 return ANIM_TANK_SMALL_DESTROYED;
1014 case OBJ_TANK_BIG:
1015 return ANIM_TANK_BIG_DESTROYED;
1016 case OBJ_TRANSPORTER:
1017 return ANIM_TRANSPORTER_DESTROYED;
1018 case OBJ_ENEMY_BOMBER:
1019 return ANIM_ENEMY_BOMBER_DIE;
1020 case OBJ_ENEMY_SHOOTER:
1021 return ANIM_ENEMY_GUNNER_DIE;
1022 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
1023 case OBJ_BUNKER4: case OBJ_BUNKER5:
1024 return ANIM_BUNKER_DESTROYED;
1025 case OBJ_GUNTURRET_MOVABLE_MACHINE:
1026 return ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED;
1027 case OBJ_GUNTURRET_MOVABLE_MAN:
1028 return ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED;
1029 default:
1030 return ANIM_INVALID;
1034 /* remove bullets that have reached their maximum number of steps */
1035 static int remove_bullets(sblist *list) {
1036 int res = 0;
1037 uint8_t *item_id;
1038 ssize_t li;
1039 sblist_iter_counter2s(list, li, item_id) {
1040 struct gameobj *bullet = &objs[*item_id];
1041 if(bullet->objspecific.bullet.step_curr >= bullet->objspecific.bullet.step_max) {
1042 gameobj_free(*item_id);
1043 sblist_delete(list, li);
1044 li--;
1045 } else {
1046 bullet->objspecific.bullet.step_curr++;
1048 res = 1;
1050 return res;
1053 static int remove_explosives(sblist *list) {
1054 int res = 0;
1055 uint8_t *item_id;
1056 ssize_t li;
1057 sblist_iter_counter2s(list, li, item_id) {
1058 struct gameobj *go = &objs[*item_id];
1059 if(go->objspecific.bullet.step_curr >= go->objspecific.bullet.step_max) {
1060 if(go->objtype == OBJ_GRENADE) init_grenade_explosion(&go->pos, list == &go_enemy_grenades);
1061 else init_rocket_explosion(&go->pos);
1062 gameobj_free(*item_id);
1063 sblist_delete(list, li);
1064 li--;
1065 } else {
1066 go->objspecific.bullet.step_curr++;
1067 if(go->objtype == OBJ_GRENADE) {
1068 if(go->objspecific.bullet.step_curr >= 32) go->animid = ANIM_GRENADE_SMALL;
1069 else if(go->objspecific.bullet.step_curr >= 8) go->animid = ANIM_GRENADE_BIG;
1072 res = 1;
1074 return res;
1077 static int remove_offscreen_objects(sblist *list) {
1078 int res = 0;
1079 uint8_t *item_id;
1080 ssize_t li;
1081 sblist_iter_counter2s(list, li, item_id) {
1082 assert(obj_slot_used[*item_id]);
1083 struct gameobj *go = &objs[*item_id];
1084 assert((int) go->spritemap_id < SI_MAX);
1085 const struct palpic *p = spritemaps[go->spritemap_id];
1086 assert(p->spritecount);
1087 int h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
1088 if(go->pos.x < SCREEN_MIN_X-w*SCALE || go->pos.x > SCREEN_MAX_X ||
1089 go->pos.y < SCREEN_MIN_Y-h*SCALE || go->pos.y > SCREEN_MAX_Y) {
1090 res = 1;
1091 dprintf(2, "offscreen: removed gameobj %d\n", (int) *item_id);
1092 gameobj_free(*item_id);
1093 sblist_delete(list, li);
1094 li--;
1097 return res;
1100 static int get_sprite_number(int id) {
1101 struct gameobj *o = &objs[id];
1102 return o->anim_curr == ANIM_STEP_INIT ? get_next_anim_frame(o->animid, o->anim_curr) : o->anim_curr;
1105 static int is_death_anim(enum animation_id anim) {
1106 switch(anim) {
1107 case ANIM_ENEMY_BOMBER_DIE:
1108 case ANIM_ENEMY_GUNNER_DIE:
1109 case ANIM_ENEMY_BURNT:
1110 case ANIM_P1_DIE:
1111 case ANIM_P2_DIE:
1112 case ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED:
1113 case ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED:
1114 case ANIM_BUNKER_DESTROYED:
1115 case ANIM_JEEP_DESTROYED:
1116 case ANIM_TANK_BIG_DESTROYED:
1117 case ANIM_TANK_SMALL_DESTROYED:
1118 case ANIM_TRANSPORTER_DESTROYED:
1119 return 1;
1120 default:
1121 return 0;
1125 static int overlap(int id1, int id2) {
1126 struct gameobj *o1 = &objs[id1];
1127 struct gameobj *o2 = &objs[id2];
1128 const struct palpic* p1 = spritemaps[o1->spritemap_id];
1129 const struct palpic* p2 = spritemaps[o2->spritemap_id];
1131 vec2f vec = velocity(&o1->pos, &o2->pos);
1132 float mh = o1->pos.y < o2->pos.y ? palpic_getspriteheight(p1) : palpic_getspriteheight(p2);
1133 float mw = o1->pos.x < o2->pos.x ? palpic_getspritewidth(p1) : palpic_getspritewidth(p2);
1134 mh*=SCALE, mw*=SCALE;
1135 return fabs(vec.x) < mw && fabs(vec.y) < mh;
1138 static int masks_collide(int id1, int id2) {
1139 struct gameobj *a = &objs[id1];
1140 struct gameobj *b = &objs[id2];
1141 const struct palpic* pa = spritemaps[a->spritemap_id];
1142 const struct palpic* pb = spritemaps[b->spritemap_id];
1143 int ax = a->pos.x/SCALE, ay = a->pos.y/SCALE,
1144 aw = palpic_getspritewidth(pa), ah = palpic_getspriteheight(pa);
1145 int bx = b->pos.x/SCALE, by = b->pos.y/SCALE,
1146 bw = palpic_getspritewidth(pb), bh = palpic_getspriteheight(pb);
1148 /* x: normalized start position */
1149 int xx = MIN(ax, bx), xy = MIN(ay, by);
1150 /* normalize coordinates to 0 */
1151 ax -= xx, ay -= xy, bx -= xx, by -= xy;
1152 /* o: upper left point of the overlapping region */
1153 int ox = MAX(ax, bx), oy = MAX(ay, by);
1154 /* e: lower right point of the overlapping region */
1155 int ex = MIN(ax+aw, bx+bw), ey = MIN(ay+ah, by+bh);
1156 /* vector o -> e */
1157 int oex = ex - ox, oey = ey - oy;
1158 /* (s)tart and (e)nd coordinates of the overlapping regions in both rects */
1159 int asx = ox - ax, asy = oy - ay;
1160 int bsx = ox - bx, bsy = oy - by;
1161 int aex = asx + oex, bex = bsx + oex;
1162 int aey = asy + oey, bey = bsy + oey;
1163 assert(asx >= 0); assert(asy >= 0);
1164 assert(bsx >= 0); assert(bsy >= 0);
1165 if(!aex || !bex || !aey || !bey) return 0;
1166 assert(aex > 0); assert(bex > 0);
1167 assert(aey > 0); assert(bey > 0);
1168 int xa, ya, xb, yb;
1169 const uint8_t *da = palpic_getspritedata(pa, get_sprite_number(id1)),
1170 *db = palpic_getspritedata(pb, get_sprite_number(id2));
1171 assert(da); assert(db);
1172 for(ya = asy, yb = bsy; ya < aey && yb < bey; ya++, yb++)
1173 for(xa = asx, xb = bsx; xa < aex && xb < bex ; xa++, xb++)
1174 if(da[ya*aw+xa] && db[yb*bw+xb]) return 1;
1175 return 0;
1179 // removes bullets and other objects if they collide. return 1 if anything happened
1180 static int hit_bullets(sblist *bullet_list, sblist *target_list) {
1181 uint8_t *bullet_id;
1182 ssize_t li;
1183 int res = 0;
1184 enum bulletsubtype {
1185 BS_BULLET = 0,
1186 BS_FLAME = 1,
1187 BS_GRENADE_EXPL = 2,
1188 BS_BIG_EXPL = 3,
1189 BS_TOUCH,
1190 } bullet_subtybe = BS_BULLET;
1192 sblist_iter_counter2s(bullet_list, li, bullet_id) {
1193 struct gameobj *bullet = &objs[*bullet_id];
1194 if(bullet->objtype == OBJ_FLAME) bullet_subtybe = BS_FLAME;
1195 else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1196 /* grenade kills only in the explosion, not in the smoke phase */
1197 if(bullet->objspecific.bullet.step_curr > 22) continue;
1198 bullet_subtybe = BS_GRENADE_EXPL;
1199 } else if(bullet->objtype == OBJ_BIG_EXPLOSION) {
1200 bullet_subtybe = BS_BIG_EXPL;
1201 } else if(bullet_list == &go_vehicles || bullet_list == &go_enemies ||
1202 bullet_list == &go_boss || bullet_list == &go_mines) {
1203 bullet_subtybe = BS_TOUCH;
1206 vec2f bullet_center = get_gameobj_center(*bullet_id);
1207 if(bullet_list == &go_player_bullets || bullet_list == &go_flames) {
1208 if(is_wall(&bullet_center) == WT_SOLID) goto remove_bullet;
1211 const float bullet_radius[] = { [BS_BULLET] = 1.f, [BS_FLAME] = 6.f,
1212 [BS_GRENADE_EXPL] = 16.f, [BS_BIG_EXPL] = 19.f, [BS_TOUCH] = 8.f, };
1214 size_t lj;
1215 uint8_t *target_id;
1216 sblist_iter_counter2(target_list, lj, target_id) {
1217 struct gameobj *target = &objs[*target_id];
1218 if(is_death_anim(target->animid) && target_list != &go_vehicles) continue;
1219 if(bullet_subtybe == BS_TOUCH &&
1220 overlap(*target_id, *bullet_id) &&
1221 masks_collide(*target_id, *bullet_id)) {
1222 dprintf(2, "hit2\n");
1223 if(bullet_list == &go_boss) return 2;
1224 goto hit;
1227 vec2f temp = get_gameobj_center(*target_id);
1228 float dist1 = vecdist(&bullet_center, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1229 vec2f newpos = vecadd(&bullet_center, &bullet->vel);
1230 float dist2 = vecdist(&newpos, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1232 unsigned w = palpic_getspritewidth(spritemaps[target->spritemap_id]),
1233 h = palpic_getspriteheight(spritemaps[target->spritemap_id]);
1234 float longest_side_div2 = ((float) MAX(h, w) / 2) * SCALE;
1235 if(dist1 < 1.f*SCALE || dist2 < 1.f*SCALE) { dprintf(2, "hit1\n"); goto hit; }
1236 if(dist1 < longest_side_div2 || dist2 < longest_side_div2) {
1237 vec2f velquarter = VEC(bullet->vel.x * 0.25, bullet->vel.y * 0.25);
1238 vec2f point = bullet_center;
1239 size_t k;
1240 for(k = 0; k < 4; k++) {
1241 if(point_in_mask(&point, *target_id)) {
1242 hit:
1244 if(bullet_list == &go_player_bullets) {
1245 if(target_list == &go_vehicles) {
1246 audio_play_wave_resource(wavesounds[WS_DROPSHOT]);
1247 goto remove_bullet;
1249 objs[player_ids[0]].objspecific.playerdata.score += 50;
1250 player_kills[0]++;
1251 } else if (bullet_list == &go_rockets) {
1252 init_rocket_explosion(&target->pos);
1253 goto remove_bullet;
1254 } else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1255 // grenade explosion has no effect on vehicles and bunkers.
1256 if(target_list == &go_vehicles || target_list == &go_bunkers)
1257 goto next_bullet;
1258 } else if(bullet->objtype == OBJ_MINE_CROSSED || bullet->objtype == OBJ_MINE_FLAT) {
1259 init_big_explosion(&target->pos);
1260 goto remove_bullet;
1262 enum animation_id death_anim = (bullet_subtybe == BS_FLAME && target_list == &go_enemies) ?
1263 ANIM_ENEMY_BURNT : get_die_anim(*target_id);
1264 if(death_anim == ANIM_INVALID) {
1265 gameobj_free(*target_id);
1266 sblist_delete(target_list, lj);
1267 lj--;
1268 goto next_target;
1270 switch_anim(*target_id, death_anim);
1271 target->vel = VEC(0,0);
1272 if(target->objtype == OBJ_ENEMY_BOMBER || target->objtype == OBJ_ENEMY_SHOOTER) {
1273 const enum wavesound_id wid[] = { WS_SCREAM, WS_SCREAM2 };
1274 audio_play_wave_resource(wavesounds[wid[rand()%2]]);
1275 if(bullet_subtybe == BS_BULLET) {
1276 vec2f bloodpos = vecadd(&target->pos, &VEC(0, (2+rand()%7)*SCALE));
1277 /* original displays at 0,8 but i prefer a random effect */
1278 //vec2f bloodpos = vecadd(&target->pos, &VEC(0, 8*SCALE));
1279 int id = init_blood(&bloodpos);
1280 if(id != -1) add_blood(id);
1283 if(bullet_subtybe == BS_BULLET) {
1284 remove_bullet:
1285 gameobj_free(*bullet_id);
1286 sblist_delete(bullet_list, li);
1287 li--;
1288 goto next_bullet;
1289 } else break;
1291 point = vecadd(&point, &velquarter);
1294 next_target:;
1296 next_bullet:
1297 res = 1;
1299 return res;
1302 uint32_t tickcounter;
1304 static int advance_animations(void) {
1305 size_t i, obj_visited;
1306 int res = 0;
1307 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1308 if(!obj_slot_used[i]) continue;
1309 struct gameobj *go = &objs[i];
1310 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)) {
1311 unsigned anim_delay = go->objtype == OBJ_BIG_EXPLOSION ? 8 : 4;
1312 if(go->anim_curr == ANIM_STEP_INIT || tickcounter % anim_delay == go->anim_frame) {
1313 anim_step last_anim = go->anim_curr;
1314 go->anim_curr = get_next_anim_frame(go->animid, go->anim_curr);
1315 if(last_anim != go->anim_curr) res = 1;
1318 obj_visited++;
1320 return res;
1323 static void switch_enemy_shape(int objid, const struct enemy_route* r) {
1324 switch_anim(objid, enemy_animation_lut[r->shape]);
1327 static void draw_golist(sblist *list) {
1328 size_t i;
1329 uint8_t *itemid;
1330 sblist_iter_counter2(list, i, itemid) {
1331 assert(obj_slot_used[*itemid]);
1332 struct gameobj *o = &objs[*itemid];
1333 const prgb *palette;
1334 switch(o->objtype) {
1335 case OBJ_BLOOD:
1336 // original blood color is bb5511 but that is hardly visible
1337 // palette = (prgb[]) {PRGB(0,0,0), PRGB(0xbb, 0x55, 0x11)};
1338 palette = (const prgb[]) {PRGB(0,0,0), PRGB(0xff, 0x0, 0x0)};
1339 break;
1340 case OBJ_ENEMY_BOMBER: case OBJ_ENEMY_SHOOTER:
1341 palette = map->enemy_palette;
1342 break;
1343 default:
1344 palette = 0;
1346 blit_sprite(o->pos.x, o->pos.y, &video,
1347 SCALE, spritemaps[o->spritemap_id],
1348 get_sprite_number(*itemid),
1349 palette);
1353 static void draw_gameobjs(void) {
1354 draw_golist(&go_mines);
1355 draw_golist(&go_turrets);
1356 draw_golist(&go_bunkers);
1357 draw_golist(&go_vehicles);
1358 draw_golist(&go_enemies);
1359 draw_golist(&go_blood);
1360 draw_golist(&go_enemy_bullets);
1361 draw_golist(&go_boss);
1362 draw_golist(&go_rockets);
1363 draw_golist(&go_players);
1364 draw_golist(&go_player_bullets);
1365 draw_golist(&go_enemy_explosions);
1366 draw_golist(&go_flames);
1367 draw_golist(&go_enemy_flames);
1368 draw_golist(&go_explosions);
1369 draw_golist(&go_grenades);
1370 draw_golist(&go_enemy_grenades);
1371 draw_golist(&go_crosshair);
1372 draw_golist(&go_muzzleflash);
1375 static void process_soldiers(void) {
1376 uint8_t *itemid;
1377 sblist_iter(&go_enemies, itemid) {
1378 struct gameobj *go = &objs[*itemid];
1379 assert(go->objtype == OBJ_ENEMY_SHOOTER || go->objtype == OBJ_ENEMY_BOMBER);
1380 if (tickcounter % 4 == go->anim_frame) {
1381 const struct enemy_route *rc = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1382 if(rc->vel) {
1383 go->objspecific.enemy.curr_step++;
1384 if(enemy_fires(&go->objspecific.enemy)) {
1385 vec2f from = get_gameobj_center(*itemid);
1386 enum direction16 dir = get_shotdirection_from_enemy(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1387 enemy_fire_bullet(dir, 41, go->objspecific.enemy.spawn->weapon, &from);
1389 const struct enemy_route *rn = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1390 if(rn->shape != rc->shape) switch_enemy_shape(*itemid, rn);
1393 if(!is_death_anim(go->animid)) go->vel = get_enemy_vel(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1394 else go->vel = VEC(0, 0);
1398 static enum animation_id get_turret_anim_from_dir(int objid, enum direction dir) {
1399 static const enum animation_id man_anim[] = {
1400 [DIR_N] = ANIM_GUNTURRET_MOVABLE_MAN_N,
1401 [DIR_NO] = ANIM_GUNTURRET_MOVABLE_MAN_NO,
1402 [DIR_O] = ANIM_GUNTURRET_MOVABLE_MAN_O,
1403 [DIR_SO] = ANIM_GUNTURRET_MOVABLE_MAN_SO,
1404 [DIR_S] = ANIM_GUNTURRET_MOVABLE_MAN_S,
1405 [DIR_SW] = ANIM_GUNTURRET_MOVABLE_MAN_SW,
1406 [DIR_W] = ANIM_GUNTURRET_MOVABLE_MAN_W,
1407 [DIR_NW] = ANIM_GUNTURRET_MOVABLE_MAN_NW,
1409 static const enum animation_id mach_anim[] = {
1410 [DIR_N] = ANIM_GUNTURRET_MOVABLE_MACHINE_N,
1411 [DIR_NO] = ANIM_GUNTURRET_MOVABLE_MACHINE_NO,
1412 [DIR_O] = ANIM_GUNTURRET_MOVABLE_MACHINE_O,
1413 [DIR_SO] = ANIM_GUNTURRET_MOVABLE_MACHINE_SO,
1414 [DIR_S] = ANIM_GUNTURRET_MOVABLE_MACHINE_S,
1415 [DIR_SW] = ANIM_GUNTURRET_MOVABLE_MACHINE_SW,
1416 [DIR_W] = ANIM_GUNTURRET_MOVABLE_MACHINE_W,
1417 [DIR_NW] = ANIM_GUNTURRET_MOVABLE_MACHINE_NW,
1419 const enum animation_id *lut = objs[objid].objtype == OBJ_GUNTURRET_MOVABLE_MAN ? man_anim : mach_anim;
1420 return lut[dir];
1423 static int process_turrets(sblist* list) {
1424 int res = 0;
1425 uint8_t *itemid;
1426 sblist_iter(list, itemid) {
1427 struct gameobj *go = &objs[*itemid];
1428 if(is_death_anim(go->animid)) continue;
1429 enum direction16 dir = DIR16_S;
1430 enum enemy_weapon ew = EW_GUN;
1431 int steps = 92;
1432 vec2f from;
1433 switch(go->objtype) {
1434 case OBJ_GUNTURRET_FIXED_NORTH:
1435 dir = DIR16_N;
1436 case OBJ_GUNTURRET_FIXED_SOUTH:
1437 from = get_gameobj_center(*itemid);
1438 shottest:
1439 if(rand()%8 == 0) {
1440 shot:
1441 enemy_fire_bullet(dir, steps, ew, &from);
1442 res = 1;
1444 break;
1445 case OBJ_FLAMETURRET:
1446 ew = EW_FLAME;
1447 steps = 48;
1449 if(objs[player_ids[0]].pos.y <= (192-25)*SCALE/2) dir = DIR16_N;
1450 from = go->pos;
1451 from.y += (dir == DIR16_N ? -8*SCALE : 8*SCALE);
1453 /* abusing curr_step variable to save position in a burst of flames */
1454 if(go->objspecific.enemy.curr_step && go->objspecific.enemy.curr_step < 16)
1455 goto do_flame;
1456 else if(go->objspecific.enemy.curr_step)
1457 go->objspecific.enemy.curr_step = 0;
1459 if(objs[player_ids[0]].pos.x < go->pos.x-32*SCALE || objs[player_ids[0]].pos.x > go->pos.x + (16+32)*SCALE)
1460 break;
1461 if(rand()%8 == 0) {
1462 do_flame:
1463 go->objspecific.enemy.curr_step++;
1464 goto shot;
1466 break;
1467 case OBJ_GUNTURRET_MOVABLE_MAN:
1468 case OBJ_GUNTURRET_MOVABLE_MACHINE: {
1469 from = get_gameobj_center(*itemid);
1470 vec2f p_center = get_gameobj_center(player_ids[0]);
1471 vec2f vel = velocity(&from, &p_center);
1472 enum direction dir8 = get_direction_from_vec(&vel);
1473 dir = get_direction16_from_direction(dir8);
1474 switch_anim(*itemid, get_turret_anim_from_dir(*itemid, dir8));
1475 goto shottest;
1476 break;
1478 case OBJ_TANK_SMALL:
1479 case OBJ_BUNKER1:
1480 case OBJ_BUNKER2:
1481 case OBJ_BUNKER3:
1482 case OBJ_BUNKER4:
1483 case OBJ_BUNKER5: {
1484 const vec2f *bunkerpos;
1485 const enum direction16 *bunkerdir;
1486 unsigned count, sec;
1487 static const enum direction16 bunker5_dir[] = {
1488 [0] = DIR16_N, [1] = DIR16_NO, [2] = DIR16_O, [3] = DIR16_SO,
1489 [4] = DIR16_S, [5] = DIR16_SW, [6] = DIR16_W, [7] = DIR16_NW,
1491 static const enum direction16 bunker1_dir[] = {
1492 [0] = DIR16_NNW, [1] = DIR16_NNO, [2] = DIR16_NO, [3] = DIR16_O,
1493 [4] = DIR16_SO, [5] = DIR16_SSO, [6] = DIR16_SSW, [7] = DIR16_SW,
1494 [8] = DIR16_W, [9] = DIR16_NW,
1496 static const vec2f bunker5_pos[] = {
1497 [0] = VEC(9,-8), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(22,10),
1498 [4] = VEC(9,16), [5] = VEC(-5,13), [6] = VEC(-7,2), [7] = VEC(-5,-6),
1500 static const vec2f bunker2_pos[] = {
1501 [0] = VEC(15,-2), [1] = VEC(27,3), [2] = VEC(31,12), [3] = VEC(28,23),
1502 [4] = VEC(15,27), [5] = VEC(2,23), [6] = VEC(0,12), [7] = VEC(4,2),
1504 static const vec2f bunker4_pos[] = {
1505 [0] = VEC(13,0), [1] = VEC(25,1), [2] = VEC(29,10), [3] = VEC(26,21),
1506 [4] = VEC(13,25), [5] = VEC(0,21), [6] = VEC(-2,10), [7] = VEC(2,0),
1508 static const vec2f bunker1_pos[] = {
1509 [0] = VEC(6,-6), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(26,12),
1510 [4] = VEC(26,16), [5] = VEC(20,24), [6] = VEC(6,24), [7] = VEC(0,20),
1511 [8] = VEC(0,12), [9] = VEC(0,2),
1513 static const enum direction16 tank_dir[] = {
1514 [0] = DIR16_WSW, [1] = DIR16_OSO, [2] = DIR16_WSW, [3] = DIR16_OSO,
1515 [4] = DIR16_WSW, [5] = DIR16_OSO, [6] = DIR16_SSW, [7] = DIR16_SSO,
1517 static const vec2f tank_pos[] = {
1518 [0] = VEC(10,8), [1] = VEC(38,8), [2] = VEC(10,20), [3] = VEC(38,20),
1519 [4] = VEC(10,32), [5] = VEC(38,32), [6] = VEC(16,40), [7] = VEC(32,40),
1521 unsigned b, dist = 28;
1522 switch(go->objtype) {
1523 case OBJ_BUNKER1:
1524 ew = EW_GRENADE;
1525 bunkerpos = bunker1_pos;
1526 bunkerdir = bunker1_dir;
1527 b = 0;
1528 count = 10;
1529 sec = 2;
1530 break;
1531 case OBJ_TANK_SMALL:
1532 if(tickcounter % 8) break;
1533 ew = EW_GUN;
1534 dist = 128;
1535 bunkerpos = tank_pos;
1536 bunkerdir = tank_dir;
1537 // FIXME: the last 2 shoots (6+7) should be fired at the same time
1538 goto bunker_circle_shoot;
1539 case OBJ_BUNKER2:
1540 if(tickcounter % 8) break;
1541 ew = EW_GUN;
1542 dist = 128;
1543 bunkerpos = bunker2_pos;
1544 bunkerdir = bunker5_dir;
1545 goto bunker_circle_shoot;
1546 case OBJ_BUNKER4:
1547 if(tickcounter % /*(fps*3.5)/8*/ 24) break;
1548 ew = EW_GRENADE;
1549 dist = 32;
1550 bunkerpos = bunker4_pos;
1551 bunkerdir = bunker5_dir;
1552 bunker_circle_shoot:;
1553 b = go->objspecific.enemy.curr_step;
1554 count = b+1;
1555 if(++go->objspecific.enemy.curr_step >= 8) go->objspecific.enemy.curr_step = 0;
1556 goto bunkerloop;
1557 case OBJ_BUNKER3:
1558 ew = EW_GUN;
1559 bunkerpos = bunker2_pos;
1560 bunkerdir = bunker5_dir;
1561 b = 0;
1562 count = 8;
1563 dist = 128;
1564 sec = 1;
1565 break;
1566 case OBJ_BUNKER5:
1567 ew = EW_FLAME;
1568 bunkerpos = bunker5_pos;
1569 bunkerdir = bunker5_dir;
1570 b = 0;
1571 count = 8;
1572 sec = 3;
1573 break;
1574 default:;
1576 if(tickcounter > (uint32_t) go->objspecific.enemy.curr_step + fps*sec) {
1577 go->objspecific.enemy.curr_step = tickcounter;
1578 bunkerloop:;
1579 for(; b < count; b++) {
1580 from = go->pos;
1581 from.x += bunkerpos[b].x*SCALE;
1582 from.y += bunkerpos[b].y*SCALE;
1583 enemy_fire_bullet(bunkerdir[b], dist, ew, &from);
1585 res = 1;
1587 break;
1589 default:;
1592 return res;
1595 static int move_gameobjs(void) {
1596 int res = 0;
1597 size_t i, obj_visited;
1598 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1599 if(obj_slot_used[i]) {
1600 struct gameobj *go = &objs[i];
1601 obj_visited++;
1602 if(go->anim_curr == ANIM_STEP_INIT) res = 1;
1603 if(go->vel.x != 0 || go->vel.y != 0) {
1604 vec2f oldpos = go->pos;
1605 go->pos.x += go->vel.x;
1606 go->pos.y += go->vel.y;
1608 if(go->objtype == OBJ_P1 || go->objtype == OBJ_P2) {
1609 if(go->pos.y < SCREEN_MIN_Y) go->pos.y = SCREEN_MIN_Y;
1610 else if(go->pos.y+25*SCALE > SCREEN_MAX_Y) go->pos.y = SCREEN_MAX_Y-25*SCALE;
1611 if(go->pos.x < SCREEN_MIN_X) go->pos.x = SCREEN_MIN_X;
1612 else if(go->pos.x+32*SCALE > SCREEN_MAX_X) go->pos.x = SCREEN_MAX_X-32*SCALE;
1613 vec2f center = get_sprite_center(spritemaps[go->spritemap_id]);
1614 center = vecadd(&center, &go->pos);
1615 if(is_wall(&center)) {
1616 go->pos = oldpos;
1617 go->vel = VEC(0,0);
1621 res = 1;
1623 if((go->objtype == OBJ_ENEMY_BOMBER || go->objtype == OBJ_ENEMY_SHOOTER) &&
1624 go->anim_curr == animations[go->animid].last &&
1625 (go->animid == ANIM_ENEMY_BOMBER_DIE ||
1626 go->animid == ANIM_ENEMY_GUNNER_DIE ||
1627 go->animid == ANIM_ENEMY_BURNT)) {
1628 dprintf(2, "removed enemy from %.2f,%.2f\n", go->pos.x, go->pos.y);
1629 gameobj_free(i);
1630 golist_remove(&go_enemies, i);
1631 res = 1;
1632 continue;
1637 return res;
1640 static void game_update_caption(void) {
1641 char buf [128];
1642 snprintf(buf, 128, "objs: %d, map x,y %d/%d, index %d, xoff %d, yoff %d, spawnscreen %d, line %d", (int) obj_count,
1643 (int)mapsquare.x, (int)mapsquare.y, (int)map->screen_map[mapsquare.y][mapsquare.x],
1644 (int)mapscreen_xoff, (int)mapscreen_yoff, (int)map_spawn_screen_index, (int) map_spawn_line);
1645 SDL_WM_SetCaption(buf, 0);
1647 static void(*update_caption)(void) = game_update_caption;
1649 /* returns 1 if level finished */
1650 static int game_tick(int force_redraw) {
1651 int need_redraw = force_redraw;
1652 if(mousebutton_down[MB_LEFT] > 1) {
1653 const int player_no = 0;
1654 const struct weapon *pw = get_active_weapon(player_no);
1655 //if(get_active_weapon_id(player_no) == WP_M134) __asm__("int3");
1656 if (pw->flags & WF_AUTOMATIC) {
1657 float shots_per_second = pw->rpm / 60.f;
1658 float shotinterval = fps / shots_per_second;
1659 if((int)((float)mousebutton_down[MB_LEFT] / shotinterval) != (int)((float)(mousebutton_down[MB_LEFT]-1) / shotinterval))
1660 fire_bullet(player_no);
1664 if(advance_animations()) need_redraw = 1;
1665 if(hit_bullets(&go_player_bullets, &go_enemies)) need_redraw = 1;
1666 if(hit_bullets(&go_player_bullets, &go_vehicles)) need_redraw = 1;
1667 if(hit_bullets(&go_flames, &go_enemies)) need_redraw = 1;
1668 if(hit_bullets(&go_enemy_flames, &go_enemies)) need_redraw = 1;
1669 if(hit_bullets(&go_rockets, &go_enemies)) need_redraw = 1;
1670 if(hit_bullets(&go_rockets, &go_vehicles)) need_redraw = 1;
1671 if(hit_bullets(&go_explosions, &go_enemies)) need_redraw = 1;
1672 if(hit_bullets(&go_explosions, &go_vehicles)) need_redraw = 1;
1673 if(hit_bullets(&go_explosions, &go_mines)) need_redraw = 1;
1674 if(hit_bullets(&go_explosions, &go_turrets)) need_redraw = 1;
1675 if(hit_bullets(&go_explosions, &go_bunkers)) need_redraw = 1;
1676 if(hit_bullets(&go_explosions, &go_players)) need_redraw = 1;
1677 if(hit_bullets(&go_enemy_explosions, &go_players)) need_redraw = 1;
1678 if(hit_bullets(&go_enemy_bullets, &go_players)) need_redraw = 1;
1679 if(hit_bullets(&go_enemy_flames, &go_players)) need_redraw = 1;
1680 if(hit_bullets(&go_vehicles, &go_players)) need_redraw = 1;
1681 if(hit_bullets(&go_enemies, &go_players)) need_redraw = 1;
1682 if(hit_bullets(&go_mines, &go_players)) need_redraw = 1;
1684 int ret, level_finished = 0;
1685 if((ret = hit_bullets(&go_boss, &go_players)) == 2) level_finished = 1;
1686 else if(ret == 1) need_redraw = 1;
1688 if(remove_bullets(&go_player_bullets)) need_redraw = 1;
1689 if(remove_bullets(&go_flames)) need_redraw = 1;
1690 if(remove_bullets(&go_explosions)) need_redraw = 1;
1691 if(remove_bullets(&go_enemy_explosions)) need_redraw = 1;
1692 if(remove_bullets(&go_enemy_bullets)) need_redraw = 1;
1693 if(remove_bullets(&go_enemy_flames)) need_redraw = 1;
1694 if(remove_bullets(&go_muzzleflash)) need_redraw = 1;
1695 if(remove_bullets(&go_blood)) need_redraw = 1;
1696 if(remove_explosives(&go_grenades)) need_redraw = 1;
1697 if(remove_explosives(&go_enemy_grenades)) need_redraw = 1;
1698 if(remove_explosives(&go_rockets)) need_redraw = 1;
1699 if(tickcounter % 2 == 0 && scroll_map()) need_redraw = 1;
1701 process_soldiers();
1702 if(tickcounter % 4 == 0 && process_turrets(&go_turrets)) need_redraw = 1;
1703 if(tickcounter % 4 == 0 && process_turrets(&go_bunkers)) need_redraw = 1;
1704 if(tickcounter % 4 == 0 && process_turrets(&go_vehicles)) need_redraw = 1;
1706 if(move_gameobjs()) need_redraw = 1;
1708 if(remove_offscreen_objects(&go_enemies)) need_redraw = 1;
1709 if(remove_offscreen_objects(&go_vehicles)) need_redraw = 1;
1710 if(remove_offscreen_objects(&go_mines)) need_redraw = 1;
1711 if(remove_offscreen_objects(&go_turrets)) need_redraw = 1;
1712 if(remove_offscreen_objects(&go_bunkers)) need_redraw = 1;
1714 long ms_used = 0;
1715 struct timeval timer;
1716 gettimestamp(&timer);
1717 if(need_redraw) {
1718 draw_map();
1719 draw_gameobjs();
1720 draw_status_bar();
1721 video_update_region(SCREEN_MIN_X ,SCREEN_MIN_Y , SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1724 ms_used = mspassed(&timer);
1725 //if(ms_used) printf("repaint took: ms_used %ld\n", ms_used);
1726 int res = audio_process();
1727 if(res == -1) music_restart();
1728 ms_used = mspassed(&timer);
1729 //if(ms_used) printf("audio processed: %d, ms_used %ld\n", res, ms_used);
1731 long sleepms = 1000/fps - ms_used;
1732 if(sleepms >= 0) SDL_Delay(sleepms);
1733 if(mousebutton_down[MB_LEFT]) mousebutton_down[MB_LEFT]++;
1735 tickcounter++;
1736 update_caption();
1738 return level_finished;
1741 enum cursor {
1742 c_down = 0,
1743 c_up,
1744 c_left,
1745 c_right,
1748 static enum cursor cursor_lut[] = {
1749 [SDLK_UP] = c_up,
1750 [SDLK_DOWN] = c_down,
1751 [SDLK_LEFT] = c_left,
1752 [SDLK_RIGHT] = c_right,
1753 [SDLK_w] = c_up,
1754 [SDLK_a] = c_left,
1755 [SDLK_s] = c_down,
1756 [SDLK_d] = c_right,
1759 static char cursors_pressed[] = {
1760 [c_up] = 0,
1761 [c_down] = 0,
1762 [c_left] = 0,
1763 [c_right] = 0,
1766 static vec2f get_vel_from_direction(enum direction dir, float speed) {
1767 #define VELLUT(a, b, c) [a] = VEC(b, c)
1768 static const vec2f vel_lut[] = {
1769 VELLUT(DIR_O, 1, 0),
1770 VELLUT(DIR_NO, 0.7071067769704655, -0.7071067769704655),
1771 VELLUT(DIR_N, 0, -1),
1772 VELLUT(DIR_NW, -0.7071067769704655, -0.7071067769704655),
1773 VELLUT(DIR_W, -1, 0),
1774 VELLUT(DIR_SW, -0.7071067769704655, 0.7071067769704655),
1775 VELLUT(DIR_S, 0, 1),
1776 VELLUT(DIR_SO, 0.7071067769704655, 0.7071067769704655),
1778 #undef VELLUT
1779 vec2f v = vel_lut[dir];
1780 v.x *= speed * SCALE;
1781 v.y *= speed * SCALE;
1782 return v;
1785 static vec2f get_vel_from_direction16(enum direction16 dir, float speed) {
1786 #define VELLUT(a, b, c) [a] = VEC(b, c)
1787 #define ANK90 0.7071067769704655
1788 #define GK90 ANK90
1789 #define ANK45 0.9238795042037964
1790 #define GK45 0.3826834261417389
1791 static const vec2f vel_lut[] = {
1792 VELLUT(DIR16_O, 1, 0),
1793 VELLUT(DIR16_ONO, ANK45, -GK45),
1794 VELLUT(DIR16_NO, ANK90, -ANK90),
1795 VELLUT(DIR16_NNO, GK45, -ANK45),
1796 VELLUT(DIR16_N, 0, -1),
1797 VELLUT(DIR16_NNW, -GK45, -ANK45),
1798 VELLUT(DIR16_NW, -ANK90, -ANK90),
1799 VELLUT(DIR16_WNW, -ANK45, -GK45),
1800 VELLUT(DIR16_W, -1, 0),
1801 VELLUT(DIR16_WSW, -ANK45, GK45),
1802 VELLUT(DIR16_SW, -ANK90, ANK90),
1803 VELLUT(DIR16_SSW, -GK45, ANK45),
1804 VELLUT(DIR16_S, 0, 1),
1805 VELLUT(DIR16_SSO, GK45, ANK45),
1806 VELLUT(DIR16_SO, ANK90, ANK90),
1807 VELLUT(DIR16_OSO, ANK45, GK45),
1809 #undef VELLUT
1810 vec2f v = vel_lut[dir];
1811 v.x *= speed * SCALE;
1812 v.y *= speed * SCALE;
1813 return v;
1817 static enum direction get_direction_from_vec(vec2f *vel) {
1818 float deg_org, deg = atan2(vel->y, vel->x);
1819 deg_org = deg;
1820 if(deg < 0) deg *= -1.f;
1821 else if(deg > 0) deg = M_PI + (M_PI - deg); // normalize atan2 result to scale from 0 to 2 pi
1822 int hexadrant = (int)(deg / ((1.0/16.0f)*2*M_PI));
1823 assert(hexadrant >= 0 && hexadrant < 16);
1824 static const enum direction rad_lut[] = {
1825 DIR_O,
1826 DIR_NO, DIR_NO,
1827 DIR_N, DIR_N,
1828 DIR_NW, DIR_NW,
1829 DIR_W, DIR_W,
1830 DIR_SW, DIR_SW,
1831 DIR_S, DIR_S,
1832 DIR_SO, DIR_SO,
1833 DIR_O
1835 return rad_lut[hexadrant];
1838 static enum direction get_direction_from_cursor(void) {
1839 enum direction dir = DIR_INVALID;
1840 if(cursors_pressed[c_up]) {
1841 if(cursors_pressed[c_left]) dir = DIR_NW;
1842 else if(cursors_pressed[c_right]) dir = DIR_NO;
1843 else dir = DIR_N;
1844 } else if (cursors_pressed[c_down]) {
1845 if(cursors_pressed[c_left]) dir = DIR_SW;
1846 else if(cursors_pressed[c_right]) dir = DIR_SO;
1847 else dir = DIR_S;
1848 } else if (cursors_pressed[c_left]) {
1849 dir = DIR_W;
1850 } else if (cursors_pressed[c_right]) {
1851 dir = DIR_O;
1853 return dir;
1856 static enum animation_id get_anim_from_direction(enum direction dir, int player_no, int throwing) {
1857 #define DIRMAP(a, b) [a] = b
1858 if(!throwing) {
1859 static const enum animation_id dir_map_p1[] = {
1860 DIRMAP(DIR_N, ANIM_P1_MOVE_N),
1861 DIRMAP(DIR_NW, ANIM_P1_MOVE_NW),
1862 DIRMAP(DIR_W, ANIM_P1_MOVE_W),
1863 DIRMAP(DIR_SW, ANIM_P1_MOVE_SW),
1864 DIRMAP(DIR_S, ANIM_P1_MOVE_S),
1865 DIRMAP(DIR_SO, ANIM_P1_MOVE_SO),
1866 DIRMAP(DIR_O, ANIM_P1_MOVE_O),
1867 DIRMAP(DIR_NO, ANIM_P1_MOVE_NO),
1869 static const enum animation_id dir_map_p2[] = {
1870 DIRMAP(DIR_N, ANIM_P2_MOVE_N),
1871 DIRMAP(DIR_NW, ANIM_P2_MOVE_NW),
1872 DIRMAP(DIR_W, ANIM_P2_MOVE_W),
1873 DIRMAP(DIR_SW, ANIM_P2_MOVE_SW),
1874 DIRMAP(DIR_S, ANIM_P2_MOVE_S),
1875 DIRMAP(DIR_SO, ANIM_P2_MOVE_SO),
1876 DIRMAP(DIR_O, ANIM_P2_MOVE_O),
1877 DIRMAP(DIR_NO, ANIM_P2_MOVE_NO),
1879 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1 : dir_map_p2;
1880 return dir_map[dir];
1881 } else {
1882 static const enum animation_id dir_map_p1_g[] = {
1883 DIRMAP(DIR_N, ANIM_P1_THROW_N),
1884 DIRMAP(DIR_NW, ANIM_P1_THROW_NW),
1885 DIRMAP(DIR_W, ANIM_P1_THROW_W),
1886 DIRMAP(DIR_SW, ANIM_P1_THROW_SW),
1887 DIRMAP(DIR_S, ANIM_P1_THROW_S),
1888 DIRMAP(DIR_SO, ANIM_P1_THROW_SO),
1889 DIRMAP(DIR_O, ANIM_P1_THROW_O),
1890 DIRMAP(DIR_NO, ANIM_P1_THROW_NO),
1892 static const enum animation_id dir_map_p2_g[] = {
1893 DIRMAP(DIR_N, ANIM_P2_THROW_N),
1894 DIRMAP(DIR_NW, ANIM_P2_THROW_NW),
1895 DIRMAP(DIR_W, ANIM_P2_THROW_W),
1896 DIRMAP(DIR_SW, ANIM_P2_THROW_SW),
1897 DIRMAP(DIR_S, ANIM_P2_THROW_S),
1898 DIRMAP(DIR_SO, ANIM_P2_THROW_SO),
1899 DIRMAP(DIR_O, ANIM_P2_THROW_O),
1900 DIRMAP(DIR_NO, ANIM_P2_THROW_NO),
1902 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1_g : dir_map_p2_g;
1903 return dir_map[dir];
1905 #undef DIRMAP
1908 #if 0
1909 static enum animation_id get_anim_from_cursor(void) {
1910 enum direction dir = get_direction_from_cursor();
1911 if(dir == DIR_INVALID) return ANIM_INVALID;
1912 return get_anim_from_direction(dir, 0, 0);
1914 /* playerno is either 0 or 1, not player_id! */
1915 static enum animation_id get_anim_from_vel(int player_no, vec2f *vel) {
1916 enum direction dir = get_direction_from_vec(vel);
1917 if(dir == DIR_INVALID) return ANIM_INVALID;
1918 return get_anim_from_direction(dir, player_no, 0);
1920 #endif
1922 static void switch_anim(int obj_id, int aid) {
1923 if(objs[obj_id].animid == aid) return;
1924 gameobj_start_anim(obj_id, aid);
1927 enum map_index choose_mission(uint8_t* completed);
1928 //RcB: DEP "mission_select.c"
1929 #include "enemytag.c"
1931 static uint8_t mission_completed[MI_MAX];
1933 static void game_delay(int s) {
1934 /* wait for s frames while continuing to play music */
1935 int x = s;
1936 while(x){
1937 audio_process();
1938 SDL_Delay(1000/fps);
1939 x--;
1940 tickcounter++;
1944 static void finish_level(void) {
1945 music_play(TUNE_TITLE);
1946 video_darken_screen();
1947 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1948 #define STRLSZ(x) (x), sizeof(x)-1
1949 font_print(SCREEN_MIN_X+40*SCALE, SCREEN_MIN_Y+13*SCALE, STRLSZ("congratulations"), SCALE, PRGB(255,255,255));
1950 font_print(SCREEN_MIN_X+24*SCALE, SCREEN_MIN_Y+24*SCALE, STRLSZ("you have completed"), SCALE, PRGB(255,255,255));
1951 font_print(SCREEN_MIN_X+48*SCALE, SCREEN_MIN_Y+35*SCALE, STRLSZ("your mission"), SCALE, PRGB(255,255,255));
1952 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1953 game_delay(2*fps);
1954 font_print(SCREEN_MIN_X+40*SCALE, SCREEN_MIN_Y+65*SCALE, STRLSZ("revenge bonus"), SCALE, PRGB(255,255,255));
1955 font_print(SCREEN_MIN_X+32*SCALE, SCREEN_MIN_Y+89*SCALE, STRLSZ("1."), SCALE, PRGB(255,255,255));
1956 uint32_t bgbuf[8*SCALE*((159-64)*SCALE)];
1957 video_save_rect(SCREEN_MIN_X+64*SCALE,SCREEN_MIN_Y+89*SCALE,(159-64)*SCALE,8*SCALE,bgbuf);
1958 char buf[16];
1959 // FIXME: figure out where initial $ value comes from. it's 50% of the map reward + something yet unknown
1960 #define UNK 0
1961 int dollars = maps[current_map]->rewardk*500+UNK;
1962 snprintf(buf, sizeof buf, "%.4d $%.6d", player_kills[0], dollars);
1963 font_print(SCREEN_MIN_X+64*SCALE, SCREEN_MIN_Y+89*SCALE, buf, 12, SCALE, PRGB(255,255,255));
1964 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1965 game_delay(1*fps);
1966 tickcounter = 0;
1967 while(player_kills[0]) {
1968 dollars+=10;
1969 player_kills[0]--;
1970 video_restore_rect(SCREEN_MIN_X+64*SCALE,SCREEN_MIN_Y+89*SCALE,(159-64)*SCALE,8*SCALE,bgbuf);
1971 snprintf(buf, sizeof buf, "%.4d $%.6d", player_kills[0], dollars);
1972 font_print(SCREEN_MIN_X+64*SCALE, SCREEN_MIN_Y+89*SCALE, buf, 12, SCALE, PRGB(255,255,255));
1973 video_update_region(SCREEN_MIN_X, SCREEN_MIN_Y, SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1974 if(tickcounter % 4 == 0) audio_play_wave_resource(wavesounds[WS_COUNTDOWN]);
1975 // FIXME: when no music is played, sound does not play either... maybe add "empty song"?
1976 game_delay(1);
1978 music_play(TUNE_LEVEL_FINISHED);
1979 game_delay(3*fps);
1981 mission_completed[current_map] = 1;
1984 int main() {
1985 video_init();
1986 clear_screen();
1987 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
1988 SDL_EnableKeyRepeat(100, 20);
1990 audio_init();
1992 mission_select:
1994 SDL_ShowCursor(1);
1996 /* background music for mission selection screen */
1997 music_play(TUNE_MAP);
1999 if((current_map = choose_mission(mission_completed)) == MI_INVALID) goto dun_goofed;
2001 music_play(TUNE_FIGHTING);
2003 SDL_ShowCursor(0);
2005 int startx = 10;
2006 int starty = 10;
2008 //redraw(surface, startx, starty);
2009 const float player_speed = 1.25f;
2010 const struct palpic* spritemap = spritemaps[SI_PLAYERS];
2011 struct { int *target; int dir; int max;} moves[] = {
2012 [c_up] = {&starty, SCALE * -1, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
2013 [c_down] = {&starty, SCALE, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
2014 [c_left] = {&startx, SCALE * -1, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
2015 [c_right] = {&startx, SCALE, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
2018 init_game_objs();
2019 int player_no = 0;
2020 int player_id = player_ids[player_no];
2022 game_tick(1);
2024 SDL_Event sdl_event;
2025 while(1) {
2026 unsigned need_redraw = 0;
2027 int weapon_inc = 0;
2028 while (SDL_PollEvent(&sdl_event)) {
2029 need_redraw = 0;
2030 switch (sdl_event.type) {
2031 case SDL_MOUSEMOTION:
2032 if((int)mousepos->x != sdl_event.motion.x || (int)mousepos->y != sdl_event.motion.y)
2033 need_redraw = 1;
2034 mousepos->x = sdl_event.motion.x;
2035 mousepos->y = sdl_event.motion.y;
2036 break;
2037 case SDL_MOUSEBUTTONDOWN:
2038 mousepos->x = sdl_event.button.x;
2039 mousepos->y = sdl_event.button.y;
2040 mousebutton_down[MB_LEFT] = 1;
2041 fire_bullet(player_no);
2042 break;
2043 case SDL_MOUSEBUTTONUP:
2044 mousepos->x = sdl_event.button.x;
2045 mousepos->y = sdl_event.button.y;
2046 mousebutton_down[MB_LEFT] = 0;
2047 break;
2048 case SDL_QUIT:
2049 dun_goofed:
2050 video_cleanup();
2051 return 0;
2052 case SDL_KEYDOWN:
2053 switch(sdl_event.key.keysym.sym) {
2054 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
2055 case SDLK_UP:
2056 case SDLK_DOWN:
2057 case SDLK_RIGHT:
2058 case SDLK_LEFT:
2059 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 1;
2060 check_anim: {
2061 enum direction dir = get_direction_from_cursor();
2062 if(dir != DIR_INVALID) {
2063 if(!mousebutton_down[MB_LEFT]) {
2064 // change animation according to cursors,
2065 // unless we're in automatic fire mode.
2066 enum animation_id aid = get_anim_from_direction(dir, player_no, 0);
2067 if(aid != ANIM_INVALID) switch_anim(player_id, aid);
2069 objs[player_id].vel = get_vel_from_direction(dir, player_speed);
2070 } else {
2071 objs[player_id].vel = VEC(0,0);
2074 break;
2075 case SDLK_RETURN:
2076 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
2077 (sdl_event.key.keysym.mod & KMOD_RALT)) {
2078 toggle_fullscreen();
2079 SDL_Delay(1);
2080 game_tick(1);
2081 need_redraw = 1;
2083 break;
2084 case SDLK_KP_PLUS:
2085 weapon_inc = 1;
2086 goto toggle_weapon;
2087 case SDLK_KP_MINUS:
2088 if((int)player_weapons[player_no][0] == 0)
2089 weapon_inc = WP_MAX-1;
2090 else weapon_inc = -1;
2091 toggle_weapon:
2092 player_weapons[player_no][0] += weapon_inc;
2093 if(player_weapons[player_no][0] == WP_INVALID)
2094 player_weapons[player_no][0] = 0;
2095 printf("%s\n", weapon_name(player_weapons[player_no][0]));
2096 need_redraw = 1;
2097 break;
2098 default:
2099 break;
2101 break;
2102 case SDL_KEYUP:
2103 switch(sdl_event.key.keysym.sym) {
2104 case SDLK_e:
2105 enemy_tag_loop();
2106 update_caption = game_update_caption;
2107 break;
2108 case SDLK_c:
2109 clear_screen();
2110 video_update();
2111 need_redraw = 1;
2112 break;
2113 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
2114 case SDLK_UP:
2115 case SDLK_DOWN:
2116 case SDLK_RIGHT:
2117 case SDLK_LEFT:
2118 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 0;
2119 goto check_anim;
2120 case SDLK_ESCAPE:
2121 goto dun_goofed;
2122 default:
2123 break;
2125 default:
2126 break;
2129 unsigned i;
2130 for (i = 0; i < ARRAY_SIZE(cursors_pressed); i++) {
2131 if(cursors_pressed[i]) {
2132 *moves[i].target += moves[i].dir;
2133 if(*moves[i].target < 0) *moves[i].target = 0;
2134 if(*moves[i].target > moves[i].max) *moves[i].target = moves[i].max;
2135 need_redraw = 1;
2138 if(game_tick(need_redraw)) {
2139 /* completed map */
2140 finish_level();
2141 goto mission_select;
2145 return 0;