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

	# pick existing Rodrigo
	if = {
		limit = {
			always = no # making use of the Rodrigo who exists at game start will cause many errors related to "fp2_el_cid_events.txt" which hopelessly tries to move him back into the character pool over and over again
			game_start_date >= 1065.1.1 # make sure he is old enough
			exists = character:107590
			character:107590 = {
				is_alive = yes
				OR = {
					is_ruler = no
					highest_held_title_tier = tier_barony
				}
				is_healthy = yes
				culture = culture:castilian
				faith = faith:catholic
			}
		}
		character:107590 = { save_scope_as = invader }
	}
	# spawn a new one in
	else = {
		create_character = {
			template = rodrigo_vivar_character_template
			dynasty = generate
			location = scope:invader_spawn_location
			save_scope_as = invader
		}
		scope:invader = {
			dynasty = {
				set_dynasty_name = hi_dynn_vivar
				set_coa = title:d_vivar
			}
			house = {
				set_house_name = hi_dynn_vivar
			}
		}
	}

	scope:invader = {
		add_gold = 450
		add_prestige = 75
		add_piety = 50
		
		dynasty = {
			add_dynasty_prestige_level = 1
			add_dynasty_prestige = 750

			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
	}
}

vivar_add_dynasty_perks_effect = {}

vivar_maintenance_effect = {
	apply_invader_modifiers_effect = {
		INVADER_DYNN = $INVADER_DYNN$
	}
	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_vivar_family_effect = {
	$INVADER_CHAR$ = { trigger_event = game_rule.1001 }
}

# Make sure invader has troops
spawn_vivar_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_vivar_army_effect = yes
	}
	spawn_vivar_army_effect = yes
	if = {
		limit = { game_start_date < 1000.1.1 }
		spawn_vivar_army_effect = yes
		spawn_vivar_army_effect = yes
	}
}
spawn_vivar_army_effect = {
	spawn_army = {
		name = vivar_event_troops
		location = primary_title.title_capital_county.title_province
		war = scope:war
		war_keep_on_attacker_victory = no
		uses_supply = no
		inheritable = no
		
		levies = 750
		men_at_arms = {
			type = armored_footmen
			stacks = 3
		}
		men_at_arms = {
			type = light_footmen
			stacks = 5
		}
		men_at_arms = {
			type = pikemen_unit
			stacks = 7
		}
		men_at_arms = {
			type = bowmen
			stacks = 7
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}