TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
builderhelper.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 "buildtype.h"
11 #include "cherrypi.h"
12 
13 #include <functional>
14 #include <list>
15 #include <vector>
16 
17 namespace cherrypi {
18 
19 class State;
20 class TilesInfo;
21 struct Tile;
22 struct UPCTuple;
23 struct Unit;
24 
25 namespace builderhelpers {
26 
27 /// Refine a building UPC by selecting a dirac location according.
28 /// This will use a combination of the individual rules defined in this
29 /// namespace.
30 /// @param upc Source UPC
31 /// @param type A concrete building type selected from the source UPC
32 std::shared_ptr<UPCTuple> upcWithPositionForBuilding(
33  State* state,
34  UPCTuple const& upc,
35  BuildType const* type);
36 
37 /// Find location to construct the building
38 /// @param state Bot's state
39 /// @param seeds Seed locations for building location search
40 /// @param type Type of the building
41 /// @param upc UPCTuple with possible restrictions regarding position
42 /// @return Location to construct the building; (-1, -1) if no suitable
43 /// location was found
45  State* state,
46  std::vector<Position> const& seeds,
47  BuildType const* type,
48  UPCTuple const& upc);
49 
51  State* state,
52  std::vector<Position> const& seeds,
53  BuildType const* type,
54  UPCTuple const& upc,
55  std::function<double(State*, const BuildType*, const Tile*)> scoreFunc);
56 
57 /// Check whether the building can be constructed at specified location
58 /// @param state Bot's state
59 /// @param type Type of the building
60 /// @param pos Location to be checked
61 bool canBuildAt(
62  State* state,
63  const BuildType* type,
64  const Position& pos,
65  bool ignoreReserved = false,
66  bool logFailure = false);
67 
68 /// Check whether there is enough creep for the building at the specified
69 /// position.
70 /// If the building does not require creep, checks that there isn't any.
71 /// Does not anticipate creep.
72 /// @param state Bot's state
73 /// @param type Type of the building
74 /// @param pos Location to be checked
75 bool checkCreepAt(State* state, const BuildType* type, const Position& pos);
76 
77 /// Find a free Vespene Geyser for a refinery
78 /// @param state Bot's state
79 /// @param type Building type of the refinery
80 /// @param upc UPCTuple with possible restrictions regarding position
81 /// @return Unit that corresponds to the Vespene Geyser on which to
82 /// build the refinery; nullptr if no suitable Vespene Geyser could be
83 /// found
85  State* state,
86  BuildType const* type,
87  UPCTuple const& upc);
88 
89 /// Find Vespene Geyser location for a refinery
90 /// @param state Bot's state
91 /// @param type Building type of the refinery
92 /// @param upc UPCTuple with possible restrictions regarding position
93 /// @return Location that corresponds to the Vespene Geyser on which to
94 /// build the refinery; (-1, -1) if no suitable location could be
95 /// found
97 findRefineryLocation(State* state, BuildType const* type, UPCTuple const& upc);
98 
99 /// Find location for a new resource depot
100 /// @param state Bot's state
101 /// @param type Type of the building
102 /// @param candidateLocations Candidate locations for resource depots,
103 /// sorted in the order of location preference - the most preferred
104 /// comes first
105 /// @return Proposed resource depot location (in walktiles) or (-1, -1),
106 /// if no suitable location was found
108  State* state,
109  const BuildType* type,
110  const std::vector<Position>& candidateLocations,
111  bool isExpansion = true);
112 
113 /// Use map information to produce candidate resource depot locations
114 /// sorted by their proximity to the main base
115 std::vector<Position> candidateExpansionResourceDepotLocations(State* state);
116 
117 /// Use map information to produce candidate resource depot locations
118 /// sorted by their proximity to the main base
119 std::list<std::pair<Position, int>>
121 
122 /// Produce seed locations for the building
123 /// @param state Bot's state
124 /// @param type Building type
125 /// @param upc UPCTuple with possible restrictions regarding position
126 /// @param builder Unit selected to construct the building,
127 /// nullptr if not yet selected
128 /// @return Proposed seed locations (in walktiles)
129 std::vector<Position> buildLocationSeeds(
130  State* state,
131  BuildType const* type,
132  UPCTuple const& upc,
133  Unit* builder = nullptr);
134 
135 /// Produces string representation of masks (buildable, building,
136 /// reservedAsUnbuildable, resourceDepotUnbuildable, reservedForResourceDepot)
137 /// around the provided build location. Used for verbose logs mostly for
138 /// debugging purposes.
139 /// @param state Bot's state
140 /// @param type Building type
141 /// @param pos Coordinates of an upper-left corner of the proposed location
142 /// (in walktiles)
143 /// @return String containing masks, where '1' (or '+' outside the building
144 /// area) stands for true, '0' (or '-') for false, 'X' for cells over
145 /// the map edge
146 std::string
147 buildLocationMasks(State* state, const BuildType* type, const Position& pos);
148 
149 /// Sets Tile::reservedAsUnbuildable to reserve the tiles occupied by a given
150 /// building type when placed at pos.
151 void fullReserve(TilesInfo& tt, BuildType const* type, Position const& pos);
152 
153 /// Clears Tile::reservedAsUnbuildable to free the tiles occupied by a given
154 /// building type when placed at pos.
155 void fullUnreserve(TilesInfo& tt, BuildType const* type, Position const& pos);
156 
157 } // namespace builderhelpers
158 } // namespace cherrypi
std::vector< Position > buildLocationSeeds(State *state, BuildType const *type, UPCTuple const &upc, Unit *builder)
Produce seed locations for the building.
Definition: builderhelper.cpp:630
bool canBuildAt(State *state, const BuildType *type, const Position &pos, bool ignoreReserved, bool logFailure)
Check whether the building can be constructed at specified location.
Definition: builderhelper.cpp:256
replayer::Unit Unit
Definition: state.h:36
std::vector< Position > candidateExpansionResourceDepotLocations(State *state)
Use map information to produce candidate resource depot locations sorted by their proximity to the ma...
Definition: builderhelper.cpp:583
Position findRefineryLocation(State *state, BuildType const *type, UPCTuple const &upc)
Find Vespene Geyser location for a refinery.
Definition: builderhelper.cpp:484
Position findBuildLocation(State *state, std::vector< Position > const &seeds, BuildType const *type, UPCTuple const &upc, ScoreFunc &&scoreFunc)
Definition: builderhelper.cpp:149
bool checkCreepAt(State *state, const BuildType *type, const Position &pos)
Check whether there is enough creep for the building at the specified position.
Definition: builderhelper.cpp:410
void fullUnreserve(TilesInfo &tt, BuildType const *type, Position const &pos)
Clears Tile::reservedAsUnbuildable to free the tiles occupied by a given building type when placed at...
Definition: builderhelper.cpp:768
std::string buildLocationMasks(State *state, const BuildType *type, const Position &pos)
Produces string representation of masks (buildable, building, reservedAsUnbuildable, resourceDepotUnbuildable, reservedForResourceDepot) around the provided build location.
Definition: builderhelper.cpp:671
void fullReserve(TilesInfo &tt, BuildType const *type, Position const &pos)
Sets Tile::reservedAsUnbuildable to reserve the tiles occupied by a given building type when placed a...
Definition: builderhelper.cpp:764
std::shared_ptr< UPCTuple > upcWithPositionForBuilding(State *state, UPCTuple const &upc, BuildType const *type)
Refine a building UPC by selecting a dirac location according.
Definition: builderhelper.cpp:88
Main namespace for bot-related code.
Definition: areainfo.cpp:17
std::list< std::pair< Position, int > > candidateExpansionResourceDepotLocationsDistances(State *state)
Use map information to produce candidate resource depot locations sorted by their proximity to the ma...
Definition: builderhelper.cpp:597
Position findResourceDepotLocation(State *state, const BuildType *type, const std::vector< Position > &candidateLocations, bool isExpansion)
Find location for a new resource depot.
Definition: builderhelper.cpp:559
Unit * findGeyserForRefinery(State *state, BuildType const *type, UPCTuple const &upc)
Find a free Vespene Geyser for a refinery.
Definition: builderhelper.cpp:442
Vec2T< int > Position
Definition: basetypes.h:178