File: Port.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 (200 lines) | stat: -rw-r--r-- 4,736 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
/* Port.cpp
Copyright (c) 2023 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 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 "Port.h"

#include "DataNode.h"
#include "text/Format.h"
#include "GameData.h"
#include "Government.h"
#include "PlayerInfo.h"
#include "Politics.h"
#include "Random.h"
#include "Ship.h"
#include "ShipEvent.h"
#include "image/SpriteSet.h"
#include "System.h"

#include <algorithm>

using namespace std;

namespace {
	const string SPACEPORT = "Space_port";
}



// Load a port's description from a node.
void Port::Load(const DataNode &node, const ConditionsStore *playerConditions)
{
	loaded = true;
	const int nameIndex = 1 + (node.Token(0) == "add");
	if(node.Size() > nameIndex)
		displayName = node.Token(nameIndex);

	for(const DataNode &child : node)
	{
		const string &key = child.Token(0);
		bool hasValue = child.Size() >= 2;
		if(key == "recharges" && (child.HasChildren() || hasValue))
		{
			auto setRecharge = [&](const DataNode &valueNode, const string &value) noexcept -> void {
				if(value == "all")
					recharge |= RechargeType::All;
				else if(value == "shields")
					recharge |= RechargeType::Shields;
				else if(value == "hull")
					recharge |= RechargeType::Hull;
				else if(value == "energy")
					recharge |= RechargeType::Energy;
				else if(value == "fuel")
					recharge |= RechargeType::Fuel;
				else
					valueNode.PrintTrace("Skipping unrecognized attribute:");
			};
			for(int i = 1; i < child.Size(); ++i)
				setRecharge(child, child.Token(i));
			for(const DataNode &grand : child)
				setRecharge(grand, grand.Token(0));
		}
		else if(key == "services" && (child.HasChildren() || hasValue))
		{
			auto setServices = [&](const DataNode &valueNode, const string &value) noexcept -> void {
				if(value == "all")
					services |= ServicesType::All;
				else if(value == "trading")
					services |= ServicesType::Trading;
				else if(value == "job board")
					services |= ServicesType::JobBoard;
				else if(value == "bank")
					services |= ServicesType::Bank;
				else if(value == "hire crew")
					services |= ServicesType::HireCrew;
				else if(value == "offers missions")
					services |= ServicesType::OffersMissions;
				else
					valueNode.PrintTrace("Skipping unrecognized attribute:");
			};
			for(int i = 1; i < child.Size(); ++i)
				setServices(child, child.Token(i));
			for(const DataNode &grand : child)
				setServices(grand, grand.Token(0));
		}
		else if(key == "news")
			hasNews = true;
		else if(key == "description" && hasValue)
		{
			description.Load(child, playerConditions);

			// If we have a description but no name then use the default spaceport name.
			if(displayName.empty())
				displayName = SPACEPORT;
		}
		else
			child.PrintTrace("Skipping unrecognized attribute:");
	}
}



void Port::LoadDefaultSpaceport()
{
	displayName = SPACEPORT;
	recharge = RechargeType::All;
	services = ServicesType::All;
	hasNews = true;
}



void Port::LoadUninhabitedSpaceport()
{
	displayName = SPACEPORT;
	recharge = RechargeType::All;
	services = ServicesType::OffersMissions;
	hasNews = true;
}



void Port::LoadDescription(const DataNode &node, const ConditionsStore *playerConditions)
{
	description.Load(node, playerConditions);
}



bool Port::CustomLoaded() const
{
	return loaded;
}



// Whether this port has any services available.
bool Port::HasServices() const
{
	return services;
}



// Get all the possible sources that can get recharged at this port.
int Port::GetRecharges() const
{
	return recharge;
}



const string &Port::DisplayName() const
{
	return displayName;
}



const Paragraphs &Port::Description() const
{
	return description;
}



// Check whether the given recharging is possible.
bool Port::CanRecharge(int type) const
{
	return (recharge & type);
}



// Check whether the given service is available.
bool Port::HasService(int type) const
{
	return (services & type);
}



bool Port::HasNews() const
{
	return hasNews;
}