-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_battla.sim.go
1180 lines (1115 loc) · 29.5 KB
/
go_battla.sim.go
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
package ogame
import (
"fmt"
"math"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/olekukonko/tablewriter"
)
const (
smallCargoConst = iota
largeCargoConst
lightFighterConst
heavyFighterConst
cruiserConst
battleshipConst
colonyShipConst
recyclerConst
espionageProbeConst
bomberConst
solarSatelliteConst
destroyerConst
deathstarConst
battlecruiserConst
rocketLauncherConst
lightLaserConst
heavyLaserConst
gaussCannonConst
ionCannonConst
plasmaTurretConst
smallShieldDomeConst
largeShieldDomeConst
)
func isAlive(unit *CombatUnit) bool {
return getUnitHull(unit) > 0
}
// CombatUnit ...
type CombatUnit struct {
PackedInfos uint64
}
const (
maxArmourLevel uint64 = 36
maxShieldLevel uint64 = 42
idMask uint64 = 31
shieldMask uint64 = 8388576
hullMask uint64 = 35184363700224
)
func getUnitID(unit *CombatUnit) uint64 {
return (unit.PackedInfos & idMask) >> 0
}
func getUnitShield(unit *CombatUnit) uint64 {
return (unit.PackedInfos & shieldMask) >> 5
}
func getUnitHull(unit *CombatUnit) uint64 {
return (unit.PackedInfos & hullMask) >> 23
}
func setUnitID(unit *CombatUnit, id uint64) {
unit.PackedInfos &= ^idMask
unit.PackedInfos |= id << 0
}
func setUnitShield(unit *CombatUnit, shield uint64) {
unit.PackedInfos &= ^shieldMask
unit.PackedInfos |= shield << 5
}
func setUnitHull(unit *CombatUnit, hull uint64) {
unit.PackedInfos &= ^hullMask
unit.PackedInfos |= hull << 23
}
type price struct {
Metal int
Crystal int
Deuterium int
}
func (p price) Total() int {
return p.Metal + p.Crystal + p.Deuterium
}
func (p *price) add(n price) {
p.Metal += n.Metal
p.Crystal += n.Crystal
p.Deuterium += n.Deuterium
}
func getUnitPrice(unitID uint64) price {
switch unitID {
case smallCargoConst:
return price{2000, 2000, 0}
case largeCargoConst:
return price{6000, 6000, 0}
case lightFighterConst:
return price{3000, 1000, 0}
case heavyFighterConst:
return price{6000, 4000, 0}
case cruiserConst:
return price{20000, 7000, 2000}
case battleshipConst:
return price{45000, 15000, 0}
case colonyShipConst:
return price{10000, 20000, 10000}
case recyclerConst:
return price{10000, 6000, 2000}
case espionageProbeConst:
return price{0, 1000, 0}
case bomberConst:
return price{50000, 25000, 15000}
case solarSatelliteConst:
return price{0, 2000, 500}
case destroyerConst:
return price{60000, 50000, 15000}
case deathstarConst:
return price{5000000, 4000000, 1000000}
case battlecruiserConst:
return price{30000, 40000, 15000}
case rocketLauncherConst:
return price{2000, 0, 0}
case lightLaserConst:
return price{1500, 500, 0}
case heavyLaserConst:
return price{6000, 2000, 0}
case gaussCannonConst:
return price{20000, 15000, 2000}
case ionCannonConst:
return price{2000, 6000, 0}
case plasmaTurretConst:
return price{50000, 50000, 30000}
case smallShieldDomeConst:
return price{10000, 10000, 0}
case largeShieldDomeConst:
return price{50000, 50000, 0}
}
return price{0, 0, 0}
}
func getUnitBaseShield(unitID uint64) int {
switch unitID {
case smallCargoConst:
return 10
case largeCargoConst:
return 25
case lightFighterConst:
return 10
case heavyFighterConst:
return 25
case cruiserConst:
return 50
case battleshipConst:
return 200
case colonyShipConst:
return 100
case recyclerConst:
return 10
case espionageProbeConst:
return 1 // 0.01
case bomberConst:
return 500
case solarSatelliteConst:
return 1
case destroyerConst:
return 500
case deathstarConst:
return 50000
case battlecruiserConst:
return 400
case rocketLauncherConst:
return 20
case lightLaserConst:
return 25
case heavyLaserConst:
return 100
case gaussCannonConst:
return 200
case ionCannonConst:
return 500
case plasmaTurretConst:
return 300
case smallShieldDomeConst:
return 2000
case largeShieldDomeConst:
return 10000
}
return 0
}
func getUnitBaseWeapon(unitID uint64) uint64 {
switch unitID {
case smallCargoConst:
return 5
case largeCargoConst:
return 5
case lightFighterConst:
return 50
case heavyFighterConst:
return 150
case cruiserConst:
return 400
case battleshipConst:
return 1000
case colonyShipConst:
return 50
case recyclerConst:
return 1
case espionageProbeConst:
return 1 // 0.01
case bomberConst:
return 1000
case solarSatelliteConst:
return 1
case destroyerConst:
return 2000
case deathstarConst:
return 200000
case battlecruiserConst:
return 700
case rocketLauncherConst:
return 80
case lightLaserConst:
return 100
case heavyLaserConst:
return 250
case gaussCannonConst:
return 1100
case ionCannonConst:
return 150
case plasmaTurretConst:
return 3000
case smallShieldDomeConst:
return 1
case largeShieldDomeConst:
return 1
}
return 0
}
func getRapidFireAgainst(unit *CombatUnit, targetUnit *CombatUnit) int {
rf := 0
switch getUnitID(unit) {
case smallCargoConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
}
case largeCargoConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
}
case lightFighterConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
}
case heavyFighterConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
case smallCargoConst:
rf = 3
}
case cruiserConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
case lightFighterConst:
rf = 6
case rocketLauncherConst:
rf = 10
}
case battleshipConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
}
case colonyShipConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
}
case recyclerConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
}
case bomberConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
case ionCannonConst:
rf = 10
case rocketLauncherConst:
rf = 20
case lightLaserConst:
rf = 20
case heavyLaserConst:
rf = 10
}
case destroyerConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
case lightLaserConst:
rf = 10
case battlecruiserConst:
rf = 2
}
case deathstarConst:
switch getUnitID(targetUnit) {
case smallCargoConst:
rf = 250
case largeCargoConst:
rf = 250
case lightFighterConst:
rf = 200
case heavyFighterConst:
rf = 100
case cruiserConst:
rf = 33
case battleshipConst:
rf = 30
case colonyShipConst:
rf = 250
case recyclerConst:
rf = 250
case espionageProbeConst:
rf = 1250
case solarSatelliteConst:
rf = 1250
case bomberConst:
rf = 25
case destroyerConst:
rf = 5
case rocketLauncherConst:
rf = 200
case lightLaserConst:
rf = 200
case heavyLaserConst:
rf = 100
case gaussCannonConst:
rf = 50
case ionCannonConst:
rf = 100
case battlecruiserConst:
rf = 15
}
case battlecruiserConst:
switch getUnitID(targetUnit) {
case espionageProbeConst:
rf = 5
case solarSatelliteConst:
rf = 5
case smallCargoConst:
rf = 3
case largeCargoConst:
rf = 3
case heavyFighterConst:
rf = 4
case cruiserConst:
rf = 4
case battleshipConst:
rf = 7
}
}
return rf
}
func getUnitName(unitID uint64) string {
switch unitID {
case smallCargoConst:
return "Small cargo"
case largeCargoConst:
return "Large cargo"
case lightFighterConst:
return "Light fighter"
case heavyFighterConst:
return "Heavy fighter"
case cruiserConst:
return "Cruiser"
case battleshipConst:
return "Battleship"
case colonyShipConst:
return "Colony ship"
case recyclerConst:
return "Recycler"
case espionageProbeConst:
return "Expionage probe"
case bomberConst:
return "Bomber"
case solarSatelliteConst:
return "Solar satellite"
case destroyerConst:
return "Destroyer"
case deathstarConst:
return "Deathstar"
case battlecruiserConst:
return "Battlecruiser"
case rocketLauncherConst:
return "Rocket launcher"
case lightLaserConst:
return "Light laser"
case heavyLaserConst:
return "Heavy laser"
case gaussCannonConst:
return "Gauss cannon"
case ionCannonConst:
return "Ion cannon"
case plasmaTurretConst:
return "Plasma turret"
case smallShieldDomeConst:
return "Small shield dome"
case largeShieldDomeConst:
return "Large shield dome"
}
return ""
}
func getUnitWeaponPower(unitID uint64, weaponTechno int) uint64 {
return uint64(float64(getUnitBaseWeapon(unitID)) * (1 + 0.1*float64(weaponTechno)))
}
func getUnitInitialShield(unitID uint64, shieldTechno int) uint64 {
return uint64(float64(getUnitBaseShield(unitID)) * (1 + 0.1*float64(shieldTechno)))
}
func getUnitInitialHullPlating(armourTechno, metalPrice, crystalPrice int) uint64 {
return uint64((1 + (float64(armourTechno) / 10)) * (float64(metalPrice+crystalPrice) / 10))
}
func newUnit(entity *entity, unitID uint64) CombatUnit {
var unit CombatUnit
setUnitID(&unit, unitID)
unitPrice := getUnitPrice(unitID)
setUnitHull(&unit, getUnitInitialHullPlating(entity.Armour, unitPrice.Metal, unitPrice.Crystal))
setUnitShield(&unit, getUnitInitialShield(unitID, entity.Shield))
return unit
}
type entity struct {
Weapon int
Shield int
Armour int
Combustion int
Impulse int
Hyperspace int
SmallCargo int
LargeCargo int
LightFighter int
HeavyFighter int
Cruiser int
Battleship int
ColonyShip int
Recycler int
EspionageProbe int
Bomber int
SolarSatellite int
Destroyer int
Deathstar int
Battlecruiser int
RocketLauncher int
LightLaser int
HeavyLaser int
GaussCannon int
IonCannon int
PlasmaTurret int
SmallShieldDome int
LargeShieldDome int
TotalUnits int
Units []CombatUnit
Losses price
}
func (e *entity) init() {
e.reset()
idx := 0
for i := 0; i < e.SmallCargo; i++ {
e.Units[idx] = newUnit(e, smallCargoConst)
idx++
}
for i := 0; i < e.LargeCargo; i++ {
e.Units[idx] = newUnit(e, largeCargoConst)
idx++
}
for i := 0; i < e.LightFighter; i++ {
e.Units[idx] = newUnit(e, lightFighterConst)
idx++
}
for i := 0; i < e.HeavyFighter; i++ {
e.Units[idx] = newUnit(e, heavyFighterConst)
idx++
}
for i := 0; i < e.Cruiser; i++ {
e.Units[idx] = newUnit(e, cruiserConst)
idx++
}
for i := 0; i < e.Battleship; i++ {
e.Units[idx] = newUnit(e, battleshipConst)
idx++
}
for i := 0; i < e.ColonyShip; i++ {
e.Units[idx] = newUnit(e, colonyShipConst)
idx++
}
for i := 0; i < e.Recycler; i++ {
e.Units[idx] = newUnit(e, recyclerConst)
idx++
}
for i := 0; i < e.EspionageProbe; i++ {
e.Units[idx] = newUnit(e, espionageProbeConst)
idx++
}
for i := 0; i < e.Bomber; i++ {
e.Units[idx] = newUnit(e, bomberConst)
idx++
}
for i := 0; i < e.SolarSatellite; i++ {
e.Units[idx] = newUnit(e, solarSatelliteConst)
idx++
}
for i := 0; i < e.Destroyer; i++ {
e.Units[idx] = newUnit(e, destroyerConst)
idx++
}
for i := 0; i < e.Deathstar; i++ {
e.Units[idx] = newUnit(e, deathstarConst)
idx++
}
for i := 0; i < e.Battlecruiser; i++ {
e.Units[idx] = newUnit(e, battlecruiserConst)
idx++
}
for i := 0; i < e.RocketLauncher; i++ {
e.Units[idx] = newUnit(e, rocketLauncherConst)
idx++
}
for i := 0; i < e.LightLaser; i++ {
e.Units[idx] = newUnit(e, lightLaserConst)
idx++
}
for i := 0; i < e.HeavyLaser; i++ {
e.Units[idx] = newUnit(e, heavyLaserConst)
idx++
}
for i := 0; i < e.GaussCannon; i++ {
e.Units[idx] = newUnit(e, gaussCannonConst)
idx++
}
for i := 0; i < e.IonCannon; i++ {
e.Units[idx] = newUnit(e, ionCannonConst)
idx++
}
for i := 0; i < e.PlasmaTurret; i++ {
e.Units[idx] = newUnit(e, plasmaTurretConst)
idx++
}
for i := 0; i < e.SmallShieldDome; i++ {
e.Units[idx] = newUnit(e, smallShieldDomeConst)
idx++
}
for i := 0; i < e.LargeShieldDome; i++ {
e.Units[idx] = newUnit(e, largeShieldDomeConst)
idx++
}
}
func newEntity() *entity {
return new(entity)
}
type combatSimulator struct {
Attacker entity
Defender entity
MaxRounds int
Rounds int
FleetToDebris float64
Winner string
IsLogging bool
Logs string
Debris price
}
func (simulator *combatSimulator) hasExploded(entity *entity, defendingUnit *CombatUnit) bool {
exploded := false
unitPrice := getUnitPrice(getUnitID(defendingUnit))
hullPercentage := float64(getUnitHull(defendingUnit)) / float64(getUnitInitialHullPlating(entity.Armour, unitPrice.Metal, unitPrice.Crystal))
if hullPercentage <= 0.7 {
probabilityOfExploding := 1.0 - hullPercentage
dice := rand.Float64()
msg := ""
if simulator.IsLogging {
msg += fmt.Sprintf("probability of exploding of %1.3f%%: dice value of %1.3f comparing with %1.3f: ", probabilityOfExploding*100, dice, 1-probabilityOfExploding)
}
if dice >= 1-probabilityOfExploding {
exploded = true
if simulator.IsLogging {
msg += "unit exploded."
}
} else {
if simulator.IsLogging {
msg += "unit didn't explode."
}
}
if simulator.IsLogging {
simulator.Logs += msg + "\n"
}
}
return exploded
}
func (simulator *combatSimulator) getAnotherShot(unit, targetUnit *CombatUnit) bool {
rapidFire := true
rf := getRapidFireAgainst(unit, targetUnit)
msg := ""
if rf > 0 {
chance := float64(rf-1) / float64(rf)
dice := rand.Float64()
if simulator.IsLogging {
msg += fmt.Sprintf("dice was %1.3f, comparing with %1.3f: ", dice, chance)
}
if dice <= chance {
if simulator.IsLogging {
msg += fmt.Sprintf("%s gets another shot.", getUnitName(getUnitID(unit)))
}
} else {
if simulator.IsLogging {
msg += fmt.Sprintf("%s does not get another shot.", getUnitName(getUnitID(unit)))
}
rapidFire = false
}
} else {
if simulator.IsLogging {
msg += fmt.Sprintf("%s doesn't have rapid fire against %s.", getUnitName(getUnitID(unit)), getUnitName(getUnitID(targetUnit)))
}
rapidFire = false
}
if simulator.IsLogging {
simulator.Logs += msg + "\n"
}
return rapidFire
}
func (simulator *combatSimulator) attack(attacker *entity, attackingUnit *CombatUnit, defender *entity, defendingUnit *CombatUnit) {
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("%s fires at %s; ", getUnitName(getUnitID(attackingUnit)), getUnitName(getUnitID(defendingUnit)))
}
weapon := getUnitWeaponPower(getUnitID(attackingUnit), attacker.Weapon)
// Check for shot bounce
if float64(weapon) < 0.01*float64(getUnitShield(defendingUnit)) {
if simulator.IsLogging {
simulator.Logs += "shot bounced\n"
}
return
}
// Attack target
currentHull := getUnitHull(defendingUnit)
currentShield := getUnitShield(defendingUnit)
if currentShield < weapon {
weapon -= currentShield
setUnitShield(defendingUnit, 0)
if (int64)(currentHull-weapon) < 0 {
setUnitHull(defendingUnit, 0)
} else {
setUnitHull(defendingUnit, currentHull-weapon)
}
} else {
setUnitShield(defendingUnit, currentShield-weapon)
}
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("result is %s %d %d\n", getUnitName(getUnitID(defendingUnit)), getUnitHull(defendingUnit), getUnitShield(defendingUnit))
}
// Check for explosion
if isAlive(defendingUnit) {
if simulator.hasExploded(defender, defendingUnit) {
setUnitHull(defendingUnit, 0)
}
}
}
func (simulator *combatSimulator) unitsFires(attacker, defender *entity) {
rand.Seed(time.Now().UnixNano())
for i := 0; i < attacker.TotalUnits; i++ {
unit := attacker.Units[i]
rapidFire := true
for rapidFire {
if defender.TotalUnits == 0 {
break
}
targetUnit := &defender.Units[rand.Intn(defender.TotalUnits)]
rapidFire = simulator.getAnotherShot(&unit, targetUnit)
if isAlive(targetUnit) {
simulator.attack(attacker, &unit, defender, targetUnit)
}
}
}
}
func (simulator *combatSimulator) attackerFires() {
if simulator.Defender.TotalUnits <= 0 {
return
}
simulator.unitsFires(&simulator.Attacker, &simulator.Defender)
}
func (simulator *combatSimulator) defenderFires() {
simulator.unitsFires(&simulator.Defender, &simulator.Attacker)
}
func isShip(unit *CombatUnit) bool {
switch getUnitID(unit) {
case smallCargoConst:
return true
case largeCargoConst:
return true
case lightFighterConst:
return true
case heavyFighterConst:
return true
case cruiserConst:
return true
case battleshipConst:
return true
case colonyShipConst:
return true
case recyclerConst:
return true
case espionageProbeConst:
return true
case bomberConst:
return true
case solarSatelliteConst:
return true
case destroyerConst:
return true
case deathstarConst:
return true
case battlecruiserConst:
return true
}
return false
}
func (simulator *combatSimulator) removeDestroyedUnits() {
l := simulator.Defender.TotalUnits
for i := l - 1; i >= 0; i-- {
unit := &simulator.Defender.Units[i]
if getUnitHull(unit) == 0 {
unitPrice := getUnitPrice(getUnitID(unit))
if isShip(unit) {
simulator.Debris.Metal += int(simulator.FleetToDebris * float64(unitPrice.Metal))
simulator.Debris.Crystal += int(simulator.FleetToDebris * float64(unitPrice.Crystal))
}
simulator.Defender.Losses.add(unitPrice)
simulator.Defender.Units[i] = simulator.Defender.Units[simulator.Defender.TotalUnits-1]
simulator.Defender.TotalUnits--
//simulator.Defender.Units = simulator.Defender.Units[:len(simulator.Defender.Units)-1]
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("%s lost all its integrity, remove from battle\n", getUnitName(getUnitID(unit)))
}
}
}
l = simulator.Attacker.TotalUnits
for i := l - 1; i >= 0; i-- {
unit := &simulator.Attacker.Units[i]
if getUnitHull(unit) == 0 {
unitPrice := getUnitPrice(getUnitID(unit))
if isShip(unit) {
simulator.Debris.Metal += int(simulator.FleetToDebris * float64(unitPrice.Metal))
simulator.Debris.Crystal += int(simulator.FleetToDebris * float64(unitPrice.Crystal))
}
simulator.Attacker.Losses.add(unitPrice)
simulator.Attacker.Units[i] = simulator.Attacker.Units[simulator.Attacker.TotalUnits-1]
simulator.Attacker.TotalUnits--
//simulator.Attacker.Units = simulator.Attacker.Units[:len(simulator.Attacker.Units)-1]
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("%s lost all its integrity, remove from battle\n", getUnitName(getUnitID(unit)))
}
}
}
}
func (simulator *combatSimulator) restoreShields() {
for i := 0; i < simulator.Attacker.TotalUnits; i++ {
unit := &simulator.Attacker.Units[i]
setUnitShield(unit, getUnitInitialShield(getUnitID(unit), simulator.Attacker.Shield))
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("%s still has integrity, restore its shield\n", getUnitName(getUnitID(unit)))
}
}
for i := 0; i < simulator.Defender.TotalUnits; i++ {
unit := &simulator.Defender.Units[i]
setUnitShield(unit, getUnitInitialShield(getUnitID(unit), simulator.Defender.Shield))
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("%s still has integrity, restore its shield\n", getUnitName(getUnitID(unit)))
}
}
}
func (simulator *combatSimulator) isCombatDone() bool {
return simulator.Attacker.TotalUnits <= 0 || simulator.Defender.TotalUnits <= 0
}
func (simulator *combatSimulator) getMoonchance() int {
debris := float64(simulator.Debris.Metal) + float64(simulator.Debris.Crystal)
return int(math.Min(debris/100000.0, 20.0))
}
func (simulator *combatSimulator) printWinner() {
if simulator.Defender.TotalUnits <= 0 && simulator.Attacker.TotalUnits <= 0 {
simulator.Winner = "draw"
if simulator.IsLogging {
simulator.Logs += "The battle ended draw.\n"
}
} else if simulator.Attacker.TotalUnits <= 0 {
simulator.Winner = "defender"
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("The battle ended after %d rounds with %s winning\n", simulator.Rounds, simulator.Winner)
}
} else if simulator.Defender.TotalUnits <= 0 {
simulator.Winner = "attacker"
if simulator.IsLogging {
simulator.Logs += fmt.Sprintf("The battle ended after %d rounds with %s winning\n", simulator.Rounds, simulator.Winner)
}
} else {
simulator.Winner = "draw"
if simulator.IsLogging {
simulator.Logs += "The battle ended draw.\n"
}
}
}
func (simulator *combatSimulator) Simulate() {
simulator.Attacker.init()
simulator.Defender.init()
for currentRound := 1; currentRound <= simulator.MaxRounds; currentRound++ {
simulator.Rounds = currentRound
if simulator.IsLogging {
simulator.Logs += strings.Repeat("-", 80) + "\n"
simulator.Logs += "ROUND " + strconv.Itoa(currentRound) + "\n"
simulator.Logs += strings.Repeat("-", 80) + "\n"
}
simulator.attackerFires()
simulator.defenderFires()
simulator.removeDestroyedUnits()
simulator.restoreShields()
if simulator.isCombatDone() {
break
}
}
simulator.printWinner()
}
func newCombatSimulator(attacker *entity, defender *entity) *combatSimulator {
cs := new(combatSimulator)
cs.Attacker = *attacker
cs.Defender = *defender
cs.IsLogging = false
cs.MaxRounds = 6
return cs
}
// Config ...
type Config struct {
IsLogging bool
Simulations int
Workers int
Attacker attackerInfo
Defender defenderInfo
}
type attackerInfo struct {
Weapon int
Shield int
Armour int
SmallCargo int
LargeCargo int
LightFighter int
HeavyFighter int
Cruiser int
Battleship int
ColonyShip int
Recycler int
EspionageProbe int
Bomber int
Destroyer int
Deathstar int
Battlecruiser int
}
type defenderInfo struct {
Weapon int
Shield int
Armour int
SmallCargo int
LargeCargo int
LightFighter int
HeavyFighter int
Cruiser int
Battleship int
ColonyShip int
Recycler int
EspionageProbe int
Bomber int
SolarSatellite int
Destroyer int
Deathstar int
Battlecruiser int
RocketLauncher int
LightLaser int
HeavyLaser int
GaussCannon int
IonCannon int
PlasmaTurret int
SmallShieldDome int
LargeShieldDome int
}
func printResult(result SimulatorResult) {
fmt.Println(fmt.Sprintf("| Results (%d simulations | ~%d rounds)", result.Simulations, result.Rounds))
table := tablewriter.NewWriter(os.Stdout)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
data2 := [][]string{
{"Attackers win", fmt.Sprintf("%d%%", result.AttackerWin)},
{"Defenders win", fmt.Sprintf("%d%%", result.DefenderWin)},
{"Draw", fmt.Sprintf("%d%%", result.Draw)},
}
table.AppendBulk(data2)
table.Render()
fmt.Println("")
table = tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"", "Metal", "Crystal", "Deuterium", "Recycler", "Moonchance"})
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
data1 := [][]string{
{
"Attacker losses",
fmt.Sprintf("%d", result.AttackerLosses.Metal),
fmt.Sprintf("%d", result.AttackerLosses.Crystal),
fmt.Sprintf("%d", result.AttackerLosses.Deuterium),
"", ""},
{
"Defender losses",
fmt.Sprintf("%d", result.DefenderLosses.Metal),
fmt.Sprintf("%d", result.DefenderLosses.Crystal),
fmt.Sprintf("%d", result.DefenderLosses.Deuterium),
"", ""},
{
"Debris",
fmt.Sprintf("%d", result.Debris.Metal),
fmt.Sprintf("%d", result.Debris.Crystal),
"",
fmt.Sprintf("%d", result.Recycler),
fmt.Sprintf("%d", result.Moonchance)},
}
table.AppendBulk(data1)
table.Render()
}
func (e *entity) reset() {
e.Losses = price{Metal: 0, Crystal: 0, Deuterium: 0}
e.TotalUnits = 0
e.TotalUnits += e.SmallCargo
e.TotalUnits += e.SmallCargo
e.TotalUnits += e.LargeCargo
e.TotalUnits += e.LightFighter