@base_peasant_discontent_progress = 2

peasant_faction = {
	casus_belli = peasant_war

	short_effect_desc = peasant_faction_short_effect_desc

	sort_order = 5

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

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

	name = FACTION_PEASANT_NAME

	requires_county = yes
	requires_character = no

	is_valid = {
		always = yes
	}

	is_county_valid = {
		# Peasants can only join a faction against their direct liege. Unless their liege is Byzantium trying to restore the Roman Empire, then they go for the emperor
		holder = scope:faction.faction_target
		title_province = {
			NOR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}
	}

	is_character_valid = {
		has_valid_faction_members_trigger = yes
	}

	demand = {
		save_scope_as = faction
		setup_peasant_leader_effect = yes
		special_character = {
			save_scope_as = peasant_leader
		}

		faction_leader = {
			add_opinion = {
				modifier = angry_opinion
				target = root.faction_target
				opinion = -50
			}
		}
		scope:faction.faction_target = {
			trigger_event = faction_demand.1101
		}
	}

	ai_demand_chance = {
		base = 0

		modifier = {
			add = 100
			any_faction_county_member = {}
		}

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

	on_creation = {
		random_faction_county_member = {
			save_scope_as = founding_county
		}

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

	on_destroy = {
		set_variable = {
			name = peasant_destroying
			value = yes
		}

		if = {
			limit = { exists = special_character }
			special_character = {
				if = {
					#Verify that the title exists before trying to destroy it
					limit = {
						has_variable = peasant_title
						exists = this.var:peasant_title
						NOT = { has_variable = peasant_war_escalates }
					}
					destroy_title = this.var:peasant_title
				}

				if = {
					limit = { is_alive = yes }
					# Zero out our wallet since the revolt is over.
					if = {
						limit = { # To make sure we're not in debt
							gold > 0
						}
						remove_long_term_gold = gold
					}

					# The peasant leader mysteriously vanishes on the next game tick.
					if = {
						limit = { NOT = { has_character_flag = peasant_revolt_do_not_kill } }
						trigger_event = {
							id = faction_demand.1102
							days = 1
						}
					}
				}
			}
		}
	}

	county_join_score =  {
		base = 0
		compare_modifier = {
			desc = "FACTION_REASON_COUNTY_OPINION"
			value = county_opinion
			multiplier = -1.0
		}

		# If a suitable Popular Faction exists, perfer to join it instead.
		modifier = {
			desc = "FACTION_REASON_PREFER_POPULIST_FACTION"
			add = -10
			AND = {
				county_opinion <= -15
				scope:faction.faction_target = {
					top_liege = {
						any_targeting_faction = {
							faction_type = populist_faction
							any_faction_county_member = {
								this.title_province.faith = root.title_province.faith
							}
						}
					}
				}
			}
		}
		# If a suitable Escalated Faction exists, prefer to join it instead.
		modifier = {
			desc = "FACTION_REASON_PREFER_ESCALATED_FACTION"
			add = -10
			scope:faction.faction_target = {
				any_targeting_faction = {
					faction_type = escalated_peasant_faction
				}
			}
		}

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

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

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

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

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

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

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

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

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

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

	county_create_score = {
		base = 0

		####
		# AI Modifiers
		####

		compare_modifier = {
			value = county_opinion
			multiplier = -1.0
		}

		# If a suitable Popular Faction exists, perfer to join it instead.
		modifier = {
			add = -10
			AND = {
				county_opinion <= -15
				scope:target = {
					top_liege = {
						any_targeting_faction = {
							faction_type = populist_faction
							any_faction_county_member = {
								this.title_province.faith = root.title_province.faith
							}
						}
					}
				}
			}
		}
		# If a suitable Escalated Faction exists, prefer to join it instead.
		modifier = {
			desc = "FACTION_REASON_PREFER_ESCALATED_FACTION"
			add = -100
			scope:target = {
				any_targeting_faction = {
					faction_type = escalated_peasant_faction
				}
			}
		}

		# Reduced weight for a character's own capital to join a revolt against them.
		modifier = {
			add = -20
			exists = scope:target.capital_province
			this.title_province = scope:target.capital_province
		}

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

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

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

		# Difficulty Settings
		modifier = { # Easy
			add = -50
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -150
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Hard
			desc = "FACTION_REASON_DIFFICULTY_HARD"
			add = -50
			has_game_rule = hard_difficulty
			scope:target = {
				is_ai = yes
			}
		}
		modifier = { # Very Hard
			desc = "FACTION_REASON_DIFFICULTY_VERY_HARD"
			add = -150
			has_game_rule = very_hard_difficulty
			scope:target = {
				is_ai = yes
			}
		}
		modifier = { # Conquerors
			add = -150
			scope:target = {
				ai_should_get_extreme_conqueror_bonuses = yes
			}
		}


		# Realm Stability Settings
		modifier = {
			add = 25
			has_game_rule = lesser_realm_stability
		}
		modifier = {
			add = -50
			has_game_rule = higher_realm_stability
		}
		modifier = {
			add = -100
			has_game_rule = extreme_realm_stability
		}

		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_LESSER"
			add = 25
			has_game_rule = lesser_realm_stability_ai_only
			scope:target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_HIGHER"
			add = -50
			has_game_rule = higher_realm_stability_ai_only
			scope:target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -100
			has_game_rule = extreme_realm_stability_ai_only
			scope:target = {
				is_ai = yes
			}
		}

		# Conquerors
		modifier = {
			add = -100
			scope:target = { ai_should_get_conqueror_bonuses = yes }
		}
		modifier = {
			add = -200
			scope:target = { ai_should_get_extreme_conqueror_bonuses = yes }
		}
		# TGP Dynastic Cycle in Instability phase
		modifier = {
			desc = "FACTION_REASON_DYNASTIC_CYCLE_INSTABILITY"
			add = 25
			county_opinion < 0
			scope:target = {
				top_participant_group:dynastic_cycle ?= {
					has_participant_group_parameter = dynastic_cycle_more_peasant_factions
				}
			}
		}
		# Pure Land
		modifier = {
			add = 25
			county_opinion < 0
			faith != scope:target.faith
			faith = {
				has_doctrine_parameter = peasant_and_populist_factions_more_common
			}
		}
	}

	ai_join_score = {
	}

	can_character_join = {
		custom_tooltip = {
			text = can_join_escalated_peasant_faction_tt
			OR = {
				joined_faction = scope:faction
				domicile ?= {
					has_domicile_parameter = can_join_peasant_factions
				}
				has_trait = peasant_leader
			}
		}
	}

	can_county_join = {
		# Peasants can only join a faction against their direct liege. Unless their liege is Byzantium trying to restore the Roman Empire, then they go for the emperor

		holder = scope:faction.faction_target

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

	can_county_create = {
		# Peasants can only join a faction against their direct liege. Unless their liege is Byzantium trying to restore the Roman Empire, then they go for the emperor

		holder = scope:target 

		scope:target = {
			NAND = {
				is_ai = yes
				OR = {
					has_trait = greatest_of_khans
					has_character_modifier = the_great_khan_modifier
				}
			}
		}

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

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

	can_character_become_leader = {
		always = yes
	}

	on_war_start = {
		# Give the peasant leader a small purse so they don't immediately go bankrupt with army maintenance.
		every_faction_county_member = {
			root.faction_leader = {
				add_gold = 10
			}
		}
		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 = root.faction_leader.location
					origin = root.faction_leader.location
					inheritable = no
				}
			}
			remove_variable = ep3_governor_yearly_8150_ignored
		}

		# If the target is Restoring Rome, the faction gets a military boon
#		if = {
#			limit = {
#				root.faction_target = { ep3_restored_rome_hard_mode_trigger = yes }
#			}
#			save_scope_value_as = {
#				name = spawn_army_size_levies
#				value = {
#					value = 1
#					root = {
#						every_faction_county_member = {
#							add = building_levies
#						}
#					}
#				}
#			}
#			root.faction_leader = {
#				spawn_army = {
#					name = ep3_restoring_rome_citizen_army_name
#					levies = scope:spawn_army_size_levies
#					men_at_arms = {
#						type = pikemen_unit
#						stacks = 16
#					}
#					men_at_arms = {
#						type = mangonel
#						stacks = 6
#					}
#					men_at_arms = {
#						type = armored_footmen
#						stacks = 10
#					}
#					men_at_arms = {
#						type = light_footmen
#						stacks = 10
#					}
#					men_at_arms = {
#						type = bowmen
#						stacks = 10
#					}
#					war = root.faction_war
#					location = root.faction_leader.location
#					origin = root.faction_leader.location
#					inheritable = no
#				}
#			}
#		}
			root.faction_leader = {
				spawn_army = {
					name = peasant_leader_army_name
					men_at_arms = {
						type = pikemen_militia
						stacks = {
						    value = root.faction_leader.location.county.county_levies_to_raise
						    divide = 100
						    multiply = {
						        value = "root.faction_leader.has_trait_xp(peasant_leader)"
						        multiply = 0.05
						    }
							round = yes
						    min = 1
						}
					}
					men_at_arms = {
						type = torch_bearers
						stacks = {
							value = "root.faction_leader.has_trait_xp(peasant_leader)"
							multiply = 0.1
							round = yes
							min = 1
						}
					}
					war = root.faction_war
					location = root.faction_leader.location
					origin = root.faction_leader.location
					inheritable = no
					war_keep_on_attacker_victory = yes
				}
			}
	}

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

	county_power = {
		value = county_levies_to_raise
#		if = {
#			limit = {
#				scope:faction.faction_target = { ep3_restored_rome_hard_mode_trigger = yes }
#			}
#			multiply = 2
#		}
		if = {
			limit = {
				faith = { has_doctrine_parameter = peasant_and_populist_factions_more_common }
			}
			multiply = 1.5
		}
	}

	character_allow_create = no

	special_character_title = "FACTIONS_WINDOW_LEADER"

	inherit_membership = no

	county_can_switch_to_other_faction = yes
}
