1 | Index: binaries/data/mods/public/simulation/ai/common-api/base.js
|
---|
2 | ===================================================================
|
---|
3 | --- binaries/data/mods/public/simulation/ai/common-api/base.js (revision 10305)
|
---|
4 | +++ binaries/data/mods/public/simulation/ai/common-api/base.js (working copy)
|
---|
5 | @@ -82,8 +82,11 @@
|
---|
6 | this.playerData = state.players[this._player];
|
---|
7 | this.templates = this._templates;
|
---|
8 | this.timeElapsed = state.timeElapsed;
|
---|
9 | - this.map = state.map;
|
---|
10 | + this.map = state.passabilityMap;
|
---|
11 | + this.passabilityMap = state.passabilityMap;
|
---|
12 | this.passabilityClasses = state.passabilityClasses;
|
---|
13 | + this.territoryMap = state.territoryMap;
|
---|
14 |
|
---|
15 | Engine.ProfileStop();
|
---|
16 |
|
---|
17 | @@ -96,7 +99,10 @@
|
---|
18 | delete this.templates;
|
---|
19 | delete this.timeElapsed;
|
---|
20 | delete this.map;
|
---|
21 | + delete this.passabilityMap;
|
---|
22 | delete this.passabilityClasses;
|
---|
23 | + delete this.territoryMap;
|
---|
24 | };
|
---|
25 |
|
---|
26 | BaseAI.prototype.ApplyEntitiesDelta = function(state)
|
---|
27 | Index: source/simulation2/components/CCmpAIManager.cpp
|
---|
28 | ===================================================================
|
---|
29 | --- source/simulation2/components/CCmpAIManager.cpp (revision 10305)
|
---|
30 | +++ source/simulation2/components/CCmpAIManager.cpp (working copy)
|
---|
31 | @@ -34,6 +34,7 @@
|
---|
32 | #include "simulation2/components/ICmpObstructionManager.h"
|
---|
33 | #include "simulation2/components/ICmpRangeManager.h"
|
---|
34 | #include "simulation2/components/ICmpTemplateManager.h"
|
---|
35 | +#include "simulation2/components/ICmpTerritoryManager.h"
|
---|
36 | #include "simulation2/helpers/Grid.h"
|
---|
37 | #include "simulation2/serialization/DebugSerializer.h"
|
---|
38 | #include "simulation2/serialization/StdDeserializer.h"
|
---|
39 | @@ -272,7 +273,8 @@
|
---|
40 | m_PlayerMetadata.clear();
|
---|
41 | m_Players.clear();
|
---|
42 | m_GameState.reset();
|
---|
43 | - m_GameStateMapVal = CScriptValRooted();
|
---|
44 | + m_PassabilityMapVal = CScriptValRooted();
|
---|
45 | + m_TerritoryMapVal = CScriptValRooted();
|
---|
46 | }
|
---|
47 |
|
---|
48 | bool AddPlayer(const std::wstring& aiName, player_id_t player, bool callConstructor)
|
---|
49 | @@ -286,20 +288,30 @@
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 |
|
---|
53 | - void StartComputation(const shared_ptr<ScriptInterface::StructuredClone>& gameState, const Grid<u16>& map)
|
---|
54 | + void StartComputation(const shared_ptr<ScriptInterface::StructuredClone>& gameState, const Grid<u16>& passabilityMap, const Grid<u8>& territoryMap, bool territoryMapDirty)
|
---|
55 | {
|
---|
56 | ENSURE(m_CommandsComputed);
|
---|
57 |
|
---|
58 | m_GameState = gameState;
|
---|
59 |
|
---|
60 | - if (map.m_DirtyID != m_GameStateMap.m_DirtyID)
|
---|
61 | + if (passabilityMap.m_DirtyID != m_PassabilityMap.m_DirtyID)
|
---|
62 | {
|
---|
63 | - m_GameStateMap = map;
|
---|
64 | + m_PassabilityMap = passabilityMap;
|
---|
65 |
|
---|
66 | JSContext* cx = m_ScriptInterface.GetContext();
|
---|
67 | - m_GameStateMapVal = CScriptValRooted(cx, ScriptInterface::ToJSVal(cx, m_GameStateMap));
|
---|
68 | + m_PassabilityMapVal = CScriptValRooted(cx, ScriptInterface::ToJSVal(cx, m_PassabilityMap));
|
---|
69 | }
|
---|
70 |
|
---|
71 | + if (territoryMapDirty)
|
---|
72 | + {
|
---|
73 | + PROFILE("territoryMap");
|
---|
74 | + m_TerritoryMap = territoryMap;
|
---|
75 | +
|
---|
76 | + JSContext* cx = m_ScriptInterface.GetContext();
|
---|
77 | + m_TerritoryMapVal = CScriptValRooted(cx, ScriptInterface::ToJSVal(cx, m_TerritoryMap));
|
---|
78 | + }
|
---|
79 | +
|
---|
80 | +
|
---|
81 | m_CommandsComputed = false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | @@ -427,12 +439,14 @@
|
---|
85 |
|
---|
86 | void PerformComputation()
|
---|
87 | {
|
---|
88 | +
|
---|
89 | // Deserialize the game state, to pass to the AI's HandleMessage
|
---|
90 | CScriptVal state;
|
---|
91 | {
|
---|
92 | PROFILE("AI compute read state");
|
---|
93 | state = m_ScriptInterface.ReadStructuredClone(m_GameState);
|
---|
94 | - m_ScriptInterface.SetProperty(state.get(), "map", m_GameStateMapVal, true);
|
---|
95 | + m_ScriptInterface.SetProperty(state.get(), "passabilityMap", m_PassabilityMapVal, true);
|
---|
96 | + m_ScriptInterface.SetProperty(state.get(), "territoryMap", m_TerritoryMapVal, true);
|
---|
97 | }
|
---|
98 |
|
---|
99 | // It would be nice to do
|
---|
100 | @@ -467,8 +481,10 @@
|
---|
101 | std::vector<shared_ptr<CAIPlayer> > m_Players; // use shared_ptr just to avoid copying
|
---|
102 |
|
---|
103 | shared_ptr<ScriptInterface::StructuredClone> m_GameState;
|
---|
104 | - Grid<u16> m_GameStateMap;
|
---|
105 | - CScriptValRooted m_GameStateMapVal;
|
---|
106 | + Grid<u16> m_PassabilityMap;
|
---|
107 | + CScriptValRooted m_PassabilityMapVal;
|
---|
108 | + Grid<u8> m_TerritoryMap;
|
---|
109 | + CScriptValRooted m_TerritoryMapVal;
|
---|
110 |
|
---|
111 | bool m_CommandsComputed;
|
---|
112 | };
|
---|
113 | @@ -570,14 +586,25 @@
|
---|
114 |
|
---|
115 | // Get the map data
|
---|
116 | Grid<u16> dummyGrid;
|
---|
117 | - const Grid<u16>* map = &dummyGrid;
|
---|
118 | + const Grid<u16>* passabilityMap = &dummyGrid;
|
---|
119 | CmpPtr<ICmpPathfinder> cmpPathfinder(GetSimContext(), SYSTEM_ENTITY);
|
---|
120 | if (!cmpPathfinder.null())
|
---|
121 | - map = &cmpPathfinder->GetPassabilityGrid();
|
---|
122 | + passabilityMap = &cmpPathfinder->GetPassabilityGrid();
|
---|
123 |
|
---|
124 | + // Get the territory data
|
---|
125 | + bool territoryMapDirty = false;
|
---|
126 | + Grid<u8> dummyGrid2;
|
---|
127 | + const Grid<u8>* territoryMap = &dummyGrid2;
|
---|
128 | + CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(GetSimContext(), SYSTEM_ENTITY);
|
---|
129 | + if (!cmpTerritoryManager.null() && !cmpTerritoryManager->NeedUpdate(&m_TerritoriesDirtyID)){
|
---|
130 | + territoryMap = &cmpTerritoryManager->GetTerritoryGrid();
|
---|
131 | + territoryMapDirty = true;
|
---|
132 | + }
|
---|
133 | +
|
---|
134 | +
|
---|
135 | LoadPathfinderClasses(state);
|
---|
136 |
|
---|
137 | - m_Worker.StartComputation(scriptInterface.WriteStructuredClone(state.get()), *map);
|
---|
138 | + m_Worker.StartComputation(scriptInterface.WriteStructuredClone(state.get()), *passabilityMap, *territoryMap, territoryMapDirty);
|
---|
139 | }
|
---|
140 |
|
---|
141 | virtual void PushCommands()
|
---|
142 | @@ -664,6 +691,7 @@
|
---|
143 | }
|
---|
144 |
|
---|
145 | CAIWorker m_Worker;
|
---|
146 | + size_t m_TerritoriesDirtyID;
|
---|
147 | };
|
---|
148 |
|
---|
149 | REGISTER_COMPONENT_TYPE(AIManager)
|
---|
150 | Index: source/simulation2/scripting/EngineScriptConversions.cpp
|
---|
151 | ===================================================================
|
---|
152 | --- source/simulation2/scripting/EngineScriptConversions.cpp (revision 10305)
|
---|
153 | +++ source/simulation2/scripting/EngineScriptConversions.cpp (working copy)
|
---|
154 | @@ -236,3 +236,30 @@
|
---|
155 |
|
---|
156 | return OBJECT_TO_JSVAL(obj);
|
---|
157 | }
|
---|
158 | +
|
---|
159 | +template<> jsval ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, const Grid<u8>& val)
|
---|
160 | +{
|
---|
161 | + JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
|
---|
162 | + if (!obj)
|
---|
163 | + return JSVAL_VOID;
|
---|
164 | +
|
---|
165 | + jsuint len = val.m_W * val.m_H;
|
---|
166 | + JSObject *darray = js_CreateTypedArray(cx, js::TypedArray::TYPE_INT8, len);
|
---|
167 | + if (!darray)
|
---|
168 | + return JSVAL_VOID;
|
---|
169 | +
|
---|
170 | + js::TypedArray *tdest = js::TypedArray::fromJSObject(darray);
|
---|
171 | + ENSURE(tdest->byteLength == len*sizeof(u8));
|
---|
172 | +
|
---|
173 | + memcpy(tdest->data, val.m_Data, tdest->byteLength);
|
---|
174 | +
|
---|
175 | + jsval w = ToJSVal(cx, val.m_W);
|
---|
176 | + jsval h = ToJSVal(cx, val.m_H);
|
---|
177 | + jsval data = OBJECT_TO_JSVAL(darray);
|
---|
178 | +
|
---|
179 | + JS_SetProperty(cx, obj, "width", &w);
|
---|
180 | + JS_SetProperty(cx, obj, "height", &h);
|
---|
181 | + JS_SetProperty(cx, obj, "data", &data);
|
---|
182 | +
|
---|
183 | + return OBJECT_TO_JSVAL(obj);
|
---|
184 | +}
|
---|
185 | \ No newline at end of file
|
---|