-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwadzilla.py
343 lines (280 loc) · 13.6 KB
/
wadzilla.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
336
337
338
339
340
341
342
343
import os
import sys
import json
import requests
from bs4 import BeautifulSoup
import struct
import argparse
class WADFile:
def __init__(self, filename):
self.filename = filename
self.lumps = {}
self.read_wad()
def read_wad(self):
with open(self.filename, 'rb') as f:
header = f.read(12)
wad_type, num_lumps, dir_offset = struct.unpack('4sII', header)
f.seek(dir_offset)
for _ in range(num_lumps):
lump_data = f.read(16)
lump_offset, lump_size, lump_name = struct.unpack('II8s', lump_data)
lump_name = lump_name.strip(b'\x00').decode('ascii')
self.lumps[lump_name] = (lump_offset, lump_size)
def read_lump(self, lump_name):
if lump_name in self.lumps:
offset, size = self.lumps[lump_name]
with open(self.filename, 'rb') as f:
f.seek(offset)
return f.read(size)
else:
raise ValueError(f'Lump {lump_name} not found in WAD.')
def adjust_coordinates(self, existing_zil_map):
# Calculate offset to align room 0's starting location with existing ZIL map
# and adjust coordinates of all rooms accordingly
portal_room_id = len(existing_zil_map)
portal_coordinates = (x, y)
existing_zil_map[portal_room_id] = f"ROOM {portal_room_id}: A mysterious atmosphere fills the room. " \
f"A glowing green disc appears on the floor."
self.rooms[0].add_portal(portal_coordinates)
def parse_vertexes(data):
vertexes = []
for i in range(0, len(data), 4):
x, y = struct.unpack('hh', data[i:i+4])
vertexes.append((x, y))
return vertexes
def parse_linedefs(data):
linedefs = []
for i in range(0, len(data), 14):
v1, v2, flags, types, tag, right_sidedef, left_sidedef = struct.unpack('hhhhhhh', data[i:i+14])
linedefs.append((v1, v2, flags, types, tag, right_sidedef, left_sidedef))
return linedefs
def parse_sidedefs(data):
sidedefs = []
for i in range(0, len(data), 30):
x_offset, y_offset, upper_tex, lower_tex, middle_tex, sector_id = struct.unpack('hh8s8s8sh', data[i:i+30])
sidedefs.append((x_offset, y_offset, upper_tex.strip(b'\x00').decode(), lower_tex.strip(b'\x00').decode(), middle_tex.strip(b'\x00').decode(), sector_id))
return sidedefs
def parse_sectors(data):
sectors = []
for i in range(0, len(data), 26):
floor_height, ceiling_height, floor_tex, ceiling_tex, light_level, sector_type, tag = struct.unpack('hh8s8shhh', data[i:i+26])
sectors.append((floor_height, ceiling_height, floor_tex.strip(b'\x00').decode(), ceiling_tex.strip(b'\x00').decode(), light_level, sector_type, tag))
return sectors
def parse_things(data):
things = []
for i in range(0, len(data), 10):
x, y, angle, type, flags = struct.unpack('hhhhH', data[i:i+10])
things.append((x, y, type))
return things
def point_in_polygon(x, y, polygon):
num = len(polygon)
j = num - 1
c = False
for i in range(num):
if ((polygon[i][1] > y) != (polygon[j][1] > y)) and \
(x < polygon[i][0] + (polygon[j][0] - polygon[i][0]) * (y - polygon[i][1]) / (polygon[j][1] - polygon[i][1])):
c = not c
j = i
return c
def scrape_texture_descriptions(url):
# stub until I arrive at a way to describe the textures. The script will just use the texture names for now.
# response = requests.get(url)
# soup = BeautifulSoup(response.text, 'html.parser')
texture_dict = {}
# for table in soup.find_all('table', class_='wikitable'):
# for row in table.find_all('tr')[1:]:
# cols = row.find_all('td')
# if len(cols) >= 2:
# texture_name = cols[0].get_text(strip=True)
# description = cols[1].get_text(strip=True)
# texture_dict[texture_name] = description
return texture_dict
def scrape_thing_types(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
thing_dict = {}
tables = soup.find_all('table', class_='wikitable')
for table in tables:
# Check if the table header matches the one for "Doom, Doom II, Final Doom"
header = table.find_previous('h2')
if header and 'Doom, Doom II, Final Doom' in header.get_text():
for row in table.find_all('tr')[1:]: # Skip the header row
cols = row.find_all('td')
if len(cols) >= 9:
type_id = cols[0].get_text(strip=True)
description = cols[8].get_text(strip=True)
if type_id.isdigit():
thing_dict[int(type_id)] = description
return thing_dict
class Room:
def __init__(self, sector_id, sector_data, offset=(0, 0)):
# Initialize Room instance with optional offset for coordinates
self.sector_id = sector_id
self.floor_height, self.ceiling_height, self.floor_tex, self.ceiling_tex, self.light_level, self.type, self.tag = sector_data
self.vertexes = set()
self.linedefs = []
self.things = []
self.wall_textures = []
self.offset = offset
self.portal_coordinates = None # Portal entrance coordinates
def add_vertex(self, vertex):
# Add vertex to room, adjusting coordinates based on offset
self.vertexes.add((vertex[0] + self.offset[0], vertex[1] + self.offset[1]))
def add_portal(self, coordinates):
# Add portal entrance coordinates to the room
self.portal_coordinates = coordinates
def add_vertex(self, vertex):
self.vertexes.add(vertex)
def add_linedef(self, linedef, sidedefs):
self.linedefs.append(linedef)
v1, v2, flags, types, tag, right_sidedef, left_sidedef = linedef
right_sidedef_data = sidedefs[right_sidedef]
left_sidedef_data = sidedefs[left_sidedef] if left_sidedef != -1 else None
self.wall_textures.append({
'right': {
'upper': right_sidedef_data[2] if right_sidedef_data[2] != '-' else None,
'lower': right_sidedef_data[3] if right_sidedef_data[3] != '-' else None,
'middle': right_sidedef_data[4] if right_sidedef_data[4] != '-' else None
},
'left': {
'upper': left_sidedef_data[2] if left_sidedef_data and left_sidedef_data[2] != '-' else None,
'lower': left_sidedef_data[3] if left_sidedef_data and left_sidedef_data[3] != '-' else None,
'middle': left_sidedef_data[4] if left_sidedef_data and left_sidedef_data[4] != '-' else None
}
})
def add_thing(self, thing):
self.things.append(thing)
def describe_zil(self, texture_descriptions, thing_type_descriptions):
description = f"<ROOM ROOM-{self.sector_id}\n"
description += f" (LOC ROOMS)\n"
description += f" (DESC \"{self.floor_tex}/{self.ceiling_tex} Room\")\n"
if self.portal_coordinates:
description += " (PORTAL)\n"
floor_desc = texture_descriptions.get(self.floor_tex, self.floor_tex)
ceiling_desc = texture_descriptions.get(self.ceiling_tex, self.ceiling_tex)
description += f" (FLOOR \"{floor_desc}\")\n"
description += f" (CEILING \"{ceiling_desc}\")\n"
for thing in self.things:
x, y, type = thing
thing_desc = thing_type_descriptions.get(type, f"Unknown type {type}")
description += f" (THING \"{thing_desc}\" AT ({x}, {y}))\n"
for wall in self.wall_textures:
if wall['right']['upper'] or wall['right']['middle'] or wall['right']['lower']:
description += " (WALL-RIGHT"
if wall['right']['upper']:
description += f" UPPER \"{wall['right']['upper']}\""
if wall['right']['middle']:
description += f" MIDDLE \"{wall['right']['middle']}\""
if wall['right']['lower']:
description += f" LOWER \"{wall['right']['lower']}\""
description += ")\n"
if wall['left']['upper'] or wall['left']['middle'] or wall['left']['lower']:
description += " (WALL-LEFT"
if wall['left']['upper']:
description += f" UPPER \"{wall['left']['upper']}\""
if wall['left']['middle']:
description += f" MIDDLE \"{wall['left']['middle']}\""
if wall['left']['lower']:
description += f" LOWER \"{wall['left']['lower']}\""
description += ")\n"
description += " (FLAGS RLANDBIT)\n"
description += ">\n"
return description
def main():
parser = argparse.ArgumentParser(description='Process WAD files and output room descriptions.')
parser.add_argument('-b', '--basewad', required=True, help='The base WAD file (e.g., doom1.wad)')
parser.add_argument('-f', '--file', required=False, help='The patch WAD file (e.g., some_mod_pwad.wad)')
parser.add_argument('-z', '--existing_zil_map', required=False, help='The patch WAD file (e.g., some_mod_pwad.wad)')
parser.add_argument('-o', '--output', required=False, help='The output file for ZIL code', default='output.zil')
parser.add_argument('-v', '--verbose', action='store_true', help='Enable debug logging')
args = parser.parse_args()
def debug_log(message):
if args.verbose:
print(message, file=sys.stderr)
if not os.path.exists(args.basewad):
print(f"Base WAD file '{args.basewad}' not found.")
sys.exit(1)
wad = WADFile(args.basewad)
if args.file:
if not os.path.exists(args.file):
print(f"Patch WAD file '{args.file}' not found.")
sys.exit(1)
patch_wad = WADFile(args.file)
wad = wad.merge(patch_wad)
if args.existing_zil_map:
existing_zil_map = load_existing_zil_map(args.existing_zil_map)
wad.adjust_coordinates(existing_zil_map)
else:
existing_zil_map = {}
# Define paths
DATA_DIR = 'data'
TEXTURE_DESCRIPTIONS_FILE = os.path.join(DATA_DIR, 'texture_descriptions.json')
THING_TYPES_FILE = os.path.join(DATA_DIR, 'thing_types.json')
os.makedirs(DATA_DIR, exist_ok=True)
# WIP: Scrape and save texture descriptions if not present
if not os.path.exists(TEXTURE_DESCRIPTIONS_FILE):
texture_url = 'https://doomwiki.org/wiki/Texture'
textures = scrape_texture_descriptions(texture_url)
with open(TEXTURE_DESCRIPTIONS_FILE, 'w') as f:
json.dump(textures, f, indent=4)
# Scrape and save thing types if not present
if not os.path.exists(THING_TYPES_FILE):
thing_url = 'https://doomwiki.org/wiki/Thing_types_by_number'
things = scrape_thing_types(thing_url)
with open(THING_TYPES_FILE, 'w') as f:
json.dump(things, f, indent=4)
# Load the texture descriptions from the JSON file
with open(TEXTURE_DESCRIPTIONS_FILE, 'r') as f:
texture_descriptions = json.load(f)
# Load the thing descriptions from the JSON file
with open(THING_TYPES_FILE, 'r') as f:
thing_type_descriptions = json.load(f)
thing_type_descriptions = {int(k): v for k, v in thing_type_descriptions.items()}
vertex_data = wad.read_lump('VERTEXES')
linedef_data = wad.read_lump('LINEDEFS')
sidedef_data = wad.read_lump('SIDEDEFS')
sector_data = wad.read_lump('SECTORS')
thing_data = wad.read_lump('THINGS')
vertexes = parse_vertexes(vertex_data)
linedefs = parse_linedefs(linedef_data)
sidedefs = parse_sidedefs(sidedef_data)
sectors = parse_sectors(sector_data)
things = parse_things(thing_data)
rooms = {}
for i, sector_data in enumerate(sectors):
rooms[i] = Room(i, sector_data)
for linedef in linedefs:
v1, v2, flags, types, tag, right_sidedef, left_sidedef = linedef
right_sector_id = sidedefs[right_sidedef][5]
rooms[right_sector_id].add_linedef(linedef, sidedefs)
rooms[right_sector_id].add_vertex(vertexes[v1])
rooms[right_sector_id].add_vertex(vertexes[v2])
if left_sidedef != -1:
left_sector_id = sidedefs[left_sidedef][5]
rooms[left_sector_id].add_linedef(linedef, sidedefs)
rooms[left_sector_id].add_vertex(vertexes[v1])
rooms[left_sector_id].add_vertex(vertexes[v2])
if args.verbose:
for room_id, room in rooms.items():
debug_log(f"Room {room_id} vertices: {room.vertexes}")
for thing in things:
x, y, type = thing
added = False
for room in rooms.values():
if point_in_polygon(x, y, list(room.vertexes)):
room.add_thing(thing)
added = True
break
if not added:
if args.verbose:
debug_log(f"Thing at ({x}, {y}) of type {type} not added to any room.")
with open(args.output, 'w') as f:
for room in rooms.values():
zil_description = room.describe_zil(texture_descriptions, thing_type_descriptions)
if args.verbose:
debug_log(zil_description)
f.write(zil_description)
f.write('\n')
debug_log(f"ZIL descriptions written to {args.output}")
if __name__ == "__main__":
main()