Pioneer
Loading...
Searching...
No Matches
Game.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 _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
13class GameLog;
14class HyperspaceCloud;
15class Player;
16class Space;
17
18namespace Graphics {
19 class Renderer;
20}
21
26 std::string error;
27 InvalidGameStartLocation(const std::string &error_) :
28 error(error_) {}
29};
30
31class View;
32class SectorView;
33class SystemView;
34class WorldView;
35class DeathView;
37
38class Game {
39public:
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 static bool DeleteSave(const std::string &filename);
48 static int CurrentSaveVersion();
49
50 // start docked in station referenced by path or nearby to body if it is no station
51 Game(const SystemPath &path, const double startDateTime, const char *shipType = "kanara");
52
53 // load game
54 Game(const Json &jsonObj);
55
56 ~Game();
57
58 // save game
59 void ToJson(Json &jsonObj);
60
61 // various game states
62 bool IsNormalSpace() const { return m_state == State::NORMAL; }
63 bool IsHyperspace() const { return m_state == State::HYPERSPACE; }
64
65 RefCountedPtr<Galaxy> GetGalaxy() const { return m_galaxy; }
66 Space *GetSpace() const { return m_space.get(); }
67 double GetTime() const { return m_time; }
68 Player *GetPlayer() const { return m_player.get(); }
69
70 // physics step
71 void TimeStep(float step);
72
73 // update time acceleration once per render frame
74 // returns true if timeaccel was changed
75 bool UpdateTimeAccel();
76
77 // request switch to hyperspace
78 void WantHyperspace();
79
80 // hyperspace parameters. only meaningful when IsHyperspace() is true
81 float GetHyperspaceProgress() const { return m_hyperspaceProgress; }
82 double GetHyperspaceDuration() const { return m_hyperspaceDuration; }
83 double GetHyperspaceEndTime() const { return m_hyperspaceEndTime; }
85 const SystemPath &GetHyperspaceDest() const { return m_hyperspaceDest; }
86 const SystemPath &GetHyperspaceSource() const { return m_hyperspaceSource; }
88
89 enum TimeAccel {
97 };
98
99 void SetTimeAccel(TimeAccel t);
100 void RequestTimeAccel(TimeAccel t, bool force = false);
101
104 void RequestTimeAccelInc(bool force = false);
107 void RequestTimeAccelDec(bool force = false);
108
109 TimeAccel GetTimeAccel() const { return m_timeAccel; }
110 TimeAccel GetRequestedTimeAccel() const { return m_requestedTimeAccel; }
111 bool IsPaused() const { return m_timeAccel == TIMEACCEL_PAUSED; }
112
113 float GetTimeAccelRate() const { return s_timeAccelRates[m_timeAccel]; }
114 float GetInvTimeAccelRate() const { return s_timeInvAccelRates[m_timeAccel]; }
115
116 float GetTimeStep() const { return s_timeAccelRates[m_timeAccel] * (1.0f / PHYSICS_HZ); }
117
118 SectorView *GetSectorView() const { return m_gameViews->m_sectorView; }
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
130private:
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 = nullptr;
141 SystemView *m_systemView = nullptr;
142 WorldView *m_worldView = nullptr;
143 DeathView *m_deathView = nullptr;
144 View *m_spaceStationView = nullptr;
145 View *m_infoView = nullptr;
146
147 /* Only use #if WITH_OBJECTVIEWER */
148 ObjectViewerView *m_objectViewerView = nullptr;
149 };
150
151 void CreateViews();
152 void LoadViewsFromJson(const Json &jsonObj);
153 void DestroyViews();
154
155 static void EmitPauseState(bool paused);
156
157 void SwitchToHyperspace();
158 void SwitchToNormalSpace();
159
160 std::unique_ptr<Player> m_player;
161
162 RefCountedPtr<Galaxy> m_galaxy;
163 std::unique_ptr<Views> m_gameViews;
164 std::unique_ptr<Space> m_space;
165 double m_time;
166
167 enum class State {
168 NORMAL,
169 HYPERSPACE,
170 };
171 State m_state;
172
173 bool m_wantHyperspace;
174
175 std::list<HyperspaceCloud *> m_hyperspaceClouds;
176 SystemPath m_hyperspaceSource;
177 SystemPath m_hyperspaceDest;
178 double m_hyperspaceProgress;
179 double m_hyperspaceDuration;
180 double m_hyperspaceEndTime;
181
182 TimeAccel m_timeAccel;
183 TimeAccel m_requestedTimeAccel;
184 bool m_forceTimeAccel;
185 static const float s_timeAccelRates[];
186 static const float s_timeInvAccelRates[];
187};
188
189#endif
nlohmann::json Json
Definition: Json.h:8
Definition: DeathView.h:14
Definition: GameLog.h:15
Definition: Game.h:38
void ToJson(Json &jsonObj)
Definition: Game.cpp:214
TimeAccel GetTimeAccel() const
Definition: Game.h:109
TimeAccel GetRequestedTimeAccel() const
Definition: Game.h:110
void RequestTimeAccelInc(bool force=false)
Definition: Game.cpp:717
View * GetInfoView() const
Definition: Game.h:123
bool IsNormalSpace() const
Definition: Game.h:62
double GetHyperspaceEndTime() const
Definition: Game.h:83
SystemView * GetSystemView() const
Definition: Game.h:119
float GetInvTimeAccelRate() const
Definition: Game.h:114
double GetTime() const
Definition: Game.h:67
ObjectViewerView * GetObjectViewerView() const
void WantHyperspace()
Definition: Game.cpp:426
void SetTimeAccel(TimeAccel t)
Definition: Game.cpp:676
TimeAccel
Definition: Game.h:89
@ TIMEACCEL_1X
Definition: Game.h:91
@ TIMEACCEL_10X
Definition: Game.h:92
@ TIMEACCEL_HYPERSPACE
Definition: Game.h:96
@ TIMEACCEL_10000X
Definition: Game.h:95
@ TIMEACCEL_1000X
Definition: Game.h:94
@ TIMEACCEL_100X
Definition: Game.h:93
@ TIMEACCEL_PAUSED
Definition: Game.h:90
void RemoveHyperspaceCloud(HyperspaceCloud *)
Definition: Game.cpp:440
bool IsHyperspace() const
Definition: Game.h:63
static int CurrentSaveVersion()
Definition: Game.cpp:1004
SectorView * GetSectorView() const
Definition: Game.h:118
static Game * LoadGame(const std::string &filename)
Definition: Game.cpp:913
const SystemPath & GetHyperspaceDest() const
Definition: Game.h:85
static bool CanLoadGame(const std::string &filename)
Definition: Game.cpp:928
void RequestTimeAccel(TimeAccel t, bool force=false)
Definition: Game.cpp:711
float GetHyperspaceProgress() const
Definition: Game.h:81
double GetHyperspaceArrivalProbability() const
Definition: Game.cpp:432
static bool DeleteSave(const std::string &filename)
Definition: Game.cpp:943
bool UpdateTimeAccel()
Definition: Game.cpp:340
Player * GetPlayer() const
Definition: Game.h:68
WorldView * GetWorldView() const
Definition: Game.h:120
float GetTimeAccelRate() const
Definition: Game.h:113
double GetHyperspaceDuration() const
Definition: Game.h:82
const SystemPath & GetHyperspaceSource() const
Definition: Game.h:86
bool IsPaused() const
Definition: Game.h:111
View * GetSpaceStationView() const
Definition: Game.h:122
void TimeStep(float step)
Definition: Game.cpp:312
~Game()
Definition: Game.cpp:119
float GetTimeStep() const
Definition: Game.h:116
Space * GetSpace() const
Definition: Game.h:66
void RequestTimeAccelDec(bool force=false)
Definition: Game.cpp:739
DeathView * GetDeathView() const
Definition: Game.h:121
RefCountedPtr< Galaxy > GetGalaxy() const
Definition: Game.h:65
static void SaveGame(const std::string &filename, Game *game)
Definition: Game.cpp:954
static Json LoadGameToJson(const std::string &filename)
Definition: Game.cpp:899
GameLog * log
Definition: Game.h:128
Definition: Renderer.h:45
Definition: HyperspaceCloud.h:18
Definition: ObjectViewerView.h:13
Definition: Player.h:16
Definition: RefCounted.h:36
Definition: SectorView.h:23
Definition: Space.h:19
Definition: SystemPath.h:13
Definition: SystemView.h:164
Definition: View.h:20
Definition: WorldView.h:30
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