﻿#EK NOTE: added our character creation effects, removed vanilla titles, added our governments

#####################################################################
# 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 = {
								NOT = { 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 = {
								NOT = { 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$
	}
}

split_noncontiguous_counties_from_revolt_effect = {
	# Start off by making a list of all disconnected Counties
	every_connected_county = {
		invert = yes
		max_naval_distance = 2000
		add_to_list = disconnected_counties
	}

	# Then release contiguous disconnected areas.
	every_in_list = {
		list = disconnected_counties

		if = {
			limit = {
				holder.top_liege = $FACTION_LEADER$
			}
			save_scope_as = county_of_origin
			
			create_character = {
				location = scope:county_of_origin.title_province
				culture = scope:county_of_origin.culture
				faith = scope:county_of_origin.faith
				gender_female_chance = 50
				age = 32
				save_scope_as = new_county_holder
				### EK EDIT
				after_creation = {
					ek_character_setup_effect = yes
					unmourned_blood_traits_effect = yes
				}
			}
				
			create_title_and_vassal_change = {
				type = independency
				save_scope_as = exclave_change
				add_claim_on_loss = no
			}
			every_in_list = {
				list = disconnected_counties
				limit = {
					holder.top_liege = $FACTION_LEADER$
					is_connected_to = { target = scope:county_of_origin }
				}
				change_title_holder = {
					holder = scope:new_county_holder
					change = scope:exclave_change
				}
			}
			scope:new_county_holder = {
				becomes_independent = {
					change = scope:exclave_change
				}
				#EK SETUP EFFECT
				ek_character_setup_effect = yes
				unmourned_blood_traits_effect = yes
			}
			resolve_title_and_vassal_change = scope:exclave_change
			
			scope:new_county_holder = {
				if = { # Just in case they need to create a duchy
					limit = {
						gold < 500
					}
					add_gold = 500
				}
			}
		}
	}
}

# 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$ = {
		joined_faction = {
			save_scope_as = popular_faction
			every_faction_member = {
				add_to_list = faction_members
			}
			faction_target = {
				save_scope_as = faction_target
			}
		}
	}

	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 = {
						limit = {
							tier >= tier_county
							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 = {
						limit = {
							tier >= tier_county
							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 = {
				add_to_list = siezed_counties
			}
		}
		# Additionally, if we're at war (and not just pressing demands)...
		if = {
			limit = {
				$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 = $FACTION_LEADER$
						faith = $FACTION_LEADER$.faith
					}
					add_to_list = siezed_counties
				}
			}
			
			# And add any war members that aren't already in the faction.
			$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
					}
				}
			}
		}

		# Select a 'capital' county for the faction leader.
		scope:popular_faction = {
			ordered_faction_county_member = {
				order_by = {
					value = total_county_levies

					multiply = {
						value = 1

						# Up to 50% bonus points for counties of the correct culture/faith.
						if = {
							limit = { culture = $FACTION_LEADER$.culture }
							add = 0.25
						}
						if = {
							limit = { faith = $FACTION_LEADER$.faith }
							add = 0.25
						}

						# Additional 50% bonus points for counties located in the de jure area of the liege title we want to sieze.
						if = {
							limit = {
								target_is_de_jure_liege_or_above = $TARGET_TITLE$
							}
							add = 0.50
						}

						# Remove 75% for counties owned by a player.
						if = {
							limit = {
								holder = { is_ai = no }
							}
							subtract = 0.75
						}
					}
				}

				#remove_from_list = siezed_counties
				save_scope_as = capital_county
			}
		}
		
		# CHANGE ZERO: Give the leader a county to stop landless characters from screwing up the hierarchy.
		hidden_effect = {
			create_title_and_vassal_change = {
				type = conquest_populist
				save_scope_as = change_zero
				add_claim_on_loss = yes
			}
			scope:capital_county = {
				change_title_holder = {
					holder = $FACTION_LEADER$
					change = scope:change_zero
					take_baronies = no
				}
			}
			resolve_title_and_vassal_change = scope:change_zero
		}
		
		# Let's add in any additional counties belonging to other participants, which isn't part of the faction already, so that we can assign a kingdom/duchy tier title properly.
		# (We'll remove these again further down.)
		if = {
			limit = {
				any_in_list = {
					list = faction_members
					NOT = { this = $FACTION_LEADER$ }
				}
			}
			every_in_list = {
				list = faction_members
				limit = {
					NOT = { this = $FACTION_LEADER$ }
				}
				every_sub_realm_county = {
					limit = {
						NOT = { is_in_list = siezed_counties }
					}
					add_to_list = siezed_counties
				}
			}
		}
		# CHANGE ONE: Give the leader an appropriately-ranked title.
		create_title_and_vassal_change = {
			type = conquest_populist
			save_scope_as = change_one
			add_claim_on_loss = yes
		}
		if = {
			# Check if we can give them a kingdom.
			limit = {
				OR = {
					any_in_list = { # If the realm is large enough to make a duchy unfeasible.
						list = siezed_counties
						count >= 20
					}
					any_in_list = { # If there is a kingdom title available for creation/usurpation.
						list = siezed_counties
						kingdom = {
							any_de_jure_county = {
								percent >= 0.5
								is_in_list = siezed_counties
							}
							OR = {
								NOT = { exists = holder }
								trigger_if = {
									limit = { exists = holder }
									holder.top_liege = scope:faction_target 

									# Don't usurp the primary title from independent rulers if they don't have an equal-tier title to replace it with.
									trigger_if = {
										limit = {
											this = holder.primary_title
											holder = holder.top_liege
										}
										holder = {
											any_held_title = {
												tier = tier_kingdom
												count >= 2
											}
										}
									}

									# Don't usurp the primary title from vassal players if they don't have an equal-tier title to replace it with
									trigger_if = {
										limit = {
											holder = { is_ai = no }
											NOT = { holder = holder.top_liege }
										}
										holder = {
											any_held_title = {
												tier = tier_kingdom
												count >= 2
											}
										}
									}
								}
							}
							#EK DISABLED: No HRE
							# Special block to stop populist factions from creating Germany if the HRE is still alive and kicking.
							#NAND = {
							#	this = title:k_east_francia
							#	exists = title:e_hre.holder
							#}
						}
					}
				}
			}

			# Check if we can create or usurp a De Jure Kingdom from the map
			every_in_list = {
				list = siezed_counties
				kingdom = {
					if = {
						limit = {
							any_de_jure_county = {
								percent >= 0.5
								is_in_list = siezed_counties
							}
							OR = {
								NOT = { exists = holder }
								trigger_if = {
									limit = { exists = holder }
									holder.top_liege = scope:faction_target 

									# Don't usurp the primary title from independent rulers if they don't have an equal-tier title to replace it with.
									trigger_if = {
										limit = {
											this = holder.primary_title
											holder = holder.top_liege
										}
										holder = {
											any_held_title = {
												tier = tier_kingdom
												count >= 2
											}
										}
									}

									# Don't usurp the primary title from vassal players if they don't have an equal-tier title to replace it with
									trigger_if = {
										limit = {
											holder = { is_ai = no }
											NOT = { holder = holder.top_liege }
										}
										holder = {
											any_held_title = {
												tier = tier_kingdom
												count >= 2
											}
										}
									}
								}
							}
							#EK DISABLED: No HRE
							# Special block to stop populist factions from creating Germany if the HRE is still alive and kicking.
							#NAND = {
							#	this = title:k_east_francia
							#	exists = title:e_hre.holder
							#}
						}
						add_to_list = potential_kingdoms
					}
				}
			}

			# If we found one or more valid De Jure Kingdoms, pick the best one for us to take.
			if = {
				limit = {
					any_in_list = {
						list = potential_kingdoms
						count >= 1
						#EK DISABLED: no HRE
						#NAND = {
						#	this = title:k_east_francia
						#	exists = title:e_hre.holder
						#}
					}
				}
				ordered_in_list = {
					list = potential_kingdoms
					#EK DISABLED: no HRE
					#limit = {
					#	NAND = {
					#		this = title:k_east_francia
					#		exists = title:e_hre.holder
					#	}
					#}

					order_by = {
						value = 1
						every_in_list = {
							list = siezed_counties
							limit = { kingdom = prev }
							add = 1
						}
					}

					change_title_holder = {
						holder = $FACTION_LEADER$
						change = scope:change_one
					}
				}
			}
			# Otherwise, create a new titular Kingdom title.
			else = {
				create_popular_revolt_title_effect = {
					FACTION = scope:popular_faction
					FACTION_LEADER = $FACTION_LEADER$
					TIER = kingdom
				}
				scope:new_title = {
					change_title_holder = {
						holder = $FACTION_LEADER$
						change = scope:change_one
					}
				}
			}
		}
		else_if = {
			# Check if we can give them a duchy.
			limit = {
				OR = {
					any_in_list = { # If the realm is large enough to make a county as the highest tier unfeasible.
						list = siezed_counties
						count >= 8
					}
					any_in_list = { # If there is a duchy title available for creation/usurpation.
						list = siezed_counties
						duchy = {
							any_de_jure_county = {
								percent >= 0.5
								is_in_list = siezed_counties
							}
							OR = {
								NOT = { exists = holder }
								trigger_if = {
									limit = { exists = holder }
									holder.top_liege = scope:faction_target
				
									# Don't usurp the primary title from independent rulers if they don't have an equal-tier title to replace it with.
									trigger_if = {
										limit = {
											this = holder.primary_title
											holder = holder.top_liege
										}
										holder = {
											any_held_title = {
												tier = tier_duchy
												count >= 2
											}
										}
									}
				
									# Don't usurp the primary title from vassal players if they don't have an equal-tier title to replace it with
									trigger_if = {
										limit = {
											holder = { is_ai = no }
											NOT = { holder = holder.top_liege }
										}
										holder = {
											any_held_title = {
												tier = tier_duchy
												count >= 2
											}
										}
									}
								}
							}
						}
					}
				}
			}

			# Check if we can create or usurp a De Jure Duchy from the map
			every_in_list = {
				list = siezed_counties
				duchy = {
					if = {
						limit = {
							any_de_jure_county = {
								percent >= 0.4
								is_in_list = siezed_counties
							}
							OR = {
								NOT = { exists = holder }
								trigger_if = {
									limit = { exists = holder }
									holder.top_liege = scope:faction_target

									# Don't usurp the primary title from independent rulers if they don't have an equal-tier title to replace it with.
									trigger_if = {
										limit = {
											this = holder.primary_title
											holder = holder.top_liege
										}
										holder = {
											any_held_title = {
												tier = tier_duchy
												count >= 2
											}
										}
									}

									# Don't usurp the primary title from vassal players if they don't have an equal-tier title to replace it with
									trigger_if = {
										limit = {
											holder = { is_ai = no }
											NOT = { holder = holder.top_liege }
										}
										holder = {
											any_held_title = {
												tier = tier_duchy
												count >= 2
											}
										}
									}
								}
							}
						}
						add_to_list = potential_duchies
					}
				}
			}

			# If we found one or more valid De Jure Duchies, pick the best one for us to take.
			if = {
				limit = {
					any_in_list = {
						list = potential_duchies
						count >= 1
					}
				}
				ordered_in_list = {
					list = potential_duchies

					order_by = {
						value = 1
						every_in_list = {
							list = siezed_counties
							limit = { duchy = prev }
							add = 1
						}
					}

					change_title_holder = {
						holder = $FACTION_LEADER$
						change = scope:change_one
					}
				}
			}
			# Otherwise, create a new titular Duchy title.
			else = {
				create_popular_revolt_title_effect = {
					FACTION = scope:popular_faction
					FACTION_LEADER = $FACTION_LEADER$
					TIER = duchy
				}
				scope:new_title = {
					change_title_holder = {
						holder = $FACTION_LEADER$
						change = scope:change_one
					}
				}
			}
		}

		# If the new realm is too small, or is unable to take/create a higher tier title, the populist leader only gets the county titles themselves.
		resolve_title_and_vassal_change = scope:change_one
		
		# If the title we gave our leader 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
			}
		}

		# CHANGE TWO: Transfer the siezed counties to the leader.
		create_title_and_vassal_change = {
			type = conquest_populist
			save_scope_as = change_two
			add_claim_on_loss = yes
		}
		if = { # But remove counties from any faction/war member first.
			limit = {
				any_in_list = {
					list = faction_members
					NOT = { this = $FACTION_LEADER$ }
				}
			}
			every_in_list = {
				list = faction_members
				limit = {
					NOT = { this = $FACTION_LEADER$ }
				}
				every_sub_realm_county = {
					remove_from_list = siezed_counties
				}
			}
		}
		every_in_list = {
			list = siezed_counties
			
			change_title_holder = {
				holder = $FACTION_LEADER$
				change = scope:change_two
				take_baronies = no
			}
		}
		resolve_title_and_vassal_change = scope:change_two

		# CHANGE THREE: Split the leader off into an independent realm. Then make all faction members vassals of the populist leader.
		create_title_and_vassal_change = {
			type = conquest_populist
			save_scope_as = change_three
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = faction_members

			# Lower-ranked vassals switch lieges to the faction leader.
			if = {
				limit = {
					this.primary_title.tier < $FACTION_LEADER$.primary_title.tier
					this.primary_title.tier >= tier_county
					NOT = { this = $FACTION_LEADER$ }
					is_landless_adventurer = no
				}
				change_liege = {
					liege = $FACTION_LEADER$
					change = scope:change_three
				}
			}
			# Same or higher-ranked vassal get independence.
			else_if = {
				limit = {
					NOT = { this = $FACTION_LEADER$ }
					is_landless_adventurer = no
				}
				hidden_effect = {
					becomes_independent = {
						change = scope:change_three
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = POPULIST_FACTION_INDEPENDENCE_TITLE
						custom_tooltip = POPULIST_FACTION_INDEPENDENCE_DESC
						left_icon = $FACTION_LEADER$
						right_icon = scope:faction_target
					}
				}
			}
		}

		resolve_title_and_vassal_change = scope:change_three

		# Sub-change: if any counties have been offended by HumSac'ing, since they're revolting, and tempers have hit fever pitch, remove their county opinion malus.
		$FACTION_LEADER$ = { fp1_remove_humsac_offended_counties_effect = yes }

		# CHANGE FOUR: If there are any disconnected counties, split them off via exclave independence rules.
		# However. We don't do this if there is a player in the faction, since it can remove land from the player, or in worst case, make the player game over.
		if = {
			limit = {
				NOT = {
					any_in_list = {
						list = faction_members
						is_ai = no
					}
				}
			}
			scope:capital_county = {
				split_noncontiguous_counties_from_revolt_effect = {
					FACTION_LEADER = $FACTION_LEADER$
				}
			}
		}
		
		# If the faction still exists, dissolve it (it's no longer relevant).
		hidden_effect = {
			if = {
				limit = {
					exists = scope:popular_faction
				}
				scope:popular_faction = {
					destroy_faction = yes
				}
			}
		}

		# Set up government type correctly.
		hidden_effect = {
			$FACTION_LEADER$ = {
				#If sourced from tribal, make tribal.
				if = {
					limit = {
						$SOURCE_GOVERNMENT$ = { government_has_flag = government_is_tribal }
					}
					change_government = tribal_government
					add_realm_law = tribal_authority_0
					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 }
				}
				### EK EDIT: Added the new EK2 governments
				# If not Tribal, then we get a Feudal-like gov, depending on our culture
				# Vanilla cares about the government type of the defender, but we don't lol
				else_if = {
					limit = { should_use_clan_government = yes }
					change_government = clan_government
				}
				else_if = {
					limit = { should_use_feudal_government = yes }
					change_government = feudal_government
				}
				else = {
					change_government = autocracy_government
				}
				#EK EDIT END
			}
		}
		
		# Give the leader some gold if they don't have any - We do this at the very end to make sure the gold is applied correctly.
		hidden_effect = {
			if = {
				limit = {
					$FACTION_LEADER$ = {
						gold < 10
					}
				}
				every_in_list = { # Scale the amount on the number of counties taken.
					list = siezed_counties
					$FACTION_LEADER$ = {
						add_gold = 50
					}
				}
			}
		}
	}
}

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 = siezed_counties
				duchy = {
					every_de_jure_county = { # Seize entire duchies
						limit = {
							holder.top_liege = scope:faction_target
							NOT = { is_in_list = siezed_counties }
						}
						add_to_list = siezed_counties
					}
					if = {
						limit = {
							NOT = { is_in_list = siezed_duchies }
						}
						add_to_list = siezed_duchies
					}
				}
			}
		}

		# Select a 'capital' county for the faction leader.
		scope:nomadic_faction = {
			ordered_in_list = {
				list = siezed_counties
				order_by = {
					value = county_fertility

					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 = siezed_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 = siezed_counties
					NOT = {
						is_in_list = divided_titles
					}
				}
			}
			random_in_list = {
				list = siezed_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 = siezed_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 = {
						gold < 50
					}
					add_gold = 150
				}
				if = {
					limit = {
						is_ruler = yes
						NOT = {
							government_has_flag = government_is_nomadic
						}
					}
					change_government = nomad_government
				}
				if = {
					limit = {
						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 = siezed_duchies
			change_title_holder = {
				holder = $FACTION_LEADER$
				change = scope:change
				take_baronies = no
			}
		}
		every_in_list = {
			list = siezed_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_bombard }
		}
		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
	spawn_army = {
		men_at_arms = {
			type = torch_bearers
			stacks = 5
		}
		location = scope:local_center_of_rebellion
		war = root.faction_war
		name = nomadic_faction_event_troops
	}
}

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

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 we can't find any, then generate a new character to be the populist leader of the revolt.
	if = {
		limit = {
			NOT = { exists = scope:peasant_leader }
		}
		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
			#EK SETUP EFFECT
			ek_character_setup_effect = yes
			unmourned_blood_traits_effect = yes
		}
	}

	# 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
		#EK SETUP EFFECT
		ek_character_setup_effect = yes
		unmourned_blood_traits_effect = yes
	}


	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
	}

	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 = {
					#EK EDIT: made peasant factions a bit more serious
					levies = {
						value = scope:county.county_levies_to_raise
						multiply = 2
					}
					#EK EDIT END
					location = scope:county_location
					war = $FACTION$.faction_war
					name = $PEASANT_ARMY_NAME$
				}
			}
		}
	}
}
