Advertisement
EphemeralKap

Untitled

Aug 6th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. local range = 32
  2.  
  3. local function proxyFor(name, required)
  4. local address = component and component.list(name)()
  5. if not address and required then
  6. error("missing component '" .. name .. "'")
  7. end
  8. return address and component.proxy(address) or nil
  9. end
  10.  
  11. local drone = proxyFor("drone", true)
  12. local nav = proxyFor("navigation", true)
  13. local invctrl = proxyFor("inventory_controller")
  14.  
  15. local colorCharing = 0xFFCC33
  16. local colorSearching = 0x66CC66
  17. local colorDelivering = 0x6699FF
  18.  
  19. local px, py, pz = 0, 0, 0
  20.  
  21. local function moveTo(x, y, z)
  22. if type(x) == "table" then
  23. x, y, z = x[1], x[2], x[3]
  24. end
  25. local rx, ry, rz = x - px, y - py, z - pz
  26. drone.move(rx, ry, rz)
  27. while drone.getOffset() > 0.5 or drone.getVelocity() > 0.5 do
  28. computer.pullSignal(0.5)
  29. end
  30. px, py, pz = x, y, z
  31. end
  32.  
  33. local function recharge()
  34. drone.setLightColor(colorCharing)
  35. moveTo(0, 0, 0)
  36. if computer.energy() < computer.maxEnergy() * 0.1 then
  37. while computer.energy() < computer.maxEnergy() * 0.9 do
  38. computer.pullSignal(1)
  39. end
  40. end
  41. drone.setLightColor(colorSearching)
  42. end
  43.  
  44. local function cargoSize()
  45. local result = 0
  46. for slot = 1, drone.inventorySize() do
  47. result = result + drone.count(slot)
  48. end
  49. return result
  50. end
  51.  
  52. local function pullItems()
  53. local start = computer.uptime()
  54. repeat until not drone.suck(0) or computer.uptime() - start > 5
  55. end
  56.  
  57. local function matchCargo(slot, filter)
  58. if not invctrl or not filter or filter == "" then
  59. return true
  60. end
  61. local result = invctrl.getStackInInternalSlot(slot).name..invctrl.getStackInInternalSlot(slot).damage
  62. local stack = invctrl.getStackInInternalSlot(slot)
  63. return stack and result:match(filter)
  64. end
  65.  
  66. local function haveCargoFor(filter)
  67. for slot = 1, drone.inventorySize() do
  68. if matchCargo(slot, filter) then
  69. return true
  70. end
  71. end
  72. end
  73.  
  74. local function dropItems(filter)
  75. for slot = 1, drone.inventorySize() do
  76. if matchCargo(slot, filter) then
  77. drone.select(slot)
  78. drone.drop(0)
  79. end
  80. end
  81. end
  82. local waypoints
  83.  
  84. local function updateWaypoints()
  85. waypoints = nav.findWaypoints(range)
  86. end
  87.  
  88. local function filterWaypoints(filter)
  89. local result = {}
  90. for _, w in ipairs(waypoints) do
  91. if filter(w) then
  92. table.insert(result, w)
  93. end
  94. end
  95. return result
  96. end
  97.  
  98. while true do
  99. recharge()
  100. updateWaypoints()
  101. local inputs = filterWaypoints(function(w) return w.redstone > 0 end)
  102. local outputs = filterWaypoints(function(w) return w.redstone < 1 end)
  103. for _, input in ipairs(inputs) do
  104. moveTo(input.position)
  105. pullItems()
  106. drone.setLightColor(colorDelivering)
  107. for _, output in ipairs(outputs) do
  108. if cargoSize() == 0 then break end
  109. if haveCargoFor(output.label) then
  110. moveTo(output.position)
  111. dropItems(output.label)
  112. end
  113. end
  114. drone.setLightColor(colorSearching)
  115. end
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement