TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
tilesinfo.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 <unordered_map>
13 #include <vector>
14 
15 namespace cherrypi {
16 
17 class State;
18 struct Unit;
19 struct BuildType;
20 
21 /**
22  * Represents a tile on the map.
23  *
24  * buildable and height are static map data, the rest are updated throughout
25  * the game. All fields might not be updated immediately every frame, but most
26  * things should only have a few frames of delay in the worst case.
27  */
28 
29 struct Tile {
30  public:
31  /// X position of tile in walk tiles
32  int x = 0;
33  /// Y position of tile in walk tiles
34  int y = 0;
35  bool visible = false;
36  bool buildable = false;
37  /// Set by builderhelpers to help with planning building placement.
38  bool reservedAsUnbuildable = false;
39  bool hasCreep = false;
40 
41  /// For lazily-updated info (at time of writing, just hasCreepIn):
42  /// When this tile was last updated
44  /// When this tile is expected to have creep.
45  /// first: Last frame this was updated
46  /// second: Frame creep is expected
49  /// If this tile is expected to contain creep within a certain number of
50  /// frames
51  FrameNum expectsCreepBy() const;
52 
53  /// Indicates that this tile is in the mineral line and that buildings
54  /// should not be placed here.
55  bool reservedForGathering = false;
56  /// Indicates that this tile is unbuildable for resource depots (too close
57  /// to resources).
59  /// Indicates that this is a resource depot tile at an expansion, and should
60  /// not be occupied by regular buildings.
62  /// Special field that can be set if a particular tile should not be used
63  /// for buildings until this frame. Usually set by BuilderModule when trying
64  /// and, for an unknown reason, failing to build something, such that we can
65  /// try to build somewhere else.
67  /// The building that is currently occupying this tile. There might be other
68  /// (stacked) buildings too.
69  Unit* building = nullptr;
70  /// Every walk tile within this tile is walkable. This usually means that
71  /// any unit can pass through this tile, ignoring buildings or other units.
72  bool entirelyWalkable = false;
73  int height = 0;
76 };
77 
78 /**
79  * Manages and updates per-tile data.
80  */
81 class TilesInfo {
82  public:
83  TilesInfo(State* state_);
84  TilesInfo(TilesInfo const&) = delete;
85  TilesInfo& operator=(TilesInfo const&) = delete;
86 
87  void preUnitsUpdate();
88  void postUnitsUpdate();
89 
90  unsigned mapTileWidth() const {
91  return mapTileWidth_;
92  }
93 
94  unsigned mapTileHeight() const {
95  return mapTileHeight_;
96  }
97 
98  static const unsigned tilesWidth = 256;
99  static const unsigned tilesHeight = 256;
100 
101  Tile& getTile(int walkX, int walkY);
102  const Tile& getTile(int walkX, int walkY) const;
103  Tile* tryGetTile(int walkX, int walkY);
104  const Tile* tryGetTile(int walkX, int walkY) const;
105 
106  /// This sets reservedAsUnbuildable in the tiles that would be occupied
107  /// by the specified building at the specified build location (upper left
108  /// corner coordinates). Should only be used by BuilderModule.
109  void reserveArea(const BuildType* type, int walkX, int walkY);
110  // Complements reserveArea.
111  void unreserveArea(const BuildType* type, int walkX, int walkY);
112 
113  /// All the tiles. Prefer to use getTile, this is only here in case it is
114  /// needed for performance.
115  std::vector<Tile> tiles;
116 
117  protected:
119  Unit* u;
120  const BuildType* type;
121  int pixelX;
122  int pixelY;
123  std::vector<Tile*> tiles;
124  };
125 
126  unsigned mapTileWidth_ = 0;
127  unsigned mapTileHeight_ = 0;
128 
129  std::unordered_map<const Unit*, TileOccupyingBuilding>
131 
132  State* state_ = nullptr;
133  FrameNum lastSlowTileUpdate_ = 0;
134  FrameNum lastUpdateBuildings_ = 0;
135  FrameNum lastFowCreepUpdate_ = 0;
136 };
137 
138 } // namespace cherrypi
Game state.
Definition: state.h:42
int FrameNum
Definition: basetypes.h:22
unsigned mapTileWidth() const
Definition: tilesinfo.h:90
int pixelX
Definition: tilesinfo.h:121
FrameNum expectsCreepUpdated_
When this tile is expected to have creep.
Definition: tilesinfo.h:47
Unit * building
The building that is currently occupying this tile.
Definition: tilesinfo.h:69
Represents and holds information about buildable types (units, upgrades, techs).
Definition: buildtype.h:36
bool buildable
Definition: tilesinfo.h:36
FrameNum lastSlowUpdate
Definition: tilesinfo.h:75
int pixelY
Definition: tilesinfo.h:122
bool hasCreep
Definition: tilesinfo.h:39
bool entirelyWalkable
Every walk tile within this tile is walkable.
Definition: tilesinfo.h:72
replayer::Unit Unit
Definition: state.h:36
int height
Definition: tilesinfo.h:73
int y
Y position of tile in walk tiles.
Definition: tilesinfo.h:34
const BuildType * type
Definition: tilesinfo.h:120
Represents a unit in the game.
Definition: unitsinfo.h:35
bool resourceDepotUnbuildable
Indicates that this tile is unbuildable for resource depots (too close to resources).
Definition: tilesinfo.h:58
bool reservedForResourceDepot
Indicates that this is a resource depot tile at an expansion, and should not be occupied by regular b...
Definition: tilesinfo.h:61
FrameNum expectsCreepFrame_
Definition: tilesinfo.h:48
Manages and updates per-tile data.
Definition: tilesinfo.h:81
int x
X position of tile in walk tiles.
Definition: tilesinfo.h:32
Represents a tile on the map.
Definition: tilesinfo.h:29
int constexpr kForever
Definition: basetypes.h:37
std::vector< Tile * > tiles
Definition: tilesinfo.h:123
std::vector< Tile > tiles
All the tiles.
Definition: tilesinfo.h:115
FrameNum lastSeen
Definition: tilesinfo.h:74
bool reservedAsUnbuildable
Set by builderhelpers to help with planning building placement.
Definition: tilesinfo.h:38
Unit * u
Definition: tilesinfo.h:119
bool visible
Definition: tilesinfo.h:35
Main namespace for bot-related code.
Definition: areainfo.cpp:17
FrameNum expectsCreepBy() const
If this tile is expected to contain creep within a certain number of frames.
Definition: tilesinfo.cpp:442
unsigned mapTileHeight() const
Definition: tilesinfo.h:94
FrameNum blockedUntil
Special field that can be set if a particular tile should not be used for buildings until this frame...
Definition: tilesinfo.h:66
int lazyUpdateFrame
For lazily-updated info (at time of writing, just hasCreepIn): When this tile was last updated...
Definition: tilesinfo.h:43
std::unordered_map< const Unit *, TileOccupyingBuilding > tileOccupyingBuildings_
Definition: tilesinfo.h:130
bool reservedForGathering
Indicates that this tile is in the mineral line and that buildings should not be placed here...
Definition: tilesinfo.h:55