Advertisement
sanderronde

refuelbot.lua

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