Skip to content

Instantly share code, notes, and snippets.

@gschizas
Forked from jasonsperske/MIT-LICENSE
Created May 30, 2014 17:32
Show Gist options
  • Save gschizas/00fb24d622db59587982 to your computer and use it in GitHub Desktop.
Save gschizas/00fb24d622db59587982 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import struct
import re
class Wad:
def __init__(self,wadFile):
self.Maps = []
self._isHexenFormat = False
with open(wadFile, "rb") as f:
identification = f.read(4)
numlumps, infotableofs = struct.unpack("<II", f.read(8))
data = f.read(infotableofs-12)
currentMap = Map(None)
lump = f.read(16)
while lump != "":
# Do stuff with byte.
filepos, size = struct.unpack("<II", lump[0:8])
name = lump[8:16].rstrip('\0')
filepos = filepos-12
if(re.match('E\dM\d|MAP\d\d', name)):
if(currentMap.Name is not None):
self.Maps.append(currentMap)
currentMap = Map(name)
elif name == 'BEHAVIOR':
self._isHexenFormat = True
else:
currentMap.Lumps[name] = data[filepos:filepos+size]
lump = f.read(16)
self.Maps.append(currentMap)
for level in self.Maps:
level.load(self._isHexenFormat)
@staticmethod
def split(n, data):
size = len(data)
index = 0
while index < size:
yield data[index : index+n]
index = index + n
return
class Map:
def __init__(self,name):
self.Name = name
self.Lumps = dict()
self._Vertices = []
self.min = None
self.max = None
self.shift = None
self._Lines = []
def _readVerticies(self):
for vertex in Wad.split(4, self.Lumps['VERTEXES']):
x,y = struct.unpack('<hh', vertex[0:4])
self._Vertices.append((x,y))
if self.min is None:
self.min = (x,y)
else:
self.min = (min(x, self.min[0]), min(y, self.min[1]))
if self.max is None:
self.max = (x,y)
else:
self.max = (max(x, self.max[0]), max(y, self.max[1]))
self.shift = (0-self.min[0],0-self.min[1])
def _readLines(self,isHexenFormat):
lineSize = 16 if isHexenFormat else 14
for data in Wad.split(lineSize, self.Lumps['LINEDEFS']):
self._Lines.append(Line(data))
def readThings(self, data):
print 'Reading things'
def normalize(self,point,padding=5):
return (self.shift[0]+point[0]+padding,self.shift[1]+point[1]+padding)
def load(self, isHexenFormat):
self._readVerticies()
self._readLines(isHexenFormat)
def saveSVG(self):
import svgwrite
dwg = svgwrite.Drawing(self.Name+'.svg', profile='tiny', size=self.normalize(self.max, 10))
for line in self._Lines:
a = self.normalize(self._Vertices[line.A])
b = self.normalize(self._Vertices[line.B])
if line.LeftSide == -1 or line.RightSide == -1:
dwg.add(dwg.line(a, b, stroke=svgwrite.rgb(10, 10, 16, '%'), stroke_width=10))
else:
dwg.add(dwg.line(a, b, stroke=svgwrite.rgb(10, 10, 16, '%'), stroke_width=3))
dwg.save()
class Line:
def __init__(self,data):
self.A, self.B = struct.unpack('<hh', data[0:4])
self.LeftSide, self.RightSide = struct.unpack('<hh', data[-4:])
def __repr__(self):
return '('+str(self.A)+','+str(self.B)+')'
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
wad = Wad(sys.argv[1])
for level in wad.Maps:
level.saveSVG()
else:
print 'You need to pass a WAD file as the only argument'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment