﻿display-name: "Games/Simcity 2000 (DOS)"
meta: {"Working": ["Packed file extraction", "All images", "Sounds"]}

palette: "mine.pal"

if (file.name like ["sc2000.dat", "resource.urk"])
{
	file
	{
		fixed-string(12) name
		unsigned32 offset
	} while (file.position < first-offset)
}

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

if (file.name like ["str*.raw", "1*.raw", "2*.raw"])
{
	unsigned16be string-count
	
	loop (string-count)
	{
		pascal-string(unsigned8) this-string
		text
		{
			string-data: this-string + "\r\n"
		}
	}
}

if (file.name like ["roadpal.raw", "1*.raw", "2*.raw", "cur.raw", "cult*.raw", "ppdt*.raw"])
{
	folder: "Other"
}

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

if (file.name == "small.dat")
{
	from "small.hed"
	{
		file [file.size / 8]: i
		{
			unsigned32 offset
			unsigned8 height
			unsigned8 width
			unsigned16 ff

            name: "Sprite " + i
			
			skip-if (offset == 0xFFFFFFFF)

			interpret-as "sc2k-draw"
		}		
	}
}

if (file.name == "large.dat")
{
    from "large.hed"
    {
        file [file.size / 8]: i
        {
            unsigned32 offset
            unsigned8 height
            unsigned8 width
            unsigned16 ff

            name: "Sprite " + i
            
            skip-if (offset == 0xFFFFFFFF)

			interpret-as "sc2k-draw"
        }       
    }
}

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

if (file.name == "title.raw")
{
	data(4) unknown
	image
	{
		format: "I8"		
		width: 640
		height: 480
	}
}

if ((file.name like "*.raw") and (!(file.name == "roadpal.raw")))
{
	unsigned16 height
	unsigned16 width

	if ((width % 2) == 1)
	{
		width = width + 1
		height = (file.size - 4) / width //hack, there is not enough data to draw?
	}

	image
	{
		format: "I8"
	}
}

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

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

if (file.name like "txt*")
{
	text
}

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

file-format sc2k-draw
{
	width: width
	height: height
	compressed[file.size] "sc2k-sprite"
	{
		image
		{
			transparent-index: 0
			format: "I8"
		}
	}
}

function sc2k-sprite
{
	bytes-written: 0
	row: 0
	loop while (file.remaining-bytes > 0)
	{
		read unsigned8 sometimes10
		row = row + 1

		if (sometimes10 == 0x10)
		{
			read unsigned8 count
			end-of-data: file.position + count - 1
			pixels-written: 0

			loop while (file.position < end-of-data)
			{
				read unsigned8 mode

				if (mode == 4)
				{
					read unsigned8 blank-pixels
					read unsigned8 b
					read unsigned8 pixel-count

					loop(blank-pixels)
					{
						write unsigned8 0
					}

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

					pixels-written = pixels-written + pixel-count + blank-pixels
				}
				else if (mode == 12)
				{
					read unsigned8 pixel-count
					loop(pixel-count)
					{
						read unsigned8 pixel
						write unsigned8 pixel
					}

					pixels-written = pixels-written + pixel-count
				}
			}

			if (pixels-written < width)
			{
				loop(width - pixels-written)
				{
					write unsigned8 0
				}
			}

			bytes-written = bytes-written + width
		}
	}

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