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

	# father
	create_character = {
		template = bajkam_ghaznavid_character_template
		location = scope:invader_spawn_location
		save_scope_as = bajkam_ghaznavid
	}

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

	scope:invader = {
		add_gold = 4500
		add_prestige = 2000
		add_piety = 150
		
		dynasty = {
			set_dynasty_name = hi_dynn_$INVADER_DYNN$
			set_coa = title:e_$INVADER_DYNN$

			add_dynasty_prestige_level = 2
			add_dynasty_prestige = 3500

			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			set_house_name = hi_dynn_$INVADER_DYNN$
		}
	}

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

ghaznavid_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
	add_dynasty_perk = warfare_legacy_2
	add_dynasty_perk = warfare_legacy_3
}

ghaznavid_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_ghaznavid_family_effect = {
	# create wife's father
	create_character = {
		template = abu_ishaq_ghaznavid_character_template
		employer = $INVADER_CHAR$
		save_scope_as = sibel_father
	}
	scope:sibel_father = {
		dynasty = {
			set_dynasty_name = hi_dynn_ghanzi
			set_coa = house_ghanzi
		}
		house = {
			set_house_name = hi_dynn_ghanzi
		}
	}

	# Sibel (wife)
	create_character = {
		template = sibel_ghaznavid_character_template
		dynasty = inherit
		gender = female
		employer = $INVADER_CHAR$
		father = scope:sibel_father
		save_scope_as = sibel_ghanzi
	}
	scope:sibel_ghanzi = {
		marry = $INVADER_CHAR$
		disease_immunity_short_effect = yes
	}

	# Ismail (son)
	create_character = {
		template = ismail_ghaznavid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:sibel_ghanzi
		father = $INVADER_CHAR$
		save_scope_as = ismail
	}
	scope:ismail = {
		#Make it more likely that he gets a good education
		hidden_effect = { # causes errors...
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}

	# Mahmud (son)
	create_character = {
		template = mahmud_ghaznavid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:sibel_ghanzi
		father = $INVADER_CHAR$
		save_scope_as = mahmud
	}
	scope:mahmud = {
		#Make it more likely that he gets a good education
		hidden_effect = { # causes errors...
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}

	# Mahmud (INVADER brother)
	create_character = {
		template = bughrachuq_ghaznavid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father
	}
}

# Make sure invader has troops
spawn_ghaznavid_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_ghaznavid_army_effect = yes
	}
	spawn_ghaznavid_army_effect = yes
	spawn_ghaznavid_army_effect = yes
}
spawn_ghaznavid_army_effect = {
	spawn_army = {
		name = ghaznavid_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = horse_archers
			stacks = 9
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 9
		}
		men_at_arms = {
			type = light_footmen
			stacks = 3
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}