Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --XT V1.6 by NEGAMARTIN
- version=1.6;
- --MODEM PART
- --Connects a modem if it's attached
- --wlr: If attach only wireless. Leave empty to attach any of them both
- --Modem is accessible at xt.modem. Returns side modem was found.
- modem=nil;
- function connectModem(wlr)
- local names=peripheral.getNames();
- local modemSide;
- local any=false;
- if wlr==nil then
- any=true;
- end
- for _,name in ipairs(names) do
- if peripheral.getType(name)=="modem" then
- if any or peripheral.call(name,"isWireless")==wlr then
- xt.modem=peripheral.wrap(name);
- modemSide=name;
- break;
- end
- end
- end
- return modemSide;
- end
- --SAVING PART
- custom={};
- local err=false;
- err=pcall(function()
- if fs.exists("xtdata") then
- if not fs.isDir("xtdata") then
- fs.move("xtdata","copyofxtdata");
- fs.makeDir("xtdata");
- end
- else
- fs.makeDir("xtdata");
- end
- end);
- ok,en=pcall(function()
- if not fs.exists("xtdata/custom") then
- fs.makeDir("xtdata/custom");
- end
- end);if not ok then print(en); end
- ok,en=pcall(function()
- for _,name in ipairs(fs.list("xtdata/custom")) do
- local file=fs.open("xtdata/custom/"..name,"r");
- local word=file.readLine();
- local value=file.readLine();
- local t=file.readLine();
- if t=="number" then value=tonumber(value); end
- if t=="table" then value=textutils.unserialize(value); end
- custom[word]=value;
- file.close();
- end
- end);if not ok then print(en); end
- function gc(word)
- return xt.custom[word];
- end
- function sc(word,value)
- xt.custom[word]=value;
- if value==nil then
- if fs.exists("xtdata/custom/"..word) then
- fs.delete("xtdata/custom/"..word);
- end
- else
- local file=fs.open("xtdata/custom/"..word,"w");
- local t=type(value);
- if type(value)=="table" then
- local temp=textutils.serialize(value);
- value=string.gsub(temp,"\n","");
- --print(value);
- end
- file.writeLine(word);
- file.writeLine(value);
- file.writeLine(t);
- file.close();
- end
- end
- --TURTLE PART
- if turtle then
- sleep(0.1); --Sometimes the select gets stuck with no delay TAKE IT AWAY AT YOUR OWN RISK
- x=0;
- y=0;
- z=0;
- f=0;
- s=1;
- turtle.select(1);
- handleBlocking=function() sleep(0.4); end
- fuelUpdated=false;
- if autoSave==nil then autoSave=true; end
- if replaceTurtle==nil then replaceTurtle=true; end
- local err=false;
- err=pcall(function()
- if fs.exists("xtdata") then
- if not fs.isDir("xtdata") then
- fs.move("xtdata","copyofxtdata");
- fs.makeDir("xtdata");
- end
- else
- fs.makeDir("xtdata");
- end
- end);
- err=err or pcall(function()
- if fs.exists("xtdata/xz") then
- local file=fs.open("xtdata/xz","r");
- x=math.floor(file.readLine());
- z=math.floor(file.readLine());
- file.close();
- end
- end);
- err=err or pcall(function()
- if fs.exists("xtdata/y") then
- local file=fs.open("xtdata/y","r");
- y=math.floor(file.readLine());
- file.close();
- end
- end);
- err=err or pcall(function()
- if fs.exists("xtdata/f") then
- local file=fs.open("xtdata/f","r");
- f=math.floor(file.readLine());
- file.close();
- end
- end);
- err=err or pcall(function()
- if fs.exists("xtdata/s") then
- local file=fs.open("xtdata/s","r");
- s=math.floor(file.readLine());
- turtle.select(s);
- file.close();
- end
- end);
- err=err or pcall(function()
- if fs.exists("xtdata/autosave") then
- local file=fs.open("xtdata/autosave","r");
- local tas=math.floor(file.readLine());
- if tas==1 then autoSave=true;
- elseif tas==0 then autoSave=false; end
- file.close();
- end
- end);
- function saveXZ()
- if not autoSave then return; end
- local file=fs.open("xtdata/xz","w");
- file.writeLine(xt.x);
- file.writeLine(xt.z);
- file.close();
- end
- function saveY()
- if not autoSave then return; end
- local file=fs.open("xtdata/y","w");
- file.writeLine(xt.y);
- file.close();
- end
- function saveF()
- if not autoSave then return; end
- local file=fs.open("xtdata/f","w");
- file.writeLine(xt.f);
- file.close();
- end
- function saveS()
- if not autoSave then return; end
- local file=fs.open("xtdata/s","w");
- file.writeLine(xt.s);
- file.close();
- end
- function savePos()
- if not autoSave then return; end
- saveXZ();
- saveY();
- saveF();
- end
- function save()
- if not autoSave then return; end
- saveXZ();
- saveY();
- saveF();
- saveS();
- end
- function setAutoSave(nas)
- autoSave=nas;
- local file=fs.open("xtdata/autosave","w");
- local tempAS;
- if autoSave then tempAS=1; else tempAS=0; end
- file.writeLine(tempAS);
- file.close();
- end
- function getFX(f)
- f=f or xt.f;
- if f==1 then return -1;
- elseif f==3 then return 1;
- else return 0; end
- end
- function getFZ(f)
- f=f or xt.f;
- if f==0 then return 1;
- elseif f==2 then return -1;
- else return 0; end
- end
- function fw(amount)
- if amount==nil then amount=1; end
- if amount<0 then bk(-amount); end
- for i=1,amount do
- if turtle.orig.forward() then
- xt.x=xt.x+getFX();
- xt.z=xt.z+getFZ();
- xt.saveXZ();
- fuelUpdated=false;
- else
- return false;
- end
- end
- return true;
- end
- function ffw(amount,...)
- if amount==nil then amount=1; end
- if amount<0 then bk(-amount); end
- for i=1,amount do
- while not turtle.orig.forward() do
- xt.handleBlocking("fw",...);
- end
- xt.x=xt.x+getFX();
- xt.z=xt.z+getFZ();
- xt.saveXZ();
- fuelUpdated=false;
- end
- return true;
- end
- function bk(amount)
- if amount==nil then amount=1; end
- if amount<0 then fw(-amount); end
- for i=1,amount do
- if turtle.orig.back() then
- xt.x=xt.x-getFX();
- xt.z=xt.z-getFZ();
- xt.saveXZ();
- fuelUpdated=false;
- else
- return false;
- end
- end
- return true;
- end
- function fbk(amount,...)
- if amount==nil then amount=1; end
- if amount<0 then fw(-amount); end
- for i=1,amount do
- while not turtle.orig.back() do
- xt.handleBlocking("bk",...);
- end
- xt.x=xt.x-getFX();
- xt.z=xt.z-getFZ();
- xt.saveXZ();
- fuelUpdated=false;
- end
- return true;
- end
- function up(amount)
- if amount==nil then amount=1; end
- if amount<0 then dn(-amount); end
- for i=1,amount do
- if turtle.orig.up() then
- xt.y=xt.y+1;
- xt.saveY();
- fuelUpdated=false;
- else
- return false;
- end
- end
- return true;
- end
- function fup(amount,...)
- if amount==nil then amount=1; end
- if amount<0 then dn(-amount); end
- for i=1,amount do
- while not turtle.orig.up() do
- xt.handleBlocking("up",...);
- end
- xt.y=xt.y+1;
- xt.saveY();
- fuelUpdated=false;
- end
- return true;
- end
- function dn(amount)
- if amount==nil then amount=1; end
- if amount<0 then up(-amount); end
- for i=1,amount do
- if turtle.orig.down() then
- xt.y=xt.y-1;
- xt.saveY();
- fuelUpdated=false;
- else
- return false;
- end
- end
- return true;
- end
- function fdn(amount,...)
- if amount==nil then amount=1; end
- if amount<0 then up(-amount); end
- for i=1,amount do
- while not turtle.orig.down() do
- xt.handleBlocking("dn",...);
- end
- xt.y=xt.y-1;
- xt.saveY();
- fuelUpdated=false;
- end
- return true;
- end
- function left(amount)
- if amount==nil then amount=1; end
- if amount<0 then right(-amount); end
- for i=1,amount do
- turtle.orig.turnLeft();
- xt.f=(xt.f-1)%4;
- xt.saveF();
- end
- return true;
- end
- function right(amount)
- if amount==nil then amount=1; end
- if amount<0 then left(-amount); end
- for i=1,amount do
- turtle.orig.turnRight();
- xt.f=(xt.f+1)%4;
- xt.saveF();
- end
- return true;
- end
- function sel(slot)
- if slot~=xt.s then
- if turtle.orig.select(slot) then
- xt.s=slot;
- xt.saveS();
- end
- end
- return true;
- end
- function inspect()
- return ({turtle.orig.inspect()})[2];
- end
- function inspectUp()
- return ({turtle.orig.inspectUp()})[2];
- end
- function inspectDn()
- return ({turtle.orig.inspectDown()})[2];
- end
- function getFuel()
- if not fuelUpdated then fuelUpdated=xt.getFuelLevel(); end
- return fuelUpdated;
- end
- fc={
- ["south"]=0,
- ["west"]=1,
- ["north"]=2,
- ["east"]=3,
- [0]="south",
- [1]="west",
- [2]="north",
- [3]="east",
- ["zplus"]=0,
- ["xminus"]=1,
- ["zminus"]=2,
- ["xplus"]=3,
- };
- function invertFace(facing)
- return (facing+2)%4;
- end
- function face(nf)
- if nf==nil then return false,"Not enough arguments"; end
- local fdif=nf-xt.f;
- if fdif==0 then return true;
- elseif fdif==3 then left();return true;
- elseif fdif==-3 then right(); return true;
- else right(fdif);return true; end
- end
- function gotoX(nx)
- if nx>xt.x+0.1 then face(3) elseif nx<xt.x-0.1 then face(1); else return true; end
- local dif=math.abs(nx-xt.x);
- return xt.fw(dif);
- end
- function gotoZ(nz)
- if nz>xt.z+0.1 then face(0) elseif nz<xt.z-0.1 then face(2); else return true; end
- local dif=math.abs(nz-xt.z);
- return xt.fw(dif);
- end
- function gotoXZ(nx,nz)
- local fx=getFX();
- local fz=getFZ();
- if (fz==1 and nz>xt.z) or (fz==-1 and nz<xt.z) or (fx==1 and nx<xt.x) or (fx==-1 and nx>xt.x) then
- gotoZ(nz);
- gotoX(nx);
- else
- gotoX(nx);
- gotoZ(nz);
- end
- end
- function gotoY(ny)
- local func;
- if ny>xt.y then func=xt.up; elseif ny<xt.y then func=xt.dn; else return true; end
- local dif=math.abs(ny-xt.y);
- return func(dif);
- end
- function gotoXZY(nx,ny,nz)
- gotoXZ(nx,nz);
- gotoY(ny);
- end
- function gotoYXZ(nx,ny,nz)
- gotoY(ny);
- gotoXZ(nx,nz);
- end
- function gotoXDig(nx)
- if nx>xt.x+0.1 then face(3) elseif nx<xt.x-0.1 then face(1); else return true; end
- local dif=math.abs(nx-xt.x);
- for i=1,dif do
- while not xt.fw() do
- xt.attack();
- xt.dig();
- end
- end
- end
- function gotoZDig(nz)
- if nz>xt.z+0.1 then face(0) elseif nz<xt.z-0.1 then face(2); else return true; end
- local dif=math.abs(nz-xt.z);
- for i=1,dif do
- while not xt.fw() do
- xt.attack();
- xt.dig();
- end
- end
- end
- function gotoYDig(ny)
- local func;
- local fDig;
- local fAtk;
- if ny>xt.y then func=xt.up;fDig=xt.digUp;fAtk=xt.attackUp; elseif ny<xt.y then func=xt.dn;fDig=xt.digDown;fAtk=xt.attackDown; else return true; end
- local dif=math.abs(ny-xt.y);
- for i=1,dif do
- while not func() do
- fAtk();
- fDig();
- end
- end
- end
- function gotoXZDig(nx,nz)
- local fx=getFX();
- local fz=getFZ();
- if (fz==1 and nz>xt.z) or (fz==-1 and nz<xt.z) or (fx==1 and nx<xt.x) or (fx==-1 and nx>xt.x) then
- gotoZDig(nz);
- gotoXDig(nx);
- else
- gotoXDig(nx);
- gotoZDig(nz);
- end
- end
- function gotoXZYDig(nx,ny,nz)
- gotoXZDig(nx,nz);
- gotoYDig(ny);
- end
- function gotoYXZDig(nx,ny,nz)
- gotoYDig(ny);
- gotoXZDig(nx,nz);
- end
- function navigate(coord,goal)
- local movePlus=true;
- if coord=="x" then
- if goal<xt.x then
- xt.face(xt.fc.xminus);
- movePlus=false;
- elseif goal>xt.x then
- xt.face(xt.fc.xplus);
- movePlus=true;
- else return; end
- elseif coord=="z" then
- if goal<xt.z then
- xt.face(xt.fc.zminus);
- movePlus=false;
- elseif goal>xt.z then
- xt.face(xt.fc.zplus);
- movePlus=true;
- else return; end
- else error("Invalid navigate coord",2); end
- while (movePlus and xt[coord]-0.1<goal) or (not movePlus and xt[coord]-0.1>goal) do
- repeat
- xt.attackDown();
- until not xt.dn();
- while not xt.fw() do
- xt.attack();
- while not xt.up() do
- xt.attackUp();
- while not xt.bk() do
- xt.dn();
- end
- end
- end
- end
- end
- function navXZ(nx,nz)
- local fx=getFX();
- local fz=getFZ();
- if (fz==1 and nz>xt.z) or (fz==-1 and nz<xt.z) or (fx==1 and nx<xt.x) or (fx==-1 and nx>xt.x) then
- navigate("z",nz);
- navigate("x",nx);
- else
- navigate("x",nx);
- navigate("z",nz);
- end
- end
- function locate()
- local tx;
- local ty;
- local tz;
- tx,ty,tz=gps.locate();
- if tx==nil or ty==nil or tz==nil then return false; end
- xt.x=tx;
- xt.z=tz;
- saveXZ();
- xt.y=ty;
- saveY();
- return true;
- end
- function calibrate(forward,back,donealready)
- if turtle.getFuelLevel()~="unlimited" then
- if turtle.getFuelLevel()<=2 then return false,"Not enough fuel"; end
- end
- if donealready==nil then donealready=false; end
- if forward==nil then forward=xt.fw; end
- if back==nil then back=xt.bk; end
- local tx;
- local tz;
- if not locate() then return false,"Could not reach GPS"; end
- tx=xt.x;tz=xt.z;
- if not forward() then
- if donealready then return false,"Could not go forward";
- else return calibrate(back,forward,true); end
- end
- if not locate() then back(); return false,"Could not reach GPS"; end
- local dx=xt.x-tx;
- local dz=xt.z-tz;
- if donealready then dx=-dx;dz=-dz; end
- if dz==1 then xt.f=0;
- elseif dz==-1 then xt.f=2;
- elseif dx==1 then xt.f=3;
- elseif dx==-1 then xt.f=1;
- end
- saveF();
- back(); return true;
- end
- forceForward=ffw;
- forceBack=fbk;
- forceUp=fup;
- forceDown=fdown;
- for k,v in pairs(turtle) do
- if k=="forward" then forward=fw;
- elseif k=="back" then back=bk;
- elseif k=="up" then
- elseif k=="down" then down=dn;
- elseif k=="turnLeft" then turnLeft=left;
- elseif k=="turnRight" then turnRight=right;
- elseif k=="select" then select=sel;
- elseif k=="inspect" then
- elseif k=="inspectUp" then
- elseif k=="inspectDown" then inspectDown=inspectDn;
- elseif k=="attack" then attack=v;atk=v;
- elseif k=="attackUp" then attackUp=v;atkUp=v;
- elseif k=="attackDown" then attackDown=v;atkDn=v;
- elseif k=="digDown" then digDown=v;digDn=v;
- else
- local env=getfenv();
- env[k]=v;
- setfenv(1,env);
- end
- end
- turtle.orig={};
- for k,v in pairs(turtle) do
- if k~="orig" then
- turtle.orig[k]=v;
- end
- end
- if replaceTurtle then
- turtle.forward=fw;
- turtle.back=bk;
- turtle.up=up;
- turtle.down=dn;
- turtle.turnLeft=left;
- turtle.turnRight=right;
- turtle.select=sel;
- replaceTurtle=nil;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment