display-name: "Games/Wolfenstein 3D"
meta: {"Working": "Some images", "Thanks to": "Andy Durdin"}

palette: 
[   0x00, 0x00, 0x00, 0x00, 0x00, 0xA8, 0x00, 0xA8, 0x00, 0x00, 0xA8, 0xA8, 0xA8, 0x00, 0x00, 0xA8,
    0x00, 0xA8, 0xA8, 0x54, 0x00, 0xA8, 0xA8, 0xA8, 0x54, 0x54, 0x54, 0x54, 0x54, 0xFC, 0x54, 0xFC,
    0x54, 0x54, 0xFC, 0xFC, 0xFC, 0x54, 0x54, 0xFC, 0x54, 0xFC, 0xFC, 0xFC, 0x54, 0xFC, 0xFC, 0xFC,
    0xEC, 0xEC, 0xEC, 0xDC, 0xDC, 0xDC, 0xD0, 0xD0, 0xD0, 0xC0, 0xC0, 0xC0, 0xB4, 0xB4, 0xB4, 0xA8,
    0xA8, 0xA8, 0x98, 0x98, 0x98, 0x8C, 0x8C, 0x8C, 0x7C, 0x7C, 0x7C, 0x70, 0x70, 0x70, 0x64, 0x64,
    0x64, 0x54, 0x54, 0x54, 0x48, 0x48, 0x48, 0x38, 0x38, 0x38, 0x2C, 0x2C, 0x2C, 0x20, 0x20, 0x20,
    0xFC, 0x00, 0x00, 0xEC, 0x00, 0x00, 0xE0, 0x00, 0x00, 0xD4, 0x00, 0x00, 0xC8, 0x00, 0x00, 0xBC,
    0x00, 0x00, 0xB0, 0x00, 0x00, 0xA4, 0x00, 0x00, 0x98, 0x00, 0x00, 0x88, 0x00, 0x00, 0x7C, 0x00,
    0x00, 0x70, 0x00, 0x00, 0x64, 0x00, 0x00, 0x58, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x40, 0x00, 0x00,
    0xFC, 0xD8, 0xD8, 0xFC, 0xB8, 0xB8, 0xFC, 0x9C, 0x9C, 0xFC, 0x7C, 0x7C, 0xFC, 0x5C, 0x5C, 0xFC,
    0x40, 0x40, 0xFC, 0x20, 0x20, 0xFC, 0x00, 0x00, 0xFC, 0xA8, 0x5C, 0xFC, 0x98, 0x40, 0xFC, 0x88,
    0x20, 0xFC, 0x78, 0x00, 0xE4, 0x6C, 0x00, 0xCC, 0x60, 0x00, 0xB4, 0x54, 0x00, 0x9C, 0x4C, 0x00,
    0xFC, 0xFC, 0xD8, 0xFC, 0xFC, 0xB8, 0xFC, 0xFC, 0x9C, 0xFC, 0xFC, 0x7C, 0xFC, 0xF8, 0x5C, 0xFC,
    0xF4, 0x40, 0xFC, 0xF4, 0x20, 0xFC, 0xF4, 0x00, 0xE4, 0xD8, 0x00, 0xCC, 0xC4, 0x00, 0xB4, 0xAC,
    0x00, 0x9C, 0x9C, 0x00, 0x84, 0x84, 0x00, 0x70, 0x6C, 0x00, 0x58, 0x54, 0x00, 0x40, 0x40, 0x00,
    0xD0, 0xFC, 0x5C, 0xC4, 0xFC, 0x40, 0xB4, 0xFC, 0x20, 0xA0, 0xFC, 0x00, 0x90, 0xE4, 0x00, 0x80,
    0xCC, 0x00, 0x74, 0xB4, 0x00, 0x60, 0x9C, 0x00, 0xD8, 0xFC, 0xD8, 0xBC, 0xFC, 0xB8, 0x9C, 0xFC,
    0x9C, 0x80, 0xFC, 0x7C, 0x60, 0xFC, 0x5C, 0x40, 0xFC, 0x40, 0x20, 0xFC, 0x20, 0x00, 0xFC, 0x00,
    0x00, 0xFC, 0x00, 0x00, 0xEC, 0x00, 0x00, 0xE0, 0x00, 0x00, 0xD4, 0x00, 0x04, 0xC8, 0x00, 0x04,
    0xBC, 0x00, 0x04, 0xB0, 0x00, 0x04, 0xA4, 0x00, 0x04, 0x98, 0x00, 0x04, 0x88, 0x00, 0x04, 0x7C,
    0x00, 0x04, 0x70, 0x00, 0x04, 0x64, 0x00, 0x04, 0x58, 0x00, 0x04, 0x4C, 0x00, 0x04, 0x40, 0x00,
    0xD8, 0xFC, 0xFC, 0xB8, 0xFC, 0xFC, 0x9C, 0xFC, 0xFC, 0x7C, 0xFC, 0xF8, 0x5C, 0xFC, 0xFC, 0x40,
    0xFC, 0xFC, 0x20, 0xFC, 0xFC, 0x00, 0xFC, 0xFC, 0x00, 0xE4, 0xE4, 0x00, 0xCC, 0xCC, 0x00, 0xB4,
    0xB4, 0x00, 0x9C, 0x9C, 0x00, 0x84, 0x84, 0x00, 0x70, 0x70, 0x00, 0x58, 0x58, 0x00, 0x40, 0x40,
    0x5C, 0xBC, 0xFC, 0x40, 0xB0, 0xFC, 0x20, 0xA8, 0xFC, 0x00, 0x9C, 0xFC, 0x00, 0x8C, 0xE4, 0x00,
    0x7C, 0xCC, 0x00, 0x6C, 0xB4, 0x00, 0x5C, 0x9C, 0xD8, 0xD8, 0xFC, 0xB8, 0xBC, 0xFC, 0x9C, 0x9C,
    0xFC, 0x7C, 0x80, 0xFC, 0x5C, 0x60, 0xFC, 0x40, 0x40, 0xFC, 0x20, 0x24, 0xFC, 0x00, 0x04, 0xFC,
    0x00, 0x00, 0xFC, 0x00, 0x00, 0xEC, 0x00, 0x00, 0xE0, 0x00, 0x00, 0xD4, 0x00, 0x00, 0xC8, 0x00,
    0x00, 0xBC, 0x00, 0x00, 0xB0, 0x00, 0x00, 0xA4, 0x00, 0x00, 0x98, 0x00, 0x00, 0x88, 0x00, 0x00,
    0x7C, 0x00, 0x00, 0x70, 0x00, 0x00, 0x64, 0x00, 0x00, 0x58, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x40,
    0x28, 0x28, 0x28, 0xFC, 0xE0, 0x34, 0xFC, 0xD4, 0x24, 0xFC, 0xCC, 0x18, 0xFC, 0xC0, 0x08, 0xFC,
    0xB4, 0x00, 0xB4, 0x20, 0xFC, 0xA8, 0x00, 0xFC, 0x98, 0x00, 0xE4, 0x80, 0x00, 0xCC, 0x74, 0x00,
    0xB4, 0x60, 0x00, 0x9C, 0x50, 0x00, 0x84, 0x44, 0x00, 0x70, 0x34, 0x00, 0x58, 0x28, 0x00, 0x40,
    0xFC, 0xD8, 0xFC, 0xFC, 0xB8, 0xFC, 0xFC, 0x9C, 0xFC, 0xFC, 0x7C, 0xFC, 0xFC, 0x5C, 0xFC, 0xFC,
    0x40, 0xFC, 0xFC, 0x20, 0xFC, 0xFC, 0x00, 0xFC, 0xE0, 0x00, 0xE4, 0xC8, 0x00, 0xCC, 0xB4, 0x00,
    0xB4, 0x9C, 0x00, 0x9C, 0x84, 0x00, 0x84, 0x6C, 0x00, 0x70, 0x58, 0x00, 0x58, 0x40, 0x00, 0x40,
    0xFC, 0xE8, 0xDC, 0xFC, 0xE0, 0xD0, 0xFC, 0xD8, 0xC4, 0xFC, 0xD4, 0xBC, 0xFC, 0xCC, 0xB0, 0xFC,
    0xC4, 0xA4, 0xFC, 0xBC, 0x9C, 0xFC, 0xB8, 0x90, 0xFC, 0xB0, 0x80, 0xFC, 0xA4, 0x70, 0xFC, 0x9C,
    0x60, 0xF0, 0x94, 0x5C, 0xE8, 0x8C, 0x58, 0xDC, 0x88, 0x54, 0xD0, 0x80, 0x50, 0xC8, 0x7C, 0x4C,
    0xBC, 0x78, 0x48, 0xB4, 0x70, 0x44, 0xA8, 0x68, 0x40, 0xA0, 0x64, 0x3C, 0x9C, 0x60, 0x38, 0x90,
    0x5C, 0x34, 0x88, 0x58, 0x30, 0x80, 0x50, 0x2C, 0x74, 0x4C, 0x28, 0x6C, 0x48, 0x24, 0x5C, 0x40,
    0x20, 0x54, 0x3C, 0x1C, 0x48, 0x38, 0x18, 0x40, 0x30, 0x18, 0x38, 0x2C, 0x14, 0x28, 0x20, 0x0C,
    0x60, 0x00, 0x64, 0x00, 0x64, 0x64, 0x00, 0x60, 0x60, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x2C, 0x30,
    0x24, 0x10, 0x48, 0x00, 0x48, 0x50, 0x00, 0x50, 0x00, 0x00, 0x34, 0x1C, 0x1C, 0x1C, 0x4C, 0x4C,
    0x4C, 0x5C, 0x5C, 0x5C, 0x40, 0x40, 0x40, 0x30, 0x30, 0x30, 0x34, 0x34, 0x34, 0xD8, 0xF4, 0xF4,
    0xB8, 0xE8, 0xE8, 0x9C, 0xDC, 0xDC, 0x74, 0xC8, 0xC8, 0x48, 0xC0, 0xC0, 0x20, 0xB4, 0xB4, 0x20,
    0xB0, 0xB0, 0x00, 0xA4, 0xA4, 0x00, 0x98, 0x98, 0x00, 0x8C, 0x8C, 0x00, 0x84, 0x84, 0x00, 0x7C,
    0x7C, 0x00, 0x78, 0x78, 0x00, 0x74, 0x74, 0x00, 0x70, 0x70, 0x00, 0x6C, 0x6C, 0x98, 0x00, 0x88
]

if (file.name like "vswap.wl?")
{
	unsigned16 chunks-in-file
	unsigned16 sprite-start
	unsigned16 sound-start

	unsigned32[chunks-in-file] offsets
	unsigned16[chunks-in-file] sizes
	
	file[1]
	{
		offset: offsets[0]
		size: offsets[sprite-start] - offsets[0]
		name: "Walls"
		interpret-as "wolf3d-walls"
	}

	file[sound-start - sprite-start]: i
	{
		offset: offsets[sprite-start + i]
		size: sizes[sprite-start + i]
		name: "Sprite " + i
		interpret-as "wolf-sprite"

		skip-if size == 0
	}

	file[chunks-in-file - sound-start]: i
	{
		offset: offsets[sound-start + i]
		size: sizes[sound-start + i]
		name: "Sound " + i
		interpret-as "vswap-sound"

		skip-if size == 0
	}
}

file-format wolf-sprite
{
	compressed [file.size] "wolf-sprite-decompress"
	{
		image
		{
			width: 64
			height: 64
			format: "I8"
			overflow-transparent: true
			transparent-index: 0x100
		}
	}
}

function wolf-sprite-decompress
{
	decompressed-data: [](64 * 64 * 2)
	loop (64 * 64): i
	{
		decompressed-data[i * 2 + 1] = 0x1
	}

	unsigned16 left-texel
	unsigned16 right-texel

	unsigned16[right-texel - left-texel + 1] data-offsets

	first-texel: file.position

	loop(right-texel - left-texel + 1): x-i
	{
		x: x-i + left-texel
		
		running: true
		inst-offset: 0

		loop while (running == true)
		{
			at data-offsets[x-i] + inst-offset:
			read unsigned16 right-bit

			if (right-bit == 0)
			{
				running = false
			}
			else
			{
				unsigned16 unknown
				unsigned16 left-bit

				left-bit = left-bit / 2
				right-bit = right-bit / 2
				loop (right-bit - left-bit): y-i
				{	
					y: y-i + left-bit
					at first-texel
					{
						read unsigned8 pixel
						first-texel = first-texel + 1
					}

					if ((y < 64) and (x < 64)) {
						decompressed-data[((y * 64 + x) * 2)] = pixel
						decompressed-data[((y * 64 + x) * 2) + 1] = 0
					}
				}
			}

			inst-offset = inst-offset + 6
		}	
	}

	write unsigned8 decompressed-data
}

file-format vswap-sound
{
	sound
	{
		channels: 1
	        samples-per-second: 8000
            	bits-per-sample: 8
	}
}

file-format wolf3d-walls
{
	image
	{
		format: "I8"
		data-size: file.size
		width: 64
		height: 64
		columnar: true
		tiles-across: 6
	}
}

headfiles: {
	"vgagraph.wl1": {"head": "vgahead.wl1", "dict": "vgadict.wl1"},
	"vgagraph.wl6": {"head": "vgahead.wl6", "dict": "vgadict.wl6"}
}

if (file.name like "vgagraph.wl?")
{
	from headfiles[file.name].dict
	{
		unsigned16[512] huff-table
	}

	bitmap-count: 0
	packed-name: file.name

	file-number: 1

	from headfiles[file.name].head
	{
		file[1]
		{
			name: "Bitmaps header"
			unsigned24 _offset			

			from packed-name at _offset
			{
				unsigned32 decompressed-size
				bitmap-count = decompressed-size / 4				
			}

			offset: _offset + 4

			compressed "keen-huffman"
			file-number = file-number + 1
		}

		file[2]
		{
			name: "Font " + (file-number - 1)
			unsigned24 _offset
			
			from packed-name at _offset
			{
				unsigned32 decompressed-size			
			}

			offset: _offset + 4

			compressed "keen-huffman"
			file-number = file-number + 1
		}

		//files (
		//	name: "unknown"
		//	unsigned24 offset
		//)

		file [bitmap-count]
		{
			name: "Bitmap " + (file-number - 3)
			bitmap-number: file-number - 4
			
			unsigned24 _offset
			from packed-name at _offset
			{
				unsigned32 decompressed-size		
			}

			offset: _offset + 4

			compressed "keen-huffman"
			file-number = file-number + 1
		}

		file [1]
		{
			name: "Unmasked 8x8 tiles"
			unsigned24 offset

			decompressed-size: 72 * 8 * 8
			compressed "keen-huffman"

			interpret-as "wolf3d-unmasked8x8"
		}

		file [(file.remaining-bytes / 3) - 1]: i
		{
			name: "Extra file " + i
			unsigned24 _offset

			from packed-name at _offset
			{
				unsigned32 decompressed-size		
			}

			offset: _offset + 4
			compressed "keen-huffman"	

			if (i < 2)
			{
				name = "DOS Screen " + (i + 1)
			}
			else if ((i == 2) or (i == 7))
			{
				interpret-as "wolf3d-text"
				name = "Text " + (i + 1)
			}	
			else
			{
				folder: "Demos"
				name = "Demo " + (i + 1)
			}
		}
	}
}

file-format wolf3d-text
{
	text
}

if (file.name like "font ?")
{
	folder: "Fonts"

	unsigned16 height
	unsigned16[256] offsets
	unsigned8[256] widths

	current-letter: 0

	loop(256)
	{
		if (offsets[current-letter] != 0)
		{
			at offsets[current-letter]:

			image
			{
				format: "I8"
				width: widths[current-letter]
			}
		}

		current-letter = current-letter + 1
	}
}

if (file.name like "bitmap *")
{
	from "Bitmaps header" at (bitmap-number * 4)
	{
		unsigned16 width
		unsigned16 height
	}

	image
	{
		format: "I8"
		interlaced: 4
	}
}

if (file.name like "DOS Screen*")
{
	data(7) unknown
	b800-text
}

file-format wolf3d-unmasked8x8
{
	image
	{
		format:  "I8"
		data-size: file.size
		width: 8
		height: 8
		tiles-across: 10
		interlaced: 4
	}
}

/*if (file.name like "audiot.wl?")
{
	from "audiohed.wl" + file.name[9]
	{
		unsigned32[file.size / 4] offsets
	} 

	file[offsets.length]: i
	{
		offset: offsets[i]
		
		name: "Audio " + i + ", " + offset
	}
}*/