Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/scripting/JSInterface_Simulation.cpp
Show All 23 Lines | |||||
#include "ps/GameSetup/Config.h" | #include "ps/GameSetup/Config.h" | ||||
#include "ps/Pyrogenesis.h" | #include "ps/Pyrogenesis.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/ICmpSelectable.h" | #include "simulation2/components/ICmpSelectable.h" | ||||
#include "simulation2/helpers/Selection.h" | #include "simulation2/helpers/Selection.h" | ||||
#include <fstream> | #include <fstream> | ||||
JS::Value JSI_Simulation::GuiInterfaceCall(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name, JS::HandleValue data) | JS::Value JSI_Simulation::GuiInterfaceCall(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name, JS::HandleValue data) | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | struct StaticObstructionFilter | ||||
{ | { | ||||
ICmpObstruction* cmpObstruction = static_cast<ICmpObstruction*>(cmp); | ICmpObstruction* cmpObstruction = static_cast<ICmpObstruction*>(cmp); | ||||
return cmpObstruction->GetObstructionType() == ICmpObstruction::STATIC; | return cmpObstruction->GetObstructionType() == ICmpObstruction::STATIC; | ||||
} | } | ||||
}; | }; | ||||
return EntitySelection::GetEntitiesWithComponentInRect<StaticObstructionFilter>(*g_Game->GetSimulation2(), IID_Obstruction, *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres); | return EntitySelection::GetEntitiesWithComponentInRect<StaticObstructionFilter>(*g_Game->GetSimulation2(), IID_Obstruction, *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres); | ||||
} | } | ||||
JS::Value JSI_Simulation::GetEdgesOfStaticObstructionsOnScreen(ScriptInterface::CxPrivate* pCxPrivate) | |||||
{ | |||||
if (!g_Game) | |||||
return JS::UndefinedValue(); | |||||
CSimulation2* sim = g_Game->GetSimulation2(); | |||||
ENSURE(sim); | |||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue edgeList(cx); | |||||
ScriptInterface::CreateArray(cx, &edgeList); | |||||
int edgeListIndex = 0; | |||||
std::vector<entity_id_t> entities = GetEntitiesWithStaticObstructionOnScreen(pCxPrivate); | |||||
for (entity_id_t entity : entities) | |||||
{ | |||||
CmpPtr<ICmpObstruction> cmpObstruction(sim->GetSimContext(), entity); | |||||
if (!cmpObstruction) | |||||
continue; | |||||
CmpPtr<ICmpPosition> cmpPosition(sim->GetSimContext(), entity); | |||||
Stan: I guess it's lighter to do that first than the math ? Else you could move it next to where it's… | |||||
Done Inline ActionsIt's easier to check, so I moved it before the size check. vladislavbelov: It's easier to check, so I moved it before the size check. | |||||
if (!cmpPosition || !cmpPosition->IsInWorld()) | |||||
continue; | |||||
CFixedVector2D halfSize = cmpObstruction->GetStaticSize() / 2; | |||||
if (halfSize.X.IsZero() || halfSize.Y.IsZero() || std::max(halfSize.X, halfSize.Y) <= fixed::FromInt(2)) | |||||
continue; | |||||
CFixedVector2D corners[4] = { | |||||
CFixedVector2D(-halfSize.X, -halfSize.Y), | |||||
CFixedVector2D(-halfSize.X, halfSize.Y), | |||||
CFixedVector2D(halfSize.X, halfSize.Y), | |||||
CFixedVector2D(halfSize.X, -halfSize.Y) | |||||
}; | |||||
fixed angle = cmpPosition->GetRotation().Y; | |||||
for (CFixedVector2D& corner : corners) | |||||
corner = corner.Rotate(angle) + cmpPosition->GetPosition2D(); | |||||
for (size_t i = 0; i < 4; ++i) | |||||
{ | |||||
JS::RootedValue edge(cx); | |||||
const CFixedVector2D& corner = corners[i]; | |||||
const CFixedVector2D& nextCorner = corners[(i + 1) & 0x3]; | |||||
CFixedVector2D normal = -(nextCorner - corner).Perpendicular(); | |||||
normal.Normalize(); | |||||
ScriptInterface::CreateObject( | |||||
cx, | |||||
&edge, | |||||
"begin", corner, | |||||
"end", nextCorner, | |||||
"angle", angle, | |||||
"normal", normal, | |||||
"order", std::string("cw")); | |||||
pCxPrivate->pScriptInterface->SetPropertyInt(edgeList, edgeListIndex++, edge); | |||||
Not Done Inline ActionsConvention says ++value but here it would make it start at -1. Stan: Convention says ++value but here it would make it start at -1.
I guess one could also use i and… | |||||
Done Inline ActionsWe use ++index for loops, but it's not about loops, but about independed from loop index. vladislavbelov: We use `++index` for loops, but it's not about loops, but about independed from loop index. | |||||
Not Done Inline ActionsTrue but we also use it for variables. Stan: True but we also use it for variables. | |||||
} | |||||
} | |||||
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(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), 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)); | ||||
Show All 10 Lines | void JSI_Simulation::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
scriptInterface.RegisterFunction<void, JS::HandleValue, &PostNetworkCommand>("PostNetworkCommand"); | scriptInterface.RegisterFunction<void, JS::HandleValue, &PostNetworkCommand>("PostNetworkCommand"); | ||||
scriptInterface.RegisterFunction<void, &DumpSimState>("DumpSimState"); | scriptInterface.RegisterFunction<void, &DumpSimState>("DumpSimState"); | ||||
scriptInterface.RegisterFunction<JS::Value, &GetAIs>("GetAIs"); | scriptInterface.RegisterFunction<JS::Value, &GetAIs>("GetAIs"); | ||||
scriptInterface.RegisterFunction<entity_id_t, int, int, &PickEntityAtPoint>("PickEntityAtPoint"); | scriptInterface.RegisterFunction<entity_id_t, int, int, &PickEntityAtPoint>("PickEntityAtPoint"); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickPlayerEntitiesInRect>("PickPlayerEntitiesInRect"); | scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickPlayerEntitiesInRect>("PickPlayerEntitiesInRect"); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickPlayerEntitiesOnScreen>("PickPlayerEntitiesOnScreen"); | scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickPlayerEntitiesOnScreen>("PickPlayerEntitiesOnScreen"); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen"); | scriptInterface.RegisterFunction<std::vector<entity_id_t>, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen"); | ||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, &GetEntitiesWithStaticObstructionOnScreen>("GetEntitiesWithStaticObstructionOnScreen"); | scriptInterface.RegisterFunction<std::vector<entity_id_t>, &GetEntitiesWithStaticObstructionOnScreen>("GetEntitiesWithStaticObstructionOnScreen"); | ||||
scriptInterface.RegisterFunction<JS::Value, &GetEdgesOfStaticObstructionsOnScreen>("GetEdgesOfStaticObstructionsOnScreen"); | |||||
scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities"); | scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities"); | ||||
scriptInterface.RegisterFunction<void, bool, &SetBoundingBoxDebugOverlay>("SetBoundingBoxDebugOverlay"); | scriptInterface.RegisterFunction<void, bool, &SetBoundingBoxDebugOverlay>("SetBoundingBoxDebugOverlay"); | ||||
} | } |
Wildfire Games · Phabricator
I guess it's lighter to do that first than the math ? Else you could move it next to where it's called.