-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNarrativeFlow_Interpreter_Script.gml
More file actions
467 lines (410 loc) · 17.4 KB
/
Copy pathNarrativeFlow_Interpreter_Script.gml
File metadata and controls
467 lines (410 loc) · 17.4 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
/*
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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function Variable(var_name, var_value) constructor {
name = var_name;
value = var_value;
}
function DataTableItem(item_name, item_value) constructor {
name = item_name;
value = item_value;
}
function Choice(choice_str, links) constructor {
choiceString = choice_str;
linksTo = links;
}
function Assignment(var_name, op, val) constructor {
variable = var_name;
operator = op;
value = val;
}
function Comparison(var_name, op, val) constructor {
variable = var_name;
operator = op;
value = val;
}
function Route(comps, route_mode, route_weight, links) constructor {
comparisons = comps;
mode = route_mode;
weight = route_weight;
linksTo = links;
}
function Property(prop_name, prop_value) constructor {
name = prop_name;
value = prop_value;
}
function Node() constructor {
id = noone;
type = "";
linksTo = noone;
properties = [];
dialogSource = "";
dialogString = "";
choices = [];
name = "";
assignments = [];
routes = [];
defaultRouteLinksTo = noone;
statement = "";
destinationExperience = "";
destinationStartNode = "";
}
function ExperienceData(exp_name, nodes) constructor {
name = exp_name;
nodeList = nodes;
}
function Project(compiled_project_json_string) constructor {
var data = json_parse(compiled_project_json_string);
experiences = data.experiences;
variables = data.variables;
dataTable = data.dataTable;
static get_experience = function(name) {
for (var i = 0; i < array_length(experiences); i++) {
if (experiences[i][$ "name"] == name) {
return experiences[i];
}
}
return noone;
}
static get_variable_value = function(name) {
for (var i = 0; i < array_length(variables); i++) {
if (variables[i][$ "name"] == name) {
return variables[i][$ "value"];
}
}
show_error("Error: Variable with the name '" + name + "' not found.", true);
return noone;
}
static set_variable_value = function(name, value) {
for (var i = 0; i < array_length(variables); i++) {
if (variables[i][$ "name"] == name) {
variables[i][$ "value"] = value;
return true;
}
}
show_error("Error: Variable with the name '" + name + "' not found.", true);
return false;
}
static get_variable_parsed_string = function(str_val) {
var result = str_val;
for (var i = 0; i < array_length(variables); i++) {
var variable = variables[i];
// Simple string replacement (GML doesn't have built-in regex)
result = string_replace_all(result, variable.name, variable.value);
}
return result;
}
static get_data_table_item_value = function(name) {
for (var i = 0; i < array_length(dataTable); i++) {
var item = dataTable[i];
if (item.name == name) {
return get_variable_parsed_string(item.value);
}
}
show_error("Error: Data Table item with the name '" + name + "' not found.", true);
return noone;
}
static can_string_convert_to_number = function(string_to_convert) {
var letters = string_letters(string_to_convert);
if (string_length(letters) > 0) return false;
else return true;
}
static process_assignment = function(assignment) {
var variable_value = get_variable_value(assignment.variable);
var is_number = can_string_convert_to_number(variable_value) && can_string_convert_to_number(assignment.value);
if (!is_number) {
switch (assignment.operator) {
case "=":
set_variable_value(assignment.variable, assignment.value);
break;
case "+":
set_variable_value(assignment.variable, string(variable_value) + string(assignment.value));
break;
case "-":
case "×":
case "÷":
show_error("Error: '" + assignment.operator + "' can't be used on strings.", true);
break;
default:
show_error("Error: Assignment operator '" + assignment.operator + "' is invalid.", true);
break;
}
} else {
var variable_number = 0;
var value_number = 0;
var result = 0;
switch (assignment.operator) {
case "=":
result = value_number;
break;
case "+":
result = variable_number + value_number;
break;
case "-":
result = variable_number - value_number;
break;
case "×":
result = variable_number * value_number;
break;
case "÷":
result = variable_number / value_number;
break;
default:
show_error("Error: Assignment operator '" + assignment.operator + "' is invalid.", true);
return;
}
set_variable_value(assignment.variable, string(result));
}
}
static does_comparison_equate_to_true = function(comparison) {
var variable_value = get_variable_value(comparison.variable);
var parsed_value = get_variable_parsed_string(comparison.value);
if (variable_value == noone) {
return false;
}
var is_number = can_string_convert_to_number(variable_value) && can_string_convert_to_number(parsed_value);
switch (comparison.operator) {
case "=":
return variable_value == parsed_value;
case "≠":
return variable_value != parsed_value;
case "<":
if (is_number) {
return real(variable_value) < real(parsed_value);
}
return variable_value < parsed_value;
case "≤":
if (is_number) {
return real(variable_value) <= real(parsed_value);
}
return variable_value <= parsed_value;
case ">":
if (is_number) {
return real(variable_value) > real(parsed_value);
}
return variable_value > parsed_value;
case "≥":
if (is_number) {
return real(variable_value) >= real(parsed_value);
}
return variable_value >= parsed_value;
case "⊇":
return string_pos(string(parsed_value), string(variable_value)) > 0;
case "⊉":
return string_pos(string(parsed_value), string(variable_value)) == 0;
case "∈":
return string_pos(string(variable_value), string(parsed_value)) > 0;
case "∉":
return string_pos(string(variable_value), string(parsed_value)) == 0;
default:
show_error("Error: Comparison operator '" + comparison.operator + "' is invalid.", true);
return false;
}
}
static get_probability_route_to_take = function(routes) {
var valid = [];
for (var i = 0; i < array_length(routes); i++) {
var route = routes[i];
var w = get_variable_parsed_string(route.weight);
if (can_string_convert_to_number(w)) {
array_push(valid, {weight: real(w), linksTo: route.linksTo});
} else {
show_error("Error: Could not parse weight '" + route.weight + "' for route at index " + string(i) + ".", true);
}
}
if (array_length(valid) == 0) {
show_error("No valid routes available.", true);
return noone;
}
var total_weight = 0;
for (var i = 0; i < array_length(valid); i++) {
total_weight += valid[i].weight;
}
var rand = random(total_weight);
var cumulative = 0;
for (var i = 0; i < array_length(valid); i++) {
var route = valid[i];
cumulative += route.weight;
if (rand < cumulative) {
return route.linksTo;
}
}
return valid[array_length(valid) - 1].linksTo;
}
}
function Experience(proj, experience_name) constructor {
project = proj;
experience_data = project.get_experience(experience_name);
current_node = noone;
static get_node = function(node_id) {
if (node_id == noone) {
show_error("Error: get_node() received a null node ID.", true);
return noone;
}
var original_node = noone;
for (var i = 0; i < array_length(experience_data.nodeList); i++) {
var n = experience_data.nodeList[i];
if (n.id == node_id) {
original_node = n;
break;
}
}
if (original_node == noone) {
return noone;
}
// Create a deep copy of the node to avoid modifying the original
var node = variable_clone(original_node);
// Helper functions
var parse = function(val) {
return project.get_variable_parsed_string(val);
}
var parse_props = function(props) {
if (array_length(props) > 0) {
for (var j = 0; j < array_length(props); j++) {
props[j].value = project.get_variable_parsed_string(props[j].value);
}
}
}
switch (node.type) {
case "dialog":
node.dialogSource = parse(node.dialogSource);
node.dialogString = parse(node.dialogString);
parse_props(node.properties);
break;
case "choice":
if (array_length(node.choices) > 0) {
for (var j = 0; j < array_length(node.choices); j++) {
node.choices[j].choiceString = parse(node.choices[j].choiceString);
}
}
parse_props(node.properties);
break;
case "start":
case "end":
parse_props(node.properties);
break;
case "function":
node.statement = parse(node.statement);
break;
}
return node;
}
static get_start_node = function(start_node_name) {
var original_start_node = noone;
for (var i = 0; i < array_length(experience_data.nodeList); i++) {
var n = experience_data.nodeList[i];
if (n.type == "start" && n.name == start_node_name) {
original_start_node = n;
break;
}
}
if (original_start_node == noone) {
show_error("Error: No Start Node with the name '" + start_node_name + "' was found.", true);
return noone;
}
// Create a deep copy of the node to avoid modifying the original
var start_node = variable_clone(original_start_node);
current_node = start_node;
return start_node;
}
static get_next_node = function(choice_index = noone) {
var next_node = current_node;
if (next_node == noone) {
show_error("Error: No current node.", true);
return noone;
}
if (next_node.type == "dialog" || next_node.type == "start" || next_node.type == "function") {
next_node = get_node(next_node.linksTo);
} else if (next_node.type == "choice") {
if (string_length(string_letters(choice_index)) == 0) {
choice_index = real(choice_index);
}
if (choice_index == noone || array_length(next_node.choices) == 0 ||
choice_index < 0 || choice_index >= array_length(next_node.choices) ||
!is_numeric(choice_index)) {
show_error("Error: Invalid or missing choice index '" + string(choice_index) + "'.", true);
return noone;
}
next_node = get_node(next_node.choices[choice_index].linksTo);
} else if (next_node.type == "end" || next_node.type == "teleport") {
show_error("Error: '" + next_node.type + "' Nodes don't link to anything.", true);
return noone;
} else {
show_error("Error: Node type '" + next_node.type + "' is invalid.", true);
return noone;
}
while (next_node != noone &&
next_node.type != "dialog" && next_node.type != "choice" &&
next_node.type != "start" && next_node.type != "end" && next_node.type != "function") {
switch (next_node.type) {
case "variable":
if (array_length(next_node.assignments) > 0) {
for (var i = 0; i < array_length(next_node.assignments); i++) {
project.process_assignment(next_node.assignments[i]);
}
}
next_node = get_node(next_node.linksTo);
break;
case "conditional":
var route_to_take = -1;
for (var i = 0; i < array_length(next_node.routes); i++) {
var route = next_node.routes[i];
var all_true = true;
var any_true = false;
for (var j = 0; j < array_length(route.comparisons); j++) {
var c = route.comparisons[j];
if (project.does_comparison_equate_to_true(c)) {
any_true = true;
} else {
all_true = false;
}
}
switch (route.mode) {
case "all":
if (all_true) route_to_take = i;
break;
case "any":
if (any_true) route_to_take = i;
break;
case "none":
if (!any_true) route_to_take = i;
break;
default:
show_error("Error: Invalid comparison mode '" + route.mode + "'.", true);
break;
}
}
if (route_to_take == -1) {
next_node = get_node(next_node.defaultRouteLinksTo);
} else {
next_node = get_node(next_node.routes[route_to_take].linksTo);
}
break;
case "probability":
next_node = get_node(project.get_probability_route_to_take(next_node.routes));
break;
case "teleport":
experience_data = project.get_experience(next_node.destinationExperience);
next_node = get_start_node(next_node.destinationStartNode);
break;
default:
show_error("Error: Node type '" + next_node.type + "' is invalid.", true);
return noone;
}
}
current_node = next_node;
if (next_node == noone) {
show_error("The chain terminated with no valid node.", true);
return noone;
}
return next_node;
}
}