Advertisement
sanderronde

refuelbot.lua

Jan 5th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. local CHEST_NAME = "EnderStorage:enderChest"
  2. local went_up = false
  3.  
  4. local function is_facing_name(str, matching_chars)
  5.     local success, data = turtle.inspect()
  6.     if not success then
  7.         return false
  8.     end
  9.     if not matching_chars then
  10.         if data.name == str then
  11.             return true
  12.         end
  13.     else
  14.         if string.sub(str, 1, matching_chars) == string.sub(data.name, 1, matching_chars) then
  15.             return true
  16.         end
  17.     end
  18.     return false
  19. end
  20.  
  21. local function orient()
  22.     local turned = 0
  23.     while not is_facing_name(CHEST_NAME) do
  24.         turtle.turnLeft()
  25.         turned = turned + 1
  26.         if turned >= 4 then
  27.             print("Could not find orientation")
  28.             return -1
  29.         end
  30.     end
  31.     turtle.turnLeft()
  32.     turtle.turnLeft()
  33.     return turned
  34. end
  35.  
  36. local function is_facing_turtle()
  37.     return is_facing_name("ComputerCraft", 13)
  38. end
  39.  
  40.  
  41.  
  42. local function up()
  43.     if turtle.getFuelLevel() < 2 then
  44.         -- Needs to go down eventually, resupply yourself first
  45.         local turned = orient()
  46.         turtle.turnLeft()
  47.         turtle.turnLeft()
  48.         turtle.suck(1)
  49.         turtle.refuel()
  50.         turtle.drop(1)
  51.         -- Wait for the bucket to be filled
  52.         sleep(60)
  53.         for t = 0, turned do
  54.             turtle.turnRight()
  55.         end
  56.     end
  57.     turtle.up()
  58. end
  59.  
  60. local function down()
  61.     turtle.down()
  62. end
  63.  
  64. local function get_bucket()
  65.     if went_up then
  66.         down()
  67.     end
  68.     while true do
  69.         turtle.select(1)
  70.         turtle.suck(1)
  71.         if turtle.getItemCount() > 0 then
  72.             if went_up then
  73.                 up()
  74.             end
  75.             return
  76.         end
  77.         sleep(1)
  78.     end
  79. end
  80.  
  81. local function on_found_turtle()
  82.     sleep(60)
  83.     if not is_facing_turtle() then
  84.         print("Turtle not there anymore")
  85.         return
  86.     else
  87.         print("Turtle is still there")
  88.     end
  89.  
  90.     -- Get rid of the old one
  91.     turtle.suck(1)
  92.     turtle.turnLeft()
  93.     turtle.drop(1)
  94.     turtle.turnLeft()
  95.  
  96.     -- Grab the new one
  97.     get_bucket()
  98.     turtle.turnLeft()
  99.     turtle.turnLeft()
  100.     turtle.drop(1)
  101.  
  102.     -- Put the old one in the chest
  103.     if went_up then
  104.         down()
  105.     end
  106.     turtle.turnLeft()
  107.     turtle.suck(1)
  108.     turtle.turnLeft()
  109.     turtle.drop(1)
  110.     turtle.turnLeft()
  111.     turtle.turnLeft()
  112.     if went_up then
  113.         up()
  114.     end
  115. end
  116.  
  117. local function find_turtles()
  118.     while true do
  119.         sleep(1)
  120.         if is_facing_turtle() then
  121.             print("Found a turtle")
  122.             on_found_turtle()
  123.         end
  124.  
  125.         if went_up then
  126.             went_up = false
  127.             up()
  128.         else
  129.             went_up = true
  130.             down()
  131.         end
  132.     end
  133. end
  134.  
  135. local function init()
  136.     if orient() > -1 then
  137.         find_turtles()
  138.     end
  139. end
  140.  
  141. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement