EphemeralKap

Untitled

Aug 6th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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 result = invctrl.getStackInInternalSlot(slot).name..invctrl.getStackInInternalSlot(slot).damage
  72. local stack = invctrl.getStackInInternalSlot(slot)
  73. return stack and result:match(filter)
  74. end
  75.  
  76. local function haveCargoFor(filter)
  77. for slot = 1, drone.inventorySize() do
  78. if matchCargo(slot, filter) then
  79. return true
  80. end
  81. end
  82. end
  83.  
  84. local function dropItems(filter)
  85. for slot = 1, drone.inventorySize() do
  86. if matchCargo(slot, filter) then
  87. drone.select(slot)
  88. drone.drop(0)
  89. end
  90. end
  91. end
  92.  
  93. -- List of known waypoints and their positions relative to the position of the
  94. -- drone when it was started. Waypoints are detected when the program first
  95. -- starts, and when it returns to its home position.
  96. local waypoints
  97.  
  98. local function updateWaypoints()
  99. waypoints = nav.findWaypoints(range)
  100. end
  101.  
  102. local function filterWaypoints(filter)
  103. local result = {}
  104. for _, w in ipairs(waypoints) do
  105. if filter(w) then
  106. table.insert(result, w)
  107. end
  108. end
  109. return result
  110. end
  111.  
  112. -- Main program loop; keep returning to recharge, then check all inputs
  113. -- sequentially, distributing what can be picked up from them to all
  114. -- outputs.
  115. while true do
  116. recharge()
  117. updateWaypoints()
  118. -- Get waypoints marking input inventories; defined as those with a high
  119. -- redstone signal going into them (because there'll usually be more input
  120. -- than output inventories).
  121. local inputs = filterWaypoints(function(w) return w.redstone > 0 end)
  122. local outputs = filterWaypoints(function(w) return w.redstone < 1 end)
  123. for _, input in ipairs(inputs) do
  124. moveTo(input.position)
  125. pullItems()
  126. drone.setLightColor(colorDelivering)
  127. for _, output in ipairs(outputs) do
  128. if cargoSize() == 0 then break end
  129. if haveCargoFor(output.label) then
  130. moveTo(output.position)
  131. dropItems(output.label)
  132. end
  133. end
  134. drone.setLightColor(colorSearching)
  135. end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment