From c5a8448c41e4ea9d33a43cebef61425d4568a46d Mon Sep 17 00:00:00 2001 From: MirceaKitsune Date: Fri, 5 Apr 2013 01:03:28 -1000 Subject: [PATCH] Allow modifying movement speed, jump height and gravity per-player via the Lua API. --- doc/lua_api.txt | 4 ++++ src/clientserver.h | 4 +++- src/content_cao.cpp | 13 +++++++++++++ src/content_sao.cpp | 17 +++++++++++++++-- src/content_sao.h | 9 ++++++--- src/environment.cpp | 2 +- src/genericobject.cpp | 12 ++++++++++++ src/genericobject.h | 3 +++ src/localplayer.cpp | 6 +++--- src/player.cpp | 5 +++++ src/player.h | 4 ++++ src/scriptapi_object.cpp | 23 +++++++++++++++++++++++ src/scriptapi_object.h | 3 +++ src/serverobject.h | 2 ++ 14 files changed, 97 insertions(+), 10 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 029c84d0..a61ffce6 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1348,6 +1348,10 @@ Player-only: (no-op for other objects) {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 +- set_physics_override(speed, jump, gravity) + modifies per-player walking speed, jump height, and gravity. + Values default to 1 and act as offsets to the physics settings + in minetest.conf. nil will keep the current setting. InvRef: Reference to an inventory methods: diff --git a/src/clientserver.h b/src/clientserver.h index 28b57997..8b1e0a7e 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -88,9 +88,11 @@ SharedBuffer makePacket_TOCLIENT_TIME_OF_DAY(u16 time, float time_speed); PROTOCOL_VERSION 18: damageGroups added to ToolCapabilities sound_place added to ItemDefinition + PROTOCOL_VERSION 19: + GENERIC_CMD_SET_PHYSICS_OVERRIDE */ -#define LATEST_PROTOCOL_VERSION 18 +#define LATEST_PROTOCOL_VERSION 19 // Server's supported network protocol range #define SERVER_PROTOCOL_VERSION_MIN 13 diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 84fc0bf7..0a1a9227 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -1679,6 +1679,19 @@ public: updateTexturePos(); } + else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) + { + float override_speed = readF1000(is); + float override_jump = readF1000(is); + float override_gravity = readF1000(is); + if(m_is_local_player) + { + LocalPlayer *player = m_env->getLocalPlayer(); + player->physics_override_speed = override_speed; + player->physics_override_jump = override_jump; + player->physics_override_gravity = override_gravity; + } + } else if(cmd == GENERIC_CMD_SET_ANIMATION) { m_animation_range = readV2F1000(is); diff --git a/src/content_sao.cpp b/src/content_sao.cpp index ae08b426..e6c8c725 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -935,7 +935,11 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_, m_moved(false), m_inventory_not_sent(false), m_hp_not_sent(false), - m_wielded_item_not_sent(false) + m_wielded_item_not_sent(false), + m_physics_override_speed(1), + m_physics_override_jump(1), + m_physics_override_gravity(1), + m_physics_override_sent(false) { assert(m_player); assert(m_peer_id != 0); @@ -1019,7 +1023,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) writeF1000(os, m_player->getYaw()); writeS16(os, getHP()); - writeU8(os, 4 + m_bone_position.size()); // number of messages stuffed in here + writeU8(os, 5 + m_bone_position.size()); // number of messages stuffed in here os<getSpeed(); if(lplayer->in_liquid == false) - speed.Y -= lplayer->movement_gravity * dtime_part * 2; + speed.Y -= lplayer->movement_gravity * lplayer->physics_override_gravity * dtime_part * 2; // Liquid floating / sinking if(lplayer->in_liquid && !lplayer->swimming_vertical) diff --git a/src/genericobject.cpp b/src/genericobject.cpp index f7b272b0..e2fbde83 100644 --- a/src/genericobject.cpp +++ b/src/genericobject.cpp @@ -117,6 +117,18 @@ std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups) return os.str(); } +std::string gob_cmd_update_physics_override(float physics_override_speed, float physics_override_jump, float physics_override_gravity) +{ + std::ostringstream os(std::ios::binary); + // command + writeU8(os, GENERIC_CMD_SET_PHYSICS_OVERRIDE); + // parameters + writeF1000(os, physics_override_speed); + writeF1000(os, physics_override_jump); + writeF1000(os, physics_override_gravity); + return os.str(); +} + std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_blend) { std::ostringstream os(std::ios::binary); diff --git a/src/genericobject.h b/src/genericobject.h index 9a21baa6..276865ab 100644 --- a/src/genericobject.h +++ b/src/genericobject.h @@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define GENERIC_CMD_SET_ANIMATION 6 #define GENERIC_CMD_SET_BONE_POSITION 7 #define GENERIC_CMD_SET_ATTACHMENT 8 +#define GENERIC_CMD_SET_PHYSICS_OVERRIDE 9 #include "object_properties.h" std::string gob_cmd_set_properties(const ObjectProperties &prop); @@ -62,6 +63,8 @@ std::string gob_cmd_punched(s16 damage, s16 result_hp); #include "itemgroup.h" std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups); +std::string gob_cmd_update_physics_override(float physics_override_speed, float physics_override_jump, float physics_override_gravity); + std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_blend); std::string gob_cmd_update_bone_position(std::string bone, v3f position, v3f rotation); diff --git a/src/localplayer.cpp b/src/localplayer.cpp index a90ae696..b69bdb24 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -529,7 +529,7 @@ void LocalPlayer::applyControl(float dtime) v3f speedJ = getSpeed(); if(speedJ.Y >= -0.5 * BS) { - speedJ.Y = movement_speed_jump; + speedJ.Y = movement_speed_jump * physics_override_jump; setSpeed(speedJ); MtEvent *e = new SimpleTriggerEvent("PlayerJump"); @@ -584,8 +584,8 @@ void LocalPlayer::applyControl(float dtime) incH = incV = movement_acceleration_default * BS * dtime; // Accelerate to target speed with maximum increment - accelerateHorizontal(speedH, incH); - accelerateVertical(speedV, incV); + accelerateHorizontal(speedH * physics_override_speed, incH * physics_override_speed); + accelerateVertical(speedV * physics_override_speed, incV * physics_override_speed); } v3s16 LocalPlayer::getStandingNodePos() diff --git a/src/player.cpp b/src/player.cpp index 4c81887b..1ca9423b 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -71,6 +71,11 @@ Player::Player(IGameDef *gamedef): movement_liquid_fluidity_smooth = 0.5 * BS; movement_liquid_sink = 10 * BS; movement_gravity = 9.81 * BS; + + // Movement overrides are multipliers and must be 1 by default + physics_override_speed = 1; + physics_override_jump = 1; + physics_override_gravity = 1; } Player::~Player() diff --git a/src/player.h b/src/player.h index 496c9953..d95e535f 100644 --- a/src/player.h +++ b/src/player.h @@ -222,6 +222,10 @@ public: f32 movement_liquid_sink; f32 movement_gravity; + float physics_override_speed; + float physics_override_jump; + float physics_override_gravity; + u16 hp; float hurt_tilt_timer; diff --git a/src/scriptapi_object.cpp b/src/scriptapi_object.cpp index a0f93cbb..05433a59 100644 --- a/src/scriptapi_object.cpp +++ b/src/scriptapi_object.cpp @@ -297,6 +297,28 @@ int ObjectRef::l_set_armor_groups(lua_State *L) return 0; } +// set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity) +int ObjectRef::l_set_physics_override(lua_State *L) +{ + ObjectRef *ref = checkobject(L, 1); + PlayerSAO *co = (PlayerSAO *) getobject(ref); + if(co == NULL) return 0; + // Do it + if(!lua_isnil(L, 2)){ + co->m_physics_override_speed = lua_tonumber(L, 2); + co->m_physics_override_sent = false; + } + if(!lua_isnil(L, 3)){ + co->m_physics_override_jump = lua_tonumber(L, 3); + co->m_physics_override_sent = false; + } + if(!lua_isnil(L, 4)){ + co->m_physics_override_gravity = lua_tonumber(L, 4); + co->m_physics_override_sent = false; + } + return 0; +} + // set_animation(self, frame_range, frame_speed, frame_blend) int ObjectRef::l_set_animation(lua_State *L) { @@ -756,6 +778,7 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, get_wielded_item), luamethod(ObjectRef, set_wielded_item), luamethod(ObjectRef, set_armor_groups), + luamethod(ObjectRef, set_physics_override), luamethod(ObjectRef, set_animation), luamethod(ObjectRef, set_bone_position), luamethod(ObjectRef, set_attach), diff --git a/src/scriptapi_object.h b/src/scriptapi_object.h index a37abbb7..a4401693 100644 --- a/src/scriptapi_object.h +++ b/src/scriptapi_object.h @@ -103,6 +103,9 @@ private: // set_armor_groups(self, groups) static int l_set_armor_groups(lua_State *L); + // set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity) + static int l_set_physics_override(lua_State *L); + // set_animation(self, frame_range, frame_speed, frame_blend) static int l_set_animation(lua_State *L); diff --git a/src/serverobject.h b/src/serverobject.h index 7a5b47bd..13a075a2 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -152,6 +152,8 @@ public: virtual void setArmorGroups(const ItemGroupList &armor_groups) {} + virtual void setPhysicsOverride(float physics_override_speed, float physics_override_jump, float physics_override_gravity) + {} virtual void setAnimation(v2f frames, float frame_speed, float frame_blend) {} virtual void setBonePosition(std::string bone, v3f position, v3f rotation)