﻿##################################################
# Struggle & Struggle Decisions
##################################################

##### Struggle Ending #####
fp3_end_persian_struggle_effect = {
	if = {
		limit = {
			exists = title:e_arabia.holder
			title:e_arabia.holder = { has_character_modifier = fp3_struggle_caliph_super_suspicious_modifier }
		}
		title:e_arabia.holder = { remove_character_modifier = fp3_struggle_caliph_super_suspicious_modifier }
	}

	every_in_global_list = { # We de-flag chars (surprisingly performance friendly, from all possible options)
		variable = fp3_intro_flag_character_list
		if = {
			limit = { has_character_flag = fp3_struggle_intro_event_flag } # Since only alive chars have flags, we don't need to check for anything else
			remove_character_flag = fp3_struggle_intro_event_flag
		}
	}

	# Global variable tracking.
	## For usage in councillor tasks.
	set_global_variable = {
		name = fp3_struggle_ending
		value = $FLAG$
	}
	## For debug purposes.
	if = {
		limit = { gather_debug_variables_for_persian_struggle_trigger = yes }
		increment_global_variable_effect = {
			VAL = current_year
			VAR = sp_end_date
		}
	}

	every_player = {
		limit = { fp3_does_this_player_care_about_the_persian_struggle = yes } # Needs to be checked before the struggle ends due to race condition issues
		add_to_list = player_to_notify # Making a list so stuff is tiggered after the struggle ends, to make extra sure I don't break anything.
	}

	every_in_list = { # Then we *actually* notify palyers, making sure the struggle has already ended
		list = player_to_notify
		limit = {
			this != root
		}
		trigger_event = $EVENT$
	}
	struggle:persian_struggle ?= { end_struggle = $ENDING$ }
}

#### MISC STRUGGLE ENDING(s) STUFF ####
fp3_ending_effects_assertion = {
	# Save Sunni caliph or previous one as scope:struggle_liege
	fp3_save_sunni_caliph_or_previous_effect = yes

	dynasty ?= { add_dynasty_prestige = 10000 }
	
	if = {
		limit = {
			NOT = { title:e_arabia.holder.dynasty ?= dynasty }
		}
		title:e_arabia.holder.dynasty ?= { add_dynasty_prestige = 10000 }
	}

	custom_tooltip = fp3_struggle_assertion_subjugation_expanded_tt
	custom_tooltip = fp3_struggle_assertion_caliphal_muslim_faction_protection_tt

	hidden_effect = {
		destroy_title = title:e_persia
		title:d_sunni = {
			set_variable = {
				name = dar_al_islam
				value = yes
			}
		}
		house = {
			set_variable = {
				name = subjugations_expanded
				value = yes
			}
		}
		title:e_arabia.holder.house = {
			set_variable = {
				name = subjugations_expanded
				value = yes
			}
		}
	}

	struggle:persian_struggle = {
		title:e_arabia.holder = {
			add_prestige = 5000
			custom_tooltip = fp3_struggle_assertion_caliph_detractor_tt
			# Destroy Persia, make each of its kingdoms a de jure part of the Caliph's central title (assuming he still has an empire)
			custom_tooltip = {
				text = fp3_struggle_assertion_arabia_absorb_persia_swing_rewards_tt
				title:e_persia = {
					every_in_de_jure_hierarchy = {
						limit = { tier = tier_kingdom }
						set_de_jure_liege_title = title:e_arabia
					}
				}
			}
			# transfer the supporter trait to the next generation while detractors fade away
			custom_tooltip = {
				text = fp3_struggle_assertion_transfer_supporter_to_next_gen_tt
				struggle:persian_struggle = {
					every_involved_ruler = {
						limit = {
							has_trait = fp3_struggle_supporter
							NOT = {
								primary_title = { is_mercenary_company = yes }
							}
						}
						every_child = {
							if = {
								limit = {
									NOR = {
										has_trait = fp3_struggle_supporter
										primary_title ?= { is_mercenary_company = yes }
									}
								}
								if = {
									limit = { has_trait = fp3_struggle_detractor }
									remove_trait = fp3_struggle_detractor
								}
								add_trait = fp3_struggle_supporter
							}
						}
					}

					every_interloper_ruler = {
						limit = {
							has_trait = fp3_struggle_supporter
							NOT = {
								primary_title = { is_mercenary_company = yes }
							}
						}
						every_child = {
							if = {
								limit = {
									NOR = {
										has_trait = fp3_struggle_supporter
										primary_title ?= { is_mercenary_company = yes }
									}
								}
								if = {
									limit = { has_trait = fp3_struggle_detractor }
									remove_trait = fp3_struggle_detractor
								}
								add_trait = fp3_struggle_supporter
							}
						}
					}
				}
			}
			custom_tooltip = fp3_struggle_persia_ending_assertion_faction_reluctance_tt
		}
		hidden_effect = {
			every_involved_ruler = {
				limit = { has_trait = fp3_struggle_detractor }
				custom = every_detractor
				title:e_arabia.holder = {
					add_opinion = {
						modifier = caliphal_authority
						target = prev
					}
				}
			}
		}
		# Vassalize supporters
		every_involved_ruler = {
			limit = {
				top_liege = this
				OR = {
					has_trait = fp3_struggle_supporter
					scope:struggle_liege = {
						has_hook_of_type = {
							target = prev
							type = caliphal_submission_hook
						}
					}
				}
				NOR = {
					this = scope:struggle_liege
					primary_title = { is_mercenary_company = yes }
				}
			}
			custom = fp3_struggle_assertion_vassalization_rewards_tt
			create_title_and_vassal_change = {
				type = swear_fealty
				save_scope_as = change
				add_claim_on_loss = no
			}
			change_liege = {
				liege = scope:struggle_liege
				change = scope:change
			}
			resolve_title_and_vassal_change = scope:change
		}
		# Involved event
		every_involved_ruler = {
			limit = { this != root }
			trigger_event = fp3_struggle.0072
		}
		# Interloper event
		every_interloper_ruler = {
			limit = { this != root }
			trigger_event = fp3_struggle.0072
		}
	}

	faith = {
		change_fervor = {
			value = 50
			desc = fervor_gain_strengthen_caliphate
		}
		# Propagate the Caliph's Faith in the struggle region
		custom_tooltip = {
			text = fp3_struggle_assertion_spread_faith_through_struggle_region_tt
			struggle:persian_struggle = {
				every_involved_county = {
					limit = {
						faith != root.faith
					}
					random = {
						chance = fp3_strengthen_caliphate_ending_conversion_chance_percent_value
						set_county_faith = root.faith
					}
				}

				every_involved_ruler = {
					limit = {
						NOR = {
							faith = root.faith
							primary_title = { is_mercenary_company = yes }
						}
					}
					random = {
						chance = fp3_strengthen_caliphate_ending_conversion_chance_percent_value
						set_character_faith = root.faith
					}
				}
			}
		}
		# Sunni faiths gets a bonus for the rest of the game converting other counties
		custom_tooltip = {
			text = struggle_persia_ending_assertion_boost_sunni_county_conversion_tt
			set_global_variable = {
				name = fp3_struggle_ending_assertion_school
				value = flag:sunni
			}
		}
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_abassid_strong_achievement_unlocked
			years = 4
		}
	}
}

fp3_struggle_ending_shia_caliphate_effects = {
	### CHARACTER
	give_nickname = nick_the_righteous_caliph

	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_persia = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
		save_scope_as = title_e_persia
	}
	resolve_title_and_vassal_change = scope:title_change

	save_scope_as = founder
	claim_the_imamate_scripted_effect = yes
	add_piety = 5000

	### FAITH
	faith = {
		change_fervor = {
			value = 50
			desc = fervor_gain_shia_caliphate
		}
		if = {
			limit = { has_doctrine = muhammad_succession_muhakkima_doctrine }
			# Muhakkima faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_muhakkima_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:muhakkima
				}
			}
		}
		else_if = {
			limit = { has_doctrine = muhammad_succession_zandaqa_doctrine }
			# Zandaqa faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_zandaqa_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:zandaqa
				}
			}
		}
		else_if = {
			limit = { has_doctrine = muhammad_succession_sunni_doctrine }
			# Sunni faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_sunni_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:sunni
				}
			}
		}
		else = {
			# Shia faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_shia_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:shia
				}
			}
		}
		# Propagate the Caliph's Faith in the struggle region
		custom_tooltip = {
			text = fp3_struggle_ending_shia_caliphate_spread_faith_through_struggle_region_tt
			struggle:persian_struggle = {
				every_involved_county = {
					limit = {
						faith != root.faith
					}
					random = {
						chance = fp3_shia_caliphate_ending_conversion_chance_percent_value
						set_county_faith = root.faith
					}
				}
				every_involved_ruler = {
					limit = {
						NOR = {
							faith = root.faith
							primary_title = { is_mercenary_company = yes }
						}
					}
					random = {
						chance = fp3_shia_caliphate_ending_conversion_chance_percent_value
						set_character_faith = root.faith
					}
				}
			}
		}
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_challenger_caliphate_achievement_unlocked
			years = 4
		}
	}
}

fp3_struggle_ending_vassalize_caliph_effects = {

	# We vassalize the caliph, their vassals might use the confusion to declare independence
	create_title_and_vassal_change = { # Note: do not attempt to simulate faction logic, it is too volatile
		type = usurped
		save_scope_as = change
	}
	title:e_arabia.holder = {
		every_vassal = {
			add_to_list = vassals_to_transfer
		}
		add_to_list = vassals_to_transfer
	}
	title:e_arabia = {
	 	change_title_holder_include_vassals = {
	 		holder = root
			change = scope:change
		}
		save_scope_as = title_e_arabia
	}
	title:d_sunni.holder = {
		change_liege = {
			liege = root
			change = scope:change
		}
	}
	hidden_effect = {
		every_in_list = {
			list = vassals_to_transfer
			limit = {
				root != liege
			}
			change_liege = {
				liege = root
				change = scope:change
			}
		}
	}

	resolve_title_and_vassal_change = scope:change

	dynasty ?= {
		add_dynasty_prestige = 5000
	}

	give_nickname = nick_the_caliph_protector
	
	add_prestige = 5000

	# Unlocks permanent bonuses:
	house = {
		custom_tooltip = fp3_struggle_request_honorary_title_interactions_rewards_tt
		custom_tooltip = fp3_struggle_request_unity_interference_interactions_rewards_tt
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_vassalize_the_caliph_achievement_unlocked
			years = 4
		}
	}
}

#### REMOVE CONTRACT COOLDOWN ####
fp3_remove_vassal_contract_cooldown_for_tension_effect = {
	if = {
		limit = { is_struggle_phase = struggle_persia_phase_stabilisation }
		every_involved_ruler = {
			remove_interaction_cooldown = liege_modify_vassal_contract_interaction
			remove_interaction_cooldown = vassal_modify_vassal_contract_interaction
			remove_interaction_cooldown = ai_only_liege_modify_vassal_contract_interaction
			remove_interaction_cooldown = ai_only_vassal_modify_vassal_contract_interaction
		}
	}
}

fp3_struggle_apply_independent_vassalage_catalyst_effect = {
	$NEW_LIEGE$ = {
		# Involved characters vassalising an independent involved ruler.
		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_vassalize_independent_ruler
						CHAR = $NEW_VASSAL$
					}
				}
			}
			every_character_struggle = {
				involvement = involved
				limit = {
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_vassalize_independent_ruler
						CHAR = $NEW_VASSAL$
					}
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_vassalize_independent_ruler
					character = $NEW_LIEGE$
				}
				log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_vassalize_independent_ruler }
			}
		}
	}
}

