MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / FindClosestBlockingRoadVeh

Function FindClosestBlockingRoadVeh

src/roadveh_cmd.cpp:627–654  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

625};
626
627static void FindClosestBlockingRoadVeh(Vehicle *v, RoadVehFindData *rvf)
628{
629 static const int8_t dist_x[] = { -4, -8, -4, -1, 4, 8, 4, 1 };
630 static const int8_t dist_y[] = { -4, -1, 4, 8, 4, 1, -4, -8 };
631
632 int x_diff = v->x_pos - rvf->x;
633 int y_diff = v->y_pos - rvf->y;
634
635 /* Not a close Road vehicle when it's not a road vehicle, in the depot, or ourself. */
636 if (v->type != VEH_ROAD || v->IsInDepot() || rvf->veh->First() == v->First()) return;
637
638 /* Not close when at a different height or when going in a different direction. */
639 if (abs(v->z_pos - rvf->veh->z_pos) >= 6 || v->direction != rvf->dir) return;
640
641 /* We 'return' the closest vehicle, in distance and then VehicleID as tie-breaker. */
642 uint diff = abs(x_diff) + abs(y_diff);
643 if (diff > rvf->best_diff || (diff == rvf->best_diff && v->index > rvf->best->index)) return;
644
645 auto IsCloseOnAxis = [](int dist, int diff) {
646 if (dist < 0) return diff > dist && diff <= 0;
647 return diff < dist && diff >= 0;
648 };
649
650 if (IsCloseOnAxis(dist_x[v->direction], x_diff) && IsCloseOnAxis(dist_y[v->direction], y_diff)) {
651 rvf->best = v;
652 rvf->best_diff = diff;
653 }
654}
655
656static RoadVehicle *RoadVehFindCloseTo(RoadVehicle *v, int x, int y, Direction dir, bool update_blocked_ctr = true)
657{

Callers 1

RoadVehFindCloseToFunction · 0.85

Calls 3

absFunction · 0.85
IsInDepotMethod · 0.45
FirstMethod · 0.45

Tested by

no test coverage detected