diff --git a/CAPI/API/include/AI.h b/CAPI/API/include/AI.h new file mode 100644 index 0000000..4fba3ad --- /dev/null +++ b/CAPI/API/include/AI.h @@ -0,0 +1,13 @@ +#pragma once +#ifndef AI_H +#define AI_H + +#include "API.h" + +class IAI +{ +public: + virtual void play(IAPI& appi) = 0; +}; + +#endif \ No newline at end of file diff --git a/CAPI/API/include/constants.h b/CAPI/API/include/constants.h new file mode 100644 index 0000000..735bd03 --- /dev/null +++ b/CAPI/API/include/constants.h @@ -0,0 +1,8 @@ +#pragma once +#ifndef CONSTANTS_H +#define CONSTANTS_H + +namespace Constants +{ +} +#endif \ No newline at end of file diff --git a/CAPI/API/include/logic.h b/CAPI/API/include/logic.h new file mode 100644 index 0000000..6851596 --- /dev/null +++ b/CAPI/API/include/logic.h @@ -0,0 +1,112 @@ +#pragma once + +#ifndef LOGIC_H +#define LOGIC_H + +#ifdef _MSC_VER +#pragma warning(disable : 4996) +#endif + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include "API.h" +#include "AI.h" + +// 封装了通信组件和对AI对象进行操作 +class Logic : public ILogic +{ +private: + // gRPC客户端的stub,所有与服务端之间的通信操作都需要基于stub完成。 + std::unique_ptr THUAI6Stub; + // ID、阵营记录 + int playerID; + THUAI6::PlayerType playerType; + + // 类型记录 + THUAI6::HumanType humanType; + THUAI6::ButcherType butcherType; + + // GUID信息 + std::vector playerGUIDs; + + // THUAI5中的通信组件可以完全被我们的stub取代,故无须再写 + + std::unique_ptr pAI; + + std::shared_ptr pAPI; + + std::thread tAI; + + mutable std::mutex mtxAI; + mutable std::mutex mtxState; + mutable std::mutex mtxBuffer; + + std::condition_variable cvBuffer; + std::condition_variable cvAI; + + // 信息队列目前可能会不用?具体待定 + + // 存储状态,分别是现在的状态和缓冲区的状态。 + State state[2]; + State* currentState; + State* bufferState; + + // 是否应该执行player() + std::atomic_bool AILoop = true; + + // buffer是否更新完毕 + bool bufferUpdated = true; + + // 是否可以启用当前状态 + bool currentStateAccessed = false; + + // 是否应当启动AI + bool AIStart = false; + + // 控制内容更新的变量 + std::atomic_bool freshed = false; + + // 所有API中声明的函数都需要在此处重写,先暂时略过,等到之后具体实现时再考虑 + + // 执行AI线程 + void PlayerWrapper(std::function player); + + // THUAI5中的一系列用于处理信息的函数可能也不会再用 + + // 将信息加载到buffer + void LoadBuffer(std::shared_ptr); + + // 解锁状态更新线程 + void UnBlockBuffer(); + + // 解锁AI线程 + void UnBlockAI(); + + // 更新状态 + void Update() noexcept; + + // 等待 + void Wait() noexcept; + +public: + // 构造函数还需要传更多参数,有待补充 + Logic(std::shared_ptr channel); + + ~Logic() = default; + + // Main函数同上 + void Main(); +}; + +#endif \ No newline at end of file diff --git a/CAPI/API/include/state.h b/CAPI/API/include/state.h new file mode 100644 index 0000000..f699d24 --- /dev/null +++ b/CAPI/API/include/state.h @@ -0,0 +1,27 @@ +#pragma once +#ifndef STATE_H +#define STATE_H + +#include + +#include "structures.h" + +// 存储场上的状态 +struct State +{ + uint32_t teamScore; + + // 自身信息,根据playerType的不同,可以调用的值也不同。 + std::shared_ptr humanSelf; + std::shared_ptr butcherSelf; + + std::vector> humans; + std::vector> butchers; + std::vector> props; + + THUAI6::PlaceType gamemap[51][51]; + + std::vector guids; +}; + +#endif \ No newline at end of file diff --git a/CAPI/API/src/AI.cpp b/CAPI/API/src/AI.cpp new file mode 100644 index 0000000..acaccd2 --- /dev/null +++ b/CAPI/API/src/AI.cpp @@ -0,0 +1,11 @@ +#include +#include +#include "AI.h" + +// 选手!!必须!!定义该变量来选择自己的阵营 +const THUAI6::PlayerType playerType = THUAI6::PlayerType::HumanPlayer; + +// 选手只需要定义两者中自己选中的那个即可,定义两个也不会有影响。 +const THUAI6::ButcherType butcherType = THUAI6::ButcherType::ButcherType1; + +const THUAI6::HumanType humanType = THUAI6::HumanType::HumanType1; diff --git a/CAPI/API/src/logic.cpp b/CAPI/API/src/logic.cpp new file mode 100644 index 0000000..9ef32c9 --- /dev/null +++ b/CAPI/API/src/logic.cpp @@ -0,0 +1,7 @@ +#pragma once +#include "logic.h" + +Logic::Logic(std::shared_ptr channel) : + THUAI6Stub(Protobuf::AvailableService::NewStub(channel)) +{ +} \ No newline at end of file diff --git a/CAPI/CMakeLists.txt b/CAPI/CMakeLists.txt index 6741c18..6b11ff0 100644 --- a/CAPI/CMakeLists.txt +++ b/CAPI/CMakeLists.txt @@ -5,6 +5,8 @@ project(THUAI6_CAPI VERSION 1.0) set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -pthread") + aux_source_directory(./API/src CPP_LIST) aux_source_directory(./proto PROTO_CPP_LIST)