Exemplo n.º 1
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);
        }
Exemplo n.º 2
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;
            }

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

            int player_collision = BulletSim.CollidesWith(this.Pos, 0.4f, bullet_poses);
            int min_col          = player_collision > -1 ? player_collision : int.MaxValue;

            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++)
                    {
                        // Border check
                        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;
                        }

                        // Bullet collision check
                        int min_bullet = BulletSim.CollidesWith(poses[j], 0.4f, bullet_poses);

                        // No collisions
                        if (min_bullet == -1)
                        {
                            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[j] > 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 * 0.5f;
            }
            else
            {
                color       = Color.HotPink * 0.5f;
                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)
                    {
                        // Check if it is safe to move towards the desired location
                        int collision = BulletSim.CollidesWith(bullets[min_len_idx].Pos, 0.4f, bullet_poses);
                        move_target = collision == -1 ? bullets[min_len_idx].Pos : this.Pos;
                    }
                    else
                    {
                        move_target = this.Pos;
                    }
                }
            }

            EnvShooter.Action action = GoToXY(move_target);
            action.aim    = AimToPos(aim_target);
            action.charge = do_charge;
            return(action);
        }