WorldSim  inDev
2D tile-based sandbox RPG with procedurally generated fantasy world simulator 🌏
World_Local.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef WORLDSIM_WORLD_LOCAL_HPP
3 #define WORLDSIM_WORLD_LOCAL_HPP
4 
5 /* WorldSim: World_Local.hpp
6 #include "World_Local.hpp" */
7 
34 #include "LocalTile.hpp"
35 
36 #include "Static.hpp"
37 #include "Static_Flora.hpp"
38 
39 #include "Creature_Species.hpp"
40 
41 #include <Interface/IdleTick/IdleTickInterface.hpp>
42 #include <Interface/LogicTick/LogicTickInterface.hpp>
43 #include <System/Time/Calendar.hpp>
44 #include <Math/Random/RandomLehmer.hpp>
45 #include <Container/Bitfield/Bitfield.hpp> // for abstract collision data
46 
47 #include <map>
48 #include <atomic> // thread-safe vars
49 
50 class Creature;
51 class Item;
52 class Tribe;
53 
54 class WorldObject_Tree;
55 class World_Biome;
56 
57 
58 class World_Local: public LogicTickInterface, public IdleTickInterface, public HasTexture
59 {
60 private:
61 
62  RandomLehmer rng;
63  // The generation seed for this local map.
64  // Used to derive heightmaps and spawning
65  unsigned int seed;
66 
67  Vector <WorldObject_Tree*> vTree;
68  // list of all types of flora in the game
69  //Vector <Flora*> vFlora;
70 
71  // Simplified world data for use in fast abstract simulations.
72  // Should generally always be loaded in, but I'd like to add caching support regardless.
73 
74  // It looks like storing all collision, flora and creatures on arrays will not be possible.
75  // However per-biome simulation should be possible, we could even group biomes together into
76  // chunks which we know can fit into memory.
77 
78  // The only other easy alternative is each tile having a map lookup with coordinates and object ID.
79  // However in cluttered maps this will not work well and probably won't be sustainable long term.
80 
81  // Grouping data by biome is probably the most sensible option.
82 
83  // in future we could probably have a generate function which solely deals in abstract data, and lets
84  // the details get filled in later. For example just randomaly put down movement blockers in the
85  // collision bitfield and later on we can actually generate their object data.
86  struct AbstractData
87  {
88  // arrays we need:
89  // collision (0-1)
90  // statics/improvements (0-255)
91  // mobs (NPCs and creatures) (0-255)
92 
93 
94  // simplified collision map for pathing. Tile is either accessible or not.
95  Bitfield bfCollision;
96 
97  //Statics have an id from 1-255 so we can just use char array
98  // Statics can be constructions, trees or other flora.
99  ArrayS2 <unsigned char> aStaticID;
100 
101  // we can just use food value bitfield and update it every 24 hours
102  //Bitfield bfFood;
103 
104  // location of flora. Must be looked up in flora vector.
105  // this can obviously be optimised but I'll do it later.
106  //Bitfield bfFlora;
107 
108  //Bitfield bfStatic;
109  // location of creatures. Must be looked up in creature vector
110  //Bitfield bfMob;
111 
112 
113  //std::map can be used as a sparse array. Push a 2D point with the ID value.
114  };
115 
116  // Data stores all data which is only present in generated maps.
117  // This is done to keep memory footprint low, because million of maps are generated on large worlds.
118  // This basically combines all pointers to these arrays into a single pointer.
119  struct Data
120  {
121  // /* LOCAL MAP ARRAYS */
122  ArrayS2 <LocalTile> aLocalTile; // Array of all tiles on map.
123  ArrayS2 <Static*> aStatic; // Array of static objects
124 
125  // Vector of all tile coordinates.
126  // This is used to do things like loop through every coordinate once in random order
127  // (using shuffle).
128  // This should be a static function.
129  //Vector <HasXY*> vAllTiles;
130 
131  // // Lists of all objects of type on this map.
132  // // Useful for counting or looping through each object of type.
133  // // Vector of all Creatures on this map
134  // Vector <Creature*> vCreature;
135  // //Vector of all Characters on this map
136  // Vector <Character*> vCharacter;
137  // //Vector of all Items on this map
138  // Vector <Item*> vItem;
139  // // // Vector of all non-categorised objects on this map.
140  // Vector <WorldObject*> vObjectGeneric;
141 
142  };
143 
144  // Subterannean data is generated separately from above-ground data
145  struct Data_Subterranean
146  {
147  ArrayS2 <LocalTile> aSubterranean; // Array of underground layer. Used for caves, tunnels, mines.
148  };
149 
150  // Keeps track of influence values for each tribe.
151  std::map<Tribe*,int> mInfluence;
152 
153  public:
154  Data * data;
155  Data_Subterranean * dataSubterranean;
156  AbstractData * abstractData;
157 
159 
160  unsigned long int accessNumber; // Increments each time the map is accessed.
161 
162  // Texture of map from far away. This texture is used to rende the tile when zoomed out far.
163  Texture texFar;
164 
165  Calendar localDate; /* The date that this map has been updated to. */
166 
167  /* Special features */
168  bool hasCave; // should be ncaves
169  bool hasRuin; // should be nruins
170 
171  //WORLD DATA
172  std::atomic <short int> globalX_TS;
173  std::atomic <short int> globalY_TS;
174  short int globalX, globalY; /* The local world's position in the world. */
175  std::atomic <bool> threadAccess;
176  std::atomic <bool> initialized;
177  std::atomic <bool> active;
178 
180 
181  //WORLD DATA
182  //short int globalY; /* The local world's position in the world. */
183 
184  // How many metals may be mined from this tile.
185  short int baseMetal;
186 
187  // texture pointer
188 
189  //enumBiome biome; /* Determines what it looks like and is called */
190  //short int baseMoveCost; /* how many ap to move onto the tile. */
191  //bool canHaveSettlement;
192  short int baseFertility;
193  bool canMove; /* True if units can walk over it. */
194  //char baseLogisticsCost; /* Will consume logistics from armies and navies. Used to prevent armies from travelling through desert, and early navies from travelling through ocean. */
195  //char defensiveBonus;
196 
197  // The id of the landmass.
198  short int landID;
199  // The id of the biome.
200  short int biomeID;
201 
202  short int hasRiver; /* In future will be expanded to have more detailed info about river direction etc */
203  char riverConnections; // NW N NE E SE S SW W. There should be at least 2.
204 
205  // WILDLIFE ABSTRACTION
206  // These vars determine maximum herbivore population, and by extension, maximum carnivore population.
207  unsigned int nFloraGrass; // Grass, low quality food
208  unsigned int nFloraGood; // Shrubs and fruit, high energy food
209  unsigned int nFloraDifficult; // Food in trees, only some creatures can eat it.
210 
211 
212  // the height of the center tile. Used to build the remaining heightmap.
213  // The value is based on the tile type and is randomised on init.
214  unsigned short int centerHeight;
215  ArrayS2 <char> aHeightDiff; //each value is the height difference from the neighboring tile closest to the center.
216 
217 
218  ArrayS2 <int> aFullHeight; // should be short int in future.
219  //Return a string with the name of the terrain.
220  std::string getTerrainName();
221 
222  //Add influence from the particular tribe for this tile.
223  void addInfluence (Tribe* tribe, int amount);
224  //Remove influence from the particular tribe for this tile.
225  void removeInfluence (Tribe* tribe, int amount);
226  //Lower all influence by a certain amount.
227  void degradeInfluence (int amount);
228  //Erase the influence entry of this tribe.
229  void destroyInfluence (Tribe* tribe);
230 
231  // Return the tribe with the greatest influence on the tile.
233  // Return the value of the greatest influence on the tile.
235 
236  // LOCAL MAP DATA
237 
238  // Local RNG
239  //RandomNonStatic random;
240 
241  /* The size of the world, measured in tiles. */
242  short int nX, nY;
243 
244  long long unsigned int ticksBacklog; /* World will simulate these ticks whenever it can, while still relinquishing for input etc. */
245 
246  //The base designated biome for this tile. Will influence generation.
247  enumBiome baseBiome;
248 
249  // This should be moved into LocalTile.
250  //ArrayS2 <bool> aIsLand;
251 
252 
253  /* LOCAL MAP ARRAYS */
254  //ArrayS2 <LocalTile> aLocalTile; // Array of all tiles on map.
255  //ArrayS2 <LocalTile> aSubterranean; // Array of underground layer. Used for caves, tunnels, mines.
256 
257  // Vector of all tile coordinates.
258  // This is used to do things like loop through every coordinate once in random order
259  // (using shuffle).
260  //Vector <HasXY*> vAllTiles;
261 
262  // A vector with a coordinate object for every tile on a local map.
263  // Useful for randomly looping through all tiles in a map.
264  //static Vector <HasXY*> vAllTiles;
265 
266  // Lists of all objects of type on this map.
267  // Useful for counting or looping through each object of type.
268  // Vector of all Creatures on this map
269  Vector <Creature*> vCreature;
270  //Vector of all Characters on this map
271  Vector <Character*> vCharacter;
272  //Vector of all Items on this map
273  Vector <Item*> vItem;
274  // Vector of all non-categorised objects on this map.
275  Vector <WorldObject*> vObjectGeneric;
276 
277  //bool hasRiver; /* For now just a basic bool. In future it will need to define the river sides. */
278 
279 
280  // Initialisation
281  World_Local();
282  virtual ~World_Local();
283  void unload();
284  void init(const int _globalX, const int _globalY, const enumBiome _biomeID, const int _seed /*=0*/, const int _hasRiver /*=-1*/);
285 
286  // Access
287  inline LocalTile* operator() (int _x, int _y);
288 
289  // Returns true if the coordinate is inside this map
290  bool isSafe(int /* x */, int /* y */);
291  bool isSafe(HasXY*);
292  bool isSafe(WorldObject*);
293 
294  bool isLand(); // returns true if this is a land tile.
295 
296  // Generate a world or load from cache.
297  bool generate(bool cache=true, World_Local* c0=0, World_Local* c1=0, World_Local* c2=0,
298  World_Local *c3=0, World_Local* c4=0, World_Local* c5=0, World_Local* c6=0, World_Local* c7=0);
299  bool generateSubterranean(); // subterranean maps should be generated on demand.
300  bool save();
301  std::string getSaveData();
302  bool load();
303 
304  void generateHeightMap(const short int c0=0, const short int c1=0, const short int c2=0, const short int c3=0,
305  const short int c4=0, const short int c5=0, const short int c6=0, const short int c7=0, const unsigned int _seed=0);
306 
307  // SPAWN FUNCTIONS
308  // spawn a random creature from Biome species list.
310 
311  //bool saveToFile(std::string /* path */);
312 
313  // PUT FUNCTION
314  // Overloaded put function automatically sorts object into relevant lists.
315  // It's important to account for all categories otherwise it will implicitly
316  // overload to a generic. And you will never see it again.
317  bool put (WorldObject* , int /* _x */, int /* _y */, bool subterranean=false);
318  bool put (Item* , int /* _x */, int /* _y */, bool subterranean=false);
319  bool put (Character* , int /* _x */, int /* _y */, bool subterranean=false);
320  bool put (Creature* , int /* _x */, int /* _y */, bool subterranean=false);
321  //bool put (WorldObject_Tree* , int /* _x */, int /* _y */, bool subterranean=false);
322 
323  bool put (WorldObject* _object, HasXY* _xy, bool subterranean=false);
324  bool put (Item* _object, HasXY* _xy, bool subterranean=false);
325  bool put (Character* _object, HasXY* _xy, bool subterranean=false);
326  bool put (Creature* _object, HasXY* _xy, bool subterranean=false);
327  //bool put (WorldObject_Tree* _object, HasXY* _xy, bool subterranean=false);
328 
329  bool put (WorldObject* _object, HasXY _xy, bool subterranean=false);
330  bool put (Item* _object, HasXY _xy, bool subterranean=false);
331  bool put (Character* _object, HasXY _xy, bool subterranean=false);
332  bool put (Creature* _object, HasXY _xy, bool subterranean=false);
333  //bool put (WorldObject_Tree* _object, HasXY _xy, bool subterranean=false);
334 
335  bool put (Static* _static, int /* _x */, int /* _y */, bool subterranean = false);
336  bool put (Static* _static, HasXY _xy, bool subterranean = false);
337  bool put (Static* _static, HasXY* _xy, bool subterranean = false);
338 
339  // Remove object from world, but don't delete it.
340  // For example if somebody picks it up.
341  bool remove (WorldObject*);
342  bool remove (Item*);
343  bool remove (Character*);
344  bool remove (Creature*);
345  // Remove object from world and delete it.
346  // For example if it is consumed.
347  bool erase(WorldObject*);
348  bool erase (Item*);
349  bool erase (Character*);
350  bool erase (Creature*);
351  // Return true if the map has this object in it
352  bool contains(WorldObject*);
353  bool contains(Character*);
354 
355  // Uses local map coordinates, but will automatically move object to adjacent maps if necessary.
356  // This shouldn't be used too much because most movement will probably be discrete steps NESW, in order to calculate
357  // wall collision and whatnot. This is more of a teleport function.
358  bool moveObject (WorldObject* , int /* newX */, int /* newY */ );
359  bool moveObject (Character* , int /* newX */, int /* newY */ );
360 
361  // Move object one move in the passed direction (NORTH, EAST, SOUTH, WEST).
362  // Multiple moves can just repeatedly call this function if necessary.
363  bool moveObject(WorldObject*, const enumDirection);
364 
365  // Move object up or down the z layer. Currently there are only 2 z-layers.
366  bool moveDown(WorldObject*);
367  bool moveDown(Character*);
368  bool moveUp(WorldObject*);
369  bool moveUp(Character*);
370 
371  bool wander (WorldObject* /* _object */);
372  bool wander (Character* /* _object */);
373  bool wander (Creature* /* _object */);
374 
375  //bool removeItem (Item* /* _item */);
376 
377  //SEARCH FUNCTIONS
378 
379  Vector <Character*> * getAdjacentCharacters(int /* _x */, int /* _y */);
380 
381  HasXY* getRandomTile();
382  HasXY* getRandomNeighbor(HasXY*);
383  //Vector <HasXY*> ge
384 
385  Vector <HasXY*> * getRandomWalk(int /* nSteps */);
386 
387  // LINE OF SIGHT
388 
389  // Returns true if an object on this tile can block line of sight */
390  bool isBlockingView(int /* _x */, int /* _y */);
391 
392  //Return a vector of coordinates visible from the given location.
393  Vector <HasXY*> * rayTraceLOS (int /* _x */, int /* _y */, const int /* RANGE */, const bool subterranean=false);
394 
395  //Return a vector of coordinates visible from the given location.
396  // New version using global coordinates
397  Vector <HasXY2 <unsigned long int> *> * rayTraceLOS (long unsigned int /* _x */, long unsigned int /* _y */, const int /* RANGE */);
398 
399  // Trace 1 ray, and adds visible coords to vector.
400  void rayTrace (int /* _x */, int /* _y */, int /* _x2 */, int /* _y2 */, Vector <HasXY*> * /* vVisibleTiles */ , const bool subterranean=false);
401 
402  // LOGIC
403  void incrementTicks(int nTicks);
404  void updateTickBacklog(Calendar);
405 
406  // HASTEXTURE
407  // Return map tile texture
408  virtual Texture* currentTexture();
409 };
410 
411 
412 #endif
Texture texFar
Definition: World_Local.hpp:163
unsigned short int centerHeight
Definition: World_Local.hpp:214
Definition: Creature.hpp:34
Creature * spawnCreature()
void rayTrace(int, int, int, int, Vector< HasXY *> *, const bool subterranean=false)
Definition: World_Local.cpp:2467
World_Biome * biome
Definition: World_Local.hpp:158
bool canMove
Definition: World_Local.hpp:193
std::atomic< bool > active
Definition: World_Local.hpp:177
Definition: WorldObject.hpp:18
void removeInfluence(Tribe *tribe, int amount)
Definition: World_Local.cpp:2762
enumDirection
Definition: Driver_Settings.cpp:116
Vector< Creature * > vCreature
Definition: World_Local.hpp:269
Calendar localDate
Definition: World_Local.hpp:165
Vector< Character * > * getAdjacentCharacters(int, int)
Definition: World_Local.cpp:2612
void generateHeightMap(const short int c0=0, const short int c1=0, const short int c2=0, const short int c3=0, const short int c4=0, const short int c5=0, const short int c6=0, const short int c7=0, const unsigned int _seed=0)
Definition: World_Local.cpp:844
short int baseMetal
Definition: World_Local.hpp:185
bool moveObject(WorldObject *, int, int)
Definition: World_Local.cpp:1724
void addInfluence(Tribe *tribe, int amount)
Definition: World_Local.cpp:2740
ArrayS2< int > aFullHeight
Definition: World_Local.hpp:218
Definition: Static.hpp:24
Vector< Item * > vItem
Definition: World_Local.hpp:273
void destroyInfluence(Tribe *tribe)
Definition: World_Local.cpp:2793
bool hasCave
Definition: World_Local.hpp:168
short int biomeID
Definition: World_Local.hpp:200
short int globalX
Definition: World_Local.hpp:174
Definition: World_Biome.hpp:46
void updateTickBacklog(Calendar)
Definition: World_Local.cpp:2729
bool load()
Definition: World_Local.cpp:1441
void incrementTicks(int nTicks)
Definition: World_Local.cpp:2692
unsigned int nFloraDifficult
Definition: World_Local.hpp:209
Definition: Item.hpp:40
AbstractData * abstractData
Definition: World_Local.hpp:156
Definition: World_Local.hpp:58
Definition: WorldObject_Tree.hpp:26
std::string getSaveData()
Definition: World_Local.cpp:1198
long long unsigned int ticksBacklog
Definition: World_Local.hpp:244
bool isLand()
Definition: World_Local.cpp:2827
std::string getTerrainName()
Definition: World_Local.cpp:2822
std::atomic< bool > threadAccess
Definition: World_Local.hpp:175
void unload()
Definition: World_Local.cpp:104
Vector< HasXY * > * getRandomWalk(int)
Definition: World_Local.cpp:2657
bool contains(WorldObject *)
Definition: World_Local.cpp:2323
bool wander(WorldObject *)
Definition: World_Local.cpp:2096
bool generateSubterranean()
Definition: World_Local.cpp:738
bool isSafe(int, int)
Definition: World_Local.cpp:331
bool hasRuin
Definition: World_Local.hpp:169
bool isBlockingView(int, int)
Definition: World_Local.cpp:2679
short int nY
Definition: World_Local.hpp:242
int testValue
Definition: World_Local.hpp:179
short int hasRiver
Definition: World_Local.hpp:202
LocalTile * operator()(int _x, int _y)
Definition: World_Local.cpp:321
HasXY * getRandomNeighbor(HasXY *)
Definition: World_Local.cpp:2634
ArrayS2< char > aHeightDiff
Definition: World_Local.hpp:215
short int globalY
Definition: World_Local.hpp:174
Definition: Character.hpp:29
short int landID
Definition: World_Local.hpp:198
virtual ~World_Local()
Definition: World_Local.cpp:76
short int nX
Definition: World_Local.hpp:242
Tribe * getDominantInfluence()
Definition: World_Local.cpp:2801
bool moveUp(WorldObject *)
Definition: World_Local.cpp:2059
void degradeInfluence(int amount)
Definition: World_Local.cpp:2781
Definition: Tribe.hpp:21
HasXY * getRandomTile()
Definition: World_Local.cpp:2626
Definition: LocalTile.hpp:34
unsigned int nFloraGrass
Definition: World_Local.hpp:207
std::atomic< short int > globalY_TS
Definition: World_Local.hpp:173
unsigned long int accessNumber
Definition: World_Local.hpp:160
Data_Subterranean * dataSubterranean
Definition: World_Local.hpp:155
enumBiome baseBiome
Definition: World_Local.hpp:247
void init(const int _globalX, const int _globalY, const enumBiome _biomeID, const int _seed, const int _hasRiver)
Definition: World_Local.cpp:126
std::atomic< short int > globalX_TS
Definition: World_Local.hpp:172
unsigned int nFloraGood
Definition: World_Local.hpp:208
Vector< Character * > vCharacter
Definition: World_Local.hpp:271
std::atomic< bool > initialized
Definition: World_Local.hpp:176
bool moveDown(WorldObject *)
Definition: World_Local.cpp:2024
Data * data
Definition: World_Local.hpp:154
short int baseFertility
Definition: World_Local.hpp:192
bool generate(bool cache=true, World_Local *c0=0, World_Local *c1=0, World_Local *c2=0, World_Local *c3=0, World_Local *c4=0, World_Local *c5=0, World_Local *c6=0, World_Local *c7=0)
Definition: World_Local.cpp:347
char riverConnections
Definition: World_Local.hpp:203
bool put(WorldObject *, int, int, bool subterranean=false)
Definition: World_Local.cpp:1561
Vector< HasXY * > * rayTraceLOS(int, int, const int, const bool subterranean=false)
Definition: World_Local.cpp:2346
virtual Texture * currentTexture()
Definition: World_Local.cpp:2832
bool erase(WorldObject *)
Definition: World_Local.cpp:2268
World_Local()
Definition: World_Local.cpp:28
int getDominantInfluenceValue()
Definition: World_Local.cpp:2813
bool save()
Definition: World_Local.cpp:1318
Vector< WorldObject * > vObjectGeneric
Definition: World_Local.hpp:275