Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- f = 0 = norden [y+1] | f = 1 = osten [x+1]
- -- f = 2 = sueden [y-1] | f = 3 = westen [x-1]
- local way = "way.dat";
- status = "_home";
- local function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- local function savePosition( varName, content, replace )
- if not replace then
- if not fs.exists(way) then
- local file = fs.open(varName .. ".dat", "w" )
- if file then
- file.write( content )
- file.close()
- else
- return false
- end
- else
- return false
- end
- else
- fs.delete(varName .. ".dat" )
- local file = fs.open(varName .. ".dat", "w" )
- if file then
- file.write( content )
- file.close()
- else
- return false
- end
- end
- return true
- end
- local function saveWay(moved, amount)
- if fs.exists(way) then option = "a"; else option = "w"; end
- local file = fs.open(way, option)
- print("moved: ",moved, " amount: ", amount);
- if ( option == "a" ) then
- file.write(";" .. moved .. "," .. amount);
- else
- file.write(moved .. "," .. amount);
- end
- file.close();
- end
- function getPositionVar( varName )
- if fs.exists(varName .. ".dat") then
- local file = fs.open(varName .. ".dat", "r")
- if file then
- local var = file.readLine()
- file.close()
- return var
- else
- return false
- end
- else
- return false
- end
- end
- position = {}
- position["x"] = 0; position["y"] = 0;
- position["z"] = 0; position["f"] = 0;
- if ( getPositionVar("x") ) then position["x"] = tonumber(getPositionVar("x")); end
- if ( getPositionVar("y") ) then position["y"] = tonumber(getPositionVar("y")); end
- if ( getPositionVar("z") ) then position["z"] = tonumber(getPositionVar("z")); end
- if ( getPositionVar("f") ) then position["f"] = tonumber(getPositionVar("f")); end
- local function move(k, v)
- v = tonumber(v);
- if ( k ~= nil and v ~= nil ) then
- if ( k == "f" ) then
- if ( v == -1 ) then turnLeft(1,1);
- elseif ( v == 1 ) then turnRight(1,1); end
- end
- if ( k == "y" ) then
- if ( v == -1 ) then
- back(1,1);
- elseif ( v == 1 ) then
- forward(1,1);
- end
- end
- if ( k == "z" ) then
- if ( v == -1 ) then
- down(1,1);
- elseif ( v == 1 ) then
- up(1,1);
- end
- end
- if ( k == "x") then
- if ( v == 1 ) then
- back(1,1);
- elseif ( v == -1 ) then
- forward(1,1);
- end
- end
- end
- end
- function status()
- return status;
- end
- function home()
- if fs.exists(way) then
- status = "_moveHome";
- local file = fs.open(way, "r")
- local mystring = file.readAll();
- local ta = split(mystring, ";")
- for index = #ta, 1, -1 do
- tt = split(ta[index], ",")
- for k, v in pairs(tt) do
- if ( k == 1 ) then doThis = v;
- elseif ( k == 2 ) then move1 = v; end
- end
- move(doThis, move1);
- end
- file.close();
- sleep(0.3);
- shell.run("remove way.dat");
- shell.run("remove x.dat");
- shell.run("remove y.dat");
- shell.run("remove z.dat");
- shell.run("remove f.dat");
- status = "_home";
- return status;
- else
- status = "_moveHomeFailed"
- return "_moveHomeFailed";
- end
- end
- function forward(amounts, value)
- if ( amounts == nil ) then amounts = 1; end
- for i = 1, amounts do
- if ( turtle.forward() ) then
- if ( position["f"] == 0 ) then
- position["y"] = position["y"] + 1;
- if ( value == nil ) then saveWay("y", -1); end
- elseif ( position["f"] == 1 ) then
- position["x"] = position["x"] + 1;
- if ( value == nil ) then saveWay("x", -1); end
- elseif ( position["f"] == 2 ) then
- position["y"] = position["y"] - 1;
- if ( value == nil ) then saveWay("y", 1); end
- elseif ( position["f"] == 3 ) then
- position["x"] = position["x"] - 1;
- if ( value == nil ) then saveWay("x", 1); end
- end
- savePosition("y", position["y"], true);
- savePosition("x", position["x"], true);
- status = "_isMoving";
- else
- return "_movingFailed";
- end
- end
- end
- function back(amounts, value)
- if ( amounts == nil ) then amounts = 1; end
- for i = 1, amounts do
- if ( turtle.back() ) then
- if ( position["f"] == 0 ) then
- position["y"] = position["y"] - 1;
- if ( value == nil ) then saveWay("y", 1); end
- elseif ( position["f"] == 1 ) then
- position["x"] = position["x"] - 1;
- if ( value == nil ) then saveWay("x", 1); end
- elseif ( position["f"] == 2 ) then
- position["y"] = position["y"] + 1;
- if ( value == nil ) then saveWay("y", -1); end
- elseif ( position["f"] == 3 ) then
- position["x"] = position["x"] + 1;
- if ( value == nil ) then saveWay("x", -1); end
- end
- savePosition("y", position["y"], true);
- savePosition("x", position["x"], true);
- status = "_isMoving";
- else
- return "_movingFailed";
- end
- end
- end
- function up(amounts, value)
- if ( amounts == nil ) then amounts = 1; end
- for i = 1, amounts do
- if ( turtle.up() ) then
- position["z"] = position["z"] + 1;
- savePosition("z", position["z"], true);
- if ( value == nil ) then saveWay("z", -1); end
- status = "_isMoving";
- else
- return "_movingFailed";
- end
- end
- end
- function down(amounts, value)
- if ( amounts == nil ) then amounts = 1; end
- for i = 1, amounts do
- if ( turtle.down() ) then
- position["z"] = position["z"] - 1;
- savePosition("z", position["z"], true);
- if ( value == nil ) then saveWay("z", 1); end
- status = "_isMoving";
- else
- return "_movingFailed";
- end
- end
- end
- function turnRight(amounts, value)
- if ( amounts == nil ) then amounts = 1; end
- for i = 1, amounts do
- if ( turtle.turnRight() ) then
- position["f"] = position["f"] + 1;
- if ( position["f"] > 3 ) then position["f"] = 0; end
- if ( value == nil ) then saveWay("f", -1); end
- savePosition("f", position["f"], true);
- status = "_isMoving";
- else
- status = "_movingFailed";
- return false;
- end
- end
- end
- function turnLeft(amounts, value)
- if ( amounts == nil or amounts < 0 ) then amounts = 1; end
- for i = 1, amounts do
- if ( turtle.turnLeft() ) then
- position["f"] = position["f"] - 1;
- if ( position["f"] < 0 ) then position["f"] = 3; end
- if ( value == nil ) then saveWay("f", 1); end
- savePosition("f", position["f"], true);
- status = "_isMoving";
- else
- status = "_movingFailed";
- return false;
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment