예제 #1
0
        // Gets a GridCell object for the starting cell of the player.
        private GridCell GetStartingCell()
        {
            int cellCount = this.grid.DryCells.Count;
            int index     = RandomNumber.Between(0, cellCount);

            return(this.grid.DryCells[index]);
        }
예제 #2
0
        //gets an array of points representing the center points
        //of islands or continents on the map.  These points are
        //used to generate the continents and islands.
        private List <Point> GetLandSeeds(Grid grid, Landmass worldLandmass)
        {
            int numSeeds = 0;

            switch (worldLandmass)
            {
            case Landmass.Archipelago:
                numSeeds = 12;
                break;

            case Landmass.Pangaea:
                numSeeds = 1;
                break;

            case Landmass.Continents:
                numSeeds = 6;
                break;
            }

            List <Point> seeds  = new List <Point>();
            Point        center = new Point(grid.Size.Width / 2, grid.Size.Height / 2);
            int          x;
            int          y;

            for (int i = 0; i < numSeeds; i++)
            {
                x = RandomNumber.Between(0, grid.Size.Width);
                y = RandomNumber.Between(0, grid.Size.Height);
                seeds.Add(new Point(x, y));
            }

            return(seeds);
        }
예제 #3
0
        private bool IsBombardmentSuccessfull()
        {
            int  chance  = CalculateChanceForBombardmentSuccess();
            int  rand    = RandomNumber.Between(0, 100);
            bool success = (rand >= (100 - chance));

            return(success);
        }
예제 #4
0
        /// <summary>
        /// Attempt to sabotage the current production item in the foreign city.
        /// This can only take place if there is a spy within the foreign country.
        /// </summary>
        /// <returns></returns>
        public EspionageResult Sabotage(City foreignCity)
        {
            if (foreignCity == null)
            {
                throw new ArgumentNullException("foreignCity");
            }
            if (!this.hasEmbassy)
            {
                throw new InvalidOperationException(ServerResources.EmbassyRequired);
            }
            if (!this.hasSpy)
            {
                throw new InvalidOperationException(ServerResources.SpyRequired);
            }
            int             randNum;
            EspionageResult result;

            if (this.foreignCountry.Government.Fallback)
            {
                return(EspionageResult.ImmuneToEspionage);
            }

            randNum = RandomNumber.Between(1, 100);
            if (randNum >= 25)
            {
                //success, but is the spy captured?  25% chance of this happening.
                randNum = RandomNumber.Between(1, 100);
                result  = randNum > 25 ? EspionageResult.Success : EspionageResult.SuccessWithCapturedSpy;
                foreignCity.SabotageProduction();
            }
            else
            {
                //failure, but is the spy captured?  50/50 chance of this happening.
                randNum = RandomNumber.Between(1, 100);
                result  = randNum > 50 ? EspionageResult.Failure : EspionageResult.SpyCaught;
            }

            if (result == EspionageResult.SpyCaught)
            {
                this.foreignCountry.CaptureSpy(this, EspionageAction.SabotageProduction, false, foreignCity);
                this.hasSpy = false;
            }
            else if (result == EspionageResult.SuccessWithCapturedSpy)
            {
                this.foreignCountry.CaptureSpy(this, EspionageAction.SabotageProduction, true, foreignCity);
                this.hasSpy = false;
            }

            return(result);
        }
예제 #5
0
        /// <summary>
        /// Attempts to expose a spy in the embassy.
        /// </summary>
        /// <returns></returns>
        public EspionageResult ExposeSpy()
        {
            if (!this.hasEmbassy)
            {
                throw new InvalidOperationException(ServerResources.EmbassyRequired);
            }

            if (this.foreignCountry.Government.Fallback)
            {
                return(EspionageResult.ImmuneToEspionage);
            }

            EspionageResult result  = EspionageResult.Failure;
            int             randNum = RandomNumber.Between(1, 100);

            if (randNum <= 75)
            {
                //success.  spy captured?  35% chance.
                randNum = RandomNumber.Between(1, 100);
                if (randNum < 35)
                {
                    //spy captured.
                    result = EspionageResult.SuccessWithCapturedSpy;
                    this.foreignCountry.CaptureSpy(this, EspionageAction.ExposeSpy, true, this.foreignCountry.CapitalCity);
                    this.hasSpy = false;
                }
                else
                {
                    result = EspionageResult.Success;
                }
            }
            else
            {
                //failure.  spy captured? 50/50 chance.
                randNum = RandomNumber.Between(1, 100);
                if (randNum < 50)
                {
                    //spy caught
                    result = EspionageResult.SpyCaught;
                    this.foreignCountry.CaptureSpy(this, EspionageAction.ExposeSpy, false, this.foreignCountry.CapitalCity);
                    this.hasSpy = false;
                }
                else
                {
                    result = EspionageResult.Failure;
                }
            }
            return(result);
        }
예제 #6
0
        //Returns a Resource reference for the given cell.  Chances are,
        //this will be null.
        private Resource FindResourceForCell(GridCell cell)
        {
            int      randResult = RandomNumber.UpTo(10);
            Resource resouce    = null;

            if (randResult == 5)    //10% chance of this happening.
            {
                List <Resource> possibleResources = FindPossibleResourcesForTerrain(cell.Terrain);

                if (possibleResources.Count > 0)
                {
                    int index = RandomNumber.Between(0, possibleResources.Count - 1);
                    resouce = possibleResources[index];
                }
            }
            return(resouce);
        }
예제 #7
0
        /// <summary>
        /// If successful, Stealing foreign plans will reveal the positions
        /// of all the foreign colonys' military units.
        /// </summary>
        /// <remarks>Unlike other espionage activities, when stealing plans
        /// from enemy countries, failure always means the spy is caught.</remarks>
        /// <returns></returns>
        public EspionageResult StealPlans()
        {
            if (!this.hasEmbassy)
            {
                throw new InvalidOperationException(ServerResources.EmbassyRequired);
            }
            if (!this.hasSpy)
            {
                throw new InvalidOperationException(ServerResources.SpyRequired);
            }
            int             randNum;
            EspionageResult result;

            if (this.foreignCountry.Government.Fallback)
            {
                return(EspionageResult.ImmuneToEspionage);
            }

            randNum = RandomNumber.Between(1, 100);
            if (randNum <= 75)
            {
                //success.  There is still a chance, however, that the
                //spy was caught in the process.
                randNum = RandomNumber.Between(1, 100);
                if (randNum <= 35)
                {
                    result = EspionageResult.SuccessWithCapturedSpy;
                    this.foreignCountry.CaptureSpy(this, EspionageAction.StealPlans, true, this.foreignCountry.CapitalCity);
                    this.hasSpy = false;
                }
                else
                {
                    result = EspionageResult.Success;
                }
            }
            else
            {
                //failure.  spy caught.
                result = EspionageResult.SpyCaught;
                this.foreignCountry.CaptureSpy(this, EspionageAction.StealPlans, false, this.foreignCountry.CapitalCity);
                this.hasSpy = false;
            }

            return(result);
        }
예제 #8
0
        /// <summary>
        /// Attempts to plant a spy within the foreign country.
        /// </summary>
        /// <remarks>
        /// There's a 50% chance this will succeed.  If it fails, however,
        /// the foreign country will immediately become furious with your
        /// country.  If it succeeds, your spy can sabotage production and
        /// spread propaganda.</remarks>
        /// <returns></returns>
        public EspionageResult PlantSpy()
        {
            if (!this.hasEmbassy)
            {
                throw new InvalidOperationException(ServerResources.EmbassyRequired);
            }
            int  randomNum = RandomNumber.Between(1, 100);
            bool success   = (randomNum >= 50);

            if (success)
            {
                this.hasSpy = true;
                return(EspionageResult.Success);
            }

            this.foreignCountry.CaptureSpy(this, EspionageAction.PlantSpy, false, this.foreignCountry.CapitalCity);
            return(EspionageResult.SpyCaught);
        }
