Symphony Of Empires
game_state.hpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // client/game_state.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <queue>
28 #include <mutex>
29 #include <vector>
30 #include <atomic>
31 #include <functional>
32 #include <algorithm>
33 
34 #include "eng3d/serializer.hpp"
35 #include "eng3d/audio.hpp"
36 #include "eng3d/state.hpp"
37 #include "eng3d/event.hpp"
38 #include "objects.hpp"
39 #include "unit.hpp"
42 #include "client/map.hpp"
43 #include "eventpp/callbacklist.h"
44 
45 enum class MapMode : unsigned char {
47  NORMAL,
48  NO_MAP,
50 };
51 
52 struct Building;
53 struct Language;
54 struct Religion;
55 
56 class ClientState {
57  eventpp::CallbackList<void (const std::vector<UnitId>)> on_update_units;
58  std::vector<UnitId> selected_units;
59 public:
60  inline const std::vector<UnitId> get_selected_units() const {
61  return selected_units;
62  }
63 
64  inline bool is_selected_unit(UnitId id) const {
65  return std::count(selected_units.begin(), selected_units.end(), id);
66  }
67 
68  inline void select_unit(UnitId id) {
69  auto it = std::lower_bound(selected_units.begin(), selected_units.end(), id);
70  if(it == selected_units.end() || *it != id) {
71  selected_units.insert(it, id);
72  on_update_units(selected_units);
73  }
74  }
75 
76  inline void unselect_unit(UnitId id) {
77  std::erase(selected_units, id);
78  on_update_units(selected_units);
79  }
80 
81  inline void clear_selected_units() {
82  selected_units.clear();
83  on_update_units(selected_units);
84  }
85 
86  inline auto add_listener(std::function<void(std::vector<UnitId>)> callback) {
87  return on_update_units.append(callback);
88  }
89 
90  inline void remove_listener(eventpp::CallbackList<void (const std::vector<UnitId>)>::Handle handle) {
91  on_update_units.remove(handle);
92  }
93 };
94 
95 class Input {
96 public:
97  glm::vec2 select_pos;
98  glm::ivec2 drag_coord;
99  bool middle_mouse_down = false;
100 
101 
104 };
105 
106 class Client;
107 class Server;
108 class World;
109 class Nation;
110 struct UnitType;
111 namespace Interface {
112  class LobbySelectView;
113  class TopWindow;
114  class Minimap;
115  class ProfilerView;
116 }
117 namespace UI {
118  class Context;
119  class Widget;
120 }
121 namespace Eng3D {
122  class Texture;
123 }
124 
125 // The all encompassing client state
126 // This is the state we could pass down to all the ui widgets
127 class GameState: public Eng3D::State {
128 public:
129  GameState(const std::vector<std::string>& pkg_paths) : Eng3D::State::State(pkg_paths) {}
130  ~GameState() = default;
131 
132  void play_nation();
133  void update_on_tick();
134  void world_thread();
135  void music_enqueue();
136  void load_world_thread();
137  void handle_resize() override;
138  void handle_mouse_btn(const Eng3D::Event::MouseButton&) override;
139  void handle_mouse_motion(const Eng3D::Event::MouseMotion&) override;
140  void handle_mouse_wheel(const Eng3D::Event::MouseWheel&) override;
141  void handle_key(const Eng3D::Event::Key&) override;
142 
143  std::unique_ptr<Client> client;
144  std::unique_ptr<Server> server;
145 
146  std::atomic<bool> loaded_world;
147  std::atomic<bool> loaded_map;
148  float load_progress = 0.f;
149 
150  std::atomic<bool> update_tick;
151  std::atomic<bool> paused;
152  std::atomic<int> ms_delay_speed;
153  std::atomic<bool> quit;
154 
155  // The ui will mostly need to read the world state
156  World* world = nullptr;
157  // NationId curr_nation
158  Nation* curr_nation = nullptr;
159  std::unique_ptr<Map> map;
163 
165  UI::Widget* top_win = nullptr;
166  UI::Widget* time_win = nullptr;
167  UI::Widget* minimap = nullptr;
170  UI::Widget* unit_menu = nullptr;
171 
172  std::vector<std::shared_ptr<Eng3D::Texture>> nation_flags;
173  std::shared_ptr<Eng3D::Texture> get_nation_flag(const Nation& nation);
174 
175  // Used for synchronization between the networking client and the rendering thread
176  std::mutex render_lock;
177  std::vector<UnitTypeId> production_queue;
178 
181 
182  std::string ip_address = "127.0.0.1";
183  bool in_game = false;
184  bool motion_blur = true;
185  bool host_mode = false;
186  bool editor = false;
187  bool sea_paint = false;
188  bool singleplayer = true;
189 
190  std::vector<Eng3D::StringRef> decision_autodo;
191 };
192 
193 // Run world tick and pending commands
194 extern "C" void game_main(int argc, char** argv);
void unselect_unit(UnitId id)
Definition: game_state.hpp:76
const std::vector< UnitId > get_selected_units() const
Definition: game_state.hpp:60
void select_unit(UnitId id)
Definition: game_state.hpp:68
void remove_listener(eventpp::CallbackList< void(const std::vector< UnitId >)>::Handle handle)
Definition: game_state.hpp:90
bool is_selected_unit(UnitId id) const
Definition: game_state.hpp:64
void clear_selected_units()
Definition: game_state.hpp:81
auto add_listener(std::function< void(std::vector< UnitId >)> callback)
Definition: game_state.hpp:86
State(const std::vector< std::string > &pkg_paths)
Definition: state.cpp:318
void handle_mouse_btn(const Eng3D::Event::MouseButton &) override
Definition: game_state.cpp:184
UI::Widget * event_tray_grp
Definition: game_state.hpp:169
void handle_mouse_wheel(const Eng3D::Event::MouseWheel &) override
Definition: game_state.cpp:234
std::unique_ptr< Client > client
Definition: game_state.hpp:143
void load_world_thread()
Definition: game_state.cpp:168
void play_nation()
Definition: game_state.cpp:65
std::atomic< bool > paused
Definition: game_state.hpp:151
std::unique_ptr< Server > server
Definition: game_state.hpp:144
std::mutex render_lock
Definition: game_state.hpp:176
void handle_resize() override
Definition: game_state.cpp:180
UI::Widget * unit_menu
Definition: game_state.hpp:170
void handle_mouse_motion(const Eng3D::Event::MouseMotion &) override
Definition: game_state.cpp:222
Nation * curr_nation
Definition: game_state.hpp:158
bool host_mode
Definition: game_state.hpp:185
void handle_key(const Eng3D::Event::Key &) override
Definition: game_state.cpp:244
std::string ip_address
Definition: game_state.hpp:182
std::atomic< bool > loaded_world
Definition: game_state.hpp:146
Interface::LobbySelectView * select_nation
Definition: game_state.hpp:164
std::atomic< bool > update_tick
Definition: game_state.hpp:150
UI::Widget * time_win
Definition: game_state.hpp:166
bool in_game
Definition: game_state.hpp:183
bool motion_blur
Definition: game_state.hpp:184
std::atomic< bool > loaded_map
Definition: game_state.hpp:147
void music_enqueue()
Definition: game_state.cpp:152
~GameState()=default
UI::Widget * profiler_view
Definition: game_state.hpp:168
Input input
Definition: game_state.hpp:160
bool singleplayer
Definition: game_state.hpp:188
std::vector< std::shared_ptr< Eng3D::Texture > > nation_flags
Definition: game_state.hpp:172
bool sea_paint
Definition: game_state.hpp:187
UI::Widget * minimap
Definition: game_state.hpp:167
void world_thread()
Definition: game_state.cpp:125
float load_progress
Definition: game_state.hpp:148
void update_on_tick()
Definition: game_state.cpp:114
MapMode current_mode
Definition: game_state.hpp:162
std::unique_ptr< Map > map
Definition: game_state.hpp:159
std::vector< Eng3D::StringRef > decision_autodo
Definition: game_state.hpp:190
std::shared_ptr< Eng3D::Texture > get_nation_flag(const Nation &nation)
Definition: game_state.cpp:84
GameState(const std::vector< std::string > &pkg_paths)
Definition: game_state.hpp:129
UI::Widget * lower_left_panel
Definition: game_state.hpp:180
ClientState client_state
Definition: game_state.hpp:161
World * world
Definition: game_state.hpp:156
std::atomic< int > ms_delay_speed
Definition: game_state.hpp:152
UI::Widget * right_side_panel
Definition: game_state.hpp:179
std::vector< UnitTypeId > production_queue
Definition: game_state.hpp:177
std::atomic< bool > quit
Definition: game_state.hpp:153
UI::Widget * top_win
Definition: game_state.hpp:165
glm::vec2 select_pos
Definition: game_state.hpp:97
glm::ivec2 drag_coord
Definition: game_state.hpp:98
bool middle_mouse_down
Definition: game_state.hpp:99
Language * selected_language
Definition: game_state.hpp:102
Religion * selected_religion
Definition: game_state.hpp:103
The UI context that handles all the ui widgets.
Definition: ui.hpp:63
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
Definition: world.hpp:114
void game_main(int argc, char **argv)
MapMode
Definition: game_state.hpp:45
@ COUNTRY_SELECT
@ DISPLAY_ONLY
A military outpost, on land serves as a "spawn" place for units When adjacent to a water tile this se...
Definition: building.hpp:90
Defines a type of unit, it can be a tank, garrison, infantry, etc this is moddable via a lua script a...
Definition: unit.hpp:44