# Settlement decisions are possible vs _all_ settlements and are shown in the Settlement Diplomacy View, not the Intrigue View. The taker is in the FROM scope.
#
# filter = [capital/owned/vassal_owned/sub_realm_owned/realm_owned/dynasty_owned/all]
# ai_target_filter = [capital/owned/vassal_owned/sub_realm_owned/realm_owned/dynasty_owned/all] (which settlements for which the AI evaluates the decision.)
#	owned: all settlements owned by the AI
#	vassal_owned: all settlements owned by direct vassal rulers of the AI's employer
#	sub_realm_owned: all settlements below the AI's employer
#	realm_owned: all settlements in the same top realm as the AI
#	dynasty_owned: all settlements owned by members of the same dynasty
#	all: all settlements (Avoid if possible. VERY CPU-HEAVY!)
#

settlement_decisions = {

	convert_tribal_to_castle = {
		# filter = owned # CleanSlate: Nonfunctional
		ai_target_filter = owned
		ai_check_interval = 12

		is_high_prio = yes

		only_playable = yes

		from_potential = {
			is_feudal = yes
			higher_real_tier_than = BARON
		}

		potential = {
			# Filter doesn't seem to be enough
			holder = FROM
			holding_type = tribal
		}

		allow = {
			has_building = tb_basic_keep_5

			location = {
				show_scope_change = no

				religion = FROM
			}
		}

		effect = {
			convert_to = castle
			set_title_flag = refill_levy

			hidden_effect = {
				refill_holding_levy = yes

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

		ai_will_do = {
			factor = 1
		}
	}

	convert_tribal_to_city = {
		# filter = owned # CleanSlate: Nonfunctional
		ai_target_filter = owned
		ai_check_interval = 12

		is_high_prio = yes

		only_playable = yes

		from_potential = {
			is_republic = yes
			higher_real_tier_than = BARON
		}

		potential = {
			holder = FROM # filter doesn't seem to be enough
			holding_type = tribal
		}

		allow = {
			has_building = tb_basic_keep_5

			location = {
				show_scope_change = no

				religion = FROM
			}
		}

		effect = {
			convert_to = city
			set_title_flag = refill_levy

			hidden_effect = {
				refill_holding_levy = yes

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

		ai_will_do = {
			factor = 1
		}
	}

	convert_tribal_to_temple = {
		# filter = owned # CleanSlate: Nonfunctional
		ai_target_filter = owned
		ai_check_interval = 12

		is_high_prio = yes

		only_playable = yes

		from_potential = {
			is_theocracy = yes
			higher_real_tier_than = BARON
			is_not_unreformed_pagan_trigger = yes
		}

		potential = {
			holder = FROM # filter doesn't seem to be enough
			holding_type = tribal
		}

		allow = {
			has_building = tb_basic_keep_5

			location = {
				show_scope_change = no

				religion = FROM
			}
		}

		effect = {
			convert_to = temple
			set_title_flag = refill_levy

			hidden_effect = {
				refill_holding_levy = yes

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

		ai_will_do = {
			factor = 0.1 # takes ten years on average
		}
	}

	make_primary = {
		# filter = owned # CleanSlate: Nonfunctional
		ai_target_filter = owned
		ai_check_interval = 3

		only_playable = yes

		from_potential = {
		}

		potential = {
			holder = FROM # filter doesn't seem to be enough
			is_capital = no

			dejure_liege_title = {
				holder = FROM
			}
		}

		allow = {
			FROM = {
				show_scope_change = no

				is_merchant_republic = no
				is_nomadic = no
			}
		}

		effect = {
			make_capital_holding = yes
		}

		ai_will_do = {
			factor = 1

			trigger = {
				is_allowed_holding_type = FROM

				# Not if existing capital is preferred
				defacto_liege_title = {
					NOR = {
						is_preferred_holding_type = FROM
						title = c_roma
					}
				}


				# Not if this settlement is not preferred and another settlement is
				trigger_if = {
					limit = { NOT = { is_preferred_holding_type = FROM } }

					NOT = {
						defacto_liege_title = {
							any_direct_de_jure_vassal_title = {
								holder = FROM
								is_preferred_holding_type = FROM
							}
						}
					}
				}

				# Not if settlement is a tribe and location is of correct culture or religion
				trigger_if = {
					limit = { holding_type = tribal }

					NAND = {
						defacto_liege_title = {
							OR = {
								holding_type = castle
								holding_type = city
							}
						}

						location = {
							OR = {
								culture = FROM
								religion = FROM
							}
						}
					}
				}
			}
		}
	}

	revoke_primary = {
		# filter = owned # CleanSlate: Nonfunctional
		ai_target_filter = owned
		ai_check_interval = 3

		only_playable = yes

		from_potential = {
			is_nomadic = yes
			has_dlc = "Horse Lords"
		}

		potential = {
			is_capital = yes
			holder = FROM # filter doesn't seem to be enough

			holder_scope = {
				capital_holding = {
					NOT = { title = PREVPREV }
				}
			}

			capital_scope = {
				has_empty_holding = yes
			}

			NOT = { holding_type = nomad }
		}

		effect = {
			revoke_capital_holding = yes
		}

		ai_will_do = {
			factor = 1
		}
	}

	pillage_settlement = {
		filter = all
		ai_target_filter = owned
		ai_check_interval = 3

		is_high_prio = yes

		only_playable = yes

		from_potential = {
			has_dlc = "Horse Lords"

			OR = {
				is_nomadic = yes
				is_tribal = yes
			}
		}

		potential = {
			OR = {
				holder = FROM
				controlled_by = FROM # Not available to AI, due to filters
			}

			has_any_building = yes

			trigger_if = {
				limit = { holding_type = tribal }
				FROM = { is_tribal = no }
			}

			trigger_if = {
				limit = { FROM = { is_nomadic = no } }

				location = {
					num_of_settlements >= 2
				}
			}

			NOR = {
				holding_type = nomad
				has_title_flag = nomad_pillage_flag
				is_allowed_holding_type = FROM
			}
		}

		allow = {
			OR = {
				FROM = { independent = yes }

				location = {
					owner = { character = FROM }
					NOT = { has_province_modifier = recently_burnt_the_land }
				}

				custom_tooltip = {
					text = TT_NOT_SAME_REALM

					location = {
						owner = {
							NOT = { same_realm = FROM }
						}
					}
				}
			}
		}

		effect = {
			set_title_flag = nomad_pillage_flag
			save_event_target_as = pillaged_settlement
			log = "[From.GetTitledName] has pillaged [Root.GetName]"

			if = {
				limit = { holding_type = tribal }

				location = {
					show_scope_change = no

					if = {
						limit = { owned_by = FROM }
						FROM = { show_scope_change = no wealth = 25 }
					}
					else = {
						FROM = { show_scope_change = no wealth = 5 }
					}
				}
			}
			else = {
				location = {
					show_scope_change = no

					if = {
						limit = { owned_by = FROM }
						FROM = { show_scope_change = no wealth = 50 }
					}
					else = {
						FROM = { show_scope_change = no wealth = 10 }
					}
				}

				FROM = {
					show_scope_change = no

					random_list = {
						33 = { military_techpoints = 5 }
						33 = { economy_techpoints = 5 }
						33 = { culture_techpoints = 5 }
					}
				}
			}

			location = {
				show_scope_change = no

				if = {
					limit = {
						OR = {
							has_castle = yes
							has_city = yes
							has_temple = yes
							has_tribal = yes
						}
					}

					if = {
						limit = { owned_by = FROM }

						add_province_modifier = {
							name = burnt_the_land
							years = 8
							stacking = yes
						}

						add_province_modifier = {
							name = recently_burnt_the_land
							months = 6
						}
					}
					else = {
						add_province_modifier = {
							name = recently_burnt_the_land_not_owner
							years = 10
						}
					}
				}
			}

			custom_tooltip = {
				text = PILLAGE_DESTROY_BUILDINGS

				destroy_random_building = THIS

				if = {
					limit = { holder = FROM }
					destroy_random_building = THIS
				}
				else = {
					holder_scope = {
						opinion = {
							who = FROM
							name = opinion_outraged
						}
					}
				}
			}

			hidden_effect = {
				location = { save_event_target_as = nickname_location }

				FROM = {
					save_event_target_as = nickname_receiver

					if = {
						limit = {
							has_nickname = no
							is_adult = yes
							is_incapable = no
						}

						random_list = {
							5 = { give_nickname = nick_the_destroyer }

							5 = {
								trigger = {
									NOT = { religion = event_target:nickname_location }
								}

								give_nickname = nick_the_scourge_of_god
							}

							5 = {
								trigger = {
									NOT = { religion = event_target:nickname_location }
								}

								give_nickname = nick_the_slayer_of_culture
							}

							5 = { give_nickname = nick_the_terror_of_provincename }

							5 = {
								trigger = {
									NOT = { religion = event_target:nickname_location }
								}

								give_nickname = nick_the_culture_marauder
							}

							75 = { }
						}
					}
				}
			}

			FROM = {
				character_event = {
					id = HL.4800
					months = 6
				}
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	stop_pillage_settlement = {
		is_high_prio = yes

		filter = all

		ai = no

		from_potential = {
			has_dlc = "Horse Lords"

			OR = {
				is_nomadic = yes
				is_tribal = yes
			}
		}

		potential = {
			OR = {
				holder = FROM
				controlled_by = FROM
			}

			has_title_flag = nomad_pillage_flag

			trigger_if = {
				limit = { holding_type = tribal }
				FROM = { is_tribal = no }
			}
		}

		allow = {
			location = {
				has_province_modifier = recently_burnt_the_land
			}
		}

		effect = {
			clr_title_flag = nomad_pillage_flag
		}

		ai_will_do = {
			factor = 0
		}
	}

	adopt_tribalism = {
		# filter = sub_realm_owned # CleanSlate: Nonfunctional
		ai_target_filter = sub_realm_owned
		ai_check_interval = 3

		is_high_prio = yes

		only_independent = yes

		from_potential = {
			independent = yes
			is_nomadic = yes
			has_dlc = "Horse Lords"
			is_landed = yes

			trigger_if = {
				limit = { ai = no }
				has_safe_religion = yes
			}
		}

		potential = {
			OR = {
				holding_type = tribal
				holding_type = castle
				holding_type = city

				AND = {
					holding_type = nomad
					holder = FROM
				}
			}

			holder_scope = {
				OR = {
					character = FROM

					AND = {
						is_vassal_or_below_of = FROM
						is_nomadic = no

						NOT = {
							any_liege = {
								NOT = { character = FROM }
								is_nomadic = yes
							}
						}
					}
				}
			}
		}

		allow = {
			OR = {
				is_capital = yes

				capital_scope = {
					owned_by = FROM
				}
			}

			has_siege = no

			FROM = {
				independent = yes
				war = no
			}

			OR = {
				holding_type = tribal
				holding_type = castle
				holding_type = city

				AND = {
					holding_type = nomad
					has_building = no_baghatur_council_2
				}
			}
		}

		effect = {
			log = "[From.GetTitledName] has adopted tribalism, with [Root.GetName] as the new capital"

			# Change name of Avaria to Hungary if relevant.
			if = {
				limit = {
					FROM = { culture = hungarian }
					kingdom = { title = k_carpathia }
					k_hungary = { has_holder = no }
				}

				hidden_effect = {
					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 }

					FROM = {
						any_realm_province = {
							limit = { held_under_PREV = yes }
							set_province_flag = change_to_tribal
						}

						character_event = {
							id = 62912
							days = 1
						}
					}
				}

				k_hungary = {
					show_scope_change = no

					reset_coa = THIS
					grant_title = FROM
				}
			}

			# Usurp vassal kingdoms
			FROM = {
				show_scope_change = no

				if = {
					limit = {
						any_vassal = {
							real_tier = KING
							is_nomadic = no
						}
					}

					any_vassal = {
						show_scope_change = no

						limit = {
							real_tier = KING
							is_nomadic = no
						}

						any_demesne_title = {
							show_scope_change = no

							limit = { real_tier = KING }

							usurp_title = FROM
						}
					}
				}
			}

			# Usurp capital duchy
			if = {
				limit = {
					duchy = {
						has_holder = yes
						holder_scope = { same_realm = FROM }
					}
				}

				duchy = {
					show_scope_change = no

					usurp_title = FROM
				}
			}

			# Usurp county
			county = {
				show_scope_change = no

				save_event_target_as = base_title

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

			if = {
				limit = { is_capital = no }
				make_capital_holding = yes
			}

			FROM = {
				show_scope_change = no

				capital = PREV
			}

			custom_tooltip = { text = TT_BUILD_TRIBAL_HOLDINGS }

			FROM = {
				show_scope_change = no

				if = {
					limit = { ai = no }

					chronicle = {
						entry = CHRONICLE_ADOPTED_TRIBALISM
						picture = GFX_evt_tribal_lands
					}
				}

				hidden_effect = {
					any_vassal = {
						limit = { is_nomadic = yes }
						set_government_type = tribal_government
					}
				}

				set_government_type = tribal_government

				if = {
					limit = { lower_real_tier_than = KING }

					create_title = {
						tier = KING
						landless = no
						temporary = no
						custom_created = yes
						culture = FROM
						holder = FROM
						name = TRIBAL_CUSTOM
						base_title = event_target:base_title
						copy_title_laws = yes
					}
				}

				if = {
					limit = { higher_real_tier_than = COUNT }

					primary_title = {
						show_scope_change = no

						set_title_flag = non_manual_law_change # No prestige loss
						add_law = tribal_organization_0
						clr_title_flag = non_manual_law_change # No prestige loss
					}
				}

				any_player = {
					limit = { NOT = { character = FROM } }
					narrative_event = { id = HL.2 }
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				year >= 1000

				FROM = {
					capital_scope = {
						NOT = { region = world_steppe }
					}

					num_of_count_titles_in_realm >= 20

					OR = {
						trait = content
						culture = hungarian
						culture = alan
						NOT = { religion_group = pagan_group }
					}

					any_realm_province = {
						count < 7

						region = world_steppe
						held_under_PREV = yes
					}
				}

				# Hungarian AI must settle inside the Carpathian basin
				trigger_if = {
					limit = {
						FROM = {
							culture = hungarian

							any_realm_province = {
								count = 4

								kingdom = { title = k_carpathia }
								held_under_PREV = yes
							}
						}
					}

					kingdom = {
						title = k_carpathia
					}

					k_hungary = {
						has_holder = no
					}
				}
			}

			# Nudge Hungarian AI to settle in Pest
			mult_modifier = {
				factor = 0.5
				NOT = { title = b_pest }
				FROM = { culture = hungarian }
				kingdom = { title = k_carpathia }
				k_hungary = { has_holder = no }
			}
		}
	}

	nomad_adopt_feudalism = {
		# filter = sub_realm_owned # CleanSlate: Nonfunctional
		ai_target_filter = sub_realm_owned
		ai_check_interval = 6
		is_high_prio = yes

		ai = no

		from_potential = {
			always = no # Disabled in CK2Plus
			is_nomadic = yes
			has_dlc = "Horse Lords"

			trigger_if = {
				limit = { ai = no }
				has_safe_religion = yes
			}
		}

		potential = {
			OR = {
				holding_type = castle

				AND = {
					holding_type = temple

					holder_scope = {
						religion_group = muslim
					}
				}
			}

			holder_scope = {
				OR = {
					character = FROM

					AND = {
						is_vassal_or_below_of = FROM
						is_nomadic = no

						NOT = {
							any_liege = {
								NOT = { character = FROM }
								is_nomadic = yes
							}
						}
					}
				}
			}
		}

		allow = {
			OR = {
				is_capital = yes

				capital_scope = {
					owned_by = FROM
				}
			}

			has_siege = no

			FROM = {
				independent = yes
				war = no
			}
		}

		effect = {
			# Change culture and religion of new capital province and possibly more provinces depending on nomad population_and_manpower
			location = {
				if = {
					limit = {
						NOT = { culture = FROM }
					}

					culture = FROM
				}

				if = {
					limit = {
						NOT = { religion = FROM }
					}

					religion = FROM
				}
			}

			if = {
				limit = {
					FROM = {
						population_and_manpower >= 5000

						any_realm_province = {
							NOT = { culture = ROOT_FROM }
							num_of_settlements >= 1
							held_under_PREV = yes

							ROOT = {
								location = {
									NOT = { province = PREVPREV } # Another province, not the capital
								}
							}
						}
					}
				}

				custom_tooltip = {
					text = nomad_settle_conversion_tooltip

					FROM = {
						# Calculate how many provinces to convert to ruler's religion and culture
						export_to_variable = {
							which = current_population_and_manpower
							value = population_and_manpower
						}

						divide_variable = {
							which = current_population_and_manpower
							value = 5000
						}

						# Cap on amount of additional provinces to convert
						if = {
							limit = {
								check_variable = {
									which = current_population_and_manpower
									value > 19
								}
							}

							set_variable = {
								which = current_population_and_manpower
								value = 19
							}
						}

						while = {
							limit = {
								check_variable = {
									which = current_population_and_manpower
									value > 0
								}
							}

							subtract_variable = {
								which = current_population_and_manpower
								value = 1
							}

							random_realm_province = {
								limit = {
									NOT = { culture = ROOT_FROM }
									num_of_settlements >= 1
									held_under_PREV = yes

									any_neighbor_province = {
										culture = ROOT_FROM
										religion = ROOT_FROM
									}
								}

								culture = ROOT_FROM
								religion = ROOT_FROM
							}
						}
					}
				}
			}

			# Usurp kingdom if only one vassal has a kingdom
			FROM = {
				show_scope_change = no

				if = {
					limit = {
						any_vassal = {
							count == 1

							tier = KING
							is_feudal = yes
						}

					#	NOT = {
					#		any_vassal = {
					#			count >= 2
					#
					#			is_feudal = yes
					#			tier = KING
					#		}
					#	}
					}

					any_vassal = {
						show_scope_change = no

						limit = {
							tier = KING

							OR = {
								is_feudal = yes
								is_tribal = yes
							}
						}

						any_demesne_title = {
							show_scope_change = no
							limit = { tier = KING }

							usurp_title = ROOT_FROM
						}
					}
				}
			}

			hidden_effect = {
				# Change name of Avaria to Hungary if relevant
				if = {
					limit = {
						FROM = { culture = hungarian }

						location = {
							kingdom = { title = k_hungary }
						}

						k_hungary = {
							OR = {
								has_holder = no
								holder = FROM
							}
						}
					}

					set_global_flag = avar_khaganate_renamed

					k_hungary = {
						reset_coa = THIS
						set_name = ""
						adjective = ""
						grant_title = FROM
					}
				}

				if = {
					limit = {
						FROM = {
							dynasty = 708 #Ýrpád
							has_landed_title = k_hungary
						}
					}

					FROM = {
						any_realm_province = {
							limit = {
								held_under_PREV = yes
								culture = avar
							}

							culture = PREV
							religion = PREV
						}

						k_hungary = {
							any_direct_de_jure_vassal_title = {
								limit = { tier = DUKE }

								add_claim = PREVPREV
							}
						}
					}
				}
			}

			# Spawn event troops based on amount of empty holdings abandoned.
			custom_tooltip = {
				text = NOMAD_CONVERSION_TROOP_SPAWN

				location = {
					FROM = {
						any_realm_province = {
							limit = {
								OR = {
									has_castle = yes
									has_temple = yes
									has_city = yes
									has_tribal = yes
								}

								held_under_PREV = yes
							}

							ROOT_FROM = {
								spawn_unit = {
									province = PREVPREVPREV
									owner = THIS
									attrition = 1
									merge = yes

									troops = {
										light_cavalry = { 140 140 }
										horse_archers = { 60 60 }
									}
								}
							}
						}
					}
				}
			}

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

			hidden_effect = {
				if = {
					limit = { is_capital = no }
					make_capital_holding = yes
				}
			}

			FROM = {
				show_scope_change = no

				capital = PREV
				custom_tooltip = { text = TT_KHAN_INDEPENDENCE }

				most_prestigious_clan_vassal = {
					show_scope_change = no

					custom_tooltip = { text = TT_NEW_KHAN }
				}

				if = {
					limit = { is_government_potential = muslim_government }

					set_government_type = muslim_government

					if = {
						limit = { ai = no }

						chronicle = {
							entry = CHRONICLE_NOMAD_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_NOMAD_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
						}
					}
				}

				hidden_effect = {
					if = {
						limit = {
							higher_tier_than = KING
							NOT = { has_dlc = "Conclave" }
						}

						primary_title = {
							show_scope_change = no

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

					if = {
						limit = { higher_tier_than = COUNT }

						primary_title = {
							show_scope_change = no

							set_title_flag = non_manual_law_change # No prestige loss
							add_law = centralization_0
							clr_title_flag = non_manual_law_change # No prestige loss
						}
					}
				}

				any_player = {
					limit = {
						NOT = { character = ROOT_FROM }
					}
					narrative_event = { id = HL.1 }
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				FROM = {
					trigger_if = {
						limit = {
							NOT = { dynasty = 708 } # Árpád
						}

						num_of_subrealm_castles >= 10

						trigger_if = {
							limit = { population_and_manpower >= 5000 }
							num_of_subrealm_castles >= 20
						}

						holding_diff = {
							first_type = castle
							first_count_vassals = yes
							second_type = none
							value >= 1.5
						}

						NOR = {
							trait = ambitious
							trait = stubborn
							trait = proud
							has_landed_title = e_mongol_empire
							has_landed_title = e_golden_horde
							has_landed_title = e_chagatai
						}
					}
				}
			}

			mult_modifier = {
				factor = 100
				FROM = { dynasty = 708 } # Ýrpád
			}
		}
	}

	nomad_adopt_republicanism = {
		# filter = sub_realm_owned # CleanSlate: Nonfunctional
		ai_target_filter = sub_realm_owned
		ai_check_interval = 6

		is_high_prio = yes

		ai = no

		from_potential = {
			always = no # Disabled in CK2Plus
			independent = yes
			has_dlc = "Horse Lords"
			is_nomadic = yes
			is_female = no

			trigger_if = {
				limit = { ai = no }

				has_dlc = "The Republic"
				has_safe_religion = yes
			}
		}

		potential = {
			holding_type = city

			trigger_if = {
				limit = { FROM = { ai = no } }

				capital_scope = {
					is_coastal = yes
				}
			}

			holder_scope = {
				OR = {
					character = FROM

					AND = {
						is_vassal_or_below_of = FROM
						is_nomadic = no

						NOT = {
							any_liege = {
								NOT = { character = FROM }
								is_nomadic = yes
							}
						}
					}
				}
			}
		}
		allow = {
			OR = {
				is_capital = yes

				capital_scope = {
					owned_by = FROM
				}
			}

			has_siege = no

			FROM = {
				independent = yes
				war = no
			}
		}

		effect = {
			# Change culture and religion of new capital province and possibly more provinces depending on nomad population_and_manpower
			location = {
				if = {
					limit = {
						NOT = { culture = FROM }
					}

					culture = FROM
				}
				if = {
					limit = {
						NOT = { religion = FROM }
					}

					religion = FROM
				}
			}

			if = {
				limit = {
					FROM = {
						population_and_manpower >= 5000

						any_realm_province = {
							NOT = { culture = ROOT_FROM }
							num_of_settlements >= 1
							held_under_PREV = yes

							ROOT = {
								location = {
									NOT = { province = PREVPREV } # Another province, not the capital
								}
							}
						}
					}
				}

				custom_tooltip = {
					text = nomad_settle_conversion_tooltip

					FROM = {
						# Calculate how many provinces to convert to ruler's religion and culture
						export_to_variable = {
							which = current_population_and_manpower
							value = population_and_manpower
						}

						divide_variable = {
							which = current_population_and_manpower
							value = 5000
						}

						# Cap on amount of additional provinces to convert
						if = {
							limit = {
								check_variable = {
									which = current_population_and_manpower
									value > 19
								}
							}

							set_variable = {
								which = current_population_and_manpower
								value = 19
							}
						}

						while = {
							limit = {
								check_variable = {
									which = current_population_and_manpower
									value > 0
								}
							}

							subtract_variable = {
								which = current_population_and_manpower
								value = 1
							}

							random_realm_province = {
								limit = {
									NOT = { culture = ROOT_FROM }
									num_of_settlements >= 1
									held_under_PREV = yes

									any_neighbor_province = {
										culture = ROOT_FROM
										religion = ROOT_FROM
									}
								}

								culture = ROOT_FROM
								religion = ROOT_FROM
							}
						}
					}
				}
			}

			# Change name of Avaria to Hungary if relevant
			hidden_effect = {
				if = {
					limit = {
						FROM = { culture = hungarian }

						location = {
							kingdom = { title = k_hungary }
						}

						k_hungary = {
							OR = {
								has_holder = no
								holder = FROM
							}
						}
					}

					set_global_flag = avar_khaganate_renamed

					k_hungary = {
						reset_coa = THIS
						set_name = ""
						adjective = ""
						grant_title = FROM
					}
				}
			}

			# Spawn event troops based on amount of empty holdings abandoned.
			custom_tooltip = {
				text = NOMAD_CONVERSION_TROOP_SPAWN

				location = {
					FROM = {
						any_realm_province = {
							limit = {
								OR = {
									has_castle = yes
									has_temple = yes
									has_city = yes
									has_tribal = yes
								}

								held_under_PREV = yes
							}

							ROOT_FROM = {
								spawn_unit = {
									province = PREVPREVPREV
									owner = THIS
									attrition = 1
									merge = yes

									troops = {
										light_cavalry = { 140 140 }
										horse_archers = { 60 60 }
									}
								}
							}
						}
					}
				}
			}

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

			if = {
				limit = { is_capital = no }
				make_capital_holding = yes
			}

			FROM = {
				show_scope_change = no

				capital = PREV
				custom_tooltip = { text = TT_KHAN_INDEPENDENCE }

				most_prestigious_clan_vassal = {
					show_scope_change = no

					custom_tooltip = { text = TT_NEW_KHAN }
				}
			}

			hidden_effect = {
				if = {
					limit = {
						capital_scope = {
							is_coastal = yes
						}
					}

					FROM = {
						primary_title = {
							create_title = {
								tier = DUKE
								landless = no
								temporary = no
								custom_created = yes
								culture = ROOT_FROM
								holder = ROOT_FROM
								base_title = THIS
							}
						}

						create_family_palace = yes

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

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

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

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

								if = {
									limit = { is_tribal = yes }

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

										convert_to = city
										refill_holding_levy = yes

										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
										}

										if = {
											limit = {
												dejure_liege_title = {
													holder = PREVPREV

													location = {
														has_castle = no
														num_of_empty_holdings = 2
													}
												}
											}

											location = {
												build_holding = {
													type = castle
												}
											}
										}

										if = {
											limit = {
												dejure_liege_title = {
													holder = PREVPREV

													location = {
														has_temple = no
														num_of_empty_holdings = 2
													}
												}
											}

											location = {
												build_holding = {
													type = temple
												}
											}
										}
									}
								}

								create_family_palace = yes
							}
						}

						set_government_type = merchant_republic_government
					}
				}
			}

			if = {
				limit = {
					capital_scope = {
						is_coastal = yes
					}
				}

				FROM = {
					show_scope_change = no

					set_government_type = merchant_republic_government
				}
			}
			else = {
				FROM = {
					show_scope_change = no

					set_government_type = republic_government
				}
			}

			FROM = {
				show_scope_change = no

				chronicle = {
					entry = CHRONICLE_NOMAD_FOUNDED_MERCHANT_REPUBLIC
					picture = GFX_evt_busy_trading_dock_republic
				}

				any_player = {
					limit = {
						NOT = { character = ROOT_FROM }
					}

					narrative_event = { id = HL.1 }
				}
			}
		}


		ai_will_do = {
			factor = 1

			trigger = {
				FROM = {
					OR = {
						trait = midas_touched
						trait = fortune_builder
						trait = greedy
						trait = administrator
						trait = architect
					}

					num_of_subrealm_cities >= 10

					trigger_if = {
						limit = { population_and_manpower >= 5000 }
						num_of_subrealm_cities >= 20
					}

					holding_diff = {
						first_type = city
						first_count_vassals = yes
						second_type = none
						value >= 1.5
					}

					NOR = {
						trait = ambitious
						trait = stubborn
						trait = proud
						has_landed_title = e_mongol_empire
						has_landed_title = e_golden_horde
						has_landed_title = e_chagatai
					}
				}
			}

			# TODO: Factor is already 1. Aren't these modifiers useless?
			mult_modifier = {
				factor = 5.0
				FROM = { trait = greedy }
			}

			mult_modifier = {
				factor = 2.0
				FROM = { stewardship >= 12 }
			}

			mult_modifier = {
				factor = 2.0
				FROM = { stewardship >= 14 }
			}

			mult_modifier = {
				factor = 2.0
				FROM = { stewardship >= 18 }
			}
		}
	}

	# Debug decision to fix holdings with a dead owner
	fix_holding_with_dead_owner = {
		ai = no

		from_potential = {
		}

		potential = {
			holder_scope = {
				is_alive = no
			}
		}

		allow = {
			holder_scope = {
				is_alive = no
			}
		}

		effect = {
			if = {
				limit = {
					dejure_liege_title = {
						holder_scope = {
							is_alive = yes
						}
					}
				}

				dejure_liege_title = {
					show_scope_change = no

					holder_scope = {
						show_scope_change = no

						grant_title = ROOT
					}
				}
			}
			else = {
				grant_title = FROM
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	settle_iceland_manually = {
		# filter = sub_realm_owned # CleanSlate: Nonfunctional
		ai_target_filter = sub_realm_owned
		ai_check_interval = 60
		is_high_prio = yes

		ai = no

		from_potential = {
			always = no # Disabled in CK2Plus
			higher_tier_than = BARON
			is_alternate_start = no
		}

		potential = {
			location = {
				has_province_modifier = incredibly_poor
			}

			holder_scope = {
				OR = {
					character = FROM
					is_vassal_or_below_of = FROM
				}
			}
		}

		allow = {
			FROM = {
				prestige >= 200
				wealth   >=  25
			}
		}

		effect = {
			location = {
				religion = FROM
				culture = FROM

				capital_holding = {
					if = {
						limit = { holding_type = temple }

						convert_to = tribal
						refill_holding_levy = yes
					}

					holder_scope = {
						if = {
							limit = {
								controls_religion = no
								holy_order = no
								government = theocracy_government
							}

							set_government_type = tribal_government
						}
					}
				}

				remove_province_modifier = incredibly_poor
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

}