예제 #9
0
        /// <summary>
        /// Attempt to steal the world map of the foreign colony.
        /// If this is successfull, all the territory the rival
        /// colony has explored will become visible to the parent
        /// colony.
        /// </summary>
        /// <returns></returns>
        public EspionageResult StealWorldMap()
        {
            if (!this.hasEmbassy)
            {
                throw new InvalidOperationException(ServerResources.EmbassyRequired);
            }
            if (!this.hasSpy)
            {
                throw new InvalidOperationException(ServerResources.SpyRequired);
            }
            if (this.foreignCountry.Government.Fallback)
            {
                return(EspionageResult.ImmuneToEspionage);
            }
            int             randNum;
            EspionageResult result;

            randNum = RandomNumber.Between(1, 100);
            if (randNum <= 75)
            {
                result = EspionageResult.Success;
            }
            else
            {
                //failure.  spy caught?
                randNum = RandomNumber.Between(1, 100);
                if (randNum <= 50)
                {
                    result = EspionageResult.SpyCaught;
                    this.foreignCountry.CaptureSpy(this, EspionageAction.StealWorldMap, false, this.foreignCountry.CapitalCity);
                    this.hasSpy = false;
                }
                else
                {
                    result = EspionageResult.Failure;
                }
            }
            return(result);
        }
예제 #10
0
        /// <summary>
        /// Engages the unit in combat with an enemy.
        /// </summary>
        /// <param name="defender">The enemy to engage in combat with.</param>
        /// <returns>a <see cref="CombatResult"/> enumeration representing the results of the
        /// fight.</returns>
        protected virtual CombatResult Combat(Unit defender)
        {
            if (defender == null)
            {
                throw new ArgumentNullException("defender");
            }

            int          defendersDefensivePower;
            int          chanceToWin;
            int          randNum;
            bool         combatOver;
            CombatResult retval = CombatResult.Unresolved;

            OnCombatStarted(new CombatEventArgs(defender));
            defendersDefensivePower = defender.CalculateDefensivePower();

            //calculate the chance to win.  (will be between 1 and 100)
            chanceToWin = CalcAttackersChanceToWin(this.OffensivePower, defendersDefensivePower);

            combatOver = false;

            do
            {
                randNum = RandomNumber.Between(1, 100);
                if (randNum <= chanceToWin)
                {
                    //attacker wins round
                    defender.LoseHitPoint();

                    if (defender.HitPoints == 1 && defender.Fast && !this.Fast)
                    {
                        combatOver = defender.Retreat();
                        if (combatOver)
                        {
                            retval = CombatResult.Unresolved;
                        }
                    }
                    else if (defender.HitPoints <= 0)
                    {
                        combatOver = true;
                        retval     = CombatResult.Win;
                    }
                }
                else
                {
                    //defender wins round
                    LoseHitPoint();

                    if (this.HitPoints == 1 && this.Fast && !defender.Fast)
                    {
                        //attempt to retreat.
                        combatOver = Retreat();
                        if (combatOver)
                        {
                            retval = CombatResult.Unresolved;
                        }
                    }
                    else if (this.HitPoints <= 0)
                    {
                        combatOver = true;
                        retval     = CombatResult.Killed;
                    }
                }
            } while (!combatOver);

            return(retval);
        }
예제 #11
0
        /// <summary>
        /// Bombards the given cell.
        /// </summary>
        /// <remarks>Not all bombardment attempts are successfull.  To determine the
        /// outcome of an attempt, review the <i>BombardmentResult</i> return value.</remarks>
        /// <param name="cell"></param>
        public BombardmentResult Bombard(GridCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException("cell");
            }

            if (!this.CanBombard)
            {
                throw new InvalidOperationException(ServerResources.UnitCannotBombard);
            }

            bool success = IsBombardmentSuccessfull();

            if (!success)
            {
                return(BombardmentResult.Failed);
            }

            if (cell.HasCity)
            {
                City city = cell.City;
                if (city.Improvements.Count > 0)
                {
                    //which improvement was destroyed?
                    int         bound     = city.Improvements.Count - 1;
                    int         idx       = RandomNumber.Between(0, bound);
                    Improvement destroyed = city.Improvements[idx];
                    city.DestroyImprovement(destroyed, this.country);
                    return(BombardmentResult.SucceededDestroyingCityImprovement);
                }
                else if (cell.Units.Count > 0)
                {
                    int bound = cell.Units.Count - 1;
                    int idx   = RandomNumber.Between(0, bound);
                    cell.Units[idx].LoseHitPoint();
                    return(BombardmentResult.SucceededInjuredUnit);
                }
                else if (city.Population > 1)
                {
                    city.BombardPopulation(this.country);
                    return(BombardmentResult.SucceededKilledCitizens);
                }
            }
            else
            {
                if (cell.IsIrrigated)
                {
                    cell.IsIrrigated = false;
                    return(BombardmentResult.SucceededDestroyingCellImprovement);
                }
                else if (cell.HasMine)
                {
                    cell.HasMine = false;
                    return(BombardmentResult.SucceededDestroyingCellImprovement);
                }
                else if (cell.HasRailroad)
                {
                    cell.HasRailroad = false;
                    cell.HasRoad     = true;
                    return(BombardmentResult.SucceededDestroyingCellImprovement);
                }
                else if (cell.HasRoad)
                {
                    cell.HasRoad = false;
                    return(BombardmentResult.SucceededDestroyingCellImprovement);
                }
            }

            return(BombardmentResult.Failed);
        }
예제 #12
0
        /// <summary>
        /// Attempts to get a foreign city to defect to the parent country by
        /// spreading propaganda.  Cities in anarchy are immune to proaganda.
        /// </summary>
        /// <param name="foreignCity"></param>
        /// <returns></returns>
        public EspionageResult SpreadPropaganda(City foreignCity)
        {
            if (!this.hasEmbassy)
            {
                throw new InvalidOperationException(ServerResources.EmbassyRequired);
            }
            if (!this.hasSpy)
            {
                throw new InvalidOperationException(ServerResources.SpyRequired);
            }
            //TODO: add code to account for continued resistance.
            //TODO: add code to account for more advanced governments.
            if (foreignCity == null)
            {
                throw new ArgumentNullException("foreignCity");
            }
            CulturalPerception perception;
            EspionageResult    result = EspionageResult.Failure;
            bool propogandaSpread;
            int  chanceForPropaganda          = 0;
            int  chanceForResistance          = 0;
            int  chanceForContinuedResistance = 0;

            //a quick check: if the foreign city is in anarchy,
            //the proganda campaign will automatically fail
            //(although there is no chance the spy will be caught).
            if (this.foreignCountry.Government.Fallback)
            {
                result = EspionageResult.Failure;
                return(result);
            }

            //there are 2 steps to taking over a city with propaganda.  The
            //first step is successfully spreading propaganda, and the second
            //step is not having that proaganda resisted.  These two taken
            //together make it very difficult for overall proaganda to succeed.
            perception = this.foreignCountry.GetCulturalPerception(this.parentCountry);

            switch (perception)
            {
            case CulturalPerception.InAwe:
                chanceForPropaganda          = 30;
                chanceForResistance          = 40;
                chanceForContinuedResistance = 30;
                break;

            case CulturalPerception.Admirer:
                chanceForPropaganda          = 25;
                chanceForResistance          = 50;
                chanceForContinuedResistance = 40;
                break;

            case CulturalPerception.Impressed:
                chanceForPropaganda          = 20;
                chanceForResistance          = 60;
                chanceForContinuedResistance = 50;
                break;

            case CulturalPerception.Unimpressed:
                chanceForPropaganda          = 10;
                chanceForResistance          = 70;
                chanceForContinuedResistance = 60;
                break;

            case CulturalPerception.Dismissive:
                chanceForPropaganda          = 5;
                chanceForResistance          = 80;
                chanceForContinuedResistance = 70;
                break;

            case CulturalPerception.Disdainful:
                chanceForPropaganda          = 3;
                chanceForResistance          = 90;
                chanceForContinuedResistance = 80;
                break;
            }

            int randNum = RandomNumber.Between(1, 100);

            if (randNum <= chanceForPropaganda)
            {
                propogandaSpread = true;
            }
            else
            {
                propogandaSpread = false;
                result           = EspionageResult.Failure;
            }

            if (propogandaSpread)
            {
                randNum = RandomNumber.Between(1, 100);
                if (randNum <= chanceForResistance)
                {
                    result = EspionageResult.Failure;
                }
                else
                {
                    //the propaganda was successfull.  The city now belongs
                    //to the parent.
                    result = EspionageResult.Success;
                    this.parentCountry.Cities.Add(foreignCity);
                    this.foreignCountry.Cities.Remove(foreignCity);
                    foreignCity.ParentCountry = this.parentCountry;
                }
            }

            if (result == EspionageResult.Failure)
            {
                //spy caught?
                randNum = RandomNumber.Between(1, 100);
                if (randNum >= 50)
                {
                    result = EspionageResult.SpyCaught;
                    this.foreignCountry.CaptureSpy(this, EspionageAction.SpreadPropaganda, false, foreignCity);
                    this.hasSpy = false;
                }
            }

            return(result);
        }