﻿# Sets up a new Magister, Run each time a new Magister is created on that character
# Sets us as the global_var:magister_character
# Makes us able to fight (regardless of gender law), Heterosexual, lustful
# Gives us Regula sacred blood and rests bloodline goals (if we use single life rule)
# Scope is Magister character
regula_initialize_magister_effect = {
	set_global_variable = {
		name = magister_character
		value = root
	}

	# Make sure we can fight
	if = {
		limit = {
			NOT = {
				has_character_modifier = ignores_gender_army_rules
			}
		}
		add_character_modifier = ignores_gender_army_rules
	}

	# Make us Hetero
	if = {
		limit = {
			NOT = {
				has_sexuality = heterosexual
			}
		}
		set_sexuality = heterosexual
	}

	# Check if we should have sacred blood
	if = {
		limit = {
			dynasty = {
				exists = var:variable_made_bloodline_holy
			}
		}
		remove_trait = regula_sacrutus_genus_sanguis
		remove_trait = regula_sacrutus_genus_dimidium
		add_trait_force_tooltip = regula_sacrutus_genus_prime
	}

	# Make us lustful, removing chaste if necessary
	remove_trait = chaste
	add_trait_force_tooltip = lustful

	# Reset Bloodline tallys if we have single life rule
	if = {
		limit = {
			has_game_rule = regula_bloodline_tally_goals_single_life
		}
		set_global_variable = {
			name = regula_fascinare_tally
			value = 0
		}
		set_global_variable = {
			name = regula_domitans_tally
			value = 0
		}
		set_global_variable = {
			name = regula_contubernalis_tally
			value = 0
		}
		set_global_variable = {
			name = regula_domination_war_tally
			value = 0
		}
		set_global_variable = {
			name = regula_servitude_war_tally
			value = 0
		}
		set_global_variable = {
			name = regula_potestas_queen_tally
			value = 0
		}
		set_global_variable = {
			name = regula_obedience_tally
			value = 0
		}
	}
}

####################################
## Secret Regula Stage #############
####################################
# Gives the target a secret conversion secret
# CONVERTER - the character that is "secretly" converting the scope:recipient
# root - the character that is being "secretly" converted.
regula_secret_conversion = {

	# Add our conversion secret
	add_secret = {
		type = regula_covert_conversion
	}

	# Reveal this to the person that converted us (should be proto-Magister)
	random_secret = {
		limit = {
			secret_type = regula_covert_conversion
			NOT = { is_known_by = $CONVERTER$ }
		}
		reveal_to = $CONVERTER$
	}

	# Bonus opinion effect
	add_opinion = {
		modifier = regula_secretly_converted_opinion
		target = $CONVERTER$
	}

	# Set our vassal succession law
	if = {
		limit = {
			is_landed = yes
		}
		add_realm_law_skip_effects = regula_covert_vassal_succession_law
	}
}

# Oh dear, the Proto-Magister died!
# Probally fell over the boat trying to bag a whale :(
# For the next Proto-Magister, we have to find all of the secretly converted.
regula_secret_reboot = {
	# Please forgive me CK3...
	# Just this once, I'll have to go all out
	# Need to reveal covert secret to new Magister, but how do I know who was already converted?
	every_living_character = {
		every_secret = {
			limit = {
				secret_type = regula_covert_conversion
			}
			reveal_to = root
		}
	}
}

# Free the Keeper of Souls!
# Some misc effects that happen as we free the Keeper of Souls
# Show Regula Law tooltip
# Set regula_initialized
# Set Heresy Blocked
regula_free_keeper_of_souls_misc_effect = {
	show_as_tooltip = {
		add_realm_law_skip_effects = regula_magister_gender_law
	}

	set_global_variable = {
		name = regula_initialized
		value = yes
	}
	
	# Heresy can't for the first 10 years after the keeper of souls is
	# released.
	set_global_variable = {
		name = regula_heresy_blocked
		value = yes
		years = regula_heresy_free_kos_block_duration_years
	}
}

# Reveal the secret!
# Convert anyone who is secretly charmed
# Then we do "bonus" conversions
# Anyone in our family at our court who is not zealous converts (basic religion change)
# Our capital barony converts (if landed)
# This effect should work even if Magister is landless
regula_secret_conversion_reveal_effect = {
	faith = { save_scope_as = magister_faith }
	save_scope_as = actor

	save_scope_as = recipient
	new_faith_created_conversion_effect = yes

	# For each secret charmed person (we should know about all of them)
	# Convert them, then if they are landed convert their capital county (as a "bonus" effect)
	every_known_secret = {
		limit = {
			secret_type = regula_covert_conversion
			NOT = { secret_owner = scope:actor }	# Don't do effect on Magister twice!
		}
		secret_owner = {
			set_character_faith = scope:magister_faith
			add_trait = mulsa
			# Remove secret conversion opinion
			remove_opinion = {
				target = scope:actor
				modifier = regula_secretly_converted_opinion
			}
			# Extra modifier for being our initial spellbound characters
			add_character_modifier = {
				modifier = regula_initial_spellbound
			}

			save_scope_as = recipient
			new_faith_created_conversion_effect = yes

			# If landed, convert our capital county and divorce (unless already married to Magister)
			if = {
				limit = {
					is_landed = yes
				}

				# Divorce from spouse if not married to Magister
				if = {
					limit = { 
						is_married = yes
						NOT = { is_consort_of = scope:actor }
					}
					divorce = this.primary_spouse
				}

				# Convert capital county
				capital_county = {
					set_county_faith = scope:magister_faith
				}
			}
		}
	}

	# Convert capital county
	if = {
		limit = { is_landed = yes }
		capital_county = {
			set_county_faith = scope:magister_faith
		}
	}

	# Create Domina
	scope:domina ?= {
		remove_trait = mulsa
		add_trait = domina
	}

	# If this was a glorious ritual we get lots of bonus conversions
	# - Convert entire realm and all neighbour counties to your realm
	# - Convert all Vassals and divorce them from spouses (if not married to Magister)
	# - Note this does not count as a "Spellbound" conversion, so we don't get the bonus opinion modifier
	# - Convert all courtiers and guests
	if = {
		limit = {
			scope:glorious_initialize_reveal_decision = yes
		}

		# Convert the Magisters realm
		if = {
			limit = { is_landed = yes }
			# Convert the Magisters realm
			every_sub_realm_county = {
				set_county_faith = scope:magister_faith
	
				# Find neighbour counties and convert as well
				every_neighboring_county = {
					set_county_faith = scope:magister_faith
				}
			}
			# Convert all vassals
			every_vassal = {
				limit = {
					is_female = yes
				}
				set_character_faith = scope:magister_faith
				add_trait = mulsa
				save_scope_as = recipient
				new_faith_created_conversion_effect = yes

				# If landed, divorce (unless already married to Magister)
				if = {
					limit = {
						is_landed = yes
					}
					# Divorce from spouse if not married to Magister
					if = {
						limit = { 
							is_married = yes
							NOT = { is_consort_of = scope:actor }
						}
						divorce = this.primary_spouse
					}
				}
			}
		}

		# Convert all female courtiers and guests (followers also count)
		every_courtier_or_guest = {
			limit = {
				is_female = yes
			}
			set_character_faith = scope:magister_faith
			add_trait = mulsa
			save_scope_as = recipient
			new_faith_created_conversion_effect = yes
		}
	}
}

# These effects should run after we choose our holy sites, but only the first time we run them
# Scoped character should be the Magister
# We have three levels, Desperate, Modest and Glorious reveal, with different values for each
# We trigger the next step (inital religious tenet) here as well
regula_faith_setup_inital_effect = {

	# Bonus piety and prestige depending on Reveal level
	if = {
		limit = {
			scope:desperate_initialize_reveal_decision = yes
		}
		add_piety = 100
		add_prestige = 100
	}
	else_if = {
		limit = {
			scope:modest_initialize_reveal_decision = yes
		}
		add_piety = 500
		add_prestige = 750
	}
	else_if = {
		limit = {
			scope:glorious_initialize_reveal_decision = yes
		}
		add_piety = 1000
		add_prestige = 1500
		dynasty = {
			add_dynasty_prestige = 1000
		}
	}

	# Save our Magister and bloodline goals
	regula_initialize_magister_effect = yes

	# Only run this if landed (landless cannot become HOF or convert land)
	# Make us the head of faith
	# We hide this because tooltip doesn't work as we set faith and holy sites together
	hidden_effect = {
		if = {
			limit = { is_landed = yes }
			set_up_dynamic_temporal_hof_title_effect = { NEW_HOLDER = root }
		}
		# If this wasn't a forced conversion (or desperate ritual), Get our Domina and convert our lands and those secretly charmed
		if = {
			limit = {
				NOR = {
					has_character_flag = regula_forced_start
					scope:desperate_initialize_reveal_decision = yes
				}
			}
			regula_secret_conversion_reveal_effect = yes
		}
	}

	# Bonus effect for glorious ritual, change personalities of our newly devoted characters
	if = {
		limit = {
			scope:glorious_initialize_reveal_decision = yes
		}
		custom_description = {
			text = regula_keeper_reveal_change_compedita_personality_effect
			faith = {
				every_faith_character = {
					limit = {
						is_regula_devoted_trigger = yes
					}
					regula_change_personality_to_virtuous_single_effect = yes
					regula_change_personality_to_virtuous_single_effect = yes
					regula_change_personality_to_virtuous_single_effect = yes
				}
			}
		}
	}


	# Remove traditions incompatible with Regula faith
	culture = {
		if = {
			limit = {
				has_cultural_tradition = tradition_monogamous
			}
			remove_culture_tradition = tradition_monogamous
		}
		if = {
			limit = {
				has_cultural_tradition = tradition_polygamous
			}
			remove_culture_tradition = tradition_polygamous
		}
		if = {
			limit = {
				has_cultural_tradition = tradition_concubines
			}
			remove_culture_tradition = tradition_concubines
		}
	}

	# Setup temple slots
	faith = {
		every_holy_site = {
			limit = {
				title_province = {
					has_special_building_slot = no
				}
			}
			title_province = {
				add_special_building_slot = holy_site_other_grand_temple_01
			}
		}
	}

	# Set Inheritance laws
	add_realm_law_skip_effects = regula_magister_gender_law
	every_vassal = {
		limit = {
			highest_held_title_tier >= tier_county
		}
		add_realm_law_skip_effects = regula_vassal_succession_law
	}

	# Starting Religious Tenet
	trigger_event = {
		id = regula_initialize_event.0020
	}

	# Maint events due to changing religion
	trigger_event = {
		on_action = regula_initialize_complete_response
	}
}

# These effects should run after we choose our holy sites, through the settings menu
# Scoped character should be the Magister
regula_change_holy_site_option = {
	# Change all characters and counties of old faith to new faith
	# We need scope:old_faith and scope:new_faith
	$NEW_FAITH$ = {
		set_religious_head_title = $OLD_FAITH$.religious_head_title
	}
	$OLD_FAITH$ = {
		remove_religious_head_title = yes
	}

	hidden_effect = {
		$NEW_FAITH$ = {
			# Clear any old tenets
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_carnal_exaltation }
					}
				}
				remove_doctrine = tenet_carnal_exaltation
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_sacred_childbirth }
					}
				}
				remove_doctrine = tenet_sacred_childbirth
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_reincarnation }
					}
				}
				remove_doctrine = tenet_reincarnation
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_mystical_birthright }
					}
				}
				remove_doctrine = tenet_mystical_birthright
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_sacred_shadows }
					}
				}
				remove_doctrine = tenet_sacred_shadows
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_communal_identity }
					}
				}
				remove_doctrine = tenet_communal_identity
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_ancestor_worship }
					}
				}
				remove_doctrine = tenet_ancestor_worship
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_astrology }
					}
				}
				remove_doctrine = tenet_astrology
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_asceticism }
					}
				}
				remove_doctrine = tenet_asceticism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_esotericism }
					}
				}
				remove_doctrine = tenet_esotericism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_literalism }
					}
				}
				remove_doctrine = tenet_literalism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_adaptive }
					}
				}
				remove_doctrine = tenet_adaptive
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_legalism }
					}
				}
				remove_doctrine = tenet_legalism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_struggle_submission }
					}
				}
				remove_doctrine = tenet_struggle_submission
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_pacifism }
					}
				}
				remove_doctrine = tenet_pacifism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_warmonger }
					}
				}
				remove_doctrine = tenet_warmonger
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_pursuit_of_power }
					}
				}
				remove_doctrine = tenet_pursuit_of_power
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_cthonic_redoubts }
					}
				}
				remove_doctrine = tenet_cthonic_redoubts
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_sanctity_of_nature }
					}
				}
				remove_doctrine = tenet_sanctity_of_nature
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_megaliths }
					}
				}
				remove_doctrine = tenet_megaliths
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_islamic_syncretism }
					}
				}
				remove_doctrine = tenet_islamic_syncretism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_christian_syncretism }
					}
				}
				remove_doctrine = tenet_christian_syncretism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_eastern_syncretism }
					}
				}
				remove_doctrine = tenet_eastern_syncretism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						NOT = { has_doctrine = tenet_unreformed_syncretism }
					}
				}
				remove_doctrine = tenet_unreformed_syncretism
			}

			# Copy New Tenets
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_carnal_exaltation
					}
				}
				add_doctrine = tenet_carnal_exaltation
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_sacred_childbirth
					}
				}
				add_doctrine = tenet_sacred_childbirth
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_reincarnation
					}
				}
				add_doctrine = tenet_reincarnation
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_mystical_birthright
					}
				}
				add_doctrine = tenet_mystical_birthright
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_natural_primitivism
					}
				}
				add_doctrine = tenet_natural_primitivism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_sacred_shadows
					}
				}
				add_doctrine = tenet_sacred_shadows
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_communal_identity
					}
				}
				add_doctrine = tenet_communal_identity
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_ancestor_worship
					}
				}
				add_doctrine = tenet_ancestor_worship
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_astrology
					}
				}
				add_doctrine = tenet_astrology
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_asceticism
					}
				}
				add_doctrine = tenet_asceticism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_esotericism
					}
				}
				add_doctrine = tenet_esotericism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_literalism
					}
				}
				add_doctrine = tenet_literalism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_adaptive
					}
				}
				add_doctrine = tenet_adaptive
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_legalism
					}
				}
				add_doctrine = tenet_legalism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_struggle_submission
					}
				}
				add_doctrine = tenet_struggle_submission
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_pacifism
					}
				}
				add_doctrine = tenet_pacifism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_warmonger
					}
				}
				add_doctrine = tenet_warmonger
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_pursuit_of_power
					}
				}
				add_doctrine = tenet_pursuit_of_power
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_cthonic_redoubts
					}
				}
				add_doctrine = tenet_cthonic_redoubts
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_sanctity_of_nature
					}
				}
				add_doctrine = tenet_sanctity_of_nature
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_megaliths
					}
				}
				add_doctrine = tenet_megaliths
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_islamic_syncretism
					}
				}
				add_doctrine = tenet_islamic_syncretism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_christian_syncretism
					}
				}
				add_doctrine = tenet_christian_syncretism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_eastern_syncretism
					}
				}
				add_doctrine = tenet_eastern_syncretism
			}
			if = {
				limit = {
					$OLD_FAITH$ = {
						has_doctrine = tenet_unreformed_syncretism
					}
				}
				add_doctrine = tenet_unreformed_syncretism
			}
		}

		$OLD_FAITH$ = {
			every_faith_character = {
				set_character_faith = $NEW_FAITH$
			}
		}

		# Performance hit, ouch
		every_county = {
			limit = {
				# We make sure all counties change
				is_regula_trigger = yes
			}
			set_county_faith = $NEW_FAITH$
		}
	}

	# Setup temple slots
	# We dont remove temple slot from old holy sites, maybe we should?
	# TODO?
	faith = {
		every_holy_site = {
			limit = {
				title_province = {
					has_special_building_slot = no
				}
			}
			title_province = {
				add_special_building_slot = holy_site_other_grand_temple_01
			}
		}
	}
}
