#############################################
# CTA: Combat Trait Advancement             #
#                                           #
# Events to complement the integrated New   #
# Duel Engine with combat trait advancement #
# via:                                      #
# - cta.0, on_combat_pulse                  #
# - cta.10, victorious duel engine output   #
#                                           #
# zijistark <azrinon@gmail.com>             #
#############################################

namespace = CombatTraits

# cta.0
# on_combat_pulse on_action event:
#
# character linearly advances through the 3 experience point (XP)
# tiers required to level-up their combat trait.
#
# valid for all characters that can take the ambition (could be
# open to more), whether they've enabled the ambition or not. of
# course, enabling the ambition dramatically increases the odds
# of this event firing.
#
# the associated ambitions are strict about gender: females can
# only take them when conditions are such that they are allowed
# to participate in battle.  neither this on_combat_pulse series
# nor the victorious duel series enforce that directly (in the
# first case because it's implied by the event firing at all and
# in the second because XP advancement is just fine if a female
# somehow gets into a duel when she can't participate in battles,
# as it is for non-rulers and non-marshals as well). 

character_event = {
	id = CombatTraits.0
	desc = OK
	
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
		NOT = {
			is_alive = no
			is_adult = no
			age = 50
			prisoner = yes
			trait = incapable
			trait = blinded
		}
		OR = {
			is_ruler = yes
			liege = { job_marshal = { character = ROOT } }
		}
		
		NOT = { trait = master_warrior }
		NOT = { has_character_flag = cta_done }
	}
	
	weight_multiplier = {
		days = 1
		
		# basically, you must select a CTA ambition to advance.
		#
		# on top of the 4x factor:
		#
		# in practice, AI will select an ambitions in the relevant cases, will be
		# present in the same pool of characters for on_combat_pulse, and thus they
		# will then harshly outweigh the random chance of firing this event for a
		# player that hasn't selected the ambition.

		modifier = {
			factor = 4.0
			OR = {
				has_ambition = obj_cta_0
				has_ambition = obj_cta_1
				has_ambition = obj_cta_2
				has_ambition = obj_cta_3
			}
		}
		
		# give the AI a flat 33% higher random weight bonus with a CTA ambition selected.
		#
		# a) further lengthens amount of time/battles/duels player must commit to advance,
		#    as a player character in the same random selection pool becomes lower weight
		#    relative to the chance divisor.
		# b) I don't want the AI stuck on the ambitions for too long.  it can't reason about
		#    when is a reasonable time to cancel the ambition.
		# c) mild AI handicaps => good (lacking an actual interface to AI scripting).

		modifier = {
			factor = 1.333
			ai = yes
			OR = {
				has_ambition = obj_cta_0
				has_ambition = obj_cta_1
				has_ambition = obj_cta_2
				has_ambition = obj_cta_3
			}
		}

		# it is increasingly less likely to earn a combat impulse with higher tiers of combat traits
		modifier = {
			factor = 0.8
			trait = poor_warrior
		}
		modifier = {
			factor = 0.7
			trait = trained_warrior
		}
		modifier = {
			factor = 0.65 # still significantly more than base weight if ambition is selected
						  # but definitely much less frequent due to far more characters with
						  # a lower-tier trait present in the same pool.
			trait = skilled_warrior
		}
		
		# age matters a lot, although it may seem a little difficult to link the dependency for the [abstracted] player
		modifier = {
			factor = 1.25
			NOT = { age = 30 }
		}
		modifier = {
			factor = 1.25
			NOT = { age = 25 }
		}
		modifier = {
			factor = 1.25
			NOT = { age = 20 }
		}
		modifier = {
			factor = 0.75
			age = 40
		}
		modifier = {
			factor = 0.75
			age = 45
		}
		
		# direct trait influence is minimized in the random weight multiplier.  if character is a player,
		# this probably means more flexible advancement for any given set of traits.  if character is AI,
		# traits are heavily covered in CTA ambition selection chance and so are redundant here beyond
		# these basics:

		modifier = {
			factor = 1.25
			trait = diligent
		}
		modifier = {
			factor = 0.75
			trait = slothful
		}
		modifier = {
			factor = 1.25
			trait = brave
		}
		modifier = {
			factor = 0.75
			trait = craven
		}
		
		# higher martial skill should equate to more ease in understanding
		# the art of personal combat, whether it's a lot of a base skill
		# level or predominantly from various traits (further amplifying
		# the effects of traits with any martial bonus or malus, e.g., agile,
		# crusader, martial educations, hunchback, or weak).  while combat
		# traits are "personal combat" and not "military theory," in practice
		# the martial attribute encodes both.
		
		modifier = {
			factor = 1.08
			martial = 10
		}
		modifier = {
			factor = 1.07
			martial = 11
		}
		modifier = {
			factor = 1.07
			martial = 12
		}
		modifier = {
			factor = 1.06
			martial = 13
		}
		modifier = {
			factor = 1.06
			martial = 14
		}
		modifier = {
			factor = 1.05
			martial = 15
		}
		modifier = {
			factor = 1.05
			martial = 16
		}
		modifier = {
			factor = 1.05
			martial = 17
		}
		modifier = {
			factor = 1.05
			martial = 18
		}
		modifier = {
			factor = 1.04
			martial = 19
		}
		modifier = {
			factor = 1.04
			martial = 20
		}
	}
	
	option = {
		name = OK # reminder: event is hidden, only does the bookkeeping, and forwards all visible events from here
		
		if = {
			limit = { has_global_flag = debug_cta }
			random_playable_ruler = {
				limit = { ai = no same_realm = ROOT }
				character_event = { id = CombatTraits.110 }
				character_event = { id = CombatTraits.111 }
			}
		}

		if = { limit = { has_character_flag = ctxp1 } # have 2nd, just earned 3rd experience tier (of 3)
			
			clr_character_flag = ctxp1
			set_character_flag = cta_level # temp. flag, not committed beyond this event
			
			if = {
				limit = {
					OR = {
						trait = poor_warrior
						trait = trained_warrior
						trait = skilled_warrior
					}
				}
				# since they aren't starting from the first rung (no combat trait), this is the only/last
				# combat trait advancement they'll get from this system (associated ambitions will also
				# be disabled, of course).
				
				set_character_flag = cta_done
				
				# otherwise, the ctxp[01] flags are all reset just as they were when they started advancing
				# toward poor_warrior, so they will be allowed to continue advancing toward trained_warrior
				# (at a significantly reduced expected frequency of firing this event)
			}
			
			if = {
				limit = { has_global_flag = debug_cta }
				random_playable_ruler = {
					limit = { ai = no same_realm = ROOT }
					character_event = { id = CombatTraits.114 }
				}
			}
			
			# notify character of level-up (level-up is an effect of that event, forcing the Ambition Fulfilled popup to only appear afterward, if applicable)
			character_event = { id = CombatTraits.3 }
		}
		
		if = {
			limit = { has_character_flag = ctxp0 } # have 1st, just earned 2nd experience tier (of 3)

			clr_character_flag = ctxp0
			set_character_flag = ctxp1
			
			# notify character of further progress being made
			character_event = { id = CombatTraits.2 }
		}
		
		if = {
			limit = { # earned 1st experience tier (of 3)
				NOT = {
					has_character_flag = ctxp0
					has_character_flag = ctxp1
					has_character_flag = cta_level # guards against reset of XP due to level-up above
				}
			}
			
			set_character_flag = ctxp0
			
			# notify character of progress being made
			character_event = { id = CombatTraits.1 }
		}
		
		clr_character_flag = cta_level
	}
}

# TODO: each notification should randomly choose between a small prestige bonus, random allied combatant opinion bonuses (at location), etc.
# TODO: even within the battle series, there should be multiple notification events (randomly chosen) for each XP tier for flavor and to avoid excessive repetition

# first notification of progress being made toward CTA (of 2): battle series
character_event = {
	id = CombatTraits.1
	desc = EVTDESC_CombatTraits_1
	picture = GFX_evt_battle
	border = GFX_event_normal_frame_war
	
	is_triggered_only = yes
	hide_from = yes
	
	trigger = {
		NOT = { trait = master_warrior }
	}
	
	option = {
		name = EVTOPTA_CombatTraits_1
		custom_tooltip = { text = EVTOPTA_CombatTraits_1.CTT }
	}
}

# second notification of progress being made toward CTA (of 2): battle series
character_event = {
	id = CombatTraits.2
	desc = EVTDESC_CombatTraits_2
	picture = GFX_evt_battle
	border = GFX_event_normal_frame_war
	
	is_triggered_only = yes
	hide_from = yes
	
	trigger = {
		NOT = { trait = master_warrior }
	}
	
	option = {
		name = EVTOPTA_CombatTraits_2
		custom_tooltip = { text = EVTOPTA_CombatTraits_2.CTT }
	}
}

# successful CTA level-up: battle series
#
# TODO: add a more significant random bonus (e.g., temporary opinion from numerous
#       friendly soldiers/leaders at the same location and possibly the liege)
# NOTE: if the character had a CTA ambition enabled (they probably did if they
#       advanced this far), they'll receive a small, tier-dependent prestige bonus
#       and an Ambition Fulfilled notification moments after accepting this event.
character_event = {
	id = CombatTraits.3
	desc = EVTDESC_CombatTraits_3
	picture = GFX_evt_battle
	border = GFX_event_normal_frame_war
	
	is_triggered_only = yes
	hide_from = yes
	
	trigger = {
		NOT = { trait = master_warrior }
	}
	
	option = {
		name = EVTOPTA_CombatTraits_3
		trigger = {
			NOT = {
				trait = poor_warrior
				trait = trained_warrior
				trait = skilled_warrior
			}
		}
		
		add_trait = poor_warrior
	}
	
	option = {
		name = EVTOPTB_CombatTraits_3
		trigger = { trait = poor_warrior }
		remove_trait = poor_warrior
		add_trait = trained_warrior
	}
	
	option = {
		name = EVTOPTC_CombatTraits_3
		trigger = { trait = trained_warrior }
		remove_trait = trained_warrior
		add_trait = skilled_warrior
	}
	
	option = {
		name = EVTOPTD_CombatTraits_3
		trigger = { trait = skilled_warrior }
		remove_trait = skilled_warrior
		add_trait = master_warrior
	}
}


# Victorious Duel (opponent slain or spared)
#
# Called for 1) battlefield duels, 2) feast duels, 3) rival duels, 4) regency duels, etc. (check various_duel_events.txt)
# FROM = loser, ROOT = winner.  Ergo, if we want to reference the loser of the duel in a notification event fired from here,
# FROMFROM will be the loser and ROOT=FROM will be the winner. This event is a model of DuelEngine.901 scope-wise, and indeed,
# it was originally called directly from NDE's duel outcomes rather than duel output events However, I wanted to minimize merge
# conflicts with NDE, so DuelEngine.901 forwards back to the [possibly dead] loser in CombatTraits.11, which immediately bounces
# back to the winner here, in CombatTraits.10, allowing full access to FROMFROM (and FROMFROM's portait) in child events. A little
# silly but not.
#
# Again, this is a hidden-window event which does bookkeeping and only fires visible messaging events when appropriate.
# It could not be (unlike the combat impulse event), but a single victorious duel does not earn an experience tier (only 3
# and always exactly 3 victories would be required if it did).  Instead, each duel win has a random chance of advancing
# the character's experience tier.  The cumulative density function for experience tier advancement needs to be tuned for
# balance.
#
# NOTE: progress notifications were skipped in this case, as there is enough event-chain clutter for the user to click
#       their way through during/after duels (and they're likely in the middle of a battle, which presents yet more).
#
# TODO: variation on flavor of level-up notification. we don't effectively use information about the opponent or the location
#       to throw out some interesting edification when we could (or at all-- the event text is generic regarding combat).

character_event = {
	id = CombatTraits.10
	desc = OK
	
	hide_window = yes
	is_triggered_only = yes
	
	# essentially same trigger as combat impulse event for uniformity's sake, though the duel engine isn't going to feed us
	# a dead/child/prisoner/incapable/blind winner (hopefully).  HOWEVER, we lift the "ruler or marshal of one" restriction,
	# because duel cases are rare, and the character might as well get a shot at advancing some state toward a CTA in the
	# future or, without the ambition, via duels alone.

	trigger = {
		NOT = {
			is_alive = no
			is_adult = no
			age = 50
			prisoner = yes
			trait = incapable
			trait = blinded
		}
		
		NOT = { trait = master_warrior }
		NOT = { has_character_flag = cta_done }
	}
	
	option = {
		name = OK
		
		# flip a coin for whether this duel win counts toward an XP tier or not

		# currently: 40% chance per win w/o ambition (regardless of tier),
		#            70% per win w/ ambition and skilled_warrior
		#            75% per win w/ ambition and trained_warrior,
		#            85% per win w/ ambition and no combat trait or poor_warrior
		#
		# the probability of k victorious duels being required to advance a combat trait (dueling on its own, without
		# the help of any earned combat pulses) is (k >= 3):
		#
		# (1 - c)^(k - 3) * c^3
		#
		# where c is the chance of a victorious duel resulting in an XP advance (the above-cited percentages)
		#
		# the consequent expectation of duel wins required to advance a level is a bit daunting (especially
		# considering that each is usually kill-or-be-killed), hence the relatively high seeming percentages.
		# of course, XP advancement works in concert with combat pulses, so these probabilities are not the
		# whole story.
		#
		# in the future: since we've taken great care to keep the duel loser, alive or dead, in FROM (ergo FROMFROM
		# of any event we call), it would be much cooler to encapsulate the "coin flip" into another hidden event
		# which uses ai_chance on two options (either set cta_xp or do not) to allow nice, multiplicative modifiers
		# to be applied to the probability of advancement based upon relative skill of winner v. loser, actual duel
		# count won (to improve determinism past a certain number of wins), the type of duel (friendly vs. not), etc.
		
		# flag cta_xp is temp. to this event and represents whether ROOT did indeed earn an XP tier this time.
		
		random_list = {
			40 = { set_character_flag = cta_xp } # 40%
			30 = { # 70%
				if = {
					limit = {
						OR = {
							has_ambition = obj_cta_0
							has_ambition = obj_cta_1
							has_ambition = obj_cta_2
							has_ambition = obj_cta_3
						}
					}
					set_character_flag = cta_xp
				}
			}
			5 = { # 75%
				if = {
					limit = {
						OR = {
							has_ambition = obj_cta_0
							has_ambition = obj_cta_1
							has_ambition = obj_cta_2
						}
					}
					set_character_flag = cta_xp
				}
			}
			10 = { # 85%
				if = {
					limit = {
						OR = {
							has_ambition = obj_cta_0
							has_ambition = obj_cta_1
						}
					}
					set_character_flag = cta_xp
				}
			}
			15 = { }
		}
		
		if = { limit = { has_character_flag = cta_xp }
		
			# cool. advanced an XP tier.

			if = {
				limit = { has_global_flag = debug_cta }
				random_playable_ruler = {
					limit = { ai = no same_realm = ROOT }
					character_event = { id = CombatTraits.112 }
				}
			}
			
			if = {
				limit = { has_character_flag = ctxp1 } # earned 3rd experience tier (of 3)
				
				clr_character_flag = ctxp1
				set_character_flag = cta_level # temp. flag, not committed beyond this event
				
				if = {
					limit = {
						OR = {
							trait = poor_warrior
							trait = trained_warrior
							trait = skilled_warrior
						}
					}
					set_character_flag = cta_done
				}
				
				if = {
					limit = { has_global_flag = debug_cta }
					random_playable_ruler = {
						limit = { ai = no same_realm = ROOT }
						character_event = { id = CombatTraits.114 }
					}
				}
				
				# notify character of level-up (actual level-up is an effect of that event)
				character_event = { id = CombatTraits.13 }
			}
			
			if = {
				limit = { has_character_flag = ctxp0 } # earned 2nd experience tier (of 3)
				clr_character_flag = ctxp0
				set_character_flag = ctxp1
			}
			
			if = {
				limit = { # earned 1st experience tier (of 3)
					NOT = {
						has_character_flag = ctxp0
						has_character_flag = ctxp1
						has_character_flag = cta_level
					}
				}
				set_character_flag = ctxp0
			}
			
		} # END: if advanced an XP tier

		if = {
			limit = {
				has_global_flag = debug_cta
				NOT = { has_character_flag = cta_xp }
			}
			random_playable_ruler = {
				limit = { ai = no same_realm = ROOT }
				character_event = { id = CombatTraits.113 }
			}
		}
		
		clr_character_flag = cta_xp
		clr_character_flag = cta_level
	}
}


# silly scope bouncer for CombatTraits.10. initial entry point from DuelEngine.90* (duel_engine_events.txt)
# FROM = winner.  ROOT = loser.
character_event = {
	id = CombatTraits.11
	desc = OK
	
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		FROM = { character_event = { id = CombatTraits.10 } }
	}
	
	option = { name = OK }
}


#############
# CombatTraits.12 free
#############


# duel series: successful CTA level-up
character_event = {
	id = CombatTraits.13
	desc = EVTDESC_CombatTraits_13
	picture = GFX_evt_battle
	border = GFX_event_normal_frame_war
	
	show_from_from = yes
	hide_from = yes
	
	is_triggered_only = yes
	
	trigger = {
		NOT = { trait = master_warrior }
	}
	
	option = {
		name = EVTOPTA_CombatTraits_13
		trigger = {
			NOT = {
				trait = poor_warrior
				trait = trained_warrior
				trait = skilled_warrior
			}
		}
		
		add_trait = poor_warrior
	}
	
	option = {
		name = EVTOPTB_CombatTraits_13
		trigger = { trait = poor_warrior }
		remove_trait = poor_warrior
		add_trait = trained_warrior
	}
	
	option = {
		name = EVTOPTC_CombatTraits_13
		trigger = { trait = trained_warrior }
		remove_trait = trained_warrior
		add_trait = skilled_warrior
	}
	
	option = {
		name = EVTOPTD_CombatTraits_13
		trigger = { trait = skilled_warrior }
		remove_trait = skilled_warrior
		add_trait = master_warrior
	}
}



