Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("lib_log");
- userEventHandler = nil;
- allowDigTurtle = false; -- <<< 1 >>>
- local lastDigDir = -1;
- local function myPlaceFunc(para1)
- lastDigDir = -1; -- <<< 2 >>>
- if para1 == 0 then
- return turtle.placeUp();
- elseif para1 == 1 then
- return turtle.place();
- elseif para1 == 2 then
- return turtle.placeDown();
- end
- end
- function myPlaceUp()
- return myPlaceFunc(0);
- end
- function myPlace()
- return myPlaceFunc(1);
- end
- function myPlaceDown()
- return myPlaceFunc(2);
- end
- local function myThrow(para1, para2)
- if para1 == 0 then
- return turtle.dropUp(para2);
- elseif para1 == 1 then
- return turtle.drop(para2);
- elseif para1 == 2 then
- return turtle.dropDown(para2);
- end
- end
- local function myExcavateZzz(para1)
- lastDigDir = para1; -- <<< 2 >>>
- if para1 == 0 then
- if turtle.detectUp() then
- for i = 1, 128 do
- if allowDigTurtle or -- <<< 1 >>>
- ( -- <<< 1 >>>
- turtle.detectUp() and -- <<< 1 >>>
- "turtle" ~= peripheral.getType("top") -- <<< 1 >>>
- ) then -- <<< 1 >>>
- turtle.digUp();
- else -- <<< 1 >>>
- sleep("0.5"); -- <<< 1 >>>
- end -- <<< 1 >>>
- sleep(0.5);
- if not turtle.detectUp() then
- return true;
- end
- end
- return false;
- else
- return true;
- end
- elseif para1 == 1 then
- if turtle.detect() then
- for h = 0, 3 do
- for i = 1, 20 do
- if allowDigTurtle or -- <<< 1 >>>
- ( -- <<< 1 >>>
- turtle.detect() and -- <<< 1 >>>
- "turtle" ~= peripheral.getType("front") -- <<< 1 >>>
- ) then -- <<< 1 >>>
- turtle.dig();
- else -- <<< 1 >>>
- sleep(1); -- <<< 1 >>>
- end -- <<< 1 >>>
- sleep(0.5 * h);
- if false == turtle.detect() then
- break; -- <<< 2 >>>
- end
- end
- end
- return not turtle.detect(); -- <<< 2 >>>
- else
- return true;
- end
- elseif para1 == 2 then
- if turtle.detectDown() then
- for i = 0, 9 do -- <<< 1 >>>
- if allowDigTurtle or -- <<< 1 >>>
- ( -- <<< 1 >>>
- turtle.detectDown() and -- <<< 1 >>>
- "turtle" ~= peripheral.getType("bottom") -- <<< 1 >>>
- ) then -- <<< 1 >>>
- return turtle.digDown();
- else -- <<< 1 >>>
- sleep(5); -- <<< 1 >>>
- end -- <<< 1 >>>
- end -- <<< 1 >>>
- return false; -- <<< 1 >>>
- else
- return true;
- end
- end
- end
- local function myExcavate(para1)
- lastDigDir = para1; -- <<< 2 >>>
- if para1 == 0 then
- if turtle.detectUp() then
- for i = 1, 128 do
- if allowDigTurtle or -- <<< 1 >>>
- ( -- <<< 1 >>>
- turtle.detectUp() and -- <<< 1 >>>
- "turtle" ~= peripheral.getType("top") -- <<< 1 >>>
- ) then -- <<< 1 >>>
- turtle.digUp();
- else -- <<< 1 >>>
- sleep("0.5"); -- <<< 1 >>>
- end -- <<< 1 >>>
- sleep(0.5);
- if not turtle.detectUp() then
- return true;
- end
- end
- return false;
- else
- return true;
- end
- elseif para1 == 1 then
- if turtle.detect() then
- if allowDigTurtle or -- <<< 1 >>>
- ( -- <<< 1 >>>
- turtle.detect() and -- <<< 1 >>>
- "turtle" ~= peripheral.getType("front") -- <<< 1 >>>
- ) then -- <<< 1 >>>
- return turtle.dig();
- end -- <<< 1 >>>
- else
- return true;
- end
- elseif para1 == 2 then
- if turtle.detectDown() then
- if allowDigTurtle or -- <<< 1 >>>
- ( -- <<< 1 >>>
- turtle.detectDown() and -- <<< 1 >>>
- "turtle" ~= peripheral.getType("bottom") -- <<< 1 >>>
- ) then -- <<< 1 >>>
- return turtle.digDown();
- end -- <<< 1 >>>
- else
- return true;
- end
- end
- end
- function myDigUp()
- return myExcavate(0);
- end
- function myDig()
- return myExcavate(1);
- end
- function myDigDown()
- return myExcavate(2);
- end
- function myTurnLeft()
- lastDigDir = -1; -- <<< 2 >>>
- turtle.turnLeft();
- end
- function myTurnRight()
- lastDigDir = -1; -- <<< 2 >>>
- turtle.turnRight();
- end
- local function myZzz(para1)
- sleep(para1 / 1000);
- end
- local function myOutput(para1, para2)
- local dir = { [0] = "top", [1] = "front", [2] = "bottom",
- [3] = "left", [4] = "back", [5] = "right" };
- if para2 == 1 then
- rs.setOutput(dir[para1], true);
- else
- rs.setOutput(dir[para1], false);
- end
- end
- local function mySuck(para1)
- if para1 == 0 then
- return turtle.suckUp();
- elseif para1 == 1 then
- return turtle.suck();
- elseif para1 == 2 then
- return turtle.suckDown();
- end
- end
- local function myAttack(para1)
- if para1 == 0 then
- return turtle.attackUp();
- elseif para1 == 1 then
- return turtle.attack();
- elseif para1 == 2 then
- return turtle.attackDown();
- end
- end
- function refuelPrompt(moveFunc)
- local firstTime = true;
- while true do
- local rslt, msg;
- if nil ~= moveFunc then
- rslt, msg = moveFunc();
- else
- if firstTime then
- firstTime = false;
- rslt = false;
- msg = "Out of fuel";
- else
- return true;
- end
- end
- if not rslt and msg == "Out of fuel" then
- print("Out of fuel. please select [r]efuel or [q]uit.");
- print("(r/q)");
- local ch = read();
- if "q" == ch then
- print("Are you sure you want to stop the script?");
- print("(y/n)");
- ch = read();
- if "q" == ch then
- error("out of fuel.");
- end
- elseif "r" == ch then
- local selSlot = 1;
- while true do
- print("Please type current selected slot number.");
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- selSlot = num;
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- while true do
- print("please set fuel item and type slot number.");
- print("When you finish refueling, Please type q.");
- print("FuelLevel = " .. turtle.getFuelLevel());
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- turtle.select(num);
- local fRslt, fMsg = turtle.refuel();
- if not fRslt then
- print(fMsg);
- end
- elseif "q" == ch then
- turtle.select(selSlot);
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- end
- else
- return rslt, msg;
- end
- end
- end
- function vacuum(slotNo)
- local result = false
- turtle.select(slotNo)
- for i = 1, 16 do
- if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
- turtle.select(i)
- turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
- result = true
- if 0 == turtle.getItemSpace(slotNo) then
- break
- end
- turtle.select(slotNo)
- end
- end
- turtle.select(slotNo)
- return result
- end
- local function myMoveZzz(dirName, moveFunc, detectFunc, attackFunc)
- local rslt, msg;
- while true do
- rslt, msg = refuelPrompt(moveFunc);
- if rslt then
- return true;
- else
- if moveFunc == turtle.back then
- myTurnRight();
- myTurnRight();
- end
- if detectFunc() then
- if moveFunc == turtle.back then
- myTurnLeft();
- myTurnLeft();
- end
- return false;
- else
- for i = 0, 9 do
- attackFunc();
- attackFunc();
- attackFunc();
- if moveFunc == turtle.back then
- if true == turtle.forward() then
- myTurnLeft();
- myTurnLeft();
- return true;
- end
- else
- if true == refuelPrompt(moveFunc) then
- return true;
- end
- end
- end
- if moveFunc == turtle.back then
- myTurnLeft();
- myTurnLeft();
- end
- while true do
- print("move [" .. dirName .. "] failed. retry? (y/n)");
- local input = read();
- if "y" == input then
- break;
- elseif "n" == input then
- error("program stopped by user request.");
- else
- print("wrong input. type y or n.");
- end
- end
- end
- end
- end
- end
- local function myMove(dirName, moveFunc, detectFunc, attackFunc, digFunc, forceMoveFlg) -- <<< 2 >>>
- local rslt;
- while true do
- rslt = refuelPrompt(moveFunc);
- if rslt then
- return true;
- else
- if moveFunc == turtle.back then
- myTurnRight();
- myTurnRight();
- end
- if detectFunc() then
- if forceMoveFlg then -- <<< 2 >>>
- for i = 1, 128 do
- for j = 1, 10 do
- sleep(0);
- digFunc();
- if false == detectFunc() then
- break;
- end
- end
- rslt = refuelPrompt(moveFunc);
- if true == rslt then
- if moveFunc == turtle.back then
- myTurnLeft();
- myTurnLeft();
- end
- return true;
- end
- end
- end
- if moveFunc == turtle.back then
- myTurnLeft();
- myTurnLeft();
- end
- return false;
- else
- for i = 0, 9 do
- attackFunc();
- attackFunc();
- attackFunc();
- if moveFunc == turtle.back then
- if true == turtle.forward() then
- myTurnLeft();
- myTurnLeft();
- return true;
- end
- else
- if true == refuelPrompt(moveFunc) then
- return true;
- end
- end
- end
- if moveFunc == turtle.back then
- myTurnLeft();
- myTurnLeft();
- end
- while true do
- print("move [" .. dirName .. "] failed. retry? (y/n)");
- local input = read();
- if "y" == input then
- break;
- elseif "n" == input then
- error("program stopped by user request.");
- else
- print("wrong input. type y or n.");
- end
- end
- end
- end
- end
- end
- function myForward()
- local forceMoveFlg = (1 == lastDigDir);
- lastDigDir = -1;
- return myMove("forward", turtle.forward, turtle.detect, turtle.attack, turtle.dig, forceMoveFlg);
- end
- function myBack()
- lastDigDir = -1;
- return myMove("back", turtle.back, turtle.detect, turtle.attack, turtle.dig, false);
- end
- function myUp()
- local forceMoveFlg = (0 == lastDigDir);
- lastDigDir = -1;
- return myMove("up", turtle.up, turtle.detectUp, turtle.attackUp, turtle.digUp, forceMoveFlg);
- end
- function myDown()
- local forceMoveFlg = (2 == lastDigDir);
- lastDigDir = -1;
- return myMove("down", turtle.down, turtle.detectDown, turtle.attackDown, turtle.digDown, forceMoveFlg);
- end
- local function myUserEvent(para1)
- if nil ~= userEventHandler then
- userEventHandler(para1);
- end
- end
- local cmdInfo =
- {
- f = { paraCnt = 0, func = myForward; }, -- [f]orward
- b = { paraCnt = 0, func = myBack; }, -- [b]ack
- l = { paraCnt = 0, func = myTurnLeft; }, -- turn [l]eft
- r = { paraCnt = 0, func = myTurnRight; }, -- turn [r]ight
- u = { paraCnt = 0, func = myUp; }, -- [u]p
- d = { paraCnt = 0, func = myDown; }, -- [d]own
- s = { paraCnt = 1, func = turtle.select; }, -- [s]elect
- p = { paraCnt = 1, func = myPlaceFunc; }, -- [p]lace
- t = { paraCnt = 2, func = myThrow; }, -- [t]hrow = drop
- e = { paraCnt = 1, func = myExcavate; }, -- [e]xcavate = dig
- z = { paraCnt = 1, func = myZzz; }, -- [z]zz = sleep
- o = { paraCnt = 2, func = myOutput; }, -- set [o]utput (redstone)
- k = { paraCnt = 1, func = mySuck; }, -- suc[k]
- a = { paraCnt = 1, func = myAttack; }, -- [a]ttack
- --v = { paraCnt = 1, func = myUserEvent; }, -- user e[v]ent
- v = { paraCnt = 1, func = vacuum; }, -- [v]acuum
- };
- local function getParam(command, idx)
- local buff = "";
- local ch = "";
- for i = idx, command:len() do
- ch = command:sub(i, i);
- if ch == "," then
- if buff == "" then
- return nil, i + 1;
- else
- return tonumber(buff), i + 1;
- end
- elseif nil ~= string.find("0123456789", ch, 1, true) then
- buff = buff .. ch;
- else
- if buff ~= "" then
- return tonumber(buff), i;
- end
- end
- end
- if buff == "" then
- return nil, command:len() + 1;
- else
- return tonumber(buff), command:len() + 1;
- end
- end
- local function get1Cmd(command, idx)
- local cmd = string.sub(command, idx, idx);
- local para1, para2, nextIdx, paraCnt;
- if nil ~= cmdInfo[cmd] then
- paraCnt = cmdInfo[cmd].paraCnt;
- else
- error("unknown command [" .. cmd .. "]");
- end
- if -1 == paraCnt then
- return nil, nil, nil, idx + 1;
- elseif paraCnt == 0 then
- return cmd, nil, nil, idx + 1;
- elseif paraCnt == 1 then
- para1, nextIdx = getParam(command, idx + 1);
- return cmd, para1, nil, nextIdx;
- elseif paraCnt == 2 then
- para1, nextIdx = getParam(command, idx + 1);
- para2, nextIdx = getParam(command, nextIdx);
- return cmd, para1, para2, nextIdx;
- end
- end
- local function checkLoop(command)
- local kBgn = {""};
- local loopInfo = {};
- local ch, repeatCnt, nextIdx, kBgnIdx;
- for i = 1, command:len() do
- ch = command:sub(i, i);
- if "(" == ch then
- table.insert(kBgn, i);
- elseif ")" == ch then
- repeatCnt, nextIdx = getParam(command, i + 1);
- loopInfo[i] = { beginIdx = table.remove(kBgn),
- endIdx = i,
- nextIdx = nextIdx,
- repeatCnt = repeatCnt,
- currentCnt = 0 };
- i = nextIdx;
- end
- end
- return loopInfo;
- end
- function doCommand(command)
- command = command .. " ";
- local loopInfo = checkLoop(command);
- local cmd, para1, para2, nextIdx, ch;
- local i = 1;
- while true do
- ch = command:sub(i, i);
- if ")" == ch then
- loopInfo[i].currentCnt = loopInfo[i].currentCnt + 1;
- if loopInfo[i].repeatCnt <= loopInfo[i].currentCnt then
- loopInfo[i].currentCnt = 0;
- i = loopInfo[i].nextIdx;
- else
- i = loopInfo[i].beginIdx;
- end
- elseif " " == ch then
- i = i + 1;
- elseif "\n" == ch then
- i = i + 1;
- elseif "(" == ch then
- i = i + 1;
- else
- cmd, para1, para2, nextIdx = get1Cmd(command, i);
- i = nextIdx;
- if cmd ~= nil then
- cmdInfo[cmd].func(para1, para2);
- end
- end
- if command:len() <= i then
- break;
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement