map table troubles

Post Reply
User avatar
Akaya
Posts: 412
Joined: Thu Apr 19, 2012 1:36 am

map table troubles

Post by Akaya » Mon Jun 15, 2015 4:35 pm

Here is a MUD that has a map made of text. I've placed it in a table called sys.map_table:
Code: [show] | [select all] lua
 sys.map_table = {
  "??????????????????????????????$$$$$$$    ??????????????????????????????",
  "?????????????????????????? $$$$$$$$$$        ??????????????????????????",
  "???????????????????????    $$$$$$$$$$           ???????????????????????",
  "????????????????????       $$$$$$$$$$             0????????????????????",
  "??????????????????    0    $$$$$$$$$$          0    0??????????????????",
  "?????????????????          $$$$$$$$$$                 ?????????????????",
  "???????????????   0        $$$$$$$$$$                   ???????????????",
  "??????????????     0       $$$$$$$$$$                    ??????????????",
  "?????????????              $$$$$$$$$$            0  0     ?????????????",
  "????????????                  0           0      0         ????????????",
  "??????????                00   0            0                ??????????",
  "??????????       0       0                  0               0??????????",
  "?????????        0                      N  D                  ?????????",
  "????????   0       0   0                                       ????????",
  "???????             0                                           ???????",
  "??????                                                           ??????",
  "??????                 N       *  *                              ??????",
  "?????               D    *                                    0 00?????",
  "????                             *         *  M                    ????",
  "????              B     *      D      *                0           ????",
  "???                               *              *                  ???",
  "???                  N         *                                    ???",
  "???   0                                                             ???",
  "??                                    *                              ??",
  "??    0                                                              ??",
  "??         0                                                   0     ??",
  "?        0                       D                                    ?",
  "?                                                           0      0  ?",
  "?                                                                     ?",
  "?                          *                   $$$$$$$$$$             ?",
  "                      D     D                  $$$$$$$$$$              ",
  "            *            D*                    $$$$$$$$$$              ",
  "                               O  *            $$$$$$$$$$              ",
  "                              D   M*     O     $$$$$$$$$$              ",
  "          0         B                M         $$$$$$$$$$              ",
  "          D                      A H   *       $$$$$$$$$$              ",
  "       0      *      *    D     *              $$$$$$$$$$          0   ",
  "             *      *       *        M         $$$$$$$$$$        0  0  ",
  "            *                 O             M  $$$$$$$$$$              ",
  "         0                             *                               ",
  "                                *                     *                ",
  "?     0 0            M                        D          *            ?",
  "?                                         M                           ?",
  "?         0          *                     M                          ?",
  "?              O                                                      ?",
  "??                      B             N                              ??",
  "??     0                             A   *                           ??",
  "??         0             *                                           ??",
  "???                                                                 ???",
  "???                        $$$$$$$$$$   M                           ???",
  "???                        $$$$$$$$$$       D                      0???",
  "????                       $$$$$$$$$$    *                         ????",
  "????            0          $$$$$$$$$$                      0       ????",
  "?????         0            $$$$$$$$$$                             ?????",
  "??????           0       MM$$$$$$$$$$                        0   ??????",
  "??????              *      $$$$$$$$$$         *                  ??????",
  "???????                    $$$$$$$$$$                           ???????",
  "????????              0    $$$$$$$$$$ O           0   0        ????????",
  "?????????             0    $$$$$$$$$$                         ?????????",
  "??????????                                 00    0           ??????????",
  "??????????        0                     0                    ??????????",
  "????????????       0 0           0          0              ????????????",
  "?????????????           0                          0      ?????????????",
  "??????????????           0                               ??????????????",
  "???????????????                                         ???????????????",
  "?????????????????     0                               ?????????????????",
  "?????????????????? 0        0             0          ??????????????????",
  "????????????????????                     0  0      ????????????????????",
  "???????????????????????                         ???????????????????????",
  "??????????????????????????                   ??????????????????????????",
  "??????????????????????????????           ??????????????????????????????"
}
Now I'd like each string in sys.map_table to be placed in a new table with each string having its own table of which the values are the characters in that string. Sounds confusing but this is what the new table should look like:
Code: [show] | [select all] lua
 my_table = {
  {"?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"," "," "," "," "," "," "," "," "," "," "," ","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"},
  {"?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"," "," "," "," "," "," "," "," "," "," "," ","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"},
}
Here is the function I am using to do such a thing:
Code: [show] | [select all] lua
function sys.scan_to_table()
  sr = {}
  local key = {
  ["?"] = "unknown",
  ["$"] = "black_hole",
  ["0"] = "planet_0",
  ["O"] = "planet_O",
  ["*"] = "star",
  ["N"] = "rocky_dead",
  ["M"] = "planet_M",
  ["B"] = "planet_B",
  ["A"] = "planet_A",
  ["D"] = "generic_dead",
  [" "] = "space",
  ["H"] = "home",
  ["C"] = "planet_C",
  ["Q"] = "planet_Q",
  }
  for k,v in pairs( sys.map_table ) do
    sr[k] = {}
    str = v:reverse()                      
    for count = 1, 71 do
      for symbol, description in pairs( key ) do   
        if str:ends( symbol ) then                        
          echo( count..": "..symbol.."\n")
          sr[k][count] = symbol
          str = str:cut( str:len() -1 )
        end
      end
    end
  end
end
The problem is that everytime there is a large group of spaces, the last space is not counted. So while each string of the map has 71 characters, my table is only grabbing 65-70 of them pending on how many groups of spaces are in the string.

If you create a global table named sys and paste in the two snippets of code I have here, you should be able to run sys.scan_to_table() without error. I've placed an echo in the function that shows what the count is along with the symbol its using. If you'll notice, after every group of space symbols, the count repeats itself.

Please please take the time to check this out and understand as I know it sounds complicated. But maybe one of you geniuses out there can see something I cannot.

User avatar
Belgarath
Posts: 232
Joined: Fri Jul 26, 2013 7:19 am
Discord: macjabeth#2283

Re: map table troubles

Post by Belgarath » Wed Jun 17, 2015 11:26 am

This method works without error:
Code: [show] | [select all] lua
function scan_to_table ()
  sr = {}
  local key = {
    ["?"] = "unknown",
    ["$"] = "black_hole",
    ["0"] = "planet_0",
    ["O"] = "planet_O",
    ["*"] = "star",
    ["N"] = "rocky_dead",
    ["M"] = "planet_M",
    ["B"] = "planet_B",
    ["A"] = "planet_A",
    ["D"] = "generic_dead",
    [" "] = "space",
    ["H"] = "home",
    ["C"] = "planet_C",
    ["Q"] = "planet_Q",
  }
  for k, v in pairs(map_table) do
    sr[k] = {}
    local count = 1
    for str in v:gmatch "." do
      for symbol, description in pairs(key) do
        if str == symbol then
          echo(count .. ": " .. symbol .. "\n")
          sr[k][count] = symbol
          break
        end
      end
      count = count + 1
    end
  end
end

User avatar
Akaya
Posts: 412
Joined: Thu Apr 19, 2012 1:36 am

Re: map table troubles

Post by Akaya » Wed Jun 17, 2015 3:05 pm

You sir, are a gentleman and a scholar! That worked perfectly. Thank you very much Belgarath!

User avatar
Akaya
Posts: 412
Joined: Thu Apr 19, 2012 1:36 am

Re: map table troubles

Post by Akaya » Wed Jun 17, 2015 3:12 pm

If you don't mind me asking, how does this line work:
Code: [show] | [select all] lua
 for str in v:gmatch "." do 


I'm not to familiar with gmatch "."

User avatar
Belgarath
Posts: 232
Joined: Fri Jul 26, 2013 7:19 am
Discord: macjabeth#2283

Re: map table troubles

Post by Belgarath » Wed Jun 17, 2015 3:30 pm

No problem. ;)

gmatch iterates over a string, allowing you to take action on each match (eg. on each word):
Code: [show] | [select all] lua
-- %a matches all letters
for word in string.gmatch("mudlet is the best", "%a+") do
  print(word)
end
In the map_table function I used a dot which represents any/all characters. Hope that clears up any confusion!

User avatar
Akaya
Posts: 412
Joined: Thu Apr 19, 2012 1:36 am

Re: map table troubles

Post by Akaya » Thu Jun 18, 2015 2:22 am

That it does! I will definitely be using this again! Thank you!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests