EphemeralKap

Untitled

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