-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyvis.py
334 lines (271 loc) · 10.5 KB
/
yvis.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
import os.path
import array
import argparse
import subprocess
import tempfile
import struct
import sys
sys.path.append("srctools")
import srctools.bsp
from srctools.bsp import BSP_LUMPS
need_game_files = False
def nostalgia_parser():
if need_game_files:
parser = argparse.ArgumentParser(usage='%(prog)s [-fast] [-game DIRECTORY] bspfile', add_help=False)
else:
parser = argparse.ArgumentParser(usage='%(prog)s [-fast] bspfile', add_help=False)
common_opts = parser.add_argument_group('Common options')
common_opts.add_argument('bspfile')
common_opts.add_argument('-h', '--help', action='help',
help=argparse.SUPPRESS)
common_opts.add_argument('-v', '-verbose', dest='verbose', action='store_true',
help='Turn on verbose output.')
common_opts.add_argument('-fast', dest='fast', action='store_true',
help='Only do first quick pass on vis calculations.')
common_opts.add_argument('-low', dest='low', action='store_true',
help='Run as an idle-priority process.')
common_opts.add_argument('-game', '-vproject', metavar='<directory>',
help='Override the VPROJECT environment variable.' if need_game_files else argparse.SUPPRESS)
other_opts = parser.add_argument_group('Other options')
other_opts.add_argument('-radius_override', metavar='<n>', dest='radius',
help='Force a vis radius, regardless of whether an env_fog_controller specifies one.')
other_opts.add_argument('-threads', metavar='<n>', dest='nthreads',
help='Control the number of threads vbsp uses (defaults to the # of processors on your machine).')
other_opts.add_argument('-trace', metavar=('<start cluster>', '<end cluster>'), nargs=2, dest='trace',
help='Writes a linefile that traces the vis from one cluster to another for debugging map vis.')
other_opts.add_argument('-FullMinidumps', action='store_true', help=argparse.SUPPRESS)
other_opts.add_argument('-nox360', '-nosort', action='store_true', help=argparse.SUPPRESS)
other_opts.add_argument('-mpi', '-novconfig', action='store_true', help=argparse.SUPPRESS)
other_opts.add_argument('-tmpin', '-tmpout', action='store_true', help=argparse.SUPPRESS)
other_opts.add_argument('-allowdebug', '-steam', action='store_true', help=argparse.SUPPRESS)
other_opts.add_argument('-mpi_pw', nargs=1, help=argparse.SUPPRESS)
return parser
def plain_parser():
parser = argparse.ArgumentParser()
parser.add_argument('bspfile')
parser.add_argument('--prt', metavar='<prtfile>', dest='prtfile')
parser.add_argument('--ref', metavar='<bspfile>', dest='reffile')
if need_game_files:
parser.add_argument('--game', metavar='<directory>', dest='game')
return parser
def determine_radius(vmf):
for entity in vmf.entities:
if entity["classname"].casefold() == "env_fog_controller":
try:
return float(entity.get("farz", ""))
except ValueError:
continue
CONTENTS_SOLID = 0x001
CONTENTS_SLIME = 0x010
CONTENTS_TESTFOGVOLUME = 0x100
SURF_WARP = 0x0008
def calc_visible_fog_volumes(dleafs, iter_cluster_vis):
# for every water leaf i:
# for every air leaf j, visible from i:
# dleafs[j].contents |= CONTENTS_TESTFOGVOLUME
cluster_contains_fog = [False] * numclusters
cluster_sees_fog = [False] * numclusters
for i in range(numleafs):
contents = dleafs[i].contents
waterid = dleafs[i].leafWaterDataID
if contents & (CONTENTS_SOLID | CONTENTS_SLIME):
continue
if waterid == -1:
continue
cluster_contains_fog[dleafs[i].cluster] = True
for i in range(numleafs):
jcontents = dleafs[j].contents
jwaterid = dleafs[j].leafWaterDataID
if jcontents & CONTENTS_SOLID:
continue
if jwaterid != -1:
continue
cluster_sees_fog[dleafs[i].cluster] = True
for i in range(clusters):
if not cluster_contains_fog[i]:
continue
for j in iter_cluster_vis(i):
for k in cluster[j]:
dleafs[k].contents |= CONTENTS_TESTFOGVOLUME
"""
def CalcDistanceFromLeafToWater(leafNum):
int j, k;
# If we know that this one doesn't see a water surface then don't bother doing anything.
if ((dleafs[leafNum].contents & CONTENTS_TESTFOGVOLUME) == 0) && ( dleafs[leafNum].leafWaterDataID == -1 ):
return 65535
# First get the vis data..
int cluster = dleafs[leafNum].cluster;
if (cluster < 0)
return 65535; # FIXME: make a define for this.
float minDist = 65535.0f; # FIXME: make a define for this.
Vector leafMin, leafMax;
leafMin[0] = ( float )dleafs[leafNum].mins[0];
leafMin[1] = ( float )dleafs[leafNum].mins[1];
leafMin[2] = ( float )dleafs[leafNum].mins[2];
leafMax[0] = ( float )dleafs[leafNum].maxs[0];
leafMax[1] = ( float )dleafs[leafNum].maxs[1];
leafMax[2] = ( float )dleafs[leafNum].maxs[2];
# something something build convex hull
for j in iter_leaf_leaf_vis(leafNum, DVIS_PVS):
jcontents = dleaf[j].contents
jwaterid = dleaf[j].leafWaterDataID
if (jcontents & CONTENTS_TESTFOGVOLUME) == 0 && jwaterid == -1:
continue
for faceId in iter_cluster_faces(j):
dface_t *pFace = &dfaces[faceID]
if( pFace->texinfo == -1 )
continue;
texinfo_t *pTexInfo = &texinfo[pFace->texinfo];
if( pTexInfo->flags & SURF_WARP )
# Woo hoo!!! We found a water face.
# compare the bounding box of the face with the bounding
# box of the leaf that we are looking from and see
# what the closest distance is.
# FIXME: this could be a face/face distance between the water
# face and the bounding volume of the leaf.
# Get the bounding box of the face
Vector faceMin, faceMax;
GetBoundsForFace( faceID, faceMin, faceMax );
float dist = GetMinDistanceBetweenBoundingBoxes( leafMin, leafMax, faceMin, faceMax );
if( dist < minDist )
minDist = dist;
return minDist
def calc_distance_from_leaves_to_water():
a = array.array('H', [calc_distance_from_leaf_to_water(i) for i in range(numleafs)])
if sys.byteorder != 'little':
a.byteswap()
return a
"""
from collections import namedtuple
dleaf_fmt = "<ihh3h3hHHHHhh"
dleaf = namedtuple("dleaf",
("contents", "cluster", "area_flags", "min_x", "min_y", "min_z",
"max_x", "max_y", "max_z", "firstleafface", "numleaffaces",
"firstleafbrush", "numleafbrushes", "leafWaterDataID", "padding"))
dface_fmt = "<Hbbihhhh4bif2i2iiHHI"
dface = namedtuple("dface",
("planenum", "side", "onNode", "firstedge", "numedges", "texinfo",
"dispinfo", "surfaceFogVolumeID", "style0", "style1", "style2",
"style3", "lightofs", "area", "luxoffx", "luxoffy", "luxdimx",
"luxdimy", "origFace", "NumPrims", "firstPrimID", "smoothingGroups"))
def build_cluster_table(dleafs):
numclusters = max(leaf.cluster for leaf in dleafs)+1
clusters = [[] for i in range(numclusters)]
for i, leaf in enumerate(dleafs):
clusters[leaf.cluster].append(i)
return clusters
# def clusterfaces(dleafs, dleaffaces, l):
# off = dleafs[l].firstleafface;
# num = dleafs[l].numleaffaces
# return dleaffaces[off:off+num]
#
# def face_bounds(dfaces, dsurfedges, dedges, dvertexes, i):
# face = dfaces[i]
# off = face.firstedge
# num = face.numedges
# x, y, z = [], [], []
# for e in dsurfedges[off:off+num]:
# e = 2 * abs(e)
# a, b = dedges[e:e+2]
# x.append(dvertexes[3*a+0])
# x.append(dvertexes[3*b+0])
# y.append(dvertexes[3*a+1])
# y.append(dvertexes[3*b+1])
# z.append(dvertexes[3*a+2])
# z.append(dvertexes[3*b+2])
# return min(x), max(x), min(y), max(y), min(z), max(z)
def steal_lumps(bsp, ref, lumpindices):
for lumpindex in lumpindices:
bsp.lumps[lumpindex].data = ref.lumps[lumpindex].data
def steal_lumps_common(bsp, ref):
steal_lumps(bsp, ref, [
BSP_LUMPS.LEAFS,
BSP_LUMPS.VISIBILITY,
BSP_LUMPS.LEAFMINDISTTOWATER,
BSP_LUMPS.DISPINFO,
BSP_LUMPS.LEAF_AMBIENT_INDEX_HDR,
BSP_LUMPS.LEAF_AMBIENT_INDEX,
BSP_LUMPS.LEAF_AMBIENT_LIGHTING_HDR,
BSP_LUMPS.LEAF_AMBIENT_LIGHTING
])
bsp.lumps[BSP_LUMPS.PHYSLEVEL].version = 16
def lump_compare(a, b):
if len(a.data) != len(b.data):
status = "LENGTH DIFFERS"
elif a.data != b.data:
status = "DATA DIFFERS"
elif a.ident != b.ident:
status = "IDENT DIFFERS"
elif a.version != b.version:
status = "VERSION DIFFERS"
else:
status = ""
print("{: <30} {:7} {:7} {:7} {:7} {: <12} {: <12} {}".format(
aindex.name.lower(),
len(a.data), len(b.data),
a.version, b.version,
repr(a.ident), repr(b.ident),
status))
def reassemble(src, ref, dst):
bsp = srctools.bsp.BSP(src)
ref = srctools.bsp.BSP(ref)
print("lumps only in src:", set(bsp.lumps.keys()) - set(ref.lumps.keys()))
print("lumps only in ref:", set(ref.lumps.keys()) - set(bsp.lumps.keys()))
for lumpindex, lump in bsp.lumps.items():
dump_compare(lump, ref.lumps[lumpindex])
print("---")
steal_lumps_common(bsp, ref)
print("---")
for lumpindex, lump in bsp.lumps.items():
dump_compare(lump, ref.lumps[lumpindex])
bsp.save(dst)
def main():
args = plain_parser().parse_args()
bsp = srctools.bsp.BSP(args.bspfile)
if not hasattr(args, "prtfile") or not args.prtfile:
if ".bsp" in args.bspfile:
args.prtfile = args.bspfile.replace(".bsp", ".prt")
else:
args.prtfile = args.bspfile + ".prt"
# to read cluster assignment
lump_leafs = bsp.get_lump(BSP_LUMPS.LEAFS)
print("len(lump_leafs) =", len(lump_leafs))
# to measure distance to water faces
lump_leaffaces = bsp.get_lump(BSP_LUMPS.LEAFFACES)
lump_faces = bsp.get_lump(BSP_LUMPS.FACES)
lump_edges = bsp.get_lump(BSP_LUMPS.EDGES)
lump_surfedges = bsp.get_lump(BSP_LUMPS.SURFEDGES)
lump_vertexes = bsp.get_lump(BSP_LUMPS.VERTEXES)
lump_texinfo = bsp.get_lump(BSP_LUMPS.TEXINFO)
dleafs = [dleaf(*x) for x in struct.iter_unpack(dleaf_fmt, lump_leafs)]
dfaces = [dface(*x) for x in struct.iter_unpack(dface_fmt, lump_faces)]
dleaffaces = array.array("h")
dleaffaces.frombytes(lump_leaffaces)
dedges = array.array("i")
dedges.frombytes(lump_edges)
dsurfedges = array.array("i")
dsurfedges.frombytes(lump_surfedges)
clusters = build_cluster_table(dleafs)
# to extract a vis radius from an env_fog_controller
vmf = bsp.read_ent_data()
with tempfile.NamedTemporaryFile() as f:
subprocess.check_call(["cargo", "run", "--bin", "yvis", "--release",
args.prtfile,
f.name])
lump_visibility = f.read()
def iter_cluster_vis(c):
# TODO
if False:
yield None
#calc_visible_fog_volumes(dleafs, iter_cluster_vis)
#lump_waterdist = calc_distance_from_leaves_to_water()
lump_leafs = b"".join(struct.pack(dleaf_fmt, *l) for l in dleafs)
if args.reffile:
ref = srctools.bsp.BSP(args.reffile)
steal_lumps_common(bsp, ref)
bsp.lumps[BSP_LUMPS.LEAFS].data = lump_leafs
bsp.lumps[BSP_LUMPS.VISIBILITY].data = lump_visibility
#bsp.get_lump(BSP_LUMPS.LEAFMINDISTTOWATER).data = lump_waterdist
bsp.save(os.path.splitext(args.bspfile)[0]+"_yvis.bsp")
if __name__ == '__main__':
main()