SHARE
TWEET

Voxelgarden/-0.4.16.3/mods/hunger/init.lua:37

webmanoffesto Dec 1st, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Voxelgarden/-0.4.16.3/mods/hunger/init.lua:37
  2.  
  3. hunger = {}
  4. local player_is_active = {}
  5. local player_step = {}
  6. local player_bar = {}
  7. local base_interval = 0.5
  8.  
  9. -- no_hunger privilege
  10. minetest.register_privilege("no_hunger", {
  11.     description = "Player will feel no hunger.",
  12.     give_to_singleplayer = false
  13. })
  14.  
  15. -- Hunger bar
  16. function hunger.update_bar(player, full)
  17.     if not player then return end
  18.     if not full then return end
  19.     local name = player:get_player_name()
  20.     if minetest.get_player_privs(name)["no_hunger"] then
  21.         player:hud_remove(player_bar[name])
  22.         return
  23.     end
  24.     if player_bar[name] then
  25.         player:hud_change(player_bar[name], "number", full)
  26.     else
  27.         player_bar[name] = player:hud_add({
  28.             hud_elem_type = "statbar",
  29.             position = {x=0.5,y=1.0},
  30.             text = "hunger.png",
  31.             number = full,
  32.             dir = 1,
  33.             offset = {x=(9*24)-6,y=-(3*24+8)},
  34.             size = {x=16, y=16},
  35.         })
  36.     end
  37. end
  38.  
  39. if minetest.settings:get_bool("enable_damage") == true then
  40.  
  41. -- Prevent players from starving while afk (<--joke)
  42. minetest.register_on_dignode(function(pos, oldnode, player)
  43.     if not player then return end
  44.     local name = player:get_player_name()
  45.     player_is_active[name] = true
  46. end)
  47.  
  48. minetest.register_on_placenode(function(pos, node, player)
  49.     if not player then return end
  50.     local name = player:get_player_name()
  51.     player_is_active[name] = true
  52. end)
  53.  
  54. minetest.register_on_joinplayer(function(player)
  55.     local name = player:get_player_name()
  56.     local full = player:get_attribute("hunger")
  57.     if not full then full = 20 end
  58.     hunger.update_bar(player, full)
  59.     player:set_attribute("hunger", full)
  60. end)
  61.  
  62. minetest.register_on_respawnplayer(function(player)
  63.     local name = player:get_player_name()
  64.     full = 20
  65.     hunger.update_bar(player, full)
  66.     player:set_attribute("hunger", full)
  67. end)
  68.  
  69. minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, player, pointed_thing)
  70.     if not player then return end
  71.     if not hp_change then return end
  72.     local full = player:get_attribute("hunger")
  73.     -- For each health, we add 2 full
  74.     if full + 2*hp_change > 20 then
  75.         full = 20
  76.     else
  77.         full = full + 2*hp_change
  78.     end
  79.     local headpos  = player:getpos()
  80.     headpos.y = headpos.y + 1
  81.     minetest.sound_play("hunger_eating", {pos = headpos, gain = 1.0, max_hear_distance = 32})
  82.     hunger.update_bar(player, full)
  83.     player:set_attribute("hunger", full)
  84. end)
  85.  
  86. -- Main function
  87. local timer = 0
  88. minetest.register_globalstep(function(dtime)
  89.     timer = timer + dtime;
  90.     if timer < base_interval then return end
  91.     timer = 0
  92.     for _,player in ipairs(minetest.get_connected_players()) do
  93.         local name = player:get_player_name()
  94.         local hp = player:get_hp()
  95.         local full = player:get_attribute("hunger")
  96.         if minetest.get_player_privs(name)["no_hunger"] then
  97.             if player_bar[name] then
  98.                 player:hud_remove(player_bar[name])
  99.             end
  100.             return
  101.         end
  102.         if not player_is_active[name] then return end
  103.         -- The hunger interval for each player depends on the health
  104.         if not player_step[name] or player_step[name] >= 20 then
  105.             player_step[name] = 0
  106.         end
  107.         player_step[name] = player_step[name] + 1
  108.         if player_step[name] < hp then return end
  109.         player_step[name] = 0
  110.         if not full then full = 20 end
  111.         full = full - 1
  112.         if full <= 0 then
  113.             player:set_hp(hp - 1)
  114.             full = 20
  115.             local pos_sound  = player:getpos()
  116.             minetest.chat_send_player(name, "You are hungry.")
  117.         end
  118.         player_is_active[name] = false
  119.         hunger.update_bar(player, full)
  120.         player:set_attribute("hunger", full)
  121.     end
  122. end)
  123.  
  124. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top