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

	# scope:nolisi_liao = dynasty root
	if = {
		limit = { NOT = { exists = scope:invader } }
		# father
		create_character = {
			template = saladi_liao_character_template
			dynasty = inherit
			father = scope:nolisi_liao
			location = scope:invader_spawn_location
			save_scope_as = saladi_liao
		}

		# INVADER
		create_character = {
			template = abaoji_liao_character_template
			dynasty = inherit
			father = scope:saladi_liao
			location = scope:invader_spawn_location
			save_scope_as = invader
			after_creation = {
				add_character_flag = spawned_$INVADER_DYNN$
				dynasty = {
					set_dynasty_name = hi_dynn_liao
					set_coa = title:e_liao
				}
				house = {
					set_house_name = hi_dynn_liao_house_yelu
					set_coa = title:e_liao
				}
			}
		}
	}

	scope:invader = {
		add_gold = 2000
		add_prestige = 2250
		add_piety = 150
		
		dynasty = {
			add_dynasty_prestige_level = 1
			add_dynasty_prestige = 500
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
	}

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

liao_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
}

liao_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$
		}
	}
	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_liao_family_effect = {
	if = {
		limit = { $INVADER_CHAR$ = { has_character_flag = spawned_liao } }
		$INVADER_CHAR$ = { trigger_event = game_rule.1001 }
	}
	else = {
		$INVADER_CHAR$ = { trigger_event = game_rule.1001 }
	}
}

# Make sure invader has troops
spawn_liao_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_liao_army_effect = yes
	}
	spawn_liao_army_effect = yes
}
spawn_liao_army_effect = {
	spawn_army = {
		name = liao_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = armored_horsemen
			stacks = 3
		}
		men_at_arms = {
			type = horse_archers
			stacks = 7
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 5
		}
		# siege
		men_at_arms = {
			type = onager
			stacks = 5
		}
	}
}