-
Notifications
You must be signed in to change notification settings - Fork 990
/
Account.cpp
334 lines (260 loc) · 8.33 KB
/
Account.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Account.cpp
Copyright (c) 2014 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include "Account.h"
#include "DataNode.h"
#include "DataWriter.h"
#include "Format.h"
#include <algorithm>
#include <sstream>
using namespace std;
namespace {
// For tracking the player's average income, store daily net worth over this
// number of days.
const unsigned HISTORY = 100;
}
// Default constructor.
Account::Account()
: credits(0), salariesOwed(0), creditScore(400)
{
}
// Load account information from a data file (saved game or starting conditions).
void Account::Load(const DataNode &node)
{
credits = 0;
salariesOwed = 0;
creditScore = 400;
history.clear();
mortgages.clear();
for(const DataNode &child : node)
{
if(child.Token(0) == "credits" && child.Size() >= 2)
credits = child.Value(1);
else if(child.Token(0) == "salaries" && child.Size() >= 2)
salariesOwed = child.Value(1);
else if(child.Token(0) == "score" && child.Size() >= 2)
creditScore = child.Value(1);
else if(child.Token(0) == "mortgage")
mortgages.emplace_back(child);
else if(child.Token(0) == "history")
for(const DataNode &grand : child)
history.push_back(grand.Value(0));
}
}
// Write account information to a saved game file.
void Account::Save(DataWriter &out) const
{
out.Write("account");
out.BeginChild();
{
out.Write("credits", credits);
if(salariesOwed)
out.Write("salaries", salariesOwed);
out.Write("score", creditScore);
out.Write("history");
out.BeginChild();
{
for(int64_t worth : history)
out.Write(worth);
}
out.EndChild();
for(const Mortgage &mortgage : mortgages)
mortgage.Save(out);
}
out.EndChild();
}
// How much the player currently has in the bank.
int64_t Account::Credits() const
{
return credits;
}
// Give the player credits (or pass negative number to subtract). If subtracting,
// the calling function needs to check that this will not result in negative credits.
void Account::AddCredits(int64_t value)
{
credits += value;
}
// Pay down extra principal on a mortgage.
void Account::PayExtra(int mortgage, int64_t amount)
{
if(static_cast<unsigned>(mortgage) >= mortgages.size() || amount > credits
|| amount > mortgages[mortgage].Principal())
return;
mortgages[mortgage].PayExtra(amount);
credits -= amount;
// If this payment was for the entire remaining amount in the mortgage,
// remove it from the list.
if(!mortgages[mortgage].Principal())
mortgages.erase(mortgages.begin() + mortgage);
}
// Step forward one day, and return a string summarizing payments made.
string Account::Step(int64_t assets, int64_t salaries)
{
ostringstream out;
// Keep track of what payments were made and whether any could not be made.
salariesOwed += salaries;
bool paid = true;
// Crew salaries take highest priority.
int64_t salariesPaid = salariesOwed;
if(salariesOwed)
{
if(salariesOwed > credits)
{
// If you can't pay the full salary amount, still pay some of it and
// remember how much back wages you owe to your crew.
salariesPaid = max<int64_t>(credits, 0);
salariesOwed -= salariesPaid;
credits -= salariesPaid;
paid = false;
out << "You could not pay all your crew salaries. ";
}
else
{
credits -= salariesOwed;
salariesOwed = 0;
}
}
// Unlike salaries, each mortgage payment must either be made in its entirety,
// or skipped completely (accruing interest and reducing your credit score).
int64_t mortgagesPaid = 0;
int64_t finesPaid = 0;
for(Mortgage &mortgage : mortgages)
{
int64_t payment = mortgage.Payment();
if(payment > credits)
{
mortgage.MissPayment();
if(paid)
out << "You missed a mortgage payment. ";
paid = false;
}
else
{
payment = mortgage.MakePayment();
credits -= payment;
// For the status text, keep track of whether this is a mortgage or a fine.
if(mortgage.Type() == "Mortgage")
mortgagesPaid += payment;
else
finesPaid += payment;
}
assets -= mortgage.Principal();
}
// If any mortgage has been fully paid off, remove it from the list.
for(auto it = mortgages.begin(); it != mortgages.end(); )
{
if(!it->Principal())
it = mortgages.erase(it);
else
++it;
}
// Keep track of your net worth over the last HISTORY days.
if(history.size() > HISTORY)
history.erase(history.begin());
history.push_back(credits + assets - salariesOwed);
// If you failed to pay any debt, your credit score drops. Otherwise, even
// if you have no debts, it increases. (Because, having no debts at all
// makes you at least as credit-worthy as someone who pays debts on time.)
creditScore = max(200, min(800, creditScore + (paid ? 1 : -5)));
// If you didn't make any payments, no need to continue further.
if(!(salariesPaid + mortgagesPaid + finesPaid))
return out.str();
out << "You paid ";
// If you made payments of all three types, the punctuation needs to
// include commas, so just handle that separately here.
if(salariesPaid && mortgagesPaid && finesPaid)
out << Format::Credits(salariesPaid) << " credits in crew salaries, " << Format::Credits(mortgagesPaid)
<< " in mortgages, and " << Format::Credits(finesPaid) << " in fines.";
else
{
if(salariesPaid)
out << Format::Credits(salariesPaid) << ((mortgagesPaid || finesPaid) ?
" credits in crew salaries and " : " credits in crew salaries.");
if(mortgagesPaid)
out << Format::Credits(mortgagesPaid) << (salariesPaid ? " " : " credits ")
<< (finesPaid ? "in mortgage payments and " : "in mortgage payments.");
if(finesPaid)
out << Format::Credits(finesPaid) << ((salariesPaid || mortgagesPaid) ?
" in fines." : " credits in fines.");
}
return out.str();
}
int64_t Account::SalariesOwed() const
{
return salariesOwed;
}
void Account::PaySalaries(int64_t amount)
{
amount = min(min(amount, salariesOwed), credits);
credits -= amount;
salariesOwed -= amount;
}
// Access the list of mortgages.
const vector<Mortgage> &Account::Mortgages() const
{
return mortgages;
}
// Add a new mortgage for the given amount, with an interest rate determined by
// your credit score.
void Account::AddMortgage(int64_t principal)
{
mortgages.emplace_back(principal, creditScore);
credits += principal;
}
// Add a "fine" with a high, fixed interest rate and a short term.
void Account::AddFine(int64_t amount)
{
mortgages.emplace_back(amount, 0, 60);
}
// Check how big a mortgage the player can afford to pay at their current income.
int64_t Account::Prequalify() const
{
int64_t payments = 0;
int64_t liabilities = 0;
for(const Mortgage &mortgage : mortgages)
{
payments += mortgage.Payment();
liabilities += mortgage.Principal();
}
// Put a limit on new debt that the player can take out, as a fraction of
// their net worth, to avoid absurd mortgages being offered when the player
// has just captured some very lucrative ships.
return max<int64_t>(0, min(
NetWorth() / 3 + 500000 - liabilities,
Mortgage::Maximum(YearlyRevenue(), creditScore, payments)));
}
// Get the player's total net worth (counting all ships and all debts).
int64_t Account::NetWorth() const
{
return history.empty() ? 0 : history.back();
}
// Find out the player's credit rating.
int Account::CreditScore() const
{
return creditScore;
}
// Get the total amount owed for "Mortgage", "Fine", or both.
int64_t Account::TotalDebt(const string &type) const
{
int64_t total = 0;
for(const Mortgage &mortgage : mortgages)
if(type.empty() || mortgage.Type() == type)
total += mortgage.Principal();
return total;
}
// Extrapolate from the player's current net worth history to determine how much
// their net worth is expected to change over the course of the next year.
int64_t Account::YearlyRevenue() const
{
if(history.empty() || history.back() <= history.front())
return 0;
// Note that this intentionally under-estimates if the player has not yet
// played for long enough to accumulate a full income history.
return ((history.back() - history.front()) * 365) / HISTORY;
}