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 #				
############################

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

	trigger = {
		any_in_list = {
			list = characters
			is_child_of = scope:liege
		}
	}

	immediate = {
		#Send message about child about to leave
		every_in_list = {
			list = characters
			limit = { is_child_of = 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 = {
			move_to_pool_effect = yes
		}
	}
}


#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 = { select_and_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 = { select_and_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 = {
		select_and_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 = bastard
							has_trait = wild_oat
						}
						any_parent = { this = scope:host }
					}
					any_traveling_family_member = {
						OR = {
							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

				# Now a month out of date, so lets update it
				select_pool_to_move_to_effect = yes
			}
			move_to_pool_effect = yes
		}
	}
}

# Maintenance - Force move to pool
courtier_guest_management.0999 = {
	hidden = yes
	
	immediate = {
		select_and_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 void?
		if = {
			limit = { NOT = { exists = location } }

			#Oops, no good! Go someplace. (family is also moved with this effect)
			fallback_move_to_pool_effect = yes
		}

		#Don't I have the "last_visited_ruler" variable? (= it ran out or I'm from some weird source that doesn't set var)
		if = {
			limit = { NOT = { exists = var:last_visited_ruler } }

			if = { # If character is prune-able, prune.
				limit = {
					pool_character_is_pruneable_trigger = yes
				}
				if = {
					limit = { 
						culture = {
							has_cultural_tradition = tradition_diasporic
						}
						is_adult = yes
						is_lowborn = no
						age < 45
					}
					random_list = {
						10 = {
							create_landless_minority_title_effect = {
								REASON = flag:voluntary
								FLAVOR_CHAR = scope:laamp_inheritor
							}
						}
						60 = {
						}
						30 = {
							death = { death_reason = death_accident }
						}
					}
				}
				else_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
			}
		}

		if = {
			limit = { 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
			}

			#Is my pool full? Then move
			if = {
				limit = {
					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 >= high_positive_opinion
	}
	reverse_opinion = {
		target = scope:current_character
		value >= high_positive_opinion
	}
}

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

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

scripted_trigger courtier_guest_management_3001_interesting_trigger = {
	OR = {
		is_lowborn = no
		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
	}
}

courtier_guest_management.3001 = {
	hidden = yes

	immediate = {
		every_courtier_or_guest = {
			limit = {
				age >= childhood_education_start_age
				NOT = { has_trait = incapable }
			}
			add_to_list = courtiers_and_guests	
		}	

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

			save_scope_as = root_scope
			random_list = {
				2 = { #Seduce
					modifier = {
						OR = {
							has_trait = lustful
							has_trait = seducer
							has_focus = intrigue_temptation_focus
						}
						add = 3
					}
					random_in_list = {
						list = courtiers_and_guests
						limit = {
							save_temporary_scope_as = instigating_courtier
							are_characters_sensible_lovers_trigger = { INSTIGATING_LOVER = scope:instigating_courtier TARGET_OF_LOVE = scope:root_scope }
							can_start_scheme = {
								type = seduce
								target_character = root
							}
						}
						weight = {
							base = 100
							opinion_modifier = {
								opinion_target = root
								multiplier = 5
							}
							compatibility_modifier = {
								compatibility_target = root
								multiplier = 20
							}
							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
							}
						}
						start_scheme = {
							type = seduce
							target_character = root
						}
					}
				}
				2 = { #Romance
					modifier = {
						OR = {
							has_trait = gallant
							has_focus = martial_chivalry_focus
						}
						add = 3
					}
					random_in_list = {
						list = courtiers_and_guests
						limit = {
							save_temporary_scope_as = instigating_courtier
							are_characters_sensible_and_appropriate_open_lovers_trigger = { INSTIGATING_LOVER = scope:instigating_courtier TARGET_OF_LOVE = scope:root_scope }
							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
							}
						}
						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
			limit = { NOT = { is_in_list = already_updated } } #If another character set a relation with you, you don't get to run your own update. (However, we don't want to block you from being selected by other characters if you are the best alternative.)

			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 = {
						ai_value_modifier = {
							ai_compassion = 0.8
							ai_sociability = 2
						}
						trigger = {
							any_relation = {
								type = friend
								count <= 2
							}
						}

						random_in_list = {
							limit = {
								can_set_relation_friend_trigger = { CHARACTER = scope:current_character }
							}
							list = courtiers_and_guests
							weight = {
								base = 100
								compatibility_modifier = {
									who = this
									compatibility_target = scope:current_character
									multiplier = 20
								}
								modifier = {
									has_relation_potential_friend = scope:current_character
									add = 500
								}
							}

							add_to_list = already_updated
							set_random_friendship_reason = { TARGET = scope:current_character }
						}
					}
					# Lover
					50 = {
						ai_value_modifier = {
							ai_sociability = 1.5
						}
						modifier = {
							has_trait = lustful
							add = 150
						}
						modifier = {
							OR = {
								any_relation = { type = soulmate always = yes }
								is_married = yes
							}
							ai_honor >= 0
							add = -150
						}
						modifier = {
							add = {
								value = attraction
								divide = high_positive_attraction
								multiply = 50
							}
						}
						trigger = {
							is_married = no
							is_concubine = no
							might_cheat_on_every_partner_trigger = yes
							NOR = {
								has_trait = chaste
								has_trait = celibate
								has_sexuality = asexual
							}
							trigger_if = {
								limit = {
									is_courtier = yes
									is_close_or_extended_family_of = liege
								}
								OR = {
									has_trait = lustful
									is_deviant_trigger = yes
								}
							}
							any_relation = {
								type = lover
								count < 1
							}
						}

						random_in_list = {
							limit = {
								is_married = no
								is_concubine = no
								might_cheat_on_every_partner_trigger = yes
								NOR = {
									has_trait = chaste
									has_trait = celibate
									has_sexuality = asexual
								}
								any_relation = {
									type = lover
									count < 1
								}
								trigger_if = {
									limit = {
										is_courtier = yes
										is_close_or_extended_family_of = liege
									}
									scope:current_character = { is_lowborn = no }
									OR = {
										is_deviant_trigger = yes
										has_trait = lustful
										trait_compatibility = {
											target = scope:current_character
											value >= high_positive_trait_compatibility
										}
									}
								}
								this != scope:current_character
								can_set_relation_lover_trigger = { CHARACTER = scope:current_character }
								is_attracted_to_gender_of = scope:current_character
								scope:current_character = { is_attracted_to_gender_of = prev }
								age <= scope:current_character.age_plus_5
								age >= scope:current_character.age_minus_10
								trait_compatibility = {
									target = scope:current_character
									value >= low_positive_trait_compatibility
								}
								save_temporary_scope_as = incest_check
								accepts_incest_with_each_other_trigger = {
									CHARACTER_1 = scope:incest_check
									CHARACTER_2 = scope:current_character
								}
							}
							list = courtiers_and_guests
							weight = {
								base = 100
								compatibility_modifier = {
									who = this
									compatibility_target = scope:current_character
									multiplier = 20
								}
								modifier = {
									has_relation_potential_lover = scope:current_character
									add = 500
								}
								modifier = {
									add = {
										value = attraction
										divide = high_positive_attraction
										multiply = 50
									}
								}
								modifier = {
									has_trait = lustful
									factor = 2
								}
							}

							add_to_list = already_updated

							set_random_lover_reason = { TARGET = scope:current_character }
						}
					}
					
					# Rival
					50 = {
						ai_value_modifier = {
							ai_compassion = -1
							ai_vengefulness = 2
						}
						trigger = {
							any_relation = {
								type = rival
								count <= 1
							}
							ai_vengefulness >= low_negative_ai_value
						}

						random_in_list = {
							limit = {
								can_set_relation_rival_trigger = { CHARACTER = scope:current_character }
							}
							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_rival = scope:current_character
									add = 500
								}
							}

							add_to_list = already_updated
							set_random_rivalry_reason = {
								TARGET = scope:current_character
								RELATION = rival
							}
						}
					}
					# Best friend
					50 = {
						trigger = {
							any_relation = {
								type = friend
								courtier_guest_management_3001_best_friend_trigger = yes
								can_set_relation_best_friend_trigger = { CHARACTER = scope:current_character }
							}
						}
						random_relation = {
							type = friend
							limit = {
								courtier_guest_management_3001_best_friend_trigger = yes
								can_set_relation_best_friend_trigger = { CHARACTER = scope:current_character }
							}

							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
					25 = {
						trigger = {
							any_relation = {
								type = lover
								courtier_guest_management_3001_soulmate_trigger = yes
								can_set_relation_soulmate_trigger = { CHARACTER = scope:current_character }
							}
						}
						random_relation = {
							type = lover
							limit = {
								courtier_guest_management_3001_soulmate_trigger = yes
								can_set_relation_soulmate_trigger = { CHARACTER = 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 = {
						trigger = {
							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 }
							}

							add_to_list = already_updated
							set_relation_nemesis = {
								reason = nemesis_at_court
								copy_reason = rival
								target = scope:current_character
							}
						}
					}

					### SECRETS ###
					30 = {
						trigger = {
							NOT = { any_secret = { always = yes } }
							is_adult = yes
						}
						courtier_guest_pool_character_secret_giving_effect = yes
					}
				}
			}

		}	
	}
}

############################
# 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 }
		}
		pool_character_is_pruneable_trigger = no # Avoid having uninteresting characters become Adventurers
		is_ai = yes
		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
			}
		}	
	}
}

courtier_guest_management.4002 = {
	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_tradition = tradition_diasporic }
			AND = {
				is_lowborn = no
				culture = { has_cultural_tradition = tradition_diasporic }
			}
			any_claim = { tier >= tier_kingdom }
		}
		pool_character_is_pruneable_trigger = no # Avoid having uninteresting characters become Adventurers
		is_ai = yes
		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_tradition = tradition_diasporic }
				add = -10
			}
			modifier = {
				has_trait = historical_character
				add = 150
			}
			create_landless_minority_title_effect = {
				REASON = flag:voluntary
				FLAVOR_CHAR = root
			}
			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
			}
		}	
	}
}

