﻿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 = {
		# 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
				}
			}
		}
	}
}
