﻿# More Personality Depth — on_actions
#
# Trait levels are set once, by context (the "childhood-locked" design):
#   1. At game start — initial roll for every existing character.
#   2. On vanilla childhood personality events — via child_personality_gain.
#   3. Catch-up nets for traits added by unhooked sources (AGOT events,
#      vanilla decisions, console, other mods): character-window view-hook
#      (common/scripted_guis/mpd_view_hook.txt, instant on display), the
#      touchpoints below (pool arrival / join court / birth), and a yearly
#      birthday sweep. The track's tier-0 entry means a freshly added trait
#      displays and applies L1 immediately while awaiting its roll.
#
# The adult progression system (hidden counters + shift events) was REMOVED
# in v0.5.0: 9/36 coverage, invisible state, exact-match one-shot thresholds
# and debated cause-effect logic (losing battles upgrading craven, etc.) made
# it neither clear nor consistent. Levels now come from upbringing, context,
# or the player's ruler designer picks (mpd_rd_* markers).
#
# The XP roller (mpd_roll_all_personality_xp) is idempotent: characters with
# trait XP >= 1 are skipped at zero cost, so every hook below is safe to fire
# repeatedly.

# on_game_start (not _after_lobby): the lobby's character selection browser
# shows a live gamestate, so levels must already be rolled there — with the
# after-lobby hook every character displayed as Mild (tier 0) in the browser.
on_game_start = {
	on_actions = {
		mpd_distribute_initial_xp
	}
}

# Characters generated after on_game_start (character pool, generated
# courtiers, etc.) were missed by the initial roll and stayed at tier 0 until
# their birthday (~1,200 characters in a 1066 start). One day after the lobby,
# a hidden event sweeps every character still holding an un-rolled trait; the
# 1-day delay guarantees vanilla's after-lobby generation has already run.
on_game_start_after_lobby = {
	on_actions = {
		mpd_post_lobby_sweep
	}
}

# Hook on the vanilla childhood personality dispatcher. Runs every time vanilla
# fires one of its child_personality.000X events. Triggers our hidden mpd_xp.0001
# event with a 1-day delay so the vanilla event has time to resolve and the
# trait is added before we try to roll its level.
child_personality_gain = {
	on_actions = {
		mpd_post_childhood_event
	}
}

# Universal yearly catch-up: fires for every character on their birthday and
# rolls XP for any personality trait still at XP=0, after converting any stray
# ruler designer level markers.
on_birthday = {
	on_actions = {
		mpd_birthday_xp_catchup
		mpd_birthday_validate_wn_flag
	}
}

# ─── View-relevance touchpoints (v0.4.1) ───
# Shorten the window between a trait being added by an unhooked source and its
# level being rolled, at the moments a character enters the player's orbit.

# Guests/wanderers arriving from the character pool. Root = the guest.
on_guest_arrived_from_pool = {
	on_actions = {
		mpd_pool_arrival_xp_catchup
	}
}

# Any character joining a court (new courtiers become visible to the player).
# Root = the joining character.
on_join_court = {
	on_actions = {
		mpd_join_court_xp_catchup
	}
}

# Newborns: AGOT canon-children and house-blood effects can add personality
# traits at birth, outside the vanilla childhood pipeline. Root = the child.
on_birth_child = {
	on_actions = {
		mpd_birth_xp_catchup
	}
}

# ─── Ruler designer marker conversion ───
# Root = the freshly created designed character. Converts mpd_rd_* level
# markers into the real trait at the chosen XP (see
# common/scripted_effects/mpd_rd_marker_effects.txt).
on_ruler_designer_finished = {
	on_actions = {
		mpd_rd_marker_conversion
	}
}

# ─── Custom on_actions ───

# Distributes initial trait XP to every existing character at game start.
mpd_distribute_initial_xp = {
	effect = {
		every_living_character = {
			mpd_roll_all_personality_xp = yes
		}
	}
}

# Triggered after each vanilla child_personality_gain dispatch. Fires the
# hidden mpd_xp.0001 event with a 1-day delay (see events/mpd_childhood_xp_events.txt).
# The delay ensures the vanilla childhood event has resolved and any newly-added
# personality trait is in place before our roller computes its level.
mpd_post_childhood_event = {
	effect = {
		trigger_event = {
			id = mpd_xp.0001
			days = 1
		}
	}
}

# Annual safety net: catches personality traits added to adults via vanilla
# events/decisions/perks that don't have a clean on_action hook. Worst-case
# delay between trait acquisition and level rolling is 1 year (until next
# birthday) — and the tier-0 track entry keeps L1 active in the meantime.
mpd_birthday_xp_catchup = {
	effect = {
		mpd_convert_rd_marker_traits = yes
		mpd_roll_all_personality_xp = yes
	}
}

# Yearly cleanup of stale wet-nurse-task flags. The wet nurse task overrides
# (common/court_positions/tasks/00_wet_nurse_tasks.txt) set mpd_wn_active_task
# on scope:liege via on_start, and remove it via on_end. But edge cases can
# leave a stale flag:
#   - Engine fails to fire on_end (rare bug)
#   - Lord inherits a court with an active wet nurse but on_start doesn't re-fire
# This cleanup removes the flag if the lord has it but no actual wet nurse is
# in court — prevents stale state from giving phantom XP bonuses.
mpd_birthday_validate_wn_flag = {
	effect = {
		if = {
			limit = {
				has_variable = mpd_wn_active_task
				NOT = { any_court_position_holder = { type = wet_nurse_court_position } }
			}
			remove_variable = mpd_wn_active_task
		}
	}
}

mpd_pool_arrival_xp_catchup = {
	effect = {
		mpd_roll_all_personality_xp = yes
	}
}

mpd_join_court_xp_catchup = {
	effect = {
		mpd_roll_all_personality_xp = yes
	}
}

mpd_birth_xp_catchup = {
	effect = {
		mpd_roll_all_personality_xp = yes
	}
}

mpd_rd_marker_conversion = {
	effect = {
		mpd_convert_rd_marker_traits = yes
	}
}

mpd_post_lobby_sweep = {
	effect = {
		random_living_character = {
			limit = { is_ruler = yes }
			trigger_event = {
				id = mpd_xp.0002
				days = 1
			}
		}
	}
}
