From 3d1c481f0bdf03b59871237d810685278e87613b Mon Sep 17 00:00:00 2001 From: MirceaKitsune Date: Thu, 22 Nov 2012 21:01:31 +0200 Subject: [PATCH] RealBadAngel's patch which allows the lua api to read pressed player keys. This should make it possible to change the player's animation based on what he is doing 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 | 8 ++++-- src/client.cpp | 10 +++---- src/clientserver.h | 4 ++- src/content_cao.cpp | 12 ++++---- src/game.cpp | 16 +++++++++++ src/localplayer.h | 50 -------------------------------- src/player.h | 69 +++++++++++++++++++++++++++++++++++++++++++-- src/scriptapi.cpp | 51 ++++++++++++++++++++++++++++++++- src/server.cpp | 14 ++++++++- 9 files changed, 165 insertions(+), 69 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index fc51cb2f7..0c093b7ee 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -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 diff --git a/src/client.cpp b/src/client.cpp index 9a056806b..f72c4b654 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -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 data(2+12+12+4+4); + SharedBuffer 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); } diff --git a/src/clientserver.h b/src/clientserver.h index 15a602fc8..ba535a66b 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -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, diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 9c1171e1f..339c9f248 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -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 >::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 diff --git a/src/game.cpp b/src/game.cpp index a1a197219..5ce214cb8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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; } /* diff --git a/src/localplayer.h b/src/localplayer.h index b613fdb0f..9d1829db8 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -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; diff --git a/src/player.h b/src/player.h index 47f34c178..6c7c1e4ea 100644 --- a/src/player.h +++ b/src/player.h @@ -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; }; diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 2b2ccfcec..91100d311 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -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} }; diff --git a/src/server.cpp b/src/server.cpp index 1a401bb62..ac243a29c 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -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 "<