MsgPackProtocol.h 11.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once

#include <cstdint>
#include <vector>

#include <msgpack.hpp>

#include "types.h"

#include "Field.h"
#include "Bot.h"

namespace MsgPackProtocol
{
	enum
	{
		MESSAGE_TYPE_GAME_INFO = 0x00,
		MESSAGE_TYPE_WORLD_UPDATE = 0x01,

		MESSAGE_TYPE_TICK = 0x10,

		MESSAGE_TYPE_BOT_SPAWN = 0x20,
		MESSAGE_TYPE_BOT_KILL = 0x21,
		MESSAGE_TYPE_BOT_MOVE = 0x22,
Hubert Denkmair's avatar
Hubert Denkmair committed
25
		MESSAGE_TYPE_BOT_LOG = 0x23,
26
		MESSAGE_TYPE_BOT_STATS = 0x24,
27
		MESSAGE_TYPE_BOT_MOVE_HEAD = 0x25,
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

		MESSAGE_TYPE_FOOD_SPAWN = 0x30,
		MESSAGE_TYPE_FOOD_CONSUME = 0x31,
		MESSAGE_TYPE_FOOD_DECAY = 0x32,

		MESSAGE_TYPE_PLAYER_INFO = 0xF0,
	};

	static constexpr const uint8_t PROTOCOL_VERSION = 1;

	struct GameInfoMessage
	{
		double world_size_x = 0;
		double world_size_y = 0;
		double food_decay_per_frame = 0;
43
44
45
46
47

		double snake_distance_per_step = 0;
		double snake_segment_distance_factor = 0;
		double snake_segment_distance_exponent = 0;
		double snake_pull_factor = 0;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	};

	struct PlayerInfoMessage
	{
		guid_t player_id; // id der von dieser Verbindung gesteuerten Schlange
	};

	struct TickMessage
	{
		guid_t frame_id; // frame counter since start of server
	};

	struct WorldUpdateMessage
	{
		Field::BotSet bots;
Hubert Denkmair's avatar
Hubert Denkmair committed
63
		std::vector<Food> food;
64
65
66
67
68
69
70
71
72
73
	};

	struct BotSpawnMessage
	{
		std::shared_ptr<Bot> bot;
	};

	struct BotMoveItem
	{
		guid_t bot_id;
74
		std::vector< Snake::Segment > new_segments;
75
76
77
78
79
80
81
82
83
		uint32_t current_length;
		uint32_t current_segment_radius;
	};

	struct BotMoveMessage
	{
		std::vector<BotMoveItem> items;
	};

84
85
86
87
88
89
90
91
92
93
94
95
96
97
	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;
	};

98
99
100
101
102
103
104
105
	struct BotKillMessage
	{
		guid_t killer_id;
		guid_t victim_id; // victim is deleted in this frame
	};

	struct FoodSpawnMessage
	{
Hubert Denkmair's avatar
Hubert Denkmair committed
106
		std::vector<Food> new_food;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	};

	struct FoodConsumeItem
	{
		guid_t food_id; // food is deleted in this frame
		guid_t bot_id; // bot consuming the food
	};

	struct FoodConsumeMessage
	{
		std::vector<FoodConsumeItem> items;
	};

	struct FoodDecayMessage
	{
		std::vector<guid_t> food_ids; // food is deleted in this frame
	};

125
126
127
128
129
130
	struct BotStatsItem
	{
		guid_t bot_id;
		double natural_food_consumed;
		double carrison_food_consumed;
		double hunted_food_consumed;
Thomas Kolb's avatar
Thomas Kolb committed
131
		double mass;
132
133
134
135
136
137
	};

	struct BotStatsMessage
	{
		std::vector<BotStatsItem> items;
	};
138

Hubert Denkmair's avatar
Hubert Denkmair committed
139
140
141
142
143
144
145
146
147
148
	struct BotLogItem
	{
		uint64_t viewer_key;
		std::string message;
	};

	struct BotLogMessage
	{
		std::vector<BotLogItem> items;
	};
149
150
151
152
153
154
155
156
157
158
}

namespace msgpack {
	MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
		namespace adaptor {

			template <> struct pack<MsgPackProtocol::GameInfoMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::GameInfoMessage const& v) const
				{
159
					o.pack_array(9);
160
161
162
163
164
					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);
165
166
167
168
					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);
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::PlayerInfoMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::PlayerInfoMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_PLAYER_INFO));
					o.pack(v.player_id);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::TickMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::TickMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_TICK));
					o.pack(v.frame_id);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::WorldUpdateMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::WorldUpdateMessage const& v) const
				{
					o.pack_array(4);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_WORLD_UPDATE));
					o.pack(v.bots);
					o.pack(v.food);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotSpawnMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotSpawnMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_SPAWN));
					o.pack(v.bot);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotMoveMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotMoveMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_MOVE));
					o.pack(v.items);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotMoveItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotMoveItem const& v) const
				{
					o.pack_array(4);
					o.pack(v.bot_id);
					o.pack(v.new_segments);
					o.pack(v.current_length);
					o.pack(v.current_segment_radius);
					return o;
				}
			};

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
			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;
				}
			};

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
			template <> struct pack<MsgPackProtocol::BotKillMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotKillMessage const& v) const
				{
					o.pack_array(4);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_KILL));
					o.pack(v.killer_id);
					o.pack(v.victim_id);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodSpawnMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodSpawnMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_FOOD_SPAWN));
					o.pack(v.new_food);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodConsumeMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodConsumeMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_FOOD_CONSUME));
					o.pack(v.items);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodConsumeItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodConsumeItem const& v) const
				{
					o.pack_array(2);
					o.pack(v.food_id);
					o.pack(v.bot_id);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodDecayMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodDecayMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_FOOD_DECAY));
					o.pack(v.food_ids);
					return o;
				}
			};

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
			template <> struct pack<MsgPackProtocol::BotStatsMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotStatsMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_STATS));
					o.pack(v.items);
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotStatsItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotStatsItem const& v) const
				{
					o.pack_array(4);
					o.pack(v.bot_id);
					o.pack(v.natural_food_consumed);
					o.pack(v.carrison_food_consumed);
					o.pack(v.hunted_food_consumed);
Thomas Kolb's avatar
Thomas Kolb committed
352
					o.pack(v.mass);
353
354
355
356
					return o;
				}
			};

357
358
359
360
361
			template <> struct pack<Snake::Segment>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, Snake::Segment const& v) const
				{
					o.pack_array(2);
362
363
					o.pack(v.pos().x());
					o.pack(v.pos().y());
364
365
366
367
368

					return o;
				}
			};

369
370
371
372
373
374
375
376
377
378
379
380
			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;
				}
			};

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
			template<>
				struct convert<Snake::Segment> {
					msgpack::object const& operator()(msgpack::object const& o, Snake::Segment& v) const {
						if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
						if (o.via.array.size != 2) throw msgpack::type_error();
						v = Snake::Segment{{
							o.via.array.ptr[0].as<float>(),
								o.via.array.ptr[1].as<float>()
						}};
						return o;
					}
				};

			template <> struct pack<Food>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, Food const& v) const
				{
					o.pack_array(4);
					o.pack(v.getGUID());
400
401
					o.pack(v.pos().x());
					o.pack(v.pos().y());
402
403
404
405
406
					o.pack(v.getValue());
					return o;
				}
			};

407
			template <> struct pack< std::shared_ptr<Bot>>
408
409
410
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, std::shared_ptr<Bot> const& v) const
				{
411
					o.pack_array(9);
412
413
					o.pack(v->getGUID());
					o.pack(v->getName());
414
415
416
417
418
					o.pack(v->getDatabaseVersionId());
					o.pack(v->getFace());
					o.pack(v->getDogTag());
					o.pack(v->getColors());
					o.pack(v->getSnake()->getMass());
419
420
421
422
423
					o.pack(v->getSnake()->getSegmentRadius());
					o.pack(v->getSnake()->getSegments());
					return o;
				}
			};
Hubert Denkmair's avatar
Hubert Denkmair committed
424

Hubert Denkmair's avatar
Hubert Denkmair committed
425
426
427
428
429
430
431
432
			template <> struct pack<MsgPackProtocol::BotLogMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotLogMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_LOG));
					o.pack(v.items);
433
434
435
436
					return o;
				}
			};

Hubert Denkmair's avatar
Hubert Denkmair committed
437
438
439
440
441
442
443
444
445
446
			template <> struct pack<MsgPackProtocol::BotLogItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotLogItem const& v) const
				{
					o.pack_array(2);
					o.pack(v.viewer_key);
					o.pack(v.message);
					return o;
				}
			};
447
448
449
		} // namespace adaptor
	} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack