-
-
Notifications
You must be signed in to change notification settings - Fork 22
feat(CAPI): ✨ add cmdline #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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()) | ||
| { | ||
| 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()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) |
||
| { | ||
| 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; | ||
|
|
@@ -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; | ||
|
|
@@ -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) | ||
| { | ||
| { | ||
|
|
@@ -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); | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -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; | ||
| } | ||
|
|
||
|
|
@@ -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); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 行尾( }
+ |
||
This file contains hidden or 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
This file contains hidden or 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,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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上