Exemplo n.º 1
0
        /// <summary>
        /// Sets up the voronoi map + the castles/towns
        /// </summary>
        /// <returns>Already ran voronoi map.</returns>
        /// <param name="sites">Number of Sites/town.</param>
        /// <param name="relaxItr">Number of Relax itrations.</param>
        /// <param name="totalSprites">Total sprites.</param>
        private VoronoiGenerator VoronoiSiteSetup(int sites, int relaxItr, int totalSprites)
        {
            // DEFINING CASTLE POSITIONS ON THE MAP:
            Vector2[] sitelist = CreateRandomPoints(sites);

            // APPLYING VORONOI TO THE MAP ARRAY
            VoronoiGenerator voronoi = new VoronoiGenerator(width, height, sitelist, relaxItr);

            // Getting new positions after relaxing:
            regionCenterPoints = voronoi.GetNewSites();

            return(voronoi);
        }
Exemplo n.º 2
0
        // ------------ The actual generating of the map. ------------\\



        /// <summary>
        /// Generates the map using a set of algorithms. This is the
        /// controller for the mapgenerator namespace.
        /// </summary>
        /// <returns>The newly generated map.</returns>
        private int[,] GenerateMap(int sites, int relaxItr, int fill, int smooth, int totalSprites, Player[] players)
        {
            // Using Voronoi Algorithm to make zones.
            VoronoiGenerator voronoi = VoronoiSiteSetup(sites, relaxItr, totalSprites);

            int[,] voronoiMap = voronoi.GetMap();

            // Converting zones to regions:
            RegionFill r = new RegionFill(voronoiMap, regionCenterPoints);

            int[,] generatedMap = r.GetMap();
            regions             = r.GetRegions();

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

            foreach (Region region in regions)
            {
                regionBySize.Add(region);
                region.ResetRegionGroundTileType(generatedMap);
            }

            regionBySize.Sort();

            int waterRegionCount = (int)(regions.Length * percentWater);
            int playerID         = 0;

            for (int i = 0; i < regions.Length; i++)
            {
                if (i <= waterRegionCount)
                {
                    regionBySize[i] = new WaterRegion(
                        regionBySize[i].GetCoordinates(),
                        regionBySize[i].RegionCenter
                        );
                }
                else
                {
                    Player player = null;
                    if (playerID < players.Length)
                    {
                        players[playerID] = player = new Player(playerID, 0);
                        playerID++;
                    }

                    regionBySize[i] = new LandRegion(
                        regionBySize[i].GetCoordinates(),
                        regionBySize[i].RegionCenter
                        );
                }

                for (int j = 0; j < regions.Length; j++)
                {
                    if (regionBySize[i].Equals(regions[j]))
                    {
                        regions[j] = regionBySize[i]; break;
                    }
                }
            }


            connectLostPointsToRegions(voronoiMap);

            // Creating randomness through procedural map generation.:
            BinaryMap binary = new BinaryMap(width, height, smooth, seed, fill, regions);

            int[,] binaryMap = binary.getMap();

            // Combining binary map and zone-devided maps:
            generatedMap = CombineMaps(binaryMap, generatedMap);
            canWalk      = CreateWalkableArea(generatedMap);

            initialMap = new int[width, height];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    initialMap[x, y] = generatedMap[x, y];
                }
            }
            // Setting the enviroment for each region:
            foreach (Region region in regions)
            {
                if (region.GetType().Equals(typeof(LandRegion)))
                {
                    LandRegion lr = (LandRegion)region;
                    lr.SetRegionGroundTileType(generatedMap);
                }
                else if (region.GetType().Equals(typeof(WaterRegion)))
                {
                    WaterRegion wr = (WaterRegion)region;
                    wr.FillRegionWithWater(generatedMap, canWalk);
                }
            }

            return(generatedMap);
        }