Ticket #2180: AIFix_.6.patch
File AIFix_.6.patch, 10.8 KB (added by , 10 years ago) |
---|
-
binaries/data/mods/public/simulation/ai/common-api-v3/shared.js
180 180 this._techModifications[o] = state.players[o].techModifications; 181 181 182 182 this._entities = {}; 183 this.entities = new EntityCollection(this);184 183 185 /*(var id in state.entities)184 for (var id in state.entities) 186 185 { 187 186 this._entities[id] = new Entity(this, state.entities[id]); 188 187 } 189 188 // entity collection updated on create/destroy event. 190 189 this.entities = new EntityCollection(this, this._entities); 191 */192 190 193 this.ApplyEntitiesDelta(state);194 195 191 // create the terrain analyzer 196 192 this.terrainAnalyzer = new TerrainAnalysis(); 197 193 this.terrainAnalyzer.init(this, state); … … 214 210 // applies entity deltas, and each gamestate. 215 211 SharedScript.prototype.onUpdate = function(state) 216 212 { 217 if (this.turn !== 0) 218 this.ApplyEntitiesDelta(state); 213 this.ApplyEntitiesDelta(state); 219 214 220 215 Engine.ProfileStart("onUpdate"); 221 216 … … 297 292 for (var i in this._players) 298 293 delete this._entityMetadata[this._players[i]][evt.msg.entity]; 299 294 } 295 else if (evt.type == "EntityRenamed") 296 { 297 //combines destroy and create. 298 299 warn(uneval(evt.msg)); 300 301 // Switch the metadata 302 for (var i in this._players) 303 { 304 warn (uneval(this._entityMetadata[this._players[i]][evt.msg.entity])); 305 this._entityMetadata[this._players[i]][evt.msg.newentity] = this._entityMetadata[this._players[i]][evt.msg.entity]; 306 this._entityMetadata[this._players[i]][evt.msg.entity] = {}; 307 } 308 } 300 309 else if (evt.type == "TrainingFinished") 301 310 { 302 311 // Apply metadata stored in training queues -
binaries/data/mods/public/simulation/components/AIInterface.js
37 37 return state; 38 38 }; 39 39 // Intended to be called first, during the map initialization: no caching 40 AIInterface.prototype.GetFullRepresentation = function( )40 AIInterface.prototype.GetFullRepresentation = function(flushEvents) 41 41 { 42 42 var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); 43 43 … … 46 46 47 47 // Add some extra AI-specific data 48 48 state.events = this.events; 49 if (flushEvents) 50 { 51 state.events = []; 52 this.events = []; 53 } 49 54 50 55 // Add entity representations 51 56 Engine.ProfileStart("proxy representations"); … … 81 86 this.events.push({"type": "PlayerDefeated", "msg": msg}); 82 87 }; 83 88 89 AIInterface.prototype.OnGlobalEntityRenamed = function(msg) 90 { 91 this.events.push({"type": "EntityRenamed", "msg": msg}); 92 }; 93 84 94 Engine.RegisterComponentType(IID_AIInterface, "AIInterface", AIInterface); -
binaries/data/mods/public/simulation/components/SkirmishReplacer.js
22 22 warn("Skirmish map elements can only be owned by regular players. Please delete entity "+this.entity+" or change the ownership to a non-gaia player."); 23 23 }; 24 24 25 /** 26 * Replace this entity with a civ-specific entity on the first turn 27 */ 28 SkirmishReplacer.prototype.OnUpdate = function(msg) 25 SkirmishReplacer.prototype.ReplaceEntities = function() 29 26 { 30 27 var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); 31 28 var civ = cmpPlayer.GetCiv(); 32 29 33 30 var templateName = ""; 34 31 if (civ in this.template) 35 32 templateName = this.template[civ]; 36 33 else if ("general" in this.template) 37 34 templateName = this.template.general; 38 35 39 36 if (!templateName || civ == "gaia") 40 37 { 41 38 Engine.DestroyEntity(this.entity); … … 43 40 } 44 41 45 42 templateName = templateName.replace(/\{civ\}/g, civ); 46 43 47 44 var cmpCurPosition = Engine.QueryInterface(this.entity, IID_Position); 48 45 var replacement = Engine.AddEntity(templateName); 49 46 var cmpReplacementPosition = Engine.QueryInterface(replacement, IID_Position) … … 54 51 var cmpCurOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 55 52 var cmpReplacementOwnership = Engine.QueryInterface(replacement, IID_Ownership); 56 53 cmpReplacementOwnership.SetOwner(cmpCurOwnership.GetOwner()); 57 54 55 Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: replacement}); 58 56 Engine.DestroyEntity(this.entity); 59 57 }; 58 /** 59 * Replace this entity with a civ-specific entity 60 * Message is sent right before InitGame() is called, in InitGame.js 61 * Replacement needs to happen early on real games to not confuse the AI 62 */ 63 SkirmishReplacer.prototype.OnSkirmishReplace = function(msg) 64 { 65 this.ReplaceEntities(); 66 }; 60 67 68 /** 69 * Replace this entity with a civ-specific entity 70 * This is needed for Atlas, when the entity isn't replaced before the game starts, 71 * so it needs to be replaced on the first turn. 72 */ 73 SkirmishReplacer.prototype.OnUpdate = function(msg) 74 { 75 this.ReplaceEntities(); 76 }; 77 61 78 Engine.RegisterComponentType(IID_SkirmishReplacer, "SkirmishReplacer", SkirmishReplacer); -
binaries/data/mods/public/simulation/components/interfaces/SkirmishReplacer.js
1 1 Engine.RegisterInterface("SkirmishReplacer"); 2 Engine.RegisterMessageType("SkirmishReplace"); -
binaries/data/mods/public/simulation/helpers/InitGame.js
1 function InitGame(settings)1 function ReplaceSkirmishGlobals() 2 2 { 3 3 // This will be called after the map settings have been loaded, 4 4 // before the simulation has started. 5 5 // This is only called at the start of a new game, not when loading 6 6 // a saved game. 7 Engine.BroadcastMessage(MT_SkirmishReplace, {}); 8 } 7 9 10 function InitGame(settings) 11 { 8 12 // No settings when loading a map in Atlas, so do nothing 9 13 if (!settings) 10 14 return; 15 11 16 12 17 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 13 18 var cmpAIManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AIManager); … … 37 42 cmpAIManager.RunGamestateInit(); 38 43 } 39 44 45 Engine.RegisterGlobal("ReplaceSkirmishGlobals", ReplaceSkirmishGlobals); 40 46 Engine.RegisterGlobal("InitGame", InitGame); -
source/ps/Game.cpp
48 48 #include "simulation2/components/ICmpPlayerManager.h" 49 49 #include "soundmanager/ISoundManager.h" 50 50 51 #include "tools/atlas/GameInterface/GameLoop.h" 52 51 53 extern bool g_GameRestarted; 54 extern GameLoopState* g_AtlasGameLoop; 52 55 53 56 /** 54 57 * Globally accessible pointer to the CGame object. … … 200 203 // Call the script function InitGame only for new games, not saved games 201 204 if (!m_IsSavedGame) 202 205 { 206 if (!g_AtlasGameLoop->running) 207 { 208 m_Simulation2->ReplaceSkirmishGlobals(); 209 m_Simulation2->FlushDestroyedEntities(); 210 } 203 211 CScriptVal settings; 204 212 m_Simulation2->GetScriptInterface().GetProperty(m_Simulation2->GetInitAttributes().get(), "settings", settings); 205 213 m_Simulation2->InitGame(settings); -
source/simulation2/Simulation2.cpp
647 647 return m->m_ComponentManager.GetScriptInterface(); 648 648 } 649 649 650 void CSimulation2::ReplaceSkirmishGlobals() 651 { 652 GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "ReplaceSkirmishGlobals"); 653 } 654 650 655 void CSimulation2::InitGame(const CScriptVal& data) 651 656 { 652 657 GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "InitGame", data); -
source/simulation2/Simulation2.h
147 147 void ResetState(bool skipScriptedComponents = false, bool skipAI = false); 148 148 149 149 /** 150 * Send a message to replace skirmish entities with real ones 151 * Called right before InitGame, on CGame instantiation. 152 * (This mustn't be used when e.g. loading saved games, only when starting new ones.) 153 * This calls the ReplaceSkirmishGlobals function defined in helpers/InitGame.js. 154 */ 155 void ReplaceSkirmishGlobals(); 156 157 /** 150 158 * Initialise a new game, based on some script data. (Called on CGame instantiation) 151 159 * (This mustn't be used when e.g. loading saved games, only when starting new ones.) 152 160 * This calls the InitGame function defined in helpers/InitGame.js. -
source/simulation2/components/CCmpAIManager.cpp
994 994 ENSURE(cmpAIInterface); 995 995 996 996 // Get the game state from AIInterface 997 CScriptVal state = cmpAIInterface->GetFullRepresentation( );997 CScriptVal state = cmpAIInterface->GetFullRepresentation(true); 998 998 999 999 // Get the passability data 1000 1000 Grid<u16> dummyGrid; -
source/simulation2/components/ICmpAIInterface.cpp
34 34 { 35 35 return m_Script.Call<CScriptVal> ("GetRepresentation"); 36 36 } 37 virtual CScriptVal GetFullRepresentation( )37 virtual CScriptVal GetFullRepresentation(bool flushEvents = false) 38 38 { 39 return m_Script.Call<CScriptVal> ("GetFullRepresentation" );39 return m_Script.Call<CScriptVal> ("GetFullRepresentation",flushEvents); 40 40 } 41 41 42 42 }; -
source/simulation2/components/ICmpAIInterface.h
32 32 * Returns a script object that represents the current world state, 33 33 * to be passed to AI scripts. No caching for initialization 34 34 */ 35 virtual CScriptVal GetFullRepresentation( ) = 0;35 virtual CScriptVal GetFullRepresentation(bool flushEvents) = 0; 36 36 37 37 DECLARE_INTERFACE_TYPE(AIInterface) 38 38 };