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
|
/* GameEvent.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 "GameEvent.h"
#include "DataWriter.h"
#include "GameData.h"
#include "Planet.h"
#include "PlayerInfo.h"
#include "System.h"
#include <algorithm>
using namespace std;
namespace {
const set<string> DEFINITION_NODES = {
"fleet",
"galaxy",
"government",
"outfitter",
"news",
"planet",
"shipyard",
"system",
"substitutions",
"wormhole",
};
}
// Determine the universe object definitions that are defined by the given list of changes.
map<string, set<string>> GameEvent::DeferredDefinitions(const list<DataNode> &changes)
{
auto definitions = map<string, set<string>> {};
for(auto &&node : changes)
if(node.Size() >= 2 && node.HasChildren() && DEFINITION_NODES.contains(node.Token(0)))
{
const string &key = node.Token(0);
const string &name = node.Token(1);
if(key == "system")
{
// A system is only actually defined by this change node if its position is set.
if(any_of(node.begin(), node.end(), [](const DataNode &child) noexcept -> bool
{
return child.Size() >= 3 && child.Token(0) == "pos";
}))
definitions[key].emplace(name);
}
// Since this (or any other) event may be used to assign a planet to a system, we cannot
// do a robust "planet definition" check. Similarly, all other GameEvent-creatable objects
// become valid once they appear as a root-level node that has at least one child node.
else
definitions[key].emplace(name);
}
return definitions;
}
// Construct and Load() at the same time.
GameEvent::GameEvent(const DataNode &node, const ConditionsStore *playerConditions)
{
Load(node, playerConditions);
}
void GameEvent::Load(const DataNode &node, const ConditionsStore *playerConditions)
{
// If the event has a name, a condition should be automatically created that
// represents the fact that this event has occurred.
if(node.Size() >= 2)
{
trueName = node.Token(1);
if(!DataNode::IsConditionName(trueName))
node.PrintTrace("Invalid event/condition name:");
conditionsToApply.AddSetCondition("event: " + trueName, playerConditions);
}
isDefined = true;
static const auto allowedChanges = []() -> set<string>
{
auto allowed = DEFINITION_NODES;
// Include other modifications that cannot create new universe objects.
allowed.insert({
"link",
"unlink",
});
return allowed;
}();
for(const DataNode &child : node)
{
const string &key = child.Token(0);
bool hasValue = child.Size() >= 2;
if(key == "date" && child.Size() >= 4)
date = Date(child.Value(1), child.Value(2), child.Value(3));
else if(key == "unvisit" && hasValue)
systemsToUnvisit.push_back(GameData::Systems().Get(child.Token(1)));
else if(key == "visit" && hasValue)
systemsToVisit.push_back(GameData::Systems().Get(child.Token(1)));
else if(key == "unvisit planet" && hasValue)
planetsToUnvisit.push_back(GameData::Planets().Get(child.Token(1)));
else if(key == "visit planet" && hasValue)
planetsToVisit.push_back(GameData::Planets().Get(child.Token(1)));
else if(allowedChanges.contains(key))
changes.push_back(child);
else
conditionsToApply.Add(child, playerConditions);
}
}
void GameEvent::Save(DataWriter &out) const
{
if(isDisabled)
return;
out.Write("event");
out.BeginChild();
{
if(date)
out.Write("date", date.Day(), date.Month(), date.Year());
conditionsToApply.Save(out);
for(auto &&system : systemsToUnvisit)
out.Write("unvisit", system->TrueName());
for(auto &&planet : planetsToUnvisit)
out.Write("unvisit planet", planet->TrueName());
for(auto &&system : systemsToVisit)
out.Write("visit", system->TrueName());
for(auto &&planet : planetsToVisit)
out.Write("visit planet", planet->TrueName());
for(auto &&change : changes)
out.Write(change);
}
out.EndChild();
}
// Prevent this GameEvent from being applied or written into a player's save.
// (Events read from a save are not associated with the managed Set of GameData::Events.)
void GameEvent::Disable()
{
isDisabled = true;
}
// All events held by GameData have a name, but those loaded from a save do not.
const string &GameEvent::TrueName() const
{
return trueName;
}
// "Stock" GameEvents require a name to be serialized with an accepted mission.
void GameEvent::SetTrueName(const string &name)
{
this->trueName = name;
}
const Date &GameEvent::GetDate() const
{
return date;
}
// Check that this GameEvent has been loaded from a file (vs. referred to only
// by name), and that the systems & planets it references are similarly defined.
// Returns an empty string if it is valid. If not, a reason will be given in the string.
string GameEvent::IsValid() const
{
// When Apply is called, we mutate the universe definition before we update
// the player's knowledge of the universe. Thus, to determine if a system or
// planet is invalid, we must first peek at what `changes` will do.
auto deferred = DeferredDefinitions(changes);
for(auto &&systems : {systemsToVisit, systemsToUnvisit})
for(auto &&system : systems)
if(!system->IsValid() && !deferred["system"].contains(system->TrueName()))
return "contains invalid system \"" + system->TrueName() + "\".";
for(auto &&planets : {planetsToVisit, planetsToUnvisit})
for(auto &&planet : planets)
if(!planet->IsValid() && !deferred["planet"].contains(planet->TrueName()))
return "contains invalid planet \"" + planet->TrueName() + "\".";
return isDefined ? "" : "not defined";
}
void GameEvent::SetDate(const Date &date)
{
this->date = date;
}
// Apply this event's changes to the player. Returns a list of data changes that need to
// be applied in a batch with other events that are applied at the same time.
list<DataNode> GameEvent::Apply(PlayerInfo &player)
{
if(isDisabled)
return {};
// Apply this event's ConditionSet to the player's conditions.
conditionsToApply.Apply();
for(const System *system : systemsToUnvisit)
player.Unvisit(*system);
for(const Planet *planet : planetsToUnvisit)
player.Unvisit(*planet);
// Perform visits after unvisits, as "unvisit <system>"
// will unvisit any planets in that system.
for(const System *system : systemsToVisit)
player.Visit(*system);
for(const Planet *planet : planetsToVisit)
player.Visit(*planet);
// Return this event's data changes so that they can be batch applied
// with the changes from other events.
return std::move(changes);
}
const list<DataNode> &GameEvent::Changes() const
{
return changes;
}
// Date comparison.
bool GameEvent::operator<(const GameEvent &other) const
{
return date < other.date;
}
|