RealBadAngel's patch which allows the lua api to read pressed player keys. This shoul...
authorMirceaKitsune <sonichedgehog_hyperblast00@yahoo.com>
Thu, 22 Nov 2012 19:01:31 +0000 (21:01 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Sun, 25 Nov 2012 17:14:24 +0000 (19:14 +0200)
Correct lua api version number

Always update animations and attachments after the entity is added to scene client side. Fixes animations not being applied in client initialization for some reason. Attachments should be re-tested now just to be safe.

Fix a segmentation fault caused by reaching materials that didn't exist in a loop for setting texture

doc/lua_api.txt
src/client.cpp
src/clientserver.h
src/content_cao.cpp
src/game.cpp
src/localplayer.h
src/player.h
src/scriptapi.cpp
src/server.cpp

index fc51cb2f730e241538f7596e29d63b8a2432f573..0c093b7ee887fcee26f0944d915dd177741761b7 100644 (file)
@@ -1,4 +1,4 @@
-Minetest Lua Modding API Reference 0.4.0
+Minetest Lua Modding API Reference 0.4.3
 ==========================================
 More information at http://c55.me/minetest/
 
@@ -1132,7 +1132,11 @@ Player-only: (no-op for other objects)
   ^ Redefine player's inventory form
   ^ Should usually be called in on_joinplayer
 - get_inventory_formspec() -> formspec string
-
+- get_player_control(): returns table with player pressed keys
+       {jump=bool,right=bool,left=bool,LMB=bool,RMB=bool,sneak=bool,aux1=bool,down=bool,up=bool}
+- get_player_control_bits(): returns integer with bit packed player pressed keys
+       bit nr/meaning: 0/up ,1/down ,2/left ,3/right ,4/jump ,5/aux1 ,6/sneak ,7/LMB ,8/RMB
+       
 InvRef: Reference to an inventory
 methods:
 - is_empty(listname): return true if list is empty
index 9a056806b700f1b04a880abb64906b9a6c69ce65..f72c4b654aad51ab34ab648d38852471b3779b42 100644 (file)
@@ -1984,7 +1984,7 @@ void Client::sendPlayerPos()
        v3s32 speed(sf.X*100, sf.Y*100, sf.Z*100);
        s32 pitch = myplayer->getPitch() * 100;
        s32 yaw = myplayer->getYaw() * 100;
-
+       u32 keyPressed=myplayer->keyPressed;
        /*
                Format:
                [0] u16 command
@@ -1992,15 +1992,15 @@ void Client::sendPlayerPos()
                [2+12] v3s32 speed*100
                [2+12+12] s32 pitch*100
                [2+12+12+4] s32 yaw*100
+               [2+12+12+4+4] u32 keyPressed
        */
-
-       SharedBuffer<u8> data(2+12+12+4+4);
+       SharedBuffer<u8> data(2+12+12+4+4+4);
        writeU16(&data[0], TOSERVER_PLAYERPOS);
        writeV3S32(&data[2], position);
        writeV3S32(&data[2+12], speed);
        writeS32(&data[2+12+12], pitch);
-       writeS32(&data[2+12+12+4], yaw);
-
+       writeS32(&data[2+12+12+4], yaw);        
+       writeU32(&data[2+12+12+4+4], keyPressed);
        // Send as unreliable
        Send(0, data, false);
 }
index 15a602fc832194e3cab86b3a20d9d66059b89b96..ba535a66be36d57cdf0225b1f9430a269933b8ec 100644 (file)
@@ -68,7 +68,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        PROTOCOL_VERSION 13:
                InventoryList field "Width" (deserialization fails with old versions)
        PROTOCOL_VERSION 14:
-               New messages for mesh and bone animation, as well as attachments
+               Added transfer of player pressed keys to the server
+               Added new messages for mesh and bone animation, as well as attachments
                GENERIC_CMD_SET_ANIMATION
                GENERIC_CMD_SET_BONE_POSITION
                GENERIC_CMD_SET_ATTACHMENT
@@ -371,6 +372,7 @@ enum ToServerCommand
                [2+12] v3s32 speed*100
                [2+12+12] s32 pitch*100
                [2+12+12+4] s32 yaw*100
+               [2+12+12+4+4] u32 keyPressed
        */
 
        TOSERVER_GOTBLOCKS = 0x24,
index 9c1171e1f8b6e1febb765a245f743b3b75e170e7..339c9f2480de1bc00b6175352980a0de84e99422 100644 (file)
@@ -970,8 +970,11 @@ public:
                                        wname.c_str(), video::SColor(255,255,255,255), node);
                        m_textnode->setPosition(v3f(0, BS*1.1, 0));
                }
-               
+
                updateNodePos();
+               updateAnimation();
+               updateBonePosition();
+               updateAttachments();
        }
 
        void expireVisuals()
@@ -1049,9 +1052,6 @@ public:
 
                        removeFromScene(false);
                        addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
-                       updateAnimation();
-                       updateBonePosition();
-                       updateAttachments();
 
                        // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
                        for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
@@ -1248,7 +1248,7 @@ public:
                {
                        if(m_prop.visual == "mesh")
                        {
-                               for (u32 i = 0; i < m_prop.textures.size(); ++i)
+                               for (u32 i = 0; i < m_prop.textures.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
                                {
                                        std::string texturestring = m_prop.textures[i];
                                        if(texturestring == "")
@@ -1271,7 +1271,7 @@ public:
                                        m_animated_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
                                        m_animated_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
                                }
-                               for (u32 i = 0; i < m_prop.colors.size(); ++i)
+                               for (u32 i = 0; i < m_prop.colors.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
                                {
                                        // This allows setting per-material colors. However, until a real lighting
                                        // system is added, the code below will have no effect. Once MineTest
index a1a197219f4bad5b0c03d013aa0f6a859d17edfd..5ce214cb8d3150c253c63c1be99d028654667f10 100644 (file)
@@ -1881,6 +1881,8 @@ void the_game(
                        bool a_jump,
                        bool a_superspeed,
                        bool a_sneak,
+                       bool a_LMB,
+                       bool a_RMB,
                        float a_pitch,
                        float a_yaw*/
                        PlayerControl control(
@@ -1891,10 +1893,24 @@ void the_game(
                                input->isKeyDown(getKeySetting("keymap_jump")),
                                input->isKeyDown(getKeySetting("keymap_special1")),
                                input->isKeyDown(getKeySetting("keymap_sneak")),
+                               input->getLeftState(),
+                               input->getRightState(),
                                camera_pitch,
                                camera_yaw
                        );
                        client.setPlayerControl(control);
+                       u32 keyPressed=
+                       1*(int)input->isKeyDown(getKeySetting("keymap_forward"))+
+                       2*(int)input->isKeyDown(getKeySetting("keymap_backward"))+
+                       4*(int)input->isKeyDown(getKeySetting("keymap_left"))+
+                       8*(int)input->isKeyDown(getKeySetting("keymap_right"))+
+                       16*(int)input->isKeyDown(getKeySetting("keymap_jump"))+
+                       32*(int)input->isKeyDown(getKeySetting("keymap_special1"))+
+                       64*(int)input->isKeyDown(getKeySetting("keymap_sneak"))+
+                       128*(int)input->getLeftState()+
+                       256*(int)input->getRightState();
+                       LocalPlayer* player = client.getEnv().getLocalPlayer();
+                       player->keyPressed=keyPressed;
                }
                
                /*
index b613fdb0f4802b5a6fb7ac913fc0a4aa661ac356..9d1829db86a12d7f70de44104cfd47103385d93d 100644 (file)
@@ -22,53 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "player.h"
 
-struct PlayerControl
-{
-       PlayerControl()
-       {
-               up = false;
-               down = false;
-               left = false;
-               right = false;
-               jump = false;
-               aux1 = false;
-               sneak = false;
-               pitch = 0;
-               yaw = 0;
-       }
-       PlayerControl(
-               bool a_up,
-               bool a_down,
-               bool a_left,
-               bool a_right,
-               bool a_jump,
-               bool a_aux1,
-               bool a_sneak,
-               float a_pitch,
-               float a_yaw
-       )
-       {
-               up = a_up;
-               down = a_down;
-               left = a_left;
-               right = a_right;
-               jump = a_jump;
-               aux1 = a_aux1;
-               sneak = a_sneak;
-               pitch = a_pitch;
-               yaw = a_yaw;
-       }
-       bool up;
-       bool down;
-       bool left;
-       bool right;
-       bool jump;
-       bool aux1;
-       bool sneak;
-       float pitch;
-       float yaw;
-};
-
 class LocalPlayer : public Player
 {
 public:
@@ -91,9 +44,6 @@ public:
        void applyControl(float dtime);
 
        v3s16 getStandingNodePos();
-       
-       PlayerControl control;
-
 private:
        // This is used for determining the sneaking range
        v3s16 m_sneak_node;
index 47f34c178a18fc1e18d366b5ed95e4c6ea536057..6c7c1e4ea2db8f7918a2ebbc68d91872eb949816 100644 (file)
@@ -28,6 +28,61 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
 
+struct PlayerControl
+{
+       PlayerControl()
+       {
+               up = false;
+               down = false;
+               left = false;
+               right = false;
+               jump = false;
+               aux1 = false;
+               sneak = false;
+               LMB = false;
+               RMB = false;
+               pitch = 0;
+               yaw = 0;
+       }
+       PlayerControl(
+               bool a_up,
+               bool a_down,
+               bool a_left,
+               bool a_right,
+               bool a_jump,
+               bool a_aux1,
+               bool a_sneak,
+               bool a_LMB,
+               bool a_RMB,
+               float a_pitch,
+               float a_yaw
+       )
+       {
+               up = a_up;
+               down = a_down;
+               left = a_left;
+               right = a_right;
+               jump = a_jump;
+               aux1 = a_aux1;
+               sneak = a_sneak;
+               LMB = a_LMB;
+               RMB = a_RMB;
+               pitch = a_pitch;
+               yaw = a_yaw;
+       }
+       bool up;
+       bool down;
+       bool left;
+       bool right;
+       bool jump;
+       bool aux1;
+       bool sneak;
+       bool LMB;
+       bool RMB;
+       float pitch;
+       float yaw;
+};
+
 class Map;
 class IGameDef;
 struct CollisionInfo;
@@ -155,9 +210,17 @@ public:
        u16 hp;
 
        u16 peer_id;
-
+       
        std::string inventory_formspec;
-
+       
+       PlayerControl control;
+       PlayerControl getPlayerControl()
+       {
+               return control;
+       }
+       
+       u32 keyPressed;
+       
 protected:
        IGameDef *m_gamedef;
 
@@ -182,7 +245,7 @@ public:
        void setPlayerSAO(PlayerSAO *sao)
        { m_sao = sao; }
        void setPosition(const v3f &position);
-
+       
 private:
        PlayerSAO *m_sao;
 };
index 2b2ccfcec772a339ffc8033421e5efeaac996226..91100d3115e56f0eaa4766cf19244e0c546f773d 100644 (file)
@@ -3025,7 +3025,54 @@ private:
                lua_pushlstring(L, formspec.c_str(), formspec.size());
                return 1;
        }
-
+       
+       // get_player_control(self)
+       static int l_get_player_control(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               Player *player = getplayer(ref);
+               if(player == NULL){
+                       lua_pushlstring(L, "", 0);
+                       return 1;
+               }
+               // Do it
+               PlayerControl control = player->getPlayerControl();
+               lua_newtable(L);
+               lua_pushboolean(L, control.up);
+               lua_setfield(L, -2, "up");
+               lua_pushboolean(L, control.down);
+               lua_setfield(L, -2, "down");
+               lua_pushboolean(L, control.left);
+               lua_setfield(L, -2, "left");
+               lua_pushboolean(L, control.right);
+               lua_setfield(L, -2, "right");
+               lua_pushboolean(L, control.jump);
+               lua_setfield(L, -2, "jump");
+               lua_pushboolean(L, control.aux1);
+               lua_setfield(L, -2, "aux1");
+               lua_pushboolean(L, control.sneak);
+               lua_setfield(L, -2, "sneak");
+               lua_pushboolean(L, control.LMB);
+               lua_setfield(L, -2, "LMB");
+               lua_pushboolean(L, control.RMB);
+               lua_setfield(L, -2, "RMB");
+               return 1;
+       }
+       
+       // get_player_control_bits(self)
+       static int l_get_player_control_bits(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               Player *player = getplayer(ref);
+               if(player == NULL){
+                       lua_pushlstring(L, "", 0);
+                       return 1;
+               }
+               // Do it        
+               lua_pushnumber(L, player->keyPressed);
+               return 1;
+       }
+       
 public:
        ObjectRef(ServerActiveObject *object):
                m_object(object)
@@ -3128,6 +3175,8 @@ const luaL_reg ObjectRef::methods[] = {
        method(ObjectRef, get_look_yaw),
        method(ObjectRef, set_inventory_formspec),
        method(ObjectRef, get_inventory_formspec),
+       method(ObjectRef, get_player_control),
+       method(ObjectRef, get_player_control_bits),
        {0,0}
 };
 
index 1a401bb6289cc2c9964a7c7770b620364835aac7..ac243a29cfa4c44bad6b42ab82483fc20392f8ac 100644 (file)
@@ -2369,7 +2369,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 
        if(command == TOSERVER_PLAYERPOS)
        {
-               if(datasize < 2+12+12+4+4)
+               if(datasize < 2+12+12+4+4+4)
                        return;
        
                u32 start = 0;
@@ -2377,6 +2377,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                v3s32 ss = readV3S32(&data[start+2+12]);
                f32 pitch = (f32)readS32(&data[2+12+12]) / 100.0;
                f32 yaw = (f32)readS32(&data[2+12+12+4]) / 100.0;
+               u32 keyPressed = (u32)readU32(&data[2+12+12+4+4]);
                v3f position((f32)ps.X/100., (f32)ps.Y/100., (f32)ps.Z/100.);
                v3f speed((f32)ss.X/100., (f32)ss.Y/100., (f32)ss.Z/100.);
                pitch = wrapDegrees(pitch);
@@ -2386,6 +2387,16 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                player->setSpeed(speed);
                player->setPitch(pitch);
                player->setYaw(yaw);
+               player->keyPressed=keyPressed;
+               player->control.up = (bool)(keyPressed&1);
+               player->control.down = (bool)(keyPressed&2);
+               player->control.left = (bool)(keyPressed&4);
+               player->control.right = (bool)(keyPressed&8);
+               player->control.jump = (bool)(keyPressed&16);
+               player->control.aux1 = (bool)(keyPressed&32);
+               player->control.sneak = (bool)(keyPressed&64);
+               player->control.LMB = (bool)(keyPressed&128);
+               player->control.RMB = (bool)(keyPressed&256);
                
                /*infostream<<"Server::ProcessData(): Moved player "<<peer_id<<" to "
                                <<"("<<position.X<<","<<position.Y<<","<<position.Z<<")"
@@ -3166,6 +3177,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        }
 
                } // action == 4
+               
 
                /*
                        Catch invalid actions