Fix the two incorrect replacements in rP21434, refs #4950.
[0ad.git] / binaries / data / mods / public / maps / random / polar_sea_triggers.js
blob426aadc47acf4fc27f3696adc35c39d395f9aa8c
1 const debugLog = false;
3 var attackerTemplate = "trigger/fauna_arctic_wolf_attack";
5 var minWaveSize = 1;
6 var maxWaveSize = 3;
8 var firstWaveTime = 5;
9 var minWaveTime = 2;
10 var maxWaveTime = 4;
12 /**
13  * Attackers will focus the targetCount closest units that have the targetClasses type.
14  */
15 var targetClasses = "Organic+!Domestic";
16 var targetCount = 3;
18 var disabledTechnologies = [
19         "gather_lumbering_ironaxes",
20         "gather_lumbering_sharpaxes",
21         "gather_lumbering_strongeraxes"
24 Trigger.prototype.InitDisableTechnologies = function()
26         for (let i = 1; i < TriggerHelper.GetNumberOfPlayers(); ++i)
27                 QueryPlayerIDInterface(i).SetDisabledTechnologies(disabledTechnologies);
30 Trigger.prototype.SpawnWolvesAndAttack = function()
32         let waveSize = Math.round(Math.random() * (maxWaveSize - minWaveSize) + minWaveSize);
33         let attackers = TriggerHelper.SpawnUnitsFromTriggerPoints("A", attackerTemplate, waveSize, 0);
35         if (debugLog)
36                 print("Spawned " + waveSize + " " + attackerTemplate + " at " + Object.keys(attackers).length + " points\n");
38         let allTargets;
40         let cmpDamage = Engine.QueryInterface(SYSTEM_ENTITY, IID_Damage);
41         let players = new Array(TriggerHelper.GetNumberOfPlayers()).fill(0).map((v, i) => i + 1);
43         for (let spawnPoint in attackers)
44         {
45                 // TriggerHelper.SpawnUnits is guaranteed to spawn
46                 let firstAttacker = attackers[spawnPoint][0];
47                 if (!firstAttacker)
48                         continue;
50                 let cmpAttackerPos = Engine.QueryInterface(firstAttacker, IID_Position);
51                 if (!cmpAttackerPos || !cmpAttackerPos.IsInWorld())
52                         continue;
54                 let attackerPos = cmpAttackerPos.GetPosition2D();
56                 // The returned entities are sorted by RangeManager already
57                 let targets = cmpDamage.EntitiesNearPoint(attackerPos, 200, players).filter(ent => {
58                         let cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
59                         return cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), targetClasses);
60                 });
62                 let goodTargets = targets.slice(0, targetCount);
64                 // Look through all targets if there aren't enough nearby ones
65                 if (goodTargets.length < targetCount)
66                 {
67                         if (!allTargets)
68                                 allTargets = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).GetNonGaiaEntities().filter(ent => {
69                                         let cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
70                                         return cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), targetClasses);
71                                 });
73                         let getDistance = target => {
74                                 let cmpPositionTarget = Engine.QueryInterface(target, IID_Position);
75                                 if (!cmpPositionTarget || !cmpPositionTarget.IsInWorld())
76                                         return Infinity;
77                                 return attackerPos.distanceToSquared(cmpPositionTarget.GetPosition2D());
78                         };
80                         goodTargets = [];
81                         let goodDists = [];
82                         for (let target of allTargets)
83                         {
84                                 let dist = getDistance(target);
85                                 let i = goodDists.findIndex(element => dist < element);
86                                 if (i != -1 || goodTargets.length < targetCount)
87                                 {
88                                         if (i == -1)
89                                                 i = goodTargets.length;
90                                         goodTargets.splice(i, 0, target);
91                                         goodDists.splice(i, 0, dist);
92                                         if (goodTargets.length > targetCount)
93                                         {
94                                                 goodTargets.pop();
95                                                 goodDists.pop();
96                                         }
97                                 }
98                         }
99                 }
101                 for (let target of goodTargets)
102                         ProcessCommand(0, {
103                                 "type": "attack",
104                                 "entities": attackers[spawnPoint],
105                                 "target": target,
106                                 "queued": true
107                         });
108         }
110         this.DoAfterDelay((Math.random() * (maxWaveTime - minWaveTime) + minWaveTime) * 60 * 1000, "SpawnWolvesAndAttack", {});
114         let cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
115         cmpTrigger.InitDisableTechnologies();
116         cmpTrigger.DoAfterDelay(firstWaveTime * 60 * 1000, "SpawnWolvesAndAttack", {});