2025-03-01 12:58:43 +00:00
local Vehicles = { }
--- Creates a vehicle with the specified model and coordinates.
---
--- This function loads the vehicle model, creates the vehicle in the world at the given coordinates, sets initial properties, and returns the vehicle handle.
---
---@param model string|number The model name or hash of the vehicle to create.
---@param coords vector4 The coordinates where the vehicle will be placed, including x, y, z, and w (heading).
---
---@return number entityID The handle of the created vehicle.
---
---@usage
--- ```lua
--- local vehicle = makeVeh('adder', vector4(123.4, 567.8, 90.1, 180.0))
--- ```
2025-07-17 12:40:43 +01:00
function makeVeh ( model , coords , synced , fade )
2025-03-01 12:58:43 +00:00
loadModel ( model )
2025-07-16 08:18:55 +01:00
local veh = CreateVehicle ( model , coords.x , coords.y , coords.z , coords.w , synced ~= false , false )
2025-03-01 12:58:43 +00:00
SetVehicleHasBeenOwnedByPlayer ( veh , true )
2025-04-07 20:52:59 +01:00
if gameName ~= " rdr3 " then
2025-07-09 15:10:53 +01:00
SetEntityAlpha ( veh , 0 , false )
SetNetworkIdCanMigrate ( NetworkGetNetworkIdFromEntity ( veh ) , true )
2025-04-07 20:52:59 +01:00
Wait ( 100 )
SetVehicleNeedsToBeHotwired ( veh , false )
SetVehRadioStation ( veh , ' OFF ' )
SetVehicleFuelLevel ( veh , 100.0 )
SetVehicleModKit ( veh , 0 )
2025-07-09 15:10:53 +01:00
2025-04-07 20:52:59 +01:00
end
2025-03-01 12:58:43 +00:00
SetVehicleOnGroundProperly ( veh )
debugPrint ( " ^6Bridge^7: ^1Veh ^2Created^7: '^6 " .. veh .. " ^7' | ^2Hash^7: ^7'^6 " .. model .. " ^7' | ^2Coord^7: " .. formatCoord ( coords ) )
unloadModel ( model )
Vehicles [ # Vehicles + 1 ] = veh
2025-07-17 12:40:43 +01:00
if fade ~= false then
CreateThread ( function ( )
fadeInEnt ( veh )
end )
end
2025-03-01 12:58:43 +00:00
return veh
end
2025-03-26 21:43:48 +00:00
local distanceVehicles = { }
--- Creates a vehicle that spawns when the player enters a designated polyzone area.
---
--- This function sets up a circular polyzone; when the player enters the zone, the vehicle is spawned,
--- and when the player exits, the vehicle is deleted.
---
---@param data table A table containing vehicle data.
--- - **vehicle** `string`: The model name or hash of the vehicle to spawn.
--- - **coords** `vector4`: The coordinates where the vehicle will be placed. Should include x, y, z, and w (heading).
---@param freeze boolean (optional) Whether to freeze the vehicle in place. Defaults to `false`.
---@param synced boolean (optional) Whether the vehicle should be synced across clients. Defaults to `false`.
function makeDistVehicle ( data , radius , onEnter , onExit )
local vehicle = nil
local zoneId = keyGen ( ) .. keyGen ( )
local zone = createCirclePoly ( {
name = zoneId ,
coords = vec3 ( data.coords . x , data.coords . y , data.coords . z ) ,
radius = radius ,
onEnter = function ( )
2025-07-16 08:18:55 +01:00
vehicle = makeVeh ( data.model , data.coords , false )
2025-03-26 21:43:48 +00:00
if onEnter then
2025-06-20 18:03:08 +01:00
debugPrint ( " ^6Bridge^7: ^4makeDistVehicle ^3onEnter^7() ^2running^7 " )
2025-03-26 21:43:48 +00:00
onEnter ( vehicle )
end
end ,
onExit = function ( )
deleteVehicle ( vehicle )
if onExit then
2025-06-20 18:03:08 +01:00
debugPrint ( " ^6Bridge^7: ^4makeDistVehicle ^3onExit^7() ^2running^7 " )
2025-03-26 21:43:48 +00:00
onExit ( vehicle )
end
end ,
debug = debugMode ,
} )
distanceVehicles [ zoneId ] = { zone = zone , vehicle = vehicle }
return zoneId
end
--- Removes a specific distance-based vehicle spawning zone.
---
---@param zoneId string The unique identifier of the zone to remove.
function removeDistVehicleZone ( zoneId )
if distanceVehicles [ zoneId ] . zone then
removePolyZone ( distanceVehicles [ zoneId ] . zone ) -- Adjust this if your polyzone library uses a different removal method.
if distanceVehicles [ zoneId ] . vehicle then
deleteVehicle ( distanceVehicles [ zoneId ] . vehicle )
end
distanceVehicles [ zoneId ] = nil
print ( " Removed polyzone for zoneId: " .. zoneId )
else
print ( " No zone found with zoneId: " .. zoneId )
end
end
--- Deletes a spawned vehicle.
---
---@param vehicle number The handle of the vehicle entity to delete.
function deleteVehicle ( vehicle )
if vehicle then
debugPrint ( " ^6Bridge^7: ^2Destroying Vehicle^7: '^6 " .. vehicle .. " ^7' " )
if IsEntityAttachedToEntity ( vehicle , PlayerPedId ( ) ) then
SetEntityAsMissionEntity ( vehicle )
DetachEntity ( vehicle , true , true )
end
DeleteVehicle ( vehicle )
end
end
2025-03-01 12:58:43 +00:00
--- Cleans up all created vehicles when the resource stops.
onResourceStop ( function ( r )
for i = 1 , # Vehicles do DeleteVehicle ( Vehicles [ i ] ) end
2025-02-21 13:47:15 +00:00
end )