Advertisement
Terrah

filler turtle

Feb 23rd, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. --dofile("api.lua");
  2.  
  3. local chesttname = "minecraft:chest";
  4.  
  5. local pos = {};
  6.  
  7. local function RefuelIfNessesary()
  8.  
  9.     local level = turtle.getFuelLevel();
  10.     if level=="unlimited" then
  11.         return true;
  12.     elseif level < 1 then
  13.         turtle.select(1);
  14.         if not turtle.refuel() then
  15.             return false, "unable to refuel, slot 1 not fuel or empty";
  16.         end
  17.     else
  18.         return true;
  19.     end
  20. end
  21.  
  22. local function IsFacingBaseChest()
  23.  
  24.     local item = turtle.inspect();
  25.     return type(item)=="table" and item.name==chesttname;
  26. end
  27.  
  28. --Must be facing the chest for this to work
  29. local function GetFuelFromChest()
  30.  
  31.     turtle.select(1);
  32.  
  33.     if turtle.getItemCount() >= 64 then
  34.         return true;
  35.     elseif not IsFacingBaseChest() then
  36.         return false, "Not facing base chest";
  37.     elseif not turtle.suck(64) then
  38.         return false, "base chest is empty";
  39.     else
  40.         return true;
  41.     end
  42. end
  43.  
  44. local function Calibrate()
  45.  
  46.     local foundchest = false;
  47.  
  48.     for n=1,4 do
  49.  
  50.         if IsFacingBaseChest() then
  51.             foundchest = true;
  52.             break;
  53.         end
  54.  
  55.         turtle.turnRight();
  56.     end
  57.  
  58.     if not foundchest then
  59.         return false,"base chest not found, please place me next to the base chest";
  60.     end
  61.  
  62.     --We're facing our base chest here
  63.  
  64.     local ok,msg = GetFuelFromChest();
  65.     if not ok then
  66.         return false,msg;
  67.     elseif RefuelIfNessesary() then
  68.         GetFuelFromChest(); -- get more
  69.     end
  70.  
  71.     turtle.turnRight();
  72.  
  73.     local steps = 0;
  74.     local blockf = turtle.inspect();
  75.     local blockd = turtle.inspectDown();
  76.  
  77.     while not blockf and blockd do
  78.         if turtle.forward() then
  79.             steps = steps + 1;
  80.             blockf = turtle.inspect();
  81.             blockd = turtle.inspectDown();
  82.         else
  83.             return false,"Cannot move forwards (no fuel?)";
  84.         end
  85.     end
  86.  
  87.     pos.base = {x=steps,y=0,z=0};
  88.  
  89.     print("base is at X offset: "..tostring(steps));
  90.  
  91.     --we're now at _our_ 0,0,0 facing forwards
  92.     --now turn and travel down the y axis
  93.  
  94.     turtle.turnRight();
  95.  
  96.     steps = 0;
  97.     blockf = turtle.inspect();
  98.     blockd = turtle.inspectDown();
  99.  
  100.     while not blockf and blockd do
  101.         if turtle.forward() then
  102.             steps = steps + 1;
  103.             blockf = turtle.inspect();
  104.             blockd = turtle.inspectDown();
  105.         else
  106.             return false,"Cannot move forwards (no fuel?)";
  107.         end
  108.     end
  109.  
  110.     local yMax = steps;
  111.     print("Y Max: "..tostring(yMax));
  112.     --now walk back
  113.     turtle.turnRight();
  114.     turtle.turnRight();
  115.  
  116.     for n=1,yMax do
  117.         if not turtle.forward() then
  118.             return false,"Cannot move forwards (no fuel?)";
  119.         end
  120.     end
  121.  
  122.     --Now we're at our 0,0,0 (in a courner)
  123.     turtle.turnLeft();
  124.  
  125.     steps = 0;
  126.     blockf = turtle.inspect();
  127.     blockd = turtle.inspectDown();
  128.  
  129.     while not blockf and blockd do
  130.         if turtle.forward() then
  131.             steps = steps + 1;
  132.             blockf = turtle.inspect();
  133.             blockd = turtle.inspectDown();
  134.         else
  135.             return false,"Cannot move forwards (no fuel?)";
  136.         end
  137.     end
  138.  
  139.     --Now we've travled down x
  140.     local xMax = steps;
  141.     pos.grid = {x=xMax,y=yMax};
  142.  
  143.     print("X Max: "..tostring(xMax));
  144.  
  145.     --now walk back
  146.     turtle.turnRight();
  147.     turtle.turnRight();
  148.  
  149.     for n=1,yMax do
  150.         if not turtle.forward() then
  151.             return false,"Cannot move forwards (no fuel?)";
  152.         end
  153.     end
  154.  
  155.     print("I'm now home");
  156.  
  157.     pos.pos = {x=0,y=0,z=0,face="right"};
  158.  
  159.     return true;
  160. end
  161.  
  162. local function Up()
  163.  
  164. end
  165.  
  166. local function Down()
  167.  
  168. end
  169.  
  170. local function Right()
  171.  
  172. end
  173.  
  174. local function Left()
  175.  
  176. end
  177.  
  178. local ok,msg=Calibrate();
  179.  
  180. print(ok,msg);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement