forked from Mirrorlandia_minetest/minetest
Customizeable max breath for players (#6411)
* Customizeable maximal breath for players
This commit is contained in:
parent
7640749d68
commit
edbc533414
@ -23,8 +23,8 @@ core.EMERGE_GENERATED = 4
|
|||||||
core.MAP_BLOCKSIZE = 16
|
core.MAP_BLOCKSIZE = 16
|
||||||
-- Default maximal HP of a player
|
-- Default maximal HP of a player
|
||||||
core.PLAYER_MAX_HP_DEFAULT = 20
|
core.PLAYER_MAX_HP_DEFAULT = 20
|
||||||
-- Maximal breath of a player
|
-- Default maximal breath of a player
|
||||||
core.PLAYER_MAX_BREATH = 11
|
core.PLAYER_MAX_BREATH_DEFAULT = 11
|
||||||
|
|
||||||
-- light.h
|
-- light.h
|
||||||
-- Maximum value for node 'light_source' parameter
|
-- Maximum value for node 'light_source' parameter
|
||||||
|
@ -17,7 +17,7 @@ local breath_bar_definition =
|
|||||||
hud_elem_type = "statbar",
|
hud_elem_type = "statbar",
|
||||||
position = { x=0.5, y=1 },
|
position = { x=0.5, y=1 },
|
||||||
text = "bubble.png",
|
text = "bubble.png",
|
||||||
number = 20,
|
number = core.PLAYER_MAX_BREATH_DEFAULT,
|
||||||
direction = 0,
|
direction = 0,
|
||||||
size = { x=24, y=24 },
|
size = { x=24, y=24 },
|
||||||
offset = {x=25,y=-(48+24+16)},
|
offset = {x=25,y=-(48+24+16)},
|
||||||
@ -25,6 +25,15 @@ local breath_bar_definition =
|
|||||||
|
|
||||||
local hud_ids = {}
|
local hud_ids = {}
|
||||||
|
|
||||||
|
local function scaleToDefault(player, field)
|
||||||
|
-- Scale "hp" or "breath" to the default dimensions
|
||||||
|
local current = player["get_" .. field](player)
|
||||||
|
local nominal = core["PLAYER_MAX_".. field:upper() .. "_DEFAULT"]
|
||||||
|
local max_display = math.max(nominal,
|
||||||
|
math.max(player:get_properties()[field .. "_max"], current))
|
||||||
|
return current / max_display * nominal
|
||||||
|
end
|
||||||
|
|
||||||
local function initialize_builtin_statbars(player)
|
local function initialize_builtin_statbars(player)
|
||||||
|
|
||||||
if not player:is_player() then
|
if not player:is_player() then
|
||||||
@ -47,31 +56,22 @@ local function initialize_builtin_statbars(player)
|
|||||||
|
|
||||||
if player:hud_get_flags().healthbar and enable_damage then
|
if player:hud_get_flags().healthbar and enable_damage then
|
||||||
if hud.id_healthbar == nil then
|
if hud.id_healthbar == nil then
|
||||||
local hp = player:get_hp()
|
local hud_def = table.copy(health_bar_definition)
|
||||||
local max_display_hp = math.max(core.PLAYER_MAX_HP_DEFAULT,
|
hud_def.number = scaleToDefault(player, "hp")
|
||||||
math.max(player:get_properties().hp_max, hp))
|
hud.id_healthbar = player:hud_add(hud_def)
|
||||||
-- Limit width of health bar: Scale to the default maximal HP
|
|
||||||
health_bar_definition.number =
|
|
||||||
hp / max_display_hp * core.PLAYER_MAX_HP_DEFAULT
|
|
||||||
hud.id_healthbar = player:hud_add(health_bar_definition)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if hud.id_healthbar ~= nil then
|
|
||||||
player:hud_remove(hud.id_healthbar)
|
|
||||||
hud.id_healthbar = nil
|
|
||||||
end
|
end
|
||||||
|
elseif hud.id_healthbar ~= nil then
|
||||||
|
player:hud_remove(hud.id_healthbar)
|
||||||
|
hud.id_healthbar = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
if player:get_breath() < core.PLAYER_MAX_BREATH then
|
local breath_max = player:get_properties().breath_max
|
||||||
if player:hud_get_flags().breathbar and enable_damage then
|
if player:hud_get_flags().breathbar and enable_damage and
|
||||||
if hud.id_breathbar == nil then
|
player:get_breath() < breath_max then
|
||||||
hud.id_breathbar = player:hud_add(breath_bar_definition)
|
if hud.id_breathbar == nil then
|
||||||
end
|
local hud_def = table.copy(breath_bar_definition)
|
||||||
else
|
hud_def.number = 2 * scaleToDefault(player, "breath")
|
||||||
if hud.id_breathbar ~= nil then
|
hud.id_breathbar = player:hud_add(hud_def)
|
||||||
player:hud_remove(hud.id_breathbar)
|
|
||||||
hud.id_breathbar = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
elseif hud.id_breathbar ~= nil then
|
elseif hud.id_breathbar ~= nil then
|
||||||
player:hud_remove(hud.id_breathbar)
|
player:hud_remove(hud.id_breathbar)
|
||||||
@ -107,12 +107,8 @@ local function player_event_handler(player,eventname)
|
|||||||
initialize_builtin_statbars(player)
|
initialize_builtin_statbars(player)
|
||||||
|
|
||||||
if hud_ids[name].id_healthbar ~= nil then
|
if hud_ids[name].id_healthbar ~= nil then
|
||||||
local hp = player:get_hp()
|
player:hud_change(hud_ids[name].id_healthbar,
|
||||||
local max_display_hp = math.max(core.PLAYER_MAX_HP_DEFAULT,
|
"number", scaleToDefault(player, "hp"))
|
||||||
math.max(player:get_properties().hp_max, hp))
|
|
||||||
-- Limit width of health bar: Scale to the default maximal HP
|
|
||||||
local hp_count = hp / max_display_hp * core.PLAYER_MAX_HP_DEFAULT
|
|
||||||
player:hud_change(hud_ids[name].id_healthbar, "number", hp_count)
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -121,7 +117,8 @@ local function player_event_handler(player,eventname)
|
|||||||
initialize_builtin_statbars(player)
|
initialize_builtin_statbars(player)
|
||||||
|
|
||||||
if hud_ids[name].id_breathbar ~= nil then
|
if hud_ids[name].id_breathbar ~= nil then
|
||||||
player:hud_change(hud_ids[name].id_breathbar, "number", player:get_breath() * 2)
|
player:hud_change(hud_ids[name].id_breathbar,
|
||||||
|
"number", 2 * scaleToDefault(player, "breath"))
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3397,10 +3397,9 @@ This is basically a reference to a C++ `ServerActiveObject`
|
|||||||
* `get_breath()`: returns players breath
|
* `get_breath()`: returns players breath
|
||||||
* `set_breath(value)`: sets players breath
|
* `set_breath(value)`: sets players breath
|
||||||
* values:
|
* values:
|
||||||
* `0`: player is drowning,
|
* `0`: player is drowning
|
||||||
* `1`-`10`: remaining number of bubbles
|
* max: bubbles bar is not shown
|
||||||
* `11`: bubbles bar is not shown
|
* See Object Properties for more information
|
||||||
* See constant: `minetest.PLAYER_MAX_BREATH`
|
|
||||||
* `set_attribute(attribute, value)`:
|
* `set_attribute(attribute, value)`:
|
||||||
* Sets an extra attribute with value on player.
|
* Sets an extra attribute with value on player.
|
||||||
* `value` must be a string.
|
* `value` must be a string.
|
||||||
@ -4105,7 +4104,9 @@ Definition tables
|
|||||||
|
|
||||||
{
|
{
|
||||||
hp_max = 1,
|
hp_max = 1,
|
||||||
-- ^ For players, the maximal HP defaults to `minetest.PLAYER_MAX_HP_DEFAULT`
|
-- ^ For players: Defaults to `minetest.PLAYER_MAX_HP_DEFAULT`
|
||||||
|
breath_max = 0,
|
||||||
|
-- ^ For players only. Defaults to `minetest.PLAYER_MAX_BREATH_DEFAULT`
|
||||||
physical = true,
|
physical = true,
|
||||||
collide_with_objects = true, -- collide with other objects if physical = true
|
collide_with_objects = true, -- collide with other objects if physical = true
|
||||||
weight = 5,
|
weight = 5,
|
||||||
|
@ -89,11 +89,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
// Size of player's main inventory
|
// Size of player's main inventory
|
||||||
#define PLAYER_INVENTORY_SIZE (8 * 4)
|
#define PLAYER_INVENTORY_SIZE (8 * 4)
|
||||||
|
|
||||||
// Maximum hit points of a player
|
// Default maximum hit points of a player
|
||||||
#define PLAYER_MAX_HP_DEFAULT 20
|
#define PLAYER_MAX_HP_DEFAULT 20
|
||||||
|
|
||||||
// Maximal breath of a player
|
// Default maximal breath of a player
|
||||||
#define PLAYER_MAX_BREATH 11
|
#define PLAYER_MAX_BREATH_DEFAULT 11
|
||||||
|
|
||||||
// Number of different files to try to save a player to if the first fails
|
// Number of different files to try to save a player to if the first fails
|
||||||
// (because of a case-insensitive filesystem)
|
// (because of a case-insensitive filesystem)
|
||||||
|
@ -797,6 +797,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id
|
|||||||
assert(m_peer_id != 0); // pre-condition
|
assert(m_peer_id != 0); // pre-condition
|
||||||
|
|
||||||
m_prop.hp_max = PLAYER_MAX_HP_DEFAULT;
|
m_prop.hp_max = PLAYER_MAX_HP_DEFAULT;
|
||||||
|
m_prop.breath_max = PLAYER_MAX_BREATH_DEFAULT;
|
||||||
m_prop.physical = false;
|
m_prop.physical = false;
|
||||||
m_prop.weight = 75;
|
m_prop.weight = 75;
|
||||||
m_prop.collisionbox = aabb3f(-0.3f, 0.0f, -0.3f, 0.3f, 1.77f, 0.3f);
|
m_prop.collisionbox = aabb3f(-0.3f, 0.0f, -0.3f, 0.3f, 1.77f, 0.3f);
|
||||||
@ -817,6 +818,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id
|
|||||||
m_prop.stepheight = PLAYER_DEFAULT_STEPHEIGHT * BS;
|
m_prop.stepheight = PLAYER_DEFAULT_STEPHEIGHT * BS;
|
||||||
m_prop.can_zoom = true;
|
m_prop.can_zoom = true;
|
||||||
m_hp = m_prop.hp_max;
|
m_hp = m_prop.hp_max;
|
||||||
|
m_breath = m_prop.breath_max;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerSAO::~PlayerSAO()
|
PlayerSAO::~PlayerSAO()
|
||||||
@ -943,7 +945,7 @@ void PlayerSAO::step(float dtime, bool send_recommended)
|
|||||||
MapNode n = m_env->getMap().getNodeNoEx(p);
|
MapNode n = m_env->getMap().getNodeNoEx(p);
|
||||||
const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
|
const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
|
||||||
// If player is alive & no drowning, breath
|
// If player is alive & no drowning, breath
|
||||||
if (m_hp > 0 && m_breath < PLAYER_MAX_BREATH && c.drowning == 0)
|
if (m_hp > 0 && m_breath < m_prop.breath_max && c.drowning == 0)
|
||||||
setBreath(m_breath + 1);
|
setBreath(m_breath + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1274,7 +1276,7 @@ void PlayerSAO::setBreath(const u16 breath, bool send)
|
|||||||
if (m_player && breath != m_breath)
|
if (m_player && breath != m_breath)
|
||||||
m_player->setDirty(true);
|
m_player->setDirty(true);
|
||||||
|
|
||||||
m_breath = MYMIN(breath, PLAYER_MAX_BREATH);
|
m_breath = MYMIN(breath, m_prop.breath_max);
|
||||||
|
|
||||||
if (send)
|
if (send)
|
||||||
m_env->getGameDef()->SendPlayerBreath(this);
|
m_env->getGameDef()->SendPlayerBreath(this);
|
||||||
|
@ -397,7 +397,7 @@ private:
|
|||||||
std::set<std::string> m_privs;
|
std::set<std::string> m_privs;
|
||||||
bool m_is_singleplayer;
|
bool m_is_singleplayer;
|
||||||
|
|
||||||
u16 m_breath = PLAYER_MAX_BREATH;
|
u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
|
||||||
f32 m_pitch = 0.0f;
|
f32 m_pitch = 0.0f;
|
||||||
f32 m_fov = 0.0f;
|
f32 m_fov = 0.0f;
|
||||||
s16 m_wanted_range = 0.0f;
|
s16 m_wanted_range = 0.0f;
|
||||||
|
@ -173,7 +173,7 @@ private:
|
|||||||
// ***** End of variables for temporary option *****
|
// ***** End of variables for temporary option *****
|
||||||
|
|
||||||
bool m_can_jump = false;
|
bool m_can_jump = false;
|
||||||
u16 m_breath = PLAYER_MAX_BREATH;
|
u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
|
||||||
f32 m_yaw = 0.0f;
|
f32 m_yaw = 0.0f;
|
||||||
f32 m_pitch = 0.0f;
|
f32 m_pitch = 0.0f;
|
||||||
bool camera_barely_in_ceiling = false;
|
bool camera_barely_in_ceiling = false;
|
||||||
|
@ -34,6 +34,7 @@ std::string ObjectProperties::dump()
|
|||||||
{
|
{
|
||||||
std::ostringstream os(std::ios::binary);
|
std::ostringstream os(std::ios::binary);
|
||||||
os << "hp_max=" << hp_max;
|
os << "hp_max=" << hp_max;
|
||||||
|
os << ", breath_max=" << breath_max;
|
||||||
os << ", physical=" << physical;
|
os << ", physical=" << physical;
|
||||||
os << ", collideWithObjects=" << collideWithObjects;
|
os << ", collideWithObjects=" << collideWithObjects;
|
||||||
os << ", weight=" << weight;
|
os << ", weight=" << weight;
|
||||||
@ -108,6 +109,7 @@ void ObjectProperties::serialize(std::ostream &os) const
|
|||||||
os << serializeString(wield_item);
|
os << serializeString(wield_item);
|
||||||
writeU8(os, can_zoom);
|
writeU8(os, can_zoom);
|
||||||
writeS8(os, glow);
|
writeS8(os, glow);
|
||||||
|
writeU16(os, breath_max);
|
||||||
|
|
||||||
// Add stuff only at the bottom.
|
// Add stuff only at the bottom.
|
||||||
// Never remove anything, because we don't want new versions of this
|
// Never remove anything, because we don't want new versions of this
|
||||||
@ -155,5 +157,9 @@ void ObjectProperties::deSerialize(std::istream &is)
|
|||||||
infotext = deSerializeString(is);
|
infotext = deSerializeString(is);
|
||||||
wield_item = deSerializeString(is);
|
wield_item = deSerializeString(is);
|
||||||
can_zoom = readU8(is);
|
can_zoom = readU8(is);
|
||||||
glow = readS8(is);
|
|
||||||
|
try {
|
||||||
|
glow = readS8(is);
|
||||||
|
breath_max = readU16(is);
|
||||||
|
} catch (SerializationError &e) {}
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
|
|
||||||
struct ObjectProperties
|
struct ObjectProperties
|
||||||
{
|
{
|
||||||
// Values are BS=1
|
|
||||||
s16 hp_max = 1;
|
s16 hp_max = 1;
|
||||||
|
u16 breath_max = 0;
|
||||||
bool physical = false;
|
bool physical = false;
|
||||||
bool collideWithObjects = true;
|
bool collideWithObjects = true;
|
||||||
float weight = 5.0f;
|
float weight = 5.0f;
|
||||||
|
// Values are BS=1
|
||||||
aabb3f collisionbox = aabb3f(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f);
|
aabb3f collisionbox = aabb3f(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f);
|
||||||
aabb3f selectionbox = aabb3f(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f);
|
aabb3f selectionbox = aabb3f(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f);
|
||||||
bool pointable = true;
|
bool pointable = true;
|
||||||
|
@ -193,6 +193,7 @@ void read_object_properties(lua_State *L, int index,
|
|||||||
if (getintfield(L, -1, "hp_max", hp_max))
|
if (getintfield(L, -1, "hp_max", hp_max))
|
||||||
prop->hp_max = (s16)rangelim(hp_max, 0, S16_MAX);
|
prop->hp_max = (s16)rangelim(hp_max, 0, S16_MAX);
|
||||||
|
|
||||||
|
getintfield(L, -1, "breath_max", prop->breath_max);
|
||||||
getboolfield(L, -1, "physical", prop->physical);
|
getboolfield(L, -1, "physical", prop->physical);
|
||||||
getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
|
getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
|
||||||
|
|
||||||
@ -306,6 +307,8 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
|
|||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
lua_pushnumber(L, prop->hp_max);
|
lua_pushnumber(L, prop->hp_max);
|
||||||
lua_setfield(L, -2, "hp_max");
|
lua_setfield(L, -2, "hp_max");
|
||||||
|
lua_pushnumber(L, prop->breath_max);
|
||||||
|
lua_setfield(L, -2, "breath_max");
|
||||||
lua_pushboolean(L, prop->physical);
|
lua_pushboolean(L, prop->physical);
|
||||||
lua_setfield(L, -2, "physical");
|
lua_setfield(L, -2, "physical");
|
||||||
lua_pushboolean(L, prop->collideWithObjects);
|
lua_pushboolean(L, prop->collideWithObjects);
|
||||||
|
@ -2542,7 +2542,7 @@ void Server::RespawnPlayer(u16 peer_id)
|
|||||||
<< " respawns" << std::endl;
|
<< " respawns" << std::endl;
|
||||||
|
|
||||||
playersao->setHP(playersao->accessObjectProperties()->hp_max);
|
playersao->setHP(playersao->accessObjectProperties()->hp_max);
|
||||||
playersao->setBreath(PLAYER_MAX_BREATH);
|
playersao->setBreath(playersao->accessObjectProperties()->breath_max);
|
||||||
|
|
||||||
bool repositioned = m_script->on_respawnplayer(playersao);
|
bool repositioned = m_script->on_respawnplayer(playersao);
|
||||||
if (!repositioned) {
|
if (!repositioned) {
|
||||||
|
Loading…
Reference in New Issue
Block a user