Botcraft 1.21.4
Loading...
Searching...
No Matches
EntitiesTasks.cpp
Go to the documentation of this file.
5
10
12
13using namespace ProtocolCraft;
14
15namespace Botcraft
16{
17 Status InteractEntityImpl(BehaviourClient& client, const int entity_id, const Hand hand, const bool swing)
18 {
19 std::shared_ptr<EntityManager> entity_manager = client.GetEntityManager();
20
21 std::shared_ptr<Entity> entity = entity_manager->GetEntity(entity_id);
22
23 if (!entity)
24 {
25 LOG_WARNING("Can't interact with unexisting entity: " << entity_id);
26 return Status::Failure;
27 }
28
29 std::shared_ptr<LocalPlayer> local_player = entity_manager->GetLocalPlayer();
30
31 Vector3<double> entity_position = entity->GetPosition();
32 Vector3<double> position = local_player->GetPosition();
33
34#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
35 constexpr double range = 3.0;
36#else
37 const double range = local_player->GetAttributePlayerEntityInteractionRangeValue();
38#endif
39
40 while (position.SqrDist(entity_position) > range * range)
41 {
42 if (GoTo(client, entity_position, static_cast<int>(range)) == Status::Failure)
43 {
44 return Status::Failure;
45 }
46
47 entity_position = entity->GetPosition();
48 position = local_player->GetPosition();
49 }
50
51 local_player->LookAt(entity_position + Vector3<double>(0, entity->GetHeight() / 2.0, 0), true);
52
53 std::shared_ptr<NetworkManager> network_manager = client.GetNetworkManager();
54 std::shared_ptr<ServerboundInteractPacket> msg_interact = std::make_shared<ServerboundInteractPacket>();
55 msg_interact->SetEntityId(entity_id);
56 msg_interact->SetAction(0);
57 msg_interact->SetHand(static_cast<int>(hand));
58#if PROTOCOL_VERSION > 722 /* > 1.15.2 */
59 msg_interact->SetUsingSecondaryAction(false);
60#endif
61 network_manager->Send(msg_interact);
62
63 if (swing)
64 {
65 std::shared_ptr<ServerboundSwingPacket> msg_swing = std::make_shared<ServerboundSwingPacket>();
66 msg_swing->SetHand(static_cast<int>(hand));
67
68 network_manager->Send(msg_swing);
69 }
70
71 return Status::Success;
72 }
73
74 Status InteractEntity(BehaviourClient& client, const int entity_id, const Hand hand, const bool swing)
75 {
76 constexpr std::array variable_names = {
77 "InteractEntity.entity_id",
78 "InteractEntity.hand",
79 "InteractEntity.swing"
80 };
81
82 Blackboard& blackboard = client.GetBlackboard();
83
84 blackboard.Set<int>(variable_names[0], entity_id);
85 blackboard.Set<Hand>(variable_names[1], hand);
86 blackboard.Set<bool>(variable_names[2], swing);
87
88 return InteractEntityImpl(client, entity_id, hand, swing);
89 }
90
92 {
93 constexpr std::array variable_names = {
94 "InteractEntity.entity_id",
95 "InteractEntity.hand",
96 "InteractEntity.swing"
97 };
98
99 Blackboard& blackboard = client.GetBlackboard();
100
101 // Mandatory
102 const int entity_id = blackboard.Get<int>(variable_names[0]);
103
104 // Optional
105 const Hand hand = blackboard.Get<Hand>(variable_names[1], Hand::Main);
106 const bool swing = blackboard.Get<bool>(variable_names[2], false);
107
108 return InteractEntityImpl(client, entity_id, hand, swing);
109 }
110}
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
A ManagersClient extended with a blackboard that can store any kind of data and a virtual Yield funct...
A map wrapper to store arbitrary data.
void Set(const std::string &key, const T &value)
Set map entry at key to value.
const T & Get(const std::string &key)
Get the map value at key, casting it to T.
std::shared_ptr< NetworkManager > GetNetworkManager() const
std::shared_ptr< EntityManager > GetEntityManager() const
Status InteractEntity(BehaviourClient &client, const int entity_id, const Hand hand=Hand::Main, const bool swing=false)
Interact with a given entity.
Status GoTo(BehaviourClient &client, const Position &goal, const int dist_tolerance=0, const int min_end_dist=0, const int min_end_dist_xz=0, const bool allow_jump=true, const bool sprint=true, const float speed_factor=1.0f)
Find a path to a block position and navigate to it.
Status InteractEntityImpl(BehaviourClient &client, const int entity_id, const Hand hand, const bool swing)
Status InteractEntityBlackboard(BehaviourClient &client)
Same thing as InteractEntity, but reads its parameters from the blackboard.
double SqrDist(const Vector3 &v) const
Definition Vector3.hpp:192