-
Notifications
You must be signed in to change notification settings - Fork 5
/
wad.py
133 lines (107 loc) · 3.81 KB
/
wad.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
import re
from omg import *
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('prefix')
parser.add_argument('wad')
parser.add_argument(
'-b',
'--behavior',
default=False,
help='path to compiled lump containing map behavior (default: None)')
parser.add_argument(
'-s',
'--script',
default=False,
help='path to script source lump containing map behavior (optional)')
BLOCK_SIZE = 96
def build_wall(maze):
things = []
linedefs = []
vertexes = []
v_indexes = {}
max_w = len(maze[0]) - 1
max_h = len(maze) - 1
def __is_edge(w, h):
return w in (0, max_w) or h in (0, max_h)
def __add_start(w, h):
x, y = w * BLOCK_SIZE, h * BLOCK_SIZE
x += int(BLOCK_SIZE / 2)
y += int(BLOCK_SIZE / 2)
things.append(ZThing(*[len(things) + 1000, x, y, 0, 0, 9001, 22279]))
def __add_vertex(w, h):
if (w, h) in v_indexes:
return
x, y = w * BLOCK_SIZE, h * BLOCK_SIZE
x += int(BLOCK_SIZE / 2)
y += int(BLOCK_SIZE / 2)
v_indexes[w, h] = len(vertexes)
vertexes.append(Vertex(x, y))
def __add_line(start, end, edge=False):
assert start in v_indexes
assert end in v_indexes
mask = 1
left = right = 0
if __is_edge(*start) and __is_edge(*end):
if not edge:
return
else:
# Changed the right side (one towards outside the map)
# to be -1 (65535 for Doom)
right = 65535
mask = 15
# Flipped end and start vertices to make lines "point" at right direction (mostly to see if it works)
line_properties = [v_indexes[end], v_indexes[start], mask
] + [0] * 6 + [left, right]
line = ZLinedef(*line_properties)
linedefs.append(line)
for h, row in enumerate(maze):
for w, block in enumerate(row.strip()):
if block == 'X':
__add_vertex(w, h)
else:
pass
corners = [(0, 0), (max_w, 0), (max_w, max_h), (0, max_h)]
for v in corners:
__add_vertex(*v)
for i in range(len(corners)):
if i != len(corners) - 1:
__add_line(corners[i], corners[i + 1], True)
else:
__add_line(corners[i], corners[0], True)
# Now connect the walls
for h, row in enumerate(maze):
for w, _ in enumerate(row):
if (w, h) not in v_indexes:
__add_start(w, h)
continue
if (w + 1, h) in v_indexes:
__add_line((w, h), (w + 1, h))
if (w, h + 1) in v_indexes:
__add_line((w, h), (w, h + 1))
return things, vertexes, linedefs
def main(flags):
new_wad = WAD()
for map_index, file_name in enumerate(
glob.glob('{}_*.txt'.format(flags.prefix))):
with open(file_name) as maze_source:
maze = [line.strip() for line in maze_source.readlines()]
maze = [line for line in maze if line]
new_map = MapEditor()
new_map.Linedef = ZLinedef
new_map.Thing = ZThing
new_map.behavior = Lump(from_file=flags.behavior or None)
new_map.scripts = Lump(from_file=flags.script or None)
things, vertexes, linedefs = build_wall(maze)
new_map.things = things + [ZThing(0, 0, 0, 0, 0, 1, 7)]
new_map.vertexes = vertexes
new_map.linedefs = linedefs
new_map.sectors = [Sector(0, 128, 'CEIL5_2', 'CEIL5_2', 240, 0, 0)]
new_map.sidedefs = [
Sidedef(0, 0, '-', '-', 'STONE2', 0),
Sidedef(0, 0, '-', '-', '-', 0)
]
new_wad.maps['MAP{:02d}'.format(map_index)] = new_map.to_lumps()
new_wad.to_file(flags.wad)
if __name__ == "__main__":
main(parser.parse_args())