diff --git a/binaries/data/mods/public/simulation/ai/qbot/entitycollection-extend.js b/binaries/data/mods/public/simulation/ai/qbot/entitycollection-extend.js index e75fb08c1b..f966daa8a2 100644 --- a/binaries/data/mods/public/simulation/ai/qbot/entitycollection-extend.js +++ b/binaries/data/mods/public/simulation/ai/qbot/entitycollection-extend.js @@ -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; diff --git a/binaries/data/mods/public/simulation/ai/qbot/military.js b/binaries/data/mods/public/simulation/ai/qbot/military.js index 0a9f45bcfe..07d2d25265 100755 --- a/binaries/data/mods/public/simulation/ai/qbot/military.js +++ b/binaries/data/mods/public/simulation/ai/qbot/military.js @@ -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 diff --git a/binaries/data/mods/public/simulation/ai/qbot/qbot.js b/binaries/data/mods/public/simulation/ai/qbot/qbot.js index b7d6517a93..85d418a3e0 100644 --- a/binaries/data/mods/public/simulation/ai/qbot/qbot.js +++ b/binaries/data/mods/public/simulation/ai/qbot/qbot.js @@ -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++){ diff --git a/binaries/data/mods/public/simulation/ai/qbot/worker.js b/binaries/data/mods/public/simulation/ai/qbot/worker.js index bcecc49f0a..d50586a45e 100644 --- a/binaries/data/mods/public/simulation/ai/qbot/worker.js +++ b/binaries/data/mods/public/simulation/ai/qbot/worker.js @@ -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");