﻿# invader appears, before story
spawn_knytling_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:101549 = { save_scope_as = knud_sigurdr }
			create_character = {
				template = gorm_gorm_character_template
				dynasty = inherit
				father = scope:knud_sigurdr
				location = scope:invader_spawn_location
				save_scope_as = gorm_gorm
			}
			create_character = {
				template = haraldr_gorm_character_template
				dynasty = inherit
				father = scope:gorm_gorm
				location = scope:invader_spawn_location
				save_scope_as = haraldr_gorm
			}
		}
		else = {
			character:101549 = { save_scope_as = haraldr_gorm } # Knud Sigurdr, fallback to create dynasty link
		}
	
		# father is created first so we can have him appear as father
		# always created (even if fallback triggers) so we can reliably create a cadet branch
		create_character = {
			template = sveinn_gorm_character_template
			dynasty = inherit
			father = scope:haraldr_gorm
			location = scope:invader_spawn_location
			save_scope_as = sveinn_gorm
		}
	
		# INVADER; cadet branch is created within template
		create_character = {
			template = knud_knytling_character_template
			father = scope:sveinn_gorm
			dynasty = inherit
			location = scope:invader_spawn_location
			save_scope_as = invader
			after_creation = {
				add_character_flag = spawned_$INVADER_DYNN$
				house = {
					set_house_name = hi_dynn_sigurdr_house_knytling
					set_coa = house:house_knytling
				}
			}
		}
	}
	# if an ai character was picked, attempt to create a cadet branch
	else = {
		scope:invader = {
			if = {
				limit = { is_ai = yes }
				create_cadet_branch = {}
			}
		}
	}

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

			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
	}

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

knytling_add_dynasty_perks_effect = {}

knytling_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_knytling_family_effect = {
	### spouse & spouse family
	# forefathers
	character:242 = { save_scope_as = hrolfr_normandie }
	create_character = {
		template = william_i_normandie_knytling_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:hrolfr_normandie
		after_creation = { save_scope_as = william_normandie }
	}
	create_character = {
		template = richard_normandie_knytling_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:william_normandie
		after_creation = { save_scope_as = richard_normandie }
	}
	# spouse
	create_character = {
		template = emma_normandie_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:richard_normandie
		after_creation = {
			save_scope_as = emma_normandie
			disease_immunity_long_effect = yes
		}
	}
	scope:emma_normandie = {
		if = {
			limit = {
				$INVADER_CHAR$ = { is_married = no }
			}
			marry = $INVADER_CHAR$
		}
		else_if = {
			limit = {
				$INVADER_CHAR$ = {
					is_married = yes
					faith = { has_doctrine = doctrine_concubines }
				}
			}
			$INVADER_CHAR$ = { make_concubine = prev }
		}
	}

	# children
	create_character = {
		template = haraldr_knytling_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:emma_normandie
		father = $INVADER_CHAR$
		after_creation = {
			disease_immunity_long_effect = yes
		}
	}
	create_character = {
		template = sveinn_knytling_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:emma_normandie
		father = $INVADER_CHAR$
		after_creation = {
			disease_immunity_long_effect = yes
		}
	}
}

# Make sure invader has troops
spawn_knytling_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_knytling_army_effect = yes
	}
	spawn_knytling_army_effect = yes
	spawn_knytling_army_effect = yes
}
spawn_knytling_army_effect = {
	spawn_army = {
		name = knytling_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 200
		men_at_arms = {
			type = light_footmen
			stacks = 10
		}
		men_at_arms = {
			type = armored_footmen
			stacks = 7
		}
		men_at_arms = {
			type = bowmen
			stacks = 7
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}

create_north_sea_empire_kingdoms_effect = {
	# England
	if = {
		limit = {
			title:k_england = { is_title_created = no }
			any_county_in_region = {
				region = special_knytling_conquest_region_start
				percent >= 0.4
				OR = {
					holder = scope:story.story_owner
					holder = { target_is_liege_or_above = scope:story.story_owner }
				}
			}
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_england = {
			change_title_holder = {
				holder = scope:story.story_owner
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	# Denmark
	if = {
		limit = {
			title:k_denmark = { is_title_created = no }
			any_county_in_region = {
				region = special_knytling_conquest_region_2
				percent >= 0.4
				OR = {
					holder = scope:story.story_owner
					holder = { target_is_liege_or_above = scope:story.story_owner }
				}
			}
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_denmark = {
			change_title_holder = {
				holder = scope:story.story_owner
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	# Norway
	if = {
		limit = {
			title:k_norway = { is_title_created = no }
			any_county_in_region = {
				region = special_knytling_conquest_region_3
				percent >= 0.4
				OR = {
					holder = scope:story.story_owner
					holder = { target_is_liege_or_above = scope:story.story_owner }
				}
			}
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_norway = {
			change_title_holder = {
				holder = scope:story.story_owner
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
}