Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

Commit

Permalink
增加选手API函数返回值,表示消息是否发送成功。去掉Agent中消息频率限制
Browse files Browse the repository at this point in the history
  • Loading branch information
junbao-zhou committed Apr 30, 2020
1 parent b546fd1 commit 3cbb99b
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 201 deletions.
26 changes: 20 additions & 6 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@

## 玩家操作

- void THUAI3::move(Direction direction_t, int duration)
- bool THUAI3::move(Direction direction_t, int duration)

人物移动,第一个参数为移动方向,一共有8个方向,分别是右,右上,上,左上,左,左下,下,右下。
第二个参数为移动的时间(以毫秒计算),本游戏帧率为20,选手初始移动速度为每秒5个单位,如设定移动时间为1000则会在接下来的1秒内每50毫秒移动一次,每次移动距离为0.05.
第二个参数为移动的时间(以毫秒计算),本游戏帧率为20,选手初始移动速度为每秒5个单位,如设定移动时间为1000则会在接下来的1秒内每50毫秒移动一次,每次移动距离为0.25。

- void THUAI3::put(double distance, double angle, bool isThrowDish);
返回值表示消息是否发送成功。

- bool THUAI3::put(double distance, double angle, bool isThrowDish);

扔出物品,第一个参数为扔出的距离,物品的飞行速度是每秒10个单位。第二个参数为扔出时的绝对角度,单位为弧度制。第三个参数为是否扔出食材,若为true则扔出手中食材,若为false则扔出道具。

- void THUAI3::use(int type, double parameter1, double parameter2)
返回值表示消息是否发送成功。

- bool THUAI3::use(int type, double parameter1, double parameter2)

使用物品,第一个参数为使用类型,0为使用厨具或提交菜品,非0为使用手中的物品。后两个参数仅在使用传送门、锤子、弓箭时有效,parameter1为使用的距离,parameter2为使用的角度。

- void THUAI3::pick(bool isSelfPosition, ObjType pickType, int dishOrToolType);
返回值表示消息是否发送成功。

- bool THUAI3::pick(bool isSelfPosition, ObjType pickType, int dishOrToolType);

捡起物品。第一个参数为是否捡起自身所在方格的物品,若为true,则先检索自身所在方格,否则检索自己面对方向的方格。第二个参数为捡起的物品类型,可以为Block、Dish、Tool;若为Block表示捡起食物生产点或灶台里的食物,若为Dish表示捡起食物,若为Tool表示捡起道具。第三个参数为捡起的Dish或Tool类型,如果第二个参数为Dish,则这个参数必须与有效的DishType相对应,如果第二个参数为Tool,则这个参数必须与有效的ToolType相对应。若第三个参数为-1,表示随缘瞎捡。

- void THUAI3::speakToFriend(string speakText)
返回值表示消息是否发送成功。

- bool THUAI3::speakToFriend(string speakText)

向队友说话,每次最多只能发16个字符,多的截取前16个字符。

返回值表示消息是否发送成功。

- unsigned long long THUAI3::getGameTime()

获取目前距离游戏开始的时间,单位为毫秒。
Expand All @@ -34,6 +44,10 @@
- void ResumeCommunication();

继续数据更新,用于Debug。

- void wait();

等待下一次消息更新的到来。


## 数据结构
Expand Down
11 changes: 6 additions & 5 deletions CAPI/include/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
namespace THUAI3
{
extern unsigned long long initGameTime;
void move(Direction direction_t, int duration = 1000);
void put(double distance, double angle, bool isThrowDish);
void pick(bool isSelfPosition, ObjType pickType, int dishOrToolType);
void use(int type, double parameter1 = 0, double parameter2 = 0);
void speakToFriend(std::string speakText);
bool move(Direction direction_t, int duration = 1000);
bool put(double distance, double angle, bool isThrowDish);
bool pick(bool isSelfPosition, ObjType pickType, int dishOrToolType);
bool use(int type, double parameter1 = 0, double parameter2 = 0);
bool speakToFriend(std::string speakText);
void initializeGameTime();
unsigned long long getGameTime();
int GetPing(); // 精确到毫秒的延时
void PauseCommunication(); // 暂停数据更新
void ResumeCommunication();
Constant::Player GetInfo();
void wait();
} // namespace THUAI3

