local ffi = require('ffi')
local sampev = require 'lib.samp.events'
local selfObjects = {}
local selfObjectsRemoved = false
local PED_ALPHA = 255
local PED_ALPHA_UPDATE = -1
local PED_ALPHA_UPDATE_TO255 = -1
function SetRwObjectAlpha(handle, alpha)
local pedEn = getCharPointer(handle)
if pedEn ~= 0 then
ffi.cast("void (__thiscall *)(int, int)", 0x5332C0)(pedEn, alpha)
end
end
function main()
while not isSampAvailable() do wait(0) end
while true do
wait(0)
local camX, camY, camZ = getActiveCameraCoordinates()
local pedX, pedY, pedZ = getCharCoordinates(PLAYER_PED)
if getDistanceBetweenCoords3d(camX, camY, camZ, pedX, pedY, pedZ) < 0.9 then
if not selfObjectsRemoved then
selfObjectsRemoved = true
for k, v in pairs(selfObjects) do
AttachetObject(myid(), k):Remove()
end
end
if PED_ALPHA_UPDATE == -1 then
PED_ALPHA_UPDATE = os.clock()
else
PED_ALPHA = bringFloatTo(255, 0, PED_ALPHA_UPDATE, 1)
SetRwObjectAlpha(PLAYER_PED, PED_ALPHA)
printStringNow(PED_ALPHA, 10)
PED_ALPHA_UPDATE_TO255 = -1
end
else
if selfObjectsRemoved then
selfObjectsRemoved = false
for k, v in pairs(selfObjects) do
AttachetObject(myid(), k):Add(v)
end
end
if PED_ALPHA_UPDATE_TO255 == -1 then
PED_ALPHA_UPDATE_TO255 = os.clock()
else
PED_ALPHA = bringFloatTo(PED_ALPHA, 255, PED_ALPHA_UPDATE_TO255, 25)
SetRwObjectAlpha(PLAYER_PED, PED_ALPHA)
PED_ALPHA_UPDATE = -1
end
end
end
end
function onScriptTerminate(s, q)
if s == thisScript() then
if not selfObjectsRemoved then
for k, v in pairs(selfObjects) do
AttachetObject(myid(), k):Add(v)
end
end
if PED_ALPHA ~= 255 then
SetRwObjectAlpha(PLAYER_PED, 255)
end
end
end
function myid()
return select(2, sampGetPlayerIdByCharHandle(PLAYER_PED))
end
function bringFloatTo(from, to, start_time, duration)
local timer = os.clock() - start_time
if timer >= 0.00 and timer <= duration then
local count = timer / (duration / 100)
return from + (count * (to - from) / 100), true
end
return (timer > duration) and to or from, false
end
function sampev.onSetPlayerAttachedObject(playerId, index, create, object)
if playerId == select(2, sampGetPlayerIdByCharHandle(PLAYER_PED)) then
selfObjects[index] = {
modelId = object.modelId,
bone = object.bone,
offset = object.offset,
rotation = object.rotation,
scale = object.scale,
color1 = object.color1,
color2 = object.color2
}
end
end
function AttachetObject(player, index)
local class = {}
function class:Add(data)
local bs = raknetNewBitStream()
raknetBitStreamWriteInt16(bs, playerId) -- playerId
raknetBitStreamWriteInt32(bs, index) -- index
raknetBitStreamWriteBool(bs, true) -- create
raknetBitStreamWriteInt32(bs, data.modelid) -- modelId
raknetBitStreamWriteInt32(bs, data.bone) -- bone
raknetBitStreamWriteFloat(bs, data.offset.x) -- offset x
raknetBitStreamWriteFloat(bs, data.offset.y) -- offset y
raknetBitStreamWriteFloat(bs, data.offset.z) -- offset z
raknetBitStreamWriteFloat(bs, data.rotation.x) -- rotation x
raknetBitStreamWriteFloat(bs, data.rotation.y) -- rotation y
raknetBitStreamWriteFloat(bs, data.rotation.z) -- rotation z
raknetBitStreamWriteFloat(bs, data.scale.x) -- scale x
raknetBitStreamWriteFloat(bs, data.scale.y) -- scale y
raknetBitStreamWriteFloat(bs, data.scale.z) -- scale z
raknetBitStreamWriteInt32(bs, data.color1) -- color 1
raknetBitStreamWriteInt32(bs, data.color2) -- color 2
raknetEmulRpcReceiveBitStream(113, bs)
raknetDeleteBitStream(bs)
end
function class:Remove()
local bs = raknetNewBitStream()
raknetBitStreamWriteInt16(bs, playerId) -- playerId
raknetBitStreamWriteInt32(bs, index) -- index
raknetBitStreamWriteBool(bs, false) -- create
raknetEmulRpcReceiveBitStream(113, bs)
raknetDeleteBitStream(bs)
end
return class
end