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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. namespace AssistFunction
  11. {
  12. constexpr int numOfGridPerCell = 100;
  13. [[nodiscard]] constexpr inline int GridToCell(int grid) noexcept
  14. {
  15. return grid / numOfGridPerCell;
  16. }
  17. inline bool HaveView(int viewRange, int x, int y, int newX, int newY, THUAI6::PlaceType myPlace, THUAI6::PlaceType newPlace, std::vector<std::vector<THUAI6::PlaceType>>& map)
  18. {
  19. int deltaX = newX - x;
  20. int deltaY = newY - y;
  21. double distance = deltaX * deltaX + deltaY * deltaY;
  22. if (newPlace == THUAI6::PlaceType::Grass && myPlace != THUAI6::PlaceType::Grass) // 草丛外必不可能看到草丛内
  23. return false;
  24. if (distance < viewRange * viewRange)
  25. {
  26. int divide = std::max(std::abs(deltaX), std::abs(deltaY)) / 100;
  27. double dx = deltaX / divide;
  28. double dy = deltaY / divide;
  29. double myX = double(x);
  30. double myY = double(y);
  31. if (newPlace == THUAI6::PlaceType::Grass && myPlace == THUAI6::PlaceType::Grass) // 都在草丛内,要另作判断
  32. for (int i = 0; i < divide; i++)
  33. {
  34. myX += dx;
  35. myY += dy;
  36. if (map[GridToCell(myX)][GridToCell(myY)] != THUAI6::PlaceType::Grass)
  37. return false;
  38. }
  39. else // 不在草丛内,只需要没有墙即可
  40. for (int i = 0; i < divide; i++)
  41. {
  42. myX += dx;
  43. myY += dy;
  44. if (map[GridToCell(myX)][GridToCell(myY)] == THUAI6::PlaceType::Wall)
  45. return false;
  46. }
  47. return true;
  48. }
  49. else
  50. return false;
  51. }
  52. } // namespace AssistFunction
  53. // 辅助函数,用于将proto信息转换为THUAI6信息
  54. namespace Proto2THUAI6
  55. {
  56. // 用于将Protobuf中的枚举转换为THUAI6的枚举
  57. inline std::map<protobuf::PlaceType, THUAI6::PlaceType> placeTypeDict{
  58. {protobuf::PlaceType::NULL_PLACE_TYPE, THUAI6::PlaceType::NullPlaceType},
  59. {protobuf::PlaceType::LAND, THUAI6::PlaceType::Land},
  60. {protobuf::PlaceType::WALL, THUAI6::PlaceType::Wall},
  61. {protobuf::PlaceType::GRASS, THUAI6::PlaceType::Grass},
  62. {protobuf::PlaceType::CLASSROOM, THUAI6::PlaceType::ClassRoom},
  63. {protobuf::PlaceType::GATE, THUAI6::PlaceType::Gate},
  64. {protobuf::PlaceType::HIDDEN_GATE, THUAI6::PlaceType::HiddenGate},
  65. {protobuf::PlaceType::WINDOW, THUAI6::PlaceType::Window},
  66. {protobuf::PlaceType::DOOR, THUAI6::PlaceType::Door},
  67. };
  68. inline std::map<protobuf::ShapeType, THUAI6::ShapeType> shapeTypeDict{
  69. {protobuf::ShapeType::NULL_SHAPE_TYPE, THUAI6::ShapeType::NullShapeType},
  70. {protobuf::ShapeType::CIRCLE, THUAI6::ShapeType::Circle},
  71. {protobuf::ShapeType::SQUARE, THUAI6::ShapeType::Square},
  72. };
  73. inline std::map<protobuf::PropType, THUAI6::PropType> propTypeDict{
  74. {protobuf::PropType::NULL_PROP_TYPE, THUAI6::PropType::NullPropType},
  75. {protobuf::PropType::PTYPE1, THUAI6::PropType::PropType1},
  76. {protobuf::PropType::PTYPE2, THUAI6::PropType::PropType2},
  77. {protobuf::PropType::PTYPE3, THUAI6::PropType::PropType3},
  78. {protobuf::PropType::PTYPE4, THUAI6::PropType::PropType4},
  79. };
  80. inline std::map<protobuf::PlayerType, THUAI6::PlayerType> playerTypeDict{
  81. {protobuf::PlayerType::NULL_PLAYER_TYPE, THUAI6::PlayerType::NullPlayerType},
  82. {protobuf::PlayerType::STUDENT_PLAYER, THUAI6::PlayerType::StudentPlayer},
  83. {protobuf::PlayerType::TRICKER_PLAYER, THUAI6::PlayerType::TrickerPlayer},
  84. };
  85. inline std::map<protobuf::StudentType, THUAI6::StudentType> studentTypeDict{
  86. {protobuf::StudentType::NULL_STUDENT_TYPE, THUAI6::StudentType::NullStudentType},
  87. {protobuf::StudentType::STUDENTTYPE1, THUAI6::StudentType::StudentType1},
  88. {protobuf::StudentType::STUDENTTYPE2, THUAI6::StudentType::StudentType2},
  89. {protobuf::StudentType::STUDENTTYPE3, THUAI6::StudentType::StudentType3},
  90. {protobuf::StudentType::STUDENTTYPE4, THUAI6::StudentType::StudentType4},
  91. };
  92. inline std::map<protobuf::TrickerType, THUAI6::TrickerType> trickerTypeDict{
  93. {protobuf::TrickerType::NULL_TRICKER_TYPE, THUAI6::TrickerType::NullTrickerType},
  94. {protobuf::TrickerType::TRICKERTYPE1, THUAI6::TrickerType::TrickerType1},
  95. {protobuf::TrickerType::TRICKERTYPE2, THUAI6::TrickerType::TrickerType2},
  96. {protobuf::TrickerType::TRICKERTYPE3, THUAI6::TrickerType::TrickerType3},
  97. {protobuf::TrickerType::TRICKERTYPE4, THUAI6::TrickerType::TrickerType4},
  98. };
  99. inline std::map<protobuf::StudentBuffType, THUAI6::StudentBuffType> studentBuffTypeDict{
  100. {protobuf::StudentBuffType::NULL_SBUFF_TYPE, THUAI6::StudentBuffType::NullStudentBuffType},
  101. {protobuf::StudentBuffType::SBUFFTYPE1, THUAI6::StudentBuffType::StudentBuffType1},
  102. {protobuf::StudentBuffType::SBUFFTYPE2, THUAI6::StudentBuffType::StudentBuffType2},
  103. {protobuf::StudentBuffType::SBUFFTYPE3, THUAI6::StudentBuffType::StudentBuffType3},
  104. {protobuf::StudentBuffType::SBUFFTYPE4, THUAI6::StudentBuffType::StudentBuffType4},
  105. };
  106. inline std::map<protobuf::TrickerBuffType, THUAI6::TrickerBuffType> trickerBuffTypeDict{
  107. {protobuf::TrickerBuffType::NULL_TBUFF_TYPE, THUAI6::TrickerBuffType::NullTrickerBuffType},
  108. {protobuf::TrickerBuffType::TBUFFTYPE1, THUAI6::TrickerBuffType::TrickerBuffType1},
  109. {protobuf::TrickerBuffType::TBUFFTYPE2, THUAI6::TrickerBuffType::TrickerBuffType2},
  110. {protobuf::TrickerBuffType::TBUFFTYPE3, THUAI6::TrickerBuffType::TrickerBuffType3},
  111. {protobuf::TrickerBuffType::TBUFFTYPE4, THUAI6::TrickerBuffType::TrickerBuffType4},
  112. };
  113. inline std::map<protobuf::PlayerState, THUAI6::PlayerState> playerStateDict{
  114. {protobuf::PlayerState::NULL_STATUS, THUAI6::PlayerState::NullState},
  115. {protobuf::PlayerState::IDLE, THUAI6::PlayerState::Idle},
  116. {protobuf::PlayerState::LEARNING, THUAI6::PlayerState::Learning},
  117. {protobuf::PlayerState::ADDICTED, THUAI6::PlayerState::Addicted},
  118. {protobuf::PlayerState::QUIT, THUAI6::PlayerState::Quit},
  119. {protobuf::PlayerState::GRADUATED, THUAI6::PlayerState::Graduated},
  120. {protobuf::PlayerState::RESCUED, THUAI6::PlayerState::Rescued},
  121. {protobuf::PlayerState::TREATED, THUAI6::PlayerState::Treated},
  122. {protobuf::PlayerState::STUNNED, THUAI6::PlayerState::Stunned},
  123. {protobuf::PlayerState::RESCUING, THUAI6::PlayerState::Rescuing},
  124. {protobuf::PlayerState::TREATING, THUAI6::PlayerState::Treating},
  125. };
  126. inline std::map<protobuf::GameState, THUAI6::GameState> gameStateDict{
  127. {protobuf::GameState::NULL_GAME_STATE, THUAI6::GameState::NullGameState},
  128. {protobuf::GameState::GAME_START, THUAI6::GameState::GameStart},
  129. {protobuf::GameState::GAME_RUNNING, THUAI6::GameState::GameRunning},
  130. {protobuf::GameState::GAME_END, THUAI6::GameState::GameEnd},
  131. };
  132. inline std::map<protobuf::BulletType, THUAI6::BulletType> bulletTypeDict{
  133. {protobuf::BulletType::NULL_BULLET_TYPE, THUAI6::BulletType::NullBulletType},
  134. {protobuf::BulletType::COMMON_BULLET, THUAI6::BulletType::CommonBullet},
  135. {protobuf::BulletType::FAST_BULLET, THUAI6::BulletType::FastBullet},
  136. {protobuf::BulletType::LINE_BULLET, THUAI6::BulletType::LineBullet},
  137. {protobuf::BulletType::ORDINARY_BULLET, THUAI6::BulletType::OrdinaryBullet},
  138. {protobuf::BulletType::ATOM_BOMB, THUAI6::BulletType::AtomBomb},
  139. };
  140. inline std::map<protobuf::MessageOfObj::MessageOfObjCase, THUAI6::MessageOfObj> messageOfObjDict{
  141. {protobuf::MessageOfObj::kStudentMessage, THUAI6::MessageOfObj::StudentMessage},
  142. {protobuf::MessageOfObj::kTrickerMessage, THUAI6::MessageOfObj::TrickerMessage},
  143. {protobuf::MessageOfObj::kPropMessage, THUAI6::MessageOfObj::PropMessage},
  144. {protobuf::MessageOfObj::kBulletMessage, THUAI6::MessageOfObj::BulletMessage},
  145. {protobuf::MessageOfObj::kBombedBulletMessage, THUAI6::MessageOfObj::BombedBulletMessage},
  146. {protobuf::MessageOfObj::MESSAGE_OF_OBJ_NOT_SET, THUAI6::MessageOfObj::NullMessageOfObj},
  147. };
  148. inline std::map<protobuf::MessageOfMapObj::MessageOfMapObjCase, THUAI6::MessageOfMapObj> messageOfMapObjDict{
  149. {protobuf::MessageOfMapObj::MessageOfMapObjCase::kClassroomMessage, THUAI6::MessageOfMapObj::ClassroomMessage},
  150. {protobuf::MessageOfMapObj::MessageOfMapObjCase::kDoorMessage, THUAI6::MessageOfMapObj::DoorMessage},
  151. {protobuf::MessageOfMapObj::MessageOfMapObjCase::kGateMessage, THUAI6::MessageOfMapObj::GateMessage},
  152. {protobuf::MessageOfMapObj::MessageOfMapObjCase::kChestMessage, THUAI6::MessageOfMapObj::ChestMessage},
  153. };
  154. // 用于将Protobuf中的类转换为THUAI6的类
  155. inline std::shared_ptr<THUAI6::Tricker> Protobuf2THUAI6Tricker(const protobuf::MessageOfTricker& trickerMsg)
  156. {
  157. auto tricker = std::make_shared<THUAI6::Tricker>();
  158. tricker->x = trickerMsg.x();
  159. tricker->y = trickerMsg.y();
  160. tricker->speed = trickerMsg.speed();
  161. tricker->damage = trickerMsg.damage();
  162. tricker->timeUntilSkillAvailable = trickerMsg.time_until_skill_available();
  163. tricker->place = placeTypeDict[trickerMsg.place()];
  164. tricker->playerState = playerStateDict[trickerMsg.player_state()];
  165. for (int i = 0; i < trickerMsg.prop().size(); i++)
  166. {
  167. tricker->props.push_back(propTypeDict[trickerMsg.prop(i)]);
  168. }
  169. tricker->trickerType = trickerTypeDict[trickerMsg.tricker_type()];
  170. tricker->guid = trickerMsg.guid();
  171. tricker->playerID = trickerMsg.player_id();
  172. tricker->viewRange = trickerMsg.view_range();
  173. tricker->radius = trickerMsg.radius();
  174. tricker->buff.clear();
  175. for (int i = 0; i < trickerMsg.buff().size(); i++)
  176. {
  177. tricker->buff.push_back(trickerBuffTypeDict[trickerMsg.buff(i)]);
  178. }
  179. return tricker;
  180. }
  181. inline std::shared_ptr<THUAI6::Student> Protobuf2THUAI6Student(const protobuf::MessageOfStudent& studentMsg)
  182. {
  183. auto student = std::make_shared<THUAI6::Student>();
  184. student->x = studentMsg.x();
  185. student->y = studentMsg.y();
  186. student->speed = studentMsg.speed();
  187. student->viewRange = studentMsg.view_range();
  188. student->playerID = studentMsg.player_id();
  189. student->guid = studentMsg.guid();
  190. student->radius = studentMsg.radius();
  191. student->timeUntilSkillAvailable = studentMsg.time_until_skill_available();
  192. student->damage = studentMsg.damage();
  193. student->playerType = THUAI6::PlayerType::StudentPlayer;
  194. for (int i = 0; i < studentMsg.prop().size(); i++)
  195. {
  196. student->props.push_back(propTypeDict[studentMsg.prop(i)]);
  197. }
  198. student->place = placeTypeDict[studentMsg.place()];
  199. student->playerState = playerStateDict[studentMsg.state()];
  200. student->determination = studentMsg.determination();
  201. student->failNum = studentMsg.fail_num();
  202. student->failTime = studentMsg.fail_time();
  203. student->emoTime = studentMsg.emo_time();
  204. student->studentType = studentTypeDict[studentMsg.student_type()];
  205. student->buff.clear();
  206. for (int i = 0; i < studentMsg.buff_size(); i++)
  207. {
  208. student->buff.push_back(studentBuffTypeDict[studentMsg.buff(i)]);
  209. }
  210. return student;
  211. }
  212. inline std::shared_ptr<THUAI6::Prop> Protobuf2THUAI6Prop(const protobuf::MessageOfProp& propMsg)
  213. {
  214. auto prop = std::make_shared<THUAI6::Prop>();
  215. prop->x = propMsg.x();
  216. prop->y = propMsg.y();
  217. prop->type = propTypeDict[propMsg.type()];
  218. prop->place = placeTypeDict[propMsg.place()];
  219. prop->guid = propMsg.guid();
  220. prop->size = propMsg.size();
  221. prop->facingDirection = propMsg.facing_direction();
  222. prop->isMoving = propMsg.is_moving();
  223. return prop;
  224. }
  225. inline std::shared_ptr<THUAI6::GameMap> Protobuf2THUAI6Map(const protobuf::MessageOfMap& mapMsg)
  226. {
  227. auto map = std::make_shared<THUAI6::GameMap>();
  228. for (int i = 0; i < mapMsg.row_size(); i++)
  229. {
  230. std::vector<THUAI6::PlaceType> row;
  231. for (int j = 0; j < mapMsg.row(i).col_size(); j++)
  232. {
  233. row.push_back(placeTypeDict[mapMsg.row(i).col(j)]);
  234. }
  235. map->gameMap.push_back(row);
  236. }
  237. for (const auto& item : mapMsg.map_obj_message())
  238. switch (messageOfMapObjDict[item.message_of_map_obj_case()])
  239. {
  240. case THUAI6::MessageOfMapObj::ClassroomMessage:
  241. map->classRoomState.emplace(std::make_pair(item.classroom_message().x(), item.classroom_message().y()), item.classroom_message().progress());
  242. break;
  243. case THUAI6::MessageOfMapObj::DoorMessage:
  244. map->doorState.emplace(std::make_pair(item.door_message().x(), item.door_message().y()), item.door_message().is_open());
  245. break;
  246. case THUAI6::MessageOfMapObj::GateMessage:
  247. map->gateState.emplace(std::make_pair(item.gate_message().x(), item.gate_message().y()), item.gate_message().progress());
  248. break;
  249. default:
  250. break;
  251. }
  252. return map;
  253. }
  254. inline std::shared_ptr<THUAI6::GameInfo> Protobuf2THUAI6GameInfo(const protobuf::MessageOfAll& allMsg)
  255. {
  256. auto gameInfo = std::make_shared<THUAI6::GameInfo>();
  257. gameInfo->gameTime = allMsg.game_time();
  258. gameInfo->subjectLeft = allMsg.subject_left();
  259. gameInfo->studentGraduated = allMsg.student_graduated();
  260. gameInfo->studentQuited = allMsg.student_quited();
  261. gameInfo->studentScore = allMsg.student_score();
  262. gameInfo->trickerScore = allMsg.tricker_score();
  263. gameInfo->gateOpened = allMsg.gate_opened();
  264. gameInfo->hiddenGateOpened = allMsg.hidden_gate_opened();
  265. gameInfo->hiddenGateRefreshed = allMsg.hidden_gate_refreshed();
  266. return gameInfo;
  267. }
  268. inline std::shared_ptr<THUAI6::Bullet> Protobuf2THUAI6Bullet(const protobuf::MessageOfBullet& bulletMsg)
  269. {
  270. auto bullet = std::make_shared<THUAI6::Bullet>();
  271. bullet->bulletType = bulletTypeDict[bulletMsg.type()];
  272. bullet->x = bulletMsg.x();
  273. bullet->y = bulletMsg.y();
  274. bullet->facingDirection = bulletMsg.facing_direction();
  275. bullet->guid = bulletMsg.guid();
  276. bullet->team = playerTypeDict[bulletMsg.team()];
  277. bullet->place = placeTypeDict[bulletMsg.place()];
  278. bullet->bombRange = bulletMsg.bomb_range();
  279. return bullet;
  280. }
  281. inline std::shared_ptr<THUAI6::BombedBullet> Protobuf2THUAI6BombedBullet(const protobuf::MessageOfBombedBullet& bombedBulletMsg)
  282. {
  283. auto bombedBullet = std::make_shared<THUAI6::BombedBullet>();
  284. bombedBullet->bulletType = bulletTypeDict[bombedBulletMsg.type()];
  285. bombedBullet->x = bombedBulletMsg.x();
  286. bombedBullet->y = bombedBulletMsg.y();
  287. bombedBullet->facingDirection = bombedBulletMsg.facing_direction();
  288. bombedBullet->mappingID = bombedBulletMsg.mapping_id();
  289. bombedBullet->bombRange = bombedBulletMsg.bomb_range();
  290. return bombedBullet;
  291. }
  292. } // namespace Proto2THUAI6
  293. namespace THUAI62Proto
  294. {
  295. // 用于将THUAI6的枚举转换为Protobuf的枚举
  296. inline std::map<THUAI6::PlaceType, protobuf::PlaceType> placeTypeDict{
  297. {THUAI6::PlaceType::NullPlaceType, protobuf::PlaceType::NULL_PLACE_TYPE},
  298. {THUAI6::PlaceType::Land, protobuf::PlaceType::LAND},
  299. {THUAI6::PlaceType::Wall, protobuf::PlaceType::WALL},
  300. {THUAI6::PlaceType::Grass, protobuf::PlaceType::GRASS},
  301. {THUAI6::PlaceType::ClassRoom, protobuf::PlaceType::CLASSROOM},
  302. {THUAI6::PlaceType::Gate, protobuf::PlaceType::GATE},
  303. {THUAI6::PlaceType::HiddenGate, protobuf::PlaceType::HIDDEN_GATE},
  304. {THUAI6::PlaceType::Door, protobuf::PlaceType::DOOR},
  305. {THUAI6::PlaceType::Window, protobuf::PlaceType::WINDOW},
  306. };
  307. inline std::map<THUAI6::ShapeType, protobuf::ShapeType> shapeTypeDict{
  308. {THUAI6::ShapeType::NullShapeType, protobuf::ShapeType::NULL_SHAPE_TYPE},
  309. {THUAI6::ShapeType::Circle, protobuf::ShapeType::CIRCLE},
  310. {THUAI6::ShapeType::Square, protobuf::ShapeType::SQUARE},
  311. };
  312. inline std::map<THUAI6::PropType, protobuf::PropType> propTypeDict{
  313. {THUAI6::PropType::NullPropType, protobuf::PropType::NULL_PROP_TYPE},
  314. {THUAI6::PropType::PropType1, protobuf::PropType::PTYPE1},
  315. {THUAI6::PropType::PropType2, protobuf::PropType::PTYPE2},
  316. {THUAI6::PropType::PropType3, protobuf::PropType::PTYPE3},
  317. {THUAI6::PropType::PropType4, protobuf::PropType::PTYPE4},
  318. };
  319. inline std::map<THUAI6::PlayerType, protobuf::PlayerType> playerTypeDict{
  320. {THUAI6::PlayerType::NullPlayerType, protobuf::PlayerType::NULL_PLAYER_TYPE},
  321. {THUAI6::PlayerType::StudentPlayer, protobuf::PlayerType::STUDENT_PLAYER},
  322. {THUAI6::PlayerType::TrickerPlayer, protobuf::PlayerType::TRICKER_PLAYER},
  323. };
  324. inline std::map<THUAI6::StudentType, protobuf::StudentType> studentTypeDict{
  325. {THUAI6::StudentType::NullStudentType, protobuf::StudentType::NULL_STUDENT_TYPE},
  326. {THUAI6::StudentType::StudentType1, protobuf::StudentType::STUDENTTYPE1},
  327. {THUAI6::StudentType::StudentType2, protobuf::StudentType::STUDENTTYPE2},
  328. {THUAI6::StudentType::StudentType3, protobuf::StudentType::STUDENTTYPE3},
  329. {THUAI6::StudentType::StudentType4, protobuf::StudentType::STUDENTTYPE4},
  330. };
  331. inline std::map<THUAI6::StudentBuffType, protobuf::StudentBuffType> studentBuffTypeDict{
  332. {THUAI6::StudentBuffType::NullStudentBuffType, protobuf::StudentBuffType::NULL_SBUFF_TYPE},
  333. {THUAI6::StudentBuffType::StudentBuffType1, protobuf::StudentBuffType::SBUFFTYPE1},
  334. {THUAI6::StudentBuffType::StudentBuffType2, protobuf::StudentBuffType::SBUFFTYPE2},
  335. {THUAI6::StudentBuffType::StudentBuffType3, protobuf::StudentBuffType::SBUFFTYPE3},
  336. {THUAI6::StudentBuffType::StudentBuffType4, protobuf::StudentBuffType::SBUFFTYPE4},
  337. };
  338. inline std::map<THUAI6::TrickerType, protobuf::TrickerType> trickerTypeDict{
  339. {THUAI6::TrickerType::NullTrickerType, protobuf::TrickerType::NULL_TRICKER_TYPE},
  340. {THUAI6::TrickerType::TrickerType1, protobuf::TrickerType::TRICKERTYPE1},
  341. {THUAI6::TrickerType::TrickerType2, protobuf::TrickerType::TRICKERTYPE2},
  342. {THUAI6::TrickerType::TrickerType3, protobuf::TrickerType::TRICKERTYPE3},
  343. {THUAI6::TrickerType::TrickerType4, protobuf::TrickerType::TRICKERTYPE4},
  344. };
  345. inline std::map<THUAI6::TrickerBuffType, protobuf::TrickerBuffType> trickerBuffTypeDict{
  346. {THUAI6::TrickerBuffType::NullTrickerBuffType, protobuf::TrickerBuffType::NULL_TBUFF_TYPE},
  347. {THUAI6::TrickerBuffType::TrickerBuffType1, protobuf::TrickerBuffType::TBUFFTYPE1},
  348. {THUAI6::TrickerBuffType::TrickerBuffType2, protobuf::TrickerBuffType::TBUFFTYPE2},
  349. {THUAI6::TrickerBuffType::TrickerBuffType3, protobuf::TrickerBuffType::TBUFFTYPE3},
  350. {THUAI6::TrickerBuffType::TrickerBuffType4, protobuf::TrickerBuffType::TBUFFTYPE4},
  351. };
  352. // 用于将THUAI6的类转换为Protobuf的消息
  353. inline protobuf::PlayerMsg THUAI62ProtobufPlayer(int64_t playerID, THUAI6::PlayerType playerType, THUAI6::StudentType studentType, THUAI6::TrickerType trickerType)
  354. {
  355. protobuf::PlayerMsg playerMsg;
  356. playerMsg.set_player_id(playerID);
  357. playerMsg.set_player_type(playerTypeDict[playerType]);
  358. if (playerType == THUAI6::PlayerType::StudentPlayer)
  359. {
  360. playerMsg.set_student_type(studentTypeDict[studentType]);
  361. }
  362. else if (playerType == THUAI6::PlayerType::TrickerPlayer)
  363. {
  364. playerMsg.set_tricker_type(trickerTypeDict[trickerType]);
  365. }
  366. return playerMsg;
  367. }
  368. inline protobuf::IDMsg THUAI62ProtobufID(int playerID, THUAI6::PlayerType playerType)
  369. {
  370. protobuf::IDMsg idMsg;
  371. idMsg.set_player_id(playerID);
  372. idMsg.set_player_type(playerTypeDict[playerType]);
  373. return idMsg;
  374. }
  375. inline protobuf::MoveMsg THUAI62ProtobufMove(int64_t time, double angle, int64_t id, THUAI6::PlayerType playerType)
  376. {
  377. protobuf::MoveMsg moveMsg;
  378. moveMsg.set_time_in_milliseconds(time);
  379. moveMsg.set_angle(angle);
  380. moveMsg.set_player_id(id);
  381. moveMsg.set_player_type(playerTypeDict[playerType]);
  382. return moveMsg;
  383. }
  384. inline protobuf::PropMsg THUAI62ProtobufProp(THUAI6::PropType prop, int64_t id, THUAI6::PlayerType playerType)
  385. {
  386. protobuf::PropMsg pickMsg;
  387. pickMsg.set_prop_type(propTypeDict[prop]);
  388. pickMsg.set_player_id(id);
  389. pickMsg.set_player_type(playerTypeDict[playerType]);
  390. return pickMsg;
  391. }
  392. inline protobuf::SendMsg THUAI62ProtobufSend(std::string msg, int64_t toID, int64_t id, THUAI6::PlayerType playerType, THUAI6::PlayerType toType)
  393. {
  394. protobuf::SendMsg sendMsg;
  395. sendMsg.set_message(msg);
  396. sendMsg.set_to_player_id(toID);
  397. sendMsg.set_player_id(id);
  398. sendMsg.set_player_type(playerTypeDict[playerType]);
  399. sendMsg.set_to_player_type(playerTypeDict[toType]);
  400. return sendMsg;
  401. }
  402. inline protobuf::AttackMsg THUAI62ProtobufAttack(double angle, int64_t id, THUAI6::PlayerType playerType)
  403. {
  404. protobuf::AttackMsg attackMsg;
  405. attackMsg.set_angle(angle);
  406. attackMsg.set_player_id(id);
  407. attackMsg.set_player_type(playerTypeDict[playerType]);
  408. return attackMsg;
  409. }
  410. inline protobuf::SkillMsg THUAI62ProtobufSkill(int32_t skillID, int64_t id, THUAI6::PlayerType playerType)
  411. {
  412. protobuf::SkillMsg skillMsg;
  413. skillMsg.set_skill_id(skillID);
  414. skillMsg.set_player_id(id);
  415. skillMsg.set_player_type(playerTypeDict[playerType]);
  416. return skillMsg;
  417. }
  418. } // namespace THUAI62Proto
  419. namespace Time
  420. {
  421. inline double TimeSinceStart(const std::chrono::system_clock::time_point& sp)
  422. {
  423. std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
  424. std::chrono::duration<double, std::milli> time_span = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(tp - sp);
  425. return time_span.count();
  426. }
  427. } // namespace Time
  428. #endif