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.

246 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. on_rotate = pipeworks.on_rotate
  138. }
  139. override = {
  140. tiles = {
  141. "default_chest_top.png"..tube_entry,
  142. "default_chest_top.png"..tube_entry,
  143. "default_chest_side.png"..tube_entry,
  144. "default_chest_side.png"..tube_entry,
  145. "default_chest_front.png",
  146. "default_chest_inside.png"
  147. },
  148. on_rightclick = function(pos, node, clicker)
  149. minetest.sound_play(old_chest_def.sound_open, {gain = 0.3, pos = pos,
  150. max_hear_distance = 10})
  151. if not chest_lid_obstructed(pos) then
  152. minetest.swap_node(pos, {
  153. name = "default:" .. "chest" .. "_open",
  154. param2 = node.param2 })
  155. end
  156. minetest.after(0.2, minetest.show_formspec,
  157. clicker:get_player_name(),
  158. "pipeworks:chest_formspec", get_chest_formspec(pos))
  159. open_chests[clicker:get_player_name()] = { pos = pos,
  160. sound = old_chest_def.sound_close, swap = "chest" }
  161. end,
  162. groups = table.copy(old_chest_def.groups),
  163. tube = {
  164. insert_object = function(pos, node, stack, direction)
  165. local meta = minetest.get_meta(pos)
  166. local inv = meta:get_inventory()
  167. return inv:add_item("main", stack)
  168. end,
  169. can_insert = function(pos, node, stack, direction)
  170. local meta = minetest.get_meta(pos)
  171. local inv = meta:get_inventory()
  172. if meta:get_int("splitstacks") == 1 then
  173. stack = stack:peek_item(1)
  174. end
  175. return inv:room_for_item("main", stack)
  176. end,
  177. input_inventory = "main",
  178. connect_sides = {left = 1, right = 1, back = 1, bottom = 1, top = 1}
  179. },
  180. after_place_node = pipeworks.after_place,
  181. after_dig_node = pipeworks.after_dig,
  182. on_rotate = pipeworks.on_rotate
  183. }
  184. --[[local override_common = {
  185. }
  186. for k,v in pairs(override_common) do
  187. override_protected[k] = v
  188. override[k] = v
  189. end]]
  190. override_open = table.copy(override)
  191. override_open.groups = table.copy(old_chest_open_def.groups)
  192. override_open.tube = table.copy(override.tube)
  193. override_open.tube.connect_sides = table.copy(override.tube.connect_sides)
  194. override_open.tube.connect_sides.top = nil
  195. override_protected_open = table.copy(override_protected)
  196. override_protected_open.groups = table.copy(old_chest_locked_open_def.groups)
  197. override_protected_open.tube = table.copy(override_protected.tube)
  198. override_protected_open.tube.connect_sides = table.copy(override_protected.tube.connect_sides)
  199. override_protected_open.tube.connect_sides.top = nil
  200. override_protected.tiles = { -- Rearranged according to the chest registration in Minetest_Game.
  201. "default_chest_top.png"..tube_entry,
  202. "default_chest_top.png"..tube_entry,
  203. "default_chest_side.png"..tube_entry.."^[transformFX",
  204. "default_chest_side.png"..tube_entry,
  205. "default_chest_side.png"..tube_entry,
  206. "default_chest_lock.png",
  207. }
  208. override.tiles = {
  209. "default_chest_top.png"..tube_entry,
  210. "default_chest_top.png"..tube_entry,
  211. "default_chest_side.png"..tube_entry.."^[transformFX",
  212. "default_chest_side.png"..tube_entry,
  213. "default_chest_side.png"..tube_entry,
  214. "default_chest_front.png",
  215. }
  216. -- Add the extra groups
  217. for i,v in ipairs({override_protected, override, override_open, override_protected_open}) do
  218. v.groups.tubedevice = 1
  219. v.groups.tubedevice_receiver = 1
  220. end
  221. -- Override with the new modifications.
  222. minetest.override_item("default:chest", override)
  223. minetest.override_item("default:chest_open", override_open)
  224. minetest.override_item("default:chest_locked", override_protected)
  225. minetest.override_item("default:chest_locked_open", override_protected_open)