# 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
		ai_target_filter = owned
		is_high_prio = yes
		
		from_potential = {
			is_feudal = yes
		}
		
		potential = {
			holding_type = tribal
			holder_scope = {
				character = FROM
			}
		}
		allow = {
			location = {
				religion = FROM
			}
		}
		effect = {
			convert_to = CASTLE
			refill_holding_levy = yes
			
			hidden_tooltip = {
				if = {
					limit = {
						is_capital = yes
						dejure_liege_title = {
							location = {
								num_of_empty_holdings = 2
							}
							NOT = {
								any_direct_de_jure_vassal_title = {
									holding_type = city
								}
							}
						}
					}
					location = {
						build_holding = {
							type = city
						}
					}
				}
				
				if = {
					limit = {
						is_capital = yes
						dejure_liege_title = {
							location = {
								num_of_empty_holdings = 2
							}
							NOT = {
								any_direct_de_jure_vassal_title = {
									holding_type = temple
								}
							}
						}
					}
					location = {
						build_holding = {
							type = temple
						}
					}
				}
			}
		}
		
		revoke_allowed = {
			always = no
		}
		
		ai_will_do = {
			factor = 1
		}
	}

	convert_tribal_to_city = {
		filter = owned
		ai_target_filter = owned
		is_high_prio = yes
		
		from_potential = {
			is_republic = yes
		}
		
		potential = {
			holding_type = tribal
			holder_scope = {
				character = FROM
			}
		}
		allow = {
			location = {
				religion = FROM
			}
		}
		effect = {
			convert_to = CITY
			refill_holding_levy = yes
			
			hidden_tooltip = {
				if = {
					limit = {
						is_capital = yes
						dejure_liege_title = {
							location = {
								num_of_empty_holdings = 2
							}
							NOT = {
								any_direct_de_jure_vassal_title = {
									holding_type = castle
								}
							}
						}
					}
					location = {
						build_holding = {
							type = castle
						}
					}
				}
				
				if = {
					limit = {
						is_capital = yes
						dejure_liege_title = {
							location = {
								num_of_empty_holdings = 2
							}
							NOT = {
								any_direct_de_jure_vassal_title = {
									holding_type = temple
								}
							}
						}
					}
					location = {
						build_holding = {
							type = temple
						}
					}
				}
			}
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 1 # On average ca 1 year before taken
		}
	}
	
	make_primary = {
		filter = owned
		ai_target_filter = owned
		
		potential = {
			is_capital = no
			holder_scope = {
				character = FROM
			}
			dejure_liege_title = {
				holder_scope = {
					character = FROM
				}
			}
		}
		allow = {
			FROM = {
				OR = {
					AND = {
						is_merchant_republic = no
						is_nomadic = no
					}
					capital_scope = {
						NOT = { province = PREVPREV }
					}
				}
			}
		}
		effect = {
			make_capital_holding = yes
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 1
			
			modifier = { # Not if this settlement is not allowed
				factor = 0
				NOT = { is_allowed_holding_type = FROM }
			}
			
			modifier = { # Not if existing capital is preferred
				factor = 0
				defacto_liege_title = {
					is_preferred_holding_type = FROM
				}
			}
			
			modifier = { # Not if this settlement is not preferred and another settlement is
				factor = 0
				NOT = { is_preferred_holding_type = FROM }
				defacto_liege_title = {
					any_direct_de_jure_vassal_title = {
						holder = FROM
						is_preferred_holding_type = FROM
					}
				}
			}
		}
	}
	
	revoke_primary = {
		filter = owned
		ai_target_filter = owned
		
		from_potential = {
			is_nomadic = yes
			has_dlc = "Horse Lords"
		}
		
		potential = {
			is_capital = yes
			holder_scope = {
				character = FROM
				capital_holding = {
					NOT = {
						title = PREVPREV
					}
				}
			}
			capital_scope = {
				has_empty_holding = yes
			}
			NOT = { holding_type = nomad }
		}
		effect = {
			revoke_capital_holding = yes
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 1
		}
	}

	pillage_settlement = {
		filter = owned
		ai_target_filter = owned
		is_high_prio = yes
		
		from_potential = {
			is_nomadic = yes
			has_dlc = "Horse Lords"
		}
		
		potential = {
			NOT = { holding_type = nomad }
			has_siege = no
			controlled_by = FROM
		}
		allow = {
			location = {
				NOT = { has_province_modifier = recently_burnt_the_land }
				OR = {
					owner = { character = FROM }
					NOT = { has_province_modifier = recently_burnt_the_land_not_owner }
				}
			}
			OR = {
				owner = {
					character = FROM
				}
				num_of_buildings = 4
			}
			OR = {
				FROM = {
					independent = yes
					
					custom_tooltip = { 
						text = TT_NOT_SAME_REALM
						hidden_tooltip = {
							FROM = {
								top_liege = {
									PREVPREV = {
										holder_scope = {
											top_liege = {
												NOT = { character = PREVPREVPREV }
											}
										} 
									}
								}
							}
						}
					}
				}
			}
		}
		effect = {
			if = {
				limit = { NOT = { holding_type = tribal } }
				FROM = {
					if = {
						limit = {
							location = {
								owner = {
									character = FROM
								}
							}
						}
						wealth = 50
					}
					if = {
						limit = {
							NOT = {
								location = {
									owner = {
										character = FROM
									}
								}
							}
						}
						wealth = 10
					}
					random_list = {
						10 = { military_techpoints = 10 }
						10 = { economy_techpoints = 10 }
						10 = { culture_techpoints = 10 }
					}
				}
			}
			if = {
				limit = { holding_type = tribal }
				FROM = {
					if = {
						limit = {
							owner = {
								character = FROM
							}
						}
						wealth = 25
					}
					if = {
						limit = {
							NOT = {
								owner = {
									character = FROM
								}
							}
						}
						wealth = 5
					}
					random_list = {
						10 = { military_techpoints = 2 }
						10 = { economy_techpoints = 2 }
						10 = { culture_techpoints = 2 }
					}
				}
			}

			location = {
				if = {
					limit = { 
						any_province_holding = {
							NOT = { holding_type = nomad }
						}
					}
					if = {
						limit = { 
							owner = { 
								character = FROM
							}
						}
						add_province_modifier = {
							modifier = burnt_the_land
							years = 8
							stacking = yes
						}
						add_province_modifier = {
							modifier = recently_burnt_the_land
							months = 6
						}
					}
					if = {
						limit = {
							NOT = {
								owner = { 
									character = FROM
								}
							}
						}
						add_province_modifier = {
							modifier = recently_burnt_the_land_not_owner
							years = 10
						}
					}
				}
			}

			custom_tooltip = {
				text = "PILLAGE_DESTROY_BUILDINGS"
				hidden_tooltip = {
					if = {
						limit = { 
							owner = { 
								character = FROM
							}
						}
						destroy_random_building = THIS
						destroy_random_building = THIS
					}
					if = {
						limit = {
							NOT = {
								owner = { 
									character = FROM
								}
							}
						}
						destroy_random_building = THIS
					}
				}
			}
			if = {
				limit = {
					NOT = { has_any_building = yes }
					owner = {
						character = FROM
					}
				}
				destroy_settlement = THIS
			}
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 1
			
			modifier = { # Only for tribal holdings
				factor = 0
				NOT = { holding_type = tribal }
			}
		}
	}
	
	adopt_tribalism = {
		filter = sub_realm_owned
		ai_target_filter = sub_realm_owned
		is_high_prio = yes
		
		from_potential = {
			is_nomadic = yes
			has_dlc = "Horse Lords"
			is_landed = yes
			
			OR = {
				ai = yes
				AND = {
					OR = {
						has_dlc = "The Sword of Islam"
						NOT = { religion_group = muslim }
					}
					OR = {
						has_dlc = "The Old Gods"
						NOR = {
							religion_group = pagan_group
							religion_group = zoroastrian_group
						}
					}
					OR = {
						has_dlc = "Sons of Abraham"
						NOT = { religion_group = jewish_group }
					}
					OR = {
						has_dlc = "Rajas of India"
						NOT = { religion_group = indian_group }
					}
				}
			}
		}
		potential = {
			OR = {
				holding_type = tribal
				AND = {
					holding_type = nomad
					holder_scope = {
						character = FROM
					}
				}
			}
			holder_scope = {
				OR = {
					character = FROM
					AND = {
						any_liege = {
							character = FROM
						}
						NOR = {
							is_nomadic = yes
							any_liege = {
								NOT = { character = FROM }
								is_nomadic = yes
							}
						}
					}
				}
			}
		}
		allow = {
			OR = {
				is_capital = yes
				capital_scope = {
					holder_scope = {
						character = FROM
					}
				}
			}
			has_siege = no
			FROM = {
				independent = yes
				war = no
			}
			
			OR = {
				holding_type = tribal
				AND = {
					holding_type = nomad
					has_building = no_baghatur_council_2
				}
			}
		}
		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
				}
			}
		
			# Change name of Avaria to Hungary if relevant.
			if = {
				limit = {
					FROM = {
						culture = hungarian
					}
					location = {
						kingdom = { title = k_hungary }
					}
					k_hungary = {
						has_holder = no
					}
				}
				set_global_flag = avar_khaganate_renamed
				k_hungary = {
					reset_coa = THIS
					set_name=""
					adjective=""
					grant_title = FROM
				}
			}
			
			if = {
				limit = { NOT = { holder = FROM } }
				usurp_title = FROM
			}
			
			if = {
				limit = { is_capital = no }
				make_capital_holding = yes
			}
			
			FROM = {
				capital = PREV
			}
			
			custom_tooltip = { 
				text = TT_BUILD_TRIBAL_HOLDINGS
			}
			
			FROM = {
				primary_title = {
					ROOT = {
						custom_tooltip = { 
							text = TT_KHAN_INDEPENDENCE
						}
						most_prestigious_clan_vassal = {
							custom_tooltip = { 
								text = TT_NEW_KHAN
							}
						}
					}
				}
		
				if = {
					limit = {
						ai = no
					}
					chronicle = {
						entry = CHRONICLE_ADOPTED_TRIBALISM
						picture = GFX_evt_tribal_lands
					}
				}
			
				set_government_type = tribal_government
			
				if = {
					limit = {
						higher_tier_than = count
					}
					primary_title = {
						add_law = tribal_organization_0
						add_law = centralization_0
					}
				}
			
				any_playable_ruler = {
					limit = {
						NOT = { character = FROM }
						ai = no
					}
					narrative_event = { id = HL.2 }
				}
			}
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 0
		}
	}
	
	nomad_adopt_feudalism = {
		filter = sub_realm_owned
		ai_target_filter = sub_realm_owned
		is_high_prio = yes
		
		from_potential = {
			is_nomadic = yes
			has_dlc = "Horse Lords"
			
			OR = {
				ai = yes
				AND = {
					OR = {
						has_dlc = "The Sword of Islam"
						NOT = { religion_group = muslim }
					}
					OR = {
						has_dlc = "The Old Gods"
						NOR = {
							religion_group = pagan_group
							religion_group = zoroastrian_group
						}
					}
					OR = {
						has_dlc = "Sons of Abraham"
						NOT = { religion_group = jewish_group }
					}
					OR = {
						has_dlc = "Rajas of India"
						NOT = { religion_group = indian_group }
					}
				}
			}
		}
		potential = {
			OR = {
				holding_type = castle
				AND =
				{
					holding_type = temple
					holder_scope = {
						religion_group = muslim
					}
				}
			}
			holder_scope = {
				OR = {
					character = FROM
					AND = {
						any_liege = {
							character = FROM
						}
						NOR = {
							is_nomadic = yes
							any_liege = {
								NOT = { character = FROM }
								is_nomadic = yes
							}
						}
					}
				}
			}
		}
		allow = {
			OR = {
				is_capital = yes
				capital_scope = {
					holder_scope = {
						character = 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 = FROM }
							num_of_settlements = 1
							ROOT = {
								location = {
									NOT = { province = PREVPREV } # Another province, not the capital
								}
							}
						}
					}
				}
				custom_tooltip = { text = nomad_settle_conversion_tooltip }
			}
			hidden_tooltip = {
				if = {
					limit = {
						FROM = {
							population_and_manpower = 5000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 10000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 15000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 20000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 25000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 30000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 35000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 40000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 45000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 50000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 55000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 60000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 65000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 70000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 75000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 80000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 85000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 90000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 95000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
			}

			# Change name of Avaria to Hungary if relevant.
			if = {
				limit = {
					FROM = {
						culture = hungarian
					}
					location = {
						kingdom = { title = k_hungary }
					}
					k_hungary = {
						has_holder = no
					}
				}
				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
				hidden_tooltip = {
					location = {
						FROM = {
							any_realm_province = {
								limit = {
									NOT = {
										any_province_holding = {
											NOT = { holding_type = nomad }
										}
									}
								}
								FROM = {
									spawn_unit = {
										province = PREVPREVPREV
										owner = THIS
										troops = {
											light_cavalry = { 140 140 }
											horse_archers = { 60 60 }
										}
										attrition = 1
										merge = yes
									}
								}
							}
						}
					}
				}
			}
			
			if = {
				limit = { NOT = { holder = FROM } }
				usurp_title = FROM
			}
			
			if = {
				limit = { is_capital = no }
				make_capital_holding = yes
			}
		
			FROM = {
				capital = PREV
				
				primary_title = {
					FROM = {
						custom_tooltip = { 
							text = TT_KHAN_INDEPENDENCE
						}
						most_prestigious_clan_vassal = {
							custom_tooltip = { 
								text = TT_NEW_KHAN
							}
						}
					}
				}
			
				if = {
					limit = {
						NOT = { religion_group = muslim }
					}
					
					if = {
						limit = {
							ai = no
						}
						chronicle = {
							entry = CHRONICLE_NOMAD_ADOPTED_FEUDALISM
							picture = GFX_evt_castle_construction
						}
					}
					set_government_type = feudal_government
				}
				if = {
					limit = {
						religion_group = muslim
					}
					
					if = {
						limit = {
							ai = no
						}
						chronicle = {
							entry = CHRONICLE_NOMAD_ADOPTED_IQTA
							picture = GFX_evt_castle_construction
						}
					}
					set_government_type = muslim_government
				}
				
				if = {
					limit = {
						higher_tier_than = king
					}
					primary_title = {
						add_law = feudal_administration
					}
				}
				
				if = {
					limit = {
						higher_tier_than = count
					}
					primary_title = {
						add_law = centralization_0
					}
				}
				
				any_playable_ruler = {
					limit = {
						NOT = { character = FROM }
						ai = no
					}
					narrative_event = { id = HL.1 }
				}
			}
		}
		
		revoke_allowed = {
			always = no
		}
		
		ai_will_do = {
			factor = 1 # On average ca 1 year before taken
			
			modifier = {
				factor = 0
				FROM = {
					trait = content
					NOT = { num_of_subrealm_castles = 10 }
				}
			}
			
			modifier = {
				factor = 0
				FROM = {
					trait = ambitious
					NOT = { num_of_subrealm_castles = 20 }
				}
			}
			
			modifier = {
				factor = 0
				FROM = {
					NOR = {
						trait = content
						trait = ambitious
					}
					NOT = { num_of_subrealm_castles = 15 }
				}
			}
			
			modifier = {
				factor = 0
				NOT = {
					FROM = {
						holding_diff = {
							first_type = castle
							first_count_vassals = yes
							second_type = none
							value = 1.5
						}
					}
				}
			}
			
			modifier = {
				factor = 0
				any_demesne_title = {
					OR = {
						title = e_mongol_empire
						title = e_golden_horde
						title = e_chagatai
					}
				}
			}
		}
	}
	
	nomad_adopt_republicanism = {
		filter = sub_realm_owned
		ai_target_filter = sub_realm_owned
		is_high_prio = yes
		
		from_potential = {
			is_nomadic = yes
			OR = {
				ai = yes
				has_dlc = "The Republic"
			}
			has_dlc = "Horse Lords"
			
			OR = {
				ai = yes
				AND = {
					OR = {
						has_dlc = "The Sword of Islam"
						NOT = { religion_group = muslim }
					}
					OR = {
						has_dlc = "The Old Gods"
						NOR = {
							religion_group = pagan_group
							religion_group = zoroastrian_group
						}
					}
					OR = {
						has_dlc = "Sons of Abraham"
						NOT = { religion_group = jewish_group }
					}
					OR = {
						has_dlc = "Rajas of India"
						NOT = { religion_group = indian_group }
					}
				}
			}
		}
		
		potential = {
			holding_type = city
			OR = {
				FROM = {
					ai = yes
				}
				capital_scope = {
					port = yes
				}
			}
			holder_scope = {
				OR = {
					character = FROM
					AND = {
						any_liege = {
							character = FROM
						}
						NOR = {
							is_nomadic = yes
							any_liege = {
								NOT = { character = FROM }
								is_nomadic = yes
							}
						}
					}
				}
			}
		}
		allow = {
			OR = {
				is_capital = yes
				capital_scope = {
					holder_scope = {
						character = 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 = FROM }
							num_of_settlements = 1
							ROOT = {
								location = {
									NOT = { province = PREVPREV } # Another province, not the capital
								}
							}
						}
					}
				}
				custom_tooltip = { text = nomad_settle_conversion_tooltip }
			}
			hidden_tooltip = {
				if = {
					limit = {
						FROM = {
							population_and_manpower = 5000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 10000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 15000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 20000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 25000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 30000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 35000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 40000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 45000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 50000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 55000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 60000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 65000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 70000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 75000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 80000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 85000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 90000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
				if = {
					limit = {
						FROM = {
							population_and_manpower = 95000
							any_realm_province = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
						}
					}
					FROM = {
						random_realm_province = {
							limit = {
								NOT = { culture = FROM }
								num_of_settlements = 1
							}
							culture = FROM
							religion = FROM
						}
					}
				}
			}
			
			# Change name of Avaria to Hungary if relevant.
			if = {
				limit = {
					FROM = {
						culture = hungarian
					}
					location = {
						kingdom = { title = k_hungary }
					}
					k_hungary = {
						has_holder = no
					}
				}
				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
				hidden_tooltip = {
					location = {
						FROM = {
							any_realm_province = {
								limit = {
									NOT = {
										any_province_holding = {
											NOT = { holding_type = nomad }
										}
									}
								}
								FROM = {
									spawn_unit = {
										province = PREVPREVPREV
										owner = THIS
										troops = {
											light_cavalry = { 140 140 }
											horse_archers = { 60 60 }
										}
										attrition = 1
										merge = yes
									}
								}
							}
						}
					}
				}
			}
		
			if = {
				limit = { NOT = { holder = FROM } }
				usurp_title = FROM 
			}
			
			if = {
				limit = { is_capital = no }
				make_capital_holding = yes
			}
		
			FROM = {
				capital = PREV
				
				primary_title = {
					FROM = {
						custom_tooltip = { 
							text = TT_KHAN_INDEPENDENCE
						}
						most_prestigious_clan_vassal = {
							custom_tooltip = { 
								text = TT_NEW_KHAN
							}
						}
					}
				}
			}
			
			hidden_tooltip = {
				if = {
					limit = {
						capital_scope = {
							port = yes
						}
					}
					FROM = {
						primary_title = {
							create_title = {
								tier = DUKE
								landless = no
								temporary = no
								custom_created = yes
								culture = ROOT
								holder = ROOT
								base_title = THIS
							}
						}
						
						create_family_palace = yes
						
						if = {
							limit = {
								NOT = {
									num_of_government_vassals = {
										government = merchant_republic_government
										value = 1
									}
								}
							}
							
							any_unique_dynasty_vassal = { # gives random vassals a family palace
								count = 4
								limit = {
									is_female = no
									OR = {
										is_republic = yes
										is_tribal = yes
										is_patrician = no
									}
									prisoner = no
									NOT = { trait = incapable }
									is_adult = yes
									NOT = { 
										dynasty = none 
									}
								}
								
								if = {
									limit = { is_tribal = yes }
									
									hidden_tooltip = {
										any_demesne_title = {
											limit = {
												tier = baron
												holding_type = tribal
											}
											convert_to = CITY
											refill_holding_levy = yes
											
											if = {
												limit = {
													dejure_liege_title = {
														holder = PREVPREV
														location = {
															num_of_empty_holdings = 2
														}
														NOT = {
															any_direct_de_jure_vassal_title = {
																holding_type = castle
															}
														}
													}
												}
												location = {
													build_holding = {
														type = castle
													}
												}
											}
											if = {
												limit = {
													dejure_liege_title = {
														holder = PREVPREV
														location = {
															num_of_empty_holdings = 2
														}
														NOT = {
															any_direct_de_jure_vassal_title = {
																holding_type = temple
															}
														}
													}
												}
												location = {
													build_holding = {
														type = temple
													}
												}
											}
										}
									}
								}
								
								create_family_palace = yes
							}
						}
							
						set_government_type = merchant_republic_government
					}
				}
			}
			
			if = {
				limit = { 
					capital_scope = {
						port = yes
					}
				}
				FROM = {
					set_government_type = merchant_republic_government
				}
			}
			if = {
				limit = { 
					capital_scope = {
						port = no
					}
				}
				FROM = {
					set_government_type = republic_government
				}
			}
			
			FROM = {
				chronicle = {
					entry = CHRONICLE_NOMAD_FOUNDED_MERCHANT_REPUBLIC
					picture = GFX_evt_busy_trading_dock_republic
				}
				
				any_playable_ruler = {
					limit = {
						NOT = { character = FROM }
						ai = no
					}
					narrative_event = { id = HL.1 }
				}
			}
		}

		revoke_allowed = {
			always = no
		}

		ai_will_do = {
			factor = 0.5
			
			modifier = {
				factor = 0
				FROM = {
					trait = content
					NOT = { num_of_subrealm_cities = 10 }
				}
			}
			
			modifier = {
				factor = 0
				FROM = {
					trait = ambitious
					NOT = { num_of_subrealm_cities = 20 }
				}
			}
			
			modifier = {
				factor = 0
				FROM = {
					NOR = {
						trait = content
						trait = ambitious
					}
					NOT = { num_of_subrealm_cities = 15 }
				}
			}
			
			modifier = {
				factor = 0
				FROM = {
					holding_diff = {
						first_type = city
						first_count_vassals = yes
						second_type = none
						value = 1.5
					}
				}
			}
			
			modifier = {
				factor = 2.0
				FROM = {
					stewardship = 10
				}
			}
			
			modifier = {
				factor = 0
				any_demesne_title = {
					OR = {
						title = e_mongol_empire
						title = e_golden_horde
						title = e_chagatai
					}
				}
			}
		}
	}
	
}