Make the bot work

This commit is contained in:
Astatin3
2024-05-02 16:12:00 -06:00
parent 7b5e763327
commit f64d8dbcde
7 changed files with 202 additions and 73 deletions
+64 -2
View File
@@ -17,8 +17,70 @@ using namespace Botcraft;
using namespace ProtocolCraft;
CommandClient::CommandClient(const bool use_renderer_) : TemplatedBehaviourClient<CommandClient>(use_renderer_){
std::cout << "Bot started!" << std::endl;
}
CommandClient::~CommandClient(){}
CommandClient::~CommandClient(){}
bool running = false;
bool gotoCmdStream = false;
void CommandClient::gotopos(int x, int y, int z){
Position target_position = Position(x, y, z);
float speed_multiplier = 2.0f;;
auto tree = Builder<CommandClient>("goto tree")
.sequence()
// Perform the pathfinding in a Selector,
// so it exits as soon as one leaf
// returns success
.selector()
// The next three lines do exactly the same,
// they're only here to show the different
// possibilities to create a leaf. Note that
// only the lambda solution can use default
// parameters values
.leaf("go to lambda", [=](CommandClient& c) { return GoTo(c, target_position, 0, 0, 0, true, false, speed_multiplier); })
.leaf("go to function", GoTo, target_position, 0, 0, 0, true, false, speed_multiplier)
.leaf("go to std::bind", std::bind(GoTo, std::placeholders::_1, target_position, 0, 0, 0, true, false, speed_multiplier))
// If goto fails, say something in chat
.leaf(Say, "Pathfinding failed :(")
.end()
// Switch back to empty behaviour
.leaf([](CommandClient& c) { c.SetBehaviourTree(nullptr); return Status::Success; })
.end();
SetBehaviourTree(tree);
}
void CommandClient::gotocmd(int x, int y, int z){
if(running && !gotoCmdStream){
std::cout << "Already running a task." << std::endl;
running = true;
return;
}
gotopos(x,y,z);
}
void CommandClient::startGotoSteamMode(){
if(running){
std::cout << "Already running a task." << std::endl;
running = true;
return;
}
running = true;
gotoCmdStream = true;
}
void CommandClient::stop(){
if(running){
std::cout << "Stopped..." << std::endl;
SetBehaviourTree(nullptr);
gotoCmdStream = false;
running = false;
}else{
std::cout << "No task is running..." << std::endl;
}
}
+4 -14
View File
@@ -13,20 +13,10 @@ public:
CommandClient(const bool use_renderer_);
~CommandClient();
protected:
#if PROTOCOL_VERSION < 759 /* < 1.19 */
// virtual void Handle(ProtocolCraft::ClientboundChatPacket& msg) override;
#else
// virtual void Handle(ProtocolCraft::ClientboundPlayerChatPacket& msg) override;
// virtual void Handle(ProtocolCraft::ClientboundSystemChatPacket& msg) override;
#endif
void gotopos(int x, int y, int z);
void gotocmd(int x, int y, int z);
// void ProcessChatMsg(const std::vector<std::string>& splitted_msg);
void startGotoSteamMode();
// Check for any spawnable blocks in a sphere from pos and prints
// all the positions into a file
// Use check_lighting to add a check on light block value (> 7)
// (warning: ignore top slabs and upside-down stairs,
// you should check for such blocks manually)
// void CheckPerimeter(const Botcraft::Position& pos, const float radius, const bool check_lighting);
void stop();
};
@@ -128,8 +128,8 @@ void ChatCommandClient::ProcessChatMsg(const std::vector<std::string>& splitted_
// possibilities to create a leaf. Note that
// only the lambda solution can use default
// parameters values
.leaf("go to lambda", [=](ChatCommandClient& c) { return GoTo(c, target_position, 0, 0, 0, true, false, speed_multiplier); })
.leaf("go to function", GoTo, target_position, 0, 0, 0, true, false, speed_multiplier)
.leaf("go to lambda", [=](ChatCommandClient& c) { return GoTo(c, target_position, 0, 0, 0, true, true, speed_multiplier); })
.leaf("go to function", GoTo, target_position, 0, 0, 0, true, true, speed_multiplier)
.leaf("go to std::bind", std::bind(GoTo, std::placeholders::_1, target_position, 0, 0, 0, true, false, speed_multiplier))
// If goto fails, say something in chat
.leaf(Say, "Pathfinding failed :(")
+51 -34
View File
@@ -20,10 +20,52 @@ struct Args
Args ParseCommandLine(int argc, char* argv[]);
int main(int argc, char* argv[])
{
try
{
std::vector<std::string> split(std::string text, char delim) {
std::string line;
std::vector<std::string> vec;
std::stringstream ss(text);
while(std::getline(ss, line, delim)) {
vec.push_back(line);
}
return vec;
}
void runSTDINThing(CommandClient &client){
for (std::string line; std::getline(std::cin, line);) {
std::cout << "Got message: " + line << std::endl;
if (line.starts_with("chat:")) {
std::string msg = line.substr(5, line.length());
std::cout << "Sending chat message: " + msg << std::endl;
client.SendChatMessage(msg);
} else if (line.starts_with("cmd:")) {
std::string cmd = line.substr(4, line.length());
std::cout << "Sending commnad: /" + cmd << std::endl;
client.SendChatCommand(cmd);
} else if (line.starts_with("Stop")){
std::cout << "Stopped current action." << std::endl;
client.stop();
} else if (line.starts_with("Disconnect")) {
std::cout << "Disconnected." << std::endl;
client.Disconnect();
} else if (line.starts_with("goto:")) {
std::vector<std::string> xyz = split(line.substr(5, line.length()), ',');
std::cout << "Moving toward: (" + xyz.at(0) + ", "
+ xyz.at(1) + ", "
+ xyz.at(2) + ")" << std::endl;
client.gotocmd(std::stoi(xyz.at(0)),
std::stoi(xyz.at(1)),
std::stoi(xyz.at(2)));
} else if (line.starts_with("startGotoSteamMode")){
std::cout << "Started GOTO stream mode..." << std::endl;
client.startGotoSteamMode();
}
}
}
int main(int argc, char* argv[]){
try {
// Init logging, log everything >= Info, only to console, no file
Botcraft::Logger::GetInstance().SetLogLevel(Botcraft::LogLevel::Info);
Botcraft::Logger::GetInstance().SetFilename("");
@@ -48,42 +90,17 @@ int main(int argc, char* argv[])
}
}
// Botcraft::ConnectionClient client;
CommandClient client(false);
client.SetAutoRespawn(true);
// CommandClient client(true);
//
LOG_INFO("Starting connection process");
//
// client.SetAutoRespawn(true);
//
// LOG_INFO("Starting connection process");
client.Connect(args.address, args.login);
//
// client.RunBehaviourUntilClosed();
// client.Disconnect();
std::thread thread(runSTDINThing, std::ref(client));
// return 0;
// client.Connect(args.address, args.login);
for (std::string line; std::getline(std::cin, line);) {
std::cout << "Got message: "+line << std::endl;
if(line.starts_with("chat:")){
client.SendChatMessage(line.substr(5,line.length()));
}else if(line.starts_with("cmd:")){
client.SendChatCommand(line.substr(4,line.length()));
}else if(line.starts_with("Disconnect")){
std::cout << "Disconnected." << std::endl;
client.Disconnect();
return 0;
}
}
client.RunBehaviourUntilClosed();
//// client.SetAutoRespawn(true);
//
+2 -1
View File
@@ -1,2 +1,3 @@
cd /home/astatin3/Documents/GitHub/meteorbot/bot/bin
./McConnectBot
#./2_ChatCommandExample_d
./McConnectBot