display-name: "Games/Dangerous Dave"
meta: {"Working": ["All graphics", "Sounds", "Levels"]}

palette-ega: [
 0x00, 0x00, 0x00,
 0x55, 0x55, 0x55,
 0xAA, 0x00, 0x00,
 0xFF, 0x55, 0x55,
 0x00, 0xAA, 0x00, 
 0x55, 0xFF, 0x55,
 0xAA, 0x55, 0x00, 
 0xFF, 0xFF, 0x55,
 0x00, 0x00, 0xAA,
 0x55, 0x55, 0xFF, 
 0xAA, 0x00, 0xAA,
 0xFF, 0x55, 0xFF,
 0x00, 0xAA, 0xAA,
 0x55, 0xFF, 0xFF,
 0xAA, 0xAA, 0xAA,
 0xFF, 0xFF, 0xFF
]

if (file.name == "egadave.dav")
{
	interpret-as "dave-archive"
	{
		dave-image-type: "I4"
	}
}

file-format dave-archive
{
	unsigned32 file-count

	//Keep variable in context
	dave-image-format: dave-image-type

	//We take the first 53 of these as tiles
	unsigned32[53] tile-offsets
	file[1]
	{
		folder: ""
		name: "Tiles"
		offset: tile-offsets[0]
		size: (tile-offsets[1] - tile-offsets[0]) * 53

		interpret-as "dave-tiles"
	}

	file-number: 0
	file [file-count - 53]
	{
		folder: "Sprites"
		unsigned32 offset

		if (offset >= 65536)
		{
			offset = offset + 1
		}

		name: "Sprite " + file-number
		file-number = file-number + 1

		interpret-as "dave-image"
	}
}

if (file.name == "dave.exe")
{
	file[1]
	{
		offset: 0xc620
		name: "CGA Images"
		compressed "DaveRLE"
		dave-image-type: "I2"
		interpret-as "dave-archive"
	}
	file[1]
	{
		offset: 0x120f0
		name: "VGA Images"
		compressed "DaveRLE"
		dave-image-type: "I8"
		interpret-as "dave-archive"
	}
	file[1]
	{
		offset: 0x1c4e0
		name: "Sounds"
		interpret-as "archive-SND_DOS"
	}
	file[1]
	{
		offset: 0x1d780
		name: "CGA Fonts"
		interpret-as "dave-cga-fontimage"
	}
	file[1]
	{
		offset: 0x1ea40
		name: "EGA Fonts"
		interpret-as "dave-ega-fontimage"
	}
	file[1]
	{
		offset: 0x20ec0
		name: "VGA Fonts"
		interpret-as "dave-vga-fontimage"
	}
	file[1]
	{
		offset: 0x26b0a
		name: "VGA Palette"
		size: 768
		interpret-as "dave-vga-palette"
	}
	level-index: 0
	
	file[10]
	{
		folder: "Levels"
		name: "Level " + level-index
		size: 1280
		offset: 0x26e0a + (level-index * 1280)
		level-index = level-index + 1
		interpret-as "dave-level"
	}
}

file-format dave-ega-fontimage
{
	header-size: 0x100
	file[1]
	{
		name: "EGA font"
		offset: header-size
		size: 8*8*128
		interpret-as "dave-ega-font"
	}

	at 0x28:
	file[13]
	{
		offset: 0
		size: file.size
		unsigned16 _width
		width: _width * 8
		unsigned16 height
		unsigned32 _offset
		fixed-string(8) name
		image-offset: _offset + header-size + (8*128)
		interpret-as "dave-ega-fontsubimage"
	}
}

file-format dave-cga-fontimage
{
	header-size: 0x100
	file[1]
	{
		name: "CGA font"
		offset: header-size
		size: 8*8*128/4
		interpret-as "dave-cga-font"
	}

	at 0x28:
	file[13]
	{
		size: file.size
		unsigned16 _width
		width: _width * 4
		unsigned16 height
		unsigned32 _offset
		fixed-string(8) name
		offset: _offset + header-size + (8*8*128/4)
		interpret-as "dave-cga-fontsubimage"
	}
}

