コード例 #1
0
        public void PaintVegetation(MapRegion region, IRegionBiomeTemplate template)
        {
            var treeType = template.AreTreesJungle ? CellVegetation.Jungle : CellVegetation.Forest;

            var openCells = new List <IHexCell>();

            foreach (var cell in region.LandCells)
            {
                if (ShouldBeMarsh(cell, template))
                {
                    ModLogic.ChangeVegetationOfCell(cell, CellVegetation.Marsh);
                }
                else if (ModLogic.CanChangeVegetationOfCell(cell, treeType))
                {
                    openCells.Add(cell);
                }
            }

            int treeCount = Mathf.RoundToInt(template.TreePercentage * openCells.Count * 0.01f);

            var treeSeeds = CellRandomSampler.SampleElementsFromSet(
                openCells, UnityEngine.Random.Range(template.MinTreeClumps, template.MaxTreeClumps),
                GetTreeSeedWeightFunction(treeType, template)
                );

            var treeCells = new List <IHexCell>();

            var treeCrawlers = treeSeeds.Select(
                seed => GridTraversalLogic.GetCrawlingEnumerator(
                    seed, openCells, treeCells, GetTreeCrawlingCostFunction(treeType, template)
                    )
                ).ToList();

            for (int i = 0; i < treeCount; i++)
            {
                if (treeCrawlers.Count == 0)
                {
                    Debug.LogWarning("Failed to paint correct number of trees into region");
                    break;
                }

                var crawler = treeCrawlers.Random();

                if (crawler.MoveNext())
                {
                    treeCells.Add(crawler.Current);
                    openCells.Remove(crawler.Current);
                }
                else
                {
                    treeCrawlers.Remove(crawler);
                    i--;
                }
            }

            foreach (var treeCell in treeCells)
            {
                ModLogic.ChangeVegetationOfCell(treeCell, treeType);
            }
        }
コード例 #2
0
 protected override void EditCell(IHexCell cell)
 {
     if (IsPainting && CellModificationLogic.CanChangeVegetationOfCell(cell, ActiveVegetation))
     {
         CellModificationLogic.ChangeVegetationOfCell(cell, ActiveVegetation);
     }
 }
コード例 #3
0
        public bool TryIncreaseScore(MapRegion region, RegionData regionData, out float scoreAdded)
        {
            var addJungleCandidates = region.Cells.Where(IncreaseScoreFilter);

            if (addJungleCandidates.Any())
            {
                var newJungle = addJungleCandidates.Random();

                var oldScore = CellScorer.GetScoreOfCell(newJungle);

                ModLogic.ChangeVegetationOfCell(newJungle, CellVegetation.Jungle);

                scoreAdded = CellScorer.GetScoreOfCell(newJungle) - oldScore;
                return(true);
            }
            else
            {
                scoreAdded = 0f;
                return(false);
            }
        }
コード例 #4
0
        public void HandleCommandOnUnit(AbilityCommandRequest command, IUnit unit)
        {
            if (CanHandleCommandOnUnit(command, unit))
            {
                var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);

                CellModificationLogic.ChangeVegetationOfCell(unitLocation, CellVegetation.None);
            }
            else
            {
                throw new InvalidOperationException("Cannot handle command");
            }
        }
コード例 #5
0
        public IImprovement BuildImprovement(
            IImprovementTemplate template, IHexCell location, float workInvested,
            bool isConstructed, bool isPillaged
            )
        {
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }
            else if (location == null)
            {
                throw new ArgumentNullException("location");
            }

            var newGameObject = GameObject.Instantiate(ImprovementPrefab);

            Container.InjectGameObject(newGameObject);

            var newImprovement = newGameObject.GetComponent <Improvement>();

            newImprovement.Template = template;

            if (template.ClearsVegetationWhenBuilt && location.Vegetation != CellVegetation.None)
            {
                CellModificationLogic.ChangeVegetationOfCell(location, CellVegetation.None);
            }

            if (!ImprovementLocationCanon.CanChangeOwnerOfPossession(newImprovement, location))
            {
                throw new ImprovementCreationException("Cannot assign the new improvement to its intended location");
            }
            ImprovementLocationCanon.ChangeOwnerOfPossession(newImprovement, location);

            if (isConstructed)
            {
                newImprovement.Construct();
            }
            else if (isPillaged)
            {
                newImprovement.Pillage();
            }

            allImprovements.Add(newImprovement);

            return(newImprovement);
        }
コード例 #6
0
        private void CreateCell(int x, int z, int i)
        {
            var position = new Vector3(
                (x + z * 0.5f - z / 2) * RenderConfig.InnerRadius * 2f,
                0f,
                z * RenderConfig.OuterRadius * 1.5f
                );

            var newCell = new HexCell(position, this, CellSignals);

            newCell.WorkerSlot = WorkerSlotFactory.BuildSlot(newCell);

            newCell.Index       = i;
            newCell.Coordinates = HexCoordinates.FromOffsetCoordinates(x, z);

            CellModificationLogic.ChangeTerrainOfCell(newCell, CellTerrain.Grassland);
            CellModificationLogic.ChangeShapeOfCell(newCell, CellShape.Flatlands);
            CellModificationLogic.ChangeVegetationOfCell(newCell, CellVegetation.None);

            cells.Add(newCell);
        }
