display-name: "Games/Wacky Wheels"
meta: {"Working": ["All images, tiles and sprites", "Sounds"], "Thanks": "Malvineous"}

if (file.name == "wacky.dat")
{
	unsigned16 file-count

	file [file-count]
	{
		fixed-string(14) name
		unsigned32 size
		unsigned32 offset-adjusted
		offset: offset-adjusted + 2
	}
}

if (file.name like "*.pcx")
{
	interpret-as "image-PCX"
}

if (file.name like "*.mid")
{
	interpret-as "music-midi"
}

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

	/*opl3-rate: 560
	compressed [file.remaining-bytes] "klm-opl3"
	{
		sound
		{
			channels: 1
			samples-per-second: 44100
			bits-per-sample: 16
		}
	}*/
}

function klm-opl3
{
	unsigned16 tempo
	unsigned8 unknown
	unsigned16 offset-notes

	b0-values: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	offset-list: [0, 1, 2, 8, 9, 0xA, 0x10, 0x11, 0x12]

	bdreg: 0

	at offset-notes:
	running: true
	loop while (running == true)
	{
		unsigned8 opcode
		opcode-shifted: opcode >> 4
		opcode-channel: opcode & 0xF

		if (opcode-shifted == 0)
		{
			b0-values[opcode-channel] = b0-values[opcode-channel] & 0xDF
			write opl3-reg (0xB0 | opcode-channel) b0-values[opcode-channel]
		}
		else if (opcode-shifted == 0x1)
		{
			if (opcode-channel <= 6)
			{
				unsigned8 freqA
				write opl3-reg (0xA0 | opcode-channel) freqA

				unsigned8 freqB
				b0-values[opcode-channel] = freqB
				write opl3-reg (0xB0 | opcode-channel) freqB
			}
			else
			{
				log(opcode-channel)
				bdreg = bdreg | (1 << (10 - opcode-channel))
				write opl3-reg 0xBD bdreg
			}
		}
		else if (opcode-shifted == 0x2)
		{
			unsigned8 vol
			if (opcode-channel <= 8) {
				write opl3-reg (0x40 + offset-list[opcode-channel] + 3) ( (127 - vol) / 2) 
			} else {
				write opl3-reg (0x140 + offset-list[opcode-channel - 8] + 3) ( (127 - vol) / 2) 
			}
		}
		else if (opcode-shifted == 0x3)
		{
			unsigned8 offs
			current-location: file.position

			at 5 + (11 * offs):
			read unsigned8[11] data-bytes

			if (opcode-channel <= 8) {
				write opl3-reg (0x40 + offset-list[opcode-channel]) data-bytes[0]
				write opl3-reg (0x43 + offset-list[opcode-channel]) data-bytes[1]
				write opl3-reg (0x60 + offset-list[opcode-channel]) data-bytes[2]
				write opl3-reg (0x63 + offset-list[opcode-channel]) data-bytes[3]
				write opl3-reg (0x80 + offset-list[opcode-channel]) data-bytes[4]
				write opl3-reg (0x83 + offset-list[opcode-channel]) data-bytes[5]
				write opl3-reg (0x20 + offset-list[opcode-channel]) data-bytes[4]
				write opl3-reg (0x23 + offset-list[opcode-channel]) data-bytes[5]
				write opl3-reg (0xE0 + offset-list[opcode-channel]) data-bytes[4]
				write opl3-reg (0xC0 + opcode-channel) data-bytes[5]
			} else {
				write opl3-reg (0x140 + offset-list[opcode-channel - 8]) data-bytes[0]
				write opl3-reg (0x143 + offset-list[opcode-channel - 8]) data-bytes[1]
				write opl3-reg (0x160 + offset-list[opcode-channel - 8]) data-bytes[2]
				write opl3-reg (0x163 + offset-list[opcode-channel - 8]) data-bytes[3]
				write opl3-reg (0x180 + offset-list[opcode-channel - 8]) data-bytes[4]
				write opl3-reg (0x183 + offset-list[opcode-channel - 8]) data-bytes[5]
				write opl3-reg (0x120 + offset-list[opcode-channel - 8]) data-bytes[4]
				write opl3-reg (0x123 + offset-list[opcode-channel - 8]) data-bytes[5]
				write opl3-reg (0x1E0 + offset-list[opcode-channel - 8]) data-bytes[4]
				write opl3-reg (0x1C0 + opcode-channel) data-bytes[5]
			}

			at current-location:
		}
		else if (opcode-shifted == 0x4)
		{
			b0-values[opcode-channel] = b0-values[opcode-channel] | 0x20
			write opl3-reg (0xB0 | opcode-channel) b0-values[opcode-channel]
		}
		else if (opcode == 0xFD)
		{
			unsigned8 delay
			write opl3-wait (delay * 3)			
		}
		else if (opcode == 0xFE)
		{
			unsigned16 delay2
			write opl3-wait (delay2 * 3)
		}
		else if (opcode == 0xFF)
		{
			running = false
		}
		else
		{
			log("ERROR!")
		}
	}
}

if (file.name like "*.voc")
{
	interpret-as "sound-VOC"
}

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

//############################################################################
//#IMAGES - no widths / heights are saved in images, so have to be hard-coded#
//############################################################################

palette: [
    0x00, 0x00, 0x00, 0xF4, 0xDC, 0x00, 0xF4, 0xDC, 0x00, 0xFC, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xF4,
    0xDC, 0x00, 0xF4, 0xDC, 0x00, 0xF4, 0xDC, 0x00, 0x2C, 0x88, 0xF8, 0xF4, 0xDC, 0x00, 0xC0, 0xB0,
    0x70, 0xB4, 0xA0, 0x60, 0xAC, 0x90, 0x50, 0xA4, 0x80, 0x44, 0x9C, 0x70, 0x38, 0x94, 0x64, 0x2C,
    0xB0, 0xB4, 0xC0, 0xC0, 0xC4, 0xD0, 0xD4, 0xD8, 0xE0, 0xE8, 0xEC, 0xF4, 0xFC, 0xFC, 0xFC, 0xE0,
    0xE0, 0xE0, 0xC4, 0xC4, 0xC4, 0xA8, 0xA8, 0xA8, 0x8C, 0x8C, 0x8C, 0x70, 0x70, 0x70, 0x64, 0x64,
    0x64, 0x54, 0x54, 0x54, 0x48, 0x48, 0x48, 0x38, 0x38, 0x38, 0x2C, 0x2C, 0x2C, 0x20, 0x20, 0x20,
    0x00, 0xE0, 0xE0, 0x3C, 0xF0, 0xF4, 0xC8, 0xFC, 0xFC, 0xA4, 0xF0, 0xFC, 0x84, 0xE4, 0xFC, 0x60,
    0xCC, 0xFC, 0x40, 0xB0, 0xFC, 0x34, 0x9C, 0xF8, 0x2C, 0x88, 0xF8, 0x24, 0x74, 0xF4, 0x1C, 0x5C,
    0xF4, 0x14, 0x48, 0xF4, 0x00, 0x00, 0xFC, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x8C, 0x00, 0x00, 0x54,
    0xF4, 0xA4, 0x6C, 0xDC, 0x8C, 0x50, 0xC4, 0x74, 0x3C, 0xFC, 0xC8, 0xC8, 0xFC, 0x9C, 0x9C, 0xFC,
    0x70, 0x70, 0xFC, 0x00, 0x00, 0xEC, 0x00, 0x00, 0xC8, 0x00, 0x00, 0xB0, 0x00, 0x00, 0xA4, 0x00,
    0x00, 0x98, 0x00, 0x00, 0x88, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x70, 0x00, 0x00, 0x64, 0x00, 0x00,
    0x00, 0x94, 0x50, 0x00, 0xA8, 0x60, 0x00, 0xC0, 0x78, 0x00, 0xD8, 0x90, 0xE4, 0xFC, 0xE0, 0xC4,
    0xFC, 0xBC, 0x88, 0xFC, 0x7C, 0x00, 0xFC, 0x00, 0x04, 0xE4, 0x00, 0x04, 0xD0, 0x00, 0x04, 0xBC,
    0x00, 0x04, 0xA8, 0x00, 0x0C, 0x94, 0x00, 0x0C, 0x7C, 0x00, 0x0C, 0x68, 0x00, 0x0C, 0x54, 0x00,
    0xFC, 0xFC, 0xE4, 0xFC, 0xFC, 0xC0, 0xFC, 0xFC, 0x84, 0xFC, 0xFC, 0x00, 0xF4, 0xDC, 0x00, 0xEC,
    0xC0, 0x00, 0xE4, 0xA4, 0x00, 0xDC, 0x8C, 0x00, 0xD4, 0x74, 0x00, 0xCC, 0x60, 0x00, 0x00, 0x00,
    0x00, 0xC4, 0x58, 0x00, 0xB8, 0x4C, 0x00, 0xEC, 0xC8, 0xBC, 0xE4, 0xB8, 0xB0, 0xDC, 0xAC, 0xA4,
    0xFC, 0xE8, 0xDC, 0xFC, 0xD4, 0xBC, 0xFC, 0xBC, 0x9C, 0xFC, 0xB0, 0x80, 0xFC, 0x9C, 0x60, 0xDC,
    0x88, 0x54, 0xC8, 0x84, 0x54, 0xAC, 0x6C, 0x44, 0xB4, 0x70, 0x44, 0xA0, 0x64, 0x3C, 0x88, 0x58,
    0x30, 0x7C, 0x4C, 0x20, 0x74, 0x40, 0x14, 0x68, 0x38, 0x0C, 0x60, 0x30, 0x04, 0x58, 0x28, 0x00,
    0xFC, 0xFC, 0xFC, 0xEC, 0xF4, 0xE8, 0xDC, 0xF0, 0xD8, 0xD0, 0xE8, 0xCC, 0xC0, 0xE4, 0xBC, 0xB4,
    0xDC, 0xB0, 0xA8, 0xD8, 0xA0, 0x9C, 0xD4, 0x94, 0x90, 0xCC, 0x88, 0x84, 0xC8, 0x7C, 0x64, 0xB8,
    0x5C, 0x48, 0xA8, 0x3C, 0x30, 0x9C, 0x24, 0x18, 0x8C, 0x0C, 0x08, 0x80, 0x00, 0x48, 0x10, 0x00,
    0xFC, 0xFC, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0xA4, 0x88, 0x40, 0x64, 0x64, 0x64, 0x54,
    0x54, 0x54, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0xE4, 0xCC, 0x00, 0xDC, 0x8C,
    0x00, 0x84, 0x68, 0x20, 0xFC, 0xFC, 0x00, 0xF0, 0xD8, 0x00, 0xE4, 0xB8, 0x00, 0xD0, 0x90, 0x00,
    0xFC, 0xFC, 0xFC, 0xE0, 0xE0, 0xE0, 0xD0, 0xD0, 0xD0, 0xBC, 0xBC, 0xBC, 0xAC, 0xAC, 0xAC, 0x84,
    0x84, 0x84, 0x80, 0x80, 0x80, 0x7C, 0x7C, 0x7C, 0x78, 0x78, 0x78, 0x70, 0x70, 0x70, 0x5C, 0x5C,
    0x5C, 0x48, 0x48, 0x48, 0x34, 0x34, 0x34, 0xFC, 0x00, 0x00, 0xE4, 0x00, 0x00, 0x00, 0x00, 0x8C,
    0xE0, 0xB8, 0x68, 0xD0, 0xA8, 0x58, 0xC4, 0x9C, 0x4C, 0xA8, 0x80, 0x2C, 0x90, 0x68, 0x14, 0xE4,
    0xD0, 0x90, 0xDC, 0xC8, 0x7C, 0xD8, 0xC0, 0x80, 0xC8, 0xB0, 0x70, 0xB8, 0xA0, 0x60, 0xAC, 0x94,
    0x54, 0xFC, 0xFC, 0x00, 0xFC, 0xD4, 0x00, 0xFC, 0xB0, 0x00, 0x64, 0x00, 0x00, 0x8C, 0x00, 0x00,
    0x5C, 0x1C, 0x04, 0x6C, 0x28, 0x0C, 0x7C, 0x38, 0x18, 0x90, 0x48, 0x24, 0xA0, 0x5C, 0x30, 0xB4,
    0x70, 0x44, 0xFC, 0xA8, 0x78, 0xF0, 0x90, 0x60, 0xE4, 0x7C, 0x4C, 0xD8, 0x6C, 0x38, 0xCC, 0x58,
    0x28, 0xC4, 0x48, 0x18, 0x00, 0x00, 0xFC, 0x00, 0x04, 0xFC, 0x04, 0x0C, 0xFC, 0x08, 0x14, 0xFC,
    0x04, 0xBC, 0x00, 0x00, 0xAC, 0x00, 0x04, 0x94, 0x00, 0x08, 0x80, 0x00, 0x08, 0x68, 0x00, 0x0C,
    0x54, 0x00, 0x0C, 0xC4, 0x00, 0x14, 0xD0, 0x00, 0x0C, 0x1C, 0xFC, 0x10, 0x24, 0xFC, 0x14, 0x2C,
    0xFC, 0x1C, 0x34, 0xFC, 0x20, 0x3C, 0xFC, 0x24, 0x44, 0xFC, 0x28, 0x4C, 0xFC, 0x2C, 0x50, 0xFC,
    0xC8, 0x84, 0x54, 0xB8, 0x78, 0x4C, 0xAC, 0x6C, 0x44, 0xA0, 0x64, 0x3C, 0xA0, 0x64, 0x3C, 0x8C,
    0x50, 0x28, 0x30, 0x58, 0xFC, 0x34, 0x60, 0xFC, 0x38, 0x64, 0xFC, 0x40, 0x6C, 0xFC, 0x44, 0x74,
    0xFC, 0x48, 0x78, 0xFC, 0x4C, 0x80, 0xFC, 0x50, 0x84, 0xFC, 0x54, 0x8C, 0xFC, 0x58, 0x90, 0xFC,
    0xC4, 0xC4, 0xC4, 0xA8, 0xA8, 0xA8, 0x8C, 0x8C, 0x8C, 0x70, 0x70, 0x70, 0x54, 0x54, 0x54, 0x3C,
    0x3C, 0x3C, 0x5C, 0x94, 0xFC, 0x64, 0x9C, 0xFC, 0x68, 0xA0, 0xFC, 0x6C, 0xA4, 0xFC, 0x70, 0xAC,
    0xFC, 0x74, 0xB0, 0xFC, 0x78, 0xB4, 0xFC, 0x7C, 0xB8, 0xFC, 0x84, 0xBC, 0xFC, 0x88, 0xC0, 0xFC,
    0x8C, 0xC4, 0xFC, 0x90, 0xC8, 0xFC, 0x94, 0xCC, 0xFC, 0x98, 0xD0, 0xFC, 0x9C, 0xD4, 0xFC, 0xA0,
    0xD8, 0xFC, 0xA8, 0xDC, 0xFC, 0xAC, 0xDC, 0xFC, 0xB0, 0xE0, 0xFC, 0xB4, 0xE4, 0xFC, 0xB8, 0xE4,
    0xFC, 0xBC, 0xE8, 0xFC, 0xC0, 0xEC, 0xFC, 0xC4, 0xEC, 0xFC, 0xCC, 0xF0, 0xFC, 0xFC, 0xFC, 0xFC
]

par-palette:
{
	"1.par": "a_f1.pcx",
	"2.par": "a_f3.pcx",
	"3.par": "a_f3.pcx",
	"4.par": "a_f7.pcx",
	"5.par": "a_f13.pcx",
	"7.par": "a_f13.pcx",
	"16.par": "a_f3.pcx",
	"17.par": "a_f17.pcx"
}

if (file.name like "*.par")
{
	folder: "Horizon graphics"

	image
	{
		format: "I8"
		width: 640
		height: 40
		palette: par-palette[file.name]
	}
}

columnar: true
transparent-index: 0

//

sprite-list-regular:
{
	"icons.sp": {"widths": [26, 42, 10], "heights": [21, 19, 15], "tile-counts": [6, 1, 10], "tiles-across": [1, 1, 1]},
	"dope.sp": {"widths": [80, 25], "heights":[72, 15], "tile-counts": [2, 1], "tiles-across": [1, 1]},
	"lap.sp": {"widths": [22, 80, 38, 126, 154], "heights": [ 29, 17, 41, 19, 9], "tile-counts": [18, 2, 1, 1, 1], "tiles-across": [1, 1, 1, 1, 1]},
	"ofont.sp": {"widths": [8, 92, 8], "heights": [8, 41, 8], "tile-counts": [61, 1, 137], "tiles-across": [26, 1, 26]}
}

if (file.name like ["icons.sp", "dope.sp", "lap.sp", "ofont.sp"])
{
	folder: "Sprites"

	sprite-info: sprite-list-regular[file.name]

	loop (sprite-info.widths.length): i
	{
		image
		{
			format: "I8"
			width: sprite-info.widths[i]
			height: sprite-info.heights[i]
			tile-count: sprite-info.tile-counts[i]
			tiles-across: sprite-info.tiles-across[i]
		}		
	}
}

//


sprite-list-effects:
{
	0: { "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	1: { "width": 30, "height": 9,	"tile-count": 1, "tiles-across": 1},
	2: { "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	3: { "width": 30, "height": 9,	"tile-count": 1, "tiles-across": 1},
	4: { "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	5: { "width": 30, "height": 9,	"tile-count": 1, "tiles-across": 1},
	6: { "width": 12, "height": 24,	"tile-count": 2, "tiles-across": 2},		// water effect
	7: { "width": 38, "height": 28,	"tile-count": 4, "tiles-across": 4},		// water flosch
	8: { "width": 32, "height": 24,	"tile-count": 8, "tiles-across": 8},		// periscope (water)
	9: { "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	10:{ "width": 30, "height": 9,	"tile-count": 1, "tiles-across": 1},
	11:{ "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	12:{ "width": 30, "height": 9,	"tile-count": 3, "tiles-across": 1},
	13:{ "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	14:{ "width": 38, "height": 28,	"tile-count": 4, "tiles-across": 4},
	15:{ "width": 32, "height": 24,	"tile-count": 8, "tiles-across": 8},		// periscope (lava)
	16:{ "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	17:{ "width": 30, "height": 9,	"tile-count": 1, "tiles-across": 1},
	18:{ "width": 60, "height": 12,	"tile-count": 1, "tiles-across": 1},
	19:{ "width": 30, "height": 9,	"tile-count": 1, "tiles-across": 1},
	20:{ "width": 30, "height": 9,	"tile-count": 1, "tiles-across": 1}
}

if (file.name == "effects.sp")
{
	folder: "Sprites"

	loop (21): i
	{
		image
		{
			format: "I8"
			width: sprite-list-effects[i].width
			height: sprite-list-effects[i].height
			tile-count: sprite-list-effects[i].tile-count
			tiles-across: sprite-list-effects[i].tiles-across
		}
	}		
}

//

sprite-list-genef:
{
	0: { "width": 28, "height": 23,	"tile-count": 6},				// medals
	1: { "width": 45, "height": 13,	"tile-count": 1},				// cups
	2: { "width": 38, "height": 28,	"tile-count": 8},				// start lion, etc
	3: { "width": 28, "height": 22,	"tile-count": 1},
	4: { "width": 12, "height": 13,	"tile-count": 1},
	5: { "width": 77, "height": 9,	"tile-count": 1},
	6: { "width": 19, "height": 14,	"tile-count": 1}
}

if (file.name == "genef.sp")
{
	folder: "Sprites"

	index: 0
	loop (7): i
	{
		image
		{
			format: "I8"
			width: sprite-list-genef[i].width
			height: sprite-list-genef[i].height
			tile-count: sprite-list-genef[i].tile-count
		}
	}
}

////

if (file.name like ["panda.sp", "pelican.sp", "ringo.sp", "shark.sp", "tiger.sp", "camel.sp", "ele.sp", "moose.sp", "digger.sp", "aj.sp", "puf.sp", "cars.sp"])
{
	folder: "Sprites"

	image
	{
		format: "I8"
		width: 38
		height: 28
		data-size: file.size
		tiles-across: 4
	}
}

if (file.name like ["bronzem.sp", "silverm.sp", "goldm.sp", "bonusb.sp", "bonuss.sp", "bonusg.sp"])
{
	folder: "Sprites"
	
	image
	{
		format: "I8"
		width: 78
		height: 50
		data-size: file.size
	}
}

if (file.name like ["ob1.sp", "ob5.sp", "ob6.sp", "ob7.sp", "ob8.sp", "ob9.sp"])
{
	folder: "Sprites"

	image
	{
		format: "I8"
		width: 28
		height: 28
		data-size: file.size
	}
}

if (file.name like ["ob2.sp", "ob3.sp", "ob4.sp", "ob10.sp", "ob11.sp", "ob13.sp"])
{
	folder: "Sprites"

	image
	{
		format: "I8"
		width: 14
		height: 28
		data-size: file.size
	}
}

if (file.name == "giggles.sp")
{
	folder: "Sprites"

	image
	{
		format: "I8"
		width: 38
		height: 44
		tiles-across: 4
		data-size: file.size
	}
}

simple-sprite-table:
{
	"hogmis.sp":	{"width": 18, 	"height": 13},
	"handle.sp":	{"width": 20,	"height": 23},
	["h*.sp"]:		{"width": 32,	"height": 24},
	"crack.sp":		{"width": 84,	"height": 62},
	"ob12.sp":		{"width": 32,	"height": 24},
	"wfont1.sp":	{"width": 15,	"height": 13},
	"action.sp":	{"width": 251,	"height": 6},
	"spark.sp":		{"width": 10,	"height": 9}
}

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

	image
	{
		format: "I8"
		width: simple-sprite-table[file.name].width
		height: simple-sprite-table[file.name].height
		data-size: file.size
	}
}