游戏消息的格式 - PJ-FDU/fudancraft GitHub Wiki

0 1 2 3 4 5 6 7 8 9 10 11 12 13
cmd(1) 0 0 gx gy camp unit_type
mov(2) u0 0 gx1 gy1 gx2 gy2
atk(3) u0 u1 damage

可不可以以这样的形式储存呢?

enum OperationCode
{
	CRT = 0,
	MOC = 1,
	ATC = 2
};
struct Coordinate {
	int x ;
	int y ;
};
struct operation
{
	OperationCode operation_code;
	int unit_1;
	int unit_2;

	int damage;

	std::vector<Coordinate> position;
	int camp;
	int unit_type;
};

很有道理,结构的存储还是比直接用向量存好,客户端和网络传输用起来可能都更加方便。我做了一点小小的修改,如下:

class GameMessage 
{
public:
	enum CmdCode
	{
		CRT = 0,
		MOV = 1,
		ATK = 2
	};

	CmdCode cmd_code;
	int unit_0;
	int unit_1;

	int damage;

	GridPath grid_path;
	int camp;
	int unit_type;

	
	GameMessage(CmdCode _cmd_code, int _unit_0, int _unit_1, int _damage, int _camp, int _unit_type, const GridPath& _grid_path) :
		cmd_code{ _cmd_code }, unit_0{ _unit_0 }, unit_1{ _unit_1 }, damage{ _damage }, camp{ _camp }, unit_type{ _unit_type }, grid_path{ _grid_path }
	{}
};

Protobuf 简单示例

#include <iostream>
#include "GameMessage.pb.h"
using std::cout;
using std::endl;
int main()
{
	GameMessage game_message;
	game_message.set_cmd_code(::GameMessage::CRT);
	game_message.set_unit_0(22);
	game_message.set_unit_1(33);
	game_message.set_damage(333);
	//*********** add grid path*****************
	GridPath* grid_path = game_message.mutable_grid_path();
	GridPoint* grid_point = grid_path->add_grid_point();
	// add the first point
	grid_point->set_x(3);
	grid_point->set_y(4);
	// add another
	grid_point = grid_path->add_grid_point();
	grid_point->set_x(5);
	grid_point->set_y(6);
	;
	//************ end of adding gired path*****************
	game_message.set_camp(1);
	game_message.set_unit_type(3);


	//BELOW is LOWLEVEL TRANSMATION
	///////////////////////////////////////////////////////////////////////
	//you can send it into the socket interface like
	// client.push(gamemessage)//just as example,not done yet
	// client.send()

	// send the game message to the client
	// below is black to upper opperation
	GameMessageSet game_message_set;
	auto gm = game_message_set.add_game_message();
	gm->CopyFrom(game_message);
	auto str = game_message_set.SerializeAsString();
//		+
//		|
//		|
//		+-------> send to server
	////////recev from the server
	GameMessageSet game_message_set_recv;
	game_message_set_recv.ParseFromString(str);

	for (auto i = 0; i < game_message_set_recv.game_message_size(); i++)
		;//out put the gamemessage
	//just as example 
	auto game_message_recv = game_message_set_recv.game_message(0);
	/////////////////////////////////////////////////////////////////////////
	//OUT OF the LOWLEVEL 
	cout << game_message_recv.cmd_code() << endl;
	cout << game_message_recv.unit_0() << endl;
	for (auto i = 0; i < game_message_recv.grid_path().grid_point_size(); ++i)
		cout << game_message_recv.grid_path().grid_point(i).x() << endl;
	cout << game_message_recv.camp() << endl;




	return 0;
}

实际游戏消息示例:

通过语句

log("%x", msgs->SerializeAsString());

得到,由于%s输出的字符串为乱码,所以采用%x以16进制格式输出。

创建3个单位的消息:

369b100

1个单位寻路移动的消息:

369b4b8

3个单位同时寻路移动的消息:

3696128

如果采用%s的格式输出,得到如下结果:

创建3个单位的消息:

8��

1个单位寻路移动的消息:

8��

3个单位同时寻路移动的消息:

8��

如果采用

log("%s", msgs->SerializeAsString().c_str());

语句输出的话,得到如下结果:

创建3个单位的消息:

8 8 * 8

1个单位寻路移动的消息:

* 8

3个单位同时寻路移动的消息:

,*$ 8

(不知道是否复制完整了,或许应该输出到文件试一下?)

可能还需要再加入命令TRC,表示索敌移动:

enum GameMessage_CmdCode {
  GameMessage_CmdCode_CRT = 0,
  GameMessage_CmdCode_MOV = 1,
  GameMessage_CmdCode_ATK = 2,
  GameMessage_CmdCode_TRC = 3,
  GameMessage_CmdCode_GameMessage_CmdCode_INT_MIN_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32min,
  GameMessage_CmdCode_GameMessage_CmdCode_INT_MAX_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32max
};
⚠️ **GitHub.com Fallback** ⚠️