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
			is_acclaimed = yes
		}
		modifier = {
			factor = 1.25
			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
			is_acclaimed = yes
		}
		modifier = {
			factor = 1.25
			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 = {
		save_scope_as = knight
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						is_alive = yes
						this.prowess <= scope:knight.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						is_alive = yes
						this.prowess <= scope:knight.prowess_for_combat_event_opponent
					}
					weight = {
						base = 100
						modifier = {
							factor = 0.75
							is_acclaimed = yes
						}
						modifier = {
							factor = 0.75
							has_perk = stalwart_leader_perk
						}
						modifier = {
							factor = 0.75
							dynasty ?= {
								has_dynasty_perk = warfare_legacy_3
							}
						}
					}
					save_temporary_scope_as = enemy_knight
				}
			}
			if = {
				limit = {
					scope:knight = { 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 = {
					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:folkgerman_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
			is_acclaimed = yes
		}
		modifier = {
			factor = 1.25
			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 = {
		save_scope_as = knight
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess <= scope:knight.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess <= scope:knight.prowess_for_combat_event_opponent
					}
					weight = {
						base = 100
						modifier = {
							factor = 0.75
							is_acclaimed = yes
						}
						modifier = {
							factor = 0.75
							has_perk = stalwart_leader_perk
						}
						modifier = {
							factor = 0.75
							dynasty ?= {
								has_dynasty_perk = warfare_legacy_3
							}
						}
					}
					save_temporary_scope_as = enemy_knight
				}
			}
			if = {
				limit = {
					scope:knight = { is_alive = yes }
					scope:enemy_knight = { is_alive = yes }
				}
				scope:combat_side = {
					battle_event = {
						key = "berserker_rage_killed_enemy"
						left_portrait = scope:knight
						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 = scope:knight
					}
				}
			}
		}
		else = {
			if = {
				limit = {
					scope:knight = { is_alive = yes }
				}
				scope:combat_side = {
					battle_event = {
						key = "berserker_rage_no_enemy"
						left_portrait = scope:knight
						type = death
						target_right = yes
					}
				}
				add_trait = berserker
			}
		}
		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
			is_acclaimed = yes
		}
		modifier = {
			factor = 1.25
			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 = {
		save_scope_as = knight
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						is_alive = yes
						this.prowess <= scope:knight.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						is_alive = yes
						this.prowess <= scope:knight.prowess_for_combat_event_opponent
					}
					weight = {
						base = 100
						modifier = {
							factor = 0.75
							is_acclaimed = yes
						}
						modifier = {
							factor = 0.75
							has_perk = stalwart_leader_perk
						}
						modifier = {
							factor = 0.75
							dynasty ?= {
								has_dynasty_perk = warfare_legacy_3
							}
						}
					}
					save_temporary_scope_as = enemy_knight
				}
			}
			if = {
				limit = {
					scope:knight = { 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 = {
					# 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 = 1
	}

	effect = {
		save_scope_as = knight
		# 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 = scope:knight.location
				}
			}
		}
	}
}

knight_wounded = {
	type = knight

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

	chance = {
		base = 100
		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.1
			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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_low
							
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						accolade ?= {
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
					any_side_commander = {
						accolade ?= {
							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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						accolade ?= {
							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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
	}

	effect = {
		save_scope_as = knight
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= scope:knight.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= scope:knight.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 = scope:knight
					right_portrait = scope:enemy_knight
					type = wound
				}
			}
			increase_wounds_effect = { REASON = fight }
			# Scope:enemy_knight gets some glory.
			scope:enemy_knight = {
				if = {
					limit = {
						is_acclaimed = yes
					}
					accolade = { add_glory = minimal_glory_gain }
				}
			}
		}
		else = {
			scope:combat_side = {
				battle_event = {
					key = "knight_wounded_no_enemy"
					left_portrait = scope:knight
					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 = 40
		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.1
			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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_low
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						accolade ?= {
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
					any_side_commander = {
						accolade ?= {
							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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						accolade ?= {
							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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
	}

	effect = {
		save_scope_as = knight
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= scope:knight.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= scope:knight.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 = scope:knight
					right_portrait = scope:enemy_knight
					type = wound
				}
			}
			maimed_in_battle_effect = yes
			# Scope:enemy_knight gets some glory.
			scope:enemy_knight = {
				accolade ?= { add_glory = minimal_glory_gain }
			}
		}
		else = {
			scope:combat_side = {
				battle_event = {
					key = "knight_maimed_no_enemy"
					left_portrait = scope:knight
					type = wound
				}
			}
			maimed_in_battle_effect = yes
		}
	}
}

knight_killed = {
	type = knight

	chance = {
		base = 30
		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.1
			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.enemy_side = {
				OR = {
					any_side_knight = {
						accolade ?= {
							has_accolade_parameter = accolade_increase_hostile_knight_death_low
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_increase_hostile_knight_death_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						accolade ?= {
							OR = {
								has_accolade_parameter = accolade_increase_hostile_knight_death_medium
								has_accolade_parameter = accolade_increase_hostile_knight_death_high
							}
						}
					}
					any_side_commander = {
						accolade ?= {
							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.enemy_side = {
				OR = {
					any_side_knight = {
						accolade ?= {
							has_accolade_parameter = accolade_increase_hostile_knight_death_medium
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_increase_hostile_knight_death_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						accolade ?= {
							has_accolade_parameter = accolade_increase_hostile_knight_death_high
						}
					}
					any_side_commander = {
						accolade ?= {
							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.enemy_side = {
				OR = {
					any_side_knight = {
						accolade ?= {
							has_accolade_parameter = accolade_increase_hostile_knight_death_high
						}
					}
					any_side_commander = {
						accolade ?= {
							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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_low
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_low
						}
					}
				}
				# Not cumulative with subsequent ranks.
				NOR = {
					any_side_knight = {
						accolade ?= {
							OR = {
								has_accolade_parameter = accolade_defends_family_medium
								has_accolade_parameter = accolade_defends_family_high
							}
						}
					}
					any_side_commander = {
						accolade ?= {
							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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_medium
						}
					}
				}
				# Not cumulative with the subsequent rank.
				NOR = {
					any_side_knight = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						accolade ?= {
							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 = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
					any_side_commander = {
						accolade ?= {
							has_accolade_parameter = accolade_defends_family_high
						}
					}
				}
			}
		}
	}

	effect = {
		save_scope_as = knight
		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 >= scope:knight.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= scope:knight.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 = {
								is_acclaimed = yes
							}
							accolade = { add_glory = minor_glory_gain }
						}
					}
				}
			}

			if = {
				limit = {
					scope:knight = { is_alive = yes }
					scope:enemy_knight = { is_alive = yes }
				}
				scope:combat_side = {
					battle_event = {
						key = "knight_killed_by_enemy"
						left_portrait = scope:knight
						right_portrait = scope:enemy_knight
						type = death
					}
					# For after-battle messaging
					add_to_variable_list = {
						name = slain_side_knights
						target = scope:knight
					}
				}
				if = {
					limit ={
						exists = house
						exists = scope:enemy_knight.house
					}
					house = {
						change_house_relation_effect = {
							HOUSE = scope:enemy_knight.house
							VALUE = house_relation_damage_medium_value
							REASON = killed
							CHAR = scope:enemy_knight
							TARGET_CHAR = scope:knight
							TITLE = scope:dummy_gender
						}
					}
				}
				if = {
					limit = {
						scope:enemy_knight = {
							faith = {
								has_doctrine = tenet_cranial_trophies
							}
						}
					}
					tgp_cranial_trophies_beheading_effect = {
						KILLER = scope:enemy_knight
						DEAD = scope:knight
					}
				}
				death = {
					death_reason = death_battle
					killer = scope:enemy_knight
				}
			}
		}
		else = {
			scope:combat_side = {
				battle_event = {
					key = "knight_killed_no_enemy"
					left_portrait = scope:knight
					type = death
				}
				# For after-battle messaging
				add_to_variable_list = {
					name = slain_side_knights
					target = scope:knight
				}
			}
			death = {
				death_reason = death_battle
			}
		}
		if = {
			limit = {
				is_acclaimed = yes
			}
			accolade = {
				add_glory = minor_glory_gain
			}
		}
		#Mongol severed head content
		if = {
			limit = {
				exists = liege
				is_knight_of = liege
				OR = {
					is_acclaimed = yes
					is_close_family_of = liege
					is_consort_of = liege
					is_powerful_vassal_of = liege
					has_relation_blood_brother = liege
				}
				liege = {
					NOR = {
						has_variable = beheaded_warrior
						has_character_flag = beheaded_warrior_cooldown
					}
					OR = {
						faith.religion = faith:tengri_pagan.religion
						AND = {
							culture = {
								OR = {
									has_cultural_pillar = heritage_mongolic
									has_cultural_pillar = heritage_turkic
								}
							}
							has_trait = nomadic_philosophy
						}
					}
				}
				scope:combat_side = {
					any_side_participant = {
						this = root.liege
					}
				}
			}
			liege = {
				set_variable = {
					name = beheaded_warrior
					value = root
				}
				set_variable = {
					name = beheaded_warrior_foe
					value = scope:combat_side.enemy_side.side_primary_participant
				}
				set_variable = {
					name = beheaded_warrior_location
					value = scope:combat_side.combat.location
				}
				if = {
					limit = {
						root = { is_powerful_vassal_of = liege }
					}
					set_variable = {
						name = beheaded_warrior_powerful_vassal
						value = root
					}
				}
				else_if = {
					limit = {
						root = { is_councillor_of = liege }
					}
					set_variable = {
						name = beheaded_warrior_councillor
						value = root
					}
				}
				if = {
					limit = {
						root = {
							is_acclaimed = yes
						}
					}
					set_variable = {
						name = beheaded_warrior_accolade
						value = root.accolade
					}
				}
			}
		}
	}
}

knight_qualify_for_accolade = {
	type = knight

	is_valid = {
		knight_army = {
			any_army_maa_regiment = { count > 0 }
		}
		can_be_acclaimed = yes
		prowess >= accolade_prowess_requirement_value
		# accolade attributes are below max cap
		# possibly: no other MAA accolade attributes? TODO LB
	}

	chance = {
		base = 5
		modifier = {
			factor = liege.accolade_progress
		}
		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 = 2.5
			is_acclaimed = yes
		}
		modifier = {
			factor = 1.25
			dynasty ?= {
				has_dynasty_perk = warfare_legacy_3
			}
		}
		modifier = {
			factor = 1.5
			has_trait = brave
		}
		modifier = {
			factor = 1.5
			has_trait = ambitious
		}
		modifier = {
			factor = 0.75
			has_trait = content
		}
		modifier = {
			factor = 0.75
			has_trait = lazy
		}
		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
			has_trait = maimed
		}
	}

	effect = {
		save_scope_as = acclaimed_knight
		liege = { # reset liege accolade progress
			set_variable = {
				name = accolade_progress
				value = 0
			}
		}
		random_list = {
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = skirmishers
						}
					}
					skirmisher_attribute_trigger = no # don't bother unlocking if already eligible
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:skirmishers
				}
				set_variable = {
					name = skirmisher_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = skirmisher_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = archers
							NOR = {
								is_maa_type = crossbowmen
								is_maa_type = shenbigong
								is_maa_type = accolade_maa_crossbowers
							}
						}
					}
					archer_attribute_trigger = no		
				}
				modifier = {
					factor = { # archers who AREN'T crossbows
						value = knight_army.army_number_maa_regiments_of_base_type:archers
						subtract = knight_army.army_number_maa_regiments_of_type:crossbowmen
						subtract = knight_army.army_number_maa_regiments_of_type:shenbigong
						subtract = knight_army.army_number_maa_regiments_of_type:accolade_maa_crossbowers
					}
				}
				set_variable = {
					name = archer_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = archer_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							OR = {
								is_maa_type = crossbowmen
								is_maa_type = shenbigong
								is_maa_type = accolade_maa_crossbowers
							}
						}
					}
					crossbowmen_attribute_trigger = no	
				}
				modifier = {
					factor = {
						value = knight_army.army_number_maa_regiments_of_type:crossbowmen
						add = knight_army.army_number_maa_regiments_of_type:shenbigong
						add = knight_army.army_number_maa_regiments_of_type:accolade_maa_crossbowers
					}
				}
				set_variable = {
					name = crossbowmen_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = crossbowmen_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = pikemen
						}
					}
					pike_attribute_trigger = no		
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:pikemen
				}
				set_variable = {
					name = pike_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = pike_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = heavy_infantry
						}
					}
					vanguard_attribute_trigger = no	
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:heavy_infantry
				}
				set_variable = {
					name = vanguard_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = vanguard_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = light_cavalry
						}
					}
					outrider_attribute_trigger = no
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:light_cavalry
				}
				set_variable = {
					name = outrider_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = outrider_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = heavy_cavalry
						}
					}
					lancer_attribute_trigger = no	
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:heavy_cavalry
				}
				set_variable = {
					name = lancer_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = lancer_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = camel_cavalry
						}
					}
					camelry_attribute_trigger = no
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:camel_cavalry
				}
				set_variable = {
					name = camelry_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = camelry_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = elephant_cavalry
						}
					}
					elephantry_attribute_trigger = no
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:elephant_cavalry
				}
				set_variable = {
					name = elephantry_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = elephantry_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = archer_cavalry
						}
					}
					horse_archer_attribute_trigger = no
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:archer_cavalry
				}
				set_variable = {
					name = horse_archer_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = horse_archer_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					knight_army = {
						any_army_maa_regiment = {
							is_unit_type = gunpowder
						}
					}
					gunpowder_attribute_trigger = no	
				}
				modifier = {
					factor = knight_army.army_number_maa_regiments_of_base_type:gunpowder
				}
				set_variable = {
					name = gunpowder_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = gunpowder_attribute.battle_message_tt
						
					}
				}
			}
			10 = {
				trigger = {
					scope:combat_side.enemy_side = {
						any_side_participant = {
							faith = {
								faith_hostility_level = {
									target = root.faith
									value >= faith_evil_level
								}
							}
						}
					}
					fanatic_attribute_trigger = no
				}
				modifier = { # otherwise it would get diluted by maa unlocks in large armies
					factor = {
						value = knight_army.army_number_maa_regiments
						divide = 2
					}
				}
				set_variable = {
					name = fanatic_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = fanatic_attribute.battle_message_tt
						
					}
				}
			}
			50 = {
				trigger = {
					scope:combat_side = { # currently outnumbered 3 to 2
						side_army_size >= {
							value = enemy_side.side_army_size
							multiply = 0.66
						}
					}
					valiant_attribute_trigger = no
				}
				modifier = { # otherwise it would get diluted by maa unlocks in large armies
					factor = {
						value = knight_army.army_number_maa_regiments
						divide = 2
					}
				}
				set_variable = {
					name = valiant_attribute_unlock
					value = yes
				}
				liege = {
					send_interface_message = {
						type = msg_accolade_eligibility
						title = maa_accolade_unlock.t
						left_icon = scope:acclaimed_knight
						custom_tooltip = valiant_attribute.battle_message_tt
						
					}
				}
			}
		}
	}
}
