1010#include " ../utils/Utils.h"
1111
1212World::World () {
13- this ->chunks = std::map<std::string, Chunk *>();
14- this ->biome_types = std::map<std::string, BiomeType *>();
15-
13+ this ->chunks = std::map<std::string, std::shared_ptr<Chunk>>();
14+ this ->biome_types = std::map<std::string, std::shared_ptr<BiomeType>>();
1615 this ->initializeBiomes ();
1716}
1817
@@ -28,38 +27,28 @@ void World::update(double x, double y) {
2827}
2928
3029void World::loadChunk (int chunk_x, int chunk_y) {
31-
3230 std::string key = std::to_string (chunk_x) + " _" + std::to_string (chunk_y);
33- Chunk * chunk = nullptr ;
31+ std::shared_ptr< Chunk> chunk;
3432
3533 if (this ->chunks .count (key) > 0 ) {
3634 chunk = this ->chunks [key];
3735 } else {
38- chunk = this ->generateChunk (chunk_x, chunk_y);
36+ chunk = std::move ( this ->generateChunk (chunk_x, chunk_y) );
3937 this ->chunks [key] = chunk;
4038 }
41-
42-
4339 chunk->render ();
4440}
4541
46- Chunk * World::generateChunk (int chunk_x, int chunk_y) {
42+ std::unique_ptr< Chunk> World::generateChunk (int chunk_x, int chunk_y) {
4743 std::string key = std::to_string (chunk_x) + " _" + std::to_string (chunk_y);
48-
49- BiomeType *biome = this ->chooseChunkBiome (chunk_x, chunk_y);
50-
51- Chunk *chunk = nullptr ;
52- chunk = new Chunk (chunk_x, chunk_y, biome);
53-
54-
44+ std::shared_ptr<BiomeType> biome_type = this ->chooseChunkBiome (chunk_x, chunk_y);
45+ std::unique_ptr<Chunk> chunk (new Chunk (chunk_x, chunk_y, biome_type.get ()));
5546 return chunk;
5647}
5748
5849int World::getBlock (int x, int y, int z) {
59-
6050 std::string key = std::to_string (int (floor (x / 16.0 ))) + " _" + std::to_string (int (floor (z / 16.0 )));
61-
62- Chunk *chunk = nullptr ;
51+ std::shared_ptr<Chunk> chunk;
6352 // /Chunk
6453 if (this ->chunks .count (key) > 0 ) {
6554 chunk = this ->chunks [key];
@@ -78,12 +67,11 @@ int World::getBlock(int x, int y, int z) {
7867 }
7968
8069 return chunk->getBlock (x, y, z);
81-
8270}
8371
8472int World::getTerrainHeight (int x, int z) {
8573 std::string key = std::to_string (int (floor (x / 16.0 ))) + " _" + std::to_string (int (floor (z / 16.0 )));
86- Chunk * chunk = nullptr ;
74+ std::shared_ptr< Chunk> chunk;
8775 if (this ->chunks .count (key) > 0 ) {
8876 chunk = this ->chunks [key];
8977 } else {
@@ -103,8 +91,7 @@ int World::getTerrainHeight(int x, int z) {
10391 return chunk->getHeight (x, z);
10492}
10593
106- BiomeType *World::chooseChunkBiome (int chunk_x, int chunk_y) {
107-
94+ std::shared_ptr<BiomeType> World::chooseChunkBiome (int chunk_x, int chunk_y) {
10895 // For the first chunk
10996 if (this ->biomes .empty ()) {
11097 // Force desert because the biome is cool
@@ -115,7 +102,6 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
115102 return this ->biome_types [key];
116103 }
117104
118-
119105 // Get the nearest biome
120106 float best_distance = this ->biomes [0 ].distance_to (chunk_x, chunk_y);
121107 Biome best_distance_biome = this ->biomes [0 ];
@@ -129,7 +115,6 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
129115 }
130116
131117 if (best_distance > 20 ) {
132-
133118 std::vector<std::string> possibleBiomeTypes;
134119
135120 for (auto const &biome_type: this ->biome_types ) {
@@ -138,7 +123,6 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
138123 }
139124 }
140125
141-
142126 auto it = possibleBiomeTypes.begin ();
143127 advance (it, utils::random (RAND_MAX) % possibleBiomeTypes.size ());
144128 Biome biome (*it, chunk_x, chunk_y, this ->biome_types [*it]);
@@ -149,9 +133,8 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
149133 return best_distance_biome.type ;
150134}
151135
152- std::vector<Chunk *> World::getNeighborsChunks (int x, int z) {
153-
154- std::vector<Chunk *> neighbors;
136+ std::vector<std::shared_ptr<Chunk>> World::getNeighborsChunks (int x, int z) {
137+ std::vector<std::shared_ptr<Chunk>> neighbors;
155138
156139 for (int i = -1 ; i <= 1 ; ++i) {
157140 for (int j = -1 ; j <= 1 ; ++j) {
@@ -163,24 +146,21 @@ std::vector<Chunk *> World::getNeighborsChunks(int x, int z) {
163146 }
164147 }
165148 }
166-
167- return std::vector<Chunk *>();
149+ return neighbors;
168150}
169151
170152void World::initializeBiomes () {
171-
172- this ->biome_types [" prairie" ] = new BiomeType ();
153+ this ->biome_types [" prairie" ] = std::make_unique<BiomeType>(new BiomeType ());
173154 this ->biome_types [" prairie" ]->id = " prairie" ;
174155 this ->biome_types [" prairie" ]->ground = GRASS;
175156 this ->biome_types [" prairie" ]->tree_frequency = 0.02 ;
176157
177- this ->biome_types [" desert" ] = new BiomeType ();
158+ this ->biome_types [" desert" ] = std::make_unique<BiomeType>( new BiomeType () );
178159 this ->biome_types [" desert" ]->id = " desert" ;
179160 this ->biome_types [" desert" ]->ground = SAND;
180161 this ->biome_types [" desert" ]->cactus_frequency = 0.02 ;
181162
182-
183- this ->biome_types [" mountain" ] = new BiomeType ();
163+ this ->biome_types [" mountain" ] = std::make_unique<BiomeType>(new BiomeType ());
184164 this ->biome_types [" mountain" ]->id = " mountain" ;
185165 this ->biome_types [" mountain" ]->ground = GRASS;
186166 this ->biome_types [" mountain" ]->rock_frequency = 0.05 ;
0 commit comments