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

	# INVADER Attila
	if = {
		limit = { has_game_rule = inversed_gender_equality }
		create_character = {
			gender = female
			template = attila_attila_character_template
			location = scope:invader_spawn_location
			save_scope_as = invader
		}
	}
	else_if = {
		limit = { has_game_rule = full_gender_equality }
		random_list = {
			50 = {
				create_character = {
					gender = female
					template = attila_attila_character_template
					location = scope:invader_spawn_location
					save_scope_as = invader
				}
			}
			50 = {
				create_character = {
					gender = male
					template = attila_attila_character_template
					location = scope:invader_spawn_location
					save_scope_as = invader
				}
			}
		}
	}
	else = {
		create_character = {
			gender = male
			template = attila_attila_character_template
			location = scope:invader_spawn_location
			save_scope_as = invader
		}
	}

	scope:invader = {
		add_gold = 3000
		add_prestige = 3000
		add_piety = 150
		
		dynasty = {
			set_dynasty_name = hi_dynn_attila
			set_coa = title:e_attila

			add_dynasty_prestige_level = 3
			add_dynasty_prestige = 7500
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			set_house_name = hi_dynn_attila
		}
	}

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

attila_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
}

attila_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$
	}
	attila_grant_historical_artifacts_effect = yes
}

### populating the life of the invader character
spawn_attila_family_effect = {
	# spouse
	if = {
		limit = { $INVADER_CHAR$ = { is_female = yes } }
		create_character = {
			age = { 20 30 }
			template = hi_random_spouse_character_template
			dynasty = none
			gender = male
			employer = $INVADER_CHAR$
			faith = $INVADER_CHAR$.faith
			culture = $INVADER_CHAR$.culture
			after_creation = {
				marry_matrilineal = $INVADER_CHAR$
				disease_immunity_short_effect = yes
			}
		}
	}
	else = {
		create_character = {
			age = { 20 30 }
			template = hi_random_spouse_character_template
			dynasty = none
			gender = female
			employer = $INVADER_CHAR$
			faith = $INVADER_CHAR$.faith
			culture = $INVADER_CHAR$.culture
			after_creation = {
				marry = $INVADER_CHAR$
				disease_immunity_short_effect = yes
			}
		}
	}

	# children
	$INVADER_CHAR$ = { trigger_event = game_rule.1001 }
}

# Make sure invader has troops
spawn_attila_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_attila_army_effect = yes
	}
	spawn_attila_army_effect = yes
	spawn_attila_army_effect = yes
	if = {
		limit = { years_from_game_start > 100 }
		spawn_attila_army_effect = yes
	}
	if = {
		limit = { years_from_game_start > 200 }
		spawn_attila_army_effect = yes
	}
	if = {
		limit = { years_from_game_start > 300 }
		spawn_attila_army_effect = yes
	}
}
spawn_attila_army_effect = {
	spawn_army = {
		name = attila_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = horse_archers
			stacks = 15
		}
		men_at_arms = {
			type = horse_archers
			stacks = 15
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 8
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 7
		}
	}
}

attila_grant_historical_artifacts_effect = {
	# see historical_artifacts.0023 for DLC restrictions
	title:e_attila.holder = {
		if = {
			limit = {
				#has_ep1_dlc_trigger = yes
				has_dlc_feature = royal_court
			}
			create_artifact_wall_sword_attila_effect = { OWNER = this } # court artifact
		}
	}
}