init.lua 8.1 KB

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