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.
economy.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 <map>
20#include <set>
21#include <string>
22#include <vector>
23
24#include <entt/entt.hpp>
25
27
34 private:
35 // I forgot why we have 2 separate ledgers for supply and demand
40 bool current = true;
41
42 public:
44
51
52 // Supply that existed, but not fufilled last time
53 // Surplus supply
55 // Demand that existed, but was not fufilled the last time
56 // Surplus demand
60
62
65
68
69 // PID controller
71
72 void ResetLedgers() {
73 // Reset the ledger values
75 demand().clear();
76 supply().clear();
77 }
78
80 if (current) {
81 return _supply;
82 } else {
83 return _previous_supply;
84 }
85 }
87 if (current) {
88 return _demand;
89 } else {
90 return _previous_demand;
91 }
92 }
93
95 if (current) {
96 return _previous_supply;
97 } else {
98 return _supply;
99 }
100 }
101
103 if (current) {
104 return _previous_demand;
105 } else {
106 return _demand;
107 }
108 }
109};
110
112 // Sum of the resources traded last time.
113 double supply;
114 double demand;
115 double price;
117 double sd_ratio;
119};
120
122 std::vector<entt::entity> participants; // The markets that are connected
123};
124
126 std::vector<MarketInformation> history;
127
128 std::map<entt::entity, MarketElementInformation> market_information;
129 std::map<entt::entity, MarketElementInformation> last_market_information;
130
131 std::set<entt::entity> participants;
132 // std::vector<std::pair<entt::entity, entt::entity>> neighbors;
133 entt::basic_sparse_set<entt::entity> connected_markets;
134
135 double GDP = 0;
136
137 // Math
138 void AddSupply(const ResourceLedger& stockpile);
139 void AddSupply(const ResourceLedger& stockpile, double multiplier);
140 void AddDemand(const ResourceLedger& stockpile);
141 void AddDemand(const ResourceLedger& stockpile, double multiplier);
142
143 double GetPrice(const ResourceLedger& stockpile);
144 double GetPrice(const entt::entity& good);
145 double GetSDRatio(const entt::entity& good);
146 double GetSupply(const entt::entity& good);
147 double GetDemand(const entt::entity& good);
148
149 void AddParticipant(entt::entity participant) { participants.insert(participant); }
150
151 MarketElementInformation& operator[](entt::entity ent) { return market_information[ent]; }
152
153 auto begin() { return market_information.begin(); }
154
155 auto end() { return market_information.end(); }
156};
157
163struct Price {
164 double price;
165
166 operator double() { return price; }
167};
168
179struct Currency {};
180
184struct CostTable : public ResourceLedger {};
185
186// TODO(EhWhoAmI): Add multiple currency support
187struct Wallet {
188 Wallet() = default;
189 Wallet(entt::entity _currency, double _balance) : balance(_balance), currency(_currency) {}
190
191 Wallet& operator+=(const double amount) {
192 this->balance += amount;
193 change += amount;
194 return *this;
195 }
196 Wallet& operator-=(const double amount) {
197 this->balance -= amount;
198 change -= amount;
199 GDP_change += amount;
200 // Record the money delta since last reset
201 return *this;
202 }
203
204 // Basic multiplication that logs the change
205 // TODO(EhWhoAmI): Make sure this is correct
206 Wallet& operator*=(const double coefficent) {
207 double newbalance = this->balance * coefficent;
208 double change = newbalance - this->balance;
209 if (change > 0) {
210 *this += change;
211 } else if (change < 0) {
212 *this -= change * -1;
213 }
214 return *this;
215 }
216
217 operator double() const { return balance; }
218
219 Wallet& operator=(double _balance) {
220 change += (_balance - balance);
221 if ((_balance - balance) < 0) {
222 GDP_change += _balance - balance;
223 }
224 balance = _balance;
225 return *this;
226 }
227
228 double GetBalance() const { return balance; }
229
230 double GetChange() const { return change; }
231
232 void Reset() {
233 change = 0;
234 GDP_change = 0;
235 }
236 double GetGDPChange() { return GDP_change; }
237
238 private:
239 double balance = 0;
240 double change = 0;
241 // Only records when spending money, so when money decreases
242 double GDP_change = 0;
243 entt::entity currency;
244};
245
250 entt::entity market;
251};
252
257 entt::entity market;
258};
259
260// This trade node has international connections
261// For space connections, the spaceport struct exists
263
270 entt::entity city;
271 int size;
272};
273
274// Something that hires people, and will pay the people
275struct Employer {
278 entt::entity segment;
279};
280
293};
294
296
297// This facility is bankrolled by something else, so if they run out of money
298// they can go to this wallet to ask for cash?
299struct Owned {
300 entt::entity owner;
301};
302
303struct TradePartners : std::vector<entt::entity> {};
304} // namespace cqsp::common::components
Definition: area.h:23
Represents commercial areas and other amenities that generate economic activity. They don't export go...
Definition: economy.h:268
entt::entity city
The city that it's based in.
Definition: economy.h:270
int size
Definition: economy.h:271
Records the prices of goods and other things
Definition: economy.h:184
Will be handled in the future, but for now is just a market
Definition: economy.h:179
Definition: economy.h:275
int population_needed
Definition: economy.h:276
entt::entity segment
Definition: economy.h:278
int population_fufilled
Definition: economy.h:277
Population segment that is employed
Definition: economy.h:284
int working_population
The population that is available to work
Definition: economy.h:288
int employed_population
The current population is currently working.
Definition: economy.h:292
An actor in a market that trades goods.
Definition: economy.h:249
entt::entity market
Definition: economy.h:250
An entity where the market is based, and the resources are traded.
Definition: economy.h:256
entt::entity market
Definition: economy.h:257
double price_ratio
Definition: economy.h:116
double sd_ratio
Definition: economy.h:117
double inputratio
Definition: economy.h:118
Definition: economy.h:125
void AddSupply(const ResourceLedger &stockpile)
Definition: economy.cpp:22
double GDP
Definition: economy.h:135
auto begin()
Definition: economy.h:153
double GetSupply(const entt::entity &good)
Definition: economy.cpp:56
std::vector< MarketInformation > history
Definition: economy.h:126
std::map< entt::entity, MarketElementInformation > market_information
Definition: economy.h:128
double GetSDRatio(const entt::entity &good)
Definition: economy.cpp:54
std::map< entt::entity, MarketElementInformation > last_market_information
Definition: economy.h:129
entt::basic_sparse_set< entt::entity > connected_markets
Definition: economy.h:133
MarketElementInformation & operator[](entt::entity ent)
Definition: economy.h:151
double GetPrice(const ResourceLedger &stockpile)
Definition: economy.cpp:46
std::set< entt::entity > participants
Definition: economy.h:131
double GetDemand(const entt::entity &good)
Definition: economy.cpp:58
void AddParticipant(entt::entity participant)
Definition: economy.h:149
void AddDemand(const ResourceLedger &stockpile)
Definition: economy.cpp:34
auto end()
Definition: economy.h:155
Historical information about the market Might change this to a different type of resource ledger so t...
Definition: economy.h:33
ResourceLedger & supply()
Definition: economy.h:79
ResourceLedger last_latent_demand
Definition: economy.h:57
ResourceLedger latent_supply
Definition: economy.h:54
ResourceLedger _supply
Definition: economy.h:37
ResourceLedger price
Definition: economy.h:50
ResourceLedger trade
Definition: economy.h:63
ResourceLedger supply_difference
Definition: economy.h:59
ResourceLedger _demand
Definition: economy.h:36
ResourceLedger delta
Definition: economy.h:64
ResourceLedger & previous_demand()
Definition: economy.h:102
ResourceLedger production
Definition: economy.h:66
void ResetLedgers()
Definition: economy.h:72
ResourceLedger latent_demand
Definition: economy.h:58
ResourceLedger & previous_supply()
Definition: economy.h:94
ResourceLedger _previous_demand
Definition: economy.h:38
ResourceLedger consumption
Definition: economy.h:67
ResourceLedger _previous_supply
Definition: economy.h:39
ResourceLedger & demand()
Definition: economy.h:86
ResourceLedger chronic_shortages
Definition: economy.h:61
ResourceLedger sd_integral
Definition: economy.h:70
ResourceLedger volume
The amount of goods that changed hands. We can use this to calculate the GDP
Definition: economy.h:49
bool current
Definition: economy.h:40
ResourceLedger sd_ratio
Definition: economy.h:43
Definition: economy.h:299
entt::entity owner
Definition: economy.h:300
std::vector< entt::entity > participants
Definition: economy.h:122
Price of a good. This is temporary, because this is to determine initial prices for goods....
Definition: economy.h:163
double price
Definition: economy.h:164
Definition: economy.h:187
double GDP_change
Definition: economy.h:242
Wallet & operator=(double _balance)
Definition: economy.h:219
double GetChange() const
Definition: economy.h:230
double balance
Definition: economy.h:239
Wallet & operator-=(const double amount)
Definition: economy.h:196
void Reset()
Definition: economy.h:232
double change
Definition: economy.h:240
entt::entity currency
Definition: economy.h:243
double GetBalance() const
Definition: economy.h:228
Wallet & operator*=(const double coefficent)
Definition: economy.h:206
Wallet & operator+=(const double amount)
Definition: economy.h:191
double GetGDPChange()
Definition: economy.h:236
Wallet(entt::entity _currency, double _balance)
Definition: economy.h:189