

#####################################################################
# EFFECT LIST
#####################################################################

# join_faction_with_leave_blocker_effect - make a character join a faction, and prevent them from leaving
# leave_faction_with_cooldown_effect - makes a character leave a FACTION and not rejoin another faction for YEARS years
# get_popular_revolt_target_effect - looks for a valid title for a popular revolt to target (may not be the title the revolt gets if successful, just determines the 'region' the revolt is supposed to occur within),
# successful_popular_revolt_outcome_effect - handles all of the title changes necessary for a successful popular revolt to break away as an independent realm.
# get_popular_revolt_title_name - Script logic to check what the new name of dynamically-created peasant titles should be (e.g., Kingdom of Isma'ili Arabia).

join_faction_with_leave_blocker_effect = {
	join_faction = $FACTION$
	hidden_effect = {
		save_temporary_scope_value_as = {
			name = join_faction_with_leave_blocker_effect_number
			value = flag:$YEARS$
		}
		add_character_flag = {
			flag = leaving_faction_block
			years = $YEARS$
		}
	}
	if = {
		limit = { this = root }
		custom_tooltip = join_faction_with_leave_blocker_effect_first.tt
	}
	else = {
		custom_tooltip = join_faction_with_leave_blocker_effect_third.tt
	}

	#and to prevent angry error messages :D:D:D:D
	if = {
		limit = {
			scope:join_faction_with_leave_blocker_effect_number = flag:1
		}
	}
}

add_faction_cooldown_effect = {
	if = {
		limit = { is_landless_adventurer = no }
		custom_description = {
			text = faction_cooldown_effect
			value = $YEARS$
			add_character_flag = {
				flag = joining_faction_block
				years = $YEARS$
			}
		}
	}
}

leave_faction_with_cooldown_effect = {
	leave_faction = $FACTION$
	add_faction_cooldown_effect = { YEARS = $YEARS$ }
}

imprisonment_retribution_start_faction_war_if_valid = {
	scope:recipient = {
		custom_tooltip = FACTION_IMPRISONMENT_RETALIATION_WARNING
	}
	hidden_effect = {
		if = {
			limit = {
				faction_power > faction_power_threshold
				NOT = {	faction_is_type = populist_faction }
			}

			if = {
				limit = {
					faction_is_type = claimant_faction
				}
				special_title = {
					save_scope_as = target_title
				}
				faction_start_war = {
					title = scope:target_title
				}
			}
			else = {
				faction_start_war = {}
			}
			every_faction_member = {
				trigger_event = faction_demand.9001
			}
		}
	}
}

# Get the most appropriate 'target title' we can for a popular revolt (determines the de jure area the war will target, not final titles granted).
get_popular_revolt_target_effect = {
	$FACTION$ = {
		save_scope_as = faction
		faction_target = {
			save_scope_as = faction_target
		}

		# First check if there are any valid kingdoms to target.
		if = {
			limit = {
				# Only large revolts in large realms are eligible to target an entire kingdom.
				any_faction_county_member = {
					count >= 11
				}
				scope:faction_target.primary_title.tier >= tier_kingdom
			}
			every_faction_county_member = {
				kingdom = {
					if = {
						# Do not target titles held by rulers outside of our realm.
						limit = {
							NOT = { exists = holder }
							holder.top_liege = scope:faction_target
						}
					}
					add_to_list = tmp_potential_kingdoms
				}
			}

			# Out of the valid kingdoms (if any), pick the 'best' one
			ordered_in_list = {
				list = tmp_potential_kingdoms

				order_by = {
					# First look at the total number of rebels within the kingdom's de jure territory.
					value = 0
					add = {
						value = 0
						every_in_de_jure_hierarchy = {
							limit = {
								tier = tier_county
								any_title_joined_faction = {
									this = $FACTION$
								}
							}
							add = total_county_levies
						}
					}

					if = {
						limit = {
							exists = holder
						}
						if = {
							# Prefer targeting titles held by the faction target...
							limit = {
								holder != scope:faction_target
							}
							multiply = 0.5
						}
						else_if = {
							#...but not the target's primary title (we don't want to usurp them if we can help it!)
							limit = {
								this = scope:faction_target.primary_title
							}
							multiply = 0.5
						}
					}

				}
				save_scope_as = tmp_target_title
			}
		}

		# If we were ineligible for kingdom-tier or failed to find a valid one, look for duchies.
		if = {
			limit = {
				NOT = { exists = scope:tmp_target_title}

				# Mid-sized or larger revolts in medium realms are eligible for duchy titles.
				any_faction_county_member = {
					count >= 5
				}
				faction_target.primary_title.tier >= tier_duchy
			}
			every_faction_county_member = {
				duchy = {
					if = {
						# Do not target titles held by rulers outside of our realm.
						limit = {
							NOT = { exists = holder }
							holder.top_liege = scope:faction_target
						}
					}
					add_to_list = tmp_potential_duchies
				}
			}

			# Out of the valid duchies (if any), pick the 'best' one
			ordered_in_list = {
				list = tmp_potential_duchies

				order_by = {
					value = 0
					# First look at the total number of rebels within the duchy's de jure territory.
					add = {
						value = 0
						every_in_de_jure_hierarchy = {
							limit = {
								tier = tier_county
								any_title_joined_faction = {
									this = $FACTION$
								}
							}
							add = total_county_levies
						}
					}

					if = {
						limit = {
							exists = holder
						}
						if = {
							# Prefer targeting titles held by the faction target...
							limit = {
								holder != scope:faction_target
							}
							multiply = 0.5
						}
						else_if = {
							#...but not the target's primary title (we don't want to usurp them if we can help it!)
							limit = {
								this = scope:faction_target.primary_title
							}
							multiply = 0.5
						}
					}
				}
				save_scope_as = tmp_target_title
			}
		}

		# If we still don't have a target title, pick the largest county.
		if = {
			limit = {
				NOT = { exists = scope:tmp_target_title}
			}
			ordered_faction_county_member = {
				order_by = total_county_levies

				save_scope_as = tmp_target_title
			}
		}

		# Re-save our temporary scope as the 'official' target scope, then clear all temporary scopes.
		if = {
			limit = {
				exists = scope:tmp_target_title
			}
			scope:tmp_target_title = {
				save_scope_as = target_title
			}
			set_special_title = scope:target_title
			clear_saved_scope = tmp_target_title
		}
		every_in_list = {
			list = tmp_potential_kingdoms
			remove_from_list = tmp_potential_kingdoms
		}
		every_in_list = {
			list = tmp_potential_duchies
			remove_from_list = tmp_potential_duchies
		}
	}
}

add_adjacent_valid_counties_to_list = {
	every_neighboring_county = {
		limit = {
			faith = $FAITH$
			holder = {
				is_ai = yes # Without this, successful Populist Revolts can lead to suprise Game Overs for players without any warning.
				top_liege = $TOP_LIEGE$
			}
			NOT = { is_in_list = $LIST$ }
		}
		add_to_list = $LIST$
	}
}

# Shared effect between the Popular Faction's demand event and victory outcome.
#
# Parameters:
#	$FACTION_LEADER$
#	$TARGET_TITLE$
#	$SOURCE_GOVERNMENT$
#
successful_popular_revolt_outcome_effect = {
	# Save scopes for later usage.
	$FACTION_LEADER$ = {
		save_scope_as = faction_leader
		joined_faction = {
			save_scope_as = popular_faction
			every_faction_member = {
				add_to_list = faction_members
			}
			faction_target = {
				save_scope_as = faction_target
			}
		}
		add_to_list = faction_members
	}
	$TARGET_TITLE$ = {
		save_scope_as = some_title
	}
	if = { # Populists factions in an admin realm should aim to change state faith, if they are of the capital culture
		limit = {
			$SOURCE_GOVERNMENT$ = { government_allows = state_faith }
			$FACTION_LEADER$.culture = scope:faction_target.capital_county.culture
			$FACTION_LEADER$.faith != scope:faction_target.primary_title.state_faith
		}

		$SOURCE_GOVERNMENT$.primary_title = {
			# Change the state faith
			set_state_faith = $FACTION_LEADER$.faith

			# Try grabbing someone from the line of succession who practices the State Faith
			if = {
				limit = {
					any_title_heir = {
						faith = $FACTION_LEADER$.faith
					}
				}
				ordered_title_heir = {
					order_by = "appointment_candidate_score(prev)"
					limit = {
						faith = $FACTION_LEADER$.faith
					}
					save_scope_as = new_ruler
				}
			}
			# Otherwise, make the peasant leader the new emperor
			else = { $FACTION_LEADER$ = { save_scope_as = new_ruler } }

			# Actually make the switch
			create_title_and_vassal_change = {
				type = conquest_populist
				save_scope_as = change
			}
			# Transfer all titles to heir of primary title if only one governorship is held
			hidden_effect = {
				$SOURCE_GOVERNMENT$ = {
					every_held_title = {
						title_tier >= county
						limit = {
							is_landless_type_title = no
							is_noble_family_title = no
						}
						change_title_holder_include_vassals = {
							holder = scope:new_ruler
							change = scope:change
							take_baronies = no
						}
					}
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
	else_if = { # Populists factions of the state faith in an admin realm where the emperor is of a heretical faith simply take the empire
		limit = {
			$SOURCE_GOVERNMENT$ = { government_allows = state_faith }
			$FACTION_LEADER$.faith = scope:faction_target.primary_title.state_faith
			$FACTION_LEADER$.faith != scope:faction_target.faith
		}

		$SOURCE_GOVERNMENT$.primary_title = {

			$FACTION_LEADER$ = { save_scope_as = new_ruler }

			# Actually make the switch
			create_title_and_vassal_change = {
				type = conquest_populist
				save_scope_as = change
			}
			# Transfer all titles to heir of primary title if only one governorship is held
			hidden_effect = {
				$SOURCE_GOVERNMENT$ = {
					every_held_title = {
						title_tier >= county
						limit = {
							is_landless_type_title = no
							is_noble_family_title = no
						}
						change_title_holder_include_vassals = {
							holder = scope:new_ruler
							change = scope:change
							take_baronies = no
						}
					}
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
	else = { # Otherwise we run the normal script
		# Compile a list of all counties belonging to the faction.
		scope:popular_faction = {
			every_faction_county_member = {
				duchy = {
					add_to_list = seized_duchies
					every_de_jure_county = {
						limit = {
							holder.top_liege = scope:faction_target
						}
						add_to_list = seized_counties
					}
				}
			}
		}
		# Additionally, if we're at war (and not just pressing demands)...
		if = {
			limit = {
				scope:faction_leader = {
					is_at_war_with = scope:faction_target
				}
			}
			# Add all occupied counties of the correct culture/faith.
			scope:faction_target = {
				every_sub_realm_county = {
					limit = {
						county_controller = scope:faction_leader
						OR = {
							culture = scope:faction_leader.culture
							faith = scope:faction_leader.faith
						}
					}
					add_to_list = seized_counties
				}
			}

			# And add any war members that aren't already in the faction.
			scope:faction_leader = {
				every_character_war = {
					limit = {
						is_defender = scope:faction_target
					}
					every_war_attacker = {
						limit = {
							NOT = { is_in_list = faction_members }
						}
						add_to_list = faction_members
					}
				}
			}
		}
		show_as_tooltip = {
			create_title_and_vassal_change = {
				type = conquest_populist
				save_scope_as = change_tooltip
				add_claim_on_loss = yes
			}
			every_in_list = {
				list = seized_duchies
				change_title_holder = {
					holder = scope:faction_leader
					change = scope:change_tooltip
					take_baronies = no
				}
				becomes_independent = {
					change = scope:change_tooltip
				}
			}
			resolve_title_and_vassal_change = scope:change_tooltip
		}
		# Give out titles in such way that each leader gets all titles within the same Kingdom
		# Step 1: Duplicate the list of all members 
		every_in_list = {
			list = faction_members
			add_to_list = faction_members_loop
		}
		# Step 2: Create a while loop to give out counties to each faction member until we run out of the counties
		while = {
			limit = {
				list_size:seized_counties >= 1
			}
			# Step 3: If there is no more leaders, create new one
			if = {
				limit = {
					list_size:faction_members_loop < 1
				}
				# Step 3.1: Pick the best county that is still available to base all other title handovers on 
				ordered_in_list = {
					list = seized_counties
					order_by = {
						value = total_county_levies
						multiply = {
							value = 1
							# Up to 50% bonus points for counties of the correct culture/faith.
							if = {
								limit = { culture != scope:faction_leader.culture }
								add = 0.25
							}
							if = {
								limit = { faith != scope:faction_leader.faith }
								add = 0.25
							}
						}
					}
					save_scope_as = capital_county
				}
				create_character = {
					location = scope:capital_county.title_province
					culture = scope:capital_county.culture
					faith = scope:capital_county.faith
					template = peasant_faction_leader_template
					gender_female_chance = scope:capital_county.location_faith_dominant_gender_female_chance
					save_scope_as = new_county_holder
				}
				scope:new_county_holder = {
					add_to_list = faction_members
					add_to_list = faction_members_loop
					add_trait_xp = {
						trait = peasant_leader
						value = peasant_leader_xp_value
					}
				}
			}
			# Step 4: Grab a faction member with highest peasant leader xp, but always grab the faction leader first
			ordered_in_list = {
				list = faction_members_loop
				order_by = {
					value = "has_trait_xp(peasant_leader)"
					if = {
						limit = {
							this = scope:faction_leader
						}
						add = 1000
					}
				}
				save_scope_as = next_leader
				# Step 4.1: Pick the best county that is still available to base all other title handovers on 
				if = {
					limit = {
						NOT = { exists = scope:capital_county }
					}
					if = {
						limit = {
							is_landed = yes
						}
						capital_county = {
							save_scope_as = capital_county
						}
					}
					else = {
						ordered_in_list = {
							list = seized_counties
							order_by = {
								value = total_county_levies
								multiply = {
									value = 1
									# Up to 300% bonus points for counties of the correct culture/faith.
									if = {
										limit = { culture = scope:next_leader.culture }
										add = 1
									}
									if = {
										limit = { faith = scope:next_leader.faith }
										add = 1
									}
								}
							}
							save_scope_as = capital_county
						}
					}
				}
				# Step 5: CHANGE ONE - Give them the selected county and make them go independent
				create_title_and_vassal_change = {
					type = conquest_populist
					save_scope_as = change_one
					add_claim_on_loss = yes
				}
				scope:capital_county = {
					change_title_holder = {
						holder = scope:next_leader
						change = scope:change_one
						take_baronies = no
					}
				}
				becomes_independent = {
					change = scope:change_one
				}
				resolve_title_and_vassal_change = scope:change_one
				# Step 6: CHANGE TWO - Give them all Duchies and Counties within the Kingdom of the capital county
				create_title_and_vassal_change = {
					type = conquest_populist
					save_scope_as = change_two
					add_claim_on_loss = yes
				}
				every_in_list = {
					list = seized_counties
					limit = {
						kingdom = scope:capital_county.kingdom
					}
					change_title_holder = {
						holder = scope:next_leader
						change = scope:change_two
						take_baronies = no
					}
					remove_from_list = seized_counties
				}
				every_in_list = {
					list = seized_duchies
					limit = {
						kingdom = scope:capital_county.kingdom
					}
					change_title_holder = {
						holder = scope:next_leader
						change = scope:change_two
						take_baronies = no
					}
					remove_from_list = seized_duchies
				}
				resolve_title_and_vassal_change = scope:change_two

				# Step 6: CHANGE THREE - Usurp the Kingdom if they hold enough land
				if = {
					limit = {
						primary_title.kingdom = {
							any_de_jure_county = {
								percent > 0.5
								holder = scope:next_leader
							}
						}
					}
					create_title_and_vassal_change = {
						type = conquest_populist
						save_scope_as = change_three
						add_claim_on_loss = yes
					}
					primary_title.kingdom = {
						change_title_holder = {
							holder = scope:next_leader
							change = scope:change_three
							take_baronies = no
						}
					}
					resolve_title_and_vassal_change = scope:change_three
				}
				# Step 6.1: If the title we gave is a new dynamic title, generate a CoA for it.
				if = {
					limit = {
						exists = scope:new_title
					}
					scope:new_title = {
						set_capital_county = scope:capital_county
						generate_coa = yes
					}
				}
				# Step 7: Destroy their peasant uprising title
				if = {
					limit = {
						has_variable = peasant_title
						exists = var:peasant_title.holder
					}
					destroy_title = var:peasant_title
				}
				# Step 8: Set up government type correctly.
				scope:capital_county = {
					previous_holder ?= {
						if = {
							limit = {
								is_alive = yes
							}
							save_scope_as = old_holder
							switch = {
								trigger = government_has_flag
								government_is_administrative = {
									save_scope_value_as = { name = old_government value = flag:admin }
								}
								government_is_clan = {
									save_scope_value_as = { name = old_government value = flag:clan }
								}
								government_is_tribal = {
									save_scope_value_as = { name = old_government value = flag:tribal }
								}
								government_is_wanua = {
									save_scope_value_as = { name = old_government value = flag:wanua }
								}
								government_is_nomadic = {
									save_scope_value_as = { name = old_government value = flag:nomad }
								}
								government_is_celestial = {
									save_scope_value_as = { name = old_government value = flag:admin }
								}
								government_is_steppe_admin = {
									save_scope_value_as = { name = old_government value = flag:admin }
								}
								government_is_meritocratic = {
									save_scope_value_as = { name = old_government value = flag:admin }
								}
								government_is_japan_administrative = {
									save_scope_value_as = { name = old_government value = flag:admin }
								}
								government_is_japan_feudal = {
									save_scope_value_as = { name = old_government value = flag:japan_feudal }
								}
								government_is_mandala = {
									save_scope_value_as = { name = old_government value = flag:mandala }
								}
							}
						}
					}
				}
				# Change our government according to the government of our liege.
				## Nomad.
				if = {
					limit = {
						OR = {
							government_has_flag = government_is_nomadic
							scope:old_government ?= flag:nomad
							scope:capital_county.title_province = {
								OR = {
									has_holding_type = nomad_holding
									has_holding_type = herder_holding
								}
							}
						}
					}
					change_government = nomad_government
				}
				## Clan.
				else_if = {
					limit = {
						OR = {
							government_has_flag = government_is_clan
							scope:old_government ?= flag:clan
							ep3_is_clan_inclined_trigger = yes
						}
					}
					change_government = clan_government
				}
				## Wanua or Tribal.
				else_if = {
					limit = {
						scope:capital_county.title_province = {
							has_holding_type = tribal_holding
						}
					}
					if = {
						limit = {
							OR = {
								government_has_flag = government_is_wanua
								scope:old_government ?= flag:wanua
								scope:capital_county.title_province = {
									geographical_region = world_asia_southeast_islands
								}
							}
						}
						change_government = wanua_government
					}
					else = {
						change_government = tribal_government
					}
				}
				## Ritsuryo.
				else_if = {
					limit = {
						OR = {
							government_has_flag = government_is_japan_feudal
							scope:old_government ?= flag:japan_feudal
							culture = culture:japanese
							culture = {
								any_parent_culture_or_above = { this = culture:japanese }
							}
						}
					}
					change_government = japan_feudal_government
				}
				## Mandala.
				else_if = {
					limit = {
						OR = {
							AND = {
								has_mandala_culture_trigger = yes
								has_mandala_faith_trigger = yes
								scope:capital_county.title_province = {
									has_holding_type = temple_citadel_holding
								}
							}
							government_has_flag = government_is_mandala
						}
					}
					change_government = mandala_government
				}
				## Administrative.
				## Celestial.
				## Steppe admin.
				## Meritocratic.
				## Soryo.
				else_if = {
					limit = {
						OR = {
							government_allows = administrative
							scope:old_government ?= flag:admin
						}
					}
					change_to_administrative_effect = yes
				}
				## Else feudal.
				else = { change_government = feudal_government }
				# Step 9: Give them some gold/treasury if they don't have any
				if = {
					limit = {
						gold <= 100
					}
					add_gold = {
						value = stewardship
						multiply = realm_size
					}
				}
				if = {
					limit = {
						has_treasury = yes
						treasury <= 100
					}
					add_treasury = {
						value = gold
						multiply = stewardship
						max = 4000
					}
				}
				# Step 10: Remove them from the looping list
				remove_from_list = faction_members_loop
				# Step 11: Clear the scopes, otherwise the while loop will break
				clear_saved_scope = next_leader
				clear_saved_scope = capital_county
			}
		}
	}
	hidden_effect = {
		# Make sure to destroy all peasant uprising titles and clean all variables
		every_in_list = {
			list = faction_members
			every_held_title = {
				limit = {
					has_variable = faction
				}
				prev = {
					destroy_title = prev
				}
			}
			remove_variable = peasant_war_escalates
			remove_variable = peasant_title
			remove_variable = rebel_leader_peasants
			remove_variable = peasant_faction_random_peasant
			remove_variable = peasant_revolt_do_not_kill
		}
		# If the faction still exists, dissolve it (it's no longer relevant).
		if = {
			limit = {
				exists = scope:popular_faction
			}
			scope:popular_faction = {
				destroy_faction = yes
			}
		}
	}
	if = {
		limit = {
			scope:faction_target.top_participant_group:dynastic_cycle ?= {
				participant_group_type = hegemon_ruler 
			}
			situation:dynastic_cycle = {
				situation_top_has_catalyst = catalyst_hegemon_lost_defensive_territorial_war
			}
		}
		situation:dynastic_cycle = {
			trigger_situation_catalyst = {
				catalyst = catalyst_hegemon_lost_defensive_territorial_war
				character = scope:faction_leader
			}
		}
	}
}

create_popular_revolt_title_effect = {
	$FACTION$ = {
		ordered_faction_county_member = {
			limit = {
				faith = $FACTION_LEADER$.faith
				culture = $FACTION_LEADER$.culture
			}
			alternative_limit = {
				always = yes
			}

			order_by = total_county_levies

			save_scope_as = base_county
		}
		$FACTION_LEADER$.culture = { save_scope_as = founder_culture }
		create_dynamic_title = {
			tier = $TIER$
			name = FACTION_POPULIST_CREATED_TITLE_NAME
		}
		scope:new_title = {
			set_variable = {
				name = faction
				value = $FACTION$
			}
			set_color_from_title = scope:base_county
		}
	}
}

successful_nomadic_revolt_outcome_effect = {
	hidden_effect = {
		# Save scopes for later usage.
		$FACTION_LEADER$ = {
			add_character_flag = successful_nomad_revolt_leader
			joined_faction = {
				save_scope_as = nomadic_faction
				every_faction_member = {
					add_to_list = faction_members
				}
				faction_target = {
					save_scope_as = faction_target
				}
			}
			if = {
				limit = {
					has_variable = peasant_title
					exists = this.var:peasant_title.holder
				}
				destroy_title = this.var:peasant_title
			}
		}
		# Compile a list of all duchies and counties belonging to the faction.
		scope:nomadic_faction = {
			every_faction_county_member = {
				add_to_list = seized_counties
				duchy = {
					every_de_jure_county = { # Seize entire duchies
						limit = {
							holder.top_liege = scope:faction_target
							NOT = { is_in_list = seized_counties }
						}
						add_to_list = seized_counties
					}
					if = {
						limit = {
							NOT = { is_in_list = seized_duchies }
						}
						add_to_list = seized_duchies
					}
				}
			}
		}

		# Select a 'capital' county for the faction leader.
		scope:nomadic_faction = {
			ordered_in_list = {
				list = seized_counties
				order_by = {
					value = 0
					if = {
						limit = {
							uses_county_fertility = yes
						}
						add = county_fertility
					}
					else = {
						add = development_level
					}

					multiply = {
						value = 1
						if = {
							limit = { culture = $FACTION_LEADER$.culture }
							add = 200
						}
						if = {
							limit = { faith = $FACTION_LEADER$.faith }
							add = 50
						}
					}
				}
				save_scope_as = capital_county
			}
		}

		# Give the leader the counties they should have and make them a nomad.
		$FACTION_LEADER$ = {
			add_to_list = independent_nomads
			change_government = nomad_government
		}
		create_title_and_vassal_change = {
			type = conquest_populist
			save_scope_as = change
			add_claim_on_loss = yes
		}
		scope:capital_county = {
			change_title_holder = {
				holder = $FACTION_LEADER$
				change = scope:change
				take_baronies = no
			}
			add_to_list = divided_titles
			duchy = {
				every_de_jure_county = { # Seize entire duchies
					limit = {
						is_in_list = seized_counties
						NOT = { is_in_list = divided_titles }
					}
					change_title_holder = {
						holder = $FACTION_LEADER$
						change = scope:change
						take_baronies = no
					}
					add_to_list = divided_titles
				}
				if = {
					limit = {
						OR = {
							NOT = { exists = holder }
							trigger_if = {
								limit = { exists = holder }
								holder.top_liege = scope:faction_target
							}
						}
						NOT = { is_in_list = divided_titles }
					}
					change_title_holder = {
						holder = $FACTION_LEADER$
						change = scope:change
					}
					add_to_list = divided_titles
				}
			}
		}
		resolve_title_and_vassal_change = scope:change

		while = {
			limit = {
				any_in_list = {
					list = seized_counties
					NOT = {
						is_in_list = divided_titles
					}
				}
			}
			random_in_list = {
				list = seized_counties
				limit = {
					NOT = {
						is_in_list = divided_titles
					}
				}
				save_scope_as = handout_county
				hidden_effect = {
					create_character = {
						location = scope:handout_county.title_province
						template = nomadic_faction_leader_template
						faith = scope:handout_county.faith
						culture = scope:handout_county.culture
						dynasty = generate
						gender_female_chance = {
							if = {
								limit = { scope:handout_county.faith = { has_doctrine = doctrine_gender_male_dominated } }
								add = 0
							}
							else_if = {
								limit = { scope:handout_county.faith = { has_doctrine = doctrine_gender_female_dominated } }
								add = 100
							}
							else = {
								add = 50
							}
						}
						save_scope_as = new_nomad
					}
					scope:new_nomad = {
						add_to_list = independent_nomads
					}
				}
				create_title_and_vassal_change = {
					type = conquest_populist
					save_scope_as = change
					add_claim_on_loss = yes
				}
				scope:handout_county = {
					change_title_holder = {
						holder = scope:new_nomad
						change = scope:change
						take_baronies = no
					}
					add_to_list = divided_titles
					duchy = {
						every_de_jure_county = { # Seize entire duchies
							limit = {
								is_in_list = seized_counties
								NOT = { is_in_list = divided_titles }
							}
							change_title_holder = {
								holder = scope:new_nomad
								change = scope:change
								take_baronies = no
							}
							add_to_list = divided_titles
						}
						if = {
							limit = {
								OR = {
									NOT = { exists = holder }
									trigger_if = {
										limit = { exists = holder }
										holder.top_liege = scope:faction_target
									}
								}
								NOT = { is_in_list = divided_titles }
							}
							change_title_holder = {
								holder = scope:new_nomad
								change = scope:change
							}
							add_to_list = divided_titles
						}
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
		}

		# Make sure everyone is independent
		create_title_and_vassal_change = {
			type = conquest_populist
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = independent_nomads
			limit = {
				is_independent_ruler = no
			}
			hidden_effect = {
				becomes_independent = {
					change = scope:change
				}
			}
		}
		resolve_title_and_vassal_change = scope:change

		# Set them up for success
		every_in_list = {
			list = independent_nomads

			hidden_effect = {
				save_temporary_scope_as = nomad_becoming_independent
				every_held_title = {
					limit = {
						tier = tier_county
					}
					if = {
						limit = {
							title_province = {
								has_holding_type = herder_holding
							}
						}
						title_province = {
							set_holding_type = nomad_holding
						}
					}
					add_county_modifier = {
						modifier = county_increased_opinion_modifier
						years = 25
					}
					if = {
						limit = {
							has_county_modifier = peasant_war_lost_county_modifier
						}
						remove_county_modifier = peasant_war_lost_county_modifier
					}
				}
				if = {
					limit = {
						gold < 50
					}
					add_gold = 150
				}
				if = {
					limit = {
						is_ruler = yes
						NOT = {
							government_has_flag = government_is_nomadic
						}
					}
					change_government = nomad_government
				}
				if = {
					limit = {
						NOT = { government_has_flag = government_allows_nomad_domicile_titles }
						NOT = {
							any_held_title = {
								is_nomad_title = yes
							}
						}
					}
					save_scope_as = holder
					create_nomad_title = {
						name = nomad_title_name
						holder = scope:holder
						government = nomad_government
						save_scope_as = new_nomad_title
					}
				}
				if = {
					limit = {
						NOT = { has_realm_law = nomadic_authority_1 }
					}
					add_realm_law_skip_effects = nomadic_authority_1
				}
				if = {
					limit = {
						highest_held_title_tier >= tier_duchy
						NOT = { has_realm_law = nomadic_authority_2 }
					}
					if = {
						limit = {
							has_realm_law = nomadic_authority_1
						}
						remove_realm_law = nomadic_authority_1
						add_realm_law_skip_effects = nomadic_authority_2
					}
				}
				if = { limit = { has_realm_law = crown_authority_0 } remove_realm_law = crown_authority_0 }
				if = { limit = { has_realm_law = crown_authority_1 } remove_realm_law = crown_authority_1 }
				if = { limit = { has_realm_law = crown_authority_2 } remove_realm_law = crown_authority_2 }
				if = { limit = { has_realm_law = crown_authority_3 } remove_realm_law = crown_authority_3 }
				domicile ?= {
					change_herd = {
						value = scope:nomad_becoming_independent.capital_county.county_fertility
						min = 20
						multiply = 150
					}
				}
			}
		}

		# If the faction still exists, dissolve it (it's no longer relevant).
		hidden_effect = {
			if = {
				limit = {
					exists = scope:nomadic_faction
				}
				scope:nomadic_faction = {
					destroy_faction = yes
				}
			}
		}
	}
	show_as_tooltip = {
		$FACTION_LEADER$ = {
			save_scope_as = tooltip_leader
			custom_tooltip = nomads_take_everything_tt
		}
		create_title_and_vassal_change = {
			type = conquest_populist
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = seized_duchies
			change_title_holder = {
				holder = $FACTION_LEADER$
				change = scope:change
				take_baronies = no
			}
		}
		every_in_list = {
			list = seized_counties
			change_title_holder = {
				holder = $FACTION_LEADER$
				change = scope:change
				take_baronies = no
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
}

spawn_popular_revolt_troops = {
	# Spawn Levies
	spawn_army = {
		levies = {
			value = 0
			joined_faction = {
				every_faction_county_member = {
					limit = {
						duchy = scope:local_center_of_rebellion.duchy
					}
					add = county_levies_to_raise
				}
			}
		}
		location = scope:local_center_of_rebellion
		war = root.faction_war
		name = populist_faction_event_troops
	}

	# Spawn MAAs - An appropriate amount of troops dependent on the terrain type they spawn in
	if = {
		limit = {
			scope:local_center_of_rebellion = {
				OR = {
					terrain = forest
					terrain = taiga
					terrain = jungle
					terrain = plains
					terrain = drylands
					terrain = oasis
					terrain = desert
					terrain = wetlands
					terrain = steppe
				}
			}
		}
		spawn_army = {
			men_at_arms = {
				type = light_footmen
				stacks = {
					value = 0
					joined_faction = {
						every_faction_county_member = {
							limit = {
								duchy = scope:local_center_of_rebellion.duchy
							}
							add = county_maa_to_raise
						}
					}
				}
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
	else_if = {
		limit = {
			scope:local_center_of_rebellion = {
				OR = {
					terrain = mountains
					terrain = desert_mountains
				}
			}
		}
		spawn_army = {
			men_at_arms = {
				type = pikemen_unit
				stacks = {
					value = 0
					joined_faction = {
						every_faction_county_member = {
							limit = {
								duchy = scope:local_center_of_rebellion.duchy
							}
							add = county_maa_to_raise
						}
					}
				}
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
	else_if = {
		limit = {
			scope:local_center_of_rebellion = {
				OR = {
					terrain = hills
					terrain = farmlands
					terrain = floodplains
				}
			}
		}
		spawn_army = {
			men_at_arms = {
				type = bowmen
				stacks = {
					value = 0
					joined_faction = {
						every_faction_county_member = {
							limit = {
								duchy = scope:local_center_of_rebellion.duchy
							}
							add = county_maa_to_raise
						}
					}
				}
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}

	# Spawn Siege Weapons depending on discovered innovations
	if = {
		limit = {
			culture = { has_innovation = innovation_gunpowder }
		}
		spawn_army = {
			men_at_arms = {
				type = bombard
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
	else_if = {
		limit = {
			culture = { has_innovation = innovation_trebuchet }
		}
		spawn_army = {
			men_at_arms = {
				type = trebuchet
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}

	}
	else_if = {
		limit = {
			culture = { has_innovation = innovation_mangonel }
		}
		spawn_army = {
			men_at_arms = {
				type = mangonel
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
	else_if = {
		limit = {
			culture = { has_innovation = innovation_catapult }
		}
		spawn_army = {
			men_at_arms = {
				type = onager
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}

	if = {
		limit = { has_variable = ep3_governor_yearly_8150_ignored }
		root.faction_leader = {
			spawn_army = {
				name = ep3_governor_yearly_8150_troop_name
				men_at_arms = {
					type = light_footmen
					stacks = 2
				}
				war = root.faction_war
				location = scope:local_center_of_rebellion
				origin = scope:local_center_of_rebellion
				inheritable = no
			}
		}
		remove_variable = ep3_governor_yearly_8150_ignored
	}
}

spawn_nomadic_revolt_troops = {
	# Spawn Horde Riders
	spawn_army = {
		men_at_arms = {
			type = nomadic_riders
			men = {
				value = 0
				joined_faction = {
					every_faction_county_member = {
						limit = {
							duchy = scope:local_center_of_rebellion.duchy
						}
						add = county_horde_riders_to_raise
					}
				}
			}
		}
		location = scope:local_center_of_rebellion
		war = root.faction_war
		name = nomadic_faction_event_troops
	}

	spawn_army = {
		men_at_arms = {
			type = steppe_raiders
			men = {
				value = 0
				joined_faction = {
					every_faction_county_member = {
						limit = {
							duchy = scope:local_center_of_rebellion.duchy
						}
						add = {
							value = county_horde_riders_to_raise
							divide = 2
						}
					}
				}
			}
		}
		location = scope:local_center_of_rebellion
		war = root.faction_war
		name = nomadic_faction_event_troops
	}

	spawn_army = {
		men_at_arms = {
			type = horse_archers
			men = {
				value = 0
				joined_faction = {
					every_faction_county_member = {
						limit = {
							duchy = scope:local_center_of_rebellion.duchy
						}
						add = {
							value = county_horde_riders_to_raise
							divide = 2
						}
					}
				}
			}
		}
		location = scope:local_center_of_rebellion
		war = root.faction_war
		name = nomadic_faction_event_troops
	}

	# Spawn Siege Weapons depending on discovered innovations
	if = {
		limit = {
			culture = { has_innovation = innovation_gunpowder }
		}
		spawn_army = {
			men_at_arms = {
				type = bombard
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
	else_if = {
		limit = {
			culture = { has_innovation = innovation_trebuchet }
		}
		spawn_army = {
			men_at_arms = {
				type = trebuchet
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}

	}
	else_if = {
		limit = {
			culture = { has_innovation = innovation_mangonel }
		}
		spawn_army = {
			men_at_arms = {
				type = mangonel
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
	else_if = {
		limit = {
			culture = { has_innovation = innovation_catapult }
		}
		spawn_army = {
			men_at_arms = {
				type = onager
				stacks = 1
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
	else = {
		spawn_army = {
			men_at_arms = {
				type = torch_bearers
				stacks = 5
			}
			location = scope:local_center_of_rebellion
			war = root.faction_war
			name = populist_faction_event_troops
		}
	}
}

peasant_faction_demands_enforced = {
	# escalate if in the Cycle
	if = {
		limit = {
			NOT = { exists = scope:defender }
		}
		save_scope_as = defender
	}
	if = {
		limit = {
			scope:defender != scope:defender.top_liege
			#only in Dynastic Cycle
			scope:defender.top_liege.top_participant_group:dynastic_cycle ?= {
				participant_group_type = hegemon_ruler
			}
		}
		scope:defender.top_liege = {
			set_variable = {
				name = peasant_war_escalates
				days = 365
			}
		}
		$FACTION$ = {
			faction_leader = {
				save_scope_as = attacker
				add_character_flag = peasant_revolt_do_not_kill
				set_variable = {
					name = peasant_war_escalates
					days = 365
				}
				add_trait_xp = {
					trait = peasant_leader
					value = 20
				}
			}
			every_faction_county_member = {
				add_to_list = faction_counties
				add_county_modifier = {
					modifier = peasant_war_lost_county_modifier
					years = 10
				}
			}
			destroy_faction = yes
		}
		if = {
			limit = {
				NOT = { exists = scope:attacker }
			}
			scope:peasant_leader = {
				save_scope_as = attacker
			}
		}
		if = {
			limit = {
				scope:defender.top_liege = {
					any_targeting_faction = {
						faction_type = escalated_peasant_faction
						save_temporary_scope_as = escalated_faction
					}
				}
			}
			scope:defender.top_liege = {
				send_interface_message = {
					type = msg_faction_grows
					left_icon = scope:attacker
					right_icon = scope:defender
					every_in_list = {
						list = faction_counties
						title_join_faction = scope:escalated_faction
					}
					scope:attacker = {
						join_faction_skip_check = scope:escalated_faction
					}
				}
			}
		}
		else = {
			scope:attacker = {
				create_faction = {
					type = escalated_peasant_faction
					target = scope:defender.top_liege
				}
			}
			scope:defender.top_liege = {
				random_targeting_faction = {
					faction_type = escalated_peasant_faction
					save_scope_as = escalated_faction
					set_special_character = scope:attacker
				}
			}
			custom_tooltip = {
				text = county_and_leader_join_faction_against_top_liege
				if = {
					limit = {
						exists = scope:escalated_faction
					}
					every_in_list = {
						list = faction_counties
						limit = {
							title_is_a_faction_member = no
						}
						title_join_faction = scope:escalated_faction
					}
					scope:attacker = {
						set_variable = {
							name = rebel_leader_peasants
							value = scope:escalated_faction
						}
						join_faction_skip_check = scope:escalated_faction
						primary_title = {
							set_title_name_dynamic = peasant_leader_title_name
						}
					}
				}
			}
		}
	}
	# otherwise finish the faction as usual
	else = {
		$FACTION$ = {
			every_faction_county_member = {
				custom = peasant_faction_every_county
				change_county_control = peasant_war_victory_county_control_loss
				add_county_modifier = {
					modifier = peasant_war_lost_county_modifier
					years = 10
				}
			}
			hidden_effect = {
				clean_revolt_county_modifiers_from_faction_members_effect = yes
				destroy_faction = yes
			}
		}
	}
}

invalidate_claimant_factions_on_death_effect = {
	every_in_list = {
		variable = claimant_factions
		if = {
			limit = {
				exists = faction_war
			}
			faction_war = {
				clear_claimant = yes
				set_casus_belli = depose_war
			}
			faction_remove_war = yes # else the war gets invalidated when we destroy the faction
		}
		destroy_faction = yes
	}
	clear_variable_list = claimant_factions
}

invalidate_ceremonial_claimant_factions_on_death_effect = {
	every_in_list = {
		variable = ceremonial_claimant_factions
		if = {
			limit = {
				exists = faction_war
			}
			faction_war = {
				clear_claimant = yes
				set_casus_belli = depose_war
			}
			faction_remove_war = yes # else the war gets invalidated when we destroy the faction
		}
		destroy_faction = yes
	}
	clear_variable_list = ceremonial_claimant_factions
}

setup_nomadic_leader_effect = {
	save_scope_as = faction

	random_faction_county_member = {
		save_scope_as = peasant_county
	}

	# Create a leader for the nomadic faction.
	create_character = {
		location = scope:peasant_county.title_province
		template = nomadic_faction_leader_template
		faith = scope:peasant_county.faith
		culture = scope:peasant_county.culture
		gender_female_chance = {
			if = {
				limit = { scope:peasant_county.faith = { has_doctrine = doctrine_gender_male_dominated } }
				add = 0
			}
			else_if = {
				limit = { scope:peasant_county.faith = { has_doctrine = doctrine_gender_female_dominated } }
				add = 100
			}
			else = {
				add = 50
			}
		}
		save_scope_as = peasant_leader
	}
	scope:peasant_leader = {
		add_character_flag = peasant_faction_random_peasant
	}

	# Create a new title for the peasant.
	create_dynamic_title = {
		tier = duchy
		name = FACTION_NOMADIC_REVOLT_TITLE_NAME
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	scope:new_title = {
		set_capital_county = scope:peasant_county
		set_landless_title = yes
		set_destroy_on_succession = yes
		set_delete_on_destroy = yes
		set_no_automatic_claims = yes
		set_definitive_form = yes
		set_can_be_named_after_dynasty = no
		change_title_holder = {
			holder = scope:peasant_leader
			change = scope:change
		}
		set_variable = {
			name = faction
			value = scope:faction
		}
	}
	resolve_title_and_vassal_change = scope:change
	scope:peasant_leader = {
		set_variable = {
			name = peasant_title
			value = scope:new_title
		}
		if = {
			limit = {
				is_ruler = yes
				NOT = {
					government_has_flag = government_is_nomadic
				}
			}
			change_government = nomad_government
		}
	}
	scope:new_title = { generate_coa = factions }

	# Set the peasant leader as the head of the faction.
	scope:peasant_leader = {
		set_variable = {
			name = rebel_leader_peasants
			value = scope:faction
		}
		join_faction_skip_check = scope:faction
	}
	set_special_character = scope:peasant_leader
}

setup_populist_leader_effect = {
	save_scope_as = faction

	random_faction_county_member = {
		save_scope_as = peasant_county
	}

	# Runs the scripted effect to find the best title to target for the rebellion.
	get_popular_revolt_target_effect = { FACTION = this }

	# Find or create a leader for the populist faction.
	scope:target_title = {
		# Look to see if there are any displaced characters who are legitimate claimants to the target title that the populists might support.
		random_claimant = {
			limit = {
				# Checks the 'is_character_valid' and 'can_character_join' triggers above, plus the faction hard/soft blocks in 00_rules.txt
				can_join_faction = scope:faction
				can_join_or_create_faction_against = scope:faction.faction_target

				# Must share a faith & culture with the revolting county.
				faith = scope:peasant_county.faith
				culture = scope:peasant_county.culture

				# Must be allowed to rule in their faith.
				trigger_if = {
					limit = {
						faith = { has_doctrine = doctrine_gender_male_dominated }
					}
					is_male = yes
				}
				trigger_else_if = {
					limit = {
						faith = { has_doctrine = doctrine_gender_female_dominated }
					}
					is_female = yes
				}
				trigger_else = {
					always = yes
				}

				# Must not already be a landed ruler, or set to inheirit a landed title.
				AND = {
					is_ruler = no
					is_playable_character = no
					NOT = {
						any_heir_title = {
							exists = this
						}
					}
				}
			}

			save_scope_as = peasant_leader
			add_character_flag = peasant_faction_claimant_without_title
			add_trait = populist_leader
		}
	}
	if = {
		limit = {
			NOT = { exists = scope:peasant_leader }
			exists = special_character
		}
		special_character = {
			save_scope_as = peasant_leader			
		}
	}
	# If we can't find any, then generate a new character to be the populist leader of the revolt.
	else = {
		create_character = {
			location = scope:peasant_county.title_province
			template = populist_faction_leader_template
			faith = scope:peasant_county.faith
			culture = scope:peasant_county.culture
			gender_female_chance = {
				if = {
					limit = { scope:peasant_county.faith = { has_doctrine = doctrine_gender_male_dominated } }
					add = 0
				}
				else_if = {
					limit = { scope:peasant_county.faith = { has_doctrine = doctrine_gender_female_dominated } }
					add = 100
				}
				else = {
					add = 50
				}
			}
			trait = populist_leader
			save_scope_as = peasant_leader
		}
		scope:peasant_leader = {
			add_character_flag = peasant_faction_random_peasant
		}
	}
	if = {
		limit = {
			NOT = { exists = scope:peasant_leader.primary_title }
		}
		# Create a new title for the peasant.
		create_dynamic_title = {
			tier = duchy
			name = FACTION_POPULIST_REVOLT_TITLE_NAME
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:new_title = {
			set_capital_county = scope:peasant_county
			set_landless_title = yes
			set_destroy_on_succession = yes
			set_delete_on_destroy = yes
			set_no_automatic_claims = yes
			set_definitive_form = yes
			set_can_be_named_after_dynasty = no
			set_can_use_nomadic_naming = no
			change_title_holder = {
				holder = scope:peasant_leader
				change = scope:change
			}
			set_variable = {
				name = faction
				value = scope:faction
			}
		}
		resolve_title_and_vassal_change = scope:change
		scope:peasant_leader = {
			set_variable = {
				name = peasant_title
				value = scope:new_title
			}
		}
		scope:new_title = { generate_coa = factions }

		# Set the peasant leader as the head of the faction.
		scope:peasant_leader = {
			set_variable = {
				name = rebel_leader_peasants
				value = scope:faction
			}
			join_faction_skip_check = scope:faction
		}
		set_special_character = scope:peasant_leader
	}
}

setup_peasant_leader_effect = {
	save_scope_as = faction
	# Does a peasant capital county already exist?
	if = {
		limit = {
			NOT = { exists = scope:peasant_county }
		}
		random_faction_county_member = { save_scope_as = peasant_county }
	}
	# Does a peasant leader already exist?
	if = {
		limit = {
			NOT = { exists = scope:peasant_leader }
		}
		create_character = {
			location = scope:peasant_county.title_province
			template = peasant_faction_leader_template
			faith = scope:peasant_county.faith
			culture = scope:peasant_county.culture
			gender_female_chance = {
				if = {
					limit = { scope:peasant_county.faith = { has_doctrine = doctrine_gender_male_dominated } }
					add = 0
				}
				else_if = {
					limit = { scope:peasant_county.faith = { has_doctrine = doctrine_gender_female_dominated } }
					add = 100
				}
				else = {
					add = 50
				}
			}
			save_scope_as = peasant_leader
		}
	}
	scope:peasant_leader = {
		add_character_flag = peasant_faction_random_peasant
	}


	create_dynamic_title = {
		tier = duchy
		name = FACTION_PEASANT_TITLE_NAME
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}

	scope:new_title = {
		set_capital_county = scope:peasant_county
		set_landless_title = yes
		set_destroy_on_succession = yes
		set_delete_on_destroy = yes
		set_no_automatic_claims = yes
		set_definitive_form = yes
		set_can_be_named_after_dynasty = no
		set_can_use_nomadic_naming = no
		change_title_holder = {
			holder = scope:peasant_leader
			change = scope:change
		}

		set_variable = {
			name = faction
			value = scope:faction
		}

		# avoid unused variable error. This variable is referenced in code
		var:faction = {}
	}
	resolve_title_and_vassal_change = scope:change
	scope:peasant_leader = {
		set_variable = {
			name = peasant_title
			value = scope:new_title
		}
	}
	scope:new_title = { generate_coa = factions }

	scope:peasant_leader = {
		set_variable = {
			name = rebel_leader_peasants
			value = scope:faction
		}
		join_faction_skip_check = scope:faction
		add_trait_xp = {
			trait = peasant_leader
			value = peasant_leader_xp_value
		}
	}

	set_special_character = scope:peasant_leader
}

faction_spawn_member_county_armies_effect = {
	every_faction_county_member = {
		custom = peasant_faction_every_county
		hidden_effect = {
			save_scope_as = county
			title_province = {
				save_scope_as = county_location
			}
		}

		# Reduce county control in faction member counties
		change_county_control = peasant_war_starts_county_control_loss

		# Spawn an army in each faction member county
		custom_tooltip = peasant_faction_spawn_county_levies
		hidden_effect = {
			$ARMY_OWNER$ = {
				spawn_army = {
					levies = scope:county.county_levies_to_raise
					location = scope:county_location
					war = $FACTION$.faction_war
					name = $PEASANT_ARMY_NAME$
				}
			}
		}
	}
}
