Beispiel #1
0
        private void updateDesigns(StaticsDB statics, StatesDB states, TemporaryDB derivates)
        {
            //Generate upgraded designs
            var upgradesTo = new Dictionary <Design, Design>();
            var newDesigns = new HashSet <Design>();

            foreach (var design in states.Designs.OwnedBy[this.Player])
            {
                var upgrade = this.DesignUpgrade(design, statics, states);
                if (states.Designs.Contains(upgrade))
                {
                    continue;
                }

                if (newDesigns.Contains(upgrade))
                {
                    upgrade = newDesigns.First(x => x == upgrade);
                }
                else
                {
                    this.Analyze(upgrade, statics);
                }

                design.IsObsolete  = true;
                upgradesTo[design] = upgrade;
                newDesigns.Add(upgrade);
            }
            states.Designs.Add(newDesigns);

            //Update refit orders to upgrade obsolete designs
            foreach (var upgrade in upgradesTo)
            {
                var orders = this.Player.Orders.RefitOrders;

                if (!orders.ContainsKey(upgrade.Key))
                {
                    orders[upgrade.Key] = upgrade.Value;
                }
                else if (orders[upgrade.Key] != null && orders[upgrade.Key].IsObsolete)
                {
                    orders[upgrade.Key] = upgradesTo[orders[upgrade.Key]];
                }
            }

            foreach (var site in this.Player.Orders.ConstructionPlans.Keys.ToList())
            {
                var updater = new ShipConstructionUpdater(this.Player.Orders.ConstructionPlans[site].Queue, this.Player.Orders.RefitOrders);
                this.Player.Orders.ConstructionPlans[site].Queue.Clear();
                this.Player.Orders.ConstructionPlans[site].Queue.AddRange(updater.Run());
            }

            //Removing inactive discarded designs
            var shipConstruction = new ShipConstructionCounter();

            shipConstruction.Check(derivates.Stellarises.OwnedBy[this.Player]);

            var activeDesigns = new HashSet <Design>(states.Fleets.
                                                     SelectMany(x => x.Ships).
                                                     Select(x => x.Design).
                                                     Concat(shipConstruction.Designs));
            var discardedDesigns = this.Player.Orders.RefitOrders.
                                   Where(x => x.Value == null && !activeDesigns.Contains(x.Key)).
                                   Select(x => x.Key).ToList();

            foreach (var design in discardedDesigns)
            {
                design.Owner.Orders.RefitOrders.Remove(design);
                states.Designs.Remove(design);
                this.DesignStats.Remove(design);
                this.RefitCosts.Remove(design);
            }
            foreach (var design in this.RefitCosts.Keys)
            {
                foreach (var discarded in discardedDesigns)
                {
                    this.RefitCosts[design].Remove(discarded);
                }
            }
        }
        private void updateDesigns(MainGame game)
        {
            //Generate upgraded designs
            var upgradesTo = new Dictionary <Design, Design>();
            var newDesigns = new HashSet <Design>();

            foreach (var design in game.States.Designs.OwnedBy[this.Player])
            {
                var upgrade = this.DesignUpgrade(design, game.Statics, game.States);
                if (game.States.Designs.Contains(upgrade))
                {
                    continue;
                }

                if (newDesigns.Contains(upgrade))
                {
                    upgrade = newDesigns.First(x => x == upgrade);
                }
                else
                {
                    this.Analyze(upgrade, game.Statics);
                }

                design.IsObsolete  = true;
                upgradesTo[design] = upgrade;
                newDesigns.Add(upgrade);
            }
            game.States.Designs.Add(newDesigns);

            //Update refit orders to upgrade obsolete designs
            var orders = game.Orders[this.Player].RefitOrders;

            foreach (var upgrade in upgradesTo)
            {
                if (!orders.ContainsKey(upgrade.Key))
                {
                    orders[upgrade.Key] = upgrade.Value;
                }
                else if (orders[upgrade.Key].IsObsolete)
                {
                    orders[upgrade.Key] = upgradesTo[orders[upgrade.Key]];
                }
            }

            foreach (var site in game.Orders[this.Player].ConstructionPlans.Keys.ToList())
            {
                var updater = new ShipConstructionUpdater(
                    game.Orders[this.Player].ConstructionPlans[site].Queue,
                    game.Orders[this.Player].RefitOrders
                    );
                game.Orders[this.Player].ConstructionPlans[site].Queue.Clear();
                game.Orders[this.Player].ConstructionPlans[site].Queue.AddRange(updater.Run());
            }

            //Removing inactive discarded designs
            var shipConstruction = new ShipConstructionCounter();

            shipConstruction.Check(game.Derivates.Stellarises.OwnedBy[this.Player]);

            var activeDesigns = new HashSet <Design>(game.States.Fleets.
                                                     SelectMany(x => x.Ships).
                                                     Select(x => x.Design).
                                                     Concat(shipConstruction.Designs));
            var discardedDesigns = game.Orders[this.Player].DiscardOrders.
                                   Where(x => !activeDesigns.Contains(x)).
                                   ToList();

            foreach (var design in discardedDesigns)
            {
                game.Orders[design.Owner].DiscardOrders.Remove(design);
                game.Orders[design.Owner].RefitOrders.Remove(design);
                game.States.Designs.Remove(design);
                this.DesignStats.Remove(design);
                this.RefitCosts.Remove(design);
            }
            foreach (var design in this.RefitCosts.Keys)
            {
                foreach (var discarded in discardedDesigns)
                {
                    this.RefitCosts[design].Remove(discarded);
                }
            }

            //TODO(v0.8) doesn't include obsolete (to be upgraded) designs
            this.ColonizerDesignOptions = this.DesignStats.
                                          Where(x => x.Value.ColonizerPopulation > 0 && !orders.ContainsKey(x.Key)).
                                          Select(x => x.Key).ToList();

            if (!this.ColonizerDesignOptions.Contains(game.Orders[this.Player].ColonizerDesign))
            {
                game.Orders[this.Player].ColonizerDesign = this.ColonizerDesignOptions.First();
            }
        }