Advertisement
EphemeralKap

Untitled

Aug 8th, 2015
197
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. for slot = 1, drone.inventorySize() do
  76. if matchCargo(slot, filter) then
  77. return true
  78. end
  79. end
  80. end
  81. local function dropItems(filter)
  82. for slot = 1, drone.inventorySize() do
  83. if matchCargo(slot, filter) then
  84. drone.select(slot)
  85. drone.drop(0)
  86. end
  87. end
  88. end
  89. local waypoints
  90. local function updateWaypoints()
  91. waypoints = nav.findWaypoints(range)
  92. end
  93. local function filterWaypoints(filter)
  94. local result = {}
  95. for _, w in ipairs(waypoints) do
  96. if filter(w) then
  97. table.insert(result, w)
  98. end
  99. end
  100. return result
  101. end
  102. while true do
  103. recharge()
  104. updateWaypoints()
  105. local inputs = filterWaypoints(function(w) return w.redstone > 0 end)
  106. local outputs = filterWaypoints(function(w) return w.redstone < 1 end)
  107. shuffleTable(outputs)
  108. for _, input in ipairs(inputs) do
  109. lx, ly, lz = input.position[1], input.position[2], input.position[3]
  110. moveTo(lx, ly+2, lz)
  111. moveTo(input.position)
  112. pullItems()
  113. drone.setLightColor(colorDelivering)
  114. if next(outputs) == nil then
  115. drone.setLightColor(colorThinking)
  116. os.sleep(2)
  117. end
  118. for _, output in ipairs(outputs) do
  119. drone.setLightColor(colorThinking)
  120. if cargoSize() == 0 then break end
  121. if tonumber(string.sub(output.label,1,1)) ~=level then break end
  122. drone.setLightColor(colorThinking2)
  123. if haveCargoFor(string.sub(output.label,2)) then
  124. llx, lly, llz = output.position[1], output.position[2], output.position[3]
  125. moveTo(llx, lly+2, llz)
  126. moveTo(output.position)
  127. dropItems(output.label)
  128. drone.setLightColor(colorThinking3)
  129. end
  130. end
  131. drone.setLightColor(colorSearching)
  132. end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement