﻿# Phase 4/7: Religious State
# The active model follows the lawgroup_church_and_state law directly, mirroring
# the 4 real laws in that group (see common/laws/00_church_and_state.txt in the
# vanilla game files):
#   law_state_religion and its parent-based Millet / People of the Book laws
#                                                     -> Confessional
#   law_freedom_of_conscience                             -> Freedom of Conscience
#   law_total_separation                                  -> Total Separation (true neutral)
#   law_state_atheism                                     -> State Atheism (aggressive secularization)
# Vanilla's law_state_atheism itself grants state_conversion_mult=+0.25 and force
# -converts 10% of every incorporated state to rel:atheist on activation; it is
# the aggressive model, not a tolerant one. law_total_separation is the one that
# disables every acceptance-gated conversion allowance (country_allow_conversion_
# <status>_bool = no for all 5 tiers) and gives flat unconditional acceptance -
# that is the truly neutral/tolerant model.

ve_is_confessional_state = {
	# `law_millet_system` and `law_people_of_the_book` inherit from
	# `law_state_religion` through `parent`, rather than being law variants.
	# They therefore need explicit checks; TUR starts with the former.
	OR = {
		has_law_or_variant = law_type:law_state_religion
		has_law = law_type:law_millet_system
		has_law = law_type:law_people_of_the_book
	}
}

ve_is_freedom_state = {
	has_law = law_type:law_freedom_of_conscience
}

ve_is_separation_state = {
	has_law = law_type:law_total_separation
}

ve_is_atheism_state = {
	has_law_or_variant = law_type:law_state_atheism
}

# Total Separation deliberately has no active/targeted lever - it is the model
# that suppresses conversion pressure entirely, matching its vanilla law.
ve_religious_project_enabled = {
	OR = {
		ve_is_confessional_state = yes
		ve_is_freedom_state = yes
		ve_is_atheism_state = yes
	}
}

ve_religious_program_affordable = {
	var:piety_reform_point >= 25
	NOT = { has_modifier = ve_religious_program_strain_mdf }
}

ve_has_religious_program_1 = {
	OR = {
		has_modifier = ve_confessional_program_1_mdf
		has_modifier = ve_freedom_program_1_mdf
		has_modifier = ve_separation_program_1_mdf
		has_modifier = ve_atheism_program_1_mdf
	}
}

ve_has_religious_program_2 = {
	OR = {
		has_modifier = ve_confessional_program_2_mdf
		has_modifier = ve_freedom_program_2_mdf
		has_modifier = ve_separation_program_2_mdf
		has_modifier = ve_atheism_program_2_mdf
	}
}

ve_has_religious_program_3 = {
	OR = {
		has_modifier = ve_confessional_program_3_mdf
		has_modifier = ve_freedom_program_3_mdf
		has_modifier = ve_separation_program_3_mdf
		has_modifier = ve_atheism_program_3_mdf
	}
}

ve_has_religious_program_4 = {
	OR = {
		has_modifier = ve_confessional_program_4_mdf
		has_modifier = ve_freedom_program_4_mdf
		has_modifier = ve_separation_program_4_mdf
		has_modifier = ve_atheism_program_4_mdf
	}
}

ve_has_religious_program_5 = {
	OR = {
		has_modifier = ve_confessional_program_5_mdf
		has_modifier = ve_freedom_program_5_mdf
		has_modifier = ve_separation_program_5_mdf
		has_modifier = ve_atheism_program_5_mdf
	}
}

ve_has_religious_program_6 = {
	OR = {
		has_modifier = ve_confessional_program_6_mdf
		has_modifier = ve_freedom_program_6_mdf
		has_modifier = ve_separation_program_6_mdf
		has_modifier = ve_atheism_program_6_mdf
	}
}

ve_has_religious_program_7 = {
	OR = {
		has_modifier = ve_confessional_program_7_mdf
		has_modifier = ve_freedom_program_7_mdf
		has_modifier = ve_separation_program_7_mdf
		has_modifier = ve_atheism_program_7_mdf
	}
}

ve_has_religious_program_8 = {
	OR = {
		has_modifier = ve_confessional_program_8_mdf
		has_modifier = ve_freedom_program_8_mdf
		has_modifier = ve_separation_program_8_mdf
		has_modifier = ve_atheism_program_8_mdf
	}
}

