negamartin

Automine

Sep 13th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.71 KB | None | 0 0
  1. do--Minified XT downloader
  2.     local inert=function()end;  --A function that does nothing. Use it to disable prints or read
  3.     local cprint=print;         --Logs an info message
  4.     local aprint=print;         --Prints an asking message, that the user should see
  5.     local cerr=printError;      --Prints an error in red
  6.     local cread=read;           --Receives user input. If it returns "yes", answer is positive
  7.     local xtpath="/xt";         --Where xt should be loaded and downloaded
  8.     local autoupdate=true;      --Wether updates should always be checked, or just when necessary
  9.     local a=false;local b=false;local function c()if http then cprint("Checking for updates...")local d=http.get("http://www.pastebin.com/raw.php?i=BHpUQ8Hn")if d then a=tonumber(d.readLine())b=d.readLine()d.close()if not a or not b then cerr("Update paste corrupted")a=false;b=false end else cerr("Cant check for updates")end else cerr("Cant check for updates, http disabled")end end;local e=false;local f=false;local g=false;local h=false;local function i()if fs.isDir(xtpath)then cprint("\""..xtpath.."\" is a directory, not XT api")e=false;f=true elseif fs.exists(xtpath)then local j=fs.open(xtpath,"r")local k=j.readLine()j.close()if k:sub(1,6)=="--XT V"and k:sub(10)==" by negamartin"and tonumber(k:sub(7,9))then e=tonumber(k:sub(7,9))cprint("\""..xtpath.."\" is XT api V"..e)else e=false;f=true;cprint("\""..xtpath.."\" is a non-XT file")end else cprint("\""..xtpath.."\" does not exist")e=false end;if xt then g=xt.version;print("XT V"..g.." is already loaded")else g=false end;if e then if g then h=math.max(e,g)else h=e end else h=g end end;local function l()if a and b then if http then cprint("Downloading XT V"..a.."...")local m=http.get("http://www.pastebin.com/raw.php?i="..b)if m then local j=fs.open(xtpath,"w")j.write(m.readAll())j.close()m.close()e=a;return true else cerr("Cant download XT")end else cerr("Cant download XT, http disabled")end end;return false end;i()if autoupdate or not h then c()if a then if h then if h<a then if l()then cprint("Updated XT to V"..a)else cprint("Couldn't download XT, using offline version")end else cprint("XT up to date")end else if f then aprint("A non-XT file has been found in \""..xtpath.."\"")aprint("Overwrite it? (\"yes\" or \"no\")")if cread()=="yes"then fs.delete(xtpath)else aprint("Move the non-XT file and try again")error()end end;if l()then cprint("XT V"..a.." downloaded")else cerr("Couldn't download xt")error()end end else if h then cprint("Couldn't update, using offline version")else cerr("Can't download xt, and no offline version")error()end end end;if g then if e and e>g then dofile(xtpath)end else dofile(xtpath)end
  10. end
  11.  
  12. xt.setTurtleUser("automine",false,true);
  13. if xt.shouldReorient() then
  14.     xt.pushForcemove("main",xt.forceDig);
  15.     xt.fup();
  16.     if not xt.orientDigging() then
  17.         print("Could not reorient");
  18.         error();
  19.     end
  20.     xt.fdn();
  21.     xt.popForcemove("main");
  22. end
  23. xt.enableRestoration();
  24.  
  25. function checkNoFuel()
  26.     xt.getFuelLevel();
  27.     if xt.getFuel()==0 then
  28.         for i=1,16 do
  29.             if xt.getItemCount(i)~=0 then
  30.                 xt.sel(i);
  31.                 xt.refuel();
  32.             end
  33.         end
  34.         if xt.getFuel()==0 then
  35.             print("Out of fuel, please add more and press a key");
  36.             repeat
  37.                 os.pullEvent("key");
  38.                 for i=1,16 do
  39.                     if xt.getItemCount(i)~=0 then
  40.                         xt.sel(i);
  41.                         xt.refuel();
  42.                     end
  43.                 end
  44.             until xt.getFuel()~=0;
  45.         end
  46.         xt.sel(1);
  47.     end
  48. end
  49. xt.addForcemove("fuel",1);
  50. xt.pushForcemove("fuel",checkNoFuel);
  51. xt.addForcemove("main",0);
  52. xt.pushForcemove("main",function(dir)
  53.     if not xt.digDir(dir) then
  54.         xt.atkDir(dir);
  55.     end
  56. end);
  57.  
  58. xt.addValue("coverUp",true);
  59. xt.addValue("coverDn",true);
  60. xt.addValue("light",false);
  61. xt.addValue("stopWhenCantLight",false);
  62. xt.addValue("runSetup",true);
  63. xt.addValue("dropStuffUp",true);
  64. if xt.runSetup then
  65.     term.clear();
  66.     term.setCursorPos(1,1);
  67.     print("--Automine setup--");
  68.     print("Do you want to set this position as the home position?");
  69.     print("Type \"yes\" to set it, anything else to not set it, this is true for all other setup questions");
  70.     if read()=="yes" then
  71.         xt.x=0;
  72.         xt.y=0;
  73.         xt.z=0;
  74.         print("Home position set");
  75.     end
  76.     print();
  77.     print("Please enter the direction this turtle is facing");
  78.     print("You can enter north, south, west, east or auto");
  79.     print("\"auto\" will dig blocks, needs fuel, and requires a solid block and a torch or ladder");
  80.     while true do
  81.         local rdir=read();
  82.         if rdir=="auto" then
  83.             xt.fup();
  84.             local ok,cause=xt.orientDigging();
  85.             xt.fdn();
  86.             if ok then break;
  87.             else
  88.                 print("Failed to auto-orient: "..tostring(cause));
  89.             end
  90.         elseif xt.fc[rdir] then
  91.             xt.f=xt.fc[rdir];
  92.             break;
  93.         end
  94.     end
  95.     print("Direction set to "..xt.fc[xt.f]);
  96.     print();
  97.     print("Do you wish to cover liquids above?");
  98.     xt.coverUp=read()=="yes";
  99.     print("Do you wish to cover liquids below?");
  100.     xt.coverDn=read()=="yes";
  101.     print("Do you wish to light the area?");
  102.     local doLight=read()=="yes";
  103.     if doLight then
  104.         xt.sel(1);
  105.         print("Please place the lighting block (eg: torches) in the highlighted slot");
  106.         while true do
  107.             os.pullEvent("turtle_inventory");
  108.             local tab=xt.getItemDetail();
  109.             if tab then
  110.                 print("Do you wish to set "..tab.name.." as the lighting block?");
  111.                 if read()=="yes" then
  112.                     xt.light=tab.name;
  113.                     break;
  114.                 else
  115.                     print("Then drop another block");
  116.                 end
  117.             end
  118.         end
  119.         print("Lighting block set to "..xt.light);
  120.         print("You can now take the lighting block, if you wish");
  121.         print("Do you wish to stop mining when lighting blocks run out?");
  122.         xt.stopWhenCantLight=read()=="yes";
  123.     else xt.light=false; end
  124.     print();
  125.     while true do
  126.         print("Should the turtle drop stuff \"up\" or \"down\"?");
  127.         local ans=read();
  128.         if ans=="up" then
  129.             xt.dropStuffUp=true;
  130.             break;
  131.         elseif ans=="down" then
  132.             xt.dropStuffUp=false;
  133.             break;
  134.         end
  135.     end
  136.     print();
  137.     print("You can rerun this setup by deleting \"/xtdata/automine/runSetup\"");
  138.     sleep(2);
  139.     xt.runSetup=false;
  140. end
  141. xt.addValue("spiralPos",1);
  142. xt.addValue("depositing",false);
  143. xt.addValue("cantContinue",false);
  144. xt.addValue("keepItems",{
  145.     ["minecraft:torch"]=1024,
  146.     ["minecraft:cobblestone"]=64,
  147.     ["minecraft:bucket"]=1,
  148.     ["minecraft:coal"]=32,
  149. });
  150. xt.addValue("liquids",{
  151.     ["minecraft:water"]=true,
  152.     ["minecraft:flowing_water"]=true,
  153.     ["minecraft:lava"]="refuel",
  154.     ["minecraft:flowing_lava"]="refuel",
  155. });
  156. function placeCobble(pfunc)
  157.     for i=1,16 do
  158.         local tab=xt.getItemDetail(i);
  159.         if tab then
  160.             if tab.name=="minecraft:cobblestone" then
  161.                 xt.sel(i);
  162.                 pfunc();
  163.                 xt.sel(1);
  164.                 return true;
  165.             end
  166.         end
  167.     end
  168.     return false;
  169. end
  170. local lastTorchS=false;
  171. local canError=false;
  172. function dig3(dir,ok)
  173.     if ok then
  174.         local minFuel=math.abs(xt.x)+math.abs(xt.z)+4;  --If fuel is just enough to return back home (+4 for safety)
  175.         if xt.getFuel()<=minFuel then
  176.             for i=1,16 do
  177.                 if xt.getItemCount(i)~=0 then
  178.                     xt.sel(i);
  179.                     xt.refuel();
  180.                 end
  181.             end
  182.             xt.sel(1);
  183.             if xt.getFuel()<=minFuel then
  184.                 print("Returning home due to fuel");
  185.                 xt.cantContinue="fuel";
  186.                 xt.spiralPos=xt.spiralPos-1;
  187.                 if canError then error(); end
  188.                 return;
  189.             end
  190.         end
  191.     end
  192.     xt.popPostmove("main");
  193.     if (xt.x==0 and (xt.z==8 or xt.z==-8)) or (xt.z==0 and (xt.x==8 or xt.x==-8)) then  --Water barrier
  194.         placeCobble(xt.placeUp);
  195.         placeCobble(xt.placeDn);
  196.     else
  197.         local goup=xt.up;
  198.         local forcedown=xt.fdn;
  199.         local inspectup=xt.inspectUp;
  200.         local placeup=xt.placeUp;
  201.         local digup=xt.digUp;
  202.         local coverup="coverUp";
  203.         for k=1,2 do
  204.             if not digup() then
  205.                 local tab=inspectup();
  206.                 if tab then
  207.                     if xt.liquids[tab.name] then
  208.                         local bucketSlot=false;
  209.                         if xt.liquids[tab.name]=="refuel" then
  210.                             for i=1,16 do
  211.                                 local tab=xt.getItemDetail(i);
  212.                                 if tab then
  213.                                     if tab.name=="minecraft:bucket" then
  214.                                         xt.sel(i);
  215.                                         placeup();
  216.                                         xt.refuel();
  217.                                         bucketSlot=i;
  218.                                         xt.sel(1);
  219.                                         break;
  220.                                     end
  221.                                 end
  222.                             end
  223.                         end
  224.                         if not bucketSlot or xt[coverup] then
  225.                             if goup() then
  226.                                 if xt[coverup] then
  227.                                     placeCobble(placeup);
  228.                                 end
  229.                                 forcedown();
  230.                             end
  231.                         end
  232.                     else
  233.                         if goup() then
  234.                             xt.liquids[tab.name]=true;
  235.                             xt.save("liquids");
  236.                             if xt[coverup] then
  237.                                 placeCobble(placeup);
  238.                             end
  239.                             forcedown();
  240.                         end
  241.                     end
  242.                 end
  243.             end
  244.             goup=xt.dn;
  245.             forcedown=xt.fup;
  246.             inspectup=xt.inspectDn;
  247.             placeup=xt.placeDn;
  248.             digup=xt.digDn;
  249.             coverup="coverDn";
  250.         end
  251.         if xt.light and xt.x%4==2 and xt.z%4==2 then
  252.             if lastTorchS then
  253.                 local tab=xt.getItemDetail(lastTorchS);
  254.                 if tab then
  255.                     if tab.name~=xt.light then lastTorchS=false; end
  256.                 else lastTorchS=false; end
  257.             end
  258.             if not lastTorchS then
  259.                 for i=1,16 do
  260.                     local tab=xt.getItemDetail(i);
  261.                     if tab then
  262.                         if tab.name==xt.light then
  263.                             lastTorchS=i;
  264.                             break;
  265.                         end
  266.                     end
  267.                 end
  268.             end
  269.             if lastTorchS then
  270.                 xt.sel(lastTorchS);
  271.                 if not xt.placeDn() then
  272.                     xt.fdn();
  273.                     xt.digDn();
  274.                     placeCobble(xt.placeDn);
  275.                     xt.fup();
  276.                     xt.sel(lastTorchS);
  277.                     xt.placeDn();
  278.                 end
  279.                 xt.suckDn();    --In case water destroyed the torch
  280.             elseif xt.stopWhenCantLight then
  281.                 print("Returning home due to lighting");
  282.                 xt.cantContinue="torch";
  283.                 xt.spiralPos=xt.spiralPos-1;
  284.             end
  285.             xt.sel(1);
  286.         end
  287.     end
  288.     xt.pushPostmove("main",dig3);
  289.     if xt.getItemCount(15)~=0 then
  290.         xt.depositing=true;
  291.     end
  292. end
  293. xt.addPostmove("main",0);
  294. xt.pushPostmove("main",dig3);
  295.  
  296. function getSpiralPos(i)
  297.     local square=math.ceil(math.floor(math.sqrt(i))/2);
  298.     local base=(1+(square-1)*2)^2-1;
  299.     local pid=i-base;
  300.     local chunksize=square*2;
  301.     local x;
  302.     local y;
  303.     local side;
  304.     if pid<=chunksize then
  305.         x=square-pid;
  306.         y=-square;
  307.         side="t";--top
  308.     elseif pid<=chunksize*2 then
  309.         x=-square;
  310.         y=-square+(pid-chunksize);
  311.         side="l";--left
  312.     elseif pid<=chunksize*3 then
  313.         x=-square+(pid-chunksize*2);
  314.         y=square;
  315.         side="b";--bottom
  316.     elseif pid<=chunksize*4 then
  317.         x=square;
  318.         y=square-(pid-chunksize*3);
  319.         side="r";--right
  320.     else error("pid is greater than chunksize*4"); end
  321.     return x,y,side;
  322. end
  323. function getBackHome()
  324.     if math.abs(xt.x)>math.abs(xt.z) then
  325.         xt.fgotoX(xt.x>0 and xt.x-1 or xt.x+1);
  326.         xt.fgotoZ(0);
  327.         xt.fgotoX(0);
  328.     else
  329.         xt.fgotoZ(xt.z>0 and xt.z-1 or xt.z+1);
  330.         xt.fgotoX(0);
  331.         xt.fgotoZ(0);
  332.     end
  333. end
  334.  
  335. xt.sel(1);
  336. function mainLoop()
  337.     while true do
  338.         if xt.depositing then
  339.             print("Depositing items");
  340.             xt.popPostmove("main");
  341.             getBackHome();
  342.             local dfunc;
  343.             if xt.dropStuffUp then
  344.                 xt.fgotoY(2);
  345.                 dfunc=xt.dropUp;
  346.             else
  347.                 xt.fgotoY(0);
  348.                 dfunc=xt.dropDn;
  349.             end
  350.             local kept={};
  351.             for i=1,16 do
  352.                 local tab=xt.getItemDetail(i);
  353.                 if tab then
  354.                     local keep=xt.keepItems[tab.name]
  355.                     if keep then
  356.                         local itemskept=kept[tab.name] or 0;
  357.                         if keep>itemskept then
  358.                             local tokeep=keep-itemskept;
  359.                             xt.sel(i);
  360.                             if tokeep<tab.count then
  361.                                 dfunc(tab.count-tokeep);
  362.                             else
  363.                                 tokeep=tab.count;
  364.                             end
  365.                             for j=1,16 do
  366.                                 if xt.transferTo(j) then break; end
  367.                             end
  368.                             kept[tab.name]=itemskept+tokeep;
  369.                         else
  370.                             xt.sel(i);
  371.                             dfunc();
  372.                         end
  373.                     else
  374.                         xt.sel(i);
  375.                         dfunc();
  376.                     end
  377.                 end
  378.             end
  379.             xt.sel(1);
  380.             xt.pushPostmove("main",dig3);
  381.             xt.depositing=false;
  382.         elseif xt.cantContinue then
  383.             xt.popPostmove("main");
  384.             getBackHome();
  385.             xt.gotoY(0);
  386.             if xt.cantContinue=="torch" then
  387.                 print("---Run out of lighting blocks---");
  388.                 print("Turtle is configured to stop mining when run out of lighting blocks");
  389.                 print("Please drop some in the turtle's inventory");
  390.                 while true do
  391.                     for i=1,16 do
  392.                         local tab=xt.getItemDetail(i);
  393.                         if tab then
  394.                             if tab.name==xt.light then
  395.                                 print("Found some lighting blocks, resuming");
  396.                                 xt.cantContinue=false;
  397.                                 break;
  398.                             end
  399.                         end
  400.                     end
  401.                     if not xt.cantContinue then break; end
  402.                     os.pullEvent("turtle_inventory");
  403.                 end
  404.             elseif xt.cantContinue=="fuel" then
  405.                 print("---Run out of fuel---");
  406.                 print("Turtle returned home when it was getting low on fuel");
  407.                 print("Please drop in enough fuel");
  408.                 local sx,sy=getSpiralPos(xt.spiralPos);
  409.                 local lastFuel=false;
  410.                 local fuelNeeded=math.abs(sx)+math.abs(sy)+4;
  411.                 while true do
  412.                     if lastFuel~=fuelNeeded-xt.getFuel() then
  413.                         lastFuel=fuelNeeded-xt.getFuel();
  414.                         if lastFuel<=0 then break; end
  415.                         print("Turtle needs atleast "..lastFuel.." fuel ("..(math.ceil(lastFuel/8)*0.1).." coals)");
  416.                     end
  417.                     os.pullEvent("turtle_inventory");
  418.                     for i=1,16 do
  419.                         if xt.getItemCount(i)~=0 then
  420.                             xt.sel(i);
  421.                             xt.refuel();
  422.                         end
  423.                     end
  424.                     sleep(0);
  425.                 end
  426.                 print("Continuing mining...");
  427.                 xt.cantContinue=false;
  428.             else
  429.                 print("Turtle has stopped mining for an unknown reason");
  430.                 print("Waiting for it to resolve...");
  431.                 while xt.cantContinue do sleep(5); end
  432.             end
  433.             xt.pushPostmove("main",dig3);
  434.         else
  435.             xt.fgotoY(1);
  436.             local x,z,s=getSpiralPos(xt.spiralPos);
  437.             local ok=false;
  438.             if s=="t" or s=="b" then
  439.                 canError=true;
  440.                 if pcall(xt.gotoZ,z) then
  441.                     if pcall(xt.fgotoX,x) then
  442.                         ok=pcall(xt.fgotoZ,z);
  443.                     end
  444.                 end
  445.                 canError=false;
  446.             else
  447.                 canError=true;
  448.                 if pcall(xt.gotoX,x) then
  449.                     if pcall(xt.fgotoZ,z) then
  450.                         ok=pcall(xt.fgotoX,x);
  451.                     end
  452.                 end
  453.                 canError=false;
  454.             end
  455.             if ok then xt.spiralPos=xt.spiralPos+1; end
  456.         end
  457.     end
  458. end
  459. function eventListener()
  460.     while true do
  461.         local ev={os.pullEvent()};
  462.         if ev[1]=="key" then
  463.             if ev[2]==keys.q then return; end
  464.         end
  465.     end
  466. end
  467. parallel.waitForAny(mainLoop,eventListener);
  468.  
  469. xt.popForcemove("main");
  470. xt.popPremove("main");
  471. xt.disableRestoration();
Advertisement
Add Comment
Please, Sign In to add comment