11 #include "common/circularbuffer.h" 14 #include "unitsinfo.h" 16 #include "upcfilter.h" 23 #include <gflags/gflags.h> 24 #include <glog/logging.h> 25 #include <mapbox/variant.hpp> 26 #include <torchcraft/client.h> 32 class SharedController;
33 class CherryVisDumperModule;
38 std::shared_ptr<UPCTuple>
upc =
nullptr;
44 : upc(upc), source(source), origin(origin) {}
50 std::shared_ptr<Task> task =
nullptr;
52 bool autoRemove =
true;
57 std::shared_ptr<Task> task,
61 : task(
std::move(task)),
63 autoRemove(autoRemove),
64 creationFrame(creationFrame) {}
91 using Data = mapbox::util::variant<
98 std::shared_ptr<SharedController>,
99 std::unordered_map<int, int>>;
100 using UPCMap = std::map<UpcId, std::shared_ptr<UPCTuple>>;
103 std::tuple<UpcId, std::string, std::chrono::milliseconds>;
127 void clearCommands();
132 void post(std::string
const& key,
Data const& data) {
136 return map_.find(key) != map_.end();
138 Data const&
get(std::string
const& key)
const {
141 template <
typename T>
142 T
const&
get(std::string
const& key)
const {
143 return map_.at(key).get<T>();
145 template <
typename T>
146 T
const&
get(std::string
const& key, T
const& defaultValue)
const {
147 auto it = map_.find(key);
148 if (it == map_.end()) {
151 return it->second.get<T>();
153 void remove(std::string
const& key) {
156 template <
typename T>
158 for (
auto it = map_.begin(); it != map_.end(); ++it) {
159 f_do(it->first, it->second);
163 bool isTracked(
UnitId uid)
const;
183 std::shared_ptr<UPCTuple>&&
upc,
186 std::shared_ptr<UpcPostData> data =
nullptr);
187 void consumeUPCs(std::vector<UpcId>
const& ids,
Module* consumer);
189 consumeUPCs({
id}, consumer);
191 void removeUPCs(std::vector<UpcId>
const& ids);
198 return upcsFrom(origin.get());
206 std::shared_ptr<UPCTuple> upcWithId(
UpcId id)
const;
210 void addUPCFilter(std::shared_ptr<UPCFilter> filter);
211 void removeUPCFilter(std::shared_ptr<UPCFilter> filter);
215 postTask(std::shared_ptr<Task> task,
Module* owner,
bool autoRemove =
false);
216 std::shared_ptr<Task> taskForId(
UpcId id)
const;
217 std::vector<std::shared_ptr<Task>> tasksOfModule(
Module* module)
const;
218 std::shared_ptr<Task> taskWithUnit(
Unit* unit)
const;
220 std::shared_ptr<Task> taskWithUnitOfModule(
Unit* unit,
Module* module)
const;
221 void markTaskForRemoval(
UpcId upcId);
223 markTaskForRemoval(task->upcId());
229 void updateUnitAccessCounts(tc::Client::Command
const& command);
232 void postCommand(tc::Client::Command
const& command,
UpcId sourceId);
233 std::vector<tc::Client::Command> commands(
int stepsBack = 0)
const;
235 return commands_.size();
239 void updateTasksByUnit(
Task* task);
244 void checkPostStep();
247 return taskTimeStats_;
250 void setCollectTimers(
bool collect);
253 traceDumper_ = tracer;
262 std::unordered_map<std::string, Data> map_;
264 std::map<UpcId, UPCData> upcs_;
265 std::unique_ptr<UpcStorage> upcStorage_;
268 std::unordered_map<UpcId, std::shared_ptr<UPCTuple>> activeUpcs_;
269 std::list<std::shared_ptr<UPCFilter>> upcFilters_;
270 std::set<UnitId> tracked_;
272 std::list<TaskData> tasks_;
273 std::map<UpcId, std::list<TaskData>::iterator> tasksById_;
274 std::unordered_multimap<Module*, std::list<TaskData>::iterator>
276 std::unordered_map<Unit*, std::list<TaskData>::iterator> tasksByUnit_;
277 std::vector<UpcId> tasksToBeRemoved_;
278 std::map<UnitId, size_t> unitAccessCounts_;
279 std::vector<TaskTimeStats> taskTimeStats_;
280 std::unordered_map<UpcId, TaskStatus> lastTaskStatus_;
281 std::shared_ptr<CherryVisDumperModule> traceDumper_;
283 bool collectTimers_ =
false;
Game state.
Definition: state.h:42
int FrameNum
Definition: basetypes.h:22
void setTraceDumper(std::shared_ptr< CherryVisDumperModule > tracer)
Definition: blackboard.h:252
Stores a graph of UPC communication, including any transactional data.
Definition: upcstorage.h:72
Module * origin
Definition: blackboard.h:40
TaskData(std::shared_ptr< Task > task, Module *owner, bool autoRemove, FrameNum creationFrame)
Definition: blackboard.h:56
Command
Abstract "meta" commands for UPCTuples.
Definition: basetypes.h:314
UpcId source
Definition: blackboard.h:39
UPCMap upcsFrom(std::shared_ptr< Module > origin) const
Returns all non-consumed UPCs from a given module.
Definition: blackboard.h:197
static char const * kMaxScoutExplorersKey
Definition: blackboard.h:113
TaskData()
Definition: blackboard.h:55
std::tuple< UpcId, std::string, std::chrono::milliseconds > TaskTimeStats
Definition: blackboard.h:103
The primary way for modules to publish their activity.
Definition: task.h:50
static char const * kGathererMinGasWorkers
Definition: blackboard.h:117
bool hasKey(std::string const &key)
Definition: blackboard.h:135
UPCData()
Definition: blackboard.h:42
static char const * kEnemyRaceKey
Definition: blackboard.h:106
static char const * kGameUidKey
Definition: blackboard.h:114
Task and associated owner.
Definition: blackboard.h:48
static char const * kBuildOrderSwitchEnabledKey
Definition: blackboard.h:109
int32_t UnitId
Definition: unitsinfo.h:26
mapbox::util::variant< bool, int, float, double, std::string, Position, std::shared_ptr< SharedController >, std::unordered_map< int, int >> Data
A variant of types that are allowed in the Blackboard's key-value storage.
Definition: blackboard.h:99
tc::Client::Command command
Definition: blackboard.h:71
static char const * kMineralsPerFramePerGatherer
Definition: blackboard.h:115
void post(std::string const &key, Data const &data)
Definition: blackboard.h:132
Represents a unit in the game.
Definition: unitsinfo.h:35
static char const * kMinScoutFrameKey
Definition: blackboard.h:111
static char const * kMaxScoutWorkersKey
Definition: blackboard.h:112
void consumeUPC(UpcId id, Module *consumer)
Definition: blackboard.h:188
static char const * kBanditRootKey
Definition: blackboard.h:119
An access-aware blackboard.
Definition: blackboard.h:88
TaskStatus
Describes the current status of a task.
Definition: task.h:23
Game command are posted with an associated UPC ID.
Definition: blackboard.h:70
size_t pastCommandsAvailable() const
Definition: blackboard.h:234
UpcId sourceId
Definition: blackboard.h:72
static char const * kOpeningBuildOrderKey
Definition: blackboard.h:110
std::vector< TaskTimeStats > getTaskTimeStats() const
Definition: blackboard.h:246
static char const * kGasPerFramePerGatherer
Definition: blackboard.h:116
std::map< UpcId, std::shared_ptr< UPCTuple >> UPCMap
Definition: blackboard.h:100
static char const * kBuildOrderKey
Definition: blackboard.h:108
UPCData(std::shared_ptr< UPCTuple > upc, UpcId source, Module *origin)
Definition: blackboard.h:43
Main namespace for bot-related code.
Definition: areainfo.cpp:17
UPCTuple and associated origin.
Definition: blackboard.h:36
void init(int64_t randomSeed)
Definition: cherrypi.cpp:293
int UpcId
Definition: basetypes.h:23
Definition: circularbuffer.h:20
void iterValues(T f_do) const
Definition: blackboard.h:157
static char const * kEnemyNameKey
Definition: blackboard.h:107
std::shared_ptr< UPCTuple > upc
Definition: blackboard.h:38
Interface for bot modules.
Definition: module.h:30
std::shared_ptr< CherryVisDumperModule > getTraceDumper()
Definition: blackboard.h:256
Vec2T< int > Position
Definition: basetypes.h:178
static char const * kGathererMaxGasWorkers
Definition: blackboard.h:118
UpcId constexpr kInvalidUpcId
Definition: basetypes.h:25
void markTaskForRemoval(std::shared_ptr< Task > task)
Definition: blackboard.h:222