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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // 杂项函数
  2. #pragma once
  3. #ifndef UTILS_HPP
  4. #define UTILS_HPP
  5. #include <cstdint>
  6. #include <cmath>
  7. #include <map>
  8. #include <vector>
  9. #include "Message2Clients.pb.h"
  10. #include "Message2Server.pb.h"
  11. #include "MessageType.pb.h"
  12. #include "structures.h"
  13. #undef GetMessage
  14. #undef SendMessage
  15. #undef PeekMessage
  16. namespace AssistFunction
  17. {
  18. constexpr int numOfGridPerCell = 1000;
  19. [[nodiscard]] constexpr inline int GridToCell(int grid) noexcept
  20. {
  21. return grid / numOfGridPerCell;
  22. }
  23. [[nodiscard]] constexpr inline int GridToCell(double grid) noexcept
  24. {
  25. return int(grid) / numOfGridPerCell;
  26. }
  27. inline bool HaveView(int viewRange, int x, int y, int newX, int newY, std::vector<std::vector<THUAI6::PlaceType>>& map)
  28. {
  29. int deltaX = newX - x;
  30. int deltaY = newY - y;
  31. double distance = deltaX * deltaX + deltaY * deltaY;
  32. THUAI6::PlaceType myPlace = map[GridToCell(x)][GridToCell(y)];
  33. THUAI6::PlaceType newPlace = map[GridToCell(newX)][GridToCell(newY)];
  34. if (newPlace == THUAI6::PlaceType::Grass && myPlace != THUAI6::PlaceType::Grass) // 草丛外必不可能看到草丛内
  35. return false;
  36. if (distance < viewRange * viewRange)
  37. {
  38. int divide = std::max(std::abs(deltaX), std::abs(deltaY)) / 100;
  39. if (divide == 0)
  40. return true;
  41. double dx = deltaX / divide;
  42. double dy = deltaY / divide;
  43. double myX = double(x);
  44. double myY = double(y);
  45. if (newPlace == THUAI6::PlaceType::Grass && myPlace == THUAI6::PlaceType::Grass) // 都在草丛内,要另作判断
  46. for (int i = 0; i < divide; i++)
  47. {
  48. myX += dx;
  49. myY += dy;
  50. if (map[GridToCell(myX)][GridToCell(myY)] != THUAI6::PlaceType::Grass)
  51. return false;
  52. }
  53. else // 不在草丛内,只需要没有墙即可
  54. for (int i = 0; i < divide; i++)
  55. {
  56. myX += dx;
  57. myY += dy;
  58. if (map[GridToCell(myX)][GridToCell(myY)] == THUAI6::PlaceType::Wall)
  59. return false;
  60. }
  61. return true;
  62. }
  63. else
  64. return false;
  65. }
  66. } // namespace AssistFunction
  67. // 辅助函数,用于将proto信息转换为THUAI6信息
  68. namespace Proto2THUAI6
  69. {
  70. // 用于将Protobuf中的枚举转换为THUAI6的枚举
  71. inline std::map<protobuf::PlaceType, THUAI6::PlaceType> placeTypeDict{
  72. {protobuf::PlaceType::NULL_PLACE_TYPE, THUAI6::PlaceType::NullPlaceType},
  73. {protobuf::PlaceType::LAND, THUAI6::PlaceType::Land},
  74. {protobuf::PlaceType::WALL, THUAI6::PlaceType::Wall},
  75. {protobuf::PlaceType::GRASS, THUAI6::PlaceType::Grass},
  76. {protobuf::PlaceType::CLASSROOM, THUAI6::PlaceType::ClassRoom},
  77. {protobuf::PlaceType::GATE, THUAI6::PlaceType::Gate},
  78. {protobuf::PlaceType::HIDDEN_GATE, THUAI6::PlaceType::HiddenGate},
  79. {protobuf::PlaceType::WINDOW, THUAI6::PlaceType::Window},
  80. {protobuf::PlaceType::DOOR3, THUAI6::PlaceType::Door3},
  81. {protobuf::PlaceType::DOOR5, THUAI6::PlaceType::Door5},
  82. {protobuf::PlaceType::DOOR6, THUAI6::PlaceType::Door6},
  83. {protobuf::PlaceType::CHEST, THUAI6::PlaceType::Chest},
  84. };
  85. inline std::map<protobuf::ShapeType, THUAI6::ShapeType> shapeTypeDict{
  86. {protobuf::ShapeType::NULL_SHAPE_TYPE, THUAI6::ShapeType::NullShapeType},
  87. {protobuf::ShapeType::CIRCLE, THUAI6::ShapeType::Circle},
  88. {protobuf::ShapeType::SQUARE, THUAI6::ShapeType::Square},
  89. };
  90. inline std::map<protobuf::PropType, THUAI6::PropType> propTypeDict{
  91. {protobuf::PropType::NULL_PROP_TYPE, THUAI6::PropType::NullPropType},
  92. {protobuf::PropType::KEY3, THUAI6::PropType::Key3},
  93. {protobuf::PropType::KEY5, THUAI6::PropType::Key5},
  94. {protobuf::PropType::KEY6, THUAI6::PropType::Key6},
  95. {protobuf::PropType::ADD_SPEED, THUAI6::PropType::AddSpeed},
  96. {protobuf::PropType::ADD_HP_OR_AP, THUAI6::PropType::AddHpOrAp},
  97. {protobuf::PropType::ADD_LIFE_OR_CLAIRAUDIENCE, THUAI6::PropType::AddLifeOrClairaudience},
  98. {protobuf::PropType::SHIELD_OR_SPEAR, THUAI6::PropType::ShieldOrSpear},
  99. {protobuf::PropType::RECOVERY_FROM_DIZZINESS, THUAI6::PropType::RecoveryFromDizziness},
  100. {protobuf::PropType::CRAFTING_BENCH, THUAI6::PropType::CraftingBench},
  101. };
  102. inline std::map<protobuf::PlayerType, THUAI6::PlayerType> playerTypeDict{
  103. {protobuf::PlayerType::NULL_PLAYER_TYPE, THUAI6::PlayerType::NullPlayerType},
  104. {protobuf::PlayerType::STUDENT_PLAYER, THUAI6::PlayerType::StudentPlayer},
  105. {protobuf::PlayerType::TRICKER_PLAYER, THUAI6::PlayerType::TrickerPlayer},
  106. };
  107. inline std::map<protobuf::StudentType, THUAI6::StudentType> studentTypeDict{
  108. {protobuf::StudentType::NULL_STUDENT_TYPE, THUAI6::StudentType::NullStudentType},
  109. {protobuf::StudentType::ATHLETE, THUAI6::StudentType::Athlete},
  110. {protobuf::StudentType::TEACHER, THUAI6::StudentType::Teacher},
  111. {protobuf::StudentType::STRAIGHT_A_STUDENT, THUAI6::StudentType::StraightAStudent},
  112. {protobuf::StudentType::ROBOT, THUAI6::StudentType::Robot},
  113. {protobuf::StudentType::TECH_OTAKU, THUAI6::StudentType::TechOtaku},
  114. {protobuf::StudentType::SUNSHINE, THUAI6::StudentType::Sunshine},
  115. };
  116. inline std::map<protobuf::TrickerType, THUAI6::TrickerType> trickerTypeDict{
  117. {protobuf::TrickerType::NULL_TRICKER_TYPE, THUAI6::TrickerType::NullTrickerType},
  118. {protobuf::TrickerType::ASSASSIN, THUAI6::TrickerType::Assassin},
  119. {protobuf::TrickerType::KLEE, THUAI6::TrickerType::Klee},
  120. {protobuf::TrickerType::A_NOISY_PERSON, THUAI6::TrickerType::ANoisyPerson},
  121. {protobuf::TrickerType::IDOL, THUAI6::TrickerType::Idol},
  122. };
  123. inline std::map<protobuf::StudentBuffType, THUAI6::StudentBuffType> studentBuffTypeDict{
  124. {protobuf::StudentBuffType::NULL_SBUFF_TYPE, THUAI6::StudentBuffType::NullStudentBuffType},
  125. {protobuf::StudentBuffType::STUDENT_ADD_SPEED, THUAI6::StudentBuffType::AddSpeed},
  126. {protobuf::StudentBuffType::ADD_LIFE, THUAI6::StudentBuffType::AddLife},
  127. {protobuf::StudentBuffType::SHIELD, THUAI6::StudentBuffType::Shield},
  128. {protobuf::StudentBuffType::STUDENT_INVISIBLE, THUAI6::StudentBuffType::Invisible},
  129. };
  130. inline std::map<protobuf::TrickerBuffType, THUAI6::TrickerBuffType> trickerBuffTypeDict{
  131. {protobuf::TrickerBuffType::NULL_TBUFF_TYPE, THUAI6::TrickerBuffType::NullTrickerBuffType},
  132. {protobuf::TrickerBuffType::TRICKER_ADD_SPEED, THUAI6::TrickerBuffType::AddSpeed},
  133. {protobuf::TrickerBuffType::SPEAR, THUAI6::TrickerBuffType::Spear},
  134. {protobuf::TrickerBuffType::ADD_AP, THUAI6::TrickerBuffType::AddAp},
  135. {protobuf::TrickerBuffType::CLAIRAUDIENCE, THUAI6::TrickerBuffType::Clairaudience},
  136. {protobuf::TrickerBuffType::TRICKER_INVISIBLE, THUAI6::TrickerBuffType::Invisible},
  137. };
  138. inline std::map<protobuf::PlayerState, THUAI6::PlayerState> playerStateDict{
  139. {protobuf::PlayerState::NULL_STATUS, THUAI6::PlayerState::NullState},
  140. {protobuf::PlayerState::IDLE, THUAI6::PlayerState::Idle},
  141. {protobuf::PlayerState::LEARNING, THUAI6::PlayerState::Learning},
  142. {protobuf::PlayerState::ADDICTED, THUAI6::PlayerState::Addicted},
  143. {protobuf::PlayerState::QUIT, THUAI6::PlayerState::Quit},
  144. {protobuf::PlayerState::GRADUATED, THUAI6::PlayerState::Graduated},
  145. {protobuf::PlayerState::RESCUED, THUAI6::PlayerState::Roused},
  146. {protobuf::PlayerState::TREATED, THUAI6::PlayerState::Encouraged},
  147. {protobuf::PlayerState::STUNNED, THUAI6::PlayerState::Stunned},
  148. {protobuf::PlayerState::RESCUING, THUAI6::PlayerState::Rousing},
  149. {protobuf::PlayerState::TREATING, THUAI6::PlayerState::Encouraging},
  150. {protobuf::PlayerState::SWINGING, THUAI6::PlayerState::Swinging},
  151. {protobuf::PlayerState::ATTACKING, THUAI6::PlayerState::Attacking},
  152. {protobuf::PlayerState::LOCKING, THUAI6::PlayerState::Locking},
  153. // {protobuf::PlayerState::RUMMAGING, THUAI6::PlayerState::Rummaging},
  154. {protobuf::PlayerState::CLIMBING, THUAI6::PlayerState::Climbing},
  155. {protobuf::PlayerState::OPENING_A_CHEST, THUAI6::PlayerState::OpeningAChest},
  156. {protobuf::PlayerState::USING_SPECIAL_SKILL, THUAI6::PlayerState::UsingSpecialSkill},
  157. {protobuf::PlayerState::OPENING_A_GATE, THUAI6::PlayerState::OpeningAGate},
  158. };
  159. inline std::map<protobuf::GameState, THUAI6::GameState> gameStateDict{
  160. {protobuf::GameState::NULL_GAME_STATE, THUAI6::GameState::NullGameState},
  161. {protobuf::GameState::GAME_START, THUAI6::GameState::GameStart},
  162. {protobuf::GameState::GAME_RUNNING, THUAI6::GameState::GameRunning},
  163. {protobuf::GameState::GAME_END, THUAI6::GameState::GameEnd},
  164. };
  165. inline std::map<protobuf::BulletType, THUAI6::BulletType> bulletTypeDict{
  166. {protobuf::BulletType::NULL_BULLET_TYPE, THUAI6::BulletType::NullBulletType},
  167. {protobuf::BulletType::FLYING_KNIFE, THUAI6::BulletType::FlyingKnife},
  168. {protobuf::BulletType::COMMON_ATTACK_OF_TRICKER, THUAI6::BulletType::CommonAttackOfTricker},
  169. {protobuf::BulletType::BOMB_BOMB, THUAI6::BulletType::BombBomb},
  170. {protobuf::BulletType::JUMPY_DUMPTY, THUAI6::BulletType::JumpyDumpty},
  171. {protobuf::BulletType::STRIKE, THUAI6::BulletType::Strike},
  172. };
  173. inline std::map<protobuf::MessageOfObj::MessageOfObjCase, THUAI6::MessageOfObj> messageOfObjDict{
  174. {protobuf::MessageOfObj::MessageOfObjCase::kStudentMessage, THUAI6::MessageOfObj::StudentMessage},
  175. {protobuf::MessageOfObj::MessageOfObjCase::kTrickerMessage, THUAI6::MessageOfObj::TrickerMessage},
  176. {protobuf::MessageOfObj::MessageOfObjCase::kPropMessage, THUAI6::MessageOfObj::PropMessage},
  177. {protobuf::MessageOfObj::MessageOfObjCase::kBulletMessage, THUAI6::MessageOfObj::BulletMessage},
  178. {protobuf::MessageOfObj::MessageOfObjCase::kBombedBulletMessage, THUAI6::MessageOfObj::BombedBulletMessage},
  179. {protobuf::MessageOfObj::MessageOfObjCase::kClassroomMessage, THUAI6::MessageOfObj::ClassroomMessage},
  180. {protobuf::MessageOfObj::MessageOfObjCase::kDoorMessage, THUAI6::MessageOfObj::DoorMessage},
  181. {protobuf::MessageOfObj::MessageOfObjCase::kGateMessage, THUAI6::MessageOfObj::GateMessage},
  182. {protobuf::MessageOfObj::MessageOfObjCase::kChestMessage, THUAI6::MessageOfObj::ChestMessage},
  183. {protobuf::MessageOfObj::MessageOfObjCase::MESSAGE_OF_OBJ_NOT_SET, THUAI6::MessageOfObj::NullMessageOfObj},
  184. {protobuf::MessageOfObj::MessageOfObjCase::kMapMessage, THUAI6::MessageOfObj::MapMessage},
  185. {protobuf::MessageOfObj::MessageOfObjCase::kNewsMessage, THUAI6::MessageOfObj::NewsMessage},
  186. {protobuf::MessageOfObj::MessageOfObjCase::kHiddenGateMessage, THUAI6::MessageOfObj::HiddenGateMessage},
  187. };
  188. inline std::map<protobuf::MessageOfNews::NewsCase, THUAI6::NewsType> newsTypeDict{
  189. {protobuf::MessageOfNews::NewsCase::NEWS_NOT_SET, THUAI6::NewsType::NullNewsType},
  190. {protobuf::MessageOfNews::NewsCase::kTextMessage, THUAI6::NewsType::TextMessage},
  191. {protobuf::MessageOfNews::NewsCase::kBinaryMessage, THUAI6::NewsType::BinaryMessage},
  192. };
  193. // 用于将Protobuf中的类转换为THUAI6的类
  194. inline std::shared_ptr<THUAI6::Tricker> Protobuf2THUAI6Tricker(const protobuf::MessageOfTricker& trickerMsg)
  195. {
  196. auto tricker = std::make_shared<THUAI6::Tricker>();
  197. tricker->x = trickerMsg.x();
  198. tricker->y = trickerMsg.y();
  199. tricker->speed = trickerMsg.speed();
  200. tricker->score = trickerMsg.score();
  201. tricker->facingDirection = trickerMsg.facing_direction();
  202. tricker->bulletType = bulletTypeDict[trickerMsg.bullet_type()];
  203. tricker->trickDesire = trickerMsg.trick_desire();
  204. tricker->classVolume = trickerMsg.class_volume();
  205. tricker->timeUntilSkillAvailable.clear();
  206. for (int i = 0; i < trickerMsg.time_until_skill_available().size(); i++)
  207. tricker->timeUntilSkillAvailable.push_back(trickerMsg.time_until_skill_available(i));
  208. // tricker->place = placeTypeDict[trickerMsg.place()];
  209. tricker->playerState = playerStateDict[trickerMsg.player_state()];
  210. tricker->props.clear();
  211. for (int i = 0; i < trickerMsg.prop().size(); i++)
  212. tricker->props.push_back(propTypeDict[trickerMsg.prop(i)]);
  213. tricker->trickerType = trickerTypeDict[trickerMsg.tricker_type()];
  214. tricker->guid = trickerMsg.guid();
  215. tricker->playerID = trickerMsg.player_id();
  216. tricker->viewRange = trickerMsg.view_range();
  217. tricker->radius = trickerMsg.radius();
  218. tricker->playerType = THUAI6::PlayerType::TrickerPlayer;
  219. tricker->buff.clear();
  220. for (int i = 0; i < trickerMsg.buff().size(); i++)
  221. tricker->buff.push_back(trickerBuffTypeDict[trickerMsg.buff(i)]);
  222. return tricker;
  223. }
  224. inline std::shared_ptr<THUAI6::Student> Protobuf2THUAI6Student(const protobuf::MessageOfStudent& studentMsg)
  225. {
  226. auto student = std::make_shared<THUAI6::Student>();
  227. student->x = studentMsg.x();
  228. student->y = studentMsg.y();
  229. student->speed = studentMsg.speed();
  230. student->viewRange = studentMsg.view_range();
  231. student->playerID = studentMsg.player_id();
  232. student->guid = studentMsg.guid();
  233. student->radius = studentMsg.radius();
  234. student->score = studentMsg.score();
  235. student->facingDirection = studentMsg.facing_direction();
  236. student->bulletType = bulletTypeDict[studentMsg.bullet_type()];
  237. student->learningSpeed = studentMsg.learning_speed();
  238. student->encourageSpeed = studentMsg.treat_speed();
  239. student->encourageProgress = studentMsg.treat_progress();
  240. student->rouseProgress = studentMsg.rescue_progress();
  241. student->dangerAlert = studentMsg.danger_alert();
  242. student->timeUntilSkillAvailable.clear();
  243. for (int i = 0; i < studentMsg.time_until_skill_available().size(); i++)
  244. student->timeUntilSkillAvailable.push_back(studentMsg.time_until_skill_available(i));
  245. student->playerType = THUAI6::PlayerType::StudentPlayer;
  246. student->props.clear();
  247. for (int i = 0; i < studentMsg.prop().size(); i++)
  248. student->props.push_back(propTypeDict[studentMsg.prop(i)]);
  249. // student->place = placeTypeDict[studentMsg.place()];
  250. student->playerState = playerStateDict[studentMsg.player_state()];
  251. student->determination = studentMsg.determination();
  252. student->addiction = studentMsg.addiction();
  253. student->studentType = studentTypeDict[studentMsg.student_type()];
  254. student->buff.clear();
  255. for (int i = 0; i < studentMsg.buff_size(); i++)
  256. student->buff.push_back(studentBuffTypeDict[studentMsg.buff(i)]);
  257. return student;
  258. }
  259. inline std::shared_ptr<THUAI6::Prop> Protobuf2THUAI6Prop(const protobuf::MessageOfProp& propMsg)
  260. {
  261. auto prop = std::make_shared<THUAI6::Prop>();
  262. prop->x = propMsg.x();
  263. prop->y = propMsg.y();
  264. prop->type = propTypeDict[propMsg.type()];
  265. // prop->place = placeTypeDict[propMsg.place()];
  266. prop->guid = propMsg.guid();
  267. prop->facingDirection = propMsg.facing_direction();
  268. return prop;
  269. }
  270. inline std::shared_ptr<THUAI6::GameInfo> Protobuf2THUAI6GameInfo(const protobuf::MessageOfAll& allMsg)
  271. {
  272. auto gameInfo = std::make_shared<THUAI6::GameInfo>();
  273. gameInfo->gameTime = allMsg.game_time();
  274. gameInfo->subjectFinished = allMsg.subject_finished();
  275. gameInfo->studentGraduated = allMsg.student_graduated();
  276. gameInfo->studentQuited = allMsg.student_quited();
  277. gameInfo->studentScore = allMsg.student_score();
  278. gameInfo->trickerScore = allMsg.tricker_score();
  279. return gameInfo;
  280. }
  281. inline std::shared_ptr<THUAI6::Bullet> Protobuf2THUAI6Bullet(const protobuf::MessageOfBullet& bulletMsg)
  282. {
  283. auto bullet = std::make_shared<THUAI6::Bullet>();
  284. bullet->bulletType = bulletTypeDict[bulletMsg.type()];
  285. bullet->x = bulletMsg.x();
  286. bullet->y = bulletMsg.y();
  287. bullet->facingDirection = bulletMsg.facing_direction();
  288. bullet->guid = bulletMsg.guid();
  289. bullet->team = playerTypeDict[bulletMsg.team()];
  290. // bullet->place = placeTypeDict[bulletMsg.place()];
  291. bullet->bombRange = bulletMsg.bomb_range();
  292. bullet->speed = bulletMsg.speed();
  293. return bullet;
  294. }
  295. inline std::shared_ptr<THUAI6::BombedBullet> Protobuf2THUAI6BombedBullet(const protobuf::MessageOfBombedBullet& bombedBulletMsg)
  296. {
  297. auto bombedBullet = std::make_shared<THUAI6::BombedBullet>();
  298. bombedBullet->bulletType = bulletTypeDict[bombedBulletMsg.type()];
  299. bombedBullet->x = bombedBulletMsg.x();
  300. bombedBullet->y = bombedBulletMsg.y();
  301. bombedBullet->facingDirection = bombedBulletMsg.facing_direction();
  302. bombedBullet->mappingID = bombedBulletMsg.mapping_id();
  303. bombedBullet->bombRange = bombedBulletMsg.bomb_range();
  304. return bombedBullet;
  305. }
  306. inline THUAI6::HiddenGateState Bool2HiddenGateState(bool gateMsg)
  307. {
  308. if (gateMsg)
  309. return THUAI6::HiddenGateState::Opened;
  310. else
  311. return THUAI6::HiddenGateState::Refreshed;
  312. }
  313. } // namespace Proto2THUAI6
  314. namespace THUAI62Proto
  315. {
  316. // 用于将THUAI6的枚举转换为Protobuf的枚举
  317. inline std::map<THUAI6::PlaceType, protobuf::PlaceType> placeTypeDict{
  318. {THUAI6::PlaceType::NullPlaceType, protobuf::PlaceType::NULL_PLACE_TYPE},
  319. {THUAI6::PlaceType::Land, protobuf::PlaceType::LAND},
  320. {THUAI6::PlaceType::Wall, protobuf::PlaceType::WALL},
  321. {THUAI6::PlaceType::Grass, protobuf::PlaceType::GRASS},
  322. {THUAI6::PlaceType::ClassRoom, protobuf::PlaceType::CLASSROOM},
  323. {THUAI6::PlaceType::Gate, protobuf::PlaceType::GATE},
  324. {THUAI6::PlaceType::HiddenGate, protobuf::PlaceType::HIDDEN_GATE},
  325. {THUAI6::PlaceType::Door3, protobuf::PlaceType::DOOR3},
  326. {THUAI6::PlaceType::Door5, protobuf::PlaceType::DOOR5},
  327. {THUAI6::PlaceType::Door6, protobuf::PlaceType::DOOR6},
  328. {THUAI6::PlaceType::Window, protobuf::PlaceType::WINDOW},
  329. };
  330. inline std::map<THUAI6::ShapeType, protobuf::ShapeType> shapeTypeDict{
  331. {THUAI6::ShapeType::NullShapeType, protobuf::ShapeType::NULL_SHAPE_TYPE},
  332. {THUAI6::ShapeType::Circle, protobuf::ShapeType::CIRCLE},
  333. {THUAI6::ShapeType::Square, protobuf::ShapeType::SQUARE},
  334. };
  335. inline std::map<THUAI6::PropType, protobuf::PropType> propTypeDict{
  336. {THUAI6::PropType::NullPropType, protobuf::PropType::NULL_PROP_TYPE},
  337. {THUAI6::PropType::Key3, protobuf::PropType::KEY3},
  338. {THUAI6::PropType::Key5, protobuf::PropType::KEY5},
  339. {THUAI6::PropType::Key6, protobuf::PropType::KEY6},
  340. {THUAI6::PropType::AddHpOrAp, protobuf::PropType::ADD_HP_OR_AP},
  341. {THUAI6::PropType::AddLifeOrClairaudience, protobuf::PropType::ADD_LIFE_OR_CLAIRAUDIENCE},
  342. {THUAI6::PropType::AddSpeed, protobuf::PropType::ADD_SPEED},
  343. {THUAI6::PropType::ShieldOrSpear, protobuf::PropType::SHIELD_OR_SPEAR},
  344. {THUAI6::PropType::CraftingBench, protobuf::PropType::CRAFTING_BENCH},
  345. };
  346. inline std::map<THUAI6::PlayerType, protobuf::PlayerType> playerTypeDict{
  347. {THUAI6::PlayerType::NullPlayerType, protobuf::PlayerType::NULL_PLAYER_TYPE},
  348. {THUAI6::PlayerType::StudentPlayer, protobuf::PlayerType::STUDENT_PLAYER},
  349. {THUAI6::PlayerType::TrickerPlayer, protobuf::PlayerType::TRICKER_PLAYER},
  350. };
  351. inline std::map<THUAI6::StudentType, protobuf::StudentType> studentTypeDict{
  352. {THUAI6::StudentType::NullStudentType, protobuf::StudentType::NULL_STUDENT_TYPE},
  353. {THUAI6::StudentType::Athlete, protobuf::StudentType::ATHLETE},
  354. {THUAI6::StudentType::Teacher, protobuf::StudentType::TEACHER},
  355. {THUAI6::StudentType::StraightAStudent, protobuf::StudentType::STRAIGHT_A_STUDENT},
  356. {THUAI6::StudentType::Robot, protobuf::StudentType::ROBOT},
  357. {THUAI6::StudentType::TechOtaku, protobuf::StudentType::TECH_OTAKU},
  358. {THUAI6::StudentType::Sunshine, protobuf::StudentType::SUNSHINE},
  359. };
  360. // inline std::map<THUAI6::StudentBuffType, protobuf::StudentBuffType> studentBuffTypeDict{
  361. // {THUAI6::StudentBuffType::NullStudentBuffType, protobuf::StudentBuffType::NULL_SBUFF_TYPE},
  362. // {THUAI6::StudentBuffType::StudentBuffType1, protobuf::StudentBuffType::ADD_SPEED},
  363. // {THUAI6::StudentBuffType::StudentBuffType2, protobuf::StudentBuffType::SBUFFTYPE2},
  364. // {THUAI6::StudentBuffType::StudentBuffType3, protobuf::StudentBuffType::SBUFFTYPE3},
  365. // {THUAI6::StudentBuffType::StudentBuffType4, protobuf::StudentBuffType::SBUFFTYPE4},
  366. // };
  367. inline std::map<THUAI6::TrickerType, protobuf::TrickerType> trickerTypeDict{
  368. {THUAI6::TrickerType::NullTrickerType, protobuf::TrickerType::NULL_TRICKER_TYPE},
  369. {THUAI6::TrickerType::Assassin, protobuf::TrickerType::ASSASSIN},
  370. {THUAI6::TrickerType::Klee, protobuf::TrickerType::KLEE},
  371. {THUAI6::TrickerType::ANoisyPerson, protobuf::TrickerType::A_NOISY_PERSON},
  372. {THUAI6::TrickerType::Idol, protobuf::TrickerType::IDOL},
  373. };
  374. // inline std::map<THUAI6::TrickerBuffType, protobuf::TrickerBuffType> trickerBuffTypeDict{
  375. // {THUAI6::TrickerBuffType::NullTrickerBuffType, protobuf::TrickerBuffType::NULL_TBUFF_TYPE},
  376. // {THUAI6::TrickerBuffType::TrickerBuffType1, protobuf::TrickerBuffType::TBUFFTYPE1},
  377. // {THUAI6::TrickerBuffType::TrickerBuffType2, protobuf::TrickerBuffType::TBUFFTYPE2},
  378. // {THUAI6::TrickerBuffType::TrickerBuffType3, protobuf::TrickerBuffType::TBUFFTYPE3},
  379. // {THUAI6::TrickerBuffType::TrickerBuffType4, protobuf::TrickerBuffType::TBUFFTYPE4},
  380. // };
  381. // 用于将THUAI6的类转换为Protobuf的消息
  382. inline protobuf::PlayerMsg THUAI62ProtobufPlayer(int64_t playerID, THUAI6::PlayerType playerType, THUAI6::StudentType studentType, THUAI6::TrickerType trickerType)
  383. {
  384. protobuf::PlayerMsg playerMsg;
  385. playerMsg.set_player_id(playerID);
  386. playerMsg.set_player_type(playerTypeDict[playerType]);
  387. if (playerType == THUAI6::PlayerType::StudentPlayer)
  388. {
  389. playerMsg.set_player_type(protobuf::PlayerType::STUDENT_PLAYER);
  390. playerMsg.set_student_type(studentTypeDict[studentType]);
  391. }
  392. else if (playerType == THUAI6::PlayerType::TrickerPlayer)
  393. {
  394. playerMsg.set_player_type(protobuf::PlayerType::TRICKER_PLAYER);
  395. playerMsg.set_tricker_type(trickerTypeDict[trickerType]);
  396. }
  397. return playerMsg;
  398. }
  399. inline protobuf::IDMsg THUAI62ProtobufID(int64_t playerID)
  400. {
  401. protobuf::IDMsg idMsg;
  402. idMsg.set_player_id(playerID);
  403. return idMsg;
  404. }
  405. inline protobuf::TreatAndRescueMsg THUAI62ProtobufTreatAndRescue(int64_t playerID, int64_t mateID)
  406. {
  407. protobuf::TreatAndRescueMsg treatAndRescueMsg;
  408. treatAndRescueMsg.set_player_id(playerID);
  409. treatAndRescueMsg.set_to_player_id(mateID);
  410. return treatAndRescueMsg;
  411. }
  412. inline protobuf::MoveMsg THUAI62ProtobufMove(int64_t time, double angle, int64_t id)
  413. {
  414. protobuf::MoveMsg moveMsg;
  415. moveMsg.set_time_in_milliseconds(time);
  416. moveMsg.set_angle(angle);
  417. moveMsg.set_player_id(id);
  418. return moveMsg;
  419. }
  420. inline protobuf::PropMsg THUAI62ProtobufProp(THUAI6::PropType prop, int64_t id)
  421. {
  422. protobuf::PropMsg pickMsg;
  423. pickMsg.set_prop_type(propTypeDict[prop]);
  424. pickMsg.set_player_id(id);
  425. return pickMsg;
  426. }
  427. inline protobuf::SendMsg THUAI62ProtobufSend(std::string msg, int64_t toID, bool binary, int64_t id)
  428. {
  429. protobuf::SendMsg sendMsg;
  430. if (binary)
  431. sendMsg.set_binary_message(msg);
  432. else
  433. sendMsg.set_text_message(msg);
  434. sendMsg.set_to_player_id(toID);
  435. sendMsg.set_player_id(id);
  436. return sendMsg;
  437. }
  438. inline protobuf::AttackMsg THUAI62ProtobufAttack(double angle, int64_t id)
  439. {
  440. protobuf::AttackMsg attackMsg;
  441. attackMsg.set_angle(angle);
  442. attackMsg.set_player_id(id);
  443. return attackMsg;
  444. }
  445. inline protobuf::SkillMsg THUAI62ProtobufSkill(int32_t skillID, int32_t skillParam, int64_t id)
  446. {
  447. protobuf::SkillMsg skillMsg;
  448. skillMsg.set_skill_id(skillID);
  449. skillMsg.set_player_id(id);
  450. skillMsg.set_skill_param(skillParam);
  451. return skillMsg;
  452. }
  453. } // namespace THUAI62Proto
  454. namespace Time
  455. {
  456. inline double TimeSinceStart(const std::chrono::system_clock::time_point& sp)
  457. {
  458. std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
  459. std::chrono::duration<double, std::milli> time_span = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(tp - sp);
  460. return time_span.count();
  461. }
  462. } // namespace Time
  463. #endif