display-name: "Z-Experimental/SCI0"

sci-resource-names: ["view", "pic", "script", "text", "sound", "memory", "vocab", "font", "cursor", "patch", "bitmap", "palette", "audio"]

if (file.name like "resource.00?")
{
	interpret-as "sci0-resource-file"
}

file-format sci0-resource-file
{
	file while(file.remaining-bytes > 0)
	{
		unsigned16 id
		unsigned16 size
		size = size - 4
		unsigned16 decompressed-size
		unsigned16 method

		offset: file.position
		type: id >> 11

		name: sci-resource-names[type] + "." + (id & 0x7FF)
		if (method == 2)
		{
			compressed "sci-huffman"
		}

		if (method == 1)
		{
			lz-reset: 256
			lz-eof: 257
			lz-start: 258	
			lz-precode: 0		
			lz-maxbits: 12
			lz-readbits: 24
			lz-resetfull: true
			lz-readmethod: "agi"
			compressed "LZW"
		}

		if (type == 0)
		{
			folder: "Sprites"
			interpret-as "sci-view"
		}
		else if (type == 1)
		{
			folder: "Backgrounds"
			interpret-as "sci-pic"
		}
		else if (type == 3)
		{
			compressed "sci-text-nulls"
			interpret-as "sci-text"
		}
		else if (type == 7)
		{
			folder: "Fonts"
			interpret-as "sci-font"
		}
		else if (type == 8)
		{
			folder: "Cursors"
			interpret-as "sci-cursor"
		}

		data(size) file-data
	}
}

file-format sci-text
{
	text
}

file-format sci-font
{
	file.oob-allow = true
	file.oob-char = 0

	unsigned16 always-zero
	unsigned16 character-count

	unsigned16 pixel-line-height

	loop (character-count)
	{
		unsigned16 offset
		at offset
		{
			unsigned8 height		
			unsigned8 output-width

			width: output-width
			if ((width % 8) != 0)
			{
				width = width + (8 - (width % 8))
			}

			image
			{
				format: "I1"
			}
		}
	}
}


file-format sci-pic
{
	compressed[file.size] "image-SCI"
	{
		image
		{
			width: 320
			height: 200 * 3
			format: "I8"
			palette: "default-ega"
		}
	}
}

file-format sci-view
{
	unsigned16 group-count
	unsigned16 mirrored
	data(4) unknown

	unsigned16[group-count] group-offsets

	loop (group-count): i
	{
		at group-offsets[i]:

		unsigned16 cell-count
		unsigned16 unknown

		unsigned16[cell-count] cell-offsets

		loop (cell-count): j
		{
			at cell-offsets[j]:
			unsigned16 width
			unsigned16 height

			signed8 offset-x
			signed8 offset-y

			unsigned8 color-key
			unsigned8 unknown

			compressed[file.remaining-bytes] "sci-viewcomp"
			{
				image
				{
					format: "I8"
					palette: "default-ega"
					transparent-index: color-key
				}
			}
		}		
	}
}

function sci-text-nulls
{
	loop (file.size)
	{
		unsigned8 char
		if (char == 0)
		{
			write unsigned8 0xD
			write unsigned8 0xA
		}
		else if (char == 0xA)
		{
			write unsigned8 0xD
			write unsigned8 0xA
		}
		else
		{
			write unsigned8 char
		}
	}
}

function sci-viewcomp
{
	bytes-written: 0

	loop while ((bytes-written < width * height) and (file.remaining-bytes > 0))
	{
		read unsigned8 value
		fill(amount: value >> 4, value: value & 0xF)
		bytes-written = bytes-written + (value >> 4)
	}

	loop ((width * height) - bytes-written)
	{
		write unsigned8 0
	}
}

file-format sci-cursor
{
	unsigned16 hotspot-x
	unsigned16 hotspot-y

	compressed [file.remaining-bytes] "sci0-cursor"
	{
		image
		{
			palette: [0,0,0, 0xFF,0xFF,0xFF, 0,0,0, 0,0,0]
			transparent-index: 2
			format: "I2"
			width: 16
			height: 16
		}
	}
}

function sci0-cursor
{
	loop (16): i
	{
		at i * 2:
		read unsigned16 alpha

		at i * 2 + 32:
		read unsigned16 color

		loop (4)
		{
			result: 0
			loop (4)
			{
				result = result << 1
				result = result | (alpha >> 15)

				result = result << 1
				result = result | (color >> 15)

				alpha = (alpha << 1) & 0xFFFF
				color = (color << 1) & 0xFFFF
			}

			write unsigned8 result
		}
	}
}

function sci-huffman
{
	file.bit-reading-shift-out-right = false
	//file.bit-reading-shift-in-right = false

	//NOT FINISHED
	read unsigned8 num-nodes
	read unsigned8 terminator
	terminator = terminator | 0x100
	read unsigned8[num-nodes * 2] nodes

	node: 0
	finished: false
	c: 0
	next: 0

	loop
	{
		node = 0
		finished = false
		c = 0
		next = 0

		loop while ((nodes[node + 1] != 0) and (finished == false))
		{
			read bits(1) b
			if (b)
			{
				next = (nodes[node + 1] & 0xF)
				if (next == 0)
				{
					finished = true
					read bits(8) literal
					c = literal | 0x100
				}
				else
				{
					node = node + (next << 1)
				}
			}
			else
			{
				node = node + ((nodes[node + 1] >> 4) << 1)
			}
		}

		if (finished == false)
		{
			c = nodes[node] | (nodes[node + 1] << 8)
		}

		if ((c != terminator) and (c >= 0))
		{
			write unsigned8 c
		}
	} while ((c != terminator) and (c >= 0) and (file.remaining-bytes > 0))
}