namespace = fp1_shieldmaiden

scripted_trigger wrong_gender_for_fighting_in_faith_trigger = {
	#Has access to the appropriate cultural parameter.
	culture = { has_cultural_parameter = has_access_to_shieldmaidens }
	trigger_if = { # Has the Royal Court and thus can modify pillars
		limit = {
			has_dlc_feature = diverge_culture # Can modify pillars
		}
		OR = {
			AND = {
				culture = { has_cultural_parameter = martial_custom_male_only_combatant }
				is_female = yes
			}
			AND = {
				culture = { has_cultural_parameter = martial_custom_female_only_combatant }
				is_male = yes
			}
		}
	}
	trigger_else = { # Does not have the Royal Court and thus combatant is governed by faith
		OR = {
			AND = {
				faith = { has_doctrine_parameter = combatant_must_be_male_if_no_roco }
				is_female = yes
			}
			AND = {
				faith = { has_doctrine_parameter = combatant_must_be_female_if_no_roco }
				is_male = yes
			}
		}
	}
}

##################################################
# Shieldmaiden Challenges You
# by Ewan Cowhig Croft
# 0001 - 0010
##################################################

scripted_trigger likely_to_be_offended_list_trigger = {
	trigger_if = { # Has the Royal Court and thus can modify pillars
		limit = { has_dlc_feature = diverge_culture }
		culture = {
			NOT = { has_cultural_parameter = martial_custom_equal_combatant	}
		}
	}
	trigger_else = {
		#Separate religions already think your ways are terrible already, co-religionists think you're making them look bad.
		religion = root.religion
		#No bias, no problem.
		faith = {
			NOT = { has_doctrine_parameter = combatant_can_be_either_gender_if_no_roco }
		}
	}
	culture = {
		# Warrior by merit, if strong enought you can be a warrior regardless of gender
		NOT = { has_cultural_parameter = high_prowess_ignores_knight_restrictions }
	}
	#Existing shieldmaidens cut slack.
	NOT = { has_trait = shieldmaiden }
	age >= childhood_education_start_age
}

scripted_trigger valid_to_become_shieldperson_trigger = {
	wrong_gender_for_fighting_in_faith_trigger = yes
	is_available_ai_adult = yes
	prowess >= decent_skill_rating
	NOT = { has_trait = shieldmaiden }
	NOT = { has_trait = craven }
}

scripted_trigger child_dreams_of_shieldpersonhood_trigger = {
	valid_to_become_shieldperson_trigger = yes
	is_courtier_of = root
	location = root.location
	has_character_flag = fp1_dreams_of_shieldpersondom
}

#	A wrong-gender courtier wishes to fight for you
fp1_shieldmaiden.0001 = {
	type = character_event
	title = fp1_shieldmaiden.0001.t
	desc = fp1_shieldmaiden.0001.desc
	theme = martial
	left_portrait = {
		character = scope:shieldmaiden
		animation = personality_bold
	}
	right_portrait = {
		character = scope:challenged_knight
		animation = dismissal
	}
	override_background = { reference = throne_room }

	trigger = {
		#DLC check.
		has_fp1_dlc_trigger = yes
		#Standard checks.
		is_available_at_peace_adult = yes
		is_landed = yes
		NOT = { has_character_flag = had_event_fp1_shieldmaiden_0001 }
		#Has access to the appropriate cultural parameter.
		culture = { has_cultural_parameter = has_access_to_shieldmaidens }
		#Your society must consider one gender or the other lesser.
		trigger_if = { # Has the Royal Court and thus can modify pillars
			limit = { has_dlc_feature = diverge_culture }
			culture = { NOT = { has_cultural_parameter = martial_custom_equal_combatant } }
		}
		trigger_else = {
			faith = { NOT = { has_doctrine_parameter = combatant_can_be_either_gender_if_no_roco } }
		}
		#You've got to have at least one suitable courtier or guest.
		any_courtier_or_guest = { valid_to_become_shieldperson_trigger = yes }
		#Aaaand you need a decent knight to be challenged.
		any_knight = {
			is_available_ai_adult = yes
			is_ruler = no
			wrong_gender_for_fighting_in_faith_trigger = no
			NOT = { has_trait = shieldmaiden }
		}
	}

	weight_multiplier = {
		base = 1

		#More likely if you're a ruling wrong-gender person yourself.
		modifier = {
			add = 1
			wrong_gender_for_fighting_in_faith_trigger = yes
		}
		#Even more so if you're actually a shieldmaiden yourself.
		modifier = {
			add = 1
			has_trait = shieldmaiden
		}
		# Weight up drastically if one of your children wishes to become a shieldmaiden.
		modifier = {
			add = 10
			any_child = { child_dreams_of_shieldpersonhood_trigger = yes }
		}
	}

	immediate = {
		#Put the event on cooldown for a decade.
		add_character_flag = {
			flag = had_event_fp1_shieldmaiden_0001
			days = 3650
		}
		#Sort the best possible shieldmaiden.
		ordered_courtier_or_guest = {
			# Grab children who were promised first.
			limit = {
				is_child_of = root
				child_dreams_of_shieldpersonhood_trigger = yes
			}
			# Otherwise, look down available courtiers.
			alternative_limit = {
				valid_to_become_shieldperson_trigger = yes
				#Check that it makes sense for this character to ask to be your shieldmaiden.
				OR = {
					#Meaning they should either not be one already...
					NOT = { has_trait = shieldmaiden }
					#... or be a wandering shieldmaiden.
					is_pool_guest = yes
				}
			}
			order_by = {
				value = prowess
				if = {
					limit = { has_trait = brave }
					add = 10
				}
				if = {
					limit = { has_trait = craven }
					add = -10
				}
				if = {
					limit = { has_trait = ambitious }
					add = 10
				}
				if = {
					limit = { has_trait = content }
					add = -10
				}
				if = {
					limit = { has_trait = arrogant }
					add = 5
				}
				if = {
					limit = { has_trait = humble }
					add = -5
				}
				if = {
					limit = { has_trait = gregarious }
					add = 5
				}
				if = {
					limit = { has_trait = shy }
					add = -5
				}
			}
			save_scope_as = shieldmaiden
		}
		#And a suitable knight.
		ordered_knight = {
			limit = {
				is_available_ai_adult = yes
				is_ruler = no
				wrong_gender_for_fighting_in_faith_trigger = no
				NOT = { has_trait = shieldmaiden }
			}
			order_by = prowess
			save_scope_as = challenged_knight
		}
		#For reference, add all courtiers & vassals who are likely to be offended if you just let any old wrong-gender human defy societal gender norms.
		every_courtier = {
			limit = { likely_to_be_offended_list_trigger = yes }
			add_to_list = prejudiced_courtiers_list
		}
		every_vassal = {
			limit = { likely_to_be_offended_list_trigger = yes }
			add_to_list = prejudiced_courtiers_list
		}
		#Finally, if scope:shieldmaiden has dreamt of this day, clear the dream. Hopefully it went positively!
		scope:shieldmaiden = {
			if = {
				limit = { has_character_flag = fp1_dreams_of_shieldpersondom }
				remove_character_flag = fp1_dreams_of_shieldpersondom
			}
		}
	}

	#You allow them to fight your knight.
	option = {
		name = fp1_shieldmaiden.0001.a

		scope:challenged_knight = {
			#Your knight may hurt the prospective shieldmaiden.
			scope:shieldmaiden = {
				random = {
					chance = scope:challenged_knight.prowess
					increase_wounds_no_death_effect = { REASON = duel }
				}
			}
			#And, depending on how fearsome they were, your knight may have been hurt right back.
			random = {
				chance = scope:shieldmaiden.prowess
				increase_wounds_no_death_effect = { REASON = duel }
			}

			#Sort the duel.
			duel = {
				skill = prowess
				target = scope:shieldmaiden
				#Your knight defeats the prospective shieldmaiden.
				40 = {
					compare_modifier = {
						value = scope:duel_value
						multiplier = 3.5
						min = -49
					}
					desc = fp1_shieldmaiden.0001.a.tt_success
					root = {
						send_interface_toast = {
							title = fp1_shieldmaiden.0001.a.tt_success
							left_icon = scope:shieldmaiden
							scope:shieldmaiden = {
								#scope:shieldmaiden is a tad miffed.
								add_opinion = {
									target = scope:challenged_knight
									modifier = angry_opinion
									opinion = -20
								}
							}
							#For your trouble, you and the knight split the prestige.
							add_prestige = minor_prestige_gain
							scope:challenged_knight = { add_prestige = minor_prestige_gain }
						}
					}
				}
				#Your knight is defeated.
				60 = {
					compare_modifier = {
						value = scope:duel_value
						multiplier = -3.5
						min = -49
					}
					desc = fp1_shieldmaiden.0001.a.tt_failure
					root = {
						send_interface_toast = {
							title = fp1_shieldmaiden.0001.a.tt_failure
							left_icon = scope:shieldmaiden
							scope:shieldmaiden = {
								#Officially designated a shieldmaiden!
								add_trait = shieldmaiden
								#Happy with the outcome of the duel.
								add_opinion = {
									target = scope:challenged_knight
									modifier = pleased_opinion
									opinion = 20
								}
								#If scope:shieldmaiden is a guest, you get them for free.
								if = {
									limit = { is_pool_guest = yes }
									root = { add_courtier = scope:shieldmaiden }
								}
							}
						}
					}
				}
			}
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 0.25
				ai_energy = -0.25
				ai_boldness = -0.25
			}
			modifier = {	#Cowardly shieldmaidens don't see the necessity of a fight.
				add = -40
				has_trait = shieldmaiden
				has_trait = craven
			}
			modifier = {	#Brave shieldmaidens, on the other hand, will insist you earn it (either way).
				add = 20
				has_trait = shieldmaiden
				has_trait = brave
			}
		}
	}

	#You take the challenge yourself.
	option = {
		name = fp1_shieldmaiden.0001.b

		#You may have hurt the prospective shieldmaiden in the duel.
		scope:shieldmaiden = {
			random = {
				chance = root.prowess
				increase_wounds_no_death_effect = { REASON = duel }
			}
		}
		#Plus they may have hurt you right back.
		random = {
			chance = scope:shieldmaiden.prowess
			increase_wounds_no_death_effect = { REASON = duel }
		}

		#Sort the duel.
		duel = {
			skill = prowess
			target = scope:shieldmaiden
			#You defeat the prospective shieldmaiden.
			40 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = fp1_shieldmaiden.0001.b.tt_success
				send_interface_toast = {
					title = fp1_shieldmaiden.0001.b.tt_success
					left_icon = scope:shieldmaiden
					scope:shieldmaiden = {
						#scope:shieldmaiden is a tad miffed.
						add_opinion = {
							target = root
							modifier = angry_opinion
							opinion = -20
						}
					}
					#Since you fought yourself, you get all the prestige.
					add_prestige = medium_prestige_gain
				}
			}
			#You are defeated.
			60 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = fp1_shieldmaiden.0001.b.tt_failure
				send_interface_toast = {
					title = fp1_shieldmaiden.0001.b.tt_failure
					left_icon = scope:shieldmaiden
					scope:shieldmaiden = {
						#Officially designated a shieldmaiden!
						add_trait = shieldmaiden
						#Happy with the outcome of the duel.
						add_opinion = {
							target = root
							modifier = pleased_opinion
							opinion = 20
						}
						#If scope:shieldmaiden is a guest, you get them for free.
						if = {
							limit = { is_pool_guest = yes }
							root = { add_courtier = scope:shieldmaiden }
						}
					}
				}
			}
		}

		stress_impact = {
			brave = medium_stress_impact_loss
			craven = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_vengefulness = 0.25
			}
			modifier = {	#Weight up for stress.
				add = 20
				has_trait = brave
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = craven
			}
			modifier = {	#Cowardly shieldmaidens don't see the necessity of a fight.
				add = -40
				has_trait = shieldmaiden
				has_trait = craven
			}
			modifier = {	#Brave shieldmaidens, on the other hand, will insist you earn it (either way).
				add = 20
				has_trait = shieldmaiden
				has_trait = brave
			}
		}
	}

	#Accept them outright.
	option = {
		name = fp1_shieldmaiden.0001.c

		scope:shieldmaiden = {
			#Grant the shieldmaiden trait.
			add_trait = shieldmaiden
			#Opinion gain with new shieldmaiden.
			add_opinion = {
				target = root
				modifier = fp1_accepted_my_sword_opinion
			}
			#If scope:shieldmaiden is a guest, you get them for free.
			if = {
				limit = { is_pool_guest = yes }
				root = { add_courtier = scope:shieldmaiden }
			}
		}
		#Loyalty hook on the new shieldmaiden for your trust.
		if = {
			limit = {
				NOR = {
					has_hook_of_type = {
						target = scope:shieldmaiden
						type = loyalty_hook
					}
					has_hook_of_type = {
						target = scope:shieldmaiden
						type = ritual_best_friend_hook
					}
				}
			}
			if = {
				limit = {
					has_hook = scope:shieldmaiden
				}
				hidden_effect = { remove_hook = { target = scope:shieldmaiden } }
			}
			add_hook = {
				type = loyalty_hook
				target = scope:shieldmaiden
			}
		}
		#Opinion loss with all courtiers who do not feel they should be able to fight.
		if = {
			limit = {
				any_in_list = {
					list = prejudiced_courtiers_list
					count >= 1
				}
			}
			every_in_list = {
				list = prejudiced_courtiers_list
				limit = {
					#Exempt shieldmaidens; we do this at this point, otherwise the shieldmaiden themself will objet to their own shieldmaidenry.
					NOT = { has_trait = shieldmaiden }
				}
				custom = fp1_accepted_shieldmaiden_without_question
				add_opinion = {
					target = root
					modifier = fp1_defied_religious_traditions_opinion
				}
			}
		}

		stress_impact = {
			arbitrary = minor_stress_impact_loss
			just = minor_stress_impact_gain
			arrogant = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_honor = 0.25
				ai_zeal = -0.25
			}
			modifier = {	#Weight down for stress.
				add = 10
				has_trait = arbitrary
			}
			modifier = {	#Weight up for stress.
				add = -10
				has_trait = just
			}
			modifier = {	#Weight up for stress.
				add = -20
				has_trait = arrogant
			}
			modifier = {	#Cowardly shieldmaidens don't see the necessity of a fight.
				add = 40
				has_trait = shieldmaiden
				has_trait = craven
			}
			modifier = {	#Brave shieldmaidens, on the other hand, will insist you earn it.
				add = -40
				has_trait = shieldmaiden
				has_trait = brave
			}
		}
	}

	#Reject them outright.
	option = {
		name = fp1_shieldmaiden.0001.d

		#Hefty opinion loss.
		reverse_add_opinion = {
			target = scope:shieldmaiden
			modifier = fp1_rejected_my_sword_opinion
		}

		#Stepped rivalry between you and the potential shieldmaiden.
		progress_towards_rival_effect = {
			REASON = rival_shieldmaiden_declined
			CHARACTER = scope:shieldmaiden
			OPINION = 0
		}

		stress_impact = {
			arbitrary = minor_stress_impact_loss
			arrogant = medium_stress_impact_loss
			just = minor_stress_impact_gain
			humble = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 0.25
				ai_compassion = -0.25
				ai_honor = -0.25
			}
			modifier = {	#Weight up for stress.
				add = 10
				has_trait = arbitrary
			}
			modifier = {	#Weight up for stress.
				add = 20
				has_trait = arrogant
			}
			modifier = {	#Weight down for stress.
				add = -10
				has_trait = just
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = humble
			}
			modifier = {	#Shieldmaidens are, themselves, more likely to give someone a shot.
				add = -40
				has_trait = shieldmaiden
			}
		}
	}
}

