Skip to content

Commit

Permalink
Added unique bed
Browse files Browse the repository at this point in the history
  • Loading branch information
GaspardCulis committed Oct 12, 2023
1 parent 95c0ba5 commit e955920
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Bread/include/tasks/SurvivalTasks.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <botcraft/AI/BehaviourTree.hpp>
#include "AdvancedClient.hpp"
#include "botcraft/AI/Status.hpp"

namespace SurvivalTasks
{
Expand All @@ -15,6 +16,12 @@ namespace SurvivalTasks
/// @return Failure if no edible food in the inventory
Botcraft::Status FindBestFoodInInventory(AdvancedClient &client);

/// @brief Finds a unique bed based on the bot id (stored in the "id" blackboard key)
/// @param client The client performing the action
/// @param blackboard_key The blackboard key where the bed position will be stored
/// @return Success if a bed was found, Failure otherwise.
Botcraft::Status FindUniqueBedBlackboard(AdvancedClient &client, const std::string blackboard_key);

std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> CreateTree();

}
25 changes: 22 additions & 3 deletions Bread/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
using namespace std;

std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> CreateMauriceTree();

std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> CreateGertrudeTree();
std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> CreateKarlosTree();

int main(int argc, char *argv[])
{
Expand All @@ -42,12 +42,21 @@ int main(int argc, char *argv[])

clients[i]->SetSharedWorld(shared_worlds[i]);
clients[i]->SetAutoRespawn(true);
// Assign unique ID
clients[i]->Connect("127.0.0.1:25565", bot_names[i]);
clients[i]->StartBehaviour();
if (i == 0) {
clients[i]->SetBehaviourTree(CreateMauriceTree());
clients[i]->SetBehaviourTree(CreateMauriceTree(), {
{ "id", i }
});
} else if (i == 1) {
clients[i]->SetBehaviourTree(CreateGertrudeTree());
clients[i]->SetBehaviourTree(CreateGertrudeTree(), {
{ "id", i }
});
} else if (i == 2) {
clients[i]->SetBehaviourTree(CreateKarlosTree(), {
{ "id", i }
});
}

Botcraft::Utilities::SleepFor(std::chrono::seconds(2));
Expand Down Expand Up @@ -123,4 +132,14 @@ std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> CreateGertrudeTree()
.end()
.end();
// clang-format on
}

std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> CreateKarlosTree()
{
// clang-format off
return Botcraft::Builder<AdvancedClient>()
.sequence()
.tree(SurvivalTasks::CreateTree())
.end();
// clang-format on
}
45 changes: 45 additions & 0 deletions Bread/src/tasks/SurvivalTasks.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "tasks/SurvivalTasks.hpp"
#include "botcraft/Game/Vector3.hpp"
#include "botcraft/Utilities/Logger.hpp"

// https://minecraft.fandom.com/wiki/Food
const std::vector<std::string> edible_items({"minecraft:rabbit_stew",
Expand Down Expand Up @@ -44,6 +46,7 @@ Botcraft::Status SurvivalTasks::InitializeBlocks(AdvancedClient &client, const i
{
Botcraft::Blackboard &b = client.GetBlackboard();

/*
auto _ = client.findBlocks(
[&b](const Blockstate *block, const Position position, std::shared_ptr<World> world) -> bool
{
Expand All @@ -62,6 +65,7 @@ Botcraft::Status SurvivalTasks::InitializeBlocks(AdvancedClient &client, const i
return false;
},
search_radius);
*/

b.Set("SurvivalTasks.initialized", true);
LOG_INFO("Blocks initialized");
Expand All @@ -88,6 +92,46 @@ Botcraft::Status SurvivalTasks::FindBestFoodInInventory(AdvancedClient &client)
return Status::Success;
}

// TODO: Fix save bed but different block issue
Botcraft::Status SurvivalTasks::FindUniqueBedBlackboard(AdvancedClient &client, const std::string blackboard_key) {
Botcraft::Blackboard &b = client.GetBlackboard();

const int id = b.Get("id", 0);

auto beds = client.findBlocks(
[](const Blockstate *block, const Position position, std::shared_ptr<World> world) -> bool
{
Position down = position + Position(0, -1, 0);
const std::string &block_name = block->GetName();
const std::string bed_suffix = "_bed";

if (
block_name.size() >= bed_suffix.size() &&
0 == block_name.compare(block_name.size() - bed_suffix.size(), bed_suffix.size(), bed_suffix)
) {
return true;
}

return false;
}
);

if (beds.size() == 0) {
LOG_WARNING("[FindUniqueBedBlackboard] No bed found");
return Status::Failure;
}

std::sort(beds.begin(), beds.end());

Position bed_pos = beds[id % beds.size()];

LOG_INFO("[FindUniqueBedBlackboard] Found bed at " << bed_pos << " for unique id " << id);

b.Set(blackboard_key, bed_pos);

return Status::Success;
}

// Absolutely not stolen code...
std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> CreateSleepTree()
{
Expand Down Expand Up @@ -144,6 +188,7 @@ std::shared_ptr<Botcraft::BehaviourTree<AdvancedClient>> SurvivalTasks::CreateTr
.leaf(CheckBlackboardBoolData, "SurvivalTasks.initialized")
.sequence()
.leaf(SurvivalTasks::InitializeBlocks, 128)
.leaf(SurvivalTasks::FindUniqueBedBlackboard, "SurvivalTasks.bed_pos")
.end()
.end()
.tree(CreateSleepTree())
Expand Down

0 comments on commit e955920

Please sign in to comment.