﻿# ROOT is a combat side in this scripted effect, but the THIS scope should be the commander
commander_trait_leveling_leveling_effect_base = {
	if = { # Terrain Based
		limit = {
			OR = {
				has_trait = desert_warrior
				has_trait = open_terrain_expert
				has_trait = rough_terrain_expert
				has_trait = forest_fighter
				has_trait = jungle_stalker
			}
		}
		if = {
			limit = {
				has_trait = desert_warrior
				location = {
					OR = {
						terrain = drylands 
						terrain = desert
						terrain = desert_mountains
						terrain = oasis
					}
				}
			}
			add_trait_xp = {
				trait = desert_warrior
				value = {
					value = $XP$
					multiply = 2
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
		else_if = {
			limit = {
				has_trait = open_terrain_expert
				location = {
					OR = {
						terrain = farmlands
						terrain = plains
						terrain = steppe
					}
				}
			}
			add_trait_xp = {
				trait = open_terrain_expert
				value = {
					value = $XP$
					multiply = 2
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
		else_if = {
			limit = {
				has_trait = rough_terrain_expert
				location = {
					OR = {
						terrain = hills
						terrain = mountains
						terrain = wetlands
					}
				}
			}
			add_trait_xp = {
				trait = rough_terrain_expert
				value = {
					value = $XP$
					multiply = 2
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
		else_if = {
			limit = {
				has_trait = forest_fighter
				location = {
					OR = {
						terrain = forest
						terrain = taiga
					}
				}
			}
			add_trait_xp = {
				trait = forest_fighter
				value = {
					value = $XP$
					multiply = 2
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
		else_if = {
			limit = {
				has_trait = jungle_stalker
				location = {
					terrain = jungle
				}
			}
			add_trait_xp = {
				trait = jungle_stalker
				value = {
					value = $XP$
					multiply = 2
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
	}
	if = { # Forder
		limit = {
			has_trait = forder
			location = {
				OR = {
					is_coastal = yes
					is_riverside_province = yes
				}
			}
		}
		add_trait_xp = {
			trait = forder
			value = {
				value = $XP$
				multiply = 2
				add = {
					value = martial
					divide = 10
				}
			}
		}
	}
	if = { # Attackers/Defenders
		limit = {
			OR = {
				has_trait = aggressive_attacker
				has_trait = unyielding_defender
			}
		}
		if = {
			limit = {
				has_trait = aggressive_attacker
				root = { is_combat_side_attacker = yes }
			}
			add_trait_xp = {
				trait = aggressive_attacker
				value = {
					value = $XP$
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
		else_if = {
			limit = {
				has_trait = unyielding_defender
				root = { is_combat_side_attacker = no }
			}
			add_trait_xp = {
				trait = unyielding_defender
				value = {
					value = $XP$
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
	}
	if = { # Flexible Leader
		limit = {
			has_trait = flexible_leader
		}
		add_trait_xp = {
			trait = flexible_leader
			value = {
				value = $XP$
				multiply = 0.5
				add = {
					value = martial
					divide = 10
				}
			}
		}
	}
	if = { # Religious
		limit = {
			has_trait = holy_warrior
			root.enemy_side.side_commander.religion != root.side_commander.religion
		}
		add_trait_xp = {
			trait = holy_warrior
			value = {
				value = $XP$
				multiply = 0.75
				add = {
					value = martial
					divide = 10
				}
			}
		}
	}
	if = { # Larger Armies
		limit = {
			OR = {
				has_trait = reckless
				has_trait = cautious_leader
			}
			root = { troops_ratio < 0.95 }
		}
		if = {
			limit = {
				has_trait = reckless
			}
			add_trait_xp = {
				trait = reckless
				value = {
					value = $XP$
					multiply = 3
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
		if = {
			limit = {
				has_trait = cautious_leader
			}
			add_trait_xp = {
				trait = cautious_leader
				value = {
					value = $XP$
					multiply = 3
					add = {
						value = martial
						divide = 10
					}
				}
			}
		}
	}
	if = { # Seasonal
		limit = {
			has_trait = winter_soldier
			location = { has_winter_combat_trigger = yes }
		}
		add_trait_xp = {
			trait = winter_soldier
			value = {
				value = $XP$
				multiply = 0.75
				add = {
					value = martial
					divide = 10
				}
			}
		}
	}
	if = { # Peasant Leader fighting non peasants
		limit = {
			has_trait = peasant_leader
			root.enemy_side.side_commander = {
				is_lowborn = no
			}
		}
		add_trait_xp = {
			trait = peasant_leader
			value = {
				value = $XP$
				multiply = 0.75
				add = {
					value = martial
					divide = 10
				}
			}
		}
	}
}

commander_trait_leveling_leveling_effect = {
	commander_trait_leveling_leveling_effect_base = { XP = $XP$ }
	if = {
		limit = {
			has_game_rule = embattle_side_commanders_trait_xp_enabled
		}
		commanding_army.var:embattle_left_flank_commander ?= {
			commander_trait_leveling_leveling_effect_base = { XP = $XP$ }
		}
		commanding_army.var:embattle_right_flank_commander ?= {
			commander_trait_leveling_leveling_effect_base = { XP = $XP$ }
		}
	}
}
