Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
universe.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
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#pragma once
18
19#include <fmt/format.h>
20
21#include <map>
22#include <memory>
23#include <ranges>
24#include <string>
25#include <unordered_map>
26#include <vector>
27
28#include <entt/entt.hpp>
29
36
37namespace cqsp::core {
38class Universe;
39class Node : public entt::handle {
40 public:
41 explicit Node(const Universe& universe, const entt::entity entity);
42 Node(const entt::handle handle, const entt::entity entity);
43 explicit Node(Universe& universe);
44 Universe& universe() const;
45 auto Convert(const std::vector<entt::entity>& entities) const {
46 return (entities |
47 std::ranges::views::transform([this](entt::entity entity) { return Node(universe(), entity); }));
48 }
49 std::set<Node> Convert(const std::set<entt::entity>& entities) const;
50 Node Convert(const entt::entity entity) const;
51
52 // Overload equivalence against entt::null_t
53 friend bool operator==(const Node& lhs, const entt::null_t&) { return lhs.entity() == entt::null; }
54 friend bool operator==(const entt::null_t&, const Node& rhs) { return rhs.entity() == entt::null; }
55 friend bool operator!=(const Node& lhs, const entt::null_t&) { return lhs.entity() != entt::null; }
56 friend bool operator!=(const entt::null_t&, const Node& rhs) { return rhs.entity() != entt::null; }
57};
58
59class Universe : public entt::registry {
60 public:
61 explicit Universe(std::string uuid);
62 Universe();
63
65
66 std::map<std::string, entt::entity> goods;
67 std::vector<entt::entity> consumergoods;
68 std::map<std::string, entt::entity> recipes;
69 std::map<std::string, entt::entity> terrain_data;
70 std::map<std::string, actions::NameGenerator> name_generators;
71 std::map<std::string, entt::entity> fields;
72 std::map<std::string, entt::entity> technologies;
73 std::map<std::string, entt::entity> planets;
74 std::map<std::string, entt::entity> time_zones;
75 std::map<std::string, entt::entity> countries;
76 std::map<std::string, entt::entity> provinces;
77 std::map<std::string, entt::entity> cities;
78 std::map<std::string, entt::entity> jobs;
79 std::map<std::string, entt::entity> zoning;
80 std::map<std::string, entt::entity> modifiers;
81 std::map<std::string, entt::entity> infrastructure;
82
83 // color -> province map
84 std::map<entt::entity, std::map<int, entt::entity>> province_colors;
85 // province -> color
86 std::map<entt::entity, std::map<entt::entity, int>> colors_province;
87 entt::entity sun;
88
92 std::vector<entt::entity> good_vector;
96 std::unordered_map<entt::entity, double> good_prices;
100 std::unordered_map<entt::entity, components::GoodEntity> good_map;
101
102 entt::entity default_job = entt::null;
103
104 entt::entity GetGood(const components::GoodEntity entity) const { return good_vector[static_cast<int>(entity)]; }
105
106 auto GoodIterator() const {
107 return (std::views::iota(0, static_cast<int>(good_vector.size())) |
108 std::ranges::views::transform(
109 [](int in) -> components::GoodEntity { return static_cast<components::GoodEntity>(in); }));
110 }
111
112 size_t GoodCount() const { return good_vector.size(); }
113
114 using entt::registry::all_of;
115 using entt::registry::any_of;
116 using entt::registry::get;
117
118 template <typename... Component>
119 [[nodiscard]] bool all_of(const components::GoodEntity entity) const {
120 return entt::registry::all_of<Component...>(GetGood(entity));
121 }
122
123 template <typename... Component>
124 [[nodiscard]] bool any_of(const components::GoodEntity entity) const {
125 return entt::registry::any_of<Component...>(GetGood(entity));
126 }
127
128 template <typename... Component>
129 [[nodiscard]] decltype(auto) get([[maybe_unused]] const components::GoodEntity entity) {
130 return entt::registry::get<Component...>(GetGood(entity));
131 }
132
133 template <typename... Component>
134 [[nodiscard]] decltype(auto) get([[maybe_unused]] const components::GoodEntity entity) const {
135 return entt::registry::get<const Component...>(GetGood(entity));
136 }
137
138 void EnableTick() { to_tick = true; }
139 void DisableTick() { to_tick = false; }
140 bool ToTick() const { return to_tick; }
142
143 int GetDate() const { return static_cast<int>(date.GetDate()); }
144 std::unique_ptr<cqsp::core::util::IRandom> random;
145 std::string uuid;
146
150 double tick_fraction = 0;
151 std::function<Node(entt::entity)> nodeFactory;
152 auto NodeTransform() const { return std::views::transform(nodeFactory); }
153 auto Convert(const std::vector<entt::entity>& entities) const {
154 return (entities | std::ranges::views::transform([this](entt::entity entity) { return Node(*this, entity); }));
155 }
156
157 std::set<Node> Convert(const std::set<entt::entity>& entities) const;
158
159 template <typename... Components>
160 auto nodes() {
161 return this->template view<Components...>() | NodeTransform();
162 }
163 template <typename... Components>
164 auto nodes() const {
165 return this->template view<Components...>() | NodeTransform();
166 }
167 template <typename... Components, typename... Exclude>
168 auto nodes(entt::exclude_t<Exclude...> exclude) {
169 return this->template view<Components...>(exclude) | NodeTransform();
170 }
171 template <typename... Components, typename... Exclude>
172 auto nodes(entt::exclude_t<Exclude...> exclude) const {
173 return this->template view<Components...>(exclude) | NodeTransform();
174 }
175
179 Node operator()(const entt::entity entity) { return Node(*this, entity); }
180
184 Node operator()() { return Node(*this, create()); }
185
189 Node null() { return Node(*this, entt::null); }
190
191 systems::EconomyConfig economy_config;
192
193 entt::entity GetPlayer() const;
194
195 entt::dispatcher event_dispatcher;
196
197 private:
198 bool to_tick = false;
199};
200} // namespace cqsp::core
201
202template <>
203struct fmt::formatter<entt::entity> : formatter<std::string> {
204 template <typename FormatContext>
205 constexpr auto format(entt::entity entity, FormatContext& ctx) const {
206 return formatter<std::string>::format(std::to_string((uint64_t)entity), ctx);
207 }
208};
209
210template <>
211struct fmt::formatter<cqsp::core::Node> : fmt::formatter<std::string> {
212 template <typename FormatContext>
213 auto format(const cqsp::core::Node ship, FormatContext& ctx) const {
214 return fmt::formatter<entt::entity> {}.format(ship.entity(), ctx);
215 }
216};
Definition: universe.h:39
auto Convert(const std::vector< entt::entity > &entities) const
Definition: universe.h:45
friend bool operator!=(const entt::null_t &, const Node &rhs)
Definition: universe.h:56
Universe & universe() const
Definition: universe.cpp:48
friend bool operator==(const entt::null_t &, const Node &rhs)
Definition: universe.h:54
friend bool operator==(const Node &lhs, const entt::null_t &)
Definition: universe.h:53
Node(const Universe &universe, const entt::entity entity)
Definition: universe.cpp:45
friend bool operator!=(const Node &lhs, const entt::null_t &)
Definition: universe.h:55
Definition: universe.h:59
std::map< std::string, entt::entity > provinces
Definition: universe.h:76
void EnableTick()
Definition: universe.h:138
std::vector< entt::entity > consumergoods
Definition: universe.h:67
auto nodes() const
Definition: universe.h:164
std::unique_ptr< cqsp::core::util::IRandom > random
Definition: universe.h:144
std::map< std::string, entt::entity > modifiers
Definition: universe.h:80
int GetDate() const
Definition: universe.h:143
size_t GoodCount() const
Definition: universe.h:112
std::function< Node(entt::entity)> nodeFactory
Definition: universe.h:151
auto Convert(const std::vector< entt::entity > &entities) const
Definition: universe.h:153
std::string uuid
Definition: universe.h:145
std::map< std::string, entt::entity > terrain_data
Definition: universe.h:69
auto nodes()
Definition: universe.h:160
auto nodes(entt::exclude_t< Exclude... > exclude)
Definition: universe.h:168
bool any_of(const components::GoodEntity entity) const
Definition: universe.h:124
entt::entity GetPlayer() const
Definition: universe.cpp:43
bool all_of(const components::GoodEntity entity) const
Definition: universe.h:119
entt::entity default_job
Definition: universe.h:102
void ToggleTick()
Definition: universe.h:141
systems::EconomyConfig economy_config
Definition: universe.h:191
std::map< std::string, entt::entity > jobs
Definition: universe.h:78
std::map< std::string, entt::entity > fields
Definition: universe.h:71
void DisableTick()
Definition: universe.h:139
Node operator()(const entt::entity entity)
Definition: universe.h:179
Node operator()()
Definition: universe.h:184
std::map< std::string, entt::entity > infrastructure
Definition: universe.h:81
bool to_tick
Definition: universe.h:198
std::unordered_map< entt::entity, components::GoodEntity > good_map
Definition: universe.h:100
auto nodes(entt::exclude_t< Exclude... > exclude) const
Definition: universe.h:172
decltype(auto) get(const components::GoodEntity entity)
Definition: universe.h:129
std::map< std::string, entt::entity > goods
Definition: universe.h:66
std::map< std::string, entt::entity > planets
Definition: universe.h:73
std::map< entt::entity, std::map< entt::entity, int > > colors_province
Definition: universe.h:86
std::map< std::string, actions::NameGenerator > name_generators
Definition: universe.h:70
bool ToTick() const
Definition: universe.h:140
std::map< std::string, entt::entity > countries
Definition: universe.h:75
std::vector< entt::entity > good_vector
Definition: universe.h:92
decltype(auto) get(const components::GoodEntity entity) const
Definition: universe.h:134
double tick_fraction
What is the current fraction of the wait of the tick we are processing
Definition: universe.h:150
auto GoodIterator() const
Definition: universe.h:106
std::map< std::string, entt::entity > recipes
Definition: universe.h:68
std::map< entt::entity, std::map< int, entt::entity > > province_colors
Definition: universe.h:84
std::map< std::string, entt::entity > cities
Definition: universe.h:77
std::map< std::string, entt::entity > time_zones
Definition: universe.h:74
entt::entity GetGood(const components::GoodEntity entity) const
Definition: universe.h:104
auto NodeTransform() const
Definition: universe.h:152
entt::dispatcher event_dispatcher
Definition: universe.h:195
std::map< std::string, entt::entity > zoning
Definition: universe.h:79
std::unordered_map< entt::entity, double > good_prices
Definition: universe.h:96
components::StarDate date
Definition: universe.h:64
std::map< std::string, entt::entity > technologies
Definition: universe.h:72
Universe()
Definition: universe.cpp:28
entt::entity sun
Definition: universe.h:87
Holds and calculates the tick that is on going. Date is the number of hours that has been ongoing sin...
Definition: stardate.h:46
uint64_t GetDate() const
Definition: stardate.h:61
std::vector< std::string > entities
Definition: loadcities.cpp:42
GoodEntity
Definition: resourceledger.h:28
Definition: cityactions.cpp:23
Definition: universe.h:211
auto format(const cqsp::core::Node ship, FormatContext &ctx) const
Definition: universe.h:213
Definition: universe.h:203
constexpr auto format(entt::entity entity, FormatContext &ctx) const
Definition: universe.h:205