-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnake.c
More file actions
3947 lines (3272 loc) · 78.4 KB
/
snake.c
File metadata and controls
3947 lines (3272 loc) · 78.4 KB
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 "snake.h"
#define STARTING_LENGTH 2
int DEBUG;
FILE *debug_fp = NULL;
FILE *hs_fp = NULL;
int hs_fd = -1;
char *user_home = NULL;
char *high_score_file = NULL;
int path_max;
char *tmp = NULL;
Player_List *player_list = NULL;
Player *player = NULL;
Player *player_prev = NULL;
char save_name[32];
struct termios nterm, oterm;
struct winsize ws;
int row_2, row_3, row_23, row_4, row_5, row_34, row_8, row_16, row_max;
int col_2, col_3, col_23, col_4, col_5, col_34, col_8, col_16, col_max;
struct Snake_Head shead;
struct Snake_Tail stail;
Food f;
int default_r;
int default_u;
/* thread-related variables */
pthread_t tid_main;
pthread_t tid_snake;
pthread_t tid_food;
pthread_t tid_dir;
pthread_t tid_score;
volatile sig_atomic_t tid_food_end;
volatile sig_atomic_t tid_dir_end;
volatile sig_atomic_t tid_score_end;
volatile sig_atomic_t put_some_food_end;
volatile sig_atomic_t EATEN;
volatile sig_atomic_t ate_food;
volatile sig_atomic_t gameover;
volatile sig_atomic_t thread_failed;
volatile sig_atomic_t user_ctrl_c;
volatile int score;
volatile int level;
volatile int featen;
pthread_attr_t attr;
pthread_mutex_t mutex;
pthread_mutex_t smutex;
pthread_mutex_t sleep_mutex;
pthread_mutex_t dir_mutex;
pthread_mutex_t in_mutex;
pthread_mutex_t list_mutex;
sigjmp_buf main_thread_env;
char *BG_COL = NULL;
char *SN_COL = NULL;
char *BR_COL = NULL;
char *HD_COL = NULL;
char *FD_COL = NULL;
char *TITLE_BG_COL = NULL;
char *GAMEOVER_BG_COL = NULL;
char *TEXT_SHADOW_COL = NULL;
int DEFAULT_USLEEP_TIME = 0;
int USLEEP_TIME = 0;
int USLEEP_VADJUST = 0;
int USLEEP_DECREMENT = 10000;
int FOOD_REFRESH_TIME = 0;
int maxr = 0, minr = 0, maxu = 0, minu = 0;
char DIRECTION = 0;
int LEVEL_THRESHOLD = 0;
int **matrix = NULL;
/* player stats-related variables */
int BEAT_OWN_SCORE = 0;
int NEW_BEST_PLAYER = 0;
void log_err(char *, ...) __nonnull ((1));
void debug(char *, ...) __nonnull ((1));
static int get_default_sleep_time(void) __wur;
static inline void write_stats(void);
static int write_high_scores(Player *, Player *) __nonnull ((1,2));
static int get_high_scores(Player **, Player **) __nonnull ((1,2)) __wur;
static void find_player_prev(Player **, Player **, Player *, Player *) __nonnull ((1,2,3,4));
static void free_high_scores(Player **, Player **) __nonnull ((1,2));
static Player *new_player_node(void) __wur;
static int remove_player_node(Player **, Player **, Player **) __nonnull ((1,2,3)) __wur;
static int show_hall_of_fame(Player *, Player *) __nonnull ((1,2)) __wur;
static int check_current_player_score(Player *, Player **, Player **) __nonnull ((1,2,3));
static void save_screen_format(Snake_Head *, Snake_Tail *) __nonnull ((1,2));
static void restore_screen_format(void);
static int title_screen(void);
static void grow_snake(Snake_Head *, Snake_Tail *) __nonnull ((1,2));
static void grow_head(Snake_Head *) __nonnull ((1));
static void grow_tail(Snake_Tail *) __nonnull ((1));
static void snip_tail(Snake_Tail *) __nonnull ((1));
static void adjust_tail(Snake_Tail *, Snake_Head *) __nonnull ((1,2));
static void go_to_head(Snake_Head *, Snake_Tail *) __nonnull ((1,2));
static void go_to_tail(Snake_Head *, Snake_Tail *) __nonnull ((1,2));
static void calibrate_snake_position(Snake_Head *, Snake_Tail *) __nonnull ((1,2));
static int hit_own_body(Snake_Head *, Snake_Tail *) __nonnull ((1,2)) __wur;
static int within_snake(Food *) __nonnull ((1)) __wur;
static int setup_game(void);
static int game_over(void);
static void change_level(int);
static void reset_snake(Snake_Head *, Snake_Tail *) __nonnull ((1,2));
//static void redraw_snake(Snake_Head *, Snake_Tail *) __nonnull ((1,2));
static void _pause(void);
static void unpause(void);
static void level_one(void);
static void level_two(void);
static void level_three(void);
static void level_four(void);
static void level_five(void);
void strip_crnl(char *) __nonnull ((1));
//static char *get_colour(char *) __nonnull ((1)) __wur;
static void
__attribute__ ((constructor)) snake_init(void)
{
int ret = 0;
int tfd = -1;
DEBUG = 0;
/* seed random number generator */
srand(time(NULL));
if ((tfd = open("/dev/tty", O_RDWR)) < 0)
{ log_err("snake_init: failed to open /dev/tty"); goto fail; }
memset(&oterm, 0, sizeof(oterm));
memset(&nterm, 0, sizeof(nterm));
/* change terminal driver attributes */
tcgetattr(tfd, &oterm);
memcpy(&nterm, &oterm, sizeof(oterm));
nterm.c_lflag &= ~(ECHO|ECHOCTL|ICANON);
tcsetattr(tfd, TCSANOW, &nterm);
close(tfd);
/* zero out data structures */
memset(&f, 0, sizeof(f));
memset(&ws, 0, sizeof(ws));
/* initialise thread-related variables */
debug("initialising thread mutexes");
if ((ret = pthread_mutex_init(&mutex, NULL)) != 0)
{ fprintf(stderr, "snake_init: pthread_mutex_init error (%s)\n", strerror(ret)); goto fail; }
if ((ret = pthread_mutex_init(&smutex, NULL)) != 0)
{ fprintf(stderr, "snake_init: pthread_mutex_init error (%s)\n", strerror(ret)); goto fail; }
if ((ret = pthread_mutex_init(&sleep_mutex, NULL)) != 0)
{ fprintf(stderr, "snake_init: pthread_mutex_init error (%s)\n", strerror(ret)); goto fail; }
if ((ret = pthread_mutex_init(&dir_mutex, NULL)) != 0)
{ fprintf(stderr, "snake_init: pthread_mutex_init error (%s)\n", strerror(ret)); goto fail; }
if ((ret = pthread_mutex_init(&in_mutex, NULL)) != 0)
{ fprintf(stderr, "snake_init: pthread_mutex_init error (%s)\n", strerror(ret)); goto fail; }
if ((ret = pthread_mutex_init(&list_mutex, NULL)) != 0)
{ fprintf(stderr, "snake_init: pthread_mutex_init error (%s)\n", strerror(ret)); goto fail; }
debug("initialising thread attributes");
if ((ret = pthread_attr_init(&attr)) != 0)
{ fprintf(stderr, "snake_init: pthread_attr_init error (%s)\n", strerror(ret)); goto fail; }
if ((ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) != 0)
{ fprintf(stderr, "snake_init: pthread_attr_setdetachstate error (%s)\n", strerror(ret)); goto fail; }
level = 1;
// _thresh
LEVEL_THRESHOLD = 30;
gameover = 0;
tid_food_end = 0;
tid_dir_end = 0;
tid_score_end = 0;
put_some_food_end = 0;
user_ctrl_c = 0;
memset(&shead, 0, sizeof(shead));
memset(&stail, 0, sizeof(stail));
path_max = 0;
if ((path_max = pathconf("/", _PC_PATH_MAX)) == 0)
path_max = 1024;
if (!(high_score_file = calloc(path_max, 1)))
{ log_err("snake_init: calloc error for high_score_file"); goto fail; }
memset(high_score_file, 0, path_max);
if (!(user_home = getenv("HOME")))
{ log_err("snake_init: failed to get user's home directory"); goto fail; }
/* first check the existence of the snake directory itself */
sprintf(high_score_file, "%s/.snake", user_home);
if (access(high_score_file, F_OK) != 0)
mkdir(high_score_file, S_IRWXU);
sprintf(high_score_file, "%s/.snake/high_scores.dat", user_home);
if (access(high_score_file, F_OK) != 0)
{
if ((hs_fd = open(high_score_file, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU & ~S_IXUSR)) < 0)
{ log_err("snake_init: failed to create high scores file"); goto fail; }
debug("created \"%s\"", high_score_file);
}
if (!(hs_fp = fopen(high_score_file, "r+")))
{ log_err("snake_init: fopen error"); goto fail; }
if (!(player = malloc(sizeof(Player))))
{ log_err("snake_init: malloc error"); goto fail; }
if (!(player_list = malloc(sizeof(Player_List))))
{ log_err("snake_init: malloc error"); goto fail; }
memset(player, 0, sizeof(*player));
memset(player_list, 0, sizeof(*player_list));
player_list->first = NULL;
player_list->last = NULL;
player->next = NULL;
player->prev = NULL;
if (!(tmp = calloc(MAXLINE, 1)))
{ log_err("snake_init: calloc error"); goto fail; }
memset(tmp, 0, MAXLINE);
FOOD_REFRESH_TIME = 12;
TITLE_BG_COL = "\e[48;5;238m";
GAMEOVER_BG_COL = "\e[48;5;234m";
//MENU_BG_COL = BLACK;
TEXT_SHADOW_COL = DARK_RED;
return;
fail:
exit(EXIT_FAILURE);
}
static void
__attribute__ ((destructor)) snake_fini(void)
{
int tfd = -1;
free_high_scores(&player_list->first, &player_list->last);
tfd = open("/dev/tty", O_RDWR);
tcgetattr(tfd, &nterm);
if (!(nterm.c_lflag & ECHO))
nterm.c_lflag |= ECHO;
if (!(nterm.c_lflag & ECHOCTL))
nterm.c_lflag |= ECHOCTL;
if (!(nterm.c_lflag & ICANON))
nterm.c_lflag |= ICANON;
tcsetattr(tfd, TCSANOW, &nterm);
close(tfd);
if (high_score_file != NULL) { free(high_score_file); high_score_file = NULL; }
if (tmp != NULL) { free(tmp); tmp = NULL; }
if (hs_fp != NULL) { fclose(hs_fp); hs_fp = NULL; }
}
sigjmp_buf main_env;
void
main_thread_sig_handler(int signo)
{
/* If the player pressed ctrl+c, then we want to be able
* to save their score before exiting. So set the flag
* and do a siglongjmp() before the main loop in main().
* Equally, we want to handle a thread failing. In both
* cases, the body of sigsetjmp() will save the player's
* score.
*/
if (signo == SIGINT)
{
user_ctrl_c = 1;
siglongjmp(main_env, 1);
return;
}
else if (signo == SIGTERM)
{
thread_failed = 1;
siglongjmp(main_env, 1);
}
else
{
return;
}
}
// MAIN
int
main(int argc, char *argv[])
{
struct sigaction sigterm_n;
struct sigaction sigint_n;
char c, choice[4];
int i, j;
int play_again = 0, quit = 0;
int tfd = -1;
memset(&sigterm_n, 0, sizeof(sigterm_n));
memset(&sigint_n, 0, sizeof(sigint_n));
sigterm_n.sa_handler = main_thread_sig_handler;
sigterm_n.sa_flags = 0;
sigemptyset(&sigterm_n.sa_mask);
sigaddset(&sigterm_n.sa_mask, SIGINT);
if (sigaction(SIGTERM, &sigterm_n, NULL) < 0)
{ log_err("main: failed to set signal handler for SIGTERM"); goto fail; }
sigint_n.sa_handler = main_thread_sig_handler;
sigint_n.sa_flags = 0;
sigemptyset(&sigint_n.sa_mask);
sigaddset(&sigint_n.sa_mask, SIGTERM);
if (sigaction(SIGINT, &sigint_n, NULL) < 0)
{ log_err("main: failed to set signal handler for SIGINT"); goto fail; }
/* get terminal window dimensions:
*
* need to do this here and not in the constructor function
* because the terminal dimensions are not known before main
* is called (I don't know why, but that is the reason it was
* moved back here to main())
*/
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
{ log_err("main: ioctl TIOCGWINSZ error"); goto fail; }
maxu = ws.ws_row-2;
maxr = ws.ws_col-3;
minu = 1;
minr = 1;
row_3 = (ws.ws_row/3);
row_23 = (row_3 * 2);
row_2 = (ws.ws_row/2);
row_4 = (row_2/2);
row_34 = (row_2 + row_4);
row_5 = (ws.ws_row/5);
row_8 = (row_4/2);
row_16 = (row_8/2);
row_max = ws.ws_row;
col_3 = (ws.ws_col/3);
col_23 = (col_3 * 2);
col_2 = (ws.ws_col/2);
col_4 = (col_2/2);
col_34 = (col_2 + col_4);
col_5 = (ws.ws_col/5);
col_8 = (col_4/2);
col_16 = (col_8/2);
col_max = (ws.ws_col-1);
USLEEP_TIME = get_default_sleep_time();
USLEEP_VADJUST = (USLEEP_TIME/3);
USLEEP_DECREMENT = 10000;
clear_screen(TITLE_BG_COL);
if (get_high_scores(&player_list->first, &player_list->last) == -1)
{ log_err("main: get_high_scores error"); goto fail; }
title_screen();
up(ws.ws_row/2);
right((ws.ws_col/2)-(strlen("Enter Name")/2));
printf("%s%sEnter name\e[m", TITLE_BG_COL, TRED);
reset_right();
reset_up();
//down(1);
//right(ws.ws_col/2);
memset(player->name, 0, 32);
user_ctrl_c = 0;
if (sigsetjmp(main_env, 1) != 0)
{
if (user_ctrl_c)
{
pthread_mutex_lock(&mutex);
reset_right();
reset_up();
pthread_mutex_unlock(&mutex);
if (player_list)
check_current_player_score(player, &(player_list->first), &(player_list->last));
exit(EXIT_SUCCESS);
}
else if (thread_failed)
{
pthread_mutex_lock(&mutex);
reset_right();
reset_up();
pthread_mutex_unlock(&mutex);
if (player_list)
check_current_player_score(player, &(player_list->first), &(player_list->last));
fprintf(stderr, "main: received SIGTERM from worker thread... exiting!\n");
goto fail;
}
else
{
pthread_mutex_lock(&mutex);
reset_right();
reset_up();
pthread_mutex_unlock(&mutex);
fprintf(stderr, "*** Caught SIGINT. Quitting ***\n");
exit(EXIT_SUCCESS);
}
}
c = 0;
i = 0;
// MAIN : GET PLAYER NAME
while (c != 0x0a)
{
c = fgetc(stdin);
if (c == 0x0a)
{
if (i == 0)
{
up((ws.ws_row/2)-1);
clear_line(TITLE_BG_COL);
right((ws.ws_col/2)-(strlen("No username entered!")/2));
printf("%s%sNo username entered!\e[m", TITLE_BG_COL, TBLACK);
reset_right();
reset_up();
c &= ~c;
continue;
}
break;
}
else if (c == 0x7f)
{
if (i == 0)
continue;
--i;
player->name[i] = 0;
up((ws.ws_row/2)-1);
clear_line(TITLE_BG_COL);
right((ws.ws_col/2)-(strlen(player->name)/2));
printf("%s%s%s\e[m", TITLE_BG_COL, TPINK, player->name);
reset_right();
reset_up();
}
else
{
if (i == 18)
continue;
player->name[i++] = c;
up((ws.ws_row/2)-1);
clear_line(TITLE_BG_COL);
right((ws.ws_col/2)-(strlen(player->name)/2));
printf("%s%s%s\e[m", TITLE_BG_COL, TPINK, player->name);
reset_right();
reset_up();
}
}
player->name[i] = 0;
player->score = 0;
player->num_eaten = 0;
player->highest_level = 1;
find_player_prev(&player, &player_prev, player_list->first, player_list->last);
reset_right();
reset_up();
while ((c = getopt(argc, argv, "D")) != -1)
{
switch(c)
{
case(0x44):
DEBUG = 1;
break;
default:
errno = EINVAL;
log_err("unknown option...");
exit(EXIT_FAILURE);
}
}
/* start the snake in the upper-right quadrant */
default_r = (((rand()%(ws.ws_col/2))+(ws.ws_col/2))-1);
default_u = (((rand()%(ws.ws_row/2))+(ws.ws_row/2))-1);
/* allocate memory for screen matrix */
if (!(matrix = calloc(ws.ws_row, sizeof(int *))))
{ log_err("main: calloc error (matrix)"); goto fail; }
for (i = 0; i < ws.ws_row; ++i)
{
matrix[i] = NULL;
if (!(matrix[i] = calloc(ws.ws_col, sizeof(int))))
{ log_err("main: matrix creation failed (calloc)"); goto fail; }
for (j = 0; j < ws.ws_col; ++j)
matrix[i][j] = 0;
}
user_ctrl_c = 0;
game_loop:
tid_dir_end = 0;
tid_food_end = 0;
tid_score_end = 0;
if (setup_game() == -1)
goto fail;
for(;;)
{
if (gameover)
{
int l1, l2;
struct termios term;
/*tid_dir_end = 1;
tid_food_end = 1;*/
tid_score_end = 1;
// ensures first thing done is check value of tid_food_end
pthread_kill(tid_food, SIGUSR2);
/*
* Worker thread that gets the user direction will be interrupted
* (more than likely from read() system call, its signal handler
* will do a siglongjmp() to just before its main loop, and then
* TID_DIR_END will be set to 1. The thread will then pass over
* the loop and exit with (void *)0;
*/
pthread_kill(tid_dir, SIGQUIT);
usleep(5000);
pthread_mutex_lock(&mutex);
player->score &= ~(player->score);
player->num_eaten &= ~(player->num_eaten);
memset(&term, 0, sizeof(term));
tfd = open("/dev/tty", O_RDWR);
tcgetattr(tfd, &term);
if (term.c_lflag & ICANON)
term.c_lflag &= ~(ICANON);
tcsetattr(tfd, TCSANOW, &term);
reset_right();
reset_up();
l1 = strlen("Play");
l2 = strlen("Quit");
play_again = 1;
quit &= ~quit;
up((ws.ws_row/2)-(ws.ws_row/16));
right((ws.ws_col/3) - l1/2);
printf("%s%sPlay\e[m", RED, TWHITE);
reset_right();
right(((ws.ws_col/3)*2) - l2/2);
printf("%s%sQuit\e[m", TITLE_BG_COL, TWHITE);
reset_right();
reset_up();
for (i = 0; i < 4; ++i)
choice[i] &= ~(choice[i]);
int ret;
for (;;)
{
if ((ret = read(STDIN_FILENO, choice, 3)) <= 0)
{
if (errno == EINTR) continue;
else { log_err("main: read error"); goto fail; }
}
if (choice[0] == 0x0a)
{
pthread_mutex_unlock(&mutex);
if (play_again)
{
gameover = 0;
memset(save_name, 0, 32);
strncpy(save_name, player->name, strlen(player->name));
save_name[strlen(player->name)] = 0;
free_high_scores(&player_list->first, &player_list->last);
if (!(player = malloc(sizeof(Player))))
{ log_err("main: malloc error"); goto fail; }
memset(player, 0, sizeof(*player));
strncpy(player->name, save_name, strlen(save_name));
player->next = NULL;
player->prev = NULL;
player->highest_level = 1;
if (get_high_scores(&player_list->first, &player_list->last) == -1)
{ log_err("main: get_high_scores error"); goto fail; }
player_prev = NULL;
find_player_prev(&player,
&player_prev,
player_list->first,
player_list->last);
EATEN = 0;
level = 1;
goto game_loop;
}
else
goto success;
}
if (strncmp("\e[D", choice, 3) == 0)
{
up((ws.ws_row/2)-(ws.ws_row/16));
right((ws.ws_col/3)-l1/2);
printf("%s%sPlay\e[m", RED, TWHITE);
reset_right();
right(((ws.ws_col/3)*2)-l2/2);
printf("%s%sQuit\e[m", GAMEOVER_BG_COL, TWHITE);
play_again = 1;
quit = 0;
for (i = 0; i < 4; ++i) choice[i] = 0;
reset_right();
reset_up();
}
else if (strncmp("\e[C", choice, 3) == 0)
{
up((ws.ws_row/2)-(ws.ws_row/16));
right((ws.ws_col/3)-l1/2);
printf("%s%sPlay\e[m", GAMEOVER_BG_COL, TWHITE);
reset_right();
right(((ws.ws_col/3)*2)-l2/2);
printf("%s%sQuit\e[m", RED, TWHITE);
quit = 1;
play_again = 0;
for (i = 0; i < 4; ++i) choice[i] = 0;
reset_right();
reset_up();
}
else
{
for (i = 0; i < 4; ++i) choice[i] = 0;
continue;
}
}
}
if (thread_failed)
{
// so that the compiler does not complain about unused result
check_current_player_score(player, &(player_list->first), &(player_list->last));
write_high_scores(player_list->first, player_list->last);
pthread_kill(tid_snake, SIGTERM);
pthread_kill(tid_food, SIGTERM);
goto fail;
}
}
success:
exit(EXIT_SUCCESS);
fail:
exit(EXIT_FAILURE);
}
// SETUP_GAME
int
setup_game(void)
{
pthread_mutex_lock(&smutex);
/* start with a single piece; head and tail both point to same piece */
EATEN = 0;
if (!shead.h)
{
shead.h = malloc(sizeof(Snake_Piece));
memset(shead.h, 0, sizeof(*(shead.h)));
shead.h->d = 0x6c;
shead.h->prev = NULL;
shead.h->next = NULL;
stail.t = shead.h;
}
//log_mutex("unlock", "smutex");
pthread_mutex_unlock(&smutex);
if (pthread_attr_init(&attr) != 0)
goto fail;
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
goto fail;
shead.sl = STARTING_LENGTH;
change_level(1);
//level_one();
thread_failed = 0;
/* create threads */
if (pthread_create(&tid_snake, &attr, snake_thread, NULL) != 0)
goto fail;
tid_main = pthread_self();
if (pthread_create(&tid_food, &attr, put_some_food, NULL) != 0)
goto fail;
if (pthread_create(&tid_dir, &attr, get_direction, NULL) != 0)
goto fail;
if (pthread_create(&tid_score, &attr, track_score, NULL) != 0)
goto fail;
return(0);
fail:
fprintf(stderr, "setup_game: failed to setup game\n");
return(-1);
}
// SNAKE_THREAD
void *
snake_thread(void *arg)
{
int sleeptime;
restart:
sleep(1);
switch(shead.h->d)
{
case(0x6c):
break;
case(0x72):
goto go_right;
break;
case(0x75):
goto go_up;
break;
case(0x64):
goto go_down;
break;
}
go_left:
pthread_mutex_lock(&smutex);
if (shead.h->d != 0x6c)
shead.h->d = 0x6c;
pthread_mutex_unlock(&smutex);
pthread_mutex_lock(&dir_mutex);
DIRECTION = 0x6c;
pthread_mutex_unlock(&dir_mutex);
for (;;)
{
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&smutex);
left(1);
draw_line_x(HD_COL, 1, 0);
--(shead.r);
draw_line_x(SN_COL, 1, 0);
left(2);
if (shead.np > 1)
++(shead.h->l);
pthread_mutex_unlock(&smutex);
pthread_mutex_unlock(&mutex);
if (matrix[shead.u][shead.r] == -1)
//if (shead.r < minr)
goto lose;
adjust_tail(&stail, &shead);
pthread_mutex_lock(&sleep_mutex);
sleeptime = USLEEP_TIME;
pthread_mutex_unlock(&sleep_mutex);
usleep(sleeptime);
if (hit_own_body(&shead, &stail))
goto lose;
pthread_mutex_lock(&smutex);
if (shead.r == f.r && shead.u == f.u) // we ate food
{
pthread_mutex_unlock(&smutex);
EATEN = 1;
ate_food = 1;
pthread_kill(tid_food, SIGUSR2);
grow_snake(&shead, &stail);
++(player->num_eaten);
if (player->num_eaten != 0 && (player->num_eaten % LEVEL_THRESHOLD) == 0)
{
++level;
change_level(level);
goto restart;
}
}
else
{ pthread_mutex_unlock(&smutex); }
pthread_mutex_lock(&dir_mutex);
if (DIRECTION != 0x6c)
{
switch(DIRECTION)
{
case(0x75):
grow_head(&shead);
//log_mutex("unlock", "dir_mutex");
pthread_mutex_unlock(&dir_mutex);
goto go_up;
break;
case(0x64):
//log_mutex("unlock", "dir_mutex");
pthread_mutex_unlock(&dir_mutex);
grow_head(&shead);
goto go_down;
break;
default:
DIRECTION = 0x6c;
}
}
pthread_mutex_unlock(&dir_mutex);
}
go_up:
pthread_mutex_lock(&smutex);
if (shead.h->d != 0x75)
shead.h->d = 0x75;
pthread_mutex_unlock(&smutex);
pthread_mutex_lock(&dir_mutex);
DIRECTION = 0x75;
pthread_mutex_unlock(&dir_mutex);
USLEEP_TIME += USLEEP_VADJUST;
for (;;)
{
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&smutex);
up(1);
draw_line_x(HD_COL, 1, 0);
++(shead.u);
left(1);
down(1);
draw_line_x(SN_COL, 1, 0);
left(1);
up(1);
if (shead.np > 1)
++(shead.h->l);
pthread_mutex_unlock(&smutex);
pthread_mutex_unlock(&mutex);
if (matrix[shead.u][shead.r] == -1
|| hit_own_body(&shead, &stail))
goto lose;
adjust_tail(&stail, &shead);
pthread_mutex_lock(&sleep_mutex);
sleeptime = USLEEP_TIME;
pthread_mutex_unlock(&sleep_mutex);
usleep(sleeptime);
pthread_mutex_lock(&smutex);
if (shead.r == f.r && shead.u == f.u) // we ate food
{
pthread_mutex_unlock(&smutex);
EATEN = 1;
ate_food = 1;
pthread_kill(tid_food, SIGUSR2);
grow_snake(&shead, &stail);
++(player->num_eaten);
if (player->num_eaten != 0 && (player->num_eaten % LEVEL_THRESHOLD) == 0)
{
++level;
change_level(level);
goto restart;
}
}
else
{ pthread_mutex_unlock(&smutex); }
pthread_mutex_lock(&dir_mutex);
if (DIRECTION != 0x75)
{
USLEEP_TIME -= USLEEP_VADJUST;
switch(DIRECTION)
{
case(0x6c):
grow_head(&shead);
pthread_mutex_unlock(&dir_mutex);
goto go_left;
break;
case(0x72):
grow_head(&shead);
pthread_mutex_unlock(&dir_mutex);
goto go_right;
break;
default:
DIRECTION = 0x75;
}
}
pthread_mutex_unlock(&dir_mutex);
}
go_down:
pthread_mutex_lock(&smutex);
if (shead.h->d != 0x64)
shead.h->d = 0x64;
pthread_mutex_unlock(&smutex);
pthread_mutex_lock(&dir_mutex);
DIRECTION = 0x64;
pthread_mutex_unlock(&dir_mutex);
USLEEP_TIME += USLEEP_VADJUST;
for (;;)
{
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&smutex);
down(1);
draw_line_x(HD_COL, 1, 0);
--(shead.u);
left(1);
up(1);
draw_line_x(SN_COL, 1, 0);
left(1);
down(1);
if (shead.np > 1)
++(shead.h->l);
pthread_mutex_unlock(&smutex);
pthread_mutex_unlock(&mutex);
if (matrix[shead.u][shead.r] == -1
|| hit_own_body(&shead, &stail))
goto lose;
adjust_tail(&stail, &shead);
pthread_mutex_lock(&sleep_mutex);
sleeptime = USLEEP_TIME;
pthread_mutex_unlock(&sleep_mutex);
usleep(sleeptime);
pthread_mutex_lock(&smutex);
if (shead.r == f.r && shead.u == f.u) // we ate food
{
pthread_mutex_unlock(&smutex);
EATEN = 1;
ate_food = 1;
pthread_kill(tid_food, SIGUSR2);
grow_snake(&shead, &stail);