File: Account.cpp

package info (click to toggle)
endless-sky 0.10.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 414,608 kB
  • sloc: cpp: 73,435; python: 893; xml: 666; sh: 271; makefile: 28
file content (452 lines) | stat: -rw-r--r-- 11,061 bytes parent folder | download
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* 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.

You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "Account.h"

#include "DataNode.h"
#include "DataWriter.h"
#include "text/Format.h"

#include <algorithm>
#include <numeric>
#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;
}



// Load account information from a data file (saved game or starting conditions).
void Account::Load(const DataNode &node, bool clearFirst)
{
	if(clearFirst)
	{
		credits = 0;
		crewSalariesOwed = 0;
		maintenanceDue = 0;
		creditScore = 400;
		mortgages.clear();
		history.clear();
	}

	for(const DataNode &child : node)
	{
		const string &key = child.Token(0);
		bool hasValue = child.Size() >= 2;
		if(key == "credits" && hasValue)
			credits = child.Value(1);
		else if(key == "salaries income")
		{
			for(const DataNode &grand : child)
			{
				if(grand.Size() < 2)
					grand.PrintTrace("Skipping incomplete salary income:");
				else
					salariesIncome[grand.Token(0)] = grand.Value(1);
			}
		}
		else if(key == "salaries" && hasValue)
			crewSalariesOwed = child.Value(1);
		else if(key == "maintenance" && hasValue)
			maintenanceDue = child.Value(1);
		else if(key == "score" && hasValue)
			creditScore = child.Value(1);
		else if(key == "mortgage")
			mortgages.emplace_back(child);
		else if(key == "history")
			for(const DataNode &grand : child)
				history.push_back(grand.Value(0));
		else
			child.PrintTrace("Skipping unrecognized account item:");
	}
}



// Write account information to a saved game file.
void Account::Save(DataWriter &out) const
{
	out.Write("account");
	out.BeginChild();
	{
		out.Write("credits", credits);
		if(!salariesIncome.empty())
		{
			out.Write("salaries income");
			out.BeginChild();
			{
				for(const auto &income : salariesIncome)
					out.Write(income.first, income.second);
			}
			out.EndChild();
		}
		if(crewSalariesOwed)
			out.Write("salaries", crewSalariesOwed);
		if(maintenanceDue)
			out.Write("maintenance", maintenanceDue);
		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, int64_t maintenance)
{
	ostringstream out;

	// Keep track of what payments were made and whether any could not be made.
	crewSalariesOwed += salaries;
	maintenanceDue += maintenance;
	bool missedPayment = false;

	// Crew salaries take highest priority.
	int64_t salariesPaid = crewSalariesOwed;
	if(crewSalariesOwed)
	{
		if(crewSalariesOwed > 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);
			crewSalariesOwed -= salariesPaid;
			credits -= salariesPaid;
			missedPayment = true;
			out << "You could not pay all your crew salaries.";
		}
		else
		{
			credits -= crewSalariesOwed;
			crewSalariesOwed = 0;
		}
	}

	// Maintenance costs are dealt with after crew salaries given that they act similarly.
	int64_t maintenancePaid = maintenanceDue;
	if(maintenanceDue)
	{
		if(maintenanceDue > credits)
		{
			// Like with crew salaries, maintenance costs can be paid in part with
			// the unpaid costs being paid later.
			maintenancePaid = max<int64_t>(credits, 0);
			maintenanceDue -= maintenancePaid;
			credits -= maintenancePaid;
			if(!missedPayment)
				out << "You could not pay all your maintenance costs.";
			missedPayment = true;
		}
		else
		{
			credits -= maintenanceDue;
			maintenanceDue = 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;
	int64_t debtPaid = 0;
	for(Mortgage &mortgage : mortgages)
	{
		int64_t payment = mortgage.Payment();
		if(payment > credits)
		{
			mortgage.MissPayment();
			if(!missedPayment)
				out << "You missed a mortgage payment.";
			missedPayment = true;
		}
		else
		{
			payment = mortgage.MakePayment();
			credits -= payment;
			// For the status text, keep track of whether this is a mortgage, fine, or debt.
			if(mortgage.Type() == "Mortgage")
				mortgagesPaid += payment;
			else if(mortgage.Type() == "Fine")
				finesPaid += payment;
			else
				debtPaid += 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 - crewSalariesOwed - maintenanceDue);

	// 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 + (missedPayment ? -5 : 1)));

	// If you didn't make any payments, no need to continue further.
	if(!(salariesPaid + maintenancePaid + mortgagesPaid + finesPaid + debtPaid))
		return out.str();
	else if(missedPayment)
		out << " ";

	out << "You paid ";

	map<string, int64_t> typesPaid;
	if(salariesPaid)
		typesPaid["crew salaries"] = salariesPaid;
	if(maintenancePaid)
		typesPaid["maintenance"] = maintenancePaid;
	if(mortgagesPaid)
		typesPaid["mortgages"] = mortgagesPaid;
	if(finesPaid)
		typesPaid["fines"] = finesPaid;
	if(debtPaid)
		typesPaid["debt"] = debtPaid;

	out << Format::List<map, string, int64_t>(typesPaid,
		[](const pair<string, int64_t> &it)
		{
			return Format::CreditString(it.second) + " in " + it.first;
		});
	out << '.';
	return out.str();
}



const map<string, int64_t> &Account::SalariesIncome() const
{
	return salariesIncome;
}



int64_t Account::SalariesIncomeTotal() const
{
	return accumulate(
		salariesIncome.begin(),
		salariesIncome.end(),
		0,
		[](int64_t value, const std::map<string, int64_t>::value_type &salary)
		{
			return value + salary.second;
		}
	);
}



void Account::SetSalaryIncome(const string &name, int64_t amount)
{
	if(amount == 0)
		salariesIncome.erase(name);
	else
		salariesIncome[name] = amount;
}



int64_t Account::CrewSalariesOwed() const
{
	return crewSalariesOwed;
}



void Account::PaySalaries(int64_t amount)
{
	amount = min(min(amount, crewSalariesOwed), credits);
	credits -= amount;
	crewSalariesOwed -= amount;
}



int64_t Account::MaintenanceDue() const
{
	return maintenanceDue;
}



void Account::PayMaintenance(int64_t amount)
{
	amount = min(min(amount, maintenanceDue), credits);
	credits -= amount;
	maintenanceDue -= 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("Mortgage", 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("Fine", amount, 0, 60);
}



// Add debt with the given interest rate and term. If no interest rate is
// given then the player's credit score is used to determine the interest rate.
void Account::AddDebt(int64_t amount, optional<double> interest, int term)
{
	if(interest)
		mortgages.emplace_back("Debt", amount, *interest, term);
	else
		mortgages.emplace_back("Debt", amount, creditScore, term);
}



// Check how big a mortgage the player can afford to pay at their current income.
int64_t Account::Prequalify() const
{
	double payments = 0.;
	int64_t liabilities = 0;
	for(const Mortgage &mortgage : mortgages)
	{
		payments += mortgage.PrecisePayment();
		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 a specific type of mortgage, or all
// mortgages if a blank string is provided.
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;
}