init.lua 9.2 KB

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