﻿#Events related to the Spouse Councillor's diplomacy task

namespace = councillor_spouse_diplomacy


# Notifications events (1001-1999)
	#1001 - The opinion of a Close Kinsman in your realm improves
	#1002 - A rivalry between your children is ended
	#1003 - Your heir becomes friend with (another) one of your children
	#1004 - The diplomacy skill of your Chancellor increases
	#1005 - Spouse lose Shy
	#1006 - Spouse gains Gregarious

# Events (2001-4999)
	#2001 - Heir, or all children, become more interesting as marriage partners
	#2002 - Friendship between your heir and the heir of one of your powerful vassals
	#2003 - Reconcile with a rival that is present in your realm
	#2004 - Foreign claimant seeks audience at your court - opportunity to use their claim!



#######################
# NOTIFICATION EVENTS
# 1001 - 1999
# by Linnéa Thimrén
#######################

#The opinion of a Close Kinsman in your realm improves
councillor_spouse_diplomacy.1001 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		NOT = {
			has_character_flag = had_event_councillor_spouse_diplomacy_1001
		}
		OR = {
			any_close_or_extended_family_member = {
				this != scope:councillor
				target_is_liege_or_above = root
				#We need one of these to be true to match the triggers in immediate
				OR = {
					is_landed = yes
					is_adult = yes
				}
			}
			AND = {
				dynasty ?= {
					any_dynasty_member = {
						is_adult = yes
						this != scope:councillor
						target_is_liege_or_above = root
						OR = {
							is_of_major_interest_to_root_trigger = yes
							is_of_minor_interest_to_root_trigger = yes
						}
					}
				}
			}	
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_1
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_1001
			days = 1825
		}
		random_close_or_extended_family_member = {
			limit = {
				this != scope:councillor
				target_is_liege_or_above = root
				is_landed = yes
			}
			save_scope_as = close_kin
		}
		if = {
			limit = {
				NOT = { exists = scope:close_kin }
				dynasty ?= {
					any_dynasty_member = {
						is_adult = yes
						this != scope:councillor
						target_is_liege_or_above = root
						OR = {
							is_of_major_interest_to_root_trigger = yes
							is_of_minor_interest_to_root_trigger = yes
						}
					}
				}
			}
			dynasty = {
				random_dynasty_member = {
					limit = {
						is_adult = yes
						this != scope:councillor
						target_is_liege_or_above = root
						is_of_major_interest_to_root_trigger = yes
						is_landed = yes
					}
					alternative_limit = {
						is_adult = yes
						this != scope:councillor
						target_is_liege_or_above = root
						is_of_minor_interest_to_root_trigger = yes
						is_landed = yes
					}
					alternative_limit = {
						is_adult = yes
						this != scope:councillor
						target_is_liege_or_above = root
						OR = {
							is_of_major_interest_to_root_trigger = yes
							is_of_minor_interest_to_root_trigger = yes
						}
					}
					save_scope_as = close_kin
				}
			}
		}
		if = {
			limit = {
				NOT = { exists = scope:close_kin }
			}
			random_close_or_extended_family_member = {
				limit = {
					is_adult = yes
					this != scope:councillor
					target_is_liege_or_above = root
				}
				save_scope_as = close_kin
			}
		}
		if = {
			limit = { exists = scope:close_kin }
			send_interface_message = {
				type = msg_spouse_task_good
				title = councillor_spouse_diplomacy.1001.desc
				desc = {
					triggered_desc = {
						trigger = {
							scope:councillor = { diplomacy < average_skill_level }
						}
						desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
					}
					triggered_desc = {
						trigger = {
							scope:councillor = { diplomacy >= average_skill_level }
						}
						desc = spouse_task_diplomacy_good_skilled_notification_tooltip
					}
				}

				left_icon = scope:close_kin
				right_icon = scope:councillor
				
				scope:close_kin = {
					add_opinion = {
						target = root
						modifier = respect_opinion
						opinion = 20
					}
					hidden_effect = {
						add_opinion = {
							target = scope:councillor
							modifier = respect_opinion
							opinion = 20
						}
					}
				}
			}
		}
	}
}


