Differential D2103 Diff 9534 ps/trunk/binaries/data/mods/public/simulation/ai/petra/researchManager.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/researchManager.js
var PETRA = function(m) | |||||
{ | |||||
/** | /** | ||||
* Manage the research | * Manage the research | ||||
*/ | */ | ||||
PETRA.ResearchManager = function(Config) | |||||
m.ResearchManager = function(Config) | |||||
{ | { | ||||
this.Config = Config; | this.Config = Config; | ||||
}; | }; | ||||
/** | /** | ||||
* Check if we can go to the next phase | * Check if we can go to the next phase | ||||
*/ | */ | ||||
m.ResearchManager.prototype.checkPhase = function(gameState, queues) | PETRA.ResearchManager.prototype.checkPhase = function(gameState, queues) | ||||
{ | { | ||||
if (queues.majorTech.hasQueuedUnits()) | if (queues.majorTech.hasQueuedUnits()) | ||||
return; | return; | ||||
// Don't try to phase up if already trying to gather resources for a civil-centre or wonder | // Don't try to phase up if already trying to gather resources for a civil-centre or wonder | ||||
if (queues.civilCentre.hasQueuedUnits() || queues.wonder.hasQueuedUnits()) | if (queues.civilCentre.hasQueuedUnits() || queues.wonder.hasQueuedUnits()) | ||||
return; | return; | ||||
let currentPhaseIndex = gameState.currentPhase(); | let currentPhaseIndex = gameState.currentPhase(); | ||||
let nextPhaseName = gameState.getPhaseName(currentPhaseIndex+1); | let nextPhaseName = gameState.getPhaseName(currentPhaseIndex+1); | ||||
if (!nextPhaseName) | if (!nextPhaseName) | ||||
return; | return; | ||||
let petraRequirements = | let petraRequirements = | ||||
currentPhaseIndex == 1 && gameState.ai.HQ.getAccountedPopulation(gameState) >= this.Config.Economy.popPhase2 || | currentPhaseIndex == 1 && gameState.ai.HQ.getAccountedPopulation(gameState) >= this.Config.Economy.popPhase2 || | ||||
currentPhaseIndex == 2 && gameState.ai.HQ.getAccountedWorkers(gameState) > this.Config.Economy.workPhase3 || | currentPhaseIndex == 2 && gameState.ai.HQ.getAccountedWorkers(gameState) > this.Config.Economy.workPhase3 || | ||||
currentPhaseIndex >= 3 && gameState.ai.HQ.getAccountedWorkers(gameState) > this.Config.Economy.workPhase4; | currentPhaseIndex >= 3 && gameState.ai.HQ.getAccountedWorkers(gameState) > this.Config.Economy.workPhase4; | ||||
if (petraRequirements && gameState.hasResearchers(nextPhaseName, true)) | if (petraRequirements && gameState.hasResearchers(nextPhaseName, true)) | ||||
{ | { | ||||
gameState.ai.HQ.phasing = currentPhaseIndex + 1; | gameState.ai.HQ.phasing = currentPhaseIndex + 1; | ||||
// Reset the queue priority in case it was changed during a previous phase update | // Reset the queue priority in case it was changed during a previous phase update | ||||
gameState.ai.queueManager.changePriority("majorTech", gameState.ai.Config.priorities.majorTech); | gameState.ai.queueManager.changePriority("majorTech", gameState.ai.Config.priorities.majorTech); | ||||
queues.majorTech.addPlan(new m.ResearchPlan(gameState, nextPhaseName, true)); | queues.majorTech.addPlan(new PETRA.ResearchPlan(gameState, nextPhaseName, true)); | ||||
} | } | ||||
}; | }; | ||||
m.ResearchManager.prototype.researchPopulationBonus = function(gameState, queues) | PETRA.ResearchManager.prototype.researchPopulationBonus = function(gameState, queues) | ||||
{ | { | ||||
if (queues.minorTech.hasQueuedUnits()) | if (queues.minorTech.hasQueuedUnits()) | ||||
return; | return; | ||||
let techs = gameState.findAvailableTech(); | let techs = gameState.findAvailableTech(); | ||||
for (let tech of techs) | for (let tech of techs) | ||||
{ | { | ||||
if (!tech[1]._template.modifications) | if (!tech[1]._template.modifications) | ||||
continue; | continue; | ||||
// TODO may-be loop on all modifs and check if the effect if positive ? | // TODO may-be loop on all modifs and check if the effect if positive ? | ||||
if (tech[1]._template.modifications[0].value !== "Cost/PopulationBonus") | if (tech[1]._template.modifications[0].value !== "Cost/PopulationBonus") | ||||
continue; | continue; | ||||
queues.minorTech.addPlan(new m.ResearchPlan(gameState, tech[0])); | queues.minorTech.addPlan(new PETRA.ResearchPlan(gameState, tech[0])); | ||||
break; | break; | ||||
} | } | ||||
}; | }; | ||||
m.ResearchManager.prototype.researchTradeBonus = function(gameState, queues) | PETRA.ResearchManager.prototype.researchTradeBonus = function(gameState, queues) | ||||
{ | { | ||||
if (queues.minorTech.hasQueuedUnits()) | if (queues.minorTech.hasQueuedUnits()) | ||||
return; | return; | ||||
let techs = gameState.findAvailableTech(); | let techs = gameState.findAvailableTech(); | ||||
for (let tech of techs) | for (let tech of techs) | ||||
{ | { | ||||
if (!tech[1]._template.modifications || !tech[1]._template.affects) | if (!tech[1]._template.modifications || !tech[1]._template.affects) | ||||
continue; | continue; | ||||
if (tech[1]._template.affects.indexOf("Trader") === -1) | if (tech[1]._template.affects.indexOf("Trader") === -1) | ||||
continue; | continue; | ||||
// TODO may-be loop on all modifs and check if the effect if positive ? | // TODO may-be loop on all modifs and check if the effect if positive ? | ||||
if (tech[1]._template.modifications[0].value !== "UnitMotion/WalkSpeed" && | if (tech[1]._template.modifications[0].value !== "UnitMotion/WalkSpeed" && | ||||
tech[1]._template.modifications[0].value !== "Trader/GainMultiplier") | tech[1]._template.modifications[0].value !== "Trader/GainMultiplier") | ||||
continue; | continue; | ||||
queues.minorTech.addPlan(new m.ResearchPlan(gameState, tech[0])); | queues.minorTech.addPlan(new PETRA.ResearchPlan(gameState, tech[0])); | ||||
break; | break; | ||||
} | } | ||||
}; | }; | ||||
/** Techs to be searched for as soon as they are available */ | /** Techs to be searched for as soon as they are available */ | ||||
m.ResearchManager.prototype.researchWantedTechs = function(gameState, techs) | PETRA.ResearchManager.prototype.researchWantedTechs = function(gameState, techs) | ||||
{ | { | ||||
let phase1 = gameState.currentPhase() === 1; | let phase1 = gameState.currentPhase() === 1; | ||||
let available = phase1 ? gameState.ai.queueManager.getAvailableResources(gameState) : null; | let available = phase1 ? gameState.ai.queueManager.getAvailableResources(gameState) : null; | ||||
let numWorkers = phase1 ? gameState.getOwnEntitiesByRole("worker", true).length : 0; | let numWorkers = phase1 ? gameState.getOwnEntitiesByRole("worker", true).length : 0; | ||||
for (let tech of techs) | for (let tech of techs) | ||||
{ | { | ||||
if (!tech[1]._template.modifications) | if (!tech[1]._template.modifications) | ||||
continue; | continue; | ||||
Show All 22 Lines | for (let i in template.modifications) | ||||
else if (template.modifications[i].value === "Attack/Ranged/MaxRange") | else if (template.modifications[i].value === "Attack/Ranged/MaxRange") | ||||
return { "name": tech[0], "increasePriority": false }; | return { "name": tech[0], "increasePriority": false }; | ||||
} | } | ||||
} | } | ||||
return null; | return null; | ||||
}; | }; | ||||
/** Techs to be searched for as soon as they are available, but only after phase 2 */ | /** Techs to be searched for as soon as they are available, but only after phase 2 */ | ||||
m.ResearchManager.prototype.researchPreferredTechs = function(gameState, techs) | PETRA.ResearchManager.prototype.researchPreferredTechs = function(gameState, techs) | ||||
{ | { | ||||
let phase2 = gameState.currentPhase() === 2; | let phase2 = gameState.currentPhase() === 2; | ||||
let available = phase2 ? gameState.ai.queueManager.getAvailableResources(gameState) : null; | let available = phase2 ? gameState.ai.queueManager.getAvailableResources(gameState) : null; | ||||
let numWorkers = phase2 ? gameState.getOwnEntitiesByRole("worker", true).length : 0; | let numWorkers = phase2 ? gameState.getOwnEntitiesByRole("worker", true).length : 0; | ||||
for (let tech of techs) | for (let tech of techs) | ||||
{ | { | ||||
if (!tech[1]._template.modifications) | if (!tech[1]._template.modifications) | ||||
continue; | continue; | ||||
Show All 19 Lines | for (let i in template.modifications) | ||||
return { "name": tech[0], "increasePriority": false }; | return { "name": tech[0], "increasePriority": false }; | ||||
else if (template.modifications[i].value === "Health/IdleRegenRate") | else if (template.modifications[i].value === "Health/IdleRegenRate") | ||||
return { "name": tech[0], "increasePriority": false }; | return { "name": tech[0], "increasePriority": false }; | ||||
} | } | ||||
} | } | ||||
return null; | return null; | ||||
}; | }; | ||||
m.ResearchManager.prototype.update = function(gameState, queues) | PETRA.ResearchManager.prototype.update = function(gameState, queues) | ||||
{ | { | ||||
if (queues.minorTech.hasQueuedUnits() || queues.majorTech.hasQueuedUnits()) | if (queues.minorTech.hasQueuedUnits() || queues.majorTech.hasQueuedUnits()) | ||||
return; | return; | ||||
let techs = gameState.findAvailableTech(); | let techs = gameState.findAvailableTech(); | ||||
let techName = this.researchWantedTechs(gameState, techs); | let techName = this.researchWantedTechs(gameState, techs); | ||||
if (techName) | if (techName) | ||||
{ | { | ||||
if (techName.increasePriority) | if (techName.increasePriority) | ||||
{ | { | ||||
gameState.ai.queueManager.changePriority("minorTech", 2*this.Config.priorities.minorTech); | gameState.ai.queueManager.changePriority("minorTech", 2*this.Config.priorities.minorTech); | ||||
let plan = new m.ResearchPlan(gameState, techName.name); | let plan = new PETRA.ResearchPlan(gameState, techName.name); | ||||
plan.queueToReset = "minorTech"; | plan.queueToReset = "minorTech"; | ||||
queues.minorTech.addPlan(plan); | queues.minorTech.addPlan(plan); | ||||
} | } | ||||
else | else | ||||
queues.minorTech.addPlan(new m.ResearchPlan(gameState, techName.name)); | queues.minorTech.addPlan(new PETRA.ResearchPlan(gameState, techName.name)); | ||||
return; | return; | ||||
} | } | ||||
if (gameState.currentPhase() < 2) | if (gameState.currentPhase() < 2) | ||||
return; | return; | ||||
techName = this.researchPreferredTechs(gameState, techs); | techName = this.researchPreferredTechs(gameState, techs); | ||||
if (techName) | if (techName) | ||||
{ | { | ||||
if (techName.increasePriority) | if (techName.increasePriority) | ||||
{ | { | ||||
gameState.ai.queueManager.changePriority("minorTech", 2*this.Config.priorities.minorTech); | gameState.ai.queueManager.changePriority("minorTech", 2*this.Config.priorities.minorTech); | ||||
let plan = new m.ResearchPlan(gameState, techName.name); | let plan = new PETRA.ResearchPlan(gameState, techName.name); | ||||
plan.queueToReset = "minorTech"; | plan.queueToReset = "minorTech"; | ||||
queues.minorTech.addPlan(plan); | queues.minorTech.addPlan(plan); | ||||
} | } | ||||
else | else | ||||
queues.minorTech.addPlan(new m.ResearchPlan(gameState, techName.name)); | queues.minorTech.addPlan(new PETRA.ResearchPlan(gameState, techName.name)); | ||||
return; | return; | ||||
} | } | ||||
if (gameState.currentPhase() < 3) | if (gameState.currentPhase() < 3) | ||||
return; | return; | ||||
// remove some techs not yet used by this AI | // remove some techs not yet used by this AI | ||||
// remove also sharedLos if we have no ally | // remove also sharedLos if we have no ally | ||||
Show All 13 Lines | if (template.modifications && template.modifications.length === 1 && | ||||
techs.splice(i--, 1); | techs.splice(i--, 1); | ||||
continue; | continue; | ||||
} | } | ||||
} | } | ||||
if (!techs.length) | if (!techs.length) | ||||
return; | return; | ||||
// randomly pick one. No worries about pairs in that case. | // randomly pick one. No worries about pairs in that case. | ||||
queues.minorTech.addPlan(new m.ResearchPlan(gameState, pickRandom(techs)[0])); | queues.minorTech.addPlan(new PETRA.ResearchPlan(gameState, pickRandom(techs)[0])); | ||||
}; | }; | ||||
m.ResearchManager.prototype.CostSum = function(cost) | PETRA.ResearchManager.prototype.CostSum = function(cost) | ||||
{ | { | ||||
let costSum = 0; | let costSum = 0; | ||||
for (let res in cost) | for (let res in cost) | ||||
costSum += cost[res]; | costSum += cost[res]; | ||||
return costSum; | return costSum; | ||||
}; | }; | ||||
m.ResearchManager.prototype.Serialize = function() | PETRA.ResearchManager.prototype.Serialize = function() | ||||
{ | { | ||||
return {}; | return {}; | ||||
}; | }; | ||||
m.ResearchManager.prototype.Deserialize = function(data) | PETRA.ResearchManager.prototype.Deserialize = function(data) | ||||
{ | { | ||||
}; | }; | ||||
return m; | |||||
}(PETRA); |
Wildfire Games · Phabricator