EphemeralKap

Untitled

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