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.
market.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
53
55
57
60
61 void ResetLedgers() {
62 // Reset the ledger values
64 demand().clear();
65 supply().clear();
66 }
67
69 if (current) {
70 return _supply;
71 } else {
72 return _previous_supply;
73 }
74 }
76 if (current) {
77 return _demand;
78 } else {
79 return _previous_demand;
80 }
81 }
82
84 if (current) {
85 return _previous_supply;
86 } else {
87 return _supply;
88 }
89 }
90
92 if (current) {
93 return _previous_demand;
94 } else {
95 return _demand;
96 }
97 }
98};
99
101 // Sum of the resources traded last time.
102 double supply;
103 double demand;
104 double price;
106 double sd_ratio;
108};
109
115 entt::entity target;
116 double amount;
117 double price;
118
119 MarketOrder(entt::entity target, double amount, double price) : target(target), amount(amount), price(price) {}
120};
121
122// A planetary market must have a regular market as well
124 std::map<entt::entity, std::vector<MarketOrder>> demands;
125 std::map<entt::entity, std::vector<MarketOrder>> requests;
126 // Resources supplied by the interplanetary market
129};
130
132 std::vector<MarketInformation> history;
133
134 std::map<entt::entity, MarketElementInformation> market_information;
135 std::map<entt::entity, MarketElementInformation> last_market_information;
136
137 std::set<entt::entity> participants;
138
139 entt::basic_sparse_set<entt::entity> connected_markets;
140
142
143 entt::entity parent_market = entt::null;
144
145 double GDP = 0;
146
147 // Math
148 void AddSupply(const ResourceLedger& stockpile);
149 void AddSupply(const ResourceLedger& stockpile, double multiplier);
150 void AddDemand(const ResourceLedger& stockpile);
151 void AddDemand(const ResourceLedger& stockpile, double multiplier);
152
153 double GetPrice(const ResourceLedger& stockpile);
154 double GetPrice(const entt::entity& good);
155 double GetSDRatio(const entt::entity& good);
156 double GetSupply(const entt::entity& good);
157 double GetDemand(const entt::entity& good);
158
159 void AddParticipant(entt::entity participant) { participants.insert(participant); }
160
161 MarketElementInformation& operator[](entt::entity ent) { return market_information[ent]; }
162
163 auto begin() { return market_information.begin(); }
164
165 auto end() { return market_information.end(); }
166};
167
173struct Price {
174 double price;
175
176 operator double() { return price; }
177};
178
189struct Currency {};
190
194struct CostTable : public ResourceLedger {};
195
196// TODO(EhWhoAmI): Add multiple currency support
197struct Wallet {
198 Wallet() = default;
199 Wallet(entt::entity _currency, double _balance) : balance(_balance), currency(_currency) {}
200
201 Wallet& operator+=(const double amount) {
202 this->balance += amount;
203 change += amount;
204 return *this;
205 }
206 Wallet& operator-=(const double amount) {
207 this->balance -= amount;
208 change -= amount;
209 GDP_change += amount;
210 // Record the money delta since last reset
211 return *this;
212 }
213
214 // Basic multiplication that logs the change
215 // TODO(EhWhoAmI): Make sure this is correct
216 Wallet& operator*=(const double coefficent) {
217 double newbalance = this->balance * coefficent;
218 double change = newbalance - this->balance;
219 if (change > 0) {
220 *this += change;
221 } else if (change < 0) {
222 *this -= change * -1;
223 }
224 return *this;
225 }
226
227 operator double() const { return balance; }
228
229 Wallet& operator=(double _balance) {
230 change += (_balance - balance);
231 if ((_balance - balance) < 0) {
232 GDP_change += _balance - balance;
233 }
234 balance = _balance;
235 return *this;
236 }
237
238 double GetBalance() const { return balance; }
239
240 double GetChange() const { return change; }
241
242 void Reset() {
243 change = 0;
244 GDP_change = 0;
245 }
246 double GetGDPChange() { return GDP_change; }
247
248 private:
249 double balance = 0;
250 double change = 0;
251 // Only records when spending money, so when money decreases
252 double GDP_change = 0;
253 entt::entity currency;
254};
255
260 entt::entity market;
261};
262
267 entt::entity market;
268};
269
270// This trade node has international connections
271// For space connections, the spaceport struct exists
273
280 entt::entity city;
281 int size;
282};
283
284// Something that hires people, and will pay the people
285struct Employer {
288 entt::entity segment;
289};
290
303};
304
306
307// This facility is bankrolled by something else, so if they run out of money
308// they can go to this wallet to ask for cash?
309struct Owned {
310 entt::entity owner;
311};
312
313struct TradePartners : std::vector<entt::entity> {};
314} // namespace cqsp::common::components
Definition: area.h:23
Represents commercial areas and other amenities that generate economic activity. They don't export go...
Definition: market.h:278
entt::entity city
The city that it's based in.
Definition: market.h:280
int size
Definition: market.h:281
Records the prices of goods and other things
Definition: market.h:194
Will be handled in the future, but for now is just a market
Definition: market.h:189
Definition: market.h:285
int population_needed
Definition: market.h:286
entt::entity segment
Definition: market.h:288
int population_fufilled
Definition: market.h:287
Population segment that is employed
Definition: market.h:294
int working_population
The population that is available to work
Definition: market.h:298
int employed_population
The current population is currently working.
Definition: market.h:302
An actor in a market that trades goods.
Definition: market.h:259
entt::entity market
Definition: market.h:260
An entity where the market is based, and the resources are traded.
Definition: market.h:266
entt::entity market
Definition: market.h:267
double price_ratio
Definition: market.h:105
Definition: market.h:131
double GDP
Definition: market.h:145
entt::entity parent_market
Definition: market.h:143
auto begin()
Definition: market.h:163
std::vector< MarketInformation > history
Definition: market.h:132
std::map< entt::entity, MarketElementInformation > market_information
Definition: market.h:134
double GetPrice(const ResourceLedger &stockpile)
Definition: market.cpp:45
ResourceLedger market_access
Definition: market.h:141
void AddSupply(const ResourceLedger &stockpile)
Definition: market.cpp:21
void AddDemand(const ResourceLedger &stockpile)
Definition: market.cpp:33
std::map< entt::entity, MarketElementInformation > last_market_information
Definition: market.h:135
entt::basic_sparse_set< entt::entity > connected_markets
Definition: market.h:139
MarketElementInformation & operator[](entt::entity ent)
Definition: market.h:161
double GetSDRatio(const entt::entity &good)
Definition: market.cpp:53
std::set< entt::entity > participants
Definition: market.h:137
void AddParticipant(entt::entity participant)
Definition: market.h:159
double GetSupply(const entt::entity &good)
Definition: market.cpp:55
auto end()
Definition: market.h:165
double GetDemand(const entt::entity &good)
Definition: market.cpp:57
Historical information about the market Might change this to a different type of resource ledger so t...
Definition: market.h:33
ResourceLedger & supply()
Definition: market.h:68
ResourceLedger _supply
Definition: market.h:37
ResourceLedger price
Definition: market.h:50
ResourceLedger trade
Definition: market.h:54
ResourceLedger _demand
Definition: market.h:36
ResourceLedger & previous_demand()
Definition: market.h:91
ResourceLedger production
Definition: market.h:58
void ResetLedgers()
Definition: market.h:61
ResourceLedger & previous_supply()
Definition: market.h:83
ResourceLedger _previous_demand
Definition: market.h:38
ResourceLedger consumption
Definition: market.h:59
ResourceLedger _previous_supply
Definition: market.h:39
ResourceLedger & demand()
Definition: market.h:75
ResourceLedger resource_fulfilled
Definition: market.h:56
ResourceLedger chronic_shortages
Definition: market.h:52
ResourceLedger volume
The amount of goods that changed hands. We can use this to calculate the GDP
Definition: market.h:49
bool current
Definition: market.h:40
ResourceLedger sd_ratio
Definition: market.h:43
double price
Definition: market.h:117
entt::entity target
Definition: market.h:115
double amount
Definition: market.h:116
MarketOrder(entt::entity target, double amount, double price)
Definition: market.h:119
Definition: market.h:309
entt::entity owner
Definition: market.h:310
std::map< entt::entity, std::vector< MarketOrder > > requests
Definition: market.h:125
ResourceLedger supply_difference
Definition: market.h:128
ResourceLedger supplied_resources
Definition: market.h:127
std::map< entt::entity, std::vector< MarketOrder > > demands
Definition: market.h:124
Price of a good. This is temporary, because this is to determine initial prices for goods....
Definition: market.h:173
double price
Definition: market.h:174
Definition: market.h:197
double GDP_change
Definition: market.h:252
Wallet & operator=(double _balance)
Definition: market.h:229
double GetChange() const
Definition: market.h:240
double balance
Definition: market.h:249
Wallet & operator-=(const double amount)
Definition: market.h:206
void Reset()
Definition: market.h:242
double change
Definition: market.h:250
entt::entity currency
Definition: market.h:253
double GetBalance() const
Definition: market.h:238
Wallet & operator*=(const double coefficent)
Definition: market.h:216
Wallet & operator+=(const double amount)
Definition: market.h:201
double GetGDPChange()
Definition: market.h:246
Wallet(entt::entity _currency, double _balance)
Definition: market.h:199