-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulator.es
1423 lines (1343 loc) · 49.8 KB
/
simulator.es
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
export class Stage {
constructor(opts) {
this.type = opts.type // StageType
this.subtype = opts.subtype
this.attacks = opts.attacks // [Attack, ...]
this.aerial = opts.aerial // AerialInfo
this.engagement = opts.engagement // EngagementInfo
this.kouku = opts.kouku // Raw API data `api_kouku` (Deprecated)
}
}
export const StageType = {
// Primary Type
Aerial : "Aerial", // Aerial Combat
Torpedo : "Torpedo", // Torpedo Salvo
Shelling : "Shelling", // Shelling
Support : "Support", // Support Fleet (Expedition)
LandBase : "LandBase", // Land Base Aerial Support
Engagement: "Engagement", // [SP] Engagement Information
// Sub Type
Main : "Main", // Shelling, main fleet
Escort : "Escort", // Shelling, escort fleet
Night : "Night", // Shelling, night combat
Opening: "Opening", // Torpedo, opening torpedo salvo
// Shelling, opening anti-sub
Assault: "Assault", // Aerial & LandBase, Jet Air Assault
}
export class Attack {
constructor(opts) {
this.type = opts.type // AttackType
this.fromShip = opts.fromShip // Ship
this.toShip = opts.toShip // Ship
this.damage = opts.damage // [int, ...]
this.hit = opts.hit // [HitType, ...]
this.fromHP = opts.fromHP // HP before attack.
this.toHP = opts.toHP // HP after attack.
this.useItem = opts.useItem // int, $slotitem[] OR null
}
}
export const AttackType = {
Normal: "Normal", // 通常攻撃
Laser : "Laser", // レーザー攻撃
Double: "Double", // 連撃
Nelson_Touch: "Nelson", // ネルソンタッチ
Nagato_Punch: "Nagato", // 一斉射かッ…胸が熱いな!
Mutsu_Splash: "Mutsu", // 長門、いい? いくわよ! 主砲一斉射ッ!
Colorado_Fire: "Colorado", // Colorado戦隊、全力斉射!各個目標に砲撃開始!一気に殲滅する、Fire!!
Kongo_Class_Kaini_C_Charge: "Kongo_Class_Kaini_C",
Yamato_Attack_Double: "Yamato_Double",
Yamato_Attack_Triple: "Yamato_Triple",
Submarine_Special_Attack_2_3: "Submarine_Special_Attack_2_3",
Submarine_Special_Attack_3_4: "Submarine_Special_Attack_3_4",
Submarine_Special_Attack_2_4: "Submarine_Special_Attack_2_4",
Zuiun_Night_Attack: "Zuiyun_Night_Attack",
Type_4_LC_Special_Attack: "Type_4_LC_Special_Attack",
Carrier_CI: "CVCI", // 空母カットイン
Primary_Secondary_CI: "PSCI", // カットイン(主砲/副砲)
Primary_Radar_CI : "PRCI", // カットイン(主砲/電探)
Primary_AP_CI : "PACI", // カットイン(主砲/徹甲)
Primary_Primary_CI : "PrCI", // カットイン(主砲/主砲)
Primary_Torpedo_CI : "PTCI", // カットイン(主砲/魚雷)
Torpedo_Torpedo_CI : "TTCI", // カットイン(魚雷/魚雷)
}
export const MultiTargetAttackType = new Set([
AttackType.Nelson_Touch,
AttackType.Nagato_Punch,
AttackType.Mutsu_Splash,
AttackType.Colorado_Fire,
AttackType.Kongo_Class_Kaini_C_Charge,
AttackType.Yamato_Attack_Double,
AttackType.Yamato_Attack_Triple,
AttackType.Submarine_Special_Attack_2_3,
AttackType.Submarine_Special_Attack_3_4,
AttackType.Submarine_Special_Attack_2_4,
AttackType.Zuiun_Night_Attack,
AttackType.Laser,
AttackType.Type_4_LC_Special_Attack,
])
export const MultiTargetAttackOrder = {
[AttackType.Nelson_Touch]: [0, 2, 4],
[AttackType.Nagato_Punch]: [0, 0, 1],
[AttackType.Mutsu_Splash]: [0, 0, 1],
[AttackType.Colorado_Fire]: [0, 1, 2],
[AttackType.Kongo_Class_Kaini_C_Charge]: [0, 1],
[AttackType.Yamato_Attack_Double]: [0, 0, 1],
[AttackType.Yamato_Attack_Triple]: [0, 1, 2],
[AttackType.Submarine_Special_Attack_2_3]: [1, 1, 2, 2],
[AttackType.Submarine_Special_Attack_3_4]: [2, 2, 3, 3],
[AttackType.Submarine_Special_Attack_2_4]: [1, 1, 3, 3],
[AttackType.Zuiun_Night_Attack]: [0, 0],
[AttackType.Laser]: [0, 0, 0],
[AttackType.Type_4_LC_Special_Attack]: [0, 0, 0, 0, 0, 0],
}
export const HitType = {
Miss : 0,
Hit : 1,
Critical: 2,
}
export class Ship {
constructor(opts) {
this.id = opts.id // int, $ships
this.owner = opts.owner // ShipOwner
this.pos = opts.pos // int, Position in fleet
this.maxHP = opts.maxHP
this.nowHP = opts.nowHP
this.initHP = opts.nowHP
this.lostHP = opts.lostHP || 0
this.damage = opts.damage || 0 // Damage from this to others
this.items = opts.items
this.useItem = opts.useItem || null
this.baseParam = opts.baseParam
this.finalParam = opts.finalParam
// parameter = [FRP, TORP, AA, AMOR]
// if !usePoiAPI, baseParam and finalParam are undefined
this.raw = opts.raw
}
}
export const ShipOwner = {
Ours : "Ours",
Enemy: "Enemy",
Friend: "Friend",
}
export class AerialInfo {
constructor(opts) {
this.fPlaneInit = opts.fPlaneInit // Init plane count of entire aerial combat (Ours)
this.fPlaneNow = opts.fPlaneNow // Now plane count of entire aerial combat (Ours)
this.ePlaneInit = opts.ePlaneInit // ^^ (Enemy)
this.ePlaneNow = opts.ePlaneNow // ^^ (Enemy)
// Stage 1
this.control = opts.control // AirControl
this.fContact = opts.fContact // api_kouku.api_stage1.api_touch_plane[0]
this.eContact = opts.eContact // api_kouku.api_stage1.api_touch_plane[1]
this.fPlaneInit1 = opts.fPlaneInit1 // Init plane count of stage 1
this.fPlaneNow1 = opts.fPlaneNow1 // Now plane count of stage 1
this.ePlaneInit1 = opts.ePlaneInit1 // ^^
this.ePlaneNow1 = opts.ePlaneNow1 // ^^
// Stage 2
this.aaciKind = opts.aaciKind // api_kouku.api_stage2.api_air_fire.api_kind
this.aaciShip = opts.aaciShip // api_kouku.api_stage2.api_air_fire.api_idx => Ship
this.aaciItems = opts.aaciItems // api_kouku.api_stage2.api_air_fire.api_use_items
this.fPlaneInit2 = opts.fPlaneInit2 // Init plane count of stage 2
this.fPlaneNow2 = opts.fPlaneNow2 // Now plane count of stage 2
this.ePlaneInit2 = opts.ePlaneInit2 // ^^
this.ePlaneNow2 = opts.ePlaneNow2 // ^^
}
}
export const AirControl = {
Supremacy : "Air Supremacy",
Superiority : "Air Superiority",
Parity : "Air Parity",
Denial : "Air Denial",
Incapability: "Air Incapability",
}
export class EngagementInfo {
constructor(opts) {
this.engagement = opts.engagement // api_formation[2] => Engagement
this.fFormation = opts.fFormation // api_formation[0] => Formation
this.eFormation = opts.eFormation // api_formation[1] => Formation
this.fDetection = opts.fDetection // api_search[0] => Detection
this.eDetection = opts.eDetection // api_search[1] => Detection
// Night Combat
this.fContact = opts.fContact // api_touch_plane[0]
this.eContact = opts.eContact // api_touch_plane[1]
this.fFlare = opts.fFlare // api_flare_pos[0] => Ship
this.eFlare = opts.eFlare // api_flare_pos[1] => Ship
// Weaken Mechanism (Event Map)
this.weakened = this.weakened // api_boss_damaged, api_xal01, ...
this.smokeType = this.smokeType // api_smoke_type
}
}
export const Engagement = {
Parallel : "Parallel Engagement",
Headon : "Head-on Engagement",
TAdvantage : "Crossing the T (Advantage)",
TDisadvantage: "Crossing the T (Disadvantage)",
}
export const Formation = {
Ahead : "Line Ahead",
Double : "Double Line",
Diamond: "Diamond",
Echelon: "Echelon",
Abreast: "Line Abreast",
Vanguard: "Vanguard",
CruisingAntiSub: "Cruising Formation 1 (anti-sub)",
CruisingForward: "Cruising Formation 2 (forward)",
CruisingDiamond: "Cruising Formation 3 (diamond)",
CruisingBattle : "Cruising Formation 4 (battle)",
}
export const Detection = {
Success : "Success",
Failure : "Failure",
SuccessNR: "Success (not return)",
FailureNR: "Failure (not return)",
SuccessNP: "Success (without plane)",
FailureNP: "Failure (without plane)",
}
export class Result {
constructor(opts) {
this.rank = opts.rank // 'A', 'B'...
this.mvp = opts.mvp // [0..5, 0..5], MVP index of mainFleet & escortFleet.
this.getShip = opts.getShip // id of store.const.$
this.getItem = opts.getItem // ^^
}
}
export const Rank = {
SS: 'SS',
S : 'S',
A : 'A',
B : 'B',
C : 'C',
D : 'D',
E : 'E',
}
/**
* NOTICE
* Constants below are considered non-public API.
**/
// api_hougeki.api_at_type[] => ~
export const DayAttackTypeMap = {
0: AttackType.Normal,
1: AttackType.Laser,
2: AttackType.Double,
3: AttackType.Primary_Secondary_CI,
4: AttackType.Primary_Radar_CI,
5: AttackType.Primary_AP_CI,
6: AttackType.Primary_Primary_CI,
7: AttackType.Carrier_CI,
100: AttackType.Nelson_Touch,
101: AttackType.Nagato_Punch,
102: AttackType.Mutsu_Splash,
103: AttackType.Colorado_Fire,
300: AttackType.Submarine_Special_Attack_2_3,
301: AttackType.Submarine_Special_Attack_3_4,
302: AttackType.Submarine_Special_Attack_2_4,
400: AttackType.Yamato_Attack_Triple,
401: AttackType.Yamato_Attack_Double,
1000: AttackType.Type_4_LC_Special_Attack,
}
// api_hougeki.api_sp_list => ~
export const NightAttackTypeMap = {
0: AttackType.Normal,
1: AttackType.Double,
2: AttackType.Primary_Torpedo_CI,
3: AttackType.Torpedo_Torpedo_CI,
4: AttackType.Primary_Secondary_CI,
5: AttackType.Primary_Primary_CI,
100: AttackType.Nelson_Touch,
101: AttackType.Nagato_Punch,
102: AttackType.Mutsu_Splash,
103: AttackType.Colorado_Fire,
104: AttackType.Kongo_Class_Kaini_C_Charge,
200: AttackType.Zuiun_Night_Attack,
300: AttackType.Submarine_Special_Attack_2_3,
301: AttackType.Submarine_Special_Attack_3_4,
302: AttackType.Submarine_Special_Attack_2_4,
400: AttackType.Yamato_Attack_Triple,
401: AttackType.Yamato_Attack_Double,
1000: AttackType.Type_4_LC_Special_Attack,
}
// api_stage1.api_disp_seiku => ~
export const AirControlMap = {
0: AirControl.Parity,
1: AirControl.Supremacy,
2: AirControl.Superiority,
3: AirControl.Denial,
4: AirControl.Incapability,
}
// api_search[] => ~
export const DetectionMap = {
1: Detection.Success,
2: Detection.SuccessNR,
3: Detection.FailureNR,
4: Detection.Failure,
5: Detection.SuccessNP,
6: Detection.FailureNP,
}
// api_formation[0,1] => ~
export const FormationMap = {
1: Formation.Ahead,
2: Formation.Double,
3: Formation.Diamond,
4: Formation.Echelon,
5: Formation.Abreast,
6: Formation.Vanguard,
11: Formation.CruisingAntiSub,
12: Formation.CruisingForward,
13: Formation.CruisingDiamond,
14: Formation.CruisingBattle,
}
// api_formation[2] => ~
export const EngagementMap = {
1: Engagement.Parallel,
2: Engagement.Headon,
3: Engagement.TAdvantage,
4: Engagement.TDisadvantage,
}
// api_support_flag => ~
export const SupportTypeMap = {
1: StageType.Aerial,
2: StageType.Shelling,
3: StageType.Torpedo,
}
// api_win_rank => ~
export const BattleRankMap = {
'SS': Rank.SS,
'S': Rank.S,
'A': Rank.A,
'B': Rank.B,
'C': Rank.C,
'D': Rank.D,
'E': Rank.E,
}
const HalfSunkNumber = [ // 7~12 is guessed.
0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 7, 7, 8,
]
function useItem(ship) {
if (ship.owner === ShipOwner.Ours && ship.nowHP <= 0 && ship.items != null)
for (const itemId of ship.items) {
// 応急修理要員
if (itemId === 42) {
ship.nowHP = Math.floor(ship.maxHP / 5)
return itemId
}
// 応急修理女神
if (itemId === 43) {
ship.nowHP = ship.maxHP
return itemId
}
}
return null
}
function damageShip(fromShip, toShip, damage) {
if (fromShip != null) {
fromShip.damage += damage
}
let fromHP = toShip.nowHP
toShip.nowHP -= damage
toShip.lostHP += damage
let item = useItem(toShip)
if (item) {
toShip.useItem = item
}
let toHP = toShip.nowHP
// `toShip.*` is updated in place
return {fromHP, toHP, item}
}
function preventNullObject(o) {
// Prevent returning Object with all properties null.
let isNull = true
for (const k of Object.keys(o)) {
isNull = isNull && o[k] == null
}
return isNull ? null : o
}
function generateAerialInfo(kouku, mainFleet, escortFleet) {
if (!(kouku != null))
return
const stage1 = kouku.api_stage1
const stage2 = kouku.api_stage2
const o = new AerialInfo({})
let fPlaneLost = 0, ePlaneLost = 0
// Stage 1
if (stage1 != null) {
const contact = stage1.api_touch_plane || [-1, -1]
o.control = AirControlMap[stage1.api_disp_seiku]
o.fContact = contact[0] > 0 ? contact[0] : null
o.eContact = contact[1] > 0 ? contact[1] : null
o.fPlaneInit1 = stage1.api_f_count
o.fPlaneNow1 = stage1.api_f_count - stage1.api_f_lostcount
o.ePlaneInit1 = stage1.api_e_count
o.ePlaneNow1 = stage1.api_e_count - stage1.api_e_lostcount
fPlaneLost += stage1.api_f_lostcount
ePlaneLost += stage1.api_e_lostcount
}
// Stage 2
if (stage2 != null) {
const airfire = kouku.api_stage2.api_air_fire
if (airfire != null) {
let ship = null, idx = airfire.api_idx
const mainFleetRange = mainFleet.length
if (0 <= idx && idx <= mainFleetRange) ship = mainFleet[idx]
if (mainFleetRange <= idx) ship = escortFleet[idx - mainFleetRange]
o.aaciKind = airfire.api_kind
o.aaciShip = ship
o.aaciItems = airfire.api_use_items
}
o.fPlaneInit2 = stage2.api_f_count
o.fPlaneNow2 = stage2.api_f_count - stage2.api_f_lostcount
o.ePlaneInit2 = stage2.api_e_count
o.ePlaneNow2 = stage2.api_e_count - stage2.api_e_lostcount
fPlaneLost += stage2.api_f_lostcount
ePlaneLost += stage2.api_e_lostcount
}
// Summary
// We assume stage2 won't exist without stage1
if (stage1 != null) {
o.fPlaneInit = stage1.api_f_count
o.fPlaneNow = stage1.api_f_count - fPlaneLost
o.ePlaneInit = stage1.api_e_count
o.ePlaneNow = stage1.api_e_count - ePlaneLost
}
return preventNullObject(o)
}
function generateEngagementInfo(packet, oursFleet, emenyFleet, opts={}) {
if (!(packet != null))
return
opts = {...{engagement: false, night: false}, ...opts}
const o = new EngagementInfo({})
if (opts.engagement) {
// Battle Engagement
const {api_formation, api_search} = packet
if (api_formation != null) {
o.engagement = EngagementMap[api_formation[2]]
o.fFormation = FormationMap[api_formation[0]]
o.eFormation = FormationMap[api_formation[1]]
}
if (api_search != null) {
o.fDetection = DetectionMap[api_search[0]]
o.eDetection = DetectionMap[api_search[1]]
}
// Weaken mechanism
const {api_boss_damaged, api_xal01} = packet
o.weakened = [api_boss_damaged, api_xal01].find(x => x != null)
// Carry over smoke_type
const {api_smoke_type} = packet
// old records doesn't have this key, unifying to 0.
o.smokeType = api_smoke_type || 0
}
if (opts.night) {
// Night combat
const {api_touch_plane, api_flare_pos} = packet
if (api_touch_plane != null) {
// KANCOLLE BUG: api-touch_plane elements is string
const plane = api_touch_plane.map(x => Number(x))
o.fContact = plane[0] > 0 ? plane[0] : null
o.eContact = plane[1] > 0 ? plane[1] : null
}
if (api_flare_pos != null) {
o.fFlare = oursFleet[api_flare_pos[0]]
o.eFlare = emenyFleet[api_flare_pos[1]]
}
}
return preventNullObject(o)
}
function simulateAerialAttack(fleet, edam, ebak_flag, erai_flag, ecl_flag) {
if (!(fleet != null && edam != null)) {
return []
}
const list = []
for (let [i, damage] of edam.entries()) {
if ((damage < 0) || (ebak_flag[i] <= 0 && erai_flag[i] <= 0))
continue
damage = Math.floor(damage)
let toShip = fleet[i]
let hit = (ecl_flag[i] === 1 ? HitType.Critical : (damage > 0 ? HitType.Hit : HitType.Miss))
let {fromHP, toHP, item} = damageShip(null, toShip, damage)
list.push(new Attack({
type : AttackType.Normal,
toShip : toShip,
damage : [damage],
hit : [hit],
fromHP : fromHP,
toHP : toHP,
useItem : item,
}))
}
return list
}
function simulateAerial(mainFleet, escortFleet, enemyFleet, enemyEscort, kouku, assault=false) {
if (!(kouku != null)) {
return
}
let attacks = []
if (kouku.api_stage3 != null) {
const st3 = kouku.api_stage3
attacks = attacks.concat(simulateAerialAttack(enemyFleet, st3.api_edam, st3.api_ebak_flag, st3.api_erai_flag, st3.api_ecl_flag))
attacks = attacks.concat(simulateAerialAttack(mainFleet, st3.api_fdam, st3.api_fbak_flag, st3.api_frai_flag, st3.api_fcl_flag))
}
if (kouku.api_stage3_combined != null) {
const st3 = kouku.api_stage3_combined
attacks = attacks.concat(simulateAerialAttack(enemyEscort, st3.api_edam, st3.api_ebak_flag, st3.api_erai_flag, st3.api_ecl_flag))
attacks = attacks.concat(simulateAerialAttack(escortFleet, st3.api_fdam, st3.api_fbak_flag, st3.api_frai_flag, st3.api_fcl_flag))
}
let aerial = generateAerialInfo(kouku, mainFleet, escortFleet)
return new Stage({
type : StageType.Aerial,
subtype: assault ? StageType.Assault : null,
attacks: attacks,
aerial : aerial,
kouku : kouku,
})
}
function simulateTorpedoAttack(mainFleet, escortFleet, enemyFleet, enemyEscort, eydam, erai, ecl) {
if (!(enemyFleet != null && eydam != null)) {
return []
}
const list = []
for (let [i, t] of erai.entries()) {
let fromShip, toShip
const mainFleetRange = mainFleet.length
const enemyFleetRange = enemyFleet.length
if (i < 0 || t < 0) continue
if (i < mainFleetRange) fromShip = mainFleet[i]
else fromShip = escortFleet[i - mainFleetRange]
if (t < enemyFleetRange) toShip = enemyFleet[t]
else toShip = enemyEscort[t - enemyFleetRange]
let damage = Math.floor(eydam[i])
let hit = (ecl[i] === 2 ? HitType.Critical : (ecl[i] === 1 ? HitType.Hit : HitType.Miss))
let {fromHP, toHP, item} = damageShip(fromShip, toShip, damage)
list.push(new Attack({
type : AttackType.Normal,
fromShip: fromShip,
toShip : toShip,
damage : [damage],
hit : [hit],
fromHP : fromHP,
toHP : toHP,
useItem : item,
}))
}
return list
}
function simulateOpeningTorpedoAttack(mainFleet, escortFleet, enemyFleet, enemyEscort, eydamList, eraiList, eclList) {
if (!(enemyFleet != null && eydamList != null)) {
return []
}
const list = []
for (let [i, tList] of eraiList.entries()) {
let fromShip, toShip
const mainFleetRange = mainFleet.length
const enemyFleetRange = enemyFleet.length
if (i < 0 || tList == null) continue
if (i < mainFleetRange) fromShip = mainFleet[i]
else fromShip = escortFleet[i - mainFleetRange]
for (let [j, t] of tList.entries()) {
if (t < enemyFleetRange) toShip = enemyFleet[t]
else toShip = enemyEscort[t - enemyFleetRange]
let damage = Math.floor(eydamList[i][j])
let hit = (eclList[i][j] === 2 ? HitType.Critical : (eclList[i][j] === 1 ? HitType.Hit : HitType.Miss))
let {fromHP, toHP, item} = damageShip(fromShip, toShip, damage)
list.push(new Attack({
type : AttackType.Normal,
fromShip: fromShip,
toShip : toShip,
damage : [damage],
hit : [hit],
fromHP : fromHP,
toHP : toHP,
useItem : item,
}))
}
}
return list
}
function simulateTorpedo(mainFleet, escortFleet, enemyFleet, enemyEscort, raigeki, subtype) {
if (!(raigeki != null)) {
return
}
let attacks = []
if (raigeki.api_frai != null)
attacks = attacks.concat(simulateTorpedoAttack(mainFleet, escortFleet, enemyFleet, enemyEscort,
raigeki.api_fydam, raigeki.api_frai, raigeki.api_fcl))
if (raigeki.api_erai != null)
attacks = attacks.concat(simulateTorpedoAttack(enemyFleet, enemyEscort, mainFleet, escortFleet,
raigeki.api_eydam, raigeki.api_erai, raigeki.api_ecl))
return new Stage({
type: StageType.Torpedo,
attacks: attacks,
subtype: subtype,
})
}
function simulateOpeningTorpedo(mainFleet, escortFleet, enemyFleet, enemyEscort, raigeki, subtype) {
if (!(raigeki != null)) {
return
}
if (raigeki.api_frai != null) {
// Backward compatibility
return simulateTorpedo(mainFleet, escortFleet, enemyFleet, enemyEscort, raigeki, subtype)
}
let attacks = []
if (raigeki.api_frai_list_items != null)
attacks = attacks.concat(simulateOpeningTorpedoAttack(mainFleet, escortFleet, enemyFleet, enemyEscort,
raigeki.api_fydam_list_items, raigeki.api_frai_list_items, raigeki.api_fcl_list_items))
if (raigeki.api_erai_list_items != null)
attacks = attacks.concat(simulateOpeningTorpedoAttack(enemyFleet, enemyEscort, mainFleet, escortFleet,
raigeki.api_eydam_list_items, raigeki.api_erai_list_items, raigeki.api_ecl_list_items))
return new Stage({
type: StageType.Torpedo,
attacks: attacks,
subtype: subtype,
})
}
function simulateShelling(mainFleet, escortFleet, enemyFleet, enemyEscort, hougeki, subtype) {
if (!(hougeki != null)) {
return
}
const isNight = (subtype == StageType.Night)
const list = []
const mainFleetRange = mainFleet.length
const enemyFleetRange = enemyFleet.length
for (let [i, attacker] of (hougeki.api_at_list || []).entries()) {
if (attacker === -1) continue
let attackType = isNight ? NightAttackTypeMap[hougeki.api_sp_list[i]] : DayAttackTypeMap[hougeki.api_at_type[i]]
let at = attacker
if (!MultiTargetAttackType.has(attackType)) {
let df, fromEnemy // Declare ahead
df = hougeki.api_df_list[i][0] // Defender
if (hougeki.api_at_eflag != null) {
fromEnemy = hougeki.api_at_eflag[i] === 1
} else {
fromEnemy = df < mainFleetRange
if (at >= mainFleetRange) at -= mainFleetRange
if (df >= mainFleetRange) df -= mainFleetRange
}
let fromShip, toShip
if (fromEnemy) {
fromShip = at < enemyFleetRange ? enemyFleet[at] : enemyEscort[at - enemyFleetRange]
toShip = df < mainFleetRange ? mainFleet[df] : escortFleet[df - mainFleetRange]
} else {
fromShip = at < mainFleetRange ? mainFleet[at] : escortFleet[at - mainFleetRange]
toShip = df < enemyFleetRange ? enemyFleet[df] : enemyEscort[df - enemyFleetRange]
}
let damage = []
let damageTotal = 0
for (let dmg of hougeki.api_damage[i]) {
if (dmg < 0) dmg = 0
dmg = Math.floor(dmg)
damage.push(dmg)
damageTotal += dmg
}
let hit = []
for (const cl of hougeki.api_cl_list[i])
hit.push(cl === 2 ? HitType.Critical : (cl === 1 ? HitType.Hit : HitType.Miss))
let {fromHP, toHP, item} = damageShip(fromShip, toShip, damageTotal)
list.push(new Attack({
type : attackType,
fromShip: fromShip,
toShip : toShip,
damage : damage,
hit : hit,
fromHP : fromHP,
toHP : toHP,
useItem : item,
}))
} else {
const order = MultiTargetAttackOrder[attackType] || []
for (let j = 0; j < hougeki.api_df_list[i].length; j++) {
let df, fromEnemy // Declare ahead
df = hougeki.api_df_list[i][j] // Defender
let at = attacker + (order[j] || 0) // Attacker
// Tanaka bug: in combined night battle the sp attack could have wrong attacker index
if (isNight && escortFleet && escortFleet.length && at < mainFleetRange) {
at += mainFleetRange
}
if (hougeki.api_at_eflag != null) {
fromEnemy = hougeki.api_at_eflag[i] === 1
} else {
fromEnemy = df < mainFleetRange
if (at >= mainFleetRange) at -= mainFleetRange
if (df >= mainFleetRange) df -= mainFleetRange
}
let fromShip, toShip
if (fromEnemy) {
fromShip = at < enemyFleetRange ? enemyFleet[at] : enemyEscort[at - enemyFleetRange]
toShip = df < mainFleetRange ? mainFleet[df] : escortFleet[df - mainFleetRange]
} else {
fromShip = at < mainFleetRange ? mainFleet[at] : escortFleet[at - mainFleetRange]
toShip = df < enemyFleetRange ? enemyFleet[df] : enemyEscort[df - enemyFleetRange]
}
let damage = []
let dmg = hougeki.api_damage[i][j]
if (dmg < 0) dmg = 0
dmg = Math.floor(dmg)
damage.push(dmg)
let hit = []
let cl = hougeki.api_cl_list[i][j]
hit.push(cl === 2 ? HitType.Critical : (cl === 1 ? HitType.Hit : HitType.Miss))
let {fromHP, toHP, item} = damageShip(fromShip, toShip, dmg)
list.push(new Attack({
type : attackType,
fromShip: fromShip,
toShip : toShip,
damage : damage,
hit : hit,
fromHP : fromHP,
toHP : toHP,
useItem : item,
}))
}
}
}
return new Stage({
type: StageType.Shelling,
attacks: list,
subtype: subtype,
})
}
function simulateNight(fleetType, mainFleet, escortFleet, enemyType, enemyFleet, enemyEscort, hougeki, packet) {
const stage = simulateShelling(mainFleet, escortFleet, enemyFleet, enemyEscort, hougeki, StageType.Night)
if (stage) {
let _oursFleet = fleetType === 0 ? mainFleet : escortFleet
let _enemyFleet = enemyType === 0 ? enemyFleet : enemyEscort
if (packet.api_active_deck != null) {
if (packet.api_active_deck[0] === 1) {
_oursFleet = mainFleet
}
if (packet.api_active_deck[0] === 2) {
_oursFleet = escortFleet
}
if (packet.api_active_deck[1] === 1) {
_enemyFleet = enemyFleet
}
if (packet.api_active_deck[1] === 2) {
_enemyFleet = enemyEscort
}
}
stage.engagement = generateEngagementInfo(packet, _oursFleet, _enemyFleet, {night: true})
}
return stage
}
function simulateSupport(enemyFleet, enemyEscort, support, flag) {
if (!(support != null && flag != null)) {
return
}
if (flag === 1 || flag === 4) {
const kouku = support.api_support_airatack
const st3 = kouku.api_stage3
let fleet = [].concat(enemyFleet, enemyEscort || [])
let attacks = simulateAerialAttack(fleet, st3.api_edam, st3.api_ebak_flag, st3.api_erai_flag, st3.api_ecl_flag)
let aerial = generateAerialInfo(kouku, null, null)
return new Stage({
type : StageType.Support,
subtype: SupportTypeMap[flag],
attacks: attacks,
aerial : aerial,
kouku : kouku,
})
}
if (flag === 2 || flag === 3) {
const hourai = support.api_support_hourai
let attacks = []
const enemyFleetRange = enemyFleet.length
for (let [i, damage] of hourai.api_damage.entries()) {
let toShip
if (0 <= i && i < enemyFleetRange)
toShip = enemyFleet[i]
if (enemyFleetRange <= i && enemyEscort) // TANAKA: api_damage.length = 7 with 6 ships
toShip = enemyEscort[i - enemyFleetRange]
if (toShip == null)
continue
damage = Math.floor(damage)
let cl = hourai.api_cl_list[i]
let hit = (cl === 2 ? HitType.Critical : (cl === 1 ? HitType.Hit : HitType.Miss))
// No showing Miss attack on support stage.
if (hit === HitType.Miss)
continue
let {fromHP, toHP, item} = damageShip(null, toShip, damage)
attacks.push(new Attack({
type : AttackType.Normal,
toShip : toShip,
damage : [damage],
hit : [hit],
fromHP : fromHP,
toHP : toHP,
useItem: item,
}))
}
return new Stage({
type: StageType.Support,
subtype: SupportTypeMap[flag],
attacks: attacks,
})
}
}
function simulateLandBase(enemyFleet, enemyEscort, kouku, assault=false) {
let stage = simulateAerial(null, null, enemyFleet, enemyEscort, kouku)
if (stage != null) {
stage.type = StageType.LandBase
stage.subtype = assault ? StageType.Assault : null
}
return stage
}
function simulateBattleRank(mainFleet, escortFleet, enemyFleet, enemyEscort) {
// https://github.com/andanteyk/ElectronicObserver/blob/master/ElectronicObserver/Other/Information/kcmemo.md#%E6%88%A6%E9%97%98%E5%8B%9D%E5%88%A9%E5%88%A4%E5%AE%9A
function calStatus(fleet) {
let shipNum = 0, sunkNum = 0, totalHP = 0, lostHP = 0
let flagshipSunk = false, flagshipCritical = false
for (const ship of fleet) {
if (ship == null) continue
let {initHP, nowHP, maxHP} = ship
if (nowHP < 0) nowHP = 0
shipNum += 1
sunkNum += (nowHP <= 0) ? 1 : 0
totalHP += initHP
lostHP += (initHP - nowHP)
if (ship.pos === 0) {
flagshipSunk = (nowHP <= 0)
flagshipCritical = (nowHP * 4 <= maxHP)
}
}
return {
num: shipNum,
sunk: sunkNum,
rate: Math.floor(lostHP / totalHP * 100),
lostHP, flagshipSunk, flagshipCritical,
}
}
const ours = calStatus([].concat(mainFleet, escortFleet))
const enemy = calStatus([].concat(enemyFleet, enemyEscort))
if (ours.sunk === 0) {
if (enemy.sunk === enemy.num) {
if (ours.lostHP <= 0)
return Rank.SS
else
return Rank.S
}
if (enemy.num > 1 && enemy.sunk >= HalfSunkNumber[enemy.num]) {
return Rank.A
}
}
if (enemy.flagshipSunk && ours.sunk < enemy.sunk) {
return Rank.B
}
if (ours.num === 1 && ours.flagshipCritical) {
return Rank.D
}
if (2 * enemy.rate > 5 * ours.rate) {
return Rank.B
}
if (10 * enemy.rate > 9 * ours.rate) {
return Rank.C
}
if (ours.sunk > 0 && (ours.num - ours.sunk) === 1) {
return Rank.E
}
return Rank.D
}
function simulateAirRaidBattleRank(mainFleet, escortFleet) {
// https://github.com/andanteyk/ElectronicObserver/blob/master/ElectronicObserver/Other/Information/kcmemo.md#%E9%95%B7%E8%B7%9D%E9%9B%A2%E7%A9%BA%E8%A5%B2%E6%88%A6%E3%81%A7%E3%81%AE%E5%8B%9D%E5%88%A9%E5%88%A4%E5%AE%9A
let initHPSum = [].concat(mainFleet, escortFleet || []).reduce((x, s) => x + (s ? s.initHP : 0), 0)
let nowHPSum = [].concat(mainFleet, escortFleet || []).reduce((x, s) => x + (s ? s.nowHP : 0), 0)
let rate = (initHPSum - nowHPSum) / initHPSum * 100
if (rate <= 0) return Rank.SS
if (rate < 10) return Rank.A
if (rate < 20) return Rank.B
if (rate < 50) return Rank.C
if (rate < 80) return Rank.D
// else
return Rank.E
}
function simulateFleetMVP(fleet) {
if (fleet == null) fleet = []
let m = -1, mvp = null
for (const [i, ship] of fleet.entries()) {
if (ship == null)
continue
if (mvp == null || ship.damage > mvp.damage) {
m = i
mvp = ship
}
}
return m
}
function simulateFleetNightMVP(stages) {
// Damage sum: Only escort fleet
let sum = Array(6).fill(0)
for (const stage of stages) {
if (!(stage != null && stage.attacks != null))
continue
if (!(stage.type === StageType.Shelling && stage.subtype === StageType.Night))
continue
for (const attack of stage.attacks) {
const {fromShip: ship, damage} = attack
if (ship == null || ship.owner != ShipOwner.Ours)
continue
const {pos} = ship
if (6 <= pos && pos <= 11)
sum[pos - 7] += damage.reduce((x, y) => x + y, 0)
else
console.warn("Non-escort fleet ship attack in night stage", ship, attack)
}
}
let m = 0 // MVP index
for (const [i, damage] of sum.entries()) {
if (damage > sum[m]) {
m = i
}
}
return m
}
function getEngagementStage(packet) {
// This function is usable for day combat only.
const engagement = generateEngagementInfo(packet, null, null, {engagement: true})
return engagement == null ? null : new Stage({
type: StageType.Engagement,
engagement: engagement,
})
}
class Simulator2 {
constructor(fleet, opts={}) {
// When no using poi API:
// enemyShip.raw == null
this.usePoiAPI = opts.usePoiAPI
this.fleetType = fleet.type || 0
this.mainFleet = this._initFleet(fleet.main, 0)
this.escortFleet = this._initFleet(fleet.escort, 6)
this.supportFleet = this._initFleet(fleet.support)
this.landBaseAirCorps = fleet.LBAC
this.enemyFleet = null // Assign at first packet
this.enemyEscort = null // ^
this.friendFleet = null // NPC Friend fleet support
// Stage
this.stages = []
this._result = null
this._isAirRaid = false
this._isEngaged = false
this._isNightOnlyMVP = false
}
static auto(battle, opts) {
if (battle == null)
return
let s = new Simulator2(battle.fleet, opts)
for (const packet of battle.packet)
s.simulate(packet)
return s
}
_initFleet(rawFleet, intl=0) {
if (!(rawFleet != null)) return
let fleet = []
for (let [i, rawShip] of rawFleet.entries()) {
if (rawShip != null) {
let slots = rawShip.poi_slot.concat(rawShip.poi_slot_ex)
let baseParam, finalParam
if (this.usePoiAPI) {
const kyouka = rawShip.api_kyouka
const $ship = window.$ships[rawShip.api_ship_id]
if (typeof $ship != 'undefined') {
baseParam =[
$ship.api_houg[0] + kyouka[0],
$ship.api_raig[0] + kyouka[1],
$ship.api_tyku[0] + kyouka[2],
$ship.api_souk[0] + kyouka[3],