Working yet too close

This commit is contained in:
2026-02-07 16:34:43 -05:00
parent 36b6c697e3
commit ada75f8479
2 changed files with 35 additions and 34 deletions

View File

@@ -4,6 +4,27 @@ if not TowBarMod.Hook then TowBarMod.Hook = {} end
local TowBarTowMass = 200
local AutoReattachCooldownHours = 1 / 7200 -- 0.5 seconds
TowBarMod.Hook.lastAutoReattachAtByVehicle = TowBarMod.Hook.lastAutoReattachAtByVehicle or {}
local AutoReattachPlayerCooldownHours = 1 / 14400 -- 0.25 seconds
TowBarMod.Hook.lastAutoReattachAtByPlayer = TowBarMod.Hook.lastAutoReattachAtByPlayer or {}
local function isTowBarTowPair(towingVehicle, towedVehicle)
if not towingVehicle or not towedVehicle then return false end
local towingModData = towingVehicle:getModData()
local towedModData = towedVehicle:getModData()
if not towingModData or not towedModData then return false end
if towingModData["isTowingByTowBar"] and towedModData["isTowingByTowBar"] and towedModData["towed"] then
return true
end
-- Rejoin fallback: original towbar state on the towed vehicle is enough to reapply rigid spacing.
if towedModData.towBarOriginalScriptName ~= nil then
return true
end
return false
end
local function getTowBarItem(playerObj)
if not playerObj then return nil end
@@ -144,8 +165,7 @@ function TowBarMod.Hook.reattachTowBarFromDriverSeat(playerObj, towingVehicle)
local towingModData = towingVehicle:getModData()
local towedModData = towedVehicle:getModData()
if not towingModData or not towedModData then return end
if not towingModData["isTowingByTowBar"] then return end
if not towedModData["isTowingByTowBar"] or not towedModData["towed"] then return end
if not isTowBarTowPair(towingVehicle, towedVehicle) then return end
local attachmentA = towingVehicle:getTowAttachmentSelf() or "trailer"
local attachmentB = towingVehicle:getTowAttachmentOther() or "trailerfront"
@@ -192,21 +212,27 @@ local function tryAutoReattachFromCharacter(character)
if not character or not instanceof(character, "IsoPlayer") or not character:isLocalPlayer() then return end
local playerObj = character
local nowHours = getGameTime() and getGameTime():getWorldAgeHours() or 0
local playerNum = playerObj:getPlayerNum()
local lastPlayerHours = TowBarMod.Hook.lastAutoReattachAtByPlayer[playerNum]
if lastPlayerHours and (nowHours - lastPlayerHours) < AutoReattachPlayerCooldownHours then
return
end
local towingVehicle = playerObj:getVehicle()
if not towingVehicle then return end
if not towingVehicle:isDriver(playerObj) then return end
if not towingVehicle:getVehicleTowing() then return end
local modData = towingVehicle:getModData()
if not modData or not modData["isTowingByTowBar"] then return end
local towedVehicle = towingVehicle:getVehicleTowing()
if not towedVehicle then return end
if not isTowBarTowPair(towingVehicle, towedVehicle) then return end
local vehicleId = towingVehicle:getId()
local nowHours = getGameTime() and getGameTime():getWorldAgeHours() or 0
local lastHours = TowBarMod.Hook.lastAutoReattachAtByVehicle[vehicleId]
if lastHours and (nowHours - lastHours) < AutoReattachCooldownHours then
return
end
TowBarMod.Hook.lastAutoReattachAtByPlayer[playerNum] = nowHours
TowBarMod.Hook.lastAutoReattachAtByVehicle[vehicleId] = nowHours
TowBarMod.Hook.reattachTowBarFromDriverSeat(playerObj, towingVehicle)
end

View File

@@ -126,23 +126,6 @@ function TowBarMod.UI.addUnhookOptionToMenu(playerObj, vehicle)
)
end
function TowBarMod.UI.addDriverReattachOptionToMenu(playerObj, towingVehicle)
local menu = getPlayerRadialMenu(playerObj:getPlayerNum())
if menu == nil then return end
if not towingVehicle then return end
if not towingVehicle:isDriver(playerObj) then return end
if not towingVehicle:getVehicleTowing() then return end
if not towingVehicle:getModData()["isTowingByTowBar"] then return end
menu:addSlice(
"Reattach Towbar (Debug)",
getTexture("media/textures/tow_bar_attach.png"),
TowBarMod.Hook.reattachTowBarFromDriverSeat,
playerObj,
towingVehicle
)
end
---------------------------------------------------------------------------
--- Mod compability
---------------------------------------------------------------------------
@@ -163,17 +146,9 @@ end
function ISVehicleMenu.showRadialMenu(playerObj)
TowBarMod.UI.defaultShowRadialMenu(playerObj)
local vehicle = playerObj:getVehicle()
if vehicle then
if vehicle:isDriver(playerObj) and vehicle:getVehicleTowing() and vehicle:getModData()["isTowingByTowBar"] then
TowBarMod.UI.removeDefaultDetachOption(playerObj)
TowBarMod.UI.addUnhookOptionToMenu(playerObj, vehicle)
TowBarMod.UI.addDriverReattachOptionToMenu(playerObj, vehicle)
end
return
end
if playerObj:getVehicle() then return end
vehicle = ISVehicleMenu.getVehicleToInteractWith(playerObj)
local vehicle = ISVehicleMenu.getVehicleToInteractWith(playerObj)
if vehicle == nil then return end
if vehicle:getModData()["isTowingByTowBar"] then