Guest User

Untitled

a guest
Aug 24th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. -- Simple drone program demonstrating the use of waypoints (added in 1.5.9) on
  2. -- the example of a simple sorting system, pulling items from multiple input
  3. -- inventories and placing the items into multiple output inventories, with
  4. -- optional filtering, based on the waypoint labels.
  5.  
  6. -- The only config option: how far a waypoint may be away from the starting
  7. -- position for it to be used.
  8. local range = 32
  9.  
  10. local function proxyFor(name, required)
  11. local address = component and component.list(name)()
  12. if not address and required then
  13. error("missing component '" .. name .. "'")
  14. end
  15. return address and component.proxy(address) or nil
  16. end
  17.  
  18. local drone = proxyFor("drone", true)
  19. local nav = proxyFor("navigation", true)
  20. local invctrl = proxyFor("inventory_controller")
  21.  
  22. -- Colors used to indicate different states of operation.
  23. local colorCharing = 0xFFCC33
  24. local colorSearching = 0x66CC66
  25. local colorDelivering = 0x6699FF
  26.  
  27. -- Keep track of our own position, relative to our starting position.
  28. local px, py, pz = 0, 0, 0
  29.  
  30. local function moveTo(x, y, z)
  31. if type(x) == "table" then
  32. x, y, z = x[1], x[2], x[3]
  33. end
  34. local rx, ry, rz = x - px, y - py, z - pz
  35. drone.move(rx, ry, rz)
  36. while drone.getOffset() > 0.5 or drone.getVelocity() > 0.5 do
  37. computer.pullSignal(0.5)
  38. end
  39. px, py, pz = x, y, z
  40. end
  41.  
  42. local function recharge()
  43. drone.setLightColor(colorCharing)
  44. moveTo(0, 0, 0)
  45. if computer.energy() < computer.maxEnergy() * 0.1 then
  46. while computer.energy() < computer.maxEnergy() * 0.9 do
  47. computer.pullSignal(1)
  48. end
  49. end
  50. drone.setLightColor(colorSearching)
  51. end
  52.  
  53. local function cargoSize()
  54. local result = 0
  55. for slot = 1, drone.inventorySize() do
  56. result = result + drone.count(slot)
  57. end
  58. return result
  59. end
  60.  
  61. local function pullItems()
  62. -- Only wait up to 5 seconds, avoids dribbling inputs from stalling us.
  63. local start = computer.uptime()
  64. repeat until not drone.suck(0) or computer.uptime() - start > 5
  65. end
  66.  
  67. local function matchCargo(slot, filter)
  68. if not invctrl or not filter or filter == "" then
  69. return true
  70. end
  71. local stack = invctrl.getStackInInternalSlot(slot)
  72. return stack and stack.name:match(filter)
  73. end
  74.  
  75. local function haveCargoFor(filter)
  76. for slot = 1, drone.inventorySize() do
  77. if matchCargo(slot, filter) then
  78. return true
  79. end
  80. end
  81. end
  82.  
  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.  
  92. -- List of known waypoints and their positions relative to the position of the
  93. -- drone when it was started. Waypoints are detected when the program first
  94. -- starts, and when it returns to its home position.
  95. local waypoints
  96.  
  97. local function updateWaypoints()
  98. waypoints = nav.findWaypoints(range)
  99. end
  100.  
  101. local function filterWaypoints(filter)
  102. local result = {}
  103. for _, w in ipairs(waypoints) do
  104. if filter(w) then
  105. table.insert(result, w)
  106. end
  107. end
  108. return result
  109. end
  110.  
  111. -- Main program loop; keep returning to recharge, then check all inputs
  112. -- sequentially, distributing what can be picked up from them to all
  113. -- outputs.
  114. while true do
  115. recharge()
  116. updateWaypoints()
  117. -- Get waypoints marking input inventories; defined as those with a high
  118. -- redstone signal going into them (because there'll usually be more input
  119. -- than output inventories).
  120. local inputs = filterWaypoints(function(w) return w.redstone > 0 end)
  121. local outputs = filterWaypoints(function(w) return w.redstone < 1 end)
  122. for _, input in ipairs(inputs) do
  123. moveTo(input.position)
  124. pullItems()
  125. drone.setLightColor(colorDelivering)
  126. for _, output in ipairs(outputs) do
  127. if cargoSize() == 0 then break end
  128. if haveCargoFor(output.label) then
  129. moveTo(output.position)
  130. dropItems(output.label)
  131. end
  132. end
  133. drone.setLightColor(colorSearching)
  134. end
  135. end
Add Comment
Please, Sign In to add comment