negamartin

XT 1.6 by negamartin

Sep 5th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.69 KB | None | 0 0
  1. --XT V1.6 by NEGAMARTIN
  2. version=1.6;
  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.     handleBlocking=function() sleep(0.4); end
  99.     fuelUpdated=false;
  100.     if autoSave==nil then autoSave=true; end
  101.     if replaceTurtle==nil then replaceTurtle=true; end
  102.    
  103.     local err=false;
  104.     err=pcall(function()
  105.         if fs.exists("xtdata") then
  106.             if not fs.isDir("xtdata") then
  107.                 fs.move("xtdata","copyofxtdata");
  108.                 fs.makeDir("xtdata");
  109.             end
  110.         else
  111.             fs.makeDir("xtdata");
  112.         end
  113.     end);
  114.     err=err or pcall(function()
  115.         if fs.exists("xtdata/xz") then
  116.             local file=fs.open("xtdata/xz","r");
  117.             x=math.floor(file.readLine());
  118.             z=math.floor(file.readLine());
  119.             file.close();
  120.         end
  121.     end);
  122.     err=err or pcall(function()
  123.         if fs.exists("xtdata/y") then
  124.             local file=fs.open("xtdata/y","r");
  125.             y=math.floor(file.readLine());
  126.             file.close();
  127.         end
  128.     end);
  129.     err=err or pcall(function()
  130.         if fs.exists("xtdata/f") then
  131.             local file=fs.open("xtdata/f","r");
  132.             f=math.floor(file.readLine());
  133.             file.close();
  134.         end
  135.     end);
  136.     err=err or pcall(function()
  137.         if fs.exists("xtdata/s") then
  138.             local file=fs.open("xtdata/s","r");
  139.             s=math.floor(file.readLine());
  140.             turtle.select(s);
  141.             file.close();
  142.         end
  143.     end);
  144.     err=err or pcall(function()
  145.         if fs.exists("xtdata/autosave") then
  146.             local file=fs.open("xtdata/autosave","r");
  147.             local tas=math.floor(file.readLine());
  148.             if tas==1 then autoSave=true;
  149.             elseif tas==0 then autoSave=false; end
  150.             file.close();
  151.         end
  152.     end);
  153.    
  154.     function saveXZ()
  155.         if not autoSave then return; end
  156.         local file=fs.open("xtdata/xz","w");
  157.         file.writeLine(xt.x);
  158.         file.writeLine(xt.z);
  159.         file.close();
  160.     end
  161.     function saveY()
  162.         if not autoSave then return; end
  163.         local file=fs.open("xtdata/y","w");
  164.         file.writeLine(xt.y);
  165.         file.close();
  166.     end
  167.     function saveF()
  168.         if not autoSave then return; end
  169.         local file=fs.open("xtdata/f","w");
  170.         file.writeLine(xt.f);
  171.         file.close();
  172.     end
  173.     function saveS()
  174.         if not autoSave then return; end
  175.         local file=fs.open("xtdata/s","w");
  176.         file.writeLine(xt.s);
  177.         file.close();
  178.     end
  179.     function savePos()
  180.         if not autoSave then return; end
  181.         saveXZ();
  182.         saveY();
  183.         saveF();
  184.     end
  185.     function save()
  186.         if not autoSave then return; end
  187.         saveXZ();
  188.         saveY();
  189.         saveF();
  190.         saveS();
  191.     end
  192.     function setAutoSave(nas)
  193.         autoSave=nas;
  194.         local file=fs.open("xtdata/autosave","w");
  195.         local tempAS;
  196.         if autoSave then tempAS=1; else tempAS=0; end
  197.         file.writeLine(tempAS);
  198.         file.close();
  199.     end
  200.    
  201.     function getFX(f)
  202.         f=f or xt.f;
  203.         if f==1 then return -1;
  204.         elseif f==3 then return 1;
  205.         else return 0; end
  206.     end
  207.     function getFZ(f)
  208.         f=f or xt.f;
  209.         if f==0 then return 1;
  210.         elseif f==2 then return -1;
  211.         else return 0; end
  212.     end
  213.    
  214.     function fw(amount)
  215.         if amount==nil then amount=1; end
  216.         if amount<0 then bk(-amount); end
  217.         for i=1,amount do
  218.             if turtle.orig.forward() then
  219.                 xt.x=xt.x+getFX();
  220.                 xt.z=xt.z+getFZ();
  221.                 xt.saveXZ();
  222.                 fuelUpdated=false;
  223.             else
  224.                 return false;
  225.             end
  226.         end
  227.         return true;
  228.     end
  229.     function ffw(amount,...)
  230.         if amount==nil then amount=1; end
  231.         if amount<0 then bk(-amount); end
  232.         for i=1,amount do
  233.             while not turtle.orig.forward() do
  234.                 xt.handleBlocking("fw",...);
  235.             end
  236.             xt.x=xt.x+getFX();
  237.             xt.z=xt.z+getFZ();
  238.             xt.saveXZ();
  239.             fuelUpdated=false;
  240.         end
  241.         return true;
  242.     end
  243.     function bk(amount)
  244.         if amount==nil then amount=1; end
  245.         if amount<0 then fw(-amount); end
  246.         for i=1,amount do
  247.             if turtle.orig.back() then
  248.                 xt.x=xt.x-getFX();
  249.                 xt.z=xt.z-getFZ();
  250.                 xt.saveXZ();
  251.                 fuelUpdated=false;
  252.             else
  253.                 return false;
  254.             end
  255.         end
  256.         return true;
  257.     end
  258.     function fbk(amount,...)
  259.         if amount==nil then amount=1; end
  260.         if amount<0 then fw(-amount); end
  261.         for i=1,amount do
  262.             while not turtle.orig.back() do
  263.                 xt.handleBlocking("bk",...);
  264.             end
  265.             xt.x=xt.x-getFX();
  266.             xt.z=xt.z-getFZ();
  267.             xt.saveXZ();
  268.             fuelUpdated=false;
  269.         end
  270.         return true;
  271.     end
  272.    
  273.     function up(amount)
  274.         if amount==nil then amount=1; end
  275.         if amount<0 then dn(-amount); end
  276.         for i=1,amount do
  277.             if turtle.orig.up() then
  278.                 xt.y=xt.y+1;
  279.                 xt.saveY();
  280.                 fuelUpdated=false;
  281.             else
  282.                 return false;
  283.             end
  284.         end
  285.         return true;
  286.     end
  287.     function fup(amount,...)
  288.         if amount==nil then amount=1; end
  289.         if amount<0 then dn(-amount); end
  290.         for i=1,amount do
  291.             while not turtle.orig.up() do
  292.                 xt.handleBlocking("up",...);
  293.             end
  294.             xt.y=xt.y+1;
  295.             xt.saveY();
  296.             fuelUpdated=false;
  297.         end
  298.         return true;
  299.     end
  300.     function dn(amount)
  301.         if amount==nil then amount=1; end
  302.         if amount<0 then up(-amount); end
  303.         for i=1,amount do
  304.             if turtle.orig.down() then
  305.                 xt.y=xt.y-1;
  306.                 xt.saveY();
  307.                 fuelUpdated=false;
  308.             else
  309.                 return false;
  310.             end
  311.         end
  312.         return true;
  313.     end
  314.     function fdn(amount,...)
  315.         if amount==nil then amount=1; end
  316.         if amount<0 then up(-amount); end
  317.         for i=1,amount do
  318.             while not turtle.orig.down() do
  319.                 xt.handleBlocking("dn",...);
  320.             end
  321.             xt.y=xt.y-1;
  322.             xt.saveY();
  323.             fuelUpdated=false;
  324.         end
  325.         return true;
  326.     end
  327.    
  328.     function left(amount)
  329.         if amount==nil then amount=1; end
  330.         if amount<0 then right(-amount); end
  331.         for i=1,amount do
  332.             turtle.orig.turnLeft();
  333.             xt.f=(xt.f-1)%4;
  334.             xt.saveF();
  335.         end
  336.         return true;
  337.     end
  338.     function right(amount)
  339.         if amount==nil then amount=1; end
  340.         if amount<0 then left(-amount); end
  341.         for i=1,amount do
  342.             turtle.orig.turnRight();
  343.             xt.f=(xt.f+1)%4;
  344.             xt.saveF();
  345.         end
  346.         return true;
  347.     end
  348.    
  349.     function sel(slot)
  350.         if slot~=xt.s then
  351.             if turtle.orig.select(slot) then
  352.                 xt.s=slot;
  353.                 xt.saveS();
  354.             end
  355.         end
  356.         return true;
  357.     end
  358.     function inspect()
  359.         return ({turtle.orig.inspect()})[2];
  360.     end
  361.     function inspectUp()
  362.         return ({turtle.orig.inspectUp()})[2];
  363.     end
  364.     function inspectDn()
  365.         return ({turtle.orig.inspectDown()})[2];
  366.     end
  367.     function getFuel()
  368.         if not fuelUpdated then fuelUpdated=xt.getFuelLevel(); end
  369.         return fuelUpdated;
  370.     end
  371.    
  372.     fc={
  373.     ["south"]=0,
  374.     ["west"]=1,
  375.     ["north"]=2,
  376.     ["east"]=3,
  377.     [0]="south",
  378.     [1]="west",
  379.     [2]="north",
  380.     [3]="east",
  381.     ["zplus"]=0,
  382.     ["xminus"]=1,
  383.     ["zminus"]=2,
  384.     ["xplus"]=3,
  385.     };
  386.     function invertFace(facing)
  387.         return (facing+2)%4;
  388.     end
  389.     function face(nf)
  390.         if nf==nil then return false,"Not enough arguments"; end
  391.         local fdif=nf-xt.f;
  392.         if fdif==0 then return true;
  393.         elseif fdif==3 then left();return true;
  394.         elseif fdif==-3 then right(); return true;
  395.         else right(fdif);return true; end
  396.     end
  397.     function gotoX(nx)
  398.         if nx>xt.x+0.1 then face(3) elseif nx<xt.x-0.1 then face(1); else return true; end
  399.         local dif=math.abs(nx-xt.x);
  400.         return xt.fw(dif);
  401.     end
  402.     function gotoZ(nz)
  403.         if nz>xt.z+0.1 then face(0) elseif nz<xt.z-0.1 then face(2); else return true; end
  404.         local dif=math.abs(nz-xt.z);
  405.         return xt.fw(dif);
  406.     end
  407.     function gotoXZ(nx,nz)
  408.         local fx=getFX();
  409.         local fz=getFZ();
  410.         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
  411.             gotoZ(nz);
  412.             gotoX(nx);
  413.         else
  414.             gotoX(nx);
  415.             gotoZ(nz);
  416.         end
  417.     end
  418.     function gotoY(ny)
  419.         local func;
  420.         if ny>xt.y then func=xt.up; elseif ny<xt.y then func=xt.dn; else return true; end
  421.         local dif=math.abs(ny-xt.y);
  422.         return func(dif);
  423.     end
  424.     function gotoXZY(nx,ny,nz)
  425.         gotoXZ(nx,nz);
  426.         gotoY(ny);
  427.     end
  428.     function gotoYXZ(nx,ny,nz)
  429.         gotoY(ny);
  430.         gotoXZ(nx,nz);
  431.     end
  432.  
  433.     function gotoXDig(nx)
  434.         if nx>xt.x+0.1 then face(3) elseif nx<xt.x-0.1 then face(1); else return true; end
  435.         local dif=math.abs(nx-xt.x);
  436.         for i=1,dif do
  437.             while not xt.fw() do
  438.                 xt.attack();
  439.                 xt.dig();
  440.             end
  441.         end
  442.     end
  443.     function gotoZDig(nz)
  444.         if nz>xt.z+0.1 then face(0) elseif nz<xt.z-0.1 then face(2); else return true; end
  445.         local dif=math.abs(nz-xt.z);
  446.         for i=1,dif do
  447.             while not xt.fw() do
  448.                 xt.attack();
  449.                 xt.dig();
  450.             end
  451.         end
  452.     end
  453.     function gotoYDig(ny)
  454.         local func;
  455.         local fDig;
  456.         local fAtk;
  457.         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
  458.         local dif=math.abs(ny-xt.y);
  459.         for i=1,dif do
  460.             while not func() do
  461.                 fAtk();
  462.                 fDig();
  463.             end
  464.         end
  465.     end
  466.     function gotoXZDig(nx,nz)
  467.         local fx=getFX();
  468.         local fz=getFZ();
  469.         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
  470.             gotoZDig(nz);
  471.             gotoXDig(nx);
  472.         else
  473.             gotoXDig(nx);
  474.             gotoZDig(nz);
  475.         end
  476.     end
  477.     function gotoXZYDig(nx,ny,nz)
  478.         gotoXZDig(nx,nz);
  479.         gotoYDig(ny);
  480.     end
  481.     function gotoYXZDig(nx,ny,nz)
  482.         gotoYDig(ny);
  483.         gotoXZDig(nx,nz);
  484.     end
  485.    
  486.     function navigate(coord,goal)
  487.         local movePlus=true;
  488.         if coord=="x" then
  489.             if goal<xt.x then
  490.                 xt.face(xt.fc.xminus);
  491.                 movePlus=false;
  492.             elseif goal>xt.x then
  493.                 xt.face(xt.fc.xplus);
  494.                 movePlus=true;
  495.             else return; end
  496.         elseif coord=="z" then
  497.             if goal<xt.z then
  498.                 xt.face(xt.fc.zminus);
  499.                 movePlus=false;
  500.             elseif goal>xt.z then
  501.                 xt.face(xt.fc.zplus);
  502.                 movePlus=true;
  503.             else return; end
  504.         else error("Invalid navigate coord",2); end
  505.         while (movePlus and xt[coord]-0.1<goal) or (not movePlus and xt[coord]-0.1>goal) do
  506.             repeat
  507.                 xt.attackDown();
  508.             until not xt.dn();
  509.             while not xt.fw() do
  510.                 xt.attack();
  511.                 while not xt.up() do
  512.                     xt.attackUp();
  513.                     while not xt.bk() do
  514.                         xt.dn();
  515.                     end
  516.                 end
  517.             end
  518.         end
  519.     end
  520.     function navXZ(nx,nz)
  521.         local fx=getFX();
  522.         local fz=getFZ();
  523.         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
  524.             navigate("z",nz);
  525.             navigate("x",nx);
  526.         else
  527.             navigate("x",nx);
  528.             navigate("z",nz);
  529.         end
  530.     end
  531.    
  532.     function locate()
  533.         local tx;
  534.         local ty;
  535.         local tz;
  536.         tx,ty,tz=gps.locate();
  537.         if tx==nil or ty==nil or tz==nil then return false; end
  538.         xt.x=tx;
  539.         xt.z=tz;
  540.         saveXZ();
  541.         xt.y=ty;
  542.         saveY();
  543.         return true;
  544.     end
  545.     function calibrate(forward,back,donealready)
  546.         if turtle.getFuelLevel()~="unlimited" then
  547.             if turtle.getFuelLevel()<=2 then return false,"Not enough fuel"; end
  548.         end
  549.         if donealready==nil then donealready=false; end
  550.         if forward==nil then forward=xt.fw; end
  551.         if back==nil then back=xt.bk; end
  552.         local tx;
  553.         local tz;
  554.         if not locate() then return false,"Could not reach GPS"; end
  555.         tx=xt.x;tz=xt.z;
  556.         if not forward() then
  557.             if donealready then return false,"Could not go forward";
  558.             else return calibrate(back,forward,true); end
  559.         end
  560.         if not locate() then back(); return false,"Could not reach GPS"; end
  561.         local dx=xt.x-tx;
  562.         local dz=xt.z-tz;
  563.         if donealready then dx=-dx;dz=-dz; end
  564.         if dz==1 then xt.f=0;
  565.         elseif dz==-1 then xt.f=2;
  566.         elseif dx==1 then xt.f=3;
  567.         elseif dx==-1 then xt.f=1;
  568.         end
  569.         saveF();
  570.         back(); return true;
  571.     end
  572.    
  573.     forceForward=ffw;
  574.     forceBack=fbk;
  575.     forceUp=fup;
  576.     forceDown=fdown;
  577.     for k,v in pairs(turtle) do
  578.         if k=="forward" then forward=fw;
  579.         elseif k=="back" then back=bk;
  580.         elseif k=="up" then
  581.         elseif k=="down" then down=dn;
  582.         elseif k=="turnLeft" then turnLeft=left;
  583.         elseif k=="turnRight" then turnRight=right;
  584.         elseif k=="select" then select=sel;
  585.         elseif k=="inspect" then
  586.         elseif k=="inspectUp" then
  587.         elseif k=="inspectDown" then inspectDown=inspectDn;
  588.         elseif k=="attack" then attack=v;atk=v;
  589.         elseif k=="attackUp" then attackUp=v;atkUp=v;
  590.         elseif k=="attackDown" then attackDown=v;atkDn=v;
  591.         elseif k=="digDown" then digDown=v;digDn=v;
  592.         else
  593.             local env=getfenv();
  594.             env[k]=v;
  595.             setfenv(1,env);
  596.         end
  597.     end
  598.     turtle.orig={};
  599.     for k,v in pairs(turtle) do
  600.         if k~="orig" then
  601.             turtle.orig[k]=v;
  602.         end
  603.     end
  604.     if replaceTurtle then
  605.         turtle.forward=fw;
  606.         turtle.back=bk;
  607.         turtle.up=up;
  608.         turtle.down=dn;
  609.         turtle.turnLeft=left;
  610.         turtle.turnRight=right;
  611.         turtle.select=sel;
  612.         replaceTurtle=nil;
  613.     end
  614. end
Advertisement
Add Comment
Please, Sign In to add comment