Esempio n. 1
0
        /// <summary>
        /// Constructs a comparison that orders cells based on the amount of a particular
        /// resource type they generate for the argued city.
        /// </summary>
        /// <remarks>
        /// The effective yield is ordered from smallest expected yield to largest.
        /// Unlike <ref>CellComparisonUtil.BuildFocusedComparisonAscending</ref>, no
        /// additional comparisons are made beyond the yield of the specified resource
        /// </remarks>
        /// <param name="sourceCity">The city whose hypothetical yield is being considered</param>
        /// <param name="focusedYield">The YieldType to be maximized</param>
        /// <param name="generationLogic">The IResourceGenerationLogic used to determine the yield for the argued city</param>
        /// <returns>A Comparison that'll sort IHexCells from least to most yield of the focused resource</returns>
        public static Comparison <IHexCell> BuildYieldComparisonAscending(
            YieldType focusedYield, ICity sourceCity, IYieldGenerationLogic generationLogic
            )
        {
            return(delegate(IHexCell firstCell, IHexCell secondCell) {
                var firstYield = generationLogic.GetYieldOfCellForCity(firstCell, sourceCity);
                var secondYield = generationLogic.GetYieldOfCellForCity(secondCell, sourceCity);

                return firstYield[focusedYield].CompareTo(secondYield[focusedYield]);
            });
        }
        /// <summary>
        /// Constructs a comparison that orders slots based on the amount of a particular
        /// resource type they generate for the argued city.
        /// </summary>
        /// <remarks>
        /// The effective yield is ordered from smallest expected yield to largest.
        /// Unlike <ref>SlotComparisonUtil.BuildFocusedComparisonAscending</ref>, no
        /// additional comparisons are made beyond the yield of the specified resource
        /// </remarks>
        /// <param name="sourceCity">The city whose hypothetical yield is being considered</param>
        /// <param name="focusedYield">The ResourceType to be maximized</param>
        /// <param name="generationLogic">The IResourceGenerationLogic used to determine the yield for the argued city</param>
        /// <returns>A Comparison that'll sort IWorkerSlots from least to most yield of the focused resource</returns>
        public static Comparison <IWorkerSlot> BuildYieldComparisonAscending(
            YieldType focusedYield, ICity sourceCity, IYieldGenerationLogic generationLogic
            )
        {
            return(delegate(IWorkerSlot firstSlot, IWorkerSlot secondSlot) {
                var firstYield = GetSlotYield(firstSlot, sourceCity, generationLogic);
                var secondYield = GetSlotYield(secondSlot, sourceCity, generationLogic);

                return firstYield[focusedYield].CompareTo(secondYield[focusedYield]);
            });
        }
Esempio n. 3
0
        public override double CalcYield(YieldType type, Tile tile)
        {
            double result = 0;

            // ReSharper disable once LoopCanBeConvertedToQuery - high frequenzy code! even tho its test code
            foreach (var modifer in _modifers)
            {
                if (modifer.IsApplicable(tile))
                {
                    result = modifer.Modify(type, tile, result);
                }
            }
            return(result);
        }
        public float this[YieldType index] {
            get {
                switch (index)
                {
                case YieldType.Food:           return(Food);

                case YieldType.Production:     return(Production);

                case YieldType.Gold:           return(Gold);

                case YieldType.Culture:        return(Culture);

                case YieldType.Science:        return(Science);

                case YieldType.GreatArtist:    return(GreatArtist);

                case YieldType.GreatEngineer:  return(GreatEngineer);

                case YieldType.GreatMerchant:  return(GreatMerchant);

                case YieldType.GreatScientist: return(GreatScientist);

                default: throw new IndexOutOfRangeException();
                }
            }
            set {
                switch (index)
                {
                case YieldType.Food:           Food = value; break;

                case YieldType.Production:     Production = value; break;

                case YieldType.Gold:           Gold = value; break;

                case YieldType.Culture:        Culture = value; break;

                case YieldType.Science:        Science = value; break;

                case YieldType.GreatArtist:    GreatArtist = value; break;

                case YieldType.GreatEngineer:  GreatEngineer = value; break;

                case YieldType.GreatMerchant:  GreatMerchant = value; break;

                case YieldType.GreatScientist: GreatScientist = value; break;

                default: throw new IndexOutOfRangeException();
                }
            }
        }
