BWAPI
|
00001 #include <BWSAL/ReservedMap.h> 00002 namespace BWSAL 00003 { 00004 ReservedMap* ReservedMap::s_reservedMap = NULL; 00005 00006 ReservedMap* ReservedMap::create() 00007 { 00008 if ( s_reservedMap ) 00009 { 00010 return s_reservedMap; 00011 } 00012 s_reservedMap = new ReservedMap(); 00013 return s_reservedMap; 00014 } 00015 00016 ReservedMap* ReservedMap::getInstance() 00017 { 00018 return ReservedMap::create(); 00019 } 00020 00021 void ReservedMap::destroy() 00022 { 00023 if ( s_reservedMap ) 00024 { 00025 delete s_reservedMap; 00026 } 00027 } 00028 00029 ReservedMap::ReservedMap() 00030 { 00031 m_reservedMap.resize( BWAPI::Broodwar->mapWidth(), BWAPI::Broodwar->mapHeight() ); 00032 m_reservedMap.setTo( BWAPI::UnitTypes::None ); 00033 } 00034 00035 ReservedMap::~ReservedMap() 00036 { 00037 s_reservedMap = NULL; 00038 } 00039 00040 void ReservedMap::reserveTiles( BWAPI::TilePosition position, BWAPI::UnitType type, int width, int height ) 00041 { 00042 for ( int x = position.x(); x < position.x() + width && x < (int)m_reservedMap.getWidth(); x++ ) 00043 { 00044 for ( int y = position.y(); y < position.y() + height && y < (int)m_reservedMap.getHeight(); y++ ) 00045 { 00046 m_reservedMap[x][y] = type; 00047 } 00048 } 00049 } 00050 00051 void ReservedMap::freeTiles( BWAPI::TilePosition position, int width, int height ) 00052 { 00053 for ( int x = position.x(); x < position.x() + width && x < (int)m_reservedMap.getWidth(); x++ ) 00054 { 00055 for ( int y = position.y(); y < position.y() + height && y < (int)m_reservedMap.getHeight(); y++ ) 00056 { 00057 m_reservedMap[x][y] = BWAPI::UnitTypes::None; 00058 } 00059 } 00060 } 00061 00062 bool ReservedMap::isReserved( int x, int y ) const 00063 { 00064 return m_reservedMap[x][y] != BWAPI::UnitTypes::None; 00065 } 00066 00067 bool ReservedMap::isReserved( BWAPI::TilePosition p ) const 00068 { 00069 return m_reservedMap[p.x()][p.y()] != BWAPI::UnitTypes::None; 00070 } 00071 00072 bool ReservedMap::canBuildHere( BWAPI::Unit* builder, BWAPI::TilePosition position, BWAPI::UnitType type ) const 00073 { 00074 if ( type.isAddon() ) 00075 { 00076 type = type.whatBuilds().first; 00077 } 00078 // returns true if we can build this type of unit here. Takes into account reserved tiles. 00079 if ( !BWAPI::Broodwar->canBuildHere( builder, position, type ) ) 00080 { 00081 return false; 00082 } 00083 for ( int x = position.x(); x < position.x() + type.tileWidth(); x++ ) 00084 { 00085 for ( int y = position.y(); y < position.y() + type.tileHeight(); y++ ) 00086 { 00087 if ( m_reservedMap[x][y] != BWAPI::UnitTypes::None ) 00088 { 00089 return false; 00090 } 00091 } 00092 } 00093 return true; 00094 } 00095 00096 BWAPI::UnitType ReservedMap::getReservedType( int x, int y ) const 00097 { 00098 return m_reservedMap[x][y]; 00099 } 00100 00101 BWAPI::UnitType ReservedMap::getReservedType( BWAPI::TilePosition p ) const 00102 { 00103 return m_reservedMap[p.x()][p.y()]; 00104 } 00105 }