File: Politics.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 (410 lines) | stat: -rw-r--r-- 10,817 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
/* Politics.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 "Politics.h"

#include "text/Format.h"
#include "GameData.h"
#include "Government.h"
#include "Planet.h"
#include "PlayerInfo.h"
#include "Random.h"
#include "Ship.h"
#include "ShipEvent.h"

#include <algorithm>
#include <cmath>

using namespace std;



namespace {
	// Check if the ship evades being cargo scanned.
	bool EvadesCargoScan(const Ship &ship)
	{
		// Illegal goods can be hidden inside legal goods to avoid detection.
		const int contraband = ship.Cargo().IllegalCargoAmount();
		const int netIllegalCargo = contraband - ship.Attributes().Get("scan concealment");
		if(netIllegalCargo <= 0)
			return true;

		const int legalGoods = ship.Cargo().Used() - contraband;
		const double illegalRatio = legalGoods ? max(1., 2. * netIllegalCargo / legalGoods) : 1.;
		const double scanChance = illegalRatio / (1. + ship.Attributes().Get("scan interference"));
		return Random::Real() > scanChance;
	}

	// Check if the ship evades being outfit scanned.
	bool EvadesOutfitScan(const Ship &ship)
	{
		return ship.Attributes().Get("inscrutable") > 0. ||
				Random::Real() > 1. / (1. + ship.Attributes().Get("scan interference"));
	}
}



// Reset to the initial political state defined in the game data.
void Politics::Reset()
{
	reputationWith.clear();
	dominatedPlanets.clear();
	ResetDaily();

	for(const auto &it : GameData::Governments())
		reputationWith[&it.second] = it.second.InitialPlayerReputation();

	// Disable fines for today (because the game was just loaded, so any fines
	// were already checked for when you first landed).
	for(const auto &it : GameData::Governments())
		fined.insert(&it.second);
}



bool Politics::IsEnemy(const Government *first, const Government *second) const
{
	if(!first || !second)
		return false;

	if(first == second)
		return false;

	// Just for simplicity, if one of the governments is the player, make sure
	// it is the first one.
	if(second->IsPlayer())
		swap(first, second);
	if(first->IsPlayer())
	{
		if(bribed.contains(second))
			return false;
		if(provoked.contains(second))
			return true;

		auto it = reputationWith.find(second);
		return (it != reputationWith.end() && it->second < 0.);
	}

	// Neither government is the player, so the question of enemies depends only
	// on the attitude matrix.
	return (first->AttitudeToward(second) < 0. || second->AttitudeToward(first) < 0.);
}



// Commit the given "offense" against the given government (which may not
// actually consider it to be an offense). This may result in temporary
// hostilities (if the even type is PROVOKE), or a permanent change to your
// reputation.
void Politics::Offend(const Government *gov, int eventType, int count)
{
	if(!gov)
		return;

	if(gov->IsPlayer())
		return;

	for(const auto &it : GameData::Governments())
	{
		const Government *other = &it.second;
		double weight = other->AttitudeToward(gov);

		// You can provoke a government even by attacking an empty ship, such as
		// a drone (count = 0, because count = crew).
		if(eventType & ShipEvent::PROVOKE)
		{
			if(weight > 0.)
			{
				// If you bribe a government but then attack it, the effect of
				// your bribe is canceled out.
				bribed.erase(other);
				provoked.insert(other);
			}
		}
		if(count && abs(weight) >= .05)
		{
			// Weights less than 5% should never cause permanent reputation
			// changes. This is to allow two governments to be hostile or
			// friendly without the player's behavior toward one of them
			// influencing their reputation with the other.
			double penalty = (count * weight) * other->PenaltyFor(eventType, gov);
			if(eventType & ShipEvent::ATROCITY && weight > 0)
				Politics::SetReputation(other, min(0., reputationWith[other]));

			Politics::AddReputation(other, -penalty);
		}
	}
}



// Bribe the given government to be friendly to you for one day.
void Politics::Bribe(const Government *gov)
{
	bribed.insert(gov);
	provoked.erase(gov);
	fined.insert(gov);
}



// Check if the given ship can land on the given planet.
bool Politics::CanLand(const Ship &ship, const Planet *planet) const
{
	if(!planet || !planet->GetSystem())
		return false;

	const Government *gov = ship.GetGovernment();
	if(!gov->IsPlayer())
		return !ship.IsRestrictedFrom(*planet) &&
			(!planet->IsInhabited() || !IsEnemy(gov, planet->GetGovernment()));

	return CanLand(planet);
}



bool Politics::CanLand(const Planet *planet) const
{
	if(!planet || !planet->GetSystem())
		return false;
	if(!planet->IsInhabited())
		return true;
	if(dominatedPlanets.contains(planet))
		return true;
	if(bribedPlanets.contains(planet))
		return true;
	if(provoked.contains(planet->GetGovernment()))
		return false;

	return Reputation(planet->GetGovernment()) >= planet->RequiredReputation();
}



bool Politics::CanUseServices(const Planet *planet) const
{
	if(!planet || !planet->GetSystem())
		return false;
	if(dominatedPlanets.contains(planet))
		return true;

	auto it = bribedPlanets.find(planet);
	if(it != bribedPlanets.end())
		return it->second;

	return Reputation(planet->GetGovernment()) >= planet->RequiredReputation();
}



// Bribe a planet to let the player's ships land there.
void Politics::BribePlanet(const Planet *planet, bool fullAccess)
{
	bribedPlanets[planet] = fullAccess;
}



void Politics::DominatePlanet(const Planet *planet, bool dominate)
{
	if(dominate)
		dominatedPlanets.insert(planet);
	else
		dominatedPlanets.erase(planet);
}



bool Politics::HasDominated(const Planet *planet) const
{
	return dominatedPlanets.contains(planet);
}



// Check to see if the player has done anything they should be fined for.
string Politics::Fine(PlayerInfo &player, const Government *gov, int scan, const Ship *target, double security)
{
	// Do nothing if you have already been fined today, or if you evade
	// detection.
	if(fined.contains(gov) || Random::Real() > security || !gov->GetFineFraction())
		return "";

	string reason;
	int64_t maxFine = 0;
	for(const shared_ptr<Ship> &ship : player.Ships())
	{
		if(target && target != &*ship)
			continue;
		if(ship->GetSystem() != player.GetSystem())
			continue;
		const Planet *planet = player.GetPlanet();
		if(planet && ship->GetPlanet() != planet)
			continue;
		// Skip parked ships. The spaceport authorities are only scanning the ships you just landed with.
		if(ship->IsParked())
			continue;

		int failedMissions = 0;

		// Illegal passengers can only be detected by planetary security.
		if(!scan)
		{
			int64_t fine = ship->Cargo().IllegalPassengersFine(gov);
			if((fine > maxFine && maxFine >= 0) || fine < 0)
			{
				maxFine = fine;
				reason = " for carrying illegal passengers.";

				for(const Mission &mission : player.Missions())
				{
					if(mission.IsFailed())
						continue;

					string fineMessage = mission.FineMessage();
					if(!fineMessage.empty())
					{
						reason = ".\n\t";
						reason.append(fineMessage);
					}
					// Fail any missions with illegal passengers and "stealth" set.
					if(mission.Fine() > 0 && mission.Passengers() && mission.FailIfDiscovered())
					{
						player.FailMission(mission);
						++failedMissions;
					}
				}
			}
		}
		if((!scan || (scan & ShipEvent::SCAN_CARGO)) && !EvadesCargoScan(*ship))
		{
			int64_t fine = ship->Cargo().IllegalCargoFine(gov);
			if((fine > maxFine && maxFine >= 0) || fine < 0)
			{
				maxFine = fine;
				reason = " for carrying illegal cargo.";

				for(const Mission &mission : player.Missions())
				{
					if(mission.IsFailed())
						continue;

					// Append the fineMessage from each applicable mission, if available.
					string fineMessage = mission.FineMessage();
					if(!fineMessage.empty())
					{
						reason = ".\n\t";
						reason.append(fineMessage);
					}
					// Fail any missions with illegal cargo and "stealth" set.
					if(mission.Fine() > 0 && mission.CargoSize() && mission.FailIfDiscovered())
					{
						player.FailMission(mission);
						++failedMissions;
					}
				}
			}
		}
		if((!scan || (scan & ShipEvent::SCAN_OUTFITS)) && !EvadesOutfitScan(*ship))
		{
			for(const auto &it : ship->Outfits())
				if(it.second)
				{
					int fine = gov->Fines(it.first);
					if(gov->Condemns(it.first))
						fine = -1;
					if((fine > maxFine && maxFine >= 0) || fine < 0)
					{
						maxFine = fine;
						reason = " for having illegal outfits installed on your ship.";
					}
				}

			int shipFine = gov->Fines(ship.get());
			if(gov->Condemns(ship.get()))
				shipFine = -1;
			if((shipFine > maxFine && maxFine >= 0) || shipFine < 0)
			{
				maxFine = shipFine;
				reason = " for flying an illegal ship.";
			}
		}
		if(failedMissions && maxFine > 0)
		{
			reason += "\n\tYou failed " + Format::Number(failedMissions)
				+ ((failedMissions > 1) ? " missions" : " mission")
				+ " after your illegal cargo was discovered.";
		}
	}

	if(maxFine < 0)
	{
		gov->Offend(ShipEvent::ATROCITY);
		if(!scan)
			reason = "atrocity";
		else
			reason = "After scanning your ship, the " + gov->DisplayName()
				+ " captain hails you with a grim expression on his face. He says, "
				"\"I'm afraid we're going to have to put you to death " + reason + " Goodbye.\"";
	}
	else if(maxFine > 0)
	{
		// Scale the fine based on how lenient this government is.
		maxFine = lround(maxFine * gov->GetFineFraction());
		reason = "The " + gov->DisplayName() + " authorities fine you "
			+ Format::CreditString(maxFine) + reason;
		player.Accounts().AddFine(maxFine);
		fined.insert(gov);
	}
	return reason;
}



// Get or set your reputation with the given government.
double Politics::Reputation(const Government *gov) const
{
	auto it = reputationWith.find(gov);
	return (it == reputationWith.end() ? 0. : it->second);
}



void Politics::AddReputation(const Government *gov, double value)
{
	SetReputation(gov, reputationWith[gov] + value);
}



void Politics::SetReputation(const Government *gov, double value)
{
	value = min(value, gov->ReputationMax());
	value = max(value, gov->ReputationMin());
	reputationWith[gov] = value;
}



// Reset any temporary provocation (typically because a day has passed).
void Politics::ResetDaily()
{
	provoked.clear();
	bribed.clear();
	bribedPlanets.clear();
	fined.clear();
}