/// <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);
        }
        /// <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);
        }
Beispiel #3
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);
            }

            BaseEnvironment envi = unitContext.Unit.Environment;

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

            unitContext.EnvironmentTarget = null;
            float    maxInfluence = float.MinValue;
            Asteroid bestAsteroid = null;

            foreach (var body in envi.EnvironmentMap.Values)
            {
                Asteroid asteroid = body as Asteroid;
                if (asteroid == null)
                {
                    continue;
                }

                if (EnableUnscannedCheck && unitContext.Unit.Owner.ScannedStarSystemBodies.Contains(asteroid.GlobalKey))
                {
                    continue;
                }

                if (EnableIsExtractedCheck && asteroid.IsExtracted)
                {
                    continue;
                }

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

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

                    asteroidInfluence = layer.Values.Sum(i => ((InfluenceElement)i).Value);
                }

                if (asteroidInfluence > maxInfluence)
                {
                    maxInfluence = asteroidInfluence;
                    bestAsteroid = asteroid;
                }
            }

            unitContext.EnvironmentTarget = bestAsteroid;
            if (bestAsteroid != null)
            {
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

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