--[[
    The following code enhances existing projectiles.
]]
addEventHandler( "onClientProjectileCreation", root, 
function (creator)

    local projectileType = getProjectileType( source )
    if projectileType == 19 or projectileType == 20 then

        local creatorVehicle
        if creator then
            if getElementType(creator) == "vehicle" then
                if not settings.replaceVehicleRockets then
                    return
                end
    
                creatorVehicle = creator
                creator = getVehicleController (creator) or nil	
            
            elseif getElementType(creator) == "player" then
                if not settings.replacePlayerRockets then
                    return
                end
    
                creatorVehicle = getPedOccupiedVehicle(creator)
            end
        elseif not settings.replacePlayerRockets then
            return
        end

        if not creator or creator == localPlayer then

            local startX, startY, startZ = getElementPosition(source)

            local xr, yr, zr = getElementRotation(source, "ZXY")

            local vehicleOffset = 0
            if creatorVehicle then
                xr, yr, zr = getElementRotation(creatorVehicle)
                zr = zr + 180
                xr = -xr
                vehicleOffset = -3
            else
                zr = -zr
            end     

            local matrix = createMatrix(startX, startY, startZ, xr, yr, zr)  

            local startX, startY, startZ = getPositionFromMatrixOffset(matrix, 0, vehicleOffset, 0)
            local targetX, targetY, targetZ = getPositionFromMatrixOffset(matrix, 0, -10000, 0)

            createCustomProjectile(creator, projectileType == 19 and "simple" or "seeking",  startX, startY, startZ, targetX, targetY, targetZ, nil, nil, nil, projectileType == 20 and getProjectileTarget ( source ) or nil, nil, nil, creator and {localPlayer, creatorVehicle})
        end
        setElementPosition(source, 0, 0, 10000)
        setProjectileCounter (source, 0)
    end
end)