Ticket #4046: AiProxyOptim.patch

File AiProxyOptim.patch, 4.2 KB (added by wraitii, 8 years ago)

git patch for the above branch

  • source/simulation2/Simulation2.cpp

    diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp
    index 8be12e1..a1852f6 100644
    a b void CSimulation2::InitGame(JS::HandleValue data)  
    678678    JSAutoRequest rq(cx);
    679679    JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject());
    680680    GetScriptInterface().CallFunctionVoid(global, "InitGame", data);
     681
     682    CmpPtr<ICmpAIManager> cmpAIManager(m->m_SimContext, SYSTEM_ENTITY);
     683    if (!cmpAIManager->HasActiveAIs())
     684        m->m_ComponentManager.SkipAiProxy();
    681685}
    682686
    683687void CSimulation2::Update(int turnLength)
  • source/simulation2/components/CCmpAIManager.cpp

    diff --git a/source/simulation2/components/CCmpAIManager.cpp b/source/simulation2/components/CCmpAIManager.cpp
    index 2a891b7..8232474 100644
    a b public:  
    11591159        }
    11601160    }
    11611161
     1162    virtual bool HasActiveAIs()
     1163    {
     1164        return m_Worker.getPlayerSize() > 0;
     1165    }
     1166
    11621167private:
    11631168    std::vector<std::string> m_TemplateNames;
    11641169    size_t m_TemplateLoadedIdx;
  • source/simulation2/components/ICmpAIManager.h

    diff --git a/source/simulation2/components/ICmpAIManager.h b/source/simulation2/components/ICmpAIManager.h
    index 97d46f7..5f97912 100644
    a b public:  
    4848    virtual void PushCommands() = 0;
    4949
    5050    /**
     51     * Returns true if there are currently any AI registered with the AiManager
     52     */
     53    virtual bool HasActiveAIs() = 0;
     54
     55    /**
    5156     * Returns a vector of {"id":"value-for-AddPlayer", "name":"Human readable name"}
    5257     * objects, based on all the available AI scripts.
    5358     */
  • source/simulation2/system/ComponentManager.cpp

    diff --git a/source/simulation2/system/ComponentManager.cpp b/source/simulation2/system/ComponentManager.cpp
    index 7ea7e2e..5b72abc 100644
    a b  
    2626
    2727#include "simulation2/MessageTypes.h"
    2828#include "simulation2/components/ICmpTemplateManager.h"
     29#include "simulation2/components/ICmpAiManager.h"
    2930
    3031#include "lib/utf8.h"
    3132#include "ps/CLogger.h"
    void CComponentManager::SetRNGSeed(u32 seed)  
    543544    m_RNG.seed(seed);
    544545}
    545546
     547void CComponentManager::SkipAiProxy()
     548{
     549    // don't actually delete the components, just make AIProxy not subscribe to anything
     550    for (auto& messageType : m_LocalMessageSubscriptions)
     551    {
     552        // 49 is AIProxy CID
     553        std::vector<ComponentTypeId>::iterator aiproxy = std::find(messageType.second.begin(),messageType.second.end(), 49);
     554        if (aiproxy != messageType.second.end())
     555        {
     556            // pop and swap, I do not believe we actually care about the order here.
     557            std::swap(*aiproxy, messageType.second.back());
     558            messageType.second.pop_back();
     559        }
     560    }
     561}
     562
     563
    546564void CComponentManager::RegisterComponentType(InterfaceId iid, ComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc,
    547565        const char* name, const std::string& schema)
    548566{
    entity_id_t CComponentManager::AddEntity(const std::wstring& templateName, entit  
    874892            continue;
    875893
    876894        CComponentManager::ComponentTypeId cid = LookupCID(it->first);
     895
    877896        if (cid == CID__Invalid)
    878897        {
    879898            LOGERROR("Unrecognised component type name '%s' in entity template '%s'", it->first, utf8_from_wstring(templateName));
  • source/simulation2/system/ComponentManager.h

    diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h
    index 26a518c..4097ea6 100644
    a b public:  
    301301     */
    302302    void SetRNGSeed(u32 seed);
    303303
     304    /**
     305     * Tells the Component Manager to remove subscriptions of AIProxy, effectively acting like it didn't exist
     306     * This is called by the simulation after game initialisation if there are no AI players present
     307     * Since AIProxy can end up taking several ms (>3/4) per turn doing nothing useful otherwise
     308     * Intended to make MP games faster.
     309     */
     310    void SkipAiProxy();
     311
    304312    // Various state serialization functions:
    305313    bool ComputeStateHash(std::string& outHash, bool quick);
    306314    bool DumpDebugState(std::ostream& stream, bool includeDebugInfo);
    private:  
    385393
    386394    boost::rand48 m_RNG;
    387395
     396    bool m_SkipAiProxy = false;
     397
    388398    friend class TestComponentManager;
    389399};
    390400