Pioneer
Sector.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 _SECTOR_H
5 #define _SECTOR_H
6 
7 #include "GalaxyCache.h"
8 #include "RefCounted.h"
9 #include "galaxy/CustomSystem.h"
10 #include "galaxy/StarSystem.h"
11 #include "galaxy/SystemPath.h"
12 #include "libs.h"
13 #include <string>
14 #include <vector>
15 
16 class Faction;
17 class Galaxy;
18 
19 class Sector : public RefCounted {
20  friend class GalaxyObjectCache<Sector, SystemPath::LessSectorOnly>;
21  friend class GalaxyGenerator;
22 
23 public:
24  // lightyears
25  static const float SIZE;
26  ~Sector();
27 
28  static float DistanceBetween(RefCountedPtr<const Sector> a, int sysIdxA, RefCountedPtr<const Sector> b, int sysIdxB);
29  static float DistanceBetweenSqr(const RefCountedPtr<const Sector> a, const int sysIdxA, const RefCountedPtr<const Sector> b, const int sysIdxB);
30 
31  // Sector is within a bounding rectangle - used for SectorView m_sectorCache pruning.
32  bool WithinBox(const int Xmin, const int Xmax, const int Ymin, const int Ymax, const int Zmin, const int Zmax) const;
33  bool Contains(const SystemPath &sysPath) const;
34 
35  // get the SystemPath for this sector
36  SystemPath GetPath() const { return SystemPath(sx, sy, sz); }
37 
38  class System {
39  public:
40  System(Sector *sector, int x, int y, int z, Uint32 si) :
41  sx(x),
42  sy(y),
43  sz(z),
44  idx(si),
45  m_sector(sector),
46  m_numStars(0),
47  m_seed(0),
48  m_customSys(nullptr),
49  m_faction(nullptr),
50  m_population(-1),
51  m_explored(StarSystem::eUNEXPLORED),
52  m_exploredTime(0.0) {}
53 
54  static float DistanceBetween(const System *a, const System *b);
55 
56  // Check that we've had our habitation status set
57 
58  const std::string &GetName() const { return m_name; }
59  const std::vector<std::string> &GetOtherNames() const { return m_other_names; }
60  const vector3f &GetPosition() const { return m_pos; }
61  vector3f GetFullPosition() const { return Sector::SIZE * vector3f(float(sx), float(sy), float(sz)) + m_pos; };
62  unsigned GetNumStars() const { return m_numStars; }
64  {
65  assert(i < m_numStars);
66  return m_starType[i];
67  }
68  Uint32 GetSeed() const { return m_seed; }
69  const CustomSystem *GetCustomSystem() const { return m_customSys; }
70  const Faction *GetFaction() const
71  {
72  if (!m_faction) AssignFaction();
73  return m_faction;
74  }
75  fixed GetPopulation() const { return m_population; }
76  void SetPopulation(fixed pop) { m_population = pop; }
77  StarSystem::ExplorationState GetExplored() const { return m_explored; }
78  double GetExploredTime() const { return m_exploredTime; }
79  bool IsExplored() const { return m_explored != StarSystem::eUNEXPLORED; }
80  void SetExplored(StarSystem::ExplorationState e, double time);
81 
82  bool IsSameSystem(const SystemPath &b) const
83  {
84  return sx == b.sectorX && sy == b.sectorY && sz == b.sectorZ && idx == b.systemIndex;
85  }
86  bool InSameSector(const SystemPath &b) const
87  {
88  return sx == b.sectorX && sy == b.sectorY && sz == b.sectorZ;
89  }
90  SystemPath GetPath() const { return SystemPath(sx, sy, sz, idx); }
91 
92  const int sx, sy, sz;
93  const Uint32 idx;
94 
95  private:
96  friend class Sector;
100 
101  void AssignFaction() const;
102 
103  Sector *m_sector;
104  std::string m_name;
105  std::vector<std::string> m_other_names;
106  vector3f m_pos;
107  unsigned m_numStars;
108  SystemBody::BodyType m_starType[4];
109  Uint32 m_seed;
110  const CustomSystem *m_customSys;
111  mutable const Faction *m_faction; // mutable because we only calculate on demand
112  fixed m_population;
113  StarSystem::ExplorationState m_explored;
114  double m_exploredTime;
115  };
116  std::vector<System> m_systems;
117  const int sx, sy, sz;
118 
119  void Dump(FILE *file, const char *indent = "") const;
120 
121  sigc::signal<void, Sector::System *, StarSystem::ExplorationState, double> onSetExplorationState;
122 
123 private:
124  Sector(const Sector &); // non-copyable
125  Sector &operator=(const Sector &); // non-assignable
126 
127  RefCountedPtr<Galaxy> m_galaxy;
128  SectorCache *m_cache;
129 
130  // Only SectorCache(Job) are allowed to create sectors
131  Sector(RefCountedPtr<Galaxy> galaxy, const SystemPath &path, SectorCache *cache);
132  void SetCache(SectorCache *cache)
133  {
134  assert(!m_cache);
135  m_cache = cache;
136  }
137  // sets appropriate factions for all systems in the sector
138 };
139 
140 #endif /* _SECTOR_H */
Definition: CustomSystem.h:71
Definition: Factions.h:21
Definition: GalaxyGenerator.h:17
Definition: GalaxyCache.h:20
Definition: Galaxy.h:18
Definition: RefCounted.h:11
Definition: SectorGenerator.h:13
Definition: SectorGenerator.h:31
Definition: SectorGenerator.h:23
Definition: Sector.h:38
static float DistanceBetween(const System *a, const System *b)
Definition: Sector.cpp:112
System(Sector *sector, int x, int y, int z, Uint32 si)
Definition: Sector.h:40
StarSystem::ExplorationState GetExplored() const
Definition: Sector.h:77
bool IsSameSystem(const SystemPath &b) const
Definition: Sector.h:82
bool InSameSector(const SystemPath &b) const
Definition: Sector.h:86
const vector3f & GetPosition() const
Definition: Sector.h:60
fixed GetPopulation() const
Definition: Sector.h:75
unsigned GetNumStars() const
Definition: Sector.h:62
const std::vector< std::string > & GetOtherNames() const
Definition: Sector.h:59
SystemBody::BodyType GetStarType(unsigned i) const
Definition: Sector.h:63
const std::string & GetName() const
Definition: Sector.h:58
vector3f GetFullPosition() const
Definition: Sector.h:61
SystemPath GetPath() const
Definition: Sector.h:90
const Uint32 idx
Definition: Sector.h:93
const int sx
Definition: Sector.h:92
Uint32 GetSeed() const
Definition: Sector.h:68
void SetPopulation(fixed pop)
Definition: Sector.h:76
const int sy
Definition: Sector.h:92
bool IsExplored() const
Definition: Sector.h:79
const CustomSystem * GetCustomSystem() const
Definition: Sector.h:69
const Faction * GetFaction() const
Definition: Sector.h:70
void SetExplored(StarSystem::ExplorationState e, double time)
Definition: Sector.cpp:70
double GetExploredTime() const
Definition: Sector.h:78
const int sz
Definition: Sector.h:92
Definition: Sector.h:19
const int sz
Definition: Sector.h:117
static const float SIZE
Definition: Sector.h:25
SystemPath GetPath() const
Definition: Sector.h:36
const int sy
Definition: Sector.h:117
sigc::signal< void, Sector::System *, StarSystem::ExplorationState, double > onSetExplorationState
Definition: Sector.h:121
void Dump(FILE *file, const char *indent="") const
Definition: Sector.cpp:79
static float DistanceBetween(RefCountedPtr< const Sector > a, int sysIdxA, RefCountedPtr< const Sector > b, int sysIdxB)
Definition: Sector.cpp:30
bool WithinBox(const int Xmin, const int Xmax, const int Ymin, const int Ymax, const int Zmin, const int Zmax) const
Definition: Sector.cpp:46
std::vector< System > m_systems
Definition: Sector.h:116
static float DistanceBetweenSqr(const RefCountedPtr< const Sector > a, const int sysIdxA, const RefCountedPtr< const Sector > b, const int sysIdxB)
Definition: Sector.cpp:38
bool Contains(const SystemPath &sysPath) const
Definition: Sector.cpp:61
const int sx
Definition: Sector.h:117
Definition: StarSystem.h:27
ExplorationState
Definition: StarSystem.h:33
@ eUNEXPLORED
Definition: StarSystem.h:34
BodyType
Definition: SystemBody.h:23
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
vector3< float > vector3f
Definition: vector3.h:289