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

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