forked from SinZ163/w3x-to-vmf
/
writevmf_displacementTest.py
335 lines (244 loc) · 14.1 KB
/
writevmf_displacementTest.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
335
## Heavy modification of writevmf for using
## displacements instead of brushes with different heights
import os
import array
import math
import time
# using BHSPitMonkey's Python vmflib: http://github.com/BHSPitMonkey/vmflib
# Even though it is for Python 3 and above, most of
# its main features work on Python 2.7 (As of this moment, 18th January, 2014)
import lib.vmflib as vmflib
import lib.vmflib.tools as tools
try:
from PIL import Image # Uses Pillow to draw a graphic representation of the map
pillow_installed = True
except:
pillow_installed = False
from read_w3e import ReadW3E
from lib.dataTypes import QuadBlobs, Bytemap
from lib.helperFunctions import make_number_divisible_by_n, map_list_with_vertex
if __name__ == "__main__":
print "Starting..."
initTime = time.clock()
data = ReadW3E("input/war3map.w3e")
print "Original map dimension: xsize: {0}, ysize: {1}".format(data.mapInfo["width"], data.mapInfo["height"])
m = vmflib.vmf.ValveMap()
m.world.skyname = "sky_dotasky_01"
# x_factor and y_factor will limit the size of objects that are drawn, if the map should be smaller
# Remains from a different program, might be removed in future revisions
x_factor = 1
y_factor = 1
# size of original map and an estimated height. The height value is set to an artificial size
xSize, ySize, height = (data.mapInfo["width"]//x_factor), (data.mapInfo["height"]//y_factor), 2500
xSize = make_number_divisible_by_n(xSize, 4)
ySize = make_number_divisible_by_n(ySize, 4)
print "Adjusted values for map dimension: xsize: {0}, ysize: {1}".format(xSize,ySize)
# size of the resulting source map. At the moment, changing the scaling requires
# changing the constant in other places in the code
xSize_real, ySize_real = xSize*64, ySize*64
# The offset variables are used to move the created brushes in such a way
# so that the middle of the wc3 map is the (0,0) coordinate in the vmf file
xOffset_real = xSize_real//2
yOffset_real = ySize_real//2
#xOffset_real = 0
#yOffset_real = 0
xOffset = (xSize//2)
yOffset = (ySize//2)
#xOffset = 0
#yOffset = 0
zOffset = (height//2)
orig = vmflib.types.Vertex(0,0-yOffset_real, 0+zOffset)
skybox_back = tools.Block(origin = orig, dimensions=(xSize_real, 8, height))
skybox_back.set_material("tools/toolsskybox")
orig = vmflib.types.Vertex(0,0+yOffset_real, 0+zOffset)
skybox_front = tools.Block(origin = orig, dimensions=(xSize_real, 8,height))
skybox_front.set_material("tools/toolsskybox")
orig = vmflib.types.Vertex(0-xOffset_real,0, 0+zOffset)
skybox_left = tools.Block(origin = orig, dimensions=(8, ySize_real,height))
skybox_left.set_material("tools/toolsskybox")
orig = vmflib.types.Vertex(0+xOffset_real,0, 0+zOffset)
skybox_right = tools.Block(origin = orig, dimensions=(8, ySize_real,height))
skybox_right.set_material("tools/toolsskybox")
orig = vmflib.types.Vertex(0,0, 0+zOffset*2)
skybox_ceiling = tools.Block(origin = orig, dimensions=(xSize_real, ySize_real, 8))
skybox_ceiling.set_material("tools/toolsskybox")
m.world.children.append(skybox_back)
m.world.children.append(skybox_front)
m.world.children.append(skybox_left)
m.world.children.append(skybox_right)
m.world.children.append(skybox_ceiling)
print "Done adding walls, now to do the map itself..."
try:
os.makedirs('./output')
print "created output directory"
except OSError as error:
print str(error)
bloblist = []
heightmap = Bytemap(data.mapInfo["width"], data.mapInfo["height"])
rampMap = Bytemap(data.mapInfo["width"], data.mapInfo["height"], init = 0, dataType = "B")
#blobAffinity = Bytemap(data.mapInfo["width"], data.mapInfo["height"])
# Iterate once over the map to store the height of each tile.
# This way, we avoid having to use the binary AND every time
# we try to retrieve the height of a tile
#lowestHeight = 10000000
for x in xrange(data.mapInfo["width"]):
for y in xrange(data.mapInfo["height"]):
index = y*data.mapInfo["width"] + x
tile = data.mapInfo["info"][index]
height = tile["layerHeight"]
ramp_flag = (tile["flags"] & 0x1)
rampMap.setVal(x, y, ramp_flag)
## voodoo magic, disabled for now until I understand it
#The tilepoint "final height" you see on the WE is given by:
#(ground_height - 0x2000 + (layer - 2)*0x0200)/4
#height = tile["groundHeight"] - 0x2000 + ((tile["nibble2"] & 0xF) -2) *0x0200 / 4
#if height < lowestHeight:
# lowestHeight = height
heightmap.setVal(x, y, height)
Blockgroups = QuadBlobs(xSize//4, ySize//4, 4, 4)
## For the moment, we will not do anything interesting with normals.
## We will set it to 0,0,1 i.e. the normal vector will point straight upwards.
normals_row = [vmflib.types.Vertex(0,0,1) for i in xrange(17)]
normals_list = [normals_row for i in xrange(17)]
#choice = ("nature/dirt_grass_00", "nature/blendrockground002")
choice = ("brick/brick_ext_07", "brick/brick_ext_06")
print "Time taken for initialization: {0}".format(time.clock()-initTime)
creationTime = time.clock()
for ix in xrange(xSize//4):
for iy in xrange(ySize//4):
blob = Blockgroups.addBlob(ix, iy)
for iix in xrange(4):
newX = (ix*4)+iix
for iiy in xrange(4):
newY = (iy*4)+iiy
if newX >= data.mapInfo["width"] or newY >= data.mapInfo["height"]:
break
else:
currentHeight = heightmap.getVal(newX, newY)
# Is the ramp flag set? If so, we will try to draw a transition between two layers
if rampMap.getVal(newX, newY) == 1 and False: # for the moment disabled because of issues
upperTile = heightmap.getVal(newX, newY+1)
lowerTile = heightmap.getVal(newX, newY-1)
leftTile = heightmap.getVal(newX-1, newY)
rightTile = heightmap.getVal(newX+1, newY)
# The startX and startY variables are coordinates for the points
# that are closest to the highest tile
startX = 0
startY = 0
if upperTile < lowerTile:
startY = 3
directionType = 1
elif upperTile > lowerTile:
startY = 0
directionType = 1
elif leftTile < rightTile:
startX = 0
directionType = 2
elif leftTile > rightTile:
startX = 3
directionType = 2
if iix > 1: xoffset = 1
else: xoffset = 0
if iiy > 1: yoffset = 1
else: yoffset = 0
if directionType == 1: # A ramp that goes from top to down along the y axis (or vice versa)
for local_y in xrange(4):
distance = 3 - local_y
# Attempt to create a weighed average value for points on the ramp
# Does not appear to work correctly just yet
average = (((local_y+1)/4.0) * lowerTile + ((distance+1)/4.0) * upperTile) / 2.0
for local_x in xrange(4):
average
blob.setVal(iix*4+local_x+xoffset, iiy*4+local_y+yoffset, average*64)
elif directionType == 2: # A ramp that goes from left to right along the x axis (or vice versa)
for local_x in xrange(4):
distance = 3 - local_x
average = (((local_x+1)/4.0) * leftTile + ((distance+1)/4.0) * rightTile) / 2.0
for local_y in xrange(4):
blob.setVal(iix*4+local_x+xoffset, iiy*4+local_y+yoffset, average*64)
"""for pointX in xrange(4):
for pointY in xrange(4):
local_x =
for point in blob.getValGroup_iter((iix*4+xoffset, iiy*4+yoffset),
((iix+1)*4+xoffset, (iiy+1)*4+yoffset)):
local_x, local_y, height = point
blob.setVal(local_x, local_y, currentHeight32)"""
else:
## A simple displacement test that uses the tile height for all points
## of the tile.
#tile = Blockgroups.getTile(ix, iy, iix, iiy)
#currentVals = tile.getValGroup()
if iix > 1: xoffset = 1
else: xoffset = 0
if iiy > 1: yoffset = 1
else: yoffset = 0
for point in blob.getValGroup_iter((iix*4+xoffset, iiy*4+yoffset),
((iix+1)*4+xoffset, (iiy+1)*4+yoffset)):
local_x, local_y, height = point
blob.setVal(local_x, local_y, currentHeight*64)
Blockgroups.sewTilesTogether(ix, iy)
#blob = Blockgroups.getBlob(ix, iy)
for ix in xrange(xSize//4):
for iy in xrange(ySize//4):
height = 64
vert = vmflib.types.Vertex((ix*4*64)-xOffset_real+2*64, (iy*4*64)-yOffset_real+2*64, 0+(height//2))
block = tools.Block(origin = vert, dimensions=(4*64, 4*64, height))
## We alternate between two types of textures. This results in a checkered pattern,
## similar to chess. It is very easy to see where a block starts and ends.
block.set_material(choice[(iy+ix*(xSize//4))%2])
#block.set_material("brick/brick_ext_07")
# Mark blocks which have a single ramp or more with a different texture
breakOut = False
for iix in xrange(4):
newx = ix*4+iix
for iiy in xrange(4):
newy = iy*4+iiy
if newx >= data.mapInfo["width"] or newy >= data.mapInfo["height"]:
break
if rampMap.getVal(newx, newy) == 1:
block.set_material("brick/brick_ext_08")
breakOut = True
break
if breakOut:
break
Blockgroups.sew_brush_neighbours(ix, iy)
blob = Blockgroups.getBlob(ix, iy)
distances_list = []
for rowNumber in xrange(17):
row = blob.getRow(rowNumber)
row = row.tolist()
#row.reverse()
distances_list.append(row)
#row = map(map_list_with_vertex, row)
#print row
"""distances_list = [[64 for i in xrange(17)]]
for columnNumber in xrange(16):
#column = blob.getColumn(columnNumber)
##column = column.tolist()
##colum.reverse()
#distances_list.append(column)
column = [64]
for i in range(4):
column.extend([i*32, i*32, i*32, i*32])
distances_list.append(column)"""
#dispInfo = vmflib.brush.DispInfo(4, normals_list, distances_list)
#dispInfo.set_startPosition((ix*4*64)-xOffset_real+0*64, (iy*4*64)-yOffset_real+0*64, 0+(height))
#block.top().children.append(dispInfo)
block.top().set_dispInfo(4, normals_list, distances_list)
m.world.children.append(block)
#scaled_lowestHeight = lowestHeight*64
#print("The lowest height found is "+str(lowestHeight))
#print("which after scaling, is "+str(scaled_lowestHeight))
scaled_lowestHeight = 16 # Placeholder value for now
# We create a floor and mark it with a different texture
orig = vmflib.types.Vertex(0,0, 0-scaled_lowestHeight//2)
floor = tools.Block(origin = orig, dimensions=(xSize_real, ySize_real, scaled_lowestHeight))
floor.set_material("nature/dirtfloor012a")
m.world.children.append(floor)
print "Building map finished."
print "Time taken for map building: {0}".format(time.clock()-creationTime)
print "Now saving data with vmflib..."
vmfTime = time.clock()
m.write_vmf("output/wmflibtestmap.vmf")
print "Time taken for vmf file creation: {0}".format(time.clock()-vmfTime)
print "Everything saved."