display-name: "Games/Baron Baldric"
meta: {"Working": ["Packed file extraction", "Tiles"], "Not yet working": "Sprites, audio, and levels" }

if (file.name like "*.bnk")
{
  	file while (file.remaining-bytes > 0)
	{
		dirty-string(4) ID == "-ID-"
	  	dirty-string(12) name

  		unsigned32 size
		offset: file.position

		data(size) file-data
	}
}

if (file.name like "*.pal")
{
	palette
	{
		format: "R6G6B6"
		size: 256
	}
}

if (file.name like "baldric.spr")
{
	i: 0
	loop while (file.remaining-bytes > 0)
	{
		size-test: 0
		file[1]
		{
			name: "sprite " + i
			unsigned16 size
			size-test = size
			offset: file.position
			//compressed "baronb-sprites"
		}

		i = i + 1
		data(size-test) unknown
	}
}

//INCORRECT
function baronb-sprites
{
	read unsigned16 width
	write unsigned16 width

	read unsigned16 height
	write unsigned16 height

	read unsigned16 initial-blank
	/*loop (initial-blank)
	{
		write unsigned8 0
	}*/

	pixels-line: 0
	pixels-written: 0

	loop (height)
	{
		read unsigned16 pixel-count
		loop (pixel-count)
		{
			read unsigned8 pixel
			write unsigned8 pixel
		}

		pixels-line = pixels-line + pixel-count
		pixels-written = pixels-written + pixel-count

		read unsigned16 empty-count

		at file.position - 2:
		read signed8 test
		read signed8 test2

		log ("" + empty-count + ", " + pixel-count + ", " + test)

		if ((empty-count & 0x80) == 0)
		{
			loop (empty-count)
			{
				write unsigned8 0
			}

			pixels-written = pixels-written + empty-count
			pixels-line = pixels-line + empty-count
		}
		else
		{
			loop ((width - pixels-line) + (255 - empty-count))
			{
				write unsigned8 0
			}

			pixels-written = pixels-written + (255 - empty-count)
			pixels-line = (255 - empty-count)
		}
	}
}

if (file.name like "baldric.fnt")
{
	image-layout-width: 80
	image-layout
	{
		loop while (file.remaining-bytes > 0)
		{
			unsigned16 width
			unsigned16 height

			image
			{
				format: "I8"
				palette: [0, 0, 0, 0xFF, 0xFF, 0xFF]
			}
		}
	}
}

int-pals: {
	"graph.int": "baldric.pal",
	"config.int": "config.pal",
	["sc*1.int"]: "scp1.pal",
	["sc*2*.int"]: "scp2.pal",
	["sc*3.int"]: "scp3.pal",
	["sc*4.int"]: "scp4.pal",
	["sc*5.int"]: "scp5.pal",
	["sc*6*.int"]: "scp6.pal",
	"scbops.int": "scpops.pal"
}

if (file.name like "*.int")
{
	compressed [file.size] "baldric-int-compression"
	{
		image
		{
			format: "I8"
			width: 320
			height: 200
			palette: int-pals[file.name]
		}
	}
}

function baldric-int-compression
{
	//unsigned8 unknown
	loop (file.size / 2)
	{
		read unsigned8 pixel
		read unsigned8 rep

		loop(rep)
		{
			write unsigned8 pixel
		}
	}
}

if (file.name like "*.bbk")
{
	loop while (file.remaining-bytes > 0)
	{
		unsigned16 width
		unsigned16 height

		image
		{
			palette: "scp" + file.name[2] + ".pal"

			format: "I8"
		}
	}
}

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

if (file.name like ["bfont2.fnt", "baldfnt.fnt", "digit.fnt"])
{
	image
	{
		tiles-across: 10
		format: "I8"
		palette: [0, 0, 0, 0xFF, 0xFF, 0xFF]
		width: 8
		height: 8
		data-size: file.size
	}
}

if (file.name == "baldric.map")
{
	text
}

/*if (file.name like "*.map")
{
	level
	{
		unsigned16be width
		unsigned16be height

		level-layer
		{
			grid-x: 16
			grid-y: 16
			data-type: "unsigned8"
			layer-image: "baldric.chr"
		}
	}
}*/

if ((file.name like "*.ico") and !(file.name == "baldric.ico"))
{
	palette: "_baldric.bnk/baldric.pal"

	image-layout-width: 460
		
	image-layout
	{
		loop while (file.remaining-bytes > 0)
		{
			unsigned16 width
			unsigned16 height

			image
			{
				format: "I8"
			}
		}
	}
}

if (file.name like "flat.fnt")
{
	folder: "Fonts"

	image
	{
		format: "I8"
		width: 8
		height: 8
		tiles-across: 26
		data-size: file.size
		//palette: "config.pal"
		transparent-index: 0
	}
}

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

	image
	{
		format: "I8"
		width: 16
		height: 16
		tiles-across: 18
		data-size: file.size
		palette: "baldric.pal"
		transparent-index: 0
	}
} 