Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("lib_turtle_0.2.3");
- local function digBack()
- local rslt;
- turtle.turnLeft();
- turtle.turnLeft();
- rslt = myDig();
- turtle.turnRight();
- turtle.turnRight();
- return rslt;
- end
- local function dir(name_, fromDir_, compareFunc_, detectFunc_,
- forwardFunc_, digFunc_, backFunc_, backDigFunc_)
- return
- {
- name = name_,
- fromDir = fromDir_,
- compareFunc = compareFunc_,
- detectFunc = detectFunc_,
- forwardFunc = forwardFunc_,
- digFunc = digFunc_,
- backFunc = backFunc_,
- backDigFunc = backDigFunc_,
- };
- end
- local dirInfo = { };
- dirInfo[0] = dir("top", 2, turtle.compareUp, turtle.detectUp, myUp, myDigUp, myDown, myDigDown);
- dirInfo[1] = dir("front", 4, turtle.compare, turtle.detect, myForward, myDig, myBack, digBack);
- dirInfo[2] = dir("bottom", 0, turtle.compareDown, turtle.detectDown, myDown, myDigDown, myUp, myDigUp);
- dirInfo[3] = dir("left", 4, turtle.compare, turtle.detect, myForward, myDig, myBack, digBack);
- dirInfo[4] = dir("back", 4, turtle.compare, turtle.detect, myForward, myDig, myBack, digBack);
- dirInfo[5] = dir("right", 4, turtle.compare, turtle.detect, myForward, myDig, myBack, digBack);
- local stack = { };
- local function doCompare(posiFlg, compareFunc, detectFunc, slotAry)
- for i, slot in ipairs(slotAry) do
- turtle.select(slot);
- if compareFunc() then
- return posiFlg;
- end
- end
- if posiFlg then
- return false;
- else
- return detectFunc();
- end
- end
- local function doNekosogi(posiFlg, dirCode, slotAry)
- local dcDI = dirInfo[dirCode];
- if not doCompare(posiFlg, dcDI.compareFunc, dcDI.detectFunc, slotAry) then
- return;
- end
- if false == dcDI.digFunc() then
- return;
- end
- dcDI.forwardFunc();
- table.insert(stack, { toDir = dirCode, progress = 0 });
- local current = stack[table.maxn(stack)];
- while true do
- if 6 <= current.progress then
- turtle.turnLeft();
- if not dirInfo[current.toDir].backDigFunc() then
- error("Since the return path was shut by unbreakable block, turtle cannot move.");
- end
- dirInfo[current.toDir].backFunc();
- table.remove(stack);
- current = stack[table.maxn(stack)];
- if nil == current then
- break;
- end
- else
- if 3 <= current.progress then
- turtle.turnLeft();
- end
- local curStaIdx = table.maxn(stack);
- if current.progress ~= dirInfo[current.toDir].fromDir then
- local cpDI = dirInfo[current.progress];
- if doCompare(posiFlg, cpDI.compareFunc, cpDI.detectFunc, slotAry) then
- if cpDI.digFunc() then
- cpDI.forwardFunc();
- table.insert(stack, { toDir = current.progress, progress = 0 });
- current = stack[table.maxn(stack)];
- end
- end
- end
- stack[curStaIdx].progress = stack[curStaIdx].progress + 1;
- end
- end
- end
- -- dirCode : 0(top), 1(front), 2(bottom)
- function nekoPosi(dirCode, slotAry)
- doNekosogi(true, dirCode, slotAry);
- end
- -- dirCode : 0(top), 1(front), 2(bottom)
- function nekoNega(dirCode, slotAry)
- doNekosogi(false, dirCode, slotAry);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement