Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/buildManager.js
var PETRA = function(m) | |||||
{ | |||||
/** | /** | ||||
* One task of this manager is to cache the list of structures we have builders for, | * One task of this manager is to cache the list of structures we have builders for, | ||||
* to avoid having to loop on all entities each time. | * to avoid having to loop on all entities each time. | ||||
* It also takes care of the structures we can't currently build and should not try to build endlessly. | * It also takes care of the structures we can't currently build and should not try to build endlessly. | ||||
*/ | */ | ||||
m.BuildManager = function() | PETRA.BuildManager = function() | ||||
{ | { | ||||
// List of buildings we have builders for, with number of possible builders. | // List of buildings we have builders for, with number of possible builders. | ||||
this.builderCounters = new Map(); | this.builderCounters = new Map(); | ||||
// List of buildings we can't currently build (because no room, no builder or whatever), | // List of buildings we can't currently build (because no room, no builder or whatever), | ||||
// with time we should wait before trying again to build it. | // with time we should wait before trying again to build it. | ||||
this.unbuildables = new Map(); | this.unbuildables = new Map(); | ||||
}; | }; | ||||
/** Initialization at start of game */ | /** Initialization at start of game */ | ||||
m.BuildManager.prototype.init = function(gameState) | PETRA.BuildManager.prototype.init = function(gameState) | ||||
{ | { | ||||
let civ = gameState.getPlayerCiv(); | let civ = gameState.getPlayerCiv(); | ||||
for (let ent of gameState.getOwnUnits().values()) | for (let ent of gameState.getOwnUnits().values()) | ||||
this.incrementBuilderCounters(civ, ent, 1); | this.incrementBuilderCounters(civ, ent, 1); | ||||
}; | }; | ||||
m.BuildManager.prototype.incrementBuilderCounters = function(civ, ent, increment) | PETRA.BuildManager.prototype.incrementBuilderCounters = function(civ, ent, increment) | ||||
{ | { | ||||
for (let buildable of ent.buildableEntities(civ)) | for (let buildable of ent.buildableEntities(civ)) | ||||
{ | { | ||||
if (this.builderCounters.has(buildable)) | if (this.builderCounters.has(buildable)) | ||||
{ | { | ||||
let count = this.builderCounters.get(buildable) + increment; | let count = this.builderCounters.get(buildable) + increment; | ||||
if (count < 0) | if (count < 0) | ||||
{ | { | ||||
API3.warn(" Petra error in incrementBuilderCounters for " + buildable + " with count < 0"); | API3.warn(" Petra error in incrementBuilderCounters for " + buildable + " with count < 0"); | ||||
continue; | continue; | ||||
} | } | ||||
this.builderCounters.set(buildable, count); | this.builderCounters.set(buildable, count); | ||||
} | } | ||||
else if (increment > 0) | else if (increment > 0) | ||||
this.builderCounters.set(buildable, increment); | this.builderCounters.set(buildable, increment); | ||||
else | else | ||||
API3.warn(" Petra error in incrementBuilderCounters for " + buildable + " not yet set"); | API3.warn(" Petra error in incrementBuilderCounters for " + buildable + " not yet set"); | ||||
} | } | ||||
}; | }; | ||||
/** Update the builders counters */ | /** Update the builders counters */ | ||||
m.BuildManager.prototype.checkEvents = function(gameState, events) | PETRA.BuildManager.prototype.checkEvents = function(gameState, events) | ||||
{ | { | ||||
this.elapsedTime = gameState.ai.elapsedTime; | this.elapsedTime = gameState.ai.elapsedTime; | ||||
let civ = gameState.getPlayerCiv(); | let civ = gameState.getPlayerCiv(); | ||||
for (let evt of events.Create) | for (let evt of events.Create) | ||||
{ | { | ||||
if (events.Destroy.some(e => e.entity == evt.entity)) | if (events.Destroy.some(e => e.entity == evt.entity)) | ||||
continue; | continue; | ||||
Show All 26 Lines | PETRA.BuildManager.prototype.checkEvents = function(gameState, events) | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Get the first buildable structure with a given class | * Get the first buildable structure with a given class | ||||
* TODO when several available, choose the best one | * TODO when several available, choose the best one | ||||
*/ | */ | ||||
m.BuildManager.prototype.findStructureWithClass = function(gameState, classes) | PETRA.BuildManager.prototype.findStructureWithClass = function(gameState, classes) | ||||
{ | { | ||||
for (let [templateName, count] of this.builderCounters) | for (let [templateName, count] of this.builderCounters) | ||||
{ | { | ||||
if (count == 0 || gameState.isTemplateDisabled(templateName)) | if (count == 0 || gameState.isTemplateDisabled(templateName)) | ||||
continue; | continue; | ||||
let template = gameState.getTemplate(templateName); | let template = gameState.getTemplate(templateName); | ||||
if (!template || !template.available(gameState)) | if (!template || !template.available(gameState)) | ||||
continue; | continue; | ||||
if (MatchesClassList(template.classes(), classes)) | if (MatchesClassList(template.classes(), classes)) | ||||
return templateName; | return templateName; | ||||
} | } | ||||
return undefined; | return undefined; | ||||
}; | }; | ||||
m.BuildManager.prototype.hasBuilder = function(template) | PETRA.BuildManager.prototype.hasBuilder = function(template) | ||||
{ | { | ||||
let numBuilders = this.builderCounters.get(template); | let numBuilders = this.builderCounters.get(template); | ||||
return numBuilders && numBuilders > 0; | return numBuilders && numBuilders > 0; | ||||
}; | }; | ||||
m.BuildManager.prototype.isUnbuildable = function(gameState, template) | PETRA.BuildManager.prototype.isUnbuildable = function(gameState, template) | ||||
{ | { | ||||
return this.unbuildables.has(template) && this.unbuildables.get(template).time > gameState.ai.elapsedTime; | return this.unbuildables.has(template) && this.unbuildables.get(template).time > gameState.ai.elapsedTime; | ||||
}; | }; | ||||
m.BuildManager.prototype.setBuildable = function(template) | PETRA.BuildManager.prototype.setBuildable = function(template) | ||||
{ | { | ||||
if (this.unbuildables.has(template)) | if (this.unbuildables.has(template)) | ||||
this.unbuildables.delete(template); | this.unbuildables.delete(template); | ||||
}; | }; | ||||
/** Time is the duration in second that we will wait before checking again if it is buildable */ | /** Time is the duration in second that we will wait before checking again if it is buildable */ | ||||
m.BuildManager.prototype.setUnbuildable = function(gameState, template, time = 90, reason = "room") | PETRA.BuildManager.prototype.setUnbuildable = function(gameState, template, time = 90, reason = "room") | ||||
{ | { | ||||
if (!this.unbuildables.has(template)) | if (!this.unbuildables.has(template)) | ||||
this.unbuildables.set(template, { "reason": reason, "time": gameState.ai.elapsedTime + time }); | this.unbuildables.set(template, { "reason": reason, "time": gameState.ai.elapsedTime + time }); | ||||
else | else | ||||
{ | { | ||||
let unbuildable = this.unbuildables.get(template); | let unbuildable = this.unbuildables.get(template); | ||||
if (unbuildable.time < gameState.ai.elapsedTime + time) | if (unbuildable.time < gameState.ai.elapsedTime + time) | ||||
{ | { | ||||
unbuildable.reason = reason; | unbuildable.reason = reason; | ||||
unbuildable.time = gameState.ai.elapsedTime + time; | unbuildable.time = gameState.ai.elapsedTime + time; | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
/** Return the number of unbuildables due to missing room */ | /** Return the number of unbuildables due to missing room */ | ||||
m.BuildManager.prototype.numberMissingRoom = function(gameState) | PETRA.BuildManager.prototype.numberMissingRoom = function(gameState) | ||||
{ | { | ||||
let num = 0; | let num = 0; | ||||
for (let unbuildable of this.unbuildables.values()) | for (let unbuildable of this.unbuildables.values()) | ||||
if (unbuildable.reason == "room" && unbuildable.time > gameState.ai.elapsedTime) | if (unbuildable.reason == "room" && unbuildable.time > gameState.ai.elapsedTime) | ||||
++num; | ++num; | ||||
return num; | return num; | ||||
}; | }; | ||||
/** Reset the unbuildables due to missing room */ | /** Reset the unbuildables due to missing room */ | ||||
m.BuildManager.prototype.resetMissingRoom = function(gameState) | PETRA.BuildManager.prototype.resetMissingRoom = function(gameState) | ||||
{ | { | ||||
for (let [key, unbuildable] of this.unbuildables) | for (let [key, unbuildable] of this.unbuildables) | ||||
if (unbuildable.reason == "room") | if (unbuildable.reason == "room") | ||||
this.unbuildables.delete(key); | this.unbuildables.delete(key); | ||||
}; | }; | ||||
m.BuildManager.prototype.Serialize = function() | PETRA.BuildManager.prototype.Serialize = function() | ||||
{ | { | ||||
return { | return { | ||||
"builderCounters": this.builderCounters, | "builderCounters": this.builderCounters, | ||||
"unbuildables": this.unbuildables | "unbuildables": this.unbuildables | ||||
}; | }; | ||||
}; | }; | ||||
m.BuildManager.prototype.Deserialize = function(data) | PETRA.BuildManager.prototype.Deserialize = function(data) | ||||
{ | { | ||||
for (let key in data) | for (let key in data) | ||||
this[key] = data[key]; | this[key] = data[key]; | ||||
}; | }; | ||||
return m; | |||||
}(PETRA); |
Wildfire Games · Phabricator