1 #include "../lib/include/timelib.h"
2 #include "../lib/include/macros.h"
3 #include "../lib/include/sblist.h"
13 #include "direction.h"
18 #include "muzzle_tab.h"
19 #include "spritemaps.h"
23 #include "mapsprites.h"
26 #include "spawnmaps.h"
30 #ifndef IN_KDEVELOP_PARSER
31 #include "../lib/include/bitarray.h"
32 #include "weapon_sprites.c"
41 // 1 if button down, 0 if not, >1 to count ms pressed
42 unsigned long mousebutton_down
[] = {
49 static void get_last_move_event(SDL_Event
* e
) {
51 SDL_Event peek
[numpeek
];
52 SDL_Event
* last_event
= NULL
;
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
];
64 SDL_PeepEvents(peek
, i
+ 1, SDL_GETEVENT
, (uint32_t) ~0);
70 static vec2f
get_sprite_center(const struct palpic
*p
) {
71 assert(p
->spritecount
);
73 res
.x
= palpic_getspritewidth(p
) * SCALE
/ 2;
74 res
.y
= palpic_getspriteheight(p
) * SCALE
/ 2;
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
);
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
) {
167 sblist_iter_counter2(l
, i
, itemid
) {
168 if(*itemid
== objid
) {
175 static int get_next_anim_frame(enum animation_id aid
, anim_step curr
) {
176 if(curr
== ANIM_STEP_INIT
) return animations
[aid
].first
;
178 if(curr
> animations
[aid
].last
) return animations
[aid
].first
;
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);
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);
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
];
232 mapscrolldir
= MS_UP
;
233 map_spawn_screen_index
= 0;
235 map_spawn_current
= 0;
238 static mapscreen_index
get_bonus_layer_index(mapscreen_index screen
) {
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
) {
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
];
270 static void draw_map() {
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
;
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);
292 assert(map
->screen_map
[mapsquare
.y
][mapsquare
.x
+x_screen_iter
] != MAPSCREEN_BLOCKED
);
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
))
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;
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
++;
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);
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
]);
408 map_spawn_line
+= scrollstep
;
411 static int scroll_map() {
414 if(scroll_needed()) {
415 if(mapscrolldir
== MS_UP
) {
416 mapscreen_yoff
-= scroll_step
;
417 if(mapscreen_yoff
< 0) {
419 if(map
->screen_map
[mapsquare
.y
][mapsquare
.x
] == MAPSCREEN_BLOCKED
) {
420 scroll_step
= -mapscreen_yoff
;
423 scroll_gameobjs(scroll_step
);
424 if(map
->screen_map
[mapsquare
.y
][mapsquare
.x
+1] == MAPSCREEN_BLOCKED
) {
425 mapscrolldir
= MS_LEFT
;
427 mapscrolldir
= MS_RIGHT
;
433 mapscreen_yoff
+= 192;
437 handle_spawns(scroll_step
);
438 scroll_gameobjs(scroll_step
);
440 } else if(mapscrolldir
== MS_LEFT
) {
441 mapscreen_xoff
-= scroll_step
;
442 if(mapscreen_xoff
< 0) {
444 if(map
->screen_map
[mapsquare
.y
][mapsquare
.x
] == MAPSCREEN_BLOCKED
) {
445 scroll_step
= -mapscreen_xoff
;
449 scroll_gameobjs(scroll_step
);
450 mapscrolldir
= MS_UP
;
453 mapscreen_xoff
+= 192;
458 } else if(mapscrolldir
== MS_RIGHT
) {
459 mapscreen_xoff
+= scroll_step
;
460 if(mapscreen_xoff
>= 192) {
462 if(map
->screen_map
[mapsquare
.y
][mapsquare
.x
+1] == MAPSCREEN_BLOCKED
) {
463 scroll_step
= mapscreen_xoff
- 192;
466 scroll_gameobjs(scroll_step
);
467 mapscrolldir
= MS_UP
;
471 mapscreen_xoff
-= 192;
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;
492 for(; i
< AMMO_MAX
; i
++)
493 player_ammo
[player_no
][i
] = 50000;
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
;
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);
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
);
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
);
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
);
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
]);
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
));
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;
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
);
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 },
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
,
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
);
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;
631 if(spawn
->route
[i
].shape
!= ES_INVALID
&&
632 curr_step
>= spawn
->route
[i
].start_step
)
633 return &spawn
->route
[i
];
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
[] = {
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
;
739 enum animation_id animid
;
741 spriteid
= enemy_soldier_sprite_lut
[map
->enemy_type
];
742 objid
= enemy_soldier_objtype_lut
[spawn
->weapon
];
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
;
757 case OBJ_BUNKER1
: case OBJ_BUNKER2
: case OBJ_BUNKER3
:
758 case OBJ_BUNKER4
: case OBJ_BUNKER5
:
761 case OBJ_GUNTURRET_MOVABLE_MACHINE
:
762 case OBJ_GUNTURRET_MOVABLE_MAN
:
763 case OBJ_FLAMETURRET
: case OBJ_GUNTURRET_FIXED_NORTH
:
764 case OBJ_GUNTURRET_FIXED_SOUTH
:
767 case OBJ_MINE_CROSSED
: case OBJ_MINE_FLAT
:
770 case OBJ_JEEP
: case OBJ_TRANSPORTER
:
771 case OBJ_TANK_BIG
: case OBJ_TANK_SMALL
:
780 static void remove_enemy(int id
) {
781 enum objtype objid
= objs
[id
].objtype
;
783 case OBJ_JEEP
: case OBJ_TRANSPORTER
:
784 case OBJ_TANK_BIG
: case OBJ_TANK_SMALL
:
785 golist_remove(&go_vehicles
, id
);
788 golist_remove(&go_enemies
, id
);
793 static int enemy_fires(struct enemy
*e
) {
795 for(i
= 0; i
< ENEMY_MAX_SHOT
; i
++)
796 if(e
->curr_step
== e
->spawn
->shots
[i
]) return 1;
800 static enum animation_id
get_flash_animation_from_direction(enum direction dir
) {
801 #define ANIMF(dir, anim) [dir] = anim
802 static const enum animation_id dir_to_anim
[] = {
803 ANIMF(DIR_O
, ANIM_FLASH_O
),
804 ANIMF(DIR_NO
, ANIM_FLASH_NO
),
805 ANIMF(DIR_N
, ANIM_FLASH_N
),
806 ANIMF(DIR_NW
, ANIM_FLASH_NW
),
807 ANIMF(DIR_W
, ANIM_FLASH_W
),
808 ANIMF(DIR_SW
, ANIM_FLASH_SW
),
809 ANIMF(DIR_S
, ANIM_FLASH_S
),
810 ANIMF(DIR_SO
, ANIM_FLASH_SO
),
813 return dir_to_anim
[dir
];
816 static int init_flash(vec2f
*pos
, enum direction dir
) {
817 int id
= gameobj_alloc();
818 gameobj_init(id
, pos
, &VEC(0, 0), SI_FLASH
, get_flash_animation_from_direction(dir
), OBJ_FLASH
);
819 gameobj_init_bulletdata(id
, 2);
823 static vec2f
get_gameobj_pos(int obj_id
) {
824 return objs
[obj_id
].pos
;
827 static vec2f
get_gameobj_center(int obj_id
) {
828 vec2f res
= objs
[obj_id
].pos
;
829 vec2f add
= get_sprite_center(spritemaps
[objs
[obj_id
].spritemap_id
]);
830 return vecadd(&res
, &add
);
833 static enum direction
get_direction_from_vec(vec2f
*vel
);
834 static enum animation_id
get_anim_from_direction(enum direction dir
, int player
, int throwing
);
836 static enum direction16
get_direction16_from_direction(enum direction dir
) {
837 static const enum direction16 dir16_transl_tab
[] = {
838 [DIR_N
] = DIR16_N
, [DIR_NO
] = DIR16_NO
, [DIR_O
] = DIR16_O
, [DIR_SO
] = DIR16_SO
,
839 [DIR_S
] = DIR16_S
, [DIR_SW
] = DIR16_SW
, [DIR_W
] = DIR16_W
, [DIR_NW
] = DIR16_NW
,
841 assert(dir
!= DIR_INVALID
);
842 return dir16_transl_tab
[dir
];
845 static enum weapon_id
get_active_weapon_id(int player_no
) {
846 return player_weapons
[player_no
][weapon_active
[player_no
]];
849 static const struct weapon
* get_active_weapon(int player_no
) {
850 return &weapons
[get_active_weapon_id(player_no
)];
852 static enum direction16
get_shotdirection_from_enemy(int curr_step
, const struct enemy_spawn
*spawn
) {
853 const struct enemy_route
* r
= get_enemy_current_route(curr_step
, spawn
);
855 case ES_SOLDIER1_DOWN
: case ES_SOLDIER2_DOWN
:
857 case ES_SOLDIER1_LEFT
: case ES_SOLDIER2_LEFT
:
859 case ES_SOLDIER1_RIGHT
: case ES_SOLDIER2_RIGHT
:
866 static void enemy_fire_bullet(enum direction16 dir
, int steps
, enum enemy_weapon wpn
, vec2f
*pos
) {
867 vec2f vel
= get_vel_from_direction16(dir
, 1.75);
870 id
= init_bullet(pos
, &vel
, steps
);
871 if(id
!= -1) add_ebullet(id
);
872 } else if (wpn
== EW_GRENADE
) {
873 id
= init_grenade(pos
, &vel
, steps
);
874 if(id
!= -1) add_enemy_grenade(id
);
876 id
= init_flame(pos
, &vel
, steps
);
877 if(id
!= -1) add_enemy_flame(id
);
881 static int get_crosshair_id(void) {
882 assert(sblist_getsize(&go_crosshair
));
883 uint8_t *id
= sblist_get(&go_crosshair
, 0);
887 static void fire_bullet(int player_no
) {
889 const struct weapon
*pw
= get_active_weapon(player_no
);
890 if(player_ammo
[player_no
][pw
->ammo
] == 0) return;
891 vec2f from
= get_gameobj_center(player_ids
[player_no
]);
892 //get_anim_from_vel(0, objs[player].
893 vec2f to
= get_gameobj_center(get_crosshair_id());
894 to
.x
+= 4*SCALE
- rand()%8*SCALE
;
895 to
.y
+= 4*SCALE
- rand()%8*SCALE
;
896 vec2f vel
= velocity(&from
, &to
);
897 enum direction dir
= get_direction_from_vec(&vel
);
898 if(dir
!= DIR_INVALID
) {
899 enum animation_id aid
= get_anim_from_direction(dir
, player_no
, pw
->ammo
== AMMO_GRENADE
);
900 if(aid
!= ANIM_INVALID
) switch_anim(player_ids
[player_no
], aid
);
901 anim_step curranim
= objs
[player_ids
[player_no
]].anim_curr
;
902 if(curranim
== ANIM_STEP_INIT
) curranim
= get_next_anim_frame(objs
[player_ids
[player_no
]].animid
, ANIM_STEP_INIT
);
903 vec2f muzzle
= muzzle_tab
[curranim
];
905 from
= get_gameobj_pos(player_ids
[player_no
]);
906 from
.x
+= muzzle
.x
* SCALE
;
907 from
.y
+= muzzle
.y
* SCALE
;
909 if(pw
->flags
& WF_MUZZLEFLASH
) {
910 static const vec2f flash_start
[] = {
911 [DIR_O
] = { 0.0, 1.0 },
912 [DIR_NO
] = { 0.5, 6.0 },
913 [DIR_N
] = { 1.0, 7.5 },
914 [DIR_NW
] = { 6.0, 6.0 },
915 [DIR_W
] = { 7.5, 1.0 },
916 [DIR_SW
] = { 4.5, 0.0 },
917 [DIR_S
] = { 1.0, 0.0 },
918 [DIR_SO
] = { 0.0, 0.0 },
921 ffrom
.x
-= flash_start
[dir
].x
* SCALE
;
922 ffrom
.y
-= flash_start
[dir
].y
* SCALE
;
923 id
= init_flash(&ffrom
, dir
);
924 if(id
!= -1) add_muzzleflash(id
);
926 vel
= velocity(&from
, &to
);
928 float dist
= veclength(&vel
);
929 float speed
= pw
->bullet_speed
* SCALE
;
930 const uint16_t range_tab
[] = {0, 80, 66, 80, 118, 118, 118, 118, 118, 118,
931 200, 200, 240, 240, 240, 240, 240, 240, 240, 240, 320 };
932 float range
= range_tab
[pw
->range
] * SCALE
;
935 float steps
= dist
/ speed
;
936 float deg
= atan2(vel
.y
, vel
.x
);
937 vel
.x
= cos(deg
) * speed
;
938 vel
.y
= sin(deg
) * speed
;
941 id
= init_rocket(dir
, &from
, &vel
, steps
);
944 id
= init_bullet(&from
, &vel
, steps
);
945 if(id
!= -1) add_pbullet(id
);
948 id
= init_player_flame(dir
, &from
, &vel
, steps
);
949 if(id
!= -1) add_flame(id
);
952 id
= init_grenade(&from
, &vel
, steps
);
958 player_ammo
[player_no
][pw
->ammo
]--;
959 const WAVE_HEADER_COMPLETE
*wf
= wavesounds
[pw
->sound
];
960 if(id
!= -1 && pw
->sound
!= WS_NONE
)
961 audio_play_wave_resource(wf
);
964 static void init_game_objs() {
965 sblist_init(&go_crosshair
, 1, 4);
966 sblist_init(&go_players
, 1, 4);
967 sblist_init(&go_muzzleflash
, 1, 4);
968 sblist_init(&go_player_bullets
, 1, 32);
969 sblist_init(&go_flames
, 1, 32);
970 sblist_init(&go_enemy_bullets
, 1, 32);
971 sblist_init(&go_explosions
, 1, 16);
972 sblist_init(&go_enemy_explosions
, 1, 16);
973 sblist_init(&go_grenades
, 1, 16);
974 sblist_init(&go_enemy_grenades
, 1, 16);
975 sblist_init(&go_rockets
, 1, 8);
976 sblist_init(&go_enemies
, 1, 32);
977 sblist_init(&go_vehicles
, 1, 4);
978 sblist_init(&go_mines
, 1, 4);
979 sblist_init(&go_turrets
, 1, 8);
980 sblist_init(&go_bunkers
, 1, 4);
981 sblist_init(&go_boss
, 1, 4);
982 sblist_init(&go_blood
, 1, 16);
983 sblist_init(&go_enemy_flames
, 1, 16);
985 add_crosshair(init_crosshair());
986 init_map(current_map
);
989 static int point_in_mask(vec2f
*point
, int obj_id
) {
990 vec2f pos_in_pic
= VEC((point
->x
- objs
[obj_id
].pos
.x
) / SCALE
,
991 (point
->y
- objs
[obj_id
].pos
.y
) / SCALE
);
992 const struct palpic
*p
= spritemaps
[objs
[obj_id
].spritemap_id
];
993 unsigned h
= palpic_getspriteheight(p
), w
= palpic_getspritewidth(p
);
994 if(pos_in_pic
.x
< 0 || pos_in_pic
.y
< 0 || pos_in_pic
.x
> w
|| pos_in_pic
.y
> h
) return 0;
995 assert(objs
[obj_id
].anim_curr
!= ANIM_STEP_INIT
);
996 anim_step curranim
= objs
[obj_id
].anim_curr
;
997 if(curranim
== ANIM_STEP_INIT
) curranim
= get_next_anim_frame(objs
[obj_id
].animid
, ANIM_STEP_INIT
);
998 uint8_t *data
= palpic_getspritedata(p
, curranim
);
999 if(data
[(unsigned) pos_in_pic
.y
* w
+ (unsigned) pos_in_pic
.x
] != 0) return 1;
1003 static enum animation_id
get_die_anim(unsigned id
) {
1004 switch(objs
[id
].objtype
) {
1010 return ANIM_JEEP_DESTROYED
;
1011 case OBJ_TANK_SMALL
:
1012 return ANIM_TANK_SMALL_DESTROYED
;
1014 return ANIM_TANK_BIG_DESTROYED
;
1015 case OBJ_TRANSPORTER
:
1016 return ANIM_TRANSPORTER_DESTROYED
;
1017 case OBJ_ENEMY_BOMBER
:
1018 return ANIM_ENEMY_BOMBER_DIE
;
1019 case OBJ_ENEMY_SHOOTER
:
1020 return ANIM_ENEMY_GUNNER_DIE
;
1021 case OBJ_BUNKER1
: case OBJ_BUNKER2
: case OBJ_BUNKER3
:
1022 case OBJ_BUNKER4
: case OBJ_BUNKER5
:
1023 return ANIM_BUNKER_DESTROYED
;
1024 case OBJ_GUNTURRET_MOVABLE_MACHINE
:
1025 return ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED
;
1026 case OBJ_GUNTURRET_MOVABLE_MAN
:
1027 return ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED
;
1029 return ANIM_INVALID
;
1033 /* remove bullets that have reached their maximum number of steps */
1034 static int remove_bullets(sblist
*list
) {
1038 sblist_iter_counter2s(list
, li
, item_id
) {
1039 struct gameobj
*bullet
= &objs
[*item_id
];
1040 if(bullet
->objspecific
.bullet
.step_curr
>= bullet
->objspecific
.bullet
.step_max
) {
1041 gameobj_free(*item_id
);
1042 sblist_delete(list
, li
);
1045 bullet
->objspecific
.bullet
.step_curr
++;
1052 static int remove_explosives(sblist
*list
) {
1056 sblist_iter_counter2s(list
, li
, item_id
) {
1057 struct gameobj
*go
= &objs
[*item_id
];
1058 if(go
->objspecific
.bullet
.step_curr
>= go
->objspecific
.bullet
.step_max
) {
1059 if(go
->objtype
== OBJ_GRENADE
) init_grenade_explosion(&go
->pos
, list
== &go_enemy_grenades
);
1060 else init_rocket_explosion(&go
->pos
);
1061 gameobj_free(*item_id
);
1062 sblist_delete(list
, li
);
1065 go
->objspecific
.bullet
.step_curr
++;
1066 if(go
->objtype
== OBJ_GRENADE
) {
1067 if(go
->objspecific
.bullet
.step_curr
>= 32) go
->animid
= ANIM_GRENADE_SMALL
;
1068 else if(go
->objspecific
.bullet
.step_curr
>= 8) go
->animid
= ANIM_GRENADE_BIG
;
1076 static int remove_offscreen_objects(sblist
*list
) {
1080 sblist_iter_counter2s(list
, li
, item_id
) {
1081 assert(obj_slot_used
[*item_id
]);
1082 struct gameobj
*go
= &objs
[*item_id
];
1083 assert((int) go
->spritemap_id
< SI_MAX
);
1084 const struct palpic
*p
= spritemaps
[go
->spritemap_id
];
1085 assert(p
->spritecount
);
1086 int h
= palpic_getspriteheight(p
), w
= palpic_getspritewidth(p
);
1087 if(go
->pos
.x
< SCREEN_MIN_X
-w
*SCALE
|| go
->pos
.x
> SCREEN_MAX_X
||
1088 go
->pos
.y
< SCREEN_MIN_Y
-h
*SCALE
|| go
->pos
.y
> SCREEN_MAX_Y
) {
1090 dprintf(2, "offscreen: removed gameobj %d\n", (int) *item_id
);
1091 gameobj_free(*item_id
);
1092 sblist_delete(list
, li
);
1099 static int get_sprite_number(int id
) {
1100 struct gameobj
*o
= &objs
[id
];
1101 return o
->anim_curr
== ANIM_STEP_INIT
? get_next_anim_frame(o
->animid
, o
->anim_curr
) : o
->anim_curr
;
1104 static int is_death_anim(enum animation_id anim
) {
1106 case ANIM_ENEMY_BOMBER_DIE
:
1107 case ANIM_ENEMY_GUNNER_DIE
:
1108 case ANIM_ENEMY_BURNT
:
1111 case ANIM_GUNTURRET_MOVABLE_MAN_DESTROYED
:
1112 case ANIM_GUNTURRET_MOVABLE_MACHINE_DESTROYED
:
1113 case ANIM_BUNKER_DESTROYED
:
1114 case ANIM_JEEP_DESTROYED
:
1115 case ANIM_TANK_BIG_DESTROYED
:
1116 case ANIM_TANK_SMALL_DESTROYED
:
1117 case ANIM_TRANSPORTER_DESTROYED
:
1124 static int overlap(int id1
, int id2
) {
1125 struct gameobj
*o1
= &objs
[id1
];
1126 struct gameobj
*o2
= &objs
[id2
];
1127 const struct palpic
* p1
= spritemaps
[o1
->spritemap_id
];
1128 const struct palpic
* p2
= spritemaps
[o2
->spritemap_id
];
1130 vec2f vec
= velocity(&o1
->pos
, &o2
->pos
);
1131 float mh
= o1
->pos
.y
< o2
->pos
.y
? palpic_getspriteheight(p1
) : palpic_getspriteheight(p2
);
1132 float mw
= o1
->pos
.x
< o2
->pos
.x
? palpic_getspritewidth(p1
) : palpic_getspritewidth(p2
);
1133 mh
*=SCALE
, mw
*=SCALE
;
1134 return fabs(vec
.x
) < mw
&& fabs(vec
.y
) < mh
;
1137 static int masks_collide(int id1
, int id2
) {
1138 struct gameobj
*a
= &objs
[id1
];
1139 struct gameobj
*b
= &objs
[id2
];
1140 const struct palpic
* pa
= spritemaps
[a
->spritemap_id
];
1141 const struct palpic
* pb
= spritemaps
[b
->spritemap_id
];
1142 int ax
= a
->pos
.x
/SCALE
, ay
= a
->pos
.y
/SCALE
,
1143 aw
= palpic_getspritewidth(pa
), ah
= palpic_getspriteheight(pa
);
1144 int bx
= b
->pos
.x
/SCALE
, by
= b
->pos
.y
/SCALE
,
1145 bw
= palpic_getspritewidth(pb
), bh
= palpic_getspriteheight(pb
);
1147 /* x: normalized start position */
1148 int xx
= MIN(ax
, bx
), xy
= MIN(ay
, by
);
1149 /* normalize coordinates to 0 */
1150 ax
-= xx
, ay
-= xy
, bx
-= xx
, by
-= xy
;
1151 /* o: upper left point of the overlapping region */
1152 int ox
= MAX(ax
, bx
), oy
= MAX(ay
, by
);
1153 /* e: lower right point of the overlapping region */
1154 int ex
= MIN(ax
+aw
, bx
+bw
), ey
= MIN(ay
+ah
, by
+bh
);
1156 int oex
= ex
- ox
, oey
= ey
- oy
;
1157 /* (s)tart and (e)nd coordinates of the overlapping regions in both rects */
1158 int asx
= ox
- ax
, asy
= oy
- ay
;
1159 int bsx
= ox
- bx
, bsy
= oy
- by
;
1160 int aex
= asx
+ oex
, bex
= bsx
+ oex
;
1161 int aey
= asy
+ oey
, bey
= bsy
+ oey
;
1162 assert(asx
>= 0); assert(asy
>= 0);
1163 assert(bsx
>= 0); assert(bsy
>= 0);
1164 if(!aex
|| !bex
|| !aey
|| !bey
) return 0;
1165 assert(aex
> 0); assert(bex
> 0);
1166 assert(aey
> 0); assert(bey
> 0);
1168 const uint8_t *da
= palpic_getspritedata(pa
, get_sprite_number(id1
)),
1169 *db
= palpic_getspritedata(pb
, get_sprite_number(id2
));
1170 assert(da
); assert(db
);
1171 for(ya
= asy
, yb
= bsy
; ya
< aey
&& yb
< bey
; ya
++, yb
++)
1172 for(xa
= asx
, xb
= bsx
; xa
< aex
&& xb
< bex
; xa
++, xb
++)
1173 if(da
[ya
*aw
+xa
] && db
[yb
*bw
+xb
]) return 1;
1178 // removes bullets and other objects if they collide. return 1 if anything happened
1179 static int hit_bullets(sblist
*bullet_list
, sblist
*target_list
) {
1183 enum bulletsubtype
{
1186 BS_GRENADE_EXPL
= 2,
1189 } bullet_subtybe
= BS_BULLET
;
1191 sblist_iter_counter2s(bullet_list
, li
, bullet_id
) {
1192 struct gameobj
*bullet
= &objs
[*bullet_id
];
1193 if(bullet
->objtype
== OBJ_FLAME
) bullet_subtybe
= BS_FLAME
;
1194 else if(bullet
->objtype
== OBJ_GRENADE_EXPLOSION
) {
1195 /* grenade kills only in the explosion, not in the smoke phase */
1196 if(bullet
->objspecific
.bullet
.step_curr
> 22) continue;
1197 bullet_subtybe
= BS_GRENADE_EXPL
;
1198 } else if(bullet
->objtype
== OBJ_BIG_EXPLOSION
) {
1199 bullet_subtybe
= BS_BIG_EXPL
;
1200 } else if(bullet_list
== &go_vehicles
|| bullet_list
== &go_enemies
||
1201 bullet_list
== &go_boss
|| bullet_list
== &go_mines
) {
1202 bullet_subtybe
= BS_TOUCH
;
1205 vec2f bullet_center
= get_gameobj_center(*bullet_id
);
1206 if(bullet_list
== &go_player_bullets
|| bullet_list
== &go_flames
) {
1207 if(is_wall(&bullet_center
) == WT_SOLID
) goto remove_bullet
;
1210 const float bullet_radius
[] = { [BS_BULLET
] = 1.f
, [BS_FLAME
] = 6.f
,
1211 [BS_GRENADE_EXPL
] = 16.f
, [BS_BIG_EXPL
] = 19.f
, [BS_TOUCH
] = 8.f
, };
1215 sblist_iter_counter2(target_list
, lj
, target_id
) {
1216 struct gameobj
*target
= &objs
[*target_id
];
1217 if(is_death_anim(target
->animid
) && target_list
!= &go_vehicles
) continue;
1218 if(bullet_subtybe
== BS_TOUCH
&&
1219 overlap(*target_id
, *bullet_id
) &&
1220 masks_collide(*target_id
, *bullet_id
)) {
1221 dprintf(2, "hit2\n");
1222 if(bullet_list
== &go_boss
) return 2;
1226 vec2f temp
= get_gameobj_center(*target_id
);
1227 float dist1
= vecdist(&bullet_center
, &temp
) - bullet_radius
[bullet_subtybe
] * SCALE
;
1228 vec2f newpos
= vecadd(&bullet_center
, &bullet
->vel
);
1229 float dist2
= vecdist(&newpos
, &temp
) - bullet_radius
[bullet_subtybe
] * SCALE
;
1231 unsigned w
= palpic_getspritewidth(spritemaps
[target
->spritemap_id
]),
1232 h
= palpic_getspriteheight(spritemaps
[target
->spritemap_id
]);
1233 float longest_side_div2
= ((float) MAX(h
, w
) / 2) * SCALE
;
1234 if(dist1
< 1.f
*SCALE
|| dist2
< 1.f
*SCALE
) { dprintf(2, "hit1\n"); goto hit
; }
1235 if(dist1
< longest_side_div2
|| dist2
< longest_side_div2
) {
1236 vec2f velquarter
= VEC(bullet
->vel
.x
* 0.25, bullet
->vel
.y
* 0.25);
1237 vec2f point
= bullet_center
;
1239 for(k
= 0; k
< 4; k
++) {
1240 if(point_in_mask(&point
, *target_id
)) {
1243 if(bullet_list
== &go_player_bullets
) {
1244 if(target_list
== &go_vehicles
) {
1245 audio_play_wave_resource(wavesounds
[WS_DROPSHOT
]);
1248 objs
[player_ids
[0]].objspecific
.playerdata
.score
+= 50;
1249 } else if (bullet_list
== &go_rockets
) {
1250 init_rocket_explosion(&target
->pos
);
1252 } else if(bullet
->objtype
== OBJ_GRENADE_EXPLOSION
) {
1253 // grenade explosion has no effect on vehicles and bunkers.
1254 if(target_list
== &go_vehicles
|| target_list
== &go_bunkers
)
1256 } else if(bullet
->objtype
== OBJ_MINE_CROSSED
|| bullet
->objtype
== OBJ_MINE_FLAT
) {
1257 init_big_explosion(&target
->pos
);
1260 enum animation_id death_anim
= (bullet_subtybe
== BS_FLAME
&& target_list
== &go_enemies
) ?
1261 ANIM_ENEMY_BURNT
: get_die_anim(*target_id
);
1262 if(death_anim
== ANIM_INVALID
) {
1263 gameobj_free(*target_id
);
1264 sblist_delete(target_list
, lj
);
1268 switch_anim(*target_id
, death_anim
);
1269 target
->vel
= VEC(0,0);
1270 if(target
->objtype
== OBJ_ENEMY_BOMBER
|| target
->objtype
== OBJ_ENEMY_SHOOTER
) {
1271 const enum wavesound_id wid
[] = { WS_SCREAM
, WS_SCREAM2
};
1272 audio_play_wave_resource(wavesounds
[wid
[rand()%2]]);
1273 if(bullet_subtybe
== BS_BULLET
) {
1274 vec2f bloodpos
= vecadd(&target
->pos
, &VEC(0, (2+rand()%7)*SCALE
));
1275 /* original displays at 0,8 but i prefer a random effect */
1276 //vec2f bloodpos = vecadd(&target->pos, &VEC(0, 8*SCALE));
1277 int id
= init_blood(&bloodpos
);
1278 if(id
!= -1) add_blood(id
);
1281 if(bullet_subtybe
== BS_BULLET
) {
1283 gameobj_free(*bullet_id
);
1284 sblist_delete(bullet_list
, li
);
1289 point
= vecadd(&point
, &velquarter
);
1300 uint32_t tickcounter
;
1302 static int advance_animations(void) {
1303 size_t i
, obj_visited
;
1305 for(i
= 0, obj_visited
= 0; obj_visited
< obj_count
&& i
< OBJ_MAX
; i
++) {
1306 if(!obj_slot_used
[i
]) continue;
1307 struct gameobj
*go
= &objs
[i
];
1308 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
)) {
1309 unsigned anim_delay
= go
->objtype
== OBJ_BIG_EXPLOSION
? 8 : 4;
1310 if(go
->anim_curr
== ANIM_STEP_INIT
|| tickcounter
% anim_delay
== go
->anim_frame
) {
1311 anim_step last_anim
= go
->anim_curr
;
1312 go
->anim_curr
= get_next_anim_frame(go
->animid
, go
->anim_curr
);
1313 if(last_anim
!= go
->anim_curr
) res
= 1;
1321 static void switch_enemy_shape(int objid
, const struct enemy_route
* r
) {
1322 switch_anim(objid
, enemy_animation_lut
[r
->shape
]);
1325 static void draw_golist(sblist
*list
) {
1328 sblist_iter_counter2(list
, i
, itemid
) {
1329 assert(obj_slot_used
[*itemid
]);
1330 struct gameobj
*o
= &objs
[*itemid
];
1331 const prgb
*palette
;
1332 switch(o
->objtype
) {
1334 // original blood color is bb5511 but that is hardly visible
1335 // palette = (prgb[]) {PRGB(0,0,0), PRGB(0xbb, 0x55, 0x11)};
1336 palette
= (const prgb
[]) {PRGB(0,0,0), PRGB(0xff, 0x0, 0x0)};
1338 case OBJ_ENEMY_BOMBER
: case OBJ_ENEMY_SHOOTER
:
1339 palette
= map
->enemy_palette
;
1344 blit_sprite(o
->pos
.x
, o
->pos
.y
, &video
,
1345 SCALE
, spritemaps
[o
->spritemap_id
],
1346 get_sprite_number(*itemid
),
1351 static void draw_gameobjs(void) {
1352 draw_golist(&go_mines
);
1353 draw_golist(&go_turrets
);
1354 draw_golist(&go_bunkers
);
1355 draw_golist(&go_vehicles
);
1356 draw_golist(&go_enemies
);
1357 draw_golist(&go_blood
);
1358 draw_golist(&go_enemy_bullets
);
1359 draw_golist(&go_boss
);
1360 draw_golist(&go_rockets
);
1361 draw_golist(&go_players
);
1362 draw_golist(&go_player_bullets
);
1363 draw_golist(&go_enemy_explosions
);
1364 draw_golist(&go_flames
);
1365 draw_golist(&go_enemy_flames
);
1366 draw_golist(&go_explosions
);
1367 draw_golist(&go_grenades
);
1368 draw_golist(&go_enemy_grenades
);
1369 draw_golist(&go_crosshair
);
1370 draw_golist(&go_muzzleflash
);
1373 static void process_soldiers(void) {
1375 sblist_iter(&go_enemies
, itemid
) {
1376 struct gameobj
*go
= &objs
[*itemid
];
1377 assert(go
->objtype
== OBJ_ENEMY_SHOOTER
|| go
->objtype
== OBJ_ENEMY_BOMBER
);
1378 if (tickcounter
% 4 == go
->anim_frame
) {
1379 const struct enemy_route
*rc
= get_enemy_current_route(go
->objspecific
.enemy
.curr_step
, go
->objspecific
.enemy
.spawn
);
1381 go
->objspecific
.enemy
.curr_step
++;
1382 if(enemy_fires(&go
->objspecific
.enemy
)) {
1383 vec2f from
= get_gameobj_center(*itemid
);
1384 enum direction16 dir
= get_shotdirection_from_enemy(go
->objspecific
.enemy
.curr_step
, go
->objspecific
.enemy
.spawn
);
1385 enemy_fire_bullet(dir
, 41, go
->objspecific
.enemy
.spawn
->weapon
, &from
);
1387 const struct enemy_route
*rn
= get_enemy_current_route(go
->objspecific
.enemy
.curr_step
, go
->objspecific
.enemy
.spawn
);
1388 if(rn
->shape
!= rc
->shape
) switch_enemy_shape(*itemid
, rn
);
1391 if(!is_death_anim(go
->animid
)) go
->vel
= get_enemy_vel(go
->objspecific
.enemy
.curr_step
, go
->objspecific
.enemy
.spawn
);
1392 else go
->vel
= VEC(0, 0);
1396 static enum animation_id
get_turret_anim_from_dir(int objid
, enum direction dir
) {
1397 static const enum animation_id man_anim
[] = {
1398 [DIR_N
] = ANIM_GUNTURRET_MOVABLE_MAN_N
,
1399 [DIR_NO
] = ANIM_GUNTURRET_MOVABLE_MAN_NO
,
1400 [DIR_O
] = ANIM_GUNTURRET_MOVABLE_MAN_O
,
1401 [DIR_SO
] = ANIM_GUNTURRET_MOVABLE_MAN_SO
,
1402 [DIR_S
] = ANIM_GUNTURRET_MOVABLE_MAN_S
,
1403 [DIR_SW
] = ANIM_GUNTURRET_MOVABLE_MAN_SW
,
1404 [DIR_W
] = ANIM_GUNTURRET_MOVABLE_MAN_W
,
1405 [DIR_NW
] = ANIM_GUNTURRET_MOVABLE_MAN_NW
,
1407 static const enum animation_id mach_anim
[] = {
1408 [DIR_N
] = ANIM_GUNTURRET_MOVABLE_MACHINE_N
,
1409 [DIR_NO
] = ANIM_GUNTURRET_MOVABLE_MACHINE_NO
,
1410 [DIR_O
] = ANIM_GUNTURRET_MOVABLE_MACHINE_O
,
1411 [DIR_SO
] = ANIM_GUNTURRET_MOVABLE_MACHINE_SO
,
1412 [DIR_S
] = ANIM_GUNTURRET_MOVABLE_MACHINE_S
,
1413 [DIR_SW
] = ANIM_GUNTURRET_MOVABLE_MACHINE_SW
,
1414 [DIR_W
] = ANIM_GUNTURRET_MOVABLE_MACHINE_W
,
1415 [DIR_NW
] = ANIM_GUNTURRET_MOVABLE_MACHINE_NW
,
1417 const enum animation_id
*lut
= objs
[objid
].objtype
== OBJ_GUNTURRET_MOVABLE_MAN
? man_anim
: mach_anim
;
1421 static int process_turrets(sblist
* list
) {
1424 sblist_iter(list
, itemid
) {
1425 struct gameobj
*go
= &objs
[*itemid
];
1426 if(is_death_anim(go
->animid
)) continue;
1427 enum direction16 dir
= DIR16_S
;
1428 enum enemy_weapon ew
= EW_GUN
;
1431 switch(go
->objtype
) {
1432 case OBJ_GUNTURRET_FIXED_NORTH
:
1434 case OBJ_GUNTURRET_FIXED_SOUTH
:
1435 from
= get_gameobj_center(*itemid
);
1439 enemy_fire_bullet(dir
, steps
, ew
, &from
);
1443 case OBJ_FLAMETURRET
:
1447 if(objs
[player_ids
[0]].pos
.y
<= (192-25)*SCALE
/2) dir
= DIR16_N
;
1449 from
.y
+= (dir
== DIR16_N
? -8*SCALE
: 8*SCALE
);
1451 /* abusing curr_step variable to save position in a burst of flames */
1452 if(go
->objspecific
.enemy
.curr_step
&& go
->objspecific
.enemy
.curr_step
< 16)
1454 else if(go
->objspecific
.enemy
.curr_step
)
1455 go
->objspecific
.enemy
.curr_step
= 0;
1457 if(objs
[player_ids
[0]].pos
.x
< go
->pos
.x
-32*SCALE
|| objs
[player_ids
[0]].pos
.x
> go
->pos
.x
+ (16+32)*SCALE
)
1461 go
->objspecific
.enemy
.curr_step
++;
1465 case OBJ_GUNTURRET_MOVABLE_MAN
:
1466 case OBJ_GUNTURRET_MOVABLE_MACHINE
: {
1467 from
= get_gameobj_center(*itemid
);
1468 vec2f p_center
= get_gameobj_center(player_ids
[0]);
1469 vec2f vel
= velocity(&from
, &p_center
);
1470 enum direction dir8
= get_direction_from_vec(&vel
);
1471 dir
= get_direction16_from_direction(dir8
);
1472 switch_anim(*itemid
, get_turret_anim_from_dir(*itemid
, dir8
));
1476 case OBJ_TANK_SMALL
:
1482 const vec2f
*bunkerpos
;
1483 const enum direction16
*bunkerdir
;
1484 unsigned count
, sec
;
1485 static const enum direction16 bunker5_dir
[] = {
1486 [0] = DIR16_N
, [1] = DIR16_NO
, [2] = DIR16_O
, [3] = DIR16_SO
,
1487 [4] = DIR16_S
, [5] = DIR16_SW
, [6] = DIR16_W
, [7] = DIR16_NW
,
1489 static const enum direction16 bunker1_dir
[] = {
1490 [0] = DIR16_NNW
, [1] = DIR16_NNO
, [2] = DIR16_NO
, [3] = DIR16_O
,
1491 [4] = DIR16_SO
, [5] = DIR16_SSO
, [6] = DIR16_SSW
, [7] = DIR16_SW
,
1492 [8] = DIR16_W
, [9] = DIR16_NW
,
1494 static const vec2f bunker5_pos
[] = {
1495 [0] = VEC(9,-8), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(22,10),
1496 [4] = VEC(9,16), [5] = VEC(-5,13), [6] = VEC(-7,2), [7] = VEC(-5,-6),
1498 static const vec2f bunker2_pos
[] = {
1499 [0] = VEC(15,-2), [1] = VEC(27,3), [2] = VEC(31,12), [3] = VEC(28,23),
1500 [4] = VEC(15,27), [5] = VEC(2,23), [6] = VEC(0,12), [7] = VEC(4,2),
1502 static const vec2f bunker4_pos
[] = {
1503 [0] = VEC(13,0), [1] = VEC(25,1), [2] = VEC(29,10), [3] = VEC(26,21),
1504 [4] = VEC(13,25), [5] = VEC(0,21), [6] = VEC(-2,10), [7] = VEC(2,0),
1506 static const vec2f bunker1_pos
[] = {
1507 [0] = VEC(6,-6), [1] = VEC(18,-6), [2] = VEC(26,2), [3] = VEC(26,12),
1508 [4] = VEC(26,16), [5] = VEC(20,24), [6] = VEC(6,24), [7] = VEC(0,20),
1509 [8] = VEC(0,12), [9] = VEC(0,2),
1511 static const enum direction16 tank_dir
[] = {
1512 [0] = DIR16_WSW
, [1] = DIR16_OSO
, [2] = DIR16_WSW
, [3] = DIR16_OSO
,
1513 [4] = DIR16_WSW
, [5] = DIR16_OSO
, [6] = DIR16_SSW
, [7] = DIR16_SSO
,
1515 static const vec2f tank_pos
[] = {
1516 [0] = VEC(10,8), [1] = VEC(38,8), [2] = VEC(10,20), [3] = VEC(38,20),
1517 [4] = VEC(10,32), [5] = VEC(38,32), [6] = VEC(16,40), [7] = VEC(32,40),
1519 unsigned b
, dist
= 28;
1520 switch(go
->objtype
) {
1523 bunkerpos
= bunker1_pos
;
1524 bunkerdir
= bunker1_dir
;
1529 case OBJ_TANK_SMALL
:
1530 if(tickcounter
% 8) break;
1533 bunkerpos
= tank_pos
;
1534 bunkerdir
= tank_dir
;
1535 // FIXME: the last 2 shoots (6+7) should be fired at the same time
1536 goto bunker_circle_shoot
;
1538 if(tickcounter
% 8) break;
1541 bunkerpos
= bunker2_pos
;
1542 bunkerdir
= bunker5_dir
;
1543 goto bunker_circle_shoot
;
1545 if(tickcounter
% /*(fps*3.5)/8*/ 24) break;
1548 bunkerpos
= bunker4_pos
;
1549 bunkerdir
= bunker5_dir
;
1550 bunker_circle_shoot
:;
1551 b
= go
->objspecific
.enemy
.curr_step
;
1553 if(++go
->objspecific
.enemy
.curr_step
>= 8) go
->objspecific
.enemy
.curr_step
= 0;
1557 bunkerpos
= bunker2_pos
;
1558 bunkerdir
= bunker5_dir
;
1566 bunkerpos
= bunker5_pos
;
1567 bunkerdir
= bunker5_dir
;
1574 if(tickcounter
> (uint32_t) go
->objspecific
.enemy
.curr_step
+ fps
*sec
) {
1575 go
->objspecific
.enemy
.curr_step
= tickcounter
;
1577 for(; b
< count
; b
++) {
1579 from
.x
+= bunkerpos
[b
].x
*SCALE
;
1580 from
.y
+= bunkerpos
[b
].y
*SCALE
;
1581 enemy_fire_bullet(bunkerdir
[b
], dist
, ew
, &from
);
1593 static int move_gameobjs(void) {
1595 size_t i
, obj_visited
;
1596 for(i
= 0, obj_visited
= 0; obj_visited
< obj_count
&& i
< OBJ_MAX
; i
++) {
1597 if(obj_slot_used
[i
]) {
1598 struct gameobj
*go
= &objs
[i
];
1600 if(go
->anim_curr
== ANIM_STEP_INIT
) res
= 1;
1601 if(go
->vel
.x
!= 0 || go
->vel
.y
!= 0) {
1602 vec2f oldpos
= go
->pos
;
1603 go
->pos
.x
+= go
->vel
.x
;
1604 go
->pos
.y
+= go
->vel
.y
;
1606 if(go
->objtype
== OBJ_P1
|| go
->objtype
== OBJ_P2
) {
1607 if(go
->pos
.y
< SCREEN_MIN_Y
) go
->pos
.y
= SCREEN_MIN_Y
;
1608 else if(go
->pos
.y
+25*SCALE
> SCREEN_MAX_Y
) go
->pos
.y
= SCREEN_MAX_Y
-25*SCALE
;
1609 if(go
->pos
.x
< SCREEN_MIN_X
) go
->pos
.x
= SCREEN_MIN_X
;
1610 else if(go
->pos
.x
+32*SCALE
> SCREEN_MAX_X
) go
->pos
.x
= SCREEN_MAX_X
-32*SCALE
;
1611 vec2f center
= get_sprite_center(spritemaps
[go
->spritemap_id
]);
1612 center
= vecadd(¢er
, &go
->pos
);
1613 if(is_wall(¢er
)) {
1621 if((go
->objtype
== OBJ_ENEMY_BOMBER
|| go
->objtype
== OBJ_ENEMY_SHOOTER
) &&
1622 go
->anim_curr
== animations
[go
->animid
].last
&&
1623 (go
->animid
== ANIM_ENEMY_BOMBER_DIE
||
1624 go
->animid
== ANIM_ENEMY_GUNNER_DIE
||
1625 go
->animid
== ANIM_ENEMY_BURNT
)) {
1626 dprintf(2, "removed enemy from %.2f,%.2f\n", go
->pos
.x
, go
->pos
.y
);
1628 golist_remove(&go_enemies
, i
);
1638 static void game_update_caption(void) {
1640 snprintf(buf
, 128, "objs: %d, map x,y %d/%d, index %d, xoff %d, yoff %d, spawnscreen %d, line %d", (int) obj_count
,
1641 (int)mapsquare
.x
, (int)mapsquare
.y
, (int)map
->screen_map
[mapsquare
.y
][mapsquare
.x
],
1642 (int)mapscreen_xoff
, (int)mapscreen_yoff
, (int)map_spawn_screen_index
, (int) map_spawn_line
);
1643 SDL_WM_SetCaption(buf
, 0);
1645 static void(*update_caption
)(void) = game_update_caption
;
1647 /* returns 1 if level finished */
1648 static int game_tick(int force_redraw
) {
1649 int need_redraw
= force_redraw
;
1650 if(mousebutton_down
[MB_LEFT
] > 1) {
1651 const int player_no
= 0;
1652 const struct weapon
*pw
= get_active_weapon(player_no
);
1653 //if(get_active_weapon_id(player_no) == WP_M134) __asm__("int3");
1654 if (pw
->flags
& WF_AUTOMATIC
) {
1655 float shots_per_second
= pw
->rpm
/ 60.f
;
1656 float shotinterval
= fps
/ shots_per_second
;
1657 if((int)((float)mousebutton_down
[MB_LEFT
] / shotinterval
) != (int)((float)(mousebutton_down
[MB_LEFT
]-1) / shotinterval
))
1658 fire_bullet(player_no
);
1662 if(advance_animations()) need_redraw
= 1;
1663 if(hit_bullets(&go_player_bullets
, &go_enemies
)) need_redraw
= 1;
1664 if(hit_bullets(&go_player_bullets
, &go_vehicles
)) need_redraw
= 1;
1665 if(hit_bullets(&go_flames
, &go_enemies
)) need_redraw
= 1;
1666 if(hit_bullets(&go_enemy_flames
, &go_enemies
)) need_redraw
= 1;
1667 if(hit_bullets(&go_rockets
, &go_enemies
)) need_redraw
= 1;
1668 if(hit_bullets(&go_rockets
, &go_vehicles
)) need_redraw
= 1;
1669 if(hit_bullets(&go_explosions
, &go_enemies
)) need_redraw
= 1;
1670 if(hit_bullets(&go_explosions
, &go_vehicles
)) need_redraw
= 1;
1671 if(hit_bullets(&go_explosions
, &go_mines
)) need_redraw
= 1;
1672 if(hit_bullets(&go_explosions
, &go_turrets
)) need_redraw
= 1;
1673 if(hit_bullets(&go_explosions
, &go_bunkers
)) need_redraw
= 1;
1674 if(hit_bullets(&go_explosions
, &go_players
)) need_redraw
= 1;
1675 if(hit_bullets(&go_enemy_explosions
, &go_players
)) need_redraw
= 1;
1676 if(hit_bullets(&go_enemy_bullets
, &go_players
)) need_redraw
= 1;
1677 if(hit_bullets(&go_enemy_flames
, &go_players
)) need_redraw
= 1;
1678 if(hit_bullets(&go_vehicles
, &go_players
)) need_redraw
= 1;
1679 if(hit_bullets(&go_enemies
, &go_players
)) need_redraw
= 1;
1680 if(hit_bullets(&go_mines
, &go_players
)) need_redraw
= 1;
1682 int ret
, level_finished
= 0;
1683 if((ret
= hit_bullets(&go_boss
, &go_players
)) == 2) level_finished
= 1;
1684 else if(ret
== 1) need_redraw
= 1;
1686 if(remove_bullets(&go_player_bullets
)) need_redraw
= 1;
1687 if(remove_bullets(&go_flames
)) need_redraw
= 1;
1688 if(remove_bullets(&go_explosions
)) need_redraw
= 1;
1689 if(remove_bullets(&go_enemy_explosions
)) need_redraw
= 1;
1690 if(remove_bullets(&go_enemy_bullets
)) need_redraw
= 1;
1691 if(remove_bullets(&go_enemy_flames
)) need_redraw
= 1;
1692 if(remove_bullets(&go_muzzleflash
)) need_redraw
= 1;
1693 if(remove_bullets(&go_blood
)) need_redraw
= 1;
1694 if(remove_explosives(&go_grenades
)) need_redraw
= 1;
1695 if(remove_explosives(&go_enemy_grenades
)) need_redraw
= 1;
1696 if(remove_explosives(&go_rockets
)) need_redraw
= 1;
1697 if(tickcounter
% 2 == 0 && scroll_map()) need_redraw
= 1;
1700 if(tickcounter
% 4 == 0 && process_turrets(&go_turrets
)) need_redraw
= 1;
1701 if(tickcounter
% 4 == 0 && process_turrets(&go_bunkers
)) need_redraw
= 1;
1702 if(tickcounter
% 4 == 0 && process_turrets(&go_vehicles
)) need_redraw
= 1;
1704 if(move_gameobjs()) need_redraw
= 1;
1706 if(remove_offscreen_objects(&go_enemies
)) need_redraw
= 1;
1707 if(remove_offscreen_objects(&go_vehicles
)) need_redraw
= 1;
1708 if(remove_offscreen_objects(&go_mines
)) need_redraw
= 1;
1709 if(remove_offscreen_objects(&go_turrets
)) need_redraw
= 1;
1710 if(remove_offscreen_objects(&go_bunkers
)) need_redraw
= 1;
1713 struct timeval timer
;
1714 gettimestamp(&timer
);
1719 video_update_region(SCREEN_MIN_X
,SCREEN_MIN_Y
, SCREEN_MAX_X
- SCREEN_MIN_X
, VMODE_H
);
1722 ms_used
= mspassed(&timer
);
1723 //if(ms_used) printf("repaint took: ms_used %ld\n", ms_used);
1724 int res
= audio_process();
1725 if(res
== -1) music_restart();
1726 ms_used
= mspassed(&timer
);
1727 //if(ms_used) printf("audio processed: %d, ms_used %ld\n", res, ms_used);
1729 long sleepms
= 1000/fps
- ms_used
;
1730 if(sleepms
>= 0) SDL_Delay(sleepms
);
1731 if(mousebutton_down
[MB_LEFT
]) mousebutton_down
[MB_LEFT
]++;
1736 return level_finished
;
1746 static enum cursor cursor_lut
[] = {
1748 [SDLK_DOWN
] = c_down
,
1749 [SDLK_LEFT
] = c_left
,
1750 [SDLK_RIGHT
] = c_right
,
1757 static char cursors_pressed
[] = {
1764 static vec2f
get_vel_from_direction(enum direction dir
, float speed
) {
1765 #define VELLUT(a, b, c) [a] = VEC(b, c)
1766 static const vec2f vel_lut
[] = {
1767 VELLUT(DIR_O
, 1, 0),
1768 VELLUT(DIR_NO
, 0.7071067769704655, -0.7071067769704655),
1769 VELLUT(DIR_N
, 0, -1),
1770 VELLUT(DIR_NW
, -0.7071067769704655, -0.7071067769704655),
1771 VELLUT(DIR_W
, -1, 0),
1772 VELLUT(DIR_SW
, -0.7071067769704655, 0.7071067769704655),
1773 VELLUT(DIR_S
, 0, 1),
1774 VELLUT(DIR_SO
, 0.7071067769704655, 0.7071067769704655),
1777 vec2f v
= vel_lut
[dir
];
1778 v
.x
*= speed
* SCALE
;
1779 v
.y
*= speed
* SCALE
;
1783 static vec2f
get_vel_from_direction16(enum direction16 dir
, float speed
) {
1784 #define VELLUT(a, b, c) [a] = VEC(b, c)
1785 #define ANK90 0.7071067769704655
1787 #define ANK45 0.9238795042037964
1788 #define GK45 0.3826834261417389
1789 static const vec2f vel_lut
[] = {
1790 VELLUT(DIR16_O
, 1, 0),
1791 VELLUT(DIR16_ONO
, ANK45
, -GK45
),
1792 VELLUT(DIR16_NO
, ANK90
, -ANK90
),
1793 VELLUT(DIR16_NNO
, GK45
, -ANK45
),
1794 VELLUT(DIR16_N
, 0, -1),
1795 VELLUT(DIR16_NNW
, -GK45
, -ANK45
),
1796 VELLUT(DIR16_NW
, -ANK90
, -ANK90
),
1797 VELLUT(DIR16_WNW
, -ANK45
, -GK45
),
1798 VELLUT(DIR16_W
, -1, 0),
1799 VELLUT(DIR16_WSW
, -ANK45
, GK45
),
1800 VELLUT(DIR16_SW
, -ANK90
, ANK90
),
1801 VELLUT(DIR16_SSW
, -GK45
, ANK45
),
1802 VELLUT(DIR16_S
, 0, 1),
1803 VELLUT(DIR16_SSO
, GK45
, ANK45
),
1804 VELLUT(DIR16_SO
, ANK90
, ANK90
),
1805 VELLUT(DIR16_OSO
, ANK45
, GK45
),
1808 vec2f v
= vel_lut
[dir
];
1809 v
.x
*= speed
* SCALE
;
1810 v
.y
*= speed
* SCALE
;
1815 static enum direction
get_direction_from_vec(vec2f
*vel
) {
1816 float deg_org
, deg
= atan2(vel
->y
, vel
->x
);
1818 if(deg
< 0) deg
*= -1.f
;
1819 else if(deg
> 0) deg
= M_PI
+ (M_PI
- deg
); // normalize atan2 result to scale from 0 to 2 pi
1820 int hexadrant
= (int)(deg
/ ((1.0/16.0f
)*2*M_PI
));
1821 assert(hexadrant
>= 0 && hexadrant
< 16);
1822 static const enum direction rad_lut
[] = {
1833 return rad_lut
[hexadrant
];
1836 static enum direction
get_direction_from_cursor(void) {
1837 enum direction dir
= DIR_INVALID
;
1838 if(cursors_pressed
[c_up
]) {
1839 if(cursors_pressed
[c_left
]) dir
= DIR_NW
;
1840 else if(cursors_pressed
[c_right
]) dir
= DIR_NO
;
1842 } else if (cursors_pressed
[c_down
]) {
1843 if(cursors_pressed
[c_left
]) dir
= DIR_SW
;
1844 else if(cursors_pressed
[c_right
]) dir
= DIR_SO
;
1846 } else if (cursors_pressed
[c_left
]) {
1848 } else if (cursors_pressed
[c_right
]) {
1854 static enum animation_id
get_anim_from_direction(enum direction dir
, int player_no
, int throwing
) {
1855 #define DIRMAP(a, b) [a] = b
1857 static const enum animation_id dir_map_p1
[] = {
1858 DIRMAP(DIR_N
, ANIM_P1_MOVE_N
),
1859 DIRMAP(DIR_NW
, ANIM_P1_MOVE_NW
),
1860 DIRMAP(DIR_W
, ANIM_P1_MOVE_W
),
1861 DIRMAP(DIR_SW
, ANIM_P1_MOVE_SW
),
1862 DIRMAP(DIR_S
, ANIM_P1_MOVE_S
),
1863 DIRMAP(DIR_SO
, ANIM_P1_MOVE_SO
),
1864 DIRMAP(DIR_O
, ANIM_P1_MOVE_O
),
1865 DIRMAP(DIR_NO
, ANIM_P1_MOVE_NO
),
1867 static const enum animation_id dir_map_p2
[] = {
1868 DIRMAP(DIR_N
, ANIM_P2_MOVE_N
),
1869 DIRMAP(DIR_NW
, ANIM_P2_MOVE_NW
),
1870 DIRMAP(DIR_W
, ANIM_P2_MOVE_W
),
1871 DIRMAP(DIR_SW
, ANIM_P2_MOVE_SW
),
1872 DIRMAP(DIR_S
, ANIM_P2_MOVE_S
),
1873 DIRMAP(DIR_SO
, ANIM_P2_MOVE_SO
),
1874 DIRMAP(DIR_O
, ANIM_P2_MOVE_O
),
1875 DIRMAP(DIR_NO
, ANIM_P2_MOVE_NO
),
1877 const enum animation_id
*dir_map
= player_no
== 0 ? dir_map_p1
: dir_map_p2
;
1878 return dir_map
[dir
];
1880 static const enum animation_id dir_map_p1_g
[] = {
1881 DIRMAP(DIR_N
, ANIM_P1_THROW_N
),
1882 DIRMAP(DIR_NW
, ANIM_P1_THROW_NW
),
1883 DIRMAP(DIR_W
, ANIM_P1_THROW_W
),
1884 DIRMAP(DIR_SW
, ANIM_P1_THROW_SW
),
1885 DIRMAP(DIR_S
, ANIM_P1_THROW_S
),
1886 DIRMAP(DIR_SO
, ANIM_P1_THROW_SO
),
1887 DIRMAP(DIR_O
, ANIM_P1_THROW_O
),
1888 DIRMAP(DIR_NO
, ANIM_P1_THROW_NO
),
1890 static const enum animation_id dir_map_p2_g
[] = {
1891 DIRMAP(DIR_N
, ANIM_P2_THROW_N
),
1892 DIRMAP(DIR_NW
, ANIM_P2_THROW_NW
),
1893 DIRMAP(DIR_W
, ANIM_P2_THROW_W
),
1894 DIRMAP(DIR_SW
, ANIM_P2_THROW_SW
),
1895 DIRMAP(DIR_S
, ANIM_P2_THROW_S
),
1896 DIRMAP(DIR_SO
, ANIM_P2_THROW_SO
),
1897 DIRMAP(DIR_O
, ANIM_P2_THROW_O
),
1898 DIRMAP(DIR_NO
, ANIM_P2_THROW_NO
),
1900 const enum animation_id
*dir_map
= player_no
== 0 ? dir_map_p1_g
: dir_map_p2_g
;
1901 return dir_map
[dir
];
1907 static enum animation_id
get_anim_from_cursor(void) {
1908 enum direction dir
= get_direction_from_cursor();
1909 if(dir
== DIR_INVALID
) return ANIM_INVALID
;
1910 return get_anim_from_direction(dir
, 0, 0);
1912 /* playerno is either 0 or 1, not player_id! */
1913 static enum animation_id
get_anim_from_vel(int player_no
, vec2f
*vel
) {
1914 enum direction dir
= get_direction_from_vec(vel
);
1915 if(dir
== DIR_INVALID
) return ANIM_INVALID
;
1916 return get_anim_from_direction(dir
, player_no
, 0);
1920 static void switch_anim(int obj_id
, int aid
) {
1921 if(objs
[obj_id
].animid
== aid
) return;
1922 gameobj_start_anim(obj_id
, aid
);
1925 enum map_index
choose_mission(uint8_t* completed
);
1926 //RcB: DEP "mission_select.c"
1927 #include "enemytag.c"
1929 static uint8_t mission_completed
[MI_MAX
];
1931 static void finish_level(void) {
1932 font_print(SCREEN_MIN_X
, SCREEN_MIN_Y
, "level done", 10, SCALE
, PRGB(255,255,255));
1937 SDL_Delay(1000/fps
);
1940 mission_completed
[current_map
] = 1;
1946 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
1947 SDL_EnableKeyRepeat(100, 20);
1955 /* background music for mission selection screen */
1956 music_play(TUNE_MAP
);
1958 if((current_map
= choose_mission(mission_completed
)) == MI_INVALID
) goto dun_goofed
;
1960 music_play(TUNE_FIGHTING
);
1968 //redraw(surface, startx, starty);
1969 const float player_speed
= 1.25f
;
1970 const struct palpic
* spritemap
= spritemaps
[SI_PLAYERS
];
1971 struct { int *target
; int dir
; int max
;} moves
[] = {
1972 [c_up
] = {&starty
, SCALE
* -1, VMODE_H
- (palpic_getspriteheight(spritemap
) * SCALE
)},
1973 [c_down
] = {&starty
, SCALE
, VMODE_H
- (palpic_getspriteheight(spritemap
) * SCALE
)},
1974 [c_left
] = {&startx
, SCALE
* -1, VMODE_W
- (palpic_getspritewidth(spritemap
) * SCALE
)},
1975 [c_right
] = {&startx
, SCALE
, VMODE_W
- (palpic_getspritewidth(spritemap
) * SCALE
)},
1980 int player_id
= player_ids
[player_no
];
1984 SDL_Event sdl_event
;
1986 unsigned need_redraw
= 0;
1988 while (SDL_PollEvent(&sdl_event
)) {
1990 switch (sdl_event
.type
) {
1991 case SDL_MOUSEMOTION
:
1992 if((int)mousepos
->x
!= sdl_event
.motion
.x
|| (int)mousepos
->y
!= sdl_event
.motion
.y
)
1994 mousepos
->x
= sdl_event
.motion
.x
;
1995 mousepos
->y
= sdl_event
.motion
.y
;
1997 case SDL_MOUSEBUTTONDOWN
:
1998 mousepos
->x
= sdl_event
.button
.x
;
1999 mousepos
->y
= sdl_event
.button
.y
;
2000 mousebutton_down
[MB_LEFT
] = 1;
2001 fire_bullet(player_no
);
2003 case SDL_MOUSEBUTTONUP
:
2004 mousepos
->x
= sdl_event
.button
.x
;
2005 mousepos
->y
= sdl_event
.button
.y
;
2006 mousebutton_down
[MB_LEFT
] = 0;
2013 switch(sdl_event
.key
.keysym
.sym
) {
2014 case SDLK_w
: case SDLK_a
: case SDLK_s
: case SDLK_d
:
2019 cursors_pressed
[cursor_lut
[sdl_event
.key
.keysym
.sym
]] = 1;
2021 enum direction dir
= get_direction_from_cursor();
2022 if(dir
!= DIR_INVALID
) {
2023 if(!mousebutton_down
[MB_LEFT
]) {
2024 // change animation according to cursors,
2025 // unless we're in automatic fire mode.
2026 enum animation_id aid
= get_anim_from_direction(dir
, player_no
, 0);
2027 if(aid
!= ANIM_INVALID
) switch_anim(player_id
, aid
);
2029 objs
[player_id
].vel
= get_vel_from_direction(dir
, player_speed
);
2031 objs
[player_id
].vel
= VEC(0,0);
2036 if((sdl_event
.key
.keysym
.mod
& KMOD_LALT
) ||
2037 (sdl_event
.key
.keysym
.mod
& KMOD_RALT
)) {
2038 toggle_fullscreen();
2048 if((int)player_weapons
[player_no
][0] == 0)
2049 weapon_inc
= WP_MAX
-1;
2050 else weapon_inc
= -1;
2052 player_weapons
[player_no
][0] += weapon_inc
;
2053 if(player_weapons
[player_no
][0] == WP_INVALID
)
2054 player_weapons
[player_no
][0] = 0;
2055 printf("%s\n", weapon_name(player_weapons
[player_no
][0]));
2063 switch(sdl_event
.key
.keysym
.sym
) {
2066 update_caption
= game_update_caption
;
2073 case SDLK_w
: case SDLK_a
: case SDLK_s
: case SDLK_d
:
2078 cursors_pressed
[cursor_lut
[sdl_event
.key
.keysym
.sym
]] = 0;
2090 for (i
= 0; i
< ARRAY_SIZE(cursors_pressed
); i
++) {
2091 if(cursors_pressed
[i
]) {
2092 *moves
[i
].target
+= moves
[i
].dir
;
2093 if(*moves
[i
].target
< 0) *moves
[i
].target
= 0;
2094 if(*moves
[i
].target
> moves
[i
].max
) *moves
[i
].target
= moves
[i
].max
;
2098 if(game_tick(need_redraw
)) {
2100 music_play(TUNE_LEVEL_FINISHED
);
2102 goto mission_select
;