示例#1
0
        /// <summary>
        /// Constructor for Team Information
        /// </summary>
        /// <param name="teamId">Instance of Team class, indicating the identifier for the TeamInformation instance (red/blue)</param>
        /// <param name="fullyAIControlled">Indicates whether the team is human controlled or not</param>
        /// <param name="ownedTurrets">Owned turrets at the start of the game</param>
        /// <param name="teamStartingCredits">Initial credits</param>
        /// <param name="teamPlayer">Team player object</param>
        /// <param name="ownedNodes">Team's patrol nodes</param>
        /// <param name="ownedSpawnPoints">Team's owned spawn points</param>
        /// <param name="maxFighters">Maximum number of fighters</param>
        /// <param name="maxDestroyers">Maximum number of destroyers</param>
        /// <param name="teamHomeBase">Team base instance</param>
        /// <param name="playerSpawnPt">Team's player's spawn point</param>
        public TeamInformation(Team teamId, bool fullyAIControlled, List<Turret> ownedTurrets,
            int teamStartingCredits, playerObject teamPlayer, List<Node> ownedNodes, List<SpawnPoint> ownedSpawnPoints,
            uint maxFighters, uint maxDestroyers, Base teamHomeBase, PlayerSpawnPoint playerSpawnPt)
        {
            this.teamId = teamId;
            this.fullyAIControlled = fullyAIControlled;
            this.ownedTurrets = ownedTurrets;
            this.teamCredits = teamStartingCredits;
            this.teamPlayer = teamPlayer;
            this.teamOwnedNodes = ownedNodes;
            this.teamSpawnPoints = ownedSpawnPoints;
            this.maxDestroyers = maxDestroyers;
            this.maxFighters = maxFighters;
            this.teamFighters = new List<Fighter>((int)maxFighters);
            this.teamDestroyers = new List<Destroyer>((int)maxDestroyers);
            this.teamBase = teamHomeBase;

            scrambleQueue = new PowerDataStructures.PriorityQueue<int, StaticObject>(true);
            fighterBattleList = new Dictionary<Fighter, StaticObject>();
            destroyerBattleList = new Dictionary<Destroyer, StaticObject>();
            turretBattleList = new Dictionary<Turret, StaticObject>();
            gunsCoolDown = new Dictionary<StaticObject, int>();
            spawnQueue = new List<DynamicObject>(ownedSpawnPoints.Count);
            playerTarget = null;
            playerObjective = null;
        }
示例#2
0
        /// <summary>
        /// Constructor for Team Information
        /// </summary>
        /// <param name="teamId">Instance of Team class, indicating the identifier for the TeamInformation instance (red/blue)</param>
        /// <param name="fullyAIControlled">Indicates whether the team is human controlled or not</param>
        /// <param name="ownedTurrets">Owned turrets at the start of the game</param>
        /// <param name="teamStartingCredits">Initial credits</param>
        /// <param name="teamPlayer">Team player object</param>
        /// <param name="ownedNodes">Team's patrol nodes</param>
        /// <param name="ownedSpawnPoints">Team's owned spawn points</param>
        /// <param name="maxFighters">Maximum number of fighters</param>
        /// <param name="maxDestroyers">Maximum number of destroyers</param>
        /// <param name="teamHomeBase">Team base instance</param>
        /// <param name="playerSpawnPt">Team's player's spawn point</param>
        public TeamInformation(Team teamId, bool fullyAIControlled, List <Turret> ownedTurrets,
                               int teamStartingCredits, playerObject teamPlayer, List <Node> ownedNodes, List <SpawnPoint> ownedSpawnPoints,
                               uint maxFighters, uint maxDestroyers, Base teamHomeBase, PlayerSpawnPoint playerSpawnPt)
        {
            this.teamId            = teamId;
            this.fullyAIControlled = fullyAIControlled;
            this.ownedTurrets      = ownedTurrets;
            this.teamCredits       = teamStartingCredits;
            this.teamPlayer        = teamPlayer;
            this.teamOwnedNodes    = ownedNodes;
            this.teamSpawnPoints   = ownedSpawnPoints;
            this.maxDestroyers     = maxDestroyers;
            this.maxFighters       = maxFighters;
            this.teamFighters      = new List <Fighter>((int)maxFighters);
            this.teamDestroyers    = new List <Destroyer>((int)maxDestroyers);
            this.teamBase          = teamHomeBase;

            scrambleQueue       = new PowerDataStructures.PriorityQueue <int, StaticObject>(true);
            fighterBattleList   = new Dictionary <Fighter, StaticObject>();
            destroyerBattleList = new Dictionary <Destroyer, StaticObject>();
            turretBattleList    = new Dictionary <Turret, StaticObject>();
            gunsCoolDown        = new Dictionary <StaticObject, int>();
            spawnQueue          = new List <DynamicObject>(ownedSpawnPoints.Count);
            playerTarget        = null;
            playerObjective     = null;
        }
