display-name: "Games/Word Rescue"
meta: {"Working": ["Images", "Levels"]}

if (file.name like ["wr?.1", "wr?.2"])
{
	data(128) unknown
	file
	{
		unsigned8 unknown
		fixed-string(11) name
		unsigned16 unknown
		unsigned32 offset
		unsigned32 size
		unsigned32 unknown
		
		if (name like ["chars*.wr", "benny1*.wr"])
		{
			transparent-index: 13
		}
		else if (name like "slime*.wr")
		{
			transparent-index: 9
		}
		else if (name like "benny2*.wr")
		{
			transparent-index: [0, 0, 267, 78, 13,
					    0, 80, 302, 172, 9,
					    288, 0, 319, 40, 9]
		}
		else if (name like "static*.wr")
		{
			transparent-index: [208, 40, 279, 142, 13,
					    15, 120, 207, 167, 11]
		}

		interpret-as "image-PCX"
	} while (file.position < first-offset)
}

if (file.name like ["wr?.4", "wr?.5", "wr?.6"])
{
	folder: "Music"
}

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

	unsigned16 width
	unsigned16 height
	unsigned16 level-bg-ega
	unsigned16 bg-tileset
	unsigned16 backdrop

	unsigned16 start-x
	unsigned16 start-y

	unsigned16 end-x
	unsigned16 end-y

	unsigned16 gruzzle-count
	unsigned16[gruzzle-count * 2] gruzzle-coords

	unsigned16 drip-count
	unsigned16[drip-count * 3] drips

	unsigned16 slime-count
	unsigned16[slime-count * 2] slimes

	unsigned16 book-count
	unsigned16[book-count * 2] books

	unsigned16[7 * 2] letters

	unsigned16 anim-count
	unsigned16[anim-count * 2] anims

	unsigned16 fgtile-count
	unsigned16[fgtile-count * 2] fgtiles

	level
	{
		grid-x: 16
		grid-y: 16

		compressed[file.remaining-bytes] "WordRescueRLE"
		{
			level-layer
			{
				data-type: "unsigned8"
				ignore-tile: 0xFF
				layer-image: "wr1.1\\back" + bg-tileset + "   .wr"
			}
		}

		//TODO: attribute layer
	}
}

if (file.name like "wr?.7")
{
	interpret-as "image-PCX"
}

if (file.name like "wr?.3")
{
	text
}

if (file.name like ["wr?.4", "wr?.5", "wr?.6"])
{
	folder: "Music"
}

if (file.name like "wr?.d*")
{
	folder: "Demos"
}

function WordRescueRLE
{
	written: 0
	loop while (written < width * height)
	{
		read unsigned8 count
		read unsigned8 value
		
		loop (count)
		{
			write unsigned8 value			
		}

		written = written + count
	}
}