﻿######################################
# Heresy system - scripted triggers  #
######################################
# Per-heresy gating, geography helpers, and component eligibility checks.
# Resurgence model: each historical heresy can fire more than once per game.
#   first outbreak: pam_heresy_X_first_fired global var is unset
#   resurgence:    first_fired set, plus pam_heresy_X_cooldown timed flag absent
# A 50-year cooldown is set after every outbreak via timed global variable.

# --- Component eligibility (faith scope) ---

# The second and third heresy components are blocked below this
# The first still fires, but we dampen the pulse with pam_heresy_pulse_no_event_value
pam_heresy_main_rite_substantial_trigger = {
	main_rite.rite_counties >= pam_heresy_main_rite_county_threshold_value
}

# Component 2 gate: main rite must be substantial (size floor) AND hold at least 60% of faith counties (dominance share).
pam_heresy_c2_main_rite_dominant_trigger = {
	pam_heresy_main_rite_substantial_trigger = yes
	main_rite.rite_counties >= pam_heresy_c2_main_rite_share_required_value
}

#pam_heresy_c3_eligible_trigger = {
#	religion = religion:christianity_religion
#	pam_heresy_main_rite_substantial_trigger = yes
#	OR = {
#		pam_heresy_lollard_eligible_trigger = yes
#		pam_heresy_hussite_eligible_trigger = yes
#		pam_heresy_joachimite_eligible_trigger = yes
#		pam_heresy_cathar_eligible_trigger = yes
#		pam_heresy_waldensian_eligible_trigger = yes
#		pam_heresy_bogomilist_eligible_trigger = yes
#		pam_heresy_paulician_eligible_trigger = yes
#		pam_heresy_iconoclast_eligible_trigger = yes
#		pam_heresy_adoptionist_eligible_trigger = yes
#		pam_heresy_messalian_eligible_trigger = yes
#	}
#}

# --- Per-heresy "is this heresy still available?" ---
# Each heresy can fire if past its earliest_date, the parent faith of the outbreak
# (root) is not the heresy faith itself, optional time windows pass, and the
# 50-year resurgence cooldown has elapsed since the last outbreak (if any).

#pam_heresy_lollard_eligible_trigger = {
#	current_date >= 1340.1.1
#	this != faith:lollard
#	pam_heresy_lollard_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_lollard_cooldown }
#}

#pam_heresy_hussite_eligible_trigger = {
#	current_date >= 1360.1.1
#	this != faith:hussite
#	pam_heresy_hussite_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_hussite_cooldown }
#}

#pam_heresy_joachimite_eligible_trigger = {
#	current_date >= 1200.1.1
#	this != faith:joachimite
#	pam_heresy_joachimite_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_joachimite_cooldown }
#}

#pam_heresy_cathar_eligible_trigger = {
#	current_date >= 1140.1.1
#	this != faith:cathar
#	pam_heresy_cathar_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_cathar_cooldown }
#}

#pam_heresy_waldensian_eligible_trigger = {
#	current_date >= 1160.1.1
#	this != faith:waldensian
#	pam_heresy_waldensian_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_waldensian_cooldown }
#}

#pam_heresy_bogomilist_eligible_trigger = {
#	current_date >= 950.1.1
#	this != faith:bogomilist
#	pam_heresy_bogomilist_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_bogomilist_cooldown }
#}

# Always-anonymous repeating outbreaks. No first-historical figure - founder is always a generic resurgence preacher.
#pam_heresy_paulician_eligible_trigger = {
#	current_date >= 870.1.1
#	this != faith:paulician
#	pam_heresy_paulician_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_paulician_cooldown }
#}

#pam_heresy_iconoclast_eligible_trigger = {
#	current_date >= 900.1.1
#	this != faith:iconoclast
#	pam_heresy_iconoclast_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_iconoclast_cooldown }
#}

# Adoptionism: Felix of Urgell died ~818, the Spanish argument fades after the first millennium - hard time-gate to before 1000.
#pam_heresy_adoptionist_eligible_trigger = {
#	current_date >= 870.1.1
#	current_date <= 999.12.31
#	this != faith:adoptionist
#	NOT = { exists = global_var:pam_heresy_adoptionist_cooldown }
#}

#pam_heresy_messalian_eligible_trigger = {
#	current_date >= 870.1.1
#	this != faith:messalian
#	pam_heresy_messalian_strict_window_ok_trigger = yes
#	NOT = { exists = global_var:pam_heresy_messalian_cooldown }
#}

# --- Strict-rule per-heresy historical windows ---
# Only enforced when game rule = strict_regional_heresy.
# The "strict" window is the historical lifetime of the named founder; outside that, strict mode rejects the outbreak entirely. 
# Always-anonymous heresies have no strict window (they're resurgence-flavoured by default).

#pam_heresy_lollard_strict_window_ok_trigger = {
#	OR = {
#		NOT = { has_game_rule = strict_regional_heresy }
#		current_date <= 1430.1.1
#	}
#}
#pam_heresy_hussite_strict_window_ok_trigger = {
#	OR = {
#		NOT = { has_game_rule = strict_regional_heresy }
#		current_date <= 1450.1.1
#	}
#}
# Joachimite: strict mode cuts off after Fra Dolcino's burning, as later Fraticelli are too institutional
#pam_heresy_joachimite_strict_window_ok_trigger = {
#	OR = {
#		NOT = { has_game_rule = strict_regional_heresy }
#		current_date <= 1310.1.1
#	}
#}
#pam_heresy_cathar_strict_window_ok_trigger = {
#	OR = {
#		NOT = { has_game_rule = strict_regional_heresy }
#		current_date <= 1230.1.1
#	}
#}
#pam_heresy_waldensian_strict_window_ok_trigger = {
#	OR = {
#		NOT = { has_game_rule = strict_regional_heresy }
#		current_date <= 1250.1.1
#	}
#}
#pam_heresy_bogomilist_strict_window_ok_trigger = {
#	OR = {
#		NOT = { has_game_rule = strict_regional_heresy }
#		current_date <= 1100.1.1
#	}
#}
# Always-anonymous: strict mode just lets them through (no historical figure to anchor).
pam_heresy_paulician_strict_window_ok_trigger = { always = yes }
pam_heresy_iconoclast_strict_window_ok_trigger = { always = yes }
pam_heresy_messalian_strict_window_ok_trigger = { always = yes }

# --- Resurgence detection (faith scope on the parent faith) ---
# Always-anonymous heresies skip this and set scope:is_resurgence in the router
#pam_heresy_lollard_is_resurgence_trigger = {
#	exists = global_var:pam_heresy_lollard_first_fired
#}
#pam_heresy_hussite_is_resurgence_trigger = {
#	exists = global_var:pam_heresy_hussite_first_fired
#}
#pam_heresy_joachimite_is_resurgence_trigger = {
#	exists = global_var:pam_heresy_joachimite_first_fired
#}
#pam_heresy_cathar_is_resurgence_trigger = {
#	exists = global_var:pam_heresy_cathar_first_fired
#}
#pam_heresy_waldensian_is_resurgence_trigger = {
#	exists = global_var:pam_heresy_waldensian_first_fired
#}
#pam_heresy_bogomilist_is_resurgence_trigger = {
#	exists = global_var:pam_heresy_bogomilist_first_fired
#}

# --- Pre-declaration vs already-declared (faith scope on the heresy faith) ---

pam_heresy_faith_is_pre_declaration_trigger = {
	has_variable = block_conversion_till_decision_taken
}

# True when the recipient is already on the heresy's rite.
# Used to gate "embrace" / reframe "reject" options and description text so the
# event does not address one of the heresy's own followers as an outside observer.
pam_heresy_player_is_heresy_insider_trigger = {
	trigger_if = {
		limit = { exists = scope:new_rite }
		rite = scope:new_rite
	}
	trigger_else_if = {
		limit = { exists = scope:heresy_faith }
		faith = scope:heresy_faith
	}
	trigger_else = {
		always = no
	}
}

# --- Geography helpers (province scope) ---
# Callers in county scope must wrap with title_province = { ... }.

