Ticket #969: AI Territories.patch
File AI Territories.patch, 4.8 KB (added by , 13 years ago) |
---|
-
binaries/data/mods/public/simulation/ai/common-api/base.js
84 84 this.timeElapsed = state.timeElapsed; 85 85 this.map = state.map; 86 86 this.passabilityClasses = state.passabilityClasses; 87 this.territoryMap = state.territoryMap; 87 88 88 89 Engine.ProfileStop(); 89 90 … … 97 98 delete this.timeElapsed; 98 99 delete this.map; 99 100 delete this.passabilityClasses; 101 delete this.territoryMap; 100 102 }; 101 103 102 104 BaseAI.prototype.ApplyEntitiesDelta = function(state) -
source/simulation2/components/CCmpAIManager.cpp
34 34 #include "simulation2/components/ICmpObstructionManager.h" 35 35 #include "simulation2/components/ICmpRangeManager.h" 36 36 #include "simulation2/components/ICmpTemplateManager.h" 37 #include "simulation2/components/ICmpTerritoryManager.h" 37 38 #include "simulation2/helpers/Grid.h" 38 39 #include "simulation2/serialization/DebugSerializer.h" 39 40 #include "simulation2/serialization/StdDeserializer.h" … … 286 287 return true; 287 288 } 288 289 289 void StartComputation(const shared_ptr<ScriptInterface::StructuredClone>& gameState, const Grid<u16>& map )290 void StartComputation(const shared_ptr<ScriptInterface::StructuredClone>& gameState, const Grid<u16>& map, const Grid<u8>& territoryMap) 290 291 { 291 292 ENSURE(m_CommandsComputed); 292 293 … … 300 301 m_GameStateMapVal = CScriptValRooted(cx, ScriptInterface::ToJSVal(cx, m_GameStateMap)); 301 302 } 302 303 304 if (true) //territoryMap.m_DirtyID != m_TerritoryMap.m_DirtyID) couldn't manage to get this working the ID's always matched 305 // I tried debugging and the DirtyID of territoryMap did not change 306 { 307 PROFILE("territoryMap"); 308 m_TerritoryMap = territoryMap; 309 310 JSContext* cx = m_ScriptInterface.GetContext(); 311 m_TerritoryMapVal = CScriptValRooted(cx, ScriptInterface::ToJSVal(cx, m_TerritoryMap)); 312 } 313 314 303 315 m_CommandsComputed = false; 304 316 } 305 317 … … 427 439 428 440 void PerformComputation() 429 441 { 442 430 443 // Deserialize the game state, to pass to the AI's HandleMessage 431 444 CScriptVal state; 432 445 { 433 446 PROFILE("AI compute read state"); 434 447 state = m_ScriptInterface.ReadStructuredClone(m_GameState); 435 448 m_ScriptInterface.SetProperty(state.get(), "map", m_GameStateMapVal, true); 449 m_ScriptInterface.SetProperty(state.get(), "territoryMap", m_TerritoryMapVal, true); 436 450 } 437 451 438 452 // It would be nice to do … … 469 483 shared_ptr<ScriptInterface::StructuredClone> m_GameState; 470 484 Grid<u16> m_GameStateMap; 471 485 CScriptValRooted m_GameStateMapVal; 486 Grid<u8> m_TerritoryMap; 487 CScriptValRooted m_TerritoryMapVal; 472 488 473 489 bool m_CommandsComputed; 474 490 }; … … 575 591 if (!cmpPathfinder.null()) 576 592 map = &cmpPathfinder->GetPassabilityGrid(); 577 593 594 // Get the territory data 595 Grid<u8> dummyGrid2; 596 const Grid<u8>* territoryMap = &dummyGrid2; 597 CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(GetSimContext(), SYSTEM_ENTITY); 598 if (!cmpTerritoryManager.null())// || !cmpTerritoryManager->NeedUpdate(&m_TerritoriesDirtyID)) - Don't think this is appropriate here so have ignored it 599 territoryMap = &cmpTerritoryManager->GetTerritoryGrid(); 600 578 601 LoadPathfinderClasses(state); 579 602 580 m_Worker.StartComputation(scriptInterface.WriteStructuredClone(state.get()), *map );603 m_Worker.StartComputation(scriptInterface.WriteStructuredClone(state.get()), *map, *territoryMap); 581 604 } 582 605 583 606 virtual void PushCommands() -
source/simulation2/scripting/EngineScriptConversions.cpp
236 236 237 237 return OBJECT_TO_JSVAL(obj); 238 238 } 239 240 template<> jsval ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, const Grid<u8>& val) 241 { 242 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL); 243 if (!obj) 244 return JSVAL_VOID; 245 246 jsuint len = val.m_W * val.m_H; 247 JSObject *darray = js_CreateTypedArray(cx, js::TypedArray::TYPE_INT8, len); 248 if (!darray) 249 return JSVAL_VOID; 250 251 js::TypedArray *tdest = js::TypedArray::fromJSObject(darray); 252 ENSURE(tdest->byteLength == len*sizeof(u8)); 253 254 memcpy(tdest->data, val.m_Data, tdest->byteLength); 255 256 jsval w = ToJSVal(cx, val.m_W); 257 jsval h = ToJSVal(cx, val.m_H); 258 jsval data = OBJECT_TO_JSVAL(darray); 259 260 JS_SetProperty(cx, obj, "width", &w); 261 JS_SetProperty(cx, obj, "height", &h); 262 JS_SetProperty(cx, obj, "data", &data); 263 264 return OBJECT_TO_JSVAL(obj); 265 } 266 No newline at end of file