fp3_struggle_apply_supporter_detractor_war_won_catalysts_effect = {
	# During the Persian Struggle, a supporter wins any war in the region.
	## Unfair wars.
	if = {
		limit = {
			has_trait = fp3_struggle_supporter
			# Scope:attacker won?
			trigger_if = {
				limit = {
					scope:winner = scope:attacker
					exists = scope:war.var:struggle_defender_tier
				}
				highest_held_title_tier > scope:war.var:struggle_defender_tier
			}
			# Else, scope:defender won.
			trigger_if = {
				limit = {
					scope:winner = scope:defender
					exists = scope:war.var:struggle_attacker_tier
				}
				highest_held_title_tier > scope:war.var:struggle_attacker_tier
			}
			any_character_struggle = {
				involvement = involved
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_supporter_win_unfair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
		}
		every_character_struggle = {
			involvement = involved
			limit = {
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_supporter_win_unfair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
			activate_struggle_catalyst = {
				catalyst = catalyst_supporter_win_unfair_war_within_the_region
				character = scope:winner
			}
			scope:war = { set_variable = used_for_struggle_catalyst_on_war_ended }
			log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_supporter_win_unfair_war_within_the_region }
		}
	}
	## Fair wars.
	else_if = {
		limit = {
			has_trait = fp3_struggle_supporter
			# Scope:attacker won?
			trigger_if = {
				limit = {
					scope:winner = scope:attacker
					exists = scope:war.var:struggle_defender_tier
				}
				highest_held_title_tier <= scope:war.var:struggle_defender_tier
			}
			# Else, scope:defender won.
			trigger_if = {
				limit = {
					scope:winner = scope:defender
					exists = scope:war.var:struggle_attacker_tier
				}
				highest_held_title_tier <= scope:war.var:struggle_attacker_tier
			}
			scope:war = {
				NOT = { has_variable = used_for_struggle_catalyst_on_war_ended }
			}
			any_character_struggle = {
				involvement = involved
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_supporter_win_fair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
		}
		every_character_struggle = {
			involvement = involved
			limit = {
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_supporter_win_fair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
			activate_struggle_catalyst = {
				catalyst = catalyst_supporter_win_fair_war_within_the_region
				character = scope:winner
			}
			scope:war = { set_variable = used_for_struggle_catalyst_on_war_ended }
			log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_supporter_win_fair_war_within_the_region }
		}
	}
	# During the Persian Struggle, a detractor wins any war in the region.
	## Unfair wars.
	else_if = {
		limit = {
			has_trait = fp3_struggle_detractor
			# Scope:attacker won?
			trigger_if = {
				limit = {
					scope:winner = scope:attacker
					exists = scope:war.var:struggle_defender_tier
				}
				highest_held_title_tier > scope:war.var:struggle_defender_tier
			}
			# Else, scope:defender won.
			trigger_if = {
				limit = {
					scope:winner = scope:defender
					exists = scope:war.var:struggle_attacker_tier
				}
				highest_held_title_tier > scope:war.var:struggle_attacker_tier
			}
			scope:war = {
				NOT = { has_variable = used_for_struggle_catalyst_on_war_ended }
			}
			any_character_struggle = {
				involvement = involved
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_detractor_win_unfair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
		}
		every_character_struggle = {
			involvement = involved
			limit = {
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_detractor_win_unfair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
			activate_struggle_catalyst = {
				catalyst = catalyst_detractor_win_unfair_war_within_the_region
				character = scope:winner
			}
			scope:war = { set_variable = used_for_struggle_catalyst_on_war_ended }
			log_debug_variable_for_persian_struggle_effect = { VAR = unrest_catalyst_detractor_win_unfair_war_within_the_region }
		}
	}
	## Fair wars.
	else_if = {
		limit = {
			has_trait = fp3_struggle_detractor
			# Scope:attacker won?
			trigger_if = {
				limit = {
					scope:winner = scope:attacker
					exists = scope:war.var:struggle_defender_tier
				}
				highest_held_title_tier <= scope:war.var:struggle_defender_tier
			}
			# Else, scope:defender won.
			trigger_if = {
				limit = {
					scope:winner = scope:defender
					exists = scope:war.var:struggle_attacker_tier
				}
				highest_held_title_tier <= scope:war.var:struggle_attacker_tier
			}
			any_character_struggle = {
				involvement = involved
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_detractor_win_fair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
		}
		every_character_struggle = {
			involvement = involved
			limit = {
				activate_struggle_catalyst_warfare_check_attacker_involvement_trigger = {
					CATALYST = catalyst_detractor_win_fair_war_within_the_region
					VAR_SCOPE = scope:war
					INVOLVEMENT = involved
				}
			}
			activate_struggle_catalyst = {
				catalyst = catalyst_detractor_win_fair_war_within_the_region
				character = scope:winner
			}
			scope:war = { set_variable = used_for_struggle_catalyst_on_war_ended }
			log_debug_variable_for_persian_struggle_effect = { VAR = unrest_catalyst_detractor_win_fair_war_within_the_region }
		}
	}
}

###################################
# MISC Effects
###################################
remove_any_education_traits_effect = { # Looks ugly but performance is better than with an IF check
	remove_trait = education_martial_1
	remove_trait = education_martial_2
	remove_trait = education_martial_3
	remove_trait = education_martial_4
	remove_trait = education_martial_prowess_1
	remove_trait = education_martial_prowess_2
	remove_trait = education_martial_prowess_3
	remove_trait = education_martial_prowess_4
	remove_trait = education_learning_1
	remove_trait = education_learning_2
	remove_trait = education_learning_3
	remove_trait = education_learning_4
	remove_trait = education_intrigue_1
	remove_trait = education_intrigue_2
	remove_trait = education_intrigue_3
	remove_trait = education_intrigue_4
	remove_trait = education_stewardship_1
	remove_trait = education_stewardship_2
	remove_trait = education_stewardship_3
	remove_trait = education_stewardship_4
	remove_trait = education_diplomacy_1
	remove_trait = education_diplomacy_2
	remove_trait = education_diplomacy_3
	remove_trait = education_diplomacy_4
}

fp3_struggle_ending_concession_effects = {
	# All the involved independent rulers get a white peace
	custom_tooltip = {
		text = fp3_concession_white_peace_tt
		struggle:persian_struggle = {
			every_involved_ruler = {
				limit = {
					top_liege = this
					NOT = {
						primary_title = { is_mercenary_company = yes }
					}
				}
				save_scope_as = peace_target
				scope:peace_target = {
					every_primary_war_enemy = {
						limit = {
							top_liege = this
							NOT = {
								primary_title = { is_mercenary_company = yes }
							}
							fp3_character_any_involvement_persian_struggle_trigger = yes
						}
						every_character_war = {
							limit = {
								OR = {
									primary_attacker = scope:peace_target
									primary_defender = scope:peace_target
								}
							}
							end_war = white_peace
						}
					}
				}
			}
		}
		
		# Silk Road bonus towards prosperity if and when Persia's turmoil comes to an end
		tgp_silk_road_iranian_intermezzo_ending_effect = yes
	}

	# All the involved independent rulers gain truces with each other
	custom_tooltip = {
		text = fp3_concession_truce_tt
		struggle:persian_struggle = {
			every_involved_ruler = {
				limit = {
					top_liege = this
					NOT = {
						primary_title = { is_mercenary_company = yes }
					}
				}
				save_scope_as = first_truce
				every_character_struggle = {
					limit = { is_struggle_type = persian_struggle }
					every_involved_ruler = {
						limit = {
							NOR = {
								this = scope:first_truce
								primary_title = { is_mercenary_company = yes }
							}
							top_liege = this
						}
						add_truce_both_ways = {
							character = scope:first_truce
							days = 18250
							name = fp3_concession_truce_reason
						}
					}
				}
			}
		}
	}

	# All involved rulers, independent or not, lose all their claims
	custom_tooltip = {
		text = fp3_concession_lose_claim_tt
		struggle:persian_struggle = {
			every_involved_ruler = {
				save_scope_as = claim_loser
				every_claim = {
					scope:claim_loser = { remove_claim = prev }
				}
			}
		}
	}

	custom_tooltip = {
		text = fp3_concession_rebuilding_efforts_tt
		struggle:persian_struggle = {
			every_involved_ruler = {
				limit = {
					has_trait = fp3_struggle_supporter
					is_house_head = yes
					NOT = {
						primary_title = { is_mercenary_company = yes }
					}
				}
				house ?= {
					add_house_modifier = {
						modifier = fp3_house_modifier_rebuilding_efforts
						years = fp3_house_modifier_concession_ending_duration
					}
				}
			}
		}
	}

	custom_tooltip = {
		text = fp3_concession_frontier_ambitions_tt
		struggle:persian_struggle = {
			every_involved_ruler = {
				limit = {
					has_trait = fp3_struggle_detractor
					is_house_head = yes
					NOT = {
						primary_title = { is_mercenary_company = yes }
					}
				}
				house ?= {
					add_house_modifier = {
						modifier = fp3_house_modifier_frontier_ambitions
						years = fp3_house_modifier_concession_ending_duration
					}
				}
			}
		}
	}
}


	# Strong independent iranian ruler becomes persian emperor, frees all the iranian subjects and has an easier time vassalizing and or converting them
fp3_struggle_rekindle_iran_effects = {
	give_nickname = nick_the_rekindler_of_iran
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_persia = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
		save_scope_as = title_e_persia
	}
	resolve_title_and_vassal_change = scope:title_change
	
	custom_tooltip = fp3_event_troops_reformer_effect_tt
	hidden_effect = {
		spawn_army = {
			name = persian_warriors
			men_at_arms = {
				type = asawira
				stacks = 5
			}
			men_at_arms = {
				type = ayyar
				stacks = 10
			}
			location = this.capital_province
			uses_supply = yes
			inheritable = yes
		}
		spawn_army = {
			name = persian_warriors
			men_at_arms = {
				type = asawira
				stacks = 5
			}
			men_at_arms = {
				type = ayyar
				stacks = 10
			}
			location = this.capital_province
			uses_supply = yes
			inheritable = yes
		}
	}
	add_prestige = 5000

	# increase the religious conversion demand and offer vassalage acceptance for this character
	dynasty = {
		add_dynasty_modifier = { modifier = fp3_rekindler_of_iran_modifier }
		custom_tooltip = fp3_rekindler_of_iran_modifier_vassalization_effect_tt
		custom_tooltip = fp3_rekindler_of_iran_modifier_conversion_effect_tt
		add_dynasty_prestige = 10000
	}

	if = {
		limit = {
			scope:struggle_persia_ending_rekindle_iran_decision_option_secret_faith = yes
		}
		set_character_faith_with_conversion = secret_faith
		hidden_effect = {
			remove_character_secret_faith = yes
		}
	}

	faith = {
		custom_tooltip = {
			text = fp3_iranian_faith_for_rekindle_persia_spread_to_fellow_iranians_tt
			struggle:persian_struggle = {
				every_involved_county = {
					limit = {
						culture = { has_cultural_pillar = heritage_iranian }
						faith != scope:propagated_faith
					}
					random = {
						chance = fp3_iranian_faith_for_rekindle_persia_conversion_chance_percent_value
						set_county_faith = scope:propagated_faith
					}
				}
				every_involved_ruler = {
					limit = {
						culture = { has_cultural_pillar = heritage_iranian }
						faith != scope:propagated_faith
						NOT = {
							primary_title = { is_mercenary_company = yes }
						}
					}
					random = {
						chance = fp3_iranian_faith_for_rekindle_persia_conversion_chance_percent_value
						set_character_faith = scope:propagated_faith
					}
				}
			}
		}
	}

	struggle:persian_struggle = {
		every_involved_ruler = {
			limit = { culture = { has_cultural_pillar = heritage_iranian } }
			# Add a character modifiers for the Iranian to help them rise!
			custom = fp3_flame_of_iran_modifier_effect_tt
			add_character_modifier = { modifier = fp3_flame_of_iran_modifier }
			custom_tooltip = fp3_flame_of_iran_independence_effect_tt
			custom_tooltip = fp3_event_troops_effect_tt
			hidden_effect = {
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = asawira
						stacks = 2
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = ayyar
						stacks = 4
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = tawashi
						stacks = 2
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = zupin_warrior
						stacks = 4
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = tarkhan
						stacks = 2
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				if = {
					limit = {
						top_liege != this
						NOT = {
							liege.culture = { has_cultural_pillar = heritage_iranian }
						}
					}
					create_title_and_vassal_change = {
						type = independency
						save_scope_as = change
						add_claim_on_loss = yes
					}
					becomes_independent = {
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
		}
	}

	culture = {
		# increase the cultural acceptance for every iranian culture with the rekindler of iran's culture
		custom_tooltip = {
			text = fp3_rekindle_iran_cultural_acceptance_tt
			every_culture_global = {
				limit = {
					has_cultural_pillar = heritage_iranian
					this != root.culture
				}
				change_cultural_acceptance = {
					target = root.culture
					value = 100
					desc = fp3_rekindle_iran_cultural_acceptance_tt
				}
			}
		}

		custom_tooltip = struggle_persia_ending_rekindle_iran_easier_to_convert_culture_tt
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_rekindle_iran_achievement_unlocked
			years = 4
		}
	}
}

fp3_sundered_caliphate_effects = {
	save_scope_as = sunderer
	# Save Sunni caliph or previous one as scope:struggle_liege
	fp3_save_sunni_caliph_or_previous_effect = yes

	add_prestige = massive_prestige_gain
	house ?= {
		add_house_modifier = {
			modifier = fp3_house_modifier_destroyed_the_caliphate
			years = fp3_house_modifier_sunder_caliphate_duration
		}
	}

	scope:the_caliph = {
		add_opinion = {
			target = scope:sunderer
			modifier = fp3_sundered_caliphate_opinion
			opinion = -75
		}
		# This is here to make effect order pretty
		every_vassal = {
			custom = fp3_sunder_the_caliphate_same_faith_vassal_tt
			limit = { faith = scope:sunderer.faith }
			# vassals from same religion as root gains opinion and give hook
			save_scope_as = same_faith_caliph_vassal
			add_opinion = {
				target = scope:sunderer
				modifier = fp3_sundered_caliphate_opinion
				opinion = 25
			}
			custom_tooltip = {
				text = fp3_sunder_the_caliphate_same_faith_vassal_hook_tt
				if = {
					limit = {
						scope:sunderer = {
							can_add_hook = {
								type = favor_hook
								target = scope:same_faith_caliph_vassal
							}
						}
					}
					scope:sunderer = {
						add_hook_no_toast = {
							type = favor_hook
							target = scope:same_faith_caliph_vassal
						}
					}
				}
			}
		}
		every_vassal = {
			custom = fp3_sunder_the_caliphate_hostile_faith_vassal_tt
			limit = {
				faith != root.faith
				faith = {
					faith_hostility_level = {
						target = scope:the_caliph.faith
						value >= religious_cb_enabled_hostility_level
					}
				}
			}
			# vassals with faith hostile to caliph gains opinion
			add_opinion = {
				target = scope:sunderer
				modifier = fp3_sundered_caliphate_opinion
				opinion = 25
			}
		}
		custom_tooltip = destroys_caliph_titles_caliph_tt
		if = {
			limit = {title:e_arabia.holder = scope:the_caliph }
			custom_tooltip = destroys_caliph_titles_arabia_tt
		}
	}

	# Notify players about the fall of the Caliphate
	hidden_effect = {
		every_player = {
			limit = {
				OR = {
				 	is_vassal_of = scope:the_caliph
				 	fp3_character_involved_in_struggle_trigger = yes
				 	this = scope:the_caliph
				}
				this != scope:sunderer
			}
			trigger_event = fp3_struggle.0192
		}
		scope:the_caliph = {
			destroy_title = title:d_sunni
			if = {
				limit = { has_title = title:e_arabia }
				destroy_title = title:e_arabia
			}
		}
	}
}

fp3_struggle_catalysts_for_activities_effect = {
	scope:activity = {
		if = {
			limit = {
				any_special_guest = {
					always = yes
				}
			}
			random_special_guest = {
				save_scope_as = special_honorary_guest
			}
		}
	}
	if = {
		limit = {
			this = scope:host
			scope:special_honorary_guest ?= { is_alive = yes }
			scope:host = {
				is_alive = yes
				any_character_struggle = {
					involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_invite_involved_as_honorary_guests_to_feast_hunt
						CHAR = scope:special_honorary_guest
					}
				}
			}
		}
		scope:host = {
			every_character_struggle = {
				involvement = involved
				limit = { phase_has_catalyst = catalyst_invite_involved_as_honorary_guests_to_feast_hunt }
				activate_struggle_catalyst = {
					catalyst = catalyst_invite_involved_as_honorary_guests_to_feast_hunt
					character = scope:host
				}
				log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_invite_involved_as_honorary_guests_to_feast_hunt }
			}
		}
	}
}

fp3_challenge_house_head_duel_challenger_win_prestige_effect = {
	scope:house_challenger = { add_prestige = medium_prestige_gain }
}

fp3_challenge_house_head_duel_challenger_win_house_effect = {
	scope:house_challenger.house = { set_house_head = scope:house_challenger }
	scope:house_challenger = {
		custom_tooltip = {
			text = fp3_challenge_house_head_recent_cooldown_tt
			add_character_flag = {
				flag = fp3_challenge_house_head_recent_flag
				years = 3
			}
		}
	}
}

fp3_challenge_house_head_duel_challenger_loss_effect = {
	scope:house_challenger = {
		if = {
			limit = { is_alive = yes }
			add_prestige = medium_prestige_loss
		}
	}
	scope:house_head = {
		add_prestige = medium_piety_value
		if = {
			limit = {
				scope:house_challenger = { is_alive = yes }
			}
			add_hook_no_toast = {
				type = trial_by_combat_hook
				target = scope:house_challenger
			}
		}
		custom_tooltip = {
			text = fp3_challenge_house_head_recent_cooldown_tt
			add_character_flag = {
				flag = fp3_challenge_house_head_recent_flag
				years = 3
			}
		}
	}
}

# Saves Sunni holder, or the last one
fp3_save_sunni_caliph_or_previous_effect = {
	if = {
		limit = { exists = title:d_sunni.holder }
		title:d_sunni.holder = { save_scope_as = struggle_liege }
	}
	else = {
		title:d_sunni.previous_holder = { save_scope_as = struggle_liege }
	}
}

fp3_struggle_apply_catalyst_interloper_uninvolved_gain_struggle_titles = {
	if = {
		limit = {
			scope:war = {
				var:struggle_scope_defender ?= struggle:persian_struggle
				var:struggle_involvement_defender ?= flag:involved
			}
			scope:attacker = {
				OR = {
					fp3_character_uninvolved_in_struggle_trigger = yes
					fp3_character_interloper_in_struggle_trigger = yes
				}
			}
			struggle:persian_struggle = {
				phase_has_catalyst = catalyst_interloper_uninvolved_gain_struggle_titles
			}
		}
		struggle:persian_struggle = {
			activate_struggle_catalyst = {
				catalyst = catalyst_interloper_uninvolved_gain_struggle_titles
				character = scope:attacker
			}
			log_debug_variable_for_persian_struggle_effect = { VAR = concession_catalyst_interloper_uninvolved_gain_struggle_titles }
		}
	}
}
