## Written by WKIII - "Court AI"
## Updated and continued by Cody Spring
namespace = court_ai

ssal = {

# Debugging ping events.
letter_event = {
	id = court_ai.1000
	is_triggered_only = yes
	option = {}
}
letter_event = {
	id = court_ai.1001
	is_triggered_only = yes
	option = {}
}

# Minor title distribution
character_event = {
	id = court_ai.40
	hide_window = yes
	only_rulers = yes
	trigger = {
		liege = {
			primary_title = { has_title_flag = court_ai_enabled }
			ai = no
			NOT = { character = ROOT }
		}
		OR = {
			AND = {
				opinion = { who = liege value = -50 }
				NOT = { opinion = { who = liege value = 50 } }
				liege = { primary_title = { has_title_flag = court_ai_minor_titles } }
				OR = {
					can_hold_title = title_court_physician
					can_hold_title = title_master_of_the_hunt
					can_hold_title = title_master_of_the_horse
					can_hold_title = title_keeper_of_swans
					can_hold_title = title_high_almoner
					can_hold_title = title_cupbearer
					can_hold_title = title_seneschal
					can_hold_title = title_court_eunuch
					can_hold_title = title_court_dwarf
					can_hold_title = title_despot
					can_hold_title = title_sebastokrator
					can_hold_title = title_caesar
					can_hold_title = title_kouropalates
					can_hold_title = title_anthypatos
					can_hold_title = title_imperial_eunuch
					can_hold_title = title_court_calligrapher
					can_hold_title = title_court_musician
					can_hold_title = title_court_poet
					can_hold_title = title_chief_architect
					can_hold_title = title_chief_qadi
					can_hold_title = title_state_inquisitor
					can_hold_title = title_high_admiral
					can_hold_title = title_high_judge
					can_hold_title = title_volva
					can_hold_title = title_lawspeaker
					can_hold_title = title_drottseti
					can_hold_title = title_court_skald
					can_hold_title = title_marksman
					can_hold_title = title_hirdman
					can_hold_title = title_cherbi
					can_hold_title = title_darkhan
					can_hold_title = title_noyan
					can_hold_title = title_behi
					can_hold_title = title_master_of_the_eagle
					can_hold_title = title_shahrwaraz
					can_hold_title = title_kardarigan
					can_hold_title = title_shahin
					can_hold_title = title_pushtigban_salar
					can_hold_title = title_spahbod
					can_hold_title = title_eran_ambaragbed
					can_hold_title = title_champion
					can_hold_title = title_master_of_the_blade
					can_hold_title = title_master_of_the_bow
					can_hold_title = title_venerable_elder
					can_hold_title = title_yabgu
					can_hold_title = title_ishad
					can_hold_title = title_kundur
					can_hold_title = title_master_of_the_royal_elephants
					can_hold_title = title_royal_builder
					can_hold_title = title_court_poet_india
					can_hold_title = title_guru
				}
			}
		}
	}
	mean_time_to_happen = {
		months = 1
		modifier = {
			factor = 0.5
			is_ruler = yes
			tier = count
		}
		modifier = {
			factor = 0.1
			is_ruler = yes
			tier = duke
		}
		modifier = {
			factor = 0.01
			is_ruler = yes
			tier = king
		}
	}

	immediate = {
	#Check for AI
		if = {
			limit = {
				liege = {
					primary_title = {
						NOT = { has_title_flag = court_ai_minor_titles }
					}
				}
			}
			break = yes
		}
	#Court Physician
		if = {
			limit = { 
				learning > 21
				opinion = { who = liege value = 60 }
				can_hold_title = title_court_physician } 
				liege = { ROOT = { give_minor_title = title_court_physician } 
				} break = yes 
			 }
	#General
		if = { 
			limit = { 
				is_ruler = yes
				can_hold_title = title_master_of_the_hunt } 
				liege = { ROOT = { give_minor_title = title_master_of_the_hunt } 
				} break = yes 
			 }
		if = { 
			limit = { 
				is_ruler = yes
				can_hold_title = title_master_of_the_horse } 
				liege = { ROOT = { give_minor_title = title_master_of_the_horse } 
				} break = yes 
			 }
		if = { 
			limit = { 
				is_ruler = yes
				can_hold_title = title_keeper_of_swans } 
				liege = { ROOT = { give_minor_title = title_keeper_of_swans } 
				} break = yes 
			 }
		if = {	
			limit = { 
				is_ruler = yes
				can_hold_title = title_high_almoner } 
				liege = { ROOT = { give_minor_title = title_high_almoner } 
				} break = yes 
			 }
		if = { 
			limit = { 
				is_ruler = yes
				can_hold_title = title_cupbearer } 
				liege = { ROOT = { give_minor_title = title_cupbearer } 
				} break = yes 
			 }
		if = { limit = { is_ruler = yes can_hold_title = title_seneschal } liege = { ROOT = { give_minor_title = title_seneschal } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_court_eunuch } liege = { ROOT = { give_minor_title = title_court_eunuch } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_court_dwarf } liege = { ROOT = { give_minor_title = title_court_dwarf } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_despot } liege = { ROOT = { give_minor_title = title_despot } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_sebastokrator } liege = { ROOT = { give_minor_title = title_sebastokrator } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_caesar } liege = { ROOT = { give_minor_title = title_caesar } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_kouropalates } liege = { ROOT = { give_minor_title = title_kouropalates } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_anthypatos } liege = { ROOT = { give_minor_title = title_anthypatos } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_imperial_eunuch } liege = { ROOT = { give_minor_title = title_imperial_eunuch } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_court_calligrapher } liege = { ROOT = { give_minor_title = title_court_calligrapher } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_court_musician } liege = { ROOT = { give_minor_title = title_court_musician } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_court_poet } liege = { ROOT = { give_minor_title = title_court_poet } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_chief_architect } liege = { ROOT = { give_minor_title = title_chief_architect } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_chief_qadi } liege = { ROOT = { give_minor_title = title_chief_qadi } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_state_inquisitor } liege = { ROOT = { give_minor_title = title_state_inquisitor } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_high_admiral } liege = { ROOT = { give_minor_title = title_high_admiral } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_high_judge } liege = { ROOT = { give_minor_title = title_high_judge } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_volva } liege = { ROOT = { give_minor_title = title_volva } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_lawspeaker } liege = { ROOT = { give_minor_title = title_lawspeaker } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_drottseti } liege = { ROOT = { give_minor_title = title_drottseti } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_court_skald } liege = { ROOT = { give_minor_title = title_court_skald } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_marksman } liege = { ROOT = { give_minor_title = title_marksman } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_hirdman } liege = { ROOT = { give_minor_title = title_hirdman } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_cherbi } liege = { ROOT = { give_minor_title = title_cherbi } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_darkhan } liege = { ROOT = { give_minor_title = title_darkhan } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_noyan } liege = { ROOT = { give_minor_title = title_noyan } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_behi } liege = { ROOT = { give_minor_title = title_behi } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_master_of_the_eagle } liege = { ROOT = { give_minor_title = title_master_of_the_eagle } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_shahrwaraz } liege = { ROOT = { give_minor_title = title_shahrwaraz } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_kardarigan } liege = { ROOT = { give_minor_title = title_kardarigan } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_shahin } liege = { ROOT = { give_minor_title = title_shahin } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_pushtigban_salar } liege = { ROOT = { give_minor_title = title_pushtigban_salar } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_spahbod } liege = { ROOT = { give_minor_title = title_spahbod } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_eran_ambaragbed } liege = { ROOT = { give_minor_title = title_eran_ambaragbed } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_champion } liege = { ROOT = { give_minor_title = title_champion } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_master_of_the_blade } liege = { ROOT = { give_minor_title = title_master_of_the_blade } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_master_of_the_bow } liege = { ROOT = { give_minor_title = title_master_of_the_bow } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_venerable_elder } liege = { ROOT = { give_minor_title = title_venerable_elder } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_yabgu } liege = { ROOT = { give_minor_title = title_yabgu } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_ishad } liege = { ROOT = { give_minor_title = title_ishad } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_kundur } liege = { ROOT = { give_minor_title = title_kundur } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_master_of_the_royal_elephants } liege = { ROOT = { give_minor_title = title_master_of_the_royal_elephants } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_royal_builder } liege = { ROOT = { give_minor_title = title_royal_builder } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_court_poet_india } liege = { ROOT = { give_minor_title = title_court_poet_india } } break = yes }
	#CK2Plus Minor Titles
		if = { limit = { is_ruler = yes can_hold_title = title_pagan_wizard } liege = { ROOT = { give_minor_title = title_pagan_wizard } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_dregvant } liege = { ROOT = { give_minor_title = title_dregvant } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_ashavan } liege = { ROOT = { give_minor_title = title_ashavan } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_sraoshavarez } liege = { ROOT = { give_minor_title = title_sraoshavarez } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_shapsheraz } liege = { ROOT = { give_minor_title = title_shapsheraz } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_title_uchelwr } liege = { ROOT = { give_minor_title = title_uchelwr } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_laoch } liege = { ROOT = { give_minor_title = title_laoch } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_royal_champion } liege = { ROOT = { give_minor_title = title_royal_champion } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_vates } liege = { ROOT = { give_minor_title = title_vates } } break = yes }
		if = { limit = { can_hold_title = title_court_bard } liege = { ROOT = { give_minor_title = title_court_bard } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_censor } liege = { ROOT = { give_minor_title = title_censor } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_aedile } liege = { ROOT = { give_minor_title = title_aedile } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_magister_equitum } liege = { ROOT = { give_minor_title = title_magister_equitum } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_vestalis } liege = { ROOT = { give_minor_title = title_vestalis } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_drottseti } liege = { ROOT = { give_minor_title = title_drottseti } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_vestalis_maxima } liege = { ROOT = { give_minor_title = title_vestalis_maxima } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_flamen_dialis } liege = { ROOT = { give_minor_title = title_flamen_dialis } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_title_flamen_martialis } liege = { ROOT = { give_minor_title = title_flamen_martialis } } break = yes }
		if = { limit = { is_ruler = yes can_hold_title = title_flamen_quirinalis } liege = { ROOT = { give_minor_title = title_flamen_quirinalis } } break = yes }
	}
}

# Hermetic Apprentice
character_event = {
	id = court_ai.40
	hide_window = yes
	ai = no
	trigger = {
		liege = {
			primary_title = { has_title_flag = court_ai_enabled }
			ai = no
			NOT = { character = ROOT }
		}
	}
	mean_time_to_happen = {
		months = 6
		modifier = {
			factor = 0.2
			age < 40
			has_minor_title = title_court_physician
		}
		modifier = {
			factor = 0.01
			age < 40
			is_primary_heir = yes
		}
	}

	immediate = {
	#Check for AI
		if = {
			limit = {
				liege = {
					primary_title = {
						NOT = { has_title_flag = court_ai_minor_titles }
					}
				}
			}
			break = yes
		}
	#Apprentice
		if = {
			limit = { 
				is_primary_heir = yes
				can_hold_title = title_hermetics_apprentice }
				 
				} break = yes 
			 }
		if = {
			limit = {
				has_minor_title = title_court_physician
				can_hold_title = title_hermetics_apprentice 
				liege = { ROOT = { give_minor_title = title_hermetics_apprentice }
				} break = yes
			 }
		}
	}

# Demand gavelkind from vassals when possible:
character_event = {
	id = court_ai.12
	hide_window = yes
	ai = no
	prisoner = no
	capable_only = yes
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_demand_gavelkind
		}
		# Can demand Gavelkind:
		prestige = 200
		is_feudal = yes
		is_vice_royalty = no
		any_vassal = {
			ROOT = { is_liege_of = THIS }
			# Can be demanded to switch to Gavelkind:
			prestige = 100
			is_playable = yes
			is_law_potential = succ_gavelkind
			NOT = { has_law = succ_gavelkind }
			NOT = { has_opinion_modifier = { who = ROOT name = opinion_demanded_succ_law_change } }
			is_feudal = yes
			is_vice_royalty = no
			# Has more than one child that can inherit, & has any holdings/titles to share:
			OR = {
				owes_favor_to = ROOT
				AND = {
					NOT = { owes_favor_to = ROOT }
					OR = {
						has_law = ze_administration_laws_1
						has_law = ze_administration_laws_2
					}
				}
			}
			OR = {
				AND = {
					has_law = agnatic_succession
					any_child = {
						count = 2
						is_female = no
						NOT = {
							trait = bastard
						}
					}
				}
				AND = {
					has_law = cognatic_succession
					OR = {
						any_child = {
							count = 2
							is_female = no
							NOT = {
								trait = bastard
							}
						}
						AND = {
							any_child = {
								count = 2
								is_female = yes
								NOT = {
									trait = bastard
								}
							}
							NOT = {
								any_child = {
									is_female = no
								}
							}
						}
					}
				}
				AND = {
					has_law = true_cognatic_succession
					any_child = {
						count = 2
						NOT = {
							trait = bastard
						}
					}
				}
				AND = {
					has_law = enatic_cognatic_succession
					OR = {
						any_child = {
							count = 2
							is_female = yes
							NOT = {
								trait = bastard
							}
						}
						AND = {
							any_child = {
								count = 2
								is_female = no
								NOT = {
									trait = bastard
								}
							}
							NOT = {
								any_child = {
									is_female = yes
								}
							}
						}
					}
				}
				AND = {
					has_law = enatic_succession
					any_child = {
						count = 2
						is_female = yes
						NOT = {
							trait = bastard
						}
					}
				}
			}
			OR = {
				num_of_baron_titles = 2
				AND = { tier = count num_of_count_titles = 2 }
				AND = { tier = duke num_of_duke_titles = 2 }
			}
		}
	}
	mean_time_to_happen = { months = 1 }
	immediate = {
		random_vassal = {
			limit = {
				ROOT = { is_liege_of = THIS }
				# Can be demanded to switch to Gavelkind:
				prestige = 100
				is_playable = yes
				is_law_potential = succ_gavelkind
				NOT = { has_law = succ_gavelkind }
				NOT = { has_opinion_modifier = { who = ROOT name = opinion_demanded_succ_law_change } }
				is_feudal = yes
				is_vice_royalty = no
				prisoner = no
				NOT = { trait = incapable }
				# Has more than one child who can inherit & has any holdings/titles to share:
				OR = {
					owes_favor_to = ROOT
					AND = {
						NOT = { owes_favor_to = ROOT }
						OR = {
							has_law = ze_administration_laws_1
							has_law = ze_administration_laws_2
						}
					}
				}
				OR = {
					AND = {
						has_law = agnatic_succession
						any_child = {
							count = 2
							is_female = no
							NOT = {
								trait = bastard
							}
						}
					}
					AND = {
						has_law = cognatic_succession
						OR = {
							any_child = {
								count = 2
								is_female = no
								NOT = {
									trait = bastard
								}
							}
							AND = {
								any_child = {
									count = 2
									is_female = yes
									NOT = {
										trait = bastard
									}
								}
								NOT = {
									any_child = {
										is_female = no
									}
								}
							}
						}
					}
					AND = {
						has_law = true_cognatic_succession
						any_child = {
							count = 2
							NOT = {
								trait = bastard
							}
						}
					}
					AND = {
						has_law = enatic_cognatic_succession
						OR = {
							any_child = {
								count = 2
								is_female = yes
								NOT = {
									trait = bastard
								}
							}
							AND = {
								any_child = {
									count = 2
									is_female = no
									NOT = {
										trait = bastard
									}
								}
								NOT = {
									any_child = {
										is_female = yes
									}
								}
							}
						}
					}
					AND = {
						has_law = enatic_succession
						any_child = {
							count = 2
							is_female = yes
							NOT = {
								trait = bastard
							}
						}
					}
				}
				OR = {
					num_of_baron_titles = 2
					AND = { tier = count num_of_count_titles = 2 }
					AND = { tier = duke num_of_duke_titles = 2 }
				}
			}
			character_event = { id = court_ai.14 days = 1 }
		}
	}
}
character_event = {
	id = court_ai.14
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		prestige = -100
		if = {
			limit = { NOT = { owes_favor_to = FROM } }
			letter_event = { id = RIP.30040 days = 3 }
		}
		if = {
			limit = { owes_favor_to = FROM }
			letter_event = { id = RIP.30050 days = 3 }
		}
		opinion = {
			who = FROM
			modifier = opinion_demanded_succ_law_change
			years = 5
		}
	}
}

# Discourage plotters:
character_event = {
	id = court_ai.15
	hide_window = yes
	only_rulers = yes
	ai = no
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_discourage_plotters
		}
		ai = no
		any_known_plotter = {
			any_plot_backer = {
				any_liege = {
					character = ROOT
				}
				NOT = { rival = ROOT }
			}
		}
	}
	mean_time_to_happen = { days = 1 }
	immediate = {
		any_known_plotter = {
			limit = {
				NOT = { character = ROOT }
			}
			any_plot_backer = {
				limit = {
					any_liege = {
						character = ROOT
					}
					NOT = { rival = ROOT }
				}
				leave_plot = PREV
			}
		}
	}
}

# Stargazing
character_event = {
	id = court_ai.151
	hide_window = yes
	ai = no
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_stargazing
		}
		NOT = { has_character_modifier = stargazing_timer } 
		NOT = { has_character_modifier = inviting_to_stargazing } #not the problem
		prisoner = no
		NOT = { trait = incapable }
		war = no
		is_adult = yes
	}
	mean_time_to_happen = { month = 1 }
	immediate = {
		random_realm_character = {
			limit = {
				has_minor_title = title_hermetics_apprentice
				prisoner = no
				NOT = { trait = incapable }
			}
			character_event = { id = court_ai.152 }
		}
	}
}
character_event = { #on apprentice
	id = court_ai.152
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		FROM = {
			character_event = { id = court_ai.153 }
		}
	}
}
character_event = { #back on liege
	id = court_ai.153
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		set_character_flag = inviting_guests_to_stargazing_timer
		set_character_flag = do_not_disturb
		FROM = { #from refers to apprentice
			save_event_target_as = stargazing_guest
			hidden_tooltip = {
				set_variable = {
					which = stargazing_satisfaction value = 0
				}
			}
			character_event = { id = court_ai.154 }
		}
		add_character_modifier = {
		modifier = inviting_to_stargazing
		duration = 182
		hidden = yes
		}
	}
}
character_event = { #back to apprentice one more time
	id = court_ai.154
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			hidden_tooltip = {
				letter_event = { id = MNM.1920 days = 7 }
			}
		}
	}
}

# Carousing
character_event = {
	id = court_ai.160
	hide_window = yes
	only_rulers = yes
	only_playable = yes
	ai = no
	war = no
	prisoner = no
	capable_only = yes
	min_age = 16
	# Checks if the ruler you are playing can even invite people
	trigger = {
		primary_title = { # checks if you have the AI enabled
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_carousing
		}
		OR = { #checks if you are either WoL Carousing or have one of the traits
			has_focus = focus_carousing
			trait = hedonist
			trait = socializer
		}
		OR = { # Makes sure you aren't busy. Or if you are, you are busy organizing the party.
			NOT = { has_character_modifier = do_not_disturb}
			has_character_modifier = organizing_party
		}
		NOT = { had_character_flag = { flag = inviting_guests_to_party_timer days = 90} } 
		NOT = { has_character_flag = party_host } # Event WoL4030, when the party starts, sets you as host. Clears in 4800 when party is over.
		NOT = { has_character_modifier = party_timer } #checks for recent carousing modifier
		has_regent = no
		is_ill = no
		is_pregnant = no
		OR = { # If you're muslim, you can't unless you have one of the traits.
			NOT = { religion_group = muslim }
			trait = decadent
			trait = hedonist
			trait = drunkard
		}
	}
	mean_time_to_happen = { month = 3 }
	immediate = {
		any_vassal = { # this sets SCOPE for all of your vassals
			limit = { # Limits to only vassals who can actually accept a Carousing invite, and does not invite friends.
				is_adult = yes
				prisoner = no
				NOT = { is_friend = liege }
				NOT = { trait = incapable }
				NOT = { has_character_flag = carousing_invite }
				NOT = { opinion = { who = liege value = 85} }
				is_ill = no
				is_pregnant = no
				NOT = { has_character_modifier = do_not_disturb }
				NOT = { has_character_modifier = organizing_party }
				NOT = { has_character_modifier = party_carousing }
				war = no
				NOT = {
					reverse_has_opinion_modifier = {
						modifier = opinion_declined_party
						who = liege
					}
				}
			}
			character_event = { id = court_ai.161 } #Sends the vassals to the next event, used in 'ping' method to store them into "FROM"
		}
	}
}
character_event = { #Sends onto 162, so that the Liege is ROOT and FROMFROM, and the Vassal invited is FROM
	id = court_ai.161
	is_triggered_only = yes
	hide_window = yes
	trigger = {
		always = yes
		}
	immediate = {
		FROM = {
			character_event = { id = court_ai.162 }
		}
	}
}
character_event = { #back on liege
	id = court_ai.162
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		always = yes
	}
	immediate = {
		if = { # If this is the first invite, set to organizing party.
			limit = {
				NOT = { has_character_modifier = organizing_party }
			}
			add_character_modifier = {
				modifier = organizing_party 
				duration = 120
			}
			hidden_tooltip = { character_event = { id = WoL.4010 days = 120 } } # sets the party to start in 120 days.
			set_character_flag = inviting_guests_to_party_timer
		}
		add_character_modifier = {
			name = do_not_disturb
			duration = 165
			hidden = yes
		}
		FROM = { 
			character_event = { id = court_ai.163 }
		}
	}
	revoke_allowed = {
		always = no
	}
}
character_event = { #back on courtier
	id = court_ai.163
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			hidden_tooltip = {
				letter_event = { id = WoL.4000 days = 7}
			}
			set_character_flag = carousing_invite
		}
	}
}

