Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do--Minified XT downloader
- local inert=function()end; --A function that does nothing. Use it to disable prints or read
- local cprint=print; --Logs an info message
- local aprint=print; --Prints an asking message, that the user should see
- local cerr=printError; --Prints an error in red
- local cread=read; --Receives user input. If it returns "yes", answer is positive
- local xtpath="/xt"; --Where xt should be loaded and downloaded
- local autoupdate=true; --Wether updates should always be checked, or just when necessary
- 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
- end
- xt.setTurtleUser("automine",false,true);
- if xt.shouldReorient() then
- xt.pushForcemove("main",xt.forceDig);
- xt.fup();
- if not xt.orientDigging() then
- print("Could not reorient");
- error();
- end
- xt.fdn();
- xt.popForcemove("main");
- end
- xt.enableRestoration();
- function checkNoFuel()
- xt.getFuelLevel();
- if xt.getFuel()==0 then
- for i=1,16 do
- if xt.getItemCount(i)~=0 then
- xt.sel(i);
- xt.refuel();
- end
- end
- if xt.getFuel()==0 then
- print("Out of fuel, please add more and press a key");
- repeat
- os.pullEvent("key");
- for i=1,16 do
- if xt.getItemCount(i)~=0 then
- xt.sel(i);
- xt.refuel();
- end
- end
- until xt.getFuel()~=0;
- end
- xt.sel(1);
- end
- end
- xt.addForcemove("fuel",1);
- xt.pushForcemove("fuel",checkNoFuel);
- xt.addForcemove("main",0);
- xt.pushForcemove("main",function(dir)
- if not xt.digDir(dir) then
- xt.atkDir(dir);
- end
- end);
- xt.addValue("coverUp",true);
- xt.addValue("coverDn",true);
- xt.addValue("light",false);
- xt.addValue("stopWhenCantLight",false);
- xt.addValue("runSetup",true);
- xt.addValue("dropStuffUp",true);
- if xt.runSetup then
- term.clear();
- term.setCursorPos(1,1);
- print("--Automine setup--");
- print("Do you want to set this position as the home position?");
- print("Type \"yes\" to set it, anything else to not set it, this is true for all other setup questions");
- if read()=="yes" then
- xt.x=0;
- xt.y=0;
- xt.z=0;
- print("Home position set");
- end
- print();
- print("Please enter the direction this turtle is facing");
- print("You can enter north, south, west, east or auto");
- print("\"auto\" will dig blocks, needs fuel, and requires a solid block and a torch or ladder");
- while true do
- local rdir=read();
- if rdir=="auto" then
- xt.fup();
- local ok,cause=xt.orientDigging();
- xt.fdn();
- if ok then break;
- else
- print("Failed to auto-orient: "..tostring(cause));
- end
- elseif xt.fc[rdir] then
- xt.f=xt.fc[rdir];
- break;
- end
- end
- print("Direction set to "..xt.fc[xt.f]);
- print();
- print("Do you wish to cover liquids above?");
- xt.coverUp=read()=="yes";
- print("Do you wish to cover liquids below?");
- xt.coverDn=read()=="yes";
- print("Do you wish to light the area?");
- local doLight=read()=="yes";
- if doLight then
- xt.sel(1);
- print("Please place the lighting block (eg: torches) in the highlighted slot");
- while true do
- os.pullEvent("turtle_inventory");
- local tab=xt.getItemDetail();
- if tab then
- print("Do you wish to set "..tab.name.." as the lighting block?");
- if read()=="yes" then
- xt.light=tab.name;
- break;
- else
- print("Then drop another block");
- end
- end
- end
- print("Lighting block set to "..xt.light);
- print("You can now take the lighting block, if you wish");
- print("Do you wish to stop mining when lighting blocks run out?");
- xt.stopWhenCantLight=read()=="yes";
- else xt.light=false; end
- print();
- while true do
- print("Should the turtle drop stuff \"up\" or \"down\"?");
- local ans=read();
- if ans=="up" then
- xt.dropStuffUp=true;
- break;
- elseif ans=="down" then
- xt.dropStuffUp=false;
- break;
- end
- end
- print();
- print("You can rerun this setup by deleting \"/xtdata/automine/runSetup\"");
- sleep(2);
- xt.runSetup=false;
- end
- xt.addValue("spiralPos",1);
- xt.addValue("depositing",false);
- xt.addValue("cantContinue",false);
- xt.addValue("keepItems",{
- ["minecraft:torch"]=1024,
- ["minecraft:cobblestone"]=64,
- ["minecraft:bucket"]=1,
- ["minecraft:coal"]=32,
- });
- xt.addValue("liquids",{
- ["minecraft:water"]=true,
- ["minecraft:flowing_water"]=true,
- ["minecraft:lava"]="refuel",
- ["minecraft:flowing_lava"]="refuel",
- });
- function placeCobble(pfunc)
- for i=1,16 do
- local tab=xt.getItemDetail(i);
- if tab then
- if tab.name=="minecraft:cobblestone" then
- xt.sel(i);
- pfunc();
- xt.sel(1);
- return true;
- end
- end
- end
- return false;
- end
- local lastTorchS=false;
- local canError=false;
- function dig3(dir,ok)
- if ok then
- local minFuel=math.abs(xt.x)+math.abs(xt.z)+4; --If fuel is just enough to return back home (+4 for safety)
- if xt.getFuel()<=minFuel then
- for i=1,16 do
- if xt.getItemCount(i)~=0 then
- xt.sel(i);
- xt.refuel();
- end
- end
- xt.sel(1);
- if xt.getFuel()<=minFuel then
- print("Returning home due to fuel");
- xt.cantContinue="fuel";
- xt.spiralPos=xt.spiralPos-1;
- if canError then error(); end
- return;
- end
- end
- end
- xt.popPostmove("main");
- 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
- placeCobble(xt.placeUp);
- placeCobble(xt.placeDn);
- else
- local goup=xt.up;
- local forcedown=xt.fdn;
- local inspectup=xt.inspectUp;
- local placeup=xt.placeUp;
- local digup=xt.digUp;
- local coverup="coverUp";
- for k=1,2 do
- if not digup() then
- local tab=inspectup();
- if tab then
- if xt.liquids[tab.name] then
- local bucketSlot=false;
- if xt.liquids[tab.name]=="refuel" then
- for i=1,16 do
- local tab=xt.getItemDetail(i);
- if tab then
- if tab.name=="minecraft:bucket" then
- xt.sel(i);
- placeup();
- xt.refuel();
- bucketSlot=i;
- xt.sel(1);
- break;
- end
- end
- end
- end
- if not bucketSlot or xt[coverup] then
- if goup() then
- if xt[coverup] then
- placeCobble(placeup);
- end
- forcedown();
- end
- end
- else
- if goup() then
- xt.liquids[tab.name]=true;
- xt.save("liquids");
- if xt[coverup] then
- placeCobble(placeup);
- end
- forcedown();
- end
- end
- end
- end
- goup=xt.dn;
- forcedown=xt.fup;
- inspectup=xt.inspectDn;
- placeup=xt.placeDn;
- digup=xt.digDn;
- coverup="coverDn";
- end
- if xt.light and xt.x%4==2 and xt.z%4==2 then
- if lastTorchS then
- local tab=xt.getItemDetail(lastTorchS);
- if tab then
- if tab.name~=xt.light then lastTorchS=false; end
- else lastTorchS=false; end
- end
- if not lastTorchS then
- for i=1,16 do
- local tab=xt.getItemDetail(i);
- if tab then
- if tab.name==xt.light then
- lastTorchS=i;
- break;
- end
- end
- end
- end
- if lastTorchS then
- xt.sel(lastTorchS);
- if not xt.placeDn() then
- xt.fdn();
- xt.digDn();
- placeCobble(xt.placeDn);
- xt.fup();
- xt.sel(lastTorchS);
- xt.placeDn();
- end
- xt.suckDn(); --In case water destroyed the torch
- elseif xt.stopWhenCantLight then
- print("Returning home due to lighting");
- xt.cantContinue="torch";
- xt.spiralPos=xt.spiralPos-1;
- end
- xt.sel(1);
- end
- end
- xt.pushPostmove("main",dig3);
- if xt.getItemCount(15)~=0 then
- xt.depositing=true;
- end
- end
- xt.addPostmove("main",0);
- xt.pushPostmove("main",dig3);
- function getSpiralPos(i)
- local square=math.ceil(math.floor(math.sqrt(i))/2);
- local base=(1+(square-1)*2)^2-1;
- local pid=i-base;
- local chunksize=square*2;
- local x;
- local y;
- local side;
- if pid<=chunksize then
- x=square-pid;
- y=-square;
- side="t";--top
- elseif pid<=chunksize*2 then
- x=-square;
- y=-square+(pid-chunksize);
- side="l";--left
- elseif pid<=chunksize*3 then
- x=-square+(pid-chunksize*2);
- y=square;
- side="b";--bottom
- elseif pid<=chunksize*4 then
- x=square;
- y=square-(pid-chunksize*3);
- side="r";--right
- else error("pid is greater than chunksize*4"); end
- return x,y,side;
- end
- function getBackHome()
- if math.abs(xt.x)>math.abs(xt.z) then
- xt.fgotoX(xt.x>0 and xt.x-1 or xt.x+1);
- xt.fgotoZ(0);
- xt.fgotoX(0);
- else
- xt.fgotoZ(xt.z>0 and xt.z-1 or xt.z+1);
- xt.fgotoX(0);
- xt.fgotoZ(0);
- end
- end
- xt.sel(1);
- function mainLoop()
- while true do
- if xt.depositing then
- print("Depositing items");
- xt.popPostmove("main");
- getBackHome();
- local dfunc;
- if xt.dropStuffUp then
- xt.fgotoY(2);
- dfunc=xt.dropUp;
- else
- xt.fgotoY(0);
- dfunc=xt.dropDn;
- end
- local kept={};
- for i=1,16 do
- local tab=xt.getItemDetail(i);
- if tab then
- local keep=xt.keepItems[tab.name]
- if keep then
- local itemskept=kept[tab.name] or 0;
- if keep>itemskept then
- local tokeep=keep-itemskept;
- xt.sel(i);
- if tokeep<tab.count then
- dfunc(tab.count-tokeep);
- else
- tokeep=tab.count;
- end
- for j=1,16 do
- if xt.transferTo(j) then break; end
- end
- kept[tab.name]=itemskept+tokeep;
- else
- xt.sel(i);
- dfunc();
- end
- else
- xt.sel(i);
- dfunc();
- end
- end
- end
- xt.sel(1);
- xt.pushPostmove("main",dig3);
- xt.depositing=false;
- elseif xt.cantContinue then
- xt.popPostmove("main");
- getBackHome();
- xt.gotoY(0);
- if xt.cantContinue=="torch" then
- print("---Run out of lighting blocks---");
- print("Turtle is configured to stop mining when run out of lighting blocks");
- print("Please drop some in the turtle's inventory");
- while true do
- for i=1,16 do
- local tab=xt.getItemDetail(i);
- if tab then
- if tab.name==xt.light then
- print("Found some lighting blocks, resuming");
- xt.cantContinue=false;
- break;
- end
- end
- end
- if not xt.cantContinue then break; end
- os.pullEvent("turtle_inventory");
- end
- elseif xt.cantContinue=="fuel" then
- print("---Run out of fuel---");
- print("Turtle returned home when it was getting low on fuel");
- print("Please drop in enough fuel");
- local sx,sy=getSpiralPos(xt.spiralPos);
- local lastFuel=false;
- local fuelNeeded=math.abs(sx)+math.abs(sy)+4;
- while true do
- if lastFuel~=fuelNeeded-xt.getFuel() then
- lastFuel=fuelNeeded-xt.getFuel();
- if lastFuel<=0 then break; end
- print("Turtle needs atleast "..lastFuel.." fuel ("..(math.ceil(lastFuel/8)*0.1).." coals)");
- end
- os.pullEvent("turtle_inventory");
- for i=1,16 do
- if xt.getItemCount(i)~=0 then
- xt.sel(i);
- xt.refuel();
- end
- end
- sleep(0);
- end
- print("Continuing mining...");
- xt.cantContinue=false;
- else
- print("Turtle has stopped mining for an unknown reason");
- print("Waiting for it to resolve...");
- while xt.cantContinue do sleep(5); end
- end
- xt.pushPostmove("main",dig3);
- else
- xt.fgotoY(1);
- local x,z,s=getSpiralPos(xt.spiralPos);
- local ok=false;
- if s=="t" or s=="b" then
- canError=true;
- if pcall(xt.gotoZ,z) then
- if pcall(xt.fgotoX,x) then
- ok=pcall(xt.fgotoZ,z);
- end
- end
- canError=false;
- else
- canError=true;
- if pcall(xt.gotoX,x) then
- if pcall(xt.fgotoZ,z) then
- ok=pcall(xt.fgotoX,x);
- end
- end
- canError=false;
- end
- if ok then xt.spiralPos=xt.spiralPos+1; end
- end
- end
- end
- function eventListener()
- while true do
- local ev={os.pullEvent()};
- if ev[1]=="key" then
- if ev[2]==keys.q then return; end
- end
- end
- end
- parallel.waitForAny(mainLoop,eventListener);
- xt.popForcemove("main");
- xt.popPremove("main");
- xt.disableRestoration();
Advertisement
Add Comment
Please, Sign In to add comment