-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.sunder
2229 lines (2055 loc) · 89.9 KB
/
server.sunder
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
import "c";
import "std";
import "util.sunder";
import "shared.sunder";
struct connection {
var player: std::optional[[player]];
var sender: message_sender[[server_message]];
# Send a reliable server message to the connection.
func send(self: *connection, message: *server_message) void {
self.*.sender.send(message);
}
# Send an unreliable server message to the connection.
func send_unreliable(self: *connection, message: *server_message) void {
self.*.sender.send_unreliable(message);
}
# Send a reliable server message to the connection, then consume the
# message.
func sendc(self: *connection, message: server_message) void {
self.*.sender.send(&message);
message.fini();
}
# Send an unreliable server message to the connection, then consume the
# message.
func sendc_unreliable(self: *connection, message: server_message) void {
self.*.sender.send_unreliable(&message);
message.fini();
}
}
struct server_state {
var connections: std::vector[[connection]];
var ticks: usize;
var board: board;
var dev_cards: std::vector[[dev_card]];
var r_live: bool;
var b_live: bool;
var w_live: bool;
var o_live: bool;
var r_info: hidden_player_info;
var b_info: hidden_player_info;
var w_info: hidden_player_info;
var o_info: hidden_player_info;
var r_discarded: bool; # discard phase
var b_discarded: bool; # discard phase
var w_discarded: bool; # discard phase
var o_discarded: bool; # discard phase
var r_trade_submitted: bool; # trade phase
var b_trade_submitted: bool; # trade phase
var w_trade_submitted: bool; # trade phase
var o_trade_submitted: bool; # trade phase
var trade: trade;
# The player turn order for the game. Shuffled from the four player colors.
# If the Nth player is not live, then that player's turn is skipped.
var turn_order: [4]player;
# The current turn.
var turn: turn;
# True if the active player has rolled this turn.
var player_rolled: bool;
# The phase a development card was played this turn. Used for restoring the
# turn state after a development card has resolved. An empty optional
# indicates that a development card has not been played this turn.
var dev_card_used: std::optional[[phase]];
var longest_road: std::optional[[player]];
var largest_army: std::optional[[player]];
var winner: std::optional[[player]];
func init() server_state {
# Board
var board = board::init_starter_map_for_beginners();
# Tiles are randomized by taking the tiles from the starter map for
# beginners, shuffling the tile kinds and tile numbers, and then
# overwriting tiles on the board with the shuffled kind-number pairs.
var kinds = std::vector[[typeof(tile::SELF.kind)]]::init();
defer kinds.fini();
var numbers = std::vector[[typeof(tile::SELF.number)]]::init();
defer numbers.fini();
var iter = board.tiles.iterator();
for iter.advance() {
if iter.current().*.value.*.kind == tile::DESERT {
continue;
}
if iter.current().*.value.*.kind == tile::OCEAN {
continue;
}
kinds.push(iter.current().*.value.*.kind);
numbers.push(iter.current().*.value.*.number);
}
shuffle[[typeof(tile::SELF.kind)]](kinds.data());
shuffle[[typeof(tile::SELF.number)]](numbers.data());
var idx = random[[usize]]() % kinds.count();
kinds.insert(idx, tile::DESERT);
numbers.insert(idx, -1);
var iter = board.tiles.iterator();
for iter.advance() {
if iter.current().*.value.*.kind == tile::OCEAN {
continue;
}
iter.current().*.value.*.kind = kinds.pop();
iter.current().*.value.*.number = numbers.pop();
}
# Robber
# The robber always starts on the desert.
var iter = board.tiles.iterator();
for iter.advance() {
if iter.current().*.value.*.kind == tile::DESERT {
board.robber = iter.current().*.key.*;
break;
}
}
# Ports
# Ports are randomized by taking the ports from the starter map for
# beginners, shuffling the port kinds, and then overwriting ports on
# the board with those shuffled kinds.
var kinds = std::vector[[typeof(port::SELF.kind)]]::init();
defer kinds.fini();
var iter = board.ports.iterator();
for iter.advance() {
kinds.push(iter.current().*.kind);
}
shuffle[[typeof(port::SELF.kind)]](kinds.data());
var iter = board.ports.iterator();
for iter.advance() {
iter.current().*.kind = kinds.pop();
}
# Development Cards
var dev_cards = std::vector[[dev_card]]::init();
# x14 Knight
for _ in 14 {
dev_cards.push(dev_card::KNIGHT);
}
# x02 Road Building
for _ in 2 {
dev_cards.push(dev_card::ROAD_BUILDING);
}
# x02 Year of Plenty
for _ in 2 {
dev_cards.push(dev_card::YEAR_OF_PLENTY);
}
# x02 Monopoly
for _ in 2 {
dev_cards.push(dev_card::MONOPOLY);
}
# x05 Victory Point
for _ in 5 {
dev_cards.push(dev_card::VICTORY_POINT);
}
shuffle[[dev_card]](dev_cards.data());
var turn_order = (:[4]player)[
player::RED,
player::BLUE,
player::WHITE,
player::ORANGE,
];
shuffle[[player]]((:[]player){&turn_order[0], countof(turn_order)});
var turn = turn::init(0, turn_order[0]);
turn.phase = phase::GAME_INIT;
return (:server_state){
.connections = std::vector[[connection]]::init(),
.ticks = 0,
.board = board,
.dev_cards = dev_cards,
.r_live = false,
.b_live = false,
.w_live = false,
.o_live = false,
.r_info = uninit,
.b_info = uninit,
.w_info = uninit,
.o_info = uninit,
.r_discarded = false,
.b_discarded = false,
.w_discarded = false,
.o_discarded = false,
.r_trade_submitted = false,
.b_trade_submitted = false,
.w_trade_submitted = false,
.o_trade_submitted = false,
.trade = uninit,
.turn_order = turn_order,
.turn = turn,
.player_rolled = false,
.dev_card_used = std::optional[[phase]]::EMPTY,
.longest_road = std::optional[[player]]::EMPTY,
.largest_army = std::optional[[player]]::EMPTY,
.winner = std::optional[[player]]::EMPTY,
};
}
func fini(self: *server_state) void {
self.*.connections.fini();
self.*.board.fini();
self.*.dev_cards.fini();
}
# Send a reliable server message to all connections.
func send_all(self: *server_state, message: *server_message) void {
for i in self.*.connections.count() {
self.*.connections.data()[i].send(message);
}
}
# Send an unreliable server message to all connections.
func send_all_unreliable(self: *server_state, message: *server_message) void {
for i in self.*.connections.count() {
self.*.connections.data()[i].send_unreliable(message);
}
}
# Send a reliable server message to all connections, then consume the
# message.
func sendc_all(self: *server_state, message: server_message) void {
self.*.send_all(&message);
message.fini();
}
# Send an unreliable server message to all connections, then consume the
# message.
func sendc_all_unreliable(self: *server_state, message: server_message) void {
self.*.send_all_unreliable(&message);
message.fini();
}
func p_live(self: *server_state, player: player) *bool {
var out: *bool = uninit;
switch player {
::player::RED {
out = &self.*.r_live;
}
::player::BLUE {
out = &self.*.b_live;
}
::player::WHITE {
out = &self.*.w_live;
}
::player::ORANGE {
out = &self.*.o_live;
}
}
return out;
}
func p_info(self: *server_state, player: player) *hidden_player_info {
var out: *hidden_player_info = uninit;
switch player {
::player::RED {
out = &self.*.r_info;
}
::player::BLUE {
out = &self.*.b_info;
}
::player::WHITE {
out = &self.*.w_info;
}
::player::ORANGE {
out = &self.*.o_info;
}
}
return out;
}
func p_discarded(self: *server_state, player: player) *bool {
var out: *bool = uninit;
switch player {
::player::RED {
out = &self.*.r_discarded;
}
::player::BLUE {
out = &self.*.b_discarded;
}
::player::WHITE {
out = &self.*.w_discarded;
}
::player::ORANGE {
out = &self.*.o_discarded;
}
}
return out;
}
func p_trade_submitted(self: *server_state, player: player) *bool {
var out: *bool = uninit;
switch player {
::player::RED {
out = &self.*.r_trade_submitted;
}
::player::BLUE {
out = &self.*.b_trade_submitted;
}
::player::WHITE {
out = &self.*.w_trade_submitted;
}
::player::ORANGE {
out = &self.*.o_trade_submitted;
}
}
return out;
}
func calculate_longest_road_visit(
self: *server_state,
edges: *std::hash_set[[edge]],
edges_visited: *std::hash_set[[edge]],
nodes_visited: *std::hash_set[[node]],
visiting: edge,
player: player
) usize {
if edges_visited.*.contains(visiting) {
return 0;
}
edges_visited.*.insert(visiting);
var longest = 1u;
var adjacent_nodes = adjacent_nodes_to_edge(visiting, &self.*.board.nodes);
defer adjacent_nodes.fini();
var iter = adjacent_nodes.iterator();
for iter.advance() {
if nodes_visited.*.contains(*iter.current()) {
continue;
}
nodes_visited.*.insert(*iter.current());
var town = self.*.board.towns.lookup(*iter.current());
if town.is_value() and town.value().*.player != player {
continue;
}
var edges_visited = std::hash_set[[edge]]::init_assign(edges_visited);
defer edges_visited.fini();
var nodes_visited = std::hash_set[[node]]::init_assign(nodes_visited);
defer nodes_visited.fini();
var adjacent_edges = adjacent_edges_to_node(*iter.current(), &self.*.board.edges);
defer adjacent_edges.fini();
var iter = adjacent_edges.iterator();
for iter.advance() {
if not edges.*.contains(*iter.current()) {
continue;
}
var length = 1 + self.*.calculate_longest_road_visit(edges, &edges_visited, &nodes_visited, *iter.current(), player);
longest = usize::max(longest, length);
}
}
return longest;
}
func calculate_longest_road(self: *server_state, player: player) usize {
var edges = std::hash_set[[edge]]::init();
defer edges.fini();
var iter = self.*.board.roads.iterator();
for iter.advance() {
if iter.current().*.value.*.player != player {
continue;
}
edges.insert(*iter.current().*.key);
}
var longest = 0u;
var iter = edges.iterator();
for iter.advance() {
var edges_visited = std::hash_set[[edge]]::init();
defer edges_visited.fini();
var nodes_visited = std::hash_set[[node]]::init();
defer nodes_visited.fini();
var length = self.*.calculate_longest_road_visit(&edges, &edges_visited, &nodes_visited, *iter.current(), player);
longest = usize::max(longest, length);
}
return longest;
}
func public_victory_points(self: *server_state, player: player) sint {
var victory_points: sint = 0;
var iter = self.*.board.towns.iterator();
for iter.advance() {
var town = *iter.current().*.value;
if town.player != player {
continue;
}
switch town.kind {
::town::SETTLEMENT {
victory_points += 1;
}
::town::CITY {
victory_points += 2;
}
}
}
if self.*.longest_road.is_value() and self.*.longest_road.value() == player {
victory_points += 2;
}
if self.*.largest_army.is_value() and self.*.largest_army.value() == player {
victory_points += 2;
}
return victory_points;
}
func hidden_victory_points(self: *server_state, player: player) sint {
var victory_points: sint = self.*.public_victory_points(player);
var info = self.*.p_info(player);
for i in info.*.dev_card_count {
if info.*.dev_card_array[i].kind == dev_card::VICTORY_POINT {
victory_points += 1;
}
}
return victory_points;
}
func prev_player(self: *server_state, player: player) player {
var idx = usize::MAX;
for i in countof(self.*.turn_order) {
if player == self.*.turn_order[i] {
idx = i;
break;
}
}
assert idx != usize::MAX;
for true {
if idx == 0 {
idx = countof(self.*.turn_order);
}
idx -= 1;
if *self.*.p_live(self.*.turn_order[idx]) {
break;
}
}
return self.*.turn_order[idx];
}
func next_player(self: *server_state, player: player) player {
var idx = usize::MAX;
for i in countof(self.*.turn_order) {
if player == self.*.turn_order[i] {
idx = i;
break;
}
}
assert idx != usize::MAX;
for true {
idx += 1;
idx %= countof(self.*.turn_order);
if *self.*.p_live(self.*.turn_order[idx]) {
break;
}
}
return self.*.turn_order[idx];
}
func game_init_advance(self: *server_state) void {
switch self.*.turn.phase {
phase::GAME_INIT {
if *self.*.p_live(self.*.turn_order[0]) {
self.*.turn.phase = phase::GAME_INIT_BUILD_TOWN;
self.*.turn.player = self.*.turn_order[0];
return;
}
if *self.*.p_live(self.*.turn_order[1]) {
self.*.turn.phase = phase::GAME_INIT_BUILD_TOWN;
self.*.turn.player = self.*.turn_order[1];
return;
}
if *self.*.p_live(self.*.turn_order[2]) {
self.*.turn.phase = phase::GAME_INIT_BUILD_TOWN;
self.*.turn.player = self.*.turn_order[2];
return;
}
if *self.*.p_live(self.*.turn_order[3]) {
self.*.turn.phase = phase::GAME_INIT_BUILD_TOWN;
self.*.turn.player = self.*.turn_order[3];
return;
}
}
else {
std::unreachable(fileof(), lineof());
}
}
}
func change_turn(self: *server_state, player: player) void {
self.*.turn = turn::init(self.*.turn.number + 1, player);
self.*.player_rolled = false;
self.*.dev_card_used = std::optional[[phase]]::EMPTY;
var name = player_to_name(player);
self.*.sendc_all(server_message::init_log_format(
"It's {}'s turn!",
(:[]std::formatter)[
std::formatter::init[[typeof(name)]](&name)]));
}
func process_begin_game(self: *server_state, connection: *connection) void {
if connection.*.player.is_empty() {
connection.*.sendc(server_message::init_rejected("Cannot begin the game (you are not an active player)."));
return;
}
if self.*.turn.phase != phase::GAME_INIT {
connection.*.sendc(server_message::init_rejected("Cannot begin the game (the game has already started)."));
return;
}
self.*.game_init_advance();
}
func process_roll(self: *server_state, connection: *connection) void {
if connection.*.player.is_empty() or connection.*.player.value() != self.*.turn.player {
connection.*.sendc(server_message::init_rejected("Cannot roll dice (it is not your turn)."));
return;
}
if self.*.turn.phase != phase::RESOURCE_PRODUCTION {
connection.*.sendc(server_message::init_rejected("Cannot roll dice (it is not the resource production phase)."));
return;
}
var roll_a = d6();
var roll_b = d6();
var roll = roll_a + roll_b;
self.*.board.d6_a = roll_a;
self.*.board.d6_b = roll_b;
self.*.player_rolled = true;
var name = player_to_name(connection.*.player.value());
self.*.sendc_all(server_message::init_log_format(
"{} rolled {} + {} = {}.",
(:[]std::formatter)[
std::formatter::init[[typeof(name)]](&name),
std::formatter::init[[typeof(roll_a)]](&roll_a),
std::formatter::init[[typeof(roll_b)]](&roll_b),
std::formatter::init[[typeof(roll)]](&roll)]));
if roll == 7 {
if (self.*.r_info.resource_count() <= 7)
and (self.*.b_info.resource_count() <= 7)
and (self.*.w_info.resource_count() <= 7)
and (self.*.o_info.resource_count() <= 7) {
self.*.sendc_all(server_message::init_log("No players need to discard."));
var name = player_to_name(self.*.turn.player);
self.*.sendc_all(server_message::init_log_format(
"Waiting for {} to move the robber.",
(:[]std::formatter)[
std::formatter::init[[typeof(name)]](&name)]));
self.*.turn.phase = phase::ROBBER;
}
else {
self.*.sendc_all(server_message::init_log("Waiting for all players to discard."));
self.*.r_discarded = self.*.r_info.resource_count() <= 7;
self.*.b_discarded = self.*.b_info.resource_count() <= 7;
self.*.w_discarded = self.*.w_info.resource_count() <= 7;
self.*.o_discarded = self.*.o_info.resource_count() <= 7;
self.*.turn.phase = phase::DISCARD;
}
return;
}
var iter = self.*.board.towns.iterator();
for iter.advance() {
var amount: sint = uninit;
var town = *iter.current().*.value;
switch town.kind {
::town::SETTLEMENT {
amount = 1;
}
::town::CITY {
amount = 2;
}
}
var node = *iter.current().*.key;
for i in countof(node.hexes) {
var tile = self.*.board.tiles.lookup(node.hexes[i]);
var tile = tile.value();
if tile.*.number != roll {
continue;
}
var info = self.*.p_info(town.player);
info.*.brick += amount * (:sint)(tile.*.kind == ::tile::HILL);
info.*.ore += amount * (:sint)(tile.*.kind == ::tile::MOUNTAIN);
info.*.sheep += amount * (:sint)(tile.*.kind == ::tile::PASTURE);
info.*.wheat += amount * (:sint)(tile.*.kind == ::tile::FIELD);
info.*.wood += amount * (:sint)(tile.*.kind == ::tile::FOREST);
}
}
self.*.turn.phase = phase::MAIN;
}
func process_end_turn(self: *server_state, connection: *connection) void {
if connection.*.player.is_empty() or connection.*.player.value() != self.*.turn.player {
connection.*.sendc(server_message::init_rejected("Cannot end turn (it is not your turn)."));
return;
}
if self.*.turn.phase != phase::MAIN {
connection.*.sendc(server_message::init_rejected("Cannot end turn (it is not the main phase)."));
return;
}
var name = player_to_name(connection.*.player.value());
self.*.sendc_all(server_message::init_log_format(
"{} ended their turn.",
(:[]std::formatter)[
std::formatter::init[[typeof(name)]](&name)]));
self.*.change_turn(self.*.next_player(self.*.turn.player));
}
func process_build_road(self: *server_state, connection: *connection, road: road) void {
if connection.*.player.is_empty() or connection.*.player.value() != self.*.turn.player {
connection.*.sendc(server_message::init_rejected("Cannot build road (it is not your turn)."));
return;
}
if self.*.turn.player != road.player {
connection.*.sendc(server_message::init_rejected("Cannot build road (it is not your road)."));
return;
}
if self.*.turn.phase != phase::GAME_INIT_BUILD_ROAD and self.*.turn.phase != phase::MAIN and self.*.turn.phase != phase::ROAD_BUILDING_1 and self.*.turn.phase != phase::ROAD_BUILDING_2 {
connection.*.sendc(server_message::init_rejected("Cannot build road (invalid phase)."));
return;
}
var tile_a = self.*.board.tiles.lookup(road.edge.hexes[0]);
var tile_b = self.*.board.tiles.lookup(road.edge.hexes[1]);
var is_road_across_water =
tile_a.value().*.kind == tile::OCEAN and
tile_b.value().*.kind == tile::OCEAN;
if is_road_across_water {
connection.*.sendc(server_message::init_rejected("Cannot build road (cannot build on water)."));
return;
}
var existing = self.*.board.roads.lookup(road.edge);
if existing.is_value() {
connection.*.sendc(server_message::init_rejected("Cannot build road (road exists at location)."));
return;
}
var adjacent_edges = adjacent_edges_to_edge(road.edge, &self.*.board.edges, &self.*.board.nodes);
defer adjacent_edges.fini();
var adjacent_roads = std::hash_set[[::road]]::init();
defer adjacent_roads.fini();
var iter = adjacent_edges.iterator();
for iter.advance() {
var adjacent = self.*.board.roads.lookup(*iter.current());
if adjacent.is_empty() {
continue;
}
var adjacent = adjacent.value();
if adjacent.*.player != road.player {
continue;
}
adjacent_roads.insert(*adjacent);
}
var adjacent_nodes = adjacent_nodes_to_edge(road.edge, &self.*.board.nodes);
defer adjacent_nodes.fini();
var adjacent_towns = std::hash_set[[town]]::init();
defer adjacent_towns.fini();
var iter = adjacent_nodes.iterator();
for iter.advance() {
var adjacent = self.*.board.towns.lookup(*iter.current());
if adjacent.is_empty() {
continue;
}
var adjacent = adjacent.value();
if adjacent.*.player != road.player {
continue;
}
adjacent_towns.insert(*adjacent);
}
if adjacent_roads.count() == 0 and adjacent_towns.count() == 0 {
connection.*.sendc(server_message::init_rejected("Cannot build road (no adjacent road or town)."));
return;
}
var unobstructed_road_count = 0u;
var iter = adjacent_roads.iterator();
for iter.advance() {
var a = adjacent_nodes_to_edge(road.edge, &self.*.board.nodes);
defer a.fini();
var b = adjacent_nodes_to_edge(iter.current().*.edge, &self.*.board.nodes);
defer b.fini();
var i = std::hash_set[[node]]::init_intersection(&a, &b);
defer i.fini();
assert i.count() == 1;
var iter = i.iterator();
iter.advance();
var town = self.*.board.towns.lookup(*iter.current());
if town.is_empty() {
# No town between the two road pieces.
unobstructed_road_count += 1;
continue;
}
var town = town.value();
if town.*.player == road.player {
# Town between the two road pieces belongs to the player
# placing the road.
unobstructed_road_count += 1;
continue;
}
}
if adjacent_towns.count() == 0 and unobstructed_road_count == 0 {
connection.*.sendc(server_message::init_rejected("Cannot build road (road is obstructed by town)."));
return;
}
var player_road_count = 0u;
var iter = self.*.board.roads.iterator();
for iter.advance() {
if iter.current().*.value.*.player == road.player {
player_road_count += 1;
}
}
if player_road_count >= 15 {
connection.*.sendc(server_message::init_rejected("Cannot build road (all roads placed)."));
return;
}
if self.*.turn.phase == phase::GAME_INIT_BUILD_ROAD {
# Ensure that all settlements have one adjoining road. Iterate over
# all towns (all settlements in this case) and verify that (1) the
# settlement has an existing adjacent road, or (2) that the road
# about to be placed will be adjacent to the settlement.
var towns_have_adjacent_roads = true;
var iter = self.*.board.towns.iterator();
for iter.advance() {
var town = *iter.current().*.value;
if town.player != self.*.turn.player {
continue;
}
var town_has_adjacent_road = false;
var adjacent_edges = adjacent_edges_to_node(town.node, &self.*.board.edges);
defer adjacent_edges.fini();
var iter = adjacent_edges.iterator();
for iter.advance() {
if std::eq[[edge]](iter.current(), &road.edge) {
# The road about to be placed is adjacent to the town.
town_has_adjacent_road = true;
break;
}
var iter = self.*.board.roads.iterator();
for iter.advance() {
var road = *iter.current().*.value;
if road.player != self.*.turn.player {
continue;
}
if not adjacent_edges.contains(road.edge) {
continue;
}
# The town already has an adjacent road.
town_has_adjacent_road = true;
break;
}
}
if not town_has_adjacent_road {
towns_have_adjacent_roads = false;
}
}
if not towns_have_adjacent_roads {
connection.*.sendc(server_message::init_rejected("Cannot build road (must place road adjacent to the starting settlement)."));
return;
}
}
var info = self.*.p_info(self.*.turn.player);
if self.*.turn.phase == phase::MAIN {
if not (info.*.brick >= 1 and info.*.wood >= 1) {
connection.*.sendc(server_message::init_rejected("Cannot build road (insufficient resources)."));
return;
}
info.*.brick -= 1;
info.*.wood -= 1;
}
insert_road(&self.*.board.roads, road);
var name = player_to_name(self.*.turn.player);
self.*.sendc_all(server_message::init_log_format(
"{} built a road.",
(:[]std::formatter)[
std::formatter::init[[typeof(name)]](&name)]));
if self.*.turn.phase == phase::GAME_INIT_BUILD_ROAD {
var live_count =
(:usize)*self.*.p_live(player::RED) +
(:usize)*self.*.p_live(player::BLUE) +
(:usize)*self.*.p_live(player::WHITE) +
(:usize)*self.*.p_live(player::ORANGE);
if self.*.board.towns.count() < live_count {
# First placement -> pass to next player in the turn order.
var next = self.*.next_player(self.*.turn.player);
self.*.turn.phase = phase::GAME_INIT_BUILD_TOWN;
self.*.turn.player = next;
}
elif self.*.board.towns.count() == live_count {
# First placement -> place next pair in reverse order.
self.*.turn.phase = phase::GAME_INIT_BUILD_TOWN;
}
elif self.*.board.towns.count() < live_count * 2 {
# Second placement -> pass to prevous player in the turn order.
var prev = self.*.prev_player(self.*.turn.player);
self.*.turn.phase = phase::GAME_INIT_BUILD_TOWN;
self.*.turn.player = prev;
}
else {
# Second placement -> initial placements are over.
self.*.change_turn(self.*.turn.player);
}
}
elif self.*.turn.phase == phase::ROAD_BUILDING_1 {
self.*.turn.phase = phase::ROAD_BUILDING_2;
}
elif self.*.turn.phase == phase::ROAD_BUILDING_2 {
assert self.*.dev_card_used.is_value();
self.*.turn.phase = self.*.dev_card_used.value();
}
}
func process_build_town(self: *server_state, connection: *connection, town: town) void {
if connection.*.player.is_empty() or connection.*.player.value() != self.*.turn.player {
connection.*.sendc(server_message::init_rejected("Cannot build town (it is not your turn)."));
return;
}
if self.*.turn.player != town.player {
connection.*.sendc(server_message::init_rejected("Cannot build town (it is not your town)."));
return;
}
if self.*.turn.phase != phase::GAME_INIT_BUILD_TOWN and self.*.turn.phase != phase::MAIN {
connection.*.sendc(server_message::init_rejected("Cannot build town (invalid phase)."));
return;
}
var existing = self.*.board.towns.lookup(town.node);
if existing.is_value() {
var existing = existing.value();
# Special case for upgrading a settlement to a city.
if not (existing.*.player == town.player and existing.*.kind == ::town::SETTLEMENT and town.kind == ::town::CITY) {
connection.*.sendc(server_message::init_rejected("Cannot build town (town exists at location)."));
return;
}
}
var adjacent_nodes = adjacent_nodes_to_node(town.node, &self.*.board.nodes);
defer adjacent_nodes.fini();
var adjacent_town_count = 0u;
var iter = adjacent_nodes.iterator();
for iter.advance() {
if self.*.board.towns.contains(*iter.current()) {
adjacent_town_count = adjacent_town_count + 1;
}
}
if adjacent_town_count != 0 {
connection.*.sendc(server_message::init_rejected("Cannot build town (adjacent town)."));
return;
}
var adjacent_edges = adjacent_edges_to_node(town.node, &self.*.board.edges);
defer adjacent_edges.fini();
var is_road_adjacent = false;
var iter = adjacent_edges.iterator();
for iter.advance() {
var adjacent = self.*.board.roads.lookup(*iter.current());
if adjacent.is_empty() {
continue;
}
var adjacent = adjacent.value();
if adjacent.*.player != town.player {
continue;
}
is_road_adjacent = true;
}
if not is_road_adjacent and self.*.turn.phase != phase::GAME_INIT_BUILD_TOWN {
connection.*.sendc(server_message::init_rejected("Cannot build town (no adjacent road)."));
return;
}
if self.*.turn.phase == phase::GAME_INIT_BUILD_TOWN and town.kind == ::town::CITY {
connection.*.sendc(server_message::init_rejected("Cannot build town (cannot place a city at the start of the game)."));
return;
}
var player_town_count = 0u;
var iter = self.*.board.towns.iterator();
for iter.advance() {
if iter.current().*.value.*.player == town.player and iter.current().*.value.*.kind == town.kind {
player_town_count = player_town_count + 1;
}
}
if town.kind == ::town::SETTLEMENT and player_town_count >= 5 {
connection.*.sendc(server_message::init_rejected("Cannot build town (all settlements placed)."));
return;
}
if town.kind == ::town::CITY and player_town_count >= 4 {
connection.*.sendc(server_message::init_rejected("Cannot build town (all cities placed)."));
return;
}
var info = self.*.p_info(self.*.turn.player);
if self.*.turn.phase != phase::GAME_INIT_BUILD_TOWN {
switch town.kind {
::town::SETTLEMENT {
if not (info.*.brick >= 1 and info.*.sheep >= 1 and info.*.wheat >= 1 and info.*.wood >= 1) {
connection.*.sendc(server_message::init_rejected("Cannot build settlement (insufficient resources)."));
return;
}
info.*.brick -= 1;
info.*.sheep -= 1;
info.*.wheat -= 1;
info.*.wood -= 1;
}
::town::CITY {
if not (info.*.ore >= 3 and info.*.wheat >= 2) {
connection.*.sendc(server_message::init_rejected("Cannot build city (insufficient resources)."));
return;
}
info.*.ore -= 3;
info.*.wheat -= 2;
}
}
}
insert_town(&self.*.board.towns, town);
var name = player_to_name(self.*.turn.player);
var kind = "settlement";
if town.kind == ::town::CITY {
kind = "city";
}
self.*.sendc_all(server_message::init_log_format(
"{} built a {}.",
(:[]std::formatter)[
std::formatter::init[[typeof(name)]](&name),
std::formatter::init[[typeof(kind)]](&kind)]));
if self.*.turn.phase == phase::GAME_INIT_BUILD_TOWN {
var live_count =
(:usize)*self.*.p_live(player::RED) +
(:usize)*self.*.p_live(player::BLUE) +
(:usize)*self.*.p_live(player::WHITE) +
(:usize)*self.*.p_live(player::ORANGE);
if self.*.board.towns.count() > live_count {
# Second placement -> give resoruces.
var iter = self.*.board.tiles.iterator();
for iter.advance() {
if std::eq[[hex]](&town.node.hexes[0], iter.current().*.key)
or std::eq[[hex]](&town.node.hexes[1], iter.current().*.key)
or std::eq[[hex]](&town.node.hexes[2], iter.current().*.key) {
var info = self.*.p_info(town.player);
var tile = iter.current().*.value;
info.*.brick += (:sint)(tile.*.kind == ::tile::HILL);
info.*.ore += (:sint)(tile.*.kind == ::tile::MOUNTAIN);
info.*.sheep += (:sint)(tile.*.kind == ::tile::PASTURE);
info.*.wheat += (:sint)(tile.*.kind == ::tile::FIELD);
info.*.wood += (:sint)(tile.*.kind == ::tile::FOREST);
}
}
}
self.*.turn.phase = phase::GAME_INIT_BUILD_ROAD;
}
}
func process_buy_dev_card(self: *server_state, connection: *connection) void {
if connection.*.player.is_empty() or connection.*.player.value() != self.*.turn.player {
connection.*.sendc(server_message::init_rejected("Cannot buy development card (it is not your turn)."));
return;
}
if self.*.turn.phase != phase::MAIN {
connection.*.sendc(server_message::init_rejected("Cannot buy development card (it is not the main phase)."));
return;
}
if self.*.dev_cards.count() == 0 {
connection.*.sendc(server_message::init_rejected("Cannot buy development card (no development cards remaining)."));
return;
}