﻿# On actions for army events, triggered automatically by the game

# called for armies every 30 days; exact date dependent on army ID
# root is the owner of the army
# scope:army is the army

on_army_monthly = {
	on_actions = {
		mig_on_army_monthly
	}
}

on_combat_end_winner = {
	events = {
		mig_event.0002
	}
}


mig_on_army_monthly = {
	events = {		
	}
	
	effect = {		
		root = {
			if = {
				limit = {
					has_government = feudal_government
				}
				random_knight = {
					limit = { 							
						knight_army = scope:army
						is_landed = yes
						is_lowborn = no
					}
					save_scope_as = discontent_knight
				}
			}
		}
		scope:army ?= {
			if = {
				limit = {
					exists = scope:discontent_knight
					exists = army_commander
					is_army_in_combat = no
					OR = {
						army_commander = { is_lowborn = yes }
						army_commander = { is_landed = no }
					}
					army_commander = { 
						commanding_army.army_owner = liege_or_court_owner
						NOT = {is_close_family_of = liege_or_court_owner }
					}
				}					
				army_commander = {
					random = {
						chance = 4
						trigger_event = mig_event.1000
					}
				}
			}
			
			if = {
				limit = {
					exists = army_commander
					is_army_in_combat = no
					NOT = { army_commander = root }
					army_commander = { 
						save_temporary_scope_as = commander
						root = liege_or_court_owner
					}
				}
				if = {
					limit = {
						army_commander = { 
							calc_true_if = {
								amount >= 4
								prestige_level > liege_or_court_owner.prestige_level
								has_claim_on = liege_or_court_owner.primary_title
								is_powerful_vassal = yes
								is_powerful_family = yes
								has_relation_rival = liege_or_court_owner
								has_strong_claim_on = liege_or_court_owner.primary_title
								opinion = { target = liege_or_court_owner value < -60  }								
								OR = {
									has_trait = deceitful
									has_trait = arbitrary
									has_trait = ambitious
								}
								liege_or_court_owner = {
									trigger_if = {
										limit = { has_legitimacy = yes }
										NOT = { meets_legitimacy_expectation_of = scope:army.army_commander }
									}
									has_trait = excommunicated
									sum_of_all_skills_value <= sum_of_all_skills_threshold_poor
									is_incapable = yes
									is_adult = no
									debt_level > 1
									OR = {
										government_has_flag = government_is_tribal
										government_has_flag = government_is_nomadic
										has_government = administrative_government
									}
								}
							}
							
						}
					}
					army_commander = {
						if = {
							limit = {
								root = { is_ai = no }
							}
							random = {
								chance = 15
								trigger_event = mig_event.7000
							}
						}
						else = {
							random = {
								chance = 5
								trigger_event = mig_event.7000
							}
						}
					}						
				}
				else_if = {
					limit = {
						army_commander = { 
							calc_true_if = {
								amount >= 3
								prestige_level > liege_or_court_owner.prestige_level
								has_claim_on = liege_or_court_owner.primary_title
								has_strong_claim_on = liege_or_court_owner.primary_title
								is_powerful_vassal = yes
								is_powerful_family = yes
								has_relation_rival = liege_or_court_owner
								opinion = { target = liege_or_court_owner value < -60  }									
								OR = {
									has_trait = deceitful
									has_trait = arbitrary									
									has_trait = ambitious

								}
								liege_or_court_owner = {
									trigger_if = {
										limit = { has_legitimacy = yes }
										NOT = { meets_legitimacy_expectation_of = scope:army.army_commander }
									}
									has_trait = excommunicated
									sum_of_all_skills_value <= sum_of_all_skills_threshold_poor
									is_incapable = yes
									is_adult = no
									debt_level > 1
									OR = {
										government_has_flag = government_is_tribal
										government_has_flag = government_is_nomadic
										has_government = administrative_government
									}			
								}
							}				
						}
					}
					army_commander = {
						if = {
							limit = {
								root = { is_ai = no }
							}
							random = {
								chance = 5
								trigger_event = mig_event.7000
							}
						}	
						else = {
							random = {
								chance = 2
								trigger_event = mig_event.7000
							}
						}						
					}	
				}
			}
			if = { # no step back
				limit = {
					exists = army_commander
					is_army_in_combat = no
					army_commander = { 
						NOT = { has_character_flag = no_step_back }
						commanding_army.army_owner = liege_or_court_owner		
						opinion = { target = liege_or_court_owner value > 80  }
						OR = {
							has_trait = wrathful
							has_trait = brave
							has_relation_best_friend = liege_or_court_owner
							has_relation_friend = liege_or_court_owner
						}
					}
				}					
				army_commander = {
					random = {
						chance = 1
						trigger_event = mig_event.8000
						add_character_flag = { 
							flag = no_step_back
							years = 5 
						}
					}
				}
			}
			
			if = { # betray warning
				limit = {
					exists = army_commander
					is_army_in_combat = no
					army_commander = { 
						liege_or_court_owner = {
							is_at_war = yes
						}
						NOT = { has_character_flag = betray_warned }
						commanding_army.army_owner = liege_or_court_owner		
						OR = {
							has_relation_rival = liege_or_court_owner
							has_claim_on = liege_or_court_owner.primary_title
							has_relation_rival = court_owner
							opinion = { target = liege_or_court_owner value < -80  }
							liege_or_court_owner = {
								has_imprisonment_reason = scope:army.army_commander
							}
						}
					}
				}					
				army_commander = {
					random = {
						chance = 6
						trigger_event = mig_event.7004
					}
				}
			}
		}
	}
}