Exemplo n.º 1
0
        public void CalculateClimate(LockBitmap heatMap)
        {
            var provinces = new List <Province>(this.ProvinceLand);

            this.heat = heatMap;
            heatMap.LockBits();
            using (System.IO.StreamWriter filein =
                       new System.IO.StreamWriter(Globals.MapOutputTotalDir + "map\\climate.txt", false,
                                                  Encoding.GetEncoding(1252)))
            {
                provinces.Sort(SortByHeat);

                int numHeavy  = provinces.Count / 7;
                int numNormal = provinces.Count / 6;
                int numLight  = provinces.Count / 5;
                if (Globals.Climate == 3)
                {
                    numHeavy  /= 2;
                    numNormal /= 2;
                    numHeavy  /= 2;
                }
                if (Globals.Climate == 4)
                {
                    numHeavy  = 0;
                    numNormal = 0;
                    numHeavy  = 0;
                }

                numNormal += numHeavy;
                numLight  += numNormal;
                List <Province> heavy  = new List <Province>();
                List <Province> normal = new List <Province>();
                List <Province> light  = new List <Province>();
                int             c      = 1;
                String          h      = "";
                String          n      = "";
                String          l      = "";
                for (int x = 0; x < provinces.Count; x++)
                {
                    var p = provinces[x];

                    if (c < numHeavy)
                    {
                        heavy.Add(p);
                        h = h + " " + p.ID;
                    }
                    else if (c < numNormal)
                    {
                        normal.Add(p);
                        n = n + " " + p.ID;
                    }
                    else if (c < numLight)
                    {
                        light.Add(p);
                        l = l + " " + p.ID;
                    }
                    c++;
                }

                filein.WriteLine("mild_winter = {");
                filein.WriteLine(l);
                filein.WriteLine("}");
                filein.WriteLine("normal_winter = {");
                filein.WriteLine(n);
                filein.WriteLine("}");
                filein.WriteLine("severe_winter = {");
                filein.WriteLine(h);
                filein.WriteLine("}");
            }

            heatMap.UnlockBits();
            foreach (var value in colorProvinceMap.Values)
            {
                value.points.Clear();
                value.border.Clear();
                value.coast.Clear();
                value.adjacent.Clear();
            }
            this.provinces.Clear();
            ProvinceSea.Clear();
            ProvinceLand.Clear();
            OceanZones.Clear();
            SeaZones.Clear();
        }
Exemplo n.º 2
0
        private void CalculateDetails()
        {
            int cc = 1;

            ProvinceSea.Clear();
            ProvinceLand.Clear();


            provinces = provinces.Distinct().OrderBy(p => p.isSea).ToList();
            int ii = 1;

            for (int index = 0; index < provinces.Count; index++)
            {
                var provinceDetailse = provinces[index];
                if (provinceDetailse.points.Count == 0)
                {
                    provinces.Remove(provinceDetailse);
                    index--;
                    continue;
                }
                if (provinceDetailse.isSea)
                {
                    ProvinceSea.Add(provinceDetailse);
                }
                else
                {
                    ProvinceLand.Add(provinceDetailse);
                }
                provinceDetailse.ID = ii;

                ii++;
            }

            foreach (var provinceDetails in provinces)
            {
                foreach (var point in provinceDetails.points)
                {
                    int x = point.X;
                    int y = point.Y;
                    if (x < provinceDetails.min.X)
                    {
                        provinceDetails.min.X = x;
                    }
                    if (y < provinceDetails.min.Y)
                    {
                        provinceDetails.min.Y = y;
                    }
                    if (x > provinceDetails.max.X)
                    {
                        provinceDetails.max.X = x;
                    }
                    if (y > provinceDetails.max.Y)
                    {
                        provinceDetails.max.Y = y;
                    }
                }
            }
            for (int y = 0; y < Bitmap.Height; y++)
            {
                for (int x = 0; x < Bitmap.Width; x++)
                {
                    var pixel = Bitmap.GetPixel(x, y);
                    if (pixel.R == 0 && pixel.G == 0 && pixel.B == 0)
                    {
                        continue;
                    }
                    var provinceDetails = colorProvinceMap[pixel];


                    for (int yy = -1; yy <= 1; yy++)
                    {
                        for (int xx = -1; xx <= 1; xx++)
                        {
                            if (xx == 0 && yy == 0)
                            {
                                continue;
                            }
                            if (x + xx < 0 || y + yy < 0 || x + xx >= Bitmap.Width || y + yy >= Bitmap.Height)
                            {
                                continue;
                            }

                            var pixel2 = Bitmap.GetPixel(x + xx, y + yy);
                            if (pixel2.R == 0 && pixel2.G == 0 && pixel2.B == 0)
                            {
                                provinceDetails.barrier.Add(new Point(x + xx, y + yy));
                            }
                            else
                            {
                                var provinceDetails2 = colorProvinceMap[pixel2];
                                if (provinceDetails2 != provinceDetails)
                                {
                                    provinceDetails.border.Add(new Point(x + xx, y + yy));
                                    if (!provinceDetails2.adjacent.Contains(provinceDetails))
                                    {
                                        provinceDetails2.adjacent.Add(provinceDetails);
                                        provinceDetails.adjacent.Add(provinceDetails2);
                                    }
                                }
                                if (provinceDetails2.isSea)
                                {
                                    provinceDetails.coast.Add(new Point(x + xx, y + yy));
                                }
                            }
                        }
                    }
                }
            }



            var seaProv2 = new List <Province>(ProvinceSea);

            List <MapGenManager.SeaZone> seaZones = new List <MapGenManager.SeaZone>();

            // get groups of sea provinces
            // number then sequentially in definitions
            // create ocean zones
            List <Province> dones = new List <Province>();

            while (seaProv2.Count > 0)
            {
                List <Province> chosen = new List <Province>();
                chosen.Add(seaProv2[0]);
                seaProv2.Remove(seaProv2[0]);
                int lastCount = chosen.Count + 1;
                while (chosen.Count != lastCount && chosen.Count < 30)
                {
                    lastCount = chosen.Count;
                    for (int index = 0; index < chosen.Count; index++)
                    {
                        var provinceDetailse = chosen[index];
                        var adj = provinceDetailse.adjacent.Where(a => a.isSea && !dones.Contains(a)).ToList();
                        chosen.AddRange(adj);
                        dones.AddRange(adj);

                        foreach (var detailse in adj)
                        {
                            seaProv2.Remove(detailse);
                        }

                        if (chosen.Count >= 30)
                        {
                            break;
                        }
                    }
                }

                var sz = new MapGenManager.SeaZone()
                {
                    ID = seaZones.Count + 1, Provinces = chosen.Distinct().ToList()
                };
                seaZones.Add(sz);
                foreach (var provinceDetailse in chosen)
                {
                    provinceDetailse.seaZone = sz;
                }
            }

            int id = ProvinceLand.Count + 1;

            foreach (var seaZone in seaZones)
            {
                foreach (var provinceDetailse in seaZone.Provinces)
                {
                    provinceDetailse.ID = id++;
                }
            }

            List <MapGenManager.SeaZone> toDo = new List <MapGenManager.SeaZone>(seaZones);

            List <MapGenManager.OceanZone> oceanZones = new List <MapGenManager.OceanZone>();

            while (toDo.Count > 0)
            {
                List <MapGenManager.SeaZone> chosenZones = new List <MapGenManager.SeaZone>();
                chosenZones.Add(toDo[0]);
                int lastChosen = chosenZones.Count + 1;
                toDo.Remove(toDo[0]);
                while (chosenZones.Count != lastChosen)
                {
                    lastChosen = chosenZones.Count;
                    for (int index = 0; index < chosenZones.Count; index++)
                    {
                        var seaZone = chosenZones[index];
                        foreach (var provinceDetailse in seaZone.Provinces)
                        {
                            var adj = provinceDetailse.adjacent.Where(a => a.isSea && a.seaZone != seaZone);
                            foreach (var detailse in adj)
                            {
                                if (toDo.Contains(detailse.seaZone))
                                {
                                    chosenZones.Add(detailse.seaZone);
                                    toDo.Remove(detailse.seaZone);
                                }
                            }
                        }
                    }
                }

                MapGenManager.OceanZone z = new MapGenManager.OceanZone()
                {
                    seaZones = chosenZones
                };

                oceanZones.Add(z);
            }
            this.provinces = this.provinces.OrderBy(p => p.ID).ToList();

            CalculatePositions();
            // CalculateClimate();
            this.OceanZones = oceanZones;
            this.SeaZones   = seaZones;
            SaveDefinitions();
            SaveDefaultMap();
        }