# debug notification: combat impulse. variant: character has selected a CTA ambition.
character_event = {
	id = CombatTraits.110
	desc = EVTDESC_CombatTraits_110
	picture = GFX_evt_battle
	
	is_triggered_only = yes
	
	trigger = {
		FROM = {
			OR = {
				has_ambition = obj_cta_0
				has_ambition = obj_cta_1
				has_ambition = obj_cta_2
				has_ambition = obj_cta_3
			}
		}
	}
	
	option = { name = OK }
}


# debug notification: combat impulse. variant: character has no CTA ambition.
character_event = {
	id = CombatTraits.111
	desc = EVTDESC_CombatTraits_111
	picture = GFX_evt_battle
	
	is_triggered_only = yes
	
	trigger = {
		FROM = {
			NOT = {
				has_ambition = obj_cta_0
				has_ambition = obj_cta_1
				has_ambition = obj_cta_2
				has_ambition = obj_cta_3
			}
		}
	}
	
	option = { name = OK }
}


# debug notification: duel victory impulse. variant: coin-flip advanced XP tier (possibly levelled).
character_event = {
	id = CombatTraits.112
	desc = EVTDESC_CombatTraits_112
	picture = GFX_evt_battle
	border = GFX_event_normal_frame_war
	
	is_triggered_only = yes
	
	option = { name = OK }
}

# debug notification: duel victory impulse. variant: coin-flip did not advance XP tier.
character_event = {
	id = CombatTraits.113
	desc = EVTDESC_CombatTraits_113
	picture = GFX_evt_battle
	border = GFX_event_normal_frame_war
	
	is_triggered_only = yes
	
	option = { name = OK }
}

# debug notification: combat trait level-up.
character_event = {
	id = CombatTraits.114
	desc = EVTDESC_CombatTraits_114
	picture = GFX_evt_battle
	border = GFX_event_normal_frame_war
	
	is_triggered_only = yes
	
	trigger = {
		FROM = { NOT = { trait = master_warrior } }
	}
	
	option = {
		name = EVTOPTA_CombatTraits_114
		trigger = {
			FROM = {
				NOT = {
					trait = poor_warrior
					trait = trained_warrior
					trait = skilled_warrior
				}
			}
		}
		
		tooltip = { FROM = { add_trait = poor_warrior } }
	}
	
	option = {
		name = EVTOPTB_CombatTraits_114
		trigger = { FROM = { trait = poor_warrior } }
		tooltip = { FROM = { add_trait = trained_warrior } }
	}
	
	option = {
		name = EVTOPTC_CombatTraits_114
		trigger = { FROM = { trait = trained_warrior } }
		tooltip = { FROM = { add_trait = skilled_warrior } }
	}
	
	option = {
		name = EVTOPTD_CombatTraits_114
		trigger = { FROM = { trait = skilled_warrior } }
		tooltip = { FROM = { add_trait = master_warrior } }
	}
}

###################################
#
# Hand out traits. Totally ripped from PB
#
###################################

character_event = {
	id =    CombatTraits.600
	title = AI_EVENT
	desc =  AI_EVENT

	hide_window = yes
	only_rulers = yes
	religion = miaphysite
	
	trigger = {
		controls_religion = yes # Let it happen for the Coptic Pope - he's always around
		NOT = { has_global_flag = cta_distributed }
	}

	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		set_global_flag = cta_distributed
		
		# Give out combat traits
		any_playable_ruler = {
			limit = {
				NOR = { # only assign a combat trait to characters that don't already have one
					trait = poor_warrior
					trait = trained_warrior
					trait = skilled_warrior
					trait = master_warrior
					has_character_flag = startup_cta_assigned
				}
			}
			any_courtier = {
				limit = {
					NOR = { # only assign a combat trait to characters that don't already have one
						trait = poor_warrior
						trait = trained_warrior
						trait = skilled_warrior
						trait = master_warrior
						has_character_flag = startup_cta_assigned
					}
					OR = {	
						trait = misguided_warrior
						trait = tough_soldier
						trait = skilled_tactician
						trait = brilliant_strategist
					}
				}
				if = {
					limit = { trait = misguided_warrior }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = tough_soldier }
					change_variable = { which = "combat_skill" value = 4 }
				}
				if = {
					limit = { trait = skilled_tactician }
					change_variable = { which = "combat_skill" value = 6 }
				}
				if = {
					limit = { trait = brilliant_strategist }
					change_variable = { which = "combat_skill" value = 8 }
				}
				if = {
					limit = { trait = diligent }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = brave }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = quick }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = shrewd }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = genius }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = robust }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = strong }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = agile }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = weak }
					change_variable = { which = "combat_skill" value = -2 }
				}
				if = {
					limit = { trait = feeble }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = slow }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = dull }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = imbecile }
					change_variable = { which = "combat_skill" value = -3 }
				}
				if = {
					limit = { trait = inbred }
					change_variable = { which = "combat_skill" value = -2 }
				}
				if = {
					limit = { trait = clubfooted }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = slothful }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = craven }
					change_variable = { which = "combat_skill" value = -2 }
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 8 }
					}
					add_trait = master_warrior
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 6 }
						NOT = { trait = master_warrior }
					}
					add_trait = skilled_warrior
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 4 }
						NOT = { trait = master_warrior }
						NOT = { trait = skilled_warrior }
					}
					add_trait = trained_warrior
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 1 }
						NOT = { trait = master_warrior }
						NOT = { trait = skilled_warrior }
						NOT = { trait = trained_warrior }
					}
					add_trait = poor_warrior
				}
				set_variable = { which = "combat_skill" value = 0 }
				set_character_flag = startup_cta_assigned
			}
			any_vassal = {
				limit = {
					NOR = { # only assign a combat trait to characters that don't already have one
						trait = poor_warrior
						trait = trained_warrior
						trait = skilled_warrior
						trait = master_warrior
						has_character_flag = startup_cta_assigned
					}
				}
				if = {
					limit = { trait = misguided_warrior }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = tough_soldier }
					change_variable = { which = "combat_skill" value = 4 }
				}
				if = {
					limit = { trait = skilled_tactician }
					change_variable = { which = "combat_skill" value = 6 }
				}
				if = {
					limit = { trait = brilliant_strategist }
					change_variable = { which = "combat_skill" value = 8 }
				}
				if = {
					limit = { trait = diligent }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = brave }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = quick }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = shrewd }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = genius }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = robust }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = strong }
					change_variable = { which = "combat_skill" value = 2 }
				}
				if = {
					limit = { trait = agile }
					change_variable = { which = "combat_skill" value = 1 }
				}
				if = {
					limit = { trait = weak }
					change_variable = { which = "combat_skill" value = -2 }
				}
				if = {
					limit = { trait = feeble }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = slow }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = dull }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = imbecile }
					change_variable = { which = "combat_skill" value = -3 }
				}
				if = {
					limit = { trait = inbred }
					change_variable = { which = "combat_skill" value = -2 }
				}
				if = {
					limit = { trait = clubfooted }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = slothful }
					change_variable = { which = "combat_skill" value = -1 }
				}
				if = {
					limit = { trait = craven }
					change_variable = { which = "combat_skill" value = -2 }
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 8 }
					}
					add_trait = master_warrior
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 6 }
						NOT = { trait = master_warrior }
					}
					add_trait = skilled_warrior
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 4 }
						NOT = { trait = master_warrior }
						NOT = { trait = skilled_warrior }
					}
					add_trait = trained_warrior
				}
				if = {
					limit = {
						check_variable = { which = "combat_skill" value = 1 }
						NOT = { trait = master_warrior }
						NOT = { trait = skilled_warrior }
						NOT = { trait = trained_warrior }
					}
					add_trait = poor_warrior
				}
				set_variable = { which = "combat_skill" value = 0 }
				set_character_flag = startup_cta_assigned
			}			
			if = {
				limit = { trait = misguided_warrior }
				change_variable = { which = "combat_skill" value = 2 }
			}
			if = {
				limit = { trait = tough_soldier }
				change_variable = { which = "combat_skill" value = 4 }
			}
			if = {
				limit = { trait = skilled_tactician }
				change_variable = { which = "combat_skill" value = 6 }
			}
			if = {
				limit = { trait = brilliant_strategist }
				change_variable = { which = "combat_skill" value = 8 }
			}
			if = {
				limit = { trait = diligent }
				change_variable = { which = "combat_skill" value = 1 }
			}
			if = {
				limit = { trait = brave }
				change_variable = { which = "combat_skill" value = 2 }
			}
			if = {
				limit = { trait = quick }
				change_variable = { which = "combat_skill" value = 1 }
			}
			if = {
				limit = { trait = shrewd }
				change_variable = { which = "combat_skill" value = 1 }
			}
			if = {
				limit = { trait = robust }
				change_variable = { which = "combat_skill" value = 1 }
			}
			if = {
				limit = { trait = genius }
				change_variable = { which = "combat_skill" value = 2 }
			}
			if = {
				limit = { trait = strong }
				change_variable = { which = "combat_skill" value = 2 }
			}
			if = {
				limit = { trait = agile }
				change_variable = { which = "combat_skill" value = 1 }
			}
			if = {
				limit = { trait = weak }
				change_variable = { which = "combat_skill" value = -2 }
			}
			if = {
				limit = { trait = feeble }
				change_variable = { which = "combat_skill" value = -1 }
			}
			if = {
				limit = { trait = slow }
				change_variable = { which = "combat_skill" value = -1 }
			}
			if = {
				limit = { trait = dull }
				change_variable = { which = "combat_skill" value = -1 }
			}
			if = {
				limit = { trait = imbecile }
				change_variable = { which = "combat_skill" value = -3 }
			}
			if = {
				limit = { trait = inbred }
				change_variable = { which = "combat_skill" value = -2 }
			}
			if = {
				limit = { trait = clubfooted }
				change_variable = { which = "combat_skill" value = -1 }
			}
			if = {
				limit = { trait = slothful }
				change_variable = { which = "combat_skill" value = -1 }
			}
			if = {
				limit = { trait = craven }
				change_variable = { which = "combat_skill" value = -2 }
			}
			if = {
				limit = {
					check_variable = { which = "combat_skill" value = 8 }
				}
				add_trait = master_warrior
			}
			if = {
				limit = {
					check_variable = { which = "combat_skill" value = 6 }
					NOT = { trait = master_warrior }
				}
				add_trait = skilled_warrior
			}
			if = {
				limit = {
					check_variable = { which = "combat_skill" value = 4 }
					NOT = { trait = master_warrior }
					NOT = { trait = skilled_warrior }
				}
				add_trait = trained_warrior
			}
			if = {
				limit = {
					check_variable = { which = "combat_skill" value = 1 }
					NOT = { trait = master_warrior }
					NOT = { trait = skilled_warrior }
					NOT = { trait = trained_warrior }
				}
				add_trait = poor_warrior
			}
			set_variable = { which = "combat_skill" value = 0 }
			set_character_flag = startup_cta_assigned
		}
	}
}

###Combat Trait Events (CombatTraits.20-22)
###Credits to Galla (Greg) for coming up with 99% of these;

character_event = { #Learning the sword from your guardian
	id = CombatTraits.20
	desc = "EVTDESC_CombatTraits_20"
	picture = GFX_evt_child_sword
	
	min_age = 6
	max_age = 16
	prisoner = no

	trigger = {
		educator = {
			OR = {
				trait = misguided_warrior
				trait = tough_soldier
				trait = skilled_tactician
				trait = brilliant_strategist
				
				trait = poor_warrior
				trait = trained_warrior
				trait = skilled_warrior
				trait = master_warrior
				
				trait = martial_cleric
				trait = duelist
				has_job_title = job_marshal
				has_minor_title = title_commander
			}
		}
		NOT = { trait = master_warrior }
	}

	mean_time_to_happen = {
		months = 30
		
		# Advancement slows down based on current level
		modifier = {
			factor = 2
			trait = poor_warrior
		}
		modifier = {
			factor = 4
			trait = trained_warrior
		}
		modifier = {
			factor = 6
			trait = skilled_warrior
		}
		# advancement slows down if ward has better trait than guardian
		modifier = {
			factor = 6
			OR = {				
				AND = {
					trait = poor_warrior
					educator = {
						NOR = {
							trait = trained_warrior
							trait = skilled_warrior
							trait = master_warrior
						}
					}
				}
				AND = {
					trait = trained_warrior
					educator = {
						NOR = {						
							trait = skilled_warrior
							trait = master_warrior
						}
					}
				}
				AND = {
					trait = skilled_warrior
					educator = {
						NOT = {
							trait = master_warrior					
						}
					}
				}
				educator = {
					ROOT = {
						combat_rating_diff = { who = PREV value = 5 }
					}
				}
			}
		}
		
		# Mentor Effects
		modifier = {
			factor = 0.85
			educator = {
				OR = {
					trait = poor_warrior
					trait = trained_warrior
					trait = skilled_warrior
					trait = master_warrior
				}
			}
		}
		modifier = {
			factor = 0.85
			educator = {
				OR = {
					trait = trained_warrior
					trait = skilled_warrior
					trait = master_warrior
				}
			}
		}
		modifier = {
			factor = 0.85
			educator = {
				OR = {
					trait = skilled_warrior
					trait = master_warrior
				}
			}
		}
		modifier = {
			factor = 0.85
			educator = {
				trait = master_warrior
			}
		}

		# Mentor has martial education
		modifier = {
			factor = 0.85
			educator = {
				OR = {
					trait = martial_cleric
					has_job_title = job_marshal
					trait = misguided_warrior
					trait = tough_soldier
					trait = skilled_tactician
					trait = brilliant_strategist
				}
			}
		}
		modifier = {
			factor = 0.85
			OR = {
				has_focus = focus_ed_martial
				has_focus = focus_ch_struggle
			}
		}
		modifier = {
			factor = 0.85
			educator = {
				OR = {
					trait = tough_soldier
					trait = skilled_tactician
					trait = brilliant_strategist
				}
			}
		}
		modifier = {
			factor = 0.85
			educator = {
				OR = {
					trait = skilled_tactician
					trait = brilliant_strategist
				}
			}
		}
		modifier = {
			factor = 0.85
			educator = {
				trait = brilliant_strategist
			}
		}
		
		# Mentor is skilled in combat
		modifier = {
			factor = 0.85
			educator = { trait = duelist }
		}

		# Personality Effects
		#Good
		modifier = {
			factor = 0.85
			OR = {
				trait = diligent
				trait = brave
				trait = rowdy
			}
		}
		#Bad
		modifier = {
			factor = 1.35
			OR = {
				trait = slothful
				trait = craven
				trait = timid
			}
		}		
		
		# Intelligence Effects
		#Good
		modifier = {
			factor = 0.85
			trait = genius
		}
		modifier = {
			factor = 0.9
			OR = {
				trait = quick
				trait = shrewd
			}
		}
		#Bad
		modifier = {
			factor = 1.2
			OR = {
				trait = slow
				trait = dull
			}
		}
		modifier = {
			factor = 1.35
			trait = imbecile
		}
		
		# Physical Effects
		#Good
		modifier = {
			factor = 0.85
			is_strong_trigger = yes
		}
		#Bad
		modifier = {
			factor = 1.35
			is_weak_trigger = yes
		}
		
		#Sexism
		modifier = {
			factor = 1.4
			is_female = yes
		}
	}

	option = {
		name = "EVTOPTA_CombatTraits_20" # Stick 'em with the pointy end!
		hidden_tooltip = { educator = { character_event = { id = CombatTraits.21 } } } #Guardian Notification.
		if = {
			limit = { trait = skilled_warrior }
			remove_trait = skilled_warrior
			add_trait = master_warrior
		}
		if = {
			limit = { trait = trained_warrior }
			remove_trait = trained_warrior
			add_trait = skilled_warrior
		}
		if = {
			limit = { trait = poor_warrior }
			remove_trait = poor_warrior
			add_trait = trained_warrior
		}
		if = {
			limit = {
				NOR = {
					trait = poor_warrior
					trait = trained_warrior
					trait = skilled_warrior
					trait = master_warrior
				}
			}
			add_trait = poor_warrior
		}
	}
}


