﻿# 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 = { #low born
			if = { 
				limit = {					
					OR = {
						has_government = feudal_government
						has_government = japan_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.1001
					}
				}
			}
			
			if = {
				limit = {
					exists = army_commander
					is_army_in_combat = no
					army_commander = { 
						save_temporary_scope_as = commander
						commanding_army.army_owner = liege_or_court_owner
						is_at_war = no
					}
				}
				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
								has_relation_rival = liege_or_court_owner
								has_strong_claim_on = liege_or_court_owner.primary_title
								opinion = { target = liege_or_court_owner value < -40  }	
								opinion = { target = liege_or_court_owner value < -80  }								
								OR = {
									has_trait = deceitful
									has_trait = arbitrary
									has_trait = ambitious
								}
								liege_or_court_owner = {
									trigger_if = {
										limit = { scope:army.army_commander = { is_landed = yes }}
										NOT = { meets_legitimacy_expectation_of = scope:army.army_commander }
									}
									sum_of_all_skills_value <= sum_of_all_skills_threshold_poor
									is_incapable = yes
									is_adult = no
									has_any_criminal_trait_in_faith_trigger = { FAITH = faith CHARACTER = this}
									debt_level > 1
									OR = {
										government_has_flag= government_is_administrative
										government_has_flag = government_is_nomadic
										AND = {
											government_has_flag = government_is_celestial
											situation:dynastic_cycle ?= {
												situation_current_phase = situation_dynastic_cycle_phase_chaos
											}
										}
									}
								}
							}
							
						}
					}
					if = {
						limit = {
							OR = {
								liege_or_court_owner = { is_ai = no }
								has_game_rule = mig_both_player_ai
							}
						}
						army_commander = {
							random = {
								chance = 25
								trigger_event = mig_event.1007
							}
						}
					}
					else = {
						army_commander = {
							random = {
								chance = 5
								trigger_event = mig_event.1007
							}
						}
					}						
				}
				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
								is_powerful_vassal = 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 < -40  }	
								opinion = { target = liege_or_court_owner value < -80  }								
								OR = {
									has_trait = deceitful
									has_trait = arbitrary
									has_trait = ambitious
								}
								liege_or_court_owner = {
									trigger_if = {
										limit = { scope:army.army_commander = { is_landed = yes }}
										NOT = { meets_legitimacy_expectation_of = scope:army.army_commander }
									}
									sum_of_all_skills_value <= sum_of_all_skills_threshold_poor
									is_incapable = yes
									is_adult = no
									has_any_criminal_trait_in_faith_trigger = { FAITH = faith CHARACTER = this}
									debt_level > 1
									OR = {
										government_has_flag = government_is_administrative
										government_has_flag = government_is_nomadic
										AND = {
											government_has_flag = government_is_celestial
											situation:dynastic_cycle ?= {
												situation_current_phase = situation_dynastic_cycle_phase_chaos
											}
										}
									}
								}
							}
							
						}
					}
					if = {
						limit = {
							OR = {
								liege_or_court_owner = { is_ai = no }
								has_game_rule = mig_both_player_ai
							}
						}
						army_commander = {
							random = {
								chance = 2
								trigger_event = mig_event.2007
							}
						}
					}
					else = {
						army_commander = {
							random = {
								chance = 1
								trigger_event = mig_event.2007
							}
						}
					}	
				}
			}
			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 > 60  }
						OR = {
							has_trait = wrathful
							has_trait = brave
							has_relation_best_friend = liege_or_court_owner
							has_relation_friend = liege_or_court_owner
							ai_honor >= medium_positive_ai_value
						}
					}
				}					
				army_commander = {
					random = {
						chance = 33
						trigger_event = mig_event.1008
					}
					add_character_flag = { 
						flag = no_step_back
						years = 5 
					}
				}
			}
			
			if = { # betray
				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 = court_owner
							opinion = { target = liege_or_court_owner value < -60  }
							liege_or_court_owner = {
								has_imprisonment_reason = scope:army.army_commander
							}
						}
					}
				}					
				army_commander = {
					random = {
						chance = 6
						trigger_event = mig_event.2006
					}
				}
			}
		}
	}
}