package codemallet.spark.map;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.Inflater;

import codemallet.spark.Spark;
import codemallet.spark.collision.GameCollision;
import codemallet.spark.engines.GameResources;

public final class Map
{
	private final Spark game;

	// Map File
	public final String MapFile;

	// Map
	public final MapStructure GameMap;

	public final MapAnimation[] anim_list;

	// Map Tiles
	public final ArrayList<Flag> Flags = new ArrayList<Flag>();

	public final ArrayList<FlagPole> FlagPoles = new ArrayList<FlagPole>();

	public final ArrayList<Switch> Switches = new ArrayList<Switch>();

	// Precalculated Variables
	public int EndX; // How far to go when drawing the map (x)

	public int EndY; // How far to go when drawing the map (y)

	public int MapWidthPixels;

	public int MapHeightPixels;

	public int FarPlaneMoveSizeX;

	public int FarPlaneMoveSizeY;

	public Map(Spark game, String MapName)
	{
		this.game = game;
		this.MapFile = MapName;

		GameMap = new MapStructure();
		anim_list = new MapAnimation[256];

		LoadMap();
		LoadAnimations();
		LoadAttributes();

		game.gameMap = this;
		game.Optimize();
		game.gameWeapons.CalculateDamage();
	}

	public final void Cycle()
	{
		for (int i = 0; i < 256; i++)
		{
			if (game.Tick > anim_list[i].FrameTick)
			// Animation frames need to be updated
			{
				anim_list[i].Frame++;
				if (anim_list[i].Frame >= anim_list[i].frameCount)
					anim_list[i].Frame = 0;
				anim_list[i].FrameTick = game.Tick + anim_list[i].speed * 13;
			}
		}
	}

	public final void RestoreMapTiles()
	{
		for (int i = 0; i < game.gameMap.FlagPoles.size(); i++)
		{
			FlagPole flagpole = game.gameMap.FlagPoles.get(i);
			flagpole.hasFlag = false;

			GameMap.MapData[(flagpole.Y * GameMap.width) + flagpole.X] = flagpole.DefaultTile;
		}

		for (int i = 0; i < game.gameMap.Switches.size(); i++)
		{
			Switch Switch = game.gameMap.Switches.get(i);
			Switch.Team = -1;

			GameMap.MapData[(Switch.Y * GameMap.width) + Switch.X] = Switch.DefaultTile;
		}

		for (int i = 0; i < game.gameMap.Flags.size(); i++)
		{
			Flag flag = game.gameMap.Flags.get(i);

			flag.onGround = false;
			flag.onPlayer = false;
			flag.onPole = true;
		}
	}

	private final void LoadAnimations()
	{
		BufferedInputStream in;
		try
		{
			in = new BufferedInputStream(new FileInputStream(
					"spark/images/anims.dat"));
		}
		catch (Exception e)
		{
			System.err.println("Anims.dat not found!");
			return;
		}

		try
		{
			for (int aLoop = 0; aLoop < 256; aLoop++)
			{
				anim_list[aLoop] = new MapAnimation();
				anim_list[aLoop].frameCount = in.read();
				anim_list[aLoop].speed = (char) in.read();
				if (anim_list[aLoop].speed == 0)
					anim_list[aLoop].speed = 255;
				if (anim_list[aLoop].frameCount == 0)
					anim_list[aLoop].frameCount = 1;

				for (int i = 0; i < 32; i++)
				{
					int Char1;
					int Char2;
					Char1 = (in.read() & 0xff);
					Char2 = (in.read() & 0xff);
					int Result = Char2 * 256 + Char1;
					anim_list[aLoop].frames[i] = Result;

				}
			}
		}
		catch (IOException e)
		{
			System.err.println("Error reading animations");
		}
	}

