Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
SvetlozarValchev committed Oct 22, 2023
0 parents commit 97f1626
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 0 deletions.
130 changes: 130 additions & 0 deletions lua/ge/extensions/dsxAdvancedHaptic.lua
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions lua/ge/extensions/dsxTriggerEnum.lua
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lua/vehicle/extensions/auto/dsxAdvancedHapticVehicleDispatcher.lua
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions lua/vehicle/extensions/auto/dsxTriggerEnum.lua
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions scripts/dsxAdvancedHaptic/modScript.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
load("dsxAdvancedHaptic")
registerCoreModule("dsxAdvancedHaptic")

0 comments on commit 97f1626

Please sign in to comment.