ai cleanup

This was SVN commit r18274.
This commit is contained in:
mimo 2016-05-31 21:06:53 +00:00
parent 7060ca888c
commit 3eea0df931
3 changed files with 63 additions and 50 deletions

View File

@ -273,7 +273,7 @@ m.Template = m.Class({
countersClasses: function(classes) {
if (!this.get("Attack"))
return false;
var mcounter = [];
let mcounter = [];
for (let type in this.get("Attack"))
{
let bonuses = this.get("Attack/" + type + "/Bonuses");

View File

@ -46,10 +46,12 @@ m.EntityCollection.prototype.Deserialize = function(data, sharedAI)
this.defreeze();
};
// If an entitycollection is frozen, it will never automatically add a unit.
// But can remove one.
// this makes it easy to create entity collection that will auto-remove dead units
// but never add new ones.
/**
* If an entitycollection is frozen, it will never automatically add a unit.
* But can remove one.
* this makes it easy to create entity collection that will auto-remove dead units
* but never add new ones.
*/
m.EntityCollection.prototype.freeze = function()
{
this.frozen = true;
@ -184,14 +186,14 @@ m.EntityCollection.prototype.attack = function(unitId)
return this;
};
// violent, aggressive, defensive, passive, standground
/** violent, aggressive, defensive, passive, standground */
m.EntityCollection.prototype.setStance = function(stance)
{
Engine.PostCommand(PlayerID,{"type": "stance", "entities": this.toIdArray(), "name" : stance, "queued": false});
return this;
};
// Returns the average position of all units
/** Returns the average position of all units */
m.EntityCollection.prototype.getCentrePosition = function()
{
let sumPos = [0, 0];
@ -208,9 +210,11 @@ m.EntityCollection.prototype.getCentrePosition = function()
return count ? [sumPos[0]/count, sumPos[1]/count] : undefined;
};
// returns the average position from the sample first units.
// This might be faster for huge collections, but there's
// always a risk that it'll be unprecise.
/**
* returns the average position from the sample first units.
* This might be faster for huge collections, but there's
* always a risk that it'll be unprecise.
*/
m.EntityCollection.prototype.getApproximatePosition = function(sample)
{
let sumPos = [0, 0];
@ -229,8 +233,12 @@ m.EntityCollection.prototype.getApproximatePosition = function(sample)
return i ? [sumPos[0]/i, sumPos[1]/i] : undefined;
};
m.EntityCollection.prototype.hasEntId = function(id)
{
return this._entities.has(id);
};
// Removes an entity from the collection, returns true if the entity was a member, false otherwise
/** Removes an entity from the collection, returns true if the entity was a member, false otherwise */
m.EntityCollection.prototype.removeEnt = function(ent)
{
if (!this._entities.has(ent.id()))
@ -239,7 +247,7 @@ m.EntityCollection.prototype.removeEnt = function(ent)
return true;
};
// Adds an entity to the collection, returns true if the entity was not member, false otherwise
/** Adds an entity to the collection, returns true if the entity was not member, false otherwise */
m.EntityCollection.prototype.addEnt = function(ent)
{
if (this._entities.has(ent.id()))
@ -248,11 +256,13 @@ m.EntityCollection.prototype.addEnt = function(ent)
return true;
};
// Checks the entity against the filters, and adds or removes it appropriately, returns true if the
// entity collection was modified.
// Force can add a unit despite a freezing.
// If an entitycollection is frozen, it will never automatically add a unit.
// But can remove one.
/**
* Checks the entity against the filters, and adds or removes it appropriately, returns true if the
* entity collection was modified.
* Force can add a unit despite a freezing.
* If an entitycollection is frozen, it will never automatically add a unit.
* But can remove one.
*/
m.EntityCollection.prototype.updateEnt = function(ent, force)
{
let passesFilters = true;

View File

@ -86,7 +86,7 @@ m.GameState.prototype.updatingGlobalCollection = function(id, filter, collection
if (this.EntCollecNames.has(id))
return this.EntCollecNames.get(id);
var newCollection = collection !== undefined ? collection.filter(filter) : this.entities.filter(filter);
let newCollection = collection !== undefined ? collection.filter(filter) : this.entities.filter(filter);
newCollection.registerUpdates();
this.EntCollecNames.set(id, newCollection);
return newCollection;
@ -193,7 +193,7 @@ m.GameState.prototype.isResearching = function(template)
// this is an "in-absolute" check that doesn't check if we have a building to research from.
m.GameState.prototype.canResearch = function(techTemplateName, noRequirementCheck)
{
var template = this.getTemplate(techTemplateName);
let template = this.getTemplate(techTemplateName);
if (!template)
return false;
@ -462,7 +462,7 @@ m.GameState.prototype.getOwnEntitiesByRole = function(role, maintain)
m.GameState.prototype.getOwnEntitiesByType = function(type, maintain)
{
var filter = m.Filters.byType(type);
let filter = m.Filters.byType(type);
if (maintain === true)
return this.updatingCollection("type-" + type, filter, this.getOwnEntities());
return this.getOwnEntities().filter(filter);
@ -470,7 +470,7 @@ m.GameState.prototype.getOwnEntitiesByType = function(type, maintain)
m.GameState.prototype.getOwnEntitiesByClass = function(cls, maintain)
{
var filter = m.Filters.byClass(cls);
let filter = m.Filters.byClass(cls);
if (maintain)
return this.updatingCollection("class-" + cls, filter, this.getOwnEntities());
return this.getOwnEntities().filter(filter);
@ -478,7 +478,7 @@ m.GameState.prototype.getOwnEntitiesByClass = function(cls, maintain)
m.GameState.prototype.getOwnFoundationsByClass = function(cls, maintain)
{
var filter = m.Filters.byClass(cls);
let filter = m.Filters.byClass(cls);
if (maintain)
return this.updatingCollection("foundations-class-" + cls, filter, this.getOwnFoundations());
return this.getOwnFoundations().filter(filter);
@ -502,11 +502,11 @@ m.GameState.prototype.countEntitiesByType = function(type, maintain)
m.GameState.prototype.countEntitiesAndQueuedByType = function(type, maintain)
{
var template = this.getTemplate(type);
let template = this.getTemplate(type);
if (!template)
return 0;
var count = this.countEntitiesByType(type, maintain);
let count = this.countEntitiesByType(type, maintain);
// Count building foundations
if (template.hasClass("Structure") === true)
@ -529,12 +529,12 @@ m.GameState.prototype.countEntitiesAndQueuedByType = function(type, maintain)
m.GameState.prototype.countFoundationsByType = function(type, maintain)
{
var foundationType = "foundation|" + type;
let foundationType = "foundation|" + type;
if (maintain === true)
return this.updatingCollection("foundation-type-" + type, m.Filters.byType(foundationType), this.getOwnFoundations()).length;
var count = 0;
let count = 0;
this.getOwnStructures().forEach(function(ent) {
if (ent.templateName() == foundationType)
++count;
@ -549,7 +549,7 @@ m.GameState.prototype.countOwnEntitiesByRole = function(role)
m.GameState.prototype.countOwnEntitiesAndQueuedWithRole = function(role)
{
var count = this.countOwnEntitiesByRole(role);
let count = this.countOwnEntitiesByRole(role);
// Count entities in building production queues
this.getOwnTrainingFacilities().forEach(function(ent) {
@ -563,7 +563,7 @@ m.GameState.prototype.countOwnEntitiesAndQueuedWithRole = function(role)
m.GameState.prototype.countOwnQueuedEntitiesWithMetadata = function(data, value)
{
// Count entities in building production queues
var count = 0;
let count = 0;
this.getOwnTrainingFacilities().forEach(function(ent) {
for (let item of ent.trainingQueue())
if (item.metadata && item.metadata[data] && item.metadata[data] == value)
@ -602,19 +602,19 @@ m.GameState.prototype.getFishableSupplies = function()
// This returns only units from buildings.
m.GameState.prototype.findTrainableUnits = function(classes, anticlasses)
{
var allTrainable = [];
var civ = this.playerData.civ;
let allTrainable = [];
let civ = this.playerData.civ;
this.getOwnStructures().forEach(function(ent) {
var trainable = ent.trainableEntities(civ);
let trainable = ent.trainableEntities(civ);
if (!trainable)
return;
for (let unit of trainable)
if (allTrainable.indexOf(unit) === -1)
allTrainable.push(unit);
});
var ret = [];
var limits = this.getEntityLimits();
var current = this.getEntityCounts();
let ret = [];
let limits = this.getEntityLimits();
let current = this.getEntityCounts();
for (let trainable of allTrainable)
{
if (this.isDisabledTemplates(trainable))
@ -658,8 +658,8 @@ m.GameState.prototype.findTrainableUnits = function(classes, anticlasses)
// If there are pairs, both techs are returned.
m.GameState.prototype.findAvailableTech = function()
{
var allResearchable = [];
var civ = this.playerData.civ;
let allResearchable = [];
let civ = this.playerData.civ;
this.getOwnEntities().forEach(function(ent) {
let searchable = ent.researchableTechs(civ);
if (!searchable)
@ -682,7 +682,8 @@ m.GameState.prototype.findAvailableTech = function()
ret.push([techs[1]._templateName, techs[1]] );
}
else
if (this.canResearch(tech) && template._templateName != this.townPhase() && template._templateName != this.cityPhase())
if (this.canResearch(tech) && template._templateName != this.townPhase() &&
template._templateName != this.cityPhase())
ret.push( [tech, template] );
}
return ret;
@ -693,7 +694,7 @@ m.GameState.prototype.findAvailableTech = function()
*/
m.GameState.prototype.findTrainers = function(template)
{
var civ = this.playerData.civ;
let civ = this.playerData.civ;
return this.getOwnTrainingFacilities().filter(function(ent) {
let trainable = ent.trainableEntities(civ);
return trainable && trainable.indexOf(template) != -1;
@ -714,14 +715,14 @@ m.GameState.prototype.findBuilder = function(template)
return undefined;
};
// Return true if one of the buildings is capable of researching the given tech
/** Return true if one of the buildings is capable of researching the given tech */
m.GameState.prototype.hasResearchers = function(templateName, noRequirementCheck)
{
// let's check we can research the tech.
if (!this.canResearch(templateName, noRequirementCheck))
return false;
var civ = this.playerData.civ;
let civ = this.playerData.civ;
for (let ent of this.getOwnResearchFacilities().values())
{
@ -732,7 +733,8 @@ m.GameState.prototype.hasResearchers = function(templateName, noRequirementCheck
if (temp.pairDef())
{
let pairedTechs = temp.getPairedTechs();
if (pairedTechs[0]._templateName == templateName || pairedTechs[1]._templateName == templateName)
if (pairedTechs[0]._templateName == templateName ||
pairedTechs[1]._templateName == templateName)
return true;
}
else if (tech == templateName)
@ -742,15 +744,15 @@ m.GameState.prototype.hasResearchers = function(templateName, noRequirementCheck
return false;
};
// Find buildings that are capable of researching the given tech
/** Find buildings that are capable of researching the given tech */
m.GameState.prototype.findResearchers = function(templateName, noRequirementCheck)
{
// let's check we can research the tech.
if (!this.canResearch(templateName, noRequirementCheck))
return [];
var self = this;
var civ = this.playerData.civ;
let self = this;
let civ = this.playerData.civ;
return this.getOwnResearchFacilities().filter(function(ent) {
let techs = ent.researchableTechs(civ);
@ -759,8 +761,9 @@ m.GameState.prototype.findResearchers = function(templateName, noRequirementChec
let thisTemp = self.getTemplate(tech);
if (thisTemp.pairDef())
{
var pairedTechs = thisTemp.getPairedTechs();
if (pairedTechs[0]._templateName == templateName || pairedTechs[1]._templateName == templateName)
let pairedTechs = thisTemp.getPairedTechs();
if (pairedTechs[0]._templateName == templateName ||
pairedTechs[1]._templateName == templateName)
return true;
}
else if (tech == templateName)