﻿# Phase 1 compatibility bridge.
# Each resource has its own monthly update entry point so later migrations can
# replace one system without changing the dispatch or the other three systems.
# The legacy variables and script values are intentionally preserved here.

ve_update_religious_state = {
	ve_initialize_or_transition_religious_model = yes
	ve_update_religious_project = yes
	change_variable = {
		name = piety_bar_point
		add = ve_monthly_religious_state
	}
	ve_clamp_religious_state = yes
	change_variable = {
		name = piety_reform_point
		add = ve_monthly_piety_reform
	}
	ve_clamp_piety_reform = yes
	ve_sync_religious_state_if_needed = yes
}

ve_clamp_national_insight = {
	if = {
		limit = { var:idea_point_pool < 0 }
		set_variable = {
			name = idea_point_pool
			value = 0
		}
	}
	else_if = {
		limit = { var:idea_point_pool > 1000 }
		set_variable = {
			name = idea_point_pool
			value = 1000
		}
	}
}

ve_update_national_insight = {
	if = {
		limit = { var:idea_point_pool < 1000 }
		change_variable = {
			name = idea_point_pool
			add = ve_monthly_national_insight
		}
	}
	ve_clamp_national_insight = yes
}

ve_update_national_identity = {
	ve_initialize_national_identity = yes
	ve_update_cultural_project = yes
	change_variable = {
		name = culture_reform_point
		add = ve_monthly_national_identity
	}
	ve_clamp_cultural_mandate = yes
	change_variable = {
		name = ve_national_cohesion
		add = ve_monthly_national_cohesion
	}
	ve_clamp_national_cohesion = yes
	ve_sync_identity_if_needed = yes
}

ve_update_era_momentum = {
	ve_initialize_era_momentum = yes
	ve_reconcile_era_objective_count = yes
	ve_update_era_objective_completions = yes
	change_variable = {
		name = ve_splender_points
		add = ve_monthly_era_momentum
	}
}

ve_monthly_points = {
	ve_update_religious_state = yes
	ve_update_national_insight = yes
	ve_update_national_identity = yes
	ve_update_era_momentum = yes
}

# Native pulse entry points. Resource accounting remains monthly, while
# strategic AI choices and state reconciliation run only twice per year.
ve_monthly_country_tick = {
	# Cheap save-compatibility fallback for countries created by an engine path
	# that does not expose a dedicated country-release hook.
	if = {
		limit = {
			NOT = { has_variable = victoria_universalis_system_on }
		}
		ve_initialize_country_system = yes
	}
	# Save migration for the post-refactor Agrarian group. Adoption also sets
	# the value, but initializing it here keeps old-save GUI/script reads numeric.
	if = {
		limit = {
			NOT = { has_variable = agrarian_idea }
		}
		set_variable = {
			name = agrarian_idea
			value = 0
		}
	}
	ve_monthly_points = yes
	# Save-compatible fallback if a loaded country somehow missed the lobby
	# initializer that creates the uncapped doctrine-investment total.
	if = {
		limit = {
			NOT = { has_variable = ve_idea_capacity_progress }
		}
		ve_recalculate_national_idea_pool = yes
	}
	# Cheap reconciliation for age-gated doctrine capacity. Purchases update
	# capacity immediately; this catches an Age transition within one month.
	ve_update_idea_group_capacity = yes
	if = {
		limit = { is_ai = yes }
		ve_ai_monthly_idea_tick = yes
	}
}

ve_half_yearly_country_tick = {
	if = {
		limit = { is_ai = yes }
		ve_ai_half_yearly_strategy_tick = yes
	}
}

ve_yearly_country_tick = {
	ve_idea_cap_effects = yes
}
