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

Function GetOrderDistance

src/order_cmd.cpp:589–609  ·  view source on GitHub ↗

* Get the distance between two orders of a vehicle. Conditional orders are resolved * and the bigger distance of the two order branches is returned. * @param prev Origin order. * @param cur Destination order. * @param v The vehicle to get the distance for. * @param conditional_depth Internal param for resolving conditional orders. * @return Maximum distance between the two orders. */

Source from the content-addressed store, hash-verified

587 * @return Maximum distance between the two orders.
588 */
589uint GetOrderDistance(VehicleOrderID prev, VehicleOrderID cur, const Vehicle *v, int conditional_depth)
590{
591 assert(v->orders != nullptr);
592 const OrderList &orderlist = *v->orders;
593 auto orders = orderlist.GetOrders();
594
595 if (orders[cur].IsType(OT_CONDITIONAL)) {
596 if (conditional_depth > v->GetNumOrders()) return 0;
597
598 conditional_depth++;
599
600 int dist1 = GetOrderDistance(prev, orders[cur].GetConditionSkipToOrder(), v, conditional_depth);
601 int dist2 = GetOrderDistance(prev, orderlist.GetNext(cur), v, conditional_depth);
602 return std::max(dist1, dist2);
603 }
604
605 TileIndex prev_tile = orders[prev].GetLocation(v, true);
606 TileIndex cur_tile = orders[cur].GetLocation(v, true);
607 if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
608 return v->type == VEH_AIRCRAFT ? DistanceSquare(prev_tile, cur_tile) : DistanceManhattan(prev_tile, cur_tile);
609}
610
611/**
612 * Add an order to the orderlist of a vehicle.

Callers 2

DrawOrderStringFunction · 0.85

Calls 8

DistanceSquareFunction · 0.85
DistanceManhattanFunction · 0.85
GetOrdersMethod · 0.80
IsTypeMethod · 0.80
GetNextMethod · 0.80
GetNumOrdersMethod · 0.45
GetLocationMethod · 0.45

Tested by

no test coverage detected