Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/worker.js
Show First 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | if (unitAIState == "INDIVIDUAL.COMBAT.APPROACHING" && ent.unitAIOrderData().length) | ||||
if (gameState.isPlayerEnemy(territoryOwner)) | if (gameState.isPlayerEnemy(territoryOwner)) | ||||
{ | { | ||||
if (this.retryWorking(gameState, subrole)) | if (this.retryWorking(gameState, subrole)) | ||||
return; | return; | ||||
ent.stopMoving(); | ent.stopMoving(); | ||||
} | } | ||||
else if (!gameState.isPlayerAlly(territoryOwner)) | else if (!gameState.isPlayerAlly(territoryOwner)) | ||||
{ | { | ||||
let distanceSquare = ent.hasClass("Cavalry") ? 90000 : 30000; | let distanceSquare = ent.hasClass("FastMoving") ? 90000 : 30000; | ||||
let targetAccess = PETRA.getLandAccess(gameState, target); | let targetAccess = PETRA.getLandAccess(gameState, target); | ||||
let foodDropsites = gameState.playerData.hasSharedDropsites ? | let foodDropsites = gameState.playerData.hasSharedDropsites ? | ||||
gameState.getAnyDropsites("food") : gameState.getOwnDropsites("food"); | gameState.getAnyDropsites("food") : gameState.getOwnDropsites("food"); | ||||
let hasFoodDropsiteWithinDistance = false; | let hasFoodDropsiteWithinDistance = false; | ||||
for (let dropsite of foodDropsites.values()) | for (let dropsite of foodDropsites.values()) | ||||
{ | { | ||||
if (!dropsite.position()) | if (!dropsite.position()) | ||||
continue; | continue; | ||||
▲ Show 20 Lines • Show All 615 Lines • ▼ Show 20 Lines | PETRA.Worker.prototype.startHunting = function(gameState, position) | ||||
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; | ||||
let isCavalry = this.ent.hasClass("Cavalry"); | let isFastMoving = this.ent.hasClass("FastMoving"); | ||||
let isRanged = this.ent.hasClass("Ranged"); | let isRanged = this.ent.hasClass("Ranged"); | ||||
let entPosition = position ? position : this.ent.position(); | let entPosition = position ? position : this.ent.position(); | ||||
let foodDropsites = gameState.playerData.hasSharedDropsites ? | let foodDropsites = gameState.playerData.hasSharedDropsites ? | ||||
gameState.getAnyDropsites("food") : gameState.getOwnDropsites("food"); | gameState.getAnyDropsites("food") : gameState.getOwnDropsites("food"); | ||||
let hasFoodDropsiteWithinDistance = function(supplyPosition, supplyAccess, distSquare) | let hasFoodDropsiteWithinDistance = function(supplyPosition, supplyAccess, distSquare) | ||||
{ | { | ||||
for (let dropsite of foodDropsites.values()) | for (let dropsite of foodDropsites.values()) | ||||
Show All 23 Lines | if (inaccessibleTime && gameState.ai.elapsedTime < inaccessibleTime) | ||||
continue; | continue; | ||||
let supplyType = supply.get("ResourceSupply/Type"); | let supplyType = supply.get("ResourceSupply/Type"); | ||||
if (!gatherRates[supplyType]) | if (!gatherRates[supplyType]) | ||||
continue; | continue; | ||||
if (PETRA.IsSupplyFull(gameState, supply)) | if (PETRA.IsSupplyFull(gameState, supply)) | ||||
continue; | continue; | ||||
// check if available resource is worth one additionnal gatherer (except for farms) | // Check if available resource is worth one additionnal gatherer (except for farms). | ||||
let nbGatherers = supply.resourceSupplyNumGatherers() + gameState.ai.HQ.GetTCGatherer(supply.id()); | let nbGatherers = supply.resourceSupplyNumGatherers() + gameState.ai.HQ.GetTCGatherer(supply.id()); | ||||
if (nbGatherers > 0 && supply.resourceSupplyAmount()/(1+nbGatherers) < 30) | if (nbGatherers > 0 && supply.resourceSupplyAmount()/(1+nbGatherers) < 30) | ||||
continue; | continue; | ||||
let canFlee = !supply.hasClass("Domestic") && supply.templateName().indexOf("resource|") == -1; | let canFlee = !supply.hasClass("Domestic") && supply.templateName().indexOf("resource|") == -1; | ||||
// Only cavalry and range units should hunt fleeing animals | // Only FastMoving and Ranged units should hunt fleeing animals. | ||||
if (canFlee && !isCavalry && !isRanged) | if (canFlee && !isFastMoving && !isRanged) | ||||
continue; | continue; | ||||
let supplyAccess = PETRA.getLandAccess(gameState, supply); | let supplyAccess = PETRA.getLandAccess(gameState, supply); | ||||
if (supplyAccess != this.entAccess) | if (supplyAccess != this.entAccess) | ||||
continue; | continue; | ||||
// measure the distance to the resource | // measure the distance to the resource. | ||||
let dist = API3.SquareVectorDistance(entPosition, supply.position()); | let dist = API3.SquareVectorDistance(entPosition, supply.position()); | ||||
if (dist > nearestSupplyDist) | if (dist > nearestSupplyDist) | ||||
continue; | continue; | ||||
// Only cavalry should hunt faraway | // Only FastMoving should hunt faraway. | ||||
if (!isCavalry && dist > 25000) | if (!isFastMoving && dist > 25000) | ||||
continue; | continue; | ||||
// Avoid ennemy territory | // Avoid enemy territory. | ||||
let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(supply.position()); | let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(supply.position()); | ||||
if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // player is its own ally | if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // Player is its own ally. | ||||
continue; | continue; | ||||
// And if in ally territory, don't hunt this ally's cattle | // And if in ally territory, don't hunt this ally's cattle. | ||||
if (territoryOwner != 0 && territoryOwner != PlayerID && supply.owner() == territoryOwner) | if (territoryOwner != 0 && territoryOwner != PlayerID && supply.owner() == territoryOwner) | ||||
continue; | continue; | ||||
// Only cavalry should hunt far from dropsite (specially for non domestic animals which flee) | // Only FastMoving should hunt far from dropsite (specially for non-Domestic animals which flee). | ||||
if (!isCavalry && canFlee && territoryOwner == 0) | if (!isFastMoving && canFlee && territoryOwner == 0) | ||||
continue; | continue; | ||||
let distanceSquare = isCavalry ? 35000 : (canFlee ? 7000 : 12000); | let distanceSquare = isFastMoving ? 35000 : (canFlee ? 7000 : 12000); | ||||
if (!hasFoodDropsiteWithinDistance(supply.position(), supplyAccess, distanceSquare)) | if (!hasFoodDropsiteWithinDistance(supply.position(), supplyAccess, distanceSquare)) | ||||
continue; | continue; | ||||
nearestSupplyDist = dist; | nearestSupplyDist = dist; | ||||
nearestSupply = supply; | nearestSupply = supply; | ||||
} | } | ||||
if (nearestSupply) | if (nearestSupply) | ||||
▲ Show 20 Lines • Show All 278 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator