--[[
	=====================================================================
	Addon      : Tosox Mini Mods Repo: Companions dont blow Disguises Enhanced
	Link       : https://www.moddb.com/mods/stalker-anomaly/addons/tosox-mini-mods-repo/
	Author     : Tosox
	Credits    : Vintar0
	Date       : 31.01.2024
	Last Edit  : 10.03.2024
	=====================================================================
--]]

-- Enable companions to not blow the disguise
orig_gameplay_disguise_npc_on_update = gameplay_disguise.npc_on_update
gameplay_disguise.npc_on_update = function(npc)
    if (not gameplay_disguise.is_enabled) or (not gameplay_disguise.default_comm) or (not db.actor) then
		return
	end

    local id = npc:id()
    if (npc:see(db.actor)) and (npc:dont_has_info("npcx_is_companion")) then
        orig_gameplay_disguise_npc_on_update(npc)
    -- Copied from gameplay_disguise.script
    elseif gameplay_disguise.npcs_memory[id] then
        gameplay_disguise.npcs_memory[id].first_seen = nil
		local last_seen = utils_data.CTime_from_table(gameplay_disguise.npcs_memory[id].last_seen)
		local time_passes = game.get_game_time():diffSec(last_seen)
		if (time_passes > gameplay_disguise.npcs_memory[id].memo) then
			gameplay_disguise.npcs_memory[id] = nil
		end
    end
end

-- Allow changing outfits with companions around
orig_gameplay_disguise_anybody_see = gameplay_disguise.anybody_see
gameplay_disguise.anybody_see = function(t)
	local actor = db.actor
	for i=1, #db.OnlineStalkers do
		local st = db.storage[db.OnlineStalkers[i]]
		local npc = st and st.object or level.object_by_id(db.OnlineStalkers[i])
		if (npc and IsStalker(npc,npc:clsid()) and npc:alive()) then
			if t then
				if t[npc:community()] and (npc:see(actor)) and (npc:dont_has_info("npcx_is_companion")) then
					return true
				end
			elseif (npc:see(actor)) and (npc:dont_has_info("npcx_is_companion")) then
				return true
			end
		end
	end
	return false
end