Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --XT V1.2 BY NEGAMARTIN
- version=1.2;
- --WIRELESS PART
- running=false;
- tempOpen=nil;
- replacedRednet=false;
- redrunEnv=nil;
- function run()
- xt.running=true;
- local packetids={};
- local timerids={};
- while xt.running do
- eva={os.pullEvent()};
- if eva[1]=="modem_message" then
- pcall(function()
- local msg=textutils.unserialize(eva[5]);
- if msg.packetID==nil then error(); end
- if packetids[msg.packetID]==nil then
- os.queueEvent("relayed_message",eva[2],eva[3],eva[4],msg,eva[6]);
- packetids[msg.packetID]=true;
- timerids[os.startTimer(4)]=msg.packetID;
- end
- end);
- elseif eva[1]=="timer" then
- pcall(function()
- packetids[timerids[eva[2]]]=nil;
- timerids[eva[2]]=nil;
- end);
- end
- end
- xt.running=false;
- if xt.replacedRednet then
- xt.replacedRednet=false;
- xt.redrunEnv.isOpen=xt.tempOpen;
- return false;
- end
- end
- function finish()
- xt.running=false;
- os.queueEvent("refresh event FTW!")
- end
- function replaceRednet()
- xt.replacedRednet=true;
- xt.redrunEnv=getfenv(rednet.run);
- xt.tempOpen=xt.redrunEnv.isOpen;
- xt.redrunEnv.isOpen=xt.run;
- os.queueEvent("modem_message","",0,0,"",0);
- end
- modem=nil;
- function connectModem()
- local names=peripheral.getNames();
- local wlr=true;
- local modemSide;
- for i=1,2 do
- for _,name in ipairs(names) do
- if peripheral.getType(name)=="modem" then
- if peripheral.call(name,"isWireless")==wlr then
- xt.modem=peripheral.wrap(name);
- modemSide=name;
- break;
- end
- end
- end
- if modemSide then break; end
- wlr=false;
- end
- if not modemSide then modemSide=false; end
- return modemSide;
- end
- function transmit(ch,rp,tab)
- if not xt.modem then
- xt.connectModem();
- if not xt.modem then error("Could not find modem",2); end
- end
- tab.packetID=math.random();
- xt.modem.transmit(ch,rp,textutils.serialize(tab))
- end
- function openChannel(ch)
- if not xt.modem then
- xt.connectModem();
- if not xt.modem then error("Could not find modem",2); end
- end
- xt.modem.open(ch);
- xt.modem.transmit(0,17871,textutils.serialize({["packetID"]=math.random(),["channel"]=ch}));
- return true;
- 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;
- custom={};
- turtle.select(1);
- if autoSave==nil then autoSave=true; end
- if replaceTurtle==nil then replaceTurtle=true; end
- local ok=true;
- local en;
- ok,en=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);if not ok then print(en); end
- ok,en=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);if not ok then print(en); end
- ok,en=pcall(function()
- if fs.exists("xtdata/y") then
- local file=fs.open("xtdata/y","r");
- y=math.floor(file.readLine());
- file.close();
- end
- end);if not ok then print(en); end
- ok,en=pcall(function()
- if fs.exists("xtdata/f") then
- local file=fs.open("xtdata/f","r");
- f=math.floor(file.readLine());
- file.close();
- end
- end);if not ok then print(en); end
- ok,en=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);if not ok then print(en); end
- ok,en=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);if not ok then print(en); end
- ok,en=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);if not ok then print(en); 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 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 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
- 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
- xt.x=xt.x+getFX();
- xt.z=xt.z+getFZ();
- xt.saveXZ();
- if not turtle.orig.forward() then
- xt.x=xt.x-getFX();
- xt.z=xt.z-getFZ();
- xt.saveXZ();
- return false;
- end
- 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
- xt.x=xt.x-getFX();
- xt.z=xt.z-getFZ();
- xt.saveXZ();
- if not turtle.orig.back() then
- xt.x=xt.x+getFX();
- xt.z=xt.z+getFZ();
- xt.saveXZ();
- return false;
- end
- 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
- xt.y=xt.y+1;
- xt.saveY();
- if not turtle.orig.up() then
- xt.y=xt.y-1;
- xt.saveY();
- return false;
- end
- 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
- xt.y=xt.y-1;
- xt.saveY();
- if not turtle.orig.down() then
- xt.y=xt.y+1;
- xt.saveY();
- return false;
- end
- 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
- xt.f=(xt.f-1)%4;
- xt.saveF();
- turtle.orig.turnLeft();
- 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
- xt.f=(xt.f+1)%4;
- xt.saveF();
- turtle.orig.turnRight();
- end
- return true;
- end
- function sel(slot)
- if turtle.orig.select(slot) then
- xt.s=slot;
- xt.saveS();
- end
- return true;
- end
- fc={
- ["south"]=0,
- ["west"]=1,
- ["north"]=2,
- ["east"]=3,
- [0]="south",
- [1]="west",
- [2]="north",
- [3]="east"
- };
- 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 then face(3) elseif nx<xt.x 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 then face(0) elseif nz<xt.z then face(2); else return true; end
- local dif=math.abs(nz-xt.z);
- return xt.fw(dif);
- 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 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
- 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;
- 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