Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSM] Add function to set the FOV of the local player #5797

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions doc/client_lua_api.txt
Expand Up @@ -900,17 +900,25 @@ Please do not try to access the reference until the camera is initialized, other
* `get_camera_mode()`
* Returns 0, 1, or 2 as described above
* `get_fov()`
* All values are in degrees
* Returns:

```lua
{
x = number,
y = number,
max = number,
actual = number
x = float, -- Horizontal FOV
y = float, -- Vertical FOV
max = float, -- The maximum of x and y
actual = integer, -- What the FOV of the player is (not adjusted for zooming)
zoom = integer -- What the FOV of the player when zooming is
}
```

* `set_fov(fov_degrees, [persist])`
* Sets the FOV for the local player
* Pass true to the second argument to save the zoom fov to the setting.
* `set_zoom_fov(fov_degrees, [persist])`
* Sets the FOV for the local player when zoomed in
* Pass true to the second argument to save the zoom fov to the setting.
* `get_pos()`
* Returns position of camera with view bobbing
* `get_offset()`
Expand Down
2 changes: 1 addition & 1 deletion src/camera.cpp
Expand Up @@ -458,7 +458,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
} else {
fov_degrees = m_cache_fov;
}
fov_degrees = rangelim(fov_degrees, 1.0, 160.0);
fov_degrees = rangelim(fov_degrees, ABSOLUTE_FOV_MIN, ABSOLUTE_FOV_MAX);

// FOV and aspect ratio
const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
Expand Down
27 changes: 27 additions & 0 deletions src/camera.h
Expand Up @@ -22,9 +22,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_extrabloated.h"
#include "inventory.h"
#include "client/tile.h"
#include "util/numeric.h"
#include "constants.h"
#include <ICameraSceneNode.h>
#include <ISceneNode.h>
#include <list>
#include "settings.h" // For g_settings

class LocalPlayer;
struct MapDrawControl;
Expand Down Expand Up @@ -88,6 +91,30 @@ class Camera
return m_camera_offset;
}

inline void setFov(f32 fov, bool persist)
{
m_cache_fov = rangelim(fov, FOV_MIN, FOV_MAX);
if (persist)
g_settings->setFloat("fov", getFov());
}

inline void setZoomFov(f32 fov, bool persist)
{
m_cache_zoom_fov = rangelim(fov, ZOOM_FOV_MIN, ZOOM_FOV_MAX);
if (persist)
g_settings->setFloat("zoom_fov", getZoomFov());
}

inline f32 getFov() const
{
return m_cache_fov;
}

inline f32 getZoomFov() const
{
return m_cache_zoom_fov;
}

// Horizontal field of view
inline f32 getFovX() const
{
Expand Down
16 changes: 16 additions & 0 deletions src/constants.h
Expand Up @@ -47,6 +47,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// resend_timeout = avg_rtt * this
#define RESEND_TIMEOUT_FACTOR 4

/*
Client
*/
// Minimum and maximum fov values for FOV.
// Zoom FOV setting
#define ZOOM_FOV_MIN (1)
#define ZOOM_FOV_MAX (160)

// FOV setting
#define FOV_MIN (30)
#define FOV_MAX (160)

// Absolute range
#define ABSOLUTE_FOV_MIN (7)
#define ABSOLUTE_FOV_MAX (160)

/*
Server
*/
Expand Down
37 changes: 35 additions & 2 deletions src/script/lua_api/l_camera.cpp
Expand Up @@ -3,6 +3,7 @@
#include "l_internal.h"
#include "content_cao.h"
#include "camera.h"
#include "constants.h"
#include "client.h"

LuaCamera::LuaCamera(Camera *m) : m_camera(m)
Expand Down Expand Up @@ -64,13 +65,44 @@ int LuaCamera::l_get_fov(lua_State *L)
lua_setfield(L, -2, "x");
lua_pushnumber(L, camera->getFovY() * core::DEGTORAD);
lua_setfield(L, -2, "y");
lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
lua_setfield(L, -2, "actual");
lua_pushnumber(L, camera->getFovMax() * core::RADTODEG);
lua_setfield(L, -2, "max");
lua_pushnumber(L, camera->getFov());
lua_setfield(L, -2, "actual");
lua_pushnumber(L, camera->getZoomFov());
lua_setfield(L, -2, "zoom");

return 1;
}

int LuaCamera::l_set_fov(lua_State *L)
{
Camera *camera = getobject(L, 1);

float fov = luaL_checknumber(L, 2);
bool persist = lua_toboolean(L, 3);

if (!camera)
return 0;

camera->setFov(fov, persist);
return 0;
}

int LuaCamera::l_set_zoom_fov(lua_State *L)
{
Camera *camera = getobject(L, 1);

float fov = luaL_checknumber(L, 2);
bool persist = lua_toboolean(L, 3);

if (!camera)
return 0;

camera->setZoomFov(fov, persist);
return 0;
}

int LuaCamera::l_get_pos(lua_State *L)
{
Camera *camera = getobject(L, 1);
Expand Down Expand Up @@ -193,6 +225,7 @@ void LuaCamera::Register(lua_State *L)
const char LuaCamera::className[] = "Camera";
const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode),
luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov),
luamethod(LuaCamera, set_fov), luamethod(LuaCamera, set_zoom_fov),
luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset),
luamethod(LuaCamera, get_look_dir),
luamethod(LuaCamera, get_look_vertical),
Expand Down
2 changes: 2 additions & 0 deletions src/script/lua_api/l_camera.h
Expand Up @@ -36,6 +36,8 @@ class LuaCamera : public ModApiBase
static int l_get_camera_mode(lua_State *L);

static int l_get_fov(lua_State *L);
static int l_set_fov(lua_State *L);
static int l_set_zoom_fov(lua_State *L);

static int l_get_pos(lua_State *L);
static int l_get_offset(lua_State *L);
Expand Down