Ticket #969: AI Territories.patch

File AI Territories.patch, 4.8 KB (added by Jonathan Waller, 13 years ago)
  • binaries/data/mods/public/simulation/ai/common-api/base.js

     
    8484    this.timeElapsed = state.timeElapsed;
    8585    this.map = state.map;
    8686    this.passabilityClasses = state.passabilityClasses;
     87    this.territoryMap = state.territoryMap;
    8788
    8889    Engine.ProfileStop();
    8990
     
    9798    delete this.timeElapsed;
    9899    delete this.map;
    99100    delete this.passabilityClasses;
     101    delete this.territoryMap;
    100102};
    101103
    102104BaseAI.prototype.ApplyEntitiesDelta = function(state)
  • source/simulation2/components/CCmpAIManager.cpp

     
    3434#include "simulation2/components/ICmpObstructionManager.h"
    3535#include "simulation2/components/ICmpRangeManager.h"
    3636#include "simulation2/components/ICmpTemplateManager.h"
     37#include "simulation2/components/ICmpTerritoryManager.h"
    3738#include "simulation2/helpers/Grid.h"
    3839#include "simulation2/serialization/DebugSerializer.h"
    3940#include "simulation2/serialization/StdDeserializer.h"
     
    286287        return true;
    287288    }
    288289
    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)
    290291    {
    291292        ENSURE(m_CommandsComputed);
    292293
     
    300301            m_GameStateMapVal = CScriptValRooted(cx, ScriptInterface::ToJSVal(cx, m_GameStateMap));
    301302        }
    302303
     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
    303315        m_CommandsComputed = false;
    304316    }
    305317
     
    427439
    428440    void PerformComputation()
    429441    {
     442
    430443        // Deserialize the game state, to pass to the AI's HandleMessage
    431444        CScriptVal state;
    432445        {
    433446            PROFILE("AI compute read state");
    434447            state = m_ScriptInterface.ReadStructuredClone(m_GameState);
    435448            m_ScriptInterface.SetProperty(state.get(), "map", m_GameStateMapVal, true);
     449            m_ScriptInterface.SetProperty(state.get(), "territoryMap", m_TerritoryMapVal, true);
    436450        }
    437451
    438452        // It would be nice to do
     
    469483    shared_ptr<ScriptInterface::StructuredClone> m_GameState;
    470484    Grid<u16> m_GameStateMap;
    471485    CScriptValRooted m_GameStateMapVal;
     486    Grid<u8> m_TerritoryMap;
     487    CScriptValRooted m_TerritoryMapVal;
    472488
    473489    bool m_CommandsComputed;
    474490};
     
    575591        if (!cmpPathfinder.null())
    576592            map = &cmpPathfinder->GetPassabilityGrid();
    577593
     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
    578601        LoadPathfinderClasses(state);
    579602
    580         m_Worker.StartComputation(scriptInterface.WriteStructuredClone(state.get()), *map);
     603        m_Worker.StartComputation(scriptInterface.WriteStructuredClone(state.get()), *map, *territoryMap);
    581604    }
    582605
    583606    virtual void PushCommands()
  • source/simulation2/scripting/EngineScriptConversions.cpp

     
    236236
    237237    return OBJECT_TO_JSVAL(obj);
    238238}
     239
     240template<> 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