namespace = mpo_misc

scripted_effect leave_behind_heritage_culture_effect = {
	# First, check if there's a previous culture who used to be here. We want them first.
	# It's immersive to see previous cultures come back, but we don't want accidental erasure of Mongolic/Turkic heritage.
	# So we only care about the previous culture if it shares the 'core heritage' of the Steppe Region they're in.
	# Mongolic heritage in the east, turkic in the center and west, etc.
	if = {
		limit = {
			scope:old_capital_county.var:migration_previous_culture ?= {
				has_same_culture_heritage = $CULTURE$
			}
		}
		scope:old_capital_county.holder = {
			set_culture = scope:old_capital_county.var:migration_previous_culture
		}
		# If cultures are large, divergent cultures are more interesting and immersive than mega-blobs.
		scope:old_capital_county.var:migration_previous_culture = {
			if = {
				limit = {
					any_culture_county = { count >= 20 }
				}
				save_scope_as = large_divergence_chance
			}
		}
	}
	else_if = {
		limit = {
			scope:old_holder.domicile.domicile_culture ?= {
				has_same_culture_heritage = $CULTURE$
			}
		}
		scope:old_capital_county.holder = {
			set_culture = scope:old_holder.domicile.domicile_culture
		}
		# If cultures are large, divergent cultures are more interesting and immersive than mega-blobs.
		scope:old_holder.domicile.domicile_culture = {
			if = {
				limit = {
					any_culture_county = { count >= 20 }
				}
				save_scope_as = large_divergence_chance
			}
		}
	}
	else = {
		scope:old_capital_county.holder = {
			set_culture = $CULTURE$
		}
		$CULTURE$ = {
			if = {
				limit = {
					any_culture_county = { count >= 20 }
				}
				save_scope_as = large_divergence_chance
			}
			else = {
				save_scope_as = small_divergence_chance
			}
		}
	}
}

mpo_misc.0001 = {
	type = character_event
	hidden = yes

	trigger = {
		scope:old_capital_county.holder = {
			government_has_flag = government_is_herder
		}
	}

	immediate = {
		# Eastern Steppe: Mongolic Heritage (Mongol)
		if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_steppe_east
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:mongol }
		}
		# Western Siberian Steppe: Turkic Heritage (Kirghiz)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = dlc_mpo_steppe_central_siberia_addon
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:kirghiz }
		}
		# Central Steppe North: Turkic Heritage (Kipchak)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_steppe_central
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:kipchak }
		}
		# Central Steppe South: Turkic Heritage (Oghuz)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = heritage_area_turkestan
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:turkish }
		}
		# Kirghiz 1066 Eastern Steppe Homelands: Turkic Heritage (Kirghiz)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = kirghiz_homelands
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:kirghiz }
		}
		# Eastern Siberian Steppe: Mongol Heritage (Buryat)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = dlc_mpo_steppe_east_buryatia_addon
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:buryat }
		}
		# Perm: Ugro Permian Heritage (Permian)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = heritage_area_perm
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:komi }
		}
		# Sibir: Ugro Permian Heritage (Ostyak)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = dlc_mpo_steppe_siberia_further_expansion
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:khanty }
		}
		# Khorasan: Iranian Heritage (Tajik)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_khorasan
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:tajik }
		}
		# Persia: Iranian Heritage (Tajik)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = dlc_mpo_steppe_persia_expansion
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:tajik }
		}
		# Nubia: East African Heritage (Beja)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = nubia_nomad_area
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:beja }
		}
		# Horn of Africa: Horn African Heritage (Somali)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = horn_of_africa_nomad_area
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:somali }
		}
		# Volga Bulgaria: Turkic Heritage (Bolghar)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = heritage_area_volga_bulgaria
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:bolghar }
		}
		# Pontic Steppe: Turkic Heritage (Cuman)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = heritage_area_pontic_steppe
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:cuman }
		}
		# Balkans: Turkic Heritage (Pecheneg)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = custom_eastern_balkans
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:pecheneg }
		}
		# Balkans: Turkic Heritage (Bolghar)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = ghw_region_northern_russia
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:bolghar }
		}
		# Poland: Turkic Heritage (Cuman)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = ghw_region_poland
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:cuman }
		}
		# Southern Russia: Turkic Heritage (Cuman)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = ghw_region_southern_russia
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:cuman }
		}
		# Northern Russia: Turkic Heritage (Bolghar)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = ghw_region_southern_russia
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:bolghar }
		}
		# Baltic: Turkic Heritage (Bolghar)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = ghw_region_baltic
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:bolghar }
		}
		# Hungary: Magyar Heritage (Mogyer)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = custom_hungary
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:mogyer }
		}
		# Tarim Basin: Turkic Heritage (Uyghur)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_steppe_tarim
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:uyghur }
		}
		# Africa: Berber Heritage (Butr)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_africa
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:butr }
		}
		# Scandinavia: Sami Heritage (Sami)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_europe_north
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:sami }
		}
		# Middle East: Arab Heritage (Bedouin)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_middle_east
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:bedouin }
		}
		# Horn of Africa: Somali Heritage (Somali)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = horn_of_africa_nomad_area
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:somali }
		}
		# Nubia: Beja Heritage (Beja)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = nubia_nomad_area
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:beja }
		}
		# Karelia: Balto-Finnic Heritage (Karelian)
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = karelia_nomad_area
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:karelian }
		}
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_asia_shiwei_steppe
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:shiwei }
		}
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = world_asia_khitan_steppe
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:khitan }
		}
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = dlc_mpo_steppe_east_buryatia_addon
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:buryat }
		}
		else_if = {
			limit = {
				scope:old_capital_county.title_province = {
					geographical_region = dlc_mpo_steppe_east_andong_addon
				}
			}
			leave_behind_heritage_culture_effect = { CULTURE = culture:jurchen }
		}
		# Everything Else: Turkic Heritage (Oghuz)
		else = {
			leave_behind_heritage_culture_effect = { CULTURE = culture:turkish }
		}
		# Divergences are interesting, and the steppe is the perfect place for them.
		# But avoid tiny, tiny divergences of less than 3 Counties.
		if = {
			limit = {
				exists = scope:large_divergence_chance
				any_in_list = {
					list = old_held_titles
					count >= 3
				}
			}
			random = {
				chance = 0 #50 - disabled for now until we know if it feels good
				scope:old_capital_county.holder = {
					add_character_flag = suppress_culture_notifications_and_achievements
					create_divergent_culture = yes
					remove_character_flag = suppress_culture_notifications_and_achievements
				}
			}
		}
		else_if = {
			limit = {
				exists = scope:small_divergence_chance
				any_in_list = {
					list = old_held_titles
					count >= 3
				}
			}
			random = {
				chance = 0 #10 - disabled for now until we know if it feels good
				scope:old_capital_county.holder = {
					add_character_flag = suppress_culture_notifications_and_achievements
					create_divergent_culture = yes
					remove_character_flag = suppress_culture_notifications_and_achievements
				}
			}
		}
		every_in_list = {
			list = old_held_titles
			limit = {
				NOT = { culture = scope:old_capital_county.holder.culture }
			}
			set_county_culture = scope:old_capital_county.holder.culture
			if = {
				limit = {
					holder ?= {
						government_has_flag = government_is_herder
					}
				}
				holder = {
					set_culture = scope:old_capital_county.holder.culture
				}
			}
		}
	}
}

mpo_misc.0002 = {
	scope = none
	hidden = yes
	orphan = yes
	
	trigger = {
		has_mpo_dlc_trigger = yes
	}
	
	immediate = {
		every_independent_ruler = {
			if = {
				limit = {
					is_ai = yes
					government_has_flag = government_is_nomadic
					is_at_war = no
					save_temporary_scope_as = nomad_capital_ruler
					primary_title.tier = tier_county
					domain_size > domain_limit
					this = top_liege
					any_held_title = {
						tier = tier_county
						is_landless_type_title = no
						this != scope:nomad_capital_ruler.capital_county
						OR = {
							any_neighboring_county = {
								holder.top_liege != scope:nomad_capital_ruler.top_liege
							}
							is_coastal_county = yes
						}
					}
				}
				save_scope_as = nomad_capital_ruler
				random_held_title = {
					limit = {
						tier = tier_county
						is_landless_type_title = no
						this != scope:nomad_capital_ruler.capital_county
						OR = {
							any_neighboring_county = {
								holder.top_liege != scope:nomad_capital_ruler.top_liege
							}
							is_coastal_county = yes
						}
						NOT = {
							any_neighboring_county = {
								this = scope:nomad_capital_ruler.capital_county
							}
						}
					}
					alternative_limit = {
						tier = tier_county
						is_landless_type_title = no
						this != scope:nomad_capital_ruler.capital_county
						OR = {
							any_neighboring_county = {
								holder.top_liege != scope:nomad_capital_ruler.top_liege
							}
							is_coastal_county = yes
						}
					}
					weight = {
						base = 100
						modifier = { #Less likely to pick someone you actively dislike
							add = {
								value = county_fertility
								multiply = -1
							}
							uses_county_fertility = yes
						}
					}
					save_scope_as = title_to_abandon
				}
				create_character = {
					template = herder_character
					dynasty = none
					location = scope:nomad_capital_ruler.location
					save_scope_as = new_herder
				}
				scope:title_to_abandon = {
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					change_title_holder_include_vassals = {
						holder = scope:new_herder
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
					if = {
						limit = {
							title_province = {
								has_holding_type = nomad_holding
							}
						}
						title_province = {
							set_holding_type = herder_holding
						}
					}
				}
				scope:new_herder = {
					change_government = herder_government
					every_held_title = {
						limit = {
							is_landless_type_title = yes
						}
						scope:new_herder = { destroy_title = prev }
					}
				}
				start_tributary_interaction_effect = {
					TRIBUTARY = scope:new_herder
					SUZERAIN = scope:nomad_capital_ruler
				}
				scope:nomad_capital_ruler = {
					add_prestige = minor_prestige_loss
				}
			}
		}
	}
}
