﻿# Picks a positive trait modification to occur as part of
# task_sparring_partners.
#
# KNIGHT_TRAIT_SCOPE = output flag scope indicating which trait should be gained / modified. May be unset.
# KNIGHT_TOURNEY_PARTICIPANT_SCOPE = output flag scope indicating modifications of tourney_participant trait to perform. May be unset.
# scope = the charaacter we are picking a trait modification for
regula_task_sparring_partners_positive_pick_trait_side_effects = {
	random_list = {
		20 = { # Improve Blademaster
			trigger = {
				regula_task_sparring_partners_can_increase_blademaster_trigger = yes
			}
			save_temporary_scope_value_as = {
				name = $KNIGHT_TRAIT_SCOPE$
				value = flag:lifestyle_blademaster
			}
		}
		20 = { # Become Strong
			trigger = {
				regula_task_sparring_partners_can_become_strong_trigger = yes
			}
			save_temporary_scope_value_as = {
				name = $KNIGHT_TRAIT_SCOPE$
				value = flag:strong
			}
		}
		20 = { # Improve Tourney Participant
			trigger = {
				regula_task_sparring_partners_can_increase_tourney_participant_trigger = yes
			}
			regula_task_sparring_partners_positive_trait_pick_tourney_track_effect = {
				SAVE_AS = $KNIGHT_TOURNEY_PARTICIPANT_SCOPE$
			}
		}
		20 = { # Gain a Commander Trait
			trigger = {
				number_of_commander_traits < commander_trait_limit
			}
			regula_task_sparring_partners_positive_trait_pick_commander_trait_effect = {
				SAVE_AS = $KNIGHT_TRAIT_SCOPE$
			}
		}
		10 = { # Craven -> Brave
			trigger = {
				has_trait = craven
			}
			save_temporary_scope_value_as = {
				name = $KNIGHT_TRAIT_SCOPE$
				value = flag:brave
			}
		}
		10 = { # Lazy -> Diligent
			trigger = {
				has_trait = lazy
			}
			save_temporary_scope_value_as = {
				name = $KNIGHT_TRAIT_SCOPE$
				value = flag:diligent
			}
		}
	}
}

# Determines which tourney_participant track a character should gain xp in for
# task_sparring_partners side effects.
#
# SAVE_AS = the name of the scope to save the selected track to
# scope = the character gaining tourney_participant xp
regula_task_sparring_partners_positive_trait_pick_tourney_track_effect = {
	if = {
		limit = {
			has_trait = tourney_participant
		}
		random_list = {
			1 = {
				trigger = {
					has_trait_xp = {
						trait = tourney_participant
						track = foot
						value < 100
					}
				}
				save_temporary_scope_value_as = {
					name = $SAVE_AS$
					value = flag:foot
				}
			}
			1 = {
				trigger = {
					has_trait_xp = {
						trait = tourney_participant
						track = bow
						value < 100
					}
				}
				save_temporary_scope_value_as = {
					name = $SAVE_AS$
					value = flag:bow
				}
			}
			1 = {
				trigger = {
					has_trait_xp = {
						trait = tourney_participant
						track = horse
						value < 100
					}
				}
				save_temporary_scope_value_as = {
					name = $SAVE_AS$
					value = flag:horse
				}
			}
			1 = {
				trigger = {
					has_trait_xp = {
						trait = tourney_participant
						track = wit
						value < 100
					}
				}
				save_temporary_scope_value_as = {
					name = $SAVE_AS$
					value = flag:wit
				}
			}
		}
	}
	else = {
		save_temporary_scope_value_as = {
			name = $SAVE_AS$
			value = flag:tourney_participant
		}
	}
}

