﻿# Commander+ integration helper effects.
# These helpers keep vanilla scripted-effect overrides as small as possible.

# Apply one of four XP-tiered character modifiers to the current character scope.
# Required parameters: TRAIT, MODIFIER_0..3, DURATION.
cmd_plus_apply_tiered_character_modifier_effect = {
	remove_character_modifier = $MODIFIER_0$
	remove_character_modifier = $MODIFIER_1$
	remove_character_modifier = $MODIFIER_2$
	remove_character_modifier = $MODIFIER_3$

	if = {
		limit = { has_trait_xp = { trait = $TRAIT$ value >= cmd_plus_trait_tier_3_xp } }
		add_character_modifier = { modifier = $MODIFIER_3$ days = $DURATION$ }
	}
	else_if = {
		limit = { has_trait_xp = { trait = $TRAIT$ value >= cmd_plus_trait_tier_2_xp } }
		add_character_modifier = { modifier = $MODIFIER_2$ days = $DURATION$ }
	}
	else_if = {
		limit = { has_trait_xp = { trait = $TRAIT$ value >= cmd_plus_trait_tier_1_xp } }
		add_character_modifier = { modifier = $MODIFIER_1$ days = $DURATION$ }
	}
	else = {
		add_character_modifier = { modifier = $MODIFIER_0$ days = $DURATION$ }
	}
}

# Refresh the Military Savior combat tier on the current commander.
# Called both when an army joins a battle and from the monthly in-combat refresh,
# so battles lasting longer than the modifier duration do not lose the bonus.
cmd_plus_refresh_military_savior_modifier_effect = {
	if = {
		limit = { has_trait = military_savior }
		cmd_plus_apply_tiered_character_modifier_effect = {
			TRAIT = military_savior
			MODIFIER_0 = cmd_plus_savior_modifier
			MODIFIER_1 = cmd_plus_savior_modifier_1
			MODIFIER_2 = cmd_plus_savior_modifier_2
			MODIFIER_3 = cmd_plus_savior_modifier_3
			DURATION = cmd_plus_army_state_modifier_duration_days
		}
	}
}

# Apply the current Bannerlord war-start tier to this character.
cmd_plus_apply_bannerlord_war_start_modifier_effect = {
	cmd_plus_apply_tiered_character_modifier_effect = {
		TRAIT = bannerlord
		MODIFIER_0 = cmd_plus_war_started_modifier
		MODIFIER_1 = cmd_plus_war_started_modifier_1
		MODIFIER_2 = cmd_plus_war_started_modifier_2
		MODIFIER_3 = cmd_plus_war_started_modifier_3
		DURATION = cmd_plus_bannerlord_war_start_duration_days
	}
}

# Refresh the Levies Specialist tier after cmd_plus_levies_specialist_var has been calculated.
cmd_plus_refresh_levies_specialist_modifier_effect = {
	if = {
		limit = { var:cmd_plus_levies_specialist_var = 0 }
		cmd_plus_apply_tiered_character_modifier_effect = {
			TRAIT = levies_specialist
			MODIFIER_0 = levies_specialist_modifier
			MODIFIER_1 = levies_specialist_modifier_1
			MODIFIER_2 = levies_specialist_modifier_2
			MODIFIER_3 = levies_specialist_modifier_3
			DURATION = cmd_plus_army_state_modifier_duration_days
		}
	}
	else = {
		remove_character_modifier = levies_specialist_modifier
		remove_character_modifier = levies_specialist_modifier_1
		remove_character_modifier = levies_specialist_modifier_2
		remove_character_modifier = levies_specialist_modifier_3
	}
}

# Initialize traits whose state depends on the whole combat side for all armies
# present at battle creation. Reinforcements are handled separately by
# on_combat_unit_join_side.
# ROOT = Combat.
cmd_plus_initialize_starting_combat_armies_effect = {
	save_temporary_scope_as = cmd_plus_starting_combat
	location = {
		every_army_in_location = {
			limit = {
				involved_combat_side.combat ?= scope:cmd_plus_starting_combat
			}
			army_commander ?= {
				cmd_plus_refresh_military_savior_modifier_effect = yes
			}
			cmd_plus_refresh_levies_specialist_for_army_effect = yes
		}
	}
}

# Recalculate Levies Specialist from this exact army only. This is used after
# combat ends, when the temporary combat-side composition must no longer affect
# the commander's persistent army-state modifier.
cmd_plus_refresh_levies_specialist_for_own_army_effect = {
	if = {
		limit = {
			exists = army_commander
			army_commander = {
				is_alive = yes
				has_trait = levies_specialist
			}
		}
		army_commander = {
			cmd_plus_cleanup_legacy_transient_state_effect = yes
			set_variable = { name = cmd_plus_levies_specialist_var value = 0 }
			save_temporary_scope_as = cmd_plus_post_combat_levies_commander
		}
		if = {
			limit = {
				any_army_maa_regiment = { NOT = { is_unit_type = siege_weapon } }
			}
			scope:cmd_plus_post_combat_levies_commander = {
				set_variable = { name = cmd_plus_levies_specialist_var value = 1 }
			}
		}
		scope:cmd_plus_post_combat_levies_commander = {
			cmd_plus_refresh_levies_specialist_modifier_effect = yes
			remove_variable = cmd_plus_levies_specialist_var
		}
	}
}

# Clear battle-only state for every commander on the current combat side.
# This is called from both on_combat_end_winner and on_combat_end_loser; unlike
# on_commander_combat_finished, neither parent hook filters out the winning side.
cmd_plus_post_combat_side_cleanup_effect = {
	every_side_commander = {
		remove_character_modifier = cmd_plus_savior_modifier
		remove_character_modifier = cmd_plus_savior_modifier_1
		remove_character_modifier = cmd_plus_savior_modifier_2
		remove_character_modifier = cmd_plus_savior_modifier_3

		if = {
			limit = {
				has_trait = levies_specialist
				exists = commanding_army
			}
			commanding_army = {
				cmd_plus_refresh_levies_specialist_for_own_army_effect = yes
			}
		}
		else = {
			remove_character_modifier = levies_specialist_modifier
			remove_character_modifier = levies_specialist_modifier_1
			remove_character_modifier = levies_specialist_modifier_2
			remove_character_modifier = levies_specialist_modifier_3
		}
	}
}

# Recalculate Levies Specialist for the current army scope. In combat, the
# condition is evaluated against every army on this exact combat side, not
# against broad diplomatic alliances or only the army that most recently joined.
cmd_plus_refresh_levies_specialist_for_army_effect = {
	if = {
		limit = {
			exists = army_commander
			army_commander = {
				is_alive = yes
				has_trait = levies_specialist
			}
		}
		army_commander = {
			cmd_plus_cleanup_legacy_transient_state_effect = yes
			set_variable = { name = cmd_plus_levies_specialist_var value = 0 }
			save_temporary_scope_as = cmd_plus_current_levies_commander
		}

		# Outside combat, only this commander's own army composition matters.
		if = {
			limit = {
				any_army_maa_regiment = { NOT = { is_unit_type = siege_weapon } }
			}
			scope:cmd_plus_current_levies_commander = {
				set_variable = { name = cmd_plus_levies_specialist_var value = 1 }
			}
		}

		# In combat, any non-siege MAA regiment on the same exact side disables it.
		if = {
			limit = {
				exists = involved_combat_side
			}
			involved_combat_side = { save_temporary_scope_as = cmd_plus_levies_combat_side }
			location ?= {
				every_army_in_location = {
					limit = {
						involved_combat_side ?= { this = scope:cmd_plus_levies_combat_side }
						any_army_maa_regiment = { NOT = { is_unit_type = siege_weapon } }
					}
					scope:cmd_plus_current_levies_commander = {
						set_variable = { name = cmd_plus_levies_specialist_var value = 1 }
					}
				}
			}
		}

		scope:cmd_plus_current_levies_commander = {
			cmd_plus_refresh_levies_specialist_modifier_effect = yes
			remove_variable = cmd_plus_levies_specialist_var
		}
	}
}

cmd_plus_give_random_commander_trait_effect = {
	random_list = {
		100 = {
			trigger = {
				NOT = { has_trait = mapper }
			}
			add_trait = mapper
		}
		100 = {
			trigger = {
				NOT = { has_trait = observant_pathfinder }
			}
			add_trait = observant_pathfinder
		}
		100 = {
			trigger = {
				NOT = { has_trait = respected_leader }
			}
			add_trait = respected_leader
		}
		100 = {
			trigger = {
				NOT = { has_trait = sea_raider }
			}
			add_trait = sea_raider
		}
		100 = {
			trigger = {
				NOT = { has_trait = seasoned_rover }
			}
			add_trait = seasoned_rover
		}
		100 = {
			trigger = {
				NOT = { has_trait = resolute_localist }
			}
			add_trait = resolute_localist
		}
		100 = {
			trigger = {
				NOT = { has_trait = relentless_pursuer }
			}
			add_trait = relentless_pursuer
		}
		100 = {
			trigger = {
				NOT = { has_trait = infiltration_specialist }
			}
			add_trait = infiltration_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = battlefield_medic }
			}
			add_trait = battlefield_medic
		}
		100 = {
			trigger = {
				NOT = { has_trait = dauntless_leader }
			}
			add_trait = dauntless_leader
		}
		100 = {
			trigger = {
				NOT = { has_trait = crafty_commander }
			}
			add_trait = crafty_commander
		}
		100 = {
			trigger = {
				NOT = { has_trait = cautious_planner }
			}
			add_trait = cautious_planner
		}
		100 = {
			trigger = {
				NOT = { has_trait = bannerlord }
			}
			add_trait = bannerlord
		}
		100 = {
			trigger = {
				NOT = { has_trait = focus_specialist }
			}
			add_trait = focus_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = master_ambusher }
			}
			add_trait = master_ambusher
		}
		100 = {
			trigger = {
				NOT = { has_trait = navigator }
			}
			add_trait = navigator
		}
		100 = {
			trigger = {
				NOT = { has_trait = zealot }
			}
			add_trait = zealot
		}
		100 = {
			trigger = {
				NOT = { has_trait = patriot }
			}
			add_trait = patriot
		}
		100 = {
			trigger = {
				NOT = { has_trait = military_savior }
			}
			add_trait = military_savior
		}
		100 = {
			trigger = {
				NOT = { has_trait = combined_arms }
			}
			add_trait = combined_arms
		}
		100 = {
			trigger = {
				NOT = { has_trait = levies_specialist }
			}
			add_trait = levies_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = skirmishers_specialist }
			}
			add_trait = skirmishers_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = archers_specialist }
			}
			add_trait = archers_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = heavy_cav_specialist }
			}
			add_trait = heavy_cav_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = light_cav_specialist }
			}
			add_trait = light_cav_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = heavy_infantry_specialist }
			}
			add_trait = heavy_infantry_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = archer_cav_specialist }
			}
			add_trait = archer_cav_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = pikemen_specialist }
			}
			add_trait = pikemen_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = knights_specialist }
			}
			add_trait = knights_specialist
		}
	}
}

cmd_plus_give_random_commander_trait_from_educator_effect = {
	random_list = {
		100 = {
			trigger = {
				NOT = { has_trait = mapper }
				scope:educator = { has_trait = mapper }
			}
			add_trait = mapper
		}
		100 = {
			trigger = {
				NOT = { has_trait = observant_pathfinder }
				scope:educator = { has_trait = observant_pathfinder }
			}
			add_trait = observant_pathfinder
		}
		100 = {
			trigger = {
				NOT = { has_trait = respected_leader }
				scope:educator = { has_trait = respected_leader }
			}
			add_trait = respected_leader
		}
		100 = {
			trigger = {
				NOT = { has_trait = sea_raider }
				scope:educator = { has_trait = sea_raider }
			}
			add_trait = sea_raider
		}
		100 = {
			trigger = {
				NOT = { has_trait = seasoned_rover }
				scope:educator = { has_trait = seasoned_rover }
			}
			add_trait = seasoned_rover
		}
		100 = {
			trigger = {
				NOT = { has_trait = resolute_localist }
				scope:educator = { has_trait = resolute_localist }
			}
			add_trait = resolute_localist
		}
		100 = {
			trigger = {
				NOT = { has_trait = relentless_pursuer }
				scope:educator = { has_trait = relentless_pursuer }
			}
			add_trait = relentless_pursuer
		}
		100 = {
			trigger = {
				NOT = { has_trait = infiltration_specialist }
				scope:educator = { has_trait = infiltration_specialist }
			}
			add_trait = infiltration_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = battlefield_medic }
				scope:educator = { has_trait = battlefield_medic }
			}
			add_trait = battlefield_medic
		}
		100 = {
			trigger = {
				NOT = { has_trait = dauntless_leader }
				scope:educator = { has_trait = dauntless_leader }
			}
			add_trait = dauntless_leader
		}
		100 = {
			trigger = {
				NOT = { has_trait = crafty_commander }
				scope:educator = { has_trait = crafty_commander }
			}
			add_trait = crafty_commander
		}
		100 = {
			trigger = {
				NOT = { has_trait = cautious_planner }
				scope:educator = { has_trait = cautious_planner }
			}
			add_trait = cautious_planner
		}
		100 = {
			trigger = {
				NOT = { has_trait = bannerlord }
				scope:educator = { has_trait = bannerlord }
			}
			add_trait = bannerlord
		}
		100 = {
			trigger = {
				NOT = { has_trait = focus_specialist }
				scope:educator = { has_trait = focus_specialist }
			}
			add_trait = focus_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = master_ambusher }
				scope:educator = { has_trait = master_ambusher }
			}
			add_trait = master_ambusher
		}
		100 = {
			trigger = {
				NOT = { has_trait = navigator }
				scope:educator = { has_trait = navigator }
			}
			add_trait = navigator
		}
		100 = {
			trigger = {
				NOT = { has_trait = zealot }
				scope:educator = { has_trait = zealot }
			}
			add_trait = zealot
		}
		100 = {
			trigger = {
				NOT = { has_trait = patriot }
				scope:educator = { has_trait = patriot }
			}
			add_trait = patriot
		}
		100 = {
			trigger = {
				NOT = { has_trait = military_savior }
				scope:educator = { has_trait = military_savior }
			}
			add_trait = military_savior
		}
		100 = {
			trigger = {
				NOT = { has_trait = combined_arms }
				scope:educator = { has_trait = combined_arms }
			}
			add_trait = combined_arms
		}
		100 = {
			trigger = {
				NOT = { has_trait = levies_specialist }
				scope:educator = { has_trait = levies_specialist }
			}
			add_trait = levies_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = skirmishers_specialist }
				scope:educator = { has_trait = skirmishers_specialist }
			}
			add_trait = skirmishers_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = archers_specialist }
				scope:educator = { has_trait = archers_specialist }
			}
			add_trait = archers_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = heavy_cav_specialist }
				scope:educator = { has_trait = heavy_cav_specialist }
			}
			add_trait = heavy_cav_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = light_cav_specialist }
				scope:educator = { has_trait = light_cav_specialist }
			}
			add_trait = light_cav_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = heavy_infantry_specialist }
				scope:educator = { has_trait = heavy_infantry_specialist }
			}
			add_trait = heavy_infantry_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = archer_cav_specialist }
				scope:educator = { has_trait = archer_cav_specialist }
			}
			add_trait = archer_cav_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = pikemen_specialist }
				scope:educator = { has_trait = pikemen_specialist }
			}
			add_trait = pikemen_specialist
		}
		100 = {
			trigger = {
				NOT = { has_trait = knights_specialist }
				scope:educator = { has_trait = knights_specialist }
			}
			add_trait = knights_specialist
		}
	}
}

cmd_plus_set_random_commander_trait_being_taught_effect = {
	random_list = {
		1 = {
			trigger = {
				NOT = { has_trait = mapper }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:mapper
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = observant_pathfinder }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:observant_pathfinder
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = respected_leader }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:respected_leader
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = sea_raider }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:sea_raider
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = seasoned_rover }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:seasoned_rover
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = resolute_localist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:resolute_localist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = relentless_pursuer }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:relentless_pursuer
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = infiltration_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:infiltration_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = battlefield_medic }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:battlefield_medic
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = dauntless_leader }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:dauntless_leader
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = crafty_commander }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:crafty_commander
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = cautious_planner }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:cautious_planner
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = bannerlord }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:bannerlord
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = focus_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:focus_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = master_ambusher }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:master_ambusher
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = navigator }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:navigator
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = zealot }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:zealot
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = patriot }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:patriot
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = military_savior }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:military_savior
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = combined_arms }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:combined_arms
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = levies_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:levies_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = skirmishers_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:skirmishers_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = archers_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:archers_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = heavy_cav_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:heavy_cav_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = light_cav_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:light_cav_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = heavy_infantry_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:heavy_infantry_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = archer_cav_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:archer_cav_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = pikemen_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:pikemen_specialist
			}
		}
		1 = {
			trigger = {
				NOT = { has_trait = knights_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:knights_specialist
			}
		}
	}
}

cmd_plus_randomize_commander_trait_to_be_taught_effect = {
	random_list = {
		100 = {
			trigger = {
				NOT = { has_trait = mapper }
				scope:teacher = { has_trait = mapper }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:mapper
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = observant_pathfinder }
				scope:teacher = { has_trait = observant_pathfinder }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:observant_pathfinder
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = respected_leader }
				scope:teacher = { has_trait = respected_leader }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:respected_leader
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = sea_raider }
				scope:teacher = { has_trait = sea_raider }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:sea_raider
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = seasoned_rover }
				scope:teacher = { has_trait = seasoned_rover }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:seasoned_rover
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = resolute_localist }
				scope:teacher = { has_trait = resolute_localist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:resolute_localist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = relentless_pursuer }
				scope:teacher = { has_trait = relentless_pursuer }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:relentless_pursuer
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = infiltration_specialist }
				scope:teacher = { has_trait = infiltration_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:infiltration_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = battlefield_medic }
				scope:teacher = { has_trait = battlefield_medic }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:battlefield_medic
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = dauntless_leader }
				scope:teacher = { has_trait = dauntless_leader }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:dauntless_leader
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = crafty_commander }
				scope:teacher = { has_trait = crafty_commander }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:crafty_commander
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = cautious_planner }
				scope:teacher = { has_trait = cautious_planner }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:cautious_planner
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = bannerlord }
				scope:teacher = { has_trait = bannerlord }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:bannerlord
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = focus_specialist }
				scope:teacher = { has_trait = focus_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:focus_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = master_ambusher }
				scope:teacher = { has_trait = master_ambusher }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:master_ambusher
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = navigator }
				scope:teacher = { has_trait = navigator }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:navigator
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = zealot }
				scope:teacher = { has_trait = zealot }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:zealot
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = patriot }
				scope:teacher = { has_trait = patriot }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:patriot
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = military_savior }
				scope:teacher = { has_trait = military_savior }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:military_savior
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = combined_arms }
				scope:teacher = { has_trait = combined_arms }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:combined_arms
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = levies_specialist }
				scope:teacher = { has_trait = levies_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:levies_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = skirmishers_specialist }
				scope:teacher = { has_trait = skirmishers_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:skirmishers_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = archers_specialist }
				scope:teacher = { has_trait = archers_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:archers_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = heavy_cav_specialist }
				scope:teacher = { has_trait = heavy_cav_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:heavy_cav_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = light_cav_specialist }
				scope:teacher = { has_trait = light_cav_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:light_cav_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = heavy_infantry_specialist }
				scope:teacher = { has_trait = heavy_infantry_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:heavy_infantry_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = archer_cav_specialist }
				scope:teacher = { has_trait = archer_cav_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:archer_cav_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = pikemen_specialist }
				scope:teacher = { has_trait = pikemen_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:pikemen_specialist
			}
		}
		100 = {
			trigger = {
				NOT = { has_trait = knights_specialist }
				scope:teacher = { has_trait = knights_specialist }
			}
			set_variable = {
				name = commander_trait_being_taught
				value = flag:knights_specialist
			}
		}
	}
}

