Add player:set_eye_offset() by @MirceaKitsune and clean up
authorBlockMen <nmuelll@web.de>
Fri, 11 Apr 2014 13:32:46 +0000 (15:32 +0200)
committerBlockMen <nmuelll@web.de>
Sat, 12 Apr 2014 15:44:20 +0000 (17:44 +0200)
16 files changed:
doc/lua_api.txt
src/camera.cpp
src/camera.h
src/client.cpp
src/clientmap.cpp
src/clientserver.h
src/content_cao.cpp
src/game.cpp
src/game.h
src/hud.cpp
src/localplayer.cpp
src/localplayer.h
src/script/lua_api/l_object.cpp
src/script/lua_api/l_object.h
src/server.cpp
src/server.h

index bfc12941b38128bbb01c935ca52c3ede1be4e5ad..b9865ae44f9d3eba9ce44428e31c400a350e1367 100644 (file)
@@ -1880,6 +1880,9 @@ Player-only: (no-op for other objects)
   ^ dig animation key frames
   ^ walk+dig animation key frames
   ^ animation frame speed
+- set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0}): defines offset value for camera per player
+  ^ in first person view
+  ^ in third person view (max. values {x=-10/10,y=-10,15,z=-5/5})
 
 InvRef: Reference to an inventory
 methods:
index 6d17ff74321e3c735c1a67edba85aa5b67b9f47d..6fbd1dd8c26b2326b886c18878f87c7418501d9c 100644 (file)
@@ -41,7 +41,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define CAMERA_OFFSET_STEP 200
 
 #include "nodedef.h"
-#include "game.h" // CameraModes
 
 Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
                IGameDef *gamedef):
@@ -297,8 +296,15 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
                fall_bobbing *= g_settings->getFloat("fall_bobbing_amount");
        }
 
+       // Calculate players eye offset for different camera modes
+       v3f PlayerEyeOffset = player->getEyeOffset();
+       if (current_camera_mode == CAMERA_MODE_FIRST)
+               PlayerEyeOffset += player->eye_offset_first;
+       else
+               PlayerEyeOffset += player->eye_offset_third;
+       
        // Set head node transformation
