Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CAPI/API/include/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class Logic : public ILogic
{
private:
// gRPC客户端的stub,所有与服务端之间的通信操作都需要基于stub完成。
// 通信组件
std::unique_ptr<Communication> pComm;
// ID、阵营记录
int64_t playerID;
Expand Down Expand Up @@ -148,7 +148,7 @@ class Logic : public ILogic
}

// Main函数同上
void Main(CreateAIFunc createAI, std::string IP, std::string port);
void Main(CreateAIFunc createAI, std::string IP, std::string port, bool level, std::string filename);
};

#endif
36 changes: 19 additions & 17 deletions CAPI/API/src/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,19 @@ void Logic::LoadBuffer(protobuf::MessageToClient& message)
bufferState->gamemap = Proto2THUAI6::Protobuf2THUAI6Map(message.map_message());
if (playerType == THUAI6::PlayerType::HumanPlayer)
{
for (auto itr = message.human_message().begin(); itr != message.human_message().end(); itr++)
for (auto item : message.human_message())

@Timothy-Liuxf Timothy-Liuxf Jan 5, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

- for (auto item : message.human_message())
+ for (const auto& item : message.human_message())

{
if (itr->player_id() == playerID)
if (item.player_id() == playerID)
{
bufferState->humanSelf = Proto2THUAI6::Protobuf2THUAI6Human(*itr);
bufferState->humanSelf = Proto2THUAI6::Protobuf2THUAI6Human(item);
}
bufferState->humans.push_back(Proto2THUAI6::Protobuf2THUAI6Human(*itr));
bufferState->humans.push_back(Proto2THUAI6::Protobuf2THUAI6Human(item));
}
for (auto itr = message.butcher_message().begin(); itr != message.butcher_message().end(); itr++)
for (auto item : message.butcher_message())

@Timothy-Liuxf Timothy-Liuxf Jan 5, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto 不会自动推导出引用

- for (auto item : message.human_message())
+ for (const auto& item : message.human_message())

{
int vr = this->bufferState->humanSelf->viewRange;
int deltaX = itr->x() - this->bufferState->humanSelf->x;
int deltaY = itr->y() - this->bufferState->humanSelf->y;
int deltaX = item.x() - this->bufferState->humanSelf->x;
int deltaY = item.y() - this->bufferState->humanSelf->y;
double distance = deltaX * deltaX + deltaY * deltaY;
if (distance > vr * vr)
continue;
Expand All @@ -270,26 +270,26 @@ void Logic::LoadBuffer(protobuf::MessageToClient& message)
}
if (barrier)
continue;
bufferState->butchers.push_back(Proto2THUAI6::Protobuf2THUAI6Butcher(*itr));
bufferState->butchers.push_back(Proto2THUAI6::Protobuf2THUAI6Butcher(item));
std::cout << "Add Butcher!" << std::endl;
}
}
}
else
{
for (auto itr = message.butcher_message().begin(); itr != message.butcher_message().end(); itr++)
for (auto item : message.butcher_message())
{
if (itr->player_id() == playerID)
if (item.player_id() == playerID)
{
bufferState->butcherSelf = Proto2THUAI6::Protobuf2THUAI6Butcher(*itr);
bufferState->butcherSelf = Proto2THUAI6::Protobuf2THUAI6Butcher(item);
}
bufferState->butchers.push_back(Proto2THUAI6::Protobuf2THUAI6Butcher(*itr));
bufferState->butchers.push_back(Proto2THUAI6::Protobuf2THUAI6Butcher(item));
}
for (auto itr = message.human_message().begin(); itr != message.human_message().end(); itr++)
for (auto item : message.human_message())
{
int vr = this->bufferState->butcherSelf->viewRange;
int deltaX = itr->x() - this->bufferState->butcherSelf->x;
int deltaY = itr->y() - this->bufferState->butcherSelf->y;
int deltaX = item.x() - this->bufferState->butcherSelf->x;
int deltaY = item.y() - this->bufferState->butcherSelf->y;
double distance = deltaX * deltaX + deltaY * deltaY;
if (distance > vr * vr)
continue;
Expand All @@ -314,11 +314,13 @@ void Logic::LoadBuffer(protobuf::MessageToClient& message)
}
if (barrier)
continue;
bufferState->humans.push_back(Proto2THUAI6::Protobuf2THUAI6Human(*itr));
bufferState->humans.push_back(Proto2THUAI6::Protobuf2THUAI6Human(item));
std::cout << "Add Human!" << std::endl;
}
}
}
for (auto item : message.prop_message())
bufferState->props.push_back(Proto2THUAI6::Protobuf2THUAI6Prop(item));
if (asynchronous)
{
{
Expand Down Expand Up @@ -398,7 +400,7 @@ bool Logic::TryConnection()
return result;
}

void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port)
void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port, bool level, std::string filename)
{
// 建立与服务器之间通信的组件
pComm = std::make_unique<Communication>(IP, port);
Expand Down
78 changes: 64 additions & 14 deletions CAPI/API/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,77 @@
#include "AI.h"
#include "logic.h"
#include "structures.h"
#include <tclap/CmdLine.h>

#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

extern const bool asynchronous;

int THUAI6Main(CreateAIFunc AIBuilder)
int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder)
{
// 仅供调试使用
int playerID = 123;
std::string sIP = "183.172.208.84";
std::string sPort = "8888";
int pID = 114514;
std::string sIP = "114.51.41.91";
std::string sPort = "9810";
std::string filename = "";
bool level = false;
extern const THUAI6::PlayerType playerType;
extern const THUAI6::ButcherType butcherType;
extern const THUAI6::HumanType humanType;
THUAI6::PlayerType player = playerType;
THUAI6::HumanType human = humanType;
THUAI6::ButcherType butcher = butcherType;
Logic logic(player, playerID, butcher, human);
logic.Main(AIBuilder, sIP, sPort);
// 仅供早期调试使用
{
Logic logic(playerType, pID, butcherType, humanType);
logic.Main(AIBuilder, sIP, sPort, level, filename);
return 0;
}

// 使用cmdline的版本
try
{
TCLAP::CmdLine cmd("THUAI6 C++ interface commandline parameter introduction");

TCLAP::ValueArg<std::string> serverIP("I", "serverIP", "Server`s IP 127.0.0.1 in default", false, "127.0.0.1", "string");
cmd.add(serverIP);

TCLAP::ValueArg<uint16_t> serverPort("P", "serverPort", "Port the server listens to 7777 in default", false, 7777, "USORT");
cmd.add(serverPort);

std::vector<int> validPlayerIDs{0, 1, 2, 3};
TCLAP::ValuesConstraint<int> playerIdConstraint(validPlayerIDs);
TCLAP::ValueArg<int> playerID("p", "playerID", "Player ID 0,1,2,3 valid only", true, -1, &playerIdConstraint);
cmd.add(playerID);

std::string DebugDesc = "Set this flag to use API for debugging.\n"
"If \"-f\" is not set, the log will be printed on the screen.\n"
"Or you could specify a file to store it.";
TCLAP::SwitchArg debug("d", "debug", DebugDesc);
cmd.add(debug);

TCLAP::ValueArg<std::string> FileName("f", "filename", "Specify a file to store the log.", false, "", "string");
cmd.add(FileName);

TCLAP::SwitchArg warning("w", "warning", "Warn of some obviously invalid operations (only when \"-d\" is set).");
cmd.add(warning);

cmd.parse(argc, argv);
pID = playerID.getValue();
sIP = serverIP.getValue();
sPort = serverPort.getValue();

bool d = debug.getValue();
bool w = warning.getValue();
if (d)
{
level = w;
}
filename = FileName.getValue();
}
catch (TCLAP::ArgException& e)
{
std::cerr << "Parsing error: " << e.error() << " for arg " << e.argId() << std::endl;
return 1;
}
Logic logic(playerType, pID, butcherType, humanType);
logic.Main(AIBuilder, sIP, sPort, level, filename);
return 0;
}

Expand All @@ -30,7 +80,7 @@ std::unique_ptr<IAI> CreateAI()
return std::make_unique<AI>();
}

int main()
int main(int argc, char* argv[])
{
return THUAI6Main(CreateAI);
return THUAI6Main(argc, argv, CreateAI);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行尾(

  }
+ 

2 changes: 1 addition & 1 deletion CAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ message(STATUS "Using gRPC ${gRPC_VERSION}")

add_executable(capi ${CPP_LIST} ${PROTO_CPP_LIST})

target_include_directories(capi PUBLIC ${PROJECT_SOURCE_DIR}/proto ${PROJECT_SOURCE_DIR}/API/include)
target_include_directories(capi PUBLIC ${PROJECT_SOURCE_DIR}/proto ${PROJECT_SOURCE_DIR}/API/include ${PROJECT_SOURCE_DIR}/tclap/include)

target_link_libraries(capi
protobuf::libprotobuf
Expand Down
23 changes: 23 additions & 0 deletions CAPI/tclap/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2003 Michael E. Smoot
Copyright (c) 2004 Daniel Aarno
Copyright (c) 2017 Google Inc.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading