﻿
namespace = commanders

##################################
# Commander Battle Pulse Event 	 #
# by Sean Hughes and Alex Oltner #
# commanders.1001				 #
##################################

commanders.0001 = {
	hidden = yes

	trigger = {
		is_alive = yes
	}

	immediate = {
		random_list = {
			1000 = {
				modifier = {
					add = 1000
					has_perk = stalwart_leader_perk
					is_ai = no
				}
				modifier = {
					add = 300
					has_perk = stalwart_leader_perk
					is_ai = yes
				}
				
				modifier = { # Difficulty
					add = 1000
					is_ai = no
					has_game_rule = easy_difficulty
				}
				modifier = {
					add = 2000
					is_ai = no
					has_game_rule = very_easy_difficulty
				}
			}
			25 = { # Wounded
				trigger = {
					has_trait_rank = {
						trait = wounded
						rank <= 2
					}
				}
				modifier = {
					factor = { # Prowess directly reduces chance of injury/death
						value = 20 # Value at which 'immortality' happens
						subtract = prowess
						divide = 40
						min = 0 # 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.8 # Outnumber by factor of 2 gives a 0.9 modifier
						max = 1
					}
					scope:combat_side.side_strength > scope:combat_side.enemy_side.side_strength
				}
				modifier = {
					factor = 2
					has_trait = brave
				}
				modifier = {
					factor = 0.5
					has_trait = craven
				}

				# Trigger the battle event indicating we have been Wounded.
				if = {
					# Check for an enemy Knight with at least 80% of our Prowess to be our wound-inflicter.
					limit = {
						scope:combat_side.enemy_side = {
							any_side_knight = {
								this.prowess >= { value = root.prowess multiply = 0.8 }
							}
						}
					}
					scope:combat_side.enemy_side = {
						random_side_knight = {
							limit = {
								this.prowess >= { value = root.prowess multiply = 0.8 }
							}
							save_scope_as = enemy_knight
						}
					}
					# 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 }
					}
				}

				

			}
			10 = { # Maimed
				trigger = {
					NAND = {
						has_trait = one_legged
						has_trait = disfigured
						has_trait = one_eyed
						has_trait = maimed
					}
				}
				modifier = {
					factor = { # Prowess directly reduces chance of injury/death
						value = 20 # Value at which 'immortality' happens
						subtract = prowess
						divide = 30
						min = 0 # 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.8 # Outnumber by factor of 2 gives a 0.9 modifier
						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 > { value = scope:combat_side.enemy_side.side_strength multiply = 5 }
				}
				modifier = {
					factor = 2
					has_trait = brave
				}
				modifier = {
					factor = 0.5
					has_trait = craven
				}
				modifier = {
					factor = 1.1
					has_trait_rank = {
						trait = wounded
						rank = 1
					}
				}
				modifier = {
					factor = 1.25
					has_trait_rank = {
						trait = wounded
						rank = 2
					}
				}
				modifier = {
					factor = 1.5
					has_trait_rank = {
						trait = wounded
						rank = 3
					}
				}

				# Trigger the battle event indicating we have been Maimed.
				if = {
					# Check for an enemy Knight with at least 80% of our Prowess to be our maimer.
					limit = {
						scope:combat_side.enemy_side = {
							any_side_knight = {
								this.prowess >= { value = root.prowess multiply = 0.8 }
							}
						}
					}
					scope:combat_side.enemy_side = {
						random_side_knight = {
							limit = {
								this.prowess >= { value = root.prowess multiply = 0.8 }
							}
							save_scope_as = enemy_knight
						}
					}

					# 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
					}	
				}
			}
			5 = { #Killed
				trigger = {
					#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
					}
				}
				modifier = {
					factor = { # Prowess directly reduces chance of injury/death
						value = 20 # Value at which 'immortality' happens
						subtract = prowess
						divide = 30
						min = 0 # 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.8 # Outnumber by factor of 2 gives a 0.9 modifier
						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 > { value = scope:combat_side.enemy_side.side_strength multiply = 5 }
				}
				modifier = {
					factor = 2
					has_trait = brave
				}
				modifier = {
					factor = 0.5
					has_trait = craven
				}
				modifier = {
					factor = 2
					has_trait_rank = {
						trait = wounded
						rank = 1
					}
				}
				modifier = {
					factor = 3
					has_trait_rank = {
						trait = wounded
						rank = 2
					}
				}
				modifier = {
					factor = 3
					OR = {
						has_trait = one_legged
						has_trait = disfigured
						has_trait = one_eyed
						has_trait = maimed
					}
				}
				if = {
					limit = {
						scope:combat_side.enemy_side = {
							any_side_knight = {
								this.prowess >= { value = root.prowess multiply = 0.8 }
							}
						}
					}
					scope:combat_side.enemy_side = {
						random_side_knight = {
							limit = {
								this.prowess >= { value = root.prowess multiply = 0.8 }
							}
							save_scope_as = enemy_knight
						}
					}
					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
						}
					}
				}
			}
		}
	}
}
