Pioneer
Loading...
Searching...
No Matches
Space.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _SPACE_H
5#define _SPACE_H
6
7#include "Background.h"
8#include "FrameId.h"
9#include "IterationProxy.h"
10#include "RefCounted.h"
11#include "galaxy/StarSystem.h"
12#include "vector3.h"
13
14class Body;
15class Frame;
16class Game;
17enum class ObjectType;
18
19class Space {
20public:
21 // empty space (eg for hyperspace)
22 Space(Game *game, RefCountedPtr<Galaxy> galaxy, Space *oldSpace = nullptr);
23
24 // initialise with system bodies
25 Space(Game *game, RefCountedPtr<Galaxy> galaxy, const SystemPath &path, Space *oldSpace = nullptr);
26
27 // initialise from save file
28 Space(Game *game, RefCountedPtr<Galaxy> galaxy, const Json &jsonObj, double at_time);
29
30 ~Space();
31
32 void ToJson(Json &jsonObj);
33
34 // body/sbody indexing for save/load. valid after
35 // construction/ToJson(), invalidated by TimeStep(). they will assert
36 // if called while invalid
37 Body *GetBodyByIndex(Uint32 idx) const;
38 SystemBody *GetSystemBodyByIndex(Uint32 idx) const;
39 Uint32 GetIndexForBody(const Body *body) const;
40 Uint32 GetIndexForSystemBody(const SystemBody *sbody) const;
41
42 RefCountedPtr<StarSystem> GetStarSystem() const { return m_starSystem; }
43
44 FrameId GetRootFrame() const { return m_rootFrameId; }
45
46 void AddBody(Body *);
47 void RemoveBody(Body *);
48 void KillBody(Body *);
49
50 void TimeStep(float step);
51
52 void GetHyperspaceExitParams(const SystemPath &source, const SystemPath &dest,
53 vector3d &pos, vector3d &vel) const;
54 vector3d GetHyperspaceExitPoint(const SystemPath &source, const SystemPath &dest) const
55 {
56 vector3d pos, vel;
57 GetHyperspaceExitParams(source, dest, pos, vel);
58 return pos;
59 }
61 {
62 return GetHyperspaceExitPoint(source, m_starSystem->GetPath());
63 }
64
65 Body *FindNearestTo(const Body *b, ObjectType t) const;
66 Body *FindBodyForPath(const SystemPath *path) const;
67
68 Uint32 GetNumBodies() const { return static_cast<Uint32>(m_bodies.size()); }
71
72 Background::Container *GetBackground() { return m_background.get(); }
73 void RefreshBackground();
74
75 // body finder delegates
76 typedef const std::vector<Body *> BodyNearList;
77 BodyNearList GetBodiesMaybeNear(const Body *b, double dist)
78 {
79 return m_bodyNearFinder.GetBodiesMaybeNear(b, dist);
80 }
81 BodyNearList GetBodiesMaybeNear(const vector3d &pos, double dist)
82 {
83 return m_bodyNearFinder.GetBodiesMaybeNear(pos, dist);
84 }
85
86 void DebugDumpFrames(bool details);
87
88 struct BodyDist {
89 BodyDist(Body *_body, double _dist) :
90 body(_body),
91 dist(_dist) {}
93 double dist;
94
95 bool operator<(const BodyDist &a) const { return dist < a.dist; }
96
97 friend bool operator<(const BodyDist &a, double d) { return a.dist < d; }
98 friend bool operator<(double d, const BodyDist &a) { return d < a.dist; }
99 };
100
101 //Find bodies within angle to given direction. dir and offset relative to b like in ship coordinates
102 //returns unsorted vector of bodies with their distance from b+offset
103 //It calculates distance from b for all the bodies so it is quite inefficiet,
104 //it is not designed to be called in each game loop!
105 std::vector<BodyDist> BodiesInAngle(const Body *b, const vector3d &offset, const vector3d &dir, double cosOfMaxAngle) const;
106
107private:
108 void GenSectorCache(RefCountedPtr<Galaxy> galaxy, const SystemPath *here);
109 void UpdateStarSystemCache(const SystemPath *here);
110 void GenBody(const double at_time, SystemBody *b, FrameId fId, std::vector<vector3d> &posAccum);
111 // make sure SystemBody* is in Pi::currentSystem
112 FrameId GetFrameWithSystemBody(const SystemBody *b) const;
113
114 void UpdateBodies();
115
116 void CollideFrame(FrameId fId);
117
118 FrameId m_rootFrameId;
119
122
123 RefCountedPtr<StarSystem> m_starSystem;
124
125 Game *m_game;
126
127 // all the bodies we know about
128 std::vector<Body *> m_bodies;
129
130 // bodies that were removed/killed this timestep and need pruning at the end
131 enum class BodyAssignation {
132 KILL = 0,
133 REMOVE = 1
134 };
135
136 std::vector<std::pair<Body *, BodyAssignation>> m_assignedBodies;
137
138 void RebuildBodyIndex();
139 void RebuildSystemBodyIndex();
140
141 void AddSystemBodyToIndex(SystemBody *sbody);
142
143 bool m_bodyIndexValid, m_sbodyIndexValid;
144 std::vector<Body *> m_bodyIndex;
145 std::vector<SystemBody *> m_sbodyIndex;
146
147 //background (elements that are infinitely far away,
148 //e.g. starfield and milky way)
149 std::unique_ptr<Background::Container> m_background;
150
151 class BodyNearFinder {
152 public:
153 BodyNearFinder(const Space *space) :
154 m_space(space) {}
155 void Prepare();
156
157 BodyNearList GetBodiesMaybeNear(const Body *b, double dist);
158 BodyNearList GetBodiesMaybeNear(const vector3d &pos, double dist);
159
160 private:
161 const Space *m_space;
162 std::vector<BodyDist> m_bodyDist;
163 std::vector<Body *> m_nearBodies;
164 };
165
166 BodyNearFinder m_bodyNearFinder;
167
168#ifndef NDEBUG
169 //to check RemoveBody and KillBody are not called from within
170 //the NotifyRemoved callback (#735)
171 bool m_processingFinalizationQueue;
172#endif
173};
174
175#endif /* _SPACE_H */
ObjectType
Definition Body.h:29
IterationProxy< Container > MakeIterationProxy(Container &container)
Definition IterationProxy.h:51
nlohmann::json Json
Definition Json.h:8
Definition Background.h:90
Definition Body.h:65
Definition Frame.h:28
Definition Game.h:38
Definition IterationProxy.h:13
Definition RefCounted.h:36
Definition Space.h:19
~Space()
Definition Space.cpp:309
Body * FindNearestTo(const Body *b, ObjectType t) const
Definition Space.cpp:552
void GetHyperspaceExitParams(const SystemPath &source, const SystemPath &dest, vector3d &pos, vector3d &vel) const
Definition Space.cpp:489
std::vector< BodyDist > BodiesInAngle(const Body *b, const vector3d &offset, const vector3d &dir, double cosOfMaxAngle) const
Definition Space.cpp:569
vector3d GetHyperspaceExitPoint(const SystemPath &source) const
Definition Space.h:60
Background::Container * GetBackground()
Definition Space.h:72
void KillBody(Body *)
Definition Space.cpp:470
void RefreshBackground()
Definition Space.cpp:324
BodyNearList GetBodiesMaybeNear(const vector3d &pos, double dist)
Definition Space.h:81
FrameId GetRootFrame() const
Definition Space.h:44
Uint32 GetIndexForSystemBody(const SystemBody *sbody) const
Definition Space.cpp:407
Uint32 GetNumBodies() const
Definition Space.h:68
vector3d GetHyperspaceExitPoint(const SystemPath &source, const SystemPath &dest) const
Definition Space.h:54
IterationProxy< std::vector< Body * > > GetBodies()
Definition Space.h:69
SystemBody * GetSystemBodyByIndex(Uint32 idx) const
Definition Space.cpp:390
Body * GetBodyByIndex(Uint32 idx) const
Definition Space.cpp:379
void ToJson(Json &jsonObj)
Definition Space.cpp:339
void AddBody(Body *)
Definition Space.cpp:457
const std::vector< Body * > BodyNearList
Definition Space.h:76
void RemoveBody(Body *)
Definition Space.cpp:462
void TimeStep(float step)
Definition Space.cpp:1055
RefCountedPtr< StarSystem > GetStarSystem() const
Definition Space.h:42
void DebugDumpFrames(bool details)
Definition Space.cpp:1152
const IterationProxy< const std::vector< Body * > > GetBodies() const
Definition Space.h:70
BodyNearList GetBodiesMaybeNear(const Body *b, double dist)
Definition Space.h:77
Body * FindBodyForPath(const SystemPath *path) const
Definition Space.cpp:591
Uint32 GetIndexForBody(const Body *body) const
Definition Space.cpp:397
Definition SystemBody.h:137
Definition SystemPath.h:13
Definition FrameId.h:9
Definition Space.h:88
bool operator<(const BodyDist &a) const
Definition Space.h:95
Body * body
Definition Space.h:92
friend bool operator<(double d, const BodyDist &a)
Definition Space.h:98
BodyDist(Body *_body, double _dist)
Definition Space.h:89
double dist
Definition Space.h:93
friend bool operator<(const BodyDist &a, double d)
Definition Space.h:97