﻿# Leo VI's Mass Vassal Directives - scripted effects
#
# Directive assignment matches vanilla give_vassal_directive_interaction
# on_accept exactly: remove_vassal_directives (vanilla scripted effect,
# 00_realm_effects.txt) followed by add_character_flag.
#
# Directive codes (stored in leo_mvd_managed and in each rule node's dir).
# 0 means no directive chosen. Settled vassals:
# 1 convert_faith, 2 convert_culture, 3 improve_development,
# 4 train_commanders, 5 build_maa, 6 improve_cultural_acceptance,
# 7 building_focus_fortification, 8 building_focus_military,
# 9 building_focus_economy. Nomads, who can be given no others:
# 10 manage_fertility, 11 explore_cultures, 12 raid_innovation_intent,
# 13 raid_herd_intent.
#
# The same numbers suffix the leo_mvd_ui_dir_N loc keys, which the panel builds
# at runtime - keep them aligned.
#
# Which directive a vassal gets is decided by the rule engine in
# leo_mvd_rules.txt; this file is the machinery around it.

# Current scope: vassal. Assign a directive and mark it as mod-managed.
# leo_mvd_hit records that this vassal has been claimed, so that the priorities
# after the one that assigned leave the vassal alone.
leo_mvd_assign_effect = {
	if = {
		limit = {
			NOT = { has_character_flag = vassal_directive_$DIRECTIVE$ }
		}
		remove_vassal_directives = yes
		add_character_flag = vassal_directive_$DIRECTIVE$
	}
	set_variable = {
		name = leo_mvd_managed
		value = $CODE$
	}
	set_variable = leo_mvd_hit
}

# Current scope: vassal. Clear the directive only if the mod still owns it.
# If the flag no longer matches the stored code, the player re-assigned it
# manually via the vanilla interaction - drop ownership, keep the directive.
leo_mvd_clear_stale_effect = {
	if = {
		limit = { has_variable = leo_mvd_managed }
		if = {
			limit = { leo_mvd_managed_matches_trigger = yes }
			remove_vassal_directives = yes
		}
		remove_variable = leo_mvd_managed
	}
}

# Current scope: vassal. Requires scope:leo_mvd_liege (the player).
# Run the liege's rule set over this vassal: each priority in turn until one
# assigns a directive. A vassal no priority claims keeps nothing the mod gave
# them earlier.
leo_mvd_evaluate_vassal_effect = {
	# If the player has re-assigned this vassal's directive by hand since the mod
	# last set it, respect that: exempt the vassal so automation leaves it be and
	# hand ownership back. This runs before the waterfall, so the manual choice is
	# never overwritten. Only vassals the mod was managing are auto-exempted - a
	# directive on one it never touched is not a change of ours to protect, and
	# the match check is nested so an unmanaged vassal's variable is never read.
	if = {
		limit = { has_variable = leo_mvd_managed }
		if = {
			limit = { NOT = { leo_mvd_managed_matches_trigger = yes } }
			set_variable = leo_mvd_exempt
			remove_variable = leo_mvd_managed
		}
	}

	# Run the rules, unless the check above just stood this vassal down.
	if = {
		limit = { NOT = { has_variable = leo_mvd_exempt } }
		if = {
			# Nomads have four directives of their own and can be given none of the
			# other nine, so they follow their own waterfall. The two sets of
			# directives divide the eligible vassals exactly, since every directive
			# below requires an administrative or a non-nomadic government.
			limit = { government_has_flag = government_is_nomadic }
			leo_mvd_eval_priority_effect = { P = q1 N = 1 COUNT = leo_mvd_qrule_count }
			leo_mvd_eval_priority_effect = { P = q2 N = 2 COUNT = leo_mvd_qrule_count }
			leo_mvd_eval_priority_effect = { P = q3 N = 3 COUNT = leo_mvd_qrule_count }
		}
		else = {
			leo_mvd_eval_priority_effect = { P = r1 N = 1 COUNT = leo_mvd_rule_count }
			leo_mvd_eval_priority_effect = { P = r2 N = 2 COUNT = leo_mvd_rule_count }
			leo_mvd_eval_priority_effect = { P = r3 N = 3 COUNT = leo_mvd_rule_count }
			leo_mvd_eval_priority_effect = { P = r4 N = 4 COUNT = leo_mvd_rule_count }
			leo_mvd_eval_priority_effect = { P = r5 N = 5 COUNT = leo_mvd_rule_count }
			leo_mvd_eval_priority_effect = { P = r6 N = 6 COUNT = leo_mvd_rule_count }
		}

		if = {
			limit = { has_variable = leo_mvd_hit }
			remove_variable = leo_mvd_hit
		}
		else = {
			leo_mvd_clear_stale_effect = yes
		}
	}
}

# Current scope: the player. Run the waterfall across all eligible vassals.
leo_mvd_run_waterfall_effect = {
	save_scope_as = leo_mvd_liege
	if = {
		limit = { government_has_mechanic = administrative }
		every_vassal_or_below = {
			limit = {
				leo_mvd_base_eligible_trigger = { LIEGE = scope:leo_mvd_liege }
			}
			leo_mvd_evaluate_vassal_effect = yes
		}
	}
	else = {
		every_vassal = {
			limit = {
				leo_mvd_base_eligible_trigger = { LIEGE = scope:leo_mvd_liege }
			}
			leo_mvd_evaluate_vassal_effect = yes
		}
	}
	# Then the vassals of the player's puppets, whom vanilla lets the player direct
	# through the puppet just as they direct their own. The rules are still the
	# player's and the conditions still measure the player: only who the vassal
	# must accept the directive from changes.
	leo_mvd_run_puppet_effect = { TYPE = theological_agent_puppet CAN_HAVE = can_have_realm_priest_puppet_trigger }
	leo_mvd_run_puppet_effect = { TYPE = external_theological_agent_puppet CAN_HAVE = can_have_theological_agent_puppet_trigger }
	leo_mvd_run_puppet_effect = { TYPE = external_ruler_puppet CAN_HAVE = can_have_external_ruler_puppet_trigger }
}

# Current scope: the player. Requires scope:leo_mvd_liege. Run the waterfall
# across the vassals the player can direct through their puppet of type $TYPE$
# (see the puppet list in leo_mvd_triggers.txt). The walk follows vanilla's
# is_shown with the puppet as the liege, everyone below an administrative
# puppet, but only direct vassals pass: vanilla's
# vassal_follows_directive_valid_trigger asks is_vassal_of for every government,
# so a sub-vassal is shown the directive and can never be given it.
leo_mvd_run_puppet_effect = {
	if = {
		limit = {
			exists = puppet:$TYPE$
			$CAN_HAVE$ = yes
		}
		puppet:$TYPE$ = {
			save_scope_as = leo_mvd_puppet
			if = {
				limit = { government_has_mechanic = administrative }
				every_vassal_or_below = {
					limit = { leo_mvd_puppet_eligible_trigger = yes }
					leo_mvd_evaluate_vassal_effect = yes
				}
			}
			else = {
				every_vassal = {
					limit = { leo_mvd_puppet_eligible_trigger = yes }
					leo_mvd_evaluate_vassal_effect = yes
				}
			}
		}
	}
}

# Current scope: the player. leo_mvd_remove_all_effect's sweep, over the vassals
# of the player's puppet of type $TYPE$.
leo_mvd_remove_all_puppet_effect = {
	if = {
		limit = { exists = puppet:$TYPE$ }
		puppet:$TYPE$ = {
			every_vassal_or_below = {
				limit = {
					OR = {
						has_variable = leo_mvd_managed
						has_variable = leo_mvd_exempt
					}
				}
				leo_mvd_clear_stale_effect = yes
				remove_variable = leo_mvd_exempt
			}
		}
	}
}

# Current scope: the player. Escape hatch: remove every mod-assigned directive
# and exemption, then disable the mod. Manually assigned directives are kept.
# Its puppets' vassals are swept too. A former puppet is out of reach: once the
# link is gone there is no way back to its vassals, just as there is none to a
# vassal who has left the realm.
leo_mvd_remove_all_effect = {
	every_vassal_or_below = {
		limit = {
			OR = {
				has_variable = leo_mvd_managed
				has_variable = leo_mvd_exempt
			}
		}
		leo_mvd_clear_stale_effect = yes
		remove_variable = leo_mvd_exempt
	}
	leo_mvd_remove_all_puppet_effect = { TYPE = theological_agent_puppet }
	leo_mvd_remove_all_puppet_effect = { TYPE = external_theological_agent_puppet }
	leo_mvd_remove_all_puppet_effect = { TYPE = external_ruler_puppet }
	remove_variable = leo_mvd_auto
}

# Current scope: the player. Which set of administration types this realm uses:
# 0 none, 1 Roads to Power's themes, 2 celestial, 3 meritocratic (and steppe
# administrative), 4 Ritsuryo.
#
# The player's own government decides it, because in an administrative realm the
# governors share it. Everything else that has to know - the option list, the
# preset, the labels - reads the family from here or asks the same triggers, so
# the panel can never offer a type under one government's name and match it
# under another's.
#
# The panel needs this as a number because a GUI binding cannot call a trigger:
# the Govern by Administration preset is hidden while this reads 0.
leo_mvd_set_admin_family_effect = {
	set_variable = { name = leo_mvd_admin_family value = 0 }
	if = {
		limit = { leo_mvd_admin_family_rtp_trigger = yes }
		set_variable = { name = leo_mvd_admin_family value = 1 }
	}
	else_if = {
		limit = { leo_mvd_admin_family_celestial_trigger = yes }
		set_variable = { name = leo_mvd_admin_family value = 2 }
	}
	else_if = {
		limit = { leo_mvd_admin_family_meritocratic_trigger = yes }
		set_variable = { name = leo_mvd_admin_family value = 3 }
	}
	else_if = {
		limit = { leo_mvd_admin_family_ritsuryo_trigger = yes }
		set_variable = { name = leo_mvd_admin_family value = 4 }
	}
}

# Current scope: the player. Idempotent config defaults.
#
# The default is deliberately no rules and no preset selected: the mod must
# never assign a directive the player did not ask for, so it does nothing at all
# until a preset is picked.
#
# Clearing the rule set writes every rule variable, so this one call is also
# what guarantees they all exist - which the panel depends on, since it reads
# them directly from GUI rather than through a scripted GUI.
leo_mvd_init_defaults_effect = {
	# The panel's dropdowns read their options out of variable lists, and it is
	# opened by a vanilla button - so it can be looked at in a session where no
	# script of ours has run yet. The lists survive in the save once built, so
	# this only has to catch the first time: a save made before they existed, or
	# a mod update that changed what the options are.
	leo_mvd_ensure_options_effect = yes

	if = {
		limit = { NOT = { has_variable = leo_mvd_preset } }
		leo_mvd_preset_0_effect = yes
	}
	# Editor state: which node the panel is writing to, and the pending write.
	if = {
		limit = { NOT = { has_variable = leo_mvd_edit_node } }
		set_variable = { name = leo_mvd_edit_node value = 0 }
		set_variable = { name = leo_mvd_stage_field value = 0 }
		set_variable = { name = leo_mvd_stage_value value = 0 }
	}
}
