public void CustomSimulationStep(ushort vehicleId,
                                         ref Vehicle vehicleData,
                                         Vector3 physicsLodRefPos)
        {
#if DEBUG
            bool vehDebug = DebugSettings.VehicleId == 0 ||
                            DebugSettings.VehicleId == vehicleId;
            bool logParkingAi = DebugSwitch.BasicParkingAILog.Get() && vehDebug;
#else
            var logParkingAi = false;
#endif

            if ((vehicleData.m_flags & Vehicle.Flags.WaitingPath) != 0)
            {
                PathManager pathManager   = Singleton <PathManager> .instance;
                byte        pathFindFlags = pathManager.m_pathUnits.m_buffer[vehicleData.m_path].m_pathFindFlags;

                // NON-STOCK CODE START
                ExtPathState mainPathState = ExtPathState.Calculating;
                if ((pathFindFlags & PathUnit.FLAG_FAILED) != 0 || vehicleData.m_path == 0)
                {
                    mainPathState = ExtPathState.Failed;
                }
                else if ((pathFindFlags & PathUnit.FLAG_READY) != 0)
                {
                    mainPathState = ExtPathState.Ready;
                }

#if DEBUG
                uint logVehiclePath = vehicleData.m_path;
                Log._DebugIf(
                    logParkingAi,
                    () => $"CustomCarAI.CustomSimulationStep({vehicleId}): " +
                    $"Path: {logVehiclePath}, mainPathState={mainPathState}");
#endif

                IExtVehicleManager extVehicleManager = Constants.ManagerFactory.ExtVehicleManager;
                ExtSoftPathState   finalPathState    = ExtCitizenInstance.ConvertPathStateToSoftPathState(mainPathState);
                if (Options.parkingAI &&
                    extVehicleManager.ExtVehicles[vehicleId].vehicleType == ExtVehicleType.PassengerCar)
                {
                    ushort driverInstanceId = extVehicleManager.GetDriverInstanceId(vehicleId, ref vehicleData);
                    finalPathState = AdvancedParkingManager.Instance.UpdateCarPathState(
                        vehicleId,
                        ref vehicleData,
                        ref Singleton <CitizenManager> .instance.m_instances.m_buffer[driverInstanceId],
                        ref ExtCitizenInstanceManager.Instance.ExtInstances[driverInstanceId],
                        mainPathState);

#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug($"CustomCarAI.CustomSimulationStep({vehicleId}): " +
                                   $"Applied Parking AI logic. Path: {vehicleData.m_path}, " +
                                   $"mainPathState={mainPathState}, finalPathState={finalPathState}");
                    }
#endif
                }

                switch (finalPathState)
                {
                case ExtSoftPathState.Ready: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleId}): Path-finding " +
                            $"succeeded for vehicle {vehicleId} (finalPathState={finalPathState}). " +
                            $"Path: {vehicleData.m_path} -- calling CarAI.PathfindSuccess");
                    }
#endif

                    vehicleData.m_pathPositionIndex = 255;
                    vehicleData.m_flags            &= ~Vehicle.Flags.WaitingPath;
                    vehicleData.m_flags            &= ~Vehicle.Flags.Arriving;
                    PathfindSuccess(vehicleId, ref vehicleData);
                    TrySpawn(vehicleId, ref vehicleData);
                    break;
                }

                case ExtSoftPathState.Ignore: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleId}): Path-finding " +
                            $"result shall be ignored for vehicle {vehicleId} " +
                            $"(finalPathState={finalPathState}). Path: {vehicleData.m_path} -- ignoring");
                    }
#endif
                    return;
                }

                case ExtSoftPathState.Calculating:
                default: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleId}): Path-finding " +
                            $"result undetermined for vehicle {vehicleId} (finalPathState={finalPathState}). " +
                            $"Path: {vehicleData.m_path} -- continue");
                    }
