Pipeworks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
8.0KB

  1. -- this bit of code modifies the default chests and furnaces to be compatible
  2. -- with pipeworks.
  3. --
  4. -- the formspecs found here are basically copies of the ones from minetest_game
  5. -- plus bits from pipeworks' sorting tubes
  6. -- Pipeworks Specific
  7. local fs_helpers = pipeworks.fs_helpers
  8. local tube_entry = "^pipeworks_tube_connection_wooden.png"
  9. -- Chest Locals
  10. local open_chests = {}
  11. local function get_chest_formspec(pos)
  12. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  13. local formspec =
  14. "size[8,9]" ..
  15. default.gui_bg ..
  16. default.gui_bg_img ..
  17. default.gui_slots ..
  18. "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
  19. "list[current_player;main;0,4.85;8,1;]" ..
  20. "list[current_player;main;0,6.08;8,3;8]" ..
  21. "listring[nodemeta:" .. spos .. ";main]" ..
  22. "listring[current_player;main]" ..
  23. default.get_hotbar_bg(0,4.85)
  24. -- Pipeworks Switch
  25. formspec = formspec ..
  26. fs_helpers.cycling_button(
  27. minetest.get_meta(pos),
  28. pipeworks.button_base,
  29. "splitstacks",
  30. {
  31. pipeworks.button_off,
  32. pipeworks.button_on
  33. }
  34. )..pipeworks.button_label
  35. return formspec
  36. end
  37. local function chest_lid_obstructed(pos)
  38. local above = { x = pos.x, y = pos.y + 1, z = pos.z }
  39. local def = minetest.registered_nodes[minetest.get_node(above).name]
  40. -- allow ladders, signs, wallmounted things and torches to not obstruct
  41. if not def then return true end
  42. if def.drawtype == "airlike" or
  43. def.drawtype == "signlike" or
  44. def.drawtype == "torchlike" or
  45. (def.drawtype == "nodebox" and def.paramtype2 == "wallmounted") then
  46. return false
  47. end
  48. return true
  49. end
  50. minetest.register_on_player_receive_fields(function(player, formname, fields)
  51. if formname == "pipeworks:chest_formspec" and player then
  52. local pn = player:get_player_name()
  53. if open_chests[pn] then
  54. local pos = open_chests[pn].pos
  55. if fields.quit then
  56. local sound = open_chests[pn].sound
  57. local swap = open_chests[pn].swap
  58. local node = minetest.get_node(pos)
  59. open_chests[pn] = nil
  60. for k, v in pairs(open_chests) do
  61. if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
  62. return true
  63. end
  64. end
  65. minetest.after(0.2, function()
  66. minetest.swap_node(pos, { name = "default:" .. swap, param2 = node.param2 })
  67. -- Pipeworks notification
  68. pipeworks.after_place(pos)
  69. end)
  70. minetest.sound_play(sound, {gain = 0.3, pos = pos, max_hear_distance = 10})
  71. end
  72. -- Pipeworks Switch
  73. if pipeworks.may_configure(pos, player) and not fields.quit then
  74. fs_helpers.on_receive_fields(pos, fields)
  75. minetest.show_formspec(player:get_player_name(), "pipeworks:chest_formspec", get_chest_formspec(pos))
  76. end
  77. return true
  78. end
  79. end
  80. end)
  81. -- Original Definitions
  82. local old_chest_def = table.copy(minetest.registered_items["default:chest"])
  83. local old_chest_open_def = table.copy(minetest.registered_items["default:chest_open"])
  84. local old_chest_locked_def = table.copy(minetest.registered_items["default:chest_locked"])
  85. local old_chest_locked_open_def = table.copy(minetest.registered_items["default:chest_locked_open"])
  86. -- Override Construction
  87. local override_protected, override, override_open, override_protected_open
  88. override_protected = {
  89. tiles = {
  90. "default_chest_top.png"..tube_entry,
  91. "default_chest_top.png"..tube_entry,
  92. "default_chest_side.png"..tube_entry,
  93. "default_chest_side.png"..tube_entry,
  94. "default_chest_lock.png",
  95. "default_chest_inside.png"
  96. },
  97. after_place_node = function(pos, placer)
  98. old_chest_locked_def.after_place_node(pos, placer)
  99. pipeworks.after_place(pos)
  100. end,
  101. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  102. if not default.can_interact_with_node(clicker, pos) then
  103. return itemstack
  104. end
  105. minetest.sound_play(old_chest_locked_def.sound_open, {gain = 0.3,
  106. pos = pos, max_hear_distance = 10})
  107. if not chest_lid_obstructed(pos) then
  108. minetest.swap_node(pos,
  109. { name = "default:" .. "chest_locked" .. "_open",
  110. param2 = node.param2 })
  111. end
  112. minetest.after(0.2, minetest.show_formspec,
  113. clicker:get_player_name(),
  114. "pipeworks:chest_formspec", get_chest_formspec(pos))
  115. open_chests[clicker:get_player_name()] = { pos = pos,
  116. sound = old_chest_locked_def.sound_close, swap = "chest_locked" }
  117. end,
  118. groups = table.copy(old_chest_locked_def.groups),
  119. tube = {
  120. insert_object = function(pos, node, stack, direction)
  121. local meta = minetest.get_meta(pos)
  122. local inv = meta:get_inventory()
  123. return inv:add_item("main", stack)
  124. end,
  125. can_insert = function(pos, node, stack, direction)
  126. local meta = minetest.get_meta(pos)
  127. local inv = meta:get_inventory()
  128. if meta:get_int("splitstacks") == 1 then
  129. stack = stack:peek_item(1)
  130. end
  131. return inv:room_for_item("main", stack)
  132. end,
  133. input_inventory = "main",
  134. connect_sides = {left = 1, right = 1, back = 1, bottom = 1, top = 1}
  135. },
  136. after_dig_node = pipeworks.after_dig
  137. }
  138. override = {
  139. tiles = {
  140. "default_chest_top.png"..tube_entry,
  141. "default_chest_top.png"..tube_entry,
  142. "default_chest_side.png"..tube_entry,
  143. "default_chest_side.png"..tube_entry,
  144. "default_chest_front.png",
  145. "default_chest_inside.png"
  146. },
  147. on_rightclick = function(pos, node, clicker)
  148. minetest.sound_play(old_chest_def.sound_open, {gain = 0.3, pos = pos,
  149. max_hear_distance = 10})
  150. if not chest_lid_obstructed(pos) then
  151. minetest.swap_node(pos, {
  152. name = "default:" .. "chest" .. "_open",
  153. param2 = node.param2 })
  154. end
  155. minetest.after(0.2, minetest.show_formspec,
  156. clicker:get_player_name(),
  157. "pipeworks:chest_formspec", get_chest_formspec(pos))
  158. open_chests[clicker:get_player_name()] = { pos = pos,
  159. sound = old_chest_def.sound_close, swap = "chest" }
  160. end,
  161. groups = table.copy(old_chest_def.groups),
  162. tube = {
  163. insert_object = function(pos, node, stack, direction)
  164. local meta = minetest.get_meta(pos)
  165. local inv = meta:get_inventory()
  166. return inv:add_item("main", stack)
  167. end,
  168. can_insert = function(pos, node, stack, direction)
  169. local meta = minetest.get_meta(pos)
  170. local inv = meta:get_inventory()
  171. if meta:get_int("splitstacks") == 1 then
  172. stack = stack:peek_item(1)
  173. end
  174. return inv:room_for_item("main", stack)
  175. end,
  176. input_inventory = "main",
  177. connect_sides = {left = 1, right = 1, back = 1, bottom = 1, top = 1}
  178. },
  179. after_place_node = pipeworks.after_place,
  180. after_dig_node = pipeworks.after_dig
  181. }
  182. --[[local override_common = {
  183. }
  184. for k,v in pairs(override_common) do
  185. override_protected[k] = v
  186. override[k] = v
  187. end]]
  188. override_open = table.copy(override)
  189. override_open.groups = table.copy(old_chest_open_def.groups)
  190. override_open.tube = table.copy(override.tube)
  191. override_open.tube.connect_sides = table.copy(override.tube.connect_sides)
  192. override_open.tube.connect_sides.top = nil
  193. override_protected_open = table.copy(override_protected)
  194. override_protected_open.groups = table.copy(old_chest_locked_open_def.groups)
  195. override_protected_open.tube = table.copy(override_protected.tube)
  196. override_protected_open.tube.connect_sides = table.copy(override_protected.tube.connect_sides)
  197. override_protected_open.tube.connect_sides.top = nil
  198. override_protected.tiles = { -- Rearranged according to the chest registration in Minetest_Game.
  199. "default_chest_top.png"..tube_entry,
  200. "default_chest_top.png"..tube_entry,
  201. "default_chest_side.png"..tube_entry.."^[transformFX",
  202. "default_chest_side.png"..tube_entry,
  203. "default_chest_side.png"..tube_entry,
  204. "default_chest_lock.png",
  205. }
  206. override.tiles = {
  207. "default_chest_top.png"..tube_entry,
  208. "default_chest_top.png"..tube_entry,
  209. "default_chest_side.png"..tube_entry.."^[transformFX",
  210. "default_chest_side.png"..tube_entry,
  211. "default_chest_side.png"..tube_entry,
  212. "default_chest_front.png",
  213. }
  214. -- Add the extra groups
  215. for i,v in ipairs({override_protected, override, override_open, override_protected_open}) do
  216. v.groups.tubedevice = 1
  217. v.groups.tubedevice_receiver = 1
  218. end
  219. -- Override with the new modifications.
  220. minetest.override_item("default:chest", override)
  221. minetest.override_item("default:chest_open", override_open)
  222. minetest.override_item("default:chest_locked", override_protected)
  223. minetest.override_item("default:chest_locked_open", override_protected_open)