0
\$\begingroup\$

Tell me if i need to be more specific.

My program is advanced enough that it can read tile data from a .txt file and display a tile image for each tile ('~' = water), I am trying to make my game use a procedurally generated world, at first i just appended to a vector, but then i found out about perlin noise, At the moment, i can generate a seed:

 let perlin = Perlin::new();
 let world_seed = perlin.get(
     [rand::thread_rng().gen_range(35.0..42.4), 
     rand::thread_rng().gen_range(30.0..37.7), 
     rand::thread_rng().gen_range(2.0..2.8)]);

But i do not know how to write noise data to a vector to create a 5000 x 5000 tile map,

example:

Example Map

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Perlin Noise

You should be able to query the Perlin noise with some 2D coordinates.

If you are using this Peril: noise::Perlin, then you can call get with a Point2. If you are using this Perlin: perlin_rust::PerlinNoise then you can call perlin2 with x and y values. Most Perlin implementations have a way to do it.

So you can query for the coordinates of a given position on the tilemap, and from the result decide what will be there in that tile, and add it to the tilemap data. You do that for every tile you want to generate. You would, of course, have some logic to decide if it is water or not, and so on, based on the result form the perlin noise.


Presumably the contents of a tile do not come from a single variable. Consider using an extra dimension so you can query the perlin noise at the same 2D coordinantes of the tile, but at different positions along the extra dimension. So you have multiple values you can use to decide what is in the tile. For example, one could be elevation, another could be vegetation, another could be temperature, and so on.


Storing to file

If you are not going to manipulate the tilemap, then storing the seed for the perlin noise is enough to store the tilemap. Because it should generate the same way from the same seed.

Thus, if you need to serialize the tilemap, I presume that either you need it in a particular format for another software to read, or you will manipulate it further after you generated it from perlin noise. For example, you might be picking spawn points or generating structures. Keep in mind that this may not only require modifying the tiles, but also adding metadata to the tiles, which you would also need to serialize.


Perhaps serializing the tilemap requires no manipulation at all, and you can simply write the memory representation to a file.

Otherwise, perhaps you want to start with some header with the tilemap size at the start of the file. So you can start adding that to the data that you will write. Although if you are going for human readable perhaps you simply use newlines. I'm trying to show you alternative scenarios of how the serialization might go.

Then we have the same question for the individual tiles. If the memory representation of the tile is the same as the file representation, you can just copy them to the data you will write.

Otherwise, you will need to iterate over each tile, figure out how the tile will be represented in the file, and adding that to the data you will write.

And that is assuming here that it will be stored as a single compact blob. However, you may want to break it into chunks, or have some other structure that helps to query nearby tiles for a given position. And then may have to also store an index for that structure. Yes, I'm saying you may want to serialize with that structure, so you don't have to expend extra time generating it when you load the tilemap.

Then you could manipulate the data you will write, for example you may want to compress it. And finally write it to a file.


Storing to file does not have to deal with perlin noise. However, if writing directly to file is a requirement, you could have a single loop that queries the perlin noise, decides what the tile will be, and how it is encoded, and writes it to file. Then you would have to read it to display it. And I remind that this would imply you are not manipulating the tilemap after you generated it from the perlin noise, so you could be writing the seed instead, unless you need the tilemap in some particular format.

Otherwise, you may want to display the result without storing it to file, and for that you better have a representation of the tilemap data in memory which you can either serialize to a file, or display, or whatever.

\$\endgroup\$

You must log in to answer this question.

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