TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
flatbuffer_conversions.h
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  */
9 
10 #pragma once
11 
12 #include <algorithm>
13 #include <vector>
14 
15 #include "messages_generated.h"
16 
17 namespace torchcraft {
18 namespace replayer {
19 
20 // Serialize to FlatBuffers
21 
22 const auto packActionsOfPlayer = [](flatbuffers::FlatBufferBuilder& builder) {
23  return [&builder](const std::pair<int32_t, std::vector<Action>>& actionPair) {
24  auto packAction = [&builder](const Action& action) {
25  auto actionsOffset = builder.CreateVector(action.action);
26  builder.Finish(actionsOffset);
27 
28  fbs::ActionBuilder fbsActionBuilder(builder);
29  fbsActionBuilder.add_action(actionsOffset);
30  fbsActionBuilder.add_uid(action.uid);
31  fbsActionBuilder.add_aid(action.aid);
32  auto output = fbsActionBuilder.Finish();
33  builder.Finish(output);
34  return output;
35  };
36 
37  std::vector<flatbuffers::Offset<fbs::Action>> fbsActions(actionPair.second.size());
38  std::transform(actionPair.second.begin(), actionPair.second.end(), fbsActions.begin(), packAction);
39 
40  auto actionOffsets = builder.CreateVector(fbsActions);
41  builder.Finish(actionOffsets);
42 
43  fbs::ActionsOfPlayerBuilder fbsActionsOfPlayerBuilder(builder);
44  fbsActionsOfPlayerBuilder.add_playerId(actionPair.first);
45  fbsActionsOfPlayerBuilder.add_actions(actionOffsets);
46  auto output = fbsActionsOfPlayerBuilder.Finish();
47  builder.Finish(output);
48  return output;
49  };
50 };
51 
52 const auto packResourcesOfPlayer = [](flatbuffers::FlatBufferBuilder& builder) {
53  return [&builder](const std::pair<int32_t, Resources>& resourcesPair) {
54  auto resources = resourcesPair.second;
55  fbs::ResourcesBuilder fbsResourcesBuilder(builder);
56  fbsResourcesBuilder.add_ore(resources.ore);
57  fbsResourcesBuilder.add_gas(resources.gas);
58  fbsResourcesBuilder.add_used_psi(resources.used_psi);
59  fbsResourcesBuilder.add_total_psi(resources.total_psi);
60  fbsResourcesBuilder.add_upgrades(resources.upgrades);
61  fbsResourcesBuilder.add_upgrades_level(resources.upgrades_level);
62  fbsResourcesBuilder.add_techs(resources.techs);
63  auto fbsResources = fbsResourcesBuilder.Finish();
64  builder.Finish(fbsResources);
65 
66  fbs::ResourcesOfPlayerBuilder fbsResourcesOfPlayerBuilder(builder);
67  fbsResourcesOfPlayerBuilder.add_playerId(resourcesPair.first);
68  fbsResourcesOfPlayerBuilder.add_resources(fbsResources);
69  auto output = fbsResourcesOfPlayerBuilder.Finish();
70  builder.Finish(output);
71  return output;
72  };
73 };
74 
75 const auto packBullet = [](const Bullet& bullet) {
76  return fbs::Bullet(bullet.type, bullet.x, bullet.y);
77 };
78 
79 // Deserialize from FlatBuffers
80 
81 const auto unpackAction = [](const fbs::Action* fbsAction) {
82  assert(fbsAction);
83  assert(fbsAction->action());
84  auto fbsActionInts = fbsAction->action();
85  Action action;
86  action.action.resize(fbsActionInts->size());
87  std::copy(fbsActionInts->begin(), fbsActionInts->end(), action.action.begin());
88  action.uid = fbsAction->uid();
89  action.aid = fbsAction->aid();
90  return action;
91 };
92 
93 const auto unpackResources = [](const fbs::ResourcesOfPlayer* fbsResourcesOfPlayer) {
94  auto fbsResources = fbsResourcesOfPlayer->resources();
95  auto resources = std::make_pair(fbsResourcesOfPlayer->playerId(), Resources()) ;
96  resources.second.ore = fbsResources->ore();
97  resources.second.gas = fbsResources->gas();
98  resources.second.used_psi = fbsResources->used_psi();
99  resources.second.total_psi = fbsResources->total_psi();
100  resources.second.upgrades = fbsResources->upgrades();
101  resources.second.upgrades_level = fbsResources->upgrades_level();
102  resources.second.techs = fbsResources->techs();
103  return resources;
104 };
105 
106 const auto unpackBullet = [](const fbs::Bullet* fbsBullet) {
107  Bullet bullet;
108  bullet.type = fbsBullet->type();
109  bullet.x = fbsBullet->x();
110  bullet.y = fbsBullet->y();
111  return bullet;
112 };
113 
114 }; //namespace replayer
115 }; //namespace storchcraft
int32_t x
Definition: frame.h:45
const auto unpackResources
Definition: flatbuffer_conversions.h:93
Copyright (c) 2015-present, Facebook, Inc.
Definition: openbwprocess.h:17
Definition: frame.h:48
const auto packActionsOfPlayer
Definition: flatbuffer_conversions.h:22
int32_t type
Definition: frame.h:45
const auto unpackAction
Definition: flatbuffer_conversions.h:81
int32_t uid
Definition: frame.h:50
Definition: frame.h:166
int32_t aid
Definition: frame.h:51
const auto packResourcesOfPlayer
Definition: flatbuffer_conversions.h:52
std::vector< int32_t > action
Definition: frame.h:49
Definition: frame.h:44
int32_t y
Definition: frame.h:45
const auto unpackBullet
Definition: flatbuffer_conversions.h:106
const auto packBullet
Definition: flatbuffer_conversions.h:75