Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/scripting/JSInterface_Simulation.cpp
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | JS::Value JSI_Simulation::GuiInterfaceCall(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name, JS::HandleValue data) | ||||
CSimulation2* sim = g_Game->GetSimulation2(); | CSimulation2* sim = g_Game->GetSimulation2(); | ||||
ENSURE(sim); | ENSURE(sim); | ||||
CmpPtr<ICmpGuiInterface> cmpGuiInterface(*sim, SYSTEM_ENTITY); | CmpPtr<ICmpGuiInterface> cmpGuiInterface(*sim, SYSTEM_ENTITY); | ||||
if (!cmpGuiInterface) | if (!cmpGuiInterface) | ||||
return JS::UndefinedValue(); | return JS::UndefinedValue(); | ||||
JSContext* cxSim = sim->GetScriptInterface().GetContext(); | ScriptInterface::Request rqSim(sim->GetScriptInterface()); | ||||
JSAutoRequest rqSim(cxSim); | JS::RootedValue arg(rqSim.cx, sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), data)); | ||||
JS::RootedValue arg(cxSim, sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), data)); | JS::RootedValue ret(rqSim.cx); | ||||
JS::RootedValue ret(cxSim); | |||||
cmpGuiInterface->ScriptCall(g_Game->GetViewedPlayerID(), name, arg, &ret); | cmpGuiInterface->ScriptCall(g_Game->GetViewedPlayerID(), name, arg, &ret); | ||||
return pCxPrivate->pScriptInterface->CloneValueFromOtherContext(sim->GetScriptInterface(), ret); | return pCxPrivate->pScriptInterface->CloneValueFromOtherContext(sim->GetScriptInterface(), ret); | ||||
} | } | ||||
void JSI_Simulation::PostNetworkCommand(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue cmd) | void JSI_Simulation::PostNetworkCommand(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue cmd) | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
return; | return; | ||||
CSimulation2* sim = g_Game->GetSimulation2(); | CSimulation2* sim = g_Game->GetSimulation2(); | ||||
ENSURE(sim); | ENSURE(sim); | ||||
CmpPtr<ICmpCommandQueue> cmpCommandQueue(*sim, SYSTEM_ENTITY); | CmpPtr<ICmpCommandQueue> cmpCommandQueue(*sim, SYSTEM_ENTITY); | ||||
if (!cmpCommandQueue) | if (!cmpCommandQueue) | ||||
return; | return; | ||||
JSContext* cxSim = sim->GetScriptInterface().GetContext(); | ScriptInterface::Request rqSim(sim->GetScriptInterface()); | ||||
JSAutoRequest rqSim(cxSim); | JS::RootedValue cmd2(rqSim.cx, 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(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | ||||
{ | { | ||||
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); | ||||
Show All 36 Lines | |||||
JS::Value JSI_Simulation::GetEdgesOfStaticObstructionsOnScreenNearTo(ScriptInterface::CxPrivate* pCxPrivate, entity_pos_t x, entity_pos_t z) | JS::Value JSI_Simulation::GetEdgesOfStaticObstructionsOnScreenNearTo(ScriptInterface::CxPrivate* pCxPrivate, entity_pos_t x, entity_pos_t z) | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
return JS::UndefinedValue(); | return JS::UndefinedValue(); | ||||
CSimulation2* sim = g_Game->GetSimulation2(); | CSimulation2* sim = g_Game->GetSimulation2(); | ||||
ENSURE(sim); | ENSURE(sim); | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | ScriptInterface::Request rq(pCxPrivate); | ||||
JSAutoRequest rq(cx); | JS::RootedValue edgeList(rq.cx); | ||||
JS::RootedValue edgeList(cx); | ScriptInterface::CreateArray(rq, &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(pCxPrivate); | ||||
for (entity_id_t entity : entities) | for (entity_id_t entity : entities) | ||||
Show All 17 Lines | std::array<CFixedVector2D, 4> corners = { | ||||
CFixedVector2D(halfSize.X, -halfSize.Y) | CFixedVector2D(halfSize.X, -halfSize.Y) | ||||
}; | }; | ||||
fixed angle = cmpPosition->GetRotation().Y; | fixed angle = cmpPosition->GetRotation().Y; | ||||
for (CFixedVector2D& corner : corners) | for (CFixedVector2D& corner : corners) | ||||
corner = corner.Rotate(angle) + cmpPosition->GetPosition2D(); | corner = corner.Rotate(angle) + cmpPosition->GetPosition2D(); | ||||
for (size_t i = 0; i < corners.size(); ++i) | for (size_t i = 0; i < corners.size(); ++i) | ||||
{ | { | ||||
JS::RootedValue edge(cx); | JS::RootedValue edge(rq.cx); | ||||
const CFixedVector2D& corner = corners[i]; | const CFixedVector2D& corner = corners[i]; | ||||
const CFixedVector2D& nextCorner = corners[(i + 1) % corners.size()]; | const CFixedVector2D& nextCorner = corners[(i + 1) % corners.size()]; | ||||
fixed distanceToEdge = | fixed distanceToEdge = | ||||
Geometry::DistanceToSegment(entityPos, corner, nextCorner); | Geometry::DistanceToSegment(entityPos, corner, nextCorner); | ||||
if (distanceToEdge.ToFloat() > distanceThreshold) | if (distanceToEdge.ToFloat() > distanceThreshold) | ||||
continue; | continue; | ||||
CFixedVector2D normal = -(nextCorner - corner).Perpendicular(); | CFixedVector2D normal = -(nextCorner - corner).Perpendicular(); | ||||
normal.Normalize(); | normal.Normalize(); | ||||
ScriptInterface::CreateObject( | ScriptInterface::CreateObject( | ||||
cx, | rq, | ||||
&edge, | &edge, | ||||
"begin", corner, | "begin", corner, | ||||
"end", nextCorner, | "end", nextCorner, | ||||
"angle", angle, | "angle", angle, | ||||
"normal", normal, | "normal", normal, | ||||
"order", "cw"); | "order", "cw"); | ||||
pCxPrivate->pScriptInterface->SetPropertyInt(edgeList, edgeListIndex++, edge); | pCxPrivate->pScriptInterface->SetPropertyInt(edgeList, edgeListIndex++, edge); | ||||
Show All 35 Lines |
Wildfire Games · Phabricator