-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZombieHordeDemolition.ts
More file actions
1082 lines (932 loc) · 36.9 KB
/
ZombieHordeDemolition.ts
File metadata and controls
1082 lines (932 loc) · 36.9 KB
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
const enableDebug: boolean = false;
const aiSpawnerId: number = 101;
const zeroVector: mod.Vector = mod.CreateVector(0, 0, 0);
export async function OnGameModeStarted() {
const pos = mod.GetObjectPosition(mod.GetHQ(1));
const rot = mod.CreateVector(mod.DegreesToRadians(-1), 0, 0);
mod.SpawnObject(mod.RuntimeSpawn_Common.DeployCam, pos, rot);
const safetyAreaFX = mod.SpawnObject(mod.RuntimeSpawn_Common.FX_Gadget_SupplyCrate_Range_Indicator, mod.GetObjectPosition(mod.GetHQ(1)), mod.CreateVector(0, 0, 0));
mod.EnableVFX(safetyAreaFX, true);
AISpawner.Setup(aiSpawnerId);
AISpawner.team = mod.GetTeam(2);
ZombieManager.team = mod.GetTeam(2);
ZombiePenalty.Setup();
GameDirector.Setup();
}
export function OnPlayerJoinGame(eventPlayer: mod.Player) {
PlayerNotifications.Create(eventPlayer);
}
export function OnPlayerLeaveGame(eventNumber: number) {
PlayerNotifications.Destroy(eventNumber);
}
export function OnPlayerDeployed(eventPlayer: mod.Player): void {
SurvivorModifier.OnDeployed(eventPlayer);
AISpawner.OnDeployed(eventPlayer);
ZombieModifier.OnDeploy(eventPlayer);
ZombieManager.OnDeployed(eventPlayer);
PlayerNotifications.Get(eventPlayer)?.Push({
message: mod.Message(mod.stringkeys.briefing),
minDuration: 5,
maxDuration: 10
});
}
export function OnPlayerUndeploy(eventPlayer: mod.Player) {
ZombieManager.OnUndeployed(eventPlayer);
}
export function OngoingGlobal(): void {
ZombieManager.OnUpdate();
DebugBoard.Set(0, ZombieManager.GetInstanceCount());
DebugBoard.Set(1, ZombieManager.GetAliveCount());
DebugBoard.Set(2, AISpawner.GetWaitingCount());
DebugBoard.Set(3, GameDirector.targetZombieCount);
}
export function OnRayCastHit(
eventPlayer: mod.Player,
eventPoint: mod.Vector,
eventNormal: mod.Vector): void {
if (!eventPlayer || mod.GetObjId(eventPlayer) < 0) {
Raycaster.OnHit(eventPoint, eventNormal);
}
}
export function OnRayCastMissed(eventPlayer: mod.Player): void {
if (!eventPlayer || mod.GetObjId(eventPlayer) < 0) {
Raycaster.OnMissed();
}
}
export function OnPlayerInteract(eventPlayer: mod.Player, eventInteractPoint: mod.InteractPoint): void {
Interactor.OnInteract(eventPlayer, eventInteractPoint);
}
class AISpawnParameter {
position: mod.Vector;
orientation: number;
constructor(position: mod.Vector, orientation: number) {
this.position = position;
this.orientation = orientation;
}
}
class AISpawner {
static spawner: mod.Spawner;
static team: mod.Team;
static #isDeploying: boolean = false;
static waitingDeployParams: AISpawnParameter[] = [];
static Setup(aiSpawnerIdForLocalServer: number): void {
this.spawner = mod.GetSpawner(aiSpawnerIdForLocalServer);
}
static OnDeployed(player: mod.Player): void {
if (mod.Equals(mod.GetTeam(player), this.team)) {
this.#isDeploying = false;
const param = this.waitingDeployParams.shift()
if (param) {
mod.Teleport(player, param.position, param.orientation);
this.#TrySpawnNext();
}
}
}
static Spawn(parameters: AISpawnParameter): void {
this.waitingDeployParams.push(parameters);
this.#TrySpawnNext();
}
static #TrySpawnNext() {
if (!this.#isDeploying && this.waitingDeployParams.length > 0) {
this.#isDeploying = true;
mod.SpawnAIFromAISpawner(this.spawner, mod.Message(mod.stringkeys.enemy_name), this.team);
}
}
static GetWaitingCount(): number {
return this.waitingDeployParams.length;
}
}
type RaycastResult = {
isHit: boolean;
hitPosition: mod.Vector;
hitNormal: mod.Vector;
}
type RaycastWaitingData = {
startPosition: mod.Vector;
endPosition: mod.Vector;
callback: (result: RaycastResult) => void;
}
class Raycaster {
static #queue: RaycastWaitingData[] = [];
static #isRunning: boolean = false;
static async RaycastBetween(startPosition: mod.Vector, endPosition: mod.Vector): Promise<RaycastResult> {
let returnValue: RaycastResult | undefined = undefined;
const callback = (result: RaycastResult): void => {
returnValue = result;
}
const data = {startPosition, endPosition, callback};
this.#queue.push(data);
this.CheckNext();
while (!returnValue) {
await mod.Wait(0.01);
}
return returnValue;
}
static async Raycast(origin: mod.Vector, direction: mod.Vector, distance: number): Promise<RaycastResult> {
const endPosition = mod.Add(origin, mod.Multiply(direction, distance));
return await this.RaycastBetween(origin, endPosition);
}
static OnHit(hitPosition: mod.Vector, hitNormal: mod.Vector): void {
this.#isRunning = false;
const data = this.#queue.shift();
if (data) {
data.callback({
isHit: true,
hitPosition,
hitNormal
});
}
this.CheckNext();
}
static OnMissed(): void {
this.#isRunning = false;
const data = this.#queue.shift();
if (data) {
data.callback({
isHit: false,
hitPosition: zeroVector,
hitNormal: zeroVector
});
}
this.CheckNext();
}
static CheckNext() {
if (!this.#isRunning && this.#queue.length > 0) {
this.#isRunning = true;
const firstData = this.#queue[0];
mod.RayCast(firstData.startPosition, firstData.endPosition);
}
}
}
type InteractResult = {
player: mod.Player;
payload: any;
}
type InteractWaitingData = {
interactPoint: mod.InteractPoint;
payload: any;
callback: (result: InteractResult) => void;
}
class Interactor {
static interactWaitingData: Map<number, InteractWaitingData> = new Map();
static Register(interactPoint: mod.InteractPoint, callback: (result: InteractResult) => void, payload: any = undefined): void {
const objId = mod.GetObjId(interactPoint);
this.interactWaitingData.set(objId, {interactPoint, payload, callback});
}
static Unregister(interactPoint: mod.InteractPoint): void {
const objId = mod.GetObjId(interactPoint);
this.interactWaitingData.delete(objId);
}
static OnInteract(eventPlayer: mod.Player, interactPoint: mod.InteractPoint): void {
const objId = mod.GetObjId(interactPoint);
const data = this.interactWaitingData.get(objId);
if (data) {
data.callback({player: eventPlayer, payload: data.payload});
}
}
}
enum ZombieBehavior {
Move,
BattleField,
}
const zombieBehaviorInterval = 120;
const zombieMoveUpdateInterval = 30;
const zombieBattleDistance = 0;
const zombieFindTargetInterval = 120;
class Zombie {
player: mod.Player;
playerId: number;
targetPosition: mod.Vector = zeroVector;
targetEyePosition: mod.Vector = zeroVector;
targetPlayer: mod.Player | undefined;
behavior: ZombieBehavior = ZombieBehavior.Move;
behaviorElapsedFrame: number = zombieBehaviorInterval;
moveBehaviorUpdateElapsedFrame: number = 0;
findTargetElapsedFrame: number = 0;
penaltyStack: number = 0;
constructor(player: mod.Player) {
this.player = player;
this.playerId = mod.GetObjId(player);
this.ChangeBehavior(this.behavior);
}
IsAlive(): boolean {
return this.player && mod.GetObjId(this.player) >= 0 && mod.GetSoldierState(this.player, mod.SoldierStateBool.IsAlive);
}
HasInstance(): boolean {
return this.player && mod.GetObjId(this.player) >= 0;
}
Update(): void {
if (!this.targetPlayer || !mod.GetSoldierState(this.targetPlayer, mod.SoldierStateBool.IsAlive)) {
this.targetPlayer = undefined;
}
this.findTargetElapsedFrame++;
if (this.findTargetElapsedFrame > zombieFindTargetInterval) {
this.findTargetElapsedFrame = 0;
//this.targetPlayer = FindClosestPlayer(mod.GetObjectPosition(this.player), mod.GetTeam(1));
}
if (this.targetPlayer) {
this.targetPosition = mod.GetObjectPosition(this.targetPlayer);
this.targetEyePosition = mod.GetSoldierState(this.targetPlayer, mod.SoldierStateVector.EyePosition);
}
if (this.behavior == ZombieBehavior.Move) {
if (this.targetPlayer) {
//mod.AISetTarget(this.player, this.targetPlayer); // Crashes when dying on an online server.
mod.AISetFocusPoint(this.player, this.targetEyePosition, true);
} else {
mod.AISetFocusPoint(this.player, this.targetEyePosition, true);
}
}
if (this.behavior == ZombieBehavior.Move) {
this.moveBehaviorUpdateElapsedFrame++;
if (this.moveBehaviorUpdateElapsedFrame > zombieMoveUpdateInterval) {
this.moveBehaviorUpdateElapsedFrame = 0;
mod.AIMoveToBehavior(this.player, this.targetPosition);
}
}
const targetDistance = mod.DistanceBetween(mod.GetObjectPosition(this.player), this.targetPosition);
if (targetDistance < zombieBattleDistance && this.targetPlayer) {
if (this.behavior != ZombieBehavior.BattleField) {
this.ChangeBehavior(ZombieBehavior.BattleField);
}
} else {
this.behaviorElapsedFrame++;
if (this.behaviorElapsedFrame > zombieBehaviorInterval) {
this.behaviorElapsedFrame = 0;
this.targetPlayer = FindClosestPlayer(mod.GetObjectPosition(this.player), mod.GetTeam(1));
if (!this.targetPlayer) {
this.targetPosition = mod.GetObjectPosition(mod.GetHQ(2));
this.targetEyePosition = this.targetPosition;
}
this.ChangeBehavior(this.behavior == ZombieBehavior.Move ? ZombieBehavior.BattleField : ZombieBehavior.Move);
}
}
}
ChangeBehavior(behavior: ZombieBehavior) {
this.behavior = behavior;
switch (behavior) {
case ZombieBehavior.Move:
mod.AIMoveToBehavior(this.player, this.targetPosition);
break;
case ZombieBehavior.BattleField:
mod.AIBattlefieldBehavior(this.player);
break;
}
}
}
class ZombieModifier {
static team: mod.Team = mod.GetTeam(2);
static OnDeploy(player: mod.Player) {
if (mod.Equals(mod.GetTeam(player), this.team)) {
// Fix for using smoke grenade
mod.RemoveEquipment(player, mod.InventorySlots.Throwable);
}
}
}
class ZombieManager {
static team: mod.Team = mod.GetTeam(2);
static allZombies: Map<number, Zombie> = new Map();
static OnDeployed(player: mod.Player) {
if (mod.Equals(mod.GetTeam(player), this.team)) {
const playerId = mod.GetObjId(player);
this.allZombies.set(playerId, new Zombie(player));
}
}
static OnUndeployed(player: mod.Player) {
if (mod.Equals(mod.GetTeam(player), this.team)) {
const playerId = mod.GetObjId(player);
const zombie = this.allZombies.get(playerId);
if (zombie) {
this.allZombies.delete(playerId);
}
}
}
static OnUpdate() {
const zombies = this.allZombies.values();
for (const zombie of zombies) {
zombie.Update()
}
for (const zombie of zombies) {
if (!zombie.HasInstance()) {
this.allZombies.delete(zombie.playerId);
break;
}
}
}
static GetZombie(player: mod.Player): Zombie | undefined {
return this.allZombies.get(mod.GetObjId(player));
}
static GetAliveCount(): number {
let count = 0;
const zombies = this.allZombies.values();
for (const zombie of zombies) {
if (zombie.IsAlive()) {
count++;
}
}
return count;
}
static GetInstanceCount(): number {
return this.allZombies.size;
}
}
type ZombieNestSettings = {
nameKey: any;
position: mod.Vector;
orientation: number;
isShowTargetText: boolean;
bombTimerDuration: number;
voiceOverFlag: mod.VoiceOverFlags;
}
class ZombieNest {
position: mod.Vector;
rotation: mod.Vector;
settings: ZombieNestSettings;
#worldIcon: mod.WorldIcon;
#interactPoint: mod.InteractPoint;
#bombObj: any;
#voiceOver: mod.VO;
#alarmSound: mod.SFX;
#explosionEffect: mod.VFX;
#areaEffect: mod.VFX;
spawnRemainCount: number = 0;
isAlive: boolean = true;
#isArmed: boolean = false;
#isPlayingAlarm: boolean = false;
#areaRadius: number = 20;
// Set position and orientation (radian)
constructor(settings: ZombieNestSettings) {
this.settings = settings;
this.position = this.settings.position;
this.rotation = mod.CreateVector(0, this.settings.orientation, 0);
this.#worldIcon = mod.SpawnObject(mod.RuntimeSpawn_Common.WorldIcon, mod.Add(this.position, mod.Multiply(mod.UpVector(), 0.3)), this.rotation);
this.#interactPoint = mod.SpawnObject(mod.RuntimeSpawn_Common.InteractPoint, mod.Add(this.position, mod.Multiply(mod.UpVector(), 0.3)), this.rotation);
this.#bombObj = mod.SpawnObject(mod.RuntimeSpawn_Common.OrdinanceCrate_01, this.position, this.rotation);
this.#voiceOver = mod.SpawnObject(mod.RuntimeSpawn_Common.SFX_VOModule_OneShot2D, zeroVector, zeroVector);
this.#alarmSound = mod.SpawnObject(mod.RuntimeSpawn_Common.SFX_Alarm, this.position, this.rotation);
this.#areaEffect = mod.SpawnObject(mod.RuntimeSpawn_Common.FX_SupplyVehicleStation_Range_Indicator, this.position, this.rotation);
this.#explosionEffect = mod.SpawnObject(mod.RuntimeSpawn_Common.FX_CivCar_SUV_Explosion, this.position, this.rotation);
this.SetupObjects();
void this.#RepeatSpawnCheck();
}
#UnspawnObjects() {
mod.UnspawnObject(this.#worldIcon);
mod.UnspawnObject(this.#interactPoint);
mod.UnspawnObject(this.#bombObj);
mod.UnspawnObject(this.#alarmSound);
mod.UnspawnObject(this.#areaEffect);
}
SetupObjects() {
// SFX
mod.SetSFXVolume(this.#alarmSound, 0.5);
// World icon
//mod.SetWorldIconPosition(this.worldIcon, mod.GetObjectPosition(this.worldIcon));
mod.SetWorldIconImage(this.#worldIcon, mod.WorldIconImages.Bomb);
mod.SetWorldIconColor(this.#worldIcon, mod.CreateVector(1, 1, 1))
mod.SetWorldIconText(this.#worldIcon, mod.Message(mod.stringkeys.target));
mod.EnableWorldIconImage(this.#worldIcon, true);
mod.EnableWorldIconText(this.#worldIcon, this.settings.isShowTargetText);
mod.SetWorldIconOwner(this.#worldIcon, mod.GetTeam(1));
// Interact
mod.EnableInteractPoint(this.#interactPoint, true);
Interactor.Register(this.#interactPoint, this.OnInteractBomb.bind(this));
}
Spawn(count: number): void {
this.spawnRemainCount += count;
}
async #RepeatSpawnCheck() {
const tick = 1 / 60;
while (this.isAlive) {
await mod.Wait(tick);
if (this.spawnRemainCount > 0) {
this.spawnRemainCount--;
AISpawner.Spawn(new AISpawnParameter(mod.Add(this.position, mod.UpVector()), mod.YComponentOf(this.rotation)));
}
}
}
OnInteractBomb(result: InteractResult): void {
if (!this.#isArmed) {
this.#isArmed = true;
void this.#PlayArmedBombSequence(result.player);
}
}
async #PlayArmedBombSequence(armedPlayer: mod.Player) {
mod.EnableVFX(this.#areaEffect, true);
mod.PlayVO(this.#voiceOver, mod.VoiceOverEvents2D.MComArmFriendly, this.settings.voiceOverFlag);
mod.EnableInteractPoint(this.#interactPoint, false);
mod.SetWorldIconText(this.#worldIcon, mod.Message(mod.stringkeys.percentage, 0, 0));
mod.EnableWorldIconText(this.#worldIcon, true);
PlayerNotifications.PushToAll({
message: mod.Message(mod.stringkeys.bomb_armed, armedPlayer),
minDuration: 2,
maxDuration: 5
});
const tick = 1 / 60;
let elapsedTime = 0;
while (this.isAlive && elapsedTime < this.settings.bombTimerDuration) {
await mod.Wait(tick);
const isInArea = this.#IsInAreaAnyPlayer();
if (isInArea) elapsedTime += tick;
if (isInArea != this.#isPlayingAlarm) {
this.#isPlayingAlarm = isInArea;
mod.EnableSFX(this.#alarmSound, this.#isPlayingAlarm);
}
const progress = elapsedTime / this.settings.bombTimerDuration;
const percentage = mod.Floor(progress * 100);
let percentageUnder = mod.Floor(((progress * 100) % 1) * 10);
if (percentageUnder >= 10) percentageUnder = 9; // fix for rounding error
mod.SetWorldIconText(this.#worldIcon, mod.Message(mod.stringkeys.percentage, percentage, percentageUnder));
}
this.isAlive = false;
mod.PlayVO(this.#voiceOver, mod.VoiceOverEvents2D.MComDestroyedFriendly, this.settings.voiceOverFlag);
mod.EnableVFX(this.#areaEffect, false);
mod.EnableSFX(this.#alarmSound, false);
this.#Explode(armedPlayer);
this.#UnspawnObjects();
}
#IsInAreaAnyPlayer(): boolean {
const survivorTeam = mod.GetTeam(1);
const allPlayers = mod.AllPlayers();
const count = mod.CountOf(allPlayers);
for (let i = 0; i < count; i++) {
const player = mod.ValueInArray(allPlayers, i);
if (mod.GetSoldierState(player, mod.SoldierStateBool.IsAlive)
&& mod.Equals(mod.GetTeam(player), survivorTeam)
&& mod.DistanceBetween(this.position, mod.GetObjectPosition(player)) < this.#areaRadius) {
return true;
}
}
return false;
}
#Explode(armedPlayer: mod.Player) {
mod.EnableVFX(this.#explosionEffect, true);
DamageUtility.Explode(armedPlayer, this.position, 20, 200);
PlayerNotifications.PushToAll({
message: mod.Message(mod.stringkeys.target_destroyed, armedPlayer, this.settings.nameKey),
minDuration: 2,
maxDuration: 5
});
}
}
class ZombieNestManager {
static allNests: ZombieNest[] = [];
static CreateNest(settings: ZombieNestSettings): ZombieNest {
const nest = new ZombieNest(settings);
this.allNests.push(nest);
return nest;
}
}
const voiceOverFlags: mod.VoiceOverFlags[] = [
mod.VoiceOverFlags.Alpha,
mod.VoiceOverFlags.Bravo,
mod.VoiceOverFlags.Charlie,
mod.VoiceOverFlags.Delta,
mod.VoiceOverFlags.Echo,
mod.VoiceOverFlags.Foxtrot,
mod.VoiceOverFlags.Golf,
]
function GetVoiceOverFlag(index: number): mod.VoiceOverFlags {
return index < voiceOverFlags.length ? voiceOverFlags[index] : voiceOverFlags[0];
}
class GameDirector {
static targetZombieCount: number = 0;
static #maxZombieSpawnCountEachNest = 15;
static #spawnInterval: number = 8;
static #outpostCount: number = 3;
static #outpostStart: number = 0.3;
static #outpostEnd: number = 0.7;
static isActive: boolean = true;
static #coreNest: ZombieNest | undefined;
static Setup(): void {
void this.#PlaySetupSequence();
}
static async #PlaySetupSequence() {
await this.#CreateNests();
void this.#RepeatCalcTargetZombieCount();
void this.#RepeatSpawn();
void this.#RepeatRuleCheck();
}
static async #CreateNests() {
const fieldStart = mod.GetObjectPosition(mod.GetHQ(1));
const fieldEnd = mod.GetObjectPosition(mod.GetHQ(2));
// Core
{
const result = await this.#DetectGround(fieldEnd);
this.#coreNest = ZombieNestManager.CreateNest({
nameKey: mod.stringkeys.main_target,
position: result.position,
orientation: result.orientation,
isShowTargetText: true,
bombTimerDuration: 30,
voiceOverFlag: GetVoiceOverFlag(this.#outpostCount),
});
}
// Outposts
const lineDirection = mod.Normalize(mod.Subtract(fieldEnd, fieldStart));
const lineWidthMin = 8;
const lineWidthMax = 100;
const lineWidthMargin = 2;
const lineRight = mod.DirectionFromAngles(mod.AngleBetweenVectors(mod.ForwardVector(), lineDirection) + 90, 0);
for (let i = 0; i < this.#outpostCount; i++) {
const t = i / (this.#outpostCount - 1);
const lineT = this.#outpostStart + (this.#outpostEnd - this.#outpostStart) * t;
const midPoint = LerpVector(fieldStart, fieldEnd, lineT);
const sideWalls = await this.#DetectSideWall(mod.Add(midPoint, mod.Multiply(mod.UpVector(), 5)), lineRight, lineWidthMin / 2, lineWidthMax / 2, lineWidthMargin);
const randomPoint = LerpVector(sideWalls.left, sideWalls.right, mod.RandomReal(0, 1));
const result = await this.#DetectGround(randomPoint);
ZombieNestManager.CreateNest({
nameKey: mod.stringkeys.sub_target,
position: result.position,
orientation: result.orientation,
isShowTargetText: false,
bombTimerDuration: 10,
voiceOverFlag: GetVoiceOverFlag(i),
});
}
}
static async #DetectSideWall(position: mod.Vector, rightDirection: mod.Vector, minDistance: number, maxDistance: number, margin: number): Promise<{
left: mod.Vector,
right: mod.Vector
}> {
// Left
const leftDirection = mod.Multiply(rightDirection, -1);
const leftResult = await Raycaster.Raycast(position, leftDirection, maxDistance);
const leftDistance = leftResult.isHit ? mod.Max(mod.DistanceBetween(position, leftResult.hitPosition) - margin, minDistance) : maxDistance;
const leftPosition = mod.Add(position, mod.Multiply(leftDirection, leftDistance));
// Right
const rightResult = await Raycaster.Raycast(position, rightDirection, maxDistance);
const rightDistance = rightResult.isHit ? mod.Max(mod.DistanceBetween(position, rightResult.hitPosition) - margin, minDistance) : maxDistance;
const rightPosition = mod.Add(position, mod.Multiply(rightDirection, rightDistance));
return {left: leftPosition, right: rightPosition}
}
static async #DetectGround(position: mod.Vector): Promise<{ position: mod.Vector, orientation: number }> {
const startHeight = 10;
const distance = 100;
const checkCount = 8;
const hitPositions: mod.Vector[] = [];
// Center
{
const raycastResult = await Raycaster.Raycast(mod.Add(position, mod.Multiply(mod.UpVector(), startHeight)), mod.DownVector(), distance);
if (raycastResult.isHit) hitPositions.push(raycastResult.hitPosition);
}
// Around
const angleOffset = mod.RandomReal(0, 360);
const radius = 1;
for (let i = 0; i < checkCount; i++) {
const angle = angleOffset + (360 / checkCount) * i;
const direction = mod.DirectionFromAngles(angle, 0);
const aroundPos = mod.Add(position, mod.Multiply(direction, radius));
const raycastResult = await Raycaster.Raycast(mod.Add(aroundPos, mod.Multiply(mod.UpVector(), startHeight)), mod.DownVector(), distance);
if (raycastResult.isHit) hitPositions.push(raycastResult.hitPosition);
}
if (hitPositions.length == 0) {
return {position: position, orientation: 0};
}
let totalY = 0;
for (const hitPosition of hitPositions) {
totalY += mod.YComponentOf(hitPosition);
}
const averageY = totalY / hitPositions.length;
let closestAveragePosition = hitPositions[0];
let closestAverageDistance = Infinity;
for (const hitPosition of hitPositions) {
const dist = mod.AbsoluteValue(averageY - mod.YComponentOf(hitPosition));
if (dist < closestAverageDistance) {
closestAveragePosition = hitPosition;
closestAverageDistance = dist;
}
}
let hightestPosition = hitPositions[0];
let hightestY = -Infinity;
let lowestPosition = hitPositions[0];
let lowestY = Infinity;
for (const hitPosition of hitPositions) {
const y = mod.YComponentOf(hitPosition);
if (y > hightestY) {
hightestPosition = hitPosition;
hightestY = y;
}
if (y < lowestY) {
lowestPosition = hitPosition;
lowestY = y;
}
}
let diff = mod.Subtract(lowestPosition, hightestPosition);
let direction = mod.Normalize(mod.CreateVector(mod.XComponentOf(diff), 0, mod.ZComponentOf(diff)));
let angle = mod.AngleBetweenVectors(mod.ForwardVector(), direction);
return {position: closestAveragePosition, orientation: mod.DegreesToRadians(angle)};
}
static async #RepeatCalcTargetZombieCount() {
// Fixed starting wave
this.targetZombieCount = 80;
await mod.Wait(30);
// Wave
const interval = 15;
while (this.isActive) {
this.targetZombieCount = mod.Floor(mod.RandomReal(30, 50));
await mod.Wait(interval);
this.targetZombieCount = 80;
await mod.Wait(interval);
}
}
static async #RepeatSpawn() {
while (this.isActive) {
this.#SpawnZombies();
await mod.Wait(this.#spawnInterval);
}
}
static #SpawnZombies() {
const totalZombieCount = AISpawner.GetWaitingCount() + ZombieManager.GetInstanceCount();
const aliveNestCount = ZombieNestManager.allNests.filter(nest => nest.isAlive).length;
if (aliveNestCount > 0) {
const totalSpawnCount = this.targetZombieCount - totalZombieCount;
if (totalSpawnCount > 0) {
const spawnCountEachNest = Math.min(mod.Floor(totalSpawnCount / aliveNestCount), this.#maxZombieSpawnCountEachNest);
DebugBoard.Set(4, totalSpawnCount);
DebugBoard.Set(5, aliveNestCount);
DebugBoard.Set(6, spawnCountEachNest);
if (spawnCountEachNest > 0) {
for (const nest of ZombieNestManager.allNests) {
if (nest.isAlive) {
nest.Spawn(spawnCountEachNest);
}
}
}
}
}
}
static async #RepeatRuleCheck() {
const tick = 1 / 30;
while (this.isActive) {
await mod.Wait(tick);
if (this.#coreNest && !this.#coreNest.isAlive) {
this.isActive = false;
void this.#PlayVictory();
return;
}
}
}
static async #PlayVictory() {
mod.EndGameMode(mod.GetTeam(1));
}
}
class DamageUtility {
static Explode(attacker: mod.Player, position: mod.Vector, radius: number, damage: number) {
const team = mod.GetTeam(attacker);
const allPlayers = mod.AllPlayers();
const count = mod.CountOf(allPlayers);
for (let i = 0; i < count; i++) {
const player = mod.ValueInArray(allPlayers, i);
if (player
&& mod.GetSoldierState(player, mod.SoldierStateBool.IsAlive)
&& mod.NotEqualTo(team, mod.GetTeam(player))) {
const distance = mod.DistanceBetween(position, mod.GetObjectPosition(player));
if (distance < radius) {
mod.DealDamage(player, damage, attacker);
}
}
}
}
}
class ZombiePenalty {
static hqSafetyRadius: number = 8;
static speedThreshold: number = 0.01;
static penaltyDecreaseRate: number = 0.5;
static penaltyKillThreshold: number = 10;
static isEnabled: boolean = true;
static Setup() {
void this.#RepeatCheck();
}
static async #RepeatCheck() {
const interval = 1 / 30;
const hqPosition = mod.GetObjectPosition(mod.GetHQ(1));
while (this.isEnabled) {
await mod.Wait(interval);
const zombies = ZombieManager.allZombies.values();
for (const zombie of zombies) {
let additionalPenalty = 0;
// Teleport zombies in HQ
const zombiePosition = mod.GetObjectPosition(zombie.player);
const hqDistance = mod.DistanceBetween(hqPosition, zombiePosition);
if (hqDistance < this.hqSafetyRadius) {
const dir = mod.DirectionTowards(hqPosition, zombiePosition);
let farPos = mod.Add(hqPosition, mod.Multiply(dir, this.hqSafetyRadius + 0.1));
farPos = mod.CreateVector(mod.XComponentOf(farPos), mod.YComponentOf(zombiePosition), mod.ZComponentOf(farPos));
const faceDir = mod.GetSoldierState(zombie.player, mod.SoldierStateVector.GetFacingDirection);
const rad = Math.atan2(mod.XComponentOf(faceDir), mod.ZComponentOf(faceDir));
mod.Teleport(zombie.player, farPos, rad);
additionalPenalty += interval * 10;
}
// Speed penalty
const velocity = mod.GetSoldierState(zombie.player, mod.SoldierStateVector.GetLinearVelocity);
const speed = mod.DistanceBetween(zeroVector, velocity);
if (speed < this.speedThreshold) {
additionalPenalty += interval;
}
zombie.penaltyStack += additionalPenalty;
if (additionalPenalty == 0) {
zombie.penaltyStack -= interval * this.penaltyDecreaseRate;
}
zombie.penaltyStack = Math.max(0, zombie.penaltyStack);
// Kill
if (zombie.penaltyStack > this.penaltyKillThreshold) {
mod.Kill(zombie.player);
}
}
}
}
}
class SurvivorModifier {
static OnDeployed(player: mod.Player) {
void this.#LookToEnemyBaseWithDelay(player);
}
static async #LookToEnemyBaseWithDelay(player: mod.Player) {
await mod.Wait(0.1);
const position = mod.GetObjectPosition(player);
const dir = mod.DirectionTowards(position, mod.GetObjectPosition(mod.GetHQ(2)));
const rad = Math.atan2(mod.XComponentOf(dir), mod.ZComponentOf(dir));
mod.Teleport(player, position, rad);
}
}
//
// UI
//
type NotificationContent = {
message: mod.Message;
minDuration: number;
maxDuration: number;
}
class Notification {
#text: mod.UIWidget;
#contentsQueue: NotificationContent[] = [];
#isPlaying: boolean = false;
static #emptyMessage: mod.Message = mod.Message(0);
constructor(receiver: mod.Player | mod.Team) {
const textName = UniqueID.GetString();
mod.AddUIText(
textName,
mod.CreateVector(0, 200, 0),
mod.CreateVector(1000, 100, 0),
mod.UIAnchor.TopCenter,
Notification.#emptyMessage,
receiver
);
this.#text = mod.FindUIWidgetWithName(textName);
mod.SetUIWidgetBgColor(this.#text, mod.CreateVector(0, 0, 0));
//mod.SetUIWidgetBgAlpha(this.#text, 0.5);
mod.SetUIWidgetBgFill(this.#text, mod.UIBgFill.Blur);
mod.SetUITextColor(this.#text, mod.CreateVector(1, 1, 1));
mod.SetUITextAnchor(this.#text, mod.UIAnchor.Center);
mod.SetUIWidgetVisible(this.#text, false);
}
Dispose() {
if (this.#text) mod.DeleteUIWidget(this.#text);
}
Push(content: NotificationContent) {
this.#contentsQueue.push(content);
this.#TryPlayNext();
}
#TryPlayNext() {
if (!this.#isPlaying) {
const content = this.#contentsQueue.shift();
if (content) {
void this.PlayNotification(content);
}
}
}
async PlayNotification(content: NotificationContent) {
this.#isPlaying = true;
try {
const tick = 1 / 60;
await mod.Wait(tick); // Wait for the UI to be updated.
mod.SetUITextLabel(this.#text, content.message);
mod.SetUIWidgetVisible(this.#text, true);
let elapsedTime = 0;
while (true) {
await mod.Wait(tick);
elapsedTime += tick;
const duration = this.#contentsQueue.length > 0 ? content.minDuration : content.maxDuration;
if (elapsedTime > duration) {
break;
}
}
mod.SetUIWidgetVisible(this.#text, false);
await mod.Wait(tick); // Wait for the UI to be updated.
} finally {
this.#isPlaying = false;
}
this.#TryPlayNext();
}
}
class PlayerNotifications {
static #notifications: Map<number, Notification> = new Map();
static Create(player: mod.Player): Notification {
const playerId = mod.GetObjId(player);
const notification = new Notification(player);
this.#notifications.set(playerId, notification);
return notification;
}
static Destroy(playerId: number) {
const notification = this.#notifications.get(playerId);
if (notification) {
notification.Dispose();
this.#notifications.delete(playerId);
}
}
static Get(player: mod.Player): Notification | undefined {
const playerId = mod.GetObjId(player);
return this.#notifications.get(playerId);
}
static PushToAll(content: NotificationContent) {
for (const [_, notification] of this.#notifications) {
notification.Push(content);
}
}
}
//
// Utilities
//
class UniqueID {
static #lastId: number = 0;
static GetString(): string {
this.#lastId++;
return mod.Concat("_UNIQUE_ID_", this.#lastId.toString());
}
}
function FindClosestPlayer(position: mod.Vector, team: mod.Team): mod.Player | undefined {
const players = mod.AllPlayers();
let closestPlayer: mod.Player | undefined;
let closestDistance = Infinity;
const playerCount = mod.CountOf(players);
for (let i = 0; i < playerCount; i++) {
const player = mod.ValueInArray(players, i);
if (!player
|| mod.GetObjId(player) < 0