--	отношение персонажа к актеру (или другому NPC) вычисляется по формуле:
--	attitude = personal_goodwill +				-- личное отношение персонажа к актеру (если раньше не встречались, то 0)
--			   community_goodwill +				-- отношение группировки персонажа лично к актеру (если раньше контактов не было, то 0)
--			   community_to_community +			-- отношение группировки персонажа к группировке актера из [communities_relations]
--             reputation_goodwill +			-- отношение репутации персонажа к репутации актера из [reputation_relations]
--			   rank_goodwill					-- отношение ранга персонажа к рангу актера из [rank_relations]
local FRIENDS = 1000
local NEUTRALS = 0
local ENEMIES = -1000
default_sympathy = 0.01
temp_goodwill_table = {}
function set_factions_community(faction, faction_to, new_community)
    if(faction~=nil) and (faction~="none") and (faction_to~="none") then
		local community = 0
		if(new_community=="enemy") then
			community = -5000
		elseif(new_community=="friend") then
			community = 5000
		end
		set_factions_community_num(faction, faction_to, community)
	else
		--printf("No such faction community: "..tostring(faction))
	end
end

function set_factions_community_num(faction, faction_to, new_community_num)
    if(faction~=nil) and (faction~="none") and (faction_to~="none") then
		relation_registry.set_community_relation(faction, faction_to, new_community_num)
	else
		--printf("No such faction community: "..tostring(faction))
	end
end

function get_factions_community(faction, faction_to)
    if(faction~=nil) and (faction~="none") and (faction_to~="none") then
		return relation_registry.community_relation(faction, faction_to)
	else
		--printf("No such faction community: "..tostring(faction))
		return nil
	end
end

function is_factions_friends(faction, faction_to)
    if(faction~=nil) and (faction~="none") and (faction_to~="none") then
		return relation_registry.community_relation(faction, faction_to)>=FRIENDS
	else
		--printf("No such faction community: "..tostring(faction))
		return false
	end
end

function is_factions_enemies(faction, faction_to)
    if(faction~=nil) and (faction~="none") and (faction_to~="none") then
		return relation_registry.community_relation(faction, faction_to)<=ENEMIES
	else
		--printf("No such faction community: "..tostring(faction))
		return false
	end
end

function get_npcs_relation(npc1, npc2)
	return npc1 and npc2 and npc1:relation(npc2)
end

function set_npcs_relation(npc1, npc2, new_relation)
    local goodwill = 0
    if(new_relation=="enemy") then
        goodwill = -1000
    elseif(new_relation=="friend") then
        goodwill = 1000
    end
	if npc1 and npc2 then
	    npc1:set_goodwill(goodwill, npc2)
	else
		abort("Npc not set in goodwill function!!!")
	end
end

function get_npc_sympathy(npc)
	return npc:sympathy()
end

function set_npc_sympathy(npc, new_sympathy)
	if(new_sympathy<0) then
		new_sympathy = 0
	elseif(new_sympathy>1) then
		new_sympathy = 1
	end
	if npc then
		npc:set_sympathy(new_sympathy)
	else
		abort("Npc not set in sympathy function!!!")
	end
end

function set_squad_goodwill(squad_id, new_goodwill)
	local board = sim_board.get_sim_board()
    local goodwill = 0
    if(new_goodwill=="enemy") then
        goodwill = -1000
    elseif(new_goodwill=="friend") then
        goodwill = 1000
    end
	if board.squads[squad_id] then
	    for k,v in pairs(board.squads[squad_id].squad_npc) do
	        local obj = db.storage[v] and db.storage[v].object
	        if obj and db.actor then
	            obj:set_goodwill(goodwill, db.actor)
	            if not(db.goodwill.relations) then
	                db.goodwill.relations = {}
	            end
	            db.goodwill.relations[k] = new_goodwill
	        end
	    end
	    board.squads[squad_id].relationship = new_goodwill
	else
		abort("There is no squad [%s] in sim_board", squad_id)
	end
end

function set_squad_goodwill_to_npc(npc, squad_id, new_goodwill)
	local board = sim_board.get_sim_board()
    local goodwill = 0
    if(new_goodwill=="enemy") then
        goodwill = -1000
    elseif(new_goodwill=="friend") then
        goodwill = 1000
    end
	if board.squads[squad_id] then
	    for k,v in pairs(board.squads[squad_id].squad_npc) do
	        local obj = db.storage[v] and db.storage[v].object
	        if obj and npc then
	            obj:set_goodwill(goodwill, npc)
	        end
	    end
	else
		abort("There is no squad [%s] in sim_board", squad_id)
	end
end

function set_squad_community_goodwill(squad_id, community, new_goodwill)
	local board = sim_board.get_sim_board()
    local goodwill = 0
    if(new_goodwill=="enemy") then
        goodwill = -1000
    elseif(new_goodwill=="friend") then
        goodwill = 1000
    end
	if board.squads[squad_id] then
	    for k,v in pairs(board.squads[squad_id].squad_npc) do
	        local obj = db.storage[v] and db.storage[v].object
	        if(obj) then
	            obj:set_community_goodwill(community, goodwill)
	        end
	    end
	else
		abort("There is no squad [%s] in sim_board", squad_id)
	end
end

function set_level_faction_community(obj)
	if(temp_goodwill_table.communities~=nil) then
	    for k,v in pairs(temp_goodwill_table.communities) do
			if(obj:character_community()==k) then
			    for kk,vv in pairs(v) do
					if(kk==obj:id()) and db.actor then
						relation_registry.set_community_goodwill(k, db.actor:id(), vv)
--	run_string xr_effects.set_level_faction_community(nil, nil, {"bandit", "peacemaker_selo", "friend"})
                        obj:set_goodwill(vv, db.actor)
						v[kk] = nil
					end
				end
			end
		end
	end
end

function check_all_squad_members(squad_name, goodwill)
	local squad = sim_board.get_sim_board().squads[squad_name]
	if squad == nil then return false end
	if db.actor == nil then return false end
	for k,v in pairs(squad.squad_npc) do
		local is_enemy
		if goodwill == "enemy" then
			--printf("npc id  = [%s]", k)
		--	if (db.storage[k] ~= nil) and (db.storage[k].object ~= nil) then
			--	printf("goodwill is [%s]", tostring(db.storage[k].object:general_goodwill(db.actor)))
			--end
			is_enemy = db.storage[k] and db.storage[k].object and db.storage[k].object:general_goodwill(db.actor)<=-1000
		else
			is_enemy = db.storage[k] and db.storage[k].object and db.storage[k].object:general_goodwill(db.actor)>=1000
		end
		if is_enemy then
			return true
		end
	end
	return false
end

function get_squad_goodwill_to_actor(squad_name)
	local squad = sim_board.get_sim_board().squads[squad_name]
	if(squad==nil) then
		abort("No such squad %s in board", tostring(squad_name))
		return false
	end
	if(squad.relationship~=nil) then
		--printf(" squad_relation %s", tostring(squad.relationship))
		return squad.relationship
	else
		local goodwill = "neutral"
		if(relation_registry.community_relation(squad.player_id, alife():actor():community())>=1000) then
			goodwill = "friend"
		elseif(relation_registry.community_relation(squad.player_id, alife():actor():community())<=-1000) then
			goodwill = "enemy"
		end
		return goodwill
	end
end

function is_squad_enemy_to_actor(squad_name)
	return get_squad_goodwill_to_actor(squad_name)=="enemy"
end

function is_squad_friend_to_actor(squad_name)
	return get_squad_goodwill_to_actor(squad_name)=="friend"
end

function is_squad_neutral_to_actor(squad_name)
	return get_squad_goodwill_to_actor(squad_name)=="neutral"
end

function set_gulag_relation_actor(smart_name, relation)
	local gulag = xr_gulag.get_gulag_by_name(smart_name)
	local goodwill = 0
	if(relation=="enemy") then
		goodwill = -1000
	elseif(relation=="friend") then
		goodwill = 1000
	end
	for k,v in pairs(gulag.npc_info) do
	    local object = db.storage[v.se_obj.id] and db.storage[v.se_obj.id].object
        if(object) then
            object:set_goodwill(goodwill, db.actor)
            object:set_community_goodwill(db.actor:character_community(), goodwill)
        end
    end
end

function get_gulag_relation_actor(smart_name, relation)
    local gulag = xr_gulag.get_gulag_by_name(smart_name)
	if gulag then
	    local goodwill = 0
	    local npc_count = 0
	    for k,v in pairs(gulag.npc_info) do
	        local object = db.storage[v.se_obj.id] and db.storage[v.se_obj.id].object
	        if object and db.actor then
	            goodwill = goodwill + object:general_goodwill(db.actor)
	            npc_count = npc_count + 1
	        end
	    end
		if npc_count ~= 0 then
	        local delta = goodwill/npc_count
	        if relation == "enemy" and delta <= -1000 then
	            return true
	        elseif relation == "friend" and delta >= 1000 then
	            return true
	        elseif relation == "neutral" and delta < 1000 and delta > -1000 then
	            return true
	        end
		end
	end
	return false
end