Fix bucket pickup with scale-aware aiming and build-local Botcraft patches
This commit is contained in:
1 parent
0a6193f68e
commit
12c7332337
10 files changed
+416
-40
No files matched your search
+119
-14
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "ActionFile.h"
|
||||
#include "BilicraftClient.h"
|
||||
#include "BucketAim.h"
|
||||
#include "EntityInteraction.h"
|
||||
|
||||
#include <chrono>
|
||||
@@ -14,6 +15,7 @@
|
||||
#include "botcraft/AI/Tasks/DigTask.hpp"
|
||||
#include "botcraft/AI/Tasks/InventoryTasks.hpp"
|
||||
#include "botcraft/AI/Tasks/PathfindingTask.hpp"
|
||||
#include "botcraft/Game/AssetsManager.hpp"
|
||||
#include "botcraft/Game/Entities/LocalPlayer.hpp"
|
||||
#include "botcraft/Game/Entities/EntityManager.hpp"
|
||||
#include "botcraft/Game/Entities/entities/decoration/ItemFrameEntity.hpp"
|
||||
@@ -159,7 +161,7 @@ Botcraft::Vector3<double> BlockCenter(const ActionCommand &command)
|
||||
return Botcraft::Vector3<double>(command.x + 0.5, command.y + 0.5, command.z + 0.5);
|
||||
}
|
||||
|
||||
void CheckTarget(BilicraftClient &client, const ActionCommand &command)
|
||||
const Botcraft::Blockstate &CheckTarget(BilicraftClient &client, const ActionCommand &command)
|
||||
{
|
||||
const Botcraft::Position pos(command.x, command.y, command.z);
|
||||
Await(client, [&] { return client.GetWorld()->GetBlock(pos) != nullptr; }, 5s, "target chunk to load");
|
||||
@@ -172,6 +174,7 @@ void CheckTarget(BilicraftClient &client, const ActionCommand &command)
|
||||
// reach boundaries. This does not claim to implement server line of sight.
|
||||
Require(range > 0 && eyes.SqrDist(BlockCenter(command)) < range * range,
|
||||
"target is outside block interaction reach; use goto explicitly");
|
||||
return *block;
|
||||
}
|
||||
|
||||
void Swing(BilicraftClient &client)
|
||||
@@ -181,12 +184,32 @@ void Swing(BilicraftClient &client)
|
||||
client.GetNetworkManager()->Send(packet);
|
||||
}
|
||||
|
||||
bool IsWaterSource(const Botcraft::Blockstate &block)
|
||||
{
|
||||
return block.GetName() == "minecraft:water" && block.GetVariableValue("level") == "0";
|
||||
}
|
||||
|
||||
bool HoldsEmptyBucket(const ProtocolCraft::Slot &hand)
|
||||
{
|
||||
static const auto bucket = Botcraft::AssetsManager::getInstance().GetItemID("minecraft:bucket");
|
||||
return !hand.IsEmptySlot() && hand.GetItemId() == bucket;
|
||||
}
|
||||
|
||||
std::string DescribeBlock(const Botcraft::Blockstate *block)
|
||||
{
|
||||
if (!block) return "unloaded";
|
||||
std::string description = block->GetName();
|
||||
for (const auto &[key, value] : block->GetVariables())
|
||||
description += " " + key + "=" + value;
|
||||
return description;
|
||||
}
|
||||
|
||||
Status ClickBlock(BilicraftClient &client, const ActionCommand &command)
|
||||
{
|
||||
CheckTarget(client, command);
|
||||
if (Botcraft::LookAt(client, BlockCenter(command), true, true) != Status::Success)
|
||||
return Status::Failure;
|
||||
CheckTarget(client, command); // The world/player may have changed during Yield.
|
||||
const auto &block = CheckTarget(client, command); // The world/player may have changed during Yield.
|
||||
ScopedSneak sneak(client, command.shift);
|
||||
const Botcraft::Position pos(command.x, command.y, command.z);
|
||||
if (command.kind == ActionKind::LeftClick)
|
||||
@@ -199,20 +222,60 @@ Status ClickBlock(BilicraftClient &client, const ActionCommand &command)
|
||||
return Status::Success;
|
||||
}
|
||||
|
||||
// InteractWithBlock unconditionally pathfinds when its own range check
|
||||
// fails. Use its verified packet layout directly to guarantee no movement.
|
||||
auto packet = std::make_shared<ProtocolCraft::ServerboundUseItemOnPacket>();
|
||||
packet->SetLocation(pos.ToNetworkPosition());
|
||||
packet->SetDirection(static_cast<int>(Botcraft::PlayerDiggingFace::Up));
|
||||
packet->SetCursorPositionX(0.5f);
|
||||
packet->SetCursorPositionY(1.0f);
|
||||
packet->SetCursorPositionZ(0.5f);
|
||||
packet->SetInside(false);
|
||||
packet->SetWorldBorderHit(false);
|
||||
packet->SetHand(static_cast<int>(Botcraft::Hand::Main));
|
||||
packet->SetSequence(client.GetWorld()->GetNextWorldInteractionSequenceId());
|
||||
const auto player = client.GetLocalPlayer();
|
||||
const auto inventory = client.GetInventoryManager();
|
||||
const auto window = inventory->GetPlayerInventory();
|
||||
const auto slot = Botcraft::Window::INVENTORY_HOTBAR_START + inventory->GetIndexHotbarSelected();
|
||||
const auto hand = window->GetSlot(slot);
|
||||
const bool emptyBucket = HoldsEmptyBucket(hand);
|
||||
const bool pickup = emptyBucket && (block.IsWaterlogged() || IsWaterSource(block));
|
||||
const int waterBucketsBefore = pickup ? CountWaterBuckets(*window) : 0;
|
||||
const auto world = client.GetWorld();
|
||||
if (pickup)
|
||||
{
|
||||
const auto eyes = player->GetPosition() + Botcraft::Vector3<double>(0, player->GetEyeHeight(), 0);
|
||||
const auto aim = FindBucketAim(*world, eyes, pos, player->GetAttributePlayerBlockInteractionRangeValue());
|
||||
Require(aim.has_value(), "no visible point for bucket pickup at (" + std::to_string(pos.x) + "," +
|
||||
std::to_string(pos.y) + "," + std::to_string(pos.z) + "); change standing position");
|
||||
if (Botcraft::LookAt(client, *aim, true, true) != Status::Success) return Status::Failure;
|
||||
Require(&CheckTarget(client, command) == &block && inventory->GetIndexHotbarSelected() ==
|
||||
slot - Botcraft::Window::INVENTORY_HOTBAR_START && HoldsEmptyBucket(window->GetSlot(slot)),
|
||||
"target or main hand changed while aiming for bucket pickup");
|
||||
}
|
||||
const auto yaw = player->GetYaw();
|
||||
const auto pitch = player->GetPitch();
|
||||
auto packet = MakeBlockRightClickPacket(command, block, hand, yaw, pitch, world->GetNextWorldInteractionSequenceId());
|
||||
if (emptyBucket || block.IsWaterlogged() || IsWaterSource(block))
|
||||
{
|
||||
const auto *item = hand.IsEmptySlot() ? nullptr : Botcraft::AssetsManager::getInstance().GetItem(hand.GetItemId());
|
||||
LOG_INFO("rclick (" << pos.x << "," << pos.y << "," << pos.z << ") " << DescribeBlock(&block)
|
||||
<< "; main hand=" << (hand.IsEmptySlot() ? "empty" :
|
||||
(item ? item->GetName() : std::to_string(hand.GetItemId())))
|
||||
<< "; " << (pickup ? "bucket pickup" : "block interaction"));
|
||||
}
|
||||
client.GetNetworkManager()->Send(packet);
|
||||
Swing(client);
|
||||
if (pickup)
|
||||
{
|
||||
try
|
||||
{
|
||||
Await(client, [&] {
|
||||
Require(client.GetWorld() == world && inventory->GetPlayerInventory() == window,
|
||||
"world or inventory changed during bucket pickup");
|
||||
if (CountWaterBuckets(*window) > waterBucketsBefore) return true;
|
||||
// Creative may keep an empty bucket even after successfully draining a block.
|
||||
const auto *now = world->GetBlock(pos);
|
||||
return player->GetInstabuild() && now && !now->IsWaterlogged() && !IsWaterSource(*now);
|
||||
}, 5s, "server confirmation of bucket pickup (not retried)");
|
||||
}
|
||||
catch (const std::exception &error)
|
||||
{
|
||||
throw std::runtime_error(std::string(error.what()) + "; target=(" + std::to_string(pos.x) + "," +
|
||||
std::to_string(pos.y) + "," + std::to_string(pos.z) + ") " + DescribeBlock(world->GetBlock(pos)) +
|
||||
"; water buckets before=" + std::to_string(waterBucketsBefore) +
|
||||
", now=" + std::to_string(CountWaterBuckets(*window)));
|
||||
}
|
||||
}
|
||||
return Status::Success;
|
||||
}
|
||||
|
||||
@@ -438,6 +501,48 @@ Status DropHotbar(BilicraftClient &client, const ActionCommand &command)
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int CountWaterBuckets(const Botcraft::Window &window)
|
||||
{
|
||||
static const auto waterBucket = Botcraft::AssetsManager::getInstance().GetItemID("minecraft:water_bucket");
|
||||
int count = 0;
|
||||
const auto slots = window.GetLockedSlots();
|
||||
for (const auto &[index, stack] : *slots)
|
||||
if (index >= Botcraft::Window::INVENTORY_STORAGE_START && index <= Botcraft::Window::INVENTORY_OFFHAND_INDEX &&
|
||||
!stack.IsEmptySlot() && stack.GetItemId() == waterBucket)
|
||||
count += stack.GetItemCount();
|
||||
return count;
|
||||
}
|
||||
|
||||
std::shared_ptr<ProtocolCraft::Packet> MakeBlockRightClickPacket(const ActionCommand &command,
|
||||
const Botcraft::Blockstate &block, const ProtocolCraft::Slot &mainHand, float yaw, float pitch, int sequence)
|
||||
{
|
||||
if ((block.IsWaterlogged() || IsWaterSource(block)) && HoldsEmptyBucket(mainHand))
|
||||
{
|
||||
// The server raycasts from the player's eyes along this rotation.
|
||||
auto packet = std::make_shared<ProtocolCraft::ServerboundUseItemPacket>();
|
||||
packet->SetHand(static_cast<int>(Botcraft::Hand::Main));
|
||||
packet->SetSequence(sequence);
|
||||
packet->SetYRot(yaw);
|
||||
packet->SetXRot(pitch);
|
||||
return packet;
|
||||
}
|
||||
|
||||
const Botcraft::Position pos(command.x, command.y, command.z);
|
||||
// InteractWithBlock unconditionally pathfinds when its own range check
|
||||
// fails. Use its verified packet layout directly to guarantee no movement.
|
||||
auto packet = std::make_shared<ProtocolCraft::ServerboundUseItemOnPacket>();
|
||||
packet->SetLocation(pos.ToNetworkPosition());
|
||||
packet->SetDirection(static_cast<int>(Botcraft::PlayerDiggingFace::Up));
|
||||
packet->SetCursorPositionX(0.5f);
|
||||
packet->SetCursorPositionY(1.0f);
|
||||
packet->SetCursorPositionZ(0.5f);
|
||||
packet->SetInside(false);
|
||||
packet->SetWorldBorderHit(false);
|
||||
packet->SetHand(static_cast<int>(Botcraft::Hand::Main));
|
||||
packet->SetSequence(sequence);
|
||||
return packet;
|
||||
}
|
||||
|
||||
void ValidateActionPosition(int x, int y, int z)
|
||||
{
|
||||
// NetworkPosition packs signed 26/12/26-bit coordinates. Restrict X/Z to
|
||||
|
||||
Reference in new issue
Block a user