﻿commander_none = {
	type = commander

	chance = {
		base = 1000
		modifier = {
			add = 1000
			has_perk = stalwart_leader_perk
			is_ai = no
		}
		modifier = {
			add = 300
			has_perk = stalwart_leader_perk
			is_ai = yes
		}
		modifier = {
			add = 300
			exists = dynasty
			dynasty = {
				has_dynasty_perk = warfare_legacy_3
			}
		}
		modifier = { # Difficulty
			add = 1000
			is_ai = no
			has_game_rule = easy_difficulty
		}
		modifier = {
			add = 2000
			is_ai = no
			has_game_rule = very_easy_difficulty
		}
	}
}

commander_wounded = {
	type = commander

	is_valid = {
		exists = root
		has_trait_rank = {
			trait = wounded
			rank <= 3
		}
	}

	chance = {
		base = 31
		modifier = {
			factor = { # Prowess directly reduces chance of injury/death
				value = 40 # Value at which 'immortality' happens
				subtract = prowess
				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.2
			faith = {
				has_doctrine_parameter = death_is_glory
			}
		}
		modifier = {
			factor = 2
			has_trait = brave
		}
		modifier = {
			factor = 0.5
			has_trait = craven
		}
	}

	effect = {
		# Trigger the battle event indicating we have been Wounded.
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
					save_scope_as = enemy_knight
					add_prestige = knight_prestige_gain_on_wound_inverse
					knight_increase_prowess_chance_effect = yes
				}
			}
			# Trigger the battle event where we are wounded by the enemy Knight.
			scope:combat_side = {
				battle_event = {
					key = "commander_wounded_by_enemy"
					left_portrait = root
					right_portrait = scope:enemy_knight
					type = wound
				}
			}
			# Send the toast telling the player if they have been wounded.
			send_interface_toast = {
				title = commander_wounded_by_enemy_interface_friendly_player
				left_icon = root
				right_icon = scope:enemy_knight

				# Inflict a wound on the commander.
				increase_wounds_effect = { REASON = fight }
			}
		}
		else = {
			# Trigger the battle event where we are wounded by a nameless soldier.
			scope:combat_side = {
				battle_event = {
					key = "commander_wounded_no_enemy"
					left_portrait = root
					type = wound
				}
			}
			# Send the toast telling the player if they have been wounded.
			send_interface_toast = {
				title = commander_wounded_no_enemy_interface_friendly_player
				left_icon = root

				# Inflict a wound on the commander.
				increase_wounds_effect = { REASON = fight }
			}
		}
	}
}

commander_maimed = {
	type = commander

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

	chance = {
		base = 12
		modifier = {
			factor = { # Prowess directly reduces chance of injury/death
				value = 30 # Value at which 'immortality' happens
				subtract = prowess
				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.2
			faith = {
				has_doctrine_parameter = death_is_glory
			}
		}
		modifier = {
			factor = 2
			has_trait = brave
		}
		modifier = {
			factor = 0.5
			has_trait = craven
		}
	}

	effect = {
		# Trigger the battle event indicating we have been Maimed.
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
					save_scope_as = enemy_knight
					add_prestige = knight_prestige_gain_on_wound_inverse
					knight_increase_prowess_chance_effect = yes
				}
			}

			# Trigger the battle event where we are maimed by an enemy Knight.
			scope:combat_side = {
				battle_event = {
					key = "commander_maimed_by_enemy"
					left_portrait = root
					right_portrait = scope:enemy_knight
					type = wound
				}
			}
			# Send the toast telling the player if they have been maimed.
			send_interface_toast = {
				title = commander_maimed_by_enemy_interface_friendly_player
				left_icon = root
				right_icon = scope:enemy_knight

				# Main the commander.
				maimed_in_battle_effect = yes
			}
		}
		else = {
			# Trigger the battle event were we are maimed by a nameless soldier.
			scope:combat_side = {
				battle_event = {
					key = "commander_maimed_no_enemy"
					left_portrait = root
					type = wound
				}
			}
			# Send the toast telling the player if they have been maimed.
			send_interface_toast = {
				title = commander_maimed_no_enemy_interface_friendly_player
				left_icon = root

				# Main the commander.
				maimed_in_battle_effect = yes
			}	
		}
	}
}

commander_killed = {
	type = commander

	is_valid = {
		exists = root
		#Commanders cannot be killed outright unless...
		OR = {
			#...they are already wounded...
			has_trait_rank = {
				trait = wounded
				rank >= 1
			}
			#...or they are absolutely terrible at battle to begin with!
			AND = {
				martial <= low_skill_rating
				prowess <= low_skill_rating
			}
		}
		NOT = {
			has_game_rule = very_easy_difficulty
		}
	}

	chance = {
		base = 3
		modifier = {
			factor = { # Prowess directly reduces chance of injury/death
				value = 30 # Value at which 'immortality' happens
				subtract = prowess
				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.2
			faith = {
				has_doctrine_parameter = death_is_glory
			}
		}
		modifier = {
			factor = 2
			has_trait = brave
		}
		modifier = {
			factor = 0.5
			has_trait = craven
		}
		modifier = {
			factor = 4
			has_trait_rank = {
				trait = wounded
				rank = 3
			}
		}
	}

	effect = {
		save_died_in_battle_variables_effect = {
			ENEMY = scope:combat_side.enemy_side.side_primary_participant
			LEADER = scope:combat_side.side_primary_participant
			LOCATION = scope:combat_side.combat.location
		}
		if = {
			limit = {
				scope:combat_side.enemy_side = {
					any_side_knight = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
				}
			}
			scope:combat_side.enemy_side = {
				random_side_knight = {
					limit = {
						this.prowess >= root.prowess_for_combat_event_opponent
					}
					save_scope_as = enemy_knight
					add_prestige = knight_prestige_gain_on_kill_inverse
					knight_increase_prowess_chance_effect = yes
				}
			}
			scope:combat_side = {
				battle_event = {
					key = "commander_killed_by_enemy"
					left_portrait = root
					right_portrait = scope:enemy_knight
					type = death
				}
			}
			send_interface_toast = {
				title = commander_killed_by_enemy_interface_friendly
				left_icon = root
				right_icon = scope:enemy_knight

				# Kill the commander.
				death = {
					death_reason = death_battle
					killer = scope:enemy_knight
				}
			}
			
		}
		else = {
			scope:combat_side = {
				battle_event = {
					key = "commander_killed_no_enemy"
					left_portrait = root
					type = death
				}
			}
			send_interface_toast = {
				title = commander_killed_no_enemy_interface_friendly
				left_icon = root

				# Kill the commander.
				death = {
					death_reason = death_battle
				}
			}
		}
	}
}
