﻿task_religious_relations = {
	default_task = yes
	position = councillor_court_chaplain

	task_type = task_type_general
	task_progress = task_progress_infinite

	asset = {
		icon = "gfx/interface/icons/council_task_types/task_religious_relations.dds"
		background = "gfx/interface/buttons/button_round_bg.dds"
		frame = "gfx/interface/buttons/button_round_frame.dds"
		glow = "gfx/particles/halo.dds"
	}

	effect_desc = {
		### Piety tooltip
		desc = task_religious_relations_piety_effect_desc
		triggered_desc = {
			trigger = {
				scope:councillor_liege = { has_perk = clerical_justifications_perk }
			}
			desc = task_religious_relations_effect_desc_perk_bonus_piety
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			desc = task_religious_relations_effect_desc_legacy_bonus_piety
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_family_business_bonus = yes
			}
			desc = task_religious_relations_effect_desc_family_business_bonus_piety
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_consulted_house_bonus = yes
			}
			desc = task_religious_relations_effect_desc_consulted_house_bonus_piety
		}

		first_valid = {
			triggered_desc = { 
				trigger = {
					scope:councillor.faith = {
						OR = {
							has_doctrine_parameter = no_head_of_faith
							NOT = { exists = religious_head }
						}
						has_doctrine = doctrine_theocracy_lay_clergy
					}
				}
				desc = task_religious_relations_effect_no_hof_layman_clergy_desc_opinion
			}
			desc = task_religious_relations_effect_desc_opinion	
		}


		triggered_desc = {
			trigger = {
				scope:councillor_liege = {	has_perk = clerical_justifications_perk }
			}
			desc = task_religious_relations_effect_desc_perk_bonus_opinion
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			desc = task_religious_relations_effect_desc_legacy_bonus_opinion
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_family_business_bonus = yes
			}
			desc = task_religious_relations_effect_desc_family_business_bonus_opinion
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_consulted_house_bonus = yes
			}
			desc = task_religious_relations_effect_desc_consulted_house_bonus_opinion
		}
		desc = line_break

		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					learning > mediocre_skill_rating
				}
				desc = task_religious_relations_opinion_gain
			}
			triggered_desc = {
				trigger = {
					learning < high_skill_rating
				}
				desc = task_religious_relations_piety_loss
			}
			triggered_desc = {
				trigger = {
					learning < high_skill_rating
				}
				desc = task_religious_relations_opinion_loss
			}
		}
	}

	on_start_task = {
		liege = {
			set_variable = {
				name = court_chaplain_religious_relations_opinion_value
				value = 0
			}
		}
	}

	on_cancel_task = {
		if = {
			limit = {
				is_alive = yes
				has_variable = court_chaplain_religious_relations_opinion_value
			}
			remove_variable = court_chaplain_religious_relations_opinion_value
		}
	}

	council_owner_modifier = {
		name = court_chaplain_religious_relations_modifier
		monthly_piety = 1
		scale = court_chaplain_religious_relations_total_piety_gain
	}

	council_owner_modifier = {
		name = court_chaplain_religious_relations_opinion_modifier
		theocracy_government_opinion_same_faith = 1
		scale = court_chaplain_religious_relations_opinion_modifier
	}

	council_owner_modifier = {
		name = court_chaplain_religious_relations_no_hof_opinion_modifier
		same_faith_opinion = 1
		scale = court_chaplain_religious_relations_no_hof_opinion_modifier
	}

	monthly_on_action = task_religious_relations_side_effects

	on_monthly = {
		if = {
			limit = {
				liege = { is_ai = no }
			}
			if = {
				limit = {
					liege = {
						has_variable = court_chaplain_religious_relations_opinion_value
					}
				}
				liege = {
					if = {
						limit = {
							var:court_chaplain_religious_relations_opinion_value < root.court_chaplain_religious_relations_opinion_max
						}
						change_variable = {
							name = court_chaplain_religious_relations_opinion_value
							add = root.court_chaplain_religious_relations_monthly_increase
						}
					}
					if = {
						limit = {
							var:court_chaplain_religious_relations_opinion_value > root.court_chaplain_religious_relations_opinion_max
						}
						set_variable = {
							name = court_chaplain_religious_relations_opinion_value
							value = root.court_chaplain_religious_relations_opinion_max
						}
					}
				}
			}
			else = {
				liege = {
					set_variable = {
						name = court_chaplain_religious_relations_opinion_value
						value = 0
					}
				}
			}
		}
	}

	ai_will_do = {
		value = 1 # Always a good backup

	}
}

