independence_faction = {
	casus_belli = independence_faction_war
	
	short_effect_desc = independence_faction_short_effect_desc

	sort_order = 2

	is_shown = {
		NAND = {
			tgp_is_ceremonial_liege_trigger = yes
			tgp_is_ceremonial_regent_trigger = no
		}
	}

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

		dynamic_power_threshold_scripted_modifier = {
			FACTION_TYPE1 = claimant_faction
			FACTION_TYPE2 = liberty_faction
			FACTION_TYPE3 = populist_faction
		}
	}	
	
	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
				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_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!
			}
		}

		modifier = {
			add = -1000
			faction_target = {
				involved_activity ?= {
					has_activity_type = activity_coronation
					activity_host = prev
				}
			}
		}
	}

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

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

		independence_faction_create_blockers  = {
			FACTION_TARGET = scope:target
		}

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

		common_faction_modifiers = {
			FACTION_TARGET = scope:target
			OPINION_MULTIPLIER = -0.4
			MAX_OPINION = 100
			POWER = 0
			THRESHOLD = 80
		}

		independence_faction_modifiers = {
			FACTION_TARGET = scope:target
		}

		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"

			scope:target.house ?= {
				this != root.house
			}
			culture = { has_cultural_parameter = house_hostility_more_common }

			factor = 1.25
	    }

	    # EP3 - Grand Ambitions Story Cycle
	    modifier = {
	    	desc = "EP3_GRAND_AMBITIONS_STORY"
	    	any_owned_story = {
	    		type = grand_ambitions_story_cycle
	    		var:target_char ?= scope:target
	    	}
	    	factor = 1.5
	    }
		# TGP Dynastic Cycle in Instability phase
		modifier = {
			desc = "FACTION_REASON_DYNASTIC_CYCLE_INSTABILITY"
			add = 25
			scope:target = {
				top_participant_group:dynastic_cycle ?= {
					has_participant_group_parameter = dynastic_cycle_external_tributaries_vassals_increased_independence_desire
				}
			}
		}

		modifier = {
			scope:target = { has_character_modifier = disbelieved_mandala_modifier }
			add = 15
			desc = is_disbelieved_mandala
		}
	}

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

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

		common_join_faction_blockers = {
			FACTION_TARGET = scope:faction.faction_target
		}

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

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

		independence_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
		}

		# Small multiplier for my neighbors already in the Faction
		modifier = {
			desc = "FACTION_REASON_NEIGHBORS"

			scope:faction = {
				any_faction_member = { #suppress the description printout if factor is 1
					any_neighboring_realm_same_rank_owner = {
						this = root
					}
				}
			}

			factor = {
				value = 1
				scope:faction = {
			    	every_faction_member = {
			    		if = {
			    			limit = {
				    			any_neighboring_realm_same_rank_owner = {
				    				this = root
				    			}
							}
			    			add = 0.25
			    		}		
			    	}
		    	}
	    	}
	    }

	    modifier = {
			desc = "FACTION_REASON_POWERFUL_VASSAL"
	    	factor = 2
			is_powerful_vassal_of = scope:faction.faction_target
	    }

	    struggle_faction_modifiers = yes

	    #House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"

			scope:faction.faction_target.house ?= {
				this != root.house
			}
			culture = { has_cultural_parameter = house_hostility_more_common }

			factor = 1.25
	    }
		#EP3 admin legitimacy event
		modifier = {
			desc = "FACTION_REASON_LOW_LEGITIMACY_CHALLENGER"
			add = 25
			scope:faction = {
				any_faction_member = {
					has_character_flag = low_legitimacy_admin_factions_flag
				}
			}
		}
		# TGP Dynastic Cycle in Instability phase
		modifier = {
			desc = "FACTION_REASON_DYNASTIC_CYCLE_INSTABILITY"
			add = 25
			scope:faction.faction_target = {
				top_participant_group:dynastic_cycle ?= {
					has_participant_group_parameter = dynastic_cycle_external_tributaries_vassals_increased_independence_desire
				}
			}
		}
	}

	is_character_valid = {
		common_character_validity_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		has_valid_faction_members_trigger = yes
	}

	can_character_create_ui = {
		base_faction_trigger = { FACTION_TYPE = independence_faction }

		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		# Bloc Cohesion requirement
		japan_faction_cohesion_trigger = yes

		liege = {
			top_liege = this
		}
		
		trigger_if = {
			limit = {
				faith.religion = religion:islam_religion
				liege = { has_title = title:d_sunni }
				title:d_sunni = {
					has_variable = dar_al_islam
				}
			}
			liege = {
				NOT = {
					has_title = title:d_sunni
				}
			}
		}
		
		#PROPPA CONFEDERATION VASSALS
		trigger_if = {
			limit = {
				liege = {
					is_confederation_member = yes
				}
			}
			#Confederations block most faction types
			custom_tooltip = {
				text = confederaton_blocks_faction
				NOR = {
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_tribal
				}
			}
		}
		# The emperor does not want to leave his empire
		tgp_is_ceremonial_liege_trigger = no
		# Ministers cannot join this faction
		custom_description = {
			text = target_is_a_minister_desc
			subject = this
			tgp_is_any_minister = no
		}
	}

	can_character_create = {
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		# Bloc Cohesion requirement
		japan_faction_cohesion_trigger = yes
		
		liege = {
			top_liege = this
		}

		custom_description = {
			text = character_blocked_from_joining # will never be displayed but without it, errors are triggered
			OR = {
		        is_ai = no
		        culture != scope:target.culture
		        faith != scope:target.faith
		        NOT = {
		            scope:target = {
		                is_rightful_liege_of = root
		            }
		        }
		        any_sub_realm_county = {
		            percent <= 0.5
		            save_temporary_scope_as = current_county
		            scope:target.primary_title = {
		                any_in_de_jure_hierarchy = { this = scope:current_county }
		            }
		        }
				government_has_flag = government_is_nomadic
	    	}
		}
		
		trigger_if = {
			limit = {
				faith.religion = religion:islam_religion
				liege = { has_title = title:d_sunni }
				title:d_sunni = {
					has_variable = dar_al_islam
				}
			}
			liege = {
				NOT = {
					has_title = title:d_sunni
				}
			}
		}
		
		#PROPPA CONFEDERATION VASSALS
		trigger_if = {
			limit = {
				liege = {
					is_confederation_member = yes
				}
			}
			#Confederations block most faction types
			custom_tooltip = {
				text = confederaton_blocks_faction
				NOR = {
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_tribal
				}
			}
		}
		# The emperor does not want to leave his empire
		tgp_is_ceremonial_liege_trigger = no
		# Ministers cannot join this faction
		custom_description = {
			text = target_is_a_minister_desc
			subject = this
			tgp_is_any_minister = no
		}
	}

	can_character_join = {
		common_can_character_join_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		
		trigger_if = {
			limit = {
				faith.religion = religion:islam_religion
				liege = { has_title = title:d_sunni }
				title:d_sunni = {
					has_variable = dar_al_islam
				}
			}
			liege = {
				NOT = {
					has_title = title:d_sunni
				}
			}
		}
		
		#PROPPA CONFEDERATION VASSALS
		trigger_if = {
			limit = {
				liege = {
					is_confederation_member = yes
				}
			}
			#Confederations block most faction types
			custom_tooltip = {
				text = confederaton_blocks_faction
				NOR = {
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_tribal
				}
			}
		}
		# The emperor does not want to leave his empire
		tgp_is_ceremonial_liege_trigger = no
		# Ministers cannot join this faction
		custom_description = {
			text = target_is_a_minister_desc
			subject = this
			tgp_is_any_minister = no
		}
	}

	can_character_become_leader = {
		can_become_leader_of_faction_trigger = yes
	}

	on_creation = {
		save_scope_as = faction
		tgp_bloc_members_join_faction_effect = yes
		if = {
			limit = {
				any_player = {
					has_government = landless_adventurer_government
					is_within_diplo_range = { CHARACTER = scope:faction.faction_target }
					OR = {
						has_contact = scope:faction.faction_leader
						NOT = { has_contact = scope:faction.faction_target }
					}
					any_character_task_contract = {
						task_contract_type = laamp_join_faction_contract
						count <= 3
					}
					can_create_task_contract = {
						type_name = laamp_join_faction_contract
						employer = scope:faction.faction_leader
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_type = laamp_join_faction_contract
					task_contract_tier = scope:faction.faction_leader.task_contract_tier_value
					location = scope:faction.faction_leader.primary_title.title_province
					task_contract_employer = scope:faction.faction_leader
					target = scope:faction.faction_target
				}
			}
		}
	}

	county_allow_join = no
	county_allow_create = no
}

