From faa1a3fd41d9b8ca0db618b76dd1761dad435bc8 Mon Sep 17 00:00:00 2001 From: mimo Date: Fri, 1 Sep 2017 17:38:49 +0000 Subject: [PATCH] petra: update some entities collections when diplomacy changes Differential Revision: https://code.wildfiregames.com/D858 This was SVN commit r20094. --- .../simulation/ai/common-api/gamestate.js | 85 ++++++++----------- .../simulation/ai/petra/headquarters.js | 8 +- .../simulation/ai/petra/tradeManager.js | 4 +- 3 files changed, 42 insertions(+), 55 deletions(-) diff --git a/binaries/data/mods/public/simulation/ai/common-api/gamestate.js b/binaries/data/mods/public/simulation/ai/common-api/gamestate.js index 9a00e69ec7..335337dfd4 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/gamestate.js +++ b/binaries/data/mods/public/simulation/ai/common-api/gamestate.js @@ -79,49 +79,46 @@ m.GameState.prototype.update = function(SharedScript) this.ceasefireActive = SharedScript.ceasefireActive; }; -m.GameState.prototype.updatingCollection = function(id, filter, collection) +m.GameState.prototype.updatingCollection = function(id, filter, parentCollection) { - let gid = this.player + "-" + id; // automatically add the player ID - return this.updatingGlobalCollection(gid, filter, collection); + let gid = "player-" + this.player + "-" + id; // automatically add the player ID + return this.updatingGlobalCollection(gid, filter, parentCollection); }; m.GameState.prototype.destroyCollection = function(id) { - let gid = this.player + "-" + id; // automatically add the player ID + let gid = "player-" + this.player + "-" + id; // automatically add the player ID this.destroyGlobalCollection(gid); }; -m.GameState.prototype.getEC = function(id) +m.GameState.prototype.updatingGlobalCollection = function(gid, filter, parentCollection) { - let gid = this.player + "-" + id; // automatically add the player ID - return this.getGEC(gid); + if (this.EntCollecNames.has(gid)) + return this.EntCollecNames.get(gid); + + let collection = parentCollection ? parentCollection.filter(filter) : this.entities.filter(filter); + collection.registerUpdates(); + this.EntCollecNames.set(gid, collection); + return collection; }; -m.GameState.prototype.updatingGlobalCollection = function(id, filter, collection) +m.GameState.prototype.destroyGlobalCollection = function(gid) { - if (this.EntCollecNames.has(id)) - return this.EntCollecNames.get(id); - - let newCollection = collection !== undefined ? collection.filter(filter) : this.entities.filter(filter); - newCollection.registerUpdates(); - this.EntCollecNames.set(id, newCollection); - return newCollection; -}; - -m.GameState.prototype.destroyGlobalCollection = function(id) -{ - if (!this.EntCollecNames.has(id)) + if (!this.EntCollecNames.has(gid)) return; - this.sharedScript.removeUpdatingEntityCollection(this.EntCollecNames.get(id)); - this.EntCollecNames.delete(id); + this.sharedScript.removeUpdatingEntityCollection(this.EntCollecNames.get(gid)); + this.EntCollecNames.delete(gid); }; -m.GameState.prototype.getGEC = function(id) +/** + * Reset the entities collections which depend on diplomacy + */ +m.GameState.prototype.resetOnDiplomacyChanged = function() { - if (!this.EntCollecNames.has(id)) - return undefined; - return this.EntCollecNames.get(id); + for (let name of this.EntCollecNames.keys()) + if (name.startsWith("player-" + this.player + "-diplo")) + this.destroyGlobalCollection(name); }; m.GameState.prototype.getTimeElapsed = function() @@ -465,7 +462,7 @@ m.GameState.prototype.getEntities = function(id) if (id === undefined) return this.entities; - return this.updatingGlobalCollection("" + id + "-entities", m.Filters.byOwner(id)); + return this.updatingGlobalCollection("player-" + id + "-entities", m.Filters.byOwner(id)); }; m.GameState.prototype.getStructures = function() @@ -475,17 +472,17 @@ m.GameState.prototype.getStructures = function() m.GameState.prototype.getOwnEntities = function() { - return this.updatingGlobalCollection("" + this.player + "-entities", m.Filters.byOwner(this.player)); + return this.updatingGlobalCollection("player-" + this.player + "-entities", m.Filters.byOwner(this.player)); }; m.GameState.prototype.getOwnStructures = function() { - return this.updatingGlobalCollection("" + this.player + "-structures", m.Filters.byClass("Structure"), this.getOwnEntities()); + return this.updatingGlobalCollection("player-" + this.player + "-structures", m.Filters.byClass("Structure"), this.getOwnEntities()); }; m.GameState.prototype.getOwnUnits = function() { - return this.updatingGlobalCollection("" + this.player + "-units", m.Filters.byClass("Unit"), this.getOwnEntities()); + return this.updatingGlobalCollection("player-" + this.player + "-units", m.Filters.byClass("Unit"), this.getOwnEntities()); }; m.GameState.prototype.getAllyEntities = function() @@ -500,12 +497,7 @@ m.GameState.prototype.getExclusiveAllyEntities = function() m.GameState.prototype.getAllyStructures = function() { - return this.updatingCollection("ally-structures", m.Filters.byClass("Structure"), this.getAllyEntities()); -}; - -m.GameState.prototype.resetAllyStructures = function() -{ - this.destroyCollection("ally-structures"); + return this.updatingCollection("diplo-ally-structures", m.Filters.byClass("Structure"), this.getAllyEntities()); }; m.GameState.prototype.getNeutralStructures = function() @@ -521,14 +513,9 @@ m.GameState.prototype.getEnemyEntities = function() m.GameState.prototype.getEnemyStructures = function(enemyID) { if (enemyID === undefined) - return this.updatingCollection("enemy-structures", m.Filters.byClass("Structure"), this.getEnemyEntities()); + return this.updatingCollection("diplo-enemy-structures", m.Filters.byClass("Structure"), this.getEnemyEntities()); - return this.updatingGlobalCollection("" + enemyID + "-structures", m.Filters.byClass("Structure"), this.getEntities(enemyID)); -}; - -m.GameState.prototype.resetEnemyStructures = function() -{ - this.destroyCollection("enemy-structures"); + return this.updatingGlobalCollection("player-" + enemyID + "-structures", m.Filters.byClass("Structure"), this.getEntities(enemyID)); }; m.GameState.prototype.getEnemyUnits = function(enemyID) @@ -536,7 +523,7 @@ m.GameState.prototype.getEnemyUnits = function(enemyID) if (enemyID === undefined) return this.getEnemyEntities().filter(m.Filters.byClass("Unit")); - return this.updatingGlobalCollection("" + enemyID + "-units", m.Filters.byClass("Unit"), this.getEntities(enemyID)); + return this.updatingGlobalCollection("player-" + enemyID + "-units", m.Filters.byClass("Unit"), this.getEntities(enemyID)); }; /** if maintain is true, this will be stored. Otherwise it's one-shot. */ @@ -578,12 +565,12 @@ m.GameState.prototype.getOwnFoundationsByClass = function(cls, maintain) m.GameState.prototype.getOwnTrainingFacilities = function() { - return this.updatingGlobalCollection("" + this.player + "-training-facilities", m.Filters.byTrainingQueue(), this.getOwnEntities()); + return this.updatingGlobalCollection("player-" + this.player + "-training-facilities", m.Filters.byTrainingQueue(), this.getOwnEntities()); }; m.GameState.prototype.getOwnResearchFacilities = function() { - return this.updatingGlobalCollection("" + this.player + "-research-facilities", m.Filters.byResearchAvailable(this.playerData.civ), this.getOwnEntities()); + return this.updatingGlobalCollection("player-" + this.player + "-research-facilities", m.Filters.byResearchAvailable(this.playerData.civ), this.getOwnEntities()); }; @@ -666,19 +653,19 @@ m.GameState.prototype.countOwnQueuedEntitiesWithMetadata = function(data, value) m.GameState.prototype.getOwnFoundations = function() { - return this.updatingGlobalCollection("" + this.player + "-foundations", m.Filters.isFoundation(), this.getOwnStructures()); + return this.updatingGlobalCollection("player-" + this.player + "-foundations", m.Filters.isFoundation(), this.getOwnStructures()); }; m.GameState.prototype.getOwnDropsites = function(resource) { - if (resource !== undefined) + if (resource) return this.updatingCollection("ownDropsite-" + resource, m.Filters.isDropsite(resource), this.getOwnEntities()); return this.updatingCollection("ownDropsite-all", m.Filters.isDropsite(), this.getOwnEntities()); }; m.GameState.prototype.getAnyDropsites = function(resource) { - if (resource !== undefined) + if (resource) return this.updatingGlobalCollection("anyDropsite-" + resource, m.Filters.isDropsite(resource), this.getEntities()); return this.updatingGlobalCollection("anyDropsite-all", m.Filters.isDropsite(), this.getEntities()); }; diff --git a/binaries/data/mods/public/simulation/ai/petra/headquarters.js b/binaries/data/mods/public/simulation/ai/petra/headquarters.js index 2d5b2ec38e..7f510ad30d 100644 --- a/binaries/data/mods/public/simulation/ai/petra/headquarters.js +++ b/binaries/data/mods/public/simulation/ai/petra/headquarters.js @@ -141,8 +141,8 @@ m.HQ.prototype.checkEvents = function (gameState, events, queues) { if (evt.player !== PlayerID && evt.otherPlayer !== PlayerID) continue; - gameState.resetAllyStructures(); - gameState.resetEnemyStructures(); + // Reset the entities collections which depend on diplomacy + gameState.resetOnDiplomacyChanged(); break; } @@ -1108,7 +1108,7 @@ m.HQ.prototype.findStrategicCCLocation = function(gameState, template) */ m.HQ.prototype.findMarketLocation = function(gameState, template) { - let markets = gameState.updatingCollection("ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).toEntityArray(); + let markets = gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).toEntityArray(); if (!markets.length) markets = gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()).toEntityArray(); @@ -2198,7 +2198,7 @@ m.HQ.prototype.isNearInvadingArmy = function(pos) m.HQ.prototype.isUnderEnemyFire = function(gameState, pos, radius = 0) { if (!this.turnCache.firingStructures) - this.turnCache.firingStructures = gameState.updatingCollection("FiringStructures", API3.Filters.hasDefensiveFire(), gameState.getEnemyStructures()); + this.turnCache.firingStructures = gameState.updatingCollection("diplo-FiringStructures", API3.Filters.hasDefensiveFire(), gameState.getEnemyStructures()); for (let ent of this.turnCache.firingStructures.values()) { let range = radius + ent.attackRange("Ranged").max; diff --git a/binaries/data/mods/public/simulation/ai/petra/tradeManager.js b/binaries/data/mods/public/simulation/ai/petra/tradeManager.js index d28cc1100c..946e386471 100644 --- a/binaries/data/mods/public/simulation/ai/petra/tradeManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/tradeManager.js @@ -392,7 +392,7 @@ m.TradeManager.prototype.checkEvents = function(gameState, events) m.TradeManager.prototype.checkRoutes = function(gameState, accessIndex) { let market1 = gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()); - let market2 = gameState.updatingCollection("ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()); + let market2 = gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()); if (market1.length + market2.length < 2) // We have to wait ... markets will be built soon { this.tradeRoute = undefined; @@ -562,7 +562,7 @@ m.TradeManager.prototype.prospectForNewMarket = function(gameState, queues) if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_market")) return; if (!gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()).length && - !gameState.updatingCollection("ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).length) + !gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).length) return; let template = gameState.getTemplate(gameState.applyCiv("structures/{civ}_market")); if (!template)