﻿namespace = courtier_guest_management

#COURTIERS/GUESTS LEAVING
# 0001 - Courtier is planning to leave
# 0102 - Courtier with claim is wanting to leave
# 0500 - Guests leaves
# 0999 - Maintenance - Force move to pool

#GUESTS ARRIVING
# 1001 - Guest arriving, message if they're great

#POOL CHARACTERS
# 2001 - "Develops" the character and gives those without location a location
# 2002 - Pool repopulation

#COURTIERS AND GUESTS DOING STUFF
# 3001 - Sets relationships and other secrets


############################
# COURTIERS/GUESTS LEAVING #				
############################

scripted_trigger courtier_leaving_should_notify = {
	OR = {
		is_close_or_extended_family_of = $CHARACTER$
		is_great_grandchild_of = $CHARACTER$
		AND = {
			real_father ?= $CHARACTER$
			any_targeting_secret = {
				is_known_by = $CHARACTER$
				OR = {
					secret_type = secret_unmarried_illegitimate_child
					secret_type = secret_disputed_heritage
				}
			}
		}
		any_child = {
			is_adult = no
			real_father ?= $CHARACTER$
			is_courtier_of = $CHARACTER$
			any_targeting_secret = {
				is_known_by = $CHARACTER$
				OR = {
					secret_type = secret_unmarried_illegitimate_child
					secret_type = secret_disputed_heritage
				}
			}
		}
		primary_spouse ?= {
			OR = {
				is_close_or_extended_family_of = $CHARACTER$
				is_great_grandchild_of = $CHARACTER$
			}
		}
	}
}

#Courtier is planning to leave
courtier_guest_management.0001 = {
	hidden = yes

	trigger = {
		any_in_list = {
			list = characters
			courtier_leaving_should_notify = { CHARACTER = scope:liege }
		}
	}

	immediate = {
		#Send message about child about to leave
		every_in_list = {
			list = characters
			limit = {
				courtier_leaving_should_notify = { CHARACTER = scope:liege }
			}

			save_scope_as = child
			scope:liege = {
				send_interface_message = {
					type = character_preparing_to_leave
					title = courtier_guest_management.0001.t
					desc = courtier_guest_management.desc
					left_icon = scope:child
				}
			}
		}
	}
}

# this = claimed title scope
# CLAIMANT = claimant
# RULER = ruler for claimant
scripted_trigger useful_courtier_claim_can_declare_for = {
	neighboring_useful_courtier_or_guest_claim_trigger = { RULER = $RULER$ }
	save_temporary_scope_as = claim_title
	$RULER$ = {
		can_declare_war = {
			defender = scope:claim_title.holder
			claimant = $CLAIMANT$
			casus_belli = claim_cb
			target_titles = { scope:claim_title }
		}
	}
}

#Courtier is leaving
courtier_guest_management.0101 = {
	hidden = yes

	immediate = {
		#Liege gets "last chance to make them stay"-event for claimants
		if = {
			limit = {
				scope:liege = { is_ai = no }
				any_in_list = {
					list = characters
					save_temporary_scope_as = check_character
					any_claim = { 
						useful_courtier_claim_can_declare_for = {
							CLAIMANT = scope:check_character
							RULER = scope:liege
						}
					}
				}
			}
			random_in_list = {
				list = characters
				limit = {
					save_temporary_scope_as = check_character
					any_claim = {
						useful_courtier_claim_can_declare_for = {
							CLAIMANT = scope:check_character
							RULER = scope:liege
						}
						tier = tier_kingdom
					}
				}
				alternative_limit = {
					save_temporary_scope_as = check_character
					any_claim = {
						useful_courtier_claim_can_declare_for = {
							CLAIMANT = scope:check_character
							RULER = scope:liege
						}
						tier = tier_duchy
					}
				}
				alternative_limit = {
					save_temporary_scope_as = check_character
					any_claim = {
						useful_courtier_claim_can_declare_for = {
							CLAIMANT = scope:check_character
							RULER = scope:liege
						}
						tier = tier_county
					}
				}
				save_scope_as = courtier
				scope:liege = { trigger_event = courtier_guest_management.0102 }
			}
		}
		else = {
			#Do I join a holy order or take vows?
			if = {
				limit = {
					NOR = {
						has_trait = devoted
						has_trait = order_member
						any_heir_title = {}
						is_pregnant = yes
						is_lowborn = yes
					}
				}
				random_list = {
					0 = {
						trigger = {
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 0.5
							ai_zeal = 1.0
							ai_honor = 0.5
						}
						modifier = {
							has_education_martial_trigger = no
							factor = 0.5
						}
						modifier = {
							add = 20
							religion = { exists = var:variable_ghw_unlocked }
						}
						modifier = {
							add = 100
							faith = { exists = great_holy_war }
						}
						min = 0
						create_character_memory = {
							type = lmf_memory_left_court
							participants = { court = scope:liege }
						}
						scope:new_memory = {
							set_variable = {
								name = liege_title
								value = scope:liege.primary_title
							}
						}
						pool_character_join_holy_order = { CHARACTER = root }
						if = {
							limit = { courtier_leaving_should_notify = { CHARACTER = scope:liege } }
							save_scope_as = child
							scope:liege = {
								send_interface_message = {
									type = character_preparing_to_leave
									title = courtier_guest_management.left.t
									desc = courtier_guest_management.holyorder.desc
									left_icon = scope:child
								}
							}
						}
					}
					0 = {
						trigger = {
							pool_character_can_take_holy_vows = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_zeal = 1
						}
						modifier = {
							factor = 0.5
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						modifier = {
							has_education_learning_trigger = no
							factor = 0.5
						}
						min = 0
						create_character_memory = {
							type = lmf_memory_left_court
							participants = { court = scope:liege }
						}
						scope:new_memory = {
							set_variable = {
								name = liege_title
								value = scope:liege.primary_title
							}
						}
						pool_character_take_vows = { CHARACTER = root }
						if = {
							limit = { courtier_leaving_should_notify = { CHARACTER = scope:liege } }
							save_scope_as = child
							scope:liege = {
								send_interface_message = {
									type = character_preparing_to_leave
									title = courtier_guest_management.left.t
									desc = courtier_guest_management.vows.desc
									left_icon = scope:child
								}
							}
						}
					}
					0 = {
						trigger = {
							pool_character_can_join_random_adventurer = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 1
							ai_energy = 0.5
							ai_rationality = -0.5
						}
						modifier = {
							factor = 0.5
							NOT = { can_be_warrior_trigger = { ARMY_OWNER = root } }
						}
						modifier = {
							factor = 0.5
							ai_zeal > 0
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						min = 0
						create_character_memory = {
							type = lmf_memory_left_court
							participants = { court = scope:liege }
						}
						scope:new_memory = {
							set_variable = {
								name = liege_title
								value = scope:liege.primary_title
							}
						}
						pool_character_join_random_adventurer = { CHARACTER = root }
						if = {
							limit = { courtier_leaving_should_notify = { CHARACTER = scope:liege } }
							save_scope_as = child
							scope:liege = {
								send_interface_message = {
									type = character_preparing_to_leave
									title = courtier_guest_management.left.t
									desc = courtier_guest_management.follower.desc
									left_icon = scope:child
								}
							}
						}
					}
					20 = {
						trigger = {
							pool_character_can_join_dynasty_adventurer = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 1
							ai_energy = 0.5
							ai_rationality = -0.5
						}
						modifier = {
							factor = 0.5
							NOT = { can_be_warrior_trigger = { ARMY_OWNER = root } }
						}
						modifier = {
							factor = 0.5
							ai_zeal > 0
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						min = 0
						create_character_memory = {
							type = lmf_memory_left_court
							participants = { court = scope:liege }
						}
						scope:new_memory = {
							set_variable = {
								name = liege_title
								value = scope:liege.primary_title
							}
						}
						pool_character_join_dynasty_adventurer = { CHARACTER = root }
						if = {
							limit = { courtier_leaving_should_notify = { CHARACTER = scope:liege } }
							save_scope_as = child
							scope:liege = {
								send_interface_message = {
									type = character_preparing_to_leave
									title = courtier_guest_management.left.t
									desc = courtier_guest_management.follower.desc
									left_icon = scope:child
								}
							}
						}
					}
					200 = {
						create_character_memory = {
							type = lmf_memory_left_court
							participants = { court = scope:liege }
						}
						scope:new_memory = {
							set_variable = {
								name = liege_title
								value = scope:liege.primary_title
							}
						}
						lmf_move_to_pool_effect = yes
						if = {
							limit = { courtier_leaving_should_notify = { CHARACTER = scope:liege } }
							save_scope_as = child
							scope:liege = {
								send_interface_message = {
									type = character_preparing_to_leave
									title = courtier_guest_management.left.t
									desc = courtier_guest_management.left.desc
									left_icon = scope:child
								}
							}
						}
					}
				}
			}
		}
	}
}


#Courtier with claim is wanting to leave
courtier_guest_management.0102 = { #by Mathilda Bjarnehed
	type = character_event
	title = courtier_guest_management.0102.t
	desc = courtier_guest_management.0102.desc
	left_portrait = scope:courtier
	right_portrait = scope:courtier_claim
	theme = crown
	override_background = {
		reference = courtyard
	}
	
	trigger = {
		scope:courtier = {
			any_claim = {
				useful_courtier_claim_can_declare_for = {
					CLAIMANT = scope:courtier
					RULER = scope:liege
				}
			}
			is_pool_character = no
		}
	}

	immediate = {
		#For text
		scope:courtier = {
			random_claim = {
				limit = {
					useful_courtier_claim_can_declare_for = {
						CLAIMANT = scope:courtier
						RULER = scope:liege
					}
					tier = tier_empire
				}
				alternative_limit = {
					useful_courtier_claim_can_declare_for = {
						CLAIMANT = scope:courtier
						RULER = scope:liege
					}
					tier = tier_kingdom
				}
				alternative_limit = {
					useful_courtier_claim_can_declare_for = {
						CLAIMANT = scope:courtier
						RULER = scope:liege
					}
					tier = tier_duchy
				}
				alternative_limit = {
					useful_courtier_claim_can_declare_for = {
						CLAIMANT = scope:courtier
						RULER = scope:liege
					}
					tier = tier_county
				}
				save_scope_as = courtier_claim
			}
		}

		capital_province = { barony = { save_scope_as = capital } }
	}

	#Yes, I promise
	option = {
		name = courtier_guest_management.0102.a

		show_as_tooltip = {
			random_list = {
				desc = courtier_guest_management.0102.a.rl.desc
				1 = {
					show_chance = no
					desc = courtier_guest_management.0102.a.stay
					custom_tooltip = courtier_guest_management.0102.a.stay.tt
				}
				1 = {
					show_chance = no
					desc = courtier_guest_management.0102.a.leave
					scope:courtier = { lmf_move_to_pool_effect = yes }
				}
			}
		}

		hidden_effect = {
			scope:courtier = {
				add_character_flag = {
					flag = courtier_staying_for_claim
					years = 1
				}
				trigger_event = {
					id = courtier_guest_management.0103
					years = 1
				}
			}
		}
	}

	#No, I cannot
	option = {
		name = courtier_guest_management.0102.b
		
		scope:courtier = {
			create_character_memory = {
				type = lmf_memory_left_court
				participants = { court = scope:liege }
			}
			scope:new_memory = {
				set_variable = {
					name = liege_title
					value = scope:liege.primary_title
				}
			}
			lmf_move_to_pool_effect = yes
		}
	}
}

#Did liege fulfill their promise to courtier claimant?
courtier_guest_management.0103 = {
	hidden = yes

	trigger = {
		exists = scope:liege
		is_courtier_of = scope:liege
		NOT = {
			scope:liege = {
				any_character_war = {
					casus_belli = {
						any_target_title = {
							scope:courtier = { has_claim_on = prev }
						}
					}
				}
			}
		}
	}

	immediate = {
		create_character_memory = {
			type = lmf_memory_left_court
			participants = { court = scope:liege }
		}
		scope:new_memory = {
			set_variable = {
				name = liege_title
				value = scope:liege.primary_title
			}
		}
		lmf_move_to_pool_effect = yes
	}
}


#Guest leaves
courtier_guest_management.0500 = {
	hidden = yes

	immediate = {
		if = { # Warning if they take your kids with them
			limit = {
				NOT = { has_character_flag = guest_has_given_warning_about_leaving }
				#To double check that they actually can leave
				is_pool_guest_of = scope:host
				scope:host = { is_ai = no }
				any_traveling_family_member = {
					count = all
					guest_allowed_to_leave_trigger = yes
				}
				OR = {
					AND = {
						OR = {
							has_trait = disinherited
							has_trait = bastard
							has_trait = wild_oat
						}
						any_parent = { this = scope:host }
					}
					any_traveling_family_member = {
						OR = {
							has_trait = disinherited
							has_trait = bastard
							has_trait = wild_oat
						}
						any_parent = { this = scope:host }
					}
					scope:host = {
						any_known_secret = {
							OR = {
								secret_type = secret_disputed_heritage
								secret_type = secret_unmarried_illegitimate_child
							}
							OR = {
								secret_target = {
									this = root
									is_adult = no
								}
								secret_target = {
									save_temporary_scope_as = target_check
									root = {
										any_traveling_family_member = {
											this = scope:target_check
											is_adult = no
										}
									}
								}
							}
						}
					}
				}
			}
			scope:host = {
				send_interface_message = {
					type = character_preparing_to_leave
					title = courtier_guest_management.0500.t
					desc = courtier_guest_management.0500.desc
					left_icon = root
				}
			}
			add_character_flag = {
				flag = guest_has_given_warning_about_leaving
				days = 35
			}
			trigger_event = {
				id = courtier_guest_management.0500
				days = { 25 30 } #You get about a month to decide if you wanna keep them around!
			}
		}
		else = {
			if = {
				limit = {
					has_character_flag = guest_has_given_warning_about_leaving
				}
				remove_character_flag = guest_has_given_warning_about_leaving
			}
			if = {
				limit = {
					NOR = {
						has_trait = devoted
						has_trait = order_member
						any_heir_title = {}
						is_pregnant = yes
						is_lowborn = yes
					}
				}
				random_list = {
					0 = {
						trigger = {
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 0.5
							ai_zeal = 1.0
							ai_honor = 0.5
						}
						modifier = {
							has_education_martial_trigger = no
							factor = 0.5
						}
						modifier = {
							add = 20
							religion = { exists = var:variable_ghw_unlocked }
						}
						modifier = {
							add = 100
							faith = { exists = great_holy_war }
						}
						min = 0
						pool_character_join_holy_order = { CHARACTER = root }
					}
					0 = {
						trigger = {
							pool_character_can_take_holy_vows = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_zeal = 1
						}
						modifier = {
							factor = 0.5
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						modifier = {
							has_education_learning_trigger = no
							factor = 0.5
						}
						min = 0
						pool_character_take_vows = { CHARACTER = root }
					}
					0 = {
						trigger = {
							pool_character_can_join_random_adventurer = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 1
							ai_energy = 0.5
							ai_rationality = -0.5
						}
						modifier = {
							factor = 0.5
							NOT = { can_be_warrior_trigger = { ARMY_OWNER = root } }
						}
						modifier = {
							factor = 0.5
							ai_zeal > 0
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						min = 0
						pool_character_join_random_adventurer = { CHARACTER = root }
					}
					20 = {
						trigger = {
							pool_character_can_join_dynasty_adventurer = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 1
							ai_energy = 0.5
							ai_rationality = -0.5
						}
						modifier = {
							factor = 0.5
							NOT = { can_be_warrior_trigger = { ARMY_OWNER = root } }
						}
						modifier = {
							factor = 0.5
							ai_zeal > 0
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						min = 0
						pool_character_join_dynasty_adventurer = { CHARACTER = root }
					}
					200 = {
						lmf_move_to_pool_effect = yes
					}
				}
			}
		}
	}
}

# Maintenance - Force move to pool
courtier_guest_management.0999 = {
	hidden = yes
	
	immediate = {
		if = {
			limit = { exists = scope:liege }
			create_character_memory = {
				type = lmf_memory_left_court
				participants = { court = scope:liege }
			}
			scope:new_memory = {
				set_variable = {
					name = liege_title
					value = scope:liege.primary_title
				}
			}
		}
		lmf_move_to_pool_effect = yes
	}
}



###################
# GUESTS ARRIVING #
###################

#Guest arriving, message if they're great
scripted_trigger courtier_guest_management_1001_exceptional_claimant_trigger = {
	any_claim = { neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host } }
}

scripted_trigger courtier_guest_management_1001_exceptional_marriage_candidate_trigger = {
	save_temporary_scope_as = marriage_candidate
	guest_marriage_candidate_trigger = yes
	age > 14
	OR = {
		num_of_good_genetic_traits = 3
		AND = {
			num_of_good_genetic_traits = 2
			OR = {
				has_trait = beauty_good_3
				has_trait = intellect_good_3
				has_trait = physique_good_3
			}
		}
		scope:host = {
			can_marry_character_trigger = { CHARACTER = scope:marriage_candidate }
			marriage_between_characters_unlocks_alliance_of_min_tier_trigger = {
				MY_CHARACTER = scope:host
				THEIR_CHARACTER = scope:marriage_candidate
				TIER = scope:host.highest_held_title_tier
			}
		}
		dynasty ?= { dynasty_prestige_level >= high_dynasty_prestige_level }
		has_useful_potential_spouse_claim_trigger = { RULER = scope:host }
		calc_true_if = {
			amount >= 2
			num_of_good_genetic_traits = 2
			AND = {
				num_of_good_genetic_traits = 1
				OR = {
					has_trait = beauty_good_2
					has_trait = intellect_good_2
					has_trait = physique_good_2
				}
			}
			dynasty ?= { dynasty_prestige_level > medium_dynasty_prestige_level }
		}
		calc_true_if = {
			amount >= 3
			dynasty ?= { dynasty_prestige_level > low_dynasty_prestige_level }
			num_of_good_genetic_traits = 1
			diplomacy >= high_skill_rating
			martial >= high_skill_rating
			stewardship >= high_skill_rating
			intrigue >= high_skill_rating
			learning >= high_skill_rating
		}
	}
	NOR = {
		num_of_bad_genetic_traits > 1
		is_lowborn = yes
	}
}

scripted_trigger courtier_guest_management_1001_exceptional_commander_trigger = {
	guest_commander_candidate_trigger = { HOST = scope:host }
	number_of_commander_traits >= 1
	martial >= very_high_skill_rating
}

scripted_trigger courtier_guest_management_1001_exceptional_vassal_trigger = {
	guest_vassal_candidate_trigger = yes
	opinion = {
		target = scope:host
		value >= medium_positive_opinion
	}
	calc_true_if = {
		amount >= 2
		OR = {
			has_trait = content
			has_trait = trusting
			has_trait = humble
		}	
		martial >= high_skill_rating
		stewardship >= high_skill_rating
	}
}

scripted_trigger courtier_guest_management_1001_exceptional_knight_trigger = {
	guest_knight_candidate_trigger = { HOST = root }
	prowess >= very_high_skill_rating
}

scripted_trigger courtier_guest_management_1001_exceptional_physician_trigger = {
	guest_physician_candidate_trigger = yes
	OR = {
		has_trait = lifestyle_mystic
		has_trait = lifestyle_physician
		has_trait = lifestyle_herbalist
	}	
	learning >= high_skill_rating
}



courtier_guest_management.1001 = {
	hidden = yes

	immediate = {
		scope:guest = {
			every_traveling_family_member = {
				add_to_temporary_list = guest_arrival_list
			}
		}
		
		# To update Adventure Inspiration to make sure that the target destination isn't in the realm they just arrived to. If it is we remove it and make them pick a new destination!
		every_in_list = {
			list = guest_arrival_list
			limit = {
				inspiration ?= { has_inspiration_type = adventure_inspiration }
				exists = var:adventure_destination #They know where they want to go
			}
			get_adventure_inspiration_start_location = { ADVENTURER = this }
			if = {
				limit = {
					scope:host = {
						any_sub_realm_county = {
							this = scope:location.county
						}
					}
				}
				remove_variable = adventure_destination
				if = {
					limit = {
						adventure_inspiration_average_skill_value >= high_inspiration_skill
					}
					set_adventure_location_effect = {
						BASE_SCOPE = this
						NO_DESTINATION = yes
					}
				}
				else = {
					set_adventure_location_effect = {
						BASE_SCOPE = this
						NO_DESTINATION = no
					}
				}
			}
		}

		###DEVELOPMENT EFFECT###
		#Because the characters spend so little time in the pool, we also run it now

		every_in_list = { # Very performance-intensive, so we restrict it as much as possible at this step.
			list = guest_arrival_list
			limit = {
				is_adult = yes
				is_lowborn = no
				is_physically_able = yes
			}
			pool_character_development_effect = yes
			if = {
				limit = {
					gold >= 1000
				}
				purge_excess_wanderer_riches_effect = yes
			}
		}

		###EXCEPTIONAL GUEST TOAST###
		if = {
			limit = { scope:host = { is_ai = no } } #This is just interface messages, AI doesn't care

			every_in_list = {
				list = guest_arrival_list
				limit = {
					courtier_guest_management_1001_exceptional_claimant_trigger = yes
				}
				save_scope_as = exceptional_guest

				#CLAIMANT MESSAGE
				if = {
					limit = { courtier_guest_management_1001_exceptional_claimant_trigger = yes }
					random_claim = {
						limit = {
							neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
							tier = tier_kingdom
							scope:host.highest_held_title_tier > tier_kingdom
						}
						alternative_limit = {
							neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
							tier = tier_duchy
							scope:host.highest_held_title_tier > tier_duchy
						}
						alternative_limit = {
							neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
							tier = tier_county
							scope:host.highest_held_title_tier > tier_county
						}
						save_scope_as = guest_claim
					}
					scope:host = {
						send_interface_toast = {
							type = msg_notable_guest
							left_icon = scope:exceptional_guest
							right_icon = scope:guest_claim
							title = courtier_guest_management.1001.t
							desc = courtier_guest_management.1001.claimant.desc
						}
					}
				}
			}
		}
	}
}

###################
# POOL CHARACTERS #
###################

#This events gives pool characters relations, skills, traits and secrets. If they lack a location, they're given a location.
courtier_guest_management.2001 = {
	hidden = yes
	trigger = {
		is_pool_character = yes # Can cease to be in the pool between it queuing and executing
	}

	immediate = {
		#Am I lost in the world or I don't have a "last_visited_ruler" variable? (= it ran out or I'm from some weird source that doesn't set var)
		if = {
			limit = {
				NOT = { has_trait = seclusion }
				OR = {
					NOT = { exists = location }
					NOT = { exists = var:last_visited_ruler }
				}
			}
			
			if = { # If character is prune-able, prune.
				limit = {
					pool_character_is_pruneable_trigger = yes
				}
				if = {
					limit = { age < 65 }
					death = { death_reason = death_accident }
				}
				else = {
					death = { death_reason = death_old_age }
				}
			}
			
			else = { # Otherwise, go somewhere else.
				fallback_move_to_pool_effect = yes
			}
		}

		# Now what?
		if = {
			limit = {
				NOT = { has_trait = seclusion }
				is_alive = yes #Because we might just have killed you and then 🎵nothing else matters🎵
			}

			#Develop
			random = { # Very performance-intensive, so we restrict it as much as possible at this step.
				chance = 50
				if = {
					limit = {
						static_group_filter = {
							group = random_dev_blocker
							match = 0.8
						}
						is_adult = yes
						is_lowborn = no
						is_physically_able = yes
					}
					pool_character_development_effect = yes
				}
			}
			if = {
				limit = {
					gold >= 1000
				}
				purge_excess_wanderer_riches_effect = yes
			}
			
			#Do I have a spouse to join in a court?
			if = {
				limit = {
					has_dynasty = yes
					primary_spouse ?= {
						is_courtier = yes
						is_physically_able = yes
						NOR = {
							has_trait = adulterer
							has_relation_rival = root
							has_character_flag = made_recent_reunite_check
							has_opinion_modifier = { target = liege modifier = refused_reunion_permission_opinion }
						}
						liege = {
							NOR = {
								has_relation_rival = root
								has_imprisonment_reason = root
								has_government = mercenary_government
								has_government = theocracy_government
								has_government = holy_order_government
								is_landless_adventurer = yes
								root.var:last_visited_ruler ?= this
							}
						}
					}
					NOR = {
						has_trait = adulterer
						has_recoverable_disease_trigger = yes
					}
				}
				#For a player, have them make a request
				if = {
					limit = {
						primary_spouse.liege = { is_ai = no }
					}
					save_scope_as = requester_spouse
					primary_spouse = {
						save_scope_as = requester
						liege = { trigger_event = lmf_scheme.1041 }
					}
				}
				else = {
					primary_spouse.liege = { add_courtier = root }
				}
			}
			
			# Do I have close family to join in a court?
			else_if = {
				limit = {
					has_dynasty = yes
					any_close_family_member = {
						OR = {
							AND = {
								is_playable_character = yes
								NOR = {
									has_relation_rival = root
									has_imprisonment_reason = root
									has_government = mercenary_government
									has_government = theocracy_government
									has_government = holy_order_government
									is_landless_adventurer = yes
								}
							}
							AND = {
								is_courtier = yes
								liege = {
									is_physically_able_ai = yes
									NOR = {
										is_parent_of = root
										has_relation_rival = root
										has_relation_rival = prev
										has_imprisonment_reason = root
										has_imprisonment_reason = prev
										has_government = mercenary_government
										has_government = theocracy_government
										has_government = holy_order_government
										is_landless_adventurer = yes
										available_courtier_slot < 1
									}
								}
							}
						}
						is_physically_able_ai = yes
						NOT = { is_parent_of = root }
					}
				}
				random_close_family_member = {
					limit = {
						is_playable_character = yes
						is_physically_able_ai = yes
						NOR = {
							is_parent_of = root
							has_relation_rival = root
							has_imprisonment_reason = root
							has_government = mercenary_government
							has_government = theocracy_government
							has_government = holy_order_government
							is_landless_adventurer = yes
							root.var:last_visited_ruler ?= this
						}
					}
					alternative_limit = {
						is_courtier = yes
						is_physically_able_ai = yes
						NOT = { is_parent_of = root }
						liege = {
							is_physically_able_ai = yes
							NOR = {
								is_parent_of = root
								has_relation_rival = root
								has_relation_rival = prev
								has_imprisonment_reason = root
								has_imprisonment_reason = prev
								has_government = mercenary_government
								has_government = theocracy_government
								has_government = holy_order_government
								is_landless_adventurer = yes
								root.var:last_visited_ruler ?= this
								available_courtier_slot < 1
							}
						}
					}
					if = {
						limit = { is_playable_character = yes }
						add_courtier = root
					}
					else = {
						liege = { add_courtier = root }
					}
				}
			}
			
			#Do I join a holy order or take vows?
			else_if = {
				limit = {
					has_dynasty = yes
					NOR = {
						has_trait = devoted
						has_trait = order_member
						any_heir_title = {}
						is_pregnant = yes
					}
				}
				random_list = {
					0 = {
						trigger = {
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 0.5
							ai_zeal = 1.0
							ai_honor = 0.5
						}
						modifier = {
							has_education_martial_trigger = no
							factor = 0.5
						}
						modifier = {
							add = 20
							religion = { exists = var:variable_ghw_unlocked }
						}
						modifier = {
							add = 100
							faith = { exists = great_holy_war }
						}
						min = 0
						pool_character_join_holy_order = { CHARACTER = root }
					}
					0 = {
						trigger = {
							pool_character_can_take_holy_vows = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_zeal = 1
						}
						modifier = {
							factor = 0.5
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						modifier = {
							has_education_learning_trigger = no
							factor = 0.5
						}
						min = 0
						pool_character_take_vows = { CHARACTER = root }
					}
					0 = {
						trigger = {
							pool_character_can_join_random_adventurer = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 1
							ai_energy = 0.5
							ai_rationality = -0.5
						}
						modifier = {
							factor = 0.5
							NOT = { can_be_warrior_trigger = { ARMY_OWNER = root } }
						}
						modifier = {
							factor = 0.5
							ai_zeal > 0
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						min = 0
						pool_character_join_random_adventurer = { CHARACTER = root }
					}
					20 = {
						trigger = {
							pool_character_can_join_dynasty_adventurer = { CHARACTER = root }
						}
						ai_value_modifier = {
							ai_boldness = 1
							ai_energy = 0.5
							ai_rationality = -0.5
						}
						modifier = {
							factor = 0.5
							NOT = { can_be_warrior_trigger = { ARMY_OWNER = root } }
						}
						modifier = {
							factor = 0.5
							ai_zeal > 0
							pool_character_can_go_to_holy_order = { CHARACTER = root }
						}
						min = 0
						pool_character_join_dynasty_adventurer = { CHARACTER = root }
					}
					200 = {
						# do nothing
					}
				}
			}
			
			else = {
				# do nothing
			}

			#Is my pool full? Then move
			if = {
				limit = {
					is_pool_character = yes # we haven't gone anywhere in the interim
					location = {
						exists = province_owner
						local_pool_is_full_trigger = yes
					}
				}

				location.province_owner = {
					random_neighboring_and_across_water_top_liege_realm_owner = {
						limit = {
							capital_province ?= { local_pool_is_full_trigger = no }
						}
						capital_province = { save_scope_as = destination }
					}
				}

				if = {
					limit = { exists = scope:destination }
					every_traveling_family_member = {
						limit = { NOT = { is_in_pool_at = scope:destination } }
						move_to_pool_at = scope:destination
					}
				}
				else = {
					fallback_move_to_pool_effect = yes
				}
			}
		}
	}
}

####################################
# COURTIERS AND GUESTS DOING STUFF #
####################################

scripted_trigger courtier_guest_management_3001_best_friend_trigger = {
	opinion = {
		target = scope:current_character
		value >= medium_positive_opinion
	}
	reverse_opinion = {
		target = scope:current_character
		value >= medium_positive_opinion
	}
}

scripted_trigger courtier_guest_management_3001_soulmate_trigger = {
	opinion = {
		target = scope:current_character
		value >= medium_positive_opinion
	}
	reverse_opinion = {
		target = scope:current_character
		value >= medium_positive_opinion
	}
}

scripted_trigger courtier_guest_management_3001_nemesis_trigger = {
	opinion = {
		target = scope:current_character
		value <= medium_negative_opinion
	}
	reverse_opinion = {
		target = scope:current_character
		value <= medium_negative_opinion
	}
}

scripted_trigger courtier_guest_management_3001_interesting_trigger = {
	is_lowborn = no
	OR = {
		has_trait = intellect_good
		has_trait = physique_good
		has_trait = beauty_good
		diplomacy >= high_skill_rating
		martial >= high_skill_rating
		stewardship >= high_skill_rating
		intrigue >= high_skill_rating
		learning >= high_skill_rating
		is_witch_trigger = yes
		is_deviant_trigger = yes
	}
}

scripted_trigger is_courtier_guest_management_lover_courtier = {
	NOR = {
		has_trait = chaste
		has_trait = celibate
		has_sexuality = asexual
		has_trait = infirm
		is_malformed_trigger = yes
		num_of_relation_soulmate > 0
		has_any_debilitating_illness = yes
		is_involved_in_any_romantic_scheme = yes
		is_in_list = already_updated
	}
	OR = {
		is_old_age_or_not_have_children = no
		is_deviant_trigger = yes
		has_focus = intrigue_temptation_focus
		faith = { has_doctrine = tenet_carnal_exaltation }
	}
	ai_boldness >= 0
	trigger_if = {
		limit = { is_married = yes }
		ai_honor < 0
	}
	is_below_ai_lover_soft_cap_trigger = yes
	might_cheat_on_every_partner_trigger = yes
	trigger_if = {
		limit = {
			OR = {
				is_clergy = yes
				has_trait = devoted
				has_council_position = councillor_court_chaplain
			}
		}
		OR = {
			faith = { has_doctrine = tenet_carnal_exaltation }
			is_deviant_trigger = yes
			has_trait = lustful
			ai_zeal <= medium_negative_ai_value
		}
	}
}

scripted_trigger is_courtier_guest_management_lover_target = {
	is_below_ai_lover_soft_cap_trigger = yes
	NOR = {
		has_trait = infirm
		is_malformed_trigger = yes
		is_unattractive_trigger = yes
		is_involved_in_any_romantic_scheme = yes
		is_in_list = already_updated
	}
	trigger_if = {
		limit = {
			scope:current_character = {
				is_female = yes
				age < 25
			}
		}
		age <= scope:current_character.age_plus_25
		age >= scope:current_character.age_minus_10
	}
	trigger_else_if = {
		limit = {
			scope:current_character = {
				is_female = yes
				age >= 25
			}
		}
		age <= scope:current_character.age_plus_10
		age >= scope:current_character.age_minus_10
	}
	trigger_else = {
		age <= scope:current_character.age
		age >= scope:current_character.age_minus_25
	}
	can_set_relation_lover_trigger = { CHARACTER = scope:current_character }
	might_cheat_on_every_partner_trigger = yes
	save_temporary_scope_as = potential_lover
	willing_to_take_character_as_lover = { LOVER = scope:current_character }
	scope:current_character = {
		willing_to_take_character_as_lover = { LOVER = scope:potential_lover }
	}
	trigger_if = {
		limit = { scope:current_character = { ai_boldness < 20 } }
		NOR = {
			primary_spouse ?= { is_liege_or_above_of = scope:current_character }
			any_parent = { is_liege_or_above_of = scope:current_character }
		}
	}
	trigger_if = {
		limit = { scope:current_character = { ai_honor >= 0 } }
		is_married = no
	}
	accepts_incest_with_each_other_trigger = {
		CHARACTER_1 = scope:potential_lover
		CHARACTER_2 = scope:current_character
	}
	trigger_if = {
		limit = {
			OR = {
				is_clergy = yes
				has_trait = devoted
				has_council_position = councillor_court_chaplain
			}
		}
		OR = {
			faith = { has_doctrine = tenet_carnal_exaltation }
			is_deviant_trigger = yes
			has_trait = lustful
			ai_zeal <= medium_negative_ai_value
		}
	}
}

scripted_trigger is_courtier_guest_management_player_rival_target = {
	save_temporary_scope_as = potential_rival_courtier
	scope:current_character = {
		OR = {
			has_relation_potential_rival = scope:potential_rival_courtier
			has_relation_bully = scope:potential_rival_courtier
			has_relation_grudge = scope:potential_rival_courtier
			has_revenge_motivation_against = { CHARACTER = scope:potential_rival_courtier }
			is_resentful_of_impious_character = { CHARACTER = scope:potential_rival_courtier }
			is_resentful_of_pious_character = { CHARACTER = scope:potential_rival_courtier }
			is_resentful_of_hostile_faith_character = { CHARACTER = scope:potential_rival_courtier }
			is_resentful_of_pious_astray_faith_character = { CHARACTER = scope:potential_rival_courtier }
			has_hatred_towards = { CHARACTER = scope:potential_rival_courtier }
			has_major_incompatibility_with = { CHARACTER = scope:potential_rival_courtier }
			is_jealous_of_their_lover = { CHARACTER = scope:potential_rival_courtier }
			is_jealous_of_their_court_position = { CHARACTER = scope:potential_rival_courtier }
			is_jealous_of_their_council_position = { CHARACTER = scope:potential_rival_courtier }
			is_jealous_of_their_heir_position = { CHARACTER = scope:potential_rival_courtier }
		}
	}
}

courtier_guest_management.3001 = {
	hidden = yes

	trigger = {
		OR = {
			is_ai = no
			any_courtier_or_guest = { count >= 15 } #restrict this to larger AI courts
		}
	}

	immediate = {
		every_courtier_or_guest = {
			limit = {
				is_physically_able_adult = yes
				is_healthy = yes
			}
			add_to_list = courtiers_and_guests	
		}

		# Am I a player? Extra fun stuff
		if = {
			limit = { is_ai = no }

			random_list = {
				2 = { #Seduce
					modifier = {
						OR = {
							has_trait = lustful
							has_trait = seducer
							has_focus = intrigue_temptation_focus
						}
						add = 3
					}
					modifier = {
						any_targeting_scheme = { scheme_type = seduce }
						factor = 0
					}
					random_in_list = {
						list = courtiers_and_guests
						limit = {
							can_set_relation_lover_trigger = { CHARACTER = root }
							accepts_incest_with_trigger = { CHARACTER = root }
							courtier_guest_management_3001_interesting_trigger = yes
							might_cheat_on_every_partner_trigger = yes
							willing_to_take_character_as_lover = { LOVER = root }
							NOR = {
								has_trait = infirm
								has_trait = incapable
								is_malformed_trigger = yes
								is_unattractive_trigger = yes
								any_scheme = {}
							}
							#filter out too large an age gap
							age <= root.age_plus_10
							age >= root.age_minus_25
							trigger_if = {
								limit = { is_female = yes }
								age <= root.age
							}
							OR = {
								is_old_age_or_not_have_children = no
								is_deviant_trigger = yes
								has_trait = ambitious
								has_focus = intrigue_temptation_focus
								faith = { has_doctrine = tenet_carnal_exaltation }
							}
							can_start_scheme = {
								type = seduce
								target_character = root
							}
						}
						weight = {
							base = 100
							opinion_modifier = {
								opinion_target = root
								multiplier = 5
							}
							compatibility_modifier = {
								compatibility_target = root
								multiplier = 10
							}
							ai_value_modifier = {
								ai_sociability = 1
							}
							modifier = {
								has_relation_potential_lover = root
								add = 500
							}
							modifier = {
								add = {
									value = attraction
									divide = high_positive_attraction
									multiply = 50
								}
							}
							modifier = {
								has_trait = lustful
								factor = 1.5
							}
							modifier = {
								OR = {
									is_clergy = yes
									has_trait = devoted
									has_council_position = councillor_court_chaplain
								}
								faith = {
									NOT = { has_doctrine_parameter = clergy_can_marry }
								}
								NOR = {
									has_trait = lustful
									has_focus = intrigue_temptation_focus
								}
								add = -100
							}
						}
						start_scheme = {
							type = seduce
							target_character = root
						}
					}
				}
				2 = { #Romance
					modifier = {
						OR = {
							has_trait = gallant
							has_focus = martial_chivalry_focus
						}
						add = 3
					}
					modifier = {
						any_targeting_scheme = { scheme_type = courting }
						factor = 0
					}
					random_in_list = {
						list = courtiers_and_guests
						limit = {
							can_set_relation_lover_trigger = { CHARACTER = root }
							accepts_incest_with_trigger = { CHARACTER = root }
							courtier_guest_management_3001_interesting_trigger = yes
							might_cheat_on_every_partner_trigger = yes
							willing_to_take_character_as_lover = { LOVER = root }
							NOR = {
								has_trait = celibate
								has_trait = infirm
								has_trait = incapable
								is_malformed_trigger = yes
								is_unattractive_trigger = yes
							}
							trigger_if = {
								limit = {
									NOT = {
										culture = { has_cultural_parameter = ai_romance_scheme_increase }
									}
								}
								ai_honor >= 0
								ai_compassion >= 0
							}
							#filter out too large an age gap
							age <= root.age_plus_10
							age >= root.age_minus_25
							trigger_if = {
								limit = { is_female = yes }
								age <= root.age
							}
							OR = {
								is_old_age_or_not_have_children = no
								has_focus = martial_chivalry_focus
							}
							can_start_scheme = {
								type = courting
								target_character = root
							}
						}
						weight = {
							base = 100
							opinion_modifier = {
								opinion_target = root
								multiplier = 5
							}
							compatibility_modifier = {
								compatibility_target = root
								multiplier = 20
							}
							modifier = {
								has_relation_potential_lover = root
								add = 500
							}
							modifier = {
								is_knight = yes
								add = 200
							}
							ai_value_modifier = {
								ai_honor = 2
								ai_sociability = 1
							}
							modifier = {
								is_consort_of = root
								add = 500
							}
							modifier = {
								OR = {
									is_clergy = yes
									has_trait = devoted
									has_council_position = councillor_court_chaplain
								}
								faith = {
									NOT = { has_doctrine_parameter = clergy_can_marry }
								}
								add = -100
							}
						}
						start_scheme = {
							type = courting
							target_character = root
						}
					}
				}
				96 = { } #Nothing
			}
		}

		# Every courtier gets a chance to do something (relations also include guests)
		every_in_list = {
			list = courtiers_and_guests

			save_scope_as = current_character

			random = {
				chance = 5
				modifier = { #More should happen in player's court!
					root = { is_ai = no }
					is_courtier_of = root
					add = 15
				}

				random_list = {
					### RELATIONS ###
					# Friend
					100 = {
						trigger = {
							num_of_relation_friend <= 3
							NOT = { is_in_list = already_updated }
						}
						ai_value_modifier = {
							ai_compassion = 0.8
							ai_sociability = 2
						}
						modifier = {
							num_of_relation_friend > 0
							add = -25
						}
						modifier = {
							num_of_relation_friend > 1
							add = -25
						}
						min = 0

						random_in_list = {
							limit = {
								num_of_relation_friend <= 3
								can_set_relation_friend_trigger = { CHARACTER = scope:current_character }
								NOR = {
									this = scope:current_character
									is_in_list = already_updated
								}
								trigger_if = {
									limit = { sex_opposite_of = scope:current_character }
									scope:current_character = {
										OR = {
											has_sexuality = homosexual
											has_trait = chaste
											has_trait = celibate
											NOR = {
												has_trait = adulterer
												has_trait = fornicator
												is_deviant_trigger = yes
												has_trait = lustful
												has_trait = seducer
												has_trait = rakish
												has_focus = intrigue_temptation_focus
												faith = { trait_is_virtue = lustful }
											}
										}
									}
								}
							}
							list = courtiers_and_guests
							weight = {
								base = 100
								opinion_modifier = {
									trigger = { root = { is_ai = no } }
									who = this
									opinion_target = scope:current_character
									multiplier = 5
								}
								opinion_modifier = {
									trigger = { root = { is_ai = no } }
									who = scope:current_character
									opinion_target = this
									multiplier = 5
								}
								compatibility_modifier = {
									who = this
									compatibility_target = scope:current_character
									multiplier = 20
								}
								modifier = {
									has_relation_potential_friend = scope:current_character
									add = 500
								}
								modifier = {
									factor = 1.5
									is_knight = yes
									scope:current_character = { is_knight = yes }
								}
								modifier = {
									factor = 0.75
									OR = {
										age > scope:current_character.age_plus_3
										age < scope:current_character.age_minus_3
									}
								}
								modifier = {
									factor = 0.75
									OR = {
										age > scope:current_character.age_plus_5
										age < scope:current_character.age_minus_5
									}
								}
								modifier = {
									factor = 0.5
									OR = {
										age > scope:current_character.age_plus_10
										age < scope:current_character.age_minus_10
									}
								}
								modifier = {
									factor = 0.5
									is_lowborn = yes
								}
								modifier = {
									factor = 0.25
									sex_opposite_of = scope:current_character
									scope:current_character = {
										NOR = {
											has_sexuality = homosexual
											has_trait = chaste
											has_trait = celibate
										}
									}
								}
								min = 0
							}
							scope:current_character = { add_to_list = already_updated }
							add_to_list = already_updated
							set_random_friendship_reason = { TARGET = scope:current_character }
						}
					}
					# Lover - for AI courtiers
					50 = {
						trigger = {
							root = { is_ai = yes }
							is_courtier_guest_management_lover_courtier = yes
						}
						ai_value_modifier = {
							ai_sociability = 1
							ai_honor = -0.5
						}
						modifier = {
							likely_to_take_lover_trigger = yes
							add = 100
						}
						modifier = {
							is_married = yes
							ai_honor >= 0
							add = -150
						}
						modifier = {
							is_unattractive_trigger = yes
							factor = 0.5
						}
						modifier = {
							factor = 0.5
							is_lowborn = yes
						}
						min = 0

						random_in_list = {
							list = courtiers_and_guests
							limit = { is_courtier_guest_management_lover_target = yes }
							weight = {
								base = 10
								compatibility_modifier = {
									who = this
									compatibility_target = scope:current_character
									multiplier = 5
								}
								modifier = {
									is_married = yes
									primary_spouse = {
										OR = {
											has_relation_rival = scope:current_character
											has_relation_nemesis = scope:current_character
										}
									}
									add = 100
								}
								modifier = {
									has_relation_crush = scope:current_character
									add = 250
								}
								modifier = {
									has_relation_potential_lover = scope:current_character
									add = 500
								}
								modifier = {
									add = {
										value = attraction
										divide = high_positive_attraction
										multiply = 50
									}
								}
								modifier = {
									likely_to_take_lover_trigger = yes
									factor = 2
								}
								modifier = {
									factor = 0.5
									scope:current_character = { is_female = no }
									is_female = yes
									NOT = { has_trait = beauty_good }
									OR = {
										age >= 25
										age >= scope:current_character.age
									}
								} 
								min = 0
							}

							add_to_list = already_updated
							scope:current_character = { add_to_list = already_updated }
							set_random_lover_reason = { TARGET = scope:current_character }
						}
					}
					# Lover - for player courtiers
					50 = {
						trigger = {
							root = { is_ai = no }
							is_courtier_guest_management_lover_courtier = yes
						}
						modifier = {
							has_sexuality = homosexual
							add = 50
						}
						ai_value_modifier = {
							ai_sociability = 1
							ai_honor = -0.5
						}
						modifier = {
							likely_to_take_lover_trigger = yes
							add = 100
						}
						modifier = {
							is_unattractive_trigger = yes
							factor = 0.5
						}
						modifier = {
							factor = 0.5
							is_lowborn = yes
						}
						min = 0
						
						every_in_list = {
							list = courtiers_and_guests
							limit = { is_courtier_guest_management_lover_target = yes }
							
							# first figure out how much the current character is attracted to them
							save_temporary_scope_as = candidate
							scope:current_character = {
								save_temporary_opinion_value_as = { name = match_opinion target = scope:candidate }
								save_temporary_scope_as = match
							}
							set_variable = { name = sexual_interest_value value = sexual_attraction_value days = 1 }
							
							# now how much they're attracted to the current character
							scope:current_character = { save_scope_as = candidate }
							save_temporary_opinion_value_as = { name = match_opinion target = scope:candidate }
							save_temporary_scope_as = match
							
							# they both need to be above 0
							if = {
								limit = {
									var:sexual_interest_value > 0
									scope:candidate.sexual_attraction_value > 0
								}
								change_variable = { name = sexual_interest_value add = scope:candidate.sexual_attraction_value }
								add_to_list = potential_lovers
							}
						}
						
						if = {
							limit = {
								list_size = { name = potential_lovers value > 0 }
							}
							scope:current_character = { save_temporary_scope_as = candidate }
							ordered_in_list = {
								list = potential_lovers
								order_by = {
									value = var:sexual_interest_value
									if = {
										limit = { has_relation_potential_lover = scope:current_character }
										add = 50
									}
									if = {
										limit = { has_relation_crush = scope:current_character }
										add = 25
									}
									if = {
										limit = { likely_to_take_lover_trigger = yes }
										add = 25
									}
								}
								add_to_list = already_updated
								scope:current_character = { add_to_list = already_updated }
								set_random_lover_reason = { TARGET = scope:current_character }
							}
							
							# now clear that list for the next character
							every_in_list = {
								list = potential_lovers
								remove_from_list = potential_lovers
							}
						}
					}
					# Rival - for AI courtiers
					50 = {
						trigger = {
							root = { is_ai = yes }
							num_of_relation_rival <= 1
							ai_vengefulness >= low_negative_ai_value
							NOT = { is_in_list = already_updated }
						}
						ai_value_modifier = {
							ai_compassion = -1
							ai_vengefulness = 2
						}
						modifier = {
							num_of_relation_rival > 0
							add = -25
						}
						min = 0

						random_in_list = {
							limit = {
								num_of_relation_rival <= 1
								can_set_relation_rival_trigger = { CHARACTER = scope:current_character }
								NOR = {
									this = scope:current_character
									is_in_list = already_updated
								}
							}
							list = courtiers_and_guests
							weight = {
								base = 10
								compatibility_modifier = {
									who = this
									compatibility_target = scope:current_character
									multiplier = -10
								}
								modifier = {
									has_relation_potential_rival = scope:current_character
									add = 500
								}
								modifier = {
									factor = 1.5
									is_knight = yes
									scope:current_character = { is_knight = yes }
								}
								modifier = {
									factor = 0.5
									sex_opposite_of = scope:current_character
								}
								modifier = {
									factor = 0.75
									OR = {
										age > scope:current_character.age_plus_3
										age < scope:current_character.age_minus_3
									}
								}
								modifier = {
									factor = 0.75
									OR = {
										age > scope:current_character.age_plus_5
										age < scope:current_character.age_minus_5
									}
								}
								modifier = {
									factor = 0.5
									OR = {
										age > scope:current_character.age_plus_10
										age < scope:current_character.age_minus_10
									}
								}
								min = 1
							}
							scope:current_character = { add_to_list = already_updated }
							add_to_list = already_updated
							set_random_rivalry_reason = {
								TARGET = scope:current_character
								RELATION = rival
							}
						}
					}
					# Rival - for player courtiers
					50 = {
						trigger = {
							root = { is_ai = no }
							num_of_relation_rival < 2
							OR = {
								ai_vengefulness > 0
								is_hot_headed_trigger = yes
							}
							NOT = { is_in_list = already_updated }
							root = {
								any_in_list = {
									list = courtiers_and_guests
									num_of_relation_rival < 2
									can_set_relation_rival_trigger = { CHARACTER = scope:current_character }
									NOR = {
										this = scope:current_character
										is_in_list = already_updated
									}
									trigger_if = {
										limit = { scope:current_character = { is_adult = no } }
										is_adult = no
										age <= scope:current_character.age_plus_3
										age >= scope:current_character.age_minus_3
									}
									trigger_else = {
										is_adult = yes
									}
									is_courtier_guest_management_player_rival_target = yes
								}
							}
						}
						ai_value_modifier = {
							ai_compassion = -1
							ai_vengefulness = 2
						}
						modifier = {
							num_of_relation_rival > 0
							factor = 0.5
						}
						min = 0
						
						random_in_list = {
							list = courtiers_and_guests
							limit = {
								num_of_relation_rival < 2
								can_set_relation_rival_trigger = { CHARACTER = scope:current_character }
								NOT = { this = scope:current_character }
								trigger_if = {
									limit = { scope:current_character = { is_adult = no } }
									is_adult = no
									age <= scope:current_character.age_plus_3
									age >= scope:current_character.age_minus_3
								}
								trigger_else = {
									is_adult = yes
								}
								is_courtier_guest_management_player_rival_target = yes
							}
							weight = {
								base = 1
								opinion_modifier = {
									who = this
									opinion_target = scope:current_character
									multiplier = -0.5
								}
								opinion_modifier = {
									who = scope:current_character
									opinion_target = this
									multiplier = -0.5
								}
								compatibility_modifier = {
									who = this
									compatibility_target = scope:current_character
									multiplier = -5
								}
								modifier = {
									has_relation_potential_rival = scope:current_character
									add = 100
								}
								modifier = {
									save_temporary_scope_as = check_potential_rival_courtier
									scope:current_character = { has_revenge_motivation_against = { CHARACTER = scope:check_potential_rival_courtier } }
									add = 250
								}
								modifier = {
									save_temporary_scope_as = check_potential_rival_courtier
									scope:current_character = { has_relation_bully = scope:check_potential_rival_courtier }
									add = 50
								}
								modifier = {
									save_temporary_scope_as = check_potential_rival_courtier
									scope:current_character = { has_relation_grudge = scope:check_potential_rival_courtier }
									add = 50
								}
								modifier = {
									save_temporary_scope_as = check_potential_rival_courtier
									scope:current_character = { is_jealous_of_their_lover = { CHARACTER = scope:check_potential_rival_courtier } }
									add = 50
								}
								modifier = {
									factor = 1.5
									is_knight = yes
									scope:current_character = { is_knight = yes }
								}
								modifier = {
									factor = 0.5
									sex_opposite_of = scope:current_character
								}
								modifier = {
									factor = 0.75
									OR = {
										age > scope:current_character.age_plus_3
										age < scope:current_character.age_minus_3
									}
								}
								modifier = {
									factor = 0.75
									OR = {
										age > scope:current_character.age_plus_5
										age < scope:current_character.age_minus_5
									}
								}
								modifier = {
									factor = 0.5
									OR = {
										age > scope:current_character.age_plus_10
										age < scope:current_character.age_minus_10
									}
								}
								modifier = {
									factor = 0.5
									is_lowborn = yes
									scope:current_character = { is_lowborn = no }
								}
								modifier = {
									factor = 0.5
									is_lowborn = no
									scope:current_character = { is_lowborn = yes }
								}
								min = 1
							}
						
							add_to_list = already_updated
							scope:current_character = { add_to_list = already_updated }
							save_temporary_scope_as = selected_rival_courtier
							
							set_random_rivalry_reason = {
								TARGET = scope:current_character
								RELATION = rival
							}
						}
					}
					# Best friend
					50 = {
						trigger = {
							num_of_relation_best_friend < 1
							num_of_relation_friend > 1
							NOT = { is_in_list = already_updated }
							any_relation = {
								type = friend
								courtier_guest_management_3001_best_friend_trigger = yes
								can_set_relation_best_friend_trigger = { CHARACTER = scope:current_character }
								NOT = { is_in_list = already_updated }
							}
						}
						modifier = {
							num_of_relation_friend > 2
							add = 50
						}
						ai_value_modifier = {
							ai_compassion = 0.5
							ai_sociability = 1
						}
						min = 0
						random_relation = {
							type = friend
							limit = {
								courtier_guest_management_3001_best_friend_trigger = yes
								can_set_relation_best_friend_trigger = { CHARACTER = scope:current_character }
								NOT = { is_in_list = already_updated }
							}
							scope:current_character = { add_to_list = already_updated }
							add_to_list = already_updated
							set_relation_best_friend = { reason = best_friend_bonded_at_court copy_reason = friend target = scope:current_character involved_character = root.liege }
						}
					}
					# Soulmate with lover/consort
					25 = {
						trigger = {
							is_appropriate_ai_courtship_courtier = yes
							NOT = { is_in_list = already_updated }
							OR = {
								any_consort = {
									is_in_list = courtiers_and_guests
									save_temporary_scope_as = potential_soulmate
									is_appropriate_ai_courtship_target = { LOVER = scope:current_character TARGET = scope:potential_soulmate }
									courtier_guest_management_3001_soulmate_trigger = yes
									NOT = { is_in_list = already_updated }
								}
								any_relation = {
									type = lover
									is_in_list = courtiers_and_guests
									save_temporary_scope_as = potential_soulmate
									is_appropriate_ai_courtship_target = { LOVER = scope:current_character TARGET = scope:potential_soulmate }
									courtier_guest_management_3001_soulmate_trigger = yes
									NOT = { is_in_list = already_updated }
								}
							}
						}
						ai_value_modifier = {
							ai_compassion = 0.5
							ai_honor = 0.5
							max = 50
							min = -25
						}
						random_in_list = {
							limit = {
								OR = {
									is_consort_of = scope:current_character
									has_relation_lover = scope:current_character
								}
								save_temporary_scope_as = potential_soulmate
								is_appropriate_ai_courtship_target = { LOVER = scope:current_character TARGET = scope:potential_soulmate }
								courtier_guest_management_3001_soulmate_trigger = yes
								NOT = { is_in_list = already_updated }
							}
							list = courtiers_and_guests
							weight = {
								base = 100
								modifier = {
									factor = 2
									is_consort_of = scope:current_character
								}
							}
							add_to_list = already_updated
							scope:current_character = { add_to_list = already_updated }
							set_relation_soulmate = { reason = soulmate_love_at_court copy_reason = lover target = scope:current_character }
						}
					}
					# Nemesis
					25 = {
						modifier = {
							add = 25
							ai_vengefulness >= high_positive_ai_value
						}
						trigger = {
							num_of_relation_nemesis < 1
							NOT = { is_in_list = already_updated }
							any_relation = {
								type = rival
								courtier_guest_management_3001_nemesis_trigger = yes
								can_set_relation_nemesis_trigger = { CHARACTER = scope:current_character }
							}
						}
						random_relation = {
							type = rival
							limit = {
								courtier_guest_management_3001_nemesis_trigger = yes
								can_set_relation_nemesis_trigger = { CHARACTER = scope:current_character }
								NOT = { is_in_list = already_updated }
							}
							scope:current_character = { add_to_list = already_updated }
							add_to_list = already_updated
							set_relation_nemesis = {
								reason = nemesis_at_court
								copy_reason = rival
								target = scope:current_character
							}
						}
					}
					# Secrets - for AI courtiers
					20 = {
						trigger = {
							root = { is_ai = yes }
							has_any_secrets = no
							NOT = { is_in_list = already_updated }
						}
						courtier_guest_pool_character_secret_giving_effect = yes
						add_to_list = already_updated
					}
					# Advancement - for player courtiers
					20 = {
						trigger = {
							root = { is_ai = no }
							NOT = { is_in_list = already_updated }
						}
						lmf_courtier_advancement_effect = { CHARACTER = scope:current_character }
						add_to_list = already_updated
					}
					# Fallthrough
					1 = {}
				}
			}

		}
		
		# players run this twice for their court
		if = {
			limit = { has_character_flag = double_check }
			remove_character_flag = double_check
		}
		else_if = {
			limit = { is_ai = no }
			add_character_flag = double_check
			# empty out the lists first
			every_in_list = {
				list = courtiers_and_guests
				remove_from_list = courtiers_and_guests
			}
			every_in_list = {
				list = already_updated
				remove_from_list = already_updated
			}
			trigger_event = courtier_guest_management.3001
		}
	}
}

############################
# WANDERER INTO ADVENTURER #
############################

courtier_guest_management.4001 = {
	hidden = yes
	trigger = {
		has_ep3_dlc_trigger = yes
		is_pool_character = yes # Can cease to be in the pool between it queuing and executing
		ai_can_valid_to_create_laamp_trigger = yes
		is_valid_for_laampdom = yes
		OR = {
			has_character_flag = easteregg
			has_trait = historical_character
			culture = {
				has_cultural_parameter = much_more_likely_to_be_laamps
			}
			AND = {
				is_lowborn = no
				culture = {
					has_cultural_parameter = more_likely_to_be_laamps
				}
			}
			any_claim = { tier >= tier_kingdom }
		}
		is_ai = yes
		pool_character_is_pruneable_trigger = no # Avoid having uninteresting characters become Adventurers
		NOT = {
			any_heir_title = { }
		}
	}

	immediate = {
		random = {
			chance = 0
			modifier = { add = ep3_laamp_chance_score_value }
			#Counteract bonus in above value somewhat
			modifier = {
				culture = { has_cultural_parameter = more_likely_to_be_laamps }
				add = -75
			}
			modifier = {
				has_trait = historical_character
				add = 150
			}
			create_landless_adventurer_title_effect = {
				REASON = flag:wanderer
				FLAVOR_CHAR = root
			}
			trigger_event = ep3_laamps.0003
			if = { # Debug AI tracking
				limit = { debug_only = yes }
				if = {
					limit = { exists = global_var:ai_wanderer_laamp_count }
					change_global_variable = {
						name = ai_wanderer_laamp_count
						add = 1
					}
				}
				else = {
					set_global_variable = {
						name = ai_wanderer_laamp_count
						value = 1
					}
				}
				debug_log = "AI became adventurer due to cultural tradition"
				debug_log_scopes = yes
			}
		}	
	}
}

