2
0

init.lua 8.4 KB

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