	private final void LoadMap()
	{
		try
		{
			BufferedInputStream in;
			try
			{
				in = new BufferedInputStream(new FileInputStream("spark/maps/"
						+ MapFile));
			}
			catch (Exception e)
			{
				System.err.println("Map " + MapFile + " not found!");
				game.CloseMessage = "Map " + MapFile + " not found!";
				game.destroy();
				return;
			}

			char MapBuffer[] = new char[23];
			for (int i = 0; i < 23; i++)
			{
				MapBuffer[i] = (char) in.read();
			}

			GameMap.id = (MapBuffer[1] * 256 + MapBuffer[0]);
			GameMap.header = (MapBuffer[3] * 256 + MapBuffer[2]);
			GameMap.version = MapBuffer[4];
			GameMap.width = (MapBuffer[6] * 256 + MapBuffer[5]);
			GameMap.height = (MapBuffer[8] * 256 + MapBuffer[7]);
			GameMap.maxPlayers = MapBuffer[9];
			GameMap.holdingTime = MapBuffer[10];
			GameMap.numTeams = MapBuffer[11];
			GameMap.objective = MapBuffer[12];
			GameMap.laserDamage = MapBuffer[13];
			GameMap.specialDamage = MapBuffer[14];
			GameMap.rechargeRate = MapBuffer[15];
			GameMap.misslesEnabled = MapBuffer[16];
			GameMap.bombsEnabled = MapBuffer[17];
			GameMap.bounciesEnabled = MapBuffer[18];
			GameMap.powerupCount = (MapBuffer[20] * 256 + MapBuffer[19]);
			GameMap.maxSimulPowerups = MapBuffer[21];
			GameMap.switchCount = MapBuffer[22];

			for (int i = 0; i < GameMap.numTeams; i++)
			{
				GameMap.flagCount[i] = (char) in.read();
			}
			for (int i = 0; i < GameMap.numTeams; i++)
			{
				GameMap.flagPoleCount[i] = (char) in.read();
			}

			for (int i = 0; i < GameMap.numTeams; i++)
			{
				if (GameMap.flagPoleCount[i] > 0)
				{
					for (int j = 0; j < GameMap.flagPoleCount[i]; j++)
					{
						GameMap.flagPoleData[i] = (char) in.read();
					}
				}
			}

			char Length1 = (char) in.read();
			char Length2 = (char) in.read();
			GameMap.nameLength = (Length2 * 256 + Length1);

			if (GameMap.nameLength > 0)
			{
				GameMap.name = "";
				for (int i = 0; i < GameMap.nameLength; i++)
				{
					GameMap.name += (char) in.read();
				}
			}

			System.err.println("Map Name: " + GameMap.name);

			Length1 = (char) in.read();
			Length2 = (char) in.read();
			GameMap.descriptionLength = (Length2 * 256 + Length1);

			if (GameMap.descriptionLength > 0)
			{
				GameMap.description = "";
				for (int i = 0; i < GameMap.descriptionLength; i++)
				{
					GameMap.description += (char) in.read();
				}
			}

			System.err.println("Map Description: " + GameMap.description);

			GameMap.neutralCount = (char) in.read();

			int TotalSize = GameMap.width * GameMap.height * 2;
			if (GameMap.version == 3) // Compressed
			{
				byte[] Buffer = new byte[TotalSize];

				int i = 0;
				while (true)
				{
					try
					{
						if (i == TotalSize)
							break;
						byte read = (byte) (in.read());
						Buffer[i] = read;
						i++;
					}
					catch (IOException e)
					{
						game.throwException(e);
						break;
					}
				}

				Inflater decompresser = new Inflater();
				byte[] NewBuffer = new byte[TotalSize];
				decompresser.setInput(Buffer, 0, i);
				try
				{
					decompresser.inflate(NewBuffer);

				}
				catch (Exception e)
				{
					System.err.println("Error decompressing map");
					game.throwException(e);
				}

				decompresser.end();

				GameMap.MapData = new int[TotalSize / 2];
				for (int x = 0; x < (TotalSize / 2); x++)
				{
					int V = x * 2;
					int Result = ((NewBuffer[V + 1] & 0xff) * 256 + (NewBuffer[V] & 0xff));
					GameMap.MapData[x] = Result;
				}
			}
			
			in.close();
		}
		catch (IOException e)
		{
			System.err.println("Error reading map");
		}
	}

