﻿revoke_title_interaction = {

	category = interaction_category_vassal
	common_interaction = yes
	highlighted_reason = HIGHLIGHTED_HAS_REVOKE_TITLE_REASON
	notification_text = REVOKE_TITLE_PROPOSAL

	desc = revoke_title_interaction_desc

	special_interaction = revoke_title_interaction
	interface = revoke_title
	target_type = title
	target_filter = recipient_domain_titles
	ai_maybe = yes
	can_send_despite_rejection = yes
	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 60
	ai_min_reply_days = 4
	ai_max_reply_days = 9

	on_decline_summary = general_rebellion_decline_summary

	# actor character giving the titles
	# recipient character receiving the titles

	is_shown = {
		scope:recipient = {
			is_landed = yes
			# should be shown but be disabled for indirect vassals
			target_is_liege_or_above = scope:actor
			# is_vassal_of = scope:actor
		}

		trigger_if = {
			limit = {
				scope:actor = {
					is_ai = yes
				}
				# No point revoking barons at this time
				scope:recipient.primary_title.tier >= tier_county
			}
		}
	}
	
	is_valid_showing_failures_only = {
		NOT = { scope:actor = { is_at_war_with = scope:recipient } }
		scope:actor = {
			custom_description = {
				text = "character_interactions_revoke_title_from_ally_without_reason"
				object = scope:recipient
				OR = {
					NOT = {
						is_allied_to = scope:recipient
					}
					has_revoke_title_reason = scope:recipient
				}
			}
			trigger_if = {
				limit = { has_government = tribal_government }
				custom_description = {
					text = "has_law_allowing_title_revocation_tribal"
					has_realm_law_flag = title_revocation_allowed
				}
			}
			trigger_if = {
				limit = {
					NOT = { has_government = tribal_government }
				}
				custom_description = {
					text = "has_law_allowing_title_revocation_crown"
					has_realm_law_flag = title_revocation_allowed
				}
			}
			trigger_if = {
				limit = {
					scope:recipient = {
						vassal_contract_has_flag = vassal_contract_cannot_revoke_titles
					}
				}
				custom_description = {
					text = vassal_contract_forbids_revocation
					scope:recipient = {
						NOT = { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles }
					}
				}
			}
		}
		scope:recipient = {
			is_vassal_of = scope:actor
			is_diplomatically_available = yes
			NOT = { has_strong_hook = scope:actor }
		}
	}

	can_send = {
		scope:actor = {
			custom_description = {
				text = "character_interactions_hostile_actions_disabled_delay"
				NOT = { has_character_flag = flag_hostile_actions_disabled_delay }
			}
		}
	}
	
	# Highlighted in the interaction menu when right-clicking a character
	is_highlighted = {
		OR = {
			scope:actor = {
				has_revoke_title_reason = scope:recipient
			}
			scope:recipient = {
			 	any_held_title = {
					scope:actor = {
						has_claim_on = prev
					}
				}
			}
		}
	}
	
	# whether a character can be picked for the interaction, interface only (e.g. picking the first character in the marriage interaction)
	# in character scope, actor and recipient event tagets can be used, but not the secondary characters
	can_be_picked = {
		AND =  {
			NOT = {
				scope:actor = scope:recipient	
			}
 			scope:recipient = {
				is_vassal_of = scope:actor
			}
		}
	}
	
	has_valid_target_showing_failures_only = {
		scope:target = {
			is_contested = no
			NOT = {
				recent_history = {
					type = granted
					years = 1
				}
			}
			custom_description = {
				text = "character_interactions_not_rel_head_title"
				NOT = { exists = controlled_faith }
			}
			
			###Imperial Mod###
			custom_description = {
				text = "character_interactions_not_patrician_estate"
				NOR = { 
					is_landless_patrician_title_trigger = yes
					AND = { #prevent revocation of player's last county
						holder = { 
							has_government = patrician_government 
							is_ai = no
						}
						tier = tier_county
						this = holder.capital_county
					}					
				}	
			}
		}
	}

	#Use hook
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = SCHEME_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	send_options_exclusive = no

	on_send = {
		scope:actor = {
			add_character_flag = {
				flag = flag_hostile_actions_disabled_delay
				days = 10
			}
		}
	}

	on_auto_accept = {
		scope:recipient = {
			trigger_event = char_interaction.0200
		}
	}

	on_accept = {
		###Imperial mod###
		#If last title title, try and create patrician title
		if = {
			limit = {
				scope:actor = {
					OR = {
						has_government = imperial_government
						top_liege = { has_government = imperial_government }
					}
				}
				scope:target = { tier <= tier_county }
				scope:recipient = {
					NOT = { any_held_title = { is_landless_patrician_title_trigger = yes } }
					domain_size < 2
					OR = {
						has_estates_trigger = yes
						primary_title.current_heir = { has_estates_trigger = yes }
					}	
					OR = {
						is_ai = no
						top_liege = { below_patrician_limit_trigger = yes }
					}
				}
			}
			scope:recipient = {
				custom_tooltip = revoke_title_CREATE_PATRICIAN
				hidden_effect = {
					new_patrician_setup_effect = { LIEGE = scope:recipient.top_liege PREDECESSOR = scope:recipient }
					top_liege = { 
						send_interface_toast = {
							title = NEW_PATRICIAN_TITLE
							custom_tooltip = NEW_PATRICIAN_DESC
							left_icon = scope:new_patrician
						}
					}
				}
			}	
		}
		
		revoke_title_interaction_effect = yes

		save_scope_value_as = {
			name = revoke_title_interaction
			value = yes
		}
		
		scope:actor = {
			trigger_event = char_interaction.0199
		}

		if = {
			limit = {
				scope:actor = { has_character_flag = flag_hostile_actions_disabled_delay }
			}
			scope:actor = { remove_character_flag = flag_hostile_actions_disabled_delay }
		}
	}

	on_decline = {
		war_for_revoke_effect = yes
		scope:recipient = {
			add_opinion = {
				target = scope:actor
				modifier = revoked_title
			}
		}
		if = {
			limit = {
				scope:actor = { has_realm_law_flag = vassal_refusal_is_treason }
			}
			scope:actor = {
				add_opinion = {
					target = scope:recipient
					modifier = treasonous_revoke_refusal
				}
			}
		}
		if = {
			limit = {
				scope:actor = { has_character_flag = flag_hostile_actions_disabled_delay }
			}
			scope:actor = { remove_character_flag = flag_hostile_actions_disabled_delay }
		}
	}

	auto_accept = {
		OR = {
			scope:recipient = {
				highest_held_title_tier = tier_barony
			}
			scope:recipient = {
				is_imprisoned_by = scope:actor
			}
			custom_description = {
				text = "spending_hook"
				subject = scope:actor
				object = scope:recipient
				scope:hook = yes
				scope:actor = { has_strong_hook = scope:recipient }
			}
		}
	}
	ai_accept = {
		base = 0 # Try to make it 0 for most interactions

		modifier = {
			scope:hook = yes
			add = 40
			desc = SCHEME_WEAK_HOOK_USED
		}

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.8
			desc = AI_OPINION_REASON
		}
		
		modifier = {
			add = 20
			scope:actor = {
				has_realm_law_flag = vassal_refusal_is_treason
			}
			desc = AI_REFUSAL_IS_TREASON
		}
		modifier = {
			exists = scope:actor.primary_title.title_capital_county
			OR = {
				scope:landed_title = scope:actor.primary_title.title_capital_county
				scope:landed_title = scope:actor.primary_title.title_capital_county.de_jure_liege
			}
			add = 50
			desc = AI_TITLE_IS_REALM_CAPITAL
		}
		modifier = {
			add = 30
			scope:actor = {
				has_claim_on = scope:landed_title
			}
			desc = AI_LIEGE_HAS_CLAIM_ON_TITLE
		}

		ai_value_modifier = {
			who = scope:recipient
			ai_greed = -0.75
	  	  	trigger = {
	  	  		scope:recipient = { NOT = { ai_greed = 0 } }
	  		}
		}
		ai_value_modifier = {
			who = scope:recipient
			ai_honor = 0.5
			trigger = {
				scope:recipient = { #If AI is honorable and the law compels to obey the liege, AI will be more likely to obey.
					ai_honor > 0
				}
				scope:actor = {
					has_realm_law_flag = vassal_refusal_is_treason
				}
			}
		}
		modifier = { #I am a King!
			desc = offer_vassalization_interaction_aibehavior_amkingtier_tt
			trigger = {
				scope:recipient = { highest_held_title_tier = tier_kingdom }
			}
			add = -20
		}
		modifier = { #Title is part of vassal's primary title de-jure.
			add = -25
			OR = {
				scope:recipient.primary_title = {
					is_de_jure_liege_or_above_target = scope:landed_title
				}
				scope:recipient.primary_title = scope:landed_title
			}
			desc = AI_REFUSAL_IS_DE_JURE_UNDER
		}
		modifier = {
			add = 25
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = 50
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		modifier = { #Comparative military strength.
			desc = offer_vassalization_interaction_aibehavior_power_tt
	  	  	add = {
				value = 1
					subtract = {
					value = scope:recipient.max_military_strength
					divide = { value = scope:actor.max_military_strength min = 1 }
					}
				multiply = 50
	  		}
		}
	}

	# AI
	ai_targets = {
		ai_recipients = vassals
	}
	ai_frequency = 0 # This is handled via code instead; see REVOKE_TITLE_TICK in the AI defines. All the logic for *if* to send a revoke though is scripted below

	ai_potential = {
		has_realm_law_flag = title_revocation_allowed
		primary_title.tier > tier_county
	}

	ai_will_do = {
		base = -1000

		# We want to avoid tyranny unless we've got a good reason
		# And even if we can avoid tyranny we don't want to revoke just because we can, so this only gets us to -10
		# Certain characters will revoke despite tyranny, if their rationality is sufficiently low
		modifier = {
			scope:actor = {
				OR = {
					NOT = {
						title_revocation_is_tyrannical_trigger = yes
					}
					AND = {
						ai_rationality <= very_high_negative_ai_value
						OR = {
							has_trait = lunatic
							has_trait = possessed
							has_trait = arbitrary
						}
					}
				}
			}

			add = 990
		}
		# Always revoke preferred capital and capital duchy
		modifier = {
			exists = scope:actor.primary_title.title_capital_county
			OR = {
				scope:landed_title = scope:actor.primary_title.title_capital_county
				scope:landed_title = scope:actor.primary_title.title_capital_county.de_jure_liege
			}
			NAND = { # Don't revoke player titles tyranically, it must be possible to play and succeed as Ulm!
				scope:landed_title.holder = {
					is_ai = no
				}
				scope:actor = {
					title_revocation_is_tyrannical_trigger = yes
				}
			}
			add = 1000
		}
		# Revoke county we're dejure liege of if below domain limit
		modifier = {
			scope:actor.domain_size < scope:actor.domain_limit
			scope:landed_title.tier = tier_county
			scope:landed_title.de_jure_liege.holder = scope:actor
			add = 500
		}
		# Revoke from vassals of the wrong faith if you can do it without tyranny
		modifier = {
			NOT = { scope:actor.faith = scope:recipient.faith }
			OR = {
				NOT = { scope:actor.faith.religion = scope:recipient.faith.religion }
				scope:actor.ai_zeal >= low_negative_ai_value
			}
			OR = { # Chill a bit until you've dealth with the ones already in your prison
				scope:recipient = {
					is_imprisoned_by = scope:actor
				}
				NOT = {
					any_vassal = {
						NOT = { faith = scope:actor.faith }
						primary_title.tier > tier_barony
						is_imprisoned_by = scope:actor
						time_in_prison < { years = 1 }
					}
				}
			}
			add = 100
		}
		# Revoke from characters you dislike if you can do it without tyranny
		modifier = {
			scope:actor = {
				OR = {
					has_relation_rival = scope:recipient
					has_relation_nemesis = scope:recipient
				}
			}
			add = 100
		}
		# Slight preference for higher-tier titles
		modifier = {
			add = scope:landed_title.tier
		}
		# Do not revoke titles from certain characters
		modifier = {
			scope:actor = {
				OR = {
					has_relation_friend = scope:recipient
					has_relation_best_friend = scope:recipient
					has_relation_lover = scope:recipient
					has_relation_soulmate = scope:recipient
					is_heir_of = scope:recipient
				}
			}
			add = -2000
		}
		# Do not revoke titles when the chance of success if exceedingly low
		modifier = {
			NOT = {
				scope:recipient = {
					is_imprisoned_by = scope:actor
				}
			}
			scope:actor = {
				OR = {
					gold < minimum_ai_gold_value_for_tyranny_wars
					AND = {
						scope:actor.max_military_strength < scope:recipient.max_military_strength
						gold < comfortable_ai_gold_value_for_tyranny_wars
					}
				}
			}
			add = -2000
		}
		
		modifier = { # The AI will only use a Hook if they couldn't otherwise do this
			scope:hook = yes
			add = -1
		}

		modifier = { # The AI shouldn't ruin the player's fun
			add = -2000
			scope:recipient = { has_character_flag = is_party_baron }
		}

		modifier = { # Avoid triggering Tyranny wars mid-war
			factor = 0
			scope:actor = {
				is_at_war = yes
			}
			scope:recipient = {
				NOT = {
					is_imprisoned_by = scope:actor
				}
			}
		}
	}
}
