negamartin

XT 1.2 by negamartin

Dec 21st, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.83 KB | None | 0 0
  1. --XT V1.2 BY NEGAMARTIN
  2. version=1.2;
  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=0;
  99.     z=0;
  100.     f=0;
  101.     s=1;
  102.     custom={};
  103.     turtle.select(1);
  104.     if autoSave==nil then autoSave=true; end
  105.     if replaceTurtle==nil then replaceTurtle=true; end
  106.    
  107.     local ok=true;
  108.     local en;
  109.     ok,en=pcall(function()
  110.         if fs.exists("xtdata") then
  111.             if not fs.isDir("xtdata") then
  112.                 fs.move("xtdata","copyofxtdata");
  113.                 fs.makeDir("xtdata");
  114.             end
  115.         else
  116.             fs.makeDir("xtdata");
  117.         end
  118.     end);if not ok then print(en); end
  119.     ok,en=pcall(function()
  120.         if fs.exists("xtdata/xz") then
  121.             local file=fs.open("xtdata/xz","r");
  122.             x=math.floor(file.readLine());
  123.             z=math.floor(file.readLine());
  124.             file.close();
  125.         end
  126.     end);if not ok then print(en); end
  127.     ok,en=pcall(function()
  128.         if fs.exists("xtdata/y") then
  129.             local file=fs.open("xtdata/y","r");
  130.             y=math.floor(file.readLine());
  131.             file.close();
  132.         end
  133.     end);if not ok then print(en); end
  134.     ok,en=pcall(function()
  135.         if fs.exists("xtdata/f") then
  136.             local file=fs.open("xtdata/f","r");
  137.             f=math.floor(file.readLine());
  138.             file.close();
  139.         end
  140.     end);if not ok then print(en); end
  141.     ok,en=pcall(function()
  142.         if fs.exists("xtdata/s") then
  143.             local file=fs.open("xtdata/s","r");
  144.             s=math.floor(file.readLine());
  145.             turtle.select(s);
  146.             file.close();
  147.         end
  148.     end);if not ok then print(en); end
  149.     ok,en=pcall(function()
  150.         if fs.exists("xtdata/autosave") then
  151.             local file=fs.open("xtdata/autosave","r");
  152.             local tas=math.floor(file.readLine());
  153.             if tas==1 then autoSave=true;
  154.             elseif tas==0 then autoSave=false; end
  155.             file.close();
  156.         end
  157.     end);if not ok then print(en); end
  158.     ok,en=pcall(function()
  159.         if fs.exists("xtdata/autosave") then
  160.             local file=fs.open("xtdata/autosave","r");
  161.             local tas=math.floor(file.readLine());
  162.             if tas==1 then autoSave=true;
  163.             elseif tas==0 then autoSave=false; end
  164.             file.close();
  165.         end
  166.     end);if not ok then print(en); end
  167.    
  168.     ok,en=pcall(function()
  169.         if not fs.exists("xtdata/custom") then
  170.             fs.makeDir("xtdata/custom");
  171.         end
  172.     end);if not ok then print(en); end
  173.     ok,en=pcall(function()
  174.         for _,name in ipairs(fs.list("xtdata/custom")) do
  175.             local file=fs.open("xtdata/custom/"..name,"r");
  176.             local word=file.readLine();
  177.             local value=file.readLine();
  178.             local t=file.readLine();
  179.             if t=="number" then value=tonumber(value); end
  180.             if t=="table" then value=textutils.unserialize(value); end
  181.             custom[word]=value;
  182.             file.close();
  183.         end
  184.     end);if not ok then print(en); end
  185.    
  186.     function saveXZ()
  187.         if not autoSave then return; end
  188.         local file=fs.open("xtdata/xz","w");
  189.         file.writeLine(xt.x);
  190.         file.writeLine(xt.z);
  191.         file.close();
  192.     end
  193.     function saveY()
  194.         if not autoSave then return; end
  195.         local file=fs.open("xtdata/y","w");
  196.         file.writeLine(xt.y);
  197.         file.close();
  198.     end
  199.     function saveF()
  200.         if not autoSave then return; end
  201.         local file=fs.open("xtdata/f","w");
  202.         file.writeLine(xt.f);
  203.         file.close();
  204.     end
  205.     function saveS()
  206.         if not autoSave then return; end
  207.         local file=fs.open("xtdata/s","w");
  208.         file.writeLine(xt.s);
  209.         file.close();
  210.     end
  211.     function savePos()
  212.         if not autoSave then return; end
  213.         saveXZ();
  214.         saveY();
  215.         saveF();
  216.     end
  217.     function save()
  218.         if not autoSave then return; end
  219.         saveXZ();
  220.         saveY();
  221.         saveF();
  222.         saveS();
  223.     end
  224.     function setAutoSave(nas)
  225.         autoSave=nas;
  226.         local file=fs.open("xtdata/autosave","w");
  227.         local tempAS;
  228.         if autoSave then tempAS=1; else tempAS=0; end
  229.         file.writeLine(tempAS);
  230.         file.close();
  231.     end
  232.     function gc(word)
  233.         return xt.custom[word];
  234.     end
  235.     function sc(word,value)
  236.         xt.custom[word]=value;
  237.         if value==nil then
  238.             if fs.exists("xtdata/custom/"..word) then
  239.                 fs.delete("xtdata/custom/"..word);
  240.             end
  241.         else
  242.             local file=fs.open("xtdata/custom/"..word,"w");
  243.             local t=type(value);
  244.             if type(value)=="table" then
  245.                 local temp=textutils.serialize(value);
  246.                 value=string.gsub(temp,"\n","");
  247.                 --print(value);
  248.             end
  249.             file.writeLine(word);
  250.             file.writeLine(value);
  251.             file.writeLine(t);
  252.             file.close();
  253.         end
  254.     end
  255.    
  256.     function getFX(f)
  257.         f=f or xt.f;
  258.         if f==1 then return -1;
  259.         elseif f==3 then return 1;
  260.         else return 0; end
  261.     end
  262.     function getFZ(f)
  263.         f=f or xt.f;
  264.         if f==0 then return 1;
  265.         elseif f==2 then return -1;
  266.         else return 0; end
  267.     end
  268.    
  269.     function fw(amount)
  270.         if amount==nil then amount=1; end
  271.         if amount<0 then bk(-amount); end
  272.         for i=1,amount do
  273.             xt.x=xt.x+getFX();
  274.             xt.z=xt.z+getFZ();
  275.             xt.saveXZ();
  276.             if not turtle.orig.forward() then
  277.                 xt.x=xt.x-getFX();
  278.                 xt.z=xt.z-getFZ();
  279.                 xt.saveXZ();
  280.                 return false;
  281.             end
  282.         end
  283.         return true;
  284.     end
  285.     function bk(amount)
  286.         if amount==nil then amount=1; end
  287.         if amount<0 then fw(-amount); end
  288.         for i=1,amount do
  289.             xt.x=xt.x-getFX();
  290.             xt.z=xt.z-getFZ();
  291.             xt.saveXZ();
  292.             if not turtle.orig.back() then
  293.                 xt.x=xt.x+getFX();
  294.                 xt.z=xt.z+getFZ();
  295.                 xt.saveXZ();
  296.                 return false;
  297.             end
  298.         end
  299.         return true;
  300.     end
  301.    
  302.     function up(amount)
  303.         if amount==nil then amount=1; end
  304.         if amount<0 then dn(-amount); end
  305.         for i=1,amount do
  306.             xt.y=xt.y+1;
  307.             xt.saveY();
  308.             if not turtle.orig.up() then
  309.                 xt.y=xt.y-1;
  310.                 xt.saveY();
  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.             xt.y=xt.y-1;
  321.             xt.saveY();
  322.             if not turtle.orig.down() then
  323.                 xt.y=xt.y+1;
  324.                 xt.saveY();
  325.                 return false;
  326.             end
  327.         end
  328.         return true;
  329.     end
  330.    
  331.     function left(amount)
  332.         if amount==nil then amount=1; end
  333.         if amount<0 then right(-amount); end
  334.         for i=1,amount do
  335.             xt.f=(xt.f-1)%4;
  336.             xt.saveF();
  337.             turtle.orig.turnLeft();
  338.         end
  339.         return true;
  340.     end
  341.     function right(amount)
  342.         if amount==nil then amount=1; end
  343.         if amount<0 then left(-amount); end
  344.         for i=1,amount do
  345.             xt.f=(xt.f+1)%4;
  346.             xt.saveF();
  347.             turtle.orig.turnRight();
  348.         end
  349.         return true;
  350.     end
  351.    
  352.     function sel(slot)
  353.         if turtle.orig.select(slot) then
  354.             xt.s=slot;
  355.             xt.saveS();
  356.         end
  357.         return true;
  358.     end
  359.    
  360.     fc={
  361.     ["south"]=0,
  362.     ["west"]=1,
  363.     ["north"]=2,
  364.     ["east"]=3,
  365.     [0]="south",
  366.     [1]="west",
  367.     [2]="north",
  368.     [3]="east"
  369.     };
  370.     function invertFace(facing)
  371.         return (facing+2)%4;
  372.     end
  373.     function face(nf)
  374.         if nf==nil then return false,"Not enough arguments"; end
  375.         local fdif=nf-xt.f;
  376.         if fdif==0 then return true;
  377.         elseif fdif==3 then left();return true;
  378.         elseif fdif==-3 then right(); return true;
  379.         else right(fdif);return true; end
  380.     end
  381.     function gotoX(nx)
  382.         if nx>xt.x then face(3) elseif nx<xt.x then face(1); else return true; end
  383.         local dif=math.abs(nx-xt.x);
  384.         return xt.fw(dif);
  385.     end
  386.     function gotoZ(nz)
  387.         if nz>xt.z then face(0) elseif nz<xt.z then face(2); else return true; end
  388.         local dif=math.abs(nz-xt.z);
  389.         return xt.fw(dif);
  390.     end
  391.     function gotoY(ny)
  392.         local func;
  393.         if ny>xt.y then func=xt.up; elseif ny<xt.y then func=xt.dn; else return true; end
  394.         local dif=math.abs(ny-xt.y);
  395.         return func(dif);
  396.     end
  397.    
  398.     function locate()
  399.         local tx;
  400.         local ty;
  401.         local tz;
  402.         tx,ty,tz=gps.locate();
  403.         if tx==nil or ty==nil or tz==nil then return false; end
  404.         xt.x=tx;
  405.         xt.z=tz;
  406.         saveXZ();
  407.         xt.y=ty;
  408.         saveY();
  409.         return true;
  410.     end
  411.     function calibrate(forward,back,donealready)
  412.         if turtle.getFuelLevel()~="unlimited" then
  413.             if turtle.getFuelLevel()<=2 then return false,"Not enough fuel"; end
  414.         end
  415.         if donealready==nil then donealready=false; end
  416.         if forward==nil then forward=xt.fw; end
  417.         if back==nil then back=xt.bk; end
  418.         local tx;
  419.         local tz;
  420.         if not locate() then return false,"Could not reach GPS"; end
  421.         tx=xt.x;tz=xt.z;
  422.         if not forward() then
  423.             if donealready then return false,"Could not go forward";
  424.             else return calibrate(back,forward,true); end
  425.         end
  426.         if not locate() then back(); return false,"Could not reach GPS"; end
  427.         local dx=xt.x-tx;
  428.         local dz=xt.z-tz;
  429.         if donealready then dx=-dx;dz=-dz; end
  430.         if dz==1 then xt.f=0;
  431.         elseif dz==-1 then xt.f=2;
  432.         elseif dx==1 then xt.f=3;
  433.         elseif dx==-1 then xt.f=1;
  434.         end
  435.         saveF();
  436.         back(); return true;
  437.     end
  438.    
  439.     for k,v in pairs(turtle) do
  440.         if k=="forward" then forward=fw;
  441.         elseif k=="back" then back=bk;
  442.         elseif k=="up" then
  443.         elseif k=="down" then down=dn;
  444.         elseif k=="turnLeft" then turnLeft=left;
  445.         elseif k=="turnRight" then turnRight=right;
  446.         elseif k=="select" then select=sel;
  447.         else
  448.             local env=getfenv();
  449.             env[k]=v;
  450.             setfenv(1,env);
  451.         end
  452.     end
  453.     turtle.orig={};
  454.     for k,v in pairs(turtle) do
  455.         if k~="orig" then
  456.             turtle.orig[k]=v;
  457.         end
  458.     end
  459.     if replaceTurtle then
  460.         turtle.forward=fw;
  461.         turtle.back=bk;
  462.         turtle.up=up;
  463.         turtle.down=dn;
  464.         turtle.turnLeft=left;
  465.         turtle.turnRight=right;
  466.         turtle.select=sel;
  467.         replaceTurtle=nil;
  468.     end
  469. end
Advertisement
Add Comment
Please, Sign In to add comment