Advertisement
sanderronde

refuelbot.lua

Jan 4th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 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, matching_chars) == string.sub(data.name, 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.     return true
  31. end
  32.  
  33. local function is_facing_turtle()
  34.     return is_facing_name("ComputerCraft", 13)
  35. end
  36.  
  37. local function get_bucket()
  38.     while true do
  39.         turtle.select(1)
  40.         turtle.suck(1)
  41.         if turtle.getItemCount() > 0 then
  42.             return
  43.         end
  44.         sleep(1)
  45.     end
  46. end
  47.  
  48. local function on_found_turtle()
  49.     sleep(60)
  50.     if not is_facing_turtle() then
  51.         return
  52.     end
  53.  
  54.     -- Get rid of the old one
  55.     turtle.suck(1)
  56.     turtle.turnLeft()
  57.     turtle.drop(1)
  58.     turtle.turnLeft()
  59.  
  60.     -- Grab the new one
  61.     get_bucket()
  62.     turtle.turnLeft()
  63.     turtle.turnLeft()
  64.     turtle.drop(1)
  65. end
  66.  
  67. local function find_turtles()
  68.     while true do
  69.         sleep(1)
  70.         os.pullEvent("turtle")
  71.         if is_facing_turtle() then
  72.  
  73.         end
  74.     end
  75. end
  76.  
  77. local function init()
  78.     if orient() then
  79.         find_turtles()
  80.     end
  81. end
  82.  
  83. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement