namespace = diplomacy_foreign

 # Diplomacy (Majesty) Events:
 	# 1074		 : Religious Refugees (Oppressed faithful in neighboring county), by Sean Hughes
 	# 3001 - 3008: Correspondence (Become pen pals with a foreign ruler), by Linnéa Thimrén and Sean Hughes
 	# 1100 - 1105: Neighboring ruler offers to organize a meeting between your respective heirs, By Flavio Verna
 	# 1150 - 1154: Neighboring rulers tidy up their de jure a bit, by Flavio Verna
 	# 1200		 : Gain Foreign Affairs Friend, by Flavio Verna

 # Diplomay (Majesty) Special Events:
 	#1010 - 1031: Foreign Relations (Invite a foreign ruler to your court), by Mathilda Bjarnehed

##################################
# Religious Refugees
# 1074
# by Sean Hughes
##################################

scripted_trigger diplomacy_foreign_1074_has_heretic_ruler = {
	exists = holder
	holder = {
		#AI Ruler must not be the same Faith as us.
		is_ai = yes
		NOT = { faith = root.faith}
		save_temporary_scope_as = heretic_ruler

		#And we must find the Faith they belong to Astray or worse.
		root = {
			faith = {
				faith_hostility_level = {
					target = scope:heretic_ruler.faith
					value >= 1
				}
			}
		}
	}
}

#A neighboring county of your religion is being oppressed by a heretic ruler!
diplomacy_foreign.1074 = {
	type = character_event
	title = diplomacy_foreign.1074.t
	desc = diplomacy_foreign.1074.desc
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:heretic_ruler
	
	trigger = {
		NOT = { has_character_flag = had_event_diplomacy_foreign_1074 }
		#We must directly own a county...
		any_held_title = {
			tier = tier_county
			#Adjacent to another county...
			any_neighboring_county = {
				#Which has the same Faith that we do...
				title_province = {
					faith = root.faith
				}
				#And that county which shares our Faith must be owned by a ruler of a different Faith.
				diplomacy_foreign_1074_has_heretic_ruler = yes
			}
		}
	}

	weight_multiplier = {
		base = 1
		upweight_for_focus_modifier = { FOCUS = diplomacy_foreign_affairs_focus }
	}

	immediate = {
		add_character_flag = {
			flag = had_event_diplomacy_foreign_1074
			years = 5
		}
		add_character_flag = {
			flag = had_recent_lifestyle_ongoing_event
			years = ongoing_lifestyle_event_delay
		}
		#Find a county we hold which borders a valid event target county.
		random_held_title = {
			limit = {
				tier = tier_county
				any_neighboring_county = {
					title_province = {
						faith = root.faith
					}
					diplomacy_foreign_1074_has_heretic_ruler = yes
				}
			}
			save_scope_as = my_county

			#Find a valid event target county.
			random_neighboring_county = {
				limit = {
					title_province = {
						faith = root.faith
					}
					diplomacy_foreign_1074_has_heretic_ruler = yes			
				}

				#Save that county's scope.
				save_scope_as = target_county

				#Also save the county's holder.
				holder = {
					save_scope_as = heretic_ruler
				}
			}

		}
	}

	#Option A: Use the situation to declare a claim on the province.
	option = {
		name = diplomacy_foreign.1074.a

		#Gain a claim on the other province.
		add_unpressed_claim = scope:target_county
		add_piety = medium_piety_gain

		stress_impact = {
			craven = medium_stress_impact_gain
			cynical = medium_stress_impact_gain
			lazy = minor_stress_impact_gain
			content = minor_stress_impact_gain
		}
		ai_chance = {
			base = 33

			ai_value_modifier = {
				ai_honor = 200
				ai_zeal = 200
				ai_greed = 200
			}
		}
	}

	#Option B: Smuggle religious refugees out of the province and into your own.
	option = {
		name = diplomacy_foreign.1074.b

		#Gain an economic boost at the expense of the other province's economy.
		scope:my_county = {
			add_county_modifier = {
				modifier = governance_1074_religious_refugees_modifier
				years = 10
			}
		}
		scope:target_county = {
			add_county_modifier = {
				modifier = governance_1074_population_fled_persecution_modifier
				years = 10
			}
		}

		#Suffer a harsh opinion penalty with the other province's ruler.
		scope:heretic_ruler = {
			add_opinion = {
				modifier = angry_opinion
				target = root
				opinion = -25
			}
		}

		stress_impact = {
			honest = medium_stress_impact_gain
		}
		ai_chance = {
			base = 33

			ai_value_modifier = {
				ai_zeal = 200
				ai_boldness = 200
				ai_honor = -200
			}
		}
	}
	
	#Option C: Send an envoy to try and ensure the safety of the faithful.
	option = {
		name = diplomacy_foreign.1074.c


		add_diplomacy_lifestyle_xp = medium_lifestyle_xp
		duel = {
			skill = diplomacy
			value = 10

			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -9
				}


				#The AI ruler approves of your attempt to see to the safety of your people.
				desc = diplomacy_foreign.1074.c.success
				send_interface_toast = {
					title = diplomacy_foreign.1074.c.success
					left_icon = scope:heretic_ruler

					#Add a modifier which makes it harder to convert the province away from it's current faith.
					scope:target_county = {
						add_county_modifier = {
							modifier = governance_1074_local_faith_respected_modifier
							years = 10
						}
					}

					#Gain an opinion boost with the other ruler.
					scope:heretic_ruler = {
						add_opinion = {
							modifier = respect_opinion
							target = root
							opinion = 25
						}
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -39
				}

				#Less likely to succeed if the AI ruler is more Zealous or less Compassionate.
				compare_modifier = {
					value = scope:heretic_ruler.ai_zeal
					multiplier = 2
				}
				compare_modifier = {
					value = scope:heretic_ruler.ai_compassion
					multiplier = -2
				}

				#The AI ruler is supicious of your attempt to bridge the gap between your faiths.
				desc = diplomacy_foreign.1074.c.failure
				send_interface_toast = {
					title = diplomacy_foreign.1074.c.failure
					left_icon = scope:heretic_ruler

					#Gain an opinion penalty with the other ruler.
					scope:heretic_ruler = {
						add_opinion = {
							modifier = suspicion_opinion
							target = root
							opinion = -10
						}
					}
				}
			}
		}

		stress_impact = {
			shy = medium_stress_impact_gain
			zealous = medium_stress_impact_gain
		}
		ai_chance = {
			base = 33

			# Rational characters know when they have a good chance of winning the duel
			compare_modifier = {
				trigger = {
					ai_rationality >= 10
				}
				value = {
					value = diplomacy
					add = -10
				}
				multiplier = 1000 # -10,000 at 0 Diplomacy, +0 at 10 Diplomacy, +10,000 at 20 Diplomacy
			}

			ai_value_modifier = {
				ai_zeal = -200
				ai_compassion = 500
				ai_greed = -100
			}
		}
	}
}

#############################
# Correspondence			#
# 3001-3008					#
# by Linnéa Thimrén			#
#############################
# A neighboring_ruler is interested in being my pen-pal!

#Basic check for character requirements
scripted_trigger diplomacy_foreign_3001_character_trigger = {
	is_available_ai_adult = yes
	NOT = { this = root }
	NOT = { is_close_or_extended_family_of = root }
	NOT = { has_any_scripted_relation = root }
	opinion = {
		target = root
		value >= -20
	}
}

#trigger to be used to check for suitable rank
scripted_trigger diplomacy_foreign_3001_same_rank_trigger = {
	diplomacy_foreign_3001_character_trigger = yes
	OR = {
		AND = {
			root = { highest_held_title_tier = tier_county }
			highest_held_title_tier = tier_duchy
		}
		AND = {
			root = { highest_held_title_tier = tier_duchy }
			OR = {
				highest_held_title_tier = tier_duchy
				highest_held_title_tier = tier_kingdom
			}
		}
		AND = {
			root = { highest_held_title_tier = tier_kingdom }
			OR = {
				highest_held_title_tier = tier_kingdom
				highest_held_title_tier = tier_empire
			}
		}
	}
}

diplomacy_foreign.3001 = {
	type = letter_event
	opening = {
		desc = diplomacy_foreign.3001.opening
	}
	desc = diplomacy_foreign.3001.desc
	sender = scope:neighboring_ruler
	
	trigger = {
		highest_held_title_tier >= tier_county
		OR = {
			# For all characters, we must have a nearby ruler of the same/similar rank.
			any_neighboring_top_liege_realm_owner = {
				NOT = { is_at_war_with = root }
				OR = {
					diplomacy_foreign_3001_same_rank_trigger = yes
					any_vassal = {
						diplomacy_foreign_3001_same_rank_trigger = yes
						NOT = { is_at_war_with = root }
					}
				}
			}

			# For vassals of another liege, any of fellow vassals of the same/similar rank are also valid.
			top_liege = {
				any_vassal = {
					NOT = { is_at_war_with = root }
					diplomacy_foreign_3001_same_rank_trigger = yes
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		upweight_for_focus_modifier = { FOCUS = diplomacy_foreign_affairs_focus }
	}

	immediate = {
		save_scope_as = ruler
		# Check for another vassal of the same/similar rank in our realm, first.
		top_liege = {
			random_vassal = {
				limit = {
					diplomacy_foreign_3001_same_rank_trigger = yes
					NOT = { is_at_war_with = root }
				}
				save_scope_as = neighboring_ruler
			}
		}
		if = {
			# If that fails, look for a neighboring ruler with the same rank as us.
			limit = {
				NOT = { exists = scope:neighboring_ruler }
			}
			if = {
				limit = {
					any_neighboring_top_liege_realm_owner = {
						diplomacy_foreign_3001_same_rank_trigger = yes
						NOT = { is_at_war_with = root }
					}
				}
				random_neighboring_top_liege_realm_owner = {
					limit = {
						diplomacy_foreign_3001_same_rank_trigger = yes
						NOT = { is_at_war_with = root }
					}
					save_scope_as = neighboring_ruler
				}
			}
			# If that fails, find a vassal of a neighboring ruler, where the vassal is the same or similar rank to us.
			else_if = {
				limit = {
					any_neighboring_top_liege_realm_owner = {
						NOT = { is_at_war_with = root }
						any_vassal = {
							diplomacy_foreign_3001_same_rank_trigger = yes
							NOT = { is_at_war_with = root }
						}
					}
				}
				random_neighboring_top_liege_realm_owner = {
					limit = {
						NOT = { is_at_war_with = root }
						any_vassal = {
							diplomacy_foreign_3001_same_rank_trigger = yes
							NOT = { is_at_war_with = root }
						}
					}
					random_vassal = {
						limit = {
							diplomacy_foreign_3001_same_rank_trigger = yes
							NOT = { is_at_war_with = root }
						}
						save_scope_as = neighboring_ruler
					}
				}
			}
		}
	}

	# Option A: I'll get to know them!
	option = {
		name = diplomacy_foreign.3001.a

		scope:neighboring_ruler = {
			add_opinion = {
				target = root
				modifier = friendliness_opinion
				opinion = 15
			}
			hidden_effect = {
				reverse_add_opinion = {
					target = root
					modifier = friendliness_opinion
					opinion = 15
				}
			}

		}
		custom_tooltip = diplomacy_foreign.3001.a.tt
		scope:neighboring_ruler = {
			trigger_event = diplomacy_foreign.3002
		}

		stress_impact = {
			shy = medium_stress_impact_gain
			dull = minor_stress_impact_gain
		}

		ai_chance = {
			base = 100

			modifier = {
				add = -100
				has_trait = shy
			}
		}
	}

	# Option B: Frame the letter for a prestige boost.
	option = {
		name = diplomacy_foreign.3001.b
		add_prestige = medium_prestige_gain
		ai_chance = {
			base = 0
			modifier = {
				add = 80
				has_trait = lazy
			}
		}

		stress_impact = {
			humble = minor_stress_impact_gain
		}

		ai_chance = {
			base = 0

			modifier = {
				add = 100
				OR = {			
					has_trait = arrogant
					has_trait = greedy
					has_trait = arbitrary
				}
			}
		}
	}
	
	# Option C: I have more important things to worry about!
	option = {
		name = diplomacy_foreign.3001.c

		scope:neighboring_ruler = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = 5
			}
		}

		# Out-out is generally a stress loss, but can be a stress gain for gregatious characters.
		stress_impact = {
			base = minor_stress_impact_loss
			shy = minor_stress_impact_loss
			gregarious = medium_stress_impact_gain
		}

		ai_chance = {
			base = 0

			modifier = {
				add = 100
				has_trait = shy
			}
		}
	}
}


#Response for neighboring_ruler (to check they're still alive etc.)
diplomacy_foreign.3002 = {
	hidden = yes

	immediate = {
		scope:ruler = {
			trigger_event = {
				id = diplomacy_foreign.3003
				days = { 14 30 }
			}
		}
	}
}


#What should we talk about?
diplomacy_foreign.3003 = {
	type = letter_event
	opening = {
		desc = diplomacy_foreign.3001.opening
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:dead_ruler
				}
				desc = diplomacy_foreign.3003.desc_dead
			}
			desc = diplomacy_foreign.3003.desc
		}
	}
	sender = scope:neighboring_ruler

	trigger = {
		exists = capital_county #In case you're an ai who lost their land
	}

	immediate = {
		if = {
			limit = {
				scope:neighboring_ruler = {
					is_alive = no
				}
			}
			scope:neighboring_ruler = {
				save_scope_as = dead_ruler
				primary_title = {
					holder = {
						save_scope_as = neighboring_ruler
						add_opinion = {
							target = root
							modifier = friendliness_opinion
							opinion = 15
						}
						reverse_add_opinion = {
							target = root
							modifier = friendliness_opinion
							opinion = 15
						}
					}
				}
			}
		}
		capital_county = {
			save_scope_as = capital
		}
		hidden_effect = {
			random_list = {
				1 = {
					modifier = {
						add = 100
						OR = {
							has_diplomacy_lifestyle_trait_trigger = yes
							has_lifestyle = diplomacy_lifestyle
							diplomacy >= high_skill_rating
						}
					}
					save_scope_value_as = {
						name = first_topic_to_write_about
						value = flag:lifestyle_diplomat
					}
				}
				1 = {
					modifier = {
						add = 100
						OR = {
							has_martial_lifestyle_trait_trigger = yes
							has_lifestyle = martial_lifestyle
							martial >= decent_skill_rating
						}
					}
					save_scope_value_as = {
						name = first_topic_to_write_about
						value = flag:lifestyle_general
					}
				}
				1 = {
					modifier = {
						add = 100
						OR = {
							has_stewardship_lifestyle_trait_trigger = yes
							has_lifestyle = stewardship_lifestyle
							stewardship >= decent_skill_rating
						}
					}
					save_scope_value_as = {
						name = first_topic_to_write_about
						value = flag:lifestyle_administrator
					}
				}
				1 = {
					modifier = {
						add = 100
						OR = {
							has_intrigue_lifestyle_trait_trigger = yes
							has_lifestyle = intrigue_lifestyle
							intrigue >= decent_skill_rating
						}
					}
					save_scope_value_as = {
						name = first_topic_to_write_about
						value = flag:lifestyle_schemer
					}
				}
				1 = {
					modifier = {
						add = 100
						OR = {
							has_learning_lifestyle_trait_trigger = yes
							has_lifestyle = learning_lifestyle
							learning >= decent_skill_rating
						}
					}
					save_scope_value_as = {
						name = first_topic_to_write_about
						value = flag:lifestyle_scholar
					}
				}
			}
		}
	}

	option = { #We'll talk about my lifestyle
		name = {
			trigger = {
				scope:first_topic_to_write_about = flag:lifestyle_diplomat
			}
			text = diplomacy_foreign.3003.a_lifestyle_diplomat
		}
		name = {
			trigger = {
				scope:first_topic_to_write_about = flag:lifestyle_general
			}
			text = diplomacy_foreign.3003.a_lifestyle_general
		}
		name = {
			trigger = {
				scope:first_topic_to_write_about = flag:lifestyle_administrator
			}
			text = diplomacy_foreign.3003.a_lifestyle_administrator
		}
		name = {
			trigger = {
				scope:first_topic_to_write_about = flag:lifestyle_schemer
			}
			text = diplomacy_foreign.3003.a_lifestyle_schemer
		}
		name = {
			trigger = {
				scope:first_topic_to_write_about = flag:lifestyle_scholar
			}
			text = diplomacy_foreign.3003.a_lifestyle_scholar
		}
		flavor = diplomacy_foreign.3003.a.tt
		save_scope_value_as = {
			name = option_chosen
			value = flag:lifestyle
		}
		scope:neighboring_ruler = {
			trigger_event = diplomacy_foreign.3004
		}
		ai_chance = {
			base = 50
			modifier = {
				add = 50
				has_trait = arrogant
			}
			ai_value_modifier = {
				ai_boldness = 0.75
			}
		}
	}

	option = { #We'll talk about their subject of interest
		name = diplomacy_foreign.3003.b
		flavor = diplomacy_foreign.3003.b.tt
		save_scope_value_as = {
			name = option_chosen
			value = flag:interest
		}
		scope:neighboring_ruler = {
			trigger_event = diplomacy_foreign.3005
		}
		ai_chance = {
			base = 50
			modifier = {
				add = 50
				OR = {
					has_trait = humble
					has_trait = compassionate
					has_trait = shy
				}
			}
			ai_value_modifier = {
				ai_compassion = 0.75
			}
		}
	}
	
	option = { #I'll send them a gift!
		name = diplomacy_foreign.3003.c
		flavor = diplomacy_foreign.3003.c.tt
		remove_short_term_gold = medium_gold_value
		save_scope_value_as = {
			name = option_chosen
			value = flag:gift
		}
		scope:neighboring_ruler = {
			trigger_event = diplomacy_foreign.3006
		}
		ai_chance = {
			base = 50
			modifier = {
				add = 50
				has_trait = generous
			}
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_greed = -0.25
			}
		}
	}
}

#Did the neighboring_ruler like your lifestyle?
diplomacy_foreign.3004 = {
	hidden = yes

	immediate = {
		if = {
			limit = {
				OR = {
					AND = { # High Diplomacy
						scope:first_topic_to_write_about = flag:lifestyle_diplomat
						OR = {
							OR = {
								has_diplomacy_lifestyle_trait_trigger = yes
								has_lifestyle = diplomacy_lifestyle
								diplomacy >= high_skill_rating
							}
						}
					}
					AND = { # High Diplomacy
						scope:first_topic_to_write_about = flag:lifestyle_general
						OR = {
							OR = {
								has_martial_lifestyle_trait_trigger = yes
								has_lifestyle = martial_lifestyle
								martial >= decent_skill_rating
							}
						}
					}
					AND = { # High Diplomacy
						scope:first_topic_to_write_about = flag:lifestyle_administrator
						OR = {
							OR = {
								has_stewardship_lifestyle_trait_trigger = yes
								has_lifestyle = stewardship_lifestyle
								stewardship >= decent_skill_rating
							}
						}
					}
					AND = { # High Diplomacy
						scope:first_topic_to_write_about = flag:lifestyle_schemer
						OR = {
							OR = {
								has_intrigue_lifestyle_trait_trigger = yes
								has_lifestyle = intrigue_lifestyle
								intrigue >= decent_skill_rating
							}
						}
					}
					AND = { # High Diplomacy
						scope:first_topic_to_write_about = flag:lifestyle_scholar
						OR = {
							OR = {
								has_learning_lifestyle_trait_trigger = yes
								has_lifestyle = learning_lifestyle
								learning >= decent_skill_rating
							}
						}
					}
				}
			}
			scope:ruler = {
				trigger_event = {
					id = diplomacy_foreign.3007
					days = { 14 30 }
				}
			}
		}
		else = {
			scope:ruler = {
				trigger_event = {
					id = diplomacy_foreign.3008
					days = { 14 30 }
				}
			}
		}
	}
}

#Did the neighboring_ruler like their subject of interest?
diplomacy_foreign.3005 = {
	hidden = yes

	immediate = {
		hidden_effect = {
			random_list = {
				25 = {
					modifier = {
						add = -10
						has_trait = shy
					}
					modifier = {
						add = -10
						has_trait = humble
					}
					modifier = {
						add = -10
						has_trait = content
					}

					scope:ruler = {
						trigger_event = {
							id = diplomacy_foreign.3007
							days = { 14 30 }
						}
					}

				}
				25 = {
					modifier = {
						add = -10
						has_trait = arrogant
					}
					modifier = {
						add = -10
						has_trait = arbitrary
					}
					modifier = {
						add = -10
						has_trait = paranoid
					}

					scope:ruler = {
						trigger_event = {
							id = diplomacy_foreign.3008
							days = { 14 30 }
						}
					}
				}
			}
		}
	}
}


#Did the neighboring_ruler like the gift?
diplomacy_foreign.3006 = {
	hidden = yes

	immediate = {
		hidden_effect = {
			random_list = {
				25 = {
					compare_modifier = {
						value = ai_greed
						multiplier = 0.5
						min = -25
					}
					scope:ruler = {
						trigger_event = {
							id = diplomacy_foreign.3007
							days = { 14 30 }
						}
					}
				}
				25 = {
					compare_modifier = {
						value = ai_greed
						multiplier = -0.5
						min = -25
					}
					scope:ruler = {
						trigger_event = {
							id = diplomacy_foreign.3008
							days = { 14 30 }
						}
					}
				}
			}
		}
	}
}


#I managed to win them over!
diplomacy_foreign.3007 = {
	type = letter_event
	opening = {
		desc = diplomacy_foreign.3001.opening
	}
	desc = {
		triggered_desc = {
			trigger = {
				exists = scope:dead_ruler_2
			}
			desc = diplomacy_foreign.3003.desc_dead
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:option_chosen = flag:lifestyle
				}
				desc = diplomacy_foreign.3007.desc_lifestyle
			}
			triggered_desc = {
				trigger = {
					scope:option_chosen = flag:interest
				}
				desc = diplomacy_foreign.3007.desc_interest
			}
			triggered_desc = {
				trigger = {
					scope:option_chosen = flag:gift
				}
				desc = diplomacy_foreign.3007.desc_gift
			}
		}
		desc = diplomacy_foreign.3007.desc_ending
	}
	sender = scope:neighboring_ruler

	immediate = {
		if = {
			limit = {
				scope:neighboring_ruler = {
					is_alive = no
				}
			}
			scope:neighboring_ruler = {
				save_scope_as = dead_ruler_2
				primary_title = {
					holder = {
						save_scope_as = neighboring_ruler
						add_opinion = {
							target = root
							modifier = friendliness_opinion
							opinion = 15
						}
						reverse_add_opinion = {
							target = root
							modifier = friendliness_opinion
							opinion = 15
						}
					}
				}
			}
		}
		if = {
			limit = {
				exists = capital_county
			}
			capital_county = {
				save_scope_as = capital
			}
		}
	}

	option = { #Focus on diplomacy
		name = diplomacy_foreign.3007.a

		# Mutual opinion increase.
		scope:neighboring_ruler = {
			add_opinion = {
				modifier = friendliness_opinion
				target = root
				opinion = 15
			}
		}
		hidden_effect = {
			add_opinion = {
				modifier = friendliness_opinion
				target = scope:neighboring_ruler
				opinion = 15
			}
		}

		# Move closer to forming a friendship.
		progress_towards_friend_effect = {
			CHARACTER = scope:neighboring_ruler
			OPINION = no
		}
		

		# How much do you learn from the letters?
		duel = {
			skill = learning
			value = medium_skill_rating

			50 = {
				desc = diplomacy_foreign.3007.a_random_list_good
				compare_modifier = {
 					value = scope:duel_value
  					multiplier = 3.5
	  			}

				send_interface_toast = {
					title = diplomacy_foreign.3007.a_random_list_good
					
					add_diplomacy_lifestyle_xp = major_lifestyle_experience
				}
			}
			50 = {
				desc = diplomacy_foreign.3007.a_random_list_bad
				compare_modifier = {
 					value = scope:duel_value
  					multiplier = -3.5
	  			}

				send_interface_toast = {
					title = diplomacy_foreign.3007.a_random_list_bad
					
					add_diplomacy_lifestyle_xp = medium_lifestyle_experience
				}
			}
		}

		stress_impact = {
			shy = medium_stress_impact_gain
			dull = medium_stress_impact_gain
			intellect_bad_1 = minor_stress_impact_gain
			intellect_bad_2 = medium_stress_impact_gain
			intellect_bad_3 = major_stress_impact_gain
		}

		ai_chance = {
			base = 75

			ai_value_modifier = {
				ai_compassion = 100
				ai_greed = -50
				ai_energy = -50
			}
		}
	}

	option = { #County modifier!
		name = diplomacy_foreign.3007.b
		trigger = {
			exists = capital_county
			NOT = {
				scope:capital = {
					 has_county_modifier = diplomacy_foreign_3001_trade_deal_modifier
				}
			}
		}
		scope:capital = {
			add_county_modifier = {
				modifier = diplomacy_foreign_3001_trade_deal_modifier
				days = 1825
			}
		}

		stress_impact = {
			generous = medium_stress_impact_gain
			content = minor_stress_impact_gain
		}

		ai_chance = {
			base = 25

			ai_value_modifier = {
				ai_greed = 300
			}
		}
	}
	
	option = { #High risk, high reward option
		trigger = {
			can_add_hook = {
				target = scope:neighboring_ruler
				type = favor_hook
			}
		}
		name = diplomacy_foreign.3007.c
		flavor = diplomacy_foreign.3007.c_tt

		duel = {
			skill = diplomacy
			target = scope:neighboring_ruler

			50 = {
				desc = diplomacy_foreign.3007.c_random_list_good
				modifier = {
					add = 25
					scope:neighboring_ruler = {
						has_personality_submissive_trigger = yes
					}
				}
				compare_modifier = {
 					value = scope:duel_value
  					multiplier = 3.5
  					min = -49
	  			}	

				send_interface_toast = {
					title = diplomacy_foreign.3007.c_random_list_good
					add_hook = {
						target = scope:neighboring_ruler
						type = favor_hook
					}
				}
			}
			50 = {
				desc = diplomacy_foreign.3007.c_random_list_bad
				modifier = {
					add = 25
					scope:neighboring_ruler = {
						has_personality_dominant_trigger = yes
					}
				}
				compare_modifier = {
 					value = scope:duel_value
  					multiplier = -3.5
  					min = -49
	  			}				
				
				send_interface_toast = {
					title = diplomacy_foreign.3007.c_random_list_bad
					add_prestige = minor_prestige_loss
					scope:neighboring_ruler = {
						add_opinion = {
							modifier = respect_opinion
							target = root
							opinion = -15
						}
					}

				}
			}
		}

		stress_impact = {
			compassionate = medium_stress_impact_gain
			content = medium_stress_impact_gain
			honest = minor_stress_impact_gain
		}

		ai_chance = {
			base = 0
			
			ai_value_modifier = {
				ai_boldness = 200
				ai_greed = 100
				ai_compassion = -100
			}
		}
	}
}

#I failed :(
diplomacy_foreign.3008 = {
	type = letter_event
	opening = diplomacy_foreign.3008.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:dead_ruler_2
				}
				desc = diplomacy_foreign.3008.desc.dead
			}
			triggered_desc = {
				trigger = {
					scope:option_chosen = flag:lifestyle
				}
				desc = diplomacy_foreign.3008.desc.skilled
			}
			triggered_desc = {
				trigger = {
					scope:option_chosen = flag:interest
				}
				desc = diplomacy_foreign.3008.desc.special
			}
			triggered_desc = {
				trigger = {
					scope:option_chosen = flag:gift
				}
				desc = diplomacy_foreign.3008.desc.bribe
			}
		}
		desc = diplomacy_foreign.3008.desc.end
	}
	sender = scope:neighboring_ruler

	immediate = {
		if = {
			limit = {
				scope:neighboring_ruler = {
					is_alive = no
				}
			}
			scope:neighboring_ruler = {
				save_scope_as = dead_ruler_2
				primary_title = {
					holder = {
						save_scope_as = neighboring_ruler
					}
				}
			}
		}
		scope:neighboring_ruler = {
			add_opinion = {
				modifier = insult_opinion
				target = root
				opinion = -15
			}
		}
	}

	option = { #Bummer
		name = diplomacy_foreign.3008.a
		flavor = diplomacy_foreign.3008.a.tt
		add_diplomacy_lifestyle_xp = minor_lifestyle_experience
	}
}

###########################################################
# Events converted from the old Progress Event system.
# These events are all challenging event chains which should reward a perk point or some other significant benefit for successfully completing them.
namespace = diplomacy_foreign_special

#############################
# Foreign Relations			#
# 1017-1031					#
# by Mathilda Bjarnehed		#
#############################

scripted_trigger diplomacy_foreign_special_1017_potential_guest_trigger = {
	is_available_healthy_ai_adult = yes
	is_at_war = no
	highest_held_title_tier > tier_barony
	OR = {
		highest_held_title_tier <= root.highest_held_title_tier
		root = { highest_held_title_tier >= tier_duchy }
	}
	opinion = {
		target = root
		value >= -20
	}
	NOR = {
		has_relation_friend = root
		has_relation_lover = root
		has_relation_potential_lover = root
		is_spouse_of = root
		is_close_or_extended_family_of = root
	}
}

scripted_trigger diplomacy_foreign_special_1017_success_trigger = { #tune if more events are added
	has_variable = court_visit_points
	has_variable = no_of_court_visit_events
	var:court_visit_points >= 3
	OR = { #Either you've had more than 2 events, or you can have no more events
		var:no_of_court_visit_events >= 2
		AND = {
			diplomacy_foreign_special_1020_can_trigger_trigger = no
			diplomacy_foreign_special_1021_can_trigger_trigger = no
			diplomacy_foreign_special_1022_can_trigger_trigger = no
			diplomacy_foreign_special_1028_can_trigger_trigger = no
		}
	}
}

scripted_trigger diplomacy_foreign_special_1017_great_success_trigger = { #tune if more events are added
	has_variable = court_visit_points
	var:court_visit_points >= 4
}

scripted_trigger diplomacy_foreign_special_1017_failure_trigger = { #tune if more events are added
	has_variable = court_visit_points
	has_variable = no_of_court_visit_events
	OR = {
		AND = { #Fail: out of events and no win
			var:court_visit_points < 3
			OR = {
				var:no_of_court_visit_events >= 4
				AND = {
					diplomacy_foreign_special_1020_can_trigger_trigger = no
					diplomacy_foreign_special_1021_can_trigger_trigger = no
					diplomacy_foreign_special_1022_can_trigger_trigger = no
					diplomacy_foreign_special_1028_can_trigger_trigger = no
				}
			}
		}
		AND = { #Fail: Negative after 1+ ongoing event
			var:no_of_court_visit_events >= 1
			var:court_visit_points < 0
		}
	}
}

scripted_trigger diplomacy_foreign_special_1017_ongoing_trigger = {
	has_variable = court_visit_points
	has_variable = no_of_court_visit_events
	NOR = {
		has_character_flag = in_success_event
		has_character_flag = in_failure_event
	}
}

diplomacy_foreign_special.1017 = {
	type = character_event
	title = diplomacy_foreign_special.1017.t
	desc = diplomacy_foreign_special.1017.desc
	
	theme = diplomacy_foreign_affairs_focus
	left_portrait = root
	lower_left_portrait = scope:target_list_member_a
	lower_center_portrait = scope:target_list_member_b
	lower_right_portrait = scope:target_list_member_c
	
	trigger = {
		has_focus = diplomacy_foreign_affairs_focus

		#Expensive event, especially for those who aren't independent, so we restrict it for AI
		trigger_if = {
			limit = { is_ai = yes }
			is_independent_ruler = yes
			highest_held_title_tier >= tier_duchy
		}

		# Only once per lifetime.
		NOT = { has_character_flag = had_diplomacy_foreign_special_1017_this_lifetime }
		NOt = { has_character_flag = is_in_diplomacy_foreign_special_event }

		# Must have a potential guest to invite.
		is_at_war = no
		OR = {
			AND = {
				is_independent_ruler = yes
				any_neighboring_top_liege_realm_owner = {
					OR = {
						diplomacy_foreign_special_1017_potential_guest_trigger = yes
						any_vassal = { diplomacy_foreign_special_1017_potential_guest_trigger = yes }
					}
				}
			}
			AND = {
				is_independent_ruler = no
				any_neighboring_top_liege_realm_owner = {
					any_vassal_or_below = {
						diplomacy_foreign_special_1017_potential_guest_trigger = yes
					}
				}
			}
		}
	}


	immediate = {
		hidden_effect = {
			add_character_flag = {
				flag = is_in_diplomacy_foreign_special_event
				days = 100
			}
			add_character_flag = had_diplomacy_foreign_special_1017_this_lifetime


			#Pick 3 options
			if = { #For independent rulers
				limit = { is_independent_ruler = yes }

				set_while_counter_variable_effect = yes

				while = {
					limit = { var:while_counter < 3 }

					if = { #Preferably, another independent ruler
						limit = {
							any_neighboring_top_liege_realm_owner = {
								diplomacy_foreign_special_1017_potential_guest_trigger = yes
								NOT = { any_in_list = { list = potential_guests this = prev } }
							}
						}
						random_neighboring_top_liege_realm_owner = {
							limit = {
								# Favor rulers of our rank or higher.
								highest_held_title_tier >= root.highest_held_title_tier

								# Basic requirements.
								diplomacy_foreign_special_1017_potential_guest_trigger = yes
								NOT = { any_in_list = { list = potential_guests this = prev } }
							}
							alternative_limit = {
								# Basic requirements only.
								diplomacy_foreign_special_1017_potential_guest_trigger = yes
								NOT = { any_in_list = { list = potential_guests this = prev } }
							}
							add_to_list = potential_guests
						}
					}
					else = { #If not, a vassal
						random_neighboring_top_liege_realm_owner = {
							limit = {
								any_vassal_or_below = {
									diplomacy_foreign_special_1017_potential_guest_trigger = yes
									NOT = { any_in_list = { list = potential_guests this = prev } }
								}
							}
							random_vassal_or_below = {
								limit = {
									# Favor rulers of our rank or higher.
									highest_held_title_tier >= root.highest_held_title_tier

									# Basic requirements.
									diplomacy_foreign_special_1017_potential_guest_trigger = yes
									NOT = { any_in_list = { list = potential_guests this = prev } }
								}
								alternative_limit = {
									# Basic requirements only.
									diplomacy_foreign_special_1017_potential_guest_trigger = yes
									NOT = { any_in_list = { list = potential_guests this = prev } }
								}

								add_to_list = potential_guests
							}
						}
					}

					increase_while_counter_variable_effect = yes
				}
			}

			else = { #For dependent rulers
				set_while_counter_variable_effect = yes
				while = {
					limit = { var:while_counter < 3 }

					random_neighboring_top_liege_realm_owner = {
						limit = {
							any_vassal_or_below = {
								diplomacy_foreign_special_1017_potential_guest_trigger = yes
								NOT = { any_in_list = { list = potential_guests this = prev } }
							}
						}
						random_vassal_or_below = {
							limit = {
								diplomacy_foreign_special_1017_potential_guest_trigger = yes
								NOT = { any_in_list = { list = potential_guests this = prev } }
							}
							weight = {
								modifier = {
									highest_held_title_tier = tier_duchy
									factor = 5
								}
								modifier = {
									highest_held_title_tier = tier_kingdom
									factor = 25
								}
							}
							add_to_list = potential_guests
						}
					}
					increase_while_counter_variable_effect = yes
				}
			}
			remove_while_counter_variable_effect = yes

			# Save every member in the list out as their own scope.
			ordered_in_list = {
				list = potential_guests
				max = 3
				check_range_bounds = no

				order_by = highest_held_title_tier

				if = {
					limit = {
						NOT = { exists = scope:target_list_member_a }
					}
					save_scope_as = target_list_member_a
				}
				else_if = {
					limit = {
						NOT = { exists = scope:target_list_member_b }
					}
					save_scope_as = target_list_member_b
				}
				else = {
					save_scope_as = target_list_member_c
				}
			}

			primary_title = {
				save_scope_as = root_realm
			}

			save_scope_as = host_ruler
		}
	}

	#Pick guest a
	option = {
		name = diplomacy_foreign_special.1017.a
		diplomacy_foreign_special_1010_court_visit_outcome_preview_effect = { CHARACTER = scope:target_list_member_a }

		scope:target_list_member_a = { save_scope_as = foreign_guest }
		hidden_effect = { trigger_event = { id = diplomacy_foreign_special.1018 days = 7 } }
	}

	#Pick guest b
	option = {
		trigger = { exists = scope:target_list_member_b }
		name = diplomacy_foreign_special.1017.b
		diplomacy_foreign_special_1010_court_visit_outcome_preview_effect = { CHARACTER = scope:target_list_member_b }

		scope:target_list_member_b = { save_scope_as = foreign_guest }
		hidden_effect = { trigger_event = { id = diplomacy_foreign_special.1018 days = 7 } }
	}
	
	#Pick guest c
	option = {
		trigger = { exists = scope:target_list_member_c }
		name = diplomacy_foreign_special.1017.c
		diplomacy_foreign_special_1010_court_visit_outcome_preview_effect = { CHARACTER = scope:target_list_member_c }

		scope:target_list_member_c = { save_scope_as = foreign_guest }
		hidden_effect = { trigger_event = { id = diplomacy_foreign_special.1018 days = 7 } }
	}

	#I don't want to do this
	option = {
		name = diplomacy_foreign_special.1017.d
		
		add_character_modifier = {
			modifier = diplomacy_foreign_1017_internal_affairs_modifier
			years = 5
		}

		remove_character_flag = is_in_diplomacy_foreign_special_event

		ai_chance = {
			base = 0
		}
	}
}

#Invited guest announced they'll be there
diplomacy_foreign_special.1018 = {
	type = letter_event
	opening = {
		desc = diplomacy_foreign_special.1018.opening
	}
	desc = diplomacy_foreign_special.1018.desc
	sender = scope:foreign_guest

	immediate = {
		#If spouse is able to help, make sure it's all good
		if = {
			limit = {
				exists = primary_spouse
				primary_spouse = {
					is_available_healthy_adult = yes
					opinion = {
						target = root
						value > 0
					}
					NOT = { has_trait = lazy }
				}
			}
			primary_spouse = { save_scope_as = helper_spouse }
		}
		play_music_cue = "mx_cue_banquet"
	}
	
	option = {
		name = {
			trigger = { exists = scope:helper_spouse }
			text = diplomacy_foreign_special.1017.a.spouse
		}
		name = {
			trigger = { NOT = { exists = scope:helper_spouse } }
			text = diplomacy_foreign_special.1017.a.no_spouse
		}

		custom_tooltip = diplomacy_foreign_special.1018.a.tt

		hidden_effect = { trigger_event = { id = diplomacy_foreign_special.1019 days = { 15 20 } } }
	}
}

#Welcoming feast! Your approach?
diplomacy_foreign_special.1019 = {
	type = character_event
	title = diplomacy_foreign_special.1019.t
	desc = {
		desc = diplomacy_foreign_special.1019.start.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:helper_spouse }
				desc = diplomacy_foreign_special.1019.spouse.desc
			}
			triggered_desc = {
				trigger = { NOT = { exists = scope:helper_spouse } }
				desc = diplomacy_foreign_special.1019.no_spouse.desc
			}
		}
	}
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = feast
	}
	left_portrait = scope:foreign_guest
	right_portrait = scope:helper_spouse

	immediate = {
		set_variable = {
			name = court_visit_points
			value = 0
			days = 2000
		}
		scope:foreign_guest = {
			primary_title = {
				save_scope_as = foreign_guest_realm
			}
		}
	}
	
	#Make it appropriate to their local cousine and customs
	option = {
		name = diplomacy_foreign_special.1019.a

		duel = {	
			skill = learning
			value = medium_skill_rating
			20 = { #You figure it out 		
				compare_modifier = {
					value = scope:duel_value
					multiplier = 1
				}
				desc = diplomacy_foreign_special.1019.a.success.tt
				send_interface_toast = {
					title = diplomacy_foreign_special.1019.a.success.tt
					left_icon = scope:foreign_guest
					custom_tooltip = diplomacy_foreign_special.court_visit.very_well
				}
				
				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = 2
					}
				}
			}
			15 = { #It's not appropriate 		
				compare_modifier = {
					value = scope:duel_value
					multiplier = -0.5
				}
				desc = diplomacy_foreign_special.1019.a.failure.tt
				send_interface_toast = {
					title = diplomacy_foreign_special.1019.a.failure.tt
					left_icon = scope:foreign_guest
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}

				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = -1
					}
				}
			}
		}
	}

	#Make it lavish
	option = {
		trigger = {
			OR = {
				is_ai = no
				short_term_gold > medium_gold_value_check
			}
		}
		name = diplomacy_foreign_special.1019.b

		remove_short_term_gold = minor_gold_value

		#For tooltip
		show_as_tooltip = {
			random_list = {
				desc = diplomacy_foreign_special.1019.b.rl.desc
				1 = {
					desc = diplomacy_foreign_special.1019.b.success.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.well
				}
				1 = {
					desc = diplomacy_foreign_special.1019.b.failure.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}
			}
		}

		#For actual effect
		hidden_effect = {
			if = {
				limit = {
					scope:foreign_guest = {
						NOR = {
							has_trait = temperate
							has_trait = content
							has_trait = craven
							has_trait = shy
						}
					}
				}

				send_interface_toast = {
					title = diplomacy_foreign_special.1019.b.success.tt
					left_icon = scope:foreign_guest
					custom_tooltip = diplomacy_foreign_special.court_visit.well
				}

				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = 1
					}
				}
			}
			else = {
				send_interface_toast = {
					title = diplomacy_foreign_special.1019.b.failure.tt
					left_icon = scope:foreign_guest
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}
				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = -1
					}
				}
			}
		}
	}
	
	#Play it safe
	option = {
		name = diplomacy_foreign_special.1019.c
		custom_tooltip = diplomacy_foreign_special.court_visit.normal
	}

	after = {
		set_variable = {
			name = no_of_court_visit_events
			value = 0
			days = 2000
		}
		trigger_event = { on_action = diplomacy_foreign_special_1010_court_visit_events days = { 10 15 } }
	}
}

#Court visit "ongoing" event hunt or luncheon
scripted_trigger diplomacy_foreign_special_1020_prefers_hunt_trigger = {
	OR = {
		has_trait = lifestyle_hunter
		ai_boldness > low_positive_boldness
		ai_energy > low_positive_energy
	}
}

scripted_trigger diplomacy_foreign_special_1020_prefers_luncheon_trigger = {
	OR = {
		has_trait = gluttonous
		has_trait = lifestyle_reveler
		ai_boldness < low_negative_boldness
		ai_energy < low_negative_boldness
	}
}

scripted_trigger diplomacy_foreign_special_1020_can_trigger_trigger = {
	NOT = { has_character_flag = court_visit_hunt_or_dance }
	has_character_flag = is_in_diplomacy_foreign_special_event
	diplomacy_foreign_special_1017_ongoing_trigger = yes
}

diplomacy_foreign_special.1020 = {
	type = character_event
	title = diplomacy_foreign_special.1020.t
	desc = diplomacy_foreign_special.1020.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = terrain
	}
	left_portrait = scope:foreign_guest
	
	trigger = {
		diplomacy_foreign_special_1020_can_trigger_trigger = yes
	}

	immediate = {
		hidden_effect = {
			add_character_flag = {
				flag = court_visit_hunt_or_dance
				days = 100
			}
			change_variable = {
				name = no_of_court_visit_events
				add = 1
			}
		}
	}

	#Hunt
	option = {
		name = diplomacy_foreign_special.1020.a

		#Tooltip (to make both entries appear)
		show_as_tooltip = {
			random_list = {
				1 = {
					desc = diplomacy_foreign_special.1020.a.success.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.well
				}
				1 = {
					desc = diplomacy_foreign_special.1020.a.failure.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}
			}
		}

		hidden_effect = {
			random_list = {
				1 = { #Success
					modifier = { #Don't like hunting and would have prefered luncheon = no chance of success
						diplomacy_foreign_special_1020_prefers_hunt_trigger = no
						diplomacy_foreign_special_1020_prefers_luncheon_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1020.a.success.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.well
					}
					change_variable = {
						name = court_visit_points
						add = 1
					}
				}
				1 = { #Failure
					modifier = { #If they prefer hunt = no chance of failure
						diplomacy_foreign_special_1020_prefers_hunt_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1020.a.failure.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.poorly
					}
					change_variable = {
						name = court_visit_points
						add = -1
					}
				}
			}
		}	
	}


	#luncheon
	option = {
		name = diplomacy_foreign_special.1020.b

		show_as_tooltip = {
			random_list = {
				1 = {
					desc = diplomacy_foreign_special.1020.b.success.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.well
				}
				1 = {
					desc = diplomacy_foreign_special.1020.a.failure.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}
			}
		}

		hidden_effect = {
			random_list = {
				1 = { #Success
					modifier = { #Don't like luncheon and would have prefered hunting = no chance of success
						diplomacy_foreign_special_1020_prefers_luncheon_trigger = no
						diplomacy_foreign_special_1020_prefers_hunt_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1020.b.success.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.well
					}
					change_variable = {
						name = court_visit_points
						add = 1
					}
				}
				1 = { #Failure
					modifier = { #If they prefer luncheon = no chance of failure
						diplomacy_foreign_special_1020_prefers_luncheon_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1020.a.failure.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.poorly
					}
					change_variable = {
						name = court_visit_points
						add = -1
					}
				}
			}
		}
	}

	after = {
		trigger_event = { on_action = diplomacy_foreign_special_1010_court_visit_events days = { 10 15 } }
	}
}


#Court visit "ongoing" event dance or holy place
scripted_trigger diplomacy_foreign_special_1021_prefers_dance_trigger = {
	OR = {
		has_trait = lifestyle_reveler
		ai_zeal < low_negative_zeal
		has_personality_extroverted_trigger = yes
	}
}

scripted_trigger diplomacy_foreign_special_1021_prefers_holy_place_trigger = {
	OR = {
		has_personality_introverted_trigger = yes
		ai_zeal > low_positive_zeal
		has_learning_lifestyle_trait_trigger = yes
	}
}

scripted_trigger diplomacy_foreign_special_1021_can_trigger_trigger = {
	NOT = { has_character_flag = court_visit_dance_or_holy_place }
	has_character_flag = is_in_diplomacy_foreign_special_event
	diplomacy_foreign_special_1017_ongoing_trigger = yes
}

diplomacy_foreign_special.1021 = {
	type = character_event
	title = diplomacy_foreign_special.1021.t
	desc = diplomacy_foreign_special.1021.desc
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:foreign_guest
	
	trigger = {
		diplomacy_foreign_special_1021_can_trigger_trigger = yes
	}

	immediate = {
		hidden_effect = {
			add_character_flag = {
				flag = court_visit_dance_or_holy_place
				days = 100
			}
			change_variable = {
				name = no_of_court_visit_events
				add = 1
			}
		}
	}

	#Dance
	option = {
		name = diplomacy_foreign_special.1021.a

		show_as_tooltip = {
			random_list = {
				1 = {
					desc = diplomacy_foreign_special.1021.a.success.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.well
				}
				1 = {
					desc = diplomacy_foreign_special.1020.a.failure.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}
			}
		}

		hidden_effect = {
			random_list = {
				1 = { #Success
					modifier = { #Don't like dance and would have prefered holy place = no chance of success
						diplomacy_foreign_special_1021_prefers_dance_trigger = no
						diplomacy_foreign_special_1021_prefers_holy_place_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1021.a.success.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.well
					}
					change_variable = {
						name = court_visit_points
						add = 1
					}
				}
				1 = { #Failure
					modifier = { #If they prefer dance = no chance of failure
						diplomacy_foreign_special_1021_prefers_dance_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1020.a.failure.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.poorly
					}
					change_variable = {
						name = court_visit_points
						add = -1
					}
				}
			}
		}
	}


	#Holy place
	option = {
		name = diplomacy_foreign_special.1021.b
		
		show_as_tooltip = {
			random_list = {
				1 = {
					desc = diplomacy_foreign_special.1021.b.success.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.well
				}
				1 = {
					desc = diplomacy_foreign_special.1020.a.failure.tt
					show_chance = no
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}
			}
		}

		hidden_effect = {
			random_list = {
				1 = { #Success
					modifier = { #Don't like holy place and would have prefered dance = no chance of success
						diplomacy_foreign_special_1021_prefers_holy_place_trigger = no
						diplomacy_foreign_special_1021_prefers_dance_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1021.b.success.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.well
					}
					change_variable = {
						name = court_visit_points
						add = 1
					}
				}
				1 = { #Failure
					modifier = { #If they prefer holy place = no chance of failure
						diplomacy_foreign_special_1021_prefers_holy_place_trigger = yes
						factor = 0
					}
					send_interface_toast = {
						title = diplomacy_foreign_special.1020.a.failure.tt
						left_icon = scope:foreign_guest
						custom_tooltip = diplomacy_foreign_special.court_visit.poorly
					}
					change_variable = {
						name = court_visit_points
						add = -1
					}
				}
			}
		}
	}

	after = {
		trigger_event = { on_action = diplomacy_foreign_special_1010_court_visit_events days = { 10 15 } }
	}
}


#Court visit ongoing: guest wants to hang out in private with your spouse/child/consort
scripted_trigger diplomacy_foreign_special_1022_wants_to_befriend = {
	OR = {
		ai_compassion >= low_positive_compassion
		has_trait = gregarious
	}
	opinion = {
		target = root
		value > 0
	}
}

scripted_trigger diplomacy_foreign_special_1022_wants_to_manipulate = {
	OR = {
		ai_greed >= medium_positive_greed
		ai_honor <= low_negative_honor
		has_trait = deceitful
		has_intrigue_lifestyle_trait_trigger = yes
		any_scheme = {
			hostile_scheme_trigger = yes
			scheme_target = root
		}
		opinion = {
			target = root
			value < 0
		}
	}
}

scripted_trigger diplomacy_foreign_special_1022_wants_to_seduce = {
	mgr_can_seduce = yes
	OR = {
		has_trait = lustful
		has_trait = seducer
		has_trait = lifestyle_reveler
		has_trait = gregarious
		has_trait = gallant
		ai_honor <= medium_negative_ai_value
	}
	NOR = {
		has_trait = chaste
		has_trait = celibate
		has_sexuality = asexual
	}
}

scripted_trigger diplomacy_foreign_special_1022_possible_interesting_relation_trigger = {
	is_available_healthy_adult = yes
	exists = liege
	liege = root
	NOT = { is_close_or_extended_family_of = scope:foreign_guest }
}

scripted_trigger diplomacy_foreign_special_1022_can_trigger_trigger = {
	scope:foreign_guest = {
		NOT = { has_trait = shy }
	}
	NOT = { has_character_flag = private_dinner }
	has_character_flag = is_in_diplomacy_foreign_special_event
	OR = {
		any_consort = { diplomacy_foreign_special_1022_possible_interesting_relation_trigger = yes } #spouses and concubines
		any_child = { diplomacy_foreign_special_1022_possible_interesting_relation_trigger = yes }
		any_parent = { diplomacy_foreign_special_1022_possible_interesting_relation_trigger = yes }
	}
	diplomacy_foreign_special_1017_ongoing_trigger = yes
}

diplomacy_foreign_special.1022 = {
	type = character_event
	title = diplomacy_foreign_special.1022.t
	desc = diplomacy_foreign_special.1022.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = feast
	}
	left_portrait = {
		character = scope:foreign_guest
		triggered_animation = {
			trigger = { ai_honor < 0 }
			animation = personality_dishonorable
		}
		animation = admiration
	}
	right_portrait = scope:interesting_relation
	
	trigger = {
		diplomacy_foreign_special_1022_can_trigger_trigger = yes
	}

	weight_multiplier = {
		base = 1
		modifier = {
			factor = 2
			scope:foreign_guest = {
				OR = {
					diplomacy_foreign_special_1022_wants_to_manipulate = yes
					diplomacy_foreign_special_1022_wants_to_seduce = yes
				}
			}
		}
	}

	immediate = {
		hidden_effect = {
			change_variable = {
				name = no_of_court_visit_events
				add = 1
			}
			add_character_flag = {
				flag = private_dinner
				days = 100
			}

			#Select someone, based on what the guest would prefer to do
			every_consort = {
				limit = { diplomacy_foreign_special_1022_possible_interesting_relation_trigger = yes }
				add_to_list = possible_interesting_relations
			}
			every_child = {
				limit = { diplomacy_foreign_special_1022_possible_interesting_relation_trigger = yes }
				add_to_list = possible_interesting_relations
			}
			every_parent = {
				limit = { diplomacy_foreign_special_1022_possible_interesting_relation_trigger = yes }
				add_to_list = possible_interesting_relations
			}

			random_in_list = {
				list = possible_interesting_relations
				weight = {
					base = 1
					modifier = { #General compatability
						factor = 10
						ai_values_divergence = {
							target = scope:foreign_guest
							value <= medium_ai_values_divergence
						}
					}
					modifier = { #For seduce
						factor = 10
						scope:foreign_guest = {
							diplomacy_foreign_special_1022_wants_to_seduce = yes
							is_attracted_to_gender_of = prev
						}
						is_attracted_to_gender_of = scope:foreign_guest
						NOT = { has_trait = celibate }
					}
					modifier = { #For seduce
						factor = 10
						attraction >= low_positive_attraction
						scope:foreign_guest = {
							diplomacy_foreign_special_1022_wants_to_seduce = yes
							is_attracted_to_gender_of = prev
						}
						NOT = { has_trait = celibate }
					}
					modifier = { #For manipulate
						factor = 10
						scope:foreign_guest = { diplomacy_foreign_special_1022_wants_to_manipulate = yes }
						OR = {
							ai_greed >= medium_positive_greed
							ai_honor <= low_negative_honor
							has_intrigue_lifestyle_trait_trigger = yes
							opinion = {
								target = root
								value < 0
							}
							has_trait = trusting
							is_landed = yes
						}
					}
					modifier = {
						factor = 3
						save_temporary_scope_as = potential_player_heir
						root = {
							exists = player_heir
							player_heir = scope:potential_player_heir
						}			
					}
				}
				save_scope_as = interesting_relation
			}		
		}
	}

	#Alright, have a private dinner
	option = {
		name = diplomacy_foreign_special.1022.a
		custom_tooltip = diplomacy_foreign_special.court_visit.very_well
		custom_tooltip = diplomacy_foreign_special.1022.a.tt

		hidden_effect = {
			save_scope_value_as = {
				name = private_dinner_response
				value = flag:yes
			}
		}

		#You might get suspicious
		if = {
			limit = {
				OR = {
					has_trait = paranoid
					AND = {
						NOT = { has_trait = trusting }
						OR = {
							is_spouse_of = scope:interesting_relation
							is_consort_of = scope:interesting_relation
						}
					}
				}
			}
			add_opinion = {
				target = scope:interesting_relation
				modifier = suspicion_opinion
				opinion = -15
			}
		}

		stress_impact = {
			paranoid = massive_stress_gain
			deceitful = minor_stress_gain
			schemer = minor_stress_gain
		}

		ai_chance = {
			base = 10
			modifier = {
				OR = {
					has_trait = paranoid
					has_trait = deceitful
					has_intrigue_lifestyle_trait_trigger = yes
				}
				factor = 0.1
			}
		}
	}

	#I will be watching
	option = {
		name = diplomacy_foreign_special.1022.b

		duel = {
			target = scope:foreign_guest
			skill = intrigue
			20 = { #You find out what they're up to 		
				compare_modifier = {
					value = scope:duel_value
					multiplier = 0.5
				}
				desc = diplomacy_foreign_special.1022.b.duel.success
				custom_tooltip = diplomacy_foreign_special.court_visit.very_well
				hidden_effect = {
					add_character_flag = successful_spy
				}
			}
			10 = { #You are discovered 		
				compare_modifier = {
					value = scope:duel_value
					multiplier = -0.5
				}
				desc = diplomacy_foreign_special.1022.b.duel.failure
				custom_tooltip = diplomacy_foreign_special.court_visit.disaster
				hidden_effect = { trigger_event = { id = diplomacy_foreign_special.1027 days = 3 } } #Discovery
			}
		}

		hidden_effect = {
			save_scope_value_as = {
				name = private_dinner_response
				value = flag:yes_but_spy
			}
		}

		stress_impact = {
			trusting = medium_stress_gain
		}

		ai_chance = {
			base = 10
			modifier = {
				OR = {
					has_trait = trusting
					intrigue < mediocre_skill_rating
				}
				factor = 0.1
			}
			modifier = {
				intrigue > decent_skill_rating
				factor = 3
			}
		}
	}
	
	#I'm not comfortable with this
	option = {
		name = diplomacy_foreign_special.1022.c
		custom_tooltip = diplomacy_foreign_special.court_visit.poorly

		hidden_effect = {
			change_variable = {
				name = court_visit_points
				add = -1
			}
		}

		stress_impact = {
			trusting = medium_stress_gain
		}

		ai_chance = {
			base = 10
			modifier = {
				has_trait = trusting
				factor = 0.2
			}
		}
	}

	#Evaluation of outcome and if you said yes, outcome effect
	after = {
		hidden_effect = {
			trigger_event = { on_action = diplomacy_foreign_special_1010_court_visit_events days = { 8 15 } }

			if = {
				limit = {
					exists = scope:private_dinner_response
					OR = {
						scope:private_dinner_response = flag:yes_but_spy
						scope:private_dinner_response = flag:yes
					}
				}

				change_variable = {
					name = court_visit_points
					add = 2
				}

				#Will the interesting_relation like foreign_guest?
				scope:interesting_relation = {
					random_list = {
						60 = { #Yes
							opinion_modifier = {
								opinion_target = scope:foreign_guest
								multiplier = 1.5
							}
							compatibility_modifier = {
								compatibility_target = scope:foreign_guest
							}
							save_scope_value_as = {
								name = private_dinner_outcome
								value = flag:good
							}
						}
						40 = { #No
							opinion_modifier = {
								opinion_target = scope:foreign_guest
								multiplier = -0.5
							}
							modifier = { #Does not want to be seduced by this person
								scope:foreign_guest = { diplomacy_foreign_special_1022_wants_to_seduce = yes }
								OR = {
									has_trait = celibate
									has_trait = chaste
									NOT = { is_attracted_to_gender_of = scope:foreign_guest }
									scope:foreign_guest = { attraction <= medium_negative_attraction }
								}
								add = -30
							}
							save_scope_value_as = {
								name = private_dinner_outcome
								value = flag:okay
							}
						}
					}
				}
				if = {
					limit = { #If it went well, decide what they did
						scope:private_dinner_outcome = flag:good
					}
					random_list = {
						2 = {
							modifier = {
								scope:foreign_guest = { diplomacy_foreign_special_1022_wants_to_befriend = yes }
								factor = 10
							}
							save_scope_value_as = {
								name = private_dinner_outcome
								value = flag:befriend
							}
						}
						3 = {
							modifier = {
								scope:foreign_guest = { diplomacy_foreign_special_1022_wants_to_manipulate = yes }
								factor = 10
							}
							save_scope_value_as = {
								name = private_dinner_outcome
								value = flag:manipulate
							}
						}
						4 = {
							modifier = {
								trigger = {
									matching_gender_and_sexuality_trigger = {
										CHARACTER_1 = scope:foreign_guest
										CHARACTER_2 = scope:interesting_relation
									}
								}
								scope:foreign_guest = { diplomacy_foreign_special_1022_wants_to_seduce = yes }
								factor = 100
							}
							save_scope_value_as = {
								name = private_dinner_outcome
								value = flag:seduce
							}
						}
					}
				}
				if = { #If no follow-up, make things happen right away
					limit = {
						scope:private_dinner_response = flag:yes
						root = { is_alive = yes }
						scope:foreign_guest = { is_alive = yes }
					}
					diplomacy_foreign_special_1010_court_visit_interesting_relation_outcome_effect = yes #Sets all opinions, relationships & hooks
				}
				if = {
					limit = { has_character_flag = successful_spy }
					remove_character_flag = successful_spy
					hidden_effect = { trigger_event = { on_action = diplomacy_foreign_special_1010_court_visit_interesting_relation_spy_outcome_events days = 3 } }
				}
			}
		}
	}
}

#They just talked
diplomacy_foreign_special.1023 = {
	type = character_event
	title = diplomacy_foreign_special.1023.t
	desc = diplomacy_foreign_special.1023.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = sitting_room
	}
	left_portrait = {
		character = scope:foreign_guest
		animation = happiness
	}
	right_portrait = scope:interesting_relation
	
	trigger = {
		scope:private_dinner_outcome = flag:okay
	}

	option = {
		name = diplomacy_foreign_special.1023.a
		diplomacy_foreign_special_1010_court_visit_interesting_relation_okay_effect = yes
	}
}

#They became really close friends
diplomacy_foreign_special.1024 = {
	type = character_event
	title = diplomacy_foreign_special.1024.t
	desc = diplomacy_foreign_special.1024.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = sitting_room
	}
	left_portrait = {
		character = scope:foreign_guest
		animation = admiration
	}
	right_portrait = {
		character = scope:interesting_relation
		animation = happiness
	}
	
	trigger = {
		scope:private_dinner_outcome = flag:befriend
	}

	#Let it happen
	option = {
		name = diplomacy_foreign_special.1024.a
		diplomacy_foreign_special_1010_court_visit_interesting_relation_befriend_effect = yes

		ai_chance = {
			base = 10
		}
	}

	#Intervene
	option = {
		name = diplomacy_foreign_special.1024.b
		custom_tooltip = diplomacy_foreign_special.court_visit.disaster

		reverse_add_opinion = {
			target = scope:foreign_guest
			modifier = refusal_opinion
			opinion = -20
		}
		hidden_effect = {
			add_opinion = {
				target = scope:foreign_guest
				modifier = disappointed_opinion
				opinion = -20
			}
		}

		hidden_effect = {
			diplomacy_foreign_special_1010_clean_up_court_visit_effect = yes
		}

		ai_chance = {
			base = 2
		}
	}
}

#The guest is manipulating the relation
diplomacy_foreign_special.1025 = {
	type = character_event
	title = diplomacy_foreign_special.1025.t
	desc = diplomacy_foreign_special.1025.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = sitting_room
	}
	left_portrait = {
		character = scope:foreign_guest
		animation = personality_dishonorable
	}
	right_portrait = {
		character = scope:interesting_relation
		animation = admiration
	}
	
	trigger = {
		scope:private_dinner_outcome = flag:manipulate
	}

	#Let it happen
	option = {
		name = diplomacy_foreign_special.1025.a
		diplomacy_foreign_special_1010_court_visit_interesting_relation_manipulate_effect = yes

		hidden_effect = {
			add_character_flag = {
				days = 150
				flag = bad_court_visit_guest
			}
		}

		ai_chance = {
			base = 1
		}
	}

	#Intervene
	option = {
		name = diplomacy_foreign_special.1025.b
		custom_tooltip = diplomacy_foreign_special.court_visit.disaster

		reverse_add_opinion = {
			target = scope:foreign_guest
			modifier = hate_opinion
			opinion = -30
		}
		hidden_effect = {	
			add_opinion = {
				target = scope:foreign_guest
				modifier = suspicion_opinion
				opinion = -30
			}
			add_opinion = {
				target = scope:interesting_relation
				modifier = suspicion_opinion
				opinion = -15
			}
		}

		hidden_effect = {
			diplomacy_foreign_special_1010_clean_up_court_visit_effect = yes
		}

		ai_chance = {
			base = 10
		}
	}
}

scripted_trigger diplomacy_foreign_special_1026_exisiting_sexual_relationship_with_root_trigger = {
	OR = {
		has_relation_lover = root
		is_consort_of = root
	}
}

#The guest is seducing
diplomacy_foreign_special.1026 = {
	type = character_event
	title = diplomacy_foreign_special.1026.t
	desc = diplomacy_foreign_special.1026.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = sitting_room
	}
	left_portrait = {
		character = scope:foreign_guest
		animation = flirtation
	}
	right_portrait = {
		character = scope:interesting_relation
		animation = happiness
	}
	
	trigger = {
		scope:private_dinner_outcome = flag:seduce
	}

	#Let it happen
	option = {
		name = diplomacy_foreign_special.1026.a

		diplomacy_foreign_special_1010_court_visit_interesting_relation_seduce_effect = yes
		custom_tooltip = diplomacy_foreign_special.1026.a.tt

		scope:foreign_guest = {
			random_secret = {
				limit = {
					secret_type = secret_lover
					secret_target = scope:interesting_relation
				}
				reveal_to_without_events_effect = { CHARACTER = root }
			}
		}

		hidden_effect = {
			add_character_flag = {
				days = 150
				flag = bad_court_visit_guest
			}
		}

		ai_chance = {
			base = 3
		}
	}

	#Intervene
	option = {
		name = diplomacy_foreign_special.1026.b
		custom_tooltip = diplomacy_foreign_special.court_visit.disaster

		reverse_add_opinion = {
			target = scope:foreign_guest
			modifier = hate_opinion
			opinion = -30
		}
		hidden_effect = {
			add_opinion = {
				target = scope:foreign_guest
				modifier = suspicion_opinion
				opinion = -30
			}
			add_opinion = {
				target = scope:interesting_relation
				modifier = suspicion_opinion
				opinion = -15
			}
		}

		hidden_effect = {
			diplomacy_foreign_special_1010_clean_up_court_visit_effect = yes
		}

		ai_chance = {
			base = 10
		}
	}

	#Join in
	option = {
		trigger = {
			OR = {
				is_ai = no
				sexually_liberal_trigger = yes
			}
		}
		name = diplomacy_foreign_special.1026.c

		random_list = { #They like it (spouse/concubine/lover)
			10 = {
				desc = diplomacy_foreign_special.1026.c.success
				trigger = {
					scope:interesting_relation = {
						diplomacy_foreign_special_1026_exisiting_sexual_relationship_with_root_trigger = yes
					}
				}
				modifier = {
					factor = 0.1
					scope:foreign_guest = {
						NOR = {
							sexually_liberal_trigger = yes
							is_attracted_to_gender_of = root
						}
					}
				}
				show_chance = no

				send_interface_toast = {
					title = diplomacy_foreign_special.1026.c.success
					left_icon = scope:foreign_guest
					left_icon = scope:interesting_relation

					custom_tooltip = diplomacy_foreign_special.court_visit.well
					diplomacy_foreign_special_1010_court_visit_join_seduction_successfully_effect = { CHARACTER = scope:foreign_guest } #Sets lover and opinions
					diplomacy_foreign_special_1010_court_visit_join_seduction_successfully_effect = { CHARACTER = scope:interesting_relation }  #Sets lover and opinions
					diplomacy_foreign_special_1010_court_visit_interesting_relation_seduce_effect = yes
				}
				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = 1
					}
					intercourse_with_character_effect = { INTERCOURSE_CHARACTER = scope:interesting_relation }
					intercourse_with_character_effect = { INTERCOURSE_CHARACTER = scope:foreign_guest }

				}

			}
			5 = { #They like it (parent/child)
				desc = diplomacy_foreign_special.1026.c.success
				show_chance = no
				trigger = {
					scope:interesting_relation = {
						diplomacy_foreign_special_1026_exisiting_sexual_relationship_with_root_trigger = no
					}
				}
				modifier = {
					factor = 0.1
					OR = {
						scope:foreign_guest = {
							NOR = {
								sexually_liberal_trigger = yes
								is_attracted_to_gender_of = root
							}
						}
						scope:interesting_relation = {
							NOR = {
								sexually_liberal_trigger = yes
								is_attracted_to_gender_of = root
							}
						}
					}
				}

				send_interface_toast = {
					title = diplomacy_foreign_special.1026.c.success
					left_icon = scope:foreign_guest
					left_icon = scope:interesting_relation

					custom_tooltip = diplomacy_foreign_special.court_visit.well
					diplomacy_foreign_special_1010_court_visit_join_seduction_successfully_effect = { CHARACTER = scope:foreign_guest }  #Sets lover and opinions
					diplomacy_foreign_special_1010_court_visit_join_seduction_successfully_effect = { CHARACTER = scope:interesting_relation }  #Sets lover and opinions
					diplomacy_foreign_special_1010_court_visit_interesting_relation_seduce_effect = yes
				}
				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = 1
					}
					intercourse_with_character_effect = { INTERCOURSE_CHARACTER = scope:interesting_relation }
					intercourse_with_character_effect = { INTERCOURSE_CHARACTER = scope:foreign_guest }
				}
			}
			3 = { #They are horrified
				desc = diplomacy_foreign_special.1026.c.failure
				show_chance = no


				send_interface_toast = {
					title = diplomacy_foreign_special.1026.c.failure
					left_icon = scope:foreign_guest
					left_icon = scope:interesting_relation

					custom_tooltip = diplomacy_foreign_special.court_visit.disaster
					reverse_add_opinion = {
						target = scope:foreign_guest
						modifier = disgusted_opinion
						opinion = -30
					}
					reverse_add_opinion = {
						target = scope:interesting_relation
						modifier = disgusted_opinion
						opinion = -30
					}
				}
				hidden_effect = {
					diplomacy_foreign_special_1010_clean_up_court_visit_effect = yes
				}
			}
		}

		ai_chance = {
			base = 10
		}
	}
}

#You are discovered as you spy
diplomacy_foreign_special.1027 = {
	type = character_event
	title = diplomacy_foreign_special.1027.t
	desc = diplomacy_foreign_special.1027.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = corridor_night
	}
	left_portrait = {
		character = root
		animation = fear
	}
	right_portrait = {
		character = scope:foreign_guest
		animation = anger
	}
	
	option = {
		name = diplomacy_foreign_special.1027.a
		custom_tooltip = diplomacy_foreign_special.court_visit.disaster

		reverse_add_opinion = {
			target = scope:foreign_guest
			modifier = suspicion_opinion
			opinion = -30
		}

		hidden_effect = {
			diplomacy_foreign_special_1010_clean_up_court_visit_effect = yes
		}
	}
}

scripted_trigger diplomacy_foreign_special_1028_can_trigger_trigger = {
	NOT = { has_character_flag = compliment }
	has_character_flag = is_in_diplomacy_foreign_special_event
	diplomacy_foreign_special_1017_ongoing_trigger = yes
}

#Court visit ongoing: compliment
diplomacy_foreign_special.1028 = {
	type = character_event
	title = diplomacy_foreign_special.1028.t
	desc = diplomacy_foreign_special.1028.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = garden
	}
	left_portrait = {
		character = scope:foreign_guest
		animation = happiness
	}

	trigger = {
		diplomacy_foreign_special_1028_can_trigger_trigger = yes
	}

	weight_multiplier = {
		base = 1
		modifier = {
			scope:foreign_guest = { can_receieve_good_available_compliment_trigger = yes }
			add = -0.8
		}
	}

	immediate = {
		hidden_effect = {
			add_character_flag = {
				flag = compliment
				days = 100
			}
			change_variable = {
				name = no_of_court_visit_events
				add = 1
			}
			#Randomize 4 compliment types
			randomize_available_compliment_effect = { COMPLIMENT_RECEIVER = scope:foreign_guest }
			randomize_available_compliment_effect = { COMPLIMENT_RECEIVER = scope:foreign_guest }
			randomize_available_compliment_effect = { COMPLIMENT_RECEIVER = scope:foreign_guest }
			randomize_good_available_compliment_effect = { COMPLIMENT_RECEIVER = scope:foreign_guest }
		}
	}

	#Compassionate
	option = {
		trigger = { has_character_flag = available_compliment_compassionate }
		name = diplomacy_foreign_special.1028.compassionate
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = compassionate COMPLIMENT_GIVER = root } }
		}
	}

	#bold
	option = {
		trigger = { has_character_flag = available_compliment_bold }
		name = diplomacy_foreign_special.1028.bold
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = bold COMPLIMENT_GIVER = root } }
		}
	}

	#honorable
	option = {
		trigger = { has_character_flag = available_compliment_honorable }
		name = diplomacy_foreign_special.1028.honorable
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = honorable COMPLIMENT_GIVER = root } }
		}
	}

	#greed
	option = {
		trigger = { has_character_flag = available_compliment_greedy }
		name = diplomacy_foreign_special.1028.greed
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = greed COMPLIMENT_GIVER = root } }
		}
	}

	#rational
	option = {
		trigger = { has_character_flag = available_compliment_rational }
		name = diplomacy_foreign_special.1028.rational
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = rational COMPLIMENT_GIVER = root } }
		}
	}

	#zealous
	option = {
		trigger = { has_character_flag = available_compliment_zealous }
		name = diplomacy_foreign_special.1028.zealous
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = zealous COMPLIMENT_GIVER = root } }
		}
	}

	#energetic
	option = {
		trigger = { has_character_flag = available_compliment_energetic }
		name = diplomacy_foreign_special.1028.energetic
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = energetic COMPLIMENT_GIVER = root } }
		}
	}

	#forgiving
	option = {
		trigger = { has_character_flag = available_compliment_forgiving }
		name = diplomacy_foreign_special.1028.forgiving
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = forgiving COMPLIMENT_GIVER = root } }
		}
	}

	#beautiful
	option = {
		trigger = { has_character_flag = available_compliment_beautiful }
		name = diplomacy_foreign_special.1028.beautiful
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = beautiful COMPLIMENT_GIVER = root } }
		}
	}

	#strong
	option = {
		trigger = { has_character_flag = available_compliment_strong }
		name = diplomacy_foreign_special.1028.strong
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = strong COMPLIMENT_GIVER = root } }
		}
	}

	#extroverted
	option = {
		trigger = { has_character_flag = available_compliment_extroverted }
		name = diplomacy_foreign_special.1028.extroverted
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = extroverted COMPLIMENT_GIVER = root } }
		}
	}

	#dominant
	option = {
		trigger = { has_character_flag = available_compliment_dominant }
		name = diplomacy_foreign_special.1028.dominant
		flavor = diplomacy_foreign_special.1028.compliment.tt

		hidden_effect = {
			scope:foreign_guest = { evaluate_compliment_effect = { COMPLIMENT_TYPE = dominant COMPLIMENT_GIVER = root } }
		}
	}

	after = {

		random_list = {
			1 = { # Good
				desc = diplomacy_foreign_special.1028.good
				modifier = {
					factor = 0
					exists = scope:compliment_outcome
					OR = {
						scope:compliment_outcome = flag:neutral
						scope:compliment_outcome = flag:bad
					}
				}
				show_chance = no
				send_interface_toast = {
					title = diplomacy_foreign_special.1028.good
					left_icon = scope:foreign_guest
					custom_tooltip = diplomacy_foreign_special.court_visit.well
				}
				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = 1
					}
				}
			}
			1 = { #Neutral
				desc = diplomacy_foreign_special.1028.neutral
				modifier = {
					factor = 0
					exists = scope:compliment_outcome
					OR = {
						scope:compliment_outcome = flag:good
						scope:compliment_outcome = flag:bad
					}
				}
				show_chance = no
				send_interface_toast = {
					title = diplomacy_foreign_special.1028.neutral
					left_icon = scope:foreign_guest
					custom_tooltip = diplomacy_foreign_special.court_visit.normal
				}
			}
			1 = { #Bad
				desc = diplomacy_foreign_special.1028.bad
				modifier = {
					factor = 0
					exists = scope:compliment_outcome
					OR = {
						scope:compliment_outcome = flag:neutral
						scope:compliment_outcome = flag:good
					}
				}
				show_chance = no
				send_interface_toast = {
					title = diplomacy_foreign_special.1028.bad
					left_icon = scope:foreign_guest
					custom_tooltip = diplomacy_foreign_special.court_visit.poorly
				}
				hidden_effect = {
					change_variable = {
						name = court_visit_points
						add = -1
					}
				}
			}
		}

		hidden_effect = {
			trigger_event = { on_action = diplomacy_foreign_special_1010_court_visit_events days = { 10 15 } }
			clear_available_compliments_effect = yes
		}
	}
}	

#Court visit Success
diplomacy_foreign_special.1030 = {
	type = character_event
	title = diplomacy_foreign_special.1030.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { has_character_flag = bad_court_visit_guest }
				desc = diplomacy_foreign_special.1030.bad_guest.desc
			}
			desc = diplomacy_foreign_special.1030.desc
		}
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:helper_spouse }
				desc = diplomacy_foreign_special.1030.spouse.desc
			}
			triggered_desc = {
				trigger = { NOT = { exists = scope:helper_spouse } }
				desc = diplomacy_foreign_special.1030.no_spouse.desc
			}
		}
	}
	theme = diplomacy_foreign_affairs_focus
	left_portrait = {
		character = scope:foreign_guest
		animation = happiness
	}
	
	trigger = {
		diplomacy_foreign_special_1017_success_trigger = yes
	}

	immediate = {
		add_character_flag = {
			flag = in_success_event
			days = 100
		}
	}

	option = {
		name = {
			trigger = { has_character_flag = bad_court_visit_guest }
			text = diplomacy_foreign_special.1030.a_bad_guest	
		}
		name = {
			trigger = {
				has_personality_extroverted_trigger = yes
				NOT = { has_character_flag = bad_court_visit_guest }
			}
			text = diplomacy_foreign_special.1030.a_extroverted
		}
		name = {
			trigger = {
				has_personality_introverted_trigger = yes
				NOT = { has_character_flag = bad_court_visit_guest }
			}
			text = diplomacy_foreign_special.1030.a_introverted
		}
		name = {
			trigger = {
				NOR = {
					has_personality_extroverted_trigger = yes
					has_personality_introverted_trigger = yes
					has_character_flag = bad_court_visit_guest
				}
			}
			text = diplomacy_foreign_special.1030.a
		}

		if = { #Great outcome
			limit = {
				diplomacy_foreign_special_1017_great_success_trigger = yes
				NOT = { has_character_flag = bad_court_visit_guest }
				scope:foreign_guest = { is_alive = yes }
			}

			# Mutually improved Opinion and additional chance for Friendship.
			diplomacy_foreign_special_1010_court_visit_outcome_great_effect = { CHARACTER = scope:foreign_guest }
		}
		else_if = { #Normal good outcome
			limit = {
				scope:foreign_guest = { is_alive = yes }
			}

			# Mutually improved Opinion
			diplomacy_foreign_special_1010_court_visit_outcome_good_effect = { CHARACTER = scope:foreign_guest }
		}

		hidden_effect = {
			diplomacy_foreign_special_1010_clean_up_court_visit_effect = yes
		}
	}
}

#Court Visit Failure
diplomacy_foreign_special.1031 = {
	type = character_event
	title = diplomacy_foreign_special.1031.t
	desc = diplomacy_foreign_special.1031.desc
	theme = diplomacy_foreign_affairs_focus
	left_portrait = {
		character = scope:foreign_guest
		animation = shame
	}

	immediate = {
		add_character_flag = {
			flag = in_failure_event
			days = 100
		}
		hidden_effect = {
			scope:foreign_guest = {
				random_dummy_gender_servant_effect = yes
			}
		}
	}
	
	trigger = {
		diplomacy_foreign_special_1017_failure_trigger = yes
	}

	option = {
		name = diplomacy_foreign_special.1031.a

		if = {
			limit = {
				scope:foreign_guest = {
					is_alive = yes
				}
			}

			# Reduced Opinion
			diplomacy_foreign_special_1010_court_visit_outcome_bad_effect = { CHARACTER = scope:foreign_guest }
		}

		hidden_effect = {
			diplomacy_foreign_special_1010_clean_up_court_visit_effect = yes
		}
	}
}

#############################
# Introduce Your Heirs		#
# 1100 - 1105				#
# by Flavio Verna 			#
#############################

scripted_trigger diplomacy_foreign_1100_valid_heir_trigger = {
	age > 10
	age <= 35
	basic_is_available_ai = yes
	exists = liege
	liege = $CHARACTER$
}

scripted_trigger diplomacy_foreign_1100_valid_neighbor_trigger = {
	basic_is_available_ai = yes
	opinion = {
		target = root
		value >= neutral_opinion
	}
	exists = player_heir
	NOR = {
		is_consort_of = root
		has_government = republic_government
		has_government = theocracy_government
		player_heir = root.player_heir
	}
	save_temporary_scope_as = neighbor_check
	player_heir = { # Must have a child available.
		diplomacy_foreign_1100_valid_heir_trigger = { CHARACTER = scope:neighbor_check }
		NOT = { has_important_relationship_trigger = { CHARACTER = root.player_heir } }
	}
}

# Letter sent by neighbor.
diplomacy_foreign.1100 = {
	type = letter_event
	opening = {
		desc = diplomacy_foreign.0100.opening
	}
	desc = diplomacy_foreign.0100.desc
	sender = scope:other_ruler

	trigger = {
		is_landed = yes
		highest_held_title_tier >= tier_duchy
		is_available = yes
		is_independent_ruler = yes
		is_at_war = no
		NOT = { has_government = republic_government }
		NOT = { has_character_flag = had_event_diplomacy_foreign_1100 }
		exists = player_heir
		player_heir = { # Must have a child available.
			diplomacy_foreign_1100_valid_heir_trigger = { CHARACTER = root }
		}
		any_neighboring_top_liege_realm_owner = {
			diplomacy_foreign_1100_valid_neighbor_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.50
			player_heir = {
				is_married = no
				is_adult = no
			}
		}
		modifier = {
			add = 0.25
			highest_held_title_tier >= tier_kingdom
		}
		upweight_for_focus_modifier = { FOCUS = diplomacy_foreign_affairs_focus }
	}

	immediate = {
		add_character_flag = {
			flag = had_event_diplomacy_foreign_1100
			years = 15
		}
		save_scope_as = main_ruler
		player_heir = { save_scope_as = main_heir }
		random_neighboring_top_liege_realm_owner = {
			limit = {
				OR = {
					has_relation_friend = root
					has_relation_lover = root
				}
				diplomacy_foreign_1100_valid_neighbor_trigger = yes
			}
			alternative_limit = {
				faith = root.faith
				diplomacy_foreign_1100_valid_neighbor_trigger = yes
			}
			alternative_limit = {
				faith.religion = root.faith.religion
				diplomacy_foreign_1100_valid_neighbor_trigger = yes
			}
			alternative_limit = {
				highest_held_title_tier >= root.highest_held_title_tier
				diplomacy_foreign_1100_valid_neighbor_trigger = yes
			}
			alternative_limit = {
				diplomacy_foreign_1100_valid_neighbor_trigger = yes
			}
			weight = {
				base = 30
				modifier = {
					add = -40
					has_relation_rival = root
				}
				opinion_modifier = {
					opinion_target = root
					min = -25
				}
				modifier = {
					exists = dynasty
					add = {
						value = dynasty.dynasty_prestige_level
						multiply = 5
					}
				}
				modifier = {
					add = 5
					root = { has_hook = prev }
				}
			}
			save_scope_as = other_ruler
			player_heir = {
				save_scope_as = other_heir
			}
		}
	}

	# Accept
	# Refuse

	option = { # Accept
		name = diplomacy_foreign.1100.a
		custom_tooltip = diplomacy_lifestyle_rank_up_indication
		scope:main_ruler = {
			trigger_event = { id = diplomacy_foreign.1101 days = 1 }
		}
		stress_impact = {
			shy = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			opinion_modifier = {
				opinion_target = scope:other_ruler
				multiplier = 1
			}
			opinion_modifier = {
				opinion_target = scope:other_heir
				multiplier = 1
			}
			modifier = {
				add = -5
				has_trait = paranoid
			}
			modifier = {
				add = -5
				has_trait = shy
			}
		}
	}
	option = { # Refuse
		name = diplomacy_foreign.1100.b

		add_opinion = {
			modifier = respect_opinion
			target = scope:other_ruler
			opinion = 5
		}
		reverse_add_opinion = {
			modifier = respect_opinion
			target = scope:other_ruler
			opinion = 5
		}
		stress_impact = {
			ambitious = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			modifier = {
				add = -15
				has_trait = ambitious
			}
		}
	}
}

# Who gets to be the host?
diplomacy_foreign.1101 = {
	type = character_event
	title = diplomacy_foreign.1100.t
	desc = diplomacy_foreign.1101.desc
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:main_heir
	lower_left_portrait = scope:main_ruler
	right_portrait = scope:other_heir
	lower_right_portrait = scope:other_ruler

	trigger = {
		scope:main_ruler = {
			is_alive = yes
		}
		scope:main_heir = {
			is_alive = yes
		}
		scope:other_ruler = {
			is_alive = yes
		}
		scope:other_heir = {
			is_alive = yes
		}
	}

	on_trigger_fail = {
		scope:main_ruler = {
			trigger_event = diplomacy_foreign.1105
		}
	}


	immediate = {
		capital_county = { save_scope_as = scoped_capital }
	}
	
	# Me
	option = {
		name = diplomacy_foreign.1101.a
		remove_short_term_gold = major_gold_value
		custom_tooltip = diplomacy_foreign.1101.a.tooltip
		save_scope_as = the_host
		save_scope_as = throne_room_scope
		set_variable = {
			name = diplomacy_foreign_1101_score
			value = 0
			days = 600
		}
		set_variable = {
			name = diplomacy_foreign_1102_heir_performance
			value = 0
			days = 600
		}
		hidden_effect = {
			scope:main_heir = {
				duel = {  # If heirs are left alone, will main_heir make a good impression?
					skill = diplomacy
					value = decent_skill_rating
					10 = {
							compare_modifier = {
								value = scope:duel_value
								multiplier = 1
							}
						modifier = {
							add = 5
							has_trait = gregarious
						}
						scope:main_ruler = {
							change_variable = {
								name = diplomacy_foreign_1102_heir_performance
								add = 2
							}
							random = {
								chance = 50
								#Trait similitarity to owner
								compatibility_modifier = {
									who = scope:main_heir
									compatibility_target = scope:other_heir
									min = -30
									max = 50
									multiplier = 3
								}
								change_variable = {
									name = diplomacy_foreign_1102_heir_performance
									add = 1
								}
							}
						}
					}
					8 = {
						modifier = {
							add = 5
							has_trait = shy
						}
						modifier = {
							add = 5
							is_adult = no
						}
						scope:main_ruler = {
							change_variable = {
								name = diplomacy_foreign_1102_heir_performance
								add = 1
							}
						}
					}
				}
			}
		}
		trigger_event = {
			id = diplomacy_foreign.1102
			days = { 3 5 }
		}
		ai_chance = {
			base = 20
			modifier = {
				add = 30
				gold >= major_gold_value
			}
			modifier = {
				add = -10
				has_trait = generous
			}
		}
	}

	# The other guy
	option = {
		name = diplomacy_foreign.1101.b
		custom_tooltip = diplomacy_foreign.1101.b.tooltip
		dynasty = { add_dynasty_prestige = minor_dynasty_prestige_gain }
		scope:other_ruler = {
			save_scope_as = the_host
			save_scope_as = throne_room_scope
		}
		set_variable = {
			name = diplomacy_foreign_1101_score
			value = 1
			days = 600
		}
		trigger_event = {
			id = diplomacy_foreign.1104
			days = { 7 19 }
		}
		ai_chance = {
			base = 20
		}
	}
}

scripted_trigger diplomacy_foreign_1102_valid_heir_trigger = {
	is_healthy = yes
	is_ai = yes
	is_imprisoned = no
}

# The other heir comes to meet yours in your court. What to do?
diplomacy_foreign.1102 = {
	type = character_event
	title = diplomacy_foreign.1100.t
	desc = diplomacy_foreign.1102.desc
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = throne_room_scope
	}
	left_portrait = scope:main_heir
	lower_left_portrait = scope:main_ruler
	right_portrait = scope:other_heir
	lower_right_portrait = scope:other_ruler

	trigger = {
		scope:main_ruler = {
			is_ruler = yes
		}
		scope:main_heir = {
			diplomacy_foreign_1102_valid_heir_trigger = yes
			this = scope:main_ruler.player_heir
		}
		scope:other_ruler = {
			is_ruler = yes
		}
		scope:other_heir = {
			diplomacy_foreign_1102_valid_heir_trigger = yes
			this = scope:other_ruler.player_heir
		}
	}
	on_trigger_fail = {
		scope:main_ruler = {
			trigger_event = diplomacy_foreign.1105
		}
	}

	# Diplomacy duel
	# Spend more money
	# Let your heir do his thing
	
	# Diplomacy duel
	option = {
		name = diplomacy_foreign.1102.a
		duel = {
			skill = diplomacy
			value = decent_skill_rating
			10 = {
				desc = diplomacy_foreign.1102.a.duel_success
					compare_modifier = {
						value = scope:duel_value
						multiplier = 1
					}
				modifier = {
					add = 5
					has_trait = gregarious
				}
				send_interface_toast = {
					left_icon = scope:main_heir
					right_icon = scope:other_heir
					title = tooltip.diplomacy_foreign.1102.a.duel_success
					add_prestige = minor_prestige_gain
					add_diplomacy_lifestyle_xp = medium_lifestyle_xp
					custom_tooltip = diplomacy_foreign.1101.court_visit.very_well
					add_opinion = {
						target = scope:other_heir
						modifier = impressed_opinion
						opinion = 10
					}
				}
				change_variable = {
					name = diplomacy_foreign_1101_score
					add = 4
				}
			}
			5 = {
				desc = diplomacy_foreign.1102.a.duel_failure
				modifier = {
					add = 5
					has_trait = shy
				}
				send_interface_toast = {
					left_icon = scope:main_heir
					right_icon = scope:other_heir
					title = tooltip.diplomacy_foreign.1102.a.duel_failure
					add_prestige = minor_prestige_loss
					add_opinion = {
						target = scope:other_heir
						modifier = bored_opinion
						opinion = -10
					}
					custom_tooltip = diplomacy_foreign.1101.court_visit.poorly
				}
			}
		}
		stress_impact = {
			shy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			modifier = {
				add = 30
				diplomacy >= high_skill_rating
			}
			modifier = {
				add = -10
				has_trait = shy
			}
		}
	}

	# Give present
	option = {
		name = diplomacy_foreign.1102.b
		custom_tooltip = diplomacy_foreign.1101.court_visit.well
		pay_short_term_gold = {
			target = scope:other_heir
			gold = medium_gold_value
		}
		add_diplomacy_lifestyle_xp = minor_lifestyle_xp
		change_variable = {
			name = diplomacy_foreign_1101_score
			add = 2
		}
		stress_impact = {
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			modifier = {
				add = -10
				has_trait = greedy
			}
			modifier = {
				add = 10
				has_trait = generous
			}
		}
	}
	# Let heir handle himself
	option = {
		name = diplomacy_foreign.1102.c
		trigger = {
			var:diplomacy_foreign_1102_heir_performance = 3
		}
		scope:main_heir = { add_prestige = medium_prestige_gain }
		change_variable = {
			name = diplomacy_foreign_1101_score
			add = 4
		}
		ai_chance = {
			base = 20
		}
	}
	after = {
		trigger_event = { # Outcome
			id = diplomacy_foreign.1103
			days = 6
		}
	}
}

# Outcome of the other heir visiting yours.
diplomacy_foreign.1103 = {
	type = character_event
	title = diplomacy_foreign.1100.t
	desc = {
		desc = diplomacy_foreign.1103.start.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_culture_conversion }
				desc = diplomacy_foreign.1103.culture_conversion.desc
			}
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_faith_conversion }
				desc = diplomacy_foreign.1103.faith_conversion.desc
			}
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_present }
				desc = diplomacy_foreign.1103.present.desc
			}
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_prestige }
				desc = diplomacy_foreign.1103.prestige.desc
			}
			desc = diplomacy_foreign.1103.empty.desc
		}
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_friendship }
				desc = diplomacy_foreign.1103.friendship.desc
			}
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_love }
				desc = diplomacy_foreign.1103.love.desc
			}
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_rivalry }
				desc = diplomacy_foreign.1103.rivalry.desc
			}
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_opinion_gain }
				desc = diplomacy_foreign.1103.opinion_gain.desc
			}
			triggered_desc = {
				trigger = { exists = scope:diplomacy_foreign_1103_opinion_loss }
				desc = diplomacy_foreign.1103.opinion_loss.desc
			}
		}
		first_valid = {
			triggered_desc = { #Still learned something, despite bad outcome.
				trigger = {
					var:diplomacy_foreign_1101_score >= 2
					OR = {
						exists = scope:diplomacy_foreign_1103_rivalry
						exists = scope:diplomacy_foreign_1103_opinion_loss
					}
				}
				desc = diplomacy_foreign.1103.tainted_exp.desc
			}
			triggered_desc = { # Learned from good outcome.
				trigger = {
					var:diplomacy_foreign_1101_score >= 2
					NOR = {
						exists = scope:diplomacy_foreign_1103_rivalry
						exists = scope:diplomacy_foreign_1103_opinion_loss
					}
				}
				desc = diplomacy_foreign.1103.good_exp.desc
			}
			desc = diplomacy_foreign.1103.no_exp.desc # Bad performance overall.
		}
	}
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = throne_room_scope
	}
	left_portrait = {
		character = scope:main_heir
		triggered_animation = {
			trigger = { exists = scope:diplomacy_foreign_1103_love }
			animation = adoration
		}
		triggered_animation = {
			trigger = {
				OR = {
					exists = scope:diplomacy_foreign_1103_friendship
					exists = scope:diplomacy_foreign_1103_opinion_gain
				}
			}
			animation = personality_compassionate
		}
		animation = disapproval
	}
	lower_left_portrait = scope:main_ruler
	right_portrait = {
		character = scope:other_heir
		triggered_animation = {
			trigger = { exists = scope:diplomacy_foreign_1103_love }
			animation = love
		}
		triggered_animation = {
			trigger = {
				OR = {
					exists = scope:diplomacy_foreign_1103_friendship
					exists = scope:diplomacy_foreign_1103_opinion_gain
				}
			}
			animation = happiness
		}
		animation = anger
	}
	lower_right_portrait = scope:other_ruler

	trigger = {
		scope:main_ruler = {
			is_landed = yes
			is_at_war = no
			scope:main_heir = {
				diplomacy_foreign_1102_valid_heir_trigger = yes
				this = scope:main_ruler.player_heir
			}
		}
		scope:other_ruler = {
			is_landed = yes
			is_at_war = no
			scope:other_heir = {
				diplomacy_foreign_1102_valid_heir_trigger = yes
				this = scope:other_ruler.player_heir
			}
		}
	}
	on_trigger_fail = {
		scope:main_ruler = {
			trigger_event = diplomacy_foreign.1105
		}
	}


	immediate = {
		hidden_effect = {
			# Chance of other heir to convert to host's culture or faith, giving gift to the host, or celebrating the host's dynasty.
			if = {
				limit = {
					var:diplomacy_foreign_1101_score >= 3
				}
				random_list = {
					15 = {
						trigger = {
							scope:main_ruler.culture = scope:main_heir.culture # Consistency, please...
							NOT = {
								scope:main_ruler.culture = scope:other_heir.culture
							}
						}
						save_scope_value_as = {
							name = diplomacy_foreign_1103_culture_conversion
							value = yes
						}
						scope:other_ruler = {
							send_interface_toast = { # Send warning toast, just in case a player took over the other ruler in the meantime.
								left_icon = scope:main_heir
								right_icon = scope:other_heir
								title = tooltip.diplomacy_foreign.1103.a.culture_conversion_success
								show_as_tooltip = {
									scope:other_heir = {
										set_culture = scope:main_ruler.culture
									}
								}
							}
						}
					}
					15 = {
						trigger = {
							scope:main_ruler.faith = scope:main_heir.faith # Consistency, please...
							NOT = {
								scope:main_ruler.faith = scope:other_heir.faith
							}
						}
						save_scope_value_as = {
							name = diplomacy_foreign_1103_faith_conversion
							value = yes
						}
						scope:other_ruler = {
							send_interface_toast = { # Send warning toast, just in case a player took over the other ruler in the meantime.
								left_icon = scope:main_heir
								right_icon = scope:other_heir
								title = tooltip.diplomacy_foreign.1103.a.faith_conversion_success
								show_as_tooltip = {
									scope:other_heir = {
										set_character_faith = scope:main_ruler.faith
									}
								}
							}
						}
					}
					10 = {
						save_scope_value_as = {
							name = diplomacy_foreign_1103_present
							value = yes
						}
					}
					5 = {
						save_scope_value_as = {
							name = diplomacy_foreign_1103_prestige
							value = yes
						}
					}
				}
			}
			# Chance of the heirs becoming friends, lovers, rivals, increasing or decreasing their opinion of each other.
			random_list = {
				25 = { # Friendship
					trigger = {
						scope:main_heir = {
							NOR = {
								has_relation_friend = scope:other_heir
								has_relation_lover = scope:other_heir
								has_relation_rival = scope:other_heir
							}
						}
						var:diplomacy_foreign_1101_score >= 2
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score = 3
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score >= 4
					}
					save_scope_value_as = {
						name = diplomacy_foreign_1103_friendship
						value = yes
					}
				}
				5 = { # Love
					trigger = {
						always = no
						scope:main_heir = {
							can_set_relation_lover_trigger = { CHARACTER = scope:other_heir }
							NOR = {
								has_relation_friend = scope:other_heir
								relation_with_character_is_incestuous_in_faith_trigger = { CHARACTER = scope:other_heir FAITH = scope:main_ruler.faith }
								relation_with_character_is_incestuous_in_faith_trigger = { CHARACTER = scope:other_heir FAITH = scope:other_ruler.faith }
	  						}
						}
						#var:diplomacy_foreign_1101_score >= 2
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score = 3
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score >= 4
					}
					save_scope_value_as = {
						name = diplomacy_foreign_1103_love
						value = yes
					}
				}
				3 = { # Rivalry
					trigger = {
					always = no
						scope:main_heir = {
							NOR = {
								has_relation_friend = scope:other_heir
								has_relation_lover = scope:other_heir
								has_relation_rival = scope:other_heir
							}
						}
						var:diplomacy_foreign_1101_score <= 2
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score = 1
					}
					save_scope_value_as = {
						name = diplomacy_foreign_1103_rivalry
						value = yes
					}
				}
				10 = { # Opinion increase
					trigger = {
					always = no
						var:diplomacy_foreign_1101_score >= 2
					}
					save_scope_value_as = {
						name = diplomacy_foreign_1103_opinion_gain
						value = yes
					}
				}
				3 = { # Opinion decrease
					trigger = {
					always = no
						var:diplomacy_foreign_1101_score <= 1
					}
					save_scope_value_as = {
						name = diplomacy_foreign_1103_opinion_loss
						value = yes
					}
				}
			}
		}
	}
	
	option = { # If lovers, and unmarried, make arrangements.
		name = diplomacy_foreign.1103.a
		trigger = {
			exists = scope:diplomacy_foreign_1103_love
			scope:main_heir = { is_adult = yes }
			scope:other_heir = { is_adult = yes }
			scope:main_heir = { can_marry_character_trigger = { CHARACTER = scope:other_heir } }
		}
		if = {
			limit = { scope:main_heir = { is_female = no } }
			scope:main_heir = { marry = scope:other_heir }
		}
		if = {
			limit = { scope:main_heir = { is_female = yes } }
			scope:main_heir = { marry_matrilineal_if_possible_PREV = { CHARACTER = scope:other_heir } }
		}
		ai_chance = {
			base = 50
		}
	}
	
	option = { # Ending.
		name = diplomacy_foreign.1103.b
		ai_chance = {
			base = 40
		}
	}

	after = {
		# Apply outcome effects.
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_culture_conversion
			}
			scope:other_heir = {
				set_culture = scope:main_ruler.culture
			}
		}
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_faith_conversion
			}
			scope:other_heir = {
				set_character_faith = scope:main_ruler.faith
			}
		}
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_present
			}
			hidden_effect = {
			scope:other_heir = {
					add_gold = major_gold_value
				}
			}
			scope:other_heir = {
				pay_short_term_gold = {
					target = scope:main_ruler
					gold = major_gold_value
				}
			}
		}
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_prestige
				scope:other_ruler = { highest_held_title_tier = tier_empire }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = massive_dynasty_prestige_gain
			}
		}
		else_if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_prestige
				scope:other_ruler = { highest_held_title_tier = tier_kingdom }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = major_dynasty_prestige_gain
			}
		}
		else_if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_prestige
				scope:other_ruler = { highest_held_title_tier = tier_duchy }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = medium_dynasty_prestige_gain
			}
		}
		else_if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_prestige
				scope:other_ruler = { highest_held_title_tier < tier_duchy }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = minor_dynasty_prestige_gain
			}
		}
		# Relation effects
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_friendship
			}
			scope:main_heir = {
				set_relation_friend = scope:other_heir
			}
		}
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_love
			}
			scope:main_heir = {
				set_relation_lover = scope:other_heir
			}
		}
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_rivalry
			}
			scope:main_heir = {
				set_relation_rival = scope:other_heir
			}
		}
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_opinion_gain
			}
			scope:main_heir = {
				add_opinion = {
					target = scope:other_heir
					modifier = courtly_friends_opinion
					opinion = 15
				}
			}
		}
		if = {
			limit = {
				exists = scope:diplomacy_foreign_1103_opinion_loss
			}
			scope:main_heir = {
				add_opinion = {
					target = scope:other_heir
					modifier = forced_friendship_opinion
					opinion = -15
				}
			}
		}
		# Add experience or perk based on performance.
		if = {
			limit = { var:diplomacy_foreign_1101_score >= 4 }
			add_diplomacy_lifestyle_perk_points = 1
		}
		else_if = {
			limit = { var:diplomacy_foreign_1101_score = 3 }
			add_diplomacy_lifestyle_xp = major_lifestyle_experience
		}
		else_if = {
			limit = { var:diplomacy_foreign_1101_score = 2 }
			add_diplomacy_lifestyle_xp = minor_lifestyle_experience
		}
		else_if = {
			limit = { var:diplomacy_foreign_1101_score = 1 }
			add_diplomacy_lifestyle_xp = miniscule_lifestyle_experience
		}
		# Clear all variables
		remove_variable = diplomacy_foreign_1101_score
		remove_variable = diplomacy_foreign_1102_heir_performance
	}
}


# Outcome of the main heir visiting other.
diplomacy_foreign.1104 = {
	type = character_event
	title = diplomacy_foreign.1100.t
	desc = {
		desc = diplomacy_foreign.1104.start.desc
		first_valid = {
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_culture_conversion }
				desc = diplomacy_foreign.1104.culture_conversion.desc
			}
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_faith_conversion }
				desc = diplomacy_foreign.1104.faith_conversion.desc
			}
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_present }
				desc = diplomacy_foreign.1104.present.desc
			}
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_prestige }
				desc = diplomacy_foreign.1104.prestige.desc
			}
			desc = diplomacy_foreign.1103.empty.desc
		}
		first_valid = {
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_friendship }
				desc = diplomacy_foreign.1103.friendship.desc
			}
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_love }
				desc = diplomacy_foreign.1103.love.desc
			}
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_rivalry }
				desc = diplomacy_foreign.1103.rivalry.desc
			}
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_opinion_gain }
				desc = diplomacy_foreign.1103.opinion_gain.desc
			}
			triggered_desc = {
				trigger = { has_character_flag = flag_diplomacy_foreign_1104_opinion_loss }
				desc = diplomacy_foreign.1103.opinion_loss.desc
			}
		}
		first_valid = {
			triggered_desc = { #Still learned something, despite bad outcome.
				trigger = {
					var:diplomacy_foreign_1101_score >= 2
					OR = {
						has_character_flag = flag_diplomacy_foreign_1104_rivalry
						has_character_flag = flag_diplomacy_foreign_1104_opinion_loss
					}
				}
				desc = diplomacy_foreign.1103.tainted_exp.desc
			}
			triggered_desc = { # Learned from good outcome.
				trigger = {
					var:diplomacy_foreign_1101_score >= 2
					NOR = {
						has_character_flag = flag_diplomacy_foreign_1104_rivalry
						has_character_flag = flag_diplomacy_foreign_1104_opinion_loss
					}
				}
				desc = diplomacy_foreign.1103.good_exp.desc
			}
			desc = diplomacy_foreign.1103.no_exp.desc # Bad performance overall.
		}
	}
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = throne_room_scope
	}
	left_portrait = {
		character = scope:main_heir
		triggered_animation = {
			trigger = { has_character_flag = flag_diplomacy_foreign_1104_love }
			animation = adoration
		}
		triggered_animation = {
			trigger = {
				OR = {
					has_character_flag = flag_diplomacy_foreign_1104_friendship
					has_character_flag = flag_diplomacy_foreign_1104_opinion_gain
				}
			}
			animation = personality_compassionate
		}
		animation = disapproval
	}
	lower_left_portrait = scope:main_ruler
	right_portrait = {
		character = scope:other_heir
		triggered_animation = {
			trigger = { has_character_flag = flag_diplomacy_foreign_1104_love }
			animation = love
		}
		triggered_animation = {
			trigger = {
				OR = {
					has_character_flag = flag_diplomacy_foreign_1104_friendship
					has_character_flag = flag_diplomacy_foreign_1104_opinion_gain
				}
			}
			animation = happiness
		}
		animation = anger
	}
	lower_right_portrait = scope:other_ruler

	trigger = {
		scope:main_ruler = {
			is_landed = yes
			scope:main_heir = {
				diplomacy_foreign_1102_valid_heir_trigger = yes
				this = scope:main_ruler.player_heir
			}
		}
		scope:other_ruler = {
			is_landed = yes
			scope:other_heir = {
				diplomacy_foreign_1102_valid_heir_trigger = yes
				this = scope:other_ruler.player_heir
			}
		}
	}

	on_trigger_fail = {
		scope:main_ruler = {
			trigger_event = diplomacy_foreign.1105
		}
	}

	immediate = {
		hidden_effect = {
			scope:main_heir = {
				duel = {  # If heirs are left alone, will main_heir make a good impression?
					skill = diplomacy
					value = decent_skill_rating
					10 = {
							compare_modifier = {
								value = scope:duel_value
								multiplier = 1
							}
						modifier = {
							add = 5
							has_trait = gregarious
						}
						#Trait similitarity to owner
						compatibility_modifier = {
							who = scope:main_heir
							compatibility_target = scope:other_heir
							min = -50
							max = 50
							multiplier = 3
						}
						scope:main_ruler = {
							change_variable = {
								name = diplomacy_foreign_1101_score
								add = 2
							}
						}
					}
					8 = {
						modifier = {
							add = 5
							has_trait = shy
						}
						modifier = {
							add = 5
							is_adult = no
						}
					}
				}
			}

			# Chance of other heir to convert to host's culture or faith, giving gift to the host, or celebrating the host's dynasty.
			if = {
				limit = {
					var:diplomacy_foreign_1101_score >= 3
				}
				random_list = {
					15 = {
						trigger = {
							scope:main_ruler.culture = scope:main_heir.culture # Consistency, please...
							NOT = {
								scope:main_ruler.culture = scope:other_heir.culture
							}
						}
						add_character_flag = flag_diplomacy_foreign_1104_culture_conversion
					}
					15 = {
						trigger = {
							scope:main_ruler.faith = scope:main_heir.faith # Consistency, please...
							NOT = {
								scope:main_ruler.faith = scope:other_heir.faith
							}
						}
						add_character_flag = flag_diplomacy_foreign_1104_faith_conversion
					}
					10 = {
						add_character_flag = flag_diplomacy_foreign_1104_present
					}
					5 = {
						add_character_flag = flag_diplomacy_foreign_1104_prestige
					}
				}
			}
			# Chance of the heirs becoming friends, lovers, rivals, increasing or decreasing their opinion of each other.
			random_list = {
				25 = { # Friendship
					trigger = {
						scope:main_heir = {
							NOR = {
								has_relation_friend = scope:other_heir
								has_relation_lover = scope:other_heir
								has_relation_rival = scope:other_heir
							}
						}
						var:diplomacy_foreign_1101_score >= 2
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score = 3
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score >= 4
					}
					add_character_flag = flag_diplomacy_foreign_1104_friendship
				}
				5 = { # Love
					trigger = {
						matching_gender_and_sexuality_trigger = { CHARACTER_1 = scope:main_heir CHARACTER_2 = scope:other_heir }
						scope:main_heir = {
							attracted_to_opposite_gender_trigger = yes
							NOR = {
								has_relation_friend = scope:other_heir
								has_relation_lover = scope:other_heir
								has_relation_rival = scope:other_heir
							}
							NOR = {
								relation_with_character_is_incestuous_in_faith_trigger = { CHARACTER = scope:other_heir FAITH = scope:main_ruler.faith }
								relation_with_character_is_incestuous_in_faith_trigger = { CHARACTER = scope:other_heir FAITH = scope:other_ruler.faith }
	  						}
						}
						var:diplomacy_foreign_1101_score >= 2
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score = 3
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score >= 4
					}
					add_character_flag = flag_diplomacy_foreign_1104_love
				}
				3 = { # Rivalry
					trigger = {
						scope:main_heir = {
							NOR = {
								has_relation_friend = scope:other_heir
								has_relation_lover = scope:other_heir
								has_relation_rival = scope:other_heir
							}
						}
						var:diplomacy_foreign_1101_score <= 2
					}
					modifier = {
						add = 10
						var:diplomacy_foreign_1101_score = 1
					}
					add_character_flag = flag_diplomacy_foreign_1104_rivalry
				}
				10 = { # Opinion increase
					trigger = {
						var:diplomacy_foreign_1101_score >= 2
					}
					add_character_flag = flag_diplomacy_foreign_1104_opinion_gain
				}
				3 = { # Opinion decrease
					trigger = {
						var:diplomacy_foreign_1101_score <= 1
					}
					add_character_flag = flag_diplomacy_foreign_1104_opinion_loss
				}
			}
		}
	}

	
	option = { # If lovers, and unmarried, make arrangements.
		name = diplomacy_foreign.1103.a
		trigger = {
			diplomacy >= high_skill_rating
			has_character_flag = flag_diplomacy_foreign_1104_love
			scope:main_heir = { is_adult = yes }
			scope:other_heir = { is_adult = yes }
			scope:main_heir = { can_marry_character_trigger = { CHARACTER = scope:other_heir } }
		}
		skill = diplomacy
		if = {
			limit = { scope:main_heir = { is_female = no } }
			scope:main_heir = { marry = scope:other_heir }
		}
		if = {
			limit = { scope:main_heir = { is_female = yes } }
			scope:main_heir = { marry_matrilineal_if_possible_PREV = { CHARACTER = scope:other_heir } }
		}
		ai_chance = {
			base = 50
		}
	}

	
	option = { # Ending.
		name = diplomacy_foreign.1103.b
		ai_chance = {
			base = 40
		}
	}

	after = {
		# Apply outcome effects.
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_culture_conversion
			}
			scope:main_heir = {
				set_culture = scope:other_ruler.culture
			}
		}
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_faith_conversion
			}
			scope:main_heir = {
				set_character_faith = scope:other_ruler.faith
			}
		}
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_present
			}
			hidden_effect = {
			scope:other_heir = {
					add_gold = major_gold_value
				}
			}
			scope:other_heir = {
				pay_short_term_gold = {
					target = scope:main_ruler
					gold = major_gold_value
				}
			}
		}
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_prestige
				scope:other_ruler = { highest_held_title_tier = tier_empire }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = massive_dynasty_prestige_gain
			}
		}
		else_if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_prestige
				scope:other_ruler = { highest_held_title_tier = tier_kingdom }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = major_dynasty_prestige_gain
			}
		}
		else_if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_prestige
				scope:other_ruler = { highest_held_title_tier = tier_duchy }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = medium_dynasty_prestige_gain
			}
		}
		else_if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_prestige
				scope:other_ruler = { highest_held_title_tier < tier_duchy }
			}
			scope:main_heir.dynasty = {
				add_dynasty_prestige = minor_dynasty_prestige_gain
			}
		}
		# Relation effects
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_friendship
			}
			scope:main_heir = {
				set_relation_friend = scope:other_heir
			}
		}
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_love
			}
			scope:main_heir = {
				set_relation_lover = scope:other_heir
			}
		}
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_rivalry
			}
			scope:main_heir = {
				set_relation_rival = scope:other_heir
			}
		}
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_opinion_gain
			}
			scope:main_heir = {
				add_opinion = {
					target = scope:other_heir
					modifier = courtly_friends_opinion
					opinion = 15
				}
			}
		}
		if = {
			limit = {
				has_character_flag = flag_diplomacy_foreign_1104_opinion_loss
			}
			scope:main_heir = {
				add_opinion = {
					target = scope:other_heir
					modifier = forced_friendship_opinion
					opinion = -15
				}
			}
		}
		# Add experience or perk based on performance.
		if = {
			limit = { var:diplomacy_foreign_1101_score >= 4 }
			add_diplomacy_lifestyle_perk_points = 1
		}
		else_if = {
			limit = { var:diplomacy_foreign_1101_score = 3 }
			add_diplomacy_lifestyle_xp = major_lifestyle_experience
		}
		else_if = {
			limit = { var:diplomacy_foreign_1101_score = 2 }
			add_diplomacy_lifestyle_xp = minor_lifestyle_experience
		}
		else_if = {
			limit = { var:diplomacy_foreign_1101_score = 1 }
			add_diplomacy_lifestyle_xp = miniscule_lifestyle_experience
		}
		# Clear all flags and variables
		remove_character_flag = flag_diplomacy_foreign_1104_culture_conversion
		remove_character_flag = flag_diplomacy_foreign_1104_faith_conversion
		remove_character_flag = flag_diplomacy_foreign_1104_present
		remove_character_flag = flag_diplomacy_foreign_1104_prestige
		remove_character_flag = flag_diplomacy_foreign_1104_friendship
		remove_character_flag = flag_diplomacy_foreign_1104_love
		remove_character_flag = flag_diplomacy_foreign_1104_rivalry
		remove_character_flag = flag_diplomacy_foreign_1104_opinion_gain
		remove_character_flag = flag_diplomacy_foreign_1104_opinion_loss

		remove_variable = diplomacy_foreign_1101_score
		remove_variable = diplomacy_foreign_1102_heir_performance
	}
}

# Fallback in case of trigger_fail.
diplomacy_foreign.1105 = {
	type = character_event
	title = diplomacy_foreign.1100.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:other_ruler = { is_alive = no } }
				desc = diplomacy_foreign.1105.hostdead.desc
			}
			triggered_desc = {
				trigger = { scope:other_heir = { is_alive = no } }
				desc = diplomacy_foreign.1105.guestdead.desc
			}
			desc = diplomacy_foreign.1105.fallback.desc
		}
	}
	theme = diplomacy_foreign_affairs_focus
	override_background = {
		event_background = throne_room_scope
	}
	left_portrait = scope:main_heir
	lower_left_portrait = scope:main_ruler
	right_portrait = scope:other_heir
	lower_right_portrait = scope:other_ruler

	immediate = {
		hidden_effect = {
			# Clear all flags and variables
			remove_character_flag = flag_diplomacy_foreign_1104_culture_conversion
			remove_character_flag = flag_diplomacy_foreign_1104_faith_conversion
			remove_character_flag = flag_diplomacy_foreign_1104_present
			remove_character_flag = flag_diplomacy_foreign_1104_prestige
			remove_character_flag = flag_diplomacy_foreign_1104_friendship
			remove_character_flag = flag_diplomacy_foreign_1104_love
			remove_character_flag = flag_diplomacy_foreign_1104_rivalry
			remove_character_flag = flag_diplomacy_foreign_1104_opinion_gain
			remove_character_flag = flag_diplomacy_foreign_1104_opinion_loss

			remove_variable = diplomacy_foreign_1101_score
			remove_variable = diplomacy_foreign_1102_heir_performance
		}
	}

	
	option = { # Ending.
		name = diplomacy_foreign.1105.a
		ai_chance = {
			base = 40
		}
	}
}


#############################
# Exchange De jure Counties	#
# 1150 - 1154				#
# by Flavio Verna 			#
#############################


scripted_trigger diplomacy_foreign_1150_preferred_disputed_counties = {
	tier = tier_county
	NOR = {
		this = root.capital_county
		holder = root
		holder = { target_is_liege_or_above = root }
	}
	any_neighboring_county = { # The county owned by the neighbor borders root's realm.
		OR = {
			holder = root
			holder = { target_is_liege_or_above = root }
		}
	}
	holder = {
		is_at_war = no
		OR = {
			is_independent_ruler = yes
			AND = {
				highest_held_title_tier = tier_county # Do not switch out Dukes and Kings for a County.
				NOT = {
					any_liege_or_above = {
						this = root
					}
				}
				is_ai = yes
				liege = { is_ai = yes }
				is_at_war = no
			}
		}
		faith = root.faith
		NOT = { has_relation_rival = root }
		any_neighboring_top_liege_realm_owner = {
			this = root
		}
		save_temporary_scope_as = potential_holder
		primary_title = {
			any_in_de_jure_hierarchy = {
				tier = tier_county
				OR = {
					holder = root
					holder = {
						is_ai = yes
						highest_held_title_tier = tier_county # Do not switch out Dukes and Kings for a County.
						is_vassal_of = root
					}
				}
				any_neighboring_county = { # The county owned by root borders the neighbor's realm.
					OR = {
						holder = scope:potential_holder
						holder = { target_is_liege_or_above = scope:potential_holder }
					}
				}
			}
		}
	}
}

scripted_trigger diplomacy_foreign_1150_valid_disputed_counties_trigger = {
	tier = tier_county
	NOR = {
		holder = root
		holder = { target_is_liege_or_above = root }
	}
	holder = {
		is_at_war = no
		OR = {
			is_independent_ruler = yes
			AND = {
				highest_held_title_tier = tier_county # Do not switch out Dukes and Kings for a County.
				NOT = {
					any_liege_or_above = {
						this = root
					}
				}
				is_ai = yes
				liege = { is_ai = yes }
				is_at_war = no
			}
		}
		faith = root.faith
		NOT = { has_relation_rival = root }
		any_neighboring_top_liege_realm_owner = {
			this = root
		}
		primary_title = {
			any_in_de_jure_hierarchy = {
				tier = tier_county
				OR = {
					holder = root
					holder = {
						is_ai = yes
						highest_held_title_tier = tier_county # Do not switch out Dukes and Kings for a County.
						is_vassal_of = root
					}
				}
			}
		}
	}
}

diplomacy_foreign.1150 = {
	type = character_event
	title = diplomacy_foreign.1150.t
	desc = diplomacy_foreign.1150.desc
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:ruler
	lower_left_portrait = scope:ruler_vassal
	lower_right_portrait = scope:neighbor_vassal
	right_portrait = scope:neighbor

	trigger = {
		is_landed = yes
		is_at_war = no
		is_available = yes
		is_independent_ruler = yes
		NOT = { has_character_flag = had_event_diplomacy_foreign_1150 }
		primary_title = {
			any_in_de_jure_hierarchy = {
				diplomacy_foreign_1150_preferred_disputed_counties = yes
			}
		}
	}

	weight_multiplier = {
		base = 1
		upweight_for_focus_modifier = { FOCUS = diplomacy_foreign_affairs_focus }
	}

	immediate = {
		hidden_effect = {
			add_character_flag = {
				flag = had_event_diplomacy_foreign_1150
				years = 15
			}
			save_scope_as = ruler
		}
		primary_title = {
			random_in_de_jure_hierarchy = {
				limit = { # Try to get counties that will result in neater borders overall.
					diplomacy_foreign_1150_preferred_disputed_counties = yes
				}
				alternative_limit = {
					diplomacy_foreign_1150_valid_disputed_counties_trigger = yes
				}
				save_scope_as = county # Should de jure be mine, but is de facto the neighbor's land.
				if = {
					limit = {
						holder = { is_independent_ruler = yes }
					}
					holder = { save_scope_as = neighbor }
				}
				else = {
					holder.top_liege = { save_scope_as = neighbor }
					holder = { save_scope_as = ruler_vassal }
				}
			}
		}
		scope:neighbor.primary_title = {
			random_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
					OR = {
						holder = root
						holder = {
							is_ai = yes
							target_is_liege_or_above = root
						}
					}
					any_neighboring_county = { # The county owned by root borders the neighbor's realm.
						OR = {
							holder = scope:neighbor
							holder = { target_is_liege_or_above = scope:neighbor }
						}
					}
				}
				alternative_limit = {
					tier = tier_county
					OR = {
						holder = root
						holder = {
							is_ai = yes
							target_is_liege_or_above = root
						}
					}
				}
				save_scope_as = neighbor_county # Should de jure be the neighbor's but is de facto mine.
				if = {
					limit = {
						NOT = { holder = root }
					}
					holder = { save_scope_as = neighbor_vassal }
				}
			}
		}
	}

	# Diplomacy duel
	# Pay gold to facilitate deal
	# Deny
	
	option = { # Diplomacy duel
		name = diplomacy_foreign.1150.a
		# Add if in the event of neighbor not being AI.
		if = {
			limit = {
				scope:neighbor = {
					is_ai = yes
				}
			}
			duel = {
				skill = diplomacy
				value = high_skill_rating
				40 = {
					desc = diplomacy_foreign.1160.a.success
					compare_modifier = {
						value = scope:duel_value
						multiplier = 3.5
						min = -30
					}
					modifier = {
						add = scope:neighbor_county.development_level
					}
					show_as_tooltip = {
						diplomacy_foreign_1150_successful_county_switch_effect = yes
					}
					trigger_event = diplomacy_foreign.1151 # Notification event
				}
				40 = {
					desc = diplomacy_foreign.1160.a.failure
					compare_modifier = {
						value = scope:duel_value
						multiplier = -3.5
						min = -30
					}
					modifier = {
						add = scope:county.development_level
					}
					show_as_tooltip = {
						diplomacy_foreign_1150_failed_county_switch_effect = yes
					}
					trigger_event = diplomacy_foreign.1152 # Notification event
				}
			}
		}
		else = { # The player will decide.
			scope:neighbor = { trigger_event = diplomacy_foreign.1153 } # Player gets the request.
			random_list = {
				50 = {
					show_chance = no
					desc = diplomacy_foreign.1160.a.success
					show_as_tooltip = {
						diplomacy_foreign_1150_successful_county_switch_effect = yes
					}
				}
				50 = {
					show_chance = no
					desc = diplomacy_foreign.1160.a.failure
					show_as_tooltip = {
						diplomacy_foreign_1150_failed_county_switch_effect = yes
					}
				}
			}
		}
		stress_impact = {
			gregarious = minor_stress_impact_loss
			shy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 40
		}
	}
	option = { # Gold payment for lesser ruler.
		name = diplomacy_foreign.1150.b
		trigger = {
			scope:neighbor = {
				is_ai = yes
			}
			scope:ruler = {
				gold >= major_gold_value_check
				highest_held_title_tier >= scope:neighbor.highest_held_title_tier
			}
		}
		pay_short_term_gold = {
			target = root
 			gold = root.major_gold_value
		}
		diplomacy_foreign_1150_successful_county_switch_effect = yes
		# Reduce control of both counties
		scope:county = {
			change_county_control = massive_county_control_loss
		}
		scope:neighbor_county = {
			change_county_control = major_county_control_loss
		}
		stress_impact = {
			greedy = major_stress_impact_gain
			just = major_stress_impact_gain
			content = major_stress_impact_gain
		}
		ai_chance = {
			base = 20
		}
	}
	option = { # I like my own county.
		name = diplomacy_foreign.1150.c
		ai_chance = {
			base = 10
		}
	}
}

# Successfully switched de jure counties.
diplomacy_foreign.1151 = {
	type = character_event
	title = diplomacy_foreign.1150.t
	desc = diplomacy_foreign.1151.desc
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:ruler
	lower_left_portrait = scope:ruler_vassal
	right_portrait = scope:neighbor
	lower_right_portrait = scope:neighbor_vassal

	immediate = {
		diplomacy_foreign_1150_successful_county_switch_effect = yes
	}

	option = {
		name = diplomacy_foreign.1151.a
	}
}

# Failed to switch de jure counties.
diplomacy_foreign.1152 = {
	type = character_event
	title = diplomacy_foreign.1150.t
	desc = diplomacy_foreign.1152.desc
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:ruler
	lower_left_portrait = scope:ruler_vassal
	right_portrait = scope:neighbor
	lower_right_portrait = scope:neighbor_vassal

	immediate = {
		# Add xp
		add_diplomacy_lifestyle_xp = miniscule_lifestyle_experience
		# Add negative opinion (both neighbor and possible vassal)
		scope:neighbor = {
			add_opinion = {
				modifier = respect_opinion
				target = scope:ruler
				opinion = -5
			}
		}
		if = {
			limit = {
				exists = scope:ruler_vassal
			}
			scope:ruler_vassal = {
				add_opinion = {
					modifier = respect_opinion
					target = scope:ruler
					opinion = -15
				}
			}
		}
		# Reduce control in your county (...sell-out)
		scope:neighbor_county = {
			change_county_control = major_county_control_loss
		}
	}

	option = {
		name = diplomacy_foreign.1152.a
	}
}

# Player neighbor gets to decide on event.
diplomacy_foreign.1153 = {
	type = character_event
	title = diplomacy_foreign.1150.t
	desc = diplomacy_foreign.1153.desc
	
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:ruler
	lower_left_portrait = scope:ruler_vassal
	right_portrait = scope:neighbor
	lower_right_portrait = scope:neighbor_vassal


	option = { # Accept proposal.
		name = diplomacy_foreign.1153.a
		show_as_tooltip = { diplomacy_foreign_1150_successful_county_switch_effect = yes }
		scope:ruler = { trigger_event = diplomacy_foreign.1151 }
		stress_impact = {
			generous = medium_stress_impact_loss
			just = medium_stress_impact_loss
			greedy = medium_stress_impact_gain
			arbitrary = medium_stress_impact_gain
		}
	}
	option = { #Refuse.
		name = diplomacy_foreign.1153.b
		show_as_tooltip = { diplomacy_foreign_1150_failed_county_switch_effect = yes }
		scope:ruler = { trigger_event = diplomacy_foreign.1152 }
		stress_impact = {
			greedy = minor_stress_impact_loss
			arbitrary = minor_stress_impact_loss
			generous = medium_stress_impact_gain
			just = medium_stress_impact_gain
		}
	}
}

#################################
# Gain Foreign Affairs Friend	#
# 1200							#
# by Flavio Verna 				#
#################################

scripted_trigger diplomacy_foreign_1200_valid_neighbor_trigger = {
	basic_is_available_ai = yes
	is_adult = yes
	NOR = {
		has_relation_friend = root
		has_relation_rival = root
		is_at_war_with = root
	}
	opinion = {
		target = root
		value < low_negative_opinion
	}

}

# Gain diplomacy helper
diplomacy_foreign.1200 = {
	type = character_event
	title = diplomacy_foreign.1200.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:diplomacy_friend = { is_consort_of = root } }
				desc = diplomacy_foreign.1200.spouse.desc
			}
			triggered_desc = {
				trigger = { scope:diplomacy_friend = { is_close_family_of = root } }
				desc = diplomacy_foreign.1200.relative.desc
			}
			desc = diplomacy_foreign.1200.fallback.desc
		}
	}
	
	theme = diplomacy_foreign_affairs_focus
	left_portrait = scope:diplomacy_friend
	right_portrait = scope:neighbor_ruler
	
	trigger = {
		NOT = { has_character_flag = had_diplomacy_foreign_1200_event }
		OR = {
			num_of_relation_friend = 0
			NOT = {
				any_relation = {
					type = friend
					lifestyle_friend_trigger = { CHARACTER = root FOCUS = diplomacy_foreign_affairs_focus }
				}
			}
		}
		
		any_neighboring_top_liege_realm = {
			holder = {
				diplomacy_foreign_1200_valid_neighbor_trigger = yes
			}
		}
	}

	weight_multiplier = {
		base = 1
		upweight_for_focus_modifier = { FOCUS = diplomacy_foreign_affairs_focus }
	}
	
	immediate = {
		add_character_flag = {
			flag = had_diplomacy_foreign_1200_event
			days = 1825
		}
		hidden_effect = {
			potential_lifestyle_friend_saving_effect = { FOCUS = diplomacy_foreign_affairs_focus SCOPE_NAME = diplomacy_friend }
			random_neighboring_top_liege_realm = {
				limit = {
					holder = {
						diplomacy_foreign_1200_valid_neighbor_trigger = yes
						faith = root.faith
					}
				}
				alternative_limit = {
					holder = {
						diplomacy_foreign_1200_valid_neighbor_trigger = yes
					}
				}
				holder = { save_scope_as = neighbor_ruler }
			}
		}
	}

	# Accept help with foreign affairs (bonus on relation with neighboring ruler)
	# Do it on your own (diplomacy duel, if successful xp bonus)
	# End work early (reduce stress, minor negative opinion)

	option = { # Accept help with foreign affairs (bonus on relation with neighboring ruler)
		name = {
			trigger = {
				scope:diplomacy_friend = {
					is_consort_of = root
				}
			}
			text = diplomacy_foreign.1200.a.a
		}
		name = {
			trigger = {
				scope:diplomacy_friend = {
					is_close_family_of = root
				}
			}
			text = diplomacy_foreign.1200.a.b
		}
		name = {
			trigger = {
				scope:diplomacy_friend = {
					NOR = {
						is_close_family_of = root
						is_consort_of = root
					}
				}
			}
			text = diplomacy_foreign.1200.a.c
		}
		set_relation_friend = scope:diplomacy_friend
		remove_character_flag = had_diplomacy_foreign_1200_event
		add_character_flag = had_diplomacy_foreign_1200_event # For life if accepted.
		reverse_add_opinion = {
			modifier = respect_opinion
			target = scope:neighbor_ruler
			opinion = 15
		}
		stress_impact = {
			shy = minor_stress_impact_gain
			paranoid = medium_stress_impact_gain
		}
	}
	option = { # Do it on your own (diplomacy duel, if successful xp bonus)
		name = diplomacy_foreign.1200.b
		duel = {
			skill = diplomacy
			value = 10
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -9
				}
				desc = diplomacy_foreign.1200.b.success
				send_interface_toast = {
					title = diplomacy_foreign.1200.b.success
					left_icon = scope:neighbor_ruler
					add_diplomacy_lifestyle_xp = medium_lifestyle_xp
					reverse_add_opinion = {
						modifier = respect_opinion
						target = scope:neighbor_ruler
						opinion = 15
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -39
				}
				desc = diplomacy_foreign.1200.b.failure
				send_interface_toast = {
					title = diplomacy_foreign.1200.b.failure
					left_icon = scope:neighbor_ruler
					reverse_add_opinion = {
						modifier = respect_opinion
						target = scope:neighbor_ruler
						opinion = -5
					}
				}
			}
		}
		stress_impact = {
			shy = medium_stress_impact_gain
			paranoid = medium_stress_impact_gain
		}
	}

	option = { # End work early (reduce stress, minor negative opinion)
		name = diplomacy_foreign.1200.c
		if = {
			limit = {
				scope:diplomacy_friend = {
					OR = {
						is_close_family_of = root
						is_consort_of = root
					}
				}
			}
			scope:diplomacy_friend = {
				add_opinion = {
					modifier = insulted_courtier
					target = root
				}
			}
		}
		stress_impact = {
			base = medium_stress_impact_loss
			diligent = minor_stress_impact_gain
		}
	}
}