﻿@base_peasant_discontent_progress = 2

rtr_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 = yes
	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
		}
	}


	name = FACTION_PEASANT_NAME

	requires_county = no
	requires_character = yes

	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
		trigger_if = {
			limit = { 
				scope:faction.faction_target = { ep3_restored_rome_hard_mode_trigger = yes }
			}
			holder.top_liege = scope:faction.faction_target
		}
		trigger_else = { holder = scope:faction.faction_target }
	}

	is_character_valid = {
		is_ai = no
	}

	demand = {
		save_scope_as = faction
		rtr_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
			}
			add_trait = peasant_leader
		}
		scope:faction.faction_target = {
			trigger_event = faction_demand.1101
		}
		faction_leader = {
		every_traveling_family_member = { limit = { has_trait = slave } 
		  rtr_release_slave_and_remove_relation_effect = yes	
		}
		if = { limit = { has_trait = slave } rtr_release_slave_and_remove_relation_effect = yes }
		}
	}

	ai_demand_chance = {
		base = 0

		modifier = {
			add = 100
			any_faction_county_member = {}
		}
	}

	on_creation = {
		random_faction_member = {
			save_scope_as = founding_member
		}

		set_variable = {
			name = faction_culture
			value = scope:founding_member.culture
		}
		set_variable = {
			name = faction_faith
			value = scope:founding_member.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
					}
					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.
				}
			}
		}
	}

	county_join_score =  {
		base = 500
		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 = {
			add = -10
			AND = {
				county_opinion <= -15
				scope:faction.faction_target = {
					top_liege = {
						any_targeting_faction = {
							faction_is_type = populist_faction
							any_faction_county_member = {
								this.title_province.faith = root.title_province.faith
							}
						}
					}
				}
			}
		}
		
		modifier = {
			add = -400
			scope:founding_member = { has_trait = slave }
		}

		# 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
			NOT = {
				culture = scope:faction.faction_target.culture
			}
			scope:faction.faction_target.culture = {
				has_cultural_parameter = peasant_and_populist_factions_less_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 = { # 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
		}
		
		# 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
			}
		}
  
	}


	ai_join_score = {
	}

	can_character_join = {
		is_ai = no
		dynasty = { has_dynasty_modifier = lowborn_modifier }
	}

	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
		trigger_if = {
			limit = { 
				scope:faction.faction_target = { ep3_restored_rome_hard_mode_trigger = yes }
			}
			holder.top_liege = scope:faction.faction_target
		}
		trigger_else = { holder = scope:faction.faction_target }
	}

	can_character_create = { 
		is_ai = no
		scope:target = { is_landed = yes }
		is_landed = no
		has_character_modifier = peasant_oppressed_modifier
		prowess >= 9
	}

	can_character_become_leader = {
		is_ai = no
	}

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

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

	character_allow_create = yes

	special_character_title = "FACTIONS_WINDOW_LEADER"

	inherit_membership = no

	county_can_switch_to_other_faction = yes
}
