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

Function waterPathfind

src/OpenLoco/src/Vehicles/VehicleHead.cpp:3807–3878  ·  view source on GitHub ↗

0x00427FC9

Source from the content-addressed store, hash-verified

3805
3806 // 0x00427FC9
3807 static WaterPathingResult waterPathfind(const VehicleHead& head)
3808 {
3809 const auto nearbyVehicles = findNearbyTilesWithBoats(head.position);
3810
3811 auto orders = head.getCurrentOrders();
3812 auto curOrder = orders.begin();
3813 auto* stationOrder = curOrder->as<OrderStation>();
3814 auto* routeOrder = curOrder->as<OrderRouteWaypoint>();
3815
3816 std::optional<WaterPathingResult> dockRes = std::nullopt;
3817 // 0x00525BC6
3818 World::TilePos2 targetOrderPos{};
3819 if (routeOrder != nullptr)
3820 {
3821 targetOrderPos = toTileSpace(routeOrder->getWaypoint());
3822 }
3823 else if (stationOrder != nullptr)
3824 {
3825 auto targetStationId = stationOrder->getStation();
3826 dockRes = getDockTargetFromStation(targetStationId);
3827 if (dockRes.has_value())
3828 {
3829 targetOrderPos = toTileSpace(dockRes->headTarget);
3830 }
3831 else
3832 {
3833 auto* station = StationManager::get(targetStationId);
3834 targetOrderPos = toTileSpace(Pos2{ station->x, station->y });
3835 }
3836 }
3837 else
3838 {
3839 targetOrderPos = toTileSpace(head.position);
3840 }
3841
3842 Vehicle train(head);
3843 const auto& veh2 = *train.veh2;
3844 // 0x00525BE3
3845 const uint8_t curRotation = ((veh2.spriteYaw + 7) >> 4) & 3;
3846
3847 const auto initialTile = toTileSpace(head.position);
3848 const auto waterMicroZ = veh2.position.z / World::kMicroZStep;
3849
3850 PathFindingResult bestResult{ std::numeric_limits<uint16_t>::max(), std::numeric_limits<uint8_t>::max() };
3851 uint8_t bestResultDirection = 0xFFU;
3852 for (auto i = 0U; i < 4; ++i)
3853 {
3854 const auto tilePos = initialTile + toTileSpace(kRotationOffset[i]);
3855 PathFindingResult initResult{ std::numeric_limits<uint16_t>::max(), std::numeric_limits<uint8_t>::max() };
3856 const auto pathResult = waterPathfindToTarget(tilePos, waterMicroZ, targetOrderPos, nearbyVehicles, 0, initResult);
3857 if (pathResult != initResult && (pathResult < bestResult || (pathResult == bestResult && i == curRotation)))
3858 {
3859 bestResult = pathResult;
3860 bestResultDirection = i;
3861 }
3862 }
3863
3864 if (bestResultDirection == 0xFF)

Callers 1

updateWaterMotionMethod · 0.85

Calls 12

findNearbyTilesWithBoatsFunction · 0.85
toTileSpaceFunction · 0.85
getDockTargetFromStationFunction · 0.85
waterPathfindToTargetFunction · 0.85
WaterPathingResultClass · 0.85
toWorldSpaceFunction · 0.85
getCurrentOrdersMethod · 0.80
getWaypointMethod · 0.80
getStationMethod · 0.80
valueMethod · 0.80
getFunction · 0.50
beginMethod · 0.45

Tested by

no test coverage detected