gPlayers = {}

RegisterLocalEventHandler("PlayerDropped",function(player)
	gPlayers[player] = nil
end)
RegisterNetworkEventHandler("psync:InitScript",function(player)
	PedSetDamageGivenMultiplier(GetSyncPlayerPed(player),0,1)
	gPlayers[player] = true
end)
RegisterNetworkEventHandler("psync:ShootWeapon",function(player,ped,action,weapon)
	if gPlayers[player] and IsSyncPedValid(ped) and F_IsWeaponValid(weapon) and type(action) == "string" then
		for other in pairs(gPlayers) do
			if other ~= player then
				SendNetworkEvent(other,"psync:ShootWeapon",ped,action,weapon)
			end
		end
	end
end)
RegisterNetworkEventHandler("psync:ShotPed",function(player,ped,weapon,damage,headshot)
	if gPlayers[player] and IsSyncPedValid(ped) and F_IsWeaponValid(weapon) and type(damage) == "number" and damage > 0 and damage < 1 / 0 then
		local dimension = GetSyncEntityDimension(GetSyncPlayerPed(player))
		if GetSyncEntityDimension(ped) == dimension and RunLocalEvent("psync:SyncProjectile",player) then
			local hp = math.max(0,PedGetHealth(ped)-damage)
			for other in pairs(gPlayers) do
				if other ~= player and GetSyncEntityDimension(GetSyncPlayerPed(other)) == dimension then
					if headshot then
						SendNetworkEvent(other,"psync:ShotPed",ped,weapon,hp,true)
					else
						SendNetworkEvent(other,"psync:ShotPed",ped,weapon,hp)
					end
				end
			end
			PedSetHealth(ped,hp)
		end
	end
end)

-- utility
function F_IsWeaponValid(weapon)
	return type(weapon) == "number" and math.floor(weapon) == weapon and weapon >= 299 and weapon <= 444
end
