display-name: "Games/Cosmo's Cosmic Adventure"
meta: {"Working": ["Images", "Sounds", "Levels"], "Thanks to": "Frenkel Smeijers"}

if (file.name like ["*.vol", "*.stn"])
{
	file
	{
		fixed-string(12) name
		unsigned32 offset
		unsigned32 size
	} while (size > 0)
}

width: 8
height: 8
bit-stride: 8

layout-options:
{
	"status.mni":   {"tiles-across": 38, "alpha-mask": 0, "folder": "Images"},
	"masktile.mni": {"tiles-across": 40, "alpha-mask": 1, "folder": "Tiles"},
	"fonts.mni":    {"tiles-across": 40, "alpha-mask": 0, "folder": "Images"},
	["*"]:         {"tiles-across": 40, "alpha-mask": 0, "folder": "Tiles"}
}

if (file.name like ["fonts.mni", "masktile.mni"])
{
	folder: layout-options[file.name].folder

	image
	{
		format: "A1I4"
		alpha-mask: layout-options[file.name].alpha-mask
		tiles-across: layout-options[file.name].tiles-across
		data-size: file.size
	}
}

if (file.name like ["bd*.mni", "status.mni", "tiles.mni"])
{
	folder: layout-options[file.name].folder
	
	image
	{
		format: "I4"
		tiles-across: layout-options[file.name].tiles-across
		data-size: file.size
	}
}

sprite-data: 
{
	"actors.mni": {"index-name": "actrinfo.mni", "header-size": 534},
	"players.mni": {"index-name": "plyrinfo.mni", "header-size":2},
	"cartoon.mni": {"index-name": "cartinfo.mni", "header-size": 2}
}

if (file.name like ["actrinfo.mni"])
{
	unsigned16 first-offset
	count: first-offset

	at 0:
	i : 0

	file[count]
	{
		unsigned16 pre-offset
		offset: pre-offset * 2
		name: "sprite " + i

		interpret-as "cosmo-formatted-sprite"

		i = i + 1
	}
}

file-format cosmo-formatted-sprite
{
	frame-count: file.size / 8

	if (drawing-level-sprite == true)
	{
		frame-count = 1
	}

	loop (frame-count)
	{
		unsigned16 tiles-down
		unsigned16 tiles-across

		//sprite image data is in blocks of 65536 bytes, why you subtract is beyond me
		unsigned32 pre_offset
		frame-offset: pre_offset - ( pre_offset / 65536 )			

		from "actors.mni" at frame-offset
		{
			image
			{
				format: "A1I4"		
				tile-count: tiles-across * tiles-down
				alpha-mask: 1
			}
		}
	}
}

if (file.name like ["players.mni", "cartoon.mni"])
{
	//Header at start = ?offsets to frames? in actrinfo, 0x0001 elsewhere
	//TODO: this will be something we need for actor drawing - split out into frames etc

	from sprite-data[file.name].index-name at sprite-data[file.name].header-size
	{
		file-number: 0
		
		file while (file.remaining-bytes > 0)
		{
			interpret-as "cosmo-sprite"

			unsigned16 tiles-down
			unsigned16 tiles-across

			//sprite image data is in blocks of 65536 bytes, why you subtract is beyond me
			unsigned32 pre_offset
			offset: pre_offset - ( pre_offset / 65536 )			

			size: ( tiles-across * tiles-down * 8 * 8 * 5 ) / 8  //image is width*8 x height*8 x 5 planes

			name: "sprite " + file-number
			file-number = file-number + 1
		}
	}
}

file-format cosmo-sprite
{
	folder: "Sprites"
	image
	{
		format: "A1I4"		
		tile-count: tiles-across * tiles-down
		alpha-mask: 1
	}
}

if (file.size == 32000)
{
	image
	{
		format: "I4"
		width: 320
		height: 200
		bit-stride: width * height
	
	}
}

if (file.size == 4000)
{
	b800-text
}

if (file.name like "m*.mni")
{
	folder: "Music"
	interpret-as "music-imf-560"
}

if (file.name like "sound*.mni")
{
	interpret-as "archive-SND_DOS"
}

level-tiles-mapping:
{
	["a*"]:	"cosmo1.stn",
	["bonus1.mni", "bonus2.mni"]: "cosmo1.stn",
	["bonus3.mni", "bonus4.mni"]: "cosmo2.stn",
	["bonus5.mni", "bonus6.mni"]: "cosmo3.stn",
	["b*"]:	"cosmo2.stn",
	["c*"]:	"cosmo3.stn",
	["*"]: "cosmo1.stn"
}

