display-name: ["Games/Electranoid", "Games/Laser Light", "Games/Xatax"]
meta: {"Working": ["Packed file extraction", "Fullscreen images", "Sprites"], "Thanks to": "Frenkel Smeijers"}

if (file.name like "*.res")
{
	unsigned16 file-count
	unsigned8 unknown

	package-name: file.name

	file [file-count]
	{
		fixed-string(12) name
		unsigned32 offset
		unsigned32 size
		data(2) unknown
	}

	
}

if (file.name like "*.txt")
{
	loop while (file.remaining-bytes > 0)
	{
		pascal-string(unsigned8) this-string
		text
		{
			string-data: this-string + "\r\n"
		}
	}
}

if (file.name like ["*.ani", "*.mpf", "*.spf"])
{
	unsigned8 unknown
	unsigned8 frames
	frames = frames + 1
	unsigned16 unknown

	transparent-index: 0xFF

	if (package-name == "xatax.res")
	{
		if (file.name like ["*.ani", "title.spf"])
		{
		}
		else
		{
			transparent-index = 0x0
		}
	}

	fuzzy-skip: false

	palette
	{
		size: 256
		format: "R6G6B6"
	}
	compressed [file.remaining-bytes] "pixelpainter-spr"
	{
		image
		{
			format: "I8"
			width: 320
			height: 200 * frames
		}
	}
}

//Experiments.
/*if (file.name like "pc_*.fmf")
{
	compressed[file.size - 4] "pp-sound-freq"
	{
		sound
		{
			channels: 1
            		samples-per-second: 44100
            		bits-per-sample: 16
            		volume: 255
		}
	}
}

function pp-sound-freq
{
	data(12) unknown

	loop(30)
	{
		unsigned16 frequency-sample
		unsigned16 frequency-time

		write frequency frequency-time frequency-sample 1193180 255
	}
}*/

//Not at all :)
/*if (file.name like "sb_*.fmf")
{
	sound
	{
		channels: 1
            	samples-per-second: 2000
            	bits-per-sample: 8
            	volume: 255
	}
}*/

function pixelpainter-spr
{
	result: [](0xFA00 * frames)
	result-pointer: 0

	sk-hack: (fuzzy-skip * 1)
	frame: 0

	if (sk-hack == true)
	{
		unsigned16 unknown
	}

	loop while (file.remaining-bytes > 0)
	{
		read unsigned8 val

		//at file.position - 1:

		opcode: val >> 5

		if (opcode == 0) {
			count0: (val & 0x1F) + 1
			loop (count0): i {
				read unsigned8 pixel
				result[result-pointer + i] = pixel
			}

			result-pointer = result-pointer + count0			
		} else if (opcode == 1) {
			//at file.position - 1:
			read unsigned8 count_op1

			count_op1: (((val << 8) + count_op1) & 0x1FFF) + 1

			backwards1: result-pointer - 64000
			loop(count_op1): i {
				result[result-pointer + i] = result[backwards1 + i]
			}

			result-pointer = result-pointer + count_op1
		} else if (opcode == 2) {
			//read unsigned8 count_op2
			count_op2: (val & 0x1F) + 1
			
			backwards2: result-pointer - 64000
			loop(count_op2): i {
				result[result-pointer + i] = result[backwards2 + i]
			}

			result-pointer = result-pointer + count_op2
		} else if (opcode == 3) {
			at file.position - 1:
			unsigned16be counter3
			counter3 = counter3 & 0x1FFF
			unsigned8 a
			unsigned8 b

			loop (counter3): i {
				result[result-pointer + (i * 2)] = a
				result[result-pointer + (i * 2) + 1] = b
			}

			result-pointer = result-pointer + (counter3 * 2)
		} else if (opcode == 4) {
			//read unsigned8 count1
			read unsigned8 count2

			count4: (((val & 0x1F) << 8) | count2) + 1

			read unsigned8 pixel4

			loop(count4): i
			{
				result[result-pointer + i] = pixel4
			}

			result-pointer = result-pointer + count4
		} else if (opcode == 5) {
			at file.position - 1:
			read unsigned16 long-counter
			count5: (long-counter & 0x1F) + 1

			pixel5: long-counter >> 8

			loop(count5): i {
				result[result-pointer + i] = pixel5
			}

			result-pointer = result-pointer + count5
		} else if (opcode == 6) {
			//at file.position - 1:
			unsigned8 backwards
			backwards = (((val << 8) + backwards) & 0x1FFF) + 1
			backwards = result-pointer - backwards
			unsigned8 counter6
			counter6 = counter6 + 1

			loop(counter6): i {
				result[result-pointer + i] = result[backwards + i]
			}

			result-pointer = result-pointer + counter6

			
		} else if (opcode == 7) {
			//unsigned8 counter-opcode7-1
			counter-opcode7-1: (val & 0x1F) + 1

			unsigned16 something-opcode-7

			current-frame: (result-pointer / 64000) - 1
			something-opcode-7 = (current-frame * 64000) + something-opcode-7

			loop(counter-opcode7-1): i {
				result[result-pointer + i] = result[something-opcode-7 + i]
			}

			result-pointer = result-pointer + counter-opcode7-1


		} 

		if ((sk-hack == true) and ((result-pointer % 64000) == 0))
		{
			frame = frame + 1
			if (frame == frames)
			{
				at file.size:
			}
			else
			{
				unsigned16 unknown
			}
		}
	}

	write unsigned8 result
}
