display-name: "Games/Duke Nukem"
meta: {"Working": "All images, tiles and sprites",
"Issues": "Layout problems", "Thanks to": "Frenkel Smeijers" }

if (file.name like ["*.dn1", "*.dn2", "*.dn3"] and ((file.size == 32000) or (file.size == 32256)))
{
	folder: "Fullscreen images"
	image
	{
		format: "I4"
		width: 320
		height: 200
		bit-stride: width * height		
	}
}


folders:
{
	["back*"]:	"Tiles",
	["solid*"]:	"Tiles",
	["*"]:		"Sprites"
}

if ((file.name like ["*.dn1", "*.dn2", "*.dn3"]) and ((file.size == 8064) or (file.size == 8192)))
{
	folder: folders[file.name]

	unsigned8 count
	unsigned8 width-in-bytes
	unsigned8 height

	image-layout-width: 160
	image-layout
	{	
		loop (count)
		{
			image
			{
				format: "A1I4"
				width: width-in-bytes * 8			
				tiles-across: 4
				bit-stride: 8
			}	
		}
	}
}


//some layout data for Duke images with a common format
layout-options:
{
	["border.dn?", "numbers.dn?"]:	{"width": 16, "height": 16, "tiles-across": 20, "folder": "Images"},
	["font?.dn?"]:	{"width": 8,  "height": 8,  "tiles-across": 25, "folder":"Images"},
	["*.dn?"]:	{"width": 16, "height": 16, "tiles-across": 13, "folder": "Backdrops"}
}

if (file.name like ["border.dn?", "font*.dn?", "numbers.dn?"] or (file.name like ["*.dn1", "*.dn2", "*.dn3"] and file.size == 20803))
{
	folder: layout-options[file.name].folder

	//same as crystal caves...?
	data(3) unknown

	image
	{
		format: "A1I4"
		width: layout-options[file.name].width
		height: layout-options[file.name].height
		tiles-across: layout-options[file.name].tiles-across
		data-size: file.size - 3
		bit-stride: 8
	}
}

level-image-lookup: {
	["*.dn1"]: "1",
	["*.dn2"]: "2",
	["*.dn3"]: "3"
}

level-layer-lookup: {
	0: "back0.dn",
	1: "back1.dn",
	2: "back2.dn",
	3: "back3.dn",
	4: "solid0.dn",
	5: "solid1.dn",
	6: "solid2.dn",
	7: "solid3.dn"
}

if (file.name like "world*.dn*")
{
	folder: "Levels" //no level support

	/*level
	{
		width: 128
		height: 90
		grid-x: 16
		grid-y: 16
		data-type: "unsigned8"

		ignore-tile: 0xFFFF

		//BG layer parts
		loop (8): i
		{
			at 0:
			bg-map: i * 0x600
			compressed [file.size] "dn1-bg-level"
			{
				level-layer
				{
					name: "Background"
					layer-image: level-layer-lookup[i] + level-image-lookup[file.name]

				}
			}
		}
	}*/
}

function dn1-bg-level
{
	last-tile: 0xFFFF

	loop (file.size / 2)
	{
		read unsigned16 value

		if (value < 0x3000)
		{
			last-tile = 0xFFFF
			tile-index: value - bg-map

			if ((tile-index >= 0) and (tile-index < 0x600))
			{
				last-tile = tile-index / 32
			}
		}

		write unsigned8 last-tile
	}
}

if (file.name like "highs.dn?")
{
	folder: "High scores"

	text
}

if (file.name like ["duke1-b.dn*", "duke1.dn*"])
{
	folder: "Sound"
	interpret-as "archive-SND_DOS"
}