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 "World_Astronomy.hpp"
17 #include "LocalTile.hpp"
18 #include "World_Local.hpp"
19 #include "World_Landmass.hpp"
20 #include "World_Biome.hpp"
21 #include "World_MapManager.hpp"
22 
23 #include <Graphics/Png/Png.hpp>
24 #include <File/FileManager.hpp>
25 #include <Container/ArrayS2/ArrayS2.hpp>
26 #include <Interface/IdleTick/IdleTickInterface.hpp>
27 #include <Interface/LogicTick/LogicTickInterface.hpp>
28 #include <System/Time/Timer.hpp>
29 #include <System/Time/Calendar.hpp>
30 #include <File/SaveFileManager.hpp>
31 #include <Math/Random/GlobalRandom.hpp>
32 #include <Game/NameGen/NameGen.hpp>
33 
34 #include <atomic>
35 #include <map>
36 
37 class Civ;
38 class Civ_Dwarven;
39 class WorldObjectGlobal;
40 class Character;
41 class Tribe;
42 class Tribe_Human;
43 class Tribe_Dwarven;
44 class Tribe_Elf;
45 
46 class World: public LogicTickInterface, public IdleTickInterface, public SaveFileInterface
47 {
48  private:
49  RandomNonStatic random;
50  World_MapManager mapManager;
51  // We could remove this later by using a pointer.
52  ArrayS2 <enumBiome> aTerrain;
53 
54  public:
55 
56  bool active; /* Whether or not the world should be simulated. */
57 
58  #if defined WILDCAT_THREADING
59  std::atomic <bool> generated;
60  #else
61  bool generated; /* False until a world has been generated. Prevents trying to simulate a non-existent world. */
62  #endif
63 
64  // The size of the world, measured in tiles.
65  int nX, nY;
66  // The maximum global coordinate
67  unsigned long int maximumX, maximumY;
68 
69  int simX, simY;
70 
71  /* The current tile that the player wants information on */
73  // same but for local map tile (if it exists)
75 
76  long long unsigned int ticksBacklog; /* World will simulate these ticks whenever it can, while still relinquishing for input etc. */
79 
80  // This array stores the base terrain data.
81  //ArrayS2 <enumBiome> aTerrain;
82 
83  std::string name; /* The name of the world */
84  std::string strSavePath; /* The path to the world's save data. Typically something like: "savedata/<world name>". */
85  std::string worldFilePath;
86 
87  // Stores astronomical details from perspective of World.
89 
91 
92  /* Counters to regulate logic */
95 
96  /* This array stores the colour map for rendering. */
97  ArrayS3 <unsigned char> aTopoMap;
98 
99  ArrayS2 <WorldObject*> aWorldObject;
100  Vector <WorldObjectGlobal*> vWorldObjectGlobal;
101 
102  // Stores all tile-specific information
103  ArrayS2 <World_Local> aWorldTile;
104  // A vector of all local maps in memory. These worlds can be rendered and simulated.
105  Vector <World_Local*> vWorldLocal;
106 
107  // All of these should be ported into aWorldTile.
108  ArrayS2 <int> aBiomeID;
109  ArrayS2 <int> aRiverID; /* Contains the river ID */
110 
111  //Vector <std::string> vLandmassName;
112  Vector <World_Landmass*> vLandmass;
113  Vector <World_Biome*> vBiome;
114 
115  // A vector of vectors used to get a random tile. Vector index represents the row. Vector value represents column. Shuffle row after use.
116  // NOTE: UNFORTUNATELY THIS DOES NOT PRODUCE A RANDOM DISTRIBUTION.
117  Vector < Vector <int> * > vAllTiles;
118  // New approach, just a single vector. Unfortunately will take longer to shuffle.
119  // Can be optimised by shifting begin and end pointers.
120  Vector <HasXY*> vAllTiles2;
121 
122  unsigned char seaLevel;
123  unsigned char mountainLevel;
124 
125  Vector <Tribe*> vTribe;
126  Vector <Civ*> vCiv;
127 
128  ArrayS2 <unsigned char> aGoodEvil;
129 
130 
131  Calendar calendar;
132  Calendar lastDate; /* The date on the last logic tick. */
133 
134  // STATES
135  bool isRaining;
136 
137  World();
138  ~World();
139 
140  // CHECK ALL CIV LOS ARRAYS, AND IF ANY OVERLAP, THEN MAKE THEM DISCOVER EACH OTHER.
141  //void updateCivContacts();
142 
143  // Return a World_Local object for this local map. Will generate a local map if necessary. Returns null pointer for invalid request.
144  inline World_Local* operator() (const int _x, const int _y);
145 
146  //New operator: Gets tiles using global coordinates.
147  // IT DOES NOT RETURN WORLD_LOCAL LIKE THE OTHER FUNCTION
148  // Yeah this is a confusing overload which needs to be fixed in the future.
149  // This will generate a local map if necessary.
150  inline LocalTile* operator() (unsigned long int _x, unsigned long int _y, const bool subterranean=false);
151 
152  inline LocalTile* operator() (HasXY2 <unsigned long int>*, const bool subterranean=false);
153 
154  // COORDINATE CONVERSIONS
155 
156  // Pass absolute coordinates and recieve world and local relative coordinates.
157  bool absoluteToRelative (const unsigned long int _absoluteX, const unsigned long int _absoluteY, int * _globalX, int * _globalY, int * _localX, int * _localY);
158 
159  // Returns true if the tile is in a map that is loaded.
160  bool isGenerated(unsigned long int _absoluteX, unsigned long int _absoluteY);
161 
162  // Transition from generation to simulation.
163  void startSimulation();
164 
165  /* TICK LOGIC */
166  /* 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. */
167  void incrementTicks ( int /* nTicks */ );
168  void incrementTicksBacklog ( long long unsigned int /* nTicks */ );
169  bool handleTickBacklog ();
170  void idleTick();
171  void logicTick();
172 
173  /* UPDATES */
174  /* World updates not relying on ticks. For example loading in maps that the player is near. */
175  void updateMaps();
176 
177  /* FILE IO */
178  bool loadWorld(std::string /* world name */);
179 
180  /* WORLD GENERATION */
181 
182  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 */);
183  // Generate a local map so it can be viewed and interacted with.
184  void generateLocal(const int /* worldX */, const int /* worldY */);
185  void generateLocal(HasXY*);
186  //Unload a local map from RAM and into a save file.
187  void unloadLocal(const int /* worldX */, const int /* worldY */);
188 
189  /* ADVENTURE MODE */
190  // Load up the required local map, do any required logic, center map on character. Return false if error.
192 
193  void buildArrays(WorldGenerator2& wg);
194  // Find all unique areas and give them names.
195  void nameRegions();
196  void generateTribes( int/* nTribes */, int /* nTribesDwarven */, int /* nTribesElven */);
197  //Put down some Dwarven civs.
198  bool addRace (int /* nTribes */, std::string /* name */);
199  //bool addElvenTribe(int nTribes);
200  //bool addHumanTribe(int nTribes);
201 
202  // Ensure conditions are set up for this character to be controlled.
203  // For example generate the local map.
204  void controlCharacter (Character*);
205 
207 
208  // Return vector of coordinates visible from given coordinate.
209  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);
210  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);
211 
212  /* WORLD QUERIES */
213  /* Returns population of all characters (Human, Dwarven, Elven)
214  alive in the world. */
215  int getPopulation();
216 
217  /* WORLD TILE / COORDINATE QUERIES */
218 
219  // RETURNS TRUE IF THE TILE IS LAND. RETURNS FALSE IF IT IS NOT LAND, OR IF IT IS OUT OF BOUNDS.
220  bool isLand(int _x, int _y);
221  inline bool isLand(HasXY*);
222 
223  // RETURNS TRUE IF THE COORDINATES ARE WITHIN BOUNDS OF THE MAP.
224  // We need to integrate HasXY with template.
225  bool isSafe (int _x, int _y);
226  // SAME BUT FOR ABSOLUTE COORDINATES.
227  bool isSafe (unsigned long int _x, unsigned long int _y);
228 
229  /* Return a pointer to the WorldTile at the coordinates.
230  Return null pointer if fail */
231  World_Local * getTile (const int /* x */, const int /* y */ );
232  /* Return the fertility value of the tile. */
233  int getTileFertility(const int /* x */, const int /* y */);
234  int getTileFertility(const HasXY* /* xy */);
235  /* Returns the total fertility of the passed tile, and its 8 neighbors. */
236  int getSurroundingFertility (const int _x, const int _y);
237 
238  /* Return average hunting yield of tile. Hunting is all non-agricultural food.
239  Hunting yield is the average of the surrounding tiles, plus the tile the tribe is standing on.
240  This is important because coastal tiles provide high yield. */
241  int getHuntingYield( const int _x, const int _y);
242 
243  /* Set the pointers to a coordinate of a random tile in the world. */
244  void getRandomTile (int* x, int* y);
245  /* 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). */
246  bool getRandomLandTile(int* x, int* y);
247 
248  HasXY* getRandomLandTile();
249 
250  HasXY* getRandomTileOfType(enumBiome _type);
251 
252  // RETURN ANY OBJECTS WHICH ARE NEXT TO THE PASSED OBJECT.
253  Vector <WorldObjectGlobal*>* getNeighboringObjects(WorldObjectGlobal*);
254 
255  // Put a worldobject anywhere in the world. If x,y are not provided
256  // It will use the object's own coordinates.
257  bool putObject(WorldObjectGlobal* _object, int x = -1, int y = -1);
258  // Remove object from world.
259  bool removeObject(WorldObjectGlobal* _object);
260 
261  //Return the name of the landmass the tile is on. Return empty string if not on a landmass.
262  std::string getLandmassName(const int /* x */, const int /* y */);
263  std::string getLandmassName (HasXY*);
264 
265  World_Biome* getBiome(const int id);
266  std::string getBiomeName( const int /* x */, const int /* y */);
267 
268  //INFO GETTING FUNCTIONS
269  std::string getTileType (const int _x, const int _y);
270  // provide info on the info panel for this tile
271  void queryTile( int hoveredXTile, int hoveredYTile);
272  // provide info on the info panel for this local tile
273  void queryTileLocal( int hoveredXTile, int hoveredYTile);
274 
275  // TRIBE FUNCTIONS
276  void addInfluence(Tribe* tribe, int amount);
277  // Subtract an influence point from every tile this tribe has.
278  void degradeInfluence(int amount=1);
279  //Set influence to 0.
280  void destroyInfluence(Tribe* tribe);
281  /* Return pointer to tribe with most influence here. Return null pointer if no tribe */
282  Tribe* getDominantInfluence (const int, const int);
283  Tribe* getDominantInfluence (HasXY*);
284 
285  Vector <Tribe*>* getTribesOn(const int /* x */, const int /* y */);
286 
287  // Check if tribe is in engagement range of another tribe.
288  // If yes, return pointer to Tribe, otherwise return 0.
289  Tribe* combatCheck (Tribe* );
290 
291  //Returns true if this landmass has an unclaimed tile.
292  bool hasFreeTerritory(int landmassID);
293  // Return number of unclaimed tiles on this landmass.
294  int nFreeTerritory (int landmassID);
295 
296  void evolveToCiv( Tribe * );
297 
298  // Return the nearest tribe to the passed tribe which is on the same landmass.
299  // Return 0 if none. Only check same race by default.
300  Tribe * getNearestConnectedTribe (Tribe *, bool sameRace = true);
301 
302  int getHighestInfluence(const int, const int);
303  int getHighestInfluence(HasXY*);
304 
306  void buildMinimap();
307 
308 //SAVEFILEINTERFACE
309  virtual void save();
310 
311 };
312 
313 #endif
~World()
Definition: World.cpp:77
Tribe * getNearestConnectedTribe(Tribe *, bool sameRace=true)
Definition: World.cpp:2234
HasXY * getRandomTileOfType(enumBiome _type)
Definition: World.cpp:1963
World astronomical detail container This class stores astronomical data from the perspective of the W...
Definition: World_Astronomy.hpp:36
Vector< World_Landmass * > vLandmass
Definition: World.hpp:112
World_Biome * getBiome(const int id)
Definition: World.cpp:2071
void addInfluence(Tribe *tribe, int amount)
Definition: World.cpp:1996
int dailyCounter
Definition: World.hpp:93
Vector< Civ * > vCiv
Definition: World.hpp:126
int landmassSeed
Definition: World.hpp:90
int getTileFertility(const int, const int)
Definition: World.cpp:1823
void generateTribes(int, int, int)
Definition: World.cpp:819
std::string worldFilePath
Definition: World.hpp:85
bool hasFreeTerritory(int landmassID)
Definition: World.cpp:2097
void generateWorld(const std::string, const int, const int, int, int, const bool, const bool, const bool, const double)
Definition: World.cpp:1355
Definition: World.hpp:46
long long unsigned int ticksBacklog
Definition: World.hpp:76
Definition: Civ.hpp:26
Definition: Tribe_Dwarven.hpp:22
bool active
Definition: World.hpp:56
void unloadLocal(const int, const int)
Definition: World.cpp:1771
Vector< WorldObjectGlobal * > vWorldObjectGlobal
Definition: World.hpp:100
void evolveToCiv(Tribe *)
Definition: World.cpp:896
Vector< HasXY * > vAllTiles2
Definition: World.hpp:120
bool isRaining
Definition: World.hpp:135
bool prepareAdventureMode(Character *)
Definition: World.cpp:2285
virtual void save()
Definition: World.cpp:2436
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:529
int simY
Definition: World.hpp:69
ArrayS2< World_Local > aWorldTile
Definition: World.hpp:103
Definition: World_Biome.hpp:46
void queryTileLocal(int hoveredXTile, int hoveredYTile)
Definition: World.cpp:1986
int nY
Definition: World.hpp:65
int monthlyCounter
Definition: World.hpp:94
HasXY * getRandomLandTile()
Definition: World.cpp:1949
unsigned long int maximumX
Definition: World.hpp:67
ArrayS3< unsigned char > aTopoMap
Definition: World.hpp:97
unsigned long int maximumY
Definition: World.hpp:67
bool isGenerated(unsigned long int _absoluteX, unsigned long int _absoluteY)
Definition: World.cpp:481
void startSimulation()
Definition: World.cpp:190
std::string strSavePath
Definition: World.hpp:84
int simX
Definition: World.hpp:69
void incrementTicksBacklog(long long unsigned int)
Definition: World.cpp:928
std::string name
Definition: World.hpp:83
void buildArrays(WorldGenerator2 &wg)
Definition: World.cpp:1198
Definition: World_Local.hpp:58
Definition: Tribe_Elf.hpp:22
Vector< Vector< int > *> vAllTiles
Definition: World.hpp:117
Tribe * getDominantInfluence(const int, const int)
Definition: World.cpp:2032
Vector< Tribe * > * getTribesOn(const int, const int)
Definition: World.cpp:1807
World()
Definition: World.cpp:42
int queryWorldY
Definition: World.hpp:72
int getSurroundingFertility(const int _x, const int _y)
Definition: World.cpp:1834
void controlCharacter(Character *)
Definition: World.cpp:2182
void idleTick()
Definition: World.cpp:1123
int queryWorldX
Definition: World.hpp:72
bool putObject(WorldObjectGlobal *_object, int x=-1, int y=-1)
Definition: World.cpp:420
World_Local * getTile(const int, const int)
Definition: World.cpp:2277
Vector< WorldObjectGlobal * > * getNeighboringObjects(WorldObjectGlobal *)
Definition: World.cpp:452
bool addRace(int, std::string)
void degradeInfluence(int amount=1)
Definition: World.cpp:2005
unsigned char seaLevel
Definition: World.hpp:122
int nX
Definition: World.hpp:65
Timer relinquishTimer
Definition: World.hpp:78
void nameRegions()
Definition: World.cpp:416
Definition: Character.hpp:29
void getRandomTile(int *x, int *y)
Definition: World.cpp:1903
bool incrementContinuous
Definition: World.hpp:77
Definition: World_MapManager.hpp:33
int getHighestInfluence(const int, const int)
Definition: World.cpp:2045
Character * getRandomCharacter()
Definition: World.cpp:501
bool handleTickBacklog()
Definition: World.cpp:1082
bool isSafe(int _x, int _y)
Definition: World.cpp:470
void buildMinimap()
Definition: World.cpp:2412
void logicTick()
Definition: World.cpp:1044
Definition: Tribe_Human.hpp:21
Definition: Tribe.hpp:21
World_Astronomy astronomy
Definition: World.hpp:88
Definition: LocalTile.hpp:34
ArrayS2< int > aRiverID
Definition: World.hpp:109
bool generated
Definition: World.hpp:61
Vector< World_Biome * > vBiome
Definition: World.hpp:113
ArrayS2< unsigned char > aGoodEvil
Definition: World.hpp:128
int getHuntingYield(const int _x, const int _y)
Definition: World.cpp:1858
void incrementTicks(int)
Definition: World.cpp:934
Tribe * combatCheck(Tribe *)
Definition: World.cpp:2259
unsigned char mountainLevel
Definition: World.hpp:123
bool removeObject(WorldObjectGlobal *_object)
Definition: World.cpp:446
std::string getLandmassName(const int, const int)
Definition: World.cpp:2056
int queryWorldYLocal
Definition: World.hpp:74
void destroyInfluence(Tribe *tribe)
Definition: World.cpp:2016
bool isLand(int _x, int _y)
Definition: World.cpp:1182
bool absoluteToRelative(const unsigned long int _absoluteX, const unsigned long int _absoluteY, int *_globalX, int *_globalY, int *_localX, int *_localY)
Definition: World.cpp:160
int getPopulation()
Definition: World.cpp:1909
std::string getTileType(const int _x, const int _y)
Definition: World.cpp:1894
Calendar lastDate
Definition: World.hpp:132
int queryWorldXLocal
Definition: World.hpp:74
void updateMaps()
Definition: World.cpp:1050
ArrayS2< WorldObject * > aWorldObject
Definition: World.hpp:99
Vector< Tribe * > vTribe
Definition: World.hpp:125
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:615
Vector< World_Local * > vWorldLocal
Definition: World.hpp:105
int nFreeTerritory(int landmassID)
Definition: World.cpp:2138
std::string getBiomeName(const int, const int)
Definition: World.cpp:2084
void queryTile(int hoveredXTile, int hoveredYTile)
Definition: World.cpp:1977
ArrayS2< int > aBiomeID
Definition: World.hpp:108
Calendar calendar
Definition: World.hpp:131
bool loadWorld(std::string)
Definition: World.cpp:2306
void generateLocal(const int, const int)
Definition: World.cpp:1636
World_Local * operator()(const int _x, const int _y)
Definition: World.cpp:82