Index: source/main.cpp =================================================================== --- source/main.cpp +++ source/main.cpp @@ -76,7 +76,7 @@ #include "graphics/TextureManager.h" #include "gui/GUIManager.h" #include "renderer/Renderer.h" -#include "rlinterface/RLInterface.cpp" +#include "rlinterface/RLInterface.h" #include "scriptinterface/ScriptEngine.h" #include "simulation2/Simulation2.h" #include "simulation2/system/TurnManager.h" Index: source/rlinterface/RLInterface.h =================================================================== --- source/rlinterface/RLInterface.h +++ source/rlinterface/RLInterface.h @@ -23,51 +23,60 @@ #include #include -struct ScenarioConfig { +struct ScenarioConfig +{ bool saveReplay; player_id_t playerID; std::string content; }; -struct Command { +struct RLGameCommand +{ int playerID; std::string json_cmd; }; -enum GameMessageType { Reset, Commands }; +enum class GameMessageType { Reset, Commands }; struct GameMessage { GameMessageType type; - std::vector commands; + std::vector commands; }; extern void EndGame(); struct mg_context; -const static std::string EMPTY_STATE; +/** + * Implements an interface providing fundamental capabilities required for reinforcement + * learning (over HTTP). + * + * This consists of enabling an external script to configure the scenario (via Reset) and + * then step the game engine manually and apply player actions (via Step). The interface + * also supports querying unit templates to provide information about max health and other + * potentially relevant game state information. + */ class RLInterface { - public: - std::string Step(const std::vector commands); + std::string Step(const std::vector& commands); std::string Reset(const ScenarioConfig* scenario); - std::vector GetTemplates(const std::vector names) const; + std::vector GetTemplates(const std::vector& names) const; void EnableHTTP(const char* server_address); - std::string SendGameMessage(const GameMessage msg); + std::string SendGameMessage(const GameMessage& msg); bool TryGetGameMessage(GameMessage& msg); void TryApplyMessage(); - std::string GetGameState(); - bool IsGameRunning(); + std::string GetGameState() const; + bool IsGameRunning() const; private: mg_context* m_MgContext = nullptr; const GameMessage* m_GameMessage = nullptr; std::string m_GameState; bool m_NeedsGameState = false; - mutable std::mutex m_lock; - std::mutex m_msgLock; - std::condition_variable m_msgApplied; + mutable std::mutex m_Lock; + std::mutex m_MsgLock; + std::condition_variable m_MsgApplied; ScenarioConfig m_ScenarioConfig; }; Index: source/rlinterface/RLInterface.cpp =================================================================== --- source/rlinterface/RLInterface.cpp +++ source/rlinterface/RLInterface.cpp @@ -42,32 +42,32 @@ // Interactions with the game engine (g_Game) must be done in the main // thread as there are specific checks for this. We will pass our commands // to the main thread to be applied -std::string RLInterface::SendGameMessage(const GameMessage msg) +std::string RLInterface::SendGameMessage(const GameMessage& msg) { - std::unique_lock msgLock(m_msgLock); + std::unique_lock msgLock(m_MsgLock); m_GameMessage = &msg; - m_msgApplied.wait(msgLock); + m_MsgApplied.wait(msgLock); return m_GameState; } -std::string RLInterface::Step(const std::vector commands) +std::string RLInterface::Step(const std::vector& commands) { - std::lock_guard lock(m_lock); + std::lock_guard lock(m_Lock); GameMessage msg = { GameMessageType::Commands, commands }; return SendGameMessage(msg); } std::string RLInterface::Reset(const ScenarioConfig* scenario) { - std::lock_guard lock(m_lock); + std::lock_guard lock(m_Lock); m_ScenarioConfig = *scenario; struct GameMessage msg = { GameMessageType::Reset }; return SendGameMessage(msg); } -std::vector RLInterface::GetTemplates(const std::vector names) const +std::vector RLInterface::GetTemplates(const std::vector& names) const { - std::lock_guard lock(m_lock); + std::lock_guard lock(m_Lock); CSimulation2& simulation = *g_Game->GetSimulation2(); CmpPtr cmpTemplateManager(simulation.GetSimContext().GetSystemEntity()); @@ -169,11 +169,11 @@ std::string postData(buf.get(), bufSize); std::stringstream postStream(postData); std::string line; - std::vector commands; + std::vector commands; while (std::getline(postStream, line, '\n')) { - Command cmd; + RLGameCommand cmd; const std::size_t splitPos = line.find(";"); if (splitPos != std::string::npos) { @@ -275,17 +275,18 @@ void RLInterface::TryApplyMessage() { + const static std::string EMPTY_STATE; const bool nonVisual = !g_GUI; const bool isGameStarted = g_Game && g_Game->IsGameStarted(); if (m_NeedsGameState && isGameStarted) { m_GameState = GetGameState(); - m_msgApplied.notify_one(); - m_msgLock.unlock(); + m_MsgApplied.notify_one(); + m_MsgLock.unlock(); m_NeedsGameState = false; } - if (m_msgLock.try_lock()) + if (m_MsgLock.try_lock()) { GameMessage msg; if (TryGetGameMessage(msg)) { @@ -311,8 +312,8 @@ LDR_NonprogressiveLoad(); ENSURE(g_Game->ReallyStartGame() == PSRETURN_OK); m_GameState = GetGameState(); - m_msgApplied.notify_one(); - m_msgLock.unlock(); + m_MsgApplied.notify_one(); + m_MsgLock.unlock(); } else { @@ -335,14 +336,14 @@ if (!g_Game) { m_GameState = EMPTY_STATE; - m_msgApplied.notify_one(); - m_msgLock.unlock(); + m_MsgApplied.notify_one(); + m_MsgLock.unlock(); return; } const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); CLocalTurnManager* turnMgr = static_cast(g_Game->GetTurnManager()); - for (Command command : msg.commands) + for (const RLGameCommand& command : msg.commands) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); @@ -362,18 +363,18 @@ g_Game->Update(deltaRealTime); m_GameState = GetGameState(); - m_msgApplied.notify_one(); - m_msgLock.unlock(); + m_MsgApplied.notify_one(); + m_MsgLock.unlock(); break; } } } else - m_msgLock.unlock(); + m_MsgLock.unlock(); } } -std::string RLInterface::GetGameState() +std::string RLInterface::GetGameState() const { const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); const CSimContext simContext = g_Game->GetSimulation2()->GetSimContext(); @@ -385,7 +386,7 @@ return scriptInterface.StringifyJSON(&state, false); } -bool RLInterface::IsGameRunning() +bool RLInterface::IsGameRunning() const { return !!g_Game; }