-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNarrativeFlow_Interpreter_Script.py
More file actions
353 lines (291 loc) · 13.8 KB
/
Copy pathNarrativeFlow_Interpreter_Script.py
File metadata and controls
353 lines (291 loc) · 13.8 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
"""
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.
"""
import json
import random
import re
import copy
class Variable:
def __init__(self, name, value):
self.name = name
self.value = value
class DataTableItem:
def __init__(self, name, value):
self.name = name
self.value = value
class Choice:
def __init__(self, choiceString, linksTo):
self.choiceString = choiceString
self.linksTo = linksTo
class Assignment:
def __init__(self, variable, operator, value):
self.variable = variable
self.operator = operator
self.value = value
class Comparison:
def __init__(self, variable, operator, value):
self.variable = variable
self.operator = operator
self.value = value
class Route:
def __init__(self, comparisons, mode, weight, linksTo):
self.comparisons = comparisons
self.mode = mode
self.weight = weight
self.linksTo = linksTo
class Property:
def __init__(self, name, value):
self.name = name
self.value = value
class Node:
def __init__(self):
self.id = None
self.type = None
self.linksTo = None
self.properties = []
self.dialogSource = None
self.dialogString = None
self.choices = []
self.name = None
self.assignments = []
self.routes = []
self.defaultRouteLinksTo = None
self.statement = None
self.destinationExperience = None
self.destinationStartNode = None
class ExperienceData:
def __init__(self, name, nodeList):
self.name = name
self.nodeList = nodeList
class Project:
def __init__(self, compiledProjectJSONString):
data = json.loads(compiledProjectJSONString)
self.experiences = data['experiences']
self.variables = data['variables']
self.dataTable = data['dataTable']
def getExperience(self, name):
return next((exp for exp in self.experiences if exp['name'] == name), None)
def getVariableValue(self, name):
variable = next((v for v in self.variables if v['name'] == name), None)
if variable:
return variable['value']
print(f"Error: Variable with the name '{name}' not found.")
return None
def setVariableValue(self, name, value):
variable = next((v for v in self.variables if v['name'] == name), None)
if variable:
variable['value'] = value
return True
print(f"Error: Variable with the name '{name}' not found.")
return False
def getVariableParsedString(self, str_val):
result = str_val
for variable in self.variables:
# Escape regex special characters
escaped_name = re.escape(variable['name'])
result = re.sub(escaped_name, str(variable['value']), result)
return result
def getDataTableItemValue(self, name):
item = next((i for i in self.dataTable if i['name'] == name), None)
if item:
return self.getVariableParsedString(item['value'])
print(f"Error: Data Table item with the name '{name}' not found.")
return None
def processAssignment(self, assignment):
variable_value = self.getVariableValue(assignment['variable'])
try:
variable_number = float(variable_value)
value_number = float(self.getVariableParsedString(assignment['value']))
is_numeric = True
except (ValueError, TypeError):
is_numeric = False
if not is_numeric:
if assignment['operator'] == '=':
self.setVariableValue(assignment['variable'], assignment['value'])
elif assignment['operator'] == '+':
self.setVariableValue(assignment['variable'], str(variable_value) + str(assignment['value']))
elif assignment['operator'] in ['-', '×', '÷']:
print(f"Error: '{assignment['operator']}' can't be used on strings.")
else:
print(f"Error: Assignment operator '{assignment['operator']}' is invalid.")
else:
if assignment['operator'] == '=':
result = value_number
elif assignment['operator'] == '+':
result = variable_number + value_number
elif assignment['operator'] == '-':
result = variable_number - value_number
elif assignment['operator'] == '×':
result = variable_number * value_number
elif assignment['operator'] == '÷':
result = variable_number / value_number
else:
print(f"Error: Assignment operator '{assignment['operator']}' is invalid.")
return
self.setVariableValue(assignment['variable'], str(result))
def doesComparisonEquateToTrue(self, comparison):
variable_value = self.getVariableValue(comparison['variable'])
parsed_value = self.getVariableParsedString(comparison['value'])
if variable_value is None:
return False
if comparison['operator'] == '=':
return variable_value == parsed_value
elif comparison['operator'] == '≠':
return variable_value != parsed_value
elif comparison['operator'] == '<':
try:
return float(variable_value) < float(parsed_value)
except ValueError:
return False
elif comparison['operator'] == '≤':
try:
return float(variable_value) <= float(parsed_value)
except ValueError:
return False
elif comparison['operator'] == '>':
try:
return float(variable_value) > float(parsed_value)
except ValueError:
return False
elif comparison['operator'] == '≥':
try:
return float(variable_value) >= float(parsed_value)
except ValueError:
return False
elif comparison['operator'] == '⊇':
return str(parsed_value) in str(variable_value)
elif comparison['operator'] == '⊉':
return str(parsed_value) not in str(variable_value)
elif comparison['operator'] == '∈':
return str(variable_value) in str(parsed_value)
elif comparison['operator'] == '∉':
return str(variable_value) not in str(parsed_value)
else:
print(f"Error: Comparison operator '{comparison['operator']}' is invalid.")
return False
def getProbabilityRouteToTake(self, routes):
valid = []
for i, route in enumerate(routes):
try:
w = float(self.getVariableParsedString(route['weight']))
valid.append({'weight': w, 'linksTo': route['linksTo']})
except ValueError:
print(f"Error: Could not parse weight '{route['weight']}' for route at index {i}.")
if not valid:
print('No valid routes available.')
return None
total_weight = sum(r['weight'] for r in valid)
rand = random.random() * total_weight
cumulative = 0
for route in valid:
cumulative += route['weight']
if rand < cumulative:
return route['linksTo']
return valid[-1]['linksTo']
class Experience:
def __init__(self, project, experienceName):
self.project = project
self.experienceData = self.project.getExperience(experienceName)
self.currentNode = None
def getNode(self, nodeId):
if nodeId is None:
print('Error: getNode() received a null node ID.')
return None
node = next((n for n in self.experienceData['nodeList'] if n['id'] == nodeId), None)
if not node:
return None
# Create a deep copy of the node to avoid modifying the original
node = copy.deepcopy(node)
def parse(val):
return self.project.getVariableParsedString(val)
def parse_props(props):
if props:
for p in props:
p['value'] = parse(p['value'])
if node['type'] == 'dialog':
node['dialogSource'] = parse(node['dialogSource'])
node['dialogString'] = parse(node['dialogString'])
parse_props(node['properties'])
elif node['type'] == 'choice':
if node['choices']:
for choice in node['choices']:
choice['choiceString'] = parse(choice['choiceString'])
parse_props(node['properties'])
elif node['type'] in ['start', 'end']:
parse_props(node['properties'])
elif node['type'] == 'function':
node['statement'] = parse(node['statement'])
return node
def getStartNode(self, startNodeName):
start_node = next((n for n in self.experienceData['nodeList']
if n['type'] == 'start' and n['name'] == startNodeName), None)
if not start_node:
print(f"Error: No Start Node with the name '{startNodeName}' was found.")
return None
# Create a deep copy of the node to avoid modifying the original
start_node = copy.deepcopy(start_node)
self.currentNode = start_node
return start_node
def getNextNode(self, choiceIndex=None):
next_node = self.currentNode
if not next_node:
print('Error: No current node.')
return None
if next_node['type'] in ['dialog', 'start', 'function']:
next_node = self.getNode(next_node['linksTo'])
elif next_node['type'] == 'choice':
if (choiceIndex is None or not next_node['choices'] or
choiceIndex < 0 or choiceIndex >= len(next_node['choices'])):
print(f"Error: Invalid or missing choice index '{choiceIndex}'.")
return None
next_node = self.getNode(next_node['choices'][choiceIndex]['linksTo'])
elif next_node['type'] in ['end', 'teleport']:
print(f"Error: '{next_node['type']}' Nodes don't link to anything.")
return None
else:
print(f"Error: Node type '{next_node['type']}' is invalid.")
return None
while next_node and next_node['type'] not in ['dialog', 'choice', 'start', 'end', 'function']:
if next_node['type'] == 'variable':
if next_node['assignments']:
for a in next_node['assignments']:
self.project.processAssignment(a)
next_node = self.getNode(next_node['linksTo'])
elif next_node['type'] == 'conditional':
route_to_take = -1
for i, route in enumerate(next_node['routes']):
all_true = all(self.project.doesComparisonEquateToTrue(c) for c in route['comparisons'])
any_true = any(self.project.doesComparisonEquateToTrue(c) for c in route['comparisons'])
if route['mode'] == 'all' and all_true:
route_to_take = i
elif route['mode'] == 'any' and any_true:
route_to_take = i
elif route['mode'] == 'none' and not any_true:
route_to_take = i
else:
print(f"Error: Invalid comparison mode '{route['mode']}'.")
if route_to_take == -1:
next_node = self.getNode(next_node['defaultRouteLinksTo'])
else:
next_node = self.getNode(next_node['routes'][route_to_take]['linksTo'])
elif next_node['type'] == 'probability':
next_node = self.getNode(self.project.getProbabilityRouteToTake(next_node['routes']))
elif next_node['type'] == 'teleport':
self.experienceData = self.project.getExperience(next_node['destinationExperience'])
next_node = self.getStartNode(next_node['destinationStartNode'])
else:
print(f"Error: Node type '{next_node['type']}' is invalid.")
return None
self.currentNode = next_node
if not next_node:
print('The chain terminated with no valid node.')
return None
return next_node