﻿# invader appears, before story
spawn_ziyarid_invader_effect = {
	random_county_in_region = {
		region = special_$INVADER_DYNN$_conquest_region_start
		title_province = { save_scope_as = invader_spawn_location }
	}
	hi_determine_county_faiths_in_region_effect = { # saves scope most_popular_region_faith
		FAITH_REGION = special_$INVADER_DYNN$_conquest_region_faith
		FAITH_RESTRICTIONS = "religion = religion:zoroastrianism_religion"
		FAITH_POPULARITY = 0 # 0 = 1st, 1 = 2nd, ...
	}
	if = { # fallback
		limit = { NOT = { exists = scope:most_popular_region_faith } }
		faith:mazdayasna = { save_scope_as = most_popular_region_faith }
	}

	# father, hist-ID
	if = {
		limit = { exists = character:166987 }
		character:166987 = { save_scope_as = ziyar_ziyarid } # id 7901, hist-id 166987
	}
	else = {
		create_character = {
			template = ziyar_ziyarid_character_template
			dynasty = generate
			faith = scope:most_popular_region_faith
			location = scope:invader_spawn_location
			save_scope_as = ziyar_ziyarid
		}
		scope:ziyar_ziyarid = {
			dynasty = {
				set_dynasty_name = hi_dynn_ziyarid
				set_coa = title:k_ziyarid
			}
			house = {
				set_house_name = hi_dynn_ziyarid
			}
		}
	}
	# INVADER
	create_character = {
		template = mardavij_ziyarid_character_template
		dynasty = inherit
		father = scope:ziyar_ziyarid
		faith = scope:most_popular_region_faith
		location = scope:invader_spawn_location
		save_scope_as = invader
	}
	scope:invader = {
		add_gold = 150
		add_prestige = 250
		add_piety = 850

		dynasty = {
			add_dynasty_prestige_level = 0
			add_dynasty_prestige = 300

			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
	}

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

ziyarid_add_dynasty_perks_effect = {}

ziyarid_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_ziyarid_family_effect = {
	# brother
	create_character = {
		template = vushmgir_ziyarid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father
		faith = $INVADER_CHAR$.faith
		save_scope_as = vushmgir_ziyarid
	}
	# brother spouse
	create_character = {
		template = hi_random_spouse_character_template
		dynasty = none
		opposite_gender = scope:vushmgir_ziyarid
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		after_creation = {
			marry = scope:vushmgir_ziyarid
		}
	}
}

# Make sure invader has troops
spawn_ziyarid_troops_effect = {
	spawn_ziyarid_army_effect = yes
}
spawn_ziyarid_army_effect = {
	spawn_army = {
		name = ziyarid_event_troops
		location = capital_province
		uses_supply = yes
		inheritable = no
		
		levies = 50
		men_at_arms = {
			type = light_footmen
			stacks = 2
		}
		men_at_arms = {
			type = bowmen
			stacks = 1
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 2
		}
	}
}