Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/worker.js
Show First 20 Lines • Show All 320 Lines • ▼ Show 20 Lines | m.Worker.prototype.retryGathering = function(gameState, subrole) | ||||
} | } | ||||
}; | }; | ||||
m.Worker.prototype.startGathering = function(gameState) | m.Worker.prototype.startGathering = function(gameState) | ||||
{ | { | ||||
let access = gameState.ai.accessibility.getAccessValue(this.ent.position()); | let access = gameState.ai.accessibility.getAccessValue(this.ent.position()); | ||||
// First look for possible treasure if any | // First look for possible treasure if any | ||||
if (this.gatherTreasure(gameState)) | if (m.gatherTreasure(gameState, this.ent)) | ||||
return true; | return true; | ||||
let resource = this.ent.getMetadata(PlayerID, "gather-type"); | let resource = this.ent.getMetadata(PlayerID, "gather-type"); | ||||
// If we are gathering food, try to hunt first | // If we are gathering food, try to hunt first | ||||
if (resource === "food" && this.startHunting(gameState)) | if (resource === "food" && this.startHunting(gameState)) | ||||
return true; | return true; | ||||
▲ Show 20 Lines • Show All 266 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* if position is given, we only check if we could hunt from this position but do nothing | * if position is given, we only check if we could hunt from this position but do nothing | ||||
* otherwise the position of the entity is taken, and if something is found, we directly start the hunt | * otherwise the position of the entity is taken, and if something is found, we directly start the hunt | ||||
*/ | */ | ||||
m.Worker.prototype.startHunting = function(gameState, position) | m.Worker.prototype.startHunting = function(gameState, position) | ||||
{ | { | ||||
// First look for possible treasure if any | // First look for possible treasure if any | ||||
if (!position && this.gatherTreasure(gameState)) | if (!position && m.gatherTreasure(gameState, this.ent)) | ||||
return true; | return true; | ||||
let resources = gameState.getHuntableSupplies(); | let resources = gameState.getHuntableSupplies(); | ||||
if (!resources.hasEntities()) | if (!resources.hasEntities()) | ||||
return false; | return false; | ||||
let nearestSupplyDist = Math.min(); | let nearestSupplyDist = Math.min(); | ||||
let nearestSupply; | let nearestSupply; | ||||
▲ Show 20 Lines • Show All 228 Lines • ▼ Show 20 Lines | if (dist > bestFarmDist) | ||||
continue; | continue; | ||||
bestFarmEnt = found; | bestFarmEnt = found; | ||||
bestFarmDist = dist; | bestFarmDist = dist; | ||||
} | } | ||||
return bestFarmEnt; | return bestFarmEnt; | ||||
}; | }; | ||||
/** | /** | ||||
* Look for some treasure to gather | |||||
*/ | |||||
m.Worker.prototype.gatherTreasure = function(gameState) | |||||
{ | |||||
let rates = this.ent.resourceGatherRates(); | |||||
if (!rates || !rates.treasure || rates.treasure <= 0) | |||||
return false; | |||||
let treasureFound; | |||||
let distmin = Math.min(); | |||||
let access = gameState.ai.accessibility.getAccessValue(this.ent.position()); | |||||
for (let treasure of gameState.ai.HQ.treasures.values()) | |||||
{ | |||||
if (m.IsSupplyFull(gameState, treasure)) | |||||
continue; | |||||
// let some time for the previous gatherer to reach the treasure befor trying again | |||||
let lastGathered = treasure.getMetadata(PlayerID, "lastGathered"); | |||||
if (lastGathered && gameState.ai.elapsedTime - lastGathered < 20) | |||||
continue; | |||||
if (access !== m.getLandAccess(gameState, treasure)) | |||||
continue; | |||||
let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(treasure.position()); | |||||
if (territoryOwner !== 0 && !gameState.isPlayerAlly(territoryOwner)) | |||||
continue; | |||||
let dist = API3.SquareVectorDistance(this.ent.position(), treasure.position()); | |||||
if (territoryOwner !== PlayerID && dist > 14000) // AI has no LOS, so restrict it a bit | |||||
continue; | |||||
if (dist > distmin) | |||||
continue; | |||||
distmin = dist; | |||||
treasureFound = treasure; | |||||
} | |||||
if (!treasureFound) | |||||
return false; | |||||
treasureFound.setMetadata(PlayerID, "lastGathered", gameState.ai.elapsedTime); | |||||
this.ent.gather(treasureFound); | |||||
gameState.ai.HQ.AddTCGatherer(treasureFound.id()); | |||||
this.ent.setMetadata(PlayerID, "supply", treasureFound.id()); | |||||
return true; | |||||
}; | |||||
/** | |||||
* Workers elephant should move away from the buildings they've built to avoid being trapped in between constructions | * Workers elephant should move away from the buildings they've built to avoid being trapped in between constructions | ||||
* For the time being, we move towards the nearest gatherer (providing him a dropsite) | * For the time being, we move towards the nearest gatherer (providing him a dropsite) | ||||
*/ | */ | ||||
m.Worker.prototype.moveAway = function(gameState) | m.Worker.prototype.moveAway = function(gameState) | ||||
{ | { | ||||
let gatherers = this.base.workersBySubrole(gameState, "gatherer"); | let gatherers = this.base.workersBySubrole(gameState, "gatherer"); | ||||
let pos = this.ent.position(); | let pos = this.ent.position(); | ||||
let dist = Math.min(); | let dist = Math.min(); | ||||
▲ Show 20 Lines • Show All 90 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator