﻿# Leo VI's Mass Vassal Directives - scripted triggers
#
# Eligibility must match vanilla exactly. Wherever possible we call the vanilla
# scripted triggers directly (00_vassal_stance_triggers.txt). The remaining
# conditions are verbatim mirrors of give_vassal_directive_interaction
# (00_vassal_interactions.txt:3592-3618 and its send_option blocks) because the
# interaction's inline is_shown/is_valid blocks cannot be invoked from script.
# Re-diff those mirrored lines against vanilla after each game patch.

# Current scope: vassal candidate. $LIEGE$ = the liege issuing directives.
#
# Verbatim mirror of give_vassal_directive_interaction's is_shown
# (00_vassal_interactions.txt:3592-3609) - the vassals vanilla will even offer a
# directive for. Re-diff this against vanilla after each game patch; it is the
# one block both the rules and the exempt interaction depend on, so a drift here
# is a drift everywhere.
#
# Vanilla writes the liege as scope:puppet_or_actor, because a ruler can also
# direct a puppet's vassals through that puppet. $LIEGE$ is always the player
# directing their own vassals, which is the case where that scope is the actor.
leo_mvd_directive_shown_trigger = {
	trigger_if = {
		limit = {
			$LIEGE$ = { government_has_mechanic = administrative }
		}
		is_vassal_or_below_of = $LIEGE$
	}
	trigger_else = {
		is_vassal_of = $LIEGE$
	}
	is_landed = yes
	NOT = { government_has_flag = government_is_herder }
	highest_held_title_tier >= tier_county
	is_ai = yes
}

# Current scope: vassal candidate. $LIEGE$ = the liege issuing directives.
# Everything vanilla asks before a directive may be given, plus the mod's own
# exemption. Mirrors the interaction's is_shown + is_valid +
# is_valid_showing_failures_only.
leo_mvd_base_eligible_trigger = {
	leo_mvd_directive_shown_trigger = { LIEGE = $LIEGE$ }
	# is_valid_showing_failures_only mirror (:3615-3618); the follows/valid
	# sub-triggers are both included in the vanilla composite below
	is_physically_able = yes
	# is_valid mirror - the canonical vanilla eligibility composite
	vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = $LIEGE$ }
	# mod-side: skip vassals the player exempted from automation
	NOT = { has_variable = leo_mvd_exempt }
}

### Puppets.
#
# By God Alone lets a ruler act through a puppet, and one of the things a puppet
# can be made to do is give its own vassals directives: vanilla's Give Vassal
# Directive then runs with the puppet as the liege (scope:puppet_or_actor). Three
# puppet types carry that action (puppet_action_give_vassal_directive in
# pam_puppet_types.txt), and a ruler holds at most one puppet of each, so every
# puppet a directive can go through is one of these three links:
#
#   theological_agent_puppet           - can_have_realm_priest_puppet_trigger
#   external_theological_agent_puppet  - can_have_theological_agent_puppet_trigger
#   external_ruler_puppet              - can_have_external_ruler_puppet_trigger
#
# Each is paired with its type's can_have. The engine removes a puppet that stops
# being valid, and vanilla does not say whether it also re-asks can_have before
# an action, so asking it here can only ever hold back a directive vanilla would
# allow, never let through one it would not.

# Current scope: vassal candidate. $ACTOR$ = the puppeteer, $TYPE$ = a puppet
# type from the list above, $CAN_HAVE$ = that type's can_have trigger.
# True when vanilla would offer the actor a directive for this vassal through
# that puppet. The exists comes first so the rest never touches a missing scope.
leo_mvd_puppet_shown_trigger = {
	exists = $ACTOR$.puppet:$TYPE$
	$ACTOR$ = { $CAN_HAVE$ = yes }
	leo_mvd_directive_shown_trigger = { LIEGE = $ACTOR$.puppet:$TYPE$ }
}

# Current scope: vassal candidate. Requires scope:leo_mvd_liege (the player) and
# scope:leo_mvd_puppet (the puppet being walked). Everything
# leo_mvd_base_eligible_trigger asks, with the puppet as the liege - which is who
# vanilla measures a directive's acceptance against when it goes through one.
#
# A vassal the player can direct in their own right is left to the player's own
# pass, so that nobody is run through the rules twice in one go. Asking the full
# eligibility rather than only whether the vassal is the player's means one the
# player cannot direct themselves, a rival say, is still reached through the
# puppet, as it could be by hand.
leo_mvd_puppet_eligible_trigger = {
	leo_mvd_base_eligible_trigger = { LIEGE = scope:leo_mvd_puppet }
	NOT = { leo_mvd_base_eligible_trigger = { LIEGE = scope:leo_mvd_liege } }
}

# Current scope: vassal candidate. $ACTOR$ = the player. True when the actor can
# direct this vassal through any of their puppets.
leo_mvd_puppet_directed_trigger = {
	OR = {
		leo_mvd_puppet_shown_trigger = { ACTOR = $ACTOR$ TYPE = theological_agent_puppet CAN_HAVE = can_have_realm_priest_puppet_trigger }
		leo_mvd_puppet_shown_trigger = { ACTOR = $ACTOR$ TYPE = external_theological_agent_puppet CAN_HAVE = can_have_theological_agent_puppet_trigger }
		leo_mvd_puppet_shown_trigger = { ACTOR = $ACTOR$ TYPE = external_ruler_puppet CAN_HAVE = can_have_external_ruler_puppet_trigger }
	}
}

# No current scope needed. $ACTOR$ = the player, $VASSAL$ = the vassal, $CHECK$ =
# one of vanilla's two directive triggers (vassal_follows_directive_trigger or
# vassal_follows_directive_valid_trigger). Asks $CHECK$ with whichever liege the
# actor would direct this vassal through: the actor themselves when they can,
# otherwise the puppet that can. This is how the exempt interaction grays out
# exactly where Give Vassal Directive would.
leo_mvd_directing_liege_check_trigger = {
	trigger_if = {
		limit = { $VASSAL$ = { leo_mvd_directive_shown_trigger = { LIEGE = $ACTOR$ } } }
		$CHECK$ = { VASSAL = $VASSAL$ LIEGE = $ACTOR$ }
	}
	trigger_else_if = {
		limit = { $VASSAL$ = { leo_mvd_puppet_shown_trigger = { ACTOR = $ACTOR$ TYPE = theological_agent_puppet CAN_HAVE = can_have_realm_priest_puppet_trigger } } }
		$CHECK$ = { VASSAL = $VASSAL$ LIEGE = $ACTOR$.puppet:theological_agent_puppet }
	}
	trigger_else_if = {
		limit = { $VASSAL$ = { leo_mvd_puppet_shown_trigger = { ACTOR = $ACTOR$ TYPE = external_theological_agent_puppet CAN_HAVE = can_have_theological_agent_puppet_trigger } } }
		$CHECK$ = { VASSAL = $VASSAL$ LIEGE = $ACTOR$.puppet:external_theological_agent_puppet }
	}
	trigger_else_if = {
		limit = { $VASSAL$ = { leo_mvd_puppet_shown_trigger = { ACTOR = $ACTOR$ TYPE = external_ruler_puppet CAN_HAVE = can_have_external_ruler_puppet_trigger } } }
		$CHECK$ = { VASSAL = $VASSAL$ LIEGE = $ACTOR$.puppet:external_ruler_puppet }
	}
	trigger_else = {
		always = no
	}
}

### Per-directive gates.
#
# One trigger per directive, each a verbatim mirror of that directive's
# send_option block in give_vassal_directive_interaction. These are pure
# vanilla: whose faith or culture a vassal should be steered toward is a
# targeting choice, expressed as a rule condition below, and is never baked in
# here.
#
# A vassal who fails the gate for the directive a rule picked is left alone and
# falls through to the next priority, which is what vanilla does when a
# send_option is unavailable.

# Current scope: vassal. improve_development (:3620)
leo_mvd_gate_improve_development_trigger = {
	government_has_mechanic = administrative
	highest_held_title_tier >= tier_duchy
}

# Current scope: vassal. train_commanders (:3634)
leo_mvd_gate_train_commanders_trigger = {
	government_has_mechanic = administrative
}

# Current scope: vassal. build_maa (:3645)
leo_mvd_gate_build_maa_trigger = {
	government_has_mechanic = administrative
}

# Current scope: vassal. convert_faith (:3656)
leo_mvd_gate_convert_faith_trigger = {
	save_temporary_scope_as = leo_mvd_vassal_tmp
	NOT = { government_has_flag = government_is_nomadic }
	any_sub_realm_county = {
		faith != scope:leo_mvd_vassal_tmp.faith
	}
}

# Current scope: vassal. convert_culture (:3675)
leo_mvd_gate_convert_culture_trigger = {
	save_temporary_scope_as = leo_mvd_vassal_tmp
	NOT = { government_has_flag = government_is_nomadic }
	any_sub_realm_county = {
		culture != scope:leo_mvd_vassal_tmp.culture
	}
}

# Current scope: vassal. improve_cultural_acceptance (:3694)
leo_mvd_gate_improve_cultural_acceptance_trigger = {
	save_temporary_scope_as = leo_mvd_vassal_tmp
	NOT = { government_has_flag = government_is_nomadic }
	highest_held_title_tier >= tier_duchy
	any_sub_realm_county = {
		culture != scope:leo_mvd_vassal_tmp.culture
	}
}

# Current scope: vassal. building_focus_fortification (:3714),
# building_focus_military (:3725) and building_focus_economy (:3736) all share
# the same single vanilla condition.
leo_mvd_gate_building_focus_trigger = {
	NOT = { government_has_flag = government_is_nomadic }
}

### Nomad directives.
#
# The four directives only nomads can be given. Every directive above requires
# either an administrative government or a non-nomadic one, so these and those
# divide the eligible vassals exactly between them - which is why the nomad
# rules are their own waterfall rather than a special case inside the main one.
#
# Herders are not part of this: vanilla's is_shown excludes them from directives
# altogether (:3605), and leo_mvd_base_eligible_trigger mirrors that.

# Current scope: vassal. manage_fertility (:3747). Vanilla asks nothing further.
leo_mvd_gate_manage_fertility_trigger = {
	government_has_flag = government_is_nomadic
}

# Current scope: vassal. explore_cultures (:3757)
leo_mvd_gate_explore_cultures_trigger = {
	save_temporary_scope_as = leo_mvd_vassal_tmp
	government_has_flag = government_is_nomadic
	any_sub_realm_county = {
		NOT = { culture = scope:leo_mvd_vassal_tmp.culture }
	}
}

# Current scope: vassal. raid_innovation_intent (:3775)
#
# Vanilla also refuses this to a vassal who already has it - alone among the
# thirteen. That is a menu nicety: it greys out an option that would do nothing.
# Mirrored literally it would mean the mod could never leave the directive in
# place - the vassal holding it would fail their own rule, fall through, and
# have it taken off them a month after it was given. What the rules can reach is
# unchanged, since a vassal with this directive is one vanilla would allow; only
# the pointless re-offer is dropped.
leo_mvd_gate_raid_innovation_intent_trigger = {
	government_has_flag = government_is_nomadic
}

# Current scope: vassal. raid_herd_intent (:3793). Vanilla asks nothing further.
leo_mvd_gate_raid_herd_intent_trigger = {
	government_has_flag = government_is_nomadic
}

### Administrative government families.
#
# Every administrative government gives its governors an administration type on
# their contract, but each one has its own set of types with its own flags:
# Roads to Power's themes, and the celestial, meritocratic (shared with steppe
# administrative) and Ritsuryo province types from All Under Heaven. The sets
# overlap by name, not by flag - Military exists in all of them, Standard and
# Industrial in the three All Under Heaven ones.
#
# These name the governments behind each set. The panel offers a player the row
# their own government uses and labels it with that government's own names, and
# the preset loads the rules that fit it.
#
# Current scope: a character, normally the player.
leo_mvd_admin_family_rtp_trigger = {
	OR = {
		has_government = administrative_government
	}
}

leo_mvd_admin_family_celestial_trigger = {
	has_government = celestial_government
}

# One set, two governments.
leo_mvd_admin_family_meritocratic_trigger = {
	OR = {
		has_government = meritocratic_government
		has_government = steppe_admin_government
	}
}

leo_mvd_admin_family_ritsuryo_trigger = {
	has_government = japan_administrative_government
}

# Any of them. What the panel asks before offering administration types at all.
leo_mvd_admin_family_any_trigger = {
	OR = {
		leo_mvd_admin_family_rtp_trigger = yes
		leo_mvd_admin_family_celestial_trigger = yes
		leo_mvd_admin_family_meritocratic_trigger = yes
		leo_mvd_admin_family_ritsuryo_trigger = yes
	}
}

### Rule conditions.
#
# The closed set of conditions a rule can branch on, evaluated in the vassal's
# scope with the liege as scope:leo_mvd_liege. Script cannot dispatch on a
# condition chosen at runtime, so this is a single value-driven chain rather
# than one trigger per node.
#
# Every numeric condition reads "is at least" (>=) so the true branch is always
# the high side; no condition gets its own comparison direction.
#
# Requires scope:leo_mvd_cond_id (the condition's code) and scope:leo_mvd_thresh
# (its threshold, 0 for the boolean conditions), both staged by
# leo_mvd_stage_cond_effect.
leo_mvd_cond_holds_trigger = {
	save_temporary_scope_as = leo_mvd_vassal_tmp

	# 1 - Faith is Yours
	trigger_if = {
		limit = { scope:leo_mvd_cond_id = 1 }
		faith = scope:leo_mvd_liege.faith
	}
	# 2 - Culture is Yours
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 2 }
		culture = scope:leo_mvd_liege.culture
	}
	# 3 - Holds Counties of Another Faith
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 3 }
		any_sub_realm_county = {
			faith != scope:leo_mvd_vassal_tmp.faith
		}
	}
	# 4 - Holds Counties of Another Culture
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 4 }
		any_sub_realm_county = {
			culture != scope:leo_mvd_vassal_tmp.culture
		}
	}
	# 5 - Same Dynasty as You
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 5 }
		has_dynasty = yes
		scope:leo_mvd_liege = { has_dynasty = yes }
		dynasty = scope:leo_mvd_liege.dynasty
	}
	# 6 - Administrative Government
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 6 }
		government_has_mechanic = administrative
	}
	# 7 - Is a Powerful Vassal
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 7 }
		is_powerful_vassal_of = scope:leo_mvd_liege
	}
	# 8 - Is on Your Council
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 8 }
		is_councillor_of = scope:leo_mvd_liege
	}
	# 9 - Military Strength is at least. The chosen value is the duchy baseline;
	# leo_mvd_effective_threshold scales it by the vassal's tier.
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 9 }
		max_military_strength >= leo_mvd_effective_threshold
	}
	# 10 - Title Tier is at least. Compared against the named tier constants so
	# their numeric mapping never has to be assumed.
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 10 }
		trigger_if = {
			limit = { scope:leo_mvd_thresh = 2 }
			highest_held_title_tier >= tier_county
		}
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 3 }
			highest_held_title_tier >= tier_duchy
		}
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 4 }
			highest_held_title_tier >= tier_kingdom
		}
		trigger_else = {
			highest_held_title_tier >= tier_empire
		}
	}
	# 11 - Capital Development is at least
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 11 }
		exists = capital_county
		capital_county = {
			development_level >= scope:leo_mvd_thresh
		}
	}
	# 12 - Opinion of You is at least
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 12 }
		opinion = {
			target = scope:leo_mvd_liege
			value >= scope:leo_mvd_thresh
		}
	}
	# 13 - Counties Held is at least
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 13 }
		any_held_county = {
			count >= scope:leo_mvd_thresh
		}
	}
	# 14 - Cultural Acceptance with You is at least
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 14 }
		culture = {
			cultural_acceptance = {
				target = scope:leo_mvd_liege.culture
				value >= scope:leo_mvd_thresh
			}
		}
	}
	# 19 - Same House as You. Narrower than condition 5: this is the one house,
	# not the dynasty and its cadet branches. Both sides are guarded because a
	# lowborn has no house and an unguarded scope comparison writes to the log.
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 19 }
		exists = house
		exists = scope:leo_mvd_liege.house
		house = scope:leo_mvd_liege.house
	}
	# 20 - Administration Type is. The only condition whose picked value selects a
	# value rather than setting a floor: administration types are unordered, so
	# this reads as equality where every other measured condition reads "is at
	# least". vassal_contract_has_flag is a flag query on the contract, not a
	# scope change, so it simply answers no for a vassal who is not a governor.
	#
	# A value is a type's name, not one government's flag. Each administrative
	# government has its own set (see the family triggers above), and where two of
	# them use the same name they are the same question asked of a different
	# contract - so each value matches every flag of that name. Which values a
	# player is offered is decided in the panel's option list; matching all of them
	# here is what keeps a rule working in a realm that holds governors of more
	# than one government, and when the liege's own government changes.
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 20 }
		is_governor = yes
		# Balanced - Roads to Power's default.
		trigger_if = {
			limit = { scope:leo_mvd_thresh = 1 }
			vassal_contract_has_flag = admin_theme_balanced
		}
		# Civilian
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 2 }
			OR = {
				vassal_contract_has_flag = admin_theme_civilian
			}
		}
		# Military - the one type every administrative government has.
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 3 }
			OR = {
				vassal_contract_has_flag = admin_theme_military
				vassal_contract_has_flag = celestial_province_military
				vassal_contract_has_flag = meritocratic_province_military
				vassal_contract_has_flag = japan_administrative_province_military
			}
		}
		# Frontier
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 4 }
			vassal_contract_has_flag = admin_theme_frontier
		}
		# Imperial
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 5 }
			vassal_contract_has_flag = admin_theme_imperial
		}
		# Naval
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 6 }
			vassal_contract_has_flag = admin_theme_naval
		}
		# Standard - the default of all three All Under Heaven sets.
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 7 }
			OR = {
				vassal_contract_has_flag = celestial_province_standard
				vassal_contract_has_flag = meritocratic_province_standard
				vassal_contract_has_flag = japan_administrative_province_standard
			}
		}
		# Industrial. Ritsuryo names the flag for trade and the type for industry,
		# so there the flag and the name it is offered under differ.
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 8 }
			OR = {
				vassal_contract_has_flag = celestial_province_industrial
				vassal_contract_has_flag = meritocratic_province_industrial
				vassal_contract_has_flag = japan_administrative_province_trade
			}
		}
		# Metropolitan - celestial only.
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 9 }
			vassal_contract_has_flag = celestial_province_metropolitan
		}
		# Protectorate - celestial only.
		trigger_else_if = {
			limit = { scope:leo_mvd_thresh = 10 }
			vassal_contract_has_flag = celestial_province_protectorate
		}
		# No type picked, or one this build does not know: answered no rather
		# than falling into whichever branch happened to be last.
		trigger_else = {
			always = no
		}
	}
	# 21 - Average Development is at least. Averaged over the counties the
	# vassal holds personally: directives are not passed down to sub-vassals,
	# so those are the counties a directive could actually improve. The average
	# is staged as a scope first because a computed value cannot stand on the
	# left of a comparison by itself. A vassal holding no county (a baron) is
	# answered no outright, which also keeps the average's divisor above zero.
	trigger_else_if = {
		limit = { scope:leo_mvd_cond_id = 21 }
		any_held_county = { always = yes }
		save_temporary_scope_value_as = {
			name = leo_mvd_avg_dev
			value = leo_mvd_avg_held_development
		}
		scope:leo_mvd_avg_dev >= scope:leo_mvd_thresh
	}
	# No condition picked yet: the branch is inert rather than silently true.
	trigger_else = {
		always = no
	}
}

