init.lua 8.0 KB

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