Advertisement
sanderronde

refuelbot.lua

Jan 5th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. local CHEST_NAME = "EnderStorage:enderChest"
  2. local y = 0
  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(total)
  43.     if turtle.getFuelLevel() < (total *  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.     for h = 1, total do
  58.         turtle.up()
  59.     end
  60. end
  61.  
  62. local function down()
  63.     turtle.down()
  64. end
  65.  
  66. local function go_all_the_way_down()
  67.     for h = 1, y do
  68.         down()
  69.     end
  70.     local prev_y = y
  71.     y = 0
  72.     return prev_y
  73. end
  74.  
  75. local function get_bucket()
  76.     local amount_down = go_all_the_way_down()
  77.     while true do
  78.         turtle.select(1)
  79.         turtle.suck(1)
  80.         if turtle.getItemCount() > 0 then
  81.             up(amount_down)
  82.             return
  83.         end
  84.         sleep(1)
  85.     end
  86. end
  87.  
  88. local function on_found_turtle()
  89.     sleep(60)
  90.     if not is_facing_turtle() then
  91.         print("Turtle not there anymore")
  92.         return
  93.     else
  94.         print("Turtle is still there")
  95.     end
  96.  
  97.     -- Get rid of the old one
  98.     turtle.suck(1)
  99.     turtle.turnLeft()
  100.     turtle.drop(1)
  101.     turtle.turnLeft()
  102.  
  103.     -- Grab the new one
  104.     get_bucket()
  105.     turtle.turnLeft()
  106.     turtle.turnLeft()
  107.     turtle.drop(1)
  108.  
  109.     -- Put the old one in the chest
  110.     local amount_down = go_all_the_way_down()
  111.     turtle.turnLeft()
  112.     turtle.suck(1)
  113.     turtle.turnLeft()
  114.     turtle.drop(1)
  115.     turtle.turnLeft()
  116.     turtle.turnLeft()
  117.     up(amount_down)
  118. end
  119.  
  120. local function find_turtles()
  121.     while true do
  122.         sleep(1)
  123.         if is_facing_turtle() then
  124.             print("Found a turtle")
  125.             on_found_turtle()
  126.         end
  127.  
  128.         print("Y = ", y)
  129.         if y < 2 then
  130.             y = y + 1
  131.             up(1)
  132.         else
  133.             for l = 1, y do
  134.                 down()
  135.             end
  136.             y = 0
  137.         end
  138.     end
  139. end
  140.  
  141. local function init()
  142.     if orient() > -1 then
  143.         find_turtles()
  144.     end
  145. end
  146.  
  147. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement