Esempio n. 1
0
        private void runSimulationButton_Click(object sender, EventArgs e)
        {
            battleResultTextBox.Clear();
            try
            {
                ValidateFileInput(fleet1TextBox, "fleet 1");
                ValidateFileInput(fleet2TextBox, "fleet 2");
                int seed = ValidateSeed();

                Random random = new Random(seed);

                Fleet fleet1 = CreateFleet(random, fleet1TextBox.Text);

                Fleet fleet2 = CreateFleet(random, fleet2TextBox.Text);

                SpaceBattle     battle   = new SpaceBattle(fleet1, fleet2);
                IBattleObserver observer = new BattleObserverImpl(battleResultTextBox);
                // add observer
                battle.Subscribe(observer);

                battle.CommenceBattle();
                battle.ReportBattleResults();
                // clean up, remove observer
                battle.UnSubscribe(observer);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Program failed with the following error" +
                                Environment.NewLine + ex.Message,
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            Console.WriteLine("TrekBattle by Jeremy Yuille");

            int seed;

            if (!ValidInputArguments(args, out seed))
            {
                return;
            }
            Random      randomGenerator = new Random(seed);
            FleetLoader fleetLoader     = new FleetLoader(args[1], randomGenerator);
            Fleet       firstFleet      = fleetLoader.LoadFleet();
            Fleet       secondFleet     = null;

            if (firstFleet != null)
            {
                fleetLoader = new FleetLoader(args[ThirdArgument], randomGenerator);
                secondFleet = fleetLoader.LoadFleet();
            }
            if (secondFleet == null)
            {
                return;
            }
            SpaceBattle battle = new SpaceBattle(firstFleet, secondFleet);

            battle.CommenceBattle();
            battle.ReportBattleResults();
        }
Esempio n. 3
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            IBattle battle;

            if (IsGroundCombat)
            {
                // TODO - let player pick a planet to fight on, or at least specify population for militia
                var template = Mod.Current.StellarObjectTemplates.OfType <Planet>().Where(p => p.Atmosphere == CurrentEmpire.Empire.PrimaryRace.NativeAtmosphere).PickRandom();
                var planet   = template.Instantiate();
                planet.Name = "Planet";
                var sim       = new SimulatedSpaceObject(planet);
                var simPlanet = (Planet)sim.SpaceObject;
                simPlanet.Colony       = new Colony();
                simPlanet.Colony.Owner = Empires.First().Empire;
                simPlanet.Sector       = new Sector(new StarSystem(0)
                {
                    Name = "Simulation"
                }, new Point());
                foreach (Troop t in Empires.SelectMany(se => se.Troops.Select(ss => ss.Unit)))
                {
                    planet.Cargo.Units.Add(t);
                }
                battle = new GroundBattle(planet);

                // simulate the battle
                battle.Resolve();
            }
            else
            {
                Sector location = new Sector(new StarSystem(0), new Point());
                foreach (ISpaceObject ispobj in (Empires.SelectMany(se => se.SpaceObjects.Select(ss => ss.SpaceObject))))
                {
                    ispobj.Sector = location;
                }
                // create battle with all our combatants
                //var battle = new Battle_Space(Empires.SelectMany(se => se.SpaceObjects.Select(ss => ss.SpaceObject)));
                battle = new SpaceBattle(location);

                // simulate the battle
                battle.Resolve();
            }

            // show the results
            var form = new BattleResultsForm(battle);

            Cursor = Cursors.Default;
            this.ShowChildForm(form);
        }
Esempio n. 4
0
        public void CreateBattle(Player player, SpaceShip enemyShip)
        {
            rwl.AcquireReaderLock(-1);
            try {
                if (player.SpaceShip.SystemId == enemyShip.SystemId)
                {
                    bool        alreadyInBattle = false;
                    SpaceBattle currentBattle   = null;

                    if (!PlayerBattles.ContainsKey(player.SpaceShip))
                    {
                        rwl.UpgradeToWriterLock(-1);
                        PlayerBattles.Add(player.SpaceShip, new List <SpaceBattle>());
                    }
                    else
                    {
                        //check if the battle already exists
                        foreach (var battle in PlayerBattles[player.SpaceShip])
                        {
                            if (battle.SideA == enemyShip || battle.SideB == enemyShip)
                            {
                                alreadyInBattle = true;
                                currentBattle   = battle;
                                break;
                            }
                        }
                    }

                    if (!PlayerBattles.ContainsKey(enemyShip))
                    {
                        if (!rwl.IsWriterLockHeld)
                        {
                            rwl.UpgradeToWriterLock(-1);
                        }
                        PlayerBattles.Add(enemyShip, new List <SpaceBattle>());
                    }

                    if (!alreadyInBattle)
                    {
                        if (!rwl.IsWriterLockHeld)
                        {
                            rwl.UpgradeToWriterLock(-1);
                        }
                        Console.WriteLine("Creating battle");
                        currentBattle = new SpaceBattle(player.SpaceShip, enemyShip);
                        PlayerBattles[player.SpaceShip].Add(currentBattle);
                        PlayerBattles[enemyShip].Add(currentBattle);
                        Battles.Add(currentBattle);
                    }
                }
            } finally {
                if (rwl.IsWriterLockHeld)
                {
                    rwl.ReleaseWriterLock();
                }
                if (rwl.IsReaderLockHeld)
                {
                    rwl.ReleaseReaderLock();
                }
            }
        }