Tile Map Generation
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Tile Map Generation
I'm planning to make a 2d survival game that's a platformer, basically like Terraria. I just need a little help on how terrain generation should work. I was planning on make the terrain generate as you walk, but the problem is that when you explore a lot then the game will lag a bunch while it's trying to go through every single tile in the map table and drawing it. How should I make it generate massive maps without lagging?
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
Re: Tile Map Generation
Best bet is to use a sprite batch with an image with all your tile textures on it. It's very fast compared to drawing each tile individually as an image or quad.
Re: Tile Map Generation
That's exactly what I was planning to do. But you didn't understand my question. Ok, for example, here is how I was planning to make my map work like:markgo wrote:Best bet is to use a sprite batch with an image with all your tile textures on it. It's very fast compared to drawing each tile individually as an image or quad.
map = {
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0},
{0, 0, X, 0, 1, 0},
{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1},
}
As you can see, the map is 6x6 tiles large (fairly small but this is only an example). 0 is the air, 1 is the grass, and X is the player's position. If the player moves one tile to the right and it's missing a column of tiles because the player hasn't explored this part yet, then it generates a new column of tiles on the player's screen:
map = {
{0, 0, 0, 0, 0, 0, 0}, --Since the screen is 6x6 tiles large, only the red is visible by the player.
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, X, 1, 0, 0},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
}
When the player moves back to the left one tile, he has been there already so no new tiles need to be generated:
map = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, X, 0, 1, 0, 0},
{ 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 0},
}
But the problem is, after the player explores a lot, the map table will become huge which will take a while for a function to go through the table (example: when drawing the tiles) which will lag. My question was, how can I make the terrain generation work without having so much lag?
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
Re: Tile Map Generation
Hmm. I see that you are rebuilding the map each time you walk to a new area, with the exception that the size of each rebuild is bigger than the next. I think it would be best to randomly generate your entire map in chunks. Each chunk is randomly generated when your vision is within range of it and you would add it to your map. It's better than having to regenerate the entire map each time you move to a new area. If your algorithm relies on the entire map for random generation, you'll probably need to tweak it so you don't have to iterate through every tile.
Pseudocode:
Pseudocode:
Code: Select all
map = {...}
love.update(dt)
if not tileInRange(x,y,w,h) then -- x,y,w,h is the box area of our vision
chunk = generateChunk(...)
map:add(chunk)
end
end
Re: Tile Map Generation
Isn't that was I was doing when I was adding a column to the map if the player moved?markgo wrote:Hmm. I see that you are rebuilding the map each time you walk to a new area, with the exception that the size of each rebuild is bigger than the next. I think it would be best to randomly generate your entire map in chunks. Each chunk is randomly generated when your vision is within range of it and you would add it to your map. It's better than having to regenerate the entire map each time you move to a new area. If your algorithm relies on the entire map for random generation, you'll probably need to tweak it so you don't have to iterate through every tile.
Pseudocode:Code: Select all
map = {...} love.update(dt) if not tileInRange(x,y,w,h) then -- x,y,w,h is the box area of our vision chunk = generateChunk(...) map:add(chunk) end end
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
Re: Tile Map Generation
I don't see how my pseudocode will "lag" as the map gets bigger. The vision range remains the same through out. Are you iterating through every tile to draw or something? You realize that you can add your tiles to a sprite batch and do one draw call with the sprite batch:
Code: Select all
addChunkToSpriteBatch(spritebatch,chunk)
love.draw()
love.graphics.draw(spritebatch,...)
end
Re: Tile Map Generation
Oh yea, your right, I was just going through every tile.markgo wrote:I don't see how my pseudocode will "lag" as the map gets bigger. The vision range remains the same through out. Are you iterating through every tile to draw or something? You realize that you can add your tiles to a sprite batch and do one draw call with the sprite batch:
Code: Select all
addChunkToSpriteBatch(spritebatch,chunk) love.draw() love.graphics.draw(spritebatch,...) end
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
Re: Tile Map Generation
Wait, what if I wanted to change or check something in the map would I have to go through all the values in the map table?
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
Re: Tile Map Generation
If you just want to check a single tile, you can just read map[x][y]
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Tile Map Generation
I know but what if I want to update all water blocks? They can't just stay static in one place.Nixola wrote:If you just want to check a single tile, you can just read map[x][y]
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
Who is online
Users browsing this forum: Google [Bot] and 7 guests