negamartin

XT 1.4 by negamartin

Jun 3rd, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.15 KB | None | 0 0
  1. --XT V1.4 by NEGAMARTIN
  2. version=1.4;
  3.  
  4. --WIRELESS PART
  5. running=false;
  6. tempOpen=nil;
  7. replacedRednet=false;
  8. redrunEnv=nil;
  9. function run()
  10.     xt.running=true;
  11.     local packetids={};
  12.     local timerids={};
  13.     while xt.running do
  14.         eva={os.pullEvent()};
  15.        
  16.         if eva[1]=="modem_message" then
  17.             pcall(function()
  18.                 local msg=textutils.unserialize(eva[5]);
  19.                 if msg.packetID==nil then error(); end
  20.                 if packetids[msg.packetID]==nil then
  21.                     os.queueEvent("relayed_message",eva[2],eva[3],eva[4],msg,eva[6]);
  22.                     packetids[msg.packetID]=true;
  23.                     timerids[os.startTimer(4)]=msg.packetID;
  24.                 end
  25.             end);
  26.         elseif eva[1]=="timer" then
  27.             pcall(function()
  28.                 packetids[timerids[eva[2]]]=nil;
  29.                 timerids[eva[2]]=nil;
  30.             end);
  31.         end
  32.     end
  33.     xt.running=false;
  34.     if xt.replacedRednet then
  35.         xt.replacedRednet=false;
  36.         xt.redrunEnv.isOpen=xt.tempOpen;
  37.         return false;
  38.     end
  39. end
  40. function finish()
  41.     xt.running=false;
  42.     os.queueEvent("refresh event FTW!")
  43. end
  44. function replaceRednet()
  45.     xt.replacedRednet=true;
  46.     xt.redrunEnv=getfenv(rednet.run);
  47.     xt.tempOpen=xt.redrunEnv.isOpen;
  48.     xt.redrunEnv.isOpen=xt.run;
  49.     os.queueEvent("modem_message","",0,0,"",0);
  50. end
  51.  
  52. modem=nil;
  53. function connectModem()
  54.     local names=peripheral.getNames();
  55.     local wlr=true;
  56.     local modemSide;
  57.     for i=1,2 do
  58.         for _,name in ipairs(names) do
  59.             if peripheral.getType(name)=="modem" then
  60.                 if peripheral.call(name,"isWireless")==wlr then
  61.                     xt.modem=peripheral.wrap(name);
  62.                     modemSide=name;
  63.                     break;
  64.                 end
  65.             end
  66.         end
  67.         if modemSide then break; end
  68.         wlr=false;
  69.     end
  70.     if not modemSide then modemSide=false; end
  71.     return modemSide;
  72. end
  73. function transmit(ch,rp,tab)
  74.     if not xt.modem then
  75.         xt.connectModem();
  76.         if not xt.modem then error("Could not find modem",2); end
  77.     end
  78.     tab.packetID=math.random();
  79.     xt.modem.transmit(ch,rp,tab);
  80. end
  81. function openChannel(ch)
  82.     if not xt.modem then
  83.         xt.connectModem();
  84.         if not xt.modem then error("Could not find modem",2); end
  85.     end
  86.     xt.modem.open(ch);
  87.     xt.modem.transmit(0,17871,textutils.serialize({["packetID"]=math.random(),["channel"]=ch}));
  88.     return true;
  89. end
  90.  
  91.  
  92.  
  93.  
  94. --SAVING PART
  95. custom={};
  96. local err=false;
  97. err=pcall(function()
  98.     if fs.exists("xtdata") then
  99.         if not fs.isDir("xtdata") then
  100.             fs.move("xtdata","copyofxtdata");
  101.             fs.makeDir("xtdata");
  102.         end
  103.     else
  104.         fs.makeDir("xtdata");
  105.     end
  106. end);
  107. ok,en=pcall(function()
  108.         if not fs.exists("xtdata/custom") then
  109.             fs.makeDir("xtdata/custom");
  110.         end
  111.     end);if not ok then print(en); end
  112.     ok,en=pcall(function()
  113.         for _,name in ipairs(fs.list("xtdata/custom")) do
  114.             local file=fs.open("xtdata/custom/"..name,"r");
  115.             local word=file.readLine();
  116.             local value=file.readLine();
  117.             local t=file.readLine();
  118.             if t=="number" then value=tonumber(value); end
  119.             if t=="table" then value=textutils.unserialize(value); end
  120.             custom[word]=value;
  121.             file.close();
  122.         end
  123.     end);if not ok then print(en); end
  124. function gc(word)
  125.     return xt.custom[word];
  126. end
  127. function sc(word,value)
  128.     xt.custom[word]=value;
  129.     if value==nil then
  130.         if fs.exists("xtdata/custom/"..word) then
  131.             fs.delete("xtdata/custom/"..word);
  132.         end
  133.     else
  134.         local file=fs.open("xtdata/custom/"..word,"w");
  135.         local t=type(value);
  136.         if type(value)=="table" then
  137.             local temp=textutils.serialize(value);
  138.             value=string.gsub(temp,"\n","");
  139.             --print(value);
  140.         end
  141.         file.writeLine(word);
  142.         file.writeLine(value);
  143.         file.writeLine(t);
  144.         file.close();
  145.     end
  146. end
  147.  
  148.  
  149.  
  150.  
  151. --TURTLE PART
  152. if turtle then
  153.     sleep(0.1); --Sometimes the select gets stuck with no delay             TAKE IT AWAY AT YOUR OWN RISK
  154.     x=0;
  155.     y=64;
  156.     z=0;
  157.     f=0;
  158.     s=1;
  159.     turtle.select(1);
  160.     if autoSave==nil then autoSave=true; end
  161.     if replaceTurtle==nil then replaceTurtle=true; end
  162.    
  163.     local err=false;
  164.     err=pcall(function()
  165.         if fs.exists("xtdata") then
  166.             if not fs.isDir("xtdata") then
  167.                 fs.move("xtdata","copyofxtdata");
  168.                 fs.makeDir("xtdata");
  169.             end
  170.         else
  171.             fs.makeDir("xtdata");
  172.         end
  173.     end);
  174.     err=err or pcall(function()
  175.         if fs.exists("xtdata/xz") then
  176.             local file=fs.open("xtdata/xz","r");
  177.             x=math.floor(file.readLine());
  178.             z=math.floor(file.readLine());
  179.             file.close();
  180.         end
  181.     end);
  182.     err=err or pcall(function()
  183.         if fs.exists("xtdata/y") then
  184.             local file=fs.open("xtdata/y","r");
  185.             y=math.floor(file.readLine());
  186.             file.close();
  187.         end
  188.     end);
  189.     err=err or pcall(function()
  190.         if fs.exists("xtdata/f") then
  191.             local file=fs.open("xtdata/f","r");
  192.             f=math.floor(file.readLine());
  193.             file.close();
  194.         end
  195.     end);
  196.     err=err or pcall(function()
  197.         if fs.exists("xtdata/s") then
  198.             local file=fs.open("xtdata/s","r");
  199.             s=math.floor(file.readLine());
  200.             turtle.select(s);
  201.             file.close();
  202.         end
  203.     end);
  204.     err=err or pcall(function()
  205.         if fs.exists("xtdata/autosave") then
  206.             local file=fs.open("xtdata/autosave","r");
  207.             local tas=math.floor(file.readLine());
  208.             if tas==1 then autoSave=true;
  209.             elseif tas==0 then autoSave=false; end
  210.             file.close();
  211.         end
  212.     end);
  213.    
  214.     function saveXZ()
  215.         if not autoSave then return; end
  216.         local file=fs.open("xtdata/xz","w");
  217.         file.writeLine(xt.x);
  218.         file.writeLine(xt.z);
  219.         file.close();
  220.     end
  221.     function saveY()
  222.         if not autoSave then return; end
  223.         local file=fs.open("xtdata/y","w");
  224.         file.writeLine(xt.y);
  225.         file.close();
  226.     end
  227.     function saveF()
  228.         if not autoSave then return; end
  229.         local file=fs.open("xtdata/f","w");
  230.         file.writeLine(xt.f);
  231.         file.close();
  232.     end
  233.     function saveS()
  234.         if not autoSave then return; end
  235.         local file=fs.open("xtdata/s","w");
  236.         file.writeLine(xt.s);
  237.         file.close();
  238.     end
  239.     function savePos()
  240.         if not autoSave then return; end
  241.         saveXZ();
  242.         saveY();
  243.         saveF();
  244.     end
  245.     function save()
  246.         if not autoSave then return; end
  247.         saveXZ();
  248.         saveY();
  249.         saveF();
  250.         saveS();
  251.     end
  252.     function setAutoSave(nas)
  253.         autoSave=nas;
  254.         local file=fs.open("xtdata/autosave","w");
  255.         local tempAS;
  256.         if autoSave then tempAS=1; else tempAS=0; end
  257.         file.writeLine(tempAS);
  258.         file.close();
  259.     end
  260.    
  261.     function getFX(f)
  262.         f=f or xt.f;
  263.         if f==1 then return -1;
  264.         elseif f==3 then return 1;
  265.         else return 0; end
  266.     end
  267.     function getFZ(f)
  268.         f=f or xt.f;
  269.         if f==0 then return 1;
  270.         elseif f==2 then return -1;
  271.         else return 0; end
  272.     end
  273.    
  274.     function fw(amount)
  275.         if amount==nil then amount=1; end
  276.         if amount<0 then bk(-amount); end
  277.         for i=1,amount do
  278.             if turtle.orig.forward() then
  279.                 xt.x=xt.x+getFX();
  280.                 xt.z=xt.z+getFZ();
  281.                 xt.saveXZ();
  282.             else
  283.                 return false;
  284.             end
  285.         end
  286.         return true;
  287.     end
  288.     function bk(amount)
  289.         if amount==nil then amount=1; end
  290.         if amount<0 then fw(-amount); end
  291.         for i=1,amount do
  292.             if turtle.orig.back() then
  293.                 xt.x=xt.x-getFX();
  294.                 xt.z=xt.z-getFZ();
  295.                 xt.saveXZ();
  296.             else
  297.                 return false;
  298.             end
  299.         end
  300.         return true;
  301.     end
  302.    
  303.     function up(amount)
  304.         if amount==nil then amount=1; end
  305.         if amount<0 then dn(-amount); end
  306.         for i=1,amount do
  307.             if turtle.orig.up() then
  308.                 xt.y=xt.y+1;
  309.                 xt.saveY();
  310.             else
  311.                 return false;
  312.             end
  313.         end
  314.         return true;
  315.     end
  316.     function dn(amount)
  317.         if amount==nil then amount=1; end
  318.         if amount<0 then up(-amount); end
  319.         for i=1,amount do
  320.             if turtle.orig.down() then
  321.                 xt.y=xt.y-1;
  322.                 xt.saveY();
  323.             else
  324.                 return false;
  325.             end
  326.         end
  327.         return true;
  328.     end
  329.    
  330.     function left(amount)
  331.         if amount==nil then amount=1; end
  332.         if amount<0 then right(-amount); end
  333.         for i=1,amount do
  334.             turtle.orig.turnLeft();
  335.             xt.f=(xt.f-1)%4;
  336.             xt.saveF();
  337.         end
  338.         return true;
  339.     end
  340.     function right(amount)
  341.         if amount==nil then amount=1; end
  342.         if amount<0 then left(-amount); end
  343.         for i=1,amount do
  344.             turtle.orig.turnRight();
  345.             xt.f=(xt.f+1)%4;
  346.             xt.saveF();
  347.         end
  348.         return true;
  349.     end
  350.    
  351.     function sel(slot)
  352.         if slot~=xt.s then
  353.             if turtle.orig.select(slot) then
  354.                 xt.s=slot;
  355.                 xt.saveS();
  356.             end
  357.         end
  358.         return true;
  359.     end
  360.    
  361.     fc={
  362.     ["south"]=0,
  363.     ["west"]=1,
  364.     ["north"]=2,
  365.     ["east"]=3,
  366.     [0]="south",
  367.     [1]="west",
  368.     [2]="north",
  369.     [3]="east",
  370.     ["zplus"]=0,
  371.     ["xminus"]=1,
  372.     ["zminus"]=2,
  373.     ["xplus"]=3,
  374.     };
  375.     function invertFace(facing)
  376.         return (facing+2)%4;
  377.     end
  378.     function face(nf)
  379.         if nf==nil then return false,"Not enough arguments"; end
  380.         local fdif=nf-xt.f;
  381.         if fdif==0 then return true;
  382.         elseif fdif==3 then left();return true;
  383.         elseif fdif==-3 then right(); return true;
  384.         else right(fdif);return true; end
  385.     end
  386.     function gotoX(nx)
  387.         if nx>xt.x+0.1 then face(3) elseif nx<xt.x-0.1 then face(1); else return true; end
  388.         local dif=math.abs(nx-xt.x);
  389.         return xt.fw(dif);
  390.     end
  391.     function gotoZ(nz)
  392.         if nz>xt.z+0.1 then face(0) elseif nz<xt.z-0.1 then face(2); else return true; end
  393.         local dif=math.abs(nz-xt.z);
  394.         return xt.fw(dif);
  395.     end
  396.     function gotoXZ(nx,nz)
  397.         local fx=getFX();
  398.         local fz=getFZ();
  399.         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
  400.             gotoZ(nz);
  401.             gotoX(nx);
  402.         else
  403.             gotoX(nx);
  404.             gotoZ(nz);
  405.         end
  406.     end
  407.     function gotoY(ny)
  408.         local func;
  409.         if ny>xt.y then func=xt.up; elseif ny<xt.y then func=xt.dn; else return true; end
  410.         local dif=math.abs(ny-xt.y);
  411.         return func(dif);
  412.     end
  413.     function gotoXZY(nx,ny,nz)
  414.         gotoXZ(nx,nz);
  415.         gotoY(ny);
  416.     end
  417.     function gotoYXZ(nx,ny,nz)
  418.         gotoY(ny);
  419.         gotoXZ(nx,nz);
  420.     end
  421.  
  422.     function gotoXDig(nx)
  423.         if nx>xt.x+0.1 then face(3) elseif nx<xt.x-0.1 then face(1); else return true; end
  424.         local dif=math.abs(nx-xt.x);
  425.         for i=1,dif do
  426.             while not xt.fw() do
  427.                 xt.attack();
  428.                 xt.dig();
  429.             end
  430.         end
  431.     end
  432.     function gotoZDig(nz)
  433.         if nz>xt.z+0.1 then face(0) elseif nz<xt.z-0.1 then face(2); else return true; end
  434.         local dif=math.abs(nz-xt.z);
  435.         for i=1,dif do
  436.             while not xt.fw() do
  437.                 xt.attack();
  438.                 xt.dig();
  439.             end
  440.         end
  441.     end
  442.     function gotoYDig(ny)
  443.         local func;
  444.         local fDig;
  445.         local fAtk;
  446.         if ny>xt.y then func=xt.up;fDig=xt.digUp;fAtk=xt.attackUp; elseif ny<xt.y then func=xt.dn;fDig=xt.attackDown;fAtk=xt.attackDown; else return true; end
  447.         local dif=math.abs(ny-xt.y);
  448.         for i=1,dif do
  449.             while not func() do
  450.                 fAtk();
  451.                 fDig();
  452.             end
  453.         end
  454.     end
  455.     function gotoXZDig(nx,nz)
  456.         local fx=getFX();
  457.         local fz=getFZ();
  458.         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
  459.             gotoZDig(nz);
  460.             gotoXDig(nx);
  461.         else
  462.             gotoXDig(nx);
  463.             gotoZDig(nz);
  464.         end
  465.     end
  466.     function gotoXZYDig(nx,ny,nz)
  467.         gotoXZDig(nx,nz);
  468.         gotoYDig(ny);
  469.     end
  470.     function gotoYXZDig(nx,ny,nz)
  471.         gotoYDig(ny);
  472.         gotoXZDig(nx,nz);
  473.     end
  474.    
  475.     function navigate(coord,goal)
  476.         local movePlus=true;
  477.         if coord=="x" then
  478.             if goal<xt.x then
  479.                 xt.face(xt.fc.xminus);
  480.                 movePlus=false;
  481.             elseif goal>xt.x then
  482.                 xt.face(xt.fc.xplus);
  483.                 movePlus=true;
  484.             else return; end
  485.         elseif coord=="z" then
  486.             if goal<xt.z then
  487.                 xt.face(xt.fc.zminus);
  488.                 movePlus=false;
  489.             elseif goal>xt.z then
  490.                 xt.face(xt.fc.zplus);
  491.                 movePlus=true;
  492.             else return; end
  493.         else error("Invalid navigate coord",2); end
  494.         while (movePlus and xt[coord]-0.1<goal) or (not movePlus and xt[coord]-0.1>goal) do
  495.             repeat
  496.                 xt.attackDown();
  497.             until not xt.dn();
  498.             while not xt.fw() do
  499.                 xt.attack();
  500.                 while not xt.up() do
  501.                     xt.attackUp();
  502.                     while not xt.bk() do
  503.                         xt.dn();
  504.                     end
  505.                 end
  506.             end
  507.         end
  508.     end
  509.     function navXZ(nx,nz)
  510.         local fx=getFX();
  511.         local fz=getFZ();
  512.         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
  513.             navigate("z",nz);
  514.             navigate("x",nx);
  515.         else
  516.             navigate("x",nx);
  517.             navigate("z",nz);
  518.         end
  519.     end
  520.    
  521.     function locate()
  522.         local tx;
  523.         local ty;
  524.         local tz;
  525.         tx,ty,tz=gps.locate();
  526.         if tx==nil or ty==nil or tz==nil then return false; end
  527.         xt.x=tx;
  528.         xt.z=tz;
  529.         saveXZ();
  530.         xt.y=ty;
  531.         saveY();
  532.         return true;
  533.     end
  534.     function calibrate(forward,back,donealready)
  535.         if turtle.getFuelLevel()~="unlimited" then
  536.             if turtle.getFuelLevel()<=2 then return false,"Not enough fuel"; end
  537.         end
  538.         if donealready==nil then donealready=false; end
  539.         if forward==nil then forward=xt.fw; end
  540.         if back==nil then back=xt.bk; end
  541.         local tx;
  542.         local tz;
  543.         if not locate() then return false,"Could not reach GPS"; end
  544.         tx=xt.x;tz=xt.z;
  545.         if not forward() then
  546.             if donealready then return false,"Could not go forward";
  547.             else return calibrate(back,forward,true); end
  548.         end
  549.         if not locate() then back(); return false,"Could not reach GPS"; end
  550.         local dx=xt.x-tx;
  551.         local dz=xt.z-tz;
  552.         if donealready then dx=-dx;dz=-dz; end
  553.         if dz==1 then xt.f=0;
  554.         elseif dz==-1 then xt.f=2;
  555.         elseif dx==1 then xt.f=3;
  556.         elseif dx==-1 then xt.f=1;
  557.         end
  558.         saveF();
  559.         back(); return true;
  560.     end
  561.    
  562.     for k,v in pairs(turtle) do
  563.         if k=="forward" then forward=fw;
  564.         elseif k=="back" then back=bk;
  565.         elseif k=="up" then
  566.         elseif k=="down" then down=dn;
  567.         elseif k=="turnLeft" then turnLeft=left;
  568.         elseif k=="turnRight" then turnRight=right;
  569.         elseif k=="select" then select=sel;
  570.         else
  571.             local env=getfenv();
  572.             env[k]=v;
  573.             setfenv(1,env);
  574.         end
  575.     end
  576.     turtle.orig={};
  577.     for k,v in pairs(turtle) do
  578.         if k~="orig" then
  579.             turtle.orig[k]=v;
  580.         end
  581.     end
  582.     if replaceTurtle then
  583.         turtle.forward=fw;
  584.         turtle.back=bk;
  585.         turtle.up=up;
  586.         turtle.down=dn;
  587.         turtle.turnLeft=left;
  588.         turtle.turnRight=right;
  589.         turtle.select=sel;
  590.         replaceTurtle=nil;
  591.     end
  592. end
Advertisement
Add Comment
Please, Sign In to add comment