task_conversion = {
	position = councillor_court_chaplain

	task_type = task_type_county
	county_target = realm
	ai_county_target = realm
	task_progress = task_progress_percentage
	highlight_own_realm = yes

	asset = {
		icon = "gfx/interface/icons/council_task_types/task_conversion.dds"
		background = "gfx/interface/buttons/button_round_bg.dds"
		frame = "gfx/interface/buttons/button_round_frame.dds"
		glow = "gfx/particles/halo.dds"
	}

	effect_desc = {
		#China changes the faith to the Huangdi's faith
		triggered_desc = {
			trigger = {
				scope:councillor_liege = {
					highest_held_title_tier >= tier_hegemony
					government_has_flag = government_has_merit
				}
			}
			desc = task_conversion_china_effect_desc
		}
		#Otherwise it's councillor's faith
		triggered_desc = {
			trigger = {
				NOT = {
					scope:councillor_liege = {
						highest_held_title_tier >= tier_hegemony
						government_has_flag = government_has_merit
					}
				}
			}
			desc = task_conversion_effect_desc
		}
		desc = task_conversion_development_loss_desc
		triggered_desc = {
			trigger = {
				scope:councillor_liege = {
					any_vassal = {
						vassal_stance = minority
					}
				}
			}
			desc = minority_disapproves_conversion_tooltip
		}
		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					learning > mediocre_skill_rating
				}
				desc = task_conversion_opinion_gain
			}
			triggered_desc = {
				trigger = {
					learning > mediocre_skill_rating
				}
				desc = task_conversion_development_gain
			}
			triggered_desc = {
				trigger = {
					learning > mediocre_skill_rating
				}
				desc = task_conversion_levy_gain
			}
			triggered_desc = {
				trigger = {
					learning < high_skill_rating
				}
				desc = task_conversion_resistance_to_conversion
			}
			triggered_desc = {
				trigger = {
					learning < high_skill_rating
				}
				desc = task_conversion_opinion_loss
			}
		}
	}

	progress = {
		value = 0
		#Base conversion speed
		add = {
			value = court_chaplain_conversion_base
			desc = COURT_CHAPLAIN_PROGRESS_BASE
		}
		add = {
			value = scope:councillor.court_chaplain_conversion_monthly_increase
			desc = SCALED_COUNCILLOR_LEARNING_VALUE
		}
		#Fervor of Court Chaplain's Faith
		if = {
			limit = { # Conditional in order to not mess up the preview-tooltip
				exists = scope:county
				exists = scope:councillor.faith	
				exists = scope:county.faith
			}
			add = {
				add = {
					add = scope:councillor.court_chaplain_progress_percentage
					multiply = scope:councillor.faith.fervor
					multiply = convert_faith_fervor_modifier_scale
				}
				subtract = {
					add = scope:councillor.court_chaplain_progress_percentage
					multiply = scope:county.faith.fervor
					multiply = convert_faith_fervor_modifier_scale
				}
				if = {
					limit = {
						scope:councillor_liege = { has_perk = religious_icon_perk }
					}
					min = 0
				}
				desc = COURT_CHAPLAIN_CONVERSION_FERVOR_IMPACT
			}
		}
		add = court_chaplain_conversion_contextual_bonuses
		
		# Faster conversion for certain special doctrines
		if = {
			limit = {
				scope:councillor_liege.faith = { has_doctrine_parameter = adoptionists_attractively_intuitive_theology }
				exists = scope:county
				OR = {
					scope:county.faith.religion = religion:eastern_orthodox_religion 
					scope:county.faith.religion = religion:catholic_religion 
					scope:county.faith.religion = religion:protestant_religion 
				}
			}
			add = {
				value = fp2_lyonese_monk_conversion_bonus_actual_value
				desc = fp2_lyonese_monk_faith_conversion_task_desc
			}
		}

		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county.faith
				scope:county.faith = {
					fervor > 80
				}
			}
			multiply = {
				value = 0.2
				desc = STEWARD_CONVERT_FAITH_ZEALOT_FERVOUR_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county.faith
				scope:county.faith = {
					fervor <= 80
					fervor > 60
				}
			}
			multiply = {
				value = 0.4
				desc = STEWARD_CONVERT_FAITH_HIGH_FERVOUR_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county.faith
				scope:county.faith = {
					fervor <= 60
					fervor > 40
				}
			}
			multiply = {
				value = 0.6
				desc = STEWARD_PROMOTE_FAITH_MEDIUM_FERVOUR_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county.faith
				scope:county.faith = {
					fervor <= 40
					fervor > 20
				}
			}
			multiply = {
				value = 0.8
				desc = STEWARD_PROMOTE_CULTURE_LOW_FERVOUR_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county.faith
				scope:county.faith = {
					fervor <= 20
					fervor >= 0
				}
			}
			multiply = {
				value = 1.2
				desc = STEWARD_PROMOTE_CULTURE_LOW_FERVOUR_BONUS
			}
		}
		####
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county
				scope:county = {
					county_control <= 25
					county_control >= 0
				}
			}
			multiply = {
				value = 0.5
				desc = STEWARD_PROMOTE_CULTURE_LOW_CONTROL_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county
				scope:county = {
					county_control <= 50
					county_control > 25
				}
			}
			multiply = {
				value = 0.7
				desc = STEWARD_PROMOTE_CULTURE_MEDIUM_CONTROL_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county
				scope:county = {
					county_control <= 75
					county_control > 50
				}
			}
			multiply = {
				value = 0.9
				desc = STEWARD_PROMOTE_CULTURE_HIGH_CONTROL_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county
				scope:county = {
					county_control > 75
				}
			}
			multiply = {
				value = 1.1
				desc = STEWARD_PROMOTE_CULTURE_HIGH_CONTROL_BONUS
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county
				scope:county = {
					county_control <= 100
					county_control > 75
				}
			}
			multiply = {
				value = 1.3
				desc = STEWARD_PROMOTE_CULTURE_HIGH_CONTROL_BONUS
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county
				scope:county = {
					county_control > 100
				}
			}
			multiply = {
				value = 1.5
				desc = STEWARD_PROMOTE_CULTURE_ABSOLUTE_CONTROL_BONUS
			}
		}
		#####
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county.title_province
				scope:county.title_province = {
					OR = {
						terrain = jungle
						terrain = mountains
						terrain = desert
						terrain = desert_mountains
						terrain = taiga
						terrain = wetlands
					}
				}
			}
			multiply = {
				value = 0.6
				desc = STEWARD_PROMOTE_CULTURE_HIGH_TERRAIN_PENALTY
			}
		}
		if = { # Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
			limit = {
				exists = scope:county.title_province
				scope:county.title_province = {
					OR = {
						terrain = floodplains
						terrain = drylands
						terrain = steppe
						terrain = forest
						terrain = hills
						terrain = tells
					}
				}
			}
			multiply = {
				value = 0.8
				desc = STEWARD_PROMOTE_CULTURE_LOW_TERRAIN_PENALTY
			}
		}
		# Development reduces the speed. Should be the last non-multiply modifier applied to avoid negative values
		if = {
			limit = {
				exists = scope:county
				scope:county = {
					development_level > 0
				}
			}
			add = {
				value = scope:county.convert_faith_development_penalty
				desc = STEWARD_PROMOTE_CULTURE_DEVELOPMENT_PENALTY
			}
		}
		
		if = {
			limit = {
				scope:councillor.faith = {
					has_doctrine_parameter = unreformed
				}
			}
			multiply = {
				value = conversion_unreformed_faith_penalty
				desc = COURT_CHAPLAIN_CONVERSION_UNREFORMED_FAITH_PENALTY
			}
		}
		
		# Faster conversion for FP2 Hostility ending, if county is in Iberia and faith was involved
		if = {
			limit = {
				exists = scope:councillor.liege.house
				scope:councillor.liege.house = {
					OR = {
						has_house_modifier = fp2_struggle_hostility_house_faith_modifier
						has_house_modifier = fp2_struggle_hostility_house_combined_modifier
					}
				}
				any_in_global_list = {
					variable = fp2_struggle_ending_faith_list
					this = scope:county.faith
				}
				scope:county.title_province = { geographical_region = world_europe_west_iberia }
			}
			multiply = {
				value = fp2_struggle_hostility_conversion_value
				desc = fp2_struggle_hostility_faith_conversion_task_desc
			}
		}
		# Faster conversion of infidels for FP2 Council Toledo Conversion
		if = {
			limit = {
				scope:councillor.liege = { has_character_modifier = fp2_council_conversion_modifier }
				scope:county.religion != scope:councillor.liege.religion
			}
			multiply = {
				value = council_toledo_conversion_modifier_value
				desc = council_toledo_conversion_task_desc
			}
		}
		# Faster conversion of heretics for FP2 Council Toledo Heresies
		if = {
			limit = {
				scope:councillor.liege = { has_character_modifier = fp2_council_conversion_modifier }
				scope:county.faith = {
					religion = scope:councillor.liege.religion
					NOT = {
						portrait_religious_faith_or_foundational_trigger = { FAITH = faith:mozarabic_church }
					}
					NOT = { exists = religious_head }
				}
			}
			multiply = {
				value = council_toledo_conversion_modifier_value
				desc = council_toledo_heresies_task_desc
			}
		}

		# Faster conversion of Persian counties with the FP3 Promote Persian Scripture decision
		if = {
			limit = {
				scope:councillor.liege = { 
					has_character_modifier = fp3_promoted_persian_scripture_modifier 
					is_ai = no #this is so that we don't get AI converting and homogenizing Persia too quickly
				}
				scope:county.culture = { has_cultural_pillar = heritage_iranian } 
			}
			multiply = {
				value = promote_persian_scripture_conversion_modifier_value
				desc = fp3_promote_persian_scripture_task_desc
			}
		}

		# FP3 faster conversion struggle ending rewards
		if = {
			limit = {
				exists = global_var:fp3_struggle_ending_assertion_school
				switch = {
					trigger = global_var:fp3_struggle_ending_assertion_school
					flag:sunni = {
						scope:councillor.liege.faith = { has_doctrine = muhammad_succession_sunni_doctrine }
					}
					flag:shia = {
						scope:councillor.liege.faith = { has_doctrine = muhammad_succession_shia_doctrine }
					}
					flag:muhakkima = {
						scope:councillor.liege.faith = { has_doctrine = muhammad_succession_muhakkima_doctrine }
					}
					flag:zandaqa = {
						scope:councillor.liege.faith = { has_doctrine = muhammad_succession_zandaqa_doctrine }
					}
				}
			}
			multiply = {
				value = fp3_struggle_ending_conversion_modifier_value
				desc = fp3_struggle_ending_conversion_modifier_desc
			}
		}
		# Court events
		if = {	#court.6010
			limit = {
				scope:councillor.liege = {
					var:6010_harsh_conversion ?= scope:county.faith
				}
			}
			multiply = {
				value = 1.2
				desc = court_6010_harsh_conversions
			}
		}
		if = {	#court.6010
			limit = {
				scope:councillor.liege = {
					var:6010_tolerant_conversion ?= scope:county.faith
				}
			}
			multiply = {
				value = 0.8
				desc = court_6010_tolerant_conversions
			}
		}
		#If someone has done some buddhist teaching in this province
		if = {
			limit = {
				scope:county ?= {
					has_county_modifier = buddhist_teachings_conversion_modifier
				}
				scope:councillor.liege.religion = religion:buddhism_religion 
			}
			multiply = {
				value = 2
				desc = buddhist_teachings_conversion_modifier
			}
		}
		# State Faith Boost
		if = {
			limit = {
				scope:councillor.top_liege.primary_title.state_faith ?= {
					this != scope:county.faith
					this = scope:councillor.top_liege.faith
				}
			}
			multiply = {
				value = ep3_state_faith_conversion_mult_value
				desc = ep3_convert_county_to_state_faith
			}
		}
		
		# Conversion speed game rules
		if = {
			limit = {
				has_game_rule = slower_faith_conversion_speed
			}
			multiply = {
				value = slower_game_rule_value
				desc = GAME_RULE_SLOWER_REASON
			}
		}
		if = {
			limit = {
				has_game_rule = significantly_slower_faith_conversion_speed
			}
			multiply = {
				value = significantly_slower_game_rule_value
				desc = GAME_RULE_SIGNIFICANTLY_SLOWER_REASON
			}
		}
		if = {
			limit = {
				has_game_rule = faster_faith_conversion_speed
			}
			multiply = {
				value = faster_game_rule_value
				desc = GAME_RULE_FASTER_REASON
			}
		}
		if = {
			limit = {
				has_game_rule = significantly_faster_faith_conversion_speed
			}
			multiply = {
				value = significantly_faster_game_rule_value
				desc = GAME_RULE_SIGNIFICANTLY_FASTER_REASON
			}
		}
		if = {
			limit = {
				exists = scope:county
				OR = {
					AND = {
						scope:councillor.faith = { has_doctrine_parameter = unreformed_syncretic_actor_opinion_active }
						scope:county.faith = { has_doctrine_parameter = unreformed }
					}
					AND = {
						scope:councillor.faith = { has_doctrine_parameter = christian_syncretic_actor_opinion_active }
						scope:county.faith = { religion_tag = eastern_orthodox_religion }
					}
					AND = {
						scope:councillor.faith = { has_doctrine_parameter = christian_syncretic_actor_opinion_active }
						scope:county.faith = { religion_tag = protestant_religion }
					}
					AND = {
						scope:councillor.faith = { has_doctrine_parameter = christian_syncretic_actor_opinion_active }
						scope:county.faith = { religion_tag = catholic_religion }
					}
					AND = {
						scope:councillor.faith = { has_doctrine_parameter = islamic_syncretic_actor_opinion_active }
						scope:county.faith = { religion_tag = islam_religion }
					}
					AND = {
						scope:councillor.faith = { has_doctrine_parameter = jewish_syncretic_actor_opinion_active }
						scope:county.faith = { religion_tag = judaism_religion }
					}
				}
			}
		}
		min = {
			value = 0.1
			desc = minimum_conversion
		}
	}

	potential_county = {
		scope:county = {
			is_landless_type_title = no
			#China changes the faith to the Huangdi's faith
			trigger_if = {
				limit = {
					scope:councillor_liege = {
						highest_held_title_tier >= tier_hegemony
						government_has_flag = government_has_merit
					}
				}
				faith != scope:councillor_liege.faith
			}
			#Otherwise it's councillor's faith
			trigger_else = { faith != scope:councillor.faith }
			trigger_if = {
				limit = {
					scope:councillor_liege = {
						is_alive = yes
						has_variable = hold_court_8120_religion_block
					}
				}
				custom_tooltip = {
					text = hold_court_8120_conversion_tt
					faith != scope:councillor_liege.var:hold_court_8120_religion_block
				}
			}
			custom_description = {
				text = "is_protected_via_contract_self_or_liege"
				subject = holder
				NAND = { # Vassal Contract forbids meddling by liege
					exists = holder.liege
					holder = {
						OR = {
							AND = {
								liege = scope:councillor_liege
								is_ruler = yes
								top_liege != this
								vassal_contract_has_flag = religiously_protected
							}
							any_liege_or_above = {
								liege ?= scope:councillor_liege
								is_ruler = yes
								top_liege != this
								vassal_contract_has_flag = religiously_protected
							}
						}
					}
				}
			}
			trigger_if = {
				limit = {	
					AND = {
						has_variable = cannot_be_converted_by_value
						var:cannot_be_converted_by_value = scope:councillor_liege		
					}
				}
				custom_tooltip = {
					text = promised_to_not_convert_county	
					NOR = {
						has_variable = cannot_be_converted_by_value
						var:cannot_be_converted_by_value = scope:councillor_liege	
					}
				}
			}
			# Unreformed Pagans do not tend to even try conversion of non-unreformed faiths unless zealous
			trigger_if = {
				limit = {
					scope:councillor_liege = {
						is_ai = yes
						faith = {
							has_doctrine_parameter = unreformed
						}
					}
				}
				OR = {
					scope:councillor_liege = {
						has_character_flag = vassal_directive_convert_faith
						vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
					}
					scope:councillor_liege = { ai_zeal >= 50 }
					scope:county.faith = { has_doctrine_parameter = unreformed }
					scope:county = { # Always convert holy sites
						any_county_province = {
							barony = {
								is_holy_site_of = scope:councillor_liege.faith
							}
						}
					}
				}
			}
			# Do not convert Righteous faiths, unless disturbingly zealous
			trigger_if = {
				limit = {
					scope:councillor_liege = {
						is_ai = yes
						faith = {
							faith_hostility_level = {
								target = scope:county.faith
								value <= faith_fully_accepted_level
							}
						}
					}
				}
				OR = {
					scope:councillor_liege = {
						has_character_flag = vassal_directive_convert_faith
						vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
					}
					scope:councillor_liege = {
						ai_zeal >= 100
						ai_rationality <= -50
					}
					scope:county = { # Always convert holy sites
						any_county_province = {
							barony = {
								is_holy_site_of = scope:councillor_liege.faith
							}
						}
					}
				}
			}
			# Converting Astray faiths only happens during certain circumstances
			trigger_if = {
				limit = {
					scope:councillor_liege = {
						is_ai = yes
						faith = {
							faith_hostility_level = {
								target = scope:county.faith
								value <= faith_astray_level
							}
						}
					}
				}
				trigger_if = {
					limit = {
						scope:councillor_liege = {
							has_tolerant_faith_or_culture_trigger = yes
						}
					}
					OR = {
						scope:councillor_liege = {
							has_character_flag = vassal_directive_convert_faith
							vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
						}
						scope:councillor_liege = { ai_zeal >= 100 }
						scope:county = { county_opinion <= -25 }
						scope:county = { # Always convert holy sites
							any_county_province = {
								barony = {
									is_holy_site_of = scope:councillor_liege.faith
								}
							}
						}
					}
				}
				trigger_else_if = {
					limit = {
						scope:councillor_liege = {
							faith = { has_doctrine = doctrine_pluralism_fundamentalist }
						}
					}
					OR = {
						scope:councillor_liege = {
							has_character_flag = vassal_directive_convert_faith
							vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
						}
						scope:councillor_liege = { ai_zeal >= 0 }
						scope:county = { county_opinion <= -10 }
						scope:county = { # Always convert holy sites
							any_county_province = {
								barony = {
									is_holy_site_of = scope:councillor_liege.faith
								}
							}
						}
					}
				}
				trigger_else = {
					OR = {
						scope:councillor_liege = {
							has_character_flag = vassal_directive_convert_faith
							vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
						}
						scope:councillor_liege = { ai_zeal >= 25 }
						scope:county = { county_opinion <= -15 }
						scope:county = { # Always convert holy sites
							any_county_province = {
								barony = {
									is_holy_site_of = scope:councillor_liege.faith
								}
							}
						}
					}
				}
			}
			# Ash'ari in Spain tend to not convert Muwhalladi often
			trigger_if = {
				limit = {
					scope:councillor_liege = {
						is_ai = yes
						faith = faith:ashari
						capital_province ?= { geographical_region = world_europe_west_iberia }
						scope:county.faith = faith:muwalladi
					}
					scope:county = {
						title_province = { geographical_region = world_europe_west_iberia }
					}
				}
				OR = {
					scope:councillor_liege = {
						has_character_flag = vassal_directive_convert_faith
						vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
					}
					scope:councillor_liege = { ai_zeal >= 50 }
					scope:county = { county_opinion <= -20 }
					scope:county = { # Always convert holy sites
						any_county_province = {
							barony = {
								is_holy_site_of = scope:councillor_liege.faith
							}
						}
					}
				}
			}
			# If a ruler has Jizya tax, they'll only convert the same *religious family* when ai_zeal is really high, they're converting unreformed pagans, counties in a kingdom with a Holy Site, or others of the same religion
			trigger_if = {
				limit = {
					scope:councillor_liege = {
						is_ai = yes
						faith = {
							OR = {
								has_doctrine = tenet_tax_nonbelievers
								has_doctrine = special_doctrine_jizya
							}
						}
					}
					OR = {
						AND = {
							scope:councillor_liege.religion = { is_in_family = rf_pagan }
							scope:county.religion = { is_in_family = rf_pagan }
						}
						AND = {
							scope:councillor_liege.religion = { is_in_family = rf_eastern }
							scope:county.religion = { is_in_family = rf_eastern }
						}
						AND = {
							scope:councillor_liege.religion = { is_in_family = rf_abrahamic }
							scope:county.religion = { is_in_family = rf_abrahamic }
						}
					}
				}
				OR = {
					scope:councillor_liege = {
						has_character_flag = vassal_directive_convert_faith
						vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
					}
					scope:councillor_liege = { ai_zeal >= 50 }
					scope:county.faith = { has_doctrine_parameter = unreformed }
					scope:councillor_liege.faith = {
						any_holy_site = {
							county = {
								kingdom = scope:county.kingdom
							}
						}
					}
					#scope:county.religion = scope:councillor_liege.religion # Excluded, as tax nonbelievers actually works for different faiths within the same religion
					AND = { # Islamic heartlands, to help fend off encroaching faiths
						scope:councillor_liege.religion = religion:islam_religion
						scope:county.title_province = {
							geographical_region = islamic_heartlands
						}
					}
				}
			}
			# Struggle phases may sometimes restrict targets for involved characters.
			trigger_if = {
				# Give an exception for pre-existing conversions: these can take a while and it's not fair to lose years or decades of progress to a phase change.
				limit = {
					NOT = {
						scope:county = { is_target_of_council_task = task_conversion }
					}
				}
				NOT = {
					scope:councillor_liege = {
						custom_tooltip = {
							text = task_conversion.tt.forbidden.due_to_struggle_phase
							any_character_struggle = {
								involvement = involved
								is_struggle_parameter_active_secondary_county_trigger = {
									PARAMETER = county_conversion_disabled_for_involved_faiths_within_region
									COUNTY = scope:county
								}
							}
						}
					}
				}
			}
		}
	}

	on_start_task_county = {
		# Message for vassals
		scope:county = { save_scope_as = set_county }
		trigger_event = {
			id = court_chaplain_task.8000
			days = 2 # To ensure it fires after the halted message
		}
		#spawn a task contract to counter the missionary, if a nearby adventurer has the same faith as the county
		if = {
			limit = {
				any_pool_character = {
					province = scope:county.title_province
					faith = scope:county.faith
					is_adult = yes
					save_temporary_scope_as = employer
				}
				any_ruler = {
					has_government = landless_adventurer_government
					is_within_diplo_range = { CHARACTER = scope:councillor }
					any_character_task_contract = {
						task_contract_type = laamp_fight_faith_conversion_contract
						count <= council_task_contracts_limit_value
					}
					NOT = {
						any_task_contract = {
							task_contract_type = laamp_fight_faith_conversion_contract
							task_contract_location = scope:employer.location
						}
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_type = laamp_fight_faith_conversion_contract
					task_contract_tier = scope:employer.task_contract_tier_value
					location = scope:county.title_province
					task_contract_employer = scope:employer
					target = scope:councillor
					destination = scope:county.title_province
				}
			}
		}
	}

	on_cancel_task_county = {
		# Message for vassals
		trigger_event = {
			id = court_chaplain_task.8001
			days = 1 # To ensure it does not fire at the same time as the finish message (both cancel and finish are fired on_finish_task_county)
		}
	}

	on_finish_task_county = {
		scope:county.faith = { save_scope_as = old_faith }
		scope:councillor_liege = {
			if = {
				limit = {
					any_vassal = {
						vassal_stance = minority
					}
				}
				custom_tooltip = minority_disapproves_conversion
				hidden_effect = {
					every_vassal = {
						vassal_stance = minority
						add_opinion = {
							target = scope:councillor_liege
							modifier = minority_disapproves_of_conversion_opinion
						}
					}
				}
			}
			add_to_variable_list = {
				name = faiths_converted_in_counties
				target = scope:old_faith
			}
		}
		scope:county = {
			add_to_variable_list = {
				name = faiths_converted_in_the_county
				target = scope:old_faith
				years = 40
			}
			scope:old_faith = {
				if = {
					limit = {
						OR = {
							NOT = { has_variable = associated_county }
							AND = {
								has_variable = converted_development
								var:converted_development <= 0
							}
						}
					}
					set_variable = {
						name = associated_county
						value = scope:county
					}
				}
				if = {
					limit = {
						OR = {
							NOT = { has_variable = associated_culture }
							AND = {
								has_variable = converted_development
								var:converted_development <= 0
							}
						}
					}
					set_variable = {
						name = associated_culture
						value = scope:county.culture
					}
				}
				if = {
					limit = {
						NOT = { has_variable = converted_development }
					}
					set_variable = {
						name = converted_development
						value = scope:county.convert_faith_development_value
					}
				}
				else = {
					change_variable = {
						name = converted_development
						add = scope:county.convert_faith_development_value
					}
				}
			}
			change_development_level = convert_faith_development_loss
			if = {
				limit = {
					scope:councillor_liege = {
						highest_held_title_tier >= tier_hegemony
						government_has_flag = government_has_merit
					}
				}
				set_county_faith = scope:councillor_liege.faith
			}
			else = { set_county_faith = scope:councillor.faith }
			
		}
		if = {
			limit = {
				scope:councillor_liege = {
					OR = {
						NOT = { has_variable_list = converted_faiths }
						NOT = {
							is_target_in_variable_list = {
								name = converted_faiths
								target = scope:old_faith
							}
						}
					}
				}
			}
			scope:councillor_liege = {
				add_to_variable_list = {
					name = converted_faiths
					target = scope:old_faith
				}
			}
		}
		if = {
			limit = {
				scope:councillor = {
					has_council_position = councillor_court_chaplain
				}
			}
			scope:councillor = {
				start_default_task = yes
			}
		}

		# Struggle parameters
		scope:councillor_liege = {
			if = {
				limit = {
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = piety_from_converting_county
					}
				}
				add_piety = medium_piety_gain
			}
		}
		# Message for vassals
		trigger_event = court_chaplain_task.8002
	}

	monthly_on_action = task_convert_side_effects

	ai_will_do = {
		value = 1000 # Always convert if possible

		if = {	#Unless you're a crypto-religionist.
			limit = {
				scope:councillor_liege = {
					any_secret = { type = secret_crypto_religionist }
				}
			}
			multiply = 0
		}

		# AI directive
		if = {
			limit = {
				scope:councillor_liege = {
					has_character_flag = vassal_directive_convert_faith
					vassal_is_valid_and_follows_directive_trigger = { VASSAL = this LIEGE = liege }
				}
			}
			add = 10000
		}
	}
}

