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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
/* UniverseObjects.cpp
Copyright (c) 2021 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 "UniverseObjects.h"
#include "DataFile.h"
#include "DataNode.h"
#include "Files.h"
#include "Information.h"
#include "Logger.h"
#include "PlayerInfo.h"
#include "image/Sprite.h"
#include "image/SpriteSet.h"
#include "TaskQueue.h"
#include <algorithm>
#include <iterator>
#include <map>
#include <set>
#include <utility>
#include <vector>
using namespace std;
shared_future<void> UniverseObjects::Load(TaskQueue &queue, const vector<filesystem::path> &sources,
const PlayerInfo &player, const ConditionsStore *globalConditions, bool debugMode)
{
progress = 0.;
// We need to copy any variables used for loading to avoid a race condition.
// 'this' is not copied, so 'this' shouldn't be accessed after calling this
// function (except for calling GetProgress which is safe due to the atomic).
return queue.Run([this, &player, &sources, globalConditions, debugMode]() noexcept -> void
{
vector<filesystem::path> files;
for(const auto &source : sources)
{
// Iterate through the paths starting with the last directory given. That
// is, things in folders near the start of the path have the ability to
// override things in folders later in the path.
auto list = Files::RecursiveList(source / "data");
files.reserve(files.size() + list.size());
files.insert(files.end(),
make_move_iterator(list.begin()),
make_move_iterator(list.end()));
}
const double step = 1. / (static_cast<int>(files.size()) + 1);
for(const auto &path : files)
{
LoadFile(path, player, globalConditions, debugMode);
// Increment the atomic progress by one step.
// We use acquire + release to prevent any reordering.
auto val = progress.load(memory_order_acquire);
progress.store(val + step, memory_order_release);
}
FinishLoading();
progress = 1.;
});
}
double UniverseObjects::GetProgress() const
{
return progress.load(memory_order_acquire);
}
void UniverseObjects::FinishLoading()
{
for(auto &&it : planets)
it.second.FinishLoading(wormholes);
// Now that all data is loaded, update the neighbor lists and other
// system information. Make sure that the default jump range is among the
// neighbor distances to be updated.
neighborDistances.insert(System::DEFAULT_NEIGHBOR_DISTANCE);
UpdateSystems();
// And, update the ships with the outfits we've now finished loading.
for(auto &&it : ships)
it.second.FinishLoading(true);
for(auto &&it : persons)
it.second.FinishLoading();
// Calculate minable values.
for(auto &&it : minables)
it.second.FinishLoading();
for(auto &&it : startConditions)
it.FinishLoading();
// Remove any invalid starting conditions, so the game does not use incomplete data.
startConditions.erase(remove_if(startConditions.begin(), startConditions.end(),
[](const StartConditions &it) noexcept -> bool { return !it.IsValid(); }),
startConditions.end()
);
// Process any disabled game objects.
for(const auto &category : disabled)
{
if(category.first == "mission")
for(const string &name : category.second)
missions.Get(name)->NeverOffer();
else if(category.first == "event")
for(const string &name : category.second)
events.Get(name)->Disable();
else if(category.first == "person")
for(const string &name : category.second)
persons.Get(name)->NeverSpawn();
else
Logger::LogError("Unhandled \"disable\" keyword of type \"" + category.first + "\"");
}
// Sort all category lists.
for(auto &list : categories)
list.second.Sort();
}
// Apply the given change to the universe.
void UniverseObjects::Change(const DataNode &node, const PlayerInfo &player)
{
const ConditionsStore *playerConditions = &player.Conditions();
const set<const System *> *visitedSystems = &player.VisitedSystems();
const set<const Planet *> *visitedPlanets = &player.VisitedPlanets();
const string &key = node.Token(0);
bool hasValue = node.Size() >= 2;
if(key == "fleet" && hasValue)
fleets.Get(node.Token(1))->Load(node);
else if(key == "galaxy" && hasValue)
galaxies.Get(node.Token(1))->Load(node);
else if(key == "government" && hasValue)
governments.Get(node.Token(1))->Load(node, visitedSystems, visitedPlanets);
else if(key == "outfitter" && hasValue)
outfitSales.Get(node.Token(1))->Load(node, outfits, playerConditions, visitedSystems, visitedPlanets);
else if(key == "planet" && hasValue)
planets.Get(node.Token(1))->Load(node, wormholes, playerConditions);
else if(key == "shipyard" && hasValue)
shipSales.Get(node.Token(1))->Load(node, ships, playerConditions, visitedSystems, visitedPlanets);
else if(key == "system" && hasValue)
systems.Get(node.Token(1))->Load(node, planets, playerConditions);
else if(key == "news" && hasValue)
news.Get(node.Token(1))->Load(node, playerConditions, visitedSystems, visitedPlanets);
else if(key == "link" && node.Size() >= 3)
systems.Get(node.Token(1))->Link(systems.Get(node.Token(2)));
else if(key == "unlink" && node.Size() >= 3)
systems.Get(node.Token(1))->Unlink(systems.Get(node.Token(2)));
else if(key == "substitutions" && node.HasChildren())
substitutions.Load(node, playerConditions);
else if(key == "wormhole" && hasValue)
wormholes.Get(node.Token(1))->Load(node);
else
node.PrintTrace("Error: Invalid \"event\" data:");
}
// Update the neighbor lists and other information for all the systems.
// (This must be done any time a GameEvent creates or moves a system.)
void UniverseObjects::UpdateSystems()
{
for(auto &it : systems)
{
// Skip systems that have no name.
if(it.first.empty() || it.second.TrueName().empty())
continue;
it.second.UpdateSystem(systems, neighborDistances);
// If there were changes to a system there might have been a change to a legacy
// wormhole which we must handle.
for(const auto &object : it.second.Objects())
if(object.GetPlanet())
planets.Get(object.GetPlanet()->TrueName())->FinishLoading(wormholes);
}
}
// Check for objects that are referred to but never defined. Some elements, like
// fleets, don't need to be given a name if undefined. Others (like outfits and
// planets) are written to the player's save and need a name to prevent data loss.
void UniverseObjects::CheckReferences()
{
// Log a warning for an "undefined" class object that was never loaded from disk.
auto Warn = [](const string &noun, const string &name)
{
Logger::LogError("Warning: " + noun + " \"" + name + "\" is referred to, but not fully defined.");
};
// Class objects with a deferred definition should still get named when content is loaded.
auto NameIfDeferred = [](const set<string> &deferred, auto &it)
{
if(deferred.contains(it.first))
it.second.SetTrueName(it.first);
else
return false;
return true;
};
// Set the name of an "undefined" class object, so that it can be written to the player's save.
auto NameAndWarn = [=](const string &noun, auto &it)
{
it.second.SetTrueName(it.first);
Warn(noun, it.first);
};
// Parse all GameEvents for object definitions.
auto deferred = map<string, set<string>>{};
for(auto &&it : events)
{
// Stock GameEvents are serialized in MissionActions by name.
if(it.second.TrueName().empty())
NameAndWarn("event", it);
else
{
// Any already-named event (i.e. loaded) may alter the universe.
auto definitions = GameEvent::DeferredDefinitions(it.second.Changes());
for(auto &&type : definitions)
deferred[type.first].insert(type.second.begin(), type.second.end());
}
}
// Stock conversations are never serialized.
for(const auto &it : conversations)
if(it.second.IsEmpty())
Warn("conversation", it.first);
// The "default intro" conversation must invoke the prompt to set the player's name.
if(!conversations.Get("default intro")->IsValidIntro())
Logger::LogError("Error: the \"default intro\" conversation must contain a \"name\" node.");
// Effects are serialized as a part of ships.
for(auto &&it : effects)
if(it.second.TrueName().empty())
NameAndWarn("effect", it);
// Fleets are not serialized. Any changes via events are written as DataNodes and thus self-define.
for(auto &&it : fleets)
{
// Plugins may alter stock fleets with new variants that exclusively use plugin ships.
// Rather than disable the whole fleet due to these non-instantiable variants, remove them.
it.second.RemoveInvalidVariants();
if(!it.second.IsValid() && !deferred["fleet"].contains(it.first))
Warn("fleet", it.first);
}
// Government names are used in mission NPC blocks and LocationFilters.
for(auto &&it : governments)
if(it.second.TrueName().empty() && !NameIfDeferred(deferred["government"], it))
NameAndWarn("government", it);
// Minables are not serialized.
for(const auto &it : minables)
if(it.second.TrueName().empty())
Warn("minable", it.first);
// Stock missions are never serialized, and an accepted mission is
// always fully defined (though possibly not "valid").
for(const auto &it : missions)
if(it.second.DisplayName().empty())
Warn("mission", it.first);
// News are never serialized or named, except by events (which would then define them).
// Outfit names are used by a number of classes.
for(auto &&it : outfits)
if(it.second.TrueName().empty())
NameAndWarn("outfit", it);
// Phrases are never serialized.
for(const auto &it : phrases)
if(it.second.Name().empty())
Warn("phrase", it.first);
// Planet names are used by a number of classes.
for(auto &&it : planets)
if(it.second.TrueName().empty() && !NameIfDeferred(deferred["planet"], it))
NameAndWarn("planet", it);
// Ship model names are used by missions and depreciation.
for(auto &&it : ships)
if(it.second.TrueModelName().empty())
{
it.second.SetTrueModelName(it.first);
Warn("ship", it.first);
}
// System names are used by a number of classes.
for(auto &&it : systems)
if(it.second.TrueName().empty() && !NameIfDeferred(deferred["system"], it))
NameAndWarn("system", it);
// Hazards are never serialized.
for(const auto &it : hazards)
if(!it.second.IsValid())
Warn("hazard", it.first);
// Wormholes are never serialized.
for(const auto &it : wormholes)
if(it.second.DisplayName().empty())
Warn("wormhole", it.first);
// Formation patterns are not serialized, but their usage is.
for(auto &&it : formations)
if(it.second.TrueName().empty())
NameAndWarn("formation", it);
// Any stock colors should have been loaded from game data files.
for(const auto &it : colors)
if(!it.second.IsLoaded())
Warn("color", it.first);
for(const auto &it : swizzles)
if(!it.second.IsLoaded())
Warn("swizzle", it.first);
// Persons can be referred to when marking them as destroyed.
for(const auto &it : persons)
if(!it.second.IsLoaded())
Warn("person", it.first);
}
void UniverseObjects::LoadFile(const filesystem::path &path, const PlayerInfo &player,
const ConditionsStore *globalConditions, bool debugMode)
{
// This is an ordinary file. Check to see if it is an image.
if(path.extension() != ".txt")
return;
DataFile data(path);
if(debugMode)
Logger::LogError("Parsing: " + path.string());
const ConditionsStore *playerConditions = &player.Conditions();
const set<const System *> *visitedSystems = &player.VisitedSystems();
const set<const Planet *> *visitedPlanets = &player.VisitedPlanets();
for(const DataNode &node : data)
{
const string &key = node.Token(0);
bool hasValue = node.Size() >= 2;
if(key == "color" && node.Size() >= 5)
{
Color *color = colors.Get(node.Token(1));
color->Load(node.Value(2), node.Value(3), node.Value(4), node.Size() >= 6 ? node.Value(5) : 1.);
color->SetTrueName(node.Token(1));
}
else if(key == "swizzle" && hasValue)
swizzles.Get(node.Token(1))->Load(node);
else if(key == "conversation" && hasValue)
conversations.Get(node.Token(1))->Load(node, playerConditions);
else if(key == "effect" && hasValue)
effects.Get(node.Token(1))->Load(node);
else if(key == "event" && hasValue)
events.Get(node.Token(1))->Load(node, playerConditions);
else if(key == "fleet" && hasValue)
fleets.Get(node.Token(1))->Load(node);
else if(key == "formation" && hasValue)
formations.Get(node.Token(1))->Load(node);
else if(key == "galaxy" && hasValue)
galaxies.Get(node.Token(1))->Load(node);
else if(key == "government" && hasValue)
governments.Get(node.Token(1))->Load(node, visitedSystems, visitedPlanets);
else if(key == "hazard" && hasValue)
hazards.Get(node.Token(1))->Load(node);
else if(key == "interface" && hasValue)
{
interfaces.Get(node.Token(1))->Load(node);
// If we modified the "menu background" interface, then
// we also update our cache of it.
if(node.Token(1) == "menu background")
{
lock_guard<mutex> lock(menuBackgroundMutex);
menuBackgroundCache.Load(node);
}
}
else if(key == "minable" && hasValue)
minables.Get(node.Token(1))->Load(node);
else if(key == "mission" && hasValue)
missions.Get(node.Token(1))->Load(node, playerConditions, visitedSystems, visitedPlanets);
else if(key == "outfit" && hasValue)
outfits.Get(node.Token(1))->Load(node, playerConditions);
else if(key == "outfitter" && hasValue)
outfitSales.Get(node.Token(1))->Load(node, outfits, playerConditions, visitedSystems, visitedPlanets);
else if(key == "person" && hasValue)
persons.Get(node.Token(1))->Load(node, playerConditions, visitedSystems, visitedPlanets);
else if(key == "phrase" && hasValue)
phrases.Get(node.Token(1))->Load(node);
else if(key == "planet" && hasValue)
planets.Get(node.Token(1))->Load(node, wormholes, playerConditions);
else if(key == "ship" && hasValue)
{
// Allow multiple named variants of the same ship model.
const string &name = node.Token((node.Size() > 2) ? 2 : 1);
ships.Get(name)->Load(node, playerConditions);
}
else if(key == "shipyard" && hasValue)
shipSales.Get(node.Token(1))->Load(node, ships, playerConditions, visitedSystems, visitedPlanets);
else if(key == "start" && node.HasChildren())
{
// This node may either declare an immutable starting scenario, or one that is open to extension
// by other nodes (e.g. plugins may customize the basic start, rather than provide a unique start).
if(node.Size() == 1)
startConditions.emplace_back(node, globalConditions, playerConditions);
else
{
const string &identifier = node.Token(1);
auto existingStart = find_if(startConditions.begin(), startConditions.end(),
[&identifier](const StartConditions &it) noexcept -> bool { return it.Identifier() == identifier; });
if(existingStart != startConditions.end())
existingStart->Load(node, globalConditions, playerConditions);
else
startConditions.emplace_back(node, globalConditions, playerConditions);
}
}
else if(key == "system" && hasValue)
systems.Get(node.Token(1))->Load(node, planets, playerConditions);
else if(key == "test" && hasValue)
tests.Get(node.Token(1))->Load(node, playerConditions);
else if(key == "test-data" && hasValue)
testDataSets.Get(node.Token(1))->Load(node, path);
else if(key == "trade")
trade.Load(node);
else if(key == "landing message" && hasValue)
{
for(const DataNode &child : node)
landingMessages[SpriteSet::Get(child.Token(0))] = node.Token(1);
}
else if(key == "star" && hasValue)
{
const Sprite *sprite = SpriteSet::Get(node.Token(1));
for(const DataNode &child : node)
{
const string &childKey = child.Token(0);
bool childHasValue = child.Size() >= 2;
if(childKey == "power" && childHasValue)
solarPower[sprite] = child.Value(1);
else if(childKey == "wind" && childHasValue)
solarWind[sprite] = child.Value(1);
else if(childKey == "icon" && childHasValue)
starIcons[sprite] = SpriteSet::Get(child.Token(1));
else
child.PrintTrace("Skipping unrecognized attribute:");
}
}
else if(key == "news" && hasValue)
news.Get(node.Token(1))->Load(node, playerConditions, visitedSystems, visitedPlanets);
else if(key == "rating" && hasValue)
{
vector<string> &list = ratings[node.Token(1)];
list.clear();
for(const DataNode &child : node)
list.push_back(child.Token(0));
}
else if(key == "category" && hasValue)
{
static const map<string, CategoryType> category = {
{"ship", CategoryType::SHIP},
{"bay type", CategoryType::BAY},
{"outfit", CategoryType::OUTFIT},
{"series", CategoryType::SERIES}
};
auto it = category.find(node.Token(1));
if(it == category.end())
{
node.PrintTrace("Skipping unrecognized category type:");
continue;
}
categories[it->second].Load(node);
}
else if((key == "tip" || key == "help") && hasValue)
{
string &text = (key == "tip" ? tooltips : helpMessages)[node.Token(1)];
text.clear();
for(const DataNode &child : node)
{
if(!text.empty())
{
text += '\n';
if(child.Token(0)[0] != '\t')
text += '\t';
}
text += child.Token(0);
}
}
else if(key == "substitutions" && node.HasChildren())
substitutions.Load(node, playerConditions);
else if(key == "wormhole" && hasValue)
wormholes.Get(node.Token(1))->Load(node);
else if(key == "gamerules" && node.HasChildren())
gamerules.Load(node);
else if(key == "disable" && hasValue)
{
static const set<string> canDisable = {"mission", "event", "person"};
const string &category = node.Token(1);
if(canDisable.contains(category))
{
if(node.HasChildren())
for(const DataNode &child : node)
disabled[category].emplace(child.Token(0));
if(node.Size() >= 3)
for(int index = 2; index < node.Size(); ++index)
disabled[category].emplace(node.Token(index));
}
else
node.PrintTrace("Invalid use of keyword \"disable\" for class \"" + category + "\"");
}
else
node.PrintTrace("Skipping unrecognized root object:");
}
}
void UniverseObjects::DrawMenuBackground(Panel *panel) const
{
lock_guard<mutex> lock(menuBackgroundMutex);
menuBackgroundCache.Draw(Information(), panel);
}
|