Add support for 3darmor mod, add poisen food and restructure code

This commit is contained in:
BlockMen 2013-09-13 20:18:16 +02:00
parent 01e267e99a
commit 09fc5ee48a
7 changed files with 139 additions and 49 deletions

@ -1,6 +1,6 @@
Minetest mod "Better HUD" Minetest mod "Better HUD"
========================= =========================
version: 1.0 version: 1.1
License of source code: WTFPL License of source code: WTFPL
----------------------------- -----------------------------
@ -15,6 +15,8 @@ hud_hunger_fg.png - PilzAdam (WTFPL), modified by BlockMen
hud_hunger_bg.png - PilzAdam (WTFPL), modified by BlockMen hud_hunger_bg.png - PilzAdam (WTFPL), modified by BlockMen
wieldhand.png (from character.png) - Jordach (CC BY-SA 3.0), modified by BlockMen wieldhand.png (from character.png) - Jordach (CC BY-SA 3.0), modified by BlockMen
hud_air_fg.png - kaeza (WTFPL), modified by BlockMen hud_air_fg.png - kaeza (WTFPL), modified by BlockMen
hud_armor_fg.png - Stu (CC BY-SA 3.0), modified by BlockMen
hud_armor_bg.png - Stu (CC BY-SA 3.0), modified by BlockMen
everything else is WTFPL: everything else is WTFPL:
(c) Copyright BlockMen (2013) (c) Copyright BlockMen (2013)
@ -29,11 +31,12 @@ http://sam.zoy.org/wtfpl/COPYING for more details.
Using the mod: Using the mod:
-------------- --------------
This mod changes the HUD of Minetest. It adds a costum crosshair, a improved health bar, breath bar and a more fancy inventory bar. This mod changes the HUD of Minetest.
Also it adds hunger to the game and and hunger bar to the HUD. It improves the apperance of the health and breath bar and adds a more fancy hotbar. Furthermore it adds a
costum crosshair, an armor bar (only for 3darmor mod) and a hunger bar. It includes also a mechanic for hunger.
You can create a "hud.conf" to costumize the positions of health, hunger and breath bar. Take a look at "hud.conf.example" to get more infos. You can create a "hud.conf" to costumize the positions of health, hunger, armor and breath bar. Take a look at "hud.conf.example" to get more infos.
!!NOTICE: Keep in mind if running a server with this mod, that the costum position should be displayed correct on every screen size!! !!NOTICE: Keep in mind if running a server with this mod, that the costum position should be displayed correct on every screen size!!
@ -56,3 +59,5 @@ Currently supported food:
- Mtfoods - Mtfoods
Example: 1 apple fills up the hunger bar by 1 bread, 1 bread (from farming) 2 breads in bar. Example: 1 apple fills up the hunger bar by 1 bread, 1 bread (from farming) 2 breads in bar.
Altough it show 20 hunger points (10 breads) in hunger bar you can fill it up to 30 points. (5 breads not shown then)

31
armor.lua Normal file

@ -0,0 +1,31 @@
minetest.after(0, function()
if not armor.def then
minetest.after(2,minetest.chat_send_all,"#Better HUD: Please update your version of 3darmor")
HUD_SHOW_ARMOR = false
end
end)
function hud.get_armor(player)
if not player or not armor.def then
return
end
local name = player:get_player_name()
hud.set_armor(player, armor.def[name].state, armor.def[name].count)
end
function hud.set_armor(player, ges_state, items)
if not player then return end
local max_items = 4
if items == 5 then max_items = items end
local max = max_items*65535
local lvl = max - ges_state
lvl = lvl/max
if ges_state == 0 and items == 0 then
lvl = 0
end
hud.armor[player:get_player_name()] = lvl*(items*(20/max_items))
end

@ -39,3 +39,9 @@
- fixed revival of player when drown - fixed revival of player when drown
- hunger bar is not shown anymore if hunger is disabled - hunger bar is not shown anymore if hunger is disabled
- hunger can be disabled by minetest.conf ("hud_hunger_enable = false") - hunger can be disabled by minetest.conf ("hud_hunger_enable = false")
1.1
---
- added support for stu's 3darmor mod
- restructured and cleaned up code
- added support for poisen food (damages player, but does not kill)