task_fabricate_claim = {
	position = councillor_court_chaplain

	task_type = task_type_county
	county_target = all
	ai_county_target = neighbor_land
	task_progress = task_progress_percentage
	restart_on_finish = yes

	asset = {
		icon = "gfx/interface/icons/council_task_types/task_fabricate_claim.dds"
		background = "gfx/interface/buttons/button_round_bg.dds"
		frame = "gfx/interface/buttons/button_round_frame.dds"
		glow = "gfx/particles/halo.dds"
	}

	is_shown = {
		trigger_if = {
			limit = {
				scope:councillor_liege = { tgp_has_access_to_ministry_trigger = yes }
			}
			has_council_position = minister_justice
		}
	}

	effect_desc = {
		desc = task_fabricate_claim_effect_desc

		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					learning > mediocre_skill_rating
				}
				desc = task_fabricate_claim_duchy_claim
			}
			triggered_desc = {
				trigger = {
					learning < high_skill_rating
				}
				desc = task_fabricate_claim_vassal_opinion_loss
			}
			triggered_desc = {
				trigger = {
					learning < high_skill_rating
				}
				desc = task_fabricate_claim_piety_loss
			}
			triggered_desc = {
				trigger = {
					learning < high_skill_rating
				}
				desc = task_fabricate_claim_upset_target
			}
		}
	}

	progress = {
		value = 0
		add = {
			value = court_chaplain_fabricate_claim_base
			desc = COURT_CHAPLAIN_PROGRESS_BASE
		}
		add = {
			value = scope:councillor.court_chaplain_fabricate_claim_monthly_increase_base
			desc = SCALED_COUNCILLOR_LEARNING_VALUE
		}
		# Relation Bonuses/Penalties
		if = { # Friend
			limit = {
				scope:councillor_liege = {
					has_relation_friend = scope:councillor
					NOT = { has_relation_best_friend = scope:councillor }
				}
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_monthly_increase_friend_bonus
				desc = COUNCILLOR_IS_YOUR_FRIEND
			}
		}
		if = { # Best Friend
			limit = {
				scope:councillor_liege = {
					has_relation_best_friend = scope:councillor
				}
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_monthly_increase_best_friend_bonus
				desc = COUNCILLOR_IS_YOUR_BEST_FRIEND
			}
		}
		if = { # Rival
			limit = {
				scope:councillor_liege = {
					has_relation_rival = scope:councillor
					NOT = { has_relation_nemesis = scope:councillor }
				}
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_monthly_increase_rival_bonus
				desc = COUNCILLOR_IS_YOUR_RIVAL
			}
		}
		if = { # Nemesis
			limit = {
				scope:councillor_liege = {
					has_relation_nemesis = scope:councillor
				}
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_monthly_increase_nemesis_bonus
				desc = COUNCILLOR_IS_YOUR_NEMESIS
			}
		}

		# Perk Bonuses
		if = {
			limit = {
				scope:councillor_liege = { has_perk = accomplished_forger_perk }
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_perk_bonus_monthly_increase
				desc = FABRICATE_CLAIM_PERK_BONUS_VALUE
			}
		}
		if = {
			limit = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_dynasty_perk_bonus
				desc = FABRICATE_CLAIM_DYNASTY_PERK_BONUS_VALUE
			}
		}
		if = {
			limit = {
				councillor_liege_has_family_business_bonus = yes
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_family_business_bonus
				desc = FAMILY_BUSINESS_BONUS_VALUE
			}
		}
		if = {
			limit = {
				councillor_liege_has_consulted_house_bonus = yes
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_consulted_house_bonus
				desc = FAMILY_BUSINESS_BONUS_VALUE
			}
		}

		# Tradition Bonuses
		if = {
			limit = {
				scope:councillor.liege = {
					culture = {
						has_cultural_parameter = languages_speed_up_claims
					}
				}
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_tradition_language_scholars_bonus
				desc = FABRICATE_CLAIM_LANGUAGE_SCHOLARS_BONUS
			}
		}

		if = { # Inspection Reward
			limit = {
				scope:councillor_liege = {
					has_character_modifier = inspection_reward_focused_claims_modifier
				}
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_inspection_reward_bonus
				desc = FABRICATE_CLAIM_INSPECTION_BONUS
			}
		}

		# Innovation Bonuses
		if = {
			limit = {
				scope:councillor.liege = {
					culture = {
						has_innovation = innovation_land_grants
					}
				}
			}
			add = {
				value = scope:councillor.court_chaplain_fabricate_claim_innovation_bonus_land_grants
				desc = COURT_CHAPLAIN_FABRICATE_CLAIM_INNOVATION_BONUS_LAND_GRANTS
			}
		}
		if = {
			limit = {
				exists = scope:county
				scope:county.holder.faith != scope:councillor.liege.faith
			}
			multiply = {
				value = scope:councillor.court_chaplain_heretic_bonus
				desc = "COURT_CHAPLAIN_FABRICATE_CLAIM_HERETIC_BONUS"
			}
		}
		if = {
			limit = {
				exists = scope:county
				NOT = {
					scope:county = {
						is_neighbor_to_realm = scope:councillor_liege
					}
				}
			}
			multiply = {
				value = scope:councillor.court_chaplain_fabricate_claim_not_adjacent_penalty
				desc = "COURT_CHAPLAIN_FABRICATE_CLAIM_NOT_ADJACENT"
			}
		}
		if = { #Progress penalty when fabricating on vassals
			limit = {
					exists = scope:county
					scope:county.holder = {
						target_is_liege_or_above = scope:councillor.liege 
				}
			}
			multiply = {
				value = scope:councillor.court_chaplain_fabricate_claim_vassal_penalty
				desc = "COURT_CHAPLAIN_FABRICATE_CLAIM_VASSAL_COUNTY"
			}
		}
		# Misc Bonuses
		if = { # FP3
			limit = {
				scope:councillor_liege = {
					any_character_struggle = {
						is_struggle_type = persian_struggle
						is_struggle_phase = struggle_persia_phase_unrest
					}
				}
			}
			add = { # +25% of total
				value = scope:councillor.court_chaplain_fabricate_claim_base_total
				divide = 4
				desc = FABRICATE_CLAIM_ANY_STRUGGLE_BONUS
			}
		}
		if = {
			limit = {
				scope:councillor_liege = {
					has_title = title:c_byzantion
					title:c_byzantion = { has_county_modifier = pandidakterion_university_modifier }
				}
			}
			multiply = {
				value = 1.3
				desc = pandidakterion_university_modifier
			}
		}
	}

	potential_county = {
		scope:county.holder.top_liege = {
			in_diplomatic_range = scope:councillor_liege
		}
		scope:county = {
			is_landless_type_title = no
			NOR = {
				holder = scope:councillor_liege
				scope:councillor_liege = {
					has_claim_on = scope:county
				}
			}
		}
	}

	on_start_task_county = {
		#spawn a task contract to counter the missionary, if a nearby adventurer has the same faith as the county
		if = {
			limit = {
				any_task_contract = {
					task_contract_type = laamp_help_fabricate_claim_contract
					NOT = {	task_contract_location = scope:county.title_province }
				}
				any_player = {
					has_government = landless_adventurer_government
					is_within_diplo_range = { CHARACTER = scope:councillor }
					any_character_task_contract = {
						task_contract_type = laamp_help_fabricate_claim_contract
						count <= council_task_contracts_limit_value
					}
					can_create_task_contract = {
						type_name = laamp_help_fabricate_claim_contract
						employer = scope:councillor
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_type = laamp_help_fabricate_claim_contract
					task_contract_tier = scope:councillor.task_contract_tier_value
					location = scope:county.title_province
					task_contract_employer = scope:councillor
					target = scope:county.holder
					destination = scope:county.title_province
				}
			}
		}
	}

	on_finish_task_county = {
		scope:county = {
			holder = {
				save_scope_as = county_holder
			}
		}
		scope:councillor_liege = {
			trigger_event = {
				on_action = task_fabricate_claim_success_effect
			}
		}
	}

	monthly_on_action = task_fabricate_claim_side_effects

	ai_target_score = {
		value = 1000
		
		if = {
			limit = { # Vassals in the same realm
				scope:councillor_liege = {
					top_liege != this
				}
				scope:county.holder = {
					top_liege != this
				}
				scope:county.holder.top_liege = scope:councillor_liege.top_liege
			}
			
			if = {
				limit = {
					exists = scope:county.holder.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege
						}
					}
					scope:county.holder.max_military_strength > scope:councillor_liege.council_task_fabricate_claim_relative_strength
				}
				subtract = 10000
			}
			else_if = {
				limit = {
					exists = scope:county.holder.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege
						}
					}
				}
				multiply = {
					value = scope:councillor_liege.max_military_strength
					divide = { value = scope:county.holder.max_military_strength min = 1 }
				}
			}
			
			else_if = {
				limit = {
					exists = scope:county.holder.liege.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege.liege
						}
					}
					scope:county.holder.liege.max_military_strength > scope:councillor_liege.council_task_fabricate_claim_relative_strength
				}
				subtract = 10000
			}
			else_if = {
				limit = {
					exists = scope:county.holder.liege.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege.liege
						}
					}
				}
				multiply = {
					value = scope:councillor_liege.max_military_strength
					divide = { value = scope:county.holder.liege.max_military_strength min = 1 }
				}
			}
			
			else_if = {
				limit = {
					exists = scope:county.holder.liege.liege.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege.liege.liege
						}
					}
					scope:county.holder.liege.liege.max_military_strength > scope:councillor_liege.council_task_fabricate_claim_relative_strength
				}
				subtract = 10000
			}
			else_if = {
				limit = {
					exists = scope:county.holder.liege.liege.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege.liege.liege
						}
					}
				}
				multiply = {
					value = scope:councillor_liege.max_military_strength
					divide = { value = scope:county.holder.liege.liege.max_military_strength min = 1 }
				}
			}
			
			else_if = {
				limit = {
					exists = scope:county.holder.liege.liege.liege.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege.liege.liege.liege
						}
					}
					scope:county.holder.liege.liege.liege.max_military_strength > scope:councillor_liege.council_task_fabricate_claim_relative_strength
				}
				subtract = 10000
			}
			else_if = {
				limit = {
					exists = scope:county.holder.liege.liege.liege.liege
					scope:councillor_liege = {
						any_liege_or_above = {
							this = scope:county.holder.liege.liege.liege.liege
						}
					}
				}
				multiply = {
					value = scope:councillor_liege.max_military_strength
					divide = { value = scope:county.holder.liege.liege.liege.max_military_strength min = 1 }
				}
			}
		}
		else_if = {
			limit = {
				scope:county.holder.top_liege.max_military_strength > scope:councillor_liege.council_task_fabricate_claim_relative_strength
			}
			subtract = 10000
		}
		else = {	
			multiply = {
				value = scope:councillor_liege.max_military_strength
				divide = { value = scope:county.holder.top_liege.max_military_strength min = 1 }
			}
		}
	}

	ai_will_do = {
		value = 0
		if = {
			limit = {
				scope:councillor = {
					is_performing_council_task = task_fabricate_claim
				}
			}
			add = 10000 # Always keep going if this is already chosen
		}
		else_if = {
			limit = {
				OR = {
					ai_greed <= ai_honor
					ai_greed <= 0
					is_at_war = yes
					gold < 100
				}
			}
			subtract = 1000
		}
		else_if = { # If AI has good war, don't fabricate
			limit = {
				any_neighboring_and_across_water_top_liege_realm_owner = {
					scope:councillor_liege = {
						tier_difference = {
							target = prev
							value >= -1
						}
						NOR = {
							is_allied_to = prev
							has_truce = prev
							has_relation_friend = prev
							has_relation_lover = prev
						}
						current_military_strength > prev.current_military_strength
						has_any_cb_on = prev
					}			
				}
			}
			subtract = 1000
		}
		else = {
			add = 100
		}

		# Agenda impact
		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = iberian_struggle
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						any_character_struggle = {
							phase_has_catalyst = catalyst_gain_claim_on_title_fp2
						}
						has_character_flag = agenda_towards_escalation
					}
					add = 200
				}
				else_if = {
					limit = {
						any_character_struggle = {
							phase_has_catalyst = catalyst_gain_claim_on_title_fp2
						}
					}
					add = -100
				}
			}
		}

		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = persian_struggle
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						any_character_struggle = {
							phase_has_catalyst = catalyst_gain_claim_on_title_fp3
						}
						has_trait = fp3_struggle_detractor
					}
					add = 200
				}
				else_if = {
					limit = {
						any_character_struggle = {
							phase_has_catalyst = catalyst_gain_claim_on_title_fp3
						}
					}
					add = -100
				}
			}
		}
	}
}
