building a map from mapsize variable.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
forestwolf42
Prole
Posts: 24
Joined: Tue Dec 20, 2011 5:18 pm

Re: building a map from mapsize variable.

Post 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.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: building a map from mapsize variable.

Post by MarekkPie »

My bad. They are just iterators. I forgot to put j = 1 and i = 1.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: building a map from mapsize variable.

Post 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].
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: building a map from mapsize variable.

Post 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.
User avatar
forestwolf42
Prole
Posts: 24
Joined: Tue Dec 20, 2011 5:18 pm

Re: building a map from mapsize variable.

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: building a map from mapsize variable.

Post by bartbes »

Jasoco's code does this..
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: building a map from mapsize variable.

Post 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]
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: building a map from mapsize variable.

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests