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 {};
29
30class ResourceLedger;
31
32typedef std::map<GoodEntity, double> LedgerMap;
33
34class ResourceMap : private LedgerMap {
35 public:
36 ResourceMap() = default;
37 ~ResourceMap() = default;
38
39 double operator[](const GoodEntity) const;
40
46 bool EnoughToTransfer(const ResourceMap& amount);
47
48 void operator-=(const ResourceMap&);
49 void operator+=(const ResourceMap&);
50 void operator*=(const ResourceMap&);
51 void operator/=(const ResourceMap&);
52
57
58 void operator-=(const double value);
59 void operator+=(const double value);
60 void operator*=(const double value);
61 void operator/=(const double value);
62
63 ResourceMap operator-(const ResourceMap&) const;
64 ResourceMap operator+(const ResourceMap&) const;
65 ResourceMap operator*(const ResourceMap&) const;
66 ResourceMap operator/(const ResourceMap&) const;
67 ResourceMap operator-(const double value) const;
68 ResourceMap operator+(const double value) const;
69 ResourceMap operator*(const double value) const;
70 ResourceMap operator/(const double value) const;
71
75 bool operator<(const ResourceMap&);
76
80 bool operator>(const ResourceMap&);
81
85 bool operator<=(const ResourceMap&);
86
90 bool operator>=(const ResourceMap&);
91
92 bool LedgerEquals(const ResourceMap&);
93
97 bool operator>(const double&);
98
102 bool operator<(const double&);
103
104 bool operator==(const double&);
105
106 bool operator<=(const double&);
107 bool operator>=(const double&);
108
109 void AssignFrom(const ResourceMap&);
110
111 void TransferTo(ResourceMap&, const ResourceMap&);
112 // Equivalant to this += other * double
113 void MultiplyAdd(const ResourceMap&, double);
114
121
127
131 ResourceMap UnitLedger(const double);
132
133 // Add all the positive values in the other ledger to this ledger
134 // Essentially this += other (if idx > 0)
135 void AddPositive(const ResourceMap&);
136
137 // Add all the negative values in the other ledger to this ledger
138 // Essentially this += abs(other) (if idx < 0)
139 void AddNegative(const ResourceMap&);
140
144 ResourceMap Clamp(const double, const double);
145
150
154 ResourceMap SafeDivision(const ResourceMap&, double);
155
160 double Average();
161
162 double Min();
163 double Max();
164
170 bool HasAllResources(const ResourceMap&);
171
172 bool HasGood(GoodEntity good) { return (*this).find(good) != (*this).end(); }
173
177 double GetSum();
178
185 double MultiplyAndGetSum(ResourceMap& other);
186
187 std::string to_string();
188
189 // All the things that we get from map
190 using LedgerMap::operator[];
191 using LedgerMap::operator=;
192 using LedgerMap::begin;
193 using LedgerMap::cbegin;
194 using LedgerMap::cend;
195 using LedgerMap::clear;
196 using LedgerMap::contains;
197 using LedgerMap::crbegin;
198 using LedgerMap::crend;
199 using LedgerMap::emplace;
200 using LedgerMap::empty;
201 using LedgerMap::end;
202 using LedgerMap::erase;
203 using LedgerMap::mapped_type;
204 using LedgerMap::rbegin;
205 using LedgerMap::rend;
206 using LedgerMap::size;
207 using LedgerMap::value_comp;
208};
209
211 private:
212 std::vector<double> ledger;
213
214 public:
215 explicit ResourceLedger(size_t count);
216
217 operator ResourceMap() const;
218
219 double operator[](const GoodEntity value) const;
220 double& operator[](const GoodEntity value);
221
227 bool EnoughToTransfer(const ResourceLedger& amount);
228
229 void operator-=(const ResourceLedger&);
230 void operator+=(const ResourceLedger&);
231 void operator*=(const ResourceLedger&);
232 void operator/=(const ResourceLedger&);
233
234 void operator-=(const ResourceMap&);
235 void operator+=(const ResourceMap&);
236 void operator*=(const ResourceMap&);
237 void operator/=(const ResourceMap&);
238
239 void operator-=(const double value);
240 void operator+=(const double value);
241 void operator*=(const double value);
242 void operator/=(const double value);
243
248
253
254 ResourceLedger operator-(const double value) const;
255 ResourceLedger operator+(const double value) const;
256 ResourceLedger operator*(const double value) const;
257 ResourceLedger operator/(const double value) const;
258
262 bool operator<(const ResourceLedger&);
263
267 bool operator>(const ResourceLedger&);
268
272 bool operator<=(const ResourceLedger&);
273
277 bool operator>=(const ResourceLedger&);
278
282 bool operator>(const double&);
283
287 bool operator<(const double&);
288
289 bool operator==(const double&);
290 bool operator!=(const double&);
291 bool operator<=(const double&);
292 bool operator>=(const double&);
293
294 void AssignFrom(const ResourceLedger&);
295
299 void TransferTo(ResourceLedger& ledger_to, const ResourceLedger& amount);
300 // Equivalent to this += other * double
301 void MultiplyAdd(const ResourceLedger&, double);
302
303 // Add all the positive values in the other ledger to this ledger
304 // Essentially this += other (if idx > 0)
306
307 // Add all the negative values in the other ledger to this ledger
308 // Essentially this += abs(other) (if idx < 0)
309 void AddNegative(const ResourceLedger&);
310
314 ResourceLedger Clamp(const double, const double);
315
320
325
326 double MultiplyAndGetSum(const ResourceMap& other) const;
327 double MultiplyAndGetSum(const ResourceLedger& other) const;
328
333 double Average() const;
334
335 double Min() const;
336 double Max() const;
337
341 double GetSum();
342
343 auto begin() { return ledger.begin(); }
344
345 auto end() { return ledger.end(); }
346
347 size_t size() { return ledger.size(); }
348
349 void clear();
350};
351
352ResourceMap CopyVals(const ResourceMap& keys, const ResourceMap& values);
353} // namespace cqsp::core::components
Definition: resourceledger.h:210
ResourceLedger operator*(const ResourceMap &) const
bool operator<(const ResourceLedger &)
All resources in this ledger are smaller than than the other ledger
Definition: resourceledger.cpp:567
void operator/=(const ResourceLedger &)
Definition: resourceledger.cpp:469
void AssignFrom(const ResourceLedger &)
Definition: resourceledger.cpp:661
ResourceLedger operator+(const ResourceMap &) const
double operator[](const GoodEntity value) const
Definition: resourceledger.cpp:499
bool operator>(const ResourceLedger &)
All resources in this ledger are greater than the other ledger
Definition: resourceledger.cpp:574
void AddPositive(const ResourceLedger &)
ResourceLedger operator*(const ResourceLedger &) const
Definition: resourceledger.cpp:519
auto end()
Definition: resourceledger.h:345
double Min() const
Definition: resourceledger.cpp:761
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:703
auto begin()
Definition: resourceledger.h:343
void operator*=(const ResourceLedger &)
Definition: resourceledger.cpp:463
bool operator>=(const ResourceLedger &)
All resources in this ledger are greater than or equal to the other ledger
Definition: resourceledger.cpp:588
void AddNegative(const ResourceLedger &)
Definition: resourceledger.cpp:692
double Max() const
Definition: resourceledger.cpp:771
double MultiplyAndGetSum(const ResourceMap &other) const
Definition: resourceledger.cpp:782
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:527
void TransferTo(ResourceLedger &ledger_to, const ResourceLedger &amount)
Definition: resourceledger.cpp:667
bool operator!=(const double &)
Definition: resourceledger.cpp:640
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:753
std::vector< double > ledger
Definition: resourceledger.h:212
ResourceLedger operator+(const ResourceLedger &) const
Definition: resourceledger.cpp:511
void MultiplyAdd(const ResourceLedger &, double)
Definition: resourceledger.cpp:674
bool operator==(const double &)
Definition: resourceledger.cpp:633
void clear()
Definition: resourceledger.cpp:711
ResourceLedger operator-(const ResourceLedger &) const
Definition: resourceledger.cpp:503
void operator-=(const ResourceLedger &)
Definition: resourceledger.cpp:457
ResourceLedger operator-(const ResourceMap &) const
void operator+=(const ResourceLedger &)
Definition: resourceledger.cpp:451
ResourceLedger(size_t count)
Definition: resourceledger.cpp:438
bool operator<=(const ResourceLedger &)
All resources in this ledger are smaller than or equal to than the other ledger
Definition: resourceledger.cpp:581
size_t size()
Definition: resourceledger.h:347
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:720
Definition: resourceledger.h:34
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:341
void AddPositive(const ResourceMap &)
Definition: resourceledger.cpp:113
bool HasGood(GoodEntity good)
Definition: resourceledger.h:172
std::string to_string()
Definition: resourceledger.cpp:415
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:357
void operator/=(const ResourceLedger &)
ResourceMap LimitedRemoveResources(const ResourceMap &)
Same as RemoveResourcesLimited, except that it returns how much resources it took out.
Definition: resourceledger.cpp:297
double MultiplyAndGetSum(ResourceMap &other)
Multiplies the numbers stated in the resource ledger. Used for calculating the price,...
Definition: resourceledger.cpp:349
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:312
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:320
double operator[](const GoodEntity) const
Definition: resourceledger.cpp:96
double Min()
Finds the smallest value in the Ledger.
Definition: resourceledger.cpp:390
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:413
void RemoveResourcesLimited(const ResourceMap &)
Removes the resources, and if the amount of resources removed are more than the resources inside the ...
Definition: resourceledger.cpp:287
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:405
bool HasAllResources(const ResourceMap &)
Checks if this current resource ledger has any resources in this list
Definition: resourceledger.cpp:334
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
Definition: area.h:23
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:430
std::map< GoodEntity, double > LedgerMap
Definition: resourceledger.h:30
GoodEntity
Definition: resourceledger.h:28