Ticket #2180: AIFix_.4.patch

File AIFix_.4.patch, 9.9 KB (added by wraitii, 10 years ago)
Line 
1Index: binaries/data/mods/public/simulation/ai/common-api-v3/shared.js
2===================================================================
3--- binaries/data/mods/public/simulation/ai/common-api-v3/shared.js (révision 14268)
4+++ binaries/data/mods/public/simulation/ai/common-api-v3/shared.js (copie de travail)
5@@ -180,18 +180,14 @@
6 this._techModifications[o] = state.players[o].techModifications;
7
8 this._entities = {};
9- this.entities = new EntityCollection(this);
10
11- /* (var id in state.entities)
12+ for (var id in state.entities)
13 {
14 this._entities[id] = new Entity(this, state.entities[id]);
15 }
16 // entity collection updated on create/destroy event.
17 this.entities = new EntityCollection(this, this._entities);
18- */
19
20- this.ApplyEntitiesDelta(state);
21-
22 // create the terrain analyzer
23 this.terrainAnalyzer = new TerrainAnalysis();
24 this.terrainAnalyzer.init(this, state);
25@@ -214,8 +210,7 @@
26 // applies entity deltas, and each gamestate.
27 SharedScript.prototype.onUpdate = function(state)
28 {
29- if (this.turn !== 0)
30- this.ApplyEntitiesDelta(state);
31+ this.ApplyEntitiesDelta(state);
32
33 Engine.ProfileStart("onUpdate");
34
35@@ -297,6 +292,20 @@
36 for (var i in this._players)
37 delete this._entityMetadata[this._players[i]][evt.msg.entity];
38 }
39+ else if (evt.type == "EntityRenamed")
40+ {
41+ //combines destroy and create.
42+
43+ // Switch the metadata
44+ for (var i in this._players)
45+ {
46+ warn (uneval(this._entityMetadata[this._players[i]][evt.msg.entity]));
47+ this._entityMetadata[this._players[i]][evt.msg.newentity] = this._entityMetadata[this._players[i]][evt.msg.entity];
48+ this._entityMetadata[this._players[i]][evt.msg.entity] = {};
49+ }
50+ }
51 else if (evt.type == "TrainingFinished")
52 {
53 // Apply metadata stored in training queues
54Index: binaries/data/mods/public/simulation/components/AIInterface.js
55===================================================================
56--- binaries/data/mods/public/simulation/components/AIInterface.js (révision 14268)
57+++ binaries/data/mods/public/simulation/components/AIInterface.js (copie de travail)
58@@ -37,7 +37,7 @@
59 return state;
60 };
61 // Intended to be called first, during the map initialization: no caching
62-AIInterface.prototype.GetFullRepresentation = function()
63+AIInterface.prototype.GetFullRepresentation = function(flushEvents)
64 {
65 var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
66
67@@ -46,6 +46,11 @@
68
69 // Add some extra AI-specific data
70 state.events = this.events;
71+ if (flushEvents)
72+ {
73+ state.events = [];
74+ this.events = [];
75+ }
76
77 // Add entity representations
78 Engine.ProfileStart("proxy representations");
79@@ -81,4 +86,9 @@
80 this.events.push({"type": "PlayerDefeated", "msg": msg});
81 };
82
83+AIInterface.prototype.OnGlobalEntityRenamed = function(msg)
84+{
85+ this.events.push({"type": "EntityRenamed", "msg": msg});
86+};
87+
88 Engine.RegisterComponentType(IID_AIInterface, "AIInterface", AIInterface);
89Index: binaries/data/mods/public/simulation/components/SkirmishReplacer.js
90===================================================================
91--- binaries/data/mods/public/simulation/components/SkirmishReplacer.js (révision 14268)
92+++ binaries/data/mods/public/simulation/components/SkirmishReplacer.js (copie de travail)
93@@ -23,19 +23,21 @@
94 };
95
96 /**
97- * Replace this entity with a civ-specific entity on the first turn
98+ * Replace this entity with a civ-specific entity
99+ * Message is sent right before InitGame() is called, in InitGame.js
100 */
101-SkirmishReplacer.prototype.OnUpdate = function(msg)
102+SkirmishReplacer.prototype.OnSkirmishReplace = function(msg)
103 {
104+ warn("toto");
105 var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
106 var civ = cmpPlayer.GetCiv();
107-
108+
109 var templateName = "";
110 if (civ in this.template)
111 templateName = this.template[civ];
112 else if ("general" in this.template)
113 templateName = this.template.general;
114-
115+
116 if (!templateName || civ == "gaia")
117 {
118 Engine.DestroyEntity(this.entity);
119@@ -43,7 +45,7 @@
120 }
121
122 templateName = templateName.replace(/\{civ\}/g, civ);
123-
124+
125 var cmpCurPosition = Engine.QueryInterface(this.entity, IID_Position);
126 var replacement = Engine.AddEntity(templateName);
127 var cmpReplacementPosition = Engine.QueryInterface(replacement, IID_Position)
128@@ -54,7 +56,8 @@
129 var cmpCurOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
130 var cmpReplacementOwnership = Engine.QueryInterface(replacement, IID_Ownership);
131 cmpReplacementOwnership.SetOwner(cmpCurOwnership.GetOwner());
132-
133+
134+ Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: replacement});
135 Engine.DestroyEntity(this.entity);
136 };
137
138Index: binaries/data/mods/public/simulation/components/interfaces/SkirmishReplacer.js
139===================================================================
140--- binaries/data/mods/public/simulation/components/interfaces/SkirmishReplacer.js (révision 14268)
141+++ binaries/data/mods/public/simulation/components/interfaces/SkirmishReplacer.js (copie de travail)
142@@ -1 +1,2 @@
143 Engine.RegisterInterface("SkirmishReplacer");
144+Engine.RegisterMessageType("SkirmishReplace");
145Index: binaries/data/mods/public/simulation/helpers/InitGame.js
146===================================================================
147--- binaries/data/mods/public/simulation/helpers/InitGame.js (révision 14268)
148+++ binaries/data/mods/public/simulation/helpers/InitGame.js (copie de travail)
149@@ -1,13 +1,21 @@
150-function InitGame(settings)
151+function ReplaceSkirmishGlobals()
152 {
153 // This will be called after the map settings have been loaded,
154 // before the simulation has started.
155 // This is only called at the start of a new game, not when loading
156 // a saved game.
157+ Engine.BroadcastMessage(MT_SkirmishReplace, {});
158+}
159
160+function InitGame(settings)
161+{
162+ // This is called right after ReplaceSkirmishGlobals
163+
164 // No settings when loading a map in Atlas, so do nothing
165 if (!settings)
166 return;
167+
168+ Engine.BroadcastMessage(MT_SkirmishReplace, {});
169
170 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
171 var cmpAIManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AIManager);
172@@ -37,4 +45,5 @@
173 cmpAIManager.RunGamestateInit();
174 }
175
176+Engine.RegisterGlobal("ReplaceSkirmishGlobals", ReplaceSkirmishGlobals);
177 Engine.RegisterGlobal("InitGame", InitGame);
178Index: source/ps/Game.cpp
179===================================================================
180--- source/ps/Game.cpp (révision 14268)
181+++ source/ps/Game.cpp (copie de travail)
182@@ -200,6 +200,9 @@
183 // Call the script function InitGame only for new games, not saved games
184 if (!m_IsSavedGame)
185 {
186+ m_Simulation2->ReplaceSkirmishGlobals();
187+ m_Simulation2->FlushDestroyedEntities();
188+
189 CScriptVal settings;
190 m_Simulation2->GetScriptInterface().GetProperty(m_Simulation2->GetInitAttributes().get(), "settings", settings);
191 m_Simulation2->InitGame(settings);
192Index: source/simulation2/Simulation2.cpp
193===================================================================
194--- source/simulation2/Simulation2.cpp (révision 14268)
195+++ source/simulation2/Simulation2.cpp (copie de travail)
196@@ -647,6 +647,11 @@
197 return m->m_ComponentManager.GetScriptInterface();
198 }
199
200+void CSimulation2::ReplaceSkirmishGlobals()
201+{
202+ GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "ReplaceSkirmishGlobals");
203+}
204+
205 void CSimulation2::InitGame(const CScriptVal& data)
206 {
207 GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "InitGame", data);
208Index: source/simulation2/Simulation2.h
209===================================================================
210--- source/simulation2/Simulation2.h (révision 14268)
211+++ source/simulation2/Simulation2.h (copie de travail)
212@@ -147,6 +147,14 @@
213 void ResetState(bool skipScriptedComponents = false, bool skipAI = false);
214
215 /**
216+ * Send a message to replace skirmish entities with real ones
217+ * Called right before InitGame, on CGame instantiation.
218+ * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
219+ * This calls the ReplaceSkirmishGlobals function defined in helpers/InitGame.js.
220+ */
221+ void ReplaceSkirmishGlobals();
222+
223+ /**
224 * Initialise a new game, based on some script data. (Called on CGame instantiation)
225 * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
226 * This calls the InitGame function defined in helpers/InitGame.js.
227Index: source/simulation2/components/CCmpAIManager.cpp
228===================================================================
229--- source/simulation2/components/CCmpAIManager.cpp (révision 14268)
230+++ source/simulation2/components/CCmpAIManager.cpp (copie de travail)
231@@ -994,7 +994,7 @@
232 ENSURE(cmpAIInterface);
233
234 // Get the game state from AIInterface
235- CScriptVal state = cmpAIInterface->GetFullRepresentation();
236+ CScriptVal state = cmpAIInterface->GetFullRepresentation(true);
237
238 // Get the passability data
239 Grid<u16> dummyGrid;
240Index: source/simulation2/components/ICmpAIInterface.cpp
241===================================================================
242--- source/simulation2/components/ICmpAIInterface.cpp (révision 14268)
243+++ source/simulation2/components/ICmpAIInterface.cpp (copie de travail)
244@@ -34,9 +34,9 @@
245 {
246 return m_Script.Call<CScriptVal> ("GetRepresentation");
247 }
248- virtual CScriptVal GetFullRepresentation()
249+ virtual CScriptVal GetFullRepresentation(bool flushEvents = false)
250 {
251- return m_Script.Call<CScriptVal> ("GetFullRepresentation");
252+ return m_Script.Call<CScriptVal> ("GetFullRepresentation",flushEvents);
253 }
254
255 };
256Index: source/simulation2/components/ICmpAIInterface.h
257===================================================================
258--- source/simulation2/components/ICmpAIInterface.h (révision 14268)
259+++ source/simulation2/components/ICmpAIInterface.h (copie de travail)
260@@ -32,7 +32,7 @@
261 * Returns a script object that represents the current world state,
262 * to be passed to AI scripts. No caching for initialization
263 */
264- virtual CScriptVal GetFullRepresentation() = 0;
265+ virtual CScriptVal GetFullRepresentation(bool flushEvents) = 0;
266
267 DECLARE_INTERFACE_TYPE(AIInterface)
268 };