﻿restore_badd_fortress_scripted_effect = {
	
	# Buff Ahar 
	title:b_ahar = {
		# Give it an extra building slot.
		title_province = {
			add_province_modifier = extra_building_slot
			add_province_modifier = { #and a mighty defensive buff
				modifier = fp3_mountain_redoubt_modifier
			}
		}	
	}
	title:c_tabriz = { # And a decent development boost.
		change_development_level = minor_development_level_gain
	}
	# Make it your capital if it wasn't already.
	if = {
		limit = {
			NOT = { capital_county = title:c_tabriz }
		}
		set_realm_capital = title:c_tabriz
	}
	title:b_ahar = { set_capital_barony = yes }		
}

destroy_the_magi_scripted_effect = {

	add_heresiarch_trait_effect = yes

	religion = { save_scope_as = root_religion }

	faith = {
		change_fervor = {
			value = 50
			desc = fervor_gain_magi_destroyed
		}
		if = {
			limit = { 
				AND = {
					has_doctrine = doctrine_spiritual_head 
					has_doctrine = doctrine_theocracy_temporal
				}
			}
			custom_tooltip = replace_the_high_priest_effect_tooltip
			hidden_effect = {
				remove_doctrine = doctrine_spiritual_head
				add_doctrine = doctrine_no_head 		
			}
		}
	}
			

	if = {
		limit = {
			AND = {
				root.religion = religion:zoroastrianism_religion
				OR = {
					has_faith = faith:mazdakism
					has_faith = faith:zurvanism 
				}
			}
		}
		add_prestige = massive_prestige_gain 
	}

 	custom_tooltip = destroy_the_magi_opinion_tooltip 
	hidden_effect = {
 		every_ruler = {
			if = {
				limit = {
					AND = {
						NOR = {
							has_faith = faith:mazdakism
							has_faith = faith:zurvanism 
						}
						religion = religion:zoroastrianism_religion
					}				
				}
				add_opinion = {
					target = root
					modifier = fp3_spawn_of_ahriman_opinion
				}
			}
			else_if = {
				limit = {
					faith = { has_doctrine = doctrine_major_branch_behdin }
				}
				add_opinion = {
					target = root
					modifier = fp3_abolished_priesthood_opinion
				}
			}	
		}
	}	
}

opinion_based_on_sky_burial_viewpoint_scripted_effect = {
	if = {
		limit = {
			OR = {
				faith = { has_doctrine_parameter = sky_burials_active }
				has_trait = compassionate
				has_trait = cynical
				has_trait = trusting 
				has_trait = forgiving 
				AND = {
					NOT = {	faith = { has_doctrine_parameter = sky_burials_active }	}
					ai_compassion >= medium_positive_ai_value
					ai_zeal <= medium_positive_ai_value
				}
				AND = {
					NOT = {	faith = { has_doctrine_parameter = sky_burials_active }	}
					faith = { has_doctrine = doctrine_pluralism_pluralistic }
					ai_zeal <= medium_positive_ai_value
				}
			}	
		}
		add_opinion = { 
			target = root
			opinion = 20
			modifier = fp3_loyal_to_dead_spouse_opinion
		}
	}
	else_if = {
		limit = { ai_zeal > medium_positive_ai_value }
		add_opinion = { 
			target = root
			opinion = -30
			modifier = fp3_blasphemous_practices_opinion 
		}
	}
	else = {
		add_opinion = { 
			target = root
			opinion = -10
			modifier = fp3_strange_rituals_opinion 
		}
	}
}

opinion_based_on_sky_burial_viewpoint_for_relatives_scripted_effect = {
	if = {
		limit = {
			OR = {
				faith = { has_doctrine_parameter = sky_burials_active }
				has_trait = compassionate
				has_trait = cynical
				has_trait = trusting 
				has_trait = forgiving 
				AND = {
					NOT = {	faith = { has_doctrine_parameter = sky_burials_active }	}
					ai_compassion >= medium_positive_ai_value
					ai_zeal <= medium_positive_ai_value
				}
				AND = {
					NOT = {	faith = { has_doctrine_parameter = sky_burials_active }	}
					faith = { has_doctrine = doctrine_pluralism_pluralistic }
					ai_zeal <= medium_positive_ai_value
				}
			}	
		}
		add_opinion = { 
			target = root
			opinion = 20
			modifier = fp3_benevolent_opinion
		}
	}
	else_if = {
		limit = { ai_zeal > medium_positive_ai_value }
		add_opinion = { 
			target = root
			opinion = -30
			modifier = fp3_blasphemous_practices_opinion 
		}
	}
	else = {
		add_opinion = { 
			target = root
			opinion = -10
			modifier = fp3_strange_rituals_opinion 
		}
	}
}



#####################################################################
##### SELJUK INVASION
#####################################################################

## Set up default title
give_seljuk_title_effect = {
	create_dynamic_title = {
		tier = duchy
		name = fp3_seljuk_horde_title_title
		adj = fp3_seljuk_horde_title_adj
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}

	scope:new_title = {
		set_capital_county = scope:seljuk_birthplace.county
		set_landless_title = yes
		set_coa = house:house_seljuk
		set_destroy_if_invalid_heir = yes
		set_destroy_on_gain_same_tier = yes
		set_delete_on_destroy = yes
		set_no_automatic_claims = yes
		set_definitive_form = yes
		set_can_be_named_after_dynasty = no
		set_can_use_nomadic_naming = no
		change_title_holder = {
			holder = scope:lead_seljuk
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
}

### A leader is created
spawn_seljuk_character_effect = {
	## CULTURE SELECTION
	# Look for a random culture in the region, weight up Oghuz with it also as a fallback
	random_county_in_region = {
		region = dlc_fp3_seljuk_birth_region
		limit = {
			exists = culture
		}
		weight = {
			base = 1
			modifier = {
				culture = culture:turkish
				add = 100
			}
			modifier = {
				culture = { has_cultural_pillar = heritage_turkic }
				add = 50
			}
		}
		culture = { save_scope_as = seljuk_birth_culture }
	}

	## FAITH SELECTION
	# Create a list of possible faiths
	every_county_in_region = {
		region = world_steppe_west
		title_province.faith = { add_to_list = seljuk_faith_selection_list }
	}
	# Find a religion in the wider region. weight it up if a county with the culture has that religion
	random_in_list = {
		list = seljuk_faith_selection_list
		weight = {
			base = 1
			# Upweight the faith of the culture if it is in their historical birth place
			modifier = {
				add = 50
				save_temporary_scope_as = temp_faith
				scope:seljuk_birth_culture = {
					any_culture_county = {
						faith = scope:temp_faith
						title_province = { geographical_region = dlc_fp3_seljuk_birth_region }
					}
				}
			}
			# Upweight Islamic faiths since it's historical (Maturidi)
			modifier = {
				religion_tag = islam_religion
				add = 5
			}
			# Exclude pagan faiths
			modifier = {
				has_doctrine_parameter = unreformed
				factor = 0 
			}
		}
		save_scope_as = seljuk_birth_faith
	}
	random_county_in_region = {
		region = dlc_fp3_seljuk_birth_region
		weight = {
			base = 1
			modifier = {
				culture = scope:seljuk_birth_culture
				add = 2
			}
			modifier = {
				faith = scope:seljuk_birth_faith
				add = 2
			}
		}
		random_county_province = { save_scope_as = seljuk_birthplace }
	}

	create_character = {
		age = { 20 40 }
		location = scope:seljuk_birthplace
		gender_female_chance = fp3_chance_female_leader
		culture = scope:seljuk_birth_culture
		faith = scope:seljuk_birth_faith
		trait = brave
		trait = ambitious
		trait = gregarious
		trait = education_martial_5
		trait = open_terrain_expert
		trait = warlike_court_2
		martial = { 16 21 }
		stewardship = { 12 15 }
		diplomacy = { 11 14 }
		intrigue = { 10 13 }
		learning = { 9 11 }
		prowess	 = { 18 24 }

		dynasty_house = house:house_seljuk

		after_creation = {
			set_variable = {
				name = seljuk_birthplace
				value = scope:seljuk_birthplace
			}
			give_seljuk_title_effect = yes
			add_gold =  { medium_gold_min_value major_gold_max_value }
			add_prestige = { medium_prestige_value massive_prestige_value }
			add_piety = { medium_piety_value massive_piety_value }
			add_martial_lifestyle_xp = 10000
			random = {
				chance = 25
				add_trait = athletic
			}

			add_character_flag = is_seljuk
			game_rule_create_spouse_and_children = { CHARACTER = this LOCATION = this } #to make sure there is someone to continue the invasions
		}
		save_scope_as = lead_seljuk
	}
}

try_to_settle_the_seljuk_effect = {
	# Search for a character who can host the tribe
	save_scope_as = lead_seljuk
	
	if = { # # Attempt to settle them in a duchy within the d_nishapur region, where the conflict historically started. Starting here to save the Seljuks the trip from the Oghuz-Il region to Persia proper, then attempt to settle them in a suitable duchy title
		limit = {
			any_county_in_region = {
				region = world_persian_empire
				de_jure_liege = {
					trigger_if = {
						limit = { exists = holder }
						holder = { is_ai = yes }
					}
					save_temporary_scope_as = duchy_temp
				}
				holder.top_liege = { capital_county.de_jure_liege != scope:duchy_temp }
			}
		}
		random_county_in_region = {
			region = world_persian_empire
			limit = {
				de_jure_liege = {
					this = title:d_nishapur
					trigger_if = {
						limit = { exists = holder }
						holder = { is_ai = yes }
					}
					save_temporary_scope_as = duchy_temp
				}
				holder.top_liege = { capital_county.de_jure_liege != scope:duchy_temp }
			}
			alternative_limit = {
				de_jure_liege = {
					de_jure_liege = title:k_khorasan
					trigger_if = {
						limit = { exists = holder }
						holder = { is_ai = yes }
					}
					save_temporary_scope_as = duchy_temp
				}
				holder.top_liege = { capital_county.de_jure_liege != scope:duchy_temp }
			}
			alternative_limit = {
				de_jure_liege = {
					trigger_if = {
						limit = { exists = holder }
						holder = { is_ai = yes }
					}
					save_temporary_scope_as = duchy_temp
				}
				holder.top_liege = { capital_county.de_jure_liege != scope:duchy_temp }
			}
			weight = {
				base = 1
				modifier = {
					holder.top_liege = { save_temporary_scope_as = top_liege_temp }
					any_neighboring_county = {
						holder.top_liege != scope:top_liege_temp
					}
				}
			}
			de_jure_liege = { save_scope_as = fp3_seljuk_settle_target }

			debug_log = "Attempting to settle seljuk in random duchy"

			holder.top_liege = { trigger_event = fp3_story_cycle_seljuks_invasion_events.0002 }
		}
	}
	else = { # nobody to settle, then attack!
		start_wars_for_seljuk_empire_effect = yes
		debug_log = "Attempting to settle seljuk by force"
	}
}

### Start war of conquest for the Seljuk heartland
start_wars_for_seljuk_empire_effect = {
	save_temporary_scope_as = seljuk_conqueror
	# Build a list of potential targets
	every_independent_ruler = {
		limit = {
			any_sub_realm_county = {
				title_province = { geographical_region = world_persian_empire }
			}
		}
		if = {
			limit = { NOT = { is_in_list = top_liege_targets } }
			add_to_temporary_list = top_liege_targets
		}
	}
	ordered_in_list = {
		list = top_liege_targets
		# Find the weakest target
		order_by = {
			value = current_military_strength
			# Inver the list so the weakest is at the top
			multiply = -1
		}
		save_temporary_scope_as = new_war_target
		primary_title = { save_temporary_scope_as = war_target_title }
		# Declare the war
		scope:seljuk_conqueror = {
			start_war = {
				cb = fp3_seljuk_invasion_cb
				target = scope:new_war_target
				target_title = scope:war_target_title
			}
		}
	}
}

# focus on forming a custom persian empire and freeing turkish people first
seljuk_war_target_evaluation_and_declaration_effect = {
	# Set the Seljuk leader as reference point
	save_scope_as = lead_seljuk

	#if the seljuk is a vassal he will try to fight other vassals first
	if = {
		limit = {
			scope:lead_seljuk = {
				top_liege != this
				liege = {
					any_vassal = {
						highest_held_title_tier > tier_barony
						NOR = {
							is_allied_to = scope:lead_seljuk
							has_usable_hook = scope:lead_seljuk
							this = scope:lead_seljuk
						}	
					}
				}
			}
		}
		#if you can't attack vassals, due to crown laws, then do a liberty faction instead
		if = {
			limit = {
				liege = {
					OR = {
						has_realm_law = crown_authority_2
						has_realm_law = crown_authority_3
					}
				}
				can_join_or_create_faction_against = {
					who = liege
					faction = liberty_faction
				}
			}
			#join one if it already exists
			if = {
				limit = {
					liege = {
						any_targeting_faction = {
							faction_type = liberty_faction
						}
					}
				}					
				liege = {
					random_targeting_faction = {
						faction_type = liberty_faction
						save_temporary_scope_as = liberty_faction
					}
				}
				scope:lead_seljuk = {
					join_faction = scope:liberty_faction
				}
				#debug_log = "Seljuk joined liberty faction"
			}
			#otherwise create it
			else = {
				create_faction = {
					type = liberty_faction
					target = liege
				}	
				#debug_log = "Seljuk created liberty faction"
			}
		}
		#if you can attack, do it
		else = {
			scope:lead_seljuk = {
				liege = {
					random_vassal = {
						limit = {
							#don't try to attack barons
							highest_held_title_tier > tier_barony
							#don't try to attack anyone that you shouldn't want to
							NOT = {
								is_allied_to = scope:lead_seljuk
								has_usable_hook = scope:lead_seljuk
								this = scope:lead_seljuk
							}
						}
						weight = {
							base = 1
							#attack the weaklings first
							modifier = {
								add = {
									value = current_military_strength
									multiply = -1
								}
							}
							#focus on the closest to you
							modifier = {
								add = 1000
								any_held_county = {
									any_neighboring_county = {
										holder = scope:lead_seljuk
									}
								}
							}
							#attack mainly those inside the persian empire
							modifier = {
								add = 1000
								any_realm_province = {
									geographical_region = world_persian_empire
								}
							}
						}
						save_temporary_scope_as = next_invasion_target
						primary_title = {
							duchy = {
								save_temporary_scope_as = next_invasion_title_target
							}
						}
					}
				}
			}
			#debug_log = "Seljuk attacked a vassal"
		}
	}
	# Select a new target
	else_if = { # Check if a direct neighbor is a valid war target
		limit = {
			any_neighboring_and_across_water_top_liege_realm_owner = {
				NOR = {
					is_allied_to = scope:lead_seljuk
					this = scope:lead_seljuk
					any_liege_or_above = {
						this = scope:lead_seljuk.liege
					}
				}
				save_temporary_scope_as = truce_check
				NOT = {
					scope:lead_seljuk = {
						any_truce_target = {
							this = scope:truce_check
						}
					}
				}
				any_sub_realm_county = {
					title_province = { geographical_region = world_persian_empire }
				}
			}
		}
		random_neighboring_and_across_water_top_liege_realm_owner = {
			limit = {
				NOR = {
					is_allied_to = scope:lead_seljuk
					this = scope:lead_seljuk
					any_liege_or_above = {
						this = scope:lead_seljuk.liege
					}
				}
				save_temporary_scope_as = truce_check
				NOT = {
					scope:lead_seljuk = {
						any_truce_target = {
							this = scope:truce_check
						}
					}
				}
				any_sub_realm_county = {
					title_province = { geographical_region = world_persian_empire }
				}
			}
			weight = {
				base = 1
				#attack the weaklings first
				modifier = {
					add = {
						value = current_military_strength
						multiply = -1
					}
				}
				#focus on the closest to you
				modifier = {
					add = 1000
					any_held_county = {
						any_neighboring_county = {
							holder = scope:lead_seljuk
						}
					}
				}
			}
			save_temporary_scope_as = next_invasion_target
			random_sub_realm_county = {
				limit = {
					title_province = { geographical_region = world_persian_empire }				
				}
				weight = {
					base = 1
					#pick a neighboring county if possible
					modifier = {
						add = 1000
						is_neighbor_to_realm = scope:lead_seljuk
					}
				}
				duchy = {
					save_temporary_scope_as = next_invasion_title_target
				}
			}
			#debug_log = "Seljuk attacked a neighbor"
		}
	}
	else_if = { # if no direct neighbors are valid war targets, then check the neighbors of the neighbors
		limit = {
			any_neighboring_and_across_water_top_liege_realm_owner = {
				any_neighboring_and_across_water_top_liege_realm_owner = {
					NOR = {
						is_allied_to = scope:lead_seljuk
						this = scope:lead_seljuk
						any_liege_or_above = {
							this = scope:lead_seljuk.liege
						}
					}
					save_temporary_scope_as = truce_check
					NOT = {
						scope:lead_seljuk = {
							any_truce_target = {
								this = scope:truce_check
							}
						}
					}
					any_sub_realm_county = {
						title_province = { geographical_region = world_persian_empire }
					}
				}
			}
		}
		random_neighboring_and_across_water_top_liege_realm_owner = {
			random_neighboring_and_across_water_top_liege_realm_owner = {
				limit = {
					NOR = {
						is_allied_to = scope:lead_seljuk
						this = scope:lead_seljuk
						any_liege_or_above = {
							this = scope:lead_seljuk.liege
						}
					}
					save_temporary_scope_as = truce_check
					NOT = {
						scope:lead_seljuk = {
							any_truce_target = {
								this = scope:truce_check
							}
						}
					}
					any_sub_realm_county = {
						title_province = { geographical_region = world_persian_empire }
					}
				}
				weight = {
					base = 1
					#attack the weaklings first
					modifier = {
						add = {
							value = current_military_strength
							multiply = -1
						}
					}
					#focus on the closest to you
					modifier = {
						add = 1000
						any_held_county = {
							any_neighboring_county = {
								holder = scope:lead_seljuk
							}
						}
					}
				}
				save_temporary_scope_as = next_invasion_target
				random_sub_realm_county = {
					limit = {
						title_province = { geographical_region = world_persian_empire }
					}
					weight = {
						base = 1
						#pick a neighboring county if possible
						modifier = {
							add = 1000
							is_neighbor_to_realm = scope:lead_seljuk
						}
					}
					duchy = {
						save_temporary_scope_as = next_invasion_title_target
					}
				}
				#debug_log = "Seljuk attacked a neihgbor's neighbor"
			}
		}
	}
	if = {
		limit = {
			exists = scope:next_invasion_target
		}
		start_war = {
			cb = fp3_seljuk_invasion_cb
			target = scope:next_invasion_target
			target_title = scope:next_invasion_title_target
		}
	}
}

# Fight for independence
seljuk_request_independence_from_liege_effect = {
	save_scope_as = lead_seljuk
	scope:lead_seljuk = {
		if = {
			limit = {
				liege = {
					any_targeting_faction = {
						faction_type = independence_faction
					}
				}
			}
			run_interaction = {
				interaction = join_independence_faction_interaction
				actor = scope:lead_seljuk
				recipient = liege
				execute_threshold = decline
			}
		}
		else_if = {
			limit = {
				can_create_faction = {
					target = liege
					type = independence_faction
				}
			}
			run_interaction = {
				interaction = start_independence_faction_interaction
				actor = scope:lead_seljuk
				recipient = liege
				execute_threshold = decline
			}
		}
		#debug_log_scopes = yes
		#debug_log = "Seljuk asked for independence"
		liege = {
			random_targeting_faction = {
				faction_type = independence_faction
				faction_start_war = {}
			}
		}
	}
}

### Zanj Rebellion Effects

#This is Ali Ibn Muhammad, leader of the Zanj Rebellion
spawn_zanj_leader_character_effect = {
	create_character = {
		age = { 55 65 }
		name = "Muhammad"
		gender = male
		faith = faith:zayidi
		location = title:c_basra.title_province
		culture = culture:bedouin

		dynasty = none

		save_scope_as = zanj_leader_father
	}

	create_character = {
		age = { 55 65 }
		gender = female
		faith = faith:azariqa
		location = title:c_basra.title_province
		culture = culture:bedouin

		dynasty = none

		save_scope_as = zanj_leader_mother
	}

	create_character = {
		age = { 25 35 }
		name = "Ali"
		gender = male
		faith = faith:zayidi
		location = title:c_basra.title_province
		culture = culture:bedouin
		father = scope:zanj_leader_father
		mother = scope:zanj_leader_mother
		dynasty = none

		trait = education_intrigue_3
		trait = ambitious
		trait = deceitful
		trait = just
		trait = shrewd
		trait = peasant_leader

		random_traits = no
		#Skills
		learning = {
			min_template_high_skill
			max_template_high_skill
		}
		martial = {
			min_template_average_skill
			max_template_average_skill
		}
		prowess = {
			min_template_low_skill
			max_template_low_skill
		}
		diplomacy = {
			min_template_average_skill
			max_template_average_skill
		}
		intrigue = {
			min_template_high_skill
			max_template_high_skill
		}
		stewardship = {
			min_template_low_skill
			max_template_low_skill
		}
		save_scope_as = zanj_leader
	}
}

spawn_zanj_representative_character_effect = {
	create_character = {
		age = { 30 40 }
		gender_female_chance = 50
		faith = faith:azariqa
		location = title:c_basra.title_province
		culture = culture:east_bantu

		dynasty = none

		trait = education_martial_3
		trait = organizer
		trait = brave
		trait = peasant_leader
		random_traits_list = {
			count = 2
			ambitious = {}
			gregarious = {}
			trusting = {}
			wrathful = {}
			just = {}
			compassionate = {}
			honest = {}
			vengeful = {}
		}
		#Skills
		learning = {
			min_template_low_skill
			max_template_average_skill
		}
		martial = {
			min_template_average_skill
			max_template_average_skill
		}
		prowess = {
			min_template_low_skill
			max_template_average_skill
		}
		diplomacy = {
			min_template_average_skill
			max_template_average_skill
		}
		intrigue = {
			min_template_low_skill
			max_template_average_skill
		}
		stewardship = {
			min_template_low_skill
			max_template_average_skill
		}
		save_scope_as = zanj_representative
	}
}

fp3_start_zanj_rebellion_effect = {

	$REBEL_LEADER$ = { save_scope_as = rebel_leader }
	$TARGET_COUNTY$ = { save_scope_as = target_county }
	#Give the zanj leader a title
	create_dynamic_title = {
		tier = duchy
		name = fp3_zanj_rebellion_name
		adj = fp3_zanj_rebellion_name_adj
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	scope:new_title = {
		set_capital_county = scope:target_county
		set_landless_title = yes
		set_destroy_on_gain_same_tier = yes
		set_no_automatic_claims = yes
		set_can_be_named_after_dynasty = no
		set_can_use_nomadic_naming = no
		change_title_holder = {
			holder = scope:rebel_leader
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
	scope:new_title = {
		generate_coa = yes
		set_variable = {
			name = temporary_title
			value = yes
		}
	}
	#Declare the war.
	scope:rebel_leader = {
		start_war = {
			cb = fp3_zanj_rebellion_war
			target = scope:target_county.holder.top_liege
			target_title = scope:target_county.duchy
		}
	}
	# Setup event troops.
	scope:rebel_leader = {
		spawn_army = {
			levies = zanj_rebellion_strength_score_levy
			men_at_arms = {
				type = pikemen_unit
				stacks = zanj_rebellion_strength_score_pikemen
			}
			men_at_arms = {
				type = bowmen
				stacks = zanj_rebellion_strength_score_bowmen
			}
			men_at_arms = {
				type = light_footmen
				stacks = zanj_rebellion_strength_score_skirmishers
			}
			location = province:6022
			origin = province:6022
			inheritable = yes
			name = fp3_zanj_rebel_army_title
		}
	}
}

create_zanj_courtier_effect = {
	hidden_effect = {
		create_character = {
			location = $LOCATION$
			faith = faith:azariqa
			culture = culture:east_bantu
			dynasty = none
			age = { 18 30 }
			save_scope_as = new_zanj_courtier
			gender_female_chance = 50
		}
		scope:new_zanj_courtier = {
			set_employer = $EMPLOYER$
		}
	}
}

create_zanj_vassal_effect = {
	hidden_effect = {
		create_character = {
			faith = faith:azariqa
			culture = culture:east_bantu
			gender_female_chance = 50
			age = { 18 30 }
			trait = peasant_leader
			save_scope_as = new_zanj_vassal
			location = $LOCATION$
		}
	}
}

inherit_zanj_rebellion_effect = {
	scope:zanj_leader = { save_scope_as = old_zanj_leader }
	spawn_zanj_representative_character_effect = yes
	scope:zanj_representative = { save_scope_as = zanj_leader }

	scope:zanj_leader = {
		set_variable = {
			name =  zanj_rebellion_strength_score
			value = 7
		}
		add_character_flag = new_zanj_leader
		save_scope_as = story_owner
	}
}


create_seljuk_army_effect = {
	scope:lead_seljuk = { # Give them a sizable amount of troops to begin with so they don't instantly wipe out
		if = {
			limit = { has_dlc_feature = tours_and_tournaments }
			# Give base_level troops that don't replenish
			if = {
				limit = {
					has_mpo_dlc_trigger = yes
				}
				spawn_army = { 
					men_at_arms = {
						type = nomadic_riders
						stacks = {
							value = seljuk_invasion_nomad_strength_score_levy
							multiply = 2
							divide = 100
						}
					}
					men_at_arms = {
						type = horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = accolade_maa_horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = heavy_horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = steppe_raiders
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_minor_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = nomad_lancers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = accolade_maa_outriders
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = onager
						stacks = {
							value = seljuk_invasion_nomad_strength_score_siege_weapon
							multiply = fp3_seljuk_stack_monumental_value
							multiply = scope:victim_strength_modifier
						}
					}
					location = scope:spawn_location
					war = scope:war
					war_keep_on_attacker_victory = yes
					uses_supply = no
					save_scope_as = seljuk_nomad_horde
					name = fp3_seljuk_horde_army_title
				}
			}
			else = {
				spawn_army = { 
					levies = {
						value = seljuk_invasion_nomad_strength_score_levy
						multiply = 2
					}
					men_at_arms = {
						type = horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = accolade_maa_horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = accolade_maa_horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = light_horsemen
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_minor_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = accolade_maa_outriders
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = accolade_maa_outriders
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_miniscule_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = onager
						stacks = {
							value = seljuk_invasion_nomad_strength_score_siege_weapon
							multiply = fp3_seljuk_stack_monumental_value
							multiply = scope:victim_strength_modifier
						}
					}
					location = scope:spawn_location
					war = scope:war
					war_keep_on_attacker_victory = yes
					uses_supply = no
					save_scope_as = seljuk_nomad_horde
					name = fp3_seljuk_horde_army_title
				}
			}				
		}
		else = {
			if = {
				limit = {
					has_mpo_dlc_trigger = yes
				}
				# Give base_level troops that don't replenish
				spawn_army = { 
					men_at_arms = {
						type = nomadic_riders
						stacks = {
							value = seljuk_invasion_nomad_strength_score_levy
							divide = 100
						}
					}
					men_at_arms = {
						type = horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_massive_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = steppe_raiders
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_massive_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = heavy_horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_massive_value
							multiply = scope:victim_strength_modifier
						}
					}
					
					men_at_arms = {
						type = onager
						stacks = {
							value = seljuk_invasion_nomad_strength_score_siege_weapon
							multiply = fp3_seljuk_stack_monumental_value
							multiply = scope:victim_strength_modifier
						}
					}
					location = scope:spawn_location
					war = scope:war
					war_keep_on_attacker_victory = yes
					uses_supply = no
					save_scope_as = seljuk_nomad_horde
					name = fp3_seljuk_horde_army_title
				}
			}
			else = {
				# Give base_level troops that don't replenish
				spawn_army = { 
					levies = {
						value = seljuk_invasion_nomad_strength_score_levy
						multiply = 1
					}
					men_at_arms = {
						type = horse_archers
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_massive_value
							multiply = scope:victim_strength_modifier
						}
					}	
					men_at_arms = {
						type = light_horsemen
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_massive_value
							multiply = scope:victim_strength_modifier
						}
					}
					men_at_arms = {
						type = bowmen
						stacks = {
							value = var:seljuk_invasion_nomad_strength_score
							multiply = fp3_seljuk_stack_massive_value
							multiply = scope:victim_strength_modifier
						}
					}
					
					men_at_arms = {
						type = onager
						stacks = {
							value = seljuk_invasion_nomad_strength_score_siege_weapon
							multiply = fp3_seljuk_stack_monumental_value
							multiply = scope:victim_strength_modifier
						}
					}
					location = scope:spawn_location
					war = scope:war
					war_keep_on_attacker_victory = yes
					uses_supply = no
					save_scope_as = seljuk_nomad_horde
					name = fp3_seljuk_horde_army_title
				}
			}
		}
	}
	scope:seljuk_nomad_horde ?= { assign_commander = scope:lead_seljuk }
}

create_seljuk_army_at_peace_effect = {
	# Give base_level troops that don't replenish
	scope:lead_seljuk = {
		# Give troops to attacker based on strength score counter
		if = {
			limit = { has_dlc_feature = tours_and_tournaments }
			spawn_army = { 
				men_at_arms = {
					type = nomadic_riders
					stacks = {
						value = seljuk_invasion_nomad_strength_score_levy
						divide = 100
					}
				}
				men_at_arms = {
					type = heavy_horse_archers
					stacks = {
						value = var:seljuk_invasion_nomad_strength_score
						multiply = fp3_seljuk_stack_minor_value
						multiply = scope:victim_strength_modifier
					}
				}
				men_at_arms = {
					type = nomad_lancers
					stacks = {
						value = var:seljuk_invasion_nomad_strength_score
						multiply = fp3_seljuk_stack_minor_value
						multiply = scope:victim_strength_modifier
					}
				}
				men_at_arms = {
					type = onager
					stacks = seljuk_invasion_nomad_strength_score_siege_weapon
				}
				location = scope:spawn_location
				origin = scope:spawn_location
				save_scope_as = seljuk_nomad_horde
				name = fp3_seljuk_horde_army_title
			}
		}
		else = {
			spawn_army = { 
				levies = seljuk_invasion_nomad_strength_score_levy
				men_at_arms = {
					type = horse_archers
					stacks = var:seljuk_invasion_nomad_strength_score
				}
				men_at_arms = {
					type = onager
					stacks = seljuk_invasion_nomad_strength_score_siege_weapon
				}
				location = scope:spawn_location
				origin = scope:spawn_location
				uses_supply = no
				save_scope_as = seljuk_nomad_horde
				name = fp3_seljuk_horde_army_title
			}
		}
	}
}

increase_tax_collector_skill = {
	switch = {
		trigger = root.var:skill_to_increase
		flag:stewardship = { add_stewardship_skill = 1 }
		flag:intrigue = { add_intrigue_skill = 1 }
		flag:diplomacy = { add_diplomacy_skill = 1 }
	}
}
