# Events fired upon resolution of a combat.
#
# Event List:
#	0001-0005: 'The Undefeated' - a commander has won 100 battles without losing a single one! By Sean Hughes
#	1001-1003: 'Prisoners of War' - one or more characters were imprisoned at the end of a battle (notifications sent to concerned parties). By Sean Hughes
#
#

namespace = combat_event

#####################
# The Undefeatable 	#
# by Sean Hughes 	#
# 0001-0005			#
#####################

#Event for Winning Side: Keeps track of total number of victories the side's commander has (if they have never lost a battle). At 100 victories, they earn the nickname 'the Undefeatable'.
combat_event.0001 = {
	hidden = yes
	scope = combat_side

	trigger = {
		side_commander ?= { is_alive = yes }
		side_commander = { is_ai = no } # Too expensive performance-wise to track for all commanders in the game. Substantial savings on cycles to only track for players.
		OR = {
			side_commander = {
				# Event will only occur if this character has never lost a battle.
				OR = {
					NOT = { has_variable = combat_event_0001_tracker }
					trigger_if = {
						limit = {
							has_variable = combat_event_0001_tracker
						}
						var:combat_event_0001_tracker > 0
					}
				}
			}
			AND = {
				# However, as a special case we will fire this event if we defeat 'the Undefeatable'...
				exists = enemy_side.side_commander
				enemy_side.side_commander = {
					has_character_flag = combat_event_undefeatable_flag
				}
				# ...But only if we haven't already defeated 'the Undefeatable' before!
				side_commander = {
					NOT = { has_character_flag = combat_event_triumphant_flag }
				}
			}
		}
	}

	immediate = {
		if = {
			#If we have defeated 'the Undefeatable', execute a special use case (skipping the rest of this event's effects).
			limit = {
				exists = enemy_side.side_commander
				enemy_side.side_commander = {
					has_character_flag = combat_event_undefeatable_flag
				}
			}

			# Save the enemy commander's scope for localization.
			enemy_side.side_commander = {
				save_scope_as = enemy_commander
			}

			# Gain special recognition for defeating 'the Undefeatable.'
			side_commander = {
				# Save this scope for localization.
				save_scope_as = friendly_commander

				# So we don't get this outcome more than once per character.
				add_character_flag = combat_event_triumphant_flag

				# Trigger the event.
				trigger_event = {
					id = combat_event.0004
					days = 1
				}
			}
		}
		else = {
			# In almost all situations, add to our victory tracker. Once we reach 100, fire the special Undefeatable event.
			side_commander = {
				if = {
					limit = {
						is_alive = yes # Can be killed in combat
					}
					# If the victory tracking variable does not yet exist, create it.
					if = {
						limit = {
							NOT = {
								has_variable = combat_event_0001_tracker
							}
						}
						set_variable = {
							name = combat_event_0001_tracker
							value = 1
						}
					}
					# Otherwise, increment it.
					else = {
						change_variable = {
							name = combat_event_0001_tracker
							add = 1
						}
					}

					#If the commander has accumulateed 100 victories, fire the event which gives them the nickname 'the Undefeatable'.
					if = {
						limit = {
							var:combat_event_0001_tracker = 100
						}

						# If there is an enemy commander, save their scope for portraits/localization in the following event.
						if = {
							limit = {
								exists = root.enemy_side.side_commander
							}
							root.enemy_side.side_commander = {
								save_scope_as = enemy_commander
							}
						}

						# Fire the event which gives the victorious commander the nickname 'the Undefeatable'.
						trigger_event = {
							id = combat_event.0003
							days = 1
						}
					}	
				}
			}
		}
	}
}

# Event for Losing Side: Disqualifies the side's commander from ever earning the nickname 'the Undefeatable'.
combat_event.0002 = {
	hidden = yes
	scope = combat_side

	trigger = {
		side_commander ?= {
			is_alive = yes
		}
	}

	immediate = {
		side_commander = {
			#If we ever lose a battle, we are disqualified from achieving 'the Undefeatable' nickname.
			set_variable = {
				name = combat_event_0001_tracker
				value = 0
			}
		}
	}
}

#Once a commander wins 100 battles with 0 defeats, they get a special 'the Undefeatable' nickname.
combat_event.0003 = {
	type = character_event
	title = combat_event.0003.t
	desc = {
		desc = combat_event.0003.desc.opening
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:enemy_commander
				}
				desc = combat_event.0003.desc.commander
			}
			desc = combat_event.0003.desc.no_commander
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					NOR = {
						has_trait = shy
						has_trait = humble
						has_trait = craven
						has_trait = content
						has_trait = paranoid
						has_trait = cautious_leader
					}
				}
				desc = combat_event.0003.desc.alt_ending
			}
			desc = combat_event.0003.desc.ending
		}
	}
	theme = battle
	left_portrait = {
		character = root
		animation = war_over_win
	}
	right_portrait = {
		character = scope:enemy_commander
		animation = war_over_loss
	}

	immediate = {
		add_character_flag = combat_event_undefeatable_flag
		set_signature_weapon_effect = yes
	}

	#Option A: Accept the title "the Undefeatable", gain prestige.
	option = {
		name = combat_event.0003.a
		give_nickname = nick_the_undefeated # We don't use 'set_nick_effect' here because this is important enough we want it to override event 'good' nicknames. Plus it's an opt-in.
		add_prestige = massive_prestige_gain

		add_martial_skill = 3	

		#AI will always pick this unless Humble.
		stress_impact = {
			humble = medium_stress_gain
		}
		ai_chance = {
			base = 100
			
			modifier = {
				has_trait = humble
				add = -100
			}
		}
	}

	#Option B: Reject the title "the Undefeatable", gain piety.
	option = {
		name = {
			trigger = { has_trait = zealous }
			text = combat_event.0003.b.zealous
		}
		name = {
			trigger = { NOT = { has_trait = zealous }}
			text = combat_event.0003.b
		}

		if = {
			limit = {
				has_trait = zealous
			}
			add_piety = massive_piety_gain
		}
		else = {
			add_prestige = medium_prestige_gain
			add_piety = medium_piety_gain
		}

		add_martial_skill = 3

		#AI will only pick this if Humble.
		stress_impact = {
			arrogant = medium_stress_gain
		}
		ai_chance = {
			base = 0
			
			modifier = {
				has_trait = humble
				add = 100
			}
		}
	}
}

#If 'the Undefeatable' is ever defeated, the commander who defeated them gets a special title.
combat_event.0004 = {
	type = character_event
	title = combat_event.0004.t
	desc = combat_event.0004.desc
	theme = battle
	left_portrait = {
		character = root
		animation = war_over_win
	}
	right_portrait = {
		character = scope:enemy_commander
		animation = war_over_loss
	}

	immediate = {
		set_signature_weapon_effect = yes
	}

	#Option A: Accept the title "the Triumphant", gain prestige.
	option = {
		name = combat_event.0004.a
		give_nickname = nick_the_triumphant
		add_prestige = massive_prestige_gain

		add_martial_skill = 3

		#AI will always pick this unless Humble
		stress_impact = {
			humble = medium_stress_gain
		}
		ai_chance = {
			base = 100
			
			modifier = {
				has_trait = humble
				add = -100
			}
		}
	}

	#Option B: Reject the title "the Triumphant", gain piety.
	option = {
		name = {
			trigger = { has_trait = zealous }
			text = combat_event.0004.b.zealous
		}
		name = {
			trigger = { NOT = { has_trait = zealous }}
			text = combat_event.0004.b
		}

		if = {
			limit = {
				has_trait = zealous
			}
			add_piety = massive_piety_gain
		}
		else = {
			add_prestige = medium_prestige_gain
			add_piety = medium_piety_gain
		}

		add_martial_skill = 3

		#AI will never pick this unless Humble
		stress_impact = {
			arrogant = medium_stress_gain
		}
		ai_chance = {
			base = 0
			
			modifier = {
				has_trait = humble
				add = 100
			}
		}
	}

	after = {
		#Inform 'the Undefeatable' that they have, in fact, been defeated.
		scope:enemy_commander = {
			remove_character_flag = combat_event_undefeatable_flag
			trigger_event = {
				id = combat_event.0005
				days = 1
			}
			show_as_tooltip = {
				add_prestige_level = -1
			}
		}
	}
}

#'The Undefeatable' is informed that they have, in fact, been defeated.
combat_event.0005 = {
	type = character_event
	title = combat_event.0005.t
	desc = combat_event.0005.desc
	theme = battle
	left_portrait = {
		character = scope:friendly_commander
		animation = war_over_win
	}

	immediate = {
		if = {
			limit = {
				scope:friendly_commander = {
					has_nickname = nick_the_triumphant
				}
			}
			show_as_tooltip = {
				scope:friendly_commander = {
					give_nickname = nick_the_triumphant
				}
			}
		}
	}

	#Option A: Accept that they have defeated you.
	option = {
		name = combat_event.0005.a

		add_prestige_level = -1

		stress_impact = {
			vengeful = medium_stress_gain
			arrogant = medium_stress_gain
			wrathful = minor_stress_gain
		}
		ai_chance = {
			base = 50
			
			ai_value_modifier = {
				ai_vengefulness = -5
			}
		}
	}

	#Option B: Declare them your rival!
	option = {
		name = combat_event.0005.b

		add_prestige_level = -1

		if = {
			limit = {
				NOT = { has_relation_rival = scope:friendly_commander }
			}
			scope:friendly_commander = {
				send_interface_toast = {
					title = combat_event.0005.message
					set_relation_rival = {
						target = root
						reason = rival_undefeated_defeated
					}
				}
			}
		}

		stress_impact = {
			forgiving = medium_stress_gain
			humble = medium_stress_gain
			calm = minor_stress_gain
		}
		ai_chance = {
			base = 5000
			
			ai_value_modifier = {
				ai_vengefulness = 5
			}
		}
	}
}

#####################
# Battle Aftermath 	#
# by Sean Hughes 	#
# 1001-1014			#
#####################

# Used for determining both who should be notifiied about captured/slain combatants.
scripted_trigger combat_event_1001_character_is_relevant_to_target_trigger = {
	OR = {
		# Character is of major or minor interest.
		is_of_major_interest_trigger = {CHARACTER = $TARGET$ }
		is_of_minor_interest_trigger = {CHARACTER = $TARGET$ }

		# Courtiers normally aren't very interesting, but if one got captured in battle there has to be a reason why they were there in the first place!
		is_courtier_of = $TARGET$
		is_knight_of = $TARGET$
	}
}

# Used for checking if a captured combatant is worth war-score.
scripted_trigger combat_event_1001_character_is_important_combatant = {
	OR = {
		this = scope:primary_combat_winner
		this = scope:primary_combat_loser
		is_heir_of = scope:primary_combat_winner
		is_heir_of = scope:primary_combat_loser
	}
}

# Used to transfer equipped battle gear to slayer/capturer
scripted_effect combat_transfer_equipped_artifact_effect = {
	if = {
		limit = {
			any_equipped_character_artifact = { artifact_slot_type = $TYPE$ }
		}
		save_scope_as = artifact_knight
		random_equipped_character_artifact = {
			limit = { artifact_slot_type = $TYPE$ }
			if = {
				limit = { artifact_durability >= define:NInventory|ARTIFACT_LOW_DURABILITY }
				save_scope_as = knight_artifact
				if = {
					limit = { exists = artifact_owner.house }
					artifact_owner.house = { save_scope_as = artifact_house }
				}
				set_owner = {
					target = $OWNER$
					history = {
						type = taken_in_battle
						actor = scope:artifact_knight
						recipient = $OWNER$
						location = scope:artifact_knight.location
					}
				}
				if = {
					limit = {
						exists = scope:artifact_house
						NOT = {
							any_artifact_house_claimant = {
								this = scope:artifact_house
							}
						}
					}
					scope:artifact_house = { add_house_artifact_claim = prev }
				}
				scope:artifact_knight = {
					if = {
						limit = {
							NOT = {
								has_personal_artifact_claim = prev
							}
						}
						add_personal_artifact_claim = prev
					}
				}
				#debug_log = "ART_TRAN"
			}
			else = {
				destroy_artifact = this
				#debug_log = "ART_DEST"
			}
		}
	}
}

# Used to check if a knight has fought in a battle recently.
scripted_effect petition_knight_battle_history_effect = {
	if = {
		limit = {
			is_alive = yes
			age >= 35
			is_landed = no
			employer.highest_held_title_tier >= tier_kingdom # No need to set variables for knights of dukes/counts
		}
		if = {
			limit = { has_variable = hc_8170_fought_battle_for }
			remove_variable = hc_8170_fought_battle_for
		}
		if = {
			limit = { has_variable = hc_8170_fought_battle_where }
			remove_variable = hc_8170_fought_battle_where
		}
		if = {
			limit = { has_variable = hc_8170_fought_battle_against }
			remove_variable = hc_8170_fought_battle_against
		}
		set_variable = {
			name = hc_8170_fought_battle_for
			value = employer
			years = 15
		}
		set_variable = {
			name = hc_8170_fought_battle_where
			value = location
			years = 15
		}
		save_scope_as = knight_scope
		if = { # For winner side knights
			limit = {
				scope:winning_side = {
					any_side_knight = { THIS = scope:knight_scope } # this knight
				}
			}
			set_variable = {
				name = hc_8170_fought_battle_against
				value = scope:winning_side.enemy_side.side_primary_participant
				years = 15
			}
		}
		else = { # For loser side knights
			set_variable = {
				name = hc_8170_fought_battle_against
				value = scope:winning_side.side_primary_participant
				years = 15
			}
		}
	}
}

