-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2046 lines (1704 loc) · 58.8 KB
/
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
/*
Twelve Snakes v3.0.0 - a 12 player snake clone by Slinga
*/
/*
** Jo Sega Saturn Engine
** Copyright (c) 2012-2017, Johannes Fetz ([email protected])
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of the Johannes Fetz nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <jo/jo.h> // Required for basic sgl functions
#define MAX_PLAYERS 12
#define MIN_SCORE -99
#define MAX_SCORE 999
#define MAX_SLOWDOWN 9
#define MIN_SLOWDOWN 0
#define MAX_SUBOPTION_VALUES 5
#define INITIAL_SLOWDOWN 5
#define PORT_TWO 9
#define DIR_UP 0
#define DIR_DOWN 1
#define DIR_RIGHT 2
#define DIR_LEFT 3
#define OFF_SCREEN -1
#define MIN_Y 7
#define MAX_Y 23
#define MIN_X 2
#define MAX_X 37
#define MAX_SUDDEN_DEATH_X (MAX_X - MIN_X + 1)
#define MAX_SUDDEN_DEATH_Y (MAX_Y - MIN_Y + 1)
#define GAME_FREE_FOR_ALL 0
#define GAME_SCORE_ATTACK 1
#define GAME_BATTLE_ROYALE 2
#define GAME_SURVIVOR 3
#define GAME_KING_OF_THE_HILL 4
// stdlib function prototypes to keep compiler happy
void* memcpy(void *dst, const void *src, unsigned int len);
int rand(void);
void srand(unsigned int seed);
void *malloc(unsigned int size);
void free(void *ptr);
void *memset(void *s, int c, unsigned int n);
int strcmp(const char* s1, const char* s2);
struct location
{
int x;
int y;
struct location* next;
};
struct food
{
char shape[2]; // The shape of the food
int x;
int y;
};
struct options
{
int gameType;
int maxLives;
int maxScore;
int maxTime;
int slowdown; // factor used to adjust the speed of the game
int startTime; // what time in seconds the game was started
int joinTimeStopped; // no longer allowed to join the game
int suddenDeath; // are we in sudden death mode for Battle Royale
};
struct suboptions
{
char optionName[16];
char optionType[8];
int position;
int values[MAX_SUBOPTION_VALUES];
};
struct sudden_death_grid
{
int lastX;
int lastY;
int count;
int dir;
char grid[MAX_SUDDEN_DEATH_X][MAX_SUDDEN_DEATH_X];
};
const struct suboptions SUBOPTION_TIME_LIMIT = {"Time Limit: ", "min", 1, {1, 3, 5, 7, 10}};
const struct suboptions SUBOPTION_LIVES_LIMIT = {"Lives Limit:", "lives", 2, {1, 3, 5, 7, 10}};
const struct suboptions SUBOPTION_SCORE_LIMIT = {"Score Limit:", "points", 2, {10, 15, 25, 50, 100}};
const struct suboptions SUBOPTION_SLOWDOWN = {"Slowdown: ", "delay", 2, {3, 4, 5, 6, 7}};
struct snake
{
int ID; // index into the array of players
int controllerNum; // which port to read data from. On multitap 2 this it != ID
char shape[2]; // the shape of the snake
int dir; // current direction snake is moving in
int active; // Is this player playing or not
int everActive; // has the player ever been active?
int dying; // Is player marked for death?
struct location* head; // points to the head of the snake
struct location* tail; // points to the tail of the snake
// variables for score
int numApples;
int numDeaths;
int numKills;
int numPlayersEaten;
int currLength;
int maxLength;
int score;
};
// init functions
void initializePlayer(struct snake* somePlayer, struct options* gameOptions);
void initializeFood(struct food* theFood, char theShape);
void initializePlayerNumbers(struct snake* players);
// display\drawing functions
void displayText(); // Displays the heading information
void displayJoinText(struct snake* players, struct options* gameOptions);
void displayMenu(); // Displays the menu choices
int displaySubMenu(struct options* gameOptions, char* gameMode, int numSubOptions, struct suboptions* subOptions);
void displaySSMTFPresents(); // Displays Sega Saturn Multiplayer Task Force logo
void drawGrid(); // Draws the playing field
void drawSnake(struct snake* somePlayer); // Updates the snake, collision detection
void drawFood(struct food* someFood, struct snake* players); // Draws the food on the screen
void drawSuddenDeathGrid(struct sudden_death_grid* deathGrid);
void displayScore(struct snake* players, struct options* gameOptions);
int displayScoreBar(struct snake* players, struct options* gameOptions);
void clearScreen();
void redrawScreen(struct snake* players, struct food* theFood, struct sudden_death_grid* deathGrid);
void redrawSuddenDeathGrid(struct sudden_death_grid* suddenDeath);
void titleScreen();
// game functions
void killPlayer(struct snake* somePlayer);
void eraseSnake(struct location* snakeHead); // erases the snake, free its memory
void pressStart(struct snake* players, struct options* gameOptions);
int safeFood(struct food* someFood, struct snake* players); // returns a 1 if the new position of the food is "safe"
void clearScore(struct snake* players); // clears the game score
void checkPlayerOneCommands(struct snake* players, struct food* theFood, struct options* gameOptions, struct sudden_death_grid* deathGrid);
void checkForCollisions(struct snake* someSnake, struct snake* players);
void checkForSuddenDeathCollisions(struct snake* players, struct options* gameOptions, struct sudden_death_grid* deathGrid);
void validateScore(struct snake* players, struct options* gameOptions);
int isAllowedToSpawn(struct snake* somePlayer, struct options* gameOptions);
void growSnake(struct snake* player, int amount);
int changeSuddenDeathDir(struct sudden_death_grid* deathGrid);
// utility functions
void getTime(jo_datetime* currentTime);
unsigned int getSeconds();
void checkForABCStart();
int g_DisplayedSSMTF = 0;
void jo_main(void)
{
int i = 0;
Uint16 data = 0;
struct snake players[MAX_PLAYERS] = {0};
struct food theFood = {0};
struct options gameOptions = {0};
struct sudden_death_grid deathGrid = {0};
int redrawGrid = 0;
int gameEnded = 0;
// Initializing functions
slInitSystem(TV_320x240, NULL, 1); // Initializes screen
jo_core_init(JO_COLOR_Black);
if(g_DisplayedSSMTF == 0)
{
displaySSMTFPresents(); // SSMTF logo
g_DisplayedSSMTF = 1;
}
drawGrid(); // Draws the playing field box
displayText();
titleScreen();
do
{
//
// Initialize game specific things
//
initializePlayerNumbers(players);
memset(&deathGrid, 0, sizeof(deathGrid));
clearScore(players);
srand(getSeconds());
//
// Prompt the player for game mode and options
//
displayMenu(&gameOptions);
initializeFood(&theFood, '*');
//
// Game play loop
//
do
{
//
// Check for special player one commands
//
checkPlayerOneCommands(players, &theFood, &gameOptions, &deathGrid);
//
// Draw existing players
//
for(i = 0; i < MAX_PLAYERS; i++)
{
data = Smpc_Peripheral[players[i].controllerNum].data;
// Check if player pressed the A button and is not already playing
if((data & PER_DGT_TA) == 0 && players[i].active == 0)
{
initializePlayer(&players[i], &gameOptions);
}
if(players[i].active == 1)
{
drawSnake(&players[i]);
}
}
if(gameOptions.suddenDeath == 1)
{
drawSuddenDeathGrid(&deathGrid);
}
//
// After all players have moved, check for collisions
//
for(i = 0; i < MAX_PLAYERS; i++)
{
if(players[i].active == 1)
{
checkForCollisions(&players[i], players);
}
}
if(gameOptions.suddenDeath == 1)
{
checkForSuddenDeathCollisions(players, &gameOptions, &deathGrid);
}
//
// Kill snakes that are marked for death
//
for(i = 0; i < MAX_PLAYERS; i++)
{
if(players[i].active == 1 && players[i].dying == 1)
{
killPlayer(&players[i]);
redrawGrid = 1; // someone died so redraw the grid
}
}
//
// Draw the food
//
drawFood(&theFood, players);
//
// Draw the grid again in case a Snake crashed into it
//
if(redrawGrid == 1)
{
redrawGrid = 0;
redrawScreen(players, &theFood, &deathGrid);
}
// display the score bar and check for end of game conditions
gameEnded = displayScoreBar(players, &gameOptions);
if(gameEnded == 1)
{
clearScreen();
displayScore(players, &gameOptions);
pressStart(players, &gameOptions);
jo_main();
}
// "Press A to Join"
displayJoinText(players, &gameOptions);
//
// synch the screen
//
slSynch(); // You won't see anything without this!!
for(i = 0; i < gameOptions.slowdown; i++)
{
slSynch(); // Slow down
}
}while(1); // game loop
}while(1); // game type loop
}
void initializePlayerNumbers(struct snake* players)
{
for(int i = 0; i < MAX_PLAYERS; i++)
{
int controllerNum = 0;
if(i < 6)
{
// player is on multitap 1
controllerNum = i;
}
else
{
// player is on multitap 2
// the controllerNum is offset
controllerNum = i + PORT_TWO;
}
players[i].ID = i;
players[i].controllerNum = controllerNum;
}
}
int isAllowedToSpawn(struct snake* somePlayer, struct options* gameOptions)
{
//
// Do not let the player spawn if:
// - they are playing a game type with lives and have run out
// - they didn't spawn in the first 30s a
//
switch(gameOptions->gameType)
{
case GAME_FREE_FOR_ALL:
case GAME_SURVIVOR:
case GAME_KING_OF_THE_HILL:
case GAME_SCORE_ATTACK:
// FFA, Survivor, and KOTH, SA always allow spawning
return 1;
break;
case GAME_BATTLE_ROYALE:
if(gameOptions->suddenDeath == 1)
{
// don't allow spawning in sudden death
return 0;
}
// don't allow spawning if the player is out of lives
if(somePlayer->numDeaths >= gameOptions->maxLives)
{
return 0;
}
return 1;
break;
}
return 0;
}
void initializePlayer(struct snake* somePlayer, struct options* gameOptions)
{
if(isAllowedToSpawn(somePlayer, gameOptions) == 0)
{
return;
}
// Create player
if(somePlayer->active != 1)
{
switch (somePlayer->ID)
{
case 0:
somePlayer->shape[0] = (char)14; // block
break;
case 1:
somePlayer->shape[0] = (char)35; // pound sign
break;
case 2:
somePlayer->shape[0] = (char)23; // gaurav's gamma
break;
case 3:
somePlayer->shape[0] = (char)127; // checkerboard
break;
case 4:
somePlayer->shape[0] = (char)92; // looks like a V
break;
case 5:
somePlayer->shape[0] = (char)38; // percent sign
break;
case 6:
somePlayer->shape[0] = (char)64; // copyright symbol
break;
case 7:
somePlayer->shape[0] = (char)56; // number eight
break;
case 8:
somePlayer->shape[0] = (char)37; // percent sign
break;
case 9:
somePlayer->shape[0] = (char)48; // zero
break;
case 10:
somePlayer->shape[0] = (char)81; // letter Q
break;
case 11:
somePlayer->shape[0] = (char)149; // evil snake!
break;
default:
somePlayer->shape[0] = 'e';
break;
}
somePlayer->shape[1] = '\0';
// allocate three segments for the snake
somePlayer->head = (struct location*)malloc(sizeof(struct location));
somePlayer->head->next = (struct location*)malloc(sizeof(struct location));
somePlayer->head->next->next= (struct location*)malloc(sizeof(struct location));
switch(somePlayer->ID)
{
case 0:
somePlayer->head->x = 1;
somePlayer->head->y = 9;
somePlayer->dir = 2;
break;
case 1:
somePlayer->head->x = 38;
somePlayer->head->y = 21;
somePlayer->dir = 3;
break;
case 2:
somePlayer->head->x = 1;
somePlayer->head->y = 21;
somePlayer->dir = 2;
break;
case 3:
somePlayer->head->x = 38;
somePlayer->head->y = 9;
somePlayer->dir = 3;
break;
case 4:
somePlayer->head->x = 1;
somePlayer->head->y = 15;
somePlayer->dir = 2;
break;
case 5:
somePlayer->head->x = 38;
somePlayer->head->y = 15;
somePlayer->dir = 3;
break;
case 6:
somePlayer->head->x = 29;
somePlayer->head->y = 24;
somePlayer->dir = 0;
break;
case 7:
somePlayer->head->x = 9;
somePlayer->head->y = 6;
somePlayer->dir = 1;
break;
case 8:
somePlayer->head->x = 9;
somePlayer->head->y = 24;
somePlayer->dir = 0;
break;
case 9:
somePlayer->head->x = 29;
somePlayer->head->y = 6;
somePlayer->dir = 1;
break;
case 10:
somePlayer->head->x = 19;
somePlayer->head->y = 24;
somePlayer->dir = 0;
break;
case 11:
somePlayer->head->x = 19;
somePlayer->head->y = 6;
somePlayer->dir = 1;
break;
}
// Initiate rest of snake to offscreen positions
somePlayer->head->next->x = OFF_SCREEN;
somePlayer->head->next->y = OFF_SCREEN;
somePlayer->head->next->next->x = OFF_SCREEN;
somePlayer->head->next->next->y = OFF_SCREEN;
somePlayer->head->next->next->next = NULL;
somePlayer->tail = somePlayer->head->next->next;
somePlayer->currLength = 3;
if(somePlayer->currLength > somePlayer->maxLength)
{
somePlayer->maxLength = somePlayer->currLength;
}
somePlayer->active = 1;
somePlayer->everActive = 1;
somePlayer->dying = 0;
// Draw the starting position of the snake
slPrint(somePlayer->shape, slLocate(somePlayer->head->x, somePlayer->head->y));
}
}
void checkPlayerOneCommands(struct snake* players, struct food* theFood, struct options* gameOptions, struct sudden_death_grid* deathGrid)
{
Uint16 data = 0;
// Read the 1st player controller
data = Smpc_Peripheral[0].data;
checkForABCStart();
// Did player decrease game speed
if((data & PER_DGT_TL) == 0)
{
gameOptions->slowdown++;
if(gameOptions->slowdown > MAX_SLOWDOWN)
{
gameOptions->slowdown = MAX_SLOWDOWN;
}
}
// Did player increase game speed
if((data & PER_DGT_TR) == 0)
{
gameOptions->slowdown--;
if(gameOptions->slowdown < MIN_SLOWDOWN)
{
gameOptions->slowdown = MIN_SLOWDOWN;
}
}
// Does the user want to see the score
if((data & PER_DGT_ST) == 0)
{
clearScreen();
displayScore(players, gameOptions);
pressStart(players, gameOptions);
clearScreen();
redrawScreen(players, theFood, deathGrid);
}
// Does the user want to clear score
if((data & PER_DGT_TZ) == 0)
{
clearScore(players);
}
}
void drawGrid()
{
int j;
char temp[10];
char top[40]; // The top of the playing field
char bottom[40]; // The bottom of the playing field
char topLeftPit[2];
char bottomRightPit[2];
char topRightPit[2];
char bottomLeftPit[2];
char sidePit[2];
char topPit[2];
// Fill the arrays with '-'
sprintf(temp, "%c", 21);
for(j = 0; j<38; j++)
{
top[j] = temp[0];
bottom[j] = temp[0];
}
// Corner pieces
sprintf(temp, "%c", 23);
top[0] = temp[0];
sprintf(temp, "%c", 24);
top[37] = temp[0];
sprintf(temp, "%c", 25);
bottom[0] = temp[0];
sprintf(temp, "%c", 26);
bottom[37] = temp[0];
top[38] = '\0';
bottom[38] = '\0';
// Draw the top and bottom borders
slPrint(top, slLocate(1, 6));
slPrint(bottom, slLocate(1, 24));
// Draw the sides
sprintf(temp, "%c", 22);
for(j = 7; j<24; j++)
{
slPrint(temp, slLocate(1, j));
slPrint(temp, slLocate(38, j));
}
// Draw the snake pits
sprintf(temp, "%c", 23);
topLeftPit[0] = temp[0];
topLeftPit[1] = '\0';
sprintf(temp, "%c", 25);
bottomLeftPit[0] = temp[0];
bottomLeftPit[1] = '\0';
sprintf(temp, "%c", 24);
topRightPit[0] = temp[0];
topRightPit[1] = '\0';
sprintf(temp, "%c", 26);
bottomRightPit[0] = temp[0];
bottomRightPit[1] = '\0';
sprintf(temp, "%c", 22);
sidePit[0] = temp[0];
sidePit[1] = '\0';
sprintf(temp, "%c", 21);
topPit[0] = temp[0];
topPit[1] = '\0';
// Left Side Pits
slPrint(bottomRightPit, slLocate(1,8));
slPrint(topLeftPit, slLocate(0,8));
slPrint(sidePit, slLocate(0,9));
slPrint(" ", slLocate(1,9));
slPrint(bottomLeftPit, slLocate(0,10));
slPrint(topRightPit, slLocate(1,10));
slPrint(bottomRightPit, slLocate(1,14));
slPrint(topLeftPit, slLocate(0,14));
slPrint(sidePit, slLocate(0,15));
slPrint(" ", slLocate(1,15));
slPrint(bottomLeftPit, slLocate(0,16));
slPrint(topRightPit, slLocate(1,16));
slPrint(bottomRightPit, slLocate(1,20));
slPrint(topLeftPit, slLocate(0,20));
slPrint(sidePit, slLocate(0,21));
slPrint(" ", slLocate(1,21));
slPrint(bottomLeftPit, slLocate(0,22));
slPrint(topRightPit, slLocate(1,22));
// Right Side Pits
slPrint(bottomLeftPit, slLocate(38,8));
slPrint(topRightPit, slLocate(39,8));
slPrint(sidePit, slLocate(39,9));
slPrint(" ", slLocate(38,9));
slPrint(bottomRightPit, slLocate(39,10));
slPrint(topLeftPit, slLocate(38,10));
slPrint(bottomLeftPit, slLocate(38,14));
slPrint(topRightPit, slLocate(39,14));
slPrint(sidePit, slLocate(39,15));
slPrint(" ", slLocate(38,15));
slPrint(bottomRightPit, slLocate(39,16));
slPrint(topLeftPit, slLocate(38,16));
slPrint(bottomLeftPit, slLocate(38,20));
slPrint(topRightPit, slLocate(39,20));
slPrint(sidePit, slLocate(39,21));
slPrint(" ", slLocate(38,21));
slPrint(bottomRightPit, slLocate(39,22));
slPrint(topLeftPit, slLocate(38,22));
// Top Pits
slPrint(bottomRightPit, slLocate(8,6));
slPrint(topLeftPit, slLocate(8,5));
slPrint(topPit, slLocate(9,5));
slPrint(" ", slLocate(9,6));
slPrint(topRightPit, slLocate(10,5));
slPrint(bottomLeftPit, slLocate(10,6));
slPrint(bottomRightPit, slLocate(18,6));
slPrint(topLeftPit, slLocate(18,5));
slPrint(topPit, slLocate(19,5));
slPrint(" ", slLocate(19,6));
slPrint(topRightPit, slLocate(20,5));
slPrint(bottomLeftPit, slLocate(20,6));
slPrint(bottomRightPit, slLocate(28,6));
slPrint(topLeftPit, slLocate(28,5));
slPrint(topPit, slLocate(29,5));
slPrint(" ", slLocate(29,6));
slPrint(topRightPit, slLocate(30,5));
slPrint(bottomLeftPit, slLocate(30,6));
// Bottom Pits
slPrint(topRightPit, slLocate(8,24));
slPrint(bottomLeftPit, slLocate(8,25));
slPrint(topPit, slLocate(9,25));
slPrint(" ", slLocate(9,24));
slPrint(bottomRightPit, slLocate(10,25));
slPrint(topLeftPit, slLocate(10,24));
slPrint(topRightPit, slLocate(18,24));
slPrint(bottomLeftPit, slLocate(18,25));
slPrint(topPit, slLocate(19,25));
slPrint(" ", slLocate(19,24));
slPrint(bottomRightPit, slLocate(20,25));
slPrint(topLeftPit, slLocate(20,24));
slPrint(topRightPit, slLocate(28,24));
slPrint(bottomLeftPit, slLocate(28,25));
slPrint(topPit, slLocate(29,25));
slPrint(" ", slLocate(29,24));
slPrint(bottomRightPit, slLocate(30,25));
slPrint(topLeftPit, slLocate(30,24));
}
void checkForSuddenDeathCollisions(struct snake* players, struct options* gameOptions, struct sudden_death_grid* deathGrid)
{
if(gameOptions->gameType != GAME_BATTLE_ROYALE || gameOptions->suddenDeath != 1)
{
return;
}
for(int i = 0; i < MAX_PLAYERS; i++)
{
struct snake* someSnake = &players[i];
if(deathGrid->grid[someSnake->head->x - MIN_X][someSnake->head->y - MIN_Y] != 0)
{
someSnake->dying = 1;
continue;
}
}
}
void drawSuddenDeathGrid(struct sudden_death_grid* deathGrid)
{
int newX = 0;
int newY = 0;
// check if this is the first time we are calling this
if(deathGrid->count == 0)
{
deathGrid->dir = DIR_DOWN;
deathGrid->lastX = 0;
deathGrid->lastY = -1;
}
switch(deathGrid->dir)
{
case DIR_DOWN:
newX = deathGrid->lastX;
newY = deathGrid->lastY + 1;
break;
case DIR_UP:
newX = deathGrid->lastX;
newY = deathGrid->lastY - 1;
break;
case DIR_LEFT:
newX = deathGrid->lastX - 1;
newY = deathGrid->lastY;
break;
case DIR_RIGHT:
newX = deathGrid->lastX + 1;
newY = deathGrid->lastY;
break;
}
if(newX < 0 || newX > MAX_X - MIN_X)
{
changeSuddenDeathDir(deathGrid);
return;
}
if(newY < 0 || newY > MAX_Y - MIN_Y)
{
changeSuddenDeathDir(deathGrid);
return;
}
if(deathGrid->grid[newX][newY] == 'X')
{
// grid is occupied, try again
changeSuddenDeathDir(deathGrid);
return;
}
deathGrid->grid[newX][newY] = 'X';
deathGrid->lastX = newX;
deathGrid->lastY = newY;
slPrint("X", slLocate(deathGrid->lastX + MIN_X, deathGrid->lastY + MIN_Y));
deathGrid->count++;
}
int changeSuddenDeathDir(struct sudden_death_grid* deathGrid)
{
switch(deathGrid->dir)
{
case DIR_DOWN:
deathGrid->dir = DIR_RIGHT;
break;
case DIR_UP:
deathGrid->dir = DIR_LEFT;
break;
case DIR_LEFT:
deathGrid->dir = DIR_DOWN;
break;
case DIR_RIGHT:
deathGrid->dir = DIR_UP;
break;
}
return 0;
}
void checkForCollisions(struct snake* someSnake, struct snake* players)
{
struct location* temp = NULL;
// Check collision with ceiling
if(someSnake->head->y < MIN_Y && someSnake->dir != DIR_DOWN)
{
someSnake->dying = 1;
return;
}
// Check collision with floor
if(someSnake->head->y > MAX_Y && someSnake->dir != DIR_UP)
{
someSnake->dying = 1;
return;
}
// Check collision with left wall
if(someSnake->head->x < MIN_X && someSnake->dir != DIR_RIGHT)
{
someSnake->dying = 1;
return;
}
// Check collision with right wall
if(someSnake->head->x > MAX_X && someSnake->dir != DIR_LEFT)
{
someSnake->dying = 1;
return;
}
// Check for collisions with yourself and other players
for(int i = 0; i < MAX_PLAYERS; i++)
{
if(players[i].active == 1)
{
// Don't check for collisions with your own head!!
if(someSnake->ID != i)
{
temp = players[i].head;
}
else
{
temp = players[i].head->next;
}
while(temp != NULL)
{
// check if there was a collision
if(someSnake->head->x == temp->x && someSnake->head->y == temp->y)
{
// check for head-on collision
if(temp == players[i].head || temp == players[i].head->next)
{
// if Snake is at least twice as big eat the other snake
if(someSnake->currLength >= players[i].currLength * 2)
{
players[i].dying = 1;
someSnake->numPlayersEaten++;
// consome the other snake
growSnake(someSnake, players[i].currLength);
return;
}
}
someSnake->dying = 1;
if(someSnake->ID != players[i].ID)
{
// Other player killed you, reward him
players[i].numKills++;
}
return;
}
temp = temp->next;
}
}
}
}
void drawSnake(struct snake* someSnake)
{
Uint16 data = 0;
//Uint16 i;
struct location* temp = NULL;
// Checks the controller for input
data = Smpc_Peripheral[someSnake->controllerNum].data;
// Check vertical movement
if((data & PER_DGT_KD)== 0)
{
if(someSnake->dir != 0)
{
someSnake->dir = 1;
}
}
else if((data & PER_DGT_KU)== 0)
{
if(someSnake->dir != 1)
{
someSnake->dir = 0;
}
}
// Check horizontal movement
else if((data & PER_DGT_KR)== 0)
{
if(someSnake->dir!= 3)
{
someSnake->dir = 2;
}
}
else if((data & PER_DGT_KL)== 0)
{
if(someSnake->dir!=2)
{
someSnake->dir = 3;
}
}