﻿# spawn invader, before story
spawn_qara_qoyunlu_invader_effect = {
	random_county_in_region = {
		region = special_$INVADER_DYNN$_conquest_region_start
		title_province = { save_scope_as = invader_spawn_location }
	}

	# legendary ancestry
	character:1000230100 = { save_scope_as = oghuz_oghuz }
	if = { # create none; the younger one (= both) exists in the history files
		limit = {
			exists = character:1000230131 # Yiwa Oghuz, > 918.1.1
		}
		character:1000230120 = { save_scope_as = tengiz_oghuz }
		character:1000230131 = { save_scope_as = yiwa_oghuz }
	}
	else_if = { # only create Yiwa, only the older one exists in the histry files
		limit = {
			exists = character:1000230120 # Tengiz Oghuz, > 893.1.1
		}
		character:1000230120 = { save_scope_as = tengiz_oghuz }

		create_character = {
			template = yiwa_oghuz_character_template
			dynasty = inherit
			father = scope:tengiz_oghuz
			location = scope:invader_spawn_location
			save_scope_as = yiwa_oghuz
			after_creation = {
				house = {
					if = {
						limit = {
							NOT = { # prevent the main house from being changed
								this = scope:yiwa_oghuz.father.house
							}
						}
						set_house_name = hi_dynn_oghuz_house_yiwa
						set_coa = dynasty_oghuz_house_yiwa_coa
					}
				}
			}
		}
	}
	else = { # create both; none exist in the history files, < 893.1.1
		create_character = {
			template = tengiz_oghuz_character_template
			dynasty = inherit
			father = scope:oghuz_oghuz
			location = scope:invader_spawn_location
			save_scope_as = tengiz_oghuz
			after_creation = {}
		}
		create_character = {
			template = yiwa_oghuz_character_template
			dynasty = inherit
			father = scope:tengiz_oghuz
			location = scope:invader_spawn_location
			save_scope_as = yiwa_oghuz
			after_creation = {
				house = {
					if = {
						limit = {
							NOT = { # prevent the main house from being changed
								this = scope:yiwa_oghuz.father.house
							}
						}
						set_house_name = hi_dynn_oghuz_house_yiwa
						set_coa = dynasty_oghuz_house_yiwa_coa
					}
				}
			}
		}
	}

	# filler for cadet branch creation
	create_character = {
		template = qara_qoyunlu_filler_character_template
		age = 450
		dynasty = inherit
		father = scope:yiwa_oghuz
		location = scope:invader_spawn_location
		save_scope_as = qara_qoyunlu_filler_01
		after_creation = { death = natural }
	}
	create_character = {
		template = qara_qoyunlu_filler_character_template
		age = 400
		dynasty = inherit
		father = scope:qara_qoyunlu_filler_01
		location = scope:invader_spawn_location
		save_scope_as = qara_qoyunlu_filler_02
		after_creation = { death = natural }
	}
	create_character = {
		template = qara_qoyunlu_filler_character_template
		age = 350
		dynasty = inherit
		father = scope:qara_qoyunlu_filler_02
		location = scope:invader_spawn_location
		save_scope_as = qara_qoyunlu_filler_03
		after_creation = { death = natural }
	}
	create_character = {
		template = qara_qoyunlu_filler_character_template
		age = 300
		dynasty = inherit
		father = scope:qara_qoyunlu_filler_03
		location = scope:invader_spawn_location
		save_scope_as = qara_qoyunlu_filler_04
		after_creation = { death = natural }
	}
	create_character = {
		template = qara_qoyunlu_filler_character_template
		age = 250
		dynasty = inherit
		father = scope:qara_qoyunlu_filler_04
		location = scope:invader_spawn_location
		save_scope_as = qara_qoyunlu_filler_05
		after_creation = { death = natural }
	}
	create_character = {
		template = qara_qoyunlu_filler_character_template
		age = 200
		dynasty = inherit
		father = scope:qara_qoyunlu_filler_05
		location = scope:invader_spawn_location
		save_scope_as = qara_qoyunlu_filler_06
		#after_creation = { death = natural } # is killed after the first real ancestor (template) has created their cadet branch
	}

	# ancestors
	create_character = {
		template = mansur_qara_qoyunlu_character_template
		dynasty = inherit
		father = scope:qara_qoyunlu_filler_06
		location = scope:invader_spawn_location
		save_scope_as = mansur_qara_qoyunlu
		after_creation = {}
	}
	create_character = {
		template = bayram_khwaja_qara_qoyunlu_character_template
		dynasty = inherit
		father = scope:mansur_qara_qoyunlu
		location = scope:invader_spawn_location
		save_scope_as = bayram_khwaja_qara_qoyunlu
		after_creation = {}
	}
	create_character = {
		template = dursun_qara_qoyunlu_character_template
		dynasty = inherit
		father = scope:mansur_qara_qoyunlu
		location = scope:invader_spawn_location
		save_scope_as = dursun_qara_qoyunlu
		after_creation = {}
	}
	create_character = {
		template = mahammad_qara_qoyunlu_character_template
		dynasty = inherit
		father = scope:dursun_qara_qoyunlu
		location = scope:invader_spawn_location
		save_scope_as = mahammad_qara_qoyunlu
		after_creation = {}
	}

	# INVADER Qara Yusuf Nuyan
	create_character = {
		template = yusuf_qara_qoyunlu_character_template
		dynasty = inherit
		father = scope:mahammad_qara_qoyunlu
		location = scope:invader_spawn_location
		save_scope_as = invader
		after_creation = {}
	}

	scope:invader = {
		add_gold = 5000
		add_prestige = 2500
		add_piety = 150
		
		dynasty = {
			add_dynasty_prestige = 250
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			if = {
				limit = {
					NOR = { # prevent the main house from being changed
						this = scope:oghuz_oghuz.house
						trigger_if = {
							limit = { exists = character:1000230120 }
							this = character:1000230120.house
						}
					}
				}
				set_coa = title:e_qara_qoyunlu
				set_house_name = hi_dynn_oghuz_house_qara_qoyunlu
			}
		}
	}

	every_player = {
		trigger_event = $INVADER_DYNN$_invasion.1001 # Notification: Start of invasion
	}
}

qara_qoyunlu_add_dynasty_perks_effect = {
}

qara_qoyunlu_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_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_qara_qoyunlu_family_effect = {
	# brother & spouse
	create_character = {
		template = misr_qara_qoyunlu_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father
		save_scope_as = misr_qara_qoyunlu
		after_creation = { disease_immunity_short_effect = yes }
	}
	create_character = {
		age = { 30 40 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = misr_qara_qoyunlu_spouse
		after_creation = {
			marry = scope:misr_qara_qoyunlu
			disease_immunity_short_effect = yes
		}
	}

	# spouse
	create_character = {
		age = { 20 30 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = invader_spouse
		after_creation = {
			marry = $INVADER_CHAR$
			disease_immunity_short_effect = yes
		}
	}

	# children & spouses
	create_character = {
		template = ispend_qara_qoyunlu_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:invader_spouse
		father = $INVADER_CHAR$
		save_scope_as = ispend_qara_qoyunlu
		after_creation = { disease_immunity_short_effect = yes }
	}
	create_character = {
		age = { 18 25 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = ispend_qara_qoyunlu_spouse
		after_creation = {
			marry = scope:ispend_qara_qoyunlu
			disease_immunity_short_effect = yes
		}
	}

	create_character = {
		template = iskander_qara_qoyunlu_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:invader_spouse
		father = $INVADER_CHAR$
		save_scope_as = iskander_qara_qoyunlu
		after_creation = { disease_immunity_short_effect = yes }
	}
	create_character = {
		age = { 18 22 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = iskander_qara_qoyunlu_spouse
		after_creation = {
			marry = scope:iskander_qara_qoyunlu
			disease_immunity_short_effect = yes
		}
	}

	create_character = {
		template = jahan_qara_qoyunlu_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:invader_spouse
		father = $INVADER_CHAR$
		save_scope_as = jahan_qara_qoyunlu
		after_creation = { disease_immunity_short_effect = yes }
	}
	create_character = {
		template = abu_said_qara_qoyunlu_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:invader_spouse
		father = $INVADER_CHAR$
		save_scope_as = abu_said_qara_qoyunlu
		after_creation = { disease_immunity_short_effect = yes }
	}
}

# Make sure invader has troops
spawn_qara_qoyunlu_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_qara_qoyunlu_army_effect = yes
		spawn_qara_qoyunlu_army_effect = yes

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

		levies = 250
		men_at_arms = {
			type = horse_archers
			stacks = 20
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 16
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 8
		}
		# siege
		men_at_arms = {
			type = bombard
			stacks = 14
		}
	}
}