rtr_recruitment_check = {
   trigger = {
   is_ai = no
   is_ruler = no
   is_travelling = no
   is_courtier = no
   OR = {
   location.province_owner = {
    any_liege_or_above = { is_at_war = yes has_raised_armies = yes }
   }
   location = { is_at_war = yes has_raised_armies = yes }
   }
   NOR = { has_variable = rtr_assigned_army character_has_flag = recent_draft }
   }
   random_events = {
		chance_to_happen = 100
		10 = 0
		50 = rtr.0118 # Recruited by war party
	}
}

rtr_drafted_on_action = {
 effect = {
  root = {
   set_variable = {
     name = rtr_assigned_army
	 value = scope:recruiting_army
   }
   set_variable = {
     name = home
	 value = root.location
  }
  set_variable = {
     name = patron
	 value = root.var:rtr_assigned_army.army_owner
  }
  }
  scope:recruiting_army = { set_variable = has_recruits }
  #add_character_modifier = forced_march_character_modifier
  #set_location = scope:recruiting_army.location
  start_travel_plan = {
            destination = scope:recruiting_army.location  # Nowhere
			players_use_planner = no
			on_arrival_on_action = rtr_army_arrival
			on_arrival_destinations = all_but_last
			can_cancel_planning = no
			return_trip = yes
        }
}
 
}

on_army_enter_province = {
 on_actions = { rtr_location_update }
}

rtr_location_update = {
trigger = { scope:army = { has_variable = has_recruits } }
effect = {
 every_player = {
 limit = { is_ruler = no has_variable = rtr_assigned_army var:rtr_assigned_army = scope:army }
 add_to_list = recruits_list
 }
 every_in_list = {
  list = recruits_list
  #set_location = scope:army.location
  start_travel_plan = {
            destination = scope:army.location  # Nowhere
			players_use_planner = no
			on_arrival_on_action = rtr_army_arrival
			on_arrival_destinations = all_but_last
			can_cancel_planning = no
			return_trip = yes
        }
  #current_travel_plan = { remove_travel_plan_modifier = forced_march_character_modifier }
  }
 
}

}	

on_siege_looting = {
  on_actions = {
   rtr_looting
   rtr_looting_knight
   }
}

rtr_looting = {
  trigger = { scope:barony.title_province = { any_character_in_location = { has_variable = rtr_assigned_army } } }
  effect = {
 every_player = {
 limit = { is_ruler = no has_variable = rtr_assigned_army location = scope:barony.title_province }
 add_to_list = recruits_list
 }
 every_in_list = {
  list = recruits_list
  send_interface_toast = {
				title = loot_title
				custom_tooltip = loot_tooltip
				add_gold = 3
			}
 }
}
}
rtr_looting_knight = {
  trigger = { 
   any_knight = { is_ai = no }
  }
  effect = {

	  root = { every_knight = {
	  # cannot get location, for player knight locations are never updated (engine limitation)
	  send_interface_toast = {
				title = loot_title
				custom_tooltip = loot_tooltip
				add_gold = 3
			}
	  
	  }
	  }
	
	}
}

rtr_army_arrival = {
 effect = { 
   root.var:home = { save_scope_as = home }
   current_travel_plan = {
   delay_travel_plan = {
	years = 100
  }
  }
 }
}
on_travel_plan_cancel = {
 on_actions = {
  rtr_prevent_desertion
 }
}

on_combat_end_loser = {
	on_actions = {
	 rtr_combat_defeat1
	 rtr_combat_defeat2
	}
}

on_combat_end_winner = {
  on_actions = {
    rtr_combat_win
  }
}

rtr_combat_defeat1 = {
  trigger = { scope:wipe = no any_player = { has_variable = rtr_assigned_army } }
  effect = {
    combat.location = { save_scope_as = location }
	scope:location = { every_army_in_location = { add_to_list = army_list } }
	random_in_list = {
	 list = army_list
	 limit = { army_owner = root.side_primary_participant has_variable = has_recruits }
	 save_scope_as = losing_army
	}
	every_player = {
 limit = { is_ruler = no has_variable = rtr_assigned_army var:rtr_assigned_army = scope:losing_army }
    trigger_event = rtr.0119
   }
  }
}
rtr_combat_defeat2 = {
  trigger = { scope:wipe = yes any_player = { has_variable = rtr_assigned_army } }
  effect = {
    combat.location = { save_scope_as = location }
	every_player = {
 limit = { is_ruler = no has_variable = rtr_assigned_army location = scope:location }
    death = { death_reason = death_battle }
   }
  }
}

rtr_combat_win = {
 trigger = { any_player = { has_variable = rtr_assigned_army } }
 effect = {
    combat.location = { save_scope_as = location }
	scope:location = { every_army_in_location = { add_to_list = army_list } }
	random_in_list = {
	 list = army_list
	 limit = { army_owner = root.side_primary_participant has_variable = has_recruits }
	 save_scope_as = winning_army
	}
	every_player = {
 limit = { is_ruler = no has_variable = rtr_assigned_army var:rtr_assigned_army = scope:winning_army }
    trigger_event = rtr.0121
   }
  }
}

rtr_prevent_desertion = {
 trigger = { has_variable = rtr_assigned_army }
 effect = {
   current_travel_plan = {
   delay_travel_plan = {
	years = 100
  }
 }
 }
}


on_war_won_defender = {
 on_actions = {
  rtr_free_from_duty
 }
}
on_war_won_attacker = {
 on_actions = {
  rtr_free_from_duty
 }
}

on_war_invalidated = {
 on_actions = {
  rtr_free_from_duty
 }
}
on_war_transferred = {
 on_actions = {
  rtr_free_from_duty
 }
}

rtr_free_from_duty = {
  trigger = { any_player = { has_variable = rtr_assigned_army } }
  effect = {
   every_player = { limit = { has_variable = patron var:patron = { is_at_war = no } } trigger_event = rtr.0120 free_from_duty = yes }
  }
}