Pioneer
Space.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 _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 
14 class Body;
15 class Frame;
16 class Game;
17 enum class ObjectType;
18 
19 class Space {
20 public:
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 
69  Uint32 GetNumBodies() const { return static_cast<Uint32>(m_bodies.size()); }
72 
73  Background::Container *GetBackground() { return m_background.get(); }
74  void RefreshBackground();
75 
76  // body finder delegates
77  typedef const std::vector<Body *> BodyNearList;
78  BodyNearList GetBodiesMaybeNear(const Body *b, double dist)
79  {
80  return m_bodyNearFinder.GetBodiesMaybeNear(b, dist);
81  }
82  BodyNearList GetBodiesMaybeNear(const vector3d &pos, double dist)
83  {
84  return m_bodyNearFinder.GetBodiesMaybeNear(pos, dist);
85  }
86 
87  void DebugDumpFrames(bool details);
88 
89  struct BodyDist {
90  BodyDist(Body *_body, double _dist) :
91  body(_body),
92  dist(_dist) {}
94  double dist;
95 
96  bool operator<(const BodyDist &a) const { return dist < a.dist; }
97 
98  friend bool operator<(const BodyDist &a, double d) { return a.dist < d; }
99  friend bool operator<(double d, const BodyDist &a) { return d < a.dist; }
100  };
101 
102  //Find bodies within angle to given direction. dir and offset relative to b like in ship coordinates
103  //returns unsorted vector of bodies with their distance from b+offset
104  //It calculates distance from b for all the bodies so it is quite inefficiet,
105  //it is not designed to be called in each game loop!
106  std::vector<BodyDist> BodiesInAngle(const Body *b, const vector3d &offset, const vector3d &dir, double cosOfMaxAngle) const;
107 
108 private:
109  void GenSectorCache(RefCountedPtr<Galaxy> galaxy, const SystemPath *here);
110  void UpdateStarSystemCache(const SystemPath *here);
111  void GenBody(const double at_time, SystemBody *b, FrameId fId, std::vector<vector3d> &posAccum);
112  // make sure SystemBody* is in Pi::currentSystem
113  FrameId GetFrameWithSystemBody(const SystemBody *b) const;
114 
115  void UpdateBodies();
116 
117  void CollideFrame(FrameId fId);
118 
119  FrameId m_rootFrameId;
120 
121  RefCountedPtr<SectorCache::Slave> m_sectorCache;
122  RefCountedPtr<StarSystemCache::Slave> m_starSystemCache;
123 
124  RefCountedPtr<StarSystem> m_starSystem;
125 
126  Game *m_game;
127 
128  // all the bodies we know about
129  std::vector<Body *> m_bodies;
130 
131  // bodies that were removed/killed this timestep and need pruning at the end
132  enum class BodyAssignation {
133  KILL = 0,
134  REMOVE = 1
135  };
136 
137  std::vector<std::pair<Body *, BodyAssignation>> m_assignedBodies;
138 
139  void RebuildBodyIndex();
140  void RebuildSystemBodyIndex();
141 
142  void AddSystemBodyToIndex(SystemBody *sbody);
143 
144  bool m_bodyIndexValid, m_sbodyIndexValid;
145  std::vector<Body *> m_bodyIndex;
146  std::vector<SystemBody *> m_sbodyIndex;
147 
148  //background (elements that are infinitely far away,
149  //e.g. starfield and milky way)
150  std::unique_ptr<Background::Container> m_background;
151 
152 
153  class BodyNearFinder {
154  public:
155  BodyNearFinder(const Space *space) :
156  m_space(space) {}
157  void Prepare();
158 
159  BodyNearList GetBodiesMaybeNear(const Body *b, double dist);
160  BodyNearList GetBodiesMaybeNear(const vector3d &pos, double dist);
161 
162  private:
163 
164  const Space *m_space;
165  std::vector<BodyDist> m_bodyDist;
166  std::vector<Body *> m_nearBodies;
167  };
168 
169  BodyNearFinder m_bodyNearFinder;
170 
171 #ifndef NDEBUG
172  //to check RemoveBody and KillBody are not called from within
173  //the NotifyRemoved callback (#735)
174  bool m_processingFinalizationQueue;
175 #endif
176 };
177 
178 #endif /* _SPACE_H */
ObjectType
Definition: Body.h:28
IterationProxy< Container > MakeIterationProxy(Container &container)
Definition: IterationProxy.h:51
nlohmann::json Json
Definition: Json.h:8
Definition: Background.h:90
Definition: Body.h:57
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
std::vector< BodyDist > BodiesInAngle(const Body *b, const vector3d &offset, const vector3d &dir, double cosOfMaxAngle) const
Definition: Space.cpp:542
vector3d GetHyperspaceExitPoint(const SystemPath &source) const
Definition: Space.h:60
void KillBody(Body *)
Definition: Space.cpp:443
void RefreshBackground()
Definition: Space.cpp:318
BodyNearList GetBodiesMaybeNear(const vector3d &pos, double dist)
Definition: Space.h:82
FrameId GetRootFrame() const
Definition: Space.h:44
Uint32 GetIndexForSystemBody(const SystemBody *sbody) const
Definition: Space.cpp:380
Uint32 GetNumBodies() const
Definition: Space.h:69
Background::Container * GetBackground()
Definition: Space.h:73
vector3d GetHyperspaceExitPoint(const SystemPath &source, const SystemPath &dest) const
Definition: Space.h:54
RefCountedPtr< StarSystem > GetStarSystem() const
Definition: Space.h:42
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
IterationProxy< std::vector< Body * > > GetBodies()
Definition: Space.h:70
Space(Game *game, RefCountedPtr< Galaxy > galaxy, Space *oldSpace=nullptr)
Definition: Space.cpp:192
const IterationProxy< const std::vector< Body * > > GetBodies() const
Definition: Space.h:71
void AddBody(Body *)
Definition: Space.cpp:430
const std::vector< Body * > BodyNearList
Definition: Space.h:77
void RemoveBody(Body *)
Definition: Space.cpp:435
void TimeStep(float step)
Definition: Space.cpp:1022
void DebugDumpFrames(bool details)
Definition: Space.cpp:1110
BodyNearList GetBodiesMaybeNear(const Body *b, double dist)
Definition: Space.h:78
Body * FindBodyForPath(const SystemPath *path) const
Definition: Space.cpp:564
Uint32 GetIndexForBody(const Body *body) const
Definition: Space.cpp:370
Definition: SystemBody.h:19
Definition: SystemPath.h:13
Definition: FrameId.h:9
Definition: Space.h:89
bool operator<(const BodyDist &a) const
Definition: Space.h:96
Body * body
Definition: Space.h:93
friend bool operator<(double d, const BodyDist &a)
Definition: Space.h:99
BodyDist(Body *_body, double _dist)
Definition: Space.h:90
double dist
Definition: Space.h:94
friend bool operator<(const BodyDist &a, double d)
Definition: Space.h:98