Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpAIManager.cpp
Show All 26 Lines | |||||
#include "lib/tex/tex.h" | #include "lib/tex/tex.h" | ||||
#include "lib/allocators/shared_ptr.h" | #include "lib/allocators/shared_ptr.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "ps/scripting/JSInterface_VFS.h" | #include "ps/scripting/JSInterface_VFS.h" | ||||
#include "ps/TemplateLoader.h" | #include "ps/TemplateLoader.h" | ||||
#include "ps/Util.h" | #include "ps/Util.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include "simulation2/components/ICmpAIInterface.h" | #include "simulation2/components/ICmpAIInterface.h" | ||||
#include "simulation2/components/ICmpCommandQueue.h" | #include "simulation2/components/ICmpCommandQueue.h" | ||||
#include "simulation2/components/ICmpObstructionManager.h" | #include "simulation2/components/ICmpObstructionManager.h" | ||||
#include "simulation2/components/ICmpRangeManager.h" | #include "simulation2/components/ICmpRangeManager.h" | ||||
#include "simulation2/components/ICmpTemplateManager.h" | #include "simulation2/components/ICmpTemplateManager.h" | ||||
#include "simulation2/components/ICmpTerritoryManager.h" | #include "simulation2/components/ICmpTerritoryManager.h" | ||||
#include "simulation2/helpers/HierarchicalPathfinder.h" | #include "simulation2/helpers/HierarchicalPathfinder.h" | ||||
#include "simulation2/helpers/LongPathfinder.h" | #include "simulation2/helpers/LongPathfinder.h" | ||||
▲ Show 20 Lines • Show All 182 Lines • ▼ Show 20 Lines | CAIWorker() : | ||||
m_ScriptInterface->ReplaceNondeterministicRNG(m_RNG); | m_ScriptInterface->ReplaceNondeterministicRNG(m_RNG); | ||||
m_ScriptInterface->SetCallbackData(static_cast<void*> (this)); | m_ScriptInterface->SetCallbackData(static_cast<void*> (this)); | ||||
m_SerializablePrototypes->init(); | m_SerializablePrototypes->init(); | ||||
JS_AddExtraGCRootsTracer(m_ScriptInterface->GetJSRuntime(), Trace, this); | JS_AddExtraGCRootsTracer(m_ScriptInterface->GetJSRuntime(), Trace, this); | ||||
m_ScriptInterface->RegisterFunction<void, int, JS::HandleValue, CAIWorker::PostCommand>("PostCommand"); | // FixMe | ||||
m_ScriptInterface->RegisterFunction<void, std::wstring, CAIWorker::IncludeModule>("IncludeModule"); | ScriptWrapper::WrapAndRegister<decltype(&CAIWorker::Script_PostCommand), &CAIWorker::Script_PostCommand>(*m_ScriptInterface, "PostCommand"); | ||||
m_ScriptInterface->RegisterFunction<void, CAIWorker::ExitProgram>("Exit"); | ScriptWrapper::WrapAndRegister<decltype(&CAIWorker::IncludeModule), &CAIWorker::IncludeModule>(*m_ScriptInterface, "IncludeModule"); | ||||
ScriptWrapper::WrapAndRegister<decltype(&CAIWorker::ExitProgram), &CAIWorker::ExitProgram>(*m_ScriptInterface, "Exit"); | |||||
m_ScriptInterface->RegisterFunction<JS::Value, JS::HandleValue, JS::HandleValue, pass_class_t, CAIWorker::ComputePath>("ComputePath"); | ScriptWrapper::WrapAndRegister<decltype(&CAIWorker::Script_ComputePath), &CAIWorker::Script_ComputePath>(*m_ScriptInterface, "ComputePath"); | ||||
m_ScriptInterface->RegisterFunction<void, std::wstring, std::vector<u32>, u32, u32, u32, CAIWorker::DumpImage>("DumpImage"); | ScriptWrapper::WrapAndRegister<decltype(&CAIWorker::DumpImage), &CAIWorker::DumpImage>(*m_ScriptInterface, "DumpImage"); | ||||
m_ScriptInterface->RegisterFunction<CParamNode, std::string, CAIWorker::GetTemplate>("GetTemplate"); | ScriptWrapper::WrapAndRegister<decltype(&CAIWorker::Script_GetTemplate), &CAIWorker::Script_GetTemplate>(*m_ScriptInterface, "GetTemplate"); | ||||
JSI_VFS::RegisterScriptFunctions_Simulation(*(m_ScriptInterface.get())); | JSI_VFS::RegisterScriptFunctions_Simulation(*(m_ScriptInterface.get())); | ||||
// Globalscripts may use VFS script functions | // Globalscripts may use VFS script functions | ||||
m_ScriptInterface->LoadGlobalScripts(); | m_ScriptInterface->LoadGlobalScripts(); | ||||
} | } | ||||
~CAIWorker() | ~CAIWorker() | ||||
Show All 34 Lines | public: | ||||
static void IncludeModule(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name) | static void IncludeModule(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name) | ||||
{ | { | ||||
ENSURE(pCxPrivate->pCBData); | ENSURE(pCxPrivate->pCBData); | ||||
CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | ||||
self->LoadScripts(name); | self->LoadScripts(name); | ||||
} | } | ||||
static void PostCommand(ScriptInterface::CxPrivate* pCxPrivate, int playerid, JS::HandleValue cmd) | static void Script_PostCommand(ScriptInterface::CxPrivate* pCxPrivate, int playerid, JS::HandleValue cmd) | ||||
{ | { | ||||
ENSURE(pCxPrivate->pCBData); | ENSURE(pCxPrivate->pCBData); | ||||
CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | ||||
self->PostCommand(playerid, cmd); | self->PostCommand(playerid, cmd); | ||||
} | } | ||||
void PostCommand(int playerid, JS::HandleValue cmd) | void PostCommand(int playerid, JS::HandleValue cmd) | ||||
{ | { | ||||
for (size_t i=0; i<m_Players.size(); i++) | for (size_t i=0; i<m_Players.size(); i++) | ||||
{ | { | ||||
if (m_Players[i]->m_Player == playerid) | if (m_Players[i]->m_Player == playerid) | ||||
{ | { | ||||
m_Players[i]->m_Commands.push_back(m_ScriptInterface->WriteStructuredClone(cmd)); | m_Players[i]->m_Commands.push_back(m_ScriptInterface->WriteStructuredClone(cmd)); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
LOGERROR("Invalid playerid in PostCommand!"); | LOGERROR("Invalid playerid in PostCommand!"); | ||||
} | } | ||||
static JS::Value ComputePath(ScriptInterface::CxPrivate* pCxPrivate, | static JS::Value Script_ComputePath(ScriptInterface::CxPrivate* pCxPrivate, | ||||
JS::HandleValue position, JS::HandleValue goal, pass_class_t passClass) | JS::HandleValue position, JS::HandleValue goal, pass_class_t passClass) | ||||
{ | { | ||||
ENSURE(pCxPrivate->pCBData); | ENSURE(pCxPrivate->pCBData); | ||||
CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | ||||
JSContext* cx(self->m_ScriptInterface->GetContext()); | JSContext* cx(self->m_ScriptInterface->GetContext()); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
CFixedVector2D pos, goalPos; | CFixedVector2D pos, goalPos; | ||||
Show All 14 Lines | void ComputePath(const CFixedVector2D& pos, const CFixedVector2D& goal, pass_class_t passClass, std::vector<CFixedVector2D>& waypoints) | ||||
WaypointPath ret; | WaypointPath ret; | ||||
PathGoal pathGoal = { PathGoal::POINT, goal.X, goal.Y }; | PathGoal pathGoal = { PathGoal::POINT, goal.X, goal.Y }; | ||||
m_LongPathfinder.ComputePath(m_HierarchicalPathfinder, pos.X, pos.Y, pathGoal, passClass, ret); | m_LongPathfinder.ComputePath(m_HierarchicalPathfinder, pos.X, pos.Y, pathGoal, passClass, ret); | ||||
for (Waypoint& wp : ret.m_Waypoints) | for (Waypoint& wp : ret.m_Waypoints) | ||||
waypoints.emplace_back(wp.x, wp.z); | waypoints.emplace_back(wp.x, wp.z); | ||||
} | } | ||||
static CParamNode GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name) | static CParamNode Script_GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name) | ||||
{ | { | ||||
ENSURE(pCxPrivate->pCBData); | ENSURE(pCxPrivate->pCBData); | ||||
CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | CAIWorker* self = static_cast<CAIWorker*> (pCxPrivate->pCBData); | ||||
return self->GetTemplate(name); | return self->GetTemplate(name); | ||||
} | } | ||||
CParamNode GetTemplate(const std::string& name) | CParamNode GetTemplate(const std::string& name) | ||||
{ | { | ||||
if (!m_TemplateLoader.TemplateExists(name)) | if (!m_TemplateLoader.TemplateExists(name)) | ||||
return CParamNode(false); | return CParamNode(false); | ||||
return m_TemplateLoader.GetTemplateFileData(name).GetChild("Entity"); | return m_TemplateLoader.GetTemplateFileData(name).GetChild("Entity"); | ||||
} | } | ||||
static void ExitProgram(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | static void ExitProgram() | ||||
{ | { | ||||
QuitEngine(); | QuitEngine(); | ||||
} | } | ||||
/** | /** | ||||
* Debug function for AI scripts to dump 2D array data (e.g. terrain tile weights). | * Debug function for AI scripts to dump 2D array data (e.g. terrain tile weights). | ||||
*/ | */ | ||||
static void DumpImage(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& name, const std::vector<u32>& data, u32 w, u32 h, u32 max) | static void DumpImage(const std::wstring& name, const std::vector<u32>& data, u32 w, u32 h, u32 max) | ||||
{ | { | ||||
// TODO: this is totally not threadsafe. | // TODO: this is totally not threadsafe. | ||||
VfsPath filename = L"screenshots/aidump/" + name; | VfsPath filename = L"screenshots/aidump/" + name; | ||||
if (data.size() != w*h) | if (data.size() != w*h) | ||||
{ | { | ||||
debug_warn(L"DumpImage: data size doesn't match w*h"); | debug_warn(L"DumpImage: data size doesn't match w*h"); | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 832 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator