﻿commander_inmotivated = {
	type = commander

	is_valid = {
		has_game_rule = GPT_all_war_events
		exists = root
		NOR = { 
			liege_or_court_owner = { has_character_flag = gpt_mig_divine_intervention }
			has_character_flag = gpt_mig_divine_intervention
		}
		commanding_army.army_owner = liege_or_court_owner
		is_independent_ruler = no
		commanding_army.army_owner = {
			is_ai = no
		}
		NOT = { has_character_modifier = commander_sabortage_army }
		scope:combat_side.combat.combat_war = {
			NOT = { is_participant = root }
		}
	}

	chance = {
		base = 10
		modifier = {
			factor = 0.5		
			is_close_or_extended_family_of = liege_or_court_owner			
		}
		
		modifier = {
			factor = {
				value = commander_opinion_to_liege
			}
		}
		
		modifier = {
			factor = 0.5
			ai_honor >= medium_positive_ai_value
		}
		
		modifier = {
			factor = 2
			has_trait = gluttonous
		}
		
		modifier = {
			factor = 2
			has_trait = callous
		}
	}

	effect = {
		scope:combat_side = { trigger_event = gpt_mig_event.1005 }
	}
}

commander_betray = {
	type = commander

	is_valid = {
		has_game_rule = GPT_all_war_events
		exists = root
		commanding_army.army_owner = liege_or_court_owner
		commanding_army.army_owner = { is_ai = no }
		is_landed = no		
		opinion = { 
			target = scope:combat_side.enemy_side.side_primary_participant 
			value > 0
		}
		opinion = { 
			target = liege_or_court_owner
			value < 60
		}
	}

	chance = {
		base = 33
		modifier = {
			factor = {
				value = commander_opinion_to_liege
			}	
		}
		
		modifier = {
			add = -100		
			is_close_or_extended_family_of = liege_or_court_owner			
		}
		
		modifier = {
			add = 100
			has_relation_rival = court_owner
		}
		
		modifier = {
			add = 100
			court_owner = {
				has_imprisonment_reason = root
			}
		}
		
		modifier = {
			add = 100
			is_close_or_extended_family_of = scope:combat_side.enemy_side.side_primary_participant 			
		}
		
		modifier = { 
			add = -50
			scope:combat_side.side_strength > scope:combat_side.enemy_side.vastly_outnumbered_combat_side_threshold
		}
		
		modifier = {
			add = 50
			scope:combat_side.side_strength < scope:combat_side.enemy_side.vastly_outnumbered_combat_side_threshold
		}
		
		modifier = {
			add = 50
			opinion = { 
				target = scope:combat_side.enemy_side.side_primary_participant 
				value > 50
			}
		}
		
		modifier = {
			add = 50
			has_any_criminal_trait_in_faith_trigger = { FAITH = faith CHARACTER = liege_or_court_owner}
		}
		
		
		modifier = {
			add = 50
			has_trait = arbitrary
		}
		
		modifier = {
			add = 50
			has_trait = deceitful
		}
		
		modifier = {
			add = -50
			has_trait = just
		}
		
		modifier = {
			add = -50
			has_trait = honest
		}
	}

	effect = {
		scope:combat_side = {
			trigger_event = gpt_mig_event.1006	
		}
	}
}


commander_wounded_mig = {
	type = commander

	is_valid = {
		has_game_rule = GPT_all_war_events
		exists = root
		has_character_flag = no_step_back_swore
		has_trait_rank = {
			trait = wounded
			rank <= 3
		}
	}

	chance = {
		base = 50
		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.1
			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 = {
				type = event_toast_effect_bad
				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 = {
				type = event_toast_effect_bad
				title = commander_wounded_no_enemy_interface_friendly_player
				left_icon = root

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

commander_maimed_mig = {
	type = commander

	is_valid = {
		has_game_rule = GPT_all_war_events
		exists = root
		has_character_flag = no_step_back_swore
		NAND = {
			has_trait = one_legged
			has_trait = disfigured
			has_trait = one_eyed
			has_trait = maimed
		}
	}

	chance = {
		base = 20
		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.1
			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 = {
				type = event_toast_effect_bad
				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 = {
				type = event_toast_effect_bad
				title = commander_maimed_no_enemy_interface_friendly_player
				left_icon = root

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

commander_killed_mig = {
	type = commander

	is_valid = {
		has_game_rule = GPT_all_war_events
		exists = root
		has_character_flag = no_step_back_swore
		#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
			}
		}
		NOR = {
			AND = {
				is_ai = no
				has_game_rule = very_easy_difficulty
			}
			ai_should_get_extreme_conqueror_bonuses = yes
		}
	}

	chance = {
		base = 10
		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.1
			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 = {
				type = event_slain_combatants_bad
				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 = {
				type = event_slain_combatants_bad
				title = commander_killed_no_enemy_interface_friendly
				left_icon = root
				# Kill the commander.
				death = {
					death_reason = death_battle
				}
			}
		}
	}
}










# commander_phase_events.txt

commander_outnumbered_outstanding = {
    type = commander

    is_valid = {
		has_game_rule = GPT_all_war_events
        exists = root
		OR = {
			AND = {
				commanding_army.army_owner = { is_ai = no }
				scope:combat_side.side_primary_participant = { is_ai = no }
				scope:combat_side.enemy_side.side_primary_participant = { is_ai = yes }
			}
			AND = {
				commanding_army.army_owner = { is_ai = yes }
				scope:combat_side.side_primary_participant = { is_ai = yes }
				scope:combat_side.enemy_side.side_primary_participant = { is_ai = yes }
			}
		}
		OR = {
			commanding_army.army_owner = liege_or_court_owner
			commanding_army.army_owner = root
		}
		NOR = { 
			liege_or_court_owner = { has_character_flag = gpt_mig_divine_intervention }
			has_character_flag = gpt_mig_divine_intervention
		}
		scope:combat_side.side_strength < scope:combat_side.enemy_side.side_strength
		scope:combat_side.side_strength > scope:combat_side.enemy_side.mig_min_strength
	 	scope:combat_side = { is_combat_side_attacker = no }
	 # 	scope:combat_side = { percent_enemies_killed < 50 }
	 #	scope:combat_side.enemy_side = { percent_enemies_killed < 50 }
    }

	chance = {
		base = 100000
	}

	effect = {
		random_list = {
			120 = {
				commanding_army.army_owner = {
					add_character_flag = {
						flag = gpt_mig_divine_intervention
						days = 30
					}
				}
			}
			12 = { # Luck 
				trigger = {
					scope:combat_side.side_strength >= scope:combat_side.enemy_side.mig_mid_strength
					scope:combat_side.side_strength < scope:combat_side.enemy_side.mig_high_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event.1077
				}
			}
			9 = { # Resolve
				trigger = {
					scope:combat_side.side_strength >= scope:combat_side.enemy_side.mig_low_strength
					scope:combat_side.side_strength < scope:combat_side.enemy_side.mig_mid_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event.1078
				}
			}
			6 = { # Strategy
				trigger = {
					scope:combat_side.side_strength >= scope:combat_side.enemy_side.mig_very_low_strength
					scope:combat_side.side_strength < scope:combat_side.enemy_side.mig_low_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event.1079
				}
			}
			3 = { # Miracle
				trigger = {
					scope:combat_side.side_strength >= scope:combat_side.enemy_side.mig_min_strength
					scope:combat_side.side_strength < scope:combat_side.enemy_side.mig_very_low_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event.1080
				}
			}
		}
	}
}

commander_outnumbered_outstanding_negative = {
    type = commander

    is_valid = {
		has_game_rule = GPT_all_war_events
        exists = root
		commanding_army.army_owner = { is_ai = no }
		scope:combat_side.side_primary_participant = { is_ai = no }
		scope:combat_side.enemy_side.side_primary_participant = { is_ai = yes }
		OR = {
			commanding_army.army_owner = liege_or_court_owner
			commanding_army.army_owner = root
		}
		NOR = { 
			liege_or_court_owner = { has_character_flag = gpt_mig_divine_intervention }
			has_character_flag = gpt_mig_divine_intervention
		}
		scope:combat_side.side_strength > scope:combat_side.enemy_side.side_strength
		scope:combat_side.enemy_side.side_strength > scope:combat_side.mig_min_strength
		scope:combat_side = { is_combat_side_attacker = yes }
	 #	scope:combat_side = { percent_enemies_killed < 20 }
	 #	scope:combat_side.enemy_side = { percent_enemies_killed < 20 }
    }

	chance = {
		base = 100000
	}

	effect = {
		random_list = {
			100 = {
				commanding_army.army_owner = {
					add_character_flag = {
						flag = gpt_mig_divine_intervention
						days = 30
					}
				}
			}
			12 = { # Luck 
				trigger = {
					scope:combat_side.enemy_side.side_strength >= scope:combat_side.mig_mid_strength
					scope:combat_side.enemy_side.side_strength < scope:combat_side.mig_high_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event_negative.1077
				}
			}
			9 = { # Resolve
				trigger = {
					scope:combat_side.enemy_side.side_strength >= scope:combat_side.mig_low_strength
					scope:combat_side.enemy_side.side_strength < scope:combat_side.mig_mid_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event_negative.1078
				}
			}
			6 = { # Strategy
				trigger = {
					scope:combat_side.enemy_side.side_strength >= scope:combat_side.mig_very_low_strength
					scope:combat_side.enemy_side.side_strength < scope:combat_side.mig_low_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event_negative.1079
				}
			}
			3 = { # Miracle
				trigger = {
					scope:combat_side.enemy_side.side_strength >= scope:combat_side.mig_min_strength
					scope:combat_side.enemy_side.side_strength < scope:combat_side.mig_very_low_strength
				}
				scope:combat_side = {
					trigger_event = gpt_mig_event_negative.1080
				}
			}
		}
	}
}
