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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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, const 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 (distance < viewRange * viewRange)
  23. {
  24. int divide = std::max(std::abs(deltaX), std::abs(deltaY)) / 100;
  25. double dx = deltaX / divide;
  26. double dy = deltaY / divide;
  27. double myX = double(x);
  28. double myY = double(y);
  29. for (int i = 0; i < divide; i++)
  30. {
  31. myX += dx;
  32. myY += dy;
  33. if (map[GridToCell(myX)][GridToCell(myY)] != THUAI6::PlaceType::Land)
  34. return false;
  35. }
  36. return true;
  37. }
  38. else
  39. return false;
  40. }
  41. } // namespace AssistFunction
  42. // 辅助函数,用于将proto信息转换为THUAI6信息
  43. namespace Proto2THUAI6
  44. {
  45. // 用于将Protobuf中的枚举转换为THUAI6的枚举
  46. inline std::map<protobuf::PlaceType, THUAI6::PlaceType> placeTypeDict{
  47. {protobuf::PlaceType::NULL_PLACE_TYPE, THUAI6::PlaceType::NullPlaceType},
  48. {protobuf::PlaceType::LAND, THUAI6::PlaceType::Land},
  49. {protobuf::PlaceType::WALL, THUAI6::PlaceType::Wall},
  50. {protobuf::PlaceType::GRASS, THUAI6::PlaceType::Grass},
  51. {protobuf::PlaceType::CLASSROOM, THUAI6::PlaceType::ClassRoom},
  52. {protobuf::PlaceType::GATE, THUAI6::PlaceType::Gate},
  53. {protobuf::PlaceType::HIDDEN_GATE, THUAI6::PlaceType::HiddenGate},
  54. };
  55. inline std::map<protobuf::ShapeType, THUAI6::ShapeType> shapeTypeDict{
  56. {protobuf::ShapeType::NULL_SHAPE_TYPE, THUAI6::ShapeType::NullShapeType},
  57. {protobuf::ShapeType::CIRCLE, THUAI6::ShapeType::Circle},
  58. {protobuf::ShapeType::SQUARE, THUAI6::ShapeType::Square},
  59. };
  60. inline std::map<protobuf::PropType, THUAI6::PropType> propTypeDict{
  61. {protobuf::PropType::NULL_PROP_TYPE, THUAI6::PropType::NullPropType},
  62. {protobuf::PropType::PTYPE1, THUAI6::PropType::PropType1},
  63. {protobuf::PropType::PTYPE2, THUAI6::PropType::PropType2},
  64. {protobuf::PropType::PTYPE3, THUAI6::PropType::PropType3},
  65. {protobuf::PropType::PTYPE4, THUAI6::PropType::PropType4},
  66. };
  67. inline std::map<protobuf::PlayerType, THUAI6::PlayerType> playerTypeDict{
  68. {protobuf::PlayerType::NULL_PLAYER_TYPE, THUAI6::PlayerType::NullPlayerType},
  69. {protobuf::PlayerType::STUDENT_PLAYER, THUAI6::PlayerType::StudentPlayer},
  70. {protobuf::PlayerType::TRICKER_PLAYER, THUAI6::PlayerType::TrickerPlayer},
  71. };
  72. inline std::map<protobuf::StudentType, THUAI6::StudentType> studentTypeDict{
  73. {protobuf::StudentType::NULL_STUDENT_TYPE, THUAI6::StudentType::NullStudentType},
  74. {protobuf::StudentType::STUDENTTYPE1, THUAI6::StudentType::StudentType1},
  75. {protobuf::StudentType::STUDENTTYPE2, THUAI6::StudentType::StudentType2},
  76. {protobuf::StudentType::STUDENTTYPE3, THUAI6::StudentType::StudentType3},
  77. {protobuf::StudentType::STUDENTTYPE4, THUAI6::StudentType::StudentType4},
  78. };
  79. inline std::map<protobuf::TrickerType, THUAI6::TrickerType> trickerTypeDict{
  80. {protobuf::TrickerType::NULL_TRICKER_TYPE, THUAI6::TrickerType::NullTrickerType},
  81. {protobuf::TrickerType::TRICKERTYPE1, THUAI6::TrickerType::TrickerType1},
  82. {protobuf::TrickerType::TRICKERTYPE2, THUAI6::TrickerType::TrickerType2},
  83. {protobuf::TrickerType::TRICKERTYPE3, THUAI6::TrickerType::TrickerType3},
  84. {protobuf::TrickerType::TRICKERTYPE4, THUAI6::TrickerType::TrickerType4},
  85. };
  86. inline std::map<protobuf::StudentBuffType, THUAI6::StudentBuffType> studentBuffTypeDict{
  87. {protobuf::StudentBuffType::NULL_SBUFF_TYPE, THUAI6::StudentBuffType::NullStudentBuffType},
  88. {protobuf::StudentBuffType::SBUFFTYPE1, THUAI6::StudentBuffType::StudentBuffType1},
  89. {protobuf::StudentBuffType::SBUFFTYPE2, THUAI6::StudentBuffType::StudentBuffType2},
  90. {protobuf::StudentBuffType::SBUFFTYPE3, THUAI6::StudentBuffType::StudentBuffType3},
  91. {protobuf::StudentBuffType::SBUFFTYPE4, THUAI6::StudentBuffType::StudentBuffType4},
  92. };
  93. inline std::map<protobuf::TrickerBuffType, THUAI6::TrickerBuffType> trickerBuffTypeDict{
  94. {protobuf::TrickerBuffType::NULL_TBUFF_TYPE, THUAI6::TrickerBuffType::NullTrickerBuffType},
  95. {protobuf::TrickerBuffType::TBUFFTYPE1, THUAI6::TrickerBuffType::TrickerBuffType1},
  96. {protobuf::TrickerBuffType::TBUFFTYPE2, THUAI6::TrickerBuffType::TrickerBuffType2},
  97. {protobuf::TrickerBuffType::TBUFFTYPE3, THUAI6::TrickerBuffType::TrickerBuffType3},
  98. {protobuf::TrickerBuffType::TBUFFTYPE4, THUAI6::TrickerBuffType::TrickerBuffType4},
  99. };
  100. inline std::map<protobuf::StudentState, THUAI6::StudentState> studentStateDict{
  101. {protobuf::StudentState::NULL_STATUS, THUAI6::StudentState::NullStudentState},
  102. {protobuf::StudentState::IDLE, THUAI6::StudentState::Idle},
  103. {protobuf::StudentState::LEARNING, THUAI6::StudentState::Learning},
  104. {protobuf::StudentState::ADDICTED, THUAI6::StudentState::Addicted},
  105. {protobuf::StudentState::QUIT, THUAI6::StudentState::Quit},
  106. {protobuf::StudentState::GRADUATED, THUAI6::StudentState::Graduated},
  107. };
  108. inline std::map<protobuf::GameState, THUAI6::GameState> gameStateDict{
  109. {protobuf::GameState::NULL_GAME_STATE, THUAI6::GameState::NullGameState},
  110. {protobuf::GameState::GAME_START, THUAI6::GameState::GameStart},
  111. {protobuf::GameState::GAME_RUNNING, THUAI6::GameState::GameRunning},
  112. {protobuf::GameState::GAME_END, THUAI6::GameState::GameEnd},
  113. };
  114. // 用于将Protobuf中的类转换为THUAI6的类
  115. inline std::shared_ptr<THUAI6::Tricker> Protobuf2THUAI6Tricker(const protobuf::MessageOfTricker& trickerMsg)
  116. {
  117. auto tricker = std::make_shared<THUAI6::Tricker>();
  118. tricker->x = trickerMsg.x();
  119. tricker->y = trickerMsg.y();
  120. tricker->speed = trickerMsg.speed();
  121. tricker->damage = trickerMsg.damage();
  122. tricker->timeUntilSkillAvailable = trickerMsg.time_until_skill_available();
  123. tricker->place = placeTypeDict[trickerMsg.place()];
  124. tricker->prop = propTypeDict[trickerMsg.prop()];
  125. tricker->trickerType = trickerTypeDict[trickerMsg.tricker_type()];
  126. tricker->guid = trickerMsg.guid();
  127. tricker->movable = trickerMsg.movable();
  128. tricker->playerID = trickerMsg.player_id();
  129. tricker->viewRange = trickerMsg.view_range();
  130. tricker->radius = trickerMsg.radius();
  131. tricker->buff.clear();
  132. for (int i = 0; i < trickerMsg.buff().size(); i++)
  133. {
  134. tricker->buff.push_back(trickerBuffTypeDict[trickerMsg.buff(i)]);
  135. }
  136. return tricker;
  137. }
  138. inline std::shared_ptr<THUAI6::Student> Protobuf2THUAI6Student(const protobuf::MessageOfStudent& studentMsg)
  139. {
  140. auto student = std::make_shared<THUAI6::Student>();
  141. student->x = studentMsg.x();
  142. student->y = studentMsg.y();
  143. student->speed = studentMsg.speed();
  144. student->viewRange = studentMsg.view_range();
  145. student->playerID = studentMsg.player_id();
  146. student->guid = studentMsg.guid();
  147. student->radius = studentMsg.radius();
  148. student->timeUntilSkillAvailable = studentMsg.time_until_skill_available();
  149. student->damage = studentMsg.damage();
  150. student->playerType = THUAI6::PlayerType::StudentPlayer;
  151. student->prop = propTypeDict[studentMsg.prop()];
  152. student->place = placeTypeDict[studentMsg.place()];
  153. student->state = studentStateDict[studentMsg.state()];
  154. student->determination = studentMsg.determination();
  155. student->failNum = studentMsg.fail_num();
  156. student->failTime = studentMsg.fail_time();
  157. student->emoTime = studentMsg.emo_time();
  158. student->studentType = studentTypeDict[studentMsg.student_type()];
  159. student->buff.clear();
  160. for (int i = 0; i < studentMsg.buff_size(); i++)
  161. {
  162. student->buff.push_back(studentBuffTypeDict[studentMsg.buff(i)]);
  163. }
  164. return student;
  165. }
  166. inline std::shared_ptr<THUAI6::Prop> Protobuf2THUAI6Prop(const protobuf::MessageOfProp& propMsg)
  167. {
  168. auto prop = std::make_shared<THUAI6::Prop>();
  169. prop->x = propMsg.x();
  170. prop->y = propMsg.y();
  171. prop->type = propTypeDict[propMsg.type()];
  172. prop->place = placeTypeDict[propMsg.place()];
  173. prop->guid = propMsg.guid();
  174. prop->size = propMsg.size();
  175. prop->facingDirection = propMsg.facing_direction();
  176. prop->isMoving = propMsg.is_moving();
  177. return prop;
  178. }
  179. inline std::vector<std::vector<THUAI6::PlaceType>> Protobuf2THUAI6Map(const protobuf::MessageOfMap& mapMsg)
  180. {
  181. std::vector<std::vector<THUAI6::PlaceType>> map;
  182. for (int i = 0; i < mapMsg.row_size(); i++)
  183. {
  184. std::vector<THUAI6::PlaceType> row;
  185. for (int j = 0; j < mapMsg.row(i).col_size(); j++)
  186. {
  187. row.push_back(placeTypeDict[mapMsg.row(i).col(j)]);
  188. }
  189. map.push_back(row);
  190. }
  191. return map;
  192. }
  193. } // namespace Proto2THUAI6
  194. namespace THUAI62Proto
  195. {
  196. // 用于将THUAI6的枚举转换为Protobuf的枚举
  197. inline std::map<THUAI6::PlaceType, protobuf::PlaceType> placeTypeDict{
  198. {THUAI6::PlaceType::NullPlaceType, protobuf::PlaceType::NULL_PLACE_TYPE},
  199. {THUAI6::PlaceType::Land, protobuf::PlaceType::LAND},
  200. {THUAI6::PlaceType::Wall, protobuf::PlaceType::WALL},
  201. {THUAI6::PlaceType::Grass, protobuf::PlaceType::GRASS},
  202. {THUAI6::PlaceType::ClassRoom, protobuf::PlaceType::CLASSROOM},
  203. {THUAI6::PlaceType::Gate, protobuf::PlaceType::GATE},
  204. {THUAI6::PlaceType::HiddenGate, protobuf::PlaceType::HIDDEN_GATE},
  205. };
  206. inline std::map<THUAI6::ShapeType, protobuf::ShapeType> shapeTypeDict{
  207. {THUAI6::ShapeType::NullShapeType, protobuf::ShapeType::NULL_SHAPE_TYPE},
  208. {THUAI6::ShapeType::Circle, protobuf::ShapeType::CIRCLE},
  209. {THUAI6::ShapeType::Square, protobuf::ShapeType::SQUARE},
  210. };
  211. inline std::map<THUAI6::PropType, protobuf::PropType> propTypeDict{
  212. {THUAI6::PropType::NullPropType, protobuf::PropType::NULL_PROP_TYPE},
  213. {THUAI6::PropType::PropType1, protobuf::PropType::PTYPE1},
  214. {THUAI6::PropType::PropType2, protobuf::PropType::PTYPE2},
  215. {THUAI6::PropType::PropType3, protobuf::PropType::PTYPE3},
  216. {THUAI6::PropType::PropType4, protobuf::PropType::PTYPE4},
  217. };
  218. inline std::map<THUAI6::PlayerType, protobuf::PlayerType> playerTypeDict{
  219. {THUAI6::PlayerType::NullPlayerType, protobuf::PlayerType::NULL_PLAYER_TYPE},
  220. {THUAI6::PlayerType::StudentPlayer, protobuf::PlayerType::STUDENT_PLAYER},
  221. {THUAI6::PlayerType::TrickerPlayer, protobuf::PlayerType::TRICKER_PLAYER},
  222. };
  223. inline std::map<THUAI6::StudentType, protobuf::StudentType> studentTypeDict{
  224. {THUAI6::StudentType::NullStudentType, protobuf::StudentType::NULL_STUDENT_TYPE},
  225. {THUAI6::StudentType::StudentType1, protobuf::StudentType::STUDENTTYPE1},
  226. {THUAI6::StudentType::StudentType2, protobuf::StudentType::STUDENTTYPE2},
  227. {THUAI6::StudentType::StudentType3, protobuf::StudentType::STUDENTTYPE3},
  228. {THUAI6::StudentType::StudentType4, protobuf::StudentType::STUDENTTYPE4},
  229. };
  230. inline std::map<THUAI6::StudentBuffType, protobuf::StudentBuffType> studentBuffTypeDict{
  231. {THUAI6::StudentBuffType::NullStudentBuffType, protobuf::StudentBuffType::NULL_SBUFF_TYPE},
  232. {THUAI6::StudentBuffType::StudentBuffType1, protobuf::StudentBuffType::SBUFFTYPE1},
  233. {THUAI6::StudentBuffType::StudentBuffType2, protobuf::StudentBuffType::SBUFFTYPE2},
  234. {THUAI6::StudentBuffType::StudentBuffType3, protobuf::StudentBuffType::SBUFFTYPE3},
  235. {THUAI6::StudentBuffType::StudentBuffType4, protobuf::StudentBuffType::SBUFFTYPE4},
  236. };
  237. inline std::map<THUAI6::TrickerType, protobuf::TrickerType> trickerTypeDict{
  238. {THUAI6::TrickerType::NullTrickerType, protobuf::TrickerType::NULL_TRICKER_TYPE},
  239. {THUAI6::TrickerType::TrickerType1, protobuf::TrickerType::TRICKERTYPE1},
  240. {THUAI6::TrickerType::TrickerType2, protobuf::TrickerType::TRICKERTYPE2},
  241. {THUAI6::TrickerType::TrickerType3, protobuf::TrickerType::TRICKERTYPE3},
  242. {THUAI6::TrickerType::TrickerType4, protobuf::TrickerType::TRICKERTYPE4},
  243. };
  244. inline std::map<THUAI6::TrickerBuffType, protobuf::TrickerBuffType> trickerBuffTypeDict{
  245. {THUAI6::TrickerBuffType::NullTrickerBuffType, protobuf::TrickerBuffType::NULL_TBUFF_TYPE},
  246. {THUAI6::TrickerBuffType::TrickerBuffType1, protobuf::TrickerBuffType::TBUFFTYPE1},
  247. {THUAI6::TrickerBuffType::TrickerBuffType2, protobuf::TrickerBuffType::TBUFFTYPE2},
  248. {THUAI6::TrickerBuffType::TrickerBuffType3, protobuf::TrickerBuffType::TBUFFTYPE3},
  249. {THUAI6::TrickerBuffType::TrickerBuffType4, protobuf::TrickerBuffType::TBUFFTYPE4},
  250. };
  251. // 用于将THUAI6的类转换为Protobuf的消息
  252. inline protobuf::PlayerMsg THUAI62ProtobufPlayer(int64_t playerID, THUAI6::PlayerType playerType, THUAI6::StudentType studentType, THUAI6::TrickerType trickerType)
  253. {
  254. protobuf::PlayerMsg playerMsg;
  255. playerMsg.set_player_id(playerID);
  256. playerMsg.set_player_type(playerTypeDict[playerType]);
  257. if (playerType == THUAI6::PlayerType::StudentPlayer)
  258. {
  259. playerMsg.set_student_type(studentTypeDict[studentType]);
  260. }
  261. else if (playerType == THUAI6::PlayerType::TrickerPlayer)
  262. {
  263. playerMsg.set_tricker_type(trickerTypeDict[trickerType]);
  264. }
  265. return playerMsg;
  266. }
  267. inline protobuf::IDMsg THUAI62ProtobufID(int playerID)
  268. {
  269. protobuf::IDMsg idMsg;
  270. idMsg.set_player_id(playerID);
  271. return idMsg;
  272. }
  273. inline protobuf::MoveMsg THUAI62ProtobufMove(int64_t time, double angle, int64_t id)
  274. {
  275. protobuf::MoveMsg moveMsg;
  276. moveMsg.set_time_in_milliseconds(time);
  277. moveMsg.set_angle(angle);
  278. moveMsg.set_player_id(id);
  279. return moveMsg;
  280. }
  281. inline protobuf::PickMsg THUAI62ProtobufPick(THUAI6::PropType prop, int64_t id)
  282. {
  283. protobuf::PickMsg pickMsg;
  284. pickMsg.set_prop_type(propTypeDict[prop]);
  285. pickMsg.set_player_id(id);
  286. return pickMsg;
  287. }
  288. inline protobuf::SendMsg THUAI62ProtobufSend(std::string msg, int64_t toID, int64_t id)
  289. {
  290. protobuf::SendMsg sendMsg;
  291. sendMsg.set_message(msg);
  292. sendMsg.set_to_player_id(toID);
  293. sendMsg.set_player_id(id);
  294. return sendMsg;
  295. }
  296. inline protobuf::TrickMsg THUAI62ProtobufTrick(double angle, int64_t id)
  297. {
  298. protobuf::TrickMsg trickMsg;
  299. trickMsg.set_angle(angle);
  300. trickMsg.set_player_id(id);
  301. return trickMsg;
  302. }
  303. } // namespace THUAI62Proto
  304. namespace Time
  305. {
  306. inline double TimeSinceStart(const std::chrono::system_clock::time_point& sp)
  307. {
  308. std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
  309. std::chrono::duration<double, std::milli> time_span = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(tp - sp);
  310. return time_span.count();
  311. }
  312. } // namespace Time
  313. #endif