Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/worker.js
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | if (subrole == "gatherer" || subrole == "hunter") | ||||
} | } | ||||
} | } | ||||
if (ent.getMetadata(PlayerID, "transport") !== undefined) | if (ent.getMetadata(PlayerID, "transport") !== undefined) | ||||
return; | return; | ||||
} | } | ||||
this.entAccess = PETRA.getLandAccess(gameState, ent); | this.entAccess = PETRA.getLandAccess(gameState, ent); | ||||
// base 0 for unassigned entities has no accessIndex, so take the one from the entity | // base 0 for unassigned entities has no accessIndex, so take the one from the entity | ||||
if (this.baseID == gameState.ai.HQ.baseManagers[0].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, ... | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "subrole", "idle"); | ent.setMetadata(PlayerID, "subrole", "idle"); | ||||
this.base.reassignIdleWorkers(gameState, [ent]); | this.base.reassignIdleWorkers(gameState, [ent]); | ||||
▲ Show 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | else if (unitAIStateOrder == "GATHER") | ||||
// in case UnitAI did something bad | // in case UnitAI did something bad | ||||
if (ent.unitAIOrderData().length) | if (ent.unitAIOrderData().length) | ||||
{ | { | ||||
let supplyId = ent.unitAIOrderData()[0].target; | let supplyId = ent.unitAIOrderData()[0].target; | ||||
let supply = gameState.getEntityById(supplyId); | let supply = gameState.getEntityById(supplyId); | ||||
if (supply && !supply.hasClasses(["Field", "Animal"]) && | if (supply && !supply.hasClasses(["Field", "Animal"]) && | ||||
supplyId != ent.getMetadata(PlayerID, "supply")) | supplyId != ent.getMetadata(PlayerID, "supply")) | ||||
{ | { | ||||
let nbGatherers = supply.resourceSupplyNumGatherers() + gameState.ai.HQ.GetTCGatherer(supplyId); | let nbGatherers = supply.resourceSupplyNumGatherers() + this.base.GetTCGatherer(supplyId); | ||||
Lint: prefer-const: 'nbGatherers' is never reassigned. Use 'const' instead. | |||||
if (nbGatherers > 1 && supply.resourceSupplyAmount()/nbGatherers < 30) | if (nbGatherers > 1 && supply.resourceSupplyAmount()/nbGatherers < 30) | ||||
{ | { | ||||
gameState.ai.HQ.RemoveTCGatherer(supplyId); | this.base.RemoveTCGatherer(supplyId); | ||||
this.startGathering(gameState); | this.startGathering(gameState); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
let gatherType = ent.getMetadata(PlayerID, "gather-type"); | let gatherType = ent.getMetadata(PlayerID, "gather-type"); | ||||
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)) | ||||
ent.setMetadata(PlayerID, "supply", supplyId); | ent.setMetadata(PlayerID, "supply", supplyId); | ||||
else if (nearby.length) | else if (nearby.length) | ||||
{ | { | ||||
gameState.ai.HQ.RemoveTCGatherer(supplyId); | this.base.RemoveTCGatherer(supplyId); | ||||
this.startGathering(gameState); | this.startGathering(gameState); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
let medium = this.base.dropsiteSupplies[gatherType].medium; | let medium = this.base.dropsiteSupplies[gatherType].medium; | ||||
if (medium.length && !medium.some(sup => sup.id == supplyId)) | if (medium.length && !medium.some(sup => sup.id == supplyId)) | ||||
{ | { | ||||
gameState.ai.HQ.RemoveTCGatherer(supplyId); | this.base.RemoveTCGatherer(supplyId); | ||||
this.startGathering(gameState); | this.startGathering(gameState); | ||||
} | } | ||||
else | else | ||||
ent.setMetadata(PlayerID, "supply", supplyId); | ent.setMetadata(PlayerID, "supply", supplyId); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | if (unitAIStateOrder == "REPAIR") | ||||
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", "idle"); | ||||
ent.stopMoving(); | ent.stopMoving(); | ||||
if (this.baseID != gameState.ai.HQ.baseManagers[0].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", "idle"); | ||||
ent.setMetadata(PlayerID, "target-foundation", undefined); | ent.setMetadata(PlayerID, "target-foundation", undefined); | ||||
if (this.baseID != gameState.ai.HQ.baseManagers[0].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 | ||||
Show All 10 Lines | PETRA.Worker.prototype.update = function(gameState, ent) | ||||
{ | { | ||||
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; | ||||
for (let base of gameState.ai.HQ.baseManagers) | for (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (!base.anchor || !base.anchor.position()) | if (!base.anchor || !base.anchor.position()) | ||||
continue; | continue; | ||||
let basePos = base.anchor.position(); | let basePos = base.anchor.position(); | ||||
if (this.startHunting(gameState, basePos)) | if (this.startHunting(gameState, basePos)) | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "base", base.ID); | ent.setMetadata(PlayerID, "base", base.ID); | ||||
if (base.accessIndex == this.entAccess) | if (base.accessIndex == this.entAccess) | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | if (PETRA.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; | ||||
let findSupply = function(ent, supplies) { | let findSupply = function(worker, supplies) { | ||||
Lint: prefer-const 'findSupply' is never reassigned. Use 'const' instead. Lint: prefer-const: 'findSupply' is never reassigned. Use 'const' instead. | |||||
let ent = worker.ent; | |||||
Lint: prefer-const 'ent' is never reassigned. Use 'const' instead. Lint: prefer-const: 'ent' is never reassigned. Use 'const' instead. | |||||
let ret = false; | let ret = false; | ||||
let gatherRates = ent.resourceGatherRates(); | let gatherRates = ent.resourceGatherRates(); | ||||
for (let i = 0; i < supplies.length; ++i) | for (let i = 0; i < supplies.length; ++i) | ||||
{ | { | ||||
// exhausted resource, remove it from this list | // exhausted resource, remove it from this list | ||||
if (!supplies[i].ent || !gameState.getEntityById(supplies[i].id)) | if (!supplies[i].ent || !gameState.getEntityById(supplies[i].id)) | ||||
{ | { | ||||
supplies.splice(i--, 1); | supplies.splice(i--, 1); | ||||
continue; | continue; | ||||
} | } | ||||
if (PETRA.IsSupplyFull(gameState, supplies[i].ent)) | if (PETRA.IsSupplyFull(gameState, supplies[i].ent)) | ||||
continue; | continue; | ||||
let inaccessibleTime = supplies[i].ent.getMetadata(PlayerID, "inaccessibleTime"); | let inaccessibleTime = supplies[i].ent.getMetadata(PlayerID, "inaccessibleTime"); | ||||
if (inaccessibleTime && gameState.ai.elapsedTime < inaccessibleTime) | if (inaccessibleTime && gameState.ai.elapsedTime < inaccessibleTime) | ||||
continue; | continue; | ||||
let supplyType = supplies[i].ent.get("ResourceSupply/Type"); | let supplyType = supplies[i].ent.get("ResourceSupply/Type"); | ||||
if (!gatherRates[supplyType]) | if (!gatherRates[supplyType]) | ||||
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 = supplies[i].ent.resourceSupplyNumGatherers() + gameState.ai.HQ.GetTCGatherer(supplies[i].id); | let nbGatherers = supplies[i].ent.resourceSupplyNumGatherers() + worker.base.GetTCGatherer(supplies[i].id); | ||||
Lint: prefer-const 'nbGatherers' is never reassigned. Use 'const' instead. Lint: prefer-const: 'nbGatherers' is never reassigned. Use 'const' instead. | |||||
if (supplies[i].ent.resourceSupplyType().specific != "grain" && nbGatherers > 0 && | if (supplies[i].ent.resourceSupplyType().specific != "grain" && nbGatherers > 0 && | ||||
supplies[i].ent.resourceSupplyAmount()/(1+nbGatherers) < 30) | supplies[i].ent.resourceSupplyAmount()/(1+nbGatherers) < 30) | ||||
continue; | continue; | ||||
// not in ennemy territory | // not in ennemy territory | ||||
let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(supplies[i].ent.position()); | let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(supplies[i].ent.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; | ||||
gameState.ai.HQ.AddTCGatherer(supplies[i].id); | worker.base.AddTCGatherer(supplies[i].id); | ||||
ent.setMetadata(PlayerID, "supply", supplies[i].id); | ent.setMetadata(PlayerID, "supply", supplies[i].id); | ||||
ret = supplies[i].ent; | ret = supplies[i].ent; | ||||
break; | break; | ||||
} | } | ||||
return ret; | return ret; | ||||
}; | }; | ||||
let navalManager = gameState.ai.HQ.navalManager; | let navalManager = gameState.ai.HQ.navalManager; | ||||
let supply; | let supply; | ||||
// first look in our own base if accessible from our present position | // first look in our own base if accessible from our present position | ||||
if (this.baseAccess == this.entAccess) | if (this.baseAccess == this.entAccess) | ||||
{ | { | ||||
supply = findSupply(this.ent, this.base.dropsiteSupplies[resource].nearby); | supply = findSupply(this, this.base.dropsiteSupplies[resource].nearby); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
return true; | return true; | ||||
} | } | ||||
// --> 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") | ||||
{ | { | ||||
supply = this.gatherNearestField(gameState, this.baseID); | supply = this.gatherNearestField(gameState, this.baseID); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
return true; | return true; | ||||
} | } | ||||
supply = this.buildAnyField(gameState, this.baseID); | supply = this.buildAnyField(gameState, this.baseID); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
this.ent.repair(supply); | this.ent.repair(supply); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
supply = findSupply(this.ent, this.base.dropsiteSupplies[resource].medium); | supply = findSupply(this, this.base.dropsiteSupplies[resource].medium); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
// So if we're here we have checked our whole base for a proper resource (or it was not accessible) | // So if we're here we have checked our whole base for a proper resource (or it was not accessible) | ||||
// --> check other bases directly accessible | // --> check other bases directly accessible | ||||
for (let base of gameState.ai.HQ.baseManagers) | for (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.ID == this.baseID) | if (base.ID == this.baseID) | ||||
continue; | continue; | ||||
if (base.accessIndex != this.entAccess) | if (base.accessIndex != this.entAccess) | ||||
continue; | continue; | ||||
supply = findSupply(this.ent, base.dropsiteSupplies[resource].nearby); | supply = findSupply(this, base.dropsiteSupplies[resource].nearby); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
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; | ||||
} | } | ||||
} | } | ||||
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 (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.ID == this.baseID) | if (base.ID == this.baseID) | ||||
continue; | continue; | ||||
if (base.accessIndex != this.entAccess) | if (base.accessIndex != this.entAccess) | ||||
continue; | continue; | ||||
supply = this.gatherNearestField(gameState, base.ID); | supply = this.gatherNearestField(gameState, base.ID); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
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; | ||||
} | } | ||||
supply = this.buildAnyField(gameState, base.ID); | supply = this.buildAnyField(gameState, base.ID); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
this.ent.setMetadata(PlayerID, "base", base.ID); | this.ent.setMetadata(PlayerID, "base", base.ID); | ||||
this.ent.repair(supply); | this.ent.repair(supply); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
for (let base of gameState.ai.HQ.baseManagers) | for (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.ID == this.baseID) | if (base.ID == this.baseID) | ||||
continue; | continue; | ||||
if (base.accessIndex != this.entAccess) | if (base.accessIndex != this.entAccess) | ||||
continue; | continue; | ||||
supply = findSupply(this.ent, base.dropsiteSupplies[resource].medium); | supply = findSupply(this, base.dropsiteSupplies[resource].medium); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
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; | ||||
} | } | ||||
} | } | ||||
Show All 14 Lines | if (structure.resourceDropsiteTypes() && structure.resourceDropsiteTypes().indexOf(resource) != -1) | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}, this); | }, this); | ||||
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 (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.accessIndex == this.entAccess) | if (base.accessIndex == this.entAccess) | ||||
continue; | continue; | ||||
supply = findSupply(this.ent, base.dropsiteSupplies[resource].nearby); | supply = findSupply(this, base.dropsiteSupplies[resource].nearby); | ||||
if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, base.accessIndex, supply.position())) | if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, 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 (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.accessIndex == this.entAccess) | if (base.accessIndex == this.entAccess) | ||||
continue; | continue; | ||||
supply = this.gatherNearestField(gameState, base.ID); | supply = this.gatherNearestField(gameState, base.ID); | ||||
if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, base.accessIndex, supply.position())) | if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, 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; | ||||
} | } | ||||
supply = this.buildAnyField(gameState, base.ID); | supply = this.buildAnyField(gameState, base.ID); | ||||
if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, base.accessIndex, supply.position())) | if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, 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 (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.accessIndex == this.entAccess) | if (base.accessIndex == this.entAccess) | ||||
continue; | continue; | ||||
supply = findSupply(this.ent, base.dropsiteSupplies[resource].medium); | supply = findSupply(this, base.dropsiteSupplies[resource].medium); | ||||
if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, base.accessIndex, supply.position())) | if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, 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. | ||||
Show All 29 Lines | if (gameState.ai.HQ.turnCache.allowDistantFood === undefined) | ||||
!gameState.ai.HQ.canBuild(gameState, "structures/{civ}/field") && | !gameState.ai.HQ.canBuild(gameState, "structures/{civ}/field") && | ||||
!gameState.ai.HQ.canBuild(gameState, "structures/{civ}/corral"); | !gameState.ai.HQ.canBuild(gameState, "structures/{civ}/corral"); | ||||
allowDistant = gameState.ai.HQ.turnCache.allowDistantFood; | allowDistant = gameState.ai.HQ.turnCache.allowDistantFood; | ||||
} | } | ||||
if (allowDistant) | if (allowDistant) | ||||
{ | { | ||||
if (this.baseAccess == this.entAccess) | if (this.baseAccess == this.entAccess) | ||||
{ | { | ||||
supply = findSupply(this.ent, this.base.dropsiteSupplies[resource].faraway); | supply = findSupply(this, this.base.dropsiteSupplies[resource].faraway); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
this.ent.gather(supply); | this.ent.gather(supply); | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
for (let base of gameState.ai.HQ.baseManagers) | for (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.ID == this.baseID) | if (base.ID == this.baseID) | ||||
continue; | continue; | ||||
if (base.accessIndex != this.entAccess) | if (base.accessIndex != this.entAccess) | ||||
continue; | continue; | ||||
supply = findSupply(this.ent, base.dropsiteSupplies[resource].faraway); | supply = findSupply(this, base.dropsiteSupplies[resource].faraway); | ||||
if (supply) | if (supply) | ||||
{ | { | ||||
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 (const base of gameState.ai.HQ.baseManagers()) | ||||
{ | { | ||||
if (base.accessIndex == this.entAccess) | if (base.accessIndex == this.entAccess) | ||||
continue; | continue; | ||||
supply = findSupply(this.ent, base.dropsiteSupplies[resource].faraway); | supply = findSupply(this, base.dropsiteSupplies[resource].faraway); | ||||
if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, base.accessIndex, supply.position())) | if (supply && navalManager.requireTransport(gameState, this.ent, this.entAccess, 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; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | for (let supply of resources.values()) | ||||
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() + this.base.GetTCGatherer(supply.id()); | ||||
Lint: prefer-const 'nbGatherers' is never reassigned. Use 'const' instead. Lint: prefer-const: 'nbGatherers' is never reassigned. Use 'const' instead. | |||||
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 FastMoving and Ranged units should hunt fleeing animals. | // Only FastMoving and Ranged units should hunt fleeing animals. | ||||
if (canFlee && !isFastMoving && !isRanged) | if (canFlee && !isFastMoving && !isRanged) | ||||
continue; | continue; | ||||
Show All 28 Lines | for (let supply of resources.values()) | ||||
nearestSupplyDist = dist; | nearestSupplyDist = dist; | ||||
nearestSupply = supply; | nearestSupply = supply; | ||||
} | } | ||||
if (nearestSupply) | if (nearestSupply) | ||||
{ | { | ||||
if (position) | if (position) | ||||
return true; | return true; | ||||
gameState.ai.HQ.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; | ||||
} | } | ||||
return false; | return false; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | resources.forEach(function(supply) | ||||
let supplyType = supply.get("ResourceSupply/Type"); | let supplyType = supply.get("ResourceSupply/Type"); | ||||
if (!gatherRates[supplyType]) | if (!gatherRates[supplyType]) | ||||
return; | return; | ||||
if (PETRA.IsSupplyFull(gameState, supply)) | if (PETRA.IsSupplyFull(gameState, supply)) | ||||
return; | return; | ||||
// 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() + this.base.GetTCGatherer(supply.id()); | ||||
Lint: prefer-const 'nbGatherers' is never reassigned. Use 'const' instead. Lint: prefer-const: 'nbGatherers' is never reassigned. Use 'const' instead. | |||||
if (nbGatherers > 0 && supply.resourceSupplyAmount()/(1+nbGatherers) < 30) | if (nbGatherers > 0 && supply.resourceSupplyAmount()/(1+nbGatherers) < 30) | ||||
return; | return; | ||||
// Avoid ennemy territory | // Avoid ennemy territory | ||||
if (!gameState.ai.HQ.navalManager.canFishSafely(gameState, supply)) | if (!gameState.ai.HQ.navalManager.canFishSafely(gameState, supply)) | ||||
return; | return; | ||||
// measure the distance from the resource to the nearest dropsite | // measure the distance from the resource to the nearest dropsite | ||||
Show All 9 Lines | PETRA.Worker.prototype.startFishing = function(gameState) | ||||
{ | { | ||||
gameState.ai.HQ.navalManager.resetFishingBoats(gameState, fisherSea); | gameState.ai.HQ.navalManager.resetFishingBoats(gameState, fisherSea); | ||||
this.ent.destroy(); | this.ent.destroy(); | ||||
return false; | return false; | ||||
} | } | ||||
if (nearestSupply) | if (nearestSupply) | ||||
{ | { | ||||
gameState.ai.HQ.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") == "fisher") | ||||
this.ent.setMetadata(PlayerID, "subrole", "idle"); | this.ent.setMetadata(PlayerID, "subrole", "idle"); | ||||
return false; | return false; | ||||
Show All 12 Lines | for (let field of ownFields.values()) | ||||
let supplyType = field.get("ResourceSupply/Type"); | let supplyType = field.get("ResourceSupply/Type"); | ||||
if (!gatherRates[supplyType]) | if (!gatherRates[supplyType]) | ||||
continue; | continue; | ||||
let rate = 1; | let rate = 1; | ||||
let diminishing = field.getDiminishingReturns(); | let diminishing = field.getDiminishingReturns(); | ||||
if (diminishing < 1) | if (diminishing < 1) | ||||
{ | { | ||||
let num = field.resourceSupplyNumGatherers() + gameState.ai.HQ.GetTCGatherer(field.id()); | let num = field.resourceSupplyNumGatherers() + this.base.GetTCGatherer(field.id()); | ||||
Lint: prefer-const 'num' is never reassigned. Use 'const' instead. Lint: prefer-const: 'num' is never reassigned. Use 'const' instead. | |||||
if (num > 0) | if (num > 0) | ||||
rate = Math.pow(diminishing, num); | rate = Math.pow(diminishing, num); | ||||
} | } | ||||
// Add a penalty distance depending on rate | // Add a penalty distance depending on rate | ||||
let dist = API3.SquareVectorDistance(field.position(), this.ent.position()) + (1 - rate) * 160000; | let dist = API3.SquareVectorDistance(field.position(), this.ent.position()) + (1 - rate) * 160000; | ||||
if (!bestFarm || dist < bestFarm.dist) | if (!bestFarm || dist < bestFarm.dist) | ||||
bestFarm = { "ent": field, "dist": dist, "rate": rate }; | bestFarm = { "ent": field, "dist": dist, "rate": rate }; | ||||
} | } | ||||
// If other field foundations available, better build them when rate becomes too small | // If other field foundations available, better build them when rate becomes too small | ||||
if (!bestFarm || bestFarm.rate < 0.70 && | if (!bestFarm || bestFarm.rate < 0.70 && | ||||
gameState.getOwnFoundations().filter(API3.Filters.byClass("Field")).filter(API3.Filters.byMetadata(PlayerID, "base", baseID)).hasEntities()) | gameState.getOwnFoundations().filter(API3.Filters.byClass("Field")).filter(API3.Filters.byMetadata(PlayerID, "base", baseID)).hasEntities()) | ||||
return false; | return false; | ||||
gameState.ai.HQ.AddTCGatherer(bestFarm.ent.id()); | this.base.AddTCGatherer(bestFarm.ent.id()); | ||||
this.ent.setMetadata(PlayerID, "supply", bestFarm.ent.id()); | this.ent.setMetadata(PlayerID, "supply", bestFarm.ent.id()); | ||||
return bestFarm.ent; | return bestFarm.ent; | ||||
}; | }; | ||||
/** | /** | ||||
* WARNING with the present options of AI orders, the unit will not gather after building the farm. | * WARNING with the present options of AI orders, the unit will not gather after building the farm. | ||||
* This is done by calling the gatherNearestField function when construction is completed. | * This is done by calling the gatherNearestField function when construction is completed. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 131 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
'nbGatherers' is never reassigned. Use 'const' instead.