# Applies tourney_participant track xp to the given character based on the
# specified track.
#
# This is a NOOP if TOURNEY_SCOPE is unset / null.
#
# TOURNEY_SCOPE = the flag scope which determines which track to gain xp in
# scope = the character to gain tourney_participant track xp
regula_task_sparring_partners_positive_trait_apply_tourney_track_effect = {
	if = {
		limit = {
			exists = scope:$TOURNEY_SCOPE$
		}
		switch = {
			trigger = scope:$TOURNEY_SCOPE$
			flag:tourney_participant = {
				add_trait = tourney_participant
			}
			flag:foot = {
				add_trait_xp = {
					trait = tourney_participant
					track = foot
					value = {
						integer_range = {
							min = small_lifestyle_random_xp_low
							max = small_lifestyle_random_xp_high
						}
					}
				}
			}
			flag:bow = {
				add_trait_xp = {
					trait = tourney_participant
					track = bow
					value = {
						integer_range = {
							min = small_lifestyle_random_xp_low
							max = small_lifestyle_random_xp_high
						}
					}
				}
			}
			flag:horse = {
				add_trait_xp = {
					trait = tourney_participant
					track = horse
					value = {
						integer_range = {
							min = small_lifestyle_random_xp_low
							max = small_lifestyle_random_xp_high
						}
					}
				}
			}
			flag:wit = {
				add_trait_xp = {
					trait = tourney_participant
					track = wit
					value = {
						integer_range = {
							min = small_lifestyle_random_xp_low
							max = small_lifestyle_random_xp_high
						}
					}
				}
			}
		}
	}
}

# Determines the commander trait to be granted for task_sparring_partners side
# effects.
#
# SAVE_AS = the scope to which the selected trait flag should be saved
# scope = the character to gain a commander trait
regula_task_sparring_partners_positive_trait_pick_commander_trait_effect = {
	random_list = {
		1 = {
			trigger = { NOT = { has_trait = holy_warrior } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:holy_warrior
			}
		}
		1 = {
			trigger = { NOT = { has_trait = reaver } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:reaver
			}
		}
		1 = {
			trigger = { NOT = { has_trait = logistician } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:logistician
			}
		}
		1 = {
			trigger = { NOT = { has_trait = military_engineer } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:military_engineer
			}
		}
		1 = {
			trigger = { NOT = { has_trait = aggressive_attacker } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:aggressive_attacker
			}
		}
		1 = {
			trigger = { NOT = { has_trait = unyielding_defender } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:unyielding_defender
			}
		}
		1 = {
			trigger = { NOT = { has_trait = forder } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:forder
			}
		}
		1 = {
			trigger = { NOT = { has_trait = flexible_leader } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:flexible_leader
			}
		}
		1 = {
			trigger = { NOT = { has_trait = reckless } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:reckless
			}
		}
		1 = {
			trigger = { NOT = { has_trait = cautious_leader } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:cautious_leader
			}
		}
		1 = {
			trigger = { NOT = { has_trait = organizer } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:organizer
			}
		}
		1 = {
			trigger = { NOT = { has_trait = open_terrain_expert } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:open_terrain_expert
			}
		}
		1 = {
			trigger = { NOT = { has_trait = rough_terrain_expert } }
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:rough_terrain_expert
			}
		}
		1 = {
			trigger = {
				location ?= {
					OR = {
						terrain = oasis
						terrain = desert
						terrain = desert_mountains
					}
				}
				NOT = { has_trait = desert_warrior }
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:desert_warrior
			}
		}
		1 = {
			trigger = {
				location ?= {
					terrain = jungle
				}
				NOT = { has_trait = jungle_stalker }
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:jungle_stalker
			}
		}
		1 = {
			trigger = {
				location ?= {
					OR = {
						terrain = taiga
						geographical_region = world_tibet
						geographical_region = world_europe_north
						geographical_region = world_europe_east
						geographical_region = world_steppe
					}
				}
				NOT = { has_trait = winter_soldier }
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:winter_soldier
			}
		}
		1 = {
			trigger = {
				location ?= {
					OR = {
						terrain = forest
						terrain = taiga
					}
				}
				NOT = { has_trait = forest_fighter }
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:forest_fighter
			}
		}
	}
}

# Adds the selected commander trait to the given character.
#
# This is a NOOP if TRAIT_SCOPE is unset / null.
#
# TRAIT_SCOPE = the flag scope which determines trait to grant
# scope = the character to gain a commander trait
regula_task_sparring_partners_positive_trait_apply_trait_effect = {
	if = {
		limit = {
			exists = scope:$TRAIT_SCOPE$
		}
		switch = {
			trigger = scope:$TRAIT_SCOPE$
			flag:logistician = {
				add_trait = logistician
			}
			flag:military_engineer = {
				add_trait = military_engineer
			}
			flag:aggressive_attacker = {
				add_trait = aggressive_attacker
			}
			flag:unyielding_defender = {
				add_trait = unyielding_defender
			}
			flag:forder = {
				add_trait = forder
			}
			flag:flexible_leader = {
				add_trait = flexible_leader
			}
			flag:reaver = {
				add_trait = reaver
			}
			flag:reckless = {
				add_trait = reckless
			}
			flag:holy_warrior = {
				add_trait = holy_warrior
			}
			flag:cautious_leader = {
				add_trait = cautious_leader
			}
			flag:organizer = {
				add_trait = organizer
			}
			flag:open_terrain_expert = {
				add_trait = open_terrain_expert
			}
			flag:rough_terrain_expert = {
				add_trait = rough_terrain_expert
			}
			flag:desert_warrior = {
				add_trait = desert_warrior
			}
			flag:jungle_stalker = {
				add_trait = jungle_stalker
			}
			flag:winter_soldier = {
				add_trait = winter_soldier
			}
			flag:forest_fighter = {
				add_trait = forest_fighter
			}
			# Following are slightly more complicated than just adding trait.
			flag:lifestyle_blademaster = {
				if = {
					limit = {
						NOT = {
							has_trait = lifestyle_blademaster
						}
					}
					add_trait = lifestyle_blademaster
				}
				else = {
					add_trait_xp = {
						trait = lifestyle_blademaster
						value = {
							integer_range = {
								min = small_lifestyle_random_xp_low
								max = small_lifestyle_random_xp_high
							}
						}
					}
				}
			}
			flag:strong = {
				if = {
					limit = {
						has_trait = weak
					}
					remove_trait = weak
				}
				else = {
					add_trait = strong
				}
			}
			flag:brave = {
				remove_trait = craven
				add_trait_force_tooltip = brave
			}
			flag:diligent = {
				remove_trait = lazy
				add_trait_force_tooltip = diligent
			}
		}
	}
}

# Determines the maiming trait to be granted for a contubernalis supervisor side
# effect.
#
# SAVE_AS = the scope to which the selected trait flag should be saved
# scope = the character to gain a commander trait
regula_contubernalis_supervisor_pick_maim_trait_effect = {
	random_list = {
		1 = {
			trigger = {
				NOT = {
					has_trait = one_legged
				}
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:one_legged
			}
		}
		1 = {
			trigger = {
				NOT = {
					has_trait = disfigured
				}
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:disfigured
			}
		}
		1 = {
			trigger = {
				NOT = {
					has_trait = one_eyed
				}
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:one_eyed
			}
		}
		1 = {
			trigger = {
				NOT = {
					has_trait = maimed
				}
			}
			save_temporary_scope_value_as = {
				name = $SAVE_AS$
				value = flag:maimed
			}
		}
	}
}

# Applies the maiming trait to be granted for a contubernalis supervisor side
# effect. Also applies wounds if the character is not at max wound level.
#
# This is a NOOP if TRAIT_SCOPE is unset / null.
#
# TRAIT_SCOPE = the flag scope which determines trait to grant
# REASON = the reason for the maiming
#
# scope = the character to gain a maiming trait
regula_contubernalis_supervisor_apply_maim_trait_effect = {
	if = {
		limit = {
			exists = scope:$TRAIT_SCOPE$
		}

		if = {
			limit = {
				has_trait_rank = {
					trait = wounded
					rank < 3
				}
			}
			increase_wounds_no_death_effect = { 
				REASON = $REASON$
			}
		}

		switch = {
			trigger = scope:$TRAIT_SCOPE$
			flag:one_legged = {
				add_trait = one_legged
			}
			flag:disfigured = {
				add_trait = disfigured
			}
			flag:one_eyed = {
				add_trait = one_eyed
			}
			flag:maimed = {
				add_trait = maimed
			}
		}
	}
}
