42.19 Support

This commit is contained in:
2026-06-01 19:51:40 -04:00
parent 8c0afd857b
commit 40f0296080
3 changed files with 186 additions and 33 deletions
+77 -12
View File
@@ -23,6 +23,53 @@ local function computeAttachmentHeight(vehicle)
return -0.5
end
local function getVehicleHalfLength(script)
if not script then return nil end
local ok, shape = pcall(function()
return script:getPhysicsChassisShape()
end)
if ok and shape then
local zOk, z = pcall(function()
return shape:z()
end)
z = zOk and tonumber(z) or nil
if z and z > 0 then return z / 2 end
end
ok, shape = pcall(function()
return script:getExtents()
end)
if ok and shape then
local zOk, z = pcall(function()
return shape:z()
end)
z = zOk and tonumber(z) or nil
if z and z > 0 then return z / 2 end
end
return nil
end
local function getExteriorAttachmentZ(script, attachmentId, originalZ, extraDistance)
local halfLength = getVehicleHalfLength(script)
if halfLength == nil then return originalZ end
local configuredPadding = TowBarMod.Config and tonumber(TowBarMod.Config.towAttachmentExteriorPadding)
local padding = configuredPadding or 0.25
local direction = originalZ >= 0 and 1 or -1
if originalZ == 0 and attachmentId == "trailer" then
direction = -1
end
local exteriorZ = direction * (halfLength + padding + (extraDistance or 0))
if math.abs(originalZ) > math.abs(exteriorZ) then
return originalZ
end
return exteriorZ
end
function TowBarMod.Utils.isTrailer(vehicle)
return string.match(string.lower(vehicle:getScript():getName()), "trailer")
end
@@ -93,8 +140,12 @@ function TowBarMod.Utils.getHookTypeVariants(vehicleA, vehicleB, hasTowBar)
end
function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicle, attachmentA, attachmentB)
local towingAttachment = towingVehicle:getScript():getAttachmentById(attachmentA)
local towedAttachment = towedVehicle:getScript():getAttachmentById(attachmentB)
local towingScript = towingVehicle:getScript()
local towedScript = towedVehicle:getScript()
if towingScript == nil or towedScript == nil then return end
local towingAttachment = towingScript:getAttachmentById(attachmentA)
local towedAttachment = towedScript:getAttachmentById(attachmentB)
if towingAttachment == nil or towedAttachment == nil then return end
towingAttachment:setUpdateConstraint(false)
@@ -109,12 +160,16 @@ function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicl
-- Store and update the towing vehicle's attachment Y.
local towingModData = towingVehicle:getModData()
if towingModData["towBarOriginalTowingOffsetY"] == nil then
towingModData["towBarOriginalTowingOffsetY"] = towingAttachment:getOffset():y()
local towingOffset = towingAttachment:getOffset()
if towingModData["towBarOriginalTowingAttachmentId"] ~= attachmentA
or towingModData["towBarOriginalTowingOffsetX"] == nil
or towingModData["towBarOriginalTowingOffsetY"] == nil
or towingModData["towBarOriginalTowingOffsetZ"] == nil then
towingModData["towBarOriginalTowingOffsetX"] = towingOffset:x()
towingModData["towBarOriginalTowingOffsetY"] = towingOffset:y()
towingModData["towBarOriginalTowingOffsetZ"] = towingOffset:z()
towingModData["towBarOriginalTowingAttachmentId"] = attachmentA
end
local towingOffset = towingAttachment:getOffset()
towingAttachment:getOffset():set(towingOffset:x(), towingHeight, towingOffset:z())
local towedModData = towedVehicle:getModData()
local spacingDistance = 1.0
@@ -122,6 +177,11 @@ function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicl
spacingDistance = tonumber(TowBarMod.Config.rigidTowbarDistance)
end
local towingBaseX = tonumber(towingModData["towBarOriginalTowingOffsetX"]) or towingOffset:x()
local towingBaseZ = tonumber(towingModData["towBarOriginalTowingOffsetZ"]) or towingOffset:z()
local towingExteriorZ = getExteriorAttachmentZ(towingScript, attachmentA, towingBaseZ, 0)
towingAttachment:getOffset():set(towingBaseX, towingHeight, towingExteriorZ)
local offset = towedAttachment:getOffset()
local storedBaseX = tonumber(towedModData["towBarBaseAttachmentOffsetX"])
local storedBaseY = tonumber(towedModData["towBarBaseAttachmentOffsetY"])
@@ -140,9 +200,9 @@ function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicl
towedModData["towBarBaseAttachmentOffsetZ"] = baseZ
end
local zDirection = baseZ >= 0 and 1 or -1
local zShift = zDirection * spacingDistance
towedAttachment:getOffset():set(baseX, towedHeight, baseZ + zShift)
local towedExteriorZ = getExteriorAttachmentZ(towedScript, attachmentB, baseZ, spacingDistance)
local zShift = towedExteriorZ - baseZ
towedAttachment:getOffset():set(baseX, towedHeight, towedExteriorZ)
towedModData["isChangedTowedAttachment"] = true
towedModData["towBarChangedAttachmentId"] = attachmentB
@@ -161,14 +221,20 @@ function TowBarMod.Utils.updateAttachmentsOnDefaultValues(towingVehicle, towedVe
local zOffset = (towingAttachmentId == "trailer") and -1 or 1
towingAttachment:setZOffset(zOffset)
-- Restore the original Y offset that was overridden by dynamic height.
-- Restore the original offset that was overridden for rigid tow spacing.
local originalX = tonumber(towingModData["towBarOriginalTowingOffsetX"])
local originalY = tonumber(towingModData["towBarOriginalTowingOffsetY"])
if originalY ~= nil then
local originalZ = tonumber(towingModData["towBarOriginalTowingOffsetZ"])
if originalX ~= nil and originalY ~= nil and originalZ ~= nil then
towingAttachment:getOffset():set(originalX, originalY, originalZ)
elseif originalY ~= nil then
local off = towingAttachment:getOffset()
towingAttachment:getOffset():set(off:x(), originalY, off:z())
end
end
towingModData["towBarOriginalTowingOffsetX"] = nil
towingModData["towBarOriginalTowingOffsetY"] = nil
towingModData["towBarOriginalTowingOffsetZ"] = nil
towingModData["towBarOriginalTowingAttachmentId"] = nil
towingVehicle:transmitModData()
@@ -249,4 +315,3 @@ local function fixTowAttachmentsForOtherVehicleMods()
end
Events.OnGameBoot.Add(fixTowAttachmentsForOtherVehicleMods)