﻿independence_faction = {
	casus_belli = independence_faction_war
	
	short_effect_desc = independence_faction_short_effect_desc

	sort_order = 2

	discontent_progress = {
		base = 0

		# We use a modifier as our base progress in order to take advantage of script values.
		modifier = {
			add = base_discontent_progress
			desc = "BASE_COLON"
		}

		# The higher above the power threshold the faction is, the faster discontent increases.
		modifier = {
			faction_power > faction_power_threshold
			add = faction_discontent_for_extra_power
			desc = "FACTION_DISCONTENT_POWER_ABOVE_THRESHOLD"
		}

		# Discontent slowly decays if power is under the threshold.
		modifier = {
			faction_power < faction_power_threshold
			add = {
				add = base_discontent_progress
				multiply = -2
			}
			desc = "FACTION_DISCONTENT_POWER_BELOW_THRESHOLD"
		}
	}

	power_threshold = {
		base = 80

		modifier = {
			add = 20
			faction_target = {
				has_perk = hard_rule_perk
			}
			desc = "FACTION_POWER_HARD_RULE"
		}
	}

	is_character_valid = {
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = cp:councillor_court_chaplain
				this = cp:councillor_court_chaplain
			}
		}

		scope:faction.faction_target = liege
		highest_held_title_tier > tier_barony

		#Not blocked through events
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}
	}

	demand = {
		save_scope_as = faction

		faction_leader = {
			save_scope_as = faction_leader
		}

		faction_target = {
			save_scope_as = faction_target
		}

		# Let the human players in the faction know that the demand will be sent
		every_faction_member = {
			limit = {
				is_ai = no
				NOT = { this = scope:faction.faction_leader }
			}
			trigger_event = faction_demand.0005
		}

		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = faction_demand.0001
				days = 5
			}
		}
	}

	ai_create_score = {
		base = -175 # Base reluctance value we must overcome to start an Independence Faction.

		############
		# BLOCKERS #

		# Refuses to make if there was a recent independence faction revolt.
		modifier = {
			add = -1000
			scope:target = {
				has_character_flag = recent_independence_faction_war
			}
		}


		# Do not join a new faction if I am already at war.
		modifier = {
			add = -1000
			is_at_war = yes
		}


		# Avoid making an independence faction against my liege if they are my De Jure Liege, of my Faith, and of my Culture ( we should Depose/Claimant instead ).
		modifier = {
			add = -100
			AND = {
				culture = scope:target.culture
				faith = scope:target.faith
				scope:target = {
					any_held_title = {
						root.primary_title.de_jure_liege = this 
					}
				}
			}
		}

		# FamilyLoyalty
        modifier = {
			add = -75
            OR = {
                any_parent = { this = scope:target }
                any_sibling = { this = scope:target }
                any_child = { this = scope:target }
				any_spouse = { this = scope:target }
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				house = scope:target.house
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				dynasty = scope:target.dynasty
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}

		# If a suitable Populist Faction already exists, prefer to side with it
		modifier = {
			add = -50
			scope:target = {
				any_targeting_faction = {
	  				faction_is_type = populist_faction
	  				var:faction_faith = root.faith
	  				var:faction_culture = root.culture
				}
		  	}
		}
		modifier = {
			add = -50
			scope:target = {
				any_targeting_faction = {
	  				faction_is_type = populist_faction
	  				var:faction_faith = root.faith
	  				var:faction_culture.culture_group = root.culture_group
				}
		  	}
		}

		#######################
		# Standard AI Weights #

		# +40 join weight at -100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:target
			multiplier = -0.4
			min = 0
		}
		# -150 join weight at +100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:target
			multiplier = -1.5
			max = 0
		}

		# for each character.religion != factionTarget.religion add X join-score
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = 25
			HOSTILE_BASE_VALUE = 50
			EVIL_BASE_VALUE = 100
			BASE_FAITH = faith
			TARGET_FAITH = scope:target.faith
		}

		# for each character.culture != factionTarget.culture add X join-score
		modifier = {
			add = 25
			has_same_culture_group_as = scope:target
			NOT = { culture = scope:target.culture }
		}
		modifier = {
			add = 50
			NOT = {
				has_same_culture_group_as = scope:target
			}
		}

		# Not a de jure vassal
		modifier = {
			add = 125
			NOT = {
				scope:target = {
					any_held_title = {
						root.primary_title.de_jure_liege = this 
					}
				}
			}
		}

		# Larger realms are inherently more rebellious.
		modifier = {
			add = sub_realm_size
		}

		# Kingdoms gain an additional boost to independence, as they don't like being under the thumb of others.
		modifier = {
			add = 25
			highest_held_title_tier = tier_kingdom
		}

		# Toe the Line Perk
		modifier = {
			add = -50
			scope:target = {
				has_perk = toe_the_line_perk
			}
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:target
			SCORE_PER_TRAIT = 25
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:target
		}

		# Difficulty Settings
		modifier = { # Easy
			add = -50
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -150
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
	}

	ai_join_score = {
		base = -150 # Base reluctance value we must overcome to start an Independence Faction.

		############
		# BLOCKERS #

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -1000
			AND = {
				is_at_war = yes
				trigger_if = {
					limit = {
						exists = joined_faction
					}
					NOT = { joined_faction = scope:faction }
				}
			}
		}

		# Avoid joining an independence faction against my liege if they are my De Jure Liege, of my Faith, and of my Culture ( we should Depose/Claimant instead ).
		modifier = {
			add = -100
			AND = {
				culture = scope:faction.faction_target.culture
				faith = scope:faction.faction_target.faith
				scope:faction.faction_target = {
					any_held_title = {
						root.primary_title.de_jure_liege = this 
					}
				}
			}
		}

		# FamilyLoyalty
        modifier = {
			add = -75
            OR = {
                any_parent = { this = scope:faction.faction_target }
                any_sibling = { this = scope:faction.faction_target }
                any_child = { this = scope:faction.faction_target }
				any_spouse = { this = scope:faction.faction_target }
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				house = scope:faction.faction_target.house
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				dynasty = scope:faction.faction_target.dynasty
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}

		#######################
		# Standard AI Weights #

		# +50 join weight at -100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:faction.faction_target
			multiplier = -0.4
			min = 0
		}
		# -150 join weight at +100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:faction.faction_target
			multiplier = -1.5
			max = 0
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:faction.faction_target
		}

		# for each character.religion != factionTarget.religion add X join-score
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = 25
			HOSTILE_BASE_VALUE = 50
			EVIL_BASE_VALUE = 100
			BASE_FAITH = faith
			TARGET_FAITH = scope:faction.faction_target.faith
		}

		# for each character.culture != factionTarget.culture add X join-score
		modifier = {
			add = 25
			has_same_culture_group_as = scope:faction.faction_target
			NOT = { culture = scope:faction.faction_target.culture }
		}
		modifier = {
			add = 50
			NOT = {
				has_same_culture_group_as = scope:faction.faction_target
			}
		}

		# Not a de jure vassal
		modifier = {
			add = 125
			NOT = {
				scope:faction.faction_target = {
					any_held_title = {
						root.primary_title.de_jure_liege = this 
					}
				}
			}
		}

		# Larger realms are inherently more rebellious.
		modifier = {
			add = sub_realm_size
		}

		# Kingdoms gain an additional boost to independence, as they don't like being under the thumb of others.
		modifier = {
			add = 25
			highest_held_title_tier = tier_kingdom
		}

		# Toe the Line Perk
		modifier = {
			add = -50
			scope:faction.faction_target = {
				has_perk = toe_the_line_perk
			}
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:faction.faction_target
			SCORE_PER_TRAIT = 25
		}

		# Difficulty Settings
		modifier = { # Easy
			add = -50
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -150
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}

		##########################
		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		modifier = {
			scope:faction.faction_power < scope:faction.faction_power_minimal
			liege = {
				any_targeting_faction = {
					OR = {
						faction_is_type = liberty_faction
						AND = {
							faction_is_type = claimant_faction
							special_character = {
								reverse_opinion = {
									target = root
									value >= -20
								}	
							}	
						}
						AND = {
							faction_is_type = populist_faction
							var:faction_faith = root.faith
						}
					}
					faction_power >= faction_power_minimal
				}
			}
			factor = faction_weight_factor_power_minimal_penalty
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_halfway_threshold
			factor = faction_weight_factor_power_halfway_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_pushing_threshold
			factor = faction_weight_factor_power_pushing_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_threshold
			factor = faction_weight_factor_power_exceeds_threshold
		}
	}

	ai_demand_chance = {
		base = 0

		# 40% base chance at minimum power (80%), increasing linearly
		compare_modifier = {
			value = faction_power
			multiplier = 0.5
		}

		# Once the faction has a good chance to win (10% stronger than liege) demand chance increases much more rapidly.
		compare_modifier = {
			trigger = {	faction_power > 110 }
			value = faction_power
			multiplier = 1
		}

		modifier = {
			add = 100
			faction_target = {
				is_at_war = yes # Independence Factions are opportunistic bastards!
			}
		}
	}

	can_character_join = {

		is_adult = yes
		NOT = { is_allied_to = scope:faction.faction_target }
		scope:faction.faction_target = {
			NOT = { has_strong_hook = root }
		}

		OR = {
			is_ai = no
			NOR = {
				has_relation_lover = scope:faction.faction_target
				has_relation_friend = scope:faction.faction_target
			}
		}
		scope:faction.faction_target.highest_held_title_tier > tier_county

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	can_character_create = {
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = liege.cp:councillor_court_chaplain
				this = liege.cp:councillor_court_chaplain
			}
		}
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}
		liege = {
			is_independent_ruler = yes
		}

		is_adult = yes
		NOT = { is_allied_to = scope:target }
		scope:target = {
			NOT = { has_strong_hook = root }
		}

		OR = {
			is_ai = no
			NOR = {
				has_relation_lover = scope:target
				has_relation_friend = scope:target
			}
		}
		OR = {
			is_ai = no
			NOT = { culture = scope:target.culture }
			NOT = { faith = scope:target.faith }
			NOT = {
				scope:target = {
					any_held_title = {
						root.primary_title.de_jure_liege = this 
					}
				}
			}
		}
		scope:target.highest_held_title_tier > tier_county
		highest_held_title_tier > tier_barony

		####
		# BLOCKERS
		####
		# General Faction immunity
		custom_description = {
			text = character_is_immune_to_factions
			subject = scope:target
			NOT = { scope:target = { immune_to_factions_trigger = yes } }
		}

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	county_allow_join = no
	county_allow_create = no
}

liberty_faction = {
	casus_belli = liberty_faction_war

	short_effect_desc = liberty_faction_short_effect_desc

	sort_order = 4

	discontent_progress = {
		base = 0

		# We use a modifier as our base progress in order to take advantage of script values.
		modifier = {
			add = base_discontent_progress
			desc = "BASE_COLON"
		}

		# The higher above the power threshold the faction is, the faster discontent increases.
		modifier = {
			faction_power > faction_power_threshold
			add = faction_discontent_for_extra_power
			desc = "FACTION_DISCONTENT_POWER_ABOVE_THRESHOLD"
		}

		# Discontent slowly decays if power is under the threshold.
		modifier = {
			faction_power < faction_power_threshold
			add = {
				add = base_discontent_progress
				multiply = -2
			}
			desc = "FACTION_DISCONTENT_POWER_BELOW_THRESHOLD"
		}
	}

	power_threshold = {
		base = 80

		modifier = {
			add = 20
			faction_target = {
				has_perk = hard_rule_perk
			}
			desc = "FACTION_POWER_HARD_RULE"
		}
	}

	is_character_valid = {
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = cp:councillor_court_chaplain
				this = cp:councillor_court_chaplain
			}
		}

		scope:faction.faction_target = liege
		highest_held_title_tier > tier_barony

		#Not blocked through events
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}

	}

	demand = {
		save_scope_as = faction

		faction_leader = {
			save_scope_as = faction_leader
		}

		faction_target = {
			save_scope_as = faction_target
		}

		# Let the human players in the faction know that the demand will be sent
		every_faction_member = {
			limit = {
				is_ai = no
				NOT = { this = scope:faction.faction_leader }
			}
			trigger_event = faction_demand.0105
		}

		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = faction_demand.0101
				days = 5
			}
		}
	}

	ai_join_score = {
		base = 0 # Join Weight is set primarily based on what Crown Laws a realm has.

		############
		# BLOCKERS #

		modifier = {
			add = -100
			scope:faction.faction_target = {
				has_character_flag = recent_liberty_faction_war
			}
		}

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -100
			AND = {
				is_at_war = yes
				trigger_if = {
					limit = {
						exists = joined_faction
					}
					NOT = { joined_faction = scope:faction }
				}
			}
		}

		# FamilyLoyalty
        modifier = {
			add = -75
            OR = {
                any_parent = { this = scope:faction.faction_target }
                any_sibling = { this = scope:faction.faction_target }
                any_child = { this = scope:faction.faction_target }
				any_spouse = { this = scope:faction.faction_target }
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				house = scope:faction.faction_target.house
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				dynasty = scope:faction.faction_target.dynasty
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}

		#######################
		# Standard AI Weights #
		#
		# Modifiers for Liberty Factions are inherently small.
		# This is because Liberty is the 'Default Faction' that vassals are join if they don't want/can't join any other faction.

		# -100 join weight at +100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:faction.faction_target
			multiplier = -1
			max = 0
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:faction.faction_target
		}

		# 'Base Join Weight' depends on the current Crown Authority level.
		modifier = {
			add = 10 # AI won't join at +10 Opinion
			scope:faction.faction_target = {
				has_realm_law = crown_authority_1
			}
		}
		modifier = {
			add = 20 # AI won't join at +20 Opinion
			scope:faction.faction_target = {
				has_realm_law = crown_authority_2
			}
		}
		modifier = {
			add = 30 # AI won't join at +30 Opinion
			scope:faction.faction_target = {
				has_realm_law = crown_authority_3
			}
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:faction.faction_target
			SCORE_PER_TRAIT = 10 # Worth 10 opinion per virtue.
		}

		# Difficulty Settings
		modifier = { # Easy
			add = -25
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -75
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}

		##########################
		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_halfway_threshold
			factor = faction_weight_factor_power_halfway_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_pushing_threshold
			factor = faction_weight_factor_power_pushing_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_threshold
			factor = faction_weight_factor_power_exceeds_threshold
		}
	}

	ai_create_score = {
		base = 0 # Join Weight is set primarily based on what Crown Laws a realm has.

		############
		# BLOCKERS #

		modifier = {
			add = -100
			scope:target = {
				has_character_flag = recent_liberty_faction_war
			}
		}

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -100
			is_at_war = yes
		}

		# FamilyLoyalty
        modifier = {
			add = -75
            OR = {
                any_parent = { this = scope:target }
                any_sibling = { this = scope:target }
                any_child = { this = scope:target }
				any_spouse = { this = scope:target }
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				house = scope:target.house
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				dynasty = scope:target.dynasty
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}

		#######################
		# Standard AI Weights #
		#
		# Modifiers for Liberty Factions are inherently small.
		# This is because Liberty is the 'Default Faction' that vassals are join if they don't want/can't join any other faction.

		# -100 join weight at +100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:target
			multiplier = -1
			max = 0
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:target
		}

		# 'Base Join Weight' depends on the current Crown Authority level.
		modifier = {
			add = 10 # AI won't join at +10 Opinion
			scope:target = {
				has_realm_law = crown_authority_1
			}
		}
		modifier = {
			add = 20 # AI won't join at +20 Opinion
			scope:target = {
				has_realm_law = crown_authority_2
			}
		}
		modifier = {
			add = 30 # AI won't join at +30 Opinion
			scope:target = {
				has_realm_law = crown_authority_3
			}
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:target
			SCORE_PER_TRAIT = 10 # Worth 10 opinion per virtue.
		}

		# Difficulty Settings
		modifier = { # Easy
			add = -25
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -75
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
	}

	ai_demand_chance = {
		base = 0

		# 30% base chance at minimum power (80%), increasing linearly
		compare_modifier = {
			value = faction_power
			multiplier = 0.375
		}

		# Once the faction has a good chance to win (10% stronger than liege) demand chance increases much more rapidly.
		compare_modifier = {
			trigger = {	faction_power > 110 }
			value = faction_power
			multiplier = 0.75
		}

		# Less likely if already in a war with an external ruler - the Liberty Faction is pretty sporting
		modifier = {
			add = -75
			faction_target = {
				any_war_enemy = {
					NOT = {
						target_is_liege_or_above = root.faction_target
					}
				}
			}
		}
	}

	can_character_join = {
		is_adult = yes
		NOT = { is_allied_to = scope:faction.faction_target }
		scope:faction.faction_target = {
			NOT = { has_strong_hook = root }
		}

		OR = {
			is_ai = no
			NOR = {
				has_relation_lover = scope:faction.faction_target
				has_relation_friend = scope:faction.faction_target
			}
		}

		OR = {
			is_ai = no
			scope:faction.faction_target = {
				OR = {
					has_realm_law = crown_authority_1
					has_realm_law = crown_authority_2
					has_realm_law = crown_authority_3
				}
			}
		}

		scope:faction.faction_target.highest_held_title_tier > tier_county

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	can_character_create = {
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = liege.cp:councillor_court_chaplain
				this = liege.cp:councillor_court_chaplain
			}
		}
		is_adult = yes
		NOT = { is_allied_to = scope:target }
		scope:target = {
			NOT = { has_strong_hook = root }
		}

		OR = {
			is_ai = no
			NOR = {
				has_relation_lover = scope:target
				has_relation_friend = scope:target
			}
		}
		OR = {
			is_ai = no
			scope:target = {
				OR = {
					has_realm_law = crown_authority_1
					has_realm_law = crown_authority_2
					has_realm_law = crown_authority_3
				}
			}
		}
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}

		scope:target.highest_held_title_tier > tier_county
		highest_held_title_tier > tier_barony

		####
		# BLOCKERS
		####
		# General Faction immunity
		custom_description = {
			text = character_is_immune_to_factions
			subject = scope:target
			NOT = { scope:target = { immune_to_factions_trigger = yes } }
		}

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	county_allow_join = no
	county_allow_create = no
}

