WorldSim  inDev
2D tile-based sandbox RPG with procedurally generated fantasy world simulator 🌏
World_MapManager.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef WORLDSIM_WORLD_MAPMANAGER_HPP
3 #define WORLDSIM_WORLD_MAPMANAGER_HPP
4 
5 /* WorldSim: World_MapManager.hpp
6 #include "World_MapManager.hpp"
7 
8 Manages the local maps, generating and caching them as required.
9 
10 Currently I'm just working on generation.
11 
12 This is basically a container for Map_Local objects, with multithreading support.
13 
14 */
15 
16 class World;
17 
18 #if defined WILDCAT_THREADING
19  #include <thread>
20  #include <mutex>
21  #include <shared_mutex>
22  #include <atomic>
23 #endif
24 
25 
26 // Keeping track of caching order:
27 // using a list is maybe not ideal because every time we push to the
28 // list we need to search the entire list anyway.
29 // Instead it's probably best if each map carries an accessID.
30 // When the accessID reaches overflow we can quickly rebuilt it from 0.
31 
32 
34 {
35  int maxWorldsToGenerate; // max number of worlds to generate at once.
36 
37  Vector <World_Local*> vMapCache; // Stores all generated maps
38  Vector <World_Local*> vJobs; // Vector of maps to be loaded in
39 
40  Vector <World_Biome*> vBiome; // Vector of biomes to load/simulate
41 
42  //Vector <World_Local*> vUnloadJobs; // Vector of maps to be unloaded
43  //Vector <World_Local*> vImportantMaps; // Maps to be kept loaded in.
44 
45 #ifdef WILDCAT_THREADING
46  std::mutex mutexArrayAccess;
47  std::mutex mutexArrayResize;
48  std::shared_mutex mutexArrayResize2;
49 
50  std::mutex mutexVector;
51  std::mutex mutexJob;
52 
53  Mutex mutexBiome;
54 
55  // keep track of number of threads running
56  // to perform safe shutdown
57  std::atomic <int> nThreads;
58  std::atomic <int> nBiomeThreads;
59 
60  //std::atomic <int> nBiomeTiles; // The current number of biome tiles in memory.
61 #endif
62 
63  public:
64  ArrayS2 <World_Local> aWorldTile; // pointer to World->Local Map array.
65 
68 
69  // Return a World_Local object for this local map. Will generate a local map if necessary. Returns null pointer for invalid request.
70  inline World_Local* operator() (const int _x, const int _y);
71 
72  void addBiome(World_Biome* _biome);
73 
74  void init(unsigned int _nX, unsigned int _nY, ArrayS2 <World_Local>* aWorldTile2);
75  void main(); // Threaded NPC generator/simulator (per tile)
76  void mainBiome(const unsigned short int /* sleepTime */); // threaded biome generator/simulator (per biome)
77 
78  // Generate map immediately, because we need it.
79  bool generateNow(int _mapX, int _mapY);
80 };
81 
82 #endif
void main()
Definition: World_MapManager.cpp:168
World_Local * operator()(const int _x, const int _y)
Definition: World_MapManager.cpp:87
Definition: World.hpp:50
Definition: World_Biome.hpp:46
Definition: World_Local.hpp:58
ArrayS2< World_Local > aWorldTile
Definition: World_MapManager.hpp:64
~World_MapManager()
Definition: World_MapManager.cpp:26
World_MapManager()
Definition: World_MapManager.cpp:17
void addBiome(World_Biome *_biome)
Definition: World_MapManager.cpp:163
Definition: World_MapManager.hpp:33
void mainBiome(const unsigned short int)
Definition: World_MapManager.cpp:294
bool generateNow(int _mapX, int _mapY)
Definition: World_MapManager.cpp:105
void init(unsigned int _nX, unsigned int _nY, ArrayS2< World_Local > *aWorldTile2)
Definition: World_MapManager.cpp:58