	private final void drawFarplane(final int StartX, final int StartY,
			final int drawEndX, final int drawEndY)
	{
		final int FarPlaneX = (int) ((game.gamePlayer.X / MapWidthPixels) * FarPlaneMoveSizeX);
		final int FarPlaneY = (int) ((game.gamePlayer.Y / MapHeightPixels) * FarPlaneMoveSizeY);

		if (game.gameOptions.displayDisableFarplane == false)
		{
			game.gameEngine.beginUse(GameResources.imgFarplane);
		}
		else
		{
			game.gameEngine.setColor(GameResources.colorBlack);
		}

		for (int x = StartX; x < drawEndX; x++)
		{

			final int DrawX = (x * 16)
					- (int) (game.gamePlayer.X - game.gamePlayer.OffsetX);
			final int DrawX2 = DrawX + 16;
			final int srcx = FarPlaneX + DrawX;
			final int srcx2 = srcx + 16;

			for (int y = StartY; y < drawEndY; y++)
			{
				if (x >= 0 && y >= 0 && x < 256 && y < 256)
				{
					int tile = GameMap.MapData[(y * GameMap.width) + x];
					if ((tile & 0x8000) != 0)
					{
						tile = (char) ((tile & 0x00FF));
						if (GameCollision.farPlaneOverrideAnim(tile) == true)
						{
							final int DrawY = (y * 16)
									- (int) (game.gamePlayer.Y - game.gamePlayer.OffsetY);
							final int DrawY2 = DrawY + 16;
							if (game.gameOptions.displayDisableFarplane == false)
							{
								final int srcy = FarPlaneY + DrawY;
								final int srcy2 = srcy + 16;
								game.gameEngine.drawInUse(DrawX, DrawY, DrawX2,
										DrawY2, srcx, srcy, srcx2, srcy2);
							}
							else
							{
								game.gameEngine.fillRect(DrawX, DrawY, 16, 16);
							}
						}
					}
					else
					{
						if (tile < GameCollision.MaxTile
								&& GameCollision.farPlaneOverride[tile] == true)
						{
							final int DrawY = (y * 16)
									- (int) (game.gamePlayer.Y - game.gamePlayer.OffsetY);
							final int DrawY2 = DrawY + 16;
							if (game.gameOptions.displayDisableFarplane == false)
							{
								final int srcy = FarPlaneY + DrawY;
								final int srcy2 = srcy + 16;
								game.gameEngine.drawInUse(DrawX, DrawY, DrawX2,
										DrawY2, srcx, srcy, srcx2, srcy2);
							}
							else
							{
								game.gameEngine.fillRect(DrawX, DrawY, 16, 16);
							}
						}
					}
				}
				else
				{
					final int DrawY = (y * 16)
							- (int) (game.gamePlayer.Y - game.gamePlayer.OffsetY);
					final int DrawY2 = DrawY + 16;
					if (game.gameOptions.displayDisableFarplane == false)
					{
						final int srcy = FarPlaneY + DrawY;
						final int srcy2 = srcy + 16;
						game.gameEngine.drawInUse(DrawX, DrawY, DrawX2, DrawY2,
								srcx, srcy, srcx2, srcy2);
					}
					else
					{
						game.gameEngine.fillRect(DrawX, DrawY, 16, 16);
					}
				}
			}
		}

		if (game.gameOptions.displayDisableFarplane == false)
			game.gameEngine.endUse();
	}

	private final void drawFlags()
	{
		final int FlagSize = game.gameMap.Flags.size();
		for (int i = 0; i < FlagSize; i++)
		{
			final Flag flag = game.gameMap.Flags.get(i);
			if (flag.onGround == true)
			{
				final int FlagStartX = (int) (flag.X - game.gamePlayer.X + game.gamePlayer.OffsetX) - 8;
				final int FlagStartY = (int) (flag.Y - game.gamePlayer.Y + game.gamePlayer.OffsetY) - 8;

				if (flag.timeTotalBlink > game.Tick)
				{
					if (game.Tick > flag.timeBlink)
					{
						flag.timeBlink = game.Tick + 100;
						flag.Blink = (1 - flag.Blink);
					}

					if (flag.Blink == 1)
					{
						game.gameEngine.drawImage(GameResources.imgTuna,
								FlagStartX, FlagStartY, FlagStartX + 16,
								FlagStartY + 16, 288, 311 + flag.Team * 32,
								288 + 16, 311 + flag.Team * 32 + 16);
					}
				}
				else
				{
					game.gameEngine.drawImage(GameResources.imgTuna,
							FlagStartX, FlagStartY, FlagStartX + 16,
							FlagStartY + 16, 288, 311 + flag.Team * 32,
							288 + 16, 311 + flag.Team * 32 + 16);
				}
			}
		}
	}

	private final void drawTiles(final int StartX, final int StartY,
			final int drawEndX, final int drawEndY)
	{
		game.gameEngine.beginUse(GameResources.imgTiles);

		for (int x = StartX; x < drawEndX; x++)
		{
			final int DrawX = (x * 16)
					- (int) (game.gamePlayer.X - game.gamePlayer.OffsetX);
			final int DrawX2 = DrawX + 16;

			for (int y = StartY; y < drawEndY; y++)
			{
				if (x >= 0 && y >= 0 && x < 256 && y < 256)
				{
					int tile = GameMap.MapData[(y * GameMap.width) + x];

					if ((tile & 0x8000) == 0) // Test whether tile is an
					// animation
					{
						if (tile < GameCollision.MaxTile
								&& GameCollision.doNotDrawTile[tile] == false)
						{
							final int DrawY = (y * 16)
									- (int) (game.gamePlayer.Y - game.gamePlayer.OffsetY);
							final int DrawY2 = DrawY + 16;
							game.gameEngine.drawInUse(DrawX, DrawY, DrawX2,
									DrawY2, GameCollision.TileSourceX[tile],
									GameCollision.TileSourceY[tile],
									GameCollision.TileSourceX[tile] + 16,
									GameCollision.TileSourceY[tile] + 16);
						}
					}
					else
					// There is an animated tile here
					{
						final int offset = ((tile & 0x7F00) >> 8);
						final char idx = (char) ((tile & 0x00FF));

						final int frame = (anim_list[idx].Frame + offset)
								% anim_list[idx].frameCount;
						tile = anim_list[idx].frames[frame];

						final int DrawY = (y * 16)
								- (int) (game.gamePlayer.Y - game.gamePlayer.OffsetY);
						final int DrawY2 = DrawY + 16;

						game.gameEngine.drawInUse(DrawX, DrawY, DrawX2, DrawY2,
								GameCollision.TileSourceX[tile],
								GameCollision.TileSourceY[tile],
								GameCollision.TileSourceX[tile] + 16,
								GameCollision.TileSourceY[tile] + 16);
					}
				}
			}
		}

		game.gameEngine.endUse();
	}

	public final void drawMap()
	{
		final int StartX = ((int) (game.gamePlayer.X - game.gamePlayer.OffsetX) / 16);
		final int StartY = ((int) (game.gamePlayer.Y - game.gamePlayer.OffsetY) / 16);

		final int drawEndX = StartX + EndX;
		final int drawEndY = StartY + EndY;

		drawFarplane(StartX, StartY, drawEndX, drawEndY);
		drawTiles(StartX, StartY, drawEndX, drawEndY);
		drawFlags();
	}

	public final int getEmptyTile(int Team, int Type)
	{
		switch (Team)
		{
			case 0:
				switch (Type)
				{
					case 0:
						return 32792;
					case 1:
						return 32793;
					case 2:
						return 32794;
					case 3:
						return 32795;
					case 4:
						return 32896;
				}
				break;
			case 1:
				switch (Type)
				{
					case 0:
						return 32800;
					case 1:
						return 32801;
					case 2:
						return 32802;
					case 3:
						return 32803;
					case 4:
						return 32897;
				}
				break;
			case 2:
				switch (Type)
				{
					case 0:
						return 32808;
					case 1:
						return 32809;
					case 2:
						return 32810;
					case 3:
						return 32811;
					case 4:
						return 32898;
				}
				break;
			case 3:
				switch (Type)
				{
					case 0:
						return 32826;
					case 1:
						return 32827;
					case 2:
						return 32828;
					case 3:
						return 32829;
					case 4:
						return 32899;
				}
				break;
			case 4:
				return 32904;
		}
		return 0;
	}

	public final int getFlagTile(int Team, int FlagTeam)
	{
		switch (Team)
		{
			case 0:
				switch (FlagTeam)
				{
					case 0:
						return 32796;
					case 1:
						return 32797;
					case 2:
						return 32798;
					case 3:
						return 32799;
					case 4:
						return 32900;
				}
				break;
			case 1:
				switch (FlagTeam)
				{
					case 0:
						return 32804;
					case 1:
						return 32805;
					case 2:
						return 32806;
					case 3:
						return 32807;
					case 4:
						return 32901;
				}
				break;
			case 2:
				switch (FlagTeam)
				{
					case 0:
						return 32812;
					case 1:
						return 32813;
					case 2:
						return 32814;
					case 3:
						return 32815;
					case 4:
						return 32902;
				}
				break;
			case 3:
				switch (FlagTeam)
				{
					case 0:
						return 32830;
					case 1:
						return 32831;
					case 2:
						return 32832;
					case 3:
						return 32833;
					case 4:
						return 32903;
				}
				break;
			case 4:
				return 32908;
		}
		return 0;

	}

	private final void LoadAttributes()
	{
		int FlagCount = 0;
		// Load Spawns, Holding Pens, Flagpoles, Flags
		for (int x = 0; x < 256; x++)
		{
			for (int y = 0; y < 256; y++)
			{
				int tile = GameMap.MapData[(y * GameMap.width) + x];

				if ((tile & 0x8000) != 0)
				{
					char idx = (char) ((tile & 0x00FF));
					switch (idx)
					{
						case 25: // Green Red Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 0;
							NewFlagPole.FlagType = 1;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 26: // Green Blue Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 0;
							NewFlagPole.FlagType = 2;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 27: // Green Yellow Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 0;
							NewFlagPole.FlagType = 3;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 28: // Green Flag
						{
							Flag NewFlag = new Flag();
							NewFlag.Team = 0;
							Flags.add(NewFlag);
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 0;
							NewFlagPole.hasFlag = false;
							NewFlagPole.FlagIndex = FlagCount;
							NewFlagPole.FlagType = 0;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							GameMap.MapData[(y * GameMap.width) + x] = getEmptyTile(
									NewFlag.Team, NewFlagPole.FlagType);
							FlagCount++;

							break;
						}
						case 32: // Red Green Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 1;
							NewFlagPole.FlagType = 0;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 34: // Red Blue Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 1;
							NewFlagPole.FlagType = 2;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 35: // Red Yellow Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 1;
							NewFlagPole.FlagType = 3;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 37: // Red Flag
						{
							Flag NewFlag = new Flag();
							NewFlag.Team = 1;
							Flags.add(NewFlag);
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 1;
							NewFlagPole.hasFlag = false;
							NewFlagPole.FlagIndex = FlagCount;
							NewFlagPole.FlagType = 1;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							GameMap.MapData[(y * GameMap.width) + x] = getEmptyTile(
									NewFlag.Team, NewFlagPole.FlagType);
							FlagCount++;

							break;
						}
						case 40: // Blue Green Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 2;
							NewFlagPole.FlagType = 0;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 41: // Blue Red Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 2;
							NewFlagPole.FlagType = 1;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 43: // Blue Yellow Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 2;
							NewFlagPole.FlagType = 3;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 46: // Blue Flag
						{
							Flag NewFlag = new Flag();
							NewFlag.Team = 2;
							Flags.add(NewFlag);
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 2;
							NewFlagPole.hasFlag = false;
							NewFlagPole.FlagIndex = FlagCount;
							NewFlagPole.FlagType = 2;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							GameMap.MapData[(y * GameMap.width) + x] = getEmptyTile(
									NewFlag.Team, NewFlagPole.FlagType);
							FlagCount++;
							break;
						}
						case 58: // Yellow Green Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 3;
							NewFlagPole.FlagType = 0;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 59: // Yellow Red Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 3;
							NewFlagPole.FlagType = 1;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 60: // Yellow Blue Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 3;
							NewFlagPole.FlagType = 2;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 65: // Yellow Flag
						{
							Flag NewFlag = new Flag();
							NewFlag.Team = 3;
							Flags.add(NewFlag);
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 3;
							NewFlagPole.hasFlag = false;
							NewFlagPole.FlagIndex = FlagCount;
							NewFlagPole.FlagType = 3;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							GameMap.MapData[(y * GameMap.width) + x] = getEmptyTile(
									NewFlag.Team, NewFlagPole.FlagType);
							FlagCount++;
							break;
						}
						case 123: // Switch
						{
							Switch NewSwitch = new Switch();
							NewSwitch.X = x;
							NewSwitch.Y = y;
							NewSwitch.Team = -1;
							NewSwitch.DefaultTile = tile;
							Switches.add(NewSwitch);
							break;
						}
						case 128: // Green Neutral Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 0;
							NewFlagPole.FlagType = 4;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 129: // Red Neutral Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 1;
							NewFlagPole.FlagType = 4;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 130: // Blue Neutral Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 2;
							NewFlagPole.FlagType = 4;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 131: // Yellow Neutral Flag Pole
						{
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 3;
							NewFlagPole.FlagType = 4;
							NewFlagPole.hasFlag = false;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							break;
						}
						case 140: // Neutral Flag
						{
							Flag NewFlag = new Flag();
							NewFlag.Team = 4;
							Flags.add(NewFlag);
							FlagPole NewFlagPole = new FlagPole();
							NewFlagPole.X = x;
							NewFlagPole.Y = y;
							NewFlagPole.Team = 4;
							NewFlagPole.hasFlag = false;
							NewFlagPole.FlagIndex = FlagCount;
							NewFlagPole.FlagType = 4;
							NewFlagPole.DefaultTile = tile;
							FlagPoles.add(NewFlagPole);
							GameMap.MapData[(y * GameMap.width) + x] = getEmptyTile(
									NewFlag.Team, NewFlagPole.FlagType);
							FlagCount++;
							break;
						}
					}
				}
			}
		}
	}
}
