-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvmf.py
288 lines (255 loc) · 6.39 KB
/
vmf.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
from random import randint
from point import *
from tex import *
# boilerplate at top of file
class Vmf:
# template for outputting a "side"
side_tpl = """\t\tside
{
"id" "%d"
"plane" "(%f %f %f) (%f %f %f) (%f %f %f)"
"material" "%s"
"uaxis" "[%f %f %f %f] %f"
"vaxis" "[%f %f %f %f] %f"
"rotation" "%f"
"lightmapscale" "16"
"smoothing_groups" "0"
"""
side_end = "\t\t}\n"
def __init__(self, filename):
self.num = 1
self.f = open(filename, 'w')
data = """versioninfo
{
"editorversion" "400"
"editorbuild" "5454"
"mapversion" "2"
"formatversion" "100"
"prefab" "0"
}
visgroups
{
}
viewsettings
{
"bSnapToGrid" "1"
"bShowGrid" "1"
"bShowLogicalGrid" "0"
"nGridSpacing" "32"
"bShow3DGrid" "0"
}
"""
self.f.write(data)
# start writing the worldspawn
def worldspawn(self):
data = """world
{
"id" "%d"
"mapversion" "2"
"classname" "worldspawn"
"detailmaterial" "detail/detailsprites"
"detailvbsp" "detail.vbsp"
"maxpropscreenwidth" "-1"
"musicpostfix" "Waterfront"
"skyname" "sky_l4d_rural02_hdr"
"""
self.f.write(data % self.num)
self.num += 1
# start outputting a solid
def solid(self):
data = '\tsolid\n\t{\n\t\t"id" "%d"\n'
self.f.write(data % self.num)
self.num += 1
def end_solid(self):
self.f.write('\t}\n')
# write a solid axis-aligned block in an entity or the worldspawn
def block(self, block, tex, displacement=None, autofit=True):
self.solid()
a = Point( block.z1, block.y1, block.x0 )
b = Point( block.z1, block.y1, block.x1 )
c = Point( block.z1, block.y0, block.x1 )
d = Point( block.z0, block.y0, block.x0 )
e = Point( block.z0, block.y0, block.x1 )
f = Point( block.z0, block.y1, block.x1 )
g = Point( block.z1, block.y0, block.x0 )
h = Point( block.z0, block.y1, block.x0 )
# point a uaxis vaxis rotation
# | point b | ushift | vshift |
# | | point c | | uscale | | vscale |
# | | | | | | | | | |
ls = [[a, b, c, tex, (1,0,0, 0, 0.25, 0,-1, 0, 0, 0.25), 0],
[d, e, f, tex, (1,0,0, 0, 0.25, 0,-1, 0, 0, 0.25), 0],
[a, g, d, tex, (0,1,0, 0, 0.25, 0, 0,-1, 0, 0.25), 0],
[f, e, c, tex, (0,1,0, 0, 0.25, 0, 0,-1, 0, 0.25), 0],
[b, a, h, tex, (1,0,0, 0, 0.25, 0, 0,-1, 0, 0.25), 0],
[e, d, g, tex, (1,0,0, 0, 0.25, 0, 0,-1, 0, 0.25), 0]]
sidenum = 0
for a,b,c,tex,deffit,rot in ls:
plane = (a.x,a.y,a.z, b.x,b.y,b.z, c.x,c.y,c.z, tex)
if autofit: deffit = texfit(a,b,c,tex)
side = (self.num,) + plane + deffit + (rot,)
self.num += 1
self.f.write( self.side_tpl % side )
if displacement and displacement.sidenum==sidenum:
self.displace(displacement,side[1],side[8],side[9])
self.f.write( self.side_end )
sidenum += 1
self.end_solid()
# output a solid pyramid
# base should be a list of Point()s
def pyramid(self, base, height, basetex, facetex):
self.solid()
a,b,c,d = base
center = (a+b+c+d) * 0.25
hvec = crossproduct(b-a,d-a)
normalize(hvec)
t = hvec * height + center # t is pyramid tip
ls = [[a, b, c, basetex],
[t, b, a, facetex],
[t, c, b, facetex],
[t, d, c, facetex],
[t, a, d, facetex]]
for a,b,c,tex in ls:
plane = (a.x,a.y,a.z, b.x,b.y,b.z, c.x,c.y,c.z, tex)
side = (self.num,) + plane + texfit(a,b,c,tex) + (0,)
self.num += 1
self.f.write( self.side_tpl % side )
self.f.write( self.side_end )
self.end_solid()
# output displacement information
def displace(self,dis,startx,starty,startz):
n = dis.nverts
data = """ dispinfo
{
"power" "%d"
"startposition" "[%d %d %d]"
"flags" "0"
"elevation" "0"
"subdiv" "0"
normals
{
"""
self.f.write(data % (dis.power, startx,starty,startz))
#output normals
for i in range(n):
self.f.write('\t\t\t\t\t"row%d" "' % i)
self.f.write(' '.join( n*["0 0 1"] ))
self.f.write('"\n')
#close normals, open distances
self.f.write("\t\t\t\t}\n\t\t\t\tdistances\n\t\t\t\t{\n")
#output distances
for i in range(n):
self.f.write('\t\t\t\t\t"row%d" "' % i)
self.f.write(' '.join( [str(x) for x in dis.dists[n*i:n*(i+1)]] ))
self.f.write('"\n')
#close distances, open alphas
self.f.write("\t\t\t\t}\n\t\t\t\talphas\n\t\t\t\t{\n")
#output alphas
for i in range(n):
self.f.write('\t\t\t\t\t"row%d" "' % i)
self.f.write(' '.join( [str(x) for x in dis.alphas[n*i:n*(i+1)]] ))
self.f.write('"\n')
#close alphas, close dispinfo
self.f.write("\t\t\t\t}\n\t\t\t}\n")
# end of any entity or the worldspawn
def end_ent(self):
self.f.write("}\n")
# boilerplate at end of file
def close(self):
data = """cameras
{
"activecamera" "-1"
}
cordons
{
"active" "0"
}
"""
self.f.write(data)
self.f.close()
def fog_controller(self,z,y,x):
data = """entity
{
"id" "%d"
"classname" "env_fog_controller"
"angles" "0 0 0"
"farz" "2200"
"fogblend" "0"
"fogcolor" "35 43 50"
"fogcolor2" "255 255 255"
"fogdir" "1 0 0"
"fogenable" "1"
"fogend" "2200"
"foglerptime" "5"
"fogmaxdensity" "1"
"fogstart" "0"
"HDRColorScale" "1"
"heightFogDensity" "0.0"
"heightFogMaxDensity" "1.0"
"heightFogStart" "0.0"
"maxcpulevel" "0"
"maxdxlevel" "0"
"maxgpulevel" "0"
"mincpulevel" "0"
"mindxlevel" "0"
"mingpulevel" "0"
"spawnflags" "1"
"targetname" "AutoInstance1-fog_master"
"use_angles" "0"
"origin" "%d %d %d"
}
"""
self.f.write(data % (self.num,x,y,z))
self.num += 1
def light_environment(self,z,y,x):
data = """entity
{
"id" "%d"
"classname" "light_environment"
"_ambient" "220 220 240 20"
"_ambientHDR" "220 220 240 20"
"_AmbientScaleHDR" "0.7"
"_light" "255 210 220 100"
"_lightHDR" "255 210 220 100"
"_lightscaleHDR" "0.7"
"angles" "-60 236 0"
"pitch" "-19"
"SunSpreadAngle" "5"
"origin" "%d %d %d"
}
"""
self.f.write(data % (self.num,x,y,z))
self.num += 1
def info_survivor_position(self,z,y,x):
data = """entity
{
"id" "%d"
"classname" "info_survivor_position"
"angles" "0 0 0"
"Order" "1"
"origin" "%d %d %d"
}
"""
self.f.write(data % (self.num,x,y,z))
self.num += 1
def info_player_start(self,z,y,x):
data = """entity
{
"id" "%d"
"classname" "info_player_start"
"angles" "0 0 0"
"Order" "1"
"origin" "%d %d %d"
}
"""
self.f.write(data % (self.num,x,y,z))
self.num += 1
def func_detail(self):
data = """entity
{
"id" "%d"
"classname" "func_detail"
"""
self.f.write(data % self.num)
self.num += 1
# vim: ts=8 sw=8 noet