-
Notifications
You must be signed in to change notification settings - Fork 0
/
Landblocks.py
285 lines (235 loc) · 8.36 KB
/
Landblocks.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
from struct import unpack_from, calcsize
class SurfaceObjectsBlock:
"""
These entries contain a list of the objects located on the surface world.
Their ID is always of the form xxyyFFFE.
Format:
uint32_t id
uint32_t unknown (related to number of dungeon blocks?)
uint32_t number of objects
Object objects[number of objects]
uint32_t number of other objects
???????? other_objects[number of other objects]
"""
def __init__(self, blob):
pos = 0
(
self.id,
self.unk1,
self.number_objects
) = unpack_from("III", blob)
pos += calcsize("III")
self.objects = []
for i in range(0, self.number_objects):
self.objects.append(Object(blob[pos:pos + calcsize("Ifffffff")]))
pos += calcsize("Ifffffff")
self.number_other_objects = unpack_from("I", blob[pos:])[0]
pos += calcsize("I")
self.other_objects = []
for i in range(0, self.number_other_objects):
self.other_objects.append(Object(blob[pos:pos + calcsize("Ifffffff")]))
pos += calcsize("Ifffffff")
def __repr__(self):
s = "{id: " + str(hex(self.id))
s += ", unk1: " + str(hex(self.unk1))
s += ", objects: " + str(self.number_objects)
s += " " + str([o for o in self.objects])
s += ", other objects: " + str(self.number_other_objects)
s += " " + str([o for o in self.other_objects])
s += "}"
return s
class Object:
"""
Cell.dat object.
The id is the id of the file describing that object in portal.dat.
(x,y,z) is the translation (position) of the specified object in the
landblock.
(a,b,c,d) is the unit quaternion a + bi + cj + dk specifying the rotation
of the object.
The first 8 words of the other objects in the file start the same way as
regular objects but have a bunch of other information afterwards, of
varying length. It may have something to do with visibility.
Format:
uint32_t id
float x
float y
float z
float a
float b
float c
float d
"""
def __init__(self, blob):
(
self.id,
self.x,
self.y,
self.z,
self.a,
self.b,
self.c,
self.d
) = unpack_from("Ifffffff", blob)
def __repr__(self):
s = "{id: " + str(hex(self.id))
s += ", x: " + str(self.x)
s += ", y: " + str(self.y)
s += ", z: " + str(self.z)
s += ", a: " + str(self.a)
s += ", b: " + str(self.b)
s += ", c: " + str(self.c)
s += ", d: " + str(self.d)
s += "}"
return s
class DungeonBlock:
"""
Contains information for one block in the dungeon.
Their ID is in the form xxyynnnn where nnnn starts at 0x0100 and increments
for each block of the dungeon.
Format:
uint32_t id
uint32_t type
uint32_t id (why repeated?)
uint8_t number of textures
uint8_t number of connections
uint16_t number of visible dungeon blocks?
uint16_t texture_id[number of textures]
uint16_t pad if number of textures is odd?
uint32_t dungeon block geometry id
float x
float y
float z
float a
float b
float c
float d
uint64_t connectivity_info[number of connections]
uint16_t visible_blocks[number of visible blocks]?
uint16_t pad if the number of visible blocks is off?
uint32_t number of objects
Object objects[number of objects]
If bit 1 of type is set then the dungeon is a surface stucture.
If bit 2 of type is set then the dungeon block contains objects.
If bit 2 of type is not set then the number of objects and the object
fields are not present.
The texture IDs are actually 0x08000000 + texture ID.
They reference texture information files in portal.dat.
The dungeon block geometry IDs are actually 0x0d000000 + geometry ID.
They reference files in portal.dat.
(x,y,z) is the translation of the dungeon block.
(a,b,c,d) is its rotation.
"""
def __init__(self, blob):
pos = 0
(
self.id,
self.type,
self.id,
self.number_textures,
self.number_connections,
self.number_visible_blocks,
) = unpack_from("IIIBBH", blob)
pos += calcsize("IIIBBH")
self.texture_id = []
for i in range(0, self.number_textures):
texture_id = 0x8000000 + unpack_from("H", blob[pos:])[0]
self.texture_id.append(texture_id)
pos += calcsize("H")
self.geometry_id = 0xd000000 + unpack_from("I", blob[pos:])[0]
pos += calcsize("I")
(
self.x,
self.y,
self.z,
self.a,
self.b,
self.c,
self.d
) = unpack_from("fffffff", blob[pos:])
pos += calcsize("fffffff")
self.connectivity_info = []
for i in range(0, self.number_connections):
info = unpack_from("Q", blob[pos:])[0]
self.connectivity_info.append(info)
pos += calcsize("Q")
self.visible_blocks = []
for i in range(0, self.number_visible_blocks):
block_id = (self.id & 0xffff0000) + unpack_from("H", blob[pos:])[0]
self.visible_blocks.append(block_id)
pos += calcsize("H")
self.number_objects = 0
self.objects = []
if (self.type & 0x2):
self.number_objects = unpack_from("I", blob[pos:])[0]
pos += calcsize("I")
self.Object = []
for i in range(0, self.number_objects):
self.objects.append(Object(blob[pos:pos + calcsize("Ifffffff")]))
pos += calcsize("Ifffffff")
def __repr__(self):
s = "{id: " + str(hex(self.id))
s += ", type: " + str(hex(self.type))
s += ", geometry_id: " + str(hex(self.geometry_id))
s += ", translation: " + str([self.x, self.y, self.z])
s += ", rotation: " + str([self.a, self.b, self.c, self.d])
s += ", textures: " + str(int(self.number_textures))
s += " " + str([hex(x) for x in self.texture_id])
s += ", connections: " + str(int(self.number_connections))
s += " " + str(self.connectivity_info)
s += ", visible_blocks: " + str(int(self.number_visible_blocks))
s += " " + str([hex(x) for x in self.visible_blocks])
s += ", objects: " + str(self.number_objects)
s += " " + str([o for o in self.objects])
s += "}"
return s
class TopographyBlock:
"""
Contains the surface topography of a landblock.
Their ID is of the form xxyyFFFF.
"""
def __init__(self, blob):
pos = 0
(
self.id,
self.hasObjects,
) = unpack_from("II", blob)
pos += calcsize("II")
self.hasObjects = bool(self.hasObjects)
self.topography = []
for i in range(0, 81):
self.topography.append(Topography(blob[pos:pos+2]))
pos += 2
self.z = []
for i in range(0, 9*9):
z = unpack_from("B", blob[pos:])[0]
self.z.append(z)
pos += calcsize("B")
def __repr__(self):
s = "{id: " + str(hex(self.id))
s += ", hasObjects: " + str(self.hasObjects)
s += ", topography: " + str([t for t in self.topography])
s += ", z: " + str([z for z in self.z])
s += "}"
return s
class Topography:
"""
Contains road information, terrain type and vegetation.
Format:
uint8_t:1 Set if this cell is a road
uint8_t:1 Very rare, but if set, this cell is a road
uint8_t:6 Terrain type
uint8_t Vegetation
"""
def __init__(self, blob):
topography = unpack_from("H", blob)[0]
self.isRoad = bool(topography & 0x8000)
self.isRareRoad = bool(topography & 0x4000)
self.terrain = topography & 0x3F00
self.vegetation = topography & 0x00FF
def __repr__(self):
s = "{isRoad: " + str(self.isRoad)
s += ", isRareRoad: " + str(self.isRareRoad)
s += ", terrain: " + str(hex(self.terrain))
s += ", vegetation: " + str(hex(self.vegetation))
s += "}"
return s