EphemeralKap

Untitled

Aug 7th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 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, 2, 0)
  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 shuffleTable(t)
  54. local rand = math.random
  55. local ite = #t
  56. local j
  57. for i = ite, 2, -1 do
  58. j = rand(i)
  59. t[i], t[j] = t[j], t[i]
  60. end
  61. end
  62.  
  63. local function pullItems()
  64. local start = computer.uptime()
  65. repeat until not drone.suck(0) or computer.uptime() - start > 5
  66. end
  67.  
  68. local function matchCargo(slot, filter)
  69. if not invctrl or not filter or filter == "" then
  70. return true
  71. end
  72. local stack = invctrl.getStackInInternalSlot(slot)
  73. if stack then
  74. local result = invctrl.getStackInInternalSlot(slot).name..invctrl.getStackInInternalSlot(slot).damage
  75. local stack = invctrl.getStackInInternalSlot(slot)
  76. return stack and result == filter
  77. end
  78. return stack and false
  79. end
  80.  
  81. local function haveCargoFor(filter)
  82. for slot = 1, drone.inventorySize() do
  83. if matchCargo(slot, filter) then
  84. return true
  85. end
  86. end
  87. end
  88.  
  89. local function dropItems(filter)
  90. for slot = 1, drone.inventorySize() do
  91. if matchCargo(slot, filter) then
  92. drone.select(slot)
  93. drone.drop(0)
  94. end
  95. end
  96. end
  97. local waypoints
  98.  
  99. local function updateWaypoints()
  100. waypoints = nav.findWaypoints(range)
  101. end
  102.  
  103. local function filterWaypoints(filter)
  104. local result = {}
  105. for _, w in ipairs(waypoints) do
  106. if filter(w) then
  107. table.insert(result, w)
  108. end
  109. end
  110. return result
  111. end
  112.  
  113. while true do
  114. recharge()
  115. updateWaypoints()
  116. local inputs = filterWaypoints(function(w) return w.redstone > 0 end)
  117. local outputs = filterWaypoints(function(w) return w.redstone < 1 end)
  118. shuffleTable(outputs)
  119. for _, input in ipairs(inputs) do
  120. lx, ly, lz = input.position[1], input.position[2], input.position[3]
  121. moveTo(lx, ly+2, lz)
  122. moveTo(input.position)
  123. pullItems()
  124. drone.setLightColor(colorDelivering)
  125. for _, output in ipairs(outputs) do
  126. if cargoSize() == 0 then break end
  127. if haveCargoFor(output.label) then
  128. llx, lly, llz = output.position[1], output.position[2], output.position[3]
  129. moveTo(llx, lly+2, llz)
  130. moveTo(output.position)
  131. dropItems(output.label)
  132. end
  133. end
  134. drone.setLightColor(colorSearching)
  135. end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment