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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. };
  66. inline std::map<protobuf::ShapeType, THUAI6::ShapeType> shapeTypeDict{
  67. {protobuf::ShapeType::NULL_SHAPE_TYPE, THUAI6::ShapeType::NullShapeType},
  68. {protobuf::ShapeType::CIRCLE, THUAI6::ShapeType::Circle},
  69. {protobuf::ShapeType::SQUARE, THUAI6::ShapeType::Square},
  70. };
  71. inline std::map<protobuf::PropType, THUAI6::PropType> propTypeDict{
  72. {protobuf::PropType::NULL_PROP_TYPE, THUAI6::PropType::NullPropType},
  73. {protobuf::PropType::PTYPE1, THUAI6::PropType::PropType1},
  74. {protobuf::PropType::PTYPE2, THUAI6::PropType::PropType2},
  75. {protobuf::PropType::PTYPE3, THUAI6::PropType::PropType3},
  76. {protobuf::PropType::PTYPE4, THUAI6::PropType::PropType4},
  77. };
  78. inline std::map<protobuf::PlayerType, THUAI6::PlayerType> playerTypeDict{
  79. {protobuf::PlayerType::NULL_PLAYER_TYPE, THUAI6::PlayerType::NullPlayerType},
  80. {protobuf::PlayerType::STUDENT_PLAYER, THUAI6::PlayerType::StudentPlayer},
  81. {protobuf::PlayerType::TRICKER_PLAYER, THUAI6::PlayerType::TrickerPlayer},
  82. };
  83. inline std::map<protobuf::StudentType, THUAI6::StudentType> studentTypeDict{
  84. {protobuf::StudentType::NULL_STUDENT_TYPE, THUAI6::StudentType::NullStudentType},
  85. {protobuf::StudentType::STUDENTTYPE1, THUAI6::StudentType::StudentType1},
  86. {protobuf::StudentType::STUDENTTYPE2, THUAI6::StudentType::StudentType2},
  87. {protobuf::StudentType::STUDENTTYPE3, THUAI6::StudentType::StudentType3},
  88. {protobuf::StudentType::STUDENTTYPE4, THUAI6::StudentType::StudentType4},
  89. };
  90. inline std::map<protobuf::TrickerType, THUAI6::TrickerType> trickerTypeDict{
  91. {protobuf::TrickerType::NULL_TRICKER_TYPE, THUAI6::TrickerType::NullTrickerType},
  92. {protobuf::TrickerType::TRICKERTYPE1, THUAI6::TrickerType::TrickerType1},
  93. {protobuf::TrickerType::TRICKERTYPE2, THUAI6::TrickerType::TrickerType2},
  94. {protobuf::TrickerType::TRICKERTYPE3, THUAI6::TrickerType::TrickerType3},
  95. {protobuf::TrickerType::TRICKERTYPE4, THUAI6::TrickerType::TrickerType4},
  96. };
  97. inline std::map<protobuf::StudentBuffType, THUAI6::StudentBuffType> studentBuffTypeDict{
  98. {protobuf::StudentBuffType::NULL_SBUFF_TYPE, THUAI6::StudentBuffType::NullStudentBuffType},
  99. {protobuf::StudentBuffType::SBUFFTYPE1, THUAI6::StudentBuffType::StudentBuffType1},
  100. {protobuf::StudentBuffType::SBUFFTYPE2, THUAI6::StudentBuffType::StudentBuffType2},
  101. {protobuf::StudentBuffType::SBUFFTYPE3, THUAI6::StudentBuffType::StudentBuffType3},
  102. {protobuf::StudentBuffType::SBUFFTYPE4, THUAI6::StudentBuffType::StudentBuffType4},
  103. };
  104. inline std::map<protobuf::TrickerBuffType, THUAI6::TrickerBuffType> trickerBuffTypeDict{
  105. {protobuf::TrickerBuffType::NULL_TBUFF_TYPE, THUAI6::TrickerBuffType::NullTrickerBuffType},
  106. {protobuf::TrickerBuffType::TBUFFTYPE1, THUAI6::TrickerBuffType::TrickerBuffType1},
  107. {protobuf::TrickerBuffType::TBUFFTYPE2, THUAI6::TrickerBuffType::TrickerBuffType2},
  108. {protobuf::TrickerBuffType::TBUFFTYPE3, THUAI6::TrickerBuffType::TrickerBuffType3},
  109. {protobuf::TrickerBuffType::TBUFFTYPE4, THUAI6::TrickerBuffType::TrickerBuffType4},
  110. };
  111. inline std::map<protobuf::StudentState, THUAI6::StudentState> studentStateDict{
  112. {protobuf::StudentState::NULL_STATUS, THUAI6::StudentState::NullStudentState},
  113. {protobuf::StudentState::IDLE, THUAI6::StudentState::Idle},
  114. {protobuf::StudentState::LEARNING, THUAI6::StudentState::Learning},
  115. {protobuf::StudentState::ADDICTED, THUAI6::StudentState::Addicted},
  116. {protobuf::StudentState::QUIT, THUAI6::StudentState::Quit},
  117. {protobuf::StudentState::GRADUATED, THUAI6::StudentState::Graduated},
  118. {protobuf::StudentState::RESCUED, THUAI6::StudentState::Rescued},
  119. {protobuf::StudentState::TREATED, THUAI6::StudentState::Treated},
  120. {protobuf::StudentState::STUNNED, THUAI6::StudentState::Stunned},
  121. {protobuf::StudentState::RESCUING, THUAI6::StudentState::Rescuing},
  122. {protobuf::StudentState::TREATING, THUAI6::StudentState::Treating},
  123. };
  124. inline std::map<protobuf::GameState, THUAI6::GameState> gameStateDict{
  125. {protobuf::GameState::NULL_GAME_STATE, THUAI6::GameState::NullGameState},
  126. {protobuf::GameState::GAME_START, THUAI6::GameState::GameStart},
  127. {protobuf::GameState::GAME_RUNNING, THUAI6::GameState::GameRunning},
  128. {protobuf::GameState::GAME_END, THUAI6::GameState::GameEnd},
  129. };
  130. inline std::map<protobuf::BulletType, THUAI6::BulletType> bulletTypeDict{
  131. {protobuf::BulletType::NULL_BULLET_TYPE, THUAI6::BulletType::NullBulletType},
  132. {protobuf::BulletType::COMMON_BULLET, THUAI6::BulletType::CommonBullet},
  133. {protobuf::BulletType::FAST_BULLET, THUAI6::BulletType::FastBullet},
  134. {protobuf::BulletType::LINE_BULLET, THUAI6::BulletType::LineBullet},
  135. {protobuf::BulletType::ORDINARY_BULLET, THUAI6::BulletType::OrdinaryBullet},
  136. {protobuf::BulletType::ATOM_BOMB, THUAI6::BulletType::AtomBomb},
  137. };
  138. // 用于将Protobuf中的类转换为THUAI6的类
  139. inline std::shared_ptr<THUAI6::Tricker> Protobuf2THUAI6Tricker(const protobuf::MessageOfTricker& trickerMsg)
  140. {
  141. auto tricker = std::make_shared<THUAI6::Tricker>();
  142. tricker->x = trickerMsg.x();
  143. tricker->y = trickerMsg.y();
  144. tricker->speed = trickerMsg.speed();
  145. tricker->damage = trickerMsg.damage();
  146. tricker->timeUntilSkillAvailable = trickerMsg.time_until_skill_available();
  147. tricker->place = placeTypeDict[trickerMsg.place()];
  148. tricker->prop = propTypeDict[trickerMsg.prop()];
  149. tricker->trickerType = trickerTypeDict[trickerMsg.tricker_type()];
  150. tricker->guid = trickerMsg.guid();
  151. tricker->movable = trickerMsg.movable();
  152. tricker->playerID = trickerMsg.player_id();
  153. tricker->viewRange = trickerMsg.view_range();
  154. tricker->radius = trickerMsg.radius();
  155. tricker->buff.clear();
  156. for (int i = 0; i < trickerMsg.buff().size(); i++)
  157. {
  158. tricker->buff.push_back(trickerBuffTypeDict[trickerMsg.buff(i)]);
  159. }
  160. return tricker;
  161. }
  162. inline std::shared_ptr<THUAI6::Student> Protobuf2THUAI6Student(const protobuf::MessageOfStudent& studentMsg)
  163. {
  164. auto student = std::make_shared<THUAI6::Student>();
  165. student->x = studentMsg.x();
  166. student->y = studentMsg.y();
  167. student->speed = studentMsg.speed();
  168. student->viewRange = studentMsg.view_range();
  169. student->playerID = studentMsg.player_id();
  170. student->guid = studentMsg.guid();
  171. student->radius = studentMsg.radius();
  172. student->timeUntilSkillAvailable = studentMsg.time_until_skill_available();
  173. student->damage = studentMsg.damage();
  174. student->playerType = THUAI6::PlayerType::StudentPlayer;
  175. student->prop = propTypeDict[studentMsg.prop()];
  176. student->place = placeTypeDict[studentMsg.place()];
  177. student->state = studentStateDict[studentMsg.state()];
  178. student->determination = studentMsg.determination();
  179. student->failNum = studentMsg.fail_num();
  180. student->failTime = studentMsg.fail_time();
  181. student->emoTime = studentMsg.emo_time();
  182. student->studentType = studentTypeDict[studentMsg.student_type()];
  183. student->buff.clear();
  184. for (int i = 0; i < studentMsg.buff_size(); i++)
  185. {
  186. student->buff.push_back(studentBuffTypeDict[studentMsg.buff(i)]);
  187. }
  188. return student;
  189. }
  190. inline std::shared_ptr<THUAI6::Prop> Protobuf2THUAI6Prop(const protobuf::MessageOfProp& propMsg)
  191. {
  192. auto prop = std::make_shared<THUAI6::Prop>();
  193. prop->x = propMsg.x();
  194. prop->y = propMsg.y();
  195. prop->type = propTypeDict[propMsg.type()];
  196. prop->place = placeTypeDict[propMsg.place()];
  197. prop->guid = propMsg.guid();
  198. prop->size = propMsg.size();
  199. prop->facingDirection = propMsg.facing_direction();
  200. prop->isMoving = propMsg.is_moving();
  201. return prop;
  202. }
  203. inline std::vector<std::vector<THUAI6::PlaceType>> Protobuf2THUAI6Map(const protobuf::MessageOfMap& mapMsg)
  204. {
  205. std::vector<std::vector<THUAI6::PlaceType>> map;
  206. for (int i = 0; i < mapMsg.row_size(); i++)
  207. {
  208. std::vector<THUAI6::PlaceType> row;
  209. for (int j = 0; j < mapMsg.row(i).col_size(); j++)
  210. {
  211. row.push_back(placeTypeDict[mapMsg.row(i).col(j)]);
  212. }
  213. map.push_back(row);
  214. }
  215. return map;
  216. }
  217. inline std::shared_ptr<THUAI6::Bullet> Protobuf2THUAI6Bullet(const protobuf::MessageOfBullet& bulletMsg)
  218. {
  219. auto bullet = std::make_shared<THUAI6::Bullet>();
  220. bullet->bulletType = bulletTypeDict[bulletMsg.type()];
  221. bullet->x = bulletMsg.x();
  222. bullet->y = bulletMsg.y();
  223. bullet->facingDirection = bulletMsg.facing_direction();
  224. bullet->guid = bulletMsg.guid();
  225. bullet->team = playerTypeDict[bulletMsg.team()];
  226. bullet->place = placeTypeDict[bulletMsg.place()];
  227. bullet->bombRange = bulletMsg.bomb_range();
  228. return bullet;
  229. }
  230. inline std::shared_ptr<THUAI6::BombedBullet> Protobuf2THUAI6BombedBullet(const protobuf::MessageOfBombedBullet& bombedBulletMsg)
  231. {
  232. auto bombedBullet = std::make_shared<THUAI6::BombedBullet>();
  233. bombedBullet->bulletType = bulletTypeDict[bombedBulletMsg.type()];
  234. bombedBullet->x = bombedBulletMsg.x();
  235. bombedBullet->y = bombedBulletMsg.y();
  236. bombedBullet->facingDirection = bombedBulletMsg.facing_direction();
  237. bombedBullet->mappingID = bombedBulletMsg.mapping_id();
  238. bombedBullet->bombRange = bombedBulletMsg.bomb_range();
  239. return bombedBullet;
  240. }
  241. } // namespace Proto2THUAI6
  242. namespace THUAI62Proto
  243. {
  244. // 用于将THUAI6的枚举转换为Protobuf的枚举
  245. inline std::map<THUAI6::PlaceType, protobuf::PlaceType> placeTypeDict{
  246. {THUAI6::PlaceType::NullPlaceType, protobuf::PlaceType::NULL_PLACE_TYPE},
  247. {THUAI6::PlaceType::Land, protobuf::PlaceType::LAND},
  248. {THUAI6::PlaceType::Wall, protobuf::PlaceType::WALL},
  249. {THUAI6::PlaceType::Grass, protobuf::PlaceType::GRASS},
  250. {THUAI6::PlaceType::ClassRoom, protobuf::PlaceType::CLASSROOM},
  251. {THUAI6::PlaceType::Gate, protobuf::PlaceType::GATE},
  252. {THUAI6::PlaceType::HiddenGate, protobuf::PlaceType::HIDDEN_GATE},
  253. };
  254. inline std::map<THUAI6::ShapeType, protobuf::ShapeType> shapeTypeDict{
  255. {THUAI6::ShapeType::NullShapeType, protobuf::ShapeType::NULL_SHAPE_TYPE},
  256. {THUAI6::ShapeType::Circle, protobuf::ShapeType::CIRCLE},
  257. {THUAI6::ShapeType::Square, protobuf::ShapeType::SQUARE},
  258. };
  259. inline std::map<THUAI6::PropType, protobuf::PropType> propTypeDict{
  260. {THUAI6::PropType::NullPropType, protobuf::PropType::NULL_PROP_TYPE},
  261. {THUAI6::PropType::PropType1, protobuf::PropType::PTYPE1},
  262. {THUAI6::PropType::PropType2, protobuf::PropType::PTYPE2},
  263. {THUAI6::PropType::PropType3, protobuf::PropType::PTYPE3},
  264. {THUAI6::PropType::PropType4, protobuf::PropType::PTYPE4},
  265. };
  266. inline std::map<THUAI6::PlayerType, protobuf::PlayerType> playerTypeDict{
  267. {THUAI6::PlayerType::NullPlayerType, protobuf::PlayerType::NULL_PLAYER_TYPE},
  268. {THUAI6::PlayerType::StudentPlayer, protobuf::PlayerType::STUDENT_PLAYER},
  269. {THUAI6::PlayerType::TrickerPlayer, protobuf::PlayerType::TRICKER_PLAYER},
  270. };
  271. inline std::map<THUAI6::StudentType, protobuf::StudentType> studentTypeDict{
  272. {THUAI6::StudentType::NullStudentType, protobuf::StudentType::NULL_STUDENT_TYPE},
  273. {THUAI6::StudentType::StudentType1, protobuf::StudentType::STUDENTTYPE1},
  274. {THUAI6::StudentType::StudentType2, protobuf::StudentType::STUDENTTYPE2},
  275. {THUAI6::StudentType::StudentType3, protobuf::StudentType::STUDENTTYPE3},
  276. {THUAI6::StudentType::StudentType4, protobuf::StudentType::STUDENTTYPE4},
  277. };
  278. inline std::map<THUAI6::StudentBuffType, protobuf::StudentBuffType> studentBuffTypeDict{
  279. {THUAI6::StudentBuffType::NullStudentBuffType, protobuf::StudentBuffType::NULL_SBUFF_TYPE},
  280. {THUAI6::StudentBuffType::StudentBuffType1, protobuf::StudentBuffType::SBUFFTYPE1},
  281. {THUAI6::StudentBuffType::StudentBuffType2, protobuf::StudentBuffType::SBUFFTYPE2},
  282. {THUAI6::StudentBuffType::StudentBuffType3, protobuf::StudentBuffType::SBUFFTYPE3},
  283. {THUAI6::StudentBuffType::StudentBuffType4, protobuf::StudentBuffType::SBUFFTYPE4},
  284. };
  285. inline std::map<THUAI6::TrickerType, protobuf::TrickerType> trickerTypeDict{
  286. {THUAI6::TrickerType::NullTrickerType, protobuf::TrickerType::NULL_TRICKER_TYPE},
  287. {THUAI6::TrickerType::TrickerType1, protobuf::TrickerType::TRICKERTYPE1},
  288. {THUAI6::TrickerType::TrickerType2, protobuf::TrickerType::TRICKERTYPE2},
  289. {THUAI6::TrickerType::TrickerType3, protobuf::TrickerType::TRICKERTYPE3},
  290. {THUAI6::TrickerType::TrickerType4, protobuf::TrickerType::TRICKERTYPE4},
  291. };
  292. inline std::map<THUAI6::TrickerBuffType, protobuf::TrickerBuffType> trickerBuffTypeDict{
  293. {THUAI6::TrickerBuffType::NullTrickerBuffType, protobuf::TrickerBuffType::NULL_TBUFF_TYPE},
  294. {THUAI6::TrickerBuffType::TrickerBuffType1, protobuf::TrickerBuffType::TBUFFTYPE1},
  295. {THUAI6::TrickerBuffType::TrickerBuffType2, protobuf::TrickerBuffType::TBUFFTYPE2},
  296. {THUAI6::TrickerBuffType::TrickerBuffType3, protobuf::TrickerBuffType::TBUFFTYPE3},
  297. {THUAI6::TrickerBuffType::TrickerBuffType4, protobuf::TrickerBuffType::TBUFFTYPE4},
  298. };
  299. // 用于将THUAI6的类转换为Protobuf的消息
  300. inline protobuf::PlayerMsg THUAI62ProtobufPlayer(int64_t playerID, THUAI6::PlayerType playerType, THUAI6::StudentType studentType, THUAI6::TrickerType trickerType)
  301. {
  302. protobuf::PlayerMsg playerMsg;
  303. playerMsg.set_player_id(playerID);
  304. playerMsg.set_player_type(playerTypeDict[playerType]);
  305. if (playerType == THUAI6::PlayerType::StudentPlayer)
  306. {
  307. playerMsg.set_student_type(studentTypeDict[studentType]);
  308. }
  309. else if (playerType == THUAI6::PlayerType::TrickerPlayer)
  310. {
  311. playerMsg.set_tricker_type(trickerTypeDict[trickerType]);
  312. }
  313. return playerMsg;
  314. }
  315. inline protobuf::IDMsg THUAI62ProtobufID(int playerID)
  316. {
  317. protobuf::IDMsg idMsg;
  318. idMsg.set_player_id(playerID);
  319. return idMsg;
  320. }
  321. inline protobuf::MoveMsg THUAI62ProtobufMove(int64_t time, double angle, int64_t id)
  322. {
  323. protobuf::MoveMsg moveMsg;
  324. moveMsg.set_time_in_milliseconds(time);
  325. moveMsg.set_angle(angle);
  326. moveMsg.set_player_id(id);
  327. return moveMsg;
  328. }
  329. inline protobuf::PickMsg THUAI62ProtobufPick(THUAI6::PropType prop, int64_t id)
  330. {
  331. protobuf::PickMsg pickMsg;
  332. pickMsg.set_prop_type(propTypeDict[prop]);
  333. pickMsg.set_player_id(id);
  334. return pickMsg;
  335. }
  336. inline protobuf::SendMsg THUAI62ProtobufSend(std::string msg, int64_t toID, int64_t id)
  337. {
  338. protobuf::SendMsg sendMsg;
  339. sendMsg.set_message(msg);
  340. sendMsg.set_to_player_id(toID);
  341. sendMsg.set_player_id(id);
  342. return sendMsg;
  343. }
  344. inline protobuf::TrickMsg THUAI62ProtobufTrick(double angle, int64_t id)
  345. {
  346. protobuf::TrickMsg trickMsg;
  347. trickMsg.set_angle(angle);
  348. trickMsg.set_player_id(id);
  349. return trickMsg;
  350. }
  351. } // namespace THUAI62Proto
  352. namespace Time
  353. {
  354. inline double TimeSinceStart(const std::chrono::system_clock::time_point& sp)
  355. {
  356. std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
  357. std::chrono::duration<double, std::milli> time_span = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(tp - sp);
  358. return time_span.count();
  359. }
  360. } // namespace Time
  361. #endif