From 8551a799d0bccc468cf1fd91a9556677f2abe0ac Mon Sep 17 00:00:00 2001 From: mimo Date: Tue, 10 Jun 2014 13:45:23 +0000 Subject: [PATCH] Petra: prevent gatherers to stay blocked when UnitAI make them return resource in an inaccessible dropsite, see #2563 This was SVN commit r15325. --- .../mods/public/simulation/ai/petra/worker.js | 64 +++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/binaries/data/mods/public/simulation/ai/petra/worker.js b/binaries/data/mods/public/simulation/ai/petra/worker.js index 46fb163302..f04e7c9824 100644 --- a/binaries/data/mods/public/simulation/ai/petra/worker.js +++ b/binaries/data/mods/public/simulation/ai/petra/worker.js @@ -87,6 +87,24 @@ m.Worker.prototype.update = function(baseManager, gameState) } } } + else if (this.ent.unitAIState() === "INDIVIDUAL.RETURNRESOURCE.APPROACHING" + && gameState.ai.playedTurn % 10 === 0) + { + // Check from time to time that UnitAI does not send us to an inaccessible dropsite + var dropsite = gameState.getEntityById(this.ent.unitAIOrderData()[0]["target"]); + if (dropsite && dropsite.position()) + { + var access = gameState.ai.accessibility.getAccessValue(this.ent.position()); + var goalAccess = dropsite.getMetadata(PlayerID, "access"); + if (!goalAccess || dropsite.hasClass("Elephant")) + { + goalAccess = gameState.ai.accessibility.getAccessValue(dropsite.position()); + dropsite.setMetadata(PlayerID, "access", goalAccess); + } + if (access !== goalAccess) + this.returnResources(gameState); + } + } } else if (subrole === "builder") { @@ -105,14 +123,17 @@ m.Worker.prototype.update = function(baseManager, gameState) } else { - var startIndex = gameState.ai.accessibility.getAccessValue(this.ent.position()); - var endIndex = target.getMetadata(PlayerID, "access"); - if (!endIndex) - endIndex = gameState.ai.accessibility.getAccessValue(target.position()); - if (startIndex === endIndex) + var access = gameState.ai.accessibility.getAccessValue(this.ent.position()); + var goalAccess = target.getMetadata(PlayerID, "access"); + if (!goalAccess) + { + goalAccess = gameState.ai.accessibility.getAccessValue(target.position()); + target.setMetadata(PlayerID, "access", goalAccess); + } + if (access === goalAccess) this.ent.repair(target); else - gameState.ai.HQ.navalManager.requireTransport(gameState, this.ent, startIndex, endIndex, target.position()); + gameState.ai.HQ.navalManager.requireTransport(gameState, this.ent, access, goalAccess, target.position()); } } else if (subrole === "hunter") @@ -146,12 +167,33 @@ m.Worker.prototype.update = function(baseManager, gameState) this.ent.setMetadata(PlayerID, "lastHuntSearch", gameState.ai.playedTurn); } } - else if (this.ent.unitAIState().split(".")[1] === "GATHER" || this.ent.unitAIState().split(".")[1] === "RETURNRESOURCE") + else if (gameState.ai.playedTurn % 10 === 0) // Perform some checks from time to time { - // we may have drifted towards ennemy territory during the hunt, if yes go home - var territoryOwner = gameState.ai.HQ.territoryMap.getOwner(this.ent.position()); - if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // player is its own ally - this.startHunting(gameState); + if (this.ent.unitAIState().split(".")[1] === "GATHER" + || this.ent.unitAIState().split(".")[1] === "RETURNRESOURCE") + { + // we may have drifted towards ennemy territory during the hunt, if yes go home + var territoryOwner = gameState.ai.HQ.territoryMap.getOwner(this.ent.position()); + if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // player is its own ally + this.startHunting(gameState); + else if (this.ent.unitAIState() === "INDIVIDUAL.RETURNRESOURCE.APPROACHING") + { + // Check that UnitAI does not send us to an inaccessible dropsite + var dropsite = gameState.getEntityById(this.ent.unitAIOrderData()[0]["target"]); + if (dropsite && dropsite.position()) + { + var access = gameState.ai.accessibility.getAccessValue(this.ent.position()); + var goalAccess = dropsite.getMetadata(PlayerID, "access"); + if (!goalAccess || dropsite.hasClass("Elephant")) + { + goalAccess = gameState.ai.accessibility.getAccessValue(dropsite.position()); + dropsite.setMetadata(PlayerID, "access", goalAccess); + } + if (access !== goalAccess) + this.returnResources(gameState); + } + } + } } } else if (subrole === "fisher")