Esempio n. 1
0
        private List <Region> LoadFromFile()
        {
            if (!File.Exists(_filename))
            {
                return(new List <Region>());
            }

            List <Region> listReg = new List <Region>();

            XmlDocument doc = new XmlDocument();

            doc.Load(_filename);

            //get inventories
            XmlNode regions = doc.GetElementsByTagName("regions").Item(0);

            //Pour chaque inventaire
            foreach (XmlElement region in regions.ChildNodes)
            {
                if (!ulong.TryParse(region.GetAttribute("id"), out ulong id))
                {
                    break;
                }

                Region reg = new Region
                {
                    Id   = id,
                    Name = region.GetAttribute("name"),
                    Type = GetTypeFromString(region.GetAttribute("type"))
                };

                //Pour chaque case
                foreach (XmlElement caseXml in region.ChildNodes)
                {
                    Case c = CaseFactory.BuildCase(caseXml);
                    reg.AddCase(c);
                }
                listReg.Add(reg);
            }

            return(listReg);
        }
Esempio n. 2
0
        public Region GenerateNewRegion(int size, ulong id, string name, RegionType regionType, Location centralCase = null, bool forceValable = false)
        {
            Random random = new Random();
            Region r      = new Region
            {
                Name = name,
                Id   = id,
                Type = regionType
            };


            if (centralCase == null)
            {
                centralCase = GetRandomNextCentralCase();
            }

            int baseX = centralCase.XPosition - (int)Math.Floor(Convert.ToDecimal(size / 2));
            int endX  = centralCase.XPosition + (int)Math.Floor(Convert.ToDecimal(size / 2));
            int baseY = centralCase.YPosition - (int)Math.Floor(Convert.ToDecimal(size / 2));
            int endY  = centralCase.YPosition + (int)Math.Floor(Convert.ToDecimal(size / 2));

            for (int i = baseX; i <= endX; i++)
            {
                for (int j = baseY; j <= endY; j++)
                {
                    Case c;

                    if (r.Type == RegionType.Sea)
                    {
                        c = CaseFactory.BuildCase("water", new Location(i, j));
                    }
                    else if (r.Type == RegionType.Desert)
                    {
                        c = CaseFactory.BuildCase("desert", new Location(i, j));
                    }
                    else if (r.Type == RegionType.Mountain)
                    {
                        int rndValue = random.Next(1, 10);
                        if (rndValue < 2)
                        {
                            c = CaseFactory.BuildCase("water", new Location(i, j));
                        }
                        else if (rndValue < 4)
                        {
                            c = CaseFactory.BuildCase("land", new Location(i, j));
                        }
                        else if (rndValue < 6)
                        {
                            c = CaseFactory.BuildCase("forest", new Location(i, j));
                        }
                        else
                        {
                            c = CaseFactory.BuildCase("mountain", new Location(i, j));
                        }
                    }
                    else
                    {
                        int rndValue = random.Next(1, 10);
                        if (rndValue < 3)
                        {
                            c = CaseFactory.BuildCase("water", new Location(i, j));
                        }
                        else if (rndValue < 6)
                        {
                            c = CaseFactory.BuildCase("forest", new Location(i, j));
                        }
                        else
                        {
                            c = CaseFactory.BuildCase("land", new Location(i, j));
                        }
                    }
                    r.AddCase(c);
                }
            }

            //Force this region to have a valid central case
            if (forceValable)
            {
                r.SetCentralCase(CaseFactory.BuildCase("land", r.GetCentralCase().Location));
            }

            _regions.Add(r);
            SaveToFile();

            return(r);
        }