Allow modifying movement speed, jump height and gravity per-player via the Lua API.

master
MirceaKitsune 2013-04-05 01:03:28 -10:00 committed by PilzAdam
parent 2a0badf2d5
commit c5a8448c41
14 changed files with 97 additions and 10 deletions

View File

@ -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:

View File

@ -88,9 +88,11 @@ SharedBuffer<u8> 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

View File

@ -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);

View File

@ -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<<serializeLongString(getPropertyPacket()); // message 1
os<<serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
os<<serializeLongString(gob_cmd_update_animation(m_animation_range, m_animation_speed, m_animation_blend)); // 3
@ -1027,6 +1031,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version)
os<<serializeLongString(gob_cmd_update_bone_position((*ii).first, (*ii).second.X, (*ii).second.Y)); // m_bone_position.size
}
os<<serializeLongString(gob_cmd_update_attachment(m_attachment_parent_id, m_attachment_bone, m_attachment_position, m_attachment_rotation)); // 4
os<<serializeLongString(gob_cmd_update_physics_override(m_physics_override_speed, m_physics_override_jump, m_physics_override_gravity)); // 5
}
else
{
@ -1196,6 +1201,14 @@ void PlayerSAO::step(float dtime, bool send_recommended)
m_messages_out.push_back(aom);
}
if(m_physics_override_sent == false){
m_physics_override_sent = true;
std::string str = gob_cmd_update_physics_override(m_physics_override_speed, m_physics_override_jump, m_physics_override_gravity);
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom);
}
if(m_animation_sent == false){
m_animation_sent = true;
std::string str = gob_cmd_update_animation(m_animation_range, m_animation_speed, m_animation_blend);

View File

@ -101,7 +101,7 @@ private:
float m_last_sent_position_timer;
float m_last_sent_move_precision;
bool m_armor_groups_sent;
v2f m_animation_range;
float m_animation_speed;
float m_animation_blend;
@ -257,8 +257,6 @@ private:
ItemGroupList m_armor_groups;
bool m_armor_groups_sent;
bool m_properties_sent;
struct ObjectProperties m_prop;
// Cached privileges for enforcement
@ -285,6 +283,11 @@ public:
bool m_inventory_not_sent;
bool m_hp_not_sent;
bool m_wielded_item_not_sent;
float m_physics_override_speed;
float m_physics_override_jump;
float m_physics_override_gravity;
bool m_physics_override_sent;
};
#endif

View File

@ -2057,7 +2057,7 @@ void ClientEnvironment::step(float dtime)
// Gravity
v3f speed = lplayer->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)

View File

@ -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);

View File

@ -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);

View File

@ -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()

View File

@ -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()

View File

@ -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;

View File

@ -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),

View File

@ -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);

View File

@ -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)