﻿
roused_rabble_faction = {
	casus_belli = roused_rabble_war

	short_effect_desc = roused_rabble_faction_short_effect_desc

	sort_order = 5

	leaders_allowed_to_leave = no
	player_can_join = no
	power_threshold = 0

	discontent_progress = {
		base = 25
	}

	name = FACTION_ROUSED_RABBLE_NAME

	requires_county = yes
	requires_character = no

	is_valid = {
		always = yes
	}

	is_character_valid = {
		has_valid_faction_members_trigger = yes
	}

	is_county_valid = {

	}

	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
			}
		}
		# Tell to the schemer war will start
		scope:faction.faction_target = {
			primary_title = {
				var:roused_rabble_schemer = {
					trigger_event = sas_faction_demand.1004
				}
			}
		}
		scope:faction.faction_target = {
			trigger_event = sas_faction_demand.1001
		}
	}

	ai_demand_chance = {
		base = 0

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

	on_creation = {
		save_scope_as = new_faction
		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
		}

		# Talk to the schemer first, potentially get funding
		scope:new_faction.faction_target = {
			primary_title = {
				var:roused_rabble_schemer = {
					trigger_event = sas_faction_demand.1003
				}
			}
		}
	}

	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.
					if = {
						limit = { NOT = { has_character_flag = peasant_revolt_do_not_kill } }
						trigger_event = { 
							id = sas_faction_demand.1002
							days = 1
						}
					}
				}
			}
		}
	}

	county_join_score =  {	
		base = 0

		modifier = {
			desc = "FACTION_REASON_RABBLE_ROUSED"
			add = 100
			has_county_modifier = county_rabble_roused_modifier
		}
		
		compare_modifier = {
			desc = "FACTION_REASON_COUNTY_OPINION"
			value = county_opinion
			multiplier = -1.0
		}

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

	county_create_score = {
		base = 100
		modifier = {
			add = -1000
			scope:target = {
				any_targeting_faction = {
					faction_is_type = roused_rabble_faction
				}
			}
		}
	}

	ai_join_score = {
	}

	can_character_join = {
		joined_faction = scope:faction
	}

	can_county_join = {

	}

	can_county_create = {
		title_is_a_faction_member = no
		has_county_modifier = county_rabble_roused_modifier
		NOT = { 
			exists = faction_war
			scope:target = {
			any_targeting_faction = {
				faction_is_type = roused_rabble_faction
			}
		}
	 	}
	}

	can_character_become_leader = {
		always = yes
	}

	on_war_start = {
		# Duchy initialization so we can concentrate the rebels in just a few important counties instead of spreading them over the entire realm.
		every_faction_county_member = {
			duchy = {
				add_to_list = areas_of_rebellion
			}
		}

		# Check where in the duchy the rebellion troops should spawn, then spawn them.
		every_in_list = {
			list = areas_of_rebellion
			save_temporary_scope_as = this_duchy

			# Determine where in the duchy those troops should spawn.
			ordered_in_de_jure_hierarchy = {
				# Troops can only spawn in counties that belong to our faction!
				limit = {
					tier = tier_county
					any_title_joined_faction = {
						this = root
					}
				}

				# Sort counties by how many levies they provide; rebels will spawn in the strongest county.
				order_by = total_county_levies

				title_province = {
					save_temporary_scope_as = local_center_of_rebellion
				}
			}
			# Spawn the faction troops in the designated location.
			root.faction_leader = {
				spawn_roused_rabble_troops = yes
			}
			
			# Give the populist leader a commander for each stack of troops.
			create_character = {
				template = new_commander_character
				location = scope:local_center_of_rebellion
				faith = root.faction_leader.faith
				culture = root.faction_leader.culture
				save_scope_as = new_populist_commander
				gender_female_chance = {
					if = {
						limit = { root.faction_leader.culture = { has_cultural_parameter = martial_custom_male_only_combatant } }
						add = 0
					}
					else_if = {
						limit = { root.faction_leader.culture = { has_cultural_parameter = martial_custom_female_only_combatant } }
						add = 100
					}
					else = {
						add = 50
					}
				}
			}
			scope:new_populist_commander = {
				set_employer = root.faction_leader
			}
		}

		# Give the populist leader a small purse so they don't immediately go bankrupt with army maintenance.
		every_faction_county_member = {
			root.faction_leader = {
				add_gold = 50
			}
		}
	}

	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 = roused_county_levies_to_raise
		if = {
			limit = {
				scope:faction.faction_target = { ep3_restored_rome_hard_mode_trigger = yes }
			}
			multiply = 2
		}
	}

	character_allow_create = no

	special_character_title = "FACTIONS_WINDOW_LEADER"

	inherit_membership = no

	county_can_switch_to_other_faction = yes
}
