Ejemplo n.º 1
0
        private void CheckFreeMovingToBonus(BonusIDType bonusID)
        {
            var availInBonus = _income.BonusRestrictions[bonusID] - _armiesUsedOnBonuses.ValueOrZero(bonusID);

            Assert.Fatal(availInBonus > 0, "CheckFreeMovingToBonus called with 0 armies available to move");

            foreach (var terrID in _map.Bonuses[bonusID].Territories)
            {
                var free = _freeArmiesUsedOn.ValueOrZero(terrID);

                if (free > 0)
                {
                    var toMove = SharedUtility.MathMin(free, availInBonus);

                    _freeArmiesUsedOn.AddTo(terrID, -toMove);
                    _armiesUsedOnBonuses.AddTo(bonusID, toMove);
                    availInBonus -= toMove;

                    if (availInBonus == 0)
                    {
                        return;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <param name="numArmies">Can be negative to indicate we're freeing armies we previously used.</param>
        /// <returns>
        /// True if the change was successful. False if we couldn't do it.
        /// </returns>
        public bool TryRecordUsedArmies(TerritoryIDType territoryID, int numArmies)
        {
            Assert.Fatal(_income != null, "_income is null");
            if (numArmies == 0)
            {
                return(true); //no-op
            }
            else if (numArmies < 0)
            {
                return(TryRemoveUsedArmies(territoryID, -numArmies));
            }


            if (_income.BonusRestrictions.Count > 0)
            {
                var td = _map.Territories[territoryID];

                foreach (var bonusID in td.PartOfBonuses)
                {
                    if (_income.BonusRestrictions.ContainsKey(bonusID))
                    {
                        var used  = _armiesUsedOnBonuses.ValueOrZero(bonusID);
                        var avail = _income.BonusRestrictions[bonusID];
                        if (avail > used)
                        {
                            var toUse = SharedUtility.MathMin(numArmies, avail - used); //can't name a variable "use" in actionscript

                            if (_income.FreeArmies - _freeArmiesUsedOn.Values.Sum() < numArmies - toUse)
                            {
                                return(false); //We have some armies we could award from this bonus, but we wouldn't have enough free armies to award the rest of what's requested.  Therefore, abort now before recording bonus armies, so that the function doesn't change something and also return false.
                            }
                            _armiesUsedOnBonuses.AddTo(bonusID, toUse);
                            _freeArmiesUsedOn.AddTo(territoryID, numArmies - toUse);

                            return(true);
                        }
                    }
                }
            }

            //Take free armies.
            int freeAvail = _income.FreeArmies - _freeArmiesUsedOn.Values.Sum();

            if (freeAvail < numArmies)
            {
                return(false);
            }

            _freeArmiesUsedOn.AddTo(territoryID, numArmies);
            return(true);
        }