﻿# More Personality Depth — XP Calculator (childhood-locked design)
#
# This file defines mpd_calculate_trait_xp, the parameterized scripted effect
# that picks a level (L1/L2/L3) for a trait when added to a character. Called
# by mpd_roll_all_personality_xp dispatcher (see mpd_scripted_effects.txt).
#
# Architecture: a single random_list with 3 branches (L1=XP 1, L2=XP 50,
# L3=XP 100). Base weights 60/35/15 produce ~60% L1, ~35% L2, ~5% L3 for a
# character with no context matches. Each modifier shifts the weights by
# adding to the L2/L3 branches (boost upward) or subtracting from them.
#
# Why this pattern instead of a character-variable score: variable-based
# accumulation (set_variable + change_variable + var:X >= N threshold) works
# when called via every_living_character but proved unreliable when called
# from inside an event's immediate block (children received only XP=1 because
# the variable comparison apparently couldn't see the accumulated value).
# Direct random_list with weight modifiers is the vanilla pattern, definitely
# works in all contexts.
#
# Calculator is idempotent: skips traits already at XP >= 1.

mpd_calculate_trait_xp = {
	# Required parameters (CK3 scripted_effects don't support optional params):
	#   TRAIT      = trait_key (e.g. lustful, wrathful, compassionate)
	#   WN_SOCIAL  = score bonus when wet nurse is doing promote_relationships task
	#   WN_COMPETE = score bonus when wet nurse is doing promote_competition task
	#
	# Pass 0 for traits that should be neutral on a given wet nurse task.

	if = {
		limit = {
			has_trait = $TRAIT$
			NOT = { has_trait_xp = { trait = $TRAIT$ value >= 1 } }
		}

		random_list = {
			# ─── L1 (XP=1, Mild) ─── base 60
			60 = {
				add_trait_xp = { trait = $TRAIT$ value = 1 }
			}

			# ─── L2 (XP=50, Normal) ─── base 35, boosted by positive context
			35 = {
				# Culture
				modifier = { add = 20  mpd_culture_$TRAIT$_more_common_trigger = yes }
				modifier = { add = -15 mpd_culture_$TRAIT$_less_common_trigger = yes }
				# Faith virtue/sin
				modifier = { add = 25  rite = { trait_is_virtue_rite = $TRAIT$ } }
				modifier = { add = -15 rite = { trait_is_sin_rite = $TRAIT$ } }
				# Guardian / court tutor
				modifier = { add = 15  guardian_or_court_tutor_trait_trigger = { TRAIT = $TRAIT$ } }
				# Parent
				modifier = { add = 10  any_parent = { has_trait = $TRAIT$ } }
				# Guardian's deliberate "influence_child_personality" interaction
				modifier = { add = 35  has_variable = mpd_trait_from_guardian_influence }
				# Wet nurse — instill_virtue task active
				modifier = {
					add = 20
					court_owner ?= {
						# Guard: unset variable comparison logs 'Failed to fetch variable' errors
						has_variable = mpd_wn_active_task
						var:mpd_wn_active_task = flag:virtue
						any_court_position_holder = { type = wet_nurse_court_position }
					}
					rite = { trait_is_virtue_rite = $TRAIT$ }
				}
				modifier = {
					add = -15
					court_owner ?= {
						# Guard: unset variable comparison logs 'Failed to fetch variable' errors
						has_variable = mpd_wn_active_task
						var:mpd_wn_active_task = flag:virtue
						any_court_position_holder = { type = wet_nurse_court_position }
					}
					rite = { trait_is_sin_rite = $TRAIT$ }
				}
				# Wet nurse — relationships task active (per-call WN_SOCIAL)
				modifier = {
					add = $WN_SOCIAL$
					court_owner ?= {
						# Guard: unset variable comparison logs 'Failed to fetch variable' errors
						has_variable = mpd_wn_active_task
						var:mpd_wn_active_task = flag:relationships
						any_court_position_holder = { type = wet_nurse_court_position }
					}
				}
				# Wet nurse — competition task active (per-call WN_COMPETE)
				modifier = {
					add = $WN_COMPETE$
					court_owner ?= {
						# Guard: unset variable comparison logs 'Failed to fetch variable' errors
						has_variable = mpd_wn_active_task
						var:mpd_wn_active_task = flag:competition
						any_court_position_holder = { type = wet_nurse_court_position }
					}
				}
				add_trait_xp = { trait = $TRAIT$ value = 50 }
			}

			# ─── L3 (XP=100, Intense) ─── base 5, requires strong context
			5 = {
				modifier = { add = 15  mpd_culture_$TRAIT$_more_common_trigger = yes }
				modifier = { add = -10 mpd_culture_$TRAIT$_less_common_trigger = yes }
				modifier = { add = 15  rite = { trait_is_virtue_rite = $TRAIT$ } }
				modifier = { add = -10 rite = { trait_is_sin_rite = $TRAIT$ } }
				modifier = { add = 10  guardian_or_court_tutor_trait_trigger = { TRAIT = $TRAIT$ } }
				modifier = { add = 5   any_parent = { has_trait = $TRAIT$ } }
				modifier = { add = 25  has_variable = mpd_trait_from_guardian_influence }
				modifier = {
					add = 15
					court_owner ?= {
						# Guard: unset variable comparison logs 'Failed to fetch variable' errors
						has_variable = mpd_wn_active_task
						var:mpd_wn_active_task = flag:virtue
						any_court_position_holder = { type = wet_nurse_court_position }
					}
					rite = { trait_is_virtue_rite = $TRAIT$ }
				}
				modifier = {
					add = $WN_SOCIAL$
					court_owner ?= {
						# Guard: unset variable comparison logs 'Failed to fetch variable' errors
						has_variable = mpd_wn_active_task
						var:mpd_wn_active_task = flag:relationships
						any_court_position_holder = { type = wet_nurse_court_position }
					}
				}
				modifier = {
					add = $WN_COMPETE$
					court_owner ?= {
						# Guard: unset variable comparison logs 'Failed to fetch variable' errors
						has_variable = mpd_wn_active_task
						var:mpd_wn_active_task = flag:competition
						any_court_position_holder = { type = wet_nurse_court_position }
					}
				}
				add_trait_xp = { trait = $TRAIT$ value = 100 }
			}
		}

		# Clean up guardian_influence variable after use
		if = {
			limit = { has_variable = mpd_trait_from_guardian_influence }
			remove_variable = mpd_trait_from_guardian_influence
		}
	}
}
