Example #1
0
    public void Update()
    {
        if (Main.IsPaused(PauseFlags.TimeOut | PauseFlags.Ai))
        {
            return;
        }
        // HACK: don't update for player
        if (this.Team == Team.PlayerTeam)
        {
            return;
        }

        _FSM.Update();
        Steering.DoUpdate();
    }
Example #2
0
    // Update section
    // ---------------------------------------------------------------------------------------------


    // Read the standard input values and translate them to the internal blocks.
    // Also set the state values expected by each block.
    // Called before each integration step.

    protected override void DoUpdateBlocks()
    {
        // Collect input and settings

        int[] inputData    = data.Get(Channel.Input);
        int[] settingsData = data.Get(Channel.Settings);

        float brakePosition     = Mathf.Clamp01(inputData[InputData.Brake] / 10000.0f);
        float handbrakePosition = Mathf.Clamp01(inputData[InputData.Handbrake] / 10000.0f);
        float throttlePosition  = Mathf.Clamp01(inputData[InputData.Throttle] / 10000.0f);
        float steerPosition     = Mathf.Clamp(inputData[InputData.Steer] / 10000.0f, -1.0f, 1.0f);

        int automaticGearInput = inputData[InputData.AutomaticGear];
        int ignitionInput      = inputData[InputData.Key];

        // Process gear mode preventing direction changes when the vehicle is not stopped

        m_gearMode = Mathf.Clamp(automaticGearInput, (int)Gearbox.AutomaticGear.R, (int)Gearbox.AutomaticGear.D);

        if (m_gearMode != m_prevGearMode)
        {
            if (m_gearMode == (int)Gearbox.AutomaticGear.D && speed < -gearChangeMaxSpeed ||
                m_gearMode == (int)Gearbox.AutomaticGear.R && speed > gearChangeMaxSpeed)
            {
                m_gearMode = m_prevGearMode;
            }
            else
            {
                m_prevGearMode = m_gearMode;
            }
        }

        // No throttle if the vehicle is switched off.

        if (ignitionInput < 0)
        {
            throttlePosition = 0.0f;
        }
        else
        {
            throttlePosition = SpeedControl.GetThrottle(speedControl, inputData, data.Get(Channel.Vehicle));
        }

        // Process inputs
        // Input settings are configured in the car independently of the torque maps.
        // Being in a separate class allows all intermediate steps to be traced separately
        // (pedal > input > electrical torque > mechanical torque > wheel torque)

        int gear = m_gearMode - (int)Gearbox.AutomaticGear.N;

        m_throttleInput = input.GetThrottleInput(throttlePosition);
        m_brakePressure = input.GetBrakePressure(brakePosition);
        if (m_brakePressure > brakePressureThreshold)
        {
            m_throttleInput = 0.0f;
        }

        m_frontPowertrain.SetInputs(gear, m_throttleInput, m_brakePressure);
        m_rearPowertrain.SetInputs(gear, m_throttleInput, m_brakePressure);

        // Traction control

        if (gear != 0)
        {
            // Independent traction control per axle

            /*
             * int tcsOverride = settingsData[SettingsData.TcsOverride];
             * if (tractionControl.enabled && tcsOverride != 2 || tcsOverride == 1)
             *      {
             *      float frontFinalRatio = frontDifferential.gearRatio * Mathf.Sign(gear);
             *      float rearFinalRatio = rearDifferential.gearRatio * Mathf.Sign(gear);
             *      float maxFrontRpm = TractionControl.GetMaxEngineRpm(this, tractionControl, 0, 1, frontFinalRatio, maxDriveRpm);
             *      float maxRearRpm = TractionControl.GetMaxEngineRpm(this, tractionControl, 2, 3, rearFinalRatio, maxDriveRpm);
             *
             *      // TO-DO: electric motors
             *      // m_frontPowertrain.directDrive.maxRpm = maxFrontRpm;
             *      // m_rearPowertrain.directDrive.maxRpm = maxRearRpm;
             *      }
             */
            // Apply motor input

            /*
             * float motorInput = throttlePosition * Mathf.Sign(gear);
             * m_rearPowertrain.electricMotor.motorInput = motorInput * frontToRearBalance;
             * m_frontPowertrain.electricMotor.motorInput = motorInput * (1.0f - frontToRearBalance);
             */
        }

        // Steering

        if (settingsData[SettingsData.SteeringAidsOverride] != 2)
        {
            SteeringAids.Apply(this, steering, steeringAids, ref steerPosition);
        }
        m_steering.steerInput = steerPosition;
        m_steering.DoUpdate();

        // Track changes in the inertia settings

        m_inertia.DoUpdate(cachedRigidbody);

        // Differential overrides

        m_frontPowertrain.differentialOverride = (Powertrain.DifferentialOverride)settingsData[SettingsData.DifferentialLock];
        m_rearPowertrain.differentialOverride  = (Powertrain.DifferentialOverride)settingsData[SettingsData.DifferentialLock];
    }