Advertisement
EphemeralKap

Untitled

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