##################################################
# Shieldmaiden Stymies an Escape Attempt
# by Ewan Cowhig Croft
# 0011 - 0020
##################################################

#	A shieldmaiden stops your escape attempt in its tracks.
fp1_shieldmaiden.0011 = {
	type = character_event
	title = fp1_shieldmaiden.0011.t
	desc = fp1_shieldmaiden.0011.desc
	theme = prison
	left_portrait = {
		character = scope:prisoner
		animation = fear
	}
	right_portrait = {
		character = scope:shieldmaiden
		animation = disapproval
	}

	trigger = {
		#DLC check.
		has_fp1_dlc_trigger = yes
		#Requires a shieldmaiden to get going.
		imprisoner = {
			any_courtier = { suitable_shieldmaiden_gaoler_trigger = yes }
		}
	}

	immediate = {
		play_music_cue = "mx_cue_prison"
		save_scope_as = prisoner
		imprisoner = {
			#Grab an appropriate shieldmaiden.
			random_courtier = {
				limit = { suitable_shieldmaiden_gaoler_trigger = yes }
				save_scope_as = shieldmaiden
			}
			#Inform the imprisoner so that they're aware they're getting value for money.
			trigger_event = fp1_shieldmaiden.0012
		}
		scope:imprisoner = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = -20
			}
		}
	}

	#The shieldmaiden lays the smackdown.
	option = {
		name = fp1_shieldmaiden.0011.a
		increase_wounds_no_death_effect = { REASON = beaten }
	}
}

#	A shieldmaiden has stopped one of your prisoners escaping.
fp1_shieldmaiden.0012 = {
	type = character_event
	title = fp1_shieldmaiden.0012.t
	desc = fp1_shieldmaiden.0012.desc
	theme = prison
	left_portrait = {
		character = scope:prisoner
		animation = fear
	}
	right_portrait = {
		character = scope:shieldmaiden
		animation = disapproval
	}

	trigger = {
		scope:prisoner = {
			is_of_major_or_minor_interest_trigger = { CHARACTER = scope:imprisoner }
		}
	}

	immediate = {
		play_music_cue = "mx_cue_prison"
		#Show the wounds.
		show_as_tooltip = {
			scope:prisoner = {
				increase_wounds_effect = { REASON = beaten }
			}
		}
	}

	#Good job!
	option = {
		name = fp1_shieldmaiden.0012.a
		scope:shieldmaiden = {
			add_opinion = {
				target = scope:imprisoner
				modifier = compliment_opinion
				opinion = 10
			}
		}
	}

	#Bad job! [in case this was an overreaction]
	option = {
		name = fp1_shieldmaiden.0012.b
		scope:shieldmaiden = {
			add_opinion = {
				target = scope:imprisoner
				modifier = annoyed_opinion
				opinion = -10
			}
		}
	}
}

##################################################
# Shieldmaiden saves you from Murder Attempt
# by Petter Vilberg & Ewan Cowhig Croft
# 0021 - 0030
##################################################

scripted_trigger suitable_shieldmaiden_savior_trigger = {
	has_trait = shieldmaiden
	bodyguard_will_actually_do_job_trigger = yes
}

