Example #1
0
        // private Army consolidatedArmy;

        public Army(Staff owner, Province where, string name) : base(name)
        {
            //if (!this.armies.Contains(this))
            where.AddArmy(this);

            Province = where;
            owner.addArmy(this);
            this.owner = owner;


            World.DayPassed += OnMoveArmy;
            //Province.OwnerChanged += CheckPathOnProvinceOwnerChanged;
            personal = new Dictionary <PopUnit, Corps>();
            foreach (var pop in where.AllPops) //mirrored in Staff. mobilization
            {
                if (pop.Type.canMobilize(owner) && pop.howMuchCanMobilize(owner, null) > 0)
                {
                    //newArmy.add(item.mobilize(this));
                    this.add(Corps.mobilize(owner, pop));
                }
            }

            var unitObject = Unit.Create(this);

            unit = unitObject.GetComponent <Unit>();

            Game.provincesToRedrawArmies.Add(where);
            OnMoveArmy(this, EventArgs.Empty);
            foreach (var enemyArmy in Province.AllStandingArmies().Where(x => x.owner != owner).ToList())
            {
                if (enemyArmy.getSize() > 0)
                {
                    this.attack(enemyArmy).createMessage();
                }
            }
        }
Example #2
0
        public void OnMoveArmy(object sender, EventArgs e)
        {
            if (getSize() > 0)
            {
                if (Path != null)
                {
                    if (Path.nodes.Count > 0)
                    {
                        var oldProvince = Province;
                        Province = Path.nodes[0].Province;
                        Path.nodes.RemoveAt(0);
                        oldProvince.RemoveArmy(this);

                        if (!Province.AllStandingArmies().Contains(this))
                        {
                            Province.AddArmy(this);
                        }

                        if (Game.selectedArmies.Contains(this))
                        {
                            ArmiesSelectionWindow.Get.Refresh();// need that ti check if units Ok to merge
                        }
                        if (Path.nodes.Count == 0)
                        {
                            Path = null;
                            //if (owner == Game.Player && !Game.isPlayerSurrended())
                            //    Message.NewMessage(this.FullName + " arrived!", "Commander, " + this.FullName + " arrived to " + Province + " province", "Fine", false, Province.getPosition());
                        }


                        if (owner != Province.Country) // thats attacking
                        {
                            if (Province.Country.isAI())
                            {
                                if (Province.Country == World.UncolonizedLand)
                                {
                                    Province.Country.mobilize(Province.Yield());
                                }
                                else
                                {
                                    Province.Country.mobilize(Province.Country.AllProvinces);
                                    Province.Country.AllArmies().PerformAction(x => x.SetPathTo(Province.Country.Capital));
                                }
                            }
                            var attackerIsDiplomat = owner as IDiplomat;
                            if (attackerIsDiplomat != null)
                            {
                                attackerIsDiplomat.Diplomacy.OnAttack(Province.Country);
                            }
                        }


                        foreach (var enemyArmy in Province.AllStandingArmies().Where(x => x.owner != owner && (x.owner is Country || owner is Country)).ToList())
                        {
                            if (enemyArmy.getSize() > 0)
                            {
                                this.attack(enemyArmy).createMessage();
                            }
                        }

                        if (getSize() > 0) // todo change to alive check
                        {
                            var isCountryOwner = owner as Country;
                            if (isCountryOwner != null && isCountryOwner != Province.Country)
                            {
                                if (Province.Country == Game.Player && !Game.isPlayerSurrended())
                                {
                                    Message.NewMessage("Province lost!", "Commander, " + isCountryOwner + " took " + Province, "Fine", false, Province.Position);
                                }
                                isCountryOwner.Provinces.TakeProvince(Province, true);
                            }
                        }
                        Game.provincesToRedrawArmies.Add(oldProvince);
                        Game.provincesToRedrawArmies.Add(Province);
                    }
                }
                if (owner.isAI())
                { // auto merge armies for AI
                    var sameCountryArmies = Province.AllStandingArmies().Where(x => x.owner == owner).ToList();
                    while (sameCountryArmies.Count > 1)
                    {
                        sameCountryArmies[0].JoinIn(sameCountryArmies[1]);
                        sameCountryArmies.Remove(sameCountryArmies[1]);
                    }
                }
            }
        }