CoderJohn

TurtleScanner

Nov 12th, 2012
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1.  
  2. local width, height = term.getSize();
  3. local width_half = width/2;
  4. local room = {};
  5.   --0=untraversed
  6.   --1=air
  7.   --2=block
  8.   --5=air-traveled
  9.   --6=block traveled
  10. local tPos = {1,1};
  11. local moves = {
  12.   {1,0},
  13.   {0,1},
  14.   {-1,0},
  15.   {0,-1}
  16. }; moves[0] = moves[4];
  17. local orientation = 0;
  18.         --right = 0
  19.         --down = 1
  20.         --left = 2
  21.         --up = 3
  22. local closedSet = {};
  23. local openSet = {};
  24. local node = 0;
  25.  
  26.  
  27. --Create the Room
  28. for x=-2,2 do
  29.   room[x]={};
  30.   for z=-2,2 do
  31.     room[x][z]=0;
  32.   end
  33. end
  34. room[1][1] = 1;
  35.  
  36. function turnLeft()
  37.   turtle.turnLeft();
  38.   orientation = (orientation - 1) % 4;
  39. end
  40.  
  41. function turnRight()
  42.   turtle.turnRight();
  43.   orientation = (orientation + 1) % 4;
  44. end
  45.  
  46. function setOrientation(o)
  47.   o = o % 4;
  48.   if(orientation==o)then return; end
  49.   local x;
  50.   if( (orientation-1) % 4 == o)then
  51.     x = turnLeft;
  52.   else
  53.     x = turnRight;
  54.   end
  55.   while(orientation~=o)do
  56.     x();
  57.   end
  58.   if(orientation~=o)then print("ERRRRRRORR"); end
  59. end
  60.  
  61. function isInRoom(o1)
  62.   o1 = o1 or orientation;
  63.   if(o1==0)then o1=4; end
  64.   local newX, newY = tPos[1]+moves[o1][1], tPos[2]+moves[o1][2];
  65.   if(room[newX]==nil)then room[newX]={}; end
  66.   if(room[newX][newY]==5 or room[newX][newY]==2)then return false, newX, newY; end
  67.   return true, newX, newY;
  68. end
  69.  
  70. function getPossibleOptions()
  71.   local optOrientations = {};
  72.   local isIn, newX, newY;
  73.   local isNot = (node~=0 and (node[1]+2)%4) or 5;
  74.   for i = 0, 3 do
  75.     isIn, newX, newY = isInRoom(i);
  76.     if(isIn)then
  77.       setOrientation(i);
  78.       if(not turtle.detect())then
  79.         if(i~=isNot)then
  80.           table.insert(optOrientations, i);
  81.           --room[newX][newY] = 1;
  82.           if(room[newX][newY]~=5)then
  83.             if(go())then
  84.               move();
  85.             end
  86.           end
  87.         end
  88.       else
  89.         room[newX][newY] = 2;
  90.       end
  91.     end
  92.   end
  93.   return optOrientations;
  94. end
  95.  
  96. function moveRight()
  97.   setOrientation(0);
  98. end
  99.  
  100. function moveUp()
  101.   setOrientation(3);
  102. end
  103.  
  104. function moveDown()
  105.   setOrientation(1);
  106. end
  107.  
  108. function moveLeft()
  109.   setOrientation(2);
  110. end
  111.  
  112. function goBack()
  113.   if(node == 0)then return false; end
  114.   setOrientation((node[1]+2)%4);
  115.   local x2 = (orientation==0 and 4) or orientation;
  116.   local resp = turtle.forward();
  117.   if(resp)then
  118.     tPos[1] = tPos[1]+moves[x2][1];
  119.     tPos[2] = tPos[2]+moves[x2][2];
  120.     node = node[2];
  121.     redraw();
  122.   end
  123.   return true;
  124. end
  125.  
  126. function go(x1)
  127.   if(x1)then x1(); end
  128.   local x2 = (orientation==0 and 4) or orientation;
  129.   local resp = turtle.forward();
  130.   if(resp)then
  131.     tPos[1] = tPos[1]+moves[x2][1];
  132.     tPos[2] = tPos[2]+moves[x2][2];
  133.     node = {orientation,node};
  134.     room[tPos[1]][tPos[2]] = 5;
  135.     redraw();
  136.   end
  137.   return resp;
  138. end
  139.  
  140. function redraw()
  141.   term.clear();
  142.   local i1 = 1;
  143.   local half = width_half;
  144.   local half2 = height/2;
  145.   local tX, tY = tPos[1], tPos[2];
  146.   for i = tX-5,tX+5 do
  147.     local y1 = 1;
  148.     if(room[i]==nil)then room[i] = {}; end
  149.     for y = tY-5,tY+5 do
  150.       term.setCursorPos(tX-i+half,tY-y+half2);
  151.       if(room[i][y] == 2)then
  152.         term.write("x");
  153.       elseif(room[i][y] == 1 or room[i][y] == 5)then
  154.         term.write("_");
  155.       else
  156.         term.write("?");
  157.       end
  158.       y1 = y1+1;
  159.     end
  160.     i1 = i1+1;
  161.   end
  162.   term.setCursorPos(half,half2);
  163.   term.write("o");
  164. end
  165.  
  166. function move()
  167.   local posOrients = getPossibleOptions();
  168.   room[tPos[1]][tPos[2]] = 5;
  169.   goBack();
  170. end
  171.  
  172. move();
  173. term.setCursorPos(1,1);
Advertisement
Add Comment
Please, Sign In to add comment