#endif
                    break;
                }

                case ExtSoftPathState.FailedHard: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleId}): HARD path-finding " +
                            $"failure for vehicle {vehicleId} (finalPathState={finalPathState}). " +
                            $"Path: {vehicleData.m_path} -- calling CarAI.PathfindFailure");
                    }
#endif
                    vehicleData.m_flags &= ~Vehicle.Flags.WaitingPath;
                    Singleton <PathManager> .instance.ReleasePath(vehicleData.m_path);

                    vehicleData.m_path = 0u;
                    PathfindFailure(vehicleId, ref vehicleData);
                    return;
                }

                case ExtSoftPathState.FailedSoft: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleId}): SOFT path-finding " +
                            $"failure for vehicle {vehicleId} (finalPathState={finalPathState}). " +
                            $"Path: {vehicleData.m_path} -- calling CarAI.InvalidPath");
                    }
#endif

                    // path mode has been updated, repeat path-finding
                    vehicleData.m_flags &= ~Vehicle.Flags.WaitingPath;
                    InvalidPath(vehicleId, ref vehicleData, vehicleId, ref vehicleData);
                    break;
                }
                }

                // NON-STOCK CODE END
            }
            else
            {
                if ((vehicleData.m_flags & Vehicle.Flags.WaitingSpace) != 0)
                {
                    TrySpawn(vehicleId, ref vehicleData);
                }
            }

            // NON-STOCK CODE START
            IExtVehicleManager extVehicleMan = Constants.ManagerFactory.ExtVehicleManager;
            extVehicleMan.UpdateVehiclePosition(vehicleId, ref vehicleData);

            if (Options.advancedAI &&
                (vehicleData.m_flags & Vehicle.Flags.Spawned) != 0)
            {
                extVehicleMan.LogTraffic(vehicleId, ref vehicleData);
            }

            // NON-STOCK CODE END
            Vector3 lastFramePosition = vehicleData.GetLastFramePosition();
            int     lodPhysics;
            if (Vector3.SqrMagnitude(physicsLodRefPos - lastFramePosition) >= 1100f * 1100f)
            {
                lodPhysics = 2;
            }
            else if (Vector3.SqrMagnitude(
                         Singleton <SimulationManager> .instance.m_simulationView.m_position -
                         lastFramePosition) >= 500f * 500f)
            {
                lodPhysics = 1;
            }
            else
            {
                lodPhysics = 0;
            }

            SimulationStep(vehicleId, ref vehicleData, vehicleId, ref vehicleData, lodPhysics);
            if (vehicleData.m_leadingVehicle == 0 && vehicleData.m_trailingVehicle != 0)
            {
                VehicleManager vehManager = Singleton <VehicleManager> .instance;
                ushort         trailerId  = vehicleData.m_trailingVehicle;
                int            numIters   = 0;
                while (trailerId != 0)
                {
                    ushort      trailingVehicle = vehManager.m_vehicles.m_buffer[trailerId].m_trailingVehicle;
                    VehicleInfo info            = vehManager.m_vehicles.m_buffer[trailerId].Info;

                    info.m_vehicleAI.SimulationStep(
                        trailerId,
                        ref vehManager.m_vehicles.m_buffer[trailerId],
                        vehicleId,
                        ref vehicleData,
                        lodPhysics);

                    trailerId = trailingVehicle;
                    if (++numIters > 16384)
                    {
                        CODebugBase <LogChannel> .Error(
                            LogChannel.Core,
                            $"Invalid list detected!\n{Environment.StackTrace}");

                        break;
                    }
                }
            }

            int privateServiceIndex = ItemClass.GetPrivateServiceIndex(m_info.m_class.m_service);
            int maxBlockCounter     = (privateServiceIndex == -1) ? 150 : 100;

            if ((vehicleData.m_flags & (Vehicle.Flags.Spawned
                                        | Vehicle.Flags.WaitingPath
                                        | Vehicle.Flags.WaitingSpace)) == 0 &&
                vehicleData.m_cargoParent == 0)
            {
                Singleton <VehicleManager> .instance.ReleaseVehicle(vehicleId);
            }
            else if (vehicleData.m_blockCounter >= maxBlockCounter)
            {
                // NON-STOCK CODE START
                if (VehicleBehaviorManager.Instance.MayDespawn(ref vehicleData))
                {
                    // NON-STOCK CODE END
                    Singleton <VehicleManager> .instance.ReleaseVehicle(vehicleId);
                } // NON-STOCK CODE
            }
        }
        public static bool Prefix(CarAI __instance,
                                  ushort vehicleID,
                                  ref Vehicle data,
                                  Vector3 physicsLodRefPos)
        {
            #if DEBUG
            bool vehDebug = DebugSettings.VehicleId == 0 ||
                            DebugSettings.VehicleId == vehicleID;
            bool logParkingAi = DebugSwitch.BasicParkingAILog.Get() && vehDebug;
#else
            var logParkingAi = false;
#endif

            if ((data.m_flags & Vehicle.Flags.WaitingPath) != 0)
            {
                PathManager pathManager   = Singleton <PathManager> .instance;
                byte        pathFindFlags = pathManager.m_pathUnits.m_buffer[data.m_path].m_pathFindFlags;

                // NON-STOCK CODE START
                ExtPathState mainPathState = ExtPathState.Calculating;
                if ((pathFindFlags & PathUnit.FLAG_FAILED) != 0 || data.m_path == 0)
                {
                    mainPathState = ExtPathState.Failed;
                }
                else if ((pathFindFlags & PathUnit.FLAG_READY) != 0)
                {
                    mainPathState = ExtPathState.Ready;
                }

#if DEBUG
                uint logVehiclePath = data.m_path;
                Log._DebugIf(
                    logParkingAi,
                    () => $"CustomCarAI.CustomSimulationStep({vehicleID}): " +
                    $"Path: {logVehiclePath}, mainPathState={mainPathState}");
#endif

                IExtVehicleManager extVehicleManager = Constants.ManagerFactory.ExtVehicleManager;
                ExtSoftPathState   finalPathState    = ExtCitizenInstance.ConvertPathStateToSoftPathState(mainPathState);
                if (Options.parkingAI &&
                    extVehicleManager.ExtVehicles[vehicleID].vehicleType == ExtVehicleType.PassengerCar)
                {
                    ushort driverInstanceId = extVehicleManager.GetDriverInstanceId(vehicleID, ref data);
                    finalPathState = AdvancedParkingManager.Instance.UpdateCarPathState(
                        vehicleID,
                        ref data,
                        ref driverInstanceId.ToCitizenInstance(),
                        ref ExtCitizenInstanceManager.Instance.ExtInstances[driverInstanceId],
                        mainPathState);

#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug($"CustomCarAI.CustomSimulationStep({vehicleID}): " +
                                   $"Applied Parking AI logic. Path: {data.m_path}, " +
                                   $"mainPathState={mainPathState}, finalPathState={finalPathState}");
                    }
#endif
                }

                switch (finalPathState)
                {
                case ExtSoftPathState.Ready: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleID}): Path-finding " +
                            $"succeeded for vehicle {vehicleID} (finalPathState={finalPathState}). " +
                            $"Path: {data.m_path} -- calling CarAI.PathfindSuccess");
                    }