# Combat Side event which handles the combatant imprisonment and death logic.
# Also determines which characters need to be notified about these occurances, and triggers notification events for them (combat_event.1002-1004 and 1012-1014).
combat_event.1001 = {
	scope = combat_side
	hidden = yes

	trigger = {
		# Prisoners of war can only be taken during an actual war.
		# Non-war battles (such as from engaging Hostile forces) do not take prisoners, since PoWs cannot be released on war end if there is no war!
		side_primary_participant = {
			is_at_war_with = root.enemy_side.side_primary_participant
		}

		#Cannot happen in the tutorial
		NOT = { has_global_variable = lesson_chain_war_tutorial_started }
	}

	immediate = {
		# Save necessary scopes for later usage.
		save_scope_as = winning_side
		side_primary_participant = {
			random_character_war = {
				limit = {
					OR = {
						is_attacker = root.enemy_side.side_primary_participant
						is_defender = root.enemy_side.side_primary_participant
					}
				}
				save_scope_as = combat_war
				if = {
					limit = {
						is_attacker = root.side_primary_participant
					}
					primary_attacker = {
						save_scope_as = primary_combat_winner
					}
					primary_defender = {
						save_scope_as = primary_combat_loser
					}
				}
				else = {
					primary_defender = {
						save_scope_as = primary_combat_winner
					}
					primary_attacker = {
						save_scope_as = primary_combat_loser
					}
				}
			}
			save_scope_as = combat_winner
		}
		enemy_side.side_primary_participant = {
			save_scope_as = combat_loser
		}
		combat.location = {
			save_scope_as = combat_location
		}

		# If there is a Commander on the losing side, check if they should be imprisoned.
		if = {
			limit = {
				exists = enemy_side.side_commander
			}

			enemy_side.side_commander = {
				random_list = {
					# No Effect
					90 = {
						# Reduced risk for characters with the Stalwart Leader perk.
						modifier = {
							add = 100
							has_perk = stalwart_leader_perk
							is_ai = no
						}
						modifier = {
							add = 30
							has_perk = stalwart_leader_perk
							is_ai = yes
						}
						#Acclaimed knights being captured has weird results, we are giving them essentially a pass
						modifier = {
							add = 500
							is_acclaimed = yes
						}

						#Reduced risk for character with Bodyguard(s) that are fighting by their side
						modifier = {
							add = 50
							employs_court_position = bodyguard_court_position
							any_court_position_holder = {
								type = bodyguard_court_position
								save_temporary_scope_as = bodyguard
								root.enemy_side = {
									any_side_knight = { this = scope:bodyguard }
								}
							}
						}

						#Reduced risk for character with Garuda that are fighting by their side
						modifier = {
							add = 50
							employs_court_position = garuda_court_position
							any_court_position_holder = {
								type = garuda_court_position
								save_temporary_scope_as = garuda
								root.enemy_side = {
									any_side_knight = { this = scope:garuda }
								}
							}
						}

						# Being stackwiped greatly increases the chance of capture.
						modifier = {
							scope:wipe = yes
							factor = 0.20
						}
					}
					# Captured in battle.
					10 = {
						trigger = {
							is_alive = yes
						}
						modifier = {
							factor = { # Prowess directly reduces chance of capture.
								value = 30 # Value at which 'immunity' happens.
								subtract = prowess
								divide = 30
								min = 0.1 # Everyone makes mistakes.
							}
						}

						# Craven characters are more likey to flee earlier and elude capture.
						modifier = {
							factor = 0.5
							has_trait = craven
						}
						modifier = {
							factor = 2
							has_trait = brave
						}

						# Certain injuries increase the chances of being caught.
						modifier = {
							factor = 3
							OR = {
								has_trait = one_legged
								has_trait = maimed
							}
						}

						# State Ransoms: characters are more likely to surrender if their liege will pay their ransom.
						modifier = {
							factor = 2
							liege.culture = { has_cultural_parameter = knights_die_less_but_are_captured_more }
							NOT = { top_liege = this }
						}
						
						add_to_list = prisoners_of_war

						# Log a battle event so that the imprisonment shows up in the battle summary screen.
						save_temporary_scope_as = this_combatant
						root = {
							battle_event = {
								key = "combatant_captured_in_battle"
								left_portrait = scope:combat_winner
								right_portrait = scope:this_combatant
								type = imprison
							}
						}
					}
					# Commanders cannot be slain in the retreat phase, since we don't want to punish players for retreating from combat after they get wounded.
				}
			}
		}

		# Set history variables for petition events
		every_side_knight = { petition_knight_battle_history_effect = yes }

		# For each Knight on the losing side, check if they should be imprisoned/slain.
		enemy_side = {
			every_side_knight = {
				random_list = {
					85 = { # Nothing Happens
						trigger = {
							is_alive = yes
						}
						modifier = {
							add = 50
							has_perk = stalwart_leader_perk
							is_ai = no
						}
						modifier = {
							add = 15
							has_perk = stalwart_leader_perk
							is_ai = yes
						}

						#Reduced risk for character with bodyguards that are fighting by their side
						modifier = {
							add = 25
							employs_court_position = bodyguard_court_position
							any_court_position_holder = {
								type = bodyguard_court_position
								save_temporary_scope_as = bodyguard
								root.enemy_side = {
									any_side_knight = { this = scope:bodyguard }
								}
							}
						}

						#Reduced risk for character with Garuda that are fighting by their side
						modifier = {
							add = 25
							employs_court_position = garuda_court_position
							any_court_position_holder = {
								type = garuda_court_position
								save_temporary_scope_as = garuda
								root.enemy_side = {
									any_side_knight = { this = scope:garuda }
								}
							}
						}

						# Being stackwiped greatly increases the chance of capture and/or death.
						modifier = {
							scope:wipe = yes
							factor = 0.20
						}
					}
					10 = { # Imprisonment
						trigger = {
							is_alive = yes
						}

						modifier = {
							factor = { # Prowess directly reduces chance of capture
								value = 30 # Value at which 'immunity' happens
								subtract = prowess
								divide = 30
								min = 0.1 # Everyone makes mistakes.
							}
						}
						modifier = {
							factor = 2
							has_trait = brave
						}
						modifier = {
							factor = 0.5
							has_trait = craven
						}
						modifier = {
							factor = 2
							OR = {
								has_trait = one_legged
								has_trait = disfigured
								has_trait = one_eyed
								has_trait = maimed
							}
						}
						# State Ransoms: characters are more likely to surrender if their liege will pay their ransom.
						modifier = {
							factor = 2
							liege.culture = { has_cultural_parameter = knights_die_less_but_are_captured_more }
						}
						# House Blocs
						modifier = {
							factor = 1.25
							scope:combat_winner.confederation ?= { has_cohesion_level_parameter = any_member_extra_capture_chance }
						}
						#Acclaimed knights being captured has weird results, we are giving them essentially a pass
						modifier = {
							factor = 0.1
							is_acclaimed = yes
						}

						# Transfer equipped armor and weapon to winner if durability not too low
						combat_transfer_equipped_artifact_effect = { TYPE = primary_armament OWNER = scope:combat_winner }
						combat_transfer_equipped_artifact_effect = { TYPE = armor OWNER = scope:combat_winner }

						add_to_list = prisoners_of_war
						
						# Log a battle event so that the imprisonment shows up in the battle summary screen.
						save_temporary_scope_as = this_combatant
						root = {
							battle_event = {
								key = "combatant_captured_in_battle"
								left_portrait = scope:combat_winner
								right_portrait = scope:this_combatant
								type = imprison
								target_right = yes
							}
						}
					}
					5 = { # Death
						trigger = {
							is_alive = yes # Don't kill knights who are already dead! They will be accounted for later.
						}

						# Modifiers influencing chance to occur.
						modifier = {
							factor = { # Prowess directly reduces chance of capture
								value = 40 # Value at which 'immunity' happens
								subtract = prowess
								divide = 40
								min = 0.1 # Everyone makes mistakes.
							}
						}

						# Craven characters are more likely to flee before they can be slain.
						modifier = {
							factor = 0.5
							has_trait = craven
						}
						modifier = {
							factor = 2
							has_trait = brave
						}
						
						# Increased chance of dying the more wounded a character is.
						modifier = {
							factor = 2
							OR = {
								has_trait = one_legged
								has_trait = disfigured
								has_trait = one_eyed
								has_trait = maimed
							}
						}
						modifier = {
							factor = 1.5
							has_trait_rank = {
								trait = wounded
								rank = 1
							}
						}
						modifier = {
							factor = 2
							has_trait_rank = {
								trait = wounded
								rank = 2
							}
						}
						modifier = {
							factor = 3
							has_trait_rank = {
								trait = wounded
								rank = 3
							}
						}

						save_died_in_battle_variables_effect = {
							ENEMY = scope:combat_winner
							LEADER = scope:combat_loser
							LOCATION = scope:combat_location
						}

						# State Ransoms: characters are more likely to surrender if their liege will pay their ransom, which makes them less likely to die.
						modifier = {
							factor = 0.5
							liege.culture = { has_cultural_parameter = knights_die_less_but_are_captured_more }
						}
						modifier = {
							factor = 0.5
							is_acclaimed = yes
						}

						# For messaging purposes.
						add_to_list = slain_combatants

						# Log a battle event so that the imprisonment shows up in the battle summary screen.
						save_temporary_scope_as = this_combatant

						# Get a random valid knight to be the killer.
						scope:winning_side = {
							random_side_knight = {
								limit = {
									this.prowess >= scope:this_combatant.prowess_for_combat_event_opponent
								}

								weight = {
									base = 1

									compare_modifier = {
										value = this.prowess
									}

									modifier = {
										factor = 10
										this.prowess > scope:this_combatant.prowess
									}
								}

								save_scope_as = enemy_knight
							}
						}

						# Log the battle event and then kill the knight
						if = {
							limit = {
								exists = scope:enemy_knight
							}
							# Transfer equipped armor and weapon to killer if durability not too low
							combat_transfer_equipped_artifact_effect = { TYPE = primary_armament OWNER = scope:enemy_knight }
							combat_transfer_equipped_artifact_effect = { TYPE = armor OWNER = scope:enemy_knight }
							root = {
								battle_event = {
									key = "combatant_killed_in_battle"
									left_portrait = scope:enemy_knight
									right_portrait = scope:this_combatant
									type = death
									target_right = yes
								}
							}

							death = {
								death_reason = death_battle
								killer = scope:enemy_knight
							}
						}
						else = {
							root = {
								battle_event = {
									key = "combatant_killed_in_battle_no_killer"
									left_portrait = scope:combat_winner
									right_portrait = scope:this_combatant
									type = death
									target_right = yes
								}
							}

							death = {
								death_reason = death_battle
							}
						}
						clear_saved_scope = enemy_knight
					}
					0 = {
						# If the knight is already dead, don't do anything (yet).
					}
				}
				petition_knight_battle_history_effect = yes # Set history variables for petition events
			}
			# Add the enemy knights which were killed before the end of the battle to the casualties list.
			every_in_list = {
				variable = slain_side_knights
				add_to_list = slain_combatants
			}
			clear_variable_list = slain_side_knights
		}
		
		# Also record who already died on the winning side, so the victor knows their own casualty count.
		scope:winning_side = {
			every_in_list = {
				variable = slain_side_knights
				add_to_list = victorious_slain_combatants
			}
			clear_variable_list = slain_side_knights
		}


		# Handle effects & messaging for any slain combatants.
		if = {
			limit = {
				OR = {			
					any_in_list = {
						list = slain_combatants
						exists = this
					}
					any_in_list = {
						list = victorious_slain_combatants
						exists = this
					}
				}
			}
			# If anyone was slain after the battle, send a message to both the winner and loser of the combat to inform them of this.
			scope:combat_winner = {
				trigger_event = combat_event.1012
			}
			scope:combat_loser = {
				trigger_event = combat_event.1013
			}

			# Send a message to all other players who may be concerned about the fate of the slain combatants.
			every_player = {
				limit = {
					NOR = {
						this = scope:combat_winner
						this = scope:combat_loser
					}

					save_temporary_scope_as = notification_target

					OR = {				
						any_in_list = {
							list = slain_combatants
							combat_event_1001_character_is_relevant_to_target_trigger = { TARGET = scope:notification_target }
						}
						any_in_list = {
							list = victorious_slain_combatants
							combat_event_1001_character_is_relevant_to_target_trigger = { TARGET = scope:notification_target }
						}
					}
				}
				trigger_event = combat_event.1014
			}
		}

		# Handle effects & messaging for any captured combatants (must be done after slain combatants for order-of-operations reasons!).
		if = {
			limit = {
				any_in_list = {
					list = prisoners_of_war
					exists = this
				}
			}
			# If anyone was captured after the battle, send a message to both the winner and loser of the combat to inform them of this.
			scope:combat_winner = {
				trigger_event = combat_event.1002
			}
			scope:combat_loser = {
				trigger_event = combat_event.1003
			}
			# Send a message to all other players who may be concerned about the fate of the captured prisoners.
			every_player = {
				limit = {
					NOR = {
						this = scope:combat_winner
						this = scope:combat_loser
					}

					save_temporary_scope_as = notification_target
					any_in_list = {
						list = prisoners_of_war

						combat_event_1001_character_is_relevant_to_target_trigger = { TARGET = scope:notification_target }
					}
				}
				trigger_event = combat_event.1004
			}
		}
		every_side_participant = {
			random = { # chance of inspiring a knight to become eligible for thug accolade
				chance = 2
				modifier = {
					factor = accolade_progress # scales with accolade progress
				}
				modifier = { # must have an eligible knight
					NOT = {
						root = {
							any_side_knight = {
								can_unlock_accolade_attribute_trigger = {
									ATTRIBUTE = thug
								}
								any_killed_character = {
									OR = {
										is_in_list = slain_combatants
										is_in_list = victorious_slain_combatants
									}
								}
							}
						}
					}
					factor = 0
				}
				save_scope_as = accolade_liege
				set_variable = {
					name = accolade_progress
					value = 0
				}
				root = {
					random_side_knight = {
						limit = {
							can_unlock_accolade_attribute_trigger = {
								ATTRIBUTE = thug
							}
							any_killed_character = {
								OR = {
									is_in_list = slain_combatants
									is_in_list = victorious_slain_combatants
								}
							}
						}
						weight = {
							base = 1
							modifier = {
								add = 50
								is_acclaimed = yes
							}
						}
						save_scope_as = accolade_knight
					}
				}
				send_interface_message = {
					type = msg_accolade_eligibility
					title = accolade_thug_unlock.t
					left_icon = scope:accolade_knight
					right_icon = scope:accolade_liege
					custom_tooltip = accolade_thug_unlock.tt
					scope:accolade_knight = {
						set_variable = {
							name = thug_attribute_unlock
							value = yes
						}
					}	
				}
			}
		}	
	}
}

#####################
# Prisoners of War	

# Prisoner notification event for combat winner.
combat_event.1002 = {
	type = character_event
	hidden = yes

	immediate = {
		# Initialize the variable we will use to keep track of how many relevant prisoners there are.
		set_variable = {
			name = num_other_relevant_captured_combatants
			value = 0
		}

		# Re-order the combatant list based on who we care about the most.
		ordered_in_list = {
			list = prisoners_of_war
			max = 99
			check_range_bounds = no

			order_by = {
				value = captured_combatant_weights_for_root_participant
			}

			# Add the prisoners to a new list tailored to be more relevant to this character.
			add_to_list = relevant_prisoners_of_war
			root = {
				change_variable = {
					name = num_other_relevant_captured_combatants
					add = 1
				}
			}

			# Since we will send custom imprisonment events, block the default ones.
			hidden_effect = {
				if = {
					limit = { is_alive = yes }
					add_character_flag = {
						flag = block_imprisonment_event
						days = 1
					}
				}
			}

			# The first two prisoners on this list (i.e., the most important people to us) should be have their individual scopes saved for use in portraits & localization.
			if = {
				limit = {
					NOT = { exists = scope:primary_prisoner }
				}
				save_scope_as = primary_prisoner
			}
			else_if = {
				limit = {
					NOT = { exists = scope:secondary_prisoner }
				}
				save_scope_as = secondary_prisoner
			}
		}

		# Decrement the number of relevant captured combatants, to exclude the primary prisoner from the count (for localization).
		change_variable = {
			name = num_other_relevant_captured_combatants
			add = -1
		}

		# Send a message telling us about the prisoners we have captured.
		send_interface_message = {
			type = event_captured_combatants_good
			title = combat_event.1002.pow_message.t
			desc = {
				first_valid = {
					# Only one relevant prisoner.
					triggered_desc = {
						trigger = {
							var:num_other_relevant_captured_combatants = 0
						}
						desc = combat_event.1002.pow_message.desc.single
					}
					# Exactly two relevant prisoners.
					triggered_desc = {
						trigger = {
							var:num_other_relevant_captured_combatants = 1
						}
						desc = combat_event.1002.pow_message.desc.double
					}
					# Three or more relevant prisoners.
					desc = combat_event.1002.pow_message.desc.multiple
				}
				desc = combat_event.1002.pow_message.desc.end
			}
			tooltip = event_message_effect
			left_icon = scope:primary_prisoner

			# Imprison all prisoners of war
			every_in_list = {
				list = relevant_prisoners_of_war
				limit = { is_alive = yes }
				
				save_temporary_scope_as = this_prisoner
				scope:combat_winner = {
					if = {
						limit = { scope:this_prisoner = { is_imprisoned = no } }
						imprison = {
							target = scope:this_prisoner
							type = house_arrest
						}
					}
				}
				hidden_effect = {
					#######################
					# If necessary, send toasts about the captured prisoner to the war leaders.

					# Check if this prisoner is important enough (worth war score) that it is worth sending a toast about them in the first place.
					if = {
						limit = {
							exists = scope:primary_combat_winner
							exists = scope:primary_combat_loser

							combat_event_1001_character_is_important_combatant = yes
						}
						save_scope_as = valuable_prisoner

						# The imprisoner must be informed that they captured an important hostage.
						scope:combat_winner = {
							trigger_event = combat_event.1005
						}

						# The losing primary participant must be informed that they will/have lost warscore because a hostage was captured.
						if = {
							limit = {
								# But not if they themselves are the prisoner! (they will get an event already upon being imprisoned).
								scope:valuable_prisoner != scope:primary_combat_loser
							}
							scope:primary_combat_loser = {
								trigger_event = combat_event.1006
							}
						}

						# If the combat winner is different from the winning war leader, we also need to send a message to the war leader.
						if = {
							limit = {
								scope:combat_winner != scope:primary_combat_winner
							}
							scope:primary_combat_winner = {
								trigger_event = combat_event.1007
							}
						}
					}
				}
			}
			if = {
				limit = {
					scope:combat_winner = { is_in_army = yes }
					any_in_list = {
						list = relevant_prisoners_of_war
						is_alive = yes
						is_imprisoned_by = scope:combat_winner
						has_relation_rival = scope:combat_winner
					}
				}
				every_in_list = {
					list = relevant_prisoners_of_war
					limit = {
						is_alive = yes
						is_imprisoned_by = scope:combat_winner
						has_relation_rival = scope:combat_winner
					}
					add_to_list = captured_rivals
				}
				scope:combat_winner = { trigger_event = bp1_yearly.8060 }
			}
		}
		
		remove_variable = num_other_relevant_captured_combatants
	}
}

# Prisoner notification event for combat loser.
combat_event.1003 = {
	type = character_event
	hidden = yes

	immediate = {
		# Initialize the variable we will use to keep track of how many relevant prisoners there are.
		set_variable = {
			name = num_other_relevant_captured_combatants
			value = 0
		}

		# Re-order the combatant list based on who we care about the most.
		ordered_in_list = {
			list = prisoners_of_war
			max = 99
			check_range_bounds = no

			limit = {
				this != root
			}

			order_by = {
				value = captured_combatant_weights_for_root_participant
			}

			# Add the prisoners to a new list tailored to be more relevant to this character.
			add_to_list = relevant_prisoners_of_war
			root = {
				change_variable = {
					name = num_other_relevant_captured_combatants
					add = 1
				}
			}

			# The first two prisoners on this list (i.e., the most important people to us) should be have their individual scopes saved for use in portraits & localization.
			if = {
				limit = {
					NOT = { exists = scope:primary_prisoner }
				}
				save_scope_as = primary_prisoner
			}
			else_if = {
				limit = {
					NOT = { exists = scope:secondary_prisoner }
				}
				save_scope_as = secondary_prisoner
			}
		}

		# Decrement the number of relevant captured combatants, to exclude the primary prisoner from the count (for localization).
		change_variable = {
			name = num_other_relevant_captured_combatants
			add = -1
		}

		# Send a message telling us about the prisoners our enemy captured.
		send_interface_message = {
			type = event_captured_combatants_bad
			title = combat_event.1003.pow_message.t
			desc = {
				first_valid = {
					# Only one relevant prisoner.
					triggered_desc = {
						trigger = {
							var:num_other_relevant_captured_combatants = 0
						}
						desc = combat_event.1003.pow_message.desc.single
					}
					# Exactly two relevant prisoners.
					triggered_desc = {
						trigger = {
							var:num_other_relevant_captured_combatants = 1
						}
						desc = combat_event.1003.pow_message.desc.double
					}
					# Three or more relevant prisoners.
					desc = combat_event.1003.pow_message.desc.multiple
				}
				desc = combat_event.1003.pow_message.desc.end
			}
			tooltip = event_message_effect
			left_icon = scope:primary_prisoner
			right_icon = scope:combat_winner

			# Imprison all prisoners of war
			every_in_list = {
				list = relevant_prisoners_of_war
				limit = { is_alive = yes }
				
				save_temporary_scope_as = this_prisoner
				show_as_tooltip = {
					scope:combat_winner = {
						imprison = {
							target = scope:this_prisoner
							type = house_arrest
						}
					}
				}
			}
		}
		
		remove_variable = num_other_relevant_captured_combatants
	}
}

# Prisoner notification event for other relevant characters (sends a personalized notification to a player about characters of interest who were captured in battle.)
combat_event.1004 = {
	type = character_event
	hidden = yes

	immediate = {
		# Initialize the variable we will use to keep track of how many relevant prisoners there are.
		set_variable = {
			name = num_other_relevant_captured_combatants
			value = 0
		}

		# Transform the list of all prisoners of war into a list of prisoners that we specifically care about.
		ordered_in_list = {
			list = prisoners_of_war
			max = 99
			check_range_bounds = no

			limit = {
				this != root # Do not send a message about ourselves.
				combat_event_1001_character_is_relevant_to_target_trigger = { TARGET = root }
			}

			order_by = {
				value = captured_combatant_weights_for_root_nonparticipant
			}

			# Add the prisoners to a new list tailored to be more relevant to this character.
			add_to_list = relevant_prisoners_of_war
			root = {
				change_variable = {
					name = num_other_relevant_captured_combatants
					add = 1
				}
			}

			# The first two prisoners on this list (i.e., the most important people to us) should be have their individual scopes saved for use in portraits & localization.
			if = {
				limit = {
					NOT = { exists = scope:primary_prisoner }
				}
				save_scope_as = primary_prisoner
			}
			else_if = {
				limit = {
					NOT = { exists = scope:secondary_prisoner }
				}
				save_scope_as = secondary_prisoner
			}
		}

		# Decrement the number of relevant captured combatants, to exclude the primary prisoner from the count (for localization).
		change_variable = {
			name = num_other_relevant_captured_combatants
			add = -1
		}

		# Send a message to the player informing them of the captured combatants.
		send_interface_message = {
			type = event_captured_combatants_neutral
			title = combat_event.1004.pow_message.t
			desc = {
				first_valid = {
					# Only one relevant prisoner.
					triggered_desc = {
						trigger = {
							var:num_other_relevant_captured_combatants = 0
						}
						desc = combat_event.1003.pow_message.desc.single
					}
					# Exactly two relevant prisoners.
					triggered_desc = {
						trigger = {
							var:num_other_relevant_captured_combatants = 1
						}
						desc = combat_event.1003.pow_message.desc.double
					}
					# Three or more relevant prisoners.
					desc = combat_event.1003.pow_message.desc.multiple
				}
				desc = combat_event.1004.pow_message.desc.end
			}
			tooltip = event_message_effect
			left_icon = scope:primary_prisoner
			right_icon = scope:combat_winner

			show_as_tooltip = {
				every_in_list = {
					list = relevant_prisoners_of_war
					limit = { is_alive = yes }
					
					save_temporary_scope_as = this_prisoner
					scope:combat_winner = {
						imprison = {
							target = scope:this_prisoner
							type = house_arrest
						}
					}
				}
			}
		}
		
		remove_variable = num_other_relevant_captured_combatants
	}
}

# Sents a toast about a captured hostage to the winning commander.
combat_event.1005 = {
	type = character_event
	hidden = yes

	immediate = {
		if = {
			limit = {
				scope:valuable_prisoner = scope:primary_combat_loser
			}
			send_interface_toast = {
				type = msg_character_captured_by_me
				title = combat_event.1005.pow_toast.t
				desc = combat_event.1005.pow_toast.desc.primary
				left_icon = scope:combat_winner
				right_icon = scope:valuable_prisoner
			}
		}
		else = {
			send_interface_toast = {
				type = msg_character_captured_by_me
				title = combat_event.1005.pow_toast.t
				desc = combat_event.1005.pow_toast.desc
				left_icon = scope:combat_winner
				right_icon = scope:valuable_prisoner
			}
		}
	}
}

# Sents a toast about a captured hostage to the losing primary participant.
combat_event.1006 = {
	type = character_event
	hidden = yes

	immediate = {
		send_interface_toast = {
			type = msg_character_captured_by_other_side
			title = combat_event.1006.pow_toast.t
			desc = combat_event.1006.pow_toast.desc
			left_icon = scope:combat_winner
			right_icon = scope:valuable_prisoner
		}
	}
}

# Sents a toast about a captured hostage to the winning primary participant.
combat_event.1007 = {
	type = character_event
	hidden = yes

	immediate = {
		if = {
			limit = {
				scope:valuable_prisoner = scope:primary_combat_loser
			}
			send_interface_toast = {
				type = msg_character_captured_by_me
				title = combat_event.1007.pow_toast_primary.t
				desc = combat_event.1007.pow_toast.desc.primary
				left_icon = scope:combat_winner
				right_icon = scope:valuable_prisoner
			}
		}
		else = {
			send_interface_toast = {
				type = msg_character_captured_by_me
				title = combat_event.1007.pow_toast.t
				desc = combat_event.1007.pow_toast.desc
				left_icon = scope:combat_winner
				right_icon = scope:valuable_prisoner
			}
		}
	}
}

scripted_effect generate_relevant_slain_combatants_list_effect = {
	# Initialize the variable we will use to keep track of how many relevant casualties there are.
	set_variable = {
		name = num_other_relevant_slain_combatants
		value = 0
	}

	# Re-order the combatant list based on who we care about the most.
	ordered_in_list = {
		list = $LIST_OF_SLAIN_COMBATANTS$
		max = 99
		check_range_bounds = no

		limit = { # We only care about people on the enemy side who matter to us, not generic knights.
			this != root # Do not send a message about ourselves.
			trigger_if = {
				limit = {
					$LIST_ALL_CHARACTERS$ = no
				}
				# Must be relevant to root in some way.
				OR = {
					AND = {
						exists = scope:combat_war
						root = { is_attacker_in_war = scope:combat_war }
						this = scope:combat_war.primary_defender
					}
					AND = {
						exists = scope:combat_war
						root = { is_defender_in_war = scope:combat_war }
						this = scope:combat_war.primary_attacker
					}
					combat_event_1001_character_is_relevant_to_target_trigger = { TARGET = root }
				}
			}
		}

		order_by = {
			value = 0
			if = {
				limit = {
					OR = {
						AND = {
							exists = scope:combat_war
							root = { is_attacker_in_war = scope:combat_war }
							this = scope:combat_war.primary_defender
						}
						AND = {
							exists = scope:combat_war
							root = { is_defender_in_war = scope:combat_war }
							this = scope:combat_war.primary_attacker
						}
					}
				}
				add = 1000
			}
			add = slain_combatant_weights_for_root_participant
		}

		# Add the casualties to a new list tailored to be more relevant to this character.
		add_to_list = relevant_slain_combatants
		root = {
			change_variable = {
				name = num_other_relevant_slain_combatants
				add = 1
			}
		}

		# The first two casualties on this list (i.e., the most important people to us) should be have their individual scopes saved for use in portraits & localization.
		if = {
			limit = {
				NOT = { exists = scope:primary_deceased }
			}
			save_scope_as = primary_deceased

			# Decrement the number of relevant slain combatants, to exclude the primary deceased from the count (for localization).
			root = {		
				change_variable = {
					name = num_other_relevant_slain_combatants
					add = -1
				}
			}
		}
		else_if = {
			limit = {
				NOT = { exists = scope:secondary_deceased }
			}
			save_scope_as = secondary_deceased
		}
	}
}

scripted_effect clear_relevant_slain_combatants_list_effect = {
	every_in_list = {
		list = relevant_slain_combatants
		remove_from_list = relevant_slain_combatants
	}
	remove_variable = num_other_relevant_slain_combatants
	clear_saved_scope = primary_deceased
	clear_saved_scope = secondary_deceased
}

#####################
# Slain Combatants	

# Casualty notification event for combat winner.
combat_event.1012 = {
	type = character_event
	hidden = yes

	immediate = {
		# Generate a list of relevant slain characters on the enemy's side.
		generate_relevant_slain_combatants_list_effect = {
			LIST_OF_SLAIN_COMBATANTS = slain_combatants
			LIST_ALL_CHARACTERS = no
		}	
		if = {
			limit = {
				any_in_list = {
					list = relevant_slain_combatants
				}
			}
			# Send a message telling us about the casualties we inflicted after the battle
			send_interface_message = {
				type = event_slain_combatants_good
				title = combat_event.1012.slain_message.t
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 0	}
							desc = combat_event.1012.slain_message.desc.single
						}
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 1	}
							desc = combat_event.1012.slain_message.desc.double
						}
						desc = combat_event.1012.slain_message.desc.multiple
					}
					desc = combat_event.1012.slain_message.desc.end
				}
				tooltip = event_message_effect
				left_icon = scope:primary_deceased

				# Show all slain combatants.
				every_in_list = {
					list = relevant_slain_combatants
					
					show_as_tooltip = {
						death = {
							death_reason = death_battle
						}
					}
				}
			}
		}
		clear_relevant_slain_combatants_list_effect = yes

		# Repeat the above, but for casualties on our own side.
		generate_relevant_slain_combatants_list_effect = {
			LIST_OF_SLAIN_COMBATANTS = victorious_slain_combatants
			LIST_ALL_CHARACTERS = yes
		}
		if = {
			limit = {
				any_in_list = {
					list = relevant_slain_combatants
				}
			}
			# Send a message telling us about the casualties we inflicted after the battle
			send_interface_message = {
				type = event_slain_combatants_bad
				title = combat_event.1013.slain_message.t
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 0	}
							desc = combat_event.1013.slain_message.desc.single
						}
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 1	}
							desc = combat_event.1013.slain_message.desc.double
						}
						desc = combat_event.1013.slain_message.desc.multiple
					}
					desc = combat_event.1013.slain_message.desc.end
				}
				tooltip = event_message_effect
				left_icon = scope:primary_deceased

				# Show all slain combatants.
				every_in_list = {
					list = relevant_slain_combatants
					
					show_as_tooltip = {
						death = {
							death_reason = death_battle
						}
					}
				}
			}
		}
		clear_relevant_slain_combatants_list_effect = yes
	}
}

# Casualty notification event for combat loser.
combat_event.1013 = {
	type = character_event
	hidden = yes

	immediate = {
		# Generate a list of relevant slain characters on the enemy's side.
		generate_relevant_slain_combatants_list_effect = {
			LIST_OF_SLAIN_COMBATANTS = victorious_slain_combatants
			LIST_ALL_CHARACTERS = no
		}	
		if = {
			limit = {
				any_in_list = {
					list = relevant_slain_combatants
				}
			}
			# Send a message telling us about the casualties we inflicted after the battle
			send_interface_message = {
				type = event_slain_combatants_good
				title = combat_event.1012.slain_message.t
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 0	}
							desc = combat_event.1012.slain_message.desc.single
						}
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 1	}
							desc = combat_event.1012.slain_message.desc.double
						}
						desc = combat_event.1012.slain_message.desc.multiple
					}
					desc = combat_event.1012.slain_message.desc.end
				}
				tooltip = event_message_effect
				left_icon = scope:primary_deceased

				# Show all slain combatants.
				every_in_list = {
					list = relevant_slain_combatants
					
					show_as_tooltip = {
						death = {
							death_reason = death_battle
						}
					}
				}
			}
		}
		clear_relevant_slain_combatants_list_effect = yes

		# Repeat the above, but for casualties on our own side.
		generate_relevant_slain_combatants_list_effect = {
			LIST_OF_SLAIN_COMBATANTS = slain_combatants
			LIST_ALL_CHARACTERS = yes
		}
		if = {
			limit = {
				any_in_list = {
					list = relevant_slain_combatants
				}
			}
			# Send a message telling us about the casualties we inflicted after the battle
			send_interface_message = {
				type = event_slain_combatants_bad
				title = combat_event.1013.slain_message.t
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 0	}
							desc = combat_event.1013.slain_message.desc.single
						}
						triggered_desc = {
							trigger = {	var:num_other_relevant_slain_combatants = 1	}
							desc = combat_event.1013.slain_message.desc.double
						}
						desc = combat_event.1013.slain_message.desc.multiple
					}
					desc = combat_event.1013.slain_message.desc.end
				}
				tooltip = event_message_effect
				left_icon = scope:primary_deceased

				# Show all slain combatants.
				every_in_list = {
					list = relevant_slain_combatants
					
					show_as_tooltip = {
						death = {
							death_reason = death_battle
						}
					}
				}
			}
		}
		clear_relevant_slain_combatants_list_effect = yes
	}		
}

# Casualty notification event for other relevant characters (sends a personalized notification to a player about characters of interest who were captured in battle.)
combat_event.1014 = {
	type = character_event
	hidden = yes

	immediate = {
		every_in_list = {
			list = slain_combatants
			add_to_list = all_slain_combatants
		}
		every_in_list = {
			list = victorious_slain_combatants
			add_to_list = all_slain_combatants
		}

		# Generate a list of all relevant slain combatants on both sides.
		generate_relevant_slain_combatants_list_effect = {
			LIST_OF_SLAIN_COMBATANTS = all_slain_combatants
			LIST_ALL_CHARACTERS = no
		}

		if = {
			limit = {
				any_in_list = {
					list = relevant_slain_combatants
				}
			}
			# Send a message to the player informing them of the slain combatants.
			send_interface_message = {
				type = event_slain_combatants_neutral
				title = combat_event.1014.slain_message.t
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {
								var:num_other_relevant_slain_combatants = 0
							}
							desc = combat_event.1013.slain_message.desc.single
						}
						triggered_desc = {
							trigger = {
								var:num_other_relevant_slain_combatants = 1
							}
							desc = combat_event.1013.slain_message.desc.double
						}
						desc = combat_event.1013.slain_message.desc.multiple
					}
					desc = combat_event.1013.slain_message.desc.end
				}
				tooltip = event_message_effect
				left_icon = scope:primary_deceased

				# Show all slain combatants.
				show_as_tooltip = {
					every_in_list = {
						list = relevant_slain_combatants
						
						death = {
							death_reason = death_battle
						}
					}
				}
			}
		}
		
		clear_relevant_slain_combatants_list_effect = yes
	}
}

# Become known as a Gajapati if you have the prerequisites
combat_event.2001 = {
	hidden = yes
	scope = combat_side

	trigger = {
		has_maa_of_type = war_elephant
		side_commander ?= {
			is_alive = yes
			culture = {
				has_cultural_parameter = elephant_bonuses_for_rank_4_education
			}
			OR = {
				has_trait = education_diplomacy_4
				has_trait = education_stewardship_4
				has_trait = education_martial_4
				has_trait = education_learning_4
				has_trait = education_diplomacy_5
				has_trait = education_stewardship_5
				has_trait = education_martial_5
				has_trait = education_learning_5
			}
			NOT = {
				has_nickname = nick_gajapati
			}
		}
	}

	immediate = {
		random = {
			chance = 15
			side_commander = { set_nickname_effect = { NICKNAME = nick_gajapati } }
		}
	}
}

#####################
# Accolade Glory Changes

#	Battle Won: glory goes up.
combat_event.2101 = {
	hidden = yes
	scope = combat_side

	trigger = {
		OR = {
			any_side_knight = {
				is_acclaimed = yes
			}
			any_side_commander = {
				is_acclaimed = yes
			}
		}
	}

	immediate = {
		every_side_knight = {
			limit = {
				is_acclaimed = yes
			}
			accolade = { add_glory = minor_glory_gain }
		}
		every_side_commander = {
			limit = {
				is_acclaimed = yes
			}
			accolade = { add_glory = minor_glory_gain }
		}
	}
}

#	Battle Lost: glory goes down [sad trombone noises].
combat_event.2111 = {
	hidden = yes
	scope = combat_side

	trigger = {
		OR = {
			any_side_knight = {
				is_acclaimed = yes
			}
			any_side_commander = {
				is_acclaimed = yes
			}
		}
	}

	immediate = {
		every_side_knight = {
			limit = {
				is_acclaimed = yes
			}
			accolade = { add_glory = minimal_glory_loss }
		}
		every_side_commander = {
			limit = {
				is_acclaimed = yes
			}
			accolade = { add_glory = minimal_glory_loss }
		}
	}
}
#
######################
## Hereward the Wake
#
## You beat a Norman! Gain XP.
#combat_event.2200 = {
#	hidden = yes
#	scope = combat_side
#
#	trigger = {
#		exists = side_commander
#		exists = enemy_side.side_commander
#		enemy_side.side_commander = {
#			has_culture = culture:norman
#		}
#		side_commander = {
#			has_trait = the_wake
#		}
#	}
#
#	immediate = {
#		if = { #Hereward should always have the trait, but just in case
#			limit = {
#				side_commander = {
#					has_trait = the_wake
#				}
#			}
#			side_commander = {
#				add_trait_xp = {
#					trait = the_wake
#					value = { 10 15 }
#				}
#			}
#		}
#	}
#}

##############
# Battle PoI #
##############
combat_event.3000 = {
	hidden = yes
	scope = combat_side

	trigger = {
		# Check to ensure all needed scopes are valid
		exists = combat.location
		exists = side_primary_participant
		exists = enemy_side.side_primary_participant

		combat.location = { # Check if the region has gotten one the last 25 years already
			switch = {
				trigger = geographical_region
				graphical_western = { battle_poi_region_trigger = { REGION = graphical_western } }
				graphical_mena = { battle_poi_region_trigger = { REGION = graphical_mena } }
				graphical_india = { battle_poi_region_trigger = { REGION = graphical_india } }
				graphical_mediterranean = { battle_poi_region_trigger = { REGION = graphical_mediterranean } }
				graphical_steppe = { battle_poi_region_trigger = { REGION = graphical_steppe } }
			}
			NOR = { # Check that it doesn't already have any of the key data variables on the province, or a historical battle
				has_travel_point_of_interest = poi_battles_historical
				has_variable = battle_poi_winner
				has_variable = battle_poi_loser
				has_variable = battle_poi_date_day
				has_variable = battle_poi_date_month
				has_variable = battle_poi_date_year
			}
		}

		# Check if this battle was important enough
		calc_true_if = {
			amount >= 3
			any_in_list = {
				list = slain_combatants
				OR = {
					this = root.enemy_side.side_primary_participant
					is_heir_of = root.enemy_side.side_primary_participant
					is_consort_of = root.enemy_side.side_primary_participant
				}
			}
			any_in_list = {
				list = prisoners_of_war
				OR = {
					this = root.enemy_side.side_primary_participant
					is_heir_of = root.enemy_side.side_primary_participant
					is_consort_of = root.enemy_side.side_primary_participant
				}
			}
			troops_ratio <= 0.5
			num_enemies_killed >= battle_poi_number_value # Scaling the later it gets, starts with 3000 before 1000 and then multiplies by 2 in 1000, 3 in 1100, etc
			AND = {
				percent_enemies_killed >= 65
				combat = {
					num_total_troops >= {
						add = battle_poi_number_value 
						multiply = 2
					}
				}
			}
			combat = {
				num_total_troops >= {
					add = battle_poi_number_value
					multiply = 3
				}
			}
			combat = { warscore_value >= 15 }
			side_primary_participant = { is_ai = no }
			enemy_side.side_primary_participant = { is_ai = no }
		}
	}

	immediate = {
		combat.location = {
			save_scope_as = combat_location

			switch = { ### Setting a cooldown for the region, so we don't get too many
				trigger = geographical_region
				graphical_western = { battle_poi_region_cooldown_effect = { REGION = graphical_western } }
				graphical_mena = { battle_poi_region_cooldown_effect = { REGION = graphical_mena } }
				graphical_india = { battle_poi_region_cooldown_effect = { REGION = graphical_india } }
				graphical_mediterranean = { battle_poi_region_cooldown_effect = { REGION = graphical_mediterranean } }
				graphical_steppe = { battle_poi_region_cooldown_effect = { REGION = graphical_steppe } }
			}

			### Key data
			add_to_global_variable_list = { # Add to list so it can be found by the PoI
				name = battle_poi_list
				target = this
			}
			set_variable = { # Winning Commander
				name = battle_poi_winner
				value = root.side_primary_participant
			}
			root.side_primary_participant = {
				if = {
					limit = {
						has_dynasty = no
					}
					make_unprunable = yes
				}
			}
			set_variable = { # Losing Commander
				name = battle_poi_loser
				value = root.enemy_side.side_primary_participant
			}
			root.enemy_side.side_primary_participant = {
				if = {
					limit = {
						has_dynasty = no
					}
					make_unprunable = yes
				}
			}
			set_variable = { # Day
				name = battle_poi_date_day
				value = current_day
			}
			set_variable = { # Month
				name = battle_poi_date_month
				value = current_month
			}
			set_variable = { # Year
				name = battle_poi_date_year
				value = current_year
			}
			random_list = { # Set a randomized variable, so we don't need the same desc every time
				10 = { battle_poi_randomizer_effect = { NAME = 1 } }
				10 = { battle_poi_randomizer_effect = { NAME = 2 } }
				10 = { battle_poi_randomizer_effect = { NAME = 3 } }
			}
			### Extra (optional) data
			if = { # Attacker
				limit = {
					root = { is_combat_side_attacker = yes }
				}
				set_variable = { name = battle_poi_attacker }
			}
			else = { # Defender
				set_variable = { name = battle_poi_defender }
			}
			set_variable = { # Slain enemies (Percentage)
				name = battle_poi_slain
				value = root.percent_enemies_killed
			}
			if = { # Slay the opposing Commander
				limit = {
					any_in_list = {
						list = slain_combatants
						this = root.enemy_side.side_primary_participant
					}
				}
				set_variable = { name = battle_poi_enemy_commander_slain }
			}
			if = { # Imprison the opposing Commander
				limit = {
					any_in_list = {
						list = prisoners_of_war
						this = root.enemy_side.side_primary_participant
					}
				}
				set_variable = { name = battle_poi_enemy_commander_imprisoned }
			}
			root = {
				random_side_knight = { # Check for particularly brave knight
					limit = {
						OR = {
							prowess >= 20
							has_trait = brave
							has_trait = lifestyle_blademaster
							has_trait = varangian
							has_trait = berserker
						}
					}
					scope:combat_location = {
						set_variable = {
							name = battle_poi_brave_knight
							value = prev
						}
					}
				}
				side_primary_participant.commanding_army = { # Find a Unit Type that was in the battle
					random_army_maa_regiment = {
						limit = {
							NOR = { 
								is_unit_type = siege_weapon 
								is_unit_type = skirmishers
							}
						}
						switch = {
							trigger = is_unit_type
							archers = { battle_poi_maa_regiment_effect = { NAME = archers } }
							light_cavalry = { battle_poi_maa_regiment_effect = { NAME = light_cavalry } }
							archer_cavalry = { battle_poi_maa_regiment_effect = { NAME = light_cavalry } }
							pikemen = { battle_poi_maa_regiment_effect = { NAME = pikemen } }
							heavy_infantry = { battle_poi_maa_regiment_effect = { NAME = heavy_infantry } }
							heavy_cavalry = { battle_poi_maa_regiment_effect = { NAME = heavy_cavalry } }
							elephant_cavalry = { battle_poi_maa_regiment_effect = { NAME = heavy_cavalry } }
						}
					}
				}
			}
		}
	}
}
