
require "SimpleSilencersModelTable"

local silencedVolumeValue = 0.3
local silencedSoundRadius = 0.2
local metalPipeSilencedVolumeValue = 0.6
local metalPipeSilencedSoundRadius = 0.5

local silencedSwingSound = 'SilencedShot'
local craftedSilencedSwingSound = 'CraftedSuppressedShot'

-- hook function for silencer handling
local silencerOnEquipPrimary = function(character, inventoryItem)
	if inventoryItem ~= nil then
		local scriptItem = inventoryItem:getScriptItem()
		local isWeapon = inventoryItem:IsWeapon()
		local ssv = scriptItem:getSoundVolume()
		local ssr = scriptItem:getSoundRadius()
		if inventoryItem:getStringItemType() == "RangedWeapon" and isWeapon  then 
			if inventoryItem:getWeaponPart("Canon") then
				local canonName = inventoryItem:getWeaponPart("Canon"):getFullType()	-- minor change to improve compatibility
				if canonName  == "Base.Silencer" then		-- as long as no other mod has module named Silencer *and* has item called Silencer and HMSilencer
					inventoryItem:setSoundVolume(silencedVolumeValue*ssv)   -- then we got good compatibility
					inventoryItem:setSoundRadius(silencedSoundRadius*ssr)	-- there's still room to improve
					inventoryItem:setSwingSound(silencedSwingSound)			-- like having an item GUID then methods/functions to test GUID
					return
				elseif canonName == "Base.MetalPipeSilencer" then
					inventoryItem:setSoundVolume(metalPipeSilencedVolumeValue*ssv)
					inventoryItem:setSoundRadius(metalPipeSilencedSoundRadius*ssr)
					inventoryItem:setSwingSound(craftedSilencedSwingSound)
					return
				end
			end
			if SimpleSilencersModelTable[scriptItem:getFullName()] ~= nil then -- this new unmount silencer code should compatible with any other mods now
				inventoryItem:setSoundVolume(ssv)							 -- but might have potential problem with mods that
				inventoryItem:setSoundRadius(ssr)							 -- modify vanilla weapons
				inventoryItem:setSwingSound(scriptItem:getSwingSound())
			end
		end
	end
end

Events.OnEquipPrimary.Add(silencerOnEquipPrimary)

Events.OnGameStart.Add(function()											 -- make sure our player is setup on game start
	local player = getPlayer()
	silencerOnEquipPrimary(player, player:getPrimaryHandItem())
end)