This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
3509 lines (3090 loc) · 120 KB
/
game.js
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
/**
* @file game.js
* @brief Implements the game.
* @author Andrew MacGillivray
* @author Jarrod Grothusen
* @author Luke McCumber
* @author Nhat Nyugen
*/
"use strict";
const debug = false;
/**
* @brief Name of the attribute that specifies how many troops are present in the region
*/
const troop_count_attr = "data-count";
/**
* @brief troop type names as used in troop icon IDs in the game's SVG doc.
*/
const troop_type_names = [
"infantry",
"helicopter",
"armor"
];
/**
* @brief The maximum # of units that can be fighting in a zone at one time.
* @note Indices correspond to troop_type_names
*/
const troop_combat_width = [
400,
10,
20
// useful for debugging combat
// 100,
// 2,
// 2
];
/**
* @brief list of troop size names and the respective maximum troop count
*/
const troop_sizes = {
fandm: 2,
fireteam: 5,
patrol: 10,
section: 20,
platoon: 40,
company: 250,
battalion: 1000,
regiment: 2000,
brigade: 5000
};
/**
* @brief Indicates a more descriptive name for the blufor / opfor
* for display in the UI.
*/
const team_key = {
bf: "<span class=\"bluetext\">NATO</span>",
of: "<span class=\"redtext\">PACT</span>"
};
/**
* @brief use these ids to select a regional polygon
*/
const region_polygon_ids = [
"alpha",
"bravo",
"charlie",
"delta",
"echo",
"foxtrot",
"golf",
"hotel"
];
// todo - can remove?
const region_phonetic_key = {
a: "alpha",
b: "bravo",
c: "charlie",
d: "delta",
e: "echo",
f: "foxtrot",
g: "golf",
h: "hotel"
};
/**
* @brief Indicates the capital of each region
*/
const regions_capitals_key = {
a: "a6",
b: "b5",
c: "c4",
d: "d2",
e: "e3",
f: "f4",
g: "g4",
h: "h1",
i: "i5",
j: "j2"
};
/**
* @brief List of capitals, without index
*/
const regions_capitals = [
"a6",
"b5",
"c4",
"d2",
"e3",
"f4",
"g4",
"h1",
"i5",
"j2"
];
/**
* @brief Sets the reinforcement bonus gained by controlling each capital
*/
const capitals_reinforcements =
{
// Each index is an array of numbers for how many troops the player is granted
// each turn for controlling the region. Each index of the array pertains to the
// same index of the troop_type_names array.
a6: [40, 0, 1],
b5: [20, 0, 0],
c4: [0, 2, 0],
d2: [40, 2, 1],
e3: [40, 2, 1],
f4: [40, 2, 0],
g4: [40, 0, 1],
h1: [20, 0, 0],
i5: [0, 2, 0],
j2: [40, 2, 0]
};
/**
* @brief use these ids to select the entire region group node (including its name).
* These are also used in troop node names
*/
const region_group_ids = [
"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9",
"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9",
"c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9",
"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9",
"e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9",
"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9",
"g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9",
"h0", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9",
"i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7", "i8", "i9",
"j0", "j1", "j2", "j3", "j4", "j5", "j6", "j7", "j8", "j9"
];
/**
* @brief using the region group id as an index, tells what regions are
* connected to each other.
*/
const region_connections = {
a0: ["a1", "a3"],
a1: ["a0", "a2", "a9"],
a2: ["a1", "a3", "a6", "a9"],
a3: ["a0", "a2", "a4", "a5", "a6"],
a4: ["a3", "a5"],
a5: ["a4", "a6", "a7"],
a6: ["a2", "a5", "a7", "a8", "a9"],
a7: ["j0", "j1", "a8", "a6", "a5"],
a8: ["a6", "a7", "a9", "e0"],
a9: ["a1", "a2", "a6", "a8", "b0"],
b0: ["a9", "e0", "d1", "b2", "b1"],
b1: ["b0", "b2", "b3"],
b2: ["b0", "b1", "b3", "b5", "c1", "d0", "d1"],
b3: ["b1", "b2", "b5", "b4"],
b4: ["b3", "b5", "b8", "b9"],
b5: ["b2", "b3", "b4", "b8", "b6", "c1"],
b6: ["b5", "b7", "b8", "c1", "c0"],
b7: ["b6", "b8"],
b8: ["b4", "b5", "b6", "b7", "b9"],
b9: ["b4", "b8"],
c0: ["b6", "b7", "c1", "c2"],
c1: ["b2", "b5", "b6", "c0", "c2", "c9"],
c2: ["c0", "c1", "c4", "c3"],
c3: ["c2", "c4", "c5"],
c4: ["c2", "c3", "c5", "c6", "c7", "c8", "c9"],
c5: ["c3", "c4", "c6"],
c6: ["c4", "c5", "c7"],
c7: ["d6", "c8", "c4", "c6"],
c8: ["d0", "d5", "c7", "c4", "c9"],
c9: ["b2", "d0", "c8", "c4", "c1"],
d0: ["c9", "c8", "d5", "d2", "d1", "b2"],
d1: ["b0", "b2", "d0", "d5", "d2", "e0", "e4"],
d2: ["d0", "d1", "d3", "d4", "d5", "e4"],
d3: ["d2", "d4", "e5", "e8"],
d4: ["d2", "d3", "e9", "d8", "d6", "d5"],
d5: ["d0", "d2", "d4", "d6", "c8"],
d6: ["d4", "d5", "d7", "d8", "c7"],
d7: ["d6", "d8", "d9", "f2", "f3"],
d8: ["d4", "d6", "d7", "d9", "e9"],
d9: ["d7", "d8", "e9", "f2"],
e0: ["e1", "e4", "d1", "b0", "a8"],
e1: ["e0", "e2", "e3", "j0", "j7"],
e2: ["e1", "e3", "e6", "j8", "i0", "i1"],
e3: ["e1", "e2", "e4", "e5"],
e4: ["e0", "e3", "e5", "d2", "d1"],
e5: ["d3", "e3", "e4", "e6","e8"],
e6: ["e2", "e5", "e7","i2", "g0", "g1"],
e7: ["e6", "e8", "e9", "f0", "f1", "g1"],
e8: ["d3", "e5", "e7", "e9"],
e9: ["e7", "e8", "f1", "f2", "d9", "d8", "d4"],
f0: ["e7", "g1", "g3", "f1", "f5", "f6"],
f1: ["f0", "f2", "f5", "e7", "e9"],
f2: ["e9", "f1", "f3", "d7", "d9"],
f3: ["d7", "f2", "f4"],
f4: ["f3", "f5", "f7", "f9"],
f5: ["f0", "f1", "f4", "f6", "f7"],
f6: ["f0", "f5", "f7", "f8", "g4", "g3"],
f7: ["f4", "f5", "f6", "f8", "f9"],
f8: ["g4", "f6", "f7", "f9"],
f9: ["f4", "f7", "f8"],
g0: ["e6", "i2", "h0", "g1", "g2"],
g1: ["e6", "e7", "f0", "g0", "g2", "g3"],
g2: ["h0", "g0", "g1", "g3", "g5", "g6"],
g3: ["g1", "g2", "g5", "g4", "f6", "f0"],
g4: ["g3", "g5", "g9", "f6", "f8"],
g5: ["g3", "g2", "g4", "g6", "g7", "g9"],
g6: ["h0", "h2", "g2", "g5", "g7"],
g7: ["h2", "h6", "h7", "g5", "g6", "g8", "g9"],
g8: ["h7", "h8", "g7", "g9"],
g9: ["g4", "g5", "g7", "g8"],
h0: ["g0", "g2", "g6", "h1", "h2", "i2"],
h1: ["i2", "i6", "h4", "h3", "h2", "h0"],
h2: ["h0", "h1", "h3", "h6", "g6", "g7"],
h3: ["h1", "h2", "h4", "h5", "h6"],
h4: ["i6", "h1", "h3", "h5"],
h5: ["h4", "h3", "h6", "h9"],
h6: ["h2", "h3", "h5", "h7", "h9", "g7"],
h7: ["g7", "h8", "h6", "h9", "h8"],
h8: ["g8", "h7", "h9"],
h9: ["h5", "h6", "h7", "h8"],
i0: ["e2", "j8", "i1", "i3", "i4"],
i1: ["e2", "i0", "i2", "i3"],
i2: ["e6", "g0", "h0", "h1", "i1", "i3", "i6"],
i3: ["i0", "i1", "i2", "i4", "i5", "i6"],
i4: ["j8", "i9", "i5", "i3", "i0"],
i5: ["i3", "i4", "i9", "i8", "i7", "i6"],
i6: ["i3", "i5", "i7", "i2", "h4", "h1"],
i7: ["i5", "i6", "i8"],
i8: ["j9", "i5", "i7", "i9"],
i9: ["i8", "i5", "i4", "j9"],
j0: ["a7", "e1", "j7", "j1"],
j1: ["j0", "j2", "a7"],
j2: ["j1", "j3", "j5", "j6", "j7"],
j3: ["j2", "j5", "j4"],
j4: ["j3", "j3"],
j5: ["j4", "j3", "j2", "j6", "j9"],
j6: ["j2", "j5", "j8", "j7"],
j7: ["e2", "e1", "j0", "j2", "j6", "j8"],
j8: ["e2", "i0", "i4", "j9", "j6", "j7"],
j9: ["j5", "j8", "i9", "i8"]
};
/**
* @brief List of all possible terrain types
*/
const terrain_types = [
"craters",
"open",
"plains",
"forest",
"water",
"urban"
];
/**
* @brief Describes the troop efficiency modifiers for each troop type within each terrain type.
*/
const terrain_mod = {
craters: [
1.4,
1,
0.7
],
open: [
1,
1,
1
],
plains: [
1,
1.8,
1.3
],
forest: [
2,
0.5,
1.2
],
water: [
.9,
2.2,
.7
],
urban: [
2.2,
1.5,
1
]
};
let region_terrain = {
a0: {craters: 0, open: 0.3, plains: 0.9, forest: 0, water: 0, urban:0 },
a1: {craters: 0, open: 0.3, plains: 0.6, forest: 0, water: 0, urban:0 },
a2: {craters: 0, open: 0.3, plains: 0, forest: 0.5, water: 0, urban:0 },
a3: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0 },
a4: {craters: 0, open: 0.3, plains: 0, forest: 0.5, water: 0, urban:0 },
a5: {craters: 0, open: 0.3, plains: 0, forest: 0.5, water: 0, urban:0 },
a6: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0 },
a7: {craters: 0, open: 0.3, plains: 0, forest: 0.5, water: 0.2, urban:0.5 },
a8: {craters: 0, open: 0.3, plains: 0.4, forest: 0.3, water: 0, urban:0 },
a9: {craters: 0, open: 0.3, plains: 0.5, forest: 0.4, water: 0, urban:0 },
b0: {craters: 0, open: 0.3, plains: 0.9, forest: 0.1, water: 0, urban:0 },
b1: {craters: 0, open: 0.3, plains: 0.5, forest: 0.1, water: 0, urban:0 },
b2: {craters: 0, open: 0.3, plains: 0.7, forest: 0.1, water: 0.1, urban:0 },
b3: {craters: 0, open: 0.3, plains: 0.8, forest: 0, water: 0, urban:0 },
b4: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.1, urban:0 },
b5: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.3, urban:0.6 },
b6: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.3, urban:0.5 },
b7: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0.8 },
b8: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0.5 },
b9: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.3, urban:0 },
c0: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.4, urban:0 },
c1: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.4, urban:0.5 },
c2: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0.8 },
c3: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.1, urban:0.9 },
c4: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.1, urban:0.8 },
c5: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0 },
c6: {craters: 0, open: 0.3, plains: 0, forest: 0.2, water: 0.2, urban:0 },
c7: {craters: 0, open: 0.3, plains: 0.3, forest: 0, water: 0, urban:0 },
c8: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0, urban:0.1 },
c9: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0.2 },
d0: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0 },
d1: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0.2 },
d2: {craters: 0, open: 0.3, plains: 0, forest: 0.2, water: 0.2, urban:0.6 },
d3: {craters: 0, open: 0.3, plains: 0, forest: 0.2, water: 0.1, urban:0.5 },
d4: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.1, urban:0 },
d5: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
d6: {craters: 0, open: 0.3, plains: 0.5, forest: 0, water: 0, urban:0 },
d7: {craters: 0, open: 0.3, plains: 0.8, forest: 0, water: 0, urban:0 },
d8: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
d9: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
e0: {craters: 0, open: 0.3, plains: 0.5, forest: 0, water: 0.2, urban:0 },
e1: {craters: 0, open: 0.3, plains: 0, forest: 0.5, water: 0.2, urban:0 },
e2: {craters: 0, open: 0.3, plains: 0.5, forest: 0.3, water: 0, urban:0 },
e3: {craters: 0, open: 0.3, plains: 0, forest: 0.2, water: 0, urban:0.5 },
e4: {craters: 0, open: 0.3, plains: 0, forest: 0.2, water: 0.2, urban:0.5 },
e5: {craters: 0, open: 0.3, plains: 0, forest: 0.2, water: 0.2, urban:0.5 },
e6: {craters: 0, open: 0.3, plains: 0, forest: 0.9, water: 0, urban:0 },
e7: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
e8: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
e9: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
f0: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
f1: {craters: 0, open: 0.3, plains: 0.7, forest: 0, water: 0.1, urban:0 },
f2: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0 },
f3: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0.4 },
f4: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0.8 },
f5: {craters: 0, open: 0.3, plains: 0.3, forest: 0, water: 0.2, urban:0.3 },
f6: {craters: 0, open: 0.3, plains: 0.3, forest: 0, water: 0, urban:0 },
f7: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0.3, urban:0.4 },
f8: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0 },
f9: {craters: 0, open: 0.3, plains: 0, forest: 0.6, water: 0.1, urban:0.3 },
g0: {craters: 0, open: 0.3, plains: 0, forest: 0.5, water: 0, urban:0 },
g1: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0 },
g2: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0 },
g3: {craters: 0, open: 0.3, plains: 0.7, forest: 0, water: 0, urban:0 },
g4: {craters: 0, open: 0.3, plains: 0.1, forest: 0, water: 0.1, urban:0 },
g5: {craters: 0, open: 0.3, plains: 0.2, forest: 0.1, water: 0, urban:0 },
g6: {craters: 0, open: 0.3, plains: 0, forest: 0.4, water: 0, urban:0 },
g7: {craters: 0, open: 0.3, plains: 0, forest: 1, water: 0, urban:0 },
g8: {craters: 0, open: 0.3, plains: 0.4, forest: 0.3, water: 0, urban:0 },
g9: {craters: 0, open: 0.3, plains: 0.7, forest: 0, water: 0.2, urban:0 },
h0: {craters: 0, open: 1, plains: 0, forest: 0, water: 0, urban:0 },
h1: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0 },
h2: {craters: 0, open: 0.3, plains: 0.3, forest: 0, water: 0, urban:0 },
h3: {craters: 0, open: 0.3, plains: 0.5, forest: 0.1, water: 0, urban:0 },
h4: {craters: 0, open: 0.3, plains: 0, forest: 0.6, water: 0, urban:0 },
h5: {craters: 0, open: 0.3, plains: 0.2, forest: 0.5, water: 0, urban:0 },
h6: {craters: 0, open: 0.3, plains: 0.8, forest: 0, water: 0, urban:0 },
h7: {craters: 0, open: 0.3, plains: 0, forest: 1, water: 0, urban:0 },
h8: {craters: 0, open: 0.3, plains: 0, forest: 0.4, water: 0, urban:0 },
h9: {craters: 0, open: 0.3, plains: 0.5, forest: 0, water: 0, urban:0 },
i0: {craters: 0, open: 0.3, plains: 0.5, forest: 0.2, water: 0, urban:0 },
i1: {craters: 0, open: 0.3, plains: 0.5, forest: 0, water: 0.1, urban:0 },
i2: {craters: 0, open: 0.3, plains: 0, forest: 0, water: 0.2, urban:0 },
i3: {craters: 0, open: 0.3, plains: 0.3, forest: 0.4, water: 0.1, urban:0 },
i4: {craters: 0, open: 0.3, plains: 0, forest: 0.9, water: 0, urban:0 },
i5: {craters: 0, open: 0.3, plains: 0.5, forest: 0.2, water: 0, urban:0 },
i6: {craters: 0, open: 0.3, plains: 0.2, forest: 0.4, water: 0, urban:0 },
i7: {craters: 0, open: 0.3, plains: 0.3, forest: 0.5, water: 0, urban:0 },
i8: {craters: 0, open: 0.3, plains: 0.3, forest: 0.3, water: 0, urban:0 },
i9: {craters: 0, open: 0.3, plains: 0.4, forest: 0.5, water: 0, urban:0 },
j0: {craters: 0, open: 0.3, plains: 0, forest: 0.2, water: 0.2, urban:0.6 },
j1: {craters: 0, open: 0.3, plains: 0, forest: 0.5, water: 0, urban:0.2 },
j2: {craters: 0, open: 0.3, plains: 0, forest: 0.4, water: 0, urban:0 },
j3: {craters: 0, open: 0.3, plains: 0, forest: 0.4, water: 0, urban:0 },
j4: {craters: 0, open: 0.3, plains: 0, forest: 0.3, water: 0, urban:0 },
j5: {craters: 0, open: 0.3, plains: 0, forest: 0.7, water: 0, urban:0 },
j6: {craters: 0, open: 0.3, plains: 0, forest: 0.8, water: 0, urban:0 },
j7: {craters: 0, open: 0.3, plains: 0, forest: 0.6, water: 0, urban:0 },
j8: {craters: 0, open: 0.3, plains: 0.3, forest: 0.7, water: 0, urban:0 },
j9: {craters: 0, open: 0.3, plains: 0.2, forest: 0.8, water: 0, urban:0 },
};
/**
* @brief Describes the state of ownership / troop presence in each region at the start
* of the game. Used by the Game class to initialize all force objects.
*/
const game_setup = {
//{side: bf/of, force:[infantry, helicopter, a
a0: {side: "neutral", force: [0, 0, 0]},
a1: {side: "neutral", force: [0, 0, 0]},
a2: {side: "neutral", force: [0, 0, 0]},
a3: {side: "neutral", force: [0, 0, 0]},
a4: {side: "neutral", force: [0, 0, 0]},
a5: {side: "neutral", force: [0, 0, 0]},
a6: {side: "bf", force: [1200, 2, 20]},
a7: {side: "bf", force: [225, 0, 6]},
a8: {side: "bf", force: [500, 0, 8]},
a9: {side: "neutral", force: [0, 0, 0]},
b0: {side: "neutral", force: [0, 0, 0]},
b1: {side: "neutral", force: [0, 0, 0]},
b2: {side: "neutral", force: [0, 0, 0]},
b3: {side: "neutral", force: [0, 0, 0]},
b4: {side: "neutral", force: [0, 0, 0]},
b5: {side: "bf", force: [850, 4, 12]},
b6: {side: "neutral", force: [0, 0, 0]},
b7: {side: "neutral", force: [0, 0, 0]},
b8: {side: "neutral", force: [0, 0, 0]},
b9: {side: "neutral", force: [0, 0, 0]},
c0: {side: "neutral", force: [0, 0, 0]},
c1: {side: "bf", force: [120, 0, 4]},
c2: {side: "neutral", force: [0, 0, 0]},
c3: {side: "neutral", force: [0, 0, 0]},
c4: {side: "bf", force: [650, 6, 0]},
c5: {side: "neutral", force: [0, 0, 0]},
c6: {side: "neutral", force: [0, 0, 0]},
c7: {side: "neutral", force: [0, 0, 0]},
c8: {side: "neutral", force: [0, 0, 0]},
c9: {side: "bf", force: [40, 1, 0]},
d0: {side: "neutral", force: [0, 0, 0]},
d1: {side: "neutral", force: [0, 0, 0]},
d2: {side: "bf", force: [1850, 0, 38]},
d3: {side: "of", force: [250, 0, 12]},
d4: {side: "bf", force: [250, 0, 12]},
d5: {side: "neutral", force: [0, 0, 0]},
d6: {side: "neutral", force: [0, 0, 0]},
d7: {side: "neutral", force: [0, 0, 0]},
d8: {side: "bf", force: [350, 8, 0]},
d9: {side: "bf", force: [200, 0, 8]},
e0: {side: "bf", force: [1500, 12, 28]},
e1: {side: "of", force: [1500, 12, 28]},
e2: {side: "neutral", force: [0, 0, 0]},
e3: {side: "of", force: [1850, 0, 38]},
e4: {side: "bf", force: [350, 0, 14]},
e5: {side: "of", force: [350, 0, 14]},
e6: {side: "neutral", force: [0, 0, 0]},
e7: {side: "neutral", force: [0, 0, 0]},
e8: {side: "of", force: [350, 0, 18]},
e9: {side: "of", force: [350, 8, 0]},
f0: {side: "neutral", force: [0, 0, 0]},
f1: {side: "of", force: [520, 6, 14]},
f2: {side: "bf", force: [520, 6, 14]},
f3: {side: "bf", force: [300, 0, 7]},
f4: {side: "bf", force: [1990, 0, 32]},
f5: {side: "of", force: [895, 0, 16]},
f6: {side: "of", force: [300, 0, 7]},
f7: {side: "neutral", force: [0, 0, 0]},
f8: {side: "neutral", force: [0, 0, 0]},
f9: {side: "bf", force: [450, 4, 10]},
g0: {side: "neutral", force: [0, 0, 0]},
g1: {side: "neutral", force: [0, 0, 0]},
g2: {side: "neutral", force: [0, 0, 0]},
g3: {side: "neutral", force: [0, 0, 0]},
g4: {side: "of", force: [450, 4, 10]},
g5: {side: "neutral", force: [0, 0, 0]},
g6: {side: "neutral", force: [0, 0, 0]},
g7: {side: "neutral", force: [0, 0, 0]},
g8: {side: "neutral", force: [0, 0, 0]},
g9: {side: "neutral", force: [0, 0, 0]},
h0: {side: "neutral", force: [0, 0, 0]},
h1: {side: "of", force: [650, 6, 0]},
h2: {side: "neutral", force: [0, 0, 0]},
h3: {side: "neutral", force: [0, 0, 0]},
h4: {side: "neutral", force: [0, 0, 0]},
h5: {side: "neutral", force: [0, 0, 0]},
h6: {side: "neutral", force: [0, 0, 0]},
h7: {side: "neutral", force: [0, 0, 0]},
h8: {side: "neutral", force: [0, 0, 0]},
h9: {side: "neutral", force: [0, 0, 0]},
i0: {side: "neutral", force: [0, 0, 0]},
i1: {side: "neutral", force: [0, 0, 0]},
i2: {side: "of", force: [40, 2, 0]},
i3: {side: "of", force: [120, 0, 4]},
i4: {side: "neutral", force: [0, 0, 0]},
i5: {side: "of", force: [850, 4, 12]},
i6: {side: "neutral", force: [0, 0, 0]},
i7: {side: "neutral", force: [0, 0, 0]},
i8: {side: "neutral", force: [0, 0, 0]},
i9: {side: "neutral", force: [0, 0, 0]},
j0: {side: "of", force: [500, 0, 8]},
j1: {side: "of", force: [225, 0, 6]},
j2: {side: "of", force: [1200, 2, 20]},
j3: {side: "neutral", force: [0, 0, 0]},
j4: {side: "neutral", force: [0, 0, 0]},
j5: {side: "neutral", force: [0, 0, 0]},
j6: {side: "neutral", force: [0, 0, 0]},
j7: {side: "neutral", force: [0, 0, 0]},
j8: {side: "neutral", force: [0, 0, 0]},
j9: {side: "neutral", force: [0, 0, 0]},
};
/**
* @brief Shorthand for "opfor" used in SVG node class names
*/
const opfor_prefix = "of";
/**
* @brief Shorthand for "blufor" used in SVG node class names
*/
const blufor_prefix = "bf";
/**
* @brief Determines the class name of the troop-size indicator most appropriate
* for the given number of troops.
* @param {number} size
* @returns string
* The name of the troop-size indicator that best fits the size.
*/
function getBestTroopCountSymbol( size )
{
let icon_sizes = [
2,
5,
10,
20,
40,
250,
1000,
2000,
5000
];
let icon_names = [
"fandm",
"fireteam",
"patrol",
"section",
"platoon",
"company",
"battalion",
"regiment",
"brigade"
];
for (let i = 0; i < icon_sizes.length; i++)
{
if (size <= icon_sizes[i]){
return icon_names[i];
}
}
// if no match is found, return the biggest supported troop symbol.
return "brigade";
}
/**
* @brief Determines if the given region is the capital
* @param {string} region_id
* The ID of the region as a two-character string, e.g. "A0".
* @returns bool
*/
function isCapitalRegion( region_id )
{
region_id = region_id.toLowerCase();
return (region_id == regions_capitals_key[region_id[0]]);
}
/**
* @brief Class containing static methods to work with the game's user interface
*/
class GameUI {
/**
* @brief Posts the provided message to the game's log (UI component)
*/
static log( message, classlist = "" )
{
let log = document.getElementById("log");
let date = new Date;
let dateStr = "";
let container = document.createElement("p");
if ( classlist != "" )
container.setAttribute("class", classlist);
container.setAttribute("id", "l" + log_entries);
date.setTime(Date.now());
dateStr = (date.getHours().toString().length < 2) ? "0" + date.getHours().toString() : date.getHours().toString();
dateStr += ":"
dateStr += (date.getMinutes().toString().length < 2) ? "0" + date.getMinutes().toString() : date.getMinutes().toString();
dateStr += ":"
dateStr += (date.getSeconds().toString().length < 2) ? "0" + date.getSeconds().toString() : date.getSeconds().toString();
container.innerHTML = "<span class=\"date\">" + dateStr + "</span>" + message;
if (log_entries-1 < 0)
log.appendChild(container);
else
log.insertBefore(container, document.getElementById("l" + (log_entries-1)));
log_entries++;
}
/**
* @brief update the ownership of a region
* @note could replace parameters with just a force object, since force knows region and owner
* @todo if kept this way, replace owner type with enum
* @param {string} region_phonetic
* @param {string} owner
* Should be "of", "bf", or "neutral"
*/
static setRegionOwner( region_phonetic, owner )
{
// If the owner isn't neutral, set the owner class
// Forcibly prevent neutral HQ regions
if (!isCapitalRegion(region_phonetic) || owner != "neutral")
document.getElementById(region_phonetic).setAttribute("class", "region " + owner);
// Update the micro-icon for the region
if (owner != "neutral")
{
let os = (owner == "bf") ? "of" : "bf";
// hide other-side ownership indicator
document.getElementById("s-" + os + "-" + region_phonetic).classList.toggle("sh", true);
// show current-side ownership indicator
document.getElementById("s-" + owner + "-" + region_phonetic).classList.toggle("sh", false);
} else {
document.getElementById("s-bf-" + region_phonetic).classList.toggle("sh", true);
document.getElementById("s-of-" + region_phonetic).classList.toggle("sh", true);
}
// Update the macro-icon if the region is a capital
if (isCapitalRegion(region_phonetic))
{
let os = (owner == "bf") ? "of" : "bf";
document.getElementById("s-" + os + "-" + region_phonetic[0]).classList.toggle("sh", true);
document.getElementById("s-" + owner + "-" + region_phonetic[0]).classList.toggle("sh", false);
}
// If the region is a capital, update the HQ unit display
if (isCapitalRegion(region_phonetic) && owner != "neutral")
{
let os = (owner == "bf") ? "of" : "bf";
let hq = document.getElementById("hq_" + os + "_" + region_phonetic[0]);
if (hq.classList.contains("hq"))
{
hq.classList.remove("hq");
hq.classList.add("hq_np");
}
hq = document.getElementById("hq_" + owner + "_" + region_phonetic[0]);
if (hq.classList.contains("hq_np"))
{
hq.classList.remove("hq_np");
hq.classList.add("hq");
}
}
}
/**
* @brief Returns an array of Unit objects based on the troops present in the region.
* @param {string} region_letter
* @returns
*/
static getUnitsInRegion( region_letter )
{
region_letter = region_letter.toLowerCase();
let units = [];
let team = document.getElementById(region_letter).classList.item(1);
// If the region is empty, return
if (team == "neutral") return units;
// otherwise, get the correct prefix for the team whose troops are present
team = (team == "blufor") ? team = blufor_prefix : team = opfor_prefix;
// then get each troop in the region
troop_type_names.forEach((unitType) => {
// node id format: [teamprefix]_[regionletter]_[trooptype]
let selector = team + "_" + region_letter + "_" + unitType;
// if (debug) console.log(selector);
let node = document.getElementById(selector);
if (node.classList.contains("t"))
{
// parse the node into a unit object
units.push( new Unit(unitType, region_letter, node.getAttribute("data-count"), team) );
} else {
units.push( null );
}
});
return units;
}
static updateUnitDisplay(unit)
{
let node = unit.side + "_" + unit.region + "_" + unit.type;
if (debug) console.log(node);
// node = document.getElementById(node);
//if (debug) console.log("Troop count: " + unit.count);
if (unit.side == "neutral")
return;
document.getElementById(node).setAttribute("data-count", unit.count)
// Hide or unhide the unit based on its count.
if (unit.count <= 0) {
// if (debug) console.log("Hiding unit.");
document.getElementById(node).setAttribute("class", "t_np");
// make the troop count hidden as well
let sz = document.getElementById(node).querySelector(".tc");
if (sz != null){
sz.classList.remove("tc");
sz.classList.add("tc_h");
}
}
else if (unit.count > 0){
// if (debug) console.log("Revealing unit.");
document.getElementById(node).setAttribute("class", "t " + unit.side);
// if (debug) console.log(".tc_h." + getBestTroopCountSymbol(unit.count));
// If the troop already existed, remove its old count indicator first
let sz0 = document.getElementById(node).querySelector(".tc");
if (sz0 != null) {
sz0.classList.remove("tc");
sz0.classList.add("tc_h");
}
// Make the appropriate troop count icon visible
let sz = document.getElementById(node).querySelector("." + getBestTroopCountSymbol(unit.count));
if (sz == null ){
// todo - test in project 4
// alert("Unable to find size symbol for " + unit.side + " " + unit.type + " with count " + unit.count);
return;
}
sz.classList.remove("tc_h");
sz.classList.add("tc");
//document.getElementById(node).querySelector("." + getBestTroopCountSymbol(unit.count)).setAttribute("class", "tc");
}
}
/**
* @brief WIP function to generate an animation showing the movement of troops from the src region to the dst region.
* @warning not finished
* @param {*} originForce
* @param {*} destinationForce
*/
static animateUnitMove( originForce, destinationForce )
{
if (0) {
troop_type_names.forEach((unit) => {
let node = originForce.side + "_" + originForce.region + "_" + unit;
node = document.getElementById(node);
// if the troop is present, add the animation to move it to the target region
if (!(node.classList.contains("t_np")))
{
let target = destinationForce.side + "_" + destinationForce.region + "_" + unit;
if (debug) console.log("Target: " + target);
target = document.getElementById(target);
target = target.getBBox();
let animation = document.createElement("animateTransform");
animation.setAttribute("attributeName", "transform");
animation.setAttribute("attributeType", "XML");
animation.setAttribute("type", "translate");
// animation.setAttribute("from", node.getBBox().x + " " + node.getBBox().y);
// animation.setAttribute("to", target.getBBox().x + " " + target.getBBox().y);
animation.setAttribute("from", "0 0");
animation.setAttribute("to", ((-1)*(node.getBBox().x - target.x)).toString() + " " + ((-1)*(node.getBBox().y - target.y)).toString());
animation.setAttribute("dur", "1s");
// animation.setAttribute("repeatCount", "indefinite");
node.innerHTML += animation.outerHTML;
// node.addEventListener("animation")
}
});
} else {
troop_type_names.forEach((unit) => {
let node = destinationForce.side + "_" + destinationForce.region + "_" + unit;
node = document.getElementById(node);
// if the troop is present, add the animation to move it to the target region
if (!(node.classList.contains("t_np")))
{
let target = originForce.side + "_" + originForce.region + "_" + unit;
if (debug) console.log("Target: " + target);
target = document.getElementById(target);
target = target.getBBox();
let animation = document.createElement("animateTransform");
animation.setAttribute("attributeName", "transform");
animation.setAttribute("attributeType", "XML");
animation.setAttribute("type", "translate");
// animation.setAttribute("from", node.getBBox().x + " " + node.getBBox().y);
// animation.setAttribute("to", target.getBBox().x + " " + target.getBBox().y);
animation.setAttribute("from", ((-1)*(node.getBBox().x - target.x)).toString() + " " + ((-1)*(node.getBBox().y - target.y)).toString());
animation.setAttribute("to", "0 0");
animation.setAttribute("dur", "1s");
// animation.setAttribute("repeatCount", "i");
node.innerHTML += animation.outerHTML;
}
});
}
}
/**
* @brief Creates combat animations to visually indicate a firefight between the source and target units.
* @param {Unit} source
* @param {array} targets
* Array of Unit objects that the source is firing at.
* @param {number} tick
* The tick # of the combat generating the animation. Used so that old animations can be removed.
*/
static animateUnitCombat( source, targets, tick )
{
// let unitType = source._type;
let container = document.getElementById("combat-layer");
let craterctn = document.getElementById("crater-layer");
let sbb = document.getElementById(source.id).getBBox();
if (source._count == 0)
return;
let sx = sbb.x + (Math.random()*sbb.width);
let sy = sbb.y + (Math.random()*sbb.height);
// Remove old animations
if (tick > 0)
{
let old_anims = document.getElementsByClassName("cbt_t_" + (tick-1).toString());
for (let i = old_anims.length-1; i >= 0; i--)
old_anims[i].remove();
}
let cl = 0;
let reftgt = null;
let refbb;
let refx, refy, refw, refh; // this allows us to get the width and heigh just once, avoiding a reflow/repaint event
let cg = document.createElement('g');
cg.setAttribute("id", 'craters_' + battle_ct + '_' + tick + '_' + source.side + '_' + source._type);
// Add firing animations for the source unit towards the enemy units
for (let i = 0; i < 3; i++)
{
let tx, ty;
if (targets[i] != null && targets[i].count > 0 && Math.random() > 0.5)
{
if (reftgt == null)
{
reftgt = targets[i];
refbb = document.getElementById(reftgt.region + "r").getBBox();
refx = refbb.x;
refy = refbb.y;
refw = refbb.width;
refh = refbb.height;
}
let tbb = document.getElementById(targets[i].id).getBBox();
tx = tbb.x + (Math.random() * tbb.width);
ty = tbb.y + (Math.random() * tbb.height);
// on rare occasions, make the animation visibly miss its target
if (Math.random() < 0.2)
{
let xs = (Math.random() > 0.5) ? (-1) : (1);
let ys = (Math.random() > 0.5) ? (-1) : (1);
let df = (Math.random() < 0.05) ? 2 : 6;
tx += (2*Math.random()*xs*tbb.width);
ty += (2*Math.random()*ys*tbb.height);
}
let fire = document.createElement("path");
fire.setAttribute("d",
"M " + sx.toString() + " " + sy.toString() + " " +
"L " + tx.toString() + " " + ty.toString());
fire.setAttribute("class", "cbtFire " + source._type + " " + source._side + " cbt_no_" + battle_ct + " cbt_t_" + tick);
container.innerHTML += fire.outerHTML;
}
// Add craters to enemy region if the type is armor or helicopter
// Note that craters are not removed at the end of combat
if (reftgt != null && troop_type_names.indexOf(source._type) > 0)
{
let ct = Math.floor(2*Math.random());
if (ct == 0) return;
let cgid = "crater_c" + battle_ct + "_t" + tick + "_" + source._side + "_" + source.type;
for (let i = 0; i < ct; i++)
{
let circle = document.createElement("circle");
circle.setAttribute("cx", (refx + (Math.random() * refw)).toString());
circle.setAttribute("cy", (refy + (Math.random() * refh)).toString());
circle.setAttribute("r", (Math.random() * 6));
circle.setAttribute("class", "crater");
circle.setAttribute("style", "animation-delay: " + (0.5*Math.random()) + "s");
circle.setAttribute("id", cgid + "_" + i);
cg.appendChild(circle);
cl++;
region_terrain[reftgt.region]["craters"] += 0.003
}
}
}
if (cl > 0) {
craterctn.innerHTML += cg.innerHTML;
}
let cc = document.getElementsByClassName("crater");
for (let e = cc.length-1; e >= 0; e--)
{
cc[e].addEventListener("animationend", GameUI.craterCb, true);
cc[e].addEventListener("animationiteration", GameUI.craterCb, true);
}
}
/**
* @brief Removes the animation class from the crater
* @param {Event} e
*/
static craterCb( e )
{
document.getElementById(e.target.id).setAttribute("class", "crater_filled");
}
/**
* @brief Replaces the "crater" class with "crater_filled", ending any active animations.
* Useful if called at the end of combat, to ensure old craters do not replay their
* "explosion" animation when new combat begins.
*/
static craterFix()
{
let cc = document.getElementsByClassName("crater");
for (let e = cc.length-1; e >= 0; e--)
{
cc[e].setAttribute("class", "crater_filled");
}
}
/**
* @brief Removes combat animations from the specified battle number
* @param {number} battle_no
*/
static removeCombatAnimations( battle_no )
{
let old_anims = document.getElementsByClassName("cbt_no_" + battle_no );
for (let i = old_anims.length-1; i >= 0; i--)
old_anims[i].remove();
}
/**
* @brief Adds an arrow to the map indicating pending movement by a force from one region
* to another. Returns the ID of the arrow so that it can be removed later.
* @todo Add global counter for # arrows and use it to determine unique IDs?
* @param {string} side
* Which team owns the force
* @param {string} origin
* The ID of the origin cell
* @param {string} destination
* The ID of the destination cell
*/
static drawMovementArrow(side, origin, destination)
{
let container = document.getElementById("arrow-container");
let o = document.getElementById(origin).getBBox();
let d = document.getElementById(destination).getBBox();