Skip to content

Commit

Permalink
Update botcraft to 1.20.5 new things
Browse files Browse the repository at this point in the history
  • Loading branch information
adepierre committed May 6, 2024
1 parent 05f05ab commit cece1c2
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 132 deletions.
Expand Up @@ -3,7 +3,6 @@
#include <iosfwd>
#include <map>
#include <optional>
#include <string>

namespace Botcraft
{
Expand Down
3 changes: 2 additions & 1 deletion botcraft/include/botcraft/Game/World/Blockstate.hpp
Expand Up @@ -154,10 +154,11 @@ namespace Botcraft
/// @param fatigue Level of mining fatigue applied to the player
/// @param on_ground Boolean indicating whether or not the player is currently on the ground
/// @param head_in_fluid_wo_aqua_affinity Boolean indicating whether or not the player head is currently in fluid without aqua affinity enchantment
/// @param speed_factor Speed multiplier to apply
/// @return The time required to mine this block in these conditions, or -1 if can't be mined
float GetMiningTimeSeconds(const ToolType tool_type, const ToolMaterial tool_material,
const unsigned char tool_efficiency = 0, const unsigned char haste = 0, const unsigned char fatigue = 0,
const bool on_ground = true, const bool head_in_fluid_wo_aqua_affinity = false) const;
const bool on_ground = true, const bool head_in_fluid_wo_aqua_affinity = false, const float speed_factor = 1.0f) const;

#if PROTOCOL_VERSION < 347 /* < 1.13 */
static unsigned int IdMetadataToId(const int id_, const unsigned char metadata_);
Expand Down
3 changes: 3 additions & 0 deletions botcraft/include/botcraft/Network/NetworkManager.hpp
Expand Up @@ -76,6 +76,9 @@ namespace Botcraft
virtual void Handle(ProtocolCraft::ClientboundChunkBatchStartPacket& msg) override;
virtual void Handle(ProtocolCraft::ClientboundChunkBatchFinishedPacket& msg) override;
#endif
#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
virtual void Handle(ProtocolCraft::ClientboundSelectKnownPacksPacket& msg) override;
#endif

private:
std::vector<ProtocolCraft::Handler*> subscribed;
Expand Down
3 changes: 3 additions & 0 deletions botcraft/include/botcraft/Utilities/ItemUtilities.hpp
Expand Up @@ -2,13 +2,16 @@

#include "botcraft/Game/Enums.hpp"

#include <string_view>

namespace ProtocolCraft
{
class Slot;
}

namespace Botcraft::Utilities
{
std::string_view EnchantmentToString(const Botcraft::Enchantment enchantment);
short GetEnchantmentLvl(const ProtocolCraft::Slot& item, const Botcraft::Enchantment enchantment);
int GetDamageCount(const ProtocolCraft::Slot& item);
}
10 changes: 8 additions & 2 deletions botcraft/src/AI/Tasks/BaseTasks.cpp
Expand Up @@ -69,10 +69,16 @@ namespace Botcraft
// Compute the distance from the hand? Might be from somewhere else
const Vector3<double> player_hand_pos = local_player->GetPosition() + Vector3<double>(0.0, 1.0, 0.0);

if (player_hand_pos.SqrDist(Vector3<double>(0.5, 0.5, 0.5) + pos) > 16.0f)
#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
constexpr double range = 4.5;
#else
const double range = local_player->GetAttributePlayerBlockInteractionRangeValue();
#endif

if (player_hand_pos.SqrDist(Vector3<double>(0.5, 0.5, 0.5) + pos) > range * range)
{
// Go in range
if (GoTo(client, pos, 4) == Status::Failure)
if (GoTo(client, pos, static_cast<int>(range)) == Status::Failure)
{
return Status::Failure;
}
Expand Down
19 changes: 18 additions & 1 deletion botcraft/src/AI/Tasks/DigTask.cpp
Expand Up @@ -112,7 +112,24 @@ namespace Botcraft
}
}

const float expected_mining_time_s = local_player->GetInstabuild() ? 0.0f : blockstate->GetMiningTimeSeconds(current_tool_type, current_tool_material, current_tool_efficiency, haste_amplifier, mining_fatigue_amplifier, is_on_ground, is_head_in_fluid);
#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
constexpr float speed_multiplier = 1.0f;
#else
const float speed_multiplier = static_cast<float>(local_player->GetAttributePlayerBlockBreakSpeedValue());
#endif

