Commit 86d3a088 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

Merge remote-tracking branch 'origin/protocol/bot_moved_head' into dev

parents c384c788 19bb6c71
......@@ -24,6 +24,7 @@ namespace MsgPackProtocol
MESSAGE_TYPE_BOT_MOVE = 0x22,
MESSAGE_TYPE_BOT_LOG = 0x23,
MESSAGE_TYPE_BOT_STATS = 0x24,
MESSAGE_TYPE_BOT_MOVE_HEAD = 0x25,
MESSAGE_TYPE_FOOD_SPAWN = 0x30,
MESSAGE_TYPE_FOOD_CONSUME = 0x31,
......@@ -39,6 +40,11 @@ namespace MsgPackProtocol
double world_size_x = 0;
double world_size_y = 0;
double food_decay_per_frame = 0;
double snake_distance_per_step = 0;
double snake_segment_distance_factor = 0;
double snake_segment_distance_exponent = 0;
double snake_pull_factor = 0;
};
struct PlayerInfoMessage
......@@ -75,6 +81,20 @@ namespace MsgPackProtocol
std::vector<BotMoveItem> items;
};
struct BotMoveHeadItem
{
guid_t bot_id;
double mass;
// one head position for each step moved in this frame, in temporal order
std::vector< Vector2D > new_head_positions;
};
struct BotMoveHeadMessage
{
std::vector<BotMoveHeadItem> items;
};
struct BotKillMessage
{
guid_t killer_id;
......@@ -135,12 +155,16 @@ namespace msgpack {
{
template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::GameInfoMessage const& v) const
{
o.pack_array(5);
o.pack_array(9);
o.pack(MsgPackProtocol::PROTOCOL_VERSION);
o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_GAME_INFO));
o.pack(v.world_size_x);
o.pack(v.world_size_y);
o.pack(v.food_decay_per_frame);
o.pack(v.snake_distance_per_step);
o.pack(v.snake_segment_distance_factor);
o.pack(v.snake_segment_distance_exponent);
o.pack(v.snake_pull_factor);
return o;
}
};
......@@ -219,6 +243,30 @@ namespace msgpack {
}
};
template <> struct pack<MsgPackProtocol::BotMoveHeadMessage>
{
template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotMoveHeadMessage const& v) const
{
o.pack_array(3);
o.pack(MsgPackProtocol::PROTOCOL_VERSION);
o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_MOVE_HEAD));
o.pack(v.items);
return o;
}
};
template <> struct pack<MsgPackProtocol::BotMoveHeadItem>
{
template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotMoveHeadItem const& v) const
{
o.pack_array(3);
o.pack(v.bot_id);
o.pack(v.mass);
o.pack(v.new_head_positions);
return o;
}
};
template <> struct pack<MsgPackProtocol::BotKillMessage>
{
template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotKillMessage const& v) const
......@@ -316,6 +364,18 @@ namespace msgpack {
}
};
template <> struct pack<Vector2D>
{
template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, Vector2D const& v) const
{
o.pack_array(2);
o.pack(v.x());
o.pack(v.y());
return o;
}
};
template<>
struct convert<Snake::Segment> {
msgpack::object const& operator()(msgpack::object const& o, Snake::Segment& v) const {
......
......@@ -75,6 +75,7 @@ void MsgPackUpdateTracker::botKilled(
void MsgPackUpdateTracker::botMoved(const std::shared_ptr<Bot> &bot, std::size_t steps)
{
// Fill BotMoveMessage
MsgPackProtocol::BotMoveItem item;
const Snake::SegmentList &segments = bot->getSnake()->getSegments();
......@@ -85,6 +86,15 @@ void MsgPackUpdateTracker::botMoved(const std::shared_ptr<Bot> &bot, std::size_t
item.current_length = segments.size();
m_botMoveMessage->items.push_back(item);
// Fill BotMoveHeadMessage
MsgPackProtocol::BotMoveHeadItem headItem;
headItem.bot_id = bot->getGUID();
headItem.mass = bot->getSnake()->getMass();
headItem.new_head_positions = bot->getSnake()->getHeadPositionsDuringLastMove();
m_botMoveHeadMessage->items.push_back(headItem);
}
void MsgPackUpdateTracker::botLogMessage(uint64_t viewerKey, const std::string& message)
......@@ -96,10 +106,15 @@ void MsgPackUpdateTracker::gameInfo(void)
{
MsgPackProtocol::GameInfoMessage msg;
msg.world_size_x = config::FIELD_SIZE_X;
msg.world_size_y = config::FIELD_SIZE_Y;
msg.world_size_x = config::FIELD_SIZE_X;
msg.world_size_y = config::FIELD_SIZE_Y;
msg.food_decay_per_frame = config::FOOD_DECAY_STEP;
msg.snake_distance_per_step = config::SNAKE_DISTANCE_PER_STEP;
msg.snake_segment_distance_factor = config::SNAKE_SEGMENT_DISTANCE_FACTOR;
msg.snake_segment_distance_exponent = config::SNAKE_SEGMENT_DISTANCE_EXPONENT;
msg.snake_pull_factor = config::SNAKE_PULL_FACTOR;
msgpack::sbuffer buf;
msgpack::pack(buf, msg);
appendMessage(buf);
......@@ -173,6 +188,13 @@ std::string MsgPackUpdateTracker::serialize(void)
appendMessage(buf);
}
// moved bots (compressed version)
if(!m_botMoveHeadMessage->items.empty()) {
msgpack::sbuffer buf;
msgpack::pack(buf, m_botMoveHeadMessage);
appendMessage(buf);
}
// bot statistics
if(!m_botStatsMessage->items.empty()) {
msgpack::sbuffer buf;
......@@ -198,6 +220,7 @@ void MsgPackUpdateTracker::reset(void)
m_foodSpawnMessage = std::make_unique<MsgPackProtocol::FoodSpawnMessage>();
m_foodDecayMessage = std::make_unique<MsgPackProtocol::FoodDecayMessage>();
m_botMoveMessage = std::make_unique<MsgPackProtocol::BotMoveMessage>();
m_botMoveHeadMessage = std::make_unique<MsgPackProtocol::BotMoveHeadMessage>();
m_botStatsMessage = std::make_unique<MsgPackProtocol::BotStatsMessage>();
m_botLogMessage = std::make_unique<MsgPackProtocol::BotLogMessage>();
......
......@@ -21,6 +21,7 @@ class MsgPackUpdateTracker : public UpdateTracker
std::unique_ptr<MsgPackProtocol::FoodSpawnMessage> m_foodSpawnMessage;
std::unique_ptr<MsgPackProtocol::FoodDecayMessage> m_foodDecayMessage;
std::unique_ptr<MsgPackProtocol::BotMoveMessage> m_botMoveMessage;
std::unique_ptr<MsgPackProtocol::BotMoveHeadMessage> m_botMoveHeadMessage;
std::unique_ptr<MsgPackProtocol::BotStatsMessage> m_botStatsMessage;
std::unique_ptr<MsgPackProtocol::BotLogMessage> m_botLogMessage;
......
......@@ -106,6 +106,8 @@ std::size_t Snake::move(real_t targetAngle, bool boost)
steps = config::SNAKE_BOOST_STEPS;
}
m_headPositionsDuringLastMove.clear();
// create new segments at head
for(std::size_t i = 0; i < steps; i++) {
// calculate new segment offset
......@@ -117,6 +119,8 @@ std::size_t Snake::move(real_t targetAngle, bool boost)
headSegment.setPos(headSegment.pos() + movementVector2D);
m_headPositionsDuringLastMove.push_back(headSegment.pos());
m_movedSinceLastSpawn += config::SNAKE_DISTANCE_PER_STEP;
// create new segments, if necessary
......
......@@ -28,6 +28,7 @@ class Snake
};
typedef std::deque< Segment > SegmentList;
typedef std::vector< Vector2D > PositionList;
private:
/*!
......@@ -35,6 +36,8 @@ class Snake
*/
SegmentList m_segments;
PositionList m_headPositionsDuringLastMove;
Field *m_field;
real_t m_mass; //!< Mass (length) of the snake
......@@ -129,4 +132,8 @@ class Snake
bool boostedLastMove(void) const { return m_boostedLastMove; }
/*!
* Get a list of head positions that were used during the last call to move().
*/
const PositionList& getHeadPositionsDuringLastMove(void) const { return m_headPositionsDuringLastMove; };
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment