Advertisement
kssr3951

lib_turtle_0.2.4

Jun 10th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.48 KB | None | 0 0
  1. dofile("lib_log");
  2.  
  3. userEventHandler = nil;
  4. allowDigTurtle = false;                                -- <<< 1 >>>
  5.  
  6. local lastDigDir = -1;
  7.  
  8. local function myPlaceFunc(para1)
  9.   lastDigDir = -1;                                     -- <<< 2 >>>
  10.   if para1 == 0 then
  11.     return turtle.placeUp();
  12.   elseif para1 == 1 then
  13.     return turtle.place();
  14.   elseif para1 == 2 then
  15.     return turtle.placeDown();
  16.   end
  17. end
  18.  
  19. function myPlaceUp()
  20.   return myPlaceFunc(0);
  21. end
  22.  
  23. function myPlace()
  24.   return myPlaceFunc(1);
  25. end
  26.  
  27. function myPlaceDown()
  28.   return myPlaceFunc(2);
  29. end
  30.  
  31. local function myThrow(para1, para2)
  32.   if para1 == 0 then
  33.     return turtle.dropUp(para2);
  34.   elseif para1 == 1 then
  35.     return turtle.drop(para2);
  36.   elseif para1 == 2 then
  37.     return turtle.dropDown(para2);
  38.   end
  39. end
  40.  
  41. local function myExcavateZzz(para1)
  42.   lastDigDir = para1;                              -- <<< 2 >>>
  43.   if para1 == 0 then
  44.     if turtle.detectUp() then
  45.       for i = 1, 128 do
  46.         if allowDigTurtle or                       -- <<< 1 >>>
  47.            (                                       -- <<< 1 >>>
  48.              turtle.detectUp() and                 -- <<< 1 >>>
  49.              "turtle" ~= peripheral.getType("top") -- <<< 1 >>>
  50.            ) then                                  -- <<< 1 >>>
  51.           turtle.digUp();
  52.         else                                       -- <<< 1 >>>
  53.           sleep("0.5");                            -- <<< 1 >>>
  54.         end                                        -- <<< 1 >>>
  55.         sleep(0.5);
  56.         if not turtle.detectUp() then
  57.           return true;
  58.         end
  59.       end
  60.       return false;
  61.     else
  62.       return true;
  63.     end
  64.   elseif para1 == 1 then
  65.     if turtle.detect() then
  66.       for h = 0, 3 do
  67.         for i = 1, 20 do
  68.           if allowDigTurtle or                         -- <<< 1 >>>
  69.              (                                         -- <<< 1 >>>
  70.                turtle.detect() and                     -- <<< 1 >>>
  71.                "turtle" ~= peripheral.getType("front") -- <<< 1 >>>
  72.              ) then                                    -- <<< 1 >>>
  73.             turtle.dig();
  74.           else                                         -- <<< 1 >>>
  75.             sleep(1);                                  -- <<< 1 >>>
  76.           end                                          -- <<< 1 >>>
  77.           sleep(0.5 * h);
  78.           if false == turtle.detect() then
  79.             break;                                     -- <<< 2 >>>
  80.           end
  81.         end
  82.       end
  83.       return not turtle.detect();                      -- <<< 2 >>>
  84.     else
  85.       return true;
  86.     end
  87.   elseif para1 == 2 then
  88.     if turtle.detectDown() then
  89.       for i = 0, 9 do                                 -- <<< 1 >>>
  90.         if allowDigTurtle or                          -- <<< 1 >>>
  91.            (                                          -- <<< 1 >>>
  92.              turtle.detectDown() and                  -- <<< 1 >>>
  93.              "turtle" ~= peripheral.getType("bottom") -- <<< 1 >>>
  94.            ) then                                     -- <<< 1 >>>
  95.           return turtle.digDown();
  96.         else                                          -- <<< 1 >>>
  97.           sleep(5);                                   -- <<< 1 >>>
  98.         end                                           -- <<< 1 >>>
  99.       end                                             -- <<< 1 >>>
  100.       return false;                                   -- <<< 1 >>>
  101.     else
  102.       return true;
  103.     end
  104.   end
  105. end
  106.  
  107. local function myExcavate(para1)
  108.   lastDigDir = para1;                              -- <<< 2 >>>
  109.   if para1 == 0 then
  110.     if turtle.detectUp() then
  111.       for i = 1, 128 do
  112.         if allowDigTurtle or                       -- <<< 1 >>>
  113.            (                                       -- <<< 1 >>>
  114.              turtle.detectUp() and                 -- <<< 1 >>>
  115.              "turtle" ~= peripheral.getType("top") -- <<< 1 >>>
  116.            ) then                                  -- <<< 1 >>>
  117.           turtle.digUp();
  118.         else                                       -- <<< 1 >>>
  119.           sleep("0.5");                            -- <<< 1 >>>
  120.         end                                        -- <<< 1 >>>
  121.         sleep(0.5);
  122.         if not turtle.detectUp() then
  123.           return true;
  124.         end
  125.       end
  126.       return false;
  127.     else
  128.       return true;
  129.     end
  130.   elseif para1 == 1 then
  131.     if turtle.detect() then
  132.       if allowDigTurtle or                         -- <<< 1 >>>
  133.          (                                         -- <<< 1 >>>
  134.            turtle.detect() and                     -- <<< 1 >>>
  135.            "turtle" ~= peripheral.getType("front") -- <<< 1 >>>
  136.          ) then                                    -- <<< 1 >>>
  137.         return turtle.dig();
  138.       end                                          -- <<< 1 >>>
  139.     else
  140.       return true;
  141.     end
  142.   elseif para1 == 2 then
  143.     if turtle.detectDown() then
  144.       if allowDigTurtle or                          -- <<< 1 >>>
  145.          (                                          -- <<< 1 >>>
  146.            turtle.detectDown() and                  -- <<< 1 >>>
  147.            "turtle" ~= peripheral.getType("bottom") -- <<< 1 >>>
  148.          ) then                                     -- <<< 1 >>>
  149.         return turtle.digDown();
  150.       end                                           -- <<< 1 >>>
  151.     else
  152.       return true;
  153.     end
  154.   end
  155. end
  156.  
  157. function myDigUp()
  158.   return myExcavate(0);
  159. end
  160.  
  161. function myDig()
  162.   return myExcavate(1);
  163. end
  164.  
  165. function myDigDown()
  166.   return myExcavate(2);
  167. end
  168.  
  169. function myTurnLeft()
  170.   lastDigDir = -1;                              -- <<< 2 >>>
  171.   turtle.turnLeft();
  172. end
  173.  
  174. function myTurnRight()
  175.   lastDigDir = -1;                              -- <<< 2 >>>
  176.   turtle.turnRight();
  177. end
  178.  
  179. local function myZzz(para1)
  180.   sleep(para1 / 1000);
  181. end
  182.  
  183. local function myOutput(para1, para2)
  184.   local dir = { [0] = "top",  [1] = "front", [2] = "bottom",
  185.                 [3] = "left", [4] = "back",  [5] = "right" };
  186.   if para2 == 1 then
  187.     rs.setOutput(dir[para1], true);
  188.   else
  189.     rs.setOutput(dir[para1], false);
  190.   end
  191. end
  192.  
  193. local function mySuck(para1)
  194.   if para1 == 0 then
  195.     return turtle.suckUp();
  196.   elseif para1 == 1 then
  197.     return turtle.suck();
  198.   elseif para1 == 2 then
  199.     return turtle.suckDown();
  200.   end
  201. end
  202.  
  203. local function myAttack(para1)
  204.   if para1 == 0 then
  205.     return turtle.attackUp();
  206.   elseif para1 == 1 then
  207.     return turtle.attack();
  208.   elseif para1 == 2 then
  209.     return turtle.attackDown();
  210.   end
  211. end
  212.  
  213. function refuelPrompt(moveFunc)
  214.   local firstTime = true;
  215.   while true do
  216.     local rslt, msg;
  217.     if nil ~= moveFunc then
  218.       rslt, msg = moveFunc();
  219.     else
  220.       if firstTime then
  221.         firstTime = false;
  222.         rslt = false;
  223.         msg  = "Out of fuel";
  224.       else
  225.         return true;
  226.       end
  227.     end
  228.     if not rslt and msg == "Out of fuel" then
  229.       print("Out of fuel. please select [r]efuel or [q]uit.");
  230.       print("(r/q)");
  231.       local ch = read();
  232.       if "q" == ch then
  233.         print("Are you sure you want to stop the script?");
  234.         print("(y/n)");
  235.         ch = read();
  236.         if "q" == ch then
  237.           error("out of fuel.");
  238.         end
  239.       elseif "r" == ch then
  240.         local selSlot = 1;
  241.         while true do
  242.           print("Please type current selected slot number.");
  243.           ch = read();
  244.           local num = tonumber(ch);
  245.           if nil ~= num and 1 <= num and num <= 16 then
  246.             selSlot = num;
  247.             break;
  248.           else
  249.             print("Wrong input. Please retry.");
  250.           end
  251.         end
  252.         while true do
  253.           print("please set fuel item and type slot number.");
  254.           print("When you finish refueling, Please type q.");
  255.           print("FuelLevel = " .. turtle.getFuelLevel());
  256.           ch = read();
  257.           local num = tonumber(ch);
  258.           if nil ~= num and 1 <= num and num <= 16 then
  259.             turtle.select(num);
  260.             local fRslt, fMsg = turtle.refuel();
  261.             if not fRslt then
  262.               print(fMsg);
  263.             end
  264.           elseif "q" == ch then
  265.             turtle.select(selSlot);
  266.             break;
  267.           else
  268.             print("Wrong input. Please retry.");
  269.           end
  270.         end
  271.       end
  272.     else
  273.       return rslt, msg;
  274.     end
  275.   end
  276. end
  277. function vacuum(slotNo)
  278.   local result = false
  279.   turtle.select(slotNo)
  280.   for i = 1, 16 do
  281.     if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
  282.       turtle.select(i)
  283.       turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
  284.       result = true
  285.       if 0 == turtle.getItemSpace(slotNo) then
  286.         break
  287.       end
  288.       turtle.select(slotNo)
  289.     end
  290.   end
  291.   turtle.select(slotNo)
  292.   return result
  293. end
  294. local function myMoveZzz(dirName, moveFunc, detectFunc, attackFunc)
  295.   local rslt, msg;
  296.   while true do
  297.     rslt, msg = refuelPrompt(moveFunc);
  298.     if rslt then
  299.       return true;
  300.     else
  301.       if moveFunc == turtle.back then
  302.         myTurnRight();
  303.         myTurnRight();
  304.       end
  305.       if detectFunc() then
  306.         if moveFunc == turtle.back then
  307.           myTurnLeft();
  308.           myTurnLeft();
  309.         end
  310.         return false;
  311.       else
  312.         for i = 0, 9 do
  313.           attackFunc();
  314.           attackFunc();
  315.           attackFunc();
  316.           if moveFunc == turtle.back then
  317.             if true == turtle.forward() then
  318.               myTurnLeft();
  319.               myTurnLeft();
  320.               return true;
  321.             end
  322.           else
  323.             if true == refuelPrompt(moveFunc) then
  324.               return true;
  325.             end
  326.           end
  327.         end
  328.         if moveFunc == turtle.back then
  329.           myTurnLeft();
  330.           myTurnLeft();
  331.         end
  332.         while true do
  333.           print("move [" .. dirName .. "] failed. retry? (y/n)");
  334.           local input = read();
  335.           if "y" == input then
  336.             break;
  337.           elseif "n" == input then
  338.             error("program stopped by user request.");
  339.           else
  340.             print("wrong input. type y or n.");
  341.           end
  342.         end
  343.       end
  344.     end
  345.   end
  346. end
  347.  
  348. local function myMove(dirName, moveFunc, detectFunc, attackFunc, digFunc, forceMoveFlg) -- <<< 2 >>>
  349.   local rslt;
  350.   while true do
  351.     rslt = refuelPrompt(moveFunc);
  352.     if rslt then
  353.       return true;
  354.     else
  355.       if moveFunc == turtle.back then
  356.         myTurnRight();
  357.         myTurnRight();
  358.       end
  359.       if detectFunc() then
  360.         if forceMoveFlg then -- <<< 2 >>>
  361.           for i = 1, 128 do
  362.             for j = 1, 10 do
  363.               sleep(0);
  364.               digFunc();
  365.               if false == detectFunc() then
  366.                 break;
  367.               end
  368.             end
  369.             rslt = refuelPrompt(moveFunc);
  370.             if true == rslt then
  371.               if moveFunc == turtle.back then
  372.                 myTurnLeft();
  373.                 myTurnLeft();
  374.               end
  375.               return true;
  376.             end
  377.           end
  378.         end
  379.         if moveFunc == turtle.back then
  380.           myTurnLeft();
  381.           myTurnLeft();
  382.         end
  383.         return false;
  384.       else
  385.         for i = 0, 9 do
  386.           attackFunc();
  387.           attackFunc();
  388.           attackFunc();
  389.           if moveFunc == turtle.back then
  390.             if true == turtle.forward() then
  391.               myTurnLeft();
  392.               myTurnLeft();
  393.               return true;
  394.             end
  395.           else
  396.             if true == refuelPrompt(moveFunc) then
  397.               return true;
  398.             end
  399.           end
  400.         end
  401.         if moveFunc == turtle.back then
  402.           myTurnLeft();
  403.           myTurnLeft();
  404.         end
  405.         while true do
  406.           print("move [" .. dirName .. "] failed. retry? (y/n)");
  407.           local input = read();
  408.           if "y" == input then
  409.             break;
  410.           elseif "n" == input then
  411.             error("program stopped by user request.");
  412.           else
  413.             print("wrong input. type y or n.");
  414.           end
  415.         end
  416.       end
  417.     end
  418.   end
  419. end
  420.  
  421. function myForward()
  422.   local forceMoveFlg = (1 == lastDigDir);
  423.   lastDigDir = -1;
  424.   return myMove("forward", turtle.forward, turtle.detect, turtle.attack, turtle.dig, forceMoveFlg);
  425. end
  426.  
  427. function myBack()
  428.   lastDigDir = -1;
  429.   return myMove("back", turtle.back, turtle.detect, turtle.attack, turtle.dig, false);
  430. end
  431.  
  432. function myUp()
  433.   local forceMoveFlg = (0 == lastDigDir);
  434.   lastDigDir = -1;
  435.   return myMove("up", turtle.up, turtle.detectUp, turtle.attackUp, turtle.digUp, forceMoveFlg);
  436. end
  437.  
  438. function myDown()
  439.   local forceMoveFlg = (2 == lastDigDir);
  440.   lastDigDir = -1;
  441.   return myMove("down", turtle.down, turtle.detectDown, turtle.attackDown, turtle.digDown, forceMoveFlg);
  442. end
  443.  
  444. local function myUserEvent(para1)
  445.   if nil ~= userEventHandler then
  446.     userEventHandler(para1);
  447.   end
  448. end
  449.  
  450. local cmdInfo =
  451. {
  452.   f = { paraCnt = 0, func = myForward;     }, -- [f]orward
  453.   b = { paraCnt = 0, func = myBack;        }, -- [b]ack
  454.   l = { paraCnt = 0, func = myTurnLeft;    }, -- turn [l]eft
  455.   r = { paraCnt = 0, func = myTurnRight;   }, -- turn [r]ight
  456.   u = { paraCnt = 0, func = myUp;          }, -- [u]p
  457.   d = { paraCnt = 0, func = myDown;        }, -- [d]own
  458.   s = { paraCnt = 1, func = turtle.select; }, -- [s]elect
  459.   p = { paraCnt = 1, func = myPlaceFunc;   }, -- [p]lace
  460.   t = { paraCnt = 2, func = myThrow;       }, -- [t]hrow = drop
  461.   e = { paraCnt = 1, func = myExcavate;    }, -- [e]xcavate = dig
  462.   z = { paraCnt = 1, func = myZzz;         }, -- [z]zz = sleep
  463.   o = { paraCnt = 2, func = myOutput;      }, -- set [o]utput (redstone)
  464.   k = { paraCnt = 1, func = mySuck;        }, -- suc[k]
  465.   a = { paraCnt = 1, func = myAttack;      }, -- [a]ttack
  466.   --v = { paraCnt = 1, func = myUserEvent;   }, -- user e[v]ent
  467.   v = { paraCnt = 1, func = vacuum;   }, -- [v]acuum
  468. };
  469.  
  470. local function getParam(command, idx)
  471.   local buff = "";
  472.   local ch = "";
  473.  
  474.   for i = idx, command:len() do
  475.     ch = command:sub(i, i);
  476.     if ch == "," then
  477.       if buff == "" then
  478.         return nil, i + 1;
  479.       else
  480.         return tonumber(buff), i + 1;
  481.       end
  482.     elseif nil ~= string.find("0123456789", ch, 1, true) then
  483.       buff = buff .. ch;
  484.     else
  485.       if buff ~= "" then
  486.         return tonumber(buff), i;
  487.       end
  488.     end
  489.   end
  490.   if buff == "" then
  491.     return nil, command:len() + 1;
  492.   else
  493.     return tonumber(buff), command:len() + 1;
  494.   end
  495. end
  496.  
  497. local function get1Cmd(command, idx)
  498.   local cmd = string.sub(command, idx, idx);
  499.   local para1, para2, nextIdx, paraCnt;
  500.  
  501.   if nil ~= cmdInfo[cmd] then
  502.     paraCnt = cmdInfo[cmd].paraCnt;
  503.   else
  504.     error("unknown command [" .. cmd .. "]");
  505.   end
  506.  
  507.   if -1 == paraCnt then
  508.     return nil, nil, nil, idx + 1;
  509.   elseif paraCnt == 0 then
  510.     return cmd, nil, nil, idx + 1;
  511.   elseif paraCnt == 1 then
  512.     para1, nextIdx = getParam(command, idx + 1);
  513.     return cmd, para1, nil, nextIdx;
  514.   elseif paraCnt == 2 then
  515.     para1, nextIdx = getParam(command, idx + 1);
  516.     para2, nextIdx = getParam(command, nextIdx);
  517.     return cmd, para1, para2, nextIdx;
  518.   end
  519. end
  520.  
  521. local function checkLoop(command)
  522.   local kBgn = {""};
  523.   local loopInfo = {};
  524.  
  525.   local ch, repeatCnt, nextIdx, kBgnIdx;
  526.   for i = 1, command:len() do
  527.     ch = command:sub(i, i);
  528.     if "(" == ch then
  529.       table.insert(kBgn, i);
  530.     elseif ")" == ch then
  531.       repeatCnt, nextIdx = getParam(command, i + 1);
  532.       loopInfo[i] = { beginIdx   = table.remove(kBgn),
  533.                       endIdx     = i,
  534.                       nextIdx    = nextIdx,
  535.                       repeatCnt  = repeatCnt,
  536.                       currentCnt = 0 };
  537.       i = nextIdx;
  538.     end
  539.   end
  540.   return loopInfo;
  541. end
  542.  
  543. function doCommand(command)
  544.  
  545.   command = command .. " ";
  546.   local loopInfo = checkLoop(command);
  547.  
  548.   local cmd, para1, para2, nextIdx, ch;
  549.  
  550.   local i = 1;
  551.   while true do
  552.     ch = command:sub(i, i);
  553.     if ")" == ch then
  554.       loopInfo[i].currentCnt = loopInfo[i].currentCnt + 1;
  555.       if loopInfo[i].repeatCnt <= loopInfo[i].currentCnt then
  556.         loopInfo[i].currentCnt = 0;
  557.         i = loopInfo[i].nextIdx;
  558.       else
  559.         i = loopInfo[i].beginIdx;
  560.       end
  561.     elseif " " == ch then
  562.       i = i + 1;
  563.     elseif "\n" == ch then
  564.       i = i + 1;
  565.     elseif "(" == ch then
  566.       i = i + 1;
  567.     else
  568.       cmd, para1, para2, nextIdx = get1Cmd(command, i);
  569.       i = nextIdx;
  570.  
  571.       if cmd ~= nil then
  572.         cmdInfo[cmd].func(para1, para2);
  573.       end
  574.     end
  575.     if command:len() <= i then
  576.       break;
  577.     end
  578.   end
  579. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement