2
0

init.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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
  124. if node_name == "air" then
  125. return true
  126. end
  127. -- don't replace nodes inside protections
  128. if minetest.is_protected(pos, player:get_player_name()) then
  129. return false
  130. end
  131. -- allow replacing liquids
  132. if node_definition.liquidtype ~= "none" then
  133. return true
  134. end
  135. -- don't replace filled chests and other nodes that don't allow it
  136. local can_dig_func = node_definition.can_dig
  137. if can_dig_func and not can_dig_func(pos, player) then
  138. return false
  139. end
  140. -- default to each nodes buildable_to; if a placed block would replace it, why shouldn't bones?
  141. -- flowers being squished by bones are more realistical than a squished stone, too
  142. return node_definition.buildable_to
  143. end
  144. local drop = function(pos, itemstack)
  145. local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count()))
  146. if obj then
  147. obj:set_velocity({
  148. x = math.random(-10, 10) / 9,
  149. y = 5,
  150. z = math.random(-10, 10) / 9,
  151. })
  152. end
  153. end
  154. local player_inventory_lists = { "main", "craft" }
  155. bones.player_inventory_lists = player_inventory_lists
  156. local function is_all_empty(player_inv)
  157. for _, list_name in ipairs(player_inventory_lists) do
  158. if not player_inv:is_empty(list_name) then
  159. return false
  160. end
  161. end
  162. return true
  163. end
  164. minetest.register_on_dieplayer(function(player)
  165. local bones_mode = minetest.settings:get("bones_mode") or "bones"
  166. if bones_mode ~= "bones" and bones_mode ~= "drop" and bones_mode ~= "keep" then
  167. bones_mode = "bones"
  168. end
  169. local bones_position_message = minetest.settings:get_bool("bones_position_message") == true
  170. local player_name = player:get_player_name()
  171. local pos = vector.round(player:get_pos())
  172. local pos_string = minetest.pos_to_string(pos)
  173. -- return if keep inventory set or in creative mode
  174. if bones_mode == "keep" or minetest.is_creative_enabled(player_name) 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, S("@1 died at @2.", player_name, pos_string))
  179. end
  180. return
  181. end
  182. local player_inv = player:get_inventory()
  183. if is_all_empty(player_inv) then
  184. minetest.log("action", player_name .. " dies at " .. pos_string ..
  185. ". No bones placed")
  186. if bones_position_message then
  187. minetest.chat_send_player(player_name, S("@1 died at @2.", player_name, pos_string))
  188. end
  189. return
  190. end
  191. -- check if it's possible to place bones, if not find space near player
  192. if bones_mode == "bones" and not may_replace(pos, player) then
  193. local air = minetest.find_node_near(pos, 1, {"air"})
  194. if air and not minetest.is_protected(air, player_name) then
  195. pos = air
  196. else
  197. bones_mode = "drop"
  198. end
  199. end
  200. if bones_mode == "drop" then
  201. for _, list_name in ipairs(player_inventory_lists) do
  202. for i = 1, player_inv:get_size(list_name) do
  203. drop(pos, player_inv:get_stack(list_name, i))
  204. end
  205. player_inv:set_list(list_name, {})
  206. end
  207. drop(pos, ItemStack("bones:bones"))
  208. minetest.log("action", player_name .. " dies at " .. pos_string ..
  209. ". Inventory dropped")
  210. if bones_position_message then
  211. minetest.chat_send_player(player_name, S("@1 died at @2, and dropped their inventory.", player_name, pos_string))
  212. end
  213. return
  214. end
  215. local param2 = minetest.dir_to_facedir(player:get_look_dir())
  216. minetest.set_node(pos, {name = "bones:bones", param2 = param2})
  217. minetest.log("action", player_name .. " dies at " .. pos_string ..
  218. ". Bones placed")
  219. if bones_position_message then
  220. minetest.chat_send_player(player_name, S("@1 died at @2, and bones were placed.", player_name, pos_string))
  221. end
  222. local meta = minetest.get_meta(pos)
  223. local inv = meta:get_inventory()
  224. inv:set_size("main", 8 * 4)
  225. for _, list_name in ipairs(player_inventory_lists) do
  226. for i = 1, player_inv:get_size(list_name) do
  227. local stack = player_inv:get_stack(list_name, i)
  228. if inv:room_for_item("main", stack) then
  229. inv:add_item("main", stack)
  230. else -- no space left
  231. drop(pos, stack)
  232. end
  233. end
  234. player_inv:set_list(list_name, {})
  235. end
  236. meta:set_string("formspec", bones_formspec)
  237. meta:set_string("owner", player_name)
  238. if share_bones_time ~= 0 then
  239. meta:set_string("infotext", S("@1's fresh bones", player_name))
  240. if share_bones_time_early == 0 or not minetest.is_protected(pos, player_name) then
  241. meta:set_int("time", 0)
  242. else
  243. meta:set_int("time", (share_bones_time - share_bones_time_early))
  244. end
  245. minetest.get_node_timer(pos):start(10)
  246. else
  247. meta:set_string("infotext", S("@1's bones", player_name))
  248. end
  249. end)