2
\$\begingroup\$

GOAL

I am making a top-down tile-based sandbox game, and it will have 400 layers of destroyable terrain, It will not use chunks, it will store the map in memory like Terraria, but only render the tiles on-screen, I don't want to take the approach that Dwarf Fortress does. Where it only renders one layer at a time. I want the player to be able to see a valley from the top of a mountain, not just the peak they are standing on.

PROBLEMS

I am not sure how i would store the 400 layers on disk, I think a file for each layer would be a nightmare for loading times.

Even if i only render the tiles on-screen, it would still probably cause lag, because it is still rendering 400 layers.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

For the loading time... Why not chunks? CHUNKS!


You don't need to render 400 layers. You only need to render the top one for each tile. That is, for each visible tile iterate over the layers, top to bottom, find the first one with something visible, and render that one.

Also, what was visible last frame, is probably the same that is visible this frame. And to paraphrase David Wheeler:

All problems in computer science can be solved by another level of indirection layer.

So, let us have a "visible layer layer", where annotate what is the top visible layer for each tile. So you don't need to iterate over all the layers every frame, you only do it once and reuse it. And update it when building or destroying.

If you have a cutout layer (e.g. to see underground), then when moving the cutout layer downwards, you only need to recompute any tile where the layer is higher than the cutout layer. And when moving the cutout layer upwards, you only need to check between the old cutout layer and the new one, if there is something to render.

\$\endgroup\$
2
  • \$\begingroup\$ That sounds like a good idea. One problem though, if you are on top of a mountain, you still will have to render around 200 layers, and i think that would cause lag on lower end machine \$\endgroup\$ Mar 24, 2022 at 20:36
  • \$\begingroup\$ @MachoOnion Perhaps my answer is not clear enough. You would be doing only want draw call per tile. Not 200, not 400, 1. And you would not be figuring out which draw call to do every frame, you cache that. Heck, you can copy it if the issue is cache locality. \$\endgroup\$
    – Theraot
    Mar 24, 2022 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .