-- This Source Code Form is subject to the terms of the bCDDL, v. 1.1.
-- If a copy of the bCDDL was not distributed with this
-- file, You can obtain one at http://beamng.com/bCDDL-1.1.txt
local M = {}
local ceil = math.ceil
local min = math.min
local max = math.max
local rpmSmoother = newTemporalSigmoidSmoothing(50000, 75000, 50000, 75000, 0)
local lightsState = 0
local lightsSavedState = 0
local signalRightState = false
local signalLeftState = false
local signalWarnState = false
local lightbarState = 0
local hornState = false
local fogLightsState = false
local blinkPulse = false
local blinkTimerThreshold = 0.4
local blinkTimer = 0
-- sounds
local hornSound1
local hornSound2
local sirenSound
local indStartSnd
local indStopSnd
local indLoopSnd1
local indLoopSnd2
local lightOn
local lightOff
local hasSteered = false -- used to see whether right/left-turn has been finished
-- set to nop in the beginning - this avoids conflict with the warn signal
local automatic_indicator_stop = nop
local generateBlinkPulse = nop
local ignitionLevelSanitization
local previousIgnitionLevel
local updateElectricsWithIgnitionLevel --used to alter the electrics output based on ignition state, made to support multiple different countries/regulations
local ignitionHoldingStarter = false
local ignitionHoldingStarterTimer = 0
local ignitionWasStartingEngine = false
local ignitionMessageLookup = {
[0] = "ui.common.vehicleOff",
[1] = "ui.common.vehicleAccessoryOn",
[2] = "ui.common.vehicleOn"
}
local function generateBlinkPulseFun(dt)
blinkTimer = blinkTimer + dt
if blinkTimer > blinkTimerThreshold then
if blinkPulse then
indLoopSnd1 = indLoopSnd1 or sounds.createSoundscapeSound("indLoop1")
sounds.playSoundSkipAI(indLoopSnd1)
else
indLoopSnd2 = indLoopSnd2 or sounds.createSoundscapeSound("indLoop2")
sounds.playSoundSkipAI(indLoopSnd2)
end
blinkPulse = not blinkPulse
blinkTimer = 0
end
end
local function updateSignals()
generateBlinkPulse = (signalLeftState or signalRightState) and generateBlinkPulseFun or nop
end
-- stops automatically indicator if turn has been finished or if wheel is steered in opposite direction
local function manage_automatic_indicator_stop()
local controlPoint = 100
local steering = M.values.steering
if steering == nil then
return
end
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
--check whether user has steered in the desired direction
if signalLeftState and steering > controlPoint then
hasSteered = true
elseif signalRightState and steering < -controlPoint then
hasSteered = true
end
--if the wheel has returned to the neutral position, turn indicator off
if signalLeftState and hasSteered and steering <= 0 then
signalLeftState = false
hasSteered = false
sounds.playSoundSkipAI(indStopSnd)
automatic_indicator_stop = nop
elseif signalRightState and hasSteered and steering >= 0 then
signalRightState = false
hasSteered = false
sounds.playSoundSkipAI(indStopSnd)
automatic_indicator_stop = nop
end
updateSignals()
end
local function stop_turn_signal()
if not signalWarnState then
if signalLeftState or signalRightState then
signalLeftState = false
signalRightState = false
hasSteered = false
sounds.playSoundSkipAI(indStopSnd)
automatic_indicator_stop = nop
end
code
updateSignals()
end
end
-- user input functions
local function toggle_left_signal()
if not signalWarnState then
signalLeftState = not signalLeftState
else
signalLeftState = true
end
if signalLeftState then
signalRightState = false
signalWarnState = false
indStartSnd = indStartSnd or sounds.createSoundscapeSound("indicatorStart")
sounds.playSoundSkipAI(indStartSnd)
automatic_indicator_stop = manage_automatic_indicator_stop
indStopSnd = indStopSnd or sounds.createSoundscapeSound("indicatorStop")
end
if not signalLeftState then
sounds.playSoundSkipAI(indStopSnd)
automatic_indicator_stop = nop
hasSteered = false
end
updateSignals()
end
local function toggle_right_signal()
if not signalWarnState then
signalRightState = not signalRightState
else
signalRightState = true
end
if signalRightState then
signalLeftState = false
signalWarnState = false
indStartSnd = indStartSnd or sounds.createSoundscapeSound("indicatorStart")
sounds.playSoundSkipAI(indStartSnd)
automatic_indicator_stop = manage_automatic_indicator_stop
indStopSnd = indStopSnd or sounds.createSoundscapeSound("indicatorStop")
end
if not signalRightState then
automatic_indicator_stop = nop
sounds.playSoundSkipAI(indStopSnd)
hasSteered = false
end
updateSignals()
end
local function toggleSound(val, snd)
if not snd then
return
end
if val then
obj:setVolume(snd, 1)
obj:playSFX(snd)
else
obj:stopSFX(snd)
end
end
local function updateIgnitionStarter(dt)
if ignitionHoldingStarter then
ignitionHoldingStarterTimer = ignitionHoldingStarterTimer + dt
if ignitionHoldingStarterTimer > 0.5 then
if not ignitionWasStartingEngine then
ignitionWasStartingEngine = true
M.setIgnitionLevel(3)
end
else
ignitionWasStartingEngine = false
end
else
ignitionHoldingStarterTimer = 0
end
end
local function updateGFX(dt)
updateIgnitionStarter(dt)
generateBlinkPulse(dt)
local vals = M.values
-- the primary source values
--mixed values for american style indicators/lights
vals.lowhighbeam_signal_R = vals.signal_right_input == 1 and (blinkPulse and 1 or 0) or ceil(vals.lowhighbeam and 1 or 0)
vals.lowhighbeam_signal_L = vals.signal_left_input == 1 and (blinkPulse and 1 or 0) or ceil(vals.lowhighbeam and 1 or 0)
--wigwag lights
--desired behavior: highbeam is controlled by normal highbeam values if lightbar is OFF
--if it's on, only the wigwag signal has control over the highbeam
local lightbarActive = vals.lightbar > 0
local wigwagRActive = vals.wigwag_R == 1
local wigwagLActive = vals.wigwag_L == 1
local highbeamActive = vals.highbeam
vals.highbeam_wigwag_R = ((highbeamActive and not lightbarActive) or (wigwagRActive)) and 1 or 0
vals.highbeam_wigwag_L = ((highbeamActive and not lightbarActive) or (wigwagLActive)) and 1 or 0
vals.reverse_wigwag_R = vals.wigwag_R == 1 or ceil(vals.reverse and 1 or 0)
vals.reverse_wigwag_L = vals.wigwag_L == 1 or ceil(vals.reverse and 1 or 0)
local rpm = vals.rpm
vals.rpmTacho = rpmSmoother:get(rpm, dt)
vals.rpmspin = (vals.rpmspin + dt * rpm * 6) % 360 --make sure to convert properly between the units here
vals.boost = (vals.turboBoost or 0) + (vals.superchargerBoost or 0)
vals.boostMax = max((vals.turboBoostMax or 0), (vals.superchargerBoostMax or 0))
-- inject imported electrics events first time, this needs to happen twice overall so that code between gfx first step and gfx second step can see these updated electrics
beamstate.updateRemoteElectrics(true)
for f, v in pairs(vals) do
if M.disabledState[f] ~= nil then
vals[f] = nil
else
if type(v) == "boolean" then
vals[f] = vals[f] and 1 or 0
end
end
end
for f, s in pairs(smoothers) do
if vals[f] ~= nil then
vals[f] = s:get(vals[f])
end
end
end
local function updateGFXSecondStep(dt)
local values = M.values
--make sure to update the brakelights value based on the brake value
values.brakelights = values.brakelights or ceil(values.brake)
values.brakelight_signal_R = values.signal_right_input == 1 and (blinkPulse and 1 or 0) or ceil(values.brakelights or values.brake or 0)
values.brakelight_signal_L = values.signal_left_input == 1 and (blinkPulse and 1 or 0) or ceil(values.brakelights or values.brake or 0)
-- inject imported electrics events second time, this should be the last step before update electrics by ignition state so that we can override everything if needed
beamstate.updateRemoteElectrics(false)
updateElectricsWithIgnitionLevel()
end
local function updateElectricsWithIgnitionLevelEuropean()
local values = M.values
if values.ignitionLevel == 0 then
--adjustments in "off" mode
values.brakelights = 0
values.parkingbrakelight = 0
values.brakelight_signal_R = 0
values.brakelight_signal_L = 0
elseif values.ignitionLevel == 1 then
--adjustments in "accessory" mode
else
--adjustments in "on" mode
end
end
local function sanitizeIgnitionLevel(desiredIgnitionLevel)
--return the actual ignition level for a desired one so that certain levels can be "forbidden"
return ignitionLevelSanitization[desiredIgnitionLevel] or 0
end
local function setAllowedIgnitionLevels(allowedIgnitionLevels)
if #allowedIgnitionLevels <= 0 then --make sure that we always have at least one allowed ignition level
allowedIgnitionLevels = {0}
--print warning
end
local allowedIgnitionLevelsLookup = {}
for _, level in ipairs(allowedIgnitionLevels) do
allowedIgnitionLevelsLookup[level] = true
end
for i = 0, 3, 1 do --for every possible ignition level
if not allowedIgnitionLevelsLookup[i] then --check if it's currently allowed
for j = min(i + 1, 3), 3, 1 do --if not, first try to find anything higher that is allowed
if allowedIgnitionLevelsLookup[j] then
ignitionLevelSanitization[i] = j
break
end
end
code
if not allowedIgnitionLevelsLookup[i] then --if we couldn't find anything higher allowed
for j = max(i - 1, 0), 0, -1 do --try to find anything lower that is allowed...
if allowedIgnitionLevelsLookup[j] then
ignitionLevelSanitization[i] = j
break
end
end
end
end
local allowedIgnitionLevels = (v.data.electrics and v.data.electrics.allowedIgnitionLevels) or {0, 1, 2, 3} --read allowed ingition levels from jbeam or use all of them by default
setAllowedIgnitionLevels(allowedIgnitionLevels)
local spawnVehicleIgnitionLevel = settings.getValue("spawnVehicleIgnitionLevel") or 3
if v.config.additionalVehicleData and v.config.additionalVehicleData.spawnWithEngineRunning ~= nil then
spawnVehicleIgnitionLevel = v.config.additionalVehicleData.spawnWithEngineRunning and 3 or 0
end
M.values.ignitionLevel = sanitizeIgnitionLevel(spawnVehicleIgnitionLevel)
previousIgnitionLevel = spawnVehicleIgnitionLevel - 1
local ignitionLevelOverrideType = (v.data.electrics and v.data.electrics.ignitionLevelOverrideType) or "european"
if ignitionLevelOverrideType == "european" then
updateElectricsWithIgnitionLevel = updateElectricsWithIgnitionLevelEuropean
elseif ignitionLevelOverrideType == "none" then
updateElectricsWithIgnitionLevel = nop
end
end
local function init()
reset()
end
local function initLastStage()
if M.values.ignitionLevel == 3 then
M.values.ignitionLevel = 2
previousIgnitionLevel = 3
end
end
local function set_warn_signal(value)
signalWarnState = value
signalRightState = signalWarnState
signalLeftState = signalWarnState
automatic_indicator_stop = nop
updateSignals()
end
local function toggle_warn_signal()
set_warn_signal(not signalWarnState)
end
local function toggle_lights()
lightsState = lightsState + 1
if lightsState == 1 then
lightOn = lightOn or sounds.createSoundscapeSound("LightOn")
sounds.playSoundSkipAI(lightOn)
elseif lightsState == 2 then
lightOn = lightOn or sounds.createSoundscapeSound("LightOn")
sounds.playSoundSkipAI(lightOn)
elseif lightsState == 3 then
lightsState = 0
lightOff = lightOff or sounds.createSoundscapeSound("LightOff")
sounds.playSoundSkipAI(lightOff)
end
end
local function light_flash_highbeams(enabled)
if enabled then
lightsSavedState = lightsState
lightsState = 2
else
lightsState = lightsSavedState
end
end
local function set_lightbar_signal(state)
if state == lightbarState then
return
end
sirenSound = sirenSound or sounds.createSoundscapeSound("siren")
lightbarState = state % (sirenSound ~= nil and 3 or 2)
-- 1 = lights, no sound
-- 2 = lights + sound
toggleSound(lightbarState == 2, sirenSound)
end
local function toggle_lightbar_signal()
set_lightbar_signal(lightbarState + 1)
end
local function setLightsState(newval)
lightsState = newval
end
local function toggle_fog_lights()
fogLightsState = not fogLightsState
end
local function set_fog_lights(state)
fogLightsState = state
end
local function horn(state)
hornState = state
if hornState then
hornSound1 = hornSound1 or sounds.createSoundscapeSound("horn")
if not hornSound1 then return end
if hornSound2 then
obj:setVolume(hornSound2, 0)
end
obj:setVolume(hornSound1, 1)
obj:playSFX(hornSound1)
else
if hornSound1 then obj:stopSFX(hornSound1) end
hornSound1, hornSound2 = hornSound2, hornSound1
end
end
local function setIgnitionLevel(ignitionLevel)
--print("Ignition Level: " .. ignitionLevel)
previousIgnitionLevel = M.values.ignitionLevel
local newIgnitionLevel = sanitizeIgnitionLevel(ignitionLevel)
M.values.ignitionLevel = newIgnitionLevel
if ignitionMessageLookup[newIgnitionLevel] then
guihooks.message({txt = ignitionMessageLookup[newIgnitionLevel], context = {}}, 4, "vehicle.ignition.ignitionLevel")
end
if newIgnitionLevel == 0 or newIgnitionLevel == 1 then
controller.mainController.setEngineIgnition(false)
controller.mainController.setStarter(false)
elseif newIgnitionLevel == 2 then
controller.mainController.setEngineIgnition(true)
controller.mainController.setStarter(false)
elseif newIgnitionLevel == 3 then
controller.mainController.setEngineIgnition(true)
controller.mainController.setStarter(true)
end
end
local function toggleIgnitionLevelOnDown()
ignitionHoldingStarter = true
end
local function toggleIgnitionLevelOnUp()
if ignitionWasStartingEngine and M.values.ignitionLevel < 3 then
--this case happens when the vehicle controller already switched to level 2 by itself after the engine fired up
--make sure to pretend that we are still in level 3 so that the following logic works. Not nice, but couldn't find a better solution that actually works
--this logic is also needed so that we can set level 3 from external (eg GE) and have the engine start autoamtically and go back to level 2 after doing so
--(vehicle controller switches to level 2 after engine fired up)
setIgnitionLevel(3)
end
local currentIgnitionLevel = M.values.ignitionLevel
ignitionHoldingStarter = false
ignitionWasStartingEngine = false
if currentIgnitionLevel == 0 then
setIgnitionLevel(1)
elseif currentIgnitionLevel == 1 then
if previousIgnitionLevel < currentIgnitionLevel then
setIgnitionLevel(2)
else
setIgnitionLevel(0)
end
elseif currentIgnitionLevel == 2 then
setIgnitionLevel(1)
elseif currentIgnitionLevel == 3 then
setIgnitionLevel(2)
end
end
используя этот код выпиши мне все переменные electrics в формате
local имяпеременной -- ед.изм (rpm/0 or 1/0 to 1/number/Degrees/etc) ---- описание переменной
local имяпеременной -- ед.изм (rpm/0 or 1/0 to 1/number/Degrees/etc) ---- описание переменной
etc
Конечно, вот полный список переменных electrics из предоставленного кода с форматированием:
lua
local throttle -- number (0 to 1) ---- Throttle position
local brake -- number (0 to 1) ---- Brake position
local steering -- number (-1 to 1) ---- Steering position
local clutch -- number (0 to 1) ---- Clutch position
local wheelspeed -- number (m/s) ---- Wheel speed
local odometer -- number (km) ---- Odometer value
local avgWheelAV -- number (rpm) ---- Average wheel angular velocity
local airspeed -- number (m/s) ---- Airspeed
local horn -- 0 or 1 ---- Horn state
local rpmspin -- number (Degrees) ---- RPM spin angle
local rpm -- rpm ---- Engine RPM
local boost -- number (Bar) ---- Boost pressure
local boostMax -- number (Bar) ---- Maximum boost pressure
local brakelights -- 0 or 1 ---- Brake lights state
local nop -- number ---- Placeholder variable for non-operational states
local parkingbrake -- number (0 to 1) ---- Parking brake position
local parkingbrakelight -- 0 or 1 ---- Parking brake light state
local lights -- 0 to 2 ---- Lights state (0: Off, 1: Low beam, 2: High beam)
local lights_state -- 0 to 2 ---- Duplicate of lights variable for clarity
local turnsignal -- -1/0/1 ---- Turn signal state (-1: Left, 0: Off, 1: Right)
local turnsignalEnabled -- 0 or 1 ---- Whether turn signal is enabled
local accXSmooth -- number (m/s^2) ---- Smoothed acceleration in the X-axis
local accYSmooth -- number (m/s^2) ---- Smoothed acceleration in the Y-axis
local accZSmooth -- number (m/s^2) ---- Smoothed acceleration in the Z-axis
local trip -- number (km) ---- Trip value
local highbeam -- 0 or 1 ---- High beam state
local fog -- 0 or 1 ---- Fog lights state
local lightbar -- 0 or 1 ---- Lightbar lights state
local hazard -- 0 or 1 ---- Hazard lights state (blinking)
local hazard_enabled -- 0 or 1 ---- Hazard lights enabled or disabled
local airflowspeed -- number (m/s) ---- Airflow speed
local altitude -- number (m) ---- Altitude
local oiltemp -- number (°C) ---- Oil temperature
local watertemp -- number (°C) ---- Water temperature
local oil -- 0 or 1 ---- Oil warning light (On if temperature exceeds threshold)
local lowpressure -- 0 or 1 ---- Tire low-pressure warning light
local parking -- 0 or 1 ---- Parking lights state
local reverse -- 0 or 1 ---- Reverse lights state
local signal_R -- 0 or 1 ---- Right turn signal (blinking with pulse)
local signal_L -- 0 or 1 ---- Left turn signal (blinking with pulse)
local lowbeam -- 0 or 1 ---- Low beam lights state
local lowhighbeam -- 0 or 1 ---- Low and high beam combined light state
local lowhighbeam_signal_R -- 0 or 1 ---- Combined low/high beam and right signal light state
local lowhighbeam_signal_L -- 0 or 1 ---- Combined low/high beam and left signal light state
local highbeam_wigwag_R -- 0 or 1 ---- High beam wig-wag (blinking) right light state
local highbeam_wigwag_L -- 0 or 1 ---- High beam wig-wag (blinking) left light state
local reverse_wigwag_R -- 0 or 1 ---- Reverse lights and wig-wag right light state
local reverse_wigwag_L -- 0 or 1 ---- Reverse lights and wig-wag left light state
local gearIndex -- number ---- Gear index
local fuel -- number (0 to 1) ---- Fuel level
local lowfuel -- 0 or 1 ---- Low fuel warning light
local abs -- 0 or 1 ---- ABS light indicator
local esc -- 0 or 1 ---- ESC light indicator
local tcs -- 0 or 1 ---- TCS light indicator
local checkengine -- 0 or 1 ---- Check engine light indicator
local ignition -- 0 or 1 ---- Ignition state
local running -- 0 or 1 ---- Engine running state
local engineRunning -- 0 or 1 ---- Engine running state (duplicate for clarity)
local ignitionLevel -- 0 to 3 ---- Ignition levels (0: Off, 1: Accessory, 2: On, 3: Start)
Описание переменных может быть уточнено в зависимости от контекста или дополнительных данных, которые могут быть доступны вне фрагмента кода.