File: News.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 (165 lines) | stat: -rw-r--r-- 4,546 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
/* News.cpp
Copyright (c) 2017 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 "News.h"

#include "DataNode.h"
#include "Random.h"
#include "image/SpriteSet.h"

#include <algorithm>

using namespace std;



void News::Load(const DataNode &node, const ConditionsStore *playerConditions,
	const set<const System *> *visitedSystems, const set<const Planet *> *visitedPlanets)
{
	for(const DataNode &child : node)
	{
		const bool add = (child.Token(0) == "add");
		const 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 &tag = child.Token((add || remove) ? 1 : 0);
		const int valueIndex = (add || remove) ? 2 : 1;
		const bool hasValue = child.Size() > valueIndex;

		if(tag == "location")
		{
			if(add && !location.IsEmpty())
				child.PrintTrace("Error: Cannot \"add\" to an existing location filter:");
			else if(remove)
			{
				location = LocationFilter{};
				if(child.HasChildren())
					child.PrintTrace("Warning: Removing full location filter; partial removal is not supported:");
			}
			else
				location.Load(child, visitedSystems, visitedPlanets);
		}
		else if(tag == "name")
		{
			if(remove)
			{
				speakerNames = Phrase{};
				if(child.HasChildren())
					child.PrintTrace("Warning: Removing all names; removal of individual names is not supported:");
			}
			else
				speakerNames.Load(child);
		}
		else if(tag == "portrait")
		{
			if(remove && !hasValue)
				portraits.clear();
			else if(remove)
			{
				// Collect all values to be removed.
				auto toRemove = set<const Sprite *>{};
				for(int i = valueIndex; i < child.Size(); ++i)
					toRemove.emplace(SpriteSet::Get(child.Token(i)));

				// Erase them in unison.
				portraits.erase(remove_if(portraits.begin(), portraits.end(),
						[&toRemove](const Sprite *sprite) { return toRemove.find(sprite) != toRemove.end(); }),
					portraits.end());
			}
			else
			{
				for(int i = valueIndex; i < child.Size(); ++i)
					portraits.push_back(SpriteSet::Get(child.Token(i)));
				for(const DataNode &grand : child)
					portraits.push_back(SpriteSet::Get(grand.Token(0)));
			}
		}
		else if(tag == "message")
		{
			if(remove)
			{
				messages = Phrase{};
				if(child.HasChildren())
					child.PrintTrace("Warning: Removing all messages; removal of single messages is not supported:");
			}
			else
				messages.Load(child);
		}
		else if(tag == "to" && hasValue && child.Token(valueIndex) == "show")
		{
			if(add && !toShow.IsEmpty())
				child.PrintTrace("Error: Cannot \"add\" to an existing condition set:");
			else if(remove)
			{
				toShow = ConditionSet{};
				if(child.HasChildren())
					child.PrintTrace("Warning: Removing all conditions; removal of condition subsets is not supported:");

			}
			else
				toShow.Load(child, playerConditions);
		}
		else
			child.PrintTrace("Skipping unrecognized attribute:");
	}
}



bool News::IsEmpty() const
{
	return messages.IsEmpty() || speakerNames.IsEmpty();
}



// Check if this news item is available given the player's planet.
bool News::Matches(const Planet *planet) const
{
	// If no location filter is specified, it should never match. This can be
	// used to create news items that are never shown until an event "activates"
	// them by specifying their location.
	// Similarly, by updating a news item with "remove location", it can be deactivated.
	return location.IsEmpty() ? false : (location.Matches(planet) && toShow.Test());
}



// Get the speaker's name.
string News::SpeakerName() const
{
	return speakerNames.Get();
}



// Pick a portrait at random out of the possible options.
const Sprite *News::Portrait() const
{
	return portraits.empty() ? nullptr : portraits[Random::Int(portraits.size())];
}



// Get the speaker's message, chosen randomly.
string News::Message() const
{
	return messages.Get();
}