init.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. -- bones/init.lua
  2. -- Minetest 0.4 mod: bones
  3. -- See README.txt for licensing and other information.
  4. -- Load support for MT game translation.
  5. local S = minetest.get_translator("bones")
  6. bones = {}
  7. local function is_owner(pos, name)
  8. local owner = minetest.get_meta(pos):get_string("owner")
  9. if owner == "" or owner == name or minetest.check_player_privs(name, "protection_bypass") then
  10. return true
  11. end
  12. return false
  13. end
  14. local bones_formspec =
  15. "size[8,9]" ..
  16. "list[current_name;main;0,0.3;8,4;]" ..
  17. "list[current_player;main;0,4.85;8,1;]" ..
  18. "list[current_player;main;0,6.08;8,3;8]" ..
  19. "listring[current_name;main]" ..
  20. "listring[current_player;main]" ..
  21. default.get_hotbar_bg(0,4.85)
  22. local share_bones_time = tonumber(minetest.settings:get("share_bones_time")) or 1200
  23. local share_bones_time_early = tonumber(minetest.settings:get("share_bones_time_early")) or share_bones_time / 4
  24. minetest.register_node("bones:bones", {
  25. description = S("Bones"),
  26. tiles = {
  27. "bones_top.png^[transform2",
  28. "bones_bottom.png",
  29. "bones_side.png",
  30. "bones_side.png",
  31. "bones_rear.png",
  32. "bones_front.png"
  33. },
  34. paramtype2 = "facedir",
  35. groups = {dig_immediate = 2},
  36. sounds = default.node_sound_gravel_defaults(),
  37. can_dig = function(pos, player)
  38. local inv = minetest.get_meta(pos):get_inventory()
  39. local name = ""
  40. if player then
  41. name = player:get_player_name()
  42. end
  43. return is_owner(pos, name) and inv:is_empty("main")
  44. end,
  45. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  46. if is_owner(pos, player:get_player_name()) then
  47. return count
  48. end
  49. return 0
  50. end,
  51. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  52. return 0
  53. end,
  54. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  55. if is_owner(pos, player:get_player_name()) then
  56. return stack:get_count()
  57. end
  58. return 0
  59. end,
  60. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  61. local meta = minetest.get_meta(pos)
  62. if meta:get_inventory():is_empty("main") then
  63. local inv = player:get_inventory()
  64. if inv:room_for_item("main", {name = "bones:bones"}) then
  65. inv:add_item("main", {name = "bones:bones"})
  66. else
  67. minetest.add_item(pos, "bones:bones")
  68. end
  69. minetest.remove_node(pos)
  70. end
  71. end,
  72. on_punch = function(pos, node, player)
  73. if not is_owner(pos, player:get_player_name()) then
  74. return
  75. end
  76. if minetest.get_meta(pos):get_string("infotext") == "" then
  77. return
  78. end
  79. local inv = minetest.get_meta(pos):get_inventory()
  80. local player_inv = player:get_inventory()
  81. local has_space = true
  82. for i = 1, inv:get_size("main") do
  83. local stk = inv:get_stack("main", i)
  84. if player_inv:room_for_item("main", stk) then
  85. inv:set_stack("main", i, nil)
  86. player_inv:add_item("main", stk)
  87. else
  88. has_space = false
  89. break
  90. end
  91. end
  92. -- remove bones if player emptied them
  93. if has_space then
  94. if player_inv:room_for_item("main", {name = "bones:bones"}) then
  95. player_inv:add_item("main", {name = "bones:bones"})
  96. else
  97. minetest.add_item(pos,"bones:bones")
  98. end
  99. minetest.remove_node(pos)
  100. end
  101. end,
  102. on_timer = function(pos, elapsed)
  103. local meta = minetest.get_meta(pos)
  104. local time = meta:get_int("time") + elapsed
  105. if time >= share_bones_time then
  106. meta:set_string("infotext", S("@1's old bones", meta:get_string("owner")))
  107. meta:set_string("owner", "")
  108. else
  109. meta:set_int("time", time)
  110. return true
  111. end
  112. end,
  113. on_blast = function(pos)
  114. end,
  115. })
  116. local function may_replace(pos, player)
  117. local node_name = minetest.get_node(pos).name
  118. local node_definition = minetest.registered_nodes[node_name]
  119. -- if the node is unknown, we return false
  120. if not node_definition then
  121. return false
  122. end
  123. -- allow replacing air and liquids
  124. if node_name == "air" or node_definition.liquidtype ~= "none" then
  125. return true
  126. end
  127. -- don't replace filled chests and other nodes that don't allow it
  128. local can_dig_func = node_definition.can_dig
  129. if can_dig_func and not can_dig_func(pos, player) then
  130. return false
  131. end
  132. -- default to each nodes buildable_to; if a placed block would replace it, why shouldn't bones?
  133. -- flowers being squished by bones are more realistical than a squished stone, too
  134. -- exception are of course any protected buildable_to
  135. return node_definition.buildable_to and not minetest.is_protected(pos, player:get_player_name())
  136. end
  137. local drop = function(pos, itemstack)
  138. local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count()))
  139. if obj then
  140. obj:set_velocity({
  141. x = math.random(-10, 10) / 9,
  142. y = 5,
  143. z = math.random(-10, 10) / 9,
  144. })
  145. end
  146. end
  147. local player_inventory_lists = { "main", "craft" }
  148. bones.player_inventory_lists = player_inventory_lists
  149. local function is_all_empty(player_inv)
  150. for _, list_name in ipairs(player_inventory_lists) do
  151. if not player_inv:is_empty(list_name) then
  152. return false
  153. end
  154. end
  155. return true
  156. end
  157. minetest.register_on_dieplayer(function(player)
  158. local bones_mode = minetest.settings:get("bones_mode") or "bones"
  159. if bones_mode ~= "bones" and bones_mode ~= "drop" and bones_mode ~= "keep" then
  160. bones_mode = "bones"
  161. end
  162. local bones_position_message = true
  163. local player_name = player:get_player_name()
  164. local pos = vector.round(player:get_pos())
  165. local pos_string = minetest.pos_to_string(pos)
  166. minetest.add_entity(pos, 'zombies:normal')
  167. -- return if keep inventory set or in creative mode
  168. if bones_mode == "keep" or (creative and creative.is_enabled_for
  169. and creative.is_enabled_for(player:get_player_name())) then
  170. minetest.log("action", player_name .. " dies at " .. pos_string ..
  171. ". No bones placed")
  172. if bones_position_message then
  173. minetest.chat_send_player(player_name, S("@1 died at @2.", player_name, pos_string))
  174. end
  175. return
  176. end
  177. local player_inv = player:get_inventory()
  178. if is_all_empty(player_inv) then
  179. minetest.log("action", player_name .. " dies at " .. pos_string ..
  180. ". No bones placed")
  181. if bones_position_message then
  182. minetest.chat_send_player(player_name, S("@1 died at @2.", player_name, pos_string))
  183. end
  184. return
  185. end
  186. -- check if it's possible to place bones, if not find space near player
  187. if bones_mode == "bones" and not may_replace(pos, player) then
  188. local air = minetest.find_node_near(pos, 1, {"air"})
  189. if air and not minetest.is_protected(air, player_name) then
  190. pos = air
  191. else
  192. bones_mode = "drop"
  193. end
  194. end
  195. if bones_mode == "drop" then
  196. for _, list_name in ipairs(player_inventory_lists) do
  197. for i = 1, player_inv:get_size(list_name) do
  198. drop(pos, player_inv:get_stack(list_name, i))
  199. end
  200. player_inv:set_list(list_name, {})
  201. end
  202. drop(pos, ItemStack("bones:bones"))
  203. minetest.log("action", player_name .. " dies at " .. pos_string ..
  204. ". Inventory dropped")
  205. if bones_position_message then
  206. minetest.chat_send_player(player_name, S("@1 died at @2, and dropped their inventory.", player_name, pos_string))
  207. end
  208. return
  209. end
  210. local param2 = minetest.dir_to_facedir(player:get_look_dir())
  211. minetest.set_node(pos, {name = "bones:bones", param2 = param2})
  212. minetest.log("action", player_name .. " dies at " .. pos_string ..
  213. ". Bones placed")
  214. if bones_position_message then
  215. minetest.chat_send_player(player_name, S("@1 died at @2, and bones were placed.", player_name, pos_string))
  216. end
  217. local meta = minetest.get_meta(pos)
  218. local inv = meta:get_inventory()
  219. inv:set_size("main", 8 * 4)
  220. for _, list_name in ipairs(player_inventory_lists) do
  221. for i = 1, player_inv:get_size(list_name) do
  222. local stack = player_inv:get_stack(list_name, i)
  223. if inv:room_for_item("main", stack) then
  224. inv:add_item("main", stack)
  225. else -- no space left
  226. drop(pos, stack)
  227. end
  228. end
  229. player_inv:set_list(list_name, {})
  230. end
  231. meta:set_string("formspec", bones_formspec)
  232. meta:set_string("owner", player_name)
  233. if share_bones_time ~= 0 then
  234. meta:set_string("infotext", S("@1's fresh bones", player_name))
  235. if share_bones_time_early == 0 or not minetest.is_protected(pos, player_name) then
  236. meta:set_int("time", 0)
  237. else
  238. meta:set_int("time", (share_bones_time - share_bones_time_early))
  239. end
  240. minetest.get_node_timer(pos):start(10)
  241. else
  242. meta:set_string("infotext", S("@1's bones", player_name))
  243. end
  244. end)