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
136} // namespace sys
137
138template<typename value_type, typename tag_type, typename allocator = std::allocator<value_type>>
140private:
141 std::vector<value_type, allocator> storage;
142
143public:
144 using public_value_type = value_type;
145 using public_tag_type = tag_type;
146
148 tagged_vector(tagged_vector<value_type, tag_type, allocator> const& other) noexcept : storage(other.storage) { }
149 tagged_vector(tagged_vector<value_type, tag_type, allocator>&& other) noexcept : storage(std::move(other.storage)) { }
151 storage.resize(size);
152 }
153
155 storage = other.storage;
156 return *this;
157 }
159 storage = std::move(other.storage);
160 return *this;
161 }
162 value_type const& operator[](tag_type t) const {
163 assert(size_t(t.index()) < storage.size());
164 return *(storage.data() + t.index());
165 }
166 value_type& operator[](tag_type t) {
167 assert(size_t(t.index()) < storage.size());
168 return *(storage.data() + t.index());
169 }
170 template<typename... T>
171 tag_type emplace_back(T&&... ts) {
172 storage.emplace_back(std::forward<T>(ts)...);
173 return tag_type(typename tag_type::value_base_t(storage.size() - 1));
174 }
175 value_type& safe_get(tag_type t) {
176 if(t.index() >= std::ssize(storage))
177 storage.resize(t.index() + 1);
178 return storage[t.index()];
179 }
180 auto data() const {
181 return storage.data();
182 }
183 auto data() {
184 return storage.data();
185 }
186 auto array() const {
187 return storage.data();
188 }
189 auto array() {
190 return storage.data();
191 }
192 auto begin() const {
193 return storage.begin();
194 }
195 auto end() const {
196 return storage.end();
197 }
198 auto begin() {
199 return storage.begin();
200 }
201 auto end() {
202 return storage.end();
203 }
204 auto size() const {
205 return storage.size();
206 }
207 auto ssize() const {
208 return std::ssize(storage);
209 }
210 void resize(size_t size) {
211 storage.resize(size);
212 }
213 void reserve(size_t size) {
214 storage.reserve(size);
215 }
216 void pop_back() {
217 storage.pop_back();
218 }
219 tag_type push_back(value_type const& v) {
220 storage.push_back(v);
221 return tag_type(typename tag_type::value_base_t(storage.size() - 1));
222 }
223 tag_type push_back(value_type&& v) {
224 storage.push_back(std::move(v));
225 return tag_type(typename tag_type::value_base_t(storage.size() - 1));
226 }
227 value_type& back() {
228 return storage.back();
229 }
230 value_type const& back() const {
231 return storage.back();
232 }
233};
234
235namespace economy {
236
238 static constexpr uint32_t set_size = 8;
239
241 dcon::commodity_id commodity_type[set_size] = {dcon::commodity_id{}};
242};
243static_assert(sizeof(commodity_set) ==
246
248 static constexpr uint32_t set_size = 6;
249
251 dcon::commodity_id commodity_type[set_size] = {dcon::commodity_id{}};
252 uint16_t padding = 0;
253};
254static_assert(sizeof(small_commodity_set) ==
258
259} // namespace economy
260
261namespace sys {
262
264 static constexpr uint32_t key_size = 64;
266
267 bool is_equal(const checksum_key& a) noexcept {
268 for(size_t i = 0; i < key_size; i++)
269 if(key[i] != a.key[i])
270 return false;
271 return true;
272 }
273
274 std::string_view to_string_view() noexcept {
275 return std::string_view{ reinterpret_cast<const char*>(&key[0]), key_size };
276 }
277 std::string to_string() noexcept {
278 return std::string(to_string_view());
279 }
280 const char* to_char() noexcept {
281 return reinterpret_cast<const char*>(&key[0]);
282 }
283};
284static_assert(sizeof(checksum_key) == sizeof(checksum_key::key));
285
286template<size_t _Size>
288 std::array<uint8_t, _Size> data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
289
290 std::string_view to_string_view() noexcept {
291 for(uint32_t i = 0; i < sizeof(data); i++) {
292 if(data[i] == ' ' || data[i] == '\0') {
293 return std::string_view{ reinterpret_cast<const char*>(&data[0]), uint32_t(i) };
294 }
295 }
296 return std::string_view{ reinterpret_cast<const char*>(&data[0]), sizeof(data) };
297 }
298
299 player_value<_Size> from_string_view(std::string_view sv) noexcept {
300 size_t length_to_copy = std::min(sv.size(), data.size());
301 sv.copy(reinterpret_cast<char*>(data.data()), length_to_copy);
302 return *this;
303 }
304
305 std::string to_string() noexcept {
306 return std::string(to_string_view());
307 }
308
309 bool empty() noexcept {
310 return data[0] == ' ' || data[0] == '\0';
311 }
312
313 void append(char c) noexcept {
314 for(uint32_t i = 0; i < sizeof(data); i++) {
315 if(data[i] == ' ' || data[i] == '\0') {
316 data[i] = c;
317 return;
318 }
319 }
320 }
321
322 char pop() noexcept {
323 for(uint32_t i = 1; i < sizeof(data); i++) {
324 if(data[i] == ' ' || data[i] == '\0') {
325 auto pop = data[i - 1];
326 data[i - 1] = ' ';
327 return pop;
328 }
329 }
330 return ' ';
331 }
332};
333
338
339static_assert(sizeof(player_name) == sizeof(player_name::data));
340static_assert(sizeof(player_password_salt) == sizeof(player_password_salt::data));
341static_assert(sizeof(player_password_hash) == sizeof(player_password_hash::data));
342static_assert(sizeof(player_password_raw) == sizeof(player_password_raw::data));
343
345 static constexpr uint32_t max_types = 48;
347 dcon::nation_id source;
348 char name[8] = { 0 };
350
352 return std::memcmp(this, &o, sizeof(*this));
353 }
354};
355
356template<typename IT, typename PTR, typename CMP>
357void merge_sort_interior(IT first, IT end, PTR buffer_start, PTR buffer_end, CMP const& cmp) noexcept {
358 auto rng_size = end - first;
359 if(rng_size == 0) {
360 return;
361 } else if(rng_size == 1) {
362 *first = *buffer_start;
363 return;
364 } else if(rng_size == 2) {
365 if(cmp(*buffer_start, *(buffer_start + 1))) {
366 *first = *buffer_start;
367 *(first + 1) = *(buffer_start + 1);
368 } else {
369 *first = *(buffer_start + 1);
370 *(first + 1) = *buffer_start;
371 }
372 return;
373 }
374
375 auto half_way = first + rng_size / 2;
376 auto b_half_way = buffer_start + rng_size / 2;
377
378 merge_sort_interior(buffer_start, b_half_way, first, half_way, cmp);
379 merge_sort_interior(b_half_way, buffer_end, half_way, end, cmp);
380
381 auto temp_index = b_half_way;
382 while(temp_index != buffer_end && buffer_start != b_half_way) {
383 if(cmp(*temp_index, *buffer_start)) {
384 *first = *temp_index;
385 ++temp_index;
386 ++first;
387 } else {
388 *first = *buffer_start;
389 ++buffer_start;
390 ++first;
391 }
392 }
393 if(temp_index == buffer_end) {
394 std::copy_n(buffer_start, (end - first), first);
395 } else if(buffer_start == b_half_way) {
396 std::copy_n(temp_index, (end - first), first);
397 }
398}
399
400template<typename IT, typename CMP>
401void merge_sort(IT first, IT end, CMP const& cmp) noexcept {
402 auto rng_size = end - first;
403 if(rng_size == 0 || rng_size == 1) {
404 return;
405 } else if(rng_size == 2) {
406 if(!cmp(*first, *(first + 1))) {
407 std::swap(*first, *(first + 1));
408 }
409 return;
410 }
411
412 using element_type = std::remove_cvref_t<decltype(*first)>;
413 auto buffer = new element_type[rng_size];
414 std::copy_n(first, rng_size, buffer);
415 merge_sort_interior(first, end, buffer, buffer + rng_size, cmp);
416 delete[] buffer;
417}
418
419struct full_wg {
420 dcon::nation_id added_by;
421 dcon::nation_id target_nation;
422 dcon::nation_id secondary_nation;
423 dcon::national_identity_id wg_tag;
424 dcon::state_definition_id state;
425 dcon::cb_type_id cb;
426};
427
429 char const* data = nullptr;
430 size_t size = 0;
431};
432
433} // 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
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.hpp:24635
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)
player_value< 24 > player_password_raw
float red_from_int(uint32_t v)
uint32_t hsv_to_rgb(hsv v)
void merge_sort(IT first, IT end, CMP const &cmp) noexcept
int32_t int_red_from_int(uint32_t v)
player_value< 24 > player_password_salt
void merge_sort_interior(IT first, IT end, PTR buffer_start, PTR buffer_end, CMP const &cmp) noexcept
int32_t int_blue_from_int(uint32_t v)
player_value< 24 > player_name
hsv rgb_to_hsv(uint32_t v)
player_value< 64 > player_password_hash
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]
std::string_view to_string_view() noexcept
uint8_t key[key_size]
static constexpr uint32_t key_size
bool is_equal(const checksum_key &a) noexcept
std::string to_string() noexcept
const char * to_char() noexcept
dcon::value_modifier_key ai_chance
dcon::text_key name
dcon::effect_key effect
dcon::nation_id target_nation
dcon::nation_id added_by
dcon::nation_id secondary_nation
dcon::cb_type_id cb
dcon::national_identity_id wg_tag
dcon::state_definition_id state
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
player_value< _Size > from_string_view(std::string_view sv) noexcept
std::string to_string() noexcept
void append(char c) noexcept
char pop() noexcept
std::string_view to_string_view() noexcept
bool empty() noexcept
std::array< uint8_t, _Size > data