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.
resourceledger.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 <cstdint>
20#include <map>
21#include <string>
22#include <vector>
23
24namespace cqsp::core::components {
28enum class GoodEntity : uint32_t { null = static_cast<uint32_t>(-1) };
29
30// NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
31constexpr GoodEntity ToGoodEntity(uint32_t val) { return static_cast<GoodEntity>(val); }
32class ResourceLedger;
33class ResourceVector;
34
35typedef std::map<GoodEntity, double> LedgerMap;
36
38class /* [[deprecated]] */ ResourceMap : private LedgerMap {
39 public:
40 ResourceMap() = default;
41 ~ResourceMap() = default;
42
43 double operator[](const GoodEntity) const;
44
50 bool EnoughToTransfer(const ResourceMap& amount);
51
52 void operator-=(const ResourceMap&);
53 void operator+=(const ResourceMap&);
54 void operator*=(const ResourceMap&);
55 void operator/=(const ResourceMap&);
56
61
62 void operator-=(const ResourceVector&);
63 void operator+=(const ResourceVector&);
64 void operator*=(const ResourceVector&);
65 void operator/=(const ResourceVector&);
66
67 void operator-=(const double value);
68 void operator+=(const double value);
69 void operator*=(const double value);
70 void operator/=(const double value);
71
72 ResourceMap operator-(const ResourceMap&) const;
73 ResourceMap operator+(const ResourceMap&) const;
74 ResourceMap operator*(const ResourceMap&) const;
75 ResourceMap operator/(const ResourceMap&) const;
76 ResourceMap operator-(const double value) const;
77 ResourceMap operator+(const double value) const;
78 ResourceMap operator*(const double value) const;
79 ResourceMap operator/(const double value) const;
80
84 bool operator<(const ResourceMap&);
85
89 bool operator>(const ResourceMap&);
90
94 bool operator<=(const ResourceMap&);
95
99 bool operator>=(const ResourceMap&);
100
101 bool LedgerEquals(const ResourceMap&);
102
106 bool operator>(const double&);
107
111 bool operator<(const double&);
112
113 bool operator==(const double&);
114
115 bool operator<=(const double&);
116 bool operator>=(const double&);
117
118 void AssignFrom(const ResourceMap&);
119
120 void TransferTo(ResourceMap&, const ResourceMap&);
121 // Equivalant to this += other * double
122 void MultiplyAdd(const ResourceMap&, double);
123 void MultiplyAdd(const ResourceVector&, double);
124
131
137
141 ResourceMap UnitLedger(const double);
142
143 // Add all the positive values in the other ledger to this ledger
144 // Essentially this += other (if idx > 0)
145 void AddPositive(const ResourceMap&);
146
147 // Add all the negative values in the other ledger to this ledger
148 // Essentially this += abs(other) (if idx < 0)
149 void AddNegative(const ResourceMap&);
150
154 ResourceMap Clamp(const double, const double);
155
160
164 ResourceMap SafeDivision(const ResourceMap&, double);
165
170 double Average();
171
172 double Min();
173 double Max();
174
180 bool HasAllResources(const ResourceMap&);
181
182 bool HasGood(GoodEntity good) { return (*this).find(good) != (*this).end(); }
183
187 double GetSum();
188
195 double MultiplyAndGetSum(ResourceMap& other);
196
197 std::string to_string();
198
199 // All the things that we get from map
200 using LedgerMap::operator[];
201 using LedgerMap::operator=;
202 using LedgerMap::begin;
203 using LedgerMap::cbegin;
204 using LedgerMap::cend;
205 using LedgerMap::clear;
206 using LedgerMap::contains;
207 using LedgerMap::crbegin;
208 using LedgerMap::crend;
209 using LedgerMap::emplace;
210 using LedgerMap::empty;
211 using LedgerMap::end;
212 using LedgerMap::erase;
213 using LedgerMap::mapped_type;
214 using LedgerMap::rbegin;
215 using LedgerMap::rend;
216 using LedgerMap::size;
217 using LedgerMap::value_comp;
218};
219
223class ResourceVector : public std::vector<std::pair<GoodEntity, double>> {
224 typedef std::pair<GoodEntity, double> LedgerPair;
225 typedef std::vector<LedgerPair> LedgerVector;
226
227 public:
228 ResourceVector() = default;
229 ~ResourceVector() = default;
230
231 void operator-=(const ResourceMap&);
232 void operator+=(const ResourceMap&);
233 void operator*=(const ResourceMap&);
234 void operator/=(const ResourceMap&);
235
236 void operator-=(const ResourceLedger&);
237 void operator+=(const ResourceLedger&);
238 void operator*=(const ResourceLedger&);
239 void operator/=(const ResourceLedger&);
240
241 void operator-=(const double value);
242 void operator+=(const double value);
243 void operator*=(const double value);
244 void operator/=(const double value);
245
246 ResourceVector operator-(const double value) const;
247 ResourceVector operator+(const double value) const;
248 ResourceVector operator*(const double value) const;
249 ResourceVector operator/(const double value) const;
250
251 ResourceVector operator+(const ResourceVector& other) const;
252 ResourceVector operator*(const ResourceVector& other) const;
253
258 double Average();
259
260 double Min();
261 double Max();
264
265 void Finalize();
266
267 bool HasGood(const GoodEntity& good) const;
268
269 double operator[](const GoodEntity& good) const;
270
274 double GetSum();
275
282 double MultiplyAndGetSum(const ResourceLedger& other) const;
283
284 bool contains(const GoodEntity& entity) const;
285
286 // All the things that we get from map
287 using LedgerVector::operator=;
288 using LedgerVector::begin;
289 using LedgerVector::cbegin;
290 using LedgerVector::cend;
291 using LedgerVector::clear;
292 using LedgerVector::crbegin;
293 using LedgerVector::crend;
294 using LedgerVector::emplace;
295 using LedgerVector::empty;
296 using LedgerVector::end;
297 using LedgerVector::erase;
298 using LedgerVector::rbegin;
299 using LedgerVector::rend;
300 using LedgerVector::size;
301};
302
304 private:
305 std::vector<double> ledger;
306
307 public:
308 explicit ResourceLedger(size_t count);
309
310 operator ResourceMap() const;
311
312 double operator[](const GoodEntity value) const;
313 double& operator[](const GoodEntity value);
314
320 bool EnoughToTransfer(const ResourceLedger& amount);
321
322 void operator-=(const ResourceLedger&);
323 void operator+=(const ResourceLedger&);
324 void operator*=(const ResourceLedger&);
325 void operator/=(const ResourceLedger&);
326
327 void operator-=(const ResourceMap&);
328 void operator+=(const ResourceMap&);
329 void operator*=(const ResourceMap&);
330 void operator/=(const ResourceMap&);
331
332 void operator-=(const ResourceVector&);
333 void operator+=(const ResourceVector&);
334 void operator*=(const ResourceVector&);
335 void operator/=(const ResourceVector&);
336
337 void operator-=(const double value);
338 void operator+=(const double value);
339 void operator*=(const double value);
340 void operator/=(const double value);
341
346
351
352 ResourceLedger operator-(const double value) const;
353 ResourceLedger operator+(const double value) const;
354 ResourceLedger operator*(const double value) const;
355 ResourceLedger operator/(const double value) const;
356
360 bool operator<(const ResourceLedger&);
361
365 bool operator>(const ResourceLedger&);
366
370 bool operator<=(const ResourceLedger&);
371
375 bool operator>=(const ResourceLedger&);
376
380 bool operator>(const double&);
381
385 bool operator<(const double&);
386
387 bool operator==(const double&);
388 bool operator!=(const double&);
389 bool operator<=(const double&);
390 bool operator>=(const double&);
391
392 void AssignFrom(const ResourceLedger&);
393
397 void TransferTo(ResourceLedger& ledger_to, const ResourceLedger& amount);
398 // Equivalent to this += other * double
399 void MultiplyAdd(const ResourceLedger&, double);
400
401 // Add all the positive values in the other ledger to this ledger
402 // Essentially this += other (if idx > 0)
404
405 // Add all the negative values in the other ledger to this ledger
406 // Essentially this += abs(other) (if idx < 0)
407 void AddNegative(const ResourceLedger&);
408
412 ResourceLedger Clamp(const double, const double);
413
418
423
424 double MultiplyAndGetSum(const ResourceMap& other) const;
425 double MultiplyAndGetSum(const ResourceLedger& other) const;
426
431 double Average() const;
432
433 double Min() const;
434 double Max() const;
435
439 double GetSum();
440
441 auto begin() { return ledger.begin(); }
442
443 auto end() { return ledger.end(); }
444
445 size_t size() { return ledger.size(); }
446
447 void clear();
448};
449
450ResourceMap CopyVals(const ResourceMap& keys, const ResourceMap& values);
451} // namespace cqsp::core::components
Definition: resourceledger.h:303
ResourceLedger operator*(const ResourceMap &) const
bool operator<(const ResourceLedger &)
All resources in this ledger are smaller than than the other ledger
Definition: resourceledger.cpp:595
void operator/=(const ResourceLedger &)
Definition: resourceledger.cpp:476
void AssignFrom(const ResourceLedger &)
Definition: resourceledger.cpp:689
double GetSum()
Definition: resourceledger.cpp:810
ResourceLedger operator+(const ResourceMap &) const
double operator[](const GoodEntity value) const
Definition: resourceledger.cpp:506
bool operator>(const ResourceLedger &)
All resources in this ledger are greater than the other ledger
Definition: resourceledger.cpp:602
void AddPositive(const ResourceLedger &)
ResourceLedger operator*(const ResourceLedger &) const
Definition: resourceledger.cpp:526
auto end()
Definition: resourceledger.h:443
double Min() const
Definition: resourceledger.cpp:789
ResourceLedger Clamp(const double, const double)
Returns a copy of the vector with the values clamped between the min and max indicated
Definition: resourceledger.cpp:731
auto begin()
Definition: resourceledger.h:441
void operator*=(const ResourceLedger &)
Definition: resourceledger.cpp:470
bool operator>=(const ResourceLedger &)
All resources in this ledger are greater than or equal to the other ledger
Definition: resourceledger.cpp:616
void AddNegative(const ResourceLedger &)
Definition: resourceledger.cpp:720
double Max() const
Definition: resourceledger.cpp:799
double MultiplyAndGetSum(const ResourceMap &other) const
Definition: resourceledger.cpp:818
bool EnoughToTransfer(const ResourceLedger &amount)
This resource ledger has enough resources inside to transfer "amount" amount of resources away
ResourceLedger operator/(const ResourceLedger &) const
Definition: resourceledger.cpp:534
void TransferTo(ResourceLedger &ledger_to, const ResourceLedger &amount)
Definition: resourceledger.cpp:695
bool operator!=(const double &)
Definition: resourceledger.cpp:668
ResourceLedger operator/(const ResourceMap &) const
double Average() const
Returns a copy of the vector divided by the indicated vector, with division by zero resulting in infi...
Definition: resourceledger.cpp:781
std::vector< double > ledger
Definition: resourceledger.h:305
ResourceLedger operator+(const ResourceLedger &) const
Definition: resourceledger.cpp:518
void MultiplyAdd(const ResourceLedger &, double)
Definition: resourceledger.cpp:702
bool operator==(const double &)
Definition: resourceledger.cpp:661
void clear()
Definition: resourceledger.cpp:739
ResourceLedger operator-(const ResourceLedger &) const
Definition: resourceledger.cpp:510
void operator-=(const ResourceLedger &)
Definition: resourceledger.cpp:464
ResourceLedger operator-(const ResourceMap &) const
void operator+=(const ResourceLedger &)
Definition: resourceledger.cpp:458
ResourceLedger(size_t count)
Definition: resourceledger.cpp:445
bool operator<=(const ResourceLedger &)
All resources in this ledger are smaller than or equal to than the other ledger
Definition: resourceledger.cpp:609
size_t size()
Definition: resourceledger.h:445
ResourceLedger SafeDivision(const ResourceLedger &)
Returns a copy of the vector divided by the indicated vector, with division by zero resulting in infi...
Definition: resourceledger.cpp:748
Try to avoid using this unless you really need it since this is extremely slow.
Definition: resourceledger.h:38
bool LedgerEquals(const ResourceMap &)
Definition: resourceledger.cpp:249
void operator-=(const ResourceLedger &)
ResourceMap operator*(const ResourceMap &) const
Definition: resourceledger.cpp:187
ResourceMap operator/(const ResourceMap &) const
Definition: resourceledger.cpp:193
double GetSum()
Definition: resourceledger.cpp:348
void AddPositive(const ResourceMap &)
Definition: resourceledger.cpp:113
bool HasGood(GoodEntity good)
Definition: resourceledger.h:182
std::string to_string()
Definition: resourceledger.cpp:422
ResourceMap SafeDivision(const ResourceMap &)
Returns a copy of the vector divided by the indicated vector, with division by zero resulting in infi...
Definition: resourceledger.cpp:364
void operator/=(const ResourceLedger &)
ResourceMap LimitedRemoveResources(const ResourceMap &)
Same as RemoveResourcesLimited, except that it returns how much resources it took out.
Definition: resourceledger.cpp:304
double MultiplyAndGetSum(ResourceMap &other)
Multiplies the numbers stated in the resource ledger. Used for calculating the price,...
Definition: resourceledger.cpp:356
bool EnoughToTransfer(const ResourceMap &amount)
This resource ledger has enough resources inside to transfer "amount" amount of resources away
Definition: resourceledger.cpp:105
ResourceMap operator-(const ResourceMap &) const
Definition: resourceledger.cpp:181
ResourceMap operator+(const ResourceMap &) const
Definition: resourceledger.cpp:175
bool operator==(const double &)
Definition: resourceledger.cpp:239
ResourceMap UnitLedger(const double)
Returns a copy of the vector with the values set to indicated value
Definition: resourceledger.cpp:319
ResourceMap Clamp(const double, const double)
Returns a copy of the vector with the values clamped between the min and max indicated
Definition: resourceledger.cpp:327
double operator[](const GoodEntity) const
Definition: resourceledger.cpp:96
double Min()
Finds the smallest value in the Ledger.
Definition: resourceledger.cpp:397
void operator*=(const ResourceLedger &)
void AddNegative(const ResourceMap &)
Definition: resourceledger.cpp:120
void operator-=(const ResourceMap &)
Definition: resourceledger.cpp:127
void TransferTo(ResourceMap &, const ResourceMap &)
Definition: resourceledger.cpp:274
double Average()
Returns a copy of the vector divided by the indicated vector, with division by zero resulting in infi...
Definition: resourceledger.cpp:420
void RemoveResourcesLimited(const ResourceMap &)
Removes the resources, and if the amount of resources removed are more than the resources inside the ...
Definition: resourceledger.cpp:294
bool operator<=(const ResourceMap &)
All resources in this ledger are smaller than or equal to than the other ledger
Definition: resourceledger.cpp:261
bool operator<(const ResourceMap &)
All resources in this ledger are smaller than than the other ledger
Definition: resourceledger.cpp:253
void operator+=(const ResourceLedger &)
bool operator>(const ResourceMap &)
All resources in this ledger are greater than the other ledger
Definition: resourceledger.cpp:257
double Max()
Finds the largest value in the Ledger.
Definition: resourceledger.cpp:412
bool HasAllResources(const ResourceMap &)
Checks if this current resource ledger has any resources in this list
Definition: resourceledger.cpp:341
void operator/=(const ResourceMap &)
Definition: resourceledger.cpp:145
void MultiplyAdd(const ResourceMap &, double)
Definition: resourceledger.cpp:281
bool operator>=(const ResourceMap &)
All resources in this ledger are greater than or equal to the other ledger
Definition: resourceledger.cpp:245
void operator+=(const ResourceMap &)
Definition: resourceledger.cpp:133
void AssignFrom(const ResourceMap &)
Definition: resourceledger.cpp:268
void operator*=(const ResourceMap &)
Definition: resourceledger.cpp:139
This is a vector meant for fast iteration
Definition: resourceledger.h:223
double Max()
Definition: resourceledger.cpp:855
double MultiplyAndGetSum(const ResourceLedger &other) const
Multiplies the numbers stated in the resource ledger. Used for calculating the price,...
Definition: resourceledger.cpp:909
void operator+=(const ResourceMap &)
Definition: resourceledger.cpp:1038
ResourceVector operator-(const double value) const
Definition: resourceledger.cpp:953
void Finalize()
Definition: resourceledger.cpp:877
ResourceVector operator/(const double value) const
Definition: resourceledger.cpp:977
void operator*=(const ResourceMap &)
Definition: resourceledger.cpp:1044
double Min()
Definition: resourceledger.cpp:845
double operator[](const GoodEntity &good) const
Definition: resourceledger.cpp:888
bool HasGood(const GoodEntity &good) const
Definition: resourceledger.cpp:884
ResourceVector operator*(const double value) const
Definition: resourceledger.cpp:969
std::pair< GoodEntity, double > LedgerPair
Definition: resourceledger.h:224
GoodEntity MinGood()
Definition: resourceledger.cpp:917
void operator-=(const ResourceMap &)
Definition: resourceledger.cpp:1032
double Average()
Returns the average of all values in the ledger, with division by zero resulting in infiniy
Definition: resourceledger.cpp:834
double GetSum()
Definition: resourceledger.cpp:901
bool contains(const GoodEntity &entity) const
Definition: resourceledger.cpp:897
GoodEntity MaxGood()
Definition: resourceledger.cpp:865
void operator/=(const ResourceMap &)
Definition: resourceledger.cpp:1050
std::vector< LedgerPair > LedgerVector
Definition: resourceledger.h:225
ResourceVector operator+(const double value) const
Definition: resourceledger.cpp:961
Definition: area.h:27
ResourceMap CopyVals(const ResourceMap &keys, const ResourceMap &values)
Creates a new resource ledger using the keys from one resource ledger, and the values from annother
Definition: resourceledger.cpp:437
std::map< GoodEntity, double > LedgerMap
Definition: resourceledger.h:33
GoodEntity
Definition: resourceledger.h:28
constexpr GoodEntity ToGoodEntity(uint32_t val)
Definition: resourceledger.h:31