const float expected_mining_time_s =
local_player->GetInstabuild() ? 0.0f :
blockstate->GetMiningTimeSeconds(
current_tool_type,
current_tool_material,
current_tool_efficiency,
haste_amplifier,
mining_fatigue_amplifier,
is_on_ground,
is_head_in_fluid,
speed_multiplier
);
if (expected_mining_time_s > 60.0f)
{
LOG_INFO("Starting an expected " << expected_mining_time_s << " seconds long mining at " << pos << ".A little help?");
Expand Down
10 changes: 8 additions & 2 deletions botcraft/src/AI/Tasks/EntitiesTasks.cpp
Expand Up @@ -31,9 +31,15 @@ namespace Botcraft
Vector3<double> entity_position = entity->GetPosition();
Vector3<double> position = local_player->GetPosition();

while (position.SqrDist(entity_position) > 16.0)
#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
constexpr double range = 3.0;
#else
const double range = local_player->GetAttributePlayerEntityInteractionRangeValue();
#endif

while (position.SqrDist(entity_position) > range * range)
{
if (GoTo(client, entity_position, 3, 0) == Status::Failure)
if (GoTo(client, entity_position, static_cast<int>(range)) == Status::Failure)
{
return Status::Failure;
}
Expand Down
6 changes: 3 additions & 3 deletions botcraft/src/Game/Inventory/InventoryManager.cpp
Expand Up @@ -485,21 +485,21 @@ namespace Botcraft
}

#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
std::vector<Trade> InventoryManager::GetAvailableTrades() const
std::vector<MerchantOffer> InventoryManager::GetAvailableMerchantOffers() const
{
std::shared_lock<std::shared_mutex> lock(inventory_manager_mutex);
return available_trades;
}

void InventoryManager::IncrementTradeUse(const int index)
void InventoryManager::IncrementMerchantOfferUse(const int index)
{
std::scoped_lock<std::shared_mutex> lock(inventory_manager_mutex);
if (index < 0 || index > available_trades.size())
{
LOG_WARNING("Trying to update trade use of an invalid trade (" << index << "<" << available_trades.size() << ")");
return;
}
Trade& trade = available_trades[index];
MerchantOffer& trade = available_trades[index];
trade.SetNumberOfTradesUses(trade.GetNumberOfTradesUses() + 1);
}
#endif
Expand Down
44 changes: 36 additions & 8 deletions botcraft/src/Game/Physics/PhysicsManager.cpp
Expand Up @@ -654,13 +654,27 @@ namespace Botcraft
block_jump_factor = 0.4f;
}

#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
player->speed.y = 0.42f * block_jump_factor + jump_boost;
if (player->GetDataSharedFlagsIdImpl(EntitySharedFlagsId::Sprinting))
{
const float yaw_rad = player->yaw * 0.017453292f /* PI/180 */;
player->speed.x -= std::sin(yaw_rad) * 0.2f;
player->speed.z += std::cos(yaw_rad) * 0.2f;
}
#else
const float jump_power = static_cast<float>(player->GetAttributeJumpStrengthValueImpl()) * block_jump_factor + jump_boost;
if (jump_power > 1e-5f)
{
player->speed.y = jump_power;
if (player->GetDataSharedFlagsIdImpl(EntitySharedFlagsId::Sprinting))
{
const float yaw_rad = player->yaw * 0.017453292f /* PI/180 */;
player->speed.x -= static_cast<double>(std::sin(yaw_rad)) * 0.2;
player->speed.z += static_cast<double>(std::cos(yaw_rad)) * 0.2;
}
}
#endif
player->jump_delay = 10;
}
}
Expand Down Expand Up @@ -887,7 +901,12 @@ namespace Botcraft
}
}
#endif

#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
const double drag = (going_down && has_slow_falling) ? 0.01 : 0.08;
#else
const double drag = (going_down && has_slow_falling) ? std::min(0.01, player->GetAttributeGravityValueImpl()) : player->GetAttributeGravityValueImpl();
#endif

// Move in water
if (player->in_water && !player->flying)
Expand Down Expand Up @@ -1081,22 +1100,31 @@ namespace Botcraft
player->speed *= 0.0;
}