liberty_faction = {
	casus_belli = liberty_faction_war

	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					faction_target = { government_has_flag = government_is_administrative }
				}
				desc = liberty_faction_desc_admin
			}
			triggered_desc = {
				trigger = {
					faction_target = { government_has_flag = government_is_tribal }
				}
				desc = liberty_faction_desc_tribal
			}
			triggered_desc = {
				trigger = {
					faction_target = { government_has_flag = government_is_feudal }
				}
				desc = liberty_faction_desc_feudal
			}
			desc = liberty_faction_desc
		}
	}

	short_effect_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					faction_target = { government_has_flag = government_is_administrative }
				}
				desc = liberty_faction_short_effect_desc_admin
			}
			triggered_desc = {
				trigger = {
					faction_target = { government_has_flag = government_is_tribal }
				}
				desc = liberty_faction_short_effect_desc_tribal
			}
			desc = liberty_faction_short_effect_desc
		}
	}

	sort_order = 4

	is_shown = {
		NAND = {
			tgp_is_ceremonial_liege_trigger = yes
			tgp_is_ceremonial_regent_trigger = no
		}
	}

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

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

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

		modifier = {
			add = -1000
			faction_target = {
				involved_activity ?= {
					has_activity_type = activity_coronation
					activity_host = prev
				}
			}
		}
	}

	on_war_start = {
		every_faction_member = {
			limit = {
				has_variable = gathered_support_for_faction
				var:gathered_support_for_faction = root
			}
			spawn_army = {
				name = gathered_support_for_faction_army
				levies = {
					value = this.massive_influence_value
					multiply = 3
				}
				men_at_arms = {
					type = light_footmen
					stacks = 2
				}
				location = this.location
				war = root.faction_war
				inheritable = yes
			}
		}
	}

	is_character_valid = {
		common_character_validity_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		has_valid_faction_members_trigger = yes
	}

	can_character_create_ui = {
		base_faction_trigger = { FACTION_TYPE = liberty_faction }

		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		# Bloc Cohesion requirement
		japan_faction_cohesion_trigger = yes

		trigger_if = {
			limit = { scope:target = { realm_law_use_imperial_bureaucracy = yes } }
			custom_description = {
				text = has_lowest_imperial_bureaucracy
				subject = scope:target
				scope:target = {
					OR = {
						has_realm_law = imperial_bureaucracy_1
						has_realm_law = imperial_bureaucracy_2
						has_realm_law = imperial_bureaucracy_3
					}
				}
			}
		}
		trigger_else_if = {
			limit = { scope:target = { government_has_flag = government_is_tribal } }
			custom_description = {
				text = has_lowest_tribal_authority
				subject = scope:target
				scope:target = {
					OR = {
						has_realm_law = tribal_authority_1
						has_realm_law = tribal_authority_2
						has_realm_law = tribal_authority_3
					}
				}
			}
		}
		trigger_else_if = {
			limit = { scope:target = { realm_law_use_celestial_bureaucracy = yes } }
			custom_description = {
				text = has_lowest_celestial_bureaucracy
				subject = scope:target
				scope:target = {
					OR = {
						has_realm_law = celestial_bureaucracy_1
						has_realm_law = celestial_bureaucracy_2
						has_realm_law = celestial_bureaucracy_3
					}
				}
			}
		}
		trigger_else_if = {
			limit = { scope:target = { realm_law_use_meritocratic_bureaucracy = yes } }
			custom_description = {
				text = has_lowest_meritocratic_bureaucracy
				subject = scope:target
				scope:target = {
					OR = {
						has_realm_law = meritocratic_bureaucracy_1
						has_realm_law = meritocratic_bureaucracy_2
						has_realm_law = meritocratic_bureaucracy_3
					}
				}
			}
		}
		trigger_else_if = {
			limit = { scope:target = { realm_law_use_japanese_bureaucracy = yes } }
			custom_description = {
				text = has_lowest_japanese_bureaucracy
				subject = scope:target
				scope:target = {
					OR = {
						has_realm_law = japanese_bureaucracy_1
						has_realm_law = japanese_bureaucracy_2
						has_realm_law = japanese_bureaucracy_3
					}
				}
			}
		}
		trigger_else = {
			custom_description = {
				text = has_lowest_crown_authority
				subject = scope:target
				scope:target = {
					OR = {
						has_realm_law = crown_authority_1
						has_realm_law = crown_authority_2
						has_realm_law = crown_authority_3
					}
				}
			}
		}
		
		trigger_if = {
			limit = {
				scope:target = { government_has_flag = government_is_administrative }
			}
			scope:target = {
				top_liege = this
			}
		}
	}

	can_character_create = {
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		# Bloc Cohesion requirement
		japan_faction_cohesion_trigger = yes

		has_valid_realm_laws_for_liberty_faction_trigger = {
			TARGET = scope:target
		}
		
		trigger_if = {
			limit = {
				scope:target = { government_allows = administrative }
			}
			scope:target = {
				top_liege = this
			}
		}
	}

	can_character_join = {
		common_can_character_join_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		OR = {
			is_ai = no
			has_valid_realm_laws_for_liberty_faction_trigger = {
				TARGET = scope:faction.faction_target
			}
		}
		
		trigger_if = {
			limit = {
				scope:faction.faction_target = { government_allows = administrative }
			}
			scope:faction.faction_target = {
				top_liege = this
			}
		}
	}

	can_character_become_leader = {
		can_become_leader_of_faction_trigger = yes
	}

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

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

		#######################
		# 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.
		# 'Base Join Weight' depends on the current Crown Authority level.
		common_faction_modifiers = {
			FACTION_TARGET = scope:target
			OPINION_MULTIPLIER = -1
			MAX_OPINION = 0
			POWER = 0
			THRESHOLD = 80
		}

		liberty_faction_modifiers = {
			FACTION_TARGET = scope:target
		}
		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"

			scope:target.house ?= {
				this != root.house
			}
			culture = { has_cultural_parameter = house_hostility_more_common }

			factor = 1.25
	    }		
	}

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

		############
		# BLOCKERS #
		common_join_faction_blockers = {
			FACTION_TARGET = scope:faction.faction_target
		}
		
		#######################
		# Standard AI Weights #
		#AI
		# 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.
		common_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
			OPINION_MULTIPLIER = -1
			MAX_OPINION = 0
			POWER = scope:faction.faction_power
			THRESHOLD = scope:faction.faction_power_threshold
		}

		liberty_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
		}

		struggle_faction_modifiers = yes

		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"

			scope:faction.faction_target.house ?= {
				this != root.house
			}
			culture = { has_cultural_parameter = house_hostility_more_common }

			factor = 1.25
	    }
		#EP3 admin legitimacy event
		modifier = {
			desc = "FACTION_REASON_LOW_LEGITIMACY_CHALLENGER"
			add = 25
			scope:faction = {
				any_faction_member = {
					has_character_flag = low_legitimacy_admin_factions_flag
				}
			}
		}
	}

	on_creation = {
		save_scope_as = faction
		tgp_bloc_members_join_faction_effect = yes
		if = {
			limit = {
				any_player = {
					has_government = landless_adventurer_government
					is_within_diplo_range = { CHARACTER = scope:faction.faction_target }
					OR = {
						has_contact = scope:faction.faction_leader
						NOT = { has_contact = scope:faction.faction_target }
					}
					any_character_task_contract = {
						task_contract_type = laamp_join_faction_contract
						count <= 3
					}
					can_create_task_contract = {
						type_name = laamp_join_faction_contract
						employer = scope:faction.faction_leader
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_type = laamp_join_faction_contract
					task_contract_tier = scope:faction.faction_leader.task_contract_tier_value
					location = scope:faction.faction_leader.primary_title.title_province
					task_contract_employer = scope:faction.faction_leader
					target = scope:faction.faction_target
				}
			}
		}
	}

	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

	character_interaction = create_claimant_faction_against_interaction

	claimant = yes

	is_shown = {
		NAND = {
			tgp_is_ceremonial_liege_trigger = yes
			tgp_is_ceremonial_regent_trigger = no
		}
	}

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

		#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
		
		trigger_if = {
			limit = {
				OR = {
					exists = special_character
					exists = faction_war
				}
			}
			special_title.holder ?= faction_target
			special_title.holder ?= { tgp_is_ceremonial_regent_trigger = no }
			OR = {
				special_character ?= {
					is_alive = yes
					#has_claim_on = root.special_title
					NOR = { 
						has_trait = incapable
						this = root.faction_target
					}
					# Under Byzantine law, a ruler must be unblemished
					trigger_if = {
						limit = {
							root.special_title.holder.culture ?= { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
						}
						custom_description = {
							text = "byzantine_claimant_is_disfigured"
							object = this
							valid_for_byz_succession_trigger = yes
						}
					}
					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
						}
					}
					# Ministers cannot be claimants unless we are targeting h_china
					trigger_if = {
						limit = {
							OR = {
								has_title = title:e_minister_chancellor
								has_title = title:e_minister_censor
								has_title = title:e_minister_grand_marshal
								has_title = title:e_minister_of_personnel
								has_title = title:e_minister_of_revenue
								has_title = title:e_minister_of_rites
								has_title = title:e_minister_of_war
								has_title = title:e_minister_of_justice
								has_title = title:e_minister_of_works
							}
						}
						custom_description = {
							text = target_is_a_minister_desc
							subject = this
							root.special_title = { this = title:h_china }
						}
					}
				}
				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
				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
					}
				}
			}
		}

		modifier = {
			add = -1000
			faction_target = {
				involved_activity ?= {
					has_activity_type = activity_coronation
					activity_host = prev
				}
			}
		}
	}

	on_war_start = {
		every_faction_member = {
			limit = {
				has_variable = gathered_support_for_faction
				var:gathered_support_for_faction = root
			}
			spawn_army = {
				name = gathered_support_for_faction_army
				levies = {
					value = this.massive_influence_value
					multiply = 3
				}
				men_at_arms = {
					type = light_footmen
					stacks = 2
				}
				location = this.location
				war = root.faction_war
				inheritable = yes
			}
		}
	}

	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
		}
		
		trigger_if = {
			limit = {
				government_allows = administrative
			}
			scope:faction.faction_target ?= {
				OR = {
					top_liege ?= this
					NOT = { government_allows = administrative }
				}
			}
		}
		
		# TGP - Use Coup against Kampaku instead
		trigger_if = {
			limit = {
				scope:faction.faction_target ?= top_liege
			}
			scope:faction.faction_target ?= {
				tgp_is_ceremonial_regent_trigger = no
			}
		}
		has_valid_faction_members_trigger = yes
	}

	can_character_create_ui = {
		is_character_interaction_valid = {
			recipient = scope:target
			interaction = create_claimant_faction_against_interaction
		}
		# Bloc Cohesion requirement
		japan_faction_cohesion_trigger = yes
		# TGP - Use Coup against Kampaku instead
		custom_tooltip = {
			text = cannot_create_claimant_faction_against_kampaku_tooltip
			scope:target = {
				# TGP - Use Coup against Kampaku instead
				tgp_is_ceremonial_regent_trigger = no
			}
		}
	}

	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
		}
		# Bloc Cohesion requirement
		japan_faction_cohesion_trigger = yes

		# TGP - Use Coup against Kampaku instead
		trigger_if = {
			limit = {
				exists = joined_faction
				joined_faction.special_title.holder ?= {
					this = scope:target
				}
			}
			joined_faction.special_title.holder ?= {
				tgp_is_ceremonial_regent_trigger = no
			}
		}
		# TGP - Use Coup against Kampaku instead
		trigger_else_if = {
			limit = {
				has_variable = claiming_title
				var:claiming_title.holder ?= {
					this = scope:target
				}
			}
			var:claiming_title.holder ?= {
				tgp_is_ceremonial_regent_trigger = no
			}
		}
		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 = {
		can_become_leader_of_faction_trigger = 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
		}

		admin_faction_modifiers = {
			FACTION_TARGET = scope:target
			FACTION_TYPE = claimant_faction
		}

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

	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
		}

		admin_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
			FACTION_TYPE = claimant_faction
		}		

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

		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		join_faction_stacking_modifiers = yes
		
		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
		
		#EP3 admin legitimacy event
		modifier = {
			desc = "FACTION_REASON_LOW_LEGITIMACY_CHALLENGER"
			add = 25
			scope:faction = {
				any_faction_member = {
					has_character_flag = low_legitimacy_admin_factions_flag
				}
			}
		}

		#EP3 King of England disgruntled vassals/life threatened modifiers
		modifier = {
			desc = "FACTION_REASON_CLAIMANT_DISGRUNTLED_VASSALS"
			add = 25
			liege = {
				has_character_modifier = ep3_disgruntled_vassals_modifier
			}
		}
		modifier = {
			desc = "FACTION_REASON_CLAIMANT_DISGRUNTLED_VASSALS"
			add = 25
			liege = {
				has_character_modifier = ep3_life_threatened_modifier
			}
		}
	}

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

	on_creation = {
		save_scope_as = faction
		tgp_bloc_members_join_faction_effect = yes
		if = {
			limit = {
				any_player = {
					has_government = landless_adventurer_government
					is_within_diplo_range = { CHARACTER = scope:faction.faction_target }
					OR = {
						has_contact = scope:faction.faction_leader
						has_contact = scope:faction.special_character
						NOT = { has_contact = scope:faction.faction_target }
					}
					any_character_task_contract = {
						task_contract_type = laamp_join_faction_contract
						count <= 3
					}
					can_create_task_contract = {
						type_name = laamp_join_faction_contract
						employer = scope:faction.faction_leader
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_type = laamp_join_faction_contract
					task_contract_tier = scope:faction.faction_leader.task_contract_tier_value
					location = scope:faction.faction_leader.primary_title.title_province
					task_contract_employer = scope:faction.faction_leader
					target = scope:faction.faction_target
				}
			}
		}
	}

	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
}
