Files
bilicraft-bot/tests/ActionExecutorTests.cpp

328 lines
14 KiB
C++

#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>
#include <string>
#include <gtest/gtest.h>
#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,
InventoryType::Generic9x4, InventoryType::Generic9x5, InventoryType::Generic9x6};
for (int rows = 1; rows <= 6; ++rows)
{
EXPECT_EQ(ActionGuiSlot(types[rows - 1], 1, 1), 0);
EXPECT_EQ(ActionGuiSlot(types[rows - 1], rows, 9), rows * 9 - 1);
EXPECT_THROW(ActionGuiSlot(types[rows - 1], rows + 1, 1), std::runtime_error);
}
EXPECT_EQ(ActionGuiSlot(InventoryType::ShulkerBox, 3, 9), 26);
EXPECT_THROW(ActionGuiSlot(InventoryType::ShulkerBox, 4, 1), std::runtime_error);
}
TEST(ActionExecutor, RejectsNonNineColumnGuiRatherThanGuessing)
{
for (const auto type : {InventoryType::PlayerInventory, InventoryType::Default, InventoryType::Generic3x3,
InventoryType::Furnace, InventoryType::Hopper, InventoryType::Crafting,
InventoryType::Merchant, InventoryType::Anvil})
EXPECT_THROW(ActionGuiSlot(type, 1, 1), std::runtime_error);
}
TEST(ActionExecutor, RejectsGuiBoundsBeforeArithmetic)
{
for (int bad : {INT_MIN, -1, 0, 10, INT_MAX})
EXPECT_THROW(ActionGuiSlot(InventoryType::Generic9x6, 1, bad), std::runtime_error);
for (int bad : {INT_MIN, -1, 0, 7, INT_MAX})
EXPECT_THROW(ActionGuiSlot(InventoryType::Generic9x6, bad, 1), std::runtime_error);
}
TEST(ActionExecutor, MapsAllBackpackAndHotbarPositions)
{
for (int row = 1; row <= 3; ++row)
for (int col = 1; col <= 9; ++col)
EXPECT_EQ(ActionBackpackSlot(row, col), 9 + (row - 1) * 9 + col - 1);
for (int slot = 1; slot <= 9; ++slot)
EXPECT_EQ(ActionHotbarIndex(slot), slot - 1);
}
TEST(ActionExecutor, RejectsBackpackAndHotbarBounds)
{
for (int bad : {INT_MIN, -1, 0, 4, INT_MAX})
EXPECT_THROW(ActionBackpackSlot(bad, 1), std::runtime_error);
for (int bad : {INT_MIN, -1, 0, 10, INT_MAX})
{
EXPECT_THROW(ActionBackpackSlot(1, bad), std::runtime_error);
EXPECT_THROW(ActionHotbarIndex(bad), std::runtime_error);
}
}
TEST(ActionExecutor, BuildsRealNumberKeySwapWithoutCursorPrediction)
{
const auto packet = MakeBackpackSwapPacket(3, 9, 9);
EXPECT_EQ(packet->GetContainerId(), 0);
EXPECT_EQ(packet->GetStateId(), -1);
EXPECT_EQ(packet->GetSlotNum(), 35);
EXPECT_EQ(packet->GetButtonNum(), 8);
EXPECT_EQ(packet->GetClickType(), 2);
EXPECT_TRUE(packet->GetChangedSlots().empty());
EXPECT_FALSE(packet->GetCarriedItem().GetActualItem().has_value());
ProtocolCraft::WriteContainer bytes;
packet->Write(bytes);
EXPECT_FALSE(bytes.empty());
}
TEST(ActionExecutor, SwapPacketRejectsInvalidPositions)
{
EXPECT_THROW(MakeBackpackSwapPacket(0, 1, 1), std::runtime_error);
EXPECT_THROW(MakeBackpackSwapPacket(1, 10, 1), std::runtime_error);
EXPECT_THROW(MakeBackpackSwapPacket(1, 1, 10), std::runtime_error);
}
TEST(ActionExecutor, ResolvesDropTargetWithoutChangingTheSelection)
{
for (int selected = 0; selected < 9; ++selected)
{
EXPECT_EQ(ActionDropSlot(0, selected), selected + 1);
for (int requested = 1; requested <= 9; ++requested)
EXPECT_EQ(ActionDropSlot(requested, selected), requested);
}
for (int invalid : {INT_MIN, -1, 9, INT_MAX})
EXPECT_THROW(ActionDropSlot(0, invalid), std::runtime_error);
for (int invalid : {INT_MIN, -1, 10, INT_MAX})
EXPECT_THROW(ActionDropSlot(invalid, 0), std::runtime_error);
}
TEST(ActionExecutor, BuildsSingleAndWholeStackDropPacketsForEveryHotbarSlot)
{
for (int slot = 1; slot <= 9; ++slot)
for (bool all : {false, true})
{
const auto packet = MakeHotbarDropPacket(slot, all);
EXPECT_EQ(packet->GetContainerId(), 0);
EXPECT_EQ(packet->GetStateId(), -1);
EXPECT_EQ(packet->GetSlotNum(), 35 + slot);
EXPECT_EQ(packet->GetClickType(), 4);
EXPECT_EQ(packet->GetButtonNum(), all ? 1 : 0);
EXPECT_TRUE(packet->GetChangedSlots().empty());
EXPECT_FALSE(packet->GetCarriedItem().GetActualItem().has_value());
ProtocolCraft::WriteContainer bytes;
packet->Write(bytes);
EXPECT_FALSE(bytes.empty());
}
for (int invalid : {INT_MIN, -1, 0, 10, INT_MAX})
EXPECT_THROW(MakeHotbarDropPacket(invalid, false), std::runtime_error);
}
TEST(ActionExecutor, DropRemainderHandlesEmptySingleAndFullStacksWithoutMutatingInput)
{
const ProtocolCraft::Slot empty;
EXPECT_TRUE(RemainingAfterDrop(empty, false).IsEmptySlot());
EXPECT_TRUE(RemainingAfterDrop(empty, true).IsEmptySlot());
ProtocolCraft::Slot original;
original.SetItemId(1);
for (int count : {1, 2, 64})
{
original.SetItemCount(count);
const auto remaining = RemainingAfterDrop(original, false);
EXPECT_EQ(remaining.GetItemCount(), count - 1);
EXPECT_EQ(remaining.IsEmptySlot(), count == 1);
if (count > 1)
{
auto expected = original;
expected.SetItemCount(count - 1);
ProtocolCraft::WriteContainer actualBytes, expectedBytes;
remaining.Write(actualBytes);
expected.Write(expectedBytes);
EXPECT_EQ(actualBytes, expectedBytes);
}
EXPECT_TRUE(RemainingAfterDrop(original, true).IsEmptySlot());
EXPECT_EQ(original.GetItemCount(), count);
}
}
TEST(ActionExecutor, DropDistinguishesKnownEmptySlotsFromInventoryReset)
{
Botcraft::Window window(InventoryType::PlayerInventory);
EXPECT_THROW(ReadDropSlot(window, 36), std::runtime_error);
window.SetSlot(36, ProtocolCraft::Slot());
EXPECT_TRUE(ReadDropSlot(window, 36).IsEmptySlot());
ProtocolCraft::Slot stack;
stack.SetItemId(1);
stack.SetItemCount(4);
window.SetSlot(36, stack);
EXPECT_EQ(ReadDropSlot(window, 36).GetItemCount(), 4);
window.Init(); // Clears the contents but preserves window identity.
EXPECT_THROW(ReadDropSlot(window, 36), std::runtime_error);
}
TEST(ActionExecutor, OfflineFailureIncludesSourceAndLine)
{
BilicraftClient client;
ActionCommand command{};
command.kind = ActionKind::SendCommand;
command.text = "/help";
command.source = "offline.act";
command.line = 42;
try
{
ExecuteAction(client, command);
FAIL() << "Offline execution should fail before sending a packet";
}
catch (const std::runtime_error &error)
{
EXPECT_NE(std::string(error.what()).find("offline.act:42:"), std::string::npos);
}
}
TEST(ActionExecutor, ValidatesWorldAndPackedCoordinateBounds)
{
EXPECT_NO_THROW(ValidateActionPosition(-29999984, -2048, 29999984));
EXPECT_NO_THROW(ValidateActionPosition(29999984, 2047, -29999984));
EXPECT_THROW(ValidateActionPosition(29999985, 0, 0), std::runtime_error);
EXPECT_THROW(ValidateActionPosition(0, -2049, 0), std::runtime_error);
EXPECT_THROW(ValidateActionPosition(0, 2048, 0), std::runtime_error);
EXPECT_THROW(ValidateActionPosition(0, 0, INT_MIN), std::runtime_error);
EXPECT_THROW(ValidateActionPosition(INT_MAX, 0, 0), std::runtime_error);
}
TEST(ActionExecutor, MaintainingInputsWithoutShiftDoesNotRequirePlayer)
{
BilicraftClient client;
EXPECT_NO_THROW(MaintainActionInputs(client));
}
TEST(ActionExecutor, ItemOnlyClicksNeverAttackKnownEmptyFrames)
{
EXPECT_EQ(PlanFrameAttack(false, false, false), FrameAttackIntent::None);
EXPECT_EQ(PlanFrameAttack(false, false, true), FrameAttackIntent::None);
EXPECT_EQ(PlanFrameAttack(false, true, false), FrameAttackIntent::TakeItem);
EXPECT_THROW(PlanFrameAttack(false, true, true), std::runtime_error);
}
TEST(ActionExecutor, ExplicitFrameRemovalPlansOnlyTheNecessaryPhases)
{
for (bool creative : {false, true})
{
EXPECT_EQ(PlanFrameAttack(true, false, creative), FrameAttackIntent::RemoveFrame);
EXPECT_EQ(PlanFrameAttack(true, true, creative), FrameAttackIntent::TakeItem);
}
}
TEST(ActionExecutor, ChangedFrameContentsCannotTurnTakeItemIntoAnExtraRemovalAttack)
{
EXPECT_NO_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, true, true, false));
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, true, false, false), std::runtime_error);
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, false, false, false), std::runtime_error);
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, false, true, true), std::runtime_error);
EXPECT_NO_THROW(ValidateFrameAttack(FrameAttackIntent::RemoveFrame, true, false, false));
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::RemoveFrame, true, true, false), std::runtime_error);
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::None, false, false, false), std::runtime_error);
}
} // namespace