-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1546 lines (1392 loc) · 42.3 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
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#ifndef VERSION
#define VERSION "unknown"
#endif
#define NUM_PLAYERS 10
#define NUM_SHORT 6
#define NUM_LONG 2
#define NUM_CHATS 50
#define MIN_NAME 2
#define MAX_NAME 10
#ifndef MOVEMENT_NOTIFICATIONS
#define MOVEMENT_NOTIFICATIONS 1
#endif
enum game_stage {
STAGE_LOBBY,
STAGE_PLAYING,
STAGE_DISCUSS,
};
enum player_stage {
PLAYER_STAGE_NAME,
PLAYER_STAGE_LOBBY,
PLAYER_STAGE_MAIN,
PLAYER_STAGE_DISCUSS,
PLAYER_STAGE_WAITING,
};
enum player_task_short {
TASK_CAFE_TRASH,
TASK_CAFE_COFFEE,
TASK_CAFE_WIRES,
TASK_STORAGE_TRASH,
TASK_STORAGE_WIRES,
TASK_STORAGE_CLEAN,
TASK_ELECTRICAL_WIRES,
TASK_ELECTRICAL_BREAKERS,
TASK_ADMIN_WIRES,
TASK_ADMIN_CLEAN,
TASK_NAVIGATION_WIRES,
TASK_NAVIGATION_COURSE,
TASK_NAVIGATION_HEADING,
TASK_WEAPONS_WIRES,
TASK_WEAPONS_CALIBRATE,
TASK_SHIELDS_WIRES,
TASK_O2_WIRES,
TASK_O2_CLEAN,
TASK_OS_WATER,
TASK_MEDBAY_WIRES,
TASK_UPPER_CATALYZER,
TASK_LOWER_CATALYZER,
TASK_UPPER_COMPRESSION_COIL,
TASK_LOWER_COMPRESSION_COIL,
TASK_SHORT_COUNT,
};
const char short_task_descriptions[][45] = {
"Empty the cafeteria trash",
"Start the coffee maker in the cafeteria",
"Fix wiring in cafeteria",
"Empty the storage trash chute",
"Fix wiring in storage",
"Clean the floor in storage",
"Fix wiring in electrical",
"Reset breakers in electrical",
"Fix wiring in admin",
"Clean the floor in admin",
"Fix wiring in navigation",
"Adjust course in navigation",
"Check headings in navigation",
"Fix wiring in weapons",
"Calibrate targeting system in weapons",
"Fix wiring in shields",
"Fix wiring in o2",
"Clean oxygenator filter in o2",
"Water plants in o2",
"Fix wiring in medbay",
"Check catalyzer in upper engine",
"Check catalyzer in lower engine",
"Replace compression coil in upper engine",
"Replace compression coil in lower engine",
};
enum player_task_long {
TASK_SHIELDS_POWER,
TASK_WEAPONS_POWER,
TASK_NAV_LOG,
TASK_SHIELD_LOG,
TASK_REACTOR_FUEL,
TASK_POTATO,
TASK_LONG_COUNT
};
const char long_task_descriptions[][2][45] = {
{"Route power to defence in electrical", "Accept rerouted power in shields"},
{"Route power to attack in electrical", "Accept rerouted power in weapons"},
{"Download the latest navigation data", "Upload data in admin"},
{"Download the latest shields data", "Upload data in admin"},
{"Pick up nuclear fuel in storage", "Insert fuel into reactor"},
{"Pick up potato in cafeteria", "Plant the potato in o2"},
{"Get radio log from communications", "Deliver communications log to admin"},
};
enum player_location {
LOC_CAFETERIA,
LOC_REACTOR,
LOC_UPPER_ENGINE,
LOC_LOWER_ENGINE,
LOC_SECURITY,
LOC_MEDBAY,
LOC_ELECTRICAL,
LOC_STORAGE,
LOC_ADMIN,
LOC_COMMUNICATIONS,
LOC_O2,
LOC_WEAPONS,
LOC_SHIELDS,
LOC_NAVIGATION,
LOC_COUNT,
};
const char locations[][45] = {
[LOC_CAFETERIA] = "cafeteria",
[LOC_REACTOR] = "reactor",
[LOC_UPPER_ENGINE] = "upper",
[LOC_LOWER_ENGINE] = "lower",
[LOC_SECURITY] = "security",
[LOC_MEDBAY] = "medbay",
[LOC_ELECTRICAL] = "electrical",
[LOC_STORAGE] = "storage",
[LOC_ADMIN] = "admin",
[LOC_COMMUNICATIONS] = "communications",
[LOC_O2] = "o2",
[LOC_WEAPONS] = "weapons",
[LOC_SHIELDS] = "shields",
[LOC_NAVIGATION] = "navigation",
};
enum player_location doors[][10] = {
[LOC_CAFETERIA] = { LOC_MEDBAY, LOC_ADMIN, LOC_WEAPONS, LOC_COUNT },
[LOC_REACTOR] = { LOC_UPPER_ENGINE, LOC_SECURITY, LOC_LOWER_ENGINE, LOC_COUNT },
[LOC_UPPER_ENGINE] = { LOC_REACTOR, LOC_SECURITY, LOC_MEDBAY, LOC_COUNT },
[LOC_LOWER_ENGINE] = { LOC_REACTOR, LOC_SECURITY, LOC_ELECTRICAL, LOC_COUNT },
[LOC_SECURITY] = { LOC_UPPER_ENGINE, LOC_REACTOR, LOC_LOWER_ENGINE, LOC_COUNT },
[LOC_MEDBAY] = { LOC_UPPER_ENGINE, LOC_CAFETERIA, LOC_COUNT },
[LOC_ELECTRICAL] = { LOC_LOWER_ENGINE, LOC_STORAGE, LOC_COUNT },
[LOC_STORAGE] = { LOC_ELECTRICAL, LOC_ADMIN, LOC_COMMUNICATIONS, LOC_SHIELDS, LOC_COUNT },
[LOC_ADMIN] = { LOC_CAFETERIA, LOC_STORAGE, LOC_COUNT },
[LOC_COMMUNICATIONS] = { LOC_STORAGE, LOC_SHIELDS, LOC_COUNT },
[LOC_O2] = { LOC_SHIELDS, LOC_WEAPONS, LOC_NAVIGATION, LOC_COUNT },
[LOC_WEAPONS] = { LOC_CAFETERIA, LOC_O2, LOC_NAVIGATION, LOC_COUNT },
[LOC_SHIELDS] = { LOC_STORAGE, LOC_COMMUNICATIONS, LOC_O2, LOC_NAVIGATION, LOC_COUNT },
[LOC_NAVIGATION] = { LOC_WEAPONS, LOC_O2, LOC_SHIELDS, LOC_COUNT },
};
const char descriptions[][256] = {
[LOC_CAFETERIA] = "You are standing in the middle of the cafeteria, in the center there's an emergency button\n",
[LOC_REACTOR] = "You are in the reactor room, it seems to be running normally\n",
[LOC_UPPER_ENGINE] = "You are in a small room, mostly filled up by an engine.\n",
[LOC_LOWER_ENGINE] = "You are in a small room, mostly filled up by an engine.\n",
[LOC_SECURITY] = "You are in a small room filled with monitors, the monitors are displaying camera images showing an overview of the ship\n",
[LOC_MEDBAY] = "You are in a room with beds and a medical scanner.\n",
[LOC_ELECTRICAL] = "You are in a room filled with equipment racks. Some of them have wires sticking out of them\n",
[LOC_STORAGE] = "You are in a large room filled with boxes. One of the walls has a large door to the outside\n",
[LOC_ADMIN] = "You are in a nice carpeted room with a holographic map in the middle\n",
[LOC_COMMUNICATIONS] = "You are in a small room with what looks like radio equipment\n",
[LOC_O2] = "You are in a room with plants in terrariums and life support equipment\n",
[LOC_WEAPONS] = "You are in a circular room with a targeting system in the middle and a view of outer space\n",
[LOC_SHIELDS] = "You are in a circular room with glowing tubes and a control panel for the shields\n",
[LOC_NAVIGATION] = "You are all the way in the front of the ship in a room with the ship controls and a great view of space\n",
};
const char map[] =
"|\\----------------|--------------|----------------|--------------\\\n"
"| \\\n"
"| UPPER ENGINE CAFETERIA WEAPONS \\\n"
"| |- --------| | \\\n"
"|/--------| |--| MEDBAY | | \\\n"
" | | | | \\------\\\n"
"/---------| |-------\\ | |----------| | \\\n"
"| | | \\ |---| |------| | |\n"
"| \\ | | |\n"
"| REACTOR SECURITY | | ADMIN OFFICE | O2 NAVIGATION |\n"
"| | | | | |\n"
"| | | | |---| |----|-|----------| |\n"
"\\---------| |----------|------| | | /\n"
" | | | /------/\n"
"|\\--------| |--| | /\n"
"| | | |-- --| /\n"
"| LOWER ENGINE ELECTRICAL STORAGE | COMMS | SHIELDS /\n"
"| | | /\n"
"|/----------------|--------------|--------------|--------|-------/\n"
;
const char usage[] =
"Usage: among-sus [-p <port>] [-h]\n"
"among-sus:\tAmong Us, but it's a text adventure\n"
"\n"
"-p, \t\tSet port number\n"
"-h, \t\tDisplay this message\n"
;
enum player_state {
PLAYER_STATE_ALIVE,
PLAYER_STATE_VENT, // TODO: implement vents
PLAYER_STATE_DEAD,
PLAYER_STATE_FOUND,
PLAYER_STATE_EJECTED,
PLAYER_STATE_KICKED,
};
struct player {
int fd;
enum player_stage stage;
char name[MAX_NAME + 1];
int is_admin;
int is_impostor;
enum player_location location;
enum player_state state;
int has_cooldown;
int voted;
int votes;
enum player_task_short short_tasks[NUM_SHORT];
int short_tasks_done[NUM_SHORT];
enum player_task_long long_tasks[NUM_LONG];
int long_tasks_done[NUM_LONG];
};
struct gamestate {
enum game_stage stage;
int has_admin;
int players;
int is_reactor_meltdown;
int chats_left;
int skips;
unsigned int impostor_cooldown;
};
struct gamestate state;
struct player players[NUM_PLAYERS];
fd_set rfds, afds;
int
random_num(int upper_bound)
{
int ret;
do {
ret = rand();
} while (ret >= (RAND_MAX - RAND_MAX % upper_bound));
ret %= upper_bound;
return ret;
}
int
alive(struct player player)
{
return player.state == PLAYER_STATE_ALIVE || player.state == PLAYER_STATE_VENT;
}
void
broadcast(char* message, int notfd)
{
char buf[1024];
int pid;
printf("*> %s\n", message);
snprintf(buf, sizeof(buf), "%s\n", message);
for (pid = 0; pid < NUM_PLAYERS; pid++) {
if (players[pid].fd == -1 || players[pid].fd == notfd
|| players[pid].stage == PLAYER_STAGE_NAME
|| players[pid].stage == PLAYER_STAGE_WAITING)
continue;
write(players[pid].fd, buf, strlen(buf));
}
}
void
broadcast_ghosts(char* message, int notfd)
{
char buf[1024];
int pid;
printf("*> %s\n", message);
snprintf(buf, sizeof(buf), "%s\n", message);
for (pid = 0; pid < NUM_PLAYERS; pid++) {
if (players[pid].fd == -1
|| players[pid].fd == notfd
|| players[pid].stage == PLAYER_STAGE_NAME
|| players[pid].stage == PLAYER_STAGE_WAITING
|| alive(players[pid]))
continue;
write(players[pid].fd, buf, strlen(buf));
}
}
void
player_move(size_t pid, enum player_location location)
{
char buf[100];
enum player_location old_location = players[pid].location;
printf("Moving player %zu to %d\n", pid, location);
players[pid].location = location;
if (players[pid].has_cooldown != 0)
--players[pid].has_cooldown;
// body detection
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if (players[i].location != players[pid].location || i == pid
|| players[i].fd == -1
|| players[i].state != PLAYER_STATE_DEAD
|| players[i].stage != PLAYER_STAGE_WAITING)
continue;
snprintf(buf, sizeof(buf), "you enter the room and see the body of [%s] laying on the floor\n", players[i].name);
write(players[pid].fd, buf, strlen(buf));
}
// Notify players you're moving
if (MOVEMENT_NOTIFICATIONS) {
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if (players[i].fd == -1 || !alive(players[i])
|| !alive(players[pid]) || i == pid)
continue;
if (players[i].location == players[pid].location) {
snprintf(buf, sizeof(buf), "[%s] just walked into the room\n", players[pid].name);
write(players[i].fd, buf, strlen(buf));
}
if (players[i].location == old_location) {
snprintf(buf, sizeof(buf), "[%s] just left the room\n", players[pid].name);
write(players[i].fd, buf, strlen(buf));
}
}
}
}
void
end_game()
{
char buf[100];
broadcast("------------------------", -1);
broadcast("The game has ended, returning to lobby", -1);
state.stage = STAGE_LOBBY;
for(int i=0; i<NUM_PLAYERS;i++) {
if (players[i].fd == -1)
continue;
if (players[i].stage == PLAYER_STAGE_WAITING) {
snprintf(buf, sizeof(buf), "Game ended, sending you to lobby.\n\a");
write(players[i].fd, buf, strlen(buf));
snprintf(buf, sizeof(buf), "[%s] has joined the lobby.", players[i].name);
broadcast(buf, players[i].fd);
}
players[i].stage = PLAYER_STAGE_LOBBY;
}
}
int
check_win_condition(void)
{
char buf[100];
size_t nalive = 0, iid = 0, tasks = 1;
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if (players[i].fd != -1 && players[i].is_impostor)
iid = i;
if (players[i].is_impostor == 1
&& !alive(players[i])) {
broadcast("The crew won, the impostor died", -1);
end_game();
return 1;
}
if (players[i].fd != -1 && alive(players[i])
&& players[i].is_impostor == 0
&& players[i].stage != PLAYER_STAGE_WAITING)
nalive++;
if (players[i].fd != -1 && !players[i].is_impostor
&& players[i].stage != PLAYER_STAGE_WAITING) {
for (size_t j = 0; j < NUM_SHORT; j++) {
if (!players[i].short_tasks_done[j]) {
tasks = 0;
break;
}
}
for (size_t j = 0; j < NUM_LONG; j++) {
if (players[i].long_tasks_done[j] != 2) {
tasks = 0;
break;
}
}
}
}
if (tasks == 1) {
broadcast("The crew won, all tasks completed", -1);
end_game();
return 1;
}
if (nalive == 1) {
broadcast("The impostor is alone with the last crewmate and murders them", -1);
snprintf(buf, sizeof(buf), "The impostor was [%s] all along...", players[iid].name);
broadcast(buf, -1);
end_game();
return 1;
}
return 0;
}
void
task_completed(size_t pid, size_t task_id, int long_task)
{
// Mark task completed for player
if (!long_task) {
for (size_t i = 0; i < NUM_SHORT; i++) {
if (players[pid].short_tasks[i] == task_id) {
players[pid].short_tasks_done[i] = 1;
}
}
} else {
for(size_t i = 0; i < NUM_LONG; i++) {
if (players[pid].long_tasks[i] == task_id) {
players[pid].long_tasks_done[i]++;
}
}
}
check_win_condition();
}
void
list_rooms_with_players(size_t pid) {
int count[LOC_COUNT] = {0};
char buf[100];
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if (players[i].fd != -1 && alive(players[i]))
count[players[i].location]++;
}
for (int i=0;i<LOC_COUNT;i++) {
if (count[i] > 0) {
snprintf(buf, sizeof(buf), " * There are %d players in %s\n",
count[i], locations[i]);
write(players[pid].fd, buf, strlen(buf));
}
}
}
void
player_list_tasks(size_t pid)
{
char buf[100];
int task_desc;
int done = 1;
for (size_t i = 0; i < TASK_SHORT_COUNT; i++) {
for (size_t j = 0; j < NUM_SHORT; j++) {
if(players[pid].short_tasks[j] == i) {
const char *cm;
if(players[pid].short_tasks_done[j]) {
cm = "*";
} else {
cm = " ";
done = 0;
}
snprintf(buf, sizeof(buf), " [%s] %s\n", cm,
short_task_descriptions[i]);
write(players[pid].fd, buf, strlen(buf));
}
}
}
for (size_t i = 0; i < TASK_LONG_COUNT; i++) {
for (size_t j = 0; j < NUM_LONG; j++) {
if(players[pid].long_tasks[j] == i) {
const char *cm;
if(players[pid].long_tasks_done[j] == 2) {
cm = "*";
task_desc = 0;
} else if(players[pid].long_tasks_done[j] == 1) {
cm = "-";
task_desc = 1;
done = 0;
} else {
cm = " ";
task_desc = 0;
done = 0;
}
snprintf(buf, sizeof(buf), " [%s] %s\n", cm,
long_task_descriptions[i][task_desc]);
write(players[pid].fd, buf, strlen(buf));
}
}
}
if (done) {
snprintf(buf, sizeof(buf), "All your tasks are completed!\n# ");
} else {
snprintf(buf, sizeof(buf), "Complete the tasks by typing the full task name in the correct location\n# ");
}
write(players[pid].fd, buf, strlen(buf));
}
bool
player_kill(size_t pid, size_t tid)
{
char buf[100];
if(players[pid].location != players[tid].location
|| players[tid].is_impostor
|| players[tid].stage != PLAYER_STAGE_MAIN)
return false;
// so sad
players[tid].state = PLAYER_STATE_DEAD;
// less murdering, reset by movement
players[pid].has_cooldown = state.impostor_cooldown;
// notify player of their recent death
snprintf(buf, sizeof(buf), "It turns out %s is the impostor, sadly the way you know is that you died.\n",
players[pid].name);
write(players[tid].fd, buf, strlen(buf));
// notify bystanders
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if (i == pid || players[i].fd == -1 || !alive(players[i])
|| players[i].location != players[pid].location
|| players[i].stage != PLAYER_STAGE_MAIN)
continue;
snprintf(buf, sizeof(buf), "someone killed [%s] while you were in the room\n",
players[tid].name);
write(players[i].fd, buf, strlen(buf));
}
check_win_condition();
return true;
}
void
start_discussion(size_t pid, size_t bid)
{
char buf[100];
state.stage = STAGE_DISCUSS;
state.skips = 0;
// switch everyone to the discussion state and mark bodies found
for(int i=0; i<NUM_PLAYERS;i++) {
if (players[i].fd == -1
|| players[i].stage != PLAYER_STAGE_MAIN)
continue;
players[i].stage = PLAYER_STAGE_DISCUSS;
players[i].voted = 0;
players[i].votes = 0;
}
broadcast("------------------------", -1);
// Inform everyone
if (bid == SIZE_MAX) {
// Emergency button was pressed
snprintf(buf, sizeof(buf), "\nAn emergency meeting was called by [%s]", players[pid].name);
} else {
// Body was reported
snprintf(buf, sizeof(buf), "\nThe body of [%s] was found by [%s]", players[bid].name, players[pid].name);
players[bid].state = PLAYER_STATE_FOUND;
}
broadcast(buf, -1);
// List the state of the players
broadcast("Players:", -1);
for(int i=0; i<NUM_PLAYERS;i++) {
if (players[i].fd == -1
|| players[i].stage != PLAYER_STAGE_DISCUSS)
continue;
switch (players[i].state) {
case PLAYER_STATE_ALIVE:
snprintf(buf, sizeof(buf), "* %d [%s]", i, players[i].name);
break;
case PLAYER_STATE_DEAD:
snprintf(buf, sizeof(buf), "* %d [%s] (dead)", i, players[i].name);
break;
case PLAYER_STATE_FOUND:
snprintf(buf, sizeof(buf), "* %d [%s] (dead, reported)", i, players[i].name);
break;
case PLAYER_STATE_VENT:
case PLAYER_STATE_EJECTED:
case PLAYER_STATE_KICKED:
continue;
}
broadcast(buf, -1);
}
// Inform people of the chat limit
snprintf(buf, sizeof(buf), "Discuss, there are %d messages left", NUM_CHATS);
state.chats_left = NUM_CHATS;
broadcast(buf, -1);
}
void
back_to_playing()
{
state.stage = STAGE_PLAYING;
// switch everyone to the playing state
for(int i=0; i<NUM_PLAYERS;i++) {
if (players[i].fd == -1
|| players[i].stage != PLAYER_STAGE_DISCUSS)
continue;
players[i].stage = PLAYER_STAGE_MAIN;
players[i].location = LOC_CAFETERIA;
}
broadcast("-- Voting has ended, back to the ship --\n\n# ", -1);
}
void
discussion(size_t pid, char *input)
{
char buf[300];
intmax_t vote = 0, max_votes = 0, tie = 0, winner = -1, hasvalidchar = 0;
for (size_t i = 0; i < strlen(input); i++) {
if (!isprint(input[i]))
input[i] = '\0';
else if (!isspace(input[i]))
hasvalidchar = 1;
}
if (input[0] == '/' && input[1] != '/') {
if ((strncmp(input, "/vote ", 6) == 0
|| strncmp(input, "/yeet ", 6) == 0
|| strncmp(input, "/skip", 6) == 0)
&& alive(players[pid]) ) {
if (players[pid].voted) {
snprintf(buf, sizeof(buf), "You can only vote once\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
if (input[1] == 's') {
vote = -1;
} else {
char *endptr = NULL;
vote = strtol(&input[6], &endptr, 10);
if (!endptr || endptr[0] != '\0') {
snprintf(buf, sizeof(buf), "Invalid vote, not an integer\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
}
if (vote != -1 && (vote < -1 || vote > NUM_PLAYERS-1
|| players[vote].fd == -1
|| players[vote].stage != PLAYER_STAGE_DISCUSS)) {
snprintf(buf, sizeof(buf), "Invalid vote, no such player\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
if (!alive(players[vote])) {
snprintf(buf, sizeof(buf), "Invalid vote, that person is dead\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
if (vote == -1) {
printf("[%s] voted to skip\n", players[pid].name);
} else {
printf("[%s] voted for %jd\n", players[pid].name, vote);
}
players[pid].voted = 1;
if (vote == -1) {
state.skips++;
} else {
players[vote].votes++;
}
check_votes:
// Check if voting is complete
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if(players[i].fd != -1 && players[i].voted == 0
&& alive(players[i])
&& players[i].stage == PLAYER_STAGE_DISCUSS) {
printf("No vote from [%s] yet\n", players[i].name);
goto not_yet;
}
}
printf("Voting complete\n");
// Count votes
max_votes = state.skips;
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if(players[i].fd == -1 || players[i].stage != PLAYER_STAGE_DISCUSS)
continue;
if(players[i].votes > max_votes){
max_votes = players[i].votes;
tie = 0;
winner = (intmax_t)i;
continue;
}
if(players[i].votes == max_votes) {
tie = 1;
}
}
printf("Vote winner: %jd\n", winner);
if (tie) {
broadcast("The voting ended in a tie", -1);
back_to_playing();
return;
} else if (winner == -1) {
snprintf(buf, sizeof(buf), "The crew voted to skip\n");
broadcast(buf, -1);
back_to_playing();
return;
} else {
snprintf(buf, sizeof(buf), "The crew voted to eject [%s]\n", players[winner].name);
broadcast(buf, -1);
}
// dramatic pause
for(int i=0;i<5;i++) {
sleep(1);
broadcast(".", -1);
}
players[winner].state = PLAYER_STATE_EJECTED;
if (players[winner].is_impostor) {
snprintf(buf, sizeof(buf), "It turns out [%s] was an impostor", players[winner].name);
broadcast(buf, -1);
} else {
snprintf(buf, sizeof(buf), "Sadly, [%s] was not an impostor", players[winner].name);
broadcast(buf, -1);
}
if(!check_win_condition()) {
back_to_playing();
}
return;
not_yet:
broadcast("A vote has been cast", -1);
} else if (strncmp(input, "/help", 6) == 0) {
snprintf(buf, sizeof(buf), "Commands: /vote [id], /skip, /list\n");
write(players[pid].fd, buf, strlen(buf));
} else if (strncmp(input, "/list", 6) == 0) {
for(int i=0; i<NUM_PLAYERS;i++) {
if (players[i].fd == -1
|| players[i].stage != PLAYER_STAGE_DISCUSS)
continue;
if (alive(players[i]) && players[i].voted) {
snprintf(buf, sizeof(buf), "* %d [%s] (voted) \n", i, players[i].name);
} else if (alive(players[i])) {
snprintf(buf, sizeof(buf), "* %d [%s]\n", i, players[i].name);
} else {
snprintf(buf, sizeof(buf), "* %d [%s] (dead)\n", i, players[i].name);
}
write(players[pid].fd, buf, strlen(buf));
}
snprintf(buf, sizeof(buf), "Commands: /vote [id], /skip, /list\n");
write(players[pid].fd, buf, strlen(buf));
} else if (strncmp(input, "/kick", 5) == 0) {
if (!players[pid].is_admin) {
snprintf(buf, sizeof(buf), "You have no kicking permission\n");
write(players[pid].fd, buf, strlen(buf));
}
char *endptr = NULL;
vote = strtol(&input[6], &endptr, 10);
if (!endptr || endptr[0] != '\0') {
snprintf(buf, sizeof(buf), "Invalid kick, not an integer\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
snprintf(buf, sizeof(buf), "Admin kicked [%s]\n", players[vote].name);
broadcast(buf, -1);
close(players[vote].fd);
FD_CLR(players[vote].fd, &afds);
players[vote].fd = -1;
players[vote].state = PLAYER_STATE_KICKED;
goto check_votes;
} else if (strncmp(input, "/me ", 4) == 0) {
if (state.chats_left > 0 && alive(players[pid])) {
snprintf(buf, sizeof(buf), "(%d) * [%s] %s", state.chats_left, players[pid].name, &input[4]);
broadcast(buf, -1);
state.chats_left--;
} else if (alive(players[pid])) {
snprintf(buf, sizeof(buf), "No chats left, you can only vote now\n");
write(players[pid].fd, buf, strlen(buf));
return;
} else {
snprintf(buf, sizeof(buf), "(dead) * [%s] %s", players[pid].name, &input[4]);
broadcast_ghosts(buf, -1);
}
} else if (strncmp(input, "/shrug", 6) == 0) {
if (state.chats_left > 0 && alive(players[pid])) {
snprintf(buf, sizeof(buf), "(%d) [%s]: ¯\\_(ツ)_/¯", state.chats_left, players[pid].name);
broadcast(buf, -1);
state.chats_left--;
} else if (alive(players[pid])) {
snprintf(buf, sizeof(buf), "No chats left, you can only vote now\n");
write(players[pid].fd, buf, strlen(buf));
return;
} else {
snprintf(buf, sizeof(buf), "(dead) [%s]: ¯\\_(ツ)_/¯", players[pid].name);
broadcast_ghosts(buf, -1);
}
} else {
snprintf(buf, sizeof(buf), "Invalid command\n");
write(players[pid].fd, buf, strlen(buf));
}
} else if (input[0] == '/') {
if (state.chats_left == 0 && alive(players[pid])) {
snprintf(buf, sizeof(buf), "No chats left, you can only vote now\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
if (alive(players[pid])) {
snprintf(buf, sizeof(buf), "(%d) [%s]: %s", state.chats_left, players[pid].name, &input[1]);
broadcast(buf, -1);
state.chats_left--;
} else {
snprintf(buf, sizeof(buf), "(dead) [%s]: %s", players[pid].name, &input[1]);
broadcast_ghosts(buf, -1);
}
} else if (hasvalidchar) {
if (state.chats_left <= 0 && alive(players[pid])) {
snprintf(buf, sizeof(buf), "No chats left, you can only vote now\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
if (alive(players[pid])) {
snprintf(buf, sizeof(buf), "(%d) [%s]: %s", state.chats_left, players[pid].name, input);
broadcast(buf, -1);
state.chats_left--;
} else {
snprintf(buf, sizeof(buf), "(dead) [%s]: %s", players[pid].name, input);
broadcast_ghosts(buf, -1);
}
}
}
void
adventure(size_t pid, char *input)
{
char buf[1024];
const char *location;
size_t task_id;
int task_is_long;
if (input[0] == 'e' || strncmp(input, "ls", 3) == 0) {
enum player_location loc = players[pid].location;
strcpy(buf, "you can move to: ");
assert(doors[loc][0] != LOC_COUNT);
strncat(buf, locations[doors[loc][0]], sizeof(buf) - 1);
for (size_t i = 1; doors[loc][i] != LOC_COUNT; i++) {
strncat(strncat(buf, ", ", sizeof(buf) - 1),
locations[doors[loc][i]], sizeof(buf) - 1);
}
strncat(buf, "\n", sizeof(buf) - 1);
location = descriptions[loc];
if (loc == LOC_REACTOR && state.is_reactor_meltdown) {
location = "You are in the reactor room, there are red warning lights on and a siren is going off.\n";
}
write(players[pid].fd, location, strlen(location));
write(players[pid].fd, buf, strlen(buf));
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if (players[i].location != players[pid].location
|| players[i].fd == -1 || i == pid
|| players[i].stage != PLAYER_STAGE_MAIN)
continue;
switch (players[i].state) {
case PLAYER_STATE_ALIVE:
snprintf(buf, sizeof(buf),
"you also see %s in the room with you\n",
players[i].name);
break;
case PLAYER_STATE_DEAD:
snprintf(buf, sizeof(buf),
"you also see %s's corpse in the room with you\n",
players[i].name);
break;
case PLAYER_STATE_FOUND:
snprintf(buf, sizeof(buf),
"you also see %s's reported corpse in the room with you\n",
players[i].name);
break;
case PLAYER_STATE_VENT:
case PLAYER_STATE_EJECTED:
case PLAYER_STATE_KICKED:
buf[0] = '\0';
}
write(players[pid].fd, buf, strlen(buf));
}
snprintf(buf, sizeof(buf), "# ");
} else if (strncmp(input, "go ", 3) == 0 || strncmp(input, "cd ", 3) == 0) {
enum player_location new;
for (new = 0; new < LOC_COUNT; new++) {
if (strcmp(locations[new], &input[3]) == 0) {
break;
}
}
if (new == LOC_COUNT) {
snprintf(buf, sizeof(buf), "INVALID MOVEMENT\n# ");
} else {
for (size_t i = 0; doors[players[pid].location][i] != LOC_COUNT; i++) {
if (doors[players[pid].location][i] == new) {
player_move(pid, new);
snprintf(buf, sizeof(buf),
"successfully moved\n# ");
new = LOC_COUNT;
break;
}
}
if (new != LOC_COUNT) {
snprintf(buf, sizeof(buf), "INVALID MOVEMENT\n# ");
}
}
} else if (strcmp(input, "murder crewmate") == 0) {
if (players[pid].is_impostor == 0) {
snprintf(buf, sizeof(buf), "you might dislike them, but you can't kill them without weapon\n# ");
} else if (players[pid].has_cooldown) {
snprintf(buf, sizeof(buf), "you can't kill that quickly\n# ");
} else {
snprintf(buf, sizeof(buf), "no one to kill here\n# ");
for (size_t i = 0; i < NUM_PLAYERS; i++) {
if (players[i].location != players[pid].location
|| i == pid || players[i].fd == -1
|| !alive(players[i])
|| players[i].stage != PLAYER_STAGE_MAIN)
continue;
// TODO: kill more randomly
if (player_kill(pid, i))
snprintf(buf, sizeof(buf), "you draw your weapon and brutally murder %s\n# ",
players[i].name);
break;
}
}
} else if (strcmp(input, "report") == 0) {
for(size_t i = 0; i < NUM_PLAYERS; i++) {
if (players[i].location != players[pid].location
|| i == pid || players[i].fd == -1
|| players[i].state != PLAYER_STATE_DEAD
|| !alive(players[pid]))
continue;
start_discussion(pid, i);
return;
}
snprintf(buf, sizeof(buf), "Nothing to report here\n# ");
} else if (strcmp(input, "press emergency button") == 0) {
if (players[pid].location != LOC_CAFETERIA) {
snprintf(buf, sizeof(buf), "You can't do that here\n# ");
} else if (!alive(players[pid])) {
snprintf(buf, sizeof(buf), "Ghosts can't call emergencies\n# ");
} else {