-       m_headnode->setPosition(player->getEyeOffset()+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
+       m_headnode->setPosition(PlayerEyeOffset+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
        m_headnode->setRotation(v3f(player->getPitch(), 0, cameratilt*player->hurt_tilt_strength));
        m_headnode->updateAbsolutePosition();
 
index 645b9355d572758d639a0d51f94c1f49240f5b71..82c8d4be34824121ab98e6fa3e84478c833e01d8 100644 (file)
@@ -33,6 +33,8 @@ class LocalPlayer;
 struct MapDrawControl;
 class IGameDef;
 
+enum CameraModes {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
+
 /*
        Client camera class, manages the player and camera scene nodes, the viewing distance
        and performs view bobbing etc. It also displays the wielded tool in front of the
index 4f392ad7a9c1afef8d17a4112dd5d9c0b2637b9e..dbe959067639211c13aaf19493dde1ede2cec8d5 100644 (file)
@@ -1928,6 +1928,17 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                player->local_animations[3] = readV2F1000(is);
                player->local_animation_speed = readF1000(is);
        }
+       else if(command == TOCLIENT_EYE_OFFSET)
+       {
+               std::string datastring((char *)&data[2], datasize - 2);
+               std::istringstream is(datastring, std::ios_base::binary);
+
+               LocalPlayer *player = m_env.getLocalPlayer();
+               assert(player != NULL);
+
+               player->eye_offset_first = readV3F1000(is);
+               player->eye_offset_third = readV3F1000(is);
+       }
        else
        {
                infostream<<"Client: Ignoring unknown command "
index c8b2d412d05312e21b1e40d1ce28796c96fbe57a..0a5d00b99a01d7b649457bf43238d39ab9ca620b 100644 (file)
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapblock.h"
 #include "profiler.h"
 #include "settings.h"
-#include "game.h" // CameraModes
+#include "camera.h" // CameraModes
 #include "util/mathconstants.h"
 #include <algorithm>
 
index 3d1adfc2d6c871c38f0a651c0303093480b83cad..a8cd2ea70d119c11118cc7d67c9432dbe1443bd9 100644 (file)
@@ -540,6 +540,13 @@ enum ToClientCommand
                v2f1000 walk+dig
                f1000 frame_speed
        */
+
+       TOCLIENT_EYE_OFFSET = 0x52,
+       /*
+               u16 command
+               v3f1000 first
+               v3f1000 third
+       */
 };
 
 enum ToServerCommand
index dc3bae00dc8c8fbe1579add0491dcbbfe300fdee..dbaf13cc878125bb183015d1ce9bc55b0b9b8ed2 100644 (file)
@@ -41,7 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/mathconstants.h"
 #include "map.h"
 #include "main.h" // g_settings
-#include "game.h" // CameraModes
+#include "camera.h" // CameraModes
 #include <IMeshManipulator.h>
 #include <IAnimatedMeshSceneNode.h>
 #include <IBoneSceneNode.h>
@@ -1099,8 +1099,14 @@ public:
                                        walking = true;
 
                                m_animation_speed = player->local_animation_speed;
+                               if(!player->touching_ground &&
+                                       g_settings->getBool("free_move") &&
+                               m_gamedef->checkLocalPrivilege("fly") &&
+                                       g_settings->getBool("fast_move") &&
+                               m_gamedef->checkLocalPrivilege("fast"))
+                                       m_animation_speed *= 1.5;
                                if(controls.sneak && walking)
-                                       m_animation_speed = player->local_animation_speed/2;
+                                       m_animation_speed /= 2;
 
                                player->last_animation_speed = m_animation_speed;
 
index 02308b65d3423a2fad78a05e99444291a4d47b16..2e8d3761d293434430233735f7527f950ebcb17a 100644 (file)
@@ -977,8 +977,6 @@ bool nodePlacementPrediction(Client &client,
        return false;
 }
 
-bool is_third_person = false;
-
 static void show_chat_menu(FormspecFormSource* current_formspec,
                TextDest* current_textdest, IWritableTextureSource* tsrc,
                IrrlichtDevice * device, Client* client, std::string text)
index de0b8483b07500d4adbea2915bfe8f0151dc3ad8..1c831c530067527c3bda16135109ec2728eae465 100644 (file)
@@ -124,7 +124,6 @@ public:
 
 class ChatBackend;  /* to avoid having to include chat.h */
 struct SubgameSpec;
-enum CameraModes {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
 
 void the_game(
        bool &kill,
index d210111768b0756c72db932d46aec72d5987ecbf..750755649c29cc580fb59870ae93a5b02d42d69d 100644 (file)
@@ -30,7 +30,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "tile.h"
 #include "localplayer.h"
 #include "camera.h"
-#include "game.h" // CameraModes
 
 #include <IGUIStaticText.h>
 
index 276d8e57c7d85dfe26cb8e9ae88a233eb75591de..264463d38d3c839decd0e8bdba0eddc392b14665 100644 (file)
@@ -52,7 +52,9 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef):
        m_need_to_get_new_sneak_node(true),
        m_can_jump(false),
        camera_mode(0),
-       last_animation(NO_ANIM)
+       last_animation(NO_ANIM),
+       eye_offset_first(v3f(0,0,0)),
+       eye_offset_third(v3f(0,0,0))
 {
        // Initialize hp to 0, so that no hearts will be shown if server
        // doesn't support health points
index 4a35ca929a930b98fdc3fda0bbd8339050b7a420..67deb9367dd5b78fb2da345259bf57da27f0fee7 100644 (file)
@@ -27,7 +27,7 @@ class ClientEnvironment;
 
 class ClientActiveObject;
 
-enum localPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM};  // no local animation, walking, digging, both
+enum LocalPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM};  // no local animation, walking, digging, both
 
 class LocalPlayer : public Player
 {
@@ -63,6 +63,9 @@ public:
 
        float camera_impact;
        int camera_mode;
+       v3f eye_offset_first;
+       v3f eye_offset_third;
+
        int last_animation;
        float last_animation_speed;
 
index e801ddd5fd69b6ac0a4eea60fd5c0f7e92cdd4c0..8f2bde036f2ea34a2f5897bd8a55d2734f810a8e 100644 (file)
@@ -406,7 +406,7 @@ int ObjectRef::l_set_animation(lua_State *L)
        return 0;
 }
 
-// set_local_animation(self, {stand/ilde}, {walk}, {dig}, {walk+dig}, frame_speed)
+// set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
 int ObjectRef::l_set_local_animation(lua_State *L)
 {
        //NO_MAP_LOCK_REQUIRED;
@@ -431,6 +431,36 @@ int ObjectRef::l_set_local_animation(lua_State *L)
        return 0;
 }
 
+// set_eye_offset(self, v3f first pv, v3f third pv)
+int ObjectRef::l_set_eye_offset(lua_State *L)
+{
+       //NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       Player *player = getplayer(ref);
+       if (player == NULL)
+               return 0;
+       // Do it
+       v3f offset_first = v3f(0, 0, 0);
+       v3f offset_third = v3f(0, 0, 0);
+       
+       if(!lua_isnil(L, 2))
+               offset_first = read_v3f(L, 2);
+       if(!lua_isnil(L, 3))
+               offset_third = read_v3f(L, 3);
+
+       // Prevent abuse of offset values (keep player always visible)
+       offset_third.X = rangelim(offset_third.X,-10,10);
+       offset_third.Z = rangelim(offset_third.Z,-5,5);
+       /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
+       offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
+
+       if (!getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third))
+               return 0;
+
+       lua_pushboolean(L, true);
+       return 0;
+}
+
 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
 int ObjectRef::l_set_bone_position(lua_State *L)
 {
@@ -1296,5 +1326,6 @@ const luaL_reg ObjectRef::methods[] = {
        luamethod(ObjectRef, set_sky),
        luamethod(ObjectRef, override_day_night_ratio),
        luamethod(ObjectRef, set_local_animation),
+       luamethod(ObjectRef, set_eye_offset),
        {0,0}
 };
index be1068c14f09392ddcede6d507d359d466af6269..f6070585d197af3eb72a1fda601330bec1e34c89 100644 (file)
@@ -231,9 +231,12 @@ private:
        // override_day_night_ratio(self, type, list)
        static int l_override_day_night_ratio(lua_State *L);
 
-       // set_local_animation(self, {stand/ilde}, {walk}, {dig}, {walk+dig}, frame_speed)
+       // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
        static int l_set_local_animation(lua_State *L);
 
+       // set_eye_offset(self, v3f first pv, v3f third pv)
+       static int l_set_eye_offset(lua_State *L);
+
 public:
        ObjectRef(ServerActiveObject *object);
 
index 4f42cfabe3c2244ccbf2c2ba0b203ec127520490..cbb9f427f5c946973a539e7f7572a0ae3b645074 100644 (file)
@@ -3502,6 +3502,20 @@ void Server::SendLocalPlayerAnimations(u16 peer_id, v2f animation_frames[4], f32
        m_clients.send(peer_id, 0, data, true);
 }
 
+void Server::SendEyeOffset(u16 peer_id, v3f first, v3f third)
+{
+       std::ostringstream os(std::ios_base::binary);
+
+       writeU16(os, TOCLIENT_EYE_OFFSET);
+       writeV3F1000(os, first);
+       writeV3F1000(os, third);
+
+       // Make data buffer
+       std::string s = os.str();
+       SharedBuffer<u8> data((u8 *)s.c_str(), s.size());
+       // Send as reliable
+       m_clients.send(peer_id, 0, data, true);
+}
 void Server::SendPlayerPrivileges(u16 peer_id)
 {
        Player *player = m_env->getPlayer(peer_id);
@@ -4605,6 +4619,15 @@ bool Server::setLocalPlayerAnimations(Player *player, v2f animation_frames[4], f
        return true;
 }
 
+bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third)
+{
+       if (!player)
+               return false;
+
+       SendEyeOffset(player->peer_id, first, third);
+       return true;
+}
+
 bool Server::setSky(Player *player, const video::SColor &bgcolor,
                const std::string &type, const std::vector<std::string> &params)
 {
index c796fc82722afd057c9446eaada6b7d97d82bf56..23194a3fff88b0cc9f9a8ceb4da2aeb5600d6a6f 100644 (file)
@@ -323,6 +323,7 @@ public:
                        { return m_con.GetPeerAddress(peer_id); }
                        
        bool setLocalPlayerAnimations(Player *player, v2f animation_frames[4], f32 frame_speed);
+       bool setPlayerEyeOffset(Player *player, v3f first, v3f third);
 
        bool setSky(Player *player, const video::SColor &bgcolor,
                        const std::string &type, const std::vector<std::string> &params);
@@ -364,6 +365,7 @@ private:
        void SendPlayerBreath(u16 peer_id);
        void SendMovePlayer(u16 peer_id);
        void SendLocalPlayerAnimations(u16 peer_id, v2f animation_frames[4], f32 animation_speed);
+       void SendEyeOffset(u16 peer_id, v3f first, v3f third);
        void SendPlayerPrivileges(u16 peer_id);
        void SendPlayerInventoryFormspec(u16 peer_id);
        void SendShowFormspecMessage(u16 peer_id, const std::string &formspec, const std::string &formname);