From f3d5b6edb5c50e4897e393e7182c927417d7368b Mon Sep 17 00:00:00 2001 From: wsor <24964441+wsor4035@users.noreply.github.com> Date: Sun, 4 Jul 2021 22:37:05 -0400 Subject: [PATCH] add hunger system --- mods/fl_hunger/init.lua | 154 ++++++++++++++++++++++++ mods/fl_hunger/textures/hunger.png | Bin 0 -> 802 bytes mods/fl_hunger/textures/hunger.png~ | Bin 0 -> 553 bytes mods/fl_hunger/textures/hunger_gone.png | Bin 0 -> 274 bytes mods/fl_wildlife/node_items.lua | 1 + 5 files changed, 155 insertions(+) create mode 100644 mods/fl_hunger/init.lua create mode 100644 mods/fl_hunger/textures/hunger.png create mode 100644 mods/fl_hunger/textures/hunger.png~ create mode 100644 mods/fl_hunger/textures/hunger_gone.png diff --git a/mods/fl_hunger/init.lua b/mods/fl_hunger/init.lua new file mode 100644 index 0000000..a02dc12 --- /dev/null +++ b/mods/fl_hunger/init.lua @@ -0,0 +1,154 @@ +--[[ + --dump of breath bar +{ + scale = {y=0, x=0}, + position = {y=1, x=0.5}, + z_index = 0, + alignment = {y=0, x=0}, + direction = 0, + text2 = "bubble_gone.png", + text = "bubble.png", + number = 18, + offset = {y=-88, x=25}, + world_pos={y=0, x=0, z=0}, + dir = 0, + type = "statbar", + item = 20, + name = "", + size = {y=24,x=24}, +} +--]] + +--do nothing here if damage not enabled +if not minetest.settings:get_bool("enable_damage") then return end + +local hunger_data = {} + + +local breath_bar_def = { + hud_elem_type = "statbar", + position = {x=0.5, y=1}, + text = "bubble.png", + text2 = "bubble_gone.png", + number = 18, + item = 20, + direction = 0, + size = {x = 24, y = 24}, + offset = {y=-88+-16*2, x=25}, +} + +core.hud_replace_builtin("breath", breath_bar_def) + +--init data +minetest.register_on_joinplayer(function(player, last_login) + local hv = player:get_meta():get("hunger_value") or 20 + hunger_data[player:get_player_name()] = {hunger_value = hv, node_interact = 0} + + local id = player:hud_add({ + hud_elem_type = "statbar", + position = {x=0.5, y=1}, + text = "hunger.png", + text2 = "hunger_gone.png", + number = hv, + item = 20, + direction = 0, + size = {x = 24, y = 24}, + offset = {y=-88, x=25}, + }) + hunger_data[player:get_player_name()]["id"] = id +end) + +--write data +minetest.register_on_leaveplayer(function(player, timed_out) + player:get_meta():set_string("hunger_value", hunger_data[player:get_player_name()]["hunger_value"]) +end) +minetest.register_on_shutdown(function() + for _, player in pairs(minetest.get_connected_players()) do + player:get_meta():set_string("hunger_value", hunger_data[player:get_player_name()]["hunger_value"]) + end +end) +local function write_data() + for _, player in pairs(minetest.get_connected_players()) do + player:get_meta():set_string("hunger_value", hunger_data[player:get_player_name()]["hunger_value"]) + end + minetest.after(600, write_data) +end +minetest.after(600, write_data) + +local old_eat = minetest.do_item_eat +function minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing) + + if hunger_data[user:get_player_name()]["hunger_value"] < 20 then + local hpchange = 0 + local change = hunger_data[user:get_player_name()]["hunger_value"] + hp_change + + if change > 20 then + hpchange = change - 20 + hunger_data[user:get_player_name()]["hunger_value"] = 20 + else + hunger_data[user:get_player_name()]["hunger_value"] = change + end + + user:hud_change(hunger_data[user:get_player_name()]["id"], "number", hunger_data[user:get_player_name()]["hunger_value"]) + + return old_eat(hpchange, replace_with_item, itemstack, user, pointed_thing) + else + return old_eat(hp_change, replace_with_item, itemstack, user, pointed_thing) + end +end + +local function node_hunger(_, _, player, test, _, _, _, _, _, val) + if not player or not player:is_player() or player.is_fake_player == true then return end + local interaction = hunger_data[player:get_player_name()]["node_interact"] + + if test then + --digging + interaction = interaction + 3 + elseif val then + --custom input + interaction = interaction + val + else + --placing/other stuff only a point + interaction = interaction + 1 + end + + if interaction > 150 then + if tonumber(hunger_data[player:get_player_name()]["hunger_value"]) >= 1 then + hunger_data[player:get_player_name()]["hunger_value"] = hunger_data[player:get_player_name()]["hunger_value"] - 1 + else + player:set_hp(player:get_hp()-1) + end + player:hud_change(hunger_data[player:get_player_name()]["id"], "number", hunger_data[player:get_player_name()]["hunger_value"]) + interaction = 0 + end + hunger_data[player:get_player_name()]["node_interact"] = interaction +end + +minetest.register_on_placenode(node_hunger) +minetest.register_on_dignode(node_hunger) + +local function after() + for _, player in pairs(minetest.get_connected_players()) do + local controls = player:get_player_control() + if controls.up or controls.down or controls.left or controls.right then + node_hunger(nil, nil, player, "randomness") + else + node_hunger(nil, nil, player) + end + end + minetest.after(4, after) +end + +minetest.after(4, after) + + + + + +--testing debug junk +fl_hunger = {} + +function fl_hunger.set_hunger(user, value) + hunger_data[user:get_player_name()]["hunger_value"] = value + user:hud_change(hunger_data[user:get_player_name()]["id"], "number", hunger_data[user:get_player_name()]["hunger_value"]) +end \ No newline at end of file diff --git a/mods/fl_hunger/textures/hunger.png b/mods/fl_hunger/textures/hunger.png new file mode 100644 index 0000000000000000000000000000000000000000..1aae2b8a3b90a00d7c53317c61692a38a28ba4be GIT binary patch literal 802 zcmV+-1Ks?IP)?t9dr%z*Dk&b~qF3*_t}$3tZ3g)IiRYNb9pZW7%$8#b-Xk7i zWw}UvLLB3CK;m1WD;~cIE_wVVnCG(#fM4{KN*%0pFe`I|c#1e`=vCG_kGM%#HCJ+! z$7qeiMiyzLAVEPDo6zAPPT8cHb@wHjzGYO%!{~3J22PF=%H)laTBWWkWsP)|J{>hN z_y6;>wb>c3dpHI3y}It_1Q6N>8gzLB7z7u+9{Tbs9+^3h>fwZ&>|_t5R`~XOf+}7yY=1W z!%D2&ZQj0}d2eQb|32d3bQyg^BmSP3#~TIL0R?r~UsVVqSk*E{Q@f%qM68Sx=C%(} zxW7_I(m{x2WdFJB&yiQtG_K2KZY!4vcUNR;Ac+ zDk-XvJ=tOa!8U6wC&*X=k%S@_R;>c%@_^PMqo-9VS3F%GK(c-z1Y8D5ErfjL3}cJu z9S@Qa@EKt?UVhH{wI@k<8?8E|-i1Q$2*<0_@Srr%+wX(gNtx9W+=hvI<&6_6K?Jm> zgyvj`rG3{HxkC;$Ke07*qoM6N<$f}B@iWB>pF literal 0 HcmV?d00001 diff --git a/mods/fl_hunger/textures/hunger.png~ b/mods/fl_hunger/textures/hunger.png~ new file mode 100644 index 0000000000000000000000000000000000000000..83e8941d067149c36b7909f32b14cc8ab33642c7 GIT binary patch literal 553 zcmV+^0@nSBP)D849qdZeIWZ0k%m* zK~y-))swMn6hRope=|4P@v>?{yl@_;VGR?$j?QxIb1 zzmOnFz%m@DAko4?#nVodGh*~|cZu0`cRq`aUX~cJv2d#4!S{Rf_};*Of7GAEspGpj zSuUlt{?Sta44=7~(#@~S%Y|zI>?@A#=(bW@;_A4^-N)f}z}DgAS5E{Nqq{88_clsv z!uBe0xUMc-JVzAHa&O52paJHlhM2u`3xN8IMalN&$qgN2|L{14qh<0VC+Hg(MHwpq zaqPG;(c1xhsyFH0d_~auhTne>V=OL?K!L45G=u4~D5Y>N!g#y*`0OD{8wDVR z1o9hDJWxWD0U%c#BV)4wNd$o?+`!l@gQqX?=Jg`FN1;lp1DLq}oPoWEQ=16d4cw1_ zjphnryMfAhiDlx$<@=IeWBK+ws+B_kczzEbo-QCFh>OX17Hw>5aK|Rr-#*w@pxXR2 r#j{r^44(h{j#n#_R4bGJ{xkgq=X#pIfmp)b00000NkvXXu0mjfP%rwA literal 0 HcmV?d00001 diff --git a/mods/fl_hunger/textures/hunger_gone.png b/mods/fl_hunger/textures/hunger_gone.png new file mode 100644 index 0000000000000000000000000000000000000000..4a31242b54fceda3cea196c6083d758a5a8b9c74 GIT binary patch literal 274 zcmeAS@N?(olHy`uVBq!ia0vp^0wBx?BpA#)4xIr~Ea{HEjtmSN`?>!lvI6-E$sR$z z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBD8W<`RdP`(kYX@0Ff`XSG|)9P4l%T_GBLL@HrF;Vv@$T*WTH3$MMG|WN@iLm WZViVsXX*eoFnGH9xvX