use lists to draw gameobjs, so the order can be arranged
[rofl0r-openDOW.git] / spriteview.c
blobf3b4f452efb42eab81128330e34d26ea54c86901
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"
27 #include <SDL/SDL.h>
29 #ifndef IN_KDEVELOP_PARSER
30 #include "../lib/include/bitarray.h"
31 #include "weapon_sprites.c"
33 #endif
35 enum mousebutton {
36 MB_LEFT = 0,
37 MB_RIGHT,
40 // 1 if button down, 0 if not, >1 to count ms pressed
41 unsigned long mousebutton_down[] = {
42 [MB_LEFT] = 0,
43 [MB_RIGHT] = 0,
46 //RcB: LINK "-lSDL"
47 #if 0
48 static void get_last_move_event(SDL_Event* e) {
49 #define numpeek 32
50 SDL_Event peek[numpeek];
51 SDL_Event* last_event = NULL;
52 int i, results;
53 results = SDL_PeepEvents(peek, numpeek, SDL_PEEKEVENT, (uint32_t) ~0);
54 if(results == -1) return;
55 for(i = 0; i < results; i++) {
56 if(peek[i].type == SDL_MOUSEMOTION)
57 last_event = &peek[i];
58 else
59 break;
61 if(last_event) {
62 *e = *last_event;
63 SDL_PeepEvents(peek, i + 1, SDL_GETEVENT, (uint32_t) ~0);
65 #undef numpeek
67 #endif
69 static vec2f get_sprite_center(const struct palpic *p) {
70 assert(p->spritecount);
71 vec2f res;
72 res.x = palpic_getspritewidth(p) * SCALE / 2;
73 res.y = palpic_getspriteheight(p) * SCALE / 2;
74 return res;
77 static int player_ids[2];
78 static enum weapon_id player_weapons[2][WP_MAX];
79 static int weapon_count[2];
80 static enum weapon_id weapon_active[2]; // index into player_weapons[playerno]
81 static int player_ammo[2][AMMO_MAX];
82 static enum weapon_id get_active_weapon_id(int player_no);
83 static void switch_anim(int obj_id, int aid);
84 static vec2f get_vel_from_direction(enum direction dir, float speed);
85 static vec2f get_vel_from_direction16(enum direction16 dir, float speed);
86 // used by game_tick
87 static sblist go_player_bullets;
88 static sblist go_enemy_bullets;
89 static sblist go_explosions;
90 static sblist go_enemy_explosions;
91 static sblist go_enemies;
92 static sblist go_players;
93 static sblist go_flames;
94 static sblist go_rockets;
95 static sblist go_grenades;
96 static sblist go_enemy_grenades;
97 static sblist go_vehicles;
98 static sblist go_mines;
99 static sblist go_turrets;
100 static sblist go_bunkers;
101 static sblist go_boss;
102 static sblist go_crosshair;
103 static sblist go_muzzleflash;
104 static void add_pbullet(uint8_t bullet_id) {
105 sblist_add(&go_player_bullets, &bullet_id);
107 static void add_ebullet(uint8_t bullet_id) {
108 sblist_add(&go_enemy_bullets, &bullet_id);
110 static void add_player(uint8_t player_id) {
111 sblist_add(&go_players, &player_id);
113 static void add_enemy(uint8_t enem_id) {
114 sblist_add(&go_enemies, &enem_id);
116 static void add_explosion(uint8_t expl_id) {
117 sblist_add(&go_explosions, &expl_id);
119 static void add_enemy_explosion(uint8_t expl_id) {
120 sblist_add(&go_enemy_explosions, &expl_id);
122 static void add_flame(uint8_t id) {
123 sblist_add(&go_flames, &id);
125 static void add_grenade(uint8_t id) {
126 sblist_add(&go_grenades, &id);
128 static void add_enemy_grenade(uint8_t id) {
129 sblist_add(&go_enemy_grenades, &id);
131 static void add_rocket(uint8_t id) {
132 sblist_add(&go_rockets, &id);
134 static void add_vehicle(uint8_t id) {
135 sblist_add(&go_vehicles, &id);
137 static void add_mine(uint8_t id) {
138 sblist_add(&go_mines, &id);
140 static void add_turret(uint8_t id) {
141 sblist_add(&go_turrets, &id);
143 static void add_bunker(uint8_t id) {
144 sblist_add(&go_bunkers, &id);
146 static void add_boss(uint8_t id) {
147 sblist_add(&go_boss, &id);
149 static void add_crosshair(uint8_t id) {
150 sblist_add(&go_crosshair, &id);
152 static void add_muzzleflash(uint8_t id) {
153 sblist_add(&go_muzzleflash, &id);
155 static void golist_remove(sblist *l, uint8_t objid) {
156 size_t i;
157 uint8_t *itemid;
158 sblist_iter_counter2(l, i, itemid) {
159 if(*itemid == objid) {
160 sblist_delete(l, i);
161 return;
166 static int get_next_anim_frame(enum animation_id aid, anim_step curr) {
167 if(curr == ANIM_STEP_INIT) return animations[aid].first;
168 curr++;
169 if(curr > animations[aid].last) return animations[aid].first;
170 return curr;
173 #define SCREEN_MIN_X 64*SCALE
174 #define SCREEN_MAX_X VMODE_W - 64*SCALE
175 #define SCREEN_MIN_Y 0
176 #define SCREEN_MAX_Y 200*SCALE
178 static void draw_status_bar(void) {
179 enum weapon_id wid = get_active_weapon_id(0);
180 int x, y;
181 sdl_rgb_t *ptr = (sdl_rgb_t *) video.mem;
182 unsigned pitch = video.pitch/4;
183 for(y = SCREEN_MAX_Y; y < VMODE_H; y++)
184 for (x = SCREEN_MIN_X; x < SCREEN_MAX_X; x++)
185 ptr[y*pitch + x] = SRGB_BLACK;
187 blit_sprite(((320 / 2) - (59 / 2)) * SCALE, (200 + (40/2) - (16/2)) * SCALE,
188 &video, SCALE, &weapon_sprites.header, wid, 0);
190 char buf[16];
191 snprintf(buf, 16, "%.6u", objs[player_ids[0]].objspecific.playerdata.score);
192 font_print(SCREEN_MIN_X + 8, SCREEN_MAX_Y + 8, buf, 6, 1 * SCALE, PRGB(255,255,255));
195 enum map_index current_map = MI_VIETNAM;
196 const struct map *map;
197 const struct map_screen* map_scr;
198 const struct palpic *map_bg;
199 const struct palpic *map_fg;
200 const mapscreen_index *map_bonus_indices;
201 const struct map_fglayer *map_bonus_scr;
203 int mapscreen_yoff, mapscreen_xoff;
204 struct { int x,y; } mapsquare;
205 enum map_scrolldir mapscrolldir;
206 unsigned map_spawn_screen_index;
207 unsigned map_spawn_line;
208 unsigned map_spawn_current;
209 #include "maps/spawn_australia.c"
210 const struct enemy_spawn_screen *spawn_map = spawn_screens_australia;
212 static void init_map(enum map_index mapindex) {
213 map = maps[mapindex];
214 map_scr = map_screens[mapindex];
215 map_bg = map_bg_sprites[map->maptype];
216 map_fg = map_fg_sprites[map->maptype];
217 map_bonus_scr = map_bonus_screens[mapindex];
218 map_bonus_indices = map_bonus_layer_indices[mapindex];
219 mapscreen_yoff = 0;
220 mapscreen_xoff = 0;
221 mapsquare.x = 5;
222 mapsquare.y = 26;
223 mapscrolldir = MS_UP;
224 map_spawn_screen_index = 0;
225 map_spawn_line = 0;
226 map_spawn_current = 0;
229 static mapscreen_index get_bonus_layer_index(mapscreen_index screen) {
230 unsigned i;
231 for (i = 0; i < map->bonuslayer_count; i++)
232 if(map_bonus_indices[i] == screen) return i;
233 return MAPSCREEN_BLOCKED;
236 static mapscreen_index screen_to_mapscreen(int *x, int *y) {
237 *x = ((int) *x - SCREEN_MIN_X) / SCALE;
238 *y = ((int) *y - SCREEN_MIN_Y) / SCALE;
239 int yscr = (*y + mapscreen_yoff) / 192;
240 *y = (*y + mapscreen_yoff) - yscr*192;
241 int xscr = (*x + mapscreen_xoff) / 192;
242 *x = (*x + mapscreen_xoff) - xscr*192;
243 return map->screen_map[mapsquare.y + yscr][mapsquare.x + xscr];
246 static enum walltype is_wall(vec2f *pos) {
247 int x = pos->x;
248 int y = pos->y;
249 mapscreen_index scr_idx = screen_to_mapscreen(&x, &y);
250 /* can happen when a bullet goes partially off-screen */
251 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
252 uint8_t spriteno = map_scr[scr_idx].fg.fg[y/16][x/16];
253 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
254 scr_idx = get_bonus_layer_index(scr_idx);
255 if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
256 spriteno = map_bonus_scr[scr_idx].fg[y/16][x/16];
257 if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
258 return WT_NONE;
261 static void draw_map() {
262 int y, x, my, mx;
263 unsigned map_off = 192-mapscreen_yoff;
264 unsigned vis_x = 192-mapscreen_xoff;
265 int x_iter_max16 = 192/16;
266 int x_iter_max64 = 192/64;
267 unsigned x_iter_start64 = x_iter_max64 - (vis_x / 64 + !!(vis_x % 64));
268 unsigned x_iter_start16 = x_iter_max16 - (vis_x / 16 + !!(vis_x % 16));
270 int x_screen_start64 = -(!!(vis_x%64)*64-(vis_x%64));
271 int x_screen_start16 = -(!!(vis_x%16)*16-(vis_x%16));
273 unsigned x_screen_iter;
274 for(x_screen_iter = 0; x_screen_iter <= !!mapscreen_xoff; x_screen_iter++) {
275 mapscreen_index bonus_layer;
276 if(x_screen_iter) {
277 x_screen_start16 = x_screen_start16+(x_iter_max16-x_iter_start16)*16;
278 x_screen_start64 = x_screen_start64+(x_iter_max64-x_iter_start64)*64;
279 x_iter_max16 = mapscreen_xoff / 16 + !!(mapscreen_xoff % 16);
280 x_iter_max64 = mapscreen_xoff / 64 + !!(mapscreen_xoff % 64);
281 x_iter_start16 = 0;
282 x_iter_start64 = 0;
283 assert(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter] != MAPSCREEN_BLOCKED);
285 uint8_t spriteno;
287 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)
288 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
289 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
290 blit_sprite(x, y, &video,
291 SCALE, map_bg, spriteno, 0);
293 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)
294 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
295 spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
296 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
298 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]);
299 if(bonus_layer != MAPSCREEN_BLOCKED) {
300 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)
301 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
302 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
303 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
308 int yleft = 200-map_off;
309 if(yleft > 192) yleft = 192;
310 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/32+!!(yleft%32); my++, y+=32*SCALE)
311 for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
312 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
313 blit_sprite(x, y, &video, SCALE, map_bg, spriteno, 0);
315 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
316 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
317 spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
318 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
321 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]);
322 if(bonus_layer != MAPSCREEN_BLOCKED) {
323 for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
324 for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
325 spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
326 if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
330 /* this is never triggered when mapscreen_xoff != 0 */
331 if(mapscreen_yoff > 192 - 8) {
332 for(mx = 0, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < 3; mx++, x += 64*SCALE)
333 blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video,
334 SCALE, map_bg, map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].bg.bg[0][mx], 0);
335 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
336 spriteno = map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].fg.fg[0][mx];
337 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
339 bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+2][mapsquare.x]);
340 if(bonus_layer != MAPSCREEN_BLOCKED) {
341 for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
342 spriteno = map_bonus_scr[bonus_layer].fg[0][mx];
343 if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
350 #define VSCROLL_TRESHOLD (200-74)
351 #define HSCROLLR_TRESHOLD (54-6)
352 #define HSCROLLL_TRESHOLD (192-(78+3))
353 static int scroll_needed() {
354 struct gameobj *player = &objs[player_ids[0]];
355 if((mapscrolldir == MS_UP && player->pos.y - SCREEN_MIN_Y < VSCROLL_TRESHOLD*SCALE) ||
356 (mapscrolldir == MS_RIGHT && player->pos.x - SCREEN_MIN_X > HSCROLLR_TRESHOLD*SCALE) ||
357 (mapscrolldir == MS_LEFT && player->pos.x - SCREEN_MIN_X < HSCROLLL_TRESHOLD*SCALE))
358 return 1;
359 return 0;
362 static void scroll_gameobjs(int scroll_step) {
363 if(!scroll_step) return;
364 unsigned i, avail = obj_count;
365 for(i = 0; i < OBJ_MAX && avail; i++) {
366 if(!obj_slot_used[i]) continue;
367 avail--;
368 if(objs[i].objtype == OBJ_CROSSHAIR) continue;
370 if(mapscrolldir == MS_UP)
371 objs[i].pos.y += scroll_step*SCALE;
372 else if(mapscrolldir == MS_LEFT)
373 objs[i].pos.x += scroll_step*SCALE;
374 else if(mapscrolldir == MS_RIGHT)
375 objs[i].pos.x -= scroll_step*SCALE;
379 static void next_screen() {
380 map_spawn_screen_index++;
381 map_spawn_line = 0;
382 map_spawn_current = 0;
385 static int init_enemy(const struct enemy_spawn *spawn);
386 static void handle_spawns(unsigned scrollstep) {
387 assert(scrollstep <= 192);
388 unsigned i;
389 if(!spawn_map[map_spawn_screen_index].spawns) goto done;
390 for(i = 0; i < scrollstep; i++) {
391 while(map_spawn_current < spawn_map[map_spawn_screen_index].num_spawns &&
392 map_spawn_line+i >= spawn_map[map_spawn_screen_index].spawns[map_spawn_current].scroll_line) {
393 init_enemy(&spawn_map[map_spawn_screen_index].spawns[map_spawn_current]);
394 map_spawn_current++;
397 done:
398 map_spawn_line += scrollstep;
401 static int scroll_map() {
402 int ret = 0;
403 int scroll_step = 1;
404 if(scroll_needed()) {
405 if(mapscrolldir == MS_UP) {
406 mapscreen_yoff -= scroll_step;
407 if(mapscreen_yoff < 0) {
408 mapsquare.y--;
409 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
410 scroll_step = -mapscreen_yoff;
411 mapscreen_yoff = 0;
412 mapsquare.y++;
413 scroll_gameobjs(scroll_step);
414 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
415 mapscrolldir = MS_LEFT;
416 } else {
417 mapscrolldir = MS_RIGHT;
418 next_screen();
420 scroll_step = 0;
421 } else {
422 next_screen();
423 mapscreen_yoff += 192;
426 handle_objs:;
427 handle_spawns(scroll_step);
428 scroll_gameobjs(scroll_step);
429 ret = 1;
430 } else if(mapscrolldir == MS_LEFT) {
431 mapscreen_xoff -= scroll_step;
432 if(mapscreen_xoff < 0) {
433 mapsquare.x--;
434 if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
435 scroll_step = -mapscreen_xoff;
436 mapscreen_xoff = 0;
437 mapscreen_yoff = 0;
438 mapsquare.x++;
439 scroll_gameobjs(scroll_step);
440 mapscrolldir = MS_UP;
441 scroll_step = 0;
442 } else {
443 mapscreen_xoff += 192;
444 next_screen();
447 goto handle_objs;
448 } else if(mapscrolldir == MS_RIGHT) {
449 mapscreen_xoff += scroll_step;
450 if(mapscreen_xoff >= 192) {
451 mapsquare.x++;
452 if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
453 scroll_step = mapscreen_xoff - 192;
454 mapscreen_xoff = 0;
455 mapscreen_yoff = 0;
456 scroll_gameobjs(scroll_step);
457 mapscrolldir = MS_UP;
458 scroll_step = 0;
459 } else {
460 next_screen();
461 mapscreen_xoff -= 192;
464 goto handle_objs;
467 return ret;
470 static int init_player(int player_no) {
471 assert(player_no == 0 || player_no == 1);
472 int pid = gameobj_alloc();
473 gameobj_init(pid, &VEC( SCREEN_MIN_X, SCREEN_MAX_Y - (25 * SCALE) ), &VEC( 0, 0 ),
474 SI_PLAYERS, player_no == 0 ? ANIM_P1_MOVE_N : ANIM_P2_MOVE_N, player_no == 0 ? OBJ_P1 : OBJ_P2);
475 if(pid == -1) return -1;
476 player_ids[player_no] = pid;
477 objs[pid].objspecific.playerdata.score = 0;
478 player_weapons[player_no][0] = WP_COLT45;
479 weapon_count[player_no] = 1;
480 weapon_active[player_no] = 0;
481 size_t i = 0;
482 for(; i < AMMO_MAX; i++)
483 player_ammo[player_no][i] = 50000;
484 add_player(pid);
485 return pid;
488 static vec2f *mousepos;
489 static int init_crosshair() {
490 int id = gameobj_alloc();
491 gameobj_init(id, &VEC(VMODE_W/2, VMODE_H/2), &VEC(0,0), SI_CROSSHAIR, ANIM_CROSSHAIR, OBJ_CROSSHAIR);
492 if(id == -1) return -1;
493 mousepos = &objs[id].pos;
494 return id;
497 static int init_bullet(vec2f *pos, vec2f *vel, int steps) {
498 int id = gameobj_alloc();
499 gameobj_init(id, pos, vel, SI_BULLET, ANIM_BULLET, OBJ_BULLET);
500 gameobj_init_bulletdata(id, steps);
501 return id;
504 static int init_grenade(vec2f *pos, vec2f *vel, int steps) {
505 int id = gameobj_alloc();
506 gameobj_init(id, pos, vel, SI_GRENADE, ANIM_GRENADE_SMALL, OBJ_GRENADE);
507 gameobj_init_bulletdata(id, steps);
508 return id;
511 static int init_grenade_explosion(vec2f *pos, int from_enemy) {
512 const int ticks_per_anim_frame = 4;
513 const int expl_anim_frames = 11;
514 vec2f grenade_center = get_sprite_center(spritemaps[SI_GRENADE_EXPLOSION]);
515 vec2f mypos = vecsub(pos, &grenade_center);
516 int id = gameobj_alloc();
517 if(id == -1) return -1;
518 gameobj_init(id, &mypos, &VEC(0,0), SI_GRENADE_EXPLOSION, ANIM_GRENADE_EXPLOSION, OBJ_GRENADE_EXPLOSION);
519 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
520 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
521 if(!from_enemy) add_explosion(id);
522 else add_enemy_explosion(id);
523 return id;
526 static int init_big_explosion(vec2f *pos) {
527 const int ticks_per_anim_frame = 8;
528 const int expl_anim_frames = 5;
529 vec2f rocket_center = get_sprite_center(spritemaps[SI_BIG_EXPLOSION]);
530 vec2f mypos = vecsub(pos, &rocket_center);
531 int id = gameobj_alloc();
532 if(id == -1) return -1;
533 gameobj_init(id, &mypos, &VEC(0,0), SI_BIG_EXPLOSION, ANIM_BIG_EXPLOSION, OBJ_BIG_EXPLOSION);
534 gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
535 audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
536 add_explosion(id);
537 return id;
540 static int init_rocket_explosion(vec2f *pos) {
541 vec2f ax = vecadd(pos, &VEC(-15*SCALE, 9*SCALE));
542 vec2f bx = vecadd(pos, &VEC(1*SCALE, -6*SCALE));
543 vec2f cx = vecadd(pos, &VEC(-8*SCALE, -8*SCALE));
544 vec2f dx = vecadd(pos, &VEC(8*SCALE, 8*SCALE));
545 int ret = 0;
546 ret += init_grenade_explosion(&ax, 0) != -1;
547 ret += init_grenade_explosion(&bx, 0) != -1;
548 ret += init_big_explosion(&cx) != -1;
549 ret += init_big_explosion(&dx) != -1;
550 return ret;
553 static int init_flame(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
554 static const vec2f flame_origin[] = {
555 [DIR_O] = { 4.0, 8.0 },
556 [DIR_NO] = { 5.0, 11.0 },
557 [DIR_N] = { 7.5, 12.0 },
558 [DIR_NW] = { 10.0, 11.0 },
559 [DIR_W] = { 11.0, 8.0 },
560 [DIR_SW] = { 10.0, 5.0 },
561 [DIR_S] = { 7.5, 3.0 },
562 [DIR_SO] = { 4.0, 4.0 },
564 vec2f mypos = *pos;
565 mypos.x -= flame_origin[dir].x * SCALE;
566 mypos.y -= flame_origin[dir].y * SCALE;
567 int id = gameobj_alloc();
568 if(id == -1) return -1;
569 gameobj_init(id, &mypos, vel, SI_FLAME, ANIM_FLAME, OBJ_FLAME);
570 gameobj_init_bulletdata(id, steps);
571 add_flame(id);
572 return id;
575 static int init_rocket(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
576 static const vec2f rocket_origin[] = {
577 [DIR_N] = { 1.0, 10.0 },
578 [DIR_S] = { 1.0, 0.0 },
579 [DIR_O] = { 0.0, 1.0 },
580 [DIR_W] = { 10.0, 1.0 },
581 [DIR_NO] = { 0.0, 7.0 },
582 [DIR_SO] = { 0.0, 0.0 },
583 [DIR_SW] = { 7.0, 0.0 },
584 [DIR_NW] = { 7.0, 7.0 },
586 static const enum animation_id rocket_anim[] = {
587 [DIR_N] = ANIM_ROCKET_N,
588 [DIR_S] = ANIM_ROCKET_S,
589 [DIR_O] = ANIM_ROCKET_O,
590 [DIR_W] = ANIM_ROCKET_W,
591 [DIR_NO] = ANIM_ROCKET_NO,
592 [DIR_SO] = ANIM_ROCKET_SO,
593 [DIR_SW] = ANIM_ROCKET_SW,
594 [DIR_NW] = ANIM_ROCKET_NW,
596 vec2f mypos = *pos;
597 mypos.x -= rocket_origin[dir].x * SCALE;
598 mypos.y -= rocket_origin[dir].y * SCALE;
599 int id = gameobj_alloc();
600 if(id == -1) return -1;
601 gameobj_init(id, &mypos, vel, SI_ROCKET, rocket_anim[dir], OBJ_ROCKET);
602 gameobj_init_bulletdata(id, steps);
603 add_rocket(id);
604 return id;
608 static const struct enemy_route* get_enemy_current_route(int curr_step, const struct enemy_spawn *spawn) {
609 int i = ENEMY_MAX_ROUTE -1;
610 for(; i >= 0; i--)
611 if(spawn->route[i].shape != ES_INVALID &&
612 curr_step >= spawn->route[i].start_step)
613 return &spawn->route[i];
614 return 0;
617 static vec2f get_enemy_vel(int curr_step, const struct enemy_spawn *spawn) {
618 const struct enemy_route *route = get_enemy_current_route(curr_step, spawn);
619 return get_vel_from_direction16(route->dir, (float)route->vel/8.f);
622 static const enum animation_id enemy_animation_lut[] = {
623 [ES_SOLDIER1_DOWN] = ANIM_ENEMY_GUNNER_DOWN,
624 [ES_SOLDIER1_RIGHT] = ANIM_ENEMY_GUNNER_RIGHT,
625 [ES_SOLDIER1_LEFT] = ANIM_ENEMY_GUNNER_LEFT,
626 [ES_SOLDIER2_DOWN] = ANIM_ENEMY_BOMBER_DOWN,
627 [ES_SOLDIER2_RIGHT] = ANIM_ENEMY_BOMBER_RIGHT,
628 [ES_SOLDIER2_LEFT] = ANIM_ENEMY_BOMBER_LEFT,
629 [ES_JEEP] = ANIM_JEEP,
630 [ES_TANK_SMALL] = ANIM_TANK_SMALL,
631 [ES_TANK_BIG] = ANIM_TANK_BIG,
632 [ES_TRANSPORTER] = ANIM_TRANSPORTER,
633 [ES_GUNTURRET_MOVABLE_MACHINE] = ANIM_GUNTURRET_MOVABLE_MACHINE_S,
634 [ES_GUNTURRET_MOVABLE_MAN] = ANIM_GUNTURRET_MOVABLE_MAN_S,
635 [ES_MINE_FLAT] = ANIM_MINE_FLAT,
636 [ES_MINE_CROSS] = ANIM_MINE_CROSSED,
637 [ES_FLAMETURRET] = ANIM_FLAMETURRET,
638 [ES_GUNTURRET_FIXED_SOUTH] = ANIM_GUNTURRET_FIXED_SOUTH,
639 [ES_GUNTURRET_FIXED_NORTH] = ANIM_GUNTURRET_FIXED_NORTH,
640 [ES_BUNKER_1] = ANIM_BUNKER1,
641 [ES_BUNKER_2] = ANIM_BUNKER2,
642 [ES_BUNKER_3] = ANIM_BUNKER3,
643 [ES_BUNKER_4] = ANIM_BUNKER4,
644 [ES_BUNKER_5] = ANIM_BUNKER5,
647 static int init_enemy(const struct enemy_spawn *spawn) {
648 const enum objtype enemy_soldier_objtype_lut[] = {
649 [0] = OBJ_ENEMY_SHOOTER,
650 [1] = OBJ_ENEMY_BOMBER
652 const enum objtype enemy_objtype_lut[] = {
653 [ES_JEEP] = OBJ_JEEP,
654 [ES_TANK_SMALL] = OBJ_TANK_SMALL,
655 [ES_TANK_BIG] = OBJ_TANK_BIG,
656 [ES_TRANSPORTER] = OBJ_TRANSPORTER,
657 [ES_GUNTURRET_MOVABLE_MACHINE] = OBJ_GUNTURRET_MOVABLE_MACHINE,
658 [ES_GUNTURRET_MOVABLE_MAN] = OBJ_GUNTURRET_MOVABLE_MAN,
659 [ES_MINE_FLAT] = OBJ_MINE_FLAT,
660 [ES_MINE_CROSS] = OBJ_MINE_CROSSED,
661 [ES_FLAMETURRET] = OBJ_FLAMETURRET,
662 [ES_GUNTURRET_FIXED_SOUTH] = OBJ_GUNTURRET_FIXED_SOUTH,
663 [ES_GUNTURRET_FIXED_NORTH] = OBJ_GUNTURRET_FIXED_NORTH,
664 [ES_BUNKER_1] = OBJ_BUNKER1,
665 [ES_BUNKER_2] = OBJ_BUNKER2,
666 [ES_BUNKER_3] = OBJ_BUNKER3,
667 [ES_BUNKER_4] = OBJ_BUNKER4,
668 [ES_BUNKER_5] = OBJ_BUNKER5,
669 [ES_BOSS] = OBJ_BOSS,
671 const enum animation_id boss_animation_lut[] = {
672 [0] = ANIM_BOSS1,
673 [1] = ANIM_BOSS2,
674 [2] = ANIM_BOSS3,
675 [3] = ANIM_BOSS4,
676 [4] = ANIM_BOSS5,
677 [5] = ANIM_BOSS6,
678 [6] = ANIM_BOSS7,
679 [7] = ANIM_BOSS8,
680 [8] = ANIM_BOSS9,
681 [9] = ANIM_BOSS10,
682 [10] = ANIM_BOSS11,
683 [11] = ANIM_BOSS12,
685 const enum sprite_index enemy_soldier_sprite_lut[] = {
686 [ET_ASIAN] = SI_ENEMY_ASIAN,
687 [ET_WESTERN] = SI_ENEMY_WESTERN,
689 const enum sprite_index enemy_sprite_lut[] = {
690 [ES_JEEP] = SI_VEHICLES_SMALL,
691 [ES_TANK_SMALL] = SI_VEHICLES_MEDIUM,
692 [ES_TANK_BIG] = SI_VEHICLES_BIG,
693 [ES_TRANSPORTER] = SI_VEHICLES_BIG,
694 [ES_BUNKER_1] = SI_BUNKERS,
695 [ES_BUNKER_2] = SI_BUNKERS,
696 [ES_BUNKER_3] = SI_BUNKERS,
697 [ES_BUNKER_4] = SI_BUNKERS,
698 [ES_BUNKER_5] = SI_BUNKERS,
699 [ES_GUNTURRET_MOVABLE_MACHINE] = SI_GUNTURRET,
700 [ES_GUNTURRET_MOVABLE_MAN] = SI_GUNTURRET,
701 [ES_MINE_FLAT] = SI_MINES,
702 [ES_MINE_CROSS] = SI_MINES,
703 [ES_FLAMETURRET] = SI_MINES,
704 [ES_GUNTURRET_FIXED_SOUTH] = SI_MINES,
705 [ES_GUNTURRET_FIXED_NORTH] = SI_MINES,
706 [ES_BOSS] = SI_BOSSES,
709 vec2f spawnpos = VEC(SCREEN_MIN_X + spawn->x*SCALE, SCREEN_MIN_Y + spawn->y*SCALE);
710 int id = gameobj_alloc();
711 if(id == -1) return -1;
712 const struct enemy_route* route_curr = get_enemy_current_route(0, spawn);
713 vec2f vel = get_enemy_vel(0, spawn);
715 int is_soldier = route_curr->shape <= ES_SOLDIER2_RIGHT;
716 int is_boss = route_curr->shape == ES_BOSS;
717 enum sprite_index spriteid;
718 enum objtype objid;
719 enum animation_id animid;
720 if(is_soldier) {
721 spriteid = enemy_soldier_sprite_lut[map->enemy_type];
722 objid = enemy_soldier_objtype_lut[spawn->weapon];
723 } else {
724 spriteid = enemy_sprite_lut[route_curr->shape];
725 objid = enemy_objtype_lut[route_curr->shape];
727 if(is_boss) animid = boss_animation_lut[map->boss_id];
728 else animid = enemy_animation_lut[route_curr->shape];
730 gameobj_init(id, &spawnpos, &vel, spriteid, animid, objid);
731 objs[id].objspecific.enemy.curr_step = 0;
732 objs[id].objspecific.enemy.spawn = spawn;
733 switch(objid) {
734 case OBJ_BOSS:
735 add_boss(id);
736 break;
737 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
738 case OBJ_BUNKER4: case OBJ_BUNKER5:
739 add_bunker(id);
740 break;
741 case OBJ_FLAMETURRET: case OBJ_GUNTURRET_FIXED_NORTH:
742 case OBJ_GUNTURRET_FIXED_SOUTH:
743 add_turret(id);
744 break;
745 case OBJ_MINE_CROSSED: case OBJ_MINE_FLAT:
746 add_mine(id);
747 break;
748 case OBJ_JEEP: case OBJ_TRANSPORTER:
749 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
750 add_vehicle(id);
751 break;
752 default:
753 add_enemy(id);
755 return id;
758 static void remove_enemy(int id) {
759 enum objtype objid = objs[id].objtype;
760 switch(objid) {
761 case OBJ_JEEP: case OBJ_TRANSPORTER:
762 case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
763 golist_remove(&go_vehicles, id);
764 break;
765 default:
766 golist_remove(&go_enemies, id);
768 gameobj_free(id);
771 static int enemy_fires(struct enemy *e) {
772 int i;
773 for(i = 0; i < ENEMY_MAX_SHOT; i++)
774 if(e->curr_step == e->spawn->shots[i]) return 1;
775 return 0;
778 static enum animation_id get_flash_animation_from_direction(enum direction dir) {
779 #define ANIMF(dir, anim) [dir] = anim
780 static const enum animation_id dir_to_anim[] = {
781 ANIMF(DIR_O, ANIM_FLASH_O),
782 ANIMF(DIR_NO, ANIM_FLASH_NO),
783 ANIMF(DIR_N, ANIM_FLASH_N),
784 ANIMF(DIR_NW, ANIM_FLASH_NW),
785 ANIMF(DIR_W, ANIM_FLASH_W),
786 ANIMF(DIR_SW, ANIM_FLASH_SW),
787 ANIMF(DIR_S, ANIM_FLASH_S),
788 ANIMF(DIR_SO, ANIM_FLASH_SO),
790 #undef ANIMF
791 return dir_to_anim[dir];
794 static int init_flash(vec2f *pos, enum direction dir) {
795 int id = gameobj_alloc();
796 gameobj_init(id, pos, &VEC(0, 0), SI_FLASH, get_flash_animation_from_direction(dir), OBJ_FLASH);
797 gameobj_init_bulletdata(id, 2);
798 return id;
801 static vec2f get_gameobj_pos(int obj_id) {
802 return objs[obj_id].pos;
805 static vec2f get_gameobj_center(int obj_id) {
806 vec2f res = objs[obj_id].pos;
807 vec2f add = get_sprite_center(spritemaps[objs[obj_id].spritemap_id]);
808 return vecadd(&res, &add);
811 static enum direction get_direction_from_vec(vec2f *vel);
812 static enum animation_id get_anim_from_direction(enum direction dir, int player, int throwing);
814 static enum weapon_id get_active_weapon_id(int player_no) {
815 return player_weapons[player_no][weapon_active[player_no]];
818 static const struct weapon* get_active_weapon(int player_no) {
819 return &weapons[get_active_weapon_id(player_no)];
821 static enum direction get_shotdirection_from_enemy(int curr_step, const struct enemy_spawn *spawn) {
822 const struct enemy_route* r = get_enemy_current_route(curr_step, spawn);
823 switch(r->shape) {
824 case ES_SOLDIER1_DOWN: case ES_SOLDIER2_DOWN:
825 return DIR_S;
826 case ES_SOLDIER1_LEFT: case ES_SOLDIER2_LEFT:
827 return DIR_W;
828 case ES_SOLDIER1_RIGHT: case ES_SOLDIER2_RIGHT:
829 return DIR_O;
830 default:
831 assert(0);
835 static void enemy_fire_bullet(int objid) {
836 struct gameobj* go = &objs[objid];
837 enum direction dir = get_shotdirection_from_enemy(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
838 vec2f from = get_gameobj_center(objid);
839 vec2f vel = get_vel_from_direction(dir, 1.75);
840 int id;
841 if(go->objspecific.enemy.spawn->weapon == EW_GUN) {
842 id = init_bullet(&from, &vel, 41);
843 if(id != -1) add_ebullet(id);
844 } else {
845 id = init_grenade(&from, &vel, 41);
846 if(id != -1) add_enemy_grenade(id);
850 static int get_crosshair_id(void) {
851 assert(sblist_getsize(&go_crosshair));
852 uint8_t *id = sblist_get(&go_crosshair, 0);
853 return *id;
856 static void fire_bullet(int player_no) {
857 int id;
858 const struct weapon *pw = get_active_weapon(player_no);
859 if(player_ammo[player_no][pw->ammo] == 0) return;
860 vec2f from = get_gameobj_center(player_ids[player_no]);
861 //get_anim_from_vel(0, objs[player].
862 vec2f to = get_gameobj_center(get_crosshair_id());
863 to.x += 4*SCALE - rand()%8*SCALE;
864 to.y += 4*SCALE - rand()%8*SCALE;
865 vec2f vel = velocity(&from, &to);
866 enum direction dir = get_direction_from_vec(&vel);
867 if(dir != DIR_INVALID) {
868 enum animation_id aid = get_anim_from_direction(dir, player_no, pw->ammo == AMMO_GRENADE);
869 if(aid != ANIM_INVALID) switch_anim(player_ids[player_no], aid);
870 anim_step curranim = objs[player_ids[player_no]].anim_curr;
871 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[player_ids[player_no]].animid, ANIM_STEP_INIT);
872 vec2f muzzle = muzzle_tab[curranim];
874 from = get_gameobj_pos(player_ids[player_no]);
875 from.x += muzzle.x * SCALE;
876 from.y += muzzle.y * SCALE;
878 if(pw->flags & WF_MUZZLEFLASH) {
879 static const vec2f flash_start[] = {
880 [DIR_O] = { 0.0, 1.0 },
881 [DIR_NO] = { 0.5, 6.0 },
882 [DIR_N] = { 1.0, 7.5 },
883 [DIR_NW] = { 6.0, 6.0 },
884 [DIR_W] = { 7.5, 1.0 },
885 [DIR_SW] = { 4.5, 0.0 },
886 [DIR_S] = { 1.0, 0.0 },
887 [DIR_SO] = { 0.0, 0.0 },
889 vec2f ffrom = from;
890 ffrom.x -= flash_start[dir].x * SCALE;
891 ffrom.y -= flash_start[dir].y * SCALE;
892 id = init_flash(&ffrom, dir);
893 if(id != -1) add_muzzleflash(id);
895 vel = velocity(&from, &to);
897 float dist = veclength(&vel);
898 float speed = pw->bullet_speed * SCALE;
899 const uint16_t range_tab[] = {0, 80, 66, 80, 118, 118, 118, 118, 118, 118,
900 200, 200, 240, 240, 240, 240, 240, 240, 240, 240, 320 };
901 float range = range_tab[pw->range] * SCALE;
902 if(dist > range)
903 dist = range;
904 float steps = dist / speed;
905 float deg = atan2(vel.y, vel.x);
906 vel.x = cos(deg) * speed;
907 vel.y = sin(deg) * speed;
908 switch(pw->shot) {
909 case ST_LAUNCHER:
910 id = init_rocket(dir, &from, &vel, steps);
911 break;
912 case ST_BULLET:
913 id = init_bullet(&from, &vel, steps);
914 if(id != -1) add_pbullet(id);
915 break;
916 case ST_FLAMES:
917 id = init_flame(dir, &from, &vel, steps);
918 break;
919 case ST_GRENADE:
920 id = init_grenade(&from, &vel, steps);
921 add_grenade(id);
922 break;
923 default:
924 abort();
926 player_ammo[player_no][pw->ammo]--;
927 const WAVE_HEADER_COMPLETE *wf = wavesounds[pw->sound];
928 if(id != -1 && pw->sound != WS_NONE)
929 audio_play_wave_resource(wf);
932 static void init_game_objs() {
933 sblist_init(&go_crosshair, 1, 4);
934 sblist_init(&go_players, 1, 4);
935 sblist_init(&go_muzzleflash, 1, 4);
936 sblist_init(&go_player_bullets, 1, 32);
937 sblist_init(&go_flames, 1, 32);
938 sblist_init(&go_enemy_bullets, 1, 32);
939 sblist_init(&go_explosions, 1, 16);
940 sblist_init(&go_enemy_explosions, 1, 16);
941 sblist_init(&go_grenades, 1, 16);
942 sblist_init(&go_enemy_grenades, 1, 16);
943 sblist_init(&go_rockets, 1, 8);
944 sblist_init(&go_enemies, 1, 32);
945 sblist_init(&go_vehicles, 1, 4);
946 sblist_init(&go_mines, 1, 4);
947 sblist_init(&go_turrets, 1, 8);
948 sblist_init(&go_bunkers, 1, 4);
949 sblist_init(&go_boss, 1, 4);
950 init_player(0);
951 add_crosshair(init_crosshair());
952 init_map(current_map);
955 static int point_in_mask(vec2f *point, int obj_id) {
956 vec2f pos_in_pic = VEC((point->x - objs[obj_id].pos.x) / SCALE,
957 (point->y - objs[obj_id].pos.y) / SCALE);
958 const struct palpic *p = spritemaps[objs[obj_id].spritemap_id];
959 unsigned h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
960 if(pos_in_pic.x < 0 || pos_in_pic.y < 0 || pos_in_pic.x > w || pos_in_pic.y > h) return 0;
961 assert(objs[obj_id].anim_curr != ANIM_STEP_INIT);
962 anim_step curranim = objs[obj_id].anim_curr;
963 if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[obj_id].animid, ANIM_STEP_INIT);
964 uint8_t *data = palpic_getspritedata(p, curranim);
965 if(data[(unsigned) pos_in_pic.y * w + (unsigned) pos_in_pic.x] != 0) return 1;
966 return 0;
969 static enum animation_id get_die_anim(unsigned id) {
970 switch(objs[id].objtype) {
971 case OBJ_P1:
972 return ANIM_P1_DIE;
973 case OBJ_P2:
974 return ANIM_P2_DIE;
975 case OBJ_JEEP:
976 return ANIM_JEEP_DESTROYED;
977 case OBJ_TANK_SMALL:
978 return ANIM_TANK_SMALL_DESTROYED;
979 case OBJ_TANK_BIG:
980 return ANIM_TANK_BIG_DESTROYED;
981 case OBJ_TRANSPORTER:
982 return ANIM_TRANSPORTER_DESTROYED;
983 case OBJ_ENEMY_BOMBER:
984 return ANIM_ENEMY_BOMBER_DIE;
985 case OBJ_ENEMY_SHOOTER:
986 return ANIM_ENEMY_GUNNER_DIE;
987 case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
988 case OBJ_BUNKER4: case OBJ_BUNKER5:
989 return ANIM_BUNKER_DESTROYED;
990 case OBJ_GUNTURRET_MOVABLE_MACHINE:
991 return ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED;
992 case OBJ_GUNTURRET_MOVABLE_MAN:
993 return ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED;
994 default:
995 return ANIM_INVALID;
999 /* remove bullets that have reached their maximum number of steps */
1000 static int remove_bullets(sblist *list) {
1001 int res = 0;
1002 uint8_t *item_id;
1003 ssize_t li;
1004 sblist_iter_counter2s(list, li, item_id) {
1005 struct gameobj *bullet = &objs[*item_id];
1006 if(bullet->objspecific.bullet.step_curr >= bullet->objspecific.bullet.step_max) {
1007 gameobj_free(*item_id);
1008 sblist_delete(list, li);
1009 li--;
1010 } else {
1011 bullet->objspecific.bullet.step_curr++;
1013 res = 1;
1015 return res;
1018 static int remove_explosives(sblist *list) {
1019 int res = 0;
1020 uint8_t *item_id;
1021 ssize_t li;
1022 sblist_iter_counter2s(list, li, item_id) {
1023 struct gameobj *go = &objs[*item_id];
1024 if(go->objspecific.bullet.step_curr >= go->objspecific.bullet.step_max) {
1025 if(go->objtype == OBJ_GRENADE) init_grenade_explosion(&go->pos, list == &go_enemy_grenades);
1026 else init_rocket_explosion(&go->pos);
1027 gameobj_free(*item_id);
1028 sblist_delete(list, li);
1029 li--;
1030 } else {
1031 go->objspecific.bullet.step_curr++;
1032 if(go->objtype == OBJ_GRENADE) {
1033 if(go->objspecific.bullet.step_curr >= 32) go->animid = ANIM_GRENADE_SMALL;
1034 else if(go->objspecific.bullet.step_curr >= 8) go->animid = ANIM_GRENADE_BIG;
1037 res = 1;
1039 return res;
1042 static int remove_offscreen_objects(sblist *list) {
1043 int res = 0;
1044 uint8_t *item_id;
1045 ssize_t li;
1046 sblist_iter_counter2s(list, li, item_id) {
1047 assert(obj_slot_used[*item_id]);
1048 struct gameobj *go = &objs[*item_id];
1049 assert((int) go->spritemap_id < SI_MAX);
1050 const struct palpic *p = spritemaps[go->spritemap_id];
1051 assert(p->spritecount);
1052 int h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
1053 if(go->pos.x < SCREEN_MIN_X-w*SCALE || go->pos.x > SCREEN_MAX_X ||
1054 go->pos.y < SCREEN_MIN_Y-h*SCALE || go->pos.y > SCREEN_MAX_Y) {
1055 res = 1;
1056 dprintf(2, "offscreen: removed gameobj %d\n", (int) *item_id);
1057 gameobj_free(*item_id);
1058 sblist_delete(list, li);
1059 li--;
1062 return res;
1065 static int is_death_anim(enum animation_id anim) {
1066 return anim == ANIM_ENEMY_BOMBER_DIE || anim == ANIM_ENEMY_GUNNER_DIE ||
1067 anim == ANIM_ENEMY_BURNT || anim == ANIM_P1_DIE || anim == ANIM_P2_DIE;
1070 // removes bullets and other objects if they collide. return 1 if anything happened
1071 static int hit_bullets(sblist *bullet_list, sblist *target_list) {
1072 uint8_t *bullet_id;
1073 ssize_t li;
1074 int res = 0;
1075 enum bulletsubtype {
1076 BS_BULLET = 0,
1077 BS_FLAME = 1,
1078 BS_GRENADE_EXPL = 2,
1079 BS_BIG_EXPL = 3,
1080 } bullet_subtybe = BS_BULLET;
1082 sblist_iter_counter2s(bullet_list, li, bullet_id) {
1083 struct gameobj *bullet = &objs[*bullet_id];
1084 if(bullet->objtype == OBJ_FLAME) bullet_subtybe = BS_FLAME;
1085 else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1086 /* grenade kills only in the explosion, not in the smoke phase */
1087 if(bullet->objspecific.bullet.step_curr > 22) continue;
1088 bullet_subtybe = BS_GRENADE_EXPL;
1089 } else if(bullet->objtype == OBJ_BIG_EXPLOSION) {
1090 bullet_subtybe = BS_BIG_EXPL;
1093 vec2f bullet_center = get_gameobj_center(*bullet_id);
1094 if(bullet_list == &go_player_bullets || bullet_list == &go_flames) {
1095 if(is_wall(&bullet_center) == WT_SOLID) goto remove_bullet;
1098 const float bullet_radius[] = { [BS_BULLET] = 1.f, [BS_FLAME] = 6.f,
1099 [BS_GRENADE_EXPL] = 16.f, [BS_BIG_EXPL] = 19.f };
1101 size_t lj;
1102 uint8_t *target_id;
1103 sblist_iter_counter2(target_list, lj, target_id) {
1104 struct gameobj *target = &objs[*target_id];
1105 if(is_death_anim(target->animid)) continue;
1106 vec2f temp = get_gameobj_center(*target_id);
1107 float dist1 = vecdist(&bullet_center, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1108 vec2f newpos = vecadd(&bullet_center, &bullet->vel);
1109 float dist2 = vecdist(&newpos, &temp) - bullet_radius[bullet_subtybe] * SCALE;
1111 unsigned w = palpic_getspritewidth(spritemaps[target->spritemap_id]),
1112 h = palpic_getspriteheight(spritemaps[target->spritemap_id]);
1113 float longest_side_div2 = ((float) MAX(h, w) / 2) * SCALE;
1114 if(dist1 < 1.f*SCALE || dist2 < 1.f*SCALE) { dprintf(2, "hit1\n"); goto hit; }
1115 if(dist1 < longest_side_div2 || dist2 < longest_side_div2) {
1116 vec2f velquarter = VEC(bullet->vel.x * 0.25, bullet->vel.y * 0.25);
1117 vec2f point = bullet_center;
1118 size_t k;
1119 for(k = 0; k < 4; k++) {
1120 if(point_in_mask(&point, *target_id)) {
1121 hit:
1123 if(bullet_list == &go_player_bullets) {
1124 if(target_list == &go_vehicles) {
1125 audio_play_wave_resource(wavesounds[WS_DROPSHOT]);
1126 goto remove_bullet;
1128 objs[player_ids[0]].objspecific.playerdata.score += 50;
1129 } else if (bullet_list == &go_rockets) {
1130 init_rocket_explosion(&target->pos);
1131 goto remove_bullet;
1132 } else if(bullet->objtype == OBJ_GRENADE_EXPLOSION) {
1133 // grenade explosion has no effect on vehicles and bunkers.
1134 if(target_list == &go_vehicles || target_list == &go_bunkers)
1135 goto next_bullet;
1137 enum animation_id death_anim = bullet_subtybe == BS_FLAME ? ANIM_ENEMY_BURNT : get_die_anim(*target_id);
1138 if(death_anim == ANIM_INVALID) {
1139 gameobj_free(*target_id);
1140 sblist_delete(target_list, lj);
1141 lj--;
1142 goto next_target;
1144 switch_anim(*target_id, death_anim);
1145 target->vel = VEC(0,0);
1146 if(target->objtype == OBJ_ENEMY_BOMBER || target->objtype == OBJ_ENEMY_SHOOTER) {
1147 const enum wavesound_id wid[] = { WS_SCREAM, WS_SCREAM2 };
1148 audio_play_wave_resource(wavesounds[wid[rand()%2]]);
1150 if(bullet_subtybe == BS_BULLET) {
1151 remove_bullet:
1152 gameobj_free(*bullet_id);
1153 sblist_delete(bullet_list, li);
1154 li--;
1155 goto next_bullet;
1156 } else break;
1158 point = vecadd(&point, &velquarter);
1161 next_target:;
1163 next_bullet:
1164 res = 1;
1166 return res;
1169 uint32_t tickcounter;
1171 static int advance_animations(void) {
1172 size_t i, obj_visited;
1173 int res = 0;
1174 for(i = 0, obj_visited = 0; obj_visited < obj_count && i < OBJ_MAX; i++) {
1175 if(!obj_slot_used[i]) continue;
1176 struct gameobj *go = &objs[i];
1177 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)) {
1178 unsigned anim_delay = go->objtype == OBJ_BIG_EXPLOSION ? 8 : 4;
1179 if(go->anim_curr == ANIM_STEP_INIT || tickcounter % anim_delay == go->anim_frame) {
1180 anim_step last_anim = go->anim_curr;
1181 go->anim_curr = get_next_anim_frame(go->animid, go->anim_curr);
1182 if(last_anim != go->anim_curr) res = 1;
1185 obj_visited++;
1187 return res;
1190 static void switch_enemy_shape(int objid, const struct enemy_route* r) {
1191 switch_anim(objid, enemy_animation_lut[r->shape]);
1194 static void draw_golist(sblist *list) {
1195 size_t i;
1196 uint8_t *itemid;
1197 sblist_iter_counter2(list, i, itemid) {
1198 assert(obj_slot_used[*itemid]);
1199 struct gameobj *o = &objs[*itemid];
1200 const prgb *palette = (o->objtype == OBJ_ENEMY_BOMBER || o->objtype == OBJ_ENEMY_SHOOTER) ? map->enemy_palette : 0;
1201 blit_sprite(o->pos.x, o->pos.y, &video,
1202 SCALE, spritemaps[o->spritemap_id],
1203 o->anim_curr == ANIM_STEP_INIT ? get_next_anim_frame(o->animid, o->anim_curr) : o->anim_curr,
1204 palette);
1208 static void draw_gameobjs(void) {
1209 draw_golist(&go_mines);
1210 draw_golist(&go_turrets);
1211 draw_golist(&go_bunkers);
1212 draw_golist(&go_vehicles);
1213 draw_golist(&go_enemies);
1214 draw_golist(&go_enemy_bullets);
1215 draw_golist(&go_boss);
1216 draw_golist(&go_rockets);
1217 draw_golist(&go_players);
1218 draw_golist(&go_player_bullets);
1219 draw_golist(&go_enemy_explosions);
1220 draw_golist(&go_flames);
1221 draw_golist(&go_explosions);
1222 draw_golist(&go_grenades);
1223 draw_golist(&go_enemy_grenades);
1224 draw_golist(&go_crosshair);
1225 draw_golist(&go_muzzleflash);
1228 static void game_update_caption(void) {
1229 char buf [128];
1230 snprintf(buf, 128, "objs: %d, map x,y %d/%d, index %d, xoff %d, yoff %d, spawnscreen %d, line %d", (int) obj_count,
1231 (int)mapsquare.x, (int)mapsquare.y, (int)map->screen_map[mapsquare.y][mapsquare.x],
1232 (int)mapscreen_xoff, (int)mapscreen_yoff, (int)map_spawn_screen_index, (int) map_spawn_line);
1233 SDL_WM_SetCaption(buf, 0);
1235 static void(*update_caption)(void) = game_update_caption;
1237 static void game_tick(int force_redraw) {
1238 int need_redraw = force_redraw;
1239 size_t obj_visited;
1240 const int fps = 64;
1241 size_t i;
1242 if(mousebutton_down[MB_LEFT] > 1) {
1243 const int player_no = 0;
1244 const struct weapon *pw = get_active_weapon(player_no);
1245 //if(get_active_weapon_id(player_no) == WP_M134) __asm__("int3");
1246 if (pw->flags & WF_AUTOMATIC) {
1247 float shots_per_second = pw->rpm / 60.f;
1248 float shotinterval = fps / shots_per_second;
1249 if((int)((float)mousebutton_down[MB_LEFT] / shotinterval) != (int)((float)(mousebutton_down[MB_LEFT]-1) / shotinterval))
1250 fire_bullet(player_no);
1253 /* 1) remove bullets/rockets hitting walls (and spawn expl in the latter case)- TODO
1254 * 2) check player bullets against enemies, remove bullets and enemies
1255 * 3) check enemy bullets against players, remove bullets and players
1256 * 4) bullets: move*
1257 * 5) check grenades, remove and spawn exposions if necessary
1258 * 6) enemies: move, kill player on hit
1259 * 7) player: move
1262 // remove dead enemies
1263 sblist_iter_counter2(&go_enemies, li, &item_id) {
1264 if(objs[item_id].anim_curr == animations[objs[item_id].animid].last &&
1265 (objs[item_id].animid == ANIM_ENEMY_BOMBER_DIE || objs[item_id].animid == ANIM_ENEMY_GUNNER_DIE))
1268 if(advance_animations()) need_redraw = 1;
1269 if(hit_bullets(&go_player_bullets, &go_enemies)) need_redraw = 1;
1270 if(hit_bullets(&go_player_bullets, &go_vehicles)) need_redraw = 1;
1271 if(hit_bullets(&go_flames, &go_enemies)) need_redraw = 1;
1272 if(hit_bullets(&go_rockets, &go_enemies)) need_redraw = 1;
1273 if(hit_bullets(&go_rockets, &go_vehicles)) need_redraw = 1;
1274 if(hit_bullets(&go_explosions, &go_enemies)) need_redraw = 1;
1275 if(hit_bullets(&go_explosions, &go_vehicles)) need_redraw = 1;
1276 if(hit_bullets(&go_explosions, &go_mines)) need_redraw = 1;
1277 if(hit_bullets(&go_explosions, &go_turrets)) need_redraw = 1;
1278 if(hit_bullets(&go_explosions, &go_bunkers)) need_redraw = 1;
1279 if(hit_bullets(&go_explosions, &go_players)) need_redraw = 1;
1280 if(hit_bullets(&go_enemy_explosions, &go_players)) need_redraw = 1;
1281 if(hit_bullets(&go_enemy_bullets, &go_players)) need_redraw = 1;
1282 if(remove_bullets(&go_player_bullets)) need_redraw = 1;
1283 if(remove_bullets(&go_flames)) need_redraw = 1;
1284 if(remove_bullets(&go_explosions)) need_redraw = 1;
1285 if(remove_bullets(&go_enemy_explosions)) need_redraw = 1;
1286 if(remove_bullets(&go_enemy_bullets)) need_redraw = 1;
1287 if(remove_explosives(&go_grenades)) need_redraw = 1;
1288 if(remove_explosives(&go_enemy_grenades)) need_redraw = 1;
1289 if(remove_explosives(&go_rockets)) need_redraw = 1;
1290 if(tickcounter % 2 == 0 && scroll_map()) need_redraw = 1;
1292 size_t obj_count_copy = obj_count;
1293 for(i = 0, obj_visited = 0; obj_visited < obj_count_copy && i < OBJ_MAX; i++) {
1294 if(obj_slot_used[i]) {
1295 struct gameobj *go = &objs[i];
1296 obj_visited++;
1297 if(go->anim_curr == ANIM_STEP_INIT) need_redraw = 1;
1298 if(go->objtype == OBJ_FLASH) {
1299 if(go->objspecific.bullet.step_curr >= go->objspecific.bullet.step_max) {
1300 gameobj_free(i);
1301 golist_remove(&go_muzzleflash, i);
1302 need_redraw = 1;
1303 continue;
1304 } else go->objspecific.bullet.step_curr++;
1305 } else if (go->objtype == OBJ_ENEMY_SHOOTER || go->objtype == OBJ_ENEMY_BOMBER) {
1306 if (tickcounter % 4 == go->anim_frame) {
1307 const struct enemy_route *rc = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1308 if(rc->vel) {
1309 go->objspecific.enemy.curr_step++;
1310 if(enemy_fires(&go->objspecific.enemy)) {
1311 enemy_fire_bullet(i);
1313 const struct enemy_route *rn = get_enemy_current_route(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1314 if(rn->shape != rc->shape) switch_enemy_shape(i, rn);
1317 if(!is_death_anim(go->animid)) go->vel = get_enemy_vel(go->objspecific.enemy.curr_step, go->objspecific.enemy.spawn);
1318 else go->vel = VEC(0, 0);
1320 int ismoving = 0;
1321 if(go->vel.x != 0 || go->vel.y != 0) {
1322 ismoving = 1;
1323 vec2f oldpos = go->pos;
1324 go->pos.x += go->vel.x;
1325 go->pos.y += go->vel.y;
1327 if(go->objtype == OBJ_P1 || go->objtype == OBJ_P2) {
1328 if(go->pos.y < SCREEN_MIN_Y) go->pos.y = SCREEN_MIN_Y;
1329 else if(go->pos.y+25*SCALE > SCREEN_MAX_Y) go->pos.y = SCREEN_MAX_Y-25*SCALE;
1330 if(go->pos.x < SCREEN_MIN_X) go->pos.x = SCREEN_MIN_X;
1331 else if(go->pos.x+32*SCALE > SCREEN_MAX_X) go->pos.x = SCREEN_MAX_X-32*SCALE;
1332 vec2f center = get_sprite_center(spritemaps[go->spritemap_id]);
1333 center = vecadd(&center, &go->pos);
1334 if(is_wall(&center)) {
1335 go->pos = oldpos;
1336 go->vel = VEC(0,0);
1340 need_redraw = 1;
1342 if((go->objtype == OBJ_ENEMY_BOMBER || go->objtype == OBJ_ENEMY_SHOOTER) &&
1343 go->anim_curr == animations[go->animid].last &&
1344 (go->animid == ANIM_ENEMY_BOMBER_DIE ||
1345 go->animid == ANIM_ENEMY_GUNNER_DIE ||
1346 go->animid == ANIM_ENEMY_BURNT)) {
1347 dprintf(2, "removed enemy from %.2f,%.2f\n", go->pos.x, go->pos.y);
1348 gameobj_free(i);
1349 golist_remove(&go_enemies, i);
1350 need_redraw = 1;
1351 continue;
1356 if(remove_offscreen_objects(&go_enemies)) need_redraw = 1;
1357 if(remove_offscreen_objects(&go_vehicles)) need_redraw = 1;
1358 if(remove_offscreen_objects(&go_mines)) need_redraw = 1;
1359 if(remove_offscreen_objects(&go_turrets)) need_redraw = 1;
1360 if(remove_offscreen_objects(&go_bunkers)) need_redraw = 1;
1361 long ms_used = 0;
1362 struct timeval timer;
1363 gettimestamp(&timer);
1364 if(need_redraw) {
1365 draw_map();
1366 draw_gameobjs();
1367 draw_status_bar();
1368 video_update_region(SCREEN_MIN_X ,SCREEN_MIN_Y , SCREEN_MAX_X - SCREEN_MIN_X, VMODE_H);
1371 ms_used = mspassed(&timer);
1372 //if(ms_used) printf("repaint took: ms_used %ld\n", ms_used);
1373 int res = audio_process();
1374 if(res == -1) music_restart();
1375 ms_used = mspassed(&timer);
1376 //if(ms_used) printf("audio processed: %d, ms_used %ld\n", res, ms_used);
1378 long sleepms = 1000/fps - ms_used;
1379 if(sleepms >= 0) SDL_Delay(sleepms);
1380 if(mousebutton_down[MB_LEFT]) mousebutton_down[MB_LEFT]++;
1382 tickcounter++;
1383 update_caption();
1386 enum cursor {
1387 c_down = 0,
1388 c_up,
1389 c_left,
1390 c_right,
1393 static enum cursor cursor_lut[] = {
1394 [SDLK_UP] = c_up,
1395 [SDLK_DOWN] = c_down,
1396 [SDLK_LEFT] = c_left,
1397 [SDLK_RIGHT] = c_right,
1398 [SDLK_w] = c_up,
1399 [SDLK_a] = c_left,
1400 [SDLK_s] = c_down,
1401 [SDLK_d] = c_right,
1404 static char cursors_pressed[] = {
1405 [c_up] = 0,
1406 [c_down] = 0,
1407 [c_left] = 0,
1408 [c_right] = 0,
1411 static vec2f get_vel_from_direction(enum direction dir, float speed) {
1412 #define VELLUT(a, b, c) [a] = VEC(b, c)
1413 static const vec2f vel_lut[] = {
1414 VELLUT(DIR_O, 1, 0),
1415 VELLUT(DIR_NO, 0.7071067769704655, -0.7071067769704655),
1416 VELLUT(DIR_N, 0, -1),
1417 VELLUT(DIR_NW, -0.7071067769704655, -0.7071067769704655),
1418 VELLUT(DIR_W, -1, 0),
1419 VELLUT(DIR_SW, -0.7071067769704655, 0.7071067769704655),
1420 VELLUT(DIR_S, 0, 1),
1421 VELLUT(DIR_SO, 0.7071067769704655, 0.7071067769704655),
1423 #undef VELLUT
1424 vec2f v = vel_lut[dir];
1425 v.x *= speed * SCALE;
1426 v.y *= speed * SCALE;
1427 return v;
1430 static vec2f get_vel_from_direction16(enum direction16 dir, float speed) {
1431 #define VELLUT(a, b, c) [a] = VEC(b, c)
1432 #define ANK90 0.7071067769704655
1433 #define GK90 ANK90
1434 #define ANK45 0.9238795042037964
1435 #define GK45 0.3826834261417389
1436 static const vec2f vel_lut[] = {
1437 VELLUT(DIR16_O, 1, 0),
1438 VELLUT(DIR16_ONO, ANK45, -GK45),
1439 VELLUT(DIR16_NO, ANK90, -ANK90),
1440 VELLUT(DIR16_NNO, GK45, -ANK45),
1441 VELLUT(DIR16_N, 0, -1),
1442 VELLUT(DIR16_NNW, -GK45, -ANK45),
1443 VELLUT(DIR16_NW, -ANK90, -ANK90),
1444 VELLUT(DIR16_WNW, -ANK45, -GK45),
1445 VELLUT(DIR16_W, -1, 0),
1446 VELLUT(DIR16_WSW, -ANK45, GK45),
1447 VELLUT(DIR16_SW, -ANK90, ANK90),
1448 VELLUT(DIR16_SSW, -GK45, ANK45),
1449 VELLUT(DIR16_S, 0, 1),
1450 VELLUT(DIR16_SSO, GK45, ANK45),
1451 VELLUT(DIR16_SO, ANK90, ANK90),
1452 VELLUT(DIR16_OSO, ANK45, GK45),
1454 #undef VELLUT
1455 vec2f v = vel_lut[dir];
1456 v.x *= speed * SCALE;
1457 v.y *= speed * SCALE;
1458 return v;
1462 static enum direction get_direction_from_vec(vec2f *vel) {
1463 float deg_org, deg = atan2(vel->y, vel->x);
1464 deg_org = deg;
1465 if(deg < 0) deg *= -1.f;
1466 else if(deg > 0) deg = M_PI + (M_PI - deg); // normalize atan2 result to scale from 0 to 2 pi
1467 int hexadrant = (int)(deg / ((1.0/16.0f)*2*M_PI));
1468 assert(hexadrant >= 0 && hexadrant < 16);
1469 static const enum direction rad_lut[] = {
1470 DIR_O,
1471 DIR_NO, DIR_NO,
1472 DIR_N, DIR_N,
1473 DIR_NW, DIR_NW,
1474 DIR_W, DIR_W,
1475 DIR_SW, DIR_SW,
1476 DIR_S, DIR_S,
1477 DIR_SO, DIR_SO,
1478 DIR_O
1480 return rad_lut[hexadrant];
1483 static enum direction get_direction_from_cursor(void) {
1484 enum direction dir = DIR_INVALID;
1485 if(cursors_pressed[c_up]) {
1486 if(cursors_pressed[c_left]) dir = DIR_NW;
1487 else if(cursors_pressed[c_right]) dir = DIR_NO;
1488 else dir = DIR_N;
1489 } else if (cursors_pressed[c_down]) {
1490 if(cursors_pressed[c_left]) dir = DIR_SW;
1491 else if(cursors_pressed[c_right]) dir = DIR_SO;
1492 else dir = DIR_S;
1493 } else if (cursors_pressed[c_left]) {
1494 dir = DIR_W;
1495 } else if (cursors_pressed[c_right]) {
1496 dir = DIR_O;
1498 return dir;
1501 static enum animation_id get_anim_from_direction(enum direction dir, int player_no, int throwing) {
1502 #define DIRMAP(a, b) [a] = b
1503 if(!throwing) {
1504 static const enum animation_id dir_map_p1[] = {
1505 DIRMAP(DIR_N, ANIM_P1_MOVE_N),
1506 DIRMAP(DIR_NW, ANIM_P1_MOVE_NW),
1507 DIRMAP(DIR_W, ANIM_P1_MOVE_W),
1508 DIRMAP(DIR_SW, ANIM_P1_MOVE_SW),
1509 DIRMAP(DIR_S, ANIM_P1_MOVE_S),
1510 DIRMAP(DIR_SO, ANIM_P1_MOVE_SO),
1511 DIRMAP(DIR_O, ANIM_P1_MOVE_O),
1512 DIRMAP(DIR_NO, ANIM_P1_MOVE_NO),
1514 static const enum animation_id dir_map_p2[] = {
1515 DIRMAP(DIR_N, ANIM_P2_MOVE_N),
1516 DIRMAP(DIR_NW, ANIM_P2_MOVE_NW),
1517 DIRMAP(DIR_W, ANIM_P2_MOVE_W),
1518 DIRMAP(DIR_SW, ANIM_P2_MOVE_SW),
1519 DIRMAP(DIR_S, ANIM_P2_MOVE_S),
1520 DIRMAP(DIR_SO, ANIM_P2_MOVE_SO),
1521 DIRMAP(DIR_O, ANIM_P2_MOVE_O),
1522 DIRMAP(DIR_NO, ANIM_P2_MOVE_NO),
1524 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1 : dir_map_p2;
1525 return dir_map[dir];
1526 } else {
1527 static const enum animation_id dir_map_p1_g[] = {
1528 DIRMAP(DIR_N, ANIM_P1_THROW_N),
1529 DIRMAP(DIR_NW, ANIM_P1_THROW_NW),
1530 DIRMAP(DIR_W, ANIM_P1_THROW_W),
1531 DIRMAP(DIR_SW, ANIM_P1_THROW_SW),
1532 DIRMAP(DIR_S, ANIM_P1_THROW_S),
1533 DIRMAP(DIR_SO, ANIM_P1_THROW_SO),
1534 DIRMAP(DIR_O, ANIM_P1_THROW_O),
1535 DIRMAP(DIR_NO, ANIM_P1_THROW_NO),
1537 static const enum animation_id dir_map_p2_g[] = {
1538 DIRMAP(DIR_N, ANIM_P2_THROW_N),
1539 DIRMAP(DIR_NW, ANIM_P2_THROW_NW),
1540 DIRMAP(DIR_W, ANIM_P2_THROW_W),
1541 DIRMAP(DIR_SW, ANIM_P2_THROW_SW),
1542 DIRMAP(DIR_S, ANIM_P2_THROW_S),
1543 DIRMAP(DIR_SO, ANIM_P2_THROW_SO),
1544 DIRMAP(DIR_O, ANIM_P2_THROW_O),
1545 DIRMAP(DIR_NO, ANIM_P2_THROW_NO),
1547 const enum animation_id *dir_map = player_no == 0 ? dir_map_p1_g : dir_map_p2_g;
1548 return dir_map[dir];
1550 #undef DIRMAP
1553 #if 0
1554 static enum animation_id get_anim_from_cursor(void) {
1555 enum direction dir = get_direction_from_cursor();
1556 if(dir == DIR_INVALID) return ANIM_INVALID;
1557 return get_anim_from_direction(dir, 0, 0);
1559 /* playerno is either 0 or 1, not player_id! */
1560 static enum animation_id get_anim_from_vel(int player_no, vec2f *vel) {
1561 enum direction dir = get_direction_from_vec(vel);
1562 if(dir == DIR_INVALID) return ANIM_INVALID;
1563 return get_anim_from_direction(dir, player_no, 0);
1565 #endif
1567 static void switch_anim(int obj_id, int aid) {
1568 if(objs[obj_id].animid == aid) return;
1569 gameobj_start_anim(obj_id, aid);
1572 enum map_index choose_mission(void);
1573 //RcB: DEP "mission_select.c"
1574 #include "enemytag.c"
1575 int main() {
1576 video_init();
1577 clear_screen();
1578 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
1579 SDL_EnableKeyRepeat(100, 20);
1581 audio_init();
1583 /* background music for mission selection screen */
1584 music_play(TUNE_MAP);
1586 if((current_map = choose_mission()) == MI_INVALID) goto dun_goofed;
1588 music_play(TUNE_FIGHTING);
1590 SDL_ShowCursor(0);
1593 int startx = 10;
1594 int starty = 10;
1596 //redraw(surface, startx, starty);
1597 const float player_speed = 1.25f;
1598 const struct palpic* spritemap = spritemaps[SI_PLAYERS];
1599 struct { int *target; int dir; int max;} moves[] = {
1600 [c_up] = {&starty, SCALE * -1, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
1601 [c_down] = {&starty, SCALE, VMODE_H - (palpic_getspriteheight(spritemap) * SCALE)},
1602 [c_left] = {&startx, SCALE * -1, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
1603 [c_right] = {&startx, SCALE, VMODE_W - (palpic_getspritewidth(spritemap) * SCALE)},
1606 init_game_objs();
1607 int player_no = 0;
1608 int player_id = player_ids[player_no];
1610 game_tick(1);
1612 SDL_Event sdl_event;
1613 while(1) {
1614 unsigned need_redraw = 0;
1615 int weapon_inc = 0;
1616 while (SDL_PollEvent(&sdl_event)) {
1617 need_redraw = 0;
1618 switch (sdl_event.type) {
1619 case SDL_MOUSEMOTION:
1620 if((int)mousepos->x != sdl_event.motion.x || (int)mousepos->y != sdl_event.motion.y)
1621 need_redraw = 1;
1622 mousepos->x = sdl_event.motion.x;
1623 mousepos->y = sdl_event.motion.y;
1624 break;
1625 case SDL_MOUSEBUTTONDOWN:
1626 mousepos->x = sdl_event.button.x;
1627 mousepos->y = sdl_event.button.y;
1628 mousebutton_down[MB_LEFT] = 1;
1629 fire_bullet(player_no);
1630 break;
1631 case SDL_MOUSEBUTTONUP:
1632 mousepos->x = sdl_event.button.x;
1633 mousepos->y = sdl_event.button.y;
1634 mousebutton_down[MB_LEFT] = 0;
1635 break;
1636 case SDL_QUIT:
1637 dun_goofed:
1638 video_cleanup();
1639 return 0;
1640 case SDL_KEYDOWN:
1641 switch(sdl_event.key.keysym.sym) {
1642 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
1643 case SDLK_UP:
1644 case SDLK_DOWN:
1645 case SDLK_RIGHT:
1646 case SDLK_LEFT:
1647 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 1;
1648 check_anim: {
1649 enum direction dir = get_direction_from_cursor();
1650 if(dir != DIR_INVALID) {
1651 if(!mousebutton_down[MB_LEFT]) {
1652 // change animation according to cursors,
1653 // unless we're in automatic fire mode.
1654 enum animation_id aid = get_anim_from_direction(dir, player_no, 0);
1655 if(aid != ANIM_INVALID) switch_anim(player_id, aid);
1657 objs[player_id].vel = get_vel_from_direction(dir, player_speed);
1658 } else {
1659 objs[player_id].vel = VEC(0,0);
1662 break;
1663 case SDLK_RETURN:
1664 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
1665 (sdl_event.key.keysym.mod & KMOD_RALT)) {
1666 toggle_fullscreen();
1667 SDL_Delay(1);
1668 game_tick(1);
1669 need_redraw = 1;
1671 break;
1672 case SDLK_KP_PLUS:
1673 weapon_inc = 1;
1674 goto toggle_weapon;
1675 case SDLK_KP_MINUS:
1676 if((int)player_weapons[player_no][0] == 0)
1677 weapon_inc = WP_MAX-1;
1678 else weapon_inc = -1;
1679 toggle_weapon:
1680 player_weapons[player_no][0] += weapon_inc;
1681 if(player_weapons[player_no][0] == WP_INVALID)
1682 player_weapons[player_no][0] = 0;
1683 printf("%s\n", weapon_name(player_weapons[player_no][0]));
1684 need_redraw = 1;
1685 break;
1686 default:
1687 break;
1689 break;
1690 case SDL_KEYUP:
1691 switch(sdl_event.key.keysym.sym) {
1692 case SDLK_e:
1693 enemy_tag_loop();
1694 update_caption = game_update_caption;
1695 break;
1696 case SDLK_c:
1697 clear_screen();
1698 video_update();
1699 need_redraw = 1;
1700 break;
1701 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
1702 case SDLK_UP:
1703 case SDLK_DOWN:
1704 case SDLK_RIGHT:
1705 case SDLK_LEFT:
1706 cursors_pressed[cursor_lut[sdl_event.key.keysym.sym]] = 0;
1707 goto check_anim;
1708 case SDLK_ESCAPE:
1709 goto dun_goofed;
1710 default:
1711 break;
1713 default:
1714 break;
1717 unsigned i;
1718 for (i = 0; i < ARRAY_SIZE(cursors_pressed); i++) {
1719 if(cursors_pressed[i]) {
1720 *moves[i].target += moves[i].dir;
1721 if(*moves[i].target < 0) *moves[i].target = 0;
1722 if(*moves[i].target > moves[i].max) *moves[i].target = moves[i].max;
1723 need_redraw = 1;
1726 game_tick(need_redraw);
1729 return 0;