complete is_death_anim
[rofl0r-openDOW.git] / spriteview.c
blob934216f380ba8368bb1cb9d24960679ed1a9722b
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 enum weapon_id player_weapons[2][WP_MAX];
80 static int weapon_count[2];
81 static enum weapon_id weapon_active[2]; // index into player_weapons[playerno]
82 static int player_ammo[2][AMMO_MAX];
83 static enum weapon_id get_active_weapon_id(int player_no);
84 static void switch_anim(int obj_id, int aid);
85 static vec2f get_vel_from_direction(enum direction dir, float speed);
86 static vec2f get_vel_from_direction16(enum direction16 dir, float speed);
87 // used by game_tick
88 static sblist go_player_bullets;
89 static sblist go_enemy_bullets;
90 static sblist go_explosions;
91 static sblist go_enemy_explosions;
92 static sblist go_enemies;
93 static sblist go_players;
94 static sblist go_flames;
95 static sblist go_enemy_flames;
96 static sblist go_rockets;
97 static sblist go_grenades;
98 static sblist go_enemy_grenades;
99 static sblist go_vehicles;
100 static sblist go_mines;
101 static sblist go_turrets;
102 static sblist go_bunkers;
103 static sblist go_boss;
104 static sblist go_crosshair;
105 static sblist go_muzzleflash;
106 static sblist go_blood;
107 static void add_pbullet(uint8_t bullet_id) {
108 sblist_add(&go_player_bullets, &bullet_id);
110 static void add_ebullet(uint8_t bullet_id) {
111 sblist_add(&go_enemy_bullets, &bullet_id);
113 static void add_player(uint8_t player_id) {
114 sblist_add(&go_players, &player_id);
116 static void add_enemy(uint8_t enem_id) {
117 sblist_add(&go_enemies, &enem_id);
119 static void add_explosion(uint8_t expl_id) {
120 sblist_add(&go_explosions, &expl_id);
122 static void add_enemy_explosion(uint8_t expl_id) {
123 sblist_add(&go_enemy_explosions, &expl_id);
125 static void add_flame(uint8_t id) {
126 sblist_add(&go_flames, &id);
128 static void add_grenade(uint8_t id) {
129 sblist_add(&go_grenades, &id);
131 static void add_enemy_grenade(uint8_t id) {
132 sblist_add(&go_enemy_grenades, &id);
134 static void add_rocket(uint8_t id) {
135 sblist_add(&go_rockets, &id);
137 static void add_vehicle(uint8_t id) {
138 sblist_add(&go_vehicles, &id);
140 static void add_mine(uint8_t id) {
141 sblist_add(&go_mines, &id);
143 static void add_turret(uint8_t id) {
144 sblist_add(&go_turrets, &id);
146 static void add_bunker(uint8_t id) {
147 sblist_add(&go_bunkers, &id);
149 static void add_boss(uint8_t id) {
150 sblist_add(&go_boss, &id);
152 static void add_crosshair(uint8_t id) {
153 sblist_add(&go_crosshair, &id);
155 static void add_muzzleflash(uint8_t id) {
156 sblist_add(&go_muzzleflash, &id);
158 static void add_blood(uint8_t id) {
159 sblist_add(&go_blood, &id);
161 static void add_enemy_flame(uint8_t id) {
162 sblist_add(&go_enemy_flames, &id);
164 static void golist_remove(sblist *l, uint8_t objid) {
165 size_t i;
166 uint8_t *itemid;
167 sblist_iter_counter2(l, i, itemid) {
168 if(*itemid == objid) {
169 sblist_delete(l, i);
170 return;
175 static int get_next_anim_frame(enum animation_id aid, anim_step curr) {
176 if(curr == ANIM_STEP_INIT) return animations[aid].first;
177 curr++;
178 if(curr > animations[aid].last) return animations[aid].first;
179 return curr;
182 #define SCREEN_MIN_X 64*SCALE
183 #define SCREEN_MAX_X VMODE_W - 64*SCALE
184 #define SCREEN_MIN_Y 0
185 #define SCREEN_MAX_Y 200*SCALE
187 static void draw_status_bar(void) {
188 enum weapon_id wid = get_active_weapon_id(0);
189 int x, y;
190 sdl_rgb_t *ptr = (sdl_rgb_t *) video.mem;
191 unsigned pitch = video.pitch/4;
192 for(y = SCREEN_MAX_Y; y < VMODE_H; y++)
193 for (x = SCREEN_MIN_X; x < SCREEN_MAX_X; x++)
194 ptr[y*pitch + x] = SRGB_BLACK;
196 blit_sprite(((320 / 2) - (59 / 2)) * SCALE, (200 + (40/2) - (16/2)) * SCALE,
197 &video, SCALE, &weapon_sprites.header, wid, 0);
199 char buf[16];
200 snprintf(buf, 16, "%.6u", objs[player_ids[0]].objspecific.playerdata.score);
201 font_print(SCREEN_MIN_X + 8, SCREEN_MAX_Y + 8, buf, 6, 1 * SCALE, PRGB(255,255,255));
204 enum map_index current_map = MI_VIETNAM;
205 const struct map *map;
206 const struct map_screen* map_scr;
207 const struct palpic *map_bg;
208 const struct palpic *map_fg;
209 const mapscreen_index *map_bonus_indices;
210 const struct map_fglayer *map_bonus_scr;
212 int mapscreen_yoff, mapscreen_xoff;
213 struct { int x,y; } mapsquare;
214 enum map_scrolldir mapscrolldir;
215 unsigned map_spawn_screen_index;
216 unsigned map_spawn_line;
217 unsigned map_spawn_current;
219 static const int fps = 64;
221 static void init_map(enum map_index mapindex) {
222 map = maps[mapindex];
223 map_scr = map_screens[mapindex];
224 map_bg = map_bg_sprites[map->maptype];
225 map_fg = map_fg_sprites[map->maptype];
226 map_bonus_scr = map_bonus_screens[mapindex];
227 map_bonus_indices = map_bonus_layer_indices[mapindex];
228 mapscreen_yoff = 0;
229 mapscreen_xoff = 0;
230 mapsquare.x = 5;
231 mapsquare.y = 26;
232 mapscrolldir = MS_UP;
233 map_spawn_screen_index = 0;
234 map_spawn_line = 0;
235 map_spawn_current = 0;
238 static mapscreen_index get_bonus_layer_index(mapscreen_index screen) {
239 unsigned i;
240 for (i = 0; i < map->bonuslayer_count; i++)
241 if(map_bonus_indices[i] == screen) return i;
242 return MAPSCREEN_BLOCKED;
245 static mapscreen_index screen_to_mapscreen(int *x, int *y) {
246 *x = ((int) *x - SCREEN_MIN_X) / SCALE;
247 *y = ((int) *y - SCREEN_MIN_Y) / SCALE;
248 int yscr = (*y + mapscreen_yoff) / 192;
249 *y = (*y + mapscreen_yoff) - yscr*192;
250 int xscr = (*x + mapscreen_xoff) / 192;
251 *x = (*x + mapscreen_xoff) - xscr*192;
252 return map->screen_map[mapsquare.y + yscr][mapsquare.x + xscr];
255 static enum walltype is_wall(vec2f *pos) {
256 int x = pos->x;
257 int y = pos->y;
258 mapscreen_index scr_idx = screen_to_mapscreen(&x, &y);
259 /* can happen when a bullet goes partially off-screen */
260 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
261 uint8_t spriteno = map_scr[scr_idx].fg.fg[y/16][x/16];
262 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
263 scr_idx = get_bonus_layer_index(scr_idx);
264 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
265 spriteno = map_bonus_scr[scr_idx].fg[y/16][x/16];
266 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
267 return WT_NONE;
270 static void draw_map() {
271 int y, x, my, mx;
272 unsigned map_off = 192-mapscreen_yoff;
273 unsigned vis_x = 192-mapscreen_xoff;
274 int x_iter_max16 = 192/16;
275 int x_iter_max64 = 192/64;
276 unsigned x_iter_start64 = x_iter_max64 - (vis_x / 64 + !!(vis_x % 64));
277 unsigned x_iter_start16 = x_iter_max16 - (vis_x / 16 + !!(vis_x % 16));
279 int x_screen_start64 = -(!!(vis_x%64)*64-(vis_x%64));
280 int x_screen_start16 = -(!!(vis_x%16)*16-(vis_x%16));
282 unsigned x_screen_iter;
283 for(x_screen_iter = 0; x_screen_iter <= !!mapscreen_xoff; x_screen_iter++) {
284 mapscreen_index bonus_layer;
285 if(x_screen_iter) {
286 x_screen_start16 = x_screen_start16+(x_iter_max16-x_iter_start16)*16;
287 x_screen_start64 = x_screen_start64+(x_iter_max64-x_iter_start64)*64;
288 x_iter_max16 = mapscreen_xoff / 16 + !!(mapscreen_xoff % 16);
289 x_iter_max64 = mapscreen_xoff / 64 + !!(mapscreen_xoff % 64);
290 x_iter_start16 = 0;
291 x_iter_start64 = 0;
292 assert(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter] != MAPSCREEN_BLOCKED);
294 uint8_t spriteno;
296 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)
297 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
298 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
299 blit_sprite(x, y, &video,
300 SCALE, map_bg, spriteno, 0);
302 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)
303 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
304 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
305 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
307 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]);
308 if(bonus_layer != MAPSCREEN_BLOCKED) {
309 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)
310 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
311 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
312 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
317 int yleft = 200-map_off;
318 if(yleft > 192) yleft = 192;
319 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/32+!!(yleft%32); my++, y+=32*SCALE)
320 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
321 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
322 blit_sprite(x, y, &video, SCALE, map_bg, spriteno, 0);
324 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
325 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
326 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
327 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
330 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]);
331 if(bonus_layer != MAPSCREEN_BLOCKED) {
332 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
333 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
334 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
335 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
339 /* this is never triggered when mapscreen_xoff != 0 */
340 if(mapscreen_yoff > 192 - 8) {
341 for(mx = 0, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < 3; mx++, x += 64*SCALE)
342 blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video,
343 SCALE, map_bg, map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].bg.bg[0][mx], 0);
344 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
345 spriteno = map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].fg.fg[0][mx];
346 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
348 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+2][mapsquare.x]);
349 if(bonus_layer != MAPSCREEN_BLOCKED) {
350 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
351 spriteno = map_bonus_scr[bonus_layer].fg[0][mx];
352 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
359 #define VSCROLL_TRESHOLD (200-74)
360 #define HSCROLLR_TRESHOLD (54-6)
361 #define HSCROLLL_TRESHOLD (192-(78+3))
362 static int scroll_needed() {
363 struct gameobj *player = &objs[player_ids[0]];
364 if((mapscrolldir == MS_UP && player->pos.y - SCREEN_MIN_Y < VSCROLL_TRESHOLD*SCALE) ||
365 (mapscrolldir == MS_RIGHT && player->pos.x - SCREEN_MIN_X > HSCROLLR_TRESHOLD*SCALE) ||
366 (mapscrolldir == MS_LEFT && player->pos.x - SCREEN_MIN_X < HSCROLLL_TRESHOLD*SCALE))
367 return 1;
368 return 0;
371 static void scroll_gameobjs(int scroll_step) {
372 if(!scroll_step) return;
373 unsigned i, avail = obj_count;
374 for(i = 0; i < OBJ_MAX && avail; i++) {
375 if(!obj_slot_used[i]) continue;
376 avail--;
377 if(objs[i].objtype == OBJ_CROSSHAIR) continue;
379 if(mapscrolldir == MS_UP)
380 objs[i].pos.y += scroll_step*SCALE;
381 else if(mapscrolldir == MS_LEFT)
382 objs[i].pos.x += scroll_step*SCALE;
383 else if(mapscrolldir == MS_RIGHT)
384 objs[i].pos.x -= scroll_step*SCALE;
388 static void next_screen() {
389 map_spawn_screen_index++;
390 map_spawn_line = 0;
391 map_spawn_current = 0;
394 static int init_enemy(const struct enemy_spawn *spawn);
395 static void handle_spawns(unsigned scrollstep) {
396 assert(scrollstep <= 192);
397 unsigned i;
398 const struct enemy_spawn_screen *spawn_map = spawn_maps[current_map];
399 if(!spawn_map[map_spawn_screen_index].spawns) goto done;
400 for(i = 0; i < scrollstep; i++) {
401 while(map_spawn_current < spawn_map[map_spawn_screen_index].num_spawns &&
402 map_spawn_line+i >= spawn_map[map_spawn_screen_index].spawns[map_spawn_current].scroll_line) {
403 init_enemy(&spawn_map[map_spawn_screen_index].spawns[map_spawn_current]);
404 map_spawn_current++;
407 done:
408 map_spawn_line += scrollstep;
411 static int scroll_map() {
412 int ret = 0;
413 int scroll_step = 1;
414 if(scroll_needed()) {
415 if(mapscrolldir == MS_UP) {
416 mapscreen_yoff -= scroll_step;
417 if(mapscreen_yoff < 0) {
418 mapsquare.y--;
419 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
420 scroll_step = -mapscreen_yoff;
421 mapscreen_yoff = 0;
422 mapsquare.y++;
423 scroll_gameobjs(scroll_step);
424 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
425 mapscrolldir = MS_LEFT;
426 } else {
427 mapscrolldir = MS_RIGHT;
428 next_screen();
430 scroll_step = 0;
431 } else {
432 next_screen();
433 mapscreen_yoff += 192;
436 handle_objs:;
437 handle_spawns(scroll_step);
438 scroll_gameobjs(scroll_step);
439 ret = 1;
440 } else if(mapscrolldir == MS_LEFT) {
441 mapscreen_xoff -= scroll_step;
442 if(mapscreen_xoff < 0) {
443 mapsquare.x--;
444 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
445 scroll_step = -mapscreen_xoff;
446 mapscreen_xoff = 0;
447 mapscreen_yoff = 0;
448 mapsquare.x++;
449 scroll_gameobjs(scroll_step);
450 mapscrolldir = MS_UP;
451 scroll_step = 0;
452 } else {
453 mapscreen_xoff += 192;
454 next_screen();
457 goto handle_objs;
458 } else if(mapscrolldir == MS_RIGHT) {
459 mapscreen_xoff += scroll_step;
460 if(mapscreen_xoff >= 192) {
461 mapsquare.x++;
462 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
463 scroll_step = mapscreen_xoff - 192;
464 mapscreen_xoff = 0;
465 mapscreen_yoff = 0;
466 scroll_gameobjs(scroll_step);
467 mapscrolldir = MS_UP;
468 scroll_step = 0;
469 } else {
470 next_screen();
471 mapscreen_xoff -= 192;
474 goto handle_objs;
477 return ret;
480 static int init_player(int player_no) {
481 assert(player_no == 0 || player_no == 1);
482 int pid = gameobj_alloc();
483 gameobj_init(pid, &VEC( SCREEN_MIN_X, SCREEN_MAX_Y - (25 * SCALE) ), &VEC( 0, 0 ),
484 SI_PLAYERS, player_no == 0 ? ANIM_P1_MOVE_N : ANIM_P2_MOVE_N, player_no == 0 ? OBJ_P1 : OBJ_P2);
485 if(pid == -1) return -1;
486 player_ids[player_no] = pid;
487 objs[pid].objspecific.playerdata.score = 0;
488 player_weapons[player_no][0] = WP_COLT45;
489 weapon_count[player_no] = 1;
490 weapon_active[player_no] = 0;
491 size_t i = 0;
492 for(; i < AMMO_MAX; i++)
493 player_ammo[player_no][i] = 50000;
494 add_player(pid);
495 return pid;
498 static vec2f *mousepos;
499 static int init_crosshair() {
500 int id = gameobj_alloc();
501 gameobj_init(id, &VEC(VMODE_W/2, VMODE_H/2), &VEC(0,0), SI_CROSSHAIR, ANIM_CROSSHAIR, OBJ_CROSSHAIR);
502 if(id == -1) return -1;
503 mousepos = &objs[id].pos;
504 return id;
507 static int init_blood(vec2f *pos) {
508 int id = gameobj_alloc();
509 gameobj_init(id, pos, &VEC(0,0), SI_MISC, ANIM_BLOOD, OBJ_BLOOD);
510 gameobj_init_bulletdata(id, 4);
511 return id;
514 static int init_bullet(vec2f *pos, vec2f *vel, int steps) {
515 int id = gameobj_alloc();
516 gameobj_init(id, pos, vel, SI_BULLET, ANIM_BULLET, OBJ_BULLET);
517 gameobj_init_bulletdata(id, steps);
518 return id;
521 static int init_grenade(vec2f *pos, vec2f *vel, int steps) {
522 int id = gameobj_alloc();
523 gameobj_init(id, pos, vel, SI_GRENADE, ANIM_GRENADE_SMALL, OBJ_GRENADE);
524 gameobj_init_bulletdata(id, steps);
525 return id;
528 static int init_grenade_explosion(vec2f *pos, int from_enemy) {
529 const int ticks_per_anim_frame = 4;
530 const int expl_anim_frames = 11;
531 vec2f grenade_center = get_sprite_center(spritemaps[SI_GRENADE_EXPLOSION]);
532 vec2f mypos = vecsub(pos, &grenade_center);
533 int id = gameobj_alloc();
534 if(id == -1) return -1;
535 gameobj_init(id, &mypos, &VEC(0,0), SI_GRENADE_EXPLOSION, ANIM_GRENADE_EXPLOSION, OBJ_GRENADE_EXPLOSION);
536 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
537 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
538 if(!from_enemy) add_explosion(id);
539 else add_enemy_explosion(id);
540 return id;
543 static int init_big_explosion(vec2f *pos) {
544 const int ticks_per_anim_frame = 8;
545 const int expl_anim_frames = 5;
546 vec2f rocket_center = get_sprite_center(spritemaps[SI_BIG_EXPLOSION]);
547 vec2f mypos = vecsub(pos, &rocket_center);
548 int id = gameobj_alloc();
549 if(id == -1) return -1;
550 gameobj_init(id, &mypos, &VEC(0,0), SI_BIG_EXPLOSION, ANIM_BIG_EXPLOSION, OBJ_BIG_EXPLOSION);
551 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
552 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
553 add_explosion(id);
554 return id;
557 static int init_rocket_explosion(vec2f *pos) {
558 vec2f ax = vecadd(pos, &VEC(-15*SCALE, 9*SCALE));
559 vec2f bx = vecadd(pos, &VEC(1*SCALE, -6*SCALE));
560 vec2f cx = vecadd(pos, &VEC(-8*SCALE, -8*SCALE));
561 vec2f dx = vecadd(pos, &VEC(8*SCALE, 8*SCALE));
562 int ret = 0;
563 ret += init_grenade_explosion(&ax, 0) != -1;
564 ret += init_grenade_explosion(&bx, 0) != -1;
565 ret += init_big_explosion(&cx) != -1;
566 ret += init_big_explosion(&dx) != -1;
567 return ret;
570 static int init_flame(vec2f *pos, vec2f *vel, int steps) {
571 int id = gameobj_alloc();
572 if(id == -1) return -1;
573 gameobj_init(id, pos, vel, SI_FLAME, ANIM_FLAME, OBJ_FLAME);
574 gameobj_init_bulletdata(id, steps);
575 return id;
578 static int init_player_flame(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
579 static const vec2f flame_origin[] = {
580 [DIR_O] = { 4.0, 8.0 },
581 [DIR_NO] = { 5.0, 11.0 },
582 [DIR_N] = { 7.5, 12.0 },
583 [DIR_NW] = { 10.0, 11.0 },
584 [DIR_W] = { 11.0, 8.0 },
585 [DIR_SW] = { 10.0, 5.0 },
586 [DIR_S] = { 7.5, 3.0 },
587 [DIR_SO] = { 4.0, 4.0 },
589 vec2f mypos = *pos;
590 mypos.x -= flame_origin[dir].x * SCALE;
591 mypos.y -= flame_origin[dir].y * SCALE;
592 return init_flame(&mypos, vel, steps);
595 static int init_rocket(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
596 static const vec2f rocket_origin[] = {
597 [DIR_N] = { 1.0, 10.0 },
598 [DIR_S] = { 1.0, 0.0 },
599 [DIR_O] = { 0.0, 1.0 },
600 [DIR_W] = { 10.0, 1.0 },
601 [DIR_NO] = { 0.0, 7.0 },
602 [DIR_SO] = { 0.0, 0.0 },
603 [DIR_SW] = { 7.0, 0.0 },
604 [DIR_NW] = { 7.0, 7.0 },
606 static const enum animation_id rocket_anim[] = {
607 [DIR_N] = ANIM_ROCKET_N,
608 [DIR_S] = ANIM_ROCKET_S,
609 [DIR_O] = ANIM_ROCKET_O,
610 [DIR_W] = ANIM_ROCKET_W,
611 [DIR_NO] = ANIM_ROCKET_NO,
612 [DIR_SO] = ANIM_ROCKET_SO,
613 [DIR_SW] = ANIM_ROCKET_SW,
614 [DIR_NW] = ANIM_ROCKET_NW,
616 vec2f mypos = *pos;
617 mypos.x -= rocket_origin[dir].x * SCALE;
618 mypos.y -= rocket_origin[dir].y * SCALE;
619 int id = gameobj_alloc();
620 if(id == -1) return -1;
621 gameobj_init(id, &mypos, vel, SI_ROCKET, rocket_anim[dir], OBJ_ROCKET);
622 gameobj_init_bulletdata(id, steps);
623 add_rocket(id);
624 return id;
628 static const struct enemy_route* get_enemy_current_route(int curr_step, const struct enemy_spawn *spawn) {
629 int i = ENEMY_MAX_ROUTE -1;
630 for(; i >= 0; i--)
631 if(spawn->route[i].shape != ES_INVALID &&
632 curr_step >= spawn->route[i].start_step)
633 return &spawn->route[i];
634 return 0;
637 static vec2f get_enemy_vel(int curr_step, const struct enemy_spawn *spawn) {
638 const struct enemy_route *route = get_enemy_current_route(curr_step, spawn);
639 return get_vel_from_direction16(route->dir, (float)route->vel/8.f);
642 static const enum animation_id enemy_animation_lut[] = {
643 [ES_SOLDIER1_DOWN] = ANIM_ENEMY_GUNNER_DOWN,
644 [ES_SOLDIER1_RIGHT] = ANIM_ENEMY_GUNNER_RIGHT,
645 [ES_SOLDIER1_LEFT] = ANIM_ENEMY_GUNNER_LEFT,
646 [ES_SOLDIER2_DOWN] = ANIM_ENEMY_BOMBER_DOWN,
647 [ES_SOLDIER2_RIGHT] = ANIM_ENEMY_BOMBER_RIGHT,
648 [ES_SOLDIER2_LEFT] = ANIM_ENEMY_BOMBER_LEFT,
649 [ES_JEEP] = ANIM_JEEP,
650 [ES_TANK_SMALL] = ANIM_TANK_SMALL,
651 [ES_TANK_BIG] = ANIM_TANK_BIG,
652 [ES_TRANSPORTER] = ANIM_TRANSPORTER,
653 [ES_GUNTURRET_MOVABLE_MACHINE] = ANIM_GUNTURRET_MOVABLE_MACHINE_S,
654 [ES_GUNTURRET_MOVABLE_MAN] = ANIM_GUNTURRET_MOVABLE_MAN_S,
655 [ES_MINE_FLAT] = ANIM_MINE_FLAT,
656 [ES_MINE_CROSS] = ANIM_MINE_CROSSED,
657 [ES_FLAMETURRET] = ANIM_FLAMETURRET,
658 [ES_GUNTURRET_FIXED_SOUTH] = ANIM_GUNTURRET_FIXED_SOUTH,
659 [ES_GUNTURRET_FIXED_NORTH] = ANIM_GUNTURRET_FIXED_NORTH,
660 [ES_BUNKER_1] = ANIM_BUNKER1,
661 [ES_BUNKER_2] = ANIM_BUNKER2,
662 [ES_BUNKER_3] = ANIM_BUNKER3,
663 [ES_BUNKER_4] = ANIM_BUNKER4,
664 [ES_BUNKER_5] = ANIM_BUNKER5,
667 static int init_enemy(const struct enemy_spawn *spawn) {
668 const enum objtype enemy_soldier_objtype_lut[] = {
669 [0] = OBJ_ENEMY_SHOOTER,
670 [1] = OBJ_ENEMY_BOMBER
672 const enum objtype enemy_objtype_lut[] = {
673 [ES_JEEP] = OBJ_JEEP,
674 [ES_TANK_SMALL] = OBJ_TANK_SMALL,
675 [ES_TANK_BIG] = OBJ_TANK_BIG,
676 [ES_TRANSPORTER] = OBJ_TRANSPORTER,
677 [ES_GUNTURRET_MOVABLE_MACHINE] = OBJ_GUNTURRET_MOVABLE_MACHINE,
678 [ES_GUNTURRET_MOVABLE_MAN] = OBJ_GUNTURRET_MOVABLE_MAN,
679 [ES_MINE_FLAT] = OBJ_MINE_FLAT,
680 [ES_MINE_CROSS] = OBJ_MINE_CROSSED,
681 [ES_FLAMETURRET] = OBJ_FLAMETURRET,
682 [ES_GUNTURRET_FIXED_SOUTH] = OBJ_GUNTURRET_FIXED_SOUTH,
683 [ES_GUNTURRET_FIXED_NORTH] = OBJ_GUNTURRET_FIXED_NORTH,
684 [ES_BUNKER_1] = OBJ_BUNKER1,
685 [ES_BUNKER_2] = OBJ_BUNKER2,
686 [ES_BUNKER_3] = OBJ_BUNKER3,
687 [ES_BUNKER_4] = OBJ_BUNKER4,
688 [ES_BUNKER_5] = OBJ_BUNKER5,
689 [ES_BOSS] = OBJ_BOSS,
691 const enum animation_id boss_animation_lut[] = {
692 [0] = ANIM_BOSS1,
693 [1] = ANIM_BOSS2,
694 [2] = ANIM_BOSS3,
695 [3] = ANIM_BOSS4,
696 [4] = ANIM_BOSS5,
697 [5] = ANIM_BOSS6,
698 [6] = ANIM_BOSS7,
699 [7] = ANIM_BOSS8,
700 [8] = ANIM_BOSS9,
701 [9] = ANIM_BOSS10,
702 [10] = ANIM_BOSS11,
703 [11] = ANIM_BOSS12,
705 const enum sprite_index enemy_soldier_sprite_lut[] = {
706 [ET_ASIAN] = SI_ENEMY_ASIAN,
707 [ET_WESTERN] = SI_ENEMY_WESTERN,
709 const enum sprite_index enemy_sprite_lut[] = {
710 [ES_JEEP] = SI_VEHICLES_SMALL,
711 [ES_TANK_SMALL] = SI_VEHICLES_MEDIUM,
712 [ES_TANK_BIG] = SI_VEHICLES_BIG,
713 [ES_TRANSPORTER] = SI_VEHICLES_BIG,
714 [ES_BUNKER_1] = SI_BUNKERS,
715 [ES_BUNKER_2] = SI_BUNKERS,
716 [ES_BUNKER_3] = SI_BUNKERS,
717 [ES_BUNKER_4] = SI_BUNKERS,
718 [ES_BUNKER_5] = SI_BUNKERS,
719 [ES_GUNTURRET_MOVABLE_MACHINE] = SI_GUNTURRET,
720 [ES_GUNTURRET_MOVABLE_MAN] = SI_GUNTURRET,
721 [ES_MINE_FLAT] = SI_MINES,
722 [ES_MINE_CROSS] = SI_MINES,
723 [ES_FLAMETURRET] = SI_MINES,
724 [ES_GUNTURRET_FIXED_SOUTH] = SI_MINES,
725 [ES_GUNTURRET_FIXED_NORTH] = SI_MINES,
726 [ES_BOSS] = SI_BOSSES,
729 vec2f spawnpos = VEC(SCREEN_MIN_X + spawn->x*SCALE, SCREEN_MIN_Y + spawn->y*SCALE);
730 int id = gameobj_alloc();
731 if(id == -1) return -1;
732 const struct enemy_route* route_curr = get_enemy_current_route(0, spawn);
733 vec2f vel = get_enemy_vel(0, spawn);
735 int is_soldier = route_curr->shape <= ES_SOLDIER2_RIGHT;
736 int is_boss = route_curr->shape == ES_BOSS;
737 enum sprite_index spriteid;
738 enum objtype objid;
739 enum animation_id animid;
740 if(is_soldier) {
741 spriteid = enemy_soldier_sprite_lut[map->enemy_type];
742 objid = enemy_soldier_objtype_lut[spawn->weapon];
743 } else {
744 spriteid = enemy_sprite_lut[route_curr->shape];
745 objid = enemy_objtype_lut[route_curr->shape];
747 if(is_boss) animid = boss_animation_lut[map->boss_id];
748 else animid = enemy_animation_lut[route_curr->shape];
750 gameobj_init(id, &spawnpos, &vel, spriteid, animid, objid);
751 objs[id].objspecific.enemy.curr_step = 0;
752 objs[id].objspecific.enemy.spawn = spawn;
753 switch(objid) {
754 case OBJ_BOSS:
755 add_boss(id);
756 break;
757 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
758 case OBJ_BUNKER4: case OBJ_BUNKER5:
759 add_bunker(id);
760 break;
761 case OBJ_FLAMETURRET: case OBJ_GUNTURRET_FIXED_NORTH:
762 case OBJ_GUNTURRET_FIXED_SOUTH:
763 add_turret(id);
764 break;
765 case OBJ_MINE_CROSSED: case OBJ_MINE_FLAT:
766 add_mine(id);
767 break;
768 case OBJ_JEEP: case OBJ_TRANSPORTER:
769 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
770 add_vehicle(id);
771 break;
772 default:
773 add_enemy(id);
775 return id;
778 static void remove_enemy(int id) {
779 enum objtype objid = objs[id].objtype;
780 switch(objid) {
781 case OBJ_JEEP: case OBJ_TRANSPORTER:
782 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
783 golist_remove(&go_vehicles, id);
784 break;
785 default:
786 golist_remove(&go_enemies, id);
788 gameobj_free(id);
791 static int enemy_fires(struct enemy *e) {
792 int i;
793 for(i = 0; i < ENEMY_MAX_SHOT; i++)
794 if(e->curr_step == e->spawn->shots[i]) return 1;
795 return 0;
798 static enum animation_id get_flash_animation_from_direction(enum direction dir) {
799 #define ANIMF(dir, anim) [dir] = anim
800 static const enum animation_id dir_to_anim[] = {
801 ANIMF(DIR_O, ANIM_FLASH_O),
802 ANIMF(DIR_NO, ANIM_FLASH_NO),
803 ANIMF(DIR_N, ANIM_FLASH_N),
804 ANIMF(DIR_NW, ANIM_FLASH_NW),
805 ANIMF(DIR_W, ANIM_FLASH_W),
806 ANIMF(DIR_SW, ANIM_FLASH_SW),
807 ANIMF(DIR_S, ANIM_FLASH_S),
808 ANIMF(DIR_SO, ANIM_FLASH_SO),
810 #undef ANIMF
811 return dir_to_anim[dir];
814 static int init_flash(vec2f *pos, enum direction dir) {
815 int id = gameobj_alloc();
816 gameobj_init(id, pos, &VEC(0, 0), SI_FLASH, get_flash_animation_from_direction(dir), OBJ_FLASH);
817 gameobj_init_bulletdata(id, 2);
818 return id;
821 static vec2f get_gameobj_pos(int obj_id) {
822 return objs[obj_id].pos;
825 static vec2f get_gameobj_center(int obj_id) {
826 vec2f res = objs[obj_id].pos;
827 vec2f add = get_sprite_center(spritemaps[objs[obj_id].spritemap_id]);
828 return vecadd(&res, &add);
831 static enum direction get_direction_from_vec(vec2f *vel);
832 static enum animation_id get_anim_from_direction(enum direction dir, int player, int throwing);
834 static enum weapon_id get_active_weapon_id(int player_no) {
835 return player_weapons[player_no][weapon_active[player_no]];
838 static const struct weapon* get_active_weapon(int player_no) {
839 return &weapons[get_active_weapon_id(player_no)];
841 static enum direction16 get_shotdirection_from_enemy(int curr_step, const struct enemy_spawn *spawn) {
842 const struct enemy_route* r = get_enemy_current_route(curr_step, spawn);
843 switch(r->shape) {
844 case ES_SOLDIER1_DOWN: case ES_SOLDIER2_DOWN:
845 return DIR16_S;
846 case ES_SOLDIER1_LEFT: case ES_SOLDIER2_LEFT:
847 return DIR16_W;
848 case ES_SOLDIER1_RIGHT: case ES_SOLDIER2_RIGHT:
849 return DIR16_O;
850 default:
851 assert(0);
855 static void enemy_fire_bullet(enum direction16 dir, int steps, enum enemy_weapon wpn, vec2f *pos) {
856 vec2f vel = get_vel_from_direction16(dir, 1.75);
857 int id;
858 if(wpn == EW_GUN) {
859 id = init_bullet(pos, &vel, steps);
860 if(id != -1) add_ebullet(id);
861 } else if (wpn == EW_GRENADE) {
862 id = init_grenade(pos, &vel, steps);
863 if(id != -1) add_enemy_grenade(id);
864 } else {
865 id = init_flame(pos, &vel, steps);
866 if(id != -1) add_enemy_flame(id);
870 static int get_crosshair_id(void) {
871 assert(sblist_getsize(&go_crosshair));
872 uint8_t *id = sblist_get(&go_crosshair, 0);
873 return *id;
876 static void fire_bullet(int player_no) {
877 int id;
878 const struct weapon *pw = get_active_weapon(player_no);
879 if(player_ammo[player_no][pw->ammo] == 0) return;
880 vec2f from = get_gameobj_center(player_ids[player_no]);
881 //get_anim_from_vel(0, objs[player].
882 vec2f to = get_gameobj_center(get_crosshair_id());
883 to.x += 4*SCALE - rand()%8*SCALE;
884 to.y += 4*SCALE - rand()%8*SCALE;
885 vec2f vel = velocity(&from, &to);
886 enum direction dir = get_direction_from_vec(&vel);
887 if(dir != DIR_INVALID) {
888 enum animation_id aid = get_anim_from_direction(dir, player_no, pw->ammo == AMMO_GRENADE);
889 if(aid != ANIM_INVALID) switch_anim(player_ids[player_no], aid);
890 anim_step curranim = objs[player_ids[player_no]].anim_curr;
891 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[player_ids[player_no]].animid, ANIM_STEP_INIT);
892 vec2f muzzle = muzzle_tab[curranim];
894 from = get_gameobj_pos(player_ids[player_no]);
895 from.x += muzzle.x * SCALE;
896 from.y += muzzle.y * SCALE;
898 if(pw->flags & WF_MUZZLEFLASH) {
899 static const vec2f flash_start[] = {
900 [DIR_O] = { 0.0, 1.0 },
901 [DIR_NO] = { 0.5, 6.0 },
902 [DIR_N] = { 1.0, 7.5 },
903 [DIR_NW] = { 6.0, 6.0 },
904 [DIR_W] = { 7.5, 1.0 },
905 [DIR_SW] = { 4.5, 0.0 },
906 [DIR_S] = { 1.0, 0.0 },
907 [DIR_SO] = { 0.0, 0.0 },
909 vec2f ffrom = from;
910 ffrom.x -= flash_start[dir].x * SCALE;
911 ffrom.y -= flash_start[dir].y * SCALE;
912 id = init_flash(&ffrom, dir);
913 if(id != -1) add_muzzleflash(id);
915 vel = velocity(&from, &to);
917 float dist = veclength(&vel);
918 float speed = pw->bullet_speed * SCALE;
919 const uint16_t range_tab[] = {0, 80, 66, 80, 118, 118, 118, 118, 118, 118,
920 200, 200, 240, 240, 240, 240, 240, 240, 240, 240, 320 };
921 float range = range_tab[pw->range] * SCALE;
922 if(dist > range)
923 dist = range;
924 float steps = dist / speed;
925 float deg = atan2(vel.y, vel.x);
926 vel.x = cos(deg) * speed;
927 vel.y = sin(deg) * speed;
928 switch(pw->shot) {
929 case ST_LAUNCHER:
930 id = init_rocket(dir, &from, &vel, steps);
931 break;
932 case ST_BULLET:
933 id = init_bullet(&from, &vel, steps);
934 if(id != -1) add_pbullet(id);
935 break;
936 case ST_FLAMES:
937 id = init_player_flame(dir, &from, &vel, steps);
938 if(id != -1) add_flame(id);
939 break;
940 case ST_GRENADE:
941 id = init_grenade(&from, &vel, steps);
942 add_grenade(id);
943 break;
944 default:
945 abort();
947 player_ammo[player_no][pw->ammo]--;
948 const WAVE_HEADER_COMPLETE *wf = wavesounds[pw->sound];
949 if(id != -1 && pw->sound != WS_NONE)
950 audio_play_wave_resource(wf);
953 static void init_game_objs() {
954 sblist_init(&go_crosshair, 1, 4);
955 sblist_init(&go_players, 1, 4);
956 sblist_init(&go_muzzleflash, 1, 4);
957 sblist_init(&go_player_bullets, 1, 32);
958 sblist_init(&go_flames, 1, 32);
959 sblist_init(&go_enemy_bullets, 1, 32);
960 sblist_init(&go_explosions, 1, 16);
961 sblist_init(&go_enemy_explosions, 1, 16);
962 sblist_init(&go_grenades, 1, 16);
963 sblist_init(&go_enemy_grenades, 1, 16);
964 sblist_init(&go_rockets, 1, 8);
965 sblist_init(&go_enemies, 1, 32);
966 sblist_init(&go_vehicles, 1, 4);
967 sblist_init(&go_mines, 1, 4);
968 sblist_init(&go_turrets, 1, 8);
969 sblist_init(&go_bunkers, 1, 4);
970 sblist_init(&go_boss, 1, 4);
971 sblist_init(&go_blood, 1, 16);
972 sblist_init(&go_enemy_flames, 1, 16);
973 init_player(0);
974 add_crosshair(init_crosshair());
975 init_map(current_map);
978 static int point_in_mask(vec2f *point, int obj_id) {
979 vec2f pos_in_pic = VEC((point->x - objs[obj_id].pos.x) / SCALE,
980 (point->y - objs[obj_id].pos.y) / SCALE);
981 const struct palpic *p = spritemaps[objs[obj_id].spritemap_id];
982 unsigned h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
983 if(pos_in_pic.x < 0 || pos_in_pic.y < 0 || pos_in_pic.x > w || pos_in_pic.y > h) return 0;
984 assert(objs[obj_id].anim_curr != ANIM_STEP_INIT);
985 anim_step curranim = objs[obj_id].anim_curr;
986 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[obj_id].animid, ANIM_STEP_INIT);
987 uint8_t *data = palpic_getspritedata(p, curranim);
988 if(data[(unsigned) pos_in_pic.y * w + (unsigned) pos_in_pic.x] != 0) return 1;
989 return 0;
992 static enum animation_id get_die_anim(unsigned id) {
993 switch(objs[id].objtype) {
994 case OBJ_P1:
995 return ANIM_P1_DIE;
996 case OBJ_P2:
997 return ANIM_P2_DIE;
998 case OBJ_JEEP:
999 return ANIM_JEEP_DESTROYED;
1000 case OBJ_TANK_SMALL:
1001 return ANIM_TANK_SMALL_DESTROYED;
1002 case OBJ_TANK_BIG:
1003 return ANIM_TANK_BIG_DESTROYED;
1004 case OBJ_TRANSPORTER:
1005 return ANIM_TRANSPORTER_DESTROYED;
1006 case OBJ_ENEMY_BOMBER:
1007 return ANIM_ENEMY_BOMBER_DIE;
1008 case OBJ_ENEMY_SHOOTER:
1009 return ANIM_ENEMY_GUNNER_DIE;
1010 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
1011 case OBJ_BUNKER4: case OBJ_BUNKER5:
1012 return ANIM_BUNKER_DESTROYED;
1013 case OBJ_GUNTURRET_MOVABLE_MACHINE:
1014 return ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED;
1015 case OBJ_GUNTURRET_MOVABLE_MAN:
1016 return ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED;
1017 default:
1018 return ANIM_INVALID;
1022 /* remove bullets that have reached their maximum number of steps */
1023 static int remove_bullets(sblist *list) {
1024 int res = 0;
1025 uint8_t *item_id;
1026 ssize_t li;
1027 sblist_iter_counter2s(list, li, item_id) {
1028 struct gameobj *bullet = &objs[*item_id];
1029 if(bullet->objspecific.bullet.step_curr >= bullet->objspecific.bullet.step_max) {
1030 gameobj_free(*item_id);
1031 sblist_delete(list, li);
1032 li--;
1033 } else {
1034 bullet->objspecific.bullet.step_curr++;
1036 res = 1;
1038 return res;
1041 static int remove_explosives(sblist *list) {
1042 int res = 0;
1043 uint8_t *item_id;
1044 ssize_t li;
1045 sblist_iter_counter2s(list, li, item_id) {
1046 struct gameobj *go = &objs[*item_id];
1047 if(go->objspecific.bullet.step_curr >= go->objspecific.bullet.step_max) {
1048 if(go->objtype == OBJ_GRENADE) init_grenade_explosion(&go->pos, list == &go_enemy_grenades);
1049 else init_rocket_explosion(&go->pos);
1050 gameobj_free(*item_id);
1051 sblist_delete(list, li);
1052 li--;
1053 } else {
1054 go->objspecific.bullet.step_curr++;
1055 if(go->objtype == OBJ_GRENADE) {
1056 if(go->objspecific.bullet.step_curr >= 32) go->animid = ANIM_GRENADE_SMALL;
1057 else if(go->objspecific.bullet.step_curr >= 8) go->animid = ANIM_GRENADE_BIG;
1060 res = 1;
1062 return res;
1065 static int remove_offscreen_objects(sblist *list) {
1066 int res = 0;
1067 uint8_t *item_id;
1068 ssize_t li;
1069 sblist_iter_counter2s(list, li, item_id) {
1070 assert(obj_slot_used[*item_id]);
1071 struct gameobj *go = &objs[*item_id];
1072 assert((int) go->spritemap_id < SI_MAX);
1073 const struct palpic *p = spritemaps[go->spritemap_id];
1074 assert(p->spritecount);
1075 int h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
1076 if(go->pos.x < SCREEN_MIN_X-w*SCALE || go->pos.x > SCREEN_MAX_X ||
1077 go->pos.y < SCREEN_MIN_Y-h*SCALE || go->pos.y > SCREEN_MAX_Y) {
1078 res = 1;
1079 dprintf(2, "offscreen: removed gameobj %d\n", (int) *item_id);
1080 gameobj_free(*item_id);
1081 sblist_delete(list, li);
1082 li--;
1085 return res;
1088 static int is_death_anim(enum animation_id anim) {
1089 switch(anim) {
1090 case ANIM_ENEMY_BOMBER_DIE:
1091 case ANIM_ENEMY_GUNNER_DIE:
1092 case ANIM_ENEMY_BURNT:
1093 case ANIM_P1_DIE:
1094 case ANIM_P2_DIE:
1095 case ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED:
1096 case ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED:
1097 case ANIM_BUNKER_DESTROYED:
1098 case ANIM_JEEP_DESTROYED:
1099 case ANIM_TANK_BIG_DESTROYED:
1100 case ANIM_TANK_SMALL_DESTROYED:
1101 case ANIM_TRANSPORTER_DESTROYED:
1102 return 1;
1103 default:
1104 return 0;
1108 // removes bullets and other objects if they collide. return 1 if anything happened
1109 static int hit_bullets(sblist *bullet_list, sblist *target_list) {
1110 uint8_t *bullet_id;
1111 ssize_t li;
1112 int res = 0;
1113 enum bulletsubtype {
1114 BS_BULLET = 0,
1115 BS_FLAME = 1,
1116 BS_GRENADE_EXPL = 2,
1117 BS_BIG_EXPL = 3,
1118 } bullet_subtybe = BS_BULLET;
1120 sblist_iter_counter2s(bullet_list, li, bullet_id) {
1121 struct gameobj *bullet = &objs[*bullet_id];
1122 if(bullet->objtype == OBJ_FLAME) bullet_subtybe = BS_FLAME;
1123 else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1124 /* grenade kills only in the explosion, not in the smoke phase */
1125 if(bullet->objspecific.bullet.step_curr > 22) continue;
1126 bullet_subtybe = BS_GRENADE_EXPL;
1127 } else if(bullet->objtype == OBJ_BIG_EXPLOSION) {
1128 bullet_subtybe = BS_BIG_EXPL;
1131 vec2f bullet_center = get_gameobj_center(*bullet_id);
1132 if(bullet_list == &go_player_bullets || bullet_list == &go_flames) {
1133 if(is_wall(&bullet_center) == WT_SOLID) goto remove_bullet;
1136 const float bullet_radius[] = { [BS_BULLET] = 1.f, [BS_FLAME] = 6.f,
1137 [BS_GRENADE_EXPL] = 16.f, [BS_BIG_EXPL] = 19.f };
1139 size_t lj;
1140 uint8_t *target_id;
1141 sblist_iter_counter2(target_list, lj, target_id) {
1142 struct gameobj *target = &objs[*target_id];
1143 if(is_death_anim(target->animid)) continue;
1144 vec2f temp = get_gameobj_center(*target_id);
1145 float dist1 = vecdist(&bullet_center, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1146 vec2f newpos = vecadd(&bullet_center, &bullet->vel);
1147 float dist2 = vecdist(&newpos, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1149 unsigned w = palpic_getspritewidth(spritemaps[target->spritemap_id]),
1150 h = palpic_getspriteheight(spritemaps[target->spritemap_id]);
1151 float longest_side_div2 = ((float) MAX(h, w) / 2) * SCALE;
1152 if(dist1 < 1.f*SCALE || dist2 < 1.f*SCALE) { dprintf(2, "hit1\n"); goto hit; }
1153 if(dist1 < longest_side_div2 || dist2 < longest_side_div2) {
1154 vec2f velquarter = VEC(bullet->vel.x * 0.25, bullet->vel.y * 0.25);
1155 vec2f point = bullet_center;
1156 size_t k;
1157 for(k = 0; k < 4; k++) {
1158 if(point_in_mask(&point, *target_id)) {
1159 hit:
1161 if(bullet_list == &go_player_bullets) {
1162 if(target_list == &go_vehicles) {
1163 audio_play_wave_resource(wavesounds[WS_DROPSHOT]);
1164 goto remove_bullet;
1166 objs[player_ids[0]].objspecific.playerdata.score += 50;
1167 } else if (bullet_list == &go_rockets) {
1168 init_rocket_explosion(&target->pos);
1169 goto remove_bullet;
1170 } else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1171 // grenade explosion has no effect on vehicles and bunkers.
1172 if(target_list == &go_vehicles || target_list == &go_bunkers)
1173 goto next_bullet;
1175 enum animation_id death_anim = (bullet_subtybe == BS_FLAME && target_list == &go_enemies) ?
1176 ANIM_ENEMY_BURNT : get_die_anim(*target_id);
1177 if(death_anim == ANIM_INVALID) {
1178 gameobj_free(*target_id);
1179 sblist_delete(target_list, lj);
1180 lj--;
1181 goto next_target;
1183 switch_anim(*target_id, death_anim);
1184 target->vel = VEC(0,0);
1185 if(target->objtype == OBJ_ENEMY_BOMBER || target->objtype == OBJ_ENEMY_SHOOTER) {
1186 const enum wavesound_id wid[] = { WS_SCREAM, WS_SCREAM2 };
1187 audio_play_wave_resource(wavesounds[wid[rand()%2]]);
1188 if(bullet_subtybe == BS_BULLET) {
1189 vec2f bloodpos = vecadd(&target->pos, &VEC(0, (2+rand()%7)*SCALE));
1190 /* original displays at 0,8 but i prefer a random effect */
1191 //vec2f bloodpos = vecadd(&target->pos, &VEC(0, 8*SCALE));
1192 int id = init_blood(&bloodpos);
1193 if(id != -1) add_blood(id);
1196 if(bullet_subtybe == BS_BULLET) {
1197 remove_bullet:
1198 gameobj_free(*bullet_id);
1199 sblist_delete(bullet_list, li);
1200 li--;
1201 goto next_bullet;
1202 } else break;
1204 point = vecadd(&point, &velquarter);
1207 next_target:;
1209 next_bullet:
1210 res = 1;
1212 return res;
1215 uint32_t tickcounter;
1217 static int advance_animations(void) {
1218 size_t i, obj_visited;
1219 int res = 0;
1220 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1221 if(!obj_slot_used[i]) continue;
1222 struct gameobj *go = &objs[i];
1223 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)) {
1224 unsigned anim_delay = go->objtype == OBJ_BIG_EXPLOSION ? 8 : 4;
1225 if(go->anim_curr == ANIM_STEP_INIT || tickcounter % anim_delay == go->anim_frame) {
1226 anim_step last_anim = go->anim_curr;
1227 go->anim_curr = get_next_anim_frame(go->animid, go->anim_curr);
1228 if(last_anim != go->anim_curr) res = 1;
1231 obj_visited++;
1233 return res;
1236 static void switch_enemy_shape(int objid, const struct enemy_route* r) {
1237 switch_anim(objid, enemy_animation_lut[r->shape]);
1240 static void draw_golist(sblist *list) {
1241 size_t i;
1242 uint8_t *itemid;
1243 sblist_iter_counter2(list, i, itemid) {
1244 assert(obj_slot_used[*itemid]);
1245 struct gameobj *o = &objs[*itemid];
1246 const prgb *palette;
1247 switch(o->objtype) {
1248 case OBJ_BLOOD:
1249 // original blood color is bb5511 but that is hardly visible
1250 // palette = (prgb[]) {PRGB(0,0,0), PRGB(0xbb, 0x55, 0x11)};
1251 palette = (const prgb[]) {PRGB(0,0,0), PRGB(0xff, 0x0, 0x0)};
1252 break;
1253 case OBJ_ENEMY_BOMBER: case OBJ_ENEMY_SHOOTER:
1254 palette = map->enemy_palette;
1255 break;
1256 default:
1257 palette = 0;
1259 blit_sprite(o->pos.x, o->pos.y, &video,
1260 SCALE, spritemaps[o->spritemap_id],
1261 o->anim_curr == ANIM_STEP_INIT ? get_next_anim_frame(o->animid, o->anim_curr) : o->anim_curr,
1262 palette);
1266 static void draw_gameobjs(void) {
1267 draw_golist(&go_mines);
1268 draw_golist(&go_turrets);
1269 draw_golist(&go_bunkers);
1270 draw_golist(&go_vehicles);
1271 draw_golist(&go_enemies);
1272 draw_golist(&go_blood);
1273 draw_golist(&go_enemy_bullets);
1274 draw_golist(&go_boss);
1275 draw_golist(&go_rockets);
1276 draw_golist(&go_players);
1277 draw_golist(&go_player_bullets);
1278 draw_golist(&go_enemy_explosions);
1279 draw_golist(&go_flames);
1280 draw_golist(&go_enemy_flames);
1281 draw_golist(&go_explosions);
1282 draw_golist(&go_grenades);
1283 draw_golist(&go_enemy_grenades);
1284 draw_golist(&go_crosshair);
1285 draw_golist(&go_muzzleflash);
1288 static void process_soldiers(void) {
1289 uint8_t *itemid;
1290 sblist_iter(&go_enemies, itemid) {
1291 struct gameobj *go = &objs[*itemid];
1292 assert(go->objtype == OBJ_ENEMY_SHOOTER || go->objtype == OBJ_ENEMY_BOMBER);
1293 if (tickcounter % 4 == go->anim_frame) {
1294 const struct enemy_route *rc = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1295 if(rc->vel) {
1296 go->objspecific.enemy.curr_step++;
1297 if(enemy_fires(&go->objspecific.enemy)) {
1298 vec2f from = get_gameobj_center(*itemid);
1299 enum direction16 dir = get_shotdirection_from_enemy(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1300 enemy_fire_bullet(dir, 41, go->objspecific.enemy.spawn->weapon, &from);
1302 const struct enemy_route *rn = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1303 if(rn->shape != rc->shape) switch_enemy_shape(*itemid, rn);
1306 if(!is_death_anim(go->animid)) go->vel = get_enemy_vel(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1307 else go->vel = VEC(0, 0);
1311 static int process_turrets(sblist* list) {
1312 int res = 0;
1313 uint8_t *itemid;
1314 sblist_iter(list, itemid) {
1315 struct gameobj *go = &objs[*itemid];
1316 enum direction16 dir = DIR16_S;
1317 enum enemy_weapon ew = EW_GUN;
1318 int steps = 92;
1319 vec2f from;
1320 switch(go->objtype) {
1321 case OBJ_GUNTURRET_FIXED_NORTH:
1322 dir = DIR16_N;
1323 case OBJ_GUNTURRET_FIXED_SOUTH:
1324 from = get_gameobj_center(*itemid);
1325 if(rand()%8 == 0) {
1326 shot:
1327 enemy_fire_bullet(dir, steps, ew, &from);
1328 res = 1;
1330 break;
1331 case OBJ_FLAMETURRET:
1332 ew = EW_FLAME;
1333 steps = 48;
1335 if(objs[player_ids[0]].pos.y <= (192-25)*SCALE/2) dir = DIR16_N;
1336 from = go->pos;
1337 from.y += (dir == DIR16_N ? -8*SCALE : 8*SCALE);
1339 /* abusing curr_step variable to save position in a burst of flames */
1340 if(go->objspecific.enemy.curr_step && go->objspecific.enemy.curr_step < 16)
1341 goto do_flame;
1342 else if(go->objspecific.enemy.curr_step)
1343 go->objspecific.enemy.curr_step = 0;
1345 if(objs[player_ids[0]].pos.x < go->pos.x-32*SCALE || objs[player_ids[0]].pos.x > go->pos.x + (16+32)*SCALE)
1346 break;
1347 if(rand()%8 == 0) {
1348 do_flame:
1349 go->objspecific.enemy.curr_step++;
1350 goto shot;
1352 break;
1353 case OBJ_BUNKER1:
1354 case OBJ_BUNKER2:
1355 case OBJ_BUNKER3:
1356 case OBJ_BUNKER4:
1357 case OBJ_BUNKER5: {
1358 const vec2f *bunkerpos;
1359 const enum direction16 *bunkerdir;
1360 unsigned count, sec;
1361 static const enum direction16 bunker5_dir[] = {
1362 [0] = DIR16_N, [1] = DIR16_NO, [2] = DIR16_O, [3] = DIR16_SO,
1363 [4] = DIR16_S, [5] = DIR16_SW, [6] = DIR16_W, [7] = DIR16_NW,
1365 static const enum direction16 bunker1_dir[] = {
1366 [0] = DIR16_NNW, [1] = DIR16_NNO, [2] = DIR16_NO, [3] = DIR16_O,
1367 [4] = DIR16_SO, [5] = DIR16_SSO, [6] = DIR16_SSW, [7] = DIR16_SW,
1368 [8] = DIR16_W, [9] = DIR16_NW,
1370 static const vec2f bunker5_pos[] = {
1371 [0] = VEC(9,-8), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(22,10),
1372 [4] = VEC(9,16), [5] = VEC(-5,13), [6] = VEC(-7,2), [7] = VEC(-5,-6),
1374 static const vec2f bunker2_pos[] = {
1375 [0] = VEC(15,-2), [1] = VEC(27,3), [2] = VEC(31,12), [3] = VEC(28,23),
1376 [4] = VEC(15,27), [5] = VEC(2,23), [6] = VEC(0,12), [7] = VEC(4,2),
1378 static const vec2f bunker4_pos[] = {
1379 [0] = VEC(13,0), [1] = VEC(25,1), [2] = VEC(29,10), [3] = VEC(26,21),
1380 [4] = VEC(13,25), [5] = VEC(0,21), [6] = VEC(-2,10), [7] = VEC(2,0),
1382 static const vec2f bunker1_pos[] = {
1383 [0] = VEC(6,-6), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(26,12),
1384 [4] = VEC(26,16), [5] = VEC(20,24), [6] = VEC(6,24), [7] = VEC(0,20),
1385 [8] = VEC(0,12), [9] = VEC(0,2),
1387 unsigned b, dist = 28;
1388 switch(go->objtype) {
1389 case OBJ_BUNKER1:
1390 ew = EW_GRENADE;
1391 bunkerpos = bunker1_pos;
1392 bunkerdir = bunker1_dir;
1393 b = 0;
1394 count = 10;
1395 sec = 2;
1396 break;
1397 case OBJ_BUNKER2:
1398 if(tickcounter % 8) break;
1399 ew = EW_GUN;
1400 dist = 128;
1401 bunkerpos = bunker2_pos;
1402 goto bunker_circle_shoot;
1403 case OBJ_BUNKER4:
1404 if(tickcounter % /*(fps*3.5)/8*/ 24) break;
1405 ew = EW_GRENADE;
1406 dist = 32;
1407 bunkerpos = bunker4_pos;
1408 bunker_circle_shoot:;
1409 bunkerdir = bunker5_dir;
1410 b = go->objspecific.enemy.curr_step;
1411 count = b+1;
1412 if(++go->objspecific.enemy.curr_step >= 8) go->objspecific.enemy.curr_step = 0;
1413 goto bunkerloop;
1414 case OBJ_BUNKER3:
1415 ew = EW_GUN;
1416 bunkerpos = bunker2_pos;
1417 bunkerdir = bunker5_dir;
1418 b = 0;
1419 count = 8;
1420 dist = 128;
1421 sec = 1;
1422 break;
1423 case OBJ_BUNKER5:
1424 ew = EW_FLAME;
1425 bunkerpos = bunker5_pos;
1426 bunkerdir = bunker5_dir;
1427 b = 0;
1428 count = 8;
1429 sec = 3;
1430 break;
1431 default:;
1433 if(tickcounter > (uint32_t) go->objspecific.enemy.curr_step + fps*sec) {
1434 go->objspecific.enemy.curr_step = tickcounter;
1435 bunkerloop:;
1436 for(; b < count; b++) {
1437 from = go->pos;
1438 from.x += bunkerpos[b].x*SCALE;
1439 from.y += bunkerpos[b].y*SCALE;
1440 enemy_fire_bullet(bunkerdir[b], dist, ew, &from);
1442 res = 1;
1444 break;
1446 default:;
1449 return res;
1452 static int move_gameobjs(void) {
1453 int res = 0;
1454 size_t i, obj_visited;
1455 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1456 if(obj_slot_used[i]) {
1457 struct gameobj *go = &objs[i];
1458 obj_visited++;
1459 if(go->anim_curr == ANIM_STEP_INIT) res = 1;
1460 if(go->vel.x != 0 || go->vel.y != 0) {
1461 vec2f oldpos = go->pos;
1462 go->pos.x += go->vel.x;
1463 go->pos.y += go->vel.y;
1465 if(go->objtype == OBJ_P1 || go->objtype == OBJ_P2) {
1466 if(go->pos.y < SCREEN_MIN_Y) go->pos.y = SCREEN_MIN_Y;
1467 else if(go->pos.y+25*SCALE > SCREEN_MAX_Y) go->pos.y = SCREEN_MAX_Y-25*SCALE;
1468 if(go->pos.x < SCREEN_MIN_X) go->pos.x = SCREEN_MIN_X;
1469 else if(go->pos.x+32*SCALE > SCREEN_MAX_X) go->pos.x = SCREEN_MAX_X-32*SCALE;
1470 vec2f center = get_sprite_center(spritemaps[go->spritemap_id]);
1471 center = vecadd(&center, &go->pos);
1472 if(is_wall(&center)) {
1473 go->pos = oldpos;
1474 go->vel = VEC(0,0);
1478 res = 1;
1480 if((go->objtype == OBJ_ENEMY_BOMBER || go->objtype == OBJ_ENEMY_SHOOTER) &&
1481 go->anim_curr == animations[go->animid].last &&
1482 (go->animid == ANIM_ENEMY_BOMBER_DIE ||
1483 go->animid == ANIM_ENEMY_GUNNER_DIE ||
1484 go->animid == ANIM_ENEMY_BURNT)) {
1485 dprintf(2, "removed enemy from %.2f,%.2f\n", go->pos.x, go->pos.y);
1486 gameobj_free(i);
1487 golist_remove(&go_enemies, i);
1488 res = 1;
1489 continue;
1494 return res;
1497 static void game_update_caption(void) {
1498 char buf [128];
1499 snprintf(buf, 128, "objs: %d, map x,y %d/%d, index %d, xoff %d, yoff %d, spawnscreen %d, line %d", (int) obj_count,
1500 (int)mapsquare.x, (int)mapsquare.y, (int)map->screen_map[mapsquare.y][mapsquare.x],
1501 (int)mapscreen_xoff, (int)mapscreen_yoff, (int)map_spawn_screen_index, (int) map_spawn_line);
1502 SDL_WM_SetCaption(buf, 0);
1504 static void(*update_caption)(void) = game_update_caption;
1506 static void game_tick(int force_redraw) {
1507 int need_redraw = force_redraw;
1508 if(mousebutton_down[MB_LEFT] > 1) {
1509 const int player_no = 0;
1510 const struct weapon *pw = get_active_weapon(player_no);
1511 //if(get_active_weapon_id(player_no) == WP_M134) __asm__("int3");
1512 if (pw->flags & WF_AUTOMATIC) {
1513 float shots_per_second = pw->rpm / 60.f;
1514 float shotinterval = fps / shots_per_second;
1515 if((int)((float)mousebutton_down[MB_LEFT] / shotinterval) != (int)((float)(mousebutton_down[MB_LEFT]-1) / shotinterval))
1516 fire_bullet(player_no);
1520 if(advance_animations()) need_redraw = 1;
1521 if(hit_bullets(&go_player_bullets, &go_enemies)) need_redraw = 1;
1522 if(hit_bullets(&go_player_bullets, &go_vehicles)) need_redraw = 1;
1523 if(hit_bullets(&go_flames, &go_enemies)) need_redraw = 1;
1524 if(hit_bullets(&go_enemy_flames, &go_enemies)) need_redraw = 1;
1525 if(hit_bullets(&go_rockets, &go_enemies)) need_redraw = 1;
1526 if(hit_bullets(&go_rockets, &go_vehicles)) need_redraw = 1;
1527 if(hit_bullets(&go_explosions, &go_enemies)) need_redraw = 1;
1528 if(hit_bullets(&go_explosions, &go_vehicles)) need_redraw = 1;
1529 if(hit_bullets(&go_explosions, &go_mines)) need_redraw = 1;
1530 if(hit_bullets(&go_explosions, &go_turrets)) need_redraw = 1;
1531 if(hit_bullets(&go_explosions, &go_bunkers)) need_redraw = 1;
1532 if(hit_bullets(&go_explosions, &go_players)) need_redraw = 1;
1533 if(hit_bullets(&go_enemy_explosions, &go_players)) need_redraw = 1;
1534 if(hit_bullets(&go_enemy_bullets, &go_players)) need_redraw = 1;
1535 if(hit_bullets(&go_enemy_flames, &go_players)) need_redraw = 1;
1536 if(remove_bullets(&go_player_bullets)) need_redraw = 1;
1537 if(remove_bullets(&go_flames)) need_redraw = 1;
1538 if(remove_bullets(&go_explosions)) need_redraw = 1;
1539 if(remove_bullets(&go_enemy_explosions)) need_redraw = 1;
1540 if(remove_bullets(&go_enemy_bullets)) need_redraw = 1;
1541 if(remove_bullets(&go_enemy_flames)) need_redraw = 1;
1542 if(remove_bullets(&go_muzzleflash)) need_redraw = 1;
1543 if(remove_bullets(&go_blood)) need_redraw = 1;
1544 if(remove_explosives(&go_grenades)) need_redraw = 1;
1545 if(remove_explosives(&go_enemy_grenades)) need_redraw = 1;
1546 if(remove_explosives(&go_rockets)) need_redraw = 1;
1547 if(tickcounter % 2 == 0 && scroll_map()) need_redraw = 1;
1549 process_soldiers();
1550 if(tickcounter % 4 == 0 && process_turrets(&go_turrets)) need_redraw = 1;
1551 if(tickcounter % 4 == 0 && process_turrets(&go_bunkers)) need_redraw = 1;
1553 if(move_gameobjs()) need_redraw = 1;
1555 if(remove_offscreen_objects(&go_enemies)) need_redraw = 1;
1556 if(remove_offscreen_objects(&go_vehicles)) need_redraw = 1;
1557 if(remove_offscreen_objects(&go_mines)) need_redraw = 1;
1558 if(remove_offscreen_objects(&go_turrets)) need_redraw = 1;
1559 if(remove_offscreen_objects(&go_bunkers)) need_redraw = 1;
1561 long ms_used = 0;
1562 struct timeval timer;
1563 gettimestamp(&timer);
1564 if(need_redraw) {
1565 draw_map();
1566 draw_gameobjs();
1567 draw_status_bar();
1568 video_update_region(SCREEN_MIN_X ,SCREEN_MIN_Y , SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1571 ms_used = mspassed(&timer);
1572 //if(ms_used) printf("repaint took: ms_used %ld\n", ms_used);
1573 int res = audio_process();
1574 if(res == -1) music_restart();
1575 ms_used = mspassed(&timer);
1576 //if(ms_used) printf("audio processed: %d, ms_used %ld\n", res, ms_used);
1578 long sleepms = 1000/fps - ms_used;
1579 if(sleepms >= 0) SDL_Delay(sleepms);
1580 if(mousebutton_down[MB_LEFT]) mousebutton_down[MB_LEFT]++;
1582 tickcounter++;
1583 update_caption();
1586 enum cursor {
1587 c_down = 0,
1588 c_up,
1589 c_left,
1590 c_right,
1593 static enum cursor cursor_lut[] = {
1594 [SDLK_UP] = c_up,
1595 [SDLK_DOWN] = c_down,
1596 [SDLK_LEFT] = c_left,
1597 [SDLK_RIGHT] = c_right,
1598 [SDLK_w] = c_up,
1599 [SDLK_a] = c_left,
1600 [SDLK_s] = c_down,
1601 [SDLK_d] = c_right,
1604 static char cursors_pressed[] = {
1605 [c_up] = 0,
1606 [c_down] = 0,
1607 [c_left] = 0,
1608 [c_right] = 0,
1611 static vec2f get_vel_from_direction(enum direction dir, float speed) {
1612 #define VELLUT(a, b, c) [a] = VEC(b, c)
1613 static const vec2f vel_lut[] = {
1614 VELLUT(DIR_O, 1, 0),
1615 VELLUT(DIR_NO, 0.7071067769704655, -0.7071067769704655),
1616 VELLUT(DIR_N, 0, -1),
1617 VELLUT(DIR_NW, -0.7071067769704655, -0.7071067769704655),
1618 VELLUT(DIR_W, -1, 0),
1619 VELLUT(DIR_SW, -0.7071067769704655, 0.7071067769704655),
1620 VELLUT(DIR_S, 0, 1),
1621 VELLUT(DIR_SO, 0.7071067769704655, 0.7071067769704655),
1623 #undef VELLUT
1624 vec2f v = vel_lut[dir];
1625 v.x *= speed * SCALE;
1626 v.y *= speed * SCALE;
1627 return v;
1630 static vec2f get_vel_from_direction16(enum direction16 dir, float speed) {
1631 #define VELLUT(a, b, c) [a] = VEC(b, c)
1632 #define ANK90 0.7071067769704655
1633 #define GK90 ANK90
1634 #define ANK45 0.9238795042037964
1635 #define GK45 0.3826834261417389
1636 static const vec2f vel_lut[] = {
1637 VELLUT(DIR16_O, 1, 0),
1638 VELLUT(DIR16_ONO, ANK45, -GK45),
1639 VELLUT(DIR16_NO, ANK90, -ANK90),
1640 VELLUT(DIR16_NNO, GK45, -ANK45),
1641 VELLUT(DIR16_N, 0, -1),
1642 VELLUT(DIR16_NNW, -GK45, -ANK45),
1643 VELLUT(DIR16_NW, -ANK90, -ANK90),
1644 VELLUT(DIR16_WNW, -ANK45, -GK45),
1645 VELLUT(DIR16_W, -1, 0),
1646 VELLUT(DIR16_WSW, -ANK45, GK45),
1647 VELLUT(DIR16_SW, -ANK90, ANK90),
1648 VELLUT(DIR16_SSW, -GK45, ANK45),
1649 VELLUT(DIR16_S, 0, 1),
1650 VELLUT(DIR16_SSO, GK45, ANK45),
1651 VELLUT(DIR16_SO, ANK90, ANK90),
1652 VELLUT(DIR16_OSO, ANK45, GK45),
1654 #undef VELLUT
1655 vec2f v = vel_lut[dir];
1656 v.x *= speed * SCALE;
1657 v.y *= speed * SCALE;
1658 return v;
1662 static enum direction get_direction_from_vec(vec2f *vel) {
1663 float deg_org, deg = atan2(vel->y, vel->x);
1664 deg_org = deg;
1665 if(deg < 0) deg *= -1.f;
1666 else if(deg > 0) deg = M_PI + (M_PI - deg); // normalize atan2 result to scale from 0 to 2 pi
1667 int hexadrant = (int)(deg / ((1.0/16.0f)*2*M_PI));
1668 assert(hexadrant >= 0 && hexadrant < 16);
1669 static const enum direction rad_lut[] = {
1670 DIR_O,
1671 DIR_NO, DIR_NO,
1672 DIR_N, DIR_N,
1673 DIR_NW, DIR_NW,
1674 DIR_W, DIR_W,
1675 DIR_SW, DIR_SW,
1676 DIR_S, DIR_S,
1677 DIR_SO, DIR_SO,
1678 DIR_O
1680 return rad_lut[hexadrant];
1683 static enum direction get_direction_from_cursor(void) {
1684 enum direction dir = DIR_INVALID;
1685 if(cursors_pressed[c_up]) {
1686 if(cursors_pressed[c_left]) dir = DIR_NW;
1687 else if(cursors_pressed[c_right]) dir = DIR_NO;
1688 else dir = DIR_N;
1689 } else if (cursors_pressed[c_down]) {
1690 if(cursors_pressed[c_left]) dir = DIR_SW;
1691 else if(cursors_pressed[c_right]) dir = DIR_SO;
1692 else dir = DIR_S;
1693 } else if (cursors_pressed[c_left]) {
1694 dir = DIR_W;
1695 } else if (cursors_pressed[c_right]) {
1696 dir = DIR_O;
1698 return dir;
1701 static enum animation_id get_anim_from_direction(enum direction dir, int player_no, int throwing) {
1702 #define DIRMAP(a, b) [a] = b
1703 if(!throwing) {
1704 static const enum animation_id dir_map_p1[] = {
1705 DIRMAP(DIR_N, ANIM_P1_MOVE_N),
1706 DIRMAP(DIR_NW, ANIM_P1_MOVE_NW),
1707 DIRMAP(DIR_W, ANIM_P1_MOVE_W),
1708 DIRMAP(DIR_SW, ANIM_P1_MOVE_SW),
1709 DIRMAP(DIR_S, ANIM_P1_MOVE_S),
1710 DIRMAP(DIR_SO, ANIM_P1_MOVE_SO),
1711 DIRMAP(DIR_O, ANIM_P1_MOVE_O),
1712 DIRMAP(DIR_NO, ANIM_P1_MOVE_NO),
1714 static const enum animation_id dir_map_p2[] = {
1715 DIRMAP(DIR_N, ANIM_P2_MOVE_N),
1716 DIRMAP(DIR_NW, ANIM_P2_MOVE_NW),
1717 DIRMAP(DIR_W, ANIM_P2_MOVE_W),
1718 DIRMAP(DIR_SW, ANIM_P2_MOVE_SW),
1719 DIRMAP(DIR_S, ANIM_P2_MOVE_S),
1720 DIRMAP(DIR_SO, ANIM_P2_MOVE_SO),
1721 DIRMAP(DIR_O, ANIM_P2_MOVE_O),
1722 DIRMAP(DIR_NO, ANIM_P2_MOVE_NO),
1724 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1 : dir_map_p2;
1725 return dir_map[dir];
1726 } else {
1727 static const enum animation_id dir_map_p1_g[] = {
1728 DIRMAP(DIR_N, ANIM_P1_THROW_N),
1729 DIRMAP(DIR_NW, ANIM_P1_THROW_NW),
1730 DIRMAP(DIR_W, ANIM_P1_THROW_W),
1731 DIRMAP(DIR_SW, ANIM_P1_THROW_SW),
1732 DIRMAP(DIR_S, ANIM_P1_THROW_S),
1733 DIRMAP(DIR_SO, ANIM_P1_THROW_SO),
1734 DIRMAP(DIR_O, ANIM_P1_THROW_O),
1735 DIRMAP(DIR_NO, ANIM_P1_THROW_NO),
1737 static const enum animation_id dir_map_p2_g[] = {
1738 DIRMAP(DIR_N, ANIM_P2_THROW_N),
1739 DIRMAP(DIR_NW, ANIM_P2_THROW_NW),
1740 DIRMAP(DIR_W, ANIM_P2_THROW_W),
1741 DIRMAP(DIR_SW, ANIM_P2_THROW_SW),
1742 DIRMAP(DIR_S, ANIM_P2_THROW_S),
1743 DIRMAP(DIR_SO, ANIM_P2_THROW_SO),
1744 DIRMAP(DIR_O, ANIM_P2_THROW_O),
1745 DIRMAP(DIR_NO, ANIM_P2_THROW_NO),
1747 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1_g : dir_map_p2_g;
1748 return dir_map[dir];
1750 #undef DIRMAP
1753 #if 0
1754 static enum animation_id get_anim_from_cursor(void) {
1755 enum direction dir = get_direction_from_cursor();
1756 if(dir == DIR_INVALID) return ANIM_INVALID;
1757 return get_anim_from_direction(dir, 0, 0);
1759 /* playerno is either 0 or 1, not player_id! */
1760 static enum animation_id get_anim_from_vel(int player_no, vec2f *vel) {
1761 enum direction dir = get_direction_from_vec(vel);
1762 if(dir == DIR_INVALID) return ANIM_INVALID;
1763 return get_anim_from_direction(dir, player_no, 0);
1765 #endif
1767 static void switch_anim(int obj_id, int aid) {
1768 if(objs[obj_id].animid == aid) return;
1769 gameobj_start_anim(obj_id, aid);
1772 enum map_index choose_mission(void);
1773 //RcB: DEP "mission_select.c"
1774 #include "enemytag.c"
1775 int main() {
1776 video_init();
1777 clear_screen();
1778 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
1779 SDL_EnableKeyRepeat(100, 20);
1781 audio_init();
1783 /* background music for mission selection screen */
1784 music_play(TUNE_MAP);
1786 if((current_map = choose_mission()) == MI_INVALID) goto dun_goofed;
1788 music_play(TUNE_FIGHTING);
1790 SDL_ShowCursor(0);
1793 int startx = 10;
1794 int starty = 10;
1796 //redraw(surface, startx, starty);
1797 const float player_speed = 1.25f;
1798 const struct palpic* spritemap = spritemaps[SI_PLAYERS];
1799 struct { int *target; int dir; int max;} moves[] = {
1800 [c_up] = {&starty, SCALE * -1, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
1801 [c_down] = {&starty, SCALE, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
1802 [c_left] = {&startx, SCALE * -1, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
1803 [c_right] = {&startx, SCALE, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
1806 init_game_objs();
1807 int player_no = 0;
1808 int player_id = player_ids[player_no];
1810 game_tick(1);
1812 SDL_Event sdl_event;
1813 while(1) {
1814 unsigned need_redraw = 0;
1815 int weapon_inc = 0;
1816 while (SDL_PollEvent(&sdl_event)) {
1817 need_redraw = 0;
1818 switch (sdl_event.type) {
1819 case SDL_MOUSEMOTION:
1820 if((int)mousepos->x != sdl_event.motion.x || (int)mousepos->y != sdl_event.motion.y)
1821 need_redraw = 1;
1822 mousepos->x = sdl_event.motion.x;
1823 mousepos->y = sdl_event.motion.y;
1824 break;
1825 case SDL_MOUSEBUTTONDOWN:
1826 mousepos->x = sdl_event.button.x;
1827 mousepos->y = sdl_event.button.y;
1828 mousebutton_down[MB_LEFT] = 1;
1829 fire_bullet(player_no);
1830 break;
1831 case SDL_MOUSEBUTTONUP:
1832 mousepos->x = sdl_event.button.x;
1833 mousepos->y = sdl_event.button.y;
1834 mousebutton_down[MB_LEFT] = 0;
1835 break;
1836 case SDL_QUIT:
1837 dun_goofed:
1838 video_cleanup();
1839 return 0;
1840 case SDL_KEYDOWN:
1841 switch(sdl_event.key.keysym.sym) {
1842 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
1843 case SDLK_UP:
1844 case SDLK_DOWN:
1845 case SDLK_RIGHT:
1846 case SDLK_LEFT:
1847 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 1;
1848 check_anim: {
1849 enum direction dir = get_direction_from_cursor();
1850 if(dir != DIR_INVALID) {
1851 if(!mousebutton_down[MB_LEFT]) {
1852 // change animation according to cursors,
1853 // unless we're in automatic fire mode.
1854 enum animation_id aid = get_anim_from_direction(dir, player_no, 0);
1855 if(aid != ANIM_INVALID) switch_anim(player_id, aid);
1857 objs[player_id].vel = get_vel_from_direction(dir, player_speed);
1858 } else {
1859 objs[player_id].vel = VEC(0,0);
1862 break;
1863 case SDLK_RETURN:
1864 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
1865 (sdl_event.key.keysym.mod & KMOD_RALT)) {
1866 toggle_fullscreen();
1867 SDL_Delay(1);
1868 game_tick(1);
1869 need_redraw = 1;
1871 break;
1872 case SDLK_KP_PLUS:
1873 weapon_inc = 1;
1874 goto toggle_weapon;
1875 case SDLK_KP_MINUS:
1876 if((int)player_weapons[player_no][0] == 0)
1877 weapon_inc = WP_MAX-1;
1878 else weapon_inc = -1;
1879 toggle_weapon:
1880 player_weapons[player_no][0] += weapon_inc;
1881 if(player_weapons[player_no][0] == WP_INVALID)
1882 player_weapons[player_no][0] = 0;
1883 printf("%s\n", weapon_name(player_weapons[player_no][0]));
1884 need_redraw = 1;
1885 break;
1886 default:
1887 break;
1889 break;
1890 case SDL_KEYUP:
1891 switch(sdl_event.key.keysym.sym) {
1892 case SDLK_e:
1893 enemy_tag_loop();
1894 update_caption = game_update_caption;
1895 break;
1896 case SDLK_c:
1897 clear_screen();
1898 video_update();
1899 need_redraw = 1;
1900 break;
1901 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
1902 case SDLK_UP:
1903 case SDLK_DOWN:
1904 case SDLK_RIGHT:
1905 case SDLK_LEFT:
1906 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 0;
1907 goto check_anim;
1908 case SDLK_ESCAPE:
1909 goto dun_goofed;
1910 default:
1911 break;
1913 default:
1914 break;
1917 unsigned i;
1918 for (i = 0; i < ARRAY_SIZE(cursors_pressed); i++) {
1919 if(cursors_pressed[i]) {
1920 *moves[i].target += moves[i].dir;
1921 if(*moves[i].target < 0) *moves[i].target = 0;
1922 if(*moves[i].target > moves[i].max) *moves[i].target = moves[i].max;
1923 need_redraw = 1;
1926 game_tick(need_redraw);
1929 return 0;