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
				}
			}
		}
		
		trigger_if = {
			limit = {
				government_allows = administrative
				has_game_rule = admin_independence_rule_off 
			}
			custom_tooltip = {
				text = admin_high_tier_vassal_create_faction
				highest_held_title_tier >= tier_kingdom
			}
		}
		#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
	}

	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
				}
			}
		}
		
		trigger_if = {
			limit = {
				government_allows = administrative
				has_game_rule = admin_independence_rule_off 
			}
			custom_tooltip = {
				text = admin_high_tier_vassal_create_faction
				highest_held_title_tier >= tier_kingdom
			}
		}
		#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
	}

	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
				}
			}
		}
		
		trigger_if = {
			limit = {
				government_allows = administrative
				has_game_rule = admin_independence_rule_off 
			}
			custom_tooltip = {
				text = admin_high_tier_vassal_join_faction
				highest_held_title_tier >= tier_kingdom
			}
		}
		#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
	}

	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
}