#endif
2 changes: 1 addition & 1 deletion CAPI/include/CAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CAPI
void OnReceive(shared_ptr<Message> message);
void Quit();
void SendChatMessage(string message);
void SendCommandMessage(Protobuf::MessageToServer message);
bool SendCommandMessage(Protobuf::MessageToServer message);
void UpdateInfo(Protobuf::MessageToClient *message);
Constant::Player GetInfo();
};
Expand Down
25 changes: 15 additions & 10 deletions CAPI/src/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,53 @@ Constant::Player THUAI3::GetInfo()
return API.GetInfo();
}

void THUAI3::move(Direction direction_t, int duration)
bool THUAI3::move(Direction direction_t, int duration)
{
MessageToServer mesC2S;
mesC2S.set_commandtype(CommandType::Move);
mesC2S.set_movedirection(direction_t);
mesC2S.set_moveduration(duration);
API.SendCommandMessage(mesC2S);
return API.SendCommandMessage(mesC2S);
}

void THUAI3::put(double distance, double angle, bool isThrowDish)
bool THUAI3::put(double distance, double angle, bool isThrowDish)
{
MessageToServer mesC2S;
mesC2S.set_throwdistance(distance);
mesC2S.set_throwangle(angle);
mesC2S.set_isthrowdish(isThrowDish);
mesC2S.set_commandtype(CommandType::Put);
API.SendCommandMessage(mesC2S);
return API.SendCommandMessage(mesC2S);
}

void THUAI3::use(int type, double parameter1, double parameter2)
bool THUAI3::use(int type, double parameter1, double parameter2)
{
MessageToServer mesC2S;
mesC2S.set_commandtype(CommandType::Use);
mesC2S.set_parameter1(parameter1);
mesC2S.set_parameter2(parameter2);
mesC2S.set_usetype(type);
API.SendCommandMessage(mesC2S);
return API.SendCommandMessage(mesC2S);
}

void THUAI3::pick(bool isSelfPosition, ObjType pickType, int dishOrToolType)
bool THUAI3::pick(bool isSelfPosition, ObjType pickType, int dishOrToolType)
{
MessageToServer mesC2S;
mesC2S.set_commandtype(CommandType::Pick);
mesC2S.set_ispickselfposition(isSelfPosition);
mesC2S.set_picktype(pickType);
mesC2S.set_pickdishortooltype(dishOrToolType);
API.SendCommandMessage(mesC2S);
return API.SendCommandMessage(mesC2S);
}

void THUAI3::speakToFriend(string speakText)
bool THUAI3::speakToFriend(string speakText)
{
MessageToServer mesC2S;
mesC2S.set_commandtype(CommandType::Speak);
if (speakText.length() > 16)
speakText = speakText.substr(0, 15);
mesC2S.set_speaktext(speakText);
API.SendCommandMessage(mesC2S);
return API.SendCommandMessage(mesC2S);
}

void THUAI3::initializeGameTime()
Expand All @@ -82,4 +82,9 @@ void THUAI3::initializeGameTime()
unsigned long long THUAI3::getGameTime()
{
return getSystemTime() - initGameTime;
}

void THUAI3::wait()
{
API.sema.wait();
}
7 changes: 4 additions & 3 deletions CAPI/src/CAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void CAPI::SendChatMessage(string message)
Send(mes);
}

void CAPI::SendCommandMessage(MessageToServer message)
bool CAPI::SendCommandMessage(MessageToServer message)
{
static const int timelimit = 45;
static long long deltaSendTime[] = { timelimit + 5,timelimit + 5 };
Expand All @@ -172,8 +172,8 @@ void CAPI::SendCommandMessage(MessageToServer message)
long long deltaTime = now - lastSendTime;
if (((double)deltaTime + (double)deltaSendTime[0] + (double)deltaSendTime[1]) / 3.0 < timelimit)
{
std::cout << "skip sending" << std::endl;
return;
//std::cout << "skip sending" << std::endl;
return false;
}
lastSendTime = now;
deltaSendTime[0] = deltaSendTime[1];
Expand All @@ -183,6 +183,7 @@ void CAPI::SendCommandMessage(MessageToServer message)
Message* mes3 = new Message(-1, mes2);
shared_ptr<Message> mes = make_shared<Message>(-1, mes3);
Send(mes);
return true;
}

void CAPI::CreateObj(int64_t id, Protobuf::MessageToClient* message)
Expand Down
2 changes: 1 addition & 1 deletion CAPI/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int main(int argc, char* argv[])
THUAI3::initializeGameTime();
while (GameRunning)
{
API.sema.wait();
THUAI3::wait();
play();
}
getchar();
Expand Down
Loading

0 comments on commit 3cbb99b

Please sign in to comment.