﻿
rouse_rabble_scheme_prep_effect = {
	save_scope_as = scheme
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:sas_scheme_critical_moments.1001
	}
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = sas_scheme_critical_moments.0002 }
	}
}

rouse_rabble_reveler_lifestyle_rank_up_check_effect = {
	if = {
		limit = {
			NOT = { has_trait = lifestyle_reveler }
		}
		random = {
			chance = 15
			modifier = {
				add = -10
				faith = { trait_is_sin = lifestyle_reveler }
			}
			modifier = {
				add = -10
				has_trait = temperate
			}
			modifier = {
				add = -10
				has_trait = inappetetic
			}
			modifier = {
				add = 10
				culture = { has_cultural_parameter = reveler_traits_more_valued }
			}
			modifier = {
				add = 5
				has_trait = gluttonous
			}
			modifier = {
				add = 5
				has_trait = drunkard
			}
			modifier = {
				add = 5
				has_trait = comfort_eater
			}
			modifier = {
				add = 15
				exists = var:reveler_lifestyle_progress
				var:reveler_lifestyle_progress >= reveler_rank_up_3_threshold
			}
			modifier = {
				add = 15
				exists = var:reveler_lifestyle_progress
				var:reveler_lifestyle_progress >= reveler_rank_up_2_threshold
			}
			modifier = {
				add = 15
				exists = var:reveler_lifestyle_progress
				var:reveler_lifestyle_progress >= reveler_rank_up_1_threshold
			}
			trigger_event = rouse_rabble_outcome.0005
			show_as_tooltip = {
				add_trait = lifestyle_reveler
			}
		}
	}
	else = {
		if = {
			limit = {
				NOT = { has_trait = lifestyle_reveler }
				exists = var:reveler_lifestyle_progress
				var:reveler_lifestyle_progress >= reveler_rank_up_3_threshold
			}
			add_trait_xp = {
				trait = lifestyle_reveler
				value = 10
			}
		}
		else_if = {
			limit = {
				exists = var:reveler_lifestyle_progress
				var:reveler_lifestyle_progress >= reveler_rank_up_2_threshold
			}
			add_trait_xp = {
				trait = lifestyle_reveler
				value = 5
			}
		}
		else_if = {
			limit = {
				exists = var:reveler_lifestyle_progress
				var:reveler_lifestyle_progress >= reveler_rank_up_1_threshold
			}
			add_trait_xp = {
				trait = lifestyle_reveler
				value = 3
			}
		}
		else = {
			add_trait_xp = {
				trait = lifestyle_reveler
				value = 1
			}
		}
		set_variable = {
			name = reveler_lifestyle_progress
			value = 0
		}
	}
}

spawn_roused_rabble_troops = {
	# Spawn Levies
	spawn_army = {
		levies = {
			value = 0
			joined_faction = {
				every_faction_county_member = {
					limit = {
						duchy = scope:local_center_of_rebellion.duchy
					}
					add = roused_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 = roused_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 = roused_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 = roused_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
	}
}

roused_rabble_demands_enforced = {
	$FACTION$ = {
		every_faction_county_member = {
			custom = roused_rabble_faction_every_county
			change_county_control = peasant_war_victory_county_control_loss
			add_county_modifier = {
				modifier = roused_rabble_war_lost_county_modifier
				years = 10
			}
		}
		hidden_effect = {
			clean_roused_rabble_county_modifiers_from_faction_members_effect = yes
			destroy_faction = yes
		}
	}
}

clean_roused_rabble_county_modifiers_from_faction_members_effect = {
	every_faction_county_member = {
		if = {
			limit = { has_county_modifier = county_rabble_roused_modifier }
			remove_county_modifier = county_rabble_roused_modifier
		}
		if = {
			limit = { exists = var:schemer_funded_rabble }
			remove_variable = schemer_funded_rabble
		}
	}
}

rouse_rabble_failure_effect = {
	scope:target = {	
		add_character_modifier = {
			modifier = watchful_modifier
			days = watchful_modifier_duration
		}

		add_opinion = {
			target = scope:owner
			modifier = attempted_to_rouse_rabble
		}
	}
}
