Advertisement
sanderronde

refuelbot.lua

Jan 4th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. local CHEST_NAME = "EnderStorage:enderChest"
  2.  
  3. local function is_facing_name(str, matching_chars)
  4.     local success, data = turtle.inspect()
  5.     if not success then
  6.         return false
  7.     end
  8.     if not matching_chars then
  9.         if data.name == str then
  10.             return true
  11.         end
  12.     else
  13.         if string.sub(str, 1, matching_chars) == string.sub(data.name, 1, matching_chars) then
  14.             return true
  15.         end
  16.     end
  17.     return false
  18. end
  19.  
  20. local function orient()
  21.     local turned = 0
  22.     while not is_facing_name(CHEST_NAME) do
  23.         turtle.turnLeft()
  24.         turned = turned + 1
  25.         if turned >= 4 then
  26.             print("Could not find orientation")
  27.             return false
  28.         end
  29.     end
  30.     turtle.turnLeft()
  31.     turtle.turnLeft()
  32.     return true
  33. end
  34.  
  35. local function is_facing_turtle()
  36.     return is_facing_name("ComputerCraft", 13)
  37. end
  38.  
  39. local function get_bucket()
  40.     while true do
  41.         turtle.select(1)
  42.         turtle.suck(1)
  43.         if turtle.getItemCount() > 0 then
  44.             return
  45.         end
  46.         sleep(1)
  47.     end
  48. end
  49.  
  50. local function on_found_turtle()
  51.     sleep(60)
  52.     if not is_facing_turtle() then
  53.         print("Turtle not there anymore")
  54.         return
  55.     else
  56.         print("Turtle is still there")
  57.     end
  58.  
  59.     -- Get rid of the old one
  60.     turtle.suck(1)
  61.     turtle.turnLeft()
  62.     turtle.drop(1)
  63.     turtle.turnLeft()
  64.  
  65.     -- Grab the new one
  66.     get_bucket()
  67.     turtle.turnLeft()
  68.     turtle.turnLeft()
  69.     turtle.drop(1)
  70.  
  71.     -- Put the old one in the chest
  72.     turtle.turnLeft()
  73.     turtle.suck(1)
  74.     turtle.turnLeft()
  75.     turtle.drop(1)
  76.     turtle.turnLeft()
  77.     turtle.turnLeft()
  78. end
  79.  
  80. local function find_turtles()
  81.     while true do
  82.         sleep(1)
  83.         if is_facing_turtle() then
  84.             print("Found a turtle")
  85.             on_found_turtle()
  86.         end
  87.     end
  88. end
  89.  
  90. local function init()
  91.     if orient() then
  92.         find_turtles()
  93.     end
  94. end
  95.  
  96. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement