populist_faction = {
	casus_belli = populist_war

	leaders_allowed_to_leave = no
	player_can_join = no

	sort_order = 3

	name = FACTION_POPULIST_DYNAMIC_NAME

	short_effect_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					faction_target = { government_has_flag = government_is_administrative }
				}
				desc = populist_faction_short_effect_desc_admin
			}
			desc = populist_faction_short_effect_desc
		}
	}

	discontent_progress = {
		base = 0

		common_discontent_progress_modifier = yes
		modifier = {
			faction_target = { can_vassals_be_attacked = yes }
			add = 2
			desc = border_war_increased_peasant_discontent
		}
		modifier = {
			faction_target = {
				top_participant_group:dynastic_cycle ?= {
					has_participant_group_parameter = dynastic_cycle_more_peasant_factions
				}
			}
			add = 2
			desc = instability_increased_peasant_discontent
		}
	}

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

	requires_county = yes
	requires_character = no

	# Faction existence is valid
	is_valid = {
		trigger_if = {
			limit = {
				exists = special_character
			}
			special_character = {
				is_alive = yes
			}
		}
	}

	###########
	# Counties/characters are allowed to exist in the faction at all (will be kicked out if they fail these checks while already in it).
	is_character_valid = {
		has_valid_faction_members_trigger = yes
		# Only AIs are allowed to be in popular factions.
		is_ai = yes

		# Landed rulers can only be in the faction...
		trigger_if = {
			limit = {
				is_playable_character = yes
				exists = liege
				this != liege
			}

			# ... If they are a direct vassal of the faction target.
			liege = scope:faction.faction_target

			# ... If they are at least a count. (Barons are not allowed.)
			highest_held_title_tier >= tier_county
		}

		# Must not be the same culture & faith as the faction target.
		custom_tooltip = {
			text = populist_faction_same_culture_faith_as_liege
			OR = {
				faith != scope:faction.faction_target.faith
				culture != scope:faction.faction_target.culture
			}
		}

		trigger_if = { # Must have a different faith than state faith in Admin
			limit = {
				exists = scope:faction.faction_target.primary_title.state_faith
			}
			faith != scope:faction.faction_target.primary_title.state_faith
		}

		# Not forbidden from being in factions.
		NOT = {
			has_character_flag = joining_faction_block
		}
		is_imprisoned = no
	}

	is_county_valid = {
		# Counties can only be in a faction against the top liege in their realm (who is inherently independent).
		scope:faction.faction_target = holder.top_liege

		# Counties must have a different faith or culture than the faction target.
		OR = {
			faith != scope:faction.faction_target.faith
			culture != scope:faction.faction_target.culture
		}

		trigger_if = { # Must have a different faith than state faith in Admin
			limit = {
				exists = scope:faction.faction_target.primary_title.state_faith
			}
			faith != scope:faction.faction_target.primary_title.state_faith
		}

		# Counties must have the same faith as the faction
		scope:faction = {
			OR = {
				NOT = {
					has_variable = faction_faith
				}
				AND = {
					has_variable = faction_faith
					root.faith = var:faction_faith
				}
			}
		}

		# Player capitals are blocked from joining populist factions (to avoid Game Overing a player due to a populist uprising against their liege, which as a vassal they can't really prevent or fight)
		save_temporary_scope_as = this_county
		trigger_if = {
			limit = {
				holder = {
					is_ai = no
				}
			}
			this != holder.capital_county
		}
		title_province = {
			NOR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}
	}

	###########
	# Counties/characters are allowed to create/join the faction (but will remain in faction if they fail these after joining).

	can_character_join = {
		# Must share this faction's faith.
		scope:faction = {
			has_variable = faction_faith
			root.faith = var:faction_faith
		}
	}

	can_county_join = {
		# Must share this faction's faith.
		scope:faction = {
			OR = {
				NOT = {
					has_variable = faction_faith
				}
				AND = {
					has_variable = faction_faith
					root.faith = var:faction_faith
				}
			}
			trigger_if = { # Must have a different faith than state faith in Admin
				limit = {
					exists = scope:faction.faction_target.primary_title.state_faith
				}
				root.faith != scope:faction.faction_target.primary_title.state_faith
			}
		}

		title_province = {
			NOR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}
	}

	can_county_create = {
		county_opinion < 0

		# Can only join factions against our top liege (not direct liege)
		scope:target = holder.top_liege

		# Must have a culture or faith different from the target's.
		OR = {
			#make sure you do not accept their faith
			faith ?= {
				faith_hostility_level = {
					target = scope:target.faith
					value > faith_fully_accepted_level
				}
			}
			culture != scope:target.culture
		}

		trigger_if = { # Must have a different faith than state faith in Admin
			limit = {
				exists = scope:faction.faction_target.primary_title.state_faith
			}
			faith != scope:faction.faction_target.primary_title.state_faith
		}

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

		title_province = {
			NOR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}
	}

	can_character_become_leader = {
		has_variable = rebel_leader_peasants
		var:rebel_leader_peasants = scope:faction
	}

	###########
	# Counties/characters are *willing* to join or remain in the faction (not blocked from being in the faction, but unless these are true they will not join the faction unless forced).

	county_create_score = {
		base = -160 # Counties can't create a new faction unless opinion is less than -20 (see summary below).

		# If a suitable Popular Faction already exists (of the same faith), side with it instead.
		modifier = {
			add = -1000
			scope:target = {
				any_targeting_faction = {
					faction_type = populist_faction
					any_faction_county_member = {
						this.title_province.faith = root.title_province.faith
					}
				}
			}
		}
		# If a suitable Escalated Faction exists, prefer to join it instead.
		modifier = {
			desc = "FACTION_REASON_PREFER_ESCALATED_FACTION"
			add = -100
			scope:target = {
				any_targeting_faction = {
					faction_type = escalated_peasant_faction
				}
			}
		}

		# Summary of score weights (base + modifiers):
		#	Different Culture Group + Evil Faith: 100% chance at -32 opinion, 1% chance at -19 opinion
		#	Different Culture (Same Group) + Hostile Faith: 90% chance at -100 opinion, 1% chance at -46 opinion
		# 	Different Culture AND Astray Faith: 40% chance at -100 opinion, 1% chance at -80 opinion
		#	Different Culture OR Astray Faith: Never Creates
		compare_modifier = {
			trigger = {
				this.title_province.culture != scope:target.culture
				this.title_province.culture = { has_same_culture_heritage = scope:target.culture }
			}
			value = county_opinion
			multiplier = -1.0
		}
		compare_modifier = {
			trigger = {
				NOT = { this.title_province.culture = { has_same_culture_heritage = scope:target.culture } }
			}
			value = county_opinion
			multiplier = -3.0
		}
		pluralism_fundamentalism_dual_modifier = {
			ASTRAY_BASE_VALUE = faction_county_opinion_astray
			HOSTILE_BASE_VALUE = faction_county_opinion_hostile
			EVIL_BASE_VALUE = faction_county_opinion_evil
			BASE_FAITH = faith
			TARGET = scope:target
		}
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = faction_county_opinion_astray
			HOSTILE_BASE_VALUE = faction_county_opinion_hostile
			EVIL_BASE_VALUE = faction_county_opinion_evil
			BASE_FAITH = faith
			TARGET = scope:target
		}

		# Greatly reduced chance for a character's capital county to form a faction against them (extra -25 opinion needed).
		modifier = {
			add = -200
			this.title_province = scope:target.capital_province
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:target
			SCORE_PER_TRAIT = 50 # Worth ~12 opinion per trait.
		}

		liege_debt_modifier = {
			TARGET = scope:target
			SCORE_PER_DEBT_LEVEL = 20 # Worth ~5 opinon per debt level, up to a max of 140 (-35 opinion)
		}

		# Ruling Caste
		modifier = {
			add = -25
			culture != scope:target.culture
			scope:target.culture = {
				has_cultural_parameter = peasant_and_populist_factions_less_common
			}
		}

		# Defiant Ambushers
		modifier = {
			add = 25
			county_opinion < 0
			culture != scope:target.culture
			culture = { has_cultural_parameter = county_peasant_and_populist_factions_more_common }
		}

		# Realm Stability Settings
		modifier = {
			add = 50
			has_game_rule = lesser_realm_stability
		}
		modifier = {
			add = -100
			has_game_rule = higher_realm_stability
		}
		modifier = {
			add = -200
			has_game_rule = extreme_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 50
			has_game_rule = lesser_realm_stability_ai_only
			scope:target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -100
			has_game_rule = higher_realm_stability_ai_only
			scope:target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -200
			has_game_rule = extreme_realm_stability_ai_only
			scope:target = {
				is_ai = yes
			}
		}

		# Conquerors
		modifier = {
			add = -100
			scope:target = { ai_should_get_conqueror_bonuses = yes }
		}
		modifier = {
			add = -200
			scope:target = { ai_should_get_extreme_conqueror_bonuses = yes }
		}
		# 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_more_peasant_factions
				}
			}
		}
	
		# 10-year 'grace period' from populist factions forming after game start
		modifier = {
			years_from_game_start <= 10
			factor = {
				value = 0
				if = {
					limit = { years_from_game_start >= 5 }
					add = years_from_game_start
					subtract = 5
					divide = 5 # In year 6 factions can begin forming, but at 20% weighting. This increases to 40% in year 7, 60% in year 8, etc., up to 100% in year 10 where things are then normal.
				}
			}
		}
	}

	county_join_score =  {
		base = -120 # Counties won't join unless opinion is less than -15 (see summary below).

		modifier = {
			add = -1000
			# Do not join if we have nothing to gain (already share Liege faith and will not share new liege Culture (Group)).
			AND = {
				# We already share a Faith with the Top Liege.
				title_province.faith = scope:faction.faction_target.faith
				# And we do NOT share a Culture Group with the faction leader.
				NOT = { this.title_province.culture = { has_same_culture_heritage = scope:faction.var:faction_culture } }
			}
		}
		# If a suitable Escalated Faction exists, prefer to join it instead.
		modifier = {
			desc = "FACTION_REASON_PREFER_ESCALATED_FACTION"
			add = -100
			scope:faction.faction_target = {
				any_targeting_faction = {
					faction_type = escalated_peasant_faction
				}
			}
		}

		# Summary of score weights (base + modifiers):
		#	Different Culture Group + Evil Faith: 100% chance at -28 opinion, 1% chance at -15 opinion
		#	Different Culture (Same Group) + Hostile Faith: 100% chance at -88 opinion, 1% chance at -34 opinion
		# 	Different Culture (Same Group) AND Astray Faith: 80% chance at -100 opinion, 1% chance at -60 opinion
		#	Different Culture (Same Group) OR Astray Faith: Never Joins
		compare_modifier = {
			desc = "FACTION_REASON_CULTURAL_REASONS"
			trigger = {
				NOR = {
					this.title_province.culture = scope:faction.faction_target.culture
					AND = {
						scope:faction.faction_target = { has_active_diarchy = yes }
						this.title_province.culture = scope:faction.faction_target.diarch.culture
					}
				}
				OR = {
					this.title_province.culture = { has_same_culture_heritage = scope:faction.faction_target.culture }
					AND = {
						scope:faction.faction_target = { has_active_diarchy = yes }
						this.title_province.culture = { has_same_culture_heritage = scope:faction.faction_target.diarch.culture }
					}
				}
			}
			value = county_opinion
			multiplier = -1.0
		}
		compare_modifier = {
			desc = "FACTION_REASON_CULTURAL_REASONS"
			trigger = {
				NOR = {
					this.title_province.culture = scope:faction.faction_target.culture
					AND = {
						scope:faction.faction_target = { has_active_diarchy = yes }
						this.title_province.culture = scope:faction.faction_target.diarch.culture
					}
				}
			}
			value = county_opinion
			multiplier = -3.0
		}
		pluralism_fundamentalism_dual_modifier = {
			ASTRAY_BASE_VALUE = faction_county_opinion_astray
			HOSTILE_BASE_VALUE = faction_county_opinion_hostile
			EVIL_BASE_VALUE = faction_county_opinion_evil
			BASE_FAITH = faith
			TARGET = scope:faction.faction_target
		}
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = faction_county_opinion_astray
			HOSTILE_BASE_VALUE = faction_county_opinion_hostile
			EVIL_BASE_VALUE = faction_county_opinion_evil
			BASE_FAITH = faith
			TARGET = scope:faction.faction_target
		}

		# Reduced chance for a character's capital county to join a faction against them (extra -10 opinion needed).
		modifier = {
			desc = "FACTION_REASON_CAPITAL_COUNTY"
			add = -80
			exists = scope:faction.faction_target.capital_province
			this.title_province = scope:faction.faction_target.capital_province
		}

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

		liege_debt_modifier = {
			TARGET = scope:faction.faction_target
			SCORE_PER_DEBT_LEVEL = 20 # Worth ~5 opinon per debt level, up to a max of 140 (-35 opinion)
		}

		# Ruling Caste
		modifier = {
			desc = "FACTION_REASON_RULING_CASTE"
			add = -25
			culture != scope:faction.faction_target.culture
			scope:faction.faction_target.culture = {
				has_cultural_parameter = peasant_and_populist_factions_less_common
			}
		}

		# Defiant Ambushers
		modifier = {
			add = 25
			county_opinion < 0
			culture != scope:faction.faction_target.culture
			culture = { has_cultural_parameter = county_peasant_and_populist_factions_more_common }
		}

		# Realm Stability Settings
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 50
			has_game_rule = lesser_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -100
			has_game_rule = higher_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -200
			has_game_rule = extreme_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 50
			has_game_rule = lesser_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -100
			has_game_rule = higher_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -200
			has_game_rule = extreme_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}

		# Conquerors
		modifier = {
			add = -100
			scope:faction.faction_target = { ai_should_get_conqueror_bonuses = yes }
		}
		modifier = {
			add = -200
			scope:faction.faction_target = { ai_should_get_extreme_conqueror_bonuses = yes }
		}

		# Regent
		modifier = {
			desc = "FACTION_REASON_REGENT_CULTURAL_REASONS"
			add = -100
			scope:faction.faction_target.culture != culture
			scope:faction.faction_target = { has_active_diarchy = yes }
			scope:faction.faction_target.diarch.culture ?= culture
		}
		modifier = {
			desc = "FACTION_REASON_REGENT_FAITH_REASONS"
			add = -100
			scope:faction.faction_target.faith != faith
			scope:faction.faction_target = { has_active_diarchy = yes }
			scope:faction.faction_target.diarch.faith ?= faith
		}
		# 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_more_peasant_factions
				}
			}
		}
	}

	county_power = county_levies_to_raise

	ai_join_score = {
		base = -25

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

		modifier = {
			add = -1000
			# Do not join if we have nothing to gain (already share Liege faith and will not share new liege Culture (Group)).
			AND = {
				# We already share a Faith with the Top Liege.
				faith = scope:faction.faction_target.faith
				# And we do NOT share a Culture Group with the faction leader.
				NOT = { culture = { has_same_culture_heritage = scope:faction.var:faction_culture } }
			}
		}

		# Block characters from joining if they have a high opinion of the target
		modifier = {
			add = -1000
			save_temporary_opinion_value_as = { name = target_opinion target = scope:faction.faction_target }
			scope:target_opinion >= 80
		}

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

		# Not a de jure vassal or consequent part of the realm is out of the De Jure
		modifier = {
			desc = "FACTION_REASON_RIGHTFUL"
			add =	{
				value = 125
				# Larger realms are inherently more rebellious.
				add = {
					value = sub_realm_size
					multiply = 2
				}
			}
			OR = {
				NOT = {
					scope:faction.faction_target = {
						is_rightful_liege_of = root
					}
				}
				any_sub_realm_county = {
					percent <= 0.5
					save_temporary_scope_as = current_county
					scope:faction.faction_target.primary_title = {
						any_in_de_jure_hierarchy = {
							this = scope:current_county
						}
					}
				}
			}
		}

		# Cultural and religious differences with liege
		modifier = { # Between +50 and +100 based on culture
			desc = "FACTION_REASON_CULTURAL_REASONS"
			NOT = { culture = { has_same_culture_heritage = scope:faction.faction_target.culture } }
			NOT = { culture = { has_cultural_parameter = doesnt_care_about_culture_faith_in_factions } }
			culture = { has_same_culture_heritage = scope:faction.var:faction_culture }
			add = {
				value = 50
				if = {
					limit = {
						culture != scope:faction.faction_target.culture
						culture = scope:faction.var:faction_culture
					}
					add = 50
				}
			}
		}

		# Base values can range from +17.5 to +250 depending on hostility and pluralism/fundamentalism levels.
		pluralism_fundamentalism_dual_modifier = {
			ASTRAY_BASE_VALUE = 35
			HOSTILE_BASE_VALUE = 75
			EVIL_BASE_VALUE = 125
			BASE_FAITH = faith
			TARGET = scope:faction.faction_target
		}
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = 35
			HOSTILE_BASE_VALUE = 75
			EVIL_BASE_VALUE = 125
			BASE_FAITH = faith
			TARGET = scope:faction.faction_target
		}

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

		liege_debt_modifier = {
			TARGET = scope:faction.faction_target
			SCORE_PER_DEBT_LEVEL = 10
		}

		# The AI is almost always unwilling to join a faction against their friends and lovers.
		modifier = {
			desc = "FACTION_REASON_TARGET_FRIEND_LOVER"
			add = -250
			OR = {
				has_relation_lover = scope:faction.faction_target
				has_relation_friend = scope:faction.faction_target
			}
		}

		# Dread. Does not apply if the faction is above the power threshold
		intimidated_from_faction_modifier = {
			TARGET = scope:faction.faction_target
			POWER =  scope:faction.faction_power
			THRESHOLD = scope:faction.faction_power_threshold
		}

		# Leader never leaves
		modifier = {
			desc = "FACTION_REASON_FACTION_LEADER"
			has_variable = rebel_leader_peasants
			add =  10000
		}

		# Ruling Caste
		modifier = {
			desc = "FACTION_REASON_RULING_CASTE"
			add = -20
			culture != scope:faction.faction_target.culture
			scope:faction.faction_target.culture = {
				has_cultural_parameter = peasant_and_populist_factions_less_common
			}
		}

		# Realm Stability Settings
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 25
			has_game_rule = lesser_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -50
			has_game_rule = higher_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -100
			has_game_rule = extreme_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 25
			has_game_rule = lesser_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -50
			has_game_rule = higher_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -100
			has_game_rule = extreme_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}

		##########################
		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		join_faction_stacking_modifiers = yes

		# Small multiplier for my neighbors already in the Faction
		modifier = {
			desc = "FACTION_REASON_NEIGHBORS"
			factor = {
				value = 1
		    	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
	    }
		#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_more_peasant_factions
				}
			}
		}
	}

	on_creation = {
		random_faction_county_member = {
			save_scope_as = founding_county
		}

		set_variable = {
			name = faction_culture
			value = scope:founding_county.culture
		}
		set_variable = {
			name = faction_faith
			value = scope:founding_county.faith
		}
	}

	on_destroy = {
		set_variable = {
			name = peasant_destroying
			value = yes
		}
		if = {
			limit = { exists = special_character }
			special_character = {
				if = {
					limit = {
						OR = {
							has_character_flag =  peasant_faction_claimant_without_title
							has_character_flag =  peasant_faction_random_peasant
						}
						has_variable = peasant_title
					}
					if = {
						limit = {
							has_variable = peasant_title
						}
						destroy_title = this.var:peasant_title
					}
					if = {
						limit = { # To make sure we aren't in debt
							gold > 0
						}
						remove_long_term_gold = gold # Zero out our wallet since the revolt is over and we're going back to being unlanded.
					}
				}
				if = {
					limit = {
						is_alive = yes # To prevent dead people from being checked for variables
					}
					if = {
						limit = {
							has_variable = rebel_leader_peasants
						}
						remove_variable = rebel_leader_peasants
					}
					remove_variable = peasant_title
					remove_character_flag = peasant_faction_random_peasant
					remove_character_flag = peasant_faction_claimant_without_title
					if = {
						limit = {
							has_character_flag =  peasant_faction_random_peasant
							NOT = {	has_character_flag = peasant_revolt_do_not_kill }
						}
						death = {
							death_reason = death_vanished
						}
					}
				}
			}
		}
	}

	demand = {
		setup_populist_leader_effect = yes
		# Faction leader is unhappy if they reach the point of needing to issue an ultimatim.
		faction_leader = {
			add_opinion = {
				modifier = angry_opinion
				target = root.faction_target
				opinion = -20
			}
		}

		# Refresh the faction's target title (best target may have changed between creation and pressing demands).
		get_popular_revolt_target_effect = { FACTION = this }

		# Save scopes that will be used in the demand event.
		save_scope_as = faction
		special_character = {
			save_scope_as = peasant_leader
		}
		special_title = {
			save_scope_as = target_title
		}

		# Check for relevant vassals that owns any land targeted by the faction.
		save_temporary_scope_as = county_faction
		every_faction_county_member = {
			limit = {
				holder = {
					this != scope:faction.faction_target
					NOT = {
						joined_faction ?= {
							this = scope:county_faction
						}
					}
					OR = {
						is_vassal_of = scope:faction.faction_target
						is_ai = no
					}
					NOT = { is_in_list = popular_faction_vassal_targets }
				}
			}
			holder = {
				add_to_list = popular_faction_vassal_targets
			}
		}
		every_faction_county_member = {
			holder = {
				every_liege_or_above = {
					limit = {
						this != scope:faction.faction_target
						NOT = {
							joined_faction ?= {
								this = scope:county_faction
							}
						}
						OR = {
							is_vassal_of = scope:faction.faction_target
							is_ai = no
						}
						NOT = { is_in_list = popular_faction_vassal_targets }
					}
					add_to_list = popular_faction_vassal_targets
				}
			}
		}

		# Check if there is a player affected by the faction. AI rulers are blocked from accepting the faction demand if so.
		if = {
			limit = {
				any_in_list = {
					list = popular_faction_vassal_targets
					is_ai = no
				}
			}
			scope:faction = {
				set_variable = {
					name = faction_targets_player
					value = yes
				}
			}
		}

		# Fire the demand event. A follow up event will be sent to affected vassals.
		scope:faction.faction_target = {
			trigger_event = faction_demand.1001
		}
	}

	ai_demand_chance = {
		base = 100

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

	on_war_start = {
		# Duchy initialization so we can concentrate the rebels in just a few important counties instead of spreading them over the entire realm.
		every_faction_county_member = {
			duchy = {
				add_to_list = areas_of_rebellion
			}
		}

		# Check where in the duchy the rebellion troops should spawn, then spawn them.
		every_in_list = {
			list = areas_of_rebellion
			save_temporary_scope_as = this_duchy

			# Determine where in the duchy those troops should spawn.
			ordered_in_de_jure_hierarchy = {
				# Troops can only spawn in counties that belong to our faction!
				limit = {
					tier = tier_county
					any_title_joined_faction = {
						this = root
					}
				}

				# Sort counties by how many levies they provide; rebels will spawn in the strongest county.
				order_by = total_county_levies

				title_province = {
					save_temporary_scope_as = local_center_of_rebellion
				}
			}

			# Spawn the faction troops in the designated location.
			root.faction_leader = {
				spawn_popular_revolt_troops = yes
			}

			# Give the populist leader a commander for each stack of troops.
			create_character = {
				template = new_commander_character
				location = scope:local_center_of_rebellion
				faith = root.faction_leader.faith
				culture = root.faction_leader.culture
				save_scope_as = new_populist_commander
				gender_female_chance = {
					if = {
						limit = { root.faction_leader.culture = { has_cultural_parameter = martial_custom_male_only_combatant } }
						add = 0
					}
					else_if = {
						limit = { root.faction_leader.culture = { has_cultural_parameter = martial_custom_female_only_combatant } }
						add = 100
					}
					else = {
						add = 50
					}
				}
			}
			scope:new_populist_commander = {
				set_employer = root.faction_leader
			}
		}

		# Give the populist leader a small purse so they don't immediately go bankrupt with army maintenance.
		every_faction_county_member = {
			root.faction_leader = {
				add_gold = 25
			}
		}
	}

	leader_leaves = {
		# Should only trigger when the leader is captured in battle.
		if = {
			limit = {
				special_character = {
					is_alive = no
				}
			}
			faction_war = {
				end_war = defender
			}
			destroy_faction = yes
		}
		if = {
			limit = {
				NOT = { has_variable = peasant_destroying }
				exists = faction_war
			}
			faction_war = {
				end_war = defender
			}
		}
	}

	character_allow_create = no

	special_character_title = "FACTIONS_WINDOW_LEADER"

	inherit_membership = yes

	multiple_targeting = yes
}

escalated_peasant_faction = {
	casus_belli = populist_war

	leaders_allowed_to_leave = no
	player_can_join = yes

	sort_order = 3

	name = FACTION_ESCALATED_PEASANT_DYNAMIC_NAME

	short_effect_desc = escalated_peasant_faction_short_effect_desc

	is_shown = {
		always = no
	}

	discontent_progress = {
		base = 2
		modifier = {
			faction_target = { ep3_restored_rome_hard_mode_trigger = yes }
			add = 10
			desc = ep3_story_cycle_restoring_rome_faction_discontent
		}
		modifier = {
			faction_target = { can_vassals_be_attacked = yes }
			add = 2
			desc = border_war_increased_peasant_discontent
		}
		modifier = {
			faction_target = {
				top_participant_group:dynastic_cycle ?= {
					has_participant_group_parameter = dynastic_cycle_more_peasant_factions
				}
			}
			add = 2
			desc = instability_increased_peasant_discontent
		}
	}

	power_threshold = 0 # Peasant Factions place their demand regardless of their Power

	requires_county = yes
	requires_character = yes

	# Faction existence is valid
	is_valid = {
		trigger_if = {
			limit = {
				exists = special_character
			}
			special_character = {
				is_alive = yes
			}
		}
	}

	###########
	# Counties/characters are allowed to exist in the faction at all (will be kicked out if they fail these checks while already in it).
	is_character_valid = {
		has_valid_faction_members_trigger = yes
		is_imprisoned = no

		# Not forbidden from being in factions.
		NOT = {
			has_character_flag = joining_faction_block
		}
	}

	is_county_valid = {
		# Only in Dynastic Cycle
		any_county_situation = {
			situation_type = dynastic_cycle
		}
		# Counties can only be in a faction against the top liege in their realm (who is inherently independent).
		scope:faction.faction_target = holder.top_liege

		# Player capitals are blocked from joining populist factions (to avoid Game Overing a player due to a populist uprising against their liege, which as a vassal they can't really prevent or fight)
		trigger_if = {
			limit = {
				holder = {
					is_ai = no
				}
			}
			this != holder.capital_county
		}
		title_province = {
			NOR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}

	}

	###########
	# Counties/characters are allowed to create/join the faction (but will remain in faction if they fail these after joining).

	can_character_join = {
		custom_tooltip = {
			text = can_join_escalated_peasant_faction_tt
			OR = {
				domicile ?= {
					has_domicile_parameter = can_join_peasant_factions
				}
				has_trait = peasant_leader
			}
		}
	}

	can_county_join = {	
		OR = {
			has_county_modifier = peasant_war_lost_county_modifier
			holder = scope:faction.faction_target
		}
	}

	can_character_create = {
		OR = {
			domicile ?= {
				has_domicile_parameter = can_join_peasant_factions
			}
			has_trait = peasant_leader
		}
	}

	can_character_become_leader = {
		OR = {
			domicile ?= {
				has_domicile_parameter = can_join_peasant_factions
			}
			has_trait = peasant_leader
		}
	}

	county_join_score =  {
		base = 0
		compare_modifier = {
			desc = "FACTION_REASON_COUNTY_OPINION"
			value = county_opinion
			multiplier = -1.0
		}
		# If a suitable Popular Faction exists, perfer to join it instead.
		modifier = {
			desc = "FACTION_REASON_PREFER_POPULIST_FACTION"
			add = -100
			scope:faction.faction_target = {
				top_liege = {
					any_targeting_faction = {
						faction_type = populist_faction
						any_faction_county_member = {
							this.title_province.faith = root.title_province.faith
						}
					}
				}
			}
		}

		# Reduced weight for a character's own capital to join a revolt against them.
		modifier = {
			desc = "FACTION_REASON_CAPITAL_COUNTY"
			add = -10
			this.title_province = scope:faction.faction_target.capital_province
		}

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

		# Ruling Caste
		modifier = {
			desc = "FACTION_REASON_RULING_CASTE"
			add = -25
			culture != scope:faction.faction_target.culture
			scope:faction.faction_target.culture = { has_cultural_parameter = peasant_and_populist_factions_less_common }
		}

		# Defiant Ambushers
		modifier = {
			add = 25
			county_opinion < 0
			culture != scope:faction.faction_target.culture
			culture = { has_cultural_parameter = county_peasant_and_populist_factions_more_common }
		}

		# Difficulty Settings
		modifier = { # Easy
			desc = "FACTION_REASON_DIFFICULTY_EASY"
			add = -50
			has_game_rule = easy_difficulty
			scope:faction.faction_target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			desc = "FACTION_REASON_DIFFICULTY_VERY_EASY"
			add = -150
			has_game_rule = very_easy_difficulty
			scope:faction.faction_target = {
				is_ai = no
			}
		}
		modifier = { # Hard
			desc = "FACTION_REASON_DIFFICULTY_HARD"
			add = -50
			has_game_rule = hard_difficulty
			scope:faction.faction_target = {
				is_ai = yes
			}
		}
		modifier = { # Very Hard
			desc = "FACTION_REASON_DIFFICULTY_VERY_HARD"
			add = -150
			has_game_rule = very_hard_difficulty
			scope:faction.faction_target = {
				is_ai = yes
			}
		}

		modifier = { # Conquerors
			desc = "NO_FRIVOLOUS_ACTIVITIES_REASON"
			add = -85
			scope:faction.faction_target = {
				ai_should_get_conqueror_bonuses = yes
			}
		}

		modifier = { # Conquerors
			desc = "NO_FRIVOLOUS_ACTIVITIES_REASON"
			add = -150
			scope:faction.faction_target = {
				ai_should_get_extreme_conqueror_bonuses = yes
			}
		}

		# Realm Stability Settings
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 25
			has_game_rule = lesser_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -50
			has_game_rule = higher_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -100
			has_game_rule = extreme_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 25
			has_game_rule = lesser_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -50
			has_game_rule = higher_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -100
			has_game_rule = extreme_realm_stability_ai_only
			scope:faction.faction_target = {
				is_ai = yes
			}
		}

		# Conquerors
		modifier = {
			add = -100
			scope:faction.faction_target = { ai_should_get_conqueror_bonuses = yes }
		}
		modifier = {
			add = -200
			scope:faction.faction_target = { ai_should_get_extreme_conqueror_bonuses = yes }
		}

		# stay if at war with the target
		modifier = {
			desc = "FACTION_REASON_AT_WAR_WITH_TARGET"
			add = 1000
			exists = joined_faction
			joined_faction = {
				faction_is_at_war = yes
			}
		}
		# TGP Dynastic Cycle in Instability phase
		modifier = {
			desc = "FACTION_REASON_DYNASTIC_CYCLE_INSTABILITY"
			add = 25
			county_opinion < 0
			scope:faction.faction_target = {
				top_participant_group:dynastic_cycle ?= {
					has_participant_group_parameter = dynastic_cycle_more_peasant_factions
				}
			}
		}
		# Pure Land
		modifier = {
			desc = "FACTION_REASON_POPULIST_PEASANT_MORE_LIKELY"
			add = 25
			county_opinion < 0
			faith != scope:faction.faction_target.faith
			faith = {
				has_doctrine_parameter = peasant_and_populist_factions_more_common
			}
		}
		modifier = {
			desc = "FACTION_REASON_ESCALATED_TO_LIEGE"
			add = 100
			has_county_modifier = peasant_war_lost_county_modifier
		}
	}

	county_power = county_levies_to_raise

	ai_create_score = {
		base = -10000 #handled by scripted effect peasant_faction_demands_enforced in 00_faction_effects.txt
	}

	ai_join_score = {
		base = 0
		modifier = {
			desc = "FACTION_REASON_ESCALATED_TO_LIEGE"
			add = 100
			has_variable = peasant_war_escalates
		}
	}

	can_county_create = {
		holder.top_liege = scope:target
		scope:target = {
			NAND = {
				is_ai = yes
				OR = {
					has_trait = greatest_of_khans
					has_character_modifier = the_great_khan_modifier
				}
			}
		}

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

		title_province = {
			NOR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}
	}

	on_creation = {
	}

	on_destroy = {
		set_variable = {
			name = peasant_destroying
			value = yes
		}
		if = {
			limit = { exists = special_character }
			special_character = {
				if = {
					limit = {
						OR = {
							has_character_flag =  peasant_faction_claimant_without_title
							has_character_flag =  peasant_faction_random_peasant
						}
						has_variable = peasant_title
					}
					if = {
						limit = {
							has_variable = peasant_title
						}
						destroy_title = this.var:peasant_title
					}
					if = {
						limit = { # To make sure we aren't in debt
							gold > 0
						}
						remove_long_term_gold = gold # Zero out our wallet since the revolt is over and we're going back to being unlanded.
					}
				}
				if = {
					limit = {
						is_alive = yes # To prevent dead people from being checked for variables
					}
					if = {
						limit = {
							has_variable = rebel_leader_peasants
						}
						remove_variable = rebel_leader_peasants
					}
					remove_variable = peasant_title
					remove_character_flag = peasant_faction_random_peasant
					remove_character_flag = peasant_faction_claimant_without_title
					if = {
						limit = {
							has_character_flag =  peasant_faction_random_peasant
							NOT = {	has_character_flag = peasant_revolt_do_not_kill }
						}
						death = {
							death_reason = death_vanished
						}
					}
				}
			}
		}
	}

	demand = {
		setup_populist_leader_effect = yes
		# Faction leader is unhappy if they reach the point of needing to issue an ultimatim.
		faction_leader = {
			add_opinion = {
				modifier = angry_opinion
				target = root.faction_target
				opinion = -20
			}
		}

		# Refresh the faction's target title (best target may have changed between creation and pressing demands).
		get_popular_revolt_target_effect = { FACTION = this }

		# Save scopes that will be used in the demand event.
		save_scope_as = faction
		if = {
			limit = {
				special_character.joined_faction ?= root
			}
			special_character = {
				save_scope_as = peasant_leader
			}
		}
		else = {
			faction_leader = {
				save_scope_as = peasant_leader
			}
		}
		special_title = {
			save_scope_as = target_title
		}

		# Check for relevant vassals that owns any land targeted by the faction.
		save_temporary_scope_as = county_faction
		every_faction_county_member = {
			limit = {
				holder = {
					this != scope:faction.faction_target
					NOT = {
						joined_faction ?= {
							this = scope:county_faction
						}
					}
					OR = {
						is_vassal_of = scope:faction.faction_target
						is_ai = no
					}
					NOT = { is_in_list = popular_faction_vassal_targets }
				}
			}
			holder = {
				add_to_list = popular_faction_vassal_targets
			}
		}
		every_faction_county_member = {
			holder = {
				every_liege_or_above = {
					limit = {
						this != scope:faction.faction_target
						NOT = {
							joined_faction ?= {
								this = scope:county_faction
							}
						}
						OR = {
							is_vassal_of = scope:faction.faction_target
							is_ai = no
						}
						NOT = { is_in_list = popular_faction_vassal_targets }
					}
					add_to_list = popular_faction_vassal_targets
				}
			}
		}

		# Check if there is a player affected by the faction. AI rulers are blocked from accepting the faction demand if so.
		if = {
			limit = {
				any_in_list = {
					list = popular_faction_vassal_targets
					is_ai = no
				}
			}
			scope:faction = {
				set_variable = {
					name = faction_targets_player
					value = yes
				}
			}
		}

		# Fire the demand event. A follow up event will be sent to affected vassals.
		scope:faction.faction_target = {
			trigger_event = faction_demand.1001
		}
	}

	ai_demand_chance = {
		base = 100

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

	on_war_start = {
		# Duchy initialization so we can concentrate the rebels in just a few important counties instead of spreading them over the entire realm.
		every_faction_county_member = {
			duchy = {
				add_to_list = areas_of_rebellion
			}
		}

		# Check where in the duchy the rebellion troops should spawn, then spawn them.
		every_in_list = {
			list = areas_of_rebellion
			save_temporary_scope_as = this_duchy

			# Determine where in the duchy those troops should spawn.
			ordered_in_de_jure_hierarchy = {
				# Troops can only spawn in counties that belong to our faction!
				limit = {
					tier = tier_county
					any_title_joined_faction = {
						this = root
					}
				}

				# Sort counties by how many levies they provide; rebels will spawn in the strongest county.
				order_by = total_county_levies

				title_province = {
					save_temporary_scope_as = local_center_of_rebellion
				}
			}

			# Spawn the faction troops in the designated location.
			root.faction_leader = {
				spawn_popular_revolt_troops = yes
			}

			# Give the populist leader a commander for each stack of troops.
			create_character = {
				template = new_commander_character
				location = scope:local_center_of_rebellion
				faith = root.faction_leader.faith
				culture = root.faction_leader.culture
				save_scope_as = new_populist_commander
				gender_female_chance = {
					if = {
						limit = { root.faction_leader.culture = { has_cultural_parameter = martial_custom_male_only_combatant } }
						add = 0
					}
					else_if = {
						limit = { root.faction_leader.culture = { has_cultural_parameter = martial_custom_female_only_combatant } }
						add = 100
					}
					else = {
						add = 50
					}
				}
			}
			scope:new_populist_commander = {
				set_employer = root.faction_leader
			}
		}

		# Give the populist leader a small purse so they don't immediately go bankrupt with army maintenance.
		every_faction_county_member = {
			root.faction_leader = {
				add_gold = 25
			}
		}
		faction_leader = {
			spawn_army = {
				name = peasant_leader_army_name
				men_at_arms = {
					type = pikemen_militia
					stacks = {
					    value = root.faction_leader.location.county.county_levies_to_raise
					    divide = 100
					    multiply = {
					        value = "root.faction_leader.has_trait_xp(peasant_leader)"
					        multiply = 0.05
					    }
						round = yes
					    min = 1
					}
				}
				men_at_arms = {
					type = torch_bearers
					stacks = {
						value = "root.faction_leader.has_trait_xp(peasant_leader)"
						multiply = 0.1
						round = yes
						min = 1
					}
				}
				war = root.faction_war
				location = root.faction_leader.location
				origin = root.faction_leader.location
				inheritable = no
				war_keep_on_attacker_victory = yes
			}
		}
		every_faction_member = {
			limit = {
				NOT = { has_trait = peasant_leader }
			}
			add_trait = peasant_leader
			add_trait_xp = {
				trait = peasant_leader
				value = peasant_leader_xp_value
			}
			set_variable = {
				name = rebel_leader_peasants
				value = root
			}
		}
	}

	leader_leaves = {
		# Should only trigger when the leader is captured in battle.
		if = {
			limit = {
				special_character = {
					is_alive = no
				}
			}
			faction_war = {
				end_war = defender
			}
			destroy_faction = yes
		}
		if = {
			limit = {
				NOT = { has_variable = peasant_destroying }
				exists = faction_war
			}
			faction_war = {
				end_war = defender
			}
		}
	}

	character_allow_create = yes

	special_character_title = "FACTIONS_WINDOW_LEADER"

	inherit_membership = no

	multiple_targeting = no

	county_can_switch_to_other_faction = no
}
