Exemplo n.º 1
0
 public static void SimulationStep(CarAI carAI, ushort vehicleID, ref Vehicle vehicleData, ref Vehicle.Frame frameData, ushort leaderID, ref Vehicle leaderData, int lodPhysics)
 {
     uint currentFrameIndex = Singleton<SimulationManager>.instance.m_currentFrameIndex;
     frameData.m_position += frameData.m_velocity * 0.5f;
     frameData.m_swayPosition += frameData.m_swayVelocity * 0.5f;
     float acceleration = carAI.m_info.m_acceleration;
     float braking = carAI.m_info.m_braking;
     float magnitude = frameData.m_velocity.magnitude;
     Vector3 vector = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
     float sqrMagnitude = vector.sqrMagnitude;
     float num = (magnitude + acceleration) * (0.5f + 0.5f * (magnitude + acceleration) / braking) + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
     float num2 = Mathf.Max(magnitude + acceleration, 5f);
     if (lodPhysics >= 2 && (ulong)(currentFrameIndex >> 4 & 3u) == (ulong)((long)(vehicleID & 3)))
     {
         num2 *= 2f;
     }
     float num3 = Mathf.Max((num - num2) / 3f, 1f);
     float num4 = num2 * num2;
     float num5 = num3 * num3;
     int i = 0;
     bool flag = false;
     if ((sqrMagnitude < num4 || vehicleData.m_targetPos3.w < 0.01f) && (leaderData.m_flags & (Vehicle.Flags.WaitingPath | Vehicle.Flags.Stopped)) == Vehicle.Flags.None)
     {
         if (leaderData.m_path != 0u)
         {
             CustomVehicleAI.UpdatePathTargetPositions(carAI, vehicleID, ref vehicleData, frameData.m_position, ref i, 4, num4, num5);
             if ((leaderData.m_flags & Vehicle.Flags.Spawned) == Vehicle.Flags.None)
             {
                 frameData = vehicleData.m_frame0;
                 return;
             }
         }
         if ((leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
         {
             while (i < 4)
             {
                 float minSqrDistance;
                 Vector3 refPos;
                 if (i == 0)
                 {
                     minSqrDistance = num4;
                     refPos = frameData.m_position;
                     flag = true;
                 }
                 else
                 {
                     minSqrDistance = num5;
                     refPos = vehicleData.GetTargetPos(i - 1);
                 }
                 int num6 = i;
                 carAI.UpdateBuildingTargetPositions(vehicleID, ref vehicleData, refPos, leaderID, ref leaderData, ref i, minSqrDistance);
                 if (i == num6)
                 {
                     break;
                 }
             }
             if (i != 0)
             {
                 Vector4 targetPos = vehicleData.GetTargetPos(i - 1);
                 while (i < 4)
                 {
                     vehicleData.SetTargetPos(i++, targetPos);
                 }
             }
         }
         vector = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
         sqrMagnitude = vector.sqrMagnitude;
     }
     if (leaderData.m_path != 0u && (leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
     {
         NetManager instance = Singleton<NetManager>.instance;
         byte b = leaderData.m_pathPositionIndex;
         byte lastPathOffset = leaderData.m_lastPathOffset;
         if (b == 255)
         {
             b = 0;
         }
         float num7 = 1f + leaderData.CalculateTotalLength(leaderID);
         PathManager instance2 = Singleton<PathManager>.instance;
         PathUnit.Position pathPos;
         if (instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetPosition(b >> 1, out pathPos))
         {
             instance.m_segments.m_buffer[(int)pathPos.m_segment].AddTraffic(Mathf.RoundToInt(num7 * 2.5f));
             bool flag2 = false;
             if ((b & 1) == 0 || lastPathOffset == 0)
             {
                 uint laneID = PathManager.GetLaneID(pathPos);
                 if (laneID != 0u)
                 {
                     Vector3 b2 = instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].CalculatePosition((float)pathPos.m_offset * 0.003921569f);
                     float num8 = 0.5f * magnitude * magnitude / carAI.m_info.m_braking + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
                     if (Vector3.Distance(frameData.m_position, b2) >= num8 - 1f)
                     {
                         instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].ReserveSpace(num7);
                         flag2 = true;
                     }
                 }
             }
             if (!flag2 && instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetNextPosition(b >> 1, out pathPos))
             {
                 uint laneID2 = PathManager.GetLaneID(pathPos);
                 if (laneID2 != 0u)
                 {
                     instance.m_lanes.m_buffer[(int)((UIntPtr)laneID2)].ReserveSpace(num7);
                 }
             }
         }
         if ((ulong)(currentFrameIndex >> 4 & 15u) == (ulong)((long)(leaderID & 15)))
         {
             bool flag3 = false;
             uint path = leaderData.m_path;
             int num9 = b >> 1;
             int j = 0;
             while (j < 5)
             {
                 bool flag4;
                 if (PathUnit.GetNextPosition(ref path, ref num9, out pathPos, out flag4))
                 {
                     uint laneID3 = PathManager.GetLaneID(pathPos);
                     if (laneID3 != 0u && !instance.m_lanes.m_buffer[(int)((UIntPtr)laneID3)].CheckSpace(num7))
                     {
                         j++;
                         continue;
                     }
                 }
                 if (flag4)
                 {
                     (carAI as IVehicle).InvalidPath(vehicleID, ref vehicleData, leaderID, ref leaderData);
                 }
                 flag3 = true;
                 break;
             }
             if (!flag3)
             {
                 leaderData.m_flags |= Vehicle.Flags.Congestion;
             }
         }
     }
     float num10;
     if ((leaderData.m_flags & Vehicle.Flags.Stopped) != Vehicle.Flags.None)
     {
         num10 = 0f;
     }
     else
     {
         num10 = vehicleData.m_targetPos0.w;
     }
     Quaternion rotation = Quaternion.Inverse(frameData.m_rotation);
     vector = rotation * vector;
     Vector3 vector2 = rotation * frameData.m_velocity;
     Vector3 a = Vector3.forward;
     Vector3 vector3 = Vector3.zero;
     Vector3 zero = Vector3.zero;
     float num11 = 0f;
     float num12 = 0f;
     bool flag5 = false;
     float num13 = 0f;
     if (sqrMagnitude > 1f)
     {
         a = VectorUtils.NormalizeXZ(vector, out num13);
         if (num13 > 1f)
         {
             Vector3 vector4 = vector;
             num2 = Mathf.Max(magnitude, 2f);
             num4 = num2 * num2;
             if (sqrMagnitude > num4)
             {
                 vector4 *= num2 / Mathf.Sqrt(sqrMagnitude);
             }
             bool flag6 = false;
             if (vector4.z < Mathf.Abs(vector4.x))
             {
                 if (vector4.z < 0f)
                 {
                     flag6 = true;
                 }
                 float num14 = Mathf.Abs(vector4.x);
                 if (num14 < 1f)
                 {
                     vector4.x = Mathf.Sign(vector4.x);
                     if (vector4.x == 0f)
                     {
                         vector4.x = 1f;
                     }
                     num14 = 1f;
                 }
                 vector4.z = num14;
             }
             float b3;
             a = VectorUtils.NormalizeXZ(vector4, out b3);
             num13 = Mathf.Min(num13, b3);
             float num15 = 1.57079637f * (1f - a.z);
             if (num13 > 1f)
             {
                 num15 /= num13;
             }
             float num16 = num13;
             if (vehicleData.m_targetPos0.w < 0.1f)
             {
                 num10 = (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15);
                 num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, Mathf.Min(vehicleData.m_targetPos0.w, vehicleData.m_targetPos1.w), braking * 0.9f));
             }
             else
             {
                 num10 = Mathf.Min(num10, (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15));
                 num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos1.w, braking * 0.9f));
             }
             num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos1 - vehicleData.m_targetPos0);
             num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos2.w, braking * 0.9f));
             num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos2 - vehicleData.m_targetPos1);
             num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos3.w, braking * 0.9f));
             num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos3 - vehicleData.m_targetPos2);
             if (vehicleData.m_targetPos3.w < 0.01f)
             {
                 num16 = Mathf.Max(0f, num16 - carAI.m_info.m_generatedInfo.m_size.z * 0.5f);
             }
             num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, 0f, braking * 0.9f));
             if (!DisableCollisionCheck(leaderID, ref leaderData))
             {
                 CustomCarAI.CheckOtherVehicles(carAI, vehicleID, ref vehicleData, ref frameData, ref num10, ref flag5, ref zero, num, braking * 0.9f, lodPhysics);
             }
             if (flag6)
             {
                 num10 = -num10;
             }
             if (num10 < magnitude)
             {
                 float num17 = Mathf.Max(acceleration, Mathf.Min(braking, magnitude));
                 num11 = Mathf.Max(num10, magnitude - num17);
             }
             else
             {
                 float num18 = Mathf.Max(acceleration, Mathf.Min(braking, -magnitude));
                 num11 = Mathf.Min(num10, magnitude + num18);
             }
         }
     }
     else
     {
         if (magnitude < 0.1f && flag && carAI.ArriveAtDestination(leaderID, ref leaderData))
         {
             leaderData.Unspawn(leaderID);
             if (leaderID == vehicleID)
             {
                 frameData = leaderData.m_frame0;
             }
             return;
         }
     }
     if ((leaderData.m_flags & Vehicle.Flags.Stopped) == Vehicle.Flags.None && num10 < 0.1f)
     {
         flag5 = true;
     }
     if (flag5)
     {
         vehicleData.m_blockCounter = (byte)Mathf.Min((int)(vehicleData.m_blockCounter + 1), 255);
         if ((vehicleData.m_blockCounter == 100 || vehicleData.m_blockCounter == 150) && (CSLTraffic.Options & OptionsManager.ModOptions.NoDespawn) == OptionsManager.ModOptions.NoDespawn)
             vehicleData.m_blockCounter++;
     }
     else
     {
         vehicleData.m_blockCounter = 0;
     }
     if (num13 > 1f)
     {
         num12 = Mathf.Asin(a.x) * Mathf.Sign(num11);
         vector3 = a * num11;
     }
     else
     {
         num11 = 0f;
         Vector3 b4 = Vector3.ClampMagnitude(vector * 0.5f - vector2, braking);
         vector3 = vector2 + b4;
     }
     bool flag7 = (currentFrameIndex + (uint)leaderID & 16u) != 0u;
     Vector3 a2 = vector3 - vector2;
     Vector3 vector5 = frameData.m_rotation * vector3;
     frameData.m_velocity = vector5 + zero;
     frameData.m_position += frameData.m_velocity * 0.5f;
     frameData.m_swayVelocity = frameData.m_swayVelocity * (1f - carAI.m_info.m_dampers) - a2 * (1f - carAI.m_info.m_springs) - frameData.m_swayPosition * carAI.m_info.m_springs;
     frameData.m_swayPosition += frameData.m_swayVelocity * 0.5f;
     frameData.m_steerAngle = num12;
     frameData.m_travelDistance += vector3.z;
     frameData.m_lightIntensity.x = 5f;
     frameData.m_lightIntensity.y = ((a2.z >= -0.1f) ? 0.5f : 5f);
     frameData.m_lightIntensity.z = ((num12 >= -0.1f || !flag7) ? 0f : 5f);
     frameData.m_lightIntensity.w = ((num12 <= 0.1f || !flag7) ? 0f : 5f);
     frameData.m_underground = ((vehicleData.m_flags & Vehicle.Flags.Underground) != Vehicle.Flags.None);
     frameData.m_transition = ((vehicleData.m_flags & Vehicle.Flags.Transition) != Vehicle.Flags.None);
     if ((vehicleData.m_flags & Vehicle.Flags.Parking) != Vehicle.Flags.None && num13 <= 1f && flag)
     {
         Vector3 forward = vehicleData.m_targetPos1 - vehicleData.m_targetPos0;
         if (forward.sqrMagnitude > 0.01f)
         {
             frameData.m_rotation = Quaternion.LookRotation(forward);
         }
     }
     else
     {
         if (num11 > 0.1f)
         {
             if (vector5.sqrMagnitude > 0.01f)
             {
                 frameData.m_rotation = Quaternion.LookRotation(vector5);
             }
         }
         else
         {
             if (num11 < -0.1f && vector5.sqrMagnitude > 0.01f)
             {
                 frameData.m_rotation = Quaternion.LookRotation(-vector5);
             }
         }
     }
 }
Exemplo n.º 2
0
        public static void SimulationStep(CarAI carAI, ushort vehicleID, ref Vehicle vehicleData, ref Vehicle.Frame frameData, ushort leaderID, ref Vehicle leaderData, int lodPhysics)
        {
            uint currentFrameIndex = Singleton<SimulationManager>.instance.m_currentFrameIndex;
            frameData.m_position += frameData.m_velocity * 0.5f;
            frameData.m_swayPosition += frameData.m_swayVelocity * 0.5f;
            float acceleration = carAI.m_info.m_acceleration;
            float braking = carAI.m_info.m_braking;
            float magnitude = frameData.m_velocity.magnitude;
            Vector3 vector = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
            float sqrMagnitude = vector.sqrMagnitude;
            float num = (magnitude + acceleration) * (0.5f + 0.5f * (magnitude + acceleration) / braking) + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
            float num2 = Mathf.Max(magnitude + acceleration, 5f);
            if (lodPhysics >= 2 && (ulong)(currentFrameIndex >> 4 & 3u) == (ulong)((long)(vehicleID & 3)))
            {
                num2 *= 2f;
            }
            float num3 = Mathf.Max((num - num2) / 3f, 1f);
            float num4 = num2 * num2;
            float num5 = num3 * num3;
            int i = 0;
            bool flag = false;
            if ((sqrMagnitude < num4 || vehicleData.m_targetPos3.w < 0.01f) && (leaderData.m_flags & (Vehicle.Flags.WaitingPath | Vehicle.Flags.Stopped)) == Vehicle.Flags.None)
            {
                if (leaderData.m_path != 0u)
                {
                    CustomVehicleAI.UpdatePathTargetPositions(carAI, vehicleID, ref vehicleData, frameData.m_position, ref i, 4, num4, num5);
                    if ((leaderData.m_flags & Vehicle.Flags.Spawned) == Vehicle.Flags.None)
                    {
                        frameData = vehicleData.m_frame0;
                        return;
                    }
                }
                if ((leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
                {
                    while (i < 4)
                    {
                        float minSqrDistance;
                        Vector3 refPos;
                        if (i == 0)
                        {
                            minSqrDistance = num4;
                            refPos = frameData.m_position;
                            flag = true;
                        }
                        else
                        {
                            minSqrDistance = num5;
                            refPos = vehicleData.GetTargetPos(i - 1);
                        }
                        int num6 = i;
                        carAI.UpdateBuildingTargetPositions(vehicleID, ref vehicleData, refPos, leaderID, ref leaderData, ref i, minSqrDistance);
                        if (i == num6)
                        {
                            break;
                        }
                    }
                    if (i != 0)
                    {
                        Vector4 targetPos = vehicleData.GetTargetPos(i - 1);
                        while (i < 4)
                        {
                            vehicleData.SetTargetPos(i++, targetPos);
                        }
                    }
                }
                vector = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
                sqrMagnitude = vector.sqrMagnitude;
            }
            if (leaderData.m_path != 0u && (leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
            {
                NetManager instance = Singleton<NetManager>.instance;
                byte b = leaderData.m_pathPositionIndex;
                byte lastPathOffset = leaderData.m_lastPathOffset;
                if (b == 255)
                {
                    b = 0;
                }
                float num7 = 1f + leaderData.CalculateTotalLength(leaderID);
                PathManager instance2 = Singleton<PathManager>.instance;
                PathUnit.Position pathPos;
                if (instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetPosition(b >> 1, out pathPos))
                {
                    instance.m_segments.m_buffer[(int)pathPos.m_segment].AddTraffic(Mathf.RoundToInt(num7 * 2.5f));
                    bool flag2 = false;
                    if ((b & 1) == 0 || lastPathOffset == 0)
                    {
                        uint laneID = PathManager.GetLaneID(pathPos);
                        if (laneID != 0u)
                        {
                            Vector3 b2 = instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].CalculatePosition((float)pathPos.m_offset * 0.003921569f);
                            float num8 = 0.5f * magnitude * magnitude / carAI.m_info.m_braking + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
                            if (Vector3.Distance(frameData.m_position, b2) >= num8 - 1f)
                            {
                                instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].ReserveSpace(num7);
                                flag2 = true;
                            }
                        }
                    }
                    if (!flag2 && instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetNextPosition(b >> 1, out pathPos))
                    {
                        uint laneID2 = PathManager.GetLaneID(pathPos);
                        if (laneID2 != 0u)
                        {
                            instance.m_lanes.m_buffer[(int)((UIntPtr)laneID2)].ReserveSpace(num7);
                        }
                    }
                }
                /* -------------------- Congestion Changes ------------------------- */
                // Not everything is new. Changes are commented
                if ((ulong)(currentFrameIndex >> 4 & 15u) == (ulong)((long)(leaderID & 15)))
                {
                    bool flag3 = false;
                    uint path = leaderData.m_path;
                    int num9 = b >> 1;
                    int j = 0, count = 0; // the count variable is used to keep track of how many of the next 5 lanes are congested
                    //int j = 0;
                    while (j < 5)
                    {
                        bool flag4;
                        if (PathUnit.GetNextPosition(ref path, ref num9, out pathPos, out flag4))
                        {
                            uint laneID3 = PathManager.GetLaneID(pathPos);
                            if (laneID3 != 0 && !instance.m_lanes.m_buffer[(int)((UIntPtr)laneID3)].CheckSpace(num7))
                            {
                                j++;
                                ++count; // this lane is congested so increase count
                                continue;
                            }
                        }
                        if (flag4)
                        {
                            (carAI as IVehicle).InvalidPath(vehicleID, ref vehicleData, leaderID, ref leaderData);
                            // flag it as not congested and set count to -1 so that it is neither congested nor completely clear
                            // this is needed here because, contrary to the default code, it does not leave the cycle below
                            if ((CSLTraffic.Options & OptionsManager.ModOptions.ImprovedAI) == OptionsManager.ModOptions.ImprovedAI)
                            {
                                flag3 = true;
                                count = -1;
                                break;
                            }
                        }
                        flag3 = true;
                        ++j;
                        if ((CSLTraffic.Options & OptionsManager.ModOptions.ImprovedAI) != OptionsManager.ModOptions.ImprovedAI)
                        {
                            break;
                        }
                        // the default code would leave the cycle at this point since it found a non congested lane.
                        // this has been changed so that vehicles detect congestions a few lanes in advance.
                        // I am yet to test the performance impact this particular "feature" has.
                    }

                    if ((CSLTraffic.Options & OptionsManager.ModOptions.ImprovedAI) == OptionsManager.ModOptions.ImprovedAI)
                    {
                        // if at least 2 out of the next 5 lanes are congested and it hasn't tried to find a new path yet, then calculates a new path and flags it as such
                        // the amounf of congested lanes necessary to calculate a new path can be tweaked to reduce the amount of new paths being calculated, if performance in bigger cities is severely affected
                        if (count >= 2 && (leaderData.m_flags & (Vehicle.Flags)1073741824) == 0)
                        {
                            leaderData.m_flags |= (Vehicle.Flags)1073741824;
                            (carAI as IVehicle).InvalidPath(vehicleID, ref vehicleData, leaderID, ref leaderData);
                        }
                        // if none of the next 5 lanes is congested and the vehicle has already searched for a new path, then it successfully avoided a congestion and the flag is cleared
                        else if (count == 0 && (leaderData.m_flags & (Vehicle.Flags)1073741824) != 0)
                        {
                            leaderData.m_flags &= ~((Vehicle.Flags)1073741824);
                        }
                        // default congestion behavior
                        else if (!flag3)
                            leaderData.m_flags |= Vehicle.Flags.Congestion;
                    }
                    else if (!flag3)
                    {
                        leaderData.m_flags |= Vehicle.Flags.Congestion;
                    }
                }
                /* ----------------------------------------------------------------- */
            }
            float num10;
            if ((leaderData.m_flags & Vehicle.Flags.Stopped) != Vehicle.Flags.None)
            {
                num10 = 0f;
            }
            else
            {
                num10 = vehicleData.m_targetPos0.w;
            }
            Quaternion rotation = Quaternion.Inverse(frameData.m_rotation);
            vector = rotation * vector;
            Vector3 vector2 = rotation * frameData.m_velocity;
            Vector3 a = Vector3.forward;
            Vector3 vector3 = Vector3.zero;
            Vector3 zero = Vector3.zero;
            float num11 = 0f;
            float num12 = 0f;
            bool flag5 = false;
            float num13 = 0f;
            if (sqrMagnitude > 1f)
            {
                a = VectorUtils.NormalizeXZ(vector, out num13);
                if (num13 > 1f)
                {
                    Vector3 vector4 = vector;
                    num2 = Mathf.Max(magnitude, 2f);
                    num4 = num2 * num2;
                    if (sqrMagnitude > num4)
                    {
                        vector4 *= num2 / Mathf.Sqrt(sqrMagnitude);
                    }
                    bool flag6 = false;
                    if (vector4.z < Mathf.Abs(vector4.x))
                    {
                        if (vector4.z < 0f)
                        {
                            flag6 = true;
                        }
                        float num14 = Mathf.Abs(vector4.x);
                        if (num14 < 1f)
                        {
                            vector4.x = Mathf.Sign(vector4.x);
                            if (vector4.x == 0f)
                            {
                                vector4.x = 1f;
                            }
                            num14 = 1f;
                        }
                        vector4.z = num14;
                    }
                    float b3;
                    a = VectorUtils.NormalizeXZ(vector4, out b3);
                    num13 = Mathf.Min(num13, b3);
                    float num15 = 1.57079637f * (1f - a.z);
                    if (num13 > 1f)
                    {
                        num15 /= num13;
                    }
                    float num16 = num13;
                    if (vehicleData.m_targetPos0.w < 0.1f)
                    {
                        num10 = (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15);
                        num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, Mathf.Min(vehicleData.m_targetPos0.w, vehicleData.m_targetPos1.w), braking * 0.9f));
                    }
                    else
                    {
                        num10 = Mathf.Min(num10, (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15));
                        num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos1.w, braking * 0.9f));
                    }
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos1 - vehicleData.m_targetPos0);
                    num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos2.w, braking * 0.9f));
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos2 - vehicleData.m_targetPos1);
                    num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos3.w, braking * 0.9f));
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos3 - vehicleData.m_targetPos2);
                    if (vehicleData.m_targetPos3.w < 0.01f)
                    {
                        num16 = Mathf.Max(0f, num16 - carAI.m_info.m_generatedInfo.m_size.z * 0.5f);
                    }
                    num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, 0f, braking * 0.9f));
                    if (!DisableCollisionCheck(leaderID, ref leaderData))
                    {
                        CustomCarAI.CheckOtherVehicles(carAI, vehicleID, ref vehicleData, ref frameData, ref num10, ref flag5, ref zero, num, braking * 0.9f, lodPhysics);
                    }
                    if (flag6)
                    {
                        num10 = -num10;
                    }
                    if (num10 < magnitude)
                    {
                        float num17 = Mathf.Max(acceleration, Mathf.Min(braking, magnitude));
                        num11 = Mathf.Max(num10, magnitude - num17);
                    }
                    else
                    {
                        float num18 = Mathf.Max(acceleration, Mathf.Min(braking, -magnitude));
                        num11 = Mathf.Min(num10, magnitude + num18);
                    }
                }
            }
            else
            {
                if (magnitude < 0.1f && flag && carAI.ArriveAtDestination(leaderID, ref leaderData))
                {
                    leaderData.Unspawn(leaderID);
                    if (leaderID == vehicleID)
                    {
                        frameData = leaderData.m_frame0;
                    }
                    return;
                }
            }
            if ((leaderData.m_flags & Vehicle.Flags.Stopped) == Vehicle.Flags.None && num10 < 0.1f)
            {
                flag5 = true;
            }
            if (flag5)
            {
                vehicleData.m_blockCounter = (byte)Mathf.Min((int)(vehicleData.m_blockCounter + 1), 255);
                if ((vehicleData.m_blockCounter == 100 || vehicleData.m_blockCounter == 150) && (CSLTraffic.Options & OptionsManager.ModOptions.NoDespawn) == OptionsManager.ModOptions.NoDespawn)
                    vehicleData.m_blockCounter++;
            }
            else
            {
                vehicleData.m_blockCounter = 0;
            }
            if (num13 > 1f)
            {
                num12 = Mathf.Asin(a.x) * Mathf.Sign(num11);
                vector3 = a * num11;
            }
            else
            {
                num11 = 0f;
                Vector3 b4 = Vector3.ClampMagnitude(vector * 0.5f - vector2, braking);
                vector3 = vector2 + b4;
            }
            bool flag7 = (currentFrameIndex + (uint)leaderID & 16u) != 0u;
            Vector3 a2 = vector3 - vector2;
            Vector3 vector5 = frameData.m_rotation * vector3;
            frameData.m_velocity = vector5 + zero;
            frameData.m_position += frameData.m_velocity * 0.5f;
            frameData.m_swayVelocity = frameData.m_swayVelocity * (1f - carAI.m_info.m_dampers) - a2 * (1f - carAI.m_info.m_springs) - frameData.m_swayPosition * carAI.m_info.m_springs;
            frameData.m_swayPosition += frameData.m_swayVelocity * 0.5f;
            frameData.m_steerAngle = num12;
            frameData.m_travelDistance += vector3.z;
            frameData.m_lightIntensity.x = 5f;
            frameData.m_lightIntensity.y = ((a2.z >= -0.1f) ? 0.5f : 5f);
            frameData.m_lightIntensity.z = ((num12 >= -0.1f || !flag7) ? 0f : 5f);
            frameData.m_lightIntensity.w = ((num12 <= 0.1f || !flag7) ? 0f : 5f);
            frameData.m_underground = ((vehicleData.m_flags & Vehicle.Flags.Underground) != Vehicle.Flags.None);
            frameData.m_transition = ((vehicleData.m_flags & Vehicle.Flags.Transition) != Vehicle.Flags.None);
            if ((vehicleData.m_flags & Vehicle.Flags.Parking) != Vehicle.Flags.None && num13 <= 1f && flag)
            {
                Vector3 forward = vehicleData.m_targetPos1 - vehicleData.m_targetPos0;
                if (forward.sqrMagnitude > 0.01f)
                {
                    frameData.m_rotation = Quaternion.LookRotation(forward);
                }
            }
            else
            {
                if (num11 > 0.1f)
                {
                    if (vector5.sqrMagnitude > 0.01f)
                    {
                        frameData.m_rotation = Quaternion.LookRotation(vector5);
                    }
                }
                else
                {
                    if (num11 < -0.1f && vector5.sqrMagnitude > 0.01f)
                    {
                        frameData.m_rotation = Quaternion.LookRotation(-vector5);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static void SimulationStep(CarAI carAI, ushort vehicleID, ref Vehicle vehicleData, ref Vehicle.Frame frameData, ushort leaderID, ref Vehicle leaderData, int lodPhysics)
        {
            uint currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex;

            frameData.m_position     += frameData.m_velocity * 0.5f;
            frameData.m_swayPosition += frameData.m_swayVelocity * 0.5f;
            float   acceleration = carAI.m_info.m_acceleration;
            float   braking      = carAI.m_info.m_braking;
            float   magnitude    = frameData.m_velocity.magnitude;
            Vector3 vector       = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
            float   sqrMagnitude = vector.sqrMagnitude;
            float   num          = (magnitude + acceleration) * (0.5f + 0.5f * (magnitude + acceleration) / braking) + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
            float   num2         = Mathf.Max(magnitude + acceleration, 5f);

            if (lodPhysics >= 2 && (ulong)(currentFrameIndex >> 4 & 3u) == (ulong)((long)(vehicleID & 3)))
            {
                num2 *= 2f;
            }
            float num3 = Mathf.Max((num - num2) / 3f, 1f);
            float num4 = num2 * num2;
            float num5 = num3 * num3;
            int   i    = 0;
            bool  flag = false;

            if ((sqrMagnitude < num4 || vehicleData.m_targetPos3.w < 0.01f) && (leaderData.m_flags & (Vehicle.Flags.WaitingPath | Vehicle.Flags.Stopped)) == Vehicle.Flags.None)
            {
                if (leaderData.m_path != 0u)
                {
                    CustomVehicleAI.UpdatePathTargetPositions(carAI, vehicleID, ref vehicleData, frameData.m_position, ref i, 4, num4, num5);
                    if ((leaderData.m_flags & Vehicle.Flags.Spawned) == Vehicle.Flags.None)
                    {
                        frameData = vehicleData.m_frame0;
                        return;
                    }
                }
                if ((leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
                {
                    while (i < 4)
                    {
                        float   minSqrDistance;
                        Vector3 refPos;
                        if (i == 0)
                        {
                            minSqrDistance = num4;
                            refPos         = frameData.m_position;
                            flag           = true;
                        }
                        else
                        {
                            minSqrDistance = num5;
                            refPos         = vehicleData.GetTargetPos(i - 1);
                        }
                        int num6 = i;
                        carAI.UpdateBuildingTargetPositions(vehicleID, ref vehicleData, refPos, leaderID, ref leaderData, ref i, minSqrDistance);
                        if (i == num6)
                        {
                            break;
                        }
                    }
                    if (i != 0)
                    {
                        Vector4 targetPos = vehicleData.GetTargetPos(i - 1);
                        while (i < 4)
                        {
                            vehicleData.SetTargetPos(i++, targetPos);
                        }
                    }
                }
                vector       = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
                sqrMagnitude = vector.sqrMagnitude;
            }
            if (leaderData.m_path != 0u && (leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
            {
                NetManager instance       = Singleton <NetManager> .instance;
                byte       b              = leaderData.m_pathPositionIndex;
                byte       lastPathOffset = leaderData.m_lastPathOffset;
                if (b == 255)
                {
                    b = 0;
                }
                float             num7      = 1f + leaderData.CalculateTotalLength(leaderID);
                PathManager       instance2 = Singleton <PathManager> .instance;
                PathUnit.Position pathPos;
                if (instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetPosition(b >> 1, out pathPos))
                {
                    instance.m_segments.m_buffer[(int)pathPos.m_segment].AddTraffic(Mathf.RoundToInt(num7 * 2.5f));
                    bool flag2 = false;
                    if ((b & 1) == 0 || lastPathOffset == 0)
                    {
                        uint laneID = PathManager.GetLaneID(pathPos);
                        if (laneID != 0u)
                        {
                            Vector3 b2   = instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].CalculatePosition((float)pathPos.m_offset * 0.003921569f);
                            float   num8 = 0.5f * magnitude * magnitude / carAI.m_info.m_braking + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
                            if (Vector3.Distance(frameData.m_position, b2) >= num8 - 1f)
                            {
                                instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].ReserveSpace(num7);
                                flag2 = true;
                            }
                        }
                    }
                    if (!flag2 && instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetNextPosition(b >> 1, out pathPos))
                    {
                        uint laneID2 = PathManager.GetLaneID(pathPos);
                        if (laneID2 != 0u)
                        {
                            instance.m_lanes.m_buffer[(int)((UIntPtr)laneID2)].ReserveSpace(num7);
                        }
                    }
                }
                if ((ulong)(currentFrameIndex >> 4 & 15u) == (ulong)((long)(leaderID & 15)))
                {
                    bool flag3 = false;
                    uint path  = leaderData.m_path;
                    int  num9  = b >> 1;
                    int  j     = 0;
                    while (j < 5)
                    {
                        bool flag4;
                        if (PathUnit.GetNextPosition(ref path, ref num9, out pathPos, out flag4))
                        {
                            uint laneID3 = PathManager.GetLaneID(pathPos);
                            if (laneID3 != 0u && !instance.m_lanes.m_buffer[(int)((UIntPtr)laneID3)].CheckSpace(num7))
                            {
                                j++;
                                continue;
                            }
                        }
                        if (flag4)
                        {
                            (carAI as IVehicle).InvalidPath(vehicleID, ref vehicleData, leaderID, ref leaderData);
                        }
                        flag3 = true;
                        break;
                    }
                    if (!flag3)
                    {
                        leaderData.m_flags |= Vehicle.Flags.Congestion;
                    }
                }
            }
            float num10;

            if ((leaderData.m_flags & Vehicle.Flags.Stopped) != Vehicle.Flags.None)
            {
                num10 = 0f;
            }
            else
            {
                num10 = vehicleData.m_targetPos0.w;
            }
            Quaternion rotation = Quaternion.Inverse(frameData.m_rotation);

            vector = rotation * vector;
            Vector3 vector2 = rotation * frameData.m_velocity;
            Vector3 a       = Vector3.forward;
            Vector3 vector3 = Vector3.zero;
            Vector3 zero    = Vector3.zero;
            float   num11   = 0f;
            float   num12   = 0f;
            bool    flag5   = false;
            float   num13   = 0f;

            if (sqrMagnitude > 1f)
            {
                a = VectorUtils.NormalizeXZ(vector, out num13);
                if (num13 > 1f)
                {
                    Vector3 vector4 = vector;
                    num2 = Mathf.Max(magnitude, 2f);
                    num4 = num2 * num2;
                    if (sqrMagnitude > num4)
                    {
                        vector4 *= num2 / Mathf.Sqrt(sqrMagnitude);
                    }
                    bool flag6 = false;
                    if (vector4.z < Mathf.Abs(vector4.x))
                    {
                        if (vector4.z < 0f)
                        {
                            flag6 = true;
                        }
                        float num14 = Mathf.Abs(vector4.x);
                        if (num14 < 1f)
                        {
                            vector4.x = Mathf.Sign(vector4.x);
                            if (vector4.x == 0f)
                            {
                                vector4.x = 1f;
                            }
                            num14 = 1f;
                        }
                        vector4.z = num14;
                    }
                    float b3;
                    a     = VectorUtils.NormalizeXZ(vector4, out b3);
                    num13 = Mathf.Min(num13, b3);
                    float num15 = 1.57079637f * (1f - a.z);
                    if (num13 > 1f)
                    {
                        num15 /= num13;
                    }
                    float num16 = num13;
                    if (vehicleData.m_targetPos0.w < 0.1f)
                    {
                        num10 = (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15);
                        num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, Mathf.Min(vehicleData.m_targetPos0.w, vehicleData.m_targetPos1.w), braking * 0.9f));
                    }
                    else
                    {
                        num10 = Mathf.Min(num10, (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15));
                        num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos1.w, braking * 0.9f));
                    }
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos1 - vehicleData.m_targetPos0);
                    num10  = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos2.w, braking * 0.9f));
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos2 - vehicleData.m_targetPos1);
                    num10  = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos3.w, braking * 0.9f));
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos3 - vehicleData.m_targetPos2);
                    if (vehicleData.m_targetPos3.w < 0.01f)
                    {
                        num16 = Mathf.Max(0f, num16 - carAI.m_info.m_generatedInfo.m_size.z * 0.5f);
                    }
                    num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, 0f, braking * 0.9f));
                    if (!DisableCollisionCheck(leaderID, ref leaderData))
                    {
                        CustomCarAI.CheckOtherVehicles(carAI, vehicleID, ref vehicleData, ref frameData, ref num10, ref flag5, ref zero, num, braking * 0.9f, lodPhysics);
                    }
                    if (flag6)
                    {
                        num10 = -num10;
                    }
                    if (num10 < magnitude)
                    {
                        float num17 = Mathf.Max(acceleration, Mathf.Min(braking, magnitude));
                        num11 = Mathf.Max(num10, magnitude - num17);
                    }
                    else
                    {
                        float num18 = Mathf.Max(acceleration, Mathf.Min(braking, -magnitude));
                        num11 = Mathf.Min(num10, magnitude + num18);
                    }
                }
            }
            else
            {
                if (magnitude < 0.1f && flag && carAI.ArriveAtDestination(leaderID, ref leaderData))
                {
                    leaderData.Unspawn(leaderID);
                    if (leaderID == vehicleID)
                    {
                        frameData = leaderData.m_frame0;
                    }
                    return;
                }
            }
            if ((leaderData.m_flags & Vehicle.Flags.Stopped) == Vehicle.Flags.None && num10 < 0.1f)
            {
                flag5 = true;
            }
            if (flag5)
            {
                vehicleData.m_blockCounter = (byte)Mathf.Min((int)(vehicleData.m_blockCounter + 1), 255);
                if ((vehicleData.m_blockCounter == 100 || vehicleData.m_blockCounter == 150) && (CSLTraffic.Options & OptionsManager.ModOptions.NoDespawn) == OptionsManager.ModOptions.NoDespawn)
                {
                    vehicleData.m_blockCounter++;
                }
            }
            else
            {
                vehicleData.m_blockCounter = 0;
            }
            if (num13 > 1f)
            {
                num12   = Mathf.Asin(a.x) * Mathf.Sign(num11);
                vector3 = a * num11;
            }
            else
            {
                num11 = 0f;
                Vector3 b4 = Vector3.ClampMagnitude(vector * 0.5f - vector2, braking);
                vector3 = vector2 + b4;
            }
            bool    flag7   = (currentFrameIndex + (uint)leaderID & 16u) != 0u;
            Vector3 a2      = vector3 - vector2;
            Vector3 vector5 = frameData.m_rotation * vector3;

            frameData.m_velocity         = vector5 + zero;
            frameData.m_position        += frameData.m_velocity * 0.5f;
            frameData.m_swayVelocity     = frameData.m_swayVelocity * (1f - carAI.m_info.m_dampers) - a2 * (1f - carAI.m_info.m_springs) - frameData.m_swayPosition * carAI.m_info.m_springs;
            frameData.m_swayPosition    += frameData.m_swayVelocity * 0.5f;
            frameData.m_steerAngle       = num12;
            frameData.m_travelDistance  += vector3.z;
            frameData.m_lightIntensity.x = 5f;
            frameData.m_lightIntensity.y = ((a2.z >= -0.1f) ? 0.5f : 5f);
            frameData.m_lightIntensity.z = ((num12 >= -0.1f || !flag7) ? 0f : 5f);
            frameData.m_lightIntensity.w = ((num12 <= 0.1f || !flag7) ? 0f : 5f);
            frameData.m_underground      = ((vehicleData.m_flags & Vehicle.Flags.Underground) != Vehicle.Flags.None);
            frameData.m_transition       = ((vehicleData.m_flags & Vehicle.Flags.Transition) != Vehicle.Flags.None);
            if ((vehicleData.m_flags & Vehicle.Flags.Parking) != Vehicle.Flags.None && num13 <= 1f && flag)
            {
                Vector3 forward = vehicleData.m_targetPos1 - vehicleData.m_targetPos0;
                if (forward.sqrMagnitude > 0.01f)
                {
                    frameData.m_rotation = Quaternion.LookRotation(forward);
                }
            }
            else
            {
                if (num11 > 0.1f)
                {
                    if (vector5.sqrMagnitude > 0.01f)
                    {
                        frameData.m_rotation = Quaternion.LookRotation(vector5);
                    }
                }
                else
                {
                    if (num11 < -0.1f && vector5.sqrMagnitude > 0.01f)
                    {
                        frameData.m_rotation = Quaternion.LookRotation(-vector5);
                    }
                }
            }
        }
Exemplo n.º 4
0
        public static void SimulationStep(CarAI carAI, ushort vehicleID, ref Vehicle vehicleData, ref Vehicle.Frame frameData, ushort leaderID, ref Vehicle leaderData, int lodPhysics)
        {
            uint currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex;

            frameData.m_position     += frameData.m_velocity * 0.5f;
            frameData.m_swayPosition += frameData.m_swayVelocity * 0.5f;
            float   acceleration = carAI.m_info.m_acceleration;
            float   braking      = carAI.m_info.m_braking;
            float   magnitude    = frameData.m_velocity.magnitude;
            Vector3 vector       = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
            float   sqrMagnitude = vector.sqrMagnitude;
            float   num          = (magnitude + acceleration) * (0.5f + 0.5f * (magnitude + acceleration) / braking) + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
            float   num2         = Mathf.Max(magnitude + acceleration, 5f);

            if (lodPhysics >= 2 && (ulong)(currentFrameIndex >> 4 & 3u) == (ulong)((long)(vehicleID & 3)))
            {
                num2 *= 2f;
            }
            float num3 = Mathf.Max((num - num2) / 3f, 1f);
            float num4 = num2 * num2;
            float num5 = num3 * num3;
            int   i    = 0;
            bool  flag = false;

            if ((sqrMagnitude < num4 || vehicleData.m_targetPos3.w < 0.01f) && (leaderData.m_flags & (Vehicle.Flags.WaitingPath | Vehicle.Flags.Stopped)) == Vehicle.Flags.None)
            {
                if (leaderData.m_path != 0u)
                {
                    CustomVehicleAI.UpdatePathTargetPositions(carAI, vehicleID, ref vehicleData, frameData.m_position, ref i, 4, num4, num5);
                    if ((leaderData.m_flags & Vehicle.Flags.Spawned) == Vehicle.Flags.None)
                    {
                        frameData = vehicleData.m_frame0;
                        return;
                    }
                }
                if ((leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
                {
                    while (i < 4)
                    {
                        float   minSqrDistance;
                        Vector3 refPos;
                        if (i == 0)
                        {
                            minSqrDistance = num4;
                            refPos         = frameData.m_position;
                            flag           = true;
                        }
                        else
                        {
                            minSqrDistance = num5;
                            refPos         = vehicleData.GetTargetPos(i - 1);
                        }
                        int num6 = i;
                        carAI.UpdateBuildingTargetPositions(vehicleID, ref vehicleData, refPos, leaderID, ref leaderData, ref i, minSqrDistance);
                        if (i == num6)
                        {
                            break;
                        }
                    }
                    if (i != 0)
                    {
                        Vector4 targetPos = vehicleData.GetTargetPos(i - 1);
                        while (i < 4)
                        {
                            vehicleData.SetTargetPos(i++, targetPos);
                        }
                    }
                }
                vector       = vehicleData.m_targetPos0 - (Vector4)frameData.m_position;
                sqrMagnitude = vector.sqrMagnitude;
            }
            if (leaderData.m_path != 0u && (leaderData.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.None)
            {
                NetManager instance       = Singleton <NetManager> .instance;
                byte       b              = leaderData.m_pathPositionIndex;
                byte       lastPathOffset = leaderData.m_lastPathOffset;
                if (b == 255)
                {
                    b = 0;
                }
                float             num7      = 1f + leaderData.CalculateTotalLength(leaderID);
                PathManager       instance2 = Singleton <PathManager> .instance;
                PathUnit.Position pathPos;
                if (instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetPosition(b >> 1, out pathPos))
                {
                    instance.m_segments.m_buffer[(int)pathPos.m_segment].AddTraffic(Mathf.RoundToInt(num7 * 2.5f));
                    bool flag2 = false;
                    if ((b & 1) == 0 || lastPathOffset == 0)
                    {
                        uint laneID = PathManager.GetLaneID(pathPos);
                        if (laneID != 0u)
                        {
                            Vector3 b2   = instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].CalculatePosition((float)pathPos.m_offset * 0.003921569f);
                            float   num8 = 0.5f * magnitude * magnitude / carAI.m_info.m_braking + carAI.m_info.m_generatedInfo.m_size.z * 0.5f;
                            if (Vector3.Distance(frameData.m_position, b2) >= num8 - 1f)
                            {
                                instance.m_lanes.m_buffer[(int)((UIntPtr)laneID)].ReserveSpace(num7);
                                flag2 = true;
                            }
                        }
                    }
                    if (!flag2 && instance2.m_pathUnits.m_buffer[(int)((UIntPtr)leaderData.m_path)].GetNextPosition(b >> 1, out pathPos))
                    {
                        uint laneID2 = PathManager.GetLaneID(pathPos);
                        if (laneID2 != 0u)
                        {
                            instance.m_lanes.m_buffer[(int)((UIntPtr)laneID2)].ReserveSpace(num7);
                        }
                    }
                }
                /* -------------------- Congestion Changes ------------------------- */
                // Not everything is new. Changes are commented
                if ((ulong)(currentFrameIndex >> 4 & 15u) == (ulong)((long)(leaderID & 15)))
                {
                    bool flag3 = false;
                    uint path = leaderData.m_path;
                    int  num9 = b >> 1;
                    int  j = 0, count = 0; // the count variable is used to keep track of how many of the next 5 lanes are congested
                    //int j = 0;
                    while (j < 5)
                    {
                        bool flag4;
                        if (PathUnit.GetNextPosition(ref path, ref num9, out pathPos, out flag4))
                        {
                            uint laneID3 = PathManager.GetLaneID(pathPos);
                            if (laneID3 != 0 && !instance.m_lanes.m_buffer[(int)((UIntPtr)laneID3)].CheckSpace(num7))
                            {
                                j++;
                                ++count; // this lane is congested so increase count
                                continue;
                            }
                        }
                        if (flag4)
                        {
                            (carAI as IVehicle).InvalidPath(vehicleID, ref vehicleData, leaderID, ref leaderData);
                            // flag it as not congested and set count to -1 so that it is neither congested nor completely clear
                            // this is needed here because, contrary to the default code, it does not leave the cycle below
                            if ((CSLTraffic.Options & OptionsManager.ModOptions.ImprovedAI) == OptionsManager.ModOptions.ImprovedAI)
                            {
                                flag3 = true;
                                count = -1;
                                break;
                            }
                        }
                        flag3 = true;
                        ++j;
                        if ((CSLTraffic.Options & OptionsManager.ModOptions.ImprovedAI) != OptionsManager.ModOptions.ImprovedAI)
                        {
                            break;
                        }
                        // the default code would leave the cycle at this point since it found a non congested lane.
                        // this has been changed so that vehicles detect congestions a few lanes in advance.
                        // I am yet to test the performance impact this particular "feature" has.
                    }

                    if ((CSLTraffic.Options & OptionsManager.ModOptions.ImprovedAI) == OptionsManager.ModOptions.ImprovedAI)
                    {
                        // if at least 2 out of the next 5 lanes are congested and it hasn't tried to find a new path yet, then calculates a new path and flags it as such
                        // the amounf of congested lanes necessary to calculate a new path can be tweaked to reduce the amount of new paths being calculated, if performance in bigger cities is severely affected
                        if (count >= 2 && (leaderData.m_flags & (Vehicle.Flags) 1073741824) == 0)
                        {
                            leaderData.m_flags |= (Vehicle.Flags) 1073741824;
                            (carAI as IVehicle).InvalidPath(vehicleID, ref vehicleData, leaderID, ref leaderData);
                        }
                        // if none of the next 5 lanes is congested and the vehicle has already searched for a new path, then it successfully avoided a congestion and the flag is cleared
                        else if (count == 0 && (leaderData.m_flags & (Vehicle.Flags) 1073741824) != 0)
                        {
                            leaderData.m_flags &= ~((Vehicle.Flags) 1073741824);
                        }
                        // default congestion behavior
                        else if (!flag3)
                        {
                            leaderData.m_flags |= Vehicle.Flags.Congestion;
                        }
                    }
                    else if (!flag3)
                    {
                        leaderData.m_flags |= Vehicle.Flags.Congestion;
                    }
                }
                /* ----------------------------------------------------------------- */
            }
            float num10;

            if ((leaderData.m_flags & Vehicle.Flags.Stopped) != Vehicle.Flags.None)
            {
                num10 = 0f;
            }
            else
            {
                num10 = vehicleData.m_targetPos0.w;
            }
            Quaternion rotation = Quaternion.Inverse(frameData.m_rotation);

            vector = rotation * vector;
            Vector3 vector2 = rotation * frameData.m_velocity;
            Vector3 a       = Vector3.forward;
            Vector3 vector3 = Vector3.zero;
            Vector3 zero    = Vector3.zero;
            float   num11   = 0f;
            float   num12   = 0f;
            bool    flag5   = false;
            float   num13   = 0f;

            if (sqrMagnitude > 1f)
            {
                a = VectorUtils.NormalizeXZ(vector, out num13);
                if (num13 > 1f)
                {
                    Vector3 vector4 = vector;
                    num2 = Mathf.Max(magnitude, 2f);
                    num4 = num2 * num2;
                    if (sqrMagnitude > num4)
                    {
                        vector4 *= num2 / Mathf.Sqrt(sqrMagnitude);
                    }
                    bool flag6 = false;
                    if (vector4.z < Mathf.Abs(vector4.x))
                    {
                        if (vector4.z < 0f)
                        {
                            flag6 = true;
                        }
                        float num14 = Mathf.Abs(vector4.x);
                        if (num14 < 1f)
                        {
                            vector4.x = Mathf.Sign(vector4.x);
                            if (vector4.x == 0f)
                            {
                                vector4.x = 1f;
                            }
                            num14 = 1f;
                        }
                        vector4.z = num14;
                    }
                    float b3;
                    a     = VectorUtils.NormalizeXZ(vector4, out b3);
                    num13 = Mathf.Min(num13, b3);
                    float num15 = 1.57079637f * (1f - a.z);
                    if (num13 > 1f)
                    {
                        num15 /= num13;
                    }
                    float num16 = num13;
                    if (vehicleData.m_targetPos0.w < 0.1f)
                    {
                        num10 = (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15);
                        num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, Mathf.Min(vehicleData.m_targetPos0.w, vehicleData.m_targetPos1.w), braking * 0.9f));
                    }
                    else
                    {
                        num10 = Mathf.Min(num10, (carAI as IVehicle).CalculateTargetSpeed(vehicleID, ref vehicleData, 1000f, num15));
                        num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos1.w, braking * 0.9f));
                    }
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos1 - vehicleData.m_targetPos0);
                    num10  = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos2.w, braking * 0.9f));
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos2 - vehicleData.m_targetPos1);
                    num10  = Mathf.Min(num10, CalculateMaxSpeed(num16, vehicleData.m_targetPos3.w, braking * 0.9f));
                    num16 += VectorUtils.LengthXZ(vehicleData.m_targetPos3 - vehicleData.m_targetPos2);
                    if (vehicleData.m_targetPos3.w < 0.01f)
                    {
                        num16 = Mathf.Max(0f, num16 - carAI.m_info.m_generatedInfo.m_size.z * 0.5f);
                    }
                    num10 = Mathf.Min(num10, CalculateMaxSpeed(num16, 0f, braking * 0.9f));
                    if (!DisableCollisionCheck(leaderID, ref leaderData))
                    {
                        CustomCarAI.CheckOtherVehicles(carAI, vehicleID, ref vehicleData, ref frameData, ref num10, ref flag5, ref zero, num, braking * 0.9f, lodPhysics);
                    }
                    if (flag6)
                    {
                        num10 = -num10;
                    }
                    if (num10 < magnitude)
                    {
                        float num17 = Mathf.Max(acceleration, Mathf.Min(braking, magnitude));
                        num11 = Mathf.Max(num10, magnitude - num17);
                    }
                    else
                    {
                        float num18 = Mathf.Max(acceleration, Mathf.Min(braking, -magnitude));
                        num11 = Mathf.Min(num10, magnitude + num18);
                    }
                }
            }
            else
            {
                if (magnitude < 0.1f && flag && carAI.ArriveAtDestination(leaderID, ref leaderData))
                {
                    leaderData.Unspawn(leaderID);
                    if (leaderID == vehicleID)
                    {
                        frameData = leaderData.m_frame0;
                    }
                    return;
                }
            }
            if ((leaderData.m_flags & Vehicle.Flags.Stopped) == Vehicle.Flags.None && num10 < 0.1f)
            {
                flag5 = true;
            }
            if (flag5)
            {
                vehicleData.m_blockCounter = (byte)Mathf.Min((int)(vehicleData.m_blockCounter + 1), 255);
                if ((vehicleData.m_blockCounter == 100 || vehicleData.m_blockCounter == 150) && (CSLTraffic.Options & OptionsManager.ModOptions.NoDespawn) == OptionsManager.ModOptions.NoDespawn)
                {
                    vehicleData.m_blockCounter++;
                }
            }
            else
            {
                vehicleData.m_blockCounter = 0;
            }
            if (num13 > 1f)
            {
                num12   = Mathf.Asin(a.x) * Mathf.Sign(num11);
                vector3 = a * num11;
            }
            else
            {
                num11 = 0f;
                Vector3 b4 = Vector3.ClampMagnitude(vector * 0.5f - vector2, braking);
                vector3 = vector2 + b4;
            }
            bool    flag7   = (currentFrameIndex + (uint)leaderID & 16u) != 0u;
            Vector3 a2      = vector3 - vector2;
            Vector3 vector5 = frameData.m_rotation * vector3;

            frameData.m_velocity         = vector5 + zero;
            frameData.m_position        += frameData.m_velocity * 0.5f;
            frameData.m_swayVelocity     = frameData.m_swayVelocity * (1f - carAI.m_info.m_dampers) - a2 * (1f - carAI.m_info.m_springs) - frameData.m_swayPosition * carAI.m_info.m_springs;
            frameData.m_swayPosition    += frameData.m_swayVelocity * 0.5f;
            frameData.m_steerAngle       = num12;
            frameData.m_travelDistance  += vector3.z;
            frameData.m_lightIntensity.x = 5f;
            frameData.m_lightIntensity.y = ((a2.z >= -0.1f) ? 0.5f : 5f);
            frameData.m_lightIntensity.z = ((num12 >= -0.1f || !flag7) ? 0f : 5f);
            frameData.m_lightIntensity.w = ((num12 <= 0.1f || !flag7) ? 0f : 5f);
            frameData.m_underground      = ((vehicleData.m_flags & Vehicle.Flags.Underground) != Vehicle.Flags.None);
            frameData.m_transition       = ((vehicleData.m_flags & Vehicle.Flags.Transition) != Vehicle.Flags.None);
            if ((vehicleData.m_flags & Vehicle.Flags.Parking) != Vehicle.Flags.None && num13 <= 1f && flag)
            {
                Vector3 forward = vehicleData.m_targetPos1 - vehicleData.m_targetPos0;
                if (forward.sqrMagnitude > 0.01f)
                {
                    frameData.m_rotation = Quaternion.LookRotation(forward);
                }
            }
            else
            {
                if (num11 > 0.1f)
                {
                    if (vector5.sqrMagnitude > 0.01f)
                    {
                        frameData.m_rotation = Quaternion.LookRotation(vector5);
                    }
                }
                else
                {
                    if (num11 < -0.1f && vector5.sqrMagnitude > 0.01f)
                    {
                        frameData.m_rotation = Quaternion.LookRotation(-vector5);
                    }
                }
            }
        }