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

ThreatGrid Maintains spatial threat information for enemy units. Uses a grid-based system to efficiently query danger levels at any position, allowing units to avoid unsafe areas. More...

#include <ThreatGrid.h>

Classes

struct  EnemyStamp
struct  GridData

Public Member Functions

void onStart ()
 Initializes threat grids based on map dimensions.
void onFrameStart (int frame)
 Updates the current frame reference for the grid.
void addOrUpdateEnemy (int id, BWAPI::UnitType type, BWAPI::Position pos, bool completed, bool burrowed, bool immobile)
 Adds or updates an enemy unit's contribution to the threat grid.
void removeEnemy (int id)
 Removes an enemy unit from the threat grid.
int groundThreatAt (BWAPI::Position p) const
 Returns the ground threat value at a given position.
int detectionAt (BWAPI::Position p) const
 Returns the detection threat value at a given position.
int airThreatAt (BWAPI::Position p) const
 Returns the air threat value at a given position.
int getAirThreat (BWAPI::Position p) const
int getDetection (BWAPI::Position p) const
 Wrapper for retrieving detection threat at a position.
int airThreatValue (BWAPI::UnitType t) const
int airThreatRangePx (BWAPI::UnitType t) const

Private Member Functions

void paintDisc (GridData &g, BWAPI::Position center, int rangePx, int delta)
void stampEnemy (const EnemyStamp &s, int delta)
int detectorRadiusPx (BWAPI::UnitType t) const
int groundThreatValue (BWAPI::UnitType t) const
int groundThreatRangePx (BWAPI::UnitType t) const

Static Private Member Functions

static int toWalkX (BWAPI::Position p)
static int toWalkY (BWAPI::Position p)

Private Attributes

int currentFrame_ = 0
int rangeBufferPx_ = 16
GridData groundThreat_
GridData airThreat_
GridData detection_
std::unordered_map< int, EnemyStampstamps_

Detailed Description

ThreatGrid Maintains spatial threat information for enemy units. Uses a grid-based system to efficiently query danger levels at any position, allowing units to avoid unsafe areas.

Tracks and updates:

  • Ground threat values (damage potential vs ground units)
  • Air threat values (damage potential vs air units)
  • Detection coverage (invisible unit detection zones)

Definition at line 18 of file ThreatGrid.h.

Member Function Documentation

◆ addOrUpdateEnemy()

void ThreatGrid::addOrUpdateEnemy ( int id,
BWAPI::UnitType type,
BWAPI::Position pos,
bool completed,
bool burrowed,
bool immobile )

Adds or updates an enemy unit's contribution to the threat grid.

If the unit state has changed, its previous contribution is removed and recalculated.

Parameters
idUnique unit ID
typeUnit type
posUnit position
completedWhether the unit is complete
burrowedWhether the unit is burrowed
immobileWhether the unit is immobile

Definition at line 94 of file ThreatGrid.cpp.

95{
96 EnemyStamp next;
97 next.type = type;
98 next.pos = pos;
99 next.completed = completed;
100 next.burrowed = burrowed;
101 next.immobile = immobile;
102
103 auto it = stamps_.find(id);
104 if (it == stamps_.end())
105 {
106 stamps_.emplace(id, next);
107 stampEnemy(next, +1);
108 return;
109 }
110
111 EnemyStamp& prev = it->second;
112
113 const bool changed =
114 prev.type != next.type ||
115 prev.pos != next.pos ||
116 prev.completed != next.completed ||
117 prev.burrowed != next.burrowed ||
118 prev.immobile != next.immobile;
119
120 if (!changed)
121 {
122 return;
123 }
124
125 stampEnemy(prev, -1);
126 prev = next;
127 stampEnemy(prev, +1);
128}

◆ airThreatAt()

int ThreatGrid::airThreatAt ( BWAPI::Position p) const

Returns the air threat value at a given position.

Parameters
pWorld position
Returns
Accumulated air threat value

Definition at line 62 of file ThreatGrid.cpp.

63{
64 return airThreat_.get(toWalkX(p), toWalkY(p));
65}

◆ airThreatRangePx()

int ThreatGrid::airThreatRangePx ( BWAPI::UnitType t) const

Definition at line 242 of file ThreatGrid.cpp.

243{
244 BWAPI::UnitType weaponType = t;
245
246 if (t == BWAPI::UnitTypes::Terran_Bunker)
247 {
248 weaponType = BWAPI::UnitTypes::Terran_Marine;
249 }
250
251 BWAPI::WeaponType w = weaponType.airWeapon();
252 if (w == BWAPI::WeaponTypes::None)
253 {
254 return 0;
255 }
256
257 int range = w.maxRange();
258 if (t == BWAPI::UnitTypes::Terran_Bunker)
259 {
260 range += 48;
261 }
262
263 return range + rangeBufferPx_;
264}

◆ airThreatValue()

int ThreatGrid::airThreatValue ( BWAPI::UnitType t) const

Definition at line 266 of file ThreatGrid.cpp.

267{
268 BWAPI::UnitType weaponType = t;
269
270 if (t == BWAPI::UnitTypes::Terran_Bunker)
271 {
272 weaponType = BWAPI::UnitTypes::Terran_Marine;
273 }
274
275 BWAPI::WeaponType w = weaponType.airWeapon();
276 if (w == BWAPI::WeaponTypes::None)
277 {
278 return 0;
279 }
280
281 int dmg = w.damageAmount() * w.damageFactor();
282 int hits = weaponType.maxAirHits();
283
284 int mult = 1;
285 if (t == BWAPI::UnitTypes::Terran_Bunker)
286 {
287 mult = 4;
288 }
289
290 return dmg * hits * mult;
291}

◆ detectionAt()

int ThreatGrid::detectionAt ( BWAPI::Position p) const

Returns the detection threat value at a given position.

Parameters
pWorld position
Returns
Detection intensity value

Definition at line 51 of file ThreatGrid.cpp.

52{
53 return detection_.get(toWalkX(p), toWalkY(p));
54}

◆ detectorRadiusPx()

int ThreatGrid::detectorRadiusPx ( BWAPI::UnitType t) const
private

Definition at line 178 of file ThreatGrid.cpp.

179{
180 if (t == BWAPI::UnitTypes::Terran_Missile_Turret) return 224;
181 if (t == BWAPI::UnitTypes::Protoss_Photon_Cannon) return 224;
182 if (t == BWAPI::UnitTypes::Zerg_Spore_Colony) return 224;
183 if (t == BWAPI::UnitTypes::Terran_Science_Vessel) return 224;
184 if (t == BWAPI::UnitTypes::Protoss_Observer) return 224;
185
186 int r = t.sightRange();
187 if (r <= 0) r = 224;
188 return r;
189}

◆ getAirThreat()

int ThreatGrid::getAirThreat ( BWAPI::Position p) const

Definition at line 67 of file ThreatGrid.cpp.

68{
69 return airThreatAt(p);
70}
int airThreatAt(BWAPI::Position p) const
Returns the air threat value at a given position.

◆ getDetection()

int ThreatGrid::getDetection ( BWAPI::Position p) const

Wrapper for retrieving detection threat at a position.

Definition at line 76 of file ThreatGrid.cpp.

77{
78 return detectionAt(p);
79}
int detectionAt(BWAPI::Position p) const
Returns the detection threat value at a given position.

◆ groundThreatAt()

int ThreatGrid::groundThreatAt ( BWAPI::Position p) const

Returns the ground threat value at a given position.

Parameters
pWorld position
Returns
Accumulated ground threat value

Definition at line 40 of file ThreatGrid.cpp.

41{
42 return groundThreat_.get(toWalkX(p), toWalkY(p));
43}

◆ groundThreatRangePx()

int ThreatGrid::groundThreatRangePx ( BWAPI::UnitType t) const
private

Definition at line 191 of file ThreatGrid.cpp.

192{
193 BWAPI::UnitType weaponType = t;
194
195 if (t == BWAPI::UnitTypes::Terran_Bunker)
196 {
197 weaponType = BWAPI::UnitTypes::Terran_Marine;
198 }
199
200 BWAPI::WeaponType w = weaponType.groundWeapon();
201 if (w == BWAPI::WeaponTypes::None)
202 {
203 return 0;
204 }
205
206 int range = w.maxRange();
207 if (t == BWAPI::UnitTypes::Terran_Bunker)
208 {
209 range += 48;
210 }
211
212 return range + rangeBufferPx_;
213}

◆ groundThreatValue()

int ThreatGrid::groundThreatValue ( BWAPI::UnitType t) const
private

Definition at line 215 of file ThreatGrid.cpp.

216{
217 BWAPI::UnitType weaponType = t;
218
219 if (t == BWAPI::UnitTypes::Terran_Bunker)
220 {
221 weaponType = BWAPI::UnitTypes::Terran_Marine;
222 }
223
224 BWAPI::WeaponType w = weaponType.groundWeapon();
225 if (w == BWAPI::WeaponTypes::None)
226 {
227 return 0;
228 }
229
230 int dmg = w.damageAmount() * w.damageFactor();
231 int hits = weaponType.maxGroundHits();
232
233 int mult = 1;
234 if (t == BWAPI::UnitTypes::Terran_Bunker)
235 {
236 mult = 4;
237 }
238
239 return dmg * hits * mult;
240}

◆ onFrameStart()

void ThreatGrid::onFrameStart ( int frame)

Updates the current frame reference for the grid.

Parameters
frameCurrent game frame

Definition at line 29 of file ThreatGrid.cpp.

30{
31 currentFrame_ = frame;
32}

◆ onStart()

void ThreatGrid::onStart ( )

Initializes threat grids based on map dimensions.

Creates walk-tile resolution grids for:

  • Ground threat
  • Air threat
  • Detection coverage

Definition at line 13 of file ThreatGrid.cpp.

14{
15 const int walkW = BWAPI::Broodwar->mapWidth() * 4;
16 const int walkH = BWAPI::Broodwar->mapHeight() * 4;
17
18 groundThreat_.init(walkW, walkH);
19 detection_.init(walkW, walkH);
20
21 stamps_.clear();
22}

◆ paintDisc()

void ThreatGrid::paintDisc ( GridData & g,
BWAPI::Position center,
int rangePx,
int delta )
private

Definition at line 149 of file ThreatGrid.cpp.

150{
151 const int cx = toWalkX(center);
152 const int cy = toWalkY(center);
153
154 const int rWalk = (rangePx >> 3) + 1;
155 const int r2 = rangePx * rangePx;
156
157 for (int dx = -rWalk; dx <= rWalk; ++dx)
158 {
159 for (int dy = -rWalk; dy <= rWalk; ++dy)
160 {
161 const int wx = cx + dx;
162 const int wy = cy + dy;
163
164 const int px = (wx << 3) + 4;
165 const int py = (wy << 3) + 4;
166
167 const int ddx = px - center.x;
168 const int ddy = py - center.y;
169
170 if (ddx * ddx + ddy * ddy <= r2)
171 {
172 g.add(wx, wy, delta);
173 }
174 }
175 }
176}

◆ removeEnemy()

void ThreatGrid::removeEnemy ( int id)

Removes an enemy unit from the threat grid.

Clears its previously stamped threat values.

Parameters
idUnit ID

Definition at line 137 of file ThreatGrid.cpp.

138{
139 auto it = stamps_.find(id);
140 if (it == stamps_.end())
141 {
142 return;
143 }
144
145 stampEnemy(it->second, -1);
146 stamps_.erase(it);
147}

◆ stampEnemy()

void ThreatGrid::stampEnemy ( const EnemyStamp & s,
int delta )
private

Definition at line 294 of file ThreatGrid.cpp.

295{
296 if (!s.completed)
297 {
298 return;
299 }
300
301 const BWAPI::UnitType t = s.type;
302
303 if (t.isDetector())
304 {
305 const int r = detectorRadiusPx(t) + rangeBufferPx_;
306 paintDisc(detection_, s.pos, r, delta);
307 }
308
309 if (s.immobile)
310 {
311 return;
312 }
313
314 const bool isLurker = (t == BWAPI::UnitTypes::Zerg_Lurker);
315
316 if (isLurker && !s.burrowed)
317 {
318 return;
319 }
320
321 if (!isLurker && s.burrowed)
322 {
323 return;
324 }
325
326 const int range = groundThreatRangePx(t);
327 const int value = groundThreatValue(t);
328
329 if (range > 0 && value > 0)
330 {
331 paintDisc(groundThreat_, s.pos, range, value * delta);
332 }
333
334 const int airRange = airThreatRangePx(t);
335 const int airValue = airThreatValue(t);
336
337 if (airRange > 0 && airValue > 0)
338 {
339 paintDisc(airThreat_, s.pos, airRange, airValue * delta);
340 }
341}

◆ toWalkX()

int ThreatGrid::toWalkX ( BWAPI::Position p)
inlinestaticprivate

Definition at line 92 of file ThreatGrid.h.

93 {
94 return p.x >> 3;
95 }

◆ toWalkY()

int ThreatGrid::toWalkY ( BWAPI::Position p)
inlinestaticprivate

Definition at line 97 of file ThreatGrid.h.

98 {
99 return p.y >> 3;
100 }

Member Data Documentation

◆ airThreat_

GridData ThreatGrid::airThreat_
private

Definition at line 86 of file ThreatGrid.h.

◆ currentFrame_

int ThreatGrid::currentFrame_ = 0
private

Definition at line 82 of file ThreatGrid.h.

◆ detection_

GridData ThreatGrid::detection_
private

Definition at line 87 of file ThreatGrid.h.

◆ groundThreat_

GridData ThreatGrid::groundThreat_
private

Definition at line 85 of file ThreatGrid.h.

◆ rangeBufferPx_

int ThreatGrid::rangeBufferPx_ = 16
private

Definition at line 83 of file ThreatGrid.h.

◆ stamps_

std::unordered_map<int, EnemyStamp> ThreatGrid::stamps_
private

Definition at line 90 of file ThreatGrid.h.


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