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

Function flipCar

src/OpenLoco/src/Vehicles/Vehicle.cpp:777–853  ·  view source on GitHub ↗

0x004AFFF3 esi: frontBogie returns new front bogie as esi

Source from the content-addressed store, hash-verified

775 // esi: frontBogie
776 // returns new front bogie as esi
777 VehicleBogie* flipCar(VehicleBogie& frontBogie)
778 {
779 Vehicle train(frontBogie.head);
780 auto precedingVehicleComponent = frontBogie.previousVehicleComponent();
781 CarComponent oldFirstComponent;
782 sfl::static_vector<CarComponent, VehicleObject::kMaxCarComponents> components;
783
784 for (auto& car : train.cars)
785 {
786 if (car.front->id != frontBogie.id)
787 {
788 continue;
789 }
790 oldFirstComponent = car;
791 for (CarComponent& component : car)
792 {
793 std::swap(component.front->objectSpriteType, component.back->objectSpriteType);
794 component.body->var_38 ^= Flags38::isReversed;
795 components.push_back(component);
796 }
797 break;
798 }
799
800 // if the Car is only one CarComponent we don't have to swap any values
801 if (components.size() == 1)
802 {
803 return &frontBogie;
804 }
805 CarComponent& newFirstComponent = components.back();
806 newFirstComponent.body->setSubType(VehicleEntityType::body_start);
807 precedingVehicleComponent->setNextCar(newFirstComponent.front->id);
808 // set the new last component to point to the next car
809
810 if (oldFirstComponent.body == nullptr)
811 {
812 throw Exception::RuntimeError("oldFirstComponent.body was nullptr");
813 }
814 oldFirstComponent.body->setNextCar(newFirstComponent.body->nextCarId);
815
816 for (auto i = static_cast<int32_t>(components.size()) - 2; i >= 0; i--)
817 {
818 components[i].body->setSubType(VehicleEntityType::body_continued);
819 if (components[i + 1].body != nullptr)
820 {
821 components[i + 1].body->setNextCar(components[i].front->id);
822 }
823 }
824
825 newFirstComponent.body->primaryCargo = oldFirstComponent.body->primaryCargo;
826 newFirstComponent.body->breakdownFlags = oldFirstComponent.body->breakdownFlags;
827 newFirstComponent.body->breakdownTimeout = oldFirstComponent.body->breakdownTimeout;
828 newFirstComponent.front->secondaryCargo = oldFirstComponent.front->secondaryCargo;
829 newFirstComponent.front->breakdownFlags = oldFirstComponent.front->breakdownFlags;
830 newFirstComponent.front->breakdownTimeout = oldFirstComponent.front->breakdownTimeout;
831 newFirstComponent.front->totalCarWeight = oldFirstComponent.front->totalCarWeight;
832 newFirstComponent.front->reliability = oldFirstComponent.front->reliability;
833 newFirstComponent.front->timeoutToBreakdown = oldFirstComponent.front->timeoutToBreakdown;
834

Callers 2

autoLayoutTrainMethod · 0.85
sub_4ADB47Method · 0.85

Calls 5

push_backMethod · 0.80
setNextCarMethod · 0.80
sizeMethod · 0.45
setSubTypeMethod · 0.45

Tested by

no test coverage detected