﻿#Taken from WtWSMS-CK3; used with permission

namespace = ce_bp_heritage_linguistic_drift

##### FIXME: Make portraits code et al. for heritages match the style of the language code
##### FIXME: Generalization of heritage shift localization

#Design Notes:
#0001-0100: Linguistic Drift Events
#0101-0200: Heritage Drift Events

##### Language

##### TRIGGERS

#Mostly we want this case to be where cultures are "well packed (i.e., small)" to allow differences to build
scripted_trigger is_culture_mostly_in_linguistic_region_trigger = {
	any_culture_county = {
		percent >= 0.50
		title_province = { geographical_region = $REGION$ }
	}
}

scripted_trigger the_culture_borders_a_specific_heritage_trigger = {
	any_culture_county = { any_neighboring_county = { this.culture = { has_cultural_pillar = $HERITAGE$ } } }
}

###### EFFECTS

scripted_effect teach_chars_new_language = {
	every_living_character = {
		limit = { this.culture = scope:shift_culture }
		if = {
			limit = { NOT = { knows_language_of_culture = scope:shift_culture } }
			learn_language_of_culture = scope:shift_culture
		}
	}
}

#Brythonek to Children
scripted_effect shift_language_brythonek_to_children_effect = {
	scope:shift_culture = {
		if = {
			limit = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:breton } }
			set_culture_pillar = language_breton
			ccu_culture_created_or_changed_effect = yes
		}
		else_if = {
			limit = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:cornish } }
			set_culture_pillar = language_cornish
			ccu_culture_created_or_changed_effect = yes
		}
		else = { #Fallback
			set_culture_pillar = language_breton
			ccu_culture_created_or_changed_effect = yes
		}
	}
}

#Kymraeg to Welsh & Cumbric
scripted_effect shift_language_kymraeg_to_children_effect = {
	scope:shift_culture = {
		if = {
			limit = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:welsh } }
			set_culture_pillar = language_cymraeg
			ccu_culture_created_or_changed_effect = yes
		}
		else_if = {
			limit = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:wylish } }
			set_culture_pillar = language_cymraeg
			ccu_culture_created_or_changed_effect = yes
		}
		else_if = {
			limit = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:cumbrian } }
			set_culture_pillar = language_cumbrian
			ccu_culture_created_or_changed_effect = yes
		}
		else = { #Fallback
			set_culture_pillar = language_cumbrian
			ccu_culture_created_or_changed_effect = yes
		}
	}
}

scripted_effect shift_language_anglic_to_children_effect = {
	scope:shift_culture = {
		set_culture_pillar = language_middle_english
		ccu_culture_created_or_changed_effect = yes
	}
}

#Main Language Shift Effect
scripted_effect shift_language_effect = {
	#Then apply the new language effect
	scope:shift_culture = {
		#NB: if/else-if prevents possible chain reaction
		if = {
			limit = { has_cultural_pillar = language_brythonek }
			shift_language_brythonek_to_children_effect = yes
			
		}
		else_if = {
			limit = { has_cultural_pillar = language_kymraeg }
			shift_language_kymraeg_to_children_effect = yes
		}
		else_if = {
			limit = { has_cultural_pillar = language_anglic }
			shift_language_anglic_to_children_effect = yes
		}
	}
	teach_chars_new_language = yes
}

####### ACTUAL EVENTS

ce_bp_heritage_linguistic_drift.0001 = {
	scope = none
	hidden = yes
	
	immediate = {
		set_global_variable = { name = recent_language_shift days = 3650 }
		#If the culture head exists, inform him/her first
		if = {
			limit = { exists = scope:shift_culture.culture_head }
			scope:shift_culture.culture_head = { trigger_event = { id = ce_bp_heritage_linguistic_drift.0002 } }
		}
		#Otherwise, directly change the culture and tell everyone
		else = {
			shift_language_effect = yes
			every_ruler = { #Informational event
				limit = { is_ai = no }
				trigger_event = { id = ce_bp_heritage_linguistic_drift.0003 }
			}
		}
	}
}

ce_bp_heritage_linguistic_drift.0002 = {
	type = character_event
	title = ce_bp_heritage_linguistic_drift.0002.t
	desc = {
		first_valid = {
			triggered_desc = { #Anglic to Middle English, English Culture
				trigger = {
					scope:shift_culture = { has_cultural_pillar = language_middle_english }
					scope:shift_culture = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:english } }
				}
				desc = ce_bp_heritage_linguistic_drift.0002.middle_english.as_english_culture.desc
			}
			triggered_desc = { #Anglic to Middle English, *not* English Culture
				trigger = { scope:shift_culture = { has_cultural_pillar = language_middle_english } }
				desc = ce_bp_heritage_linguistic_drift.0002.middle_english.not_as_english_culture.desc
			}
			desc = ce_bp_heritage_linguistic_drift.0002.desc
		}
	}
	
	theme = culture_change
	right_portrait = root
	
	immediate = { hidden_effect = { shift_language_effect = yes } }
	
	option = {
		name = ce_bp_heritage_linguistic_drift.0002.accept
		every_ruler = {
			limit = {
				OR = {
					this = { in_diplomatic_range = root }
					this.culture = { has_same_culture_language = scope:shift_culture }
				}
				NOT = { this = root }
			}
			trigger_event = { id = ce_bp_heritage_linguistic_drift.0003 }
		}
		flavor = ce_bp_heritage_linguistic_drift.0002.accept.tooltip
	}
}

ce_bp_heritage_linguistic_drift.0003 = {
	type = character_event
	title = ce_bp_heritage_linguistic_drift.0003.t
	desc = {
		first_valid = {
			triggered_desc = { #Anglic to Middle English, English Culture
				trigger = {
					scope:shift_culture = { has_cultural_pillar = language_middle_english }
					scope:shift_culture = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:english } }
				}
				desc = ce_bp_heritage_linguistic_drift.0003.middle_english.as_english_culture.desc
			}
			triggered_desc = { #Anglic to Middle English, *not* English Culture
				trigger = { scope:shift_culture = { has_cultural_pillar = language_middle_english } }
				desc = ce_bp_heritage_linguistic_drift.0003.middle_english.not_as_english_culture.desc
			}
			desc = ce_bp_heritage_linguistic_drift.0003.desc
		}
	}
	theme = culture_change
	left_portrait = scope:chosen_character
	right_portrait = root
	
	immediate = {
		if = {
			limit = { exists = scope:shift_culture.culture_head }
			scope:shift_culture.culture_head = { save_scope_as = chosen_character }
		}
		else_if = {
			limit = {
				any_ruler = {
					culture = scope:shift_culture
					NOT = { has_government = mercenary_government } #Keep it to chars on the map please
				}
			}
			random_ruler = {
				limit = { culture = scope:shift_culture }
				save_scope_as = chosen_character
			}
		}
		#Fallback, don't show anything
	}
	
	#For other rulers of the culture
	option = {
		name = ce_bp_heritage_linguistic_drift.0003.accept_same_culture
		flavor = ce_bp_heritage_linguistic_drift.0003.accept_same_culture.tooltip
		trigger = { root.culture = scope:shift_culture }
	}
	#For other rulers not of the culture
	option = {
		name = ce_bp_heritage_linguistic_drift.0003.accept_different_culture
		flavor = ce_bp_heritage_linguistic_drift.0003.accept_different_culture.tooltip
		trigger = { NOT = { root.culture = scope:shift_culture } }
		fallback = yes
	}
}

##### Heritage

# scripted_effect shift_heritage_iranian_to_caucasian_effect = {
# 	scope:shift_culture = {
# 		if = {
# 			limit = { the_culture_or_parent_or_above_is_trigger = { CULTURE = culture:alan } }
# 			set_culture_pillar = heritage_caucasian
# 			ccu_culture_created_or_changed_effect = yes
# 		}
# 		else = { debug_log = "UNHANDLED CULTURE IN ce_bp_heritage_linguistic_drift EVENT SPACE: HERITAGE (IRANIAN=>CAUCASIAN)" }
# 	}
# }

# scripted_effect shift_heritage_effect = {
# 	shift_heritage_iranian_to_caucasian_effect = yes
# }

# ce_bp_heritage_linguistic_drift.0101 = {
# 	scope = none
# 	hidden = yes

# 	immediate = {
# 		set_global_variable = { name = recent_heritage_shift days = 3650 }
# 		#If the culture head exists, inform him/her first
# 		if = {
# 			limit = { exists = scope:shift_culture.culture_head }
# 			scope:shift_culture.culture_head = { trigger_event = { id = ce_bp_heritage_linguistic_drift.0102 } }
# 		}
# 		#Otherwise, directly change the culture and tell everyone
# 		else = {
# 			shift_heritage_effect = yes
# 			every_ruler = { #Informational event
# 				limit = { is_ai = no }
# 				trigger_event = { id = ce_bp_heritage_linguistic_drift.0103 }
# 			}
# 		}
# 	}
# }

# ce_bp_heritage_linguistic_drift.0102 = {
# 	type = character_event
# 	title = ce_bp_heritage_linguistic_drift.0102.t
# 	desc = {
# 		first_valid = {
# 			#Alan to Caucasian
# 			triggered_desc = {
# 				trigger = { scope:shift_culture = culture:alan }
# 				desc = ce_bp_heritage_linguistic_drift.0102.alan.desc
# 			}
# 			#Default
# 			desc = ce_bp_heritage_linguistic_drift.0102.desc
# 		}
# 	}
	
# 	theme = culture_change
# 	left_portrait = root
	
# 	immediate = { hidden_effect = { shift_heritage_effect = yes } }
	
# 	option = {
# 		name = ce_bp_heritage_linguistic_drift.0102.accept
# 		every_ruler = {
# 			limit = {
# 				OR = {
# 					this = { in_diplomatic_range = root }
# 					this.culture = { has_same_culture_language = scope:shift_culture }
# 				}
# 				NOT = { this = root }
# 			}
# 			trigger_event = { id = ce_bp_heritage_linguistic_drift.0103 }
# 		}
# 		flavor = ce_bp_heritage_linguistic_drift.0102.accept.tooltip
# 	}
# }

# ce_bp_heritage_linguistic_drift.0103 = {
# 	type = character_event
# 	title = ce_bp_heritage_linguistic_drift.0103.t
# 	desc = {
# 		first_valid = {
# 			#Alan to Caucasian
# 			triggered_desc = {
# 				trigger = { scope:shift_culture = culture:alan }
# 				desc = ce_bp_heritage_linguistic_drift.0103.alan.desc
# 			}
# 			#Default
# 			desc = ce_bp_heritage_linguistic_drift.0103.desc
# 		}
# 	}
# 	theme = culture_change
# 	left_portrait = scope:chosen_character
	
# 	immediate = {
# 		if = {
# 			limit = { exists = scope:shift_culture.culture_head }
# 			scope:shift_culture.culture_head = { save_scope_as = chosen_character }
# 		}
# 		else_if = {
# 			limit = {
# 				any_ruler = {
# 					culture = scope:shift_culture
# 					NOT = { has_government = mercenary_government } #Keep it to chars on the map please
# 				}
# 			}
# 			random_ruler = {
# 				limit = { culture = scope:shift_culture }
# 				save_scope_as = chosen_character
# 			}
# 		}
# 		#Fallback, don't show anything
# 	}
	
# 	#For other rulers of the culture
# 	option = {
# 		name = ce_bp_heritage_linguistic_drift.0103.accept_same_culture
# 		flavor = ce_bp_heritage_linguistic_drift.0103.accept_same_culture.tooltip
# 		trigger = { root.culture = scope:shift_culture }
# 	}
# 	#For other rulers not of the culture
# 	option = {
# 		name = ce_bp_heritage_linguistic_drift.0103.accept_different_culture
# 		flavor = ce_bp_heritage_linguistic_drift.0103.accept_different_culture.tooltip
# 		trigger = { NOT = { root.culture = scope:shift_culture } }
# 		fallback = yes
# 	}
# }