file-format dave-cga-font
{
	image
	{
		width: 8
		height: 8
		tile-count: 128
		tiles-across: 32
		format: "I2"
		bit-stride: 1
	}
}

file-format dave-ega-font
{
	image
	{
		width: 8
		height: 8
		tile-count: 128
		tiles-across: 32
		format: "I4"
		bit-stride: 64
		per-image-offset: 8
		plane-offset: [0, 2272, 4544, 6816] 
		//data-size: file.size
	}
}

file-format dave-vga-fontimage
{
	header-size: 0x100
	file[1]
	{
		name: "VGA font"
		offset: header-size
		size: 8*8*128
		interpret-as "dave-vga-font"
	}

	at 0x28:
	file[13]
	{
		unsigned16 width
		unsigned16 height
		unsigned32 _offset
		fixed-string(8) name
		offset: _offset + header-size + (8*8*128)
		interpret-as "dave-vga-fontsubimage"
	}
}

file-format dave-vga-font
{
	image
	{
		width: 8
		height: 8
		tile-count: 128
		tiles-across: 32
		format: "I8"
		palette: "dave.exe\\vga palette"
	}
}

file-format dave-cga-fontsubimage
{
	image
	{
		format: "I2"
		bit-stride: 1
	}
}

file-format dave-vga-fontsubimage
{
	image
	{
		format: "I8"
		palette: "dave.exe\\vga palette"
	}
}

file-format dave-ega-fontsubimage
{
	image
	{
		format: "I4"
		bit-stride: width * height
		plane-offset: [0 + image-offset, 2272 + image-offset, 4544 + image-offset, 6816 + image-offset] 
	}
}

file-format dave-level
{
	folder: "Levels"

	unsigned8[256] path

	level
	{
		grid-x: 16
		grid-y: 16

		width: 100
		height: 10

		level-layer
		{
			data-type: "unsigned8"
			order: 0
			layer-image: "egadave.dav\\tiles"
			name: "Background"
		}
	}
}

if (file.name == "dscores.dav")
{
	loop (5)
	{
		property-list
		{
			unsigned32 score
			unsigned16 level
			fixed-string(3) name
		}
	}
}

file-format dave-vga-palette
{
	palette
	{
		size: 256
		format: "R6G6B6"
	}
}

file-format dave-image
{
	unsigned16 width
	unsigned16 height-with-offset
	image
	{
		format: dave-image-format
		height: height-with-offset
		data-size: file.size - 4 //bytesRemaining
		tiles-across: 40
		
		if (format == "I4")
		{
			palette: palette-ega
			bit-stride: width
		}
		else if (format == "I8")
		{
			palette: "dave.exe\\vga palette"
		}
		else 
		{
			if ((width % 2) == 1)
			{
				width = width + 1
			} 
		}
	}
}

file-format dave-tiles
{
	image
	{
		format: dave-image-format
		width: 16
		height: 16

		if (format == "I4")
		{
			palette: palette-ega
			bit-stride: width
		}
		else if (format == "I8")
		{
			palette: "dave.exe\\vga palette"
		}

		tile-count: 53
		tiles-across: 10
	}
}

function DaveRLE
{
	read unsigned32 max-count	

	buffer: []
	running-total: 0

	loop while (file.remaining-bytes > 0)
	{
		read unsigned8 b	

		if ((b & 0x80) != 0)
		{
			byte-count: (b & 0x7F) + 1

			loop (byte-count)
			{
				read unsigned8 c				
				write unsigned8 c
			}
		}
		else
		{
			byte-count: b + 3
			read unsigned8 c
			
			loop (byte-count)
			{
				write unsigned8 c
			}
		}		
	}
}