###########################################
#                                         #
# Charlemagne Religious Events            #
#                                         #
# ID CM.4000-CM.4999                      #
#                                         #
###########################################

# Written by Tobias Bodlund

namespace = CM

###########################################
# Knights of the Sun                      #
###########################################


# The birth of the Knights of the Sun
narrative_event = {
	id = CM.4000
	title = EVTTITLE_CM_4000
	desc = EVTDESC_CM_4000
	picture = GFX_evt_sun_temple
	border = GFX_event_narrative_frame_religion

	major = yes
	hide_from = yes
	
	only_rulers = yes
	
	trigger = {
		has_dlc = "Charlemagne"
		has_global_flag = zun_reformation
		NOT = {
			is_title_active = d_zun_warriors
		}
		religion = catholic
		controls_religion = yes # Let it happen for the Pope - he's always around
	}
	
	mean_time_to_happen = {
		months = 36
	}
	
	immediate = {
		activate_title = { title = d_zun_warriors status = yes }
		create_character = {
			random_traits = no
			dynasty = random
			religion = zun_pagan_reformed
			culture = afghan
			age = 28
		}
		new_character = {
			wealth = 500
			d_zun_warriors = {
				grant_title = PREV
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 16
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 18
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 18
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 20
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 25
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 26
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 28
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				female = yes
				age = 16
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = zun_pagan_reformed
				culture = afghan
				age = 17
			}
		}
	}
	
	option = {
		name = EVTOPTA_CM_4000
		trigger = {
			OR = {
				religion = zun_pagan_reformed
				religion = zun_pagan
			}
		}
	}
	option = {
		name = EVTOPTB_SOA_2009
		trigger = {
			NOT = { religion = zun_pagan_reformed }
			NOT = { religion = zun_pagan }
		}
	}
}

# The son of a ruler volunteers to join the Knights of the Sun
character_event = {
	id = CM.4001

	hide_window = yes
	
	only_men = yes
	min_age = 16
	max_age = 45
	only_capable = yes
	prisoner = no
	
	trigger = {
		is_ruler = no
		religion = zun_pagan_reformed
		
		martial = 5
		
		is_title_active = d_zun_warriors
		
		OR = {
			father_even_if_dead = {
				primary_title = { higher_tier_than = BARON }
			}
			mother_even_if_dead = {
				primary_title = { higher_tier_than = BARON }
			}
		}
		
		liege = {
			holy_order = no
			liege = {
				holy_order = no
			}
		}
		
		has_dlc = "Charlemagne"
		
		NOT = { has_character_modifier = expelled_d_zun_warriors }
		NOT = {
			any_liege = {
				has_character_modifier = expelled_d_zun_warriors
			}
		}
	}
	
	mean_time_to_happen = {
		years = 100
		modifier = {
			factor = 0.25
			trait = zealous
		}
		modifier = {
			factor = 0.5
			trait = celibate
		}
		modifier = {
			factor = 0.5
			trait = homosexual
		}
		modifier = {
			factor = 0.7
			trait = content
		}
		modifier = {
			factor = 0.75
			trait = chaste
		}
		modifier = {
			factor = 0.75
			NOT = { age = 25 }
		}
		modifier = {
			factor = 1.5
			trait = slothful
		}
		modifier = {
			factor = 2.0
			trait = lustful
		}
		modifier = {
			factor = 2.0
			trait = cynical
		}
		modifier = {
			factor = 2.0
			trait = hedonist
		}
		modifier = {
			factor = 2.0
			trait = ambitious
		}
		modifier = {
			factor = 2.0
			any_heir_title = {
				always = yes
			}
		}
		modifier = {
			factor = 2.0
			is_primary_heir = yes
		}
		modifier = {
			factor = 2
			any_spouse = {
				reverse_opinion = { who = ROOT value = 100 }
			}
		}
	}
	
	option = {
		name = OK # Ask to go
		ai_chance = {
			factor = 100
		}
		liege = {
			character_event = {
				id = CM.4002
			}
		}
	}
	option = {
		name = OK # It's a bad idea
	}
}

# Son or brother asks to join the Knights of the Sun
character_event = {
	id = CM.4002
	desc = EVTDESC_CM_4002
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_SOA_1031 # Agree
		ai_chance = {
			factor = 100
		}
		
		piety = 100
		
		FROM = {
			character_event = {
				id = CM.4003
				tooltip = EVTTOOLTIP_CM_4003
			}
		}
	}
	
	option = {
		name = EVTOPTB_SOA_1031 # Refuse
		
		piety = -100
		
		FROM = {
			character_event = {
				id = CM.4004
				tooltip = EVTTOOLTIP_SOA_1033
			}
		}
	}
}

# Son or brother joins the Knights of the Sun
character_event = {
	id = CM.4003

	hide_window = yes

	is_triggered_only = yes
	
	immediate = {
		d_zun_warriors = {
			holder_scope = {
				ROOT = {
					move_character = PREV
					hidden_tooltip = {
						clear_wealth = PREV
					}
					any_spouse = {
						remove_spouse = spouse
						hidden_tooltip = { character_event = { id = SoA.2863 } }
					}
					if = {
						limit = { trait = in_hiding	}
						remove_trait = in_hiding
						clr_character_flag = do_not_disturb
						add_character_modifier = {
							name = went_out_of_hiding_timer
							duration = 180
							hidden = yes
						}
						hidden_tooltip = { character_event = { id = CM.6400 } } # Notify plotters and family
					}
				}
			}
		}
	}
}

# Liege refuses request to join the Knights of the Sun
character_event = {
	id = CM.4004

	hide_window = yes

	is_triggered_only = yes
	
	immediate = {
		opinion = {
			who = FROM
			modifier = opinion_refused_request_holy_order
		}
	}
}

# Ping event when donating money to the Knights of the Sun
character_event = {
	id = CM.4005
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		wealth = 300
		FROM = {
			letter_event = {
				id = CM.4006
			}
		}
		opinion = {
			who = FROM
			modifier = opinion_generous_donation
		}
	}
}

# Notification event when donating money to the Knights of the Sun
letter_event = {
	id = CM.4006
	desc = EVTDESC_CM_4006
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EXCELLENT
	}
}

# The Grand Master humbly requests the right to build a castle in your demesne
character_event = {
	id = CM.4007
	
	hide_window = yes
	
	only_playable = yes
	
	trigger = {
		religion = zun_pagan_reformed
		
		d_zun_warriors = {
			holder_scope = {
				wealth = 300
				NOT = {
					realm_size = 10
				}
			}
		}
		
		any_demesne_province = {
			has_empty_holding = yes
			any_province_holding = {
				NOT = { holding_type = nomad }
			}
		}
		
		has_dlc = "Charlemagne"
		
		NOT = { has_character_modifier = expelled_d_zun_warriors }
		NOT = {
			any_liege = {
				has_character_modifier = expelled_d_zun_warriors
			}
		}
	}
	
	mean_time_to_happen = {
		months = 240
		modifier = {
			factor = 0.5
			d_zun_warriors = {
				holder_scope = {
					NOT = { demesne_size = 1 }
				}
			}
		}
		modifier = {
			factor = 0.5
			d_zun_warriors = {
				holder_scope = {
					NOT = { demesne_size = 2 }
				}
			}
		}
		modifier = {
			factor = 2.0
			d_zun_warriors = {
				holder_scope = {
					num_of_count_titles = 1
				}
			}
		}
		modifier = {
			factor = 2.0
			d_zun_warriors = {
				holder_scope = {
					realm_size = 5
				}
			}
		}
	}
	
	immediate = {
		random_demesne_province = {
			limit = {
				has_empty_holding = yes
				any_province_holding = {
					NOT = { holding_type = nomad }
				}
			}
			province_event = {
				id = CM.4008
			}
		}
	}
}

province_event = {
	id = CM.4008
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		d_zun_warriors = {
			holder_scope = {
				character_event = {
					id = CM.4009
				}
			}
		}
	}
}

character_event = {
	id = CM.4009
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			character_event = {
				id = CM.4010
			}
		}
	}
}

letter_event = {
	id = CM.4010
	desc = EVTDESC_CM_4010
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_SOA_1023 # Agree
		ai_chance = {
			factor = 80
			modifier = {
				factor = 0
				has_character_modifier = expelled_d_zun_warriors
			}
		}
		
		hidden_tooltip = {
			FROMFROM = {
				province_event = { id = CM.4011 }
			}
			remove_character_modifier = expelled_d_zun_warriors
		}
		
		piety = 200
		wealth = 100
	}
	
	option = {
		name = EVTOPTB_SOA_1153 # Refuse
		ai_chance = {
			factor = 20
		}
		
		piety = -100
	}
}

# Castle has been erected
province_event = {
	id = CM.4011
	desc = EVTDESC_CM_4011
	picture = GFX_evt_castle_construction
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			wealth = -300
		}
	}
	
	option = {
		name = EVTOPTA_SOA_1024
		build_holding = {
		   type = castle
		   holder = FROMFROM
		}
	}
}

# Ping event when expelling the Knights of the Sun
character_event = {
	id = CM.4012
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		any_title_under = {
			limit = {
				tier = BARON
				location = {
					owner = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
			}
			usurp_title = FROM
		}
	
		FROM = {
			letter_event = {
				id = CM.4013
			}
		}
		
		opinion = {
			who = FROM
			modifier = opinion_expelled_from_realm
		}
	}
}

# Notification event when expelling the Knights of the Sun
letter_event = {
	id = CM.4013
	desc = EVTDESC_CM_4013
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4013
	}
}

###########################################
# Festival of the Sun  CM.4100-CM.4199    #
###########################################

# Invite vassals to festival
character_event = {
	id = CM.4100
	desc = EVTDESC_CM_4100
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	option = {
		name = EVTOPTA_CM_4100
		any_vassal = {
			limit = {
				prisoner = no
				war = no
				NOT = { trait = incapable }
				age = 16
				NOT = { has_character_flag = do_not_disturb }
			}
			letter_event = {
				id = CM.4101
				tooltip = EVTTOOLTIP1_RoI_1000
			}
		}
		hidden_tooltip = { character_event = { id = CM.4120 days = 12 } }
	}
}

# Vassal receives invitation
letter_event = {
	id = CM.4101
	desc = EVTDESC_CM_4101
	picture = GFX_evt_emissary
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4101 #Travel to the feast
		ai_chance = {
			factor = 2
			modifier = {
				factor = 10
				has_focus = focus_theology
				OR = {
					religion = zun_pagan
					religion = zun_pagan_reformed
				}
			}
			modifier  = {
				factor = 0.1
				trait = in_hiding
			}
			modifier  = {
				factor = 2
				opinion = { who = from value = -30 }
			}
			modifier  = {
				factor = 5
				opinion = { who = from value = -15 }
			}
			modifier  = {
				factor = 5
				opinion = { who = from value = 0 }
			}
		}
		set_character_flag = do_not_disturb
		hidden_tooltip = { character_event = { id = CM.4105 days = 12 } } # Hidden - arrives at feast
		hidden_tooltip = { character_event = { id = CM.4199 days = 42 } } # Safety event for clearing all flags
		if = {
			limit = { trait = in_hiding	}
			remove_trait = in_hiding
			add_character_modifier = {
				name = went_out_of_hiding_timer
				duration = 180
				hidden = yes
			}
			hidden_tooltip = { character_event = { id = CM.6400 } } # Notify plotters and family
		}
	}
	option = {
		name = EVTOPTB_RoI_1001 #Refuse invitation
		ai_chance = {
			factor = 1
			modifier = {
				factor = 0
				opinion = { who = FROM value = 25 }
				NOT = { trait = in_hiding }
			}
			modifier  = {
				factor = 5
				NOT = {
					OR = {
						religion = zun_pagan
						religion = zun_pagan_reformed
					}
				}
			}
		}
		FROM = {
			letter_event = {
				id = CM.4102
				days = 3
				tooltip = EVTTOOLTIP1_RoI_2001
			}
		}
	}
}

# If vassal refuses to come to the feast
letter_event = {
	id = CM.4102
	desc = EVTDESC_CM_4102
	picture = GFX_evt_emissary
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_SoA_502 
		opinion = {
			modifier = opinion_dislike
			who = FROM
			years = 2
		}
	}
}

# Festival begins
character_event = {
	id = CM.4120
	desc = EVTDESC_CM_4120
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4120
		set_character_flag = zun_feast_begun
		hidden_tooltip = { character_event = { id = CM.4195 days = 30 } } # End festival
		hidden_tooltip = { character_event = { id = CM.4199 days = 31 } } # Clearing all flags and modifiers after feast
		hidden_tooltip = {
			narrative_event = { id = CM.4130 days = 10 } # Walking in the sun
			character_event = { id = CM.4190 days = 20 } # Temple ceremony
		}
		any_courtier = {
			limit = {
				is_adult = yes
				is_ruler = no
				NOT = { trait = incapable }
				OR = {
					religion = zun_pagan
					religion = zun_pagan_reformed
				}
			}
			set_character_flag = attending_zun_feast
			narrative_event = { id = CM.4130 days = 10 } # Walking in the sun
			hidden_tooltip = { character_event = { id = CM.4199 days = 31 } } # Clearing all flags and modifiers after feast
		}
	}
}

# Vassal arrives at festival
character_event = {
	id = CM.4105
	desc = EVTDESC_CM_4120
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4120
		hidden_tooltip = {
			if = {
				limit = {
					OR = {
						religion = zun_pagan
						religion = zun_pagan_reformed
					}
				}
				narrative_event = { id = CM.4130 days = 10 } # Walking in the sun
			}
			character_event = { id = CM.4190 days = 20 } # Temple ceremony
		}
	}
}

# Festival ends
character_event = {
	id = CM.4195

	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		character_event = { id = CM.4196 }
		any_vassal = {
			limit = {
				has_character_flag = attending_zun_feast
				ROOT = { is_liege_of = THIS }
			}
			character_event = { id = CM.4198 }
		}
	}
}

# Festival is over - host
character_event = {
	id = CM.4196
	desc = EVTDESC_CM_4196
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4196
		add_character_modifier = {
			name = large_feast_timer
			duration = 365
			hidden = yes
		}
		remove_character_modifier = holding_zun_feast
		clr_character_flag = zun_feast_begun
		clr_character_flag = do_not_disturb
	}
}

# Festival is over - guest
character_event = {
	id = CM.4198
	desc = EVTDESC_CM_4196
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4196
		add_character_modifier = {
			name = large_feast_timer
			duration = 365
			hidden = yes
		}
		opinion = {
			modifier = opinion_enjoyed_feast
			who = FROM
			years = 2
		}
		clr_character_flag = attending_zun_feast
		clr_character_flag = do_not_disturb
	}
}

# Clear all character flags and modifiers - safety catch
character_event = {
	id = CM.4199

	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		remove_character_modifier = holding_zun_feast
		clr_character_flag = attending_zun_feast
		clr_character_flag = zun_feast_begun
		clr_character_flag = do_not_disturb
	}
}

# Walking in the sun
narrative_event = {
	id = CM.4130
	title = EVTTITLE_CM_4130
	desc = EVTDESC_CM_4130
	picture = GFX_evt_sun_temple
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes
	
	capable_only = yes
	
	option = {
		name = EVTOPTA_CM_4130
		piety = 30
		hidden_tooltip = {
			random_list = {
				5 = { character_event = { id = CM.4131 } } # Weak or old character becomes ill
				5 = { character_event = { id = CM.4133 } } # Character goes insane
				5 = { character_event = { id = CM.4135 } } # Character becomes possessed
				10 = { character_event = { id = CM.4137 } } # Character becomes zealous
				10 = { character_event = { id = CM.4139 } } # Character becomes stronger
				10 = { character_event = { id = CM.4141 } } # Character becomes wiser
				5 = { character_event = { id = CM.4143 } } # Zealous character stares into sun and goes blind
				300 = {} # No event
			}
		}
	}
}

# Weak or old character becomes ill or incapable
character_event = {
	id = CM.4131
	desc = EVTDESC_CM_4131
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		OR = {
			age = 70
			health_traits = 1
		}
	}
	
	option = {
		name = EVTOPTA_CM_4131
		if = {
			limit = {
				OR = {
					trait = ill
					trait = pneumonic
					trait = infirm
					trait = has_tuberculosis
					trait = has_typhoid_fever
					trait = has_typhus
					trait = has_bubonic_plague
					trait = has_measles
					trait = has_small_pox
					trait = syphilitic
					trait = leper
				}
			}
			add_trait = incapable
		}
		if = {
			limit = {
				NOT = { trait = ill }
				NOT = { trait = pneumonic }
				NOT = { trait = infirm }
				NOT = { trait = has_tuberculosis }
				NOT = { trait = has_typhoid_fever }
				NOT = { trait = has_typhus }
				NOT = { trait = has_bubonic_plague }
				NOT = { trait = has_measles }
				NOT = { trait = has_small_pox }
				NOT = { trait = syphilitic }
				NOT = { trait = leper }
			}
			add_trait = ill
		}
	}
}

# Character goes insane
character_event = {
	id = CM.4133
	desc = EVTDESC_CM_4133
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		NOT = { trait = lunatic }
		OR = {
			trait = stressed
			trait = depressed
			AND = {
				health_traits = 1
				trait = zealous
			}
		}
	}
	
	option = {
		name = EVTOPTA_CM_4133
		add_trait = lunatic
	}
}

# Character becomes possessed
character_event = {
	id = CM.4135
	desc = EVTDESC_CM_4135
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		NOT = { trait = possessed }
		trait = zealous
	}
	
	option = {
		name = EVTOPTA_CM_4135
		add_trait = possessed
	}
}

# Character becomes zealous
character_event = {
	id = CM.4137
	desc = EVTDESC_CM_4137
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		NOT = { trait = zealous }
		NOT = { trait = cynical }
	}
	
	option = {
		name = EVTOPTA_CM_4137
		add_trait = zealous
	}
}

# Character becomes stronger
character_event = {
	id = CM.4139
	desc = EVTDESC_CM_4139
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		NOT = { trait = strong }
		NOT = { trait = weak }
		NOT = { trait = slothful }
		NOT = { health_traits = 1 }
	}
	
	option = {
		name = EVTOPTA_CM_4137
		add_trait = strong
	}
}

# Character becomes wiser
character_event = {
	id = CM.4141
	desc = EVTDESC_CM_4141
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		NOT = {
			trait = quick
			trait = genius
		}
	}
	
	option = {
		name = EVTOPTA_CM_4141
		if = {
			limit = { trait = quick }
			add_trait = genius
		}
		if = {
			limit = { NOT = { trait = quick } }
			add_trait = quick
		}
	}
}

# Character stares into sun and becomes blind
character_event = {
	id = CM.4143
	desc = EVTDESC_CM_4143
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		trait = zealous
		NOT = { trait = blinded }
	}
	
	option = {
		name = EVTOPTA_CM_4143
		add_trait = blinded
	}
}


# Temple ceremony - host and guests
character_event = {
	id = CM.4190
	desc = EVTDESC_CM_4190
	picture = GFX_evt_sun_temple
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4190
		piety = 20
		add_character_modifier = {
			name = large_feast_timer
			duration = 365
			hidden = yes
		}
	}
}

###########################################
# Byzantine religious events              #
###########################################

# Byzantine Emperor renounces iconoclasm
character_event = {
	id = CM.4200
	desc = EVTDESC_CM_4200
	picture = GFX_evt_icon
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4200
		religion = FROM
		ai_chance = {
			factor = 1
			modifier = {
				factor = 0.3
				trait = zealous
				intrigue = 3
			}
			modifier = {
				factor = 0
				trait = zealous
				NOT = { intrigue = 3 }
			}
		}
		if = {
			limit = { trait = zealous }
			remove_trait = zealous
			piety = -50
		}
	}
	option = {
		name = EVTOPTB_CM_4200
		piety = 100
		ai_chance = {
			factor = 1
			modifier = {
				factor = 0
				NOT = { trait = zealous }
			}
		}
	}
}

##################################
# The Haruriyyah CM.4300-CM.4399 #
##################################

# The Grand Master demands one of your sons due to debt
character_event = {
	id = CM.4300
	
	hide_window = yes
	
	only_playable = yes
	
	trigger = {
		OR = {
			religion = ibadi
			religion = kharijite
		}
		is_heretic = no
		has_character_modifier = borrowed_from_haruriyyah
		NOT = { has_character_flag = sent_son_to_haruriyyah }
		any_child = {
			is_adult = yes
			is_ruler = no
			prisoner = no
			NOT = { trait = incapable }
			ROOT = {
				NOT = { 
					current_heir = { 
						character = PREVPREV 
					}
				}
			}
		}
		has_dlc = "Charlemagne"
	}
	
	mean_time_to_happen = {
		months = 120
	}
	
	immediate = {
		random_child = {
			limit = {
				is_adult = yes
				is_ruler = no
				prisoner = no
				NOT = { trait = incapable }
				ROOT = {
					NOT = { 
						current_heir = { 
							character = PREVPREV 
						}
					}
				}
			}
			character_event = {
				id = CM.4301
			}
		}
	}
}

character_event = {
	id = CM.4301

	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		d_haruriyyah = {
			holder_scope = {
				character_event = {
					id = CM.4302
				}
			}
		}
	}
}

character_event = {
	id = CM.4302
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			character_event = {
				id = CM.4303
			}
		}
	}
}

letter_event = {
	id = CM.4303
	desc = EVTDESC_CM_4303
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_SOA_1003 # Agree
		ai_chance = {
			factor = 100
		}
		
		set_character_flag = sent_son_to_haruriyyah
		
		piety = 100
		
		d_haruriyyah = {
			holder_scope = {
				FROMFROM = {
					move_character = PREV
					hidden_tooltip = {
						clear_wealth = PREV
					}
				}
			}
		}
	}
	
	option = {
		name = EVTOPTB_SOA_1003 # Pay them back
		wealth = -300
		hidden_tooltip = {
			remove_character_modifier = borrowed_from_haruriyyah
		}
		FROM = {
			wealth = 300
		}
	}
	
	option = {
		name = EVTOPTC_SOA_1003 # Refuse
		piety = -1000
	}
}


# Birth of the Haruriyyah Order
narrative_event = {
	id = CM.4309
	title = EVTTITLE_CM_4309
	desc = EVTDESC_CM_4309
	picture = GFX_evt_moors
	border = GFX_event_narrative_frame_religion
	
	major = yes
	hide_from = yes
	
	only_playable = yes
	only_men = yes
	
	trigger = {
		has_dlc = "Charlemagne"
			OR = {
				religion = ibadi
				religion = kharijite
			}
			is_heretic = no
		NOT = {
			is_title_active = d_haruriyyah
		}
		
		has_global_flag = muslim_jihads_unlocked
		
		OR = {
			869 = { # Hajar
				NOT = { owner = { religion_group = muslim } }
			}
			728 = { # Damascus
				NOT = { owner = { religion_group = muslim } }
			}
			796 = { # Cairo
				NOT = { owner = { religion_group = muslim } }
			}
			774 = { # Jerusalem
				NOT = { owner = { religion_group = muslim } }
			}
			719 = { # Mecca
				NOT = { owner = { religion_group = muslim } }
			}
		}
	}
	
	mean_time_to_happen = {
		months = 6
	}
	
	immediate = {
		activate_title = { title = d_haruriyyah status = yes }
		create_character = {
			random_traits = no
			dynasty = random
			religion = ROOT
			culture = persian
			age = 25
			attributes = {
				martial = 10
				diplomacy = 9
				stewardship = 9
				intrigue = 7
				learning = 9
			}
			health = 6
			fertility = 0.5
			trait = brilliant_strategist
			trait = sayyid
			trait = hajjaj
			trait = hafiz
			trait = patient
			trait = zealous
			trait = scholar
			trait = temperate
		}
		new_character = {
			wealth = 500
			d_haruriyyah = {
				grant_title = PREV
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 16
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 18
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 18
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 20
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 25
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 26
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 28
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				female = yes
				age = 16
			}
			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = persian
				age = 17
			}
		}
	}
	
	option = {
		name = EVTOPTA_CM_4309
		trigger = {
			religion = ROOT
		}
	}
	option = {
		name = EVTOPTB_CM_4309
		trigger = {
			NOT = { religion = ROOT }
		}
	}
}

# Ping event when borrowing money from the haruriyyah
character_event = {
	id = CM.4310

	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		wealth = -300
		FROM = {
			letter_event = {
				id = CM.4311
			}
		}
	}
}

# Notification event when borrowing money from the haruriyyah
letter_event = {
	id = CM.4311
	desc = EVTDESC_CM_4311
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EXCELLENT
	}
}


# The Grand Master demands the right to build a castle in your demesne
character_event = {
	id = CM.4320
	
	hide_window = yes
	
	only_playable = yes
	
	trigger = {
		OR = {
			religion = ibadi
			religion = kharijite
		}
		is_heretic = no
		
		has_character_modifier = borrowed_from_haruriyyah
		NOT = { has_character_flag = sent_son_to_haruriyyah }
		
		any_demesne_province = {
			has_empty_holding = yes
			any_province_holding = {
				NOT = { holding_type = nomad }
			}
		}
		
		d_haruriyyah = {
			holder_scope = {
				wealth = 300
			}
		}
		has_dlc = "Charlemagne"
		
		NOT = { has_character_modifier = expelled_d_haruriyyah }
		NOT = {
			any_liege = {
				has_character_modifier = expelled_d_haruriyyah
			}
		}
	}
	
	mean_time_to_happen = {
		months = 60
	}
	
	immediate = {
		random_demesne_province = {
			limit = {
				has_empty_holding = yes
				any_province_holding = {
					NOT = { holding_type = nomad }
				}
			}
			province_event = {
				id = CM.4321
			}
		}
	}
}

