Example #1
0
        public override BehaviourTreeStatus Tick()
        {
            if (this.bb.CurrentEnemy == null)
            {
                return(BehaviourTreeStatus.Failure);
            }

            List <GravPoint> gravpoints = new List <GravPoint>();

            AdvancedRobot r = this.bb.Robot;

            gravpoints.Add(new GravPoint(this.bb.CurrentEnemy.x, this.bb.CurrentEnemy.y, 1000));

            double changeInEnergy = this.bb.PreviousEnergy - this.bb.CurrentEnemy.E.Energy;

            if (changeInEnergy >= -3 && changeInEnergy <= 3)
            {
                //Detect if enemy energy has changed
                //Now we need to "predict" where the bot will fire its bullets and assign grav points to them
                gravpoints.Add(new GravPoint(r.X + 15, 0, RandomNumber(-1000, 1000)));
                gravpoints.Add(new GravPoint(r.X - 15, 0, RandomNumber(-1000, 1000)));
            }

            double xforce = 0;
            double yforce = 0;
            double force;
            double ang;

            GravPoint p;

            for (int i = 0; i < gravpoints.Count; i++)
            {
                p = (GravPoint)gravpoints[i];
                //Calculate the total force from this point on us
                force = p.power / Math.Pow(GetRange(r.X, r.Y, p.x, p.y), 1.5);
                //Find the bearing from the point to us
                ang =
                    NormaliseBearing(Math.PI / 2 - Math.Atan2(r.Y - p.y, r.X - p.x));
                //Add the components of this force to the total force in their
                //respective directions
                xforce += Math.Sin(ang) * force;
                yforce += Math.Cos(ang) * force;
            }

            /**The next section adds a middle point with a random (positive or negative) strength.
             * The strength changes every 5 turns, and goes between -1000 and 1000.  This gives a better
             * overall movement.**/
            midPointCount++;
            if (midPointCount > 5)
            {
                midPointCount = 0;
                midPointPower = RandomNumber(-1000, 1000);
            }
            p     = new GravPoint(this.bb.Robot.BattleFieldWidth / 2, this.bb.Robot.BattleFieldHeight / 2, midPointPower);
            force = p.power / Math.Pow(GetRange(r.X, r.Y, p.x, p.y), 1.5);
            ang   = NormaliseBearing(Math.PI / 2 - Math.Atan2(r.Y - p.y, r.X - p.x));

            /*
             * double TOP = r.BattleFieldWidth - 18.0;
             * double RIGHT = r.BattleFieldHeight - 18.0;
             * double BOTTOM = 18.0;
             * double LEFT = 18.0;
             *
             * double N = 2.0 * Math.PI;
             * double E = Math.PI / 2.0;
             * double S = Math.PI;
             * double W = 3.0 * Math.PI / 2.0;
             *
             * double s = 8.0;
             * double x = r.X;
             * double y = r.Y;
             * double a = ang; // whatever angle you wish to travel, we can smooth it!
             * bool clockwise = true; // your choice!
             *
             * if (clockwise)
             * {
             *  if (S < a)
             *  { // left wall
             *      if (shouldSmooth(a - S, LEFT - x, s))
             *      {
             *          a = smooth(a - S, LEFT - x, s) + S;
             *      }
             *  }
             *  else if (a < S)
             *  { // right wall
             *      if (shouldSmooth(a, x - RIGHT, s))
             *      {
             *          a = smooth(a, x - RIGHT, s);
             *      }
             *  }
             *  if (W < a || a < E)
             *  { // top wall
             *      if (shouldSmooth(a + E, y - TOP, s))
             *      {
             *          a = smooth(a + E, y - TOP, s) - E;
             *      }
             *  }
             *  else if (E < a && a < W)
             *  { // bottom wall
             *      if (shouldSmooth(a - E, BOTTOM - y, s))
             *      {
             *          a = smooth(a - E, BOTTOM - y, s) + E;
             *      }
             *  }
             * }
             * else
             * {
             *  if (S < a)
             *  { // left wall
             *      if (shouldSmooth(N - a, LEFT - x, s))
             *      {
             *          a = N - smooth(N - a, LEFT - x, s);
             *      }
             *  }
             *  else if (a < S)
             *  { // right wall
             *      if (shouldSmooth(S - a, x - RIGHT, s))
             *      {
             *          a = S - smooth(S - a, x - RIGHT, s);
             *      }
             *  }
             *  if (W < a || a < E)
             *  { // top wall
             *      if (shouldSmooth(E - a, y - TOP, s))
             *      {
             *          a = E - smooth(E - a, y - TOP, s);
             *      }
             *  }
             *  else if (E < a && a < W)
             *  { // bottom wall
             *      if (shouldSmooth(W - a, BOTTOM - y, s))
             *      {
             *          a = W - smooth(W - a, BOTTOM - y, s);
             *      }
             *  }
             * }*/
            xforce += Math.Sin(ang) * force;
            yforce += Math.Cos(ang) * force;

            /**The following four lines add wall avoidance.  They will only
             * affect us if the bot is close to the walls due to the
             * force from the walls decreasing at a power 3.**/
            xforce += 5000 / Math.Pow(GetRange(r.X, r.Y, r.BattleFieldWidth, r.Y), 3);
            xforce -= 5000 / Math.Pow(GetRange(r.X, r.Y, 0, r.Y), 3);
            yforce += 5000 / Math.Pow(GetRange(r.X, r.Y, r.X, r.BattleFieldHeight), 3);
            yforce -= 5000 / Math.Pow(GetRange(r.X, r.Y, r.X, 0), 3);



            //Move in the direction of our resolved force.
            GoTo(this.bb.Robot.X - xforce, this.bb.Robot.Y - yforce);

            //if(Math.Abs(r.X) != Math.Abs(xforce) && Math.Abs(r.Y) != Math.Abs(yforce))
            //{
            //    return BehaviourTreeStatus.Running;
            //}
            //else
            return(BehaviourTreeStatus.Success);
        }
        void antiGravMove()
        {
            double    xforce = 0;
            double    yforce = 0;
            double    force;
            double    ang;
            GravPoint p;
            Enemy     en;

            // cycle through all the enemies.  If they are alive, they are repulsive.  Calculate the force on us
            if (targets.Count > 0)
            {
                foreach (DictionaryEntry pair in targets)
                {
                    en = ((Enemy)(pair.Value));
                    if (en.live)
                    {
                        p     = new GravPoint(en.x, en.y, -1000);
                        force = (p.power / Math.Pow(this.getRange(X, Y, p.x, p.y), 2));
                        // Find the bearing from the point to us
                        ang = this.normaliseBearing(((Math.PI / 2)
                                                     - Math.Atan2((Y - p.y), (X - p.x))));
                        // Add the components of this force to the total force in their respective directions
                        xforce = (xforce
                                  + (Math.Sin(ang) * force));
                        yforce = (yforce
                                  + (Math.Cos(ang) * force));
                    }
                }
            }

            /*
             * while (targets.Count > 0)
             * {
             *  en = ((Enemy)(e.MoveNext()));
             *  if (en.live)
             *  {
             *      p = new GravPoint(en.x, en.y, -1000);
             *      force = (p.power / Math.Pow(this.getRange(X, Y, p.x, p.y), 2));
             *      // Find the bearing from the point to us
             *      ang = this.normaliseBearing(((Math.PI / 2)
             *                      - Math.Atan2((Y - p.y), (X - p.x))));
             *      // Add the components of this force to the total force in their respective directions
             *      xforce = (xforce
             + (Math.Sin(ang) * force));
             +      yforce = (yforce
             + (Math.Cos(ang) * force));
             +  }
             +
             + }*/

            this.midpointcount++;
            if ((this.midpointcount > 5))
            {
                this.midpointcount    = 0;
                this.midpointstrength = ((new Random().Next(0, 100) * 2000)
                                         - 1000);
            }

            p     = new GravPoint((BattleFieldWidth / 2), (BattleFieldHeight / 2), this.midpointstrength);
            force = (p.power / Math.Pow(this.getRange(X, Y, p.x, p.y), 1.5));
            ang   = this.normaliseBearing(((Math.PI / 2)
                                           - Math.Atan2((Y - p.y), (X - p.x))));
            xforce = (xforce
                      + (Math.Sin(ang) * force));
            yforce = (yforce
                      + (Math.Cos(ang) * force));
            xforce = (xforce + (5000 / Math.Pow(this.getRange(X, Y, BattleFieldWidth, Y), 3)));
            xforce = (xforce - (5000 / Math.Pow(this.getRange(X, Y, 0, Y), 3)));
            yforce = (yforce + (5000 / Math.Pow(this.getRange(X, Y, X, BattleFieldHeight), 3)));
            yforce = (yforce - (5000 / Math.Pow(this.getRange(X, Y, X, 0), 3)));
            // Move in the direction of our resolved force.
            this.goTo((X - xforce), (Y - yforce));
        }