# Current scope: vassal with leo_mvd_managed set.
# True when the directive flag currently on the vassal is the one the mod assigned.
leo_mvd_managed_matches_trigger = {
	OR = {
		AND = {
			var:leo_mvd_managed = 1
			has_character_flag = vassal_directive_convert_faith
		}
		AND = {
			var:leo_mvd_managed = 2
			has_character_flag = vassal_directive_convert_culture
		}
		AND = {
			var:leo_mvd_managed = 3
			has_character_flag = vassal_directive_improve_development
		}
		AND = {
			var:leo_mvd_managed = 4
			has_character_flag = vassal_directive_train_commanders
		}
		AND = {
			var:leo_mvd_managed = 5
			has_character_flag = vassal_directive_build_maa
		}
		AND = {
			var:leo_mvd_managed = 6
			has_character_flag = vassal_directive_improve_cultural_acceptance
		}
		AND = {
			var:leo_mvd_managed = 7
			has_character_flag = vassal_directive_building_focus_fortification
		}
		AND = {
			var:leo_mvd_managed = 8
			has_character_flag = vassal_directive_building_focus_military
		}
		AND = {
			var:leo_mvd_managed = 9
			has_character_flag = vassal_directive_building_focus_economy
		}
		AND = {
			var:leo_mvd_managed = 10
			has_character_flag = vassal_directive_manage_fertility
		}
		AND = {
			var:leo_mvd_managed = 11
			has_character_flag = vassal_directive_explore_cultures
		}
		AND = {
			var:leo_mvd_managed = 12
			has_character_flag = vassal_directive_raid_innovation_intent
		}
		AND = {
			var:leo_mvd_managed = 13
			has_character_flag = vassal_directive_raid_herd_intent
		}
	}
}
