# -*- ck2.events -*-

namespace = emf_heresy

#reserved: emf_heresy.100 to emf_heresy.2199

#######################################
# NEW HERESY EVENTS
# Adapted from Heretic Pride by Rylock
#######################################

#######################################
# MANICHAEAN EVENTS
#######################################

# Manichean leader restored
narrative_event = {
	id = emf_heresy.228
	title = EVTNAME_emf_heresy_228
	desc = EVTDESC_emf_heresy_228
	picture = GFX_evt_zoroaster
	border = GFX_event_narrative_frame_religion

	major = yes
	show_ROOT = yes
	hide_from = yes
	is_triggered_only = yes
	
	immediate = {
		random_realm_lord = {
			limit = {
				in_revolt = no
				religion = manichean
				capital_holding = { is_holy_site = ROOT }
				prisoner = no
				NOT = { trait = incapable }
			}
			d_manichean = { grant_title = PREV }
			set_defacto_liege = ROOT
			save_event_target_as = new_manichean_leader
			break = yes
		}
		random_realm_lord = {
			limit = {
				in_revolt = no
				religion = manichean
				capital_holding = { holding_type = temple }
				prisoner = no
				NOT = { trait = incapable }
				NOT = { trait = bad_priest_zoroastrian }
			}
			d_manichean = { grant_title = PREV }
			set_defacto_liege = ROOT
			save_event_target_as = new_manichean_leader
			ROOT = {
				random_realm_title = {
					limit = { is_holy_site = ROOT }
					gain_title = PREVPREV
				}
			}
			break = yes
		}
		create_random_priest = {
			random_traits = yes
			dynasty = random
			religion = manichean
			culture = ROOT
			age = 40
			health = 7
			trait = quick
			trait = zealous
			trait = scholar
			trait = mastermind_theologian
			attributes = {
				learning = 5
				diplomacy = 5
				martial = 2
				stewardship = 5
				intrigue = 5
			}
		}
		new_character = {
			d_manichean = { grant_title = PREV }
			set_defacto_liege = ROOT
			save_event_target_as = new_manichean_leader
			ROOT = {
				random_realm_title = {
					limit = { is_holy_site = ROOT }
					gain_title = PREVPREV
				}
			}
		}
	}
	
	option = {
		name = EVTOPTA_emf_heresy_228
		trigger = {
			NOT = { religion = manichean }
		}
		event_target:new_manichean_leader = { }
	}
	option = {
		name = EVTOPTB_emf_heresy_228
		trigger = { religion = manichean }
		event_target:new_manichean_leader = { }
	}
}


#######################################
# SHI'ITE EVENTS
#######################################

# Isma'ili Imamah claimant passes on trait to heir
character_event = {
	id = emf_heresy.1970
	
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
		trait = imamah_claimant
	}
	
	immediate = {
		remove_trait = imamah_claimant
		# Pass it onto heir first, if one exists
		if = {
			limit = {
				is_ruler = yes
				current_heir = {
					is_female = no
					religion = ismaili
					NOT = { trait = incapable }
				}
			}
			current_heir = {
				set_character_flag = is_imamah_claimant
				add_trait = imamah_claimant
				add_pressed_claim = d_ismaili
			}		
			break = yes
		}
		# Then pass onto eldest son
		random_child = {
			limit = {
				is_alive = yes
				is_female = no
				religion = ismaili
				NOR = {
					trait = bastard
					trait = incapable
					any_sibling = {
						is_alive = yes
						is_female = no
						religion = ismaili
						NOT = { trait = bastard }
						NOT = { trait = incapable }
						is_older_than = PREV
					}
				}
			}
			set_character_flag = is_imamah_claimant
			add_trait = imamah_claimant
			add_pressed_claim = d_ismaili
			break = yes
		}
		# Then pass onto eldest brother
		random_sibling = {
			limit = {
				is_alive = yes
				is_female = no
				trait = sayyid
				religion = ismaili
				NOR = {
					trait = bastard
					trait = incapable
					any_sibling = {
						is_alive = yes
						is_female = no
						trait = sayyid
						religion = ismaili
						NOT = { trait = bastard }
						NOT = { trait = incapable }
						NOT = { character = ROOT }
						is_older_than = PREV
					}
				}
			}
			set_character_flag = is_imamah_claimant
			add_trait = imamah_claimant
			add_pressed_claim = d_ismaili
			break = yes
		}
		# Then pass onto eldest grandson
		random_dynasty_member = {
			limit = {
				is_alive = yes
				is_female = no
				trait = sayyid
				religion = ismaili
				father_even_if_dead = {
					is_child_of = ROOT
					NOT = { trait = bastard }
				}
				NOR = {
					trait = bastard
					trait = incapable
					any_dynasty_member = {
						is_alive = yes
						is_female = no
						trait = sayyid
						religion = ismaili
						father_even_if_dead = {
							is_child_of = ROOT
							NOT = { trait = bastard }
						}
						NOT = { trait = bastard }
						NOT = { trait = incapable }
						is_older_than = PREV
					}
				}
			}
			set_character_flag = is_imamah_claimant
			add_trait = imamah_claimant
			add_pressed_claim = d_ismaili
		}
	}
}

# Existing Imamah claimant starts uprising
character_event = {
	id = emf_heresy.1971
	
	hide_window = yes
	
	min_age = 18
	max_age = 50
	only_men = yes
	capable_only = yes
	prisoner = no
	religion = ismaili

	trigger = {
		ai = yes
		year = 900
		trait = imamah_claimant
		d_ismaili = { has_holder = no }
		OR = {
			diplomacy = 8
			martial = 8
		}
		NOR = {
			higher_tier_than = DUKE
			trait = incapable
			trait = content
			trait = humble
			trait = cynical
			trait = imbecile
			trait = inbred
			has_character_flag = led_ismaili_caliphate_revolt
			has_global_flag = ismaili_caliphate_revolt
			has_global_flag = ismaili_caliphate_revolt_ongoing
		}
		any_independent_ruler = {
			ai = yes
			is_landed = yes
			higher_tier_than = DUKE
			religion_group = muslim
			NOT = { religion = ismaili }
			decadence = 25
			OR = {
				NOT = { is_liege_of = ROOT }
				NOT = { reverse_opinion = { who = ROOT value = 0 } }
			}
			capital_scope = {
				OR = {
					region = world_persia
					region = world_middle_east_jerusalem
					region = world_africa_north
				}
			}
			any_realm_province = {
				religion_group = muslim
				OR = {
					region = world_persia
					region = world_middle_east_jerusalem
					region = world_africa_north
				}
				kingdom = {
					OR = {
						has_holder = no
						holder_scope = { character = PREVPREVPREV }
					}
					num_title_realm_provs = {
						who = PREVPREV
						value = 6
					}
				}
			}
		}
	}
	
	mean_time_to_happen = {
		months = 100
		modifier = {
			factor = 0.5
			trait = ambitious
		}
		modifier = {
			factor = 0.75
			trait = zealous
		}
		modifier = {
			factor = 2
			NOT = { diplomacy = 4 }
		}
		modifier = {
			factor = 1.5
			NOT = { diplomacy = 8 }
		}
		modifier = {
			factor = 0.8
			diplomacy = 12
		}
		modifier = {
			factor = 0.8
			diplomacy = 14
		}
		modifier = {
			factor = 0.8
			diplomacy = 16
		}
		modifier = {
			factor = 0.8
			diplomacy = 18
		}
		modifier = {
			factor = 0.8
			diplomacy = 20
		}
		modifier = {
			factor = 2
			NOT = { martial = 4 }
		}
		modifier = {
			factor = 1.5
			NOT = { martial = 8 }
		}
		modifier = {
			factor = 0.8
			martial = 12
		}
		modifier = {
			factor = 0.8
			martial = 14
		}
		modifier = {
			factor = 0.8
			martial = 16
		}
		modifier = {
			factor = 0.8
			martial = 18
		}
		modifier = {
			factor = 0.8
			martial = 20
		}
		modifier = {
			factor = 1.5
			NOT = { intrigue = 4 }
		}
		modifier = {
			factor = 1.25
			NOT = { intrigue = 8 }
		}
		modifier = {
			factor = 0.9
			intrigue = 12
		}
		modifier = {
			factor = 0.9
			intrigue = 14
		}
		modifier = {
			factor = 0.9
			intrigue = 16
		}
		modifier = {
			factor = 0.9
			intrigue = 18
		}
		modifier = {
			factor = 0.9
			intrigue = 20
		}
		modifier = {
			factor = 1.5
			NOR = {
				diplomacy = 12
				martial = 12
				intrigue = 12
			}
		}
		modifier = {
			factor = 0.5
			year = 900
		}
		modifier = {
			factor = 0.5
			year = 950
		}
		modifier = {
			factor = 0.25
			year = 1000
		}
		modifier = {
			factor = 0.25
			year = 1050
		}
		modifier = {
			factor = 0.25
			year = 1100
		}
	}
	
	immediate = {
		log = "emf_heresy.1971: [Root.GetBestName] is an existing Isma'ili Imamah claimant who has begun the Isma'ili Caliphate Revolt"
		set_character_flag = ismaili_caliphate_pretender
		set_character_flag = led_ismaili_caliphate_revolt
		set_global_flag = ismaili_caliphate_revolt
		save_event_target_as = ismaili_revolter
		
		# Give priority to Africa/Egypt
		random_independent_ruler = {
			limit = {
				ai = yes
				is_landed = yes
				higher_tier_than = DUKE
				religion_group = muslim
				NOT = { religion = ismaili }
				decadence = 25
				OR = {
					NOT = { is_liege_of = ROOT }
					NOT = { reverse_opinion = { who = ROOT value = 0 } }
				}
				capital_scope = {
					OR = {
						region = world_persia
						region = world_middle_east_jerusalem
						region = world_africa_north
					}
				}
				any_realm_province = {
					religion_group = muslim
					kingdom = {
						OR = {
							title = k_egypt
							title = k_africa
						}
						OR = {
							has_holder = no
							holder_scope = { character = PREVPREVPREV }
						}
						num_title_realm_provs = {
							who = PREVPREV
							value = 6
						}
					}
				}
			}
			save_event_target_as = ismaili_revolt_target
			random_realm_province = {
				limit = {
					religion_group = muslim
					kingdom = {
						OR = {
							title = k_egypt
							title = k_africa
						}
						OR = {
							has_holder = no
							holder_scope = { character = PREVPREVPREV }
						}
						num_title_realm_provs = {
							who = PREVPREV
							value = 6
						}
					}
				}
				kingdom = { save_event_target_as = ismaili_revolt_target_title }
			}
			if = {
				limit = {
					is_ruler = yes
					primary_title = {
						NOT = { kingdom = { title = event_target:ismaili_revolt_target_title } }
					}
				}
				set_character_flag = abdicated_claimant
				primary_title = { save_event_target_as = abdicated_title }
				current_heir = {
					ROOT = { transfer_scaled_wealth = { to = PREV value = all } }
				}
				abdicate = yes
			}
			event_target:ismaili_revolter = {
				narrative_event = { id = emf_heresy.1972 }
			}
			break = yes
		}
		
		# Then anywhere else on continental Africa
		random_independent_ruler = {
			limit = {
				ai = yes
				is_landed = yes
				higher_tier_than = DUKE
				religion_group = muslim
				NOT = { religion = ismaili }
				decadence = 25
				OR = {
					NOT = { is_liege_of = ROOT }
					NOT = { reverse_opinion = { who = ROOT value = 0 } }
				}
				capital_scope = {
					OR = {
						region = world_persia
						region = world_middle_east_jerusalem
						region = world_africa_north
					}
				}
				any_realm_province = {
					religion_group = muslim
					region = world_africa_north
					kingdom = {
						OR = {
							has_holder = no
							holder_scope = { character = PREVPREVPREV }
						}
						num_title_realm_provs = {
							who = PREVPREV
							value = 6
						}
					}
				}
			}
			save_event_target_as = ismaili_revolt_target
			random_realm_province = {
				limit = {
					religion_group = muslim
					region = world_africa_north
					kingdom = {
						OR = {
							has_holder = no
							holder_scope = { character = PREVPREVPREV }
						}
						num_title_realm_provs = {
							who = PREVPREV
							value = 6
						}
					}
				}
				kingdom = { save_event_target_as = ismaili_revolt_target_title }
			}
			if = {
				limit = {
					is_ruler = yes
					primary_title = {
						NOT = { kingdom = { title = event_target:ismaili_revolt_target_title } }
					}
				}
				set_character_flag = abdicated_claimant
				primary_title = { save_event_target_as = abdicated_title }
				current_heir = {
					ROOT = { transfer_scaled_wealth = { to = PREV value = all } }
				}
				abdicate = yes
			}
			event_target:ismaili_revolter = {
				narrative_event = { id = emf_heresy.1972 }
			}
			break = yes
		}

		# Failing that, anywhere valid will do
		random_independent_ruler = {
			limit = {
				ai = yes
				is_landed = yes
				higher_tier_than = DUKE
				religion_group = muslim
				NOT = { religion = ismaili }
				decadence = 25
				OR = {
					NOT = { is_liege_of = ROOT }
					NOT = { reverse_opinion = { who = ROOT value = 0 } }
				}
				capital_scope = {
					OR = {
						region = world_persia
						region = world_middle_east_jerusalem
						region = world_africa_north
					}
				}
				any_realm_province = {
					religion_group = muslim
					OR = {
						region = world_persia
						region = world_middle_east_jerusalem
						region = world_africa_north
					}
					kingdom = {
						OR = {
							has_holder = no
							holder_scope = { character = PREVPREVPREV }
						}
						num_title_realm_provs = {
							who = PREVPREV
							value = 6
						}
					}
				}
			}
			save_event_target_as = ismaili_revolt_target
			random_realm_province = {
				limit = {
					religion_group = muslim
					OR = {
						region = world_persia
						region = world_middle_east_jerusalem
						region = world_africa_north
					}
					kingdom = {
						OR = {
							has_holder = no
							holder_scope = { character = PREVPREVPREV }
						}
						num_title_realm_provs = {
							who = PREVPREV
							value = 6
						}
					}
				}
				kingdom = { save_event_target_as = ismaili_revolt_target_title }
			}
		}		
		if = {
			limit = {
				is_ruler = yes
				primary_title = {
					NOT = { kingdom = { title = event_target:ismaili_revolt_target_title } }
				}
			}
			set_character_flag = abdicated_claimant
			primary_title = { save_event_target_as = abdicated_title }
			current_heir = {
				ROOT = { transfer_scaled_wealth = { to = PREV value = all } }
			}
			abdicate = yes
		}
		event_target:ismaili_revolter = {
			narrative_event = { id = emf_heresy.1972 }
		}
	}
}

# Main Isma'ili Caliphate Revolt event
narrative_event = {
	id = emf_heresy.1972
	title = EVTNAME_emf_heresy_1972
	picture = GFX_evt_bishop_muslim
	border = GFX_event_narrative_frame_religion
	
	desc = {
		text = EVTDESC_emf_heresy_1972_found
		trigger = {
			event_target:ismaili_revolter = {
				has_character_flag = found_claimant
			}
		}
	}
	desc = {
		text = EVTDESC_emf_heresy_1972_abdicate
		trigger = {
			event_target:ismaili_revolter = {
				NOT = { has_character_flag = found_claimant }
				has_character_flag = abdicated_claimant
			}
		}
	}
	desc = {
		text = EVTDESC_emf_heresy_1972
		trigger = {
			event_target:ismaili_revolter = {
				NOT = { has_character_flag = found_claimant }
				NOT = { has_character_flag = abdicated_claimant }
			}
		}
	}

	is_triggered_only = yes
	hide_from = yes
	show_ROOT = yes
	major = yes
	
	immediate = {
		set_global_flag = ismaili_caliphate_revolt_ongoing
		event_target:ismaili_revolt_target = {
			random_realm_province = {
				limit = { kingdom = { title = event_target:ismaili_revolt_target_title } }
				event_target:ismaili_revolter = {
					create_title = {
						tier = KING
						landless = yes
						temporary = yes
						culture = THIS
						name = ISMAILI_CALIPHATE_REVOLT
						holder = THIS
						ruler = "IMAM"
						ruler_female = "IMAMA"
						foa = "CALIPH_FOA"
					}
					primary_title = {
						add_law = succ_turkish_succession
					}
					any_dynasty_member = {
						limit = {
							is_ruler = no
							prisoner = no
							is_close_relative = PREV
						}
						move_character = PREV
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 30
						trait = brilliant_strategist
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.5
							attrition = 0.25
							maintenance_multiplier = 0
							reinforces = yes
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 27
						trait = brilliant_strategist
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.3
							attrition = 0.25
							maintenance_multiplier = 0
							reinforces = yes
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 25
						trait = skilled_tactician
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.15
							attrition = 0.25
							maintenance_multiplier = 0
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 23
						trait = skilled_tactician
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.15
							attrition = 0.25
							maintenance_multiplier = 0
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 27
						trait = skilled_tactician
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.15
							attrition = 0.25
							maintenance_multiplier = 0
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 24
						trait = skilled_tactician
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.15
							attrition = 0.25
							maintenance_multiplier = 0
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 32
						trait = brilliant_strategist
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.15
							attrition = 0.25
							maintenance_multiplier = 0
							cannot_inherit = yes
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 25
						trait = skilled_tactician
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.15
							attrition = 0.25
							maintenance_multiplier = 0
							cannot_inherit = yes
							disband_on_peace = yes
						}
					}
					create_character = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 28
						trait = skilled_tactician
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
						spawn_unit = {
							province = PREVPREV
							home = PREVPREV
							owner = PREV
							match_character = PREVPREVPREV
							match_mult = 0.15
							attrition = 0.25
							maintenance_multiplier = 0
							cannot_inherit = yes
							disband_on_peace = yes
						}
					}
					create_random_priest = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 50
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
					}
					create_random_diplomat = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 40
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
					}
					create_random_intriguer = {
						random_traits = yes
						dynasty = none
						religion = THIS
						culture = THIS
						female = no
						age = 40
					}
					new_character = {
						opinion = { who = PREV modifier = opinion_loyal_vassal multiplier = 2 }
					}
					war = {
						target = PREVPREV
						casus_belli = emf_ismaili_caliphate_rising
						thirdparty_title = PREV
						tier = KING
						infamy = 0
					}
					recalc_succession = yes
				}
			}
		}
	}
	
	option = {
		name = CURSES
		trigger = { character = event_target:ismaili_revolt_target }
	}
	option = {
		name = "EVTOPTB39640"
		trigger = { religion = ismaili }
	}
	option = {
		name = "EVTOPTC39640"
		trigger = {
			religion_group = muslim
			NOT = { religion = ismaili }
			NOT = { character = event_target:ismaili_revolt_target }
		}
	}
	option = {
		name = "EVTOPTD39640"
		trigger = {
			NOT = { religion_group = muslim }
			NOT = { character = event_target:ismaili_revolt_target }
		}
	}
}

# Event troops received upon forming the Ismaili Caliphate
character_event = {
	id = emf_heresy.1973
	desc = EVTDESC_emf_heresy_1973
	picture = GFX_evt_bishop_muslim
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EXCELLENT
		d_ismaili = { set_title_flag = ismaili_followers }
		capital_scope = {
			ROOT = {
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						light_infantry = { 1600 1600 }
						light_cavalry = { 840 840 }
						heavy_infantry = { 210 210 }
						knights = { 55 55 }
					}
					attrition = 1.0
					maintenance_multiplier = 0
					earmark = ismaili_claimant
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						light_infantry = { 1600 1600 }
						light_cavalry = { 840 840 }
						heavy_infantry = { 210 210 }
						knights = { 55 55 }
					}
					attrition = 1.0
					maintenance_multiplier = 0
					earmark = ismaili_claimant
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						light_infantry = { 1600 1600 }
						light_cavalry = { 840 840 }
						heavy_infantry = { 210 210 }
						knights = { 55 55 }
					}
					attrition = 1.0
					maintenance_multiplier = 0
					earmark = ismaili_claimant
				}
			}
		}		
	}
}

# Prompt AI Imamah claimant to marry
character_event = {
	id = emf_heresy.1974
	
	hide_window = yes
	ai = yes
	min_age = 20
	prisoner = no
	capable_only = yes
	has_character_flag = is_imamah_claimant
	
	trigger = {
		is_married = no
		is_ill = no
		fertility = 0.33
	}
	
	mean_time_to_happen = {
		months = 6
	}
	
	immediate = {
		if = {
			limit = { is_ruler = no }
			liege = {
				create_character = {
					random_traits = yes
					dynasty = random
					female = yes
					age = 17
					culture = ROOT
					religion = ROOT
				}
				new_character = {
					add_spouse = ROOT
				}
				break = yes
			}
		}
		create_character = {
			random_traits = yes
			dynasty = random
			female = yes
			age = 17
			culture = ROOT
			religion = ROOT
		}
		new_character = {
			add_spouse = ROOT
		}
	}
}
		
# Prompt AI Imamah claimant to have children
character_event = {
	id = emf_heresy.1975
	
	hide_window = yes
	ai = yes
	min_age = 20
	prisoner = no
	capable_only = yes
	has_character_flag = is_imamah_claimant
	
	trigger = {
		is_married = yes
		fertility = 0.33
		NOR = {
			any_child = {
				is_female = no
				NOT = { trait = bastard }
			}
			any_spouse = { is_pregnant = yes }
		}
		any_spouse = {
			is_pregnant = no
			is_ill = no
			prisoner = no
			NOT = { trait = incapable }
			fertility = 0.33
		}
	}
	
	mean_time_to_happen = {
		months = 6
	}
	
	immediate = {
		random_spouse = {
			limit = {
				is_pregnant = no
				is_ill = no
				prisoner = no
				NOT = { trait = incapable }
				fertility = 0.33
			}
			ROOT = { impregnate = PREV }
		}
	}
}

# New Imamah Claimant arises
narrative_event = {
	id = emf_heresy.1976
	title = EVTNAME_emf_heresy_1976
	desc = EVTDESC_emf_heresy_1976
	picture = GFX_evt_bishop_muslim
	border = GFX_event_narrative_frame_religion
	
	major = yes
	hide_from = yes
	show_ROOT = yes
	is_triggered_only = yes
	
	major_trigger = {
		religion_group = muslim
	}
	
	option = {
		name = EVTOPTA_emf_heresy_1976
		trigger = {
			religion = ismaili
		}
	}
	option = {
		name = EVTOPTB_emf_heresy_1976
		trigger = {
			NOT = { religion = ismaili }
		}
	}
}

# Isma'ili Caliphate established
narrative_event = {
	id = emf_heresy.1977
	title = EVTNAME_emf_heresy_1977
	desc = EVTDESC_emf_heresy_1977
	picture = GFX_evt_caliph
	border = GFX_event_narrative_frame_religion
	
	major = yes
	hide_from = yes
	show_ROOT = yes
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_emf_heresy_1977
		trigger = {
			character = ROOT
		}
		hidden_tooltip = {
			any_vassal = {
				limit = {
					is_playable = yes
					religion = ismaili
				}
				character_event = { id = emf_heresy.1978 days = 4 } # Choose whether to convert
			}
			any_independent_ruler = {
				limit = {
					NOT = { character = ROOT }
					is_playable = yes
					religion = ismaili
				}
				character_event = { id = emf_heresy.1978 days = 4 } # Choose whether to convert
			}
		}
	}
	option = {
		name = EVTOPTA_emf_heresy_1978 # Stay Ismaili
		trigger = {
			NOT = { character = ROOT }
			religion = ismaili
			independent = yes
		}
		ai_chance = {
			factor = 75
			modifier = {
				factor = 0.5
				trait = zealous
				NOR = {
					is_close_relative = ROOT
					dynasty = ROOT
					has_non_aggression_pact_with = ROOT
					is_allied_with = ROOT
				}
			}
			modifier = {
				factor = 1.5
				trait = humble
			}
			modifier = {
				factor = 1.5
				trait = craven
			}
			modifier = {
				factor = 1.5
				trait = content
			}
		}
	}
	option = {
		name = EVTOPTB_emf_heresy_1978 # Become Qarmatian
		trigger = {
			NOT = { character = ROOT }
			religion = ismaili
			independent = yes
			controls_religion = no
			mercenary = no
			holy_order = no
			can_change_religion = yes
		}
		ai_chance = {
			factor = 25
			modifier = {
				factor = 0
				OR = {
					is_close_relative = ROOT
					dynasty = ROOT
					has_non_aggression_pact_with = ROOT
					is_allied_with = ROOT
					trait = decadent
				}
			}
			modifier = {
				factor = 0
				NOR = {
					trait = zealous
					trait = lunatic
					trait = paranoid
					trait = proud
					trait = wroth
					trait = misguided_warrior
					trait = tough_soldier
					trait = skilled_tactician
					trait = brilliant_strategist
				}
			}
			modifier = {
				factor = 0
				trait = cynical
			}
			modifier = {
				factor = 3
				trait = lunatic
			}
			modifier = {
				factor = 1.5
				trait = wroth
			}
			modifier = {
				factor = 1.5
				trait = proud
			}
			modifier = {
				factor = 1.5
				trait = brave
			}
			modifier = {
				factor = 2
				independent = yes
				real_tier = KING
			}
			modifier = {
				factor = 3
				independent = yes
				real_tier = EMPEROR
			}
		}
		hidden_tooltip = { religion_authority = { modifier = ruler_converted_from } }
		religion = qarmatian
		hidden_tooltip = { religion_authority = { modifier = ruler_converted_to } }
		any_demesne_title = {
			limit = {
				tier = COUNT
				location = { religion = ismaili }
			}
			location = { religion = qarmatian }
		}
		hidden_tooltip = {
			any_courtier = {
				limit = {
					religion = ismaili
					is_ruler = no
					controls_religion = no
					mercenary = no
					holy_order = no
					can_change_religion = yes
				}
				religion = qarmatian
			}
			any_vassal = {
				limit = {
					religion = ismaili
					is_playable = no
					controls_religion = no
					mercenary = no
					holy_order = no
					can_change_religion = yes
				}
				religion = qarmatian
				any_courtier = {
					limit = {
						religion = ismaili
						is_ruler = no
						controls_religion = no
						mercenary = no
						holy_order = no
						can_change_religion = yes
					}
					religion = qarmatian
				}
			}
		}
	}
	option = {
		name = EVTOPTB_emf_heresy_1977
		trigger = {
			NOT = { character = ROOT }
			religion = ismaili
			independent = no
		}
	}
	option = {
		name = EVTOPTC_emf_heresy_1976
		trigger = {
			NOT = { religion = ismaili }
			religion_group = muslim
		}
	}
	option = {
		name = EVTOPTD_emf_heresy_1976
		trigger = {
			NOT = { religion_group = muslim }
		}
	}
}

