﻿scandi_adventurer_deal_effect = {
	# negotiated settlement ends the war
	if = {
		limit = { scope:war_result = flag:is_negotiated }
		scope:attacker = {
			# End relevant wars
			every_character_war = {
				limit = {
					OR = {
						primary_attacker = scope:attacker
						primary_defender = scope:attacker
					}
				}
				end_war = white_peace
			}
		}
	}
	
	# remove from relevant wars
	scope:attacker = {
		every_character_war = {
			limit = {
				NOR = {
					primary_attacker = scope:attacker
					primary_defender = scope:attacker
				}
				any_war_defender = {
					OR = {
						this = scope:defender
						this.top_liege = scope:defender.top_liege
					}
				}
			}
			remove_participant = scope:attacker
		}
	}
	
	# give lands to the adventurer, if they don't already have them
	if = {
		limit = {
			NOT = { scope:war_result = flag:defender_defeat }
		}
		
		# first give a laamp adventurer and their followers their traits
		hidden_effect = {
			scope:attacker = {
				if = {
					limit = {
						has_government = landless_adventurer_government
						NOT = { has_trait = adventurer }
					}
					add_trait = adventurer
				}
				every_courtier = {
					limit = { NOT = { has_trait = adventurer_follower } }
					set_variable = { name = former_camp_leader value = scope:attacker }
					add_trait = adventurer_follower
				}
			}
		}
		
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}

		every_in_list = {
			list = target_titles
			custom_tooltip = DEAL_NORMANDY_CB_TITLE
			every_in_de_jure_hierarchy = {
				continue = {
					OR = {
						NOT = {	exists = holder }
						NOT = {
							holder = { target_is_liege_or_above = scope:defender }
						}
						AND = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = { prev.holder.faith <= scope:defender.faith }
							}
							holder = {
								OR = {
									this = scope:defender
									target_is_liege_or_above = scope:defender
								}
							}
						}
					}
					tier > tier_county
				}
				limit = {
					tier <= tier_duchy
					holder ?= {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
				if = {
					limit = {
						OR = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = {
									prev.holder.faith >= scope:defender.faith
								}
							}
							holder.primary_title.tier >= scope:attacker.primary_title.tier
						}
					}
					add_to_temporary_list = titles_taken
				}
				else = {
					holder = {
						add_to_temporary_list = vassals_taken
					}
				}
			}
		}
		
		every_in_list = {
			list = titles_taken
			hidden_effect = {
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
					take_baronies = yes
				}
			}
		}
		
		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}
		
		resolve_title_and_vassal_change = scope:change
	}

	# then vassalize the adventurer
	create_title_and_vassal_change = {
		type = swear_fealty
		save_scope_as = change
	}
	scope:attacker = {
		change_liege = {
			liege = scope:defender
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
	
	# Give a nice long truce so they can't immediately usurp you
	scope:defender = {
		hidden_effect = {
			cancel_truce_both_ways = scope:attacker
		}
		add_truce_both_ways = {
			character = scope:attacker
			days = 9125
			override = yes
			name = FP1_SCANDINAVIAN_ADVENTURER_VASSAL_TRUCE_NAME
		}
	}
	
	# Adventurer victory
	if = {
		limit = { scope:war_result = flag:defender_defeat }
		# They get lower vassal obligations right off the bat
		if = {
			limit = {
				scope:defender = { government_has_flag = government_is_feudal }
			}
			scope:attacker = {
				custom_tooltip = rollo_gains_low_obligations_tt
				hidden_effect = {
					vassal_contract_set_obligation_level = { type = feudal_government_taxes level = 1 }
					vassal_contract_set_obligation_level = { type = feudal_government_levies level = 1 }
					set_vassal_contract_modification_blocked = yes
					every_held_county = { change_county_control = 50 }
				}
			}
		}
	}
	# Adventurer defeat
	else_if = {
		limit = { scope:war_result = flag:defender_victory }		
		# higher obligations to start
		if = {
			limit = {
				scope:defender = { government_has_flag = government_is_feudal }
			}
			scope:attacker = {
				custom_tooltip = rollo_gains_high_obligations_tt
				hidden_effect = {
					vassal_contract_set_obligation_level = { type = feudal_government_taxes level = 3 }
					vassal_contract_set_obligation_level = { type = feudal_government_levies level = 3 }
					set_vassal_contract_modification_blocked = yes
				}
			}
		}
		# they get some replacement units (since they likely have nothing left)
		scope:attacker = {
			hidden_effect = {
				every_held_county = { change_county_control = 50 }
				spawn_army = {
					men_at_arms = { type = huscarl stacks = 4 }
					inheritable = yes
					location = scope:attacker.capital_province
					name = scandi_adventurer_event_troops
				}
				spawn_army = {
					men_at_arms = { type = vigmen stacks = 4 }
					inheritable = yes
					location = scope:attacker.capital_province
					name = scandi_adventurer_event_troops
				}
				spawn_army = {
					men_at_arms = { type = light_footmen stacks = 4 }
					inheritable = yes
					location = scope:attacker.capital_province
					name = scandi_adventurer_event_troops
				}
			}
			# plus they give up any current claims
			every_claim = {
				scope:attacker = { remove_claim = prev }
			}
		}
	}
	# Adventurer white peace
	else_if = {
		limit = { scope:war_result = flag:white_peace }
		scope:attacker = {
			hidden_effect = {
				every_held_county = { change_county_control = 50 }
			}
			# plus they give up any current claims
			every_claim = {
				scope:attacker = { remove_claim = prev }
			}
		}
	}
	
	# Wipe out an AI adventurer's debt (if any) and then give them some additional coin to get started
	scope:attacker = {
		if = {
			limit = { is_ai = yes }
			if = {
				limit = { gold < 0 }
				add_gold = {
					value = scope:attacker.gold
					multiply = -1
				}
			}
			add_gold = 250
		}
	}

	# Adventurer converts to the king's faith
	scope:attacker = {
		add_character_flag = { flag = immune_to_disease years = 15 }
		player_heir ?= {
			if = {
				limit = { is_child_of = scope:attacker }
				add_character_flag = { flag = immune_to_disease years = 15 }
			}
		}
		if = {
			limit = {
				NOT = { faith = scope:defender.faith }
			}
			custom_tooltip = {
				text = rollo_will_convert_tt
				scope:defender = { save_scope_as = actor }
				scope:attacker = { save_scope_as = recipient }
				grab_spouses_and_family_to_convert_effect = yes
				convert_family_to_faith_effect = {
					FALSE_CONVERSION = no
					FORCED = no
				}
			}
		}
	}
	
	# is this a deal for Normandy?
	if = {
		limit = {
			NOT = { has_game_rule = always_invades_no_special_rules }
			scope:attacker.culture = culture:norse
			any_in_list = {
				list = target_titles
				this = title:d_normandy
			}
			title:c_rouen = { culture = culture:french }
		}
		# See if there's a close family member for marriage with a male adventurer
		if = {
			limit = {
				exists = scope:alliance_marriage
				scope:attacker = { is_male = yes }
				NOT = { scope:war_result = flag:defender_victory }
			}
			if = {
				limit = {
					OR = {
						scope:attacker = { is_ai = yes }
						root = scope:attacker
					}
				}
				if = {
					limit = { scope:attacker = { is_married = yes } }
					scope:attacker = {
						primary_spouse = { save_scope_as = former_wife }
						divorce = scope:former_wife
						hidden_effect = {
							reverse_add_opinion = {
								modifier = divorced_me_opinion
								target = scope:former_wife
							}
							scope:former_wife = {
								every_close_or_extended_family_member = {
									limit = { NOT = { this = scope:attacker } }
									custom = all_family_members
									add_opinion = {
										modifier = divorced_close_kin
										target = scope:attacker
									}
								}
							}

							if = {
								limit = {
									scope:attacker = { has_character_modifier = rejected_from_marriage_bed_modifier }
								}
								scope:attacker = { remove_character_modifier = rejected_from_marriage_bed_modifier }
							}
							if = {
								limit = {
									scope:former_wife = { has_character_modifier = rejected_from_marriage_bed_modifier }
								}
								scope:former_wife = { remove_character_modifier = rejected_from_marriage_bed_modifier }
							}
							add_courtier = scope:former_wife
						}
					}
				}
				if = {
					limit = { scope:attacker = { is_betrothed = yes } }
					scope:attacker = { break_betrothal = scope:attacker.betrothed }
				}
				if = {
					limit = { scope:alliance_marriage = { is_betrothed = yes } }
					scope:alliance_marriage = { break_betrothal = scope:alliance_marriage.betrothed }
				}
				scope:alliance_marriage = {
					if = {
						limit = { is_adult = yes }
						marry = scope:attacker
					}
					else = {
						create_betrothal = scope:attacker
					}
				}
				scope:defender = {
					create_alliance = {
						target = scope:attacker
						allied_through_owner = scope:attacker
						allied_through_target = scope:alliance_marriage
					}
				}
			}
			else = {
				if = {
					limit = { scope:alliance_marriage = { is_adult = no } }
					custom_tooltip = attacker_will_betroth_alliance_tt
				}
				else = {
					custom_tooltip = attacker_will_marry_alliance_tt
				}
			}
		}
		# Otherwise we add a negotiated alliance
		else = {
			scope:defender = {
				hidden_effect = {
					add_opinion = { modifier = event_negotiated_alliance_opinion target = scope:attacker }
					reverse_add_opinion = { modifier = event_negotiated_alliance_opinion target = scope:attacker }
				}
				create_alliance = {
					target = scope:attacker
					allied_through_owner = scope:defender
					allied_through_target = scope:attacker
				}
			}
			# and and AI adventurer will put aside an existing wife unless she's very young
			scope:attacker = {
				if = {
					limit = {
						is_ai = yes
						is_married = yes
						primary_spouse = {
							NOT = { has_relation_soulmate = scope:attacker }
							OR = {
								age > 29
								fertility <= 0.10
							}
						}
					}
					primary_spouse = { save_scope_as = former_wife }
					divorce = scope:former_wife
					hidden_effect = {
						reverse_add_opinion = {
							modifier = divorced_me_opinion
							target = scope:former_wife
						}
						scope:former_wife = {
							every_close_or_extended_family_member = {
								limit = { NOT = { this = scope:attacker } }
								custom = all_family_members
								add_opinion = {
									modifier = divorced_close_kin
									target = scope:attacker
								}
							}
						}

						if = {
							limit = {
								scope:attacker = { has_character_modifier = rejected_from_marriage_bed_modifier }
							}
							scope:attacker = { remove_character_modifier = rejected_from_marriage_bed_modifier }
						}
						if = {
							limit = {
								scope:former_wife = { has_character_modifier = rejected_from_marriage_bed_modifier }
							}
							scope:former_wife = { remove_character_modifier = rejected_from_marriage_bed_modifier }
						}
						if = {
							limit = {
								scope:former_wife = {
									NOT = { is_courtier_of = scope:attacker }
								}
							}
							add_courtier = scope:former_wife
						}
					}
				}
			}
		}
		
		# Plus we whack some opinion in there for good measure.
		scope:defender = {
			set_relation_friend = { 
				reason = friend_fp1_vassalized_viking 
				target = scope:attacker
			}
		}
	}
}

scandi_adventurer_family_check = {
	# first check if the male adventurer is 25+ and unmarried
	if = {
		limit = {
			age >= 25
			is_male = yes
			is_married = no
			can_marry_common_trigger = yes
		}
		# give him a wife with a faith and culture of the target county
		create_character = {
			template = scandi_adventurer_wife
			gender_female_chance = 100
			age = { 16 scandi_wife_max_age }
			employer = scope:scandi_adventurer
			faith = scope:adventurer_target.faith
			culture = scope:adventurer_target.culture
			save_scope_as = scandi_wife
			after_creation = {
				marry = scope:scandi_adventurer
				remove_bad_congenital_traits = yes
				# set her faith to his if his faith considers hers evil
				if = {
					limit = {
						scope:scandi_adventurer.faith = {
							faith_hostility_level = {
								target = scope:scandi_wife.faith
								value >= faith_hostility_prevents_marriage_level
							}
						}
					}
					set_character_faith = scope:scandi_adventurer.faith
				}
			}
		}
		# potentially give him a child if he's childless and a spawned character (has no parents)
		if = {
			limit = {
				is_married = yes
				can_have_children = yes
				NOR = {
					exists = mother
					exists = father
					any_child = { is_alive = yes }
				}
			}
			random = {
				chance = 50
				modifier = {
					add = 25
					scope:scandi_adventurer.primary_spouse.age >= 30
				}
				modifier = {
					add = 25
					scope:scandi_adventurer.primary_spouse.age >= 35
				}
				create_character = {
					gender_female_chance = 25
					age = { 1 scandi_child_adventurer_max_age }
					employer = scope:scandi_adventurer
					faith = scope:scandi_adventurer.faith
					culture = scope:scandi_adventurer.culture
					mother = scope:scandi_adventurer.primary_spouse
					father = scope:scandi_adventurer
					dynasty = inherit
					random_traits = yes
					save_scope_as = scandi_child
					after_creation = {
						remove_bad_congenital_traits = yes
						child_inherit_education_effect = { PRIMARY_PARENT = scope:scandi_adventurer NEW_CHILD = scope:scandi_child }
					}
				}
			}
			# chance for a second child?
			if = {
				limit = { exists = scope:scandi_child }
				random = {
					chance = 25
					modifier = {
						add = 25
						scope:scandi_adventurer.primary_spouse.age >= 30
					}
					modifier = {
						add = 25
						scope:scandi_adventurer.primary_spouse.age >= 40
					}
					create_character = {
						gender_female_chance = {
							if = {
								limit = {
									scope:scandi_child = { is_male = yes }
								}
								add = 75
							}
							else = {
								add = 25
							}
						}
						age = { 1 scandi_child_adventurer_max_age }
						employer = scope:scandi_adventurer
						faith = scope:scandi_adventurer.faith
						culture = scope:scandi_adventurer.culture
						mother = scope:scandi_adventurer.primary_spouse
						father = scope:scandi_adventurer
						dynasty = inherit
						random_traits = yes
						save_scope_as = scandi_child2
						after_creation = {
							remove_bad_congenital_traits = yes
							child_inherit_education_effect = { PRIMARY_PARENT = scope:scandi_adventurer NEW_CHILD = scope:scandi_child2 }
							if = {
								limit = { age = scope:scandi_child.age }
								add_trait = twin
								scope:scandi_child = { add_trait = twin }
							}
						}
					}
				}
			}
		}
	}
	# now check for an unmarried female adventurer of 25+
	else_if = {
		limit = {
			age >= 25
			is_female = yes
			is_married = no
			can_marry_common_trigger = yes
		}
		# give her a warrior husband of her culture
		create_character = {
			template = new_commander_character
			gender_female_chance = 0
			employer = scope:scandi_adventurer
			faith = scope:scandi_adventurer.faith
			culture = scope:scandi_adventurer.culture
			prowess = { medium_skill_rating high_skill_rating }
			save_scope_as = scandi_husband
			after_creation = {
				marry_matrilineal = scope:scandi_adventurer
				remove_bad_congenital_traits = yes
				spawned_soldier_traits_effect = yes
			}
		}
		# potentially give her a child if she's childless and a spawned character (has no parents)
		if = {
			limit = {
				is_married = yes
				can_have_children = yes
				NOR = {
					exists = mother
					exists = father
					any_child = { is_alive = yes }
				}
			}
			random = {
				chance = 50
				modifier = {
					add = 25
					scope:scandi_adventurer.age >= 30
				}
				modifier = {
					add = 25
					scope:scandi_adventurer.age >= 35
				}
				create_character = {
					gender_female_chance = 50
					age = { 1 scandi_child_adventurer_max_age }
					employer = scope:scandi_adventurer
					faith = scope:scandi_adventurer.faith
					culture = scope:scandi_adventurer.culture
					mother = scope:scandi_adventurer
					father = scope:scandi_adventurer.primary_spouse
					dynasty = inherit
					random_traits = yes
					save_scope_as = scandi_child
					after_creation = {
						remove_bad_congenital_traits = yes
						child_inherit_education_effect = { PRIMARY_PARENT = scope:scandi_adventurer NEW_CHILD = scope:scandi_child }
					}
				}
			}
			# chance for a second child?
			if = {
				limit = { exists = scope:scandi_child }
				random = {
					chance = 25
					modifier = {
						add = 25
						scope:scandi_adventurer.age >= 30
					}
					modifier = {
						add = 25
						scope:scandi_adventurer.age >= 35
					}
					create_character = {
						gender_female_chance = {
							if = {
								limit = {
									scope:scandi_child = { is_male = yes }
								}
								add = 75
							}
							else = {
								add = 25
							}
						}
						age = { 1 scandi_child_adventurer_max_age }
						employer = scope:scandi_adventurer
						faith = scope:scandi_adventurer.faith
						culture = scope:scandi_adventurer.culture
						mother = scope:scandi_adventurer
						father = scope:scandi_adventurer.primary_spouse
						dynasty = inherit
						random_traits = yes
						save_scope_as = scandi_child2
						after_creation = {
							remove_bad_congenital_traits = yes
							child_inherit_education_effect = { PRIMARY_PARENT = scope:scandi_adventurer NEW_CHILD = scope:scandi_child2 }
							if = {
								limit = { age = scope:scandi_child.age }
								add_trait = twin
								scope:scandi_child = { add_trait = twin }
							}
						}
					}
				}
			}
		}
	}
}

spawned_soldier_traits_effect = {
	if = {
		limit = {
			is_female = yes
			has_education_martial_trigger = yes
			culture = { has_cultural_parameter = has_access_to_shieldmaidens }
			NOR = {
				has_trait = shieldmaiden
				culture = { has_cultural_parameter = martial_custom_female_only_combatant }
				faith = { has_doctrine_parameter = combatant_must_be_female_if_no_roco }
				culture = { has_cultural_parameter = martial_custom_equal_combatant }
				faith = { has_doctrine_parameter = combatant_can_be_either_gender_if_no_roco }
			}
		}
		add_trait = shieldmaiden
	}
	if = {
		limit = {
			has_education_martial_trigger = yes
			OR = {
				is_male = yes
				AND = {
					is_female = yes
					OR = {
						culture = { has_cultural_parameter = martial_custom_female_only_combatant }
						faith = { has_doctrine_parameter = combatant_must_be_female_if_no_roco }
						culture = { has_cultural_parameter = martial_custom_equal_combatant }
						faith = { has_doctrine_parameter = combatant_can_be_either_gender_if_no_roco }
					}
				}
			}
		}
		random_list = {
			50 = {
				trigger = {
					is_female = yes
					NOT = { has_trait = shieldmaiden }
					culture = { has_cultural_parameter = has_access_to_shieldmaidens }
				}
				add_trait = shieldmaiden
			}
			25 = {
				trigger = {
					is_male = yes
					NOR = {
						has_trait = berserker
						has_trait = viking
					}
					faith = { has_doctrine = tenet_warmonger }
					OR = {
						culture = { has_cultural_pillar = heritage_north_germanic }
						religion = religion:germanic_religion
					}
				}
				add_trait = berserker
			}
			25 = {
				trigger = {
					is_male = yes
					NOR = {
						has_trait = berserker
						has_trait = viking
					}
					faith = { has_doctrine = tenet_warmonger }
					OR = {
						culture = { has_cultural_pillar = heritage_north_germanic }
						religion = religion:germanic_religion
					}
				}
				add_trait = viking
			}
			25 = {
				trigger = {
					NOT = { has_trait = physique_good }
				}
				add_trait = physique_good_1
			}
			25 = {
				trigger = {
					NOT = { has_trait = physique_good }
				}
				add_trait = physique_good_2
			}
			25 = {
				trigger = {
					NOT = { has_trait = lifestyle_blademaster }
				}
				add_trait = lifestyle_blademaster
				add_trait_xp = {
					trait = lifestyle_blademaster
					value = {
						integer_range = {
							min = medium_lifestyle_random_xp_low
							max = medium_lifestyle_random_xp_high
						}
					}
				}
			}
			50 = {}
		}
	}
}

scandi_adventurer_army_spawn = {
	# spawn regular levies
	if = {
		limit = { scandi_levy_total > 5000 }
		spawn_army = {
			levies = {
				add = scandi_levy_total
				subtract = 500
				multiply = 0.5
				round = yes
			}
			war = $SCANDI_WAR$
			inheritable = no
			location = $LAUNCH$
			name = scandi_adventurer_event_troops
		}
		spawn_army = {
			levies = {
				add = scandi_levy_total
				subtract = 500
				multiply = 0.5
				round = yes
			}
			war = $SCANDI_WAR$
			inheritable = no
			location = $LAUNCH$
			name = scandi_adventurer_event_troops
		}
	}
	else_if = {
		limit = { scandi_levy_total > 1000 }
		spawn_army = {
			levies = {
				add = scandi_levy_total
				subtract = 1000
			}
			war = $SCANDI_WAR$
			inheritable = no
			location = $LAUNCH$
			name = scandi_adventurer_event_troops
		}
	}
	# spawn maa regiments
	if = {
		limit = { is_ai = no }
		if = {
			limit = { has_fp1_dlc_trigger = no }
			spawn_army = {
				levies = 1000
				men_at_arms = {
					type = huscarl
					stacks = {
						value = 1
						multiply = $TARGET$.holder.top_liege.primary_title.tier
					}
				}
				men_at_arms = {
					type = light_footmen
					stacks = {
						value = 1
						multiply = $TARGET$.holder.top_liege.primary_title.tier
					}
				}
				war = $SCANDI_WAR$
				inheritable = no
				location = $LAUNCH$
				name = scandi_adventurer_event_troops
			}
		} 
		else = {
			spawn_army = {
				levies = 1000
				men_at_arms = {
					type = huscarl
					stacks = {
						value = 1
						multiply = $TARGET$.holder.top_liege.primary_title.tier
					}
				}
				men_at_arms = {
					type = bondi
					stacks = {
						value = 1
						multiply = $TARGET$.holder.top_liege.primary_title.tier
					}
				}
				men_at_arms = {
					type = vigmen
					stacks = {
						value = 1
						multiply = $TARGET$.holder.top_liege.primary_title.tier
					}
				}
				war = $SCANDI_WAR$
				inheritable = no
				location = $LAUNCH$
				name = scandi_adventurer_event_troops
			}
		}
	}
	else_if = {
		limit = {
			has_character_flag = prioritised_scandinavian_adventurer
			is_ai = yes
		}
		if = {
			limit = { has_fp1_dlc_trigger = no }
			spawn_army = {
				levies = 1000
				men_at_arms = {
					type = huscarl
					stacks = {
						value = scandi_maa_total
						multiply = 0.5
						round = yes
					}
				}
				men_at_arms = {
					type = light_footmen
					stacks = {
						value = scandi_maa_total
						multiply = 0.5
						round = yes
					}
				}
				inheritable = yes
				location = $LAUNCH$
				name = scandi_adventurer_event_troops
				save_scope_as = main_scandi_army
			}
		} 
		else = {
			if = {
				limit = { has_fp1_dlc_trigger = no }
				spawn_army = {
					levies = 1000
					men_at_arms = {
						type = huscarl
						stacks = {
							value = scandi_maa_total
							multiply = 0.5
							round = yes
						}
					}
					men_at_arms = {
						type = light_footmen
						stacks = {
							value = scandi_maa_total
							multiply = 0.5
							round = yes
						}
					}
					inheritable = yes
					location = $LAUNCH$
					name = scandi_adventurer_event_troops
					save_scope_as = main_scandi_army
				}
			} 
			else = {
				if = {
					limit = { scandi_maa_total <= 30 }
					spawn_army = {
						levies = 1000
						men_at_arms = {
							type = huscarl
							stacks = {
								value = scandi_maa_total
								multiply = 0.33
								round = yes
							}
						}
						men_at_arms = {
							type = bondi
							stacks = {
								value = scandi_maa_total
								multiply = 0.33
								round = yes
							}
						}
						men_at_arms = {
							type = vigmen
							stacks = {
								value = scandi_maa_total
								multiply = 0.33
								round = yes
							}
						}
						inheritable = yes
						location = $LAUNCH$
						name = scandi_adventurer_event_troops
						save_scope_as = main_scandi_army
					}
				}
				else = {
					spawn_army = {
						levies = 1000
						men_at_arms = {
							type = huscarl
							stacks = {
								value = scandi_maa_total
								subtract = 10
								multiply = 0.33
								round = yes
							}
						}
						men_at_arms = {
							type = bondi
							stacks = {
								value = scandi_maa_total
								subtract = 10
								multiply = 0.33
								round = yes
							}
						}
						men_at_arms = {
							type = vigmen
							stacks = {
								value = scandi_maa_total
								subtract = 10
								multiply = 0.33
								round = yes
							}
						}
						inheritable = yes
						location = $LAUNCH$
						name = scandi_adventurer_event_troops
						save_scope_as = main_scandi_army
					}
					spawn_army = {
						men_at_arms = {
							type = handpicked_faithful
							stacks = {
								value = scandi_maa_total
								subtract = 30
							}
						}
						inheritable = yes
						location = $LAUNCH$
						name = scandi_adventurer_event_troops
					}
				}
			}
		}
	}
	else = {
		if = {
			limit = { has_fp1_dlc_trigger = no }
			spawn_army = {
				levies = 1000
				men_at_arms = {
					type = huscarl
					stacks = {
						value = scandi_maa_total
						multiply = 0.5
						round = yes
					}
				}
				men_at_arms = {
					type = light_footmen
					stacks = {
						value = scandi_maa_total
						multiply = 0.5
						round = yes
					}
				}
				inheritable = no
				location = $LAUNCH$
				name = scandi_adventurer_event_troops
				save_scope_as = main_scandi_army
			}
		} 
		else = {
			if = {
				limit = { scandi_maa_total <= 30 }
				spawn_army = {
					levies = 1000
					men_at_arms = {
						type = huscarl
						stacks = {
							value = scandi_maa_total
							multiply = 0.33
							round = yes
						}
					}
					men_at_arms = {
						type = bondi
						stacks = {
							value = scandi_maa_total
							multiply = 0.33
							round = yes
						}
					}
					men_at_arms = {
						type = vigmen
						stacks = {
							value = scandi_maa_total
							multiply = 0.33
							round = yes
						}
					}
					inheritable = no
					location = $LAUNCH$
					name = scandi_adventurer_event_troops
					save_scope_as = main_scandi_army
				}
			}
			else = {
				spawn_army = {
					levies = 1000
					men_at_arms = {
						type = huscarl
						stacks = {
							value = scandi_maa_total
							subtract = 10
							multiply = 0.33
							round = yes
						}
					}
					men_at_arms = {
						type = bondi
						stacks = {
							value = scandi_maa_total
							subtract = 10
							multiply = 0.33
							round = yes
						}
					}
					men_at_arms = {
						type = vigmen
						stacks = {
							value = scandi_maa_total
							subtract = 10
							multiply = 0.33
							round = yes
						}
					}
					inheritable = no
					location = $LAUNCH$
					name = scandi_adventurer_event_troops
					save_scope_as = main_scandi_army
				}
				spawn_army = {
					men_at_arms = {
						type = huscarl
						stacks = {
							value = scandi_maa_total
							subtract = 30
							multiply = 0.5
							round = yes
						}
					}
					men_at_arms = {
						type = bondi
						stacks = {
							value = scandi_maa_total
							subtract = 30
							multiply = 0.5
							round = yes
						}
					}
					inheritable = yes
					location = $LAUNCH$
					name = scandi_adventurer_event_troops
				}
			}
		}
	}
	if = {
		limit = {
			is_ai = yes
			culture = { has_innovation = innovation_catapult }
		}
		spawn_army = {
			men_at_arms = {
				type = onager
				stacks = 3
			}
			location = $LAUNCH$
			war = $SCANDI_WAR$
			name = scandi_adventurer_event_troops
		}
	}
	if = {
		limit = { exists = scope:main_scandi_army }
		scope:main_scandi_army = { assign_commander = prev }
	}
}

character_becomes_adventurer_effect = {
	save_scope_as = new_landless_adventurer
	
	create_adventurer_title = {
		name = $BAND$
		holder = scope:new_landless_adventurer
		article = DEFAULT_TITLE_NAME_ARTICLE
		save_scope_as = new_title
	}	
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	scope:new_title ?= {
		if = {
			limit = {
				NOT = { $COA$ = no }
			}
			set_coa = $COA$
		}
		create_landless_adventurer_title_history_effect = yes
		set_variable = { name = adventurer_creation_reason value = flag:wanderer }
		set_variable = { name = adventurer_flavor_char value = scope:new_landless_adventurer }
		change_title_holder = {
			holder = scope:new_landless_adventurer
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
	scope:new_title = { save_scope_as = new_landless_adventurer_title }
	if = {
		limit = { NOT = { has_government = landless_adventurer_government } }
		change_government = landless_adventurer_government
	}
	if = {
		limit = { NOT = { has_realm_law = landless_adventurer_succession_law } }
		add_realm_law = landless_adventurer_succession_law
	}
	if = {
		limit = { has_realm_law = crown_authority_0 }
		remove_realm_law = crown_authority_0
	}
	domicile = { change_provisions = starting_provisions_value }
	every_player = {
		limit = { in_diplomatic_range = $ADVENTURER$ }
		send_interface_message = {
			type = msg_landless_adventurer_nearby
			title = nearby_landless_adventurer_created_message_title
			left_icon = $ADVENTURER$
			custom_tooltip = nearby_landless_adventurer_created_message_wanderer_tt
		}
	}
	every_councillor = {
		if = {
			limit = {
				NOR = {
					has_council_position = councillor_court_chaplain
					has_council_position = councillor_spouse
				}
			}
			$ADVENTURER$ = { fire_councillor = prev }
			remove_opinion = { modifier = fired_from_council_opinion target = $ADVENTURER$ }
		}
		else_if = {
			limit = {
				NOR = {
					is_close_or_extended_family_of = $ADVENTURER$
					has_relation_lover = $ADVENTURER$
					has_relation_friend = $ADVENTURER$
					is_consort_of = $ADVENTURER$
					has_council_position = councillor_spouse
				}
			}
			death = { death_reason = death_vanished }
		}
	}
	every_courtier = {
		limit = {
			NOR = {
				is_close_or_extended_family_of = $ADVENTURER$
				has_relation_lover = $ADVENTURER$
				has_relation_friend = $ADVENTURER$
				is_consort_of = $ADVENTURER$
			}
		}
		move_to_pool = yes
	}
	histmod_spawn_court_effect = yes
	local_arbitration_boon_change_purpose_effect = yes
	add_to_global_variable_list = {
		name = laamps_tally
		target = scope:new_landless_adventurer.primary_title
	}
	set_variable = flavourise_camp_purpose_wanderers 
}

histmod_spawn_court_effect = {
	save_temporary_scope_as = new_ruler
	create_character = {
		template = new_commander_character
		gender_female_chance = scandi_commander_female_chance
		employer = scope:new_ruler
		faith = scope:new_ruler.faith
		culture = scope:new_ruler.culture
		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:new_ruler
		faith = scope:new_ruler.faith
		culture = scope:new_ruler.culture
		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:new_ruler
		faith = scope:new_ruler.faith
		culture = scope:new_ruler.culture
		prowess = { medium_skill_rating high_skill_rating }
		after_creation = {
			spawned_soldier_traits_effect = yes
		}
	}
	create_character = {
		template = histmod_physician_template
		employer = scope:new_ruler
		faith = scope:new_ruler.faith
		culture = scope:new_ruler.culture
		dynasty = none
		save_scope_as = new_physician
		after_creation = {
			set_court_physician_effect = {
				EMPLOYER = scope:new_ruler
				PHYSICIAN = scope:new_physician
			}
		}
	}
}



