Pioneer
Space.h
Go to the documentation of this file.
1// Copyright © 2008-2022 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 // initalise 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
88private:
89 void GenSectorCache(RefCountedPtr<Galaxy> galaxy, const SystemPath *here);
90 void UpdateStarSystemCache(const SystemPath *here);
91 void GenBody(const double at_time, SystemBody *b, FrameId fId, std::vector<vector3d> &posAccum);
92 // make sure SystemBody* is in Pi::currentSystem
93 FrameId GetFrameWithSystemBody(const SystemBody *b) const;
94
95 void UpdateBodies();
96
97 void CollideFrame(FrameId fId);
98
99 FrameId m_rootFrameId;
100
103
104 RefCountedPtr<StarSystem> m_starSystem;
105
106 Game *m_game;
107
108 // all the bodies we know about
109 std::vector<Body *> m_bodies;
110
111 // bodies that were removed/killed this timestep and need pruning at the end
112 enum class BodyAssignation {
113 KILL = 0,
114 REMOVE = 1
115 };
116
117 std::vector<std::pair<Body *, BodyAssignation>> m_assignedBodies;
118
119 void RebuildBodyIndex();
120 void RebuildSystemBodyIndex();
121
122 void AddSystemBodyToIndex(SystemBody *sbody);
123
124 bool m_bodyIndexValid, m_sbodyIndexValid;
125 std::vector<Body *> m_bodyIndex;
126 std::vector<SystemBody *> m_sbodyIndex;
127
128 //background (elements that are infinitely far away,
129 //e.g. starfield and milky way)
130 std::unique_ptr<Background::Container> m_background;
131
132 class BodyNearFinder {
133 public:
134 BodyNearFinder(const Space *space) :
135 m_space(space) {}
136 void Prepare();
137
138 BodyNearList GetBodiesMaybeNear(const Body *b, double dist);
139 BodyNearList GetBodiesMaybeNear(const vector3d &pos, double dist);
140
141 private:
142 struct BodyDist {
143 BodyDist(Body *_body, double _dist) :
144 body(_body),
145 dist(_dist) {}
146 Body *body;
147 double dist;
148
149 bool operator<(const BodyDist &a) const { return dist < a.dist; }
150
151 friend bool operator<(const BodyDist &a, double d) { return a.dist < d; }
152 friend bool operator<(double d, const BodyDist &a) { return d < a.dist; }
153 };
154
155 const Space *m_space;
156 std::vector<BodyDist> m_bodyDist;
157 std::vector<Body *> m_nearBodies;
158 };
159
160 BodyNearFinder m_bodyNearFinder;
161
162#ifndef NDEBUG
163 //to check RemoveBody and KillBody are not called from within
164 //the NotifyRemoved callback (#735)
165 bool m_processingFinalizationQueue;
166#endif
167};
168
169#endif /* _SPACE_H */
ObjectType
Definition: Body.h:27
IterationProxy< Container > MakeIterationProxy(Container &container)
Definition: IterationProxy.h:51
nlohmann::json Json
Definition: Json.h:8
Definition: Background.h:90
Definition: Body.h:54
Definition: Frame.h:28
Definition: Game.h:38
Definition: IterationProxy.h:13
Definition: Space.h:19
~Space()
Definition: Space.cpp:309
Body * FindNearestTo(const Body *b, ObjectType t) const
Definition: Space.cpp:525
void GetHyperspaceExitParams(const SystemPath &source, const SystemPath &dest, vector3d &pos, vector3d &vel) const
Definition: Space.cpp:462
vector3d GetHyperspaceExitPoint(const SystemPath &source) const
Definition: Space.h:60
Background::Container * GetBackground()
Definition: Space.h:72
void KillBody(Body *)
Definition: Space.cpp:443
void RefreshBackground()
Definition: Space.cpp:318
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:380
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:363
Body * GetBodyByIndex(Uint32 idx) const
Definition: Space.cpp:352
void ToJson(Json &jsonObj)
Definition: Space.cpp:327
Space(Game *game, RefCountedPtr< Galaxy > galaxy, Space *oldSpace=nullptr)
Definition: Space.cpp:192
void AddBody(Body *)
Definition: Space.cpp:430
const std::vector< Body * > BodyNearList
Definition: Space.h:76
void RemoveBody(Body *)
Definition: Space.cpp:435
void TimeStep(float step)
Definition: Space.cpp:1000
RefCountedPtr< StarSystem > GetStarSystem() const
Definition: Space.h:42
void DebugDumpFrames(bool details)
Definition: Space.cpp:1088
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:542
Uint32 GetIndexForBody(const Body *body) const
Definition: Space.cpp:370
Definition: SystemBody.h:19
Definition: SystemPath.h:13
Definition: FrameId.h:9