@ -1,4 +1,35 @@
function hud.item_eat(hunger_change, replace_with_item) function hud.save_hunger(player)
local file = io.open(minetest.get_worldpath().."/hud_"..player:get_player_name().."_hunger", "w+")
if file then
file:write(hud.hunger[player:get_player_name()])
file:close()
end
end
function hud.load_hunger(player)
local file = io.open(minetest.get_worldpath().."/hud_"..player:get_player_name().."_hunger", "r")
if file then
hud.hunger[player:get_player_name()] = file:read("*all")
file:close()
return hud.hunger[player:get_player_name()]
else
return
end
end
local function poisenp(tick, time, time_left, player)
time_left = time_left + tick
if time_left < time then
minetest.after(tick, poisenp, tick, time, time_left, player)
end
if player:get_hp()-1 > 0 then
player:set_hp(player:get_hp()-1)
end
end
function hud.item_eat(hunger_change, replace_with_item, poisen)
return function(itemstack, user, pointed_thing) return function(itemstack, user, pointed_thing)
if itemstack:take_item() ~= nil then if itemstack:take_item() ~= nil then
local h = tonumber(hud.hunger[user:get_player_name()]) local h = tonumber(hud.hunger[user:get_player_name()])
@ -8,15 +39,18 @@ function hud.item_eat(hunger_change, replace_with_item)
hud.save_hunger(user) hud.save_hunger(user)
itemstack:add_item(replace_with_item) -- note: replace_with_item is optional itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
--sound:eat --sound:eat
if poisen then
poisenp(1.0, poisen, 0, user)
end
end end
return itemstack return itemstack
end end
end end
local function overwrite(name, hunger_change, replace_with_item) local function overwrite(name, hunger_change, replace_with_item, poisen)
local tab = minetest.registered_items[name] local tab = minetest.registered_items[name]
if tab == nil then return end if tab == nil then return end
tab.on_use = hud.item_eat(hunger_change)--, replace_with_item) tab.on_use = hud.item_eat(hunger_change, replace_with_item, poisen)
minetest.registered_items[name] = tab minetest.registered_items[name] = tab
end end
@ -58,8 +92,8 @@ if minetest.get_modpath("animalmaterials") ~= nil then
overwrite("animalmaterials:meat_chicken", 3) overwrite("animalmaterials:meat_chicken", 3)
overwrite("animalmaterials:meat_lamb", 3) overwrite("animalmaterials:meat_lamb", 3)
overwrite("animalmaterials:meat_venison", 3) overwrite("animalmaterials:meat_venison", 3)
--overwrite("animalmaterials:meat_undead", 3)-- -3 damage overwrite("animalmaterials:meat_undead", 3, "", 3)
--overwrite("animalmaterials:meat_toxic", 3)-- -5 damage overwrite("animalmaterials:meat_toxic", 3, "", 5)
overwrite("animalmaterials:meat_ostrich", 3) overwrite("animalmaterials:meat_ostrich", 3)
overwrite("animalmaterials:fish_bluewhite", 2) overwrite("animalmaterials:fish_bluewhite", 2)
overwrite("animalmaterials:fish_clownfish", 2) overwrite("animalmaterials:fish_clownfish", 2)

@ -4,12 +4,15 @@ local health_hud = {}
hud.hunger = {} hud.hunger = {}
local hunger_hud = {} local hunger_hud = {}
local air_hud = {} local air_hud = {}
local inv_hud = {} hud.armor = {}
local armor_hud = {}
local SAVE_INTERVAL = 0.5*60--currently useless local SAVE_INTERVAL = 0.5*60--currently useless
--default settings --default settings
HUD_ENABLE_HUNGER = minetest.setting_getbool("hud_hunger_enable") HUD_ENABLE_HUNGER = minetest.setting_getbool("hud_hunger_enable")
HUD_SHOW_ARMOR = false
if minetest.get_modpath("3d_armor") ~= nil then HUD_SHOW_ARMOR = true end
if HUD_ENABLE_HUNGER == nil then HUD_ENABLE_HUNGER = minetest.setting_getbool("enable_damage") end if HUD_ENABLE_HUNGER == nil then HUD_ENABLE_HUNGER = minetest.setting_getbool("enable_damage") end
HUD_HUNGER_TICK = 300 HUD_HUNGER_TICK = 300
HUD_HEALTH_POS = {x=0.5,y=0.9} HUD_HEALTH_POS = {x=0.5,y=0.9}
@ -18,6 +21,8 @@ HUD_HUNGER_POS = {x=0.5,y=0.9}
HUD_HUNGER_OFFSET = {x=15, y=2} HUD_HUNGER_OFFSET = {x=15, y=2}
HUD_AIR_POS = {x=0.5,y=0.9} HUD_AIR_POS = {x=0.5,y=0.9}
HUD_AIR_OFFSET = {x=15,y=-15} HUD_AIR_OFFSET = {x=15,y=-15}
HUD_ARMOR_POS = {x=0.5,y=0.9}
HUD_ARMOR_OFFSET = {x=-175, y=-15}
--load costum settings --load costum settings
local set = io.open(minetest.get_modpath("hud").."/hud.conf", "r") local set = io.open(minetest.get_modpath("hud").."/hud.conf", "r")
@ -93,40 +98,63 @@ local function costum_hud(player)
position = HUD_AIR_POS, position = HUD_AIR_POS,
scale = {x=1, y=1}, scale = {x=1, y=1},
text = "hud_air_fg.png", text = "hud_air_fg.png",
number = 20, number = 0,
alignment = {x=-1,y=-1}, alignment = {x=-1,y=-1},
offset = HUD_AIR_OFFSET, offset = HUD_AIR_OFFSET,
}) })
--armor
if HUD_SHOW_ARMOR then
player:hud_add({
hud_elem_type = "statbar",
position = HUD_ARMOR_POS,
scale = {x=1, y=1},
text = "hud_armor_bg.png",
number = 20,
alignment = {x=-1,y=-1},
offset = HUD_ARMOR_OFFSET,
})
armor_hud[player:get_player_name()] = player:hud_add({
hud_elem_type = "statbar",
position = HUD_ARMOR_POS,
scale = {x=1, y=1},
text = "hud_armor_fg.png",
number = 0,
alignment = {x=-1,y=-1},
offset = HUD_ARMOR_OFFSET,
})
end
end end
end end
--needs to be set always(for 3darmor)
function hud.set_armor()
end
if HUD_ENABLE_HUNGER then dofile(minetest.get_modpath("hud").."/hunger.lua") end
if HUD_SHOW_ARMOR then dofile(minetest.get_modpath("hud").."/armor.lua") end
local function update_hud(player) local function update_hud(player)
--health --air
local air = player:get_breath()*2
if player:get_breath() > 10 then air = 0 end
player:hud_change(air_hud[player:get_player_name()], "number", air)
--health
player:hud_change(health_hud[player:get_player_name()], "number", player:get_hp()) player:hud_change(health_hud[player:get_player_name()], "number", player:get_hp())
--hunger --armor
local arm = tonumber(hud.armor[player:get_player_name()])
if not arm then arm = 0 end
player:hud_change(armor_hud[player:get_player_name()], "number", arm)
--hunger
local h = tonumber(hud.hunger[player:get_player_name()]) local h = tonumber(hud.hunger[player:get_player_name()])
if h>20 then h=20 end if h>20 then h=20 end
player:hud_change(hunger_hud[player:get_player_name()], "number", h) player:hud_change(hunger_hud[player:get_player_name()], "number", h)
end end
local function update_fast(player)
--air
local air = player:get_breath()*2
if player:get_breath() >= 11 then air = 0 end
player:hud_change(air_hud[player:get_player_name()], "number", air)
end
function hud.save_hunger(player)
local file = io.open(minetest.get_worldpath().."/hud_"..player:get_player_name().."_hunger", "w+")
if file then
file:write(hud.hunger[player:get_player_name()])
file:close()
end
end
local function timer(interval, player) local function timer(interval, player)
if interval > 0 then if interval > 0 then
hud.save_hunger(player) hud.save_hunger(player)
@ -134,35 +162,23 @@ local function timer(interval, player)
end end
end end
local function load_hunger(player)
local file = io.open(minetest.get_worldpath().."/hud_"..player:get_player_name().."_hunger", "r")
if file then
hud.hunger[player:get_player_name()] = file:read("*all")
file:close()
return hud.hunger[player:get_player_name()]
else
return
end
end
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
hud.hunger[player:get_player_name()] = load_hunger(player) hud.armor[player:get_player_name()] = 0
if hud.hunger[player:get_player_name()] == nil then if HUD_ENABLE_HUNGER then hud.hunger[player:get_player_name()] = hud.load_hunger(player) end
if not hud.hunger[player:get_player_name()] then
hud.hunger[player:get_player_name()] = 20 hud.hunger[player:get_player_name()] = 20
end end
minetest.after(0.5, function() minetest.after(0.5, function()
hud.save_hunger(player)
hide_builtin(player) hide_builtin(player)
costum_hud(player) costum_hud(player)
if HUD_ENABLE_HUNGER then hud.save_hunger(player) end
end) end)
end) end)
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
hud.hunger[player:get_player_name()] = 20 hud.hunger[player:get_player_name()] = 20
minetest.after(0.5, function() minetest.after(0.5, function()
hud.save_hunger(player) if HUD_ENABLE_HUNGER then hud.save_hunger(player) end
end) end)
end) end)
@ -173,7 +189,6 @@ minetest.after(2.5, function()
timer = timer + dtime timer = timer + dtime
timer2 = timer2 + dtime timer2 = timer2 + dtime
for _,player in ipairs(minetest.get_connected_players()) do for _,player in ipairs(minetest.get_connected_players()) do
update_fast(player)
if minetest.setting_getbool("enable_damage") then if minetest.setting_getbool("enable_damage") then
local h = tonumber(hud.hunger[player:get_player_name()]) local h = tonumber(hud.hunger[player:get_player_name()])
if HUD_ENABLE_HUNGER and timer > 4 then if HUD_ENABLE_HUNGER and timer > 4 then
@ -190,6 +205,7 @@ minetest.after(2.5, function()
hud.save_hunger(player) hud.save_hunger(player)
end end
end end
if HUD_SHOW_ARMOR then hud.get_armor(player) end
update_hud(player) update_hud(player)
end end
end end
@ -197,5 +213,3 @@ minetest.after(2.5, function()
if timer2>HUD_HUNGER_TICK then timer2=0 end if timer2>HUD_HUNGER_TICK then timer2=0 end
end) end)
end) end)
if HUD_ENABLE_HUNGER then dofile(minetest.get_modpath("hud").."/hunger.lua") end

BIN
textures/hud_armor_bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

BIN
textures/hud_armor_fg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 507 B