decisions = {

	hold_a_epic_tournament = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			independent = yes
			higher_real_tier_than = DUKE
			is_landed = yes
			is_nomadic = no

			NOT = { religion_group = muslim }
			religion_openly_celtic_or_reformed_trigger = no

			trigger_if = {
				limit = { is_female = yes }
				gender_equality_trigger = yes
			}

			OR = {
				is_crowned_trigger = yes
				has_horde_culture = yes
				is_feudal = no
			}

			NOR = {
				has_character_modifier = epic_tournament
				has_character_modifier = epic_tournament_furusiyya
			}

			trigger_if = {
				limit = { has_character_flag = tournament_cooldown }

				had_character_flag = {
					flag = tournament_cooldown
					years >= 1
				}
			}
		}

		allow = {
			prestige >= 500
			wealth >= 200
			ruled_years >= 5

			trigger_if = {
				limit = { is_female = yes }

				OR = {
					custom_tooltip = {
						text = FEMALE_CAN_FIGHT_IN_TOURNAMENT

						OR = {
							trait = martial_lady_pagan
							trait = martial_lady
							trait = martial_lady_tengri
						}
					}

					custom_tooltip = {
						text = HAS_CHAMPION_AVAILABLE

						any_courtier_or_vassal = {
							OR = {
								has_minor_title = title_ladys_champion
								has_minor_title = title_champion
							}

							prisoner = no
							is_incapable = no
						}
					}
				}
			}

			is_abroad = no
			is_ill = no
			war = no
			in_command_trigger = no

			custom_tooltip = {
				text = NO_OCCUPIED_DEMESNE_TITLE

				NOT = {
					any_demesne_title = {
						lower_tier_than = DUKE
						is_occupied = yes
					}
				}
			}

			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			wealth = -200
			prestige = 100

			add_character_modifier = {
				name = epic_tournament
				years = 100
				hidden = yes
			}

			character_event = {
				id = 70033
				tooltip = tournament_invite
			}

			hidden_effect = {
				add_character_modifier = {
					name = holding_epic_tournament
					duration = 210
				}

				add_character_modifier = {
					name = do_not_disturb
					days = 210
					hidden = yes
				}

				character_event = {
					id = 70001 # Tournament start
					days = 60
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				wealth >= 300

				NOT = { has_character_modifier = ai_austerity }
			}
		}
	}

	buy_indulgence_for_sins = {
		ai_check_interval = 6

		only_playable = yes

		potential = {
			OR = {
				religion = catholic

				AND = {
					religion = chalcedonian
					rightful_religious_head_scope = { trait = latin_rites }
				}
			}

			is_adult = yes
			is_landed = yes

			NOR = {
				has_character_flag = ask_for_indulgence
				trait = excommunicated
				rightful_religious_head = THIS
			}
		}

		allow = {
			wealth >= 50

			custom_tooltip = {
				text = RECENT_INDULGENCE

				NOT = { has_character_modifier = indulgence_timer }
			}

			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			wealth = -50
			piety = 25

			rightful_religious_head_scope = {
				show_scope_change = no

				opinion = {
					name = bought_indulgences
					who = ROOT
					years = 3
				}
			}

			pf_tradition_plus1_effect = yes

			if = {
				limit = { ai = no }

				add_character_modifier = {
					name = indulgence_timer
					years = 3
					hidden = yes
				}
			}
			else = {
				add_character_modifier = {
					name = indulgence_timer
					years = 10
					hidden = yes
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				piety < 100

				trigger_if = {
					limit = { piety > 0 }

					wealth >= 200

					NOR = {
						trait = cynical
						trait = cruel
						trait = greedy
					}
				}
			}
		}
	}

	issue_declaration_of_repentance = {
		ai_check_interval = 80

		is_high_prio = yes

		only_playable = yes

		potential = {
			higher_real_tier_than = BARON # Low nobility requires somebody else to ask their religious head to Lift the Excommunication on their behalf
			is_landed = yes
			is_adult = yes

			rightful_religious_head_scope = {
				ROOT = { excommunicated_for = PREV }
			}
		}

		allow = {
			is_heretic = no
			is_not_in_regency_trigger = yes

			custom_tooltip = {
				text = tooltip_issued_decla_repentance_recently

				NOT = { has_character_modifier = asked_repentance_from_sins_cooldown }
			}
		}

		effect = {
			add_character_modifier = {
				name = asked_repentance_from_sins_cooldown
				years = 5
				hidden = yes
			}

			rightful_religious_head_scope = {
				show_scope_change = no

				character_event = {
					id = HFP.23003
					days = 5
					random = 12
					tooltip = repentance_ask
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				NOR = {
					trait = stubborn
					trait = cynical
				}
			}

			mult_modifier = {
				factor = 0.25
				NOT = { scaled_wealth = 2.0 }
			}

			mult_modifier = {
				factor = 2
				trait = zealous
			}
		}
	}

	demand_liege_title = {
		is_high_prio = yes

		ai = no # The AI is limited to the similar event 63040

		potential = {
			real_tier = COUNT
			independent = no
			NOT = { has_character_flag = requested_de_jure_title }

			liege = {
				higher_real_tier_than = DUKE

				any_demesne_title = {
					tier = DUKE
					has_de_jure_pretension = ROOT
					can_be_given_away = yes

					NOT = {
						any_direct_de_jure_vassal_title = {
							holder = PREVPREV
						}
					}
				}
			}
		}

		allow = {
			liege = {
				opinion = {
					who = ROOT
					value >= 25
				}
			}

			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_character_flag = requested_de_jure_title

			liege = {
				show_scope_change = no

				random_demesne_title = {
					limit = {
						tier = DUKE
						has_de_jure_pretension = ROOT
						can_be_given_away = yes

						NOT = {
							any_direct_de_jure_vassal_title = {
								holder = PREVPREV
							}
						}
					}

					save_event_target_as = vassal_wanted_title
				}

				set_character_flag = wants_duchy_title

				letter_event = {
					id = 63041 # "vassal_request_events.txt"
					days = 4
					tooltip = EVTTOOLTIP63041
				}
			}
		}

		ai_will_do = {
			factor = 0 # The AI is limited to the similar event 63040
		}
	}

	sadaqah_saddka = {
		ai_check_interval = 24

		only_playable = yes

		potential = {
			OR = {
				is_sunni_branch = yes
				is_shiite_branch = yes
			}

			is_landed = yes
			NOT = { rightful_religious_head = THIS }
		}

		allow = {
			wealth >= 50

			custom_tooltip = {
				text = RECENTLY_GAVE_TO_CHARITY

				NOT = { has_character_modifier = charity_timer }
			}

			is_not_in_regency_trigger = yes
		}

		effect = {
			wealth = -50
			piety = 25

			rightful_religious_head_scope = {
				show_scope_change = no

				opinion = {
					name = opinion_sadaqah
					who = ROOT
					years = 3
				}
			}

			pf_tradition_plus1_effect = yes

			if = {
				limit = { ai = no }

				add_character_modifier = {
					name = indulgence_timer
					years = 3
					hidden = yes
				}
			}
			else = {
				add_character_modifier = {
					name = indulgence_timer
					years = 10
					hidden = yes
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				higher_real_tier_than = COUNT
				piety < 100
				wealth >= 200

				NOR = {
					trait = cynical
					trait = cruel
					trait = greedy
					has_character_modifier = ai_austerity
				}
			}
		}
	}

	hold_a_furusiyya = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			independent = yes
			religion_group = muslim
			higher_real_tier_than = DUKE

			trigger_if = {
				limit = { is_female = yes }
				gender_equality_trigger = yes
			}

			location = {
				owner = { character = ROOT }
			}

			NOR = {
				has_character_modifier = epic_tournament_furusiyya
				has_character_modifier = epic_tournament
			}
		}

		allow = {
			prestige >= 500
			wealth   >= 200

			trigger_if = {
				limit = { is_female = yes }

				custom_tooltip = {
					text = HAS_CHAMPION_AVAILABLE

					any_courtier_or_vassal = {
						OR = {
							has_minor_title = title_ladys_champion
							has_minor_title = title_champion
						}

						prisoner = no
						is_incapable = no
					}
				}
			}

			war = no
			in_command_trigger = no
			is_ill = no

			custom_tooltip = {
				text = NO_OCCUPIED_DEMESNE_TITLE

				NOT = {
					any_demesne_title = {
						lower_tier_than = DUKE
						is_occupied = yes
					}
				}
			}

			is_inaccessible_trigger = no
		}

		effect = {
			wealth  = -200
			prestige = 100

			if = {
				limit = {
					dynasty_head = { character = ROOT }
				}
				piety = 50
			}

			add_character_modifier = {
				name = epic_tournament_furusiyya
				years = 100
				hidden = yes
			}

			character_event = {
				id = 88283
				tooltip = tournament_invite
			}

			hidden_effect = {
				add_character_modifier = {
					name = holding_epic_tournament
					duration = 210
				}

				add_character_modifier = {
					name = do_not_disturb
					days = 210
					hidden = yes
				}

				character_event = {
					id = 70001 # Tournament start
					days = 60
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				wealth >= 300
				NOT = { has_character_modifier = ai_austerity }
			}
		}
	}

	restore_roman_empire = {
		ai_check_interval = 12

		is_high_prio = yes

		only_independent = yes

		potential = {
			primary_title = {
				OR = {
					has_title_flag = eastern_roman_successor
					has_title_flag = western_roman_successor
				}
			}

			has_dlc = "Legacy of Rome"

			NOR = {
				has_global_flag = rome_restored

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

		allow = {
			prestige >= 3000

			trigger_if = {
				trigger = { NOT = { completely_controls = d_latium } }
				completely_controls = d_latium
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_venice } }
				completely_controls = d_venice
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_sicily } }
				completely_controls = d_sicily
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_genoa } }
				completely_controls = d_genoa
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_capua } }
				completely_controls = d_capua
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_apulia } }
				completely_controls = d_apulia
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_thrace } }
				completely_controls = d_thrace
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_karvuna } }
				completely_controls = d_karvuna
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_thessalonika } }
				completely_controls = d_thessalonika
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_moesia } }
				completely_controls = d_moesia
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_antioch } }
				completely_controls = d_antioch
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_jerusalem } }
				completely_controls = d_jerusalem
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_alexandria } }
				completely_controls = d_alexandria
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_hellas } }
				completely_controls = d_hellas
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_tunis } }
				completely_controls = d_tunis
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_croatia } }
				completely_controls = d_croatia
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_hum } }
				completely_controls = d_hum
			}
			trigger_if = {
				limit = { NOT = { completely_controls = d_dioclea } }
				completely_controls = d_dioclea
			}

			is_not_in_regency_trigger = yes
		}

		effect = {
			if = {
				limit = {
					NOT = { is_title_active = e_roman_empire }
				}

				hidden_effect = {
					activate_title = {
						title = e_roman_empire
						status = yes
					}
				}
			}

			if = {
				limit = {
					primary_title = {
						has_title_flag = eastern_roman_successor
					}
				}

				e_roman_empire = {
					show_scope_change = no

					set_pentarch_title = b_hagiasophia
				}
			}

			narrative_event = {
				id = LoR.20
				tooltip = EVTTOOLTIPLoR.20
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	mend_great_schism = {
		ai_check_interval = 12

		is_high_prio = yes

		only_playable = yes

		potential = {
			OR = {
				controls_religion = yes

				AND = {
					real_tier = EMPEROR
					religion_head = { always = yes }
				}
			}

			# There must be a non-heretic religion head with whom to negotiate
			trigger_if = {
				limit = { is_catholic_branch = yes }

				OR = {
					k_orthodox = { holder_scope = { is_heretic = no } }
					d_monothelite = { holder_scope = { is_heretic = no } }
					d_iconoclast = { holder_scope = { is_heretic = no } }
					d_bogomilist = { holder_scope = { is_heretic = no } }
				}
			}
			trigger_else = {
				is_orthodox_branch = yes

				OR = {
					k_papacy = { holder_scope = { is_heretic = no } }
					d_fraticelli = { holder_scope = { is_heretic = no } }
					d_cathar = { holder_scope = { is_heretic = no } }
				}
			}

			has_dlc = "Legacy of Rome"
			has_global_flag = great_schism_happened
			is_nomadic = no
			is_tribal = no
			is_heretic = no

			NOR = {
				has_global_flag = great_schism_mending
				has_global_flag = great_schism_mended
				has_character_flag = great_schism_mender
			}
		}

		allow = {
			piety >= 2000
			religion_authority = 0.9

			OR = {
				controls_religion = yes
				religion_head = { is_vassal_or_below_of = ROOT }
				completely_controls = c_constantinopolis
				completely_controls = c_jerusalem
				completely_controls = c_roma
			}

			b_hagiasophia = {
				show_scope_change = no

				custom_tooltip = {
					text = CONTROL_RELIGION_NO_WAR

					has_holder = yes

					holder_scope = {
						religion = ROOT
						war = no
					}
				}
			}

			b_antioch = {
				show_scope_change = no

				custom_tooltip = {
					text = CONTROL_RELIGION_NO_WAR

					has_holder = yes

					holder_scope = {
						religion = ROOT
						war = no
					}
				}
			}

			b_jerusalem = {
				show_scope_change = no

				custom_tooltip = {
					text = CONTROL_RELIGION_NO_WAR

					has_holder = yes

					holder_scope = {
						religion = ROOT
						war = no
					}
				}
			}

			b_alexandria = {
				show_scope_change = no

				custom_tooltip = {
					text = CONTROL_RELIGION_NO_WAR

					has_holder = yes

					holder_scope = {
						religion = ROOT
						war = no
					}
				}
			}

			b_roma = {
				show_scope_change = no

				custom_tooltip = {
					text = CONTROL_RELIGION_NO_WAR

					has_holder = yes

					holder_scope = {
						religion = ROOT
						war = no
					}
				}
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_global_flag = great_schism_mending

			long_character_event = {
				id = PlusGreatSchism.061
				tooltip = EVTTOOLTIPLoR.210
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				NOR = {
					trait = cynical
					trait = bad_priest_christian
				}
			}
		}
	}

	claim_constantinople = {
		is_high_prio = yes

		only_independent = yes

		potential = {
			has_landed_title = e_byzantium
			has_dlc = "Legacy of Rome"
			is_nomadic = no
			NOT = { has_landed_title = c_constantinopolis }

			c_constantinopolis = {
				holder_scope = {
					is_vassal_or_below_of = ROOT
					ai = yes
				}
			}
		}

		allow = {
			NOT = { has_landed_title = c_constantinopolis }

			c_constantinopolis = {
				holder_scope = {
					is_vassal_or_below_of = ROOT
				}
			}
		}

		effect = {
			c_constantinopolis = {
				show_scope_change = no

				usurp_title_plus_barony_if_unlanded_and_vassals = ROOT

				# Also take over suitable holding if necessary,
				# to prevent title being handed out by AI,
				# then usurped by this decision ad infinitum
				location = { # Because 'capital_holding' doesn't work in county title scope...
					show_scope_change = no

					if = {
						limit = { capital_holding = { NOT = { is_allowed_holding_type = ROOT } } }

						random_province_holding = {
							show_scope_change = no

							limit = {
								is_allowed_holding_type = ROOT
								holder_scope = { ai = yes }
							}

							preferred_limit = { title = b_constantinople }

							usurp_title = ROOT
						}
					}
				}
			}

			# Restore Hagia Sophia back to the patriarch, if necessary
			if = {
				limit = {
					is_orthodox_branch = yes

					trigger_if = {
						limit = { religion = iconoclast }

						d_iconoclast = {
							holder_scope = {
								is_vassal_or_below_of = ROOT
								NOT = { has_landed_title = b_hagiasophia }
							}
						}
					}
					trigger_else = {
						k_orthodox = {
							holder_scope = {
								vassal_of = ROOT
								NOT = { has_landed_title = b_hagiasophia }
							}
						}
					}

					b_hagiasophia = {
						holder_scope = {
							trigger_if = {
								limit = { NOT = { character = ROOT } }

								ai = yes
								is_vassal_or_below_of = ROOT
							}
						}
					}
				}

				if = {
					limit = { religion = iconoclast }

					d_iconoclast = {
						show_scope_change = no

						holder_scope = {
							show_scope_change = no
							usurp_title = b_hagiasophia
						}
					}
				}
				else = {
					k_orthodox = {
						show_scope_change = no

						holder_scope = {
							show_scope_change = no
							usurp_title = b_hagiasophia
						}
					}
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	make_rome_capital = {
		is_high_prio = yes

		ai = no

		potential = {
			has_dlc = "Legacy of Rome"
			has_landed_title = e_roman_empire

			capital_scope = {
				NOT = { province_id = c_roma }
			}

			c_roma = {
				holder_scope = {
					is_vassal_or_below_of = ROOT
					ai = yes
				}
			}

			b_tivoli = {
				holder_scope = {
					is_vassal_or_below_of = ROOT
					ai = yes
				}
			}
		}

		allow = {
			has_landed_title = e_roman_empire

			c_roma = {
				holder_scope = {
					is_vassal_or_below_of = ROOT
				}
			}
		}

		effect = {
			custom_tooltip = {
				text = rome_capital

				b_tivoli = {
					make_capital_holding = yes
					usurp_title = ROOT
					set_name = Rome
				}

				capital = b_tivoli
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	petition_doge_for_tp = {
		ai_check_interval = 6

		is_high_prio = yes

		only_playable = yes

		potential = {
			is_patrician = yes
			lower_real_tier_than = DUKE

			trigger_if = {
				limit = { num_of_trade_posts > 0 }

				liege = {
					any_vassal = {
						is_patrician = yes

						num_of_trade_post_diff = {
							character = ROOT
							value >= 10
						}
					}
				}
			}
			trigger_else = {
				liege = {
					any_vassal = {
						is_patrician = yes
						num_of_trade_posts >= 10
					}
				}
			}

			NOT = { has_character_flag = petition_doge_for_tp_taken }
		}

		allow = {
			liege = {
				opinion = {
					who = ROOT
					value >= 25
				}
			}
		}

		effect = {
			set_character_flag = petition_doge_for_tp_taken

			liege = {
				letter_event = {
					id = REP.500
					days = 5
					tooltip = EVTTOOLTIP_REP_500
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	usurp_tp = {
		ai_check_interval = 6

		is_high_prio = yes

		only_playable = yes

		potential = {
			is_merchant_republic = yes
			NOT = { has_character_flag = usurp_tp_taken }

			trigger_if = {
				limit = { num_of_trade_posts > 0 }

				any_vassal = {
					is_patrician = yes

					num_of_trade_post_diff = {
						character = ROOT
						value >= 10
					}
				}
			}
			trigger_else = {
				any_vassal = {
					is_patrician = yes
					num_of_trade_posts >= 10
				}
			}
		}

		allow = {
			is_merchant_republic = yes
		}

		effect = {
			set_character_flag = usurp_tp_taken

			if = {
				limit = { num_of_trade_posts > 0 }

				random_vassal = {
					limit = {
						is_patrician = yes

						num_of_trade_post_diff = {
							character = ROOT
							value >= 10
						}
					}

					letter_event = {
						id = REP.510
						days = 5
						tooltip = EVTTOOLTIP_REP_510
					}
				}
			}
			else = {
				random_vassal = {
					limit = {
						is_patrician = yes
						num_of_trade_posts >= 10
					}

					letter_event = {
						id = REP.510
						days = 5
						tooltip = EVTTOOLTIP_REP_510
					}
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	organize_varangian_guard = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			OR = {
				has_landed_title = e_byzantium
				has_landed_title = e_roman_empire
			}

			has_global_flag = viking_age_started
			NOT = { has_global_flag = varangian_guard_founded }

			trigger_if = {
				limit = { is_female = yes }
				gender_equality_trigger = yes
			}

			any_playable_ruler = {
				religion = norse_pagan

				capital_scope = {
					distance = {
						where = c_constantinopolis
						value >= 400 # Not too far away
					}
				}
			}
		}

		allow = {
			prestige >= 300
			wealth   >= 300
			is_not_in_regency_trigger = yes
		}

		effect = {
			set_global_flag = varangian_guard_founded

			prestige = -300
			wealth   = -300

			hidden_tooltip = {
				activate_title = {
					title = d_varangian_guard
					status = yes
				}

				narrative_event = { id = TOG.3100 }

				if = {
					limit = { gender_equality_trigger = yes }

					create_character = {
						random_traits = yes
						religion = norse_pagan
						culture = norse
						dynasty = none
						female = 50
						age = 34
						trait = skilled_tactician
					}
				}
				else = {
					create_character = {
						random_traits = yes
						religion = norse_pagan
						culture = norse
						dynasty = none
						female = no
						age = 34
						trait = skilled_tactician
					}
				}

				new_character = {
					add_trait = varangian
					set_character_flag = is_chosen_varangian_leader
					grant_title = d_varangian_guard
					set_defacto_liege = ROOT
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	create_hungary = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			has_landed_title = k_magyar
			culture = hungarian
			independent = yes
			NOT = { has_dlc = "Horse Lords" }

			k_hungary = {
				has_holder = no
			}

			k_magyar = {
				is_vice_royalty = no
			}

			k_carpathia = {
				is_titular = no
			}
		}

		allow = {
			completely_controls = d_pest

			OR = {
				completely_controls = d_transylvania
				completely_controls = d_ungvar
				completely_controls = d_nyitra
			}

			ruled_years >= 5
			is_not_unreformed_pagan_trigger = yes
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_global_flag = avar_khaganate_renamed
			reset_coa = k_hungary

			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_great
			}

			set_government_type = feudal_government

			narrative_event = {
				id = 62910
				tooltip = EVTTOOLTIP62910
			}

			hidden_effect = {
				disband_event_forces = king_magyar_forces

				d_pecs = { de_jure_liege = k_hungary }
				d_esztergom = { de_jure_liege = k_hungary }
				d_nyitra = { de_jure_liege = k_hungary }
				d_ungvar = { de_jure_liege = k_hungary }
				d_pest = { de_jure_liege = k_hungary }
				d_transylvania = { de_jure_liege = k_hungary }
				d_temes = { de_jure_liege = k_hungary }
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	become_saoshyant = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			independent = yes
			higher_real_tier_than = DUKE

			OR = {
				religion = zoroastrian
				religion = mazdaki
				religion = zurvanist
			}

			has_dlc = "The Old Gods"
			is_random_world = no

			NOR = {
				has_global_flag = saoshyant_appears

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

		allow = {
			has_landed_title = e_persia # TODO: Generalize
			completely_controls_region = world_middle_east_persia

			OR = {
				piety >= 2000

				AND = {
					piety >= 1500

					OR = {
						trait = grey_eminence
						trait = brilliant_strategist
						trait = midas_touched
						trait = elusive_shadow
						trait = mastermind_theologian
						trait = genius
					}
				}
			}
		}

		effect = {
			set_global_flag = saoshyant_appears
			give_nickname = nick_the_saoshyant
			save_event_target_as = pahlavi_creator
			add_trait = saoshyant

			custom_tooltip = {
				text = tooltip_forge_pahalavi_crown

				add_artifact = crown_pahlavi

				new_artifact = {
					set_name = crown_pahlavi_name
				}
			}

			prestige = 500
			piety = 500

			if = {
				limit = { has_dlc = "Holy Fury" }

				if = {
					limit = { is_female = yes }

					create_bloodline = {
						type = saoshyant_female
					}

					new_bloodline = {
						set_description = bloodline_desc_saoshyant
					}
				}
				else = {
					create_bloodline = {
						type = saoshyant_male
					}

					new_bloodline = {
						set_description = bloodline_desc_saoshyant
					}
				}
			}

			narrative_event = { id = TOG.6000 }

			any_child = {
				character_event = { id = TOG.6001 }
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	restore_priesthood = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			independent = yes
			religion = zoroastrian
			has_dlc = "The Old Gods"
			is_random_world = no

			NOR = {
				has_global_flag = zoroastrian_priesthood_founded

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

		allow = {
			real_tier = EMPEROR
			num_of_holy_sites >= 3
			religion_authority = 0.60
			realm_learning >= 25
			piety >= 500
			wealth >= 500
			is_heretic = no
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			piety = -300
			set_global_flag = zoroastrian_priesthood_founded

			random_realm_lord = {
				limit = {
					is_theocracy = yes
					religion = ROOT

					any_demesne_title = {
						is_holy_site = ROOT
					}
				}

				preferred_limit = {
					vassal_of = ROOT
				}

				grant_title = d_zoroastrian
				set_defacto_liege = ROOT

				narrative_event = {
					id = TOG.6004
					days = 2
				}
			}

			event_target:pulse_province = {
				province_event = {
					id = SoA.1808 # Found holy order
					years = 1
					random = 1460 # 4 years
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	build_third_temple = {
		ai_check_interval = 24

		is_high_prio = yes

		only_playable = yes

		potential = {
			religion = jewish
			has_dlc = "Sons of Abraham"

			trigger_if = {
				limit = { independent = no }
				religion = top_liege
			}

			NOR = {
				has_global_flag = third_temple_built
				has_global_flag = building_third_temple

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

		allow = {
			higher_real_tier_than = DUKE
			completely_controls = c_jerusalem
			is_heretic = no

			c_jerusalem = {
				location = {
					show_scope_change = no

					religion = ROOT
				}
			}

			wealth >= 1500
			piety >= 1500
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			character_event = { id = SoA.100 }

			c_jerusalem = {
				location = {
					show_scope_change = no

					province_event = {
						id = Plus.332
						days = 1
					}

					add_province_modifier = {
						name = third_temple_builder
						years = 4
					}
				}
			}

			wealth = -1500
		}

		ai_will_do = {
			factor = 1
		}
	}

	restore_high_priesthood = {
		is_high_prio = yes
		ai_check_interval = 24

		ai = no

		potential = {
			always = no # Disabled in CK2Plus
			has_landed_title = k_israel
			has_dlc = "Sons of Abraham"
			religion = jewish
			has_global_flag = third_temple_built

			NOR = {
				has_global_flag = jewish_priesthood_founded

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

		allow = {
			completely_controls = c_jerusalem

			c_jerusalem = {
				location = {
					show_scope_change = no

					religion = ROOT
				}
			}

			piety >= 1000
		}

		effect = {
			hidden_effect = {
				set_global_flag = jewish_priesthood_founded

				activate_title = {
					title = d_jewish
					status = yes
				}

				create_random_priest = {
					random_traits = yes
					religion = ROOT
					culture = ROOT
					dynasty = random
					female = no
					age = 41
				}

				new_character = {
					grant_title = d_jewish
					set_defacto_liege = k_israel
					narrative_event = { id = SoA.550 }
				}
			}

			event_target:pulse_province = {
				province_event = {
					id = SoA.1908 # Found holy order
					years = 1
					random = 1460 # 4 years
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	create_israel = {
		is_high_prio = yes
		ai_check_interval = 24

		only_playable = yes

		potential = {
			religion_group = jewish_group
			has_dlc = "Sons of Abraham"

			k_israel = {
				has_holder = no
			}

			NOR = {
				has_global_flag = israel_remade

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

		allow = {
			independent = yes
			completely_controls = d_jerusalem
			completely_controls = d_ascalon
			completely_controls = d_galilee
			completely_controls = d_oultrejourdain

			c_jerusalem = {
				location = {
					religion = ROOT
				}
			}

			piety >= 500
			prestige >= 500
			is_heretic = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_global_flag = israel_remade
			set_character_flag = centralize_without_penalties

			hidden_effect = {
				primary_title = {
					k_israel = {
						set_title_flag = non_manual_law_change # No prestige loss
						copy_title_laws = PREV
						clr_title_flag = non_manual_law_change # No prestige loss
					}
				}
			}

			gain_title = k_israel
			clr_character_flag = centralize_without_penalties

			hidden_effect = {
				any_character = {
					limit = { has_claim = k_jerusalem }

					if = {
						limit = { has_weak_pressed_claim = k_jerusalem }
						add_weak_pressed_claim = k_israel
						remove_claim = k_jerusalem
					}
					else_if = {
						limit = { has_weak_claim = k_jerusalem }
						add_weak_claim = k_israel
						remove_claim = k_jerusalem
					}
					else_if = {
						limit = { has_strong_pressed_claim = k_jerusalem }
						add_pressed_claim = k_israel
						remove_claim = k_jerusalem
					}
					else = {
						add_claim = k_israel
						remove_claim = k_jerusalem
					}
				}

				k_jerusalem = {
					any_direct_de_jure_vassal_title = {
						de_jure_liege = k_israel
					}

					destroy_landed_title = yes
				}
			}

			narrative_event = {
				id = SoA.102
				tooltip = EVTTOOLTIP_SoA_102
			}

			prestige = 1000

			if = {
				limit = { has_dlc = "Holy Fury" }

				if = {
					limit = { is_female = yes }

					create_bloodline = {
						type = israel_female
					}

					new_bloodline = {
						set_description = bloodline_desc_israel
					}
				}

				else = {
					create_bloodline = {
						type = israel_male
					}

					new_bloodline = {
						set_description = bloodline_desc_israel
					}
				}
			}

			hidden_effect = {
				activate_title = {
					title = d_sons_of_judea
					status = yes
				}
			}

			d_sons_of_judea = {
				show_scope_change = no

				holder_scope = {
					show_scope_change = no

					set_defacto_liege = PREVPREV
					pf_liege_change_effect = yes
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	welcome_jews = {
		ai_check_interval = 6

		only_playable = yes

		potential = {
			higher_real_tier_than = DUKE
			# has_dlc = "Sons of Abraham"
			has_character_modifier = expelled_jewish

			NOR = {
				religion_group = jewish_group
				has_character_modifier = expelled_jews_timer
				government = order_government
				has_character_flag = pogrom
			}
		}

		allow = {
			has_character_modifier = expelled_jewish
		}

		effect = {
			prestige = 50
			remove_character_modifier = expelled_jewish

			if = {
				limit = {
					NOR = {
						trait = zealous
						trait = sympathy_judaism
					}
				}

				random = {
					chance = 50
					add_trait = sympathy_judaism # TODO: Replace with scripted effect
				}
			}
			else_if = {
				limit = { trait = zealous }
				remove_trait = zealous
			}

			custom_tooltip = {
				text = jews_are_welcome

				character_event = { id = SoA.106 }
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				NOT = { trait = zealous }
			}

			mult_modifier = {
				factor = 10
				trait = sympathy_judaism
			}
		}
	}

	expel_jews = {
		ai_check_interval = 60

		only_independent = yes

		potential = {
			has_dlc = "Sons of Abraham"

			OR = {
				religion_group = christian
				religion_group = muslim
				religion_group = zoroastrian_group
				is_not_unreformed_pagan_trigger = yes
			}

			OR = {
				has_character_modifier = borrowed_from_jews

				any_courtier = {
					religion_group = jewish_group
				}

				any_realm_title = {
					real_tier = BARON
					has_building = ct_jewish_community_1

					holder_scope = {
						OR = {
							character = ROOT
							is_republic = yes
						}
					}
				}
			}

			is_nomadic = no

			NOR = {
				has_character_modifier = expelled_jewish
				secret_religion_group = jewish_group
				has_character_flag = currently_considering_expelling_jews
				government = order_government
				has_character_flag = pogrom
			}
		}

		allow = {
			independent = yes

			trigger_if = {
				limit = {
					any_realm_province = {
						religion_group = jewish_group
						held_under_PREV = yes
					}
				}

				NOT = {
					any_realm_province = {
						religion_group = jewish_group
						hidden_trigger = { held_under_PREV = yes }
					}
				}
			}

			trigger_if = {
				limit = {
					any_realm_lord = {
						religion_group = jewish_group
					}
				}

				NOT = {
					any_realm_lord = {
						religion_group = jewish_group
					}
				}
			}

			trigger_if = {
				limit = { has_character_flag = epidemic_blame_game }

				custom_tooltip = {
					text = cannot_expel_jews_during_blame_game_chain_TT
					always = no
				}
			}

			is_not_in_regency_trigger = yes

			custom_tooltip = {
				text = NOT_DESCENDANT_OF_A_JEWISH_SAINT

				NOT = {
					any_owned_bloodline = {
						has_bloodline_flag = jewish_saintly_bloodline
					}
				}
			}
		}

		effect = {
			prestige = -100

			if = {
				limit = { trait = sympathy_judaism }
				remove_trait = sympathy_judaism
			}

			if = {
				limit = { has_character_modifier = borrowed_from_jews }

				remove_character_modifier = borrowed_from_jews
				remove_character_modifier = loan_timer
				set_character_flag = offended_moneylenders

				hidden_effect = {
					set_variable = {
						which = loan_amount
						value = 0
					}
				}

				any_demesne_province = {
					show_scope_change = no

					limit = { owned_by = PREV }

					add_province_modifier = {
						name = offended_moneylenders
						years = 10
					}
				}
			}

			add_character_modifier = {
				name = expelled_jewish
				years = 60
			}

			custom_tooltip = {
				text = jews_are_expelled

				character_event = { id = SoA.105 }
				set_character_flag = pogrom

				add_character_modifier = {
					name = expelled_jews_timer
					years = 60
					hidden = yes
					inherit = yes
				}
			}

			any_friend = {
				limit = { religion_group = jewish_group }
				remove_friend = ROOT
			}
		}

		ai_will_do = {
			factor = 0.001

			trigger = {
				NOR = {
					trait = kind
					trait = charitable
					trait = sympathy_judaism

					trigger_if = {
						limit = { has_character_modifier = borrowed_from_jews }
						trait = just
					}

					any_courtier = {
						religion_group = jewish_group

						reverse_opinion = {
							who = ROOT
							value >= 20
						}
					}

					any_friend = {
						religion_group = jewish_group
					}

					any_spouse = {
						religion_group = jewish_group
					}

					any_child = {
						religion_group = jewish_group
					}
				}
			}

			mult_modifier = {
				factor = 1.2
				trait = zealous
				trait = arbitrary
			}

			mult_modifier = {
				factor = 2
				has_character_modifier = borrowed_from_jews
				trait = greedy
			}

			# CK2Plus
			mult_modifier = {
				factor = 25

				has_character_modifier = ai_austerity

				any_realm_title = {
					tier = BARON
					has_building = ct_jewish_community_1

					holder_scope = {
						OR = {
							character = ROOT
							is_republic = yes
						}
					}
				}
			}
		}
	}

	become_samrat_chakravartin = {
		ai_check_interval = 24
		is_high_prio = yes

		only_independent = yes

		potential = {
			has_dlc = "Rajas of India"
			higher_real_tier_than = DUKE
			has_dharmic_religion_trigger = yes
			is_feudal = yes

			capital_scope = {
				region = world_india
			}

			e_india = {
				has_holder = no
			}

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

		allow = {
			independent = yes
			prestige >= 1000
			wealth   >=  500
			war = no
			is_not_in_regency_trigger = yes

			# TODO: Generalize
			OR = {
				has_landed_title = e_rajastan
				e_rajastan = { has_holder = no }
			}

			OR = {
				has_landed_title = e_bengal
				e_bengal = { has_holder = no }
			}

			OR = {
				has_landed_title = e_deccan
				e_deccan = { has_holder = no }
			}

			completely_controls_region = world_india
		}

		effect = {
			wealth  = -500
			prestige = 500

			hidden_effect = {
				activate_title = {
					title = e_india
					status = yes
				}

				any_title = {
					limit = {
						tier = KING
						region = world_india
					}
					de_jure_liege = e_india
				}
			}

			primary_title = {
				show_scope_change = no

				e_india = {
					show_scope_change = no

					set_title_flag = non_manual_law_change # No prestige loss
					grant_title = ROOT
					copy_title_laws = PREV
					copy_title_history = PREV
					clr_title_flag = non_manual_law_change # No prestige loss
				}
			}

			hidden_effect = {
				e_india = {
					make_primary_title = yes
				}
			}

			if = {
				limit = { has_landed_title = e_rajastan }
				destroy_landed_title = e_rajastan
			}

			if = {
				limit = { has_landed_title = e_bengal }
				destroy_landed_title = e_bengal
			}

			if = {
				limit = { has_landed_title = e_deccan }
				destroy_landed_title = e_deccan
			}

			hidden_effect = {
				activate_title = {
					title = e_rajastan
					status = no
				}

				activate_title = {
					title = e_bengal
					status = no
				}

				activate_title = {
					title = e_deccan
					status = no
				}
			}

			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_magnificent
			}

			if = {
				limit = {
					ai = no
					is_ironman = yes
				}

				set_character_flag = achievement_my_very_own_subcontinent
			}

			if = {
				limit = { has_dlc = "Holy Fury" }

				if = {
					limit = { is_female = yes }

					create_bloodline = {
						type = samrat_chakravartin_female
					}

					new_bloodline = {
						set_description = bloodline_desc_samrat_chakravartin
					}
				}
				else = {
					create_bloodline = {
						type = samrat_chakravartin_male
					}

					new_bloodline = {
						set_description = bloodline_desc_samrat_chakravartin
					}
				}
			}

			chronicle = {
				entry = CHRONICLE_UNITED_INDIA
				picture = GFX_evt_throne_room
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	form_the_hre = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			higher_real_tier_than = DUKE

			OR = {
				religion = catholic
				religion = fraticelli

				trigger_if = {
					limit = { religion = chalcedonian }

					rightful_religious_head_scope = {
						trait = latin_rites
					}
				}
			}

			has_global_flag = hre_formed
			is_alternate_start = no
			NOT = { has_global_flag = hre_removed }
			e_hre = { has_holder = no }
			e_francia = { has_holder = no }
		}

		allow = {
			has_landed_title = k_germany

			trigger_if = {
				limit = { start_date >= 936.8.7 }
				has_landed_title = k_italy
			}
			trigger_else = {
				custom_tooltip = {
					text = CONTROLS_6_GERMAN_DUCHIES

					any_realm_title = {
						count = 6

						tier = DUKE
						region = custom_germany

						OR = {
							holder_scope = { same_realm = ROOT }

							NOT = {
								any_de_jure_vassal_title = {
									holder_scope = {
										NOT = {	same_realm = ROOT }
									}
								}
							}
						}
					}
				}
			}

			prestige >= 1000
			realm_size >= 150
			scaled_wealth = 1
			is_not_in_regency_trigger = yes
		}

		effect = {
			scaled_wealth = -1
			give_nickname = nick_the_great

			if = {
				limit = { has_character_flag = is_otto }

				narrative_event = { id = LT.40015 }

				any_player = {
					limit = {
						is_within_diplo_range = ROOT
						NOT = { character = ROOT }
					}

					narrative_event = { id = LT.40015 }
				}

				create_bloodline = {
					type = otto
				}
			}

			add_artifact = crown_hre

			primary_title = {
				show_scope_change = no

				e_hre = {
					show_scope_change = no

					set_title_flag = non_manual_law_change # No prestige loss
					grant_title = ROOT
					copy_title_laws = PREV
					add_law = succ_hre_elective
					clr_title_flag = non_manual_law_change # No prestige loss
				}
			}

			# We don't need to use dynamic elector titles because in Plus the HRE can only be formed from Germany.
			# hidden_effect = {
			#	princely_elective_german_elector_setup_effect = yes
			# }

			any_demesne_title = {
				show_scope_change = no

				limit = {
					real_tier = KING
					NOT = { title = k_germany }
				}

				destroy_landed_title = THIS
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	# Available if the Carolingian Empire was never formed
	# Also see 'first_coronation', CK2Plus_specific_realm_decisions
	form_the_hre_early = {
		ai_check_interval = 12

		is_high_prio = yes

		only_independent = yes

		potential = {
			higher_real_tier_than = DUKE

			trigger_if = {
				limit = { religion = chalcedonian }
				trait = latin_rites
			}
			trigger_else = {
				religion = catholic
			}

			is_heretic = no

			rightful_religious_head_scope = {
				has_landed_title = k_papacy

				NOR = {
					character = ROOT
					has_character_flag = approached_for_coronation
				}
			}

			NOR = {
				dynasty = 25061 # form_carolingian_empire instead
				is_title_active = e_karling
				has_global_flag = carolingian_empire_formed
				has_global_flag = hre_formed
				has_global_flag = rome_restored
			}
		}

		allow = {
			independent = yes
			realm_size >= 150
			prestige >= 1000
			piety >= 100

			custom_tooltip = {
				text = POPE_WOULD_HEAR_REQUEST

				NOT = { trait = excommunicated }

				rightful_religious_head_scope = {
					NOT = {
						has_opinion_modifier = {
							name = coronation_rejected
							who = ROOT
						}
					}
				}
			}

			custom_tooltip = {
				text = CONTROL_ANY_THREE_KINGDOMS

				# TODO: Generalize
				calc_true_if = {
					amount >= 3

					k_france = {
						is_titular = no

						holder_scope = {
							OR = {
								character = ROOT
								vassal_of = ROOT
							}
						}
					}

					k_lotharingia = {
						is_titular = no

						holder_scope = {
							OR = {
								character = ROOT
								vassal_of = ROOT
							}
						}
					}

					k_saxony = {
						is_titular = no

						holder_scope = {
							OR = {
								character = ROOT
								vassal_of = ROOT
							}
						}
					}

					k_bavaria = {
						is_titular = no

						holder_scope = {
							OR = {
								character = ROOT
								vassal_of = ROOT
							}
						}
					}

					k_italy = {
						is_titular = no

						holder_scope = {
							OR = {
								character = ROOT
								vassal_of = ROOT
							}
						}
					}

					k_germany = {
						is_titular = no

						holder_scope = {
							OR = {
								character = ROOT
								vassal_of = ROOT
							}
						}
					}
				}
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			rightful_religious_head_scope = {
				show_scope_change = no

				set_character_flag = approached_for_coronation

				letter_event = {
					id = Plus.1155
					tooltip = ask_for_coronation
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	form_new_kingdom = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			real_tier = DUKE

			trigger_if = {
				limit = { is_alternate_start = yes }

				has_alternate_start_parameter = {
					key = dejure_kingdoms
					value = none
				}
			}
			trigger_else = {
				has_dlc = "Charlemagne"
			}

			holy_order = no

			primary_title = {
				is_primary_type_title = no
			}

			NOT = {
				has_game_rule = {
					name = dynamic_realms
					value = off
				}
			}
		}

		allow = {
			independent = yes

			trigger_if = {
				limit = {
					is_alternate_start = yes

					has_alternate_start_parameter = {
						key = dejure_kingdoms
						value = none
					}
				}

				prestige >= 250
				wealth   >= 125
			}
			trigger_else = {
				prestige >= 1000
				wealth   >=  500
			}

			custom_tooltip = {
				text = HAS_TWO_DUKE_TITLES

				any_demesne_title = {
					count = 2

					real_tier = DUKE
					is_titular = no
				}
			}

			capital_scope = {
				TECH_MAJESTY >= 1
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			if = {
				limit = {
					is_alternate_start = yes

					has_alternate_start_parameter = {
						key = dejure_kingdoms
						value = none
					}
				}

				prestige = -250
				wealth   = -125
			}
			else = {
				prestige = -1000
				wealth   =  -500
			}

			hidden_effect = {
				primary_title = {
					save_event_target_as = current_duchy

					# Special Title Selection
					trigger_switch = {
						on_trigger = title

						d_algarve = { k_algarve = { save_event_target_as = target_title } }
						d_antioch = { k_antioch = { save_event_target_as = target_title } }
						d_armenia = { k_armenia = { save_event_target_as = target_title } }
						d_bavaria = { k_bavaria = { save_event_target_as = target_title } }
						d_bosnia = { k_bosnia = { save_event_target_as = target_title } }
						d_cornwall = { k_cornwall = { save_event_target_as = target_title } }
						d_croatia = { k_croatia = { save_event_target_as = target_title } }
						d_cyprus = { k_cyprus = { save_event_target_as = target_title } }
						d_edessa = { k_edessa = { save_event_target_as = target_title } }
						d_epirus = { k_epirus = { save_event_target_as = target_title } }
						d_finland = { k_finland = { save_event_target_as = target_title } }
						d_franconia = { k_franconia = { save_event_target_as = target_title } }
						d_gascogne = { k_gascony = { save_event_target_as = target_title } }
						d_gondar = { k_gondar = { save_event_target_as = target_title } }
						d_iceland = { k_iceland = { save_event_target_as = target_title } }
						d_krete = { k_krete = { save_event_target_as = target_title } }
						d_luxembourg = { k_luxembourg = { save_event_target_as = target_title } }
						d_mallorca = { k_mallorca = { save_event_target_as = target_title } }
						d_milano = { k_lombardy = { save_event_target_as = target_title } }
						d_moravia = { k_moravia = { save_event_target_as = target_title } }
						d_normandy = { k_normandy = { save_event_target_as = target_title } }
						d_oman = { k_oman = { save_event_target_as = target_title } }
						d_osterreich = { k_austria = { save_event_target_as = target_title } }
						d_provence = { k_provence = { save_event_target_as = target_title } }
						d_prussia = { k_prussia = { save_event_target_as = target_title } }
						d_slavonia = { k_slavonia = { save_event_target_as = target_title } }
						d_saxony = { k_saxony = { save_event_target_as = target_title } }
						d_skane = { k_scania = { save_event_target_as = target_title } }
						d_swabia = { k_swabia = { save_event_target_as = target_title } }
						d_swiss = { k_swiss = { save_event_target_as = target_title } }
						d_thessalonika = { k_thessalonika = { save_event_target_as = target_title } }
						d_valencia = { k_valencia = { save_event_target_as = target_title } }
					}
				}

				# If the special title doesn't have a holder, give it to ROOT
				if = {
					limit = { event_target:target_title = { has_holder = no } }

					# Activate the title if necessary
					if = {
						limit = { NOT = { is_title_active = event_target:target_title } }

						activate_title = {
							title = event_target:target_title
							status = yes
						}
					}

					event_target:target_title = {
						show_scope_change = no

						set_title_flag = non_manual_law_change
						grant_title = ROOT
						copy_title_laws = event_target:current_duchy
						clr_title_flag = non_manual_law_change
					}
				}
				# If the special title has a holder, create a new title using the capital province to avoid name conflicts and give it to ROOT
				else_if = {
					limit = { event_target:target_title = { has_holder = yes } }

					create_title = {
						tier = KING
						landless = no
						temporary = no
						custom_created = yes
						culture = ROOT
						holder = ROOT
						base_title = capital_scope
					}

					new_title = {
						set_title_flag = custom_created_title
						set_title_flag = non_manual_law_change
						copy_title_laws = event_target:current_duchy
						copy_title_history = event_target:current_duchy
						clr_title_flag = non_manual_law_change
					}
				}
				# Fallback if the title isn't special just use it as the base for a new title
				else = {
					create_title = {
						tier = KING
						landless = no
						temporary = no
						custom_created = yes
						culture = ROOT
						holder = ROOT
						base_title = primary_title
					}

					new_title = {
						set_title_flag = custom_created_title
						set_title_flag = non_manual_law_change
						copy_title_laws = event_target:current_duchy
						copy_title_history = event_target:current_duchy
						clr_title_flag = non_manual_law_change
					}
				}
			}

			custom_tooltip = {
				text = CREATE_NEW_KINGDOM
			}

			chronicle = {
				entry = CHRONICLE_FOUNDED_NEW_KINGDOM_OR_EMPIRE
				portrait = [Root.GetID]
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				OR = {
					trigger_if = {
						limit = {
							OR = {
								trait = proud
								trait = ambitious
							}
						}

						prestige >= 2500
					}
					trigger_else = {
						prestige >= 5000
					}

					AND = {
						realm_size >= 70
						num_of_duke_titles >= 4
					}
				}
			}
		}
	}

	form_new_empire = {
		ai_check_interval = 24

		is_high_prio = yes

		only_independent = yes

		potential = {
			real_tier = KING

			trigger_if = {
				limit = { is_alternate_start = yes }

				OR = {
					has_alternate_start_parameter = {
						key = dejure_kingdoms
						value = none
					}

					has_alternate_start_parameter = {
						key = dejure_empires
						value = none
					}
				}
			}
			trigger_else = {
				has_dlc = "Charlemagne"
			}

			primary_title = {
				is_primary_type_title = no
			}

			holy_order = no

			NOT = {
				has_game_rule = {
					name = dynamic_realms
					value = off
				}
			}
		}

		allow = {
			independent = yes

			# Not using the new conditional from the vanilla version of this decision because Plus always has few to no de jure empires
			prestige >= 2500
			wealth   >= 1000

			capital_scope = {
				TECH_MAJESTY >= 2
			}

			has_empire_size_trigger = yes
			empire_creation_kingdom_rules_trigger = yes

			piety >= 0
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			prestige = -2500
			wealth = -1000

			hidden_effect = {
				primary_title = {
					save_event_target_as = current_kingdom

					# Special Title Selection
					trigger_switch = {
						on_trigger = title

						k_armenia = { e_armenia = { save_event_target_as = target_title } }
						k_abyssinia = { e_abyssinia = { save_event_target_as = target_title } }
						k_andalusia = { e_andalusia = { save_event_target_as = target_title } }
						k_arabia = { e_arabia = { save_event_target_as = target_title } }
						k_austria = { e_austria = { save_event_target_as = target_title } }
						k_bengal = { e_bengal = { save_event_target_as = target_title } }
						k_bohemia = { e_bohemia = { save_event_target_as = target_title } }
						k_bulgaria = { e_bulgaria = { save_event_target_as = target_title } }
						k_carpathia = { e_carpathia = { save_event_target_as = target_title } }
						k_croatia = { e_croatia = { save_event_target_as = target_title } }
						k_cumania = { e_cuman = { save_event_target_as = target_title } }
						k_cornwall = { e_cornwall = { save_event_target_as = target_title } }
						k_denmark = { e_denmark = { save_event_target_as = target_title } }
						k_egypt = { e_egypt = { save_event_target_as = target_title } }
						k_france = { e_francia = { save_event_target_as = target_title } }
						k_england = { e_england = { save_event_target_as = target_title } }
						k_finland = { e_finland = { save_event_target_as = target_title } }
						k_georgia = { e_georgia = { save_event_target_as = target_title } }
						k_germany = { e_germania = { save_event_target_as = target_title } }
						k_ghana = { e_ghana = { save_event_target_as = target_title } }
						k_greece = { e_byzantium = { save_event_target_as = target_title } }
						k_hungary = { e_hungary = { save_event_target_as = target_title } }
						k_iceland = { e_iceland = { save_event_target_as = target_title } }
						k_ireland = { e_ireland = { save_event_target_as = target_title } }
						k_israel = { e_greater_israel = { save_event_target_as = target_title } }
						k_italy = { e_italia = { save_event_target_as = target_title } }
						k_lithuania = { e_lithuania = { save_event_target_as = target_title } }
						k_mali = { e_mali = { save_event_target_as = target_title } }
						k_norway = { e_norway = { save_event_target_as = target_title } }
						k_north_sea = { e_north_sea = { save_event_target_as = target_title } }
						k_nubia = { e_nubia = { save_event_target_as = target_title } }
						k_ottoman = { e_ottoman = { save_event_target_as = target_title } }
						k_poland = { e_poland = { save_event_target_as = target_title } }
						k_prussia = { e_prussia = { save_event_target_as = target_title } }
						k_scotland = { e_scotland = { save_event_target_as = target_title } }
						k_sicily = { e_sicily = { save_event_target_as = target_title } }
						k_serbia = { e_serbia = { save_event_target_as = target_title } }
						k_syria = { e_syria = { save_event_target_as = target_title } }
						k_sweden = { e_sweden = { save_event_target_as = target_title } }
						k_takrur = { e_jolof = { save_event_target_as = target_title } }
						k_wales = { e_wales = { save_event_target_as = target_title } }
						k_wend = { e_wend = { save_event_target_as = target_title } }
					}
				}

				# If the special title doesn't have a holder, give it to ROOT
				if = {
					limit = { event_target:target_title = { has_holder = no } }

					# Activate the title if necessary
					if = {
						limit = { NOT = { is_title_active = event_target:target_title } }

						activate_title = {
							title = event_target:target_title
							status = yes
						}
					}

					event_target:target_title = {
						set_title_flag = non_manual_law_change
						grant_title = ROOT
						copy_title_laws = event_target:current_kingdom
						copy_title_history = event_target:current_kingdom
						clr_title_flag = non_manual_law_change
					}
				}
				# If the special title has a holder, create a new title using the capital province to avoid name conflicts and give it to ROOT
				else_if = {
					limit = { event_target:target_title = { has_holder = yes } }

					create_title = {
						tier = EMPEROR
						landless = no
						temporary = no
						custom_created = yes
						culture = ROOT
						holder = ROOT
						base_title = capital_scope
					}

					new_title = {
						set_title_flag = custom_created_title
						set_title_flag = non_manual_law_change
						copy_title_laws = event_target:current_kingdom
						copy_title_history = event_target:current_kingdom
						clr_title_flag = non_manual_law_change
					}
				}
				# Fallback if the title isn't special just use it as the base for a new title
				else = {
					create_title = {
						tier = EMPEROR
						landless = no
						temporary = no
						custom_created = yes
						culture = ROOT
						holder = ROOT
						base_title = primary_title
					}

					new_title = {
						set_title_flag = custom_created_title
						set_title_flag = non_manual_law_change
						copy_title_laws = event_target:current_kingdom
						copy_title_history = event_target:current_kingdom
						clr_title_flag = non_manual_law_change
					}
				}
			}

			custom_tooltip = {
				text = CREATE_NEW_KINGDOM
			}

			chronicle = {
				entry = CHRONICLE_FOUNDED_NEW_KINGDOM_OR_EMPIRE
				portrait = [Root.GetID]
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				OR = {
					trigger_if = {
						limit = {
							OR = {
								trait = proud
								trait = ambitious
							}
						}
						prestige >= 5000
					}
					trigger_else = {
						prestige >= 10000
					}

					num_of_king_titles >= 4
				}

				NOT = { has_character_flag = is_charlemagne }
			}
		}
	}

	# Hold Wargames - for nomads
	hold_nomad_wargames = {
		ai_check_interval = 4

		only_playable = yes

		potential = {
			is_nomadic = yes

			NOR = {
				has_character_modifier = holding_nomad_wargames
				has_character_modifier = held_nomad_wargames
			}
		}

		allow = {
			wealth >= 20
			prestige >= 100
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no

			trigger_if = {
				limit = {
					NOT = {
						any_demesne_title = {
							lower_real_tier_than = DUKE
							is_occupied = no
						}
					}
				}

				custom_tooltip = {
					text = UNOCCUPIED_DEMESNE_TITLE
					always = no
				}
			}
		}

		effect = {
			wealth = -30

			add_character_modifier = {
				name = holding_nomad_wargames
				duration = -1
			}

			set_character_flag = recent_minor_decision
			set_character_flag = do_not_disturb
			character_event = { id = HL.7000 }
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				wealth >= 200

				trigger_if = {
					limit = { has_character_flag = recent_minor_decision }

					had_character_flag = {
						flag = recent_minor_decision
						years >= 2
					}
				}
			}

			mult_modifier = {
				factor = 2

				current_heir = {
					dynasty = ROOT
					combat_rating >= 80
				}
			}

			mult_modifier = {
				factor = 0.5
				martial < 10
			}
		}
	}

	convert_to_feudalism_indep = {
		ai_check_interval = 12

		is_high_prio = yes

		only_independent = yes

		potential = {
			independent = yes
			is_tribal = yes
		}

		allow = {
			OR = {
				has_law = tribal_organization_4

				AND = {
					has_law = tribal_organization_2
					TECH_LEGALISM >= 3
					TECH_NOBLE_CUSTOMS >= 3
					TECH_MAJESTY >= 3
				}
			}

			capital_scope = {
				show_scope_change = no

				TECH_NOBLE_CUSTOMS >= 1

				OR = {
					ROOT = {
						show_scope_change = no

						is_not_unreformed_pagan_trigger = yes
					}

					TECH_NOBLE_CUSTOMS >= 3
				}
			}

			capital_holding = {
				OR = {
					holding_type = castle

					AND = {
						holding_type = tribal
						has_building = tb_basic_keep_5
					}
				}
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			# Notify of holdings that will not upgrade or cause issues
			any_demesne_province = {
				show_scope_change = no

				limit = {
					owned_by = PREV

					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_basic_keep_5 }
					}
				}

				if = {
					limit = { culture = ROOT }

					random_province_holding = {
						show_scope_change = no
						limit = { holding_type = tribal }
						custom_tooltip = { text = TRIBAL_NOT_UPGRADE }
					}
				}
				else = {
					random_province_holding = {
						show_scope_change = no
						limit = { holding_type = tribal }
						custom_tooltip = { text = TRIBAL_PROVINCE_NOT_CULTURE_WARNING }
					}
				}
			}

			# Upgrade tribal holdings which have full hillforts
			any_demesne_province = {
				show_scope_change = no

				limit = {
					owned_by = PREV

					any_province_holding = {
						holding_type = tribal
						has_building = tb_basic_keep_5
					}
				}

				# Clear Tribal Great Pillars
				clr_province_flag = flag_great_pillar_norse
				clr_province_flag = flag_great_pillar_tengri
				clr_province_flag = flag_great_pillar_slavic
				clr_province_flag = flag_great_pillar_baltic
				clr_province_flag = flag_great_pillar_finnish
				clr_province_flag = flag_great_pillar_west_african
				clr_province_flag = flag_great_pillar_aztec
				clr_province_flag = flag_great_pillar_bon
				clr_province_flag = flag_great_pillar_zun
				clr_province_flag = flag_great_pillar_hellenic

				any_province_holding = {
					show_scope_change = no

					limit = {
						holding_type = tribal
						has_building = tb_basic_keep_5
					}

					set_title_flag = refill_levy
					convert_to = castle
					refill_holding_levy = yes
				}

				add_province_modifier = {
					name = new_buildings_timer
					years = 100
					hidden = yes
				}
			}

			if = {
				limit = {
					ai = no
					is_ironman = yes
				}

				set_character_flag = achievement_not_a_tribe
			}

			if = {
				limit = { is_government_potential = muslim_government}

				set_government_type = muslim_government

				if = {
					limit = { ai = no }

					chronicle = {
						entry = CHRONICLE_ADOPTED_IQTA
						picture = GFX_evt_castle_construction
					}
				}
			}
			else_if = {
				limit = { is_government_potential = theocratic_feudal_government }

				set_government_type = theocratic_feudal_government


				if = {
					limit = { ai = no }

					chronicle = {
						entry = CHRONICLE_ADOPTED_FEUDALISM
						picture = GFX_evt_castle_construction
					}
				}
			}
			else = {
				set_government_type = feudal_government

				if = {
					limit = { ai = no }

					chronicle = {
						entry = CHRONICLE_ADOPTED_FEUDALISM
						picture = GFX_evt_castle_construction
					}
				}
			}

			if = {
				limit = {
					higher_real_tier_than = KING
					NOT = { has_dlc = "Conclave" }
				}

				primary_title = {
					set_title_flag = non_manual_law_change # No prestige loss
					add_law = feudal_administration
					clr_title_flag = non_manual_law_change # No prestige loss
				}
			}

			hidden_effect = {
				primary_title = {
					revoke_law = tribal_organization_4
				}

				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				NOR = {
					any_demesne_title = {
						real_tier = BARON
						holding_type = tribal
						NOT = { has_building = tb_basic_keep_5 }

						location = {
							NOT = { culture = ROOT }
						}
					}

					any_vassal = {
						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.75
						}
					}

					any_vassal = {
						count = 2

						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.5
						}
					}

					any_vassal = {
						count = 3

						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.25
						}
					}
				}
			}
		}
	}

	convert_to_feudalism_vassal = {
		ai_check_interval = 12

		is_high_prio = yes

		only_playable = yes

		potential = {
			is_tribal = yes
			independent = no
		}

		allow = {
			war = no

			liege = {
				is_tribal = no
				is_nomadic = no
			}

			OR = {
				capital_holding = {
					OR = {
						holding_type = castle

						AND = {
							holding_type = tribal
							has_building = tb_basic_keep_5
						}
					}
				}

				AND = {
					NOT = { tier = BARON }

					capital_scope = {
						any_province_holding = {
							holding_type = castle

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

		effect = {
			# Notify of holdings that will not upgrade or cause issues
			any_demesne_province = {
				show_scope_change = no

				limit = {
					owned_by = PREV

					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_basic_keep_5 }
					}
				}

				if = {
					limit = { culture = ROOT }

					random_province_holding = {
						show_scope_change = no
						limit = { holding_type = tribal }
						custom_tooltip = { text = TRIBAL_NOT_UPGRADE }
					}
				}
				else = {
					random_province_holding = {
						show_scope_change = no
						limit = { holding_type = tribal }
						custom_tooltip = { text = TRIBAL_PROVINCE_NOT_CULTURE_WARNING }
					}
				}
			}

			# Upgrade tribal holdings which have full hillforts
			any_demesne_province = {
				show_scope_change = no

				limit = {
					owned_by = PREV

					any_province_holding = {
						holding_type = tribal
						has_building = tb_basic_keep_5
					}
				}

				# Clear Tribal Great Pillars
				clr_province_flag = flag_great_pillar_norse
				clr_province_flag = flag_great_pillar_tengri
				clr_province_flag = flag_great_pillar_slavic
				clr_province_flag = flag_great_pillar_baltic
				clr_province_flag = flag_great_pillar_finnish
				clr_province_flag = flag_great_pillar_west_african
				clr_province_flag = flag_great_pillar_aztec
				clr_province_flag = flag_great_pillar_bon
				clr_province_flag = flag_great_pillar_zun
				clr_province_flag = flag_great_pillar_hellenic

				any_province_holding = {
					show_scope_change = no

					limit = {
						holding_type = tribal
						has_building = tb_basic_keep_5
					}

					set_title_flag = refill_levy
					convert_to = castle
					refill_holding_levy = yes
				}

				add_province_modifier = {
					name = new_buildings_timer
					years = 100
					hidden = yes
				}
			}

			hidden_effect = {
				# If upgrading from non-capital castle
				if = {
					limit = {
						higher_real_tier_than = BARON

						capital_holding = {
							holding_type = tribal
						}

						capital_scope = {
							any_province_holding = {
								holding_type = castle

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

					capital_scope = {
						random_province_holding = {
							limit = {
								holding_type = castle

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

							if = {
								limit = {
									NOT = { holder = ROOT }
								}
								usurp_title = ROOT
							}

							make_capital_holding = yes
						}
					}

					set_character_flag = feudalize_handled_capital
				}

				primary_title = {
					revoke_law = tribal_organization_0
					revoke_law = tribal_organization_1
					revoke_law = tribal_organization_2
					revoke_law = tribal_organization_3
					revoke_law = tribal_organization_4
				}

				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}

				liege = {
					character_event = { id = 55100 } # Notify Liege
				}
			}

			if = {
				limit = {
					ai = no
					is_ironman = yes
				}

				set_character_flag = achievement_not_a_tribe
			}

			if = {
				limit = { is_government_potential = muslim_government}

				set_government_type = muslim_government

				if = {
					limit = { ai = no }

					chronicle = {
						entry = CHRONICLE_ADOPTED_IQTA
						picture = GFX_evt_castle_construction
					}
				}
			}
			else_if = {
				limit = { is_government_potential = theocratic_feudal_government }

				set_government_type = theocratic_feudal_government

				if = {
					limit = { ai = no }

					chronicle = {
						entry = CHRONICLE_ADOPTED_FEUDALISM
						picture = GFX_evt_castle_construction
					}
				}
			}
			else = {
				set_government_type = feudal_government

				if = {
					limit = { ai = no }

					chronicle = {
						entry = CHRONICLE_ADOPTED_FEUDALISM
						picture = GFX_evt_castle_construction
					}
				}
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				NOR = {
					any_demesne_title = {
						real_tier = BARON
						holding_type = tribal
						NOT = { has_building = tb_basic_keep_5 }

						location = {
							NOT = { culture = ROOT }
						}
					}

					any_vassal = {
						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.75
						}
					}

					any_vassal = {
						count = 2

						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.5
						}
					}

					any_vassal = {
						count = 3

						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.25
						}
					}
				}
			}

			mult_modifier = {
				factor = 0.001

				liege = {
					trigger_if = {
						limit = { religion_group = ROOT }

						religion_group = pagan_group
						NOT = { religion = ROOT }
					}
				}
			}

			mult_modifier = {
				factor = 0.01
				NOT = { culture_group = liege }
			}

			mult_modifier = {
				factor = 0.01

				liege = {
					religion_group = ROOT

					NOR = {
						religion_group = pagan_group
						religion = ROOT
					}
				}
			}

			# CK2Plus
			mult_modifier = {
				factor = 0.1

				culture_group = liege
				NOT = { culture = liege }
			}

			mult_modifier = {
				factor = 0.5
				liege = { is_republic = yes }
			}

			mult_modifier = {
				factor = 1.5
				liege = { is_feudal = yes }
			}

			mult_modifier = {
				factor = 5
				has_education_martial_trigger = yes
			}

			mult_modifier = {
				factor = 0.5

				liege = {
					is_republic = yes
				}

				OR = {
					is_patrician = yes
					leads_faction = faction_prosperity
					in_faction = faction_prosperity
				}
			}
		}
	}

	convert_to_republic_indep = {
		is_high_prio = yes

		only_independent = yes

		potential = {
			independent = yes
			is_tribal = yes

			trigger_if = {
				limit = { ai = no }
				has_dlc = "The Republic"
			}
		}

		allow = {
			higher_real_tier_than = COUNT
			has_law = tribal_organization_4
			is_female = no
			war = no

			capital_scope = {
				is_coastal = yes
				TECH_TRADE_PRACTICES >= 1

				OR = {
					ROOT = {
						show_scope_change = no
						is_not_unreformed_pagan_trigger = yes
					}

					TECH_TRADE_PRACTICES >= 3
				}
			}

			capital_holding = {
				OR = {
					holding_type = city

					AND = {
						holding_type = tribal
						has_building = tb_town_hall_5
					}
				}
			}
		}

		effect = {
			# Notify of holdings that will not upgrade or cause issues
			any_demesne_province = {
				show_scope_change = no

				limit = {
					owned_by = PREV

					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_town_hall_5 }
					}
				}

				if = {
					limit = { culture = ROOT }

					random_province_holding = {
						show_scope_change = no
						limit = { holding_type = tribal }
						custom_tooltip = { text = TRIBAL_NOT_UPGRADE }
					}
				}
				else = {
					random_province_holding = {
						show_scope_change = no
						limit = { holding_type = tribal }
						custom_tooltip = { text = TRIBAL_PROVINCE_NOT_CULTURE_WARNING }
					}
				}
			}

			# Notify of vassals that will cause an issue
			any_vassal = {
				show_scope_change = no

				limit = {
					higher_real_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
				}

				custom_tooltip = { text = TRIBAL_VASSAL_NOT_CULTURE_WARNING }
			}

			create_family_palace = yes
			set_government_type = merchant_republic_government

			hidden_effect = {
				if = {
					limit = {
						num_of_government_vassals = {
							government = merchant_republic_government
							value < 1
						}
					}

					# Gives random vassals a family palace
					any_unique_dynasty_vassal = {
						count = 4

						limit = {
							OR = {
								is_republic = yes
								is_tribal = yes
							}

							is_female = no
							is_patrician = no
							prisoner = no
							is_incapable = no
							is_adult = yes
							is_lowborn = no
						}

						if = {
							limit = { is_tribal = yes }

							capital_holding = {
								if = {
									limit = { holding_type = tribal }

									set_title_flag = refill_levy
									convert_to = city
									refill_holding_levy = yes

									location = {
										add_province_modifier = {
											name = new_buildings_timer
											years = 100
											hidden = yes
										}
									}
								}
							}

							any_demesne_title = {
								limit = {
									tier = BARON
									holding_type = tribal
									has_building = tb_town_hall_5
								}

								convert_to = city
								refill_holding_levy = yes
								set_title_flag = refill_levy

								location = { # Clear Tribal Great Pillars
									clr_province_flag = flag_great_pillar_norse
									clr_province_flag = flag_great_pillar_tengri
									clr_province_flag = flag_great_pillar_slavic
									clr_province_flag = flag_great_pillar_baltic
									clr_province_flag = flag_great_pillar_finnish
									clr_province_flag = flag_great_pillar_west_african
									clr_province_flag = flag_great_pillar_aztec
									clr_province_flag = flag_great_pillar_bon
									clr_province_flag = flag_great_pillar_zun
									clr_province_flag = flag_great_pillar_hellenic

									add_province_modifier = {
										name = new_buildings_timer
										years = 100
										hidden = yes
									}
								}
							}
						}

						create_family_palace = yes
						set_government_type = merchant_republic_government
					}
				}

				# Upgrade tribal holdings which have full market towns
				any_demesne_title = {
					limit = {
						tier = BARON
						holding_type = tribal
						has_building = tb_town_hall_5
					}

					convert_to = city
					refill_holding_levy = yes
					set_title_flag = refill_levy

					location = { #Clear Tribal Great Pillars
						clr_province_flag = flag_great_pillar_norse
						clr_province_flag = flag_great_pillar_tengri
						clr_province_flag = flag_great_pillar_slavic
						clr_province_flag = flag_great_pillar_baltic
						clr_province_flag = flag_great_pillar_finnish
						clr_province_flag = flag_great_pillar_west_african
						clr_province_flag = flag_great_pillar_aztec
						clr_province_flag = flag_great_pillar_bon
						clr_province_flag = flag_great_pillar_zun
						clr_province_flag = flag_great_pillar_hellenic

						add_province_modifier = {
							name = new_buildings_timer
							years = 100
							hidden = yes
						}
					}
				}

				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
			}

			if = {
				limit = {
					ai = no
					is_ironman = yes
				}

				set_character_flag = achievement_res_publica
			}

			if = {
				limit = { ai = no }

				chronicle = {
					entry = CHRONICLE_FOUNDED_MERCHANT_REPUBLIC
					picture = GFX_evt_busy_trading_dock_republic
				}
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				NOR = {
					any_demesne_title = {
						real_tier = BARON
						holding_type = tribal
						NOT = { has_building = tb_town_hall_5 }

						location = {
							NOT = { culture = ROOT }
						}
					}

					any_vassal = {
						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.75
						}
					}

					any_vassal = {
						count = 2

						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.5
						}
					}

					any_vassal = {
						count = 3

						higher_real_tier_than = BARON
						is_tribal = yes
						NOT = { culture = ROOT }

						relative_power = {
							who = ROOT
							power >= 0.25
						}
					}
				}
			}

			mult_modifier = {
				factor = 2.0
				stewardship >= 10
			}

			mult_modifier = {
				factor = 4.0
				capital_holding = { holding_type = city }
			}
		}
	}

}

targeted_decisions = {
	embrace_mutazila = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 60

		only_playable = yes

		potential = {
			has_dlc = "Sons of Abraham"
			is_sunni_branch = yes

			NOR = {
				trait = mutazilite
				trait = ashari
			}

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

		allow = {
			piety >= 50
			learning >= 8
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			add_trait = mutazilite

			character_event = {
				id = SoA.600
				tooltip = EVTTOOLTIP_SoA_600
			}
		}

		ai_will_do = {
			factor = 0.005

			trigger = {
				NOT = { trait = zealous }
			}

			mult_modifier = {
				factor = 1.2
				trait = cynical
			}

			mult_modifier = {
				factor = 1.2
				liege = { trait = mutazilite }
			}
		}
	}

	embrace_ashari = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 60

		only_playable = yes

		potential = {
			has_dlc = "Sons of Abraham"
			is_sunni_branch = yes

			NOR = {
				trait = mutazilite
				trait = ashari
			}

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

		allow = {
			piety >= 50
			learning >= 8
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			add_trait = ashari

			character_event = {
				id = SoA.601
				tooltip = EVTTOOLTIP_SoA_601
			}
		}

		ai_will_do = {
			factor = 0.005

			trigger = {
				NOT = { trait = cynical }
			}

			mult_modifier = {
				factor = 1.2
				trait = zealous
			}

			mult_modifier = {
				factor = 1.2
				liege = { trait = ashari }
			}
		}
	}

}