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 // color -> province map
79 std::map<entt::entity, std::map<int, entt::entity>> province_colors;
80 // province -> color
81 std::map<entt::entity, std::map<entt::entity, int>> colors_province;
82 entt::entity sun;
83
84 std::vector<entt::entity> good_vector;
85 std::unordered_map<entt::entity, components::GoodEntity> good_map;
86
87 entt::entity GetGood(const components::GoodEntity entity) const { return good_vector[static_cast<int>(entity)]; }
88
89 auto GoodIterator() const {
90 return (std::views::iota(0, static_cast<int>(good_vector.size())) |
91 std::ranges::views::transform(
92 [](int in) -> components::GoodEntity { return static_cast<components::GoodEntity>(in); }));
93 }
94
95 size_t GoodCount() const { return good_vector.size(); }
96
97 using entt::registry::all_of;
98 using entt::registry::any_of;
99 using entt::registry::get;
100
101 template <typename... Component>
102 [[nodiscard]] bool all_of(const components::GoodEntity entity) const {
103 return entt::registry::all_of<Component...>(GetGood(entity));
104 }
105
106 template <typename... Component>
107 [[nodiscard]] bool any_of(const components::GoodEntity entity) const {
108 return entt::registry::any_of<Component...>(GetGood(entity));
109 }
110
111 template <typename... Component>
112 [[nodiscard]] decltype(auto) get([[maybe_unused]] const components::GoodEntity entity) {
113 return entt::registry::get<Component...>(GetGood(entity));
114 }
115
116 template <typename... Component>
117 [[nodiscard]] decltype(auto) get([[maybe_unused]] const components::GoodEntity entity) const {
118 return entt::registry::get<const Component...>(GetGood(entity));
119 }
120
121 void EnableTick() { to_tick = true; }
122 void DisableTick() { to_tick = false; }
123 bool ToTick() const { return to_tick; }
125
126 int GetDate() const { return static_cast<int>(date.GetDate()); }
127 std::unique_ptr<cqsp::core::util::IRandom> random;
128 std::string uuid;
129
133 double tick_fraction = 0;
134 std::function<Node(entt::entity)> nodeFactory;
135 auto NodeTransform() const { return std::views::transform(nodeFactory); }
136 auto Convert(const std::vector<entt::entity>& entities) const {
137 return (entities | std::ranges::views::transform([this](entt::entity entity) { return Node(*this, entity); }));
138 }
139
140 std::set<Node> Convert(const std::set<entt::entity>& entities) const;
141
142 template <typename... Components>
143 auto nodes() {
144 return this->template view<Components...>() | NodeTransform();
145 }
146 template <typename... Components>
147 auto nodes() const {
148 return this->template view<Components...>() | NodeTransform();
149 }
150 template <typename... Components, typename... Exclude>
151 auto nodes(entt::exclude_t<Exclude...> exclude) {
152 return this->template view<Components...>(exclude) | NodeTransform();
153 }
154 template <typename... Components, typename... Exclude>
155 auto nodes(entt::exclude_t<Exclude...> exclude) const {
156 return this->template view<Components...>(exclude) | NodeTransform();
157 }
158
162 Node operator()(const entt::entity entity) { return Node(*this, entity); }
163
167 Node operator()() { return Node(*this, create()); }
168
172 Node null() { return Node(*this, entt::null); }
173
174 systems::EconomyConfig economy_config;
175
176 private:
177 bool to_tick = false;
178};
179} // namespace cqsp::core
180
181template <>
182struct fmt::formatter<entt::entity> : formatter<std::string> {
183 template <typename FormatContext>
184 constexpr auto format(entt::entity entity, FormatContext& ctx) const {
185 return formatter<std::string>::format(std::to_string((uint64_t)entity), ctx);
186 }
187};
188
189template <>
190struct fmt::formatter<cqsp::core::Node> : fmt::formatter<std::string> {
191 template <typename FormatContext>
192 auto format(const cqsp::core::Node ship, FormatContext& ctx) const {
193 return fmt::formatter<entt::entity> {}.format(ship.entity(), ctx);
194 }
195};
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:45
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:42
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:121
std::vector< entt::entity > consumergoods
Definition: universe.h:67
auto nodes() const
Definition: universe.h:147
std::unique_ptr< cqsp::core::util::IRandom > random
Definition: universe.h:127
int GetDate() const
Definition: universe.h:126
size_t GoodCount() const
Definition: universe.h:95
std::function< Node(entt::entity)> nodeFactory
Definition: universe.h:134
auto Convert(const std::vector< entt::entity > &entities) const
Definition: universe.h:136
std::string uuid
Definition: universe.h:128
std::map< std::string, entt::entity > terrain_data
Definition: universe.h:69
auto nodes()
Definition: universe.h:143
auto nodes(entt::exclude_t< Exclude... > exclude)
Definition: universe.h:151
bool any_of(const components::GoodEntity entity) const
Definition: universe.h:107
bool all_of(const components::GoodEntity entity) const
Definition: universe.h:102
void ToggleTick()
Definition: universe.h:124
systems::EconomyConfig economy_config
Definition: universe.h:174
std::map< std::string, entt::entity > fields
Definition: universe.h:71
void DisableTick()
Definition: universe.h:122
Node operator()(const entt::entity entity)
Definition: universe.h:162
Node operator()()
Definition: universe.h:167
bool to_tick
Definition: universe.h:177
std::unordered_map< entt::entity, components::GoodEntity > good_map
Definition: universe.h:85
auto nodes(entt::exclude_t< Exclude... > exclude) const
Definition: universe.h:155
decltype(auto) get(const components::GoodEntity entity)
Definition: universe.h:112
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:81
std::map< std::string, actions::NameGenerator > name_generators
Definition: universe.h:70
bool ToTick() const
Definition: universe.h:123
std::map< std::string, entt::entity > countries
Definition: universe.h:75
std::vector< entt::entity > good_vector
Definition: universe.h:84
decltype(auto) get(const components::GoodEntity entity) const
Definition: universe.h:117
double tick_fraction
What is the current fraction of the wait of the tick we are processing
Definition: universe.h:133
auto GoodIterator() const
Definition: universe.h:89
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:79
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:87
auto NodeTransform() const
Definition: universe.h:135
components::StarDate date
Definition: universe.h:64
std::map< std::string, entt::entity > technologies
Definition: universe.h:72
Universe()
Definition: universe.cpp:27
entt::entity sun
Definition: universe.h:82
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:40
GoodEntity
Definition: resourceledger.h:28
Definition: cityactions.cpp:23
Definition: universe.h:190
auto format(const cqsp::core::Node ship, FormatContext &ctx) const
Definition: universe.h:192
Definition: universe.h:182
constexpr auto format(entt::entity entity, FormatContext &ctx) const
Definition: universe.h:184