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

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