#!/usr/bin/python
# generate a map
# map will be bottom-left (0,0) - 256 x 256 in dimensions.
# player will be at (32,32) due north
# sector 0 will be the only sector with light level 120, floor 0,
# ceiling 128, no specials
# floor will be floor0_1
# sky will be f_sky1
# linedefs will all have a mid-texture STARTAN2
# things first: each int is 2 bytes le;
from struct import pack, unpack
import sys
x = y = 32
angle = 0
doomed = 0x01 # player start
flags = 0x0001 | 0x0002 | 0x0004
THINGS = pack("<5h", x, y, angle, doomed, flags)
# vertices next: we'll go clockwise from the start point
vertices = [ (0,0), (0,128), (0,256), (256,256), (256,128), (256,0) ]
VERTEXES = ''.join(
map(''.join,
map(lambda tuple: map(lambda x: pack("<h", x), tuple), vertices)
)
)
# sectors
# a 32 bit int it seems
floor = 0
ceil = 128
ftex = "FLOOR0_1"
ctex = "F_SKY1\0\0"
light = 120
type = 0
tag = 0
sector = pack("<hh8s8shhh", floor,ceil,ftex,ctex,light,type,tag)
SECTORS = sector * 2
# sidedefs
# x off / y off / upper / lower / middle / sector number
blank = "-%s" % (7 * '\0')
tex = "STARTAN2"
xoff = yoff = 0
#sidedefs = pack("<hh8s8s8sh", xoff, yoff, blank, blank, tex, sector)
sidedefs = [
(0, 0, blank, blank, tex, 0),
(0, 0, blank, blank, tex, 1),
(0, 0, blank, blank, tex, 1),
(0, 0, blank, blank, tex, 1),
(0, 0, blank, blank, tex, 0),
(0, 0, blank, blank, tex, 0),
(0, 0, blank, blank, tex, 0),
(0, 0, blank, blank, tex, 1)
]
# first pass: concatenate all the lists
SIDEDEFS = []
for list in sidedefs:
SIDEDEFS += list
# second pass: run pack on it all
SIDEDEFS = map(lambda x: pack("<hh8s8s8sh", x), SIDEDEFS)
# third pass: concatenate the result
tmp = ''
for bit in SIDEDEFS:
tmp += bit
SIDEDEFS = tmp
# linedefs now
# 14 bytes long (7 x shorts)
# start v / end v / flags / special / tag / r side / l side
# what value is used for a non-existent sidedef? -1
# flags 1 = blocks
linedefs = [
(0, 1, 1, 0, 0, 0, -1),
(1, 2, 1, 0, 0, 1, -1),
(2, 3, 1, 0, 0, 2, -1),
(3, 4, 1, 0, 0, 3, -1),
(4, 5, 1, 0, 0, 4, -1),
(5, 0, 1, 0, 0, 5, -1),
(1, 4, 1, 0, 0, 6, 7)
]
# first pass: concatenate all the lists
LINEDEFS = []
for list in linedefs:
LINEDEFS += list
# second pass: run pack on it all
LINEDEFS = map(lambda x: pack("<h", x), LINEDEFS)
# third pass: concatenate the result
tmp = ''
for bit in LINEDEFS:
tmp += bit
LINEDEFS = tmp
# build the directory
hoffs = 12 # header length offset
MAP01 = ''
lumps = {
"MAP01\0\0\0":MAP01,
"THINGS\0\0": THINGS,
"VERTEXES": VERTEXES,
"LINEDEFS": LINEDEFS,
"SIDEDEFS": SIDEDEFS,
"SECTORS\0": SECTORS
}
order = [
"MAP01\0\0\0",
"THINGS\0\0",
"VERTEXES",
"LINEDEFS",
"SIDEDEFS",
"SECTORS\0"
]
filepos = hoffs
dir = ''
for k in order:
v = lumps[k]
size = len(v)
ent = pack("<ll8s", filepos, size, k)
dir += ent
filepos += size
# now the data
data = MAP01 + THINGS + VERTEXES + LINEDEFS + SIDEDEFS + SECTORS
# now the header
header = pack("<4sll", "PWAD", len(lumps.values()), len(data) + hoffs)
# now write it all out
sys.stdout.write(header)
sys.stdout.write(data)
sys.stdout.write(dir)