From f9bee5d3ec7a97859b2b3474a3bfbb5b045c90ff Mon Sep 17 00:00:00 2001 From: wraitii Date: Mon, 9 Dec 2013 14:20:11 +0000 Subject: [PATCH] Slight de-obfuscation of the AI code. This was SVN commit r14317. --- .../mods/public/simulation/ai/aegis/aegis.js | 2 +- .../simulation/ai/aegis/headquarters.js | 1 + .../simulation/ai/common-api-v3/shared.js | 21 ++++++++----------- .../simulation2/components/CCmpAIManager.cpp | 8 +++---- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/binaries/data/mods/public/simulation/ai/aegis/aegis.js b/binaries/data/mods/public/simulation/ai/aegis/aegis.js index e4d608ed35..b9bb113b25 100644 --- a/binaries/data/mods/public/simulation/ai/aegis/aegis.js +++ b/binaries/data/mods/public/simulation/ai/aegis/aegis.js @@ -34,7 +34,7 @@ function AegisBot(settings) { AegisBot.prototype = new BaseAI(); -AegisBot.prototype.InitShared = function(gameState, sharedScript) { +AegisBot.prototype.CustomInit = function(gameState, sharedScript) { this.HQ.init(gameState,sharedScript.events,this.queues); debug ("Initialized with the difficulty " + Config.difficulty); diff --git a/binaries/data/mods/public/simulation/ai/aegis/headquarters.js b/binaries/data/mods/public/simulation/ai/aegis/headquarters.js index 6ba0033b3b..02d451a83a 100644 --- a/binaries/data/mods/public/simulation/ai/aegis/headquarters.js +++ b/binaries/data/mods/public/simulation/ai/aegis/headquarters.js @@ -678,6 +678,7 @@ HQ.prototype.buildMarket = function(gameState, queues){ // Build a farmstead to go to town phase faster and prepare for research. Only really active on higher diff mode. HQ.prototype.buildFarmstead = function(gameState, queues){ if (gameState.getPopulation() > Config.Economy.popForFarmstead) { + // achtung: "DropsiteFood" does not refer to CCs. if (queues.economicBuilding.countQueuedUnitsWithClass("DropsiteFood") === 0 && gameState.countEntitiesAndQueuedByType(gameState.applyCiv("structures/{civ}_farmstead")) === 0){ //only ever build one storehouse/CC/market at a time diff --git a/binaries/data/mods/public/simulation/ai/common-api-v3/shared.js b/binaries/data/mods/public/simulation/ai/common-api-v3/shared.js index 6e6c8c7bea..e14045c84d 100644 --- a/binaries/data/mods/public/simulation/ai/common-api-v3/shared.js +++ b/binaries/data/mods/public/simulation/ai/common-api-v3/shared.js @@ -165,10 +165,10 @@ SharedScript.prototype.GetTemplate = function(name) return null; }; -// initialize the shared component using a given gamestate (the initial gamestate after map creation, usually) -// this is called right at the end of map generation, before you actually reach the map. -SharedScript.prototype.initWithState = function(state) { - this.events = state.events; +// Initialize the shared component. +// We need to now the initial state of the game for this, as we will use it. +// This is called right at the end of the map generation. +SharedScript.prototype.init = function(state) { this.passabilityClasses = state.passabilityClasses; this.passabilityMap = state.passabilityMap; this.players = this._players; @@ -180,11 +180,9 @@ SharedScript.prototype.initWithState = function(state) { this._techModifications[o] = state.players[o].techModifications; this._entities = {}; - for (var id in state.entities) - { this._entities[id] = new Entity(this, state.entities[id]); - } + // entity collection updated on create/destroy event. this.entities = new EntityCollection(this, this._entities); @@ -203,21 +201,21 @@ SharedScript.prototype.initWithState = function(state) { this.gameState[this._players[i]] = new GameState(); this.gameState[this._players[i]].init(this,state,this._players[i]); } - }; // General update of the shared script, before each AI's update // applies entity deltas, and each gamestate. SharedScript.prototype.onUpdate = function(state) { + // deals with updating based on create and destroy messages. this.ApplyEntitiesDelta(state); Engine.ProfileStart("onUpdate"); - + + // those are dynamic and need to be reset as the "state" object moves in memory. this.events = state.events; this.passabilityClasses = state.passabilityClasses; this.passabilityMap = state.passabilityMap; - this.players = this._players; this.playersData = state.players; this.territoryMap = state.territoryMap; this.timeElapsed = state.timeElapsed; @@ -228,11 +226,10 @@ SharedScript.prototype.onUpdate = function(state) for (var i in this.gameState) this.gameState[i].update(this,state); + // TODO: merge those two with "ApplyEntitiesDelta" since after all they do the same. this.updateResourceMaps(this, this.events); this.terrainAnalyzer.updateMapWithEvents(this); - //this.OnUpdate(); - this.turn++; Engine.ProfileStop(); diff --git a/source/simulation2/components/CCmpAIManager.cpp b/source/simulation2/components/CCmpAIManager.cpp index 94ef276c3c..8d7eb76a17 100644 --- a/source/simulation2/components/CCmpAIManager.cpp +++ b/source/simulation2/components/CCmpAIManager.cpp @@ -282,10 +282,10 @@ private: m_Commands.clear(); m_ScriptInterface.CallFunctionVoid(m_Obj.get(), "HandleMessage", state, SharedAI); } - void InitWithSharedScript(CScriptVal state, CScriptValRooted SharedAI) + void InitAI(CScriptVal state, CScriptValRooted SharedAI) { m_Commands.clear(); - m_ScriptInterface.CallFunctionVoid(m_Obj.get(), "InitWithSharedScript", state, SharedAI); + m_ScriptInterface.CallFunctionVoid(m_Obj.get(), "Init", state, SharedAI); } CAIWorker& m_Worker; @@ -523,13 +523,13 @@ public: m_ScriptInterface.SetProperty(state.get(), "passabilityMap", m_PassabilityMapVal, true); m_ScriptInterface.SetProperty(state.get(), "territoryMap", m_TerritoryMapVal, true); - m_ScriptInterface.CallFunctionVoid(m_SharedAIObj.get(), "initWithState", state); + m_ScriptInterface.CallFunctionVoid(m_SharedAIObj.get(), "init", state); m_ScriptInterface.MaybeGC(); for (size_t i = 0; i < m_Players.size(); ++i) { if (m_HasSharedComponent && m_Players[i]->m_UseSharedComponent) - m_Players[i]->InitWithSharedScript(state,m_SharedAIObj); + m_Players[i]->InitAI(state,m_SharedAIObj); } }