Example #1
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 || unit.StrikeGroup == null || unit.StrikeGroup.GroupLeader == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            short q = unit.StrikeGroup.GroupLeader.Q;
            short r = unit.StrikeGroup.GroupLeader.R;

            unit.CalculatePath(unit.Environment, q, r);
            if (unit.SelectedPath != null)
            {
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Example #2
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);
            }

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

            MoveableUnit unit = unitContext.Unit as MoveableUnit;

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

            BaseEnvironment envi = unit.Environment;

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

            for (int i = 1; i < envi.MapRadius / RadiusDenominator; i++)
            {
                List <Tuple <short, short> > ring = HexMap.GetRing(unitContext.EnvironmentTarget.Q, unitContext.EnvironmentTarget.R, i);

                foreach (var coord in ring)
                {
                    int key = HexMap.CalculateKey(coord.Item1, coord.Item2);
                    if (envi.EnvironmentMap.ContainsKey(key))
                    {
                        continue;
                    }

                    unit.CalculatePath(envi, coord.Item1, coord.Item2);
                    if (unit.SelectedPath != null)
                    {
                        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);
            }

            MoveableUnit unit = unitContext.Unit as MoveableUnit;

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

            var formationPoint = unit.StrikeGroup.FormationPoints.FirstOrDefault(p => p.UnitGlobalKey == unit.GlobalKey);

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

            short q = (short)(unit.StrikeGroup.GroupLeader.Q + formationPoint.LocalQ);
            short r = (short)(unit.StrikeGroup.GroupLeader.R + formationPoint.LocalR);

            unit.CalculatePath(unit.Environment, q, r);
            if (unit.SelectedPath != null)
            {
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Example #4
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);
            }

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

            Vector targetCoords = new Vector();

            targetCoords.X = unit.Target.Q;
            targetCoords.Y = unit.Target.R;
            Vector unitCoords = new Vector();

            unitCoords.X = unit.Q;
            unitCoords.Y = unit.R;

            Vector distance  = targetCoords - unitCoords;
            Vector direction = Vector.Normalize(distance);

            direction.X = (float)Math.Round(direction.X, 0);
            direction.Y = (float)Math.Round(direction.Y, 0);

            float      fleeDistance    = unit.SensorsEnergy;
            StaticUnit targetWithRange = unit.Target as StaticUnit;

            if (targetWithRange != null)
            {
                // stay out of target sensor range
                fleeDistance = targetWithRange.SensorsEnergy;
            }

            PointF fleeCoords = new PointF();

            unit.SelectedPath = null;
            int directionIndex = 0;

            while (unit.SelectedPath == null && directionIndex < HexMap.Neighbors.Count)
            {
                fleeCoords.X = unitCoords.X - (direction.X * fleeDistance);
                fleeCoords.Y = unitCoords.Y - (direction.Y * fleeDistance);
                unit.CalculatePath(unit.Environment, fleeCoords);
                direction.X = HexMap.Neighbors[directionIndex].x;
                direction.Y = HexMap.Neighbors[directionIndex].y;
                directionIndex++;
            }

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

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Example #5
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);
            }

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

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

            /*
             * Vector targetCoords = new Vector();
             *
             * Vector unitCoords = new Vector();
             * unitCoords.X = unit.Q;
             * unitCoords.Y = unit.R;
             *
             * Vector direction = Vector.Normalize(targetCoords - unitCoords);
             * direction.X = (float)Math.Round(direction.X, 0);
             * direction.Y = (float)Math.Round(direction.Y, 0);
             *
             * if (PathTargetPosition)
             * {
             *  direction = Vector.Zero;
             * }
             *
             * unit.SelectedPath = null;
             * int range = (int) unit.SensorsEnergy;
             * while (unit.SelectedPath == null && range > 0)
             * {
             *  targetCoords -= (direction * range);
             *  unit.CalculatePath(unit.Environment, (short) targetCoords.X, (short) targetCoords.Y);
             *  range--;
             * }
             */

            short      nq           = unit.Target.Q;
            short      nr           = unit.Target.R;
            HexElement existingUnit = null;
            bool       foundHex     = false;
            int        searchRadius = (int)unit.SensorsEnergy;
            short      centerQ      = nq;
            short      centerR      = nr;

            while (!foundHex && searchRadius > 0)
            {
                var range = HexMap.GetRing(centerQ, centerR, searchRadius);
                foreach (var coord in range)
                {
                    nq = coord.Item1;
                    nr = coord.Item2;
                    if (unit.Environment.MapRadius > HexMap.Distance(0, nq, 0, nr) &&
                        !unit.Environment.UnitsMap.TryGetValue(HexMap.CalculateKey(nq, nr), out existingUnit))
                    {
                        foundHex = true;
                        break;
                    }
                }

                searchRadius--;
            }

            if (foundHex)
            {
                unit.CalculatePath(unit.Environment, nq, nr);
            }

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

            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);
            }

            MoveableUnit unit = unitContext.Unit as MoveableUnit;

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

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

            short q = unitContext.EnvironmentTarget.Q;
            short r = unitContext.EnvironmentTarget.R;

            if (UsePlanetNextPosition && unitContext.EnvironmentTarget is Planet)
            {
                q = ((Planet)unitContext.EnvironmentTarget).NextTurnQ;
                r = ((Planet)unitContext.EnvironmentTarget).NextTurnR;
            }

            Star star = unitContext.EnvironmentTarget as Star;

            if (star != null)
            {
                int radius = unit.Owner.GameSession.EnvironmentConfig.SpectralTypeConfig.SpectralTypesMinMaxValues[(int)star.SpectralType].HexRadius + 1;
                var range  = HexMap.GetRing(0, 0, radius);
                if (range != null)
                {
                    int minDistance = int.MaxValue;
                    foreach (var coord in range)
                    {
                        int distance = HexMap.Distance(unit.Q, coord.Item1, unit.R, coord.Item2);
                        if (distance > minDistance)
                        {
                            continue;
                        }

                        minDistance = distance;
                        q           = coord.Item1;
                        r           = coord.Item2;
                    }
                }
            }

            unit.CalculatePath(unit.Environment, q, r);
            if (unit.SelectedPath != null)
            {
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

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