init.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. if not minetest.global_exists("hb4") then hb4 = {} end
  2. hb4.modpath = minetest.get_modpath("hb4")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. local math_max = math.max
  6. dofile(hb4.modpath .. "/leafscatter.lua")
  7. dofile(hb4.modpath .. "/fruitregrow.lua")
  8. dofile(hb4.modpath .. "/floodfill.lua")
  9. dofile(hb4.modpath .. "/breath.lua")
  10. dofile(hb4.modpath .. "/notify.lua")
  11. dofile(hb4.modpath .. "/mailall.lua")
  12. dofile(hb4.modpath .. "/spawn_sanitizer.lua")
  13. dofile(hb4.modpath .. "/nodeinspector.lua")
  14. dofile(hb4.modpath .. "/diving_equipment.lua")
  15. dofile(hb4.modpath .. "/find_ground.lua")
  16. -- Server restart countdown not active in singleplayer.
  17. if not minetest.is_singleplayer() then
  18. dofile(hb4.modpath .. "/countdown.lua")
  19. end
  20. --[[
  21. {name="player", step=2, min=1, max=3, msg="He died!", poison=false}
  22. --]]
  23. function hb4.delayed_harm2(data)
  24. local player = minetest.get_player_by_name(data.name)
  25. if player then
  26. -- finished harming?
  27. if data.step < 1 then
  28. if data.poison then
  29. hud.change_item(player, "hunger", {text="hud_hunger_fg.png"})
  30. end
  31. -- Execute termination callback.
  32. if data.done then
  33. data.done()
  34. end
  35. return
  36. end
  37. if data.poison then
  38. hud.change_item(player, "hunger", {text="hunger_statbar_poisen.png"})
  39. end
  40. local damage = math_random(data.min, data.max)
  41. --minetest.log('damage2: ' .. damage)
  42. local hp = player:get_hp()
  43. if hp > (data.hp_min or 0) then
  44. local new_hp = hp - damage
  45. new_hp = math_max(new_hp, (data.hp_min or 0))
  46. -- Ensure damage is not greater than would cause player to go under 'hp_min'.
  47. local hpdiff = (hp - new_hp)
  48. --minetest.log('damage: ' .. hpdiff)
  49. local dtype = "fleshy"
  50. if data.poison then
  51. dtype = "poison"
  52. end
  53. utility.damage_player(player, dtype, hpdiff)
  54. end
  55. -- Message is printed only if player died. Return if we killed them.
  56. if player:get_hp() <= 0 then
  57. if data.poison then
  58. hud.change_item(player, "hunger", {text="hud_hunger_fg.png"})
  59. end
  60. -- Execute termination callback.
  61. if data.done then
  62. data.done()
  63. end
  64. if data.msg then
  65. minetest.chat_send_all(data.msg)
  66. end
  67. return
  68. end
  69. data.step = data.step - 1
  70. local time = (data.time or 1)
  71. minetest.after(time, hb4.delayed_harm2, data)
  72. else
  73. -- Player logged off. Wait until they come back.
  74. -- Player cannot escape harm!
  75. minetest.after(10, hb4.delayed_harm2, data)
  76. end
  77. end
  78. function hb4.delayed_harm(data)
  79. minetest.after(0, hb4.delayed_harm2, data)
  80. end
  81. -- Return reference to nearest player, or nil.
  82. function hb4.nearest_player(pos)
  83. local players = minetest.get_connected_players()
  84. local admin = minetest.get_player_by_name(gdac.name_of_admin)
  85. local pref
  86. local dist = 100000
  87. for i=1, #players, 1 do
  88. local p = players[i]
  89. if p ~= admin then
  90. local d = vector.distance(p:get_pos(), pos)
  91. if d < dist then
  92. dist = d
  93. pref = p
  94. end
  95. end
  96. end
  97. return pref
  98. end
  99. -- Return reference to nearest player, or nil.
  100. function hb4.nearest_player_not(pos, pnot)
  101. local players = minetest.get_connected_players()
  102. local admin = minetest.get_player_by_name(gdac.name_of_admin)
  103. local pref
  104. local dist = 100000
  105. for i=1, #players, 1 do
  106. local p = players[i]
  107. if p ~= pnot and p ~= admin then
  108. local d = vector.distance(p:get_pos(), pos)
  109. if d < dist then
  110. dist = d
  111. pref = p
  112. end
  113. end
  114. end
  115. return pref
  116. end
  117. if not hb4.reload_registered then
  118. local c = "hb4:core"
  119. local f = hb4.modpath .. "/init.lua"
  120. reload.register_file(c, f, false)
  121. hb4.reload_registered = true
  122. end