display-name: "Games/Dangerous Dave 2"
meta: {"Working": ["Graphics", "Levels", "Audio"], "Thanks to": "Andy Durdin"}

if (file.name == "egatiles.dd2")
{
	image
	{
		format: "I4"
		width: 16
		height: 16
		data-size: file.size
		tiles-across: 13
		bit-stride: width * height
	}
}

if (file.name == "haunted.exe")
{
	file[1]
	{
		name: "Sounds"
		offset: 0x259A0
		size: 0x1970
		interpret-as "archive-SND_DOS"
	}

	file[1]
	{
		name: "Death images"
		offset: 0x18BE0
		size: 0x33B0 * 4
		interpret-as "dave2-death"
	}
}

file-format dave2-death
{
	image
	{
		width: 48
		height: 48
		format: "I4"
		bit-stride: 0x19D80
		data-size: file.size
		tile-count: 40
		tiles-across:5
		per-image-offset: 48*48/8
	}

	at 48*48*40/8:
	image
	{
		width: 208
		height: 56
		format: "I4"
		data-size: file.remaining-bytes
		bit-stride: 0x19D80
	}
}

if (file.name like ["title*.dd2", "*pic.dd2"])
{
	fixed-string(4) header == "HUFF"
	unsigned32 decompressed-size
	unsigned16[510] huff-table

	compressed[file.size - 1028] "keen-huffman"
	{
		at 8:

		image
		{
			format: "I4"
			width: 320
			height: 200
			bit-stride: 320 * 200
		}
	}
}

level-sprite-mapping: {
	1: "s_chunk1.dd2\\0",
	2: "s_chunk1.dd2\\18",
	3: "s_chunk2.dd2\\18",
	4: "s_chunk2.dd2\\0", //gargoyle
	5: "s_chunk2.dd2\\5", //gargoyle
	6: "s_chunk2.dd2\\27",
	7: "s_chunk2.dd2\\36", //flaming skull
	9: "s_chunk2.dd2\\47", //werewolf
	11: "s_frank.dd2\\0", //Frank
	14: "s_dave.dd2\\56",
	15: "s_dave.dd2\\58",
	16: "s_dave.dd2\\60",
	17: "s_dave.dd2\\62",
	18: "s_dave.dd2\\64",
	19: "s_dave.dd2\\66",
	255: "s_dave.dd2\\0",
	0x0633: "debug",
	0x0a06: "debug",
	0x8000: "debug",
	0x8001: "s_dave.dd2\\56",
	0x8002: "s_dave.dd2\\58",
	0x8003: "s_dave.dd2\\60", 
	0x8004: "s_dave.dd2\\62",
	0x8005: "s_dave.dd2\\64",
	0x8006: "s_dave.dd2\\66"
}

if (file.name like "level??.dd2")
{
	unsigned32 decompressed-size
	magic-number: 0xFEFE

	level
	{
		grid-x: 16
		grid-y: 16
		compressed[file.remaining-bytes - 1] "KeenRLE"
		{
			unsigned16 width
			unsigned16 height
			unsigned16 level-plane-count //always 2
			unsigned32 zero //always 0
			unsigned32 ted-variables
			unsigned16 level-plane-size
			data(16) unknown

			level-layer
			{
				data-type: "unsigned16"
				layer-image: "egatiles.dd2"
				order: 0
			}

			at level-plane-size + 32:

			level-layer
			{
				layer-name: "Objects"
				ignore-tile: 0
				data-type: "unsigned16"
				layer-images: level-sprite-mapping
				order: 1
				max-valid-tile: 0xFF
				dump-tile-id: true
			}
		}
	}
}

sprite-data:
{
	"s_dave.dd2": {"sprite-count": 82, "exe-offset": 0x127E0},
	"s_frank.dd2": {"sprite-count": 10, "exe-offset": 0x17EE0},
	"s_chunk1.dd2": {"sprite-count": 32, "exe-offset": 0x15260},
	"s_chunk2.dd2": {"sprite-count": 52, "exe-offset": 0x16460},
	"s_master.dd2": {"sprite-count": 10, "exe-offset": 0x18560}
}

file-format dave2-sprite-indexer
{
	full-file-size: file.size
	full-name: file.name
	file [sprite-data[file.name].sprite-count]: i
	{
		from "haunted.exe" at sprite-data[file.name].exe-offset + (i * 0x80)
		{
			unsigned16 _width
			width: _width * 8
			unsigned16 height
		}

		sprite-index: i
		name: "" + i
		offset: 0
		size: full-file-size
		original-name: full-name
		interpret-as "dave2-sprite-internal"
	}
}

file-format dave2-sprite-internal
{
	fixed-string(4) header == "HUFF"
	unsigned32 decompressed-size
	unsigned16[510] huff-table

	compressed[file.remaining-bytes] "keen-huffman"
	{
		unsigned16 _bit-stride
		//data(8) unknown
		bit-stride: _bit-stride * 8

		seek-to: 10

		image-layout-width: 400
		image-layout
		{
			loop (sprite-data[original-name].sprite-count): i
			{
				from "haunted.exe" at sprite-data[original-name].exe-offset + (i * 0x80)
				{
					unsigned16 _width
					width: _width * 8

					unsigned16 height
					data(12) unknown
					data(16) unknown
				}

				at seek-to:

				if (i == sprite-index)
				{
				image
				{
					format: "I4A1"
					alpha-mask: 1
					data-size: decompressed-size - seek-to
				}
				}

				seek-to = seek-to + (height * _width)
			}
		}
	}
}

if (file.name like "s_*.dd2")
{
	interpret-as-internally "dave2-sprite-indexer"
	
	fixed-string(4) header == "HUFF"
	unsigned32 decompressed-size
	unsigned16[510] huff-table

	compressed[file.remaining-bytes] "keen-huffman"
	{
		unsigned16 _bit-stride
		//data(8) unknown
		bit-stride: _bit-stride * 8

		seek-to: 10

		image-layout-width: 400
		image-layout
		{
			loop (sprite-data[file.name].sprite-count): i
			{
				from "haunted.exe" at sprite-data[file.name].exe-offset + (i * 0x80)
				{
					unsigned16 _width
					width: _width * 8

					unsigned16 height
					data(12) unknown
					data(16) unknown
				}

				at seek-to:

				image
				{
					format: "I4A1"
					alpha-mask: 1
					data-size: decompressed-size - seek-to
				}

				seek-to = seek-to + (height * _width)
			}
		}
	}
}

if (file.name == "intro.dd2")
{
	unsigned32 decompressed-size

	magic-number: 0xFE
	compressed[file.size - 4] "KeenRLEB"
	{
		image
		{
			width: 256
			height: 64
			format: "I4"
			bit-stride: width * height
		}
	}
}

file-format dave2-sprite
{
	unsigned16 _bit-stride
	data(8) unknown
	bit-stride: _bit-stride * 8

	seek-to: 10

	loop (4): i
	{
		from "haunted.exe" at 0x127E0 + (i * 0x80)
		{
			unsigned16 _width
			width: _width * 8

			unsigned16 height
			data(12) unknown
			data(16) unknown
		}

		at seek-to:

		image
		{
			format: "I4A1"
			alpha-mask: 1
			data-size: file.remaining-bytes
		}

		seek-to = seek-to + (height * _width)
	}
}