-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbsp_format.py
More file actions
216 lines (170 loc) · 5.49 KB
/
bsp_format.py
File metadata and controls
216 lines (170 loc) · 5.49 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
import struct
import logging
import enum
import numpy as np
logger = logging.getLogger(__name__)
class Q1Lump(enum.Enum):
ENTITIES = 0
PLANES = 1
TEXTURES = 2
VERTEXES = 3
VISIBILITY = 4
NODES = 5
TEXINFO = 6
FACES = 7
LIGHTING = 8
CLIPNODES = 9
LEAFS = 10
MARKSURFACES = 11
EDGES = 12
SURFEDGES = 13
MODELS = 14
BSP_LUMPS = 15
# Some Quake BSP utilities adapted from Matthew Earl's pyquake
# https://github.com/matthewearl/pyquake/blob/2f1b0350dfab24b6557a37579cf15c78745e30e1/pyquake/bsp.py
class BspVersion(enum.IntEnum):
BSP = 29
_2PSB = struct.unpack("<L", b"2PSB")[0]
BSP2 = struct.unpack("<L", b"BSP2")[0]
@property
def uses_longs(self):
return self != BspVersion.BSP
@property
def leaf_fmt(self):
if self == BspVersion.BSP:
return "<llhhhhhhHHBBBB"
elif self == BspVersion._2PSB:
return "<llhhhhhhLLBBBB"
elif self == BspVersion.BSP2:
return "<llffffffLLBBBB"
raise ValueError()
def compress_visbits(in_arr):
dst = bytearray()
i = 0
while i < len(in_arr):
val = in_arr[i]
dst.append(val)
if val != 0:
i += 1
continue
rep = 1
for j in range(i + 1, len(in_arr)):
repval = in_arr[j]
if repval != val or rep == 255:
break
rep += 1
dst.append(rep)
i += rep
return dst
def load_q1_bsp(file_path: str, lumps_to_load: list[Q1Lump]):
with open(file_path, "rb") as f:
version = BspVersion(struct.unpack("<I", f.read(4))[0])
lumps_info = []
for i in range(BSP_LUMPS):
lump_offset, lump_size = struct.unpack("<ii", f.read(8))
lumps_info.append((Q1Lump(i), lump_offset, lump_size))
lumps_data = {}
for lump_id in lumps_to_load:
_, ofs, size = lumps_info[lump_id.value]
f.seek(ofs)
lumps_data[lump_id] = f.read(size)
return version, lumps_info, lumps_data
def read_leaves(leafdata, version):
leaf_size = struct.calcsize(version.leaf_fmt)
leaves = [
struct.unpack(version.leaf_fmt, leafdata[i : (i + leaf_size)])
for i in range(0, len(leafdata), leaf_size)
]
return leaves
def write_leaves(leaves, version):
written = bytearray()
for leaf in leaves:
written.extend(struct.pack(version.leaf_fmt, *leaf))
return written
def cluster_to_bits(mask, num_leaves, clusters):
cluster_inds = np.nonzero(mask)[0]
data = np.zeros(num_leaves, dtype=bool)
for cl in cluster_inds:
for leaf_idx in clusters[cl]:
data[leaf_idx] = True
return bytearray(np.packbits(data, bitorder="little"))
def update_bsp_leaf_visibility(
old_path: str,
new_path: str,
vis: np.ndarray,
clusters: list[list[int]],
portalleaves_real: int,
):
num_clusters = len(clusters)
version, info, lumps_data = load_q1_bsp(old_path, [Q1Lump.LEAFS])
leaves = read_leaves(lumps_data[Q1Lump.LEAFS], version)
num_leaves = len(leaves)
logger.debug(f"{num_leaves=}")
if num_clusters > 0:
assert vis.shape == (num_clusters, num_clusters)
vis_lump = bytearray()
patched_leaves = []
leaf_vis_offsets = {}
if num_clusters > 0:
for cluster_idx, cluster in enumerate(clusters):
vis_offset = len(vis_lump)
vis_bits = cluster_to_bits(vis[cluster_idx], portalleaves_real, clusters)
vis_bits_compressed = compress_visbits(vis_bits)
for vis_leaf_idx in cluster:
leaf_vis_offsets[vis_leaf_idx + 1] = vis_offset
vis_lump.extend(vis_bits_compressed)
else:
for leaf_idx, leaf in enumerate(leaves):
logger.debug(f"{leaf_idx}, {leaf}")
if leaf_idx == 0:
continue
if leaf_idx - 1 < vis.shape[0]:
# Leaf #0 is ignored by visibility calculations, so we map a BSP leaf index to a visibility
# leaf index by subtracting one.
vis_full = vis[leaf_idx - 1]
vis_bits = np.packbits(vis_full, bitorder="little")
vis_bits_compressed = compress_visbits(bytearray(vis_bits))
leaf_vis_offsets[leaf_idx] = len(vis_lump)
vis_lump.extend(vis_bits_compressed)
else:
leaf_vis_offsets[leaf_idx] = -1
for leaf_idx, leaf in enumerate(leaves):
logger.debug(f"{leaf_idx}, {leaf}")
old_vis_offset = leaf[1]
logger.debug(f" {old_vis_offset=}")
if leaf_idx == 0:
patched_leaves.append(leaf)
continue
vis_offset = leaf_vis_offsets.get(leaf_idx, -1)
logger.debug(f" Set new {vis_offset=}")
patched_leaves.append((leaf[0], vis_offset, *leaf[2:]))
leaf_lump = write_leaves(patched_leaves, version)
assert len(lumps_data[Q1Lump.LEAFS]) == len(leaf_lump)
to_replace = {Q1Lump.LEAFS: leaf_lump, Q1Lump.VISIBILITY: vis_lump}
with open(old_path, "rb") as inf:
with open(new_path, "wb") as outf:
outf.write(inf.read(4))
ofs_header = outf.tell()
for i in range(BSP_LUMPS):
outf.write(struct.pack("<ii", 0, 0))
lump_ofs_sizes = {}
for lump_id, old_ofs, old_size in info:
new_ofs = outf.tell()
if lump_id in to_replace:
new_size = len(to_replace[lump_id])
outf.write(to_replace[lump_id])
else:
new_size = old_size
inf.seek(old_ofs)
outf.write(inf.read(new_size))
lump_ofs_sizes[lump_id] = (new_ofs, new_size)
while outf.tell() % 4 == 0:
outf.write(bytes([0]))
logger.debug("New lumps:")
outf.seek(ofs_header)
for i in range(BSP_LUMPS):
key = Q1Lump(i)
ofs, size = lump_ofs_sizes[key]
logger.debug(f"{key.name:<15} {ofs:<8} {size:<8}")
outf.write(struct.pack("<ii", ofs, size))
print(f"File {new_path} written")