-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.sunder
2501 lines (2243 loc) · 97.4 KB
/
client.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 "raylib";
import "smolui";
import "shared.sunder";
# The radius of the outer circle touching the corners of the hexagon.
let HEX_RADIUS = 56.0f32;
# By default, raylib uses an EPSILON value of 0.000001f, which is more precise
# than we would like. Check float equivalence using our own custom EPSILON.
func vec2_feq(a: Vector2, b: Vector2) bool {
let EPSILON = 0.0001f32;
var x_feq = f32::abs(a.x - b.x) < EPSILON;
var y_feq = f32::abs(a.y - b.y) < EPSILON;
return x_feq and y_feq;
}
func tile_color(tile: tile) Color {
var out: Color = uninit;
switch tile.kind {
::tile::DESERT {
out = (:Color){.r = 0xEE, .g = 0xCC, .b = 0x88, .a = 0xFF};
}
::tile::OCEAN {
out = (:Color){.r = 0x66, .g = 0xBB, .b = 0xFF, .a = 0xFF};
}
::tile::FOREST {
out = (:Color){.r = 0x11, .g = 0x55, .b = 0x11, .a = 0xFF};
}
::tile::FIELD {
out = (:Color){.r = 0xDD, .g = 0xAA, .b = 0x22, .a = 0xFF};
}
::tile::HILL {
out = (:Color){.r = 0xCC, .g = 0x55, .b = 0x44, .a = 0xFF};
}
::tile::MOUNTAIN {
out = (:Color){.r = 0x88, .g = 0x88, .b = 0x88, .a = 0xFF};
}
::tile::PASTURE {
out = (:Color){.r = 0x66, .g = 0xAA, .b = 0x66, .a = 0xFF};
}
}
return out;
}
func player_color(player: player) Color {
var out: Color = uninit;
switch player {
::player::RED {
out = RED;
}
::player::BLUE {
out = BLUE;
}
::player::WHITE {
out = WHITE;
}
::player::ORANGE {
out = ORANGE;
}
}
return out;
}
func point_to_pointy_hex(point: Vector2, radius: f32) hex {
var q = (f32::sqrt(3.0)/3.0 * point.x - 1.0/3.0 * point.y) / radius;
var r = (2.0/3.0 * point.y) / radius;
var s = -q - r;
return hex::init_round(q, r, s);
}
func pointy_hex_to_point(hex: hex, radius: f32) Vector2 {
var x = radius * (f32::sqrt(3.0) * (:f32)hex.q + f32::sqrt(3.0)/2.0 * (:f32)hex.r);
var y = radius * (3.0/2.0 * (:f32)hex.r);
return (:Vector2){.x = x, .y = y};
}
func pointy_hex_vertices(hex: hex, radius: f32) [6]Vector2 {
var result: [6]Vector2 = uninit;
var center = pointy_hex_to_point(hex, radius);
for i in 6 {
var x = center.x + f32::cos(f32::degrees_to_radians((:f32)i * 360.0 / 6.0 + 30.0)) * radius;
var y = center.y + f32::sin(f32::degrees_to_radians((:f32)i * 360.0 / 6.0 + 30.0)) * radius;
result[i] = (:Vector2){.x = x, .y = y};
}
return result;
}
func edge_to_points(edge: edge, radius: f32) [2]Vector2 {
var ret: [2]Vector2 = uninit;
var cur = 0u;
var vertices_a = pointy_hex_vertices(edge.hexes[0], radius);
var vertices_b = pointy_hex_vertices(edge.hexes[1], radius);
for i in countof(vertices_a) {
for j in countof(vertices_b) {
if vec2_feq(vertices_a[i], vertices_b[j]) {
ret[cur] = vertices_a[i];
cur = cur + 1;
}
}
}
return ret;
}
func node_to_point(node: node, radius: f32) Vector2 {
var ret: Vector2 = uninit;
var vertices_a = pointy_hex_vertices(node.hexes[0], radius);
var vertices_b = pointy_hex_vertices(node.hexes[1], radius);
var vertices_c = pointy_hex_vertices(node.hexes[2], radius);
for i in countof(vertices_a) {
for j in countof(vertices_b) {
for k in countof(vertices_c) {
if vec2_feq(vertices_a[i], vertices_b[j]) and vec2_feq(vertices_b[j], vertices_c[k]) {
ret = vertices_a[i];
break;
}
}
}
}
return ret;
}
func draw_pointy_hexagon(center: Vector2, radius: f32, color: Color) void {
let SIDES: sint = 6;
let ANGLE: f32 = 30.0; # pointy-top orientation
DrawPoly(center, SIDES, radius, ANGLE, color);
}
func draw_pointy_hexagon_outline(center: Vector2, radius: f32, color: Color) void {
let SIDES: sint = 6;
let ANGLE: f32 = 30.0; # pointy-top orientation
DrawPolyLinesEx(center, SIDES, radius, ANGLE, radius / 8.0, color);
}
func draw_pointy_hexagon_border(center: Vector2, radius: f32, color: Color) void {
let SIDES: sint = 6;
let ANGLE: f32 = 30.0; # pointy-top orientation
var thick: f32 = radius / 16.0;
DrawPolyLinesEx(center, SIDES, radius + thick / 2.0, ANGLE, thick, color);
}
func draw_circle(center: Vector2, radius: f32, color: Color) void {
DrawCircleV(center, radius, color);
}
func draw_circle_outline(center: Vector2, radius: f32, thickness: f32, color: Color) void {
assert thickness <= radius;
let SIDES: sint = 36;
let ANGLE: f32 = 0.0;
DrawPolyLinesEx(center, SIDES, radius, ANGLE, thickness, color);
}
func draw_rect(position: Vector2, w: f32, h: f32, color: Color) void {
DrawRectangleV(position, (:Vector2){.x = w, .y = h}, color);
}
func draw_rect_outline(position: Vector2, w: f32, h: f32, thickness: f32, color: Color) void {
assert thickness <= w;
assert thickness <= h;
# left side
DrawRectangleV(
(:Vector2){
.x = position.x,
.y = position.y,
},
(:Vector2){
.x = thickness,
.y = h,
},
color);
# right side
DrawRectangleV(
(:Vector2){
.x = position.x + w - thickness,
.y = position.y,
},
(:Vector2){
.x = thickness,
.y = h,
},
color);
# top side
DrawRectangleV(
(:Vector2){
.x = position.x,
.y = position.y,
},
(:Vector2){
.x = w,
.y = thickness,
},
color);
# bottom side
DrawRectangleV(
(:Vector2){
.x = position.x,
.y = position.y + h - thickness,
},
(:Vector2){
.x = w,
.y = thickness,
},
color);
}
func draw_number_token(center: Vector2, number: sint, radius: f32, font: Font, font_size: f32) void {
var CIRCLE_RADIUS = radius / 2.0;
let TAN = (:Color){.r = 0xDD, .g = 0xBB, .b = 0x88, .a = 0xFF};
draw_circle(center, CIRCLE_RADIUS, TAN);
# Need a little bit extra radius so that all pixels of the
# outline will cover up the number token circle.
var CIRCLE_OUTLINE_RADIUS = CIRCLE_RADIUS + 1.0;
var CIRCLE_OUTLINE_THICKNESS = CIRCLE_RADIUS / 8.0;
draw_circle_outline(center, CIRCLE_OUTLINE_RADIUS, CIRCLE_OUTLINE_THICKNESS, BLACK);
var size = measure_text_format(font, font_size,
"{}",
(:[]std::formatter)[
std::formatter::init[[sint]](&number)]);
var color = BLACK;
if number == 6 or number == 8 {
color = RED;
}
draw_text_format(font, font_size, color,
(:Vector2){
.x = center.x - size.x / 2.0,
.y = center.y - size.y / 2.0 - CIRCLE_RADIUS / 16.0,
},
"{}",
(:[]std::formatter)[
std::formatter::init[[sint]](&number)]);
assert 2 <= number and number <= 12;
let DOTS = (:[]usize)[
0, # 0 (invalid)
0, # 1 (invalid)
1, # 2
2, # 3
3, # 4
4, # 5
5, # 6
0, # 7 (invalid)
5, # 8
4, # 9
3, # 10
2, # 11
1, # 12
];
var distance = CIRCLE_RADIUS / 3.6;
var width = (:f32)DOTS[(:usize)number] * distance;
for i in DOTS[(:usize)number] {
var position = (:Vector2){
.x = center.x - width / 2.0 + distance * ((:f32)i + 0.5),
.y = center.y + CIRCLE_RADIUS / 2.0,
};
draw_circle(position, distance / 4.0, color);
}
}
func draw_port(port: port, radius: f32, font: Font, font_size: f32) void {
var center = pointy_hex_to_point(port.hex, radius);
var a = node_to_point(port.nodes[0], radius);
var b = node_to_point(port.nodes[1], radius);
var thick: f32 = radius / 8.0;
DrawLineEx(Vector2Lerp(center, a, 0.49), Vector2Lerp(center, a, 0.91), thick + 1.0, BLACK);
DrawLineEx(Vector2Lerp(center, a, 0.50), Vector2Lerp(center, a, 0.90), thick, BROWN);
DrawLineEx(Vector2Lerp(center, b, 0.49), Vector2Lerp(center, b, 0.91), thick + 1.0, BLACK);
DrawLineEx(Vector2Lerp(center, b, 0.50), Vector2Lerp(center, b, 0.90), thick, BROWN);
var text = "<error>";
switch port.kind {
::port::ANY {
text = "3:1";
}
::port::BRICK {
text = "brick 2:1";
}
::port::ORE {
text = "ore 2:1";
}
::port::SHEEP {
text = "sheep 2:1";
}
::port::WHEAT {
text = "wheat 2:1";
}
::port::WOOD {
text = "wood 2:1";
}
}
var size = measure_text_cstr(font, font_size, startof(text));
var position = (:Vector2){.x = center.x - size.x / 2.0, .y = center.y - size.y / 2.0};
draw_text_cstr(font, font_size, BLACK, position, startof(text));
}
func draw_road(a: Vector2, b: Vector2, radius: f32, color: Color) void {
var thick: f32 = radius / 8.0;
DrawLineEx(Vector2Lerp(a, b, 0.09), Vector2Lerp(a, b, 0.91), thick + 1.0, BLACK);
DrawLineEx(Vector2Lerp(a, b, 0.10), Vector2Lerp(a, b, 0.90), thick, color);
}
func draw_settlement(center: Vector2, radius: f32, color: Color) void {
var center = (:Vector2){
.x = center.x,
.y = center.y - radius / 8.0f32, # y-offset
};
var size = (:Vector2){
.x = radius / 3.0,
.y = radius / 4.0,
};
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0 - 1.0, .y = center.y},
(:Vector2){.x = size.x + 2.0, .y = size.y + 1.0},
BLACK);
DrawTriangle(
(:Vector2){.x = center.x - size.x / 2.0 - 1.2, .y = center.y},
(:Vector2){.x = center.x + size.x / 2.0 + 1.2, .y = center.y},
(:Vector2){.x = center.x, .y = center.y - size.y / 2.0 - 1.25},
BLACK);
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0, .y = center.y},
(:Vector2){.x = size.x, .y = size.y},
color);
DrawTriangle(
(:Vector2){.x = center.x - size.x / 2.0, .y = center.y},
(:Vector2){.x = center.x + size.x / 2.0, .y = center.y},
(:Vector2){.x = center.x, .y = center.y - size.y / 2.0},
color);
}
func draw_city(center: Vector2, radius: f32, color: Color) void {
var center = (:Vector2){
.x = center.x,
.y = center.y - radius / 8.0f32, # y-offset
};
var size = (:Vector2){
.x = radius / 2.2,
.y = radius / 4.0,
};
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0 - 1.0, .y = center.y - 1.0},
(:Vector2){.x = size.x + 2.0, .y = size.y + 2.0},
BLACK);
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0 - 1.0, .y = center.y - size.y / 2.0},
(:Vector2){.x = size.x / 2.0 + 2.0, .y = size.y / 2.0 + 1.0},
BLACK);
DrawTriangle(
(:Vector2){.x = center.x - size.x / 2.0 - 1.0, .y = center.y - size.y / 2.0},
(:Vector2){.x = center.x + 1.0, .y = center.y - size.y / 2.0},
(:Vector2){.x = center.x - size.x / 4.0, .y = center.y - size.y - 1.0},
BLACK);
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0, .y = center.y},
(:Vector2){.x = size.x, .y = size.y},
color);
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0, .y = center.y - size.y / 2.0},
(:Vector2){.x = size.x / 2.0, .y = size.y / 2.0},
color);
DrawTriangle(
(:Vector2){.x = center.x - size.x / 2.0, .y = center.y - size.y / 2.0},
(:Vector2){.x = center.x, .y = center.y - size.y / 2.0},
(:Vector2){.x = center.x - size.x / 4.0, .y = center.y - size.y},
color);
}
func draw_robber(center: Vector2, radius: f32, outline: Color) void {
var radius_h = radius / 6.0;
var radius_v = radius / 3.0;
var size = (:Vector2){
.x = radius_h * 2.0,
.y = radius_v / 2.0,
};
DrawEllipse(
(:sint)center.x,
(:sint)center.y,
radius_h + 1.0,
radius_v + 1.0,
outline);
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0 - 1.0, .y = center.y + radius_v - size.y / 2.0 - 1.0},
(:Vector2){.x = size.x + 2.0, .y = size.y + 2.0},
outline);
DrawCircleV(
(:Vector2){.x = center.x, .y = center.y - radius_v},
radius_h * 0.9 + 1.0,
outline);
let COLOR = GRAY;
DrawEllipse(
(:sint)center.x,
(:sint)center.y,
radius_h,
radius_v,
COLOR);
DrawRectangleV(
(:Vector2){.x = center.x - size.x / 2.0, .y = center.y + radius_v - size.y / 2.0},
(:Vector2){.x = size.x, .y = size.y},
COLOR);
DrawCircleV(
(:Vector2){.x = center.x, .y = center.y - radius_v},
radius_h * 0.9,
COLOR);
}
func measure_text(font: Font, size: f32, text: []byte) Vector2 {
var s = std::string::init_from_str(text);
defer s.fini();
let SPACING = 0.0f32;
return MeasureTextEx(font, s.cstr(), size, SPACING);
}
func draw_text(font: Font, size: f32, color: Color, position: Vector2, text: []byte) void {
var s = std::string::init_from_str(text);
defer s.fini();
let SPACING = 0.0f32;
DrawTextEx(font, s.cstr(), position, size, SPACING, color);
}
func measure_text_cstr(font: Font, size: f32, text: *byte) Vector2 {
let SPACING = 0.0f32;
return MeasureTextEx(font, text, size, SPACING);
}
func draw_text_cstr(font: Font, size: f32, color: Color, position: Vector2, text: *byte) void {
let SPACING = 0.0f32;
DrawTextEx(font, text, position, size, SPACING, color);
}
func measure_text_format(font: Font, size: f32, format: []byte, args: []std::formatter) Vector2 {
var s = std::string::init_from_format(format, args);
defer s.fini();
let SPACING = 0.0f32;
return MeasureTextEx(font, s.cstr(), size, SPACING);
}
func draw_text_format(font: Font, size: f32, color: Color, position: Vector2, format: []byte, args: []std::formatter) void {
var s = std::string::init_from_format(format, args);
defer s.fini();
let SPACING = 0.0f32;
DrawTextEx(font, s.cstr(), position, size, SPACING, color);
}
func draw_d6(font: Font, font_size: f32, position: Vector2, side: f32, number: sint, fg: Color, bg: Color) void {
assert 1 <= number and number <= 6;
let BOARDER = 2.0f32;
draw_rect(position, side, side, bg);
draw_rect_outline(position, side, side, BOARDER, BLACK);
var text_size = measure_text_format(font, font_size,
"{}",
(:[]std::formatter)[
std::formatter::init[[sint]](&number)]);
draw_text_format(
font,
font_size,
fg,
(:Vector2){
.x = position.x + side / 2.0 - text_size.x / 2.0,
.y = position.y + side / 2.0 - text_size.y / 2.0,
},
"{}",
(:[]std::formatter)[std::formatter::init[[sint]](&number)]);
}
func screen_w() f32 {
return (:f32)GetScreenWidth();
}
func screen_h() f32 {
return (:f32)GetScreenHeight();
}
func camera() Camera2D {
return (:Camera2D){
.target = (:Vector2){
.x = 0.0f32,
.y = 0.0f32,
},
.offset = (:Vector2){
.x = screen_w() / 2.0 - screen_w() / 5.0,
.y = screen_h() / 2.0 - screen_h() / 8.0,
},
.rotation = 0.0f32,
.zoom = 1.0f32,
};
}
struct ui_dynamic_state {
var kind: enum {
ROOT;
BUILD_ROAD;
BUILD_TOWN;
PLAY_DEV_CARD;
PLAY_DEV_CARD_YEAR_OF_PLENTY;
PLAY_DEV_CARD_MONOPOLY;
TRADE_WITH_BANK;
TRADE_WITH_PLAYERS;
};
var data: union {
var trade_with_bank_resource: std::optional[[resource]]; # x:1 resource->...
var trade_with_players: struct {
var give: resources;
var recv: resources;
};
};
let SELF: ui_dynamic_state = uninit;
func init(kind: typeof(SELF.kind)) ui_dynamic_state {
return (:ui_dynamic_state){
.kind = kind,
.data = uninit,
};
}
}
struct client_state {
var debug: bool;
var connected: bool;
var wav_ding: Wave;
var sound_ding: Sound;
var wav_dice: Wave;
var sound_dice: Sound;
var wav_impact: Wave;
var sound_impact: Sound;
var wav_winner: Wave;
var sound_winner: Sound;
var board_font: Font;
var board_font_size: f32;
var ui_font: *Font;
var ui_font_size: f32;
var ui_log: std::string;
var ui_log_updated: bool;
var ui_textbox_buf: [256]char;
var ui_dynamic_state: ui_dynamic_state;
var ui_resources_select: resources;
var ui_trade_submitted: bool;
var ui: smol::ui;
var player: std::optional[[player]];
var pinfo: hidden_player_info;
var board: board;
var r: public_player_info;
var b: public_player_info;
var w: public_player_info;
var o: public_player_info;
var dev_cards_remaining: sint;
var longest_road: std::optional[[player]];
var largest_army: std::optional[[player]];
var turn_order: [4]player;
var turn: turn;
var trade: trade;
var mouse: Vector2;
var world: Vector2;
var world_hex: hex;
var world_tile: std::optional[[tile]];
var world_edge: std::optional[[edge]];
var world_node: std::optional[[node]];
func init() client_state {
let WAV = embed("assets/BellDing1.wav");
var wav_ding = LoadWaveFromMemory(
startof(".wav"),
(:*u8)startof(WAV),
(:sint)countof(WAV));
var sound_ding = LoadSoundFromWave(wav_ding);
let WAV = embed("assets/Dice25.wav");
var wav_dice = LoadWaveFromMemory(
startof(".wav"),
(:*u8)startof(WAV),
(:sint)countof(WAV));
var sound_dice = LoadSoundFromWave(wav_dice);
let WAV = embed("assets/WoodPlasticHitImpact.wav");
var wav_impact = LoadWaveFromMemory(
startof(".wav"),
(:*u8)startof(WAV),
(:sint)countof(WAV));
var sound_impact = LoadSoundFromWave(wav_impact);
let WAV = embed("assets/SuccessFanfareTrumpets.wav");
var wav_winner = LoadWaveFromMemory(
startof(".wav"),
(:*u8)startof(WAV),
(:sint)countof(WAV));
var sound_winner = LoadSoundFromWave(wav_winner);
# Raylib internally renders fonts to a texture, which may look
# pixilated when scaled up to a higher resolution. For this font, we
# internally load it at a very large font size, and use bilinear
# filtering on the resulting texture, which together will make the font
# legible for a wider range of font sizes.
let FONT_NUMCHARS: sint = '~' - ' ' + 1; # Printable ASCII
let BOARD_FONT_DATA = embed("assets/MinSans-Bold.otf");
var board_font_size = 128.0f32;
var board_font = LoadFontFromMemory(
startof(".ttf"),
(:*u8)startof(BOARD_FONT_DATA),
(:sint)countof(BOARD_FONT_DATA),
(:sint)board_font_size,
std::ptr[[sint]]::NULL,
FONT_NUMCHARS);
SetTextureFilter(board_font.texture, TEXTURE_FILTER_BILINEAR);
let UI_FONT_DATA = embed("assets/RuneScapeUF.ttf");
var ui_font_size = 16.0f32;
var ui_font = std::new[[Font]]();
*ui_font = LoadFontFromMemory(
startof(".ttf"),
(:*u8)startof(UI_FONT_DATA),
(:sint)countof(UI_FONT_DATA),
(:sint)ui_font_size,
std::ptr[[sint]]::NULL,
FONT_NUMCHARS);
var ctx = std::new[[mu_Context]]();
mu_init(ctx);
smol::setup_font(ctx, ui_font);
var ui = smol::ui::init(ctx);
var ui_log = std::string::init();
var log_writer = std::writer::init[[std::string]](&ui_log);
std::print_line(log_writer, "Welcome to Natac!");
var ui_log_updated = true;
var board = board::init();
return (:client_state){
.debug = false,
.connected = false,
.wav_ding = wav_ding,
.sound_ding = sound_ding,
.wav_dice = wav_dice,
.sound_dice = sound_dice,
.wav_impact = wav_impact,
.sound_impact = sound_impact,
.wav_winner = wav_winner,
.sound_winner = sound_winner,
.board_font = board_font,
.board_font_size = board_font_size,
.ui_font = ui_font,
.ui_font_size = ui_font_size,
.ui_log = ui_log,
.ui_log_updated = ui_log_updated,
.ui_textbox_buf = uninit,
.ui_dynamic_state = uninit,
.ui_resources_select = uninit,
.ui_trade_submitted = false,
.ui = ui,
.player = std::optional[[player]]::EMPTY,
.pinfo = uninit,
.board = board,
.r = uninit,
.b = uninit,
.w = uninit,
.o = uninit,
.dev_cards_remaining = uninit,
.longest_road = std::optional[[player]]::EMPTY,
.largest_army = std::optional[[player]]::EMPTY,
.turn_order = (:[4]player)[
player::RED,
player::BLUE,
player::WHITE,
player::ORANGE,
],
.turn = uninit,
.trade = uninit,
.mouse = uninit,
.world = uninit,
.world_hex = uninit,
.world_tile = uninit,
.world_edge = uninit,
.world_node = uninit,
};
}
func fini(self: *client_state) void {
std::delete[[mu_Context]](self.*.ui.context);
self.*.ui_log.fini();
UnloadFont(*self.*.ui_font);
std::delete[[Font]](self.*.ui_font);
UnloadFont(self.*.board_font);
UnloadSound(self.*.sound_winner);
UnloadWave(self.*.wav_winner);
UnloadSound(self.*.sound_ding);
UnloadWave(self.*.wav_ding);
UnloadSound(self.*.sound_impact);
UnloadWave(self.*.wav_impact);
UnloadSound(self.*.sound_dice);
UnloadWave(self.*.wav_dice);
self.*.board.fini();
}
func p_info(self: *client_state, player: player) *public_player_info {
var out: *public_player_info = uninit;
switch player {
::player::RED {
out = &self.*.r;
}
::player::BLUE {
out = &self.*.b;
}
::player::WHITE {
out = &self.*.w;
}
::player::ORANGE {
out = &self.*.o;
}
}
return out;
}
func p_discarded(self: *client_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: *client_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 update_from_server_message(self: *client_state, message: *server_message) void {
switch message.*.kind {
server_message::LOG {
var log_writer = std::writer::init[[std::string]](&self.*.ui_log);
std::print_line(log_writer, message.*.data.log.data());
self.*.ui_log_updated = true;
}
server_message::INFO {
self.*.pinfo = message.*.data.info;
}
server_message::STATE {
var is_new_turn = self.*.turn.number < message.*.data.state.turn.number;
if is_new_turn {
# Reset the UI dynamic state every turn so that all menus and
# menu data appear uniform at the start of every new turn.
self.*.ui_dynamic_state = ui_dynamic_state::init(ui_dynamic_state::ROOT);
}
# We purposefully check that the player of the turn has changed
# rather than the turn number changing in combination with it being
# the current players turn so that one player games are not
# constantly dinging during testing.
var is_your_turn =
self.*.player.is_value() and
self.*.player.value() != self.*.turn.player and
self.*.player.value() == message.*.data.state.turn.player;
if is_your_turn {
PlaySound(self.*.sound_ding);
}
var is_dice_rolled =
self.*.turn.phase == phase::RESOURCE_PRODUCTION and
(message.*.data.state.turn.phase == phase::DISCARD or
message.*.data.state.turn.phase == phase::ROBBER or
message.*.data.state.turn.phase == phase::MAIN);
if is_dice_rolled {
PlaySound(self.*.sound_dice);
}
var old_road_count = self.*.board.roads.count();
var new_road_count = message.*.data.state.board.roads.count();
var old_settlement_count = 0u;
var old_city_count = 0u;
var iter = self.*.board.towns.iterator();
for iter.advance() {
old_settlement_count += (:usize)(iter.current().*.value.*.kind == town::SETTLEMENT);
old_city_count += (:usize)(iter.current().*.value.*.kind == town::CITY);
}
var new_settlement_count = 0u;
var new_city_count = 0u;
var iter = message.*.data.state.board.towns.iterator();
for iter.advance() {
new_settlement_count += (:usize)(iter.current().*.value.*.kind == town::SETTLEMENT);
new_city_count += (:usize)(iter.current().*.value.*.kind == town::CITY);
}
# Since the robber will populate to tile (+0, +0, +0) on the
# intital state message from the server, skip playing a sound for
# the robber being "moved" when the previous board was empty.
var is_robber_moved =
self.*.board.tiles.count() != 0 and
std::ne[[hex]](&self.*.board.robber, &message.*.data.state.board.robber);
if new_road_count > old_road_count or new_settlement_count > old_settlement_count or new_city_count > old_city_count or is_robber_moved {
PlaySound(self.*.sound_impact);
}
var is_game_ended = self.*.turn.phase != phase::GAME_FINI and message.*.data.state.turn.phase == phase::GAME_FINI;
if is_game_ended {
PlaySound(self.*.sound_winner);
}
board::assign(&self.*.board, &message.*.data.state.board);
self.*.turn_order = message.*.data.state.turn_order;
self.*.turn = message.*.data.state.turn;
self.*.trade = message.*.data.state.trade;
self.*.r = message.*.data.state.r;
self.*.b = message.*.data.state.b;
self.*.w = message.*.data.state.w;
self.*.o = message.*.data.state.o;
self.*.dev_cards_remaining =message.*.data.state.dev_cards_remaining;
self.*.longest_road = message.*.data.state.longest_road;
self.*.largest_army = message.*.data.state.largest_army;
}
server_message::PLAYER {
self.*.player = std::optional[[player]]::init_value(message.*.data.player);
}
server_message::REJECTED {
var log_writer = std::writer::init[[std::string]](&self.*.ui_log);
std::print_format_line(
log_writer,
"[ACTION REJECTED]: {}",
(:[]std::formatter)[
std::formatter::init[[std::string]](&message.*.data.rejected)]);
self.*.ui_log_updated = true;
}
}
}
}
func ui_reset_cached_container_size(container: *smol::container, x: sint, y: sint, w: sint, h: sint) void {
container.*.rect.x = x;
container.*.rect.y = y;
container.*.rect.w = w;
container.*.rect.h = h;
}
func ui_labled_number(ui: *smol::ui, label: []byte, value: sint, layout: []sint) void {
assert countof(layout) == 2;
ui.*.push_id(startof(label), countof(label));
ui.*.layout_begin_column();
ui.*.layout_row(layout, 0);
ui.*.text(label);
var s = std::string::init_from_format("{}", (:[]std::formatter)[std::formatter::init[[sint]](&value)]);
defer s.fini();
ui.*.text(s.data());
ui.*.layout_end_column();
ui.*.pop_id();
}
# In HTML this input element is has type "number", but elsewhere it is referred
# to as a spinner for some reason. I'm calling it a "UI Number Select" for now.
#
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number
# https://jqueryui.com/spinner/
# https://www.mathworks.com/help/matlab/ref/uispinner.html
func ui_number_select(ui: *smol::ui, value: *sint, min: sint, max: sint) void {
ui.*.push_id(&value, sizeof(typeof(value)));
ui.*.layout_begin_column();
var button_w: sint = 32;
ui.*.layout_row((:[]sint)[button_w, button_w, -1], 0);
if ui.*.button("-1") {
*value -= 1;
}
if ui.*.button("+1") {
*value += 1;
}
*value = sint::max(min, *value);
*value = sint::min(max, *value);
ui.*.begin_panel_ex("PANEL", smol::OPT_NOSCROLL); # XXX: Duplicate IDs?
var s = std::string::init_from_format("{}", (:[]std::formatter)[std::formatter::init[[sint]](value)]);
defer s.fini();
ui.*.text(s.data());
ui.*.end_panel();
ui.*.layout_end_column();
ui.*.pop_id();
}
let UI_BORDER: sint = 8;
func ui_window_play_dev_card(cstate: *client_state, sender: *message_sender[[client_message]]) void {
var screen_w = screen_w();
var screen_h = screen_h();
var ui_w = (:sint)screen_w / 4;
var ui_h = (:sint)screen_h / 4;
var ui_x = (:sint)screen_w * 2 / 3 - ui_w - 2 * UI_BORDER;
var ui_y = (:sint)screen_h - ui_h - UI_BORDER;
if cstate.*.ui.begin_window_ex("Play a Development Card", smol::rect::init(ui_x, ui_y, ui_w, ui_h), smol::OPT_NOCLOSE) {
ui_reset_cached_container_size(cstate.*.ui.get_current_container(), ui_x, ui_y, ui_w, ui_h);
cstate.*.ui.layout_row((:[]sint)[-1], 0);
if cstate.*.ui.button("Play Knight") {
sender.*.sendc(client_message::init_use_dev_card_knight());
cstate.*.ui_dynamic_state = ui_dynamic_state::init(ui_dynamic_state::ROOT);
}
if cstate.*.ui.button("Play Road Building") {
sender.*.sendc(client_message::init_use_dev_card_road_building());
cstate.*.ui_dynamic_state = ui_dynamic_state::init(ui_dynamic_state::ROOT);
}
if cstate.*.ui.button("Play Year of Plenty") {
cstate.*.ui_dynamic_state = ui_dynamic_state::init(ui_dynamic_state::PLAY_DEV_CARD_YEAR_OF_PLENTY);
}
if cstate.*.ui.button("Play Monopoly") {
cstate.*.ui_dynamic_state = ui_dynamic_state::init(ui_dynamic_state::PLAY_DEV_CARD_MONOPOLY);
}
if cstate.*.ui.button("Back") {
cstate.*.ui_dynamic_state = ui_dynamic_state::init(ui_dynamic_state::ROOT);
}
cstate.*.ui.end_window();
}
}
func ui_window_resource_production(cstate: *client_state, sender: *message_sender[[client_message]]) void {
var screen_w = screen_w();
var screen_h = screen_h();
var ui_w = (:sint)screen_w / 4;
var ui_h = (:sint)screen_h / 4;
var ui_x = (:sint)screen_w * 2 / 3 - ui_w - 2 * UI_BORDER;
var ui_y = (:sint)screen_h - ui_h - UI_BORDER;
if cstate.*.ui.begin_window_ex("Your Turn (resource production)", smol::rect::init(ui_x, ui_y, ui_w, ui_h), smol::OPT_NOCLOSE) {
ui_reset_cached_container_size(cstate.*.ui.get_current_container(), ui_x, ui_y, ui_w, ui_h);
cstate.*.ui.layout_row((:[]sint)[-1], 0);
if cstate.*.ui.button("Roll") {
sender.*.sendc(client_message::init_roll());
}
if cstate.*.ui.button("Play a Development Card") {
cstate.*.ui_dynamic_state = ui_dynamic_state::init(ui_dynamic_state::PLAY_DEV_CARD);
}
cstate.*.ui.end_window();
}
}
func ui_window_play_dev_card_year_of_plenty(cstate: *client_state, sender: *message_sender[[client_message]]) void {
var screen_w = screen_w();
var screen_h = screen_h();
var ui_w = (:sint)screen_w / 4;
var ui_h = (:sint)screen_h / 4;
var ui_x = (:sint)screen_w * 2 / 3 - ui_w - 2 * UI_BORDER;
var ui_y = (:sint)screen_h - ui_h - UI_BORDER;
if cstate.*.ui.begin_window_ex("Select Resources (Year of Plenty)", smol::rect::init(ui_x, ui_y, ui_w, ui_h), smol::OPT_NOCLOSE) {
ui_reset_cached_container_size(cstate.*.ui.get_current_container(), ui_x, ui_y, ui_w, ui_h);