﻿# Story cycle which encapsulates a hidden conspiracy against the magister by
# heretics within their own realm.
story_regula_heresy = {
	on_setup = {
		# State flag used to control behavior of the story.
		#
		# Valid Values:
		# flag:conspiracy_hidden - the story is in it's starting, hidden state.
		# flag:conspiracy_discovered - the story in it's main, active state.
		# flag:faction_active - the conspirators have formed a faction.
		# flag:faction_disband - the faction disbanded due to high threat (while conspiracy was publicly known).
		# flag:faction_defeat - the conspirators faction has been defeated.
		# flag:faction_peace - the conspirators faction ended in white peace.
		# flag:faction_victory - the conspirators faction was victorious.
		# flag:crushed - all of the conspiracy members were apprehended.
		# flag:disband - the faction disbanded due to high percieved threat (while conspiracy was still secret).
		# flag:silent_end - state to capture silent endings (non-notified).
		set_variable = {
			name = regula_heresy_state
			value = flag:conspiracy_hidden
		}

		# Tracker for growth of the conspiracy.
		# Once this reaches 200, an additional member will be added to the
		# conspiracy.
		set_variable = {
			name = regula_heresy_growth
			value = 0
		}

		# Tracker for discontent of the conspiracy.
		# Once this reaches 100, the conspiracy will break into open opposition
		# (aka regula_heresy_state => flag:faction_active).
		set_variable = {
			name = regula_heresy_discontent
			value = 0
		}

		# Tracker for discontent of the conspiracy.
		# Once this reaches 100, the disband due to high percieved threat.
		# (aka regula_heresy_state => flag:disband).
		set_variable = {
			name = regula_heresy_threat
			value = 0
		}
	}

	on_end = {
		# Flag all remaining known heretics as criminals.
		regula_heresy_flag_known_members_as_criminals_effect = yes

		# Send notifications that the story cycle has ended.
		regula_heresy_send_end_event_effect = yes

		# Clean up any no longer needed characters.
		regula_heresy_cleanup_generated_conspirators_effect = yes

		if = {
			limit = {
				var:regula_heresy_state = flag:faction_peace
			}

			# Heresy can't happen again for 5 to 10 years
			set_global_variable = {
				name = regula_heresy_blocked
				value = yes
				years = { 5 10 }
			}
		}
		else = {
			# Heresy can't happen again for 20 to 50 years
			set_global_variable = {
				name = regula_heresy_blocked
				value = yes
				years = { 20 50 }
			}
		}

		regula_heresy_story_debug_end_log_effect = yes
	}

	on_owner_death = {
		# On magister death, the heresy faction dissolves silently.
		set_variable = {
			name = regula_heresy_state
			value = flag:silent_end
		}
		end_story = yes
	}

	effect_group = {
		# Update monthly
		days = 30

		triggered_effect = {
			trigger = {always = yes}
			effect = {
				# First: Check for conspiracy end states.
				regula_heresy_check_for_end_state_effect = yes

				# Second: If a heresy secret has been discovered, but we are
				# still in hidden state, update the state & notify.
				regula_heresy_check_for_newly_discovered_effect = yes

				# Third: Update growth.
				regula_heresy_update_growth_effect = yes

				# Fourth: Update discontent.
				regula_heresy_update_discontent_effect = yes

				# Fifth: Log heresy conspiracy state.
				regula_heresy_story_debug_state_log_effect = yes

				# Finally: If we are in an end state, end the story.
				if = {
					limit = {
						regula_heresy_story_in_terminal_state_trigger = yes
					}
					end_story = yes
				}
			}
		}
	}
}
