#pragma once #ifndef API_H #define API_H #ifdef _MSC_VER #pragma warning(disable : 4996) #endif #include #include #include #include #include #include #include #include #include #include "structures.h" const constexpr int num_of_grid_per_cell = 1000; class ILogic { // API中依赖Logic的部分 public: // 获取服务器发来的所有消息,要注意线程安全问题 virtual protobuf::MessageToClient GetFullMessage() = 0; // 供IAPI使用的操作相关的部分 virtual bool Move(protobuf::MoveMsg) = 0; virtual bool PickProp(protobuf::PickMsg) = 0; virtual bool UseProp(protobuf::IDMsg) = 0; virtual bool UseSkill(protobuf::IDMsg) = 0; virtual void SendMessage(protobuf::SendMsg) = 0; virtual bool HaveMessage(protobuf::IDMsg) = 0; virtual protobuf::MsgRes GetMessage(protobuf::IDMsg) = 0; virtual bool Escape(protobuf::IDMsg) = 0; // 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数 virtual bool StartFixMachine(protobuf::IDMsg) = 0; virtual bool EndFixMachine(protobuf::IDMsg) = 0; virtual bool GetFixStatus() = 0; virtual bool StartSaveHuman(protobuf::IDMsg) = 0; virtual bool EndSaveHuman(protobuf::IDMsg) = 0; virtual bool GetSaveStatus() = 0; virtual bool Attack(protobuf::AttackMsg) = 0; virtual bool CarryHuman(protobuf::IDMsg) = 0; virtual bool ReleaseHuman(protobuf::IDMsg) = 0; virtual bool HangHuman(protobuf::IDMsg) = 0; virtual bool WaitThread() = 0; virtual int GetCounter() = 0; }; class IAPI { public: // 选手可执行的操作,应当保证所有函数的返回值都应当为std::future,例如下面的移动函数: // 指挥本角色进行移动,`timeInMilliseconds` 为移动时间,单位为毫秒;`angleInRadian` 表示移动的方向,单位是弧度,使用极坐标——竖直向下方向为 x 轴,水平向右方向为 y 轴 virtual std::future Move(uint32_t timeInMilliseconds, double angleInRadian) = 0; // 向特定方向移动 virtual std::future MoveRight(uint32_t timeInMilliseconds) = 0; virtual std::future MoveUp(uint32_t timeInMilliseconds) = 0; virtual std::future MoveLeft(uint32_t timeInMilliseconds) = 0; virtual std::future MoveDown(uint32_t timeInMilliseconds) = 0; // 捡道具、使用技能 virtual std::future PickProp() = 0; virtual std::future UseProp() = 0; virtual std::future UseSkill() = 0; // 发送信息、接受信息 virtual std::future SendMessage(int, std::string) = 0; [[nodiscard]] virtual std::future HaveMessage() = 0; [[nodiscard]] virtual std::future> GetMessage() = 0; // 等待下一帧 virtual std::future Wait() = 0; // 获取视野内可见的人类/屠夫的信息 [[nodiscard]] virtual std::vector> GetHuman() const = 0; [[nodiscard]] virtual std::vector> GetButcher() const = 0; // 获取视野内可见的道具信息 [[nodiscard]] virtual std::vector> GetProps() const = 0; // 获取地图信息,视野外的地图统一为Land [[nodiscard]] virtual std::array, 50> GetFullMap() const = 0; [[nodiscard]] virtual THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const = 0; // 获取所有玩家的GUID [[nodiscard]] virtual const std::vector GetPlayerGUIDs() const = 0; // 获取游戏目前所进行的帧数 [[nodiscard]] virtual int GetFrameCount() const = 0; /*****人类阵营的特定函数*****/ virtual std::future StartFixMachine() = 0; virtual std::future EndFixMachine() = 0; virtual std::future GetFixStatus() = 0; virtual std::future StartSaveHuman() = 0; virtual std::future EndSaveHuman() = 0; virtual std::future GetSaveStatus() = 0; virtual std::future Escape() = 0; [[nodiscard]] virtual std::shared_ptr HumanGetSelfInfo() const = 0; /*****屠夫阵营的特定函数*****/ virtual std::future Attack(double angleInRadian) = 0; virtual std::future CarryHuman() = 0; virtual std::future ReleaseHuman() = 0; virtual std::future HangHuman() = 0; [[nodiscard]] virtual std::shared_ptr ButcherGetSelfInfo() const = 0; /*****选手可能用的辅助函数*****/ // 获取指定格子中心的坐标 [[nodiscard]] static inline int CellToGrid(int cell) noexcept { return cell * num_of_grid_per_cell + num_of_grid_per_cell / 2; } // 获取指定坐标点所位于的格子的 X 序号 [[nodiscard]] static inline int GridToCell(int grid) noexcept { return grid / num_of_grid_per_cell; } IAPI(ILogic& logic) : logic(logic) { } virtual ~IAPI() { } protected: ILogic& logic; }; // 给Logic使用的IAPI接口,为了保证面向对象的设计模式 class IAPIForLogic : public IAPI { public: IAPIForLogic(ILogic& logic) : IAPI(logic) { } virtual void StartTimer() = 0; virtual void EndTimer() = 0; }; class HumanAPI : public IAPIForLogic { public: HumanAPI(ILogic& logic) : IAPIForLogic(logic) { } }; class ButcherAPI : public IAPIForLogic { public: ButcherAPI(ILogic& logic) : IAPIForLogic(logic) { } }; // class IHumanAPI : public IAPIForLogic // { // public: // IHumanAPI(ILogic& logic) : // IAPIForLogic(logic) // { // } // virtual std::future StartFixMachine() = 0; // virtual std::future EndFixMachine() = 0; // virtual std::future GetFixStatus() = 0; // virtual std::future StartSaveHuman() = 0; // virtual std::future EndSaveHuman() = 0; // virtual std::future GetSaveStatus() = 0; // virtual std::future Escape() = 0; // [[nodiscard]] virtual std::shared_ptr GetSelfInfo() const = 0; // }; // class IButcherAPI : public IAPIForLogic // { // public: // IButcherAPI(Logic& logic) : // IAPIForLogic(logic) // { // } // virtual std::future Attack(double angleInRadian) = 0; // virtual std::future CarryHuman() = 0; // virtual std::future ReleaseHuman() = 0; // virtual std::future HangHuman() = 0; // [[nodiscard]] virtual std::shared_ptr GetSelfInfo() const = 0; // }; // class HumanAPI : public IHumanAPI // { // public: // HumanAPI(Logic& logic) : // IHumanAPI(logic) // { // } // }; // class DebugHumanAPI : public IHumanAPI // { // public: // DebugHumanAPI(Logic& logic) : // IHumanAPI(logic) // { // } // }; // class ButhcerAPI : public IButcherAPI // { // public: // ButhcerAPI(Logic& logic) : // IButcherAPI(logic) // { // } // }; // class DebugButcherAPI : public IButcherAPI // { // public: // DebugButcherAPI(Logic& logic) : // IButcherAPI(logic) // { // } // }; #endif