예제 #1
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }

            if (obj is Location)
            {
                if (!GetType().Equals(obj.GetType()))
                {
                    return(false);
                }

                Location other = (Location)obj;

                return(Mathf.RoundToInt(x) == other.Col && Mathf.RoundToInt(y) == other.Row);
            }
            else
            {
                if (!GetType().Equals(obj.GetType()))
                {
                    return(false);
                }

                LocationF other = (LocationF)obj;

                return(x == other.x && y == other.y);
            }
        }
예제 #2
0
        public static float GetDot(MapObject start, MapObject end, MapObject target)
        {
            LocationF startF = start.GetLocation().GetFloatLocation();
            LocationF endF   = end.GetLocation().GetFloatLocation();

            LocationF direction = (endF - startF).Normalized();

            return((target.GetLocation().GetFloatLocation() - startF).Normalized().Dot(direction));
            //return target.GetLocation().Subtract(start.GetLocation()).Normalized().Dot(direction);
        }
예제 #3
0
        /// <summary>
        /// Returns the velocity location (meaning speed+direction) of the given gameObject
        /// </summary>
        /// <param name="gameObject"></param>
        /// <returns></returns>
        public static LocationF GetVelocity(GameObject gameObject)
        {
            LocationF lastLocation = GetLastLocation(gameObject).GetFloatLocation();

            if (lastLocation == null)
            {
                return(new LocationF());
            }
            return(gameObject.GetLocation().GetFloatLocation() - lastLocation);
        }
예제 #4
0
        /// <summary>
        /// Gets the direction this gameObject is heading
        /// </summary>
        /// <param name="gameObject"></param>
        /// <returns></returns>
        public static LocationF GetDirection(GameObject gameObject)
        {
            LocationF velocity = GetVelocity(gameObject);

            if (velocity.Magnitude() == 0)
            {
                return(new LocationF());
            }
            return(velocity.Normalized());
        }
예제 #5
0
        public static LocationF Normalized(this Location a)
        {
            LocationF b         = a.GetFloatLocation();
            float     magnitude = b.Magnitude();

            if (magnitude > 0)
            {
                return(b / magnitude);
            }
            else
            {
                return(new LocationF());
            }
        }
예제 #6
0
        /// <summary>
        /// Is a given object heading approximately towards this target location?
        /// </summary>
        /// <param name="gameObject"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static bool IsHeadingTowards(GameObject gameObject, MapObject target, float certainty)
        {
            LocationF direction = GetDirection(gameObject);

            if (direction == null)
            {
                return(false);
            }
            if (direction.Magnitude() == 0)
            {
                return(false);                            //gameObject is standing still
            }
            float dot = direction.Dot(target.GetLocation().Subtract(gameObject.GetLocation()).Normalized());

            return(dot >= certainty);
        }
예제 #7
0
        /// <summary>
        /// I have tried to do this the smart/elegant/good way using a simple equation, but it doesnt f*****g work.
        /// So here it is, hacked and done really weirdly and maybe even badly but it should work more reliably
        /// Also possibly more intensive too
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="timeToImpact"></param>
        /// <returns></returns>
        private static Location GetStupidInterceptLocation(GameObject source, GameObject target, out int timeToImpact)
        {
            int       sourceVelocity = GetMaximumSpeed(source);
            LocationF targetVelocity = target.GetVelocity();

            //Before we do any calculations, we do some simple, non-intensive checks to make sure we can actually intercept the target before getting the location to intercept

            //We can not move! Interception is impossible
            if (sourceVelocity == 0)
            {
                timeToImpact = -1;
                return(target.GetLocation());
            }

            if (targetVelocity.Magnitude() == 0) //target is not moving, we will never get him
            {
                timeToImpact = source.Distance(target) / sourceVelocity;
                return(target.GetLocation());
            }

            if (!IsHeadingTowards(target, source.GetLocation(), 0f)) //if the target isn't heading towards us
            {
                timeToImpact = source.Distance(target) / sourceVelocity;
                return(target.GetLocation());
            }

            for (int i = 0; i < 4000; i++)
            {
                Location possibleLocation = target.GetLocation().Add(targetVelocity.GetIntLocation().Multiply(i));

                float sourceTimeToLocation = source.Distance(possibleLocation) / sourceVelocity;
                float targetTimeToLocation = target.Distance(possibleLocation) / targetVelocity.Magnitude();

                if (Mathf.Abs(sourceTimeToLocation - targetTimeToLocation) < 1)
                {
                    timeToImpact = Mathf.RoundToInt(sourceTimeToLocation);
                    return(possibleLocation);
                }
            }

            //Failed to find intercept point, just head to target
            timeToImpact = source.Distance(target) / sourceVelocity;
            return(target.GetLocation());
        }
예제 #8
0
        /// <summary>
        /// Runs away from any elfs if it needs to. Returns true if it needs to run away, false otherwise
        /// </summary>
        /// <param name="elf"></param>
        /// <returns> If is running away from elfs true, else false</returns>
        public static bool RunAwayFromElfs(this Elf elf)
        {
            Elf[] elfs = Constants.GameCaching.GetEnemyLivingElves();

            LocationF myNextLocation = elf.GetLocation().GetFloatLocation() + LastPosition.GetVelocity(elf);

            foreach (Elf enemyElf in elfs)
            {
                LocationF enemyNextLocation = enemyElf.GetLocation().GetFloatLocation() + LastPosition.GetVelocity(enemyElf);

                if (enemyElf.InRange(elf, enemyElf.AttackRange * 2) || enemyNextLocation.InRange(elf, enemyElf.AttackRange * 2)) // if the ice troll is going to reach the elf
                {
                    float    angle           = Mathf.GetAngle(elf, enemyElf) + 10;
                    Location runAwayLocation = Mathf.GetNewLocationFromLocation(enemyElf, angle, enemyElf.AttackRange * 2);
                    elf.MoveTo(runAwayLocation);

                    return(true);
                }
            }

            return(false);
        }
예제 #9
0
 public float Dot(LocationF l2)
 {
     return(x * l2.x + y * l2.y);
 }