Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/worker.js
/** | /** | ||||
* This class makes a worker do as instructed by the economy manager | * This class makes a worker do as instructed by the economy manager | ||||
*/ | */ | ||||
PETRA.Worker = function(base) | PETRA.Worker = function(base) | ||||
{ | { | ||||
this.ent = undefined; | this.ent = undefined; | ||||
this.base = base; | this.base = base; | ||||
this.baseID = base.ID; | this.baseID = base.ID; | ||||
}; | }; | ||||
PETRA.Worker.ROLE_EMPTY = undefined; | |||||
PETRA.Worker.ROLE_ATTACK = "attack"; | |||||
PETRA.Worker.ROLE_TRADER = "trader"; | |||||
PETRA.Worker.ROLE_SWITCH_TO_TRADER = "switchToTrader"; | |||||
PETRA.Worker.ROLE_WORKER = "worker"; | |||||
PETRA.Worker.ROLE_CRITICAL_ENT_GUARD = "criticalEntGuard"; | |||||
PETRA.Worker.ROLE_CRITICAL_ENT_HEALER = "criticalEntHealer"; | |||||
PETRA.Worker.SUBROLE_EMPTY = undefined; | |||||
JCWasmx86: I added the _EMPTY constants so it looks a bit nicer/uniform | |||||
PETRA.Worker.SUBROLE_DEFENDER = "defender"; | |||||
PETRA.Worker.SUBROLE_IDLE = "idle"; | |||||
PETRA.Worker.SUBROLE_BUILDER = "builder"; | |||||
PETRA.Worker.SUBROLE_COMPLETING = "completing"; | |||||
PETRA.Worker.SUBROLE_WALKING = "walking"; | |||||
PETRA.Worker.SUBROLE_ATTACKING = "attacking"; | |||||
PETRA.Worker.SUBROLE_GATHERER = "gatherer"; | |||||
PETRA.Worker.SUBROLE_HUNTER = "hunter"; | |||||
PETRA.Worker.SUBROLE_FISHER = "fisher"; | |||||
PETRA.Worker.SUBROLE_GARRISONING = "garrisoning"; | |||||
PETRA.Worker.prototype.update = function(gameState, ent) | PETRA.Worker.prototype.update = function(gameState, ent) | ||||
{ | { | ||||
if (!ent.position() || ent.getMetadata(PlayerID, "plan") == -2 || ent.getMetadata(PlayerID, "plan") == -3) | if (!ent.position() || ent.getMetadata(PlayerID, "plan") == -2 || ent.getMetadata(PlayerID, "plan") == -3) | ||||
return; | return; | ||||
let subrole = ent.getMetadata(PlayerID, "subrole"); | let subrole = ent.getMetadata(PlayerID, "subrole"); | ||||
// If we are waiting for a transport or we are sailing, just wait | // If we are waiting for a transport or we are sailing, just wait | ||||
if (ent.getMetadata(PlayerID, "transport") !== undefined) | if (ent.getMetadata(PlayerID, "transport") !== undefined) | ||||
{ | { | ||||
// Except if builder with their foundation destroyed, in which case cancel the transport if not yet on board | // Except if builder with their foundation destroyed, in which case cancel the transport if not yet on board | ||||
if (subrole == "builder" && ent.getMetadata(PlayerID, "target-foundation") !== undefined) | if (subrole == PETRA.Worker.SUBROLE_BUILDER && ent.getMetadata(PlayerID, "target-foundation") !== undefined) | ||||
{ | { | ||||
let plan = gameState.ai.HQ.navalManager.getPlan(ent.getMetadata(PlayerID, "transport")); | let plan = gameState.ai.HQ.navalManager.getPlan(ent.getMetadata(PlayerID, "transport")); | ||||
let target = gameState.getEntityById(ent.getMetadata(PlayerID, "target-foundation")); | let target = gameState.getEntityById(ent.getMetadata(PlayerID, "target-foundation")); | ||||
if (!target && plan && plan.state == "boarding" && ent.position()) | if (!target && plan && plan.state == PETRA.TransportPlan.BOARDING && ent.position()) | ||||
plan.removeUnit(gameState, ent); | plan.removeUnit(gameState, ent); | ||||
} | } | ||||
// and gatherer if there are no more dropsite accessible in the base the ent is going to | // and gatherer if there are no more dropsite accessible in the base the ent is going to | ||||
if (subrole == "gatherer" || subrole == "hunter") | if (subrole == PETRA.Worker.SUBROLE_GATHERER || subrole == PETRA.Worker.SUBROLE_HUNTER) | ||||
{ | { | ||||
let plan = gameState.ai.HQ.navalManager.getPlan(ent.getMetadata(PlayerID, "transport")); | let plan = gameState.ai.HQ.navalManager.getPlan(ent.getMetadata(PlayerID, "transport")); | ||||
if (plan.state == "boarding" && ent.position()) | if (plan.state == PETRA.TransportPlan.BOARDING && ent.position()) | ||||
{ | { | ||||
let hasDropsite = false; | let hasDropsite = false; | ||||
let gatherType = ent.getMetadata(PlayerID, "gather-type") || "food"; | let gatherType = ent.getMetadata(PlayerID, "gather-type") || "food"; | ||||
for (let structure of gameState.getOwnStructures().values()) | for (let structure of gameState.getOwnStructures().values()) | ||||
{ | { | ||||
if (PETRA.getLandAccess(gameState, structure) != plan.endIndex) | if (PETRA.getLandAccess(gameState, structure) != plan.endIndex) | ||||
continue; | continue; | ||||
let resourceDropsiteTypes = PETRA.getBuiltEntity(gameState, structure).resourceDropsiteTypes(); | let resourceDropsiteTypes = PETRA.getBuiltEntity(gameState, structure).resourceDropsiteTypes(); | ||||
Show All 25 Lines | PETRA.Worker.prototype.update = function(gameState, ent) | ||||
this.entAccess = PETRA.getLandAccess(gameState, ent); | this.entAccess = PETRA.getLandAccess(gameState, ent); | ||||
// Base for unassigned entities has no accessIndex, so take the one from the entity. | // Base for unassigned entities has no accessIndex, so take the one from the entity. | ||||
if (this.baseID == gameState.ai.HQ.basesManager.baselessBase().ID) | if (this.baseID == gameState.ai.HQ.basesManager.baselessBase().ID) | ||||
this.baseAccess = this.entAccess; | this.baseAccess = this.entAccess; | ||||
else | else | ||||
this.baseAccess = this.base.accessIndex; | this.baseAccess = this.base.accessIndex; | ||||
if (!subrole) // subrole may-be undefined after a transport, garrisoning, army, ... | if (!subrole) // subrole may-be undefined after a transport, garrisoning, army, ... | ||||
Not Done Inline ActionsI am wondering whether we shouldn't just set the subroles to IDLE when e.g. ungarrisoning? Freagarach: I am wondering whether we shouldn't just set the subroles to IDLE when e.g. ungarrisoning? | |||||
{ | { | ||||
ent.setMetadata(PlayerID, "subrole", "idle"); | ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_IDLE); | ||||
this.base.reassignIdleWorkers(gameState, [ent]); | this.base.reassignIdleWorkers(gameState, [ent]); | ||||
this.update(gameState, ent); | this.update(gameState, ent); | ||||
return; | return; | ||||
} | } | ||||
this.ent = ent; | this.ent = ent; | ||||
let unitAIState = ent.unitAIState(); | let unitAIState = ent.unitAIState(); | ||||
if ((subrole == "hunter" || subrole == "gatherer") && | if ((subrole == PETRA.Worker.SUBROLE_HUNTER || subrole == PETRA.Worker.SUBROLE_GATHERER) && | ||||
(unitAIState == "INDIVIDUAL.GATHER.GATHERING" || unitAIState == "INDIVIDUAL.GATHER.APPROACHING" || | (unitAIState == "INDIVIDUAL.GATHER.GATHERING" || unitAIState == "INDIVIDUAL.GATHER.APPROACHING" || | ||||
unitAIState == "INDIVIDUAL.COMBAT.APPROACHING")) | unitAIState == "INDIVIDUAL.COMBAT.APPROACHING")) | ||||
{ | { | ||||
if (this.isInaccessibleSupply(gameState)) | if (this.isInaccessibleSupply(gameState)) | ||||
{ | { | ||||
if (this.retryWorking(gameState, subrole)) | if (this.retryWorking(gameState, subrole)) | ||||
return; | return; | ||||
ent.stopMoving(); | ent.stopMoving(); | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | PETRA.Worker.prototype.update = function(gameState, ent) | ||||
} | } | ||||
let unitAIStateOrder = unitAIState.split(".")[1]; | let unitAIStateOrder = unitAIState.split(".")[1]; | ||||
// If we're fighting or hunting, let's not start gathering except if inaccessible target | // If we're fighting or hunting, let's not start gathering except if inaccessible target | ||||
// but for fishers where UnitAI must have made us target a moving whale. | // but for fishers where UnitAI must have made us target a moving whale. | ||||
// Also, if we are attacking, do not capture | // Also, if we are attacking, do not capture | ||||
if (unitAIStateOrder == "COMBAT") | if (unitAIStateOrder == "COMBAT") | ||||
{ | { | ||||
if (subrole == "fisher") | if (subrole == PETRA.Worker.SUBROLE_FISHER) | ||||
this.startFishing(gameState); | this.startFishing(gameState); | ||||
else if (unitAIState == "INDIVIDUAL.COMBAT.APPROACHING" && ent.unitAIOrderData().length && | else if (unitAIState == "INDIVIDUAL.COMBAT.APPROACHING" && ent.unitAIOrderData().length && | ||||
!ent.getMetadata(PlayerID, "PartOfArmy")) | !ent.getMetadata(PlayerID, "PartOfArmy")) | ||||
{ | { | ||||
let orderData = ent.unitAIOrderData()[0]; | let orderData = ent.unitAIOrderData()[0]; | ||||
if (orderData && orderData.target) | if (orderData && orderData.target) | ||||
{ | { | ||||
let target = gameState.getEntityById(orderData.target); | let target = gameState.getEntityById(orderData.target); | ||||
Show All 20 Lines | if (unitAIStateOrder == "COMBAT") | ||||
} | } | ||||
return; | return; | ||||
} | } | ||||
// Okay so we have a few tasks. | // Okay so we have a few tasks. | ||||
// If we're gathering, we'll check that we haven't run idle. | // If we're gathering, we'll check that we haven't run idle. | ||||
// And we'll also check that we're gathering a resource we want to gather. | // And we'll also check that we're gathering a resource we want to gather. | ||||
if (subrole == "gatherer") | if (subrole == PETRA.Worker.SUBROLE_GATHERER) | ||||
{ | { | ||||
if (ent.isIdle()) | if (ent.isIdle()) | ||||
{ | { | ||||
// if we aren't storing resources or it's the same type as what we're about to gather, | // if we aren't storing resources or it's the same type as what we're about to gather, | ||||
// let's just pick a new resource. | // let's just pick a new resource. | ||||
// TODO if we already carry the max we can -> returnresources | // TODO if we already carry the max we can -> returnresources | ||||
if (!ent.resourceCarrying() || !ent.resourceCarrying().length || | if (!ent.resourceCarrying() || !ent.resourceCarrying().length || | ||||
ent.resourceCarrying()[0].type == ent.getMetadata(PlayerID, "gather-type")) | ent.resourceCarrying()[0].type == ent.getMetadata(PlayerID, "gather-type")) | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | else if (unitAIStateOrder == "GATHER") | ||||
ent.setMetadata(PlayerID, "supply", undefined); | ent.setMetadata(PlayerID, "supply", undefined); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
else if (subrole == "builder") | else if (subrole == PETRA.Worker.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) | ||||
{ | { | ||||
let targetId = ent.unitAIOrderData()[0].target; | let targetId = ent.unitAIOrderData()[0].target; | ||||
let target = gameState.getEntityById(targetId); | let target = gameState.getEntityById(targetId); | ||||
if (target && !target.hasClass("Field")) | if (target && !target.hasClass("Field")) | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "target-foundation", targetId); | ent.setMetadata(PlayerID, "target-foundation", targetId); | ||||
return; | return; | ||||
} | } | ||||
ent.setMetadata(PlayerID, "target-foundation", undefined); | ent.setMetadata(PlayerID, "target-foundation", undefined); | ||||
ent.setMetadata(PlayerID, "subrole", "idle"); | ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_IDLE); | ||||
ent.stopMoving(); | ent.stopMoving(); | ||||
if (this.baseID != gameState.ai.HQ.basesManager.baselessBase().ID) | if (this.baseID != gameState.ai.HQ.basesManager.baselessBase().ID) | ||||
{ | { | ||||
// reassign it to something useful | // reassign it to something useful | ||||
this.base.reassignIdleWorkers(gameState, [ent]); | this.base.reassignIdleWorkers(gameState, [ent]); | ||||
this.update(gameState, ent); | this.update(gameState, ent); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
// Otherwise check that the target still exists (useful in REPAIR.APPROACHING) | // Otherwise check that the target still exists (useful in REPAIR.APPROACHING) | ||||
let targetId = ent.getMetadata(PlayerID, "target-foundation"); | let targetId = ent.getMetadata(PlayerID, "target-foundation"); | ||||
if (targetId && gameState.getEntityById(targetId)) | if (targetId && gameState.getEntityById(targetId)) | ||||
return; | return; | ||||
ent.stopMoving(); | ent.stopMoving(); | ||||
} | } | ||||
// okay so apparently we aren't working. | // okay so apparently we aren't working. | ||||
// Unless we've been explicitely told to keep our role, make us idle. | // Unless we've been explicitely told to keep our role, make us idle. | ||||
let target = gameState.getEntityById(ent.getMetadata(PlayerID, "target-foundation")); | let target = gameState.getEntityById(ent.getMetadata(PlayerID, "target-foundation")); | ||||
if (!target || target.foundationProgress() === undefined && target.needsRepair() === false) | if (!target || target.foundationProgress() === undefined && target.needsRepair() === false) | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "subrole", "idle"); | ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_IDLE); | ||||
ent.setMetadata(PlayerID, "target-foundation", undefined); | ent.setMetadata(PlayerID, "target-foundation", undefined); | ||||
if (this.baseID != gameState.ai.HQ.basesManager.baselessBase().ID) | if (this.baseID != gameState.ai.HQ.basesManager.baselessBase().ID) | ||||
{ | { | ||||
// reassign it to something useful | // reassign it to something useful | ||||
this.base.reassignIdleWorkers(gameState, [ent]); | this.base.reassignIdleWorkers(gameState, [ent]); | ||||
this.update(gameState, ent); | this.update(gameState, ent); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
let goalAccess = PETRA.getLandAccess(gameState, target); | let goalAccess = PETRA.getLandAccess(gameState, target); | ||||
let queued = PETRA.returnResources(gameState, ent); | let queued = PETRA.returnResources(gameState, ent); | ||||
if (this.entAccess == goalAccess) | if (this.entAccess == goalAccess) | ||||
ent.repair(target, target.hasClass("House"), queued); // autocontinue=true for houses | ent.repair(target, target.hasClass("House"), queued); // autocontinue=true for houses | ||||
else | else | ||||
gameState.ai.HQ.navalManager.requireTransport(gameState, ent, this.entAccess, goalAccess, target.position()); | gameState.ai.HQ.navalManager.requireTransport(gameState, ent, this.entAccess, goalAccess, target.position()); | ||||
} | } | ||||
} | } | ||||
else if (subrole == "hunter") | else if (subrole == PETRA.Worker.SUBROLE_HUNTER) | ||||
{ | { | ||||
let lastHuntSearch = ent.getMetadata(PlayerID, "lastHuntSearch"); | let lastHuntSearch = ent.getMetadata(PlayerID, "lastHuntSearch"); | ||||
if (ent.isIdle() && (!lastHuntSearch || gameState.ai.elapsedTime - lastHuntSearch > 20)) | if (ent.isIdle() && (!lastHuntSearch || gameState.ai.elapsedTime - lastHuntSearch > 20)) | ||||
{ | { | ||||
if (!this.startHunting(gameState)) | if (!this.startHunting(gameState)) | ||||
{ | { | ||||
// nothing to hunt around. Try another region if any | // nothing to hunt around. Try another region if any | ||||
let nowhereToHunt = true; | let nowhereToHunt = true; | ||||
Show All 30 Lines | else // Perform some sanity checks | ||||
// 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); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
else if (subrole == "fisher") | else if (subrole == PETRA.Worker.SUBROLE_FISHER) | ||||
{ | { | ||||
if (ent.isIdle()) | if (ent.isIdle()) | ||||
this.startFishing(gameState); | this.startFishing(gameState); | ||||
else // if we have drifted towards ennemy territory during the fishing, go home | else // if we have drifted towards ennemy territory during the fishing, 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.startFishing(gameState); | this.startFishing(gameState); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
PETRA.Worker.prototype.retryWorking = function(gameState, subrole) | PETRA.Worker.prototype.retryWorking = function(gameState, subrole) | ||||
{ | { | ||||
switch (subrole) | switch (subrole) | ||||
{ | { | ||||
case "gatherer": | case PETRA.Worker.SUBROLE_GATHERER: | ||||
return this.startGathering(gameState); | return this.startGathering(gameState); | ||||
case "hunter": | case PETRA.Worker.SUBROLE_HUNTER: | ||||
return this.startHunting(gameState); | return this.startHunting(gameState); | ||||
case "fisher": | case PETRA.Worker.SUBROLE_FISHER: | ||||
return this.startFishing(gameState); | return this.startFishing(gameState); | ||||
case "builder": | case PETRA.Worker.SUBROLE_BUILDER: | ||||
return this.startBuilding(gameState); | return this.startBuilding(gameState); | ||||
default: | default: | ||||
return false; | return false; | ||||
} | } | ||||
}; | }; | ||||
PETRA.Worker.prototype.startBuilding = function(gameState) | PETRA.Worker.prototype.startBuilding = function(gameState) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | let shouldBuild = this.ent.isBuilder() && foundations.some(function(foundation) { | ||||
if (!foundation || PETRA.getLandAccess(gameState, foundation) != this.entAccess) | if (!foundation || PETRA.getLandAccess(gameState, foundation) != this.entAccess) | ||||
return false; | return false; | ||||
let structure = gameState.getBuiltTemplate(foundation.templateName()); | let structure = gameState.getBuiltTemplate(foundation.templateName()); | ||||
if (structure.resourceDropsiteTypes() && structure.resourceDropsiteTypes().indexOf(resource) != -1) | if (structure.resourceDropsiteTypes() && structure.resourceDropsiteTypes().indexOf(resource) != -1) | ||||
{ | { | ||||
if (foundation.getMetadata(PlayerID, "base") != this.baseID) | if (foundation.getMetadata(PlayerID, "base") != this.baseID) | ||||
this.ent.setMetadata(PlayerID, "base", foundation.getMetadata(PlayerID, "base")); | this.ent.setMetadata(PlayerID, "base", foundation.getMetadata(PlayerID, "base")); | ||||
this.ent.setMetadata(PlayerID, "target-foundation", foundation.id()); | this.ent.setMetadata(PlayerID, "target-foundation", foundation.id()); | ||||
this.ent.setMetadata(PlayerID, "subrole", "builder"); | this.ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_BUILDER); | ||||
this.ent.repair(foundation); | this.ent.repair(foundation); | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}, this); | }, this); | ||||
if (shouldBuild) | if (shouldBuild) | ||||
return true; | return true; | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | shouldBuild = this.ent.isBuilder() && foundations.some(function(foundation) { | ||||
if (structure.resourceDropsiteTypes() && structure.resourceDropsiteTypes().indexOf(resource) != -1) | if (structure.resourceDropsiteTypes() && structure.resourceDropsiteTypes().indexOf(resource) != -1) | ||||
{ | { | ||||
let foundationAccess = PETRA.getLandAccess(gameState, foundation); | let foundationAccess = PETRA.getLandAccess(gameState, foundation); | ||||
if (navalManager.requireTransport(gameState, this.ent, this.entAccess, foundationAccess, foundation.position())) | if (navalManager.requireTransport(gameState, this.ent, this.entAccess, foundationAccess, foundation.position())) | ||||
{ | { | ||||
if (foundation.getMetadata(PlayerID, "base") != this.baseID) | if (foundation.getMetadata(PlayerID, "base") != this.baseID) | ||||
this.ent.setMetadata(PlayerID, "base", foundation.getMetadata(PlayerID, "base")); | this.ent.setMetadata(PlayerID, "base", foundation.getMetadata(PlayerID, "base")); | ||||
this.ent.setMetadata(PlayerID, "target-foundation", foundation.id()); | this.ent.setMetadata(PlayerID, "target-foundation", foundation.id()); | ||||
this.ent.setMetadata(PlayerID, "subrole", "builder"); | this.ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_BUILDER); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
}, this); | }, this); | ||||
if (shouldBuild) | if (shouldBuild) | ||||
return true; | return true; | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | for (const base of gameState.ai.HQ.baseManagers()) | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// If we are here, we have nothing left to gather ... certainly no more resources of this type | // If we are here, we have nothing left to gather ... certainly no more resources of this type | ||||
gameState.ai.HQ.lastFailedGather[resource] = gameState.ai.elapsedTime; | gameState.ai.HQ.lastFailedGather[resource] = gameState.ai.elapsedTime; | ||||
if (gameState.ai.Config.debug > 2) | if (gameState.ai.Config.debug > 2) | ||||
API3.warn(" >>>>> worker with gather-type " + resource + " with nothing to gather "); | API3.warn(" >>>>> worker with gather-type " + resource + " with nothing to gather "); | ||||
this.ent.setMetadata(PlayerID, "subrole", "idle"); | this.ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_IDLE); | ||||
return false; | return false; | ||||
}; | }; | ||||
/** | /** | ||||
* 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 | ||||
*/ | */ | ||||
PETRA.Worker.prototype.startHunting = function(gameState, position) | PETRA.Worker.prototype.startHunting = function(gameState, position) | ||||
▲ Show 20 Lines • Show All 188 Lines • ▼ Show 20 Lines | PETRA.Worker.prototype.startFishing = function(gameState) | ||||
if (nearestSupply) | if (nearestSupply) | ||||
{ | { | ||||
this.base.AddTCGatherer(nearestSupply.id()); | this.base.AddTCGatherer(nearestSupply.id()); | ||||
this.ent.gather(nearestSupply); | this.ent.gather(nearestSupply); | ||||
this.ent.setMetadata(PlayerID, "supply", nearestSupply.id()); | this.ent.setMetadata(PlayerID, "supply", nearestSupply.id()); | ||||
this.ent.setMetadata(PlayerID, "target-foundation", undefined); | this.ent.setMetadata(PlayerID, "target-foundation", undefined); | ||||
return true; | return true; | ||||
} | } | ||||
if (this.ent.getMetadata(PlayerID, "subrole") == "fisher") | if (this.ent.getMetadata(PlayerID, "subrole") == PETRA.Worker.SUBROLE_FISHER) | ||||
this.ent.setMetadata(PlayerID, "subrole", "idle"); | this.ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_IDLE); | ||||
return false; | return false; | ||||
}; | }; | ||||
PETRA.Worker.prototype.gatherNearestField = function(gameState, baseID) | PETRA.Worker.prototype.gatherNearestField = function(gameState, baseID) | ||||
{ | { | ||||
let ownFields = gameState.getOwnEntitiesByClass("Field", true).filter(API3.Filters.isBuilt()).filter(API3.Filters.byMetadata(PlayerID, "base", baseID)); | let ownFields = gameState.getOwnEntitiesByClass("Field", true).filter(API3.Filters.isBuilt()).filter(API3.Filters.byMetadata(PlayerID, "base", baseID)); | ||||
let bestFarm; | let bestFarm; | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
PETRA.Worker.prototype.moveToGatherer = function(gameState, ent, forced) | PETRA.Worker.prototype.moveToGatherer = function(gameState, ent, forced) | ||||
{ | { | ||||
let pos = ent.position(); | let pos = ent.position(); | ||||
if (!pos || ent.getMetadata(PlayerID, "target-foundation") !== undefined) | if (!pos || ent.getMetadata(PlayerID, "target-foundation") !== undefined) | ||||
return; | return; | ||||
if (!forced && gameState.ai.elapsedTime < (ent.getMetadata(PlayerID, "nextMoveToGatherer") || 5)) | if (!forced && gameState.ai.elapsedTime < (ent.getMetadata(PlayerID, "nextMoveToGatherer") || 5)) | ||||
return; | return; | ||||
let gatherers = this.base.workersBySubrole(gameState, "gatherer"); | const gatherers = this.base.workersBySubrole(gameState, PETRA.Worker.SUBROLE_GATHERER); | ||||
let dist = Math.min(); | let dist = Math.min(); | ||||
let destination; | let destination; | ||||
let access = PETRA.getLandAccess(gameState, ent); | let access = PETRA.getLandAccess(gameState, ent); | ||||
let types = ent.resourceDropsiteTypes(); | let types = ent.resourceDropsiteTypes(); | ||||
for (let gatherer of gatherers.values()) | for (let gatherer of gatherers.values()) | ||||
{ | { | ||||
let gathererType = gatherer.getMetadata(PlayerID, "gather-type"); | let gathererType = gatherer.getMetadata(PlayerID, "gather-type"); | ||||
if (!gathererType || types.indexOf(gathererType) == -1) | if (!gathererType || types.indexOf(gathererType) == -1) | ||||
▲ Show 20 Lines • Show All 86 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
I added the _EMPTY constants so it looks a bit nicer/uniform