Advertisement
EphemeralKap

Untitled

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