display-name: "Games/Jill of the Jungle"
meta: {"Working": ["Tiles", "Sprites", "Sounds", "Level backgrounds"],"Thanks to": ["Malvineous", "Ilovemyq3map2", "SaxxonPike"]}

//TODO: clean this bad boy up
//TODO: add CGA/EGA/VGA folders to original .sha, use passed param to switch out which index we use
//Final pieces of the puzzle came from Shikadi's modding Wiki

file-format jill-image 
{
	interpret-as-internally "jill-internal-split"

	unsigned8 image-count
	unsigned16 num-rots
	unsigned16 len-CGA
	unsigned16 len-EGA
	unsigned16 len-VGA	
	unsigned8 num-color-bits
	unsigned16 flags

	//build remap table
	remap-table: [](256)
	
	counter: 0
	loop(256)
	{
		remap-table[counter] = counter
		counter = counter + 1		
	}

	if (num-color-bits == 4)
	{
		if (flags != 1)
		{	
			counter: 0		
			loop(16)
			{
				unsigned8 CGA
				unsigned8 EGA
				unsigned8 VGA
				unsigned8 zero

				remap-table[counter] = VGA
				counter = counter + 1
			}
		}
	}
	else if ((num-color-bits != 2) and (num-color-bits != 8))
	{		
		counter: 0
		loop(8)
		{
			unsigned8 CGA
			unsigned8 EGA
			unsigned8 VGA
			unsigned8 zero
			remap-table[counter] = VGA	
			counter = counter + 1		
		}
	}

	//Draw the images
	image-layout-width: 320
	image-layout
	{
		loop (image-count)
		{
			unsigned8 width
			unsigned8 height
			unsigned8 compression		

			compressed [width * height] "JillColorMapping"
			{			
				image
				{
					format: "I8"
					transparent-index: 0
				}
			}
		}
	}	
}

file-format jill-internal-split
{
	unsigned8 image-count
	unsigned16 num-rots
	unsigned16 len-CGA
	unsigned16 len-EGA
	unsigned16 len-VGA	
	unsigned8 num-color-bits
	unsigned16 flags

	//build remap table
	remap-table: [](256)
	
	counter: 0
	loop(256)
	{
		remap-table[counter] = counter
		counter = counter + 1		
	}

	if (num-color-bits == 4)
	{
		if (flags != 1)
		{	
			counter: 0		
			loop(16)
			{
				unsigned8 CGA
				unsigned8 EGA
				unsigned8 VGA
				unsigned8 zero

				remap-table[counter] = VGA
				counter = counter + 1
			}
		}
	}
	else if ((num-color-bits != 2) and (num-color-bits != 8))
	{		
		counter: 0
		loop(8)
		{
			unsigned8 CGA
			unsigned8 EGA
			unsigned8 VGA
			unsigned8 zero
			remap-table[counter] = VGA	
			counter = counter + 1		
		}
	}

	//Draw the images
	file [image-count]: i
	{
		name: "" + i
		offset: file.position

		unsigned8 width
		unsigned8 height
		unsigned8 compression
		data(width * height) unknown
		palette: palette
		interpret-as "jill-internal-image"
	}	
}

file-format jill-internal-image
{
	unsigned8 width
	unsigned8 height
	unsigned8 compression		

	compressed [width * height] "JillColorMapping"
	{			
		image
		{
			format: "I8"
			transparent-index: 0
		}
	}
}

if (file.name like "*.vcl")
{
	interpret-as "jill-vcl"
}

file-format jill-vcl
{
	unsigned32[50] sound-offsets
	unsigned16[50] sound-lengths
	unsigned16[50] sound-frequencies
	unsigned32[40] text-offsets
	unsigned16[40] text-lengths

	file[50]: i {
		skip-if sound-lengths[i] == 0

		name: "Sound " + i
		offset: sound-offsets[i]
		size: sound-lengths[i]
		frequency: sound-frequencies[i]

		interpret-as "jill-sound"
	}

	file[40]: i {
		skip-if text-lengths[i] == 0

		name: "Text " + i
		offset: text-offsets[i]
		size: text-lengths[i]

		interpret-as "jill-text"
	}
}

file-format jill-text
{
	text
}

file-format jill-sound
{
	sound
	{
		samples-per-second: frequency
		bits-per-sample: 8
		channels: 1
	}
}

palette-jill: [
    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,
    0x1C, 0x1C, 0x1C, 0x38, 0x38, 0x38, 0x6C, 0x6C, 0x6C, 0x80, 0x80, 0x80, 0x94, 0x94, 0x94, 0xBC,
    0xBC, 0xBC, 0xD0, 0xD0, 0xD0, 0xE4, 0xE4, 0xE4, 0x64, 0x28, 0x00, 0x94, 0x28, 0x00, 0xC8, 0x28,
    0x00, 0xFC, 0x28, 0x00, 0x00, 0x54, 0x00, 0x30, 0x54, 0x00, 0x64, 0x54, 0x00, 0x94, 0x54, 0x00,
    0xC8, 0x54, 0x00, 0xFC, 0x54, 0x00, 0x00, 0x7C, 0x00, 0x30, 0x7C, 0x00, 0x64, 0x7C, 0x00, 0x94,
    0x7C, 0x00, 0xC8, 0x7C, 0x00, 0xFC, 0x7C, 0x00, 0x00, 0xA8, 0x00, 0x30, 0xA8, 0x00, 0x64, 0xA8,
    0x00, 0x94, 0xA8, 0x00, 0xC8, 0xA8, 0x00, 0xFC, 0xA8, 0x00, 0x30, 0xD0, 0x00, 0x64, 0xD0, 0x00,
    0x94, 0xD0, 0x00, 0xC8, 0xD0, 0x00, 0xFC, 0xD0, 0x00, 0x30, 0xFC, 0x00, 0x64, 0xFC, 0x00, 0x94,
    0xFC, 0x00, 0xC8, 0xFC, 0x00, 0xFC, 0xFC, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x64, 0x00,
    0x30, 0x94, 0x00, 0x30, 0xC8, 0x00, 0x30, 0xFC, 0x00, 0x30, 0x00, 0x28, 0x30, 0x30, 0x28, 0x30,
    0x64, 0x28, 0x30, 0x94, 0x28, 0x30, 0xC8, 0x28, 0x30, 0xFC, 0x28, 0x30, 0x00, 0x54, 0x30, 0x30,
    0x54, 0x30, 0x64, 0x54, 0x30, 0x94, 0x54, 0x30, 0xC8, 0x54, 0x30, 0xFC, 0x54, 0x30, 0x00, 0x7C,
    0x30, 0x30, 0x7C, 0x30, 0x64, 0x7C, 0x30, 0x94, 0x7C, 0x30, 0xC8, 0x7C, 0x30, 0xFC, 0x7C, 0x30,
    0x00, 0xA8, 0x30, 0x30, 0xA8, 0x30, 0x64, 0xA8, 0x30, 0x94, 0xA8, 0x30, 0xC8, 0xA8, 0x30, 0xFC,
    0xA8, 0x30, 0x30, 0xD0, 0x30, 0x64, 0xD0, 0x30, 0x94, 0xD0, 0x30, 0xC8, 0xD0, 0x30, 0xFC, 0xD0,
    0x30, 0x30, 0xFC, 0x30, 0x64, 0xFC, 0x30, 0x94, 0xFC, 0x30, 0xC8, 0xFC, 0x30, 0xFC, 0xFC, 0x30,
    0x00, 0x00, 0x64, 0x30, 0x00, 0x64, 0x64, 0x00, 0x64, 0x94, 0x00, 0x64, 0xC8, 0x00, 0x64, 0xFC,
    0x00, 0x64, 0x00, 0x28, 0x64, 0x30, 0x28, 0x64, 0x64, 0x28, 0x64, 0x94, 0x28, 0x64, 0xC8, 0x28,
    0x64, 0xFC, 0x28, 0x64, 0x00, 0x54, 0x64, 0x30, 0x54, 0x64, 0x64, 0x54, 0x64, 0x94, 0x54, 0x64,
    0xC8, 0x54, 0x64, 0xFC, 0x54, 0x64, 0x00, 0x7C, 0x64, 0x30, 0x7C, 0x64, 0x64, 0x7C, 0x64, 0x94,
    0x7C, 0x64, 0xC8, 0x7C, 0x64, 0xFC, 0x7C, 0x64, 0x00, 0xA8, 0x64, 0x30, 0xA8, 0x64, 0x64, 0xA8,
    0x64, 0x94, 0xA8, 0x64, 0xC8, 0xA8, 0x64, 0xFC, 0xA8, 0x64, 0x30, 0xD0, 0x64, 0x64, 0xD0, 0x64,
    0x94, 0xD0, 0x64, 0xC8, 0xD0, 0x64, 0xFC, 0xD0, 0x64, 0x30, 0xFC, 0x64, 0x64, 0xFC, 0x64, 0x94,
    0xFC, 0x64, 0xC8, 0xFC, 0x64, 0xFC, 0xFC, 0x64, 0x00, 0x00, 0x94, 0x30, 0x00, 0x94, 0x64, 0x00,
    0x94, 0x94, 0x00, 0x94, 0xC8, 0x00, 0x94, 0xFC, 0x00, 0x94, 0x00, 0x28, 0x94, 0x30, 0x28, 0x94,
    0x64, 0x28, 0x94, 0x94, 0x28, 0x94, 0xC8, 0x28, 0x94, 0xFC, 0x28, 0x94, 0x00, 0x54, 0x94, 0x30,
    0x54, 0x94, 0x64, 0x54, 0x94, 0x94, 0x54, 0x94, 0xC8, 0x54, 0x94, 0xFC, 0x54, 0x94, 0x00, 0x7C,
    0x94, 0x30, 0x7C, 0x94, 0x64, 0x7C, 0x94, 0x94, 0x7C, 0x94, 0xC8, 0x7C, 0x94, 0xFC, 0x7C, 0x94,
    0x00, 0xA8, 0x94, 0x30, 0xA8, 0x94, 0x64, 0xA8, 0x94, 0x94, 0xA8, 0x94, 0xC8, 0xA8, 0x94, 0xFC,
    0xA8, 0x94, 0x30, 0xD0, 0x94, 0x64, 0xD0, 0x94, 0x94, 0xD0, 0x94, 0xC8, 0xD0, 0x94, 0xFC, 0xD0,
    0x94, 0x30, 0xFC, 0x94, 0x64, 0xFC, 0x94, 0x94, 0xFC, 0x94, 0xC8, 0xFC, 0x94, 0xFC, 0xFC, 0x94,
    0x00, 0x00, 0xC8, 0x30, 0x00, 0xC8, 0x64, 0x00, 0xC8, 0x94, 0x00, 0xC8, 0xC8, 0x00, 0xC8, 0xFC,
    0x00, 0xC8, 0x00, 0x28, 0xC8, 0x30, 0x28, 0xC8, 0x64, 0x28, 0xC8, 0x94, 0x28, 0xC8, 0xC8, 0x28,
    0xC8, 0xFC, 0x28, 0xC8, 0x00, 0x54, 0xC8, 0x30, 0x54, 0xC8, 0x64, 0x54, 0xC8, 0x94, 0x54, 0xC8,
    0xC8, 0x54, 0xC8, 0xFC, 0x54, 0xC8, 0x00, 0x7C, 0xC8, 0x30, 0x7C, 0xC8, 0x64, 0x7C, 0xC8, 0x94,
    0x7C, 0xC8, 0xC8, 0x7C, 0xC8, 0xFC, 0x7C, 0xC8, 0x00, 0xA8, 0xC8, 0x30, 0xA8, 0xC8, 0x64, 0xA8,
    0xC8, 0x94, 0xA8, 0xC8, 0xC8, 0xA8, 0xC8, 0xFC, 0xA8, 0xC8, 0x30, 0xD0, 0xC8, 0x64, 0xD0, 0xC8,
    0x94, 0xD0, 0xC8, 0xC8, 0xD0, 0xC8, 0xFC, 0xD0, 0xC8, 0x30, 0xFC, 0xC8, 0x64, 0xFC, 0xC8, 0x94,
    0xFC, 0xC8, 0xC8, 0xFC, 0xC8, 0xFC, 0xFC, 0xC8, 0x00, 0x00, 0xFC, 0x30, 0x00, 0xFC, 0x64, 0x00,
    0xFC, 0x94, 0x00, 0xFC, 0xC8, 0x00, 0xFC, 0xFC, 0x00, 0xFC, 0x00, 0x28, 0xFC, 0x30, 0x28, 0xFC,
    0x64, 0x28, 0xFC, 0x94, 0x28, 0xFC, 0xC8, 0x28, 0xFC, 0xFC, 0x28, 0xFC, 0x00, 0x54, 0xFC, 0x30,
    0x54, 0xFC, 0x64, 0x54, 0xFC, 0x94, 0x54, 0xFC, 0xC8, 0x54, 0xFC, 0xFC, 0x54, 0xFC, 0x00, 0x7C,
    0xFC, 0x30, 0x7C, 0xFC, 0x64, 0x7C, 0xFC, 0x94, 0x7C, 0xFC, 0xC8, 0x7C, 0xFC, 0xFC, 0x7C, 0xFC,
    0x00, 0xA8, 0xFC, 0x30, 0xA8, 0xFC, 0x64, 0xA8, 0xFC, 0x94, 0xA8, 0xFC, 0xC8, 0xA8, 0xFC, 0xFC,
    0xA8, 0xFC, 0x30, 0xD0, 0xFC, 0x64, 0xD0, 0xFC, 0x94, 0xD0, 0xFC, 0xC8, 0xD0, 0xFC, 0xFC, 0xD0,
    0xFC, 0x30, 0xFC, 0xFC, 0x64, 0xFC, 0xFC, 0x94, 0xFC, 0xFC, 0xC8, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC
]

if (file.name like "*.sha")
{
	file-number: 0
	file[64]: i
	{
		unsigned32 offset
		name: "Jill Image " + i
		palette: palette-jill		
		interpret-as "jill-image"

		at (file.position + (128 * 4))
		{
			unsigned16 size
		}
	
		skip-if ((offset == 0) and (size == 0))
	}
}



function JillColorMapping
{
	loop while (file.remaining-bytes > 0)
	{
		read unsigned8 oldValue
		if (oldValue < 16)
		{
			write unsigned8 remap-table[oldValue]
		}
		else
		{
			write unsigned8 oldValue
		}
	}
}

file-format jill-tile-mapping
{
	loop while (file.remaining-bytes > 0)
	{	
		property-list
		{			
			unsigned16 mapCode
			unsigned8 tile
			unsigned8 tileset
			tl: tileset & 0x3F
			unsigned16 flags
			pascal-string(unsigned8) tilename
		}
	}
}

if (file.name == "jill.dma")
{
	interpret-as "jill-tile-mapping"
}

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

graphics-map: {
	["*.jn1", "*.dem"]: "jill1.sha",
	["*.jn2"]: "jill2.sha",
	["*.jn3"]: "jill3.sha"
}

if (file.name like ["*.jn?", "*.dem"])
{
	//Read tile mapping table
	tile-mapping: [](1000)
	from "jill.dma"
	{
		loop while (file.remaining-bytes > 0): i
		{			
			unsigned16 mapCode
			unsigned8 tile
			unsigned8 tileset
			unsigned16 flags
			pascal-string(unsigned8) tilename

			tile-mapping[mapCode] = {
				"tile": tile,
				"tileset": (tileset & 0x3F)
			}
		}
	}

	//Level
	level-bg-ega: 0
	level
	{
		loop (128 * 64): i
		{
			unsigned16 tile-id
			tile-id = tile-id & 0xFFF

			image-name: graphics-map[file.name] + "\\jill image " + tile-mapping[tile-id].tileset + "\\" + tile-mapping[tile-id].tile

			level-object
			{
				sprite: image-name
				offset-x: (i / 64) * 16
				offset-y: (i % 64) * 16
			}
		}
	}
}

if (file.name like "*.doc")
{
	text
}