﻿betterbattles_phase = {
	type = commander

	chance = {
		base = 0
		modifier = {
			add = 2250
			is_ai = no
		}
	}

	is_valid = {
		exists = root
	}

	effect = {
		
		ROOT = {
			save_scope_as = player_commander
		}

		# Checking if the character has one of the better battles base modifiers or not
		if = {
			limit = {
				scope:player_commander = {
					NOT = {
						OR = {
							has_character_modifier = melt
							has_character_modifier = unit
						}
					}
				}
			}
			# If the character does not have one of the BB modifiers, adds the base one which allows
			#	the firing of BB events
			scope:player_commander = {
				add_character_modifier = {
					modifier = unit
					years = 5
					# If the character does not serve as a commander for 5 years, they forget what command style they are
				}
			}
		}

		scope:combat_side = {
			random_side_knight = {
				limit = {
					is_alive = yes
				}
				save_scope_as = good_knight
			}
		}

		scope:combat_side.enemy_side = {
			random_side_knight = {
				limit = {
					is_alive = yes
				}
				save_scope_as = bad_knight
			}
		}

		scope:combat_side.enemy_side.side_commander = {
			save_scope_as = enemy_commander
		}

		scope:player_commander.location.barony = {
			save_scope_as = battle_location
		}
		
		# Randomly picks an event
		scope:player_commander = {
			if = {
				limit = {
					has_character_modifier = unit
					# If the player has the modifier that causes their character to be involved in the battle events
					#	then the code prompting the chance of events fires
				}
				random_list = {
					6000 = { # Should be 4000 BASE chance, scale appropriately when adding more events to the below list

						## Old People shouldn't be fighting
						modifier = {
							factor = 5
							age > 60
						}

						# Nothing happens
					}
					125 = { # Should be 125

						## Weak characters would know they'd lose
						modifier = {
							add = -25
							prowess < 10
						}
						modifier = {
							add = -25
							prowess < 7
						}
						modifier = {
							add = 25
							prowess >= 10
						}
						modifier = {
							add = 50
							prowess >= 30
						}

						trigger_event = {
							id = betterbattles.0102 # Avenge a fallen knight - 10% LETHAL
							days = 2
						}
					}
					200 = { # Should be 200

						## Cowards and weak characters wouldn't bother
						modifier = {
							add = -25
							prowess < 10
						}
						modifier = {
							add = -25
							prowess < 7
						}
						modifier = {
							factor = 0.5
							has_trait = craven
						}

						trigger_event = {
							id = betterbattles.0107 # Rescue a knight from massive odds - 35% LETHAL
							days = 2
						}
					}
					350 = { # Should be 350
						trigger_event = {
							id = betterbattles.0112 # Battlefield commander - NONLETHAL/55% LETHAL WITH ANY INJURY
							days = 2
						}
					}
					150 = { # Should be 150

						## Weak characters would get overwhelmed easily
						modifier = {
							add = 25
							prowess < 10
						}
						modifier = {
							add = 25
							prowess < 7
						}

						trigger_event = {
							id = betterbattles.0120 # Surrounded in battle - 50% LETHAL WITH BRAVE TRAIT/55% LETHAL
							days = 2
						}
					}
					350 = { # Should be 350
					
						## Helps weaker characters
						modifier = {
							add = 25
							prowess < 10
						}
						modifier = {
							add = 50
							prowess < 7
						}
						modifier = {
							add = 100
							prowess < 5
						}

						trigger_event = {
							id = betterbattles.0500 # Gain prowess - NONLETHAL
							days = 2
						}
					}
					350 = { # Should be 350
					
						## Helps less experienced characters
						modifier = {
							add = 25
							martial < 10
						}
						modifier = {
							add = 50
							martial < 7
						}
						modifier = {
							add = 100
							martial < 5
						}

						trigger_event = {
							id = betterbattles.0501 # Gain martial - NONLETHAL
							days = 2
						}
					}
					100 = { # Should be 100

						## Stronger cowardly characters would be more likely to gain confidence and become brave
						modifier = {
							factor = 5
							prowess >= 12
						}

						trigger_event = {
							id = betterbattles.0502 # Lose craven gain brave - NONLETHAL
							days = 2
						}
					}
					75 = { # Should be 75
						trigger_event = {
							id = betterbattles.0503 # Lose eye - NONLETHAL
							days = 2
						}
					}
					100 = { # Should be 100
						trigger_event = {
							id = betterbattles.0504 # Gain wounded_1 - NONLETHAL
							days = 2
						}
					}
					350 = { # Should be 350
						trigger_event = {
							id = betterbattles.0126 # Rival tries to slay you - 35% LETHAL BUT HAS ESCAPE OPTION
							days = 2
						}
					}
					350 = { # Should be 350
				
						## Zealots and humble characters are more holy
						modifier = {
							add = 100
							has_trait = zealous
						}
						modifier = {
							add = 100
							has_trait = humble
						}

						trigger_event = {
							id = betterbattles.1000 # Chance to gain holy warrior - NONLETHAL
							days = 2
						}
					}
					200 = { # Should be 200
						trigger_event = {
							id = betterbattles.1001 # Prestige gain - NONLETHAL
							days = 2
						}
					}
					200 = { # Should be 200
						trigger_event = {
							id = betterbattles.1002 # Prestige loss - NONLETHAL
							days = 2
						}
					}
					200 = { # Should be 200
						trigger_event = {
							id = betterbattles.1003 # Diplomacy/Advantage Trade Modifier - NONLETHAL
							days = 2
						}
					}
					150 = { # Should be 150
				
						## Zealots and humble characters are more holy
						modifier = {
							add = 100
							has_trait = zealous
						}
						modifier = {
							add = 100
							has_trait = humble
						}

						trigger_event = {
							id = betterbattles.1004 # Piety gain - NONLETHAL
							days = 2 
						} 
					}
					25 = { # Should be 25
						trigger_event = {
							id = betterbattles.1005 # The Black Knight Appears - NONLETHAL
							days = 2 
						} 
					}
					200 = { # Should be 200
						trigger_event = { 
							id = betterbattles.1006 # A Standout Warrior - NONLETHAL
							days = 2 
						} 
					}
					100 = { # Should be 100
						trigger_event = {
							id = betterbattles.1007 # A Brush with Death - NONLETHAL
							days = 2 
						} 
					}
					100 = { # Should be 100
						trigger_event = {
							id = betterbattles.1010 # Fighting an enemy levy - NONLETHAL
							days = 2 
						} 
					}
					75 = { # Should be 100
						trigger_event = {
							id = betterbattles.1009 # The Enemy Within
							days = 2 
						} 
					}
					250 = { # Should be 250

						## Gregarious characters would like speaking
						modifier = {
							factor = 3
							has_trait = gregarious
						}

						trigger_event = {
							id = betterbattles.0130 # Battlefield Speech - NONLETHAL
							days = 2 
						} 
					}
				}
			}
		}
	}
}