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

Method UpdateDeltaXY

src/train_cmd.cpp:1493–1554  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1491}
1492
1493void Train::UpdateDeltaXY()
1494{
1495 /* Set common defaults. */
1496 this->bounds = {{-1, -1, 0}, {3, 3, 6}, {}};
1497
1498 /* Set if flipped and engine is NOT flagged with custom flip handling. */
1499 int flipped = this->flags.Test(VehicleRailFlag::Flipped) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips);
1500 /* If flipped and vehicle length is odd, we need to adjust the bounding box offset slightly. */
1501 int flip_offs = flipped && (this->gcache.cached_veh_length & 1);
1502
1503 Direction dir = this->direction;
1504 if (flipped) dir = ReverseDir(dir);
1505
1506 if (!IsDiagonalDirection(dir)) {
1507 static const Point _sign_table[] = {
1508 /* x, y */
1509 {-1, -1}, // DIR_N
1510 {-1, 1}, // DIR_E
1511 { 1, 1}, // DIR_S
1512 { 1, -1}, // DIR_W
1513 };
1514
1515 int half_shorten = (VEHICLE_LENGTH - this->gcache.cached_veh_length + flipped) / 2;
1516
1517 /* For all straight directions, move the bound box to the centre of the vehicle, but keep the size. */
1518 this->bounds.offset.x -= half_shorten * _sign_table[DirToDiagDir(dir)].x;
1519 this->bounds.offset.y -= half_shorten * _sign_table[DirToDiagDir(dir)].y;
1520 } else {
1521 switch (dir) {
1522 /* Shorten southern corner of the bounding box according the vehicle length
1523 * and center the bounding box on the vehicle. */
1524 case DIR_NE:
1525 this->bounds.origin.x = -(this->gcache.cached_veh_length + 1) / 2 + flip_offs;
1526 this->bounds.extent.x = this->gcache.cached_veh_length;
1527 this->bounds.offset.x = 1;
1528 break;
1529
1530 case DIR_NW:
1531 this->bounds.origin.y = -(this->gcache.cached_veh_length + 1) / 2 + flip_offs;
1532 this->bounds.extent.y = this->gcache.cached_veh_length;
1533 this->bounds.offset.y = 1;
1534 break;
1535
1536 /* Move northern corner of the bounding box down according to vehicle length
1537 * and center the bounding box on the vehicle. */
1538 case DIR_SW:
1539 this->bounds.origin.x = -(this->gcache.cached_veh_length) / 2 - flip_offs;
1540 this->bounds.extent.x = this->gcache.cached_veh_length;
1541 this->bounds.offset.x = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
1542 break;
1543
1544 case DIR_SE:
1545 this->bounds.origin.y = -(this->gcache.cached_veh_length) / 2 - flip_offs;
1546 this->bounds.extent.y = this->gcache.cached_veh_length;
1547 this->bounds.offset.y = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
1548 break;
1549
1550 default:

Callers 1

TrainControllerFunction · 0.45

Calls 6

EngInfoFunction · 0.85
ReverseDirFunction · 0.85
IsDiagonalDirectionFunction · 0.85
DirToDiagDirFunction · 0.85
NOT_REACHEDFunction · 0.85
TestMethod · 0.80

Tested by

no test coverage detected