Fix UnitAI states in PetraAI.

Sometimes PetraAI relies on UnitAI state of an entity, but those were
not changed after modifying UnitAI.
Introduced in ea96e81098 and 3579097d95.

Differential revision: https://code.wildfiregames.com/D3976
This was SVN commit r25468.
This commit is contained in:
Freagarach 2021-05-20 08:31:30 +00:00
parent 453fe486de
commit 13c1b55f42
6 changed files with 34 additions and 34 deletions

View file

@ -709,7 +709,7 @@ m.Entity = m.Class({
return undefined;
if (this.unitAIOrderData().length &&
(this.unitAIState().split(".")[1] == "GATHER" || this.unitAIState().split(".")[1] == "RETURNRESOURCE"))
this.unitAIState().split(".")[1] == "GATHER")
{
let res;
// this is an abuse of "_ai" but it works.

View file

@ -430,7 +430,7 @@ PETRA.BaseManager.prototype.checkResourceLevels = function(gameState, queues)
{
if (ent.unitAIState() == "INDIVIDUAL.GATHER.GATHERING")
++this.gatherers[type].used;
else if (ent.unitAIState() == "INDIVIDUAL.RETURNRESOURCE.APPROACHING")
else if (ent.unitAIState() == "INDIVIDUAL.GATHER.RETURNINGRESOURCE.APPROACHING")
++this.gatherers[type].lost;
}
// TODO add also a test on remaining resources.

View file

@ -2448,7 +2448,7 @@ PETRA.HQ.prototype.assignGatherers = function()
{
for (let worker of base.workers.values())
{
if (worker.unitAIState().split(".")[1] != "RETURNRESOURCE")
if (worker.unitAIState().split(".").indexOf("RETURNRESOURCE") === -1)
continue;
let orders = worker.unitAIOrderData();
if (orders.length < 2 || !orders[1].target || orders[1].target != worker.getMetadata(PlayerID, "supply"))

View file

@ -560,7 +560,7 @@ PETRA.NavalManager.prototype.moveApart = function(gameState)
let unitAIState = ship.unitAIState();
if (ship.getMetadata(PlayerID, "transporter") !== undefined ||
unitAIState == "INDIVIDUAL.GATHER.APPROACHING" ||
unitAIState == "INDIVIDUAL.RETURNRESOURCE.APPROACHING")
unitAIState == "INDIVIDUAL.GATHER.RETURNINGRESOURCE.APPROACHING")
{
let previousPosition = ship.getMetadata(PlayerID, "previousPosition");
if (!previousPosition || previousPosition[0] != shipPosition[0] ||
@ -675,7 +675,7 @@ PETRA.NavalManager.prototype.moveApart = function(gameState)
let unitAIState = blockingShip.unitAIState();
if (blockingShip.getMetadata(PlayerID, "transporter") === undefined &&
unitAIState != "INDIVIDUAL.GATHER.APPROACHING" &&
unitAIState != "INDIVIDUAL.RETURNRESOURCE.APPROACHING")
unitAIState != "INDIVIDUAL.GATHER.RETURNINGRESOURCE.APPROACHING")
{
if (distSquare < 1600)
blockingShip.moveToRange(shipPosition[0], shipPosition[1], 40, 45);

View file

@ -110,7 +110,7 @@ PETRA.TradeManager.prototype.trainMoreTraders = function(gameState, queues)
PETRA.TradeManager.prototype.updateTrader = function(gameState, ent)
{
if (ent.hasClass("Ship") && gameState.ai.playedTurn % 5 == 0 &&
!ent.unitAIState().startsWith("INDIVIDUAL.GATHER") &&
!ent.unitAIState().startsWith("INDIVIDUAL.COLLECTTREASURE") &&
PETRA.gatherTreasure(gameState, ent, true))
return;

View file

@ -253,37 +253,37 @@ PETRA.Worker.prototype.update = function(gameState, ent)
}
}
}
}
else if (unitAIState == "INDIVIDUAL.RETURNRESOURCE.APPROACHING")
{
if (gameState.ai.playedTurn % 10 == 0)
if (unitAIState == "INDIVIDUAL.GATHER.RETURNINGRESOURCE.APPROACHING")
{
// Check from time to time that UnitAI does not send us to an inaccessible dropsite
let dropsite = gameState.getEntityById(ent.unitAIOrderData()[0].target);
if (dropsite && dropsite.position() && this.entAccess != PETRA.getLandAccess(gameState, dropsite))
PETRA.returnResources(gameState, this.ent);
}
// If gathering a sparse resource, we may have been sent to a faraway resource if the one nearby was full.
// Let's check if it is still the case. If so, we reset its metadata supplyId so that the unit will be
// reordered to gather after having returned the resources (when comparing its supplyId with the UnitAI one).
let gatherType = ent.getMetadata(PlayerID, "gather-type");
let influenceGroup = Resources.GetResource(gatherType).aiAnalysisInfluenceGroup;
if (influenceGroup && influenceGroup == "sparse")
{
let supplyId = ent.getMetadata(PlayerID, "supply");
if (supplyId)
if (gameState.ai.playedTurn % 10 == 0)
{
let nearby = this.base.dropsiteSupplies[gatherType].nearby;
if (!nearby.some(sup => sup.id == supplyId))
// Check from time to time that UnitAI does not send us to an inaccessible dropsite
let dropsite = gameState.getEntityById(ent.unitAIOrderData()[0].target);
if (dropsite && dropsite.position() && this.entAccess != PETRA.getLandAccess(gameState, dropsite))
PETRA.returnResources(gameState, this.ent);
}
// If gathering a sparse resource, we may have been sent to a faraway resource if the one nearby was full.
// Let's check if it is still the case. If so, we reset its metadata supplyId so that the unit will be
// reordered to gather after having returned the resources (when comparing its supplyId with the UnitAI one).
let gatherType = ent.getMetadata(PlayerID, "gather-type");
let influenceGroup = Resources.GetResource(gatherType).aiAnalysisInfluenceGroup;
if (influenceGroup && influenceGroup == "sparse")
{
let supplyId = ent.getMetadata(PlayerID, "supply");
if (supplyId)
{
if (nearby.length)
ent.setMetadata(PlayerID, "supply", undefined);
else
let nearby = this.base.dropsiteSupplies[gatherType].nearby;
if (!nearby.some(sup => sup.id == supplyId))
{
let medium = this.base.dropsiteSupplies[gatherType].medium;
if (!medium.some(sup => sup.id == supplyId) && medium.length)
if (nearby.length)
ent.setMetadata(PlayerID, "supply", undefined);
else
{
let medium = this.base.dropsiteSupplies[gatherType].medium;
if (!medium.some(sup => sup.id == supplyId) && medium.length)
ent.setMetadata(PlayerID, "supply", undefined);
}
}
}
}
@ -379,13 +379,13 @@ PETRA.Worker.prototype.update = function(gameState, ent)
}
else // Perform some sanity checks
{
if (unitAIStateOrder == "GATHER" || unitAIStateOrder == "RETURNRESOURCE")
if (unitAIStateOrder == "GATHER")
{
// we may have drifted towards ennemy territory during the hunt, if yes go home
let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(ent.position());
if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // player is its own ally
this.startHunting(gameState);
else if (unitAIState == "INDIVIDUAL.RETURNRESOURCE.APPROACHING")
else if (unitAIState == "INDIVIDUAL.GATHER.RETURNINGRESOURCE.APPROACHING")
{
// Check that UnitAI does not send us to an inaccessible dropsite
let dropsite = gameState.getEntityById(ent.unitAIOrderData()[0].target);