Fix bucket pickup with scale-aware aiming and build-local Botcraft patches

This commit is contained in:
cantyonion committed 2026-11-24 15:31:16 +08:00
1 parent 0a6193f68e
commit 12c7332337
10 files changed
+416 -40

No files matched your search

+96
View File
@@ -1,6 +1,9 @@
#include "ActionExecutor.h"
#include "ActionFile.h"
#include "BilicraftClient.h"
#include "BucketAim.h"
#include "botcraft/Game/Entities/LocalPlayer.hpp"
#include "botcraft/Game/World/World.hpp"
#include <climits>
#include <stdexcept>
@@ -10,12 +13,105 @@
#include "protocolCraft/Packets/Game/Serverbound/ServerboundContainerClickPacket.hpp"
#include "botcraft/Game/Inventory/Window.hpp"
#include "botcraft/Game/AssetsManager.hpp"
namespace
{
using namespace Bilicraft;
using Botcraft::InventoryType;
TEST(ActionExecutor, EmptyBucketUsesAimedMainHandOnWaterloggedBlocksAndWaterSources)
{
auto &assets = Botcraft::AssetsManager::getInstance();
const Botcraft::Blockstate *wet = nullptr, *dry = nullptr;
for (const auto *state : assets.GetBlockstates("minecraft:oak_slab"))
(state->IsWaterlogged() ? wet : dry) = state;
ASSERT_NE(wet, nullptr);
ASSERT_NE(dry, nullptr);
const auto command = ParseInteractiveCommand("rclick 1 1 1").value();
ProtocolCraft::Slot hand;
hand.SetItemCount(1);
hand.SetItemId(assets.GetItemID("minecraft:bucket"));
const auto click = [&](const Botcraft::Blockstate &state) {
return MakeBlockRightClickPacket(command, state, hand, 73.0f, -18.0f, 42);
};
const auto pickup = std::dynamic_pointer_cast<ProtocolCraft::ServerboundUseItemPacket>(click(*wet));
ASSERT_TRUE(pickup);
EXPECT_EQ(pickup->GetHand(), static_cast<int>(Botcraft::Hand::Main));
EXPECT_EQ(pickup->GetYRot(), 73.0f);
EXPECT_EQ(pickup->GetXRot(), -18.0f);
EXPECT_EQ(pickup->GetSequence(), 42);
EXPECT_TRUE(std::dynamic_pointer_cast<ProtocolCraft::ServerboundUseItemOnPacket>(click(*dry)));
const auto waterStates = assets.GetBlockstates("minecraft:water");
ASSERT_EQ(waterStates.size(), 16u);
for (const auto *water : waterStates)
{
const auto &level = water->GetVariableValue("level");
SCOPED_TRACE(level);
if (level == "0")
EXPECT_TRUE(std::dynamic_pointer_cast<ProtocolCraft::ServerboundUseItemPacket>(click(*water)));
else
EXPECT_TRUE(std::dynamic_pointer_cast<ProtocolCraft::ServerboundUseItemOnPacket>(click(*water)));
}
hand.SetItemId(assets.GetItemID("minecraft:water_bucket"));
for (const auto *water : waterStates)
EXPECT_TRUE(std::dynamic_pointer_cast<ProtocolCraft::ServerboundUseItemOnPacket>(click(*water)));
EXPECT_TRUE(std::dynamic_pointer_cast<ProtocolCraft::ServerboundUseItemOnPacket>(click(*wet)));
}
TEST(ActionExecutor, BucketCanReachSourceThroughGapBelowUpperSlab)
{
auto &assets = Botcraft::AssetsManager::getInstance();
Botcraft::World world(false);
const std::string dimension = "minecraft:overworld";
world.SetDimensionMinY(dimension, 0);
world.SetDimensionHeight(dimension, 256);
world.LoadChunk(0, 0, dimension);
const Botcraft::Position target(5, 93, 5);
world.SetBlock({5, 93, 4}, assets.GetBlockstate("minecraft:oak_planks")->GetId());
for (const auto *slab : assets.GetBlockstates("minecraft:oak_slab"))
if (slab->GetVariableValue("type") == "top")
world.SetBlock({5, slab->IsWaterlogged() ? 92 : 94, 5}, slab->GetId());
for (const auto *water : assets.GetBlockstates("minecraft:water"))
if (water->GetVariableValue("level") == "0") world.SetBlock(target, water->GetId());
Botcraft::LocalPlayer player;
player.SetPosition({5.438707, 94, 4.368732});
for (const double scale : {1.0, 0.58522856})
{
SCOPED_TRACE(scale);
player.SetAttributeBaseValue(Botcraft::EntityAttribute::Type::Scale, scale);
const auto eyes = player.GetPosition() + Botcraft::Vector3<double>(0, player.GetEyeHeight(), 0);
const auto aim = FindBucketAim(world, eyes, target, 4.5);
ASSERT_TRUE(aim);
player.LookAt(*aim, true);
const auto direction = player.GetFrontVector();
const double frontY = 94 + 1.62 * scale + direction.y * (5 - eyes.z) / direction.z;
EXPECT_GT(frontY, 94.0);
EXPECT_LT(frontY, 94.5);
EXPECT_FALSE(FindBucketAim(world, eyes, {5, 92, 5}, 4.5));
}
}
TEST(ActionExecutor, EyeHeightAndLookAtFollowChangesToScaleAndPose)
{
Botcraft::LocalPlayer player;
player.SetPosition({0, 10, 0});
for (const double scale : {0.58522856, 2.0, 1.0})
{
SCOPED_TRACE(scale);
player.SetAttributeBaseValue(Botcraft::EntityAttribute::Type::Scale, scale);
for (const auto [pose, height] : {std::pair{Botcraft::Pose::Standing, 1.62},
std::pair{Botcraft::Pose::Crouching, 1.27},
std::pair{Botcraft::Pose::Swimming, 0.4}})
{
player.SetDataPose(pose);
EXPECT_DOUBLE_EQ(player.GetEyeHeight(), height * scale);
player.LookAt({0, 10 + height * scale, 2}, true);
EXPECT_NEAR(player.GetPitch(), 0, 1e-5);
}
}
}
TEST(ActionExecutor, MapsEveryGenericContainerSize)
{
const InventoryType types[] = {InventoryType::Generic9x1, InventoryType::Generic9x2, InventoryType::Generic9x3,
+23
View File
@@ -2,6 +2,7 @@
#include <gtest/gtest.h>
#include "botcraft/Game/AssetsManager.hpp"
#include "botcraft/Game/Inventory/InventoryManager.hpp"
#include "botcraft/Game/Inventory/Window.hpp"
@@ -44,6 +45,28 @@ class InventorySync : public testing::Test
}
};
TEST_F(InventorySync, FilledBucketConfirmationCountsServerUpdatesAcrossInventory)
{
Full();
auto &assets = Botcraft::AssetsManager::getInstance();
auto emptyBuckets = Item(2);
emptyBuckets.SetItemId(assets.GetItemID("minecraft:bucket"));
auto filled = Item(1);
filled.SetItemId(assets.GetItemID("minecraft:water_bucket"));
PlayerSlot(0, emptyBuckets);
PlayerSlot(1, filled);
const auto window = inventory.GetPlayerInventory();
const auto before = Bilicraft::CountWaterBuckets(*window);
ASSERT_EQ(before, 1);
emptyBuckets.SetItemCount(1);
PlayerSlot(0, emptyBuckets);
EXPECT_EQ(Bilicraft::CountWaterBuckets(*window), before);
PlayerSlot(9, filled);
EXPECT_EQ(Bilicraft::CountWaterBuckets(*window), before + 1);
PlayerSlot(0, filled);
EXPECT_EQ(Bilicraft::CountWaterBuckets(*window), before + 2);
}
TEST_F(InventorySync, EmptyHotbarUpdateClearsHeldItemInsteadOfCraftingOutput)
{
Full();