Esempio n. 5
0
        private void BringYieldTypeToMin(
            YieldType type, float minYield, HomelandData homelandData,
            ref YieldSummary currentYield
            )
        {
            float yieldDeficit = minYield - currentYield[type];

            var strategyWeightsByRegion = new Dictionary <MapRegion, Dictionary <IBalanceStrategy, int> >();

            foreach (var region in homelandData.AllRegions)
            {
                strategyWeightsByRegion[region] = homelandData.GetDataOfRegion(region).GetBalanceStrategyWeights();
            }

            int iterations = homelandData.AllRegions.Count() * 10;

            List <MapRegion> regions = homelandData.AllRegions.ToList();

            while (yieldDeficit > 0 && iterations-- > 0)
            {
                if (regions.Count == 0)
                {
                    regions = homelandData.AllRegions.ToList();
                }

                var region = regions.Random();
                regions.Remove(region);

                var strategyWeights = strategyWeightsByRegion[region];

                var strategy = GetStrategy(strategyWeights);

                YieldSummary yieldAdded;
                if (strategy.TryIncreaseYield(region, homelandData.GetDataOfRegion(region), type, out yieldAdded))
                {
                    yieldDeficit -= yieldAdded[type];
                    currentYield += yieldAdded;
                }
            }

            if (currentYield[type] < minYield)
            {
                Debug.LogWarningFormat("Failed to bring yield type {0} to min yield {1}", type, minYield);
            }
        }
Esempio n. 6
0
        public bool TryIncreaseYield(
            MapRegion region, RegionData regionData, YieldType type, out YieldSummary yieldAdded
            )
        {
            var availableResources = BonusResourcesWithYield[type].ToList();

            while (availableResources.Count > 0)
            {
                var chosenResource = ResourceRandomSampler.SampleElementsFromSet(
                    availableResources, 1, GetResourceSelectionWeightFunction(region, regionData)
                    ).FirstOrDefault();

                if (chosenResource == null)
                {
                    break;
                }

                var cell = CellRandomSampler.SampleElementsFromSet(
                    region.Cells, 1, GetCellPlacementWeightFunction(chosenResource)
                    ).FirstOrDefault();

                if (cell != null)
                {
                    var oldYield = YieldEstimator.GetYieldEstimateForCell(cell, TechCanon.AvailableTechs);

                    int copies = chosenResource.Type == ResourceType.Strategic
                               ? StrategicCopiesLogic.GetWeightedRandomCopies()
                               : 0;

                    ResourceNodeFactory.BuildNode(cell, chosenResource, copies);

                    yieldAdded = YieldEstimator.GetYieldEstimateForCell(cell, TechCanon.AvailableTechs) - oldYield;
                    return(true);
                }
                else
                {
                    availableResources.Remove(chosenResource);
                }
            }

            yieldAdded = YieldSummary.Empty;
            return(false);
        }
Esempio n. 7
0
        /// <summary>
        /// Constructs a comparison that orders cells based on the amount of a particular
        /// resource type they would generate for the argued city, with other considerations
        /// in case of ties.
        /// </summary>
        /// <remarks>
        /// The effective yield is ordered from the smallest expected yield to the largest.
        /// Ties are broken first by the amount of food the resources yield, then by total
        /// yield on the tile. Both tiebreakers are organized in ascending order
        /// </remarks>
        /// <param name="sourceCity">The city whose hypothetical yield is being considered</param>
        /// <param name="focusedResource">The ResourceType to be maximized</param>
        /// <param name="generationLogic">The IResourceGenerationLogic used to determine the yield for the argued city</param>
        /// <returns>A Comparison that'll sort IHexCells from least to most yield of the focused resource</returns>
        public static Comparison <IHexCell> BuildFocusedComparisonAscending(ICity sourceCity,
                                                                            YieldType focusedResource, IYieldGenerationLogic generationLogic)
        {
            return(delegate(IHexCell firstCell, IHexCell secondCell) {
                var firstYield = generationLogic.GetYieldOfCellForCity(firstCell, sourceCity);
                var secondYield = generationLogic.GetYieldOfCellForCity(secondCell, sourceCity);

                var focusComparison = firstYield[focusedResource].CompareTo(secondYield[focusedResource]);
                if (focusComparison == 0)
                {
                    focusComparison = firstYield[YieldType.Food].CompareTo(secondYield[YieldType.Food]);
                }
                if (focusComparison == 0)
                {
                    focusComparison = firstYield.Total.CompareTo(secondYield.Total);
                }

                return focusComparison;
            });
        }
        /// <summary>
        /// Constructs a comparison that orders slots based on the amount of a particular
        /// resource type they generate for the argued city.
        /// </summary>
        /// <remarks>
        /// The effective yield is ordered from the smallest expected yield to the largest.
        /// Ties are broken first by the amount of food the resources yield, then by total
        /// yield on the tile. Both tiebreakers are organized in ascending order
        /// </remarks>
        /// <param name="sourceCity">The city whose hypothetical yield is being considered</param>
        /// <param name="focusedResource">The ResourceType to be maximized</param>
        /// <param name="generationLogic">The IResourceGenerationLogic used to determine the yield for the argued city</param>
        /// <returns>A Comparison that'll sort IWorkerSlots from least to most yield of the focused resource</returns>
        public static Comparison <IWorkerSlot> BuildFocusedComparisonAscending(ICity sourceCity,
                                                                               YieldType focusedResource, IYieldGenerationLogic generationLogic)
        {
            return(delegate(IWorkerSlot firstSlot, IWorkerSlot secondSlot) {
                var firstYield = GetSlotYield(firstSlot, sourceCity, generationLogic);
                var secondYield = GetSlotYield(secondSlot, sourceCity, generationLogic);

                var focusComparison = firstYield[focusedResource].CompareTo(secondYield[focusedResource]);
                if (focusComparison == 0)
                {
                    focusComparison = firstYield[YieldType.Food].CompareTo(secondYield[YieldType.Food]);
                }
                if (focusComparison == 0)
                {
                    focusComparison = firstYield.Total.CompareTo(secondYield.Total);
                }

                return focusComparison;
            });
        }
