EphemeralKap

Untitled

Aug 8th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. local range = 32
  2. local level = 1
  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. local drone = proxyFor("drone", true)
  11. local nav = proxyFor("navigation", true)
  12. local invctrl = proxyFor("inventory_controller")
  13. local colorCharing = 0xFFCC33
  14. local colorSearching = 0x66CC66
  15. local colorDelivering = 0x6699FF
  16. local px, py, pz = 0, 0, 0
  17. local function moveTo(x, y, z)
  18. if type(x) == "table" then
  19. x, y, z = x[1], x[2], x[3]
  20. end
  21. local rx, ry, rz = x - px, y - py, z - pz
  22. drone.move(rx, ry, rz)
  23. while drone.getOffset() > 0.5 or drone.getVelocity() > 0.5 do
  24. computer.pullSignal(0.5)
  25. end
  26. px, py, pz = x, y, z
  27. end
  28. local function recharge()
  29. drone.setLightColor(colorCharing)
  30. moveTo(0, 2, 0)
  31. moveTo(0, 0, 0)
  32. if computer.energy() < computer.maxEnergy() * 0.1 then
  33. while computer.energy() < computer.maxEnergy() * 0.9 do computer.pullSignal(1) end
  34. computer.beep(783.99) computer.pullSignal(.25) computer.beep(783.99) computer.pullSignal(.25) computer.beep(783.99) computer.beep(659.25) computer.beep(1046.5)
  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 = string.sub(invctrl.getStackInInternalSlot(slot).name, 2)..string.sub(invctrl.getStackInInternalSlot(slot).damage, 2)
  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. local function updateWaypoints()
  87. waypoints = nav.findWaypoints(range)
  88. end
  89. local function filterWaypoints(filter)
  90. local result = {}
  91. for lab, w in ipairs(waypoints) do
  92. if filter(w) and string.sub(lab, 1, 1) == level then
  93. table.insert(result, w)
  94. end
  95. end
  96. return result
  97. end
  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. shuffleTable(outputs)
  104. for _, input in ipairs(inputs) do
  105. lx, ly, lz = input.position[1], input.position[2], input.position[3]
  106. moveTo(lx, ly+2, lz)
  107. moveTo(input.position)
  108. pullItems()
  109. drone.setLightColor(colorDelivering)
  110. for _, output in ipairs(outputs) do
  111. if cargoSize() == 0 then break end
  112. if haveCargoFor(string.sub(output.label,2) then
  113. llx, lly, llz = output.position[1], output.position[2], output.position[3]
  114. moveTo(llx, lly+2, llz)
  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