negamartin

XT 1.1 by negamartin

Jun 2nd, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.10 KB | None | 0 0
  1. --XT V1.1 BY NEGAMARTIN
  2. version=1.1;
  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,textutils.serialize(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. --TURTLE PART
  95. if turtle then
  96.     sleep(0.1); --Sometimes the select gets stuck with no delay             TAKE IT AWAY AT YOUR OWN RISK
  97.     x=0;
  98.     y=64;
  99.     z=0;
  100.     f=0;
  101.     s=1;
  102.     turtle.select(1);
  103.     if autoSave==nil then autoSave=true; end
  104.     if replaceTurtle==nil then replaceTurtle=true; end
  105.    
  106.     local err=false;
  107.     err=pcall(function()
  108.         if fs.exists("xtdata") then
  109.             if not fs.isDir("xtdata") then
  110.                 fs.move("xtdata","copyofxtdata");
  111.                 fs.makeDir("xtdata");
  112.             end
  113.         else
  114.             fs.makeDir("xtdata");
  115.         end
  116.     end);
  117.     err=err or pcall(function()
  118.         if fs.exists("xtdata/xz") then
  119.             local file=fs.open("xtdata/xz","r");
  120.             x=math.floor(file.readLine());
  121.             z=math.floor(file.readLine());
  122.             file.close();
  123.         end
  124.     end);
  125.     err=err or pcall(function()
  126.         if fs.exists("xtdata/y") then
  127.             local file=fs.open("xtdata/y","r");
  128.             y=math.floor(file.readLine());
  129.             file.close();
  130.         end
  131.     end);
  132.     err=err or pcall(function()
  133.         if fs.exists("xtdata/f") then
  134.             local file=fs.open("xtdata/f","r");
  135.             f=math.floor(file.readLine());
  136.             file.close();
  137.         end
  138.     end);
  139.     err=err or pcall(function()
  140.         if fs.exists("xtdata/s") then
  141.             local file=fs.open("xtdata/s","r");
  142.             s=math.floor(file.readLine());
  143.             turtle.select(s);
  144.             file.close();
  145.         end
  146.     end);
  147.     err=err or pcall(function()
  148.         if fs.exists("xtdata/autosave") then
  149.             local file=fs.open("xtdata/autosave","r");
  150.             local tas=math.floor(file.readLine());
  151.             if tas==1 then autoSave=true;
  152.             elseif tas==0 then autoSave=false; end
  153.             file.close();
  154.         end
  155.     end);
  156.    
  157.     function saveXZ()
  158.         if not autoSave then return; end
  159.         local file=fs.open("xtdata/xz","w");
  160.         file.writeLine(xt.x);
  161.         file.writeLine(xt.z);
  162.         file.close();
  163.     end
  164.     function saveY()
  165.         if not autoSave then return; end
  166.         local file=fs.open("xtdata/y","w");
  167.         file.writeLine(xt.y);
  168.         file.close();
  169.     end
  170.     function saveF()
  171.         if not autoSave then return; end
  172.         local file=fs.open("xtdata/f","w");
  173.         file.writeLine(xt.f);
  174.         file.close();
  175.     end
  176.     function saveS()
  177.         if not autoSave then return; end
  178.         local file=fs.open("xtdata/s","w");
  179.         file.writeLine(xt.s);
  180.         file.close();
  181.     end
  182.     function savePos()
  183.         if not autoSave then return; end
  184.         saveXZ();
  185.         saveY();
  186.         saveF();
  187.     end
  188.     function save()
  189.         if not autoSave then return; end
  190.         saveXZ();
  191.         saveY();
  192.         saveF();
  193.         saveS();
  194.     end
  195.     function setAutoSave(nas)
  196.         autoSave=nas;
  197.         local file=fs.open("xtdata/autosave","w");
  198.         local tempAS;
  199.         if autoSave then tempAS=1; else tempAS=0; end
  200.         file.writeLine(tempAS);
  201.         file.close();
  202.     end
  203.    
  204.     function getFX(f)
  205.         f=f or xt.f;
  206.         if f==1 then return -1;
  207.         elseif f==3 then return 1;
  208.         else return 0; end
  209.     end
  210.     function getFZ(f)
  211.         f=f or xt.f;
  212.         if f==0 then return 1;
  213.         elseif f==2 then return -1;
  214.         else return 0; end
  215.     end
  216.    
  217.     function fw(amount)
  218.         if amount==nil then amount=1; end
  219.         if amount<0 then bk(-amount); end
  220.         for i=1,amount do
  221.             if turtle.orig.forward() then
  222.                 xt.x=xt.x+getFX();
  223.                 xt.z=xt.z+getFZ();
  224.                 xt.saveXZ();
  225.             else
  226.                 return false;
  227.             end
  228.         end
  229.         return true;
  230.     end
  231.     function bk(amount)
  232.         if amount==nil then amount=1; end
  233.         if amount<0 then fw(-amount); end
  234.         for i=1,amount do
  235.             if turtle.orig.back() then
  236.                 xt.x=xt.x-getFX();
  237.                 xt.z=xt.z-getFZ();
  238.                 xt.saveXZ();
  239.             else
  240.                 return false;
  241.             end
  242.         end
  243.         return true;
  244.     end
  245.    
  246.     function up(amount)
  247.         if amount==nil then amount=1; end
  248.         if amount<0 then dn(-amount); end
  249.         for i=1,amount do
  250.             if turtle.orig.up() then
  251.                 xt.y=xt.y+1;
  252.                 xt.saveY();
  253.             else
  254.                 return false;
  255.             end
  256.         end
  257.         return true;
  258.     end
  259.     function dn(amount)
  260.         if amount==nil then amount=1; end
  261.         if amount<0 then up(-amount); end
  262.         for i=1,amount do
  263.             if turtle.orig.down() then
  264.                 xt.y=xt.y-1;
  265.                 xt.saveY();
  266.             else
  267.                 return false;
  268.             end
  269.         end
  270.         return true;
  271.     end
  272.    
  273.     function left(amount)
  274.         if amount==nil then amount=1; end
  275.         if amount<0 then right(-amount); end
  276.         for i=1,amount do
  277.             turtle.orig.turnLeft();
  278.             xt.f=(xt.f-1)%4;
  279.             xt.saveF();
  280.         end
  281.         return true;
  282.     end
  283.     function right(amount)
  284.         if amount==nil then amount=1; end
  285.         if amount<0 then left(-amount); end
  286.         for i=1,amount do
  287.             turtle.orig.turnRight();
  288.             xt.f=(xt.f+1)%4;
  289.             xt.saveF();
  290.         end
  291.         return true;
  292.     end
  293.    
  294.     function sel(slot)
  295.         if turtle.orig.select(slot) then
  296.             xt.s=slot;
  297.             xt.saveS();
  298.         end
  299.         return true;
  300.     end
  301.    
  302.     fc={
  303.     ["south"]=0,
  304.     ["west"]=1,
  305.     ["north"]=2,
  306.     ["east"]=3,
  307.     [0]="south",
  308.     [1]="west",
  309.     [2]="north",
  310.     [3]="east"
  311.     };
  312.     function invertFace(facing)
  313.         return (facing+2)%4;
  314.     end
  315.     function face(nf)
  316.         if nf==nil then return false,"Not enough arguments"; end
  317.         local fdif=nf-xt.f;
  318.         if fdif==0 then return true;
  319.         elseif fdif==3 then left();return true;
  320.         elseif fdif==-3 then right(); return true;
  321.         else right(fdif);return true; end
  322.     end
  323.     function gotoX(nx)
  324.         if nx>xt.x then face(3) elseif nx<xt.x then face(1); else return true; end
  325.         local dif=math.abs(nx-xt.x);
  326.         return xt.fw(dif);
  327.     end
  328.     function gotoZ(nz)
  329.         if nz>xt.z then face(0) elseif nz<xt.z then face(2); else return true; end
  330.         local dif=math.abs(nz-xt.z);
  331.         return xt.fw(dif);
  332.     end
  333.     function gotoY(ny)
  334.         local func;
  335.         if ny>xt.y then func=xt.up; elseif ny<xt.y then func=xt.dn; else return true; end
  336.         local dif=math.abs(ny-xt.y);
  337.         return func(dif);
  338.     end
  339.    
  340.     function locate()
  341.         local tx;
  342.         local ty;
  343.         local tz;
  344.         tx,ty,tz=gps.locate();
  345.         if tx==nil or ty==nil or tz==nil then return false; end
  346.         xt.x=tx;
  347.         xt.z=tz;
  348.         saveXZ();
  349.         xt.y=ty;
  350.         saveY();
  351.         return true;
  352.     end
  353.     function calibrate(forward,back,donealready)
  354.         if turtle.getFuelLevel()~="unlimited" then
  355.             if turtle.getFuelLevel()<=2 then return false,"Not enough fuel"; end
  356.         end
  357.         if donealready==nil then donealready=false; end
  358.         if forward==nil then forward=xt.fw; end
  359.         if back==nil then back=xt.bk; end
  360.         local tx;
  361.         local tz;
  362.         if not locate() then return false,"Could not reach GPS"; end
  363.         tx=xt.x;tz=xt.z;
  364.         if not forward() then
  365.             if donealready then return false,"Could not go forward";
  366.             else return calibrate(back,forward,true); end
  367.         end
  368.         if not locate() then back(); return false,"Could not reach GPS"; end
  369.         local dx=xt.x-tx;
  370.         local dz=xt.z-tz;
  371.         if donealready then dx=-dx;dz=-dz; end
  372.         if dz==1 then xt.f=0;
  373.         elseif dz==-1 then xt.f=2;
  374.         elseif dx==1 then xt.f=3;
  375.         elseif dx==-1 then xt.f=1;
  376.         end
  377.         saveF();
  378.         back(); return true;
  379.     end
  380.    
  381.     for k,v in pairs(turtle) do
  382.         if k=="forward" then forward=fw;
  383.         elseif k=="back" then back=bk;
  384.         elseif k=="up" then
  385.         elseif k=="down" then down=dn;
  386.         elseif k=="turnLeft" then turnLeft=left;
  387.         elseif k=="turnRight" then turnRight=right;
  388.         elseif k=="select" then select=sel;
  389.         else
  390.             local env=getfenv();
  391.             env[k]=v;
  392.             setfenv(1,env);
  393.         end
  394.     end
  395.     turtle.orig={};
  396.     for k,v in pairs(turtle) do
  397.         if k~="orig" then
  398.             turtle.orig[k]=v;
  399.         end
  400.     end
  401.     if replaceTurtle then
  402.         turtle.forward=fw;
  403.         turtle.back=bk;
  404.         turtle.up=up;
  405.         turtle.down=dn;
  406.         turtle.turnLeft=left;
  407.         turtle.turnRight=right;
  408.         turtle.select=sel;
  409.         replaceTurtle=nil;
  410.     end
  411. end
Advertisement
Add Comment
Please, Sign In to add comment