-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstall_main.c
executable file
·1721 lines (1541 loc) · 40.6 KB
/
stall_main.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
/* stall - shoot them all game */
#include <stdio.h>
#include <stdlib.h> /* for malloc */
#include <string.h> /* for memset */
#include <time.h> /* for time */
#include <unistd.h> /* for usleep */
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <SDL_ttf.h>
#include "stall_types.h"
#include "stall_ini.h"
#include "stall_ai.h"
#include "stall_render.h"
void wave_inc();
void wave_end();
void stall_quit();
void stall_player_shot();
void stall_inc_score(unsigned int);
void stall_reset();
void dead_part_create(int, int);
void stall_objects_clear();
char *version = "0.83a";
global_t global;
player_ship_t *player_ship;
enemy_ship_t enemy_ship[MAX_ENEMIES];
bullet_t player_bullet[PLAYER_BULLETS] = {0}; /* all elements set to zero */
bullet_t enemy_bullet[ENEMY_BULLETS] = {0};
wave_t waves[WAVE_NUM] = {0};
bonus_t bonus[BONUS_NUM] = {0};
bonus_defshield_t bonus_defshield;
dead_parts_t dead_parts[TOTAL_DEAD_PARTS] = {0}; /* dead_parts v2.0 */
TTF_Font *font[5];
/* contains pressed keys. 1 - held, 0 - released */
enum keys_t keys[KEY_MAX] = {0};
/* --- menu functions --- */
void menu_down()
{
if (global.focus == FOCUS_MAX - 1)
global.focus = 0;
else
global.focus++;
}
void menu_up()
{
if (global.focus > FOCUS_START)
global.focus--;
else
global.focus = FOCUS_MAX - 1;
}
void menu_execute()
{
switch (global.focus)
{
case FOCUS_START:
{
global.state = STATE_WAVESTART;
global.wavestart_time = SDL_GetTicks();
debug("switched to STATE_WAVESTART\n");
break;
}
case FOCUS_OPTIONS:
{
break;
}
case FOCUS_EXIT:
{
stall_quit();
break;
}
default:
break;
}
}
/* --- keyboard functions --- */
void key_press(SDL_keysym *keysym)
{
//debug("press\n");
switch (keysym->sym)
{
case SDLK_ESCAPE:
{
stall_quit();
break;
}
case SDLK_LEFT:
{
keys[KEY_LEFT] = 1;
break;
}
case SDLK_RIGHT:
{
keys[KEY_RIGHT] = 1;
break;
}
case SDLK_UP:
{
keys[KEY_UP] = 1;
if (global.state == STATE_MENU)
menu_up();
break;
}
case SDLK_DOWN:
{
keys[KEY_DOWN] = 1;
if (global.state == STATE_MENU)
menu_down();
break;
}
case SDLK_SPACE:
{
if (global.state == STATE_MENU)
{
menu_execute();
break;
}
if (global.state == STATE_MAIN)
stall_player_shot();
break;
}
case SDLK_PAGEDOWN:
{
if (global.state == STATE_MAIN)
{
wave_end();
debug("level changed.\n");
}
break;
}
default:
break;
}
return;
}
void key_release(SDL_keysym *keysym)
{
//debug("release\n");
switch (keysym->sym)
{
case SDLK_ESCAPE:
{
stall_quit();
break;
}
case SDLK_LEFT:
{
keys[KEY_LEFT] = 0;
break;
}
case SDLK_RIGHT:
{
keys[KEY_RIGHT] = 0;
break;
}
case SDLK_UP:
{
keys[KEY_UP] = 0;
break;
}
case SDLK_DOWN:
{
keys[KEY_DOWN] = 0;
break;
}
default:
break;
}
return;
}
void stall_keyboard(void)
{
while (SDL_PollEvent(&global.event))
{
switch (global.event.type)
{
case SDL_KEYDOWN:
key_press(&global.event.key.keysym);
break;
case SDL_KEYUP:
key_release(&global.event.key.keysym);
break;
default:
break;
}
}
}
/* ---------------------------- destroying enemy functions ------------------ */
/* enemy gets killed by bullet - few dead parts created */
void stall_kill_enemy(int ship_num)
{
int i;
if (enemy_ship[ship_num].alive)
{
enemy_ship[ship_num].alive = 0;
global.enemies_alive--;
//debug("enemy #%d killed \n",ship_num);
if (global.state == STATE_MAIN)
stall_inc_score(ENEMY_COST);
/* create dead_parts. quantity according to current wave */
for (i = 0; i < waves[global.wave].dead_parts_num; i++)
{
dead_part_create(ship_num,i);
}
}
}
/* enemy gets crushed by player_ship - no dead parts created */
void stall_krash_enemy(int ship_num)
{
int i;
if (enemy_ship[ship_num].alive)
{
enemy_ship[ship_num].alive = 0;
global.enemies_alive--;
debug("enemy #%d krushed \n",ship_num);
if (global.state == STATE_MAIN)
stall_inc_score(ENEMY_COST);
if (bonus[BONUS_RAM].on_player)
return;
}
}
/* --- BONUS_DEF_SHIELD functions --- */
void bonus_defshield_move()
{
bonus_defshield.crd.x1 = player_ship->crd.x1 - BONUS_DEFSHIELD_SIZE;
bonus_defshield.crd.y1 = player_ship->crd.y1 - BONUS_DEFSHIELD_SIZE;
bonus_defshield.crd.x2 = player_ship->crd.x2 + BONUS_DEFSHIELD_SIZE;
bonus_defshield.crd.y2 = player_ship->crd.y2 + BONUS_DEFSHIELD_SIZE;
}
void bonus_defshield_setenergy(int energy)
{
bonus_defshield.energy = energy;
}
void bonus_defshield_blink()
{
static Uint32 cur_time, prev_time = 0;
if (!bonus_defshield.blinking)
return;
cur_time = SDL_GetTicks();
if (cur_time - prev_time > BONUS_DEFSHIELD_BLINK_FREQ)
{
bonus_defshield.show = !bonus_defshield.show;
prev_time = cur_time;
}
}
void bonus_defshield_create()
{
bonus_defshield_move();
bonus_defshield.show = 1;
bonus_defshield_setenergy(100);
}
void bonus_defshield_destroy()
{
bonus[BONUS_DEF_SHIELD].on_player = 0;
bonus_defshield.show = 0;
bonus_defshield.blinking = 0;
debug("defshield destroyed \n");
}
void bonus_defshield_decenergy(int energy)
{
bonus_defshield.energy -=energy;
debug("decenergy %d , remain: %d \n", energy, bonus_defshield.energy);
if (bonus_defshield.energy <= 0)
bonus_defshield_destroy();
}
/* --- END BONUS_DEF_SHIELD functions --- */
/* --- BONUS_RANDOMBOMB functions --- */
void bonus_randombomb()
{
int i, cnt = 0;
for (i = 0; i < MAX_ENEMIES; i++)
{
if (enemy_ship[i].alive)
{
stall_kill_enemy(i);
cnt++;
}
if (cnt == BONUS_RANDOMBOMB_ENEMIES)
break;
}
}
/* --- BONUS_RAM functions --- */
void bonus_ram_start()
{
player_ship->ram = 1;
}
void bonus_ram_stop()
{
player_ship->ram = 0;
}
/* --- BONUS_AIM functions --- */
/* need to find the closest one */
void bonus_aim_target(int num)
{
int i;
/* player coords */
int base_x = player_ship->crd.x1 + PLAYER_WIDTH/2;
int base_y = player_ship->crd.y1;
/* enemy coord */
int cur_target_pts, cur_target_num;
int target_pts = BONUS_AIM_ERRVALUE, target_num = BONUS_AIM_ERRVALUE;
for (i = 0; i < MAX_ENEMIES; i++)
{
if (enemy_ship[i].alive)
{
/* calculate distance (points) from base to target by x+y sum */
cur_target_pts = base_x - (enemy_ship[i].crd.x1 + waves[global.wave].enemy_width/2);
cur_target_pts += base_y - enemy_ship[i].crd.y2;
/* if we found the closer target - save its num and points */
if (cur_target_pts < target_pts)
{
target_pts = cur_target_pts;
target_num = i;
}
}
}
if (target_pts == BONUS_AIM_ERRVALUE || target_num == BONUS_AIM_ERRVALUE)
{
enemy_bullet[num].target_id = -1;
debug("error: no aim target found! \n");
}
else
{
enemy_bullet[num].target_id = target_num;
debug(" target found: #%d \n",target_num);
}
}
void bonus_aim_move()
{
//if (enemy_bullet[num].target_id > 0)
/*TODO: change bullet coords here (move) */
}
/* yes we decrease coord first and then check.
if value is negative - set it to 0.
so we need signed int coords
*/
void stall_player_move()
{
/* cant control dead ship */
if (!player_ship->alive)
return;
if (keys[KEY_LEFT])
{
player_ship->crd.x1 -= player_ship->speed;
player_ship->crd.x2 -= player_ship->speed;
if (player_ship->crd.x1 < 0)
{
player_ship->crd.x1 = 0;
player_ship->crd.x2 = PLAYER_WIDTH;
}
}
if (keys[KEY_RIGHT])
{
player_ship->crd.x1 += player_ship->speed;
player_ship->crd.x2 += player_ship->speed;
if (player_ship->crd.x2 > GAME_WIDTH)
{
player_ship->crd.x2 = GAME_WIDTH;
player_ship->crd.x1 = GAME_WIDTH - PLAYER_WIDTH;
}
}
if (keys[KEY_UP])
{
player_ship->crd.y1 -= player_ship->speed;
player_ship->crd.y2 -= player_ship->speed;
if (player_ship->crd.y1 < 0)
{
player_ship->crd.y1 = 0;
player_ship->crd.y2 = PLAYER_HEIGHT;
}
}
if (keys[KEY_DOWN])
{
player_ship->crd.y1 += player_ship->speed;
player_ship->crd.y2 += player_ship->speed;
if (player_ship->crd.y2 > GAME_HEIGHT)
{
player_ship->crd.y2 = GAME_HEIGHT;
player_ship->crd.y1 = GAME_HEIGHT - PLAYER_HEIGHT;
}
}
bonus_defshield_move();
}
void stall_player_ship_creator()
{
player_ship->crd.x1 = GAME_WIDTH/2;
player_ship->crd.y1 = GAME_HEIGHT/2 + 100;
player_ship->crd.x2 = player_ship->crd.x1 + PLAYER_WIDTH;
player_ship->crd.y2 = player_ship->crd.y1 + PLAYER_HEIGHT;
player_ship->speed = PLAYER_SPEED;
player_ship->bullet_speed = PLAYER_BULLET_SPEED;
player_ship->alive = 1;
player_ship->dead_frame = 0;
player_ship->ram = 0;
}
void stall_enemy_ship_creator()
{
if (global.enemies_alive < MAX_ENEMIES)
{
int i;
for (i = 0; i < MAX_ENEMIES; i++)
{
if (!enemy_ship[i].alive)
{
/* choose - appear from left or from right side */
int rnd = rand() % 3;
if (rnd == 0) /* from left side */
{
enemy_ship[i].crd.x1 = - waves[global.wave].enemy_width + 5;
enemy_ship[i].crd.y1 = rand() % (GAME_HEIGHT/2);
enemy_ship[i].move = MOVE_RIGHT;
enemy_ship[i].goal_x = rand() % (GAME_WIDTH - waves[global.wave].enemy_width);
enemy_ship[i].goal_y = enemy_ship[i].crd.y1;
}
if (rnd == 1) /* from right side */
{
enemy_ship[i].crd.x1 = GAME_WIDTH - 5;
enemy_ship[i].crd.y1 = rand() % (GAME_HEIGHT/2);
enemy_ship[i].move = MOVE_LEFT;
enemy_ship[i].goal_x = rand() % (GAME_WIDTH - waves[global.wave].enemy_width);
enemy_ship[i].goal_y = enemy_ship[i].crd.y1;
}
if (rnd == 2) /* from top side */
{
enemy_ship[i].crd.x1 = rand() % (GAME_WIDTH - waves[global.wave].enemy_width);
enemy_ship[i].crd.y1 = - waves[global.wave].enemy_height + 5;
enemy_ship[i].move = MOVE_DOWN;
enemy_ship[i].goal_x = enemy_ship[i].crd.x1;
enemy_ship[i].goal_y = rand() % 400;
}
/* enemy_ship[i].crd.x1 = rand() % (GAME_WIDTH - waves[global.wave].enemy_width);
enemy_ship[i].move = MOVE_DONT; */
/* common */
enemy_ship[i].crd.x2 = enemy_ship[i].crd.x1 + waves[global.wave].enemy_width;
enemy_ship[i].crd.y2 = enemy_ship[i].crd.y1 + waves[global.wave].enemy_height;
enemy_ship[i].speed = waves[global.wave].enemy_speed;
enemy_ship[i].bullet_speed = ENEMY_BULLET_SPEED;
enemy_ship[i].mental = rand() % MENTAL_MAX;
enemy_ship[i].in_action = 1;
enemy_ship[i].alive = 1;
enemy_ship[i].anim = 0;
enemy_ship[i].time_anim = 0;
break;
}
}
global.enemies_alive++;
debug("enemy created. alive: %u, speed %u, mental %d \n",
global.enemies_alive, enemy_ship[i].speed, enemy_ship[i].mental);
}
}
void wave_inc()
{
global.wave++;
}
/* called once when wave is ended */
void wave_end()
{
int i;
debug("wave end(). #%u \n", global.wave);
/* this is STATE_WAVEEND from now and next 5 seconds */
global.state = STATE_WAVEEND;
global.waveend_time = SDL_GetTicks();
/* kill all enemies */
for (i = 0; i < MAX_ENEMIES; i++)
{
stall_kill_enemy(i);
}
for (i = 0; i < BONUS_NUM; i++)
{
bonus[i].active = 0;
}
global.phase = 0;
}
/* creating enemies, switching phases, switch state to STATE_WAVEEND */
void wave_live()
{
static Uint32 prev_time = 0;
static Uint32 phase_duration, phase_enemyfreq, phase_starttime;
static Uint32 phase_start = 1; /* we have just switched to this phase */
Uint32 cur_time = SDL_GetTicks();
/* switch to STATE_WAVEEND or not */
if (cur_time - waves[global.wave].start_time > waves[global.wave].duration)
{
if (player_ship->alive)
{
wave_end();
return;
}
}
/* phase init - do it just once per phase */
if (phase_start)
{
phase_duration = waves[global.wave].phases[global.phase][0] * 1000;
phase_enemyfreq = waves[global.wave].phases[global.phase][1];
phase_starttime = cur_time;
phase_start = 0;
}
/* check - is it time to create enemy? */
if ((cur_time - prev_time) >= phase_enemyfreq)
{
stall_enemy_ship_creator();
prev_time = cur_time;
}
/* check - is it time to switch to another phase? */
if ((cur_time - phase_starttime) >= phase_duration)
{
/* additional check to avoid bug which can switch to phase6 */
if (global.phase < WAVE_PHASES - 1)
{
phase_start = 1;
global.phase++;
debug("phase: %u/%d. duration: %d sec, enemy freq: %d ms\n",
global.phase, WAVE_PHASES, waves[global.wave].phases[global.phase][0],
waves[global.wave].phases[global.phase][1]);
}
}
}
/* check every second - is it time to create new bonus */
void bonus_creator()
{
int i, rnd;
static Uint32 cur_time, prev_time = 0;
if (global.state != STATE_MAIN)
return;
/* is it time to create bonus? */
cur_time = SDL_GetTicks();
if ((cur_time - prev_time) >= 1000)
{
for (i = 0; i < BONUS_NUM; i++)
{
rnd = rand() % 100;
//debug("bonus_creator() rnd %d \n", rnd);
/* do not create bonus if we already have it on screen */
if (!bonus[i].active && rnd < bonus[i].chance[global.wave])
{
/* creating bonus! */
bonus[i].active = 1;
bonus[i].scr_lifetime = bonus[i].scr_duration;
bonus[i].crd.x1 = rand() % (GAME_WIDTH - BONUS_WIDTH);
bonus[i].crd.x2 = bonus[i].crd.x1 + BONUS_WIDTH;
bonus[i].crd.y1 = rand() % (GAME_HEIGHT - BONUS_HEIGHT);
bonus[i].crd.y2 = bonus[i].crd.y1 + BONUS_HEIGHT;
/*debug("bonus created! #%d ,x1 %d, y1 %d\n",
i,bonus[i].crd.x1, bonus[i].crd.y1);*/
}
}
prev_time = cur_time;
}
}
/* check every sec - is it time to decrease bonus display_time or stop bonus */
void bonus_disappear()
{
int i;
static Uint32 cur_time, prev_time = 0;
/* is it time to disappear/stop bonus? */
cur_time = SDL_GetTicks();
if ((cur_time - prev_time) >= 1000)
{
for (i = 0; i < BONUS_NUM; i++)
{
if (bonus[i].active)
{
if (bonus[i].scr_lifetime > 0)
{
bonus[i].scr_lifetime--;
/* debug("#%d : dec scr_lifetime %d \n",
i, bonus[i].scr_lifetime);*/
}
else
{
bonus[i].active = 0;
debug("bonus_disappear #%d \n", i);
}
}
if (bonus[i].on_player)
{
if (bonus[i].plr_lifetime > 0)
{
bonus[i].plr_lifetime--;
if (i == BONUS_DEF_SHIELD &&
bonus[i].plr_lifetime == BONUS_DEFSHIELD_BLINKING_TIME)
bonus_defshield.blinking = 1;
/* debug("#%d : dec plr_lifetime %d \n",
i, bonus[i].plr_lifetime); */
}
else
{
bonus[i].on_player = 0;
if (i == BONUS_RAM)
bonus_ram_stop();
else if (i == BONUS_ACCELERATION)
player_ship->speed = PLAYER_SPEED;
}
}
}
prev_time = cur_time;
}
}
void stall_bullets_move()
{
int i;
/* move player bullets */
for (i = 0; i < PLAYER_BULLETS; i++)
{
if (player_bullet[i].exist)
{
player_bullet[i].crd.y1 -= player_ship->bullet_speed;
player_bullet[i].crd.y2 -= player_ship->bullet_speed;
if (player_bullet[i].btype == BULLET_TYPE_RIGHT)
{
player_bullet[i].crd.x1 +=1;
player_bullet[i].crd.x2 +=1;
}
else if (player_bullet[i].btype == BULLET_TYPE_LEFT)
{
player_bullet[i].crd.x1 -=1;
player_bullet[i].crd.x2 -=1;
}
if (player_bullet[i].crd.y2 < 1 || player_bullet[i].crd.x1 < 1
|| player_bullet[i].crd.x2 > GAME_WIDTH )
{
player_bullet[i].exist = 0;
}
}
}
/* move enemy bullets and destroy them if out of field */
for (i = 0; i < ENEMY_BULLETS; i++)
{
if (enemy_bullet[i].exist)
{
int id = enemy_bullet[i].ship_id;
enemy_bullet[i].crd.y1 += enemy_ship[id].bullet_speed;
enemy_bullet[i].crd.y2 += enemy_ship[id].bullet_speed;
if (enemy_bullet[i].crd.y1 > GAME_HEIGHT)
{
enemy_bullet[i].exist = 0;
enemy_bullet[i].anim = 0;
enemy_bullet[i].time_anim = 0;
global.enemy_bullets--;
if (player_ship->alive)
stall_inc_score(BULLET_COST);
}
}
}
}
void stall_player_shot()
{
int i;
static int triple_shot_cnt = 0;
if (!player_ship->alive)
return;
for (i = 0; i < PLAYER_BULLETS; i++)
{
if (!player_bullet[i].exist)
{
player_bullet[i].crd.x1 = player_ship->crd.x1 + PLAYER_WIDTH/2;
player_bullet[i].crd.y1 = player_ship->crd.y1 - 1;
player_bullet[i].crd.x2 = player_bullet[i].crd.x1 + PLAYER_BULLET_WIDTH;
player_bullet[i].crd.y2 = player_bullet[i].crd.y1 + PLAYER_BULLET_HEIGHT;
player_bullet[i].btype = triple_shot_cnt;
player_bullet[i].exist = 1;
if (bonus[BONUS_TRIPLE_SHOT].on_player)
{
/* we already launched 3 bullets, so break */
if (triple_shot_cnt == 2)
{
triple_shot_cnt = 0;
break;
}
triple_shot_cnt++;
continue;
}
if (bonus[BONUS_AUTO_AIM_MISSILES].on_player)
{
player_bullet[i].btype = BULLET_TYPE_MISSILE;
bonus_aim_target(i);
}
break;
}
}
}
void stall_enemy_shot()
{
int i, j;
for (i = 0; i < MAX_ENEMIES; i++)
{
if (enemy_ship[i].alive)
{
int random = rand() % 200;
if (random == 77)
{
for (j = 0; j < ENEMY_BULLETS; j++)
{
if (!enemy_bullet[j].exist)
{
enemy_bullet[j].crd.x1 = enemy_ship[i].crd.x1 + waves[global.wave].enemy_width/2;
enemy_bullet[j].crd.y1 = enemy_ship[i].crd.y2 + 1;
enemy_bullet[j].crd.x2 = enemy_bullet[j].crd.x1 + ENEMY_BULLET_WIDTH;
enemy_bullet[j].crd.y2 = enemy_bullet[j].crd.y1 + ENEMY_BULLET_HEIGHT;
enemy_bullet[j].btype = BULLET_TYPE_CENTER;
enemy_bullet[j].exist = 1;
enemy_bullet[j].time_anim = SDL_GetTicks();
global.enemy_bullets++;
// debug("enemy bullet: %u\n",global.enemy_bullets);
break;
}
}
}
}
}
}
/* --- Math --- */
/* inc when enemy killed or dodge from bullet */
void stall_inc_score(unsigned int score)
{
//debug("inc_score() +%d \n", score);
global.score += score;
}
#if 0
/* returns: 1 if we have a collision, 0 if not.
x1, y1, x2, y2 */
int stall_check_collision_old ( int left1, int top1, int right1, int bottom1,
int left2, int top2, int right2, int bottom2 )
{
if (bottom1 < top2 || top1 > bottom2 || right1 < left2 || left1 > right2 )
return 0;
/* here we have collision */
//debug("l1 %d, t1 %d, r1 %d, b1 %d , l2 %d, t2 %d, r2 %d, b2 %d \n",
// left1, top1, right1, bottom1, left2, top2, right2, bottom2);
return 1;
}
#endif
/* returns: 1 if we have a collision, 0 if not */
int stall_check_collision (rect_t rect1, rect_t rect2)
{
if (rect1.y2 < rect2.y1 || rect1.y1 > rect2.y2 || rect1.x2 < rect2.x1 || rect1.x1 > rect2.x2)
return 0;
/* here we have collision */
return 1;
}
/* if player took bonus - `on player` enabled */
void bonus_check()
{
int i, j;
for (i = 0; i < BONUS_NUM; i++)
{
if (bonus[i].active)
{
if (player_ship->alive)
{
if (stall_check_collision(player_ship->crd, bonus[i].crd))
{
debug("bonus_check() : bonus was taken %d \n", i);
bonus[i].active = 0;
bonus[i].on_player = 1;
bonus[i].plr_lifetime = bonus[i].plr_duration;
if (i == BONUS_DEF_SHIELD)
bonus_defshield_create();
else if (i == BONUS_RANDOM_BOMB)
{
bonus_randombomb();
}
else if (i == BONUS_RAM)
{
bonus_ram_start();
}
else if (i == BONUS_ACCELERATION)
{
player_ship->speed = PLAYER_ACC_SPEED;
}
}
}
/* enemies can eat the bonus! */
for (j = 0; j < MAX_ENEMIES; j++)
{
if (enemy_ship[j].alive)
{
if (stall_check_collision(enemy_ship[j].crd, bonus[i].crd))
bonus[i].active = 0;
}
}
}
}
}
/* --- dead_part v2.0 functions --- */
/* creates 1 dead part when enemy gets killed
params:
@ ship_number - number of parent ship (to get coords),
@ part_number - number of part (from 1 to 4)
*/
void dead_part_create(int ship_number,int part_number)
{
int i;
for (i = 0; i < TOTAL_DEAD_PARTS; i++)
{
if (!dead_parts[i].alive)
{
// debug("dp create()ship %d, part_n %d ,num %d\n",ship_number, part_number, i);
dead_parts[i].alive = 1;
dead_parts[i].speed = waves[global.wave].enemy_speed;
switch (global.wave)
{
case 0:
{
if (part_number == 0) /* top */
{
/* set coords of new part based on parent ship coords */
dead_parts[i].crd.x1 = enemy_ship[ship_number].crd.x1;
dead_parts[i].crd.y1 = enemy_ship[ship_number].crd.y1;
dead_parts[i].crd.x2 = enemy_ship[ship_number].crd.x2;
dead_parts[i].crd.y2 = enemy_ship[ship_number].crd.y1 + waves[global.wave].enemy_height/2;
/* set part of the sprite for the current part */
dead_parts[i].pic.x = 0;
dead_parts[i].pic.y = 0;
dead_parts[i].pic.w = waves[global.wave].enemy_width;
dead_parts[i].pic.h = waves[global.wave].enemy_height/2;
/* set way to move */
dead_parts[i].way[0] = 0; //x
dead_parts[i].way[1] = -1; //y
}
else if (part_number == 1) /* bottom right */
{
/* set coords of new part based on parent ship coords */
dead_parts[i].crd.x1 = enemy_ship[ship_number].crd.x1 + waves[global.wave].enemy_width/2;
dead_parts[i].crd.y1 = enemy_ship[ship_number].crd.y1 + waves[global.wave].enemy_height/2;
dead_parts[i].crd.x2 = enemy_ship[ship_number].crd.x2;
dead_parts[i].crd.y2 = enemy_ship[ship_number].crd.y2;
/* set part of the sprite for the current part */
dead_parts[i].pic.x = waves[global.wave].enemy_width/2;
dead_parts[i].pic.y = waves[global.wave].enemy_height/2;
dead_parts[i].pic.w = waves[global.wave].enemy_width/2;
dead_parts[i].pic.h = waves[global.wave].enemy_height/2;
/* set way to move */
dead_parts[i].way[0] = 1; //x
dead_parts[i].way[1] = 0; //y
}
else if (part_number == 2)
{
/* set coords of new part based on parent ship coords */
dead_parts[i].crd.x1 = enemy_ship[ship_number].crd.x1;
dead_parts[i].crd.y1 = enemy_ship[ship_number].crd.y1 + waves[global.wave].enemy_height/2;
dead_parts[i].crd.x2 = enemy_ship[ship_number].crd.x1 + waves[global.wave].enemy_width/2;
dead_parts[i].crd.y2 = enemy_ship[ship_number].crd.y2;
/* set part of the sprite for the current part */
dead_parts[i].pic.x = 0;
dead_parts[i].pic.y = waves[global.wave].enemy_height/2;
dead_parts[i].pic.w = waves[global.wave].enemy_width/2;
dead_parts[i].pic.h = waves[global.wave].enemy_height/2;
/* set way to move */
dead_parts[i].way[0] = -1; //x
dead_parts[i].way[1] = 0; //y
}
break;
}
case 1:
{
if (part_number == 0) /* top left */
{
/* set coords of new part based on parent ship coords */
dead_parts[i].crd.x1 = enemy_ship[ship_number].crd.x1;
dead_parts[i].crd.y1 = enemy_ship[ship_number].crd.y1;
dead_parts[i].crd.x2 = dead_parts[i].crd.x1 + waves[global.wave].enemy_width/2;
dead_parts[i].crd.y2 = dead_parts[i].crd.y1 + waves[global.wave].enemy_height/2;
/* set part of the sprite for the current part */
dead_parts[i].pic.x = 0;
dead_parts[i].pic.y = 0;
dead_parts[i].pic.w = waves[global.wave].enemy_width/2;
dead_parts[i].pic.h = waves[global.wave].enemy_height/2;
/* set way to move */
dead_parts[i].way[0] = -1; //x
dead_parts[i].way[1] = -1; //y
}
else if (part_number == 1) /* top right */
{
/* set coords of new part based on parent ship coords */
dead_parts[i].crd.x1 = enemy_ship[ship_number].crd.x1 + waves[global.wave].enemy_width/2;
dead_parts[i].crd.y1 = enemy_ship[ship_number].crd.y1;
dead_parts[i].crd.x2 = enemy_ship[ship_number].crd.x2;
dead_parts[i].crd.y2 = enemy_ship[ship_number].crd.y1 + waves[global.wave].enemy_height/2;
/* set part of the sprite for the current part */
dead_parts[i].pic.x = waves[global.wave].enemy_width/2;
dead_parts[i].pic.y = 0;
dead_parts[i].pic.w = waves[global.wave].enemy_width/2;
dead_parts[i].pic.h = waves[global.wave].enemy_height/2;
/* set way to move */
dead_parts[i].way[0] = 1; //x
dead_parts[i].way[1] = -1; //y
}
else if (part_number == 2) /* bottom right */
{
/* set coords of new part based on parent ship coords */
dead_parts[i].crd.x1 = enemy_ship[ship_number].crd.x1 + waves[global.wave].enemy_width/2;
dead_parts[i].crd.y1 = enemy_ship[ship_number].crd.y1 + waves[global.wave].enemy_height/2;