Skip to content
Navigation Menu
{{ message }}
forked from endless-sky/endless-sky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversation.cpp
More file actions
464 lines (376 loc) · 11.9 KB
/
Copy pathConversation.cpp
File metadata and controls
464 lines (376 loc) · 11.9 KB
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
/* Conversation.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.
*/
#include "Conversation.h"
#include "DataNode.h"
#include "DataWriter.h"
#include "Format.h"
#include "Sprite.h"
#include "SpriteSet.h"
using namespace std;
namespace {
// Lookup table for matching special tokens to enumeration values.
map<string, int> TOKEN_INDEX = {
{"accept", Conversation::ACCEPT},
{"decline", Conversation::DECLINE},
{"defer", Conversation::DEFER},
{"launch", Conversation::LAUNCH},
{"flee", Conversation::FLEE},
{"depart", Conversation::DEPART},
{"die", Conversation::DIE},
{"explode", Conversation::EXPLODE}
};
// Get the index of the given special string. 0 means it is "goto", a number
// less than 0 means it is an outcome, and 1 means no match.
int TokenIndex(const string &token)
{
auto it = TOKEN_INDEX.find(token);
return (it == TOKEN_INDEX.end() ? 0 : it->second);
}
// Map an index back to a string, for saving the conversation to a file.
string TokenName(int index)
{
for(const auto &it : TOKEN_INDEX)
if(it.second == index)
return it.first;
return to_string(index);
}
// Write a "goto" or endpoint.
void WriteToken(int index, DataWriter &out)
{
out.BeginChild();
{
if(index >= 0)
out.Write("goto", index);
else
out.Write(TokenName(index));
}
out.EndChild();
}
}
// The possible outcomes of a conversation:
const int Conversation::ACCEPT;
const int Conversation::DECLINE;
const int Conversation::DEFER;
const int Conversation::LAUNCH;
const int Conversation::FLEE;
const int Conversation::DEPART;
const int Conversation::DIE;
const int Conversation::EXPLODE;
// Check if this conversation outcome requires the player to leave immediately.
bool Conversation::RequiresLaunch(int outcome)
{
return outcome == LAUNCH || outcome == FLEE || outcome == DEPART;
}
// Load a conversation from file.
void Conversation::Load(const DataNode &node)
{
// Make sure this really is a conversation specification.
if(node.Token(0) != "conversation")
return;
// Free any previously loaded data.
nodes.clear();
for(const DataNode &child : node)
{
if(child.Token(0) == "scene" && child.Size() >= 2)
{
// A scene always starts a new text node.
AddNode();
nodes.back().scene = SpriteSet::Get(child.Token(1));
}
else if(child.Token(0) == "label" && child.Size() >= 2)
{
// You cannot merge text above a label with text below it.
if(!nodes.empty())
nodes.back().canMergeOnto = false;
AddLabel(child.Token(1), child);
}
else if(child.Token(0) == "choice")
{
// Create a new node with one or more choices in it.
nodes.emplace_back(true);
for(const DataNode &grand : child)
{
// Store the text of this choice. By default, the choice will
// just bring you to the next node in the script.
nodes.back().data.emplace_back(grand.Token(0), nodes.size());
nodes.back().data.back().first += '\n';
LoadGotos(grand);
}
if(nodes.back().data.empty())
{
child.PrintTrace("Conversation contains an empty \"choice\" node:");
nodes.pop_back();
}
}
else if(child.Token(0) == "name")
{
// A name entry field is just represented as an empty choice node.
nodes.emplace_back(true);
}
else if(child.Token(0) == "branch")
{
// Don't merge "branch" nodes with any other nodes.
nodes.emplace_back();
nodes.back().canMergeOnto = false;
nodes.back().conditions.Load(child);
// A branch should always specify what node to go to if the test is
// true, and may also specify where to go if it is false.
for(int i = 1; i <= 2; ++i)
{
// If no link is provided, just go to the next node.
nodes.back().data.emplace_back("", nodes.size());
if(child.Size() > i)
{
int index = TokenIndex(child.Token(i));
if(!index)
Goto(child.Token(i), nodes.size() - 1, i - 1);
else if(index < 0)
nodes.back().data.back().second = index;
}
}
}
else if(child.Token(0) == "apply")
{
// Don't merge "apply" nodes with any other nodes.
AddNode();
nodes.back().canMergeOnto = false;
nodes.back().conditions.Load(child);
}
else
{
// This is just an ordinary text node.
// If the previous node is a choice, or if the previous node ended
// in a goto, create a new node. Otherwise, just merge this new
// paragraph into the previous node.
if(nodes.empty() || !nodes.back().canMergeOnto)
AddNode();
// Always append a newline to the end of the text.
nodes.back().data.back().first += child.Token(0);
nodes.back().data.back().first += '\n';
// Check whether there is a goto attached to this block of text. If
// so, future nodes can't merge onto this one.
if(LoadGotos(child))
nodes.back().canMergeOnto = false;
}
}
// Display a warning if a label was not resolved.
if(!unresolved.empty())
for(const auto &it : unresolved)
node.PrintTrace("Conversation contains unrecognized label \"" + it.first + "\":");
// Check for any loops in the conversation.
for(const auto &it : labels)
{
int nodeIndex = it.second;
while(nodeIndex >= 0 && Choices(nodeIndex) <= 1)
{
nodeIndex = NextNode(nodeIndex);
if(nodeIndex == it.second)
{
node.PrintTrace("Conversation contains infinite loop beginning with label \"" + it.first + "\":");
nodes.clear();
return;
}
}
}
// Free the working buffers that we no longer need.
labels.clear();
unresolved.clear();
}
// Write a conversation to file.
void Conversation::Save(DataWriter &out) const
{
out.Write("conversation");
out.BeginChild();
{
for(unsigned i = 0; i < nodes.size(); ++i)
{
// The original label names are not preserved anywhere. Instead,
// the label for every node is just its node index.
out.Write("label", i);
const Node &node = nodes[i];
if(node.scene)
out.Write("scene", node.scene->Name());
if(!node.conditions.IsEmpty())
{
// The only thing differentiating a "branch" from an "apply" node
// is that a branch has two data entries instead of one.
if(node.data.size() > 1)
out.Write("branch", TokenName(node.data[0].second), TokenName(node.data[1].second));
else
out.Write("apply", TokenName(node.data[0].second));
// Write the condition set as a child of this node.
out.BeginChild();
{
node.conditions.Save(out);
}
out.EndChild();
continue;
}
if(node.isChoice)
{
out.Write(node.data.empty() ? "name" : "choice");
out.BeginChild();
}
for(const auto &it : node.data)
{
// Break the text up into paragraphs.
for(const string &line : Format::Split(it.first, "\n"))
out.Write(line);
// Check what node the conversation goes to after this.
int index = it.second;
if(index > 0 && static_cast<unsigned>(index) >= nodes.size())
index = Conversation::DECLINE;
// Write the node that we go to next after this.
WriteToken(index, out);
}
if(node.isChoice)
out.EndChild();
}
}
out.EndChild();
}
// Check if this conversation contains any data.
bool Conversation::IsEmpty() const
{
return nodes.empty();
}
// Do text replacement throughout this conversation.
Conversation Conversation::Substitute(const map<string, string> &subs) const
{
Conversation result = *this;
for(Node &node : result.nodes)
for(pair<string, int> &choice : node.data)
choice.first = Format::Replace(choice.first, subs);
return result;
}
// Check if the given conversation node is a choice node.
bool Conversation::IsChoice(int node) const
{
if(static_cast<unsigned>(node) >= nodes.size())
return false;
return nodes[node].isChoice;
}
// If the given node is a choice node, check how many choices it offers.
int Conversation::Choices(int node) const
{
if(static_cast<unsigned>(node) >= nodes.size())
return 0;
return nodes[node].isChoice ? nodes[node].data.size() : 0;
}
// Check if the given converation node is a conditional branch.
bool Conversation::IsBranch(int node) const
{
if(static_cast<unsigned>(node) >= nodes.size())
return false;
return !nodes[node].conditions.IsEmpty() && nodes[node].data.size() > 1;
}
// Check if the given converation node applies changes to condition variables.
bool Conversation::IsApply(int node) const
{
if(static_cast<unsigned>(node) >= nodes.size())
return false;
return !nodes[node].conditions.IsEmpty() && nodes[node].data.size() == 1;
}
// Get the list of conditions that the given node tests or applies.
const ConditionSet &Conversation::Conditions(int node) const
{
static ConditionSet empty;
if(static_cast<unsigned>(node) >= nodes.size())
return empty;
return nodes[node].conditions;
}
// Get the text of the given choice of the given node.
const string &Conversation::Text(int node, int choice) const
{
static const string empty;
if(static_cast<unsigned>(node) >= nodes.size()
|| static_cast<unsigned>(choice) >= nodes[node].data.size())
return empty;
return nodes[node].data[choice].first;
}
// Get the scene image, if any, associated with the given node.
const Sprite *Conversation::Scene(int node) const
{
if(static_cast<unsigned>(node) >= nodes.size())
return nullptr;
return nodes[node].scene;
}
// Find out where the conversation goes if the given option is chosen.
int Conversation::NextNode(int node, int choice) const
{
if(static_cast<unsigned>(node) >= nodes.size()
|| static_cast<unsigned>(choice) >= nodes[node].data.size())
return DECLINE;
return nodes[node].data[choice].second;
}
// Parse the children of the given node to see if then contain any "gotos."
// If so, link them up properly. Return true if gotos were found.
bool Conversation::LoadGotos(const DataNode &node)
{
bool hasGoto = false;
for(const DataNode &child : node)
{
if(hasGoto)
child.PrintTrace("Ignoring extra text in conversation choice:");
else if(child.Size() == 2 && child.Token(0) == "goto")
{
// Each choice can only have one goto
Goto(child.Token(1), nodes.size() - 1, nodes.back().data.size() - 1);
hasGoto = true;
}
else
{
// Check if this is a recognized endpoint name.
int index = TokenIndex(child.Token(0));
if(child.Size() == 1 && index < 0)
{
nodes.back().data.back().second = index;
hasGoto = true;
}
else
child.PrintTrace("Expected goto or endpoint in conversation, found this:");
}
}
return hasGoto;
}
// Add a label, pointing to whatever node is created next.
void Conversation::AddLabel(const string &label, const DataNode &node)
{
if(labels.count(label))
{
node.PrintTrace("Conversation: label \"" + label + "\" is used more than once:");
return;
}
// If there are any unresolved references to this label, we can now set
// their indices correctly.
auto range = unresolved.equal_range(label);
for(auto it = range.first; it != range.second; ++it)
nodes[it->second.first].data[it->second.second].second = nodes.size();
unresolved.erase(range.first, range.second);
// Remember what index this label points to.
labels[label] = nodes.size();
}
// Set up a "goto". Depending on whether the named label has been seen yet
// or not, it is either resolved immediately or added to the unresolved set.
void Conversation::Goto(const string &label, int node, int choice)
{
auto it = labels.find(label);
if(it == labels.end())
unresolved.insert({label, {node, choice}});
else
nodes[node].data[choice].second = it->second;
}
// Add an "empty" node. It will contain one empty line of text, with its
// goto link set to fall through to the next node.
void Conversation::AddNode()
{
nodes.emplace_back();
nodes.back().data.emplace_back("", nodes.size());
}
You can’t perform that action at this time.