ve_can_adopt_any_religious_program = {
	ve_religious_program_affordable = yes
	OR = {
		NOT = { ve_has_religious_program_1 = yes }
		NOT = { ve_has_religious_program_2 = yes }
		NOT = { ve_has_religious_program_3 = yes }
		NOT = { ve_has_religious_program_4 = yes }
		NOT = { ve_has_religious_program_5 = yes }
		NOT = { ve_has_religious_program_6 = yes }
		NOT = { ve_has_religious_program_7 = yes }
		NOT = { ve_has_religious_program_8 = yes }
	}
}

# Phase 7: Targeted Religious Project (replaces the old missionary system).
# Mirrors common/scripted_triggers/ve_cultural_project_triggers.txt.

ve_has_active_religious_project = {
	has_variable = ve_religious_project_type
	var:ve_religious_project_type > 0
}

# 1 = Confessional Conversion, 2 = Freedom Outreach, 3 = Secularization Campaign
# (set by ve_begin_targeted_religious_project). Used by the Religion page to name
# and describe the running project.
ve_religious_project_type_is = {
	has_variable = ve_religious_project_type
	var:ve_religious_project_type == $TYPE$
}

ve_has_no_active_religious_project = {
	NOT = { ve_has_active_religious_project = yes }
}

# `var:ve_religious_project_target_state ?= { <state-only trigger> }` fails
# Jomini's static scope validation (logged as "Wrong scope for trigger: country,
# expected state") for triggers that require state scope specifically, such as
# turmoil or is_incorporated, even though the variable holds a valid state at
# runtime: the compiler cannot infer a bare variable's scope type ahead of time
# and falls back to the surrounding (country) type. Routing through a saved
# scope sidesteps this because save_scope_as is scope-type agnostic and the
# resulting scope: reference carries the runtime type correctly. Pass the
# state-only check as CHECK.
ve_religious_project_target_state_check = {
	AND = {
		var:ve_religious_project_target_state ?= {
			save_scope_as = ve_religious_project_target_state_check_scope
		}
		scope:ve_religious_project_target_state_check_scope ?= {
			$CHECK$
		}
	}
}

ve_can_begin_religious_project = {
	ve_has_no_active_religious_project = yes
	ve_religious_project_enabled = yes
	NOT = { has_modifier = ve_religious_project_backlash_mdf }
	# The core project clears immediately, while its JE resolves on the next
	# journal update. Prevent a new project from inheriting that pending JE.
	NOT = { has_journal_entry = ve_je_religious_project }
}

ve_can_begin_confessional_conversion = {
	ve_can_begin_religious_project = yes
	ve_is_confessional_state = yes
	var:piety_reform_point >= 125
}

ve_can_begin_freedom_outreach = {
	ve_can_begin_religious_project = yes
	ve_is_freedom_state = yes
	var:piety_reform_point >= 100
}

ve_can_begin_secularization_campaign = {
	ve_can_begin_religious_project = yes
	ve_is_atheism_state = yes
	var:piety_reform_point >= 150
}

ve_can_begin_religious_project_any = {
	OR = {
		ve_can_begin_confessional_conversion = yes
		ve_can_begin_freedom_outreach = yes
		ve_can_begin_secularization_campaign = yes
	}
}

# Scope: state. Room to push if the owner's own religious model isn't already
# dominant there - avoids needing an unverified "iterate every religion" list.
ve_state_has_religious_project_target = {
	owner = {
		ve_can_begin_religious_project_any = yes
	}
	NOT = {
		religion_percent_state = {
			target = owner.religion
			value >= 0.90
		}
	}
}

ve_religious_project_is_targeted = {
	has_variable = ve_religious_project_type
	var:ve_religious_project_type > 0
}

# Just the Piety Reform threshold for the active model, without the base
# no-active-project/backlash checks - used to itemize the "why is this
# disabled" tooltip on the state button instead of one flat requirements line.
ve_country_meets_religious_project_cost = {
	OR = {
		AND = {
			ve_is_confessional_state = yes
			var:piety_reform_point >= 125
		}
		AND = {
			ve_is_freedom_state = yes
			var:piety_reform_point >= 100
		}
		AND = {
			ve_is_atheism_state = yes
			var:piety_reform_point >= 150
		}
	}
}

# Scope: state. Is this state the one the owner's active project is running
# against? The state modifier is applied only by project start and removed on
# every project-clear path, so it is the authoritative state-side marker. Used
# to show a live progress bar directly on the state panel.
ve_state_is_active_religious_project_target = {
	has_modifier = ve_religious_project_active_mdf
	owner = {
		ve_has_active_religious_project = yes
	}
}
