示例#1
0
        void VelocityToActionExact(ref EnvShooter.Action action, Vector2 velocity)
        {
            action.right = velocity.X > 0;
            action.left  = velocity.X < 0;
            action.up    = velocity.Y < 0;
            action.down  = velocity.Y > 0;

            SetVelocityAccordingToWalls(ref action);
        }
示例#2
0
 void VelocityToAction(ref EnvShooter.Action action, Vector2 velocity)
 {
     action.right = action.left = action.up = action.down = false;
     if (Math.Abs(velocity.X) > RADIUS)
     {
         action.right = velocity.X > 0;
         action.left  = velocity.X < 0;
     }
     if (Math.Abs(velocity.Y) > RADIUS)
     {
         action.up   = velocity.Y < 0;
         action.down = velocity.Y > 0;
     }
 }
示例#3
0
        void SetVelocityAccordingToWalls(ref EnvShooter.Action action)
        {
            if (action.right && Pos.X > env.Width - 1f - RADIUS - SPEED)
            {
                action.right = false;
            }
            if (action.left && Pos.X < 1f + RADIUS + SPEED)
            {
                action.left = false;
            }

            if (action.down && Pos.Y > env.Height - 1f - RADIUS - SPEED)
            {
                action.down = false;
            }
            if (action.up && Pos.Y < 1f + RADIUS + SPEED)
            {
                action.up = false;
            }
        }
示例#4
0
        protected override EnvShooter.Action GetAction()
        {
            //Loop

            //env.Bullets  - bullet array
            //this. eigene variablen (this ist nicht benötigt)

            lastAction = new EnvShooter.Action()
            {
                right  = false,
                up     = false,
                left   = false,
                down   = false,
                charge = false,
                aim    = MathHelper.Pi
            };



            return(lastAction);
        }
示例#5
0
        protected override EnvShooter.Action GetAction()
        {
            Vector2 move_target = new Vector2(this.Pos.X, 20);
            Vector2 aim_target  = new Vector2(0, 0);


            if (env.Bullets.Length == 0)
            {
                move_target.Y = this.Pos.Y;
            }


            EnvShooter.Bot[] bots = this.enemies.ToList().FindAll(x => x.Alive).ToArray();
            aim_target = bots[rand.Next(bots.Length)].Pos;

            bool do_charge = false;//this.Id == 0;


            Bullet[] collectible_bullets = this.env.Bullets.ToList().FindAll(x => x.Collectible).ToArray();
            Bullet[] active_bullets      = this.env.Bullets.ToList().FindAll(x => !x.Collectible).ToArray();
            Vector2[,] bullet_poses = BulletSim3.Get_Bullet_Poses(active_bullets, this.map, frames_till_hit_max);

            /*  MOVEMENT START  */
            MovementDecision md = new MovementDecision(bullet_poses);

            // Add the most desired position first and least desired one last

            // 1. Get out of danger Zone
            if (InMortalDanger(this.Pos, enemies[0].Pos))
            {
                Vector2 rel_vec = -(enemies[0].Pos - this.Pos);
                if (!IsPosInWall(this.Pos + rel_vec))
                {
                    md.desired_poses.Add(TargetToNextFramePos(this.Pos + rel_vec));
                }
            }

            // 2. Get to collectable
            List <Tuple <float, Vector2> > collectible_dists = collectible_bullets.Select(x => new Tuple <float, Vector2>(Vector2.Distance(x.Pos, this.Pos), x.Pos)).ToList();

            collectible_dists.OrderBy(x => x.Item1);
            for (int i = 0; i < collectible_dists.Count; i++)
            {
                if (!InMortalDanger(collectible_dists[i].Item2, enemies[0].Pos))
                {
                    md.desired_poses.Add(TargetToNextFramePos(collectible_dists[i].Item2));
                }
            }


            //if (!InMortalDanger(new Vector2(9, 9), enemies[0].Pos) &&
            //    !InNoChargeSafeZone(new Vector2(9, 9), enemies[0].Pos))
            //    md.desired_poses.Add(TargetToNextFramePos(new Vector2(9, 9)));

            // 3. Get to edge of no charge safe zone
            if (InSafety(this.Pos, enemies[0].Pos))
            {
                Vector2 rel_vec = (enemies[0].Pos - this.Pos);

                if (!IsPosInWall(this.Pos + rel_vec))
                {
                    md.desired_poses.Add(TargetToNextFramePos(this.Pos + rel_vec));
                }
            }
            else if (InNoChargeSafeZone(this.Pos, enemies[0].Pos))
            {
                Vector2 rel_vec = -(enemies[0].Pos - this.Pos);

                if (!IsPosInWall(this.Pos + rel_vec))
                {
                    md.desired_poses.Add(TargetToNextFramePos(this.Pos + rel_vec));
                }
            }



            // Add backup targets
            Vector2[] move_vecs = new Vector2[]
            {
                new Vector2(1, -1), new Vector2(1, 0), new Vector2(1, 1),
                new Vector2(0, 1), new Vector2(-1, 1),
                new Vector2(-1, 0), new Vector2(-1, -1), new Vector2(0, -1)
            };
            for (int i = 0; i < move_vecs.Length; i++)
            {
                md.desired_poses.Add(TargetToNextFramePos(this.Pos + move_vecs[i]));
            }
            md.desired_poses.Add(Vector2.Zero);

            move_target = md.GetOptimalPos(this.Pos);

            /*  MOVEMENT END  */

            color = md.IsMovementTargetSafe == true ? Color.DeepPink : Color.Purple;

            /*  CHARGE START  */
            if (InMortalDanger(move_target, enemies[0].Pos) || md.IsMovementTargetSafe == false)
            {
                do_charge = false;
            }
            else
            {
                // Fully charged
                if (this.Charge >= 0.95)
                {
                    // Only shoot in no charge safety zone
                    if (InNoChargeSafeZone(this.Pos, enemies[0].Pos))
                    {
                        do_charge = false;
                    }
                    else
                    {
                        do_charge = true;
                    }
                }
                else
                {
                    do_charge = true;
                }
            }


            /*  CHARGE END  */

            //if (this.Id == 0)
            //    do_charge = false;

            EnvShooter.Action action = GoToXY(move_target);
            action.aim    = AimToPos(aim_target);
            action.charge = do_charge;
            return(action);
        }
示例#6
0
        protected override EnvShooter.Action GetAction()
        {
            //Loop
            EnvShooter.Action test = new EnvShooter.Action()
            {
                right  = false,
                up     = false,
                left   = false,
                down   = false,
                charge = false,
                aim    = MathHelper.Pi
            };

            Vector2 movementDirection = new Vector2(0, 0);
            Vector2 temp = new Vector2(0, 0);
            Vector2 closestCollectiblePosition = this.Pos;
            bool    isSet              = false;
            bool    isDodging          = false;
            int     closestEnemeyindex = 0;
            int     maxAmmo            = 0;

            if (this.env.Bullets.Length > 0)
            {
                foreach (Bullet bullet in this.env.Bullets)
                {
                    if (!bullet.Collectible)
                    {
                        if (TestForCollision(bullet.Pos, bulletCollsionRadius))
                        {
                            isDodging          = true;
                            movementDirection += RotateVector(bullet.Velocity, 90);
                        }
                        else if (TestForCollision(bullet.Pos, bulletCollsionRadius + 1) && bullet.Velocity.Length() > 0.8f)
                        {
                            isDodging          = true;
                            movementDirection += RotateVector(bullet.Velocity, 90);
                        }
                    }
                    else
                    {
                        if (isSet == false)
                        {
                            isSet = true;
                            closestCollectiblePosition = bullet.Pos;
                        }
                        else
                        {
                            if (getAbsoluteDistance(closestCollectiblePosition) > getAbsoluteDistance(bullet.Pos))
                            {
                                closestCollectiblePosition = bullet.Pos;
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < this.enemies.Length; i++)
            {
                if (this.enemies[i].Alive)
                {
                    maxAmmo += this.enemies[i].Ammo;
                }
            }
            maxAmmo += this.Ammo;
            maxAmmo += this.env.Bullets.Length;

            if (this.Ammo == maxAmmo)
            {
                test.charge = true;
            }


            if (this.Ammo == maxAmmo && this.Charge > 0.99)
            {
                for (int i = 0; i < this.enemies.Length; i++)
                {
                    if (!enemies[i].Alive)
                    {
                        continue;
                    }

                    if (getDistance(this.enemies[closestEnemeyindex].Pos) < getDistance(this.enemies[i].Pos))
                    {
                        closestEnemeyindex = i;
                    }
                }

                test.aim    = (float)Math.Atan2(this.enemies[closestEnemeyindex].Pos.Y - this.Pos.Y, this.enemies[closestEnemeyindex].Pos.X - this.Pos.X);
                test.charge = false;
            }

            if (isDodging)
            {
                movementDirection = RotateVector(movementDirection, 90);
            }
            else if (isSet)
            {
                movementDirection.X += closestCollectiblePosition.X - Pos.X;
                movementDirection.Y += closestCollectiblePosition.Y - Pos.Y;
            }

            if (!(movementDirection.X == 0) && !(movementDirection.Y == 0))
            {
                movementDirection.Normalize();
            }
            else
            {
                movementDirection.X = rand.Next() % 2 - 0.5f;
                movementDirection.Y = rand.Next() % 2 - 0.5f;
            }

            if (movementDirection.Y > 0.1)
            {
                test.down = true;
                test.up   = false;
            }
            else if (movementDirection.Y < -0.1)
            {
                test.down = false;
                test.up   = true;
            }
            else
            {
            }
            if (movementDirection.X > 0.1)
            {
                test.right = true;
                test.left  = false;
            }
            else if (movementDirection.X < -0.1)
            {
                test.right = false;
                test.left  = true;
            }
            else
            {
            }

            return(test);
        }
示例#7
0
        protected override EnvShooter.Action GetAction()
        {
            //Loop

            EnvShooter.Action act = new EnvShooter.Action()
            {
                right  = false,
                left   = false,
                up     = false,
                down   = false,
                charge = false,
                aim    = -MathHelper.Pi / 2
            };

            int dec = Env.constRand.Next(4);

            act.right = (dec == 0);
            act.left  = (dec == 1);
            act.up    = (dec == 2);
            act.down  = (dec == 3);

            Vector2 vec = enemies[0].Pos - this.Pos;

            if (this.Ammo > 0)
            {
                act.charge = true;
            }
            if (this.Charge == 1)
            {
                act.charge = false;
                act.aim    = (float)Math.Atan2(vec.Y, vec.X);
            }

            float wurzel = (float)(1 / Math.Sqrt(2));

            float[] test = new float[8] {
                vec.X, wurzel *(vec.X) + wurzel * (vec.Y), vec.Y, -wurzel * (vec.X) + wurzel * (vec.Y), -vec.X, -wurzel * (vec.X) - wurzel * (vec.Y), -vec.Y, wurzel *(vec.X) - wurzel * (vec.Y)
            };;


            /*
             * if (Math.Abs(vec.X) > Math.Abs(vec.Y))
             * {
             *  act.right = false;
             *  act.left = false;
             *  if (vec.Y > 0) { act.up = true; act.down = false; }
             *  else { act.up = false; act.down = true; }
             * }
             * else
             * {
             *  act.up = false;
             *  act.down = false;
             *  if (vec.X > 0) { act.left = true; act.right = false; }
             *  else { act.left = false; act.right = true; }
             * }
             */



            return(act);
        }
示例#8
0
        protected override EnvShooter.Action GetAction()
        {
            Vector2 move_target = new Vector2(rand.Next(19), rand.Next(19));

            Vector2 aim_target = new Vector2(0, 0);


            EnvShooter.Bot[] bots = this.enemies.ToList().FindAll(x => x.Alive).ToArray();
            aim_target = bots[rand.Next(bots.Length)].Pos;

            bool do_charge = true;

            if (Charge >= 0.99)
            {
                do_charge = false;
            }



            int min_col = int.MaxValue;
            int col_idx = -1;

            for (int i = 0; i < this.env.Bullets.Length; i++)
            {
                if (this.env.Bullets[i].Collectible)
                {
                    continue;
                }
                int player_collision = BulletSim.CollidesWith(this.Pos, 0.4f, this.env.Bullets[i], this.map, 120);
                if (player_collision > -1)
                {
                    if (player_collision < min_col)
                    {
                        min_col = player_collision;
                        col_idx = i;
                    }
                }
            }

            if (min_col != int.MaxValue)
            {
                Vector2 bullet_vec = this.env.Bullets[col_idx].Velocity;
                Vector2 orth0      = new Vector2(bullet_vec.Y, -bullet_vec.X);
                Vector2 orth1      = new Vector2(-bullet_vec.Y, bullet_vec.X);
                orth0.Normalize();
                orth1.Normalize();

                Vector2 point_out = Vector2.Zero;

                for (int i = 0; i < 10; i++)
                {
                    Vector2 pos0 = this.Pos + (orth0 * (float)i * 0.4f);
                    Vector2 pos1 = this.Pos + (orth1 * (float)i * 0.4f);

                    int steps0 = BulletSim.CollidesWith(pos0, 0.4f, this.env.Bullets[col_idx], this.map, 120);
                    int steps1 = BulletSim.CollidesWith(pos1, 0.4f, this.env.Bullets[col_idx], this.map, 120);


                    if (steps0 < steps1)
                    {
                        point_out = pos0;
                    }
                    else
                    {
                        point_out = pos1;
                    }

                    if (steps0 < 0 || steps1 < 0)
                    {
                        break;
                    }
                }

                move_target = point_out;

                color = Color.Purple;
            }
            else
            {
                color       = Color.HotPink;
                move_target = Pos;


                Bullet[] bullets     = this.env.Bullets.ToList().FindAll(x => x.Collectible).ToArray();
                float    min_len     = int.MaxValue;
                int      min_len_idx = -1;
                for (int i = 0; i < bullets.Length; i++)
                {
                    if ((bullets[i].Pos - this.Pos).Length() < min_len)
                    {
                        min_len     = (bullets[i].Pos - this.Pos).Length();
                        min_len_idx = i;
                    }

                    if (min_len_idx > -1)
                    {
                        move_target = bullets[min_len_idx].Pos;
                    }
                    else
                    {
                        move_target = this.Pos;
                    }
                }
            }

            EnvShooter.Action action = GoToXY(move_target);
            action.aim    = AimToPos(aim_target);
            action.charge = do_charge;
            return(action);
        }
示例#9
0
        protected override EnvShooter.Action GetAction()
        {
            Vector2 move_target = new Vector2(rand.Next(19), rand.Next(19));

            Vector2 aim_target = new Vector2(0, 0);


            EnvShooter.Bot[] bots = this.enemies.ToList().FindAll(x => x.Alive).ToArray();
            aim_target = bots[rand.Next(bots.Length)].Pos;

            bool do_charge = true;

            if (Charge >= 0.99)
            {
                do_charge = false;
            }



            int min_col = int.MaxValue;
            int col_idx = -1;

            for (int i = 0; i < this.env.Bullets.Length; i++)
            {
                if (this.env.Bullets[i].Collectible)
                {
                    continue;
                }
                int player_collision = BulletSim2.CollidesWith(this.Pos, 0.4f, this.env.Bullets[i], this.map, 120);
                if (player_collision > -1)
                {
                    if (player_collision < min_col)
                    {
                        min_col = player_collision;
                        col_idx = i;
                    }
                }
            }

            if (min_col != int.MaxValue)
            {
                Vector2[] move_vecs = new Vector2[]
                {
                    new Vector2(1, -1), new Vector2(1, 0), new Vector2(1, 1),
                    new Vector2(0, 1), new Vector2(-1, 1),
                    new Vector2(-1, 0), new Vector2(-1, -1), new Vector2(0, -1)
                };
                //for (int i = 0; i < move_vecs.Length; i++)
                //    move_vecs[i].Normalize();



                Vector2 point_out = Vector2.Zero;

                for (int i = 1; i < 2; i++)
                {
                    Vector2[] poses = new Vector2[move_vecs.Length];
                    for (int j = 0; j < move_vecs.Length; j++)
                    {
                        poses[j] = this.Pos + (move_vecs[j] * (float)i * 0.4f);
                    }

                    int[] col_steps = new int[poses.Length];
                    for (int j = 0; j < col_steps.Length; j++)
                    {
                        if (poses[j].X - 0.4f < 1 || poses[j].X + 0.4f > 19 || poses[j].Y - 0.4f < 1 || poses[j].Y + 0.4f > 19)
                        {
                            col_steps[j] = 0;
                            continue;
                        }

                        int min_bullet     = int.MaxValue;
                        int min_bullet_idx = -1;
                        for (int y = 0; y < this.env.Bullets.Length; y++)
                        {
                            int c_steps = BulletSim2.CollidesWith(poses[j], 0.4f, this.env.Bullets[y], this.map, 120);
                            // closest bullet death found
                            if (c_steps > -1 && c_steps < min_bullet)
                            {
                                min_bullet     = c_steps;
                                min_bullet_idx = y;
                            }
                        }

                        // No collisions
                        if (min_bullet == int.MaxValue)
                        {
                            col_steps[j] = -1;
                            break;
                        }
                        else
                        {
                            col_steps[j] = min_bullet;
                        }
                    }

                    int  max_dist        = -1;
                    int  max_dist_idx    = -1;
                    bool minus_one_found = false;
                    for (int j = 0; j < col_steps.Length; j++)
                    {
                        if (col_steps[j] == -1)
                        {
                            point_out       = poses[j];
                            minus_one_found = true;
                            break;
                        }
                        else if (col_steps[i] > max_dist)
                        {
                            max_dist     = col_steps[j];
                            max_dist_idx = j;
                        }
                    }

                    if (minus_one_found == false)
                    {
                        point_out = poses[max_dist_idx];
                    }
                    else
                    {
                        break;
                    }
                }

                move_target = point_out;

                color = Color.Purple;
            }
            else
            {
                color       = Color.HotPink;
                move_target = Pos;


                Bullet[] bullets     = this.env.Bullets.ToList().FindAll(x => x.Collectible).ToArray();
                float    min_len     = int.MaxValue;
                int      min_len_idx = -1;
                for (int i = 0; i < bullets.Length; i++)
                {
                    if ((bullets[i].Pos - this.Pos).Length() < min_len)
                    {
                        min_len     = (bullets[i].Pos - this.Pos).Length();
                        min_len_idx = i;
                    }

                    if (min_len_idx > -1)
                    {
                        move_target = bullets[min_len_idx].Pos;
                    }
                    else
                    {
                        move_target = this.Pos;
                    }
                }
            }

            EnvShooter.Action action = GoToXY(move_target);
            action.aim    = AimToPos(aim_target);
            action.charge = do_charge;
            return(action);
        }
示例#10
0
        protected override EnvShooter.Action GetAction()
        {
            //Loop

            EnvShooter.Action act = new EnvShooter.Action()
            {
                right  = false,
                left   = false,
                up     = false,
                down   = false,
                charge = false,
                aim    = 0
            };

            int ind_enemy = 0;

            for (int i = 0; i < this.enemies.Length; i++)
            {
                if (this.enemies[i].Alive == true)
                {
                    ind_enemy = i; break;
                }
            }
            Vector2 vec = enemies[ind_enemy].Pos - this.Pos;


            // schießen, falls Munition vorhanden und Gegner nicht schießt
            if (this.Ammo > 0 && this.enemies[ind_enemy].Charge == 0.0f)
            {
                act.charge = true;
            }

            if (this.Charge == 1)
            {
                act.charge = false;
                act.aim    = (float)Math.Atan2(vec.Y, vec.X);
            }



            // Berechne nächste Bullet
            bool bln_foundDanger   = false;
            int  ind_nearestBullet = 0;

            if (env.Bullets.Length > 1)
            {
                for (int i = 1; i < env.Bullets.Length; i++)
                {
                    if (bln_foundDanger)
                    {
                        if ((Vector2.Dot(env.Bullets[i].Velocity, env.Bullets[i].Pos - this.Pos) < 0) &&
                            (env.Bullets[i].Velocity.Length() > 0.1f) &&
                            (Vector2.Distance(env.Bullets[i].Pos, this.Pos) < Vector2.Distance(env.Bullets[ind_nearestBullet].Pos, this.Pos)))
                        {
                            ind_nearestBullet = i;
                        }
                    }
                    else
                    {
                        if ((Vector2.Dot(env.Bullets[i].Velocity, env.Bullets[i].Pos - this.Pos) < 0) &&
                            (env.Bullets[i].Velocity.Length() > 0.05f))
                        {
                            ind_nearestBullet = i;
                            bln_foundDanger   = true;
                        }
                    }
                }
            }

            // wird eine Kugel gefährlich?
            if (bln_foundDanger && (env.Bullets.Length > 0) && (Vector2.Distance(env.Bullets[ind_nearestBullet].Pos, this.Pos) < 5))
            {
                bool[] dir = getDirection(env.Bullets[ind_nearestBullet].Velocity);
                act.right = dir[0]; act.down = dir[1]; act.left = dir[2]; act.up = dir[3];
            }
            // schießt ein Gegner gerade?
            else if (enemies[ind_enemy].Charge > 0.0f)
            {
                act.charge = false;

                float   wurzel = (float)(1f / Math.Sqrt(2));
                float[] test   = new float[8] {
                    Math.Abs(vec.X), Math.Abs(wurzel * (vec.X) + wurzel * (vec.Y)), Math.Abs(vec.Y), Math.Abs(-wurzel * (vec.X) + wurzel * (vec.Y)),
                    Math.Abs(-vec.X), Math.Abs(-wurzel * (vec.X) - wurzel * (vec.Y)), Math.Abs(-vec.Y), Math.Abs(wurzel * (vec.X) - wurzel * (vec.Y))
                };

                int ind = 0;
                for (int i = 1; i < 8; i++)
                {
                    if (test[i] < test[ind])
                    {
                        ind = i; break;
                    }
                }

                int fix = Env.constRand.Next(2);
                switch (ind)
                {
                case 0:
                    if (fix == 0)
                    {
                        act.right = true;
                    }
                    else
                    {
                        act.left = true;
                    }
                    break;

                case 1:
                    if (fix == 0)
                    {
                        act.right = true; act.down = true;
                    }
                    else
                    {
                        act.left = true; act.up = true;
                    }
                    break;

                case 2:
                    if (fix == 0)
                    {
                        act.down = true;
                    }
                    else
                    {
                        act.up = true;
                    }
                    break;

                case 3:
                    if (fix == 0)
                    {
                        act.left = true; act.down = true;
                    }
                    else
                    {
                        act.right = true; act.up = true;
                    }
                    break;

                case 4:
                    if (fix == 0)
                    {
                        act.left = true;
                    }
                    else
                    {
                        act.right = true;
                    }
                    break;

                case 5:
                    if (fix == 0)
                    {
                        act.left = true; act.up = true;
                    }
                    else
                    {
                        act.right = true; act.down = true;
                    }
                    break;

                case 6:
                    if (fix == 0)
                    {
                        act.up = true;
                    }
                    else
                    {
                        act.down = true;
                    }
                    break;

                case 7:
                    if (fix == 0)
                    {
                        act.right = true; act.up = true;
                    }
                    else
                    {
                        act.left = true; act.down = true;
                    }
                    break;

                default:
                    break;
                }
            }
            else
            {
                // collect bullets
                int ind = 0;
                if (env.Bullets.Length > 0)
                {
                    // search nearest bullet
                    for (int i = 0; i < env.Bullets.Length; i++)
                    {
                        if (env.Bullets[i].Collectible == true)
                        {
                            if (Vector2.Distance(this.Pos, env.Bullets[i].Pos) < Vector2.Distance(this.Pos, env.Bullets[ind].Pos))
                            {
                                ind = i;
                            }
                        }
                    }

                    // in which direction go first
                    if (env.Bullets[ind].Collectible == true)
                    {
                        Vector2 dist = env.Bullets[ind].Pos - this.Pos;
                        if (Math.Abs(dist.X) > Math.Abs(dist.Y))
                        {
                            if (dist.X > 0)
                            {
                                act.right = true;
                            }
                            else
                            {
                                act.left = true;
                            }
                        }
                        else
                        {
                            if (dist.Y > 0)
                            {
                                act.down = true;
                            }
                            else
                            {
                                act.up = true;
                            }
                        }
                    }
                    else
                    {
                        // Random
                        int dec = Env.constRand.Next(4);
                        act.right = (dec == 0);
                        act.left  = (dec == 1);
                        act.up    = (dec == 2);
                        act.down  = (dec == 3);
                    }
                }
                else
                {
                    // Random
                    int dec = Env.constRand.Next(4);
                    act.right = (dec == 0);
                    act.left  = (dec == 1);
                    act.up    = (dec == 2);
                    act.down  = (dec == 3);
                }
            }



            return(act);
        }
示例#11
0
        protected override EnvShooter.Action GetAction()
        {
            //Loop

            EnvShooter.Action act = new EnvShooter.Action()
            {
                right  = false,
                left   = false,
                up     = false,
                down   = false,
                charge = false,
                aim    = 0
            };

            int ind_enemy = 0;

            for (int i = 0; i < this.enemies.Length; i++)
            {
                if (this.enemies[i].Alive == true)
                {
                    ind_enemy = i; break;
                }
            }
            Vector2 vec = enemies[ind_enemy].Pos - this.Pos;


            // schießen, falls Munition vorhanden und Gegner nicht schießt
            if (this.Ammo > 0 && this.enemies[ind_enemy].Charge == 0.0f)
            {
                act.charge = true;
            }

            if (this.Charge == 1)
            {
                act.charge = false;
                act.aim    = (float)Math.Atan2(vec.Y, vec.X);
            }

            if (enemies[ind_enemy].Charge > 0.0f)
            {
                act.charge = false;

                float   wurzel = (float)(1f / Math.Sqrt(2));
                float[] test   = new float[8] {
                    Math.Abs(vec.X), Math.Abs(wurzel * (vec.X) + wurzel * (vec.Y)), Math.Abs(vec.Y), Math.Abs(-wurzel * (vec.X) + wurzel * (vec.Y)),
                    Math.Abs(-vec.X), Math.Abs(-wurzel * (vec.X) - wurzel * (vec.Y)), Math.Abs(-vec.Y), Math.Abs(wurzel * (vec.X) - wurzel * (vec.Y))
                };

                int ind = 0;
                for (int i = 1; i < 8; i++)
                {
                    if (test[i] < test[ind])
                    {
                        ind = i; break;
                    }
                }

                int fix = Env.constRand.Next(2);
                switch (ind)
                {
                case 0:
                    if (fix == 0)
                    {
                        act.right = true;
                    }
                    else
                    {
                        act.left = true;
                    }
                    break;

                case 1:
                    if (fix == 0)
                    {
                        act.right = true; act.down = true;
                    }
                    else
                    {
                        act.left = true; act.up = true;
                    }
                    break;

                case 2:
                    if (fix == 0)
                    {
                        act.down = true;
                    }
                    else
                    {
                        act.up = true;
                    }
                    break;

                case 3:
                    if (fix == 0)
                    {
                        act.left = true; act.down = true;
                    }
                    else
                    {
                        act.right = true; act.up = true;
                    }
                    break;

                case 4:
                    if (fix == 0)
                    {
                        act.left = true;
                    }
                    else
                    {
                        act.right = true;
                    }
                    break;

                case 5:
                    if (fix == 0)
                    {
                        act.left = true; act.up = true;
                    }
                    else
                    {
                        act.right = true; act.down = true;
                    }
                    break;

                case 6:
                    if (fix == 0)
                    {
                        act.up = true;
                    }
                    else
                    {
                        act.down = true;
                    }
                    break;

                case 7:
                    if (fix == 0)
                    {
                        act.right = true; act.up = true;
                    }
                    else
                    {
                        act.left = true; act.down = true;
                    }
                    break;

                default:
                    break;
                }
            }
            else
            {
                // collect bullets
                int ind = 0;
                if (env.Bullets.Length > 0)
                {
                    // search nearest bullet
                    for (int i = 0; i < env.Bullets.Length; i++)
                    {
                        if (env.Bullets[i].Collectible == true)
                        {
                            if (Vector2.Distance(this.Pos, env.Bullets[i].Pos) < Vector2.Distance(this.Pos, env.Bullets[ind].Pos))
                            {
                                ind = i;
                            }
                        }
                    }

                    // in which direction go first
                    if (env.Bullets[ind].Collectible == true)
                    {
                        Vector2 dist = env.Bullets[ind].Pos - this.Pos;
                        if (Math.Abs(dist.X) > Math.Abs(dist.Y))
                        {
                            if (dist.X > 0)
                            {
                                act.right = true;
                            }
                            else
                            {
                                act.left = true;
                            }
                        }
                        else
                        {
                            if (dist.Y > 0)
                            {
                                act.down = true;
                            }
                            else
                            {
                                act.up = true;
                            }
                        }
                    }
                    else
                    {
                        // Random
                        int dec = Env.constRand.Next(4);
                        act.right = (dec == 0);
                        act.left  = (dec == 1);
                        act.up    = (dec == 2);
                        act.down  = (dec == 3);
                    }
                }
                else
                {
                    // Random
                    int dec = Env.constRand.Next(4);
                    act.right = (dec == 0);
                    act.left  = (dec == 1);
                    act.up    = (dec == 2);
                    act.down  = (dec == 3);
                }
            }



            return(act);
        }
示例#12
0
        protected override EnvShooter.Action GetAction()
        {
            //Loop
            EnvShooter.Action test = new EnvShooter.Action()
            {
                right  = false,
                up     = false,
                left   = false,
                down   = false,
                charge = false,
                aim    = MathHelper.Pi
            };

            Vector2 movementDirection = new Vector2(0, 0);
            Vector2 temp = new Vector2(0, 0);
            Bullet  closestCollectible;
            bool    isSet = false;

            if (this.env.Bullets.Length > 0)
            {
                closestCollectible = this.env.Bullets[0];

                foreach (Bullet bullet in this.env.Bullets)
                {
                    if (!bullet.Collectible)
                    {
                        if (TestForCollision(bullet.Pos))
                        {
                            temp.X += (this.Pos.X - bullet.Pos.X);
                            temp.Y += (this.Pos.Y - bullet.Pos.Y);

                            movementDirection.X += temp.X;
                            movementDirection.Y += temp.Y;
                        }
                    }
                    else
                    {
                        if (getAbsoluteDistance(closestCollectible.Pos) < getAbsoluteDistance(bullet.Pos) || isSet == false)
                        {
                            closestCollectible = bullet;
                            isSet = true;
                        }
                    }
                }

                /*
                 * if (isSet)
                 * {
                 *  movementDirection.X += this.Pos.X - closestCollectible.Pos.X;
                 *  movementDirection.Y += this.Pos.Y - closestCollectible.Pos.Y;
                 * }*/
            }

            //movementDirection = RotateVector(movementDirection, 90);

            movementDirection.Normalize();

            if (movementDirection.Y > 0)
            {
                test.right = false;
                test.left  = true;
            }
            else if (movementDirection.Y == 0)
            {
            }
            else
            {
                test.right = true;
                test.left  = false;
            }
            if (movementDirection.X > 0)
            {
                test.down = false;
                test.up   = true;
            }
            else if (movementDirection.X == 0)
            {
            }
            else
            {
                test.down = true;
                test.up   = false;
            }

            return(test);
        }