﻿revoke_title_interaction_effect = {
	if = {
		limit = {
			scope:recipient = {
				faith = scope:actor.faith
				government_has_flag = government_is_theocracy
			}
			NOT = { scope:actor = scope:actor.faith.religious_head } #No loss if actor is head of faith themselves
		}
		scope:actor = {
			add_piety = major_piety_loss
		}
		if = {
			limit = {
				exists = scope:actor.faith.religious_head
			}
			scope:actor.faith.religious_head = {
				add_opinion = {
					target = scope:actor
					modifier = impious_opinion
					opinion = -20
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:actor.faith.religious_head
			scope:recipient = scope:actor.faith.religious_head
		}
		scope:actor = {
			add_piety_level = -2
		}
	}
	if = {
		limit = {
			scope:landed_title.tier != tier_barony
		}
		scope:actor = {
			revocation_tyranny_effect = { VASSAL = scope:recipient }
			title_revocation_stress_effect = { VASSAL = scope:recipient }
			consume_revoke_title_reason = scope:recipient
		}
		scope:recipient = {
			if = {
				limit = {
					NOT = { exists = scope:administrative_revocation }
					is_landed_or_landless_administrative = no
				}
				add_pressed_claim = scope:landed_title
			}
			if = {
				limit = {
					is_a_faction_member = yes
				}
				add_joined_faction_discontent = 25
			}
		}
		
		if = {
			limit = { scope:landed_title.tier = tier_county }
			scope:landed_title = {
				change_county_control = {
					value = -30
					if = {
						limit = {
							NOT = { scope:recipient.culture = scope:actor.culture }
							scope:recipient.culture = this.culture
						}
						multiply = 1.5
					}
					if = {
						limit = {
							NOT = { scope:recipient.faith = scope:actor.faith }
							scope:recipient.faith = this.faith
						}
						multiply = 1.5
					}
				}
			}
		}

		if = {
			limit = {
				scope:actor.culture = { has_cultural_parameter = penalty_for_revoking_titles_from_house_members }
				exists = scope:actor.house
				exists = scope:recipient.house
				scope:actor.house = scope:recipient.house
			}
			if = {
				limit = {
					scope:landed_title.tier = tier_county
				}
				scope:actor = {
					add_prestige = medium_prestige_loss
				}
			}
			else_if = {
				limit = {
					scope:landed_title.tier = tier_duchy
				}
				scope:actor = {
					add_prestige = major_prestige_loss
				}
			}
			else_if = { #Empire
				limit = {
					scope:landed_title.tier = tier_kingdom
				}
				scope:actor = {
					add_prestige = massive_prestige_loss
				}
			}
		}

		if = {
			limit = {
				scope:actor.culture = { has_cultural_parameter = penalty_for_revoking_titles_from_close_family }
				exists = scope:actor
				exists = scope:recipient
				scope:actor = {
					is_close_family_of = scope:recipient
				}
			}
			if = {
				limit = {
					scope:landed_title.tier = tier_county
				}
				scope:actor = {
					add_prestige = medium_prestige_loss
				}
			}
			else_if = {
				limit = {
					scope:landed_title.tier = tier_duchy
				}
				scope:actor = {
					add_prestige = major_prestige_loss
				}
			}
			else_if = { #Empire
				limit = {
					scope:landed_title.tier = tier_kingdom
				}
				scope:actor = {
					add_prestige = massive_prestige_loss
				}
			}
		}

		if = {
			limit = {
				scope:actor.culture = { has_cultural_parameter = piety_penalty_for_revoking_titles_from_house_members }
				exists = scope:actor.house
				exists = scope:recipient.house
				scope:actor.house = scope:recipient.house
			}
			if = {
				limit = {
					scope:landed_title.tier = tier_county
				}
				scope:actor = {
					add_piety = medium_piety_loss
				}
			}
			else_if = {
				limit = {
					scope:landed_title.tier = tier_duchy
				}
				scope:actor = {
					add_piety = major_piety_loss
				}
			}
			else = { #Can't revoke empire titles anyway
				limit = {
					scope:landed_title.tier = tier_kingdom
				}
				scope:actor = {
					add_piety = massive_piety_loss
				}
			}
		}
				
		# If we're a clan this interaction affects unity
		if = {
			limit = {
				scope:actor = { government_has_flag = government_is_clan }
				scope:recipient = { government_has_flag = government_is_clan }
				exists = scope:actor.house
				exists = scope:recipient.house
				scope:actor.house = scope:recipient.house
				scope:landed_title.tier >= tier_county
			}
			scope:actor.house = {
				if = {
					limit = {
						scope:landed_title.tier = tier_county
					}
					save_scope_value_as = {
						name = revoke_title_unity_loss
						value = minor_unity_loss
					}
				}
				if = {
					limit = {
						scope:landed_title.tier = tier_duchy
					}
					save_scope_value_as = {
						name = revoke_title_unity_loss
						value = medium_unity_loss
					}
				}
				else_if = {
					limit = {
						scope:landed_title.tier = tier_kingdom
					}
					save_scope_value_as = {
						name = revoke_title_unity_loss
						value = major_unity_loss
					}
				}
				if = {
					limit = { exists = scope:revoke_title_unity_loss }
					add_clan_unity_interaction_effect = {
						CHARACTER = scope:actor
						TARGET = scope:recipient
						VALUE = scope:revoke_title_unity_loss
						DESC = clan_unity_title_revoked.desc
						REVERSE_NON_HOUSE_TARGET = no
					}
				}
			}
		}

		# EP3 - Become Adventurer - Revocation
		scope:recipient = {
			if = {
				limit = {
					has_ep3_dlc_trigger = yes
					is_valid_for_laampdom = yes
				}
				# Plus for our memory variable.
				primary_title = { save_scope_as = lost_primary_title }
				custom_tooltip = {
					text = laamp_may_become_adventurer_tt
					trigger_event = {
						id = ep3_laamps.0041
						delayed = yes
					}
				}
			}
		}		
	}
	else = {
		if = {
			limit = {
				NOT = { exists = scope:revoke_title_interaction }
			}
			custom_tooltip = IS_ALLOWED_TO_REVOKE_TITLE_BARONY_DESC
			scope:actor = {
				if = {
					limit = {
						any_vassal = {
							has_vassal_stance = barons_and_minor_landholders
						}
					}
					custom_tooltip = barons_and_minor_landholders_disapproves_baron_revocations
					hidden_effect = {
						every_vassal = {
							limit = {
								has_vassal_stance = barons_and_minor_landholders
							}
							add_opinion = {
								target = scope:actor
								modifier = barons_and_minor_landholders_disapproves_baron_revocations_opinion
							}
						}
					}
				}
			}			
		}
	}


	scope:actor = {
		if = {
			limit = {
				scope:hook = yes
				scope:recipient = { is_alive = yes }
			}
			use_hook = scope:recipient
		}
		if = {
			limit = {
				scope:hook_liege ?= yes
				scope:intermediary = { is_alive = yes }
			}
			use_hook = scope:intermediary
		}
	}
	scope:recipient = {
		add_opinion = {
			target = scope:actor
			modifier = revoked_title
		}

		###esr
		if = {
			limit = {
				OR = {
					has_relation_friend = scope:actor
					has_relation_lover = scope:actor
				}
			}
			add_opinion = {
				target = scope:actor
				modifier = betrayed_me_opinion
				opinion = -30
			}
		}

		if = {
			limit = {
				OR = {
					has_relation_best_friend = scope:actor
					has_relation_soulmate = scope:actor
				}
			}
			add_opinion = {
				target = scope:actor
				modifier = betrayed_me_opinion
				opinion = -60
			}
		}

		if = {
			limit = { can_set_relation_rival_if_adult_trigger = { CHARACTER = scope:actor } }
			random = {
				chance = 20
				modifier = {
					factor = 0.5
					scope:recipient = { ai_vengefulness <= medium_negative_vengefulness }
				}
				modifier = {
					factor = 2
					scope:recipient = { ai_vengefulness >= medium_positive_vengefulness }
				}
				modifier = {
					factor = 2
					scope:recipient = { ai_vengefulness >= high_positive_vengefulness }
				}
				progress_towards_rival_effect = {
					REASON = rival_revoked_title_progress
					CHARACTER = scope:actor
					OPINION = 0
				}
			}
		}


		if = {
			limit = {scope:actor = {title_revocation_is_tyrannical_trigger = { VASSAL = scope:recipient }}}
			every_spouse = {
				limit = { NOT = { this = scope:actor } }
				add_to_temporary_list = close_family_revoke_opinion_list
				add_to_temporary_list = esr_spouse_revoke_opinion_list		
			}
	
			every_close_or_extended_family_member ={
				limit = { NOT = { this = scope:actor } }
				add_to_temporary_list = close_family_revoke_opinion_list
				###Note close_family_revoke_opinion_list stores both close and extened family in esr
			}
	
			if = {
				limit = {
					any_in_list = {
						list = close_family_revoke_opinion_list
						always = yes
					}
				}
				every_in_list = {
					list = close_family_revoke_opinion_list
					custom = all_family_members
	
					if = {
						limit = {
							OR = {
								is_close_family_of = scope:recipient
								is_in_list = esr_spouse_revoke_opinion_list
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = esr_revoked_close_family
						}					
					}
	
					else = {
						add_opinion = {
							target = scope:actor
							modifier = esr_revoked_extended_family
						}		
					}
				}
			}
	
			#recipient's friends and lovers
			every_relation = {
				type = friend
				limit = {
					NOR = {
						this = scope:recipient
						this = scope:actor
						has_relation_best_friend = scope:recipient				
					}
				}
				add_to_list = recipient_close_relations_list
			}
			every_relation = {
				type = lover
				limit = {
					NOR = {
						this = scope:recipient
						this = scope:actor
						has_relation_soulmate = scope:recipient
						is_in_list = recipient_close_relations_list
					}
				}
				add_to_list = recipient_close_relations_list
			}
			if = {
				limit = {
					any_in_list = {
						list = recipient_close_relations_list
						always = yes
					}
				}
				every_in_list = {
					list = recipient_close_relations_list
					custom = all_friends_and_lovers
					add_opinion = {
						target = scope:actor
						modifier = esr_revoked_close_relation_opinion
					}
				}
			}
	
			#recipient's best friend and soulmate
			every_relation = {
				type = best_friend
				limit = { NOT = {this = scope:actor}}
				add_opinion = {
					target = scope:actor
					modifier = esr_revoked_best_friend
				}
			}
	
			every_relation = {
				type = soulmate
				limit = { NOT = {this = scope:actor}}
				add_opinion = {
					target = scope:actor
					modifier = esr_revoked_soulmate
				}
			}
	
			#recipient's rivals and nemesis
			every_relation = {
				type = rival
				limit = {
					NOR = {
						this = scope:actor
						has_relation_nemesis = scope:recipient
						has_trait = compassionate
					}
				}
				custom = esr_rivals_execution
				add_opinion = {
					target = scope:actor
					modifier = esr_revoked_rival
				}
			}
	
			every_relation = {
				type = nemesis
	
				limit = {
					NOR = {
						this = scope:actor
						has_trait = compassionate
					}
				}
				add_opinion = {
					target = scope:actor
					modifier = esr_revoked_nemesis
				}
			}
		}
		###esr/

		if = { #If they recently imprisoned you, or asked for your excommunication, they become your rival
			limit = {
				OR = {
					has_opinion_modifier = {
						target = scope:actor
						modifier = released_from_prison
					}
					AND = {
						exists = var:requested_my_excommunication
						var:requested_my_excommunication = scope:actor
					}
				}
				NOT = {
					has_relation_rival = scope:actor
				}
			}
			if = {
				limit = {
					has_relation_potential_rival = scope:actor
				}
				remove_relation_potential_rival = scope:actor
			}
			set_relation_rival = {
				target = scope:actor
				reason = rival_revoked_title_reversed
			}
		}
		else = {
			progress_towards_rival_effect = {
				CHARACTER = scope:actor
				REASON = rival_revoked_title_progress
				OPINION = 0
			}
		}
	}

	# Penalty for taking the Party Baron's last title. I mean, come on...
	if = {
		limit = {
			scope:recipient = {
				has_character_flag = is_party_baron
				NOT = {
					any_held_title = {
						NOT = { this = scope:landed_title }
					}
				}
			}
		}
		scope:actor = {
			add_character_modifier = {
				modifier = party_baron_ended_the_party_modifier
				years = 10
			}
			random_owned_story = {
				limit = {
					story_type = story_party_baron
				}
				end_story = yes
			}
		}
	}
}