/// <summary>
        /// Makes the decision.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override bool MakeDecision(IBehaviorContext context)
        {
            PlanetBehaviorContext planetContext = context as PlanetBehaviorContext;

            if (planetContext == null)
            {
                return(false);
            }

            List <FactoryItem> possibleUpgrades = planetContext.Planet.Owner.GetPossiblePlanetUpgrades(planetContext.Planet, UpgradeModifier);

            if (possibleUpgrades.Count == 0)
            {
                return(false);
            }

            FactoryItem upgrade = possibleUpgrades.OrderBy(u => u.ProductionCost).First();

            if (upgrade != null)
            {
                context.AddLogMessage($"Upgrade found - {upgrade.Name}");
                planetContext.ItemToBuild = upgrade;
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

            if (playerContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            Player player = playerContext.Player;

            if (player == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            FactoryItem existingFactoryItem = player.AvailFactoryItems.FirstOrDefault(f => f.Id == FactoryItemId);

            if (existingFactoryItem != null)
            {
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Пример #3
0
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            PlanetBehaviorContext planetContext = context as PlanetBehaviorContext;

            if (planetContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            FactoryItem item = null;

            if (!string.IsNullOrEmpty(ItemTypeName))
            {
                item = planetContext.Planet.Owner.AvailFactoryItems.FirstOrDefault(i => i.FactoryTypeName.EndsWith(ItemTypeName));
            }
            else if (planetContext.ItemToBuild != null)
            {
                item = planetContext.ItemToBuild;
            }


            if (item == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            bool canAcquire = item.Acquire(planetContext.Planet.Owner, planetContext.Planet);

            if (!canAcquire)
            {
                planetContext.ItemToBuild = null;
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            FactoryTask task = new BuildFactoryTask(item, planetContext.Planet);

            if (string.IsNullOrEmpty(BehaviorName))
            {
                task.ItemBehaviorName          = planetContext.ItemBehaviorName;
                planetContext.ItemBehaviorName = string.Empty;
            }
            else
            {
                task.ItemBehaviorName = BehaviorName;
            }

            planetContext.Planet.AddFactoryTask(task);

            planetContext.ItemToBuild = null;

            returnCode = BehaviorReturnCode.Success;
            return(returnCode);
        }
Пример #4
0
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            PlanetBehaviorContext planetContext = context as PlanetBehaviorContext;
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

            if (planetContext == null && playerContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            Player owner  = null;
            Planet planet = null;

            if (planetContext != null)
            {
                owner  = planetContext.Planet.Owner;
                planet = planetContext.Planet;
            }
            else if (playerContext != null)
            {
                owner  = playerContext.Player;
                planet = playerContext.BehaviorTarget as Planet;
            }

            if (owner == null || planet == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            List <FactoryItem> possibleUpgrades = owner.GetPossiblePlanetUpgrades(planet, UpgradeModifier);

            if (possibleUpgrades.Count == 0)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            FactoryItem upgrade = possibleUpgrades.OrderBy(u => u.ProductionCost).First();

            if (upgrade != null)
            {
                context.AddLogMessage("Upgrade found - " + upgrade.Name);
                if (planetContext != null)
                {
                    planetContext.ItemToBuild = upgrade;
                }
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Пример #5
0
            public void IncrementFactoryCounter(string factory, int minutes)
            {
                FactoryItem Item = Factories.Where(P => P.Name == factory).Select(P => P).FirstOrDefault();

                if (null == Item)
                {
                    Factories.Add(new FactoryItem {
                        Name = factory, Count = 1, Minutes = minutes
                    });
                }
                else
                {
                    Item.Count++;
                    Item.Minutes += minutes;
                }
            }
Пример #6
0
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            UnitBehaviorContext unitContext = context as UnitBehaviorContext;

            if (unitContext == null || string.IsNullOrEmpty(UnitTypeName))
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            Builder unit = unitContext.Unit as Builder;

            if (unit == null || !unit.CanBuild)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            FactoryItem item = unit.Owner.AvailFactoryItems.FirstOrDefault(i => i.FactoryTypeName.EndsWith(UnitTypeName));

            if (item == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            bool canAcquire = item.Acquire(unit.Owner, unit);

            if (!canAcquire)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            BuilderUnitFactoryTask factoryTask = new BuilderUnitFactoryTask(item, unit);

            unit.ActiveFactoryTask = factoryTask;
            if (!string.IsNullOrEmpty(UnitBehaviorName))
            {
                factoryTask.ItemBehaviorName = UnitBehaviorName;
            }

            BuildTask task = new BuildTask(unit);

            returnCode = BehaviorReturnCode.Success;
            return(returnCode);
        }
Пример #7
0
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;
            PlanetBehaviorContext planetContext = context as PlanetBehaviorContext;
            UnitBehaviorContext   unitContext   = context as UnitBehaviorContext;

            if (playerContext == null && planetContext == null && unitContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            Player      player = null;
            FactoryItem item   = null;

            if (playerContext != null)
            {
                player = playerContext.Player;
                item   = player.AvailFactoryItems.FirstOrDefault(i => i.FactoryTypeName.EndsWith(ItemTypeName));
            }
            else if (planetContext != null)
            {
                player = planetContext.Planet.Owner;
                if (planetContext.ItemToBuild != null)
                {
                    item = planetContext.ItemToBuild;
                }
                else if (!string.IsNullOrEmpty(ItemTypeName))
                {
                    item = player.AvailFactoryItems.FirstOrDefault(i => i.FactoryTypeName.EndsWith(ItemTypeName));
                }
            }
            else if (unitContext != null)
            {
                player = unitContext.Unit.Owner;
                item   = player.AvailFactoryItems.FirstOrDefault(i => i.FactoryTypeName.EndsWith(ItemTypeName));
            }


            if (player == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            if (item == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            float maintenance = item.UnitConfig.MaintenanceCost;

            if (player.EnergyIncome > maintenance && player.Energy > maintenance)
            {
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }