Exemple #1
0
        public void CustomCalculateSegmentPosition(ushort vehicleId, ref Vehicle vehicleData, PathUnit.Position nextPosition, PathUnit.Position position, uint laneID, byte offset, PathUnit.Position prevPos, uint prevLaneID, byte prevOffset, int index, out Vector3 pos, out Vector3 dir, out float maxSpeed)
        {
            Vehicle.Frame lastFrameData = vehicleData.GetLastFrameData();
            float sqrVelocity           = lastFrameData.m_velocity.sqrMagnitude;

            // NON-STOCK CODE START
#if BENCHMARK
            using (var bm = new Benchmark(null, "UpdateVehiclePosition")) {
#endif
            VehicleStateManager.Instance.UpdateVehiclePosition(vehicleId, ref vehicleData, lastFrameData.m_velocity.magnitude);
#if BENCHMARK
        }
#endif
            // NON-STOCK CODE END

            NetManager netManager = Singleton <NetManager> .instance;
            netManager.m_lanes.m_buffer[laneID].CalculatePositionAndDirection((float)offset * 0.003921569f, out pos, out dir);
            Vector3 b  = netManager.m_lanes.m_buffer[prevLaneID].CalculatePosition((float)prevOffset * 0.003921569f);
            Vector3 a  = lastFrameData.m_position;
            Vector3 a2 = lastFrameData.m_position;
            Vector3 b2 = lastFrameData.m_rotation * new Vector3(0f, 0f, this.m_info.m_generatedInfo.m_wheelBase * 0.5f);
            a  += b2;
            a2 -= b2;
            float crazyValue = 0.5f * sqrVelocity / this.m_info.m_braking;
            float a3         = Vector3.Distance(a, b);
            float b3         = Vector3.Distance(a2, b);
            if (Mathf.Min(a3, b3) >= crazyValue - 1f)
            {
                Segment3 segment;
                segment.a = pos;
                ushort targetNodeId;
                ushort nextTargetNodeId;
                if (offset < position.m_offset)
                {
                    segment.b        = pos + dir.normalized * this.m_info.m_generatedInfo.m_size.z;
                    targetNodeId     = netManager.m_segments.m_buffer[(int)position.m_segment].m_startNode;
                    nextTargetNodeId = netManager.m_segments.m_buffer[(int)position.m_segment].m_endNode;
                }
                else
                {
                    segment.b        = pos - dir.normalized * this.m_info.m_generatedInfo.m_size.z;
                    targetNodeId     = netManager.m_segments.m_buffer[(int)position.m_segment].m_endNode;
                    nextTargetNodeId = netManager.m_segments.m_buffer[(int)position.m_segment].m_startNode;
                }
                ushort prevTargetNodeId;
                if (prevOffset == 0)
                {
                    prevTargetNodeId = netManager.m_segments.m_buffer[(int)prevPos.m_segment].m_startNode;
                }
                else
                {
                    prevTargetNodeId = netManager.m_segments.m_buffer[(int)prevPos.m_segment].m_endNode;
                }
                if (targetNodeId == prevTargetNodeId)
                {
#if BENCHMARK
                    using (var bm = new Benchmark(null, "MayChangeSegment")) {
#endif
                    if (!VehicleBehaviorManager.MayChangeSegmentFull(vehicleId, ref VehicleStateManager.Instance.VehicleStates[vehicleId], ref vehicleData, sqrVelocity, false, ref prevPos, ref netManager.m_segments.m_buffer[prevPos.m_segment], prevTargetNodeId, prevLaneID, ref position, targetNodeId, ref netManager.m_nodes.m_buffer[targetNodeId], laneID, ref nextPosition, nextTargetNodeId, out maxSpeed))
                    {
                        return;
                    }
#if BENCHMARK
                }
#endif
                }
            }

            NetInfo info = netManager.m_segments.m_buffer[(int)position.m_segment].Info;
            if (info.m_lanes != null && info.m_lanes.Length > (int)position.m_lane)
            {
                float speedLimit = 1;
#if BENCHMARK
                using (var bm = new Benchmark(null, "GetLockFreeGameSpeedLimit")) {
#endif
                speedLimit = Options.customSpeedLimitsEnabled ? SpeedLimitManager.Instance.GetLockFreeGameSpeedLimit(position.m_segment, position.m_lane, laneID, info.m_lanes[position.m_lane]) : info.m_lanes[position.m_lane].m_speedLimit;
#if BENCHMARK
            }
#endif
                maxSpeed = CalculateTargetSpeed(vehicleId, ref vehicleData, speedLimit, netManager.m_lanes.m_buffer[laneID].m_curve);
            }
            else
            {
                maxSpeed = this.CalculateTargetSpeed(vehicleId, ref vehicleData, 1f, 0f);
            }
        }
