Pioneer
GalaxyGenerator.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef GALAXYGENERATOR_H
5 #define GALAXYGENERATOR_H
6 
7 #include "RefCounted.h"
8 #include "Sector.h"
9 #include "StarSystem.h"
10 #include "SystemPath.h"
11 #include <list>
12 #include <string>
13 
16 
17 class GalaxyGenerator : public RefCounted {
18 public:
19  typedef int Version;
20  static const Version LAST_VERSION = -1;
21 
22  static void Init(const std::string &name = std::string("legacy"), Version version = LAST_VERSION);
23  static void Uninit();
24 
25  static RefCountedPtr<Galaxy> Create(const std::string &name, Version version = LAST_VERSION);
27  {
28  return Create(s_defaultGenerator, s_defaultVersion);
29  }
30  static RefCountedPtr<Galaxy> CreateFromJson(const Json &jsonObj);
31 
32  static std::string GetDefaultGeneratorName() { return s_defaultGenerator; }
33  static Version GetDefaultGeneratorVersion() { return s_defaultVersion; }
34  static Version GetLastVersion(const std::string &name);
35 
36  virtual ~GalaxyGenerator();
37 
38  const std::string &GetName() const { return m_name; }
39  Version GetVersion() const { return m_version; }
40 
41  bool IsDefault() const { return m_name == s_defaultGenerator && m_version == s_defaultVersion; }
42 
43  void ToJson(Json &jsonObj, RefCountedPtr<Galaxy> galaxy);
44  void FromJson(const Json &jsonObj, RefCountedPtr<Galaxy> galaxy);
45 
46  // Templated for the template cache class.
47  template <typename T, typename Cache>
48  RefCountedPtr<T> Generate(RefCountedPtr<Galaxy> galaxy, const SystemPath &path, Cache *cache);
49 
52 
53  struct SectorConfig {
55 
57  isCustomOnly(false) {}
58  };
59 
62 
64  isCustomOnly(false) {}
65  };
66 
67 private:
68  GalaxyGenerator(const std::string &name, Version version = LAST_VERSION) :
69  m_name(name),
70  m_version(version) {}
71 
72  virtual RefCountedPtr<Sector> GenerateSector(RefCountedPtr<Galaxy> galaxy, const SystemPath &path, SectorCache *cache);
73  virtual RefCountedPtr<StarSystem> GenerateStarSystem(RefCountedPtr<Galaxy> galaxy, const SystemPath &path, StarSystemCache *cache);
74 
75  const std::string m_name;
76  const Version m_version;
77 
78  std::list<SectorGeneratorStage *> m_sectorStage;
79  std::list<StarSystemGeneratorStage *> m_starSystemStage;
80 
81  static RefCountedPtr<Galaxy> s_galaxy;
82  static std::string s_defaultGenerator;
83  static Version s_defaultVersion;
84 };
85 
86 template <>
87 inline RefCountedPtr<Sector> GalaxyGenerator::Generate<Sector, SectorCache>(RefCountedPtr<Galaxy> galaxy, const SystemPath &path, SectorCache *cache)
88 {
89  return GenerateSector(galaxy, path, cache);
90 }
91 
92 template <>
93 inline RefCountedPtr<StarSystem> GalaxyGenerator::Generate<StarSystem, StarSystemCache>(RefCountedPtr<Galaxy> galaxy, const SystemPath &path, StarSystemCache *cache)
94 {
95  return GenerateStarSystem(galaxy, path, cache);
96 }
97 
99 public:
101 
102  virtual void ToJson(Json &jsonObj, RefCountedPtr<Galaxy> galaxy) {}
103  virtual void FromJson(const Json &jsonObj, RefCountedPtr<Galaxy> galaxy) {}
104 
105 protected:
107  m_galaxyGenerator(nullptr) {}
108 
109  friend class GalaxyGenerator;
110  void AssignToGalaxyGenerator(GalaxyGenerator *galaxyGenerator) { m_galaxyGenerator = galaxyGenerator; }
111 
113 };
114 
116 public:
118 
120 };
121 
123 public:
125 
127 };
128 
129 #endif
nlohmann::json Json
Definition: Json.h:8
Definition: GalaxyGenerator.h:98
GalaxyGeneratorStage()
Definition: GalaxyGenerator.h:106
void AssignToGalaxyGenerator(GalaxyGenerator *galaxyGenerator)
Definition: GalaxyGenerator.h:110
virtual ~GalaxyGeneratorStage()
Definition: GalaxyGenerator.h:100
virtual void FromJson(const Json &jsonObj, RefCountedPtr< Galaxy > galaxy)
Definition: GalaxyGenerator.h:103
GalaxyGenerator * m_galaxyGenerator
Definition: GalaxyGenerator.h:112
virtual void ToJson(Json &jsonObj, RefCountedPtr< Galaxy > galaxy)
Definition: GalaxyGenerator.h:102
Definition: GalaxyGenerator.h:17
static Version GetDefaultGeneratorVersion()
Definition: GalaxyGenerator.h:33
static std::string GetDefaultGeneratorName()
Definition: GalaxyGenerator.h:32
static RefCountedPtr< Galaxy > Create()
Definition: GalaxyGenerator.h:26
void FromJson(const Json &jsonObj, RefCountedPtr< Galaxy > galaxy)
Definition: GalaxyGenerator.cpp:132
static Version GetLastVersion(const std::string &name)
Definition: GalaxyGenerator.cpp:36
bool IsDefault() const
Definition: GalaxyGenerator.h:41
GalaxyGenerator * AddSectorStage(SectorGeneratorStage *sectorGenerator)
Definition: GalaxyGenerator.cpp:157
const std::string & GetName() const
Definition: GalaxyGenerator.h:38
int Version
Definition: GalaxyGenerator.h:19
static RefCountedPtr< Galaxy > CreateFromJson(const Json &jsonObj)
Definition: GalaxyGenerator.cpp:86
virtual ~GalaxyGenerator()
Definition: GalaxyGenerator.cpp:149
static const Version LAST_VERSION
Definition: GalaxyGenerator.h:20
Version GetVersion() const
Definition: GalaxyGenerator.h:39
RefCountedPtr< T > Generate(RefCountedPtr< Galaxy > galaxy, const SystemPath &path, Cache *cache)
static void Uninit()
Definition: GalaxyGenerator.cpp:29
static void Init(const std::string &name=std::string("legacy"), Version version=LAST_VERSION)
Definition: GalaxyGenerator.cpp:20
void ToJson(Json &jsonObj, RefCountedPtr< Galaxy > galaxy)
Definition: GalaxyGenerator.cpp:103
GalaxyGenerator * AddStarSystemStage(StarSystemGeneratorStage *starSystemGenerator)
Definition: GalaxyGenerator.cpp:164
Definition: Random.h:27
Definition: RefCounted.h:11
Definition: GalaxyGenerator.h:115
virtual ~SectorGeneratorStage()
Definition: GalaxyGenerator.h:117
virtual bool Apply(Random &rng, RefCountedPtr< Galaxy > galaxy, RefCountedPtr< Sector > sector, GalaxyGenerator::SectorConfig *config)=0
Definition: GalaxyGenerator.h:122
virtual ~StarSystemGeneratorStage()
Definition: GalaxyGenerator.h:124
virtual bool Apply(Random &rng, RefCountedPtr< Galaxy > galaxy, RefCountedPtr< StarSystem::GeneratorAPI > system, GalaxyGenerator::StarSystemConfig *config)=0
Definition: SystemPath.h:13
Definition: GalaxyGenerator.h:53
bool isCustomOnly
Definition: GalaxyGenerator.h:54
SectorConfig()
Definition: GalaxyGenerator.h:56
Definition: GalaxyGenerator.h:60
StarSystemConfig()
Definition: GalaxyGenerator.h:63
bool isCustomOnly
Definition: GalaxyGenerator.h:61