sanderronde

obsidianbot.lua

Jan 10th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local function is_facing_name(str)
  2.     local success, data = turtle.inspect()
  3.     if not success then
  4.         return false
  5.     end
  6.     if data.name == str then
  7.         return true
  8.     end
  9.     return false
  10. end
  11.  
  12. local function is_facing_something_interesting()
  13.     if is_facing_name("minecraft:stone") then
  14.         return true
  15.     elseif is_facing_name("minecraft:flowing_lava") then
  16.         return true
  17.     elseif is_facing_name("minecraft:redstone_wire") then
  18.         return true
  19.     elseif is_facing_name("minecraft:obsidian") then
  20.         return true
  21.     end
  22.     return false
  23. end
  24.  
  25. local function orient()
  26.     local turned = 0
  27.     while not is_facing_something_interesting() do
  28.         turtle.turnLeft()
  29.         turned = turned + 1
  30.         if turned >= 4 then
  31.             print("Could not find orientation")
  32.             return false
  33.         end
  34.     end
  35.     return true
  36. end
  37.  
  38. local function get_state()
  39.     if is_facing_name("minecraft:flowing_lava") then
  40.         return 4
  41.     elseif is_facing_name("minecraft:redstone_wire") then
  42.         return 2
  43.     elseif is_facing_name("minecraft:obsidian") then
  44.         return 3
  45.     end
  46.     return 4
  47. end
  48.  
  49. local function select_redstone()
  50.     for s = 2, 9 do
  51.         turtle.select(s)
  52.         if turtle.getItemCount() > 0 then
  53.             return true
  54.         end
  55.     end
  56.     return false
  57. end
  58.  
  59. local function get_redstone()
  60.     if select_redstone() then
  61.         return
  62.     end
  63.  
  64.     turtle.turnRight()
  65.     for s = 2, 14 do
  66.         turtle.select(s)
  67.         turtle.suck(64)
  68.     end
  69.  
  70.     if select_redstone() then
  71.         turtle.turnLeft()
  72.         return
  73.     end
  74.  
  75.     print("Redstone chest is empty!")
  76.     while not select_redstone() do
  77.         sleep(5)
  78.     end
  79.  
  80.     turtle.turnLeft()
  81. end
  82.  
  83. local function empty_inventory()
  84.     turtle.select(15)
  85.     if turtle.getItemCount() == 64 then
  86.         turtle.turnLeft()
  87.         if turtle.drop(64) then
  88.             turtle.turnRight()
  89.             return
  90.         end
  91.  
  92.         print("Dumping chest is full")
  93.         while not turtle.drop(64) do
  94.             sleep(5)
  95.         end
  96.     end
  97. end
  98.  
  99. local function do_iteration(initial_state)
  100.     if not initial_state then
  101.         initial_state = 1
  102.     end
  103.  
  104.  
  105.  
  106.     if initial_state < 2 then
  107.         -- Place redstone wire
  108.         turtle.select(15)
  109.         turtle.dig()
  110.  
  111.         select_redstone()
  112.  
  113.         turtle.place()
  114.     end
  115.     if initial_state < 3 then
  116.         redstone.setOutput("top", false)
  117.  
  118.         -- Wait for a bit
  119.         while not is_facing_name("minecraft:obsidian") do
  120.             sleep(1)
  121.         end
  122.     end
  123.     if initial_state < 4 then
  124.         redstone.setOutput("top", true)
  125.  
  126.         -- Make sure you have enough redstone and stuff for the next
  127.         -- iteration
  128.         get_redstone()
  129.         empty_inventory()
  130.        
  131.         -- Mine it
  132.         turtle.select(15)
  133.         turtle.dig()
  134.     end
  135. end
  136.  
  137. local function start()
  138.     redstone.setOutput("top", true)
  139.  
  140.     do_iteration(get_state())
  141.     while true do
  142.         do_iteration()
  143.     end
  144. end
  145.  
  146. local function init()
  147.     orient()
  148.     start()
  149. end
  150.  
  151. init()
Add Comment
Please, Sign In to add comment