2023-07-07 13:12:16 -07:00
|
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* This file is part of 0 A.D.
|
2013-09-12 05:40:05 -07:00
|
|
|
|
*
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2013-09-12 05:40:05 -07:00
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
*
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2013-09-12 05:40:05 -07:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-12 05:40:05 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "JSInterface_GameView.h"
|
2016-06-21 03:33:11 -07:00
|
|
|
|
|
2017-09-11 16:03:27 -07:00
|
|
|
|
#include "graphics/Camera.h"
|
2013-09-12 05:40:05 -07:00
|
|
|
|
#include "graphics/GameView.h"
|
2017-09-11 16:03:27 -07:00
|
|
|
|
#include "graphics/Terrain.h"
|
2021-03-02 12:01:14 -08:00
|
|
|
|
#include "maths/FixedVector3D.h"
|
2013-09-12 05:40:05 -07:00
|
|
|
|
#include "ps/Game.h"
|
2017-09-11 16:03:27 -07:00
|
|
|
|
#include "ps/World.h"
|
2013-09-12 05:40:05 -07:00
|
|
|
|
#include "ps/CLogger.h"
|
2021-03-02 12:01:14 -08:00
|
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2023-07-07 13:12:16 -07:00
|
|
|
|
#include "scriptinterface/Object.h"
|
2021-03-02 12:01:14 -08:00
|
|
|
|
#include "simulation2/helpers/Position.h"
|
2013-09-12 05:40:05 -07:00
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
|
namespace JSI_GameView
|
|
|
|
|
|
{
|
2013-09-12 05:40:05 -07:00
|
|
|
|
#define IMPLEMENT_BOOLEAN_SCRIPT_SETTING(NAME) \
|
2021-03-02 12:01:14 -08:00
|
|
|
|
bool Get##NAME##Enabled() \
|
2013-09-12 05:40:05 -07:00
|
|
|
|
{ \
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView()) \
|
|
|
|
|
|
{ \
|
2015-01-22 12:31:30 -08:00
|
|
|
|
LOGERROR("Trying to get a setting from GameView when it's not initialized!"); \
|
2013-09-12 05:40:05 -07:00
|
|
|
|
return false; \
|
|
|
|
|
|
} \
|
|
|
|
|
|
return g_Game->GetView()->Get##NAME##Enabled(); \
|
|
|
|
|
|
} \
|
|
|
|
|
|
\
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void Set##NAME##Enabled(bool Enabled) \
|
2013-09-12 05:40:05 -07:00
|
|
|
|
{ \
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView()) \
|
|
|
|
|
|
{ \
|
2015-01-22 12:31:30 -08:00
|
|
|
|
LOGERROR("Trying to set a setting of GameView when it's not initialized!"); \
|
2013-09-12 05:40:05 -07:00
|
|
|
|
return; \
|
|
|
|
|
|
} \
|
|
|
|
|
|
g_Game->GetView()->Set##NAME##Enabled(Enabled); \
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_BOOLEAN_SCRIPT_SETTING(Culling);
|
|
|
|
|
|
IMPLEMENT_BOOLEAN_SCRIPT_SETTING(LockCullCamera);
|
|
|
|
|
|
IMPLEMENT_BOOLEAN_SCRIPT_SETTING(ConstrainCamera);
|
|
|
|
|
|
|
|
|
|
|
|
#undef IMPLEMENT_BOOLEAN_SCRIPT_SETTING
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define REGISTER_BOOLEAN_SCRIPT_SETTING(NAME) \
|
2021-03-02 12:01:14 -08:00
|
|
|
|
ScriptFunction::Register<&Get##NAME##Enabled>(rq, "GameView_Get" #NAME "Enabled"); \
|
|
|
|
|
|
ScriptFunction::Register<&Set##NAME##Enabled>(rq, "GameView_Set" #NAME "Enabled");
|
2013-09-12 05:40:05 -07:00
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void RegisterScriptFunctions_Settings(const ScriptRequest& rq)
|
2013-09-12 05:40:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
REGISTER_BOOLEAN_SCRIPT_SETTING(Culling);
|
|
|
|
|
|
REGISTER_BOOLEAN_SCRIPT_SETTING(LockCullCamera);
|
|
|
|
|
|
REGISTER_BOOLEAN_SCRIPT_SETTING(ConstrainCamera);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef REGISTER_BOOLEAN_SCRIPT_SETTING
|
|
|
|
|
|
|
2022-11-18 11:24:45 -08:00
|
|
|
|
JS::Value GetCameraRotation(const ScriptRequest& rq)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
|
|
|
|
|
|
|
|
const CVector3D rotation = g_Game->GetView()->GetCameraRotation();
|
|
|
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
|
Script::CreateObject(rq, &val, "x", rotation.X, "y", rotation.Y);
|
|
|
|
|
|
return val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JS::Value GetCameraZoom()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
|
|
return JS::NumberValue(g_Game->GetView()->GetCameraZoom());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
|
JS::Value GetCameraPivot(const ScriptRequest& rq)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
2022-11-18 11:24:45 -08:00
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return JS::UndefinedValue();
|
2019-12-19 15:41:20 -08:00
|
|
|
|
|
2022-11-18 11:24:45 -08:00
|
|
|
|
const CVector3D pivot = g_Game->GetView()->GetCameraPivot();
|
2020-11-13 05:18:22 -08:00
|
|
|
|
JS::RootedValue pivotValue(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
|
Script::CreateObject(rq, &pivotValue, "x", pivot.X, "z", pivot.Z);
|
2019-12-19 15:41:20 -08:00
|
|
|
|
return pivotValue;
|
2017-09-11 16:03:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-18 11:24:45 -08:00
|
|
|
|
JS::Value GetCameraPosition(const ScriptRequest& rq)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
|
|
|
|
|
|
|
|
const CVector3D position = g_Game->GetView()->GetCameraPosition();
|
|
|
|
|
|
JS::RootedValue positionValue(rq.cx);
|
|
|
|
|
|
Script::CreateObject(rq, &positionValue, "x", position.X, "y", position.Y, "z", position.Z);
|
|
|
|
|
|
return positionValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-11 16:03:27 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* Move camera to a 2D location.
|
|
|
|
|
|
*/
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void CameraMoveTo(entity_pos_t x, entity_pos_t z)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
2023-09-26 13:10:40 -07:00
|
|
|
|
if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView())
|
2017-09-11 16:03:27 -07:00
|
|
|
|
return;
|
|
|
|
|
|
|
2023-09-26 13:10:40 -07:00
|
|
|
|
const CTerrain& terrain = g_Game->GetWorld()->GetTerrain();
|
2017-09-11 16:03:27 -07:00
|
|
|
|
|
|
|
|
|
|
CVector3D target;
|
|
|
|
|
|
target.X = x.ToFloat();
|
|
|
|
|
|
target.Z = z.ToFloat();
|
2023-09-26 13:10:40 -07:00
|
|
|
|
target.Y = terrain.GetExactGroundLevel(target.X, target.Z);
|
2017-09-11 16:03:27 -07:00
|
|
|
|
|
|
|
|
|
|
g_Game->GetView()->MoveCameraTarget(target);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Set the camera to look at the given location.
|
|
|
|
|
|
*/
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void SetCameraTarget(float x, float y, float z)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
2021-02-27 01:01:20 -08:00
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return;
|
|
|
|
|
|
g_Game->GetView()->MoveCameraTarget(CVector3D(x, y, z));
|
2017-09-11 16:03:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Set the data (position, orientation and zoom) of the camera.
|
|
|
|
|
|
*/
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void SetCameraData(entity_pos_t x, entity_pos_t y, entity_pos_t z, entity_pos_t rotx, entity_pos_t roty, entity_pos_t zoom)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
2021-02-27 08:52:51 -08:00
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
2017-09-11 16:03:27 -07:00
|
|
|
|
return;
|
2013-09-12 05:40:05 -07:00
|
|
|
|
|
2021-02-27 08:52:51 -08:00
|
|
|
|
CVector3D pos(x.ToFloat(), y.ToFloat(), z.ToFloat());
|
2013-09-12 05:40:05 -07:00
|
|
|
|
|
2021-02-27 08:52:51 -08:00
|
|
|
|
g_Game->GetView()->SetCamera(pos, rotx.ToFloat(), roty.ToFloat(), zoom.ToFloat());
|
2017-09-11 16:03:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Start / stop camera following mode.
|
|
|
|
|
|
* @param entityid unit id to follow. If zero, stop following mode
|
|
|
|
|
|
*/
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void CameraFollow(entity_id_t entityid)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2019-10-03 05:58:47 -07:00
|
|
|
|
g_Game->GetView()->FollowEntity(entityid, false);
|
2017-09-11 16:03:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Start / stop first-person camera following mode.
|
|
|
|
|
|
* @param entityid unit id to follow. If zero, stop following mode.
|
|
|
|
|
|
*/
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void CameraFollowFPS(entity_id_t entityid)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2019-10-03 05:58:47 -07:00
|
|
|
|
g_Game->GetView()->FollowEntity(entityid, true);
|
2017-09-11 16:03:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
|
entity_id_t GetFollowedEntity()
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (!g_Game || !g_Game->GetView())
|
|
|
|
|
|
return INVALID_ENTITY;
|
|
|
|
|
|
|
|
|
|
|
|
return g_Game->GetView()->GetFollowedEntity();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
|
CFixedVector3D GetTerrainAtScreenPoint(int x, int y)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
CVector3D pos = g_Game->GetView()->GetCamera()->GetWorldCoordinates(x, y, true);
|
|
|
|
|
|
return CFixedVector3D(fixed::FromFloat(pos.X), fixed::FromFloat(pos.Y), fixed::FromFloat(pos.Z));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
|
void RegisterScriptFunctions(const ScriptRequest& rq)
|
2017-09-11 16:03:27 -07:00
|
|
|
|
{
|
2021-03-02 12:01:14 -08:00
|
|
|
|
RegisterScriptFunctions_Settings(rq);
|
|
|
|
|
|
|
2022-11-18 11:24:45 -08:00
|
|
|
|
ScriptFunction::Register<&GetCameraRotation>(rq, "GetCameraRotation");
|
|
|
|
|
|
ScriptFunction::Register<&GetCameraZoom>(rq, "GetCameraZoom");
|
2021-03-02 12:01:14 -08:00
|
|
|
|
ScriptFunction::Register<&GetCameraPivot>(rq, "GetCameraPivot");
|
2022-11-18 11:24:45 -08:00
|
|
|
|
ScriptFunction::Register<&GetCameraPosition>(rq, "GetCameraPosition");
|
2021-03-02 12:01:14 -08:00
|
|
|
|
ScriptFunction::Register<&CameraMoveTo>(rq, "CameraMoveTo");
|
|
|
|
|
|
ScriptFunction::Register<&SetCameraTarget>(rq, "SetCameraTarget");
|
|
|
|
|
|
ScriptFunction::Register<&SetCameraData>(rq, "SetCameraData");
|
|
|
|
|
|
ScriptFunction::Register<&CameraFollow>(rq, "CameraFollow");
|
|
|
|
|
|
ScriptFunction::Register<&CameraFollowFPS>(rq, "CameraFollowFPS");
|
|
|
|
|
|
ScriptFunction::Register<&GetFollowedEntity>(rq, "GetFollowedEntity");
|
|
|
|
|
|
ScriptFunction::Register<&GetTerrainAtScreenPoint>(rq, "GetTerrainAtScreenPoint");
|
|
|
|
|
|
}
|
2017-09-11 16:03:27 -07:00
|
|
|
|
}
|