Page 2 of 2

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 10:11 pm
by forestwolf42
MarekkPie wrote:Well, if you just want to build a table of size WIDTH, HEIGHT, just:

Code: Select all

map = {}
for j, HEIGHT do
  for i, WIDTH do
    table.insert(map, {x = i, y = j, visited = false})
  end
end
At that point, then follow one of the algorithms in that wikipedia article. If that still isn't what you need, then I have no idea what you're asking.
Thank you this is exactly what I was wondering, but what are j and i? I still have a very poor understanding of Lua tables and I have read the tutorial on the wiki a couple times nothing has really helped me understand what exactly all the table commands are and what they do.

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 10:13 pm
by MarekkPie
My bad. They are just iterators. I forgot to put j = 1 and i = 1.

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 10:17 pm
by Jasoco
I don't see how that grid creation mode is going to actually work vs. mine that puts each grid square in its own table cell with the X and Y being their indices. i.e. map[x][y].

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 10:19 pm
by MarekkPie
I already said yours was better :)

You can still do it...but it would require the modulus operator, and the modulus operator make me cry inside.

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 11:28 pm
by forestwolf42
Jasoco wrote:As I said, you start with a grid of a certain size:
Although this will probably be very useful later, I think I phrased my question poorly, I'm not trying to fill a grid with anything right now, I'm trying to create the grid. So in pseudo-code

Code: Select all

sizeY = 5
sizeX = 5
map = magicfunction
map is 
{
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1}
}
now if sizeY was 4 and sizeX 6 map would be

Code: Select all

map = {
{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1} 
I'm trying to create the magic function that would do this, or do I just have to always create the map table manually? Thank you for your patience with me.

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 11:30 pm
by bartbes
Jasoco's code does this..

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 11:32 pm
by MarekkPie
Both of our methods do that. If you want to fill it with 1's, then simply make the node equal to 1.

Jasoco's:

Code: Select all

map = {}
for j = 1, HEIGHT do
  map[j] = {}
  for i = 1, WIDTH do
    map[j][i] = 1
  end
end
A visual representation of Jasoco's:

Code: Select all

[1][2][3]...[WIDTH]
[2]
[3]
.
.
.
[HEIGHT]

Re: building a map from mapsize variable.

Posted: Tue Jan 10, 2012 11:36 pm
by Jasoco
forestwolf42 wrote:
Jasoco wrote:As I said, you start with a grid of a certain size:
Although this will probably be very useful later, I think I phrased my question poorly, I'm not trying to fill a grid with anything right now, I'm trying to create the grid.
And I already told you exactly how to do that.

Code: Select all

map = {}
for x = 0, mapWidth - 1 do
  map[x] = {}
  for y = 0, mapHeight - 1 do
    map[x][y] = { marked = false }
  end
end
It doesn't matter what you make map[x][y] equal, that's up to you. You can set it to 0 or 1 or nil or true or whatever you want. In my example I made it a table so you can easily expand on it. i.e. each grid square can hold however many "properties" you want it to. Like whether it's solid or not, what its tile image is, or in my example, whether it's marked as visited or not. You'll want to mark a square as visited as you are drawing the maze when you visit a square, and unless you pad the grid with extra squares between each cell, you'll want to also mark each square as having separate exits. i.e. which sides of the square are open to the next one.

It will be complicated. But in the end, worth it. My example .love was created as a Link's Awakening style dungeon engine with each room having 1 to 4 exits. North, south, east and west. And each is marked with a door or as solid and the door can be opened or closed. Etc... It's crappy code though and will probably be confusing.

It's how I create all my grids. And pretty much every game I've started making uses a grid. I've typed that same code sooooo many times it's gotten to the point I no longer hate doing it.