decisions = {
	form_knights_of_calatrava = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			has_dlc = "Sons of Abraham"
			NOT = { is_title_active = d_knights_calatrava }
			is_catholic_branch = yes

			c_calatrava = {
				holder_scope = {
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}
				}
			}

			any_demesne_title = {
				higher_tier_than = DUKE

				capital_scope = {
					region = world_europe_west_iberia
				}
			}

			OR = {
				# Prevent Army from being created at start
				has_global_flag = christian_crusades_unlocked
				has_global_flag = pagan_ghws_unlocked
				has_global_flag = muslim_jihads_unlocked
			}

			any_demesne_title = {
				tier = COUNT
				NOT = { title = c_calatrava }
			}

			NOT = {
				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			culture_group = iberian
			is_heretic = no
			NOT = { trait = excommunicated }
			war = no
			wealth >= 500
			piety  >= 500

			custom_tooltip = {
				text = only_one_vassalized_holy_order_TT

				NOT = {
					any_realm_lord = {
						holy_order = yes
					}
				}
			}
		}

		effect = {
			wealth = -500
			piety  = -500

			custom_tooltip = {
				text = form_knights_of_calatrava_TT

				create_random_soldier = {
					random_traits = yes
					dynasty = random
					culture = castilian
					religion = ROOT
					age = 25
				}

				new_character = {
					set_government_type = order_government
					add_trait = shrewd
					add_trait = zealous
					add_trait = diligent
					add_trait = inspiring_leader
					add_trait = organizer

					if = {
						limit = {
							NOT = { trait = brilliant_strategist }
						}

						remove_trait = misguided_warrior
						remove_trait = tough_soldier
						remove_trait = skilled_tactician
						add_trait = brilliant_strategist
					}

					grant_title_no_opinion = d_knights_calatrava
					grant_title_no_opinion = c_calatrava
					set_defacto_liege = ROOT
					add_friend = ROOT
					save_event_target_as = grandmaster_of_calatrava

					opinion = {
						name = opinion_formed_order
						who = ROOT
						years = 25
					}

					primary_title = {
						holy_order_set_law_effect = yes
					}
				}

				c_calatrava = {
					any_direct_de_jure_vassal_title = {
						limit = {
							holder_scope = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}

						if = {
							limit = { holder = ROOT }

							if = {
								limit = { holding_type = city }

								create_random_steward = {
									random_traits = yes
									dynasty = random
								}
							}
							else_if = {
								limit = { holding_type = temple }

								create_random_priest = {
									random_traits = yes
									dynasty = random
								}
							}
							else = {
								create_random_soldier = {
									random_traits = yes
									dynasty = random
								}
							}

							new_character = {
								grant_title_no_opinion = PREV
								set_defacto_liege = event_target:grandmaster_of_calatrava
							}
						}
						else = {
							holder_scope = {
								set_defacto_liege = event_target:grandmaster_of_calatrava
								religion = ROOT
							}
						}
					}
				}

				d_knights_calatrava = {
					holder_scope = {
						set_defacto_liege = ROOT
					}
				}
			}

			narrative_event = { id = HFP.40029 } # Notify
			sound_effect = generic_click_01
		}
	}

	vassalize_knights_of_calatrava = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			is_title_active = d_knights_calatrava
			is_catholic_branch = yes

			c_calatrava = {
				holder_scope = {
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
						has_landed_title = d_knights_calatrava
						is_vassal_or_below_of = d_knights_calatrava
					}
				}
			}

			d_knights_calatrava = {
				holder_scope = {
					independent = yes

					NOT = {
						has_opinion_modifier = {
							who = ROOT
							name = opinion_unfit_ruler
						}
					}
				}
			}

			any_demesne_title = {
				higher_tier_than = DUKE

				capital_scope = {
					region = world_europe_west_iberia
				}
			}

			any_demesne_title = {
				tier = COUNT
				NOT = { title = c_calatrava }
			}

			NOR = {
				has_character_modifier = expelled_d_knights_calatrava

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			culture_group = iberian
			wealth >= 300
			is_heretic = no
			war = no
			NOT = { trait = excommunicated }

			trigger_if = {
				limit = {
					d_knights_calatrava = {
						holder_scope = {
							opinion = {
								who = ROOT
								value < 25
							}
						}
					}

				}

				custom_tooltip = {
					text = vassalize_knights_calatrava_opinion_TT

					always = no
				}
			}

			custom_tooltip = {
				text = only_one_vassalized_holy_order_TT

				NOT = {
					any_realm_lord = {
						holy_order = yes
					}
				}
			}
		}

		effect = {
			wealth = -300

			d_knights_calatrava = {
				show_scope_change = no

				holder_scope = {
					show_scope_change = no

					grant_title_no_opinion = c_calatrava
				}
			}

			hidden_effect = {
				d_knights_calatrava = {
					holder_scope = {
						save_event_target_as = grandmaster_of_calatrava
						wealth = 300
					}

					set_defacto_liege = ROOT
				}

				c_calatrava = {
					any_direct_de_jure_vassal_title = {
						limit = {
							holder_scope = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}

						if = {
							limit = { holder = ROOT }

							if = {
								limit = { holding_type = city }

								create_random_steward = {
									random_traits = yes
									dynasty = random
								}
							}
							else_if = {
								limit = { holding_type = temple }

								create_random_priest = {
									random_traits = yes
									dynasty = random
								}
							}
							else = {
								create_random_soldier = {
									random_traits = yes
									dynasty = random
								}
							}

							new_character = {
								grant_title_no_opinion = PREV
								set_defacto_liege = event_target:grandmaster_of_calatrava
							}
						}
						else = {
							holder_scope = {
								set_defacto_liege = event_target:grandmaster_of_calatrava
								religion = ROOT
							}
						}
					}
				}
			}
		}
	}

	form_knights_of_santiago = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			has_dlc = "Sons of Abraham"
			NOT = { is_title_active = d_knights_santiago }
			is_catholic_branch = yes

			any_demesne_title = {
				tier = COUNT
				NOT = { title = c_santiago }
			}

			any_demesne_title = {
				higher_tier_than = DUKE

				capital_scope = {
					region = world_europe_west_iberia
				}
			}

			c_santiago = {
				holder_scope = {
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}
				}
			}

			OR = {
				# Prevent Army from being created at start
				has_global_flag = christian_crusades_unlocked
				has_global_flag = pagan_ghws_unlocked
				has_global_flag = muslim_jihads_unlocked
			}

			NOT = {
				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			culture_group = iberian
			is_heretic = no
			NOT = { trait = excommunicated }
			war = no
			wealth >= 500
			piety  >= 500

			custom_tooltip = {
				text = only_one_vassalized_holy_order_TT

				NOT = {
					any_realm_lord = {
						holy_order = yes
					}
				}
			}
		}

		effect = {
			wealth = -500
			piety  = -500

			custom_tooltip = {
				text = form_knights_of_santiago_TT

				create_random_soldier = {
					random_traits = yes
					dynasty = random
					culture = castilian
					religion = ROOT
					age = 25
				}

				new_character = {
					set_government_type = order_government
					add_trait = shrewd
					add_trait = zealous
					add_trait = diligent
					add_trait = inspiring_leader
					add_trait = organizer

					if = {
						limit = {
							NOT = { trait = brilliant_strategist }
						}

						remove_trait = misguided_warrior
						remove_trait = tough_soldier
						remove_trait = skilled_tactician
						add_trait = brilliant_strategist
					}

					grant_title_no_opinion = d_knights_santiago
					grant_title_no_opinion = c_santiago
					set_defacto_liege = ROOT
					add_friend = ROOT
					save_event_target_as = grandmaster_of_santiago

					opinion = {
						name = opinion_formed_order
						who = ROOT
						years = 25
					}

					primary_title = {
						holy_order_set_law_effect = yes
					}
				}

				c_santiago = {
					any_direct_de_jure_vassal_title = {
						limit = {
							holder_scope = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}

						if = {
							limit = { holder = ROOT }

							if = {
								limit = { holding_type = city }

								create_random_steward = {
									random_traits = yes
									dynasty = random
								}
							}
							else_if = {
								limit = { holding_type = temple }

								create_random_priest = {
									random_traits = yes
									dynasty = random
								}
							}
							else = {
								create_random_soldier = {
									random_traits = yes
									dynasty = random
								}
							}

							new_character = {
								grant_title_no_opinion = PREV
								set_defacto_liege = event_target:grandmaster_of_santiago
							}
						}
						else = {
							holder_scope = {
								set_defacto_liege = event_target:grandmaster_of_santiago
								religion = ROOT
							}
						}
					}
				}

				d_knights_santiago = {
					holder_scope = {
						set_defacto_liege = ROOT
					}
				}
			}

			narrative_event = { id = HFP.40030 } # Notify
			sound_effect = generic_click_01
		}
	}

	vassalize_knights_of_santiago = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			is_title_active = d_knights_santiago
			is_catholic_branch = yes

			c_santiago = {
				holder_scope = {
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
						has_landed_title = d_knights_santiago
						is_vassal_or_below_of = d_knights_santiago
					}
				}
			}

			d_knights_santiago = {
				holder_scope = {
					independent = yes

					NOT = {
						has_opinion_modifier = {
							who = ROOT
							name = opinion_unfit_ruler
						}
					}
				}
			}

			any_demesne_title = {
				higher_tier_than = DUKE

				capital_scope = {
					region = world_europe_west_iberia
				}
			}

			any_demesne_title = {
				tier = COUNT
				NOT = { title = c_santiago }
			}

			NOR = {
				has_character_modifier = expelled_d_knights_santiago

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			culture_group = iberian
			war = no
			wealth >= 300
			is_heretic = no
			NOT = { trait = excommunicated }

			trigger_if = {
				limit = {
					d_knights_santiago = {
						holder_scope = {
							opinion = {
								who = ROOT
								value < 25
							}
						}
					}
				}

				custom_tooltip = {
					text = vassalize_knights_santiago_opinion_TT

					always = no
				}
			}

			custom_tooltip = {
				text = only_one_vassalized_holy_order_TT

				NOT = {
					any_realm_lord = {
						holy_order = yes
					}
				}
			}
		}

		effect = {
			wealth = -300

			d_knights_santiago = {
				show_scope_change = no

				holder_scope = {
					show_scope_change = no

					grant_title_no_opinion = c_santiago
				}
			}

			hidden_effect = {
				d_knights_santiago = {
					holder_scope = {
						save_event_target_as = grandmaster_of_santiago
						wealth = 300
					}

					set_defacto_liege = ROOT
				}

				c_santiago = {
					any_direct_de_jure_vassal_title = {
						limit = {
							holder_scope = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}

						if = {
							limit = { holder = ROOT }

							if = {
								limit = { holding_type = city }

								create_random_steward = {
									random_traits = yes
									dynasty = random
								}
							}
							else_if = {
								limit = { holding_type = temple }

								create_random_priest = {
									random_traits = yes
									dynasty = random
								}
							}
							else = {
								create_random_soldier = {
									random_traits = yes
									dynasty = random
								}
							}

							new_character = {
								grant_title_no_opinion = PREV
								set_defacto_liege = event_target:grandmaster_of_santiago
							}
						}
						else = {
							holder_scope = {
								set_defacto_liege = event_target:grandmaster_of_santiago
							}
						}
					}
				}
			}
		}
	}

	form_teutonic_order = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			higher_real_tier_than = DUKE
			is_catholic_branch = yes

			NOR = {
				is_title_active = d_teutonic_order
				is_title_active = k_teutonic_state
			}

			trigger_if = {
				limit = { is_title_active = e_hre }
				has_landed_title = e_hre
			}
			trigger_else = {
				culture = german
			}

			OR = {
				# Prevent Army from being created at start
				has_global_flag = christian_crusades_unlocked
				has_global_flag = pagan_ghws_unlocked
				has_global_flag = muslim_jihads_unlocked
			}

			NOT = {
				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			culture = german
			is_heretic = no
			NOT = { trait = excommunicated }
			war = no
			wealth >= 500
			piety  >= 500

			custom_tooltip = {
				text = form_teutonic_order_jerusalem_TT

				k_jerusalem = {
					holder_scope = {
						religion = ROOT
					}
				}
			}
		}

		effect = {
			wealth = -500
			piety  = -500

			custom_tooltip = {
				text = form_teutonic_order_TT

				create_random_soldier = {
					random_traits = yes
					dynasty = random
					culture = german
					religion = ROOT
					age = 25
				}

				new_character = {
					set_government_type = order_government
					add_trait = shrewd
					add_trait = zealous
					add_trait = diligent
					add_trait = inspiring_leader
					add_trait = organizer

					if = {
						limit = {
							NOT = { trait = brilliant_strategist }
						}

						remove_trait = misguided_warrior
						remove_trait = tough_soldier
						remove_trait = skilled_tactician
						add_trait = brilliant_strategist
					}

					grant_title_no_opinion = d_teutonic_order
					add_friend = ROOT
				#	save_event_target_as = teutonic_hochmeister

					opinion = {
						name = opinion_formed_order
						who = ROOT
						years = 25
					}

					primary_title = {
						holy_order_set_law_effect = yes
					}
				}
			}

			narrative_event = { id = HFP.40031 } # Notify
			sound_effect = generic_click_01
		}
	}

	vassalize_teutonic_knights = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			religion_group = christian
			is_heretic = no

			any_owned_bloodline = {
				has_bloodline_flag = northern_crusade_bloodline
			}

			trigger_if = {
				limit = { is_title_active = d_teutonic_order }

				d_teutonic_order = {
					holder_scope = {
						independent = yes

						NOT = {
							has_opinion_modifier = {
								name = opinion_unfit_ruler
								who = ROOT
							}
						}
					}
				}
			}
			trigger_else = {
				is_title_active = k_teutonic_state

				k_teutonic_state = {
					holder_scope = {
						independent = yes

						NOT = {
							has_opinion_modifier = {
								name = opinion_unfit_ruler
								who = ROOT
							}
						}
					}
				}
			}

			OR = {
				# Prevent Army from being created at start
				has_global_flag = christian_crusades_unlocked
				has_global_flag = pagan_ghws_unlocked
				has_global_flag = muslim_jihads_unlocked
			}

			NOR = {
				has_character_modifier = expelled_d_teutonic_order
				has_character_modifier = expelled_k_teutonic_state

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			piety >= 300

			custom_tooltip = {
				text = higher_tier_than_teutonic_knights_TT

				trigger_if = {
					limit = { is_title_active = d_teutonic_order }
					higher_real_tier_than = DUKE
				}
				trigger_else = {
					is_title_active = k_teutonic_state
					real_tier = EMPEROR
				}
			}

			trigger_if = {
				limit = {
					d_teutonic_order = {
						holder_scope = {
							opinion = {
								who = ROOT
								value < 25
							}
						}
					}
				}

				custom_tooltip = {
					text = teutonic_order_opinion_TT

					always = no
				}
			}

			trigger_if = {
				limit = {
					k_teutonic_state = {
						holder_scope = {
							opinion = {
								who = ROOT
								value < 25
							}
						}
					}
				}

				custom_tooltip = {
					text = teutonic_state_opinion_TT
					always = no
				}
			}

			custom_tooltip = {
				text = form_teutonic_order_jerusalem_TT

				k_jerusalem = {
					holder_scope = {
						religion = ROOT
					}
				}
			}
		}

		effect = {
			piety = -300

			custom_tooltip = {
				text = vassalize_teutonic_knights_TT

				if = {
					limit = { is_title_active = d_teutonic_order }

					d_teutonic_order = {
						holder_scope = {
							set_defacto_liege = ROOT
						}
					}
				}
				else_if = {
					limit = { is_title_active = k_teutonic_state }

					k_teutonic_state = {
						holder_scope = {
							set_defacto_liege = ROOT
						}
					}
				}
			}
		}
	}

	hire_teutonic_honor_guard = {
		is_high_prio = yes

		ai = no

		potential = {
			any_owned_bloodline = {
				has_bloodline_flag = northern_crusade_bloodline
			}

			religion_group = christian
			is_heretic = no
			NOT = { has_earmarked_regiments = teutonic_honor_guard }

			OR = {
				is_title_active = d_teutonic_order
				is_title_active = k_teutonic_state
			}
		}

		allow = {
			war = yes
			piety >= 300

			trigger_if = {
				limit = {
					d_teutonic_order = {
						holder_scope = {
							is_foe = ROOT
						}
					}
				}

				d_teutonic_order = {
					show_scope_change = no

					holder_scope = {
						show_scope_change = no

						NOT = { is_foe = ROOT }
					}
				}
			}


			trigger_if = {
				limit = {
					k_teutonic_state = {
						holder_scope = {
							is_foe = ROOT
						}
					}
				}

				k_teutonic_state = {
					show_scope_change = no

					holder_scope = {
						show_scope_change = no

						NOT = {	is_foe = ROOT }
					}
				}
			}

			trigger_if = {
				limit = { has_character_modifier = hire_teutonic_honor_guard_cooldown }

				custom_tooltip = {
					text = hire_teutonic_honor_guard_cooldown_TT
					always = no
				}
			}
		}

		effect = {
			piety = -300

			spawn_unit = {
				owner = ROOT
				province = capital_scope
				home = capital_scope

				troops = {
					heavy_infantry = { 1500 1500 }
					light_infantry = { 1500 1500 }
					archers = { 1000 1000 }
				}

				attrition = 1.0
				disband_on_peace = yes
				maintenance_multiplier = 0.4
				earmark = teutonic_honor_guard
			}

			hidden_effect = {
				sound_effect = generic_click_01

				add_character_modifier = {
					name = hire_teutonic_honor_guard_cooldown
					hidden = yes
					years = 1
				}
			}
		}
	}

	form_holy_sepulchre = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			higher_real_tier_than = COUNT
			NOT = { is_title_active = d_holy_sepulchre }

			c_jerusalem = {
				holder_scope = {
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}
				}
			}

			is_orthodox_branch = yes

			OR = {
				# Prevent Army from being created at start
				has_global_flag = christian_crusades_unlocked
				has_global_flag = pagan_ghws_unlocked
				has_global_flag = muslim_jihads_unlocked
			}

			NOT = {
				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			is_heretic = no
			NOT = { trait = excommunicated }
			war = no
			wealth >= 500
			piety  >= 500

			custom_tooltip = {
				text = form_holy_sepulchre_jerusalem_TT

				c_jerusalem = {
					holder_scope = {
						OR = {
							character = ROOT
							is_vassal_or_below_of = ROOT
						}
					}
				}
			}
		}

		effect = {
			wealth = -500
			piety  = -500

			custom_tooltip = {
				text = form_holy_sepulchre_TT

				create_random_soldier = {
					random_traits = yes
					dynasty = random
					culture = greek
					religion = ROOT
					age = 25
				}

				new_character = {
					set_government_type = order_government
					add_trait = shrewd
					add_trait = zealous
					add_trait = diligent
					add_trait = inspiring_leader
					add_trait = organizer

					if = {
						limit = {
							NOT = { trait = brilliant_strategist }
						}

						remove_trait = misguided_warrior
						remove_trait = tough_soldier
						remove_trait = skilled_tactician
						add_trait = brilliant_strategist
					}

					grant_title_no_opinion = d_holy_sepulchre
					add_friend = ROOT

					opinion = {
						name = opinion_formed_order
						who = ROOT
						years = 25
					}

					primary_title = {
						holy_order_set_law_effect = yes
					}
				}
			}

			if = {
				limit = {
					higher_tier_than = DUKE

					NOT = {
						any_realm_lord = {
							holy_order = yes
						}
					}
				}

				custom_tooltip = {
					text = form_vassalize_holy_sepulchre_TT

					d_holy_sepulchre = {
						holder_scope = {
							set_defacto_liege = ROOT
						}
					}
				}
			}

			narrative_event = { id = HFP.40032 } # Notify
			sound_effect = generic_click_01
		}
	}

	vassalize_holy_sepulchre = {
		ai_check_interval = 60

		is_high_prio = yes

		only_playable = yes

		potential = {
			higher_tier_than = DUKE

			c_jerusalem = {
				holder_scope = {
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}
				}
			}

			is_orthodox_branch = yes

			d_holy_sepulchre = {
				holder_scope = {
					independent = yes

					NOT = {
						has_opinion_modifier = {
							name = opinion_unfit_ruler
							who = ROOT
						}
					}
				}
			}

			OR = {
				# Prevent Army from being created at start
				has_global_flag = christian_crusades_unlocked
				has_global_flag = pagan_ghws_unlocked
				has_global_flag = muslim_jihads_unlocked
			}

			NOR = {
				has_character_modifier = expelled_d_holy_sepulchre

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			independent = yes
			is_heretic = no
			NOT = { trait = excommunicated }
			war = no
			wealth >= 500
			piety  >= 500

			custom_tooltip = {
				text = form_holy_sepulchre_jerusalem_TT

				c_jerusalem = {
					holder_scope = {
						OR = {
							character = ROOT
							is_vassal_or_below_of = ROOT
						}
					}
				}
			}

			trigger_if = {
				limit = {
					d_holy_sepulchre = {
						holder_scope = {
							opinion = {
								who = ROOT
								value < 25
							}
						}
					}
				}

				custom_tooltip = {
					text = vassalize_holy_sepulchre_TT

					always = no
				}
			}

			custom_tooltip = {
				text = only_one_vassalized_holy_order_TT

				NOT = {
					any_realm_lord = {
						holy_order = yes
					}
				}
			}
		}

		effect = {
			wealth = -500
			piety  = -500

			custom_tooltip = {
				text = form_vassalize_holy_sepulchre_TT

				d_holy_sepulchre = {
					holder_scope = {
						set_defacto_liege = ROOT
					}
				}
			}
		}
	}
}

targeted_decisions = {

	order_to_join_holy_order = {
		filter = court
		ai_target_filter = court
		third_party_filter = all
		ai_check_interval = 1200

		show_third_party_potential = no

		only_playable = yes

		from_potential = {
			holy_order_check_active_trigger = yes
			is_heretic = no
			is_adult = yes
			has_regent = no
			prisoner = no
			mercenary = no
		}

		potential = {
			host = { character = FROM }
			religion = FROM
			is_adult = yes
			is_ruler = no
			is_female = no
			is_incapable = no
			liege = { holy_order = no }
		}

		third_party_potential = {
			FROMFROM = {
				holy_order = yes
				religion = ROOT_FROM
			}
		}

		allow = {
			trigger_if = {
				limit = { prisoner = no }

				is_married = no

				custom_tooltip = {
					text = NOT_HEIR_TITLE_TT

					NOT = {
						any_heir_title = { always = yes }
					}
				}
			}

			FROM = {
				piety >= 50
			}
		}

		effect = {
			imprison = no

			FROM = {
				show_scope_change = no
				piety = -50
			}

			if = {
				limit = {
					OR = {
						trait = content
						trait = monk
						trait = nun
						trait = zealous
						trait = humble
						trait = brave
						trait = crusader
						trait = celibate
					}
				}

				move_character = FROMFROM

				hidden_effect = {
					add_trait = celibate
				}
			}
			else = {
				random_list = {
					80 = {
						# Intrigue based chance modifiers for FROM
						mult_modifier = {
							factor = 0.5
							FROM = { intrigue < 5 }
						}

						mult_modifier = {
							factor = 1.5
							FROM = { intrigue >= 10 }
						}

						mult_modifier = {
							factor = 1.5
							FROM = { intrigue >= 15 }
						}

						mult_modifier = {
							factor = 1.5
							FROM = { intrigue >= 20 }
						}

						custom_tooltip = {
							text = join_holy_order_success_TT

							move_character = FROMFROM
							add_trait = celibate
						}
					}

					20 = {
						# Traits making ROOT more likely to join a Holy Order
						mult_modifier = {
							factor = 0.5
							trait = diligent
						}

						mult_modifier = {
							factor = 0.5
							trait = temperate
						}

						mult_modifier = {
							factor = 0.5
							trait = chaste
						}

						mult_modifier = {
							factor = 0.5
							trait = brave
						}

						mult_modifier = {
							factor = 0.5
							is_married = yes
						}

						mult_modifier = {
							factor = 0.5
							trait = holy_warrior
						}

						# Traits making ROOT less likely to join a Holy order

						mult_modifier = {
							factor = 2
							trait = ambitious
						}

						mult_modifier = {
							factor = 1.5
							trait = greedy
						}

						mult_modifier = {
							factor = 1.5
							trait = cynical
						}

						mult_modifier = {
							factor = 1.5
							trait = envious
						}

						mult_modifier = {
							factor = 1.5
							trait = proud
						}

						# Intrigue based chance modifiers for ROOT
						mult_modifier = {
							factor = 0.5
							intrigue < 5
						}

						mult_modifier = {
							factor = 1.5
							intrigue >= 10
						}

						mult_modifier = {
							factor = 1.5
							intrigue >= 15
						}

						mult_modifier = {
							factor = 1.5
							intrigue >= 20
						}

						mult_modifier = {
							factor = 1.5
							intrigue >= 25
						}

						# Tooltip
						custom_tooltip = {
							text = join_holy_order_flee_TT

							banish = yes
							add_rival = FROM
						}
					}
				}
			}
		}

		# CleanSlate: Copied from order_to_take_vows, because that's better than pure randomness
		ai_will_do = {
			factor = 1

			trigger = {
				FROM = {
					opinion = {
						who = ROOT
						value < 50
					}

					# If I'm a good ruler...
					trigger_if = {
						limit = { is_benevolent_trigger = yes }

						# ...you must not be a dynastic heir
						NAND = {
							is_parent_of = ROOT
							dynasty = ROOT

							opinion = {
								who = ROOT
								value >= 0
							}
						}
					}
					trigger_else = {
						# Otherwise I must not hate you enough to just send you off
						OR = {
							ROOT = { prisoner = no }
							is_parent_of = ROOT

							opinion = {
								who = ROOT
								value >= -20
							}
						}
					}
				}
			}

			# Must protect the line
			mult_modifier = {
				factor = 5

				ROOT = {
					is_child_of = FROM
					NOT = { dynasty = FROM }
				}
			}

			# Hate you
			mult_modifier = {
				factor = 5

				ROOT = {
					is_child_of = FROM
					is_rival = FROM
				}
			}

			# There are better spares
			mult_modifier = {
				factor = 20

				ROOT = {
					is_child_of = FROM

					OR = {
						has_negative_congenital_trigger = yes
						has_infertile_trait_trigger = yes
						has_severe_disability_trigger = yes
					}
				}

				FROM = {
					any_child = {
						is_alive = yes
						has_negative_congenital_trigger = no
						has_infertile_trait_trigger = no
						has_severe_disability_trigger = no
					}
				}
			}

			# Must keep the realm from falling to evil
			mult_modifier = {
				factor = 5

				ROOT = { is_evil_trigger = yes }

				FROM = {
					is_benevolent_trigger = yes

					any_child = {
						is_alive = yes
						is_evil_trigger = no
					}
				}
			}
		}
	}

	ask_teutonic_for_help_teutonic = {
		# filter = independent_rulers # CleanSlate: Nonfunctional
		ai_target_filter = independent_rulers
		third_party_filter = independent_rulers
		ai_check_interval = 60

		show_third_party_potential = no

		only_playable = yes

		from_potential = {
			has_global_flag = northern_crusades_active
			independent = yes
			is_catholic_branch = yes
			is_heretic = no

			any_demesne_title = {
				higher_real_tier_than = DUKE

				capital_scope = {
					OR = {
						region = world_europe_north
						region = custom_eastern_baltic
						region = custom_russia
					}
				}
			}

			NOR = {
				has_global_flag = northern_crusade_failure
				has_character_flag = northern_crusade_asked_for_help
			}
		}

		potential = {
			religion = FROM
			independent = yes

			OR = {
				has_landed_title = d_teutonic_order
				has_landed_title = k_teutonic_state
			}

			NOT = {
				any_war = {
					using_cb = northern_crusade_war
				}
			}
		}

		third_party_potential = {
			FROMFROM = {
				independent = yes
				holy_order = no
				mercenary = no

				NOR = {
					religion_group = christian
					reverse_has_truce = ROOT
				}

				any_demesne_title = {
					higher_real_tier_than = BARON

					OR = {
						region = world_europe_north
						region = custom_eastern_baltic
						region = custom_russia
					}
				}

				trigger_if = {
					limit = { has_global_flag = northern_crusade_active }

					any_demesne_title = {
						kingdom = {
							title = event_target:northern_crusade_kingdom_title
						}
					}
				}
				trigger_else = {
					higher_real_tier_than = COUNT
				}
			}
		}

		allow = {
			FROM = {
				show_scope_change = no

				piety >= 200

			#	custom_tooltip = {
			#		text = northern_crusade_targeted_decision_TT
			#
			#		d_teutonic_order = {
			#			holder_scope = {
			#				opinion = {
			#					who = PREVPREV
			#					value >= 10
			#				}
			#			}
			#		}
			#	}
			}

			any_independent_ruler = {
				NOT = { religion_group = christian }
				holy_order = no
				mercenary = no

				trigger_if = {
					limit = { has_global_flag = northern_crusade_active }

					custom_tooltip = {
						text = northern_crusade_fitting_title_TT

						any_demesne_province = {
							owned_by = PREV

							kingdom = {
								title = event_target:northern_crusade_kingdom_title
							}

							OR = {
								region = world_europe_north
								region = custom_eastern_baltic
								region = custom_russia
							}
						}
					}
				}
				trigger_else = {
					custom_tooltip = {
						text = higher_tier_than_count_TT
						higher_real_tier_than = COUNT
					}

					custom_tooltip = {
						text = northern_crusade_fitting_title_02_TT
					}
				}

				custom_tooltip = {
					text = not_reverse_truce_TT
					NOT = { reverse_has_truce = ROOT }
				}
			}

			trigger_if = {
				limit = {
					FROM = { has_character_modifier = northern_crusade_targeted_decision_cooldown }
				}

				custom_tooltip = {
					text = northern_crusade_decision_cooldown_TT
					always = no
				}
			}
		}

		effect = {
			hidden_effect = {
				FROM = {
					set_character_flag = northern_crusade_asked_for_help
					save_event_target_as = northern_crusade_asker

					add_character_modifier = {
						name = northern_crusade_targeted_decision_cooldown
						months = 6
						hidden = yes
					}
				}

				FROMFROM = {
					save_event_target_as = northern_crusade_potential_target
				}

				if = {
					limit = { is_title_active = d_teutonic_order }

					d_teutonic_order = {
						holder_scope = {
							character_event = { id = HF.49105 } # Teutonic Order receives target suggestion
						}
					}
				}
				else_if = {
					limit = { is_title_active = k_teutonic_state }

					k_teutonic_state = {
						holder_scope = {
							character_event = { id = HF.49105 } # Teutonic Order receives target suggestion
						}
					}
				}
			}
		}
	}

	ask_teutonic_for_help_pagan = {
		# filter = independent_rulers # CleanSlate: Nonfunctional

		ai = no

		from_potential = {
			has_global_flag = northern_crusades_active
			is_catholic_branch = yes
			is_heretic = no
			independent = yes

			OR = {
				is_title_active = d_teutonic_order
				is_title_active = k_teutonic_state
			}

			any_demesne_title = {
				higher_tier_than = DUKE

				capital_scope = {
					OR = {
						region = world_europe_north
						region = custom_eastern_baltic
						region = custom_russia
					}
				}
			}

			NOR = {
				has_global_flag = northern_crusade_failure
				has_character_flag = northern_crusade_asked_for_help
			}
		}

		potential = {
			holy_order = no
			mercenary = no
			independent = yes
			NOT = { religion_group = christian }

			trigger_if = {
				limit = { has_global_flag = northern_crusade_active }

				any_demesne_title = {
					kingdom = {
						title = event_target:northern_crusade_kingdom_title
					}
				}
			}
			trigger_else = {
				higher_real_tier_than = COUNT
			}

			any_demesne_title = {
				higher_real_tier_than = BARON

				OR = {
					region = world_europe_north
					region = custom_eastern_baltic
					region = custom_russia
				}
			}

			NOR = {
				d_teutonic_order = {
					holder_scope = {
						OR = {
							has_truce = ROOT

							any_war = {
								using_cb = northern_crusade_war
							}
						}
					}
				}

				k_teutonic_state = {
					holder_scope = {
						OR = {
							has_truce = ROOT

							any_war = {
								using_cb = northern_crusade_war
							}
						}
					}
				}
			}
		}

		allow = {
			d_teutonic_order = {
				show_scope_change = no

				trigger_if = {
					limit = { has_holder = yes }

					holder_scope = {
						show_scope_change = no

						religion = FROM
					}
				}
			}

			k_teutonic_state = {
				show_scope_change = no

				trigger_if = {
					limit = { has_holder = yes }

					holder_scope = {
						show_scope_change = no

						religion = FROM
					}
				}
			}

			FROM = {
				show_scope_change = no

				piety >= 200

			#	custom_tooltip = {
			#		text = northern_crusade_targeted_decision_TT
			#
			#		d_teutonic_order = {
			#			holder_scope = {
			#				opinion = {
			#					who = PREVPREV
			#					value >= 10
			#				}
			#			}
			#		}
			#	}
			}

			trigger_if = {
				limit = { has_global_flag = northern_crusade_active }

				custom_tooltip = {
					text = northern_crusade_correct_kingdom_teutonic_order_TT

					any_demesne_title = {
						higher_real_tier_than = BARON

						OR = {
							region = world_europe_north
							region = custom_eastern_baltic
							region = custom_russia
						}

						kingdom = {
							title = event_target:northern_crusade_kingdom_title
						}
					}
				}
			}

			trigger_else = {
				higher_real_tier_than = COUNT
				custom_tooltip = { text = northern_crusade_fitting_title_02_TT }
			}

			trigger_if = {
				limit = {
					FROM = { has_character_modifier = northern_crusade_targeted_decision_cooldown }
				}

				custom_tooltip = {
					text = northern_crusade_decision_cooldown_TT
					always = no
				}
			}

			trigger_if = {
				limit = {
					trigger_if = {
						limit = { is_title_active = d_teutonic_order }

						d_teutonic_order = {
							holder_scope = {
								war = yes
							}
						}
					}
					trigger_else = {
						is_title_active = k_teutonic_state

						k_teutonic_state = {
							holder_scope = {
								war = yes
							}
						}
					}
				}

				custom_tooltip = {
					text = teutonic_order_at_peace_TT
					always = no
				}
			}

			NOT = { religion_group = christian }
			holy_order = no
			mercenary = no

			custom_tooltip = {
				text = not_reverse_truce_TT
				NOT = { reverse_has_truce = ROOT }
			}
		}

		effect = {
			save_event_target_as = northern_crusade_potential_target

			FROM = {
				set_character_flag = northern_crusade_asked_for_help
				save_event_target_as = northern_crusade_asker

				add_character_modifier = {
					name = northern_crusade_targeted_decision_cooldown
					months = 6
					hidden = yes
				}
			}

			if = {
				limit = { is_title_active = d_teutonic_order }

				d_teutonic_order = {
					holder_scope = {
						character_event = { id = HF.49105 } # Teutonic Order receives target suggestion
					}
				}
			}
			else_if = {
				limit = { is_title_active = k_teutonic_state }

				k_teutonic_state = {
					holder_scope = {
						character_event = { id = HF.49105 } # Teutonic Order receives target suggestion
					}
				}
			}
		}
	}
}