﻿fp3_should_use_iranian_graphics_trigger = {
	culture = { has_building_gfx = iranian_building_gfx }
}

eligible_for_fp3_khvarenah_legacy_trigger = {
	has_fp3_dlc_trigger = yes
	dynasty = {
		OR = {
			dynast = {
				culture = {
					OR = {
						has_cultural_pillar = heritage_iranian 
						any_parent_culture_or_above = {
							has_cultural_pillar = heritage_iranian 
						}
					}
				}
			}
			has_dynasty_perk = fp3_khvarenah_legacy_1
		}
	}
}

fp3_warrior_culture_trigger = { 
	AND = {
		OR = {
			has_cultural_pillar = ethos_bellicose
			has_cultural_tradition = tradition_swords_for_hire
		}
		OR = {
			has_cultural_pillar = heritage_iranian
			has_cultural_pillar = heritage_arabic
			has_cultural_pillar = heritage_turkic
			has_cultural_pillar = heritage_tocharian
		}
	}
}

fp3_custom_divinity_for_zoroastrianism_faiths = {
	OR = {
		faith.religion = faith:mazdayasna.religion
		faith.religion = faith:gayomarthianism.religion
		faith.religion = faith:khurmazta.religion
		faith.religion = faith:zurvanism.religion
		faith.religion = faith:urartuism.religion
		faith.religion = faith:mazdakism.religion
		faith.religion = faith:khurramism.religion
	}
}

fp3_sufism_enthusiast_trigger = {
	has_religion = religion:islam_religion
	learning > good_skill_level 
	OR = {
		faith = { has_doctrine = tenet_esotericism }
		faith = { has_doctrine_parameter = meditation_mechanics_active }
		has_character_modifier = hallucinogenic_visions_uplifting_modifier
		has_character_modifier = cosmic_insights_modifier 
		has_character_modifier = fp3_sufi_initiate_modifier 
		has_character_modifier = foreboding_visions_modifier
		has_character_modifier = mysterious_prophetess_illness_modifier 
		learning >= excellent_skill_level
	}
}

# Used for speeding-up debug. Has little to no impact on performance, does not cause errors, or affect player experience.
is_developer_testing_trigger = {
	is_ai = no
	debug_only = yes
	has_multiple_players = no
}

fp3_will_remain_in_caliphate_regardless_of_takeover = {
	OR = {
		opinion = { # They must either not hate you
			target = scope:struggle_ender
			value > -29
		}
		opinion = { # Or really want to stay by the side of the caliph
			target = scope:struggle_liege
			value > 54
		}
	}
}
		
fp3_eligible_for_yearly_events_trigger = {
	has_fp3_dlc_trigger = yes
	OR = {
		culture = { has_cultural_pillar = heritage_iranian }
		culture = { has_cultural_pillar = heritage_turkic }
		culture = { has_cultural_pillar = heritage_mongolic }
		culture = { has_cultural_pillar = heritage_arabic }
	}
	any_realm_county = { # Most events are geographical in nature
		any_county_province = { geographical_region = world_persian_empire }
	}
}

fp3_is_valid_futuwaa_member = {
	$SPONSOR$.capital_province ?= { this = prev.location } # Prevents error log errors from dumbo AI
	is_ai = yes
	age > 12
	age < 25
	is_married = no
	can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $SPONSOR$ }
}

fp3_is_valid_madrasa_pupil = {
	$SPONSOR$.capital_province ?= { this = prev.location } # Prevents error log errors from dumbo AI
	is_ai = yes
	age > 8
	age < 16
	NOT = { has_character_flag = studying_at_university }
	#Gender rules
	OR = {
		$SPONSOR$ = { has_realm_law = equal_law }
		AND = {
			$SPONSOR$ = {
				OR = {
					has_realm_law = female_only_law
					has_realm_law = female_preference_law
				}
			}
			is_female = yes
		}
		AND = {
			$SPONSOR$ = {
				OR = {
					has_realm_law = male_only_law
					has_realm_law = male_preference_law
				}
			}
			is_male = yes
		}
	}
}

fp3_valid_shia_head = {
	calc_true_if = {
		amount >= 2

		# Sayyids get a free pass.
		has_trait = sayyid
		# The wise get a free pass.
		has_trait = lifestyle_mystic
		piety_level >= very_high_piety_level
		# Having many faithful counties.
		custom_description = {
			text = claim_the_imamate_decision.need_at_least_fifteen_same_faith_counties
			any_sub_realm_county = {
				count >= claim_the_imamate_decision_same_faith_greater_county_count
				faith = root.faith
			}
		}
		# Ancient Authority legacy.
		dynasty ?= { has_dynasty_perk = fp3_khvarenah_legacy_4 }
	}
}

fp3_valid_muhakkima_head = {
	calc_true_if = {
		amount >= 2

		# Sayyids get a free pass.
		has_trait = sayyid
		piety_level >= very_high_piety_level
		# Having many faithful counties
		custom_description = {
			text = claim_the_imamate_decision.need_at_least_fifteen_same_faith_counties
			any_sub_realm_county = {
				count >= claim_the_imamate_decision_same_faith_greater_county_count
				faith = root.faith
			}
		}
		# Famed
		prestige_level >= 4
		# #Muhakkima cares about justified warfare - and a justified cause.
		OR = {
			has_trait = faith_warrior #mujahid really
			has_trait = theologian
		}
	}
}

fp3_does_this_player_care_about_the_persian_struggle = {
	save_temporary_scope_as = char_temp
	OR = {
		capital_province ?= {
			OR = { # Is in region
				geographical_region = world_middle_east_persia
				geographical_region = world_africa_north_east
			}
		}
		struggle:persian_struggle = { is_culture_involved_in_struggle = scope:char_temp.culture }
		struggle:persian_struggle = { is_faith_involved_in_struggle = scope:char_temp.faith }
		# Ping islamic and zoroastrian players as they might be interested
		religion = religion:islam_religion
		religion = religion:zoroastrianism_religion
		fp3_character_involved_in_struggle_trigger = yes
		fp3_character_interloper_in_struggle_trigger = yes
	}
}

fp3_reward_from_vassalization_ending = {
	has_fp3_dlc_trigger = yes

	# Only available when the vassalization happened
	exists = global_var:fp3_struggle_ending
	global_var:fp3_struggle_ending = flag:fp3_struggle_vassalize_caliph_ending

	#need to be the liege of the caliph or from their House

	scope:recipient = {
		any_held_title = {
			this = title:d_sunni
		}
	}

	scope:actor.house ?= scope:recipient.top_liege.house
}

fp3_struggle_involves_one_supporter_and_one_detractor = {
	OR = {
		AND = {
			$FIRST$ = { has_trait = fp3_struggle_detractor }
			$SECOND$ = { has_trait = fp3_struggle_supporter }
		}
		AND = {
			$FIRST$ = { has_trait = fp3_struggle_supporter }
			$SECOND$ = { has_trait = fp3_struggle_detractor }
		}
	}
}

fp3_struggle_scoped_is_the_arabian_caliph_trigger = {
	OR = {
		has_title = title:e_arabia
		has_title = title:d_sunni
	}
}

fp3_fortress_of_faith_county_trigger = {
	faith = { save_temporary_scope_as = compare_faith }
	root.faith = { 
		faith_hostility_level = { 
			target = scope:compare_faith
			value < faith_evil_level
		}
	}
}

fp3_is_valid_loyal_puppet = { # Install a suitable lackey to do your bidding
	has_trait = fp3_struggle_supporter
	# Can't be friends w/liege or indebted to them
	liege ?= {
		NOR = {
			has_relation_friend = prev
			has_hook = prev
		}
	}
	primary_title.tier >= tier_county # We don't want to promote baron to avoid having Republic or Theocracies realms
}

fp3_proper_gender_for_dynastic_ambition_trigger = {
	OR = { #checking for appropriate gender
		AND = {
			root = {
				OR = {
					has_realm_law = male_preference_law
					has_realm_law = male_only_law
				}
			}
			is_male = yes
		}
		AND = {
			root = {
				OR = {
					has_realm_law = female_preference_law
					has_realm_law = female_only_law
				}
			}
			is_female = yes
		}
		AND = {
			root = { has_realm_law = equal_law }
			OR = {
				is_female = yes
				is_male = yes
			}
		}
	}
}

fp3_character_any_involvement_persian_struggle_trigger = {
	has_fp3_dlc_trigger = yes
	OR = {
		fp3_character_involved_in_struggle_trigger = yes
		fp3_character_interloper_in_struggle_trigger = yes
	}
}

fp3_character_involved_in_struggle_trigger = {
	has_fp3_dlc_trigger = yes
	any_character_struggle = {
		involvement = involved
		is_struggle_type = persian_struggle
	}
}

fp3_character_interloper_in_struggle_trigger = {
	has_fp3_dlc_trigger = yes
	any_character_struggle = {
		involvement = interloper
		is_struggle_type = persian_struggle
	}
}

fp3_character_uninvolved_in_struggle_trigger = {
	has_fp3_dlc_trigger = yes
	exists = struggle:persian_struggle
	fp3_character_interloper_in_struggle_trigger = no
	fp3_character_involved_in_struggle_trigger = no
}

fp3_struggle_is_the_caliph = {
	AND = {
		any_held_title = {
			this = title:d_sunni
		}
		any_held_title = {
			this = title:e_arabia
		}
	}
}

fp3_embellish_capital_decision_character_trigger = {
	is_capable_adult_ai = yes
	is_imprisoned = no
	is_healthy = yes 
	NOR = { 
		has_relation_rival = root 
		is_vassal_of = root
	}
}

fp3_name_read_in_friday_prayer_decision_liege_requirements_trigger = {
	OR = {
		is_independent_ruler = yes
		custom_description = {
			text = name_read_in_friday_prayer_crown_authority
			is_independent_ruler = no
			liege ?= { has_realm_law = crown_authority_0  }
		}
		AND = {
			has_trait = fp3_struggle_detractor
			liege = { has_title = title:d_sunni }
		}
		# We have to trigger_if this for tooltip reasons.
		trigger_if = {
			limit = { is_independent_ruler = no }
			liege = { government_has_flag = government_is_tribal }
		}
	}
}

favour_skilled_outsiders_decision_valid_character_to_take_trigger = {
	OR = {
		has_perk = meritocracy_perk
		government_has_flag = government_is_clan
		culture = { has_cultural_pillar = ethos_egalitarian }
		calc_true_if = {
			amount >= 2
			has_trait = calm
			has_trait = humble
			has_trait = ambitious
		}
	}
}

favour_skilled_outsiders_decision_valid_county_to_generate_trigger = {
	culture = {
		# We don't technically need a custom tooltip for this, but the formatting looks much nicer.
		custom_tooltip = {
			text = favour_skilled_outsiders_decision.tt.not_share_your_heritage
			NOT = { has_same_culture_heritage = root.culture }
		}
		trigger_if = {
			limit = { exists = root.cp:councillor_chancellor }
			custom_tooltip = {
				text = favour_skilled_outsiders_decision.tt.not_share_chancellor_heritage
				NOT = { has_same_culture_heritage = root.cp:councillor_chancellor.culture }
			}
		}
		trigger_if = {
			limit = { exists = root.cp:councillor_marshal }
			custom_tooltip = {
				text = favour_skilled_outsiders_decision.tt.not_share_marshal_heritage
				NOT = { has_same_culture_heritage = root.cp:councillor_marshal.culture }
			}
		}
		trigger_if = {
			limit = { exists = root.cp:councillor_steward }
			custom_tooltip = {
				text = favour_skilled_outsiders_decision.tt.not_share_steward_heritage
				NOT = { has_same_culture_heritage = root.cp:councillor_steward.culture }
			}
		}
		trigger_if = {
			limit = { exists = root.cp:councillor_spymaster }
			custom_tooltip = {
				text = favour_skilled_outsiders_decision.tt.not_share_spymaster_heritage
				NOT = { has_same_culture_heritage = root.cp:councillor_spymaster.culture }
			}
		}
		trigger_if = {
			limit = { exists = root.cp:councillor_court_chaplain }
			custom_tooltip = {
				text = favour_skilled_outsiders_decision.tt.not_share_court_chaplain_heritage
				NOT = { has_same_culture_heritage = root.cp:councillor_court_chaplain.culture }
			}
		}
		trigger_if = {
			limit = {
				root = { is_independent_ruler = no }
			}
			custom_tooltip = {
				text = favour_skilled_outsiders_decision.tt.not_share_liege_heritage
				save_temporary_scope_as = culture_temp
				NOT = {
					root = {
						any_liege_or_above = {
							culture = { has_same_culture_heritage = scope:culture_temp }
						}
					}
				}
			}
		}
	}
}

favour_skilled_outsiders_decision_grab_courtlies_trigger = {
	OR = {
		NOT = { exists = scope:outsider_select }
		NOT = {
			culture = { has_same_culture_heritage = scope:outsider_select.culture }
		}
	}
	has_vassal_stance = courtly
}

favour_skilled_outsiders_decision_grab_landless_councillors_trigger = {
	OR = {
		NOT = { exists = scope:outsider_select }
		NOT = {
			culture = { has_same_culture_heritage = scope:outsider_select.culture }
		}
	}
	is_landed = no
}

favour_skilled_outsiders_decision_non_courtlies_trigger = {
	OR = {
		NOT = { exists = scope:outsider_select }
		NOT = {
			culture = { has_same_culture_heritage = scope:outsider_select.culture }
		}
	}
	NOT = { has_vassal_stance = courtly }
}

has_clan_succession_law = {
	is_ruler = yes
	OR = {
		has_realm_law = clan_antagonistic_partition_succession_law
		has_realm_law = clan_competitive_partition_succession_law
		has_realm_law = clan_impassive_partition_succession_law
		has_realm_law = clan_friendly_partition_succession_law
		has_realm_law = clan_harmonious_partition_succession_law
	}
}
