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

Method GetDistance

src/cargopacket.h:220–253  ·  view source on GitHub ↗

* Get the current distance the cargo has traveled. * * @param current_tile Current tile of the cargo. * @return uint The distance (in tiles) traveled. */

Source from the content-addressed store, hash-verified

218 * @return uint The distance (in tiles) traveled.
219 */
220 inline uint GetDistance(TileIndex current_tile) const
221 {
222 assert(this->source_xy != INVALID_TILE);
223#ifdef WITH_ASSERT
224 assert(this->in_vehicle);
225#endif /* WITH_ASSERT */
226
227 /* Distance is always requested when the cargo is still inside the
228 * vehicle. So first finish the calculation for travelled to
229 * become a vector. */
230 auto local_travelled = travelled;
231 local_travelled.x -= TileX(current_tile);
232 local_travelled.y -= TileY(current_tile);
233
234 /* Cargo-movement is a vector that indicates how much the cargo has
235 * actually traveled in a vehicle. This is the distance you get paid
236 * for. However, one could construct a route where this vector would
237 * be really long. To not overpay the player, cap out at the distance
238 * between source and destination.
239 *
240 * This way of calculating is to counter people moving cargo for free
241 * and instantly in stations, where you deliver it in one part of the
242 * station and pick it up in another. By using the actual distance
243 * traveled in a vehicle, using this trick doesn't give you more money.
244 *
245 * However, especially in large networks with large transfer station,
246 * etc, one could actually make the route a lot longer. In that case,
247 * use the actual distance between source and destination.
248 */
249
250 uint distance_travelled = abs(local_travelled.x) + abs(local_travelled.y);
251 uint distance_source_dest = DistanceManhattan(this->source_xy, current_tile);
252 return std::min(distance_travelled, distance_source_dest);
253 }
254
255 /**
256 * Gets the ID of station the cargo wants to go next.

Callers 2

PayFinalDeliveryMethod · 0.45
PayTransferMethod · 0.45

Calls 4

TileXFunction · 0.85
TileYFunction · 0.85
absFunction · 0.85
DistanceManhattanFunction · 0.85

Tested by

no test coverage detected