示例#3
0
        //return all the objects in-front of the player for targetting
        public List <GridObjectInterface> getTargets(int distance, Microsoft.Xna.Framework.Matrix rotation, GridObjectInterface player)
        {
            List <GridObjectInterface> targets = new List <GridObjectInterface>();

            PowerDataStructures.PriorityQueue <Double, GridObjectInterface> queue = new PowerDataStructures.PriorityQueue <Double, GridObjectInterface>(true);

            Vector3[] boxPoints = new Vector3[7];
            //we want to take the length of the entire ship (if it is at the edge of a cell) into account (add the tip and the tail to the list):
            Vector3 frontPoint = Vector3.Normalize(rotation.Forward) * player.getBoundingSphere().Radius + player.Position;
            Vector3 backPoint  = Vector3.Normalize(rotation.Backward) * player.getBoundingSphere().Radius + player.Position;
            //now compute the left and right wing tip points:
            Vector3 leftPoint  = Vector3.Normalize(rotation.Left) * player.getBoundingSphere().Radius + player.Position;
            Vector3 rightPoint = Vector3.Normalize(rotation.Right) * player.getBoundingSphere().Radius + player.Position;
            //also compute the tail fin and the belly points
            Vector3 topPoint    = Vector3.Normalize(rotation.Up) * player.getBoundingSphere().Radius + player.Position;
            Vector3 bottomPoint = Vector3.Normalize(rotation.Down) * player.getBoundingSphere().Radius + player.Position;

            //convert the front and back points to grid coords:
            Vector3 objPosFront = new Vector3((int)Math.Round((double)((frontPoint.X) / GRID_BLOCK_SIZE)) + grid_offset,
                                              (int)Math.Round((double)((frontPoint.Y) / GRID_BLOCK_SIZE)) + grid_offset,
                                              (int)Math.Round((double)((frontPoint.Z) / GRID_BLOCK_SIZE)) + grid_offset); //players own position
            Vector3 objPosBack = new Vector3((int)Math.Round((double)((backPoint.X) / GRID_BLOCK_SIZE)) + grid_offset,
                                             (int)Math.Round((double)((backPoint.Y) / GRID_BLOCK_SIZE)) + grid_offset,
                                             (int)Math.Round((double)((backPoint.Z) / GRID_BLOCK_SIZE)) + grid_offset); //players own position

            //Construct the radar area (front, left and right, above and below):
            //now add the front and the back points to the list
            boxPoints[0] = objPosFront;
            boxPoints[1] = objPosBack;
            //add a distance to the left and right wingtips, as well as the tail and belly points
            boxPoints[2] = gridPtAlongDirection(rotation.Backward, backPoint, distance);
            boxPoints[3] = gridPtAlongDirection(rotation.Left, leftPoint, distance);
            boxPoints[4] = gridPtAlongDirection(rotation.Right, rightPoint, distance);
            boxPoints[5] = gridPtAlongDirection(rotation.Up, topPoint, distance);
            boxPoints[6] = gridPtAlongDirection(rotation.Down, bottomPoint, distance);
            //Now compute lower and upper bounds
            Vector3 lb = Vector3.Zero;
            Vector3 ub = Vector3.Zero;

            getMinimumsAndMaximums(boxPoints, out lb, out ub);
            //System.Diagnostics.Debug.WriteLine(lb + "  ,  " + ub);
            lb = clampCellCoordToGridLimits(lb);
            ub = clampCellCoordToGridLimits(ub);
            //Now get all the objects in front of the player:
            for (int x = (int)lb.X; x <= (int)ub.X; ++x)
            {
                for (int y = (int)lb.Y; y <= (int)ub.Y; ++y)
                {
                    for (int z = (int)lb.Z; z <= (int)ub.Z; ++z)
                    {
                        for (int i = 0; i < grid[x, y, z].Count; i++)
                        {
                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                            foreach (KeyValuePair <double, GridObjectInterface> value in queue)
                            {
                                if (value.Value == tempObj)
                                {
                                    goto dontAdd;
                                }
                            }
                            //check if object is within player's view cone +- 45 degrees
                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Backward), Vector3.Normalize(tempObj.Position - player.Position));
                            if (theta >= 0.707)
                            {
                                double dist = (tempObj.Position - player.Position).Length();
                                KeyValuePair <double, GridObjectInterface> temp = new KeyValuePair <double, GridObjectInterface>(dist, tempObj);
                                queue.Add(temp);
                            }
                            dontAdd : { /*DO NOTHING IF THE ITEM IS ALREADY ON THE QUEUE*/ }
                        }
                    }
                }
            }
            #region old-code

            /*
             * //convert real-world coords to grid coords
             * int gridX_pos = (int)Math.Round((double)((playerPosition.X) / GRID_BLOCK_SIZE)) + grid_offset;//for player position
             * int gridY_pos = (int)Math.Round((double)((playerPosition.Y) / GRID_BLOCK_SIZE)) + grid_offset;
             * int gridZ_pos = (int)Math.Round((double)((playerPosition.Z) / GRID_BLOCK_SIZE)) + grid_offset;
             *
             * int gridX_forward = (int)Math.Round((double)((rotation.Forward.X*distance) / GRID_BLOCK_SIZE)) + grid_offset;//forward position
             * int gridY_forward = (int)Math.Round((double)((rotation.Forward.Y*distance) / GRID_BLOCK_SIZE)) + grid_offset;
             * int gridZ_forward = (int)Math.Round((double)((rotation.Forward.Z * distance) / GRID_BLOCK_SIZE)) + grid_offset;*/

            //loop through blocks in-front of player

            /*
             #region along x-axis
             * if (gridX_pos < gridX_forward)
             * {
             *  for (int x = gridX_pos; x < gridX_forward; x++)
             *  {
             #region along y-axis
             *      if (gridY_pos < gridY_forward)
             *      {
             *          for (int y = gridY_pos; y < gridY_forward; y++)
             *          {
             #region along z-axis
             *              if (gridZ_pos < gridZ_forward)
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z++)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             *              else
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z--)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             #endregion
             *          }
             *      }
             *      else
             *      {
             *          for (int y = gridY_pos; y < gridY_forward; y--)
             *          {
             #region along z-axis
             *              if (gridZ_pos < gridZ_forward)
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z++)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             *              else
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z--)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             #endregion
             *          }
             *      }
             #endregion
             *  }
             * }
             * else
             * {
             *  for (int x = gridX_pos; x < gridX_forward; x--)
             *  {
             #region along y-axis
             *      if (gridY_pos < gridY_forward)
             *      {
             *          for (int y = gridY_pos; y < gridY_forward; y++)
             *          {
             #region along z-axis
             *              if (gridZ_pos < gridZ_forward)
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z++)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             *              else
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z--)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             #endregion
             *          }
             *      }
             *      else
             *      {
             *          for (int y = gridY_pos; y < gridY_forward; y--)
             *          {
             #region along z-axis
             *              if (gridZ_pos < gridZ_forward)
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z++)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             *              else
             *              {
             *                  for (int z = gridZ_pos; z < gridZ_forward; z--)
             *                  {
             *                      if (grid[x, y, z].Count > 0)
             *                      {
             *                          for (int i = 0; i < grid[x, y, z].Count; i++)
             *                          {
             *                              GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
             *                              //check if object is within player's view cone +- 45 degrees
             *                              float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
             *                              //if (theta >= 0.75)
             *                              {
             *                                  double dist = (tempObj.Position - playerPosition).Length();
             *                                  KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
             *                                  queue.Add(temp);
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             #endregion
             *          }
             *      }
             #endregion
             *  }
             * }
             #endregion
             */
            #endregion
            for (int i = 0; i < queue.Count; ++i)
            {
                targets.Add(queue.ElementAt(i).Value);
            }

            //return queue.Count;

            return(targets);
        }
示例#4
0
        private PowerDataStructures.PriorityQueue<float, Fighter> getHealthiestFighters(TeamInformation ti, FighterState requiredFighterState)
        {
            PowerDataStructures.PriorityQueue<float, Fighter> healthiestFighters = new PowerDataStructures.PriorityQueue<float,Fighter>(true);
            foreach (Fighter fi in ti.teamFighters)
                if (requiredFighterState == FighterState.FS_IDLE)
                {
                    if (!isFighterEngagedInBattle(ti,fi))
                        healthiestFighters.Add(new KeyValuePair<float,Fighter>(fi.getHealth,fi));
                }
                else if (requiredFighterState == FighterState.FS_ENGAGED)
                {
                    if (!isFighterEngagedInBattle(ti, fi))
                        healthiestFighters.Add(new KeyValuePair<float, Fighter>(fi.getHealth, fi));
                }
                else if (requiredFighterState == FighterState.FS_DONTCARE)
                    healthiestFighters.Add(new KeyValuePair<float, Fighter>(fi.getHealth, fi));

            return healthiestFighters;
        }
示例#5
0
        private void detectEnemyTargets()
        {
            foreach (TeamInformation homeTeam in infoOnTeams)
                foreach (TeamInformation enemyTeam in infoOnTeams)
                    if (homeTeam != enemyTeam)
                    {
                        //Fighter detects enemy? (Fighters only concerned with enemy player and destroyers --- defensive measures):
                        foreach (Fighter homeFi in homeTeam.teamFighters)
                        {
                            PowerDataStructures.PriorityQueue<float, DynamicObject> targetQueue = new PowerDataStructures.PriorityQueue<float, DynamicObject>(true);
                            foreach (Destroyer enemyDes in enemyTeam.teamDestroyers)
                            {
                                float dist = (enemyDes.Position - homeFi.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, DynamicObject>(this.getEliminationPriority(enemyDes) * 100 + dist, enemyDes));
                            }
                            float distToPlayer = (enemyTeam.teamPlayer.Position - homeFi.Position).Length();
                            if (distToPlayer <= DETECTION_RADIUS)
                                targetQueue.Add(new KeyValuePair<float, DynamicObject>(this.getEliminationPriority(enemyTeam.teamPlayer) * 100 + distToPlayer, enemyTeam.teamPlayer));
                            if (targetQueue.Count > 0)
                                if (!isTargetMarkedForElimination(targetQueue.PeekValue(), homeTeam))
                                    if (!isTargetAlreadyBattledByFighters(homeTeam, targetQueue.PeekValue()))
                                        homeTeam.scrambleQueue.Add(new KeyValuePair<int, StaticObject>(this.getEliminationPriority(targetQueue.PeekValue()), targetQueue.PeekValue()));

                        }
                        //Destroyer detects enemy? (player/turret/destroyer/fighter/base --- offensive measures):
                        foreach (Destroyer homeDs in homeTeam.teamDestroyers)
                        {
                            PowerDataStructures.PriorityQueue<float, StaticObject> targetQueue = new PowerDataStructures.PriorityQueue<float, StaticObject>(true);
                            foreach (Destroyer enemyDes in enemyTeam.teamDestroyers)
                            {
                                float dist = (enemyDes.Position - homeDs.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyDes) * 100 + dist, enemyDes));
                            }
                            float distToPlayer = (enemyTeam.teamPlayer.Position - homeDs.Position).Length();
                            if (distToPlayer <= DETECTION_RADIUS)
                                targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTeam.teamPlayer) * 100 + distToPlayer, enemyTeam.teamPlayer));
                            float distToBase = (enemyTeam.teamBase.Position - homeDs.Position).Length();
                            if (distToBase <= DETECTION_RADIUS)
                                targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTeam.teamBase) * 100 + distToPlayer, enemyTeam.teamBase));
                            foreach (Fighter enemyFi in enemyTeam.teamFighters)
                            {
                                float dist = (enemyFi.Position - homeDs.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyFi) * 100 + dist, enemyFi));
                            }
                            foreach (Turret enemyTurret in enemyTeam.ownedTurrets)
                            {
                                float dist = (enemyTurret.Position - homeDs.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTurret) * 100 + dist, enemyTurret));
                            }
                            if (targetQueue.Count > 0)
                                if (isDestroyerEngaged(homeTeam, homeDs))
                                    homeTeam.destroyerBattleList[homeDs] = targetQueue.PeekValue();
                                else
                                    homeTeam.destroyerBattleList.Add(homeDs, targetQueue.PeekValue());

                        }
                        //Turret detects enemy?
                        foreach (Turret homeTurret in homeTeam.ownedTurrets)
                        {
                            PowerDataStructures.PriorityQueue<float, StaticObject> targetQueue = new PowerDataStructures.PriorityQueue<float, StaticObject>(true);
                            foreach (Destroyer enemyDes in enemyTeam.teamDestroyers)
                            {
                                float dist = (enemyDes.Position - homeTurret.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyDes) * 100 + dist, enemyDes));
                            }
                            float distToPlayer = (enemyTeam.teamPlayer.Position - homeTurret.Position).Length();
                            if (distToPlayer <= DETECTION_RADIUS)
                                targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTeam.teamPlayer) * 100 + distToPlayer, enemyTeam.teamPlayer));
                            float distToBase = (enemyTeam.teamBase.Position - homeTurret.Position).Length();
                            if (distToBase <= DETECTION_RADIUS)
                                targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTeam.teamBase) * 100 + distToPlayer, enemyTeam.teamBase));
                            foreach (Fighter enemyFi in enemyTeam.teamFighters)
                            {
                                float dist = (enemyFi.Position - homeTurret.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyFi) * 100 + dist, enemyFi));
                            }
                            foreach (Turret enemyTurret in enemyTeam.ownedTurrets)
                            {
                                float dist = (enemyTurret.Position - homeTurret.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTurret) * 100 + dist, enemyTurret));
                            }
                            if (targetQueue.Count > 0)
                                if (homeTeam.turretBattleList.Keys.Contains(homeTurret))
                                    homeTeam.turretBattleList[homeTurret] = targetQueue.PeekValue();
                                else
                                    homeTeam.turretBattleList.Add(homeTurret, targetQueue.PeekValue());
                        }
                        //Player detects enemy?
                        {
                            PowerDataStructures.PriorityQueue<float, StaticObject> targetQueue = new PowerDataStructures.PriorityQueue<float, StaticObject>(true);
                            foreach (Destroyer enemyDes in enemyTeam.teamDestroyers)
                            {
                                float dist = (enemyDes.Position - homeTeam.teamPlayer.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyDes) * 100 + dist, enemyDes));
                            }
                            float distToPlayer = (enemyTeam.teamPlayer.Position - homeTeam.teamPlayer.Position).Length();
                            if (distToPlayer <= DETECTION_RADIUS)
                                targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTeam.teamPlayer) * 100 + distToPlayer, enemyTeam.teamPlayer));
                            float distToBase = (enemyTeam.teamBase.Position - homeTeam.teamPlayer.Position).Length();
                            if (distToBase <= DETECTION_RADIUS)
                                targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTeam.teamBase) * 100 + distToPlayer, enemyTeam.teamBase));
                            foreach (Fighter enemyFi in enemyTeam.teamFighters)
                            {
                                float dist = (enemyFi.Position - homeTeam.teamPlayer.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyFi) * 100 + dist, enemyFi));
                            }
                            foreach (Turret enemyTurret in enemyTeam.ownedTurrets)
                            {
                                float dist = (enemyTurret.Position - homeTeam.teamPlayer.Position).Length();
                                if (dist <= DETECTION_RADIUS)
                                    targetQueue.Add(new KeyValuePair<float, StaticObject>(this.getEliminationPriority(enemyTurret) * 100 + dist, enemyTurret));
                            }
                            if (targetQueue.Count > 0)
                                homeTeam.playerTarget = targetQueue.PeekValue();
                        }
                    }
        }
示例#6
0
        //return all the objects in-front of the player for targetting
        public List<GridObjectInterface> getTargets(int distance, Microsoft.Xna.Framework.Matrix rotation, GridObjectInterface player)
        {
            List<GridObjectInterface> targets = new List<GridObjectInterface>();
            PowerDataStructures.PriorityQueue<Double,GridObjectInterface> queue = new PowerDataStructures.PriorityQueue<Double,GridObjectInterface>(true);

            Vector3[] boxPoints = new Vector3[7];
            //we want to take the length of the entire ship (if it is at the edge of a cell) into account (add the tip and the tail to the list):
            Vector3 frontPoint = Vector3.Normalize(rotation.Forward) * player.getBoundingSphere().Radius + player.Position;
            Vector3 backPoint = Vector3.Normalize(rotation.Backward) * player.getBoundingSphere().Radius + player.Position;
            //now compute the left and right wing tip points:
            Vector3 leftPoint = Vector3.Normalize(rotation.Left) * player.getBoundingSphere().Radius + player.Position;
            Vector3 rightPoint = Vector3.Normalize(rotation.Right) * player.getBoundingSphere().Radius + player.Position;
            //also compute the tail fin and the belly points
            Vector3 topPoint = Vector3.Normalize(rotation.Up) * player.getBoundingSphere().Radius + player.Position;
            Vector3 bottomPoint = Vector3.Normalize(rotation.Down) * player.getBoundingSphere().Radius + player.Position;

            //convert the front and back points to grid coords:
            Vector3 objPosFront = new Vector3((int)Math.Round((double)((frontPoint.X) / GRID_BLOCK_SIZE)) + grid_offset,
            (int)Math.Round((double)((frontPoint.Y) / GRID_BLOCK_SIZE)) + grid_offset,
            (int)Math.Round((double)((frontPoint.Z) / GRID_BLOCK_SIZE)) + grid_offset); //players own position
            Vector3 objPosBack = new Vector3((int)Math.Round((double)((backPoint.X) / GRID_BLOCK_SIZE)) + grid_offset,
            (int)Math.Round((double)((backPoint.Y) / GRID_BLOCK_SIZE)) + grid_offset,
            (int)Math.Round((double)((backPoint.Z) / GRID_BLOCK_SIZE)) + grid_offset); //players own position

            //Construct the radar area (front, left and right, above and below):
            //now add the front and the back points to the list
            boxPoints[0] = objPosFront;
            boxPoints[1] = objPosBack;
            //add a distance to the left and right wingtips, as well as the tail and belly points
            boxPoints[2] = gridPtAlongDirection(rotation.Backward,backPoint, distance);
            boxPoints[3] = gridPtAlongDirection(rotation.Left, leftPoint, distance);
            boxPoints[4] = gridPtAlongDirection(rotation.Right, rightPoint, distance);
            boxPoints[5] = gridPtAlongDirection(rotation.Up, topPoint, distance);
            boxPoints[6] = gridPtAlongDirection(rotation.Down, bottomPoint, distance);
            //Now compute lower and upper bounds
            Vector3 lb = Vector3.Zero;
            Vector3 ub = Vector3.Zero;
            getMinimumsAndMaximums(boxPoints, out lb, out ub);
            //System.Diagnostics.Debug.WriteLine(lb + "  ,  " + ub);
            lb = clampCellCoordToGridLimits(lb);
            ub = clampCellCoordToGridLimits(ub);
            //Now get all the objects in front of the player:
            for (int x = (int)lb.X; x <= (int)ub.X; ++x)
                for (int y = (int)lb.Y; y <= (int)ub.Y; ++y)
                    for (int z = (int)lb.Z; z <= (int)ub.Z; ++z)
                        for (int i = 0; i < grid[x, y, z].Count; i++)
                        {
                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                            foreach(KeyValuePair<double,GridObjectInterface> value in queue)
                                if (value.Value == tempObj)
                                    goto dontAdd;
                            //check if object is within player's view cone +- 45 degrees
                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Backward), Vector3.Normalize(tempObj.Position - player.Position));
                            if (theta >= 0.707)
                            {
                                double dist = (tempObj.Position - player.Position).Length();
                                KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                queue.Add(temp);
                            }
                        dontAdd: { /*DO NOTHING IF THE ITEM IS ALREADY ON THE QUEUE*/ }
                        }
            #region old-code
            /*
            //convert real-world coords to grid coords
            int gridX_pos = (int)Math.Round((double)((playerPosition.X) / GRID_BLOCK_SIZE)) + grid_offset;//for player position
            int gridY_pos = (int)Math.Round((double)((playerPosition.Y) / GRID_BLOCK_SIZE)) + grid_offset;
            int gridZ_pos = (int)Math.Round((double)((playerPosition.Z) / GRID_BLOCK_SIZE)) + grid_offset;

            int gridX_forward = (int)Math.Round((double)((rotation.Forward.X*distance) / GRID_BLOCK_SIZE)) + grid_offset;//forward position
            int gridY_forward = (int)Math.Round((double)((rotation.Forward.Y*distance) / GRID_BLOCK_SIZE)) + grid_offset;
            int gridZ_forward = (int)Math.Round((double)((rotation.Forward.Z * distance) / GRID_BLOCK_SIZE)) + grid_offset;*/

            //loop through blocks in-front of player

            /*
            #region along x-axis
            if (gridX_pos < gridX_forward)
            {
                for (int x = gridX_pos; x < gridX_forward; x++)
                {
                    #region along y-axis
                    if (gridY_pos < gridY_forward)
                    {
                        for (int y = gridY_pos; y < gridY_forward; y++)
                        {
                            #region along z-axis
                            if (gridZ_pos < gridZ_forward)
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z++)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);

                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z--)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        for (int y = gridY_pos; y < gridY_forward; y--)
                        {
                            #region along z-axis
                            if (gridZ_pos < gridZ_forward)
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z++)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z--)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion
                }
            }
            else
            {
                for (int x = gridX_pos; x < gridX_forward; x--)
                {
                    #region along y-axis
                    if (gridY_pos < gridY_forward)
                    {
                        for (int y = gridY_pos; y < gridY_forward; y++)
                        {
                            #region along z-axis
                            if (gridZ_pos < gridZ_forward)
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z++)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z--)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        for (int y = gridY_pos; y < gridY_forward; y--)
                        {
                            #region along z-axis
                            if (gridZ_pos < gridZ_forward)
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z++)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int z = gridZ_pos; z < gridZ_forward; z--)
                                {
                                    if (grid[x, y, z].Count > 0)
                                    {
                                        for (int i = 0; i < grid[x, y, z].Count; i++)
                                        {
                                            GridObjectInterface tempObj = grid[x, y, z].ElementAt(i);
                                            //check if object is within player's view cone +- 45 degrees
                                            float theta = Vector3.Dot(Vector3.Normalize(rotation.Forward), Vector3.Normalize(tempObj.Position-playerPosition));
                                            //if (theta >= 0.75)
                                            {
                                                double dist = (tempObj.Position - playerPosition).Length();
                                                KeyValuePair<Double, GridObjectInterface> temp = new KeyValuePair<double, GridObjectInterface>(dist, tempObj);
                                                queue.Add(temp);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion
                }
            }
            #endregion
            */
            #endregion
            for (int i = 0; i < queue.Count; ++i)
            {
                targets.Add(queue.ElementAt(i).Value);
            }

            //return queue.Count;

            return targets;
        }