From 3cbb99bf8dd65af019e2daec313b7bbb913e28c3 Mon Sep 17 00:00:00 2001 From: BowmanChow Date: Thu, 30 Apr 2020 14:21:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=89=E6=89=8BAPI?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=BF=94=E5=9B=9E=E5=80=BC=EF=BC=8C=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E6=B6=88=E6=81=AF=E6=98=AF=E5=90=A6=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E3=80=82=E5=8E=BB=E6=8E=89Agent=E4=B8=AD?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=A2=91=E7=8E=87=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.md | 26 ++- CAPI/include/API.h | 11 +- CAPI/include/CAPI.h | 2 +- CAPI/src/API.cpp | 25 +-- CAPI/src/CAPI.cpp | 7 +- CAPI/src/main.cpp | 2 +- CAPI/src/player.cpp | 295 ++++++++++++++++----------------- communication/Agent/Program.cs | 52 +++--- 8 files changed, 219 insertions(+), 201 deletions(-) diff --git a/API.md b/API.md index 3aaa351..327c61c 100644 --- a/API.md +++ b/API.md @@ -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() 获取目前距离游戏开始的时间,单位为毫秒。 @@ -34,6 +44,10 @@ - void ResumeCommunication(); 继续数据更新,用于Debug。 + +- void wait(); + + 等待下一次消息更新的到来。 ## 数据结构 diff --git a/CAPI/include/API.h b/CAPI/include/API.h index ded09f5..5c33e32 100644 --- a/CAPI/include/API.h +++ b/CAPI/include/API.h @@ -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 \ No newline at end of file diff --git a/CAPI/include/CAPI.h b/CAPI/include/CAPI.h index 1dc5425..5499105 100644 --- a/CAPI/include/CAPI.h +++ b/CAPI/include/CAPI.h @@ -69,7 +69,7 @@ class CAPI void OnReceive(shared_ptr 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(); }; diff --git a/CAPI/src/API.cpp b/CAPI/src/API.cpp index a1daecd..7c4abd6 100644 --- a/CAPI/src/API.cpp +++ b/CAPI/src/API.cpp @@ -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() @@ -82,4 +82,9 @@ void THUAI3::initializeGameTime() unsigned long long THUAI3::getGameTime() { return getSystemTime() - initGameTime; +} + +void THUAI3::wait() +{ + API.sema.wait(); } \ No newline at end of file diff --git a/CAPI/src/CAPI.cpp b/CAPI/src/CAPI.cpp index 63f70ca..82ab9f2 100644 --- a/CAPI/src/CAPI.cpp +++ b/CAPI/src/CAPI.cpp @@ -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 }; @@ -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]; @@ -183,6 +183,7 @@ void CAPI::SendCommandMessage(MessageToServer message) Message* mes3 = new Message(-1, mes2); shared_ptr mes = make_shared(-1, mes3); Send(mes); + return true; } void CAPI::CreateObj(int64_t id, Protobuf::MessageToClient* message) diff --git a/CAPI/src/main.cpp b/CAPI/src/main.cpp index 0dd33f8..9772d31 100644 --- a/CAPI/src/main.cpp +++ b/CAPI/src/main.cpp @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) THUAI3::initializeGameTime(); while (GameRunning) { - API.sema.wait(); + THUAI3::wait(); play(); } getchar(); diff --git a/CAPI/src/player.cpp b/CAPI/src/player.cpp index 1ea1bff..2aaa2f2 100644 --- a/CAPI/src/player.cpp +++ b/CAPI/src/player.cpp @@ -7,155 +7,152 @@ using namespace THUAI3; Protobuf::Talent initTalent = Protobuf::Talent::None;//指定人物天赋。选手代码必须定义此变量,否则报错 void play() { - //char c; - //cin.clear(); - //cin.ignore(); - //cin >> c; - //switch (c) - //{ - //case 'd': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // cout << moveDistance / PlayerInfo.moveSpeed * 1000 << endl; - // move(Protobuf::Direction::Right, moveDistance / PlayerInfo.moveSpeed * 1000); - //}break; - //case 'e': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // move(Protobuf::Direction::RightUp, moveDistance / PlayerInfo.moveSpeed * 1000); - //} - //break; - //case 'w': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // move(Protobuf::Direction::Up, moveDistance / PlayerInfo.moveSpeed * 1000); - //}break; - //case 'q': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // move(Protobuf::Direction::LeftUp, moveDistance / PlayerInfo.moveSpeed * 1000); - //}break; - //case 'a': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // move(Protobuf::Direction::Left, moveDistance / PlayerInfo.moveSpeed * 1000); - //}break; - //case 'z': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // move(Protobuf::Direction::LeftDown, moveDistance / PlayerInfo.moveSpeed * 1000); - //}break; - //case 'x': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // move(Protobuf::Direction::Down, moveDistance / PlayerInfo.moveSpeed * 1000); - //}break; - //case 'c': - //{ - // int moveDistance = 0; - // std::cout << endl << "Please Input your move distance" << endl; - // cin >> moveDistance; - // move(Protobuf::Direction::RightDown, moveDistance / PlayerInfo.moveSpeed * 1000); - //}break; - //case 'f': - //{ - // std::cout << endl << "Please Input 3 parameters : isSelfPosition, pickType, dishOrToolType" << endl; - // bool isSelfPosition = 0; - // cin >> isSelfPosition; - // int pickType = 0; - // cin >> pickType; - // int dishOrToolType = 0; - // cin >> dishOrToolType; - // pick(isSelfPosition, (ObjType)pickType, dishOrToolType); - //} - //break; - //case 'u': - //{ - // std::cout << endl << "Please Input 2 parameters : " << endl; - // double param1 = 0; - // cin >> param1; - // double param2 = 0; - // cin >> param2; - // use(1, param1, param2); - //} - //break; - //case 'i': use(0); break; - //case 'r': - //{ - // std::cout << endl << "Please Input 2 parameters : " << endl; - // double distance = 0; - // cin >> distance; - // double angle = 0; - // cin >> angle; - // put(distance, angle, true); - // _sleep(26); - // move(Left, 1000); - //} - //break; - //case 't': - //{ - // std::cout << endl << "Please Input 2 parameters : " << endl; - // double distance = 0; - // cin >> distance; - // double angle = 0; - // cin >> angle; - // put(distance, angle, false); - //} - //break; - //case ':': - //{ - // std::cout << endl << "Please Input your text to speak : " << endl; - // string str; - // cin >> str; - // speakToFriend(str); - //} - //break; - //case 'm': - //{ - // std::cout << endl << "Input two interger to print a map cell :" << endl; - // int x, y; - // cin.clear(); - // cin.ignore(); - // cin >> x >> y; - // list l = MapInfo::get_mapcell(PlayerInfo.position.x, PlayerInfo.position.y); - // if (l.empty()) cout << "empty" << endl; - // std::cout << "objs in map[" << PlayerInfo.position.x << "][" << PlayerInfo.position.y << "] :" << endl; - // for (list::iterator i = l.begin(); i != l.end(); i++) - // { - // std::cout << "\tblocktype : " << i->objType << endl; - // } - //} - //break; - //case 's': - //{ - // std::cout << endl << "Self info :" << endl; - // std::cout << "\tposition : " << PlayerInfo.position.x << "," << PlayerInfo.position.y << endl; - // std::cout << "\tdish : " << PlayerInfo.dish << endl; - // std::cout << "\ttool : " << PlayerInfo.tool << endl; - // std::cout << "\trecieveText : " << PlayerInfo.recieveText << endl; - //} + char c; + cin.clear(); + cin.ignore(); + cin >> c; + switch (c) + { + case 'd': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + cout << moveDistance / PlayerInfo.moveSpeed * 1000 << endl; + move(Protobuf::Direction::Right, moveDistance / PlayerInfo.moveSpeed * 1000); + }break; + case 'e': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + move(Protobuf::Direction::RightUp, moveDistance / PlayerInfo.moveSpeed * 1000); + } + break; + case 'w': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + move(Protobuf::Direction::Up, moveDistance / PlayerInfo.moveSpeed * 1000); + }break; + case 'q': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + move(Protobuf::Direction::LeftUp, moveDistance / PlayerInfo.moveSpeed * 1000); + }break; + case 'a': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + move(Protobuf::Direction::Left, moveDistance / PlayerInfo.moveSpeed * 1000); + }break; + case 'z': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + move(Protobuf::Direction::LeftDown, moveDistance / PlayerInfo.moveSpeed * 1000); + }break; + case 'x': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + move(Protobuf::Direction::Down, moveDistance / PlayerInfo.moveSpeed * 1000); + }break; + case 'c': + { + int moveDistance = 0; + std::cout << endl << "Please Input your move distance" << endl; + cin >> moveDistance; + move(Protobuf::Direction::RightDown, moveDistance / PlayerInfo.moveSpeed * 1000); + }break; + case 'f': + { + std::cout << endl << "Please Input 3 parameters : isSelfPosition, pickType, dishOrToolType" << endl; + bool isSelfPosition = 0; + cin >> isSelfPosition; + int pickType = 0; + cin >> pickType; + int dishOrToolType = 0; + cin >> dishOrToolType; + pick(isSelfPosition, (ObjType)pickType, dishOrToolType); + } + break; + case 'u': + { + std::cout << endl << "Please Input 2 parameters : " << endl; + double param1 = 0; + cin >> param1; + double param2 = 0; + cin >> param2; + use(1, param1, param2); + } + break; + case 'i': use(0); break; + case 'r': + { + std::cout << endl << "Please Input 2 parameters : " << endl; + double distance = 0; + cin >> distance; + double angle = 0; + cin >> angle; + put(distance, angle, true); + _sleep(26); + move(Left, 1000); + } + break; + case 't': + { + std::cout << endl << "Please Input 2 parameters : " << endl; + double distance = 0; + cin >> distance; + double angle = 0; + cin >> angle; + put(distance, angle, false); + } + break; + case ':': + { + std::cout << endl << "Please Input your text to speak : " << endl; + string str; + cin >> str; + speakToFriend(str); + } + break; + case 'm': + { + std::cout << endl << "Input two interger to print a map cell :" << endl; + int x, y; + cin.clear(); + cin.ignore(); + cin >> x >> y; + list l = MapInfo::get_mapcell(PlayerInfo.position.x, PlayerInfo.position.y); + if (l.empty()) cout << "empty" << endl; + std::cout << "objs in map[" << PlayerInfo.position.x << "][" << PlayerInfo.position.y << "] :" << endl; + for (list::iterator i = l.begin(); i != l.end(); i++) + { + std::cout << "\tblocktype : " << i->objType << endl; + } + } + break; + case 's': + { + std::cout << endl << "Self info :" << endl; + std::cout << "\tposition : " << PlayerInfo.position.x << "," << PlayerInfo.position.y << endl; + std::cout << "\tdish : " << PlayerInfo.dish << endl; + std::cout << "\ttool : " << PlayerInfo.tool << endl; + std::cout << "\trecieveText : " << PlayerInfo.recieveText << endl; + } - //default: - // break; - //} + default: + break; + } - static Direction direc = (Direction)0; - //std::cout << "Game Time : " << THUAI3::getGameTime() << endl; - move(direc, 0); - direc = (Direction)((direc + 1) % 8); - /* 玩家在这里写代码 */ + std::cout << "Game Time : " << THUAI3::getGameTime() << endl; + /* 玩家在这里写代码 */ } \ No newline at end of file diff --git a/communication/Agent/Program.cs b/communication/Agent/Program.cs index 0e69173..d89b795 100644 --- a/communication/Agent/Program.cs +++ b/communication/Agent/Program.cs @@ -13,8 +13,8 @@ public class Program private static IDServer server = new IDServer(); private static System.Timers.Timer myTimer = new System.Timers.Timer(); private static IPEndPoint Server; - private static object[] LastSpam;// = Constants.MaxMessage; - private static int[][] deltaSendTime; + //private static object[] LastSpam;// = Constants.MaxMessage; + //private static int[][] deltaSendTime; /* private static void TimeCount(object source, System.Timers.ElapsedEventArgs e) //倒计时 { @@ -37,13 +37,13 @@ public static void Main(string[] args) Constants.PlayerCount = ushort.Parse(playercount.Value()); if (Constants.PlayerCount < 1) Constants.PlayerCount = 1; else if (Constants.PlayerCount > 2) Constants.PlayerCount = 2; - LastSpam = new object[Constants.PlayerCount]; - deltaSendTime = new int[Constants.PlayerCount][]; + //LastSpam = new object[Constants.PlayerCount]; + //deltaSendTime = new int[Constants.PlayerCount][]; //Constants.MaxMessage = int.Parse(messagelmt.Value()); Constants.TimeLimit = double.Parse(timelmt.Value()); for (int i = 0; i < Constants.PlayerCount; i++) { - deltaSendTime[i] = new int[] { (int)Constants.TimeLimit + 5, (int)Constants.TimeLimit + 5 }; + //deltaSendTime[i] = new int[] { (int)Constants.TimeLimit + 5, (int)Constants.TimeLimit + 5 }; } if (Constants.TimeLimit < 10) Constants.TimeLimit = 10; return MainInternal(server.Value(), ushort.Parse(port.Value()), token.Value(), int.Parse(debugLevel.Value())); @@ -59,8 +59,8 @@ private static int MainInternal(string ep, ushort port, string token, int debugL server.Port = port; Constants.Debug("Agent Listen Port: " + server.Port.ToString()); Constants.Debug("Client Token: " + (token ?? "")); - for (int i = 0; i < LastSpam.Length; i++) - LastSpam[i] = Environment.TickCount; + //for (int i = 0; i < LastSpam.Length; i++) + // LastSpam[i] = Environment.TickCount; //init timer myTimer.Interval = Interval; @@ -77,25 +77,25 @@ private static int MainInternal(string ep, ushort port, string token, int debugL }; server.OnReceive += delegate (Message message) { - if (!(((message.Content as Message)?.Content as Message)?.Content is PingPacket)) - { - var now = Environment.TickCount; - lock (LastSpam) - { - //Console.WriteLine(message.Address + " : " + now + " deltaSendTime : " + deltaSendTime[message.Address][0] + ", " + deltaSendTime[message.Address][1]); - int deltaTime = now - (int)LastSpam[message.Address]; - if (((double)deltaTime + (double)deltaSendTime[message.Address][0] + (double)deltaSendTime[message.Address][1]) / 3.0 <= Constants.TimeLimit) - { - Console.WriteLine("skip client's message"); - return; - } - LastSpam[message.Address] = now; - deltaSendTime[message.Address][0] = deltaSendTime[message.Address][1]; - deltaSendTime[message.Address][1] = deltaTime; - } - } - else - Constants.Debug("Ignoring PingPacket"); + //if (!(((message.Content as Message)?.Content as Message)?.Content is PingPacket)) + //{ + // var now = Environment.TickCount; + // lock (LastSpam) + // { + // //Console.WriteLine(message.Address + " : " + now + " deltaSendTime : " + deltaSendTime[message.Address][0] + ", " + deltaSendTime[message.Address][1]); + // int deltaTime = now - (int)LastSpam[message.Address]; + // if (((double)deltaTime + (double)deltaSendTime[message.Address][0] + (double)deltaSendTime[message.Address][1]) / 3.0 <= Constants.TimeLimit) + // { + // Console.WriteLine("skip client's message"); + // return; + // } + // LastSpam[message.Address] = now; + // deltaSendTime[message.Address][0] = deltaSendTime[message.Address][1]; + // deltaSendTime[message.Address][1] = deltaTime; + // } + //} + //else + // Constants.Debug("Ignoring PingPacket"); client.Send(message.Content as Message); };