Differential D4343 Diff 19161 ps/trunk/binaries/data/mods/public/simulation/ai/petra/headquarters.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/headquarters.js
Show First 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | for (let evt of events.OwnershipChanged) // capture events | ||||
} | } | ||||
ent.setMetadata(PlayerID, "role", undefined); | ent.setMetadata(PlayerID, "role", undefined); | ||||
ent.setMetadata(PlayerID, "subrole", undefined); | ent.setMetadata(PlayerID, "subrole", undefined); | ||||
ent.setMetadata(PlayerID, "plan", undefined); | ent.setMetadata(PlayerID, "plan", undefined); | ||||
ent.setMetadata(PlayerID, "PartOfArmy", undefined); | ent.setMetadata(PlayerID, "PartOfArmy", undefined); | ||||
if (ent.hasClass("Trader")) | if (ent.hasClass("Trader")) | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "role", "trader"); | ent.setMetadata(PlayerID, "role", PETRA.Worker.ROLE_TRADER); | ||||
ent.setMetadata(PlayerID, "route", undefined); | ent.setMetadata(PlayerID, "route", undefined); | ||||
} | } | ||||
if (ent.hasClass("Worker")) | if (ent.hasClass("Worker")) | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "role", "worker"); | ent.setMetadata(PlayerID, "role", PETRA.Worker.ROLE_WORKER); | ||||
ent.setMetadata(PlayerID, "subrole", "idle"); | ent.setMetadata(PlayerID, "subrole", PETRA.Worker.SUBROLE_IDLE); | ||||
} | } | ||||
if (ent.hasClass("Ship")) | if (ent.hasClass("Ship")) | ||||
PETRA.setSeaAccess(gameState, ent); | PETRA.setSeaAccess(gameState, ent); | ||||
if (!ent.hasClasses(["Support", "Ship"]) && ent.attackTypes() !== undefined) | if (!ent.hasClasses(["Support", "Ship"]) && ent.attackTypes() !== undefined) | ||||
ent.setMetadata(PlayerID, "plan", -1); | ent.setMetadata(PlayerID, "plan", -1); | ||||
} | } | ||||
// deal with the different rally points of training units: the rally point is set when the training starts | // deal with the different rally points of training units: the rally point is set when the training starts | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | if (evt.to) | ||||
if (!this.decayingStructures.has(evt.entity)) | if (!this.decayingStructures.has(evt.entity)) | ||||
this.decayingStructures.add(evt.entity); | this.decayingStructures.add(evt.entity); | ||||
} | } | ||||
else if (ent.isGarrisonHolder()) | else if (ent.isGarrisonHolder()) | ||||
this.garrisonManager.removeDecayingStructure(evt.entity); | this.garrisonManager.removeDecayingStructure(evt.entity); | ||||
} | } | ||||
// Then deals with decaying structures: destroy them if being lost to enemy (except in easier difficulties) | // Then deals with decaying structures: destroy them if being lost to enemy (except in easier difficulties) | ||||
if (this.Config.difficulty < 2) | if (this.Config.difficulty < PETRA.DIFFICULTY_EASY) | ||||
return; | return; | ||||
for (let entId of this.decayingStructures) | for (let entId of this.decayingStructures) | ||||
{ | { | ||||
let ent = gameState.getEntityById(entId); | let ent = gameState.getEntityById(entId); | ||||
if (ent && ent.decaying() && ent.isOwn(PlayerID)) | if (ent && ent.decaying() && ent.isOwn(PlayerID)) | ||||
{ | { | ||||
let capture = ent.capturePoints(); | let capture = ent.capturePoints(); | ||||
if (!capture) | if (!capture) | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | PETRA.HQ.prototype.trainMoreWorkers = function(gameState, queues) | ||||
let requirementsDef = [ ["costsResource", 1, "food"] ]; | let requirementsDef = [ ["costsResource", 1, "food"] ]; | ||||
const classesDef = ["Support+Worker"]; | const classesDef = ["Support+Worker"]; | ||||
let templateDef = this.findBestTrainableUnit(gameState, classesDef, requirementsDef); | let templateDef = this.findBestTrainableUnit(gameState, classesDef, requirementsDef); | ||||
// counting the workers that aren't part of a plan | // counting the workers that aren't part of a plan | ||||
let numberOfWorkers = 0; // all workers | let numberOfWorkers = 0; // all workers | ||||
let numberOfSupports = 0; // only support workers (i.e. non fighting) | let numberOfSupports = 0; // only support workers (i.e. non fighting) | ||||
gameState.getOwnUnits().forEach(ent => { | gameState.getOwnUnits().forEach(ent => { | ||||
if (ent.getMetadata(PlayerID, "role") == "worker" && ent.getMetadata(PlayerID, "plan") === undefined) | if (ent.getMetadata(PlayerID, "role") === PETRA.Worker.ROLE_WORKER && ent.getMetadata(PlayerID, "plan") === undefined) | ||||
{ | { | ||||
++numberOfWorkers; | ++numberOfWorkers; | ||||
if (ent.hasClass("Support")) | if (ent.hasClass("Support")) | ||||
++numberOfSupports; | ++numberOfSupports; | ||||
} | } | ||||
}); | }); | ||||
let numberInTraining = 0; | let numberInTraining = 0; | ||||
gameState.getOwnTrainingFacilities().forEach(function(ent) { | gameState.getOwnTrainingFacilities().forEach(function(ent) { | ||||
for (let item of ent.trainingQueue()) | for (let item of ent.trainingQueue()) | ||||
{ | { | ||||
numberInTraining += item.count; | numberInTraining += item.count; | ||||
if (item.metadata && item.metadata.role && item.metadata.role == "worker" && | if (item.metadata && item.metadata.role && item.metadata.role === PETRA.Worker.ROLE_WORKER && | ||||
item.metadata.plan === undefined) | item.metadata.plan === undefined) | ||||
{ | { | ||||
numberOfWorkers += item.count; | numberOfWorkers += item.count; | ||||
if (item.metadata.support) | if (item.metadata.support) | ||||
numberOfSupports += item.count; | numberOfSupports += item.count; | ||||
} | } | ||||
} | } | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | if (!templateDef || numberOfSupports + numberOfQueuedSupports > supportNum) | ||||
classes[0].push(pickRandom(["Ranged", "Melee", "Infantry"])); | classes[0].push(pickRandom(["Ranged", "Melee", "Infantry"])); | ||||
template = this.findBestTrainableUnit(gameState, classes, requirements); | template = this.findBestTrainableUnit(gameState, classes, requirements); | ||||
} | } | ||||
// If the template variable is empty, the default unit (Support unit) will be used | // If the template variable is empty, the default unit (Support unit) will be used | ||||
// base "0" means automatic choice of base | // base "0" means automatic choice of base | ||||
if (!template && templateDef) | if (!template && templateDef) | ||||
queues.villager.addPlan(new PETRA.TrainingPlan(gameState, templateDef, { "role": "worker", "base": 0, "support": true }, size, size)); | queues.villager.addPlan(new PETRA.TrainingPlan(gameState, templateDef, { "role": PETRA.Worker.ROLE_WORKER, "base": 0, "support": true }, size, size)); | ||||
else if (template) | else if (template) | ||||
queues.citizenSoldier.addPlan(new PETRA.TrainingPlan(gameState, template, { "role": "worker", "base": 0 }, size, size)); | queues.citizenSoldier.addPlan(new PETRA.TrainingPlan(gameState, template, { "role": PETRA.Worker.ROLE_WORKER, "base": 0 }, size, size)); | ||||
}; | }; | ||||
/** picks the best template based on parameters and classes */ | /** picks the best template based on parameters and classes */ | ||||
PETRA.HQ.prototype.findBestTrainableUnit = function(gameState, classes, requirements) | PETRA.HQ.prototype.findBestTrainableUnit = function(gameState, classes, requirements) | ||||
{ | { | ||||
let units; | let units; | ||||
if (classes.indexOf("Hero") != -1) | if (classes.indexOf("Hero") != -1) | ||||
units = gameState.findTrainableUnits(classes, []); | units = gameState.findTrainableUnits(classes, []); | ||||
▲ Show 20 Lines • Show All 1,381 Lines • ▼ Show 20 Lines | if (!queueManager.canAfford("emergency", cost)) | ||||
{ | { | ||||
if (q == "emergency") | if (q == "emergency") | ||||
continue; | continue; | ||||
queueManager.transferAccounts(cost, q, "emergency"); | queueManager.transferAccounts(cost, q, "emergency"); | ||||
if (queueManager.canAfford("emergency", cost)) | if (queueManager.canAfford("emergency", cost)) | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
let metadata = { "role": "worker", "base": nearestAnchor.getMetadata(PlayerID, "base"), "plan": -1, "trainer": nearestAnchor.id() }; | const metadata = { "role": PETRA.Worker.ROLE_WORKER, "base": nearestAnchor.getMetadata(PlayerID, "base"), "plan": -1, "trainer": nearestAnchor.id() }; | ||||
if (autogarrison) | if (autogarrison) | ||||
metadata.garrisonType = PETRA.GarrisonManager.TYPE_PROTECTION; | metadata.garrisonType = PETRA.GarrisonManager.TYPE_PROTECTION; | ||||
gameState.ai.queues.emergency.addPlan(new PETRA.TrainingPlan(gameState, templateFound[0], metadata, 1, 1)); | gameState.ai.queues.emergency.addPlan(new PETRA.TrainingPlan(gameState, templateFound[0], metadata, 1, 1)); | ||||
return true; | return true; | ||||
}; | }; | ||||
PETRA.HQ.prototype.canBuild = function(gameState, structure) | PETRA.HQ.prototype.canBuild = function(gameState, structure) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 270 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Get the number of workers already accounted for | * Get the number of workers already accounted for | ||||
*/ | */ | ||||
PETRA.HQ.prototype.getAccountedWorkers = function(gameState) | PETRA.HQ.prototype.getAccountedWorkers = function(gameState) | ||||
{ | { | ||||
if (this.turnCache.accountedWorkers == undefined) | if (this.turnCache.accountedWorkers == undefined) | ||||
{ | { | ||||
let workers = gameState.getOwnEntitiesByRole("worker", true).length; | let workers = gameState.getOwnEntitiesByRole(PETRA.Worker.ROLE_WORKER, true).length; | ||||
for (let ent of gameState.getOwnTrainingFacilities().values()) | for (let ent of gameState.getOwnTrainingFacilities().values()) | ||||
{ | { | ||||
for (let item of ent.trainingQueue()) | for (let item of ent.trainingQueue()) | ||||
{ | { | ||||
if (!item.metadata || !item.metadata.role || item.metadata.role != "worker") | if (!item.metadata || !item.metadata.role || item.metadata.role !== PETRA.Worker.ROLE_WORKER) | ||||
continue; | continue; | ||||
workers += item.count; | workers += item.count; | ||||
} | } | ||||
} | } | ||||
this.turnCache.accountedWorkers = workers; | this.turnCache.accountedWorkers = workers; | ||||
} | } | ||||
return this.turnCache.accountedWorkers; | return this.turnCache.accountedWorkers; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | PETRA.HQ.prototype.update = function(gameState, queues, events) | ||||
this.tradeManager.update(gameState, events, queues); | this.tradeManager.update(gameState, events, queues); | ||||
this.garrisonManager.update(gameState, events); | this.garrisonManager.update(gameState, events); | ||||
this.defenseManager.update(gameState, events); | this.defenseManager.update(gameState, events); | ||||
if (gameState.ai.playedTurn % 3 == 0) | if (gameState.ai.playedTurn % 3 == 0) | ||||
{ | { | ||||
this.constructTrainingBuildings(gameState, queues); | this.constructTrainingBuildings(gameState, queues); | ||||
if (this.Config.difficulty > 0) | if (this.Config.difficulty > PETRA.DIFFICULTY_SANDBOX) | ||||
this.buildDefenses(gameState, queues); | this.buildDefenses(gameState, queues); | ||||
} | } | ||||
this.basesManager.update(gameState, queues, events); | this.basesManager.update(gameState, queues, events); | ||||
this.navalManager.update(gameState, queues, events); | this.navalManager.update(gameState, queues, events); | ||||
if (this.Config.difficulty > 0 && (this.hasActiveBase() || !this.canBuildUnits)) | if (this.Config.difficulty > PETRA.DIFFICULTY_SANDBOX && (this.hasActiveBase() || !this.canBuildUnits)) | ||||
this.attackManager.update(gameState, queues, events); | this.attackManager.update(gameState, queues, events); | ||||
this.diplomacyManager.update(gameState, events); | this.diplomacyManager.update(gameState, events); | ||||
this.victoryManager.update(gameState, events, queues); | this.victoryManager.update(gameState, events, queues); | ||||
// We update the capture strength at the end as it can change attack orders | // We update the capture strength at the end as it can change attack orders | ||||
if (gameState.ai.elapsedTime - this.capturableTargetsTime > 3) | if (gameState.ai.elapsedTime - this.capturableTargetsTime > 3) | ||||
▲ Show 20 Lines • Show All 106 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator