1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _GALAXY_H
5 #define _GALAXY_H
6 
7 #include "CustomSystem.h"
8 #include "Factions.h"
9 #include "GalaxyCache.h"
10 #include "JsonFwd.h"
11 #include "PerfStats.h"
12 #include "RefCounted.h"
13 #include <cstdio>
14 
15 struct SDL_Surface;
16 class GalaxyGenerator;
17 
18 class Galaxy : public RefCounted {
19 protected:
20 	friend class GalaxyGenerator;
21 	Galaxy(RefCountedPtr<GalaxyGenerator> galaxyGenerator, float radius, float sol_offset_x, float sol_offset_y,
22 		const std::string &factionsDir, const std::string &customSysDir);
23 	void SetGalaxyGenerator(RefCountedPtr<GalaxyGenerator> galaxyGenerator);
24 	virtual void Init();
25 
26 public:
27 	// lightyears
28 	const float GALAXY_RADIUS;
29 	const float SOL_OFFSET_X;
30 	const float SOL_OFFSET_Y;
31 
32 	static RefCountedPtr<Galaxy> LoadFromJson(const Json &jsonObj);
33 	void ToJson(Json &jsonObj);
34 
35 	~Galaxy();
36 
IsInitialized()37 	bool IsInitialized() const { return m_initialized; }
38 	/* 0 - 255 */
39 	virtual Uint8 GetSectorDensity(const int sx, const int sy, const int sz) const = 0;
GetFactions()40 	FactionsDatabase *GetFactions() { return &m_factions; }				   // XXX const correctness
GetCustomSystems()41 	CustomSystemsDatabase *GetCustomSystems() { return &m_customSystems; } // XXX const correctness
42 
GetSector(const SystemPath & path)43 	RefCountedPtr<const Sector> GetSector(const SystemPath &path) { return m_sectorCache.GetCached(path); }
GetMutableSector(const SystemPath & path)44 	RefCountedPtr<Sector> GetMutableSector(const SystemPath &path) { return m_sectorCache.GetCached(path); }
NewSectorSlaveCache()45 	RefCountedPtr<SectorCache::Slave> NewSectorSlaveCache() { return m_sectorCache.NewSlaveCache(); }
46 
GetStarSystem(const SystemPath & path)47 	RefCountedPtr<StarSystem> GetStarSystem(const SystemPath &path) { return m_starSystemCache.GetCached(path); }
NewStarSystemSlaveCache()48 	RefCountedPtr<StarSystemCache::Slave> NewStarSystemSlaveCache() { return m_starSystemCache.NewSlaveCache(); }
49 
50 	void FlushCaches();
51 	void Dump(FILE *file, Sint32 centerX, Sint32 centerY, Sint32 centerZ, Sint32 radius);
52 
53 	RefCountedPtr<GalaxyGenerator> GetGenerator() const;
54 	const std::string &GetGeneratorName() const;
55 	int GetGeneratorVersion() const;
56 
GetStats()57 	Perf::Stats &GetStats() { return m_stats; }
GetStats()58 	const Perf::Stats &GetStats() const { return m_stats; }
59 
60 private:
61 	bool m_initialized;
62 	Perf::Stats m_stats;
63 	RefCountedPtr<GalaxyGenerator> m_galaxyGenerator;
64 	SectorCache m_sectorCache;
65 	StarSystemCache m_starSystemCache;
66 	FactionsDatabase m_factions;
67 	CustomSystemsDatabase m_customSystems;
68 };
69 
70 class DensityMapGalaxy : public Galaxy {
71 private:
72 	friend class GalaxyGenerator;
73 	DensityMapGalaxy(RefCountedPtr<GalaxyGenerator> galaxyGenerator, const std::string &mapfile,
74 		float radius, float sol_offset_x, float sol_offset_y, const std::string &factionsDir, const std::string &customSysDir);
75 
76 public:
77 	virtual Uint8 GetSectorDensity(const int sx, const int sy, const int sz) const;
78 
79 private:
80 	std::unique_ptr<float[]> m_galaxyMap;
81 	Sint32 m_mapWidth, m_mapHeight;
82 };
83 
84 #endif /* _GALAXY_H */
85