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

	if = {
		limit = { NOT = { exists = scope:invader } }
		if = {
			limit = { game_start_date = 867.1.1 }
			### create distant, dead ancestors
			character:extra_ghurids_1 = { save_scope_as = shansab_ghurid }
			create_character = {
				template = suri_ghurid_character_template
				dynasty = inherit
				father = scope:shansab_ghurid
				location = scope:invader_spawn_location
				save_scope_as = suri_ghurid
			}
			create_character = {
				template = muhammad_i_ghurid_character_template
				dynasty = inherit
				father = scope:suri_ghurid
				location = scope:invader_spawn_location
				save_scope_as = muhammad_i_ghurid
			}
			create_character = {
				template = muhammad_ii_ghurid_character_template
				dynasty = inherit
				father = scope:muhammad_i_ghurid
				location = scope:invader_spawn_location
				save_scope_as = muhammad_ii_ghurid
			}
			create_character = {
				template = shith_ghurid_character_template
				dynasty = inherit
				father = scope:muhammad_ii_ghurid
				location = scope:invader_spawn_location
				save_scope_as = shith_ghurid
			}
			create_character = {
				template = abbas_ghurid_character_template
				dynasty = inherit
				father = scope:shith_ghurid
				location = scope:invader_spawn_location
				save_scope_as = abbas_ghurid
			}
			create_character = {
				template = muhammad_iii_ghurid_character_template
				dynasty = inherit
				father = scope:abbas_ghurid
				location = scope:invader_spawn_location
				save_scope_as = muhammad_iii_ghurid
			}
			create_character = {
				template = hasan_ghurid_character_template
				dynasty = inherit
				father = scope:muhammad_iii_ghurid
				location = scope:invader_spawn_location
				save_scope_as = hasan_ghurid
			}
		}
		else_if = {
			limit = { game_start_date = 1066.9.15 }
			character:93138 = { save_scope_as = hasan_ghurid }
		}
		else = {
			character:extra_ghurids_1 = { save_scope_as = hasan_ghurid } # shansab ghurid, fallback to create dynasty link
		}
	
		# father is created first so we can have him appear as father
		create_character = {
			template = izz_ghurid_character_template
			dynasty = inherit
			father = scope:hasan_ghurid
			location = scope:invader_spawn_location
			save_scope_as = izz_ghurid
		}
	
		# INVADER
		create_character = {
			template = husayn_ghurid_character_template
			father = scope:izz_ghurid
			dynasty = inherit
			location = scope:invader_spawn_location
			save_scope_as = invader
			after_creation = {
				add_character_flag = spawned_$INVADER_DYNN$
			}
		}
	}

	scope:invader = {
		add_gold = 4500
		add_prestige = 2000
		add_piety = 150
		
		dynasty = {
			add_dynasty_prestige_level = 1
			add_dynasty_prestige = 3000

			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
	}

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

ghurid_add_dynasty_perks_effect = {}

ghurid_maintenance_effect = {
	apply_invader_modifiers_effect = {
		INVADER_DYNN = $INVADER_DYNN$
	}
	if = {
		limit = { $INVADER_CHAR$ = { has_character_flag = spawned_$INVADER_DYNN$ } }
		give_invader_land_effect = {
			INVADER_DYNN = $INVADER_DYNN$
			INVADER_CHAR = $INVADER_CHAR$
			STARTING_TIER = $STARTING_TIER$
		}
	}
	if = {
		limit = { game_start_date < 1152.1.1 }
		spawn_$INVADER_DYNN$_family_effect = {
			INVADER_CHAR = $INVADER_CHAR$
		}
	}
	else = { $INVADER_CHAR$ = { remove_character_flag = is_first_invader_flag } }
	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_ghurid_family_effect = {
	create_character = {
		template = sayf_ghurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$
	}

	create_character = {
		template = baha_ghurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father
		save_scope_as = baha_ghaznavid
	}

	create_character = {
		template = ghiyath_ghurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:baha_ghaznavid
	}

	create_character = {
		template = muizz_ghurid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:baha_ghaznavid
	}
}

# Make sure invader has troops
spawn_ghurid_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_ghurid_army_effect = yes
		spawn_ghurid_army_effect = yes
	}
	spawn_ghurid_army_effect = yes
	spawn_ghurid_army_effect = yes
	spawn_ghurid_army_effect = yes
	if = {
		limit = { game_start_date < 1000.1.1 }
		spawn_ghurid_army_effect = yes
	}
}
spawn_ghurid_army_effect = {
	spawn_army = {
		name = ghurid_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = war_elephant
			stacks = 5
		}
		men_at_arms = {
			type = ayyar
			stacks = 18
		}
		men_at_arms = {
			type = paiks
			stacks = 18
		}
		men_at_arms = {
			type = light_footmen
			stacks = 3
		}
		# siege
		men_at_arms = {
			type = trebuchet
			stacks = 7
		}
	}
}