﻿task_religious_relations = {
	default_task = yes
	position = councillor_court_chaplain

	task_type = task_type_general
	task_progress = task_progress_infinite

	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
		}

		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
		}
		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 = {
		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_modifier
	}

	council_owner_modifier = {
		name = court_chaplain_religious_relations_perk_bonus_modifier
		monthly_piety = 1
		scale = court_chaplain_religious_relations_perk_bonus_modifier
	}

	council_owner_modifier = {
		name = court_chaplain_religious_relations_erudition_legacy_5_bonus_modifier
		monthly_piety = 1
		scale = court_chaplain_religious_relations_erudition_legacy_5_bonus_modifier
	}

	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

	effect_desc = {
		desc = task_conversion_effect_desc
		triggered_desc = {
			trigger = {
				scope:councillor_liege = {
					any_vassal = {
						has_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
				scope:county.faith.religion = religion:christianity_religion
			}
			add = {
				value = fp2_lyonese_monk_conversion_bonus_actual_value
				desc = fp2_lyonese_monk_faith_conversion_task_desc
			}
		}

		# 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 }
				NOT = { 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
					fp2_struggle_faith_is_mozarabic_trigger = no
					NOT = { exists = religious_head }
				}
			}
			multiply = {
				value = council_toledo_conversion_modifier_value
				desc = council_toledo_heresies_task_desc
			}
		}
		
		# 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 = christianity_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
			}
		}
	
		### TFE county conversion changes
		# Bonus for converting a province within the Empire to its Official Faith
		if = {
			limit = {
				OR = {
					exists = scope:councillor_liege.primary_title.var:official_imperial_faith
					AND = {
						exists = scope:councillor_liege.top_liege
						exists = scope:councillor_liege.top_liege.primary_title.var:official_imperial_faith
					}
				}
				OR = {
					AND = {
						exists = scope:councillor_liege.primary_title.var:official_imperial_faith
						scope:councillor.faith = scope:councillor_liege.primary_title.var:official_imperial_faith
					}
					AND = {
						exists = scope:councillor_liege.top_liege
						exists = scope:councillor_liege.top_liege.primary_title.var:official_imperial_faith
						scope:councillor.faith = scope:councillor_liege.top_liege.primary_title.var:official_imperial_faith
					}
				}
			}
			multiply = {
				value = 1.1
				desc = COURT_CHAPLAIN_CONVERSION_OFFICIAL_IMPERIAL_FAITH_EMPIRE
			}
		}
		# Eranshar bonus
		if = {
			limit = {
				scope:councillor_liege = {
					vassal_contract_has_flag = eranshar_special_contracts_religious_enforcment
				}
			}
			add = {
				value = 30
			}
		}
	}

	potential_county = { 
		scope:county = {
			NOT = { 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
					NOT = { 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
								is_independent_ruler = no
								vassal_contract_has_flag = religiously_protected
							}
							any_liege_or_above = {
								exists = liege
								liege = scope:councillor_liege
								is_ruler = yes
								is_independent_ruler = no
								vassal_contract_has_flag = religiously_protected
							}
						}
					}
				}
			}
			# 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 = { 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 = { ai_zeal >= 100 }
					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 = { 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
								}
							}
						}
					}
				}
				trigger_else_if = {
					limit = {
						scope:councillor_liege = {
							faith = { has_doctrine = doctrine_pluralism_fundamentalist }
						}
					}
					OR = {
						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 = { ai_zeal >= 10 }
						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 = { 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 = {
							has_doctrine = tenet_tax_nonbelievers
						}
					}
					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 = { 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_finish_task_county = {
		scope:councillor_liege = {
			if = {
				limit = {
					any_vassal = {
						has_vassal_stance = minority
					}
				}
				custom_tooltip = minority_disapproves_conversion
				hidden_effect = {
					every_vassal = {
						limit = {
							has_vassal_stance = minority
						}
						add_opinion = {
							target = scope:councillor_liege
							modifier = minority_disapproves_of_conversion_opinion
						}
					}
				}
			}
		}
		scope:county = {
			set_county_faith = scope:councillor.faith
		}
		if = {
			limit = {
				scope:councillor = {
					has_council_position = councillor_court_chaplain
				}
			}
			scope:councillor = {
				start_default_task = yes
			}
		}
	}

	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 = { has_variable = false_convert }
			}
			multiply = 0
		}
	}
}

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

	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
			}
		}

		# 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
			}
		}

		# 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
				NOT = {
					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"
			}
		}
	}

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

	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 = {
					is_independent_ruler = no
				}
				scope:county.holder = {
					is_independent_ruler = no
				}
				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_any_cb_on = prev
					}			
				}
			}
			subtract = 1000
		}
		else = {
			add = 100
		}

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