﻿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

		common_discontent_progress_modifier = yes
	}

	power_threshold = {
		base = 80

		modifier = {
			add = 20 
			faction_target = {
				has_perk = hard_rule_perk
			}
			desc = "FACTION_POWER_HARD_RULE"
		}
        
        ### VtA_begin
        modifier = {
			add = vta_claimant_faction_threshold
			desc = "VTA_FACTION_POWER_ADD"
		}
        ### VtA_end

		#Lower the threshold depending on the state of other factions
		dynamic_power_threshold_scripted_modifier = {
			FACTION_TYPE1 = liberty_faction
			FACTION_TYPE2 = independence_faction
			FACTION_TYPE3 = populist_faction
		}
	}

	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
				NOR = { 
					has_trait = incapable
					this = root.faction_target
				}
				trigger_if = { #If they're imprisoned by the faction target they can only be so for five years before invalidating
					limit = {
						is_imprisoned_by = root.faction_target
					}
					time_in_prison_type = {
						years < 5
					}
				}
			}
			exists = faction_war
		}
	}

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

	ai_demand_chance = {
		base = 0

		#Won't send ultimatum if the claimant is imprisoned by the faction-target!
		modifier = {
			add = -500
			faction_target = {
				any_prisoner = {
					this = root.special_character
				}
			}
		}

		# Won't start a war is the Realm is fighting against a Dissolution faction or Independent factions: you want to get the best realm possible. Better wait a bit longer
		modifier = {
			add = -1000
			faction_target = {
				any_character_war = {
					OR = {
						using_cb = independence_faction_war
						using_cb = independence_war
						using_cb = nation_fracturing_faction_war
					}
				}
			}
		}

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

	on_war_start = {}

	is_character_valid = { # Is the character valid to stay in the faction? This is also checked when creating it...
		common_character_validity_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
	}

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

	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.

		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}

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

	can_character_join = { # Can a character join an existing faction?
		common_can_character_join_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
	}

	can_character_become_leader = {
		is_landed = yes
	}

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

		####
		# BLOCKERS
		####
		common_create_faction_blockers = {
			FACTION_TARGET = scope:target
			FLAG = none
		}

		claimant_faction_blockers = {
			FACTION_TARGET = scope:target
			FACTION_CLAIMANT = scope:claimant
		}

		####
		# AI modifiers
		####
		common_faction_modifiers = {
			FACTION_TARGET = scope:target
			OPINION_MULTIPLIER = -1.5
			MAX_OPINION = 150
			POWER = 0
			THRESHOLD = 80
		}

		claimant_faction_modifiers = {
			FACTION_TARGET = scope:target
			FACTION_CLAIMANT = scope:claimant
			FACTION_TITLE = scope:title
		}

		# If another Claimant Faction already exists, prefer to side with it
		modifier = {
			add = -100
			scope:target = {
				any_targeting_faction = {
			  		faction_is_type = claimant_faction
				  	NOT = {
			  			any_faction_member = {
			  				is_ai = no
			  			}
			  		}
					special_character = {
						OR = {
							OR = {
								has_relation_soulmate = root
								has_relation_best_friend = root
							}
							AND = {
								root = {
									save_temporary_opinion_value_as = { name = opinion_of_current_liege target = scope:target }
									opinion = { target = prev value > scope:opinion_of_current_liege }
								}
								OR = {
									AND = {
										root.faith = {
											has_doctrine = doctrine_pluralism_pluralistic
										}
										faith = {
											has_doctrine = doctrine_pluralism_pluralistic
										}
									}
									faith = root.faith
								}
								culture = {
									OR = {
										this = root.culture
										cultural_acceptance = { target = root.culture value >= 90 } # Essentially the same culture
									}
								}
							}
						}
					}
		  		}
		  	}
		}

	}

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

		common_join_faction_blockers = {
			FACTION_TARGET = scope:faction.faction_target
		}

		claimant_faction_blockers = {
			FACTION_TARGET = scope:faction.faction_target 
			FACTION_CLAIMANT = scope:faction.special_character 
		}

		common_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
			OPINION_MULTIPLIER = -1
			MAX_OPINION = 100
			POWER = scope:faction.faction_power
			THRESHOLD = scope:faction.faction_power_threshold
		}

		claimant_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target 
			FACTION_CLAIMANT = scope:faction.special_character 
			FACTION_TITLE = scope:faction.special_title
		}		

		##########################
		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		modifier = {
			desc = "FACTION_REASON_POWER_HALFWAY_THRESHOLD"
			scope:faction.faction_power >= scope:faction.faction_power_halfway_threshold
			factor = faction_weight_factor_power_halfway_threshold
		}
		modifier = {
			desc = "FACTION_REASON_POWER_PUSHING_THRESHOLD"
			scope:faction.faction_power >= scope:faction.faction_power_pushing_threshold
			factor = faction_weight_factor_power_pushing_threshold
		}
		modifier = {
			desc = "FACTION_REASON_POWER_THRESHOLD"
			scope:faction.faction_power >= scope:faction.faction_power_threshold
			factor = faction_weight_factor_power_exceeds_threshold
		}
		modifier = { # CE1 Divine Mandate perk
			desc = "FACTION_REASON_LEGITIMACY_LEGACY_4"
			liege.dynasty = { has_dynasty_perk = ce1_legitimacy_legacy_4 }
			add = -10
		}
		
		struggle_faction_modifiers = yes
	}

	is_county_valid = {
		scope:faction.faction_target = holder
	}

	can_county_join = {
		# Counties can only join if the De Jure belong to the title being claimed.
		exists = scope:faction.special_title
		target_is_de_jure_liege_or_above = scope:faction.special_title
	}

	county_join_score =  {
		base = -80 # Needs to be less than the weight set in 00_populist_faction.txt, or counties will never join a claimant faction over a populist faction.

		# Up to +3 join score per negative opinion (+80 at -27 opinion) if different culture. 
		compare_modifier = {
			desc = "FACTION_REASON_CLAIMANT_COUNTY_SHARED_CULTURE"
			trigger = {
				NOT = { scope:faction.faction_target.culture = this.title_province.culture }
				scope:faction.special_character.culture = this.title_province.culture 
			}
			value = county_opinion
			multiplier = -2.0
		}
		compare_modifier = {
			desc = "FACTION_REASON_CLAIMANT_COUNTY_SHARED_HERITAGE"
			trigger = {
				NOT = { this.title_province.culture = { has_same_culture_heritage = scope:faction.faction_target.culture } }
				this.title_province.culture = { has_same_culture_heritage = scope:faction.special_character.culture }
			}
			value = county_opinion
			multiplier = -2.0
		}

		# If we have better relations with the claimant's faith than our current liege's, gain up to +5 join score per negative opinion (at Righteous) down to +1 join score per negative opinion (at Hostile).
		compare_modifier = {
			desc = "FACTION_REASON_LIKE_CLAIMANT_FAITH"
			trigger = {
				faith = {
					faith_hostility_level_comparison = {
						scope:faction.faction_target.faith > scope:faction.special_character.faith
					}
				}
			}
			value = county_opinion
			multiplier = {
				value = 0.0
				if = {
					limit = {
						faith = {
							faith_hostility_level = {
								target = scope:faction.special_character.faith
								value = faith_hostile_level
							}
						}
					}
					value = -1.0 # Hostile
				}
				else_if = {
					limit = {
						faith = {
							faith_hostility_level = {
								target = scope:faction.special_character.faith
								value = faith_astray_level
							}
						}
					}
					value = -3.0 # Astray
				}
				else = {
					value = -5.0 # Righteous
				}
			}
		}

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

		# Greatly reduced chance for a character's capital county to form a faction against them (extra -25 opinion needed).
		modifier = {
			desc = "FACTION_REASON_CAPITAL_COUNTY"
			add = -200
			this.title_province = scope:faction.faction_target.capital_province
		}
		
		# More likely to support adult claimants when a child is on the throne.
		modifier = {
			desc = "FACTION_REASON_ADULT_CLAIMANT_CHILD_RULER"
			add = 10
			exists = scope:faction.faction_target
			exists = scope:faction.special_character
			scope:faction.faction_target = { is_adult = no }
			scope:faction.special_character = { is_adult = yes }
		}

		# Peasants perfer magnanimous rulers over harsh or cruel ones.
		compare_modifier = {
			desc = "FACTION_REASON_CLAIMANT_KINDNESS"
			factor = {
				# Gets a value between 0 (max greedy/dishonorable/vengeful/cruel) and 800 (max honorable/compassionate/generous/forgiving).
				value = 400
				add = scope:faction.special_character.ai_honor
				add = scope:faction.special_character.ai_compassion
				subtract = scope:faction.special_character.ai_greed
				subtract = scope:faction.special_character.ai_vengefulness

				# Converts the personality score into a percentage between 0% (will never support) and 100% (really wants to support)
				divide = 800 
			}

			# How big the factor is for supporting this claimant over other factions at max support. 
			multiplier = 2
		}
	}

	county_allow_join = yes
	county_allow_create = no
	county_power = county_levies_to_raise

	character_allow_create = yes

	character_allow_join = yes

	multiple_targeting = yes

	special_character_title = "FACTIONS_WINDOW_CLAIMANT"

	ignore_soft_block = yes
}
