This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
1215 lines (1042 loc) · 34.4 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 functionality.
*
* @author Jarrod Grothusen
* @author Andrew MacGillivray
*/
"use strict";
/**
* @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 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
};
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"
];
const region_phonetic_key = {
a: "alpha",
b: "bravo",
c: "charlie",
d: "delta",
e: "echo",
f: "foxtrot",
g: "golf",
h: "hotel"
};
/**
* @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 = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"
];
/**
* @brief using the region group id as an index, tells what regions are
* connected to each other.
*/
const region_connections = {
a: [
"b",
"e",
"h"
],
b: [
"a",
"c",
"e"
],
c: [
"b",
"d",
"e",
],
d: [
"c",
"e",
"f",
],
e: [
"a",
"b",
"c",
"d",
"f",
"g",
"h"
],
f: [
"d",
"e",
"g"
],
g: [
"e",
"f",
"h"
],
h: [
"a",
"e",
"g"
]
}
/**
* @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";
function gameLog( 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++;
}
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";
}
function gameRegionClickCallback( e )
{
e.currentTarget.obj._regionClickHandler(e);
}
function gameSelectedRegionClickCallback( e )
{
e.currentTarget.obj._moveCancelHandler(e);
}
function gameMoveRegionClickCallback( e )
{
e.currentTarget.obj._moveHandler(e);
}
/**
* @brief Class containing static methods to interact with the map
*/
class GameMap {
/**
* @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 "opfor", "blufor", or "neutral"
*/
static setRegionOwner( region_phonetic, owner )
{
document.getElementById(region_phonetic).className = "region " + owner;
}
/**
* @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;
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;
//console.log(node);
// node = document.getElementById(node);
//console.log("Troop count: " + unit.count);
document.getElementById(node).setAttribute("data-count", unit.count)
// Hide or unhide the unit based on its count.
if (unit.count <= 0) {
// 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){
// console.log("Revealing unit.");
document.getElementById(node).setAttribute("class", "t");
// 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 Represents the entirety of one team's troops (of multiple types) within a given region.
* @note make sure to prevent any addition of troops from the opposite side to a force when a battle
* begins. Either make moving to an occupied region immediately begin a battle, or copy the
* "side" variable from found units on startup and check when adding troops.
*/
class Force{
constructor(region_group_id){
this._region = region_group_id;
this._unitList = GameMap.getUnitsInRegion(region_group_id);
this._side = "neutral";
this._determineSide();
}
//getters
get side(){
return this._side;
}
get region(){
return this._region;
}
get region_phonetic(){
return region_phonetic_key[this._region];
}
get unitList(){
return this._unitList;
}
get infantry(){
return this._unitList[0];
}
get helicopter(){
return this._unitList[1];
}
get armor(){
return this._unitList[2];
}
get infantryCount(){
return (this._unitList[0] == null) ? 0 : this._unitList[0].count;
}
get helicopterCount(){
return (this._unitList[1] == null) ? 0 : this._unitList[1].count;
}
get armorCount(){
return (this._unitList[2] == null) ? 0 : this._unitList[2].count;
}
get totalCount() {
return this.infantryCount + this.helicopterCount + this.armorCount;
}
//setters
set region(p){
this._region = p;
}
set unitList(uts){
this._unitList = uts;
this._determineSide();
}
// set side(newSide)
// {
// this._side = newSide;
// document.getElementById(this._region).setAttribute("class", "region " + this._side);
// }
//methods
alterForce(list){
for(let i = 0; i < 3; i++){
if(this._unitList[i] != null){
this._unitList[i].alterUnits(list[i]);
GameMap.updateUnitDisplay(this._unitList[i]);
} else {
// todo check later
this._unitList[i] = new Unit(
troop_type_names[i],
this._region,
list[i],
this._side
);
console.log(this._unitList[i] + ": " + list[i]);
GameMap.updateUnitDisplay(this._unitList[i]);
}
}
// Remove empty units
for(let i = 0; i < 3; i++){
if(this._unitList[i].count == 0){
this._unitList[i] = null;
}
}
this._determineSide();
}
// Todo - make "damage" an array to tell what fraction is from inf, hel, armor,
// and give the respective buffs / debuffs vs other units.
distributeDamage( damage )
{
let types_present = 0;
// let fractional_damage = 0;
let newIc = 0;
let newHc = 0;
let newAc = 0;
let af = [ 0, 0, 0 ];
// random number between 0 and 1 to determine how to distribute damage between infantry
// and vehicles. Infantry damage is multiplied by balanceFactor, while vehicle damage
// is divided by it.
let balanceFactor = Math.random();
if (this.infantryCount > 0)
types_present++;
if (this.armorCount > 0)
types_present++;
if (this.helicopterCount > 0)
types_present++;
let damageMatrix = [
balanceFactor * (this.infantryCount / this.totalCount),
(1/balanceFactor) * (this.helicopterCount / this.totalCount),
(1/balanceFactor) * (this.armorCount / this.totalCount)
];
if (this.infantryCount > 0)
{
newIc = this.infantryCount - ((damageMatrix[0] * damage) / this.infantry.hpMod);
if (newIc < 0) {
newIc = 0;
}
af[0] = (newIc - this.infantryCount);
}
if (this.helicopterCount > 0)
{
newHc = this.helicopterCount - ((damageMatrix[1] * damage) / this.helicopter.hpMod);
if (newHc < 0) {
newHc = 0;
}
af[1] = (newHc - this.helicopterCount);
}
if (this.armorCount > 0)
{
newAc = this.armorCount - ((damageMatrix[2] * damage) / this.armor.hpMod);
if (newAc < 0) {
newAc = 0;
}
af[2] = (newAc - this.armorCount);
}
this.alterForce(af);
}
_determineSide()
{
let prev_side = this._side;
this._side = "neutral";
for (let i = 0; i < troop_type_names.length; i++)
{
if (this._unitList[i] != null)
{
this._side = this._unitList[i].side;
break;
}
}
// troop_type_names.forEach((name, i) => {
//});
if (!document.getElementById(this._region).classList.contains(this._side))
{
document.getElementById(this._region).setAttribute("class", "region " + this._side);
}
if (prev_side != "neutral")
document.getElementById("s-" + prev_side + "-" + this._region).classList.toggle("sh", true);
if (this._side != "neutral") {
console.log("s-" + this._side + "-" + this._region);
document.getElementById("s-" + this._side + "-" + this._region).classList.toggle("sh", false);
}
}
}
/**
* @brief represents an individual troop type (infantry, helicopter, or armor)
*/
class Unit{
constructor(type, region, count, side){
this._side = side;
this._id = side + "_" + region + "_" + type;
switch (type) {
case troop_type_names[0]:
this.dmgMod = 2;
this.hpMod = 10;
break;
case troop_type_names[1]:
this.dmgMod = 100;
this.hpMod = 125;
break;
case troop_type_names[2]:
this.dmgMod = 100;
this.hpMod = 250;
}
this.health = this.hpMod * count;
this.type = type;
this.region = region;
// this.movement =
// make a dom to attach to the visual element
}
//getters
get hpMod(){
return this._hpMod;
}
get dmgMod(){
return this._dmgMod;
}
get side(){
return this._side;
}
get type(){
return this._type;
}
get region(){
return this._region;
}
get region_phonetic(){
return region_phonetic_key[this._region];
}
get health(){
return this._health;
}
get count(){
return (this.health > 0) ? Math.ceil((this.health) / (this.hpMod)) : 0;
}
get id()
{
return this._id;
}
//setters
set health(hp){
this._health = hp;
}
set region(pos){
this._region = pos;
}
set count(ct){
this._count = ct;
}
set side(sd){
this._side = sd;
}
set dmgMod(dm){
this._dmgMod = dm;
}
set hpMod(hm){
this._hpMod = hm;
}
set type(t){
this._type = t;
}
//methods
//movement calc(){
// mov = movementFunction(region.terrain, this.type)
// this.movement = mov;
// }
updateHealth(dmg){
this.health = this.health - dmg;
}
alterUnits(cnt){
console.log("Adding " + cnt + " to " + this._id);
this._health += (this.hpMod * cnt);
document.getElementById(this._id).setAttribute("data-count", this.count);
}
}
// let test = new Unit("Inf", 1, 100, "A");
// console.log(test.type);
// console.log(test.health);
// console.log(test.hpMod);
// console.log(test.count);
// console.log(test.side);
/**
* @brief todo
*/
class Terrain{
constructor(pos){
this.type;
this.region;
}
}
function battleCb( obj )
{
obj._tick();
}
class Battle {
/**
* @brief Construct a battle using a defender and attacker.
* Battle takes place in the defending force's cell.
* @param {*} defending_force
* @param {*} attacking_force
*/
constructor( defending_force, attacking_force )
{
this._off = attacking_force;
this._offRefCt = [
this._off.infantryCount,
this._off.helicopterCount,
this._off.armorCount
];
this._def = defending_force;
this._defRefCt = [
this._def.infantryCount,
this._def.helicopterCount,
this._def.armorCount
];
this._refSides = [
this._off.side,
this._def.side
];
// Choose a location for defender casualties to
// retreat to in case of defeat.
this._defFb = null;
let avail_fb = region_connections[this._def.region];
for (let i = 0; i < avail_fb.length; i++)
{
for (let e = 0; e < game.forces.length; e++)
{
if (game.forces[e].region == avail_fb[i] && game.forces[e].side == this._def.side)
this._defFb = game.forces[e];
}
}
this._ticks = 0;
//log.innerHTML += "<p>" + this._off.side.toUpperCase() + " attacked " + this._def.region_phonetic + " from " + this._off.region_phonetic + "</p>\n";
gameLog(
team_key[this._off.side] +
" attacks " + this._def.region_phonetic +
" from " + this._off.region_phonetic +
"<br/><progress id=\"p_battle_" + battle_ct + "\" class=\"battle\" max=\"100\" value=\"50\"></progress>");
// document.getElementById("p_bfof").setAttribute("class", "battle");
// this._interval = null
// document.getElementById("battleind").innerHTML = " - IN BATTLE AT " + defending_force.region_phonetic.toUpperCase();
//document.getElementById("s-" + this._off._side + "-" + this._def._region).classList.toggle("sh", false);
}
start()
{
this._interval = setInterval(battleCb, [200], this);
}
// called by the move handler fn when opposing armies try to
// occupy the same cell.
end()
{
clearInterval(this._interval);
let winside = "";
let verb = "";
let troopLossRecord = "";
if (this._off.totalCount == 0)
{
// partially restore casualties
this._def.alterForce(
[
Math.floor((this._defRefCt[0]-this._def.infantryCount)*Math.random()),
Math.floor((this._defRefCt[1]-this._def.helicopterCount)*Math.random()),
Math.floor((this._defRefCt[2]-this._def.armorCount)*Math.random())
]
);
this._off._side = this._refSides[0];
this._off.alterForce(
[
Math.floor((this._offRefCt[0]-this._off.infantryCount)*Math.random()),
Math.floor((this._offRefCt[1]-this._off.helicopterCount)*Math.random()),
Math.floor((this._offRefCt[2]-this._off.armorCount)*Math.random())
]
);
winside = team_key[this._def.side];
verb = "maintains";
troopLossRecord =
"<pre>" +
"LOSSES:\n" +
"-------\n" +
" ATTACKER" + "\n" +
"INFANTRY: " + (this._off.infantryCount - this._offRefCt[0]).toString() + "\n" +
"ROTORCRAFT: " + (this._off.helicopterCount - this._offRefCt[1]).toString() + "\n" +
"ARMOR: " + (this._off.helicopterCount - this._offRefCt[2]).toString() + "\n\n" +
" DEFENDER" + "\n" +
"INFANTRY: " + (this._def.infantryCount - this._defRefCt[0]).toString() + "\n" +
"ROTORCRAFT: " + (this._def.helicopterCount - this._defRefCt[1]).toString() + "\n" +
"ARMOR: " + (this._def.helicopterCount - this._defRefCt[2]).toString() +
"</pre>";
} else {
winside = team_key[this._off.side];
verb = "takes";
// restore defender losses if a fallback position exists
let def_restored = [
Math.floor((this._defRefCt[0])*Math.random()/2),
Math.floor((this._defRefCt[1])*Math.random()/2),
Math.floor((this._defRefCt[1])*Math.random()/2)
];
if (this._defFb != null)
this._defFb.alterForce(def_restored);
else
def_restored = [0,0,0];
// restore attacker losses
this._off.alterForce(
[
Math.floor((2/3)*(this._offRefCt[0]-this._off.infantryCount)*Math.random()),
Math.floor((2/3)*(this._offRefCt[1]-this._off.helicopterCount)*Math.random()),
Math.floor((2/3)*(this._offRefCt[1]-this._off.armorCount)*Math.random())
]
);
// create the loss record using the numbers from AFTER restorations are performed
// provide notice of where the defenders routed to
troopLossRecord =
"<pre>" +
"LOSSES:\n" +
"-------\n" +
" ATTACKER" + "\n" +
"INFANTRY: " + (this._off.infantryCount - this._offRefCt[0]).toString() + "\n" +
"ROTORCRAFT: " + (this._off.helicopterCount - this._offRefCt[1]).toString() + "\n" +
"ARMOR: " + (this._off.helicopterCount - this._offRefCt[2]).toString() + "\n\n" +
" DEFENDER" + "\n" +
"INFANTRY: " + -(this._defRefCt[0] - def_restored[0]).toString() + "\n" +
"ROTORCRAFT: " + -(this._defRefCt[1] - def_restored[1]).toString() + "\n" +
"ARMOR: " + -(this._defRefCt[2] - def_restored[2]).toString();
if (this._defFb != null)
troopLossRecord += "\n\n" + (def_restored[0]+def_restored[1]+def_restored[2]).toString() + " SURVIVING DEFENDERS\nROUTED TO " + region_phonetic_key[ this._defFb.region ].toUpperCase();
troopLossRecord += "</pre>";
// Update the owner of the cell to be the attacker, and move their troops there.
this._def._side = this._off.side;
this._def.alterForce(
[
this._off.infantryCount,
this._off.helicopterCount,
this._off.armorCount
]
);
this._off.alterForce(
[
(-1)*this._off.infantryCount,
(-1)*this._off.helicopterCount,
(-1)*this._off.armorCount
]
);
}
gameLog( winside + " " + verb + " control of " + this._def.region_phonetic + "." + troopLossRecord);
battle_ct++;
game.battleEndCb();
return;
}
/**
* @brief called repeatedly until the battle ends.
* For now, should deal damage entirely at random
* Will later be updated to match specifications.
*/
_tick()
{
const s = Date.now();
let now = null;
console.log("Tick #" + this._ticks);
this._ticks++;
this._drawProgress();
// Damage by attackers
let dmgo = 0;
// Damage by defenders
let dmgd = 0;
// Calculate dmgo
if (this._off.infantry != null)
dmgo += this._off.infantryCount * this._off.infantry.dmgMod * Math.random();
if (this._off.helicopter != null)
dmgo += this._off.helicopterCount * this._off.helicopter.dmgMod * Math.random();
if (this._off.armor != null)
dmgo += this._off.armorCount * this._off.armor.dmgMod * Math.random();
// Calculate dmgd
if (this._def.infantry != null)
dmgd += this._def.infantryCount * this._def.infantry.dmgMod * Math.random();
if (this._def.helicopter != null)
dmgd += this._def.helicopterCount * this._def.helicopter.dmgMod * Math.random();
if (this._def.armor != null)
dmgd += this._def.armorCount * this._def.armor.dmgMod * Math.random();
// Deal damage to offense
this._off.distributeDamage(dmgd);
// Deal damage to defense
this._def.distributeDamage(dmgo);
if ( this._off.totalCount <= 0 || this._def.totalCount <= 0 )
{
this._drawProgress();
this.end();
}
return;
}
_drawProgress()
{
if (this._off.side == "of")
{
document.getElementById("p_battle_" + battle_ct).setAttribute("value", (this._def.totalCount/(this._def.totalCount+this._off.totalCount+1))*100);
} else {
document.getElementById("p_battle_" + battle_ct).setAttribute("value", (this._off.totalCount/(this._off.totalCount+this._def.totalCount+1))*100);
}
}
}
class Game{
constructor()
{
this.forces = [];
this._initialize_forces();
this._initialize_listeners();
this._state = "initial";
this._currentPlayerTurn = "bf";
this.forces.forEach((force) => {
if (force.side == this._currentPlayerTurn)
{
document.getElementById(force.region).classList.toggle("cpt", true);
}
});
}
getRegionForce(region_letter)
{
for (let i = 0; i < this.forces.length; i++)
{
if (this.forces[i].region == region_letter)
return this.forces[i];
else
continue;
}
return null;
}
moveTroops(src, dst, count){
//assuming count is valid
let A = getRegionForce(src);
let B = getRegionForce(dst);
let removeCount = count.map(function(x){x * -1});
//some verification may be needed for both zones
if(A.side == B.side || A.side == "neutral" || B.side == "neutral"){
//reduce count in source and increase count in destination
A.alterForce(removeCount);
B.alterForce(count);
}else{
console.log("invalid src or dst");
return 0;
}
//check win
return 0;
}
_initialize_forces()
{
region_group_ids.forEach((region) => {
this.forces.push( new Force(region) );
});
console.log(this.forces);
}
_initialize_listeners()
{
// ADD LISTENER FOR REGION CLICK BY CURRENT PLAYER
region_group_ids.forEach((id) => {
console.log("Added event listener for " + id);
document.getElementById(id).addEventListener(
"click",
gameRegionClickCallback,
[false, false]
);
document.getElementById(id).obj = this;
});
}
_changeTurn()
{
this.forces.forEach((force) => {
if (force.side == this._currentPlayerTurn)
document.getElementById(force.region).classList.toggle("cpt", false);
});
// troop counts
let bf_tc = 0;
let of_tc = 0;
// region counts
let bf_rc = 0;
let of_rc = 0;
for(let i = 0; i < this.forces.length; i++)
{
if (this.forces[i].side == "bf")
{
bf_tc += this.forces[i].totalCount;
bf_rc++;
}
else if (this.forces[i].side == "of")
{
of_tc += this.forces[i].totalCount;
of_rc++;
}
}
document.getElementById("p_bfof").setAttribute("value", ((bf_tc)/(bf_tc+of_tc))*100);
if (bf_rc == 0)
{
this._handleWin("of");
return;
} else if (of_rc == 0)
{
this._handleWin("bf");
return;
}
if(this._currentPlayerTurn == "bf"){