diff --git a/source/graphics/scripting/JSInterface_GameView.cpp b/source/graphics/scripting/JSInterface_GameView.cpp index 5db7770917..739650cb9a 100644 --- a/source/graphics/scripting/JSInterface_GameView.cpp +++ b/source/graphics/scripting/JSInterface_GameView.cpp @@ -19,8 +19,11 @@ #include "JSInterface_GameView.h" +#include "graphics/Camera.h" #include "graphics/GameView.h" +#include "graphics/Terrain.h" #include "ps/Game.h" +#include "ps/World.h" #include "ps/CLogger.h" #include "ps/Profile.h" #include "scriptinterface/ScriptInterface.h" @@ -57,7 +60,7 @@ IMPLEMENT_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); scriptInterface.RegisterFunction("GameView_Get" #NAME "Enabled"); \ scriptInterface.RegisterFunction("GameView_Set" #NAME "Enabled"); -void JSI_GameView::RegisterScriptFunctions(const ScriptInterface& scriptInterface) +void JSI_GameView::RegisterScriptFunctions_Settings(const ScriptInterface& scriptInterface) { REGISTER_BOOLEAN_SCRIPT_SETTING(Culling); REGISTER_BOOLEAN_SCRIPT_SETTING(LockCullCamera); @@ -66,5 +69,119 @@ void JSI_GameView::RegisterScriptFunctions(const ScriptInterface& scriptInterfac #undef REGISTER_BOOLEAN_SCRIPT_SETTING +/** + * Get the current X coordinate of the camera. + */ +float JSI_GameView::CameraGetX(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_Game || !g_Game->GetView()) + return -1; + return g_Game->GetView()->GetCameraX(); +} +/** + * Get the current Z coordinate of the camera. + */ +float JSI_GameView::CameraGetZ(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_Game || !g_Game->GetView()) + return -1; + + return g_Game->GetView()->GetCameraZ(); +} + +/** + * Move camera to a 2D location. + */ +void JSI_GameView::CameraMoveTo(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_pos_t x, entity_pos_t z) +{ + if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView() || !g_Game->GetWorld()->GetTerrain()) + return; + + CTerrain* terrain = g_Game->GetWorld()->GetTerrain(); + + CVector3D target; + target.X = x.ToFloat(); + target.Z = z.ToFloat(); + target.Y = terrain->GetExactGroundLevel(target.X, target.Z); + + g_Game->GetView()->MoveCameraTarget(target); +} + +/** + * Set the camera to look at the given location. + */ +void JSI_GameView::SetCameraTarget(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), float x, float y, float z) +{ + g_Game->GetView()->ResetCameraTarget(CVector3D(x, y, z)); +} + +/** + * Set the data (position, orientation and zoom) of the camera. + */ +void JSI_GameView::SetCameraData(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_pos_t x, entity_pos_t y, entity_pos_t z, entity_pos_t rotx, entity_pos_t roty, entity_pos_t zoom) +{ + if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView() || !g_Game->GetWorld()->GetTerrain()) + return; + + CVector3D Pos = CVector3D(x.ToFloat(), y.ToFloat(), z.ToFloat()); + float RotX = rotx.ToFloat(); + float RotY = roty.ToFloat(); + float Zoom = zoom.ToFloat(); + + g_Game->GetView()->SetCamera(Pos, RotX, RotY, Zoom); +} + +/** + * Start / stop camera following mode. + * @param entityid unit id to follow. If zero, stop following mode + */ +void JSI_GameView::CameraFollow(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_id_t entityid) +{ + if (!g_Game || !g_Game->GetView()) + return; + + g_Game->GetView()->CameraFollow(entityid, false); +} + +/** + * Start / stop first-person camera following mode. + * @param entityid unit id to follow. If zero, stop following mode. + */ +void JSI_GameView::CameraFollowFPS(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_id_t entityid) +{ + if (!g_Game || !g_Game->GetView()) + return; + + g_Game->GetView()->CameraFollow(entityid, true); +} + +entity_id_t JSI_GameView::GetFollowedEntity(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_Game || !g_Game->GetView()) + return INVALID_ENTITY; + + return g_Game->GetView()->GetFollowedEntity(); +} + +CFixedVector3D JSI_GameView::GetTerrainAtScreenPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y) +{ + CVector3D pos = g_Game->GetView()->GetCamera()->GetWorldCoordinates(x, y, true); + return CFixedVector3D(fixed::FromFloat(pos.X), fixed::FromFloat(pos.Y), fixed::FromFloat(pos.Z)); +} + +void JSI_GameView::RegisterScriptFunctions(const ScriptInterface& scriptInterface) +{ + RegisterScriptFunctions_Settings(scriptInterface); + + scriptInterface.RegisterFunction("CameraGetX"); + scriptInterface.RegisterFunction("CameraGetZ"); + scriptInterface.RegisterFunction("CameraMoveTo"); + scriptInterface.RegisterFunction("SetCameraTarget"); + scriptInterface.RegisterFunction("SetCameraData"); + scriptInterface.RegisterFunction("CameraFollow"); + scriptInterface.RegisterFunction("CameraFollowFPS"); + scriptInterface.RegisterFunction("GetFollowedEntity"); + scriptInterface.RegisterFunction("GetTerrainAtScreenPoint"); +} diff --git a/source/graphics/scripting/JSInterface_GameView.h b/source/graphics/scripting/JSInterface_GameView.h index cd5449afcb..79b27bd25d 100644 --- a/source/graphics/scripting/JSInterface_GameView.h +++ b/source/graphics/scripting/JSInterface_GameView.h @@ -19,8 +19,10 @@ #ifndef INCLUDED_JSINTERFACE_GAMEVIEW #define INCLUDED_JSINTERFACE_GAMEVIEW -#include "ps/CStr.h" #include "scriptinterface/ScriptInterface.h" +#include "maths/FixedVector3D.h" +#include "simulation2/helpers/Position.h" +#include "simulation2/system/Entity.h" #define DECLARE_BOOLEAN_SCRIPT_SETTING(NAME) \ bool Get##NAME##Enabled(ScriptInterface::CxPrivate* pCxPrivate); \ @@ -29,13 +31,23 @@ namespace JSI_GameView { void RegisterScriptFunctions(const ScriptInterface& ScriptInterface); + void RegisterScriptFunctions_Settings(const ScriptInterface& scriptInterface); DECLARE_BOOLEAN_SCRIPT_SETTING(Culling); DECLARE_BOOLEAN_SCRIPT_SETTING(LockCullCamera); DECLARE_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); + + float CameraGetX(ScriptInterface::CxPrivate* pCxPrivate); + float CameraGetZ(ScriptInterface::CxPrivate* pCxPrivate); + void CameraMoveTo(ScriptInterface::CxPrivate* pCxPrivate, entity_pos_t x, entity_pos_t z); + void SetCameraTarget(ScriptInterface::CxPrivate* pCxPrivate, float x, float y, float z); + void SetCameraData(ScriptInterface::CxPrivate* pCxPrivate, entity_pos_t x, entity_pos_t y, entity_pos_t z, entity_pos_t rotx, entity_pos_t roty, entity_pos_t zoom); + void CameraFollow(ScriptInterface::CxPrivate* pCxPrivate, entity_id_t entityid); + void CameraFollowFPS(ScriptInterface::CxPrivate* pCxPrivate, entity_id_t entityid); + entity_id_t GetFollowedEntity(ScriptInterface::CxPrivate* pCxPrivate); + CFixedVector3D GetTerrainAtScreenPoint(ScriptInterface::CxPrivate* pCxPrivate, int x, int y); } #undef DECLARE_BOOLEAN_SCRIPT_SETTING #endif - diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index 5404c808a5..6b159b9d83 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -19,7 +19,6 @@ #include "scriptinterface/ScriptInterface.h" -#include "graphics/Camera.h" #include "graphics/FontMetrics.h" #include "graphics/GameView.h" #include "graphics/MapReader.h" @@ -36,7 +35,6 @@ #include "lib/utf8.h" #include "lobby/scripting/JSInterface_Lobby.h" #include "lobby/IXmppClient.h" -#include "maths/FixedVector3D.h" #include "network/NetClient.h" #include "network/NetMessage.h" #include "network/NetServer.h" @@ -183,12 +181,6 @@ std::vector PickSimilarPlayerEntities(ScriptInterface::CxPrivate* U return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), templateName, g_Game->GetViewedPlayerID(), includeOffScreen, matchRank, false, allowFoundations); } -CFixedVector3D GetTerrainAtScreenPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y) -{ - CVector3D pos = g_Game->GetView()->GetCamera()->GetWorldCoordinates(x, y, true); - return CFixedVector3D(fixed::FromFloat(pos.X), fixed::FromFloat(pos.Y), fixed::FromFloat(pos.Z)); -} - std::wstring SetCursor(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& name) { std::wstring old = g_CursorName; @@ -560,88 +552,6 @@ JS::Value GetInitAttributes(ScriptInterface::CxPrivate* pCxPrivate) initAttribs); } -/** - * Get the current X coordinate of the camera. - */ -float CameraGetX(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (g_Game && g_Game->GetView()) - return g_Game->GetView()->GetCameraX(); - return -1; -} - -/** - * Get the current Z coordinate of the camera. - */ -float CameraGetZ(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (g_Game && g_Game->GetView()) - return g_Game->GetView()->GetCameraZ(); - return -1; -} - -/** - * Start / stop camera following mode - * @param entityid unit id to follow. If zero, stop following mode - */ -void CameraFollow(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_id_t entityid) -{ - if (g_Game && g_Game->GetView()) - g_Game->GetView()->CameraFollow(entityid, false); -} - -/** - * Start / stop first-person camera following mode - * @param entityid unit id to follow. If zero, stop following mode - */ -void CameraFollowFPS(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_id_t entityid) -{ - if (g_Game && g_Game->GetView()) - g_Game->GetView()->CameraFollow(entityid, true); -} - -/** - * Set the data (position, orientation and zoom) of the camera - */ -void SetCameraData(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_pos_t x, entity_pos_t y, entity_pos_t z, entity_pos_t rotx, entity_pos_t roty, entity_pos_t zoom) -{ - // called from JS; must not fail - if(!(g_Game && g_Game->GetWorld() && g_Game->GetView() && g_Game->GetWorld()->GetTerrain())) - return; - - CVector3D Pos = CVector3D(x.ToFloat(), y.ToFloat(), z.ToFloat()); - float RotX = rotx.ToFloat(); - float RotY = roty.ToFloat(); - float Zoom = zoom.ToFloat(); - - g_Game->GetView()->SetCamera(Pos, RotX, RotY, Zoom); -} - -/// Move camera to a 2D location -void CameraMoveTo(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_pos_t x, entity_pos_t z) -{ - // called from JS; must not fail - if(!(g_Game && g_Game->GetWorld() && g_Game->GetView() && g_Game->GetWorld()->GetTerrain())) - return; - - CTerrain* terrain = g_Game->GetWorld()->GetTerrain(); - - CVector3D target; - target.X = x.ToFloat(); - target.Z = z.ToFloat(); - target.Y = terrain->GetExactGroundLevel(target.X, target.Z); - - g_Game->GetView()->MoveCameraTarget(target); -} - -entity_id_t GetFollowedEntity(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (g_Game && g_Game->GetView()) - return g_Game->GetView()->GetFollowedEntity(); - - return INVALID_ENTITY; -} - bool HotkeyIsPressed_(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& hotkeyName) { return HotkeyIsPressed(hotkeyName); @@ -695,12 +605,6 @@ void SetTurnLength(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int length) LOGERROR("Only network host can change turn length"); } -// Focus the game camera on a given position. -void SetCameraTarget(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), float x, float y, float z) -{ - g_Game->GetView()->ResetCameraTarget(CVector3D(x, y, z)); -} - // Deliberately cause the game to crash. // Currently implemented via access violation (read of address 0). // Useful for testing the crashlog/stack trace code. @@ -968,7 +872,6 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction, int, &PickPlayerEntitiesOnScreen>("PickPlayerEntitiesOnScreen"); scriptInterface.RegisterFunction, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen"); scriptInterface.RegisterFunction, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities"); - scriptInterface.RegisterFunction("GetTerrainAtScreenPoint"); // Network / game setup functions scriptInterface.RegisterFunction("StartNetworkGame"); @@ -1013,13 +916,6 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("IsAtlasRunning"); scriptInterface.RegisterFunction("LoadMapSettings"); scriptInterface.RegisterFunction("GetInitAttributes"); - scriptInterface.RegisterFunction("CameraGetX"); - scriptInterface.RegisterFunction("CameraGetZ"); - scriptInterface.RegisterFunction("CameraFollow"); - scriptInterface.RegisterFunction("CameraFollowFPS"); - scriptInterface.RegisterFunction("SetCameraData"); - scriptInterface.RegisterFunction("CameraMoveTo"); - scriptInterface.RegisterFunction("GetFollowedEntity"); scriptInterface.RegisterFunction("HotkeyIsPressed"); scriptInterface.RegisterFunction("DisplayErrorDialog"); scriptInterface.RegisterFunction("GetProfilerState"); @@ -1043,7 +939,6 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("SetSimRate"); scriptInterface.RegisterFunction("GetSimRate"); scriptInterface.RegisterFunction("SetTurnLength"); - scriptInterface.RegisterFunction("SetCameraTarget"); scriptInterface.RegisterFunction("Crash"); scriptInterface.RegisterFunction("DebugWarn"); scriptInterface.RegisterFunction("ForceGC");