namespace = faith_conversion




# Fired when you convert to a new faith. Convert capital county.
faith_conversion.0001 = {
	hidden = yes

	trigger = {
		is_landed = yes
		primary_title.tier > tier_barony
		exists = capital_county
		exists = scope:old_faith
		capital_county.faith = scope:old_faith
	}

	immediate = {
		capital_county = { set_county_faith = root.faith }
	}
}

# Fired when you convert to a new faith. Remove obsolete character modifiers.
faith_conversion.0002 = {
	hidden = yes

	immediate = {
		if = {
			limit = {
				faith.religion != scope:old_faith.religion
			}
			remove_trait = pilgrim
		}
		if = {
			limit = {
				NOR = {
					has_character_flag = converted_by_heresy_0010_event
					has_character_flag = converted_by_heresy_0011_event
					has_character_flag = converted_by_heresy_decision
					has_character_flag = new_heresiarch
				}
			}
			remove_trait = heresiarch
		}
		remove_trait = excommunicated
		remove_character_modifier = recent_excommunication
		remove_character_modifier = excommunication_recently_lifted
		remove_character_modifier = vow_of_poverty_modifier
		remove_character_modifier = temporal_condemnation_modifier
		remove_character_modifier = ultimate_blasphemer_modifier
		remove_character_modifier = defiant_high_king_conversion_boost_modifier
		remove_character_modifier = defiant_high_queen_conversion_boost_modifier
		remove_character_modifier = fp1_jomsvikings_caused_faith_crisis_modifier
		remove_character_modifier = fp2_opportunistic_schismatic_modifier
		remove_character_modifier = fp2_friend_of_the_old_ways_modifier
		remove_character_modifier = bp2_yearly_0641_hof_modifier
		remove_character_modifier = bp2_yearly_0641_realm_priest_modifier
		remove_character_modifier = next_free_ho_hire_modifier
		# If you championed the old ways but then reform regardless, get booted down to friend.
		if = {
			limit = {
				faith.religion = scope:old_faith.religion
				has_character_modifier = fp2_champion_of_the_old_ways_modifier
			}
			remove_character_modifier = fp2_champion_of_the_old_ways_modifier
			add_character_modifier = fp2_friend_of_the_old_ways_modifier
		}
		else = { remove_character_modifier = fp2_champion_of_the_old_ways_modifier }
		# Reset patron deities.
		if = {
			limit = {
				faith = {
					OR = {
						has_doctrine = tenet_bhakti
						religion != scope:old_faith.religion
					}
				}
			}
			remove_character_modifier = bhakti_hinduism_ganga
			remove_character_modifier = bhakti_hinduism_saraswati
			remove_character_modifier = bhakti_hinduism_kali
			remove_character_modifier = bhakti_hinduism_kubera
			remove_character_modifier = bhakti_vaishnavism_lakishmi
			remove_character_modifier = bhakti_vaishnavism_jagganath
			remove_character_modifier = bhakti_vaishnavism_hayagriva
			remove_character_modifier = bhakti_shaivism_parvati
			remove_character_modifier = bhakti_shaivism_virabhadra
			remove_character_modifier = bhakti_shaivism_munishwarar
			remove_character_modifier = bhakti_shaivism_dakshinamoorthy
			remove_character_modifier = bhakti_smartism_ganesha
			remove_character_modifier = bhakti_smartism_vishnu
			remove_character_modifier = bhakti_smartism_shiva
			remove_character_modifier = bhakti_germanic_generic_odin
			remove_character_modifier = bhakti_germanic_not_danish_ullr
			remove_character_modifier = bhakti_germanic_danish_tyr
			remove_character_modifier = bhakti_germanic_generic_thor
			remove_character_modifier = bhakti_germanic_generic_freyr
		}
		# No Friday prayer shenanigans unless you're involved in the Sunni caliphal drama.
		## Friday prayers would absolutely happen for others but these modifiers are currently tied heavily to the struggle.
		## Remove insubordination if you leave Islam.
		if = {
			limit = {
				NOT = { faith.religion = religion:islam_religion }
			}
			remove_character_modifier = fp3_name_read_in_friday_prayer_modifier
		}
		## Remove subordination.
		### Special use-case because you might convert faith within the various ones that consider the caliph to be their HoF, in which case it'd stay.
		if = {
			limit = {
				faith.religious_head_title ?= {
					NOT = { this = title:d_sunni }
				}
			}
			remove_character_modifier = fp3_displayed_pious_submission_to_caliph_modifier
		}
		bastard_to_wild_oat_conversion_effect = yes
	}
}


