﻿ptt_ext_intermezzo_become_seljuks_effect = {
   #Get buff
	hidden_effect = {
		remove_trait = education_diplomacy_1
		remove_trait = education_diplomacy_2
		remove_trait = education_diplomacy_3
		remove_trait = education_diplomacy_4
		remove_trait = education_diplomacy_5
		remove_trait = education_intrigue_1
		remove_trait = education_intrigue_2
		remove_trait = education_intrigue_3
		remove_trait = education_intrigue_4
		remove_trait = education_intrigue_5
		remove_trait = education_stewardship_1
		remove_trait = education_stewardship_2
		remove_trait = education_stewardship_3
		remove_trait = education_stewardship_4
		remove_trait = education_stewardship_5
		remove_trait = education_learning_1
		remove_trait = education_learning_2
		remove_trait = education_learning_3
		remove_trait = education_learning_4
		remove_trait = education_learning_5
		remove_trait = education_martial_1
		remove_trait = education_martial_2
		remove_trait = education_martial_3
		remove_trait = education_martial_4
	}
	add_trait_force_tooltip = education_martial_5
	add_trait_force_tooltip = warlike_court_2
	add_trait_force_tooltip = conqueror
	if = { limit = { prestige_level < 5 }
		add_prestige_level = 1
	}
	dynasty = { add_dynasty_prestige = 1500 }
}

ptt_ext_intermezzo_dominate_caliph_fame_and_claim = {
	
	add_pressed_claim = title:e_arabia

	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
		}
	}
}

ptt_ext_intermezzo_vassalize_caliph = {
	
	if = {
		limit = { title:d_sunni.holder = { highest_held_title_tier >= kingdom } }
		debug_log = "Caliph has a kingdom"
	
		create_title_and_vassal_change = { # Note: do not attempt to simulate faction logic, it is too volatile
			type = usurped
			save_scope_as = change
		}
		title:d_sunni.holder = {
			every_vassal = {
				add_to_list = vassals_to_transfer
			}
			add_to_list = vassals_to_transfer
		}
		title:d_sunni.holder = {
			every_held_title = {
				limit = { tier >= tier_kingdom }
				change_title_holder_include_vassals = {
					holder = root
					change = scope:change
				}
			}
		}
		title:d_sunni.holder = {
			change_liege = {
				liege = root
				change = scope:change
			}
		}
		hidden_effect = {
			every_in_list = {
				list = vassals_to_transfer
				limit = {
					NOT = { root = liege }
				}
				change_liege = {
					liege = root
					change = scope:change
				}
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
	else = {
		create_title_and_vassal_change = { # Note: do not attempt to simulate faction logic, it is too volatile
			type = usurped
			save_scope_as = change
		}
		title:d_sunni.holder = {
			change_liege = {
				liege = root
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
}

ptt_ext_intermezzo_capital_damascus = {
	hidden_effect = {
		title:e_arabia = {
			set_capital_county = title:c_damascus
		}
	}
	debug_log = "Attempting to move Arabian capital to Damascus"
}

ptt_ext_intermezzo_capital_baghdad = {
	hidden_effect = {
		title:e_arabia = {
			set_capital_county = title:c_baghdad
		}
	}
}

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 
			}
			modifier = {
				religion_tag = christianity_religion
				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 30 }
		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
			add_trait = athletic
			add_trait = whole_of_body
			add_trait = conqueror

			add_character_flag = is_seljuk
			game_rule_create_spouse_and_children = { CHARACTER = 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_khorezm
					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_transoxiana
					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 = {
						NOT = { 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"
	}
}


# 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

	#iv the seljuk is a vassal he will try to fight other vassals first
	if = {
		limit = {
			scope:lead_seljuk = {
				is_independent_ruler = no
				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
						}	
					}
				}
			}
		}
		#iv 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_is_type = liberty_faction
						}
					}
				}					
				liege = {
					random_targeting_faction = {
						limit = {
							faction_is_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"
			}
		}
		#iv 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_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_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 = 1000
				#attack the weaklings first
				modifier = {
					factor = {
						value = 1
						subtract = {
							value = current_military_strength
							divide = 10000
						}
					}
				}
				#focus on the closest to you
				modifier = {
					add = 200
					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_top_liege_realm_owner = {
				any_neighboring_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_top_liege_realm_owner = {
			random_neighboring_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
		}
	}
}