Exemplo n.º 1
0
        private void ApplyBiomes()
        {
            parsedBiomes = new Dictionary <HexCellBiome, int>();
            foreach (HexCellBiome biome in Enum.GetValues(typeof(HexCellBiome)))
            {
                parsedBiomes.Add(biome, 0);
            }

            for (int z = 0; z < CELL_COUNT_Z; z++)
            {
                for (int x = 0; x < CELL_COUNT_X; x++)
                {
                    HexCellBiome biome      = parseBiome(x, z);
                    int          height     = parseHeight(x, z);
                    byte         waterDepth = 0;
                    if (biome == HexCellBiome.WATER)
                    {
                        waterDepth = 1;
                    }
                    parsedBiomes[biome] += 1;
                    HexCellData cellData = new HexCellData(height, biome, waterDepth);
                    hexGrid.GetCell(x, z).Data = cellData;
                }
            }
        }
Exemplo n.º 2
0
        private HexCellBiome parseBiome(int x, int z)
        {
            float posX = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f) + 0.5f * HexMetrics.outerRadius;
            float posZ = z * (HexMetrics.outerRadius * 1.5f) + 0.5f * HexMetrics.innerRadius;

            Color landPixel = GetPixel(landImages, posX, posZ);
            //return fromColorToBiome(landPixel);

            List <Tuple <HexCellBiome, int> > foundBiomes = new List <Tuple <HexCellBiome, int> >();

            int index = foundBiomes.FindIndex(elem => elem.Item1 == fromColorToBiome(landPixel));

            if (index == -1)
            {
                foundBiomes.Add(new Tuple <HexCellBiome, int>(fromColorToBiome(landPixel), 2));
            }
            else
            {
                foundBiomes[index] = new Tuple <HexCellBiome, int>(fromColorToBiome(landPixel), foundBiomes[index].Item2 + 2);
            }

            Vector3 position = new Vector3(posX, 0, posZ);


            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                position += 0.5f * HexMetrics.GetFirstCorner(d);

                Color        pixel = GetPixel(landImages, position.x, position.z);
                HexCellBiome biome = fromColorToBiome(pixel);

                index = foundBiomes.FindIndex(elem => elem.Item1 == biome);
                if (index == -1)
                {
                    foundBiomes.Add(new Tuple <HexCellBiome, int>(biome, 1));
                }
                else
                {
                    foundBiomes[index] = new Tuple <HexCellBiome, int>(biome, foundBiomes[index].Item2 + 1);
                }

                position -= 0.5f * HexMetrics.GetFirstCorner(d);
            }


            foundBiomes.Sort((a, b) => b.Item2 - a.Item2);
            foreach (Tuple <HexCellBiome, int> tpl in foundBiomes)
            {
                if (cellQueryThresholds[tpl.Item1] <= tpl.Item2)
                {
                    return(tpl.Item1);
                }
            }

            //gooaphtermayoriti
            return(foundBiomes[0].Item1);
        }
Exemplo n.º 3
0
        private void AddMissingBiomes()
        {
            foreach (HexCellBiome biome in biomeThresholds.Keys)
            {
                if (biome != HexCellBiome.WATER && biome != HexCellBiome.BUILDINGS && biome != HexCellBiome.CITY && biome != HexCellBiome.SNOW)
                {
                    if (parsedBiomes[biome] < biomeThresholds[biome].Item1)
                    {
                        //Console.WriteLine("Adjusting missing: " + biome.ToString());
                        if (biome == HexCellBiome.ROCK || biome == HexCellBiome.COAL)
                        {
                            List <HexCell> buildings = new List <HexCell>();
                            foreach (HexCell cell in hexGrid.cells)
                            {
                                if (cell.Data.Biome == HexCellBiome.BUILDINGS)
                                {
                                    buildings.Add(cell);
                                }
                            }
                            int count3 = 0;
                            if (biome == HexCellBiome.COAL)
                            {
                                count3 = Mathf.Min(biomeThresholds[biome].Item1 - parsedBiomes[biome], (int)(((float)biomeThresholds[biome].Item1 / (float)(biomeThresholds[HexCellBiome.COAL].Item1 + (float)biomeThresholds[HexCellBiome.ROCK].Item1 + 1.0f)) * (float)buildings.Count));
                            }
                            else
                            {
                                count3 = Mathf.Min(biomeThresholds[biome].Item1 - parsedBiomes[biome], buildings.Count);
                            }
                            ReplaceMissingBiome(biome, HexCellBiome.BUILDINGS, count3);
                        }

                        /*
                         * HexCellBiome majority = this.parsedBiomes.Aggregate(
                         *  HexCellBiome.FOREST,
                         *  (HexCellBiome agg, KeyValuePair<HexCellBiome, int> elem) => agg = (elem.Value > parsedBiomes[agg] ? elem.Key : agg));
                         */
                        HexCellBiome majorityBiome = this.parsedBiomes.Aggregate(HexCellBiome.FOREST, (HexCellBiome elem1, KeyValuePair <HexCellBiome, int> elem2) =>
                        {
                            if (parsedBiomes[elem1] < elem2.Value)
                            {
                                return(elem2.Key);
                            }
                            else
                            {
                                return(elem1);
                            }
                        });

                        int count = biomeThresholds[biome].Item1 - parsedBiomes[biome];
                        ReplaceMissingBiome(biome, majorityBiome, count);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void ReplaceMissingBiome(HexCellBiome biome, HexCellBiome replace, int count)
        {
            List <HexCell> replaceCells = new List <HexCell>();

            foreach (HexCell cell in hexGrid.cells)
            {
                if (cell.Data.Biome == replace)
                {
                    replaceCells.Add(cell);
                }
            }
            while (count > 0)
            {
                //Console.WriteLine("Replacing " + replace.ToString() + " with " + biome.ToString());
                HexCell cell = replaceCells[random.Next(0, replaceCells.Count)];
                //cell.Data.SetBiome(biome);
                cell.Data = new HexCellData(cell.Data.Elevation, biome, cell.Data.WaterDepth);
                //Console.WriteLine(cell.Data.Biome.ToString() + biome.ToString());
                this.parsedBiomes[biome]++;
                this.parsedBiomes[replace]--;
                replaceCells.Remove(cell);
                count--;
            }
        }
Exemplo n.º 5
0
 public void SetBiome(HexCellBiome biome)
 {
     this.Biome = biome;
 }
Exemplo n.º 6
0
 public HexCellData(int elevation, HexCellBiome biome, byte waterDepth)
 {
     Elevation  = elevation;
     Biome      = biome;
     WaterDepth = waterDepth;
 }