Files
Towbar/42.20/media/lua/server/BTTow.lua
T
2026-08-13 18:59:54 -04:00

83 lines
2.6 KiB
Lua

BTtow = {}
BTtow.Create = {}
BTtow.Init = {}
local TowbarVariantSize = 24
local TowbarMaxIndex = TowbarVariantSize - 1
local TowbarFirstZ = 1.0
local TowbarSlotStep = 0.1
-- Measured from Towbar.fbx at its original 0.01 script scale. Keep this in
-- lockstep with the client selector so the enlarged mesh's inner end remains
-- on the same vehicle-hitbox contact point.
local TowbarVisualScale = 2.5
local TowbarModelLength = 0.9714089036
local TowbarScaledModelLength = TowbarModelLength * TowbarVisualScale
local TowbarModelHalfLength = TowbarScaledModelLength / 2
local function getTowbarFrontEdgeZ(script)
if not script then return nil end
local ok, shape = pcall(function()
return script:getPhysicsChassisShape()
end)
if not ok or not shape then return nil end
local shapeOk, shapeZ = pcall(function()
return shape:z()
end)
if not shapeOk or type(shapeZ) ~= "number" or shapeZ <= 0 then
return nil
end
local centerOk, center = pcall(function()
return script:getCenterOfMassOffset()
end)
if not centerOk or not center then return nil end
local zOk, centerZ = pcall(function()
return center:z()
end)
if not zOk or type(centerZ) ~= "number" then return nil end
return centerZ + shapeZ / 2
end
local function getTowbarModelSlot(script)
local frontEdgeZ = getTowbarFrontEdgeZ(script)
if frontEdgeZ == nil then return nil end
-- Model offsets position the mesh origin. Move its center outward so the
-- towbar's inner end, rather than its center, meets the vehicle hitbox.
local modelCenterZ = frontEdgeZ + TowbarModelHalfLength
local index = math.floor(((modelCenterZ - TowbarFirstZ) / TowbarSlotStep) + 0.5)
return math.max(0, math.min(TowbarMaxIndex, index))
end
function BTtow.Create.towbar(vehicle, part)
if part == nil then return end
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 - 1 do
part:setModelVisible("towbar" .. j, false)
end
local modData = vehicle:getModData()
if modData and modData.towBarModelSwapInProgress then
return
end
if modData and modData["isTowingByTowBar"] and modData["towed"] then
local script = vehicle:getScript()
if script then
local index = getTowbarModelSlot(script)
local shouldShowOnThisPart = part:getId() == "towbar"
if shouldShowOnThisPart and index ~= nil then
part:setModelVisible("towbar" .. index, true)
end
end
end
end