claimant_faction = {
	casus_belli = claimant_faction_war

	short_effect_desc = claimant_faction_short_effect_desc

	sort_order = 1

	show_special_title = yes
	name = FACTION_CLAMAINT_DYNAMIC_NAME

	discontent_progress = {
		base = 0

		# We use a modifier as our base progress in order to take advantage of script values.
		modifier = {
			add = base_discontent_progress
			desc = "BASE_COLON"
		}

		# The higher above the power threshold the faction is, the faster discontent increases.
		modifier = {
			faction_power > faction_power_threshold
			add = faction_discontent_for_extra_power
			desc = "FACTION_DISCONTENT_POWER_ABOVE_THRESHOLD"
		}

		# Discontent slowly decays if power is under the threshold.
		modifier = {
			faction_power < faction_power_threshold
			add = {
				add = base_discontent_progress
				multiply = -2
			}
			desc = "FACTION_DISCONTENT_POWER_BELOW_THRESHOLD"
		}
	}

	power_threshold = {
		base = 80

		modifier = {
			add = 20
			faction_target = {
				has_perk = hard_rule_perk
			}
			desc = "FACTION_POWER_HARD_RULE"
		}
	}

	is_valid = { # If the FACTION itself is valid
		# see also invalidate_claimant_factions_on_death_effect for what happens when the claimant dies
		
		exists = special_character
		special_title.holder = faction_target
		OR = {
			special_character = {
				is_alive = yes
				has_claim_on = root.special_title
				NOT = { has_trait = incapable }
			}
			exists = faction_war
		}
	}

	ai_demand_chance = {
		base = 0

		# 40% base chance at minimum power (80%), increasing linearly
		compare_modifier = {
			value = faction_power
			multiplier = 0.5
		}

		# Once the faction has a good chance to win (10% stronger than liege) demand chance increases much more rapidly.
		compare_modifier = {
			trigger = {	faction_power > 110 }
			value = faction_power
			multiplier = 1
		}

		# Less likely if already in a war with an external ruler
		modifier = {
			add = -50
			faction_target = {
				any_war_enemy = {
					NOT = {
						target_is_liege_or_above = root.faction_target
					}
				}
			}
		}
	}

	demand = { # The effect of pressing demand
		save_scope_as = faction

		faction_leader = {
			save_scope_as = faction_leader
		}

		faction_target = {
			save_scope_as = faction_target
		}

		special_character = {
			save_scope_as = faction_claimant
		}

		special_title = {
			save_scope_as = faction_targeted_title
		}

		# Let the human players in the faction know that the demand will be sent
		every_faction_member = {
			limit = {
				is_ai = no
				NOT = { this = scope:faction.faction_leader }
			}
			trigger_event = faction_demand.2005
		}

		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = faction_demand.2001
				days = 5
			}
		}
	}

	can_character_create = { # If a character is allowed to create the faction. The 'is_character_valid' block is also checked in conjunction with this when creating a faction.
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = cp:councillor_court_chaplain
				this = cp:councillor_court_chaplain
			}
		}

		trigger_if = {
			limit = {
				exists = joined_faction
			}
			joined_faction = {
				exists = special_title
				special_title.holder = scope:target
			}
		}
		trigger_else_if = {
			limit = {
				has_variable = claiming_title
			}
			var:claiming_title.holder = scope:target
		}
		trigger_else = {
			always = no
		}

		is_adult = yes
		NOT = { is_allied_to = scope:target }
		NOT = { has_truce = scope:target }
		scope:target = {
			NOT = { has_strong_hook = root }
		}

		OR = {
			is_ai = no
			NOR = {
				has_relation_lover = scope:target
				has_relation_friend = scope:target
			}
		}
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}

		####
		# BLOCKERS
		####
		# General Faction immunity
		custom_description = {
			text = character_is_immune_to_factions
			subject = scope:target
			NOT = { scope:target = { immune_to_factions_trigger = yes } }
		}

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	can_character_join = { # Can a character join an existing faction?
		is_adult = yes
		NOT = { is_allied_to = scope:faction.faction_target }
		scope:faction.faction_target = {
			NOT = { has_strong_hook = root }
		}

		SCOPE:faction.faction_target.highest_held_title_tier > tier_county
		highest_held_title_tier > tier_barony

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	is_character_valid = { # Is the character valid to stay in the faction? This is also checked when creating it...
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = cp:councillor_court_chaplain
				this = cp:councillor_court_chaplain
			}
		}

		exists = liege
		scope:faction.faction_target = liege
		highest_held_title_tier > tier_barony

		#Not blocked through events
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}
	}

	ai_create_score = { # How likely is an AI to create a faction?
		base = -25

		####
		# BLOCKERS
		####

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -1000
			is_at_war = yes
		}

		# Do not create if I put my current liege on the throne in a different claimant faction.
		modifier = {
			add = -100
			has_opinion_modifier = {
				modifier = claimant_faction_member_opinion
				target = scope:target
			}
		}

		# Do not start factions for certain claimants
		modifier = {
			add = -1000
			scope:claimant = {
				OR = {
					has_trait = incapable
					has_government = theocracy_government
				}
			}
		}

		# FamilyLoyalty
        modifier = {
			add = -75
            OR = {
                any_parent = { this = scope:target }
                any_sibling = { this = scope:target }
                any_child = { this = scope:target }
				any_spouse = { this = scope:target }
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				house = scope:target.house
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				dynasty = scope:target.dynasty
            }
			AND = {
				scope:target = {
					is_ai = no
				}
			}
		}

		####
		# AI modifiers
		####
		
		# Gender concerns (general)
		modifier = {
			add = -25
			NOT = { scope:claimant = root }
			OR = {
				AND = {
					faith = { has_doctrine = doctrine_gender_female_dominated }
					scope:claimant = {
						is_male = yes
						faith = root.faith
					}
				}
				AND = {
					faith = { has_doctrine = doctrine_gender_male_dominated }
					scope:claimant = {
						is_female = yes
						faith = root.faith
					}
				}
			}
		}
		
		# Gender concerns (target of wrong gender)
		modifier = {
			add = 25
			OR = {
				AND = {
					faith = { has_doctrine = doctrine_gender_female_dominated }
					scope:target = { is_male = yes }
					scope:claimant = {
						is_female = yes
						faith = root.faith
					}
				}
				AND = {
					faith = { has_doctrine = doctrine_gender_male_dominated }
					scope:target = { is_female = yes }
					scope:claimant = {
						is_male = yes
						faith = root.faith
					}
				}
			}
		}

		# Opinion of the Liege
		opinion_modifier = {
			who = root
			opinion_target = scope:target
			multiplier = -1.5
		}
		# Liege is too young to rule
		modifier = {
			add = 25
			scope:target = { is_adult = no }
		}

		# Opinion of the potential Claimant (not self)
		opinion_modifier = {
			trigger = {
				NOT = { scope:claimant = root }
			}
			who = root
			opinion_target = scope:claimant
			multiplier = 1.5
		}
		modifier = {
			add = { # Claimant is too old
				value = scope:claimant.age
				subtract = 45
				multiply = -2
			}
			AND = {
				NOT = {	scope:claimant = root }
				scope:claimant.age > 45
			}
		}
		modifier = {
			add = { # Claimant is dying
				value = 1.5
				subtract = scope:claimant.health
				multiply = 20
			}
			AND = {
				NOT = {	scope:claimant = root }
				scope:claimant.health < 1.5
			}
		}
		modifier = {
			add = -50
			AND = { # Claimant does not have children and is unlikely to have any.
				scope:claimant.fertility <= 0.1
				NOT = {
					scope:claimant = {
						any_child = {
							count >= 1
						}
					}
				}
			}
		}

		# I am the claimant!
		modifier = {
			value = {
				value = 50 # Basic 'self-love' bonus
				add = { 
					value = ai_greed
					multiply = 1.5
					min = 0
				}
			}
			AND = {
				scope:claimant = root
				NOT = { has_trait = content } # Not opposed to acquiring more power/wealth
			}
		}

		
		# Friends & Lovers don't want to join
		modifier = {
			add = -500
			OR = {
				has_secret_relation_lover = scope:target
				has_relation_lover = scope:target
				has_relation_soulmate = scope:target
				has_relation_friend = scope:target
				has_relation_best_friend = scope:target
			}
		}
		# Friends & Lovers want to support their claimants!
		modifier = {
			add = 25
			OR = {
				has_secret_relation_lover = scope:claimant
				has_relation_lover = scope:claimant
				has_relation_soulmate = scope:claimant
				has_relation_friend = scope:claimant
				has_relation_best_friend = scope:claimant
			}
		}
		# Best Friends & Soulmates really want to support their claimants!
		modifier = {
			add = 75
			OR = {
				has_relation_soulmate = scope:claimant
				has_relation_best_friend = scope:claimant
			}
		}

		# Cultural reasons
		modifier = {
			NOT = { culture = scope:target.culture }
			has_same_culture_group_as = scope:claimant
			add = {
				value = 25
				if = {
					limit = {
						culture = scope:claimant.culture
					}
					add = 25
				}
				if = {
					limit = {
						NOT = { has_same_culture_group_as = scope:target }
					}
					add = 25
				}
			}
		}

		# Religious reasons
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = 10
			HOSTILE_BASE_VALUE = 25
			EVIL_BASE_VALUE = 50
			BASE_FAITH = faith
			TARGET_FAITH = scope:target.faith
		}
		modifier = {
			add = 100
			NOT = { faith = scope:target.faith }
			faith = scope:claimant.faith
		}
		legalism_virtue_and_sin_modifier = {
			TARGET = scope:target
			SCORE_PER_TRAIT = 25
		}

		# Don't support claims against the liege's lower tier titles
		modifier = {
			add = -500
			scope:title.tier < scope:target.primary_title.tier
			scope:title.tier < tier_kingdom
		}

		# When creating a Faction, primarily care about Claims on Titles de jure above my primary title
		modifier = {
			add = -200
			NOT = {
				scope:title = {
					is_de_jure_liege_or_above_target = root.primary_title
				}
			}
			scope:target = {
				any_held_title = {
					NOT = { this = scope:title }
					OR = {
						this.tier >= scope:target.primary_title.tier
						this.tier >= tier_kingdom
					}
					is_de_jure_liege_or_above_target = root.primary_title
				}
			}
		}

		# If another Claimant Faction already exists, prefer to side with it
		modifier = {
			add = -50
			scope:target = {
				any_targeting_faction = {
			  		faction_is_type = claimant_faction
				  	NOT = {
			  			any_faction_member = {
			  				is_ai = no
			  			}
			  		}
		  		}
		  	}
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:target
		}

		# Difficulty Settings
		modifier = { # Easy
			add = -50
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -150
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
	}

	ai_join_score = { # How likely is an AI to join an existing faction?
		base = -25

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -1000
			AND = {
				is_at_war = yes
				trigger_if = {
					limit = {
						exists = joined_faction
					}
					NOT = { joined_faction = scope:faction }
				}
			}
		}

		# Do not join if I put my current liege on the throne in a different claimant faction.
		modifier = {
			add = -100
			has_opinion_modifier = {
				modifier = claimant_faction_member_opinion
				target = scope:faction.faction_target
			}
		}

		# FamilyLoyalty
        modifier = {
			add = -75
            OR = {
                any_parent = { this = scope:faction.faction_target }
                any_sibling = { this = scope:faction.faction_target }
                any_child = { this = scope:faction.faction_target }
				any_spouse = { this = scope:faction.faction_target }
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				house = scope:faction.faction_target.house
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}
		modifier = {
			add = -15
            OR = {
				dynasty = scope:faction.faction_target.dynasty
            }
			AND = {
				scope:faction.faction_target = {
					is_ai = no
				}
			}
		}
		
		# Gender concerns (general)
		modifier = {
			add = -25
			NOT = { scope:faction.special_character = root }
			OR = {
				AND = {
					faith = { has_doctrine = doctrine_gender_female_dominated }
					scope:faction.special_character = {
						is_male = yes
						faith = root.faith
					}
				}
				AND = {
					faith = { has_doctrine = doctrine_gender_male_dominated }
					scope:faction.special_character = {
						is_female = yes
						faith = root.faith
					}
				}
			}
		}
		
		# Gender concerns (target of wrong gender)
		modifier = {
			add = 25
			OR = {
				AND = {
					faith = { has_doctrine = doctrine_gender_female_dominated }
					scope:faction.faction_target = { is_male = yes }
					scope:faction.special_character = {
						is_female = yes
						faith = root.faith
					}
				}
				AND = {
					faith = { has_doctrine = doctrine_gender_male_dominated }
					scope:faction.faction_target = { is_female = yes }
					scope:faction.special_character = {
						is_male = yes
						faith = root.faith
					}
				}
			}
		}

		# Opinion of the Liege
		opinion_modifier = {
			who = root
			opinion_target = scope:faction.faction_target
			multiplier = -1.0
		}

		# Opinion of the potential Claimant
		opinion_modifier = {
			trigger = {
				NOT = { scope:faction.special_character = root }
			}
			who = root
			opinion_target = scope:faction.special_character
			multiplier = 1.0
		}

		ai_value_modifier = {
			trigger = {
				scope:faction.special_character = root
			}
			ai_greed = 3
		}

		# Incapable to rule
		modifier = {
			add = 25
			exists = scope:faction.faction_target
			exists = scope:faction.special_character
			scope:faction.faction_target = { is_adult = no }
			scope:faction.special_character = { is_adult = yes }
		}

		# Friends & Lovers don't want to join
		modifier = {
			add = -500
			OR = {
				has_secret_relation_lover = scope:faction.faction_target
				has_relation_lover = scope:faction.faction_target
				has_relation_soulmate = scope:faction.faction_target
				has_relation_friend = scope:faction.faction_target
				has_relation_best_friend = scope:faction.faction_target
			}
		}
		
		# Friends & Lovers want to support their claimants!
		modifier = {
			add = 25
			OR = {
				has_secret_relation_lover = scope:faction.special_character
				has_relation_lover = scope:faction.special_character
				has_relation_soulmate = scope:faction.special_character
				has_relation_friend = scope:faction.special_character
				has_relation_best_friend = scope:faction.special_character
			}
		}
		
		# Best Friends & Soulmates really want to support their claimants!
		modifier = {
			add = 75
			OR = {
				has_relation_soulmate = scope:faction.special_character
				has_relation_best_friend = scope:faction.special_character
			}
		}

		# Cultural reasons
		modifier = {
			NOT = { culture = scope:faction.faction_target.culture }
			has_same_culture_group_as = scope:faction.special_character
			add = {
				value = 25
				if = {
					limit = {
						culture = scope:faction.special_character.culture
					}
					add = 25
				}
				if = {
					limit = {
						NOT = { has_same_culture_group_as = scope:faction.faction_target }
					}
					add = 25
				}
			}
		}

		# Religious reasons
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = 10
			HOSTILE_BASE_VALUE = 25
			EVIL_BASE_VALUE = 50
			BASE_FAITH = faith
			TARGET_FAITH = scope:faction.faction_target.faith
		}
		modifier = {
			add = 100
			NOT = { faith = scope:faction.faction_target.faith }
			faith = scope:faction.special_character.faith
		}
		legalism_virtue_and_sin_modifier = {
			TARGET = scope:faction.faction_target
			SCORE_PER_TRAIT = 25
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:faction.faction_target
		}

		# All else being equal, the AI might support a claimant over their current liege just to get better feudal obligations.
		modifier = {
			add = 10
			scope:faction.faction_target = {
				has_realm_law = crown_authority_1
			}
		}
		modifier = {
			add = 15
			scope:faction.faction_target = {
				has_realm_law = crown_authority_2
			}
		}
		modifier = {
			add = 20
			scope:faction.faction_target = {
				has_realm_law = crown_authority_3
			}
		}

		# Difficulty Settings
		modifier = { # Easy
			add = -50
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -150
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}

		##########################
		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_halfway_threshold
			factor = faction_weight_factor_power_halfway_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_pushing_threshold
			factor = faction_weight_factor_power_pushing_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_threshold
			factor = faction_weight_factor_power_exceeds_threshold
		}
	}

	can_county_join = {
	}

	is_county_valid = {
		scope:faction.faction_target = holder
	}

	county_join_score =  {
		base = -5

		modifier = {
			add = 6
			county_opinion < 0
		}
		modifier = {
			add = 5
			county_opinion < -5
		}
		modifier = {
			add = 5
			county_opinion < -10
		}
		modifier = {
			add = 5
			county_opinion < -15
		}
		modifier = {
			add = 5
			county_opinion < -20
		}
		modifier = {
			add = 5
			county_opinion < -25
		}
		modifier = {
			add = 5
			county_opinion < -30
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:faction.faction_target
			SCORE_PER_TRAIT = 10
		}

		# Incapable to rule
		modifier = {
			add = 5
			exists = scope:faction.faction_target
			exists = scope:faction.special_character
			scope:faction.faction_target = { is_adult = no }
			scope:faction.special_character = { is_adult = yes }
		}

		# Dread
		modifier = {
			add = {
				add = scope:faction.faction_target.dread
				multiply = -0.24
			}
		}
	}

	can_character_become_leader = {
		is_landed = yes
	}

	can_character_create_ui = {
		is_character_interaction_valid = {
			recipient = scope:target
			interaction = create_claimant_faction_against_interaction
		}
	}

	county_allow_join = yes
	county_allow_create = no

	character_allow_create = yes

	character_allow_join = yes

	multiple_targeting = yes

	special_character_title = "FACTIONS_WINDOW_CLAIMANT"

	ignore_soft_block = yes
}