level-sprite-lookup:
{
	1: 79, //?
	2: 79,
	3: 79,
	4: 79,
	5: 79,
	6: 79, //?
	7: 79, //?
	8: 79, //?
	16: 79, //?
	22: 79, //?
	37: 5,
	53: 20,
	62: 0,
	64: 0,
	66: 0,
	68: 0,
	71: 41,
	73: 43,
	79: 49,
	81: 49,
	83: 0,
	84: 0,
	87: 29,
	89: 29,
	90: 60,
	92: 60,
	97: 68,
	98: 68,
	112: 0,
	115: 83,
	124: 0,
	127: 95,
	131: 29,
	135: 105,
	138: 108,
	140: 68,
	141: 68,
	142: 112,
	143: 111,
	146: 0,
	147: 0,
	148: 29,
	150: 0,
	151: 60,
	152: 60,
	173: 29,
	179: 0,
	187: 29,
	188: 29,
	189: 29,
	190: 32,
	191: 34,
	192: 36,
	196: 164,
	197: 164,
	198: 0,
	200: 0,
	202: 0,
	204: 29,
	206: 29,
	221: 192,
	222: 192,
	224: 29,
	226: 29,
	228: 29,
	230: 29,
	234: 108,
	235: 125,
	236: 125,
	237: 125,
	238: 125,
	239: 125,
	240: 125,
	241: 125,
	242: 125,
	243: 125,
	244: 194,
	245: 196,
	246: 198,
	247: 200,
	248: 2,
	249: 29,
	250: 220,
	251: 220,
	252: 221,
	254: 223,
	255: 0,
	256: 226,
	258: 0,
	259: 229,
	261: 0,
	262: 232,
	267: 237,
	269: 125,
	270: 125,
	271: 125,
	272: 125,
	273: 125,
	274: 125,
	282: 25,
	283: 155,
	284: 125,
	285: 125,
	286: 125,
	287: 125,
	288: 125,
	289: 125,
	290: 125,
	291: 125,
	292: 125,
	293: 125,
	294: 28,
	295: 1,
	["*"]: 0xFFFF
}

if (file.name like ["a?.mni", "a?0.mni", "a11.mni", "b?.mni", "b?0.mni", "b11.mni", "c?.mni", "c?0.mni", "c31.mni", "bonus?.mni"])
{
	folder: "Levels"

	unsigned8 backdropID     // ? id of backdrop to use ?
	unsigned8 unknown
	unsigned16 level-width
	level-height: 32750 / level-width
	unsigned16 actorSize

	level
	{
		actor-start: file.position
		
		at file.position + actorSize * 2:

		grid-x: 8
		grid-y: 8
		width: level-width
		height: level-height

		tile-layout-start: file.position

		encrypted[32750 * 2] "cosmo-level-back-tiles"
		{
			level-layer
			{
				data-type: "unsigned16"
				order: 0
				layer-image: level-tiles-mapping[file.name] + "\\tiles.mni"
				name: "Background"
				ignore-tile: 0
			}
		}

		at tile-layout-start:

		encrypted [32750 * 2] "cosmo-level-fore-tiles"
		{
			level-layer
			{
				data-type: "unsigned16"
				order: 1
				layer-image: level-tiles-mapping[file.name] + "\\maskTile.mni"
				name: "Foreground"
				ignore-tile: 0xDEAD
			}
		}

		at actor-start:
		loop (actorSize / 3)
		{
				unsigned16 object-type
				unsigned16 xInTiles
				unsigned16 yInTiles

				if (object-type == 0)
				{
					sprite-index: 0
					sprite: level-tiles-mapping[file.name] + "\\players.mni\\sprite 23"
				}
				else
				{
					sprite-index: level-sprite-lookup[object-type]

					if (sprite-index == 0xFFFF)
					{
						sprite: level-tiles-mapping[file.name] + "\\actrinfo.mni\\sprite " + (object-type - 31)
						sprite-index = (object-type - 31)
					}
					else
					{
						sprite: level-tiles-mapping[file.name] + "\\actrinfo.mni\\sprite " + sprite-index
					}
				}

				if (object-type == 120)
				{
					xInTiles = xInTiles - 3

				}

				if (!(sprite-index like [85, 146, 202, 223]))
				{
					align-level-object-bottom: true
					yInTiles = yInTiles + 1
				}
				else
				{
					align-level-object-bottom: false
				}

				level-object
				{					
					offset-x: xInTiles * 8
					offset-y: yInTiles * 8	
				}
		}
	}
}

function cosmo-level-back-tiles
{
	loop while (file.remaining-bytes > 0)
	{
		read unsigned16 tile
		if (tile < 16000)
		{
			write unsigned16 tile / 8
		}
		else
		{
			write unsigned16 0
		}
	}
}

function cosmo-level-fore-tiles
{
	loop while (file.remaining-bytes > 0)
	{
		read unsigned16 tile

		if (tile >= 16000)
		{
			write unsigned16 ((tile - 16000) / 40)
		}
		else
		{
			write unsigned16 0xDEAD
		}
	}
}