ProtoBot
Loading...
Searching...
No Matches
InfluenceMap Class Reference

Public Member Functions

void onStart ()
void onFrame ()
void toggleDraw ()
void draw () const
void addEnemyInfluence (const BWAPI::Position &pos, int strength)
void addAllyInfluence (const BWAPI::Position &pos, int strength)
void addEnemyBuildingInfluence (const BWAPI::Position &pos, const BWAPI::UnitType &type)
void addAllyBuildingInfluence (const BWAPI::Position &pos, const BWAPI::UnitType &type)
int getEnemyInfluence (int x, int y) const
int getAllyInfluence (int x, int y) const
int getEnemyInfluenceAtTile (const BWAPI::TilePosition &tile) const
int getAllyInfluenceAtTile (const BWAPI::TilePosition &tile) const
int getCombinedInfluenceAtTile (const BWAPI::TilePosition &tile) const
int buildingInfluenceValue (const BWAPI::UnitType &type) const
int getHeight () const
int getWidth () const
BWAPI::Position findWeakBuildingSpot ()
std::vector< BWAPI::Position > getNoMansAreas () const
std::string toStringSummary () const

Public Attributes

bool drawEnabled = true

Private Member Functions

void decayMaps ()
void normalizeMaps ()

Private Attributes

int width
int height
std::vector< std::vector< int > > enemyMap
std::vector< std::vector< int > > allyMap

Detailed Description

Definition at line 5 of file InfluenceMap.h.

Constructor & Destructor Documentation

◆ InfluenceMap()

InfluenceMap::InfluenceMap ( )

Definition at line 4 of file InfluenceMap.cpp.

4{}

Member Function Documentation

◆ addAllyBuildingInfluence()

void InfluenceMap::addAllyBuildingInfluence ( const BWAPI::Position & pos,
const BWAPI::UnitType & type )

Definition at line 243 of file InfluenceMap.cpp.

244{
245 int strength = buildingInfluenceValue(type);
246 addAllyInfluence(pos, strength);
247}

◆ addAllyInfluence()

void InfluenceMap::addAllyInfluence ( const BWAPI::Position & pos,
int strength )

Definition at line 44 of file InfluenceMap.cpp.

45{
46 int x = pos.x / 8;
47 int y = pos.y / 8;
48
49 int radius = 5;
50 for (int dx = -radius; dx <= radius; dx++)
51 {
52 for (int dy = -radius; dy <= radius; dy++)
53 {
54 int nx = x + dx;
55 int ny = y + dy;
56
57 if (nx >= 0 && nx < width && ny >= 0 && ny < height)
58 {
59 int dist = dx * dx + dy * dy;
60 if (dist <= radius * radius)
61 allyMap[nx][ny] += strength;
62 }
63 }
64 }
65}

◆ addEnemyBuildingInfluence()

void InfluenceMap::addEnemyBuildingInfluence ( const BWAPI::Position & pos,
const BWAPI::UnitType & type )

Definition at line 237 of file InfluenceMap.cpp.

238{
239 int strength = buildingInfluenceValue(type);
240 addEnemyInfluence(pos, strength);
241}

◆ addEnemyInfluence()

void InfluenceMap::addEnemyInfluence ( const BWAPI::Position & pos,
int strength )

Definition at line 21 of file InfluenceMap.cpp.

22{
23 int x = pos.x / 8;
24 int y = pos.y / 8;
25
26 int radius = 6;
27 for (int dx = -radius; dx <= radius; dx++)
28 {
29 for (int dy = -radius; dy <= radius; dy++)
30 {
31 int nx = x + dx;
32 int ny = y + dy;
33
34 if (nx >= 0 && nx < width && ny >= 0 && ny < height)
35 {
36 int dist = dx * dx + dy * dy;
37 if (dist <= radius * radius)
38 enemyMap[nx][ny] += strength;
39 }
40 }
41 }
42}

◆ buildingInfluenceValue()

int InfluenceMap::buildingInfluenceValue ( const BWAPI::UnitType & type) const

Definition at line 230 of file InfluenceMap.cpp.

231{
232 //This can be expanded upon to have different buildings return different values
233 // but I'm not sure what those values should be, so there's just a placeholder for now
234 return 10;
235}

◆ decayMaps()

void InfluenceMap::decayMaps ( )
private

Definition at line 102 of file InfluenceMap.cpp.

103{
104 for (int x = 0; x < width; x++)
105 for (int y = 0; y < height; y++)
106 {
107 enemyMap[x][y] *= 0.98;
108 allyMap[x][y] *= 0.98;
109 }
110}

◆ draw()

void InfluenceMap::draw ( ) const

Definition at line 186 of file InfluenceMap.cpp.

187{
188 if (!drawEnabled)
189 return;
190
191 const int step = 8; // skip walktiles to reduce lag
192
193 for (int x = 0; x < width; x += step)
194 {
195 for (int y = 0; y < height; y += step)
196 {
197 int enemyInf = enemyMap[x][y];
198 int allyInf = allyMap[x][y];
199
200 if (enemyInf == 0 && allyInf == 0)
201 continue;
202
203 BWAPI::Position pos(x * 8, y * 8);
204
205 // Draw ally influence in blue-green
206 BWAPI::Broodwar->drawBoxMap(
207 pos,
208 BWAPI::Position(pos.x + 8, pos.y + 8),
209 influenceColor(allyInf),
210 false
211 );
212
213 // Draw enemy influence border in red
214 BWAPI::Broodwar->drawBoxMap(
215 pos,
216 BWAPI::Position(pos.x + 8, pos.y + 8),
217 influenceColor(enemyInf + allyInf),
218 false
219 );
220
221 // Print influence values
222 BWAPI::Broodwar->drawTextMap(
223 pos,
224 "%d/%d", allyInf, enemyInf
225 );
226 }
227 }
228}

◆ findWeakBuildingSpot()

BWAPI::Position InfluenceMap::findWeakBuildingSpot ( )

Definition at line 112 of file InfluenceMap.cpp.

113{
114 int bestScore = 9999999;
115 BWAPI::Position bestPos = BWAPI::Positions::Invalid;
116
117 for (int x = 0; x < width; x++)
118 for (int y = 0; y < height; y++)
119 {
120 int score = enemyMap[x][y] - allyMap[x][y];
121 if (score < bestScore)
122 {
123 bestScore = score;
124 bestPos = BWAPI::Position(x * 8, y * 8);
125 }
126 }
127
128 return bestPos;
129}

◆ getAllyInfluence()

int InfluenceMap::getAllyInfluence ( int x,
int y ) const

Definition at line 72 of file InfluenceMap.cpp.

73{
74 return allyMap[x][y];
75}

◆ getAllyInfluenceAtTile()

int InfluenceMap::getAllyInfluenceAtTile ( const BWAPI::TilePosition & tile) const

Definition at line 88 of file InfluenceMap.cpp.

89{
90 if (!tile.isValid()) return 0;
91 int wx = tile.x * 4 + 2;
92 int wy = tile.y * 4 + 2;
93 if (wx < 0 || wx >= width || wy < 0 || wy >= height) return 0;
94 return getAllyInfluence(wx, wy);
95}

◆ getCombinedInfluenceAtTile()

int InfluenceMap::getCombinedInfluenceAtTile ( const BWAPI::TilePosition & tile) const

Definition at line 97 of file InfluenceMap.cpp.

98{
99 return getEnemyInfluenceAtTile(tile) - getAllyInfluenceAtTile(tile);
100}

◆ getEnemyInfluence()

int InfluenceMap::getEnemyInfluence ( int x,
int y ) const

Definition at line 67 of file InfluenceMap.cpp.

68{
69 return enemyMap[x][y];
70}

◆ getEnemyInfluenceAtTile()

int InfluenceMap::getEnemyInfluenceAtTile ( const BWAPI::TilePosition & tile) const

Definition at line 78 of file InfluenceMap.cpp.

79{
80 if (!tile.isValid()) return 0;
81 // Each tile = 4 walktiles; use center walktile of the tile
82 int wx = tile.x * 4 + 2;
83 int wy = tile.y * 4 + 2;
84 if (wx < 0 || wx >= width || wy < 0 || wy >= height) return 0;
85 return getEnemyInfluence(wx, wy);
86}

◆ getHeight()

int InfluenceMap::getHeight ( ) const

Definition at line 249 of file InfluenceMap.cpp.

250{
251 return height;
252}

◆ getNoMansAreas()

std::vector< BWAPI::Position > InfluenceMap::getNoMansAreas ( ) const

Definition at line 131 of file InfluenceMap.cpp.

132{
133 std::vector<BWAPI::Position> spots;
134
135 for (int x = 0; x < width; x++)
136 for (int y = 0; y < height; y++)
137 if (enemyMap[x][y] < 10 && allyMap[x][y] < 10)
138 spots.push_back(BWAPI::Position(x * 8, y * 8));
139
140 return spots;
141}

◆ getWidth()

int InfluenceMap::getWidth ( ) const

Definition at line 254 of file InfluenceMap.cpp.

255{
256 return width;
257}

◆ onFrame()

void InfluenceMap::onFrame ( )

Definition at line 15 of file InfluenceMap.cpp.

16{
17 // Decay slightly to keep map "fresh"
18 decayMaps();
19}

◆ onStart()

void InfluenceMap::onStart ( )

Definition at line 6 of file InfluenceMap.cpp.

7{
8 width = BWAPI::Broodwar->mapWidth() * 4; // 1 tile = 4 walktiles
9 height = BWAPI::Broodwar->mapHeight() * 4;
10
11 enemyMap.assign(width, std::vector<int>(height, 0));
12 allyMap.assign(width, std::vector<int>(height, 0));
13}

◆ toggleDraw()

void InfluenceMap::toggleDraw ( )

Definition at line 170 of file InfluenceMap.cpp.

171{
172 drawEnabled = !drawEnabled;
173}

◆ toStringSummary()

std::string InfluenceMap::toStringSummary ( ) const

Definition at line 143 of file InfluenceMap.cpp.

144{
145 std::stringstream ss;
146
147 int enemyMax = 0, allyMax = 0;
148 long long enemySum = 0, allySum = 0;
149 int count = width * height;
150
151 for (int x = 0; x < width; x++)
152 for (int y = 0; y < height; y++)
153 {
154 int e = enemyMap[x][y];
155 int a = allyMap[x][y];
156 enemySum += e;
157 allySum += a;
158 if (e > enemyMax) enemyMax = e;
159 if (a > allyMax) allyMax = a;
160 }
161
162 ss << "Influence Map Summary:\n"
163 << " Size: " << width << " x " << height << "\n"
164 << " Enemy Influence: max=" << enemyMax << " avg=" << (enemySum / count) << "\n"
165 << " Ally Influence: max=" << allyMax << " avg=" << (allySum / count) << "\n";
166
167 return ss.str();
168}

Member Data Documentation

◆ allyMap

std::vector<std::vector<int> > InfluenceMap::allyMap
private

Definition at line 40 of file InfluenceMap.h.

◆ drawEnabled

bool InfluenceMap::drawEnabled = true

Definition at line 12 of file InfluenceMap.h.

◆ enemyMap

std::vector<std::vector<int> > InfluenceMap::enemyMap
private

Definition at line 39 of file InfluenceMap.h.

◆ height

int InfluenceMap::height
private

Definition at line 37 of file InfluenceMap.h.

◆ width

int InfluenceMap::width
private

Definition at line 36 of file InfluenceMap.h.


The documentation for this class was generated from the following files: