Botcraft 26.1.2
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->GetAttributeEntityInteractionRangeValue();
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> packet_interact = std::make_shared<ServerboundInteractPacket>();
55 packet_interact->SetEntityId(entity_id);
56#if PROTOCOL_VERSION < 775 /* < 26.1 */
57 packet_interact->SetAction(0);
58#endif
59 packet_interact->SetHand(static_cast<int>(hand));
60#if PROTOCOL_VERSION > 722 /* > 1.15.2 */
61 packet_interact->SetUsingSecondaryAction(false);
62#endif
63 network_manager->Send(packet_interact);
64
65 if (swing)
66 {
67 std::shared_ptr<ServerboundSwingPacket> packet_swing = std::make_shared<ServerboundSwingPacket>();
68 packet_swing->SetHand(static_cast<int>(hand));
69
70 network_manager->Send(packet_swing);
71 }
72
73 return Status::Success;
74 }
75
76 Status InteractEntity(BehaviourClient& client, const int entity_id, const Hand hand, const bool swing)
77 {
78 constexpr std::array variable_names = {
79 "InteractEntity.entity_id",
80 "InteractEntity.hand",
81 "InteractEntity.swing"
82 };
83
84 Blackboard& blackboard = client.GetBlackboard();
85
86 blackboard.Set<int>(variable_names[0], entity_id);
87 blackboard.Set<Hand>(variable_names[1], hand);
88 blackboard.Set<bool>(variable_names[2], swing);
89
90 return InteractEntityImpl(client, entity_id, hand, swing);
91 }
92
94 {
95 constexpr std::array variable_names = {
96 "InteractEntity.entity_id",
97 "InteractEntity.hand",
98 "InteractEntity.swing"
99 };
100
101 Blackboard& blackboard = client.GetBlackboard();
102
103 // Mandatory
104 const int entity_id = blackboard.Get<int>(variable_names[0]);
105
106 // Optional
107 const Hand hand = blackboard.Get<Hand>(variable_names[1], Hand::Main);
108 const bool swing = blackboard.Get<bool>(variable_names[2], false);
109
110 return InteractEntityImpl(client, entity_id, hand, swing);
111 }
112}
#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:204