display-name: ["Games/The Smurfs"]
meta: {"Working": ["Packed file extraction", "Sprites"], "Partially working": "Tiles - have colour issues"}


if (file.name like "*.epf")
{
	interpret-as "archive-epfs"	
}

if (file.name like ["*.hmp", "*.mid"])
{
	folder: "Music"
}

if (file.name like "*.lbm")
{
	interpret-as "image-ILBM"
}

if (file.name like ["*.map", "*.sen", "*.col", "*.dav"])
{
	folder: "Levels\\" + (file.name - 4)
}

file-format internal-expand-palette
{
	file[8]: i
	{
		name: "" + i
		offset: 16 * 3 * i
		size: 16 * 3
		interpret-as "short-pal"
	}
}

file-format short-pal
{
	palette
	{
		size: 16
		format: "R6G6B6"
	}
}

stiles-table:
{
	["village.*"]: 0x3,
	["champ1.*", "barrage.*", "carteb.*"]: 0x5,
	["foret1.*", "marais.*", "mine.*"/*, "salle.*"*/]: 0x2,
	["lac1.*", "luge.*"]: 0x3,
	["*"]: 0x3
}

//yxup pptt tttt tttt

//p - palette index
//u - unknown
//x - x flip
//y - y flip

if (file.name == "lave.dfd")
{
	folder: "Level backgrounds (missing tilesheet)"
}

if (file.name like "*.dfd")
{
	folder: "Level backgrounds"

	level
	{
		loop(8): i 
		{
			at 0:

			current-pal: i
			compressed[file.size] "merge-bg-layers"
			{
				level-layer
				{
					grid-x: 8
					grid-y: 8
					width: 32
					height: (file.size / 2) / 32
					data-type: "unsigned16"
					ignore-tile: 0x3FF

					name-for-pal: file.name
					if (name-for-pal like "foret*")
					{
						name-for-pal = "foret1.dfd"
					}

					layer-image: (name-for-pal - 3) + "sbk\\" + i
					mask: 0x3FF
					xflip-bit: 14
					yflip-bit: 15
				}
			}
		}
	}
}

function merge-bg-layers
{
	loop (file.size / 2)
	{
		read unsigned16 thisTile
		palette-index: (thisTile >> 10) & 0x7

		if (palette-index != current-pal)
		{
			write unsigned16 0x3FF
		}
		else
		{
			write unsigned16 thisTile
		}
	}
}

extra-pal: [
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0,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0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
77, 40, 24,
130, 73, 32,
113, 60, 32,
121, 65, 32,
146, 85, 32,
60, 32, 20,
255, 255, 255,
255, 255, 255,
0, 158, 223,
0, 121, 182,
0, 89, 146,
0, 60, 109,
203, 203, 203,
146, 146, 146,
97, 97, 97,
48, 48, 48,
0, 0, 0,
97, 81, 0,
174, 130, 20,
243, 174, 24,
195, 0, 0,
255, 0, 0,
89, 48, 12,
97, 65, 0,
113, 73, 20,
134, 81, 24,
134, 89, 0,
146, 89, 32,
158, 97, 73,
134, 105, 65,
146, 105, 0,
166, 121, 65,
182, 134, 65,
186, 166, 121,
186, 182, 89,
0, 0, 0,
20, 89, 97,
24, 109, 109,
211, 113, 48,
211, 105, 81,
223, 113, 81,
227, 121, 81,
235, 142, 81,
243, 146, 89,
243, 158, 89,
255, 174, 89,
186, 113, 32,
211, 134, 40,
235, 166, 0,
243, 195, 0,
255, 211, 0,
195, 158, 65,
227, 182, 48,
227, 211, 121,
40, 56, 20,
56, 77, 24,
73, 97, 32,
89, 117, 40,
105, 138, 48,
125, 158, 56,
142, 174, 69,
162, 195, 77,
182, 223, 89,
211, 158, 121,
235, 182, 146,
223, 203, 182,
32, 48, 20,
97, 56, 97,
134, 89, 134,
158, 113, 158,
203, 146, 203,
56, 56, 56,
73, 73, 73,
89, 89, 89,
97, 105, 97,
134, 134, 134,
223, 223, 223,
142, 20, 0,
166, 24, 0,
174, 56, 12,
174, 81, 40,
211, 65, 32,
227, 65, 89,
227, 89, 73,
97, 97, 48,
113, 113, 32,
134, 134, 73,
158, 146, 56,
158, 158, 89,
182, 182, 113,
195, 203, 166,
48, 134, 81,
20, 81, 81,
32, 105, 73,
32, 121, 24,
32, 146, 24,
32, 174, 24,
174, 255, 211,
48, 121, 134,
65, 158, 158,
89, 174, 158,
121, 186, 195,
146, 203, 211,
166, 223, 223,
186, 227, 227,
211, 243, 243,
235, 255, 255,
40, 146, 158,
97, 97, 97,
117, 117, 117,
134, 134, 134,
150, 150, 150,
166, 166, 166,
186, 186, 186,
203, 203, 203,
219, 219, 219,
235, 235, 235,
255, 255, 255
]

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

	//This is incorrect and still being worked out
	unsigned16 frame-count

	offsets: [](frame-count)
	sizes: [](frame-count)
	widths: [](frame-count)
	heights: [](frame-count)

	loop (frame-count): i
	{
		unsigned32 this-offset
		offsets[i] = this-offset

		unsigned16 this-width
		widths[i] = this-width

		unsigned16 this-height
		heights[i] = this-height
	}

	loop(frame-count - 1): i
	{
		sizes[i] = offsets[i + 1] - offsets[i]
	}

	sizes[frame-count - 1] = file.size - offsets[frame-count - 1]

	loop (frame-count): i
	//file[frame-count]: i
	{
		at offsets[i]:

		width: widths[i]
		height: heights[i]

		compressed [sizes[i]] "epf-sprite-2"
		{
			image
			{
				format: "I8"
				transparent-index: 0
				palette: extra-pal
			}
		}
		/*offset: offsets[i]
		size: sizes[i]
		name: "Frame " + i

		interpret-as "sprite-test"*/
	} 
}


file-format sprite-test
{
	width2: width
	height2: height
	compressed[file.size] "epf-sprite-2"
	{
		image
		{
			format: "I8"
		}
	}
}

function epf-sprite-2
{
	bytes-written: 0
	any-written: false

	loop while ((bytes-written < width * height) and (file.remaining-bytes > 0))
	{
		read unsigned8 amount
		if (amount == 0)
		{
			remaining: bytes-written % width

			if ((remaining != 0) or (any-written == false))
			{
				remaining = width - remaining
				loop (remaining)
				{
					write unsigned8 0
				}
				bytes-written = bytes-written + remaining
			}
		}
		else if (amount == 0x83)
		{
			if (any-written == false)
			{
				loop (width)
				{
					write unsigned8 0
				}
				bytes-written = bytes-written + width
			}
		}
		else
		{
			any-written = true

			count: amount

			if ((amount & 0x80) != 0)
			{
				actual-amount: 256-amount

				if (amount == 0x81)
				{
					read unsigned8 next
					actual-amount = actual-amount + (256-next)
				}

				loop (actual-amount)
				{
					write unsigned8 0
				}

				bytes-written = bytes-written + actual-amount
				read unsigned8 _count
				count = _count
			}
			/*else
			{
				count: amount	
			}*/

			if (count != 0)
			{
				loop (count)
				{
					read unsigned8 value
					write unsigned8 value
				}

				bytes-written = bytes-written + count
			}

		}
	}

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

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

	compressed [file.size] "smurf-tiles-fix"
	{
		image
		{
			format: "I8"
			width: 16
			height: 16
			tiles-across: 10
			data-size: file.size
			palette: (file.name - ".lbk") + ".mpl"
		}
	}
}

function smurf-tiles-fix
{
	loop (file.size)
	{
		read unsigned8 value
		write unsigned8 (value & 0x7F)
	}
}

function smurf-stiles-fix
{
	loop (file.size)
	{
		read unsigned8 value
		write unsigned8 (value + stiles-offset)
	}
}

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

	interpret-as-internally "sbk-expand"

	palette: [](16 * 3)
	loop(16): i
	{
		palette[i * 3] = i * 16
		palette[i * 3 + 1] = i * 16
		palette[i * 3 + 2] = i * 16
	}

	image
	{
		format: "I8"
		width: 8
		height: 8
		tiles-across: 20
		data-size: file.size
	}
}

file-format sbk-expand
{
	file[8]: i
	{
		offset: 0
		name: "" + i
		size: file.size
		index: i
		palette-name: (file.name - 3) + "mpl"

		interpret-as "internal-sbk"
	}
}

file-format internal-sbk
{
	image
	{
		format: "I8"
		width: 8
		height: 8
		tiles-across: 20
		data-size: file.size
		palette: palette-name + "\\" + index
	}
}

if (file.name like "*.mpl")
{
	interpret-as-internally "internal-expand-palette"
	
	palette
	{
		format: "R6G6B6"
		size: 128
	}
}