-
Notifications
You must be signed in to change notification settings - Fork 4
/
spriteview.c
2194 lines (2029 loc) · 70.2 KB
/
spriteview.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../lib/include/timelib.h"
#include "../lib/include/macros.h"
#include "../lib/include/sblist.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "vec2f.h"
#include "anim.h"
#include "gameobj.h"
#include "video.h"
#include "direction.h"
#include "weapon.h"
#include "palpic.h"
#include "sdl_rgb.h"
#include "audio.h"
#include "muzzle_tab.h"
#include "spritemaps.h"
#include "enemy.h"
#include "font.h"
#include "maps.h"
#include "mapsprites.h"
#include "walls.h"
#include "music.h"
#include "spawnmaps.h"
#include <SDL/SDL.h>
#ifndef IN_KDEVELOP_PARSER
#include "../lib/include/bitarray.h"
#endif
enum mousebutton {
MB_LEFT = 0,
MB_RIGHT,
};
// 1 if button down, 0 if not, >1 to count ms pressed
unsigned long mousebutton_down[] = {
[MB_LEFT] = 0,
[MB_RIGHT] = 0,
};
#pragma RcB2 LINK "-lSDL"
#if 0
static void get_last_move_event(SDL_Event* e) {
#define numpeek 32
SDL_Event peek[numpeek];
SDL_Event* last_event = NULL;
int i, results;
results = SDL_PeepEvents(peek, numpeek, SDL_PEEKEVENT, (uint32_t) ~0);
if(results == -1) return;
for(i = 0; i < results; i++) {
if(peek[i].type == SDL_MOUSEMOTION)
last_event = &peek[i];
else
break;
}
if(last_event) {
*e = *last_event;
SDL_PeepEvents(peek, i + 1, SDL_GETEVENT, (uint32_t) ~0);
}
#undef numpeek
}
#endif
static vec2f get_sprite_center(const struct palpic *p) {
assert(p->spritecount);
vec2f res;
res.x = palpic_getspritewidth(p) * SCALE / 2;
res.y = palpic_getspriteheight(p) * SCALE / 2;
return res;
}
static int player_ids[2];
static int player_kills[2];
static int player_score[2];
static int player_cash[2];
static enum weapon_id player_weapons[2][WP_MAX];
static int weapon_count[2];
static int weapon_active[2]; // index into player_weapons[playerno]
static int player_ammo[2][AMMO_MAX];
static enum weapon_id get_active_weapon_id(int player_no);
static void switch_anim(int obj_id, int aid);
static vec2f get_vel_from_direction(enum direction dir, float speed);
static vec2f get_vel_from_direction16(enum direction16 dir, float speed);
// used by game_tick
static sblist go_player_bullets;
static sblist go_enemy_bullets;
static sblist go_explosions;
static sblist go_enemy_explosions;
static sblist go_enemies;
static sblist go_players;
static sblist go_flames;
static sblist go_enemy_flames;
static sblist go_rockets;
static sblist go_grenades;
static sblist go_enemy_grenades;
static sblist go_vehicles;
static sblist go_mines;
static sblist go_turrets;
static sblist go_bunkers;
static sblist go_boss;
static sblist go_crosshair;
static sblist go_muzzleflash;
static sblist go_blood;
static void add_pbullet(uint8_t bullet_id) {
sblist_add(&go_player_bullets, &bullet_id);
}
static void add_ebullet(uint8_t bullet_id) {
sblist_add(&go_enemy_bullets, &bullet_id);
}
static void add_player(uint8_t player_id) {
sblist_add(&go_players, &player_id);
}
static void add_enemy(uint8_t enem_id) {
sblist_add(&go_enemies, &enem_id);
}
static void add_explosion(uint8_t expl_id) {
sblist_add(&go_explosions, &expl_id);
}
static void add_enemy_explosion(uint8_t expl_id) {
sblist_add(&go_enemy_explosions, &expl_id);
}
static void add_flame(uint8_t id) {
sblist_add(&go_flames, &id);
}
static void add_grenade(uint8_t id) {
sblist_add(&go_grenades, &id);
}
static void add_enemy_grenade(uint8_t id) {
sblist_add(&go_enemy_grenades, &id);
}
static void add_rocket(uint8_t id) {
sblist_add(&go_rockets, &id);
}
static void add_vehicle(uint8_t id) {
sblist_add(&go_vehicles, &id);
}
static void add_mine(uint8_t id) {
sblist_add(&go_mines, &id);
}
static void add_turret(uint8_t id) {
sblist_add(&go_turrets, &id);
}
static void add_bunker(uint8_t id) {
sblist_add(&go_bunkers, &id);
}
static void add_boss(uint8_t id) {
sblist_add(&go_boss, &id);
}
static void add_crosshair(uint8_t id) {
sblist_add(&go_crosshair, &id);
}
static void add_muzzleflash(uint8_t id) {
sblist_add(&go_muzzleflash, &id);
}
static void add_blood(uint8_t id) {
sblist_add(&go_blood, &id);
}
static void add_enemy_flame(uint8_t id) {
sblist_add(&go_enemy_flames, &id);
}
static void golist_remove(sblist *l, uint8_t objid) {
size_t i;
uint8_t *itemid;
sblist_iter_counter2(l, i, itemid) {
if(*itemid == objid) {
sblist_delete(l, i);
return;
}
}
}
static int get_next_anim_frame(enum animation_id aid, anim_step curr) {
if(curr == ANIM_STEP_INIT) return animations[aid].first;
curr++;
if(curr > animations[aid].last) return animations[aid].first;
return curr;
}
#define SCREEN_MIN_X 64*SCALE
#define SCREEN_MAX_X VMODE_W - 64*SCALE
#define SCREEN_MIN_Y 0
#define SCREEN_MAX_Y 200*SCALE
static void draw_status_bar(void) {
enum weapon_id wid = get_active_weapon_id(0);
int x, y;
sdl_rgb_t *ptr = (sdl_rgb_t *) video.mem;
unsigned pitch = video.pitch/4;
for(y = SCREEN_MAX_Y; y < VMODE_H; y++)
for (x = SCREEN_MIN_X; x < SCREEN_MAX_X; x++)
ptr[y*pitch + x] = SRGB_BLACK;
blit_sprite(((320 / 2) - (59 / 2)) * SCALE, (200 + (40/2) - (16/2)) * SCALE,
&video, SCALE, spritemaps[SI_WEAPONS], wid, 0);
char buf[16];
snprintf(buf, 16, "%.6u", player_score[0]);
font_print(SCREEN_MIN_X + 8, SCREEN_MAX_Y + 8, buf, 6, 1 * SCALE, PRGB(255,255,255));
}
enum map_index current_map = MI_VIETNAM;
const struct map *map;
const struct map_screen* map_scr;
const struct palpic *map_bg;
const struct palpic *map_fg;
const mapscreen_index *map_bonus_indices;
const struct map_fglayer *map_bonus_scr;
int mapscreen_yoff, mapscreen_xoff;
struct { int x,y; } mapsquare;
enum map_scrolldir mapscrolldir;
unsigned map_spawn_screen_index;
unsigned map_spawn_line;
unsigned map_spawn_current;
static const int fps = 64;
static void init_map(enum map_index mapindex) {
map = maps[mapindex];
map_scr = map_screens[mapindex];
map_bg = map_bg_sprites[map->maptype];
map_fg = map_fg_sprites[map->maptype];
map_bonus_scr = map_bonus_screens[mapindex];
map_bonus_indices = map_bonus_layer_indices[mapindex];
mapscreen_yoff = 0;
mapscreen_xoff = 0;
mapsquare.x = 5;
mapsquare.y = 26;
mapscrolldir = MS_UP;
map_spawn_screen_index = 0;
map_spawn_line = 0;
map_spawn_current = 0;
}
static mapscreen_index get_bonus_layer_index(mapscreen_index screen) {
unsigned i;
for (i = 0; i < map->bonuslayer_count; i++)
if(map_bonus_indices[i] == screen) return i;
return MAPSCREEN_BLOCKED;
}
static mapscreen_index screen_to_mapscreen(int *x, int *y) {
*x = ((int) *x - SCREEN_MIN_X) / SCALE;
*y = ((int) *y - SCREEN_MIN_Y) / SCALE;
int yscr = (*y + mapscreen_yoff) / 192;
*y = (*y + mapscreen_yoff) - yscr*192;
int xscr = (*x + mapscreen_xoff) / 192;
*x = (*x + mapscreen_xoff) - xscr*192;
return map->screen_map[mapsquare.y + yscr][mapsquare.x + xscr];
}
static enum walltype is_wall(vec2f *pos) {
int x = pos->x;
int y = pos->y;
mapscreen_index scr_idx = screen_to_mapscreen(&x, &y);
/* can happen when a bullet goes partially off-screen */
if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
uint8_t spriteno = map_scr[scr_idx].fg.fg[y/16][x/16];
if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
scr_idx = get_bonus_layer_index(scr_idx);
if(scr_idx == MAPSCREEN_BLOCKED) return WT_NONE;
spriteno = map_bonus_scr[scr_idx].fg[y/16][x/16];
if(spriteno && walls[map->maptype][spriteno]) return walls[map->maptype][spriteno];
return WT_NONE;
}
static void draw_map() {
int y, x, my, mx;
unsigned map_off = 192-mapscreen_yoff;
unsigned vis_x = 192-mapscreen_xoff;
int x_iter_max16 = 192/16;
int x_iter_max64 = 192/64;
unsigned x_iter_start64 = x_iter_max64 - (vis_x / 64 + !!(vis_x % 64));
unsigned x_iter_start16 = x_iter_max16 - (vis_x / 16 + !!(vis_x % 16));
int x_screen_start64 = -(!!(vis_x%64)*64-(vis_x%64));
int x_screen_start16 = -(!!(vis_x%16)*16-(vis_x%16));
unsigned x_screen_iter;
for(x_screen_iter = 0; x_screen_iter <= !!mapscreen_xoff; x_screen_iter++) {
mapscreen_index bonus_layer;
if(x_screen_iter) {
x_screen_start16 = x_screen_start16+(x_iter_max16-x_iter_start16)*16;
x_screen_start64 = x_screen_start64+(x_iter_max64-x_iter_start64)*64;
x_iter_max16 = mapscreen_xoff / 16 + !!(mapscreen_xoff % 16);
x_iter_max64 = mapscreen_xoff / 64 + !!(mapscreen_xoff % 64);
x_iter_start16 = 0;
x_iter_start64 = 0;
assert(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter] != MAPSCREEN_BLOCKED);
}
uint8_t spriteno;
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)
for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
blit_sprite(x, y, &video,
SCALE, map_bg, spriteno, 0);
}
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)
for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
spriteno = map_scr[map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
}
bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y][mapsquare.x+x_screen_iter]);
if(bonus_layer != MAPSCREEN_BLOCKED) {
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)
for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
}
}
int yleft = 200-map_off;
if(yleft > 192) yleft = 192;
for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/32+!!(yleft%32); my++, y+=32*SCALE)
for(mx = x_iter_start64, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < x_iter_max64; mx++, x += 64*SCALE) {
spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].bg.bg[my][mx];
blit_sprite(x, y, &video, SCALE, map_bg, spriteno, 0);
}
for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
spriteno = map_scr[map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]].fg.fg[my][mx];
if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
}
bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+1][mapsquare.x+x_screen_iter]);
if(bonus_layer != MAPSCREEN_BLOCKED) {
for(my = 0, y = SCREEN_MIN_Y + (map_off * SCALE); my < yleft/16+!!(yleft%16); my++, y+=16*SCALE)
for(mx = x_iter_start16, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < x_iter_max16; mx++, x += 16*SCALE) {
spriteno = map_bonus_scr[bonus_layer].fg[my][mx];
if(spriteno) blit_sprite(x, y, &video, SCALE, map_fg, spriteno, 0);
}
}
/* this is never triggered when mapscreen_xoff != 0 */
if(mapscreen_yoff > 192 - 8) {
for(mx = 0, x = SCREEN_MIN_X + x_screen_start64*SCALE; mx < 3; mx++, x += 64*SCALE)
blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video,
SCALE, map_bg, map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].bg.bg[0][mx], 0);
for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
spriteno = map_scr[map->screen_map[mapsquare.y+2][mapsquare.x]].fg.fg[0][mx];
if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
}
bonus_layer = get_bonus_layer_index(map->screen_map[mapsquare.y+2][mapsquare.x]);
if(bonus_layer != MAPSCREEN_BLOCKED) {
for(mx = 0, x = SCREEN_MIN_X + x_screen_start16*SCALE; mx < 12; mx++, x += 16*SCALE) {
spriteno = map_bonus_scr[bonus_layer].fg[0][mx];
if(spriteno) blit_sprite(x, SCALE*(192*2-mapscreen_yoff), &video, SCALE, map_fg, spriteno, 0);
}
}
}
}
}
#define VSCROLL_TRESHOLD (200-74)
#define HSCROLLR_TRESHOLD (54-6)
#define HSCROLLL_TRESHOLD (192-(78+3))
static int scroll_needed() {
struct gameobj *player = &objs[player_ids[0]];
if((mapscrolldir == MS_UP && player->pos.y - SCREEN_MIN_Y < VSCROLL_TRESHOLD*SCALE) ||
(mapscrolldir == MS_RIGHT && player->pos.x - SCREEN_MIN_X > HSCROLLR_TRESHOLD*SCALE) ||
(mapscrolldir == MS_LEFT && player->pos.x - SCREEN_MIN_X < HSCROLLL_TRESHOLD*SCALE))
return 1;
return 0;
}
static void scroll_gameobjs(int scroll_step) {
if(!scroll_step) return;
unsigned i, avail = obj_count;
for(i = 0; i < OBJ_MAX && avail; i++) {
if(!obj_slot_used[i]) continue;
avail--;
if(objs[i].objtype == OBJ_CROSSHAIR) continue;
if(mapscrolldir == MS_UP)
objs[i].pos.y += scroll_step*SCALE;
else if(mapscrolldir == MS_LEFT)
objs[i].pos.x += scroll_step*SCALE;
else if(mapscrolldir == MS_RIGHT)
objs[i].pos.x -= scroll_step*SCALE;
}
}
static void next_screen() {
map_spawn_screen_index++;
map_spawn_line = 0;
map_spawn_current = 0;
}
static int init_enemy(const struct enemy_spawn *spawn);
static void handle_spawns(unsigned scrollstep) {
assert(scrollstep <= 192);
unsigned i;
const struct enemy_spawn_screen *spawn_map = spawn_maps[current_map];
if(!spawn_map[map_spawn_screen_index].spawns) goto done;
for(i = 0; i < scrollstep; i++) {
while(map_spawn_current < spawn_map[map_spawn_screen_index].num_spawns &&
map_spawn_line+i >= spawn_map[map_spawn_screen_index].spawns[map_spawn_current].scroll_line) {
init_enemy(&spawn_map[map_spawn_screen_index].spawns[map_spawn_current]);
map_spawn_current++;
}
}
done:
map_spawn_line += scrollstep;
}
static int scroll_map() {
int ret = 0;
int scroll_step = 1;
if(scroll_needed()) {
if(mapscrolldir == MS_UP) {
mapscreen_yoff -= scroll_step;
if(mapscreen_yoff < 0) {
mapsquare.y--;
if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
scroll_step = -mapscreen_yoff;
mapscreen_yoff = 0;
mapsquare.y++;
scroll_gameobjs(scroll_step);
if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
mapscrolldir = MS_LEFT;
} else {
mapscrolldir = MS_RIGHT;
next_screen();
}
scroll_step = 0;
} else {
next_screen();
mapscreen_yoff += 192;
}
}
handle_objs:;
handle_spawns(scroll_step);
scroll_gameobjs(scroll_step);
ret = 1;
} else if(mapscrolldir == MS_LEFT) {
mapscreen_xoff -= scroll_step;
if(mapscreen_xoff < 0) {
mapsquare.x--;
if(map->screen_map[mapsquare.y][mapsquare.x] == MAPSCREEN_BLOCKED) {
scroll_step = -mapscreen_xoff;
mapscreen_xoff = 0;
mapscreen_yoff = 0;
mapsquare.x++;
scroll_gameobjs(scroll_step);
mapscrolldir = MS_UP;
scroll_step = 0;
} else {
mapscreen_xoff += 192;
next_screen();
}
}
goto handle_objs;
} else if(mapscrolldir == MS_RIGHT) {
mapscreen_xoff += scroll_step;
if(mapscreen_xoff >= 192) {
mapsquare.x++;
if(map->screen_map[mapsquare.y][mapsquare.x+1] == MAPSCREEN_BLOCKED) {
scroll_step = mapscreen_xoff - 192;
mapscreen_xoff = 0;
mapscreen_yoff = 0;
scroll_gameobjs(scroll_step);
mapscrolldir = MS_UP;
scroll_step = 0;
} else {
next_screen();
mapscreen_xoff -= 192;
}
}
goto handle_objs;
}
}
return ret;
}
static void init_player_once(int player_no) {
player_score[player_no] = 0;
player_cash[player_no] = 10000;
}
static int init_player(int player_no) {
assert(player_no == 0 || player_no == 1);
int pid = gameobj_alloc();
gameobj_init(pid, &VEC( SCREEN_MIN_X, SCREEN_MAX_Y - (25 * SCALE) ), &VEC( 0, 0 ),
SI_PLAYERS, player_no == 0 ? ANIM_P1_MOVE_N : ANIM_P2_MOVE_N, player_no == 0 ? OBJ_P1 : OBJ_P2);
if(pid == -1) return -1;
player_ids[player_no] = pid;
weapon_active[player_no] = 0;
add_player(pid);
return pid;
}
static vec2f *mousepos;
static int init_crosshair() {
int id = gameobj_alloc();
gameobj_init(id, &VEC(VMODE_W/2, VMODE_H/2), &VEC(0,0), SI_CROSSHAIR, ANIM_CROSSHAIR, OBJ_CROSSHAIR);
if(id == -1) return -1;
mousepos = &objs[id].pos;
return id;
}
static int init_blood(vec2f *pos) {
int id = gameobj_alloc();
gameobj_init(id, pos, &VEC(0,0), SI_MISC, ANIM_BLOOD, OBJ_BLOOD);
gameobj_init_bulletdata(id, 4);
return id;
}
static int init_bullet(vec2f *pos, vec2f *vel, int steps) {
int id = gameobj_alloc();
gameobj_init(id, pos, vel, SI_BULLET, ANIM_BULLET, OBJ_BULLET);
gameobj_init_bulletdata(id, steps);
return id;
}
static int init_grenade(vec2f *pos, vec2f *vel, int steps) {
int id = gameobj_alloc();
gameobj_init(id, pos, vel, SI_GRENADE, ANIM_GRENADE_SMALL, OBJ_GRENADE);
gameobj_init_bulletdata(id, steps);
return id;
}
static int init_grenade_explosion(vec2f *pos, int from_enemy) {
const int ticks_per_anim_frame = 4;
const int expl_anim_frames = 11;
vec2f grenade_center = get_sprite_center(spritemaps[SI_GRENADE_EXPLOSION]);
vec2f mypos = vecsub(pos, &grenade_center);
int id = gameobj_alloc();
if(id == -1) return -1;
gameobj_init(id, &mypos, &VEC(0,0), SI_GRENADE_EXPLOSION, ANIM_GRENADE_EXPLOSION, OBJ_GRENADE_EXPLOSION);
gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
if(!from_enemy) add_explosion(id);
else add_enemy_explosion(id);
return id;
}
static int init_big_explosion(vec2f *pos) {
const int ticks_per_anim_frame = 8;
const int expl_anim_frames = 5;
vec2f rocket_center = get_sprite_center(spritemaps[SI_BIG_EXPLOSION]);
vec2f mypos = vecsub(pos, &rocket_center);
int id = gameobj_alloc();
if(id == -1) return -1;
gameobj_init(id, &mypos, &VEC(0,0), SI_BIG_EXPLOSION, ANIM_BIG_EXPLOSION, OBJ_BIG_EXPLOSION);
gameobj_init_bulletdata(id, expl_anim_frames*ticks_per_anim_frame -1);
audio_play_wave_resource(wavesounds[WS_GRENADE_EXPLOSION]);
add_explosion(id);
return id;
}
static int init_rocket_explosion(vec2f *pos) {
vec2f ax = vecadd(pos, &VEC(-15*SCALE, 9*SCALE));
vec2f bx = vecadd(pos, &VEC(1*SCALE, -6*SCALE));
vec2f cx = vecadd(pos, &VEC(-8*SCALE, -8*SCALE));
vec2f dx = vecadd(pos, &VEC(8*SCALE, 8*SCALE));
int ret = 0;
ret += init_grenade_explosion(&ax, 0) != -1;
ret += init_grenade_explosion(&bx, 0) != -1;
ret += init_big_explosion(&cx) != -1;
ret += init_big_explosion(&dx) != -1;
return ret;
}
static int init_flame(vec2f *pos, vec2f *vel, int steps) {
int id = gameobj_alloc();
if(id == -1) return -1;
gameobj_init(id, pos, vel, SI_FLAME, ANIM_FLAME, OBJ_FLAME);
gameobj_init_bulletdata(id, steps);
return id;
}
static int init_player_flame(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
static const vec2f flame_origin[] = {
[DIR_O] = { 4.0, 8.0 },
[DIR_NO] = { 5.0, 11.0 },
[DIR_N] = { 7.5, 12.0 },
[DIR_NW] = { 10.0, 11.0 },
[DIR_W] = { 11.0, 8.0 },
[DIR_SW] = { 10.0, 5.0 },
[DIR_S] = { 7.5, 3.0 },
[DIR_SO] = { 4.0, 4.0 },
};
vec2f mypos = *pos;
mypos.x -= flame_origin[dir].x * SCALE;
mypos.y -= flame_origin[dir].y * SCALE;
return init_flame(&mypos, vel, steps);
}
static int init_rocket(enum direction dir, vec2f *pos, vec2f *vel, int steps) {
static const vec2f rocket_origin[] = {
[DIR_N] = { 1.0, 10.0 },
[DIR_S] = { 1.0, 0.0 },
[DIR_O] = { 0.0, 1.0 },
[DIR_W] = { 10.0, 1.0 },
[DIR_NO] = { 0.0, 7.0 },
[DIR_SO] = { 0.0, 0.0 },
[DIR_SW] = { 7.0, 0.0 },
[DIR_NW] = { 7.0, 7.0 },
};
static const enum animation_id rocket_anim[] = {
[DIR_N] = ANIM_ROCKET_N,
[DIR_S] = ANIM_ROCKET_S,
[DIR_O] = ANIM_ROCKET_O,
[DIR_W] = ANIM_ROCKET_W,
[DIR_NO] = ANIM_ROCKET_NO,
[DIR_SO] = ANIM_ROCKET_SO,
[DIR_SW] = ANIM_ROCKET_SW,
[DIR_NW] = ANIM_ROCKET_NW,
};
vec2f mypos = *pos;
mypos.x -= rocket_origin[dir].x * SCALE;
mypos.y -= rocket_origin[dir].y * SCALE;
int id = gameobj_alloc();
if(id == -1) return -1;
gameobj_init(id, &mypos, vel, SI_ROCKET, rocket_anim[dir], OBJ_ROCKET);
gameobj_init_bulletdata(id, steps);
add_rocket(id);
return id;
}
static const struct enemy_route* get_enemy_current_route(int curr_step, const struct enemy_spawn *spawn) {
int i = ENEMY_MAX_ROUTE -1;
for(; i >= 0; i--)
if(spawn->route[i].shape != ES_INVALID &&
curr_step >= spawn->route[i].start_step)
return &spawn->route[i];
return 0;
}
static vec2f get_enemy_vel(int curr_step, const struct enemy_spawn *spawn) {
const struct enemy_route *route = get_enemy_current_route(curr_step, spawn);
return get_vel_from_direction16(route->dir, (float)route->vel/8.f);
}
static const enum animation_id enemy_animation_lut[] = {
[ES_SOLDIER1_DOWN] = ANIM_ENEMY_GUNNER_DOWN,
[ES_SOLDIER1_RIGHT] = ANIM_ENEMY_GUNNER_RIGHT,
[ES_SOLDIER1_LEFT] = ANIM_ENEMY_GUNNER_LEFT,
[ES_SOLDIER2_DOWN] = ANIM_ENEMY_BOMBER_DOWN,
[ES_SOLDIER2_RIGHT] = ANIM_ENEMY_BOMBER_RIGHT,
[ES_SOLDIER2_LEFT] = ANIM_ENEMY_BOMBER_LEFT,
[ES_JEEP] = ANIM_JEEP,
[ES_TANK_SMALL] = ANIM_TANK_SMALL,
[ES_TANK_BIG] = ANIM_TANK_BIG,
[ES_TRANSPORTER] = ANIM_TRANSPORTER,
[ES_GUNTURRET_MOVABLE_MACHINE] = ANIM_GUNTURRET_MOVABLE_MACHINE_S,
[ES_GUNTURRET_MOVABLE_MAN] = ANIM_GUNTURRET_MOVABLE_MAN_S,
[ES_MINE_FLAT] = ANIM_MINE_FLAT,
[ES_MINE_CROSS] = ANIM_MINE_CROSSED,
[ES_FLAMETURRET] = ANIM_FLAMETURRET,
[ES_GUNTURRET_FIXED_SOUTH] = ANIM_GUNTURRET_FIXED_SOUTH,
[ES_GUNTURRET_FIXED_NORTH] = ANIM_GUNTURRET_FIXED_NORTH,
[ES_BUNKER_1] = ANIM_BUNKER1,
[ES_BUNKER_2] = ANIM_BUNKER2,
[ES_BUNKER_3] = ANIM_BUNKER3,
[ES_BUNKER_4] = ANIM_BUNKER4,
[ES_BUNKER_5] = ANIM_BUNKER5,
};
static int init_enemy(const struct enemy_spawn *spawn) {
const enum objtype enemy_soldier_objtype_lut[] = {
[0] = OBJ_ENEMY_SHOOTER,
[1] = OBJ_ENEMY_BOMBER
};
const enum objtype enemy_objtype_lut[] = {
[ES_JEEP] = OBJ_JEEP,
[ES_TANK_SMALL] = OBJ_TANK_SMALL,
[ES_TANK_BIG] = OBJ_TANK_BIG,
[ES_TRANSPORTER] = OBJ_TRANSPORTER,
[ES_GUNTURRET_MOVABLE_MACHINE] = OBJ_GUNTURRET_MOVABLE_MACHINE,
[ES_GUNTURRET_MOVABLE_MAN] = OBJ_GUNTURRET_MOVABLE_MAN,
[ES_MINE_FLAT] = OBJ_MINE_FLAT,
[ES_MINE_CROSS] = OBJ_MINE_CROSSED,
[ES_FLAMETURRET] = OBJ_FLAMETURRET,
[ES_GUNTURRET_FIXED_SOUTH] = OBJ_GUNTURRET_FIXED_SOUTH,
[ES_GUNTURRET_FIXED_NORTH] = OBJ_GUNTURRET_FIXED_NORTH,
[ES_BUNKER_1] = OBJ_BUNKER1,
[ES_BUNKER_2] = OBJ_BUNKER2,
[ES_BUNKER_3] = OBJ_BUNKER3,
[ES_BUNKER_4] = OBJ_BUNKER4,
[ES_BUNKER_5] = OBJ_BUNKER5,
[ES_BOSS] = OBJ_BOSS,
};
const enum animation_id boss_animation_lut[] = {
[0] = ANIM_BOSS1,
[1] = ANIM_BOSS2,
[2] = ANIM_BOSS3,
[3] = ANIM_BOSS4,
[4] = ANIM_BOSS5,
[5] = ANIM_BOSS6,
[6] = ANIM_BOSS7,
[7] = ANIM_BOSS8,
[8] = ANIM_BOSS9,
[9] = ANIM_BOSS10,
[10] = ANIM_BOSS11,
[11] = ANIM_BOSS12,
};
const enum sprite_index enemy_soldier_sprite_lut[] = {
[ET_ASIAN] = SI_ENEMY_ASIAN,
[ET_WESTERN] = SI_ENEMY_WESTERN,
};
const enum sprite_index enemy_sprite_lut[] = {
[ES_JEEP] = SI_VEHICLES_SMALL,
[ES_TANK_SMALL] = SI_VEHICLES_MEDIUM,
[ES_TANK_BIG] = SI_VEHICLES_BIG,
[ES_TRANSPORTER] = SI_VEHICLES_BIG,
[ES_BUNKER_1] = SI_BUNKERS,
[ES_BUNKER_2] = SI_BUNKERS,
[ES_BUNKER_3] = SI_BUNKERS,
[ES_BUNKER_4] = SI_BUNKERS,
[ES_BUNKER_5] = SI_BUNKERS,
[ES_GUNTURRET_MOVABLE_MACHINE] = SI_GUNTURRET,
[ES_GUNTURRET_MOVABLE_MAN] = SI_GUNTURRET,
[ES_MINE_FLAT] = SI_MINES,
[ES_MINE_CROSS] = SI_MINES,
[ES_FLAMETURRET] = SI_MINES,
[ES_GUNTURRET_FIXED_SOUTH] = SI_MINES,
[ES_GUNTURRET_FIXED_NORTH] = SI_MINES,
[ES_BOSS] = SI_BOSSES,
};
vec2f spawnpos = VEC(SCREEN_MIN_X + spawn->x*SCALE, SCREEN_MIN_Y + spawn->y*SCALE);
int id = gameobj_alloc();
if(id == -1) return -1;
const struct enemy_route* route_curr = get_enemy_current_route(0, spawn);
vec2f vel = get_enemy_vel(0, spawn);
int is_soldier = route_curr->shape <= ES_SOLDIER2_RIGHT;
int is_boss = route_curr->shape == ES_BOSS;
enum sprite_index spriteid;
enum objtype objid;
enum animation_id animid;
if(is_soldier) {
spriteid = enemy_soldier_sprite_lut[map->enemy_type];
objid = enemy_soldier_objtype_lut[spawn->weapon];
} else {
spriteid = enemy_sprite_lut[route_curr->shape];
objid = enemy_objtype_lut[route_curr->shape];
}
if(is_boss) animid = boss_animation_lut[map->boss_id];
else animid = enemy_animation_lut[route_curr->shape];
gameobj_init(id, &spawnpos, &vel, spriteid, animid, objid);
objs[id].objspecific.enemy.curr_step = 0;
objs[id].objspecific.enemy.spawn = spawn;
switch(objid) {
case OBJ_BOSS:
add_boss(id);
break;
case OBJ_BUNKER1: case OBJ_BUNKER2: case OBJ_BUNKER3:
case OBJ_BUNKER4: case OBJ_BUNKER5:
add_bunker(id);
break;
case OBJ_GUNTURRET_MOVABLE_MACHINE:
case OBJ_GUNTURRET_MOVABLE_MAN:
case OBJ_FLAMETURRET: case OBJ_GUNTURRET_FIXED_NORTH:
case OBJ_GUNTURRET_FIXED_SOUTH:
add_turret(id);
break;
case OBJ_MINE_CROSSED: case OBJ_MINE_FLAT:
add_mine(id);
break;
case OBJ_JEEP: case OBJ_TRANSPORTER:
case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
add_vehicle(id);
break;
default:
add_enemy(id);
}
return id;
}
static void remove_enemy(int id) {
enum objtype objid = objs[id].objtype;
switch(objid) {
case OBJ_JEEP: case OBJ_TRANSPORTER:
case OBJ_TANK_BIG: case OBJ_TANK_SMALL:
golist_remove(&go_vehicles, id);
break;
default:
golist_remove(&go_enemies, id);
}
gameobj_free(id);
}
static int enemy_fires(struct enemy *e) {
int i;
for(i = 0; i < ENEMY_MAX_SHOT; i++)
if(e->curr_step == e->spawn->shots[i]) return 1;
return 0;
}
static enum animation_id get_flash_animation_from_direction(enum direction dir) {
#define ANIMF(dir, anim) [dir] = anim
static const enum animation_id dir_to_anim[] = {
ANIMF(DIR_O, ANIM_FLASH_O),
ANIMF(DIR_NO, ANIM_FLASH_NO),
ANIMF(DIR_N, ANIM_FLASH_N),
ANIMF(DIR_NW, ANIM_FLASH_NW),
ANIMF(DIR_W, ANIM_FLASH_W),
ANIMF(DIR_SW, ANIM_FLASH_SW),
ANIMF(DIR_S, ANIM_FLASH_S),
ANIMF(DIR_SO, ANIM_FLASH_SO),
};
#undef ANIMF
return dir_to_anim[dir];
}
static int init_flash(vec2f *pos, enum direction dir) {
int id = gameobj_alloc();
gameobj_init(id, pos, &VEC(0, 0), SI_FLASH, get_flash_animation_from_direction(dir), OBJ_FLASH);
gameobj_init_bulletdata(id, 2);
return id;
}
static vec2f get_gameobj_pos(int obj_id) {
return objs[obj_id].pos;
}
static vec2f get_gameobj_center(int obj_id) {
vec2f res = objs[obj_id].pos;
vec2f add = get_sprite_center(spritemaps[objs[obj_id].spritemap_id]);
return vecadd(&res, &add);
}
static enum direction get_direction_from_vec(vec2f *vel);
static enum animation_id get_anim_from_direction(enum direction dir, int player, int throwing);
static enum direction16 get_direction16_from_direction(enum direction dir) {
static const enum direction16 dir16_transl_tab[] = {
[DIR_N] = DIR16_N, [DIR_NO] = DIR16_NO, [DIR_O] = DIR16_O, [DIR_SO] = DIR16_SO,
[DIR_S] = DIR16_S, [DIR_SW] = DIR16_SW, [DIR_W] = DIR16_W, [DIR_NW] = DIR16_NW,
};
assert(dir != DIR_INVALID);
return dir16_transl_tab[dir];
}
static enum weapon_id get_active_weapon_id(int player_no) {
return player_weapons[player_no][weapon_active[player_no]];
}
static const struct weapon* get_active_weapon(int player_no) {
return &weapons[get_active_weapon_id(player_no)];
}
static enum direction16 get_shotdirection_from_enemy(int curr_step, const struct enemy_spawn *spawn) {
const struct enemy_route* r = get_enemy_current_route(curr_step, spawn);
switch(r->shape) {
case ES_SOLDIER1_DOWN: case ES_SOLDIER2_DOWN:
return DIR16_S;
case ES_SOLDIER1_LEFT: case ES_SOLDIER2_LEFT:
return DIR16_W;
case ES_SOLDIER1_RIGHT: case ES_SOLDIER2_RIGHT:
return DIR16_O;
default:
assert(0);
}
}
static void enemy_fire_bullet(enum direction16 dir, int steps, enum enemy_weapon wpn, vec2f *pos) {
vec2f vel = get_vel_from_direction16(dir, 1.75);
int id;
if(wpn == EW_GUN) {
id = init_bullet(pos, &vel, steps);
if(id != -1) add_ebullet(id);
} else if (wpn == EW_GRENADE) {
id = init_grenade(pos, &vel, steps);
if(id != -1) add_enemy_grenade(id);
} else {
id = init_flame(pos, &vel, steps);
if(id != -1) add_enemy_flame(id);
}
}
static int get_crosshair_id(void) {
assert(sblist_getsize(&go_crosshair));
uint8_t *id = sblist_get(&go_crosshair, 0);
return *id;
}
static void fire_bullet(int player_no) {
int id;
const struct weapon *pw = get_active_weapon(player_no);
if(player_ammo[player_no][pw->ammo] == 0) return;
vec2f from = get_gameobj_center(player_ids[player_no]);
//get_anim_from_vel(0, objs[player].
vec2f to = get_gameobj_center(get_crosshair_id());
to.x += 4*SCALE - rand()%8*SCALE;
to.y += 4*SCALE - rand()%8*SCALE;
vec2f vel = velocity(&from, &to);
enum direction dir = get_direction_from_vec(&vel);
if(dir != DIR_INVALID) {
enum animation_id aid = get_anim_from_direction(dir, player_no, pw->ammo == AMMO_GRENADE);
if(aid != ANIM_INVALID) switch_anim(player_ids[player_no], aid);
anim_step curranim = objs[player_ids[player_no]].anim_curr;
if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[player_ids[player_no]].animid, ANIM_STEP_INIT);
vec2f muzzle = muzzle_tab[curranim];
from = get_gameobj_pos(player_ids[player_no]);
from.x += muzzle.x * SCALE;
from.y += muzzle.y * SCALE;
if(pw->flags & WF_MUZZLEFLASH) {
static const vec2f flash_start[] = {
[DIR_O] = { 0.0, 1.0 },
[DIR_NO] = { 0.5, 6.0 },
[DIR_N] = { 1.0, 7.5 },
[DIR_NW] = { 6.0, 6.0 },
[DIR_W] = { 7.5, 1.0 },
[DIR_SW] = { 4.5, 0.0 },
[DIR_S] = { 1.0, 0.0 },
[DIR_SO] = { 0.0, 0.0 },
};
vec2f ffrom = from;
ffrom.x -= flash_start[dir].x * SCALE;
ffrom.y -= flash_start[dir].y * SCALE;
id = init_flash(&ffrom, dir);
if(id != -1) add_muzzleflash(id);
}
vel = velocity(&from, &to);
}
float dist = veclength(&vel);
float speed = pw->bullet_speed * SCALE;
const uint16_t range_tab[] = {0, 80, 66, 80, 118, 118, 118, 118, 118, 118,
200, 200, 240, 240, 240, 240, 240, 240, 240, 240, 320 };
float range = range_tab[pw->range] * SCALE;
if(dist > range)
dist = range;
float steps = dist / speed;
float deg = atan2(vel.y, vel.x);
vel.x = cos(deg) * speed;
vel.y = sin(deg) * speed;
switch(pw->shot) {
case ST_LAUNCHER:
id = init_rocket(dir, &from, &vel, steps);
break;
case ST_BULLET:
id = init_bullet(&from, &vel, steps);
if(id != -1) add_pbullet(id);
break;
case ST_FLAMES:
id = init_player_flame(dir, &from, &vel, steps);
if(id != -1) add_flame(id);
break;
case ST_GRENADE:
id = init_grenade(&from, &vel, steps);
add_grenade(id);
break;
default:
abort();
}
player_ammo[player_no][pw->ammo]--;
const WAVE_HEADER_COMPLETE *wf = wavesounds[pw->sound];
if(id != -1 && pw->sound != WS_NONE)
audio_play_wave_resource(wf);
}
static void init_game_objs() {
sblist_init(&go_crosshair, 1, 4);
sblist_init(&go_players, 1, 4);
sblist_init(&go_muzzleflash, 1, 4);
sblist_init(&go_player_bullets, 1, 32);
sblist_init(&go_flames, 1, 32);
sblist_init(&go_enemy_bullets, 1, 32);
sblist_init(&go_explosions, 1, 16);
sblist_init(&go_enemy_explosions, 1, 16);
sblist_init(&go_grenades, 1, 16);
sblist_init(&go_enemy_grenades, 1, 16);
sblist_init(&go_rockets, 1, 8);
sblist_init(&go_enemies, 1, 32);
sblist_init(&go_vehicles, 1, 4);
sblist_init(&go_mines, 1, 4);
sblist_init(&go_turrets, 1, 8);
sblist_init(&go_bunkers, 1, 4);
sblist_init(&go_boss, 1, 4);
sblist_init(&go_blood, 1, 16);
sblist_init(&go_enemy_flames, 1, 16);
init_player(0);
add_crosshair(init_crosshair());
init_map(current_map);
}
static int point_in_mask(vec2f *point, int obj_id) {
vec2f pos_in_pic = VEC((point->x - objs[obj_id].pos.x) / SCALE,
(point->y - objs[obj_id].pos.y) / SCALE);
const struct palpic *p = spritemaps[objs[obj_id].spritemap_id];
unsigned h = palpic_getspriteheight(p), w = palpic_getspritewidth(p);
if(pos_in_pic.x < 0 || pos_in_pic.y < 0 || pos_in_pic.x > w || pos_in_pic.y > h) return 0;
assert(objs[obj_id].anim_curr != ANIM_STEP_INIT);
anim_step curranim = objs[obj_id].anim_curr;
if(curranim == ANIM_STEP_INIT) curranim = get_next_anim_frame(objs[obj_id].animid, ANIM_STEP_INIT);
uint8_t *data = palpic_getspritedata(p, curranim);