#pam_heresy_lollard_region_trigger = {
#	OR = {
#		geographical_region = world_europe_west_britannia
#		geographical_region = world_europe_west_germania
#	}
#}
#pam_heresy_hussite_region_trigger = {
#	# Bohemia / Moravia sit inside world_europe_west_germania.
#	geographical_region = world_europe_west_germania
#}
#pam_heresy_joachimite_region_trigger = {
#	# Calabria, the Italian peninsula, Provence, and southern France
#	OR = {
#		geographical_region = world_europe_south_italy
#		geographical_region = world_europe_west_francia
#	}
#}
#pam_heresy_cathar_region_trigger = {
#	OR = {
#		geographical_region = world_europe_west_francia
#		geographical_region = world_europe_south_italy
#	}
#}
#pam_heresy_waldensian_region_trigger = {
#	OR = {
#		geographical_region = world_europe_west_francia
#		geographical_region = world_europe_south_italy
#	}
#}
#pam_heresy_bogomilist_region_trigger = {
#	geographical_region = world_europe_south_east
#}
#pam_heresy_paulician_region_trigger = {
#	geographical_region = world_asia_minor
#}
#pam_heresy_iconoclast_region_trigger = {
#	OR = {
#		geographical_region = world_asia_minor
#		geographical_region = world_europe_south_east
#	}
#}
#pam_heresy_adoptionist_region_trigger = {
#	geographical_region = world_europe_west_iberia
#}
#pam_heresy_messalian_region_trigger = {
#	geographical_region = world_middle_east
#}

# --- Component 2 founder eligibility (character scope) ---
# Mirrors is_eligible_rite_growth_founder_trigger but is permissive about existing heresiarch / herald carriers
# C2 is the "charismatic schismatic" path and those traits are an asset here.
# Archbishop-only candidates (no held county on the source rite) are accepted via the clerical_region branch.
is_eligible_pam_heresy_c2_founder_trigger = {
	is_available_ai_adult = yes
	rite = scope:source_rite
	learning >= medium_skill_rating
	NOR = {
		recently_converted_faith_trigger = yes
		has_character_flag = recently_converted_by_heresy
		has_character_flag = recently_converted_by_rite_growth
		faith.religious_head ?= this
		scope:source_rite.head_of_rite ?= this
		highest_held_title_tier >= tier_empire
		government_has_flag = government_is_nomadic
		government_has_flag = government_is_herder
	}
	OR = {
		AND = {
			is_landed = yes
			highest_held_title_tier >= tier_duchy
			any_held_title = {
				tier = tier_county
				rite = scope:source_rite
			}
		}
		any_held_title = { has_clerical_region = yes }
	}
}

# Test convenience trigger for narrative branching (e.g. event text).
pam_heresy_c2_founder_is_archbishop_trigger = {
	scope:founder = {
		any_held_title = { has_clerical_region = yes }
	}
}

# --- Epicenter ruler eligibility (character scope) ---
# A duke+ same-faith landed AI ruler.
# Capital-in-region check is applied separately by the picker (with title_province wrapping).
is_eligible_heresy_epicenter_ruler_trigger = {
	is_landed = yes
	is_ai = yes
	highest_held_title_tier >= tier_duchy
	NOR = {
		this = faith.religious_head
		has_trait = heresiarch
		has_trait = herald
		recently_converted_faith_trigger = yes
	}
}

# --- Component 3 game-rule gates ---

pam_heresy_relaxed_rule_trigger = {
	has_game_rule = relaxed_regional_heresy
}

pam_heresy_strict_rule_trigger = {
	has_game_rule = strict_regional_heresy
}

pam_heresy_default_rule_trigger = {
	OR = {
		has_game_rule = default_regional_heresy
		# Defensive: if no rule explicitly matches, treat as default.
		AND = {
			NOT = { has_game_rule = strict_regional_heresy }
			NOT = { has_game_rule = relaxed_regional_heresy }
		}
	}
}

# Province scope, branching on scope:heresy_faith
#pam_heresy_capital_in_heresy_region_trigger = {
#	OR = {
#		AND = {
#			scope:heresy_faith = faith:lollard
#			pam_heresy_lollard_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:hussite
#			pam_heresy_hussite_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:joachimite
#			pam_heresy_joachimite_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:cathar
#			pam_heresy_cathar_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:waldensian
#			pam_heresy_waldensian_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:bogomilist
#			pam_heresy_bogomilist_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:paulician
#			pam_heresy_paulician_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:iconoclast
#			pam_heresy_iconoclast_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:adoptionist
#			pam_heresy_adoptionist_region_trigger = yes
#		}
#		AND = {
#			scope:heresy_faith = faith:messalian
#			pam_heresy_messalian_region_trigger = yes
#		}
#	}
#}
