Symphony Of Empires
product.hpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
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 // ----------------------------------------------------------------------------
18 // Name:
19 // product.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <string>
28 #include <deque>
29 #include <limits>
30 #include <glm/glm.hpp>
31 #include <glm/gtx/compatibility.hpp>
32 #include "objects.hpp"
33 
35 struct Commodity : RefnameEntity<CommodityId> {
37  std::string get_icon_path() const {
38  return string_format("gfx/commodity/%s.png", ref_name.c_str());
39  }
40 };
41 template<>
43  template<bool is_const>
45  template<bool is_serialize>
46  static inline void deser_dynamic(Eng3D::Deser::Archive& ar, type<is_serialize>& obj) {
47  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.cached_id);
48  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.name);
49  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.ref_name);
50  }
51 };
52 
54 struct Product : Entity<ProductId> {
55  float get_price_delta() const {
56  constexpr auto price_elasticity = 1.f;
57  if(demand == 0.f || supply == 0.f)
58  return -price_elasticity;
59 
60  auto ratio = 0.f;
61  if(supply > demand)
62  ratio -= supply / demand;
63  else if(supply < demand)
64  ratio += demand / supply;
65  return price_elasticity * glm::clamp(ratio, -1.f, 1.f);
66  }
67 
68  float sd_ratio() const {
69  if(demand <= 0.f)
70  return supply;
71  return glm::min(supply, supply / demand);
72  }
73 
74  float ds_ratio() const {
75  if(supply <= 0.f)
76  return demand;
77  return glm::min(demand, demand / supply);
78  }
79 
80  void close_market() {
81  this->demand = this->bought;
82  this->supply += this->produced;
83 
84  // Increase price with more demand
85  this->price_delta = this->get_price_delta();
86  // Set the new price
87  this->price = glm::clamp(this->price + this->price_delta, glm::epsilon<float>(), 100'000.f);
88  if(glm::epsilonEqual(this->price, 0.f, glm::epsilon<float>()))
89  this->price_delta = 0.f;
90 
91  this->produced = this->bought = 0.f;
92  }
93 
97  float buy(float amount) {
98  assert(amount >= 0.f && amount <= this->supply);
99  this->supply -= amount;
100  this->bought += amount;
101  assert(this->supply >= 0.f);
102  return this->price * amount;
103  }
104 
108  float produce(float amount) {
109  assert(amount >= 0.f);
110  this->produced += amount;
111  return this->price * amount;
112  }
113 
114  float price = 1.f;
115  float price_delta = 0.f;
116  float bought = 0.f;
117  float produced = 0.f;
118  float supply = 1.f;
119  float demand = 1.f;
120  float global_demand = 1.f;
121  float speculative_demand = 0.f;
122 };
123 template<>
125  template<bool is_const>
127  template<bool is_serialize>
129  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.cached_id);
130  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.price);
131  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.price_delta);
132  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.supply);
133  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.demand);
134  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.global_demand);
135  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.speculative_demand);
136  }
137 };
138 
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
Definition: string.hpp:100
A commodity, mostly serves as a "product type".
Definition: product.hpp:35
Eng3D::StringRef name
Definition: product.hpp:36
std::string get_icon_path() const
Definition: product.hpp:37
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
Definition: serializer.hpp:64
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
Definition: product.hpp:46
typename Eng3D::Deser::CondConstType< is_const, Commodity >::type type
Definition: product.hpp:44
typename Eng3D::Deser::CondConstType< is_const, Product >::type type
Definition: product.hpp:126
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
Definition: product.hpp:128
A serializer (base class) which can be used to serialize objects and create per-object optimized clas...
Definition: serializer.hpp:111
A reference to a string on the global string pool.
Definition: string.hpp:39
const char * c_str() const
Definition: string.hpp:55
An entity which can only be referenced by an (presumably) unique Id this is the base class for the ot...
Definition: entity.hpp:94
A product (based off a Commodity) which can be bought by POPs, converted by factories and transported...
Definition: product.hpp:54
float ds_ratio() const
Definition: product.hpp:74
float supply
Definition: product.hpp:118
float price_delta
Definition: product.hpp:115
float bought
Definition: product.hpp:116
void close_market()
Definition: product.hpp:80
float speculative_demand
Definition: product.hpp:121
float produced
Definition: product.hpp:117
float sd_ratio() const
Definition: product.hpp:68
float buy(float amount)
Buy a portion of the item.
Definition: product.hpp:97
float global_demand
Definition: product.hpp:120
float demand
Definition: product.hpp:119
float get_price_delta() const
Definition: product.hpp:55
float produce(float amount)
Produce the product.
Definition: product.hpp:108
float price
Definition: product.hpp:114
An entity which can be referenced via a ref_name and also via id.
Definition: entity.hpp:160
Eng3D::StringRef ref_name
Definition: entity.hpp:161