Pioneer
StarSystem.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 _STARSYSTEM_H
5 #define _STARSYSTEM_H
6 
7 #include "Polit.h"
8 #include "RefCounted.h"
9 #include "galaxy/Economy.h"
10 #include "galaxy/GalaxyCache.h"
11 #include "galaxy/SystemBody.h"
12 #include "galaxy/SystemPath.h"
13 #include "gameconsts.h"
14 
15 #include <SDL_stdinc.h>
16 #include <string>
17 #include <vector>
18 
19 class Faction;
20 class Galaxy;
21 class CustomSystemBody;
22 class CustomSystem;
23 
24 // doubles - all masses in Kg, all lengths in meters
25 // fixed - any mad scheme
26 
27 class StarSystem : public RefCounted {
28 public:
29  friend class SystemBody;
30  friend class GalaxyObjectCache<StarSystem, SystemPath::LessSystemOnly>;
31  class GeneratorAPI; // Complete definition below
32 
37  };
38 
39  void ExportToLua(const char *filename);
40 
41  const std::string &GetName() const { return m_name; }
42  std::vector<std::string> GetOtherNames() const { return m_other_names; }
43  SystemPath GetPathOf(const SystemBody *sbody) const;
44  SystemBody *GetBodyByPath(const SystemPath &path) const;
45  static void ToJson(Json &jsonObj, StarSystem *);
46  static RefCountedPtr<StarSystem> FromJson(RefCountedPtr<Galaxy> galaxy, const Json &jsonObj);
47  const SystemPath &GetPath() const { return m_path; }
48  const std::string &GetShortDescription() const { return m_shortDesc; }
49  const std::string &GetLongDescription() const { return m_longDesc; }
50  unsigned GetNumStars() const { return m_numStars; }
51  const SysPolit &GetSysPolit() const { return m_polit; }
52 
53  static const Color starColors[];
54  static const Color starRealColors[];
55  static const double starLuminosities[];
56  static const float starScale[];
57 
58  RefCountedPtr<const SystemBody> GetRootBody() const { return m_rootBody; }
59  RefCountedPtr<SystemBody> GetRootBody() { return m_rootBody; }
60  bool HasSpaceStations() const { return !m_spaceStations.empty(); }
61  Uint32 GetNumSpaceStations() const { return static_cast<Uint32>(m_spaceStations.size()); }
66  Uint32 GetNumBodies() const { return static_cast<Uint32>(m_bodies.size()); }
69 
71  {
72  return m_commodityLegal[t];
73  }
74 
76  {
77  return m_tradeLevel[t];
78  }
79 
80  const Faction *GetFaction() const { return m_faction; }
81  bool GetUnexplored() const { return m_explored == eUNEXPLORED; }
82  ExplorationState GetExplored() const { return m_explored; }
83  double GetExploredTime() const { return m_exploredTime; }
84  void ExploreSystem(double time);
85 
86  fixed GetMetallicity() const { return m_metallicity; }
87  fixed GetIndustrial() const { return m_industrial; }
88  fixed GetAgricultural() const { return m_agricultural; }
89  GalacticEconomy::EconomyId GetEconType() const { return m_econType; }
90  int GetSeed() const { return m_seed; }
91  fixed GetHumanProx() const { return m_humanProx; }
92  fixed GetTotalPop() const { return m_totalPop; }
93 
94  void Dump(FILE *file, const char *indent = "", bool suppressSectorData = false) const;
95 
97 
98 protected:
99  StarSystem(const SystemPath &path, RefCountedPtr<Galaxy> galaxy, StarSystemCache *cache, Random &rand);
100  virtual ~StarSystem();
101 
103  {
104  SystemBody *body = new SystemBody(SystemPath(m_path.sectorX, m_path.sectorY, m_path.sectorZ, m_path.systemIndex, static_cast<Uint32>(m_bodies.size())), this);
105  m_bodies.push_back(RefCountedPtr<SystemBody>(body));
106  return body;
107  }
108 
109  void MakeShortDescription();
110  void SetShortDesc(const std::string &desc) { m_shortDesc = desc; }
111 
112 private:
113  void SetCache(StarSystemCache *cache)
114  {
115  assert(!m_cache);
116  m_cache = cache;
117  }
118 
119  std::string ExportBodyToLua(FILE *f, SystemBody *body);
120  std::string GetStarTypes(SystemBody *body);
121 
122  SystemPath m_path;
123  unsigned m_numStars;
124  std::string m_name;
125  std::vector<std::string> m_other_names;
126  std::string m_shortDesc, m_longDesc;
127  SysPolit m_polit;
128 
129  bool m_isCustom;
130  bool m_hasCustomBodies;
131 
132  const Faction *m_faction;
133  ExplorationState m_explored;
134  double m_exploredTime;
135  fixed m_metallicity;
136  fixed m_industrial;
137  GalacticEconomy::EconomyId m_econType;
138  Uint32 m_seed;
139 
140  // percent price alteration
141  std::vector<int> m_tradeLevel;
142 
143  fixed m_agricultural;
144  fixed m_humanProx;
145  fixed m_totalPop;
146 
147  RefCountedPtr<SystemBody> m_rootBody;
148  // index into this will be the SystemBody ID used by SystemPath
149  std::vector<RefCountedPtr<SystemBody>> m_bodies;
150  std::vector<SystemBody *> m_spaceStations;
151  std::vector<SystemBody *> m_stars;
152  std::vector<bool> m_commodityLegal;
153 
154  StarSystemCache *m_cache;
155 };
156 
158 private:
159  friend class GalaxyGenerator;
160  GeneratorAPI(const SystemPath &path, RefCountedPtr<Galaxy> galaxy, StarSystemCache *cache, Random &rand);
161 
162 public:
163  bool HasCustomBodies() const { return m_hasCustomBodies; }
164 
165  void SetCustom(bool isCustom, bool hasCustomBodies)
166  {
167  m_isCustom = isCustom;
168  m_hasCustomBodies = hasCustomBodies;
169  }
170  void SetNumStars(int numStars) { m_numStars = numStars; }
171  void SetRootBody(RefCountedPtr<SystemBody> rootBody) { m_rootBody = rootBody; }
172  void SetRootBody(SystemBody *rootBody) { m_rootBody.Reset(rootBody); }
173  void SetName(const std::string &name) { m_name = name; }
174  void SetOtherNames(const std::vector<std::string> &other_names) { m_other_names = other_names; }
175  void SetLongDesc(const std::string &desc) { m_longDesc = desc; }
176  void SetExplored(ExplorationState explored, double time)
177  {
178  m_explored = explored;
179  m_exploredTime = time;
180  }
181  void SetSeed(Uint32 seed) { m_seed = seed; }
182  void SetFaction(const Faction *faction) { m_faction = faction; }
183  void SetEconType(GalacticEconomy::EconomyId econType) { m_econType = econType; }
184  void SetSysPolit(SysPolit polit) { m_polit = polit; }
185  void SetMetallicity(fixed metallicity) { m_metallicity = metallicity; }
186  void SetIndustrial(fixed industrial) { m_industrial = industrial; }
187  void SetAgricultural(fixed agricultural) { m_agricultural = agricultural; }
188  void SetHumanProx(fixed humanProx) { m_humanProx = humanProx; }
189  void SetTotalPop(fixed pop) { m_totalPop = pop; }
190  void AddTotalPop(fixed pop) { m_totalPop += pop; }
191 
192  int GetTradeLevel(GalacticEconomy::CommodityId type) { return m_tradeLevel[type]; }
193  void SetTradeLevel(GalacticEconomy::CommodityId type, int level) { m_tradeLevel[type] = level; }
194  void AddTradeLevel(GalacticEconomy::CommodityId type, int level) { m_tradeLevel[type] += level; }
195  void SetCommodityLegal(GalacticEconomy::CommodityId type, bool legal) { m_commodityLegal[type] = legal; }
196 
198  {
199  assert(station->GetSuperType() == SystemBody::SUPERTYPE_STARPORT);
200  m_spaceStations.push_back(station);
201  }
202  void AddStar(SystemBody *star)
203  {
204  assert(star->GetSuperType() == SystemBody::SUPERTYPE_STAR);
205  m_stars.push_back(star);
206  }
208  using StarSystem::NewBody;
210 };
211 
212 #endif /* _STARSYSTEM_H */
IterationProxy< Container > MakeIterationProxy(Container &container)
Definition: IterationProxy.h:51
nlohmann::json Json
Definition: Json.h:8
Definition: CustomSystem.h:17
Definition: CustomSystem.h:71
Definition: Factions.h:21
Definition: GalaxyGenerator.h:17
Definition: GalaxyCache.h:20
Definition: Galaxy.h:18
Definition: IterationProxy.h:13
Definition: Random.h:27
Definition: RefCounted.h:11
Definition: StarSystem.h:157
void SetExplored(ExplorationState explored, double time)
Definition: StarSystem.h:176
void SetOtherNames(const std::vector< std::string > &other_names)
Definition: StarSystem.h:174
void SetTradeLevel(GalacticEconomy::CommodityId type, int level)
Definition: StarSystem.h:193
void SetFaction(const Faction *faction)
Definition: StarSystem.h:182
void AddStar(SystemBody *star)
Definition: StarSystem.h:202
void SetTotalPop(fixed pop)
Definition: StarSystem.h:189
void SetCommodityLegal(GalacticEconomy::CommodityId type, bool legal)
Definition: StarSystem.h:195
void SetSeed(Uint32 seed)
Definition: StarSystem.h:181
void AddTotalPop(fixed pop)
Definition: StarSystem.h:190
void SetNumStars(int numStars)
Definition: StarSystem.h:170
void SetEconType(GalacticEconomy::EconomyId econType)
Definition: StarSystem.h:183
bool HasCustomBodies() const
Definition: StarSystem.h:163
void AddTradeLevel(GalacticEconomy::CommodityId type, int level)
Definition: StarSystem.h:194
void SetCustom(bool isCustom, bool hasCustomBodies)
Definition: StarSystem.h:165
int GetTradeLevel(GalacticEconomy::CommodityId type)
Definition: StarSystem.h:192
void SetHumanProx(fixed humanProx)
Definition: StarSystem.h:188
void SetRootBody(SystemBody *rootBody)
Definition: StarSystem.h:172
void SetIndustrial(fixed industrial)
Definition: StarSystem.h:186
void SetAgricultural(fixed agricultural)
Definition: StarSystem.h:187
void SetRootBody(RefCountedPtr< SystemBody > rootBody)
Definition: StarSystem.h:171
void SetMetallicity(fixed metallicity)
Definition: StarSystem.h:185
void AddSpaceStation(SystemBody *station)
Definition: StarSystem.h:197
void SetName(const std::string &name)
Definition: StarSystem.h:173
void SetSysPolit(SysPolit polit)
Definition: StarSystem.h:184
void SetLongDesc(const std::string &desc)
Definition: StarSystem.h:175
Definition: StarSystem.h:27
const std::string & GetName() const
Definition: StarSystem.h:41
const std::string & GetLongDescription() const
Definition: StarSystem.h:49
IterationProxy< std::vector< SystemBody * > > GetSpaceStations()
Definition: StarSystem.h:62
int GetCommodityBasePriceModPercent(GalacticEconomy::CommodityId t)
Definition: StarSystem.h:75
const Faction * GetFaction() const
Definition: StarSystem.h:80
const RefCountedPtr< Galaxy > m_galaxy
Definition: StarSystem.h:96
Uint32 GetNumSpaceStations() const
Definition: StarSystem.h:61
bool IsCommodityLegal(const GalacticEconomy::CommodityId t)
Definition: StarSystem.h:70
IterationProxy< std::vector< SystemBody * > > GetStars()
Definition: StarSystem.h:64
fixed GetHumanProx() const
Definition: StarSystem.h:91
friend class SystemBody
Definition: StarSystem.h:29
static void ToJson(Json &jsonObj, StarSystem *)
Definition: StarSystem.cpp:347
RefCountedPtr< const SystemBody > GetRootBody() const
Definition: StarSystem.h:58
std::vector< std::string > GetOtherNames() const
Definition: StarSystem.h:42
GalacticEconomy::EconomyId GetEconType() const
Definition: StarSystem.h:89
const SystemPath & GetPath() const
Definition: StarSystem.h:47
const std::string & GetShortDescription() const
Definition: StarSystem.h:48
ExplorationState GetExplored() const
Definition: StarSystem.h:82
static const double starLuminosities[]
Definition: StarSystem.h:55
int GetSeed() const
Definition: StarSystem.h:90
virtual ~StarSystem()
Definition: StarSystem.cpp:337
bool HasSpaceStations() const
Definition: StarSystem.h:60
const IterationProxy< const std::vector< SystemBody * > > GetSpaceStations() const
Definition: StarSystem.h:63
SystemBody * NewBody()
Definition: StarSystem.h:102
const IterationProxy< const std::vector< SystemBody * > > GetStars() const
Definition: StarSystem.h:65
fixed GetAgricultural() const
Definition: StarSystem.h:88
SystemBody * GetBodyByPath(const SystemPath &path) const
Definition: StarSystem.cpp:199
fixed GetMetallicity() const
Definition: StarSystem.h:86
void ExploreSystem(double time)
Definition: StarSystem.cpp:324
IterationProxy< std::vector< RefCountedPtr< SystemBody > > > GetBodies()
Definition: StarSystem.h:67
double GetExploredTime() const
Definition: StarSystem.h:83
void Dump(FILE *file, const char *indent="", bool suppressSectorData=false) const
Definition: StarSystem.cpp:521
static const float starScale[]
Definition: StarSystem.h:56
fixed GetTotalPop() const
Definition: StarSystem.h:92
void SetShortDesc(const std::string &desc)
Definition: StarSystem.h:110
const SysPolit & GetSysPolit() const
Definition: StarSystem.h:51
bool GetUnexplored() const
Definition: StarSystem.h:81
SystemPath GetPathOf(const SystemBody *sbody) const
Definition: StarSystem.cpp:209
StarSystem(const SystemPath &path, RefCountedPtr< Galaxy > galaxy, StarSystemCache *cache, Random &rand)
Definition: StarSystem.cpp:220
unsigned GetNumStars() const
Definition: StarSystem.h:50
const IterationProxy< const std::vector< RefCountedPtr< SystemBody > > > GetBodies() const
Definition: StarSystem.h:68
void MakeShortDescription()
Definition: StarSystem.cpp:297
void ExportToLua(const char *filename)
Definition: StarSystem.cpp:486
ExplorationState
Definition: StarSystem.h:33
@ eUNEXPLORED
Definition: StarSystem.h:34
@ eEXPLORED_BY_PLAYER
Definition: StarSystem.h:35
@ eEXPLORED_AT_START
Definition: StarSystem.h:36
static const Color starColors[]
Definition: StarSystem.h:53
Uint32 GetNumBodies() const
Definition: StarSystem.h:66
static const Color starRealColors[]
Definition: StarSystem.h:54
fixed GetIndustrial() const
Definition: StarSystem.h:87
static RefCountedPtr< StarSystem > FromJson(RefCountedPtr< Galaxy > galaxy, const Json &jsonObj)
Definition: StarSystem.cpp:359
RefCountedPtr< SystemBody > GetRootBody()
Definition: StarSystem.h:59
Definition: Polit.h:51
Definition: SystemBody.h:19
@ SUPERTYPE_STAR
Definition: SystemBody.h:75
@ SUPERTYPE_STARPORT
Definition: SystemBody.h:78
BodySuperType GetSuperType() const
Definition: SystemBody.cpp:114
Definition: SystemPath.h:13
Sint32 sectorZ
Definition: SystemPath.h:56
Uint32 systemIndex
Definition: SystemPath.h:57
Sint32 sectorX
Definition: SystemPath.h:54
Sint32 sectorY
Definition: SystemPath.h:55
uint32_t CommodityId
Definition: Economy.h:29
uint32_t EconomyId
Definition: Economy.h:31
Definition: Color.h:66