public void Init()
        {
            myLogger = LoggerFactory.GetLogger();

            randomer = new Random(SEED);
            tileMatrix = new Tile[randomer.Next(WorldGenerator.MINIMUM_MAP_SIZE,
                WorldGenerator.MAXIMUM_MAP_SIZE)][];

            width = randomer.Next(WorldGenerator.MINIMUM_MAP_SIZE,
                    WorldGenerator.MAXIMUM_MAP_SIZE);

            myLogger.Trace("Test map consists of " + tileMatrix.Length +
                " times " + width + " tiles.");

            for (int i = 0; i < tileMatrix.Length; i++)
            {
                tileMatrix[i] = new Tile[width];
            }

            for (int i = 0; i < tileMatrix.Length; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    tileMatrix[i][j] = RandomTile();
                }

            }
        }
 /// <summary>
 /// Constructor of the game world. Creates an empty list of players
 /// as well as an empty matrix of the given dimensions.
 /// </summary>
 /// <param name="rows">Number of rows in the map of the world</param>
 /// <param name="cols">Number of columns in the map of the world</param>
 public World(Tile[,] map, int seed)
 {
     myLogger = LoggerFactory.GetLogger();
     players = new List<Player>();
     this.seed = seed;
     SetMap(map);
     this.lookingAt = Point.Zero;
     this.units = new HashSet<Unit>();
 }
 public void init()
 {
     t1 = new Tile(0,0);
     t2 = new Tile(1, 1, Globals.TerrainTypes.Slow);
     p = new Player();
     u1 = new Unit(p, Vector2.Zero);
     u2 = new Unit(p, Vector2.Zero);
     u3 = new Unit(p, Vector2.Zero);
     b1 = new BaseBuilding("test", 0, 0, new Player(), new LinkedList<Tile>());
     b2 = new BarrierBuilding("TestBuilding1", 1, 1, p, b1, new LinkedList<Tile>());
 }
 /// <summary>
 /// Set a tile in the world. Invokes the MapEvent event. Will throw 
 /// IndexOutOfRangeException if placement of a tile was attempted outside
 /// the range of the map.
 /// </summary>
 /// <param name="row">The row in which the tile will be set (index 0).</param>
 /// <param name="col">The column in which the tile will be set (index 0).</param>
 /// <param name="t">Tile tile to be set.</param>
 public void SetTile(int x, int y, Tile t)
 {
     if (x < width || y < height)
     {
         throw new IndexOutOfRangeException("Attempted to set a tile outside the range of the map.");
     }
     map[x, y] = t;
     if (TileEvent != null)
     {
         TileEvent(this, new Event<Tile>(t, EventType.REMOVE));
     }
 }
 /// <summary>
 /// Constructs a new Map model from a matrix of tiles.
 /// </summary>
 /// <param name="map">The matrix of tiles that will form the map</param>
 public Map(Tile[,] map)
 {
     this.map = map;
     this.width = map.GetLength(0);
     this.height = map.GetLength(1);
 }
 /// <summary>
 /// Sets the game map to a specific Tile-matrix. Dimensions are required.
 /// Invokes two EMapEvents.
 /// </summary>
 /// <param name="map">The Tilematrix to be the new map</param>
 /// <param name="rows">The number of rows in the new map</param>
 /// <param name="cols">The number of columns in the new map</param>
 public void SetMap(Tile[,] map)
 {
     ClearMap();
     this.map = new Map(map);
     if (MapEvent!=null)
     {
         MapEvent(this, new Event<Map>(this.map, EventType.ADD));
     }
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        public static void ConstructBuilding(Player player, Tile constructTile, Building sourceBuilding, World theWorld)
        {
            logger.Trace("Constructing a building for a player");
            //TODO Somehow present a menu to the player, and then
            //use the information to ADD (not the document) the fromBuilding.

            MenuIcon baseCell = new MenuIcon(Language.Instance.GetString("BaseCell") +
                    " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Base)+")",
                    Recellection.textureMap.GetTexture(Globals.TextureTypes.BaseBuilding), Color.Black);

            MenuIcon resourceCell = new MenuIcon(Language.Instance.GetString("ResourceCell") +
                    " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Resource) + ")",
                    Recellection.textureMap.GetTexture(Globals.TextureTypes.ResourceBuilding), Color.Black);

            MenuIcon defensiveCell = new MenuIcon(Language.Instance.GetString("DefensiveCell") +
                    " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Barrier) + ")",
                    Recellection.textureMap.GetTexture(Globals.TextureTypes.BarrierBuilding), Color.Black);

            MenuIcon aggressiveCell = new MenuIcon(Language.Instance.GetString("AggressiveCell") +
                    " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Aggressive) + ")",
                    Recellection.textureMap.GetTexture(Globals.TextureTypes.AggressiveBuilding), Color.Black);
            MenuIcon cancel = new MenuIcon(Language.Instance.GetString("Cancel"),
                    Recellection.textureMap.GetTexture(Globals.TextureTypes.No));
            List<MenuIcon> menuIcons = new List<MenuIcon>();
            menuIcons.Add(baseCell);
            menuIcons.Add(resourceCell);
            menuIcons.Add(defensiveCell);
            menuIcons.Add(aggressiveCell);
            menuIcons.Add(cancel);
            Menu ConstructBuildingMenu = new Menu(Globals.MenuLayout.NineMatrix, menuIcons, Language.Instance.GetString("ChooseBuilding"), Color.Black);
            MenuController.LoadMenu(ConstructBuildingMenu);
            Recellection.CurrentState = MenuView.Instance;
            Globals.BuildingTypes building;

            MenuIcon choosenMenu = MenuController.GetInput();
            Recellection.CurrentState = WorldView.Instance;
            MenuController.UnloadMenu();
            if (choosenMenu.Equals(baseCell))
            {
                building = Globals.BuildingTypes.Base;
            }
            else if (choosenMenu.Equals(resourceCell))
            {
                building = Globals.BuildingTypes.Resource;
            }
            else if (choosenMenu.Equals(defensiveCell))
            {
                building = Globals.BuildingTypes.Barrier;
            }
            else if (choosenMenu.Equals(aggressiveCell))
            {
                building = Globals.BuildingTypes.Aggressive;
            }
            else
            {
                return;
            }

            // If we have selected a tile, and we can place a building at the selected tile...
            try
            {
                if (!AddBuilding(building, sourceBuilding,
                        constructTile.position, theWorld, player))
                {
                    //SoundsController.playSound("Denied");
                }
            }
            catch (BuildingOutOfRangeException bore)
            {
                throw bore;
            }
        }
 /// <summary>
 /// Checks whether or not there is a Resource Point on the given Tile.
 /// </summary>
 /// <param name="current"></param>
 /// <returns></returns>
 internal bool ContainsResourcePoint(Tile current)
 {
     if (current.GetTerrainType().GetEnum() == Globals.TerrainTypes.Mucus)
     {
         return true;
     }
     return false;
 }
 /// <summary>
 /// Checks whether or not the given tile contains an enemy building.
 /// </summary>
 /// <param name="t"></param>
 /// <returns></returns>
 internal bool ContainsEnemyBuilding(Tile t)
 {
     if (t.GetBuilding() == null)
     {
         return false;
     }
     if (t.GetBuilding().GetOwner() != ai)
     {
         return true;
     }
     return false;
 }
        private void SelectTile(Tile t)
        {
            // If this is the first time we select a tile...
            if (selectedTile != null)
                selectedTile.active = false;

            selectedTile = t;
            selectedTile.active = true;
        }
        /// <summary>
        /// This is a recurisve method that each call creates a new tile for the 
        /// specific terrain type to spread. It will call itself until the number of
        /// tiles to spread is zero or less. The spread works by randomly choose
        /// a direction as seen futher down.
        /// </summary>
        /// <param name="tileMatrix"></param>
        /// <param name="xCoord"></param>
        /// <param name="yCoord"></param>
        /// <param name="numberOfTiles"></param>
        /// <param name="type"></param>
        /// <param name="randomer"></param>
        private static void SpreadTiles(Tile[,] tileMatrix, int xCoord,
            int yCoord, int numberOfTiles, Globals.TerrainTypes type,
            Random randomer)
        {
            tileMatrix[xCoord, yCoord] = new Tile(xCoord, yCoord, type);

            //X represents the adjecent tiles, O is the current tile
            //      X = 1
            //2 = X O X = 3
            //  4 = X
            //
            if (numberOfTiles<=0 || !(xCoord > 1 && xCoord < tileMatrix.GetLength(0)-1 && yCoord > 1 && yCoord < tileMatrix.GetLength(1)-1))
            {
                return;
            }
            switch (randomer.Next(4))
            {
                case 0:
                    SpreadTiles(tileMatrix, xCoord, yCoord - 1,
                        numberOfTiles - 1, type, randomer);
                    break;

                case 1:
                    SpreadTiles(tileMatrix, xCoord - 1, yCoord,
                        numberOfTiles - 1, type, randomer);
                    break;

                case 2:
                    SpreadTiles(tileMatrix, xCoord + 1, yCoord,
                        numberOfTiles - 1, type, randomer);
                    break;

                case 3:
                    SpreadTiles(tileMatrix, xCoord, yCoord + 1,
                        numberOfTiles - 1, type, randomer);
                    break;

            }
        }
        private static void RandomPlaceResources(Random randomer, Tile[,] retur)
        {
            myLogger.Trace("Randomly placing resources.");
            int randomX;
            int randomY;

            int numberOfResourceTiles = 5;

            //Place five randomly placed resourceTiles, then
            for (int i = 0; i < numberOfResourceTiles; i++)
            {
                //Randomly choose a tile excluding all the edge tiles.
                randomY = randomer.Next((map_rows / 3), map_rows * 2 / 3);

                randomX = randomer.Next((map_cols / 3), map_cols * 2 / 3);

                retur[randomX, randomY] = new Tile(randomX, randomY, Globals.TerrainTypes.Mucus);
            }

            myLogger.Trace("Doop");
            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    int minY = (map_rows / 5) + y * (map_rows * 2 / 3);
                    int minX = (map_cols / 5) + x * (map_cols * 2 / 3);

                    int maxY = (map_rows / 3) + map_rows * y - 2 - (map_rows / 3) * y;
                    int maxX = (map_cols / 3) + map_cols * x - 2 - (map_cols / 3) * x;
                    do
                    {
                        randomY = randomer.Next(minY, maxY);

                        randomX = randomer.Next(minX, maxX);
                    }//Make sure that the random coordinates are not on the base building spawn point.
                    while ((randomX == 5 || randomX == map_cols - 5) && (randomY == 5 || randomY == map_rows - 5));

                    retur[randomX, randomY] = new Tile(randomX, randomY, Globals.TerrainTypes.Mucus);
                }
            }
            myLogger.Trace("Finished placing resources");
        }
        /// <summary>
        /// Initiates the tile matrix with default tiles for every tile
        /// </summary>
        /// <param name="randomer">The random generator used to generate the map.</param>
        /// <returns></returns>
        private static Tile[,] InitTileMatrix(Random randomer)
        {
            map_rows = randomer.Next(MINIMUM_MAP_SIZE, MAXIMUM_MAP_SIZE);

            map_cols = randomer.Next(MINIMUM_MAP_SIZE, MAXIMUM_MAP_SIZE);

            //Construct the matrix, the size is limited by MINIMUM and MAXIMUM
            Tile[,] retur = new Tile[map_cols, map_rows];

            myLogger.Info("Map consists of " + map_cols + " times "
                + map_rows + " tiles.");

            //Each row needs an array initiated each row needs to have
            //an array of equal size.
            for (int i = 0; i < map_cols; i++)
            {
                for (int j = 0; j < map_rows ; j++)
                {
                    retur[i, j] = new Tile(i, j);
                }

            }

            return retur;
        }
        /// <summary>
        /// Move a set of units from one tile to another
        /// </summary>
        /// <param name="amount">The amount of units to be moved</param>
        /// <param name="from">The tile to move units from</param>
        /// <param name="to">Tile tile to move units to</param>
        public static void MoveUnits(Player player, Tile from, Tile to, int amount)
        {
            logger.SetThreshold(LogLevel.TRACE);

            bool fromBuilding = (from.GetBuilding() != null);
            bool toBuilding = (to.GetBuilding() != null);
            logger.Debug("Moving "+amount+" units from "+from+" to "+to);

            List<Unit> units;
            // If we are moving units from a fromBuilding, they might not
            // all be on the same tile
            if (fromBuilding)
            {
                units = from.GetBuilding().GetUnits();
                logger.Debug("Moving "+units.Count()+" ("+from.GetBuilding().CountUnits()+") units from a building.");
            }
            else
            {
                units = new List<Unit>(from.GetUnits(player));
                logger.Debug("Moving "+units.Count()+" from a tile!");
            }

            List<Unit> toBeRemovedFromBuilding = new List<Unit>();

            // Move some units from the targetted tile
            lock (units)
            {
                foreach (Unit u in units)
                {
                    if (amount <= 0)
                    {
                        break;
                    }

                    u.TargetEntity = null;
                    u.MissionEntity = to;
                    amount--;

                    if (u.IsAtBase())
                    {
                        logger.Trace("Unit is patrolling! Removing from rotation.");
                        toBeRemovedFromBuilding.Add(u);
                    }

                    if (toBuilding)
                    {
                        logger.Trace("Adding unit to patrol the 'to' building!");
                        u.TargetEntity = null;
                        u.MissionEntity = to.GetBuilding();
                    }
                }
            }

            // We don't want to remove units from a tile, as they do it themselves
            foreach(Unit u in toBeRemovedFromBuilding)
            {
                if (! (u.BaseEntity is Building))
                    continue;

                ((Building)u.BaseEntity).RemoveUnit(u);
            }
        }