negamartin

XT 1.5 by negamartin

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