Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

AI::Player Class Reference

#include <AI.h>

Collaboration diagram for AI::Player:

Collaboration graph
[legend]
List of all members.

Detailed Description

A computer controlled player.

Definition at line 40 of file AI.h.

Public Methods

 Player (const Vec3D teamColor, vector< MilitaryUnit * > &units, vector< City * > &cities, Map::WorldMap worldMap)
 Creates a player and assigns it to the specified team.

void update ()
 Updates the AI player. Calculating threats, building units, etc...

Vec3D color () const
 Tells which team the player is on.

const ThreatMapthreatMap ()
 gets the threat map, usefull for debugging.


Private Methods

void clearThreatMap ()
void buildThreatMap ()
void sendUnitsToCity (vector< MilitaryUnit * > &units, const City *city)
 send units to a city to capture it. PRE: The vector of military units is not empty. POST: Some number of units(for now, at most 2) is sent to attack the target city.


Private Attributes

Vec3D m_teamColor
vector< MilitaryUnit * > & m_units
vector< City * > & m_cities
ThreatMap m_threatMap
Map::WorldMap m_worldMap


Constructor & Destructor Documentation

AI::Player::Player const Vec3D    teamColor,
vector< MilitaryUnit * > &    units,
vector< City * > &    cities,
Map::WorldMap    worldMap
 

Creates a player and assigns it to the specified team.

Parameters:
teamColor the team which this AI player will represent.
units the list of ALL the units in the game.
cities 
worldMap 

Definition at line 34 of file AI.cpp.

References Overhead::cities, and Overhead::units.

00037                                         :        m_teamColor(teamColor),        
00038                                        m_units(units),
00039                                        m_cities(cities),
00040                                        m_threatMap(wm.gridWidth, vector<FLOAT>(wm.gridHeight)),
00041                                        m_worldMap(wm) 
00042   {
00043     //clearThreatMap();
00044   }


Member Function Documentation

void AI::Player::buildThreatMap   [private]
 

Definition at line 141 of file AI.cpp.

References Int2Tuple::a, Int2Tuple::b, clearThreatMap(), Map::WorldMap::gridHeight, Map::WorldMap::gridWidth, m_teamColor, m_threatMap, m_units, m_worldMap, AI::UnitIter, and Map::WorldMap::worldToGridCoord().

Referenced by update().

00142   {
00143     clearThreatMap();   
00144     for(UnitIter iter=m_units.begin(); iter != m_units.end(); ++iter)
00145     {
00146       
00147       // dont calculate my units
00148       if( (*iter)->color() != m_teamColor )
00149       {
00150         // a unit should have a great affect on the grid cell its in and 
00151         // a lesser affect to each neighbor cell. so increment the containing cell twice.
00152         Int2Tuple gridCoord = m_worldMap.worldToGridCoord((*iter)->position());
00153         m_threatMap[gridCoord.a][gridCoord.b] += AI::unitThreatIncr;
00154         // calculate value for surrounding cells
00155         int endRow = gridCoord.a+1 <= m_worldMap.gridHeight-1 ? gridCoord.a+1 : m_worldMap.gridHeight-1;
00156         int endCol = gridCoord.b+1 <= m_worldMap.gridWidth-1 ? gridCoord.b+1 : m_worldMap.gridWidth-1;
00157         for(int row=(gridCoord.a-1 >= 0 ? gridCoord.a-1 : 0); row <= endRow; ++row)
00158         {
00159           for(int col=(gridCoord.b-1 >= 0 ? gridCoord.b-1 : 0); col <= endCol; ++col)
00160             m_threatMap[row][col] += AI::unitThreatIncr;
00161         }
00162      
00163       }
00164     }
00165  
00166   }

void AI::Player::clearThreatMap   [private]
 

Definition at line 130 of file AI.cpp.

References Map::WorldMap::gridHeight, Map::WorldMap::gridWidth, m_threatMap, and m_worldMap.

Referenced by buildThreatMap().

00131   {
00132     for(int     i=0; i < m_worldMap.gridHeight; ++i)
00133       for(int   j=0; j < m_worldMap.gridWidth;  ++j)
00134                           m_threatMap[i][j] = 0.0f;
00135   }

Vec3D AI::Player::color   const
 

Tells which team the player is on.

Definition at line 110 of file AI.cpp.

References m_teamColor, and Vec3D.

00110 {       return m_teamColor;     }

void AI::Player::sendUnitsToCity vector< MilitaryUnit * > &    units,
const City   city
[private]
 

send units to a city to capture it. PRE: The vector of military units is not empty. POST: Some number of units(for now, at most 2) is sent to attack the target city.

Definition at line 180 of file AI.cpp.

References City::position(), AI::UnitDist, AI::UnitIter, and Overhead::units.

Referenced by update().

00181   {
00182     // list of units and and there distance to the target city
00183     vector<UnitDist> unitDists;
00184     for(UnitIter iter=units.begin(); iter != units.end(); ++iter)
00185     {
00186       unitDists.push_back(UnitDist((*iter), Helper::distXY((*iter)->position(), city->position())));
00187     }
00188 
00189     // sort the units by the distance to the target city
00190     sort(unitDists.begin(), unitDists.end(), SmallerDistToCity());
00191 
00192     if(unitDists.size() > 0 && (!unitDists[0].first->isIdle())) unitDists[0].first->goal(city->position());
00193     if(unitDists.size() >= 2 && !unitDists[1].first->isIdle()) 
00194     {
00195       unitDists[1].first->goal(city->position());
00196     }
00197       
00198   }

const ThreatMap & AI::Player::threatMap  
 

gets the threat map, usefull for debugging.

Returns:
a 2d integer vector who's dims are: [AI::worldGridWidth] x [AI::worldGridHeight]

Definition at line 112 of file AI.cpp.

References m_threatMap, and AI::ThreatMap.

00112 { return m_threatMap; }

void AI::Player::update  
 

Updates the AI player. Calculating threats, building units, etc...

:

Definition at line 48 of file AI.cpp.

References buildThreatMap(), AI::CityIter, m_cities, m_teamColor, m_units, sendUnitsToCity(), and AI::UnitIter.

00049   {
00050     buildThreatMap();
00051 
00052     // build list of units i own
00054     //                     access to each list of units.
00055     vector<MilitaryUnit*> myUnits;
00056     UnitIter unitIter;
00057     for(unitIter = m_units.begin(); unitIter != m_units.end(); ++unitIter)
00058     {
00059       if( (*unitIter)->color() == m_teamColor ) myUnits.push_back((*unitIter));
00060     }
00061 
00062     // find least threatened neutral city
00063     vector<City*> neutralCities;
00064     vector<City*> enemyCities;
00065     CityIter cityIter;
00066     for(cityIter = m_cities.begin(); cityIter != m_cities.end(); ++cityIter)
00067     {
00068       if( (*cityIter)->color() == Globals::NO_OWNER ) 
00069         neutralCities.push_back(*cityIter);
00070       else if( (*cityIter)->color() != m_teamColor )
00071         enemyCities.push_back(*cityIter);
00072     }
00073 
00074     if( !neutralCities.empty() )
00075     {
00076       // sort cities by their threatened valued. less threatened cities first
00077       sort(neutralCities.begin(), neutralCities.end(), CityLessThreatened(m_threatMap, m_worldMap) );
00078       // send units to the cities
00079       size_t numCities = neutralCities.size();
00080       size_t city;
00081       for(unitIter = myUnits.begin(), city=0; (unitIter != myUnits.end()) && city < numCities; ++unitIter, ++city)
00082       {
00083             if(!(*unitIter)->isIdle()) (*unitIter)->goal( neutralCities[city]->position() );
00084       }
00085 
00086     }
00087     // send the units to the enemy cites
00088     else
00089     {
00090       // sort cities by their threatened valued. less threatened cities first
00091       sort(enemyCities.begin(), enemyCities.end(), CityLessThreatened(m_threatMap, m_worldMap) );
00092       // send the two closest units to each of the cities
00093       //size_t numCities = enemyCities.size();
00094       //size_t city;
00095       //for(unitIter = myUnits.begin(), city=0; (unitIter != myUnits.end()) && city < numCities; ++unitIter, ++city)
00096       //{
00097       //  if( (*unitIter)->currState != MilitaryUnit::BUSY )
00098       //    (*unitIter)->goal( enemyCities[city]->position() );
00099       //}
00100       for(cityIter = enemyCities.begin(); cityIter != enemyCities.end(); ++cityIter)
00101       {
00102         sendUnitsToCity(myUnits, *cityIter);
00103       }
00104     }
00105 
00106   }


Member Data Documentation

vector<City*>& AI::Player::m_cities [private]
 

Definition at line 68 of file AI.h.

Referenced by update().

Vec3D AI::Player::m_teamColor [private]
 

Definition at line 66 of file AI.h.

Referenced by buildThreatMap(), color(), and update().

ThreatMap AI::Player::m_threatMap [private]
 

Definition at line 69 of file AI.h.

Referenced by buildThreatMap(), clearThreatMap(), and threatMap().

vector<MilitaryUnit*>& AI::Player::m_units [private]
 

Definition at line 67 of file AI.h.

Referenced by buildThreatMap(), and update().

Map::WorldMap AI::Player::m_worldMap [private]
 

Definition at line 70 of file AI.h.

Referenced by buildThreatMap(), and clearThreatMap().


The documentation for this class was generated from the following files:
Generated on Wed Apr 23 05:52:17 2003 for Modern Warfare by doxygen1.3-rc2