Breaking Down the ASCII String

The ASCII string is defining the map, so it can be good to understand how the ASCII string is being turned into a 2D table that will be used later to create a 3D map.

The ASCII String

When you look at the ASCII string, think of it as 1 line of string with no line breaks. The Clean function you wrote in the previous section will remove characters that are not supported, this includes line breaks. So then you get a string that looks like this:

222222200002200002200002222222

ASCII Width and Height

Because you know the height and width of the map that will be generated, you can use those values to determine where the rows are in that string. Combined with the width, you then have access to each character per row. Looking at the string below, you can see there are 5 rows (MAP_HEIGHT is set to 5)

Row 1
222222
Row 2
200002
Row 3
200002
Row 4
200002
Row 5
222222

Accessing the Characters

Accessing the individual characters (columns) requires a little more thought because you need to start a specific position in the ASCII string for each iteration of the MAP_HEIGHT loop. Below is some simplified code from the previous section where you can focus just on how accessing rows and columns is done.

local MAP_HEIGHT = 5
local MAP_WIDTH = 6
local map = "222222200002200002200002222222" -- ASCII string cleaned up.
local mapStr = "" -- Will be used to rebuild the ASCII string with line breaks.

for r = 1, MAP_HEIGHT do -- Loop through the height (rows), 1 to 5.
	for c = 1, MAP_WIDTH do -- Loop through the width (columns), 1 to 6.
		mapStr = mapStr .. string.sub(map, ((r - 1) * MAP_WIDTH) + c, ((r - 1) * MAP_WIDTH) + c)
	end

	mapStr = mapStr .. "\n"
end

print(mapStr)
Code language: Lua (lua)

The string.sub function is going to return 1 character (column) from the map, but to do this, you need to work out where to get that character from the ASCII string. Both second and third arguments of the string.sub function is the same, because you only need to get 1 character.

mapStr = mapStr .. string.sub(map, ((r - 1) * MAP_WIDTH) + c, ((r - 1) * MAP_WIDTH) + c)
Code language: Lua (lua)

Lua does indexing at 1 and not 0 like most other languages. So when you access an element in an index value table, the first element can be accessed using myTable[1], whereas using 0 indexing would return an error. Because of this, you need to subtract 1 from the variable r (row index), otherwise when you multiple the MAP_WIDTH, you will return a sub string starting at 6.

Each iteration of the outer loop which is looping over the MAP_HEIGHT (6 rows) will increment the variable r by one. This will allow you to access each block of characters (rows). By adding on the c variable (column index), you can get each character from the row.

For example, if you want to access the last character of row 5 (colored blue), it can be broken down in these steps:

  • The r (row) variable would be 5, and the c (column) variable would be 6 for the current iteration of both loops.
  • You subtract 1 from r to return it to 0 indexing.
  • The first character of row 4 (r - 1) is 2 (character index 24). (r - 1) * MAP_WIDTH gives the starting character of the row.
  • Then to access the last character 2 (character index 30), the variable c (column) is added on ((r - 1) * MAP_WIDTH) + c.

Row 1
222222
Row 2
200002
Row 3
200002
Row 4
200002
Row 5
222222

Scroll to Top