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

Support setting camera roll via lua #14333

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7709,10 +7709,14 @@ child will follow movement and rotation of that bone.
respectively.
* `get_look_horizontal()`: yaw in radians
* Angle is counter-clockwise from the +z direction.
* `get_look_roll()`: roll in radians
* Angle is clockwise from the player's perspective.
* `set_look_vertical(radians)`: sets look pitch
* radians: Angle from looking forward, where positive is downwards.
* `set_look_horizontal(radians)`: sets look yaw
* radians: Angle from the +z direction, where positive is counter-clockwise.
* `set_look_roll(radians)`:
* radians: Angle from upright, where positive is clockwise from the player's perspective.
* `get_look_pitch()`: pitch in radians - Deprecated as broken. Use
`get_look_vertical`.
* Angle ranges between -pi/2 and pi/2, which are straight down and up
Expand Down
3 changes: 2 additions & 1 deletion src/client/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)

f32 yaw = player->getYaw();
f32 pitch = player->getPitch();
f32 roll = player->get_camera_roll() * core::DEGTORAD;

// This is worse than `LocalPlayer::getPosition()` but
// mods expect the player head to be at the parent's position
Expand Down Expand Up @@ -399,7 +400,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
// Compute relative camera position and target
v3f rel_cam_pos = v3f(0,0,0);
v3f rel_cam_target = v3f(0,0,1);
v3f rel_cam_up = v3f(0,1,0);
v3f rel_cam_up = v3f(sin(roll),cos(roll),0);

if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
m_camera_mode < CAMERA_MODE_THIRD) {
Expand Down
1 change: 1 addition & 0 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void handleCommand_MediaPush(NetworkPacket *pkt);
void handleCommand_MinimapModes(NetworkPacket *pkt);
void handleCommand_SetLighting(NetworkPacket *pkt);
void handleCommand_CameraRoll(NetworkPacket* pkt);

void ProcessData(NetworkPacket *pkt);

Expand Down
1 change: 1 addition & 0 deletions src/network/clientopcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_FORMSPEC_PREPEND", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_FormspecPrepend }, // 0x61,
{ "TOCLIENT_MINIMAP_MODES", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_MinimapModes }, // 0x62,
{ "TOCLIENT_SET_LIGHTING", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_SetLighting }, // 0x63,
{ "TOCLIENT_CAMERA_ROLL", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_CameraRoll }, // 0x64
};

const static ServerCommandFactory null_command_factory = { nullptr, 0, false };
Expand Down
11 changes: 11 additions & 0 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1827,3 +1827,14 @@ void Client::handleCommand_SetLighting(NetworkPacket *pkt)
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.volumetric_light_strength;
}

void Client::handleCommand_CameraRoll(NetworkPacket* pkt)
{
LocalPlayer *player = m_env.getLocalPlayer();
assert(player);

f32 roll;

*pkt >> roll;
player->set_camera_roll(roll);
}
4 changes: 3 additions & 1 deletion src/network/networkprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,9 @@ enum ToClientCommand : u16
f32 center_weight_power
*/

TOCLIENT_NUM_MSG_TYPES = 0x64,
TOCLIENT_CAMERA_ROLL = 0x64,

TOCLIENT_NUM_MSG_TYPES = 0x65,
};

enum ToServerCommand : u16
Expand Down
1 change: 1 addition & 0 deletions src/network/serveropcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,5 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_FORMSPEC_PREPEND", 0, true }, // 0x61
{ "TOCLIENT_MINIMAP_MODES", 0, true }, // 0x62
{ "TOCLIENT_SET_LIGHTING", 0, true }, // 0x63
{ "TOCLIENT_CAMERA_ROLL", 0, true }, // 0x64
};
11 changes: 11 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ class Player
v3f eye_offset_third;
v3f eye_offset_third_front;

void set_camera_roll(f32 roll)
{
m_camera_roll = roll;
}
f32 get_camera_roll() const
{
return m_camera_roll;
}

Inventory inventory;

f32 movement_acceleration_default;
Expand Down Expand Up @@ -247,4 +256,6 @@ class Player
// and ServerThread
std::mutex m_mutex;
PlayerSettings m_player_settings;

f32 m_camera_roll = 0.0f;
};
30 changes: 30 additions & 0 deletions src/script/lua_api/l_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,20 @@ int ObjectRef::l_get_look_horizontal(lua_State *L)
return 1;
}

// get_look_roll(self)
int ObjectRef::l_get_look_roll(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkObject<ObjectRef>(L, 1);
RemotePlayer *player = getplayer(ref);
if (!player)
return 0;
regulus79 marked this conversation as resolved.
Show resolved Hide resolved

lua_pushnumber(L, player->get_camera_roll() / core::RADTODEG);
return 1;
}


// set_look_vertical(self, radians)
int ObjectRef::l_set_look_vertical(lua_State *L)
{
Expand Down Expand Up @@ -1274,6 +1288,20 @@ int ObjectRef::l_set_look_horizontal(lua_State *L)
return 0;
}

// set_look_roll(self, radians)
int ObjectRef::l_set_look_roll(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkObject<ObjectRef>(L, 1);
RemotePlayer *player = getplayer(ref);
if (player == nullptr)
return 0;

float roll = readParam<float>(L, 2) * core::RADTODEG;
getServer(L)->setPlayerCameraRoll(player,roll);
return 0;
}

// DEPRECATED
// set_look_pitch(self, radians)
int ObjectRef::l_set_look_pitch(lua_State *L)
Expand Down Expand Up @@ -2696,10 +2724,12 @@ luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, get_look_yaw),
luamethod(ObjectRef, get_look_vertical),
luamethod(ObjectRef, get_look_horizontal),
luamethod(ObjectRef, get_look_roll),
luamethod(ObjectRef, set_look_horizontal),
luamethod(ObjectRef, set_look_vertical),
luamethod(ObjectRef, set_look_yaw),
luamethod(ObjectRef, set_look_pitch),
luamethod(ObjectRef, set_look_roll),
luamethod(ObjectRef, get_fov),
luamethod(ObjectRef, set_fov),
luamethod(ObjectRef, get_breath),
Expand Down
6 changes: 6 additions & 0 deletions src/script/lua_api/l_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ class ObjectRef : public ModApiBase {
// get_look_yaw2(self)
static int l_get_look_horizontal(lua_State *L);

// get_look_roll(self)
static int l_get_look_roll(lua_State *L);

// set_fov(self, degrees, is_multiplier, transition_time)
static int l_set_fov(lua_State *L);

Expand All @@ -242,6 +245,9 @@ class ObjectRef : public ModApiBase {
// set_look_horizontal(self, radians)
static int l_set_look_horizontal(lua_State *L);

// set_look_roll(self, radians)
static int l_set_look_roll(lua_State *L);

// DEPRECATED
// set_look_pitch(self, radians)
static int l_set_look_pitch(lua_State *L);
Expand Down
14 changes: 14 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,13 @@ void Server::SendEyeOffset(session_t peer_id, v3f first, v3f third, v3f third_fr
Send(&pkt);
}

void Server::SendCameraRoll(session_t peer_id, float roll)
{
NetworkPacket pkt(TOCLIENT_CAMERA_ROLL, 0, peer_id);
pkt << roll;
Send(&pkt);
}

void Server::SendPlayerPrivileges(session_t peer_id)
{
RemotePlayer *player = m_env->getPlayer(peer_id);
Expand Down Expand Up @@ -3444,6 +3451,13 @@ void Server::setPlayerEyeOffset(RemotePlayer *player, const v3f &first, const v3
SendEyeOffset(player->getPeerId(), first, third, third_front);
}

void Server::setPlayerCameraRoll(RemotePlayer *player, float roll)
{
sanity_check(player);
player->set_camera_roll(roll);
SendCameraRoll(player->getPeerId(), roll);
}

void Server::setSky(RemotePlayer *player, const SkyboxParams &params)
{
sanity_check(player);
Expand Down
3 changes: 3 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
f32 frame_speed);
void setPlayerEyeOffset(RemotePlayer *player, const v3f &first, const v3f &third, const v3f &third_front);

void setPlayerCameraRoll(RemotePlayer *player, f32 roll);

void setSky(RemotePlayer *player, const SkyboxParams &params);
void setSun(RemotePlayer *player, const SunParams &params);
void setMoon(RemotePlayer *player, const MoonParams &params);
Expand Down Expand Up @@ -466,6 +468,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
void SendLocalPlayerAnimations(session_t peer_id, v2s32 animation_frames[4],
f32 animation_speed);
void SendEyeOffset(session_t peer_id, v3f first, v3f third, v3f third_front);
void SendCameraRoll(session_t peer_id, float roll);
void SendPlayerPrivileges(session_t peer_id);
void SendPlayerInventoryFormspec(session_t peer_id);
void SendPlayerFormspecPrepend(session_t peer_id);
Expand Down