2013-12-30 11:04:59 +01:00
|
|
|
var AEGIS = function(m)
|
|
|
|
{
|
|
|
|
|
2013-08-17 15:59:53 +02:00
|
|
|
/*
|
|
|
|
* Used to know which templates I have, which templates I know I can train, things like that.
|
|
|
|
* Mostly unused.
|
|
|
|
*/
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
m.TemplateManager = function(gameState) {
|
2013-08-17 15:59:53 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.knownTemplatesList = [];
|
|
|
|
this.buildingTemplates = [];
|
|
|
|
this.unitTemplates = [];
|
|
|
|
this.templateCounters = {};
|
|
|
|
this.templateCounteredBy = {};
|
|
|
|
|
|
|
|
// this will store templates that exist
|
|
|
|
this.AcknowledgeTemplates(gameState);
|
|
|
|
this.getBuildableSubtemplates(gameState);
|
|
|
|
this.getTrainableSubtemplates(gameState);
|
|
|
|
this.getBuildableSubtemplates(gameState);
|
|
|
|
this.getTrainableSubtemplates(gameState);
|
|
|
|
// should be enough in 100% of the cases.
|
|
|
|
|
|
|
|
this.getTemplateCounters(gameState);
|
|
|
|
|
|
|
|
};
|
2013-12-30 11:04:59 +01:00
|
|
|
m.TemplateManager.prototype.AcknowledgeTemplates = function(gameState)
|
2013-08-17 15:59:53 +02:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
var myEntities = gameState.getOwnEntities();
|
|
|
|
myEntities.forEach(function(ent) { // }){
|
|
|
|
var template = ent._templateName;
|
|
|
|
if (self.knownTemplatesList.indexOf(template) === -1) {
|
|
|
|
self.knownTemplatesList.push(template);
|
|
|
|
if (ent.hasClass("Unit") && self.unitTemplates.indexOf(template) === -1)
|
|
|
|
self.unitTemplates.push(template);
|
|
|
|
else if (self.buildingTemplates.indexOf(template) === -1)
|
|
|
|
self.buildingTemplates.push(template);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2013-12-30 11:04:59 +01:00
|
|
|
m.TemplateManager.prototype.getBuildableSubtemplates = function(gameState)
|
2013-08-17 15:59:53 +02:00
|
|
|
{
|
|
|
|
for each (var templateName in this.knownTemplatesList) {
|
|
|
|
var template = gameState.getTemplate(templateName);
|
|
|
|
if (template !== null) {
|
|
|
|
var buildable = template.buildableEntities();
|
|
|
|
if (buildable !== undefined)
|
|
|
|
for each (var subtpname in buildable) {
|
|
|
|
if (this.knownTemplatesList.indexOf(subtpname) === -1) {
|
|
|
|
this.knownTemplatesList.push(subtpname);
|
|
|
|
var subtemplate = gameState.getTemplate(subtpname);
|
|
|
|
if (subtemplate.hasClass("Unit") && this.unitTemplates.indexOf(subtpname) === -1)
|
|
|
|
this.unitTemplates.push(subtpname);
|
|
|
|
else if (this.buildingTemplates.indexOf(subtpname) === -1)
|
|
|
|
this.buildingTemplates.push(subtpname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-30 11:04:59 +01:00
|
|
|
m.TemplateManager.prototype.getTrainableSubtemplates = function(gameState)
|
2013-08-17 15:59:53 +02:00
|
|
|
{
|
|
|
|
for each (var templateName in this.knownTemplatesList) {
|
|
|
|
var template = gameState.getTemplate(templateName);
|
|
|
|
if (template !== null) {
|
|
|
|
var trainables = template.trainableEntities();
|
|
|
|
if (trainables !== undefined)
|
|
|
|
for each (var subtpname in trainables) {
|
|
|
|
if (this.knownTemplatesList.indexOf(subtpname) === -1) {
|
|
|
|
this.knownTemplatesList.push(subtpname);
|
|
|
|
var subtemplate = gameState.getTemplate(subtpname);
|
|
|
|
if (subtemplate.hasClass("Unit") && this.unitTemplates.indexOf(subtpname) === -1)
|
|
|
|
this.unitTemplates.push(subtpname);
|
|
|
|
else if (this.buildingTemplates.indexOf(subtpname) === -1)
|
|
|
|
this.buildingTemplates.push(subtpname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-30 11:04:59 +01:00
|
|
|
m.TemplateManager.prototype.getTemplateCounters = function(gameState)
|
2013-08-17 15:59:53 +02:00
|
|
|
{
|
|
|
|
for (var i in this.unitTemplates)
|
|
|
|
{
|
|
|
|
var tp = gameState.getTemplate(this.unitTemplates[i]);
|
|
|
|
var tpname = this.unitTemplates[i];
|
|
|
|
this.templateCounters[tpname] = tp.getCounteredClasses();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// features auto-caching
|
2013-12-30 11:04:59 +01:00
|
|
|
m.TemplateManager.prototype.getCountersToClasses = function(gameState,classes,templateName)
|
2013-08-17 15:59:53 +02:00
|
|
|
{
|
|
|
|
if (templateName !== undefined && this.templateCounteredBy[templateName])
|
|
|
|
return this.templateCounteredBy[templateName];
|
|
|
|
|
|
|
|
var templates = [];
|
|
|
|
for (var i in this.templateCounters) {
|
|
|
|
var okay = false;
|
|
|
|
for each (var ticket in this.templateCounters[i]) {
|
|
|
|
var okaya = true;
|
|
|
|
for (var a in ticket[0]) {
|
|
|
|
if (classes.indexOf(ticket[0][a]) === -1)
|
|
|
|
okaya = false;
|
|
|
|
}
|
|
|
|
if (okaya && templates.indexOf(i) === -1)
|
|
|
|
templates.push([i, ticket[1]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
templates.sort (function (a,b) { return -a[1] + b[1]; });
|
|
|
|
|
|
|
|
if (templateName !== undefined)
|
|
|
|
this.templateCounteredBy[templateName] = templates;
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
|
|
|
|
return m;
|
|
|
|
}(AEGIS);
|