EphemeralKap

Untitled

Aug 8th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 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. drone.setStatusText(string.sub(filter,1,3))
  70. local stack = invctrl.getStackInInternalSlot(slot)
  71. return stack and result == filter
  72. end
  73. return stack and false
  74. end
  75. local function haveCargoFor(filter)
  76. drone.setLightColor(colorThinking3)
  77. for slot = 1, drone.inventorySize() do
  78. if matchCargo(slot, filter) then
  79. return true
  80. end
  81. end
  82. end
  83. local function dropItems(filter)
  84. for slot = 1, drone.inventorySize() do
  85. if matchCargo(slot, filter) then
  86. drone.select(slot)
  87. drone.drop(0)
  88. end
  89. end
  90. end
  91. local waypoints
  92. local function updateWaypoints()
  93. waypoints = nav.findWaypoints(range)
  94. end
  95. local function filterWaypoints(filter)
  96. local result = {}
  97. for _, w in ipairs(waypoints) do
  98. if filter(w) then
  99. table.insert(result, w)
  100. end
  101. end
  102. return result
  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. shuffleTable(outputs)
  110. for _, input in ipairs(inputs) do
  111. lx, ly, lz = input.position[1], input.position[2], input.position[3]
  112. moveTo(lx, ly+2, lz)
  113. moveTo(input.position)
  114. pullItems()
  115. drone.setLightColor(colorDelivering)
  116. if next(outputs) == nil then
  117. drone.setLightColor(colorThinking)
  118. os.sleep(2)
  119. end
  120. for _, output in ipairs(outputs) do
  121. drone.setLightColor(colorThinking)
  122. if cargoSize() == 0 then break end
  123. if tonumber(string.sub(output.label,1,1)) ~=level then break end
  124. drone.setLightColor(colorThinking2)
  125. if "OpenComputers:item1" == string.sub(output.label,2) then computer.beep(200, 1) end
  126. if haveCargoFor(string.sub(output.label,2)) then
  127. llx, lly, llz = output.position[1], output.position[2], output.position[3]
  128. moveTo(llx, lly+2, llz)
  129. moveTo(output.position)
  130. dropItems(output.label)
  131. end
  132. end
  133. drone.setLightColor(colorSearching)
  134. end
  135. end
Advertisement
Add Comment
Please, Sign In to add comment