Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/worker.js
Show All 31 Lines | m.Worker.prototype.update = function(gameState, ent) | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "subrole", "idle"); | ent.setMetadata(PlayerID, "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; | ||||
if (gameState.ai.HQ.turnCache.allowDistantFood === undefined) | |||||
gameState.ai.HQ.turnCache.allowDistantFood = | |||||
!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_field") && | |||||
!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_corral"); | |||||
this.allowDistantFood = gameState.ai.HQ.turnCache.allowDistantFood; | |||||
Sandarac: Perhaps it would be better to include "worker" in this variable name, in order to make it clear… | |||||
mimoAuthorUnsubmitted Not Done Inline Actionsgood catch. It is now moved where needed. mimo: good catch. It is now moved where needed. | |||||
let unitAIState = ent.unitAIState(); | let unitAIState = ent.unitAIState(); | ||||
if ((subrole === "hunter" || subrole === "gatherer") && | if ((subrole === "hunter" || 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) && !this.retryGathering(gameState, subrole)) | if (this.isInaccessibleSupply(gameState) && !this.retryGathering(gameState, subrole)) | ||||
ent.stopMoving(); | ent.stopMoving(); | ||||
▲ Show 20 Lines • Show All 333 Lines • ▼ Show 20 Lines | if (this.accessIndex === access) | ||||
// --> for food, try to gather from fields if any, otherwise build one if any | // --> for food, try to gather from fields if any, otherwise build one if any | ||||
if (resource === "food") | if (resource === "food") | ||||
{ | { | ||||
if ((supply = this.gatherNearestField(gameState, this.baseID))) | if ((supply = this.gatherNearestField(gameState, this.baseID))) | ||||
{ | { | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
return true; | return true; | ||||
} | } | ||||
else if (this.ent.isBuilder() && (supply = this.buildAnyField(gameState, this.baseID))) | if (this.ent.isBuilder() && (supply = this.buildAnyField(gameState, this.baseID))) | ||||
{ | { | ||||
this.ent.repair(supply); | this.ent.repair(supply); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
if ((supply = findSupply(this.ent, this.base.dropsiteSupplies[resource].medium))) | if ((supply = findSupply(this.ent, this.base.dropsiteSupplies[resource].medium))) | ||||
{ | { | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | m.Worker.prototype.startGathering = function(gameState) | ||||
if (shouldBuild) | if (shouldBuild) | ||||
return true; | return true; | ||||
// Still nothing ... try bases which need a transport | // Still nothing ... try bases which need a transport | ||||
for (let base of gameState.ai.HQ.baseManagers) | for (let base of gameState.ai.HQ.baseManagers) | ||||
{ | { | ||||
if (base.accessIndex === access) | if (base.accessIndex === access) | ||||
continue; | continue; | ||||
if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].nearby))) | if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].nearby)) && | ||||
SandaracUnsubmitted Not Done Inline ActionsMaybe it is time to rework some of this function to remove these assignments in selection statements, which are bad practice (perhaps not in this diff). Sandarac: Maybe it is time to rework some of this function to remove these assignments in selection… | |||||
mimoAuthorUnsubmitted Not Done Inline Actionsok done mimo: ok done | |||||
{ | navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | ||||
if (navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | |||||
{ | { | ||||
if (base.ID !== this.baseID) | if (base.ID !== this.baseID) | ||||
this.ent.setMetadata(PlayerID, "base", base.ID); | this.ent.setMetadata(PlayerID, "base", base.ID); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
} | |||||
if (resource === "food") // --> for food, try to gather from fields if any, otherwise build one if any | if (resource === "food") // --> for food, try to gather from fields if any, otherwise build one if any | ||||
{ | { | ||||
for (let base of gameState.ai.HQ.baseManagers) | for (let base of gameState.ai.HQ.baseManagers) | ||||
{ | { | ||||
if (base.accessIndex === access) | if (base.accessIndex === access) | ||||
continue; | continue; | ||||
if ((supply = this.gatherNearestField(gameState, base.ID))) | if ((supply = this.gatherNearestField(gameState, base.ID)) && | ||||
{ | navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | ||||
if (navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | |||||
{ | { | ||||
if (base.ID !== this.baseID) | if (base.ID !== this.baseID) | ||||
this.ent.setMetadata(PlayerID, "base", base.ID); | this.ent.setMetadata(PlayerID, "base", base.ID); | ||||
return true; | return true; | ||||
} | } | ||||
} | if ((supply = this.buildAnyField(gameState, base.ID)) && | ||||
if ((supply = this.buildAnyField(gameState, base.ID))) | navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | ||||
{ | |||||
if (navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | |||||
{ | { | ||||
if (base.ID !== this.baseID) | if (base.ID !== this.baseID) | ||||
this.ent.setMetadata(PlayerID, "base", base.ID); | this.ent.setMetadata(PlayerID, "base", base.ID); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | |||||
for (let base of gameState.ai.HQ.baseManagers) | for (let base of gameState.ai.HQ.baseManagers) | ||||
{ | { | ||||
if (base.accessIndex === access) | if (base.accessIndex === access) | ||||
continue; | continue; | ||||
if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].medium))) | if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].medium)) && | ||||
{ | navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | ||||
if (navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | |||||
{ | { | ||||
if (base.ID !== this.baseID) | if (base.ID !== this.baseID) | ||||
this.ent.setMetadata(PlayerID, "base", base.ID); | this.ent.setMetadata(PlayerID, "base", base.ID); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
} | |||||
// Okay so we haven't found any appropriate dropsite anywhere. | // Okay so we haven't found any appropriate dropsite anywhere. | ||||
// Try to help building one if any non-accessible foundation available | // Try to help building one if any non-accessible foundation available | ||||
shouldBuild = this.ent.isBuilder() && foundations.some(function(foundation) { | shouldBuild = this.ent.isBuilder() && foundations.some(function(foundation) { | ||||
if (!foundation || foundation.getMetadata(PlayerID, "access") === access) | if (!foundation || foundation.getMetadata(PlayerID, "access") === access) | ||||
return false; | return false; | ||||
if (foundation.resourceDropsiteTypes() && foundation.resourceDropsiteTypes().indexOf(resource) !== -1) | if (foundation.resourceDropsiteTypes() && foundation.resourceDropsiteTypes().indexOf(resource) !== -1) | ||||
{ | { | ||||
let foundationAccess = m.getLandAccess(gameState, foundation); | let foundationAccess = m.getLandAccess(gameState, foundation); | ||||
if (navalManager.requireTransport(gameState, this.ent, access, foundationAccess, foundation.position())) | if (navalManager.requireTransport(gameState, this.ent, access, 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()); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
}, this); | }, this); | ||||
if (shouldBuild) | if (shouldBuild) | ||||
return true; | return true; | ||||
// Still nothing, we look now for faraway resources, first in the accessible ones, then in the others | // Still nothing, we look now for faraway resources, first in the accessible ones, then in the others | ||||
// except for food which is not worth (farms or corrals should be used) | // except for food when farms or corrals can be used | ||||
if (resource !== "food") | if (resource !== "food" || this.allowDistantFood) | ||||
{ | |||||
if (this.accessIndex === access) | |||||
{ | { | ||||
if ((supply = findSupply(this.ent, this.base.dropsiteSupplies[resource].faraway))) | if (this.accessIndex === access && | ||||
(supply = findSupply(this.ent, this.base.dropsiteSupplies[resource].faraway))) | |||||
{ | { | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
return true; | return true; | ||||
} | } | ||||
} | |||||
for (let base of gameState.ai.HQ.baseManagers) | for (let base of gameState.ai.HQ.baseManagers) | ||||
{ | { | ||||
Not Done Inline ActionsI was rather thinking of doing (after updating turnCache.allowDistantFood): if (resource !== "food" || gameState.ai.HQ.turnCache.allowDistantFood) What do you think? Sandarac: I was rather thinking of doing (after updating turnCache.allowDistantFood):
```
if (resource ! | |||||
Not Done Inline ActionsWouldn't that force us to update allowDistantFood in the cache even if no food gatherer at this turn? which does not happen with the current code (although that would be irrelevant for performance, better avoid useless computations). mimo: Wouldn't that force us to update allowDistantFood in the cache even if no food gatherer at this… | |||||
Not Done Inline ActionsOkay. Sandarac: Okay. | |||||
if (base.ID === this.baseID) | if (base.ID === this.baseID) | ||||
continue; | continue; | ||||
if (base.accessIndex !== access) | if (base.accessIndex !== access) | ||||
continue; | continue; | ||||
if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].faraway))) | if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].faraway))) | ||||
{ | { | ||||
this.ent.setMetadata(PlayerID, "base", base.ID); | this.ent.setMetadata(PlayerID, "base", base.ID); | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
for (let base of gameState.ai.HQ.baseManagers) | for (let base of gameState.ai.HQ.baseManagers) | ||||
{ | { | ||||
if (base.accessIndex === access) | if (base.accessIndex === access) | ||||
continue; | continue; | ||||
if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].faraway))) | if ((supply = findSupply(this.ent, base.dropsiteSupplies[resource].faraway)) && | ||||
{ | navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | ||||
if (navalManager.requireTransport(gameState, this.ent, access, base.accessIndex, supply.position())) | |||||
{ | { | ||||
if (base.ID !== this.baseID) | if (base.ID !== this.baseID) | ||||
this.ent.setMetadata(PlayerID, "base", base.ID); | this.ent.setMetadata(PlayerID, "base", base.ID); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | |||||
// 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) | ||||
warn(" >>>>> worker with gather-type " + resource + " with nothing to gather "); | warn(" >>>>> worker with gather-type " + resource + " with nothing to gather "); | ||||
this.ent.setMetadata(PlayerID, "subrole", "idle"); | this.ent.setMetadata(PlayerID, "subrole", "idle"); | ||||
return false; | return false; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 390 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Perhaps it would be better to include "worker" in this variable name, in order to make it clear what this var is for, as it can be accessed from outside of worker.
But I'm also not sure if this var is needed, as it is only used in the one check at the end of the function, and this whole check here on turnCache.allowDistantFood could be moved below, so it is only done if needed.