﻿knight_none = {
	type = knight

	chance = {
		base = 2000
		modifier = {
			factor = 1.5
			has_perk = stalwart_leader_perk
			is_ai = no
		}
		modifier = {
			factor = 1.15
			has_perk = stalwart_leader_perk
			is_ai = yes
		}
		modifier = {
			factor = 1.25
			exists = dynasty
			dynasty = {
				has_dynasty_perk = warfare_legacy_3
			}
		}
		
		# Wounded knights hang back
		modifier = {
			factor = 1.1
			has_trait_rank = {
				trait = wounded
				rank = 1
			}
		}
		modifier = {
			factor = 1.2
			has_trait_rank = {
				trait = wounded
				rank = 2
			}
		}
		modifier = {
			factor = 1.3
			OR = {
				has_trait_rank = {
					trait = wounded
					rank = 3
				}
				has_trait = maimed
			}
		}
	}
}

knight_berserker_attack = {
	type = knight

	is_valid = {
		has_trait = berserker
		scope:combat_side.enemy_side = {
			any_side_knight = {
				is_alive = yes
				this.prowess <= root.prowess_for_combat_event_opponent
			}
		}
	}

	chance = {
		base = 30
		modifier = {
			factor = 1.5
			has_perk = stalwart_leader_perk
			is_ai = no
		}
		modifier = {
			factor = 1.15
			has_perk = stalwart_leader_perk
			is_ai = yes
		}
		modifier = {
			factor = 1.25
			exists = dynasty
			dynasty = {
				has_dynasty_perk = warfare_legacy_3
			}
		}

		modifier = {
			factor = 2
			has_trait = wrathful
		}
		modifier = {
			factor = 10
			has_trait = giant
		}
		modifier = {
			factor = 1.25
			has_trait_rank = {
				trait = wounded
				rank = 1
			}
		}
		modifier = {
			factor = 1.25
			has_trait_rank = {
				trait = wounded
				rank = 2
			}
		}
		modifier = {
			factor = 1.25
			has_trait_rank = {
				trait = wounded
				rank = 3
			}
		}
		modifier = {
			factor = 0.25
			OR = {
				has_trait = one_legged
				has_trait = disfigured
				has_trait = one_eyed
				has_trait = maimed
			}
		}
	}

	effect = {
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						is_alive = yes
						this.prowess <= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						is_alive = yes
						this.prowess <= root.prowess_for_combat_event_opponent
					}
					save_temporary_scope_as = enemy_knight
				}
			}
			if = {
				limit = {
					root = { is_alive = yes }
					scope:enemy_knight = { is_alive = yes }
				}
				add_prestige = knight_prestige_gain_on_kill
				knight_increase_prowess_chance_effect = yes
				berserker_kill_version_randomization_effect = yes
				scope:enemy_knight = {
					if = {
						limit = {
							exists = accolade
							accolade = { is_accolade_active = yes }
						}
						accolade = { add_glory = minimal_glory_gain }
					}
				}
			}
		}
	}
}

knight_become_berserker = {
	type = knight

	is_valid = {
		faith = {
			has_doctrine = tenet_warmonger
		}
		OR = {
			culture = { has_cultural_pillar = heritage_north_germanic }
			religion = religion:germanic_religion
		}
		NOR = {
			has_trait = craven
			has_trait = berserker
			has_trait = calm
		}
	}

	chance = {
		base = 30
		modifier = {
			factor = 1.5
			has_perk = stalwart_leader_perk
			is_ai = no
		}
		modifier = {
			factor = 1.15
			has_perk = stalwart_leader_perk
			is_ai = yes
		}
		modifier = {
			factor = 1.25
			exists = dynasty
			dynasty = {
				has_dynasty_perk = warfare_legacy_3
			}
		}
		modifier = {
			factor = 5
			has_trait = wrathful
		}
		modifier = {
			factor = 5
			has_trait = giant
		}
		modifier = {
			factor = 3
			has_trait = impatient
		}
		modifier = {
			factor = 2
			has_trait = sadistic
		}
		modifier = {
			factor = 2
			has_trait = brave
		}
		modifier = {
			factor = 2
			has_trait = ambitious
		}
		modifier = {
			factor = 2
			culture = { has_cultural_pillar = heritage_north_germanic }
		}
		modifier = {
			factor = 0.5
			has_trait = content
		}
		modifier = {
			factor = 0.25
			has_trait = compassionate
		}
		modifier = {
			factor = 0.25
			has_trait = temperate
		}
		modifier = {
			factor = 0.25
			has_trait = lazy
		}
		modifier = {
			factor = 0.25
			has_trait = patient
		}
		modifier = {
			factor = 0.5
			has_trait_rank = {
				trait = wounded
				rank = 1
			}
		}
		modifier = {
			factor = 0.5
			has_trait_rank = {
				trait = wounded
				rank = 2
			}
		}
		modifier = {
			factor = 0.25
			has_trait_rank = {
				trait = wounded
				rank = 3
			}
		}
		modifier = {
			factor = 0.5
			OR = {
				has_trait = one_legged
				has_trait = disfigured
				has_trait = one_eyed
				has_trait = maimed
			}
		}
	}

	effect = {
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess <= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess <= root.prowess_for_combat_event_opponent
					}
					save_temporary_scope_as = enemy_knight
				}
			}
			if = {
				limit = {
					root = { is_alive = yes }
					scope:enemy_knight = { is_alive = yes }
				}
				scope:combat_side = {
					battle_event = {
						key = "berserker_rage_killed_enemy"
						left_portrait = root
						right_portrait = scope:enemy_knight
						type = death
						target_right = yes
					}
				}
				add_prestige = knight_prestige_gain_on_kill
				knight_increase_prowess_chance_effect = yes
				add_trait = berserker
				scope:enemy_knight = {
					death = {
						death_reason = death_head_ripped_off
						killer = root
					}
				}
			}
		}
		else = {
			if = {
				limit = {
					root = { is_alive = yes }
				}
				scope:combat_side = {
					battle_event = {
						key = "berserker_rage_no_enemy"
						left_portrait = root
						type = death
						target_right = yes
					}
				}
				add_trait = berserker
			}
		}
		if = {
			limit = {
				is_acclaimed = yes
				accolade = {
					is_accolade_active = yes
				}
			}
			accolade = {
				add_glory = minimal_glory_gain
			}
		}
	}
}

knight_shieldmaiden_attack = {
	type = knight

	is_valid = {
		has_trait = shieldmaiden
		scope:combat_side.enemy_side = {
			any_side_knight = {
				is_alive = yes
				this.prowess <= root.prowess_for_combat_event_opponent
			}
		}
	}

	chance = {
		base = 30
		modifier = {
			factor = 1.5
			has_perk = stalwart_leader_perk
			is_ai = no
		}
		modifier = {
			factor = 1.15
			has_perk = stalwart_leader_perk
			is_ai = yes
		}
		modifier = {
			factor = 1.25
			exists = dynasty
			dynasty = {
				has_dynasty_perk = warfare_legacy_3
			}
		}
		modifier = {
			factor = 2
			has_trait = wrathful
		}
		modifier = {
			factor = 10
			has_trait = giant
		}
		modifier = {
			factor = 1.25
			has_trait_rank = {
				trait = wounded
				rank = 1
			}
		}
		modifier = {
			factor = 1.25
			has_trait_rank = {
				trait = wounded
				rank = 2
			}
		}
		modifier = {
			factor = 1.25
			has_trait_rank = {
				trait = wounded
				rank = 3
			}
		}
		modifier = {
			factor = 0.25
			OR = {
				has_trait = one_legged
				has_trait = disfigured
				has_trait = one_eyed
				has_trait = maimed
			}
		}
	}

	effect = {
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						is_alive = yes
						this.prowess <= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						is_alive = yes
						this.prowess <= root.prowess_for_combat_event_opponent
					}
					save_temporary_scope_as = enemy_knight
				}
			}
			if = {
				limit = {
					root = { is_alive = yes }
					scope:enemy_knight = { is_alive = yes }
				}
				add_prestige = knight_prestige_gain_on_kill
				knight_increase_prowess_chance_effect = yes
				shieldmaiden_kill_version_randomisation_effect = yes
				scope:enemy_knight = {
					if = {
						limit = {
							exists = accolade
							accolade = { is_accolade_active = yes }
						}
						# Shieldmaidens are especially good at earning glory.
						accolade = { add_glory = minor_glory_gain }
					}
				}
			}
		}
	}
}

knight_becomes_incapable = {
	type = knight

	is_valid = {
		is_incapable = no
	}

	chance = {
		# This is just random bad luck of suffering a serious head injury.
		base = 2
	}

	effect = {
		# Long as we're alive, conk 'em out.
		if = {
			limit = { is_alive = yes }
			add_trait = incapable
			create_character_memory = { type = became_incapable_due_to_battle_concussion }
			# We have to do this weird double-scoping due to some kind of dark pact with Code, else things won't work.
			scope:new_memory = { save_scope_as = battle_memory }
			scope:battle_memory = {
				set_variable = {
					name = battle_location
					value = root.location
				}
			}
		}
	}
}

knight_wounded = {
	type = knight

	is_valid = {
		NOT = {
			has_trait_rank = {
				trait = wounded
				rank = 3
			}
		}
	}

	chance = {
		base = 105
		modifier = {
			factor = { # Prowess directly reduces chance of injury/death
				value = 40 # Value at which 'immortality' happens
				subtract = prowess
				if = {
					limit = {
						has_court_position = garuda_court_position
					}
					add = garuda_prowess_svalue
				}
				divide = 40
				min = 0.1 # Everyone dies.
			}
		}
		modifier = { # Reduce chance when outnumbering the enemy
			factor = {
				value = scope:combat_side.enemy_side.side_strength
				divide = scope:combat_side.side_strength
				multiply = 1.4 # Outnumbering 2-1 reduces chance by 30%
				max = 1
			}
			scope:combat_side.side_strength > scope:combat_side.enemy_side.side_strength
		}
		modifier = {
			factor = 1.05
			culture = {
				has_cultural_parameter = knights_slightly_more_prone_to_injury
			}
		}
		modifier = {
			factor = 1.2
			faith = {
				has_doctrine_parameter = death_is_glory
			}
		}
		modifier = {
			factor = 4
			has_court_position = garuda_court_position
		}
		modifier = {
			factor = 2
			has_trait = brave
		}
		modifier = {
			factor = 0.5
			has_trait = craven
		}
		#House Knight defends family
		## Low decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_low_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_low
							
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
				}
			}
		}
		## Medium decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_medium_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
		## High decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_high_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
	}

	effect = {
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
					save_temporary_scope_as = enemy_knight
					add_prestige = knight_prestige_gain_on_wound_inverse
					knight_increase_prowess_chance_effect = yes
				}
			}
			scope:combat_side = {
				battle_event = {
					key = "knight_wounded_by_enemy"
					left_portrait = root
					right_portrait = scope:enemy_knight
					type = wound
				}
			}
			increase_wounds_effect = { REASON = fight }
			# Scope:enemy_knight gets some glory.
			scope:enemy_knight = {
				if = {
					limit = {
						exists = accolade
						accolade = { is_accolade_active = yes }
					}
					accolade = { add_glory = minimal_glory_gain }
				}
			}
		}
		else = {
			scope:combat_side = {
				battle_event = {
					key = "knight_wounded_no_enemy"
					left_portrait = root
					type = wound
				}
			}
			increase_wounds_effect = { REASON = fight }
		}
	}
}

knight_maimed = {
	type = knight

	is_valid = {
		NAND = {
			has_trait = one_legged
			has_trait = disfigured
			has_trait = one_eyed
			has_trait = maimed
		}
	}

	chance = {
		base = 33
		modifier = {
			factor = { # Prowess directly reduces chance of injury/death
				value = 30 # Value at which 'immortality' happens
				subtract = prowess
				if = {
					limit = {
						has_court_position = garuda_court_position
					}
					add = garuda_prowess_svalue
				}
				divide = 30
				min = 0.1 # Everyone dies.
			}
		}
		modifier = { # Reduce chance when outnumbering the enemy
			factor = {
				value = scope:combat_side.enemy_side.side_strength
				divide = scope:combat_side.side_strength
				multiply = 1.4 # Outnumbering 2-1 reduces chance by 30%
				max = 1
			}
			scope:combat_side.side_strength > scope:combat_side.enemy_side.side_strength
		}
		modifier = { # No deaths when vastly outnumbering the enemy
			factor = 0
			scope:combat_side.side_strength > scope:combat_side.enemy_side.vastly_outnumbered_combat_side_threshold
		}
		modifier = {
			factor = 1.05
			culture = {
				has_cultural_parameter = knights_slightly_more_prone_to_injury
			}
		}
		modifier = {
			factor = 1.2
			faith = {
				has_doctrine_parameter = death_is_glory
			}
		}
		modifier = {
			factor = 3
			has_court_position = garuda_court_position
		}
		modifier = {
			factor = 2
			has_trait = brave
		}
		modifier = {
			factor = 0.5
			has_trait = craven
		}
		#House Knight defends family
		## Low decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_low_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_low
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
				}
			}
		}
		## Medium decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_medium_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
		## High decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_high_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
	}

	effect = {
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
					save_temporary_scope_as = enemy_knight
					add_prestige = knight_prestige_gain_on_wound_inverse
					knight_increase_prowess_chance_effect = yes
				}
			}
			scope:combat_side = {
				battle_event = {
					key = "knight_maimed_by_enemy"
					left_portrait = root
					right_portrait = scope:enemy_knight
					type = wound
				}
			}
			maimed_in_battle_effect = yes
			# Scope:enemy_knight gets some glory.
			scope:enemy_knight = {
				if = {
					limit = {
						exists = accolade
						accolade = { is_accolade_active = yes }
					}
					accolade = { add_glory = minimal_glory_gain }
				}
			}
		}
		else = {
			scope:combat_side = {
				battle_event = {
					key = "knight_maimed_no_enemy"
					left_portrait = root
					type = wound
				}
			}
			maimed_in_battle_effect = yes
		}
	}
}

knight_killed = {
	type = knight

	chance = {
		base = 27
		modifier = {
			factor = { # Prowess directly reduces chance of injury/death
				value = 30 # Value at which 'immortality' happens
				subtract = prowess
				if = {
					limit = {
						has_court_position = garuda_court_position
					}
					add = garuda_prowess_svalue
				}
				divide = 30
				min = 0.1 # Everyone dies.
			}
		}
		modifier = { # Reduce chance when outnumbering the enemy
			factor = {
				value = scope:combat_side.enemy_side.side_strength
				divide = scope:combat_side.side_strength
				multiply = 1.4 # Outnumbering 2-1 reduces chance by 30%
				max = 1
			}
			scope:combat_side.side_strength > scope:combat_side.enemy_side.side_strength
		}
		modifier = { # No deaths when vastly outnumbering the enemy
			factor = 0
			scope:combat_side.side_strength > scope:combat_side.enemy_side.vastly_outnumbered_combat_side_threshold
		}
		modifier = {
			factor = 1.05
			culture = {
				has_cultural_parameter = knights_slightly_more_prone_to_injury
			}
		}
		modifier = {
			factor = 1.2
			faith = {
				has_doctrine_parameter = death_is_glory
			}
		}
		modifier = {
			factor = 3
			has_court_position = garuda_court_position
		}
		modifier = {
			factor = 2
			has_trait = brave
		}
		modifier = {
			factor = 0.5
			has_trait = craven
		}
		modifier = {
			factor = 4
			has_trait_rank = {
				trait = wounded
				rank = 3
			}
		}
		# Accolades.
		## Low increase chance.
		modifier = {
			factor = accolade_hostile_knight_death_in_battle_actual_low_value
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_low
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_increase_hostile_knight_death_medium
								has_accolade_parameter = accolade_increase_hostile_knight_death_high
							}
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_increase_hostile_knight_death_medium
								has_accolade_parameter = accolade_increase_hostile_knight_death_high
							}
						}
					}
				}
			}
		}
		## Medium increase chance.
		modifier = {
			factor = accolade_hostile_knight_death_in_battle_actual_medium_value
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_medium
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_high
						}
					}
				}
			}
		}
		## High increase chance.
		modifier = {
			factor = accolade_hostile_knight_death_in_battle_actual_high_value
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_increase_hostile_knight_death_high
						}
					}
				}
			}
		}
		#House Knight defends family
		## Low decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_low_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_low
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
				}
			}
		}
		## Medium decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_medium_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
		## High decrease chance.
		modifier = {
			factor = accolade_defends_family_actual_high_value
			exists = house
			exists = liege.house
			house = liege.house
			scope:combat_side = {
				OR = {
					any_side_knight = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						exists = accolade
						accolade = {
							is_accolade_active = yes
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
	}

	effect = {
		if = {
			limit = {
				has_variable = hold_court_8050_knight
				employer = {
					has_variable = hold_court_8050_promise
					var:hold_court_8050_promise = PREV
				}
			}
			save_scope_as = knight
			location = { save_scope_as = battle_location }
			var:hold_court_8050_knight = { save_scope_as = vassal }
			remove_variable = hold_court_8050_knight
			employer = {
				remove_variable = hold_court_8050_promise
				trigger_event = {
					id = hold_court.8053
					days = 1
				}
			}
		}
		save_died_in_battle_variables_effect = {
			ENEMY = scope:combat_side.enemy_side.side_primary_participant
			LEADER = scope:combat_side.side_primary_participant
			LOCATION = scope:combat_side.combat.location
		}
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
					save_temporary_scope_as = enemy_knight
					add_prestige = knight_prestige_gain_on_kill_inverse
					knight_increase_prowess_chance_effect = yes
					# Scope:enemy_knight gets some glory.
					scope:enemy_knight = {
						if = {
							limit = {
								exists = accolade
								accolade = { is_accolade_active = yes }
							}
							accolade = { add_glory = minor_glory_gain }
						}
					}
				}
			}

			if = {
				limit = {
					root = { is_alive = yes }
					scope:enemy_knight = { is_alive = yes }
				}
				scope:combat_side = {
					battle_event = {
						key = "knight_killed_by_enemy"
						left_portrait = root
						right_portrait = scope:enemy_knight
						type = death
					}
					# For after-battle messaging
					add_to_variable_list = {
						name = slain_side_knights
						target = root
					}
				}
				death = {
					death_reason = death_battle
					killer = scope:enemy_knight
				}
			}
		}
		else = {
			scope:combat_side = {
				battle_event = {
					key = "knight_killed_no_enemy"
					left_portrait = root
					type = death
				}
				# For after-battle messaging
				add_to_variable_list = {
					name = slain_side_knights
					target = root
				}
			}
			death = {
				death_reason = death_battle
			}
		}
		if = {
			limit = {
				is_acclaimed = yes
			}
			accolade = {
				add_glory = minor_glory_gain
			}
		}
	}
}
