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

	# family
	create_character = {
		template = sabbah_sabbah_character_template
		dynasty = generate
		location = scope:invader_spawn_location
		save_scope_as = sabbah_sabbah
	}
	create_character = {
		template = muhammad_i_sabbah_character_template
		dynasty = inherit
		father = scope:sabbah_sabbah
		location = scope:invader_spawn_location
		save_scope_as = muhammad_i_sabbah
	}
	create_character = {
		template = husayn_sabbah_character_template
		dynasty = inherit
		father = scope:muhammad_i_sabbah
		location = scope:invader_spawn_location
		save_scope_as = husayn_sabbah
	}
	create_character = {
		template = jafar_sabbah_character_template
		dynasty = inherit
		father = scope:husayn_sabbah
		location = scope:invader_spawn_location
		save_scope_as = jafar_sabbah
	}
	create_character = {
		template = muhammad_ii_sabbah_character_template
		dynasty = inherit
		father = scope:jafar_sabbah
		location = scope:invader_spawn_location
		save_scope_as = muhammad_ii_sabbah
	}
	create_character = {
		template = ali_sabbah_character_template
		dynasty = inherit
		father = scope:muhammad_ii_sabbah
		location = scope:invader_spawn_location
		save_scope_as = ali_sabbah
	}

	# INVADER
	create_character = {
		template = hassan_sabbah_character_template
		dynasty = inherit
		father = scope:ali_sabbah
		location = scope:invader_spawn_location
		save_scope_as = invader
	}

	scope:invader = {
		add_gold = 500
		add_prestige = 500
		add_piety = 200
		
		dynasty = {
			set_dynasty_name = hi_dynn_sabbah
			set_coa = title:k_sabbah

			add_dynasty_prestige_level = 1
			add_dynasty_prestige = 1500
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			set_house_name = hi_dynn_sabbah
		}
	}

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

sabbah_add_dynasty_perks_effect = {
	add_dynasty_perk = guile_legacy_1
}

sabbah_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_sabbah_family_effect = {
	$INVADER_CHAR$ = { trigger_event = game_rule.1001 }
	# spawn loyal assassins
	create_character = {
		template = hi_generic_assassin_template
		gender_female_chance = historicinvasions_soldier_female_chance
		dynasty = none
		employer = $INVADER_CHAR$
		after_creation = { 
			add_opinion = {
				target = $INVADER_CHAR$
				modifier = loyal_servant
			}
		}
	}
	create_character = {
		template = hi_generic_assassin_template
		gender_female_chance = historicinvasions_soldier_female_chance
		dynasty = none
		employer = $INVADER_CHAR$
		after_creation = { 
			add_opinion = {
				target = $INVADER_CHAR$
				modifier = loyal_servant
			}
		}
	}
	create_character = {
		template = hi_generic_assassin_template
		gender_female_chance = historicinvasions_soldier_female_chance
		dynasty = none
		employer = $INVADER_CHAR$
		after_creation = { 
			add_opinion = {
				target = $INVADER_CHAR$
				modifier = loyal_servant
			}
		}
	}
}

# Make sure invader has troops
spawn_sabbah_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_sabbah_army_effect = yes
	}
	spawn_sabbah_army_effect = yes
}
spawn_sabbah_army_effect = {
	spawn_army = {
		name = sabbah_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = light_horsemen
			stacks = 5
		}
		men_at_arms = {
			type = bowmen
			stacks = 3
		}
		men_at_arms = {
			type = light_footmen
			stacks = 7
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}