# Ismaili vassal must decide whether to become Qarmatian
character_event = {
	id = emf_heresy.1978
	picture = GFX_evt_bishop_muslim
	border = GFX_event_normal_frame_religion
	
	desc = {
		text = EVTDESC_emf_heresy_1978_caliph
		trigger = {
			FROM = {
				religion = ismaili
				controls_religion = ismaili
			}
		}
	}
	desc = {
		text = EVTDESC_emf_heresy_1978_ismaili
		trigger = {
			FROM = {
				religion = ismaili
				NOT = { controls_religion = ismaili }
			}
		}
	}
	desc = {
		text = EVTDESC_emf_heresy_1978_qarmatian
		trigger = { FROM = { religion = qarmatian } }
	}
	
	is_triggered_only = yes
	
	trigger = {
		religion = ismaili
		is_playable = yes
	}
	
	immediate = {
		any_vassal = {
			limit = {
				is_playable = yes
				religion = ismaili
			}
			character_event = { id = emf_heresy.1978 days = 4 }
		}
	}
	
	option = {
		name = EVTOPTA_emf_heresy_1978 # Stay Ismaili
		ai_chance = {
			factor = 75
			modifier = {
				factor = 0.5
				trait = zealous
				religion_head = {
					NOR = {
						is_close_relative = ROOT
						dynasty = ROOT
						has_non_aggression_pact_with = ROOT
						is_allied_with = ROOT
					}
				}
			}
			modifier = {
				factor = 1.5
				trait = humble
			}
			modifier = {
				factor = 1.5
				trait = craven
			}
			modifier = {
				factor = 1.5
				trait = content
			}
			modifier = {
				factor = 1.5
				liege = { religion = ismaili }
			}
		}
	}
	option = {
		name = EVTOPTA_emf_heresy_1978 # Become Qarmatian
		trigger = {
			controls_religion = no
			mercenary = no
			holy_order = no
			can_change_religion = yes
		}
		ai_chance = {
			factor = 25
			modifier = {
				factor = 0
				religion_head = {
					OR = {
						is_close_relative = ROOT
						dynasty = ROOT
						has_non_aggression_pact_with = ROOT
						is_allied_with = ROOT
						trait = decadent
					}
				}
			}
			modifier = {
				factor = 0
				NOR = {
					trait = zealous
					trait = lunatic
					trait = paranoid
					trait = proud
					trait = wroth
					trait = misguided_warrior
					trait = tough_soldier
					trait = skilled_tactician
					trait = brilliant_strategist
					any_liege = { religion = qarmatian }
				}
			}
			modifier = {
				factor = 2
				any_close_relative = { religion = qarmatian }
			}
			modifier = {
				factor = 0
				trait = cynical
			}
			modifier = {
				factor = 3
				trait = lunatic
			}
			modifier = {
				factor = 1.5
				trait = wroth
			}
			modifier = {
				factor = 1.5
				trait = paranoid
			}
			modifier = {
				factor = 1.5
				trait = proud
			}
			modifier = {
				factor = 1.5
				trait = brave
			}
			modifier = {
				factor = 2
				independent = yes
				real_tier = KING
			}
			modifier = {
				factor = 3
				independent = yes
				real_tier = EMPEROR
			}
			modifier = {
				factor = 3
				liege = { religion = qarmatian }
			}
		}
		hidden_tooltip = { religion_authority = { modifier = ruler_converted_from } }
		religion = qarmatian
		hidden_tooltip = { religion_authority = { modifier = ruler_converted_to } }
		any_demesne_title = {
			limit = {
				tier = COUNT
				location = { religion = ismaili }
			}
			location = { religion = qarmatian }
		}
		hidden_tooltip = {
			any_courtier = {
				limit = {
					religion = ismaili
					is_ruler = no
					controls_religion = no
					mercenary = no
					holy_order = no
					can_change_religion = yes
				}
				religion = qarmatian
			}
			any_vassal = {
				limit = {
					religion = ismaili
					is_playable = no
					controls_religion = no
					mercenary = no
					holy_order = no
					can_change_religion = yes
				}
				religion = qarmatian
				any_courtier = {
					limit = {
						religion = ismaili
						is_ruler = no
						controls_religion = no
						mercenary = no
						holy_order = no
						can_change_religion = yes
					}
					religion = qarmatian
				}
			}
		}
	}
}		


#######################################
# PAULICIAN EVENTS
#######################################

# Paulician Patriarch appears
narrative_event = {
	id = emf_heresy.2107
	title = EVTNAME_emf_heresy_2107
	desc = EVTDESC_emf_heresy_2107
	picture = GFX_evt_heretic
	border = GFX_event_narrative_frame_religion

	major = yes
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_emf_heresy_2107
		trigger = { religion = paulician }
	}
	option = {
		name = EVTOPTB_emf_heresy_2107
		trigger = {
			religion_group = christian
			NOT = { religion = paulician }
		}
	}
	option = {
		name = EVTOPTC_emf_heresy_2107
		trigger = {
			NOT = { religion_group = christian }
		}
	}
}

