﻿offer_nato_interaction = {
	category = interaction_category_diplomacy
	icon = TMA_nato_interaction
	
	desc = offer_nato_interaction_desc

	ai_targets = {
		ai_recipients = neighboring_rulers_including_tributary_borders
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 8
		duchy = 8
		kingdom = 8 
		empire = 8
		hegemony = 8
	}
	popup_on_receive = yes
	pause_on_receive = yes

	common_interaction = yes

	cooldown_against_recipient = { years = 3 }

	is_shown = {
		scope:actor = {
			TMA_nato_goverments_allowed = yes
			is_playable_character = yes
			OR = {
				is_independent_ruler = yes
				confederation ?= { is_house_based = no }
			}
			
		}
		scope:recipient = {
			is_playable_character = yes
			is_independent_ruler = yes
			TMA_nato_goverments_allowed = yes
			NOT = { this = scope:actor }
			NOT = {is_confederation_member = yes}
		}
	}

	is_valid_showing_failures_only = {
		trigger_if = {
			limit = {
				scope:actor = { is_confederation_member = no }
			}
			custom_tooltip = {
				text = forming_nato_tt 
				scope:actor = {
					has_character_flag = forming_confederation
				}
			}
		}
		scope:actor = {
			is_imprisoned = no
		}
		scope:recipient = {
			valid_defensive_league_member_trigger = { CHARACTER = scope:actor } 
			is_imprisoned = no
			is_at_war = no
		}
	}

	cost = {
		prestige = {
			value = 0
			if = {
				limit = { scope:prestige_send_option = yes }
				add = scope:actor.minor_prestige_value
				desc = PRESTIGE_INTERACTION_ACCEPTANCE_SEND_OPTION
			}
		}
	}

	greeting = positive
	notification_text = OFFER_CONFEDERATION_INTERACTION_NOTIFICATION

	ai_accept = {
		base = -50
		
		# PERKS
		modifier = { # Perk boost
			desc = offer_vassalization_true_ruler_perk_tt
			trigger = {
				scope:actor = {
					has_perk = true_ruler_perk
					TMA_confederations = yes 
				}
			}
			add = true_ruler_value
		}
		modifier = { # Education 5 boost
			desc = offer_vassalization_education_diplomacy_5_tt
			trigger = {
				scope:actor = { has_trait_with_flag = offer_vassalisation_25 }
			}
			add = 25
		}

		# EVENTS - temporary bonuses gained by events
		modifier = {
			desc = event_bonus_to_vassal_accept_tt
			trigger = {
				scope:actor = { has_character_modifier = event_bonus_to_vassal_accept }
			}
			add = 20
		}


		# STRUGGLES - bonus gained by successful Sway scheme during the Persian Struggle
		modifier = {
			desc = fp3_persian_struggle_previously_swayed_tt
			trigger = {
				scope:recipient = {
					has_opinion_modifier = {
						modifier = scheme_sway_and_compelled_to_submit_opinion
						target = scope:actor
					}
				}
			}
			add = 20
		}

		modifier = {
			desc = fp3_rekindler_of_iran_modifier_reason
			trigger = {
				AND = {
					scope:actor = { dynasty ?={ has_dynasty_modifier = fp3_rekindler_of_iran_modifier } }
					scope:recipient = { culture = { has_cultural_pillar = heritage_iranian } }
				}
			}
			add = 20
		}


		modifier = { # Cultural Acceptance
			add = offer_vassalage_acceptance_value
			desc = cultural_acceptance_interaction_reason
			trigger = {
				scope:actor = {
					NOT = { has_same_culture_as = scope:recipient }
					culture = {
						cultural_acceptance = { target = scope:recipient.culture value <= 90 }
					}
				}
			}
		}

		# MAIN
		modifier = { #Different faith, no pluralism.
			desc = offer_vassalization_interaction_aibehavior_differentfaith_tt
			trigger = {
				scope:recipient = {
					NOR = { #Of two different faiths AND the potential vassal's faith is not pluralistic.
						faith = scope:actor.faith
						faith = { has_doctrine = doctrine_pluralism_pluralistic }
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_herder
					}
				}
			}
			add = {
				value = -30
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_hostile_level
							}
						}
					}
					add = -30
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_evil_level
							}
						}
					}
					add = -30
				}
			}
		}

		modifier = { #Different faith, pluralism.
			desc = offer_vassalization_interaction_aibehavior_differentfaith_tt
			trigger = {
				scope:recipient = {
					NOR = {
						faith = scope:actor.faith
						scope:actor.faith = { has_doctrine = doctrine_pluralism_pluralistic }
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_herder
					}
					faith = { has_doctrine = doctrine_pluralism_pluralistic }
				}
			}
			add = {
				value = -15
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_hostile_level
							}
						}
					}
					add = -15
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_evil_level
							}
						}
					}
					add = -15
				}
			}
		}

		modifier = { #Different faith, both have pluralism.
			desc = offer_vassalization_interaction_aibehavior_differentfaith_tt
			trigger = {
				scope:recipient = {
					NOR = {
						faith = scope:actor.faith
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_herder
					}
					scope:actor.faith = { has_doctrine = doctrine_pluralism_pluralistic }
					faith = { has_doctrine = doctrine_pluralism_pluralistic }
				}
			}
			add = {
				value = -10
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_hostile_level
							}
						}
					}
					add = -10
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_evil_level
							}
						}
					}
					add = -10
				}
			}
		}
		modifier = { #We just fought against each other.
			desc = offer_vassalization_interaction_aibehavior_recent_war_tt
			trigger = {
				scope:recipient = {
					any_truce_holder = {
						this = scope:actor
					}
				}
				# Ensure the truce wasn't purchased and is indeed from a war
				scope:actor = {
					NOT = {
						has_purchased_truce_with_char = { TARGET = scope:recipient }
					}
				}
			}
			add = -40
		}
		modifier = { #I fought an independence war against you.
			desc = offer_confederation_independence_war_tt
			trigger = {
				scope:recipient = {
					exists = var:independence_war_former_liege
					var:independence_war_former_liege = scope:actor.top_liege
				}
			}
			add = -100
		}
		modifier = { # Isolationist tradition
			desc = isolationist_reason
			trigger = {
				NOT = {
					scope:actor.culture = scope:recipient.culture
				}
				scope:recipient.culture = {
					has_cultural_tradition = tradition_isolationist
				}
			}
			add = -50
		}
		modifier = { #Bankrupt
			desc = bankrupt_reason
			trigger = {
				scope:actor.gold <= -1
			}
			add = -30
		}
		modifier = { #Wide difference in rank
			desc = offer_vassalization_interaction_aibehavior_widetitletier_tt
			trigger = {
				scope:actor = {
					tier_difference = {
						target = scope:recipient
						value > 1
					}
				}
			}
			add = 10
		}
		modifier = { # Allied
			desc = offer_vassalization_interaction_aibehavior_allied_tt
			trigger = {
				scope:recipient = {
					is_allied_to = scope:actor
				}
			}
			add = 50
		}
		modifier = { # Is the Rightful Liege of recipient
			desc = offer_vassalization_interaction_aibehavior_rightfulliegetitleholder_tt
			trigger = {
				scope:actor = { is_rightful_liege_of = scope:recipient }
			}
			add = 20
		}
		modifier = { # Encircled
			desc = offer_vassalization_interaction_aibehavior_encircled_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							NOT = {
								this = scope:actor
							}
						}
					}
					NOT = {
						any_realm_county = {
							is_coastal_county = yes
						}
					}
				}
			}
			add = 40
		}
		modifier = { #No adjacency
			desc = offer_vassalization_interaction_aibehavior_unconnectedrealm_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							OR = {
								this = scope:actor
								AND = {
									exists = scope:actor.confederation
									is_member_of_confederation = scope:actor.confederation
								}
								AND = {
									exists = scope:actor.confederation
									suzerain ?= {
										is_member_of_confederation = scope:actor.confederation
									}
								}
								suzerain ?= {
									this = scope:actor
								}
							}
						}
					}
				}
				scope:recipient.capital_province = { squared_distance = { target = scope:actor.capital_province value < 100000 } }
			}
			add = -25
		}
		modifier = { #Distant Realm
			desc = offer_vassalization_interaction_aibehavior_distantrealm_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							OR = {
								this = scope:actor
								AND = {
									exists = scope:actor.confederation
									is_member_of_confederation = scope:actor.confederation
								}
								AND = {
									exists = scope:actor.confederation
									suzerain ?= {
										is_member_of_confederation = scope:actor.confederation
									}
								}
								suzerain ?= {
									this = scope:actor
								}
							}
						}
					}
				}
				scope:recipient.capital_province = { squared_distance = { target = scope:actor.capital_province value >= 100000 } }
			}
			add = -100
		}
		modifier = { #Remote Realm.
			desc = offer_vassalization_interaction_aibehavior_remoterealm_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							OR = {
								this = scope:actor
								AND = {
									exists = scope:actor.confederation
									is_member_of_confederation = scope:actor.confederation
								}
								AND = {
									exists = scope:actor.confederation
									suzerain ?= {
										is_member_of_confederation = scope:actor.confederation
									}
								}
								suzerain ?= {
									this = scope:actor
								}
							}
						}
					}
				}
				scope:recipient.capital_province = { squared_distance = { target = scope:actor.capital_province value >= 200000 } }
			}
			add = -175
		}
		#Relative power
		modifier = {
			desc = offer_vassalization_interaction_aibehavior_power_tt
	  	  	add = {
				value = 1
				subtract = {
					value = scope:recipient.max_military_strength # Intended for recipient to use max, to avoid having vassalizations become too easy for weakened realms
					divide = { value = scope:actor.top_liege.current_military_strength min = 1 }
				}
				multiply = 5
				ceiling = yes
				min = -100
	  		}
		}
		#Powerful vassal opinion of you
		modifier = {
			desc = offer_confederation_offerer_vassal_opinion_tt
			trigger = {
				scope:actor.top_liege = {
				number_of_powerful_vassals >= 1
				}
			}

	  	  	add = {
				value = 0
				scope:actor.top_liege = {
					every_powerful_vassal = {
						if = {
							limit = {
								save_temporary_opinion_value_as = {
									name = vassal_opinion
									target = scope:actor.top_liege
								}
							}
							add = scope:vassal_opinion
						}
					}

					if = {
						limit = {
							number_of_powerful_vassals > 0
						}
						divide = number_of_powerful_vassals
					}
					else = {
						divide = 5
					}
				}
				divide = 10
	  		}
		}

		# Legitimacy
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = very_reduced_tributarization_acceptance
			}
			add = -20
		}
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = reduced_tributarization_acceptance
			}
			add = -10
		}
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = increased_tributarization_acceptance
			}
			add = 10
		}
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = very_increased_tributarization_acceptance
			}
			add = 20
		}

		# MINOR
		modifier = { #Friend modifier.
			desc = offer_vassalization_interaction_aibehavior_friend_tt
			trigger = {
				scope:recipient = {
					has_relation_friend = scope:actor
					NOT = { has_relation_best_friend = scope:actor }
				}
			}
			add = 25
		}
		modifier = { #Best Friend modifier.
			desc = offer_vassalization_interaction_aibehavior_best_friend_tt
			trigger = {
				scope:recipient = {
					has_relation_best_friend = scope:actor
				}
			}
			add = 50
		}
		modifier = { #Lover modifier.
			desc = interaction_lover
			trigger = {
				scope:recipient = {
					has_relation_lover = scope:actor
					NOT = { has_relation_soulmate = scope:actor }
				}
			}
			add = 25
		}
		modifier = { #Soulmate modifier.
			desc = interaction_soulmate
			trigger = {
				scope:recipient = {
					has_relation_soulmate = scope:actor
				}
			}
			add = 50
		}
		modifier = { #Rivalry modifier.
			desc = offer_vassalization_interaction_aibehavior_rival_tt
			trigger = {
				scope:recipient = {
					has_relation_rival = scope:actor
					NOT = { has_relation_nemesis = scope:actor }
				}
			}
			add = -200
		}
		modifier = { #Nemesis modifier.
			desc = offer_vassalization_interaction_aibehavior_nemesis_tt
			trigger = {
				scope:recipient = {
					has_relation_nemesis = scope:actor
				}
			}
			add = -1000
		}

		modifier = { #Rivalry modifier.
			desc = offer_vassalization_interaction_aibehavior_rival_confederation_tt
			trigger = {
				scope:actor.confederation ?= {
					any_confederation_member = {
						NOT = { this = scope:actor }
						has_relation_rival = scope:recipient
						NOT = { has_relation_nemesis = scope:recipient }
					}
				}
			}
			add = -100
		}
		modifier = { #Nemesis modifier.
			desc = offer_vassalization_interaction_aibehavior_nemesis_confederation_tt
			trigger = {
				scope:actor.confederation ?= {
					any_confederation_member = {
						NOT = { this = scope:actor }
						has_relation_nemesis = scope:recipient
					}
				}
			}
			add = -500
		}
		modifier = { #Same Dynasty modifier.
			desc = offer_vassalization_interaction_aibehavior_dynasty_tt
			trigger = {
				exists = scope:actor.dynasty
				exists = scope:recipient.dynasty
				scope:recipient = {
					dynasty = scope:actor.dynasty
				}
			}
			add = 25
		}
		modifier = { #Same House modifier.
			desc = offer_vassalization_interaction_aibehavior_house_tt
			trigger = {
				exists = scope:actor.house
				exists = scope:recipient.house
				scope:recipient = {
					house = scope:actor.house
				}
			}
			add = 50
		}

		modifier = { # Same language
			add = 20
			desc = speaks_same_language_interaction_reason
			trigger = {
				scope:actor = {
					knows_language_of_culture = scope:recipient.culture
				}
			}
		}

		modifier = { # Iberian Struggle, less likely for outsiders to vassalize inside
			add = -35
			desc = iberian_struggle_reason_reason
			trigger = {
				scope:actor = {
					NOT = {
						any_character_struggle = { is_struggle_type = iberian_struggle }
					}
				}
				scope:recipient = {
					any_character_struggle = { is_struggle_type = iberian_struggle }
				}
			}
		}

		modifier = { #Ageism modifier vs kids.
			desc = offer_vassalization_interaction_aibehavior_child_tt
			trigger = {
				scope:actor = {
					age < 12
				}
				scope:recipient = {
					age > 16
				}
			}
			add = -5
		}
		modifier = { #Illegitimacy modifier.
			desc = confederation_offerer_illegitimate_tt
			trigger = {
				scope:actor.top_liege = {
					OR = {
						AND = {
							has_trait = bastard
							scope:recipient = {
								faith = { NOT = { has_doctrine = doctrine_bastardry_none } }
							}
						}
						has_trait = denounced
						has_trait = disinherited
					}
				}
			}
			add = -20
		}

		modifier = { #Claimant modifier.
			desc = confederation_interaction_aibehavior_claimant_tt
			trigger = {
				scope:actor.top_liege.primary_title = {
					scope:recipient = {
						has_claim_on = prev
					}
				}
			}
			add = -20
		}

		modifier = { # Ambitious
			desc = TAKE_THE_VOWS_AMBITIOUS
			trigger = {
				scope:recipient = {
					has_trait = ambitious
				}
			}
			add = -25
		}

		modifier = { # Paranoid
			desc = INTERACTION_PARANOID
			trigger = {
				scope:recipient = {
					has_trait = paranoid
				}
			}
			add = -25
		}

		modifier = { # Arrogant
			desc = INTERACTION_ARROGANT
			trigger = {
				scope:recipient = {
					has_trait = arrogant
				}
			}
			add = -25
		}

		modifier = { # Fickle
			desc = INTERACTION_FICKLE
			trigger = {
				scope:recipient = {
					has_trait = fickle
				}
			}
			add = -15
		}

		modifier = { # Stubborn
			desc = INTERACTION_STUBBORN
			trigger = {
				scope:recipient = {
					has_trait = stubborn
				}
			}
			add = -15
		}

		modifier = { # Greedy
			desc = INTERACTION_GREEDY
			trigger = {
				scope:recipient = {
					has_trait = greedy
				}
			}
			add = -15
		}

		modifier = { # Trusting
			desc = TAKE_THE_VOWS_TRUSTING
			trigger = {
				scope:recipient = {
					has_trait = trusting
				}
			}
			add = 15
		}

		modifier = { # Content
			desc = INTERACTION_CONTENT
			trigger = {
				scope:recipient = {
					has_trait = content
				}
			}
			add = 15
		}

		modifier = { # Craven
			desc = INTERACTION_CRAVEN
			trigger = {
				scope:recipient = {
					has_trait = craven
				}
			}
			add = 25
		}

		modifier = { # FP3 modifier.
			desc = GENERIC_YOUR_PIETY_LEVEL_MODIFIER
			trigger = { scope:actor = { any_character_struggle = { has_struggle_phase_parameter = piety_level_affects_vassalage_acceptance } } }
			add = {
				value = {
					value = scope:actor.piety_level
					subtract = low_piety_level
				}
				multiply = 10
			}
		}

		# OPINION INFLUENCE
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		opinion_modifier = { #Compare Opinion modifier.
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.35
		}

		# DIPLOMATIC COURT GRANDEUR BONUS
		modifier = {
			trigger = {
				scope:actor.top_liege = {
					has_royal_court = yes
					has_dlc_feature = royal_court
					has_court_type = court_diplomatic
					court_grandeur_current_level >= 1
				}
			}
			add = {
				value = scope:actor.top_liege.court_grandeur_current
				if = {
					limit = { # Reduce the bonus if you are below your expected level
						scope:actor.top_liege = {
							court_grandeur_current_level < court_grandeur_minimum_expected_level
						}
					}
					multiply = 0.15
				}
				else = {
					multiply = 0.3
				}
			}
			desc = DIPLOMATIC_COURT_ACCEPTANCE_INCREASE_REASON
		}

		# LOW LEGITIMACY
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			add = -10
			scope:actor = {
				has_legitimacy_flag = reduced_confederation_acceptance
			}
		}
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			add = -25
			scope:actor = {
				has_legitimacy_flag = very_reduced_confederation_acceptance
			}
		}

		# HIGH LEGITIMACY
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			add = 15
			scope:actor = {
				has_legitimacy_flag = increased_confederation_acceptance
			}
		}
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			add = 40
			scope:actor = {
				has_legitimacy_flag = very_increased_confederation_acceptance
			}
		}

		# HERD
		modifier = {
			add = 40
			scope:herd_send_option = yes
			desc = HERD_INTERACTION_ACCEPTANCE_SEND_OPTION
		}

		# PRESTIGE
		modifier = {
			add = 40
			scope:prestige_send_option = yes
			desc = PRESTIGE_INTERACTION_ACCEPTANCE_SEND_OPTION
		}

		#CULTURE
		modifier = {
			add = 50
			scope:actor = {
				culture = scope:recipient.culture
			}
			desc = "SAME_CULTURE_REASON"
		}
		modifier = {
			add = -20
			NOT = {
				scope:actor = {
					culture = scope:recipient.culture
				}
			}
			scope:actor = {
				NOR = {
					culture = {
						any_parent_culture_or_above = {
							this = scope:recipient.culture
						}
					}
					scope:recipient.culture = {
						any_parent_culture_or_above = {
							this = scope:actor.culture
						}
					}
					culture = {
						has_same_culture_heritage = scope:recipient.culture
					}
				}
			}
			desc = "DIFFERENT_CULTURE_REASON"
		}

		#Conquerors have no interest in this
		modifier = {
			scope:recipient = {
				has_trait = conqueror
			}
			add = -1000
		}
		modifier = {
			scope:recipient = {
				has_trait = greatest_of_khans
			}
			add = -1000
		}

		# Are you using a hook?
		modifier = {
			trigger = {
				scope:hook ?= yes
			}
			add = 100
			desc = LEGEND_HOOK_USED
		}

		#No neighbor scaring them
		modifier = {
			add = -30
			desc = NO_FRIGHTENING_NEIGHBOR_REASON
			scope:actor.culture = {
				NOT = {
					has_cultural_parameter = forming_confederations_is_easier
				}
			}
			scope:recipient.culture = {
				NOT = {
					has_cultural_parameter = forming_confederations_is_easier
				}
			}
			NOT = {
				scope:recipient = {
					confederation_neighboring_foe_trigger = { CHARACTER = scope:recipient }
				}
			}
		}

		#Neighbor is TERRIFYING them
		modifier = {
			add = 30
			desc = TERRIFYING_NEIGHBOR_REASON
			scope:recipient = {
				save_temporary_scope_as = confederate
				any_land_neighboring_realm_with_tributaries_owner = {
					top_suzerain ?= {
						is_confederation_member = no
						NOR = {
							this = scope:actor
							this = scope:actor.top_liege
						}
						confederation_worthy_foe_strength_ratio_value <= 0.25
						NOT = { is_allied_to = scope:recipient }
						OR = {
							highest_held_title_tier >= tier_kingdom
							faith = {
								faith_hostility_level = {
									target = scope:recipient.faith
									value >= faith_evil_level
								}
							}
							has_trait = conqueror
							has_trait = greatest_of_khans
							any_owned_story = {
								OR = {
									story_type = story_greatest_of_khans
									story_type = story_mongol_invasion
								}
							}
						}
					}
				}
			}
		}

		#You have too many confederates
		#For duchy
		modifier = {
			add = duchy_confederation_vassals_value
			desc = TOO_MANY_CONFEDERATION_VASSALS_REASON
			scope:actor.confederation ?= {
				any_confederation_member = {
					count >= 6
					highest_held_title_tier >= tier_county
				}
			}
		}

		modifier = {
			add = -100
			desc = ANOTHER_CULTURE_LEAGUE_REASON
			NOT = {
				scope:actor.culture = {
					this = scope:recipient.culture
				}
			}
			any_confederation = {
				confederation_type = confederation_type:TMA_nato_type
				NOT = {
					this = scope:actor.confederation
				}
				has_variable = confederation_culture
				var:confederation_culture = scope:recipient.culture 
				scope:recipient = { TMA_nato_goverments_allowed = yes }
			}
		}

		#Another confederation of recipient's faith exists
		modifier = {
			add = -100
			desc = ANOTHER_FAITH_LEAGUE_REASON
			NOT = {
				scope:actor.faith = {
					this = scope:recipient.faith
				}
			}
			any_confederation = {
				confederation_type = confederation_type:TMA_nato_type
				NOT = {
					this = scope:actor.confederation
				}
				has_variable = confederation_faith
				var:confederation_faith = scope:recipient.faith
				scope:recipient = { TMA_nato_goverments_allowed = yes }
			}
		}

		#Forcing them to join war
		modifier = {
			add = -100
			desc = FORCED_TO_JOIN_WAR_REASON
			scope:actor = {
				is_at_war_as_defender = yes
				any_character_war = {
					primary_defender = {
						OR = {
							this = scope:actor
							AND = {
								exists = scope:actor.confederation
								is_member_of_confederation = scope:actor.confederation
							}
						}
					}
					is_defender = scope:actor
					NOR = {
						any_war_attacker = {
							is_allied_to = scope:recipient
						}
						any_war_attacker = {
							this = scope:recipient
						}
						any_war_defender = {
							this = scope:recipient
						}
						any_war_attacker = {
							scope:recipient = {
								has_truce = prev
							}
						}
					}
				}
			}
		}

		#You will be taking over their war
		modifier = {
			add = 100
			desc = TAKING_OVER_DEFENSIVE_WAR_REASON
			scope:recipient = {
				is_at_war_as_defender = yes
				any_character_war = {
					is_war_leader = scope:recipient
					is_defender = scope:recipient
					NOR = {
						any_war_attacker = {
							is_allied_to = scope:actor
						}
						any_war_attacker = {
							this = scope:actor
						}
						any_war_defender = {
							this = scope:actor
						}
					}
					defender_war_score < 80
					defender_war_score >= 0
				}
			}
		}
		modifier = {
			add = 200
			desc = TAKING_OVER_DEFENSIVE_WAR_REASON
			scope:recipient = {
				is_at_war_as_defender = yes
				any_character_war = {
					is_war_leader = scope:recipient
					is_defender = scope:recipient
					NOR = {
						any_war_attacker = {
							is_allied_to = scope:actor
						}
						any_war_attacker = {
							this = scope:actor
						}
						any_war_defender = {
							this = scope:actor
						}
					}
					defender_war_score < 0
				}
			}
		}

		#Culture is into/not into confederation
		modifier = {
			add = 10
			desc = CONFEDERATION_ETHOS_REASON
			scope:recipient.culture = {
				OR = {
					has_cultural_pillar = ethos_stoic
					has_cultural_pillar = ethos_communal
				}
			}
		}
		modifier = {
			add = 20
			desc = CONFEDERATION_ETHOS_REASON
			scope:recipient.culture = {
				OR = {
					has_cultural_pillar = ethos_egalitarian
				}
			}
		}
		modifier = {
			add = -10
			desc = CONFEDERATION_ETHOS_REASON
			scope:recipient.culture = {
				OR = {
					has_cultural_pillar = ethos_bellicose
					has_cultural_pillar = ethos_courtly
					has_cultural_pillar = ethos_bureaucratic
				}
			}
		}

		modifier = { # Herder
			add = 50
			desc = CONFEDERATION_HERDER_REASON
			trigger = {
				scope:recipient = {
					government_has_flag = government_is_herder
				}
			}
		}
		modifier = {
			add = -20
			desc = NOMADIC_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = nomadic_authority_3
				}
			}
		}
		modifier = {
			add = -50
			desc = NOMADIC_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = nomadic_authority_4
				}
			}
		}
		modifier = {
			add = -100
			desc = NOMADIC_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = nomadic_authority_5
				}
			}
		}
		modifier = {
			add = -20
			desc = TRIBAL_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = tribal_authority_2
				}
			}
		}
		modifier = {
			add = -50
			desc = TRIBAL_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = tribal_authority_3
				}
			}
		}

		modifier = {
			add = -20
			desc = CROWN_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = crown_authority_2
				}
			}
		}
		modifier = {
			add = -50
			desc = CROWN_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = crown_authority_3
				}
			}
		}

		modifier = {
			desc = "BARANGAY_TRADITION_REASON"
			trigger = {
				scope:recipient.culture = {
					has_cultural_parameter = invite_to_confederation_extra_reasons
				}
			}
			add = 20
		}
		modifier = { #left your confederation
			add = -50
			desc = LEFT_YOUR_CONFEDERATION_REASON 
			exists = scope:actor.confederation
			scope:recipient = {
				has_variable = left_confederation
				var:left_confederation ?= {
					this = scope:actor.confederation
				}
			}
		}
	}

	send_option = {
		flag = hook
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		localization = GENERIC_SPEND_A_HOOK
	}

	send_option = { # Herd
		is_shown = {
			scope:actor = {
				government_has_flag = government_is_nomadic
				exists = domicile
			}
		}
		is_valid = {
			scope:recipient = {
				government_has_flag = government_is_nomadic
				exists = domicile
			}
			scope:actor.domicile = { herd >= minor_herd_value }
		}
		flag = herd_send_option
		localization = TRADE_HERD_FOR_BETTER_AI_ACCEPTANCE_CONFEDERATION
	}

	send_option = { # Prestige
		is_shown = {
			always = yes
		}
		is_valid = {
			scope:actor = { prestige >= minor_prestige_value }
		}
		flag = prestige_send_option
		localization = TRADE_PRESTIGE_FOR_BETTER_AI_ACCEPTANCE
	}

	send_options_exclusive = no

	on_accept = {
		if = {
			#checking that the delay hasn't made the character invalid
			limit = {
				scope:recipient = {
					is_playable_character = yes
				}
			}
			scope:actor = {
				if = {
					limit = { has_character_flag = forming_confederation }
					remove_character_flag = forming_confederation
				}
			}
			if = {
				limit = {
					scope:recipient = {
						is_at_war_as_defender = yes
						any_character_war = {
							is_war_leader = scope:recipient
							is_defender = scope:recipient
						}
					}
				}
				scope:recipient = {
					every_character_war = {
						limit = {
							is_war_leader = scope:recipient
							is_defender = scope:recipient
						}
						add_to_list = recipient_wars
					}
				}
				every_in_list = {
					list = recipient_wars
					save_scope_as = recipient_war
					primary_attacker = {
						save_scope_as = attacker
					}
					add_defender = scope:actor
					hidden_effect = {
						scope:actor.confederation ?= {
							every_confederation_member = {
								limit = {
									NOR = {
										is_attacker_in_war = scope:recipient_war
										is_defender_in_war = scope:recipient_war
										is_at_war_with = scope:recipient
										is_allied_to = scope:attacker
										is_imprisoned_by = scope:recipient
										is_at_war_with = scope:recipient
									}
								}
								scope:recipient_war = { add_defender = prev }
							}
						}
					}
				}
				if = {
					limit = {
						scope:actor.confederation ?= {
							any_confederation_member = {
								NOR = {
									is_attacker_in_war = scope:recipient_war
									is_defender_in_war = scope:recipient_war
									this = scope:actor
									is_at_war_with = scope:recipient
									is_allied_to = scope:attacker
									is_imprisoned_by = scope:recipient
									is_at_war_with = scope:recipient
								}
							}
						}
					}
					scope:actor = {
						custom_tooltip = league_members_joining_recipient_war_tt
					}
				}
			}
			scope:actor = {
				save_scope_as = confederation_offerer
			}
			scope:recipient = {
				save_scope_as = confederation_accepter
			}
			#Confederation doesn't exist yet
			if = {
				limit = {
					scope:actor = {
						is_confederation_member = no
					}
				}
				scope:actor = {
					custom_tooltip = create_league_tt
					custom_tooltip = league_defensive_wars_tt
					custom_tooltip = unlock_leave_league_interaction_tt
					if = {
						limit = {
							government_has_flag = government_is_nomadic
						}
						add_character_modifier = {
							modifier = mpo_confederation_member_modifier
							years = 5
						}
						capital_county ?= {
							change_county_fertility = major_county_fertility_level_gain
						}
					}
				}
				scope:recipient = {
					offer_defensive_league_accepter_effect = yes
				}
			}
			#Confederation already exists
			else = {
				scope:actor.confederation = {
					save_scope_as = confederation
				}
				show_as_tooltip = {
					#If actor is top liege and offering vassaldom
					scope:recipient = {
						offer_defensive_league_accepter_effect = yes 
					}
				}
			}
			if = {
				limit = {
					scope:recipient = {
						government_has_flag = government_is_nomadic
					}
				}
				scope:recipient = {
					add_character_modifier = {
						modifier = mpo_confederation_member_modifier
						years = 5
					}
					capital_county ?= {
						change_county_fertility = major_county_fertility_level_gain
					}
				}
			}
			if = {
				limit = {
					scope:recipient = {
						is_ai = no
					}
				}
				scope:recipient = {
					custom_tooltip = nato_raiding_attacking_tt
					if = {
						limit = {
							realm_law_use_nomadic_authority = yes
						}
						custom_tooltip = confederation_restrictions_warning_tt
					}
					else_if = {
						limit = {
							realm_law_use_tribal_authority = yes
						}
						custom_tooltip = confederation_restrictions_tribe_warning_tt
					}
					else_if = {
						limit = {
							realm_law_use_crown_authority = yes
						}
						custom_tooltip = nato_restrictions_crown_authority_warning_tt
					}
				}
			}
			scope:recipient = {
				if = {
					limit = {
						government_has_flag = government_is_nomadic
					}
					custom_tooltip = confederation_migrating_leaving_warning_tt
				}
			}
			scope:actor = {
				save_scope_value_as = {
					name = new_TMA_type
					value = flag:defensive_league
				}
				trigger_event = mpo_interactions_events.0001
			}
			if = {
				limit = {
					scope:actor = {
						is_at_war_as_defender = yes
						any_character_war = {
							primary_defender = {
								OR = {
									this = scope:actor
									AND = {
										exists = scope:actor.confederation
										is_member_of_confederation = scope:actor.confederation
									}
								}
							}
							is_defender = scope:actor
							NOR = {
								any_war_attacker = {
									is_allied_to = scope:recipient
								}
								any_war_attacker = {
									this = scope:recipient
								}
								any_war_defender = {
									this = scope:recipient
								}
								any_war_attacker = {
									scope:recipient = {
										has_truce = prev
									}
								}
							}
						}
					}
				}
				scope:actor = {
					every_character_war = {
						limit = {
							primary_defender = {
								OR = {
									this = scope:actor
									AND = {
										exists = scope:actor.confederation
										is_member_of_confederation = scope:actor.confederation
									}
								}
							}
							is_defender = scope:actor
							NOR = {
								any_war_attacker = {
									is_allied_to = scope:recipient
								}
								any_war_attacker = {
									this = scope:recipient
								}
								any_war_defender = {
									this = scope:recipient
								}
								any_war_attacker = {
									scope:recipient = {
										has_truce = prev
									}
								}
							}
						}
						add_defender = scope:recipient
					}
					#Lose legitimacy unless you're taking on a war too
					if = {
						limit = {
							NOT = {
								scope:recipient = {
									is_at_war_as_defender = yes
									any_character_war = {
										is_war_leader = scope:recipient
										is_defender = scope:recipient
										NOR = {
											any_war_attacker = {
												is_allied_to = scope:actor
											}
											any_war_attacker = {
												this = scope:actor
											}
											any_war_attacker = {
												scope:actor = {
													has_truce = prev
												}
											}
										}
									}
								}
							}
						}
						add_legitimacy = medium_legitimacy_loss
						custom_tooltip = confederation_legitimacy_loss_war_tt
					}
				}
			}
			if = {
				limit = {
					scope:prestige_send_option = yes
				}
				scope:recipient = {
					add_prestige = scope:actor.minor_prestige_value
				}
			}
			if = {
				limit = {
					scope:herd_send_option = yes
				}
				scope:actor = {
					pay_herd = {
						target = scope:recipient
						value = domicile.minor_herd_value
					}
				}
			}
		}
	}

	on_decline = {
		#Stop spamming players with this
		if = {
			limit = {
				exists = scope:actor.confederation
				scope:recipient = {
					is_ai = no
				}
			}
			scope:recipient = {
				set_variable = {
					name = refused_confederation
					value = scope:actor.confederation
					years = 3
				}
			}
		}
		scope:actor = {
			#letter response
			trigger_event = mpo_interactions_events.0004
		}
	}

	ai_potential = {
		age >= 6
		OR = {
			AND = {
				is_independent_ruler = yes
				TMA_has_appropriate_title_tier = yes
				has_character_flag = forming_confederation
			}
			is_confederation_member = yes
		}
		TMA_nato_goverments_allowed = yes
	}

	ai_will_do = {
		base = 50

		modifier = {
			add = 25
			scope:actor = {
				confederation_neighboring_foe_trigger = { CHARACTER = scope:actor }
			}
		}
		modifier = {
			add = 25
			scope:recipient = {
				confederation_neighboring_foe_trigger = { CHARACTER = scope:recipient }
			}
		}
		modifier = {
			add = -25
			scope:recipient = {
				is_at_war = yes
			}
		}
		modifier = {
			factor = 0
			OR = {
				#Will only throw the confederation into a war
				scope:recipient = {
					is_at_war = yes
					NOR = {
						any_character_war = { primary_defender = scope:recipient }
						culture = scope:actor.culture
						scope:actor.confederation ?= {
							any_confederation_member = {
								count >= 4
								highest_held_title_tier >= tier_county
							}
						}
						reverse_opinion = {
							target = scope:actor
							value >= 30
						}
					}
				}
				AND = {
					exists = scope:actor.confederation
					scope:recipient = {
						has_variable = left_confederation
						var:left_confederation ?= {
							this = scope:actor.confederation
						}
					}
				}
				#Recipient hasn't been asked to join this confederation already
				scope:recipient = {
					has_variable = refused_confederation
					exists = scope:actor.confederation
					var:refused_confederation = {
						this = scope:actor.confederation
					}
				}
			}
		}
	}
}
offer_csto_interaction = {
	category = interaction_category_diplomacy
	icon = TMA_csto_interaction
	
	desc = offer_csto_interaction_desc

	ai_targets = {
		ai_recipients = neighboring_rulers_including_tributary_borders
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 8
		duchy = 8
		kingdom = 8 
		empire = 8
		hegemony = 8
	}
	popup_on_receive = yes
	pause_on_receive = yes

	common_interaction = yes

	cooldown_against_recipient = { years = 3 }

	is_shown = {
		scope:actor = {
			TMA_csto_goverments_allowed = yes
			is_playable_character = yes
			OR = {
				is_independent_ruler = yes
				confederation ?= { is_house_based = no }
			}
			confederation = {has_flag = TMA_csto}
		}
		scope:recipient = {
			is_playable_character = yes
			is_independent_ruler = yes
			TMA_csto_goverments_allowed = yes
			NOT = { this = scope:actor }
			NOT = {is_confederation_member = yes}
		}
	}

	is_valid_showing_failures_only = {
		trigger_if = {
			limit = {
				scope:actor = { is_confederation_member = no }
			}
			custom_tooltip = {
				text = forming_csto_tt 
				scope:actor = {
					has_character_flag = forming_confederation
				}
			}
		}
		scope:actor = {
			is_imprisoned = no
		}
		scope:recipient = {
			valid_defensive_league_member_trigger = { CHARACTER = scope:actor } 
			is_imprisoned = no
			is_at_war = no
		}
	}

	cost = {
		prestige = {
			value = 0
			if = {
				limit = { scope:prestige_send_option = yes }
				add = scope:actor.minor_prestige_value
				desc = PRESTIGE_INTERACTION_ACCEPTANCE_SEND_OPTION
			}
		}
	}

	greeting = positive
	notification_text = OFFER_CONFEDERATION_INTERACTION_NOTIFICATION

	ai_accept = {
		base = -50
		
		# PERKS
		modifier = { # Perk boost
			desc = offer_vassalization_true_ruler_perk_tt
			trigger = {
				scope:actor = {
					has_perk = true_ruler_perk
					TMA_confederations = yes 
				}
			}
			add = true_ruler_value
		}
		modifier = { # Education 5 boost
			desc = offer_vassalization_education_diplomacy_5_tt
			trigger = {
				scope:actor = { has_trait_with_flag = offer_vassalisation_25 }
			}
			add = 25
		}

		# EVENTS - temporary bonuses gained by events
		modifier = {
			desc = event_bonus_to_vassal_accept_tt
			trigger = {
				scope:actor = { has_character_modifier = event_bonus_to_vassal_accept }
			}
			add = 20
		}


		# STRUGGLES - bonus gained by successful Sway scheme during the Persian Struggle
		modifier = {
			desc = fp3_persian_struggle_previously_swayed_tt
			trigger = {
				scope:recipient = {
					has_opinion_modifier = {
						modifier = scheme_sway_and_compelled_to_submit_opinion
						target = scope:actor
					}
				}
			}
			add = 20
		}

		modifier = {
			desc = fp3_rekindler_of_iran_modifier_reason
			trigger = {
				AND = {
					scope:actor = { dynasty ?={ has_dynasty_modifier = fp3_rekindler_of_iran_modifier } }
					scope:recipient = { culture = { has_cultural_pillar = heritage_iranian } }
				}
			}
			add = 20
		}


		modifier = { # Cultural Acceptance
			add = offer_vassalage_acceptance_value
			desc = cultural_acceptance_interaction_reason
			trigger = {
				scope:actor = {
					NOT = { has_same_culture_as = scope:recipient }
					culture = {
						cultural_acceptance = { target = scope:recipient.culture value <= 90 }
					}
				}
			}
		}

		# MAIN
		modifier = { #Different faith, no pluralism.
			desc = offer_vassalization_interaction_aibehavior_differentfaith_tt
			trigger = {
				scope:recipient = {
					NOR = { #Of two different faiths AND the potential vassal's faith is not pluralistic.
						faith = scope:actor.faith
						faith = { has_doctrine = doctrine_pluralism_pluralistic }
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_herder
					}
				}
			}
			add = {
				value = -30
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_hostile_level
							}
						}
					}
					add = -30
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_evil_level
							}
						}
					}
					add = -30
				}
			}
		}

		modifier = { #Different faith, pluralism.
			desc = offer_vassalization_interaction_aibehavior_differentfaith_tt
			trigger = {
				scope:recipient = {
					NOR = {
						faith = scope:actor.faith
						scope:actor.faith = { has_doctrine = doctrine_pluralism_pluralistic }
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_herder
					}
					faith = { has_doctrine = doctrine_pluralism_pluralistic }
				}
			}
			add = {
				value = -15
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_hostile_level
							}
						}
					}
					add = -15
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_evil_level
							}
						}
					}
					add = -15
				}
			}
		}

		modifier = { #Different faith, both have pluralism.
			desc = offer_vassalization_interaction_aibehavior_differentfaith_tt
			trigger = {
				scope:recipient = {
					NOR = {
						faith = scope:actor.faith
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_herder
					}
					scope:actor.faith = { has_doctrine = doctrine_pluralism_pluralistic }
					faith = { has_doctrine = doctrine_pluralism_pluralistic }
				}
			}
			add = {
				value = -10
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_hostile_level
							}
						}
					}
					add = -10
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_evil_level
							}
						}
					}
					add = -10
				}
			}
		}
		modifier = { #We just fought against each other.
			desc = offer_vassalization_interaction_aibehavior_recent_war_tt
			trigger = {
				scope:recipient = {
					any_truce_holder = {
						this = scope:actor
					}
				}
				# Ensure the truce wasn't purchased and is indeed from a war
				scope:actor = {
					NOT = {
						has_purchased_truce_with_char = { TARGET = scope:recipient }
					}
				}
			}
			add = -40
		}
		modifier = { #I fought an independence war against you.
			desc = offer_confederation_independence_war_tt
			trigger = {
				scope:recipient = {
					exists = var:independence_war_former_liege
					var:independence_war_former_liege = scope:actor.top_liege
				}
			}
			add = -100
		}
		modifier = { # Isolationist tradition
			desc = isolationist_reason
			trigger = {
				NOT = {
					scope:actor.culture = scope:recipient.culture
				}
				scope:recipient.culture = {
					has_cultural_tradition = tradition_isolationist
				}
			}
			add = -50
		}
		modifier = { #Bankrupt
			desc = bankrupt_reason
			trigger = {
				scope:actor.gold <= -1
			}
			add = -30
		}
		modifier = { #Wide difference in rank
			desc = offer_vassalization_interaction_aibehavior_widetitletier_tt
			trigger = {
				scope:actor = {
					tier_difference = {
						target = scope:recipient
						value > 1
					}
				}
			}
			add = 10
		}
		modifier = { # Allied
			desc = offer_vassalization_interaction_aibehavior_allied_tt
			trigger = {
				scope:recipient = {
					is_allied_to = scope:actor
				}
			}
			add = 50
		}
		modifier = { # Is the Rightful Liege of recipient
			desc = offer_vassalization_interaction_aibehavior_rightfulliegetitleholder_tt
			trigger = {
				scope:actor = { is_rightful_liege_of = scope:recipient }
			}
			add = 20
		}
		modifier = { # Encircled
			desc = offer_vassalization_interaction_aibehavior_encircled_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							NOT = {
								this = scope:actor
							}
						}
					}
					NOT = {
						any_realm_county = {
							is_coastal_county = yes
						}
					}
				}
			}
			add = 40
		}
		modifier = { #No adjacency
			desc = offer_vassalization_interaction_aibehavior_unconnectedrealm_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							OR = {
								this = scope:actor
								AND = {
									exists = scope:actor.confederation
									is_member_of_confederation = scope:actor.confederation
								}
								AND = {
									exists = scope:actor.confederation
									suzerain ?= {
										is_member_of_confederation = scope:actor.confederation
									}
								}
								suzerain ?= {
									this = scope:actor
								}
							}
						}
					}
				}
				scope:recipient.capital_province = { squared_distance = { target = scope:actor.capital_province value < 100000 } }
			}
			add = -25
		}
		modifier = { #Distant Realm
			desc = offer_vassalization_interaction_aibehavior_distantrealm_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							OR = {
								this = scope:actor
								AND = {
									exists = scope:actor.confederation
									is_member_of_confederation = scope:actor.confederation
								}
								AND = {
									exists = scope:actor.confederation
									suzerain ?= {
										is_member_of_confederation = scope:actor.confederation
									}
								}
								suzerain ?= {
									this = scope:actor
								}
							}
						}
					}
				}
				scope:recipient.capital_province = { squared_distance = { target = scope:actor.capital_province value >= 100000 } }
			}
			add = -100
		}
		modifier = { #Remote Realm.
			desc = offer_vassalization_interaction_aibehavior_remoterealm_tt
			trigger = {
				scope:recipient = {
					NOT = {
						any_land_neighboring_realm_with_tributaries_owner = {
							OR = {
								this = scope:actor
								AND = {
									exists = scope:actor.confederation
									is_member_of_confederation = scope:actor.confederation
								}
								AND = {
									exists = scope:actor.confederation
									suzerain ?= {
										is_member_of_confederation = scope:actor.confederation
									}
								}
								suzerain ?= {
									this = scope:actor
								}
							}
						}
					}
				}
				scope:recipient.capital_province = { squared_distance = { target = scope:actor.capital_province value >= 200000 } }
			}
			add = -175
		}
		#Relative power
		modifier = {
			desc = offer_vassalization_interaction_aibehavior_power_tt
	  	  	add = {
				value = 1
				subtract = {
					value = scope:recipient.max_military_strength # Intended for recipient to use max, to avoid having vassalizations become too easy for weakened realms
					divide = { value = scope:actor.top_liege.current_military_strength min = 1 }
				}
				multiply = 5
				ceiling = yes
				min = -100
	  		}
		}
		#Powerful vassal opinion of you
		modifier = {
			desc = offer_confederation_offerer_vassal_opinion_tt
			trigger = {
				scope:actor.top_liege = {
				number_of_powerful_vassals >= 1
				}
			}

	  	  	add = {
				value = 0
				scope:actor.top_liege = {
					every_powerful_vassal = {
						if = {
							limit = {
								save_temporary_opinion_value_as = {
									name = vassal_opinion
									target = scope:actor.top_liege
								}
							}
							add = scope:vassal_opinion
						}
					}

					if = {
						limit = {
							number_of_powerful_vassals > 0
						}
						divide = number_of_powerful_vassals
					}
					else = {
						divide = 5
					}
				}
				divide = 10
	  		}
		}

		# Legitimacy
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = very_reduced_tributarization_acceptance
			}
			add = -20
		}
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = reduced_tributarization_acceptance
			}
			add = -10
		}
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = increased_tributarization_acceptance
			}
			add = 10
		}
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			scope:actor = {
				has_legitimacy_flag = very_increased_tributarization_acceptance
			}
			add = 20
		}

		# MINOR
		modifier = { #Friend modifier.
			desc = offer_vassalization_interaction_aibehavior_friend_tt
			trigger = {
				scope:recipient = {
					has_relation_friend = scope:actor
					NOT = { has_relation_best_friend = scope:actor }
				}
			}
			add = 25
		}
		modifier = { #Best Friend modifier.
			desc = offer_vassalization_interaction_aibehavior_best_friend_tt
			trigger = {
				scope:recipient = {
					has_relation_best_friend = scope:actor
				}
			}
			add = 50
		}
		modifier = { #Lover modifier.
			desc = interaction_lover
			trigger = {
				scope:recipient = {
					has_relation_lover = scope:actor
					NOT = { has_relation_soulmate = scope:actor }
				}
			}
			add = 25
		}
		modifier = { #Soulmate modifier.
			desc = interaction_soulmate
			trigger = {
				scope:recipient = {
					has_relation_soulmate = scope:actor
				}
			}
			add = 50
		}
		modifier = { #Rivalry modifier.
			desc = offer_vassalization_interaction_aibehavior_rival_tt
			trigger = {
				scope:recipient = {
					has_relation_rival = scope:actor
					NOT = { has_relation_nemesis = scope:actor }
				}
			}
			add = -200
		}
		modifier = { #Nemesis modifier.
			desc = offer_vassalization_interaction_aibehavior_nemesis_tt
			trigger = {
				scope:recipient = {
					has_relation_nemesis = scope:actor
				}
			}
			add = -1000
		}

		modifier = { #Rivalry modifier.
			desc = offer_vassalization_interaction_aibehavior_rival_confederation_tt
			trigger = {
				scope:actor.confederation ?= {
					any_confederation_member = {
						NOT = { this = scope:actor }
						has_relation_rival = scope:recipient
						NOT = { has_relation_nemesis = scope:recipient }
					}
				}
			}
			add = -100
		}
		modifier = { #Nemesis modifier.
			desc = offer_vassalization_interaction_aibehavior_nemesis_confederation_tt
			trigger = {
				scope:actor.confederation ?= {
					any_confederation_member = {
						NOT = { this = scope:actor }
						has_relation_nemesis = scope:recipient
					}
				}
			}
			add = -500
		}
		modifier = { #Same Dynasty modifier.
			desc = offer_vassalization_interaction_aibehavior_dynasty_tt
			trigger = {
				exists = scope:actor.dynasty
				exists = scope:recipient.dynasty
				scope:recipient = {
					dynasty = scope:actor.dynasty
				}
			}
			add = 25
		}
		modifier = { #Same House modifier.
			desc = offer_vassalization_interaction_aibehavior_house_tt
			trigger = {
				exists = scope:actor.house
				exists = scope:recipient.house
				scope:recipient = {
					house = scope:actor.house
				}
			}
			add = 50
		}

		modifier = { # Same language
			add = 20
			desc = speaks_same_language_interaction_reason
			trigger = {
				scope:actor = {
					knows_language_of_culture = scope:recipient.culture
				}
			}
		}

		modifier = { # Iberian Struggle, less likely for outsiders to vassalize inside
			add = -35
			desc = iberian_struggle_reason_reason
			trigger = {
				scope:actor = {
					NOT = {
						any_character_struggle = { is_struggle_type = iberian_struggle }
					}
				}
				scope:recipient = {
					any_character_struggle = { is_struggle_type = iberian_struggle }
				}
			}
		}

		modifier = { #Ageism modifier vs kids.
			desc = offer_vassalization_interaction_aibehavior_child_tt
			trigger = {
				scope:actor = {
					age < 12
				}
				scope:recipient = {
					age > 16
				}
			}
			add = -5
		}
		modifier = { #Illegitimacy modifier.
			desc = confederation_offerer_illegitimate_tt
			trigger = {
				scope:actor.top_liege = {
					OR = {
						AND = {
							has_trait = bastard
							scope:recipient = {
								faith = { NOT = { has_doctrine = doctrine_bastardry_none } }
							}
						}
						has_trait = denounced
						has_trait = disinherited
					}
				}
			}
			add = -20
		}

		modifier = { #Claimant modifier.
			desc = confederation_interaction_aibehavior_claimant_tt
			trigger = {
				scope:actor.top_liege.primary_title = {
					scope:recipient = {
						has_claim_on = prev
					}
				}
			}
			add = -20
		}

		modifier = { # Ambitious
			desc = TAKE_THE_VOWS_AMBITIOUS
			trigger = {
				scope:recipient = {
					has_trait = ambitious
				}
			}
			add = -25
		}

		modifier = { # Paranoid
			desc = INTERACTION_PARANOID
			trigger = {
				scope:recipient = {
					has_trait = paranoid
				}
			}
			add = -25
		}

		modifier = { # Arrogant
			desc = INTERACTION_ARROGANT
			trigger = {
				scope:recipient = {
					has_trait = arrogant
				}
			}
			add = -25
		}

		modifier = { # Fickle
			desc = INTERACTION_FICKLE
			trigger = {
				scope:recipient = {
					has_trait = fickle
				}
			}
			add = -15
		}

		modifier = { # Stubborn
			desc = INTERACTION_STUBBORN
			trigger = {
				scope:recipient = {
					has_trait = stubborn
				}
			}
			add = -15
		}

		modifier = { # Greedy
			desc = INTERACTION_GREEDY
			trigger = {
				scope:recipient = {
					has_trait = greedy
				}
			}
			add = -15
		}

		modifier = { # Trusting
			desc = TAKE_THE_VOWS_TRUSTING
			trigger = {
				scope:recipient = {
					has_trait = trusting
				}
			}
			add = 15
		}

		modifier = { # Content
			desc = INTERACTION_CONTENT
			trigger = {
				scope:recipient = {
					has_trait = content
				}
			}
			add = 15
		}

		modifier = { # Craven
			desc = INTERACTION_CRAVEN
			trigger = {
				scope:recipient = {
					has_trait = craven
				}
			}
			add = 25
		}

		modifier = { # FP3 modifier.
			desc = GENERIC_YOUR_PIETY_LEVEL_MODIFIER
			trigger = { scope:actor = { any_character_struggle = { has_struggle_phase_parameter = piety_level_affects_vassalage_acceptance } } }
			add = {
				value = {
					value = scope:actor.piety_level
					subtract = low_piety_level
				}
				multiply = 10
			}
		}

		# OPINION INFLUENCE
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		opinion_modifier = { #Compare Opinion modifier.
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.35
		}

		# DIPLOMATIC COURT GRANDEUR BONUS
		modifier = {
			trigger = {
				scope:actor.top_liege = {
					has_royal_court = yes
					has_dlc_feature = royal_court
					has_court_type = court_diplomatic
					court_grandeur_current_level >= 1
				}
			}
			add = {
				value = scope:actor.top_liege.court_grandeur_current
				if = {
					limit = { # Reduce the bonus if you are below your expected level
						scope:actor.top_liege = {
							court_grandeur_current_level < court_grandeur_minimum_expected_level
						}
					}
					multiply = 0.15
				}
				else = {
					multiply = 0.3
				}
			}
			desc = DIPLOMATIC_COURT_ACCEPTANCE_INCREASE_REASON
		}

		# LOW LEGITIMACY
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			add = -10
			scope:actor = {
				has_legitimacy_flag = reduced_confederation_acceptance
			}
		}
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			add = -25
			scope:actor = {
				has_legitimacy_flag = very_reduced_confederation_acceptance
			}
		}

		# HIGH LEGITIMACY
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			add = 15
			scope:actor = {
				has_legitimacy_flag = increased_confederation_acceptance
			}
		}
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			add = 40
			scope:actor = {
				has_legitimacy_flag = very_increased_confederation_acceptance
			}
		}

		# HERD
		modifier = {
			add = 40
			scope:herd_send_option = yes
			desc = HERD_INTERACTION_ACCEPTANCE_SEND_OPTION
		}

		# PRESTIGE
		modifier = {
			add = 40
			scope:prestige_send_option = yes
			desc = PRESTIGE_INTERACTION_ACCEPTANCE_SEND_OPTION
		}

		#CULTURE
		modifier = {
			add = 50
			scope:actor = {
				culture = scope:recipient.culture
			}
			desc = "SAME_CULTURE_REASON"
		}
		modifier = {
			add = -20
			NOT = {
				scope:actor = {
					culture = scope:recipient.culture
				}
			}
			scope:actor = {
				NOR = {
					culture = {
						any_parent_culture_or_above = {
							this = scope:recipient.culture
						}
					}
					scope:recipient.culture = {
						any_parent_culture_or_above = {
							this = scope:actor.culture
						}
					}
					culture = {
						has_same_culture_heritage = scope:recipient.culture
					}
				}
			}
			desc = "DIFFERENT_CULTURE_REASON"
		}

		#Conquerors have no interest in this
		modifier = {
			scope:recipient = {
				has_trait = conqueror
			}
			add = -1000
		}
		modifier = {
			scope:recipient = {
				has_trait = greatest_of_khans
			}
			add = -1000
		}

		# Are you using a hook?
		modifier = {
			trigger = {
				scope:hook ?= yes
			}
			add = 100
			desc = LEGEND_HOOK_USED
		}

		#No neighbor scaring them
		modifier = {
			add = -30
			desc = NO_FRIGHTENING_NEIGHBOR_REASON
			scope:actor.culture = {
				NOT = {
					has_cultural_parameter = forming_confederations_is_easier
				}
			}
			scope:recipient.culture = {
				NOT = {
					has_cultural_parameter = forming_confederations_is_easier
				}
			}
			NOT = {
				scope:recipient = {
					confederation_neighboring_foe_trigger = { CHARACTER = scope:recipient }
				}
			}
		}

		#Neighbor is TERRIFYING them
		modifier = {
			add = 30
			desc = TERRIFYING_NEIGHBOR_REASON
			scope:recipient = {
				save_temporary_scope_as = confederate
				any_land_neighboring_realm_with_tributaries_owner = {
					top_suzerain ?= {
						is_confederation_member = no
						NOR = {
							this = scope:actor
							this = scope:actor.top_liege
						}
						confederation_worthy_foe_strength_ratio_value <= 0.25
						NOT = { is_allied_to = scope:recipient }
						OR = {
							highest_held_title_tier >= tier_kingdom
							faith = {
								faith_hostility_level = {
									target = scope:recipient.faith
									value >= faith_evil_level
								}
							}
							has_trait = conqueror
							has_trait = greatest_of_khans
							any_owned_story = {
								OR = {
									story_type = story_greatest_of_khans
									story_type = story_mongol_invasion
								}
							}
						}
					}
				}
			}
		}

		#You have too many confederates
		#For duchy
		modifier = {
			add = duchy_confederation_vassals_value
			desc = TOO_MANY_CONFEDERATION_VASSALS_REASON
			scope:actor.confederation ?= {
				any_confederation_member = {
					count >= 6
					highest_held_title_tier >= tier_county
				}
			}
		}

		modifier = {
			add = -100
			desc = ANOTHER_CULTURE_LEAGUE_REASON
			NOT = {
				scope:actor.culture = {
					this = scope:recipient.culture
				}
			}
			any_confederation = {
				confederation_type = confederation_type:TMA_csto_type
				NOT = {
					this = scope:actor.confederation
				}
				has_variable = confederation_culture
				var:confederation_culture = scope:recipient.culture 
				scope:recipient = { TMA_csto_goverments_allowed = yes }
			}
		}

		#Another confederation of recipient's faith exists
		modifier = {
			add = -100
			desc = ANOTHER_FAITH_LEAGUE_REASON
			NOT = {
				scope:actor.faith = {
					this = scope:recipient.faith
				}
			}
			any_confederation = {
				confederation_type = confederation_type:TMA_csto_type
				NOT = {
					this = scope:actor.confederation
				}
				has_variable = confederation_faith
				var:confederation_faith = scope:recipient.faith
				scope:recipient = { TMA_csto_goverments_allowed = yes }
			}
		}

		#Forcing them to join war
		modifier = {
			add = -100
			desc = FORCED_TO_JOIN_WAR_REASON
			scope:actor = {
				is_at_war_as_defender = yes
				any_character_war = {
					primary_defender = {
						OR = {
							this = scope:actor
							AND = {
								exists = scope:actor.confederation
								is_member_of_confederation = scope:actor.confederation
							}
						}
					}
					is_defender = scope:actor
					NOR = {
						any_war_attacker = {
							is_allied_to = scope:recipient
						}
						any_war_attacker = {
							this = scope:recipient
						}
						any_war_defender = {
							this = scope:recipient
						}
						any_war_attacker = {
							scope:recipient = {
								has_truce = prev
							}
						}
					}
				}
			}
		}

		#You will be taking over their war
		modifier = {
			add = 100
			desc = TAKING_OVER_DEFENSIVE_WAR_REASON
			scope:recipient = {
				is_at_war_as_defender = yes
				any_character_war = {
					is_war_leader = scope:recipient
					is_defender = scope:recipient
					NOR = {
						any_war_attacker = {
							is_allied_to = scope:actor
						}
						any_war_attacker = {
							this = scope:actor
						}
						any_war_defender = {
							this = scope:actor
						}
					}
					defender_war_score < 80
					defender_war_score >= 0
				}
			}
		}
		modifier = {
			add = 200
			desc = TAKING_OVER_DEFENSIVE_WAR_REASON
			scope:recipient = {
				is_at_war_as_defender = yes
				any_character_war = {
					is_war_leader = scope:recipient
					is_defender = scope:recipient
					NOR = {
						any_war_attacker = {
							is_allied_to = scope:actor
						}
						any_war_attacker = {
							this = scope:actor
						}
						any_war_defender = {
							this = scope:actor
						}
					}
					defender_war_score < 0
				}
			}
		}

		#Culture is into/not into confederation
		modifier = {
			add = 10
			desc = CONFEDERATION_ETHOS_REASON
			scope:recipient.culture = {
				OR = {
					has_cultural_pillar = ethos_stoic
					has_cultural_pillar = ethos_communal
				}
			}
		}
		modifier = {
			add = 20
			desc = CONFEDERATION_ETHOS_REASON
			scope:recipient.culture = {
				OR = {
					has_cultural_pillar = ethos_egalitarian
				}
			}
		}
		modifier = {
			add = -10
			desc = CONFEDERATION_ETHOS_REASON
			scope:recipient.culture = {
				OR = {
					has_cultural_pillar = ethos_bellicose
					has_cultural_pillar = ethos_courtly
					has_cultural_pillar = ethos_bureaucratic
				}
			}
		}

		modifier = { # Herder
			add = 50
			desc = CONFEDERATION_HERDER_REASON
			trigger = {
				scope:recipient = {
					government_has_flag = government_is_herder
				}
			}
		}
		modifier = {
			add = -20
			desc = NOMADIC_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = nomadic_authority_3
				}
			}
		}
		modifier = {
			add = -50
			desc = NOMADIC_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = nomadic_authority_4
				}
			}
		}
		modifier = {
			add = -100
			desc = NOMADIC_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = nomadic_authority_5
				}
			}
		}
		modifier = {
			add = -20
			desc = TRIBAL_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = tribal_authority_2
				}
			}
		}
		modifier = {
			add = -50
			desc = TRIBAL_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = tribal_authority_3
				}
			}
		}

		modifier = {
			add = -20
			desc = CROWN_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = crown_authority_2
				}
			}
		}
		modifier = {
			add = -50
			desc = CROWN_AUTHORITY_REASON
			trigger = {
				scope:recipient = {
					has_realm_law = crown_authority_3
				}
			}
		}

		modifier = {
			desc = "BARANGAY_TRADITION_REASON"
			trigger = {
				scope:recipient.culture = {
					has_cultural_parameter = invite_to_confederation_extra_reasons
				}
			}
			add = 20
		}
		modifier = { #left your confederation
			add = -50
			desc = LEFT_YOUR_CONFEDERATION_REASON 
			exists = scope:actor.confederation
			scope:recipient = {
				has_variable = left_confederation
				var:left_confederation ?= {
					this = scope:actor.confederation
				}
			}
		}
	}

	send_option = {
		flag = hook
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		localization = GENERIC_SPEND_A_HOOK
	}

	send_option = { # Herd
		is_shown = {
			scope:actor = {
				government_has_flag = government_is_nomadic
				exists = domicile
			}
		}
		is_valid = {
			scope:recipient = {
				government_has_flag = government_is_nomadic
				exists = domicile
			}
			scope:actor.domicile = { herd >= minor_herd_value }
		}
		flag = herd_send_option
		localization = TRADE_HERD_FOR_BETTER_AI_ACCEPTANCE_CONFEDERATION
	}

	send_option = { # Prestige
		is_shown = {
			always = yes
		}
		is_valid = {
			scope:actor = { prestige >= minor_prestige_value }
		}
		flag = prestige_send_option
		localization = TRADE_PRESTIGE_FOR_BETTER_AI_ACCEPTANCE
	}

	send_options_exclusive = no

	on_accept = {
		if = {
			#checking that the delay hasn't made the character invalid
			limit = {
				scope:recipient = {
					is_playable_character = yes
				}
			}
			scope:actor = {
				if = {
					limit = { has_character_flag = forming_confederation }
					remove_character_flag = forming_confederation
				}
			}
			if = {
				limit = {
					scope:recipient = {
						is_at_war_as_defender = yes
						any_character_war = {
							is_war_leader = scope:recipient
							is_defender = scope:recipient
						}
					}
				}
				scope:recipient = {
					every_character_war = {
						limit = {
							is_war_leader = scope:recipient
							is_defender = scope:recipient
						}
						add_to_list = recipient_wars
					}
				}
				every_in_list = {
					list = recipient_wars
					save_scope_as = recipient_war
					primary_attacker = {
						save_scope_as = attacker
					}
					add_defender = scope:actor
					hidden_effect = {
						scope:actor.confederation ?= {
							every_confederation_member = {
								limit = {
									NOR = {
										is_attacker_in_war = scope:recipient_war
										is_defender_in_war = scope:recipient_war
										is_at_war_with = scope:recipient
										is_allied_to = scope:attacker
										is_imprisoned_by = scope:recipient
										is_at_war_with = scope:recipient
									}
								}
								scope:recipient_war = { add_defender = prev }
							}
						}
					}
				}
				if = {
					limit = {
						scope:actor.confederation ?= {
							any_confederation_member = {
								NOR = {
									is_attacker_in_war = scope:recipient_war
									is_defender_in_war = scope:recipient_war
									this = scope:actor
									is_at_war_with = scope:recipient
									is_allied_to = scope:attacker
									is_imprisoned_by = scope:recipient
									is_at_war_with = scope:recipient
								}
							}
						}
					}
					scope:actor = {
						custom_tooltip = league_members_joining_recipient_war_tt
					}
				}
			}
			scope:actor = {
				save_scope_as = confederation_offerer
			}
			scope:recipient = {
				save_scope_as = confederation_accepter
			}
			#Confederation doesn't exist yet
			if = {
				limit = {
					scope:actor = {
						is_confederation_member = no
					}
				}
				scope:actor = {
					custom_tooltip = create_league_tt
					custom_tooltip = league_defensive_wars_tt
					custom_tooltip = unlock_leave_league_interaction_tt
					if = {
						limit = {
							government_has_flag = government_is_nomadic
						}
						add_character_modifier = {
							modifier = mpo_confederation_member_modifier
							years = 5
						}
						capital_county ?= {
							change_county_fertility = major_county_fertility_level_gain
						}
					}
				}
				scope:recipient = {
					offer_defensive_league_accepter_effect = yes
				}
			}
			#Confederation already exists
			else = {
				scope:actor.confederation = {
					save_scope_as = confederation
				}
				show_as_tooltip = {
					#If actor is top liege and offering vassaldom
					scope:recipient = {
						offer_defensive_league_accepter_effect = yes 
					}
				}
			}
			if = {
				limit = {
					scope:recipient = {
						government_has_flag = government_is_nomadic
					}
				}
				scope:recipient = {
					add_character_modifier = {
						modifier = mpo_confederation_member_modifier
						years = 5
					}
					capital_county ?= {
						change_county_fertility = major_county_fertility_level_gain
					}
				}
			}
			if = {
				limit = {
					scope:recipient = {
						is_ai = no
					}
				}
				scope:recipient = {
					custom_tooltip = csto_raiding_attacking_tt
					if = {
						limit = {
							realm_law_use_nomadic_authority = yes
						}
						custom_tooltip = confederation_restrictions_warning_tt
					}
					else_if = {
						limit = {
							realm_law_use_tribal_authority = yes
						}
						custom_tooltip = confederation_restrictions_tribe_warning_tt
					}
					else_if = {
						limit = {
							realm_law_use_crown_authority = yes
						}
						custom_tooltip = csto_restrictions_crown_authority_warning_tt
					}
				}
			}
			scope:recipient = {
				if = {
					limit = {
						government_has_flag = government_is_nomadic
					}
					custom_tooltip = confederation_migrating_leaving_warning_tt
				}
			}
			scope:actor = {
				save_scope_value_as = {
					name = new_TMA_type
					value = flag:defensive_league
				}
				trigger_event = mpo_interactions_events.0001
			}
			if = {
				limit = {
					scope:actor = {
						is_at_war_as_defender = yes
						any_character_war = {
							primary_defender = {
								OR = {
									this = scope:actor
									AND = {
										exists = scope:actor.confederation
										is_member_of_confederation = scope:actor.confederation
									}
								}
							}
							is_defender = scope:actor
							NOR = {
								any_war_attacker = {
									is_allied_to = scope:recipient
								}
								any_war_attacker = {
									this = scope:recipient
								}
								any_war_defender = {
									this = scope:recipient
								}
								any_war_attacker = {
									scope:recipient = {
										has_truce = prev
									}
								}
							}
						}
					}
				}
				scope:actor = {
					every_character_war = {
						limit = {
							primary_defender = {
								OR = {
									this = scope:actor
									AND = {
										exists = scope:actor.confederation
										is_member_of_confederation = scope:actor.confederation
									}
								}
							}
							is_defender = scope:actor
							NOR = {
								any_war_attacker = {
									is_allied_to = scope:recipient
								}
								any_war_attacker = {
									this = scope:recipient
								}
								any_war_defender = {
									this = scope:recipient
								}
								any_war_attacker = {
									scope:recipient = {
										has_truce = prev
									}
								}
							}
						}
						add_defender = scope:recipient
					}
					#Lose legitimacy unless you're taking on a war too
					if = {
						limit = {
							NOT = {
								scope:recipient = {
									is_at_war_as_defender = yes
									any_character_war = {
										is_war_leader = scope:recipient
										is_defender = scope:recipient
										NOR = {
											any_war_attacker = {
												is_allied_to = scope:actor
											}
											any_war_attacker = {
												this = scope:actor
											}
											any_war_attacker = {
												scope:actor = {
													has_truce = prev
												}
											}
										}
									}
								}
							}
						}
						add_legitimacy = medium_legitimacy_loss
						custom_tooltip = confederation_legitimacy_loss_war_tt
					}
				}
			}
			if = {
				limit = {
					scope:prestige_send_option = yes
				}
				scope:recipient = {
					add_prestige = scope:actor.minor_prestige_value
				}
			}
			if = {
				limit = {
					scope:herd_send_option = yes
				}
				scope:actor = {
					pay_herd = {
						target = scope:recipient
						value = domicile.minor_herd_value
					}
				}
			}
		}
	}

	on_decline = {
		#Stop spamming players with this
		if = {
			limit = {
				exists = scope:actor.confederation
				scope:recipient = {
					is_ai = no
				}
			}
			scope:recipient = {
				set_variable = {
					name = refused_confederation
					value = scope:actor.confederation
					years = 3
				}
			}
		}
		scope:actor = {
			#letter response
			trigger_event = mpo_interactions_events.0004
		}
	}

	ai_potential = {
		age >= 6
		OR = {
			AND = {
				is_independent_ruler = yes
				TMA_has_appropriate_title_tier = yes
				has_character_flag = forming_confederation
			}
			is_confederation_member = yes
		}
		TMA_csto_goverments_allowed = yes
	}

	ai_will_do = {
		base = 50

		modifier = {
			add = 25
			scope:actor = {
				confederation_neighboring_foe_trigger = { CHARACTER = scope:actor }
			}
		}
		modifier = {
			add = 25
			scope:recipient = {
				confederation_neighboring_foe_trigger = { CHARACTER = scope:recipient }
			}
		}
		modifier = {
			add = -25
			scope:recipient = {
				is_at_war = yes
			}
		}
		modifier = {
			factor = 0
			OR = {
				#Will only throw the confederation into a war
				scope:recipient = {
					is_at_war = yes
					NOR = {
						any_character_war = { primary_defender = scope:recipient }
						culture = scope:actor.culture
						scope:actor.confederation ?= {
							any_confederation_member = {
								count >= 4
								highest_held_title_tier >= tier_county
							}
						}
						reverse_opinion = {
							target = scope:actor
							value >= 30
						}
					}
				}
				AND = {
					exists = scope:actor.confederation
					scope:recipient = {
						has_variable = left_confederation
						var:left_confederation ?= {
							this = scope:actor.confederation
						}
					}
				}
				#Recipient hasn't been asked to join this confederation already
				scope:recipient = {
					has_variable = refused_confederation
					exists = scope:actor.confederation
					var:refused_confederation = {
						this = scope:actor.confederation
					}
				}
			}
		}
	}
}
