display-name: "Games/Captain Comic I"
meta: {"Working":["Images", "Tiles", "Levels (tiles only)"], "Not yet working":"Audio", "Thanks to":"Mouser"}

sprites-start: {
	0x17B00: {"start": 36528, "missing": 0}, // REV 1
	0x13E90: {"start": 37904, "missing": 1}, // REV 2
	0x13EC0: {"start": 37952, "missing": 1}, //REV 3
	0x14230: {"start": 38416, "missing": 1}, //REV 4a
	0x187B0: {"start": 40256, "missing": 1} //REV 5
}

level-start: {
	0x17B00: 0xEFB0,
	0x13E90: 0xF520,
	0x13EC0: 0xF550,
	0x14230: 0xF7F0,
	0x187B0: 0xFF00
}

sounds: {
	0x17B00: [{"name": "title", "offset": 0x2351, "size": 0xF8},
		  {"name": "start", "offset": 0x2495, "size": 0x98},
		  {"name": "unused", "offset": 0x24d5, "size": 0x58},
		  {"name": "door", "offset": 0xebd9, "size": 0x70},
		  {"name": "player death", "offset": 0xec01, "size": 0x48},
		  {"name": "teleport", "offset": 0xec29, "size": 0x2B},
		  {"name": "bonus", "offset": 0xec49, "size": 0xC},
		  {"name": "materialise", "offset": 0xec55, "size": 0xED},
		  {"name": "game over", "offset": 0xecb5, "size": 0x48},
		  {"name": "screen transition", "offset": 0xed0e, "size": 0x34},
		  {"name": "lost life", "offset": 0xed2e, "size": 0x14},
		  {"name": "shoot", "offset": 0xed7e, "size": 0xC},
		  {"name": "enemy death", "offset": 0xed8a, "size": 0xC},
		  {"name": "player damaged", "offset": 0xeda1, "size": 0x10},
		  {"name": "item get", "offset": 0xedb1, "size": 0x2C},
		  {"name": "extra life", "offset": 0xedc5, "size": 0x18}],
	0x13E90: [{"name": "title", "offset": 0x23d2, "size": 0xF8},
		  {"name": "start", "offset": 0x29f0, "size": 0x98},
		  {"name": "unused", "offset": 0x2a30, "size": 0x58},
		  {"name": "door", "offset": 0xf139, "size": 0x70},
		  {"name": "player death", "offset": 0xf161, "size": 0x48},
		  {"name": "teleport", "offset": 0xf189, "size": 0x2B},
		  {"name": "bonus", "offset": 0xf1a9, "size": 0xC},
		  {"name": "materialise", "offset": 0xf1b5, "size": 0xED},
		  {"name": "game over", "offset": 0xf215, "size": 0x48},
		  {"name": "screen transition", "offset": 0xf26e, "size": 0x34},
		  {"name": "lost life", "offset": 0xf28e, "size": 0x14},
		  {"name": "shoot", "offset": 0xf2de, "size": 0xC},
		  {"name": "enemy death", "offset": 0xf2ea, "size": 0xC},
		  {"name": "player damaged", "offset": 0xf301, "size": 0x10},
		  {"name": "item get", "offset": 0xf311, "size": 0x2C},
		  {"name": "extra life", "offset": 0xf325, "size": 0x18}],
	0x13EC0: [{"name": "title", "offset": 0x2402, "size": 0xF8},
		  {"name": "start", "offset": 0x2a22, "size": 0x98},
		  {"name": "unused", "offset": 0x2a62, "size": 0x58},
		  {"name": "door", "offset": 0xf169, "size": 0x70},
		  {"name": "player death", "offset": 0xf191, "size": 0x48},
		  {"name": "teleport", "offset": 0xf1b9, "size": 0x2B},
		  {"name": "bonus", "offset": 0xf1d9, "size": 0xC},
		  {"name": "materialise", "offset": 0xf1e5, "size": 0xED},
		  {"name": "game over", "offset": 0xf245, "size": 0x48},
		  {"name": "screen transition", "offset": 0xf29f, "size": 0x34},
		  {"name": "lost life", "offset": 0xf2bf, "size": 0x14},
		  {"name": "shoot", "offset": 0xf30e, "size": 0xC},
		  {"name": "enemy death", "offset": 0xf31a, "size": 0xC},
		  {"name": "player damaged", "offset": 0xf331, "size": 0x10},
		  {"name": "item get", "offset": 0xf341, "size": 0x2C},
		  {"name": "extra life", "offset": 0xf355, "size": 0x18}],
	0x14230: [{"name": "title", "offset": 0x25e2, "size": 0xF8},
		  {"name": "start", "offset": 0x2bea, "size": 0x98},
		  {"name": "unused", "offset": 0x2c2e, "size": 0x58},
		  {"name": "door", "offset": 0xf339, "size": 0x70},
		  {"name": "player death", "offset": 0xf361, "size": 0x48},
		  {"name": "teleport", "offset": 0xf389, "size": 0x2B},
		  {"name": "bonus", "offset": 0xf3a9, "size": 0xC},
		  {"name": "materialise", "offset": 0xf3b5, "size": 0xED},
		  {"name": "game over", "offset": 0xf415, "size": 0x48},
		  {"name": "screen transition", "offset": 0xf46f, "size": 0x34},
		  {"name": "lost life", "offset": 0xf48f, "size": 0x14},
		  {"name": "shoot", "offset": 0xf4de, "size": 0xC},
		  {"name": "enemy death", "offset": 0xf4ea, "size": 0xC},
		  {"name": "player damaged", "offset": 0xf501, "size": 0x10},
		  {"name": "item get", "offset": 0xf511, "size": 0x2C},
		  {"name": "extra life", "offset": 0xf525, "size": 0x18}],
	0x187B0: [{"name": "title", "offset": 0x2804, "size": 0x110},
		  {"name": "door", "offset": 0xfa69, "size": 0x70},
		  {"name": "player death", "offset": 0xfa91, "size": 0x48},
		  {"name": "teleport", "offset": 0xfab9, "size": 0x2B},
		  {"name": "bonus", "offset": 0xfad9, "size": 0xC},
		  {"name": "materialise", "offset": 0xfae5, "size": 0xED},
		  {"name": "game over", "offset": 0xfb45, "size": 0x28},
		  {"name": "screen transition", "offset": 0xfb7f, "size": 0x20},
		  {"name": "lost life", "offset": 0xfb9f, "size": 0x14},
		  {"name": "shoot", "offset": 0xfbee, "size": 0xC},
		  {"name": "enemy death", "offset": 0xfbfa, "size": 0xC},
		  {"name": "player damaged", "offset": 0xfc11, "size": 0x10},
		  {"name": "item get", "offset": 0xfc21, "size": 0x2C},
		  {"name": "extra life", "offset": 0xfc35, "size": 0x18}]
}

 //level info 23812 - 28496
