TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
interpretstate.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 "cherrypi.h"
11 #include "state.h"
12 #include "tilesinfo.h"
13 
14 #include <deque>
15 
16 namespace cherrypi {
17 namespace utils {
18 
19 /* Fills in a vector of ints corresponding to all tiles in the map with a
20  * 1 if the tile is within ally base area.
21  */
22 inline void updateInBaseArea(State* state, std::vector<uint8_t>& inBaseArea) {
23  std::fill(inBaseArea.begin(), inBaseArea.end(), 0);
24 
25  auto& tilesInfo = state->tilesInfo();
26  auto* tilesData = tilesInfo.tiles.data();
27 
28  const int mapWidth = state->mapWidth();
29  const int mapHeight = state->mapHeight();
30 
31  struct OpenNode {
32  const Tile* tile;
33  const Tile* sourceTile;
34  float maxDistance;
35  };
36 
37  std::vector<Position> staticDefence;
38  for (Unit* u :
40  staticDefence.emplace_back(u->x, u->y);
41  }
42 
43  const Tile* mainBaseTile = nullptr;
44  std::deque<OpenNode> open;
45  bool isMain = true;
46  for (Unit* u : state->unitsInfo().myResourceDepots()) {
47  auto* tile = tilesInfo.tryGetTile(u->x, u->y);
48  if (tile) {
49  float maxDistance = isMain ? 4 * 24 : 4 * 14;
50  if (isMain) {
51  mainBaseTile = tile;
52  }
53  isMain = false;
54  open.push_back({tile, tile, maxDistance});
55  inBaseArea.at(tile - tilesData) = 1;
56  }
57  }
58  while (!open.empty()) {
59  OpenNode curNode = open.front();
60  open.pop_front();
61 
62  auto add = [&](const Tile* ntile) {
63  if (!curNode.tile->entirelyWalkable) {
64  return;
65  }
66 
67  float sourceDistance = utils::distance(
68  ntile->x, ntile->y, curNode.sourceTile->x, curNode.sourceTile->y);
69  if (sourceDistance >= curNode.maxDistance) {
70  return;
71  }
72 
73  if (!mainBaseTile) {
74  bool staticDefenceIsCloserThanHome = false;
75  bool isInStaticDefenceRange = false;
76  for (Position pos : staticDefence) {
77  float d = utils::distance(ntile->x, ntile->y, pos.x, pos.y);
78  if (d <= 4 * 6) {
79  isInStaticDefenceRange = true;
80  }
81  if (d < sourceDistance) {
82  staticDefenceIsCloserThanHome = true;
83  }
84  }
85  if (staticDefenceIsCloserThanHome && !isInStaticDefenceRange) {
86  return;
87  }
88  }
89 
90  auto& v = inBaseArea[ntile - tilesData];
91  if (v) {
92  return;
93  }
94  v = 1;
95  open.push_back({ntile, curNode.sourceTile, curNode.maxDistance});
96  };
97 
98  const Tile* tile = curNode.tile;
99 
100  if (tile->x > 0) {
101  add(tile - 1);
102  if (tile->y > 0) {
103  add(tile - 1 - TilesInfo::tilesWidth);
104  add(tile - TilesInfo::tilesWidth);
105  }
106  if (tile->y < mapHeight - tc::BW::XYWalktilesPerBuildtile) {
107  add(tile - 1 + TilesInfo::tilesHeight);
108  add(tile + TilesInfo::tilesHeight);
109  }
110  } else {
111  if (tile->y > 0) {
112  add(tile - TilesInfo::tilesWidth);
113  }
114  if (tile->y < mapHeight - tc::BW::XYWalktilesPerBuildtile) {
115  add(tile + TilesInfo::tilesHeight);
116  }
117  }
118  if (tile->x < mapWidth - tc::BW::XYWalktilesPerBuildtile) {
119  add(tile + 1);
120  if (tile->y > 0) {
121  add(tile + 1 - TilesInfo::tilesWidth);
122  }
123  if (tile->y < mapHeight - tc::BW::XYWalktilesPerBuildtile) {
124  add(tile + 1 + TilesInfo::tilesHeight);
125  }
126  }
127  }
128 }
129 
130 } // namespace utils
131 } // namespace cherrypi
Game state.
Definition: state.h:42
void updateInBaseArea(State *state, std::vector< uint8_t > &inBaseArea)
Definition: interpretstate.h:22
int mapHeight() const
Definition: state.h:84
UnitsInfo & unitsInfo()
Definition: state.h:116
int y
Y position of tile in walk tiles.
Definition: tilesinfo.h:34
const Units & myResourceDepots()
Definition: unitsinfo.h:345
const Units & myUnitsOfType(const BuildType *type)
Our units of a particular type (does not include dead units).
Definition: unitsinfo.cpp:234
Represents a unit in the game.
Definition: unitsinfo.h:35
const BuildType * Zerg_Sunken_Colony
Definition: buildtype.cpp:358
float distance(int x1, int y1, int x2, int y2)
Walktile distance.
Definition: gamemechanics.h:49
static const unsigned tilesWidth
Definition: tilesinfo.h:98
int mapWidth() const
Definition: state.h:81
static const unsigned tilesHeight
Definition: tilesinfo.h:99
TilesInfo & tilesInfo()
Definition: state.h:119
int x
X position of tile in walk tiles.
Definition: tilesinfo.h:32
Represents a tile on the map.
Definition: tilesinfo.h:29
std::vector< Tile > tiles
All the tiles.
Definition: tilesinfo.h:115
Main namespace for bot-related code.
Definition: areainfo.cpp:17