#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
constexpr double max_up_step = 0.6;
#else
const double max_up_step = player->GetAttributeStepHeightValueImpl();
#endif
const AABB player_aabb = player->GetColliderImpl();
if (!player->flying && movement.y <= 0.0 && player->inputs.sneak && player->on_ground)
if (!player->flying
&& movement.y <= 0.0
&& player->inputs.sneak
&& player->on_ground
)
{ // Player::maybeBackOffFromEdge
const double step = 0.05;

while (movement.x != 0.0 && world->IsFree((player_aabb + Vector3<double>(movement.x, -0.6, 0.0)).Inflate(-1e-7), false))
while (movement.x != 0.0 && world->IsFree((player_aabb + Vector3<double>(movement.x, -max_up_step, 0.0)).Inflate(-1e-7), false))
{
movement.x = (movement.x < step && movement.x >= -step) ? 0.0 : (movement.x > 0.0 ? (movement.x - step) : (movement.x + step));
}

while (movement.z != 0.0 && world->IsFree((player_aabb + Vector3<double>(0.0, -0.6, movement.z)).Inflate(-1e-7), false))
while (movement.z != 0.0 && world->IsFree((player_aabb + Vector3<double>(0.0, -max_up_step, movement.z)).Inflate(-1e-7), false))
{
movement.z = (movement.z < step && movement.z >= -step) ? 0.0 : (movement.z > 0.0 ? (movement.z - step) : (movement.z + step));
}

while (movement.x != 0.0 && movement.z != 0.0 && world->IsFree((player_aabb + Vector3<double>(movement.x, -0.6, movement.z)).Inflate(-1e-7), false))
while (movement.x != 0.0 && movement.z != 0.0 && world->IsFree((player_aabb + Vector3<double>(movement.x, -max_up_step, movement.z)).Inflate(-1e-7), false))
{
movement.x = (movement.x < step && movement.x >= -step) ? 0.0 : (movement.x > 0.0 ? (movement.x - step) : (movement.x + step));
movement.z = (movement.z < step && movement.z >= -step) ? 0.0 : (movement.z > 0.0 ? (movement.z - step) : (movement.z + step));
Expand All @@ -1110,19 +1138,19 @@ namespace Botcraft
movement = CollideBoundingBox(player_aabb, movement);
}

// Step up if block height delta is < 0.6
// Step up if block height delta is < max_up_step
// If already on ground (or collided with the ground while moving down) and horizontal collision
if ((player->on_ground || (movement.y != movement_before_collisions.y && movement_before_collisions.y < 0.0)) &&
(movement.x != movement_before_collisions.x || movement.z != movement_before_collisions.z))
{
Vector3<double> movement_with_step_up = CollideBoundingBox(player_aabb, Vector3<double>(movement_before_collisions.x, 0.6, movement_before_collisions.z));
Vector3<double> movement_with_step_up = CollideBoundingBox(player_aabb, Vector3<double>(movement_before_collisions.x, max_up_step, movement_before_collisions.z));
const Vector3<double> horizontal_movement(
movement_before_collisions.x,
0.0,
movement_before_collisions.z
);
const Vector3<double> movement_step_up_only = CollideBoundingBox(AABB(player_aabb.GetCenter() + horizontal_movement * 0.5, player_aabb.GetHalfSize() + horizontal_movement.Abs() * 0.5), Vector3<double>(0.0, 0.6, 0.0));
if (movement_step_up_only.y < 0.6)
const Vector3<double> movement_step_up_only = CollideBoundingBox(AABB(player_aabb.GetCenter() + horizontal_movement * 0.5, player_aabb.GetHalfSize() + horizontal_movement.Abs() * 0.5), Vector3<double>(0.0, max_up_step, 0.0));
if (movement_step_up_only.y < max_up_step)
{
const Vector3<double> check = CollideBoundingBox(player_aabb + movement_step_up_only, horizontal_movement) + movement_step_up_only;
if (check.x * check.x + check.z * check.z > movement_with_step_up.x * movement_with_step_up.x + movement_with_step_up.z * movement_with_step_up.z)
Expand Down
3 changes: 2 additions & 1 deletion botcraft/src/Game/World/Blockstate.cpp
Expand Up @@ -839,7 +839,7 @@ namespace Botcraft

float Blockstate::GetMiningTimeSeconds(const ToolType tool_type, const ToolMaterial tool_material,
const unsigned char tool_efficiency, const unsigned char haste, const unsigned char fatigue,
const bool on_ground, const bool head_in_fluid_wo_aqua_affinity) const
const bool on_ground, const bool head_in_fluid_wo_aqua_affinity, const float speed_factor) const
{
if (hardness < 0.0f || IsWater() || IsLava())
{
Expand Down Expand Up @@ -894,6 +894,7 @@ namespace Botcraft
speed_multiplier += (tool_efficiency > 0) * 1.0f + tool_efficiency * tool_efficiency;
speed_multiplier *= 1.0f + 0.2f * haste;
speed_multiplier *= static_cast<float>(std::pow(0.3f, std::min(static_cast<int>(fatigue), 4)));
speed_multiplier *= speed_factor;
speed_multiplier *= head_in_fluid_wo_aqua_affinity ? 0.2f : 1.0f;
speed_multiplier *= on_ground ? 1.0f : 0.2f;

Expand Down
25 changes: 25 additions & 0 deletions botcraft/src/Game/World/World.cpp
Expand Up @@ -918,13 +918,38 @@ namespace Botcraft
void World::Handle(ProtocolCraft::ClientboundRegistryDataPacket& msg)
{
std::scoped_lock<std::shared_mutex> lock(world_mutex);
#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
for (const auto& d : msg.GetRegistryHolder()["minecraft:dimension_type"]["value"].as_list_of<ProtocolCraft::NBT::TagCompound>())
{
const std::string& dim_name = d["name"].get<std::string>();
dimension_height[dim_name] = static_cast<unsigned int>(d["element"]["height"].get<int>());
dimension_min_y[dim_name] = d["element"]["min_y"].get<int>();
dimension_ultrawarm[dim_name] = static_cast<bool>(d["element"]["ultrawarm"].get<char>());
}
#else
if (msg.GetRegistry().GetFull() != "minecraft:dimension_type")
{
return;
}

const auto& entries = msg.GetEntries();
for (size_t i = 0; i < entries.size(); ++i)
{
const std::string dim_name = entries[i].GetId_().GetFull();
// Make sure we use the same indices as minecraft registry
// Not really useful but just in case
dimension_index_map.insert({ dim_name, i });
index_dimension_map.insert({ i, dim_name });

if (entries[i].GetData().has_value())
{
const ProtocolCraft::NBT::Value& data = entries[i].GetData().value();
dimension_height[dim_name] = static_cast<unsigned int>(data["height"].get<int>());
dimension_min_y[dim_name] = data["min_y"].get<int>();
dimension_ultrawarm[dim_name] = static_cast<bool>(data["ultrawarm"].get<char>());
}
}
#endif
}
#endif

Expand Down
25 changes: 25 additions & 0 deletions botcraft/src/Network/NetworkManager.cpp
Expand Up @@ -238,8 +238,21 @@ namespace Botcraft

void NetworkManager::SendChatCommand(const std::string& command)
{
#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
if (authentifier == nullptr)
{
std::shared_ptr<ServerboundChatCommandPacket> chat_command = std::make_shared<ServerboundChatCommandPacket>();
chat_command->SetCommand(command);
Send(chat_command);
return;
}
#endif
#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
std::shared_ptr<ServerboundChatCommandSignedPacket> chat_command = std::make_shared<ServerboundChatCommandSignedPacket>();
#else
std::shared_ptr<ServerboundChatCommandPacket> chat_command = std::make_shared<ServerboundChatCommandPacket>();
#endif
chat_command->SetCommand(command);
chat_command->SetTimestamp(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
#if PROTOCOL_VERSION > 759 /* > 1.19 */
Expand Down Expand Up @@ -601,4 +614,16 @@ namespace Botcraft
Send(chunk_per_tick_msg);
}
#endif

#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
void NetworkManager::Handle(ProtocolCraft::ClientboundSelectKnownPacksPacket& msg)
{
std::shared_ptr<ServerboundSelectKnownPacksPacket> select_known_packs = std::make_shared<ServerboundSelectKnownPacksPacket>();
// Datapacks are not supported by Botcraft
select_known_packs->SetKnownPacks({});

Send(select_known_packs);

}
#endif
}

0 comments on commit cece1c2

Please sign in to comment.