function samp_create_sync_data(sync_type)
-- from SAMP.Lua
local raknet = require 'samp.raknet'
require 'samp.synchronization'
local sync_traits = {
player = {'PlayerSyncData', raknet.PACKET.PLAYER_SYNC },
vehicle = {'VehicleSyncData', raknet.PACKET.VEHICLE_SYNC },
passenger = {'PassengerSyncData', raknet.PACKET.PASSENGER_SYNC },
aim = {'AimSyncData', raknet.PACKET.AIM_SYNC },
trailer = {'TrailerSyncData', raknet.PACKET.TRAILER_SYNC },
unoccupied = {'UnoccupiedSyncData', raknet.PACKET.UNOCCUPIED_SYNC },
bullet = {'BulletSyncData', raknet.PACKET.BULLET_SYNC },
spectator = {'SpectatorSyncData', raknet.PACKET.SPECTATOR_SYNC }
}
local sync_info = sync_traits[sync_type]
local data_type = 'struct ' .. sync_info[1]
local data = ffi.new(data_type, {})
local raw_data_ptr = tonumber(ffi.cast('uintptr_t', ffi.new(data_type .. '*', data)))
-- function to send packet
local func_send = function()
local bs = bitStream.new()
bs:writeUInt8(sync_info[2])
bs:writeBuffer(raw_data_ptr, ffi.sizeof(data))
bs:sendPacketEx(HIGH_PRIORITY, UNRELIABLE_SEQUENCED, 1)
bs:reset()
end
-- metatable to access sync data and 'send' function
local mt = {
__index = function(t, index)
return data[index]
end,
__newindex = function(t, index, value)
data[index] = value
end
}
return setmetatable({send = func_send}, mt)
end