﻿namespace = ai_hybridization_events
# AI can hybridize with liege's culture
ai_hybridization_events.1 = {
	scope = culture
	hidden = yes

	immediate = {
		if = {
			limit = {
				exists = culture_head
			}
			culture_head = {
				trigger_event = {
					id = ai_hybridization_events.2
					days = 1
				}
			}
		}
	}
}
ai_hybridization_events.2 = {
	trigger = {
		has_dlc_feature = hybridize_culture
		is_ai = yes
		culture.culture_head = root
		exists = root.liege
		culture = {
			NOR = { 
				has_cultural_parameter = harder_to_hybridize
				has_same_culture_heritage = root.liege.culture
				any_parent_culture_or_above = { this = root.liege.culture }
			}
			cultural_acceptance = { target = root.liege.culture value >= hybridization_threshold_value }
			culture_age >= culture_hybrid_cooldown
		}
		NOR = {
			government_has_flag = government_is_herder
			has_game_rule = none_hybrid_culture_ai_frequency
			faith.religious_head = root
		}
	}

	immediate = {
		# Global cooldowns for the lower frequency game rule settings
		root = {
			save_scope_as = hybridizer
			culture = { save_scope_as = hybridizer_old_culture }
		}
		liege = {
			save_scope_as = liege_scope
		}
		liege.culture = {
			save_scope_as = hybdridization_offer_culture
		}

		if = { # If Liege already hybridized with my culture
			root.liege.culture = {
				limit = {
					any_parent_culture = { this = root.culture}
				}
				save_scope_as = existing_hybrid_culture
			}
		}

		if = { # If a hybrid culture already exists
			limit = {
				any_in_global_list = {
					variable = hybrid_cultures
					any_parent_culture = { this = root.culture }
					any_parent_culture = { this = root.liege.culture }
				}
			}
			random_in_global_list = {
				variable = hybrid_cultures
				limit = {
					any_parent_culture = { this = root.culture }
					any_parent_culture = { this = root.liege.culture }
				}
				save_scope_as = existing_hybrid_culture
			}
		}
	}

	option = {
		name = culture_emergence_events.0001.a

		flavor = new_hybrid_culture_tt

		if = { # If a hybrid culture already exist, convert to that one
			limit = {
				exists = scope:existing_hybrid_culture
			}
			custom_tooltip = culture_emergence_events.0001.existing_hybrid
			custom_tooltip = culture_emergence_events.vassals
			hidden_effect = {
				new_culture_created_vassal_conversion_effect = {
					CONVERTEE = root
					CONVERTER = root
					CULTURE = scope:existing_hybrid_culture
				}
			}
		}
		else = {
			custom_tooltip = culture_emergence_events.0001.new_hybrid
			custom_tooltip = culture_emergence_events.vassals
			hidden_effect = {
				create_hybrid_culture = scope:hybdridization_offer_culture
			}
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = culture_emergence_events.0001.b

		scope:hybdridization_offer_culture = {
			change_cultural_acceptance = {
				target = root.culture
				value = 5
				desc = cultural_acceptance_gain_event
			}
		}

		ai_chance = {
			base = 0
		}
	}

	after = {
		if = {
			limit = {
				exists = domicile.domicile_culture
				domicile.domicile_culture = scope:hybridizer_old_culture
				domicile.domicile_culture != scope:hybridizer.culture
			}
			domicile = {
				set_domicile_culture = scope:hybridizer.culture
			}
			every_held_title = {
				limit = {
					tier = tier_county
					culture = scope:hybridizer_old_culture
				}
				set_county_culture = scope:hybridizer.culture
			}
		}
	}
}
