private void btnAddBuilding_Click(object sender, EventArgs e)
        {
            if (tbName.Text == "")
            {
                MessageBox.Show("Select a name");
                return;
            }

            if (cbBuildingType.Text == "")
            {
                MessageBox.Show("Select a building type");
                return;
            }

            if (playerDoc.XPathSelectElements("Players/Player/Buildings/*[Name=\"" + tbName.Text + "\"]").Count() > 0)
            {
                MessageBox.Show("Building with that name already exists. Select another name");
                return;
            }

            String _name = tbName.Text;
            String _type = cbBuildingType.Text;
            int _x = coords.User.X;
            int _y = coords.User.Y;

            switch (_type)
            {
                case "Town":
                    Town t = new Town(_name, _x, _y, @"Terrain\Possible Buildings or Encampments\human-tile.png", player);
                    break;
                case "Watchtower":
                    Watchtower wt = new Watchtower(_name, _x, _y, player, @"Terrain\Possible Buildings or Encampments\elven-tile.png");
                    break;
                case "Castle":
                    Castle c = new Castle(_name, _x, _y, @"Terrain\Possible Buildings or Encampments\keep-tile.png", player);
                    break;
                default:
                    MessageBox.Show("Building type not defined yet");
                    break;
            }

            this.Close();
            this.Dispose();
        }
        private void LoadMap()
        {
            if (tm != null)
            {
                tm.Dispose();
            }

            tm.Objects = new List<MapObject>();
            newObjects = new List<MapObject>();

            playerDoc = XDocument.Load(Player.SavePath);
            tileDoc = XDocument.Load(TerrainGridHex.SavePath);
            turnDoc = XDocument.Load(Turn.SavePath);

            players = new List<Player>();

            int cnt = playerDoc.XPathSelectElements("Players/Player").Count();

            for (int i = 1; i <= cnt; i++)
            {
                XElement e = playerDoc.XPathSelectElement("Players/Player[Position=\"" + i.ToString() + "\"]");

                String name = e.Element("Name").Value;
                int position = int.Parse(e.Element("Position").Value);
                String race = e.Element("Race").Value;
                String password = e.Element("Password").Value;
                int hor = int.Parse(e.Element("Horizontal").Value);
                int ver = int.Parse(e.Element("Vertical").Value);

                Player p = new Player(name, position, race, password, false, hor, ver);

                players.Add(p);

                //Load towns
                foreach (var elem in e.XPathSelectElements("Buildings/Town"))
                {
                    String _name = elem.Element("Name").Value;
                    int _level = int.Parse(elem.Element("Level").Value);
                    bool _levelChanged = bool.Parse(elem.Element("LevelChanged").Value);
                    String _coverLevel = elem.Element("CoverLevel").Value;
                    String _wallLevel = elem.Element("WallLevel").Value;
                    String _towerLevel = elem.Element("TowerLevel").Value;
                    int _x = int.Parse(elem.Element("X").Value);
                    int _y = int.Parse(elem.Element("Y").Value);
                    String _imgPath = elem.Element("ImagePath").Value;

                    Town t = new Town(_name, _level, _x, _y, _imgPath, name, _coverLevel, _wallLevel, _towerLevel, _levelChanged);

                    if (_x == -1)
                    {
                        newObjects.Add(t);
                    }

                    tm.AddObject(t);
                    p.AddTown(t);
                }

                //Load watchtowers
                foreach (var elem in e.XPathSelectElements("Buildings/Watchtower"))
                {
                    String _name = elem.Element("Name").Value;
                    int _gs = int.Parse(elem.Element("GarrisonSize").Value);
                    int _x = int.Parse(elem.Element("X").Value);
                    int _y = int.Parse(elem.Element("Y").Value);
                    String _imgPath = elem.Element("ImagePath").Value;

                    Watchtower wt = new Watchtower(_name, _x, _y, name, _imgPath, _gs);

                    if (_x == -1)
                    {
                        newObjects.Add(wt);
                    }

                    tm.AddObject(wt);
                    p.AddWatchtower(wt);
                }

                //Load castles
                foreach (var elem in e.XPathSelectElements("Buildings/Castle"))
                {
                    String _name = elem.Element("Name").Value;
                    int _level = int.Parse(elem.Element("Level").Value);
                    bool _levelChanged = bool.Parse(elem.Element("LevelChanged").Value);
                    int _x = int.Parse(elem.Element("X").Value);
                    int _y = int.Parse(elem.Element("Y").Value);
                    String _imgPath = elem.Element("ImagePath").Value;
                    String _wallLevel = elem.Element("WallLevel").Value;
                    String _towerLevel = elem.Element("TowerLevel").Value;
                    String _gatehouseLevel = elem.Element("GatehouseLevel").Value;

                    Castle c = new Castle(_name, _level, _levelChanged, _x, _y, _imgPath, name, _wallLevel, _towerLevel, _gatehouseLevel);

                    if (_x == -1)
                    {
                        newObjects.Add(c);
                    }

                    tm.AddObject(c);
                    p.AddCastle(c);
                }

                //Load armies
                foreach (var elem in e.XPathSelectElements("Armies/Army"))
                {
                    String _name = elem.Element("Name").Value;
                    String _type = elem.Element("Type").Value;
                    int _x = int.Parse(elem.Element("X").Value);
                    int _y = int.Parse(elem.Element("Y").Value);
                    double _moveTotal = double.Parse(elem.Element("MoveTotal").Value);
                    double _moveLeft = double.Parse(elem.Element("MoveLeft").Value);
                    Pen _color = new Pen(Color.FromName(elem.Element("Color").Value));
                    int _size = int.Parse(elem.Element("Size").Value);
                    bool _canBuild = bool.Parse(elem.Element("CanBuild").Value);
                    String _imgPath = elem.Element("ImgPath").Value;
                    List<Resource> _upkeepCost = new List<Resource>();
                    elem.Element("Upkeep").Attributes().ToList().ForEach(val => _upkeepCost.Add(new Resource(val.Name.ToString(), int.Parse(val.Value))));

                    Army a = new Army(_x, _y, _name, _type, _moveTotal, _moveLeft, _color, _size, name, _canBuild, _imgPath, _upkeepCost);

                    if (_x == -1)
                    {
                        newObjects.Add(a);
                    }

                    tm.AddObject(a);
                    p.AddArmy(a);
                }

                //Load resources
                foreach (var elem in e.XPathSelectElements("Resources").Elements())
                {
                    String _name = elem.Name.ToString();
                    int _amt = int.Parse(elem.Value);

                    p.Race.GetResource(_name).Value = _amt;
                }
            }

            //Load battles
            foreach (var e in playerDoc.XPathSelectElements("Players/Player/Armies/Army[IsInBattle=\"true\" or IsInBattle=\"True\"]"))
            {
                Army a = tm.Armies.Find((val) => val.Name == e.Element("Name").Value);
                MapObject mo = tm.Objects.Find((val) => val.Name == e.Element("UnitAttacked").Value);

                a.Attack(mo);
            }

            //Load sieges
            foreach (var e in playerDoc.XPathSelectElements("Players/Player/Armies/Army[IsSieging=\"true\" or IsSieging=\"True\"]"))
            {
                Army a = tm.Armies.Find((val) => val.Name == e.Element("Name").Value);
                Castle c = tm.Castles.Find((val) => val.Name == e.Element("UnitAttacked").Value);
                Town t = tm.Towns.Find((val) => val.Name == e.Element("UnitAttacked").Value);

                if (c != null) a.Siege(c);
                if (t != null) a.Siege(t);
            }

            //Load turn
            String _currentPlayer = turnDoc.XPathSelectElement("Turn/CurrentPlayer").Value;
            int _yearNo = int.Parse(turnDoc.XPathSelectElement("Turn/YearNo").Value);
            int _monthNo = int.Parse(turnDoc.XPathSelectElement("Turn/MonthNo").Value);
            bool _isStartOfTurn = bool.Parse(turnDoc.XPathSelectElement("Turn/IsStartOfTurn").Value);

            turn = new Turn(players, _currentPlayer, _yearNo, _monthNo, _isStartOfTurn);

            tm.BoardHexes.ForEach((val) => ((TerrainGridHex)val).IsSummer = (_monthNo <= 8));

            //New object to be placed logic
            if (newObjects.Count > 0) newObject = newObjects.First();
        }
        public void Siege(Castle c)
        {
            IsSieging = true;
            c.IsUnderSiege = true;
            _unitAttacked = c;

            Save();
            c.Save();
        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            tm.Objects = new List<MapObject>();
            newObject = null;
            newObjects = new List<MapObject>();

            playerDoc.XPathSelectElements("Players/Player").Remove();
            playerDoc.Save(Player.SavePath);

            tileDoc.XPathSelectElements("Tiles/Tile").Remove();
            tileDoc.Save(TerrainGridHex.SavePath);

            players = new List<Player>();

            String password = Player.EncryptPassword("a");

            players.Add(new Player("Red Player", 1, "Ogres", password, true, 0, 0));
            players.Add(new Player("Blue Player", 2, "High Elves", password, true, 0, 0));

            List<Resource> res1 = new List<Resource>() { new Resource("Food", 60), new Resource("Magic", 1) };
            List<Resource> res2 = new List<Resource>() { new Resource("Gold", 50), new Resource("Magic", 1) };

            Army a1, a2, a3, a4;

            a1 = new Army(25, 5, "Army 1", "Builder", 4, Pens.Red, 0, "Red Player", true, @"Races\Ogres\builder.png", null);
            tm.AddObject(a1);

            a2 = new Army(8, 8, "Army 2", "Flying", 10, Pens.Blue, 2000, "Blue Player", false, @"Races\HighElves\flying.png", res2);
            tm.AddObject(a2);

            a3 = new Army(14, 3, "Army 3", "Monstrous", 6, Pens.Red, 1500, "Red Player", false, @"Races\Ogres\monstrous.png", res1);
            tm.AddObject(a3);

            a4 = new Army(3, 14, "Army 4", "Builder", 5, Pens.Blue, 0, "Blue Player", true, @"Races\HighElves\builder.png", null);
            tm.AddObject(a4);

            players.ElementAt(0).AddArmy(a1);
            players.ElementAt(1).AddArmy(a2);
            players.ElementAt(0).AddArmy(a3);
            players.ElementAt(1).AddArmy(a4);

            Town t1 = new Town("Town 1", 3, 12, @"Terrain\Possible Buildings or Encampments\human-tile.png", "Red Player");
            t1.Upgrade(10);
            t1.LevelChanged = false;

            tm.AddObject(t1);
            players.ElementAt(0).AddTown(t1);

            Town t2 = new Town("Town 2", 8, 12, @"Terrain\Possible Buildings or Encampments\human-tile.png", "Blue Player");
            t2.Upgrade(5);
            t2.LevelChanged = false;

            tm.AddObject(t2);
            players.ElementAt(1).AddTown(t2);

            Watchtower wt1 = new Watchtower("Tower 1", 5, 7, "Blue Player", @"Terrain\Possible Buildings or Encampments\elven-tile.png");
            tm.AddObject(wt1);
            players.ElementAt(1).AddWatchtower(wt1);

            Castle c1 = new Castle("Castle 1", 20, 13, @"Terrain\Possible Buildings or Encampments\keep-tile.png", "Blue Player");
            c1.Upgrade(6);
            tm.AddObject(c1);
            players.ElementAt(1).AddCastle(c1);

            hexgridPanel1.MapBuffer = null;
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(5, 7)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(6, 6)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(7, 6)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(8, 5)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(9, 5)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(10, 4)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(11, 4)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(12, 3)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(13, 3)]).SpecialType = "Road";
            ((TerrainGridHex)tm.BoardHexes[HexCoords.NewUserCoords(14, 3)]).SpecialType = "Road";

            turn = new Turn(players);

            tm.SelectedArmy = null;
            clickCoords = HexCoords.NewUserCoords(-1, -1);

            Refresh();

            playerDoc = XDocument.Load(Player.SavePath);
            playerDoc.Save(Player.UndoPath);

            UpdateResources();
            UpdateSummary();
            UpdateLos();
        }