TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
areainfo.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include "basetypes.h"
11 
12 #include <tuple>
13 #include <unordered_map>
14 #include <unordered_set>
15 #include <vector>
16 
17 namespace BWEM {
18 class Map;
19 class Area;
20 class ChokePoint;
21 } // namespace BWEM
22 
23 namespace cherrypi {
24 
25 class AreaInfo;
26 class State;
27 struct Unit;
28 struct Tile;
29 
30 /**
31  * Represents an area on the map.
32  *
33  * Areas are regions determined by static map analysis using BWEM
34  * (Brood War Easy Map). Areas correspond to the respective BWEM::Area. This
35  * struct is used to aggregate all game state information (e.g. units,
36  * visibility) local to the respective area.
37  */
38 struct Area {
39  /// ID of BWEM area. This corresponds to the index in the areas vector (+1)
40  int id = -1;
41  /// Center in the area's bounding box in walk tiles.
42  int x = 0;
43  /// Center of the area's bounding box in walk tiles.
44  int y = 0;
45  /// Top left of the area's bounding box in walk tiles.
47  /// Bottom right of the area's bounding box in walk tiles.
49  /// Area size in walk tiles; includes walkable tiles only.
50  int size = 0;
51  /// Possible base locations.
52  std::vector<Position> baseLocations;
53  /// All units in this area that are not dead. This includes gone units.
54  std::vector<Unit*> liveUnits;
55  /// All units in this area that are currently visible.
56  std::vector<Unit*> visibleUnits;
57  /// All Minerals in this area
58  std::vector<Unit*> minerals;
59  /// All Geysers/Extractors/Refineries/Assimilators in this area
60  std::vector<Unit*> geysers;
61  /// All areas that are accessible from/to each other by ground share the same
62  /// groupId.
63  int groupId = -1;
64 
65  BWEM::Area const* area;
66 
67  /// Pointer to container object
68  AreaInfo* areaInfo = nullptr;
69  /// Accessible neighbors
70  std::vector<Area*> neighbors;
71 
72  FrameNum lastExplored = 0;
73 
74  /// True if this area contains our base (our resource depot has been
75  /// constructed in one of the base locations at some point). This
76  /// flag is cleared if the area no longer contains any our buildings
77  bool isMyBase = false;
78  /// True if this area contains enemy base (we've seen enemy's resource depot
79  /// constructed at one of the base locations at some point). This
80  /// flag is cleared if the area no longer contains any enemy buildings, or if
81  /// we guessed that this location should contain enemy base, but haven't seen
82  /// the resource depot yet.
83  bool isEnemyBase = false;
84  /// True if this area may contain enemy's start location. Used only in the
85  /// beginning of the game to determine the enemy's start location.
86  /// Once the enemy start location is found, this flag is true only for the
87  /// corresponding area.
88  bool isPossibleEnemyStartLocation = false;
89  /// True if we ever had a base at one of the area's baseLocations.
90  bool wasMyBase = false;
91  /// True if enemy ever had a base at one of the area's baseLocations.
92  bool wasEnemyBase = false;
93  bool hasMyBuildings = false;
94  bool hasEnemyBuildings = false;
95  /// Strength of all ground units in this area.
96  double myGndStrength = 0;
97  /// Strength of all air units in this area.
98  double myAirStrength = 0;
99  /// Strength of all detector units in this area.
100  double myDetStrength = 0;
101  double enemyGndStrength = 0;
102  double enemyAirStrength = 0;
103  double enemyDetStrength = 0;
104 };
105 
106 /**
107  * Access point for area and base information.
108  *
109  * Beside providing access to area information, this class also provides a few
110  * convenience wrappers for functionality in BWEM::Map.
111  */
112 class AreaInfo {
113  public:
114  struct BaseInfo {
115  /// Area to which the base belongs
117  /// Base index within the area, which corresponds to the build location
118  int baseId;
119  /// Resource depot constructed on the base
121  /// How saturated are this base's resources?
122  /// Currently calculated by GathererController for speed + expediency.
123  /// (No, this isn't a good design)
124  float saturation = 0.0f;
125  };
126 
127  AreaInfo(State* state);
128  AreaInfo(AreaInfo const&) = delete;
129  AreaInfo& operator=(AreaInfo const&) = delete;
130 
131  void update();
132 
133  std::vector<Area> const& areas() const;
134  Area& getArea(int id);
135  Area const& getArea(int id) const;
136  Area& getArea(Position p);
137  Area const& getArea(Position p) const;
138  Area& getArea(Tile const& tile);
139  Area const& getArea(Tile const& tile) const;
140  Area* tryGetArea(int id);
141  Area const* tryGetArea(int id) const;
142  Area* tryGetArea(Position p);
143  Area const* tryGetArea(Position p) const;
144 
145  int numMyBases() const;
146  /// Returns data for our n-th base
147  const BaseInfo* myBase(int n) const;
148  /// Returns data for all our bases
149  std::vector<BaseInfo> const& myBases() const;
150  /// Returns true, if our start location is known
151  bool foundMyStartLocation() const;
152  /// Returns our start location if it is known, otherwise returns
153  /// kInvalidPosition
154  Position myStartLocation() const;
155  /// Returns the closest base according to Euclidean distance
156  int myClosestBaseIdx(Position const& p) const;
157  std::vector<Unit*> myBaseResources(int n) const;
158 
159  int numEnemyBases() const;
160  /// Returns data for enemy's n-th base
161  const BaseInfo* enemyBase(int n) const;
162  /// Returns true, if enemy start location is known
163  bool foundEnemyStartLocation() const;
164  /// Returns enemy start location if it is known, otherwise returns
165  /// kInvalidPosition
166  Position enemyStartLocation() const;
167 
168  std::vector<Position> const& candidateEnemyStartLocations() const;
169 
170  /// Returns a path of choke points to walk from a to b.
171  /// If a or b are not accessible or a is not accessible from b, returns an
172  /// empty path and sets length to infinity.
173  std::vector<Position>
174  walkPath(Position a, Position b, float* length = nullptr) const;
175 
176  /// Returns a path of areas to walk from a to b.
177  /// If a or b are not accessible or a is not accessible from b, returns an
178  /// empty path and sets length to infinity.
179  std::vector<Area const*>
180  walkPathAreas(Position a, Position b, float* length = nullptr) const;
181 
182  /// Returns the distance in walktiles for a walking path a to b.
183  float walkPathLength(Position a, Position b) const;
184 
185  private:
186  void initialize();
187  void findMyStartLocation();
188  void initializePossibleEnemyLocations();
189  /// Returns area base location index, if the unit coresponds to some
190  /// base location (i.e. the distance between the 2 is within certain
191  /// threshold). Returns -1, if no corresponding base location was found
192  int findBaseLocationIndexInArea(Unit* unit, Area& area);
193  void updateUnits();
194  void updateEnemyStartLocations();
195  void updateStrengths();
196  void updateNeighbors();
197  void updateBases();
198  void populateCache();
199  Area* getCachedArea(Position p);
200 
201  bool isMyBaseAlive(const BaseInfo& baseInfo) const;
202  bool isEnemyBaseAlive(const BaseInfo& baseInfo) const;
203  void walkPathHelper(
204  BWEM::Map* map,
205  Position a,
206  Position b,
207  std::vector<Area const*>* areas,
208  std::vector<Position>* chokePoints,
209  float* length) const;
210 
211  State* state_ = nullptr;
212  BWEM::Map* map_ = nullptr;
213  std::vector<Area> areas_;
214  std::vector<Position> candidateEnemyStartLoc_;
215  Position myStartLoc_ = kInvalidPosition;
216  std::vector<BaseInfo> myBases_;
217  std::vector<BaseInfo> enemyBases_;
218  std::unordered_map<Unit*, BaseInfo*> myDepot2Base_;
219  std::unordered_map<Unit*, BaseInfo*> enemyDepot2Base_;
220  std::unordered_set<Unit*> macroDepots_;
221  std::unordered_map<int, int> neighborAreaCache_;
222 };
223 
224 } // namespace cherrypi
BWEM::Area const * area
Definition: areainfo.h:65
Game state.
Definition: state.h:42
int FrameNum
Definition: basetypes.h:22
std::vector< Unit * > geysers
All Geysers/Extractors/Refineries/Assimilators in this area.
Definition: areainfo.h:60
Position topLeft
Top left of the area&#39;s bounding box in walk tiles.
Definition: areainfo.h:46
std::vector< Position > baseLocations
Possible base locations.
Definition: areainfo.h:52
Area * area
Area to which the base belongs.
Definition: areainfo.h:116
std::vector< Unit * > liveUnits
All units in this area that are not dead. This includes gone units.
Definition: areainfo.h:54
std::vector< Area * > neighbors
Accessible neighbors.
Definition: areainfo.h:70
Unit * resourceDepot
Resource depot constructed on the base.
Definition: areainfo.h:120
void initialize()
Definition: buildtype.cpp:500
Definition: areainfo.h:114
Represents an area on the map.
Definition: areainfo.h:38
Represents a unit in the game.
Definition: unitsinfo.h:35
int baseId
Base index within the area, which corresponds to the build location.
Definition: areainfo.h:118
std::vector< Unit * > minerals
All Minerals in this area.
Definition: areainfo.h:58
constexpr Position kInvalidPosition
Definition: basetypes.h:179
Position bottomRight
Bottom right of the area&#39;s bounding box in walk tiles.
Definition: areainfo.h:48
Represents a tile on the map.
Definition: tilesinfo.h:29
std::vector< Unit * > visibleUnits
All units in this area that are currently visible.
Definition: areainfo.h:56
Definition: areainfo.h:17
Main namespace for bot-related code.
Definition: areainfo.cpp:17
Access point for area and base information.
Definition: areainfo.h:112