EphemeralKap

Untitled

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