G2A Many GEOs
SHARE
TWEET

lib_turtle_0.2.3

kssr3951 Sep 28th, 2014 168 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dofile("lib_log");
  2.  
  3. userEventHandler = nil;
  4.  
  5. local function myPlace(para1)
  6.   if para1 == 0 then
  7.     return turtle.placeUp();
  8.   elseif para1 == 1 then
  9.     return turtle.place();
  10.   elseif para1 == 2 then
  11.     return turtle.placeDown();
  12.   end
  13. end
  14.  
  15. local function myThrow(para1, para2)
  16.   if para1 == 0 then
  17.     return turtle.dropUp(para2);
  18.   elseif para1 == 1 then
  19.     return turtle.drop(para2);
  20.   elseif para1 == 2 then
  21.     return turtle.dropDown(para2);
  22.   end
  23. end
  24.  
  25. local function myExcavate(para1)
  26.   if para1 == 0 then
  27.     if turtle.detectUp() then
  28.       for i = 1, 40 do
  29.         turtle.digUp();
  30.         sleep(0.5);
  31.         if not turtle.detectUp() then
  32.           return true;
  33.         end
  34.       end
  35.       return false;
  36.     else
  37.       return true;
  38.     end
  39.   elseif para1 == 1 then
  40.     if turtle.detect() then
  41.       for i = 0, 9 do
  42.         turtle.dig();
  43.         sleep(0);
  44.         if false == turtle.detect() then
  45.           return true;
  46.         end
  47.       end
  48.       for i = 0, 9 do
  49.         turtle.dig();
  50.         sleep(0.5);
  51.         if false == turtle.detect() then
  52.           return true;
  53.         end
  54.       end
  55.       return false;
  56.     else
  57.       return true;
  58.         end
  59.   elseif para1 == 2 then
  60.     if turtle.detectDown() then
  61.       return turtle.digDown();
  62.     else
  63.       return true;
  64.     end
  65.   end
  66. end
  67.  
  68. function myDigUp()
  69.   return myExcavate(0);
  70. end
  71.  
  72. function myDig()
  73.   return myExcavate(1);
  74. end
  75.  
  76. function myDigDown()
  77.   return myExcavate(2);
  78. end
  79.  
  80. local function myZzz(para1)
  81.   sleep(para1 / 1000);
  82. end
  83.  
  84. local function myOutput(para1, para2)
  85.   local dir = { [0] = "top",  [1] = "front", [2] = "bottom",
  86.                 [3] = "left", [4] = "back",  [5] = "right" };
  87.   if para2 == 1 then
  88.     rs.setOutput(dir[para1], true);
  89.   else
  90.     rs.setOutput(dir[para1], false);
  91.   end
  92. end
  93.  
  94. local function mySuck(para1)
  95.   if para1 == 0 then
  96.     return turtle.suckUp();
  97.   elseif para1 == 1 then
  98.     return turtle.suck();
  99.   elseif para1 == 2 then
  100.     return turtle.suckDown();
  101.   end
  102. end
  103.  
  104. local function myAttack(para1)
  105.   if para1 == 0 then
  106.     return turtle.attackUp();
  107.   elseif para1 == 1 then
  108.     return turtle.attack();
  109.   elseif para1 == 2 then
  110.     return turtle.attackDown();
  111.   end
  112. end
  113.  
  114. function refuelPrompt(moveFunc)
  115.   local firstTime = true;
  116.   while true do
  117.     local rslt, msg;
  118.     if nil ~= moveFunc then
  119.       rslt, msg = moveFunc();
  120.     else
  121.       if firstTime then
  122.         firstTime = false;
  123.         rslt = false;
  124.         msg  = "Out of fuel";
  125.       else
  126.         return true;
  127.       end
  128.     end
  129.     if not rslt and msg == "Out of fuel" then
  130.       print("Out of fuel. please select [r]efuel or [q]uit.");
  131.       print("(r/q)");
  132.       local ch = read();
  133.       if "q" == ch then
  134.         print("Are you sure you want to stop the script?");
  135.         print("(y/n)");
  136.         ch = read();
  137.         if "q" == ch then
  138.           error("out of fuel.");
  139.         end
  140.       elseif "r" == ch then
  141.         local selSlot = 1;
  142.         while true do
  143.           print("Please type current selected slot number.");
  144.           ch = read();
  145.           local num = tonumber(ch);
  146.           if nil ~= num and 1 <= num and num <= 16 then
  147.             selSlot = num;
  148.             break;
  149.           else
  150.             print("Wrong input. Please retry.");
  151.           end
  152.         end
  153.         while true do
  154.           print("please set fuel item and type slot number.");
  155.           print("When you finish refueling, Please type q.");
  156.           print("FuelLevel = " .. turtle.getFuelLevel());
  157.           ch = read();
  158.           local num = tonumber(ch);
  159.           if nil ~= num and 1 <= num and num <= 16 then
  160.             turtle.select(num);
  161.             local fRslt, fMsg = turtle.refuel();
  162.             if not fRslt then
  163.               print(fMsg);
  164.             end
  165.           elseif "q" == ch then
  166.             turtle.select(selSlot);
  167.             break;
  168.           else
  169.             print("Wrong input. Please retry.");
  170.           end
  171.         end
  172.       end
  173.     else
  174.       return rslt, msg;
  175.     end
  176.   end
  177. end
  178.  
  179. local function myMove(dirName, moveFunc, detectFunc, attackFunc)
  180.   local rslt, msg;
  181.   while true do
  182.     --rslt, msg = moveFunc();
  183.     rslt, msg = refuelPrompt(moveFunc);
  184.     if rslt then
  185.       return true;
  186.     else
  187.       if moveFunc == turtle.back then
  188.         turtle.turnRight();
  189.         turtle.turnRight();
  190.       end
  191.       if detectFunc() then
  192.         if moveFunc == turtle.back then
  193.           turtle.turnLeft();
  194.           turtle.turnLeft();
  195.         end
  196.         return false;
  197.       else
  198.         for i = 0, 9 do
  199.           attackFunc();
  200.           attackFunc();
  201.           attackFunc();
  202.           if moveFunc == turtle.back then
  203.             if true == turtle.forward() then
  204.               turtle.turnLeft();
  205.               turtle.turnLeft();
  206.               return true;
  207.             end
  208.           else
  209.             --if true == moveFunc() then
  210.             if true == refuelPrompt(moveFunc) then
  211.               return true;
  212.             end
  213.           end
  214.         end
  215.         if moveFunc == turtle.back then
  216.           turtle.turnLeft();
  217.           turtle.turnLeft();
  218.         end
  219.         while true do
  220.           print("move [" .. dirName .. "] failed. retry? (y/n)");
  221.           local input = read();
  222.           if "y" == input then
  223.             break;
  224.           elseif "n" == input then
  225.             error("program stopped by user request.");
  226.           else
  227.             print("wrong input. type y or n.");
  228.           end
  229.         end
  230.       end
  231.     end
  232.   end
  233. end
  234.  
  235. function myForward()
  236.   return myMove("forward", turtle.forward, turtle.detect, turtle.attack);
  237. end
  238.  
  239. function myBack()
  240.   return myMove("back", turtle.back, turtle.detect, turtle.attack);
  241. end
  242.  
  243. function myUp()
  244.   return myMove("up", turtle.up, turtle.detectUp, turtle.attackUp);
  245. end
  246.  
  247. function myDown()
  248.   return myMove("down", turtle.down, turtle.detectDown, turtle.attackDown);
  249. end
  250.  
  251. local function myUserEvent(para1)
  252.   if nil ~= userEventHandler then
  253.     userEventHandler(para1);
  254.   end
  255. end
  256.  
  257. local cmdInfo =
  258. {
  259.   f = { paraCnt = 0, func = myForward;        }, -- [f]orward
  260.   b = { paraCnt = 0, func = myBack;           }, -- [b]ack
  261.   l = { paraCnt = 0, func = turtle.turnLeft;  }, -- turn [l]eft
  262.   r = { paraCnt = 0, func = turtle.turnRight; }, -- turn [r]ight
  263.   u = { paraCnt = 0, func = myUp;             }, -- [u]p
  264.   d = { paraCnt = 0, func = myDown;           }, -- [d]own
  265.   s = { paraCnt = 1, func = turtle.select;    }, -- [s]elect
  266.   p = { paraCnt = 1, func = myPlace;          }, -- [p]lace
  267.   t = { paraCnt = 2, func = myThrow;          }, -- [t]hrow = drop
  268.   e = { paraCnt = 1, func = myExcavate;       }, -- [e]xcavate = dig
  269.   z = { paraCnt = 1, func = myZzz;            }, -- [z]zz = sleep
  270.   o = { paraCnt = 2, func = myOutput;         }, -- set [o]utput (redstone)
  271.   k = { paraCnt = 1, func = mySuck;           }, -- suc[k]
  272.   a = { paraCnt = 1, func = myAttack;         }, -- [a]ttack
  273.   v = { paraCnt = 1, func = myUserEvent;      }, -- user e[v]ent
  274. };
  275.  
  276. local function getParam(command, idx)
  277.   local buff = "";
  278.   local ch = "";
  279.  
  280.   for i = idx, command:len() do
  281.     ch = command:sub(i, i);
  282.     if ch == "," then
  283.       if buff == "" then
  284.         return nil, i + 1;
  285.       else
  286.         return tonumber(buff), i + 1;
  287.       end
  288.     elseif nil ~= string.find("0123456789", ch, 1, true) then
  289.       buff = buff .. ch;
  290.     else
  291.       if buff ~= "" then
  292.         return tonumber(buff), i;
  293.       end
  294.     end
  295.   end
  296.   if buff == "" then
  297.     return nil, command:len() + 1;
  298.   else
  299.     return tonumber(buff), command:len() + 1;
  300.   end
  301. end
  302.  
  303. local function get1Cmd(command, idx)
  304.   local cmd = string.sub(command, idx, idx);
  305.   local para1, para2, nextIdx, paraCnt;
  306.  
  307.   if nil ~= cmdInfo[cmd] then
  308.     paraCnt = cmdInfo[cmd].paraCnt;
  309.   else
  310.     error("unknown command [" .. cmd .. "]");
  311.   end
  312.  
  313.   if -1 == paraCnt then
  314.     return nil, nil, nil, idx + 1;
  315.   elseif paraCnt == 0 then
  316.     return cmd, nil, nil, idx + 1;
  317.   elseif paraCnt == 1 then
  318.     para1, nextIdx = getParam(command, idx + 1);
  319.     return cmd, para1, nil, nextIdx;
  320.   elseif paraCnt == 2 then
  321.     para1, nextIdx = getParam(command, idx + 1);
  322.     para2, nextIdx = getParam(command, nextIdx);
  323.     return cmd, para1, para2, nextIdx;
  324.   end
  325. end
  326.  
  327. local function checkLoop(command)
  328.   local kBgn = {""};
  329.   local loopInfo = {};
  330.  
  331.   local ch, repeatCnt, nextIdx, kBgnIdx;
  332.   for i = 1, command:len() do
  333.     ch = command:sub(i, i);
  334.     if "(" == ch then
  335.       table.insert(kBgn, i);
  336.     elseif ")" == ch then
  337.       repeatCnt, nextIdx = getParam(command, i + 1);
  338.       loopInfo[i] = { beginIdx   = table.remove(kBgn),
  339.                       endIdx     = i,
  340.                       nextIdx    = nextIdx,
  341.                       repeatCnt  = repeatCnt,
  342.                       currentCnt = 0 };
  343.       i = nextIdx;
  344.     end
  345.   end
  346.   return loopInfo;
  347. end
  348.  
  349. function doCommand(command)
  350.  
  351.   command = command .. " ";
  352.   local loopInfo = checkLoop(command);
  353.  
  354.   local cmd, para1, para2, nextIdx, ch;
  355.  
  356.   local i = 1;
  357.   while true do
  358.     ch = command:sub(i, i);
  359.     if ")" == ch then
  360.       loopInfo[i].currentCnt = loopInfo[i].currentCnt + 1;
  361.       if loopInfo[i].repeatCnt <= loopInfo[i].currentCnt then
  362.         loopInfo[i].currentCnt = 0;
  363.         i = loopInfo[i].nextIdx;
  364.       else
  365.         i = loopInfo[i].beginIdx;
  366.       end
  367.     elseif " " == ch then
  368.       i = i + 1;
  369.     elseif "\n" == ch then
  370.       i = i + 1;
  371.     elseif "(" == ch then
  372.       i = i + 1;
  373.     else
  374.       cmd, para1, para2, nextIdx = get1Cmd(command, i);
  375.       i = nextIdx;
  376.  
  377.       if cmd ~= nil then
  378.         cmdInfo[cmd].func(para1, para2);
  379.       end
  380.     end
  381.     if command:len() <= i then
  382.       break;
  383.     end
  384.   end
  385. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top