﻿courtiers_return_home_effect = {
	save_temporary_scope_as = root_scope
	every_courtier = {
		limit = {
			has_variable = remain_at_court
			var:remain_at_court = {
				holder ?= { is_ai = yes }
			}
		}
		save_temporary_scope_as = return_home
		if = {
			limit = {
				is_married = yes
				primary_spouse = {
					NOT = { has_variable = remain_at_court }
					is_courtier_of = scope:root_scope
				}
			}
			scope:return_home.var:remain_at_court.holder = { add_courtier = scope:return_home.primary_spouse }
		}
		every_child = {
			limit = {
				is_adult = no
				NOT = { has_variable = remain_at_court }
				is_courtier_of = scope:root_scope
			}
			scope:return_home.var:remain_at_court.holder = { add_courtier = prev }
		}
		scope:return_home.var:remain_at_court.holder = { add_courtier = scope:return_home }
	}
}

create_first_wave_hautevilles = {
	# first pick a Norman lord whose court they come from
	if = {
		limit = {
			culture:norman = {
				any_culture_county = {
					OR = {
						holder.culture = culture:norman
						holder.culture = { has_cultural_pillar = heritage_frankish }
					}
					count > 0
				}
			}
		}
		culture:norman = {
			random_culture_county = {
				limit = { holder.culture = culture:norman }
				alternative_limit = { holder.culture = { has_cultural_pillar = heritage_frankish } }
				weight = {
					base = 1
					modifier = {
						add = 20
						this = title:c_avranches
					}
					modifier = {
						add = 50
						duchy = title:d_normandy
					}
					modifier = {
						add = 100
						holder = { has_title = title:d_normandy }
					}
					modifier = {
						add = 5
						kingdom = title:k_france
					}
					modifier = {
						add = 20
						holder = { highest_held_title_tier = tier_duchy }
					}
					modifier = {
						add = 30
						holder = { highest_held_title_tier >= tier_kingdom }
					}
					modifier = {
						add = -100
						holder = { is_ai = no }
					}
					min = 1
				}
				save_scope_as = norman_court
			}
		}
	}
	else = {
		culture:norman = {
			random_culture_county = {
				weight = {
					base = 1
					modifier = {
						add = 20
						this = title:c_avranches
					}
					modifier = {
						add = 50
						duchy = title:d_normandy
					}
					modifier = {
						add = 100
						holder = { has_title = title:d_normandy }
					}
					modifier = {
						add = 5
						kingdom = title:k_france
					}
					modifier = {
						add = 20
						holder = { highest_held_title_tier = tier_duchy }
					}
					modifier = {
						add = 30
						holder = { highest_held_title_tier >= tier_kingdom }
					}
					modifier = {
						add = -100
						holder = { is_ai = no }
					}
					min = 1
				}
			}
			save_scope_as = norman_court
		}
	}
	
	# then pick the duchy they're going to attack
	if = {
		limit = { should_be_invading_sicily = yes }
		random_county_in_region = {
			region = custom_sicily
			limit = {
				save_temporary_scope_as = potential_county
				NOR = {
					# not Malta, thank you
					this = title:c_malta
					# no conquerors
					holder = { has_trait = conqueror }
					# we don't want to attack a county in a duchy that includes a player's capital
					duchy = {
						any_de_jure_county = {
							holder = {
								is_ai = no
								OR = {
									capital_county.duchy = scope:potential_county.duchy
									domain_size < 6
								}
							}
						}
					}
					# and we don't want to attack the realms of players that aren't very large
					holder = {
						any_liege_or_above = {
							is_ai = no
							sub_realm_size < 10
						}
					}
				}
				# for the historical arrival, let's target a duchy that can get us at least 3 counties and isn't terribly strong
				trigger_if = {
					limit = { current_year <= 1040 }
					holder.top_liege = {
						strength_with_relevant_allies_value <= 5000
						any_sub_realm_county = {
							duchy = scope:potential_county.duchy
							count > 2
						}
					}
				}
				# for an arrival up to 10 years later, we loosen up the requirements a bit
				trigger_else_if = {
					limit = { current_year <= 1050 }
					holder.top_liege = {
						strength_with_relevant_allies_value <= 8000
						any_sub_realm_county = {
							duchy = scope:potential_county.duchy
							count > 1
						}
					}
				}
				# after that, it's anything
				trigger_else = {
					always = yes
				}
			}
			weight = {
				base = 10
				modifier = {
					add = 2500
					has_game_rule = hautevilles_invade_sicily
					duchy = title:d_apulia
				}
				modifier = {
					add = 250
					holder.top_liege = {
						any_sub_realm_county = {
							duchy = scope:sicily_county.duchy
							count > 2
						}
					}
				}
				modifier = {
					add = 100
					holder.top_liege = {
						any_sub_realm_county = {
							duchy = scope:sicily_county.duchy
							count > 1
						}
					}
				}
				modifier = {
					add = 250
					holder.top_liege.strength_with_relevant_allies_value <= 2500
				}
				modifier = {
					add = 100
					holder.top_liege.strength_with_relevant_allies_value <= 5000
				}
				modifier = {
					factor = 0.5
					holder.faith = faith:catholic
				}
				modifier = {
					factor = 0
					exists = faith:catholic.religious_head
					holder.top_liege = faith:catholic.religious_head
				}
				min = 1
			}
			save_scope_as = target_county
		}
	}
	
	# and pick a spot in Italy for the brothers to spawn
	if = {
		limit = {
			any_county_in_region = {
				region = custom_sicily
				holder = { is_ai = yes }
				NOR = {
					holder.top_liege = scope:target_county.holder.top_liege
					this = title:c_malta
				}
			}
		}
		random_county_in_region = {
			region = custom_sicily
			limit = {
				holder = { is_ai = yes }
				NOR = {
					holder.top_liege = scope:target_county.holder.top_liege
					this = title:c_malta
				}
			}
			title_province = { save_scope_as = iron_arm_start }
		}
	}
	else = {
		random_county_in_region = {
			region = world_europe_south_italy
			limit = {
				holder = { is_ai = yes }
				NOR = {
					holder.top_liege = scope:target_county.holder.top_liege
					this = title:c_malta
					kingdom = title:k_sardinia
				}
			}
			title_province = { save_scope_as = iron_arm_start }
		}
	}
	
	# create the father
	create_character = {
		template = hauteville_family_template
		employer = scope:norman_court.holder
		age = 58
		gender = male
		dynasty = generate
		trait = physique_good_1
		trait = brave
		trait = education_stewardship_2
		save_scope_as = tancred
		after_creation = {
			dynasty = {
				add_dynasty_prestige_level = 1
				add_dynasty_prestige = 1000
				add_dynasty_perk = warfare_legacy_1
			}
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Tancred
				histmod_create_dynasty_effect = { NAME = dynn_Hauteville PREFIX = dynnp_de COA = c_apulia }
			}
			set_variable = { name = remain_at_court value = scope:norman_court }
		}
	}
	set_global_variable = { name = is_hauteville_dynasty value = scope:tancred.dynasty }
	
	# create the mother
	create_character = {
		template = hauteville_family_template
		employer = scope:norman_court.holder
		age = 52
		gender = female
		dynasty = generate
		save_scope_as = muriella
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Muriella
			}
			marry = scope:tancred
		}
	}

	# only create Serlo if this is Hauteville
	if = {
		limit = { should_be_hauteville = yes }
		create_character = {
			name = Serlo
			template = hauteville_warrior_template
			employer = scope:norman_court.holder
			age = 33
			gender = male
			dynasty = inherit
			mother = scope:muriella
			father = scope:tancred
			trait = vengeful
			save_scope_as = serlo
			after_creation = {
				set_variable = { name = remain_at_court value = scope:norman_court }
			}
		}
		# give him a Norman wife
		create_character = {
			template = hauteville_family_template
			employer = scope:norman_court.holder
			age = 25
			gender = female
			dynasty = generate
			save_scope_as = serlo_wife
			after_creation = {
				marry = scope:serlo
			}
		}
		# and create his son
		create_character = {
			name = Serlo
			employer = scope:norman_court.holder
			age = 3
			gender = male
			dynasty = inherit
			mother = scope:serlo_wife
			father = scope:serlo
			trait = physique_good_1
			random_traits = yes
			save_scope_as = serlo_son
			after_creation = {
				every_character_trait = {
					limit = { has_trait_category = childhood }
					scope:serlo_son = { remove_trait = prev }
				}
				add_trait = rowdy
				set_focus = education_martial
			}
		}
	}

	# now create the three brothers & sister
	create_character = {
		template = hauteville_family_template
		location = scope:iron_arm_start
		age = 32
		gender = female
		dynasty = inherit
		intrigue = 8
		learning = 8
		trait = education_intrigue_3
		trait = lifestyle_herbalist
		mother = scope:muriella
		father = scope:tancred
		save_scope_as = beatrix
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Beatrix
			}
		}
	}
	
	create_character = {
		template = hauteville_warrior_template
		location = scope:iron_arm_start
		age = 27
		gender = male
		dynasty = inherit
		trait = strong
		trait = greedy
		trait = education_martial_4
		mother = scope:muriella
		father = scope:tancred
		save_scope_as = william
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = William
				give_nickname = nick_iron_arm
			}
			add_gold = 500
			add_prestige = 2000
			add_piety = 250
		}
	}
	
	create_character = {
		template = hauteville_warrior_template
		location = scope:iron_arm_start
		age = 26
		gender = male
		dynasty = inherit
		mother = scope:muriella
		father = scope:tancred
		save_scope_as = drogo
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Drogo
			}
			add_prestige = 1000
			add_piety = 100
		}
	}
	
	create_character = {
		template = hauteville_warrior_template
		location = scope:iron_arm_start
		age = 24
		gender = male
		dynasty = inherit
		mother = scope:muriella
		father = scope:tancred
		save_scope_as = humphrey
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Humphrey
			}
			add_prestige = 500
			add_piety = 100
		}
	}

	# now make William an adventurer & give him starting forces
	scope:william = {
		character_becomes_adventurer_effect = { ADVENTURER = scope:william BAND = robert_band_name COA = scope:william.dynasty }
		set_variable = { name = summoned_norman_bannermen value = yes }
		if = {
			limit = {
				culture = { has_innovation = innovation_mangonel }
			}
			spawn_army = {
				levies = 2000
				men_at_arms = {
					type = pikemen_unit
					stacks = 5
				}
				men_at_arms = {
					type = armored_footmen
					stacks = 5
				}
				men_at_arms = {
					type = armored_horsemen
					stacks = 3
				}
				men_at_arms = {
					type = mangonel
					stacks = 3
				}
				inheritable = yes
				uses_supply = no
				location = scope:iron_arm_start
				name = spawned_norman_bannermen_name
			}
		}
		else = {
			spawn_army = {
				levies = 2000
				men_at_arms = {
					type = pikemen_unit
					stacks = 5
				}
				men_at_arms = {
					type = armored_footmen
					stacks = 5
				}
				men_at_arms = {
					type = armored_horsemen
					stacks = 3
				}
				men_at_arms = {
					type = onager
					stacks = 3
				}
				inheritable = yes
				uses_supply = no
				location = scope:iron_arm_start
				name = spawned_norman_bannermen_name
			}
		}
		# make sure he didn't take any courtiers he's not supposed to
		courtiers_return_home_effect = yes
		# give him the story
		create_story = { type = histmod_hauteville_adventurer_story_cycle }
	}

	# Kill the mothers and marry Serlo to a new wife
	scope:muriella = {
		death = { death_reason = death_accident }
	}
	create_character = {
		template = hauteville_family_template
		employer = scope:norman_court
		age = 40
		gender = female
		dynasty = generate
		trait = physique_good_1
		save_scope_as = fressenda
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Fressenda
			}
			marry = scope:tancred
			set_variable = { name = remain_at_court value = scope:norman_court }
		}
	}
	if = {
		limit = { exists = scope:serlo_wife }
		scope:serlo_wife = {
			death = { death_reason = death_accident }
		}
	}
	
	# and create a couple more Norman commanders
	create_character = {
		template = new_commander_character
		gender_female_chance = scandi_commander_female_chance
		employer = scope:william
		faith = faith:catholic
		culture = culture:norman
		prowess = { medium_skill_rating high_skill_rating }
		after_creation = {
			spawned_soldier_traits_effect = yes
		}
	}
	create_character = {
		template = new_commander_character
		gender_female_chance = scandi_commander_female_chance
		employer = scope:william
		faith = faith:catholic
		culture = culture:norman
		prowess = { medium_skill_rating high_skill_rating }
		after_creation = {
			spawned_soldier_traits_effect = yes
		}
	}
}

create_second_wave_hautevilles = {
	# first pick a place for them to appear
	random_county_in_region = {
		region = custom_sicily
		limit = {
			NOT = { this = title:c_malta }
		}
		weight = {
			base = 5
			modifier = {
				exists = global_var:is_hauteville_dynasty
				holder.dynasty = global_var:is_hauteville_dynasty
				add = 20
			}
			modifier = {
				holder = { is_ai = no }
				add = -4
			}
		}
		title_province = { save_scope_as = robert_start }
	}

	# now spawn robert
	create_character = {
		template = hauteville_warrior_template
		location = scope:robert_start
		#age = 32
		age = 23
		gender = male
		dynasty = inherit
		trait = arbitrary
		trait = greedy
		trait = ambitious
		trait = physique_good_2
		trait = education_martial_5
		martial = 9
		diplomacy = 4
		intrigue = 6
		stewardship = 6
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = robert
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Robert
			}
			give_nickname = nick_the_fox
			add_gold = 500
			add_prestige = 1000
			add_piety = 250
		}
	}
	
	# create an adventurer title for him
	scope:robert = {
		character_becomes_adventurer_effect = { ADVENTURER = scope:robert BAND = robert_band_name COA = no }
		set_variable = { name = summoned_norman_bannermen value = yes }
		if = {
			limit = {
				culture = { has_innovation = innovation_mangonel }
			}
			spawn_army = {
				levies = 3000
				men_at_arms = {
					type = pikemen_unit
					stacks = 5
				}
				men_at_arms = {
					type = armored_footmen
					stacks = 5
				}
				men_at_arms = {
					type = armored_horsemen
					stacks = 5
				}
				men_at_arms = {
					type = mangonel
					stacks = 5
				}
				inheritable = yes
				uses_supply = no
				location = scope:robert_start
				name = spawned_norman_bannermen_name
			}
		}
		else = {
			spawn_army = {
				levies = 3000
				men_at_arms = {
					type = pikemen_unit
					stacks = 5
				}
				men_at_arms = {
					type = armored_footmen
					stacks = 5
				}
				men_at_arms = {
					type = armored_horsemen
					stacks = 5
				}
				men_at_arms = {
					type = onager
					stacks = 5
				}
				inheritable = yes
				uses_supply = no
				location = scope:robert_start
				name = spawned_norman_bannermen_name
			}
		}
		
		# make sure he didn't take any courtiers he's not supposed to
		courtiers_return_home_effect = yes
		
		# give him the story
		create_story = { type = histmod_hauteville_adventurer_story_cycle }
	}
	
	# then create the rest of the family
	if = {
		limit = { should_be_hauteville = yes }
		create_character = {
			name = Geoffrey
			template = hauteville_warrior_template
			location = scope:iron_arm_start
			#age = 29
			age = 20
			gender = male
			dynasty = inherit
			trait = education_martial_4
			trait = legitimized_bastard
			martial = 8
			diplomacy = 7
			intrigue = 4
			stewardship = 8
			mother = scope:muriella
			father = scope:tancred
			save_scope_as = geoffrey
			after_creation = {
				add_prestige = 500
				add_piety = 100
			}
		}
	}
	
	create_character = {
		template = hauteville_warrior_template
		employer = scope:robert
		#age = 26
		age = 17
		gender = male
		dynasty = inherit
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = mauger
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Mauger
			}
			add_prestige = 500
			add_piety = 100
		}
	}
	
	create_character = {
		template = hauteville_family_template
		employer = scope:robert
		#age = 25
		age = 16
		gender = male
		dynasty = inherit
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = william2
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = William
			}
		}
	}
	
	create_character = {
		template = hauteville_family_template
		employer = scope:robert
		#age = 23
		age = 14
		gender = male
		dynasty = inherit
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = aubrey
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Aubrey
			}
		}
	}
	
	create_character = {
		template = hauteville_family_template
		employer = scope:robert
		#age = 21
		age = 12
		gender = male
		dynasty = inherit
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = hubert
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Hubert
			}
		}
	}
	
	create_character = {
		template = hauteville_warrior_template
		employer = scope:robert
		#age = 19
		age = 10
		gender = male
		dynasty = inherit
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = tancred2
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Tancred
			}
		}
	}
	
	create_character = {
		template = hauteville_family_template
		employer = scope:robert
		#age = 18
		age = 9
		gender = male
		dynasty = inherit
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = frumentin
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Frumentin
			}
		}
	}
	
	create_character = {
		template = hauteville_family_template
		employer = scope:robert
		#age = 16
		age = 7
		gender = male
		dynasty = inherit
		trait = education_martial_4
		trait = just
		trait = calm
		martial = 8
		diplomacy = 7
		intrigue = 4
		stewardship = 8
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = roger
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Roger
			}
		}
	}
	
	create_character = {
		template = hauteville_family_template
		employer = scope:robert
		#age = 30
		age = 21
		gender = female
		dynasty = inherit
		trait = education_intrigue_3
		diplomacy = 6
		intrigue = 6
		stewardship = 5
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = emma
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Emma
			}
		}
	}
	
	create_character = {
		template = hauteville_family_template
		employer = scope:robert
		#age = 22
		age = 13
		gender = female
		dynasty = inherit
		mother = scope:fressenda
		father = scope:tancred
		save_scope_as = fressenda2
		after_creation = {
			if = {
				limit = { should_be_hauteville = yes }
				change_first_name = Fressenda
			}
		}
	}
}



