MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / autoLayoutTrain

Method autoLayoutTrain

src/OpenLoco/src/Vehicles/VehicleHead.cpp:545–730  ·  view source on GitHub ↗

0x004AF7A4

Source from the content-addressed store, hash-verified

543
544 // 0x004AF7A4
545 void VehicleHead::autoLayoutTrain()
546 {
547 Vehicle train(*this);
548 if (train.cars.empty())
549 {
550 return;
551 }
552
553 // This function has been modified from vanilla but it should
554 // produce the same results. Vanilla version performed a lot
555 // of flipping and inserting to reorder the cars. This version
556 // does the final flipping/inserting in one go at the end.
557
558 // Pretty safe to assume this as its hard to exceed 30
559 // you can do about ~200 if you use some tricks
560 assert(train.cars.size() < 100);
561
562 sfl::static_vector<CarMetaData, 100> carData;
563
564 for (auto& car : train.cars)
565 {
566 auto* vehicleObj = ObjectManager::get<VehicleObject>(car.front->objectId);
567 CarMetaData data{};
568 data.frontId = car.front->id;
569 data.flags = vehicleObj->flags;
570 data.power = vehicleObj->power;
571 data.isReversed = car.body->has38Flags(Flags38::isReversed);
572 data.wasReversed = car.body->has38Flags(Flags38::isReversed);
573 carData.push_back(data);
574 }
575 auto pushCarToFront = [&carData](const size_t index) {
576 std::rotate(carData.begin(), carData.begin() + index, carData.begin() + index + 1);
577 };
578 auto pushCarToBack = [&carData](const size_t index) {
579 std::rotate(carData.begin() + index, carData.begin() + index + 1, carData.end());
580 };
581
582 if (!hasVehicleFlags(VehicleFlags::shuntCheat))
583 {
584 // Places first powered car at front of train
585
586 for (auto i = 0U; i < carData.size(); ++i)
587 {
588 auto& cd = carData[i];
589 if (cd.power != 0)
590 {
591 pushCarToFront(i);
592 break;
593 }
594 }
595 }
596
597 {
598 // Alternate forward/backward if VehicleObjectFlags::alternatingDirection set
599 bool curIsReversed = false;
600 for (auto& cd : carData)
601 {
602 if (cd.hasFlags(VehicleObjectFlags::alternatingDirection))

Callers 3

updateWholeVehicleFunction · 0.80
vehicleRearrangeFunction · 0.80
sellVehicleFunction · 0.80

Calls 12

flipCarFunction · 0.85
insertCarBeforeFunction · 0.85
connectJacobsBogiesFunction · 0.85
has38FlagsMethod · 0.80
push_backMethod · 0.80
rotateFunction · 0.50
VehicleClass · 0.50
emptyMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
hasFlagsMethod · 0.45

Tested by

no test coverage detected