﻿# Trigger which evaluates whether or not debug messaging for regula heresy is
# enabled.
regula_heresy_debug_enabled_trigger = {
	debug_only = yes
	exists = global_var:regula_heresy_debug_enabled
}

# Trigger which evaluates whether or not a regula heresy story cycle is
# valid / possible for the scoped character.
#
# Note: Specifically doesn't test whether heresy is already ongoing.
#
# scope = the character being evaluated.
regula_heresy_valid_trigger = {
	# Only magister may be targeted
	magister_alive_trigger = yes
	has_trait = magister_trait_group

	# Only independent rulers may be targeted.
	is_independent_ruler = yes

	# Heresy can't begin if the realm is too small in general. Specifically,
	# one must have at least `regula_heresy_minimum_dutchy_or_below_realm_size`
	# counties in the realm for heresy to occur. Alternatively, the if the
	# magister's tier is at least kingdom, then we can ignore the realm size
	# requirement.
	OR = {
		realm_size >= regula_heresy_minimum_dutchy_or_below_realm_size
		highest_held_title_tier >= tier_kingdom
	}

	# Heresy can't begin if there are too few sources of heresy relative to
	# the realm size. `regula_heresy_minimum_heretical_county_fraction` of the
	# realm must be a potential source of heresy for the story cycle to begin.
	any_realm_county = {
		percent > regula_heresy_minimum_heretical_county_fraction
		regula_heresy_potential_heresy_county_including_neighbors_trigger = yes
	}
}

# Trigger which evaluates whether heresy story cycles are blocked from starting
# either due to start of game delay or due to a previously completed heresy
# story cycle.
regula_heresy_blocked_trigger = {
	exists = global_var:regula_heresy_blocked
}

# Trigger which evaluates whether a regula heresy conspiracy story cycle is
# ongoing.
#
# scope = the character against whom heresy might be ongoing.
regula_heresy_ongoing_trigger = {
	owns_story_of_type = story_regula_heresy
}

# Trigger which evaluates whether the scoped heresy story is currently in a
# secret state in which members are being recruited & discontent is rising.
#
# scope = the heresy story
regula_heresy_story_in_secret_state_trigger = {
	OR = {
		var:regula_heresy_state = flag:conspiracy_hidden
		var:regula_heresy_state = flag:conspiracy_discovered
	}
}

# Trigger which evaluates whether a heresy story is currently in the secret
# state against the scoped character.
#
# scope = the target / owner of the heresy story
regula_heresy_story_against_character_in_secret_state_trigger = {
	any_owned_story = {
		story_type = story_regula_heresy
		regula_heresy_story_in_secret_state_trigger = yes
	}
}

# Trigger which evaluates whether the scoped heresy story is currently in a
# publicly known state.
#
# scope = the heresy story
regula_heresy_story_in_public_state_trigger = {
	var:regula_heresy_state = flag:faction_active
}

# Trigger which evaluates whether a heresy story is currently in a publicly
# known state against the scoped character.
#
# scope = the target / owner of the heresy story
regula_heresy_story_against_character_in_public_state_trigger = {
	any_owned_story = {
		story_type = story_regula_heresy
		regula_heresy_story_in_public_state_trigger = yes
	}
}

# Trigger which evaluates whether the scoped heresy story is currently in a
# terminal state.
#
# scope = the heresy story
regula_heresy_story_in_terminal_state_trigger = {
	OR = {
		var:regula_heresy_state = flag:faction_disband
		var:regula_heresy_state = flag:faction_defeat
		var:regula_heresy_state = flag:faction_peace
		var:regula_heresy_state = flag:faction_victory
		var:regula_heresy_state = flag:crushed
		var:regula_heresy_state = flag:disband
		var:regula_heresy_state = flag:silent_end
	}
}

# Trigger which evaluates whether the heresy conspiracy is at war with it's
# target.
#
# scope = the heresy conspiracy story
regula_heresy_at_war_trigger = {
	story_owner = {
		any_targeting_faction = {
			faction_is_at_war = yes
			regula_faction_is_heresy_faction_trigger = yes
		}
	}
}

# Trigger which evaluates whether or not the given character is a member of a
# regula heresy conspiracy.
#
# scope = the character being evaluated
regula_heresy_conspirator_character_trigger = {
	OR = {
		joined_faction ?= {
			regula_faction_is_heresy_faction_trigger = yes
		}
		any_secret = {
			secret_type = regula_heresy_conspirator_secret
		}
	}
}

# Trigger which evaluates whether the character knows of any conspirators
# heresy conspirators acting against them which could be apprehended.
#
# scope = the character target of the heresy conspiracy.
regula_heresy_any_capturable_conspirator_trigger = {
	OR = {
		any_targeting_secret = {
			secret_type = regula_heresy_conspirator_secret
			is_known_by = prev
			secret_owner = {
				is_imprisoned = no
			}
		}
		any_targeting_faction = {
			faction_is_at_war = no
			regula_faction_is_heresy_faction_trigger = yes
			any_faction_member = {
				is_imprisoned = no
			}
		}
	}
}

# Trigger which evaluates whether or not the given character could potentially
# join a heresy conspiracy at the present moment.
#
# This assumes (and thus doesn't check) that the character has a relation to
# the magister such that joining would make sense vassal, courtier, etc.
#
# Additionally, this doesn't check that the member is not already part of the
# heresy conspiracy. If both cases are needed, also test against
# `regula_heresy_conspirator_character_trigger`.
#
# scope = the character who may join a heresy scheme
regula_heresy_potential_heresy_character_trigger = {
	# Heresy can only happen while the magister is alive.
	magister_alive_trigger = yes

	# Only adults join heresy
	is_adult = yes

	# Devoted can't join heresy.
	is_regula_devoted_trigger = no

	# Can only join heresy if faith is at least hostile
	# (or we are secretly non-regula).
	OR = {
		AND = {
			is_regula_trigger = no
			faith = {
				faith_hostility_level = {
					target = global_var:magister_character.faith
					value >= faith_hostile_level
				}
			}
		}
		AND = {
			exists = secret_faith
			secret_faith = {
				is_regula_faith_trigger = no
				faith_hostility_level = {
					target = global_var:magister_character.faith
					value >= faith_hostile_level
				}
			}
		}
	}

	# Prevent barons from joining heresy conspiracy due to some implementation
	# complexities. In particular:
	# - Barons are `landed` but without occupiable titles, making wars drag on.
	# - Barons hold a single holding, not a county, so on victory, what should
	#   be done for their titles is unclear.
	# - Even more so, what should we do if a baron is part of a faction, but
	#   not the county which contains their holding?
	highest_held_title_tier != tier_barony

	# Can only join if dislikes the magister enough.
	opinion = {
		target = global_var:magister_character
		value < 0
	}

	# Court Memberships - not valid if we are a member of a court outside of
	# the magister's realm.
	trigger_if = {
		limit = {
			exists = liege_or_court_owner
		}

		liege_or_court_owner = {
			OR = {
				regula_heresy_conspirator_character_trigger = yes
				target_is_same_character_or_above = global_var:magister_character
			}
		}
	}

	# Imprisoning removes from the conspiracy, unless we are already at war.
	trigger_if = {
		limit = {
			NOT = {
				exists = joined_faction
				joined_faction ?= { exists = faction_war }
			}
		}

		is_imprisoned = no
	}
}

# Trigger which evaluates whether there are any available characters from
# within a character's realm who are eligible to join a heresy conspiracy.
#
# scope = the target character of the heresy
regula_heresy_any_potential_heresy_character_trigger = {
	OR = {
		any_vassal_or_below = {
			regula_heresy_potential_heresy_character_trigger = yes
			regula_heresy_conspirator_character_trigger = no
		}
		any_courtier = {
			regula_heresy_potential_heresy_character_trigger = yes
			regula_heresy_conspirator_character_trigger = no
			is_landed = no
		}
	}
}

# Trigger which evaluates whether or not the given county could potentially
# produce heretical individuals for a regula heresy scheme.
#
# Unlike regula_heresy_potential_heresy_county_including_neighbors_trigger,
# this trigger does not allow for heresy to be determined by including
# neighboring counties.
#
# scope = the county which may produce heretics
regula_heresy_potential_heresy_county_directly_trigger = {
	# Can produce heretics if faith is at least hostile.
	faith = {
		is_regula_faith_trigger = no
		faith_hostility_level = {
			target = global_var:magister_character.faith
			value >= faith_hostile_level
		}
	}

	# Counties which have recently been suppressed won't directly contribute to
	# heresy.
	NOT = {
		has_county_modifier = regula_impromptu_inspections_county_suppressed_modifier
	}
}

# Trigger which evaluates whether or not the given county could potentially
# produce heretical individuals for a regula heresy scheme.
#
# Unlike regula_heresy_potential_heresy_county_directly_trigger, this trigger
# also results in true if any neighboring counties are potential heresy
# counties.
#
# The tested county is assumed to be part of the magister's realm.
#
# scope = the county which may produce heretics
regula_heresy_potential_heresy_county_including_neighbors_trigger = {
	# Heretics can only come from counties which are non-regula or bordered by
	# non-regula.
	OR = {
		regula_heresy_potential_heresy_county_directly_trigger = yes
		any_title_to_title_neighboring_and_across_water_county = {
			regula_heresy_potential_heresy_county_directly_trigger = yes
		}
	}
}

# Trigger which evaluates whether there are any available counties from within
# a character's realm which are eligible to produce heretics for a heresy
# conspiracy.
#
# scope = the target character of the heresy
regula_heresy_any_potential_heresy_county_trigger = {
	any_realm_county = {
		regula_heresy_potential_heresy_county_including_neighbors_trigger = yes
	}
}