scripted_trigger faith_conversion_0003_valid_character = {
	faith = scope:old_faith
	is_alive = yes
	is_ai = yes
}

# Fired when you convert to a new faith. Convert close family.
faith_conversion.0003 = {
	hidden = yes

	immediate = {
		every_spouse = {
			limit = {
				faith_conversion_0003_valid_character = yes
				is_landed = no
			}
			set_character_faith = root.faith
		}

		every_child = {
			limit = {
				faith_conversion_0003_valid_character = yes
				target_is_liege_or_above = root #If they have travelled outside of your realm/control, they will not be converted.
				# Children are converted regardless of Landed status, assuming they are Landed within your realm.
			}
			set_character_faith_with_conversion = root.faith #Also converts their spouse, children, and parents if applicable.
		}

		if = {
			limit = {
				exists = mother
			}
			mother = {
				if = {
					limit = {
						faith_conversion_0003_valid_character = yes
						target_is_liege_or_above = root #If they have travelled outside of your realm/control, they will not be converted.
						is_landed = no #If inside your realm but landed, follow normal vassal conversion rules.
					}
					set_character_faith = root.faith
				}
			}
		}

		if = {
			limit = {
				exists = father
			}
			father = {
				if = {
					limit = {
						faith_conversion_0003_valid_character = yes
						target_is_liege_or_above = root #If they have travelled outside of your realm/control, they will not be converted.
						is_landed = no #If inside your realm but landed, follow normal vassal conversion rules.
					}
					set_character_faith = root.faith
				}
			}
		}
	}
}

faith_conversion.0004 = {
	hidden = yes

	trigger = {
		government_has_flag = government_is_theocracy
		is_landed = yes
		faith = {
			has_doctrine = doctrine_theocracy_lay_clergy
		}
	}

	immediate = {
		if = {
			limit = {
				OR = {
					culture = { has_cultural_pillar = heritage_arabic }
					culture = { has_cultural_pillar = heritage_iranian }
					culture = { has_cultural_pillar = heritage_turkic }
				}
			}
			change_government = clan_government
		}	
		else = {
			change_government = feudal_government
		}
	}
}

# For softening opinion penalties, e.g., I believed in monogamous faith but was in a polygamous marriage, but now I've been converted to polygamous faith.
faith_conversion.0005 = {
	hidden = yes

	trigger = {
		any_consort = { }
	}

	immediate = {
		every_consort = {
			save_scope_as = this_consort
			root = {
				update_active_consort_opinion_effect = { PARTNER = scope:this_consort }
			}
		}
	}
}

faith_conversion.1001 = {
	type = character_event
	title = faith_conversion.1001.t
	desc = {
		desc = faith_conversion.1001.start
		first_valid = {
			triggered_desc = {
				trigger = {
					has_trait = zealous
					faith = { has_doctrine = tenet_carnal_exaltation }
					is_male = yes
				}
				desc = faith_conversion.1001.desc.zealous.carnal_male
			}
			triggered_desc = {
				trigger = {
					has_trait = zealous
					faith = { has_doctrine = tenet_carnal_exaltation }
					is_female = yes
				}
				desc = faith_conversion.1001.desc.zealous.carnal_female
			}
			triggered_desc = {
				trigger = { has_trait = zealous }
				desc = faith_conversion.1001.desc.zealous
			}
			triggered_desc = {
				trigger = { has_trait = cynical }
				desc = faith_conversion.1001.desc.cynical
			}
			triggered_desc = {
				trigger = { faith = { has_doctrine_parameter = reincarnation_events_active } }
				desc = faith_conversion.1001.desc.reincarnation
			}
			triggered_desc = {
				trigger = { has_trait = compassionate }
				desc = faith_conversion.1001.desc.compassionate
			}
			desc = faith_conversion.1001.desc
		}
		first_valid = {
			triggered_desc = {
				trigger = { has_government = landless_adventurer_government }
				desc = faith_conversion.1001.end.is_landless_adventurer
			}
			triggered_desc = {
				trigger = {
					top_liege != this
					liege = {
						faith != root.faith
					} 
				}
				desc = faith_conversion.1001.end.is_vassal.convert_away
			}
			triggered_desc = {
				trigger = {
					top_liege != this
					liege = {
						faith = root.faith
					}
				}
				desc = faith_conversion.1001.end.is_vassal.convert_to
			}
			triggered_desc = {
				trigger = {
					any_vassal = {
						percent < 0.5
						faith = root.faith
					}
				}
				desc = faith_conversion.1001.end.is_top_liege.convert_away
			}
			triggered_desc = {
				trigger = {
					any_vassal = {
						percent >= 0.5
						faith = root.faith
					}
				}
				desc = faith_conversion.1001.end.is_top_liege.convert_to
			}
			
		}
	}
	theme = faith
	left_portrait = {
		character = root
		animation = prayer
	}
	right_portrait = {
		character = scope:template_priest
		animation = wedding_priest
	}

	trigger = {
		is_ai = no # Since the event is narrative-only, there's no need to generate a temporary priest character only to kill them with no other effects for AI rulers.
	}

	immediate = {
		faith = {
			save_scope_as = new_faith
		}
		liege ?= {
			save_scope_as = my_liege
		}
		primary_title = {
			save_scope_as = my_title
		}
		dynasty = {
			save_scope_as = my_dynasty
		}

		create_character = {
			template = priest_character_template
			location = root.capital_province
			save_scope_as = template_priest
		}
		# If necessary, make them celibate.
		hidden_effect = {
			if = {
				limit = {
					NOT = {
						scope:new_faith = { has_doctrine_parameter = clergy_can_marry } # scoping directly to the character was firing null character errors
					}
				}
				scope:template_priest = {
					add_trait = devoted
				}
			}
		}
		
		#Dummy check to avoid errors since the flag is only checked in portrait modifiers otherwise
		if = {
			limit = {
				has_character_flag = need_priest_outfit
			}
			#Please dress properly!
		}
		if = {
			limit = {
				scope:template_priest = { should_be_naked_trigger = yes }
			}
			scope:template_priest = {
				add_character_flag = is_naked
			}
		}
		else = {
			scope:template_priest = {
				add_character_flag = need_priest_outfit
			}
		}
	}

	option = { 
		name = faith_conversion.1001.a
	}

	option = { 
		name = faith_conversion.1001.b
		# Gain scope:template priest as a courtier & earmark them for future friendship.
		add_courtier = scope:template_priest
		reverse_add_opinion = {
			target = scope:template_priest
			modifier = pious_opinion
			opinion = 50
		}
		set_relation_potential_friend = scope:template_priest
	}

	after = {
		hidden_effect = {
			if = {
				limit = {
					this != scope:template_priest.host
				}
				scope:template_priest = {		
					death = {
						death_reason = death_disappearance
					}
				}
			}
		}
		if = {
			limit = {
				scope:template_priest ?= {
					has_character_flag = is_naked
				}
			}
			scope:template_priest = {
				remove_character_flag = is_naked
			}
		}
		

		add_character_flag  = {
			flag = recent_convert
			years = 20
		}
	}
}

faith_conversion.1002 = {
	hidden = yes

	immediate = {
		# If it's desirable to delay this event (e.g., due to decision) a little, do so.
		if = {
			limit = { has_character_flag = delay_player_faith_conversion_notification_event }
			trigger_event = {
				id = faith_conversion.1001
				days = 7
			}
		}
		# Otherwise, just send it.
		else = { trigger_event = faith_conversion.1001 }
	}
}

faith_conversion.1101 = {
	hidden = yes

	immediate = {
		every_vassal = {
			limit = {
				is_ai = no
			}
			send_interface_toast = {
				title = faith_conversion.1101.toast
				left_icon = root

				show_as_tooltip = {
					root = {
						set_character_faith = faith
					}
				}
			}
		}
	}
}
