init.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. hb4 = hb4 or {}
  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. local hp = player:get_hp()
  42. if hp > (data.hp_min or 0) then
  43. local new_hp = hp - damage
  44. new_hp = math_max(new_hp, (data.hp_min or 0))
  45. player:set_hp(new_hp)
  46. end
  47. -- Message is printed only if player died. Return if we killed them.
  48. if player:get_hp() <= 0 then
  49. if data.poison then
  50. hud.change_item(player, "hunger", {text="hud_hunger_fg.png"})
  51. end
  52. -- Execute termination callback.
  53. if data.done then
  54. data.done()
  55. end
  56. if data.msg then
  57. minetest.chat_send_all(data.msg)
  58. end
  59. return
  60. end
  61. data.step = data.step - 1
  62. local time = (data.time or 1)
  63. minetest.after(time, hb4.delayed_harm2, data)
  64. else
  65. -- Player logged off. Wait until they come back.
  66. -- Player cannot escape harm!
  67. minetest.after(10, hb4.delayed_harm2, data)
  68. end
  69. end
  70. function hb4.delayed_harm(data)
  71. minetest.after(0, hb4.delayed_harm2, data)
  72. end
  73. -- Return reference to nearest player, or nil.
  74. function hb4.nearest_player(pos)
  75. local players = minetest.get_connected_players()
  76. local pref
  77. local dist = 100000
  78. for i=1, #players, 1 do
  79. local p = players[i]
  80. local d = vector.distance(p:get_pos(), pos)
  81. if d < dist then
  82. dist = d
  83. pref = p
  84. end
  85. end
  86. return pref
  87. end
  88. -- Return reference to nearest player, or nil.
  89. function hb4.nearest_player_not(pos, pnot)
  90. local players = minetest.get_connected_players()
  91. local admin = minetest.get_player_by_name(gdac.name_of_admin)
  92. local pref
  93. local dist = 100000
  94. for i=1, #players, 1 do
  95. local p = players[i]
  96. if p ~= pnot and p ~= admin then
  97. local d = vector.distance(p:get_pos(), pos)
  98. if d < dist then
  99. dist = d
  100. pref = p
  101. end
  102. end
  103. end
  104. return pref
  105. end
  106. if not hb4.reload_registered then
  107. local c = "hb4:core"
  108. local f = hb4.modpath .. "/init.lua"
  109. reload.register_file(c, f, false)
  110. hb4.reload_registered = true
  111. end