#endif

                    data.m_pathPositionIndex = 255;
                    data.m_flags            &= ~Vehicle.Flags.WaitingPath;
                    data.m_flags            &= ~Vehicle.Flags.Arriving;
                    GameConnectionManager.Instance.VehicleAIConnection.PathfindSuccess(__instance, vehicleID, ref data);
                    __instance.TrySpawn(vehicleID, ref data);
                    break;
                }

                case ExtSoftPathState.Ignore: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleID}): Path-finding " +
                            $"result shall be ignored for vehicle {vehicleID} " +
                            $"(finalPathState={finalPathState}). Path: {data.m_path} -- ignoring");
                    }
#endif
                    return(false);
                }

                case ExtSoftPathState.Calculating:
                default: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleID}): Path-finding " +
                            $"result undetermined for vehicle {vehicleID} (finalPathState={finalPathState}). " +
                            $"Path: {data.m_path} -- continue");
                    }
#endif
                    break;
                }

                case ExtSoftPathState.FailedHard: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleID}): HARD path-finding " +
                            $"failure for vehicle {vehicleID} (finalPathState={finalPathState}). " +
                            $"Path: {data.m_path} -- calling CarAI.PathfindFailure");
                    }
#endif
                    data.m_flags &= ~Vehicle.Flags.WaitingPath;
                    Singleton <PathManager> .instance.ReleasePath(data.m_path);

                    data.m_path = 0u;
                    GameConnectionManager.Instance.VehicleAIConnection.PathfindFailure(__instance, vehicleID, ref data);
                    return(false);
                }

                case ExtSoftPathState.FailedSoft: {
#if DEBUG
                    if (logParkingAi)
                    {
                        Log._Debug(
                            $"CustomCarAI.CustomSimulationStep({vehicleID}): SOFT path-finding " +
                            $"failure for vehicle {vehicleID} (finalPathState={finalPathState}). " +
                            $"Path: {data.m_path} -- calling CarAI.InvalidPath");
                    }
#endif

                    // path mode has been updated, repeat path-finding
                    data.m_flags &= ~Vehicle.Flags.WaitingPath;
                    GameConnectionManager.Instance.VehicleAIConnection.InvalidPath(__instance, vehicleID, ref data, vehicleID, ref data);
                    break;
                }
                }

                // NON-STOCK CODE END
            }
            else
            {
                if ((data.m_flags & Vehicle.Flags.WaitingSpace) != 0)
                {
                    __instance.TrySpawn(vehicleID, ref data);
                }
            }

            // NON-STOCK CODE START
            IExtVehicleManager extVehicleMan = Constants.ManagerFactory.ExtVehicleManager;
            extVehicleMan.UpdateVehiclePosition(vehicleID, ref data);

            if (Options.advancedAI &&
                (data.m_flags & Vehicle.Flags.Spawned) != 0)
            {
                extVehicleMan.LogTraffic(vehicleID, ref data);
            }

            // NON-STOCK CODE END
            Vector3 lastFramePosition = data.GetLastFramePosition();
            int     lodPhysics;
            if (Vector3.SqrMagnitude(physicsLodRefPos - lastFramePosition) >= 1100f * 1100f)
            {
                lodPhysics = 2;
            }
            else if (Vector3.SqrMagnitude(
                         Singleton <SimulationManager> .instance.m_simulationView.m_position -
                         lastFramePosition) >= 500f * 500f)
            {
                lodPhysics = 1;
            }
            else
            {
                lodPhysics = 0;
            }

            __instance.SimulationStep(vehicleID, ref data, vehicleID, ref data, lodPhysics);
            if (data.m_leadingVehicle == 0 && data.m_trailingVehicle != 0)
            {
                ushort trailerId = data.m_trailingVehicle;
                int    numIters  = 0;
                while (trailerId != 0)
                {
                    ref Vehicle trailer = ref trailerId.ToVehicle();

                    trailer.Info.m_vehicleAI.SimulationStep(
                        trailerId,
                        ref trailer,
                        vehicleID,
                        ref data,
                        lodPhysics);

                    trailerId = trailer.m_trailingVehicle;
                    if (++numIters > 16384)
                    {
                        CODebugBase <LogChannel> .Error(
                            LogChannel.Core,
                            $"Invalid list detected!\n{Environment.StackTrace}");

                        break;
                    }
                }
            }