Пример #1
0
        // ------------ Methods to initialize buildings, players and other details on to the map ------------\\



        /// <summary>
        /// Initializes the buildings. Creates all buildings for a given economy.
        /// </summary>
        /// <param name="r">The red component.</param>
        private void InitBuildings(LandRegion r)
        {
            r.createEconomy(canWalk, new Economy(Economy.POOR));

            foreach (OverworldBuilding building in r.GetBuildings())
            {
                int x = (int)building.Origo.x;
                int y = (int)building.Origo.y;
                map[x, y] = building.GetSpriteID();
            }
        }
Пример #2
0
        /// <summary>
        /// Tests the paths between all buildings inside a given land-region.
        /// </summary>
        /// <returns><c>true</c>, if paths between buildings was accessable, <c>false</c> otherwise.</returns>
        /// <param name="region">Region.</param>
        /// <param name="canWalk">Can walk.</param>
        public bool TestPathsBetweenBuildings(LandRegion region, int[,] canWalk)
        {
            AStarAlgo aStar = new AStarAlgo(canWalk, canWalk.GetLength(0), canWalk.GetLength(1), false);

            Point castlePlacement = region.GetCastle().GetPosition();

            foreach (OverworldBuilding building in region.GetBuildings())
            {
                Point buildingPlacement = building.Origo;
                if (aStar.calculate(castlePlacement, buildingPlacement).Count == 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Initializes the players onto te map.
        /// </summary>
        /// <param name="map">Map.</param>
        /// <param name="canWalk">Can walk.</param>
        /// <param name="players">Players.</param>
        public void initializePlayers(int[,] map, int[,] canWalk, Player[] players)
        {
            int i = 0;

            foreach (Region region in regions)
            {
                if (region.GetType().Equals(typeof(LandRegion)))
                {
                    LandRegion lr = (LandRegion)region;
                    // Place a castle at every landregion
                    lr.PlaceCastle(map, canWalk);
                    if (i < players.Length)
                    {
                        // Create a player, set the castles owner as that player.
                        // Also place a corresponding hero.
                        players[i] = new Player(i, 0);
                        lr.GetCastle().Player = players[i];
                        lr.GetCastle().Town.Owner = players[i];
                        players[i].Castle.Add(lr.GetCastle());
                        i++;
                    }
                }
            }
        }
Пример #4
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);
        }