-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpd_helper.py
336 lines (282 loc) · 11.4 KB
/
bpd_helper.py
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
from __future__ import annotations
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import List, ClassVar
__all__ = [
"Behavior",
"BehaviorLink",
"BpdVariable",
"EBehaviorVariableType",
"EBehaviorVariableLinkType",
"EventData",
"generate_bpd",
"VariableLinkData",
]
_ALL_EVENTS: List[EventData] = []
_ALL_BEHAVIORS: List[Behavior] = []
_ALL_VARIABLES: List[BpdVariable] = []
class EBehaviorVariableType(Enum):
BVAR_None = auto()
BVAR_Bool = auto()
BVAR_Int = auto()
BVAR_Float = auto()
BVAR_Vector = auto()
BVAR_Object = auto()
BVAR_AllPlayers = auto()
BVAR_Attribute = auto()
BVAR_InstanceData = auto()
BVAR_NamedVariable = auto()
BVAR_NamedKismetVariable = auto()
BVAR_DirectionVector = auto()
BVAR_AttachmentLocation = auto()
BVAR_UnaryMath = auto()
BVAR_BinaryMath = auto()
BVAR_Flag = auto()
BVAR_MAX = auto()
class EBehaviorVariableLinkType(Enum):
BVARLINK_Unknown = auto()
BVARLINK_Context = auto()
BVARLINK_Input = auto()
BVARLINK_Output = auto()
BVARLINK_MAX = auto()
@dataclass
class BpdVariable:
"""
An object to represent an entry in the VariableData array.
By default commands will not be made for these as setting the VariableData array crashes,
instead specific variables will have commands written for them if needs_command is True.
Variables do not need to be set up to match their actual values in the bpd since they are
only there for referencing when linking, you only need to properly set the properties
when a command is to be written.
By default the idx will be -1, once it is generated it will be automatically appended to
the array of variables and given it's correct index.
Specifying an index will override the specified index with the new variable, if the index
is outisde the variables array, it will be extended with BpdVariable objects
Attributes:
var_type: the type of the variable
name: the name of the variable
idx: index in the VariableData array
needs_command: if a command should be written to set this variable
'set {bpd_name} BehaviorSequences[{sequence}].VariableData[{idx}] {command_str}
command_str: string used to generate variable commands
"""
var_type: EBehaviorVariableType = EBehaviorVariableType.BVAR_MAX
name: str = ""
idx: int = -1
needs_command: bool = False
command_str: ClassVar[str] = '(Name="{}",Type={})'
def __post_init__(self):
if self.idx == -1:
self.idx = len(_ALL_VARIABLES)
_ALL_VARIABLES.append(self)
return
if len(_ALL_VARIABLES) < (self.idx + 1):
_ALL_VARIABLES.extend(
[BpdVariable() for _ in range(self.idx + 1 - len(_ALL_VARIABLES))]
)
_ALL_VARIABLES[self.idx] = self
@dataclass
class VariableLinkData:
"""
An object to represent an entry in the ConsolidatedVariableLinkData array.
Specifies how variables are used by behaviors and events,
events will only use BVARLINK_Output, while behaviors use any of the types.
For BVARLINK_Output you need to specify the connection_index as it is used
to specify which output variable is sent.
For BVARLINK_Input the name is used to specify what property of the behavior
the variable will be used for.
For BVARLINK_Context the name should be just 'Context'.
Attributes:
vars: list of variable indexes to link to
name: the PropertyName
connection_index: the ConnectionIndex
command_str: string used to generate variable commands
"""
vars: List[int]
name: str
link_type: EBehaviorVariableLinkType
connection_index: int = 0
command_str: ClassVar[
str
] = '(PropertyName="{}",VariableLinkType={},ConnectionIndex={},LinkedVariables=(ArrayIndexAndLength={}),CachedProperty=None)'
@dataclass
class EventData:
"""
An object to represent an entry in the EventData2 array.
Attributes:
event_name: the EventName property
enabled: the bEnabled property
replicate: the bReplicate property
max_trigger_count: the MaxTriggerCount property
retrigger_delay: the ReTriggerDelay property
filter_object: the FilterObject property
output_variables: list of VariableLinkData for the OutputVariables
output_links: list of BehaviorLink for the OutputLinks
"""
event_name: str
enabled: bool = True
replicate: bool = False
max_trigger_count: int = 0
retrigger_delay: float = 0.0
filter_object: str = "None"
output_variables: List[VariableLinkData] = field(default_factory=lambda: [])
output_links: List[BehaviorLink] = field(default_factory=lambda: [])
command_str: ClassVar[
str
] = '(UserData=(EventName="{}",bEnabled={},bReplicate={},MaxTriggerCount={},ReTriggerDelay={},FilterObject={}),OutputVariables=(ArrayIndexAndLength={}),OutputLinks=(ArrayIndexAndLength={}))'
def __post_init__(self):
_ALL_EVENTS.append(self)
def add_output_link(self, link: BehaviorLink):
self.output_links.append(link)
@dataclass(unsafe_hash=True)
class Behavior:
behavior: str
linked_variables: List[VariableLinkData] = field(default_factory=lambda: [], hash=False)
output_links: List[BehaviorLink] = field(default_factory=lambda: [], hash=False)
variables_ArrayIndexAndLength: ClassVar[int] = 0
output_ArrayIndexAndLength: ClassVar[int] = 0
command_str: ClassVar[
str
] = "(Behavior={},LinkedVariables=(ArrayIndexAndLength={}),OutputLinks=(ArrayIndexAndLength={}))"
def __post_init__(self):
_ALL_BEHAVIORS.append(self)
def add_output_link(self, link: BehaviorLink):
self.output_links.append(link)
@dataclass
class BehaviorLink:
behavior: Behavior
link_id: int = 0
delay: int = 0
command_str: ClassVar[str] = "(LinkIdAndLinkedBehavior={},ActivateDelay={})"
def get_arrayindexandlength(idx: int, length: int):
if length == 0:
return 0
return idx << 16 | length
def get_linkidandlinkedbehavior(id: int, idx: int):
return id << 24 | idx
def get_var_link_commands(var_links: List[VariableLinkData], variable_links: List[int]):
variable_link_commands = []
for var_link in var_links:
idx = 0
length = len(var_link.vars)
if length > 1:
idx = len(variable_links)
variable_links.extend(var_link.vars)
elif length == 1:
idx = var_link.vars[0]
command_str = var_link.command_str.format(
var_link.name,
var_link.link_type.name,
var_link.connection_index,
get_arrayindexandlength(idx, length),
)
variable_link_commands.append(command_str)
return variable_link_commands
def get_behavior_link_commands(
behaviour_links: List[BehaviorLink], known_behaviors: List[Behavior]
):
behavior_link_commands = []
for link in behaviour_links:
if link.behavior not in known_behaviors:
known_behaviors.append(link.behavior)
idx = known_behaviors.index(link.behavior)
command_str = link.command_str.format(
get_linkidandlinkedbehavior(link.link_id, idx), link.delay
)
behavior_link_commands.append(command_str)
return behavior_link_commands
def generate_bpd(bpd_name: str, sequence: int = 0):
event_commands: List[str] = []
behavior_commands: List[str] = []
behavior_link_commands: List[str] = []
variable_link_commands: List[str] = []
variable_links: List[int] = [x for x in range(len(_ALL_VARIABLES))]
behavior_stack: List[Behavior] = []
known_behaviors: List[Behavior] = []
for event in _ALL_EVENTS:
output_vars = get_arrayindexandlength(
len(variable_link_commands), len(event.output_variables)
)
output_links = get_arrayindexandlength(len(behavior_link_commands), len(event.output_links))
variable_link_commands.extend(get_var_link_commands(event.output_variables, variable_links))
# Find unqiue set of unseen behaviours, add to stack in reverse order so they will be handled in order linked to
new_behaviors = reversed(
list(
{
link.behavior
for link in event.output_links
if link.behavior not in known_behaviors
}
)
)
behavior_stack.extend(new_behaviors)
behavior_link_commands.extend(
get_behavior_link_commands(event.output_links, known_behaviors)
)
command_str = event.command_str.format(
event.event_name,
event.enabled,
event.replicate,
event.max_trigger_count,
event.retrigger_delay,
event.filter_object,
output_vars,
output_links,
)
event_commands.append(command_str)
while len(behavior_stack) > 0:
behavior = behavior_stack[-1]
behavior_stack.pop()
if behavior is None:
continue
behavior.variables_ArrayIndexAndLength = get_arrayindexandlength(
len(variable_link_commands), len(behavior.linked_variables)
)
variable_link_commands.extend(
get_var_link_commands(behavior.linked_variables, variable_links)
)
behavior.output_ArrayIndexAndLength = get_arrayindexandlength(
len(behavior_link_commands), len(behavior.output_links)
)
new_behaviors = reversed(
list(
{
link.behavior
for link in behavior.output_links
if link.behavior not in known_behaviors
}
)
)
behavior_stack.extend(new_behaviors)
behavior_link_commands.extend(
get_behavior_link_commands(behavior.output_links, known_behaviors)
)
for behavior in known_behaviors:
command_str = behavior.command_str.format(
behavior.behavior,
behavior.variables_ArrayIndexAndLength,
behavior.output_ArrayIndexAndLength,
)
behavior_commands.append(command_str)
with open(f"{bpd_name.replace(':','.')}[{sequence}].txt", "w") as FILE:
FILE.write(
f"set {bpd_name} BehaviorSequences[{sequence}].EventData2 ({','.join(event_commands)})\n"
)
FILE.write(
f"set {bpd_name} BehaviorSequences[{sequence}].BehaviorData2 ({','.join(behavior_commands)})\n"
)
FILE.write(
f"set {bpd_name} BehaviorSequences[{sequence}].ConsolidatedOutputLinkData ({','.join(behavior_link_commands)})\n"
)
FILE.write(
f"set {bpd_name} BehaviorSequences[{sequence}].ConsolidatedVariableLinkData ({','.join(variable_link_commands)})\n"
)
FILE.write(
f"set {bpd_name} BehaviorSequences[{sequence}].ConsolidatedLinkedVariables ({','.join([str(x) for x in variable_links])})\n"
)
for variable in _ALL_VARIABLES:
if not variable.needs_command:
continue
FILE.write(
f'set {bpd_name} BehaviorSequences[{sequence}].VariableData[{variable.idx}] (Name="{variable.name}",Type={variable.var_type.name})\n'
)