protected override void EditCell(IHexCell cell)
 {
     if (IsPainting && ModLogic.CanChangeFeatureOfCell(cell, ActiveFeature))
     {
         ModLogic.ChangeFeatureOfCell(cell, ActiveFeature);
     }
 }
        public void RazeCity(ICity city)
        {
            var cityLocation = CityLocationCanon.GetOwnerOfPossession(city);

            city.Destroy();

            if (cityLocation.Feature == CellFeature.None && CellModLogic.CanChangeFeatureOfCell(cityLocation, CellFeature.CityRuins))
            {
                CellModLogic.ChangeFeatureOfCell(cityLocation, CellFeature.CityRuins);
            }
        }
        public bool TryIncreaseYield(
            MapRegion region, RegionData regionData, YieldType type, out YieldSummary yieldAdded
            )
        {
            yieldAdded = YieldSummary.Empty;
            if (type != YieldType.Food && type != YieldType.Gold)
            {
                return(false);
            }

            var newOasis = CellRandomSampler.SampleElementsFromSet(
                region.LandCells, 1, GetOasisCandidateWeightFunction(region)
                ).FirstOrDefault();

            if (newOasis == null)
            {
                return(false);
            }

            var oldYields = new Dictionary <IHexCell, YieldSummary>();

            foreach (var cell in Grid.GetCellsInRadius(newOasis, 1))
            {
                oldYields[cell] = YieldEstimator.GetYieldEstimateForCell(cell, TechCanon.AvailableTechs);
            }

            ModLogic.ChangeFeatureOfCell(newOasis, CellFeature.Oasis);

            yieldAdded = YieldSummary.Empty;
            foreach (var cell in oldYields.Keys)
            {
                yieldAdded += YieldEstimator.GetYieldEstimateForCell(cell, TechCanon.AvailableTechs) - oldYields[cell];
            }

            return(true);
        }
        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);
            }
        }