Advertisement
Guest User

livingstuff.lua

a guest
Mar 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. -- livingstuff v0.1
  2.  
  3. local SLOT_INPUT = 1
  4. local SLOT_OUTPUT = 15
  5. local SLOT_TRASH = 3
  6.  
  7. local SLEEP_TIME = 60
  8.  
  9. function trydig(slot)
  10.   turtle.select(slot)
  11.   if turtle.detect() then
  12.     turtle.dig()
  13.   end
  14. end
  15.  
  16. function trydigup(slot)
  17.   turtle.select(slot)
  18.   if turtle.detectUp() then
  19.     turtle.digUp()
  20.   end
  21. end
  22.  
  23. function trydigdown(slot)
  24.   turtle.select(slot)
  25.   if turtle.detectDown() then
  26.     turtle.digDown()
  27.   end
  28. end
  29.  
  30. function digforward(slot)
  31.   trydig(slot)
  32.   turtle.forward()
  33. end
  34.  
  35. function digright(slot)
  36.   turtle.turnRight()
  37.   digforward(slot)
  38. end
  39.  
  40. function digup(slot)
  41.   trydigup(slot)
  42.   turtle.up()
  43. end
  44.  
  45. function digback(slot)
  46.   turtle.turnRight()
  47.   turtle.turnRight()
  48.   digforward(slot)
  49. end
  50.  
  51. function digleft(slot)
  52.   turtle.turnLeft()
  53.   digforward(slot)
  54. end
  55.  
  56. function digdown(slot)
  57.   trydigdown(slot)
  58.   turtle.down()
  59. end
  60.  
  61. function digloop(slot)
  62.   for _ = 1, 4 do
  63.     digright(slot)
  64.     digforward(slot)
  65.   end
  66.   digup(slot)
  67. end
  68.  
  69. function placedown()
  70.   turtle.select(SLOT_TRASH)
  71.   turtle.digDown()
  72.   if turtle.getItemCount(SLOT_INPUT) > 0 then
  73.     turtle.select(SLOT_INPUT)
  74.     turtle.placeDown()
  75.   elseif turtle.getItemCount(SLOT_INPUT + 1) > 0 then
  76.     turtle.select(SLOT_INPUT + 1)
  77.     turtle.placeDown()
  78.   end
  79. end
  80.  
  81. function placeloop()
  82.   for _ = 1, 4 do
  83.     digright(SLOT_TRASH)
  84.     placedown()
  85.     digforward(SLOT_TRASH)
  86.     placedown()
  87.   end
  88.   digup(SLOT_TRASH)
  89. end
  90.  
  91. -- always from neutral pos
  92. function deposit_trash()
  93.   digback(SLOT_TRASH)
  94.   digforward(SLOT_TRASH)
  95.   digforward(SLOT_TRASH)
  96.   digright(SLOT_TRASH)
  97.   digforward(SLOT_TRASH)
  98.   digdown(SLOT_TRASH)
  99.   digdown(SLOT_TRASH)
  100.   for i = SLOT_TRASH, SLOT_OUTPUT - 1 do
  101.     turtle.select(i)
  102.     turtle.turnRight()
  103.     turtle.drop()
  104.     turtle.turnLeft()
  105.   end
  106.   digup(SLOT_TRASH)
  107.   digup(SLOT_TRASH)
  108.   digback(SLOT_TRASH)
  109.   digforward(SLOT_TRASH)
  110.   digleft(SLOT_TRASH)
  111.   digforward(SLOT_TRASH)
  112.   digforward(SLOT_TRASH)
  113. end
  114.  
  115. function deposit_output()
  116.   digright(SLOT_TRASH)
  117.   for _ = 1, 5 do
  118.     digforward(SLOT_TRASH)
  119.   end
  120.   digdown(SLOT_TRASH)
  121.   for i = SLOT_OUTPUT, SLOT_OUTPUT + 1 do
  122.     turtle.select(i)
  123.     turtle.dropDown()
  124.   end
  125.   digup(SLOT_TRASH)
  126.   digback(SLOT_TRASH)
  127.   for _ = 1, 5 do
  128.     digforward(SLOT_TRASH)
  129.   end
  130.   turtle.turnRight()
  131. end
  132.  
  133. function start()
  134.   turtle.select(1)
  135.  
  136.   -- count loops to make
  137.  
  138.   local items_left = turtle.getItemCount(SLOT_INPUT) + turtle.getItemCount(SLOT_INPUT + 1)
  139.   local output_left = turtle.getItemCount(SLOT_OUTPUT) + turtle.getItemCount(SLOT_OUTPUT + 1)
  140.   local trash_left = 0
  141.   for i = SLOT_TRASH, SLOT_OUTPUT - 1 do
  142.     trash_left = trash_left + turtle.getItemCount(i)
  143.   end
  144.   local loopcount = math.ceil(items_left / 8)
  145.   -- Now we know how many times we want to put down items, then pick them all up
  146.   -- Notably, this can never be more than 16: I's only checked once.
  147.   -- This means 2 slots will always hold all initial items!
  148.  
  149.   if items_left == 0 then
  150.     print("Zero items given?")
  151.     return
  152.   end
  153.  
  154.   print("Running!")
  155.   if trash_left > 0 then
  156.     deposit_trash()
  157.   end
  158.  
  159.   if output_left > 0 then
  160.     deposit_output()
  161.   end
  162.  
  163.   -- start the choreography
  164.   for i = 1, loopcount do
  165.      digdown(SLOT_TRASH)
  166.      placeloop()
  167.      placeloop()
  168.      digdown(SLOT_TRASH)
  169.  
  170.     -- wait for transmutation
  171.     os.sleep(SLEEP_TIME)  
  172.    
  173.     -- dig down twice
  174.     digdown(SLOT_OUTPUT)
  175.     digdown(SLOT_OUTPUT)
  176.     -- go through other blocks
  177.     digloop(SLOT_OUTPUT)
  178.     digloop(SLOT_OUTPUT)
  179.   end
  180.  
  181.   deposit_output()
  182.   deposit_trash()
  183. end
  184.  
  185. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement