示例#1
0
        public static Army operator +(Army a, Army b)
        {
            if (a == null && b == null)
            {
                return(null);
            }

            Army result = new Army();

            if ((a == null) != (b == null))
            {
                foreach (var kvp in a ?? b)
                {
                    result[kvp.Key] = kvp.Value;
                }
            }
            else
            {
                foreach (var kvp in a)
                {
                    result[kvp.Key] = kvp.Value;
                }

                foreach (var kvp in b)
                {
                    result[kvp.Key] = result.ContainsKey(kvp.Key)
                        ? result[kvp.Key] + kvp.Value
                        : kvp.Value;
                }
            }

            return(result);
        }
示例#2
0
        public static Army Max(params Army[] armies)
        {
            var result = new Army();

            foreach (var army in armies.Where(a => a != null))
            {
                foreach (var type in army.Keys)
                {
                    if (!result.ContainsKey(type))
                    {
                        result.Add(type, army[type]);
                    }
                    else if (result[type] < army[type])
                    {
                        result[type] = army[type];
                    }
                }
            }

            return(result);
        }
示例#3
0
        public JSON.Army CalculatePossibleArmyRecruitment(TimeSpan timeSpan, List <JSON.TroopType> troopTypes, Dictionary <JSON.TroopType, int> maxCounts = null)
        {
            //  Keep time span reasonable so we don't get integer overflow
            if (timeSpan.TotalDays > 30)
            {
                timeSpan = TimeSpan.FromDays(30);
            }

            var result = new JSON.Army();

            foreach (var type in troopTypes)
            {
                result.Add(type, 0);
            }

            var typesBySource = troopTypes.GroupBy(t => Native.ArmyStats.UnitSource[t]).ToDictionary((s) => s.Key, s => s.Select(t => t));

            //  Simulate recruitment by time interval
            var interval      = TimeSpan.FromHours(6);
            var simulatedTime = TimeSpan.Zero;
            int totalPop      = 0;
            int previousPop   = -1;

            bool ReachedCountLimit(JSON.TroopType troopType)
            {
                return(maxCounts != null && maxCounts.ContainsKey(troopType) && result[troopType] >= maxCounts[troopType]);
            }

            while (simulatedTime < timeSpan && totalPop < MaxPopulation && totalPop != previousPop)
            {
                foreach (var source in typesBySource.Keys)
                {
                    var types = typesBySource[source].Where(t => !ReachedCountLimit(t));

                    var sumTimeSeconds = types.Sum(t => Native.ArmyStats.BaseRecruitTime[t].TotalSeconds);
                    foreach (var type in types)
                    {
                        var factor = 1 - Native.ArmyStats.BaseRecruitTime[type].TotalSeconds / sumTimeSeconds;

                        //  If there's only 1 unit type for this building
                        if (factor <= float.Epsilon)
                        {
                            factor = 1;
                        }

                        var troops = CalculatePossibleUnitRecruitment(type, interval * factor);
                        result[type] += troops;
                    }
                }

                if (maxCounts != null)
                {
                    foreach (var type in maxCounts.Keys)
                    {
                        if (!result.ContainsKey(type))
                        {
                            continue;
                        }

                        if (result[type] > maxCounts[type])
                        {
                            result[type] = maxCounts[type];
                        }
                    }
                }

                //  Keep track of previous/total in case we reach max counts before time limit or pop limit are reached
                //  (otherwise we may get infinite loop)
                previousPop    = totalPop;
                totalPop       = Native.ArmyStats.CalculateTotalPopulation(result);
                simulatedTime += interval;
            }

            return(result);
        }