derpierre65

turtleutils

Feb 8th, 2013 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.99 KB | None | 0 0
  1. --  f = 0 = norden [y+1]  | f = 1 = osten   [x+1]
  2. --  f = 2 = sueden [y-1]  | f = 3 = westen  [x-1]
  3.  
  4. local way = "way.dat";
  5.  
  6. status = "_home";
  7.  
  8. local function split(pString, pPattern)
  9.     local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  10.     local fpat = "(.-)" .. pPattern
  11.     local last_end = 1
  12.     local s, e, cap = pString:find(fpat, 1)
  13.     while s do
  14.         if s ~= 1 or cap ~= "" then
  15.             table.insert(Table,cap)
  16.         end
  17.         last_end = e+1
  18.         s, e, cap = pString:find(fpat, last_end)
  19.     end
  20.  
  21.     if last_end <= #pString then
  22.         cap = pString:sub(last_end)
  23.         table.insert(Table, cap)
  24.     end
  25.     return Table
  26. end
  27.  
  28. local function savePosition( varName, content, replace )
  29.     if not replace then
  30.         if not fs.exists(way) then
  31.             local file = fs.open(varName .. ".dat", "w" )
  32.             if file then
  33.                 file.write( content )
  34.                 file.close()
  35.             else
  36.                 return false
  37.             end
  38.         else
  39.             return false
  40.         end
  41.     else
  42.         fs.delete(varName .. ".dat" )
  43.         local file = fs.open(varName .. ".dat", "w" )
  44.         if file then
  45.             file.write( content )
  46.             file.close()
  47.         else
  48.             return false
  49.         end
  50.     end
  51.     return true
  52. end
  53.  
  54. local function saveWay(moved, amount)
  55.     if fs.exists(way) then option = "a"; else option = "w"; end  
  56.     local file = fs.open(way, option)
  57.     print("moved: ",moved, " amount: ", amount);
  58.     if ( option == "a" ) then
  59.         file.write(";" .. moved .. "," .. amount);
  60.     else
  61.         file.write(moved .. "," .. amount);
  62.     end
  63.     file.close();
  64. end
  65.  
  66. function getPositionVar( varName )
  67.     if fs.exists(varName .. ".dat") then
  68.         local file = fs.open(varName .. ".dat", "r")
  69.         if file then
  70.             local var = file.readLine()
  71.             file.close()
  72.             return var
  73.         else
  74.             return false
  75.         end
  76.     else
  77.         return false
  78.     end
  79. end
  80.  
  81. position = {}
  82. position["x"] = 0; position["y"] = 0;
  83. position["z"] = 0; position["f"] = 0;
  84. if ( getPositionVar("x") ) then position["x"] = tonumber(getPositionVar("x")); end
  85. if ( getPositionVar("y") ) then position["y"] = tonumber(getPositionVar("y")); end
  86. if ( getPositionVar("z") ) then position["z"] = tonumber(getPositionVar("z")); end
  87. if ( getPositionVar("f") ) then position["f"] = tonumber(getPositionVar("f")); end
  88.  
  89. local function move(k, v)
  90.     v = tonumber(v);
  91.     if ( k ~= nil and v ~= nil ) then
  92.         if ( k == "f" ) then
  93.             if ( v == -1 ) then turnLeft(1,1);
  94.             elseif ( v == 1 ) then turnRight(1,1); end
  95.         end
  96.        
  97.         if ( k == "y" ) then
  98.             if ( v == -1 ) then
  99.                 back(1,1);
  100.             elseif ( v == 1 ) then
  101.                 forward(1,1);
  102.             end
  103.         end
  104.        
  105.         if ( k == "z" ) then
  106.             if ( v == -1 ) then
  107.                 down(1,1);
  108.             elseif ( v == 1 ) then
  109.                 up(1,1);
  110.             end
  111.         end
  112.        
  113.         if ( k == "x") then
  114.             if ( v == 1 ) then
  115.                 back(1,1);
  116.             elseif ( v == -1 ) then
  117.                 forward(1,1);
  118.             end
  119.         end
  120.     end
  121. end
  122.  
  123. function status()
  124.     return status;
  125. end
  126.  
  127. function home()
  128.     if fs.exists(way) then
  129.         status = "_moveHome";
  130.         local file = fs.open(way, "r")
  131.         local mystring = file.readAll();
  132.         local ta = split(mystring, ";")
  133.         for index = #ta, 1, -1 do
  134.             tt = split(ta[index], ",")
  135.             for k, v in pairs(tt) do
  136.                 if ( k == 1 ) then doThis = v;
  137.                 elseif ( k == 2 ) then move1 = v; end
  138.             end
  139.             move(doThis, move1);
  140.         end
  141.         file.close();
  142.         sleep(0.3);
  143.         shell.run("remove way.dat");
  144.         shell.run("remove x.dat");
  145.         shell.run("remove y.dat");
  146.         shell.run("remove z.dat");
  147.         shell.run("remove f.dat");
  148.         status = "_home";
  149.         return status;
  150.     else
  151.         status = "_moveHomeFailed"
  152.         return "_moveHomeFailed";
  153.     end
  154. end
  155.  
  156. function forward(amounts, value)
  157.     if ( amounts == nil ) then amounts = 1; end
  158.     for i = 1, amounts do
  159.         if ( turtle.forward() ) then
  160.             if ( position["f"] == 0 ) then
  161.                 position["y"] = position["y"] + 1;
  162.                 if ( value == nil ) then saveWay("y", -1); end
  163.             elseif ( position["f"] == 1 ) then
  164.                 position["x"] = position["x"] + 1;
  165.                 if ( value == nil ) then saveWay("x", -1); end
  166.             elseif ( position["f"] == 2 ) then
  167.                 position["y"] = position["y"] - 1;
  168.                 if ( value == nil ) then saveWay("y", 1); end
  169.             elseif ( position["f"] == 3 ) then
  170.                 position["x"] = position["x"] - 1;
  171.                 if ( value == nil ) then saveWay("x", 1); end
  172.             end
  173.             savePosition("y", position["y"], true);
  174.             savePosition("x", position["x"], true);
  175.             status = "_isMoving";
  176.         else
  177.             return "_movingFailed";
  178.         end
  179.     end
  180. end
  181.  
  182. function back(amounts, value)
  183.     if ( amounts == nil ) then amounts = 1; end
  184.     for i = 1, amounts do
  185.         if ( turtle.back() ) then
  186.             if ( position["f"] == 0 ) then
  187.                 position["y"] = position["y"] - 1;
  188.                 if ( value == nil ) then saveWay("y", 1); end
  189.             elseif ( position["f"] == 1 ) then
  190.                 position["x"] = position["x"] - 1;
  191.                 if ( value == nil ) then saveWay("x", 1); end
  192.             elseif ( position["f"] == 2 ) then
  193.                 position["y"] = position["y"] + 1;
  194.                 if ( value == nil ) then saveWay("y", -1); end
  195.             elseif ( position["f"] == 3 ) then
  196.                 position["x"] = position["x"] + 1;
  197.                 if ( value == nil ) then saveWay("x", -1); end
  198.             end
  199.             savePosition("y", position["y"], true);
  200.             savePosition("x", position["x"], true);
  201.             status = "_isMoving";
  202.         else
  203.             return "_movingFailed";
  204.         end
  205.     end
  206. end
  207.  
  208. function up(amounts, value)
  209.     if ( amounts == nil ) then amounts = 1; end
  210.     for i = 1, amounts do
  211.         if ( turtle.up() ) then
  212.             position["z"] = position["z"] + 1;
  213.             savePosition("z", position["z"], true);
  214.             if ( value == nil ) then saveWay("z", -1); end
  215.             status = "_isMoving";
  216.         else
  217.             return "_movingFailed";
  218.         end
  219.     end
  220. end
  221.  
  222. function down(amounts, value)
  223.     if ( amounts == nil ) then amounts = 1; end
  224.     for i = 1, amounts do
  225.         if ( turtle.down() ) then
  226.             position["z"] = position["z"] - 1;
  227.             savePosition("z", position["z"], true);
  228.             if ( value == nil ) then saveWay("z", 1); end
  229.             status = "_isMoving";
  230.         else
  231.             return "_movingFailed";
  232.         end
  233.     end
  234. end
  235.  
  236. function turnRight(amounts, value)
  237.     if ( amounts == nil ) then amounts = 1; end
  238.     for i = 1, amounts do
  239.         if ( turtle.turnRight() ) then
  240.             position["f"] = position["f"] + 1;
  241.             if ( position["f"] > 3 ) then position["f"] = 0; end
  242.             if ( value == nil ) then saveWay("f", -1); end
  243.             savePosition("f", position["f"], true);
  244.             status = "_isMoving";
  245.         else
  246.             status = "_movingFailed";
  247.             return false;
  248.         end
  249.     end
  250. end
  251.  
  252. function turnLeft(amounts, value)
  253.     if ( amounts == nil or amounts < 0 ) then amounts = 1; end
  254.     for i = 1, amounts do
  255.         if ( turtle.turnLeft() ) then
  256.             position["f"] = position["f"] - 1;
  257.             if ( position["f"] < 0 ) then position["f"] = 3; end
  258.             if ( value == nil ) then saveWay("f", 1); end
  259.             savePosition("f", position["f"], true);
  260.             status = "_isMoving";
  261.         else
  262.             status = "_movingFailed";
  263.             return false;
  264.         end
  265.     end
  266. end
Advertisement
Add Comment
Please, Sign In to add comment