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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
|
/* Planet.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 "Planet.h"
#include "DataNode.h"
#include "text/Format.h"
#include "GameData.h"
#include "Government.h"
#include "Logger.h"
#include "PlayerInfo.h"
#include "Politics.h"
#include "Random.h"
#include "Ship.h"
#include "ShipEvent.h"
#include "image/SpriteSet.h"
#include "System.h"
#include "Wormhole.h"
#include <algorithm>
using namespace std;
namespace {
const string WORMHOLE = "wormhole";
const string PLANET = "planet";
// Planet attributes in the form "requires: <attribute>" restrict the ability of ships to land
// unless the ship has all required attributes.
void SetRequiredAttributes(const set<string> &attributes, set<string> &required)
{
static const string PREFIX = "requires: ";
static const string PREFIX_END = "requires:!";
required.clear();
for_each(attributes.lower_bound(PREFIX), attributes.lower_bound(PREFIX_END), [&](const string &attribute)
{
required.emplace_hint(required.cend(), attribute.substr(PREFIX.length()));
});
}
}
// Load a planet's description from a file.
void Planet::Load(const DataNode &node, Set<Wormhole> &wormholes, const ConditionsStore *playerConditions)
{
if(node.Size() < 2)
return;
trueName = node.Token(1);
// The planet's name is needed to save references to this object, so a
// flag is used to test whether Load() was called at least once for it.
isDefined = true;
// If this planet has been loaded before, these sets of items should be
// reset instead of appending to them:
set<string> shouldOverwrite = {"attributes", "description", "spaceport", "port"};
for(const DataNode &child : node)
{
// Check for the "add" or "remove" keyword.
bool add = (child.Token(0) == "add");
bool remove = (child.Token(0) == "remove");
if((add || remove) && child.Size() < 2)
{
child.PrintTrace("Skipping " + child.Token(0) + " with no key given:");
continue;
}
// Get the key and value (if any).
const string &key = child.Token((add || remove) ? 1 : 0);
int valueIndex = (add || remove) ? 2 : 1;
bool hasValue = (child.Size() > valueIndex);
const string &value = child.Token(hasValue ? valueIndex : 0);
// Check for conditions that require clearing this key's current value.
// "remove <key>" means to clear the key's previous contents.
// "remove <key> <value>" means to remove just that value from the key.
bool removeAll = (remove && !hasValue);
// "<key> clear" is the deprecated way of writing "remove <key>."
removeAll |= (!add && !remove && hasValue && value == "clear");
// If this is the first entry for the given key, and we are not in "add"
// or "remove" mode, its previous value should be cleared.
bool overwriteAll = (!add && !remove && !removeAll && shouldOverwrite.contains(key));
// Clear the data of the given type.
if(removeAll || overwriteAll)
{
// Clear the data of the given type.
if(key == "display name")
displayName.clear();
else if(key == "music")
music.clear();
else if(key == "attributes")
attributes.clear();
else if(key == "description")
description.Clear();
else if(key == "port" || key == "spaceport")
{
port = Port();
// Overwriting either port or spaceport counts as overwriting the other.
if(overwriteAll)
{
shouldOverwrite.erase("port");
shouldOverwrite.erase("spaceport");
}
}
else if(key == "shipyard")
shipSales.clear();
else if(key == "outfitter")
outfitSales.clear();
else if(key == "government")
government = nullptr;
else if(key == "required reputation")
requiredReputation = 0.;
else if(key == "bribe")
bribe = 0.;
else if(key == "security")
security = 0.;
else if(key == "tribute")
{
tribute = 0;
defenseThreshold = 4000;
defenseFleets.clear();
ResetDefense();
}
else if(key == "wormhole")
wormhole = nullptr;
// If not in "overwrite" mode, move on to the next node.
if(overwriteAll)
shouldOverwrite.erase(key);
else
continue;
}
if(key == "port")
port.Load(child, playerConditions);
// Handle the attributes which can be "removed."
else if(!hasValue)
{
child.PrintTrace("Error: Expected key to have a value:");
continue;
}
else if(key == "attributes")
{
if(remove)
for(int i = valueIndex; i < child.Size(); ++i)
attributes.erase(child.Token(i));
else
for(int i = valueIndex; i < child.Size(); ++i)
attributes.insert(child.Token(i));
}
else if(key == "shipyard")
{
if(remove)
shipSales.erase(GameData::Shipyards().Get(value));
else
shipSales.insert(GameData::Shipyards().Get(value));
}
else if(key == "outfitter")
{
if(remove)
outfitSales.erase(GameData::Outfitters().Get(value));
else
outfitSales.insert(GameData::Outfitters().Get(value));
}
// Handle the attributes which cannot be "removed."
else if(remove)
{
child.PrintTrace("Error: Cannot \"remove\" a specific value from the given key:");
continue;
}
else if(key == "display name")
displayName = value;
else if(key == "landscape")
landscape = SpriteSet::Get(value);
else if(key == "music")
music = value;
else if(key == "description")
description.Load(child, playerConditions);
else if(key == "spaceport")
{
port.LoadDefaultSpaceport();
port.LoadDescription(child, playerConditions);
}
else if(key == "government")
government = GameData::Governments().Get(value);
else if(key == "required reputation")
requiredReputation = child.Value(valueIndex);
else if(key == "bribe")
bribe = child.Value(valueIndex);
else if(key == "security")
{
customSecurity = true;
security = child.Value(valueIndex);
}
else if(key == "tribute")
{
tribute = child.Value(valueIndex);
bool resetFleets = !defenseFleets.empty();
for(const DataNode &grand : child)
{
const string &grandKey = grand.Token(0);
bool grandHasValue = grand.Size() >= 2;
if(grandKey == "threshold" && grandHasValue)
defenseThreshold = grand.Value(1);
else if(grandKey == "fleet")
{
if(grandHasValue && !grand.HasChildren())
{
// Allow only one "tribute" node to define the tribute fleets.
if(resetFleets)
{
defenseFleets.clear();
resetFleets = false;
}
defenseFleets.insert(defenseFleets.end(),
grand.Size() >= 3 ? grand.Value(2) : 1,
GameData::Fleets().Get(grand.Token(1))
);
}
else
grand.PrintTrace("Skipping unsupported tribute fleet definition:");
}
else
grand.PrintTrace("Skipping unrecognized tribute attribute:");
}
}
else if(key == "wormhole")
{
wormhole = wormholes.Get(value);
wormhole->SetPlanet(*this);
}
else
child.PrintTrace("Skipping unrecognized attribute:");
}
// For reverse compatibility, if this planet has a spaceport but it was not custom loaded,
// and the planet has the "uninhabited" attribute, replace the spaceport with a special-case
// uninhabited spaceport.
if(HasNamedPort() && !port.CustomLoaded())
{
if(attributes.contains("uninhabited"))
port.LoadUninhabitedSpaceport();
else
port.LoadDefaultSpaceport();
}
// Apply any auto-attributes to this planet depending on what it has.
static const vector<string> AUTO_ATTRIBUTES = {
"spaceport",
"port",
"shipyard",
"outfitter",
"service: trading",
"service: jobs",
"service: bank",
"service: crew",
"service: missions",
"recharges: shields",
"recharges: hull",
"recharges: energy",
"recharges: fuel",
"spaceport news",
};
bool autoValues[14] = {
port.HasService(Port::ServicesType::All) && port.CanRecharge(Port::RechargeType::All)
&& port.HasNews() && HasNamedPort(),
HasNamedPort(),
!shipSales.empty(),
!outfitSales.empty(),
port.HasService(Port::ServicesType::Trading),
port.HasService(Port::ServicesType::JobBoard),
port.HasService(Port::ServicesType::Bank),
port.HasService(Port::ServicesType::HireCrew),
port.HasService(Port::ServicesType::OffersMissions),
port.CanRecharge(Port::RechargeType::Shields),
port.CanRecharge(Port::RechargeType::Hull),
port.CanRecharge(Port::RechargeType::Energy),
port.CanRecharge(Port::RechargeType::Fuel),
port.HasNews(),
};
for(unsigned i = 0; i < AUTO_ATTRIBUTES.size(); ++i)
{
if(autoValues[i])
attributes.insert(AUTO_ATTRIBUTES[i]);
else
attributes.erase(AUTO_ATTRIBUTES[i]);
}
// Precalculate commonly used values that can only change due to Load().
inhabited = (HasServices() || requiredReputation || !defenseFleets.empty()) && !attributes.contains("uninhabited");
SetRequiredAttributes(Attributes(), requiredAttributes);
}
// Legacy wormhole do not have an associated Wormhole object so
// we must auto generate one if we detect such legacy wormhole.
void Planet::FinishLoading(Set<Wormhole> &wormholes)
{
// If this planet is in multiple systems, then it is a wormhole.
if(!wormhole && systems.size() > 1)
{
wormhole = wormholes.Get(TrueName());
wormhole->LoadFromPlanet(*this);
Logger::LogError("Warning: deprecated automatic generation of wormhole \""
+ trueName + "\" from a multi-system planet.");
}
// If the wormhole was autogenerated we need to update it to
// match the planet's state.
else if(wormhole && wormhole->IsAutogenerated())
wormhole->LoadFromPlanet(*this);
// If this planet is not a wormhole, make sure it has a display name.
else if(!wormhole && displayName.empty())
displayName = trueName;
}
// Test if this planet has been loaded (vs. just referred to). It must also be located in
// at least one system, and all systems that claim it must themselves be valid.
bool Planet::IsValid() const
{
return isDefined && !systems.empty() && all_of(systems.begin(), systems.end(),
[](const System *s) noexcept -> bool { return s->IsValid(); });
}
// Get the name used for this planet in the data files.
const string &Planet::TrueName() const
{
return trueName;
}
void Planet::SetTrueName(const string &name)
{
trueName = name;
if(displayName.empty())
displayName = trueName;
}
// Get the display name of the planet.
const string &Planet::DisplayName() const
{
return IsWormhole() ? wormhole->DisplayName() : displayName;
}
// Get the planet's descriptive text.
const Paragraphs &Planet::Description() const
{
return description;
}
// Get the landscape sprite.
const Sprite *Planet::Landscape() const
{
return landscape;
}
// Get the name of the ambient audio to play on this planet.
const string &Planet::MusicName() const
{
return music;
}
// Get the list of "attributes" of the planet.
const set<string> &Planet::Attributes() const
{
return attributes;
}
// Get planet's noun descriptor from attributes
const string &Planet::Noun() const
{
if(IsWormhole())
return WORMHOLE;
for(const string &attribute : attributes)
if(attribute == "moon" || attribute == "station")
return attribute;
return PLANET;
}
// Check whether this planet's port is named.
bool Planet::HasNamedPort() const
{
return !port.DisplayName().empty();
}
// Get this planet's port.
const Port &Planet::GetPort() const
{
return port;
}
// Check whether there are port services (such as trading, jobs, banking, and hiring)
// available on this planet.
bool Planet::HasServices() const
{
return port.HasServices();
}
// Check if this planet is inhabited (i.e. it has a spaceport, and does not
// have the "uninhabited" attribute).
bool Planet::IsInhabited() const
{
return inhabited;
}
// Check if this planet has a permanent shipyard.
bool Planet::HasShipyard() const
{
return !shipSales.empty();
}
// Get the list of ships in the permanent shipyard.
const Sale<Ship> &Planet::ShipyardStock() const
{
shipyard.clear();
for(const Shop<Ship> *sale : shipSales)
shipyard.Add(sale->Stock());
return shipyard;
}
// Get the list of shipyards currently available on this planet.
// This will include conditionally available shops.
set<const Shop<Ship> *> Planet::Shipyards() const
{
set<const Shop<Ship> *> shops = shipSales;
for(const auto &shop : GameData::Shipyards())
if(shop.second.CanStock(this))
shops.insert(&shop.second);
return shops;
}
// Check if this planet has a permanent outfitter.
bool Planet::HasOutfitter() const
{
return !outfitSales.empty();
}
// Get the list of outfits available from the permanent outfitter.
const Sale<Outfit> &Planet::OutfitterStock() const
{
outfitter.clear();
for(const Shop<Outfit> *sale : outfitSales)
outfitter.Add(sale->Stock());
return outfitter;
}
// Get the list of outitters available on this planet.
// This will include conditionally available shops.
set<const Shop<Outfit> *> Planet::Outfitters() const
{
set<const Shop<Outfit> *> shops = outfitSales;
for(const auto &shop : GameData::Outfitters())
if(shop.second.CanStock(this))
shops.insert(&shop.second);
return shops;
}
// Get this planet's government. Most planets follow the government of the system they are in.
const Government *Planet::GetGovernment() const
{
return government ? government : systems.empty() ? nullptr : GetSystem()->GetGovernment();
}
// You need this good a reputation with this system's government to land here.
double Planet::RequiredReputation() const
{
return requiredReputation;
}
// This is what fraction of your fleet's value you must pay as a bribe in
// order to land on this planet. (If zero, you cannot bribe it.)
double Planet::GetBribeFraction() const
{
return bribe;
}
// This is how likely the planet's authorities are to notice if you are
// doing something illegal.
double Planet::Security() const
{
return security;
}
bool Planet::HasCustomSecurity() const
{
return customSecurity;
}
const System *Planet::GetSystem() const
{
return (systems.empty() ? nullptr : systems.front());
}
// Check if this planet is in the given system. Note that wormholes may be
// in more than one system.
bool Planet::IsInSystem(const System *system) const
{
return (find(systems.begin(), systems.end(), system) != systems.end());
}
void Planet::SetSystem(const System *system)
{
if(find(systems.begin(), systems.end(), system) == systems.end())
systems.push_back(system);
}
// Remove the given system from the list of systems this planet is in. This
// must be done when game events rearrange the planets in a system.
void Planet::RemoveSystem(const System *system)
{
auto it = find(systems.begin(), systems.end(), system);
if(it != systems.end())
systems.erase(it);
}
const vector<const System *> &Planet::Systems() const
{
return systems;
}
// Check if this is a wormhole (that is, it appears in multiple systems).
bool Planet::IsWormhole() const
{
return wormhole;
}
const Wormhole *Planet::GetWormhole() const
{
return wormhole;
}
// Check if the given ship has all the attributes necessary to allow it to
// land on this planet.
bool Planet::IsAccessible(const Ship *ship) const
{
// If this is a wormhole that leads to an inaccessible system, no ship can land here.
if(wormhole && ship && ship->GetSystem() && wormhole->WormholeDestination(*ship->GetSystem()).Inaccessible())
return false;
// If there are no required attributes, then any ship may land here.
if(IsUnrestricted())
return true;
if(!ship)
return false;
const auto &shipAttributes = ship->Attributes();
return all_of(requiredAttributes.cbegin(), requiredAttributes.cend(),
[&](const string &attr) -> bool { return shipAttributes.Get(attr); });
}
// Check if this planet has any required attributes that restrict landability.
bool Planet::IsUnrestricted() const
{
return requiredAttributes.empty();
}
// Below are convenience functions which access the game state in Politics,
// but do so with a less convoluted syntax:
bool Planet::HasFuelFor(const Ship &ship) const
{
return !IsWormhole() && port.CanRecharge(Port::RechargeType::Fuel) && CanLand(ship);
}
bool Planet::CanLand(const Ship &ship) const
{
return IsAccessible(&ship) && GameData::GetPolitics().CanLand(ship, this);
}
bool Planet::CanLand() const
{
return GameData::GetPolitics().CanLand(this);
}
Planet::Friendliness Planet::GetFriendliness() const
{
if(GameData::GetPolitics().HasDominated(this))
return Friendliness::DOMINATED;
else if(GetGovernment()->IsEnemy())
return Friendliness::HOSTILE;
else if(CanLand())
return Friendliness::FRIENDLY;
else
return Friendliness::RESTRICTED;
}
bool Planet::CanUseServices() const
{
return GameData::GetPolitics().CanUseServices(this);
}
void Planet::Bribe(bool fullAccess) const
{
GameData::GetPolitics().BribePlanet(this, fullAccess);
}
// Demand tribute, and get the planet's response.
string Planet::DemandTribute(PlayerInfo &player) const
{
const auto &playerTribute = player.GetTribute();
if(playerTribute.find(this) != playerTribute.end())
return "We are already paying you as much as we can afford.";
if(!tribute || defenseFleets.empty())
return "Please don't joke about that sort of thing.";
if(player.Conditions().Get("combat rating") < defenseThreshold)
return "You're not worthy of our time.";
// The player is scary enough for this planet to take notice. Check whether
// this is the first demand for tribute, or not.
if(!isDefending)
{
isDefending = true;
set<const Government *> toProvoke;
for(const auto &fleet : defenseFleets)
toProvoke.insert(fleet->GetGovernment());
for(const auto &gov : toProvoke)
if(gov)
gov->Offend(ShipEvent::PROVOKE);
// Terrorizing a planet is not taken lightly by it or its allies.
// TODO: Use a distinct event type for the domination system and
// expose syntax for controlling its impact on the targeted government
// and those that know it.
GetGovernment()->Offend(ShipEvent::ATROCITY);
return "Our defense fleet will make short work of you.";
}
// The player has already demanded tribute. Have they defeated the entire defense fleet?
bool isDefeated = (defenseDeployed == defenseFleets.size());
for(const shared_ptr<Ship> &ship : defenders)
if(!ship->IsDisabled() && !ship->IsYours())
{
isDefeated = false;
break;
}
if(!isDefeated)
return "We're not ready to surrender yet.";
player.SetTribute(this, tribute);
return "We surrender. We will pay you " + Format::CreditString(tribute) + " per day to leave us alone.";
}
// While being tributed, attempt to spawn the next specified defense fleet.
void Planet::DeployDefense(list<shared_ptr<Ship>> &ships) const
{
if(!isDefending || Random::Int(60) || defenseDeployed == defenseFleets.size())
return;
auto end = defenders.begin();
if(defenseFleets[defenseDeployed]->IsValid())
defenseFleets[defenseDeployed]->Enter(*GetSystem(), defenders, this);
else
Logger::LogError("Warning: skipped an incomplete defense fleet of planet \"" + trueName + "\".");
ships.insert(ships.begin(), defenders.begin(), end);
// All defenders use a special personality.
Personality defenderPersonality = Personality::Defender();
Personality fighterPersonality = Personality::DefenderFighter();
for(auto it = defenders.begin(); it != end; ++it)
{
(**it).SetPersonality(defenderPersonality);
if((**it).HasBays())
for(auto bay = (**it).Bays().begin(); bay != (**it).Bays().end(); ++bay)
if(bay->ship)
bay->ship->SetPersonality(fighterPersonality);
}
++defenseDeployed;
}
void Planet::ResetDefense() const
{
isDefending = false;
defenseDeployed = 0;
defenders.clear();
}
bool Planet::IsDefending() const
{
return isDefending;
}
|