cmd_plus_select_ct_for_teacher_to_teach_student_effect = {
	random_list = {
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = mapper }
				$STUDENT$ = { NOT = { has_trait = mapper } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:mapper
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = observant_pathfinder }
				$STUDENT$ = { NOT = { has_trait = observant_pathfinder } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:observant_pathfinder
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = respected_leader }
				$STUDENT$ = { NOT = { has_trait = respected_leader } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:respected_leader
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = sea_raider }
				$STUDENT$ = { NOT = { has_trait = sea_raider } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:sea_raider
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = seasoned_rover }
				$STUDENT$ = { NOT = { has_trait = seasoned_rover } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:seasoned_rover
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = resolute_localist }
				$STUDENT$ = { NOT = { has_trait = resolute_localist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:resolute_localist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = relentless_pursuer }
				$STUDENT$ = { NOT = { has_trait = relentless_pursuer } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:relentless_pursuer
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = infiltration_specialist }
				$STUDENT$ = { NOT = { has_trait = infiltration_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:infiltration_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = battlefield_medic }
				$STUDENT$ = { NOT = { has_trait = battlefield_medic } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:battlefield_medic
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = dauntless_leader }
				$STUDENT$ = { NOT = { has_trait = dauntless_leader } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:dauntless_leader
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = crafty_commander }
				$STUDENT$ = { NOT = { has_trait = crafty_commander } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:crafty_commander
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = cautious_planner }
				$STUDENT$ = { NOT = { has_trait = cautious_planner } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:cautious_planner
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = bannerlord }
				$STUDENT$ = { NOT = { has_trait = bannerlord } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:bannerlord
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = focus_specialist }
				$STUDENT$ = { NOT = { has_trait = focus_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:focus_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = master_ambusher }
				$STUDENT$ = { NOT = { has_trait = master_ambusher } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:master_ambusher
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = navigator }
				$STUDENT$ = { NOT = { has_trait = navigator } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:navigator
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = zealot }
				$STUDENT$ = { NOT = { has_trait = zealot } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:zealot
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = patriot }
				$STUDENT$ = { NOT = { has_trait = patriot } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:patriot
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = military_savior }
				$STUDENT$ = { NOT = { has_trait = military_savior } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:military_savior
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = combined_arms }
				$STUDENT$ = { NOT = { has_trait = combined_arms } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:combined_arms
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = levies_specialist }
				$STUDENT$ = { NOT = { has_trait = levies_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:levies_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = skirmishers_specialist }
				$STUDENT$ = { NOT = { has_trait = skirmishers_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:skirmishers_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = archers_specialist }
				$STUDENT$ = { NOT = { has_trait = archers_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:archers_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = heavy_cav_specialist }
				$STUDENT$ = { NOT = { has_trait = heavy_cav_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:heavy_cav_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = light_cav_specialist }
				$STUDENT$ = { NOT = { has_trait = light_cav_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:light_cav_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = heavy_infantry_specialist }
				$STUDENT$ = { NOT = { has_trait = heavy_infantry_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:heavy_infantry_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = archer_cav_specialist }
				$STUDENT$ = { NOT = { has_trait = archer_cav_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:archer_cav_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = pikemen_specialist }
				$STUDENT$ = { NOT = { has_trait = pikemen_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:pikemen_specialist
				}
			}
		}
		1 = {
			trigger = {
				$TEACHER$ = { has_trait = knights_specialist }
				$STUDENT$ = { NOT = { has_trait = knights_specialist } }
			}
			$STUDENT$ = {
				set_variable = {
					name = commander_trait_being_taught
					value = flag:knights_specialist
				}
			}
		}
	}
}

cmd_plus_learn_commander_trait_success_effect = {
	if = {
		limit = { var:commander_trait_being_taught = flag:mapper }
		add_trait = mapper
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:observant_pathfinder }
		add_trait = observant_pathfinder
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:respected_leader }
		add_trait = respected_leader
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:sea_raider }
		add_trait = sea_raider
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:seasoned_rover }
		add_trait = seasoned_rover
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:resolute_localist }
		add_trait = resolute_localist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:relentless_pursuer }
		add_trait = relentless_pursuer
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:infiltration_specialist }
		add_trait = infiltration_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:battlefield_medic }
		add_trait = battlefield_medic
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:dauntless_leader }
		add_trait = dauntless_leader
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:crafty_commander }
		add_trait = crafty_commander
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:cautious_planner }
		add_trait = cautious_planner
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:bannerlord }
		add_trait = bannerlord
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:focus_specialist }
		add_trait = focus_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:master_ambusher }
		add_trait = master_ambusher
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:navigator }
		add_trait = navigator
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:zealot }
		add_trait = zealot
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:patriot }
		add_trait = patriot
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:military_savior }
		add_trait = military_savior
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:combined_arms }
		add_trait = combined_arms
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:levies_specialist }
		add_trait = levies_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:skirmishers_specialist }
		add_trait = skirmishers_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:archers_specialist }
		add_trait = archers_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:heavy_cav_specialist }
		add_trait = heavy_cav_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:light_cav_specialist }
		add_trait = light_cav_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:heavy_infantry_specialist }
		add_trait = heavy_infantry_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:archer_cav_specialist }
		add_trait = archer_cav_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:pikemen_specialist }
		add_trait = pikemen_specialist
	}
	else_if = {
		limit = { var:commander_trait_being_taught = flag:knights_specialist }
		add_trait = knights_specialist
	}
}