コード例 #7
0
        public ICity Create(IHexCell location, ICivilization owner, string name)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }
            else if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            var newCityGameObject = GameObject.Instantiate(CityPrefab);

            Container.InjectGameObject(newCityGameObject);

            newCityGameObject.transform.position = location.AbsolutePosition;
            newCityGameObject.name = name;
            newCityGameObject.transform.SetParent(CityContainer, true);

            var newCity = newCityGameObject.GetComponent <City>();

            CityLocationCanon.ChangeOwnerOfPossession(newCity, location);

            location.SuppressSlot = true;

            CellModificationLogic.ChangeVegetationOfCell(location, CellVegetation.None);

            if (CityPossessionCanon.CanChangeOwnerOfPossession(newCity, owner))
            {
                CityPossessionCanon.ChangeOwnerOfPossession(newCity, owner);
            }
            else
            {
                throw new CityCreationException("Cannot assign the newly created city to its intended civilization");
            }

            var combatantTemplate = Container.Instantiate <CityCombatantTemplate>(new object[] { newCity });

            newCity.CombatFacade = UnitFactory.BuildUnit(location, combatantTemplate, owner);

            if (CellPossessionCanon.CanChangeOwnerOfPossession(location, newCity))
            {
                CellPossessionCanon.ChangeOwnerOfPossession(location, newCity);
            }
            else
            {
                throw new CityCreationException("Cannot assign the given location to the newly created city");
            }

            foreach (var neighbor in Grid.GetNeighbors(location))
            {
                if (CellPossessionCanon.CanChangeOwnerOfPossession(neighbor, newCity))
                {
                    CellPossessionCanon.ChangeOwnerOfPossession(neighbor, newCity);
                }
            }

            newCity.YieldFocus = YieldFocusType.TotalYield;

            newCity.Population = 1;

            allCities.Add(newCity);

            return(newCity);
        }
コード例 #8
0
        public void DecomposeCells(SerializableMapData mapData)
        {
            Grid.Build(mapData.CellCountX, mapData.CellCountZ);

            foreach (var cellData in mapData.HexCells)
            {
                var cellToModify = Grid.GetCellAtCoordinates(cellData.Coordinates);

                CellModificationLogic.ChangeTerrainOfCell(cellToModify, cellData.Terrain);
                CellModificationLogic.ChangeShapeOfCell(cellToModify, cellData.Shape);
                CellModificationLogic.ChangeVegetationOfCell(cellToModify, cellData.Vegetation);
                CellModificationLogic.ChangeFeatureOfCell(cellToModify, cellData.Feature);
                CellModificationLogic.ChangeHasRoadsOfCell(cellToModify, cellData.HasRoads);

                cellToModify.SuppressSlot = cellData.SuppressSlot;

                //Converging rivers (where two rivers combine and flow into a third) have
                //order-sensitive creation, since attempting to place both of the inflow
                //rivers before the outflow river has been created is invalid. To account for
                //this, we delay the creation of any invalid rivers (since those represent
                //an inflow being attached to another inflow) until after all other rivers
                //have been placed.
                var delayedRivers = new List <System.Tuple <IHexCell, HexDirection, RiverFlow> >();

                for (int i = 0; i < 6; i++)
                {
                    var edge = (HexDirection)i;

                    if (cellData.HasRiverAtEdge[i] && !RiverCanon.HasRiverAlongEdge(cellToModify, edge))
                    {
                        if (RiverCanon.CanAddRiverToCell(cellToModify, edge, cellData.DirectionOfRiverAtEdge[i]))
                        {
                            RiverCanon.AddRiverToCell(cellToModify, edge, cellData.DirectionOfRiverAtEdge[i]);
                        }
                        else
                        {
                            delayedRivers.Add(new System.Tuple <IHexCell, HexDirection, RiverFlow>(
                                                  cellToModify, edge, cellData.DirectionOfRiverAtEdge[i]
                                                  ));
                        }
                    }
                }

                foreach (var river in delayedRivers)
                {
                    if (RiverCanon.CanAddRiverToCell(river.Item1, river.Item2, river.Item3))
                    {
                        RiverCanon.AddRiverToCell(river.Item1, river.Item2, river.Item3);
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "Failed to decompose river ({0}, {1}, {2})",
                                                                river.Item1, river.Item2, river.Item3
                                                                ));
                    }
                }

                cellToModify.WorkerSlot.IsOccupied = cellData.IsSlotOccupied;
                cellToModify.WorkerSlot.IsLocked   = cellData.IsSlotLocked;
            }

            foreach (var chunk in Grid.Chunks)
            {
                chunk.Refresh(MapRendering.TerrainRefreshType.All);
            }
        }