Advertisement
Guest User

livingstuff.lua

a guest
Mar 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 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.   if trash_left > 0 then
  153.     deposit_trash()
  154.   end
  155.  
  156.   if output_left > 0 then
  157.     deposit_output()
  158.   end
  159.  
  160.   -- start the choreography
  161.   for i = 1, loopcount do
  162.      digdown(SLOT_TRASH)
  163.      placeloop()
  164.      digup(SLOT_TRASH)
  165.      placeloop()
  166.  
  167.     -- wait for transmutation
  168.     os.sleep(SLEEP_TIME)  
  169.    
  170.     -- dig down twice
  171.     digdown(SLOT_OUTPUT)
  172.     digdown(SLOT_OUTPUT)
  173.     -- go through other blocks
  174.     digloop(SLOT_OUTPUT)
  175.     digloop(SLOT_OUTPUT)
  176.   end
  177.  
  178.   deposit_output()
  179.   deposit_trash()
  180. end
  181.  
  182. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement