Advertisement
charonme

quarry

May 8th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- quarry
  2. -- "swapper" (engineering turtle (turtle+crescent hammer)) must face NORTH
  3. -- "mover" (digging turtle) must face the swapper from the right (should face west)
  4. -- the quarry, 3 enderchests and a power cell must be placed at the beginning
  5.  
  6. local args = {...}
  7. local role = args[1]
  8. local FUEL_SLOT = 1
  9. local QUARRY_SLOT = 2
  10. local ENERGY_CELL_SLOT = 3
  11. local CHARGED_CELLS_ENDERCHEST_SLOT = 4
  12. local EMPTY_CELLS_ENDERCHEST_SLOT = 5
  13. local ITEMS_ENDERCHEST_SLOT = 6
  14. local TRASH_SLOT = 7
  15. local RELOCATE_DISTANCE = 9
  16. local MIN_FUEL_FOR_CYCLE = RELOCATE_DISTANCE + 12
  17. local STEPS_COUNT = 3
  18. local STEP_SLEEP = 360
  19. local MEETING_SLEEP = 5
  20. local MOVING_SLEEP = 1
  21. local QUARRY_STOP_SLEEP = 45 -- wait for the quarry to stop after disconnecting power
  22. local WAIT_FOR_MOVER = 20
  23.  
  24. function saveState(state)
  25.     f = fs.open("state","w")
  26.     f.write(tostring(state))
  27.     f.close()
  28. end
  29.  
  30. function swapper_recharge()
  31.     turtle.select(ENERGY_CELL_SLOT)
  32.     turtle.dig()
  33.     turtle.dropDown()
  34.     while turtle.getItemCount(ENERGY_CELL_SLOT)==0 do
  35.         turtle.suckUp()
  36.     end
  37.     turtle.place()
  38. end
  39.  
  40. function swapper_pack()
  41.     turtle.select(ENERGY_CELL_SLOT)
  42.     turtle.dig()
  43.     turtle.dropDown()
  44.     sleep(QUARRY_STOP_SLEEP)
  45.     turtle.back() -- make room for mover
  46.     turtle.turnLeft()
  47.     while not(turtle.forward()) do
  48.         sleep(1)
  49.     end
  50.     sleep(1)
  51.     while not(turtle.forward()) do
  52.         sleep(1)
  53.     end
  54.     turtle.turnRight()
  55.     print("giving mover time to relocate in front of swapper")
  56.     sleep(WAIT_FOR_MOVER)
  57. end
  58.  
  59. function swapper_relocate()
  60.     for i=1,RELOCATE_DISTANCE do
  61.         sleep(MOVING_SLEEP)
  62.         while not turtle.forward() do
  63.             print("couldn't move forward, sleeping for a while")
  64.             sleep(MOVING_SLEEP)
  65.         end
  66.     end
  67. end
  68.  
  69. function swapper_unpack()
  70.     turtle.turnRight()
  71.     while not(turtle.forward()) do
  72.         print("couldn't move forward, sleeping for a while")
  73.         sleep(1)
  74.     end
  75.     sleep(1)
  76.     while not(turtle.forward()) do
  77.         print("couldn't move forward, sleeping for a while")
  78.         sleep(1)
  79.     end
  80.     turtle.turnLeft()
  81.     sleep(WAIT_FOR_MOVER)
  82.     while not turtle.forward() do
  83.         sleep(MEETING_SLEEP)
  84.     end
  85.     swapper_recharge()
  86. end
  87.  
  88. function swapper_cycle()
  89.     for i=1,(STEPS_COUNT-1) do
  90.         print("giving quarry time")
  91.         sleep(STEP_SLEEP)
  92.         print("recharging quarry")
  93.         swapper_recharge()
  94.     end
  95.     print("giving quarry time")
  96.     sleep(STEP_SLEEP)
  97.     print("packing")
  98.     swapper_pack()
  99.     print("relocating")
  100.     swapper_relocate()
  101.     print("unpacking")
  102.     swapper_unpack()
  103. end
  104.  
  105. function mover_cleanup()
  106.     turtle.select(TRASH_SLOT)
  107.     turtle.dig()
  108.     turtle.digUp()
  109.     turtle.digDown()
  110.     turtle.dropDown()
  111.     turtle.drop()
  112. end
  113.  
  114. function mover_pack()
  115.     turtle.select(CHARGED_CELLS_ENDERCHEST_SLOT)
  116.     turtle.digUp()
  117.     turtle.select(EMPTY_CELLS_ENDERCHEST_SLOT)
  118.     turtle.digDown()
  119.     turtle.turnLeft()
  120.     turtle.forward()
  121.     turtle.turnRight()
  122.     turtle.select(QUARRY_SLOT)
  123.     turtle.dig()
  124.     turtle.turnLeft()
  125.     turtle.forward()
  126.     turtle.turnRight()
  127.     turtle.select(ITEMS_ENDERCHEST_SLOT)
  128.     turtle.dig()
  129. end
  130. function mover_unpack()
  131.     mover_cleanup()
  132.     turtle.select(ITEMS_ENDERCHEST_SLOT)
  133.     turtle.place()
  134.     turtle.turnRight()
  135.     turtle.forward()
  136.     turtle.turnLeft()
  137.     mover_cleanup()
  138.     turtle.select(QUARRY_SLOT)
  139.     turtle.place()
  140.     turtle.turnRight()
  141.     turtle.forward()
  142.     turtle.turnLeft()
  143.     mover_cleanup()
  144.     turtle.select(EMPTY_CELLS_ENDERCHEST_SLOT)
  145.     turtle.placeDown()
  146.     turtle.select(CHARGED_CELLS_ENDERCHEST_SLOT)
  147.     turtle.placeUp()
  148.  
  149.     turtle.turnLeft()
  150.     turtle.back()
  151.     sleep(MEETING_SLEEP)
  152. end
  153. function mover_relocate()
  154.     for i=1,RELOCATE_DISTANCE do
  155.         mover_cleanup()
  156.         turtle.forward()
  157.     end
  158. end
  159.  
  160. function mover_cycle()
  161.     sleep(STEPS_COUNT*STEP_SLEEP+QUARRY_STOP_SLEEP)
  162.     while not(turtle.forward()) do
  163.         sleep(MEETING_SLEEP)
  164.     end
  165.     sleep(MEETING_SLEEP)
  166.     turtle.turnRight()
  167.     mover_pack()
  168.     sleep(2)
  169.     mover_relocate()
  170.     mover_unpack()
  171. end
  172.  
  173. function check_fuel()
  174.     if turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE then
  175.         print("not enough fuel, waiting for fuel")
  176.         while turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE do
  177.             sleep(2)
  178.             turtle.select(FUEL_SLOT)
  179.             turtle.refuel()
  180.         end
  181.     end
  182. end
  183.  
  184. -- ***********************************************************************
  185.  
  186. if role=="swapper" then
  187.     print("starting swapper")
  188.     while true do
  189.         check_fuel()
  190.         swapper_cycle()
  191.     end
  192. elseif role=="mover" then
  193.     print("starting mover")
  194.     while true do
  195.         check_fuel()
  196.         mover_cycle()
  197.     end
  198. else
  199.     print("invalid parameter, expecting 'mover' or 'swapper'")
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement