You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.hpp 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // 杂项函数
  2. #pragma once
  3. #ifndef UTILS_HPP
  4. #define UTILS_HPP
  5. #include <cstdint>
  6. #include "Message2Clients.pb.h"
  7. #include "Message2Server.pb.h"
  8. #include "MessageType.pb.h"
  9. #include "structures.h"
  10. // 辅助函数,用于将proto信息转换为THUAI6信息
  11. namespace Proto2THUAI6
  12. {
  13. // 用于将Protobuf中的枚举转换为THUAI6的枚举
  14. inline std::map<protobuf::PlaceType, THUAI6::PlaceType> placeTypeDict{
  15. {protobuf::PlaceType::NULL_PLACE_TYPE, THUAI6::PlaceType::NullPlaceType},
  16. {protobuf::PlaceType::LAND, THUAI6::PlaceType::Land},
  17. {protobuf::PlaceType::WALL, THUAI6::PlaceType::Wall},
  18. {protobuf::PlaceType::GRASS, THUAI6::PlaceType::Grass},
  19. {protobuf::PlaceType::MACHINE, THUAI6::PlaceType::Machine},
  20. {protobuf::PlaceType::GATE, THUAI6::PlaceType::Gate},
  21. {protobuf::PlaceType::HIDDEN_GATE, THUAI6::PlaceType::HiddenGate},
  22. };
  23. inline std::map<protobuf::ShapeType, THUAI6::ShapeType> shapeTypeDict{
  24. {protobuf::ShapeType::NULL_SHAPE_TYPE, THUAI6::ShapeType::NullShapeType},
  25. {protobuf::ShapeType::CIRCLE, THUAI6::ShapeType::Circle},
  26. {protobuf::ShapeType::SQUARE, THUAI6::ShapeType::Square},
  27. };
  28. inline std::map<protobuf::PropType, THUAI6::PropType> propTypeDict{
  29. {protobuf::PropType::NULL_PROP_TYPE, THUAI6::PropType::NullPropType},
  30. {protobuf::PropType::PTYPE1, THUAI6::PropType::PropType1},
  31. {protobuf::PropType::PTYPE2, THUAI6::PropType::PropType2},
  32. {protobuf::PropType::PTYPE3, THUAI6::PropType::PropType3},
  33. {protobuf::PropType::PTYPE4, THUAI6::PropType::PropType4},
  34. };
  35. inline std::map<protobuf::PlayerType, THUAI6::PlayerType> playerTypeDict{
  36. {protobuf::PlayerType::NULL_PLAYER_TYPE, THUAI6::PlayerType::NullPlayerType},
  37. {protobuf::PlayerType::HUMAN_PLAYER, THUAI6::PlayerType::HumanPlayer},
  38. {protobuf::PlayerType::BUTCHER_PLAYER, THUAI6::PlayerType::ButcherPlayer},
  39. };
  40. inline std::map<protobuf::HumanType, THUAI6::HumanType> humanTypeDict{
  41. {protobuf::HumanType::NULL_HUMAN_TYPE, THUAI6::HumanType::NullHumanType},
  42. {protobuf::HumanType::HUMANTYPE1, THUAI6::HumanType::HumanType1},
  43. {protobuf::HumanType::HUMANTYPE2, THUAI6::HumanType::HumanType2},
  44. {protobuf::HumanType::HUMANTYPE3, THUAI6::HumanType::HumanType3},
  45. {protobuf::HumanType::HUMANTYPE4, THUAI6::HumanType::HumanType4},
  46. };
  47. inline std::map<protobuf::ButcherType, THUAI6::ButcherType> butcherTypeDict{
  48. {protobuf::ButcherType::NULL_BUTCHER_TYPE, THUAI6::ButcherType::NullButcherType},
  49. {protobuf::ButcherType::BUTCHERTYPE1, THUAI6::ButcherType::ButcherType1},
  50. {protobuf::ButcherType::BUTCHERTYPE2, THUAI6::ButcherType::ButcherType2},
  51. {protobuf::ButcherType::BUTCHERTYPE3, THUAI6::ButcherType::ButcherType3},
  52. {protobuf::ButcherType::BUTCHERTYPE4, THUAI6::ButcherType::ButcherType4},
  53. };
  54. inline std::map<protobuf::HumanBuffType, THUAI6::HumanBuffType> humanBuffTypeDict{
  55. {protobuf::HumanBuffType::NULL_HBUFF_TYPE, THUAI6::HumanBuffType::NullHumanBuffType},
  56. {protobuf::HumanBuffType::HBUFFTYPE1, THUAI6::HumanBuffType::HumanBuffType1},
  57. {protobuf::HumanBuffType::HBUFFTYPE2, THUAI6::HumanBuffType::HumanBuffType2},
  58. {protobuf::HumanBuffType::HBUFFTYPE3, THUAI6::HumanBuffType::HumanBuffType3},
  59. {protobuf::HumanBuffType::HBUFFTYPE4, THUAI6::HumanBuffType::HumanBuffType4},
  60. };
  61. inline std::map<protobuf::ButcherBuffType, THUAI6::ButcherBuffType> butcherBuffTypeDict{
  62. {protobuf::ButcherBuffType::NULL_BBUFF_TYPE, THUAI6::ButcherBuffType::NullButcherBuffType},
  63. {protobuf::ButcherBuffType::BBUFFTYPE1, THUAI6::ButcherBuffType::ButcherBuffType1},
  64. {protobuf::ButcherBuffType::BBUFFTYPE2, THUAI6::ButcherBuffType::ButcherBuffType2},
  65. {protobuf::ButcherBuffType::BBUFFTYPE3, THUAI6::ButcherBuffType::ButcherBuffType3},
  66. {protobuf::ButcherBuffType::BBUFFTYPE4, THUAI6::ButcherBuffType::ButcherBuffType4},
  67. };
  68. inline std::map<protobuf::HumanState, THUAI6::HumanState> humanStateDict{
  69. {protobuf::HumanState::NULL_STATUS, THUAI6::HumanState::NullHumanState},
  70. {protobuf::HumanState::IDLE, THUAI6::HumanState::Idle},
  71. {protobuf::HumanState::FIXING, THUAI6::HumanState::Fixing},
  72. {protobuf::HumanState::DYING, THUAI6::HumanState::Dying},
  73. {protobuf::HumanState::ON_CHAIR, THUAI6::HumanState::OnChair},
  74. {protobuf::HumanState::DEAD, THUAI6::HumanState::Dead},
  75. };
  76. inline std::map<protobuf::GameState, THUAI6::GameState> gameStateDict{
  77. {protobuf::GameState::NULL_GAME_STATE, THUAI6::GameState::NullGameState},
  78. {protobuf::GameState::GAME_START, THUAI6::GameState::GameStart},
  79. {protobuf::GameState::GAME_RUNNING, THUAI6::GameState::GameRunning},
  80. {protobuf::GameState::GAME_END, THUAI6::GameState::GameEnd},
  81. };
  82. // 用于将Protobuf中的类转换为THUAI6的类
  83. inline std::shared_ptr<THUAI6::Butcher> Protobuf2THUAI6Butcher(const protobuf::MessageOfButcher& butcherMsg)
  84. {
  85. auto butcher = std::make_shared<THUAI6::Butcher>();
  86. butcher->x = butcherMsg.x();
  87. butcher->y = butcherMsg.y();
  88. butcher->speed = butcherMsg.speed();
  89. butcher->damage = butcherMsg.damage();
  90. butcher->timeUntilSkillAvailable = butcherMsg.time_until_skill_available();
  91. butcher->place = placeTypeDict[butcherMsg.place()];
  92. butcher->prop = propTypeDict[butcherMsg.prop()];
  93. butcher->butcherType = butcherTypeDict[butcherMsg.butcher_type()];
  94. butcher->guid = butcherMsg.guid();
  95. butcher->movable = butcherMsg.movable();
  96. butcher->playerID = butcherMsg.player_id();
  97. butcher->viewRange = butcherMsg.view_range();
  98. butcher->radius = butcherMsg.radius();
  99. butcher->buff.clear();
  100. for (int i = 0; i < butcherMsg.buff().size(); i++)
  101. {
  102. butcher->buff.push_back(butcherBuffTypeDict[butcherMsg.buff(i)]);
  103. }
  104. return butcher;
  105. }
  106. inline std::shared_ptr<THUAI6::Human> Protobuf2THUAI6Human(const protobuf::MessageOfHuman& humanMsg)
  107. {
  108. auto human = std::make_shared<THUAI6::Human>();
  109. human->x = humanMsg.x();
  110. human->y = humanMsg.y();
  111. human->speed = humanMsg.speed();
  112. human->viewRange = humanMsg.view_range();
  113. human->playerID = humanMsg.player_id();
  114. human->guid = humanMsg.guid();
  115. human->radius = humanMsg.radius();
  116. human->timeUntilSkillAvailable = humanMsg.time_until_skill_available();
  117. human->playerType = THUAI6::PlayerType::HumanPlayer;
  118. human->prop = propTypeDict[humanMsg.prop()];
  119. human->place = placeTypeDict[humanMsg.place()];
  120. human->state = humanStateDict[humanMsg.state()];
  121. human->life = humanMsg.life();
  122. human->hangedTime = humanMsg.hanged_time();
  123. human->humanType = humanTypeDict[humanMsg.human_type()];
  124. human->buff.clear();
  125. for (int i = 0; i < humanMsg.buff_size(); i++)
  126. {
  127. human->buff.push_back(humanBuffTypeDict[humanMsg.buff(i)]);
  128. }
  129. return human;
  130. }
  131. inline std::shared_ptr<THUAI6::Prop> Protobuf2THUAI6Prop(const protobuf::MessageOfProp& propMsg)
  132. {
  133. auto prop = std::make_shared<THUAI6::Prop>();
  134. prop->x = propMsg.x();
  135. prop->y = propMsg.y();
  136. prop->type = propTypeDict[propMsg.type()];
  137. prop->place = placeTypeDict[propMsg.place()];
  138. prop->guid = propMsg.guid();
  139. prop->size = propMsg.size();
  140. prop->facingDirection = propMsg.facing_direction();
  141. prop->isMoving = propMsg.is_moving();
  142. return prop;
  143. }
  144. inline std::vector<std::vector<THUAI6::PlaceType>> Protobuf2THUAI6Map(const protobuf::MessageOfMap& mapMsg)
  145. {
  146. std::vector<std::vector<THUAI6::PlaceType>> map;
  147. for (int i = 0; i < mapMsg.row_size(); i++)
  148. {
  149. std::vector<THUAI6::PlaceType> row;
  150. for (int j = 0; j < mapMsg.row(i).col_size(); j++)
  151. {
  152. row.push_back(placeTypeDict[mapMsg.row(i).col(j)]);
  153. }
  154. map.push_back(row);
  155. }
  156. return map;
  157. }
  158. } // namespace Proto2THUAI6
  159. namespace THUAI62Proto
  160. {
  161. // 用于将THUAI6的枚举转换为Protobuf的枚举
  162. inline std::map<THUAI6::PlaceType, protobuf::PlaceType> placeTypeDict{
  163. {THUAI6::PlaceType::NullPlaceType, protobuf::PlaceType::NULL_PLACE_TYPE},
  164. {THUAI6::PlaceType::Land, protobuf::PlaceType::LAND},
  165. {THUAI6::PlaceType::Wall, protobuf::PlaceType::WALL},
  166. {THUAI6::PlaceType::Grass, protobuf::PlaceType::GRASS},
  167. {THUAI6::PlaceType::Machine, protobuf::PlaceType::MACHINE},
  168. {THUAI6::PlaceType::Gate, protobuf::PlaceType::GATE},
  169. {THUAI6::PlaceType::HiddenGate, protobuf::PlaceType::HIDDEN_GATE},
  170. };
  171. inline std::map<THUAI6::ShapeType, protobuf::ShapeType> shapeTypeDict{
  172. {THUAI6::ShapeType::NullShapeType, protobuf::ShapeType::NULL_SHAPE_TYPE},
  173. {THUAI6::ShapeType::Circle, protobuf::ShapeType::CIRCLE},
  174. {THUAI6::ShapeType::Square, protobuf::ShapeType::SQUARE},
  175. };
  176. inline std::map<THUAI6::PropType, protobuf::PropType> propTypeDict{
  177. {THUAI6::PropType::NullPropType, protobuf::PropType::NULL_PROP_TYPE},
  178. {THUAI6::PropType::PropType1, protobuf::PropType::PTYPE1},
  179. {THUAI6::PropType::PropType2, protobuf::PropType::PTYPE2},
  180. {THUAI6::PropType::PropType3, protobuf::PropType::PTYPE3},
  181. {THUAI6::PropType::PropType4, protobuf::PropType::PTYPE4},
  182. };
  183. inline std::map<THUAI6::PlayerType, protobuf::PlayerType> playerTypeDict{
  184. {THUAI6::PlayerType::NullPlayerType, protobuf::PlayerType::NULL_PLAYER_TYPE},
  185. {THUAI6::PlayerType::HumanPlayer, protobuf::PlayerType::HUMAN_PLAYER},
  186. {THUAI6::PlayerType::ButcherPlayer, protobuf::PlayerType::BUTCHER_PLAYER},
  187. };
  188. inline std::map<THUAI6::HumanType, protobuf::HumanType> humanTypeDict{
  189. {THUAI6::HumanType::NullHumanType, protobuf::HumanType::NULL_HUMAN_TYPE},
  190. {THUAI6::HumanType::HumanType1, protobuf::HumanType::HUMANTYPE1},
  191. {THUAI6::HumanType::HumanType2, protobuf::HumanType::HUMANTYPE2},
  192. {THUAI6::HumanType::HumanType3, protobuf::HumanType::HUMANTYPE3},
  193. {THUAI6::HumanType::HumanType4, protobuf::HumanType::HUMANTYPE4},
  194. };
  195. inline std::map<THUAI6::HumanBuffType, protobuf::HumanBuffType> humanBuffTypeDict{
  196. {THUAI6::HumanBuffType::NullHumanBuffType, protobuf::HumanBuffType::NULL_HBUFF_TYPE},
  197. {THUAI6::HumanBuffType::HumanBuffType1, protobuf::HumanBuffType::HBUFFTYPE1},
  198. {THUAI6::HumanBuffType::HumanBuffType2, protobuf::HumanBuffType::HBUFFTYPE2},
  199. {THUAI6::HumanBuffType::HumanBuffType3, protobuf::HumanBuffType::HBUFFTYPE3},
  200. {THUAI6::HumanBuffType::HumanBuffType4, protobuf::HumanBuffType::HBUFFTYPE4},
  201. };
  202. inline std::map<THUAI6::ButcherType, protobuf::ButcherType> butcherTypeDict{
  203. {THUAI6::ButcherType::NullButcherType, protobuf::ButcherType::NULL_BUTCHER_TYPE},
  204. {THUAI6::ButcherType::ButcherType1, protobuf::ButcherType::BUTCHERTYPE1},
  205. {THUAI6::ButcherType::ButcherType2, protobuf::ButcherType::BUTCHERTYPE2},
  206. {THUAI6::ButcherType::ButcherType3, protobuf::ButcherType::BUTCHERTYPE3},
  207. {THUAI6::ButcherType::ButcherType4, protobuf::ButcherType::BUTCHERTYPE4},
  208. };
  209. inline std::map<THUAI6::ButcherBuffType, protobuf::ButcherBuffType> butcherBuffTypeDict{
  210. {THUAI6::ButcherBuffType::NullButcherBuffType, protobuf::ButcherBuffType::NULL_BBUFF_TYPE},
  211. {THUAI6::ButcherBuffType::ButcherBuffType1, protobuf::ButcherBuffType::BBUFFTYPE1},
  212. {THUAI6::ButcherBuffType::ButcherBuffType2, protobuf::ButcherBuffType::BBUFFTYPE2},
  213. {THUAI6::ButcherBuffType::ButcherBuffType3, protobuf::ButcherBuffType::BBUFFTYPE3},
  214. {THUAI6::ButcherBuffType::ButcherBuffType4, protobuf::ButcherBuffType::BBUFFTYPE4},
  215. };
  216. // 用于将THUAI6的类转换为Protobuf的消息
  217. inline protobuf::PlayerMsg THUAI62ProtobufPlayer(int64_t playerID, THUAI6::PlayerType playerType, THUAI6::HumanType humanType, THUAI6::ButcherType butcherType)
  218. {
  219. protobuf::PlayerMsg playerMsg;
  220. playerMsg.set_player_id(playerID);
  221. playerMsg.set_player_type(playerTypeDict[playerType]);
  222. if (playerType == THUAI6::PlayerType::HumanPlayer)
  223. {
  224. playerMsg.set_human_type(humanTypeDict[humanType]);
  225. }
  226. else if (playerType == THUAI6::PlayerType::ButcherPlayer)
  227. {
  228. playerMsg.set_butcher_type(butcherTypeDict[butcherType]);
  229. }
  230. return playerMsg;
  231. }
  232. inline protobuf::IDMsg THUAI62ProtobufID(int playerID)
  233. {
  234. protobuf::IDMsg idMsg;
  235. idMsg.set_player_id(playerID);
  236. return idMsg;
  237. }
  238. inline protobuf::MoveMsg THUAI62ProtobufMove(int64_t time, double angle, int64_t id)
  239. {
  240. protobuf::MoveMsg moveMsg;
  241. moveMsg.set_time_in_milliseconds(time);
  242. moveMsg.set_angle(angle);
  243. moveMsg.set_player_id(id);
  244. return moveMsg;
  245. }
  246. inline protobuf::PickMsg THUAI62ProtobufPick(THUAI6::PropType prop, int64_t id)
  247. {
  248. protobuf::PickMsg pickMsg;
  249. pickMsg.set_prop_type(propTypeDict[prop]);
  250. pickMsg.set_player_id(id);
  251. return pickMsg;
  252. }
  253. inline protobuf::SendMsg THUAI62ProtobufSend(std::string msg, int64_t toID, int64_t id)
  254. {
  255. protobuf::SendMsg sendMsg;
  256. sendMsg.set_message(msg);
  257. sendMsg.set_to_player_id(toID);
  258. sendMsg.set_player_id(id);
  259. return sendMsg;
  260. }
  261. inline protobuf::AttackMsg THUAI62ProtobufAttack(double angle, int64_t id)
  262. {
  263. protobuf::AttackMsg attackMsg;
  264. attackMsg.set_angle(angle);
  265. attackMsg.set_player_id(id);
  266. return attackMsg;
  267. }
  268. } // namespace THUAI62Proto
  269. #endif