// Smart Tile Handlers.
// Worthless Bums 2012.


// Test function to return indices of squad members.
function getSquadIndices ()
{//echo("getSquadIndices()");
	for (%i = 0; %i < 4; %i++)
	{
		if (isObject($playerSquad[%i]))
		{
			echo(%i SPC $playerSquad[%i].xIndex SPC $playerSquad[%i].yIndex);
		}
	}
}

// Gets the frame index in the tileset atlas for the specified tile.
function getAtlasFrame (%north, %south, %east, %west,
					    %nw, %ne, %sw, %se,
					    %ss, %ssw, %sse)			// Special case for protruding south wall!
{//echo("getAtlasFrame()");
	// "Default" frame.
	%frame = -1;
	
	/*
	 * SPECIAL CASE: Bottom-most edge of map.
	 */
	// West, East, South, South-West, and South-East blocked.
	// North, North-West. amd North-East clear.
	if (	 %west		&&
			 %east		&&
			 %south		&&
			 %sw		&&
			 %se		&&
			 !%north)
	{
		%frame = 70;//"WallThick1ImageMap";
	}
	/*
	 * SPECIAL CASE: Tile North of "South Protruding Wall".
	 */
	// West and East blocked.
	// North and South clear.
	if (	%north		&&
			 %west		&&
			 %east		&&
			 %south		&&
			 !%sw		&&
			 !%se		&&
			 !%ss)
	{
		%frame = 53;//"SouthProtrudingCenterImageMap";
		return %frame;
	}
	/*
	 * SPECIAL CASE: SE rounded corner placement.
	 */
	// North and West blocked.
	// South and East clear.
	if (	%north		&&
			 %west		&&
			 !%south	&&
			 !%east)
	{
		%frame = 37;//"OuterCorner4ImageMap";
		return %frame;
	}
	/*
	 * SPECIAL CASE: Island Wall tile.
	 */
	// North, South, East, and West clear.
	if (	!%north		&&
			 !%south	&&
			 !%east		&&
			 !%west)
	{//echo("NSEW:" SPC %north SPC %south SPC %east SPC %west);
		%frame = 42;//"IslandWallImageMap";
		return %frame;
	}
	/*
	 * SPECIAL CASE: North-West Outer Corner and South-East Inner Corner.
	 */
	// East and South blocked.
	// North, West, South-East clear.
	if (	 %east		&&
			 %south		&&
			 !%north	&&
			 !%west		&&
			 !%se)
	{//echo("frame 48!");
		%frame = 48;//49;//"OuterNWInnerSEImageMap";
		return %frame;
	}
	/*
	 * SPECIAL CASE: North-East Outer Corner and South-West Inner Corner.
	 */
	// West and South blocked.
	// North, East, and South-West clear.
	if (	 %west		&&
			 %south		&&
			 !%north	&&
			 !%east		&&
			 !%sw)
	{
		%frame = 48;//"OuterNEInnerSWImageMap";
		return %frame;
	}
	/*
	 * SPECIAL CASE: North, NW, W, SE clear, south door.
	 */
	// East and South blocked.
	// North, West, and South-East clear.
	if (	 %east		&&
			 %south		&&
			 !%north	&&
			 !%west		&&
			 !%se)
	{
		%frame = 48;//"OuterNEInnerSWImageMap";
		return %frame;
	}
	
	
	
	
	/*
	 * "Rounded Corner" tiles.
	 */
	// East, South, and South-East blocked
	// North and West clear.
	else if (%east		&&
			 %south		&&
			 %se		&&
			 !%north	&&
			 !%west)
	{
		%frame = 44;//"OuterCorner2ImageMap";
	}
	// West, South, and South-East blocked
	// North and East clear.
	else if (%west		&&
			 %south		&&
			 //%se		&&
			 !%north	&&
			 !%east)
	{
		%frame = 43;//"OuterCorner1ImageMap";
	}
	// North, East, and North-East blocked
	// West and South clear.
	else if (%north		&&
			 %east		&&
			 //%ne		&&
			 !%west		&&
			 !%south)
	{
		%frame = 45;//"OuterCorner3ImageMap";
	}
	// North, West, and North-West blocked
	// East and South clear.
	else if (%north		&&
			 %west		&&
			 %nw		&&
			 !%east		&&
			 !%south)
	{
		%frame = 46;//"OuterCorner4ImageMap";
	}
	
	
	/*
	 * "Corner Edge" Tiles.
	 */
	// North, West, and East blocked.
	// North-West and South clear.
	else if (%north		&&
			 %west		&&
			 %east		&&
			 !%nw		&&
			 !%south)
	{
		%frame = 15;//"CornerEdgeNWSImageMap";
		return %frame;
	}
	// North, West, and East blocked.
	// North-East and South clear.
	else if (%north		&&
			 %west		&&
			 %east		&&
			 !%ne		&&
			 !%south)
	{
		%frame = 14;//"CornerEdgeNESImageMap";
		return %frame;
	}
	
	/*
	 * South edge wall tile!!!
	 */
	// West, East, South, South-West, and South-East blocked.
	// North clear.
	/*else if (%west		&&
			 %east		&&
			 %south		&&
			 %sw		&&
			 %se		&&
			 !%north)
	{//echo("70!");
		%frame = 70;
		return %frame;
	}*/
	
	
	/*
	 * "Corner Wall" Tiles.
	 */
	// West, East, South, and South-West blocked.
	// North and South-East clear.
	else if (%west		&&
			 %east		&&
			 %south		&&
			 %sw		&&
			 !%north	&&
			 !%se)
	{//echo("18!");
	//echo(%north SPC %south SPC %east SPC %west);
	//echo(%sw SPC %se);
		%frame = 18;//"CornerWallSENImageMap";
		return %frame;
	}
	// West, East, South, and South-East blocked.
	// North and South-West clear.
	else if (%west		&&
			 %east		&&
			 %south		&&
			 %se		&&
			 !%north	&&
			 !%sw)
	{
		%frame = 21;//"CornerWallSWNImageMap";
		return %frame;
	}
	
	
	
	/*
	 * "Horizontal Sandwich" wall tile.
	 */
	// West and East blocked.
	// North and South clear.
	else if (%west		&&
			 %east		&&
			 !%north	&&
			 !%south)
	{
		%frame = 77;//"WallThin4ImageMap";
	}
	
	
	/*
	 * "Wall Corner" tiles.
	 */
	// North, South, and West blocked.
	// South-West and East clear.
	else if (%north		&&
			 %south		&&
			 %west		&&
			 !%sw		&&
			 !%east)
	{
		%frame = 20;//"CornerWallSWEImageMap";
	}
	// North, South, and East blocked.
	// South-East and West clear.
	else if (%north		&&
			 %south		&&
			 %east		&&
			 !%se		&&
			 !%west)
	{
		%frame = 19;//"CornerWallSEWImageMap";
	}
	// North, South, and West blocked.
	// North-West and East clear.
	else if (%north		&&
			 %south		&&
			 %west		&&
			 !%nw		&&
			 !%east)
	{
		%frame = 17;//"CornerWallNWEImageMap";
	}
	// North, South, and East blocked.
	// North-East and West clear.
	else if (%north		&&
			 %south		&&
			 %east		&&
			 !%ne		&&
			 !%west)
	{
		%frame = 16;//"CornerWallNEWImageMap";
	}
	// East and South blocked.
	// South-East clear.
	else if (%east		&&
			 %south		&&
			 !%se)
	{
		//%frame = 41;//"InnerCorner4ImageMap";
		//%frame = 65;//"TWallThick4ImageMap";
		if (!%sw	&&
			!%north)
		{
			%frame = 69;//"WallThick1ImageMap";
		}
		else if (!%sw)
		{
			%frame = 53;//"SouthProtrudingCenterImageMap";
		}
		else if (%ne)
		{
			%frame = 41;//"InnerCorner4ImageMap";
		}
		else
		{
			%frame = 64;//"TWallThick3ImageMap";
		}
	}
	// West and South blocked.
	// South-West clear.
	else if (%west		&&
			 %south		&&
			 !%sw)
	{
		%frame = 40;//"InnerCorner3ImageMap";
	}
	// North and East blocked.
	// North-East clear.
	else if (%north		&&
			 %east		&&
			 !%ne)
	{
		%frame = 39;//"InnerCorner2ImageMap";
	}
	// North and West blocked.
	// North-West clear.
	else if (%north		&&
			 %west		&&
			 !%nw)
	{
		%frame = 38;//"InnerCorner1ImageMap";
	}
	
	
	/*
	 * "Corner Bits" tiles.
	 */
	// North, South, East, and West blocked.
	// North-West, North-East, South-West, and South-East clear.
	else if (%north	&&
		%south	&&
		%east	&&
		%west	&&
		!%nw	&&
		!%ne	&&
		!%sw	&&
		!%se)
	{
		%frame = 34;//"FourCornerBitsImageMap";
	}
	
	// North, South, East, and West blocked.
	// North-West, North-East, and South-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%ne	&&
			 !%sw)
	{
		%frame = 7;//"CornerBitsNWNESWImageMap";
	}
	// North, South, East, and West blocked.
	// North-West, North-East, and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%ne	&&
			 !%se)
	{
		%frame = 6;//"CornerBitsNWNESEImageMap";
	}
	// North, South, East, and West blocked.
	// North-East, South-West, and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%ne	&&
			 !%sw	&&
			 !%se)
	{
		%frame = 3;//"CornerBitsNESWSEImageMap";
	}
	// North, South, East, and West blocked.
	// North-West, South-West, and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%sw	&&
			 !%se)
	{
		%frame = 10;//"CornerBitsNWSWSEImageMap";
	}
	
	// North, South, East, and West blocked.
	// North-West and North-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%ne)
	{
		%frame = 5;//"CornerBitsNWNEImageMap";
	}
	// North, South, East, and West blocked.
	// North-East and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%ne	&&
			 !%se)
	{
		%frame = 1;//"CornerBitsNESEImageMap";
	}
	// North, South, East, and West blocked.
	// South-West and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%sw	&&
			 !%se)
	{
		%frame = 13;//"CornerBitsSWSEImageMap";
	}
	// North, South, East, and West blocked.
	// North-West and South-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%sw)
	{
		%frame = 9;//"CornerBitsNWSWImageMap";
	}
	
	// North, South, East, and West blocked.
	// North-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw)
	{
		%frame = 4;//"CornerBitsNWImageMap";
	}
	// North, South, East, and West blocked.
	// North-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%ne)
	{
		%frame = 0;//"CornerBitsNEImageMap";
	}
	// North, South, East, and West blocked.
	// South-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%sw)
	{
		%frame = 12;//"CornerBitsSWImageMap";
	}
	// North, South, East, and West blocked.
	// South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%se)
	{
		%frame = 11;//"CornerBitsSEImageMap";
	}
	
	
	/*
	 * "Corner Wall" tiles.
	 */
	// North and West blocked.
	// South and East clear.
	else if (%north	&&
		%west	&&
		!%south	&&
		!%east)
	{
		%frame = 38;//"HardOuterCorner4ImageMap";
	}
	// North and East blocked.
	// South and West clear.
	else if (%north		&&
			 %east		&&
			 !%south	&&
			 !%west)
	{
		%frame = 37;//"HardOuterCorner3ImageMap";
	}
	// South and West blocked.
	// North and East clear.
	else if (%south		&&
			 %west		&&
			 !%north	&&
			 !%east)
	{
		%frame = 35;//"HardOuterCorner1ImageMap";
	}
	// South and East blocked.
	// North and West clear.
	else if (%south		&&
			 %east		&&
			 !%north	&&
			 !%west)
	{
		%frame = 36;//"HardOuterCorner2ImageMap";
	}
	
	
	/*
	 * "Double Sided Wall" tiles.
	 */
	// North and South blocked.
	// West and East clear.
	else if (%north	&&
			 %south	&&
			 !%west	&&
			 !%east)
	{
		%frame = 75;//"WallThin2ImageMap";
	}
	
	
	/*
	 *	 "Even" NSEW edges. No fancy corners or such!
	 */
	// "North" Wall.
	// North, West, and East blocked.
	// South clear.
	else if (%north	&&
			 %west	&&
			 %east	&&
			 !%south)
	{
		%frame = 73;//"WallThick4ImageMap";
		
		switch (getRandom(0, 5))
		{
			case 0: %frame = 73;
			case 1: %frame = 54;
			case 2: %frame = 55;
			case 3: %frame = 56;
			case 4: %frame = 57;
			case 5: %frame = 51;
		}
	}
	// "South" Wall.
	// South, West, and East blocked.
	// North Clear.
	else if (%south &&
			 %west  &&
			 %east	&&
			 !%north)
	{
		%frame = 70;//"WallThick1ImageMap";
	}
	// "West" Wall.
	// North and South blocked.
	// East clear.
	else if (%north	&&
			 %south	&&
			 !%east)
	{
		%frame = 72;//"WallThick3ImageMap";
	}
	// "East" Wall.
	// North and South blocked.
	// West clear.
	else if (%north	&&
			 %south	&&
			 !%west)
	{
		%frame = 71;//"WallThick2ImageMap";
	}
	
	
	/*
	 * "Odd" Walls that hang out.
	 */
	// "South Hanging" Wall.
	// North blocked.
	// West, East, and South clear.
	else if (%north	&&
		!%west	&&
		!%east	&&
		!%south)
	{
		%frame = 29;//"EndThin4ImageMap";
	}
	// "North Hanging" Wall.
	// South blocked.
	// North, West, and East clear.
	else if (%south	&&
			 !%west	&&
			 !%east	&&
			 !%north)
	{
		%frame = 26;//"EndThin1ImageMap";
	}
	// "West Hanging" Wall.
	// East blocked.
	// North, West, and South clear.
	else if (%east	&&
			 !%north	&&
			 !%west	&&
			 !%south)
	{
		%frame = 27;//"EndThin2ImageMap";
	}
	// "East Hanging" Wall.
	// West blocked.
	// North, East, and South clear.
	else if (%west	&&
			 !%north	&&
			 !%east	&&
			 !%south)
	{
		%frame = 28;//"EndThin3ImageMap";
	}
	
	
	// Return frame index.
	return %frame;
}


// Returns the source rect for the specified tile.
function getTileSourceRect (%imagemap)
{//echo("getTileSourceRect()");
	switch$ (%imagemap)
	{
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	// 10
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	// 20
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	//	30
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	// 40
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	// 50
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	// 60
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	// 70
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";
		case "WallThick1ImageMap":	return "0 0 0 0";	// 77		
	}
}

// Returns a proper wall status code depending on the status of adjacent wall tiles.
// NOTE: All parameters are binary. True is blocked, false is clear.
function getWallStatusCode (%north, %south, %east, %west,
							%nw, %ne, %sw, %se,
							%ss, %ssw, %sse)		// Special case for protruding south wall!
{//echo("getWallStatusCode()");
	// "Default" imagemap.
	%imageMap = "solidTileImageMap";//"Water_Tile_32x32ImageMap";	// Change this!
	
	
	
	/*
	 * SPECIAL CASE: Bottom-most edge of map.
	 */
	// West, East, South, South-West, and South-East blocked.
	// North, North-West. amd North-East clear.
	if (	 %west		&&
			 %east		&&
			 %south		&&
			 %sw		&&
			 %se		&&
			 !%north)//	&&
			 //!%nw		&&
			 //!%ne)
	{
		%imageMap = "WallThick1ImageMap";
	}
	/*
	 * SPECIAL CASE: Tile North of "South Protruding Wall".
	 */
	// West and East blocked.
	// North and South clear.
	if (	%north		&&
			 %west		&&
			 %east		&&
			 %south		&&
			 !%sw		&&
			 !%se		&&
			 !%ss)
	{
		%imageMap = "SouthProtrudingCenterImageMap";
		return %imageMap;
	}
	/*
	 * SPECIAL CASE: SE rounded corner placement.
	 */
	// North and West blocked.
	// South and East clear.
	if (	%north		&&
			 %west		&&
			 !%south	&&
			 !%east)
	{
		%imageMap = "OuterCorner4ImageMap";
		return %imageMap;
	}
	/*
	 * SPECIAL CASE: Island Wall tile.
	 */
	// North, South, East, and West clear.
	if (	!%north		&&
			 !%south	&&
			 !%east		&&
			 !%west)
	{
		%imageMap = "IslandWallImageMap";
		return %imageMap;
	}
	/*
	 * SPECIAL CASE: North-West Outder Corner and South-East Inner Corner.
	 */
	// East and South blocked.
	// North, West, South-East clear.
	if (	 %east		&&
			 %south		&&
			 !%north	&&
			 !%west		&&
			 !%se)
	{
		%imageMap = "OuterNWInnerSEImageMap";
		return %imageMap;
	}
	/*
	 * SPECIAL CASE: North-East Outder Corner and South-West Inner Corner.
	 */
	// West and South blocked.
	// North, East, and South-West clear.
	if (	 %west		&&
			 %south		&&
			 !%north	&&
			 !%east		&&
			 !%sw)
	{
		%imageMap = "OuterNEInnerSWImageMap";
		return %imageMap;
	}
	
	
	
	
	/*
	 * "Rounded Corner" tiles.
	 */
	// East, South, and South-East blocked
	// North and West clear.
	else if (%east		&&
			 %south		&&
			 %se		&&
			 !%north	&&
			 !%west)
	{
		%imageMap = "OuterCorner2ImageMap";
	}
	// West, South, and South-East blocked
	// North and East clear.
	else if (%west		&&
			 %south		&&
			 //%se		&&
			 !%north	&&
			 !%east)
	{
		%imageMap = "OuterCorner1ImageMap";
	}
	// North, East, and North-East blocked
	// West and South clear.
	else if (%north		&&
			 %east		&&
			 //%ne		&&
			 !%west		&&
			 !%south)
	{
		%imageMap = "OuterCorner3ImageMap";
	}
	// North, West, and North-West blocked
	// East and South clear.
	else if (%north		&&
			 %west		&&
			 %nw		&&
			 !%east		&&
			 !%south)
	{
		%imageMap = "OuterCorner4ImageMap";
	}
	
	
	/*
	 * "Corner Edge" Tiles.
	 */
	// North, West, and East blocked.
	// North-West and South clear.
	else if (%north		&&
			 %west		&&
			 %east		&&
			 !%nw		&&
			 !%south)
	{
		%imageMap = "CornerEdgeNWSImageMap";
		return %imageMap;
	}
	// North, West, and East blocked.
	// North-East and South clear.
	else if (%north		&&
			 %west		&&
			 %east		&&
			 !%ne		&&
			 !%south)
	{
		%imageMap = "CornerEdgeNESImageMap";
		return %imageMap;
	}
	
	
	/*
	 * "Corner Wall" Tiles.
	 */
	// West, East, South, and South-West blocked.
	// North and South-East clear.
	else if (%west		&&
			 %east		&&
			 %south		&&
			 %sw		&&
			 !%north	&&
			 !%se)
	{
		%imageMap = "CornerWallSENImageMap";
		return %imageMap;
	}
	// West, East, South, and South-East blocked.
	// North and South-West clear.
	else if (%west		&&
			 %east		&&
			 %south		&&
			 %se		&&
			 !%north	&&
			 !%sw)
	{
		%imageMap = "CornerWallSWNImageMap";
		return %imageMap;
	}
	
	
	
	/*
	 * "Horizontal Sandwich" wall tile.
	 */
	// West and East blocked.
	// North and South clear.
	else if (%west		&&
			 %east		&&
			 !%north	&&
			 !%south)
	{
		%imageMap = "WallThin4ImageMap";
	}
	
	
	/*
	 * "Wall Corner" tiles.
	 */
	// North, South, and West blocked.
	// South-West and East clear.
	else if (%north		&&
			 %south		&&
			 %west		&&
			 !%sw		&&
			 !%east)
	{
		%imageMap = "CornerWallSWEImageMap";
	}
	// North, South, and East blocked.
	// South-East and West clear.
	else if (%north		&&
			 %south		&&
			 %east		&&
			 !%se		&&
			 !%west)
	{
		%imageMap = "CornerWallSEWImageMap";
	}
	// North, South, and West blocked.
	// North-West and East clear.
	else if (%north		&&
			 %south		&&
			 %west		&&
			 !%nw		&&
			 !%east)
	{
		%imageMap = "CornerWallNWEImageMap";
	}
	// North, South, and East blocked.
	// North-East and West clear.
	else if (%north		&&
			 %south		&&
			 %east		&&
			 !%ne		&&
			 !%west)
	{
		%imageMap = "CornerWallNEWImageMap";
	}
	// East and South blocked.
	// South-East clear.
	else if (%east		&&
			 %south		&&
			 !%se)
	{
		%imageMap = "InnerCorner4ImageMap";
	}
	// West and South blocked.
	// South-West clear.
	else if (%west		&&
			 %south		&&
			 !%sw)
	{
		%imageMap = "InnerCorner3ImageMap";
	}
	// North and East blocked.
	// North-East clear.
	else if (%north		&&
			 %east		&&
			 !%ne)
	{
		%imageMap = "InnerCorner2ImageMap";
	}
	// North and West blocked.
	// North-West clear.
	else if (%north		&&
			 %west		&&
			 !%nw)
	{
		%imageMap = "InnerCorner1ImageMap";
	}
	
	
	/*
	 * "Corner Bits" tiles.
	 */
	// North, South, East, and West blocked.
	// North-West, North-East, South-West, and South-East clear.
	else if (%north	&&
		%south	&&
		%east	&&
		%west	&&
		!%nw	&&
		!%ne	&&
		!%sw	&&
		!%se)
	{
		%imageMap = "FourCornerBitsImageMap";
	}
	
	// North, South, East, and West blocked.
	// North-West, North-East, and South-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%ne	&&
			 !%sw)
	{
		%imageMap = "CornerBitsNWNESWImageMap";
	}
	// North, South, East, and West blocked.
	// North-West, North-East, and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%ne	&&
			 !%se)
	{
		%imageMap = "CornerBitsNWNESEImageMap";
	}
	// North, South, East, and West blocked.
	// North-East, South-West, and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%ne	&&
			 !%sw	&&
			 !%se)
	{
		%imageMap = "CornerBitsNESWSEImageMap";
	}
	// North, South, East, and West blocked.
	// North-West, South-West, and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%sw	&&
			 !%se)
	{
		%imageMap = "CornerBitsNWSWSEImageMap";
	}
	
	// North, South, East, and West blocked.
	// North-West and North-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%ne)
	{
		%imageMap = "CornerBitsNWNEImageMap";
	}
	// North, South, East, and West blocked.
	// North-East and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%ne	&&
			 !%se)
	{
		%imageMap = "CornerBitsNESEImageMap";
	}
	// North, South, East, and West blocked.
	// South-West and South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%sw	&&
			 !%se)
	{
		%imageMap = "CornerBitsSWSEImageMap";
	}
	// North, South, East, and West blocked.
	// North-West and South-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw	&&
			 !%sw)
	{
		%imageMap = "CornerBitsNWSWImageMap";
	}
	
	// North, South, East, and West blocked.
	// North-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%nw)
	{
		%imageMap = "CornerBitsNWImageMap";
	}
	// North, South, East, and West blocked.
	// North-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%ne)
	{
		%imageMap = "CornerBitsNEImageMap";
	}
	// North, South, East, and West blocked.
	// South-West clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%sw)
	{
		%imageMap = "CornerBitsSWImageMap";
	}
	// North, South, East, and West blocked.
	// South-East clear.
	else if (%north	&&
			 %south	&&
			 %east	&&
			 %west	&&
			 !%se)
	{
		%imageMap = "CornerBitsSEImageMap";
	}
	
	
	/*
	 * "Corner Wall" tiles.
	 */
	// North and West blocked.
	// South and East clear.
	else if (%north	&&
		%west	&&
		!%south	&&
		!%east)
	{
		%imageMap = "HardOuterCorner4ImageMap";
	}
	// North and East blocked.
	// South and West clear.
	else if (%north		&&
			 %east		&&
			 !%south	&&
			 !%west)
	{
		%imageMap = "HardOuterCorner3ImageMap";
	}
	// South and West blocked.
	// North and East clear.
	else if (%south		&&
			 %west		&&
			 !%north	&&
			 !%east)
	{
		%imageMap = "HardOuterCorner1ImageMap";
	}
	// South and East blocked.
	// North and West clear.
	else if (%south		&&
			 %east		&&
			 !%north	&&
			 !%west)
	{
		%imageMap = "HardOuterCorner2ImageMap";
	}
	
	
	/*
	 * "Double Sided Wall" tiles.
	 */
	// North and South blocked.
	// West and East clear.
	else if (%north	&&
			 %south	&&
			 !%west	&&
			 !%east)
	{
		%imageMap = "WallThin2ImageMap";
	}
	
	
	/*
	 *	 "Even" NSEW edges. No fancy corners or such!
	 */
	// "North" Wall.
	// North, West, and East blocked.
	// South clear.
	else if (%north	&&
			 %west	&&
			 %east	&&
			 !%south)
	{
		%imageMap = "WallThick4ImageMap";
	}
	// "South" Wall.
	// South, West, and East blocked.
	// North Clear.
	else if (%south &&
			 %west  &&
			 %east	&&
			 !%north)
	{
		%imageMap = "WallThick1ImageMap";
	}
	// "West" Wall.
	// North and South blocked.
	// East clear.
	else if (%north	&&
			 %south	&&
			 !%east)
	{
		%imageMap = "WallThick3ImageMap";
	}
	// "East" Wall.
	// North and South blocked.
	// West clear.
	else if (%north	&&
			 %south	&&
			 !%west)
	{
		%imageMap = "WallThick2ImageMap";
	}
	
	
	/*
	 * "Odd" Walls that hang out.
	 */
	// "South Hanging" Wall.
	// North blocked.
	// West, East, and South clear.
	else if (%north	&&
		!%west	&&
		!%east	&&
		!%south)
	{
		%imageMap = "EndThin4ImageMap";
	}
	// "North Hanging" Wall.
	// South blocked.
	// North, West, and East clear.
	else if (%south	&&
			 !%west	&&
			 !%east	&&
			 !%north)
	{
		%imageMap = "EndThin1ImageMap";
	}
	// "West Hanging" Wall.
	// East blocked.
	// North, West, and South clear.
	else if (%east	&&
			 !%north	&&
			 !%west	&&
			 !%south)
	{
		%imageMap = "EndThin2ImageMap";
	}
	// "East Hanging" Wall.
	// West blocked.
	// North, East, and South clear.
	else if (%west	&&
			 !%north	&&
			 !%east	&&
			 !%south)
	{
		%imageMap = "EndThin3ImageMap";
	}
	
	return %imageMap;
}