character_event = { #Guardian Notification for trait incrase
	id = CombatTraits.21
	desc = "EVTDESC_CombatTraits_21" #I have increased my ward's fighting skills.
	picture = "GFX_evt_child_sword"
	
	is_triggered_only = yes
	
	option = { name = "OK" }
}

character_event = { #Trait demotion due to age
	id = CombatTraits.22
	desc = "EVTDESC_CombatTraits_22"
	picture = GFX_evt_illness
	
	min_age = 45

	trigger = {
		immortal = no
		OR = {
			trait = poor_warrior
			trait = trained_warrior
			trait = skilled_warrior
			trait = master_warrior
		}
		NOT = { has_character_flag = cte_demoted }
	}

	mean_time_to_happen = {
		months = 120

		# Personality Effects
		#Good
		modifier = {
			factor = 1.25
			OR = {
				trait = diligent
				trait = brave
				trait = duelist
			}
		}
		#Bad
		modifier = {
			factor = 0.75
			trait = slothful
		}
		
		# Physical Effects
		#Good
		modifier = {
			factor = 1.5
			is_strong_trigger = yes
		}
		#Bad
		modifier = {
			factor = 0.2
			is_weak_trigger = yes
		}
	}

	option = {
		name = "CURSES"
		
		if = {
			limit = { trait = poor_warrior }
			remove_trait = poor_warrior
			set_character_flag = cte_demoted
		}
		if = {
			limit = { trait = trained_warrior }
			remove_trait = trained_warrior
			add_trait = poor_warrior
			set_character_flag = cte_demoted
		}
		if = {
			limit = { trait = skilled_warrior }
			remove_trait = skilled_warrior
			add_trait = trained_warrior
			set_character_flag = cte_demoted
		}
		if = {
			limit = { trait = master_warrior }
			remove_trait = master_warrior
			add_trait = skilled_warrior
			set_character_flag = cte_demoted
		}
	}
}