Initial commit

This commit is contained in:
Astatin3
2024-04-30 22:07:50 -06:00
commit 8565caa62a
8463 changed files with 4915934 additions and 0 deletions
@@ -0,0 +1,13 @@
project(5_MobHitterExample)
set(${PROJECT_NAME}_SOURCE_FILES
${PROJECT_SOURCE_DIR}/include/MobHitterTasks.hpp
${PROJECT_SOURCE_DIR}/src/MobHitterTasks.cpp
${PROJECT_SOURCE_DIR}/src/main.cpp
)
set(${PROJECT_NAME}_INCLUDE_FOLDERS
${PROJECT_SOURCE_DIR}/include
)
add_example("${${PROJECT_NAME}_INCLUDE_FOLDERS}" "${${PROJECT_NAME}_SOURCE_FILES}")
@@ -0,0 +1,16 @@
#pragma once
#include <string>
#include "botcraft/AI/BehaviourClient.hpp"
#include "botcraft/AI/Status.hpp"
/// @brief Hit all nearby monster, respecting the invulnerability cooldown of 0.5s
/// @param c The client performing the action
/// @return Always return Success
Botcraft::Status HitCloseHostiles(Botcraft::BehaviourClient& c);
/// @brief Remove entries in Entities.LastTimeHit if last seen > 10s
/// @param c The client performing the action
/// @return Always return Success
Botcraft::Status CleanLastTimeHit(Botcraft::BehaviourClient& c);
@@ -0,0 +1,77 @@
#include "MobHitterTasks.hpp"
#include "botcraft/Game/Entities/EntityManager.hpp"
#include "botcraft/Game/Entities/LocalPlayer.hpp"
#include "botcraft/Network/NetworkManager.hpp"
using namespace Botcraft;
using namespace ProtocolCraft;
Status HitCloseHostiles(BehaviourClient& c)
{
std::shared_ptr<EntityManager> entity_manager = c.GetEntityManager();
std::shared_ptr<LocalPlayer> local_player = entity_manager->GetLocalPlayer();
std::shared_ptr<NetworkManager> network_manager = c.GetNetworkManager();
Blackboard& blackboard = c.GetBlackboard();
const NotifyOnEndUseRef<std::map<int, std::chrono::steady_clock::time_point>> last_time_hit_wrapper = blackboard.GetRef("Entities.LastTimeHit", std::map<int, std::chrono::steady_clock::time_point>());
std::map<int, std::chrono::steady_clock::time_point>& last_time_hit = last_time_hit_wrapper.ref();
const Vector3<double> player_pos = local_player->GetPosition();
auto now = std::chrono::steady_clock::now();
{
auto entities = entity_manager->GetEntities();
for (const auto& [id, entity] : *entities)
{
if (entity->IsMonster() && (entity->GetPosition()- player_pos).SqrNorm() < 16.0)
{
auto time = last_time_hit.find(id);
if (time != last_time_hit.end() &&
std::chrono::duration_cast<std::chrono::milliseconds>(now - time->second).count() < 500)
{
continue;
}
last_time_hit[id] = now;
local_player->LookAt(entity->GetPosition());
std::shared_ptr<ServerboundInteractPacket> msg = std::make_shared<ServerboundInteractPacket>();
msg->SetAction(1);
msg->SetEntityId(id);
#if PROTOCOL_VERSION > 722 /* > 1.15.2 */
msg->SetUsingSecondaryAction(false);
#endif
std::shared_ptr<ServerboundSwingPacket> msg_swing = std::make_shared<ServerboundSwingPacket>();
msg_swing->SetHand(0);
network_manager->Send(msg);
network_manager->Send(msg_swing);
}
}
}
return Status::Success;
}
Status CleanLastTimeHit(BehaviourClient& c)
{
const NotifyOnEndUseRef<std::map<int, std::chrono::steady_clock::time_point>> last_time_hit_wrapper = c.GetBlackboard().GetRef("Entities.LastTimeHit", std::map<int, std::chrono::steady_clock::time_point>());
std::map<int, std::chrono::steady_clock::time_point>& last_time_hit = last_time_hit_wrapper.ref();
auto now = std::chrono::steady_clock::now();
for (auto it = last_time_hit.begin(); it != last_time_hit.end();)
{
if (std::chrono::duration_cast<std::chrono::seconds>(now - it->second).count() > 10)
{
it = last_time_hit.erase(it);
}
else
{
++it;
}
}
return Status::Success;
}
@@ -0,0 +1,132 @@
#include <iostream>
#include <string>
#include "botcraft/AI/SimpleBehaviourClient.hpp"
#include "botcraft/Utilities/Logger.hpp"
#include "MobHitterTasks.hpp"
void ShowHelp(const char* argv0)
{
std::cout << "Usage: " << argv0 << " <options>\n"
<< "Options:\n"
<< "\t-h, --help\tShow this help message\n"
<< "\t--address\tAddress of the server you want to connect to, default: 127.0.0.1:25565\n"
<< "\t--login\t\tPlayer name in offline mode, empty for Microsoft account, default: BCChatCommand\n"
<< std::endl;
}
struct Args
{
bool help = false;
std::string address = "127.0.0.1:25565";
std::string login = "BCMobHitter";
int return_code = 0;
};
Args ParseCommandLine(int argc, char* argv[]);
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("");
// Add a name to this thread for logging
Botcraft::Logger::GetInstance().RegisterThread("main");
Args args;
if (argc == 1)
{
LOG_WARNING("No command arguments. Using default options.");
ShowHelp(argv[0]);
}
else
{
args = ParseCommandLine(argc, argv);
if (args.help)
{
ShowHelp(argv[0]);
return 0;
}
if (args.return_code != 0)
{
return args.return_code;
}
}
auto mob_hitter_tree = Botcraft::Builder<Botcraft::SimpleBehaviourClient>()
.sequence()
.leaf("hit close hostiles mobs", HitCloseHostiles)
.leaf("remove old entities from last time hit", CleanLastTimeHit)
.end();
Botcraft::SimpleBehaviourClient client(true);
client.SetAutoRespawn(true);
LOG_INFO("Starting connection process");
client.Connect(args.address, args.login);
client.SetBehaviourTree(mob_hitter_tree);
client.RunBehaviourUntilClosed();
client.Disconnect();
return 0;
}
catch (std::exception& e)
{
LOG_FATAL("Exception: " << e.what());
return 1;
}
catch (...)
{
LOG_FATAL("Unknown exception");
return 2;
}
}
Args ParseCommandLine(int argc, char* argv[])
{
Args args;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "-h" || arg == "--help")
{
ShowHelp(argv[0]);
args.help = true;
return args;
}
else if (arg == "--address")
{
if (i + 1 < argc)
{
args.address = argv[++i];
}
else
{
LOG_FATAL("--address requires an argument");
args.return_code = 1;
return args;
}
}
else if (arg == "--login")
{
if (i + 1 < argc)
{
args.login = argv[++i];
}
else
{
LOG_FATAL("--login requires an argument");
args.return_code = 1;
return args;
}
}
}
return args;
}