-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNarrativeFlow-Interpreter-Script.cs
More file actions
487 lines (431 loc) · 21.3 KB
/
NarrativeFlow-Interpreter-Script.cs
File metadata and controls
487 lines (431 loc) · 21.3 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* -----
NarrativeFlow Interpreter Script V1.0.1
NarrativeFlow Interpreter Script Code License – Modified MIT License
Copyright (c) 2025 NarrativeFlow LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to use, copy, modify, merge, publish, and distribute the Software, subject to the following conditions:
1. The Software may be used in personal and commercial projects, including but not limited to, games, applications, educational content, and interactive experiences.
2. The Software may be included in commercial products, provided that it is not the primary value of the product (e.g., inclusion in a game or educational course is allowed and encouraged, but selling the code itself as a standalone template or package is not).
3. You may not sell, sublicense, or redistribute the Software on its own, or as part of a product where the main offering is access to the source code or derivative works of the Software, whether free or paid.
4. This copyright notice and permission notice must be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
----- */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
namespace NarrativeFlow {
public class Variable {
public string Name { get; set; }
public string Value { get; set; }
}
public class DataTableItem {
public string Name { get; set; }
public string Value { get; set; }
}
public class Choice {
public string ChoiceString { get; set; }
public int LinksTo { get; set; }
}
public class Assignment {
public string Variable { get; set; }
public string Operator { get; set; }
public string Value { get; set; }
}
public class Comparison {
public string Variable { get; set; }
public string Operator { get; set; }
public string Value { get; set; }
}
public class Route {
public List<Comparison> Comparisons { get; set; }
public string Mode { get; set; }
public string Weight { get; set; }
public int LinksTo { get; set; }
}
public class Property {
public string Name { get; set; }
public string Value { get; set; }
}
public class Node {
public int Id { get; set; }
public string Type { get; set; }
public int? LinksTo { get; set; }
public List<Property> Properties { get; set; }
public string DialogSource { get; set; }
public string DialogString { get; set; }
public List<Choice> Choices { get; set; }
public string Name { get; set; }
public List<Assignment> Assignments { get; set; }
public List<Route> Routes { get; set; }
public int? DefaultRouteLinksTo { get; set; }
public string Statement { get; set; }
public string DestinationExperience { get; set; }
public string DestinationStartNode { get; set; }
}
public class ExperienceData {
public string Name { get; set; }
public List<Node> NodeList { get; set; }
}
public class ProjectData {
public List<ExperienceData> Experiences { get; set; }
public List<Variable> Variables { get; set; }
public List<DataTableItem> DataTable { get; set; }
}
public class Project {
private readonly List<ExperienceData> Experiences;
public readonly List<Variable> Variables;
private readonly List<DataTableItem> DataTable;
public Project(string compiledProjectFileJSONString) {
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var DeserializedProject = JsonSerializer.Deserialize<ProjectData>(compiledProjectFileJSONString, options);
Experiences = DeserializedProject.Experiences;
Variables = DeserializedProject.Variables;
DataTable = DeserializedProject.DataTable;
}
public ExperienceData GetExperience(string experienceName) {
return Experiences.FirstOrDefault(experience => experience.Name == experienceName);
}
public string GetVariableValue(string variableName) {
var variable = Variables.FirstOrDefault(v => v.Name == variableName);
if (variable != null) {
return variable.Value;
}
else {
Console.Error.WriteLine($"Error: Variable with the name '{variableName}' not found.");
return null;
}
}
public bool SetVariableValue(string variableName, string newValue) {
var variable = Variables.FirstOrDefault(v => v.Name == variableName);
if (variable != null) {
variable.Value = newValue;
return true;
}
else {
Console.Error.WriteLine($"Error: Variable with the name '{variableName}' not found.");
return false;
}
}
public string GetVariableParsedString(string str) {
foreach (var variable in Variables) {
string escapedName = System.Text.RegularExpressions.Regex.Escape(variable.Name);
str = System.Text.RegularExpressions.Regex.Replace(str, escapedName, variable.Value);
}
return str;
}
public string GetDataTableItemValue(string itemName) {
var item = DataTable.FirstOrDefault(i => i.Name == itemName);
if (item != null) {
return GetVariableParsedString(item.Value);
}
else {
Console.Error.WriteLine($"Error: Data Table item with the name '{itemName}' not found.");
return null;
}
}
public void ProcessAssignment(Assignment Assignment) {
var VariableValue = GetVariableValue(Assignment.Variable);
double? VariableNumber = double.TryParse(GetVariableValue(Assignment.Variable), out double varNum) ? varNum : null;
double? ValueNumber = double.TryParse(GetVariableParsedString(Assignment.Value), out double valNum) ? valNum : null;
if (VariableNumber == null || ValueNumber == null) {
switch (Assignment.Operator) {
case "=":
SetVariableValue(Assignment.Variable, Assignment.Value);
break;
case "+":
SetVariableValue(Assignment.Variable, VariableValue + Assignment.Value);
break;
case "-":
case "×":
case "÷":
Console.Error.WriteLine($"Error: '{Assignment.Operator}' can't be used on strings.");
break;
default:
Console.Error.WriteLine($"Error: Assignment operator '{Assignment.Operator}' is invalid.");
break;
}
}
else { // VariableNumber and ValueNumber both successfully parsed numbers
switch (Assignment.Operator) {
case "=":
SetVariableValue(Assignment.Variable, Assignment.Value);
break;
case "+":
SetVariableValue(Assignment.Variable, (VariableNumber + ValueNumber).ToString());
break;
case "-":
SetVariableValue(Assignment.Variable, (VariableNumber - ValueNumber).ToString());
break;
case "×":
SetVariableValue(Assignment.Variable, (VariableNumber * ValueNumber).ToString());
break;
case "÷":
SetVariableValue(Assignment.Variable, (VariableNumber / ValueNumber).ToString());
break;
default:
Console.Error.WriteLine($"Error: Assignment operator '{Assignment.Operator}' is invalid.");
break;
}
}
}
public bool DoesComparisonEquateToTrue(Comparison comparison) {
var variableValue = GetVariableValue(comparison.Variable);
var parsedValue = GetVariableParsedString(comparison.Value);
if (variableValue == null) {
return false;
}
switch (comparison.Operator) {
case "=":
return variableValue.ToString() == parsedValue;
case "≠":
return variableValue.ToString() != parsedValue;
case "<":
return Convert.ToDouble(variableValue) < Convert.ToDouble(parsedValue);
case "≤":
return Convert.ToDouble(variableValue) <= Convert.ToDouble(parsedValue);
case ">":
return Convert.ToDouble(variableValue) > Convert.ToDouble(parsedValue);
case "≥":
return Convert.ToDouble(variableValue) >= Convert.ToDouble(parsedValue);
case "⊇":
return variableValue.ToString().Contains(parsedValue);
case "⊉":
return !variableValue.ToString().Contains(parsedValue);
case "∈":
return parsedValue.Contains(variableValue.ToString());
case "∉":
return !parsedValue.Contains(variableValue.ToString());
default:
Console.Error.WriteLine($"Error: Comparison operator '{comparison.Operator}' is invalid.");
return false;
}
}
public int? GetProbabilityRouteToTake(List<Route> routes) {
var validWeightsWithIndices = new List<(double weight, int linksTo, int index)>();
for (int i = 0; i < routes.Count; i++) {
var route = routes[i];
var parsedWeight = GetVariableParsedString(route.Weight);
if (double.TryParse(parsedWeight, out double weight)) {
validWeightsWithIndices.Add((weight, route.LinksTo, i));
}
else {
Console.Error.WriteLine($"Error: Could not parse weight '{route.Weight}' for route at index {i} linking to {route.LinksTo}.");
}
}
if (!validWeightsWithIndices.Any()) {
Console.Error.WriteLine("No valid routes available.");
return null;
}
var totalWeight = validWeightsWithIndices.Sum(r => r.weight);
var random = new Random().NextDouble() * totalWeight;
double sum = 0;
foreach (var route in validWeightsWithIndices) {
sum += route.weight;
if (random < sum) {
return route.linksTo;
}
}
return validWeightsWithIndices.Last().linksTo;
}
}
public class Experience {
private readonly Project Project;
private ExperienceData ExperienceData;
private Node CurrentNode;
public Experience(Project project, string experienceName) {
Project = project;
ExperienceData = Project.GetExperience(experienceName);
CurrentNode = null;
}
private Node DeepCopyNode(Node original) {
if (original == null) return null;
Node copy = new Node {
Id = original.Id,
Type = original.Type,
LinksTo = original.LinksTo,
Name = original.Name,
DialogSource = original.DialogSource,
DialogString = original.DialogString,
Statement = original.Statement,
DefaultRouteLinksTo = original.DefaultRouteLinksTo,
DestinationExperience = original.DestinationExperience,
DestinationStartNode = original.DestinationStartNode
};
// Deep copy Properties
if (original.Properties != null) {
copy.Properties = original.Properties.Select(p => new Property {
Name = p.Name,
Value = p.Value
}).ToList();
}
// Deep copy Choices
if (original.Choices != null) {
copy.Choices = original.Choices.Select(c => new Choice {
ChoiceString = c.ChoiceString,
LinksTo = c.LinksTo
}).ToList();
}
// Deep copy Assignments
if (original.Assignments != null) {
copy.Assignments = original.Assignments.Select(a => new Assignment {
Variable = a.Variable,
Operator = a.Operator,
Value = a.Value
}).ToList();
}
// Deep copy Routes
if (original.Routes != null) {
copy.Routes = original.Routes.Select(r => new Route {
Comparisons = r.Comparisons?.Select(c => new Comparison {
Variable = c.Variable,
Operator = c.Operator,
Value = c.Value
}).ToList(),
Mode = r.Mode,
Weight = r.Weight,
LinksTo = r.LinksTo
}).ToList();
}
return copy;
}
public Node GetNode(int? nodeId) {
if (nodeId == null) {
Console.Error.WriteLine("Error: GetNode() received a null node ID.");
return null;
}
Node originalNode = ExperienceData.NodeList.FirstOrDefault(n => n.Id == nodeId);
if (originalNode == null) {
return null;
}
// Create a deep copy of the node to avoid modifying the original
Node node = DeepCopyNode(originalNode);
switch (node.Type) {
case "dialog":
node.DialogSource = Project.GetVariableParsedString(node.DialogSource);
node.DialogString = Project.GetVariableParsedString(node.DialogString);
node.Properties?.ForEach(property => property.Value = Project.GetVariableParsedString(property.Value));
break;
case "choice":
node.Choices?.ForEach(choice => choice.ChoiceString = Project.GetVariableParsedString(choice.ChoiceString));
node.Properties?.ForEach(property => property.Value = Project.GetVariableParsedString(property.Value));
break;
case "start":
case "end":
node.Properties?.ForEach(property => property.Value = Project.GetVariableParsedString(property.Value));
break;
case "function":
node.Statement = Project.GetVariableParsedString(node.Statement);
break;
}
return node;
}
public Node GetStartNode(string startNodeName) {
var originalStartNode = ExperienceData.NodeList.FirstOrDefault(node => node.Type == "start" && node.Name == startNodeName);
if (originalStartNode == null) {
Console.Error.WriteLine($"Error: No Start Node with the name '{startNodeName}' was found.");
return null;
}
// Create a deep copy of the node to avoid modifying the original
Node startNode = DeepCopyNode(originalStartNode);
CurrentNode = startNode;
return startNode;
}
public Node GetNextNode(int? choiceIndex = null) {
Node nextNode = CurrentNode;
if (nextNode == null) {
Console.Error.WriteLine("Error: No current node.");
return null;
}
switch (CurrentNode.Type) {
case "dialog":
case "start":
case "function":
nextNode = GetNode(nextNode.LinksTo);
break;
case "choice":
if (!choiceIndex.HasValue || nextNode.Choices == null || choiceIndex.Value < 0 || choiceIndex.Value >= nextNode.Choices.Count) {
Console.Error.WriteLine($"Error: Invalid or missing choice index '{choiceIndex}' for node with {nextNode.Choices?.Count ?? 0} choices.");
return null;
}
nextNode = GetNode(nextNode.Choices[choiceIndex.Value].LinksTo);
break;
case "end":
case "teleport":
Console.Error.WriteLine($"Error: '{nextNode.Type}' Nodes don't link to anything.");
return null;
default:
Console.Error.WriteLine($"Error: Node type '{nextNode.Type}' is invalid.");
return null;
}
do {
switch (nextNode.Type) {
case "variable":
nextNode.Assignments?.ForEach(assignment => Project.ProcessAssignment(assignment));
nextNode = GetNode(nextNode.LinksTo);
break;
case "conditional":
int routeToTake = -1;
for (int routeIndex = 0; routeIndex < nextNode.Routes.Count; routeIndex++) {
bool doAllComparisonsEquateToTrue = true;
bool doesAnyComparisonEquateToTrue = false;
for (int comparisonIndex = 0; comparisonIndex < nextNode.Routes[routeIndex].Comparisons.Count; comparisonIndex++) {
var comparison = nextNode.Routes[routeIndex].Comparisons[comparisonIndex];
if (!Project.DoesComparisonEquateToTrue(comparison)) {
doAllComparisonsEquateToTrue = false;
}
else {
doesAnyComparisonEquateToTrue = true;
}
}
switch (nextNode.Routes[routeIndex].Mode) {
case "all":
if (doAllComparisonsEquateToTrue) {
routeToTake = routeIndex;
}
break;
case "any":
if (doesAnyComparisonEquateToTrue) {
routeToTake = routeIndex;
}
break;
case "none":
if (!doesAnyComparisonEquateToTrue) {
routeToTake = routeIndex;
}
break;
default:
Console.Error.WriteLine($"Error: Invalid comparison mode '{nextNode.Routes[routeIndex].Mode}'.");
break;
}
}
nextNode = routeToTake == -1
? GetNode(nextNode.DefaultRouteLinksTo)
: GetNode(nextNode.Routes[routeToTake].LinksTo);
break;
case "probability":
nextNode = GetNode(Project.GetProbabilityRouteToTake(nextNode.Routes));
break;
case "teleport":
ExperienceData = Project.GetExperience(nextNode.DestinationExperience);
nextNode = GetStartNode(nextNode.DestinationStartNode);
break;
default:
if (nextNode == null) {
Console.Error.WriteLine("Error: nextNode is undefined.");
}
else {
Console.Error.WriteLine($"Error: Node type '{nextNode.Type}' is invalid.");
}
break;
}
}
while (nextNode != null && !new[] { "dialog", "choice", "start", "end", "function" }.Contains(nextNode.Type));
CurrentNode = nextNode;
if (nextNode == null) {
Console.Error.WriteLine("The chain either contained no Dialog, Choice, Start, End, or Function Nodes or contained an invalid property/value.");
return null;
}
return nextNode;
}
}
}