﻿persian_struggle = {
	# Use fp3_struggle.9982 for nav
	#########################################################################
	# Feature Ownership and Implementation by Hugo Cortell
	# Design by Emil Tisander, Alexander Oltner, and Hugo Cortell
	# Additional Design by Eren Humphrey, Ola Jentzsch, and Nikola Stokić
	# Reworked by Maxence Voleau & Ewan Cowhig Croft
	#########################################################################

	##################################################
	# Setup
	##################################################
	cultures = {
		# Iranian
		eranian
		persian
		kurdish
		daylamite
		baloch
		tajik
		afghan
		brahui
		sogdian
		khwarezmian

		# Arabic
		bedouin
		levantine # "mashriqi"
		yemeni
	}
	faiths = {
		# Islamic
		ashari
		zayidi
		ismaili
		azariqa
		mutazila
		maturidi
		imami
		alevi

		# Zoroastrian
		mazdayasna
		khurmazta
		khurramism
		afridunism

		# Buddhist
		mahayana

		# Christian
		nestorian
	}
	regions = {
		world_persian_empire
		ghw_region_kurdistan
	}

	involvement_prerequisite_percentage = 0.5 # Easier to join the struggle due to the nature of the conflict

	on_start = {
		# Start the over-time catalysts.
		trigger_event = neutral_struggle.0001
		# Tell the AI to change sides every now and then.
		trigger_event = {
			id = fp3_struggle.0001
			years = { 1 5 }
		}
	}

	transition_state_duration = { months = 3 }

	on_change_phase = {
		# If relevant, gather data on when we changed phase.
		if = {
			limit = { gather_debug_variables_for_persian_struggle_trigger = yes }
			# Are we switching out of the Concession phase?
			if = {
				limit = { is_struggle_phase = struggle_persia_phase_stabilisation }
				increment_global_variable_effect = {
					VAL = 1
					VAR = sp_phase_stabil
				}
			}
			# Are we switching out of the Unrest phase?
			else_if = {
				limit = { is_struggle_phase = struggle_persia_phase_unrest }
				increment_global_variable_effect = {
					VAL = 1
					VAR = sp_phase_unrest
				}
			}
		}
	}
	
	# Root = the character which has joined the struggle
 	on_join = {
		if = {
			limit = {
				NOR = {
					has_trait = fp3_struggle_supporter
					has_trait = fp3_struggle_detractor
					culture = { has_cultural_pillar = heritage_turkic }
					fp3_character_involved_in_struggle_trigger = no
				}
				trigger_if = {
					limit = {
						NOR = {
							has_trait = zealous
							has_trait = humble
							has_trait = loyal
							ai_zeal >= 75
						}
					}
					NOR = {
						has_trait = cynical
						has_trait = arrogant
						has_trait = vengeful
						has_trait = ambitious
					}
				}
				OR = {
					fp3_struggle_is_the_caliph = yes
					AND = {
						liege = {
							fp3_struggle_is_the_caliph = yes
						}
						opinion = {
							target = title:d_sunni.holder
							value >= low_positive_opinion
						}
					}
					AND = {
						faith = { has_doctrine = muhammad_succession_sunni_doctrine }
						exists = title:d_sunni.holder
						opinion = {
							target = title:d_sunni.holder
							value >= medium_negative_opinion
						}
					}
				}

			}
			add_trait = fp3_struggle_supporter
		}
		else_if = {
			limit = {
				NOR = {
					has_trait = fp3_struggle_supporter
					has_trait = fp3_struggle_detractor
				}
			}
			add_trait = fp3_struggle_detractor
		}

		if = {
			limit = { is_ai = no }
			trigger_event = fp3_struggle.0051
		}
 	}

	##################################################
	# Phases
	##################################################
	start_phase = struggle_persia_phase_unrest
	
	phase_list = {

		struggle_persia_phase_stabilisation = {
			duration = { points = 500 }

			future_phases = {
				struggle_persia_phase_unrest = {
					catalysts = {
						# Global
						catalyst_yearly_influential_house_is_antagonistic_major = major_struggle_catalyst_gain
						catalyst_yearly_influential_house_is_antagonistic_medium = medium_struggle_catalyst_gain
						catalyst_yearly_influential_house_is_antagonistic_minor = minor_struggle_catalyst_gain
						catalyst_yearly_influential_house_is_antagonistic_minimal = minimal_struggle_catalyst_gain
						
						# Flavour
						catalyst_becomes_rival_supporter_detractor = medium_struggle_catalyst_gain
						catalyst_gain_claim_on_title_fp3 = minor_struggle_catalyst_gain
						catalyst_usurp_title_fp3 = medium_struggle_catalyst_gain
						catalyst_cadet_branch_created = major_struggle_catalyst_gain
						catalyst_turn_supporter_into_detractor = medium_struggle_catalyst_gain
						catalyst_revealed_secret_faith_non_islam = medium_struggle_catalyst_gain

						# CBs
						catalyst_detractor_declare_unfair_war_within_the_region = medium_struggle_catalyst_gain
						catalyst_detractor_declare_fair_war_within_the_region = major_struggle_catalyst_gain
						catalyst_detractor_win_unfair_war_within_the_region = major_struggle_catalyst_gain
						catalyst_detractor_win_fair_war_within_the_region = massive_struggle_catalyst_gain

						# Negative actions on Supporters
						catalyst_supporter_forced_conversion = medium_struggle_catalyst_gain
						catalyst_imprison_supporter = minor_struggle_catalyst_gain
						catalyst_reveal_secret_supporter = medium_struggle_catalyst_gain
						catalyst_execute_supporter = medium_struggle_catalyst_gain
						catalyst_unnatural_death_supporter = medium_struggle_catalyst_gain

						# Positive actions of Detractors
						catalyst_forming_alliance_between_detractor_uninvolved_rulers = medium_struggle_catalyst_gain
						catalyst_ruler_chooses_persian_faith = major_struggle_catalyst_gain
						catalyst_became_detractor_fp3 = massive_struggle_catalyst_gain
					}
				}

				struggle_persia_ending_phase_concession = {
					# Catalyst values are low here because we track this stuff across every phase, so they need to take _roughly_ ~100-120 years to build up, all else being equal.
					catalysts = {
						catalyst_passing_of_time = catalyst_fp3_yearly_time_out_ending_drift

						# Supporter / Detractor dynamic
						catalyst_war_ends_in_white_peace_between_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_became_best_friend_soulmate_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_became_friend_lover_supporter_detractor = minimal_struggle_catalyst_gain
						catalyst_forming_alliance_between_supporter_detractor_rulers = catalyst_forming_alliance_between_supporter_detractor_rulers_value
						catalyst_abandon_hook_on_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_grants_pardon_supporter_detractor = minimal_struggle_catalyst_gain
						catalyst_release_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_ransom_supporter_detractor = minimal_struggle_catalyst_gain
						catalyst_grants_vassal_to_de_jure_liege_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_gift_supporter_detractor_ruler = minimal_struggle_catalyst_gain

						# Progress of the outsiders
						catalyst_interloper_uninvolved_gain_struggle_titles = minor_struggle_catalyst_gain

						# Global catalysts
						catalyst_raided_involved = minor_struggle_catalyst_gain

						# Caliphal things.
						catalyst_missing_caliphate_or_arabian_empire = monumental_struggle_catalyst_over_time_gain
						catalyst_caliph_or_emperor_loses_revolt_war = major_struggle_catalyst_gain
						catalyst_caliph_or_emperor_accepts_faction_demands = minor_struggle_catalyst_gain
						catalyst_caliph_died_violently = massive_struggle_catalyst_gain
						catalyst_caliph_died_naturally = major_struggle_catalyst_gain
					}
				}
			}

			war_effects = {
				name = WAR_EFFECTS_NAME
				# lower cost and higher gain for external wars
				common_parameters = {
					apply_truce_when_sending_ward = yes
					invasion_conquest_war_cannot_be_declared = yes
				}

				involved_character_modifier = {
					# Everyone hires soldiers, either to maintain the peace or ready for the next phase of war.
					men_at_arms_recruitment_cost = -0.5
					men_at_arms_maintenance = -0.25
					# This pumps some characters up.
					glory_hound_ai_energy = very_high_positive_ai_value
					glory_hound_ai_boldness = very_high_positive_ai_value
				}

				interloper_character_modifier = {
					# Interlopers benefit a little here too.
					men_at_arms_recruitment_cost = -0.25
				}
			}

			faith_effects = {
				name = FAITH_EFFECTS_NAME
				
				involved_parameters = {
					piety_from_new_alliance = yes
					completing_building_in_temple_gives_piety = yes
					same_faith_friend_piety_gain = yes
				}

				involved_character_modifier = {
					# The Sunni Caliphate consolidates - everyone else closes ranks too.
					same_faith_opinion = 20
					# Whilst bunkering down and arguing 
					minority_different_faith_opinion = -20
				}
			}

			culture_effects = {
				name = CULTURE_EFFECTS_NAME

				involved_parameters = {
					learning_languages_gives_prestige = yes
					granting_title_to_local_noble_gives_prestige = yes
					gain_acceptance_when_developing_other_culture_county = yes
					release_prisoner_diff_culture_gives_prestige = yes
				}

				involved_character_modifier = {
					# Solidarity within cultural blocks.
					same_culture_opinion = 10
					opinion_of_same_culture = 10
					# Authority figures work to reestablish themselves.
					cultural_head_acceptance_gain_mult = 0.5
				}
			}

			other_effects = {
				name = OTHER_EFFECTS_NAME
				involved_parameters = {
					struggle_unlocks_befriend_schemes_for_everyone = yes
					unlocks_epic_commission_for_independent_rulers = yes
					granting_independence_to_non_dejure_gives_renown = yes
					unlocks_claim_throne_for_caliph_vassals = yes
					less_dissolution_more_claimants = yes

					# Note: this parameter doesn't _do_ anything, it's just for the tooltip.
					## You need to manually set/clear the "struggle_block_dissolution_faction" variable on affected titles instead.
					### We do it this way to save a bit on performance.
					caliph_cant_be_dissolutioned = yes
				}
				involved_character_modifier = {
					castle_holding_build_gold_cost = -0.3
				}
				interloper_parameters = {
					less_dissolution_more_claimants = yes
				}
			}

			ending_decisions = {
				struggle_persia_ending_foundation_decision # renamed Tempering the Caliphate
				struggle_persia_ending_assertion_decision # renamed Strenghten the Caliphate
				struggle_persia_ending_rekindle_iran_decision
			}
		}

		struggle_persia_phase_unrest = {
			duration = { points = 500 }

			future_phases = {
				struggle_persia_phase_stabilisation = {
					catalysts = {
						# global catalysts
						catalyst_yearly_influential_house_is_harmonious_major = major_struggle_catalyst_gain
						catalyst_yearly_influential_house_is_harmonious_medium = medium_struggle_catalyst_gain
						catalyst_yearly_influential_house_is_harmonious_minor = minor_struggle_catalyst_gain
						catalyst_yearly_influential_house_is_harmonious_minimal = minimal_struggle_catalyst_gain
						catalyst_vassalize_independent_ruler = major_struggle_catalyst_gain
						
						# Flavour
						catalyst_learned_new_language_important = minor_struggle_catalyst_gain
						catalyst_invite_involved_as_honorary_guests_to_feast_hunt = medium_struggle_catalyst_gain
						catalyst_revealed_secret_faith_islam = medium_struggle_catalyst_gain

						# CBs
						catalyst_supporter_declare_unfair_war_within_the_region = medium_struggle_catalyst_gain
						catalyst_supporter_declare_fair_war_within_the_region = major_struggle_catalyst_gain
						catalyst_supporter_win_unfair_war_within_the_region = major_struggle_catalyst_gain
						catalyst_supporter_win_fair_war_within_the_region = massive_struggle_catalyst_gain

						# Suppporters do good things
						catalyst_install_loyalist_cb = major_struggle_catalyst_gain
						catalyst_became_supporter_fp3 = massive_struggle_catalyst_gain

						# Detractor negative impact
						catalyst_imprison_detractor = minor_struggle_catalyst_gain
						catalyst_execute_detractor = medium_struggle_catalyst_gain
						catalyst_unnatural_death_detractor = medium_struggle_catalyst_gain
						catalyst_reveal_secret_detractor = medium_struggle_catalyst_gain
						catalyst_detractor_forced_conversion = medium_struggle_catalyst_gain
						catalyst_turn_detractor_into_supporter = medium_struggle_catalyst_gain

						# chasing outsiders
						catalyst_gain_struggle_titles_from_interlopers_uninvolved = medium_struggle_catalyst_gain
					}
				}

				struggle_persia_ending_phase_concession = {
					# Catalyst values are low here because we track this stuff across every phase, so they need to take _roughly_ ~100-120 years to build up, all else being equal.
					catalysts = {
						catalyst_passing_of_time = catalyst_fp3_yearly_time_out_ending_drift

						# Supporter / Detractor dynamic
						catalyst_war_ends_in_white_peace_between_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_became_best_friend_soulmate_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_became_friend_lover_supporter_detractor = minimal_struggle_catalyst_gain
						catalyst_forming_alliance_between_supporter_detractor_rulers = catalyst_forming_alliance_between_supporter_detractor_rulers_value
						catalyst_abandon_hook_on_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_grants_pardon_supporter_detractor = minimal_struggle_catalyst_gain
						catalyst_release_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_ransom_supporter_detractor = minimal_struggle_catalyst_gain
						catalyst_grants_vassal_to_de_jure_liege_supporter_detractor = minor_struggle_catalyst_gain
						catalyst_gift_supporter_detractor_ruler = minimal_struggle_catalyst_gain

						# Progress of the outsiders
						catalyst_interloper_uninvolved_gain_struggle_titles = minor_struggle_catalyst_gain

						# Global catalysts
						catalyst_raided_involved = minor_struggle_catalyst_gain

						# Caliphal things.
						catalyst_missing_caliphate_or_arabian_empire = monumental_struggle_catalyst_over_time_gain
						catalyst_caliph_or_emperor_loses_revolt_war = major_struggle_catalyst_gain
						catalyst_caliph_or_emperor_accepts_faction_demands = minor_struggle_catalyst_gain
						catalyst_caliph_died_violently = massive_struggle_catalyst_gain
						catalyst_caliph_died_naturally = major_struggle_catalyst_gain
					}
				}
			}

			war_effects = {
				name = WAR_EFFECTS_NAME

				involved_parameters = {
					install_loyalist_cb_unlocked = yes
					offer_vassalization_removes_disloyalty = yes
					fp3_caliph_gets_cheaper_subjugation_cb = yes
					cheaper_incursions_vs_caliph = yes
				}

				all_county_modifier = {
					# The region is more amenable for waging war.
					supply_limit_mult = 1
					levy_reinforcement_rate_friendly_territory = 1
				}

				involved_county_modifier = {
					# The region is more amenable for waging war.
					levy_size = 1
					garrison_size = -0.5
				}

				interloper_county_modifier = {
					# Interlopers get partial benefits for waging war.
					levy_size = 0.5
					garrison_size = -0.25
				}

				uninvolved_county_modifier = {
					# Uninvolved characters shouldn't be here (lookin' at you caliphs that lose Baghdad), so they get punished.
					levy_size = -0.5
					garrison_size = -0.75
				}

				involved_character_modifier = {
					# Fighting is encouraged, especially defensively.
					same_heritage_county_advantage_add = 5
					mercenary_hire_cost_mult = -0.25
					# Get out there and do your part.
					led_by_owner_extra_advantage_add = 5
					ai_war_chance = 2
					ai_war_cooldown = -0.25
				}

				interloper_character_modifier = {
					# Interlopers'll have an easier time with hiring mercenaries.
					mercenary_hire_cost_mult = -0.35
					# But also benefit greatly from taking part personally.
					# Get out there and do your part.
					led_by_owner_extra_advantage_add = 10
					ai_war_chance = 10
					ai_war_cooldown = -0.5
				}
			}

			faith_effects = {
				name = FAITH_EFFECTS_NAME

				involved_parameters = {
					completing_building_in_temple_gives_piety = yes
					demand_submission_interaction_unlocked = yes
					piety_level_affects_vassalage_acceptance = yes
					can_trade_piety_for_marriage_acceptance = yes
					piety_from_converting_county = yes
					piety_from_converting_involved_rulers = yes
				}

				involved_character_modifier = {
					# Zealots are ready to throw down.
					zealot_opinion = 20
					zealot_levy_contribution_mult = 0.5
					zealot_tax_contribution_mult = 0.5
					zealot_ai_vengefulness = very_high_positive_ai_value
					zealot_ai_zeal = very_high_positive_ai_value
					zealot_ai_boldness = high_positive_ai_value
					church_holding_build_gold_cost = -0.3
				}
			}

			culture_effects = {
				name = CULTURE_EFFECTS_NAME

				involved_parameters = {
				  struggle_prestige_from_befriending_same_culture = yes
				  struggle_prestige_install_same_culture_claimant = yes
				  struggle_different_culture_more_likely_to_use_minority_stance = yes
				}

				involved_character_modifier = {
					# Minorities within your realm want no part of whatever you're pushing.
					minority_opinion = -10
					minority_tax_contribution_mult = -0.5
					minority_levy_contribution_mult = -0.5
					minority_ai_energy = high_positive_ai_value
					# Diff-faith minorities _especially_ have no time for you.
					minority_different_faith_opinion = -10
				}
			}

			other_effects = {
				name = OTHER_EFFECTS_NAME

				# We don't immediately dunk on your economics too much: you get tools to build up in the other phase + tools to wage war here, so we want characters to injure themselves because of conflicts they start first and foremost rather than nebulous regional factors.

				all_county_modifier = {
					# Not much happening economically.
					build_speed = -0.5
					# Lotta armed folk on the road.
					travel_danger = 25
				}

				involved_county_modifier = {
					# If you're at war, there's chaos at home.
					monthly_county_control_decline_at_war_add = 0.02
				}

				interloper_county_modifier = {
					# If you're at war, there's chaos at home.
					monthly_county_control_decline_at_war_add = 0.01
				}

				uninvolved_county_modifier = {
					# Everyone's squabbling amongst themselves in your struggle lands.
					monthly_county_control_decline_at_war_add = 0.03
				}

				involved_character_modifier = {
					owned_hostile_scheme_success_chance_add = 20
					claim_throne_scheme_phase_duration_add = monumental_scheme_phase_duration_bonus_value
				}

				involved_parameters = {
					unlocks_abandon_hook_for_all = yes
					unlocks_claim_throne_for_all = yes
					fp3_sway_scheme_more_powerful = yes 
					fp3_sway_vassals_removes_disloyalty = yes
					empowered_vizierate = yes
					unlocks_challenge_house_head_for_clans = yes
					less_dissolution_more_claimants = yes

					# Note: this parameter doesn't _do_ anything, it's just for the tooltip.
					## You need to manually set/clear the "struggle_block_dissolution_faction" variable on affected titles instead.
					### We do it this way to save a bit on performance.
					caliph_cant_be_dissolutioned = yes
				}
				interloper_parameters = {
					less_dissolution_more_claimants = yes
				}
			}

			ending_decisions = {
				struggle_persia_ending_foundation_decision # renamed Tempering the Caliphate
				struggle_persia_ending_assertion_decision # renamed Strenghten the Caliphate
				struggle_persia_ending_rekindle_iran_decision #renamed to Rekindle Iran
			}
		}

		# Phase Endings
		struggle_persia_ending_phase_concession = {
			save_progress = yes

			on_start = {
				#this has to be run before fp3_end_persian_struggle_effect for concession effects to work
				fp3_struggle_ending_concession_effects = yes
				# Save Sunni caliph or previous one as scope:struggle_liege
				fp3_save_sunni_caliph_or_previous_effect = yes

				fp3_end_persian_struggle_effect = {
					ENDING = yes
					EVENT = fp3_struggle.0082
					FLAG = flag:fp3_struggle_concession_ending
				}
			}
		}
	}
}
