﻿### an invader appears
spawn_timurid_invader_effect = {
	random_county_in_region = {
		region = special_$INVADER_DYNN$_conquest_region_start
		title_province = { save_scope_as = invader_spawn_location }
	}
	if = {
		limit = {
			has_game_rule = default_timurid_invasion
		}
		hi_determine_county_faiths_in_region_effect = { # saves scope most_popular_region_faith
			FAITH_REGION = special_$INVADER_DYNN$_conquest_region_faith
			FAITH_RESTRICTIONS = "NOT = { has_doctrine_parameter = unreformed }"
			FAITH_POPULARITY = 0 # 0 = 1st, 1 = 2nd, ...
		}
	}
	else = {
		faith:maturidi = {
			save_scope_as = most_popular_region_faith
		}
	}

	if = {
		limit = { has_game_rule = inversed_gender_equality }
		create_character = {
			gender = female
			location = scope:invader_spawn_location
			template = saray_mulkh_character_template
			faith = scope:most_popular_region_faith
			save_scope_as = invader
		}
	}
	else_if = {
		limit = { has_game_rule = full_gender_equality }
		random_list = {
			#Female Timur.
			50 = {
				create_character = {
					gender = female
					location = scope:invader_spawn_location
					template = saray_mulkh_character_template
					faith = scope:most_popular_region_faith
					save_scope_as = invader
				}
			}
			#Male Timur.
			50 = {
				create_character = {
					location = scope:invader_spawn_location
					template = timur_timurid_character_template
					faith = scope:most_popular_region_faith
					save_scope_as = invader
				}
			}
		}
	}
	else = {
		create_character = {
			location = scope:invader_spawn_location
			template = timur_timurid_character_template
			faith = scope:most_popular_region_faith
			save_scope_as = invader
		}
	}

	scope:invader = {
		add_trait = true_heir_of_genghis
		
		add_gold = 5000
		add_prestige = 8000
		add_piety = 150
		
		dynasty = {
			set_dynasty_name = hi_dynn_timurid
			set_coa = title:e_timurid

			add_dynasty_prestige_level = 5
			add_dynasty_prestige = 20250
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			set_house_name = hi_dynn_timurid
		}
	}

	every_player = {
		trigger_event = $INVADER_DYNN$_invasion.1001 # invader title has spawned notification
	}
}

timurid_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
	add_dynasty_perk = warfare_legacy_2
	add_dynasty_perk = warfare_legacy_3
	add_dynasty_perk = warfare_legacy_4
	add_dynasty_perk = warfare_legacy_5
	add_dynasty_perk = glory_legacy_1
	if = {
		limit = { has_ep1_dlc_trigger = yes } # Royal Court
		add_dynasty_perk = ep1_culture_legacy_1
		add_dynasty_perk = ep1_culture_legacy_2
		add_dynasty_perk = ep1_culture_legacy_3
	}
	else = {
		add_dynasty_perk = erudition_legacy_1
		add_dynasty_perk = erudition_legacy_2
		add_dynasty_perk = erudition_legacy_3
	}
}

timurid_maintenance_effect = {
	apply_invader_modifiers_effect = {
		INVADER_DYNN = $INVADER_DYNN$
	}
	give_invader_land_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		STARTING_TIER = $STARTING_TIER$
	}
	spawn_$INVADER_DYNN$_family_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
	}
	spawn_invader_servants_effect = {
		INVADER_CHAR = $INVADER_CHAR$
	}
	form_the_invader_title_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		INVADER_TITLE = $INVADER_TITLE$
		LANDLESS_YES_NO = $LANDLESS_YES_NO$
	}
}

### populating the life of the invader character
spawn_timurid_family_effect = {
	give_nickname = nick_hi_the_lame
	# spouse
	if = {
		limit = {
			$INVADER_CHAR$ = { is_female = yes }
		}
		# Create timur (husband)...
		create_character = {
			template = timur_timurid_character_template
			employer = $INVADER_CHAR$
			save_scope_as = saray_mulk
		}
	}
	else = {
		# Create saray_mulk (wife)...
		create_character = {
			template = saray_mulkh_character_template
			gender = female
			employer = $INVADER_CHAR$
			save_scope_as = saray_mulk
		}
	}
	scope:saray_mulk = {
		marry = $INVADER_CHAR$
		disease_immunity_short_effect = yes
	}
	if = {
		limit = {
			$INVADER_CHAR$ = { is_male = yes }
		}
		$INVADER_CHAR$ = { save_temporary_scope_as = $INVADER_DYNN$_family_father }
		scope:saray_mulk = { save_temporary_scope_as = $INVADER_DYNN$_family_mother }
	}
	else = {
		$INVADER_CHAR$ = { save_temporary_scope_as = $INVADER_DYNN$_family_mother }
		scope:saray_mulk = { save_temporary_scope_as = $INVADER_DYNN$_family_father }		
	}

	### Family
	# Create Umar (son)...
	create_character = {
		template = umar_timurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:$INVADER_DYNN$_family_mother
		father = scope:$INVADER_DYNN$_family_father
		save_scope_as = umar
	}
	scope:umar = {
		disease_immunity_short_effect = yes

		#Make it more likely that he gets a good education
		hidden_effect = {
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}

	# Create jahangir (son)...
	create_character = {
		template = jahangir_timurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:$INVADER_DYNN$_family_mother
		father = scope:$INVADER_DYNN$_family_father
		save_scope_as = jahangir
	}
	scope:jahangir = {
		#Make it more likely that he gets a good education
		hidden_effect = {
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}

	# Create Miran (son)...
	create_character = {
		template = miran_timurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:$INVADER_DYNN$_family_mother
		father = scope:$INVADER_DYNN$_family_father
		save_scope_as = miran
	}
	scope:miran = {
		disease_immunity_short_effect = yes

		#Make it more likely that he gets a good education
		hidden_effect = {
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}

	# Create Rukh (son)...
	create_character = {
		template = rukh_timurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:$INVADER_DYNN$_family_mother
		father = scope:$INVADER_DYNN$_family_father
		save_scope_as = rukh
	}
	scope:rukh = {
		disease_immunity_short_effect = yes

		#Make it more likely that he gets a good education
		hidden_effect = {
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}

	# Create Tohun (Secondary Wive)...
	create_character = {
		template = tohun_character_template
		employer = $INVADER_CHAR$
		gender = female
		save_scope_as = tohun
	}
	scope:tohun = {
		if = {
			limit = { $INVADER_CHAR$ = { is_male = yes } }
			marry = $INVADER_CHAR$
		}
		disease_immunity_short_effect = yes
	}

	# Create Turmish (Secondary Wive)...
	create_character = {
		template = turmish_character_template
		employer = $INVADER_CHAR$
		gender = female
		save_scope_as = turmish
	}
	scope:turmish = {
		if = {
			limit = { $INVADER_CHAR$ = { is_male = yes } }
			marry = $INVADER_CHAR$
		}
		disease_immunity_short_effect = yes
	}

	# Create Mengli (Secondary Wive)...
	create_character = {
		template = mengli_character_template
		employer = $INVADER_CHAR$
		gender = female
		save_scope_as = mengli
	}
	scope:mengli = {
		if = {
			limit = { $INVADER_CHAR$ = { is_male = yes } }
			marry = $INVADER_CHAR$
		}
		disease_immunity_short_effect = yes
	}

	# Create Taghay (Secondary Wive)...
	create_character = {
		template = taghay_character_template
		employer = $INVADER_CHAR$
		gender = female
		save_scope_as = taghay
	}
	scope:taghay = {
		# if = {
		# 	limit = { $INVADER_CHAR$ = { is_male = yes } }
		# 	marry = $INVADER_CHAR$
		# }
		disease_immunity_short_effect = yes
	}

	### others
	create_character = {
		template = hafez_character_template
		employer = $INVADER_CHAR$
		save_scope_as = hafez
	}
	scope:hafez = {
		disease_immunity_short_effect = yes
		# Set friendship with invader
		set_random_friendship_reason = { TARGET = $INVADER_CHAR$ }
	}
}

# Make sure invader has troops
spawn_timurid_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_timurid_army_effect = yes
		spawn_timurid_army_effect = yes
		spawn_timurid_army_effect = yes

	}
	spawn_timurid_army_effect = yes
	spawn_timurid_army_effect = yes
	spawn_timurid_army_effect = yes
	if = {
		limit = { game_start_date < 1000.1.1 }
		spawn_timurid_army_effect = yes
	}
}
spawn_timurid_army_effect = {
	spawn_army = {
		name = timurid_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no

		levies = 250
		men_at_arms = {
			type = horse_archers
			stacks = 24
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 20
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = ayyar
			stacks = 3
		}
		# siege
		men_at_arms = {
			type = bombard
			stacks = 17
		}
	}
}