# Ransom Prisoners
character_event = { #checks if the ruler has any foreign, non-heir, non-warring prisoners and such.
	id = court_ai.170
	hide_window = yes
	prisoner = yes
	trigger = {
		host = {
			primary_title = { #check for AI enabled
				has_title_flag = court_ai_enabled
				has_title_flag = court_ai_ransom
			}
			has_regent = no
		}
		NOT = { war_with = host } # not at war with
		NOT = { same_realm = host } # foreign
		NOT = { has_character_modifier = ransom_timer } # isn't already thinking
		is_heir = no
	}
	mean_time_to_happen = { days = 30 }
	immediate = { #select prisoner matching conditions
		if = {
			limit = {
				is_ruler = no
				is_primary_heir = no
				OR = {
					wealth = 10
					any_liege = { is_close_relative = yes }
					AND = { # if family member is liege
						dynasty = any_liege
						any_liege = { wealth = 10 }
						is_primary_heir = no
					}
					AND = { # if liege likes you
						opinion = { who = liege value = 50 }
						any_liege = { wealth = 10 }
						is_primary_heir = no
					}
				}
			}
			wealth = -10
			host = {wealth = 10 }
			prisoner = no
		}
		if = {
			limit = {
				is_ruler = yes
				tier = baron
				wealth = 25
			}
			wealth = -25
			host = {wealth = 25 }
			prisoner = no
		}
		if = {
			limit = {
				is_ruler = yes
				tier = count
				wealth = 70
			}
			wealth = -70
			host = {wealth = 70 }
			prisoner = no
		}
		if = {
			limit = {
				is_ruler = yes
				tier = duke
				wealth = 145
			}
			wealth = -145
			host = {wealth = 145}
			prisoner = no
		}
		if = {
			limit = {
				is_ruler = yes
				tier = king
				wealth = 250
			}
			wealth = -250
			host = {wealth = 250}
			prisoner = no
		}
		if = {
			limit = {
				is_ruler = yes
				tier = emperor
				wealth = 375
			}
			wealth = -375
			host = {wealth = 375}
			prisoner = no
		}
	}
}

# Seduction
character_event = { #checks your status, then selects a random of the realm
	id = court_ai.690
	hide_window = yes
	ai = no
	prisoner = no
	capable_only = yes
	min_age = 16
	trigger = {
		primary_title = { # checks if you have the AI enabled
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_seduction
		}
		has_focus = focus_seduction
		is_ill = no
		NOT = {
			is_pregnant = yes
			trait = incapable
			trait = eunuch
			trait = celibate
			trait = infirm
			trait = inbred
			trait = imbecile_visible
			has_any_opinion_modifier = opinion_seducing
			has_character_modifier = seduction_timer
			has_character_modifier = messalian_prayer
		}
	}
	mean_time_to_happen = { month = 2 }
	immediate = {
		random_courtier = { # First culling of the herd
			limit = { # 
				is_marriage_adult = yes
				prisoner = no
				is_ill = no
				is_pregnant = no
				is_married = no
				NOT = { trait = celibate }
				religion_group = ROOT 
				opinion = { who = ROOT value = 40} 
				custom_tooltip = {
					text = SEDUCTION_DISTANCE_OKAY
					hidden_tooltip = {
						same_realm = ROOT
					}
				}
				custom_tooltip = {
					text = SEDUCTION_TARGET_TIMER_RECENT
					hidden_tooltip = {
						NOT = { has_character_modifier = seduction_target_timer }
					}
				}
				NOR = { 
					trait = incapable
					trait = eunuch
					trait = infirm
					is_lover = ROOT 
					is_former_lover = ROOT 
					is_consort = ROOT 
					is_married = ROOT 
				}
				OR = {
					AND = {
						NOT = { is_child_of = ROOT }
						NOT = { mother_even_if_dead = { is_child_of = ROOT } }
						NOT = { father_even_if_dead = { is_child_of = ROOT } }
					}
					AND = {
						religion = zoroastrian_group
						ROOT = { religion_group = zoroastrian_group }
					}
					AND = {
						religion = messalian 
						ROOT = { religion = messalian }
					}
					AND = {
						religion = egyptian_pagan
						FROM = { religion = egyptian_pagan }
					}
					AND = {
						trait = lunatic
						trait = syphilitic
					}
					AND = {
						trait = possessed
						trait = syphilitic
					}
				}
				OR = {
					AND = {
						ROOT = { NOT = { is_homosexual_trigger = yes } }
						is_opposite_sex = ROOT
					}
					AND = {
						ROOT = { is_homosexual_trigger = yes }
						same_sex = ROOT
					}
				}
			}
			character_event = { id = court_ai.691 } #Sends potentials to next event
		}
	}
}
character_event = { #Sends onto 692, here the Liege is FROM, courtier is ROOT
	id = court_ai.691
	is_triggered_only = yes
	hide_window = yes
	trigger = {
		always = yes
		}
	immediate = {
		FROM = {
			letter_event = { id = court_ai.692 }
		}
	}
}
character_event = { #back on liege, is ROOT and FROMFROM, courtier is FROM
	id = court_ai.692
	desc = "EVTDESCcourt_ai.692"
	is_triggered_only = yes
	trigger = {
		always = yes
	}
	option = {
		name = "EVTOPTAcourt_ai.692"
		hidden_tooltip = { change_variable = { which = "lifestyle_hedonist" value = 1 } }
		FROM = {
			add_character_modifier = {
				name = seduction_target_timer
				duration = 90
				hidden = yes
			}
			character_event = { id = court_ai.693 }
		}
		opinion = {
			who = FROM 
			modifier = opinion_seducing
			years = 1000
		}
	}
	option = {
		name "EVTOPTBcourt_ai.692"
		break = yes
	}
}
character_event = { #sets things back in right 'FROM/ROOT' order for events
	id = court_ai.693
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		FROM = { 
			if = { # homosexual scandal
				limit = {
					same_sex = FROM 
				}
				character_event = { id = WoL.104 days = 1 } 
				break = yes
			}
			if = { # priest scandal
				limit = {
					religion_group = christian 
					OR = {
						is_priest = yes
						is_ascetic_trigger = yes
						FROM = { is_priest = yes }
						FROM = { is_ascetic_trigger = yes }
					}
				}
				character_event = { id = WoL.103 days = 1 }
				break = yes
			}
			if = { # age scandal
				limit = {
					age_diff = {
						who = ROOT
						years = 25
					}
				}
				character_event = { id = WoL.101 days = 1 }
				break = yes
			}
			character_event = { id = WoL.100 days = 1 }
		}
	}
}

## Children's education:
# Educate children (early):
character_event = {
	id = court_ai.101
	hide_window = yes
	only_rulers = yes
	
	trigger = {
		primary_title = { # check if AI enabled
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_kids_education
		}
		ai = no
		any_courtier = { # check for wittle babies
			NOT = { age = 12 }
			age = 2
			prisoner = no
			NOR = {
				has_focus = focus_ch_pride
				has_focus = focus_ch_humility
				has_focus = focus_ch_duty
				has_focus = focus_ch_etiquette
				has_focus = focus_ch_thrift
				has_focus = focus_ch_faith
				has_focus = focus_ch_struggle
				has_focus = focus_ch_heritage
			}
		}
	}
	
	mean_time_to_happen = { # increased to 16 for less impact
		days = 16
	}
	
	immediate = {
		random_courtier = { # picks a widdle baby
			limit = { # makes sure they don't already have a focus
				NOT = { age = 12 }
				age = 2
				prisoner = no
				NOR = {
					has_focus = focus_ch_pride
					has_focus = focus_ch_humility
					has_focus = focus_ch_duty
					has_focus = focus_ch_etiquette
					has_focus = focus_ch_thrift
					has_focus = focus_ch_faith
					has_focus = focus_ch_struggle
					has_focus = focus_ch_heritage
				}
			}
			# Heritage: (for foreign kids)
			if = {
				limit = {
					NOR = {
						culture = ROOT
						is_close_relative = ROOT
						trait = genius
						trait = quick
						trait = strong
						trait = fair
						trait = inbred
					}
				}
				set_focus = focus_ch_heritage
				break = yes
			}
			# Faith: (always for different religion, and for helpless kids)
			if = {
				limit = {
					OR = {
						trait = inbred
						NOT = { religion = ROOT }
					}
				}
				set_focus = focus_ch_faith
				break = yes
			}
			# Thrift: (potential heirs, and autistic kids)
			if = {
				limit = {
					OR = { #if slow or imbecile, or...
						trait = slow
						trait = imbecile
						is_primary_heir = ROOT
						AND = { # is child AND is primo, ultimo, feudal elective
							is_child_of = ROOT
							ROOT = {
								OR = { # primo, ultimo, or fuedal elective?
									has_law = succ_primogeniture 
									has_law = succ_ultimogeniture 
									has_law = succ_feudal_elective 
								}
								OR = { #check demesne size
									AND = {
										tier = count
										demesne_size = 3
									}
									AND = {
										tier = duke
										demesne_size = 5
									}
									AND = {
										tier = king
										demesne_size = 7
									}
									AND = {
										tier = emperor
										demesne_size = 9
									}
								}
							}
						}
					}
				}
				set_focus = focus_ch_thrift
				break = yes
			}
			# Duty: (potential heirs, and gifted kids)
			if = {
				limit = {
					NOR = {
						trait = slow
						trait = imbecile
					}
					OR = { #checks if baby is genius/quick
						OR = { 
							trait = genius
							trait = quick
						}
						AND = { 
							is_close_relative = ROOT
							ROOT = {
								OR = {
									AND = {
										NOR = { # checks demesne size
											AND = {
												tier = count
												demesne_size = 3
											}
											AND = {
												tier = duke
												demesne_size = 5
											}
											AND = {
												tier = king
												demesne_size = 7
											}
											AND = {
												tier = emperor
												demesne_size = 9
											}
										}
										OR = { # tanistry or elective?
											has_law = succ_tanistry 
											has_law = succ_feudal_elective 
										}
										any_vassal = {
											NOT = { tier = baron }
											count = 5
										}
									}
									any_vassal = {
										NOT = { tier = baron }
										count = 10
									}
								}
							}
						}
					}
				}
				set_focus = focus_ch_duty
				break = yes
			}
			# Struggle: (potential heirs, savages, liberators, conquerors, and tough kids)
			if = {
				limit = {
					OR = {
						trait = strong
						ROOT = {
							OR = {
								AND = {
									OR = {
										has_law = succ_nomad_succession
										has_law = succ_elective_gavelkind
									}
									PREV = {
										is_close_relative = ROOT
									}
								}
								NOR = {
									tier = king
									tier = emperor
								}
								liege = {
									OR = {
										NOT = { culture = ROOT }
										NOT = { religion = ROOT }
									}
								}
							}
						}
					}
				}
				set_focus = focus_ch_struggle
				break = yes
			}
			# Humility: (secondary gavelkind heirs)
			if = {
				limit = {
					is_child_of = ROOT
					NOT = { is_primary_heir = ROOT }
					ROOT = {
						has_law = succ_gavelkind
					}
				}
				set_focus = focus_ch_humility
				break = yes
			}
			# Defualt:
			if = {
				limit = {
					is_child_of = ROOT
					is_primary_heir = ROOT
				}
				set_focus = focus_ch_thrift
				break = yes
			}
			random = {
				chance = 30
				set_focus = focus_ch_thrift
				break = yes
			}
			random = {
				chance = 30
				set_focus = focus_ch_duty
				break = yes
			}
			set_focus = focus_ch_humility
			# Pride - never use. (it's lousy)
		}
	}
}

# Educate children (late):
character_event = {
	id = court_ai.102
	hide_window = yes
	only_rulers = yes
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_kids_education
		}
		ai = no
		any_courtier = {
			age = 13 # increased to give one year to select manually
			is_adult = no
			prisoner = no
			NOR = {
				has_focus = focus_ed_diplomacy
				has_focus = focus_ed_martial
				has_focus = focus_ed_stewardship
				has_focus = focus_ed_intrigue
				has_focus = focus_ed_learning
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		random_courtier = { # selects adolescence for training, depending on child trait stuff, learning if they don't match the others
			limit = {
				age = 13
				is_adult = no
				prisoner = no
				NOR = {
					has_focus = focus_ed_diplomacy
					has_focus = focus_ed_martial
					has_focus = focus_ed_stewardship
					has_focus = focus_ed_intrigue
					has_focus = focus_ed_learning
				}
			}
			# Stewardship
			if = {
				limit = {
					OR = {
						trait = conscientious
						trait = fussy
						trait = curious
						trait = brooding
					}
					NOR = {
						trait = indolent
						trait = haughty
						trait = playful
						trait = rowdy
					}
				}
				set_focus = focus_ed_stewardship
				break = yes
			}
			# Martial:
			if = {
				limit = {
					OR = {
						trait = rowdy
						trait = willful
					}
					NOR = {
						trait = idolizer
						trait = timid
					}
				}
				set_focus = focus_ed_martial
				break = yes
			}
			# Diplomacy:
			if = {
				limit = {
					OR = {
						trait = affectionate
						trait = conscientious
						trait = indolent
					}
					NOR = {
						trait = willful
						trait = fussy
						trait = haughty
					}
				}
				set_focus = focus_ed_diplomacy
				break = yes
			}
			# Intrigue:
			if = {
				limit = {
					OR = {
						trait = playful
						trait = brooding
						trait = fussy
					}
					NOR = {
						trait = affectionate
						trait = rowdy
					}
				}
				set_focus = focus_ed_intrigue
				break = yes
			}
			set_focus = focus_ed_learning
		}
	}
}

# Guardians for children:
character_event = {
	id = court_ai.103
	hide_window = yes
	only_rulers = yes
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_kids_education
		}
		ai = no
		any_courtier = {
			prisoner = no
			NOT = { guardian = { always = yes } }
			age = 6
			is_adult = no
			OR = {
				has_focus = focus_ch_pride
				has_focus = focus_ch_humility
				has_focus = focus_ch_duty
				has_focus = focus_ch_etiquette
				has_focus = focus_ch_thrift
				has_focus = focus_ch_faith
				has_focus = focus_ch_struggle
				has_focus = focus_ch_heritage
				has_focus = focus_ed_diplomacy
				has_focus = focus_ed_martial
				has_focus = focus_ed_stewardship
				has_focus = focus_ed_intrigue
				has_focus = focus_ed_learning
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		random_courtier = {
			limit = {
				prisoner = no
				NOT = { guardian = { always = yes } }
				age = 6 # in CK2Plus, gives a year to be manual. In vanilla, not so much.
				is_adult = no
				OR = {
					has_focus = focus_ch_pride
					has_focus = focus_ch_humility
					has_focus = focus_ch_duty
					has_focus = focus_ch_etiquette
					has_focus = focus_ch_thrift
					has_focus = focus_ch_faith
					has_focus = focus_ch_struggle
					has_focus = focus_ch_heritage
					has_focus = focus_ed_diplomacy
					has_focus = focus_ed_martial
					has_focus = focus_ed_stewardship
					has_focus = focus_ed_intrigue
					has_focus = focus_ed_learning
				}
			}
			# Diplomacy guardian:
			if = {
				limit = {
					OR = {
						has_focus = focus_ch_humility
						has_focus = focus_ch_etiquette
						has_focus = focus_ed_diplomacy
					}
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									adult = no
								}
							}
							decent_guardian = yes
							diplomacy = 10
							OR = {
								trait = underhanded_rogue
								trait = charismatic_negotiator
								trait = grey_eminence
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			# Martial guardian:
			if = {
				limit = {
					OR = {
						has_focus = focus_ch_struggle
						has_focus = focus_ed_martial
					}
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									adult = no
								}
							}
							decent_guardian = yes
							martial = 10
							OR = {
								trait = tough_soldier
								trait = skilled_tactician
								trait = brilliant_strategist
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			# Stewardship guardian:
			if = {
				limit = {
					OR = {
						has_focus = focus_ch_duty
						has_focus = focus_ch_thrift
						has_focus = focus_ed_stewardship
					}
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									adult = no
								}
							}
							decent_guardian = yes
							stewardship = 10
							OR = {
								trait = thrifty_clerk
								trait = fortune_builder
								trait = midas_touched
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			# Intrigue guardian:
			if = {
				limit = {
					OR = {
						has_focus = focus_ch_pride
						has_focus = focus_ed_intrigue
					}
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									adult = no
								}
							}
							decent_guardian = yes
							intrigue = 10
							OR = {
								trait = flamboyant_schemer
								trait = intricate_webweaver
								trait = elusive_shadow
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			# Learning guardian:
			if = {
				limit = {
					OR = {
						has_focus = focus_ch_heritage
						has_focus = focus_ch_faith
						has_focus = focus_ed_learning
					}
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									adult = no
								}
							}
							decent_guardian = yes
							learning = 10
							OR = {
								trait = martial_cleric
								trait = scholarly_theologian
								trait = mastermind_theologian
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			set_guardian = event_target:guardian
		}
	}

}

}
	


