cmd_plus_commander_trait_lvling_ai_effect = {
	random_list = {
		1 = {
			trigger = { has_trait = mapper }
			add_trait_xp = {
				trait = mapper
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(mapper)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = observant_pathfinder }
			add_trait_xp = {
				trait = observant_pathfinder
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(observant_pathfinder)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = respected_leader }
			add_trait_xp = {
				trait = respected_leader
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(respected_leader)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = sea_raider }
			add_trait_xp = {
				trait = sea_raider
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(sea_raider)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = seasoned_rover }
			add_trait_xp = {
				trait = seasoned_rover
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(seasoned_rover)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = resolute_localist }
			add_trait_xp = {
				trait = resolute_localist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(resolute_localist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = relentless_pursuer }
			add_trait_xp = {
				trait = relentless_pursuer
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(relentless_pursuer)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = infiltration_specialist }
			add_trait_xp = {
				trait = infiltration_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(infiltration_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = battlefield_medic }
			add_trait_xp = {
				trait = battlefield_medic
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(battlefield_medic)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = dauntless_leader }
			add_trait_xp = {
				trait = dauntless_leader
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(dauntless_leader)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = crafty_commander }
			add_trait_xp = {
				trait = crafty_commander
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(crafty_commander)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = cautious_planner }
			add_trait_xp = {
				trait = cautious_planner
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(cautious_planner)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = bannerlord }
			add_trait_xp = {
				trait = bannerlord
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(bannerlord)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = focus_specialist }
			add_trait_xp = {
				trait = focus_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(focus_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = master_ambusher }
			add_trait_xp = {
				trait = master_ambusher
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(master_ambusher)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = navigator }
			add_trait_xp = {
				trait = navigator
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(navigator)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = zealot }
			add_trait_xp = {
				trait = zealot
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(zealot)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = patriot }
			add_trait_xp = {
				trait = patriot
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(patriot)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = military_savior }
			add_trait_xp = {
				trait = military_savior
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(military_savior)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = combined_arms }
			add_trait_xp = {
				trait = combined_arms
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(combined_arms)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = levies_specialist }
			add_trait_xp = {
				trait = levies_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(levies_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = skirmishers_specialist }
			add_trait_xp = {
				trait = skirmishers_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(skirmishers_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = archers_specialist }
			add_trait_xp = {
				trait = archers_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(archers_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = heavy_cav_specialist }
			add_trait_xp = {
				trait = heavy_cav_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(heavy_cav_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = light_cav_specialist }
			add_trait_xp = {
				trait = light_cav_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(light_cav_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = heavy_infantry_specialist }
			add_trait_xp = {
				trait = heavy_infantry_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(heavy_infantry_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = archer_cav_specialist }
			add_trait_xp = {
				trait = archer_cav_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(archer_cav_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = pikemen_specialist }
			add_trait_xp = {
				trait = pikemen_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(pikemen_specialist)"
					divide = 100
				}
			}
		}
		1 = {
			trigger = { has_trait = knights_specialist }
			add_trait_xp = {
				trait = knights_specialist
				value = {
					value = 100
					subtract = "scope:root_scope.has_trait_xp(knights_specialist)"
					divide = 100
				}
			}
		}
	}
}

cmd_plus_commander_trait_leveling_leveling_effect = {
	# Commander+ 1.20 - contextual XP pass
	# Territory-dependent XP must follow the owner of this commander's exact army,
	# not the combat side's primary participant (allied armies can have another owner).
	save_temporary_scope_as = cmd_plus_trait_xp_commander
	commanding_army ?= {
		army_owner = {
			save_temporary_scope_as = cmd_plus_trait_xp_army_owner
			save_temporary_scope_as = cmd_plus_trait_xp_friendly_realm_owner
			top_liege ?= { save_temporary_scope_as = cmd_plus_trait_xp_friendly_realm_owner }
		}
	}

		# Campaign/navigation traits: slower, but still progress through relevant campaigns.
		if = { # mapper - learns from campaigning as an active commander.
			limit = {
				has_trait = mapper
				is_commanding_army = yes
			}
			add_trait_xp = {
				trait = mapper
				value = {
					value = $XP$
					multiply = 0.5
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # observant pathfinder - open terrain specialist.
			limit = {
				has_trait = observant_pathfinder
				location = {
					OR = {
						terrain = farmlands
						terrain = plains
						terrain = steppe
					}
				}
			}
			add_trait_xp = {
				trait = observant_pathfinder
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # respected leader - grows through difficult battles, not routine farming.
			limit = {
				has_trait = respected_leader
				root = { troops_ratio < 1.25 }
			}
			add_trait_xp = {
				trait = respected_leader
				value = {
					value = $XP$
					multiply = 0.75
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # sea raider - coastal and riverine warfare.
			limit = {
				has_trait = sea_raider
				location = {
					OR = {
						is_coastal = yes
						is_riverside_province = yes
					}
				}
			}
			add_trait_xp = {
				trait = sea_raider
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
	
		# Home/away territory traits.
		if = { # seasoned rover - fighting away from friendly territory.
			limit = {
				has_trait = seasoned_rover
				exists = scope:cmd_plus_trait_xp_friendly_realm_owner
				exists = location.barony.barony_controller
				NOT = {
					location.barony.barony_controller = {
						OR = {
							this = scope:cmd_plus_trait_xp_friendly_realm_owner
							is_vassal_or_below_of = scope:cmd_plus_trait_xp_friendly_realm_owner
						}
					}
				}
			}
			add_trait_xp = {
				trait = seasoned_rover
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # resolute localist - defending or fighting in friendly territory.
			limit = {
				has_trait = resolute_localist
				exists = scope:cmd_plus_trait_xp_friendly_realm_owner
				exists = location.barony.barony_controller
				location.barony.barony_controller = {
					OR = {
						this = scope:cmd_plus_trait_xp_friendly_realm_owner
						is_vassal_or_below_of = scope:cmd_plus_trait_xp_friendly_realm_owner
					}
				}
			}
			add_trait_xp = {
				trait = resolute_localist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
	
		# Tactical behavior traits.
		if = { # relentless pursuer - best learned when the side is not badly outnumbered.
			limit = {
				has_trait = relentless_pursuer
				root = { troops_ratio > 0.9 }
			}
			add_trait_xp = {
				trait = relentless_pursuer
				value = {
					value = $XP$
					multiply = 0.75
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # infiltration specialist - irregular tactics in harder fights.
			limit = {
				has_trait = infiltration_specialist
				root = { troops_ratio < 1.25 }
			}
			add_trait_xp = {
				trait = infiltration_specialist
				value = {
					value = $XP$
					multiply = 0.75
					add = { value = intrigue divide = 25 }
				}
			}
		}
		if = { # battlefield medic - learns from battlefield experience, but slower than hard specialists.
			limit = { has_trait = battlefield_medic }
			add_trait_xp = {
				trait = battlefield_medic
				value = {
					value = $XP$
					multiply = 0.5
					add = { value = martial divide = 30 }
				}
			}
		}
		if = { # dauntless leader - outnumbered or near-even battles.
			limit = {
				has_trait = dauntless_leader
				root = { troops_ratio < 0.95 }
			}
			add_trait_xp = {
				trait = dauntless_leader
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # crafty commander - supply/planning in enemy territory.
			limit = {
				has_trait = crafty_commander
				exists = scope:cmd_plus_trait_xp_friendly_realm_owner
				exists = location.barony.barony_controller
				NOT = {
					location.barony.barony_controller = {
						OR = {
							this = scope:cmd_plus_trait_xp_friendly_realm_owner
							is_vassal_or_below_of = scope:cmd_plus_trait_xp_friendly_realm_owner
						}
					}
				}
			}
			add_trait_xp = {
				trait = crafty_commander
				value = {
					value = $XP$
					multiply = 0.75
					add = { value = stewardship divide = 25 }
				}
			}
		}
		if = { # cautious planner - defensive battles.
			limit = {
				has_trait = cautious_planner
				root = { is_combat_side_attacker = no }
			}
			add_trait_xp = {
				trait = cautious_planner
				value = {
					value = $XP$
					multiply = 1.0
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # bannerlord - offensive war leadership.
			limit = {
				has_trait = bannerlord
				root = { is_combat_side_attacker = yes }
			}
			add_trait_xp = {
				trait = bannerlord
				value = {
					value = $XP$
					multiply = 1.0
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # focus specialist - siege warfare or battle in an enemy ruler's capital county.
			limit = {
				has_trait = focus_specialist
				OR = {
					commanding_army ?= { any_army_maa_regiment = { is_unit_type = siege_weapon } }
					AND = {
						exists = scope:cmd_plus_trait_xp_army_owner
						exists = location.county.holder
						scope:cmd_plus_trait_xp_army_owner = { is_at_war_with = location.county.holder }
						location.county = location.county.holder.primary_title.title_capital_county
					}
				}
			}
			add_trait_xp = {
				trait = focus_specialist
				value = {
					value = $XP$
					multiply = 1.0
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # master ambusher - rough, forested or concealed terrain.
			limit = {
				has_trait = master_ambusher
				location = {
					OR = {
						terrain = forest
						terrain = taiga
						terrain = jungle
						terrain = hills
						terrain = mountains
						terrain = wetlands
					}
				}
			}
			add_trait_xp = {
				trait = master_ambusher
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # navigator - coastal and riverine maneuvering.
			limit = {
				has_trait = navigator
				location = {
					OR = {
						is_coastal = yes
						is_riverside_province = yes
					}
				}
			}
			add_trait_xp = {
				trait = navigator
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # zealot - fighting commanders of another faith.
			limit = {
				has_trait = zealot
				exists = root.enemy_side.side_commander
				root.enemy_side.side_commander.religion != scope:cmd_plus_trait_xp_commander.religion
			}
			add_trait_xp = {
				trait = zealot
				value = {
					value = $XP$
					multiply = 1.0
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # patriot - fighting in friendly territory.
			limit = {
				has_trait = patriot
				exists = scope:cmd_plus_trait_xp_friendly_realm_owner
				exists = location.barony.barony_controller
				location.barony.barony_controller = {
					OR = {
						this = scope:cmd_plus_trait_xp_friendly_realm_owner
						is_vassal_or_below_of = scope:cmd_plus_trait_xp_friendly_realm_owner
					}
				}
			}
			add_trait_xp = {
				trait = patriot
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # military savior - only in genuinely dangerous battles.
			limit = {
				has_trait = military_savior
				root = { troops_ratio < 0.8 }
			}
			add_trait_xp = {
				trait = military_savior
				value = {
					value = $XP$
					multiply = 1.5
					add = { value = martial divide = 25 }
				}
			}
		}
	
		# Army-composition traits.
		if = { # combined arms - needs a real non-siege MAA component.
			limit = {
				has_trait = combined_arms
				commanding_army ?= {
					any_army_maa_regiment = {
						count >= 2
						NOT = { is_unit_type = siege_weapon }
					}
				}
			}
			add_trait_xp = {
				trait = combined_arms
				value = {
					value = $XP$
					multiply = 1.0
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # levies specialist - only when this exact combat side has no non-siege MAA.
			limit = { has_trait = levies_specialist }
			set_variable = { name = cmd_plus_levies_xp_side_has_maa value = 0 }
			save_temporary_scope_as = cmd_plus_levies_xp_commander
			root = {
				combat.location = {
					every_army_in_location = {
						limit = {
							involved_combat_side ?= { this = root }
							any_army_maa_regiment = {
								NOT = { is_unit_type = siege_weapon }
							}
						}
						scope:cmd_plus_levies_xp_commander = {
							set_variable = { name = cmd_plus_levies_xp_side_has_maa value = 1 }
						}
					}
				}
			}
			if = {
				limit = { var:cmd_plus_levies_xp_side_has_maa = 0 }
				add_trait_xp = {
					trait = levies_specialist
					value = {
						value = $XP$
						multiply = 1.5
						add = { value = martial divide = 25 }
					}
				}
			}
			remove_variable = cmd_plus_levies_xp_side_has_maa
		}
		if = { # skirmishers specialist
			limit = {
				has_trait = skirmishers_specialist
				commanding_army ?= { any_army_maa_regiment = { is_unit_type = skirmishers } }
			}
			add_trait_xp = {
				trait = skirmishers_specialist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # archers specialist
			limit = {
				has_trait = archers_specialist
				commanding_army ?= { any_army_maa_regiment = { is_unit_type = archers } }
			}
			add_trait_xp = {
				trait = archers_specialist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # heavy cav specialist
			limit = {
				has_trait = heavy_cav_specialist
				commanding_army ?= {
					any_army_maa_regiment = {
						OR = {
							is_unit_type = heavy_cavalry
							is_unit_type = elephant_cavalry
						}
					}
				}
			}
			add_trait_xp = {
				trait = heavy_cav_specialist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # light cav specialist
			limit = {
				has_trait = light_cav_specialist
				commanding_army ?= {
					any_army_maa_regiment = {
						OR = {
							is_unit_type = light_cavalry
							is_unit_type = camel_cavalry
						}
					}
				}
			}
			add_trait_xp = {
				trait = light_cav_specialist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # heavy infantry specialist
			limit = {
				has_trait = heavy_infantry_specialist
				commanding_army ?= { any_army_maa_regiment = { is_unit_type = heavy_infantry } }
			}
			add_trait_xp = {
				trait = heavy_infantry_specialist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # archer cav specialist
			limit = {
				has_trait = archer_cav_specialist
				commanding_army ?= { any_army_maa_regiment = { is_unit_type = archer_cavalry } }
			}
			add_trait_xp = {
				trait = archer_cav_specialist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # pikemen specialist
			limit = {
				has_trait = pikemen_specialist
				commanding_army ?= { any_army_maa_regiment = { is_unit_type = pikemen } }
			}
			add_trait_xp = {
				trait = pikemen_specialist
				value = {
					value = $XP$
					multiply = 1.25
					add = { value = martial divide = 25 }
				}
			}
		}
		if = { # knights specialist - only knights assigned to this commander's army.
			limit = {
				has_trait = knights_specialist
				is_commanding_army = yes
			}
			commanding_army = { save_temporary_scope_as = cmd_plus_knights_xp_army }
			if = {
				limit = {
					root = {
						any_side_knight = {
							is_alive = yes
							knight_army ?= scope:cmd_plus_knights_xp_army
						}
					}
				}
				add_trait_xp = {
					trait = knights_specialist
					value = {
						value = $XP$
						multiply = 1.0
						add = { value = prowess divide = 25 }
					}
				}
			}
		}
}
