Skip to content

Commit

Permalink
Merge pull request #1703 from CitiesSkylinesMods/bugfix/1701-cargo-tr…
Browse files Browse the repository at this point in the history
…uck-transfer-mixed-path-issues

Cargo truck transfer pathfinding fixes
  • Loading branch information
krzychu124 committed Dec 13, 2022
2 parents 0a6caa0 + 5ed3f7c commit 38f2e55
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
37 changes: 35 additions & 2 deletions TLM/TLM/Custom/PathFinding/CustomPathFind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ private struct BufferItem {

// custom fields
private PathUnitQueueItem queueItem_;
/// <summary>
/// Contains allowed extVehicle types, in normal conditions just QueueIten.vehicleType,
/// when path may include CargoVehicle lanes then it contains also respective cargo vehicle types
/// </summary>
private ExtVehicleType allowedPathVehicleTypes_;
private bool isHeavyVehicle_;

private bool debugLog_; // this will be false in non-Debug
Expand Down Expand Up @@ -289,8 +294,15 @@ private struct BufferItem {
disableMask_ |= NetSegment.Flags.Flooded;
}

allowedPathVehicleTypes_ = queueItem_.vehicleType;
if ((laneTypes_ & NetInfo.LaneType.Vehicle) != NetInfo.LaneType.None) {
laneTypes_ |= NetInfo.LaneType.TransportVehicle;

// allow mixed vehicle path when requested path is Vehicle + CargoVehicle (e.g.: CargoTruck and PostVanAI)
bool allowMixedCargoPath = (laneTypes_ & NetInfo.LaneType.CargoVehicle) != NetInfo.LaneType.None;
if (allowMixedCargoPath) {
allowedPathVehicleTypes_ |= VehicleTypesToMixedCargoExtVehicle(vehicleTypes_);
}
}

#if ROUTING
Expand Down Expand Up @@ -4088,15 +4100,36 @@ private struct BufferItem {
return true;
}

ExtVehicleType allowedTypes = vehicleRestrictionsManager.GetAllowedVehicleTypes(
ExtVehicleType allowedLaneVehicleTypes = vehicleRestrictionsManager.GetAllowedVehicleTypes(
segmentId,
segmentInfo,
(uint)laneIndex,
laneInfo,
VehicleRestrictionsMode.Configured);
return (allowedLaneVehicleTypes & allowedPathVehicleTypes_) != ExtVehicleType.None;
}

/// <summary>
/// Maps all allowed vehicle types to mixed ExtVehicleType
/// Later used to correctly test lane for match with configured allowed vehicle types
/// </summary>
/// <param name="vehicleTypes">Allowed additional vehicle types</param>
/// <returns>Combined ExtVehicleType of all alowed cargo vehicle lane types</returns>
private ExtVehicleType VehicleTypesToMixedCargoExtVehicle(VehicleInfo.VehicleType vehicleTypes) {
ExtVehicleType extVehicleType = ExtVehicleType.None;
if ((vehicleTypes & VehicleInfo.VehicleType.Train) != VehicleInfo.VehicleType.None) {
extVehicleType |= ExtVehicleType.CargoTrain;
}
if ((vehicleTypes & VehicleInfo.VehicleType.Plane) != VehicleInfo.VehicleType.None) {
extVehicleType |= ExtVehicleType.CargoPlane;
}
if ((vehicleTypes & VehicleInfo.VehicleType.Ship) != VehicleInfo.VehicleType.None) {
extVehicleType |= ExtVehicleType.CargoShip;
}

return (allowedTypes & queueItem_.vehicleType) != ExtVehicleType.None;
return extVehicleType;
}

#endif

#if ADVANCEDAI && ROUTING
Expand Down
8 changes: 4 additions & 4 deletions TLM/TLM/Manager/Impl/RoutingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RoutingManager
private RoutingManager() { }

public const NetInfo.LaneType ROUTED_LANE_TYPES =
NetInfo.LaneType.Vehicle | NetInfo.LaneType.TransportVehicle;
NetInfo.LaneType.Vehicle | NetInfo.LaneType.TransportVehicle | NetInfo.LaneType.CargoVehicle;

public const VehicleInfo.VehicleType ROUTED_VEHICLE_TYPES =
VehicleInfo.VehicleType.Car | VehicleInfo.VehicleType.Metro |
Expand Down Expand Up @@ -558,7 +558,7 @@ public class RoutingManager
bool onHighway = Options.highwayRules && onOnewayHighway;
bool applyHighwayRules = onHighway && nodeIsSimpleJunction;
bool applyHighwayRulesAtJunction = applyHighwayRules && nodeIsRealJunction;
bool iterateViaGeometry = (applyHighwayRulesAtJunction || applyHighwayMergingRules) && prevLaneInfo.MatchesRoad();
bool iterateViaGeometry = (applyHighwayRulesAtJunction || applyHighwayMergingRules) && prevLaneInfo.MatchesRoutedRoad();

// start with u-turns at highway junctions
ushort nextSegmentId = iterateViaGeometry ? prevSegmentId : (ushort)0;
Expand Down Expand Up @@ -759,7 +759,7 @@ public class RoutingManager
}
}

