Pioneer
Game.h
Go to the documentation of this file.
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 _GAME_H
5 #define _GAME_H
6 
7 #include "JsonFwd.h"
8 #include "galaxy/Galaxy.h"
9 #include "galaxy/SystemPath.h"
10 #include "gameconsts.h"
11 #include <string>
12 
13 class GameLog;
14 class HyperspaceCloud;
15 class Player;
16 class Space;
17 
18 namespace Graphics {
19  class Renderer;
20 }
21 
26  std::string error;
27  InvalidGameStartLocation(const std::string &error_) :
28  error(error_) {}
29 };
30 
31 class View;
32 class SectorView;
33 class SystemInfoView;
34 class SystemView;
35 class WorldView;
36 class DeathView;
37 class ObjectViewerView;
38 
39 class Game {
40 public:
41  static Json LoadGameToJson(const std::string &filename);
42  // LoadGame and SaveGame throw exceptions on failure
43  static Game *LoadGame(const std::string &filename);
44  static bool CanLoadGame(const std::string &filename);
45  // XXX game arg should be const, and this should probably be a member function
46  // (or LoadGame/SaveGame should be somewhere else entirely)
47  static void SaveGame(const std::string &filename, Game *game);
48 
49  // start docked in station referenced by path or nearby to body if it is no station
50  Game(const SystemPath &path, const double startDateTime = 0.0);
51 
52  // load game
53  Game(const Json &jsonObj);
54 
55  ~Game();
56 
57  // save game
58  void ToJson(Json &jsonObj);
59 
60  // various game states
61  bool IsNormalSpace() const { return m_state == State::NORMAL; }
62  bool IsHyperspace() const { return m_state == State::HYPERSPACE; }
63 
64  RefCountedPtr<Galaxy> GetGalaxy() const { return m_galaxy; }
65  Space *GetSpace() const { return m_space.get(); }
66  double GetTime() const { return m_time; }
67  Player *GetPlayer() const { return m_player.get(); }
68 
69  // physics step
70  void TimeStep(float step);
71 
72  // update time acceleration once per render frame
73  // returns true if timeaccel was changed
74  bool UpdateTimeAccel();
75 
76  // request switch to hyperspace
77  void WantHyperspace();
78 
79  // hyperspace parameters. only meaningful when IsHyperspace() is true
80  float GetHyperspaceProgress() const { return m_hyperspaceProgress; }
81  double GetHyperspaceDuration() const { return m_hyperspaceDuration; }
82  double GetHyperspaceEndTime() const { return m_hyperspaceEndTime; }
83  double GetHyperspaceArrivalProbability() const;
84  const SystemPath &GetHyperspaceDest() const { return m_hyperspaceDest; }
85  const SystemPath &GetHyperspaceSource() const { return m_hyperspaceSource; }
87 
88  enum TimeAccel {
96  };
97 
98  void SetTimeAccel(TimeAccel t);
99  void RequestTimeAccel(TimeAccel t, bool force = false);
100 
103  void RequestTimeAccelInc(bool force = false);
106  void RequestTimeAccelDec(bool force = false);
107 
108  TimeAccel GetTimeAccel() const { return m_timeAccel; }
109  TimeAccel GetRequestedTimeAccel() const { return m_requestedTimeAccel; }
110  bool IsPaused() const { return m_timeAccel == TIMEACCEL_PAUSED; }
111 
112  float GetTimeAccelRate() const { return s_timeAccelRates[m_timeAccel]; }
113  float GetInvTimeAccelRate() const { return s_timeInvAccelRates[m_timeAccel]; }
114 
115  float GetTimeStep() const { return s_timeAccelRates[m_timeAccel] * (1.0f / PHYSICS_HZ); }
116 
117  SectorView *GetSectorView() const { return m_gameViews->m_sectorView; }
118  SystemInfoView *GetSystemInfoView() const { return m_gameViews->m_systemInfoView; }
119  SystemView *GetSystemView() const { return m_gameViews->m_systemView; }
120  WorldView *GetWorldView() const { return m_gameViews->m_worldView; }
121  DeathView *GetDeathView() const { return m_gameViews->m_deathView; }
122  View *GetSpaceStationView() const { return m_gameViews->m_spaceStationView; }
123  View *GetInfoView() const { return m_gameViews->m_infoView; }
124 
125  /* Only use #if WITH_OBJECTVIEWER */
127 
129 
130 private:
131  class Views {
132  public:
133  Views();
134  void Init(Game *game);
135  void LoadFromJson(const Json &jsonObj, Game *game);
136  ~Views();
137 
138  void SetRenderer(Graphics::Renderer *r);
139 
140  SectorView *m_sectorView;
141  SystemInfoView *m_systemInfoView;
142  SystemView *m_systemView;
143  WorldView *m_worldView;
144  DeathView *m_deathView;
145  View *m_spaceStationView;
146  View *m_infoView;
147 
148  /* Only use #if WITH_OBJECTVIEWER */
149  ObjectViewerView *m_objectViewerView;
150  };
151 
152  void CreateViews();
153  void LoadViewsFromJson(const Json &jsonObj);
154  void DestroyViews();
155 
156  static void EmitPauseState(bool paused);
157 
158  void SwitchToHyperspace();
159  void SwitchToNormalSpace();
160 
161  RefCountedPtr<Galaxy> m_galaxy;
162  std::unique_ptr<Views> m_gameViews;
163  std::unique_ptr<Space> m_space;
164  double m_time;
165 
166  std::unique_ptr<Player> m_player;
167 
168  enum class State {
169  NORMAL,
170  HYPERSPACE,
171  };
172  State m_state;
173 
174  bool m_wantHyperspace;
175 
176  std::list<HyperspaceCloud *> m_hyperspaceClouds;
177  SystemPath m_hyperspaceSource;
178  SystemPath m_hyperspaceDest;
179  double m_hyperspaceProgress;
180  double m_hyperspaceDuration;
181  double m_hyperspaceEndTime;
182 
183  TimeAccel m_timeAccel;
184  TimeAccel m_requestedTimeAccel;
185  bool m_forceTimeAccel;
186  static const float s_timeAccelRates[];
187  static const float s_timeInvAccelRates[];
188 };
189 
190 #endif
nlohmann::json Json
Definition: Json.h:8
Definition: DeathView.h:14
Definition: GameLog.h:12
Definition: Game.h:39
void ToJson(Json &jsonObj)
Definition: Game.cpp:186
Game(const SystemPath &path, const double startDateTime=0.0)
Definition: Game.cpp:38
RefCountedPtr< Galaxy > GetGalaxy() const
Definition: Game.h:64
TimeAccel GetTimeAccel() const
Definition: Game.h:108
SystemInfoView * GetSystemInfoView() const
Definition: Game.h:118
TimeAccel GetRequestedTimeAccel() const
Definition: Game.h:109
void RequestTimeAccelInc(bool force=false)
Definition: Game.cpp:684
Space * GetSpace() const
Definition: Game.h:65
bool IsNormalSpace() const
Definition: Game.h:61
double GetHyperspaceEndTime() const
Definition: Game.h:82
Player * GetPlayer() const
Definition: Game.h:67
float GetInvTimeAccelRate() const
Definition: Game.h:113
double GetTime() const
Definition: Game.h:66
void WantHyperspace()
Definition: Game.cpp:394
void SetTimeAccel(TimeAccel t)
Definition: Game.cpp:643
TimeAccel
Definition: Game.h:88
@ TIMEACCEL_1X
Definition: Game.h:90
@ TIMEACCEL_10X
Definition: Game.h:91
@ TIMEACCEL_HYPERSPACE
Definition: Game.h:95
@ TIMEACCEL_10000X
Definition: Game.h:94
@ TIMEACCEL_1000X
Definition: Game.h:93
@ TIMEACCEL_100X
Definition: Game.h:92
@ TIMEACCEL_PAUSED
Definition: Game.h:89
const SystemPath & GetHyperspaceDest() const
Definition: Game.h:84
void RemoveHyperspaceCloud(HyperspaceCloud *)
Definition: Game.cpp:408
View * GetInfoView() const
Definition: Game.h:123
bool IsHyperspace() const
Definition: Game.h:62
static Game * LoadGame(const std::string &filename)
Definition: Game.cpp:882
const SystemPath & GetHyperspaceSource() const
Definition: Game.h:85
View * GetSpaceStationView() const
Definition: Game.h:122
static bool CanLoadGame(const std::string &filename)
Definition: Game.cpp:897
DeathView * GetDeathView() const
Definition: Game.h:121
void RequestTimeAccel(TimeAccel t, bool force=false)
Definition: Game.cpp:678
float GetHyperspaceProgress() const
Definition: Game.h:80
double GetHyperspaceArrivalProbability() const
Definition: Game.cpp:400
bool UpdateTimeAccel()
Definition: Game.cpp:308
SectorView * GetSectorView() const
Definition: Game.h:117
float GetTimeAccelRate() const
Definition: Game.h:112
double GetHyperspaceDuration() const
Definition: Game.h:81
bool IsPaused() const
Definition: Game.h:110
ObjectViewerView * GetObjectViewerView() const
void TimeStep(float step)
Definition: Game.cpp:280
~Game()
Definition: Game.cpp:95
float GetTimeStep() const
Definition: Game.h:115
SystemView * GetSystemView() const
Definition: Game.h:119
void RequestTimeAccelDec(bool force=false)
Definition: Game.cpp:706
static void SaveGame(const std::string &filename, Game *game)
Definition: Game.cpp:907
static Json LoadGameToJson(const std::string &filename)
Definition: Game.cpp:868
GameLog * log
Definition: Game.h:128
WorldView * GetWorldView() const
Definition: Game.h:120
Definition: Renderer.h:39
Definition: HyperspaceCloud.h:19
Definition: ObjectViewerView.h:15
Definition: Player.h:16
Definition: SectorView.h:25
Definition: Space.h:19
Definition: SystemInfoView.h:23
Definition: SystemPath.h:13
Definition: SystemView.h:80
Definition: View.h:22
Definition: WorldView.h:33
void Init()
Definition: EnumStrings.cpp:14
void LoadFromJson(const Json &obj)
Definition: Economy.cpp:279
Definition: Background.h:13
Definition: Game.h:22
Definition: Game.h:24
Definition: Game.h:23
Definition: Game.h:25
InvalidGameStartLocation(const std::string &error_)
Definition: Game.h:27
std::string error
Definition: Game.h:26