replace_ceremonial_regent_faction = {
	casus_belli = replace_ceremonial_regent_faction_war

	name = FACTION_JAPANESE_REPLACE_REGENT_NAME_DYNAMIC

	desc = {
		first_valid = {
			desc = replace_ceremonial_regent_faction_desc
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					faction_leader = { tgp_is_ceremonial_liege_trigger = yes }
				}
				desc = replace_ceremonial_regent_faction_desc_emperor
			}
		}
	}

	short_effect_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					faction_leader ?= { tgp_is_ceremonial_liege_trigger = yes }
				}
				desc = replace_ceremonial_regent_faction_short_effect_desc_emperor
			}
			desc = replace_ceremonial_regent_faction_short_effect_desc_regent
		}
	}

	special_character_title = "FACTIONS_WINDOW_JAPANESE_REGENT"

	sort_order = 4

	show_special_title = yes

	multiple_targeting = yes

	discontent_progress = {
		base = 0
		common_discontent_progress_modifier = yes
	}

	power_threshold = {
		base = 80
		# Hard rule perk
		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
				NOT = { this = scope:faction.faction_leader }
			}
			trigger_event = tgp_faction_events.0105
		}
		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = tgp_faction_events.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 }
				}
			}
		}
	}

	on_war_start = {
		every_faction_member = {
			limit = { 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_shown = {
		tgp_realm_has_ceremonial_liege_trigger = yes
		has_dlc_feature = all_under_heaven
		NAND = {
			tgp_is_ceremonial_liege_trigger = yes
			tgp_is_ceremonial_regent_trigger = no
		}
	}

	is_valid = {
		trigger_if = {
			limit = { exists = faction_target }
			faction_target = { tgp_install_regent_faction_target_valid_trigger = yes }
		}
		trigger_if = {
			limit = { exists = special_character }
			special_character = { is_alive = yes }
		}
	}

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

	can_character_create_ui = {
		base_faction_trigger = { FACTION_TYPE = replace_ceremonial_regent_faction }
		common_can_character_create_trigger = { FACTION_TARGET = scope:target }
		tgp_install_regent_faction_can_create_trigger = yes
		NOR = {
			custom_tooltip = {
				text = not_house_head_allied_to_faction_target_trigger # this is correct, keys are swapped in loc
				house ?= scope:target.house
			}
			custom_tooltip = {
				text = not_house_member_of_faction_target_trigger # this is correct, keys are swapped in loc
				house.house_head ?= { is_allied_to = scope:target }
			}
		}
	}

	can_character_create = {
		common_can_character_create_trigger = { FACTION_TARGET = scope:target }
		tgp_install_regent_faction_can_create_trigger = yes
		trigger_if = {
			limit = {
				is_ai = yes
				is_male = yes
			}
			faith = {
				OR = {
					has_doctrine_parameter = male_dominated_law
					has_doctrine_parameter = gender_equal_law
				}
			}
		}
		trigger_else_if = {
			limit = {
				is_ai = yes
				is_female = yes
			}
			faith = {
				OR = {
					has_doctrine_parameter = female_dominated_law
					has_doctrine_parameter = gender_equal_law
				}
			}
		}
		trigger_else = {
			always = yes
		}
		NOR = {
			custom_tooltip = {
				text = not_house_head_allied_to_faction_target_trigger # this is correct, keys are swapped in loc
				house ?= scope:target.house
			}
			custom_tooltip = {
				text = not_house_member_of_faction_target_trigger # this is correct, keys are swapped in loc
				house.house_head ?= { is_allied_to = scope:target }
			}
		}
	}

	can_character_join = {
		common_can_character_join_trigger = { FACTION_TARGET = scope:faction.faction_target }
		NOR = {
			custom_tooltip = {
				text = not_house_head_allied_to_faction_target_trigger # this is correct, keys are swapped in loc
				house ?= scope:faction.faction_target.house
			}
			custom_tooltip = {
				text = not_house_member_of_faction_target_trigger # this is correct, keys are swapped in loc
				house.house_head ?= { is_allied_to = scope:faction.faction_target }
			}
		}
		japan_faction_cohesion_hard_trigger = yes
	}

	can_character_become_leader = {
		can_become_leader_of_faction_trigger = yes
		is_adult = 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_replace_regent_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
		}
		replace_ceremonial_regent_faction_modifiers = { FACTION_TARGET = scope:target }
		#House Hostility from Armenian tradition
		modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"
			NOT = { scope:target.house ?= root.house }
			culture = { has_cultural_parameter = house_hostility_more_common }
			factor = 1.25
		}
		modifier = {
			house.house_confederation.leading_house.house_head ?= root
			add = 50
		}
		# TODO_CD_JP_TGP AWAITING CODE SUPPORT
		#modifier = {
		#	confederation.leading_house.house_head ?= root
		#	tgp_confederation_military_power > scope:target.tgp_confederation_military_power_half
		#	add = 50
		#}

		# Extra check on leading house
	    modifier = {
	    	factor = 0
	    	NOT = { house.house_confederation.leading_house ?= house }
	    }
	}

	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
		}
		replace_ceremonial_regent_faction_modifiers = { FACTION_TARGET = scope:faction.faction_target }
		struggle_faction_modifiers = yes
		modifier = {
			is_allied_to = scope:faction.faction_leader
			NOT = { is_allied_to = scope:faction.faction_target }
			add = 25
		}
		modifier = {
			house ?= scope:faction.faction_leader.house
			add = 15
			desc = "FACTION_REASON_SAME_HOUSE_LEADER"
		}
		modifier = {
			dynasty ?= scope:faction.faction_leader.dynasty
			add = 10
			desc = "FACTION_REASON_SAME_DYNASTY_LEADER"
		}
		modifier = {
			house ?= scope:faction.faction_target.house
			add = -15
			desc = "FACTION_REASON_SAME_HOUSE_TARGET"
		}
		modifier = {
			dynasty ?= scope:faction.faction_target.dynasty
			add = -10
			desc = "FACTION_REASON_SAME_DYNASTY_TARGET"
		}
		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"
			NOT = { scope:faction.faction_target.house ?= 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
		set_special_title = scope:faction.faction_leader.top_liege.primary_title
		set_special_character = scope:faction.faction_leader
		tgp_bloc_members_join_faction_effect = yes
		if = {
			limit = {
				any_player = {
					is_landless_adventurer = yes
					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 = {
						has_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
}

restore_ceremonial_liege_faction = {
	casus_belli = restore_ceremonial_liege_faction_war

	name = FACTION_JAPANESE_RESTORE_EMPEROR_NAME_DYNAMIC

	desc = {
		desc = restore_ceremonial_liege_faction_desc
	}
	short_effect_desc = restore_ceremonial_liege_faction_short_effect_desc

	special_character_title = "FACTIONS_WINDOW_JAPANESE_RESTORE_EMPEROR"

	sort_order = 4

	show_special_title = yes

	multiple_targeting = no

	discontent_progress = {
		base = 0
		common_discontent_progress_modifier = yes
	}

	power_threshold = {
		base = 80
		# Hard rule perk
		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
				NOT = { this = scope:faction.faction_leader }
			}
			trigger_event = tgp_faction_events.0205
		}
		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = tgp_faction_events.0201
				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 }
				}
			}
		}
	}

	on_war_start = {
		every_faction_member = {
			limit = { 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_shown = {
		tgp_realm_has_ceremonial_liege_trigger = yes
		top_liege = { tgp_has_ceremonial_liege_title_trigger = no }
		has_dlc_feature = all_under_heaven
	}

	is_valid = {
		trigger_if = {
			limit = { exists = special_character }
			special_character = { is_alive = yes }
		}
	}

	is_character_valid = {
		common_character_validity_trigger = { FACTION_TARGET = scope:faction.faction_target }
		trigger_if = {
			limit = { 
				government_is_japanese_trigger = yes
				exists = scope:faction.faction_leader
			}
			scope:faction = {
				any_faction_member = {
					this = root.house.house_confederation.leading_house.house_head
				}
			}
			house.house_confederation ?= {
				OR = {
					leading_house.house_head ?= scope:faction.faction_leader
					leading_house.house_head ?= { tgp_is_ceremonial_liege_trigger = yes }
				}
				OR = {
					has_cohesion_level_parameter = bloc_leader_unlocks_restore_emperor_faction
					root.joined_faction ?= {
						faction_is_at_war = yes
					}
				}
			}
		}
	}

	can_character_become_leader = {
		can_become_leader_of_faction_trigger = yes
	}

	can_character_create_ui = {
		base_faction_trigger = { FACTION_TYPE = restore_ceremonial_liege_faction }
		common_can_character_create_trigger = { FACTION_TARGET = scope:target }
		restore_ceremonial_liege_faction_can_create_trigger = yes
	}

	can_character_create = {
		common_can_character_create_trigger = { FACTION_TARGET = scope:target }
		restore_ceremonial_liege_faction_can_create_trigger = yes
	}

	can_character_join = {
		common_can_character_join_trigger = { FACTION_TARGET = scope:faction.faction_target }
		NOR = {
			house ?= {
				this = scope:faction.faction_target.house
				house_head ?= { is_allied_to = scope:faction.faction_target }
			}
			is_allied_to = scope:faction.faction_target
		}
		scope:faction.faction_target ?= {
			is_independent_ruler = yes
			tgp_realm_has_ceremonial_liege_trigger = yes
			tgp_has_ceremonial_liege_title_trigger = no
		}
	}

	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_restore_ceremonial_liege_faction_war
		}
		#######################
		# Standard AI Weights #
		# '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
		}
		restore_ceremonial_liege_faction_modifiers = { FACTION_TARGET = scope:target }
		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"
			NOT = { scope:target.house ?= root.house }
			culture = { has_cultural_parameter = house_hostility_more_common }
			factor = 1.25
	    }

	    # Extra check on leading house
	    modifier = {
	    	factor = 0
	    	NOT = { house.house_confederation.leading_house ?= house }
	    }
	}

	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 #
		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
		}
		restore_ceremonial_liege_faction_modifiers = { FACTION_TARGET = scope:faction.faction_target }
		struggle_faction_modifiers = yes
		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"
			NOT = { scope:faction.faction_target.house ?= 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
		scope:faction ?= {
			random_faction_member = {
				limit = {
					house.house_confederation.leading_house.house_head ?= this
				}
				save_scope_as = faction_leader_scope
			}
		}
	
		if = { # Japanese blocs
			limit = { scope:faction_leader_scope.house.house_confederation.leading_house ?= scope:faction_leader_scope.house }
			scope:faction_leader_scope.house.house_confederation ?= {
				every_confederation_member = {
					limit = { can_join_faction = scope:faction }
					join_faction = scope:faction
				}
			}
		}
		
		set_special_title = scope:faction_leader_scope.top_liege.primary_title.var:administrative_ui_special_title
		set_special_character = scope:faction_leader_scope.top_liege.primary_title.var:administrative_ui_special_title.holder

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

	county_allow_join = no
	county_allow_create = no
}

