Beispiel #1
0
        public void Parse(Game game, ArrayList teams /*, Scenario2DPainter scenario2DPainter */)
        {
            String line;
            Team team = null;   //current team

            if (File.Exists(filePath))
            {
                StreamReader file = null;
                try
                {
                    file = new StreamReader(filePath);
                    while ((line = file.ReadLine()) != null)
                    {
                        line = line.TrimStart(' '); //remove whitespaces from the start of the line
                        if (line.Equals("") || line.StartsWith("//"))    //ignore empty and comment lines
                            continue;
                        else if (line.StartsWith("T:")) //new team
                        {
                            String teamName = line.Substring(2).TrimStart(' ');
                            team = new Team(game, teamName, Color.White);

                            //add team to teams
                            teams.Add(team);
                        }
                        else if (line.StartsWith("C:"))
                        {
                            String color = line.Substring(2).TrimStart(' ');

                            byte r = byte.Parse(color.Substring(0, 3));
                            byte g = byte.Parse(color.Substring(3, 3));
                            byte b = byte.Parse(color.Substring(6, 3));

                            team.Color = new Color(r, g, b);
                        }
                        else if (line.StartsWith("P:")) //positioning
                        {
                            String teamStrategy = line.Substring(2).TrimStart(' ');
                            team.SetStrategy(teamStrategy);
                        }
                        else if (team != null)   //read players
                        {
                            String playerName = "", position = "", side = "";
                            int number, defense, goalkeeping, offense, shot, speed, stamina;
                            int extraWhiteSpaces = 0;

                            Player newPlayer = null;
                            String[] tokens = line.Split(' ');

                            int i = 0;  //in the end of the cycle it will be the index of the player number
                            double num;
                            //get player name
                            while (!double.TryParse(tokens[i], out num))    //while not a number
                            {
                                if (tokens[i].Equals(""))  //extra white space between player's names
                                {
                                    extraWhiteSpaces++;
                                    i++;
                                    continue;
                                }
                                if (i == 0) playerName += tokens[i];
                                else playerName += " " + tokens[i];
                                i++;
                            }
                            int j = i;
                            i -= extraWhiteSpaces;
                            while (j < tokens.Length)   //extra white space between the rest of the info
                            {
                                if (tokens[j].Equals(""))
                                {
                                    extraWhiteSpaces++;
                                }
                                j++;
                            }
                            //get player properties
                            if (tokens.Length - i == 9 + extraWhiteSpaces) //correct amount of info
                            {
                                //limit the skill level allowed
                                int maxSkill = 100;
                                try
                                {
                                    i += extraWhiteSpaces;
                                    number = int.Parse(tokens[i++]);
                                    position = tokens[i++];
                                    side = tokens[i++];
                                    defense = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    goalkeeping = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    offense = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    shot = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    speed = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    stamina = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);

                                    TacticalPosition position2 = TacticalPosition.goalkeeper;
                                    Side side2 = Side.center;

                                    if (position.Equals("defender")) position2 = TacticalPosition.defender;
                                    else if (position.Equals("dfmidfielder")) position2 = TacticalPosition.dfmidfielder;
                                    else if (position.Equals("midfielder")) position2 = TacticalPosition.midfielder;
                                    else if (position.Equals("ofmidfielder")) position2 = TacticalPosition.ofmidfielder;
                                    else if (position.Equals("striker")) position2 = TacticalPosition.striker;

                                    if (side.Equals("left")) side2 = Side.left;
                                    else if (side.Equals("right")) side2 = Side.right;

                                    //create new player
                                    newPlayer = new Player(game, team, playerName, number, position2, side2, defense, goalkeeping, offense, shot, speed, stamina /*scenario2DPainter*/, 0f, 0f);

                                    //insert player in team
                                    team.Players.Add(newPlayer);
                                }
                                catch (FormatException)
                                {
                                    Console.WriteLine("Wrong line format for player " + playerName + "!");
                                }
                            }
                            else Console.WriteLine("Incorrect amount of info in line: " + line);

                        }
                        Console.WriteLine(line);
                    }
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            }
            else Console.WriteLine("Teams file not found!");
        }