sprite-sizes: [ {"width": 16, "height": 32, "tile-count": 10, "extra": 0, "alpha": "A1"}, {"width": 16, "height": 8, "tile-count": 2, "extra": 0, "alpha": "A1"}, {"width": 16, "height": 16, "tile-count": 6, "extra": 0, "alpha": "A1"}, {"width": 16, "height": 32, "tile-count": 23, "extra": 36, "alpha": "A1"}, {"width": 16, "height": 16, "tile-count": 32, "extra": 0, "alpha": "A1"},
{"width": 8, "height": 16, "tile-count": 13, "extra": 0, "alpha": ""}, {"width": 128, "height": 48, "tile-count": 2, "extra": 4684, "alpha": ""},
{"width": 320, "height": 200, "tile-count": 1, "extra": 0, "alpha": ""}]

if (file.name == "comic.exe")
{
	file[1]
	{
		offset: sprites-start[file.size].start
		size: file.size - sprites-start[file.size].start
		name: "sprites"
		missing: sprites-start[file.size].missing
		interpret-as "cc1-sprites"
	}

	file[sounds[file.size].length]:i {
		offset: sounds[file.size][i].offset
		size: sounds[file.size][i].size
		name: sounds[file.size][i].name

		interpret-as "cc1-sound"
	}

	file[8]: i {
		offset: level-start[file.size] + (i * 208)
		size: 208
		name: "level info " + (i + 1)
	}
}

file-format cc1-sound
{
	compressed[file.size - 4] "cc1-sound-freq"
	{
		sound
		{
			channels: 1
            		samples-per-second: 44100
            		bits-per-sample: 16
            		volume: 255
		}
	}
}

function cc1-sound-freq
{
	loop
	{
		unsigned16 frequency-sample
		unsigned16 frequency-time

		if (frequency-sample == 0x28) {
			write frequency ((44100 / 18) * frequency-time) 0 1193180 0
		}
		else if (frequency-sample != 0) {
			write frequency ((44100 / 18) * frequency-time) frequency-sample 1193180 255
		}
	} while (frequency-sample != 0)
}

file-format cc1-sprites
{
	loop (sprite-sizes.length - missing): i
	{
		image
		{
			format: "I4" + sprite-sizes[i].alpha
			width: sprite-sizes[i].width
			height: sprite-sizes[i].height
			tile-count: sprite-sizes[i].tile-count
			bit-stride: width * height
			tiles-across: 10
			alpha-mask: 1
		}

		data(sprite-sizes[i].extra) unknown
	}
}

if (file.name like "*.ega")
{
	file-format captaincomic1-fullscreen
	{
		folder: "Fullscreen images"

		if (file.size == 32000)
		{
			image
			{
				format: "I4"
				width: 320
				height: 200
				bit-stride: width * height
			}
		}
		else
		{
			unsigned16 uncompressed-plane-size
			compressed[file.remaining-bytes] "CC-RLE"
			{
				image
				{
					format: "I4"
					width: 320
					height: 200
					bit-stride: width * height
				}
			}
		}
	}
}

if (file.name like "*.tt2")
{
	folder: "Tiles"

	unsigned32 unknown
	image
	{
		format: "I4"
		width: 16
		height: 16
		data-size: file.size
		tiles-across: 8
		bit-stride: width * height
	}
}

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

	image-layout-width: 320
	image-layout
	{		
		loop while (file.remaining-bytes > 0)
		{
			image
			{
				format: "I4A1"
				alpha-mask: 1
				width: 16
				height: 16			
				bit-stride: width * height
			}
		}
	}
}

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

	unsigned16 width
	unsigned16 height

	//Get tileset
	tileset: ""
	loop (file.name.length - 4): i
	{
		tileset = tileset + file.name[i]
	}

	tileset = tileset + ".tt2"

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

		level-layer
		{
			data-type: "unsigned8"
			layer-image: tileset 
		}
	}
}

function CC-RLE
{
	loop while (file.remaining-bytes > 0)
	{
		read unsigned8 token
		if ((token & 0x80) == 0)
		{
			loop (token)
			{
				read unsigned8 byte-to-copy
				write unsigned8 byte-to-copy
			}
		}
		else
		{
			read unsigned8 byte-to-repeat
			loop (token & 0x7F)
			{
				write unsigned8 byte-to-repeat
			}
		}
	}
}