Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/worker.js
Show First 20 Lines • Show All 247 Lines • ▼ Show 20 Lines | else if (unitAIStateOrder == "GATHER") | ||||
this.startGathering(gameState); | this.startGathering(gameState); | ||||
} | } | ||||
else | else | ||||
ent.setMetadata(PlayerID, "supply", supplyId); | ent.setMetadata(PlayerID, "supply", supplyId); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | if (unitAIState == "INDIVIDUAL.GATHER.RETURNINGRESOURCE.APPROACHING") | ||||
else if (unitAIState == "INDIVIDUAL.RETURNRESOURCE.APPROACHING") | |||||
{ | { | ||||
if (gameState.ai.playedTurn % 10 == 0) | if (gameState.ai.playedTurn % 10 == 0) | ||||
{ | { | ||||
// Check from time to time that UnitAI does not send us to an inaccessible dropsite | // Check from time to time that UnitAI does not send us to an inaccessible dropsite | ||||
let dropsite = gameState.getEntityById(ent.unitAIOrderData()[0].target); | let dropsite = gameState.getEntityById(ent.unitAIOrderData()[0].target); | ||||
if (dropsite && dropsite.position() && this.entAccess != PETRA.getLandAccess(gameState, dropsite)) | if (dropsite && dropsite.position() && this.entAccess != PETRA.getLandAccess(gameState, dropsite)) | ||||
PETRA.returnResources(gameState, this.ent); | 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. | // 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 | // 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). | // reordered to gather after having returned the resources (when comparing its supplyId with the UnitAI one). | ||||
let gatherType = ent.getMetadata(PlayerID, "gather-type"); | let gatherType = ent.getMetadata(PlayerID, "gather-type"); | ||||
let influenceGroup = Resources.GetResource(gatherType).aiAnalysisInfluenceGroup; | let influenceGroup = Resources.GetResource(gatherType).aiAnalysisInfluenceGroup; | ||||
if (influenceGroup && influenceGroup == "sparse") | if (influenceGroup && influenceGroup == "sparse") | ||||
{ | { | ||||
let supplyId = ent.getMetadata(PlayerID, "supply"); | let supplyId = ent.getMetadata(PlayerID, "supply"); | ||||
if (supplyId) | if (supplyId) | ||||
{ | { | ||||
let nearby = this.base.dropsiteSupplies[gatherType].nearby; | let nearby = this.base.dropsiteSupplies[gatherType].nearby; | ||||
if (!nearby.some(sup => sup.id == supplyId)) | if (!nearby.some(sup => sup.id == supplyId)) | ||||
{ | { | ||||
if (nearby.length) | if (nearby.length) | ||||
ent.setMetadata(PlayerID, "supply", undefined); | ent.setMetadata(PlayerID, "supply", undefined); | ||||
else | else | ||||
{ | { | ||||
let medium = this.base.dropsiteSupplies[gatherType].medium; | let medium = this.base.dropsiteSupplies[gatherType].medium; | ||||
if (!medium.some(sup => sup.id == supplyId) && medium.length) | if (!medium.some(sup => sup.id == supplyId) && medium.length) | ||||
ent.setMetadata(PlayerID, "supply", undefined); | ent.setMetadata(PlayerID, "supply", undefined); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | |||||
else if (subrole == "builder") | else if (subrole == "builder") | ||||
{ | { | ||||
if (unitAIStateOrder == "REPAIR") | if (unitAIStateOrder == "REPAIR") | ||||
{ | { | ||||
// Update our target in case UnitAI sent us to a different foundation because of autocontinue | // Update our target in case UnitAI sent us to a different foundation because of autocontinue | ||||
// and abandon it if UnitAI has sent us to build a field (as we build them only when needed) | // and abandon it if UnitAI has sent us to build a field (as we build them only when needed) | ||||
if (ent.unitAIOrderData()[0] && ent.unitAIOrderData()[0].target && | if (ent.unitAIOrderData()[0] && ent.unitAIOrderData()[0].target && | ||||
ent.getMetadata(PlayerID, "target-foundation") != ent.unitAIOrderData()[0].target) | ent.getMetadata(PlayerID, "target-foundation") != ent.unitAIOrderData()[0].target) | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | if (ent.isIdle() && (!lastHuntSearch || gameState.ai.elapsedTime - lastHuntSearch > 20)) | ||||
} | } | ||||
} | } | ||||
if (nowhereToHunt) | if (nowhereToHunt) | ||||
ent.setMetadata(PlayerID, "lastHuntSearch", gameState.ai.elapsedTime); | ent.setMetadata(PlayerID, "lastHuntSearch", gameState.ai.elapsedTime); | ||||
} | } | ||||
} | } | ||||
else // Perform some sanity checks | 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 | // we may have drifted towards ennemy territory during the hunt, if yes go home | ||||
let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(ent.position()); | let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(ent.position()); | ||||
if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // player is its own ally | if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // player is its own ally | ||||
this.startHunting(gameState); | 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 | // Check that UnitAI does not send us to an inaccessible dropsite | ||||
let dropsite = gameState.getEntityById(ent.unitAIOrderData()[0].target); | let dropsite = gameState.getEntityById(ent.unitAIOrderData()[0].target); | ||||
if (dropsite && dropsite.position() && this.entAccess != PETRA.getLandAccess(gameState, dropsite)) | if (dropsite && dropsite.position() && this.entAccess != PETRA.getLandAccess(gameState, dropsite)) | ||||
PETRA.returnResources(gameState, ent); | PETRA.returnResources(gameState, ent); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 707 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator