## Written by WKIII - "Court AI"
namespace = court_ai
namespace = court_ai_councilor

# Commander appointment:
character_event = {
	id = court_ai.7
	hide_window = yes
	min_age = 16
	capable_only = yes
	prisoner = no
	trigger = {
		liege = {
			primary_title = {
				has_title_flag = court_ai_enabled
				has_title_flag = court_ai_commander_appointments
			}
			ai = no
			NOT = { character = ROOT }
		}
		independent = no
		#######################################
		OR = {
			can_hold_title = title_commander #<== always false when capped with max # of commanders.
			AND = {
				NOT = { 
					primary_title = { temporary = yes } 
				}
				OR = {
					AND = {
						OR = {
							male_can_hold_minor_title_trigger = yes
							female_can_hold_minor_commander_title_trigger = yes
						}
						OR = {
							NOT = { religion = hindu }
							trait = kshatriya
						}
						is_adult = yes
					}
					has_character_flag = special_marshal
				}
				
				NOT = { is_inaccessible_or_incapable_trigger = yes }
				NOT = { has_character_flag = guru }
				NOT = { has_character_modifier = refusing_to_lead }
				OR = {
					NOT = { liege = { has_law = succ_byzantine_elective } }  #Either not Imperial...
					higher_real_tier_than = BARON #...Or potential commander is vassal...
					NOT = { #...Or there are no valid vassal candidates that can be commanders.
						custom_tooltip = { 
							text = tooltip_byzantine_empire_can_appoint_commanders
							liege = { 
								any_vassal = { 
									higher_real_tier_than = BARON
									NOT = { 
										primary_title = { temporary = yes } 
									}
									OR = {
										AND = {
											OR = {
												male_can_hold_minor_title_trigger = yes
												female_can_hold_minor_commander_title_trigger = yes
											}
											OR = {
												NOT = { religion = hindu }
												trait = kshatriya
											}
											is_adult = yes
										}
										has_character_flag = special_marshal
									}
									
									prisoner = no
									NOT = { trait = incapable }
									NOT = { is_inaccessible_trigger = yes }
									NOT = { has_character_flag = guru }
									NOT = { has_character_modifier = refusing_to_lead }
									NOR = {
										has_job_title = job_chancellor
										has_job_title = job_marshal
										has_job_title = job_treasurer
										has_job_title = job_spymaster
										has_job_title = job_spiritual
										has_minor_title = title_commander
									}
								} 
							} 
						} 
					}
				}
			}
		}
		#######################################
		commander_requirements_trigger = yes
		is_councillor = no
		NOT = { has_minor_title = title_commander }
		liege = {
			any_realm_character = {
				liege = { character = PREVPREV }
				OR = {
					can_hold_title = title_commander
					has_minor_title = title_commander
				}
				NOT = { martial = ROOT }
				in_command = no
			}
		}
	}
	mean_time_to_happen = {
		days = 365
		modifier = {
			factor = 0.1
			has_education_martial_trigger = yes
		}
		modifier = {
			factor = 0.5
			martial = 12
		}
		modifier = {
			factor = 0.5
			martial = 16
		}
		modifier = {
			factor = 0.5
			martial = 20
		}
		modifier = {
			factor = 0.5
			martial = 25
		}
	}
	immediate = {
		if = {
			limit = {
				can_hold_title = title_commander
			}
			liege = {
				ROOT = {
					give_minor_title = title_commander
				}
			}
			break = yes
		}
		liege = {
			if = {
				limit = {
					any_realm_character = {
						liege = { character = PREVPREV }
						has_minor_title = title_commander
						OR = {
							NOT = { martial = ROOT }
							is_councillor = yes
						}
						in_command = no
					}
				}
				random_realm_character = {
					limit = {
						liege = { character = PREVPREV }
						has_minor_title = title_commander
						OR = {
							NOT = { martial = ROOT }
							is_councillor = yes
						}
						in_command = no
						NOT = { has_character_flag = keep_commander_flag }
					}
					remove_title = title_commander
				}
			}
			ROOT = { give_minor_title = title_commander }
		}
	}
}

# 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 = { is_rival = ROOT }
				NOR = {
					tier = duke
					tier = king
					tier = emperor
				}
			}
		}
	}
	mean_time_to_happen = { days = 1 }
	immediate = {
		any_known_plotter = {
			limit = {
				NOT = { character = ROOT }
			}
			any_plot_backer = {
				limit = {
					any_liege = {
						character = ROOT
					}
					NOT = { is_rival = ROOT }
					NOR = {
						tier = duke
						tier = king
					}
				}
				leave_plot = PREV
			}
		}
	}
}

# 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
				has_title_flag = court_ai_minor_titles
			}
			ai = no
			NOT = { character = ROOT }
		}
		OR = {
			AND = {
				opinion = { who = liege value = -10 }
				among_most_powerful_vassals = 10
				NOT = { opinion = { who = liege value = 50 } }
				liege = { primary_title = { has_title_flag = court_ai_minor_titles } }
				OR = {
					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
					can_hold_title = title_paramount_knight
					can_hold_title = title_baghatur
					can_hold_title = title_court_architect
					can_hold_title = title_zhangshi
					can_hold_title = title_canjun
					can_hold_title = title_yuhou
					can_hold_title = title_historiographer
					can_hold_title = title_administrator
					can_hold_title = title_master_engineer
					can_hold_title = title_teacher
				}
			}
			AND = {
				is_ruler = no
				OR = {
					can_hold_title = title_food_taster
				}
			}
		}
	}
	mean_time_to_happen = {
		month = 6
		modifier = {
			factor = 2
			is_ruler = no
		}
		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 = {
		if = { limit = { is_ruler = no }
			if = { limit = { can_hold_title = title_food_taster } liege = { ROOT = { give_minor_title = title_food_taster } } break = yes }
		}
		if = {
			limit = {
				OR = {
					can_hold_title = title_court_physician
					liege = {
						any_realm_character = {
							liege = {
								character = PREVPREV
								has_minor_title = title_court_physician
								NOT = {
									learning = ROOT
								}
							}
						}
					}
				}
				OR = {
					learning = 15
					AND = {
						learning = 10
						OR = {
							trait = physician
							trait = scholar
							trait = mystic
							trait = mutazilite
						}
					}
				}
			}
			liege = { ROOT = { give_minor_title = title_court_physician } } break = yes
		}
		trigger_switch = {
			on_trigger = can_hold_title
			title_master_of_the_hunt = { liege = { ROOT = { give_minor_title = title_master_of_the_hunt } } }
			title_master_of_the_horse = { liege = { ROOT = { give_minor_title = title_master_of_the_horse } } }
			title_keeper_of_swans = { liege = { ROOT = { give_minor_title = title_keeper_of_swans } } }
			title_high_almoner = { liege = { ROOT = { give_minor_title = title_high_almoner } } }
			title_cupbearer = { liege = { ROOT = { give_minor_title = title_cupbearer } } }
			title_seneschal = { liege = { ROOT = { give_minor_title = title_seneschal } } }
			title_court_eunuch = { liege = { ROOT = { give_minor_title = title_court_eunuch } } }
			title_court_dwarf = { liege = { ROOT = { give_minor_title = title_court_dwarf } } }
			title_despot = { liege = { ROOT = { give_minor_title = title_despot } } }
			title_sebastokrator = { liege = { ROOT = { give_minor_title = title_sebastokrator } } }
			title_caesar = { liege = { ROOT = { give_minor_title = title_caesar } } }
			title_kouropalates = { liege = { ROOT = { give_minor_title = title_kouropalates } } }
			title_anthypatos = { liege = { ROOT = { give_minor_title = title_anthypatos } } }
			title_imperial_eunuch = { liege = { ROOT = { give_minor_title = title_imperial_eunuch } } }
			title_court_calligrapher = { liege = { ROOT = { give_minor_title = title_court_calligrapher } } }
			title_court_musician = { liege = { ROOT = { give_minor_title = title_court_musician } } }
			title_court_poet = { liege = { ROOT = { give_minor_title = title_court_poet } } }
			title_chief_architect = { liege = { ROOT = { give_minor_title = title_chief_architect } } }
			title_chief_qadi = { liege = { ROOT = { give_minor_title = title_chief_qadi } } }
			title_state_inquisitor = { liege = { ROOT = { give_minor_title = title_state_inquisitor } } }
			title_high_admiral = { liege = { ROOT = { give_minor_title = title_high_admiral } } }
			title_high_judge = { liege = { ROOT = { give_minor_title = title_high_judge } } }
			title_volva = { liege = { ROOT = { give_minor_title = title_volva } } break = yes }
			title_lawspeaker = { liege = { ROOT = { give_minor_title = title_lawspeaker } } }
			title_drottseti = { liege = { ROOT = { give_minor_title = title_drottseti } } }
			title_court_skald = { liege = { ROOT = { give_minor_title = title_court_skald } } }
			title_marksman = { liege = { ROOT = { give_minor_title = title_marksman } } }
			title_hirdman = { liege = { ROOT = { give_minor_title = title_hirdman } } }
			title_cherbi = { liege = { ROOT = { give_minor_title = title_cherbi } } }
			title_darkhan = { liege = { ROOT = { give_minor_title = title_darkhan } } }
			title_noyan = { liege = { ROOT = { give_minor_title = title_noyan } } }
			title_behi = { liege = { ROOT = { give_minor_title = title_behi } } }
			title_master_of_the_eagle = { liege = { ROOT = { give_minor_title = title_master_of_the_eagle } } }
			title_shahrwaraz = { liege = { ROOT = { give_minor_title = title_shahrwaraz } } }
			title_kardarigan = { liege = { ROOT = { give_minor_title = title_kardarigan } } }
			title_shahin = { liege = { ROOT = { give_minor_title = title_shahin } } }
			title_pushtigban_salar = { liege = { ROOT = { give_minor_title = title_pushtigban_salar } } }
			title_spahbod = { liege = { ROOT = { give_minor_title = title_spahbod } } }
			title_eran_ambaragbed = { liege = { ROOT = { give_minor_title = title_eran_ambaragbed } } }
			title_champion = { liege = { ROOT = { give_minor_title = title_champion } } }
			title_master_of_the_blade = { liege = { ROOT = { give_minor_title = title_master_of_the_blade } } }
			title_master_of_the_bow = { liege = { ROOT = { give_minor_title = title_master_of_the_bow } } }
			title_venerable_elder = { liege = { ROOT = { give_minor_title = title_venerable_elder } } }
			title_yabgu = { liege = { ROOT = { give_minor_title = title_yabgu } } }
			title_ishad = { liege = { ROOT = { give_minor_title = title_ishad } } }
			title_kundur = { liege = { ROOT = { give_minor_title = title_kundur } } }
			title_master_of_the_royal_elephants = { liege = { ROOT = { give_minor_title = title_master_of_the_royal_elephants } } }
			title_royal_builder = { liege = { ROOT = { give_minor_title = title_royal_builder } } }
			title_court_poet_india = { liege = { ROOT = { give_minor_title = title_court_poet_india } } }
			title_guru = { liege = { ROOT = { give_minor_title = title_guru } } }
			title_paramount_knight = { liege = { ROOT = { give_minor_title = title_paramount_knight } } }
			title_baghatur = { liege = { ROOT = { give_minor_title = title_baghatur } } }
			title_court_architect = { liege = { ROOT = { give_minor_title = title_court_architect } } }
			title_zhangshi = { liege = { ROOT = { give_minor_title = title_zhangshi } } }
			title_canjun = { liege = { ROOT = { give_minor_title = title_canjun } } }
			title_yuhou = { liege = { ROOT = { give_minor_title = title_yuhou } } }
			title_historiographer = { liege = { ROOT = { give_minor_title = title_historiographer } } }
			title_administrator = { liege = { ROOT = { give_minor_title = title_administrator } } }
			title_master_engineer = { liege = { ROOT = { give_minor_title = title_master_engineer } } }
			title_teacher = { liege = { ROOT = { give_minor_title = title_teacher } } }

		}
	}
}

## Tasks:
# Chaplain missionary task:
character_event = {
	id = court_ai.20
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_spiritual_jobs
		}
		NOT = {
			has_religion_feature = religion_cosmopolitan
		}
		job_spiritual = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				AND = {
					has_job_action = action_inquisition
					location  = {
						NOT = { religion = ROOT }
					}
				}
				has_job_action = action_advance_cul_tech
				AND = {
					is_tribal = yes
					OR = {
						has_job_action = action_hunt_heretics
						has_job_action = action_build_zeal
					}
				}
				AND = {
					religion_group = muslim
					has_job_action = action_charity
				}
			}
			OR = {
				is_tribal = yes
				NOT = {
					has_job_action = action_hunt_heretics
				}
				has_religion_feature = religion_no_leader
				religion_head = {
					OR = {
						character = ROOT
						is_alive = no
						is_landed = no
						opinion = {
							who = ROOT
							value = 50
						}
					}
				}
			}
		}
		any_realm_province = {
			NOT = { religion = ROOT }
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		if = {
			limit = {
				any_demesne_province = {
					NOT = { religion = ROOT }
				}
			}
			random_demesne_province = {
				limit ={
					NOT = { religion = ROOT }
				}
				ROOT = {
					job_spiritual = {
						set_job_action = {
							action = action_inquisition
							where = PREVPREV
						}
					}
				}
			}
		}
		else = {
			random_realm_province = {
				limit = {
					NOT = { religion = ROOT }
				}
				ROOT = { 
					job_spiritual = {
						set_job_action = {
							action = action_inquisition 
							where = PREVPREV
						}
					}
				}
			}
		}
	}
}

#Chaplain proselytize independents task:
character_event = {
	id = court_ai_councilor.1
	hide_window = yes
	ai = no
	has_dlc = "Zeus"
	
	trigger = {
		has_religion_feature = religion_proselytizing
		trait = zealous
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_spiritual_jobs
		}
		OR = {
			independent = yes
			higher_tier_than = duke
		}
		ai = no
		OR = {
			has_religion_feature = religion_no_leader
			religion_group = muslim
			religion_head = {
				OR = {
					is_alive = no
					is_landed = no
					character = ROOT
					opinion = {
						who = ROOT
						value = 50
					}
					NOT = {
						is_within_diplo_range = ROOT
					}
				}
			}
		}
		is_tribal = no
		job_spiritual = {
			NOR = {
				in_command = yes
				is_inaccessible_trigger = yes
				has_character_modifier = spiritual_refused_pagans
				has_character_modifier = in_seclusion
				AND = {
					has_job_action = action_inquisition
					location  = {
						NOT = { religion = ROOT }
					}
				}
			}
		}
		any_independent_ruler = {
			ca_proselytize_convert_independent_trigger = yes
		}
	}
	mean_time_to_happen = {
		days = 1
	}
	immediate = {
		random_independent_ruler = {
			limit = {
				ca_proselytize_convert_independent_trigger = yes
			}
			capital_scope = {
				ROOT = {
					job_spiritual = {
						set_job_action = {
							action = action_inquisition
							where = PREVPREV
						}
					}
				}
			}
		}
	}
}

# Chaplain research task:
character_event = {
	id = court_ai.21
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_spiritual_jobs
		}
		ai = no
		is_tribal = no
		demesne_size = 1
		OR = {
			has_religion_feature = religion_no_leader
			religion_group = muslim
			religion_head = {
				OR = {
					is_alive = no
					is_landed = no
					character = ROOT
					ROOT = {
						job_spiritual = {
							NOT = { has_job_action = action_improve_rel_relations }
						}
					}
					opinion = {
						who = ROOT 
						value = 50
					}
					NOT = {
						is_within_diplo_range = ROOT
					}
				}
			}
		}
		job_spiritual = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				AND = {
					has_job_action = action_inquisition
					location  = {
						NOT = { religion = ROOT }
					}
				}
				has_job_action = action_advance_cul_tech
				has_job_action = action_hunt_heretics
				has_job_action = action_charity
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		capital_scope = {
			ROOT = { 
				job_spiritual = {
					set_job_action = {
						action = action_advance_cul_tech 
						where = PREVPREV
					}
				}
			}
		}
	}
}

# Chaplain improve relations task:
character_event = {
	id = court_ai.28
	hide_window = yes
	ai = no
	
	trigger = {
		NOT = {
			religion_group = muslim
		}
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_spiritual_jobs
		}
		ai = no
		is_tribal = no
		NOT = { has_religion_feature = religion_no_leader }
		religion_head = {
			is_alive = yes
			is_landed = yes
			is_within_diplo_range = ROOT
			NOR = {
				character = ROOT
				opinion = {
					who = ROOT 
					value = 50
				}
			}
		}
		job_spiritual = {
			NOT = { has_job_action = action_improve_rel_relations }
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				AND = {
					has_job_action = action_inquisition
					location  = {
						NOT = { religion = ROOT }
					}
				}
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		religion_head = {
			capital_scope = {
				ROOT = { 
					job_spiritual = {
						set_job_action = {
							action = action_improve_rel_relations 
							where = PREVPREV
						}
					}
				}
			}
		}
	}
}

#Chaplain build zeal task
#character_event = {
#	id = court_ai_councilor.2
#	hide_window = yes
#	ai = no
#
#	trigger = {
#		primary_title = {
#			has_title_flag = court_ai_enabled
#			has_title_flag = court_ai_spiritual_jobs
#		}
#		is_tribal = yes
#		OR = {
#			has_religion_feature = religion_no_leader
#			religion_group = muslim
#			religion_head = {
#				OR = {
#					is_alive = no
#					is_landed = no
#					character = ROOT
#					opinion = {
#						who = ROOT 
#						value = 50
#					}
#					NOT = {
#						is_within_diplo_range = ROOT
#					}
#				}
#			}
#		}
#		job_spiritual = {
#			NOR = {
#				in_command = yes
#				has_character_modifier = in_seclusion
#				has_job_action = action_hunt_heretics
#				AND = {
#					has_job_action = action_inquisition
#					location = {
#						NOT = { religion = ROOT }
#					}
#				}
#			}
#		}
#	}
#	mean_time_to_happen = {
#		days = 1
#	}
#	immediate = {
#		capital_scope = {
#			ROOT = {
#				job_spiritual = {
#					set_job_action = {
#						action = action_build_zeal
#						where = PREVPREV
#					}
#				}
#			}
#		}
#	}
#}

# Marshal research task:
character_event = {
	id = court_ai.22
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_marshal_jobs
		}
		is_tribal = no
		is_nomadic = no
		war = no
		demesne_size = 1
		job_marshal = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_advance_mil_tech
				has_job_action = action_organize_army
				has_job_action = action_assist_arrest
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		capital_scope = {
			ROOT = { 
				job_marshal = {
					set_job_action = {
						action = action_advance_mil_tech 
						where = PREVPREV
					}
				}
			}
		}
	}
}

# Marshal train troops task:
character_event = {
	id = court_ai.27
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_marshal_jobs
		}
		demesne_size = 1
		OR = {
			war = yes
			is_tribal = yes
			is_nomadic = yes
		}
		job_marshal = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_train_troops 
				has_job_action = action_train_troops_nomadic
				has_job_action = action_organize_army
				has_job_action = action_assist_arrest
				has_job_action = action_organize_raid
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		capital_scope = {
			ROOT = { 
				job_marshal = {
					if = {
						limit = {
							ROOT = { is_nomadic = no }
						}
						set_job_action = {
							action = action_train_troops 
							where = PREVPREV
						}
					}
					else = {
						set_job_action = {
							action = action_train_troops_nomadic 
							where = PREVPREV
						}
					}
				}
			}
		}
	}
}

# Steward research task:
character_event = {
	id = court_ai.23
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_treasurer_jobs
		}
		is_tribal = no
		demesne_size = 1
		should_collect_taxes_trigger = no
		job_treasurer = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_advance_eco_tech
				has_job_action = action_administer_realm
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		capital_scope = {
			ROOT = { 
				job_treasurer = {
					set_job_action = {
						action = action_advance_eco_tech 
						where = PREVPREV
					}
				}
			}
		}
	}
}

# Steward collect taxes task:
character_event = {
	id = court_ai.24
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_treasurer_jobs
		}
		is_tribal = no
		is_nomadic = no
		demesne_size = 1
		should_collect_taxes_trigger = yes
		job_treasurer = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_squeeze_peasants
				has_job_action = action_administer_realm
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		capital_scope = {
			ROOT = { 
				job_treasurer = {
					set_job_action = {
						action = action_squeeze_peasants 
						where = PREVPREV
					}
				}
			}
		}
	}
}

#Steward settle tribe task
character_event = {
	id = court_ai_councilor.7
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_treasurer_jobs
		}
		is_tribal = yes
		job_treasurer = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_administer_realm
				has_job_action = action_build_legend
			}
			OR = {
				NOT = {
					has_job_action = action_settle_tribe
				}
				location = {
					culture = ROOT
				}
			}
		}
		any_demesne_province = {
			NOT = {
				culture = ROOT
			}
		}
	}
	mean_time_to_happen = {
		days = 1
	}
	immediate = {
		random_demesne_province = {
			limit = {
				NOT = {
					culture = ROOT
				}
			}
			ROOT = {
				job_treasurer = {
					set_job_action = {
						action = action_settle_tribe
						where = PREVPREV
					}
				}
			}
		}
	}
}

#Steward build legend task:
#character_event = {
#	id = court_ai_councilor.8
#	hide_window = yes
#	ai = no
#	
#	trigger = {
#		primary_title = {
#			has_title_flag = court_ai_enabled
#			has_title_flag = court_ai_treasurer_jobs
#		}
#		is_tribal = yes
#		job_treasurer = {
#			NOR = {
#				in_command = yes
#				has_character_modifier = in_seclusion
#				AND = {
#					has_job_action = action_settle_tribe
#					location = {
#						NOT = {
#							culture = ROOT
#						}
#					}
#				}
#				has_job_action = action_build_legend
#				has_job_action = action_administer_realm
#			}
#		}
#	}
#	mean_time_to_happen = {
#		days = 1
#	}
#	immediate = {
#		capital_scope = {
#			ROOT = {
#				job_treasurer = {
#					set_job_action = {
#						action = action_build_legend
#						where = PREVPREV
#					}
#				}
#			}
#		}
#	}
#}

#Steward administer realm task
character_event = {
	id = court_ai_councilor.3
	hide_window = yes
	ai = no
	has_dlc = "Mystics"
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_treasurer_jobs
		}
		is_tribal = yes
		NOT = { has_character_modifier = in_seclusion }
		job_treasurer = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_administer_realm
				has_job_action = action_build_legend
				AND = {
					has_job_action = action_settle_tribe
					location = {
						NOT = {
							culture = ROOT
						}
					}
				}
			}
		}
		NOT = {
			any_demesne_province = {
				NOT = {
					culture = ROOT
				}
			}
		}
	}
	mean_time_to_happen = {
		days = 1
	}
	immediate = {
		capital_scope = {
			ROOT = {
				job_treasurer = {
					set_job_action = {
						action = action_administer_realm
						where = PREVPREV
					}
				}
			}
		}
	}
}

#Steward collect tribute task
character_event = {
	id = court_ai_councilor.5
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_treasurer_jobs
		}
		is_nomadic = yes
		NOT = { has_character_modifier = in_seclusion }
		job_treasurer = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_collect_tribute
			}
		}
		OR = {
			any_tributary = {
				always = yes
			}
			any_realm_province = {
				num_of_settlements = 1
				holder_scope = {
					NOT = { character = ROOT }
					ROOT = {
						job_treasurer = {
							NOT = { character = PREVPREV }
						}
					}
				}
			}
		}
	}
	mean_time_to_happen = {
		days = 1
	}
	immediate = {
		if = {
			limit = {
				any_tributary = {
					always = yes
				}
			}
			random_tributary = {
				capital_scope = {
					ROOT = {
						job_treasurer = {
							set_job_action = {
								action = action_collect_tribute
								where = PREVPREV
							}
						}
					}
				}
			}
		}
		else = {
			random_realm_lord = {
				limit = {
					liege = {
						character = ROOT
					}
					NOT = {
						has_job_title = job_treasurer
					}
				}
				capital_scope = {
					ROOT = {
						job_treasurer = {
							set_job_action = {
								action = action_collect_tribute
								where = PREVPREV
							}
						}
					}
				}
			}
		}
	}
}

# Chancellor improve relations task:
#Priority: Improve Relation up to 80 Opinion with:
#1. Top 5 Vassals
#2. Liege
#3. Top 10 Vassals
#4. All Vassals
character_event = {
	id = court_ai.25
	hide_window = yes
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_chancellor_jobs
		}
		job_chancellor = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_fabricate_claims
				has_job_action = action_sow_dissent
			}
			OR = {
				NOR = {
					has_job_action = action_improve_relations
					has_job_action = action_statecraft
					has_job_action = action_clan_sentiment
				}
				AND = {
					has_job_action = action_statecraft
					ROOT = {
						NOT = {
							infamy = 5
						}
					}
					ROOT = {
						OR = {
							liege = {
								NOR = {
									character = ROOT
									opinion = {
										who = ROOT
										value = 80
									}
								}
								avoid_improve_relations_trigger = no
							}
							any_vassal = {
								avoid_improve_relations_trigger = no
								NOR = {
									has_job_title = job_chancellor
									opinion = {
										who = ROOT
										value = 80
									}
								}
							}
						}
					}
				}
				AND = {
					has_job_action = action_improve_relations
					location  = {
						any_province_lord = {
							OR = {
								ROOT = { is_tribal = no }
								higher_tier_than = baron
							}
							OR = {
								liege = {
									character = ROOT
								}
								any_vassal = {
									character = ROOT
								}
							}
							opinion = {
								who = ROOT 
								value = 80
							}
						}
					}
				}
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		if = {
			limit = {
				any_vassal = {
					OR = {
						ROOT = { is_tribal = no }
						higher_tier_than = baron #Tribals can't improve relations with barons/mayors/priests
					}
					among_most_powerful_vassals = 5
					NOT = { has_job_title = job_chancellor }
					avoid_improve_relations_trigger = no
					NOT = {
						opinion = {
							who = ROOT
							value = 80
						}
					}
				}
			}
			random_vassal = {
				if = {
					limit = {
						OR = {
							ROOT = {
								is_tribal = no
							}
							higher_tier_than = baron
						}
						among_most_powerful_vassals = 5
						avoid_improve_relations_trigger = no
						NOR = {
							has_job_title = job_chancellor
							opinion = {
								who = ROOT
								value = 80
							}
						}
					}
					capital_scope = {
						ROOT = {
							job_chancellor = {
								set_job_action = {
									action = action_improve_relations
									where = PREVPREV
								}
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				liege = {
					avoid_improve_relations_trigger = no
					NOT = {
						opinion = {
							who = ROOT
							value = 50
						}
					}
				}
			}
			liege = {
				capital_scope = {
					ROOT = {
						job_chancellor = {
							set_job_action = {
								action = action_improve_relations
								where = PREVPREV
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				any_vassal = {
					OR = {
						ROOT = { is_tribal = no }
						higher_tier_than = baron
					}
					among_most_powerful_vassals = 10
					avoid_improve_relations_trigger = no
					NOR = {
						has_job_title = job_chancellor
						opinion = {
							who = ROOT
							value = 80
						}
					}
				}
			}
			random_vassal = {
				limit = {
					OR = {
						ROOT = { is_tribal = no }
						higher_tier_than = baron
					}
					among_most_powerful_vassals = 10
					avoid_improve_relations_trigger = no
					NOR = {
						has_job_title = job_chancellor
						opinion = {
							who = ROOT
							value = 80
						}
					}
				}
				capital_scope = {
					ROOT = {
						job_chancellor = {
							set_job_action = {
								action = action_improve_relations
								where = PREVPREV
							}
						}
					}
				}
			}
		}
		else = {
			random_vassal = {
				limit = {
					OR = {
						ROOT = {
							is_tribal = no
						}
						higher_tier_than = baron
					}
					avoid_improve_relations_trigger = no
					NOR = {
						has_job_title = job_chancellor
						opinion = {
							who = ROOT
							value = 80
						}
					}
				}
				capital_scope = {
					ROOT = { 
						job_chancellor = {
							set_job_action = {
								action = action_improve_relations 
								where = PREVPREV
							}
						}
					}
				}
			}
		}
	}
}

# Chancellor: statecraft
character_event = {
	id = court_ai_councilor.4
	hide_window = yes
	has_dlc = "Mystics"
	ai = no
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_chancellor_jobs
		}
		job_chancellor = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_fabricate_claims
				has_job_action = action_sow_dissent
			}
			OR = {
				NOR = {
					has_job_action = action_statecraft
					has_job_action = action_improve_relations
					has_job_action = action_clan_sentiment
				}
				AND = {
					NOT = {
						has_job_action = action_statecraft
					}
					ROOT = {
						infamy = 5
					}
				}
				AND = {
					has_job_action = action_improve_relations
					ROOT = {
						NOR = {
							any_vassal = {
								OR = {
									ROOT = { is_tribal = no }
									higher_tier_than = baron
								}
								NOR = {
									has_job_title = job_chancellor
									opinion = {
										who = ROOT
										value = 80
									}
								}
								avoid_improve_relations_trigger = no
							}
							liege = {
								NOR = {
									character = ROOT
									opinion = {
										who = ROOT
										value = 80
									}
								}
								avoid_improve_relations_trigger = no
							}
						}
					}
				}
			}
		}
	}
	mean_time_to_happen = {
		days = 1
	}
	immediate = {
		capital_scope = {
			ROOT = {
				job_chancellor = {
					set_job_action = {
						action = action_statecraft
						where = PREVPREV
					}
				}
			}
		}
	}
}

#Chancellor clan relations task
character_event = {
	id = court_ai_councilor.6
	hide_window = yes
	ai = no
	has_dlc = "Horse Lords"
	
	trigger = {
		is_nomadic = yes
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_chancellor_jobs
		}
		OR = {
			liege = {
				is_nomadic = yes
			}
			any_vassal = {
				is_nomadic = yes
			}
			any_tributary = {
				is_nomadic = yes
			}
		}
		job_chancellor = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_sow_dissent
			}
			OR = {
				NOR = {
					has_job_action = action_clan_sentiment
					has_job_action = action_improve_relations
					has_job_action = action_statecraft
				}
				AND = {
					has_job_action = action_clan_sentiment
					location = {
						owner = {
							clan_opinion = {
								who = ROOT
								value = 50
							}
						}
					}
					OR = {
						liege = {
							is_nomadic = yes
							NOT = {
								clan_opinion = {
									who = ROOT
									value = 50
								}
							}
						}
						any_vassal = {
							is_nomadic = yes
							NOT = {
								has_job_title = job_chancellor
							}
							NOT = {
								clan_opinion = {
									who = ROOT
									value = 50
								}
							}
						}
						any_tributary = {
							is_nomadic = yes
							NOT = {
								clan_opinion = {
									who = ROOT
									value = 50
								}
							}
						}
					}
				}
			}
		}
	}
	mean_time_to_happen = {
		days = 1
	}
	immediate = {
		if = {
			limit = {
				any_vassal = {
					is_nomadic = yes
					among_most_powerful_vassals = 5
					NOT = {
						has_job_title = job_chancellor
					}
					NOT = {
						clan_opinion = {
							who = ROOT
							value = 50
						}
					}
				}
			}
			random_vassal = {
				limit = {
					is_nomadic = yes
					among_most_powerful_vassals = 5
					NOT = {
						clan_opinion = {
							who = ROOT
							value = 50
						}
					}
				}
				capital_scope = {
					ROOT = {
						job_chancellor = {
							set_job_action = {
								action = action_clan_sentiment
								where = PREVPREV
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				liege = {
					is_nomadic = yes
					NOT = {
						clan_opinion = {
							who = ROOT
							value = 50
						}
					}
				}
			}
			liege = {
				capital_scope = {
					ROOT = {
						job_chancellor = {
							set_job_action = {
								action = action_clan_sentiment
								where = PREVPREV
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				any_vassal = {
					is_nomadic = yes
					NOT = {
						has_job_title = job_chancellor
					}
					NOT = {
						clan_opinion = {
							who = ROOT
							value = 50
						}
					}
				}
			}
			random_vassal = {
				limit = {
					is_nomadic = yes
					NOT = {
						has_job_title = job_chancellor
					}
					NOT = {
						clan_opinion = {
							who = ROOT
							value = 50
						}
					}
				}
				capital_scope = {
					ROOT = {
						job_chancellor = {
							set_job_action = {
								action = action_clan_sentiment
								where = PREVPREV
							}
						}
					}
				}
			}
		}
		else = {
			random_tributary = {
				limit = {
					is_nomadic = yes
					NOT = {
						clan_opinion = {
							who = ROOT
							value = 50
						}
					}
				}
				capital_scope = {
					ROOT = {
						job_chancellor = {
							set_job_action = {
								action = action_clan_sentiment
								where = PREVPREV
							}
						}
					}
				}
			}
		}
	}
}

# Spymaster uncover plots task:
character_event = {
	id = court_ai.26
	hide_window = yes
	only_rulers = yes
	
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_spymaster_jobs
		}
		ai = no
		demesne_size = 1
		job_spymaster = {
			NOR = {
				in_command = yes
				has_character_modifier = in_seclusion
				has_job_action = action_uncover_plots
				has_job_action = action_uncover_plots_offmap
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		capital_scope = {
			ROOT = { 
				job_spymaster = {
					if = {
						limit = {
							NOT = {
								has_dlc = "Mystics"
							}
						}
						set_job_action = {
							action = action_uncover_plots 
							where = PREVPREV
						}
					}
					else = {
						set_job_action = {
							action = action_uncover_plots_offmap
							where = PREVPREV
						}
					}
				}
			}
		}
	}
}

# Epidemic Capital Gate:
character_event = {
	id = court_ai.104
	hide_window = yes
	only_rulers = yes
	trigger = {
        primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_epidemic_capital_gate
		}
		AND = {
			prisoner = no
			ai = no
			capital_scope = {
				has_epidemic = yes
				#OR = {
				#	has_epidemic = yes
				#	any_neighbor_province = {
				#		has_epidemic = yes
				#	}
				#}
			}
			NOT = { has_character_modifier = recently_opened_gates}
			NOT = { has_character_modifier = in_seclusion}
		}
		has_dlc = "Reapers"
		OR = {
			is_landed = yes
			is_patrician = yes
		}
		NOR = {
			is_tribal = yes
			is_nomadic = yes
			is_inaccessible_trigger = yes
		}
	}
	option = {
	
		#Achievement flag
			if = {
				limit = {
					ai = no
				}
				set_character_flag = achievement_no_solicitors
			}
			if = {
				limit = {
					has_minor_title = title_commander
				}
				remove_title = title_commander
			}
		set_character_flag = do_not_disturb
			custom_tooltip = { text = shut_the_gates_info_tooltip }
			add_character_modifier = {
				name = in_seclusion
				duration = -1
			}
			if = { # If you are a councillor, stop working and return home
				limit = {
					host = { NOT = { character = ROOT } }
					OR = {
						is_councillor = yes
						has_minor_title = title_councilmember_king
						has_minor_title = title_councilmember_emperor
					}
				}
				force_host = ROOT
			}
		hidden_tooltip = {
				character_event = { id = RIP.12000 }
				any_courtier = { # Unlanded councillors stop working in order to go into seclusion with their liege
					limit = {
						prisoner = no
						is_councillor = yes
						is_landed = no
						liege = { character = ROOT }
						NOT = { character = ROOT }
					}
					add_character_modifier = {
						name = in_seclusion
						duration = -1
					}
				}
				any_courtier = { # Unlanded non-councillors join their liege in seclusion
					limit = {
						prisoner = no
						is_councillor = no
						is_landed = no
						liege = { character = ROOT }
						NOT = { character = ROOT }
					}
					add_character_modifier = {
						name = in_seclusion
						duration = -1
					}
				}
				any_courtier = { # Landed councillors return home while you, their liege, is in seclusion
					limit = {
						prisoner = no
						OR = {
							is_councillor = yes
							has_minor_title = title_councilmember_king
							has_minor_title = title_councilmember_emperor
						}
						is_landed = yes
						liege = { character = ROOT }
						NOT = { character = ROOT }
					}
					force_host = THIS
				}
				any_courtier = { # Landed children being educated in your court are sent home, with the guardianship ending
					limit = {
						prisoner = no
						is_landed = yes
						is_adult = no
						has_guardian = yes
					}
					remove_guardian = yes
				}
				if = { #If you are getting educated elsewhere, then go home to be secluded
					limit = {
						NOT = { age = 16 }
						is_abroad = yes
					}
					remove_guardian = yes
					force_host = ROOT
				}
				any_vassal = {
					character_event = { id = RIP.12001 days = 7 random = 7 }
				}
				if = { #Notify employer that councillor is going into seclusion
					limit = { 
						is_councillor = yes 
						liege = { ai = no }
					}
					liege = { letter_event = { id = RIP.12101 days = 1 } }
				}
		}
	}
}
character_event = {
	id = court_ai.105
	hide_window = yes
	only_rulers = yes
	trigger = {
        primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_epidemic_capital_gate
		}
		prisoner = no
		ai = no
	    capital_scope = {
			has_epidemic = no
		}
		NOT = { any_neighbor_province = { has_epidemic = yes } }
		has_character_modifier = in_seclusion
	}
	option = {
			clr_character_flag = do_not_disturb
			clr_character_flag = exposed_to_outside
			clr_character_flag = seclusion_piling_corpses
			clr_character_flag = seclusion_lazy_seduction
			clr_character_flag = seclusion_nightmares
			clr_character_flag = seclusion_gained_friend
			clr_character_flag = seclusion_child_has_lover
			clr_character_flag = seclusion_tunnel
			clr_character_flag = seclusion_food_events

			custom_tooltip = { text = open_the_gates_info_tooltip }
			remove_character_modifier = in_seclusion
			add_character_modifier = {
				name = recently_opened_gates
				duration = 365
			}
			hidden_tooltip = {
				clr_character_flag = seclusion_events_on
				character_event = { id = RIP.11557 days = 40 random = 20} #clears starvation modifiers
				any_courtier = {
					clr_character_flag = seclusion_events_on
					remove_character_modifier = in_seclusion
					character_event = { id = RIP.11557 days = 40 random = 20} #clears starvation modifiers
				}
				any_vassal = {
					limit = {
						OR = {
							is_councillor = yes
							has_minor_title = title_councilmember_king
							has_minor_title = title_councilmember_emperor
						}
						is_landed = yes
						liege = { character = ROOT }
						NOT = {
							has_character_modifier = in_seclusion
						}
						host = { NOT = { character = ROOT } }
					}
					force_host = ROOT
				}
			}
			if = { # Rejoin your liege's court if both of you are not in seclusion
				limit = {
					OR = {
						is_councillor = yes
						has_minor_title = title_councilmember_king
						has_minor_title = title_councilmember_emperor
					}
					NOT = {
						liege = {
							has_character_modifier = in_seclusion
						}
					}
				}
				liege = {
					ROOT = {
						force_host = PREV
					}
				}

				if = { #Notify employer that councillor is going into seclusion
					limit = { 
						is_councillor = yes 
						#liege = { ai = no }
					}
					liege = { character_event = { id = RIP.12100 days = 1 } }
				}
			}

			if = { limit = { has_character_modifier = secluded_revelry } remove_character_modifier = secluded_revelry }
			if = { limit = { has_character_modifier = savoring_the_end_days } remove_character_modifier = savoring_the_end_days }
		}
}

# Demands religious conversions:
character_event = {
	id = court_ai.106
	hide_window = yes
	ai = no
	prisoner = no
	capable_only = yes
	min_age = 16
	trigger = {
		primary_title = {
			has_title_flag = court_ai_enabled
			has_title_flag = court_ai_demands_religious_conversions
		}
		#is_reformed_religion = yes
	}
	mean_time_to_happen = { days = 2 }
	immediate = {
		random_courtier_or_vassal = {
			limit = {
				prisoner = no
				NOT = { religion = ROOT }
				opinion = { who = ROOT value = 40} 
				controls_religion = no
				temporary = no
				age = 16
				NOT = { trait = incapable }
			}
			religion = ROOT
		}
	}
}

## 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 = 5
			prisoner = no
			NOT = { has_character_flag = not_educate_children }
			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 = 1
	}
	
	immediate = {
		random_courtier = { # picks a widdle baby
			limit = { # makes sure they don't already have a focus
				NOT = { age = 12 }
				age = 5
				prisoner = no
				NOT = { has_character_flag = not_educate_children }
				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 = 12 # increased to give one year to select manually
			is_adult = no
			prisoner = no
			NOT = { has_character_flag = not_educate_children }
			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 = 12
				is_adult = no
				prisoner = no
				NOT = { has_character_flag = not_educate_children }
				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
				}
			}
			if = {
				limit = {
					has_character_flag = diplomacy_education
				}
				set_focus = focus_ed_diplomacy
				remove_guardian = yes
				break = yes
			}
			if = {
				limit = {
					has_character_flag = martial_education
				}
				set_focus = focus_ed_martial
				remove_guardian = yes
				break = yes
			}
			if = {
				limit = {
					has_character_flag = stewardship_education
				}
				set_focus = focus_ed_stewardship
				remove_guardian = yes
				break = yes
			}
			if = {
				limit = {
					has_character_flag = intrigue_education
				}
				set_focus = focus_ed_intrigue
				remove_guardian = yes
				break = yes
			}
			if = {
				limit = {
					has_character_flag = learning_education
				}
				set_focus = focus_ed_learning
				remove_guardian = yes
				break = yes
			}
			# Diplomacy:
			if = {
				limit = {
					OR = {
						trait = affectionate
						trait = conscientious
						trait = indolent
					}
					NOR = {
						trait = willful
						trait = fussy
						trait = haughty
					}
					NOT = {
						has_character_flag = diplomacy_education
						has_character_flag = martial_education
						has_character_flag = stewardship_education
						has_character_flag = intrigue_education
						has_character_flag = learning_education
					}
				}
				set_focus = focus_ed_diplomacy
				remove_guardian = yes
				break = yes
			}
			# Martial:
			if = {
				limit = {
					OR = {
						trait = rowdy
						trait = willful
					}
					NOR = {
						trait = idolizer
						trait = timid
					}
					NOT = {
						has_character_flag = diplomacy_education
						has_character_flag = martial_education
						has_character_flag = stewardship_education
						has_character_flag = intrigue_education
						has_character_flag = learning_education
					}
				}
				set_focus = focus_ed_martial
				remove_guardian = yes
				break = yes
			}
			# Stewardship
			if = {
				limit = {
					OR = {
						trait = conscientious
						trait = fussy
						trait = curious
						trait = brooding
					}
					NOR = {
						trait = indolent
						trait = haughty
						trait = playful
						trait = rowdy
					}
					NOT = {
						has_character_flag = diplomacy_education
						has_character_flag = martial_education
						has_character_flag = stewardship_education
						has_character_flag = intrigue_education
						has_character_flag = learning_education
					}
				}
				set_focus = focus_ed_stewardship
				remove_guardian = yes
				break = yes
			}
			# Intrigue:
			if = {
				limit = {
					OR = {
						trait = playful
						trait = brooding
						trait = fussy
					}
					NOR = {
						trait = affectionate
						trait = rowdy
					}
					NOT = {
						has_character_flag = diplomacy_education
						has_character_flag = martial_education
						has_character_flag = stewardship_education
						has_character_flag = intrigue_education
						has_character_flag = learning_education
					}
				}
				set_focus = focus_ed_intrigue
				remove_guardian = yes
				break = yes
			}
			else = {
				set_focus = focus_ed_learning
				remove_guardian = yes
			}
		}
	}
}

# 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 = { has_character_flag = not_educate_children }
			NOT = { guardian = { always = yes } }
			age = 5
			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 = { has_character_flag = not_educate_children }
				NOT = { guardian = { always = yes } }
				age = 5
				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
					}
					NOT = { has_character_flag = diplomacy_education }
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							diplomacy = 12
							OR = {
								trait = underhanded_rogue
								trait = charismatic_negotiator
								trait = grey_eminence
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			if = {
				limit = {
					has_character_flag = diplomacy_education
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							diplomacy = 12
							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
					}
					NOT = { has_character_flag = martial_education }
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							martial = 12
							OR = {
								trait = tough_soldier
								trait = skilled_tactician
								trait = brilliant_strategist
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			if = {
				limit = {
					has_character_flag = martial_education
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							martial = 12
							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
					}
					NOT = { has_character_flag = stewardship_education }
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							stewardship = 12
							OR = {
								trait = thrifty_clerk
								trait = fortune_builder
								trait = midas_touched
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			if = {
				limit = {
					has_character_flag = stewardship_education
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							stewardship = 12
							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
					}
					NOT = { has_character_flag = intrigue_education }
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							intrigue = 12
							OR = {
								trait = flamboyant_schemer
								trait = intricate_webweaver
								trait = elusive_shadow
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			if = {
				limit = {
					has_character_flag = intrigue_education
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							intrigue = 12
							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
					}
					NOT = { has_character_flag = learning_education }
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							learning = 12
							OR = {
								trait = martial_cleric
								trait = scholarly_theologian
								trait = mastermind_theologian
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			if = {
				limit = {
					has_character_flag = learning_education
				}
				ROOT = {
					random_courtier = {
						limit = {
							NOT = {
								any_ward = {
									count = 2
									is_adult = no
								}
							}
							decent_guardian_trigger = yes
							learning = 12
							OR = {
								trait = martial_cleric
								trait = scholarly_theologian
								trait = mastermind_theologian
							}
						}
						save_event_target_as = guardian
					}
				}
			}
			
			set_guardian = event_target:guardian
		}
	}
}