Advertisement
Guest User

livingstuff.lua

a guest
Mar 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 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.dropRight()
  103.   end
  104.   digup(SLOT_TRASH)
  105.   digup(SLOT_TRASH)
  106.   digback(SLOT_TRASH)
  107.   digforward(SLOT_TRASH)
  108.   digleft(SLOT_TRASH)
  109.   digforward(SLOT_TRASH)
  110.   digforward(SLOT_TRASH)
  111. end
  112.  
  113. function deposit_output()
  114.   digright(SLOT_TRASH)
  115.   for _ = 1, 5 do
  116.     digforward(SLOT_TRASH)
  117.   end
  118.   digdown(SLOT_TRASH)
  119.   for i = SLOT_OUTPUT, SLOT_OUTPUT + 1 do
  120.     turtle.select(i)
  121.     turtle.dropDown()
  122.   end
  123.   digup(SLOT_TRASH)
  124.   digback(SLOT_TRASH)
  125.   for _ = 1, 5 do
  126.     digforward(SLOT_TRASH)
  127.   end
  128.   turtle.turnRight()
  129. end
  130.  
  131. function start()
  132.   turtle.select(1)
  133.  
  134.   -- count loops to make
  135.  
  136.   local items_left = turtle.getItemCount(SLOT_INPUT) + turtle.getItemCount(SLOT_INPUT + 1)
  137.   local output_left = turtle.getItemCount(SLOT_OUTPUT) + turtle.getItemCount(SLOT_OUTPUT + 1)
  138.   local trash_left = 0
  139.   for i = SLOT_TRASH, SLOT_OUTPUT - 1 do
  140.     trash_left = trash_left + turtle.getItemCount(i)
  141.   end
  142.   local loopcount = math.ceil(items_left / 8)
  143.   -- Now we know how many times we want to put down items, then pick them all up
  144.   -- Notably, this can never be more than 16: I's only checked once.
  145.   -- This means 2 slots will always hold all initial items!
  146.  
  147.   if items_left == 0 then
  148.     print("Zero items given?")
  149.     return
  150.   end
  151.  
  152.   print("Running!")
  153.   if trash_left > 0 then
  154.     deposit_trash()
  155.   end
  156.  
  157.   if output_left > 0 then
  158.     deposit_output()
  159.   end
  160.  
  161.   -- start the choreography
  162.   for i = 1, loopcount do
  163.      digdown(SLOT_TRASH)
  164.      placeloop()
  165.      placeloop()
  166.      digdown(SLOT_TRASH)
  167.  
  168.     -- wait for transmutation
  169.     os.sleep(SLEEP_TIME)  
  170.    
  171.     -- dig down twice
  172.     digdown(SLOT_OUTPUT)
  173.     digdown(SLOT_OUTPUT)
  174.     -- go through other blocks
  175.     digloop(SLOT_OUTPUT)
  176.     digloop(SLOT_OUTPUT)
  177.   end
  178.  
  179.   deposit_output()
  180.   deposit_trash()
  181. end
  182.  
  183. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement