Advertisement
King0fGamesYami

mapreader

Jul 2nd, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.19 KB | None | 0 0
  1. function path( tMaps, _yield )
  2.     --Find start
  3.     local function findStart()
  4.         for k, sMap in ipairs( tMaps ) do
  5.             local l = 1
  6.             for line in sMap:gmatch( '[^\r\n]+' ) do
  7.                 for i = 1, #line do
  8.                     if line:sub( i, i ):upper() == "X" then
  9.                         return {x = i, y = l, z = k}
  10.                     end
  11.                 end
  12.                 l = l + 1
  13.             end
  14.         end
  15.     end
  16.     --update removes already used path bits
  17.     local function update( tbl )
  18.         local l = 1
  19.         local newString = {}
  20.         local oldChar
  21.         for line in tMaps[ tbl.z ]:gmatch( '[^\r\n]+' ) do
  22.             if l == tbl.y then
  23.                 newString[l] = line:sub( 1, tbl.x - 1 ).." "..line:sub( tbl.x + 1, #line )
  24.                 oldChar = line:sub( tbl.x, tbl.x )
  25.             else
  26.                 newString[l] = line
  27.             end
  28.             l = l + 1
  29.         end
  30.         return table.concat( newString, "\n" ), oldChar
  31.     end
  32.     --orient function
  33.     local face = "up"
  34.     local function orient( direction )
  35.         if face == "up" and direction == "up" then
  36.             return
  37.         elseif face == "up" and direction == "right" then
  38.             turtle.turnRight()
  39.             face = "right"
  40.         elseif face == "up" and direction == "left" then
  41.             turtle.turnLeft()
  42.             face = "left"
  43.         elseif face == "up" and direction == "down" then
  44.             turtle.turnRight()
  45.             turtle.turnRight()
  46.             face = "down"
  47.         elseif face == "right" and direction == "up" then
  48.             turtle.turnLeft()
  49.             face = "up"
  50.         elseif face == "right" and direction == "right" then
  51.             return
  52.         elseif face == "right" and direction == "left" then
  53.             turtle.turnLeft()
  54.             turtle.turnLeft()
  55.             face = "left"
  56.         elseif face == "right" and direction == "down" then
  57.             turtle.turnRight()
  58.             face = "down"
  59.         elseif face == "left" and direction == "up" then
  60.             turtle.turnRight()
  61.             face = "up"
  62.         elseif face == "left" and direction == "right" then
  63.             turtle.turnRight()
  64.             turtle.turnRight()
  65.             face = "right"
  66.         elseif face == "left" and direction == "left" then
  67.             return
  68.         elseif face == "left" and direction == "down" then
  69.             turtle.turnLeft()
  70.             face = "down"
  71.         elseif face == "down" and direction == "up" then
  72.             turtle.turnLeft()
  73.             turtle.turnLeft()
  74.             face = "up"
  75.         elseif face == "down" and direction == "right" then
  76.             turtle.turnLeft()
  77.             face = "right"
  78.         elseif face == "down" and direction == "left" then
  79.             turtle.turnRight()
  80.             face = "left"
  81.         elseif face == "down" and direction == "down" then
  82.             return
  83.         end
  84.     end
  85.     --Find next position
  86.     local function findNext( tbl )
  87.         local oldChar
  88.         tMaps[ tbl.z ], oldChar = update( tbl )
  89.         local l = 1
  90.         local compare = {
  91.             D = true,
  92.             U = true,
  93.             ["#"] = true,
  94.             d = true,
  95.             u = true,
  96.         }
  97.         --Check current level
  98.         for line in tMaps[ tbl.z ]:gmatch( '[^\r\n]+' ) do
  99.             if l == tbl.y - 1 and compare[ line:sub( tbl.x, tbl.x ) ] then
  100.                 --turtle should move "up"
  101.                 return function() orient( "up" ) return turtle.forward() end, {x = tbl.x, y = tbl.y - 1, z = tbl.z}
  102.             elseif l == tbl.y and compare[ line:sub( tbl.x - 1, tbl.x - 1 ) ] then
  103.                 --turtle should move "left"
  104.                 return function() orient( "left" ) return turtle.forward() end, {x = tbl.x - 1, y = tbl.y, z = tbl.z}
  105.             elseif l == tbl.y and compare[ line:sub( tbl.x + 1, tbl.x + 1 ) ] then
  106.                 --turtle should move "right"
  107.                 return function() orient( "right" ) return turtle.forward() end, {x = tbl.x + 1, y = tbl.y, z = tbl.z}
  108.             elseif l == tbl.y + 1 and compare[ line:sub( tbl.x, tbl.x ) ]then
  109.                 --turtle should move "down"
  110.                 return function() orient( "down" ) return turtle.forward() end, {x = tbl.x, y = tbl.y + 1, z = tbl.z}
  111.             elseif oldChar:upper() == "U" then
  112.                 return function() return turtle.up() end, {x = tbl.x, y = tbl.y, z = tbl.z + 1}
  113.             elseif oldChar:upper() == "D" then
  114.                 return function() return turtle.down() end, {x = tbl.x, y = tbl.y, z = tbl.z - 1}
  115.             end
  116.             l = l + 1
  117.         end    
  118.     end
  119.     local place = findStart()
  120.     local path = {}
  121.     while place do
  122.         path[#path + 1], place = findNext( place )
  123.     end
  124.     path.x = 1
  125.     path.yield = _yield
  126.     local mt = {
  127.     __call = function()
  128.         if path[ path.x ] and path.yield then
  129.             local success = path[ path.x ]()
  130.             path.x = path.x + 1
  131.             return success
  132.         elseif path[ path.x ] then
  133.             repeat
  134.                 path[ path.x ]()
  135.                 path.x = path.x + 1
  136.             until not path[ path.x ]
  137.         else
  138.             return false
  139.         end
  140.     end,
  141.     }
  142.     setmetatable( path, mt )
  143.     return path
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement