diff --git a/binaries/data/mods/public/simulation/ai/petra/attackPlan.js b/binaries/data/mods/public/simulation/ai/petra/attackPlan.js index 75f5c11e5d..690270d095 100644 --- a/binaries/data/mods/public/simulation/ai/petra/attackPlan.js +++ b/binaries/data/mods/public/simulation/ai/petra/attackPlan.js @@ -1329,7 +1329,7 @@ m.AttackPlan.prototype.update = function(gameState, events) { // if units are attacked, abandon their target (if it was a structure or a support) and retaliate // also if our unit is attacking a range unit and the attacker is a melee unit, retaliate var orderData = ourUnit.unitAIOrderData(); - if (orderData.length !== 0 && orderData[0]["target"]) + if (orderData && orderData.length && orderData[0]["target"]) { var target = gameState.getEntityById(orderData[0]["target"]); if (target && !target.hasClass("Structure") && !target.hasClass("Support")) @@ -1462,14 +1462,14 @@ m.AttackPlan.prototype.update = function(gameState, events) { if (!maybeUpdate) { - this.CheckCapture(ent); + this.CheckCapture(gameState, ent); continue; } let deltat = (ent.unitAIState() === "INDIVIDUAL.COMBAT.APPROACHING") ? 10 : 5; let lastAttackPlanUpdateTime = ent.getMetadata(PlayerID, "lastAttackPlanUpdateTime"); if (lastAttackPlanUpdateTime && (time - lastAttackPlanUpdateTime) < deltat) { - this.CheckCapture(ent); + this.CheckCapture(gameState, ent); continue; } } @@ -1636,7 +1636,7 @@ m.AttackPlan.prototype.update = function(gameState, events) this.unitCollection.forEach( function (unit) { if (!unit.position()) return; - if (unit.unitAIState().split(".")[1] !== "COMBAT" || unit.unitAIOrderData().length == 0 + if (unit.unitAIState().split(".")[1] !== "COMBAT" || !unit.unitAIOrderData().length || !unit.unitAIOrderData()[0]["target"]) return; var dist = API3.SquareVectorDistance(unit.position(), ent.position()); @@ -1787,22 +1787,22 @@ m.AttackPlan.prototype.debugAttack = function() API3.warn("------------------------------"); }; -m.AttackPlan.prototype.CheckCapture = function(ent) +m.AttackPlan.prototype.CheckCapture = function(gameState, ent) { let state = ent.unitAIState(); if (!state || !state.split(".")[1] || state.split(".")[1] !== "COMBAT") return; let orderData = ent.unitAIOrderData(); - if (!orderData || !orderData.target || !orderData.attackType || orderData.attackType !== "Capture") + if (!orderData || !orderData.length || !orderData[0].target || !orderData[0].attackType || orderData[0].attackType !== "Capture") return; - let target = gameState.getEntityById(orderData.target); + let target = gameState.getEntityById(orderData[0].target); if (!target) return; // For the time being, do not try to capture rams if (target.hasClass("Siege") && target.hasClass("Melee")) { - ent.attack(target.id(), false); + ent.attack(orderData[0].target, false); return; } @@ -1814,7 +1814,7 @@ m.AttackPlan.prototype.CheckCapture = function(ent) // but TODO need to know how many units are currently capturing this target // For the time being, we check on our army length if (target.garrisoned() && target.garrisoned().length > Math.floor(this.unitCollection.length / 2)) - ent.attack(target.id(), false); + ent.attack(orderData[0].target, false); }; m.AttackPlan.prototype.Serialize = function() diff --git a/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js b/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js index 5994eab69f..ad81236449 100644 --- a/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js +++ b/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js @@ -25,9 +25,9 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID) var distMin = undefined; var idMinAll = undefined; var distMinAll = undefined; - for (var id of this.foeEntities) + for (let id of this.foeEntities) { - var eEnt = gameState.getEntityById(id); + let eEnt = gameState.getEntityById(id); if (!eEnt || !eEnt.position()) // probably can't happen. continue; @@ -43,7 +43,7 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID) || (this.assignedAgainst[id].length > 5 && !eEnt.hasClass("Hero") && !eEnt.hasClass("Siege"))) continue; - var dist = API3.SquareVectorDistance(ent.position(), eEnt.position()); + let dist = API3.SquareVectorDistance(ent.position(), eEnt.position()); if (idMinAll === undefined || dist < distMinAll) { idMinAll = id; @@ -65,14 +65,15 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID) else return false; - var ownIndex = gameState.ai.accessibility.getAccessValue(ent.position()); - var foePosition = gameState.getEntityById(idFoe).position(); - var foeIndex = gameState.ai.accessibility.getAccessValue(foePosition); + let ownIndex = gameState.ai.accessibility.getAccessValue(ent.position()); + let foeEnt = gameState.getEntityById(idFoe); + let foePosition = foeEnt.position(); + let foeIndex = gameState.ai.accessibility.getAccessValue(foePosition); if (ownIndex == foeIndex || ent.hasClass("Ship")) { this.assignedTo[entID] = idFoe; this.assignedAgainst[idFoe].push(entID); - ent.attack(idFoe); + ent.attack(idFoe, (!foeEnt.hasClass("Siege") || !foeEnt.hasClass("Melee"))); } else gameState.ai.HQ.navalManager.requireTransport(gameState, ent, ownIndex, foeIndex, foePosition); @@ -105,9 +106,15 @@ m.DefenseArmy.prototype.update = function (gameState) let ent = gameState.getEntityById(entId); if (!ent) continue; - let orders = ent.unitAIOrderData(); - if (orders.length == 0 && !ent.getMetadata(PlayerID, "transport")) + let orderData = ent.unitAIOrderData(); + if (!orderData.length && !ent.getMetadata(PlayerID, "transport")) this.assignUnit(gameState, entId); + else if (orderData.length && orderData[0].target && orderData[0].attackType && orderData[0].attackType === "Capture") + { + let target = gameState.getEntityById(orderData[0].target); + if (target && target.hasClass("Siege") && target.hasClass("Melee")) + ent.attack(orderData[0].target, false); + } } return this.onUpdate(gameState);