1 // Copyright 2014-2018 the openage authors. See copying.md for legal info.
2 
3 #pragma once
4 
5 #include <memory>
6 #include <vector>
7 #include <SDL2/SDL.h>
8 
9 #include <QObject>
10 
11 #include "market.h"
12 #include "player.h"
13 #include "team.h"
14 #include "../options.h"
15 #include "../terrain/terrain.h"
16 #include "../unit/unit_container.h"
17 #include "../util/timing.h"
18 
19 
20 namespace openage {
21 
22 class Engine;
23 class Generator;
24 class Terrain;
25 
26 
27 /**
28  * Contains information for a single game
29  * This information must be synced across network clients
30  *
31  * TODO: include a list of actions to be saved
32  *       as the game replay file
33  */
34 class GameMain : public options::OptionNode {
35 public:
36 	GameMain(const Generator &generator);
37 	~GameMain();
38 
39 	/**
40 	 * the number of players
41 	 */
42 	unsigned int player_count() const;
43 
44 	/**
45 	 * player by index
46 	 */
47 	Player *get_player(unsigned int player_id);
48 
49 	/**
50 	 * the number of teams
51 	 */
52 	unsigned int team_count() const;
53 
54 	/**
55 	 * team by id
56 	 */
57 	Team *get_team(unsigned int team_id);
58 
59 	/**
60 	 * the spec in this games settings
61 	 */
62 	GameSpec *get_spec();
63 
64 	/**
65 	 * updates the game by one frame
66 	 */
67 	void update(time_nsec_t lastframe_duration);
68 
69 	/**
70 	 * map information
71 	 */
72 	std::shared_ptr<Terrain> terrain;
73 
74 	/**
75 	 * all teams in the game
76 	 */
77 	std::vector<Team> teams;
78 
79 	/**
80 	 * The global market (the global market prices).
81 	 */
82 	Market market;
83 
84 	/**
85 	 * all the objects that have been placed.
86 	 */
87 	UnitContainer placed_units;
88 
89 private:
90 
91 	/**
92 	 * all players in the game
93 	 * no objects should be added of removed once populated
94 	 */
95 	std::vector<std::shared_ptr<Player>> players;
96 
97 	/**
98 	 * creates a random civ, owned and managed by this game
99 	 */
100 	Civilisation *add_civ(int civ_id);
101 
102 	/**
103 	 * civs used in this game
104 	 */
105 	std::vector<std::shared_ptr<Civilisation>> civs;
106 
107 	std::shared_ptr<GameSpec> spec;
108 };
109 
110 } // openage
111 
112 namespace qtsdl {
113 class GuiItemLink;
114 } // qtsdl
115 
116 namespace openage {
117 
118 class GameMainSignals : public QObject {
119 	Q_OBJECT
120 
121 public:
122 signals:
123 	void game_running(bool running);
124 };
125 
126 
127 /**
128  * Class linked to the QML object "GameMain" via GameMainLink.
129  * Gets instanciated from QML.
130  */
131 class GameMainHandle {
132 public:
133 	explicit GameMainHandle(qtsdl::GuiItemLink *gui_link);
134 
135 	void set_engine(Engine *engine);
136 
137 	/**
138 	 * End the game and delete the game handle.
139 	 */
140 	void clear();
141 
142 	/**
143 	 * Pass the given game to the engine and start it.
144 	 */
145 	void set_game(std::unique_ptr<GameMain> &&game);
146 
147 	/**
148 	 * Return the game.
149 	 */
150 	GameMain* get_game() const;
151 
152 	/**
153 	 * Test if there is a game running.
154 	 */
155 	bool is_game_running() const;
156 
157 	/**
158 	 * Emit a qt signal to notify for changes in a running game.
159 	 */
160 	void announce_running();
161 
162 private:
163 	/**
164 	 * The game state as currently owned by the engine,
165 	 * just remembered here to access it quickly.
166 	 */
167 	GameMain *game;
168 
169 	/**
170 	 * The engine the main game handle is attached to.
171 	 */
172 	Engine *engine;
173 
174 public:
175 	GameMainSignals gui_signals;
176 	qtsdl::GuiItemLink *gui_link;
177 };
178 
179 } // openage
180