#	Scheme owner: shieldmaiden intervenes in murder.
fp1_shieldmaiden.0021 = {
	type = character_event
	window = scheme_failed_event
	title = fp1_shieldmaiden.0021.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:murder_method = flag:hired_thugs }
				desc = fp1_shieldmaiden.0021.desc.thugs
			}
			triggered_desc = {
				trigger = { scope:murder_method = flag:riled_up_mob }
				desc = fp1_shieldmaiden.0021.desc.mob
			}
			triggered_desc = {
				trigger = { scope:murder_method = flag:lured_into_forest }
				desc = fp1_shieldmaiden.0021.desc.lured_into_forest
			}
			triggered_desc = {
				trigger = { scope:murder_method = flag:child_pushed }
				desc = fp1_shieldmaiden.0021.desc.child_pushed
			}
			desc =  fp1_shieldmaiden.0021.desc.assassin
		}
		triggered_desc = {
			trigger = { exists = local_var:shieldmaiden_dies }
			desc = fp1_shieldmaiden.0021.shieldmaiden_dies
		}
	}
	theme = murder_scheme
	left_portrait = {
		character = scope:owner
		triggered_animation = {
			trigger = { exists = scope:scheme_discovered }
			animation = fear
		}
		animation = stress
	}
	center_portrait = {
		character = scope:bodyguard
		triggered_animation = {
			trigger = { is_alive = no }
			animation = map_fear
		}
		animation = random_weapon_aggressive
		camera = camera_event_center_pointing_left
	}
	right_portrait = {
		character = scope:target
		triggered_animation = {
			trigger = { exists = scope:scheme_discovered }
			animation = rage
		}
		animation = paranoia
	}
	widget = {
		gui = "event_window_widget_scheme"
		container = "custom_widgets_container"
	}

	trigger = {
		#DLC check.
		has_fp1_dlc_trigger = yes
		scope:target = {
			any_court_position_holder = {
				type = bodyguard_court_position
				suitable_shieldmaiden_savior_trigger = yes
			}
		}
	}

	immediate = {
		# Select a murder method.
		random_list = {
			100 = {
				save_scope_value_as = {
					name = murder_method
					value = flag:hired_assassin
				}
			}
			100 = {
				save_scope_value_as = {
					name = murder_method
					value = flag:hired_thugs
				}
			}
			100 = {
				save_scope_value_as = {
					name = murder_method
					value = flag:riled_up_mob
				}
			}
			100 = {
				save_scope_value_as = {
					name = murder_method
					value = flag:lured_into_forest
				}
			}
			100 = {
				save_scope_value_as = {
					name = murder_method
					value = flag:child_pushed
				}
				# Try to grab a suitable thing to lure them in with too.
				rooftop_murder_lure_for_child_effect = yes
			}
		}
		# Sort our shieldmaiden
		scope:target = {
			ordered_court_position_holder = {
				type = bodyguard_court_position
				limit = { suitable_shieldmaiden_savior_trigger = yes }
				order_by = { value = murder_weight_best_bodyguard_value }
				save_scope_as = shieldmaiden
			}
		}
		hidden_effect = {
			if = {
				limit = {
					# Shieldmaidens can't die from some murder methods.
					NOR = {
						scope:murder_method = flag:lured_into_forest
						scope:murder_method = flag:child_pushed
					}
				}
				random = {
					chance = {
						value = 75
						subtract = scope:shieldmaiden.prowess
					}
					set_local_variable = {
						name = shieldmaiden_dies
						value = yes
					}
				}
			}
		}
		if = {
			limit = { exists = local_var:shieldmaiden_dies }
			show_as_tooltip = {
				murder_interception_handle_extra_deaths_effect = {
					VICTIM = scope:shieldmaiden
					REASON = death_murder
				}
			}
		}
		random_dummy_gender_assassin_effect = yes
		murder_failure_effect = yes
	}

	#It's over.
	option = { name = fp1_shieldmaiden.0021.a }

	#It's not over till I say it's over!
	option = {
		name = fp1_shieldmaiden.0021.b
		trigger = {
			NOT = { exists = scope:scheme_discovered }
		}
		restart_murder_scheme_effect = yes
	}

	# Fire the rest of the outcome.
	after = {
		scope:target = { trigger_event = fp1_shieldmaiden.0022 }
	}
}

# Shieldmaiden saves you from murder
fp1_shieldmaiden.0022 = {
	type = character_event
	window = scheme_conclusion_event_no_header
	title = fp1_shieldmaiden.0022.t
	desc = {
		# This block is only for the setup of child_pushed.
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:murder_method = flag:child_pushed
					exists = scope:child_pushed_dog
				}
				desc = murder_outcome.5001.child_pushed_dog
			}
			triggered_desc = {
				trigger = {
					scope:murder_method = flag:child_pushed
					exists = scope:child_pushed_cat
				}
				desc = murder_outcome.5001.child_pushed_cat
			}
			triggered_desc = {
				trigger = {
					scope:murder_method = flag:child_pushed
					exists = scope:child_pushed_toy
				}
				desc = murder_outcome.5001.child_pushed_toy
			}
			triggered_desc = {
				trigger = {
					scope:murder_method = flag:child_pushed
				}
				desc = murder_outcome.5001.child_pushed
			}
		}
		# Shieldmaided saves you!
		first_valid = {
			triggered_desc = {
				trigger = { scope:murder_method = flag:hired_thugs }
				desc = fp1_shieldmaiden.0022.desc.thugs
			}
			triggered_desc = {
				trigger = { scope:murder_method = flag:riled_up_mob }
				desc = fp1_shieldmaiden.0022.desc.mob
			}
			triggered_desc = {
				trigger = { scope:murder_method = flag:lured_into_forest }
				desc = fp1_shieldmaiden.0022.desc.lured_into_forest
			}
			triggered_desc = {
				trigger = { scope:murder_method = flag:child_pushed }
				desc = fp1_shieldmaiden.0022.desc.child_pushed
			}
			desc =  fp1_shieldmaiden.0022.desc.assassin
		}
		# ...but did they pay the ultimate price?
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = local_var:shieldmaiden_dies
					scope:murder_method = flag:hired_assassin
				}
				desc = fp1_shieldmaiden.0022.desc.assassin.shieldmaiden_dies
			}
			triggered_desc = {
				trigger = {
					exists = local_var:shieldmaiden_dies
					scope:murder_method = flag:hired_thugs
				}
				desc = fp1_shieldmaiden.0022.desc.thugs.shieldmaiden_dies
			}
			triggered_desc = {
				trigger = {
					exists = local_var:shieldmaiden_dies
					scope:murder_method = flag:riled_up_mob
				}
				desc = fp1_shieldmaiden.0022.desc.mob.shieldmaiden_dies
			}
			triggered_desc = {
				trigger = {
					NOT = { exists = local_var:shieldmaiden_dies }
					scope:murder_method = flag:riled_up_mob
				}
				desc = fp1_shieldmaiden.0022.desc.mob.shieldmaiden_lives
			}
		}
		# And who was responsible!?
		triggered_desc = {
			trigger = { exists = scope:scheme_discovered }
			desc = fp1_shieldmaiden.0022.owner_discovered
		}
	}
	theme = murder_scheme
	override_background = {
		trigger = { scope:murder_method = flag:hired_thugs }
		reference = wilderness_scope
	}
	override_background = {
		trigger = {
			OR = {
				scope:murder_method = flag:riled_up_mob
				scope:murder_method = flag:child_pushed
			}
		}
		reference = courtyard
	}
	override_background = {
		trigger = { scope:murder_method = flag:lured_into_forest }
		reference = wilderness_forest
	}
	left_portrait = {
		character = scope:target
		triggered_animation = {
			trigger = { exists = scope:scheme_discovered }
			animation = rage
		}
		animation = paranoia
	}
	center_portrait = {
		character = scope:bodyguard
		triggered_animation = {
			trigger = { is_alive = no }
			animation = map_fear
		}
		animation = random_weapon_aggressive
	}
	right_portrait = {
		character = scope:owner_to_reveal
		triggered_animation = {
			trigger = { exists = scope:scheme_discovered }
			animation = fear
		}
		animation = stress
	}

	immediate = {
		if = {
			limit = { scope:murder_method = flag:hired_thugs }
			scope:home_province = {	save_scope_as = background_wilderness_scope }
		}
		# Handle the death of the Shieldmaiden if necessary
		if = {
			limit = { exists = local_var:shieldmaiden_dies }
			murder_interception_handle_extra_deaths_effect = {
				VICTIM = scope:shieldmaiden
				REASON = death_murder
			}
		}
	}

	#Who could do such a thing?
	option = {
		name = fp1_shieldmaiden.0022.a
		trigger = {
			NOT = { exists = scope:scheme_discovered }
		}
		custom_tooltip = murder_save.failure_unknown_owner_tt
	}

	#Vengeance!
	option = {
		name = fp1_shieldmaiden.0022.b
		trigger = { exists = scope:scheme_discovered }
		custom_tooltip = murder_save.failure_known_owner_tt
	}

	after = {
		add_character_modifier = {
			modifier = watchful_modifier
			days = watchful_modifier_duration
		}
	}
}

##################################################
# Shieldmaiden has Developed Mad Skills
# by Ewan Cowhig Croft
# 0031 - 0040
##################################################

scripted_trigger valid_for_shieldmaiden_veterancy = {
	# Must actually have the shieldmaiden trait.
	has_trait = shieldmaiden
	# Must still be in a situation where shieldmaidenhood is exceptional.
	wrong_gender_for_fighting_in_faith_trigger = yes
}

scripted_trigger valid_for_shieldmaiden_level_up = {
	# Are they an eligible shieldmaiden?
	valid_for_shieldmaiden_veterancy = yes
	# Have they met the level up requirements?
	has_variable = shieldmaiden_veterancy
	this.var:shieldmaiden_veterancy >= shieldmaiden_veterancy_cap
	# Already checked for a root shieldmaiden, but we re-check for courtiers.
	is_available_at_peace_adult = yes
	# Finally, they mustn't have all the shieldmaiden modifiers maxed out already.
	NAND = {
		has_character_modifier = shieldmaiden_level_martial_3_fp1_modifier
		has_character_modifier = shieldmaiden_level_prowess_3_fp1_modifier
		has_character_modifier = shieldmaiden_level_intrigue_3_fp1_modifier
	}
}

