Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/scripting/JSInterface_Simulation.cpp
Show All 18 Lines | |||||
#include "JSInterface_Simulation.h" | #include "JSInterface_Simulation.h" | ||||
#include "graphics/GameView.h" | #include "graphics/GameView.h" | ||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||
#include "ps/Game.h" | #include "ps/Game.h" | ||||
#include "ps/GameSetup/Config.h" | #include "ps/GameSetup/Config.h" | ||||
#include "ps/Pyrogenesis.h" | #include "ps/Pyrogenesis.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "simulation2/system/Entity.h" | #include "simulation2/system/Entity.h" | ||||
#include "simulation2/components/ICmpAIManager.h" | #include "simulation2/components/ICmpAIManager.h" | ||||
#include "simulation2/components/ICmpCommandQueue.h" | #include "simulation2/components/ICmpCommandQueue.h" | ||||
#include "simulation2/components/ICmpGuiInterface.h" | #include "simulation2/components/ICmpGuiInterface.h" | ||||
#include "simulation2/components/ICmpPosition.h" | #include "simulation2/components/ICmpPosition.h" | ||||
#include "simulation2/components/ICmpSelectable.h" | #include "simulation2/components/ICmpSelectable.h" | ||||
Show All 37 Lines | void JSI_Simulation::PostNetworkCommand(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue cmd) | ||||
JSContext* cxSim = sim->GetScriptInterface().GetContext(); | JSContext* cxSim = sim->GetScriptInterface().GetContext(); | ||||
JSAutoRequest rqSim(cxSim); | JSAutoRequest rqSim(cxSim); | ||||
JS::RootedValue cmd2(cxSim, sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), cmd)); | JS::RootedValue cmd2(cxSim, sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), cmd)); | ||||
cmpCommandQueue->PostNetworkCommand(cmd2); | cmpCommandQueue->PostNetworkCommand(cmd2); | ||||
} | } | ||||
void JSI_Simulation::DumpSimState(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_Simulation::DumpSimState() | ||||
{ | { | ||||
OsPath path = psLogDir()/"sim_dump.txt"; | OsPath path = psLogDir()/"sim_dump.txt"; | ||||
std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); | std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); | ||||
g_Game->GetSimulation2()->DumpDebugState(file); | g_Game->GetSimulation2()->DumpDebugState(file); | ||||
} | } | ||||
entity_id_t JSI_Simulation::PickEntityAtPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y) | entity_id_t JSI_Simulation::PickEntityAtPoint(int x, int y) | ||||
{ | { | ||||
return EntitySelection::PickEntityAtPoint(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x, y, g_Game->GetViewedPlayerID(), false); | return EntitySelection::PickEntityAtPoint(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x, y, g_Game->GetViewedPlayerID(), false); | ||||
} | } | ||||
std::vector<entity_id_t> JSI_Simulation::PickPlayerEntitiesInRect(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x0, int y0, int x1, int y1, int player) | std::vector<entity_id_t> JSI_Simulation::PickPlayerEntitiesInRect(int x0, int y0, int x1, int y1, int player) | ||||
{ | { | ||||
return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x0, y0, x1, y1, player, false); | return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x0, y0, x1, y1, player, false); | ||||
} | } | ||||
std::vector<entity_id_t> JSI_Simulation::PickPlayerEntitiesOnScreen(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int player) | std::vector<entity_id_t> JSI_Simulation::PickPlayerEntitiesOnScreen(int player) | ||||
{ | { | ||||
return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, player, false); | return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, player, false); | ||||
} | } | ||||
std::vector<entity_id_t> JSI_Simulation::PickNonGaiaEntitiesOnScreen(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::vector<entity_id_t> JSI_Simulation::PickNonGaiaEntitiesOnScreen() | ||||
{ | { | ||||
return EntitySelection::PickNonGaiaEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, false); | return EntitySelection::PickNonGaiaEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, false); | ||||
} | } | ||||
std::vector<entity_id_t> JSI_Simulation::GetEntitiesWithStaticObstructionOnScreen(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::vector<entity_id_t> JSI_Simulation::GetEntitiesWithStaticObstructionOnScreen() | ||||
{ | { | ||||
struct StaticObstructionFilter | struct StaticObstructionFilter | ||||
{ | { | ||||
bool operator()(IComponent* cmp) | bool operator()(IComponent* cmp) | ||||
{ | { | ||||
ICmpObstruction* cmpObstruction = static_cast<ICmpObstruction*>(cmp); | ICmpObstruction* cmpObstruction = static_cast<ICmpObstruction*>(cmp); | ||||
return cmpObstruction->GetObstructionType() == ICmpObstruction::STATIC; | return cmpObstruction->GetObstructionType() == ICmpObstruction::STATIC; | ||||
} | } | ||||
Show All 14 Lines | JS::Value JSI_Simulation::GetEdgesOfStaticObstructionsOnScreenNearTo(ScriptInterface::CxPrivate* pCxPrivate, entity_pos_t x, entity_pos_t z) | ||||
JS::RootedValue edgeList(cx); | JS::RootedValue edgeList(cx); | ||||
ScriptInterface::CreateArray(cx, &edgeList); | ScriptInterface::CreateArray(cx, &edgeList); | ||||
int edgeListIndex = 0; | int edgeListIndex = 0; | ||||
float distanceThreshold = 10.0f; | float distanceThreshold = 10.0f; | ||||
CFG_GET_VAL("gui.session.snaptoedgesdistancethreshold", distanceThreshold); | CFG_GET_VAL("gui.session.snaptoedgesdistancethreshold", distanceThreshold); | ||||
CFixedVector2D entityPos(x, z); | CFixedVector2D entityPos(x, z); | ||||
std::vector<entity_id_t> entities = GetEntitiesWithStaticObstructionOnScreen(pCxPrivate); | std::vector<entity_id_t> entities = GetEntitiesWithStaticObstructionOnScreen(); | ||||
for (entity_id_t entity : entities) | for (entity_id_t entity : entities) | ||||
{ | { | ||||
CmpPtr<ICmpObstruction> cmpObstruction(sim->GetSimContext(), entity); | CmpPtr<ICmpObstruction> cmpObstruction(sim->GetSimContext(), entity); | ||||
if (!cmpObstruction) | if (!cmpObstruction) | ||||
continue; | continue; | ||||
CmpPtr<ICmpPosition> cmpPosition(sim->GetSimContext(), entity); | CmpPtr<ICmpPosition> cmpPosition(sim->GetSimContext(), entity); | ||||
if (!cmpPosition || !cmpPosition->IsInWorld()) | if (!cmpPosition || !cmpPosition->IsInWorld()) | ||||
Show All 38 Lines | for (size_t i = 0; i < corners.size(); ++i) | ||||
"order", "cw"); | "order", "cw"); | ||||
pCxPrivate->pScriptInterface->SetPropertyInt(edgeList, edgeListIndex++, edge); | pCxPrivate->pScriptInterface->SetPropertyInt(edgeList, edgeListIndex++, edge); | ||||
} | } | ||||
} | } | ||||
return edgeList; | return edgeList; | ||||
} | } | ||||
std::vector<entity_id_t> JSI_Simulation::PickSimilarPlayerEntities(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& templateName, bool includeOffScreen, bool matchRank, bool allowFoundations) | std::vector<entity_id_t> JSI_Simulation::PickSimilarPlayerEntities(const std::string& templateName, bool includeOffScreen, bool matchRank, bool allowFoundations) | ||||
{ | { | ||||
return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), templateName, g_Game->GetViewedPlayerID(), includeOffScreen, matchRank, false, allowFoundations); | return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), templateName, g_Game->GetViewedPlayerID(), includeOffScreen, matchRank, false, allowFoundations); | ||||
} | } | ||||
JS::Value JSI_Simulation::GetAIs(ScriptInterface::CxPrivate* pCxPrivate) | JS::Value JSI_Simulation::GetAIs(ScriptInterface::CxPrivate* pCxPrivate) | ||||
{ | { | ||||
return ICmpAIManager::GetAIs(*(pCxPrivate->pScriptInterface)); | return ICmpAIManager::GetAIs(*(pCxPrivate->pScriptInterface)); | ||||
} | } | ||||
void JSI_Simulation::SetBoundingBoxDebugOverlay(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), bool enabled) | void JSI_Simulation::SetBoundingBoxDebugOverlay(bool enabled) | ||||
{ | { | ||||
ICmpSelectable::ms_EnableDebugOverlays = enabled; | ICmpSelectable::ms_EnableDebugOverlays = enabled; | ||||
} | } | ||||
void JSI_Simulation::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_Simulation::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, JS::HandleValue, &GuiInterfaceCall>("GuiInterfaceCall"); | #define Register(name) ScriptWrapper::WrapAndRegister<decltype(&name), &name>(scriptInterface, #name); | ||||
scriptInterface.RegisterFunction<void, JS::HandleValue, &PostNetworkCommand>("PostNetworkCommand"); | Register(GuiInterfaceCall); | ||||
scriptInterface.RegisterFunction<void, &DumpSimState>("DumpSimState"); | Register(PostNetworkCommand); | ||||
scriptInterface.RegisterFunction<JS::Value, &GetAIs>("GetAIs"); | Register(DumpSimState); | ||||
scriptInterface.RegisterFunction<entity_id_t, int, int, &PickEntityAtPoint>("PickEntityAtPoint"); | Register(GetAIs); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickPlayerEntitiesInRect>("PickPlayerEntitiesInRect"); | Register(PickEntityAtPoint); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickPlayerEntitiesOnScreen>("PickPlayerEntitiesOnScreen"); | Register(PickPlayerEntitiesInRect); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen"); | Register(PickPlayerEntitiesOnScreen); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, &GetEntitiesWithStaticObstructionOnScreen>("GetEntitiesWithStaticObstructionOnScreen"); | Register(PickNonGaiaEntitiesOnScreen); | ||||
scriptInterface.RegisterFunction<JS::Value, entity_pos_t, entity_pos_t, &GetEdgesOfStaticObstructionsOnScreenNearTo>("GetEdgesOfStaticObstructionsOnScreenNearTo"); | Register(GetEntitiesWithStaticObstructionOnScreen); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities"); | Register(GetEdgesOfStaticObstructionsOnScreenNearTo); | ||||
scriptInterface.RegisterFunction<void, bool, &SetBoundingBoxDebugOverlay>("SetBoundingBoxDebugOverlay"); | Register(PickSimilarPlayerEntities); | ||||
Register(SetBoundingBoxDebugOverlay); | |||||
#undef Register | |||||
} | } |
Wildfire Games · Phabricator