﻿peasant_war = {
	icon = peasant_war
	group = civil_war
	ai_only_against_liege = yes
	target_titles = none
	allowed_for_character =	{
		scope:attacker = {
		   OR = {
			is_leading_faction_type = peasant_faction
			is_ai = no
		   }
		}
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}

	allowed_against_character = {
		scope:attacker = {
			joined_faction = {
				faction_leader = scope:attacker
				any_faction_county_member = {
					holder = {
						OR = {
							is_vassal_or_below_of = scope:defender
							this = scope:defender
						}
					}
				}
			}
		}
	}

	target_de_jure_regions_above = yes
	target_top_liege_if_outside_realm = no

	valid_to_start = {}

	should_invalidate = {}

	on_declaration = {
		#on_declared_war = yes

		scope:defender = {
			add_opinion = {
				modifier = peasant_revolt_opinion
				target = scope:attacker
			}
			
			# Legitimacy loss because why would you have peasants revolting
			if = {
				limit = { is_valid_for_legitimacy_change = yes }
				add_legitimacy = medium_legitimacy_loss
			}

			# Struggle Stuffs
			if = {
				limit = {
					any_character_struggle = {
						phase_has_catalyst = catalyst_populist_uprise
						involvement = involved
					}
				}
				every_character_struggle = {
					involvement = involved
					activate_struggle_catalyst = {
						catalyst = catalyst_populist_uprise
						character = this
					}
				}
			}
		}

		# Generate troops
		hidden_effect = {
			generate_troops_from_revolt_county_modifiers_effect = yes
		}
	}

	on_victory_desc = {
		desc = peasant_war_victory_desc
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker = {
			joined_faction = {
				peasant_faction_demands_enforced = {FACTION = this}
			}
			remove_character_modifier = peasant_oppressed_modifier
			dynasty = { add_dynasty_modifier = priviliged_peasants_modifier }																	
		}

		scope:defender = {
			# Legitimacy effect
			faction_war_end_defeat_legitimacy_effect = yes
			# Struggle Catalysts
			hidden_effect = {
				if = {
					limit = {
						is_important_or_vip_struggle_character = yes
						any_character_struggle = {
							involvement = involved
							activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
								CATALYST = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
								CHAR = scope:attacker
							}
						}
					}
					every_character_struggle = {
						involvement = involved
						limit = {
							activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
								CATALYST = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
								CHAR = scope:attacker
							}
						}
						activate_struggle_catalyst = {
							catalyst = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
							character = scope:defender
						}
					}
				}
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes											
	}

	on_white_peace_desc = {
		desc = peasant_war_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = { 
			show_pow_release_message_effect = yes
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}
		scope:attacker = {
			joined_faction = {
				destroy_faction = yes
			}
			if = { limit = { is_ai = no }
			add_character_flag = peasant_revolt_do_not_kill								  
            }
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}
	}
	
	on_invalidated = { 
      scope:attacker = {
			joined_faction = {
				destroy_faction = yes
			}
			if = { limit = { is_ai = no }
			add_character_flag = peasant_revolt_do_not_kill								  
             }
		}
	}
		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	on_defeat_desc = {
		desc = peasant_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:defender = {
			add_character_flag = peasant_revolt_do_not_kill
			remove_variable = rebel_leader_peasants
		}
		scope:attacker = {
			joined_faction = {
				every_faction_county_member = {
					custom = peasant_faction_every_county
					add_county_modifier = {
						modifier = county_increased_opinion_modifier
						years = 25
					}
				}
				destroy_faction = yes
			}
		}
		scope:defender = {
			if = {
				limit = {
					NOT = { #In case defender has changed
						has_opinion_modifier = {
							modifier = peasant_revolt_opinion
							target = scope:attacker
						}
					}
				}
				add_opinion = {
					modifier = peasant_revolt_opinion
					target = scope:attacker
				}
			}
			if = {
				limit = {
					scope:attacker = { is_imprisoned = no }
				}
				imprison = {
					target = scope:attacker
					type = dungeon
				}
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes											
	}

	on_invalidated_desc = msg_invalidate_war_title

	on_primary_attacker_death = invalidate
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = no
	defender_allies_inherit = yes

	war_name = "PEASANT_WAR_NAME"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_attacker_score_from_battles = 100
	max_defender_score_from_battles = 50

	max_ai_diplo_distance_to_title = 500
}
