Beispiel #1
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;

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

            Player     player           = playerContext.Player;
            float      goalTechnologyId = playerContext.GoalTechnologyId.HasValue ? playerContext.GoalTechnologyId.Value : GoalTechnologyId;
            Technology goalTech         = player.Technologies.FirstOrDefault(t => t.Data.Id == goalTechnologyId);

            if (goalTech == null)
            {
                context.AddLogMessage($"Goal Technology {goalTechnologyId} not found.");
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            if (goalTech.IsAquired)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            if (goalTech.IsAvailable)
            {
                goalTech.ProgressIncrement = player.ResearchProgressPool;
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            int?techId = GetTechnologyIdFromGoal(player, goalTech.Data.Parents);

            if (techId.HasValue)
            {
                Technology techToResearch = player.Technologies.FirstOrDefault(t => t.Data.Id == techId);
                if (techToResearch == null)
                {
                    context.AddLogMessage($"Wrong Technology {techId} found.");
                    returnCode = BehaviorReturnCode.Failure;
                    return(returnCode);
                }

                techToResearch.ProgressIncrement = player.ResearchProgressPool;
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Beispiel #2
0
        /// <summary>
        /// Makes the decision.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override bool MakeDecision(IBehaviorContext context)
        {
            BaseUnit unit = GetUnitFromContext(context);

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

            Behavior behavior = null;

            if (!string.IsNullOrEmpty(BehaviorName))
            {
                behavior = BehaviorsManager.Instance.GetBehavior(BehaviorName);
                if (behavior == null)
                {
                    Debug.Assert(false, "Behavior does not exist");
                    return(false);
                }
            }

            unit.Behavior = behavior;
            context.AddLogMessage($"Unit Decision Behavior - {BehaviorName}");

            return(true);
        }
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override float GetValue(IBehaviorContext context)
        {
            context.AddLogMessage("ExplorationRatioValue");
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;
            PlanetBehaviorContext planetContext = context as PlanetBehaviorContext;

            if (playerContext == null && planetContext == null)
            {
                Debug.Assert(false, "Wrong Behavior Context");
                return(0);
            }

            Player player = null;

            if (playerContext != null)
            {
                player = playerContext.Player;
            }
            else if (planetContext != null)
            {
                player = planetContext.Planet.Owner;
            }

            if (player == null)
            {
                return(0);
            }

            float scoutUnits = player.Units.Count(u => u is Scout);
            float value      = scoutUnits / ((player.GameSession.Galaxy.TotalSystems - player.TotalExploredStarSystems) / Ratio);

            return(value);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            StaticUnit unit = unitContext.Unit as StaticUnit;

            if (unit == null ||
                unit.Environment is Galaxy ||
                unit.Target == null ||
                unit.Environment != unit.Target.Environment ||
                !unit.CanAttack ||
                unit.Owner == unit.Target.Owner)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            context.AddLogMessage("Attacking Target - " + unit.Target.Name);
            AttackTask task = new AttackTask(unit);

            while (!task.IsTaskFinished)
            {
                task.Execute();
            }

            returnCode = BehaviorReturnCode.Success;
            return(returnCode);
        }
Beispiel #5
0
        /// <summary>
        /// Makes the decision.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override bool MakeDecision(IBehaviorContext context)
        {
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

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

            Planet planet = playerContext.BehaviorTarget as Planet;

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

            PlanetBehaviorContext planetContext = planet.BehaviorContext as PlanetBehaviorContext;

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

            planetContext.ItemToBuild = playerContext.Player.AvailFactoryItems.FirstOrDefault(i => i.FactoryTypeName.EndsWith(UnitTypeName));
            if (planetContext.ItemToBuild == null)
            {
                return(false);
            }

            planetContext.ItemBehaviorName = BehaviorName;
            context.AddLogMessage($"Player Unit Decision - {UnitTypeName}");

            return(true);
        }
        /// <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);
        }
Beispiel #7
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override float GetValue(IBehaviorContext context)
        {
            context.AddLogMessage($"PlanetValue - {Property}");
            PlanetBehaviorContext planetContext = context as PlanetBehaviorContext;

            if (planetContext == null)
            {
                Debug.Assert(false, "Wrong Behavior Context");
                return(0);
            }

            if (string.IsNullOrEmpty(Property))
            {
                Debug.Assert(false, "Property null or empty");
                return(0);
            }

            if (info == null || info.Name != Property)
            {
                info = planetType.GetRuntimeProperty(Property);
                if (info == null)
                {
                    Debug.Assert(false, "Wrong Property Name");
                    return(0);
                }
            }

            return(Convert.ToSingle(info.GetValue(planetContext.Planet)));
        }
Beispiel #8
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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            MoveableUnit unit = unitContext.Unit as MoveableUnit;

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

            BaseEnvironment closestEnvi = null;
            int             minDistance = int.MaxValue;

            foreach (var elem in unit.Owner.GameSession.Galaxy.EnvironmentMap.Values)
            {
                BaseEnvironment envi = elem as BaseEnvironment;
                if (envi == null || !(envi is StarSystem))
                {
                    continue;
                }

                if (unit.Owner.ExploredEnvironments.Contains(envi.HexMapKey))
                {
                    continue;
                }

                if (!envi.IsPathAccessible(unit.Owner, envi.HexMapKey))
                {
                    continue;
                }

                int distance = HexMap.Distance(unit, envi);
                if (minDistance > distance)
                {
                    minDistance = distance;
                    closestEnvi = envi;
                }
            }

            unitContext.EnvironmentTarget = closestEnvi;
            if (closestEnvi != null)
            {
                context.AddLogMessage("System found - " + closestEnvi.Name);
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            StaticUnit unit = unitContext.Unit as StaticUnit;

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

            BaseEnvironment envi = unit.Environment;

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

            BaseUnit strongestFriendlyUnit = null;
            float    maxInfluence          = float.MinValue;

            foreach (var elem in envi.UnitsMap.Values)
            {
                BaseUnit target = elem as BaseUnit;
                if (target == null || target.Owner != unitContext.Unit.Owner)
                {
                    continue;
                }

                if (target.Influence > maxInfluence)
                {
                    maxInfluence          = target.Influence;
                    strongestFriendlyUnit = target;
                }
            }

            unit.Target = strongestFriendlyUnit;
            if (strongestFriendlyUnit != null)
            {
                context.AddLogMessage("Friendly found - " + strongestFriendlyUnit.Name);
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Beispiel #10
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);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            BaseEnvironment envi = unitContext.Unit.Environment;

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

            returnCode = BehaviorReturnCode.Failure;
            unitContext.EnvironmentTarget = null;
            foreach (var body in envi.EnvironmentMap.Values)
            {
                Planet planet = body as Planet;
                if (planet == null)
                {
                    continue;
                }

                if (envi.UnitsMap.ContainsKey(planet.HexMapKey))
                {
                    continue;
                }

                if (planet.Owner != unitContext.Unit.Owner)
                {
                    continue;
                }

                if (!planet.CanEnterMoreUnits(unitContext.Unit.Owner))
                {
                    continue;
                }

                unitContext.EnvironmentTarget = planet;
                context.AddLogMessage("Planet found - " + planet.Name);
                returnCode = BehaviorReturnCode.Success;
                break;
            }

            return(returnCode);
        }
Beispiel #12
0
        /// <summary>
        /// Makes the decision.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override bool MakeDecision(IBehaviorContext context)
        {
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

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

            playerContext.GoalTechnologyId = GoalTechnologyId;
            context.AddLogMessage($"Research Decision - {GoalTechnologyId}");

            return(true);
        }
Beispiel #13
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override float GetValue(IBehaviorContext context)
        {
            context.AddLogMessage("PlanetUpgradesValue");
            PlanetBehaviorContext planetContext = context as PlanetBehaviorContext;

            if (planetContext == null)
            {
                Debug.Assert(false, "Wrong Behavior Context");
                return(0);
            }

            float value = planetContext.Planet.Owner.GetPossiblePlanetUpgrades(planetContext.Planet, UpgradeModifier).Count;

            return(value);
        }
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override float GetValue(IBehaviorContext context)
        {
            context.AddLogMessage("ProductionAverageValue");
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

            if (playerContext == null)
            {
                Debug.Assert(false, "Wrong Behavior Context");
                return(0);
            }

            float value = playerContext.Player.TotalProduction / playerContext.Player.TotalPlanets;

            return(value);
        }
Beispiel #15
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;

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

            BaseUnit unit = playerContext.BehaviorTarget as BaseUnit;

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

            Behavior behavior = null;

            if (playerContext.Behavior != null)
            {
                behavior = playerContext.Behavior;
            }
            else
            {
                if (string.IsNullOrEmpty(BehaviorName))
                {
                    returnCode = BehaviorReturnCode.Failure;
                    return(returnCode);
                }

                behavior = BehaviorsManager.Instance.GetBehavior(BehaviorName);
            }

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

            context.AddLogMessage($"Unit {unit.Name} Set Behavior {behavior.Name}");  //string.Format("Unit {0} Set Behavior {1}", unit.Name, behavior.Name));
            unit.Behavior = behavior;
            unit.BehaviorContext.EnvironmentTarget = null;

            returnCode = BehaviorReturnCode.Success;
            return(returnCode);
        }
Beispiel #16
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;

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

            Planet planet = playerContext.BehaviorTarget as Planet;

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

            Behavior behavior = null;

            if (playerContext.Behavior != null)
            {
                behavior = playerContext.Behavior;
            }
            else
            {
                if (string.IsNullOrEmpty(BehaviorName))
                {
                    returnCode = BehaviorReturnCode.Failure;
                    return(returnCode);
                }

                behavior = BehaviorsManager.Instance.GetBehavior(BehaviorName);
            }

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

            context.AddLogMessage("Behavior Name - " + behavior.Name);
            planet.Behavior = behavior;

            returnCode = BehaviorReturnCode.Success;
            return(returnCode);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            EmptySpace space = unitContext.Unit.Environment as EmptySpace;

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

            returnCode = BehaviorReturnCode.Failure;
            unitContext.EnvironmentTarget = null;

            foreach (var body in space.EnvironmentMap.Values)
            {
                Anomaly anomaly = body as Anomaly;
                if (anomaly == null)
                {
                    continue;
                }

                unitContext.EnvironmentTarget = body;
                break;
            }

            if (unitContext.EnvironmentTarget != null)
            {
                context.AddLogMessage("Anomaly found.");
                returnCode = BehaviorReturnCode.Success;
            }

            return(returnCode);
        }
Beispiel #18
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override float GetValue(IBehaviorContext context)
        {
            context.AddLogMessage("ColonizationRatioValue");
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

            if (playerContext == null)
            {
                Debug.Assert(false, "Wrong Behavior Context");
                return(0);
            }

            Player player          = playerContext.Player;
            float  colonyShipCount = player.Units.Count(u => u is ColonyShip);

            foreach (var body in player.StarSystemBodies)
            {
                Planet planet = body as Planet;
                if (planet == null || planet.FactoryQueue == null || planet.FactoryQueue.Count == 0)
                {
                    continue;
                }

                var colonyShip = planet.FactoryQueue.FirstOrDefault(f => f.Item?.UnitConfig != null && f.Item.UnitConfig.Actions.HasFlag(UnitActions.Colonize));
                if (colonyShip != null)
                {
                    colonyShipCount++;
                }
            }

            float planetTypesRatio          = player.ColonizablePlanetTypes.Count / planetTypesCount;
            int   scannedColonizablePlanets = player.ScannedPlanets.Count;

            if (planetTypesRatio != 1)
            {
                scannedColonizablePlanets = player.ScannedPlanets.Count(p => player.CanColonizePlanetType(p.PlanetType));
            }

            float value = (scannedColonizablePlanets - player.TotalPlanets - (colonyShipCount * Ratio)) / (player.GameSession.Galaxy.TotalPlanets * planetTypesRatio);

            return(value);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            BaseEnvironment closestEnvi = null;
            int             minDistance = int.MaxValue;

            foreach (var body in unitContext.Unit.Owner.StarSystemBodies)
            {
                Planet planet = body as Planet;
                if (planet == null)
                {
                    continue;
                }

                int distance = HexMap.Distance(unitContext.Unit, planet.Environment);
                if (minDistance > distance)
                {
                    minDistance = distance;
                    closestEnvi = planet.Environment;
                }
            }

            unitContext.EnvironmentTarget = closestEnvi;
            if (closestEnvi != null)
            {
                context.AddLogMessage("System found - " + closestEnvi.Name);
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            StarSystem system = unitContext.Unit.Environment as StarSystem;

            if (system == null || system.Planets == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            unitContext.EnvironmentTarget = null;
            int minDistance = int.MaxValue;

            foreach (var planet in system.Planets)
            {
                if (unitContext.Unit.Owner.ScannedStarSystemBodies.Contains(planet.GlobalKey))
                {
                    continue;
                }

                if (system.UnitsMap.ContainsKey(planet.HexMapKey))
                {
                    continue;
                }

                if (planet.Owner != null)
                {
                    continue;
                }

                /*
                 * if (!unitContext.Unit.Owner.ColonizablePlanetTypes.Contains(planet.PlanetType))
                 * {
                 *  continue;
                 * }
                 */

                int distance = HexMap.Distance(unitContext.Unit, planet.Environment);
                if (minDistance > distance)
                {
                    minDistance = distance;
                    unitContext.EnvironmentTarget = planet;
                }
            }

            if (unitContext.EnvironmentTarget != null)
            {
                context.AddLogMessage("Planet found - " + ((Planet)unitContext.EnvironmentTarget).Name);
                returnCode = BehaviorReturnCode.Success;
            }

            return(returnCode);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            StaticUnit unit = unitContext.Unit as StaticUnit;

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

            BaseEnvironment envi = unit.Environment;

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

            Player   player            = unitContext.Unit.Owner;
            BaseUnit weakestUnitTarget = null;
            float    minUtility        = float.MaxValue;

            foreach (var elem in envi.UnitsMap.Values)
            {
                BaseUnit target = elem as BaseUnit;
                if (target == null || target.Owner == player || target.Owner == null || target.IsDead)
                {
                    continue;
                }

                MoveableUnit moveableUnit = elem as MoveableUnit;
                if (moveableUnit != null && (moveableUnit.IsInDock || moveableUnit.IsOnOrbit))
                {
                    continue;
                }

                if (!(player.IsHostile(target.Owner) || target.Owner.IsHostile(player)))
                {
                    continue;
                }

                HexElement infElem;
                if (!envi.UnitsInfluenceMap.TryGetValue(target.HexMapKey, out infElem))
                {
                    continue;
                }

                float            utility   = 0;
                InfluenceElement influence = infElem as InfluenceElement;
                if (influence != null)
                {
                    utility = influence.Value;
                }
                else
                {
                    MultiLayerElement layer = infElem as MultiLayerElement;
                    if (layer == null)
                    {
                        continue;
                    }

                    InfluenceElement targetInf = layer.Values.FirstOrDefault(inf => ((InfluenceElement)inf).Owner == target.Owner) as InfluenceElement;
                    if (targetInf == null)
                    {
                        continue;
                    }

                    utility = targetInf.Value;
                }

                utility += HexMap.Distance(unitContext.Unit, target);

                if (player.GameSession.DysonSphereBuilders.Count > 0)
                {
                    // target builder if building dyson sphere
                    Builder builder = target as Builder;
                    if (builder != null && player.GameSession.DysonSphereBuilders.Contains(builder) && builder.IsBuilding)
                    {
                        utility = int.MinValue;
                    }
                }

                if (minUtility > utility)
                {
                    minUtility        = utility;
                    weakestUnitTarget = target;
                }
            }

            unit.Target = weakestUnitTarget;
            if (weakestUnitTarget != null)
            {
                context.AddLogMessage("Target found - " + weakestUnitTarget.Name);
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
        /// <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)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            StaticUnit unit = unitContext.Unit as StaticUnit;

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

            BaseEnvironment envi = unit.Environment;

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

            Player player        = unitContext.Unit.Owner;
            Planet weakestPlanet = null;
            float  minUtility    = float.MaxValue;

            foreach (var elem in unit.Environment.EnvironmentMap.Values)
            {
                Planet target = elem as Planet;
                if (target == null || target.Owner == player || target.Owner == null || !target.CanEnterMoreUnits(player))
                {
                    continue;
                }

                if (!(player.IsHostile(target.Owner) || target.Owner.IsHostile(player)))
                {
                    continue;
                }

                HexElement infElem;
                if (!unit.Environment.EnvironmentInfluenceMap.TryGetValue(target.HexMapKey, out infElem))
                {
                    continue;
                }

                float            utility   = 0;
                InfluenceElement influence = infElem as InfluenceElement;
                if (influence != null)
                {
                    utility = influence.Value;
                }
                else
                {
                    MultiLayerElement layer = infElem as MultiLayerElement;
                    if (layer == null)
                    {
                        continue;
                    }

                    InfluenceElement targetInf = layer.Values.FirstOrDefault(inf => ((InfluenceElement)inf).Owner == target.Owner) as InfluenceElement;
                    if (targetInf == null)
                    {
                        continue;
                    }

                    utility = targetInf.Value;
                }

                utility += HexMap.Distance(unitContext.Unit, target);

                if (minUtility > utility)
                {
                    minUtility    = utility;
                    weakestPlanet = target;
                }
            }

            unitContext.EnvironmentTarget = weakestPlanet;
            if (weakestPlanet != null)
            {
                context.AddLogMessage("Target Planet found - " + weakestPlanet.Name);
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

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