Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local width, height = term.getSize();
- local width_half = width/2;
- local room = {};
- --0=untraversed
- --1=air
- --2=block
- --5=air-traveled
- --6=block traveled
- local tPos = {1,1};
- local moves = {
- {1,0},
- {0,1},
- {-1,0},
- {0,-1}
- }; moves[0] = moves[4];
- local orientation = 0;
- --right = 0
- --down = 1
- --left = 2
- --up = 3
- local closedSet = {};
- local openSet = {};
- local node = 0;
- --Create the Room
- for x=-2,2 do
- room[x]={};
- for z=-2,2 do
- room[x][z]=0;
- end
- end
- room[1][1] = 1;
- function turnLeft()
- turtle.turnLeft();
- orientation = (orientation - 1) % 4;
- end
- function turnRight()
- turtle.turnRight();
- orientation = (orientation + 1) % 4;
- end
- function setOrientation(o)
- o = o % 4;
- if(orientation==o)then return; end
- local x;
- if( (orientation-1) % 4 == o)then
- x = turnLeft;
- else
- x = turnRight;
- end
- while(orientation~=o)do
- x();
- end
- if(orientation~=o)then print("ERRRRRRORR"); end
- end
- function isInRoom(o1)
- o1 = o1 or orientation;
- if(o1==0)then o1=4; end
- local newX, newY = tPos[1]+moves[o1][1], tPos[2]+moves[o1][2];
- if(room[newX]==nil)then room[newX]={}; end
- if(room[newX][newY]==5 or room[newX][newY]==2)then return false, newX, newY; end
- return true, newX, newY;
- end
- function getPossibleOptions()
- local optOrientations = {};
- local isIn, newX, newY;
- local isNot = (node~=0 and (node[1]+2)%4) or 5;
- for i = 0, 3 do
- isIn, newX, newY = isInRoom(i);
- if(isIn)then
- setOrientation(i);
- if(not turtle.detect())then
- if(i~=isNot)then
- table.insert(optOrientations, i);
- --room[newX][newY] = 1;
- if(room[newX][newY]~=5)then
- if(go())then
- move();
- end
- end
- end
- else
- room[newX][newY] = 2;
- end
- end
- end
- return optOrientations;
- end
- function moveRight()
- setOrientation(0);
- end
- function moveUp()
- setOrientation(3);
- end
- function moveDown()
- setOrientation(1);
- end
- function moveLeft()
- setOrientation(2);
- end
- function goBack()
- if(node == 0)then return false; end
- setOrientation((node[1]+2)%4);
- local x2 = (orientation==0 and 4) or orientation;
- local resp = turtle.forward();
- if(resp)then
- tPos[1] = tPos[1]+moves[x2][1];
- tPos[2] = tPos[2]+moves[x2][2];
- node = node[2];
- redraw();
- end
- return true;
- end
- function go(x1)
- if(x1)then x1(); end
- local x2 = (orientation==0 and 4) or orientation;
- local resp = turtle.forward();
- if(resp)then
- tPos[1] = tPos[1]+moves[x2][1];
- tPos[2] = tPos[2]+moves[x2][2];
- node = {orientation,node};
- room[tPos[1]][tPos[2]] = 5;
- redraw();
- end
- return resp;
- end
- function redraw()
- term.clear();
- local i1 = 1;
- local half = width_half;
- local half2 = height/2;
- local tX, tY = tPos[1], tPos[2];
- for i = tX-5,tX+5 do
- local y1 = 1;
- if(room[i]==nil)then room[i] = {}; end
- for y = tY-5,tY+5 do
- term.setCursorPos(tX-i+half,tY-y+half2);
- if(room[i][y] == 2)then
- term.write("x");
- elseif(room[i][y] == 1 or room[i][y] == 5)then
- term.write("_");
- else
- term.write("?");
- end
- y1 = y1+1;
- end
- i1 = i1+1;
- end
- term.setCursorPos(half,half2);
- term.write("o");
- end
- function move()
- local posOrients = getPossibleOptions();
- room[tPos[1]][tPos[2]] = 5;
- goBack();
- end
- move();
- term.setCursorPos(1,1);
Advertisement
Add Comment
Please, Sign In to add comment