Bugfixes with compound hudbars and health boost issue #4434

This commit is contained in:
WillConker 2024-06-14 20:13:31 +01:00
parent 32505a122e
commit 15c6b4ab31
4 changed files with 109 additions and 41 deletions

View File

@ -25,7 +25,7 @@ if minetest.settings:get_bool("enable_damage") or vl_hudbars.settings.forceload_
part_sort_index = 10,
take_up_space = true,
z_index = 0,
z_index_step = -1,
z_index_step = 1,
},
absorption = {
default_max_val = 0,
@ -38,7 +38,7 @@ if minetest.settings:get_bool("enable_damage") or vl_hudbars.settings.forceload_
part_sort_index = 8,
take_up_space = true,
z_index = 0,
z_index_step = -1,
z_index_step = 1,
}
}
}--hb.register_hudbar("absorption", 0xFFFFFF, S("Absorption"), {bar = "[fill:2x16:#B59500", icon = "mcl_potions_icon_absorb.png"}, 0, 0, 0, false)
@ -85,7 +85,7 @@ local function custom_hud(player)
end
local hp = player:get_hp()
local hp_max = player:get_properties().hp_max
minetest.debug("Going to init health")
minetest.debug("Going to init health to", math.min(hp, hp_max), hp_max)
vl_hudbars.init_hudbar(player, "health")
vl_hudbars.change_value(player, "health", math.min(hp, hp_max), hp_max, "health_main")
if hide then
@ -108,11 +108,13 @@ local function custom_hud(player)
end
end
local function update_health(player)
local function update_health(player, hp_change)
hp_change = hp_change or 0
local hp_max = player:get_properties().hp_max
local hp = player:get_hp()
local hp = player:get_hp() + hp_change
if hp > hp_max then hp = hp_max end
--minetest.debug("update_health", "new hp: " .. tostring(hp) .. " max_hp: " .. tostring(hp_max))
minetest.debug("Going to update health to", hp, hp_max)
vl_hudbars.change_value(player, "health", hp, hp_max, "health_main")
end
@ -121,7 +123,7 @@ local function update_hud(player, has_damage)
if not player or not player.get_player_name then return end
if has_damage then
if vl_hudbars.settings.forceload_default_hudbars then
vl_hudbars.show(player, "health", "health_main")
vl_hudbars.show(player, "health")
end
--air
local breath_max = player:get_properties().breath_max
@ -134,16 +136,16 @@ local function update_hud(player, has_damage)
vl_hudbars.change_value(player, "breath", math.min(breath, breath_max), breath_max)
end
--health
update_health(player)
--update_health(player)
elseif vl_hudbars.settings.forceload_default_hudbars then
vl_hudbars.hide(player, "health", "health_main")
vl_hudbars.hide(player, "health")
vl_hudbars.hide(player, "breath")
end
end
minetest.register_on_player_hpchange(function(player)
minetest.register_on_player_hpchange(function(player, hp_change)
if vl_hudbars.has_hudbar(player, "health") then
update_health(player)
update_health(player, hp_change)
end
end)

View File

@ -84,6 +84,20 @@ local function insert_hudbar(hudbar_order, identifier)
table.insert(hudbar_order, identifier)
end
-- Works out if a hudbar is hidden - for compound hudbars this is if all of their parts are hidden
local function is_hudbar_hidden(hudbar_state, is_compound)
if is_compound then
for _, part_state in pairs(hudbar_state.parts) do
-- If any parts are visible then this hudbar is visible
if not part_state.state.hidden then return false end
end
-- Otherwise it's hidden
return true
else
return hudbar_state.state.hidden
end
end
-- Gets the display height in pixels that this hudbar takes up, or nil if it takes up no space
-- `hudbar_def` is the general hudbar definition and `hudbar_state` is player-specific current state
local function get_hudbar_display_height(hudbar_state, hudbar_def)
@ -95,7 +109,6 @@ local function get_hudbar_display_height(hudbar_state, hudbar_def)
-- If hudbar does display, work out how many layers it has
elseif hudbar_def.is_compound then
if hudbar_def.value_type == "proportional" then
-- There are one fewer gaps than layers, so subtract layer_gap
for part_id, part_def in pairs(hudbar_def.parts) do
local part_state = hudbar_state.parts[part_id]
if not part_state.state.hidden then
@ -184,7 +197,7 @@ local function add_new_proportional_layer(player, hudbar_def, part_state, offset
else
type_field_name = "hud_elem_type"
end
minetest.debug("Adding new hudbar with info: ", dump({
--[[--minetest.debug("Adding new hudbar with info: ", dump({
[type_field_name] = "statbar",
position = vl_hudbars.settings.base_pos,
text = part_state.icon,
@ -196,7 +209,7 @@ local function add_new_proportional_layer(player, hudbar_def, part_state, offset
direction = hudbar_def.direction,
size = {x = vl_hudbars.settings.scale_x, y = texture_height_y},
z_index = z_index,
}))
}))]]
local layer_id = player:hud_add({
[type_field_name] = "statbar",
position = vl_hudbars.settings.base_pos,
@ -280,18 +293,32 @@ local function draw_proportional_hudbar(player, hudbar_state, hudbar_def, offset
local z_index = hudbar_def.z_index
local old_offset_y = offset_y
-- If all of the parts are hidden then this hudbar will be hidden
local is_hidden = true
if hudbar_def.is_compound then
for _, part_id in pairs(hudbar_state.parts_order) do
local part_def = hudbar_def.parts[part_id]
local part_state = hudbar_state.parts[part_id]
if not part_state.state.hidden then end
offset_y, z_index = draw_proportional_hudbar_part(player, part_state, part_def, hudbar_def, offset_y, offset_x_left, offset_x_right, texture_height_y, z_index)
z_index = z_index + part_def.z_index_step
-- If wasn't hidden, move up for next part
if not part_state.state.hidden then
is_hidden = false
offset_y = offset_y - texture_height_y - hudbar_def.layer_gap
end
end
else
-- Only has one part
is_hidden = hudbar_state.state.hidden
offset_y, z_index = draw_proportional_hudbar_part(player, hudbar_state, hudbar_def, hudbar_def, offset_y, offset_x_left, offset_x_right, texture_height_y, z_index)
end
if not is_hidden then
-- We added another gap, but we don't want one above the bar
offset_y = offset_y + hudbar_def.layer_gap
end
-- Return y-offset of top of hudbar
if hudbar_def.take_up_space then
return offset_y
@ -328,7 +355,7 @@ local function add_new_absolute_layer(player, hudbar_def, part_state, offset_y,
else
type_field_name = "hud_elem_type"
end
minetest.debug("Adding new hudbar with info: ", dump({
--[[--minetest.debug("Adding new hudbar with info: ", dump({
[type_field_name] = "statbar",
position = vl_hudbars.settings.base_pos,
text = part_state.icon,
@ -340,7 +367,7 @@ local function add_new_absolute_layer(player, hudbar_def, part_state, offset_y,
direction = hudbar_def.direction,
size = {x = vl_hudbars.settings.scale_x, y = texture_height_y},
z_index = z_index,
}))
}))]]
local layer_id = player:hud_add({
[type_field_name] = "statbar",
position = vl_hudbars.settings.base_pos,
@ -365,13 +392,13 @@ local function draw_absolute_hudbar_part(player, part_state, part_def, hudbar_de
local value_parts = part_state.state.value / hudbar_def.value_scale
local max_val_parts = part_state.state.max_val / hudbar_def.value_scale
-- Passed as `below_layer_parts` to next hudbar part
local top_layer_parts = 0
local top_layer_parts = below_layer_parts
local old_offset_y = offset_y
if not part_state.state.hidden then
local layer_index = 1
-- Draw continuation layer if applicable
if below_layer_parts < bar_length then
if below_layer_parts and below_layer_parts < bar_length then
-- Calculate value, max_val for continuation layer
local current_max_val_parts = math.min(max_val_parts, bar_length - below_layer_parts)
if hudbar_def.round_to_full_texture and current_max_val_parts % 2 == 1 then
@ -380,6 +407,7 @@ local function draw_absolute_hudbar_part(player, part_state, part_def, hudbar_de
max_val_parts = max_val_parts - current_max_val_parts
local current_value_parts = math.min(value_parts, current_max_val_parts)
value_parts = value_parts - current_value_parts
top_layer_parts = below_layer_parts + current_max_val_parts
-- Draw continuation layer
local layer_id = part_state.layer_ids[1]
@ -402,19 +430,21 @@ local function draw_absolute_hudbar_part(player, part_state, part_def, hudbar_de
player:hud_remove(part_state.layer_ids[i])
end
return offset_y, z_index, below_layer_parts + current_max_val_parts
-- Move up a layer if this layer is full
return offset_y, z_index, top_layer_parts
end
-- Otherwise keep going: draw next layers
-- Change z_index by step for next layer
z_index = z_index + part_def.z_index_step
layer_index = layer_index + 1
-- Update y-offset for next layer
minetest.debug("Adding y-offset from continuation layer")
end
if top_layer_parts ~= nil and max_val_parts > 0 then
-- Update y-offset for next layer, only if this is not the first part drawn or the last layer
--minetest.debug("Adding y-offset from continuation layer")
offset_y = offset_y - texture_height_y - hudbar_def.layer_gap
end
----minetest.debug("dahp while")
while max_val_parts > 0 do
-- Calculate value, max_val for layer
@ -449,8 +479,8 @@ local function draw_absolute_hudbar_part(player, part_state, part_def, hudbar_de
part_state.layer_ids[layer_index] = add_new_absolute_layer(player, hudbar_def, part_state, offset_y, offset_x, current_value_parts, current_max_val_parts, texture_height_y, z_index)
--minetest.debug("Added layer index " .. tostring(layer_index) .. " new value " .. tostring(current_max_val_parts) .. " val " .. tostring(current_value_parts))
end
-- Update y-offset for next layer, but only if this layer is full
if current_max_val_parts >= bar_length then
-- Update y-offset for next layer, unless this is the last layer
if max_val_parts > 0 then
offset_y = offset_y - texture_height_y - hudbar_def.layer_gap
end
layer_index = layer_index + 1
@ -482,31 +512,41 @@ end
-- Draws a whole absolute hudbar
local function draw_absolute_hudbar(player, hudbar_state, hudbar_def, offset_y, offset_x_left, offset_x_right)
minetest.debug("dah", hudbar_def.identifier)
local bar_length = vl_hudbars.settings.bar_length
local scale_x = vl_hudbars.settings.scale_x
local texture_height_y = math.floor((hudbar_def.scale_y) * scale_x + 0.5)
local z_index = hudbar_def.z_index
-- Initialise the `below_layer_parts` to a full bar so that bar drawing starts with a full layer
local below_layer_parts = bar_length
-- If all of the parts are hidden then this hudbar will be hidden
local is_hidden = true
-- Stores the number of max parts in the last layer drawn
local below_layer_parts = nil
if hudbar_def.is_compound then
minetest.debug("dah2", dump(hudbar_state.parts_order), dump(hudbar_state.parts))
--minetest.debug("dah2", dump(hudbar_state.parts_order), dump(hudbar_state.parts))
for _, part_id in pairs(hudbar_state.parts_order) do
minetest.debug("Drawing part", part_id)
--minetest.debug("Drawing part", part_id)
local part_def = hudbar_def.parts[part_id]
local part_state = hudbar_state.parts[part_id]
minetest.debug("Info before drawing part", below_layer_parts, offset_y)
--minetest.debug("Info before drawing part", below_layer_parts, offset_y)
offset_y, z_index, below_layer_parts = draw_absolute_hudbar_part(player, part_state, part_def, hudbar_def, offset_y, offset_x_left, offset_x_right, texture_height_y, z_index, below_layer_parts)
minetest.debug("Drew part", part_id, below_layer_parts, offset_y)
--minetest.debug("Drew part", part_id, below_layer_parts, offset_y)
z_index = z_index + part_def.z_index_step
if not part_state.state.hidden then
is_hidden = false
end
end
else
is_hidden = hudbar_state.state.hidden
----minetest.debug("dah")--, offset_y, dump(hudbar_state), dump(hudbar_def))
offset_y, z_index, below_layer_parts = draw_absolute_hudbar_part(player, hudbar_state, hudbar_def, hudbar_def, offset_y, offset_x_left, offset_x_right, texture_height_y, z_index, below_layer_parts)
----minetest.debug("dah 2", offset_y)
end
----minetest.debug("dah out")
if not is_hidden then
-- Move to top of hudbar so update_hudbar_display can get height
offset_y = offset_y - texture_height_y
end
return offset_y
end
@ -514,6 +554,7 @@ end
-- Also moves any hudbars above the bar being updated up or down if the height changed
-- This function must be called when an absolute hudbar changes height or a new hudbar is added
local function update_hudbar_display(player, identifier)
--minetest.debug("uhd_debug", identifier)
local name = player:get_player_name()
----minetest.debug(dump(vl_hudbars.players))
local hudstate = vl_hudbars.players[name]
@ -545,34 +586,54 @@ local function update_hudbar_display(player, identifier)
-- Loop through the hudbars until we find the relevant one, then update it and reposition all above
local is_above_updated = false
local hudbar_translation_amount
local is_hidden
for _, other_identifier in pairs(hudbar_order) do
if other_identifier == identifier then
local above_offset_y
if hudbar_def.value_type == "proportional" then
above_offset_y = draw_proportional_hudbar(player, hudbar_state, hudbar_def, offset_y, offset_x_left, offset_x_right)
above_offset_y, is_hidden = draw_proportional_hudbar(player, hudbar_state, hudbar_def, offset_y, offset_x_left, offset_x_right)
else
above_offset_y = draw_absolute_hudbar(player, hudbar_state, hudbar_def, offset_y, offset_x_left, offset_x_right)
above_offset_y, is_hidden = draw_absolute_hudbar(player, hudbar_state, hudbar_def, offset_y, offset_x_left, offset_x_right)
end
is_above_updated = true
local old_height = hudbar_state.current_height_pixels
-- Calculate amount to translate above hudbars by
-- Positive y-offset is down!
local new_height = (offset_y - above_offset_y)
local old_height = hudbar_state.current_height_pixels
local was_hidden = old_height == 0
local is_hidden = new_height == 0
--minetest.debug(identifier .. " was hidden? " .. tostring(was_hidden))
hudbar_state.current_height_pixels = new_height
hudbar_translation_amount = old_height - new_height
--minetest.debug("uhdheight", identifier, above_offset_y, offset_y, new_height, old_height, hudbar_translation_amount)
if was_hidden and not is_hidden then
-- The previous bottom hudbar doesn't have a gap beneath it, so we need push it up a bit extra
-- OR this bar was hidden and now it isn't, so we need to add a gap above
hudbar_translation_amount = hudbar_translation_amount - vl_hudbars.settings.hudbar_height_gap
--minetest.debug("Adding space")
elseif is_hidden and not was_hidden then
-- We just hid it, so there shouldn't be a gap above
hudbar_translation_amount = hudbar_translation_amount + vl_hudbars.settings.hudbar_height_gap
--minetest.debug("Subtracting space")
end
--minetest.debug("uhd_bar_height", identifier, above_offset_y, offset_y, new_height, old_height, hudbar_translation_amount, is_hidden, was_hidden)
elseif is_above_updated then -- above updated: update position by translation
if hudbar_translation_amount ~= 0 then
-- update position
--minetest.debug("uhd_translate", other_identifier, hudbar_translation_amount)
translate_hudbar(player, hudstate.hudbar_states[other_identifier], hudbar_translation_amount, hudbar_defs[other_identifier].is_compound)
end
else -- below updated: do not update, just add height to offset
-- add height to offset
local hudbar_height = get_hudbar_display_height(hudstate.hudbar_states[other_identifier], hudbar_defs[other_identifier])
--minetest.debug("uhd_calc_height1", other_identifier, offset_y)
local hudbar_height = hudstate.hudbar_states[other_identifier].current_height_pixels--get_hudbar_display_height(hudstate.hudbar_states[other_identifier], hudbar_defs[other_identifier])
offset_y = offset_y - hudbar_height
--minetest.debug("uhd_calc_height2", other_identifier, offset_y)
if hudbar_height ~= 0 then -- only add gap if hudbar is displayed/takes up space
offset_y = offset_y - vl_hudbars.settings.hudbar_height_gap
end
--minetest.debug("uhd_calc_height3", other_identifier, offset_y)
end
end
end
@ -628,7 +689,7 @@ end
-- Should be callable even if hudbar has already been inited, used to reset hudbar
function vl_hudbars.init_hudbar(player, identifier)
minetest.debug("Initing hudbar " .. identifier)
--minetest.debug("Initing hudbar " .. identifier)
if not (player ~= nil and player:is_player()) then return end
local name = player:get_player_name()
init_player_hudtracking(name)
@ -747,7 +808,6 @@ end
function vl_hudbars.change_value(player, identifier, value, max_val, part)
if not vl_hudbars.has_hudbar(player, identifier) then return end
minetest.debug("vl_hudbars.change_value", "Changing " .. identifier .. " value to " .. tostring(value) .. " max_val to " .. tostring(max_val) .. " part " .. tostring(part))
local name = player:get_player_name()
local hudbar_state = vl_hudbars.players[name].hudbar_states[identifier]
local hudbar_def = vl_hudbars.hudbar_defs[identifier]
@ -755,11 +815,14 @@ function vl_hudbars.change_value(player, identifier, value, max_val, part)
if hudbar_def.is_compound then
hudbar_state = hudbar_state.parts[part]
end
-- Don't update display if nothing changed
if value == hudbar_state.state.value and max_val == hudbar_state.state.max_val then return end
if value == nil then value = hudbar_state.state.value end
if max_val == nil then max_val = hudbar_state.state.max_val end
hudbar_state.state.value = value
hudbar_state.state.max_val = max_val
--minetest.debug("vl_hudbars.change_value", "Changing " .. identifier .. " value to " .. tostring(value) .. " max_val to " .. tostring(max_val) .. " part " .. tostring(part))
update_hudbar_display(player, identifier)
--minetest.debug("Finished changing value of hudbar " .. identifier)
end
@ -767,7 +830,6 @@ end
--function vl_hudbars.change_bg_texture(player, identifier, newtext)
function vl_hudbars.hide(player, identifier, part)
if not vl_hudbars.has_hudbar(player, identifier) then return end
minetest.debug("vl_hudbars.hide", "Hiding " .. identifier .. " part " .. tostring(part))
local name = player:get_player_name()
local hudbar_state = vl_hudbars.players[name].hudbar_states[identifier]
--minetest.debug("Hiding:", identifier, dump(vl_hudbars.players[name]), dump(vl_hudbars.players[name].hudbar_states), dump(vl_hudbars.players[name].hudbar_states[identifier]))
@ -799,13 +861,13 @@ function vl_hudbars.hide(player, identifier, part)
-- Only update display if hudbar wasn't already hidden
if state_changed then
--minetest.debug("vl_hudbars.hide", "Hiding " .. identifier .. " part " .. tostring(part))
update_hudbar_display(player, identifier)
end
--minetest.debug("Finished hiding hudbar " .. identifier)
end
function vl_hudbars.show(player, identifier, part)
minetest.debug("vl_hudbars.show", "Showing " .. identifier .. " part " .. tostring(part))
if not vl_hudbars.has_hudbar(player, identifier) then return end
local name = player:get_player_name()
local hudbar_state = vl_hudbars.players[name].hudbar_states[identifier]
@ -837,6 +899,7 @@ function vl_hudbars.show(player, identifier, part)
-- Only update display if hudbar wasn't already shown
if state_changed then
--minetest.debug("vl_hudbars.show", "Showing " .. identifier .. " part " .. tostring(part))
update_hudbar_display(player, identifier)
end
--minetest.debug("Finished showing hudbar " .. identifier)

View File

@ -597,6 +597,9 @@ mcl_potions.register_effect({
on_start = function(object, factor)
object:set_properties({hp_max = minetest.PLAYER_MAX_HP_DEFAULT+factor})
end,
on_load = function(object, factor)
object:set_properties({hp_max = minetest.PLAYER_MAX_HP_DEFAULT+factor})
end,
on_end = function(object)
object:set_properties({hp_max = minetest.PLAYER_MAX_HP_DEFAULT})
end,

View File

@ -320,7 +320,7 @@ mcl_potions.register_potion({
_longdesc = "Trolololololo",
stack_max = 2,
color = "#00AA00",
nocreative = true,
--nocreative = true,
_effect_list = {
night_vision = {},
strength = {},