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.
bodies.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 <limits>
20#include <map>
21#include <ranges>
22#include <string>
23#include <tuple>
24#include <vector>
25
26#include <entt/entt.hpp>
27
29
31struct Body {
32 /*
33 * Radius of the body
34 */
36
41 types::kilometer SOI = std::numeric_limits<double>::infinity();
43
44 // gravitational constant in km^3 * s^-2
45 double GM = 1;
46
47 // Rotation period in seconds
48 double rotation = 1;
49
50 // Axial rotation
51 double axial = 0.0;
52
53 double rotation_offset = 0.0;
54};
55
64inline double CalculateSOI(const double& mass, const double& reference_mass, const double& sma) {
65 return sma * std::pow(mass / reference_mass, 0.4);
66}
67
74inline double CalculateMass(const double& GM) { return GM / types::G_km; }
75
83inline double GetPlanetRotationAngle(const double& time, const double& day_length, const double& offset) {
84 return (time / day_length - offset) * types::TWOPI;
85}
86
88 std::string terrain_name;
89 std::string normal_name;
90 std::string roughness_name;
91};
92
93struct NautralObject {};
94
95// For convenience, create a type-function that returns the first type of a parameter pack.
96template <typename T, typename... Rest>
98 using Type = T;
99};
100template <typename... Pack>
101using GetFirstType = GetFirstTypeT<Pack...>::Type;
102
103// The `ExpandedIter` type will be recursively instantiated to implement the `ConcatIter` for a
104// concatenated view.
105template <typename... Ranges>
107
108template <typename R, typename... Ranges>
109struct ExpandedIter<R, Ranges...> {
110 using EltT = std::ranges::range_value_t<R>;
111 using IterT = std::ranges::iterator_t<R>;
112 R& r;
115 ExpandedIter(R& r, Ranges&... rest) : r(r), rest_iter(rest...) { iter = r.begin(); }
116 bool is_end() const {
117 if (iter == r.end()) {
118 return rest_iter.is_end();
119 } else {
120 return false;
121 }
122 }
123 auto current() {
124 if constexpr (sizeof...(Ranges) != 0) {
125 if (iter == r.end()) {
126 return rest_iter.current();
127 }
128 }
129 return *iter;
130 }
131 void next() {
132 if (iter == r.end()) {
133 rest_iter.next();
134 } else {
135 ++iter;
136 }
137 }
138};
139
140template <>
141struct ExpandedIter<> {
142 bool is_end() const { return true; }
143 void next() {}
144};
145
146template <typename... Views>
149
150 public:
151 using EltT = std::ranges::range_value_t<GetFirstType<Views...>>;
152
153 ConcatIter(Views&... views) : exp_iter(views...) {}
154 ConcatIter(const ConcatIter&) = delete;
155 ConcatIter(ConcatIter&& other) = default;
156 ConcatIter& operator=(const ConcatIter&) = delete;
158
159 bool is_end() const { return exp_iter.is_end(); }
160 EltT operator*() { return exp_iter.current(); }
162 exp_iter.next();
163 return *this;
164 }
165
166 private:
167};
168
169struct EndIter {};
170
171template <typename... Rs>
173 return iter.is_end();
174}
175
176template <typename... Ranges>
179 std::tuple<decltype(std::views::all(std::forward<Ranges>(std::declval<Ranges&>())))...> views;
180
181 public:
182 ConcatView(Ranges&&... ranges) : views(std::views::all(std::forward<Ranges>(ranges))...) {}
183
184 IterT begin() { return std::make_from_tuple<IterT>(views); }
185 EndIter end() { return {}; };
186
187 private:
188};
189
190// Concatenate ranges with same (or compatible?) element type.
191template <typename... Rs>
192 requires(std::ranges::range<Rs> && ...)
193ConcatView<Rs...> concat(Rs&&... rs) {
194 ConcatView<Rs...> v(std::forward<Rs>(rs)...);
195 return v;
196}
197
202 // Set the tree
203 // TODO(EhWhoAmI): Large optimizations to be gained by separating bodies with a SOI to bodies without.
204 // We can have one iterator that interacts with both
205 std::vector<entt::entity> children;
206 // Large bodies such as moons or planets
207 std::vector<entt::entity> bodies;
208 void push_back(const entt::entity entity) { children.push_back(entity); }
209 auto all() { return concat(bodies, children); }
210};
211
212struct DirtyOrbit {};
213
214struct Star {};
215
216struct Planet {};
217
218struct LightEmitter {};
219
222};
223} // namespace cqsp::core::components::bodies
ConcatIter & operator=(ConcatIter &&)=delete
bool is_end() const
Definition: bodies.h:159
ConcatIter & operator++()
Definition: bodies.h:161
ConcatIter(const ConcatIter &)=delete
EltT operator*()
Definition: bodies.h:160
ConcatIter(ConcatIter &&other)=default
ConcatIter & operator=(const ConcatIter &)=delete
std::ranges::range_value_t< GetFirstType< Views... > > EltT
Definition: bodies.h:151
ExpandedIter< Views... > exp_iter
Definition: bodies.h:148
ConcatIter(Views &... views)
Definition: bodies.h:153
IterT begin()
Definition: bodies.h:184
ConcatView(Ranges &&... ranges)
Definition: bodies.h:182
EndIter end()
Definition: bodies.h:185
std::tuple< decltype(std::views::all(std::forward< Ranges >(std::declval< Ranges & >())))... > views
Definition: bodies.h:179
Definition: bodies.h:30
GetFirstTypeT< Pack... >::Type GetFirstType
Definition: bodies.h:101
double CalculateSOI(const double &mass, const double &reference_mass, const double &sma)
Calculates SOI
Definition: bodies.h:64
double GetPlanetRotationAngle(const double &time, const double &day_length, const double &offset)
Calculates the current planet rotation angle
Definition: bodies.h:83
bool operator==(const ConcatIter< Rs... > &iter, EndIter)
Definition: bodies.h:172
double CalculateMass(const double &GM)
Calculates mass from gravitational constant
Definition: bodies.h:74
return v
Definition: bodies.h:193
constexpr double G_km
Definition: units.h:55
double kilogram
Definition: units.h:40
constexpr double TWOPI
Definition: units.h:47
double kilometer
Definition: units.h:35
double scale_height
Definition: bodies.h:221
double rotation_offset
Definition: bodies.h:53
types::kilogram mass
Definition: bodies.h:42
double axial
Definition: bodies.h:51
types::kilometer radius
Definition: bodies.h:35
double rotation
Definition: bodies.h:48
types::kilometer SOI
Radius of sphere of influence rsoi = a(m/M)^2/5
Definition: bodies.h:41
double GM
Definition: bodies.h:45
std::ranges::iterator_t< R > IterT
Definition: bodies.h:111
ExpandedIter(R &r, Ranges &... rest)
Definition: bodies.h:115
ExpandedIter< Ranges... > rest_iter
Definition: bodies.h:114
std::ranges::range_value_t< R > EltT
Definition: bodies.h:110
bool is_end() const
Definition: bodies.h:142
void next()
Definition: bodies.h:143
An object for the children of an orbital object.
Definition: bodies.h:201
std::vector< entt::entity > bodies
Definition: bodies.h:207
std::vector< entt::entity > children
Definition: bodies.h:205
void push_back(const entt::entity entity)
Definition: bodies.h:208
auto all()
Definition: bodies.h:209
Definition: bodies.h:214
std::string normal_name
Definition: bodies.h:89
std::string terrain_name
Definition: bodies.h:88
std::string roughness_name
Definition: bodies.h:90