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 _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
GetPath()36 	SystemPath GetPath() const { return SystemPath(sx, sy, sz); }
37 
38 	class System {
39 	public:
System(Sector * sector,int x,int y,int z,Uint32 si)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 
GetName()58 		const std::string &GetName() const { return m_name; }
GetOtherNames()59 		const std::vector<std::string> &GetOtherNames() const { return m_other_names; }
GetPosition()60 		const vector3f &GetPosition() const { return m_pos; }
GetFullPosition()61 		vector3f GetFullPosition() const { return Sector::SIZE * vector3f(float(sx), float(sy), float(sz)) + m_pos; };
GetNumStars()62 		unsigned GetNumStars() const { return m_numStars; }
GetStarType(unsigned i)63 		SystemBody::BodyType GetStarType(unsigned i) const
64 		{
65 			assert(i < m_numStars);
66 			return m_starType[i];
67 		}
GetSeed()68 		Uint32 GetSeed() const { return m_seed; }
GetCustomSystem()69 		const CustomSystem *GetCustomSystem() const { return m_customSys; }
GetFaction()70 		const Faction *GetFaction() const
71 		{
72 			if (!m_faction) AssignFaction();
73 			return m_faction;
74 		}
GetPopulation()75 		fixed GetPopulation() const { return m_population; }
SetPopulation(fixed pop)76 		void SetPopulation(fixed pop) { m_population = pop; }
GetExplored()77 		StarSystem::ExplorationState GetExplored() const { return m_explored; }
GetExploredTime()78 		double GetExploredTime() const { return m_exploredTime; }
IsExplored()79 		bool IsExplored() const { return m_explored != StarSystem::eUNEXPLORED; }
80 		void SetExplored(StarSystem::ExplorationState e, double time);
81 
IsSameSystem(const SystemPath & b)82 		bool IsSameSystem(const SystemPath &b) const
83 		{
84 			return sx == b.sectorX && sy == b.sectorY && sz == b.sectorZ && idx == b.systemIndex;
85 		}
InSameSector(const SystemPath & b)86 		bool InSameSector(const SystemPath &b) const
87 		{
88 			return sx == b.sectorX && sy == b.sectorY && sz == b.sectorZ;
89 		}
GetPath()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;
97 		friend class SectorCustomSystemsGenerator;
98 		friend class SectorRandomSystemsGenerator;
99 		friend class SectorPersistenceGenerator;
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);
SetCache(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 */
141