#A rivalry between your children is ended
councillor_spouse_diplomacy.1002 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		spouse_diplomacy_tier_3_trigger = yes
		NOT = {
			has_character_flag = had_event_councillor_spouse_diplomacy_1002
		}
		any_child = {
			NOT = { this = root.cp:councillor_spouse }
			any_relation = {
				type = rival
				NOT = { this = root.cp:councillor_spouse }
				is_child_of = root
			}
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_3
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_1002
			days = 1825
		}
		random_child = {
			limit = {
				this != scope:councillor
				any_relation = {
					type = rival
					is_child_of = root
				}
			}
			save_scope_as = child_1
		}
		random_child = {
			limit = {
				this != scope:councillor
				any_relation = {
					type = rival
					this = scope:child_1
				}
			}
			save_scope_as = child_2
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1002.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:child_1
			right_icon = scope:child_2
			
			scope:child_1 = {
				remove_relation_rival = scope:child_2
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = respect_opinion
						opinion = 5
					}
				}
			}
			scope:child_2 = {
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = respect_opinion
						opinion = 5
					}
				}
			}
		}
	}
}



#Your heir becomes friend with (another) one of your children (the oldest one, i.e. the one most likely to be next in line)

scripted_trigger councillor_spouse_diplomacy_1003_child_trigger = {
	NOR = {
		this = root.cp:councillor_spouse
		is_player_heir_of = root
		has_relation_rival = root.player_heir
		has_relation_potential_rival = root.player_heir
		has_relation_friend = root.player_heir
	}
	top_liege = root.top_liege
	opinion = {
		target = root.player_heir
		value <= 10
	}
}

councillor_spouse_diplomacy.1003 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		OR = { # Expensive, so we restrict the AI use of this
			is_ai = no
			highest_held_title_tier > tier_county
		}
		spouse_diplomacy_tier_2_trigger = yes
		exists = player_heir
		NOT = {
			has_character_flag = had_event_councillor_spouse_diplomacy_1003
		}
		NOT = {
			cp:councillor_spouse = { is_player_heir_of = root }
		}
		any_child = {
			councillor_spouse_diplomacy_1003_child_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_2
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_1003
			days = 1825
		}
		player_heir = { save_scope_as = heir }
		random_child = {
			limit = {
				councillor_spouse_diplomacy_1003_child_trigger = yes
				save_temporary_scope_as = current_character
				root = {
					player_heir_position = {
						value = 1
						target = scope:current_character
					}
				}
			}
			alternative_limit = {
				councillor_spouse_diplomacy_1003_child_trigger = yes
				save_temporary_scope_as = current_character
				root = {
					player_heir_position = {
						value = 2
						target = scope:current_character
					}
				}
			}
			alternative_limit = {
				councillor_spouse_diplomacy_1003_child_trigger = yes
				save_temporary_scope_as = current_character
				root = {
					player_heir_position = {
						value = 3
						target = scope:current_character
					}
				}
			}
			alternative_limit = {
				councillor_spouse_diplomacy_1003_child_trigger = yes
				save_temporary_scope_as = current_character
				root = {
					player_heir_position = {
						value = 4
						target = scope:current_character
					}
				}
			}
			alternative_limit = {
				councillor_spouse_diplomacy_1003_child_trigger = yes
				save_temporary_scope_as = current_character
				root = {
					player_heir_position = {
						value = 5
						target = scope:current_character
					}
				}
			}
			alternative_limit = { councillor_spouse_diplomacy_1003_child_trigger = yes }
			save_scope_as = child_1
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1003.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:heir
			right_icon = scope:child_1
			
			scope:child_1 = {
				set_relation_friend = { reason = friend_cultivated_friendship target = scope:heir }
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = respect_opinion
						opinion = 5
					}
				}
			}
			scope:heir = {
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = respect_opinion
						opinion = 5
					}
				}
			}
		}
	}
}


#The diplomacy skill of your Chancellor increases
councillor_spouse_diplomacy.1004 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		NOT = {
			has_character_flag = had_event_councillor_spouse_diplomacy_1004
		}
		cp:councillor_spouse = {
			diplomacy > mediocre_skill_rating
		}

		any_diplomacy_councillor = {
			diplomacy <= mediocre_skill_rating
		}
	}

	weight_multiplier = {
		base = 0.75
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_1
		}
		modifier = {
			add = 0.25
			any_diplomacy_councillor = {
				diplomacy <= low_skill_rating
			}
		}
		modifier = {
			add = 0.25
			scope:councillor = {
				has_diplomacy_lifestyle_trait_trigger = yes
			}
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_1004
			days = 1825
		}
		random_diplomacy_councillor = {
			limit = {
				diplomacy <= low_skill_rating
			}
			alternative_limit = {
				diplomacy <= mediocre_skill_rating
			}
			save_scope_as = diplo_councillor
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1004.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:councillor
			right_icon = scope:diplo_councillor
			
			scope:diplo_councillor = {
				show_as_tooltip = {
					add_diplomacy_skill = 1
				}
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = respect_opinion
						opinion = 10
					}
				}
			}
		}
		scope:diplo_councillor = {
			send_interface_message = {
				type = msg_spouse_task_good
				title = councillor_spouse_diplomacy.1004.desc_diplo_councillor
				desc = {
					triggered_desc = {
						trigger = {
							scope:councillor = { diplomacy < average_skill_level }
						}
						desc = task_diplomacy_good_unskilled_notification_tooltip
					}
					triggered_desc = {
						trigger = {
							scope:councillor = { diplomacy >= average_skill_level }
						}
						desc = task_diplomacy_good_skilled_notification_tooltip
					}
				}
				
				left_icon = scope:councillor
				
				add_diplomacy_skill = 1
			}
		}
	}
}


#Spouse lose Shy
councillor_spouse_diplomacy.1005 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		spouse_diplomacy_tier_2_trigger = yes
		cp:councillor_spouse = {
			has_diplomacy_lifestyle_trait_trigger = yes
			has_trait = shy
			NOT = { has_trait = gregarious }
			number_of_personality_traits >= 3
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_2
		}
		modifier = {
			add = 0.5
			cp:councillor_spouse = {
				number_of_personality_traits = 4
			}
		}
		modifier = {
			add = 0.25
			cp:councillor_spouse = {
				has_diplomacy_lifestyle_trait_trigger = yes
			}
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1005.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}

			left_icon = scope:councillor
			scope:councillor = {
				remove_trait = shy
			}
		}
	}
}

#Spouse gains Gregarious
councillor_spouse_diplomacy.1006 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		spouse_diplomacy_tier_2_trigger = yes
		cp:councillor_spouse = {
			has_diplomacy_lifestyle_trait_trigger = yes
			NOR = {
				has_trait = shy
				has_trait = gregarious
			}
			number_of_personality_traits <= 3
		}
	}

	weight_multiplier = {
		base = 0.5
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_2
		}
		modifier = {
			add = 0.5
			cp:councillor_spouse = {
				number_of_personality_traits <= 2
			}
		}
		modifier = {
			add = 0.25
			cp:councillor_spouse = {
				has_diplomacy_lifestyle_trait_trigger = yes
			}
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1005.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:councillor
			
			scope:councillor = {
				add_trait = gregarious
			}
		}
	}
}


#Opinion boost for close family member
councillor_spouse_diplomacy.1007 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		OR = { # Expensive, so we restrict the AI use of this
			is_ai = no
			highest_held_title_tier > tier_county
		}
		NOT = { has_character_flag = had_event_councillor_spouse_diplomacy_1007 }
		any_close_or_extended_family_member = {
			NOT = { this = root.cp:councillor_spouse }
			has_any_bad_relationship_with_root_trigger = no
			opinion = {
				target = root
				value <= 50
			}
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_1
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_1007
			days = 1825
		}
		random_close_or_extended_family_member = {
			limit = {
				this != scope:councillor
				has_any_bad_relationship_with_root_trigger = no
				target_is_liege_or_above = root
				opinion = {
					target = root
					value <= 50
				}
			}
			alternative_limit = {
				this != scope:councillor
				has_any_bad_relationship_with_root_trigger = no
				opinion = {
					target = root
					value <= 50
				}
			}
			save_scope_as = close_kin
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1007.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:close_kin
			right_icon = scope:councillor

			scope:close_kin = {
				add_opinion = {
					target = root
					modifier = friendliness_opinion
					opinion = 20
				}
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = friendliness_opinion
						opinion = 20
					}
				}
			}
		}
	}
}


