9 #include "blackboard.h" 10 #include "buildtype.h" 14 #include "unitsinfo.h" 16 #include <fmt/format.h> 17 #include <nlohmann/json.hpp> 23 class CherryVisLogSink;
25 #define CVIS_LOG(state) \ 26 if (auto _cvisDumper = state->board()->getTraceDumper()) \ 27 _cvisDumper->getGlobalLogger().getStream(state, __FILE__, __LINE__) 29 #define CVIS_LOG_UNIT(state, unit) \ 30 if (auto _cvisDumper = state->board()->getTraceDumper()) \ 31 _cvisDumper->getUnitLogger(unit).getStream(state, __FILE__, __LINE__) 39 using Dumpable = std::variant<Unit*, Position, int, float, std::string>;
43 template <
typename T,
typename _U>
45 static constexpr
const char*
kName =
nullptr;
47 #define ACCEPT_TYPE(TYPE) \ 48 template <typename _U> \ 49 struct TypeInfo<TYPE, _U> { \ 50 static constexpr const char* kName = #TYPE; \ 63 : state_(state), logger_(logger), filename_(filename), line_(line) {}
66 state_, message_.str(), std::move(attachments_), filename_, line_);
75 template <
typename K,
typename V>
79 attachments_.push_back({
80 {
"map", std::move(m)},
103 std::vector<nlohmann::json> attachments = {},
104 const char* full_filename =
"",
106 google::LogSeverity severity = 0);
136 template <
typename T,
typename V,
typename W>
139 std::string
const&
name,
145 std::unordered_map<std::string, ag::Variant>
const& tensors);
154 std::unordered_map<std::string, ag::Variant>
const& tensors,
155 std::array<int, 2>
const& topLeftPixel,
156 std::array<float, 2>
const& scalingToPixels);
170 node[
"module"] =
name;
175 void setId(int32_t
id, std::string prefix =
"") {
177 node[
"type_prefix"] = prefix;
180 prob_distr.push_back({{
"type_prefix",
"i"},
181 {
"id", unit ? unit->
id : -1},
185 if (!str().empty()) {
186 node[
"description"] = str();
188 if (!prob_distr.empty()) {
189 node[
"distribution"] = prob_distr;
195 std::unordered_map<std::string, nlohmann::json>
node;
201 int32_t lastSeenTask = -1;
202 int32_t lastSeenType = -1;
211 std::unordered_map<std::shared_ptr<Task>, int32_t>
taskToId_;
221 std::unordered_map<std::string , std::vector<nlohmann::json>>
229 std::unordered_map<std::string , std::vector<nlohmann::json>>
239 std::unordered_map<std::string , nlohmann::json>>
241 std::unordered_map<std::string , std::string >
244 std::unordered_map<std::string , std::vector<nlohmann::json>>
254 void writeTrees(std::string
const& dumpDirectory);
256 nlohmann::json
getTensorSummary(std::string
const& name, at::Tensor
const& t);
267 : module_(module), state_(state), threadId_(
std::this_thread::get_id()) {
268 google::AddLogSink(
this);
271 google::RemoveLogSink(
this);
275 google::LogSeverity severity,
276 const char* full_filename,
277 const char* base_filename,
279 const struct ::tm* tm_time,
281 size_t message_len)
override {
283 if (threadId_ == std::this_thread::get_id()) {
284 module_->getGlobalLogger().addMessage(
286 std::string(message, message_len),
300 template <
typename T,
typename V,
typename W>
303 std::string
const&
name,
307 std::string filename =
"tree__" + std::to_string(
trace_.
trees_.size()) +
308 "__f" + std::to_string(s->
currentFrame()) +
".json.zstd";
310 g.graph = std::make_shared<TreeNode>();
311 uint32_t nodesCount = 0;
312 std::vector<std::pair<W , std::shared_ptr<TreeNode>>> todo;
315 dump_node(root, g.graph);
317 g.allNodes.push_back(g.graph);
318 std::vector<W> childs = get_children(root);
319 for (
auto c : childs) {
320 todo.push_back(std::make_pair(c, g.graph));
324 while (!todo.empty()) {
325 auto doing = todo.back();
329 doing.second->children.emplace_back(std::make_shared<TreeNode>());
330 dump_node(doing.first, doing.second->children.back());
332 g.allNodes.push_back(doing.second->children.back());
333 std::vector<W> childs = get_children(doing.first);
334 for (
auto c : childs) {
335 todo.push_back(std::make_pair(c, doing.second->children.back()));
341 {
"nodes", nodesCount},
342 {
"filename", filename},
349 nlohmann::json& json,
351 void to_json(nlohmann::json& json,
Unit const* unit);
Game state.
Definition: state.h:42
std::unordered_map< std::string, nlohmann::json > gameValues_
Definition: cherryvisdumper.h:248
std::vector< nlohmann::json > prob_distr
Definition: cherryvisdumper.h:196
int FrameNum
Definition: basetypes.h:22
LoggerStream getStream(State *state, const char *filename, int line)
Definition: cherryvisdumper.h:96
static constexpr const char * kName
Definition: cherryvisdumper.h:45
Definition: cherryvisdumper.h:200
std::unordered_map< std::string, std::unordered_map< std::string, nlohmann::json > > tensors_
Definition: cherryvisdumper.h:240
std::shared_ptr< TreeNode > graph
Definition: cherryvisdumper.h:206
Definition: cherryvisdumper.h:59
std::unordered_map< std::string, std::vector< nlohmann::json > > unitsFirstSeen_
Definition: cherryvisdumper.h:222
virtual void step(State *s) override
Definition: cherryvisdumper.cpp:34
Definition: cherryvisdumper.h:264
Logger & getUnitLogger(Unit *u)
Definition: cherryvisdumper.h:130
void dumpGameUpcs(State *s)
Definition: cherryvisdumper.cpp:297
TraceData trace_
Definition: cherryvisdumper.h:260
FrameNum currentFrame() const
Definition: state.h:57
std::unordered_map< std::string, std::vector< nlohmann::json > > tensorsSummary_
Definition: cherryvisdumper.h:245
int32_t getUnitTaskId(State *s, Unit *unit)
Definition: cherryvisdumper.cpp:212
void addUnitWithProb(Unit *unit, float proba)
Definition: cherryvisdumper.h:179
Definition: cherryvisdumper.h:209
std::unordered_map< std::string, std::string > tensorNameToFile_
Definition: cherryvisdumper.h:242
std::unordered_map< std::string, nlohmann::json > unitsUpdates_
Definition: cherryvisdumper.h:220
void dumpTerrainHeatmaps(State *s, std::unordered_map< std::string, ag::Variant > const &tensors, std::array< int, 2 > const &topLeftPixel, std::array< float, 2 > const &scalingToPixels)
Dumps a heatmap that can be displayed as overlay of the terrain.
Definition: cherryvisdumper.cpp:416
const char * filename_
Definition: cherryvisdumper.h:92
nlohmann::json to_json()
Definition: cherryvisdumper.h:184
CherryVisDumperModule * module_
Definition: cherryvisdumper.h:295
State * state_
Definition: cherryvisdumper.h:296
void addTree(State *s, std::string const &name, T dump_node, V get_children, W root)
Definition: cherryvisdumper.h:301
std::unordered_map< std::string, std::string > boardKnownValues_
Definition: cherryvisdumper.h:215
std::vector< std::shared_ptr< TreeNode > > allNodes
Definition: cherryvisdumper.h:207
std::unordered_map< std::shared_ptr< Task >, int32_t > taskToId_
Definition: cherryvisdumper.h:211
static void writeGameSummary(State *final_state, std::string const &file)
Definition: cherryvisdumper.cpp:232
std::variant< Unit *, Position, int, float, std::string > Dumpable
Definition: cherryvisdumper.h:39
void onDrawCommand(State *s, tc::Client::Command const &command)
Definition: cherryvisdumper.cpp:189
std::unordered_map< std::string, Logger > unitsLogs_
Definition: cherryvisdumper.h:226
LoggerStream(State *state, Logger &logger, const char *filename, int line)
Definition: cherryvisdumper.h:62
virtual void send(google::LogSeverity severity, const char *full_filename, const char *base_filename, int line, const struct::tm *tm_time, const char *message, size_t message_len) override
Definition: cherryvisdumper.h:274
Definition: cherryvisdumper.h:165
std::string name()
Definition: module.cpp:41
nlohmann::json boardUpdates_
Definition: cherryvisdumper.h:216
virtual void onGameStart(State *s) override
Definition: cherryvisdumper.cpp:87
std::stringstream message_
Definition: cherryvisdumper.h:88
int line_
Definition: cherryvisdumper.h:93
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
UnitId id
Definition: unitsinfo.h:36
virtual ~CherryVisDumperModule()=default
Represents a unit in the game.
Definition: unitsinfo.h:35
void setId(int32_t id, std::string prefix="")
Definition: cherryvisdumper.h:175
std::string getBoardValueAsString(Blackboard::Data const &value)
Definition: cherryvisdumper.cpp:292
std::vector< nlohmann::json > treesMetadata_
Definition: cherryvisdumper.h:234
nlohmann::json to_json() const
Definition: cherryvisdumper.h:108
void setReplayFile(std::string const &replayFile)
Definition: cherryvisdumper.h:118
void setModule(std::string name)
Definition: cherryvisdumper.h:169
std::vector< nlohmann::json > logs_
Definition: cherryvisdumper.h:113
~LoggerStream()
Definition: cherryvisdumper.h:64
void dumpGameValue(State *s, std::string const &key, float value)
Definition: cherryvisdumper.h:158
void dumpTensorsSummary(State *s, std::unordered_map< std::string, ag::Variant > const &tensors)
Definition: cherryvisdumper.cpp:397
Records bot internal state and dumps it to a file readable by CherryVis.
Definition: cherryvisdumper.h:36
void writeTrees(std::string const &dumpDirectory)
Definition: cherryvisdumper.cpp:330
State * state_
Definition: cherryvisdumper.h:90
Logger & getGlobalLogger()
Definition: cherryvisdumper.h:126
std::unique_ptr< CherryVisLogSink > logSink_
Definition: cherryvisdumper.h:261
Logger logs_
Definition: cherryvisdumper.h:225
static std::string parseReplayFileName(std::string name)
Definition: cherryvisdumper.cpp:96
std::thread::id threadId_
Definition: cherryvisdumper.h:297
std::unordered_map< std::string, std::vector< nlohmann::json > > drawCommands_
Definition: cherryvisdumper.h:230
Logger & logger_
Definition: cherryvisdumper.h:91
std::unordered_map< UnitId, UnitData > unitsInfos_
Definition: cherryvisdumper.h:219
nlohmann::json metadata
Definition: cherryvisdumper.h:205
virtual ~CherryVisLogSink()
Definition: cherryvisdumper.h:270
std::vector< nlohmann::json > tasks_
Definition: cherryvisdumper.h:212
std::vector< std::shared_ptr< TreeNode > > children
Definition: cherryvisdumper.h:167
void setFrame(FrameNum f)
Definition: cherryvisdumper.h:172
CherryVisLogSink(CherryVisDumperModule *module, State *state)
Definition: cherryvisdumper.h:266
Main namespace for bot-related code.
Definition: areainfo.cpp:17
nlohmann::json getTensor1d(at::Tensor const &t)
Definition: cherryvisdumper.cpp:387
std::vector< nlohmann::json > heatmapsMetadata_
Definition: cherryvisdumper.h:243
nlohmann::json getTensorSummary(std::string const &name, at::Tensor const &t)
Definition: cherryvisdumper.cpp:355
std::string replayFileName_
Definition: cherryvisdumper.h:259
void addMessage(State *state, std::string message, std::vector< nlohmann::json > attachments={}, const char *full_filename="", int line=0, google::LogSeverity severity=0)
Definition: cherryvisdumper.cpp:478
std::unordered_map< std::string, nlohmann::json > node
Definition: cherryvisdumper.h:195
void writeTensors(std::string const &dumpDirectory)
Definition: cherryvisdumper.cpp:469
Definition: cherryvisdumper.h:41
std::vector< nlohmann::json > attachments_
Definition: cherryvisdumper.h:89
Definition: cherryvisdumper.h:44
Interface for bot modules.
Definition: module.h:30
virtual void onGameEnd(State *s) override
Definition: cherryvisdumper.cpp:130
std::unordered_map< std::string, TreeData > trees_
Definition: cherryvisdumper.h:233
LoggerStream & operator<<(T const &m)
Definition: cherryvisdumper.h:70
Definition: cherryvisdumper.h:204