Advertisement
EphemeralKap

Untitled

Aug 8th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 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 colorThinking = 0xFF0000
  17. local colorThinking2 = 0xFF00FF
  18. local colorThinking3 = 0xFFFFFF
  19. local px, py, pz = 0, 0, 0
  20. local function moveTo(x, y, z)
  21. if type(x) == "table" then
  22. x, y, z = x[1], x[2], x[3]
  23. end
  24. local rx, ry, rz = x - px, y - py, z - pz
  25. drone.move(rx, ry, rz)
  26. while drone.getOffset() > 0.5 or drone.getVelocity() > 0.5 do
  27. computer.pullSignal(0.5)
  28. end
  29. px, py, pz = x, y, z
  30. end
  31. local function recharge()
  32. drone.setLightColor(colorCharing)
  33. moveTo(0, 2, 0)
  34. moveTo(0, 0, 0)
  35. if computer.energy() < computer.maxEnergy() * 0.1 then
  36. while computer.energy() < computer.maxEnergy() * 0.9
  37. do computer.pullSignal(1)
  38. end
  39. end
  40. drone.setLightColor(colorSearching)
  41. end
  42. local function cargoSize()
  43. local result = 0
  44. for slot = 1, drone.inventorySize() do
  45. result = result + drone.count(slot)
  46. end
  47. return result
  48. end
  49. local function shuffleTable(t)
  50. local rand = math.random
  51. local ite = #t
  52. local j
  53. for i = ite, 2, -1 do
  54. j = rand(i)
  55. t[i], t[j] = t[j], t[i]
  56. end
  57. end
  58. local function pullItems()
  59. local start = computer.uptime()
  60. repeat until not drone.suck(0) or computer.uptime() - start > 5
  61. end
  62. local function matchCargo(slot, filter)
  63. if not invctrl or not filter or filter == "" then
  64. return true
  65. end
  66. local stack = invctrl.getStackInInternalSlot(slot)
  67. if stack then
  68. local result = string.sub(invctrl.getStackInInternalSlot(slot).name, 2)..invctrl.getStackInInternalSlot(slot).damage
  69. local stack = invctrl.getStackInInternalSlot(slot)
  70. return stack and result == filter
  71. end
  72. return stack and false
  73. end
  74. local function haveCargoFor(filter)
  75. drone.setLightColor(colorThinking3)
  76. for slot = 1, drone.inventorySize() do
  77. if matchCargo(slot, filter) then
  78. return true
  79. end
  80. end
  81. end
  82. local function dropItems(filter)
  83. for slot = 1, drone.inventorySize() do
  84. if matchCargo(slot, filter) then
  85. drone.select(slot)
  86. drone.drop(0)
  87. end
  88. end
  89. end
  90. local waypoints
  91. local function updateWaypoints()
  92. waypoints = nav.findWaypoints(range)
  93. end
  94. local function filterWaypoints(filter)
  95. local result = {}
  96. for _, w in ipairs(waypoints) do
  97. if filter(w) then
  98. table.insert(result, w)
  99. end
  100. end
  101. return result
  102. end
  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. shuffleTable(outputs)
  109. for _, input in ipairs(inputs) do
  110. lx, ly, lz = input.position[1], input.position[2], input.position[3]
  111. moveTo(lx, ly+2, lz)
  112. moveTo(input.position)
  113. pullItems()
  114. drone.setLightColor(colorDelivering)
  115. if next(outputs) == nil then
  116. drone.setLightColor(colorThinking)
  117. os.sleep(2)
  118. end
  119. for _, output in ipairs(outputs) do
  120. drone.setLightColor(colorThinking)
  121. if cargoSize() == 0 then break end
  122. if tonumber(string.sub(output.label,1,1)) ~=level then break end
  123. drone.setLightColor(colorThinking2)
  124. if haveCargoFor(string.sub(output.label,2)) then
  125. llx, lly, llz = output.position[1], output.position[2], output.position[3]
  126. moveTo(llx, lly+2, llz)
  127. moveTo(output.position)
  128. dropItems(output.label)
  129. end
  130. end
  131. drone.setLightColor(colorSearching)
  132. end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement