Exemplo n.º 1
0
        public World(int widthArea, int heightArea)
        {
regen:

            this.sites    = new List <FortuneSite>();
            this.cities   = new List <Region>();
            this.kingdoms = new List <Kingdom>();
            this.colors   = new List <Color>();
            this.voronoi  = false;
            this.affSite  = true;
            this.croyance = false;
            this.pantheon = new Pantheon();

            this.mapHeight = heightArea;
            this.mapWidth  = widthArea;
            Random rand   = new Random();
            int    nbSite = widthArea * heightArea / 1000; // default: /400

            this.perlinMap = new double[widthArea, heightArea];
            //nbSite = 30;

            this.colors.Add(Color.Red);
            this.colors.Add(Color.Black);
            this.colors.Add(Color.Purple);
            this.colors.Add(Color.Gray);
            this.colors.Add(Color.Cyan);

            this.name = NameGenerator.GenWorldName();

            Perlin p = new Perlin
            {
                Persistence = 0.65,
                Frequency   = .003333,
                OctaveCount = 8,
                Seed        = /*628594615;*/ new Random().Next(999999999)
            };

            for (int i = 0; i < widthArea; i++)
            {
                for (int j = 0; j < heightArea; j++)
                {
                    perlinMap[i, j] = p.GetValue(i, j, 0);
                }
            }

            for (int i = 0; i < nbSite; i++)
            {
                double cadre = 50d;
                var    x     = rand.NextDouble() * widthArea;
                var    y     = rand.NextDouble() * heightArea;
                var    tmp   = new Region(x, y, perlinMap);
                if (!(x < this.mapWidth - cadre && x > cadre &&
                      y < this.mapHeight - cadre && y > cadre))
                {
                    tmp.City = false;
                }
                sites.Add(tmp);
                if (tmp.City)
                {
                    cities.Add(tmp);
                }
            }

            if (this.cities.Count == 0)
            {
                this.Empty();
                goto regen;
            }

            this.vedges = FortunesAlgorithm.Run(ref sites, 0, 0, widthArea, heightArea);

            // Génération Pays
            int nbCapital = 5;

            while (nbCapital > 0)
            {
                var tmp = cities.ElementAt(new Random().Next(cities.Count));
                if (!tmp.Capital)
                {
                    var r = new Random().Next(this.colors.Count);
                    tmp.Capital = true;
                    this.kingdoms.Add(new Kingdom(tmp, this.colors.ElementAt(r), this.pantheon));
                    tmp.GenRegion(this.perlinMap, r + this.colors.ElementAt(r).ToArgb());
                    this.colors.RemoveAt(r);
                    nbCapital--;
                }
            }

            List <string> verifKingdomName = new List <string>();

            foreach (Kingdom k in this.kingdoms)
            {
                if (verifKingdomName.Contains(k.Name))
                {
                    k.Name = NameGenerator.GenKingdomName();
                }

                verifKingdomName.Add(k.Name);
                k.Purge();
            }

            int n = 0;

            foreach (Region c in cities)
            {
                if (!c.Capital)
                {
                    c.GenRegion(this.perlinMap, new Random().Next(999999) + n);
                }

                n++;
            }

            //Génération des religions
            var pTmp = this.pantheon.Gods.Where(g => !g.Forgot).ToList();

            foreach (Kingdom k in this.kingdoms.Where(e => e.Type == KingdomType.Théocratie))
            {
                var id = rand.Next(pTmp.Count);

                var r = pTmp.ElementAt(id);

                k.God      = r;
                r.Capitale = k.Capital;
                r.Followers.Add(k.Capital);
                k.Capital.God          = r;
                k.Capital.GodInfluence = 1;

                pTmp.RemoveAt(id);
            }

            foreach (God g in pTmp)
            {
                var tmp = this.cities.Where(c => c.Kingdom != null ? c.Kingdom.Type != KingdomType.Théocratie : true).ToList();
                var r   = tmp.ElementAt(rand.Next(tmp.Count));
                r.GodInfluence = 1;
                r.God          = g;
                g.Capitale     = r;
                g.Followers.Add(r);
            }

            // foreach region avec 1 de GodInfluence expendre en -0.1 jusqu'a rencontrer une influence equivalente ou atteindre 0.1

            foreach (Region r in this.sites)
            {
                if (r.GodInfluence == 1)
                {
                    var g = this.pantheon.Gods.Where(e => e == r.God).FirstOrDefault();
                    g.GenInfluence(r);
                }
            }

            foreach (FortuneSite site in sites)
            {
                foreach (VEdge vedge in vedges)
                {
                    if (vedge.Left == site || vedge.Right == site)
                    {
                        if (!site.Cell.Contains(vedge))
                        {
                            site.Cell.Add(vedge);
                        }
                    }
                }
            }
        }