1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Split liquid_viscosity to liquid_viscosity and move_resistance (#10810)

This commit is contained in:
Wuzzy 2021-10-01 14:21:24 +00:00 committed by GitHub
parent f5040707fe
commit 21113ad410
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 288 additions and 44 deletions

View file

@ -194,32 +194,41 @@ void ClientEnvironment::step(float dtime)
lplayer->applyControl(dtime_part, this);
// Apply physics
if (!free_move && !is_climbing) {
if (!free_move) {
// Gravity
v3f speed = lplayer->getSpeed();
if (!lplayer->in_liquid)
if (!is_climbing && !lplayer->in_liquid)
speed.Y -= lplayer->movement_gravity *
lplayer->physics_override_gravity * dtime_part * 2.0f;
// Liquid floating / sinking
if (lplayer->in_liquid && !lplayer->swimming_vertical &&
if (!is_climbing && lplayer->in_liquid &&
!lplayer->swimming_vertical &&
!lplayer->swimming_pitch)
speed.Y -= lplayer->movement_liquid_sink * dtime_part * 2.0f;
// Liquid resistance
if (lplayer->in_liquid_stable || lplayer->in_liquid) {
// How much the node's viscosity blocks movement, ranges
// between 0 and 1. Should match the scale at which viscosity
// Movement resistance
if (lplayer->move_resistance > 0) {
// How much the node's move_resistance blocks movement, ranges
// between 0 and 1. Should match the scale at which liquid_viscosity
// increase affects other liquid attributes.
static const f32 viscosity_factor = 0.3f;
static const f32 resistance_factor = 0.3f;
v3f d_wanted = -speed / lplayer->movement_liquid_fluidity;
v3f d_wanted;
bool in_liquid_stable = lplayer->in_liquid_stable || lplayer->in_liquid;
if (in_liquid_stable) {
d_wanted = -speed / lplayer->movement_liquid_fluidity;
} else {
d_wanted = -speed / BS;
}
f32 dl = d_wanted.getLength();
if (dl > lplayer->movement_liquid_fluidity_smooth)
dl = lplayer->movement_liquid_fluidity_smooth;
if (in_liquid_stable) {
if (dl > lplayer->movement_liquid_fluidity_smooth)
dl = lplayer->movement_liquid_fluidity_smooth;
}
dl *= (lplayer->liquid_viscosity * viscosity_factor) +
(1 - viscosity_factor);
dl *= (lplayer->move_resistance * resistance_factor) +
(1 - resistance_factor);
v3f d = d_wanted.normalize() * (dl * dtime_part * 100.0f);
speed += d;
}

View file

@ -227,8 +227,9 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.1f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
in_liquid = nodemgr->get(node.getContent()).isLiquid();
liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
const ContentFeatures &cf = nodemgr->get(node.getContent());
in_liquid = cf.liquid_move_physics;
move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@ -238,8 +239,9 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.5f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
in_liquid = nodemgr->get(node.getContent()).isLiquid();
liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
const ContentFeatures &cf = nodemgr->get(node.getContent());
in_liquid = cf.liquid_move_physics;
move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@ -252,7 +254,7 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
in_liquid_stable = nodemgr->get(node.getContent()).liquid_move_physics;
} else {
in_liquid_stable = false;
}
@ -800,8 +802,9 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.1f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
in_liquid = nodemgr->get(node.getContent()).isLiquid();
liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
const ContentFeatures &cf = nodemgr->get(node.getContent());
in_liquid = cf.liquid_move_physics;
move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@ -810,8 +813,9 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.5f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
in_liquid = nodemgr->get(node.getContent()).isLiquid();
liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
const ContentFeatures &cf = nodemgr->get(node.getContent());
in_liquid = cf.liquid_move_physics;
move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@ -823,7 +827,7 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position)
in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
in_liquid_stable = nodemgr->get(node.getContent()).liquid_move_physics;
else
in_liquid_stable = false;

View file

@ -55,8 +55,8 @@ public:
bool in_liquid = false;
// This is more stable and defines the maximum speed of the player
bool in_liquid_stable = false;
// Gets the viscosity of water to calculate friction
u8 liquid_viscosity = 0;
// Slows down the player when moving through
u8 move_resistance = 0;
bool is_climbing = false;
bool swimming_vertical = false;
bool swimming_pitch = false;

View file

@ -403,6 +403,8 @@ void ContentFeatures::reset()
palette_name = "";
palette = NULL;
node_dig_prediction = "air";
move_resistance = 0;
liquid_move_physics = false;
}
void ContentFeatures::setAlphaFromLegacy(u8 legacy_alpha)
@ -512,9 +514,12 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, legacy_facedir_simple);
writeU8(os, legacy_wallmounted);
// new attributes
os << serializeString16(node_dig_prediction);
writeU8(os, leveled_max);
writeU8(os, alpha);
writeU8(os, move_resistance);
writeU8(os, liquid_move_physics);
}
void ContentFeatures::deSerialize(std::istream &is)
@ -584,9 +589,11 @@ void ContentFeatures::deSerialize(std::istream &is)
// liquid
liquid_type = (enum LiquidType) readU8(is);
liquid_move_physics = liquid_type != LIQUID_NONE;
liquid_alternative_flowing = deSerializeString16(is);
liquid_alternative_source = deSerializeString16(is);
liquid_viscosity = readU8(is);
move_resistance = liquid_viscosity; // set default move_resistance
liquid_renewable = readU8(is);
liquid_range = readU8(is);
drowning = readU8(is);
@ -618,6 +625,16 @@ void ContentFeatures::deSerialize(std::istream &is)
if (is.eof())
throw SerializationError("");
alpha = static_cast<enum AlphaMode>(tmp);
tmp = readU8(is);
if (is.eof())
throw SerializationError("");
move_resistance = tmp;
tmp = readU8(is);
if (is.eof())
throw SerializationError("");
liquid_move_physics = tmp;
} catch(SerializationError &e) {};
}

View file

@ -376,11 +376,15 @@ struct ContentFeatures
u32 damage_per_second;
// client dig prediction
std::string node_dig_prediction;
// how slow players move through
u8 move_resistance = 0;
// --- LIQUID PROPERTIES ---
// Whether the node is non-liquid, source liquid or flowing liquid
enum LiquidType liquid_type;
// If true, movement (e.g. of players) inside this node is liquid-like.
bool liquid_move_physics;
// If the content is liquid, this is the flowing version of the liquid.
std::string liquid_alternative_flowing;
content_t liquid_alternative_flowing_id;

View file

@ -719,6 +719,9 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
// the slowest possible
f.liquid_viscosity = getintfield_default(L, index,
"liquid_viscosity", f.liquid_viscosity);
// If move_resistance is not set explicitly,
// move_resistance is equal to liquid_viscosity
f.move_resistance = f.liquid_viscosity;
f.liquid_range = getintfield_default(L, index,
"liquid_range", f.liquid_range);
f.leveled = getintfield_default(L, index, "leveled", f.leveled);
@ -822,6 +825,21 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
getstringfield(L, index, "node_dig_prediction",
f.node_dig_prediction);
// How much the node slows down players, ranging from 1 to 7,
// the higher, the slower.
f.move_resistance = getintfield_default(L, index,
"move_resistance", f.move_resistance);
// Whether e.g. players in this node will have liquid movement physics
lua_getfield(L, index, "liquid_move_physics");
if(lua_isboolean(L, -1)) {
f.liquid_move_physics = lua_toboolean(L, -1);
} else if(lua_isnil(L, -1)) {
f.liquid_move_physics = f.liquid_type != LIQUID_NONE;
} else {
errorstream << "Field \"liquid_move_physics\": Invalid type!" << std::endl;
}
lua_pop(L, 1);
}
void push_content_features(lua_State *L, const ContentFeatures &c)
@ -949,6 +967,10 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
lua_setfield(L, -2, "legacy_wallmounted");
lua_pushstring(L, c.node_dig_prediction.c_str());
lua_setfield(L, -2, "node_dig_prediction");
lua_pushnumber(L, c.move_resistance);
lua_setfield(L, -2, "move_resistance");
lua_pushboolean(L, c.liquid_move_physics);
lua_setfield(L, -2, "liquid_move_physics");
}
/******************************************************************************/

View file

@ -53,6 +53,7 @@ public:
static struct EnumString es_ContentParamType[];
static struct EnumString es_ContentParamType2[];
static struct EnumString es_LiquidType[];
static struct EnumString es_LiquidMoveType[];
static struct EnumString es_NodeBoxType[];
static struct EnumString es_TextureAlphaMode[];
};

View file

@ -128,11 +128,11 @@ int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
return 1;
}
int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
int LuaLocalPlayer::l_get_move_resistance(lua_State *L)
{
LocalPlayer *player = getobject(L, 1);
lua_pushinteger(L, player->liquid_viscosity);
lua_pushinteger(L, player->move_resistance);
return 1;
}
@ -466,7 +466,6 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, is_touching_ground),
luamethod(LuaLocalPlayer, is_in_liquid),
luamethod(LuaLocalPlayer, is_in_liquid_stable),
luamethod(LuaLocalPlayer, get_liquid_viscosity),
luamethod(LuaLocalPlayer, is_climbing),
luamethod(LuaLocalPlayer, swimming_vertical),
luamethod(LuaLocalPlayer, get_physics_override),
@ -488,5 +487,7 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, hud_change),
luamethod(LuaLocalPlayer, hud_get),
luamethod(LuaLocalPlayer, get_move_resistance),
{0, 0}
};

View file

@ -51,7 +51,6 @@ private:
static int l_is_touching_ground(lua_State *L);
static int l_is_in_liquid(lua_State *L);
static int l_is_in_liquid_stable(lua_State *L);
static int l_get_liquid_viscosity(lua_State *L);
static int l_is_climbing(lua_State *L);
static int l_swimming_vertical(lua_State *L);
@ -96,6 +95,8 @@ private:
// hud_get(self, id)
static int l_hud_get(lua_State *L);
static int l_get_move_resistance(lua_State *L);
LocalPlayer *m_localplayer = nullptr;
public: