tubelib.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. tubelib.register_node('asrs:controller', {}, {
  2. on_pull_item = function(pos, side)
  3. local meta = minetest.get_meta(pos)
  4. return tubelib.get_item(meta, 'storage')
  5. end,
  6. on_push_item = function(pos, side, item)
  7. local meta = minetest.get_meta(pos)
  8. return tubelib.put_item(meta, 'storage', item)
  9. end,
  10. on_unpull_item = function(pos, side, item)
  11. local meta = minetest.get_meta(pos)
  12. return tubelib.put_item(meta, 'storage', item)
  13. end,
  14. valid_sides = {'B', 'D'}
  15. })
  16. minetest.register_node('asrs:tubelib', {
  17. description = 'A.S.R.S tubelib interface',
  18. tiles = {'asrs_cell.png^tubelib_hole.png'},
  19. groups = {cracky=2, choppy=2, oddly_breakable_by_hand=2},
  20. after_place_node = function(pos, placer)
  21. local neighbor, pos1 = asrs.connected_nodes(pos, 'asrs:lift, asrs:connection_point')
  22. if neighbor then
  23. local this_meta = minetest.get_meta(pos)
  24. local that_meta = minetest.get_meta(pos1)
  25. local children = that_meta:get_int('children')
  26. local sys_id = that_meta:get_string('system_id')
  27. this_meta:set_string('system_id', sys_id)
  28. this_meta:set_int('children', 0)
  29. that_meta:set_int('children', children + 1)
  30. local sys_inv_max = asrs.data[sys_id].max_inv
  31. asrs.data[sys_id].max_inv = sys_inv_max + 10
  32. local connected_nodes = asrs.data[sys_id].nodes
  33. asrs.data[sys_id].nodes = connected_nodes + 1
  34. asrs.save()
  35. else
  36. local name = placer:get_player_name() or ''
  37. minetest.chat_send_player(name, 'You must place this adjacent to a lift or controller node.')
  38. minetest.remove_node(pos)
  39. return true
  40. end
  41. end,
  42. can_dig = function(pos, player)
  43. local meta = minetest.get_meta(pos)
  44. local sys_id = meta:get_string('system_id')
  45. local sys_inv_max = asrs.data[sys_id].max_inv
  46. asrs.sort_inventory(asrs.data[sys_id].inv_pos)
  47. local inv_count = asrs.count_inventory(asrs.data[sys_id].inv_pos)
  48. if inv_count > (sys_inv_max - 10) then
  49. minetest.chat_send_player(player:get_player_name(), 'Remove some inventory from the system first.')
  50. return false
  51. else
  52. return true
  53. end
  54. end,
  55. after_dig_node = function(pos, _, oldmetadata)
  56. local _, pos1 = asrs.connected_nodes(pos, 'asrs:lift')
  57. if pos1 then
  58. local that_meta = minetest.get_meta(pos1)
  59. local children = that_meta:get_int('children')
  60. that_meta:set_int('children', children - 1)
  61. end
  62. local sys_id = oldmetadata.fields.system_id
  63. local sys_inv_max = asrs.data[sys_id].max_inv
  64. asrs.data[sys_id].max_inv = sys_inv_max - 10
  65. local connected_nodes = asrs.data[sys_id].nodes
  66. asrs.data[sys_id].nodes = connected_nodes - 1
  67. asrs.save()
  68. end,
  69. })
  70. minetest.register_craft({
  71. output = 'asrs:tubelib',
  72. type = 'shapeless',
  73. recipe = {'asrs:cell', 'tubelib:tubeS'}
  74. })
  75. tubelib.register_node('asrs:tubelib', {}, {
  76. on_pull_item = function(pos, side)
  77. local meta = minetest.get_meta(pos)
  78. local sys_id = meta:get_string('system_id')
  79. pos = asrs.data[sys_id].inv_pos
  80. meta = minetest.get_meta(pos)
  81. return tubelib.get_item(meta, 'storage')
  82. end,
  83. on_push_item = function(pos, side, item)
  84. local meta = minetest.get_meta(pos)
  85. local sys_id = meta:get_string('system_id')
  86. pos = asrs.data[sys_id].inv_pos
  87. meta = minetest.get_meta(pos)
  88. return tubelib.put_item(meta, 'storage', item)
  89. end,
  90. on_unpull_item = function(pos, side, item)
  91. local meta = minetest.get_meta(pos)
  92. local sys_id = meta:get_string('system_id')
  93. pos = asrs.data[sys_id].inv_pos
  94. meta = minetest.get_meta(pos)
  95. return tubelib.put_item(meta, 'storage', item)
  96. end,
  97. valid_sides = {'B', 'R', 'F', 'L', 'D', 'U'}
  98. })