Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix heart + bubble bar size on different texture packs
Add DPI support for statbar
Move heart+bubble bar to Lua HUD
Add statbar size (based upon an idea by blue42u)
Add support for customizing breath and statbar
  • Loading branch information
sapier authored and sapier committed May 7, 2014
1 parent c80d67f commit d3ee617
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 51 deletions.
1 change: 1 addition & 0 deletions builtin/builtin.lua
Expand Up @@ -29,3 +29,4 @@ dofile(modpath.."/features.lua")
dofile(modpath.."/voxelarea.lua")
dofile(modpath.."/vector.lua")
dofile(modpath.."/forceloading.lua")
dofile(modpath.."/statbars.lua")
1 change: 1 addition & 0 deletions builtin/misc_register.lua
Expand Up @@ -389,6 +389,7 @@ end

minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration()
minetest.registered_globalsteps, minetest.register_globalstep = make_registration()
minetest.registered_playerevents, minetest.register_playerevent = make_registration()
minetest.registered_on_mapgen_inits, minetest.register_on_mapgen_init = make_registration()
minetest.registered_on_shutdown, minetest.register_on_shutdown = make_registration()
minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
Expand Down
160 changes: 160 additions & 0 deletions builtin/statbars.lua
@@ -0,0 +1,160 @@

local health_bar_definition =
{
hud_elem_type = "statbar",
position = { x=0.5, y=1 },
text = "heart.png",
number = 20,
direction = 0,
size = { x=24, y=24 },
offset = { x=(-10*24)-25, y=-(48+24+10)},
}

local breath_bar_definition =
{
hud_elem_type = "statbar",
position = { x=0.5, y=1 },
text = "bubble.png",
number = 20,
direction = 0,
size = { x=24, y=24 },
offset = {x=25,y=-(48+24+10)},
}

local hud_ids = {}

local function initialize_builtin_statbars(player)

if not player:is_player() then
return
end

local name = player:get_player_name()

if name == "" then
return
end

if (hud_ids[name] == nil) then
hud_ids[name] = {}
end

if player:hud_get_flags().healthbar then
if hud_ids[name].id_healthbar == nil then
health_bar_definition.number = player:get_hp()
hud_ids[name].id_healthbar = player:hud_add(health_bar_definition)
end
else
if hud_ids[name].id_healthbar ~= nil then
player:hud_remove(hud_ids[name].id_healthbar)
hud_ids[name].id_healthbar = nil
end
end

if (player:get_breath() < 11) then
if player:hud_get_flags().breathbar then
if hud_ids[name].id_breathbar == nil then
hud_ids[name].id_breathbar = player:hud_add(breath_bar_definition)
end
else
if hud_ids[name].id_breathbar ~= nil then
player:hud_remove(hud_ids[name].id_breathbar)
hud_ids[name].id_breathbar = nil
end
end
elseif hud_ids[name].id_breathbar ~= nil then
player:hud_remove(hud_ids[name].id_breathbar)
hud_ids[name].id_breathbar = nil
end
end

local function cleanup_builtin_statbars(player)

if not player:is_player() then
return
end

local name = player:get_player_name()

if name == "" then
return
end

hud_ids[name] = nil
end

local function player_event_handler(player,eventname)
assert(player:is_player())

local name = player:get_player_name()

if name == "" then
return
end

if eventname == "health_changed" then
initialize_builtin_statbars(player)

if hud_ids[name].id_healthbar ~= nil then
player:hud_change(hud_ids[name].id_healthbar,"number",player:get_hp())
return true
end
end

if eventname == "breath_changed" then
initialize_builtin_statbars(player)

if hud_ids[name].id_breathbar ~= nil then
player:hud_change(hud_ids[name].id_breathbar,"number",player:get_breath()*2)
return true
end
end

if eventname == "hud_changed" then
initialize_builtin_statbars(player)
return true
end

return false
end

function minetest.hud_replace_builtin(name, definition)

if definition == nil or
type(definition) ~= "table" or
definition.hud_elem_type ~= "statbar" then
return false
end

if name == "health" then
health_bar_definition = definition

for name,ids in pairs(hud_ids) do

This comment has been minimized.

Copy link
@BlockMen

BlockMen May 8, 2014

Contributor

And here you try to change the healthbar for all players with name "health"...works great -.-

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja May 8, 2014

Member

The loop's name variable is local. But the name of one of the variables should be changed to prevent confusion.

This comment has been minimized.

Copy link
@BlockMen

BlockMen May 9, 2014

Contributor

@ShadowNinja indeed, you are right. then it was caused by some other problem. But i agree to rename varible to prevent future confusion.

This comment has been minimized.

Copy link
@Calinou

Calinou May 19, 2014

Member

On 0.4.9 servers, the breath bar looks like an health one with 5 hearts (which can divide in half).

