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 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. // 用于将Protobuf中的类转换为THUAI6的类
  69. inline std::shared_ptr<THUAI6::Butcher> Protobuf2THUAI6Butcher(const protobuf::MessageOfButcher& butcherMsg)
  70. {
  71. auto butcher = std::make_shared<THUAI6::Butcher>();
  72. butcher->x = butcherMsg.x();
  73. butcher->y = butcherMsg.y();
  74. butcher->speed = butcherMsg.speed();
  75. butcher->damage = butcherMsg.damage();
  76. butcher->timeUntilSkillAvailable = butcherMsg.time_until_skill_available();
  77. butcher->place = placeTypeDict[butcherMsg.place()];
  78. butcher->prop = propTypeDict[butcherMsg.prop()];
  79. butcher->butcherType = butcherTypeDict[butcherMsg.butcher_type()];
  80. butcher->guid = butcherMsg.guid();
  81. butcher->movable = butcherMsg.movable();
  82. butcher->playerID = butcherMsg.player_id();
  83. butcher->viewRange = butcherMsg.view_range();
  84. butcher->radius = butcherMsg.radius();
  85. butcher->buff.clear();
  86. for (int i = 0; i < butcherMsg.buff().size(); i++)
  87. {
  88. butcher->buff.push_back(butcherBuffTypeDict[butcherMsg.buff(i)]);
  89. }
  90. return butcher;
  91. }
  92. inline std::shared_ptr<THUAI6::Human> Protobuf2THUAI6Human(const protobuf::MessageOfHuman& humanMsg)
  93. {
  94. auto human = std::make_shared<THUAI6::Human>();
  95. human->x = humanMsg.x();
  96. human->y = humanMsg.y();
  97. human->speed = humanMsg.speed();
  98. human->viewRange = humanMsg.view_range();
  99. human->playerID = humanMsg.player_id();
  100. human->guid = humanMsg.guid();
  101. human->radius = humanMsg.radius();
  102. human->timeUntilSkillAvailable = humanMsg.time_until_skill_available();
  103. human->playerType = THUAI6::PlayerType::HumanPlayer;
  104. human->prop = propTypeDict[humanMsg.prop()];
  105. human->place = placeTypeDict[humanMsg.place()];
  106. human->onChair = humanMsg.on_chair();
  107. human->onGround = humanMsg.on_ground();
  108. human->life = humanMsg.life();
  109. human->hangedTime = humanMsg.hanged_time();
  110. human->humanType = humanTypeDict[humanMsg.human_type()];
  111. human->buff.clear();
  112. for (int i = 0; i < humanMsg.buff_size(); i++)
  113. {
  114. human->buff.push_back(humanBuffTypeDict[humanMsg.buff(i)]);
  115. }
  116. return human;
  117. }
  118. inline std::shared_ptr<THUAI6::Prop> Protobuf2THUAI6Prop(const protobuf::MessageOfProp& propMsg)
  119. {
  120. auto prop = std::make_shared<THUAI6::Prop>();
  121. prop->x = propMsg.x();
  122. prop->y = propMsg.y();
  123. prop->type = propTypeDict[propMsg.type()];
  124. prop->place = placeTypeDict[propMsg.place()];
  125. prop->guid = propMsg.guid();
  126. prop->size = propMsg.size();
  127. prop->facingDirection = propMsg.facing_direction();
  128. prop->isMoving = propMsg.is_moving();
  129. return prop;
  130. }
  131. inline std::vector<std::vector<THUAI6::PlaceType>> Protobuf2THUAI6Map(const protobuf::MessageOfMap& mapMsg)
  132. {
  133. std::vector<std::vector<THUAI6::PlaceType>> map;
  134. for (int i = 0; i < mapMsg.row_size(); i++)
  135. {
  136. std::vector<THUAI6::PlaceType> row;
  137. for (int j = 0; j < mapMsg.row(i).col_size(); j++)
  138. {
  139. row.push_back(placeTypeDict[mapMsg.row(i).col(j)]);
  140. }
  141. map.push_back(row);
  142. }
  143. return map;
  144. }
  145. } // namespace Proto2THUAI6
  146. namespace THUAI62Proto
  147. {
  148. // 用于将THUAI6的枚举转换为Protobuf的枚举
  149. inline std::map<THUAI6::PlaceType, protobuf::PlaceType> placeTypeDict{
  150. {THUAI6::PlaceType::NullPlaceType, protobuf::PlaceType::NULL_PLACE_TYPE},
  151. {THUAI6::PlaceType::Land, protobuf::PlaceType::LAND},
  152. {THUAI6::PlaceType::Wall, protobuf::PlaceType::WALL},
  153. {THUAI6::PlaceType::Grass, protobuf::PlaceType::GRASS},
  154. {THUAI6::PlaceType::Machine, protobuf::PlaceType::MACHINE},
  155. {THUAI6::PlaceType::Gate, protobuf::PlaceType::GATE},
  156. {THUAI6::PlaceType::HiddenGate, protobuf::PlaceType::HIDDEN_GATE},
  157. };
  158. inline std::map<THUAI6::ShapeType, protobuf::ShapeType> shapeTypeDict{
  159. {THUAI6::ShapeType::NullShapeType, protobuf::ShapeType::NULL_SHAPE_TYPE},
  160. {THUAI6::ShapeType::Circle, protobuf::ShapeType::CIRCLE},
  161. {THUAI6::ShapeType::Square, protobuf::ShapeType::SQUARE},
  162. };
  163. inline std::map<THUAI6::PropType, protobuf::PropType> propTypeDict{
  164. {THUAI6::PropType::NullPropType, protobuf::PropType::NULL_PROP_TYPE},
  165. {THUAI6::PropType::PropType1, protobuf::PropType::PTYPE1},
  166. {THUAI6::PropType::PropType2, protobuf::PropType::PTYPE2},
  167. {THUAI6::PropType::PropType3, protobuf::PropType::PTYPE3},
  168. {THUAI6::PropType::PropType4, protobuf::PropType::PTYPE4},
  169. };
  170. inline std::map<THUAI6::PlayerType, protobuf::PlayerType> playerTypeDict{
  171. {THUAI6::PlayerType::NullPlayerType, protobuf::PlayerType::NULL_PLAYER_TYPE},
  172. {THUAI6::PlayerType::HumanPlayer, protobuf::PlayerType::HUMAN_PLAYER},
  173. {THUAI6::PlayerType::ButcherPlayer, protobuf::PlayerType::BUTCHER_PLAYER},
  174. };
  175. inline std::map<THUAI6::HumanType, protobuf::HumanType> humanTypeDict{
  176. {THUAI6::HumanType::NullHumanType, protobuf::HumanType::NULL_HUMAN_TYPE},
  177. {THUAI6::HumanType::HumanType1, protobuf::HumanType::HUMANTYPE1},
  178. {THUAI6::HumanType::HumanType2, protobuf::HumanType::HUMANTYPE2},
  179. {THUAI6::HumanType::HumanType3, protobuf::HumanType::HUMANTYPE3},
  180. {THUAI6::HumanType::HumanType4, protobuf::HumanType::HUMANTYPE4},
  181. };
  182. inline std::map<THUAI6::HumanBuffType, protobuf::HumanBuffType> humanBuffTypeDict{
  183. {THUAI6::HumanBuffType::NullHumanBuffType, protobuf::HumanBuffType::NULL_HBUFF_TYPE},
  184. {THUAI6::HumanBuffType::HumanBuffType1, protobuf::HumanBuffType::HBUFFTYPE1},
  185. {THUAI6::HumanBuffType::HumanBuffType2, protobuf::HumanBuffType::HBUFFTYPE2},
  186. {THUAI6::HumanBuffType::HumanBuffType3, protobuf::HumanBuffType::HBUFFTYPE3},
  187. {THUAI6::HumanBuffType::HumanBuffType4, protobuf::HumanBuffType::HBUFFTYPE4},
  188. };
  189. inline std::map<THUAI6::ButcherType, protobuf::ButcherType> butcherTypeDict{
  190. {THUAI6::ButcherType::NullButcherType, protobuf::ButcherType::NULL_BUTCHER_TYPE},
  191. {THUAI6::ButcherType::ButcherType1, protobuf::ButcherType::BUTCHERTYPE1},
  192. {THUAI6::ButcherType::ButcherType2, protobuf::ButcherType::BUTCHERTYPE2},
  193. {THUAI6::ButcherType::ButcherType3, protobuf::ButcherType::BUTCHERTYPE3},
  194. {THUAI6::ButcherType::ButcherType4, protobuf::ButcherType::BUTCHERTYPE4},
  195. };
  196. inline std::map<THUAI6::ButcherBuffType, protobuf::ButcherBuffType> butcherBuffTypeDict{
  197. {THUAI6::ButcherBuffType::NullButcherBuffType, protobuf::ButcherBuffType::NULL_BBUFF_TYPE},
  198. {THUAI6::ButcherBuffType::ButcherBuffType1, protobuf::ButcherBuffType::BBUFFTYPE1},
  199. {THUAI6::ButcherBuffType::ButcherBuffType2, protobuf::ButcherBuffType::BBUFFTYPE2},
  200. {THUAI6::ButcherBuffType::ButcherBuffType3, protobuf::ButcherBuffType::BBUFFTYPE3},
  201. {THUAI6::ButcherBuffType::ButcherBuffType4, protobuf::ButcherBuffType::BBUFFTYPE4},
  202. };
  203. // 用于将THUAI6的类转换为Protobuf的消息
  204. inline protobuf::PlayerMsg THUAI62ProtobufPlayer(int64_t playerID, THUAI6::PlayerType playerType, THUAI6::HumanType humanType, THUAI6::ButcherType butcherType)
  205. {
  206. protobuf::PlayerMsg playerMsg;
  207. playerMsg.set_player_id(playerID);
  208. playerMsg.set_player_type(playerTypeDict[playerType]);
  209. if (playerType == THUAI6::PlayerType::HumanPlayer)
  210. {
  211. playerMsg.set_human_type(humanTypeDict[humanType]);
  212. }
  213. else if (playerType == THUAI6::PlayerType::ButcherPlayer)
  214. {
  215. playerMsg.set_butcher_type(butcherTypeDict[butcherType]);
  216. }
  217. return playerMsg;
  218. }
  219. inline protobuf::IDMsg THUAI62ProtobufID(int playerID)
  220. {
  221. protobuf::IDMsg idMsg;
  222. idMsg.set_player_id(playerID);
  223. return idMsg;
  224. }
  225. inline protobuf::MoveMsg THUAI62ProtobufMove(int64_t time, double angle, int64_t id)
  226. {
  227. protobuf::MoveMsg moveMsg;
  228. moveMsg.set_time_in_milliseconds(time);
  229. moveMsg.set_angle(angle);
  230. moveMsg.set_player_id(id);
  231. return moveMsg;
  232. }
  233. inline protobuf::PickMsg THUAI62ProtobufPick(THUAI6::PropType prop, int64_t id)
  234. {
  235. protobuf::PickMsg pickMsg;
  236. pickMsg.set_prop_type(propTypeDict[prop]);
  237. pickMsg.set_player_id(id);
  238. return pickMsg;
  239. }
  240. inline protobuf::SendMsg THUAI62ProtobufSend(std::string msg, int64_t toID, int64_t id)
  241. {
  242. protobuf::SendMsg sendMsg;
  243. sendMsg.set_message(msg);
  244. sendMsg.set_to_player_id(toID);
  245. sendMsg.set_player_id(id);
  246. return sendMsg;
  247. }
  248. inline protobuf::AttackMsg THUAI62ProtobufAttack(double angle, int64_t id)
  249. {
  250. protobuf::AttackMsg attackMsg;
  251. attackMsg.set_angle(angle);
  252. attackMsg.set_player_id(id);
  253. return attackMsg;
  254. }
  255. } // namespace THUAI62Proto
  256. #endif