WorldSim  inDev
2D tile-based sandbox RPG with procedurally generated fantasy world simulator 🌏
World.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef WORLDSIM_WORLD_HPP
3 #define WORLDSIM_WORLD_HPP
4 
5 /* WorldSim: World.hpp
6 #include "World.hpp"
7 
8 The World stores global-level data, such as the position of global units.
9 
10 Map caching and generation is to be handled by World_MapManager.
11 
12 Local Map data is stored in World_Local.
13 
14 */
15 
16 #include "Mythology.hpp"
17 #include "Mythology_Deity.hpp"
18 
19 #include "World_Events.cpp"
20 #include "World_Astronomy.hpp"
21 #include "LocalTile.hpp"
22 #include "World_Local.hpp"
23 #include "World_Landmass.hpp"
24 #include "World_Biome.hpp"
25 #include "World_MapManager.hpp"
26 
27 #include <Graphics/Png/Png.hpp>
28 #include <File/FileManager.hpp>
29 #include <Container/ArrayS2/ArrayS2.hpp>
30 #include <Interface/IdleTick/IdleTickInterface.hpp>
31 #include <Interface/LogicTick/LogicTickInterface.hpp>
32 #include <System/Time/Timer.hpp>
33 
34 #include <File/SaveFileManager.hpp>
35 #include <Math/Random/GlobalRandom.hpp>
36 #include <Game/Language/NameGenerator.cpp>
37 
38 #include <atomic>
39 #include <map>
40 
41 class Civ;
42 class Civ_Dwarven;
43 class WorldObjectGlobal;
44 class Character;
45 class Tribe;
46 class Tribe_Human;
47 class Tribe_Dwarven;
48 class Tribe_Elf;
49 
50 class World: public LogicTickInterface, public IdleTickInterface, public SaveFileInterface
51 {
52  private:
53  RandomNonStatic random;
54  World_MapManager mapManager;
55  // We could remove this later by using a pointer.
56  ArrayS2 <enumBiome> aTerrain;
57 
58  //Vector <Mythology_Deity*> vDeity;
59 
60  public:
61 
62  // This flips to true once the player has entered the world simulator, which calls startSimulation().
63  // some functionality shouldn't work if the world isn't active, especially something like generating
64  // local maps.
65  bool active;
66 
67 
68  #if defined WILDCAT_THREADING
69  std::atomic <bool> generated;
70  #else
71  bool generated; /* False until a world has been generated. Prevents trying to simulate a non-existent world. */
72  #endif
73 
74  // The size of the world, measured in tiles.
75  int nX, nY;
76  // The maximum global coordinate
77  unsigned long int maximumX, maximumY;
78 
79  int simX, simY;
80 
81  /* The current tile that the player wants information on */
83  // same but for local map tile (if it exists)
85 
86  long long unsigned int ticksBacklog; /* World will simulate these ticks whenever it can, while still relinquishing for input etc. */
89 
90  // This array stores the base terrain data.
91  //ArrayS2 <enumBiome> aTerrain;
92 
93  std::string name; /* The name of the world */
94  std::string strSavePath; /* The path to the world's save data. Typically something like: "savedata/<world name>". */
95  std::string worldFilePath;
96 
97  // Stores astronomical details from perspective of World.
99 
101 
102  /* Counters to regulate logic */
105 
106  /* This array stores the colour map for rendering. */
107  ArrayS3 <unsigned char> aTopoMap;
108 
109  ArrayS2 <WorldObject*> aWorldObject;
110  Vector <WorldObjectGlobal*> vWorldObjectGlobal;
111 
112  // Stores all tile-specific information
113  ArrayS2 <World_Local> aWorldTile;
114  // A vector of all local maps in memory. These worlds can be rendered and simulated.
115  Vector <World_Local*> vWorldLocal;
116 
117  // All of these should be ported into aWorldTile.
118  ArrayS2 <int> aBiomeID;
119  ArrayS2 <int> aRiverID; /* Contains the river ID */
120 
121  //Vector <std::string> vLandmassName;
122  Vector <World_Landmass*> vLandmass;
123  Vector <World_Biome*> vBiome;
124 
125  // A vector of vectors used to get a random tile. Vector index represents the row. Vector value represents column. Shuffle row after use.
126  // NOTE: UNFORTUNATELY THIS DOES NOT PRODUCE A RANDOM DISTRIBUTION.
127  Vector < Vector <int> * > vAllTiles;
128  // New approach, just a single vector. Unfortunately will take longer to shuffle.
129  // Can be optimised by shifting begin and end pointers.
130  Vector <HasXY*> vAllTiles2;
131 
132  unsigned char seaLevel;
133  unsigned char mountainLevel;
134 
135  Vector <Tribe*> vTribe;
136  Vector <Civ*> vCiv;
137 
138  ArrayS2 <unsigned char> aGoodEvil;
139 
140 
141  //Calendar calendar;
142  Calendar lastDate; /* The date on the last logic tick. */
143 
146 
147  // STATES
148  bool isRaining;
149 
150  // keep track of how many ticks for deity actions
152 
153  World();
154  ~World();
155 
156  // CHECK ALL CIV LOS ARRAYS, AND IF ANY OVERLAP, THEN MAKE THEM DISCOVER EACH OTHER.
157  //void updateCivContacts();
158 
159  // Return a World_Local object for this local map. Will generate a local map if necessary. Returns null pointer for
160  // invalid request. Also returns null pointer if world is not ready yet. Do not call this function until you are in
161  // World Simulator mode unless you like hard-to-track crashes.
162  inline World_Local* operator() (const int _x, const int _y);
163 
164  //New operator: Gets tiles using global coordinates.
165  // IT DOES NOT RETURN WORLD_LOCAL LIKE THE OTHER FUNCTION
166  // Yeah this is a confusing overload which needs to be fixed in the future.
167  // This will generate a local map if necessary.
168  inline LocalTile* operator() (unsigned long int _x, unsigned long int _y, const bool subterranean=false);
169 
170  inline LocalTile* operator() (HasXY2 <unsigned long int>*, const bool subterranean=false);
171 
172  // COORDINATE CONVERSIONS
173 
174  // Pass absolute coordinates and recieve world and local relative coordinates.
175  bool absoluteToRelative (const unsigned long int _absoluteX, const unsigned long int _absoluteY, int * _globalX, int * _globalY, int * _localX, int * _localY);
176 
177  // Returns true if the tile is in a map that is loaded.
178  bool isGenerated(unsigned long int _absoluteX, unsigned long int _absoluteY);
179 
180  bool generateMythology(); // Generate the base mythology
181 
182 
183  // STATE CHANGES
184  // Transition from generation to simulation.
185  void startSimulation();
186  // Load up the required local map, do any required logic, center map on character. Return false if error.
188 
189  /* TICK LOGIC */
190  /* I want this to be the new method of incrementing time in the world. It will dynamically abstract things based on the amount of turns to simulate. Each tick is one second. */
191  void incrementTicks ( int /* nTicks */ );
192  void incrementTicksBacklog ( long long unsigned int /* nTicks */ );
193  bool handleTickBacklog ();
194  void idleTick();
195  void logicTick();
196 
197  /* UPDATES */
198  /* World updates not relying on ticks. For example loading in maps that the player is near. */
199  void updateMaps();
200 
201  /* FILE IO */
202  bool loadWorld(std::string /* world name */);
203 
204  /* WORLD GENERATION */
205 
206  void generateWorld (const std::string /* _name */, const int /* x */, const int /* y */, int /* seed */, int /* fragmentation */, const bool /* islandMode */, const bool /* wrapX */, const bool /* wrapY */, const double /* landPercent */);
207  // Generate a local map so it can be viewed and interacted with.
208  void generateLocal(const int /* worldX */, const int /* worldY */);
209  void generateLocal(HasXY*);
210  //Unload a local map from RAM and into a save file.
211  void unloadLocal(const int /* worldX */, const int /* worldY */);
212 
213  void buildArrays(WorldGenerator2& wg);
214  // Find all unique areas and give them names.
215  void nameRegions();
216  void generateTribes( int/* nTribes */, int /* nTribesDwarven */, int /* nTribesElven */);
217  //Put down some Dwarven civs.
218  bool addRace (int /* nTribes */, std::string /* name */);
219  //bool addElvenTribe(int nTribes);
220  //bool addHumanTribe(int nTribes);
221 
222 
223  // Since the races are hardcoded, they should be their own functions.
224  void generateDwarves(int /* nCivs */ ); // Dwarves spawn directly into mountain tiles
225  void generateElves(int /* nTribes */ );
226  void generateHumans(int /* nTribes */ );
227 
228  // Ensure conditions are set up for this character to be controlled.
229  // For example generate the local map.
230  void controlCharacter (Character*);
231 
233 
234  // Return vector of coordinates visible from given coordinate.
235  Vector <HasXY2 <unsigned long int> *> * rayTraceLOS (unsigned long int _x, unsigned long int _y, const int RANGE, const bool /* isSneaking = false */, const bool subterranean=false);
236  void rayTrace (unsigned long int _x1, unsigned long int _y1, unsigned long int _x2, unsigned long int _y2, Vector <HasXY2 <unsigned long int> *> * vVisibleTiles, bool subterranean=false);
237 
238  /* WORLD QUERIES */
239  /* Returns population of all characters (Human, Dwarven, Elven)
240  alive in the world. */
241  int getPopulation();
242 
243  /* WORLD TILE / COORDINATE QUERIES */
244 
245  // RETURNS TRUE IF THE TILE IS LAND. RETURNS FALSE IF IT IS NOT LAND, OR IF IT IS OUT OF BOUNDS.
246  bool isLand(int _x, int _y);
247  inline bool isLand(HasXY*);
248 
249  // RETURNS TRUE IF THE COORDINATES ARE WITHIN BOUNDS OF THE MAP.
250  // We need to integrate HasXY with template.
251  bool isSafe (int _x, int _y);
252  // SAME BUT FOR ABSOLUTE COORDINATES.
253  bool isSafe (unsigned long int _x, unsigned long int _y);
254 
255  /* Return a pointer to the WorldTile at the coordinates.
256  Return null pointer if fail */
257  World_Local * getTile (const int /* x */, const int /* y */ );
258  /* Return the fertility value of the tile. */
259  int getTileFertility(const int /* x */, const int /* y */);
260  int getTileFertility(const HasXY* /* xy */);
261  /* Returns the total fertility of the passed tile, and its 8 neighbors. */
262  int getSurroundingFertility (const int _x, const int _y);
263 
264  /* Return average hunting yield of tile. Hunting is all non-agricultural food.
265  Hunting yield is the average of the surrounding tiles, plus the tile the tribe is standing on.
266  This is important because coastal tiles provide high yield. */
267  int getHuntingYield( const int _x, const int _y);
268 
269  /* Set the pointers to a coordinate of a random tile in the world. */
270  void getRandomTile (int* x, int* y);
271  /* Set the pointers to a coordinate of a random land tile in the world. May fail, in which case returns false and sets coords to (0,0). */
272  bool getRandomLandTile(int* x, int* y);
273 
274  HasXY* getRandomLandTile();
275 
276  HasXY* getRandomTileOfType(enumBiome _type);
277 
278  // RETURN ANY OBJECTS WHICH ARE NEXT TO THE PASSED OBJECT.
279  Vector <WorldObjectGlobal*>* getNeighboringObjects(WorldObjectGlobal*);
280 
281  // Put a worldobject anywhere in the world. If x,y are not provided
282  // It will use the object's own coordinates.
283  bool putObject(WorldObjectGlobal* _object, int x = -1, int y = -1);
284  // Remove object from world.
285  bool removeObject(WorldObjectGlobal* _object);
286 
287  //Return the name of the landmass the tile is on. Return empty string if not on a landmass.
288  std::string getLandmassName(const int /* x */, const int /* y */);
289  std::string getLandmassName (HasXY*);
290 
291  World_Biome* getBiome(const int id);
292  World_Biome* getBiome(const int x, const int y);
293  std::string getBiomeName( const int /* x */, const int /* y */);
294 
295  //INFO GETTING FUNCTIONS
296  std::string getTileType (const int _x, const int _y);
297  // provide info on the info panel for this tile
298  void queryTile( int hoveredXTile, int hoveredYTile);
299  // provide info on the info panel for this local tile
300  void queryTileLocal( int hoveredXTile, int hoveredYTile);
301 
302  // TRIBE FUNCTIONS
303  void addInfluence(Tribe* tribe, int amount);
304  // Subtract an influence point from every tile this tribe has.
305  void degradeInfluence(int amount=1);
306  //Set influence to 0.
307  void destroyInfluence(Tribe* tribe);
308  /* Return pointer to tribe with most influence here. Return null pointer if no tribe */
309  Tribe* getDominantInfluence (const int, const int);
310  Tribe* getDominantInfluence (HasXY*);
311 
312  Vector <Tribe*>* getTribesOn(const int /* x */, const int /* y */);
313 
314  // Check if tribe is in engagement range of another tribe.
315  // If yes, return pointer to Tribe, otherwise return 0.
316  Tribe* combatCheck (Tribe* );
317 
318  //Returns true if this landmass has an unclaimed tile.
319  bool hasFreeTerritory(int landmassID);
320  // Return number of unclaimed tiles on this landmass.
321  int nFreeTerritory (int landmassID);
322 
323  void evolveToCiv( Tribe * );
324 
325  // Allow deities to act
326  void incrementDeities(int /* nTicks */ );
327 
328  // Return the nearest tribe to the passed tribe which is on the same landmass.
329  // Return 0 if none. Only check same race by default.
330  Tribe * getNearestConnectedTribe (Tribe *, bool sameRace = true);
331 
332  int getHighestInfluence(const int, const int);
333  int getHighestInfluence(HasXY*);
334 
335  bool hasSettlement(const int, const int);
336 
338  void buildMinimap();
339 
340 //SAVEFILEINTERFACE
341  virtual void save();
342 
343 };
344 
345 #endif
~World()
Definition: World.cpp:81
Tribe * getNearestConnectedTribe(Tribe *, bool sameRace=true)
Definition: World.cpp:2386
HasXY * getRandomTileOfType(enumBiome _type)
Definition: World.cpp:2105
World astronomical detail container This class stores astronomical data from the perspective of the W...
Definition: World_Astronomy.hpp:36
bool generateMythology()
Definition: World.cpp:205
Vector< World_Landmass * > vLandmass
Definition: World.hpp:122
World_Biome * getBiome(const int id)
Definition: World.cpp:2212
void addInfluence(Tribe *tribe, int amount)
Definition: World.cpp:2137
int dailyCounter
Definition: World.hpp:103
Vector< Civ * > vCiv
Definition: World.hpp:136
int landmassSeed
Definition: World.hpp:100
int getTileFertility(const int, const int)
Definition: World.cpp:1964
void generateTribes(int, int, int)
Definition: World.cpp:853
std::string worldFilePath
Definition: World.hpp:95
bool hasFreeTerritory(int landmassID)
Definition: World.cpp:2249
void generateWorld(const std::string, const int, const int, int, int, const bool, const bool, const bool, const double)
Definition: World.cpp:1469
Definition: World.hpp:50
long long unsigned int ticksBacklog
Definition: World.hpp:86
Definition: Civ.hpp:28
Definition: Tribe_Dwarven.hpp:24
bool active
Definition: World.hpp:65
void unloadLocal(const int, const int)
Definition: World.cpp:1912
Vector< WorldObjectGlobal * > vWorldObjectGlobal
Definition: World.hpp:110
void evolveToCiv(Tribe *)
Definition: World.cpp:967
Vector< HasXY * > vAllTiles2
Definition: World.hpp:130
bool isRaining
Definition: World.hpp:148
bool prepareAdventureMode(Character *)
Definition: World.cpp:2453
virtual void save()
Definition: World.cpp:2604
Definition: WorldObjectGlobal.hpp:17
Vector< HasXY2< unsigned long int > * > * rayTraceLOS(unsigned long int _x, unsigned long int _y, const int RANGE, const bool, const bool subterranean=false)
Definition: World.cpp:563
int simY
Definition: World.hpp:79
ArrayS2< World_Local > aWorldTile
Definition: World.hpp:113
void generateHumans(int)
Definition: World.cpp:961
Definition: World_Biome.hpp:46
void queryTileLocal(int hoveredXTile, int hoveredYTile)
Definition: World.cpp:2127
int nY
Definition: World.hpp:75
int monthlyCounter
Definition: World.hpp:104
HasXY * getRandomLandTile()
Definition: World.cpp:2090
unsigned long int maximumX
Definition: World.hpp:77
ArrayS3< unsigned char > aTopoMap
Definition: World.hpp:107
void incrementDeities(int)
Definition: World.cpp:1143
unsigned long int maximumY
Definition: World.hpp:77
bool isGenerated(unsigned long int _absoluteX, unsigned long int _absoluteY)
Definition: World.cpp:515
void startSimulation()
Definition: World.cpp:218
std::string strSavePath
Definition: World.hpp:94
bool hasSettlement(const int, const int)
Definition: World.cpp:2436
int simX
Definition: World.hpp:79
int deityTicks
Definition: World.hpp:151
void incrementTicksBacklog(long long unsigned int)
Definition: World.cpp:1016
void generateDwarves(int)
Definition: World.cpp:915
std::string name
Definition: World.hpp:93
void buildArrays(WorldGenerator2 &wg)
Definition: World.cpp:1312
Definition: World_Local.hpp:58
Definition: Tribe_Elf.hpp:23
Vector< Vector< int > *> vAllTiles
Definition: World.hpp:127
Tribe * getDominantInfluence(const int, const int)
Definition: World.cpp:2173
Vector< Tribe * > * getTribesOn(const int, const int)
Definition: World.cpp:1948
World()
Definition: World.cpp:45
int queryWorldY
Definition: World.hpp:82
int getSurroundingFertility(const int _x, const int _y)
Definition: World.cpp:1975
void controlCharacter(Character *)
Definition: World.cpp:2334
void idleTick()
Definition: World.cpp:1237
int queryWorldX
Definition: World.hpp:82
bool putObject(WorldObjectGlobal *_object, int x=-1, int y=-1)
Definition: World.cpp:454
World_Local * getTile(const int, const int)
Definition: World.cpp:2429
Vector< WorldObjectGlobal * > * getNeighboringObjects(WorldObjectGlobal *)
Definition: World.cpp:486
bool addRace(int, std::string)
void degradeInfluence(int amount=1)
Definition: World.cpp:2146
Mythology_Manager mythologyManager
Definition: World.hpp:145
unsigned char seaLevel
Definition: World.hpp:132
int nX
Definition: World.hpp:75
Timer relinquishTimer
Definition: World.hpp:88
void nameRegions()
Definition: World.cpp:450
Definition: Character.hpp:38
void getRandomTile(int *x, int *y)
Definition: World.cpp:2044
bool incrementContinuous
Definition: World.hpp:87
Definition: World_MapManager.hpp:33
EventManager eventManager
Definition: World.hpp:144
int getHighestInfluence(const int, const int)
Definition: World.cpp:2186
Character * getRandomCharacter()
Definition: World.cpp:535
bool handleTickBacklog()
Definition: World.cpp:1195
bool isSafe(int _x, int _y)
Definition: World.cpp:504
void buildMinimap()
Definition: World.cpp:2580
void logicTick()
Definition: World.cpp:1137
Definition: Tribe_Human.hpp:21
Definition: Tribe.hpp:23
World_Astronomy astronomy
Definition: World.hpp:98
Definition: LocalTile.hpp:34
ArrayS2< int > aRiverID
Definition: World.hpp:119
bool generated
Definition: World.hpp:71
Definition: Mythology.hpp:111
Vector< World_Biome * > vBiome
Definition: World.hpp:123
ArrayS2< unsigned char > aGoodEvil
Definition: World.hpp:138
int getHuntingYield(const int _x, const int _y)
Definition: World.cpp:1999
void incrementTicks(int)
Definition: World.cpp:1024
Tribe * combatCheck(Tribe *)
Definition: World.cpp:2411
unsigned char mountainLevel
Definition: World.hpp:133
bool removeObject(WorldObjectGlobal *_object)
Definition: World.cpp:480
std::string getLandmassName(const int, const int)
Definition: World.cpp:2197
int queryWorldYLocal
Definition: World.hpp:84
void destroyInfluence(Tribe *tribe)
Definition: World.cpp:2157
bool isLand(int _x, int _y)
Definition: World.cpp:1296
bool absoluteToRelative(const unsigned long int _absoluteX, const unsigned long int _absoluteY, int *_globalX, int *_globalY, int *_localX, int *_localY)
Definition: World.cpp:175
int getPopulation()
Definition: World.cpp:2050
Definition: World_Events.cpp:150
std::string getTileType(const int _x, const int _y)
Definition: World.cpp:2035
Calendar lastDate
Definition: World.hpp:142
int queryWorldXLocal
Definition: World.hpp:84
void updateMaps()
Definition: World.cpp:1163
ArrayS2< WorldObject * > aWorldObject
Definition: World.hpp:109
Vector< Tribe * > vTribe
Definition: World.hpp:135
void generateElves(int)
Definition: World.cpp:958
Definition: Civ_Dwarven.hpp:14
void rayTrace(unsigned long int _x1, unsigned long int _y1, unsigned long int _x2, unsigned long int _y2, Vector< HasXY2< unsigned long int > *> *vVisibleTiles, bool subterranean=false)
Definition: World.cpp:649
Vector< World_Local * > vWorldLocal
Definition: World.hpp:115
int nFreeTerritory(int landmassID)
Definition: World.cpp:2290
std::string getBiomeName(const int, const int)
Definition: World.cpp:2237
void queryTile(int hoveredXTile, int hoveredYTile)
Definition: World.cpp:2118
ArrayS2< int > aBiomeID
Definition: World.hpp:118
bool loadWorld(std::string)
Definition: World.cpp:2474
void generateLocal(const int, const int)
Definition: World.cpp:1756
World_Local * operator()(const int _x, const int _y)
Definition: World.cpp:91