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