-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworld.cpp
More file actions
192 lines (182 loc) · 5.08 KB
/
world.cpp
File metadata and controls
192 lines (182 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <sstream>
#include <stdlib.h>
#include "world.h"
#include "window.h"
#include "globals.h"
#include "rng.h"
#include "string_tags.h"
std::string short_date_string(int day);
World::World()
{
day = 1;
news.clear();
for (int i = 0; i < NUM_GOODS; i++) {
price_adjustment[i] = 0;
}
}
std::string World::get_date_str()
{
std::stringstream ret;
int year = 3000 + day / 100;
ret << "Year " << year << ", day " << day % 100;
return ret.str();
}
void World::advance_days(int amount)
{
if (amount == 0) {
return;
}
int total_supply[NUM_GOODS], total_demand[NUM_GOODS];
for (int i = 0; i < NUM_GOODS; i++) {
total_supply[i] = 0;
total_demand[i] = 0;
}
for (int i = 0; i < PLANETS.size(); i++) {
for (int n = 0; n < NUM_GOODS; n++) {
total_supply[n] += PLANETS[i].supply[n];
total_demand[n] += PLANETS[i].demand[n];
}
}
day += amount;
// Make minimum debt payments
for (int i = 0; i < amount; i++) {
update_prices(total_supply, total_demand);
PLR.update_rep();
if (PLR.debt > 0) {
// TODO: Don't hard-code minimum payment.
int min_payment = 10 + (PLR.debt / 250);
if (min_payment > PLR.debt) {
min_payment = PLR.debt;
}
if (PLR.cash < min_payment) {
// Increase debt rather than make a payment!
min_payment -= PLR.cash;
PLR.cash = 0;
PLR.debt += min_payment;
} else {
PLR.cash -= min_payment;
PLR.debt -= min_payment;
}
}
for (int n = 0; n < current_events.size(); n++) {
for (int m = 0; m < current_events[n].modifiers.size(); m++) {
if (day >= current_events[n].modifiers[m].timeout) {
current_events[n].modifiers.erase(
current_events[n].modifiers.begin() + m );
m--;
}
}
if (current_events[n].modifiers.empty()) {
current_events.erase( current_events.begin() + n );
n--;
}
}
for (int n = 0; n < EVENTS.size(); n++) {
if (one_in(4) && one_in(EVENTS[n]->frequency)) {
add_event(EVENTS[n]);
}
}
}
}
void World::update_prices(int total_supply[NUM_GOODS],
int total_demand[NUM_GOODS])
{
int maxdiff = PLANETS.size() * 10;
for (int i = 0; i < NUM_GOODS; i++) {
int range = GOOD_DATA[i]->high_value - GOOD_DATA[i]->low_value;
int diff = total_demand[i] - total_supply[i];
int unit = range / maxdiff;
if (unit < 1) {
unit = 1;
}
int curr = price_adjustment[i] / unit;
if (diff < 0) {
if (diff < curr) {
price_adjustment[i] -= rng(0, unit);
if (price_adjustment[i] < 0 - range / 2) {
price_adjustment[i] = 0 - range / 2;
}
}
} else if (diff > curr) {
price_adjustment[i] += rng(0, unit);
if (price_adjustment[i] > range / 2) {
price_adjustment[i] = range / 2;
}
}
}
}
void World::add_event(Event* ev)
{
Event new_event("Nope", "No way", 0, c_white, 0);
new_event.name = ev->name;
new_event.description = ev->description;
int num_planets = PLANETS.size();
int planet_used[num_planets];
int end_date = day;
for (int i = 0; i < num_planets; i++) {
planet_used[i] = -1;
}
for (int i = 0; i < ev->modifiers.size(); i++) {
Price_modifier tmpmod;
Price_modifier *ref = &(ev->modifiers[i]);
int planet = ref->planet_id;
if (planet != -1) {
if (planet_used[planet] == -1) {
planet_used[planet] = rng(0, num_planets - 1);
}
tmpmod.planet_id = planet_used[planet];
} else {
tmpmod.planet_id = -1;
}
tmpmod.good = ref->good;
tmpmod.static_change = ref->static_change;
tmpmod.percent_change = ref->percent_change;
tmpmod.supply_change = ref->supply_change;
tmpmod.demand_change = ref->demand_change;
int min_timeout = day + (ref->timeout * 0.9);
int max_timeout = day + (ref->timeout * 1.1);
tmpmod.timeout = rng(min_timeout, max_timeout);
if (tmpmod.timeout > end_date) {
end_date = tmpmod.timeout;
}
new_event.modifiers.push_back(tmpmod);
}
current_events.push_back(new_event);
std::string planet_name = "the galaxy";
if (planet_used[0] != -1) {
planet_name = PLANETS[ planet_used[0] ].name;
}
add_news( new_event.description, planet_name, ev->base_color, day, end_date );
}
void World::add_news(std::string text, std::string planet_name, nc_color color,
int start, int end)
{
std::string processed = process_string_tags( text, planet_name, color );
News_item tmp(processed, start, end);
news.push_back(tmp);
}
std::string World::get_news()
{
if (news.empty()) {
return "No news.";
}
std::string ret;
for (int i = 0; i < news.size(); i++) {
if (news[i].end <= day) {
news.erase( news.begin() + i);
i--;
} else {
ret += "*** " + short_date_string(news[i].start) + " - " +
short_date_string(news[i].end) + " ***\n";
ret += news[i].text + "\n";
}
}
return ret;
}
std::string short_date_string(int day)
{
std::stringstream ret;
int year = 3000 + day / 100;
ret << year << "-" << day % 100;
return ret.str();
}