在Lua中打印5x5网格地图

时间:2014-07-10 17:13:58

标签: dictionary lua grid

我试图找出如何打印5x5网格地图,每个地方都是一个带退出的房间。我有一个25个值的表,是每个房间的出口。如果该值为0,则表示该位置没有空间。

基本上,我试图找出如何绕过弹出的任何异常情况。例如......

[ ]         [ ]            
    \     /  |  /          
      [ ]   [ ]   [ ]      
          X  |  /          
            [+]         [ ]
                \     /    
                  [ ]      
                /          
[ ]---[ ]---[ ]            

它显示我在+的中间,每条线都是退出。 \位于东南,/东北,|北/南,---东/西,X是所有对角线出口。这个例子中的问题是我的北方位置应该是空的,因为它与西北和东北的位置相同。所以它打印了两次相同的房间。

这是另一个混​​乱的立场的例子。

      [ ]         [ ]---[ ]
    /     \     /  |       
            [ ]   [ ]      
             |  X  |       
      [ ]---[+]   [ ]      
          \     X  |       
            [ ]   [ ]      
                      \    
                        [ ]

空荡荡的房间只是空间。任何帮助都是< 3ly ^ _ ^

::编辑::

以下是用于打印地图的代码。

我键入的别名使其全部发生:^ gmap $及其代码:

local i = 1
for k, v in ipairs(on.map.info.grid) do
  local x = tonumber(v[2])
  local exits = getExits(v[2])
  local exits2 = {}
  local exits5 = {}
  local exits6 = {}
  local colour = getTerrainColour(v[1])
  if k < 25 then
    exits2 = getExits(on.map.info.grid[k+1][2])
    if k < 20 then
      exits5 = getExits(on.map.info.grid[k+5][2])
    end
    if k < 20 then
      exits6 = getExits(on.map.info.grid[k+6][2])
    end
    diagExit(i, exits, exits2, exits5, exits6)
  end
  southExit(exits, i)
  if i % 5 ~= 0 then
    if x > 0 then
      local space = k == 13 and "+" or getRoomSpacer(exits) or " "
      cecho(string.format("<%s>[%s]", colour, space))
    else
      echo("   ")
    end
    space = eastExit(exits, exits2) or "   "
    echo(space)
  else
    if x > 0 then
      local space = k == 13 and "x" or getRoomSpacer(exits) or " "
      cecho(string.format("<%s>[%s]", colour, space))
    else
      echo("   ")
    end
    echo("\n")
    if i <= 20 then
      for i = 1, 4 do
        echo(tmp.sx[i])
        echo(tmp.dx[i])
      end
      echo(tmp.sx[5])
      echo("\n")
    end
  end
  i = i + 1
end

相关功能:

function parse_exits(s)
   on.map.exits = {}
   for x, stat in s:gmatch("(%a+)(%d)") do
      on.map.exits[x] = stat
   end
end

function getTerrain(n)
   n = tonumber(n)
   local terrain = {
      [1] = "Forest",
      [2] = "City",
      [3] = "Swamp",
      [4] = "Underground",
      [5] = "Snow",
      [10] = "Freshwater",
      [11] = "Saltwater",
      [12] = "Beach",
      [13] = "Mountain",
      [15] = "Tropical",
      [26] = "Skies",
   }
   for k, v in pairs(terrain) do
      if k == n then return v end
   end
   return false
end

function getTerrainColour(n)
  local terrain = getTerrain(n)
  local tcolours = {
    Forest = "forest_green",
    City = "light_goldenrod",
    Swamp = "DarkOliveGreen",
    Underground = "a_brown",
    Snow = "snow",
    Freshwater = "a_blue",
    Saltwater = "spring_green",
    Beach = "yellow",
    Mountain = "brown",
    Tropical = "orange_red",
    Skies = "light_sky_blue",
  }
  return tcolours[terrain] or "red"
end

function getExits(n)
   n = tonumber(n)
   local x = {}
   local exits = {
      {"n", 1},
      {"ne", 2},
      {"e", 4},
      {"se", 8},
      {"s", 16},
      {"sw", 32},
      {"w", 64},
      {"nw", 128},
      {"u", 256},
      {"d", 512},
      {"in", 1024},
      {"o", 2048},
   }
   local i = 12
   while n > 0 do
      if n >= exits[i][2] then
         x[#x+1] = exits[i][1]
         n = n - exits[i][2]
      end
      i = i - 1
   end
   return x or 0
end

function getRoomSpacer(t)
  local xlist = {
    u  = [[^]],
    d  = [[_]],
    ["in"] = [[>]],
    o  = [[<]],
  }
  if table.isMember(t, "u") and table.isMember(t, "d") then
    return "="
  else
    for x, s in pairs(xlist) do
      if table.isMember(t, x) then
        return s
      end
    end
  end
  return false
end

function eastExit(t, t2)
  if table.isMember(t, "e") or table.isMember(t2, "w") then
    return "---"
  end
  return false
end

function southExit(t, pos)
  if pos > 5 then
    pos = pos % 5
  end
  tmp.sx = tmp.sx or {}
  if table.contains(t, "s") then
    tmp.sx[pos] = [[ | ]]
  else
    tmp.sx[pos] = [[   ]]
  end
end

function diagExit(pos, t, t2, t5, t6)
  if pos > 5 then
    pos = pos % 5
  end
  tmp.dx = tmp.dx or {}
  if table.isMember(t, "se") and table.isMember(t2, "sw") then
    tmp.dx[pos] = [[ X ]]
  elseif table.isMember(t, "se") then
    tmp.dx[pos] = [[ \ ]]
  elseif table.isMember(t2, "sw") then
    tmp.dx[pos] = [[ / ]]
  elseif table.isMember(t5, "ne") then
    tmp.dx[pos] = [[ / ]]
  elseif table.isMember(t6, "nw") then
    tmp.dx[pos] = [[ \ ]]
  else
    tmp.dx[pos] = [[   ]]
  end
end

cecho打印出彩色,因此地形功能可以获得颜色......

以下是用于第一次打印输出错误的数据:

{
  {
    "1",
    "136"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "50"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "138"
  },
  {
    "1",
    "50"
  },
  {
    "2",
    "32"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "139"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "36"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "162"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "148"
  },
  {
    "1",
    "76"
  },
  {
    "1",
    "66"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  }
}

第二次打印输出:

{
  {
    "0",
    "0"
  },
  {
    "1",
    "168"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "54"
  },
  {
    "1",
    "192"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "154"
  },
  {
    "1",
    "177"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "12"
  },
  {
    "1",
    "75"
  },
  {
    "1",
    "177"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "128"
  },
  {
    "1",
    "137"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "0",
    "0"
  },
  {
    "1",
    "136"
  }
}

0 个答案:

没有答案