province_event = {
	id = CM.4321
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		d_haruriyyah = {
			holder_scope = {
				character_event = {
					id = CM.4322
				}
			}
		}
	}
	
	option = {
		name = OK
	}
}

character_event = {
	id = CM.4322
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			character_event = {
				id = CM.4323
			}
		}
	}
}

letter_event = {
	id = CM.4323
	desc = EVTDESC_CM_4323
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_SOA_1023 # Agree
		ai_chance = {
			factor = 100
		}
		
		hidden_tooltip = {
			remove_character_modifier = borrowed_from_haruriyyah
			FROMFROM = {
				province_event = { id = CM.4324 }
			}
		}
		
		piety = 100
	}
	
	option = {
		name = EVTOPTB_SOA_1003 # Pay them back
		wealth = -300
		hidden_tooltip = {
			remove_character_modifier = borrowed_from_haruriyyah
		}
		FROM = {
			wealth = 300
		}
	}
	
	option = {
		name = EVTOPTC_SOA_1003 # Refuse
		piety = -1000
	}
}

# Castle has been erected
province_event = {
	id = CM.4324
	desc = EVTDESC_CM_4324
	picture = GFX_evt_castle_construction
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			wealth = -300
		}
	}
	
	option = {
		name = EVTOPTA_SOA_1024
		build_holding = {
		   type = castle
		   holder = FROMFROM
		}
	}
}

# The son of a ruler volunteers to join the haruriyyah Order
character_event = {
	id = CM.4330
	
	hide_window = yes
	
	only_men = yes
	min_age = 16
	max_age = 45
	only_capable = yes
	prisoner = no
	
	trigger = {
		is_ruler = no
		OR = {
			religion = ibadi
			religion = kharijite
		}
		is_heretic = no
		
		martial = 5
		
		is_title_active = d_haruriyyah
		
		OR = {
			father_even_if_dead = {
				primary_title = { higher_tier_than = BARON }
			}
			mother_even_if_dead = {
				primary_title = { higher_tier_than = BARON }
			}
		}
		
		liege = {
			holy_order = no
			liege = {
				holy_order = no
			}
		}
		
		has_dlc = "Charlemagne"
		
		NOT = { has_character_modifier = expelled_d_haruriyyah }
		NOT = {
			any_liege = {
				has_character_modifier = expelled_d_haruriyyah
			}
		}
	}
	
	mean_time_to_happen = {
		years = 100
		modifier = {
			factor = 0.25
			trait = zealous
		}
		modifier = {
			factor = 0.5
			trait = celibate
		}
		modifier = {
			factor = 0.5
			trait = homosexual
		}
		modifier = {
			factor = 0.7
			trait = content
		}
		modifier = {
			factor = 0.75
			trait = chaste
		}
		modifier = {
			factor = 0.75
			NOT = { age = 25 }
		}
		modifier = {
			factor = 1.5
			trait = slothful
		}
		modifier = {
			factor = 2.0
			trait = lustful
		}
		modifier = {
			factor = 2.0
			trait = cynical
		}
		modifier = {
			factor = 2.0
			trait = hedonist
		}
		modifier = {
			factor = 2.0
			trait = ambitious
		}
		modifier = {
			factor = 2.0
			any_heir_title = {
				always = yes
			}
		}
		modifier = {
			factor = 2.0
			is_primary_heir = yes
		}
		modifier = {
			factor = 2
			any_spouse = {
				reverse_opinion = { who = ROOT value = 100 }
			}
		}
	}
	
	option = {
		name = EVTOPTA_SOA_1030 # Ask to go
		ai_chance = {
			factor = 100
		}
		liege = {
			character_event = {
				id = CM.4331
			}
		}
	}
	option = {
		name = EVTOPTB_SOA_1030 # It's a bad idea
	}
}

# Son or brother asks to join the haruriyyah
character_event = {
	id = CM.4331
	desc = EVTDESC_CM_4331
	picture = GFX_evt_moors
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_SOA_1031 # Agree
		ai_chance = {
			factor = 100
		}
		
		piety = 100
		
		FROM = {
			character_event = {
				id = CM.4332
				tooltip = EVTTOOLTIP_CM_4332
			}
		}
	}
	
	option = {
		name = EVTOPTB_SOA_1031 # Refuse
		
		piety = -100
		
		FROM = {
			character_event = {
				id = CM.4333
				tooltip = EVTTOOLTIP_SOA_1033
			}
		}
	}
}

# Son or brother joins the haruriyyah Order
character_event = {
	id = CM.4332

	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		d_haruriyyah = {
			holder_scope = {
				ROOT = {
					move_character = PREV
					hidden_tooltip = {
						clear_wealth = PREV
					}
					any_spouse = {
						remove_spouse = spouse
						hidden_tooltip = { character_event = { id = SoA.2863 } }
					}
					if = {
						limit = { trait = in_hiding	}
						remove_trait = in_hiding
						clr_character_flag = do_not_disturb
						add_character_modifier = {
							name = went_out_of_hiding_timer
							duration = 180
							hidden = yes
						}
						hidden_tooltip = { character_event = { id = CM.6400 } } # Notify plotters and family
					}
				}
			}
		}
	}
}

# Liege refuses request to join the haruriyyah
character_event = {
	id = CM.4333

	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		opinion = {
			who = FROM
			modifier = opinion_refused_request_holy_order
		}
	}
}


# The Grand Master requests the right to build a castle in your demesne
character_event = {
	id = CM.4350
	
	hide_window = yes
	
	only_playable = yes
	
	trigger = {
		OR = {
			religion = ibadi
			religion = kharijite
		}
		
		d_haruriyyah = {
			holder_scope = {
				wealth = 300
				NOT = {
					realm_size = 10
				}
				NOT = {
					num_of_count_titles = 1
				}
			}
		}
		
		any_demesne_province = {
			has_empty_holding = yes
			port = yes
			any_province_holding = {
				NOT = { holding_type = nomad }
			}
		}
		
		has_dlc = "Charlemagne"
		
		NOT = { has_character_modifier = expelled_d_haruriyyah }
		NOT = {
			any_liege = {
				has_character_modifier = expelled_d_haruriyyah
			}
		}
	}
	
	mean_time_to_happen = {
		months = 240
		modifier = {
			factor = 0.5
			d_haruriyyah = {
				holder_scope = {
					NOT = { demesne_size = 1 }
				}
			}
		}
		modifier = {
			factor = 0.5
			d_haruriyyah = {
				holder_scope = {
					NOT = { demesne_size = 2 }
				}
			}
		}
		modifier = {
			factor = 2.0
			d_haruriyyah = {
				holder_scope = {
					realm_size = 5
				}
			}
		}
	}
	
	immediate = {
		random_demesne_province = {
			limit = {
				has_empty_holding = yes
				port = yes
				any_province_holding = {
					NOT = { holding_type = nomad }
				}
			}
			province_event = {
				id = CM.4351
			}
		}
	}
}

province_event = {
	id = CM.4351
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		d_haruriyyah = {
			holder_scope = {
				character_event = {
					id = CM.4352
				}
			}
		}
	}
}

character_event = {
	id = CM.4352
	
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			character_event = {
				id = CM.4353
			}
		}
	}
}

letter_event = {
	id = CM.4353
	desc = EVTDESC_CM_4353
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_SOA_1023 # Agree
		ai_chance = {
			factor = 100
			modifier = {
				factor = 0
				has_character_modifier = expelled_d_haruriyyah
			}
		}
		
		hidden_tooltip = {
			FROMFROM = {
				province_event = { id = CM.4354 }
			}
			remove_character_modifier = expelled_d_haruriyyah
		}
		
		piety = 200
		wealth = 100
	}
	
	option = {
		name = EVTOPTB_SOA_1031 # Refuse
		ai_chance = {
			factor = 20
		}
		
		piety = -100
	}
}

# Castle has been erected
province_event = {
	id = CM.4354
	desc = EVTDESC_CM_4324
	picture = GFX_evt_moors
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			wealth = -300
		}
	}
	
	option = {
		name = EVTOPTA_SOA_1024
		build_holding = {
		   type = castle
		   holder = FROMFROM
		}
	}
}

# Notification event when donating money to the Muslim orders
letter_event = {
	id = CM.4399
	desc = EVTDESC_CM_4399
	border = GFX_event_letter_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_CM_4399
	}
}