local player = minetest.get_player_by_name(name)
if player and hud_ids[name].id_healthbar then
player:hud_remove(hud_ids[name].id_healthbar)
initialize_builtin_statbars(player)
end
end
return true
end

if name == "breath" then
breath_bar_definition = definition

for name,ids in pairs(hud_ids) do
local player = minetest.get_player_by_name(name)
if player and hud_ids[name].id_breathbar then
player:hud_remove(hud_ids[name].id_breathbar)
initialize_builtin_statbars(player)
end
end
return true
end

return false
end

minetest.register_on_joinplayer(initialize_builtin_statbars)
minetest.register_on_leaveplayer(cleanup_builtin_statbars)
minetest.register_playerevent(player_event_handler)
40 changes: 25 additions & 15 deletions doc/lua_api.txt
Expand Up @@ -475,6 +475,7 @@ values can be used.
The offset field specifies a pixel offset from the position. Contrary to position,
the offset is not scaled to screen size. This allows for some precisely-positioned
items in the HUD.
Note offset WILL adapt to screen dpi as well as user defined scaling factor!
Below are the specific uses for fields in each type; fields not listed for that type are ignored.

Note: Future revisions to the HUD API may be incompatible; the HUD API is still in the experimental stages.
Expand Down Expand Up @@ -504,6 +505,7 @@ Note: Future revisions to the HUD API may be incompatible; the HUD API is still
If odd, will end with a vertically center-split texture.
- direction
- offset: offset in pixels from position.
- size: If used will force full-image size to this value (override texture pack image size)
- inventory
- text: The name of the inventory list to be displayed.
- number: Number of items in the inventory to be displayed.
Expand All @@ -515,7 +517,7 @@ Note: Future revisions to the HUD API may be incompatible; the HUD API is still
- text: Distance suffix. Can be blank.
- number: An integer containing the RGB value of the color used to draw the text.
- world_pos: World position of the waypoint.

Representations of simple things
--------------------------------
Position/vector:
Expand Down Expand Up @@ -1212,7 +1214,7 @@ minetest.get_player_information(playername)
max_jitter = 0.5, -- maximum packet time jitter
avg_jitter = 0.03, -- average packet time jitter
connection_uptime = 200, -- seconds since client connected

-- following information is available on debug build only!!!
-- DO NOT USE IN MODS
--ser_vers = 26, -- serialization version used by client
Expand Down Expand Up @@ -1375,7 +1377,7 @@ minetest.dig_node(pos)
^ Dig node with the same effects that a player would cause
minetest.punch_node(pos)
^ Punch node with the same effects that a player would cause

minetest.get_meta(pos) -- Get a NodeMetaRef at that position
minetest.get_node_timer(pos) -- Get NodeTimerRef

Expand Down Expand Up @@ -1750,7 +1752,7 @@ methods:
- to_table() -> nil or {fields = {...}, inventory = {list1 = {}, ...}}
- from_table(nil or {})
^ See "Node Metadata"

NodeTimerRef: Node Timers - a high resolution persistent per-node timer
- Can be gotten via minetest.get_node_timer(pos)
methods:
Expand Down Expand Up @@ -1853,12 +1855,18 @@ Player-only: (no-op for other objects)
^ flags: (is visible) hotbar, healthbar, crosshair, wielditem
^ pass a table containing a true/false value of each flag to be set or unset
^ if a flag is nil, the flag is not modified
- hud_get_flags(): returns a table containing status of hud flags
^ returns { hotbar=true, healthbar=true, crosshair=true, wielditem=true, breathbar=true }
- hud_set_hotbar_itemcount(count): sets number of items in builtin hotbar
^ count: number of items, must be between 1 and 23
- hud_set_hotbar_image(texturename)
^ sets background image for hotbar
- hud_set_hotbar_selected_image(texturename)
^ sets image for selected item of hotbar
- hud_replace_builtin(name, hud definition)
^ replace definition of a builtin hud element
^ name: "breath" or "health"
^ hud definition: definition to replace builtin definition
- set_sky(bgcolor, type, {texture names})
^ bgcolor: {r=0...255, g=0...255, b=0...255} or nil, defaults to white
^ Available types:
Expand Down Expand Up @@ -2172,7 +2180,7 @@ Object Properties
Entity definition (register_entity)
{
(Deprecated: Everything in object properties is read directly from here)

initial_properties = <initial object properties>,

on_activate = function(self, staticdata, dtime_s),
Expand All @@ -2182,7 +2190,7 @@ Entity definition (register_entity)
get_staticdata = function(self),
^ Called sometimes; the string returned is passed to on_activate when
the entity is re-activated from static state

# Also you can define arbitrary member variables here
myvariable = whatever,
}
Expand Down Expand Up @@ -2345,7 +2353,7 @@ Node definition (register_node)
can_dig = function(pos,player)
^ returns true if node can be dug, or false if not
^ default: nil

on_punch = func(pos, node, puncher, pointed_thing),
^ default: minetest.node_punch
^ By default: Calls minetest.register_on_punchnode callbacks
Expand All @@ -2354,11 +2362,11 @@ Node definition (register_node)
^ if defined, itemstack will hold clicker's wielded item
^ Shall return the leftover itemstack
^ Note: pointed_thing can be nil, if a mod calls this function

on_dig = func(pos, node, digger),
^ default: minetest.node_dig
^ By default: checks privileges, wears out tool and removes node

on_timer = function(pos,elapsed),
^ default: nil
^ called by NodeTimers, see minetest.get_node_timer and NodeTimerRef
Expand All @@ -2374,12 +2382,12 @@ Node definition (register_node)
to_list, to_index, count, player),
^ Called when a player wants to move items inside the inventory
^ Return value: number of items allowed to move

allow_metadata_inventory_put = func(pos, listname, index, stack, player),
^ Called when a player wants to put something into the inventory
^ Return value: number of items allowed to put
^ Return value: -1: Allow and don't modify item count in inventory

allow_metadata_inventory_take = func(pos, listname, index, stack, player),
^ Called when a player wants to take something out of the inventory
^ Return value: number of items allowed to take
Expand All @@ -2391,7 +2399,7 @@ Node definition (register_node)
on_metadata_inventory_take = func(pos, listname, index, stack, player),
^ Called after the actual action has happened, according to what was allowed.
^ No return value

on_blast = func(pos, intensity),
^ intensity: 1.0 = mid range of regular TNT
^ If defined, called when an explosion touches the node, instead of
Expand Down Expand Up @@ -2543,17 +2551,17 @@ Detached inventory callbacks
allow_move = func(inv, from_list, from_index, to_list, to_index, count, player),
^ Called when a player wants to move items inside the inventory
^ Return value: number of items allowed to move

allow_put = func(inv, listname, index, stack, player),
^ Called when a player wants to put something into the inventory
^ Return value: number of items allowed to put
^ Return value: -1: Allow and don't modify item count in inventory

allow_take = func(inv, listname, index, stack, player),
^ Called when a player wants to take something out of the inventory
^ Return value: number of items allowed to take
^ Return value: -1: Allow and don't modify item count in inventory

on_move = func(inv, from_list, from_index, to_list, to_index, count, player),
on_put = func(inv, listname, index, stack, player),
on_take = func(inv, listname, index, stack, player),
Expand All @@ -2579,6 +2587,8 @@ HUD Definition (hud_add, hud_get)
^ See "HUD Element Types"
offset = {x=0, y=0},
^ See "HUD Element Types"
size = { x=100, y=100 },
^ Size of element in pixels
}

Particle definition (add_particle)
Expand Down
9 changes: 9 additions & 0 deletions src/client.cpp
Expand Up @@ -1787,9 +1787,13 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
v2f align = readV2F1000(is);
v2f offset = readV2F1000(is);
v3f world_pos;
v2s32 size;
try{
world_pos = readV3F1000(is);
}catch(SerializationError &e) {};
try{
size = readV2S32(is);
} catch(SerializationError &e) {};

ClientEvent event;
event.type = CE_HUDADD;
Expand All @@ -1805,6 +1809,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
event.hudadd.align = new v2f(align);
event.hudadd.offset = new v2f(offset);
event.hudadd.world_pos = new v3f(world_pos);
event.hudadd.size = new v2s32(size);
m_client_event_queue.push_back(event);
}
else if(command == TOCLIENT_HUDRM)
Expand All @@ -1825,6 +1830,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
v2f v2fdata;
v3f v3fdata;
u32 intdata = 0;
v2s32 v2s32data;

std::string datastring((char *)&data[2], datasize - 2);
std::istringstream is(datastring, std::ios_base::binary);
Expand All @@ -1839,6 +1845,8 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
sdata = deSerializeString(is);
else if (stat == HUD_STAT_WORLD_POS)
v3fdata = readV3F1000(is);
else if (stat == HUD_STAT_SIZE )
v2s32data = readV2S32(is);
else
intdata = readU32(is);

Expand All @@ -1850,6 +1858,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
event.hudchange.v3fdata = new v3f(v3fdata);
event.hudchange.sdata = new std::string(sdata);
event.hudchange.data = intdata;
event.hudchange.v2s32data = new v2s32(v2s32data);
m_client_event_queue.push_back(event);
}
else if(command == TOCLIENT_HUD_SET_FLAGS)
Expand Down

4 comments on commit d3ee617

@BlockMen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You broke backwards compatibility:
screenshot_142300662

The builtin statbars gets not hidden by flags...

@BlockMen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the health bar is shown aswell when damage is disabled. You did no tests before merging, no?

@Calinou
Copy link
Member

@Calinou Calinou commented on d3ee617 May 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.4.9 servers show no health bar, regardless of texture pack. This makes PvP quite trickier. ;)

@HybridDog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some time the whole hud is hidden, not only the hearts.

Please sign in to comment.