-
Notifications
You must be signed in to change notification settings - Fork 10
/
invaders.c
1125 lines (1039 loc) · 31 KB
/
invaders.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
/**
* ascii invaders - A curses clone of the classic video game Space Invaders
* (c) 2001 Thomas Munro
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* https://github.com/macdice/ascii-invaders
* Thomas Munro <[email protected]>
*
* $Id: invaders.c,v 1.20 2002/07/21 21:52:13 munro Exp $
*/
#include "invaders.h"
#include <curses.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <time.h>
static const Sprite sprites[][2][2] = {
[ALIEN30] = {
[ASCII] = {
{
{
" {@@} ",
" /\"\"\\ ",
" "
}
},
{
{
" {@@} ",
" \\/ ",
" "
}
}
},
[UNICODE] = {
{
{
"⢀⡴⣿⢦⡀ ",
"⢈⢝⠭⡫⡁ ",
" "
}
},
{
{
"⢀⡴⣿⢦⡀ ",
"⠨⡋⠛⢙⠅ ",
" "
}
}
}
},
[ALIEN20] = {
[ASCII] = {
{
{
" dOOb ",
" ^/\\^ ",
" "
}
},
{
{
" dOOb ",
" ~||~ ",
" "
}
}
},
[UNICODE] = {
{
{
"⢀⡵⣤⡴⣅ ",
"⠏⢟⡛⣛⠏⠇",
" "
}
},
{
{
"⣆⡵⣤⡴⣅⡆",
"⢘⠟⠛⠛⢟⠀",
" "
}
}
},
},
[ALIEN10] = {
[ASCII] = {
{
{
" /MM\\ ",
" |~~| ",
" "
}
},
{
{
" /MM\\ ",
" \\~~/ ",
" "
}
}
},
[UNICODE] = {
{
{
"⣴⡶⢿⡿⢶⣦",
"⠩⣟⠫⠝⣻⠍",
" "
}
},
{
{
"⣴⡶⢿⡿⢶⣦",
"⣉⠽⠫⠝⠯⣉",
" "
}
}
}
},
[MA] = {
[ASCII] = {
{
{
"_/MMM\\_",
"qWAVAWp"
}
}
},
[UNICODE] = {
{
{
"⢀⡴⣾⢿⡿⣷⢦⡀",
"⠉⠻⠋⠙⠋⠙⠟⠉"
}
}
}
},
[GUNNER] = {
[ASCII] = {
{
{
" mAm ",
" MAZAM "
}
}
},
[UNICODE] = {
{
{
" ⢀⣀⣾⣷⣀⡀ ",
" ⣿⣿⣿⣿⣿⣿ "
}
}
}
},
[GUNNER_EXPLODE] = {
[ASCII] = {
{
{
" ,' % ",
" ;&+,! "
}
},
{
{
" -,+$! ",
" + ^~ "
}
}
},
[UNICODE] = {
/* TODO */
{
{
" ,' % ",
" ;&+,! "
}
},
{
{
" -,+$! ",
" + ^~ "
}
}
}
},
[ALIEN_EXPLODE] = {
[ASCII] = {
{
{
" \\||/ ",
" /||\\ ",
" "
}
}
},
[UNICODE] = {
/* TODO */
{
{
" \\||/ ",
" /||\\ ",
" "
}
}
}
},
[SHELTER] = {
[ASCII] = {
{
{
"/MMMMM\\",
"MMMMMMM",
"MMM MMM"
}
}
},
[UNICODE] = {
/* TODO */
{
{
"/MMMMM\\",
"MMMMMMM",
"MMM MMM"
}
}
}
}
};
const char *alienBlank = " ";
const char *bombAnim = "\\|/-";
static int ctype = ASCII;
// We have to use global variables becase our main loop is driven by
// an alarm signal - so we put them into tidy structures.
// TODO! Define the structs, but declare the game state objects on
// the stack of main. Down with globalisation!
struct {
int score;
int lives;
int state;
int screenCols; // screen columns
int screenRows; // screen rows
int timer; // timer used to switch between game states
struct itimerval myTimer; // alarm signal timer
} game;
struct {
int rows, cols; // how many rows and columns of aliens are there?
int x, y; // alien position
int *table; // the table of aliens
// which rows and columns have been cleared?
int emptyLeft, emptyRight, emptyTop, emptyBottom;
int direction;
int paintRow; // cursor for repainting row by row
int paintWait; // counter for repainting row by row
int sideAnim; // staggered sideways movement
int count; // how many aliens are left?
int anim; // used for wiggling
struct Bomb *headBomb; // linked list of bombs
} aliens;
struct {
int x; // x position
int move; // buffered moves
int explodeTimer; // timer used when gunner explodes
struct {
int x; // x position
int y; // y position
} missile;
char *shields; // pointer to shield table
} gun;
struct {
int x; // x position
int pointsTimer; // how long to leave the points on the screen
} ma;
void paintShelters();
void resetAliens();
void resetShields();
void initGame();
void cleanUp(int signal);
void handleTimer(int signal);
void paintAlienRow(int row, int clean);
void paintGunner();
void paintIntro();
void paintExplodingAlien(int y, int x);
void removeAlien(int y, int x);
void paintScore();
void trimAliens();
void addBomb(int x, int y);
int removeBomb(struct Bomb *b);
void freeBombs();
void moveAliensDown();
int main(int argc, char **argv) {
if (argc > 1 && strcmp(argv[1], "--unicode") == 0) {
setlocale(LC_CTYPE, "");
ctype = UNICODE;
}
// set up curses library
initscr();
cbreak();
noecho();
#ifdef USE_COLORS
curs_set(0); // hide cursor
#endif
initscr();
cbreak();
noecho();
#ifdef USE_KEYS
keypad(stdscr, TRUE);
#endif
#ifdef USE_COLORS
if (has_colors()) {
start_color();
init_pair(0, COLOR_BLACK, COLOR_BLACK);
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(3, COLOR_CYAN, COLOR_BLACK);
init_pair(4, COLOR_WHITE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_BLUE, COLOR_BLACK);
init_pair(7, COLOR_YELLOW, COLOR_BLACK);
}
#endif
game.screenCols = COLS;
initGame();
paintIntro();
game.state = STATE_INTRO;
// TODO!
// Instead of registering handleTime here, we should use select
// to wait for a key with a timeout, and run handleTimer when
// enough time for one frame has elapsed.
// set up realtime interrupt timer and signals
game.myTimer.it_value.tv_sec = 0;
game.myTimer.it_value.tv_usec = 1000000 / FPS;
game.myTimer.it_interval.tv_sec = 0;
game.myTimer.it_interval.tv_usec = 1000000 / FPS;
setitimer(ITIMER_REAL, &game.myTimer, NULL);
signal(SIGALRM, handleTimer);
for (;;) {
switch(getch()) {
case 'q':
cleanUp(0);
break;
case 'j':
#ifdef USE_KEYS
case KEY_LEFT:
#endif
gun.move = -2;
break;
case 'k':
#ifdef USE_KEYS
case KEY_RIGHT:
#endif
gun.move = 2;
break;
case ' ':
if (game.state == STATE_INTRO) {
game.lives = 3;
game.score = 0;
game.state = STATE_PLAY;
resetShields();
resetAliens();
game.timer = 0;
clear();
paintShelters();
} else if (game.state == STATE_PLAY
&& game.timer > GUNNER_ENTRANCE
&& gun.missile.y == 0) {
gun.missile.x = gun.x;
gun.missile.y = LINES - GUNNER_HEIGHT - 1;
}
break;
}
}
return 0; // not reached
}
void paintShelters() {
int n, y, x;
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(1));
#endif
n = 0;
for (y = 0; y < SHELTER_HEIGHT; y++) {
move(LINES - 1 - SHELTER_HEIGHT - GUNNER_HEIGHT + y, 0);
for (x = 0; x < game.screenCols; x++) {
addch(gun.shields[n++]);
}
}
refresh();
}
/**
* Handle timer signal. This is the logic that moves all the sprites
* around the screen.
*/
void handleTimer(int signal) {
// TODO! This should take a pointer to the game state, which points
// to object on the stack of main, instead of accessing evil
// global variables.
int x;
struct Bomb *b;
// check which state the game is in
if (game.state == STATE_INTRO) {
// intro anim
return;
} else if (game.state == STATE_WAIT) {
if (game.timer++ == 10) {
game.state = STATE_PLAY;
paintScore();
}
return;
} else if (game.state == STATE_GAMEOVER) {
if (game.timer++ == 40) {
game.state = STATE_INTRO;
paintIntro();
refresh();
}
// game over anim
return;
} else if (game.state == STATE_EXPLODE) {
// explode anim
if (gun.explodeTimer++ % 4 == 0) {
paintGunner();
}
if (gun.explodeTimer == 40) {
if (game.lives-- == 0) {
// game over
game.state = STATE_GAMEOVER;
game.timer = 0;
mvprintw(LINES / 2, (COLS / 2) - 5, "GAME OVER");
refresh();
return;
} else {
// start next life
// if the aliens have reached the shields
// then move them back up to the top of the screen
int lastLine = aliens.y + (ALIEN_HEIGHT * aliens.emptyBottom) - 1;
int shieldTop = LINES - GUNNER_HEIGHT - SHELTER_HEIGHT - 1;
if (lastLine >= shieldTop) aliens.y = 0;
game.timer = 0;
game.state = STATE_WAIT;
ma.x = 0; // just in case she was there at the time
freeBombs(); // get rid of any bombs
clear();
paintShelters();
refresh();
aliens.paintRow = aliens.rows;
return;
}
}
}
// otherwise handle game play...
game.timer++;
if (game.timer == GUNNER_ENTRANCE && game.state == STATE_PLAY) {
paintGunner();
}
// decide if it's time to send on ma
if (game.timer % MA_ENTRANCE == MA_ENTRANCE - 1 && aliens.y > 5) {
ma.x = COLS - ALIEN_WIDTH - 1;
}
// if ma is currently on, display
if (ma.x > 0) {
int i;
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(2));
#endif
if (ma.pointsTimer != 0) {
// ma has been shot and is now just showing points
if (ma.pointsTimer++ == 20) {
mvprintw(2, ma.x, "%s", alienBlank);
ma.pointsTimer = 0;
ma.x = 0;
}
} else {
// ma is grooving across the top of the screen
ma.x--;
for (i = 0; i < MA_HEIGHT; i++)
mvprintw(2 + i, ma.x, "%s ", sprites[MA][ctype][0].lines[i]);
refresh();
// if we have reach the edge then remove ma
if (ma.x == 0) {
for (i = 0; i < MA_HEIGHT; i++)
mvprintw(2 + i, ma.x, "%s ", alienBlank);
}
}
}
// drop bombs
if (game.timer > GUNNER_ENTRANCE) {
for (x = aliens.emptyLeft; x < aliens.emptyRight; x++) {
int y;
// find the first alien from the bottom
for (y = aliens.emptyBottom - 1; y >= 0; y--) {
if (aliens.table[(aliens.cols * y) + x] > ALIEN_EMPTY) {
if (1 == (int) (50.0 * rand() / (RAND_MAX + 1.0))) {
addBomb(aliens.x + (x * ALIEN_WIDTH) + (ALIEN_WIDTH / 2),
aliens.y + (y * ALIEN_HEIGHT) + ALIEN_HEIGHT - 1);
}
break;
}
}
}
}
// handle gunner movements
if (game.state == STATE_PLAY && game.timer > GUNNER_ENTRANCE) {
if (gun.move < 0 && gun.x > GUNNER_WIDTH / 2) {
gun.move++;
gun.x--;
paintGunner();
refresh();
}
if (gun.move > 0 && gun.x < COLS - (GUNNER_WIDTH / 2)) {
gun.move--;
gun.x++;
paintGunner();
refresh();
}
}
// handle alien movements
if (--aliens.paintWait <= 0) {
// time to repaint one row of aliens (speeds up as you shoot aliens)
aliens.paintWait = (int) ((double) PAINT_WAIT * ((double) aliens.count
/ (double) (aliens.cols * aliens.rows)));;
aliens.paintRow--;
paintAlienRow(aliens.paintRow, 0);
refresh();
if (aliens.paintRow <= aliens.emptyTop) {
// time to move the block of aliens
aliens.paintRow = aliens.emptyBottom; // reset counter
aliens.anim = (aliens.anim ? 0 : 1); // wiggle
if (aliens.direction == -1) {
if (--aliens.x + (aliens.emptyLeft * ALIEN_WIDTH) == 0) {
// change direction, clear top line, shuffle down
aliens.direction = 1;
move(aliens.y, 0);
clrtoeol();
moveAliensDown();
}
} else if (aliens.direction == 1) {
if (++aliens.x + (aliens.emptyRight * ALIEN_WIDTH) == COLS) {
// change direction, clear top line, shuffle down
aliens.direction = -1;
move(aliens.y, 0);
clrtoeol();
moveAliensDown();
}
}
paintScore();
/*
// see if the aliens have hit the bottom
if (game.state == STATE_PLAY
&& aliens.y + (ALIEN_HEIGHT *
(aliens.rows - aliens.emptyBottom))
> LINES - 1 - GUNNER_HEIGHT - SHELTER_HEIGHT) {
game.state = STATE_EXPLODE;
gun.explodeTimer = 0;
}
*/
}
}
#ifdef USE_COLORS
// use white for missiles and bombs
if (has_colors()) attron(COLOR_PAIR(4));
#endif
// handle bomb movements
for (b = aliens.headBomb; b != NULL; ) {
struct Bomb *next = b->next;
move(b->y, b->x);
addch(' ');
if (++(b->y) < LINES) {
if (gun.missile.y != 0
&& abs(b->x - gun.missile.x) < 2
&& abs(b->y - gun.missile.y) < 2) {
// collision with missile
removeBomb(b);
move(gun.missile.y, gun.missile.x);
addch(' ');
gun.missile.y = 0;
} else if (game.state == STATE_PLAY
&& game.timer > GUNNER_ENTRANCE
&& b->y >= LINES - GUNNER_HEIGHT
&& b->x > (gun.x - (GUNNER_WIDTH / 2))
&& b->x < (gun.x + (GUNNER_WIDTH / 2))) {
// collision with gunner
removeBomb(b);
#ifndef BULLET_PROOF
game.state = STATE_EXPLODE;
gun.explodeTimer = 0;
#endif
} else if (b->y < LINES - 1 - GUNNER_HEIGHT
&& b->y >= LINES - 1 - GUNNER_HEIGHT - SHELTER_HEIGHT
&& gun.shields[((b->y - (LINES - 1 - GUNNER_HEIGHT - SHELTER_HEIGHT))
* game.screenCols) + b->x] != ' ') {
// collision with shield
gun.shields[((b->y - (LINES - 1 - GUNNER_HEIGHT
- SHELTER_HEIGHT))
* game.screenCols) + b->x] = ' ';
mvaddch(b->y, b->x, ' ');
removeBomb(b);
} else {
// advance bomb
move(b->y, b->x);
addch(bombAnim[b->anim++]);
if (b->anim == BOMB_ANIM_SIZE) {
b->anim = 0;
}
}
} else {
removeBomb(b);
}
b = next;
refresh();
}
// handle missile movements
if (gun.missile.y != 0) {
move(gun.missile.y, gun.missile.x);
addch(' ');
if ((gun.missile.y -= 2) > 0) {
move(gun.missile.y, gun.missile.x);
addch('!');
} else {
gun.missile.y = 0;
}
// test for collision with shield
if (gun.missile.y < LINES - 1 - GUNNER_HEIGHT
&& gun.missile.y >= LINES - 1 - GUNNER_HEIGHT
- SHELTER_HEIGHT) {
if (gun.shields[((gun.missile.y - (LINES - 1 - GUNNER_HEIGHT
- SHELTER_HEIGHT))
* game.screenCols) + gun.missile.x] != ' ') {
gun.shields[((gun.missile.y - (LINES - 1 - GUNNER_HEIGHT
- SHELTER_HEIGHT))
* game.screenCols) + gun.missile.x] = ' ';
mvaddch(gun.missile.y, gun.missile.x, ' ');
gun.missile.y = 0;
}
}
// test for collision with aliens
else if (gun.missile.x >= aliens.x && gun.missile.x < aliens.x
+ (ALIEN_WIDTH * aliens.cols)
&& gun.missile.y < aliens.y + ALIEN_HEIGHT * aliens.rows &&
gun.missile.y >= aliens.y) {
int alien;
int x = gun.missile.x - aliens.x;
int y = gun.missile.y - aliens.y;
if (x % ALIEN_WIDTH != 0 && x % ALIEN_WIDTH != ALIEN_WIDTH -1) {
// it didn't sneak between two aliens
x /= ALIEN_WIDTH;
y /= ALIEN_HEIGHT;
alien = aliens.table[(y * aliens.cols) + x];
if (alien > ALIEN_EMPTY) {
game.score += alien * 10;
paintExplodingAlien(y, x);
paintScore();
gun.missile.y = 0; // no more missile
aliens.table[(y * aliens.cols) + x] = ALIEN_EXPLODE1;
if (--aliens.count == 0) {
resetAliens();
game.timer = 0;
clear();
paintShelters();
}
refresh();
trimAliens();
}
}
}
// test for collection with ma
else if (ma.x != 0
&& gun.missile.y <= 2 + MA_HEIGHT
&& gun.missile.x >= ma.x
&& gun.missile.x <= ma.x + ALIEN_WIDTH) {
// chose a 'random' number of points, either 50, 100 or 150
int points = ((time(0) % 3) + 1) * 50;
int i;
game.score += points;
// remove ma
for (i = 0; i < MA_HEIGHT; i++)
mvprintw(2 + i, ma.x, "%s ", alienBlank);
// draw the number of points
mvprintw(2, ma.x, " %d", points);
ma.pointsTimer = 1;
paintScore();
}
refresh();
}
}
/**
* Move aliens down one row.
*/
void moveAliensDown() {
// figure out which screen row the bottom alien is one
// and if it's over the sheilds then clear the line of
// the shields or at the bottom of the screen
int lastLine = ++aliens.y + (ALIEN_HEIGHT * aliens.emptyBottom) - 1;
int topShield = LINES - GUNNER_HEIGHT - SHELTER_HEIGHT - 1;
int gunnerTop = LINES - GUNNER_HEIGHT - 1;
if (lastLine >= topShield && lastLine < topShield + SHELTER_HEIGHT) {
// clear the shield line
int i = (lastLine - topShield) * game.screenCols;
int j;
for (j = 0; j < game.screenCols; j++) {
gun.shields[i + j] = ' '; // DEBUG
}
paintShelters();
}
if (lastLine == gunnerTop) {
// blow up gunner if not already blowing up
if (game.state != STATE_EXPLODE) {
game.state = STATE_EXPLODE;
gun.explodeTimer = 0;
}
}
}
/**
* Adjusts the size of the block of aliens for left and right (the bottom
* adjustment is made by the alien drawing code).
*/
void trimAliens() {
// update empty line pointers
int found = 0;
for (;;) {
int i;
for (i = 0; i < aliens.rows; i++) {
if (aliens.table[(i * aliens.cols) + aliens.emptyLeft]
!= ALIEN_EMPTY) {
found = 1;
break;
}
}
if (found) break;
else aliens.emptyLeft++;
}
found = 0;
for (;;) {
int i;
for (i = 0; i < aliens.rows; i++) {
if (aliens.table[(i * aliens.cols) + aliens.emptyRight - 1]
!= ALIEN_EMPTY) {
found = 1;
break;
}
}
if (found) break;
else aliens.emptyRight--;
}
found = 0;
for (;;) {
int i;
for (i = 0; i < aliens.cols; i++) {
if (aliens.table[((aliens.emptyBottom - 1) * aliens.cols) + i]
!= ALIEN_EMPTY) {
found = 1;
break;
}
}
if (found) break;
else aliens.emptyBottom--;
}
}
/**
* Ends and tidies up.
*/
void cleanUp(int signal) {
if (aliens.table != NULL) free(aliens.table);
if (gun.shields != NULL) free(gun.shields);
endwin();
exit(EXIT_SUCCESS);
}
void paintGunner() {
int i;
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(1));
#endif
for (i = 0; i < GUNNER_HEIGHT; i++) {
move(LINES - GUNNER_HEIGHT + i, gun.x - (GUNNER_WIDTH / 2));
if (game.state == STATE_PLAY) {
printw("%s", sprites[GUNNER][ctype][0].lines[i]);
} else if (game.state == STATE_EXPLODE) {
printw("%s", sprites[GUNNER_EXPLODE][ctype][(game.timer / 4) % 2].lines[i]);
}
}
}
void paintExplodingAlien(int y, int x) {
int i;
for (i = 0; i < ALIEN_HEIGHT; i++) {
move((y * ALIEN_HEIGHT) + aliens.y + i, (x * ALIEN_WIDTH) + aliens.x
+ (y > aliens.paintRow ? aliens.direction : 0));
// adjustment because of
// alien drawing technique
printw("%s", sprites[ALIEN_EXPLODE][ctype][0].lines[i]);
}
}
void removeAlien(int y, int x) {
int i;
for (i = 0; i < ALIEN_HEIGHT; i++) {
move((y * ALIEN_HEIGHT) + aliens.y + i, (x * ALIEN_WIDTH) + aliens.x);
printw("%s", alienBlank);
}
}
void paintScore() {
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(4));
#endif
if (aliens.y > 0) {
move(0, 0);
printw("Ascii-Invaders Score: %d Lives remaining: %d", game.score,
game.lives);
}
}
/**
* Paints a row of aliens (but doesn't call refresh).
* @param row which row of aliens to draw
* @param clean whether to paint the line above this row of aliens white
*/
void paintAlienRow(int row, int clean) {
int x, i;
if (clean) {
move((row * ALIEN_HEIGHT) + aliens.y - 1, 0);
deleteln();
}
// draw the alien space ships
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(4));
#endif
// this is a slight kludge - occasionally bits of explosion were left
// behind when the aliens were moving and exploding at the same time, so:
// if we are not right against the left or right of the screen,
// we delete the column immediately before and after each line
if (aliens.x > 0) {
for (i = 0; i < ALIEN_HEIGHT; i++) {
move((row * ALIEN_HEIGHT) + aliens.y + i,
(aliens.emptyLeft * ALIEN_WIDTH) + aliens.x - 1);
addch(' ');
}
}
if (aliens.x + (ALIEN_WIDTH * aliens.emptyRight) < game.screenCols) {
for (i = 0; i < ALIEN_HEIGHT; i++) {
move((row * ALIEN_HEIGHT) + aliens.y + i,
(aliens.emptyRight * ALIEN_WIDTH) + aliens.x);
addch(' ');
}
}
// draw the aliens
for (x = aliens.emptyLeft; x < aliens.emptyRight; x++) {
int alien = aliens.table[(row * aliens.cols) + x];
for (i = 0; i < ALIEN_HEIGHT; i++) {
move((row * ALIEN_HEIGHT) + aliens.y + i,
(x * ALIEN_WIDTH) + aliens.x);
switch (alien) {
case ALIEN10:
case ALIEN20:
case ALIEN30:
printw("%s", sprites[alien][ctype][aliens.anim].lines[i]);
break;
case ALIEN_EXPLODE1:
//printw("%s", alienExplode[i]);
// do nothing, to leave the explosion on the screen
break;
case ALIEN_EXPLODE2:
case ALIEN_EMPTY:
// wipe out
printw("%s", alienBlank);
}
}
// if the alien is exploding then advance its explosion state
if (alien == ALIEN_EXPLODE1) {
aliens.table[(row * aliens.cols) + x] = ALIEN_EXPLODE2;
} else if (alien == ALIEN_EXPLODE2) {
aliens.table[(row * aliens.cols) + x] = ALIEN_EMPTY;
trimAliens();
}
}
}
void initGame() {
// how many aliens?
if (COLS < 80 || LINES < 20) {
fprintf(stderr, "Terminal size too small to play Ascii Invaders.\n");
cleanUp(0);
}
aliens.cols = (COLS / ALIEN_WIDTH) - 4;
aliens.rows = (LINES / ALIEN_HEIGHT) - 4;
aliens.table = malloc(aliens.cols * aliens.rows * sizeof(int));
gun.shields = malloc(game.screenCols * SHELTER_HEIGHT);
if (aliens.table == NULL || gun.shields == NULL) {
fprintf(stderr, "Out of memory.\n");
cleanUp(0);
}
}
void paintIntro() {
clear();
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(4));
#endif
mvprintw(2, (COLS / 2) - 30, " _ _ _ _ ");
mvprintw(3, (COLS / 2) - 30, " __ _ ___ ___(_|_) (_)_ ____ ____ _ __| | ___ _ __ ___ ");
mvprintw(4, (COLS / 2) - 30, " / _` / __|/ __| | | | | '_ \\ \\ / / _` |/ _` |/ _ \\ '__/ __|");
mvprintw(5, (COLS / 2) - 30, "| (_| \\__ \\ (__| | | | | | | \\ V / (_| | (_| | __/ | \\__ \\");
mvprintw(6, (COLS / 2) - 30, " \\__,_|___/\\___|_|_| |_|_| |_|\\_/ \\__,_|\\__,_|\\___|_| |___/");
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(2));
#endif
mvprintw( 9, (COLS / 2) - 9, sprites[MA][ctype][0].lines[0]);
mvprintw(10, (COLS / 2) - 9, sprites[MA][ctype][0].lines[1]);
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(4));
#endif
mvprintw( 9, (COLS / 2), "= ? points");
mvprintw(12, (COLS / 2) - 8, sprites[ALIEN30][ctype][0].lines[0]);
mvprintw(13, (COLS / 2) - 8, sprites[ALIEN30][ctype][0].lines[1]);
mvprintw(12, (COLS / 2), "= 30 points");
mvprintw(15, (COLS / 2) - 8, sprites[ALIEN20][ctype][0].lines[0]);
mvprintw(16, (COLS / 2) - 8, sprites[ALIEN20][ctype][0].lines[1]);
mvprintw(15, (COLS / 2), "= 20 points");
mvprintw(18, (COLS / 2) - 8, sprites[ALIEN10][ctype][0].lines[0]);
mvprintw(19, (COLS / 2) - 8, sprites[ALIEN10][ctype][0].lines[1]);
mvprintw(18, (COLS / 2), "= 10 points");
#ifdef USE_COLORS
if (has_colors()) attron(COLOR_PAIR(1));
#endif