ceremonial_claimant_faction = {
	casus_belli = ceremonial_claimant_faction_war

	short_effect_desc = claimant_faction_short_effect_desc

	sort_order = 1

	character_interaction = create_ceremonial_claimant_faction_against_interaction

	show_special_title = yes
	name = FACTION_CEREMONIAL_CLAIMANT_DYNAMIC_NAME

	claimant = yes

	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_shown = {
		tgp_realm_has_ceremonial_liege_trigger = yes
		top_liege = { tgp_is_ceremonial_regent_trigger = yes }
		has_dlc_feature = all_under_heaven
		NAND = {
			tgp_is_ceremonial_liege_trigger = yes
			tgp_is_ceremonial_regent_trigger = no
		}
	}

	# Is valid?
	# (see also invalidate_claimant_factions_on_death_effect for what happens when the claimant dies)
	is_valid = { # If the FACTION itself is valid
		trigger_if = {
			limit = {
				OR = {
					exists = special_character
					exists = faction_war
				}
			}
			OR = {
				special_title.holder ?= faction_target
				special_title.holder.top_liege ?= faction_target
			}
			faction_target = { tgp_realm_has_ceremonial_liege_trigger = yes }
			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
				this != scope:faction.faction_leader
			}
			trigger_event = faction_demand.2015
		}
		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = faction_demand.2011
				days = 5
			}
		}
	}

	ai_demand_chance = {
		base = 0

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

		# 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 = {
		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
		}
		tgp_realm_has_ceremonial_liege_trigger = yes
		has_valid_faction_members_trigger = yes
	}

	can_character_create_ui = {
		base_faction_trigger = { FACTION_TYPE = ceremonial_claimant_faction }
		
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		ceremonial_claimant_faction_can_create_trigger = yes
	}

	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
		}
		ceremonial_claimant_faction_can_create_trigger = yes
	}

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

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

		ceremonial_claimant_faction_blockers = {
			FACTION_TARGET = scope:target.top_liege.primary_title.var:administrative_ui_special_title.holder
			FACTION_CLAIMANT = scope:claimant
		}

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

		ceremonial_claimant_faction_modifiers = {
			FACTION_TARGET = scope:target.top_liege.primary_title.var:administrative_ui_special_title.holder
			FACTION_CLAIMANT = scope:claimant
			FACTION_TITLE = scope:title
		}

		# Extra check on leading house
	    modifier = {
	    	factor = 0
	    	NOT = { house.house_confederation.leading_house ?= house }
	    }
	}

	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
		}

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

		ceremonial_claimant_faction_modifiers = {
			FACTION_TARGET = scope:target.top_liege.primary_title.var:administrative_ui_special_title.holder
			FACTION_CLAIMANT = scope:claimant
			FACTION_TITLE = scope: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
}

imperial_policy_faction = {
	casus_belli = imperial_policy_faction_war

	desc = imperial_policy_faction_desc

	short_effect_desc = imperial_policy_faction_short_effect_desc

	sort_order = 4

	is_shown = {
		realm_law_use_imperial_policy_trigger = yes
		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 = liberty_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.0225
		}

		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = faction_demand.0221
				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_valid = {
		faction_target ?= {
			is_independent_ruler = yes
			realm_law_use_imperial_policy_trigger = 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 = imperial_policy_faction }
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		imperial_policy_faction_can_create_trigger = yes
	}

	can_character_create = {
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		imperial_policy_faction_can_create_trigger = yes
	}

	can_character_join = {
		common_can_character_join_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		
		trigger_if = {
			limit = {
				scope:faction.faction_target = { government_has_flag = government_is_administrative }
			}
			scope:faction.faction_target = {
				top_liege = this
			}
		}
		realm_law_use_imperial_policy_trigger = yes
		japan_faction_cohesion_trigger = yes
	}

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

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

	    # Extra check on leading house
	    modifier = {
	    	factor = 0
	    	NOT = { house.house_confederation.leading_house ?= house }
	    }
	}

	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
		}

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