1
0
forked from 0ad/0ad

Bug fixes and dead code removal

Fixed Filters.byOwners, before it was using indexOf() which caused
problems with number/string inequality.
Fixed the countUnassignedUnits and getAvailablenits functions to work
with the new filters.
Removed old updating entity collection code since it has been replaced
by the common-api-v2

This was SVN commit r11545.
This commit is contained in:
Jonathan Waller 2012-04-18 10:46:24 +00:00
parent 3027ce1a04
commit 167cd7151e
4 changed files with 17 additions and 87 deletions

View File

@ -22,63 +22,6 @@ function EntityCollectionFromIds(gameState, idList){
return new EntityCollection(gameState.ai, ents);
}
// Do naughty stuff to replace the entity collection constructor for updating entity collections
var tmpEntityCollection = function(baseAI, entities, filter, gameState){
this._ai = baseAI;
this._entities = entities;
if (filter){
this.filterFunc = filter;
this._entities = this.filter(function(ent){
return filter(ent, gameState);
})._entities;
this._ai.registerUpdate(this);
}
// Compute length lazily on demand, since it can be
// expensive for large collections
// This is updated by the update() function.
this._length = undefined;
Object.defineProperty(this, "length", {
get: function () {
if (this._length === undefined)
{
this._length = 0;
for (var id in this._entities)
++this._length;
}
return this._length;
}
});
};
//tmpEntityCollection.prototype = new EntityCollection;
//EntityCollection = tmpEntityCollection;
// Keeps an EntityCollection with a filter function up to date by watching for events
tmpEntityCollection.prototype.update = function(gameState, events){
if (!this.filterFunc)
return;
for (var i in events){
if (events[i].type === "Create"){
var ent = gameState.getEntityById(events[i].msg.entity);
if (ent){
var raw_ent = ent._entity;
if (ent && this.filterFunc(ent, gameState)){
this._entities[events[i].msg.entity] = raw_ent;
if (this._length !== undefined)
this._length ++;
}
}
}else if(events[i].type === "Destroy"){
if (this._entities[events[i].msg.entity]){
delete this._entities[events[i].msg.entity];
if (this._length !== undefined)
this._length --;
}
}
}
};
EntityCollection.prototype.getCentrePosition = function(){
var sumPos = [0, 0];
var count = 0;

View File

@ -149,13 +149,16 @@ MilitaryAttackManager.prototype.getEnemyBuildings = function(gameState,cls) {
MilitaryAttackManager.prototype.getAvailableUnits = function(n, filter) {
var ret = [];
var count = 0;
this.getUnassignedUnits().forEach(function(ent){
if (filter){
if (!filter(ent)){
return;
}
}
var units = undefined;
if (filter){
units = this.getUnassignedUnits().filter(filter);
}else{
units = this.getUnassignedUnits();
}
units.forEach(function(ent){
ret.push(ent.id());
ent.setMetadata("military", "assigned");
ent.setMetadata("role", "military");
@ -185,16 +188,11 @@ MilitaryAttackManager.prototype.getUnassignedUnits = function(){
MilitaryAttackManager.prototype.countAvailableUnits = function(filter){
var count = 0;
this.getUnassignedUnits().forEach(function(ent){
if (filter){
if (filter(ent)){
count += 1;
}
}else{
count += 1;
}
});
return count;
if (filter){
return this.getUnassignedUnits().filter(filter).length;
}else{
return this.getUnassignedUnits().length;
}
};
// Takes an entity id and returns an entity object or false if there is no entity with that id

View File

@ -29,8 +29,6 @@ function QBotAI(settings) {
this.firstTime = true;
this.savedEvents = [];
this.toUpdate = [];
}
QBotAI.prototype = new BaseAI();
@ -74,10 +72,6 @@ QBotAI.prototype.runInit = function(gameState){
}
};
QBotAI.prototype.registerUpdate = function(obj){
this.toUpdate.push(obj);
};
QBotAI.prototype.OnUpdate = function() {
if (this.gameFinished){
return;
@ -99,12 +93,6 @@ QBotAI.prototype.OnUpdate = function() {
return; // With no entities to control the AI cannot do anything
}
// Run these updates before the init so they don't get hammered by the initial creation
// events at the start of the game.
for (var i = 0; i < this.toUpdate.length; i++){
this.toUpdate[i].update(gameState, this.savedEvents);
}
this.runInit(gameState);
for (var i = 0; i < this.modules.length; i++){

View File

@ -10,7 +10,8 @@ Worker.prototype.update = function(gameState) {
var subrole = this.ent.getMetadata("subrole");
if (subrole === "gatherer"){
if (!(this.ent.unitAIState().split(".")[1] === "GATHER" && this.getResourceType(this.ent.unitAIOrderData().type) === this.ent.getMetadata("gather-type"))
if (!(this.ent.unitAIState().split(".")[1] === "GATHER" && this.ent.unitAIOrderData().type
&& this.getResourceType(this.ent.unitAIOrderData().type) === this.ent.getMetadata("gather-type"))
&& !(this.ent.unitAIState().split(".")[1] === "RETURNRESOURCE")){
// TODO: handle combat for hunting animals
Engine.ProfileStart("Start Gathering");