Beispiel #2
0
        //returns this team's closest player to the given position
        public Player GetClosestPlayer(Vector2 position, Player ignore)
        {
            Player tmp = null;

            float tempDist = float.MaxValue;
            float newDist = 0f;

            foreach (Player p in Players)
            {
                if (p == ignore)
                    continue;
                newDist = (float)Math.Sqrt(Math.Pow((p.Position.X - position.X), 2) + Math.Pow((p.Position.Y - position.Y), 2));
                if (newDist <= tempDist)
                {
                    tmp = p;
                    tempDist = newDist;
                }
            }
            return tmp;
        }
Beispiel #3
0
 public Player GetClosestPlayer(Team team, Player ignore)
 {
     int closest = 0;    //current closest
     float tempDist = float.MaxValue;
     float newDist = 0f;
     for (int i = 0; i < team.Players.Count; i++)
     {
         Player p = (Player)team.Players[i];
         if (p.Equals(ignore))
             continue;
         newDist = (float)Math.Sqrt(Math.Pow((p.Position.X - Position.X), 2) + Math.Pow((p.Position.Y - Position.Y), 2));
         if (newDist <= tempDist)
         {
             closest = i;
             tempDist = newDist;
         }
     }
     return (Player)team.Players[closest];
 }
Beispiel #4
0
        //escolhe o jogador para que o jogador tenta passar através de um tunel de cerca de 30 graus na direccao do passe e o tempo que a tecla foi premida
        public Player ChoosePassPlayer(Player origin, float PassForce, Vector2 aim)
        {
            ArrayList eligiblePlayers = new ArrayList();    //players who can receive the pass
            Vector2 areaX;
            Vector2 areaY;

            if(aim.X > 0) areaX = new Vector2(origin.Position.X, -((Game)Game).Match.Field.Measures.Left);
            else if (aim.X < 0) areaX = new Vector2(((Game)Game).Match.Field.Measures.Left, origin.Position.X);
            else areaX = new Vector2(0);

            if(aim.Y > 0) areaY = new Vector2(origin.Position.Y, ((Game)Game).Match.Field.Measures.Bottom);
            else if (aim.Y < 0) areaY = new Vector2(((Game)Game).Match.Field.Measures.Top,origin.Position.Y);
            else areaY = new Vector2(0);

            //ArrayList players = GetOnScreenPlayers(11);

            foreach (Player p in Players)
            {
                if (p == origin) continue;
                if(aim.X != 0)  //para os lados ou diagonais
                {
                    if (aim.Y == 0)  //para um dos lados apenas
                    {
                        if (p.Position.X > areaX.X && p.Position.X <= areaX.Y
                            && p.Position.Y <= origin.Position.Y + (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(25f * Math.PI / 180f)
                            && p.Position.Y >= origin.Position.Y - (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(25f * Math.PI / 180f))
                            eligiblePlayers.Add(p);
                    }
                    else    //diagonais
                    {
                        if (aim.Y < 0)  //diagonal para cima
                        {
                            if (p.Position.X > areaX.X && p.Position.X <= areaX.Y
                                && p.Position.Y >= (origin.Position.Y - (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(80f * Math.PI / 180f))    //abaixo de
                                && p.Position.Y <= (origin.Position.Y - (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(10f * Math.PI / 180f)))   //acima de
                            {
                                /*Console.WriteLine("passer: " + origin.Position.X + ", " + origin.Position.Y);
                                Console.WriteLine("abaixo de: " + (origin.Position.Y - (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(90f * Math.PI / 180f)));
                                Console.WriteLine("acima de: " + (origin.Position.Y - (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(10f * Math.PI / 180f)));
                                */
                                eligiblePlayers.Add(p);
                            }
                        }
                        else    //diagonal para baixo
                        {
                            if (p.Position.X > areaX.X && p.Position.X <= areaX.Y
                                && p.Position.Y >= origin.Position.Y + (Math.Abs(p.Position.X + origin.Position.X)) * Math.Sin(10f * Math.PI / 180f)
                                && p.Position.Y <= origin.Position.Y + (Math.Abs(p.Position.X + origin.Position.X)) * Math.Sin(80f * Math.PI / 180f))
                            {
                                /*Console.WriteLine("passer: " + origin.Position.X + ", " + origin.Position.Y);
                                Console.WriteLine("abaixo de: " + (origin.Position.Y - (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(90f * Math.PI / 180f)));
                                Console.WriteLine("acima de: " + (origin.Position.Y - (Math.Abs(p.Position.X - origin.Position.X)) * Math.Sin(10f * Math.PI / 180f)));
                                */
                                eligiblePlayers.Add(p);
                            }
                        }
                    }
                }
                else if (aim.Y != 0)    //para cima ou baixo
                {
                    if (aim.X == 0)     //cima ou baixo
                    {
                        if (p.Position.Y > areaY.X && p.Position.Y <= areaY.Y
                            && p.Position.X <= origin.Position.X + (Math.Abs(p.Position.Y - origin.Position.Y)) * Math.Cos(25f * Math.PI / 180f)
                            && p.Position.X >= origin.Position.X - (Math.Abs(p.Position.Y - origin.Position.Y)) * Math.Cos(25f * Math.PI / 180f))
                            eligiblePlayers.Add(p);
                    }
                }
            }
            eligiblePlayers.Sort();

            if (eligiblePlayers.Count == 0)
                return ((Game)Game).Match.Ball.GetClosestPlayer(this, origin);
            else
            {
                int maxForce = 50;
                float div = maxForce / eligiblePlayers.Count;
                for (int i = 1; i <= eligiblePlayers.Count; i++)
                {
                    if (PassForce <= div * i) return (Player)eligiblePlayers[i - 1];
                }
            }
            return (Player)eligiblePlayers[0];
        }
Beispiel #5
0
 public AIPlayer getPlayer(Player player)
 {
     foreach(AIPlayer p in Players)
     {
         if (p.Player == player)
             return p;
     }
     return null;
 }
Beispiel #6
0
 public AIPlayer(Game game, Player player)
     : base(game)
 {
     Player = player;
 }
Beispiel #7
0
        public void Pass(GameTime gameTime, Player destination)
        {
            if (HasBall && !Numb)
            {
                HasBall = false;

                Vector2 direction = destination.Position - Position;

                //perfect pass

                float passDistance = (float)Math.Sqrt(Math.Pow(direction.X, 2) + Math.Pow(direction.Y, 2));
                float perfectPassFactor = ((Game)Game).Match.PerfectPassFactor;

                if (passDistance <= 250 && destination.TacticalPosition != TacticalPosition.goalkeeper)
                    perfectPassFactor = 3.5f;

                float speed = passDistance * perfectPassFactor;

                float passSpeed = Shot * (((Game)Game).Match.MaxPassSpeed / 100f); //this player's max pass speed

                if (speed > passSpeed)
                    speed = passSpeed;

                float errorFactor = (100 - Offense) * ((((Game)Game).Match.MaxErrorAngle-5.0f) / 100f);

                if (errorFactor != 0)
                {
                    float angle = (float)((Game)Game).Match.Rand.Next(0, (int)errorFactor);    //deviation angle due to lack of skill
                    Console.WriteLine("error angle: " + angle);

                    bool rand = (angle >= errorFactor / 2f) ? true : false;
                    direction.X += (rand) ? (float)Math.Cos(angle * Math.PI / 180f) * direction.X : -(float)Math.Cos((angle + 270) * Math.PI / 180f) * direction.X;
                    direction.Y -= (rand) ? (float)Math.Sin(angle * Math.PI / 180f) * direction.Y : -(float)Math.Sin(angle * Math.PI / 180f) * direction.Y;
                }
                else Console.WriteLine("error angle: 0");

                direction.Normalize();
                direction *= speed;

                ((Game)Game).Match.Ball.ApplyImpulse(direction);

                ((Game)Game).Match.Ball.Pass = true;

                if (Team.IsHomeTeam) ((Game)Game).Match.Player1 = destination;
                else ((Game)Game).Match.Player2 = destination;

                Numb = true;

                PassForce = 0f;
            }
        }
Beispiel #8
0
 //tells if the given player's in a box
 public bool InTheBox(Player player)
 {
     if (player.position.Y > ((Game)Game).Match.Field.Measures.Bottom - ((Game)Game).Match.Field.Measures.BoxHeight ||
         player.position.Y < ((Game)Game).Match.Field.Measures.Top + ((Game)Game).Match.Field.Measures.BoxHeight)
     {
         if (player.position.X > -((Game)Game).Match.Field.Measures.HalfBoxWidth &&
             player.position.X < ((Game)Game).Match.Field.Measures.HalfBoxWidth)
             return true;
     }
     return false;
 }