Exemple #1
0
    void CreateMap(HexGrid hexGrid, SetupData setupData, WorldController worldController)
    {
        //LAND OR OCEAN
        foreach (HexCell cell in hexGrid.Cells)
        {
            List <HexCell> neighborLandTiles = new List <HexCell>();
            neighborLandTiles.PopulateListWithMatchingConditions(cell.Neighbors, (c) => c.IsLand == true);
            if (neighborLandTiles.Count == 0)
            {
                bool isNewLand = setupData.newLandmassChance > HexMetrics.SampleHashGrid(cell.Position).a;
                cell.IsLand = isNewLand;
                if (isNewLand)
                {
                    Landmass newLandmass = new Landmass();
                    worldController.Landmasses.Add(newLandmass);
                    cell.Landmass = newLandmass;
                }
            }
            else
            {
                bool isLand = setupData.landByLandChance * neighborLandTiles.Count > HexMetrics.SampleHashGrid(cell.Position).a;

                //Add all nearby landmasses to list
                List <Landmass> neighboringLandmasses = new List <Landmass>();
                foreach (var landCell in neighborLandTiles)
                {
                    if (neighboringLandmasses.Contains(landCell.Landmass))
                    {
                        continue;
                    }
                    neighboringLandmasses.Add(landCell.Landmass);
                }
                //Get size of all nearby landmasses
                int totalSize = 0;
                foreach (var landmass in neighboringLandmasses)
                {
                    totalSize += landmass.landCells.Count;
                }
                //If exceeding maximum landmasssize after conversion to land (and combining any landmasses previously not connected, then abort
                if (totalSize + 1 > setupData.landMassMaxSize)
                {
                    isLand = false;
                }

                cell.IsLand = isLand;
                if (isLand)
                {
                    cell.Landmass = neighborLandTiles[0].Landmass;
                }
            }
        }

        //SET HEXES SURROUNDED BY LAND AS LAND
        foreach (HexCell cell in hexGrid.Cells)
        {
            List <HexCell> neighborLandTiles = new List <HexCell>();
            neighborLandTiles.PopulateListWithMatchingConditions(cell.Neighbors, (c) => c.IsLand == true);
            if (neighborLandTiles.Count == 6)
            {
                cell.IsLand   = true;
                cell.Landmass = neighborLandTiles[0].Landmass;
            }
        }

        //Clear empty landmasses
        for (int i = 0; i < worldController.Landmasses.Count; i++)
        {
            if (worldController.Landmasses[i].landCells.Count < 1)
            {
                worldController.Landmasses.RemoveAt(i);
                i--;
            }
        }

        //BITMASK AND VISUALS
        foreach (HexCell cell in hexGrid.Cells)
        {
            cell.CalculateBitmask();
            SetTerrainCellVisual(cell);
        }

        //HARBORS AND POINTS OF INTERESTS
        int strongholds = 0;

        for (int i = 0; i < worldController.Landmasses.Count; i++)
        {
            Landmass landmass = worldController.Landmasses[i];
            HexCell  poiCell  = Utility.ReturnRandom(landmass.GetShores(), setupData.Seed);
            landmass.poiLocationCell = poiCell;
            PointOfInterest.Type typeToSpawn = PointOfInterest.Type.Harbor;
            if (HexMetrics.SampleHashGrid(poiCell.Position).c < setupData.strongholdSpawnChance && strongholds < setupData.strongholdsToSpawn)
            {
                typeToSpawn = PointOfInterest.Type.Stronghold;
                strongholds++;
            }
            landmass.TypeOfPOI = typeToSpawn;
        }
        int remainingStrongholdsToSpawn = setupData.strongholdsToSpawn - strongholds;

        while (remainingStrongholdsToSpawn > 0)
        {
            Landmass newPoiLandmass = Utility.ReturnRandomElementWithCondition(worldController.Landmasses, (landmass) => landmass.TypeOfPOI != PointOfInterest.Type.Stronghold);
            newPoiLandmass.TypeOfPOI = PointOfInterest.Type.Stronghold;
            remainingStrongholdsToSpawn--;
        }
        //Setup point of interests
        foreach (Landmass landmass in worldController.Landmasses)
        {
            switch (landmass.TypeOfPOI)
            {
            case PointOfInterest.Type.Harbor:
                worldController.Harbors.Add(AddHarbor(landmass.poiLocationCell, setupData));
                break;

            case PointOfInterest.Type.Stronghold:
                worldController.Strongholds.Add(AddStronghold(landmass.poiLocationCell, setupData));
                break;
            }
        }
    }