forked from Fast-64/fast64
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathactors.py
More file actions
286 lines (224 loc) · 10.9 KB
/
Copy pathactors.py
File metadata and controls
286 lines (224 loc) · 10.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
from dataclasses import dataclass, field
from typing import Optional
from mathutils import Matrix
from bpy.types import Object
from ....utility import PluginError, CData, indent
from ...oot_utility import getObjectList
from ...oot_constants import ootData
from ..utility import Utility
from ..actor import Actor
@dataclass
class TransitionActor(Actor):
"""Defines a Transition Actor"""
isRoomTransition: Optional[bool] = field(init=False, default=None)
roomFrom: Optional[int] = field(init=False, default=None)
roomTo: Optional[int] = field(init=False, default=None)
cameraFront: Optional[str] = field(init=False, default=None)
cameraBack: Optional[str] = field(init=False, default=None)
def getEntryC(self):
"""Returns a single transition actor entry"""
sides = [(self.roomFrom, self.cameraFront), (self.roomTo, self.cameraBack)]
roomData = "{ " + ", ".join(f"{room}, {cam}" for room, cam in sides) + " }"
posData = "{ " + ", ".join(f"{round(pos)}" for pos in self.pos) + " }"
actorInfos = [roomData, self.id, posData, self.rot, self.params]
infoDescs = ["Room & Cam Index (Front, Back)", "Actor ID", "Position", "Rotation Y", "Parameters"]
return (
(indent + f"// {self.name}\n" + indent if self.name != "" else "")
+ "{\n"
+ ",\n".join((indent * 2) + f"/* {desc:30} */ {info}" for desc, info in zip(infoDescs, actorInfos))
+ ("\n" + indent + "},\n")
)
def getEntryXML(self)
"""Returns a single transition actor entry as an XML ``str``"""
rot = self.rot[len("DEF_TO_BINANG("):-1]
data = indent + "<TransitionActor "
data += f"FrontSideRoom='{self.roomFrom}' "
data += f"FrontSideEffects={self.cameraFront}' "
data += f"BackSideRoom='{self.roomTo}' "
data += f"BackSideEffects='{self.cameraBack}' "
data += f"PosX='{self.posData[0]}' "
data += f"PosY='{self.posData[1]}' "
data += f"PosZ='{self.posData[2]}' "
data += f"RotY='{rot}' "
data += f"Params='{self.params}'/>\n"
return data
@dataclass
class SceneTransitionActors:
name: str
entries: list[TransitionActor]
@staticmethod
def new(name: str, sceneObj: Object, transform: Matrix, headerIndex: int):
# we need to get the corresponding room index if a transition actor
# do not change rooms
roomObjList = getObjectList(sceneObj.children_recursive, "EMPTY", "Room")
actorToRoom: dict[Object, Object] = {}
for obj in roomObjList:
for childObj in obj.children_recursive:
if childObj.type == "EMPTY" and childObj.ootEmptyType == "Transition Actor":
actorToRoom[childObj] = obj
actorObjList = getObjectList(sceneObj.children_recursive, "EMPTY", "Transition Actor")
actorObjList.sort(key=lambda obj: actorToRoom[obj].ootRoomHeader.roomIndex)
entries: list[TransitionActor] = []
for obj in actorObjList:
transActorProp = obj.ootTransitionActorProperty
if (
Utility.isCurrentHeaderValid(transActorProp.actor.headerSettings, headerIndex)
and transActorProp.actor.actorID != "None"
):
pos, rot, _, _ = Utility.getConvertedTransform(transform, sceneObj, obj, True)
transActor = TransitionActor()
if transActorProp.isRoomTransition:
if transActorProp.fromRoom is None or transActorProp.toRoom is None:
raise PluginError("ERROR: Missing room empty object assigned to transition.")
fromIndex = transActorProp.fromRoom.ootRoomHeader.roomIndex
toIndex = transActorProp.toRoom.ootRoomHeader.roomIndex
else:
fromIndex = toIndex = actorToRoom[obj].ootRoomHeader.roomIndex
front = (fromIndex, Utility.getPropValue(transActorProp, "cameraTransitionFront"))
back = (toIndex, Utility.getPropValue(transActorProp, "cameraTransitionBack"))
if transActorProp.actor.actorID == "Custom":
transActor.id = transActorProp.actor.actorIDCustom
else:
transActor.id = transActorProp.actor.actorID
transActor.name = (
ootData.actorData.actorsByID[transActorProp.actor.actorID].name.replace(
f" - {transActorProp.actor.actorID.removeprefix('ACTOR_')}", ""
)
if transActorProp.actor.actorID != "Custom"
else "Custom Actor"
)
transActor.pos = pos
transActor.rot = f"DEG_TO_BINANG({(rot[1] * (180 / 0x8000)):.3f})" # TODO: Correct axis?
transActor.params = transActorProp.actor.actorParam
transActor.roomFrom, transActor.cameraFront = front
transActor.roomTo, transActor.cameraBack = back
entries.append(transActor)
return SceneTransitionActors(name, entries)
def getCmd(self):
"""Returns the transition actor list scene command"""
return indent + f"SCENE_CMD_TRANSITION_ACTOR_LIST({len(self.entries)}, {self.name}),\n"
def getC(self):
"""Returns the transition actor array"""
transActorList = CData()
listName = f"TransitionActorEntry {self.name}"
# .h
transActorList.header = f"extern {listName}[];\n"
# .c
transActorList.source = (
(f"{listName}[]" + " = {\n") + "\n".join(transActor.getEntryC() for transActor in self.entries) + "};\n\n"
)
return transActorList
def getXML(self):
"""Returns the transition actor list as an XML string"""
data = "<TransitionActorList>\n"
for actor in self.entries:
data += actor.getXML();
data += "</TransitionActorList>\n"
return data
@dataclass
class EntranceActor(Actor):
"""Defines an Entrance Actor"""
roomIndex: Optional[int] = field(init=False, default=None)
spawnIndex: Optional[int] = field(init=False, default=None)
def getEntryC(self):
"""Returns a single spawn entry"""
return indent + "{ " + f"{self.spawnIndex}, {self.roomIndex}" + " },\n"
def getEntryXML(self)
"""Returns a single spawn entry as an XML ``str``"""
rot = self.rot[len("DEF_TO_BINANG("):-1]
data = indent + "<EntraceEntry "
data += f"Spawn='{self.spawnIndex}' "
data += f"Room='{self.roomIndex}'/>\n"
return data
@dataclass
class SceneEntranceActors:
name: str
entries: list[EntranceActor]
@staticmethod
def new(name: str, sceneObj: Object, transform: Matrix, headerIndex: int):
"""Returns the entrance actor list based on empty objects with the type 'Entrance'"""
entranceActorFromIndex: dict[int, EntranceActor] = {}
actorObjList = getObjectList(sceneObj.children_recursive, "EMPTY", "Entrance")
for obj in actorObjList:
entranceProp = obj.ootEntranceProperty
if (
Utility.isCurrentHeaderValid(entranceProp.actor.headerSettings, headerIndex)
and entranceProp.actor.actorID != "None"
):
pos, rot, _, _ = Utility.getConvertedTransform(transform, sceneObj, obj, True)
entranceActor = EntranceActor()
entranceActor.name = (
ootData.actorData.actorsByID[entranceProp.actor.actorID].name.replace(
f" - {entranceProp.actor.actorID.removeprefix('ACTOR_')}", ""
)
if entranceProp.actor.actorID != "Custom"
else "Custom Actor"
)
entranceActor.id = "ACTOR_PLAYER" if not entranceProp.customActor else entranceProp.actor.actorIDCustom
entranceActor.pos = pos
entranceActor.rot = ", ".join(f"DEG_TO_BINANG({(r * (180 / 0x8000)):.3f})" for r in rot)
entranceActor.params = entranceProp.actor.actorParam
if entranceProp.tiedRoom is not None:
entranceActor.roomIndex = entranceProp.tiedRoom.ootRoomHeader.roomIndex
else:
raise PluginError("ERROR: Missing room empty object assigned to the entrance.")
entranceActor.spawnIndex = entranceProp.spawnIndex
if entranceProp.spawnIndex not in entranceActorFromIndex:
entranceActorFromIndex[entranceProp.spawnIndex] = entranceActor
else:
raise PluginError(f"ERROR: Repeated Spawn Index: {entranceProp.spawnIndex}")
entranceActorFromIndex = dict(sorted(entranceActorFromIndex.items()))
if list(entranceActorFromIndex.keys()) != list(range(len(entranceActorFromIndex))):
raise PluginError("ERROR: The spawn indices are not consecutive!")
return SceneEntranceActors(name, list(entranceActorFromIndex.values()))
def getCmd(self):
"""Returns the spawn list scene command"""
name = self.name if len(self.entries) > 0 else "NULL"
return indent + f"SCENE_CMD_SPAWN_LIST({len(self.entries)}, {name}),\n"
def getC(self):
"""Returns the spawn actor array"""
spawnActorList = CData()
listName = f"ActorEntry {self.name}"
# .h
spawnActorList.header = f"extern {listName}[];\n"
# .c
spawnActorList.source = (
(f"{listName}[]" + " = {\n") + "".join(entrance.getActorEntry() for entrance in self.entries) + "};\n\n"
)
return spawnActorList
def getXML(self):
"""Returns the spawn list as an XML string"""
data = "<EntranceList>\n"
for actor in self.entries:
data += actor.getXML();
data += "</EntranceList>\n"
return data
@dataclass
class SceneSpawns(Utility):
"""This class handles scene actors (transition actors and entrance actors)"""
name: str
entries: list[EntranceActor]
def getCmd(self):
"""Returns the entrance list scene command"""
return indent + f"SCENE_CMD_ENTRANCE_LIST({self.name if len(self.entries) > 0 else 'NULL'}),\n"
def getC(self):
"""Returns the spawn array"""
spawnList = CData()
listName = f"Spawn {self.name}"
# .h
spawnList.header = f"extern {listName}[];\n"
# .c
spawnList.source = (
(f"{listName}[]" + " = {\n")
+ (indent + "// { Spawn Actor List Index, Room Index }\n")
+ "".join(entrance.getEntryC() for entrance in self.entries)
+ "};\n\n"
)
return spawnList
def getXML(self):
"""Returns the spawn list as an XML string"""
data = "<EntranceList>\n"
for actor in self.entries:
data += actor.getXML();
data += "</EntranceList>\n"
return data