targeted_decisions = {
	declare_as_kafir = {
		filter = sub_realm
		ai_target_filter = sub_realm
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			religion = kharijite
			is_ruler = yes
			has_regent = no
			NOT = {
				trait = kharijite_kafir
				trait = decadent
			}
			
		}
		potential = { 
			religion = kharijite
			trait = decadent
			NOT = { trait = kharijite_kafir }
			is_liege_or_above = FROM
		}
		allow = {
			FROM = { piety = 50 }
		}
		effect = {
			tooltip = { add_trait = kharijite_kafir }
			hidden_tooltip = { letter_event = { id = Plus.1915 } }
			FROM = {
				piety = -50
				opinion = {
					modifier = opinion_takfir
					who = ROOT
					months = 240
				}
			}	
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				OR = {
					reverse_opinion = { who = FROM value = 0 }
					NOT = {
						dynasty = FROM
						is_close_relative = FROM
						vassal_of = FROM
					}
				}
			}
			modifier = {
				factor = 0
				FROM = {
					OR = {
						trait = drunkard
						trait = hedonist
						trait = cynical
					}
				}
			}
			modifier = {
				factor = 0.5
				FROM = { trait = greedy }
			}
			modifier = {
				factor = 0.5
				FROM = { trait = slothful }
			}
			modifier = {
				factor = 0.5
				FROM = { trait = gluttonous }
			}
			modifier = {
				factor = 0.5
				FROM = { trait = arbitrary }
			}
			modifier = {
				factor = 2.0
				FROM = { trait = just }
			}
			modifier = {
				factor = 2.0
				FROM = { trait = diligent }
			}
			modifier = {
				factor = 4.0
				FROM = { trait = zealous }
			}
		}
	}

	accuse_as_kafir = {
		filter = realm
		ai_target_filter = realm
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			religion = kharijite
			is_adult = yes
			prisoner = no
			OR = {
				is_ruler = yes
				is_priest = yes
			}
			NOT = {
				trait = kharijite_kafir
				trait = decadent
				trait = incapable
			}
		}
		potential = {
			is_liege_of = FROM
			is_adult = yes
			prisoner = no
			NOR = {
				trait = kharijite_kafir
				trait = incapable
			}
			OR = {
				trait = decadent
				trait = drunkard
				trait = hedonist
				AND = {
					OR = {
						trait = greedy
						trait = slothful
						trait = cynical
						trait = gluttonous
						trait = arbitrary
					}
					NOT = { piety = 0 }
				}
			}
		}
		allow = {
			FROM = { piety = 100 } 
		}
		effect = {
			FROM = { piety = -100 }
			tooltip = { add_trait = kharijite_kafir }
			hidden_tooltip = { letter_event = { id = Plus.1916 } }
			primary_title = { add_claim = FROM }
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				OR = {
					current_heir = { character = FROM }
					reverse_opinion = { who = FROM value = 0 }
					NOT = {
						dynasty = FROM
						is_close_relative = FROM
						vassal_of = FROM
					}
				}
			}
			modifier = {
				factor = 0
				FROM = {
					OR = {
						trait = drunkard
						trait = hedonist
						trait = cynical
					}
				}
			}
			modifier = {
				factor = 0.5
				FROM = { trait = greedy }
			}
			modifier = {
				factor = 0.5
				FROM = { trait = slothful }
			}
			modifier = {
				factor = 0.5
				FROM = { trait = gluttonous }
			}
			modifier = {
				factor = 0.5
				FROM = { trait = arbitrary }
			}
			modifier = {
				factor = 2.0
				FROM = { trait = just }
			}
			modifier = {
				factor = 2.0
				FROM = { trait = diligent }
			}
			modifier = {
				factor = 4.0
				FROM = { trait = zealous }
			}
		}
	}
	
	public_execution = {
		filter = court
		ai_target_filter = court
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			religion = kharijite
			is_ruler = yes
			has_regent = no
		}
		potential = {
			host = { character = FROM }
			is_adult = yes
			prisoner = yes
			NOT = { trait = incapable }
			OR = {
				NOT = { religion = kharijite }
				trait = kharijite_kafir
			}
		}
		allow = {
			OR = {
				NOT = { religion = kharijite }
				trait = kharijite_kafir
			}
		}
		effect = {
			FROM = {
				ROOT = { save_event_target_as = kinslayer_target }
				add_kinslayer_trait_effect = yes
				if = {
					limit = { is_married = ROOT }
					add_trait = cruel
				}
			}
			hidden_tooltip = {
				if = {
					limit = {
						NOT = { is_mother = FROM }
					}
					mother = {
						opinion = {
							who = FROM
							modifier = executed_kin
							months = 120
						}
					}
				}
				if = {
					limit = {
						NOT = { is_father = FROM }
					}
					father = {
						opinion = {
							who = FROM
							modifier = executed_kin
							months = 120
						}
					}
				}
				any_sibling = {
					limit = {
						NOT = { character = FROM }
					}
					opinion = {
						who = FROM
						modifier = executed_kin
						months = 120
					}
				}
				any_child = {
					limit = {
						NOT = { character = FROM }
					}
					opinion = {
						who = FROM
						modifier = executed_kin
						months = 120
					}
				}
			}
			hidden_tooltip = {
				FROM = { character_event = { id = Plus.1918 } }
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 10
				controls_religion = yes
			}
			modifier = {
				factor = 5
				FROM = { trait = zealous }
			}
			modifier = {
				factor = 2
				FROM = { trait = cruel }
			}
			modifier = {
				factor = 0.5
				FROM = { trait = cynical }
			}
			modifier = {
				factor = 0.25
				FROM = { trait = kind }
			}
			modifier = {
				factor = 0
				OR = {
					sibling = FROM
					is_father = FROM
					is_mother = FROM
					is_married = FROM
					is_child_of = FROM
					dynasty = FROM
				}
				FROM = {
					NOT = {
						trait = zealous
						trait = cruel
						is_kinslayer_trigger = yes
					}
				}
			}
		}
	}

	banish_takfir = {
		filter = dynasty
		ai_target_filter = dynasty
		only_playable = yes
		ai_check_interval = 3
		
		from_potential = {
			is_playable = yes
			religion_group = muslim
			OR = {
 				independent = yes
 				NOT = { any_dynasty_member = { is_vassal_or_below = FROM } }
 			}
		}
		potential = {
			dynasty = FROM
			prisoner = yes
			host = { character = FROM }
			NOT = { employer = { character = FROM } }
			reverse_has_opinion_modifier = { who = FROM modifier = opinion_takfir }
		}
		effect = {
			hidden_tooltip = {
				prisoner = no
				opinion = {
					who = FROM
					modifier = opinion_hate
					years = 100
				}
				any_dynasty_member = {
					limit = {
						is_close_relative = ROOT
						is_ruler = yes
						is_feudal = yes
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					primary_title = {
						if = {
							limit = { ROOT = { NOT = { has_strong_claim = PREV } } }
							add_claim = ROOT
						}
					}
				}
			}
			if = {
				limit = { is_ruler = yes }
				abdicate = yes
			}
			random_dynasty_member = {
				limit = {
					trait = decadent
					NOT = { is_liege_or_above = FROM }
					is_ruler = yes
				}
				ROOT = { banish = PREV }
				break = yes
			}
			top_liege = {
				random_vassal = {
					limit = {
						NOT = { dynasty = ROOT }
						NOT = { is_liege_or_above = FROM }
						religion = ROOT
						higher_tier_than = BARON
					}
					ROOT = { banish = PREV }
					break = yes
				}
			}
			top_liege = {
				random_realm_lord = {
					limit = {
						NOT = { dynasty = ROOT }
						NOT = { is_liege_or_above = FROM }
						religion = ROOT
						higher_tier_than = BARON
					}
					ROOT = { banish = PREV }
					break = yes
				}
			}
			random_independent_ruler = {
				limit = {
					NOT = { character = FROM }
					NOT = { dynasty = ROOT }
					religion = ROOT
					trait = decadent
				}
				ROOT = { banish = PREV }
				break = yes
			}
			random_independent_ruler = {
				limit = {
					NOT = { character = FROM }
					NOT = { dynasty = ROOT }
					religion = ROOT
					NOT = { distance = { who = ROOT value = 300 } }
				}
				ROOT = { banish = PREV }
				break = yes
			}
			random_independent_ruler = {
				limit = {
					NOT = { character = FROM }
					NOT = { dynasty = ROOT }
					religion = ROOT
				}
				ROOT = { banish = PREV }
				break = yes
			}
			banish = yes
		}
		ai_will_do = {
			factor = 1
		}
	}
		
}	

decisions = {
	restore_court_order = {
		is_high_prio = yes
		ai_check_interval = 3
		potential = {
			trait = court_anarchy
			has_global_flag = anarchy_at_samarra
			OR = {
				ai = no
				had_global_flag = { flag = anarchy_at_samarra days = 365 }
			}
		}
		allow = {
			war = no
			prisoner = no
			NOT = { trait = incapable }
			OR = {
				diplomacy = 12
				intrigue = 12
				martial = 12
			}
		}
		effect = {
			piety = 100
			hidden_tooltip = {
				any_courtier = {
					limit = {
						is_councillor = yes
						NOT = { has_minor_title = title_regent }
					}
					reverse_opinion = { who = ROOT modifier = opinion_traitor years = 20 }
					opinion = { who = ROOT modifier = opinion_hate years = 20 }
				}
			}
			regent = {
				hidden_tooltip = {
					save_event_target_as = imprisoned_regent
					reverse_opinion = { who = ROOT modifier = opinion_traitor years = 20 }
				}
				imprison = ROOT
				hidden_tooltip = { opinion = { who = ROOT modifier = opinion_hate years = 20 } }
			}
			remove_trait = court_anarchy
			hidden_tooltip = {
				narrative_event = { id = Plus.1902 }
			}
		}
		ai_will_do = {
			factor = 1
		}
	}

	kafir_clear_gluttonous = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = gluttonous
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 250
		}
		effect = {
			piety = -250
			character_event = { id = Plus.1920 tooltip = BEGIN_FASTING }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_greed = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = greedy
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 250
			scaled_wealth = 1.0
		}
		effect = {
			piety = -250
			scaled_wealth = -1.0
			character_event = { id = Plus.1922 tooltip = ATTEMPT_REMOVE_GREED }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_slothful = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = slothful
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 250
			NOT = { trait = stressed }
		}
		effect = {
			piety = -250
			character_event = { id = Plus.1923 tooltip = ATTEMPT_REMOVE_SLOTH }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_arbitrary = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = arbitrary
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 250
			prestige = 250
		}
		effect = {
			piety = -250
			prestige = -250
			character_event = { id = Plus.1924 tooltip = ATTEMPT_REMOVE_ARBITRARY }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_cynical = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = cynical
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 250
			prestige = 500
		}
		effect = {
			piety = -250
			prestige = -500
			character_event = { id = Plus.1925 tooltip = ATTEMPT_REMOVE_CYNICAL }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_drunkard = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = drunkard
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 250
			prestige = 500
		}
		effect = {
			piety = -250
			prestige = -500
			character_event = { id = Plus.1926 tooltip = ATTEMPT_REMOVE_DRUNKARD }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_hedonist = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = hedonist
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 250
			prestige = 500
		}
		effect = {
			piety = -250
			prestige = -500
			character_event = { id = Plus.1927 tooltip = ATTEMPT_REMOVE_HEDONIST }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_decadent = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			trait = decadent
			religion = kharijite
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 500
			prestige = 500
		}
		effect = {
			piety = -500
			prestige = -500
			character_event = { id = Plus.1928 tooltip = ATTEMPT_REMOVE_DECADENT }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	kafir_clear_name = {
		is_high_prio = yes
		icon_religion = kharijite
		ai_check_interval = 3
		
		potential = {
			trait = kharijite_kafir
			religion = kharijite
		}
		allow = {
			piety = 100
			NOT = {
				trait = decadent
				trait = hedonist
				trait = drunkard
				trait = slothful
				trait = greedy
				trait = arbitrary
				trait = cynical
				trait = gluttonous
			}
		}
		effect = {
			piety = -100
			character_event = { id = Plus.1929 tooltip = ATTEMPT_CLEAR_NAME }
		}
		ai_will_do = {
			factor = 1
		}
	}

	raise_zealot_units = {
		is_high_prio = yes
		is_mercenary = yes
		only_rulers = yes

		potential = {
			OR = {
				AND = {
					religion = kharijite
					NOT = { trait = kharijite_kafir }
				}
				AND = {
					religion = paulician
					NOT = { trait = excommunicated }
				}
			}
			is_ruler = yes
			is_tribal = no
			war = yes
		}
		allow = {
			piety = 250
			wealth = 0
			custom_tooltip = {
				text = NOT_RECENTLY_RAISE_ZEALOTS
				hidden_tooltip = {
					NOT = { has_character_modifier = raise_tribal_units_timer }
				}
			}
		}
		effect = {
			piety = -250
			custom_tooltip = {
				text = ZEALOT_ARMY_RAISED
				hidden_tooltip = {
					capital_scope = {
						ROOT = {
							spawn_unit = {
								owner = THIS
								province = PREV
								home = PREV
								match_character = THIS
								match_mult = 0.2
								match_min = 500
								match_max = 5000
								attrition = 1.0
								disband_on_peace = yes
								maintenance_multiplier = 0.5
							}
						}
					}
				}
			}
			hidden_tooltip = {
				add_character_modifier = {
					name = raise_tribal_units_timer
					duration = 30
					hidden = yes
				}
			}
		}
		ai_will_do = {
			factor = 0 # decisions with the "is_mercenary" will not be calculated from the script
		}
	}

	hurufi_study1 = {
		ai_check_interval = 12
		potential = {
            religion = hurufi
			is_adult = yes
			is_female = no
			prisoner = no
			NOR = { 
				is_dumb_trigger = yes
				trait = inbred
				trait = blinded
				trait = incapable
				trait = hurufi_mystic_a
				trait = hurufi_mystic_b
				trait = hurufi_mystic_c
				has_character_modifier = writing_epic_poem
				has_character_modifier = hurufi_studying
			}
			is_not_busy_trigger = yes
		}	
		allow = {
			war = no
			in_command_trigger = no
			is_ill = no
			piety = 100
			OR = {
				has_focus = focus_theology
				is_playable = no
			}
		}
		effect = {
			set_character_flag = hurufy_studying_1
			piety = -100
			add_character_modifier = {
				name = hurufi_studying
				duration = -1
			}
			add_character_modifier = {
				name = do_not_disturb
				days = 300
				hidden = yes
			}
			character_event = { id = Plus.1943 days = 200 random = 100 tooltip = BEGIN_HURUFI_STUDIES }
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				is_playable = no
				NOT = {
					is_priest = yes
					trait = scholar
					trait = theologian
					trait = mystic
				}
			}
			modifier = {
				factor = 10
				is_priest = yes
			}
			modifier = {
				factor = 20
				OR = {
					trait = scholar
					trait = theologian
					trait = mystic
				}
			}
			modifier = {
				factor = 0
				OR = {
					NOT = { age = 30 }
					trait = slothful
				}
			}
		}
	}

	hurufi_study2 = {
		ai_check_interval = 12
		potential = {
            religion = hurufi
			is_adult = yes
			is_female = no
			prisoner = no
			trait = hurufi_mystic_a
			NOR = { 
				is_dumb_trigger = yes
				trait = inbred
				trait = blinded
				trait = incapable
				trait = hurufi_mystic_b
				trait = hurufi_mystic_c
				has_character_modifier = writing_epic_poem
				has_character_modifier = hurufi_studying
			}
			is_not_busy_trigger = yes
		}	
		allow = {
			war = no
			in_command_trigger = no
			is_ill = no
			piety = 200	
		}
		effect = {
			set_character_flag = hurufy_studying_2
			piety = -200
			add_character_modifier = {
				name = hurufi_studying
				duration = -1
			}
			add_character_modifier = {
				name = do_not_disturb
				days = 300
				hidden = yes
			}
			character_event = { id = Plus.1943 days = 200 random = 100 tooltip = BEGIN_HURUFI_STUDIES }
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				is_playable = no
				NOT = {
					is_priest = yes
					trait = scholar
					trait = theologian
					trait = mystic
				}
			}
			modifier = {
				factor = 5
				is_priest = yes
			}
			modifier = {
				factor = 10
				OR = {
					trait = scholar
					trait = theologian
					trait = mystic
				}
			}
			modifier = {
				factor = 0
				OR = {
					NOT = { age = 30 }
					trait = slothful
				}
			}
		}
	}

	hurufi_study3 = {
		ai_check_interval = 12
		potential = {
            religion = hurufi
			is_adult = yes
			is_female = no
			prisoner = no
			trait = hurufi_mystic_b
			NOR = { 
				is_dumb_trigger = yes
				trait = inbred
				trait = blinded
				trait = incapable
				trait = hurufi_mystic_c
				has_character_modifier = writing_epic_poem
				has_character_modifier = hurufi_studying
			}
			is_not_busy_trigger = yes
		}	
		allow = {
			war = no
			in_command_trigger = no
			is_ill = no
			piety = 400	
		}
		effect = {
			set_character_flag = hurufy_studying_3
			piety = -400
			add_character_modifier = {
				name = hurufi_studying
				duration = -1
			}
			add_character_modifier = {
				name = do_not_disturb
				days = 300
				hidden = yes
			}
			character_event = { id = Plus.1943 days = 200 random = 100 tooltip = BEGIN_HURUFI_STUDIES }
		}
		ai_will_do = {
			factor = 1

			modifier = {
				factor = 0
				is_playable = no
				NOT = {
					is_priest = yes
					trait = scholar
					trait = theologian
					trait = mystic
				}
			}
			modifier = {
				factor = 5
				is_priest = yes
			}
			modifier = {
				factor = 10
				OR = {
					trait = scholar
					trait = theologian
					trait = mystic
				}
			}
			modifier = {
				factor = 0
				OR = {
					NOT = { age = 30 }
					trait = slothful
				}
			}
		}
	}
	
	hurufi_omens = {
		ai_check_interval = 12
		potential = {
            religion = hurufi
			is_adult = yes
			is_female = no
			prisoner = no
			trait = hurufi_mystic_c	
			NOT = {
				trait = incapable
				has_character_modifier = hurufi_prediction
				has_character_modifier = writing_epic_poem
			}
			is_not_busy_trigger = yes
		}
		allow = {
			piety = 50
			war = no	
		}
		effect = {
			piety = -50
			add_character_modifier = {
				name = do_not_disturb
				days = 150
				hidden = yes
			}
			hidden_tooltip = { character_event = { id = Plus.1958 days = 100 random = 50 tooltip = GET_HURUFI_OMENS } }
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 10
				OR = {
					trait = zealous
					trait = mystic
					trait = diligent
				}
			}
			modifier = {
				factor = 0
				OR = {
					NOT = { age = 30 }
					trait = paranoid
					trait = slothful
					trait = cynical
				}
			}
		}
	}
	
	hurufi_write_epic_poem = {
		ai_check_interval = 120
		potential = {
			religion = hurufi
			trait = hurufi_diwan
			NOT = {
				trait = incapable
				has_character_modifier = writing_epic_poem
				has_character_modifier = wrote_epic_poem
				has_character_modifier = hurufi_studying
			}
			is_not_busy_trigger = yes
		}
		allow = {
			war = no
			prisoner = no
			is_ill = no
			OR = {
				has_focus = focus_scholarship
				diplomacy = 15
				learning = 15
			}
			NOT = {
				trait = depressed
				trait = stressed
			}
		}
		effect = {
			add_character_modifier = {
				name = writing_epic_poem
				duration = -1
			}
			hidden_tooltip = {
				set_variable = { which = poem_completeness value = 0 }
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.1
				always = yes
			}
			modifier = {
				factor = 5
				OR = {
					diplomacy = 15
					learning = 15
				}
			}
			modifier = {
				factor = 5
				OR = {
					diplomacy = 20
					learning = 20
				}
			}
			modifier = {
				factor = 10
				trait = ambitious
			}
			modifier = {
				factor = 10
				trait = diligent
			}
			modifier = {
				factor = 2.5
				trait = shrewd
			}
			modifier = {
				factor = 5
				trait = quick
			}
			modifier = {
				factor = 10
				trait = genius
			}
			modifier = {
				factor = 0
				OR = {
					NOT = { age = 40 }
					trait = slothful
					trait = content
				}
			}
		}
	}

	druze_pact_of_time = {
		is_high_prio = yes
		ai_check_interval = 3
		potential = {
			religion = druze
			prisoner = no
			NOR = {
				trait = druze_juhhal
				trait = druze_uqqal
				trait = druze_ajawid
				trait = incapable
			}
		}
		allow = {
			is_adult = yes
		}
		effect = {
			piety = 10
			add_trait = druze_juhhal
		}
		ai_will_do = {
			factor = 1
		}
	}

	claim_imamah = {
		is_high_prio = yes
		icon_religion = shiite
		ai_check_interval = 3
		potential = {
			dynasty = 589 #Fatimid
			NOR = {
				trait = shiite_claimant
				shiite_claimant = { is_alive = yes }
				has_global_flag = shia_caliphate_revolt
				has_global_flag = shia_caliphate_revolt_ongoing
			}
		}
		allow = {
			dynasty_head = { character = ROOT }
		}
		effect = {
			set_character_flag = is_shiite_claimant
			add_trait = shiite_claimant
		}
		ai_will_do = {
			factor = 1
		}
	}
}

