-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathMap.cpp
256 lines (183 loc) · 5.55 KB
/
Map.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
/* Map.cpp
Copyright (c) 2015 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 "Map.h"
#include "DataFile.h"
#include "DataWriter.h"
#include "SpriteSet.h"
#include <QFileInfo>
#include <QString>
#include <algorithm>
using namespace std;
void Map::Load(const QString &path)
{
// Clear everything first.
*this = Map();
QFileInfo p = QFileInfo(path);
dataDirectory = p.absolutePath();
fileName = p.fileName();
QString rootDir = dataDirectory.left(dataDirectory.lastIndexOf('/'));
dataDirectory += "/";
SpriteSet::SetRootPath(rootDir + "/images/");
DataFile data(path);
comments = data.Comments();
for(const DataNode &node : data)
{
if(node.Token(0) == "planet" && node.Size() >= 2)
planets[node.Token(1)].Load(node);
else if(node.Token(0) == "system" && node.Size() >= 2)
systems[node.Token(1)].Load(node);
else if(node.Token(0) == "galaxy")
galaxies.emplace_back(node);
else
unparsed.push_back(node);
}
QString commodityPath = dataDirectory + "commodities.txt";
DataFile tradeData(commodityPath);
// Load in "standard" commodities - those that supply a category, low, and high price.
// "Special" commodities that are only used as names for mission cargo are not loaded.
for(const DataNode &node : tradeData)
if(node.Token(0) == "trade")
for(const DataNode &child : node)
if(child.Token(0) == "commodity" && child.Size() >= 4)
commodities.emplace_back(child.Token(1), child.Value(2), child.Value(3));
isChanged = false;
}
void Map::Save(const QString &path)
{
QFileInfo p = QFileInfo(path);
fileName = p.fileName();
DataWriter file(path);
file.WriteRaw(comments);
file.Write();
for(const Galaxy &it : galaxies)
{
it.Save(file);
file.Write();
}
for(const auto &it : systems)
{
it.second.Save(file);
file.Write();
}
for(const auto &it : planets)
{
it.second.Save(file);
file.Write();
}
for(const auto &it : unparsed)
{
file.Write(it);
file.Write();
}
isChanged = false;
}
const QString &Map::DataDirectory() const
{
return dataDirectory;
}
const QString &Map::FileName() const
{
return fileName;
}
void Map::SetChanged(bool changed)
{
isChanged = changed;
}
bool Map::IsChanged() const
{
return isChanged;
}
list<Galaxy> &Map::Galaxies()
{
return galaxies;
}
const list<Galaxy> &Map::Galaxies() const
{
return galaxies;
}
map<QString, System> &Map::Systems()
{
return systems;
}
const map<QString, System> &Map::Systems() const
{
return systems;
}
map<QString, Planet> &Map::Planets()
{
return planets;
}
const map<QString, Planet> &Map::Planets() const
{
return planets;
}
const vector<Map::Commodity> &Map::Commodities() const
{
return commodities;
}
// Map a price to a value between 0 and 1 (lowest vs. highest).
double Map::MapPrice(const QString &commodity, int price) const
{
for(const Commodity &it : commodities)
if(it.name == commodity)
return max(0., min(1., static_cast<double>(price - it.low) / (it.high - it.low)));
return .5;
}
QString Map::PriceLevel(const QString &commodity, int price) const
{
static const QString LEVEL[] = {
"(very low)",
"(low)",
"(medium)",
"(high)",
"(very high)"
};
for(const Commodity &it : commodities)
if(it.name == commodity)
{
int level = max(0, min(4, ((price - it.low) * 5) / (it.high - it.low)));
return LEVEL[level];
}
return "";
}
// Rename a system. This requires updating all the known systems that link to it.
void Map::RenameSystem(const QString &from, const QString &to)
{
// If the desired name is taken, or the current name doesn't exist, bail out.
if(systems.count(to) || !systems.count(from))
return;
System &renamed = systems[to] = systems[from];
renamed.SetName(to);
// Links to "plugin" systems (i.e. those not a part of this map file)
// are kept, but the returning link from the plugin system to this
// system will not exist. (There is no way to update it.)
for(const QString &link : systems[from].Links())
if(systems.count(link))
systems[link].ChangeLink(from, to);
// Erase the original name's system definition.
systems.erase(from);
}
// Rename a planet. The editor does not support planets sharing a name with
// a system, or renaming an object to share a planet definition (i.e. wormholes).
void Map::RenamePlanet(StellarObject *object, const QString &name)
{
if(!object || systems.find(name) != systems.end())
return;
auto it = planets.find(object->GetPlanet());
if(it != planets.end())
{
// Copy the existing definition to the new name.
planets[name] = it->second;
// Erase the previous definition.
planets.erase(it);
}
planets[name].SetName(name);
object->SetPlanet(name);
}