Add KI5 Compat

This commit is contained in:
2026-02-12 19:01:14 -05:00
parent 633ebb4ac4
commit e694f746f8
5 changed files with 443 additions and 193 deletions

View File

@@ -6,6 +6,8 @@ local TowbarVariantSize = 24
local TowbarNormalStart = 0
local TowbarLargeStart = 24
local TowbarMaxIndex = TowbarVariantSize - 1
local VanillaScaleMin = 1.5
local VanillaScaleMax = 2.0
local function getVehicleModelScale(script)
if not script then return nil end
@@ -31,47 +33,85 @@ local function getVehicleModelScale(script)
return nil
end
local function shouldUseLargeTowbarModel(script)
local function isVanillaScale(script)
local modelScale = getVehicleModelScale(script)
if modelScale == nil then
return false
return true
end
local configuredThreshold = TowBarMod and TowBarMod.Config and tonumber(TowBarMod.Config.largeTowbarModelScaleThreshold)
local threshold = configuredThreshold or 1.2
return modelScale < threshold
local configuredMin = TowBarMod and TowBarMod.Config and tonumber(TowBarMod.Config.vanillaTowbarModelScaleMin)
local configuredMax = TowBarMod and TowBarMod.Config and tonumber(TowBarMod.Config.vanillaTowbarModelScaleMax)
local minScale = configuredMin or VanillaScaleMin
local maxScale = configuredMax or VanillaScaleMax
return modelScale >= minScale and modelScale <= maxScale
end
local function getTowbarIndexVanilla(script)
local z = script:getPhysicsChassisShape():z() / 2 - 0.1
local index = math.floor((z * 2 / 3 - 1) * 10)
return math.max(0, math.min(TowbarMaxIndex, index))
end
local function getTowbarIndexSmallScale(script)
if not script then return nil end
local maxAbsTowZ = nil
local trailer = script:getAttachmentById("trailer")
if trailer then
maxAbsTowZ = math.abs(trailer:getOffset():z())
end
local trailerFront = script:getAttachmentById("trailerfront")
if trailerFront then
local frontAbsZ = math.abs(trailerFront:getOffset():z())
if not maxAbsTowZ or frontAbsZ > maxAbsTowZ then
maxAbsTowZ = frontAbsZ
end
end
if maxAbsTowZ ~= nil then
local index = math.floor((maxAbsTowZ + 0.1 - 1.0) * 10)
return math.max(0, math.min(TowbarMaxIndex, index))
end
return nil
end
local function getTowbarModelSlot(script)
local chassisZ = script:getPhysicsChassisShape():z()
local index = 0
if chassisZ > 3.0 then
local halfZ = chassisZ / 2
index = math.floor((halfZ - 1.0) * 16 - 1)
local isVanilla = isVanillaScale(script)
local index = getTowbarIndexVanilla(script)
if not isVanilla then
local attachmentIndex = getTowbarIndexSmallScale(script)
if attachmentIndex ~= nil then
index = attachmentIndex
else
local offset = TowBarMod and TowBarMod.Config and tonumber(TowBarMod.Config.smallScaleTowbarIndexOffset) or 2
index = math.max(0, math.min(TowbarMaxIndex, index + offset))
end
end
index = math.max(0, math.min(TowbarMaxIndex, index))
local slotStart = shouldUseLargeTowbarModel(script) and TowbarLargeStart or TowbarNormalStart
return slotStart + index
return index, isVanilla
end
function BTtow.Create.towbar(vehicle, part)
if part == nil then return end
for j=0, (TowbarVariantSize * 2) - 1 do
for j=0, TowbarVariantSize - 1 do
part:setModelVisible("towbar" .. j, false)
end
end
function BTtow.Init.towbar(vehicle, part)
if part == nil then return end
for j=0, (TowbarVariantSize * 2) - 1 do
for j=0, TowbarVariantSize - 1 do
part:setModelVisible("towbar" .. j, false)
end
if vehicle:getModData()["isTowingByTowBar"] and vehicle:getModData()["towed"] then
local script = vehicle:getScript()
if script then
local slot = getTowbarModelSlot(script)
part:setModelVisible("towbar" .. slot, true)
local index, isVanilla = getTowbarModelSlot(script)
local partId = part:getId()
local shouldShowOnThisPart = (isVanilla and partId == "towbar") or ((not isVanilla) and partId == "towbarLarge")
if shouldShowOnThisPart then
part:setModelVisible("towbar" .. index, true)
end
end
end
end