-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
161 lines (128 loc) · 5.19 KB
/
convert.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
import mapedit
import wad
import collections
import sys
import json
import base64
import os.path
import nodes
if len(sys.argv) != 4:
print "enter filename, iwad, mapname"
sys.exit(1)
outfile, iwad, mapname = sys.argv[1:]
class DoomExporter:
def __init__(self, wadfile, mapname, texturereader):
print "wadfile", wadfile
self.w = wad.WAD(wadfile)
self.mapentry = self.w.maps[mapname]
self.texturereader = texturereader
m = mapedit.MapEditor(self.mapentry)
self.m = collections.namedtuple("doom_map", ["sectors", "linedefs", "segs", "ssectors", "sidedefs", "vertexes", "things"])
self.m.sectors = m.sectors
self.m.linedefs = m.linedefs
self.m.segs = m.segs
self.m.ssectors = m.ssectors
self.m.sidedefs = m.sidedefs
self.m.vertexes = m.vertexes
self.m.things = m.things
self.nodes = nodes.nodes(wadfile, mapname, self.m)
#self.nodes = None
self.export()
for thing in self.m.things:
if thing.type == 1:
player1 = [thing.x, thing.y]
map = {
"sectors": self.sectors,
"vertices": list(self.vertices),
"sidedefs": self.sidedefs,
"linedefs": self.linedefs,
"segs": self.segs,
"ssectors": self.ssectors,
"player1": player1,
"extents": self.extents
}
map["nodes"] = self.nodes
self.json = json.dumps(map, sort_keys=True, indent=4)
def export(self):
self.textures = self.__gettextures(self.m)
self.sectors = [{"tx_ceil": s.tx_ceil, "tx_floor": s.tx_floor, "z_ceil": s.z_ceil, "z_floor":s.z_floor, "light":s.light} for s in self.m.sectors]
self.linedefs = [{"vx_a": l.vx_a, "vx_b": l.vx_b, "right": l.front, "left": l.back} for l in self.m.linedefs]
self.vertices = [{"x":v.x, "y":v.y} for v in self.m.vertexes]
self.sidedefs = [{"sector":s.sector} for s in self.m.sidedefs]
self.segs = [{"vx_a": seg.vx_a, "vx_b": seg.vx_b, "line": seg.line, "side": seg.side} for seg in self.m.segs]
self.ssectors = [{"seg_a": ssector.seg_a, "numsegs":ssector.numsegs} for ssector in self.m.ssectors]
self.extents = {
"x1": min([v.x for v in self.m.vertexes]),
"x2": max([v.x for v in self.m.vertexes]),
"y1": min([v.y for v in self.m.vertexes]),
"y2": max([v.y for v in self.m.vertexes]),
}
def _a(self, line, side): # python2 lambdas are really restrictive, so have to have this function
if side == 0:
return self.m.sidedefs[self.m.linedefs[line].front].sector
elif side == 1:
return self.m.sidedefs[self.m.linedefs[line].back].sector
raise "what does side %i mean?" % side
def get_seg_sector(self, segs):
# find the first one that is not 65536 - special glbsp node
for s in segs:
if s != 65535:
return self._a(s.line, s.side)
raise "seg doesn't have a sector!"
def __gettextures(self, m):
textures = {}
a = 0
for sector in m.sectors:
for texture in texture_finder(sector.tx_floor.lower()):
textures[a] = texture
a += 1
return textures
class WrappedLinedef:
def __init__(self, vx_a, vx_b):
self.vx_a = vx_a
self.vx_b = vx_b
def texture_finder(texture):
animated_textures = [
["NUKAGE1", "NUKAGE2", "NUKAGE3"],
["FWATER1", "FWATER2", "FWATER3", "FWATER4"],
["SWATER1", "SWATER2", "SWATER3", "SWATER4"],
["LAVA1", "LAVA2", "LAVA3", "LAVA4"],
["BLOOD1", "BLOOD2", "BLOOD3"],
["RROCK05", "RROCK06", "RROCK07", "RROCK08"],
["SLIME01", "SLIME02", "SLIME03", "SLIME04"],
["SLIME05", "SLIME06", "SLIME07", "SLIME08"],
["SLIME09", "SLIME10", "SLIME11", "SLIME12"]
]
try:
return [x.lower() for x in filter(lambda x: texture.upper() in x, animated_textures)[0]]
except IndexError:
return [texture]
class TextureReader:
def __init__(self, basepath):
self.basepath = basepath
def getdata(self, texturename):
texturedata = base64.b64encode(open(os.path.join(self.basepath, texturename + ".png"), "r").read())
return "data:image/png;base64," + texturedata
def Map(texturedata, player1, extents, sectors, vertexes, linedefs, sidedefs, ssectors):
return {
"sectors": sectors,
"vertexes": vertexes,
"sidedefs": sidedefs,
"linedefs": linedefs,
"ssectors": ssectors,
"texturedata": texturedata,
"player1": player1,
"extents": extents
}
if __name__ == "__main__":
de = DoomExporter(iwad, mapname, TextureReader("/home/nicok/Downloads/jsdoom"))
print "----------------------------------------------"
print " WROTE "
print "----------------------------------------------"
print "%i vertices" % (len(de.vertices))
print "%i linedefs" % (len(de.linedefs))
print "%i sidedefs" % (len(de.sidedefs))
print "%i ssectors" % (len(de.ssectors))
print "%i sectors" % (len(de.sectors))
print "%i nodes" % (len(de.nodes))
open(outfile, "w").write(de.json)