Esempio n. 9
0
        public bool TryIncreaseYield(
            MapRegion region, RegionData regionData, YieldType type, out YieldSummary yieldAdded
            )
        {
            if (type != YieldType.Food)
            {
                yieldAdded = YieldSummary.Empty;
                return(false);
            }

            var candidates = region.Cells.Where(GetLakeCandidateFilter(region));

            if (candidates.Any())
            {
                var newLake = candidates.Random();

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

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

                ModLogic.ChangeTerrainOfCell(newLake, CellTerrain.FreshWater);

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

                return(true);
            }
            else
            {
                yieldAdded = YieldSummary.Empty;
                return(false);
            }
        }
        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 bool TryIncreaseYield(MapRegion region, RegionData regionData, YieldType type, out YieldSummary yieldAdded)
 {
     yieldAdded = YieldSummary.Empty;
     return(false);
 }
Esempio n. 12
0
 public override double Modify(YieldType type, Tile tile, double input)
 {
     return(input + Modifiers[type.Index]);
 }
Esempio n. 13
0
 public BuildingYieldChange(YieldType yieldType, float byHowMuch)
 {
     this.yieldType = yieldType;
     this.byHowMuch = byHowMuch;
 }
Esempio n. 14
0
        void DispatchYield(IEnumerator runner, object yieldResult)
        {
            if (!_active)
            {
                return;
            }
            if (yieldResult is YieldType)
            {
                YieldType y = (YieldType)yieldResult;
                switch (y.id)
                {
                case KEY_UPDATE:
                    AddTo(runner, _updateQueue);
                    break;

                case KEY_FIXED:
                    AddTo(runner, _fixedQueue);
                    break;

                case KEY_LATE:
                    AddTo(runner, _lateQueue);
                    break;

                case KEY_ANY:
                    AddTo(runner, _anyMainQueue);
                    break;

                case KEY_ASYN:
                    AddTo(runner, _asynQueue);
                    break;

                default:
                    Debug.LogError("yield result id error=====>" + y.id);
                    break;
                }
            }
            else if (yieldResult is float || yieldResult is int)
            {
                float nextTime = (float)yieldResult + _time;
                lock (_timer)
                {
                    int i = _timer.Count - 1;
                    while (i >= 0)
                    {
                        if (nextTime < _timer[i])
                        {
                            break;
                        }
                        i--;
                    }
                    i++;
                    if (i == _timer.Count)
                    {
                        _timer.Add(nextTime);
                        _timeRunner.Add(runner);
                    }
                    else
                    {
                        _timer.Insert(i, nextTime);
                        _timeRunner.Insert(i, runner);
                    }
                }
            }
            else
            {
                Debug.LogError("yield result id error=====>" + yieldResult.GetType().FullName + ":" + yieldResult.ToString());
            }
        }