-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ce35c7
commit 295ed40
Showing
24 changed files
with
12,209 additions
and
9,781 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
#ifndef AI_H | ||
#define AI_H | ||
|
||
#include "API.h" | ||
|
||
#undef GetMessage | ||
#undef SendMessage | ||
#undef PeekMessage | ||
|
||
class IAI | ||
{ | ||
public: | ||
virtual ~IAI() = default; | ||
IAI() = default; | ||
virtual void play(IShipAPI& api,int8_t playerTeam) = 0; | ||
virtual void play(IHomeAPI& api,int8_t playerTeam) = 0; | ||
}; | ||
|
||
using CreateAIFunc = std::unique_ptr<IAI> (*)(int64_t shipID); | ||
|
||
class AI : public IAI | ||
{ | ||
public: | ||
AI(int64_t pID) : | ||
IAI(), | ||
playerID(pID) | ||
{ | ||
} | ||
void play(IShipAPI& api,int8_t playerTeam) override; | ||
void play(IHomeAPI& api,int8_t playerTeam) override; | ||
|
||
private: | ||
int64_t shipID; | ||
int64_t homeID; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,291 @@ | ||
#pragma once | ||
#ifndef API_H | ||
#define API_H | ||
|
||
#ifdef _MSC_VER | ||
#pragma warning(disable : 4996) | ||
#endif | ||
|
||
#include "Message2Server.pb.h" | ||
#include "Message2Clients.pb.h" | ||
#include "MessageType.pb.h" | ||
#include "Services.grpc.pb.h" | ||
#include "Services.pb.h" | ||
#include <future> | ||
#include <iostream> | ||
#include <vector> | ||
#include <optional> | ||
|
||
#include <spdlog/spdlog.h> | ||
#include <spdlog/sinks/basic_file_sink.h> | ||
#include <spdlog/sinks/stdout_color_sinks.h> | ||
|
||
#include "structures.h" | ||
|
||
#undef GetMessage | ||
#undef SendMessage | ||
#undef PeekMessage | ||
|
||
const constexpr int32_t numOfGridPerCell = 1000; | ||
|
||
class IAI; | ||
|
||
class ILogic | ||
{ | ||
// API中依赖Logic的部分 | ||
|
||
public: | ||
// 获取服务器发来的消息 | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetShip() const = 0; | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetEnemyShip() const = 0; | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const = 0; | ||
[[nodiscard]] virtual std::shared_ptr<const THUAI7::Ship> ShipGetSelfInfo() const = 0; | ||
|
||
[[nodiscard]] virtual std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const = 0; | ||
[[nodiscard]] virtual THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const = 0; | ||
[[nodiscard]] virtual int32_t GetBuildingHp(int32_t cellX, int32_t cellY) const = 0; | ||
[[nodiscard]] virtual std::shared_ptr<const THUAI7::GameInfo> GetGameInfo() const = 0; | ||
|
||
// 供IAPI使用的操作相关的部分 | ||
virtual bool Move(int64_t time, double angle) = 0; | ||
virtual bool SendMessage(int64_t toID, std::string message, bool binary) = 0; | ||
virtual bool HaveMessage() = 0; | ||
virtual std::pair<int64_t, std::string> GetMessage() = 0; | ||
|
||
virtual bool WaitThread() = 0; | ||
|
||
virtual int32_t GetCounter() const = 0; | ||
|
||
// IShipAPI使用的部分 | ||
|
||
|
||
virtual bool StartRecovering() = 0; | ||
virtual bool StartRecycling() = 0; | ||
virtual bool StartProducing() = 0; | ||
virtual bool StartBuilding(int32_t cellX, int32_t cellY) = 0; | ||
virtual bool InstallModule(const THUAI7::Module module) = 0; | ||
virtual bool EndAllAction() = 0; | ||
virtual bool Attack(double angle) = 0; | ||
|
||
virtual std::vector<int64_t> GetShipGUIDs() const = 0; | ||
|
||
[[nodiscard]] virtual bool HaveView(int32_t gridX, int32_t gridY, int32_t selfX, int32_t selfY, int32_t viewRange) const = 0; | ||
}; | ||
|
||
class IAPI | ||
{ | ||
public: | ||
// 船可执行的操作,应当保证所有函数的返回值都应当为std::future,例如下面的移动函数: | ||
// 指挥船进行移动,`timeInMilliseconds` 为移动时间,单位为毫秒;`angleInRadian` 表示移动的方向,单位是弧度,使用极坐标——竖直向下方向为 x 轴,水平向右方向为 y 轴 | ||
virtual std::future<bool> Move(int64_t timeInMilliseconds, double angleInRadian) = 0; | ||
|
||
// 向特定方向移动 | ||
virtual std::future<bool> MoveRight(int64_t timeInMilliseconds) = 0; | ||
virtual std::future<bool> MoveUp(int64_t timeInMilliseconds) = 0; | ||
virtual std::future<bool> MoveLeft(int64_t timeInMilliseconds) = 0; | ||
virtual std::future<bool> MoveDown(int64_t timeInMilliseconds) = 0; | ||
|
||
// | ||
virtual std::future<bool> InstallCollectorModule(const THUAI7::CollectorType type) = 0; | ||
virtual std::future<bool> InstallArmorModule(const THUAI7::ArmorType type) = 0; | ||
virtual std::future<bool> InstallShieldModule(const THUAI7::ShieldType type)=0; | ||
virtual std::future<bool> InstallBuilderModule(const THUAI7::BuilderType type) = 0; | ||
virtual std::future<bool> InstallBulletModule(const THUAI7::BulletType type) = 0; | ||
virtual std::future<bool> Attack(double angleInRadian) = 0; | ||
virtual std::future<bool> EndAllAction() = 0; | ||
|
||
// 发送信息、接受信息,注意收消息时无消息则返回nullopt | ||
virtual std::future<bool> SendTextMessage(int64_t, std::string) = 0; | ||
virtual std::future<bool> SendBinaryMessage(int64_t, std::string) = 0; | ||
[[nodiscard]] virtual bool HaveMessage() = 0; | ||
[[nodiscard]] virtual std::pair<int64_t, std::string> GetMessage() = 0; | ||
|
||
// 等待下一帧 | ||
virtual bool Wait() = 0; | ||
|
||
// 获取视野内可见的信息 | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetShip() const = 0; | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetEnemyShip() const = 0; | ||
|
||
|
||
// 获取视野内可见的子弹信息 | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const = 0; | ||
|
||
[[nodiscard]] virtual std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const = 0; | ||
[[nodiscard]] virtual THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const = 0; | ||
[[nodiscard]] virtual int32_t GetBuildingHp(int32_t cellX, int32_t cellY) const = 0; | ||
|
||
|
||
[[nodiscard]] virtual std::shared_ptr<const THUAI7::GameInfo> GetGameInfo() const = 0; | ||
|
||
// 获取所有船的GUID | ||
[[nodiscard]] virtual std::vector<int64_t> GetShipGUIDs() const = 0; | ||
|
||
// 获取游戏目前所进行的帧数 | ||
[[nodiscard]] virtual int32_t GetFrameCount() const = 0; | ||
|
||
/*****选手可能用的辅助函数*****/ | ||
|
||
// 获取指定格子中心的坐标 | ||
[[nodiscard]] static inline int32_t CellToGrid(int32_t cell) noexcept | ||
{ | ||
return cell * numOfGridPerCell + numOfGridPerCell / 2; | ||
} | ||
|
||
// 获取指定坐标点所位于的格子的 X 序号 | ||
[[nodiscard]] static inline int32_t GridToCell(int32_t grid) noexcept | ||
{ | ||
return grid / numOfGridPerCell; | ||
} | ||
|
||
[[nodiscard]] virtual bool HaveView(int32_t gridX, int32_t gridY) const = 0; | ||
|
||
// 用于DEBUG的输出函数,选手仅在开启Debug模式的情况下可以使用 | ||
|
||
virtual void Print(std::string str) const = 0; | ||
virtual void PrintStudent() const = 0; | ||
virtual void PrintTricker() const = 0; | ||
virtual void PrintProp() const = 0; | ||
virtual void PrintSelfInfo() const = 0; | ||
}; | ||
|
||
|
||
|
||
class IShipAPI: public IAPI | ||
{ | ||
public: | ||
virtual std::future<bool> StartRecovering() = 0; | ||
virtual std::future<bool> StartRecycling() = 0; | ||
virtual std::future<bool> StartProducing() = 0; | ||
virtual std::future<bool> StartBuilding(int32_t cellX, int32_t cellY) = 0; | ||
[[nodiscard]] virtual std::shared_ptr<const THUAI7::Ship> GetSelfInfo() const = 0; | ||
} | ||
|
||
|
||
|
||
class IGameTimer | ||
{ | ||
public: | ||
virtual ~IGameTimer() = default; | ||
virtual void StartTimer() = 0; | ||
virtual void EndTimer() = 0; | ||
virtual void Play(IAI& ai) = 0; | ||
}; | ||
|
||
class ShipAPI: public IShipAPI, public IGameTimer | ||
{ | ||
ShipAPI(ILogic& logic) : | ||
logic(logic) | ||
{ | ||
} | ||
void StartTimer() override {} | ||
void EndTimer() override {} | ||
void Play(IAI& ai) override; | ||
[[nodiscard]] int32_t GetFrameCount() const override; | ||
|
||
|
||
std::future<bool> Move(int64_t timeInMilliseconds, double angleInRadian) override; | ||
std::future<bool> MoveRight(int64_t timeInMilliseconds) override; | ||
std::future<bool> MoveUp(int64_t timeInMilliseconds) override; | ||
std::future<bool> MoveLeft(int64_t timeInMilliseconds) override; | ||
std::future<bool> MoveDown(int64_t timeInMilliseconds) override; | ||
std::future<bool> InstallCollectorModule(const THUAI7::CollectorType type) override; | ||
std::future<bool> InstallArmorModule(const THUAI7::ArmorType type) override; | ||
std::future<bool> InstallShieldModule(const THUAI7::ShieldType type) override; | ||
std::future<bool> InstallBuilderModule(const THUAI7::BuilderType type) override; | ||
std::future<bool> InstallBulletModule(const THUAI7::BulletType type) override; | ||
std::future<bool> Attack(double angleInRadian) override; | ||
std::future<bool> EndAllAction() override; | ||
|
||
std::future<bool> SendTextMessage(int64_t, std::string) override; | ||
std::future<bool> SendBinaryMessage(int64_t, std::string) override; | ||
[[nodiscard]] bool HaveMessage() override; | ||
[[nodiscard]] std::pair<int64_t, std::string> GetMessage() override; | ||
|
||
bool Wait() override; | ||
|
||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetShip() const override; | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetEnemyShip() const override; | ||
[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const override; | ||
[[nodiscard]] virtual std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const override; | ||
[[nodiscard]] virtual THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const override; | ||
[[nodiscard]] virtual int32_t GetBuildingHp(int32_t cellX, int32_t cellY) const override; | ||
|
||
[[nodiscard]] virtual std::shared_ptr<const THUAI7::GameInfo> GetGameInfo() const override; | ||
|
||
[[nodiscard]] virtual std::vector<int64_t> GetShipGUIDs() const override; | ||
[[nodiscard]] virtual std::shared_ptr<const THUAI7::Ship> GetSelfInfo() const override; | ||
|
||
[[nodiscard]] bool HaveView(int32_t gridX, int32_t gridY) const override; | ||
|
||
void Print(std::string str) const override {} | ||
void PrintStudent() const override {} | ||
void PrintTricker() const override {} | ||
void PrintProp() const override {} | ||
void PrintSelfInfo() const override {} | ||
|
||
private: | ||
ILogic& logic; | ||
} | ||
|
||
|
||
|
||
|
||
class ShipDebugAPI: public IShipAPI, public IGameTimer | ||
{ | ||
ShipDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t shipID); | ||
void StartTimer() override {} | ||
void EndTimer() override {} | ||
void Play(IAI& ai) override; | ||
[[nodiscard]] int32_t GetFrameCount() const override; | ||
|
||
|
||
std::future<bool> Move(int64_t timeInMilliseconds, double angleInRadian) override; | ||
std::future<bool> MoveRight(int64_t timeInMilliseconds) override; | ||
std::future<bool> MoveUp(int64_t timeInMilliseconds) override; | ||
std::future<bool> MoveLeft(int64_t timeInMilliseconds) override; | ||
std::future<bool> MoveDown(int64_t timeInMilliseconds) override; | ||
std::future<bool> InstallCollectorModule(const THUAI7::CollectorType type) override; | ||
std::future<bool> InstallArmorModule(const THUAI7::ArmorType type) override; | ||
std::future<bool> InstallShieldModule(const THUAI7::ShieldType type) override; | ||
std::future<bool> InstallBuilderModule(const THUAI7::BuilderType type) override; | ||
std::future<bool> InstallBulletModule(const THUAI7::BulletType type) override; | ||
std::future<bool> Attack(double angleInRadian) override; | ||
std::future<bool> EndAllAction() override; | ||
|
||
std::future<bool> SendTextMessage(int64_t, std::string) override; | ||
std::future<bool> SendBinaryMessage(int64_t, std::string) override; | ||
[[nodiscard]] bool HaveMessage() override; | ||
[[nodiscard]] std::pair<int64_t, std::string> GetMessage() override; | ||
|
||
bool Wait() override; | ||
|
||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetShip() const override; | ||
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI7::Ship>> GetEnemyShip() const override; | ||
[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const override; | ||
[[nodiscard]] virtual std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const override; | ||
[[nodiscard]] virtual THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const override; | ||
[[nodiscard]] virtual int32_t GetBuildingHp(int32_t cellX, int32_t cellY) const override; | ||
|
||
[[nodiscard]] virtual std::shared_ptr<const THUAI7:GameInfo> GetGameInfo() const override; | ||
|
||
[[nodiscard]] virtual std::vector<int64_t> GetShipGUIDs() const override; | ||
[[nodiscard]] virtual std::shared_ptr<const THUAI7::Ship> GetSelfInfo() const override; | ||
|
||
[[nodiscard]] bool HaveView(int32_t gridX, int32_t gridY) const override; | ||
|
||
void Print(std::string str) const override {} | ||
void PrintStudent() const override {} | ||
void PrintTricker() const override {} | ||
void PrintProp() const override {} | ||
void PrintSelfInfo() const override {} | ||
|
||
private: | ||
std::chrono::system_clock::time_point startPoint; | ||
std::unique_ptr<spdlog::logger> logger; | ||
ILogic& logic; | ||
} | ||
|
||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef COMMUNICATION_H | ||
#define COMMUNICATION_H | ||
|
||
#include "Message2Server.pb.h" | ||
#include "Message2Clients.pb.h" | ||
#include "MessageType.pb.h" | ||
#include "Services.grpc.pb.h" | ||
#include "Services.pb.h" | ||
#include <grpcpp/grpcpp.h> | ||
#include "structures.h" | ||
#include <thread> | ||
#include <mutex> | ||
#include <condition_variable> | ||
#include <queue> | ||
#include <atomic> | ||
|
||
#undef GetMessage | ||
#undef SendMessage | ||
#undef PeekMessage | ||
|
||
class Logic; | ||
|
||
class Communication | ||
{ | ||
public: | ||
Communication(std::string sIP, std::string sPort); | ||
~Communication() | ||
{ | ||
} | ||
bool TryConnection(int64_t shipID); | ||
protobuf::MessageToClient GetMessage2Client(); | ||
void AddShip(HUAI7::PlayerTeam team, int64_t shipID, THUAI7::ShipType shipType); | ||
|
||
//船 | ||
bool Move(int64_t time, double angle, int64_t shipID); | ||
bool Recover(int64_t shipID); | ||
bool Produce(int64_t shipID, int32_t x, int32_t y); | ||
bool Rebuild(int64_t shipID, int32_t x, int32_t y); | ||
bool Construct(int64_t shipID, int32_t x, int32_t y); | ||
bool Attack(double angle, int64_t shipID); | ||
|
||
//大本营 | ||
bool InstallModule(THUAI7::Module module, int64_t shipID); | ||
bool BuildShip(int32_t x, int32_t y, THUAI7::ShipType shipType) | ||
bool EndAllAction(int64_t shipID); | ||
bool SendMessage(int64_t toID, std::string message, bool binary, int64_t shipID); | ||
bool Wait() = 0; | ||
|
||
private: | ||
std::unique_ptr<protobuf::AvailableService::Stub> THUAI7Stub; | ||
bool haveNewMessage = false; | ||
protobuf::MessageToClient message2Client; | ||
std::mutex mtxMessage; | ||
std::mutex mtxLimit; | ||
int32_t counter; | ||
int32_t counterMove; | ||
static constexpr const int32_t limit = 50; | ||
static constexpr const int32_t moveLimit = 10; | ||
std::condition_variable cvMessage; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.