Jump to content

Special vehicledoors [Free to use script]


Puma

Recommended Posts

Hi,

Long time no see :) Picked up Lua today and decided to make something other people would like to have, so here's a script that basically alters vehicledoors.

Syntax (clientside):

setVehicleDoorType ( vehicle, doorType ) 

Reset doortype by not filling in a doortype.

My script uses elementdata, so when you set doortype for a vehicle at client 1, the altered doortype on that vehicle is also visible to all other players.

Default doortypes are "scissor" and "butterfly".

Small vid:

Script: (feel free to implement it in your own scripts, some credit would be nice)

-- Special doors script by Puma 
-- Free to use and free to edit, but please aks me for approval if you want to release a significantly altered version of this script 
  
-- YOU CAN EDIT THIS: 
  
local doorTypeRotation = { 
    ["scissor"] = {72, 0, 0}, 
    ["butterfly"] = {0, -60, 60} 
} 
    -- add doortypes 
  
----------------------- 
-- HOW TO USE THIS for douchebags 
----------------------- 
  
--         Just put this somewhere in your script and fill in a vehicle and a doortype (see above for available doortypes) 
--         setVehicleDoorType ( v, type ) 
  
--         If you want to reset the doortype, just use setVehicleDoorType ( v ) without filling in a type 
  
-------------------------------- 
-- Now comes the part you shouldn't mess with 
-------------------------------- 
  
-- Defining som shit ----------- 
  
local p = getLocalPlayer() 
local doorAnimTime = 350 
local doorIDComponentTable = { 
    [2] = "door_lf_dummy", 
    [3] = "door_rf_dummy", 
    [4] = "door_lr_dummy", 
    [5] = "door_rr_dummy" 
                        } 
local doorTypes = {} 
for doorType, doorTable in pairs ( doorTypeRotation ) do 
    table.insert ( doorTypes, doorType ) 
end 
  
local vDoorType = {} 
for i, v in pairs ( getElementsByType("vehicle")) do 
    local doorType = getElementData ( v, "vDoorType" ) 
    if doorType then 
        vDoorType[v] = doorType 
    end 
end 
  
local function vRemoveDoorTypeFromTable ( v ) 
    if isVehicle(v) then 
        vDoorType[v] = nil 
        oldDoorRatios[v] = nil 
        doorStatus[v] = nil 
        doorTimers[v] = nil 
    end 
end 
function vehicleDestroyed () 
    vRemoveDoorTypeFromTable(source) 
end 
addEventHandler ( "onClientElementDestroy", root, vehicleDestroyed ) 
addEventHandler ( "onClientVehicleExplode", root ,vehicleDestroyed ) 
  
-- MAIN ----------------------------------------------------------------------- 
  
function setVehicleDoorType ( v, type ) 
    if v and isElement(v) and getElementType(v) == "vehicle" then 
        if type and tostring(type) and isDoorType(type) then 
            setElementData ( v, "vDoorType", type ) 
        else 
            setElementData ( v, "vDoorType", nil ) 
            vRemoveDoorTypeFromTable ( v ) 
        end 
    end 
end 
  
local function elementDataChange ( key, oldValue ) 
    if key == "vDoorType" and isVehicle(source) then 
        vDoorType[source] = getElementData ( source, "vDoorType" ) 
    end 
end 
addEventHandler ( "onClientElementDataChange", root, elementDataChange ) 
  
local oldDoorRatios = {} 
local doorStatus = {} 
local doorTimers = {} 
local function preRender () 
    for v, doorType in pairs ( vDoorType ) do 
        if isElement(v) then 
            if not doorTimers[v] then doorTimers[v] = {} end 
            local doorRatios = {} 
            for i=1, 4 do 
                local i = i + 1 -- leftfront=2, rightfront=3, leftrear=4, rightrear=5 
                local doorRatio = getVehicleDoorOpenRatio ( v, i ) 
  
                if doorRatio and oldDoorRatios[v] and oldDoorRatios[v][i] then 
                    local oldDoorRatio = oldDoorRatios[v][i] 
  
                    if not doorStatus[v] then doorStatus[v] ={} end 
                    local doorPreviousState = doorStatus[v][i] 
                    if not doorPreviousState then doorPreviousState = "closed" end 
                    if doorPreviousState == "closed" and doorRatio > oldDoorRatio then 
                        doorStatus[v][i] = "opening" 
                        doorTimers[v][i] = setTimer(function(v,i)doorStatus[v][i]="open" doorTimers[v][i]=nil end,doorAnimTime,1,v,i) 
                    elseif doorPreviousState == "open" and doorRatio < oldDoorRatio then 
                        doorStatus[v][i] = "closing" 
                        doorTimers[v][i] = setTimer(function(v,i)doorStatus[v][i]="closed" doorTimers[v][i]=nil end,doorAnimTime,1,v,i) 
                    end 
                elseif not oldDoorRatios[v] then 
                    oldDoorRatios[v] = {} 
                end 
                if doorRatio then 
                    oldDoorRatios[v][i] = doorRatio 
                end 
            end 
        else 
            vDoorType[v] = nil 
            oldDoorRatios[v] = nil 
            doorStatus[v] = nil 
            doorTimers[v] = nil 
        end 
    end 
  
    for v, doors in pairs ( doorStatus ) do 
        local doorType = vDoorType[v] 
        local rx, ry, rz = unpack ( doorTypeRotation[doorType] ) 
        for door, status in pairs ( doors ) do 
            local ratio = 0 
            if status == "open" then ratio = 1 end 
            local timer = doorTimers[v][door] 
            if timer and isTimer ( timer ) then 
                local timeLeft = getTimerDetails ( timer ) 
                ratio = timeLeft/doorAnimTime 
                if status == "opening" then ratio = 1 - ratio end 
            end 
            local dummyName = doorIDComponentTable[door] 
            if dummyName then 
                local rx, ry, rz = unpack(doorTypeRotation[doorType]) 
                local rx, ry, rz = rx*ratio, ry*ratio, rz*ratio 
                if string.find(dummyName,"rf") or string.find(dummyName,"rr") then 
                    ry, rz = ry*-1, rz*-1 
                end 
                setVehicleComponentRotation ( v, dummyName, rx, ry, rz ) 
            end 
        end 
    end 
end 
addEventHandler ( "onClientPreRender", root, preRender ) 
  
-- UTILS ----------------------------------------------------------------- 
  
local function isDoorType ( type ) 
    if type and tostring(type) then 
        for i, t in pairs ( doorTypes ) do 
            if t == type then return true end 
        end 
    end 
end 
  
local function pv (player) 
    if not player then 
        return getPedOccupiedVehicle ( p ) 
    elseif isPlayer(player) then 
        return getPedOccupiedVehicle ( player ) 
    end 
end 
  
local function isPlayer ( player ) 
    if player and isElement ( player ) and getElementType(player) == "player" then 
        return true 
    end 
end 
local function isVehicle ( vehicle ) 
    if vehicle and isElement ( vehicle ) and getElementType(vehicle) == "vehicle" then 
        return true 
    end 
end 

Have fun with it, you can add your own doortypes at the top of the script, if you want to. It's not perfect, but it's fun to mess around with and if there's someone who wants to optimize it: feel free to do so! Just let me know if you want to release it somewhere.

Have fun with it. Let me know if I forgot something.

Link to comment
  • 4 months later...
  • 5 months later...
  • 11 months later...

Today I saw this topic again and I decided to script this awesome resource.

But I have a small problem.

I created this in client side, meta file is correct, client.lua is correct.

But for some reason the resource is not working.

I copied the code above and I pasted, and that's it, do I have to do more anything else?

My Client file:

Debug: http://i.imgur.com/hGzRDlB.png

  
-- Special doors script by Puma 
-- Free to use and free to edit, but please aks me for approval if you want to release a significantly altered version of this script 
  
-- YOU CAN EDIT THIS: 
  
local doorTypeRotation = { 
    ["scissor"] = {72, 0, 0}, 
    ["butterfly"] = {0, -60, 60} 
} 
    -- add doortypes 
  
----------------------- 
-- HOW TO USE THIS for douchebags 
----------------------- 
  
--         Just put this somewhere in your script and fill in a vehicle and a doortype (see above for available doortypes) 
--         setVehicleDoorType ( v, type ) 
  
--         If you want to reset the doortype, just use setVehicleDoorType ( v ) without filling in a type 
  
-------------------------------- 
-- Now comes the part you shouldn't mess with 
-------------------------------- 
  
-- Defining som :~ ----------- 
  
local p = getLocalPlayer() 
local doorAnimTime = 350 
local doorIDComponentTable = { 
    [2] = "door_lf_dummy", 
    [3] = "door_rf_dummy", 
    [4] = "door_lr_dummy", 
    [5] = "door_rr_dummy" 
                        } 
local doorTypes = {} 
for doorType, doorTable in pairs ( doorTypeRotation ) do 
    table.insert ( doorTypes, doorType ) 
end 
  
local vDoorType = {} 
for i, v in pairs ( getElementsByType("vehicle")) do 
    local doorType = getElementData ( v, "vDoorType" ) 
    if doorType then 
        vDoorType[v] = doorType 
    end 
end 
  
local function vRemoveDoorTypeFromTable ( v ) 
    if isVehicle(v) then 
        vDoorType[v] = nil 
        oldDoorRatios[v] = nil 
        doorStatus[v] = nil 
        doorTimers[v] = nil 
    end 
end 
function vehicleDestroyed () 
    vRemoveDoorTypeFromTable(source) 
end 
addEventHandler ( "onClientElementDestroy", root, vehicleDestroyed ) 
addEventHandler ( "onClientVehicleExplode", root ,vehicleDestroyed ) 
  
-- MAIN ----------------------------------------------------------------------- 
  
function setVehicleDoorType ( v, type ) 
    if v and isElement(v) and getElementType(v) == "vehicle" then 
        if type and tostring(type) and isDoorType(type) then 
            setElementData ( v, "vDoorType", type ) 
        else 
            setElementData ( v, "vDoorType", nil ) 
            vRemoveDoorTypeFromTable ( v ) 
        end 
    end 
end 
  
local function elementDataChange ( key, oldValue ) 
    if key == "vDoorType" and isVehicle(source) then 
        vDoorType[source] = getElementData ( source, "vDoorType" ) 
    end 
end 
addEventHandler ( "onClientElementDataChange", root, elementDataChange ) 
  
local oldDoorRatios = {} 
local doorStatus = {} 
local doorTimers = {} 
local function preRender () 
    for v, doorType in pairs ( vDoorType ) do 
        if isElement(v) then 
            if not doorTimers[v] then doorTimers[v] = {} end 
            local doorRatios = {} 
            for i=1, 4 do 
                local i = i + 1 -- leftfront=2, rightfront=3, leftrear=4, rightrear=5 
                local doorRatio = getVehicleDoorOpenRatio ( v, i ) 
  
                if doorRatio and oldDoorRatios[v] and oldDoorRatios[v][i] then 
                    local oldDoorRatio = oldDoorRatios[v][i] 
  
                    if not doorStatus[v] then doorStatus[v] ={} end 
                    local doorPreviousState = doorStatus[v][i] 
                    if not doorPreviousState then doorPreviousState = "closed" end 
                    if doorPreviousState == "closed" and doorRatio > oldDoorRatio then 
                        doorStatus[v][i] = "opening" 
                        doorTimers[v][i] = setTimer(function(v,i)doorStatus[v][i]="open" doorTimers[v][i]=nil end,doorAnimTime,1,v,i) 
                    elseif doorPreviousState == "open" and doorRatio < oldDoorRatio then 
                        doorStatus[v][i] = "closing" 
                        doorTimers[v][i] = setTimer(function(v,i)doorStatus[v][i]="closed" doorTimers[v][i]=nil end,doorAnimTime,1,v,i) 
                    end 
                elseif not oldDoorRatios[v] then 
                    oldDoorRatios[v] = {} 
                end 
                if doorRatio then 
                    oldDoorRatios[v][i] = doorRatio 
                end 
            end 
        else 
            vDoorType[v] = nil 
            oldDoorRatios[v] = nil 
            doorStatus[v] = nil 
            doorTimers[v] = nil 
        end 
    end 
  
    for v, doors in pairs ( doorStatus ) do 
        local doorType = vDoorType[v] 
        local rx, ry, rz = unpack ( doorTypeRotation[doorType] ) 
        for door, status in pairs ( doors ) do 
            local ratio = 0 
            if status == "open" then ratio = 1 end 
            local timer = doorTimers[v][door] 
            if timer and isTimer ( timer ) then 
                local timeLeft = getTimerDetails ( timer ) 
                ratio = timeLeft/doorAnimTime 
                if status == "opening" then ratio = 1 - ratio end 
            end 
            local dummyName = doorIDComponentTable[door] 
            if dummyName then 
                local rx, ry, rz = unpack(doorTypeRotation[doorType]) 
                local rx, ry, rz = rx*ratio, ry*ratio, rz*ratio 
                if string.find(dummyName,"rf") or string.find(dummyName,"rr") then 
                    ry, rz = ry*-1, rz*-1 
                end 
                setVehicleComponentRotation ( v, dummyName, rx, ry, rz ) 
            end 
        end 
    end 
end 
addEventHandler ( "onClientPreRender", root, preRender ) 
  
-- UTILS ----------------------------------------------------------------- 
  
local function isDoorType ( type ) 
    if type and tostring(type) then 
        for i, t in pairs ( doorTypes ) do 
            if t == type then return true end 
        end 
    end 
end 
  
local function pv (player) 
    if not player then 
        return getPedOccupiedVehicle ( p ) 
    elseif isPlayer(player) then 
        return getPedOccupiedVehicle ( player ) 
    end 
end 
  
local function isPlayer ( player ) 
    if player and isElement ( player ) and getElementType(player) == "player" then 
        return true 
    end 
end 
local function isVehicle ( vehicle ) 
    if vehicle and isElement ( vehicle ) and getElementType(vehicle) == "vehicle" then 
        return true 
    end 
end 
  

It's exactly how you see in main topic, I think that I'm missing a step that I have to do.

Can anyone help me, please?

I'm noob, ok? :(

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...