fix regression: enemy soldiers fired in wrong direction
[rofl0r-openDOW.git] / spriteview.c
blob217d45e9a795116086572eafc27838fe3a64b6b6
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 return anim == ANIM_ENEMY_BOMBER_DIE || anim == ANIM_ENEMY_GUNNER_DIE ||
1090 anim == ANIM_ENEMY_BURNT || anim == ANIM_P1_DIE || anim == ANIM_P2_DIE;
1093 // removes bullets and other objects if they collide. return 1 if anything happened
1094 static int hit_bullets(sblist *bullet_list, sblist *target_list) {
1095 uint8_t *bullet_id;
1096 ssize_t li;
1097 int res = 0;
1098 enum bulletsubtype {
1099 BS_BULLET = 0,
1100 BS_FLAME = 1,
1101 BS_GRENADE_EXPL = 2,
1102 BS_BIG_EXPL = 3,
1103 } bullet_subtybe = BS_BULLET;
1105 sblist_iter_counter2s(bullet_list, li, bullet_id) {
1106 struct gameobj *bullet = &objs[*bullet_id];
1107 if(bullet->objtype == OBJ_FLAME) bullet_subtybe = BS_FLAME;
1108 else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1109 /* grenade kills only in the explosion, not in the smoke phase */
1110 if(bullet->objspecific.bullet.step_curr > 22) continue;
1111 bullet_subtybe = BS_GRENADE_EXPL;
1112 } else if(bullet->objtype == OBJ_BIG_EXPLOSION) {
1113 bullet_subtybe = BS_BIG_EXPL;
1116 vec2f bullet_center = get_gameobj_center(*bullet_id);
1117 if(bullet_list == &go_player_bullets || bullet_list == &go_flames) {
1118 if(is_wall(&bullet_center) == WT_SOLID) goto remove_bullet;
1121 const float bullet_radius[] = { [BS_BULLET] = 1.f, [BS_FLAME] = 6.f,
1122 [BS_GRENADE_EXPL] = 16.f, [BS_BIG_EXPL] = 19.f };
1124 size_t lj;
1125 uint8_t *target_id;
1126 sblist_iter_counter2(target_list, lj, target_id) {
1127 struct gameobj *target = &objs[*target_id];
1128 if(is_death_anim(target->animid)) continue;
1129 vec2f temp = get_gameobj_center(*target_id);
1130 float dist1 = vecdist(&bullet_center, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1131 vec2f newpos = vecadd(&bullet_center, &bullet->vel);
1132 float dist2 = vecdist(&newpos, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1134 unsigned w = palpic_getspritewidth(spritemaps[target->spritemap_id]),
1135 h = palpic_getspriteheight(spritemaps[target->spritemap_id]);
1136 float longest_side_div2 = ((float) MAX(h, w) / 2) * SCALE;
1137 if(dist1 < 1.f*SCALE || dist2 < 1.f*SCALE) { dprintf(2, "hit1\n"); goto hit; }
1138 if(dist1 < longest_side_div2 || dist2 < longest_side_div2) {
1139 vec2f velquarter = VEC(bullet->vel.x * 0.25, bullet->vel.y * 0.25);
1140 vec2f point = bullet_center;
1141 size_t k;
1142 for(k = 0; k < 4; k++) {
1143 if(point_in_mask(&point, *target_id)) {
1144 hit:
1146 if(bullet_list == &go_player_bullets) {
1147 if(target_list == &go_vehicles) {
1148 audio_play_wave_resource(wavesounds[WS_DROPSHOT]);
1149 goto remove_bullet;
1151 objs[player_ids[0]].objspecific.playerdata.score += 50;
1152 } else if (bullet_list == &go_rockets) {
1153 init_rocket_explosion(&target->pos);
1154 goto remove_bullet;
1155 } else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1156 // grenade explosion has no effect on vehicles and bunkers.
1157 if(target_list == &go_vehicles || target_list == &go_bunkers)
1158 goto next_bullet;
1160 enum animation_id death_anim = (bullet_subtybe == BS_FLAME && target_list == &go_enemies) ?
1161 ANIM_ENEMY_BURNT : get_die_anim(*target_id);
1162 if(death_anim == ANIM_INVALID) {
1163 gameobj_free(*target_id);
1164 sblist_delete(target_list, lj);
1165 lj--;
1166 goto next_target;
1168 switch_anim(*target_id, death_anim);
1169 target->vel = VEC(0,0);
1170 if(target->objtype == OBJ_ENEMY_BOMBER || target->objtype == OBJ_ENEMY_SHOOTER) {
1171 const enum wavesound_id wid[] = { WS_SCREAM, WS_SCREAM2 };
1172 audio_play_wave_resource(wavesounds[wid[rand()%2]]);
1173 if(bullet_subtybe == BS_BULLET) {
1174 vec2f bloodpos = vecadd(&target->pos, &VEC(0, (2+rand()%7)*SCALE));
1175 /* original displays at 0,8 but i prefer a random effect */
1176 //vec2f bloodpos = vecadd(&target->pos, &VEC(0, 8*SCALE));
1177 int id = init_blood(&bloodpos);
1178 if(id != -1) add_blood(id);
1181 if(bullet_subtybe == BS_BULLET) {
1182 remove_bullet:
1183 gameobj_free(*bullet_id);
1184 sblist_delete(bullet_list, li);
1185 li--;
1186 goto next_bullet;
1187 } else break;
1189 point = vecadd(&point, &velquarter);
1192 next_target:;
1194 next_bullet:
1195 res = 1;
1197 return res;
1200 uint32_t tickcounter;
1202 static int advance_animations(void) {
1203 size_t i, obj_visited;
1204 int res = 0;
1205 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1206 if(!obj_slot_used[i]) continue;
1207 struct gameobj *go = &objs[i];
1208 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)) {
1209 unsigned anim_delay = go->objtype == OBJ_BIG_EXPLOSION ? 8 : 4;
1210 if(go->anim_curr == ANIM_STEP_INIT || tickcounter % anim_delay == go->anim_frame) {
1211 anim_step last_anim = go->anim_curr;
1212 go->anim_curr = get_next_anim_frame(go->animid, go->anim_curr);
1213 if(last_anim != go->anim_curr) res = 1;
1216 obj_visited++;
1218 return res;
1221 static void switch_enemy_shape(int objid, const struct enemy_route* r) {
1222 switch_anim(objid, enemy_animation_lut[r->shape]);
1225 static void draw_golist(sblist *list) {
1226 size_t i;
1227 uint8_t *itemid;
1228 sblist_iter_counter2(list, i, itemid) {
1229 assert(obj_slot_used[*itemid]);
1230 struct gameobj *o = &objs[*itemid];
1231 const prgb *palette;
1232 switch(o->objtype) {
1233 case OBJ_BLOOD:
1234 // original blood color is bb5511 but that is hardly visible
1235 // palette = (prgb[]) {PRGB(0,0,0), PRGB(0xbb, 0x55, 0x11)};
1236 palette = (const prgb[]) {PRGB(0,0,0), PRGB(0xff, 0x0, 0x0)};
1237 break;
1238 case OBJ_ENEMY_BOMBER: case OBJ_ENEMY_SHOOTER:
1239 palette = map->enemy_palette;
1240 break;
1241 default:
1242 palette = 0;
1244 blit_sprite(o->pos.x, o->pos.y, &video,
1245 SCALE, spritemaps[o->spritemap_id],
1246 o->anim_curr == ANIM_STEP_INIT ? get_next_anim_frame(o->animid, o->anim_curr) : o->anim_curr,
1247 palette);
1251 static void draw_gameobjs(void) {
1252 draw_golist(&go_mines);
1253 draw_golist(&go_turrets);
1254 draw_golist(&go_bunkers);
1255 draw_golist(&go_vehicles);
1256 draw_golist(&go_enemies);
1257 draw_golist(&go_blood);
1258 draw_golist(&go_enemy_bullets);
1259 draw_golist(&go_boss);
1260 draw_golist(&go_rockets);
1261 draw_golist(&go_players);
1262 draw_golist(&go_player_bullets);
1263 draw_golist(&go_enemy_explosions);
1264 draw_golist(&go_flames);
1265 draw_golist(&go_enemy_flames);
1266 draw_golist(&go_explosions);
1267 draw_golist(&go_grenades);
1268 draw_golist(&go_enemy_grenades);
1269 draw_golist(&go_crosshair);
1270 draw_golist(&go_muzzleflash);
1273 static void process_soldiers(void) {
1274 uint8_t *itemid;
1275 sblist_iter(&go_enemies, itemid) {
1276 struct gameobj *go = &objs[*itemid];
1277 assert(go->objtype == OBJ_ENEMY_SHOOTER || go->objtype == OBJ_ENEMY_BOMBER);
1278 if (tickcounter % 4 == go->anim_frame) {
1279 const struct enemy_route *rc = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1280 if(rc->vel) {
1281 go->objspecific.enemy.curr_step++;
1282 if(enemy_fires(&go->objspecific.enemy)) {
1283 vec2f from = get_gameobj_center(*itemid);
1284 enum direction16 dir = get_shotdirection_from_enemy(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1285 enemy_fire_bullet(dir, 41, go->objspecific.enemy.spawn->weapon, &from);
1287 const struct enemy_route *rn = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1288 if(rn->shape != rc->shape) switch_enemy_shape(*itemid, rn);
1291 if(!is_death_anim(go->animid)) go->vel = get_enemy_vel(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1292 else go->vel = VEC(0, 0);
1296 static int process_turrets(sblist* list) {
1297 int res = 0;
1298 uint8_t *itemid;
1299 sblist_iter(list, itemid) {
1300 struct gameobj *go = &objs[*itemid];
1301 enum direction16 dir = DIR16_S;
1302 enum enemy_weapon ew = EW_GUN;
1303 int steps = 92;
1304 vec2f from;
1305 switch(go->objtype) {
1306 case OBJ_GUNTURRET_FIXED_NORTH:
1307 dir = DIR16_N;
1308 case OBJ_GUNTURRET_FIXED_SOUTH:
1309 from = get_gameobj_center(*itemid);
1310 if(rand()%8 == 0) {
1311 shot:
1312 enemy_fire_bullet(dir, steps, ew, &from);
1313 res = 1;
1315 break;
1316 case OBJ_FLAMETURRET:
1317 ew = EW_FLAME;
1318 steps = 48;
1320 if(objs[player_ids[0]].pos.y <= (192-25)*SCALE/2) dir = DIR16_N;
1321 from = go->pos;
1322 from.y += (dir == DIR16_N ? -8*SCALE : 8*SCALE);
1324 /* abusing curr_step variable to save position in a burst of flames */
1325 if(go->objspecific.enemy.curr_step && go->objspecific.enemy.curr_step < 16)
1326 goto do_flame;
1327 else if(go->objspecific.enemy.curr_step)
1328 go->objspecific.enemy.curr_step = 0;
1330 if(objs[player_ids[0]].pos.x < go->pos.x-32*SCALE || objs[player_ids[0]].pos.x > go->pos.x + (16+32)*SCALE)
1331 break;
1332 if(rand()%8 == 0) {
1333 do_flame:
1334 go->objspecific.enemy.curr_step++;
1335 goto shot;
1337 break;
1338 case OBJ_BUNKER5:
1339 if(tickcounter > (uint32_t) go->objspecific.enemy.curr_step + fps*3) {
1340 go->objspecific.enemy.curr_step = tickcounter;
1341 static const enum direction16 bunker5_dir[] = {
1342 [0] = DIR16_N, [1] = DIR16_NO, [2] = DIR16_O, [3] = DIR16_SO,
1343 [4] = DIR16_S, [5] = DIR16_SW, [6] = DIR16_W, [7] = DIR16_NW,
1345 static const vec2f bunker5_pos[] = {
1346 [0] = VEC(9,-8), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(22,10),
1347 [4] = VEC(9,16), [5] = VEC(-5,13), [6] = VEC(-7,2), [7] = VEC(-5,-6),
1349 unsigned b5;
1350 for(b5 = 0; b5 < 8; b5++) {
1351 from = go->pos;
1352 from.x += bunker5_pos[b5].x*SCALE;
1353 from.y += bunker5_pos[b5].y*SCALE;
1354 enemy_fire_bullet(bunker5_dir[b5], 28, EW_FLAME, &from);
1356 res = 1;
1358 break;
1359 default:;
1362 return res;
1365 static int move_gameobjs(void) {
1366 int res = 0;
1367 size_t i, obj_visited;
1368 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1369 if(obj_slot_used[i]) {
1370 struct gameobj *go = &objs[i];
1371 obj_visited++;
1372 if(go->anim_curr == ANIM_STEP_INIT) res = 1;
1373 if(go->vel.x != 0 || go->vel.y != 0) {
1374 vec2f oldpos = go->pos;
1375 go->pos.x += go->vel.x;
1376 go->pos.y += go->vel.y;
1378 if(go->objtype == OBJ_P1 || go->objtype == OBJ_P2) {
1379 if(go->pos.y < SCREEN_MIN_Y) go->pos.y = SCREEN_MIN_Y;
1380 else if(go->pos.y+25*SCALE > SCREEN_MAX_Y) go->pos.y = SCREEN_MAX_Y-25*SCALE;
1381 if(go->pos.x < SCREEN_MIN_X) go->pos.x = SCREEN_MIN_X;
1382 else if(go->pos.x+32*SCALE > SCREEN_MAX_X) go->pos.x = SCREEN_MAX_X-32*SCALE;
1383 vec2f center = get_sprite_center(spritemaps[go->spritemap_id]);
1384 center = vecadd(&center, &go->pos);
1385 if(is_wall(&center)) {
1386 go->pos = oldpos;
1387 go->vel = VEC(0,0);
1391 res = 1;
1393 if((go->objtype == OBJ_ENEMY_BOMBER || go->objtype == OBJ_ENEMY_SHOOTER) &&
1394 go->anim_curr == animations[go->animid].last &&
1395 (go->animid == ANIM_ENEMY_BOMBER_DIE ||
1396 go->animid == ANIM_ENEMY_GUNNER_DIE ||
1397 go->animid == ANIM_ENEMY_BURNT)) {
1398 dprintf(2, "removed enemy from %.2f,%.2f\n", go->pos.x, go->pos.y);
1399 gameobj_free(i);
1400 golist_remove(&go_enemies, i);
1401 res = 1;
1402 continue;
1407 return res;
1410 static void game_update_caption(void) {
1411 char buf [128];
1412 snprintf(buf, 128, "objs: %d, map x,y %d/%d, index %d, xoff %d, yoff %d, spawnscreen %d, line %d", (int) obj_count,
1413 (int)mapsquare.x, (int)mapsquare.y, (int)map->screen_map[mapsquare.y][mapsquare.x],
1414 (int)mapscreen_xoff, (int)mapscreen_yoff, (int)map_spawn_screen_index, (int) map_spawn_line);
1415 SDL_WM_SetCaption(buf, 0);
1417 static void(*update_caption)(void) = game_update_caption;
1419 static void game_tick(int force_redraw) {
1420 int need_redraw = force_redraw;
1421 if(mousebutton_down[MB_LEFT] > 1) {
1422 const int player_no = 0;
1423 const struct weapon *pw = get_active_weapon(player_no);
1424 //if(get_active_weapon_id(player_no) == WP_M134) __asm__("int3");
1425 if (pw->flags & WF_AUTOMATIC) {
1426 float shots_per_second = pw->rpm / 60.f;
1427 float shotinterval = fps / shots_per_second;
1428 if((int)((float)mousebutton_down[MB_LEFT] / shotinterval) != (int)((float)(mousebutton_down[MB_LEFT]-1) / shotinterval))
1429 fire_bullet(player_no);
1433 if(advance_animations()) need_redraw = 1;
1434 if(hit_bullets(&go_player_bullets, &go_enemies)) need_redraw = 1;
1435 if(hit_bullets(&go_player_bullets, &go_vehicles)) need_redraw = 1;
1436 if(hit_bullets(&go_flames, &go_enemies)) need_redraw = 1;
1437 if(hit_bullets(&go_enemy_flames, &go_enemies)) need_redraw = 1;
1438 if(hit_bullets(&go_rockets, &go_enemies)) need_redraw = 1;
1439 if(hit_bullets(&go_rockets, &go_vehicles)) need_redraw = 1;
1440 if(hit_bullets(&go_explosions, &go_enemies)) need_redraw = 1;
1441 if(hit_bullets(&go_explosions, &go_vehicles)) need_redraw = 1;
1442 if(hit_bullets(&go_explosions, &go_mines)) need_redraw = 1;
1443 if(hit_bullets(&go_explosions, &go_turrets)) need_redraw = 1;
1444 if(hit_bullets(&go_explosions, &go_bunkers)) need_redraw = 1;
1445 if(hit_bullets(&go_explosions, &go_players)) need_redraw = 1;
1446 if(hit_bullets(&go_enemy_explosions, &go_players)) need_redraw = 1;
1447 if(hit_bullets(&go_enemy_bullets, &go_players)) need_redraw = 1;
1448 if(hit_bullets(&go_enemy_flames, &go_players)) need_redraw = 1;
1449 if(remove_bullets(&go_player_bullets)) need_redraw = 1;
1450 if(remove_bullets(&go_flames)) need_redraw = 1;
1451 if(remove_bullets(&go_explosions)) need_redraw = 1;
1452 if(remove_bullets(&go_enemy_explosions)) need_redraw = 1;
1453 if(remove_bullets(&go_enemy_bullets)) need_redraw = 1;
1454 if(remove_bullets(&go_enemy_flames)) need_redraw = 1;
1455 if(remove_bullets(&go_muzzleflash)) need_redraw = 1;
1456 if(remove_bullets(&go_blood)) need_redraw = 1;
1457 if(remove_explosives(&go_grenades)) need_redraw = 1;
1458 if(remove_explosives(&go_enemy_grenades)) need_redraw = 1;
1459 if(remove_explosives(&go_rockets)) need_redraw = 1;
1460 if(tickcounter % 2 == 0 && scroll_map()) need_redraw = 1;
1462 process_soldiers();
1463 if(tickcounter % 4 == 0 && process_turrets(&go_turrets)) need_redraw = 1;
1464 if(tickcounter % 4 == 0 && process_turrets(&go_bunkers)) need_redraw = 1;
1466 if(move_gameobjs()) need_redraw = 1;
1468 if(remove_offscreen_objects(&go_enemies)) need_redraw = 1;
1469 if(remove_offscreen_objects(&go_vehicles)) need_redraw = 1;
1470 if(remove_offscreen_objects(&go_mines)) need_redraw = 1;
1471 if(remove_offscreen_objects(&go_turrets)) need_redraw = 1;
1472 if(remove_offscreen_objects(&go_bunkers)) need_redraw = 1;
1474 long ms_used = 0;
1475 struct timeval timer;
1476 gettimestamp(&timer);
1477 if(need_redraw) {
1478 draw_map();
1479 draw_gameobjs();
1480 draw_status_bar();
1481 video_update_region(SCREEN_MIN_X ,SCREEN_MIN_Y , SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1484 ms_used = mspassed(&timer);
1485 //if(ms_used) printf("repaint took: ms_used %ld\n", ms_used);
1486 int res = audio_process();
1487 if(res == -1) music_restart();
1488 ms_used = mspassed(&timer);
1489 //if(ms_used) printf("audio processed: %d, ms_used %ld\n", res, ms_used);
1491 long sleepms = 1000/fps - ms_used;
1492 if(sleepms >= 0) SDL_Delay(sleepms);
1493 if(mousebutton_down[MB_LEFT]) mousebutton_down[MB_LEFT]++;
1495 tickcounter++;
1496 update_caption();
1499 enum cursor {
1500 c_down = 0,
1501 c_up,
1502 c_left,
1503 c_right,
1506 static enum cursor cursor_lut[] = {
1507 [SDLK_UP] = c_up,
1508 [SDLK_DOWN] = c_down,
1509 [SDLK_LEFT] = c_left,
1510 [SDLK_RIGHT] = c_right,
1511 [SDLK_w] = c_up,
1512 [SDLK_a] = c_left,
1513 [SDLK_s] = c_down,
1514 [SDLK_d] = c_right,
1517 static char cursors_pressed[] = {
1518 [c_up] = 0,
1519 [c_down] = 0,
1520 [c_left] = 0,
1521 [c_right] = 0,
1524 static vec2f get_vel_from_direction(enum direction dir, float speed) {
1525 #define VELLUT(a, b, c) [a] = VEC(b, c)
1526 static const vec2f vel_lut[] = {
1527 VELLUT(DIR_O, 1, 0),
1528 VELLUT(DIR_NO, 0.7071067769704655, -0.7071067769704655),
1529 VELLUT(DIR_N, 0, -1),
1530 VELLUT(DIR_NW, -0.7071067769704655, -0.7071067769704655),
1531 VELLUT(DIR_W, -1, 0),
1532 VELLUT(DIR_SW, -0.7071067769704655, 0.7071067769704655),
1533 VELLUT(DIR_S, 0, 1),
1534 VELLUT(DIR_SO, 0.7071067769704655, 0.7071067769704655),
1536 #undef VELLUT
1537 vec2f v = vel_lut[dir];
1538 v.x *= speed * SCALE;
1539 v.y *= speed * SCALE;
1540 return v;
1543 static vec2f get_vel_from_direction16(enum direction16 dir, float speed) {
1544 #define VELLUT(a, b, c) [a] = VEC(b, c)
1545 #define ANK90 0.7071067769704655
1546 #define GK90 ANK90
1547 #define ANK45 0.9238795042037964
1548 #define GK45 0.3826834261417389
1549 static const vec2f vel_lut[] = {
1550 VELLUT(DIR16_O, 1, 0),
1551 VELLUT(DIR16_ONO, ANK45, -GK45),
1552 VELLUT(DIR16_NO, ANK90, -ANK90),
1553 VELLUT(DIR16_NNO, GK45, -ANK45),
1554 VELLUT(DIR16_N, 0, -1),
1555 VELLUT(DIR16_NNW, -GK45, -ANK45),
1556 VELLUT(DIR16_NW, -ANK90, -ANK90),
1557 VELLUT(DIR16_WNW, -ANK45, -GK45),
1558 VELLUT(DIR16_W, -1, 0),
1559 VELLUT(DIR16_WSW, -ANK45, GK45),
1560 VELLUT(DIR16_SW, -ANK90, ANK90),
1561 VELLUT(DIR16_SSW, -GK45, ANK45),
1562 VELLUT(DIR16_S, 0, 1),
1563 VELLUT(DIR16_SSO, GK45, ANK45),
1564 VELLUT(DIR16_SO, ANK90, ANK90),
1565 VELLUT(DIR16_OSO, ANK45, GK45),
1567 #undef VELLUT
1568 vec2f v = vel_lut[dir];
1569 v.x *= speed * SCALE;
1570 v.y *= speed * SCALE;
1571 return v;
1575 static enum direction get_direction_from_vec(vec2f *vel) {
1576 float deg_org, deg = atan2(vel->y, vel->x);
1577 deg_org = deg;
1578 if(deg < 0) deg *= -1.f;
1579 else if(deg > 0) deg = M_PI + (M_PI - deg); // normalize atan2 result to scale from 0 to 2 pi
1580 int hexadrant = (int)(deg / ((1.0/16.0f)*2*M_PI));
1581 assert(hexadrant >= 0 && hexadrant < 16);
1582 static const enum direction rad_lut[] = {
1583 DIR_O,
1584 DIR_NO, DIR_NO,
1585 DIR_N, DIR_N,
1586 DIR_NW, DIR_NW,
1587 DIR_W, DIR_W,
1588 DIR_SW, DIR_SW,
1589 DIR_S, DIR_S,
1590 DIR_SO, DIR_SO,
1591 DIR_O
1593 return rad_lut[hexadrant];
1596 static enum direction get_direction_from_cursor(void) {
1597 enum direction dir = DIR_INVALID;
1598 if(cursors_pressed[c_up]) {
1599 if(cursors_pressed[c_left]) dir = DIR_NW;
1600 else if(cursors_pressed[c_right]) dir = DIR_NO;
1601 else dir = DIR_N;
1602 } else if (cursors_pressed[c_down]) {
1603 if(cursors_pressed[c_left]) dir = DIR_SW;
1604 else if(cursors_pressed[c_right]) dir = DIR_SO;
1605 else dir = DIR_S;
1606 } else if (cursors_pressed[c_left]) {
1607 dir = DIR_W;
1608 } else if (cursors_pressed[c_right]) {
1609 dir = DIR_O;
1611 return dir;
1614 static enum animation_id get_anim_from_direction(enum direction dir, int player_no, int throwing) {
1615 #define DIRMAP(a, b) [a] = b
1616 if(!throwing) {
1617 static const enum animation_id dir_map_p1[] = {
1618 DIRMAP(DIR_N, ANIM_P1_MOVE_N),
1619 DIRMAP(DIR_NW, ANIM_P1_MOVE_NW),
1620 DIRMAP(DIR_W, ANIM_P1_MOVE_W),
1621 DIRMAP(DIR_SW, ANIM_P1_MOVE_SW),
1622 DIRMAP(DIR_S, ANIM_P1_MOVE_S),
1623 DIRMAP(DIR_SO, ANIM_P1_MOVE_SO),
1624 DIRMAP(DIR_O, ANIM_P1_MOVE_O),
1625 DIRMAP(DIR_NO, ANIM_P1_MOVE_NO),
1627 static const enum animation_id dir_map_p2[] = {
1628 DIRMAP(DIR_N, ANIM_P2_MOVE_N),
1629 DIRMAP(DIR_NW, ANIM_P2_MOVE_NW),
1630 DIRMAP(DIR_W, ANIM_P2_MOVE_W),
1631 DIRMAP(DIR_SW, ANIM_P2_MOVE_SW),
1632 DIRMAP(DIR_S, ANIM_P2_MOVE_S),
1633 DIRMAP(DIR_SO, ANIM_P2_MOVE_SO),
1634 DIRMAP(DIR_O, ANIM_P2_MOVE_O),
1635 DIRMAP(DIR_NO, ANIM_P2_MOVE_NO),
1637 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1 : dir_map_p2;
1638 return dir_map[dir];
1639 } else {
1640 static const enum animation_id dir_map_p1_g[] = {
1641 DIRMAP(DIR_N, ANIM_P1_THROW_N),
1642 DIRMAP(DIR_NW, ANIM_P1_THROW_NW),
1643 DIRMAP(DIR_W, ANIM_P1_THROW_W),
1644 DIRMAP(DIR_SW, ANIM_P1_THROW_SW),
1645 DIRMAP(DIR_S, ANIM_P1_THROW_S),
1646 DIRMAP(DIR_SO, ANIM_P1_THROW_SO),
1647 DIRMAP(DIR_O, ANIM_P1_THROW_O),
1648 DIRMAP(DIR_NO, ANIM_P1_THROW_NO),
1650 static const enum animation_id dir_map_p2_g[] = {
1651 DIRMAP(DIR_N, ANIM_P2_THROW_N),
1652 DIRMAP(DIR_NW, ANIM_P2_THROW_NW),
1653 DIRMAP(DIR_W, ANIM_P2_THROW_W),
1654 DIRMAP(DIR_SW, ANIM_P2_THROW_SW),
1655 DIRMAP(DIR_S, ANIM_P2_THROW_S),
1656 DIRMAP(DIR_SO, ANIM_P2_THROW_SO),
1657 DIRMAP(DIR_O, ANIM_P2_THROW_O),
1658 DIRMAP(DIR_NO, ANIM_P2_THROW_NO),
1660 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1_g : dir_map_p2_g;
1661 return dir_map[dir];
1663 #undef DIRMAP
1666 #if 0
1667 static enum animation_id get_anim_from_cursor(void) {
1668 enum direction dir = get_direction_from_cursor();
1669 if(dir == DIR_INVALID) return ANIM_INVALID;
1670 return get_anim_from_direction(dir, 0, 0);
1672 /* playerno is either 0 or 1, not player_id! */
1673 static enum animation_id get_anim_from_vel(int player_no, vec2f *vel) {
1674 enum direction dir = get_direction_from_vec(vel);
1675 if(dir == DIR_INVALID) return ANIM_INVALID;
1676 return get_anim_from_direction(dir, player_no, 0);
1678 #endif
1680 static void switch_anim(int obj_id, int aid) {
1681 if(objs[obj_id].animid == aid) return;
1682 gameobj_start_anim(obj_id, aid);
1685 enum map_index choose_mission(void);
1686 //RcB: DEP "mission_select.c"
1687 #include "enemytag.c"
1688 int main() {
1689 video_init();
1690 clear_screen();
1691 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
1692 SDL_EnableKeyRepeat(100, 20);
1694 audio_init();
1696 /* background music for mission selection screen */
1697 music_play(TUNE_MAP);
1699 if((current_map = choose_mission()) == MI_INVALID) goto dun_goofed;
1701 music_play(TUNE_FIGHTING);
1703 SDL_ShowCursor(0);
1706 int startx = 10;
1707 int starty = 10;
1709 //redraw(surface, startx, starty);
1710 const float player_speed = 1.25f;
1711 const struct palpic* spritemap = spritemaps[SI_PLAYERS];
1712 struct { int *target; int dir; int max;} moves[] = {
1713 [c_up] = {&starty, SCALE * -1, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
1714 [c_down] = {&starty, SCALE, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
1715 [c_left] = {&startx, SCALE * -1, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
1716 [c_right] = {&startx, SCALE, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
1719 init_game_objs();
1720 int player_no = 0;
1721 int player_id = player_ids[player_no];
1723 game_tick(1);
1725 SDL_Event sdl_event;
1726 while(1) {
1727 unsigned need_redraw = 0;
1728 int weapon_inc = 0;
1729 while (SDL_PollEvent(&sdl_event)) {
1730 need_redraw = 0;
1731 switch (sdl_event.type) {
1732 case SDL_MOUSEMOTION:
1733 if((int)mousepos->x != sdl_event.motion.x || (int)mousepos->y != sdl_event.motion.y)
1734 need_redraw = 1;
1735 mousepos->x = sdl_event.motion.x;
1736 mousepos->y = sdl_event.motion.y;
1737 break;
1738 case SDL_MOUSEBUTTONDOWN:
1739 mousepos->x = sdl_event.button.x;
1740 mousepos->y = sdl_event.button.y;
1741 mousebutton_down[MB_LEFT] = 1;
1742 fire_bullet(player_no);
1743 break;
1744 case SDL_MOUSEBUTTONUP:
1745 mousepos->x = sdl_event.button.x;
1746 mousepos->y = sdl_event.button.y;
1747 mousebutton_down[MB_LEFT] = 0;
1748 break;
1749 case SDL_QUIT:
1750 dun_goofed:
1751 video_cleanup();
1752 return 0;
1753 case SDL_KEYDOWN:
1754 switch(sdl_event.key.keysym.sym) {
1755 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
1756 case SDLK_UP:
1757 case SDLK_DOWN:
1758 case SDLK_RIGHT:
1759 case SDLK_LEFT:
1760 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 1;
1761 check_anim: {
1762 enum direction dir = get_direction_from_cursor();
1763 if(dir != DIR_INVALID) {
1764 if(!mousebutton_down[MB_LEFT]) {
1765 // change animation according to cursors,
1766 // unless we're in automatic fire mode.
1767 enum animation_id aid = get_anim_from_direction(dir, player_no, 0);
1768 if(aid != ANIM_INVALID) switch_anim(player_id, aid);
1770 objs[player_id].vel = get_vel_from_direction(dir, player_speed);
1771 } else {
1772 objs[player_id].vel = VEC(0,0);
1775 break;
1776 case SDLK_RETURN:
1777 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
1778 (sdl_event.key.keysym.mod & KMOD_RALT)) {
1779 toggle_fullscreen();
1780 SDL_Delay(1);
1781 game_tick(1);
1782 need_redraw = 1;
1784 break;
1785 case SDLK_KP_PLUS:
1786 weapon_inc = 1;
1787 goto toggle_weapon;
1788 case SDLK_KP_MINUS:
1789 if((int)player_weapons[player_no][0] == 0)
1790 weapon_inc = WP_MAX-1;
1791 else weapon_inc = -1;
1792 toggle_weapon:
1793 player_weapons[player_no][0] += weapon_inc;
1794 if(player_weapons[player_no][0] == WP_INVALID)
1795 player_weapons[player_no][0] = 0;
1796 printf("%s\n", weapon_name(player_weapons[player_no][0]));
1797 need_redraw = 1;
1798 break;
1799 default:
1800 break;
1802 break;
1803 case SDL_KEYUP:
1804 switch(sdl_event.key.keysym.sym) {
1805 case SDLK_e:
1806 enemy_tag_loop();
1807 update_caption = game_update_caption;
1808 break;
1809 case SDLK_c:
1810 clear_screen();
1811 video_update();
1812 need_redraw = 1;
1813 break;
1814 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
1815 case SDLK_UP:
1816 case SDLK_DOWN:
1817 case SDLK_RIGHT:
1818 case SDLK_LEFT:
1819 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 0;
1820 goto check_anim;
1821 case SDLK_ESCAPE:
1822 goto dun_goofed;
1823 default:
1824 break;
1826 default:
1827 break;
1830 unsigned i;
1831 for (i = 0; i < ARRAY_SIZE(cursors_pressed); i++) {
1832 if(cursors_pressed[i]) {
1833 *moves[i].target += moves[i].dir;
1834 if(*moves[i].target < 0) *moves[i].target = 0;
1835 if(*moves[i].target > moves[i].max) *moves[i].target = moves[i].max;
1836 need_redraw = 1;
1839 game_tick(need_redraw);
1842 return 0;