Exemple #2
0
        public void CustomCalculateSegmentPosition(ushort vehicleId, ref Vehicle vehicleData, PathUnit.Position nextPosition,
                                                   PathUnit.Position position, uint laneID, byte offset, PathUnit.Position prevPos, uint prevLaneID,
                                                   byte prevOffset, int index, out Vector3 pos, out Vector3 dir, out float maxSpeed)
        {
            var netManager = Singleton <NetManager> .instance;
            ushort targetNodeId;
            ushort nextTargetNodeId;

            if (offset < position.m_offset)
            {
                targetNodeId     = netManager.m_segments.m_buffer[position.m_segment].m_startNode;
                nextTargetNodeId = netManager.m_segments.m_buffer[position.m_segment].m_endNode;
            }
            else
            {
                targetNodeId     = netManager.m_segments.m_buffer[position.m_segment].m_endNode;
                nextTargetNodeId = netManager.m_segments.m_buffer[position.m_segment].m_startNode;
            }
            var prevTargetNodeId = prevOffset == 0 ? netManager.m_segments.m_buffer[prevPos.m_segment].m_startNode : netManager.m_segments.m_buffer[prevPos.m_segment].m_endNode;

            Vehicle.Frame lastFrameData = vehicleData.GetLastFrameData();
            Vector3 lastFrameVehiclePos = lastFrameData.m_position;
            float sqrVelocity           = lastFrameData.m_velocity.sqrMagnitude;

            netManager.m_lanes.m_buffer[laneID].CalculatePositionAndDirection(offset * 0.003921569f, out pos, out dir);

            float braking = this.m_info.m_braking;

            if ((vehicleData.m_flags & Vehicle.Flags.Emergency2) != (Vehicle.Flags) 0)
            {
                braking *= 2f;
            }

            // car position on the Bezier curve of the lane
            var vehiclePosOnBezier = netManager.m_lanes.m_buffer[prevLaneID].CalculatePosition(prevOffset * 0.003921569f);
            //ushort currentSegmentId = netManager.m_lanes.m_buffer[prevLaneID].m_segment;

            // this seems to be like the required braking force in order to stop the vehicle within its half length.
            var crazyValue             = 0.5f * sqrVelocity / braking + m_info.m_generatedInfo.m_size.z * 0.5f;
            bool withinBrakingDistance = Vector3.Distance(lastFrameVehiclePos, vehiclePosOnBezier) >= crazyValue - 1f;

            // NON-STOCK CODE START
#if BENCHMARK
            using (var bm = new Benchmark(null, "UpdateVehiclePosition")) {
#endif
            VehicleStateManager.Instance.UpdateVehiclePosition(vehicleId, ref vehicleData, lastFrameData.m_velocity.magnitude);
#if BENCHMARK
        }
#endif
            // NON-STOCK CODE END

            bool isRecklessDriver = VehicleStateManager.Instance.VehicleStates[vehicleId].recklessDriver;
            if (targetNodeId == prevTargetNodeId && withinBrakingDistance)
            {
                // NON-STOCK CODE START (stock code replaced)
#if BENCHMARK
                using (var bm = new Benchmark(null, "MayChangeSegment")) {
#endif
                //bool isRecklessDriver = VehicleStateManager.Instance.IsRecklessDriver(vehicleId, ref vehicleData); // NON-STOCK CODE

                if (!VehicleBehaviorManager.MayChangeSegmentFull(vehicleId, ref VehicleStateManager.Instance.VehicleStates[vehicleId], ref vehicleData, sqrVelocity, isRecklessDriver, ref prevPos, ref netManager.m_segments.m_buffer[prevPos.m_segment], prevTargetNodeId, prevLaneID, ref position, targetNodeId, ref netManager.m_nodes.m_buffer[targetNodeId], laneID, ref nextPosition, nextTargetNodeId, out maxSpeed))                         // NON-STOCK CODE
                {
                    return;
                }
#if BENCHMARK
            }
#endif
                // NON-STOCK CODE END
            }

            var segmentInfo = netManager.m_segments.m_buffer[position.m_segment].Info;
            if (segmentInfo.m_lanes != null && segmentInfo.m_lanes.Length > position.m_lane)
            {
                // NON-STOCK CODE START
                // NON-STOCK CODE START
                float laneSpeedLimit = 1f;
#if BENCHMARK
                using (var bm = new Benchmark(null, "GetLockFreeGameSpeedLimit")) {
#endif
                if (!Options.customSpeedLimitsEnabled)
                {
                    laneSpeedLimit = segmentInfo.m_lanes[position.m_lane].m_speedLimit;
                }
                else
                {
                    // === START INLINED VERSION OF SpeedLimitManager.GetLockFreeGameSpeedLimit ===
                    ushort?[] fastArray = Flags.laneSpeedLimitArray[position.m_segment];
                    if (fastArray != null && fastArray.Length > position.m_lane && fastArray[position.m_lane] != null)
                    {
                        // === START INLINED VERSION OF SpeedLimitManager.ToGameSpeedLimit ===
                        laneSpeedLimit = (float)fastArray[position.m_lane];
                        if (laneSpeedLimit == 0)
                        {
                            laneSpeedLimit = SpeedLimitManager.MAX_SPEED;
                        }
                        else
                        {
                            laneSpeedLimit = laneSpeedLimit / 50f;
                        }
                        // === END INLINED VERSION OF SpeedLimitManager.ToGameSpeedLimit ===
                        // laneSpeedLimit = ToGameSpeedLimit((ushort)fastArray[position.m_lane]);
                    }
                    else
                    {
                        laneSpeedLimit = segmentInfo.m_lanes[position.m_lane].m_speedLimit;
                    }
                    // === END INLINED VERSION OF SpeedLimitManager.GetLockFreeGameSpeedLimit ===
                    //laneSpeedLimit = SpeedLimitManager.Instance.GetLockFreeGameSpeedLimit(position.m_segment, position.m_lane, laneID, segmentInfo.m_lanes[position.m_lane]); // info2.m_lanes[position.m_lane].m_speedLimit; // NON-STOCK CODE
                }
#if BENCHMARK
            }
#endif
                // NON-STOCK CODE END
                maxSpeed = CalculateTargetSpeed(vehicleId, ref vehicleData, laneSpeedLimit, netManager.m_lanes.m_buffer[laneID].m_curve);
            }
            else
            {
                maxSpeed = CalculateTargetSpeed(vehicleId, ref vehicleData, 1f, 0f);
            }

            // NON-STOCK CODE START (stock code replaced)
#if BENCHMARK
            using (var bm = new Benchmark(null, "CalcMaxSpeed")) {
#endif
            // === START INLINED VERSION OF VehicleBehaviorManager.CalcMaxSpeed ===
            if (Singleton <NetManager> .instance.m_treatWetAsSnow)
            {
                DistrictManager districtManager = Singleton <DistrictManager> .instance;
                byte district = districtManager.GetDistrict(pos);
                DistrictPolicies.CityPlanning cityPlanningPolicies = districtManager.m_districts.m_buffer[(int)district].m_cityPlanningPolicies;
                if ((cityPlanningPolicies & DistrictPolicies.CityPlanning.StuddedTires) != DistrictPolicies.CityPlanning.None)
                {
                    if (Options.strongerRoadConditionEffects)
                    {
                        if (maxSpeed > VehicleBehaviorManager.ICY_ROADS_STUDDED_MIN_SPEED)
                        {
                            maxSpeed = VehicleBehaviorManager.ICY_ROADS_STUDDED_MIN_SPEED + (float)(255 - netManager.m_segments.m_buffer[position.m_segment].m_wetness) * 0.0039215686f * (maxSpeed - VehicleBehaviorManager.ICY_ROADS_STUDDED_MIN_SPEED);
                        }
                    }
                    else
                    {
                        maxSpeed *= 1f - (float)netManager.m_segments.m_buffer[position.m_segment].m_wetness * 0.0005882353f;                                 // vanilla: -15% .. ±0%
                    }
                    districtManager.m_districts.m_buffer[(int)district].m_cityPlanningPoliciesEffect |= DistrictPolicies.CityPlanning.StuddedTires;
                }
                else
                {
                    if (Options.strongerRoadConditionEffects)
                    {
                        if (maxSpeed > VehicleBehaviorManager.ICY_ROADS_MIN_SPEED)
                        {
                            maxSpeed = VehicleBehaviorManager.ICY_ROADS_MIN_SPEED + (float)(255 - netManager.m_segments.m_buffer[position.m_segment].m_wetness) * 0.0039215686f * (maxSpeed - VehicleBehaviorManager.ICY_ROADS_MIN_SPEED);
                        }
                    }
                    else
                    {
                        maxSpeed *= 1f - (float)netManager.m_segments.m_buffer[position.m_segment].m_wetness * 0.00117647066f;                                 // vanilla: -30% .. ±0%
                    }
                }
            }
            else
            {
                if (Options.strongerRoadConditionEffects)
                {
                    float minSpeed = Math.Min(maxSpeed * VehicleBehaviorManager.WET_ROADS_FACTOR, VehicleBehaviorManager.WET_ROADS_MAX_SPEED);                             // custom: -25% .. 0
                    if (maxSpeed > minSpeed)
                    {
                        maxSpeed = minSpeed + (float)(255 - netManager.m_segments.m_buffer[position.m_segment].m_wetness) * 0.0039215686f * (maxSpeed - minSpeed);
                    }
                }
                else
                {
                    maxSpeed *= 1f - (float)netManager.m_segments.m_buffer[position.m_segment].m_wetness * 0.0005882353f;                             // vanilla: -15% .. ±0%
                }
            }

            if (Options.strongerRoadConditionEffects)
            {
                float minSpeed = Math.Min(maxSpeed * VehicleBehaviorManager.BROKEN_ROADS_FACTOR, VehicleBehaviorManager.BROKEN_ROADS_MAX_SPEED);
                if (maxSpeed > minSpeed)
                {
                    maxSpeed = minSpeed + (float)netManager.m_segments.m_buffer[position.m_segment].m_condition * 0.0039215686f * (maxSpeed - minSpeed);
                }
            }
            else
            {
                maxSpeed *= 1f + (float)netManager.m_segments.m_buffer[position.m_segment].m_condition * 0.0005882353f;                         // vanilla: ±0% .. +15 %
            }

            // === START INLINED VERSION OF VehicleBehaviorManager.ApplyRealisticSpeeds ===
            if (Options.realisticSpeeds)
            {
                // === START INLINED VERSION OF VehicleBehaviorManager.ApplyRealisticSpeeds ===
                float vehicleRand = 0.01f * (float)(vehicleId % 100);
                // float vehicleRand = 0.01f * (float)GetVehicleRand(vehicleId);
                // === END INLINED VERSION OF VehicleBehaviorManager.ApplyRealisticSpeeds ===
                if (this.m_info.m_isLargeVehicle)
                {
                    maxSpeed *= 0.9f + vehicleRand * 0.1f;                             // a little variance, 0.9 .. 1
                }
                else if (isRecklessDriver)
                {
                    maxSpeed *= 1.3f + vehicleRand * 1.7f;                             // woohooo, 1.3 .. 3
                }
                else
                {
                    maxSpeed *= 0.8f + vehicleRand * 0.5f;                             // a little variance, 0.8 .. 1.3
                }
            }
            else if (isRecklessDriver)
            {
                maxSpeed *= 1.5f;
            }
            // === END INLINED VERSION OF VehicleBehaviorManager.ApplyRealisticSpeeds ===

            //maxSpeed = ApplyRealisticSpeeds(maxSpeed, vehicleId, this.m_info, isRecklessDriver);
            maxSpeed = Math.Max(VehicleBehaviorManager.MIN_SPEED, maxSpeed);                     // at least 10 km/h
            // === END INLINED VERSION OF VehicleBehaviorManager.CalcMaxSpeed ===
            //maxSpeed = VehicleBehaviorManager.Instance.CalcMaxSpeed(vehicleId, this.m_info, position, ref netManager.m_segments.m_buffer[position.m_segment], pos, maxSpeed, isRecklessDriver);
#if BENCHMARK
        }
#endif
            // NON-STOCK CODE END
        }