if (nextLaneInfo.MatchesRoad() && prevLaneInfo.MatchesRoad()) {
if (nextLaneInfo.MatchesRoutedRoad() && prevLaneInfo.MatchesRoutedRoad()) {
// routing road vehicles (car, SOS, bus, trolleybus, ...)
// lane may be mixed car+tram
++incomingCarLanes;
Expand Down Expand Up @@ -905,7 +905,7 @@ public class RoutingManager
nextLaneInfo = new { nextLaneInfo.m_finalDirection }, nextExpectedDirection });

bool outgoing = (nextLaneInfo.m_finalDirection & NetInfo.InvertDirection(nextExpectedDirection)) != NetInfo.Direction.None;
if (outgoing && nextLaneInfo.MatchesRoad()) {
if (outgoing && nextLaneInfo.MatchesRoutedRoad()) {
++outgoingCarLanes;

extendedLog?.Invoke(new { _ = "increasing number of outgoing lanes at ",
Expand Down
13 changes: 11 additions & 2 deletions TLM/TLM/Util/TrackUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,31 @@ internal static class TrackUtils {
VehicleInfo.VehicleType.Monorail;

internal const NetInfo.LaneType ROAD_LANE_TYPES = LaneArrowManager.LANE_TYPES;
internal const NetInfo.LaneType ROUTED_ROAD_LANE_TYPES = LaneArrowManager.LANE_TYPES | NetInfo.LaneType.CargoVehicle;

internal const VehicleInfo.VehicleType ROAD_VEHICLE_TYPES = LaneArrowManager.VEHICLE_TYPES | VehicleInfo.VehicleType.Trolleybus;

internal static bool MatchesTrack([NotNull] this NetInfo.Lane laneInfo) =>
laneInfo.Matches(TRACK_LANE_TYPES, TRACK_VEHICLE_TYPES);

internal static bool MatchesRoad([NotNull] this NetInfo.Lane laneInfo) =>
laneInfo.Matches(ROAD_LANE_TYPES, ROAD_VEHICLE_TYPES);

/// <summary>
/// Testing lane info if matches with predefined allowed road lane types and vehicle types
/// </summary>
/// <param name="laneInfo">Lane info instance to test</param>
/// <returns>True if matches, otherwise False</returns>
internal static bool MatchesRoutedRoad([NotNull] this NetInfo.Lane laneInfo) =>
laneInfo.Matches(ROUTED_ROAD_LANE_TYPES, ROAD_VEHICLE_TYPES);

internal static bool Matches([NotNull] this NetInfo.Lane laneInfo , NetInfo.LaneType laneType, VehicleInfo.VehicleType vehicleType) {
return (laneType & laneInfo.m_laneType) != 0 && (vehicleType & laneInfo.m_vehicleType) != 0;
}

internal static bool IsTrackOnly(this NetInfo.Lane laneInfo) {
return
laneInfo.MatchesTrack() &&
laneInfo.MatchesTrack() &&
!laneInfo.m_laneType.IsFlagSet(~TRACK_LANE_TYPES) &&
!laneInfo.m_vehicleType.IsFlagSet(~TRACK_VEHICLE_TYPES);
}
Expand Down

0 comments on commit 38f2e55

Please sign in to comment.