Botcraft 26.3
Loading...
Searching...
No Matches
PathfindingTask.cpp
Go to the documentation of this file.
12
13namespace Botcraft
14{
16 {
17 public:
19 PathfindingBlockstate(const Blockstate* block, const Position& pos, const bool take_damage) : block(block)
20 {
21 height = pos.y;
22 if (block == nullptr)
23 {
24 empty = true;
25 return;
26 }
27 empty = false;
28
29 if (take_damage && block->IsHazardous())
30 {
31 hazardous = true;
32 return;
33 }
34
36 {
37 climbable = true;
38 fluid = true;
39 return;
40 }
41
42 if (block->IsClimbable())
43 {
44 climbable = true;
45 return;
46 }
47
48 if (block->IsSolid())
49 {
50 solid = true;
51 const auto& colliders = block->GetCollidersAtPos(pos);
52 for (const auto& c : colliders)
53 {
54 height = std::max(static_cast<float>(c.GetMax().y), height);
55 }
56 }
57 else
58 {
59 empty = true;
60 }
61 }
62
63 const Blockstate* GetBlockstate() const { return block; }
64 bool IsEmpty() const { return empty; }
65 bool IsSolid() const { return solid; }
66 bool IsHazardous() const { return hazardous; }
67 bool IsClimbable() const { return climbable; }
68 bool IsFluid() const { return fluid; }
69 float GetHeight() const { return height; }
70
71 private:
72 const Blockstate* block = nullptr;
73 bool empty = true;
74 bool solid = false;
75 bool hazardous = false;
76 bool climbable = false;
77 bool fluid = false;
78 float height = 0.0f;
79 };
80
82 {
83 public:
84 size_t operator()(const std::pair<Position, float>& p) const
85 {
86 size_t value = std::hash<Position>()(p.first);
87 value ^= std::hash<float>()(p.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
88 return value;
89 }
90 };
91
92 std::vector<std::pair<Position, float>> FindPath(
93 const std::shared_ptr<World>& world,
94 const Position& start, const Position& end,
95 const int dist_tolerance, const int min_end_dist, const int min_end_dist_xz,
96 const bool allow_jump, const bool take_damage, const float step_height
97 )
98 {
99 struct PathNode
100 {
101 std::pair<Position, float> pos; // <Block in which the feet are, feet height>
102 float score; // distance from start + heuristic to goal
103
104 PathNode(const std::pair<Position, float>& p, const float s)
105 {
106 pos = p;
107 score = s;
108 }
109
110 bool operator>(const PathNode& rhs) const
111 {
112 return score > rhs.score;
113 }
114
115 static float Heuristic(const Position& a, const Position& b)
116 {
117 return static_cast<float>(std::abs(a.x - b.x) + std::abs(a.y - b.y) + std::abs(a.z - b.z));
118 }
119 };
120
121 constexpr int budget_visit = 15000;
122
123 const std::array<Position, 4> neighbour_offsets = { Position(1, 0, 0), Position(-1, 0, 0), Position(0, 0, 1), Position(0, 0, -1) };
124 std::priority_queue<PathNode, std::vector<PathNode>, std::greater<PathNode> > nodes_to_explore;
125 std::unordered_map<std::pair<Position, float>, std::pair<Position, float>, PosFloatPairHash> came_from;
126 std::unordered_map<std::pair<Position, float>, float, PosFloatPairHash> cost;
127
128 auto add_node_if_better = [&](const std::pair<Position, float>& new_pos, float new_cost, const PathNode& from) {
129 auto it = cost.find(new_pos);
130 if (it == cost.end() || new_cost < it->second)
131 {
132 cost[new_pos] = new_cost;
133 nodes_to_explore.emplace(PathNode(new_pos, new_cost + PathNode::Heuristic(new_pos.first, end)));
134 came_from[new_pos] = from.pos;
135 }
136 };
137
138 const Blockstate* block = world->GetBlock(start);
139 nodes_to_explore.emplace(PathNode({ start, PathfindingBlockstate(block, start, take_damage).GetHeight() }, 0.0f));
140 came_from[nodes_to_explore.top().pos] = nodes_to_explore.top().pos;
141 cost[nodes_to_explore.top().pos] = 0.0f;
142
143 int count_visit = 0;
144 // We found one location matching all the criterion, but
145 // continue the search to see if we can find a better one
146 bool suitable_location_found = false;
147 // We found a path to the desired goal
148 bool end_reached = false;
149
150 bool end_is_inside_solid = false;
151 block = world->GetBlock(end);
152 end_is_inside_solid = block != nullptr && block->IsSolid();
153
154 while (!nodes_to_explore.empty())
155 {
156 count_visit++;
157 PathNode current_node = nodes_to_explore.top();
158 nodes_to_explore.pop();
159
160 // If this node is already present with a better score
161 {
162 const float h = PathNode::Heuristic(current_node.pos.first, end);
163 if (cost[current_node.pos] + h < current_node.score)
164 {
165 continue;
166 }
167 }
168
169 end_reached |= current_node.pos.first == end;
170 suitable_location_found |=
171 std::abs(end.x - current_node.pos.first.x) + std::abs(end.y - current_node.pos.first.y) + std::abs(end.z - current_node.pos.first.z) <= dist_tolerance &&
172 std::abs(end.x - current_node.pos.first.x) + std::abs(end.y - current_node.pos.first.y) + std::abs(end.z - current_node.pos.first.z) >= min_end_dist &&
173 std::abs(end.x - current_node.pos.first.x) + std::abs(end.z - current_node.pos.first.z) >= min_end_dist_xz;
174
175 if (// If we exceeded the search budget
176 count_visit > budget_visit ||
177 // Or if we found a suitable location in the process and already reached the goal/can't reach it anyway
178 (suitable_location_found && (end_reached || end_is_inside_solid)))
179 {
180 break;
181 }
182
183 // Get the state around the player in the given location
184 std::array<PathfindingBlockstate, 6> vertical_surroundings;
185
186 // Assuming the player is standing on 3 (feeet on 2 and head on 1)
187 // 0
188 // 1
189 // 2
190 // 3
191 // 4
192 // 5
193 Position pos = current_node.pos.first + Position(0, 2, 0);
194 block = world->GetBlock(pos);
195 vertical_surroundings[0] = PathfindingBlockstate(block, pos, take_damage);
196 pos = current_node.pos.first + Position(0, 1, 0);
197 block = world->GetBlock(pos);
198 vertical_surroundings[1] = PathfindingBlockstate(block, pos, take_damage);
199 // Current feet block
200 pos = current_node.pos.first;
201 block = world->GetBlock(pos);
202 vertical_surroundings[2] = PathfindingBlockstate(block, pos, take_damage);
203 const bool can_jump =
204 vertical_surroundings[2].GetBlockstate() != nullptr &&
205 vertical_surroundings[2].GetBlockstate()->CanJumpWhenFeetInside();
206
207 // if 2 is solid or hazardous, no down pathfinding is possible,
208 // so we can skip a few checks
209 if (!vertical_surroundings[2].IsSolid() && !vertical_surroundings[2].IsHazardous())
210 {
211 // if 3 is solid or hazardous, no down pathfinding is possible,
212 // so we can skip a few checks
213 pos = current_node.pos.first + Position(0, -1, 0);
214 block = world->GetBlock(pos);
215 vertical_surroundings[3] = PathfindingBlockstate(block, pos, take_damage);
216
217 // If we can move down, we need 4 and 5
218 if (!vertical_surroundings[3].IsSolid() && !vertical_surroundings[3].IsHazardous())
219 {
220 pos = current_node.pos.first + Position(0, -2, 0);
221 block = world->GetBlock(pos);
222 vertical_surroundings[4] = PathfindingBlockstate(block, pos, take_damage);
223 pos = current_node.pos.first + Position(0, -3, 0);
224 block = world->GetBlock(pos);
225 vertical_surroundings[5] = PathfindingBlockstate(block, pos, take_damage);
226 }
227 }
228
229
230 // Check all vertical cases that would allow the bot to pass
231 // -
232 // x
233 // ^
234 // ?
235 // ?
236 // ?
237 if (vertical_surroundings[2].IsClimbable()
238 && !vertical_surroundings[1].IsSolid()
239 && !vertical_surroundings[1].IsHazardous()
240 && !vertical_surroundings[0].IsSolid()
241 && !vertical_surroundings[0].IsHazardous()
242 )
243 {
244 const float new_cost = cost[current_node.pos] + 1.0f;
245 const std::pair<Position, float> new_pos = {
246 current_node.pos.first + Position(0, 1, 0),
247 current_node.pos.first.y + 1.0f
248 };
249 add_node_if_better(new_pos, new_cost, current_node);
250 }
251
252 // -
253 // ^
254 // x
255 // o
256 // ?
257 // ?
258 if (can_jump
259 && vertical_surroundings[1].IsClimbable()
260 && !vertical_surroundings[0].IsSolid()
261 && !vertical_surroundings[0].IsHazardous()
262 && (vertical_surroundings[2].IsSolid() || // we stand on top of 2
263 vertical_surroundings[3].IsSolid()) // if not, it means we stand on 3. Height difference check is not necessary, as the feet are in 2, we know 3 is at least 1 tall
264 )
265 {
266 const float new_cost = cost[current_node.pos] + 1.5f;
267 const std::pair<Position, float> new_pos = {
268 current_node.pos.first + Position(0, 1, 0),
269 current_node.pos.first.y + 1.0f
270 };
271 add_node_if_better(new_pos, new_cost, current_node);
272 }
273
274 // ?
275 // x
276 //
277 // -
278 // ?
279 // ?
280 if (!vertical_surroundings[2].IsSolid() &&
281 vertical_surroundings[3].IsClimbable()
282 )
283 {
284 const float new_cost = cost[current_node.pos] + 1.0f;
285 const std::pair<Position, float> new_pos = {
286 current_node.pos.first + Position(0, -1, 0),
287 current_node.pos.first.y - 1.0f
288 };
289 add_node_if_better(new_pos, new_cost, current_node);
290 }
291
292 // ?
293 // x
294 //
295 // -
296 //
297 // o
298 if (!vertical_surroundings[2].IsSolid() &&
299 vertical_surroundings[3].IsClimbable()
300 && vertical_surroundings[4].IsEmpty()
301 && !vertical_surroundings[5].IsEmpty()
302 && !vertical_surroundings[5].IsHazardous()
303 )
304 {
305 const bool above_block = vertical_surroundings[5].IsClimbable() || vertical_surroundings[5].GetHeight() + 1e-3f > current_node.pos.first.y - 2;
306 const float new_cost = cost[current_node.pos] + 3.0f - 1.0f * above_block;
307 const std::pair<Position, float> new_pos = {
308 current_node.pos.first + Position(0, -3 + 1 * above_block, 0),
309 above_block ? std::max(current_node.pos.first.y - 2.0f, vertical_surroundings[5].GetHeight()) : vertical_surroundings[5].GetHeight()
310 };
311 add_node_if_better(new_pos, new_cost, current_node);
312 }
313
314
315
316 // ?
317 // x
318 // ^
319 //
320 //
321 // o
322 if (vertical_surroundings[2].IsClimbable()
323 && vertical_surroundings[3].IsEmpty()
324 && vertical_surroundings[4].IsEmpty()
325 && !vertical_surroundings[5].IsEmpty()
326 && !vertical_surroundings[5].IsHazardous()
327 )
328 {
329 const bool above_block = vertical_surroundings[5].IsClimbable() || vertical_surroundings[5].GetHeight() + 1e-3f > current_node.pos.first.y - 2;
330 const float new_cost = cost[current_node.pos] + 3.0f - 1.0f * above_block;
331 const std::pair<Position, float> new_pos = {
332 current_node.pos.first + Position(0, -3 + 1 * above_block, 0),
333 above_block ? std::max(current_node.pos.first.y - 2.0f, vertical_surroundings[5].GetHeight()) : vertical_surroundings[5].GetHeight()
334 };
335 add_node_if_better(new_pos, new_cost, current_node);
336 }
337
338
339 // ?
340 // x
341 //
342 // -
343 //
344 //
345 // Special case here, we can drop down
346 // if there is a climbable at the bottom
347 if (!vertical_surroundings[2].IsSolid() &&
348 vertical_surroundings[3].IsClimbable()
349 && vertical_surroundings[4].IsEmpty()
350 && vertical_surroundings[5].IsEmpty()
351 )
352 {
353 for (int y = -4; current_node.pos.first.y + y >= world->GetMinY(); --y)
354 {
355 pos = current_node.pos.first + Position(0, y, 0);
356 block = world->GetBlock(pos);
357
358 if (block != nullptr && ((block->IsSolid() && !block->IsClimbable()) || (take_damage && block->IsHazardous())))
359 {
360 break;
361 }
362
363 const PathfindingBlockstate landing_block(block, pos, take_damage);
364 if (landing_block.IsClimbable())
365 {
366 const float new_cost = cost[current_node.pos] + std::abs(y);
367 const std::pair<Position, float> new_pos = {
368 current_node.pos.first + Position(0, y + 1, 0),
369 current_node.pos.first.y + y + 1.0f
370 };
371 add_node_if_better(new_pos, new_cost, current_node);
372
373 break;
374 }
375 }
376 }
377
378
379 // For each neighbour, check if it's reachable
380 // and add it to the search list if it is
381 for (int i = 0; i < neighbour_offsets.size(); ++i)
382 {
383 const Position next_location = current_node.pos.first + neighbour_offsets[i];
384 const Position next_next_location = next_location + neighbour_offsets[i];
385
386 // Get the state around the player in the given direction
387 std::array<PathfindingBlockstate, 12> horizontal_surroundings;
388
389 // Assuming the player is standing on v3 (feeet on v2 and head on v1)
390 // v0 0 6 --> ? ? ?
391 // v1 1 7 --> x ? ?
392 // v2 2 8 --> x ? ?
393 // v3 3 9 --> ? ? ?
394 // v4 4 10 --> ? ? ?
395 // v5 5 11 --> ? ? ?
396
397 // if 1 is solid and tall, no horizontal pathfinding is possible,
398 // so we can skip a lot of checks
399 pos = next_location + Position(0, 2, 0);
400 block = world->GetBlock(pos);
401 horizontal_surroundings[0] = PathfindingBlockstate(block, pos, take_damage);
402 pos = next_location + Position(0, 1, 0);
403 block = world->GetBlock(pos);
404 horizontal_surroundings[1] = PathfindingBlockstate(block, pos, take_damage);
405 const bool horizontal_movement =
406 (!horizontal_surroundings[1].IsSolid() || // 1 is not solid
407 (horizontal_surroundings[1].GetHeight() - current_node.pos.second < 1.25f && // or 1 is solid and small
408 !horizontal_surroundings[0].IsSolid() && !horizontal_surroundings[0].IsHazardous()) // and 0 does not prevent standing
409 ) && !horizontal_surroundings[1].IsHazardous();
410
411 // If we can move horizontally, get the full column
412 if (horizontal_movement)
413 {
414 pos = next_location;
415 block = world->GetBlock(pos);
416 horizontal_surroundings[2] = PathfindingBlockstate(block, pos, take_damage);
417 pos = next_location + Position(0, -1, 0);
418 block = world->GetBlock(pos);
419 horizontal_surroundings[3] = PathfindingBlockstate(block, pos, take_damage);
420 pos = next_location + Position(0, -2, 0);
421 block = world->GetBlock(pos);
422 horizontal_surroundings[4] = PathfindingBlockstate(block, pos, take_damage);
423 pos = next_location + Position(0, -3, 0);
424 block = world->GetBlock(pos);
425 horizontal_surroundings[5] = PathfindingBlockstate(block, pos, take_damage);
426 }
427
428 // We can't make large jumps if our feet are in an incompatible block
429 // If we can jump, then we need the third column
430 if (allow_jump && can_jump)
431 {
432 pos = next_next_location + Position(0, 2, 0);
433 block = world->GetBlock(pos);
434 horizontal_surroundings[6] = PathfindingBlockstate(block, pos, take_damage);
435 pos = next_next_location + Position(0, 1, 0);
436 block = world->GetBlock(pos);
437 horizontal_surroundings[7] = PathfindingBlockstate(block, pos, take_damage);
438 pos = next_next_location;
439 block = world->GetBlock(pos);
440 horizontal_surroundings[8] = PathfindingBlockstate(block, pos, take_damage);
441 pos = next_next_location + Position(0, -1, 0);
442 block = world->GetBlock(pos);
443 horizontal_surroundings[9] = PathfindingBlockstate(block, pos, take_damage);
444 pos = next_next_location + Position(0, -2, 0);
445 block = world->GetBlock(pos);
446 horizontal_surroundings[10] = PathfindingBlockstate(block, pos, take_damage);
447 pos = next_next_location + Position(0, -3, 0);
448 block = world->GetBlock(pos);
449 horizontal_surroundings[11] = PathfindingBlockstate(block, pos, take_damage);
450 }
451
452 // Now that we know the surroundings, we can check all
453 // horizontal cases that would allow the bot to pass
454
455 /************ HORIZONTAL **************/
456
457 // ? ? ?
458 // x - ?
459 // x - ?
460 //--- o ?
461 // ? ?
462 // ? ?
463 if (!horizontal_surroundings[1].IsSolid()
464 && !horizontal_surroundings[1].IsHazardous()
465 && !horizontal_surroundings[2].IsSolid()
466 && !horizontal_surroundings[2].IsHazardous()
467 && !horizontal_surroundings[3].IsEmpty()
468 && !horizontal_surroundings[3].IsHazardous()
469 && (!horizontal_surroundings[3].IsFluid() // We can't go from above a fluid to above
470 || !vertical_surroundings[3].IsFluid() // another one to avoid "walking on water"
471 || horizontal_surroundings[2].IsFluid() // except if one or both "leg level" blocks
472 || vertical_surroundings[2].IsFluid()) // are also fluids
473 )
474 {
475 const bool above_block = horizontal_surroundings[2].IsClimbable() || horizontal_surroundings[3].IsClimbable() || horizontal_surroundings[3].GetHeight() + 1e-3f > current_node.pos.first.y;
476 const float new_cost = cost[current_node.pos] + 2.0f - 1.0f * above_block;
477 const std::pair<Position, float> new_pos = {
478 next_location + Position(0, -1 + 1 * above_block, 0),
479 above_block ? std::max(static_cast<float>(next_location.y), horizontal_surroundings[3].GetHeight()) : std::max(horizontal_surroundings[3].GetHeight(), horizontal_surroundings[4].GetHeight())
480 };
481 add_node_if_better(new_pos, new_cost, current_node);
482 }
483
484
485 // - - ?
486 // x o ?
487 // x ? ?
488 //--- ? ?
489 // ? ?
490 // ? ?
491 if (can_jump
492 && !vertical_surroundings[0].IsSolid()
493 && !vertical_surroundings[0].IsHazardous()
494 && vertical_surroundings[1].IsEmpty()
495 && (vertical_surroundings[2].IsSolid() || !vertical_surroundings[3].IsClimbable())
496 && !horizontal_surroundings[0].IsSolid()
497 && !horizontal_surroundings[0].IsHazardous()
498 && !horizontal_surroundings[1].IsEmpty()
499 && !horizontal_surroundings[1].IsHazardous()
500 && horizontal_surroundings[1].GetHeight() - current_node.pos.second < 1.25f
501 )
502 {
503 const float new_cost = cost[current_node.pos] + 2.5f;
504 const std::pair<Position, float> new_pos = {
505 next_location + Position(0, 1, 0),
506 std::max(horizontal_surroundings[1].GetHeight(), horizontal_surroundings[2].GetHeight()) // for the carpet on wall trick
507 };
508 add_node_if_better(new_pos, new_cost, current_node);
509 }
510
511 // - - ?
512 // x - ?
513 // x o ?
514 //--- ? ?
515 // ? ?
516 // ? ?
517 if ((can_jump || horizontal_surroundings[2].GetHeight() - current_node.pos.second < step_height)
518 && !vertical_surroundings[0].IsSolid()
519 && !vertical_surroundings[0].IsHazardous()
520 && vertical_surroundings[1].IsEmpty()
521 && (vertical_surroundings[2].IsSolid() || (vertical_surroundings[2].IsEmpty() && vertical_surroundings[3].IsSolid()))
522 && !horizontal_surroundings[0].IsSolid()
523 && !horizontal_surroundings[0].IsHazardous()
524 && !horizontal_surroundings[1].IsSolid()
525 && !horizontal_surroundings[1].IsHazardous()
526 && !horizontal_surroundings[2].IsEmpty()
527 && !horizontal_surroundings[2].IsHazardous()
528 && horizontal_surroundings[2].GetHeight() - current_node.pos.second < 1.25f
529 )
530 {
531 const bool above_block = horizontal_surroundings[1].IsClimbable() || horizontal_surroundings[2].IsClimbable() || horizontal_surroundings[2].GetHeight() + 1e-3f > current_node.pos.first.y + 1;
532 const float new_cost = cost[current_node.pos] + 1.0f + 1.0f * above_block + 0.5f * (horizontal_surroundings[1].IsClimbable() || horizontal_surroundings[2].GetHeight() - vertical_surroundings[2].GetHeight() > 0.5);
533 const std::pair<Position, float> new_pos = {
534 next_location + Position(0, 1 * above_block, 0),
535 above_block ? std::max(current_node.pos.first.y + 1.0f, horizontal_surroundings[2].GetHeight()) : std::max(horizontal_surroundings[2].GetHeight(), horizontal_surroundings[3].GetHeight())
536 };
537 add_node_if_better(new_pos, new_cost, current_node);
538 }
539
540 // ? ? ?
541 // x - ?
542 // x ?
543 //--- ?
544 // o ?
545 // ? ?
546 if (!horizontal_surroundings[1].IsSolid()
547 && !horizontal_surroundings[1].IsHazardous()
548 && horizontal_surroundings[2].IsEmpty()
549 && horizontal_surroundings[3].IsEmpty()
550 && !horizontal_surroundings[4].IsEmpty()
551 && !horizontal_surroundings[4].IsHazardous()
552 )
553 {
554 const bool above_block = horizontal_surroundings[4].IsClimbable() || horizontal_surroundings[4].GetHeight() + 1e-3f > current_node.pos.first.y - 1;
555 const float new_cost = cost[current_node.pos] + 3.5f - 1.0f * above_block;
556 const std::pair<Position, float> new_pos = {
557 next_location + Position(0, -2 + 1 * above_block, 0),
558 above_block ? std::max(current_node.pos.first.y - 1.0f, horizontal_surroundings[4].GetHeight()) : std::max(horizontal_surroundings[4].GetHeight(), horizontal_surroundings[5].GetHeight())
559 };
560 add_node_if_better(new_pos, new_cost, current_node);
561 }
562
563 // ? ? ?
564 // x - ?
565 // x ?
566 //--- ?
567 // ?
568 // o ?
569 if (!horizontal_surroundings[1].IsSolid()
570 && !horizontal_surroundings[1].IsHazardous()
571 && horizontal_surroundings[2].IsEmpty()
572 && horizontal_surroundings[3].IsEmpty()
573 && horizontal_surroundings[4].IsEmpty()
574 && !horizontal_surroundings[5].IsEmpty()
575 && !horizontal_surroundings[5].IsHazardous()
576 )
577 {
578 const bool above_block = horizontal_surroundings[5].IsClimbable() || horizontal_surroundings[5].GetHeight() + 1e-3f > current_node.pos.first.y - 2;
579 const float new_cost = cost[current_node.pos] + 4.5f - 1.0f * above_block;
580 const std::pair<Position, float> new_pos = {
581 next_location + Position(0, -3 + 1 * above_block, 0),
582 above_block ? std::max(current_node.pos.first.y - 2.0f, horizontal_surroundings[5].GetHeight()) : horizontal_surroundings[5].GetHeight() // no carpet on wall check here as we don't have the block below
583 };
584 add_node_if_better(new_pos, new_cost, current_node);
585 }
586
587 // ? ? ?
588 // x - ?
589 // x ?
590 //--- ?
591 // ?
592 // ?
593 // Special case here, we can drop down
594 // if there is a climbable at the bottom
595 if (!horizontal_surroundings[1].IsSolid()
596 && !horizontal_surroundings[1].IsHazardous()
597 && horizontal_surroundings[2].IsEmpty()
598 && horizontal_surroundings[3].IsEmpty()
599 && horizontal_surroundings[4].IsEmpty()
600 && horizontal_surroundings[5].IsEmpty()
601 )
602 {
603 for (int y = -4; next_location.y + y >= world->GetMinY(); --y)
604 {
605 pos = next_location + Position(0, y, 0);
606 block = world->GetBlock(pos);
607
608 if (block != nullptr && ((block->IsSolid() && !block->IsClimbable()) || (take_damage && block->IsHazardous())))
609 {
610 break;
611 }
612
613 const PathfindingBlockstate landing_block(block, pos, take_damage);
614 if (landing_block.IsClimbable())
615 {
616 const float new_cost = cost[current_node.pos] + std::abs(y) + 1.5f;
617 const std::pair<Position, float> new_pos = {
618 next_location + Position(0, y + 1, 0),
619 next_location.y + y + 1.0f
620 };
621 add_node_if_better(new_pos, new_cost, current_node);
622
623 break;
624 }
625 }
626 }
627
628 // If we can't make jumps, don't bother explore the rest
629 // of the cases
630 if (!allow_jump
631 || !can_jump
632 || vertical_surroundings[0].IsSolid() // Block above
633 || vertical_surroundings[0].IsHazardous() // Block above
634 || vertical_surroundings[1].IsSolid() // Block above
635 || vertical_surroundings[1].IsHazardous() // Block above
636 || vertical_surroundings[3].IsFluid() // "Walking" on fluid
637 || (!vertical_surroundings[2].IsSolid() && // Feet on nothing (inside climbable)
638 vertical_surroundings[3].IsEmpty())
639 || horizontal_surroundings[0].IsSolid() // Block above next column
640 || horizontal_surroundings[0].IsHazardous() // Hazard above next column
641 || horizontal_surroundings[1].IsSolid() // Non empty block in next column, can't jump through it
642 || horizontal_surroundings[1].IsHazardous() // Non empty block in next column, can't jump through it
643 || horizontal_surroundings[2].IsSolid() // Non empty block in next column, can't jump through it
644 || horizontal_surroundings[2].IsHazardous() // Non empty block in next column, can't jump through it
645 || horizontal_surroundings[6].IsSolid() // Block above nextnext column
646 || horizontal_surroundings[6].IsHazardous() // Hazard above nextnext column
647 )
648 {
649 continue;
650 }
651
652 /************ BIG JUMP **************/
653 // - - -
654 // x o
655 // x ?
656 //--- ? ?
657 // ? ?
658 // ? ?
659 if (!horizontal_surroundings[7].IsEmpty()
660 && !horizontal_surroundings[7].IsHazardous()
661 && horizontal_surroundings[7].GetHeight() - current_node.pos.second < 1.25f
662 )
663 {
664 // 5 > 4.5 as if horizontal_surroundings[3] is solid we prefer to walk then jump instead of big jump
665 // but if horizontal_surroundings[3] is hazardous we can jump over it
666 const float new_cost = cost[current_node.pos] + 5.0f;
667 const std::pair<Position, float> new_pos = {
668 next_next_location + Position(0, 1, 0),
669 std::max(horizontal_surroundings[7].GetHeight(), horizontal_surroundings[8].GetHeight()), // for the carpet on wall trick
670 };
671 add_node_if_better(new_pos, new_cost, current_node);
672 }
673
674 // - - -
675 // x
676 // x o
677 //--- ? ?
678 // ? ?
679 // ? ?
680 if (horizontal_surroundings[7].IsEmpty()
681 && !horizontal_surroundings[8].IsEmpty()
682 && !horizontal_surroundings[8].IsHazardous()
683 && horizontal_surroundings[8].GetHeight() - current_node.pos.second < 1.25f
684 )
685 {
686 const bool above_block = horizontal_surroundings[8].IsClimbable() || horizontal_surroundings[8].GetHeight() + 1e-3f > current_node.pos.first.y + 1;
687 // 4 > 3.5 as if horizontal_surroundings[3] is solid we prefer to walk then jump instead of big jump
688 // but if horizontal_surroundings[3] is hazardous we can jump over it
689 const float new_cost = cost[current_node.pos] + 3.0f + 1.0f * above_block;
690 const std::pair<Position, float> new_pos = {
691 next_next_location + Position(0, above_block * 1, 0),
692 above_block ? std::max(current_node.pos.first.y + 1.0f, horizontal_surroundings[8].GetHeight()) : std::max(horizontal_surroundings[8].GetHeight(), horizontal_surroundings[9].GetHeight())
693 };
694 add_node_if_better(new_pos, new_cost, current_node);
695 }
696
697 // - - -
698 // x
699 // x
700 //--- ? o
701 // ? ?
702 // ? ?
703 if (horizontal_surroundings[7].IsEmpty()
704 && horizontal_surroundings[8].IsEmpty()
705 && !horizontal_surroundings[9].IsEmpty()
706 && !horizontal_surroundings[9].IsHazardous()
707 )
708 {
709 const bool above_block = horizontal_surroundings[9].IsClimbable() || horizontal_surroundings[9].GetHeight() + 1e-3f > current_node.pos.first.y;
710 const float new_cost = cost[current_node.pos] + 3.5f - 1.0f * above_block;
711 const std::pair<Position, float> new_pos = {
712 next_next_location + Position(0, -1 + 1 * above_block, 0),
713 above_block ? std::max(static_cast<float>(current_node.pos.first.y), horizontal_surroundings[9].GetHeight()) : std::max(horizontal_surroundings[9].GetHeight(), horizontal_surroundings[10].GetHeight())
714 };
715 add_node_if_better(new_pos, new_cost, current_node);
716 }
717
718 // - - -
719 // x
720 // x
721 //--- ?
722 // ? o
723 // ? ?
724 if (horizontal_surroundings[7].IsEmpty()
725 && horizontal_surroundings[8].IsEmpty()
726 && horizontal_surroundings[9].IsEmpty()
727 && !horizontal_surroundings[10].IsEmpty()
728 && !horizontal_surroundings[10].IsHazardous()
729 )
730 {
731 const bool above_block = horizontal_surroundings[10].IsClimbable() || horizontal_surroundings[10].GetHeight() + 1e-3f > current_node.pos.first.y - 1;
732 const float new_cost = cost[current_node.pos] + 4.5f - 1.0f * above_block;
733 const std::pair<Position, float> new_pos = {
734 next_next_location + Position(0, -2 + 1 * above_block, 0),
735 above_block ? std::max(current_node.pos.first.y - 1.0f, horizontal_surroundings[10].GetHeight()) : std::max(horizontal_surroundings[10].GetHeight(), horizontal_surroundings[11].GetHeight())
736 };
737 add_node_if_better(new_pos, new_cost, current_node);
738 }
739
740 // - - -
741 // x
742 // x
743 //--- ?
744 // ?
745 // ? o
746 if (horizontal_surroundings[7].IsEmpty()
747 && horizontal_surroundings[8].IsEmpty()
748 && horizontal_surroundings[9].IsEmpty()
749 && horizontal_surroundings[10].IsEmpty()
750 && !horizontal_surroundings[11].IsEmpty()
751 && !horizontal_surroundings[11].IsHazardous()
752 )
753 {
754 const bool above_block = horizontal_surroundings[11].IsClimbable() || horizontal_surroundings[11].GetHeight() + 1e-3f > current_node.pos.first.y - 2;
755 const float new_cost = cost[current_node.pos] + 6.5f - 1.0f * above_block;
756 const std::pair<Position, float> new_pos = {
757 next_next_location + Position(0, -3 + 1 * above_block, 0),
758 above_block ? std::max(current_node.pos.first.y - 2.0f, horizontal_surroundings[11].GetHeight()) : horizontal_surroundings[11].GetHeight()
759 };
760 add_node_if_better(new_pos, new_cost, current_node);
761 }
762 } // neighbour loop
763 }
764
765 auto it_end_path = came_from.begin();
766
767 // We search for the node respecting
768 // the criteria AND the closest to
769 // start as it should often lead
770 // to a shorter path. In case of a tie,
771 // take the one the closest to the end
772 int best_dist = std::numeric_limits<int>::max();
773 int best_dist_start = std::numeric_limits<int>::max();
774 for (auto it = came_from.begin(); it != came_from.end(); ++it)
775 {
776 const Position diff = it->first.first - end;
777 const int d_xz = std::abs(diff.x) + std::abs(diff.z);
778 const int d = d_xz + std::abs(diff.y);
779 const Position diff_start = it->first.first - start;
780 const int d_start = std::abs(diff_start.x) + std::abs(diff_start.y) + std::abs(diff_start.z);
781 if (d <= dist_tolerance && d >= min_end_dist && d_xz >= min_end_dist_xz &&
782 (d_start < best_dist_start || (d_start == best_dist_start && d < best_dist))
783 )
784 {
785 best_dist = d;
786 best_dist_start = d_start;
787 it_end_path = it;
788 }
789 }
790
791 // There were no node respecting the criteria in the search,
792 // this might mean we reached search limit
793 // Take closest node to the goal in this case
794 if (best_dist == std::numeric_limits<int>::max())
795 {
796 for (auto it = came_from.begin(); it != came_from.end(); ++it)
797 {
798 const Position diff = it->first.first - end;
799 const int d_xz = std::abs(diff.x) + std::abs(diff.z);
800 const int d = d_xz + std::abs(diff.y);
801 const Position diff_start = it->first.first - start;
802 const int d_start = std::abs(diff_start.x) + std::abs(diff_start.y) + std::abs(diff_start.z);
803 if (d >= min_end_dist && d_xz >= min_end_dist_xz && (d < best_dist || d == best_dist && d_start < best_dist_start))
804 {
805 best_dist = d;
806 best_dist_start = d_start;
807 it_end_path = it;
808 }
809 }
810 }
811
812 std::deque<std::pair<Position, float>> output_deque;
813 output_deque.push_front(it_end_path->first);
814 while (it_end_path->second.first != start)
815 {
816 it_end_path = came_from.find(it_end_path->second);
817 output_deque.push_front(it_end_path->first);
818 }
819
820 return std::vector<std::pair<Position, float>>(output_deque.begin(), output_deque.end());
821 }
822
823#if PROTOCOL_VERSION < 767 /* < 1.21 */
824 // a75f87e0-0583-435b-847a-cf0c18ede2d1
825 static constexpr std::array<unsigned char, 16> botcraft_pathfinding_speed_key= { 0xA7, 0x5F, 0x87, 0xE0, 0x05, 0x83, 0x43, 0x5B, 0x84, 0x7A, 0xCF, 0x0C, 0x18, 0xED, 0xE2, 0xD1 };
826#else
827 static const std::string botcraft_pathfinding_speed_key = "botcraft:speed";
828#endif
829
830 bool Move(BehaviourClient& client, std::shared_ptr<LocalPlayer>& local_player, const Vector3<double>& target_position, const float speed_factor, const bool sprint)
831 {
832 const Position target_block = target_position.ToBlockPos();
833 const Vector3<double> look_at_target = target_position + Vector3<double>(0.0, local_player->GetEyeHeight(), 0.0);
834 const Vector3<double> motion_vector = target_position - local_player->GetPosition();
835 const double half_player_width = 0.5 * local_player->GetWidth();
836 const Vector3<double> horizontal_target_position(target_position.x, 0.0, target_position.z);
837 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
838
839 std::shared_ptr<World> world = client.GetWorld();
840
841 if (speed_factor != 1.0f)
842 {
843 local_player->SetAttributeModifier(
847 speed_factor - 1.0f, // -1 as MultiplyTotal will multiply by (1.0 + x)
849 });
850 }
851 Utilities::OnEndScope botcraft_speed_modifier_remover([&]() {
852 local_player->RemoveAttributeModifier(EntityAttribute::Type::MovementSpeed, botcraft_pathfinding_speed_key);
853 });
854
855 local_player->LookAt(look_at_target, true);
856
857 // Move horizontally (with a jump if necessary)
858 if (std::abs(motion_vector.x) > 0.5 || std::abs(motion_vector.z) > 0.5)
859 {
860 // If we need to jump
861 if (motion_vector.y > 0.5 || std::abs(motion_vector.x) > 1.5 || std::abs(motion_vector.z) > 1.5)
862 {
863 // If not a jump above a gap, wait until reaching the next Y value before moving X/Z
864 if (std::abs(motion_vector.x) < 1.5 && std::abs(motion_vector.z) < 1.5)
865 {
866 local_player->SetInputsJump(true);
867
868 if (!Utilities::YieldForCondition([&]() -> bool
869 {
870 return local_player->GetY() >= target_block.y;
871 }, client, 40.0 * ms_per_tick))
872 {
873 return false;
874 }
875 }
876 // It's a long jump above a gap
877 else
878 {
879 const Vector3<double> current_block_center_xz(
880 std::floor(local_player->GetX()) + 0.5,
881 0.0,
882 std::floor(local_player->GetZ()) + 0.5
883 );
884 // Start to move forward to have speed before jumping
885 if (!Utilities::YieldForCondition([&]() -> bool
886 {
887 if (local_player->GetDirtyInputs())
888 {
889 return false;
890 }
891
892 // We need to add the forward input even for the tick we do the jump
893 local_player->LookAt(look_at_target, false);
894 local_player->SetInputsForward(1.0);
895 local_player->SetInputsSprint(sprint);
896
897 if (Vector3<double>(local_player->GetX(), 0.0, local_player->GetZ()).SqrDist(current_block_center_xz) > half_player_width * half_player_width)
898 {
899 // Then jump
900 local_player->SetInputsJump(true);
901 return true;
902 }
903
904 return false;
905 }, client, 20.0 * ms_per_tick))
906 {
907 return false;
908 }
909 }
910 }
911 // Move forward to the right X/Z location
912 if (!Utilities::YieldForCondition([&]() -> bool
913 {
914 if (local_player->GetDirtyInputs())
915 {
916 return false;
917 }
918
919 const Vector3<double> current_pos = local_player->GetPosition();
920 const Vector3<double> current_motion_vector = target_position - current_pos;
921 // If we moved past the target (dot product between initial and current motion vector < 0 ) or we're close enough
922 if ((current_motion_vector.x * motion_vector.x + current_motion_vector.z * motion_vector.z) < 0.0 ||
923 Vector3<double>(current_pos.x, 0.0, current_pos.z).SqrDist(horizontal_target_position) < (0.5 - half_player_width) * (0.5 - half_player_width))
924 {
925 return true;
926 }
927
928 local_player->LookAt(look_at_target, false);
929 const Vector3<double> speed = local_player->GetSpeed();
930 double forward = 1.0;
931 // If we need to fall, stop accelerating to prevent "overshooting" and potentially
932 // hit some block on the bottom or on the side of the dropshoot
933 if (motion_vector.y < -0.5 &&
934 static_cast<int>(std::floor(current_pos.x)) == target_block.x &&
935 static_cast<int>(std::floor(current_pos.z)) == target_block.z)
936 {
937 if (std::max(std::abs(speed.x), std::abs(speed.z)) > 0.12) // 0.12 because I needed a value so why not
938 {
939 forward = -1.0;
940 }
941 else if (std::max(std::abs(speed.x), std::abs(speed.z)) > 0.06)
942 {
943 forward = 0.0;
944 }
945 }
946 local_player->SetInputsForward(forward);
947 local_player->SetInputsSprint(sprint && (forward == 1.0));
948
949 return false;
950 }, client, (std::abs(motion_vector.x) + std::abs(motion_vector.z) + (motion_vector.y < -0.5)) * 20.0 * ms_per_tick))
951 {
952 return false;
953 }
954 }
955
956 // If we need to go down, let the gravity do it's job, unless we are in a scaffholding or water,
957 // in which case we need to press sneak to go down. If free falling in air, press sneak to catch
958 // climbable at the bottom, preventing fall damage
959 if (local_player->GetY() > target_position.y && !Utilities::YieldForCondition([&]() -> bool
960 {
961 // Previous inputs have not been processed by physics thread yet
962 if (local_player->GetDirtyInputs())
963 {
964 return false;
965 }
966
967 if (static_cast<int>(std::floor(local_player->GetY())) <= target_block.y &&
968 (local_player->GetOnGround() || local_player->IsClimbing() || local_player->IsInFluid()))
969 {
970 return true;
971 }
972
973 const Vector3<double> current_pos = local_player->GetPosition();
974 const Blockstate* feet_block = world->GetBlock(current_pos.ToBlockPos());
975 local_player->SetInputsSneak(feet_block != nullptr &&
976 (feet_block->IsFluidOrWaterlogged() || feet_block->IsScaffolding() || (feet_block->IsAir() && motion_vector.y < -2.5)));
977 local_player->SetInputsJump(local_player->GetFlying()); // Stop flying
978
979 // If we drifted too much, adjust toward target X/Z position
980 if (Vector3<double>(current_pos.x, 0.0, current_pos.z).SqrDist(horizontal_target_position) > (0.5 - half_player_width) * (0.5 - half_player_width))
981 {
982 local_player->LookAt(look_at_target, false);
983 local_player->SetInputsForward(1.0);
984 }
985
986 return false;
987 }, client, 20.0 * ms_per_tick * (1 + std::abs(motion_vector.y))))
988 {
989 return false;
990 }
991
992 // We need to go up (either from ground or in a climbable/water)
993 if (local_player->GetY() < target_position.y && !Utilities::YieldForCondition([&]() -> bool
994 {
995 // Previous inputs have not been processed by physics thread yet
996 if (local_player->GetDirtyInputs())
997 {
998 return false;
999 }
1000
1001 const Vector3<double> current_pos = local_player->GetPosition();
1002 if (static_cast<int>(std::floor(current_pos.y)) >= target_block.y)
1003 {
1004 return true;
1005 }
1006
1007 local_player->SetInputsJump(true);
1008
1009 // If we drifted too much, adjust toward target X/Z position
1010 if (Vector3<double>(current_pos.x, 0.0, current_pos.z).SqrDist(horizontal_target_position) > (0.5 - half_player_width) * (0.5 - half_player_width))
1011 {
1012 local_player->LookAt(look_at_target, false);
1013 local_player->SetInputsForward(1.0);
1014 }
1015
1016 return false;
1017 }, client, 20.0 * ms_per_tick * (1 + std::abs(motion_vector.y))))
1018 {
1019 return false;
1020 }
1021
1022 // We are in the target block, make sure we are not a bit too high
1023 return Utilities::YieldForCondition([&]() -> bool
1024 {
1025 if (local_player->GetDirtyInputs())
1026 {
1027 return false;
1028 }
1029
1030 // One physics tick climbing down is 0.15 at most, so this should never get too low in theory
1031 if (local_player->GetY() >= target_position.y && local_player->GetY() - target_position.y < 0.2)
1032 {
1033 return true;
1034 }
1035
1036 const Vector3<double> current_pos = local_player->GetPosition();
1037 const Blockstate* feet_block = world->GetBlock(current_pos.ToBlockPos());
1038 local_player->SetInputsSneak(feet_block != nullptr &&
1039 (feet_block->IsFluidOrWaterlogged() || feet_block->IsScaffolding() || (feet_block->IsAir() && motion_vector.y < -2.5)));
1040 local_player->SetInputsJump(local_player->GetFlying()); // Stop flying
1041
1042 // If we drifted too much, adjust toward target X/Z position
1043 if (Vector3<double>(current_pos.x, 0.0, current_pos.z).SqrDist(horizontal_target_position) > (0.5 - half_player_width) * (0.5 - half_player_width))
1044 {
1045 local_player->LookAt(look_at_target, false);
1046 local_player->SetInputsForward(1.0);
1047 }
1048
1049 return false;
1050 }, client, 20.0 * ms_per_tick);
1051 }
1052
1053 // Try to cancel speed while going toward the target position
1055 {
1056 std::shared_ptr<LocalPlayer> player = client.GetLocalPlayer();
1057 const Vector3<double> pos = player->GetPosition();
1058 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
1059
1060 Utilities::YieldForCondition([&]() -> bool
1061 {
1062 if (player->GetDirtyInputs())
1063 {
1064 return false;
1065 }
1066
1067 const Vector3<double> pos = player->GetPosition();
1068 const Vector3<double> speed = player->GetSpeed();
1069 if (std::abs(pos.x - target.x) < 0.2 &&
1070 std::abs(pos.z - target.z) < 0.2 &&
1071 std::abs(speed.x) < 0.05 &&
1072 std::abs(speed.z) < 0.05)
1073 {
1074 return true;
1075 }
1076
1077 const float yaw_rad = player->GetYaw() * 0.01745329251994 /* PI/180 */;
1078 const double cos_yaw = std::cos(yaw_rad);
1079 const double sin_yaw = std::sin(yaw_rad);
1080
1081 // Project on forward/left axis
1082 const double delta_forward = (target.z - pos.z) * cos_yaw - (target.x - pos.x) * sin_yaw;
1083 const double delta_left = (target.x - pos.x) * cos_yaw + (target.z - pos.z) * sin_yaw;
1084 const double speed_forward = speed.z * cos_yaw - speed.x * sin_yaw;
1085 const double speed_left = speed.x * cos_yaw + speed.z * sin_yaw;
1086
1087 // If going too fast, reduce speed
1088 if (std::abs(speed_forward) > 0.1)
1089 {
1090 player->SetInputsForward(speed_forward > 0.0 ? -1.0f : 1.0f);
1091 }
1092 // Opposite signs or going toward the same direction but slowly
1093 else if (delta_forward * speed_forward < 0.0 || std::abs(speed_forward) < 0.02)
1094 {
1095 player->SetInputsForward(delta_forward > 0.0 ? 1.0f : -1.0f);
1096 }
1097 else
1098 {
1099 player->SetInputsForward(0.0f);
1100 }
1101
1102 // Same thing on left axis
1103 if (std::abs(speed_left) > 0.1)
1104 {
1105 player->SetInputsLeft(speed_left > 0.0 ? -1.0f : 1.0f);
1106 }
1107 else if (delta_left * speed_left < 0.0 || std::abs(speed_left) < 0.02)
1108 {
1109 player->SetInputsLeft(delta_left > 0.0 ? 1.0f : -1.0f);
1110 }
1111 else
1112 {
1113 player->SetInputsLeft(0.0f);
1114 }
1115
1116 return false;
1117 }, client, 20.0 * ms_per_tick);
1118 }
1119
1120 Status GoToImpl(BehaviourClient& client, const Vector3<double>& goal, const int dist_tolerance, const int min_end_dist, const int min_end_dist_xz, const bool allow_jump, const bool sprint, float speed_factor)
1121 {
1122 if (min_end_dist > dist_tolerance)
1123 {
1124 LOG_WARNING("GoTo.min_end_dist should be <= dist_tolerance if you want pathfinding to work as expected");
1125 }
1126 if (min_end_dist_xz > dist_tolerance)
1127 {
1128 LOG_WARNING("GoTo.min_end_dist_xz should be <= dist_tolerance if you want pathfinding to work as expected");
1129 }
1130 if (speed_factor <= 0.0f)
1131 {
1132 LOG_WARNING("GoTo.speed_factor should be >0 or the bot won't move. Setting it to default value of 1.0");
1133 speed_factor = 1.0f;
1134 }
1135 std::shared_ptr<LocalPlayer> local_player = client.GetLocalPlayer();
1136 const Position goal_block = goal.ToBlockPos();
1137
1138 // Don't bother with pathfinding in spectator mode, directly go to the goal
1139 if (local_player->GetGameMode() == GameType::Spectator)
1140 {
1141 const Vector3<double> target(0.5 + goal_block.x, 0.5 + goal_block.y, 0.5 + goal_block.z);
1142 const double square_half_width = local_player->GetWidth() * local_player->GetWidth() / 4.0;
1143 const double dist = std::sqrt((target - local_player->GetPosition()).SqrNorm());
1144 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
1145 if (Utilities::YieldForCondition([&]() -> bool
1146 {
1147 if (!local_player->GetDirtyInputs())
1148 {
1149 local_player->LookAt(target, true);
1150 local_player->SetInputsForward(1.0);
1151 }
1152
1153 return local_player->GetPosition().SqrDist(target) < square_half_width;
1154 }, client, dist * 20.0 * ms_per_tick))
1155 {
1156 AdjustPosSpeed(client, goal);
1157 return Status::Success;
1158 }
1159 else
1160 {
1161 return Status::Failure;
1162 }
1163 }
1164
1165 if (StopFlying(client) == Status::Failure)
1166 {
1167 return Status::Failure;
1168 }
1169
1170 std::shared_ptr<World> world = client.GetWorld();
1171#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
1172 const float step_height = static_cast<float>(local_player->GetAttributeStepHeightValue());
1173#else
1174 const float step_height = 0.6f;
1175#endif
1176 Position current_position;
1177 do
1178 {
1179 // Wait until we are on the ground or climbing
1180 const std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
1181 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
1182 while (!local_player->GetOnGround() && !local_player->IsClimbing() && !local_player->IsInFluid())
1183 {
1184 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() >= 40.0 * ms_per_tick)
1185 {
1186 LOG_WARNING("Timeout waiting for the bot to land on the floor between two block move. Staying at " << local_player->GetPosition());
1187 return Status::Failure;
1188 }
1189 client.Yield();
1190 }
1191
1192 // Get the position, we add 0.25 to Y in case we are at X.97 instead of X+1
1193 current_position = (local_player->GetPosition() + Vector3<double>(0.0, 0.25, 0.0)).ToBlockPos();
1194
1195 std::vector<std::pair<Position, float>> path;
1196 const bool is_goal_loaded = world->IsLoaded(goal_block);
1197
1198 const int current_diff_xz = std::abs(goal_block.x - current_position.x) + std::abs(goal_block.z - current_position.z);
1199 const int current_diff = current_diff_xz + std::abs(goal_block.y - current_position.y);
1200 // Path finding step
1201 if (!is_goal_loaded)
1202 {
1203 LOG_INFO('[' << client.GetNetworkManager()->GetMyName() << "] Current goal position " << goal_block << " is either air or not loaded, trying to get closer to load the chunk");
1204 Vector3<double> goal_direction(goal_block.x - current_position.x, goal_block.y - current_position.y, goal_block.z - current_position.z);
1205 goal_direction.Normalize();
1206 path = FindPath(
1207 world,
1208 current_position,
1209 current_position + Position(
1210 static_cast<int>(goal_direction.x * 32.0),
1211 static_cast<int>(goal_direction.y * 32.0),
1212 static_cast<int>(goal_direction.z * 32.0)
1213 ),
1214 dist_tolerance, min_end_dist, min_end_dist_xz,
1215 allow_jump, !local_player->GetInvulnerable(), step_height
1216 );
1217 }
1218 else
1219 {
1220 if (dist_tolerance && current_diff <= dist_tolerance && current_diff >= min_end_dist && current_diff_xz >= min_end_dist_xz)
1221 {
1222 AdjustPosSpeed(client, goal);
1223 return Status::Success;
1224 }
1225 path = FindPath(
1226 world,
1227 current_position, goal_block,
1228 dist_tolerance, min_end_dist, min_end_dist_xz,
1229 allow_jump, !local_player->GetInvulnerable(), step_height
1230 );
1231 }
1232
1233 if (path.size() == 0 || path.back().first == current_position)
1234 {
1235 if (!dist_tolerance && current_diff > 0)
1236 {
1237 LOG_WARNING("Pathfinding cannot find a better position than " << current_position << " (asked " << goal_block << "). Staying there.");
1238 return Status::Failure;
1239 }
1240 else if (!dist_tolerance)
1241 {
1242 AdjustPosSpeed(client, goal);
1243 return Status::Success;
1244 }
1245 else
1246 {
1247 if (current_diff <= dist_tolerance)
1248 {
1249 AdjustPosSpeed(client, goal);
1250 return Status::Success;
1251 }
1252 else
1253 {
1254 return Status::Failure;
1255 }
1256 }
1257 }
1258 // To avoid going back and forth two positions that are at the same distance of an unreachable goal
1259 else if (current_diff >= min_end_dist && current_diff_xz >= min_end_dist_xz && std::abs(goal_block.x - path.back().first.x) + std::abs(goal_block.y - path.back().first.y) + std::abs(goal_block.z - path.back().first.z) >= current_diff)
1260 {
1261 LOG_WARNING("Pathfinding cannot find a better position than " << current_position << " (asked " << goal_block << "). Staying there.");
1262 return Status::Failure;
1263 }
1264
1265 for (int i = 0; i < path.size(); ++i)
1266 {
1267 // Basic verification to check we won't try to walk on air.
1268 // If so, it means some blocks have changed, better to
1269 // recompute a new path
1270 const Blockstate* next_target = world->GetBlock(path[i].first);
1271 const Blockstate* below = world->GetBlock(path[i].first + Position(0, -1, 0));
1272 if ((next_target == nullptr || (!next_target->IsClimbable() && !next_target->IsFluid())) &&
1273 (below == nullptr || below->IsAir()))
1274 {
1275 break;
1276 }
1277
1278 // If something went wrong, break and
1279 // replan the whole path to the goal
1280 if (!Move(client, local_player, Vector3<double>(path[i].first.x + 0.5, path[i].second, path[i].first.z + 0.5), speed_factor, sprint))
1281 {
1282 break;
1283 }
1284 // Otherwise just update current position for
1285 // next move
1286 else
1287 {
1288 const Vector3<double> local_player_pos = local_player->GetPosition();
1289 // Get the position, we add 0.25 to Y in case we are at X.97 instead of X+1
1290 current_position = (local_player_pos + Vector3<double>(0.0, 0.25, 0.0)).ToBlockPos();
1291 }
1292 }
1293 } while (current_position != goal_block);
1294
1295 AdjustPosSpeed(client, goal);
1296 return Status::Success;
1297 }
1298
1299 Status GoTo(BehaviourClient& client, const Position& goal, const int dist_tolerance, const int min_end_dist, const int min_end_dist_xz, const bool allow_jump, const bool sprint, const float speed_factor)
1300 {
1301 constexpr std::array variable_names = {
1302 "GoTo.goal",
1303 "GoTo.dist_tolerance",
1304 "GoTo.min_end_dist",
1305 "GoTo.min_end_dist_xz",
1306 "GoTo.allow_jump",
1307 "GoTo.sprint",
1308 "GoTo.speed_factor"
1309 };
1310
1311 Blackboard& blackboard = client.GetBlackboard();
1312
1313 blackboard.Trace(variable_names[0], goal);
1314 blackboard.Trace(variable_names[1], dist_tolerance);
1315 blackboard.Trace(variable_names[2], min_end_dist);
1316 blackboard.Trace(variable_names[3], min_end_dist_xz);
1317 blackboard.Trace(variable_names[4], allow_jump);
1318 blackboard.Trace(variable_names[5], sprint);
1319 blackboard.Trace(variable_names[6], speed_factor);
1320
1321 return GoToImpl(client, Vector3<double>(goal.x + 0.5, goal.y, goal.z + 0.5), dist_tolerance, min_end_dist, min_end_dist_xz, allow_jump, sprint, speed_factor);
1322 }
1323
1325 {
1326 constexpr std::array variable_names = {
1327 "GoTo.goal",
1328 "GoTo.dist_tolerance",
1329 "GoTo.min_end_dist",
1330 "GoTo.min_end_dist_xz",
1331 "GoTo.allow_jump",
1332 "GoTo.sprint",
1333 "GoTo.speed_factor"
1334 };
1335
1336 Blackboard& blackboard = client.GetBlackboard();
1337
1338 // Mandatory
1339 const Position& goal = blackboard.Get<Position>(variable_names[0]);
1340
1341 // Optional
1342 const int dist_tolerance = blackboard.Get(variable_names[1], 0);
1343 const int min_end_dist = blackboard.Get(variable_names[2], 0);
1344 const int min_end_dist_xz = blackboard.Get(variable_names[3], 0);
1345 const bool allow_jump = blackboard.Get(variable_names[4], true);
1346 const bool sprint = blackboard.Get(variable_names[5], true);
1347 const float speed_factor = blackboard.Get(variable_names[6], 1.0f);
1348
1349 return GoToImpl(client, Vector3<double>(goal.x + 0.5, goal.y, goal.z + 0.5), dist_tolerance, min_end_dist, min_end_dist_xz, allow_jump, sprint, speed_factor);
1350 }
1351
1352 Status GoToDouble(BehaviourClient& client, const Vector3<double>& goal, const bool allow_jump, const bool sprint, const float speed_factor)
1353 {
1354 constexpr std::array variable_names = {
1355 "GoToDouble.goal",
1356 "GoToDouble.allow_jump",
1357 "GoToDouble.sprint",
1358 "GoToDouble.speed_factor"
1359 };
1360
1361 Blackboard& blackboard = client.GetBlackboard();
1362
1363 blackboard.Trace(variable_names[0], goal);
1364 blackboard.Trace(variable_names[1], allow_jump);
1365 blackboard.Trace(variable_names[2], sprint);
1366 blackboard.Trace(variable_names[3], speed_factor);
1367
1368 return GoToImpl(client, goal, 0, 0, 0, allow_jump, sprint, speed_factor);
1369 }
1370
1372 {
1373 constexpr std::array variable_names = {
1374 "GoToDouble.goal",
1375 "GoToDouble.allow_jump",
1376 "GoToDouble.sprint",
1377 "GoToDouble.speed_factor"
1378 };
1379
1380 Blackboard& blackboard = client.GetBlackboard();
1381
1382 // Mandatory
1383 const Vector3<double>& goal = blackboard.Get<Vector3<double>>(variable_names[0]);
1384
1385 // Optional
1386 const bool allow_jump = blackboard.Get(variable_names[1], true);
1387 const bool sprint = blackboard.Get(variable_names[2], true);
1388 const float speed_factor = blackboard.Get(variable_names[3], 1.0f);
1389
1390 return GoToImpl(client, goal, 0, 0, 0, allow_jump, sprint, speed_factor);
1391 }
1392
1393
1394 Status LookAtImpl(BehaviourClient& client, const Vector3<double>& target, const bool set_pitch, const bool sync_to_server)
1395 {
1396 client.GetLocalPlayer()->LookAt(target, set_pitch);
1397
1398 if (!sync_to_server)
1399 {
1400 return Status::Success;
1401 }
1402
1403 return SyncPosRotToServer(client);
1404 }
1405
1406 Status LookAt(BehaviourClient& client, const Vector3<double>& target, const bool set_pitch, const bool sync_to_server)
1407 {
1408 const std::array variable_names = {
1409 "LookAt.target",
1410 "LookAt.set_pitch",
1411 "LookAt.sync_to_server",
1412 };
1413
1414 Blackboard& blackboard = client.GetBlackboard();
1415
1416 blackboard.Trace(variable_names[0], target);
1417 blackboard.Trace(variable_names[1], set_pitch);
1418 blackboard.Trace(variable_names[2], sync_to_server);
1419
1420 return LookAtImpl(client, target, set_pitch, sync_to_server);
1421 }
1422
1424 {
1425 const std::array variable_names = {
1426 "LookAt.target",
1427 "LookAt.set_pitch",
1428 "LookAt.sync_to_server",
1429 };
1430
1431 Blackboard& blackboard = client.GetBlackboard();
1432
1433 // Mandatory
1434 const Vector3<double>& target = blackboard.Get<Vector3<double>>(variable_names[0]);
1435
1436 // Optional
1437 const bool set_pitch = blackboard.Get(variable_names[1], true);
1438 const bool sync_to_server = blackboard.Get(variable_names[2], true);
1439
1440 return LookAtImpl(client, target, set_pitch, sync_to_server);
1441 }
1442
1444 {
1445 std::shared_ptr<LocalPlayer> local_player = client.GetLocalPlayer();
1446 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
1447 if (!local_player->GetMayFly())
1448 {
1449 return Status::Failure;
1450 }
1451
1452 if (local_player->GetFlying())
1453 {
1454 return Status::Success;
1455 }
1456
1457 local_player->SetInputsJump(true);
1458 if (!Utilities::YieldForCondition([&]() -> bool
1459 {
1460 return !local_player->GetDirtyInputs();
1461 }, client, 3.0 * ms_per_tick))
1462 {
1463 return Status::Failure;
1464 }
1465 local_player->SetInputsJump(false);
1466 if (!Utilities::YieldForCondition([&]() -> bool
1467 {
1468 return !local_player->GetDirtyInputs();
1469 }, client, 3.0 * ms_per_tick))
1470 {
1471 return Status::Failure;
1472 }
1473 local_player->SetInputsJump(true);
1474 if (!Utilities::YieldForCondition([&]() -> bool
1475 {
1476 return !local_player->GetDirtyInputs();
1477 }, client, 3.0 * ms_per_tick))
1478 {
1479 return Status::Failure;
1480 }
1481
1482 return local_player->GetFlying() ? Status::Success : Status::Failure;
1483 }
1484
1486 {
1487 std::shared_ptr<LocalPlayer> local_player = client.GetLocalPlayer();
1488 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
1489 if (!local_player->GetFlying())
1490 {
1491 return Status::Success;
1492 }
1493
1494 local_player->SetInputsJump(true);
1495 if (!Utilities::YieldForCondition([&]() -> bool
1496 {
1497 return !local_player->GetDirtyInputs();
1498 }, client, 3.0 * ms_per_tick))
1499 {
1500 return Status::Failure;
1501 }
1502 local_player->SetInputsJump(false);
1503 if (!Utilities::YieldForCondition([&]() -> bool
1504 {
1505 return !local_player->GetDirtyInputs();
1506 }, client, 3.0 * ms_per_tick))
1507 {
1508 return Status::Failure;
1509 }
1510 local_player->SetInputsJump(true);
1511 if (!Utilities::YieldForCondition([&]() -> bool
1512 {
1513 return !local_player->GetDirtyInputs();
1514 }, client, 3.0 * ms_per_tick))
1515 {
1516 return Status::Failure;
1517 }
1518
1519 return local_player->GetFlying() ? Status::Failure : Status::Success;
1520 }
1521
1523 {
1524 std::shared_ptr<LocalPlayer> local_player = client.GetLocalPlayer();
1525 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
1526 // Wait for next physics update
1527 local_player->SetDirtyInputs();
1528 return Utilities::YieldForCondition([&] {
1529 return !local_player->GetDirtyInputs();
1530 }, client, 5.0 * ms_per_tick) ? Status::Success : Status::Failure;
1531 }
1532}
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
#define LOG_INFO(osstream)
Definition Logger.hpp:43
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.
const T & Get(const std::string &key)
Get the map value at key, casting it to T.
void Trace(const std::string &key, const std::any &value, const bool add_trace_prefix=true) const
Notify observers of a value without storing it (debug view only)
bool IsHazardous() const
bool IsFluidOrWaterlogged() const
bool IsClimbable() const
std::set< AABB > GetCollidersAtPos(const Position &pos) const
bool IsScaffolding() const
std::shared_ptr< NetworkManager > GetNetworkManager() const
std::shared_ptr< PhysicsManager > GetPhysicsManager() const
std::shared_ptr< LocalPlayer > GetLocalPlayer() const
std::shared_ptr< World > GetWorld() const
const Blockstate * GetBlockstate() const
PathfindingBlockstate(const Blockstate *block, const Position &pos, const bool take_damage)
A class to execute a callback when destroyed.
bool YieldForCondition(const std::function< bool()> &condition, BehaviourClient &client, const long long int timeout_ms=0)
Status GoToImpl(BehaviourClient &client, const Vector3< double > &goal, const int dist_tolerance, const int min_end_dist, const int min_end_dist_xz, const bool allow_jump, const bool sprint, float speed_factor)
Status GoToDouble(BehaviourClient &client, const Vector3< double > &goal, const bool allow_jump=true, const bool sprint=true, const float speed_factor=1.0f)
Find a path to a position and navigate to it.
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 LookAtImpl(BehaviourClient &client, const Vector3< double > &target, const bool set_pitch, const bool sync_to_server)
void AdjustPosSpeed(BehaviourClient &client, const Vector3< double > &target)
Status StartFlying(BehaviourClient &client)
Make the current player fly (as in creative/spectator mode, NOT WITH ELYTRA)
Status SyncPosRotToServer(BehaviourClient &client)
This task will make sure the current player position/orientation have been sent to the server This is...
Vector3< int > Position
Definition Vector3.hpp:314
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.
bool Move(BehaviourClient &client, std::shared_ptr< LocalPlayer > &local_player, const Vector3< double > &target_position, const float speed_factor, const bool sprint)
std::vector< std::pair< Position, float > > FindPath(const std::shared_ptr< World > &world, const Position &start, const Position &end, const int dist_tolerance, const int min_end_dist, const int min_end_dist_xz, const bool allow_jump, const bool take_damage, const float step_height=0.6f)
Not actually a task.
Status GoToDoubleBlackboard(BehaviourClient &client)
Same thing as GoToDouble, but reads its parameters from the blackboard.
static const std::string botcraft_pathfinding_speed_key
Status GoToBlackboard(BehaviourClient &client)
Same thing as GoTo, but reads its parameters from the blackboard.
Status StopFlying(BehaviourClient &client)
Make the current player not fly (as in creative/spectator mode, NOT WITH ELYTRA)
Status LookAtBlackboard(BehaviourClient &client)
Same thing as LookAt, but reads its parameters from the blackboard.
size_t operator()(const std::pair< Position, float > &p) const
double SqrDist(const Vector3 &v) const
Definition Vector3.hpp:215
Vector3< int > ToBlockPos() const
Definition Vector3.hpp:305