Project Alice
Loading...
Searching...
No Matches
container_types.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include "dcon_generated.hpp"
5
6namespace sys {
7struct state; // this is here simply to declare the state struct in a very general location
8
9inline float red_from_int(uint32_t v) {
10 return float(v & 0xFF) / 255.0f;
11}
12inline float green_from_int(uint32_t v) {
13 return float((v >> 8) & 0xFF) / 255.0f;
14}
15inline float blue_from_int(uint32_t v) {
16 return float((v >> 16) & 0xFF) / 255.0f;
17}
18inline uint32_t pack_color(float r, float g, float b) {
19 return ((uint32_t(r * 255.0f) & 0xFF) << 0) | ((uint32_t(g * 255.0f) & 0xFF) << 8) | ((uint32_t(b * 255.0f) & 0xFF) << 16);
20}
21
22inline int32_t int_red_from_int(uint32_t v) {
23 return int32_t(v & 0xFF);
24}
25inline int32_t int_green_from_int(uint32_t v) {
26 return int32_t((v >> 8) & 0xFF);
27}
28inline int32_t int_blue_from_int(uint32_t v) {
29 return int32_t((v >> 16) & 0xFF);
30}
31inline uint32_t pack_color(int32_t r, int32_t g, int32_t b) {
32 return ((uint32_t(r) & 0xFF) << 0) | ((uint32_t(g) & 0xFF) << 8) | ((uint32_t(b) & 0xFF) << 16);
33}
34
35struct hsv {
36 float h;
37 float s;
38 float v;
39};
40
42 auto r = red_from_int(v);
43 auto g = green_from_int(v);
44 auto b = blue_from_int(v);
45
46 auto cmin = std::min(r, std::min(g, b));
47 auto cmax = std::max(r, std::max(g, b));
48 auto delta = cmax - cmin;
49
50 float h = 0.0f;
51 if(delta == 0.0f) {
52 h = 0.0f;
53 } else if(cmax == r) {
54 h = 60.0f * (fmod((g - b) / delta, 6.0f));
55 } else if(cmax == g) {
56 h = 60.0f * ((b - r) / delta + 2.0f);
57 } else /*if(cmax == b)*/ {
58 h = 60.0f * ((r - g) / delta + 4.0f);
59 }
60
61 return hsv{
62 h,
63 cmax == 0.0f ? 0.0f : delta / cmax,
64 cmax
65 };
66}
67
69 auto c = v.v * v.s;
70 auto x = c * (1.0f - std::abs(fmod(v.h / 60.0f, 2.0f) - 1.0f));
71 auto m = v.v - c;
72 float r = 0.0f; float g = 0.0f; float b = 0.0f;
73 if(0.0f <= v.h && v.h < 60.0f) {
74 r = c; g = x; b = 0.0f;
75 } else if(60.0f <= v.h && v.h < 120.0f) {
76 r = x; g = c; b = 0.0f;
77 } else if(120.0f <= v.h && v.h < 180.0f) {
78 r = 0.0f; g = c; b = x;
79 } else if(180.0f <= v.h && v.h < 240.0f) {
80 r = 0.0f; g = x; b = c;
81 } else if(240.0f <= v.h && v.h < 300.0f) {
82 r = x; g = 0.0f; b = c;
83 } else /* if(300.0f <= v.h && v.h < 360.0f) */ {
84 r = c; g = 0.0f; b = x;
85 }
86 return pack_color(
87 std::clamp(r + m, 0.0f, 255.0f),
88 std::clamp(g + m, 0.0f, 255.0f),
89 std::clamp(b + m, 0.0f, 255.0f)
90 );
91}
92
94 float factor = 0.0f;
95 dcon::trigger_key condition;
96 uint16_t padding = 0;
97};
98static_assert(sizeof(value_modifier_segment) ==
102
104 float factor = 0.0f;
105 float base = 0.0f;
107 uint16_t segments_count = 0;
108};
109static_assert(sizeof(value_modifier_description) ==
114
116 dcon::text_key name;
117 dcon::value_modifier_key ai_chance;
118 dcon::effect_key effect;
119};
120static_assert(sizeof(event_option) ==
121 sizeof(event_option::name)
123 + sizeof(event_option::effect));
124
126 using is_avalanching = void;
127
129
130 auto operator()(dcon::modifier_id m) const noexcept -> uint64_t {
131 int32_t index = m.index();
132 return ankerl::unordered_dense::hash<int32_t>()(index);
133 }
134};
135
137 dcon::nation_id target;
138 dcon::state_definition_id wargoal_state;
139 dcon::national_identity_id wargoal_tag;
141 dcon::cb_type_id wargoal_type;
142};
143static_assert(sizeof(crisis_join_offer) ==
149
150} // namespace sys
151
152template<typename value_type, typename tag_type, typename allocator = std::allocator<value_type>>
154private:
155 std::vector<value_type, allocator> storage;
156
157public:
158 using public_value_type = value_type;
159 using public_tag_type = tag_type;
160
162 tagged_vector(tagged_vector<value_type, tag_type, allocator> const& other) noexcept : storage(other.storage) { }
163 tagged_vector(tagged_vector<value_type, tag_type, allocator>&& other) noexcept : storage(std::move(other.storage)) { }
165 storage.resize(size);
166 }
167
169 storage = other.storage;
170 return *this;
171 }
173 storage = std::move(other.storage);
174 return *this;
175 }
176 value_type const& operator[](tag_type t) const {
177 assert(size_t(t.index()) < storage.size());
178 return *(storage.data() + t.index());
179 }
180 value_type& operator[](tag_type t) {
181 assert(size_t(t.index()) < storage.size());
182 return *(storage.data() + t.index());
183 }
184 template<typename... T>
185 tag_type emplace_back(T&&... ts) {
186 storage.emplace_back(std::forward<T>(ts)...);
187 return tag_type(typename tag_type::value_base_t(storage.size() - 1));
188 }
189 value_type& safe_get(tag_type t) {
190 if(t.index() >= std::ssize(storage))
191 storage.resize(t.index() + 1);
192 return storage[t.index()];
193 }
194 auto data() const {
195 return storage.data();
196 }
197 auto data() {
198 return storage.data();
199 }
200 auto array() const {
201 return storage.data();
202 }
203 auto array() {
204 return storage.data();
205 }
206 auto begin() const {
207 return storage.begin();
208 }
209 auto end() const {
210 return storage.end();
211 }
212 auto begin() {
213 return storage.begin();
214 }
215 auto end() {
216 return storage.end();
217 }
218 auto size() const {
219 return storage.size();
220 }
221 auto ssize() const {
222 return std::ssize(storage);
223 }
224 void resize(size_t size) {
225 storage.resize(size);
226 }
227 void reserve(size_t size) {
228 storage.reserve(size);
229 }
230 void pop_back() {
231 storage.pop_back();
232 }
233 tag_type push_back(value_type const& v) {
234 storage.push_back(v);
235 return tag_type(typename tag_type::value_base_t(storage.size() - 1));
236 }
237 tag_type push_back(value_type&& v) {
238 storage.push_back(std::move(v));
239 return tag_type(typename tag_type::value_base_t(storage.size() - 1));
240 }
241 value_type& back() {
242 return storage.back();
243 }
244 value_type const& back() const {
245 return storage.back();
246 }
247};
248
249namespace economy {
250
252 static constexpr uint32_t set_size = 8;
253
255 dcon::commodity_id commodity_type[set_size] = {dcon::commodity_id{}};
256};
257static_assert(sizeof(commodity_set) ==
260
262 static constexpr uint32_t set_size = 6;
263
265 dcon::commodity_id commodity_type[set_size] = {dcon::commodity_id{}};
266 uint16_t padding = 0;
267};
268static_assert(sizeof(small_commodity_set) ==
272
273} // namespace economy
274
275namespace sys {
276
278 static constexpr uint32_t key_size = 64;
280
281 bool is_equal(const checksum_key& a) noexcept {
282 for(size_t i = 0; i < key_size; i++)
283 if(key[i] != a.key[i])
284 return false;
285 return true;
286 }
287};
288static_assert(sizeof(checksum_key) == sizeof(checksum_key::key));
289
291 char data[48] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
292
293 std::string_view to_string_view() noexcept {
294 for(uint32_t i = 0; i < sizeof(data); i++) {
295 if(data[i] == ' ' || data[i] == '\0') {
296 return std::string_view{ reinterpret_cast<const char*>(&data[0]), uint32_t(i) };
297 }
298 }
299 return "???";
300 }
301};
302static_assert(sizeof(player_name) == sizeof(player_name::data));
303
305 static constexpr uint32_t max_types = 48;
307 dcon::nation_id source;
308 char name[8] = { 0 };
310
312 return std::memcmp(this, &o, sizeof(*this));
313 }
314};
315
316} // namespace sys
value_type & operator[](tag_type t)
tagged_vector(tagged_vector< value_type, tag_type, allocator > &&other) noexcept
value_type const & operator[](tag_type t) const
auto array() const
tagged_vector & operator=(tagged_vector< value_type, tag_type, allocator > &&other) noexcept
auto begin() const
tagged_vector(tagged_vector< value_type, tag_type, allocator > const &other) noexcept
tagged_vector(size_t size)
tag_type push_back(value_type &&v)
auto end() const
tag_type push_back(value_type const &v)
void reserve(size_t size)
value_type & back()
auto data() const
value_type const & back() const
auto ssize() const
tag_type emplace_back(T &&... ts)
value_type public_value_type
auto size() const
value_type & safe_get(tag_type t)
tagged_vector & operator=(tagged_vector< value_type, tag_type, allocator > const &other) noexcept
tag_type public_tag_type
void resize(size_t size)
#define assert(condition)
Definition: debug.h:74
Definition: constants.hpp:4
float blue_from_int(uint32_t v)
float green_from_int(uint32_t v)
uint32_t pack_color(float r, float g, float b)
float red_from_int(uint32_t v)
uint32_t hsv_to_rgb(hsv v)
int32_t int_red_from_int(uint32_t v)
int32_t int_blue_from_int(uint32_t v)
hsv rgb_to_hsv(uint32_t v)
int32_t int_green_from_int(uint32_t v)
uint uint32_t
ulong uint64_t
uchar uint8_t
float commodity_amounts[set_size]
dcon::commodity_id commodity_type[set_size]
static constexpr uint32_t set_size
static constexpr uint32_t set_size
dcon::commodity_id commodity_type[set_size]
uint8_t key[key_size]
static constexpr uint32_t key_size
bool is_equal(const checksum_key &a) noexcept
dcon::national_identity_id wargoal_tag
dcon::state_definition_id wargoal_state
dcon::nation_id wargoal_secondary_nation
dcon::cb_type_id wargoal_type
dcon::value_modifier_key ai_chance
dcon::text_key name
dcon::effect_key effect
bool operator!=(macro_builder_template &o)
sys::checksum_key scenario_checksum
static constexpr uint32_t max_types
auto operator()(dcon::modifier_id m) const noexcept -> uint64_t
std::string_view to_string_view() noexcept