#Opinion boost for relevant dynasty member
councillor_spouse_diplomacy.1008 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		OR = { # Expensive, so we restrict the AI use of this
			is_ai = no
			highest_held_title_tier > tier_county
		}
		NOT = { has_character_flag = had_event_councillor_spouse_diplomacy_1008 }
		dynasty ?= {
			any_dynasty_member = {
				NOT = { this = root.cp:councillor_spouse }
				is_house_head = no
				has_any_bad_relationship_with_root_trigger = no
				opinion = {
					target = root
					value <= 50
				}
				OR = {
					is_of_major_interest_to_root_trigger = yes
					is_of_minor_interest_to_root_trigger = yes
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_1
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_1008
			days = 1825
		}
		dynasty = {
			random_dynasty_member = {
				limit = {
					this != scope:councillor
					is_house_head = no
					has_any_bad_relationship_with_root_trigger = no
					target_is_liege_or_above = root
					opinion = {
						target = root
						value <= 50
					}
					OR = {
						is_of_major_interest_to_root_trigger = yes
						is_of_minor_interest_to_root_trigger = yes
					}
				}
				alternative_limit = {
					this != scope:councillor
					is_house_head = no
					has_any_bad_relationship_with_root_trigger = no
					opinion = {
						target = root
						value <= 50
					}
					OR = {
						is_of_major_interest_to_root_trigger = yes
						is_of_minor_interest_to_root_trigger = yes
					}
				}
				save_scope_as = relevant_dynasty_member
			}
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1007.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:relevant_dynasty_member
			right_icon = scope:councillor
			
			scope:relevant_dynasty_member = {
				add_opinion = {
					target = root
					modifier = friendliness_opinion
					opinion = 20
				}
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = friendliness_opinion
						opinion = 20
					}
				}
			}
		}
	}
}


#Opinion boost for house head
councillor_spouse_diplomacy.1009 = {
	hidden = yes
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		NOT = { has_character_flag = had_event_councillor_spouse_diplomacy_1009 }
		has_dynasty = yes
		house = {
			house_head = {
				this != scope:councillor
				this != root
				has_any_bad_relationship_with_root_trigger = no
				opinion = {
					target = root
					value <= 50
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_1
		}
	}

	immediate = {
		cp:councillor_spouse = {
			save_scope_as = councillor
		}
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_1009
			days = 1825
		}
		house = {
			house_head = {
				save_scope_as = house_head
			}
		}
		send_interface_message = {
			type = msg_spouse_task_good
			title = councillor_spouse_diplomacy.1007.desc
			desc = {
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy < average_skill_level }
					}
					desc = spouse_task_diplomacy_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:councillor = { diplomacy >= average_skill_level }
					}
					desc = spouse_task_diplomacy_good_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:house_head
			right_icon = scope:councillor

			scope:house_head = {
				add_opinion = {
					target = root
					modifier = friendliness_opinion
					opinion = 20
				}
				hidden_effect = {
					add_opinion = {
						target = scope:councillor
						modifier = friendliness_opinion
						opinion = 20
					}
				}
			}
		}
	}
}


#######################
# EVENTS			
# 2001-4999
# by Linnéa Thimrén
#######################

#Your heir, or all your children, become more interesting as marriage partners
councillor_spouse_diplomacy.2001 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:child_heir = { is_female = yes }
				}
				desc = councillor_spouse_diplomacy.2001.t_female
			}
			desc = councillor_spouse_diplomacy.2001.t_male
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					any_child = {
						is_married = no
						liege = root
						NOT = { mother = { this = scope:councillor } }
					}
				}
				desc = councillor_spouse_diplomacy.2001.desc
			}
			desc = councillor_spouse_diplomacy.2001.desc_mother
		}
		desc = councillor_spouse_diplomacy.2001.desc_end
	}
	theme = diplomacy
	left_portrait = {
		character = scope:child_heir
		animation = boredom
	}
	right_portrait = {
		character = scope:councillor
		animation = scheme
	}
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		spouse_diplomacy_tier_2_trigger = yes
		NOT = {
			has_character_flag = had_event_councillor_spouse_diplomacy_2001
		}
		player_heir ?= {
			this != scope:councillor
			is_child_of = root
			is_married = no
			age >= 10
		}
		any_child = {
			this != scope:councillor
			NOT = { is_player_heir_of = root }
			is_married = no
			liege ?= root
			age >= 10
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_2
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_2001
			days = 3650
		}
		player_heir = {
			save_scope_as = child_heir
		}
		every_child = {
			limit = {
				this != scope:councillor
				is_married = no
				liege ?= root
				age >= 10
			}
			add_to_list = child_list
		}
		if = {
			limit = { root = { is_ai = no }	}
			scope:councillor = {
				assign_quirk_effect = yes
			}
		}
	}

	option = { #Much more likely for heir
		name = councillor_spouse_diplomacy.2001.a
		scope:child_heir = {
			add_character_modifier = {
				modifier = heir_easier_to_marry_off_modifier
				days = 1825
			}
		}
		ai_chance = {
			base = 75
		}
	}

	option = { #More likely for all children
		name = councillor_spouse_diplomacy.2001.b
		if = {
			limit = {
				any_in_list = {
					list = child_list
					count >= 3
				}
			}
			every_in_list = {
				list = child_list
				custom = councillor_spouse_diplomacy.2001.b.all_kids
				add_character_modifier = {
					modifier = child_easier_to_marry_off_modifier
					days = 1825
				}
			}
		}
		else = {
			every_in_list = {
				list = child_list
				add_character_modifier = {
					modifier = child_easier_to_marry_off_modifier
					days = 1825
				}
			}
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_compassion = 0.5
			}
		}
	}
	
	option = {
		name = councillor_spouse_diplomacy.2001.c
		add_character_modifier = {
			modifier = practiced_manners_modifier
			days = 1825
		}
		ai_chance = {
			base = 50
			modifier = {
				add = 25
				has_trait = arrogant
			}
		}
	}
}

#Friendship between your heir and the heir of one of your powerful vassals
scripted_trigger councillor_spouse_diplomacy_2002_child_trigger = {
	is_alive = yes
	is_adult = no
	NOR = {
		this = scope:councillor
		this = root
		is_primary_heir_of = root
		age < childhood_education_start_age
		is_ai = no
		is_child_of = root
		is_sibling_of = root.player_heir
		has_relation_friend = root.player_heir
		has_relation_lover = root.player_heir
	}
}

councillor_spouse_diplomacy.2002 = {
	type = character_event
	title = councillor_spouse_diplomacy.2002.t
	desc = councillor_spouse_diplomacy.2002.desc
	theme = diplomacy
	left_portrait = {
		character = scope:councillor
		animation = personality_content
	}
	right_portrait = {
		character = scope:child_heir
		animation = happiness
	}
	lower_left_portrait = scope:vassal_heir_1
	lower_center_portrait = scope:vassal_heir_2
	lower_right_portrait = scope:vassal_heir_3
	
	trigger = {
		is_in_civil_war = no
		valid_spouse_councillor_trigger = yes
		NOT = {
			has_character_flag = had_event_councillor_spouse_diplomacy_2002
		}
		player_heir ?= {
			this != scope:councillor
			is_child_of = root
			age < 16
		}
		any_powerful_vassal = {
			player_heir ?= {
				councillor_spouse_diplomacy_2002_child_trigger = yes
				save_temporary_scope_as = heir_temp
			}
		}
		OR = {
			any_powerful_vassal = {
				player_heir ?= {
					this != scope:heir_temp
					councillor_spouse_diplomacy_2002_child_trigger = yes
				}
			}
			liege ?= {
				this != root
				player_heir ?= {
					this != scope:heir_temp
					councillor_spouse_diplomacy_2002_child_trigger = yes
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_1
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_2002
			days = 3650
		}
		player_heir = {
			save_scope_as = child_heir
		}
		every_powerful_vassal = {
			player_heir ?= {
				if = {
					limit = {
						NOT = { is_in_list = vassal_heir_list }
						councillor_spouse_diplomacy_2002_child_trigger = yes
					}
					add_to_list = vassal_heir_list
				}
			}
		}
		liege ?= {
			if = {
				limit = {
					this != root
				}
				player_heir ?= {
					if = {
						limit = {
							NOT = { is_in_list = vassal_heir_list }
							councillor_spouse_diplomacy_2002_child_trigger = yes
						}
						add_to_list = vassal_heir_list
					}
				}
			}
		}
		random_in_list = {
			list = vassal_heir_list
			save_scope_as = vassal_heir_1
		}
		random_in_list = {
			list = vassal_heir_list
			limit = { this != scope:vassal_heir_1 }
			weight = {
				base = 1
				modifier = {
					add = 3 #more likely that it's your liege's kid if you're of a lower tier
					root.highest_held_title_tier <= tier_county
					is_child_of = root.liege
				}
			}
			save_scope_as = vassal_heir_2
		}
		#Are there more than 2? There doesn't have to be one, but if there is we save that scope as well and add one more option!
		random_in_list = {
			list = vassal_heir_list
			limit = {
				NOR = {
					this = scope:vassal_heir_1
					this = scope:vassal_heir_2
				}
			}
			save_scope_as = vassal_heir_3
		}
	}

	option = { #vassal_heir_1
		name = councillor_spouse_diplomacy.2002.a
		scope:child_heir = {
			set_relation_friend = { reason = friend_vassal_childhood target = scope:vassal_heir_1 }
			hidden_effect = {
				if = {
					limit = {
						possible_mutual_interest_trigger = {
							CHARACTER_1 = scope:child_heir
							CHARACTER_2 = scope:vassal_heir_1
						}
						OR = {
							AND = {
								is_adult = yes
								scope:vassal_heir_1 = { is_adult = yes }
							}
							AND = {
								is_adult = no
								OR = {
									has_sexuality = heterosexual
									has_sexuality = bisexual
									has_sexuality = homosexual
								}
								scope:vassal_heir_1 = {
									is_adult = no
									OR = {
										has_sexuality = heterosexual
										has_sexuality = bisexual
										has_sexuality = homosexual
									}
								}
							}
						}
					}
					random = {
						chance = 10
						modifier = {
							add = 5
							number_of_personality_traits_in_common = {
								target = scope:vassal_heir_1
								value >= 2
							}
						}
						opinion_modifier = {
							opinion_target = scope:vassal_heir_1
							multiplier = 0.25
						}
						if = {
							limit = {
								is_adult = yes
								can_set_relation_potential_lover_trigger = { CHARACTER = scope:vassal_heir_1 } #Unop: Ensure relation can be set
							}
							set_relation_potential_lover = scope:vassal_heir_1
						}
						else_if = { #The heir gets a crush if they're not an adult
							limit = { #Unop: Ensure they do not already have the relation
								NOT = { has_relation_crush = scope:vassal_heir_1 }
							}
 							set_relation_crush = scope:vassal_heir_1
 						}
					}
				}
			}
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:vassal_heir_1
				multiplier = 0.5
			}
		}
	}

	option = { #vassal_heir_2
		name = councillor_spouse_diplomacy.2002.b
		scope:child_heir = {
			set_relation_friend = { reason = friend_vassal_childhood target = scope:vassal_heir_2 }
			hidden_effect = {
				if = {
					limit = {
						possible_mutual_interest_trigger = {
							CHARACTER_1 = scope:child_heir
							CHARACTER_2 = scope:vassal_heir_2
						}
						OR = {
							AND = {
								is_adult = yes
								scope:vassal_heir_2 = { is_adult = yes }
							}
							AND = {
								is_adult = no
								OR = {
									has_sexuality = heterosexual
									has_sexuality = bisexual
									has_sexuality = homosexual
								}
								scope:vassal_heir_2 = {
									is_adult = no
									OR = {
										has_sexuality = heterosexual
										has_sexuality = bisexual
										has_sexuality = homosexual
									}
								}
							}
						}
					}
					random = {
						chance = 10
						modifier = {
							add = 5
							number_of_personality_traits_in_common = {
								target = scope:vassal_heir_2
								value >= 2
							}
						}
						opinion_modifier = {
							opinion_target = scope:vassal_heir_2
							multiplier = 0.25
						}
						if = {
							limit = {
								is_adult = yes
							}
							set_relation_potential_lover = scope:vassal_heir_2
						}
						else = { #The heir gets a crush if they're not an adult
							set_relation_crush = scope:vassal_heir_2
						}
					}
				}
			}
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:vassal_heir_2
				multiplier = 0.5
			}
		}
	}
	
	option = { #vassal_heir_3 (optional)
		name = councillor_spouse_diplomacy.2002.c
		trigger = {
			exists = scope:vassal_heir_3
		}
		scope:child_heir = {
			set_relation_friend = { reason = friend_vassal_childhood target = scope:vassal_heir_3 }
			hidden_effect = {
				if = {
					limit = {
						possible_mutual_interest_trigger = {
							CHARACTER_1 = scope:child_heir
							CHARACTER_2 = scope:vassal_heir_3
						}
						OR = {
							AND = {
								is_adult = yes
								scope:vassal_heir_3 = { is_adult = yes }
							}
							AND = {
								is_adult = no
								OR = {
									has_sexuality = heterosexual
									has_sexuality = bisexual
									has_sexuality = homosexual
								}
								scope:vassal_heir_3 = {
									is_adult = no
									OR = {
										has_sexuality = heterosexual
										has_sexuality = bisexual
										has_sexuality = homosexual
									}
								}
							}
						}
					}
					random = {
						chance = 10
						modifier = {
							add = 5
							number_of_personality_traits_in_common = {
								target = scope:vassal_heir_3
								value >= 2
							}
						}
						opinion_modifier = {
							opinion_target = scope:vassal_heir_3
							multiplier = 0.25
						}
						if = {
							limit = {
								is_adult = yes
							}
							set_relation_potential_lover = scope:vassal_heir_3
						}
						else = { #The heir gets a crush if they're not an adult
							set_relation_crush = scope:vassal_heir_3
						}
					}
				}
			}
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:vassal_heir_3
				multiplier = 0.5
			}
		}
	}

	option = { #No friend for them.
		name = councillor_spouse_diplomacy.2002.d
		add_prestige = minor_prestige_gain
		scope:child_heir = {
			add_prestige = minor_prestige_gain
		}
		ai_chance = {
			base = 0
		}
	}
}


#Reconcile with a rival that is present in your realm
councillor_spouse_diplomacy.2003 = {
	type = character_event
	title = councillor_spouse_diplomacy.2003.t
	desc = councillor_spouse_diplomacy.2003.desc
	theme = diplomacy
	left_portrait = {
		character = scope:rival
		animation = personality_cynical
	}
	right_portrait = {
		character = scope:councillor
		animation = personality_forgiving
	}
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		NOT = { has_character_flag = had_event_councillor_spouse_diplomacy_2003 }
		any_relation = {
			type = rival
			this != scope:councillor
			is_ai = yes
			top_liege = root.top_liege
			number_of_personality_traits_in_common = {
				target = root
				value >= 1
			}
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_2
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_2003
			days = 3650
		}
		random_relation = {
			type = rival
			limit = {
				this != scope:councillor
				is_ai = yes
				top_liege = root.top_liege
				number_of_personality_traits_in_common = {
					target = root
					value >= 1
				}
			}
			save_scope_as = rival
			personality_check_shared_good_ruler_traits_effect = {
				CHAR1 = this
				CHAR2 = root
				STRICT = no
			}
		}
		if = {
			limit = { root = { is_ai = no }	}
			scope:councillor = {
				assign_quirk_effect = yes
			}
		}
	}

	option = { #Sure, let's calm down
		name = councillor_spouse_diplomacy.2003.a
		remove_relation_rival = scope:rival
		hidden_effect = {
			scope:rival = {
				add_opinion = {
					target = scope:councillor
					modifier = respect_opinion
					opinion = 10
				}
			}
		}
		stress_impact = {
			vengeful = medium_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_compassion = 0.75
			}
		}
	}

	option = { #No
		name = councillor_spouse_diplomacy.2003.b
		add_piety = minor_piety_loss
		duel = {
			skill = prowess
			target = scope:rival
			17 = {	
				compare_modifier = {
					value = scope:duel_value
					multiplier = 2
				}
				scope:rival = {
					increase_wounds_effect = { REASON = fight }
				}
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -2
				}
				increase_wounds_effect = { REASON = fight }
			}
		}
		scope:rival = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = -40
			}
		}
		scope:councillor = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = -20
			}
		}
		stress_impact = {
			compassionate = medium_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_vengefulness = 0.75
			}
		}
	}

	option = { #Let me convince you of their vile vile ways
		name = councillor_spouse_diplomacy.2003.c
		duel = {
			skill = diplomacy
			target = scope:rival
			20 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 1
				}
				scope:councillor = {
					add_opinion = {
						target = scope:rival
						modifier = respect_opinion
						opinion = -30
					}
					add_opinion = {
						target = root
						modifier = respect_opinion
						opinion = 20
					}
				}
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -1
				}
				scope:councillor = {
					add_opinion = {
						target = root
						modifier = respect_opinion
						opinion = -20
					}
				}
			}
		}
		ai_chance = {
			base = 50
			compare_modifier = {
				value = diplomacy
				step = 4
				multiplier = 0.5
			}
		}
	}
}


#Foreign claimant seeks audience at your court - opportunity to use their claim!
scripted_trigger councillor_spouse_diplomacy_2004_claimant_trigger = {
	NOT = { is_courtier_of = root }
	NOT = { is_close_family_of = root}
	is_available_ai_adult = yes
	is_ruler = no
	is_married = no
}

scripted_trigger councillor_spouse_diplomacy_2004_title_trigger = {
	exists = this
	holder ?= {
		is_ai = yes
		root = {
			can_add_hook = {
				target = prev
				type = favor_hook
			}
		}
	}
	any_claimant = {
		councillor_spouse_diplomacy_2004_claimant_trigger = yes
	}
}

scripted_trigger councillor_spouse_diplomacy_2004_realm_trigger = {
	holder = {
		highest_held_title_tier > tier_county
	}
	any_in_de_facto_hierarchy = {
		councillor_spouse_diplomacy_2004_title_trigger = yes
	}
}

councillor_spouse_diplomacy.2004 = {
	type = character_event
	title = councillor_spouse_diplomacy.2004.t
	desc = councillor_spouse_diplomacy.2004.desc
	theme = diplomacy
	left_portrait = {
		character = scope:claimant
		animation = personality_honorable
	}
	right_portrait = {
		character = scope:councillor
		animation = personality_dishonorable
	}
	lower_left_portrait = scope:current_holder
	
	trigger = {
		valid_spouse_councillor_trigger = yes
		OR = { # Expensive, so we restrict the AI use of this
			is_ai = no
			highest_held_title_tier > tier_county
		}
		spouse_diplomacy_tier_2_trigger = yes
		NOT = { has_character_flag = had_event_councillor_spouse_diplomacy_2004 }
		any_neighboring_top_liege_realm = {
			councillor_spouse_diplomacy_2004_realm_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		councillor_spouse_limited_skill_rating_modifier = {
			SKILL = diplomacy
			SCALE = 1
			SKILL_RATING = spouse_skill_rating_2
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_councillor_spouse_diplomacy_2004
			days = 3650
		}
		random_neighboring_top_liege_realm = {
			limit = {
				councillor_spouse_diplomacy_2004_realm_trigger = yes
			}
			random_in_de_facto_hierarchy = {
				limit = {
					councillor_spouse_diplomacy_2004_title_trigger = yes
				}
				save_scope_as = title_to_claim
				holder = {
					save_scope_as = current_holder
				}
				random_claimant = {
					limit = {
						councillor_spouse_diplomacy_2004_claimant_trigger = yes
					}
					save_scope_as = claimant
				}
			}
		}
	}

	option = { #Please, stay here!
		name = councillor_spouse_diplomacy.2004.a
		custom_tooltip = councillor_spouse_diplomacy.2004.a.tt
		add_courtier = scope:claimant
		scope:claimant = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = 10
			}
		}
		hidden_effect = {
			scope:claimant = {
				add_opinion = {
					target = scope:councillor
					modifier = respect_opinion
					opinion = 10
				}
			}
		}
		ai_chance = {
			base = 75
			modifier = {
				add = 25
				has_trait = ambitious
			}
			ai_value_modifier = {
				ai_compassion = 0.25
			}
		}
	}

	option = { #Nah
		name = councillor_spouse_diplomacy.2004.b
		scope:claimant = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = -20
			}
		}
		ai_chance = {
			base = 0
		}
	}
	
	option = { #Throw them in jail
		name = councillor_spouse_diplomacy.2004.c
		custom_tooltip = councillor_spouse_diplomacy.2004.c.tt
		imprison_character_effect = {
			TARGET = scope:claimant
			IMPRISONER = root
		}
		add_hook = {
			target = scope:current_holder
			type = favor_hook
		}
		stress_impact = {
			compassionate = medium_stress_impact_gain
			just = medium_stress_impact_gain
		}
		ai_chance = {
			base = 50
			modifier = {
				add = 25
				has_trait = fickle
			}
			modifier = {
				add = 25
				dread < scope:current_holder.dread
			}
			ai_value_modifier = {
				ai_vengefulness = 0.25
				ai_rationality = 0.25
			}
		}
	}
}