#	Track the number of valid battles as a shieldmaiden a character has been in.
fp1_shieldmaiden.0031 = {
	hidden = yes
	scope = combat_side

	trigger = {
		#DLC check.
		has_fp1_dlc_trigger = yes
		OR = {
			any_side_commander = { valid_for_shieldmaiden_veterancy = yes }
			any_side_knight = { valid_for_shieldmaiden_veterancy = yes }
		}
	}

	immediate = {
		# Sort side commander shieldmaidens.
		every_side_commander = {
			limit = { valid_for_shieldmaiden_veterancy = yes }
			# Boost their veterancy if they already have it.
			if = {
				limit = { exists = var:shieldmaiden_veterancy }
				change_variable = {
					name = shieldmaiden_veterancy
					add = 1
				}
			}
			# Or else set it up otherwise.
			else = {
				set_variable = {
					name = shieldmaiden_veterancy
					value = 1
				}
			}
		}
		# Sort side knight shieldmaidens.
		every_side_knight = {
			limit = { valid_for_shieldmaiden_veterancy = yes }
			if = {
				limit = { exists = var:shieldmaiden_veterancy }
				change_variable = {
					name = shieldmaiden_veterancy
					add = 1
				}
			}
			else = {
				set_variable = {
					name = shieldmaiden_veterancy
					value = 1
				}
			}
		}
	}
}

#	Your shieldmaiden improves their skills.
fp1_shieldmaiden.0032 = {
	type = character_event
	title = fp1_shieldmaiden.0032.t
	desc = {
		first_valid = {
			# Am *I* the shieldmaiden in question?
			triggered_desc = {
				trigger = { scope:shieldmaiden = root }
				desc = fp1_shieldmaiden.0032.desc.root
			}
			# Or is it a courtier?
			desc = fp1_shieldmaiden.0032.desc.courtier
		}
	}
	theme = martial
	left_portrait = {
		character = scope:shieldmaiden
		animation = rage
	}
	override_background = { reference = army_camp }

	trigger = {
		#DLC check.
		has_fp1_dlc_trigger = yes
		#Standard checks.
		is_available_at_peace_adult = yes
		NOT = { has_character_flag = had_event_fp1_shieldmaiden_0032 }
		# Must either be or have a shieldmaiden who's at veterancy cap.
		OR = {
			valid_for_shieldmaiden_level_up = yes
			any_courtier = { valid_for_shieldmaiden_level_up = yes }
		}
	}

	weight_multiplier = {
		base = 1

		# Much more likely if you're a shieldmaiden yourself.
		modifier = {
			add = 2
			has_trait = shieldmaiden
		}
	}

	immediate = {
		play_music_cue = "mx_cue_positive_effect"
		add_character_flag = {
			flag = had_event_fp1_shieldmaiden_0032
			days = 1825
		}
		# Designate a valid shieldmaiden.
		## First, we always level up root, if possible.
		if = {
			limit = { valid_for_shieldmaiden_level_up = yes }
			save_scope_as = shieldmaiden
		}
		## Otherwise, we grab all relevant courtiers.
		else = {
			every_courtier = {
				limit = { valid_for_shieldmaiden_level_up = yes }
				add_to_list = veteran_shieldmaidens
			}
		}
		## Select a suitable one from the list to be scope:shieldmaiden, prioritising the most veteran.
		if = {
			limit = {
				NOT = { exists = scope:shieldmaiden }
			}
			ordered_in_list = {
				list = veteran_shieldmaidens
				order_by = var:shieldmaiden_veterancy
				save_scope_as = shieldmaiden
			}
		}
		# Next, we reduce scope:shieldmaiden's veterancy, as they'll be levelling it up in the event.
		scope:shieldmaiden = {
			change_variable = {
				name = shieldmaiden_veterancy
				subtract = shieldmaiden_veterancy_cap
			}
		}
	}

	# Martial: look how they command the shieldwall!
	option = {
		# Separate loc for first person POV.
		name = {
			trigger = { scope:shieldmaiden = root }
			text = fp1_shieldmaiden.0032.a.root
		}
		# Aaaaand third person POV.
		name = {
			trigger = {
				scope:shieldmaiden != root
			}
			text = fp1_shieldmaiden.0032.a.courtier
		}
		# Can't have the max-level version of this modifier.
		trigger = {
			NOT = {
				scope:shieldmaiden = { has_character_modifier = shieldmaiden_level_martial_3_fp1_modifier }
			}
		}

		# Process the level-up modifier for this stat block.
		scope:shieldmaiden = {
			# If we have no level on this path, give us the first rank.
			if = {
				limit = {
					NOR = {
						has_character_modifier = shieldmaiden_level_martial_1_fp1_modifier
						has_character_modifier = shieldmaiden_level_martial_2_fp1_modifier
						has_character_modifier = shieldmaiden_level_martial_3_fp1_modifier
					}
				}
				add_character_modifier = shieldmaiden_level_martial_1_fp1_modifier
			}
			# If have the first level, quietly remove it and add the second.
			else_if = {
				limit = { has_character_modifier = shieldmaiden_level_martial_1_fp1_modifier }
				hidden_effect = { remove_character_modifier = shieldmaiden_level_martial_1_fp1_modifier }
				add_character_modifier = shieldmaiden_level_martial_2_fp1_modifier
			}
			# If we have the second level, quietly remove it and add the third.
			else_if = {
				limit = { has_character_modifier = shieldmaiden_level_martial_2_fp1_modifier }
				hidden_effect = { remove_character_modifier = shieldmaiden_level_martial_2_fp1_modifier }
				add_character_modifier = shieldmaiden_level_martial_3_fp1_modifier
			}

		}

		# No stress involved.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 0.25
				ai_rationality = 0.25
			}
		}
	}

	# Prowess: see how they swing that sword!
	option = {
		# Separate loc for first person POV.
		name = {
			trigger = { scope:shieldmaiden = root }
			text = fp1_shieldmaiden.0032.b.root
		}
		# Aaaaand third person POV.
		name = {
			trigger = {
				scope:shieldmaiden != root
			}
			text = fp1_shieldmaiden.0032.b.courtier
		}
		# Can't have the max-level version of this modifier.
		trigger = {
			NOT = {
				scope:shieldmaiden = { has_character_modifier = shieldmaiden_level_prowess_3_fp1_modifier }
			}
		}

		# Process the level-up modifier for this stat block.
		scope:shieldmaiden = {
			# If we have no level on this path, give us the first rank.
			if = {
				limit = {
					NOR = {
						has_character_modifier = shieldmaiden_level_prowess_1_fp1_modifier
						has_character_modifier = shieldmaiden_level_prowess_2_fp1_modifier
						has_character_modifier = shieldmaiden_level_prowess_3_fp1_modifier
					}
				}
				add_character_modifier = shieldmaiden_level_prowess_1_fp1_modifier
			}
			# If have the first level, quietly remove it and add the second.
			else_if = {
				limit = { has_character_modifier = shieldmaiden_level_prowess_1_fp1_modifier }
				hidden_effect = { remove_character_modifier = shieldmaiden_level_prowess_1_fp1_modifier }
				add_character_modifier = shieldmaiden_level_prowess_2_fp1_modifier
			}
			# If we have the second level, quietly remove it and add the third.
			else_if = {
				limit = { has_character_modifier = shieldmaiden_level_prowess_2_fp1_modifier }
				hidden_effect = { remove_character_modifier = shieldmaiden_level_prowess_2_fp1_modifier }
				add_character_modifier = shieldmaiden_level_prowess_3_fp1_modifier
			}

		}

		# No stress involved.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 0.25
				ai_honor = 0.25
			}
		}
	}

	# Intrigue: never a finer backstabber!
	option = {
		# Separate loc for first person POV.
		name = {
			trigger = { scope:shieldmaiden = root }
			text = fp1_shieldmaiden.0032.c.root
		}
		# Aaaaand third person POV.
		name = {
			trigger = {
				scope:shieldmaiden != root
			}
			text = fp1_shieldmaiden.0032.c.courtier
		}
		# Can't have the max-level version of this modifier.
		trigger = {
			NOT = {
				scope:shieldmaiden = { has_character_modifier = shieldmaiden_level_intrigue_3_fp1_modifier }
			}
		}

		# Process the level-up modifier for this stat block.
		scope:shieldmaiden = {
			# If we have no level on this path, give us the first rank.
			if = {
				limit = {
					NOR = {
						has_character_modifier = shieldmaiden_level_intrigue_1_fp1_modifier
						has_character_modifier = shieldmaiden_level_intrigue_2_fp1_modifier
						has_character_modifier = shieldmaiden_level_intrigue_3_fp1_modifier
					}
				}
				add_character_modifier = shieldmaiden_level_intrigue_1_fp1_modifier
			}
			# If have the first level, quietly remove it and add the second.
			else_if = {
				limit = { has_character_modifier = shieldmaiden_level_intrigue_1_fp1_modifier }
				hidden_effect = { remove_character_modifier = shieldmaiden_level_intrigue_1_fp1_modifier }
				add_character_modifier = shieldmaiden_level_intrigue_2_fp1_modifier
			}
			# If we have the second level, quietly remove it and add the third.
			else_if = {
				limit = { has_character_modifier = shieldmaiden_level_intrigue_2_fp1_modifier }
				hidden_effect = { remove_character_modifier = shieldmaiden_level_intrigue_2_fp1_modifier }
				add_character_modifier = shieldmaiden_level_intrigue_3_fp1_modifier
			}
		}

		# Minor stress revolving around paranoia: what if they backstab *YOU*, after all?
		if = {
			limit = {
				scope:shieldmaiden != root
			}
			stress_impact = {
				trusting = minor_stress_impact_loss
				paranoid = minor_stress_impact_gain
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_rationality = 0.5
				ai_vengefulness = -0.5
			}
			modifier = {	#Weight up for stress.
				add = 10
				scope:shieldmaiden != root
				has_trait = trusting
			}
			modifier = {	#Weight down for stress.
				add = -10
				scope:shieldmaiden != root
				has_trait = paranoid
			}
		}
	}
}
