// 杂项函数 #pragma once #ifndef UTILS_HPP #define UTILS_HPP #include #include "Message2Clients.pb.h" #include "Message2Server.pb.h" #include "MessageType.pb.h" #include "structures.h" namespace AssistFunction { constexpr int numOfGridPerCell = 100; [[nodiscard]] constexpr inline int GridToCell(int grid) noexcept { return grid / numOfGridPerCell; } inline bool HaveView(int viewRange, int x, int y, int newX, int newY, THUAI6::PlaceType myPlace, THUAI6::PlaceType newPlace, std::vector>& map) { int deltaX = newX - x; int deltaY = newY - y; double distance = deltaX * deltaX + deltaY * deltaY; if (newPlace == THUAI6::PlaceType::Grass && myPlace != THUAI6::PlaceType::Grass) // 草丛外必不可能看到草丛内 return false; if (distance < viewRange * viewRange) { int divide = std::max(std::abs(deltaX), std::abs(deltaY)) / 100; double dx = deltaX / divide; double dy = deltaY / divide; double myX = double(x); double myY = double(y); if (newPlace == THUAI6::PlaceType::Grass && myPlace == THUAI6::PlaceType::Grass) // 都在草丛内,要另作判断 for (int i = 0; i < divide; i++) { myX += dx; myY += dy; if (map[GridToCell(myX)][GridToCell(myY)] != THUAI6::PlaceType::Grass) return false; } else // 不在草丛内,只需要没有墙即可 for (int i = 0; i < divide; i++) { myX += dx; myY += dy; if (map[GridToCell(myX)][GridToCell(myY)] == THUAI6::PlaceType::Wall) return false; } return true; } else return false; } } // namespace AssistFunction // 辅助函数,用于将proto信息转换为THUAI6信息 namespace Proto2THUAI6 { // 用于将Protobuf中的枚举转换为THUAI6的枚举 inline std::map placeTypeDict{ {protobuf::PlaceType::NULL_PLACE_TYPE, THUAI6::PlaceType::NullPlaceType}, {protobuf::PlaceType::LAND, THUAI6::PlaceType::Land}, {protobuf::PlaceType::WALL, THUAI6::PlaceType::Wall}, {protobuf::PlaceType::GRASS, THUAI6::PlaceType::Grass}, {protobuf::PlaceType::CLASSROOM, THUAI6::PlaceType::ClassRoom}, {protobuf::PlaceType::GATE, THUAI6::PlaceType::Gate}, {protobuf::PlaceType::HIDDEN_GATE, THUAI6::PlaceType::HiddenGate}, {protobuf::PlaceType::WINDOW, THUAI6::PlaceType::Window}, {protobuf::PlaceType::DOOR3, THUAI6::PlaceType::Door3}, {protobuf::PlaceType::DOOR5, THUAI6::PlaceType::Door5}, {protobuf::PlaceType::DOOR6, THUAI6::PlaceType::Door6}, {protobuf::PlaceType::CHEST, THUAI6::PlaceType::Chest}, }; inline std::map shapeTypeDict{ {protobuf::ShapeType::NULL_SHAPE_TYPE, THUAI6::ShapeType::NullShapeType}, {protobuf::ShapeType::CIRCLE, THUAI6::ShapeType::Circle}, {protobuf::ShapeType::SQUARE, THUAI6::ShapeType::Square}, }; inline std::map propTypeDict{ {protobuf::PropType::NULL_PROP_TYPE, THUAI6::PropType::NullPropType}, {protobuf::PropType::KEY3, THUAI6::PropType::Key3}, {protobuf::PropType::KEY5, THUAI6::PropType::Key5}, {protobuf::PropType::KEY6, THUAI6::PropType::Key6}, {protobuf::PropType::ADD_SPEED, THUAI6::PropType::AddSpeed}, {protobuf::PropType::ADD_HP_OR_AP, THUAI6::PropType::AddHpOrAp}, {protobuf::PropType::ADD_LIFE_OR_AP, THUAI6::PropType::AddLifeOrAp}, {protobuf::PropType::SHIELD_OR_SPEAR, THUAI6::PropType::ShieldOrSpear}, }; inline std::map playerTypeDict{ {protobuf::PlayerType::NULL_PLAYER_TYPE, THUAI6::PlayerType::NullPlayerType}, {protobuf::PlayerType::STUDENT_PLAYER, THUAI6::PlayerType::StudentPlayer}, {protobuf::PlayerType::TRICKER_PLAYER, THUAI6::PlayerType::TrickerPlayer}, }; inline std::map studentTypeDict{ {protobuf::StudentType::NULL_STUDENT_TYPE, THUAI6::StudentType::NullStudentType}, {protobuf::StudentType::ATHLETE, THUAI6::StudentType::Athlete}, {protobuf::StudentType::STUDENTTYPE2, THUAI6::StudentType::StudentType2}, {protobuf::StudentType::STUDENTTYPE3, THUAI6::StudentType::StudentType3}, {protobuf::StudentType::STUDENTTYPE4, THUAI6::StudentType::StudentType4}, }; inline std::map trickerTypeDict{ {protobuf::TrickerType::NULL_TRICKER_TYPE, THUAI6::TrickerType::NullTrickerType}, {protobuf::TrickerType::ASSASSIN, THUAI6::TrickerType::Assassin}, {protobuf::TrickerType::TRICKERTYPE2, THUAI6::TrickerType::TrickerType2}, {protobuf::TrickerType::TRICKERTYPE3, THUAI6::TrickerType::TrickerType3}, {protobuf::TrickerType::TRICKERTYPE4, THUAI6::TrickerType::TrickerType4}, }; inline std::map studentBuffTypeDict{ {protobuf::StudentBuffType::NULL_SBUFF_TYPE, THUAI6::StudentBuffType::NullStudentBuffType}, {protobuf::StudentBuffType::SBUFFTYPE1, THUAI6::StudentBuffType::StudentBuffType1}, {protobuf::StudentBuffType::SBUFFTYPE2, THUAI6::StudentBuffType::StudentBuffType2}, {protobuf::StudentBuffType::SBUFFTYPE3, THUAI6::StudentBuffType::StudentBuffType3}, {protobuf::StudentBuffType::SBUFFTYPE4, THUAI6::StudentBuffType::StudentBuffType4}, }; inline std::map trickerBuffTypeDict{ {protobuf::TrickerBuffType::NULL_TBUFF_TYPE, THUAI6::TrickerBuffType::NullTrickerBuffType}, {protobuf::TrickerBuffType::TBUFFTYPE1, THUAI6::TrickerBuffType::TrickerBuffType1}, {protobuf::TrickerBuffType::TBUFFTYPE2, THUAI6::TrickerBuffType::TrickerBuffType2}, {protobuf::TrickerBuffType::TBUFFTYPE3, THUAI6::TrickerBuffType::TrickerBuffType3}, {protobuf::TrickerBuffType::TBUFFTYPE4, THUAI6::TrickerBuffType::TrickerBuffType4}, }; inline std::map playerStateDict{ {protobuf::PlayerState::NULL_STATUS, THUAI6::PlayerState::NullState}, {protobuf::PlayerState::IDLE, THUAI6::PlayerState::Idle}, {protobuf::PlayerState::LEARNING, THUAI6::PlayerState::Learning}, {protobuf::PlayerState::ADDICTED, THUAI6::PlayerState::Addicted}, {protobuf::PlayerState::QUIT, THUAI6::PlayerState::Quit}, {protobuf::PlayerState::GRADUATED, THUAI6::PlayerState::Graduated}, {protobuf::PlayerState::RESCUED, THUAI6::PlayerState::Rescued}, {protobuf::PlayerState::TREATED, THUAI6::PlayerState::Treated}, {protobuf::PlayerState::STUNNED, THUAI6::PlayerState::Stunned}, {protobuf::PlayerState::RESCUING, THUAI6::PlayerState::Rescuing}, {protobuf::PlayerState::TREATING, THUAI6::PlayerState::Treating}, {protobuf::PlayerState::SWINGING, THUAI6::PlayerState::Swinging}, {protobuf::PlayerState::ATTACKING, THUAI6::PlayerState::Attacking}, {protobuf::PlayerState::LOCKING, THUAI6::PlayerState::Locking}, {protobuf::PlayerState::RUMMAGING, THUAI6::PlayerState::Rummaging}, {protobuf::PlayerState::CLIMBING, THUAI6::PlayerState::Climbing}, {protobuf::PlayerState::OPENING_A_CHEST, THUAI6::PlayerState::OpeningAChest}, {protobuf::PlayerState::USING_SPECIAL_SKILL, THUAI6::PlayerState::UsingSpecialSkill}, {protobuf::PlayerState::OPENING_A_GATE, THUAI6::PlayerState::OpeningAGate}, }; inline std::map gameStateDict{ {protobuf::GameState::NULL_GAME_STATE, THUAI6::GameState::NullGameState}, {protobuf::GameState::GAME_START, THUAI6::GameState::GameStart}, {protobuf::GameState::GAME_RUNNING, THUAI6::GameState::GameRunning}, {protobuf::GameState::GAME_END, THUAI6::GameState::GameEnd}, }; inline std::map bulletTypeDict{ {protobuf::BulletType::NULL_BULLET_TYPE, THUAI6::BulletType::NullBulletType}, {protobuf::BulletType::FLYING_KNIFE, THUAI6::BulletType::FlyingKnife}, {protobuf::BulletType::COMMON_ATTACK_OF_TRICKER, THUAI6::BulletType::CommonAttackOfTricker}, {protobuf::BulletType::FAST_BULLET, THUAI6::BulletType::FastBullet}, {protobuf::BulletType::ORDINARY_BULLET, THUAI6::BulletType::OrdinaryBullet}, {protobuf::BulletType::ATOM_BOMB, THUAI6::BulletType::AtomBomb}, }; inline std::map messageOfObjDict{ {protobuf::MessageOfObj::MessageOfObjCase::kStudentMessage, THUAI6::MessageOfObj::StudentMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kTrickerMessage, THUAI6::MessageOfObj::TrickerMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kPropMessage, THUAI6::MessageOfObj::PropMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kBulletMessage, THUAI6::MessageOfObj::BulletMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kBombedBulletMessage, THUAI6::MessageOfObj::BombedBulletMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kClassroomMessage, THUAI6::MessageOfObj::ClassroomMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kDoorMessage, THUAI6::MessageOfObj::DoorMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kGateMessage, THUAI6::MessageOfObj::GateMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kChestMessage, THUAI6::MessageOfObj::ChestMessage}, {protobuf::MessageOfObj::MessageOfObjCase::MESSAGE_OF_OBJ_NOT_SET, THUAI6::MessageOfObj::NullMessageOfObj}, {protobuf::MessageOfObj::MessageOfObjCase::kMapMessage, THUAI6::MessageOfObj::MapMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kNewsMessage, THUAI6::MessageOfObj::NewsMessage}, {protobuf::MessageOfObj::MessageOfObjCase::kHiddenGateMessage, THUAI6::MessageOfObj::HiddenGateMessage}, }; // 用于将Protobuf中的类转换为THUAI6的类 inline std::shared_ptr Protobuf2THUAI6Tricker(const protobuf::MessageOfTricker& trickerMsg) { auto tricker = std::make_shared(); tricker->x = trickerMsg.x(); tricker->y = trickerMsg.y(); tricker->speed = trickerMsg.speed(); tricker->score = trickerMsg.score(); tricker->facingDirection = trickerMsg.facing_direction(); tricker->bulletType = bulletTypeDict[trickerMsg.bullet_type()]; tricker->trickDesire = trickerMsg.trick_desire(); tricker->classVolume = trickerMsg.class_volume(); for (int i = 0; i < trickerMsg.time_until_skill_available().size(); i++) tricker->timeUntilSkillAvailable.push_back(trickerMsg.time_until_skill_available(i)); tricker->place = placeTypeDict[trickerMsg.place()]; tricker->playerState = playerStateDict[trickerMsg.player_state()]; for (int i = 0; i < trickerMsg.prop().size(); i++) tricker->props.push_back(propTypeDict[trickerMsg.prop(i)]); tricker->trickerType = trickerTypeDict[trickerMsg.tricker_type()]; tricker->guid = trickerMsg.guid(); tricker->playerID = trickerMsg.player_id(); tricker->viewRange = trickerMsg.view_range(); tricker->radius = trickerMsg.radius(); tricker->buff.clear(); for (int i = 0; i < trickerMsg.buff().size(); i++) tricker->buff.push_back(trickerBuffTypeDict[trickerMsg.buff(i)]); return tricker; } inline std::shared_ptr Protobuf2THUAI6Student(const protobuf::MessageOfStudent& studentMsg) { auto student = std::make_shared(); student->x = studentMsg.x(); student->y = studentMsg.y(); student->speed = studentMsg.speed(); student->viewRange = studentMsg.view_range(); student->playerID = studentMsg.player_id(); student->guid = studentMsg.guid(); student->radius = studentMsg.radius(); student->score = studentMsg.score(); student->facingDirection = studentMsg.facing_direction(); student->bulletType = bulletTypeDict[studentMsg.bullet_type()]; student->learningSpeed = studentMsg.learning_speed(); student->treatSpeed = studentMsg.treat_speed(); student->treatProgress = studentMsg.treat_progress(); student->rescueProgress = studentMsg.rescue_progress(); student->dangerAlert = studentMsg.danger_alert(); for (int i = 0; i < studentMsg.time_until_skill_available().size(); i++) student->timeUntilSkillAvailable.push_back(studentMsg.time_until_skill_available(i)); student->playerType = THUAI6::PlayerType::StudentPlayer; for (int i = 0; i < studentMsg.prop().size(); i++) student->props.push_back(propTypeDict[studentMsg.prop(i)]); student->place = placeTypeDict[studentMsg.place()]; student->playerState = playerStateDict[studentMsg.player_state()]; student->determination = studentMsg.determination(); student->addiction = studentMsg.addiction(); student->studentType = studentTypeDict[studentMsg.student_type()]; student->buff.clear(); for (int i = 0; i < studentMsg.buff_size(); i++) student->buff.push_back(studentBuffTypeDict[studentMsg.buff(i)]); return student; } inline std::shared_ptr Protobuf2THUAI6Prop(const protobuf::MessageOfProp& propMsg) { auto prop = std::make_shared(); prop->x = propMsg.x(); prop->y = propMsg.y(); prop->type = propTypeDict[propMsg.type()]; prop->place = placeTypeDict[propMsg.place()]; prop->guid = propMsg.guid(); prop->facingDirection = propMsg.facing_direction(); return prop; } inline std::shared_ptr Protobuf2THUAI6GameInfo(const protobuf::MessageOfAll& allMsg) { auto gameInfo = std::make_shared(); gameInfo->gameTime = allMsg.game_time(); gameInfo->subjectLeft = allMsg.subject_left(); gameInfo->studentGraduated = allMsg.student_graduated(); gameInfo->studentQuited = allMsg.student_quited(); gameInfo->studentScore = allMsg.student_score(); gameInfo->trickerScore = allMsg.tricker_score(); return gameInfo; } inline std::shared_ptr Protobuf2THUAI6Bullet(const protobuf::MessageOfBullet& bulletMsg) { auto bullet = std::make_shared(); bullet->bulletType = bulletTypeDict[bulletMsg.type()]; bullet->x = bulletMsg.x(); bullet->y = bulletMsg.y(); bullet->facingDirection = bulletMsg.facing_direction(); bullet->guid = bulletMsg.guid(); bullet->team = playerTypeDict[bulletMsg.team()]; bullet->place = placeTypeDict[bulletMsg.place()]; bullet->bombRange = bulletMsg.bomb_range(); bullet->speed = bulletMsg.speed(); return bullet; } inline std::shared_ptr Protobuf2THUAI6BombedBullet(const protobuf::MessageOfBombedBullet& bombedBulletMsg) { auto bombedBullet = std::make_shared(); bombedBullet->bulletType = bulletTypeDict[bombedBulletMsg.type()]; bombedBullet->x = bombedBulletMsg.x(); bombedBullet->y = bombedBulletMsg.y(); bombedBullet->facingDirection = bombedBulletMsg.facing_direction(); bombedBullet->mappingID = bombedBulletMsg.mapping_id(); bombedBullet->bombRange = bombedBulletMsg.bomb_range(); return bombedBullet; } inline THUAI6::HiddenGateState Bool2HiddenGateState(bool gateMsg) { if (gateMsg) return THUAI6::HiddenGateState::Opened; else return THUAI6::HiddenGateState::Refreshed; } } // namespace Proto2THUAI6 namespace THUAI62Proto { // 用于将THUAI6的枚举转换为Protobuf的枚举 inline std::map placeTypeDict{ {THUAI6::PlaceType::NullPlaceType, protobuf::PlaceType::NULL_PLACE_TYPE}, {THUAI6::PlaceType::Land, protobuf::PlaceType::LAND}, {THUAI6::PlaceType::Wall, protobuf::PlaceType::WALL}, {THUAI6::PlaceType::Grass, protobuf::PlaceType::GRASS}, {THUAI6::PlaceType::ClassRoom, protobuf::PlaceType::CLASSROOM}, {THUAI6::PlaceType::Gate, protobuf::PlaceType::GATE}, {THUAI6::PlaceType::HiddenGate, protobuf::PlaceType::HIDDEN_GATE}, {THUAI6::PlaceType::Door3, protobuf::PlaceType::DOOR3}, {THUAI6::PlaceType::Door5, protobuf::PlaceType::DOOR5}, {THUAI6::PlaceType::Door6, protobuf::PlaceType::DOOR6}, {THUAI6::PlaceType::Window, protobuf::PlaceType::WINDOW}, }; inline std::map shapeTypeDict{ {THUAI6::ShapeType::NullShapeType, protobuf::ShapeType::NULL_SHAPE_TYPE}, {THUAI6::ShapeType::Circle, protobuf::ShapeType::CIRCLE}, {THUAI6::ShapeType::Square, protobuf::ShapeType::SQUARE}, }; inline std::map propTypeDict{ {THUAI6::PropType::NullPropType, protobuf::PropType::NULL_PROP_TYPE}, {THUAI6::PropType::Key3, protobuf::PropType::KEY3}, {THUAI6::PropType::Key5, protobuf::PropType::KEY5}, {THUAI6::PropType::Key6, protobuf::PropType::KEY6}, {THUAI6::PropType::AddHpOrAp, protobuf::PropType::ADD_HP_OR_AP}, {THUAI6::PropType::AddLifeOrAp, protobuf::PropType::ADD_LIFE_OR_AP}, {THUAI6::PropType::AddSpeed, protobuf::PropType::ADD_SPEED}, {THUAI6::PropType::ShieldOrSpear, protobuf::PropType::SHIELD_OR_SPEAR}, }; inline std::map playerTypeDict{ {THUAI6::PlayerType::NullPlayerType, protobuf::PlayerType::NULL_PLAYER_TYPE}, {THUAI6::PlayerType::StudentPlayer, protobuf::PlayerType::STUDENT_PLAYER}, {THUAI6::PlayerType::TrickerPlayer, protobuf::PlayerType::TRICKER_PLAYER}, }; inline std::map studentTypeDict{ {THUAI6::StudentType::NullStudentType, protobuf::StudentType::NULL_STUDENT_TYPE}, {THUAI6::StudentType::Athlete, protobuf::StudentType::ATHLETE}, {THUAI6::StudentType::StudentType2, protobuf::StudentType::STUDENTTYPE2}, {THUAI6::StudentType::StudentType3, protobuf::StudentType::STUDENTTYPE3}, {THUAI6::StudentType::StudentType4, protobuf::StudentType::STUDENTTYPE4}, }; inline std::map studentBuffTypeDict{ {THUAI6::StudentBuffType::NullStudentBuffType, protobuf::StudentBuffType::NULL_SBUFF_TYPE}, {THUAI6::StudentBuffType::StudentBuffType1, protobuf::StudentBuffType::SBUFFTYPE1}, {THUAI6::StudentBuffType::StudentBuffType2, protobuf::StudentBuffType::SBUFFTYPE2}, {THUAI6::StudentBuffType::StudentBuffType3, protobuf::StudentBuffType::SBUFFTYPE3}, {THUAI6::StudentBuffType::StudentBuffType4, protobuf::StudentBuffType::SBUFFTYPE4}, }; inline std::map trickerTypeDict{ {THUAI6::TrickerType::NullTrickerType, protobuf::TrickerType::NULL_TRICKER_TYPE}, {THUAI6::TrickerType::Assassin, protobuf::TrickerType::ASSASSIN}, {THUAI6::TrickerType::TrickerType2, protobuf::TrickerType::TRICKERTYPE2}, {THUAI6::TrickerType::TrickerType3, protobuf::TrickerType::TRICKERTYPE3}, {THUAI6::TrickerType::TrickerType4, protobuf::TrickerType::TRICKERTYPE4}, }; inline std::map trickerBuffTypeDict{ {THUAI6::TrickerBuffType::NullTrickerBuffType, protobuf::TrickerBuffType::NULL_TBUFF_TYPE}, {THUAI6::TrickerBuffType::TrickerBuffType1, protobuf::TrickerBuffType::TBUFFTYPE1}, {THUAI6::TrickerBuffType::TrickerBuffType2, protobuf::TrickerBuffType::TBUFFTYPE2}, {THUAI6::TrickerBuffType::TrickerBuffType3, protobuf::TrickerBuffType::TBUFFTYPE3}, {THUAI6::TrickerBuffType::TrickerBuffType4, protobuf::TrickerBuffType::TBUFFTYPE4}, }; // 用于将THUAI6的类转换为Protobuf的消息 inline protobuf::PlayerMsg THUAI62ProtobufPlayer(int64_t playerID, THUAI6::PlayerType playerType, THUAI6::StudentType studentType, THUAI6::TrickerType trickerType) { protobuf::PlayerMsg playerMsg; playerMsg.set_player_id(playerID); playerMsg.set_player_type(playerTypeDict[playerType]); if (playerType == THUAI6::PlayerType::StudentPlayer) { playerMsg.set_student_type(studentTypeDict[studentType]); } else if (playerType == THUAI6::PlayerType::TrickerPlayer) { playerMsg.set_tricker_type(trickerTypeDict[trickerType]); } return playerMsg; } inline protobuf::IDMsg THUAI62ProtobufID(int playerID) { protobuf::IDMsg idMsg; idMsg.set_player_id(playerID); return idMsg; } inline protobuf::TreatAndRescueMsg THUAI62ProtobufTreatAndRescue(int64_t playerID, int64_t mateID) { protobuf::TreatAndRescueMsg treatAndRescueMsg; treatAndRescueMsg.set_player_id(playerID); treatAndRescueMsg.set_to_player_id(mateID); return treatAndRescueMsg; } inline protobuf::MoveMsg THUAI62ProtobufMove(int64_t time, double angle, int64_t id) { protobuf::MoveMsg moveMsg; moveMsg.set_time_in_milliseconds(time); moveMsg.set_angle(angle); moveMsg.set_player_id(id); return moveMsg; } inline protobuf::PropMsg THUAI62ProtobufProp(THUAI6::PropType prop, int64_t id) { protobuf::PropMsg pickMsg; pickMsg.set_prop_type(propTypeDict[prop]); pickMsg.set_player_id(id); return pickMsg; } inline protobuf::SendMsg THUAI62ProtobufSend(std::string msg, int64_t toID, int64_t id) { protobuf::SendMsg sendMsg; sendMsg.set_message(msg); sendMsg.set_to_player_id(toID); sendMsg.set_player_id(id); return sendMsg; } inline protobuf::AttackMsg THUAI62ProtobufAttack(double angle, int64_t id) { protobuf::AttackMsg attackMsg; attackMsg.set_angle(angle); attackMsg.set_player_id(id); return attackMsg; } inline protobuf::SkillMsg THUAI62ProtobufSkill(int32_t skillID, int64_t id) { protobuf::SkillMsg skillMsg; skillMsg.set_skill_id(skillID); skillMsg.set_player_id(id); return skillMsg; } } // namespace THUAI62Proto namespace Time { inline double TimeSinceStart(const std::chrono::system_clock::time_point& sp) { std::chrono::system_clock::time_point tp = std::chrono::system_clock::now(); std::chrono::duration time_span = std::chrono::duration_cast>(tp - sp); return time_span.count(); } } // namespace Time #endif