Botcraft 1.21.4
Loading...
Searching...
No Matches
BaseTasks.cpp
Go to the documentation of this file.
8
9namespace Botcraft
10{
12 {
13 client.Yield();
14
15 return Status::Success;
16 }
17
18
20 {
21 client.SetShouldBeClosed(true);
22 return Status::Success;
23 }
24
25
26 Status SayImpl(BehaviourClient& client, const std::string& msg)
27 {
28 client.SendChatMessage(msg);
29 return Status::Success;
30 }
31
32 Status Say(BehaviourClient& client, const std::string& msg)
33 {
34 constexpr std::array variable_names = {
35 "Say.msg"
36 };
37
38 Blackboard& blackboard = client.GetBlackboard();
39
40 blackboard.Set<std::string>(variable_names[0], msg);
41
42 return SayImpl(client, msg);
43 }
44
46 {
47 constexpr std::array variable_names = {
48 "Say.msg"
49 };
50
51 Blackboard& blackboard = client.GetBlackboard();
52
53 // Mandatory
54 const std::string& msg = blackboard.Get<std::string>(variable_names[0]);
55
56 return SayImpl(client, msg);
57 }
58
59
60 Status InteractWithBlockImpl(BehaviourClient& client, const Position& pos, const PlayerDiggingFace face, const bool animation)
61 {
62 std::shared_ptr<LocalPlayer> local_player = client.GetLocalPlayer();
63
64 if (local_player == nullptr)
65 {
66 return Status::Failure;
67 }
68
69 // Compute the distance from the hand? Might be from somewhere else
70 const Vector3<double> player_hand_pos = local_player->GetPosition() + Vector3<double>(0.0, 1.0, 0.0);
71
72#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
73 constexpr double range = 4.5;
74#else
75 const double range = local_player->GetAttributePlayerBlockInteractionRangeValue();
76#endif
77
78 if (player_hand_pos.SqrDist(Vector3<double>(0.5, 0.5, 0.5) + pos) > range * range)
79 {
80 // Go in range
81 if (GoTo(client, pos, static_cast<int>(range)) == Status::Failure)
82 {
83 return Status::Failure;
84 }
85 }
86
87 std::shared_ptr<ProtocolCraft::ServerboundUseItemOnPacket> place_block_msg = std::make_shared<ProtocolCraft::ServerboundUseItemOnPacket>();
88 place_block_msg->SetLocation(pos.ToNetworkPosition());
89 place_block_msg->SetDirection(static_cast<int>(face));
90 switch (face)
91 {
93 place_block_msg->SetCursorPositionX(0.5f);
94 place_block_msg->SetCursorPositionY(0.0f);
95 place_block_msg->SetCursorPositionZ(0.5f);
96 break;
98 place_block_msg->SetCursorPositionX(0.5f);
99 place_block_msg->SetCursorPositionY(1.0f);
100 place_block_msg->SetCursorPositionZ(0.5f);
101 break;
103 place_block_msg->SetCursorPositionX(0.5f);
104 place_block_msg->SetCursorPositionY(0.5f);
105 place_block_msg->SetCursorPositionZ(0.0f);
106 break;
108 place_block_msg->SetCursorPositionX(0.5f);
109 place_block_msg->SetCursorPositionY(0.5f);
110 place_block_msg->SetCursorPositionZ(1.0f);
111 break;
113 place_block_msg->SetCursorPositionX(1.0f);
114 place_block_msg->SetCursorPositionY(0.5f);
115 place_block_msg->SetCursorPositionZ(0.5f);
116 break;
118 place_block_msg->SetCursorPositionX(0.0f);
119 place_block_msg->SetCursorPositionY(0.5f);
120 place_block_msg->SetCursorPositionZ(0.5f);
121 break;
122 default:
123 break;
124 }
125#if PROTOCOL_VERSION > 452 /* > 1.13.2 */
126 place_block_msg->SetInside(false);
127#endif
128 place_block_msg->SetHand(static_cast<int>(Hand::Right));
129#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
130 place_block_msg->SetSequence(client.GetWorld()->GetNextWorldInteractionSequenceId());
131#endif
132 client.GetNetworkManager()->Send(place_block_msg);
133
134 if (animation)
135 {
136 std::shared_ptr<ProtocolCraft::ServerboundSwingPacket> animation_msg = std::make_shared<ProtocolCraft::ServerboundSwingPacket>();
137 animation_msg->SetHand(static_cast<int>(Hand::Right));
138 client.GetNetworkManager()->Send(animation_msg);
139 }
140
141 LookAt(client, Vector3<double>(0.5) + pos, true);
142
143 return Status::Success;
144 }
145
146 Status InteractWithBlock(BehaviourClient& client, const Position& pos, const PlayerDiggingFace face, const bool animation)
147 {
148 constexpr std::array variable_names = {
149 "InteractWithBlock.pos",
150 "InteractWithBlock.face",
151 "InteractWithBlock.animation"
152 };
153
154 Blackboard& blackboard = client.GetBlackboard();
155
156 blackboard.Set<Position>(variable_names[0], pos);
157 blackboard.Set<PlayerDiggingFace>(variable_names[1], face);
158 blackboard.Set<bool>(variable_names[2], animation);
159
160 return InteractWithBlockImpl(client, pos, face, animation);
161 }
162
164 {
165 constexpr std::array variable_names = {
166 "InteractWithBlock.pos",
167 "InteractWithBlock.face",
168 "InteractWithBlock.animation"
169 };
170
171 Blackboard& blackboard = client.GetBlackboard();
172
173 // Mandatory
174 const Position& pos = blackboard.Get<Position>(variable_names[0]);
175
176 // Optional
177 const PlayerDiggingFace face = blackboard.Get<PlayerDiggingFace>(variable_names[1], PlayerDiggingFace::Up);
178 const bool animation = blackboard.Get(variable_names[2], true);
179
180 return InteractWithBlockImpl(client, pos, face, animation);
181 }
182
183
184 Status CheckBlackboardBoolDataImpl(BehaviourClient& client, const std::string& key)
185 {
186 return client.GetBlackboard().Get(key, false) ? Status::Success : Status::Failure;
187 }
188
189 Status CheckBlackboardBoolData(BehaviourClient& client, const std::string& key)
190 {
191 constexpr std::array variable_names = {
192 "CheckBlackboardBoolData.key"
193 };
194
195 Blackboard& blackboard = client.GetBlackboard();
196
197 blackboard.Set<std::string>(variable_names[0], key);
198
199 return CheckBlackboardBoolDataImpl(client, key);
200 }
201
203 {
204 constexpr std::array variable_names = {
205 "CheckBlackboardBoolData.key"
206 };
207
208 Blackboard& blackboard = client.GetBlackboard();
209
210 // Mandatory
211 const std::string& key = blackboard.Get<std::string>(variable_names[0]);
212
213 return CheckBlackboardBoolDataImpl(client, key);
214 }
215
216
217 Status RemoveBlackboardDataImpl(BehaviourClient& client, const std::string& key)
218 {
219 client.GetBlackboard().Erase(key);
220
221 return Status::Success;
222 }
223
224 Status RemoveBlackboardData(BehaviourClient& client, const std::string& key)
225 {
226 constexpr std::array variable_names = {
227 "RemoveBlackboardData.key"
228 };
229
230 Blackboard& blackboard = client.GetBlackboard();
231
232 blackboard.Set<std::string>(variable_names[0], key);
233
234 return RemoveBlackboardDataImpl(client, key);
235 }
236
238 {
239 constexpr std::array variable_names = {
240 "RemoveBlackboardData.key"
241 };
242
243 Blackboard& blackboard = client.GetBlackboard();
244
245 // Mandatory
246 const std::string& key = blackboard.Get<std::string>(variable_names[0]);
247
248 return RemoveBlackboardDataImpl(client, key);
249 }
250
251
252 Status IsHungryImpl(BehaviourClient& client, const int threshold)
253 {
254 return (client.GetLocalPlayer() == nullptr || client.GetLocalPlayer()->GetFood() >= threshold) ? Status::Failure : Status::Success;
255 }
256
257 Status IsHungry(BehaviourClient& client, const int threshold)
258 {
259 constexpr std::array variable_names = {
260 "IsHungry.threshold"
261 };
262
263 Blackboard& blackboard = client.GetBlackboard();
264
265 blackboard.Set<int>(variable_names[0], threshold);
266
267 return IsHungryImpl(client, threshold);
268 }
269
271 {
272 constexpr std::array variable_names = {
273 "IsHungry.threshold"
274 };
275
276 Blackboard& blackboard = client.GetBlackboard();
277
278 // Mandatory
279 const int threshold = blackboard.Get<int>(variable_names[0]);
280
281 return IsHungryImpl(client, threshold);
282 }
283
284
285 Status CopyBlackboardDataImpl(BehaviourClient& client, const std::string& src, const std::string& dst)
286 {
287 client.GetBlackboard().Copy(src, dst);
288
289 return Status::Success;
290 }
291
292 Status CopyBlackboardData(BehaviourClient& client, const std::string& src, const std::string& dst)
293 {
294 constexpr std::array variable_names = {
295 "CopyBlackboardData.src",
296 "CopyBlackboardData.dst"
297 };
298
299 Blackboard& blackboard = client.GetBlackboard();
300
301 blackboard.Set<std::string>(variable_names[0], src);
302 blackboard.Set<std::string>(variable_names[1], dst);
303
304 return CopyBlackboardDataImpl(client, src, dst);
305 }
306
308 {
309 constexpr std::array variable_names = {
310 "CopyBlackboardData.src",
311 "CopyBlackboardData.dst"
312 };
313
314 Blackboard& blackboard = client.GetBlackboard();
315
316 // Mandatory
317 const std::string& src = blackboard.Get<std::string>(variable_names[0]);
318 const std::string& dst = blackboard.Get<std::string>(variable_names[1]);
319
320 return CopyBlackboardDataImpl(client, src, dst);
321 }
322
323
325 {
326 const int day_time = client.GetDayTime();
327 return (day_time > 12541 && day_time < 23460) ? Status::Success : Status::Failure;
328 }
329
330
332 {
333 return (client.GetLocalPlayer() == nullptr || client.GetLocalPlayer()->GetHealth() <= 0.0f) ? Status::Failure : Status::Success;
334 }
335}
A ManagersClient extended with a blackboard that can store any kind of data and a virtual Yield funct...
virtual void Yield()=0
A map wrapper to store arbitrary data.
void Erase(const std::string &key)
Remove a map entry if present.
void Set(const std::string &key, const T &value)
Set map entry at key to value.
void Copy(const std::string &src, const std::string &dst)
Copy a blackboard value.
const T & Get(const std::string &key)
Get the map value at key, casting it to T.
std::shared_ptr< NetworkManager > GetNetworkManager() const
void SendChatMessage(const std::string &msg)
Send a message in the game chat.
void SetShouldBeClosed(const bool b)
std::shared_ptr< LocalPlayer > GetLocalPlayer() const
int GetDayTime() const
Get the current tick.
std::shared_ptr< World > GetWorld() const
Status IsHungryBlackboard(BehaviourClient &client)
Same thing as IsHungry, but reads its parameters from the blackboard.
Status RemoveBlackboardDataBlackboard(BehaviourClient &client)
Same thing as RemoveBlackboardData, but reads its parameters from the blackboard.
Status SayBlackboard(BehaviourClient &client)
Same thing as Say, but reads its parameters from the blackboard.
Definition BaseTasks.cpp:45
Status RemoveBlackboardDataImpl(BehaviourClient &client, const std::string &key)
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 InteractWithBlockImpl(BehaviourClient &client, const Position &pos, const PlayerDiggingFace face, const bool animation)
Definition BaseTasks.cpp:60
Status CopyBlackboardData(BehaviourClient &client, const std::string &src, const std::string &dst)
Copy a blackboard data.
Status SayImpl(BehaviourClient &client, const std::string &msg)
Definition BaseTasks.cpp:26
Status IsHungry(BehaviourClient &client, const int threshold=20)
Return success if player food is below a threshold.
Status Disconnect(BehaviourClient &client)
Ask this client to disconnect from the server by setting should_be_closed to true.
Definition BaseTasks.cpp:19
Status CopyBlackboardDataBlackboard(BehaviourClient &client)
Same thing as CopyBlackboardData, but reads its parameters from the blackboard.
Status Yield(BehaviourClient &client)
Just call client.Yield().
Definition BaseTasks.cpp:11
Status Say(BehaviourClient &client, const std::string &msg)
Send a message in the game chat.
Definition BaseTasks.cpp:32
Status CheckBlackboardBoolDataBlackboard(BehaviourClient &client)
Same thing as CheckBlackboardBoolData, but reads its parameters from the blackboard.
Status LookAt(BehaviourClient &client, const Vector3< double > &target, const bool set_pitch=true, const bool sync_to_server=true)
Turn the camera to look at a given target and send the new rotation to the server.
Status InteractWithBlock(BehaviourClient &client, const Position &pos, const PlayerDiggingFace face=PlayerDiggingFace::Up, const bool animation=true)
Interact (right click) with the block at the given location.
Status IsHungryImpl(BehaviourClient &client, const int threshold)
Status IsNightTime(BehaviourClient &client)
Return Success if it's night time (sleep is possible)
Status InteractWithBlockBlackboard(BehaviourClient &client)
Same thing as InteractWithBlock, but reads its parameters from the blackboard.
Status CheckBlackboardBoolDataImpl(BehaviourClient &client, const std::string &key)
Status CopyBlackboardDataImpl(BehaviourClient &client, const std::string &src, const std::string &dst)
Status CheckBlackboardBoolData(BehaviourClient &client, const std::string &key)
Check in the blackboard if the bool at key is true.
Status RemoveBlackboardData(BehaviourClient &client, const std::string &key)
Remove a value from the blackboard if exist.
Status IsAlive(BehaviourClient &client)
Return success if player is alive.
double SqrDist(const Vector3 &v) const
Definition Vector3.hpp:192
ProtocolCraft::NetworkPosition ToNetworkPosition() const
Definition Vector3.hpp:272