1

I want to make a 2d top down zombie Apocalypse game with procedural generation in Lua.

Using multiple noise maps, I can easily get a 2d array for a heightmap, moisture map, and temperature map, wich I can use to generate realistic biomes. Thats easy to do, but, to reduce lag, I need to divide the map into chunks, so I only generate chunks once. I can also do that, but what I want is a huge open world, with rivers, trees(more trees in the forests), farms and other special buildings, villages, and cities, roads, ...

My current thought is, to have other smaller 2d arrays for the different structures, and randomly place them with another structure map, but I don't know how to do that, but also then buildings and roads could possibly overlap, which I don't want. And I don't think I can generate a single structure which is within 2 or more chunks (because the schematics would overwrite the map, where the structure is)

So how can I do this, can someone please help? Thank you!

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
GR00G0
  • 61
  • 3
  • You could rely on your height map matrices in order to favor some 'ideal' spots, bigger near water, smaller in mountain terrain. Also involve some distance minimal between towns. – Christophe Roussy Apr 27 '17 at 08:54

1 Answers1

3

Is the world static or dynamic? If trees can burn down and villages be destroyed but re-created elsewhere, etc. complexity jumps. Also, if villagers are present and have any sort of AI, they'll need to "know" what's around them, which can get sticky with procedural generation.

One "trick" would be to create world chunks and, when a new chunk needs created, generate and save structure data then. You can also have this happen as a "perimeter" function, where if player is in chunk A, in the background generate and save for any chunks around it that haven't already been generated.

Placing structures should be trivial. Chunk id and (chunk-specific) xy coordinates for the structures is all you need to place them. You can include structure "region" width and height to assist with structure placement collision detection. For say, a fenced in farm, you can just have the fencing rendered right there and the player can walk around it or whatever. For things like a dungeon in a mountain with a cave entrance, you'd want an "entrance structure" that, when the player interacts with it, puts the player in the dungeon.

Work on getting these mechanics figured out BEFORE trying to make the open world super huge. Work with a 2x2 or 3x3 chunk world first and test structures individually in it. This is more important than landscape decisions initially since the gameplay presumably will revolve around these structures more than the landscape.

Lua's a great choice for realizing your game, but you will probably eventually want to do the procedural generation / structure layouts / etc in C/C++. Use C/C++ to do the heavy lifting like the procedural generation, and turn that into a C-call that Lua/LOVE consumes. Here's some code that is not 2D top-down but takes into account generation, timing, etc. that can be helpful. Here's a city generator in Lua/LOVE. More on map generation from Stanford, and a Stackoverflow post. NPC movement in 2d map.

Have fun!

Community
  • 1
  • 1
ZagNut
  • 1,431
  • 15
  • 20
  • Can you elaborate a bit on _why_ C/C++ is better for these tasks? – Mike Lyons Apr 06 '17 at 13:28
  • @MikeLyons Not necessarily better. An algorithm's an algorithm. But per my loose suggestions above, as more features are incorporated in the world generation, the processing burden gets larger, and you'll need that simply for game performance. Certainly use Lua to flesh out the generation algorithm(s) especially if you're more comfortable doing this in that language, but ultimately as the generation gets more complex, you'll see performance start taking a big hit. – ZagNut Apr 07 '17 at 14:21
  • @MikeLyons (con't) When the performance starts dropping, you'll need something lower-level to take over. It doesn't even need to be C/C++ - you can use any low-level language that has Lua bindings. Also, I should add when I'm picturing this in my ever-aging brain, I'm thinking of "real-time" procedural generation (e.g. chunks reassembled on request and staged in memory). If the chunks are static data on disk, then really you only need to track state changes to that data (e.g. digging a hole on a ground tile). – ZagNut Apr 07 '17 at 14:32