display-name: "Games/Mystic Towers"
meta: {"Working": ["Packed file extraction", "All images"]}
  
if (file.name like "*myst*.dat")
{
	at file.size - 2:
	unsigned16 file-count

	at file.size - 2 - (file-count * 21):

	file [file-count]
	{
		dirty-string(12) name
		unsigned32 offset
		unsigned32 size
	}
}

if (file.name like "*.pcx")
{
	interpret-as "image-PCX"
}

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

if (file.name like "*.sp")
{
	folder: "Sprites"

	unsigned16 sprite-count

	file[sprite-count]: i
	{
		unsigned32 offset
		name: "Frame " + i
		interpret-as "mystictowers-sprite"
	}
}

/*if (file.name like "*.sbk")
{
	unsigned16 file-count
	
	file[file-count]: i
	{
		unsigned32 offset
		at offset
		{
			fixed-string(0x8) name
		}
	}
}*/

if (file.name == "mystsw.bin")
{
	b800-text
}

if (file.name like "*.pcs")
{
	unsigned16 file-count
	
	file[file-count]: i
	{
		unsigned32 offset
		name: "File " + i
	}
}

file-format mystictowers-sprite
{
	unsigned16 width
	unsigned16 height
	unsigned16 unknown
	unsigned16 offset-x
	unsigned16 offset-y

	compressed [file.remaining-bytes] "mystictowers-sprite-compression"
	{
		image
		{
			format: "I8"
			transparent-index: 0
			palette: "main2.pcx"
			//padding-left: -(offset-x & 0x3FFF)
			//padding-top: -(offset-y)
		}
	}
}

function mystictowers-sprite-compression
{
	loop while(file.remaining-bytes > 0)
	{
		unsigned8 pre-alpha
		loop (pre-alpha)
		{
			write unsigned8 0
		}

		unsigned8 actual-data
		loop (actual-data)
		{
			read unsigned8 pixel
			write unsigned8 pixel
		}

		loop (width - (actual-data + pre-alpha))
		{
			write unsigned8 0
		}
	}
}