display-name: "Games/The Lion King"
meta: {"Working": ["Packed file extraction", "Sprites (some incorrect colours)", "Tiles", "Sounds"]}

sprite-palettes:
{
	["sa*", "sy*", "complete.mch", "levels.mch", "porcu*", "*lvl1*", "rafiki2.mch", "up1.mch", "arrow*", "destiny.mch",
		 "circle.mch", "general.mch", "rain.mch"]: "level1.map",
	["confuse.mch", "lev2hipo.mch", "hipoeyes.mch", "rhino*", "rhintal3.mch", "giraf.mch", "monky*", "loglvl2.mch", 
		"ostr*", "spidr*", "branch.mch", "sparkle2.mch"]: "level2.map",
	["hyen*", "vult*", "*lvl3*", "bone*", "bontsk*", "walls.mch", "skin*", "wall2s.mch", "mufasa3.mch"]: "level3.map",
	["*lvl4*", "rocks.mch", "rckvn7sb.mch", "wb1*"]: "level4.map",
	["*lvl5*", "leaf2.mch", "splash.mch", "liz*", "bold*5.mch", "rock*", "bomb*"]: "level5.map",
	["frog*", "goril*", "crumblin.mch"]: "level6.map",
	["bugs.mch", "dust.mch", "bars.mch", "lvl2bird.mch", "chet*", "lv7*", "marm*", "gener.mch", "vine*"]: "level7.map",
	["log*.mch", "drip.mch", "lavabubs.mch", "plug8s.mch", "8gey*", "rafts.mch", "stalac8.mch", "f8x8_s.mch", "bat*"]: "level8.map",
	["bfire*", "fire*"]: "level9.map",
	["sc*"]: "level10.map",
	["rafiki.mch", "intro.mch", "title_s.mch", "1timons.mch", "continue.mch", "collect.mch", "gameover.mch", "sun.mch", "paws_gen.mch", "numbers.mch"]: "titles.map",
	["tm*", "pmeat.mch", "pumba*", "pumb*"]: "bonus.map",
	["*"]: "unknown"
}

if (file.name like "*.epf")
{
	//interpret-as "archive-epfs"
	unsigned32 signature //EPFS
	unsigned32 fat-offset
	unsigned8 unknown
	unsigned16 file-count

	data-start: 11 //current location

	at fat-offset:

	file [file-count]
	{
		fixed-string(13) name

		if ((name like "*.mch") and (sprite-palettes[name] == "unknown"))
		{	
			folder: "Sprites - unknown colours"
		} else if (name like "*.mch")
		{
			folder: "Sprites"
		}

		unsigned8 compression-flag
		unsigned32 size
		unsigned32 decompressed-size

		if (compression-flag == 1)
		{
			lz-readmethod: "8LToH"
			lz_resetfull: false
			lz-maxbits: 14
			lz-epf: true
			compressed "LZW"
		}
	}	

}

if (file.name like "*.mch")
{
	//folder: "Sprites"
	unsigned16 frame-count

	palette: sprite-palettes[file.name]
	if (palette == "unknown")
	{
		palette = "credits.map"
	}

	palette = palette + "\\palette"

	loop (frame-count)
	{
		unsigned16 width
		unsigned16 height
		unsigned32 offset

		at offset
		{
			compressed [file.remaining-bytes] "mch"
			{
				image
				{
					transparent-index: 0
					format: "I8"
				}
			}
		}
	}
}

if (file.name like "*.map")
{
	folder: "Levels"

	unsigned32 palette-offset
	unsigned32 tile-offset
	unsigned32 level-width
	unsigned32 level-height
	unsigned32 level-data-start

	at file.position - 12:

	//TODO: EXTRA DATA AFTER OFFSETS!

	file[1]
	{
		offset: palette-offset
		size: tile-offset - palette-offset
		name: "Palette"
		interpret-as "lionking-palette"
		folder: ""
	}

	file[1]
	{
		offset: tile-offset
		size: level-data-start - tile-offset
		name: "Tiles"
		interpret-as "lionking-tiles"
		folder: ""
	}

	unsigned32[8 * 3] offsets

	loop(8): i
	{
		if (offsets[(i * 3) + 2] != 0)
		{
			file[1]
			{
				offset: offsets[(i * 3) + 2]

				size: offsets[((i + 1) * 3) + 2] - offset

				if (offsets[((i + 1) * 3) + 2] == 0)
				{
					size = file.size - offset
				}
				level-width: offsets[(i * 3) + 0]
				level-height: offsets[(i * 3) + 1]
				name: "Layer " + i
				compressed "lionking-tilefix"
				interpret-as "lionking-level"
				folder: ""
			}
		}
	}
}

file-format lionking-level
{
	level
	{
		grid-x: 8
		grid-y: 8
		width: level-width
		height: level-height
		data-type: "unsigned16"

		level-layer
		{
			ignore-tile: 0xFFF
			mask: 0xFFF
			yflip-bit: 14
			xflip-bit: 15
			layer-image: "Tiles"
		}

		//And then objects, collision etc
	}
}

function lionking-tilefix
{
	loop (file.size / 2)
	{
		read unsigned16 tile
		if (tile == 0)
		{
			write unsigned16 0xFFFF
		}
		else
		{
			write unsigned16 (tile - 1)
		}
	}
}

file-format lionking-tiles
{
	image
	{
		width: 8
		height: 8
		format: "I8"
		transparent-index: 0
		palette: "Palette"
		tiles-across: 20
		data-size: file.size
	}
}

file-format lionking-palette
{
	palette
	{
		format: "R6G6B6"
		size: 256
	}
}

if (file.name like "*.amf")
{
	folder: "Music"
}

if (!(file.name like "*.*"))
{
	folder: "Sounds"

	sound
	{
		samples-per-second: 11000
		bits-per-sample: 8
		channels: 1
	}
}

function mch
{
	decompressed: [](width * height)
	
	loop
	{
		read unsigned8 x
		if (x != 0xFF)
		{
			read unsigned8 y
			read unsigned8 count

			start-offset: y * width + x

			loop (count): i
			{
				read unsigned8 dat
				decompressed[start-offset + i] = dat
			}
		}
	} while (x != 0xFF)

	write unsigned8 decompressed
}