diff --git a/lua/ge/extensions/dsxAdvancedHaptic.lua b/lua/ge/extensions/dsxAdvancedHaptic.lua new file mode 100644 index 0000000..ff7d301 --- /dev/null +++ b/lua/ge/extensions/dsxAdvancedHaptic.lua @@ -0,0 +1,130 @@ +local socket = require("socket") +local Triggers = require("dsxTriggerEnum") + +local M = {} + +local udp = socket.udp() + +local address = "127.0.0.1" +local port = 6969 + +local state = { + throttle = 0, + brake = 0, + isABSActive = false, + airSpeedKmh = 0, + beamDamage = 0, +} + +local function packetToJson(packet) + return jsonEncode(packet) +end + +local function send(data) + local jsonData = packetToJson(data) + udp:sendto(jsonData, address, port) +end + +-- modified lerp with bias toward 'a' +function lerp(a, b, t) + return a + (b - a) * 0.5 * t +end + +function inverseLerp(a, b, v) + if a ~= b then + return (v - a) / (b - a) + end + return 0 +end + +local function handleLeftTrigger() + local startOfResistanceBrake = 1 * lerp(0.4, 1, state.brake) + local startOfResistance = lerp(25, 175, startOfResistanceBrake) + local airSpeed = math.min(100, inverseLerp(0, 100, state.airSpeedKmh)) + local amountOfForceExcerted = math.min(255, lerp(50, 255, airSpeed)) + + if not state.isABSActive then + return { + type = Triggers.InstructionType.TriggerUpdate, + parameters = { + 0, + Triggers.Trigger.Left, + Triggers.TriggerMode.CustomTriggerValue, + Triggers.CustomTriggerValueMode.Rigid, + startOfResistance, + amountOfForceExcerted, + 255, + 0, + 0, + 0, + 0 + } + } + else + return { + type = Triggers.InstructionType.TriggerUpdate, + parameters = { + 0, + Triggers.Trigger.Left, + Triggers.TriggerMode.CustomTriggerValue, + Triggers.CustomTriggerValueMode.VibrateResistanceAB, + 255, + 255, + 255, + 255, + 255, + 255, + 10 + } + } + end +end + +local function handleRightTrigger() + local startOfResistanceThrottle = 1 * lerp(0.4, 1, state.throttle) + local startOfResistance = lerp(50, 175, startOfResistanceThrottle) + local totalForceExcerted = 25 + + return { + type = Triggers.InstructionType.TriggerUpdate, + parameters = { + 0, + Triggers.Trigger.Right, + Triggers.TriggerMode.CustomTriggerValue, + Triggers.CustomTriggerValueMode.Rigid, + startOfResistance, + totalForceExcerted, + 255, + 0, + 0, + 0, + 0 + } + } +end + +local function handleVehicleData() + local instructions = {} + + table.insert(instructions, handleLeftTrigger()) + table.insert(instructions, handleRightTrigger()) + + send( + { + instructions = instructions + } + ) +end + +local function dsxAdvancedUpdate(throttle, brake, isABSActive, airSpeedKmh, beamDamage) + state.throttle = throttle + state.brake = brake + state.isABSActive = isABSActive + state.airSpeedKmh = airSpeedKmh + state.beamDamage = beamDamage + + handleVehicleData() +end + +M.dsxAdvancedUpdate = dsxAdvancedUpdate +return M diff --git a/lua/ge/extensions/dsxTriggerEnum.lua b/lua/ge/extensions/dsxTriggerEnum.lua new file mode 100644 index 0000000..b58e384 --- /dev/null +++ b/lua/ge/extensions/dsxTriggerEnum.lua @@ -0,0 +1,64 @@ +local Triggers = {} + +-- TriggerMode Enum +Triggers.TriggerMode = { + Normal = 0, + GameCube = 1, + VerySoft = 2, + Soft = 3, + Hard = 4, + VeryHard = 5, + Hardest = 6, + Rigid = 7, + VibrateTrigger = 8, + Choppy = 9, + Medium = 10, + VibrateTriggerPulse = 11, + CustomTriggerValue = 12, + Resistance = 13, + Bow = 14, + Galloping = 15, + SemiAutomaticGun = 16, + AutomaticGun = 17, + Machine = 18 +} + +-- CustomTriggerValueMode Enum +Triggers.CustomTriggerValueMode = { + OFF = 0, + Rigid = 1, + RigidA = 2, + RigidB = 3, + RigidAB = 4, + Pulse = 5, + PulseA = 6, + PulseB = 7, + PulseAB = 8, + VibrateResistance = 9, + VibrateResistanceA = 10, + VibrateResistanceB = 11, + VibrateResistanceAB = 12, + VibratePulse = 13, + VibratePulseA = 14, + VibratePulsB = 15, + VibratePulseAB = 16 +} + +-- Trigger Enum +Triggers.Trigger = { + Invalid = 0, + Left = 1, + Right = 2 +} + +-- InstructionType Enum +Triggers.InstructionType = { + Invalid = 0, + TriggerUpdate = 1, + RGBUpdate = 2, + PlayerLED = 3, + TriggerThreshold = 4 +} + +-- This is the representation of your enums in Lua tables +return Triggers \ No newline at end of file diff --git a/lua/vehicle/extensions/auto/dsxAdvancedHapticVehicleDispatcher.lua b/lua/vehicle/extensions/auto/dsxAdvancedHapticVehicleDispatcher.lua new file mode 100644 index 0000000..774fc56 --- /dev/null +++ b/lua/vehicle/extensions/auto/dsxAdvancedHapticVehicleDispatcher.lua @@ -0,0 +1,13 @@ +local M = {} + +local function updateGFX(dt) + if not playerInfo.firstPlayerSeated then + return + end + + obj:queueGameEngineLua(string.format("extensions.hook('dsxAdvancedUpdate', %f, %f, %s, %f, %f)", input.state.throttle.val, input.state.brake.val, electrics.values.absActive == 1, electrics.values.airspeed * 3.6, beamstate.damage)) +end + +M.updateGFX = updateGFX + +return M diff --git a/lua/vehicle/extensions/auto/dsxTriggerEnum.lua b/lua/vehicle/extensions/auto/dsxTriggerEnum.lua new file mode 100644 index 0000000..b58e384 --- /dev/null +++ b/lua/vehicle/extensions/auto/dsxTriggerEnum.lua @@ -0,0 +1,64 @@ +local Triggers = {} + +-- TriggerMode Enum +Triggers.TriggerMode = { + Normal = 0, + GameCube = 1, + VerySoft = 2, + Soft = 3, + Hard = 4, + VeryHard = 5, + Hardest = 6, + Rigid = 7, + VibrateTrigger = 8, + Choppy = 9, + Medium = 10, + VibrateTriggerPulse = 11, + CustomTriggerValue = 12, + Resistance = 13, + Bow = 14, + Galloping = 15, + SemiAutomaticGun = 16, + AutomaticGun = 17, + Machine = 18 +} + +-- CustomTriggerValueMode Enum +Triggers.CustomTriggerValueMode = { + OFF = 0, + Rigid = 1, + RigidA = 2, + RigidB = 3, + RigidAB = 4, + Pulse = 5, + PulseA = 6, + PulseB = 7, + PulseAB = 8, + VibrateResistance = 9, + VibrateResistanceA = 10, + VibrateResistanceB = 11, + VibrateResistanceAB = 12, + VibratePulse = 13, + VibratePulseA = 14, + VibratePulsB = 15, + VibratePulseAB = 16 +} + +-- Trigger Enum +Triggers.Trigger = { + Invalid = 0, + Left = 1, + Right = 2 +} + +-- InstructionType Enum +Triggers.InstructionType = { + Invalid = 0, + TriggerUpdate = 1, + RGBUpdate = 2, + PlayerLED = 3, + TriggerThreshold = 4 +} + +-- This is the representation of your enums in Lua tables +return Triggers \ No newline at end of file diff --git a/scripts/dsxAdvancedHaptic/modScript.lua b/scripts/dsxAdvancedHaptic/modScript.lua new file mode 100644 index 0000000..563bb10 --- /dev/null +++ b/scripts/dsxAdvancedHaptic/modScript.lua @@ -0,0 +1,2 @@ +load("dsxAdvancedHaptic") +registerCoreModule("dsxAdvancedHaptic") \ No newline at end of file