﻿nomadic_faction = {
	casus_belli = nomadic_war

	leaders_allowed_to_leave = no
	player_can_join = no

	sort_order = 3

	name = FACTION_NOMAD_DYNAMIC_NAME

	short_effect_desc = nomadic_faction_short_effect_desc

	discontent_progress = {
		base = 0

		modifier = {
			add = 5
			desc = "BASE_COLON"
		}
	}

	power_threshold = 0

	requires_county = yes
	requires_character = no

	is_valid = {
		always = yes
	}

	is_county_valid = {
		# Counties can only be in a faction against the top liege in their realm (who is inherently independent).
		scope:faction.faction_target = holder.top_liege
		holder.top_liege = {
			NOR = {
				has_government = herder_government
				any_owned_story = {
					OR = {
						story_type = story_greatest_of_khans
						story_type = story_mongol_invasion
					}
				}
			}
		}
		
		title_province = {
			OR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}
	}

	can_character_join = {
		joined_faction = scope:faction
	}

	can_county_join = {
		title_province = {
			OR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}
	}

	can_county_create = {
		county_opinion < 0

		# Can only join factions against our top liege (not direct liege)
		scope:target = holder.top_liege
		holder.top_liege = {
			NOR = {
				has_government = herder_government
				any_owned_story = {
					OR = {
						story_type = story_greatest_of_khans
						story_type = story_mongol_invasion
					}
				}
			}
		}
		
		title_province = {
			OR = {
				has_holding_type = nomad_holding
				has_holding_type = herder_holding
			}
		}

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

	can_character_become_leader = {
		always = yes
	}

	county_create_score = {
		base = -5
		
		compare_modifier = { # Non-nomads get it at -15 county opinion
			trigger = {
				scope:target = {
					NOT = { government_has_flag = government_is_nomadic }
				}
			}
			value = county_opinion
			multiplier = -2
			
			desc = "FACTION_REASON_COUNTY_OPINION"
		}
		
		compare_modifier = { # Nomads get it at roughly -35 county opinion
			trigger = {
				scope:target = {
					government_has_flag = government_is_nomadic
				}
			}
			value = county_opinion
			multiplier = -1
			
			desc = "FACTION_REASON_COUNTY_OPINION"
		}

		# If the targeted ruler isn't nomadic... time to rumble
		modifier = {
			add = 20
			scope:target = {
				NOR = {
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_herder
				}
			}
			desc = "FACTION_REASON_DISLIKE_NON_NOMADS"
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:target
			SCORE_PER_TRAIT = 10
		}

		# Realm Stability Settings
		modifier = {
			add = 25
			has_game_rule = lesser_realm_stability
		}
		modifier = {
			add = -25
			has_game_rule = higher_realm_stability
		}
		modifier = {
			add = -50
			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 = -25
			has_game_rule = higher_realm_stability_ai_only
			scope:target = {
				is_ai = yes
			}
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -50
			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 }
		}

		# 10-year 'grace period' from nomadic factions forming after game start
		modifier = {
			years_from_game_start <= 10
			factor = {
				value = 0
				if = {
					limit = { years_from_game_start >= 5 }
					add = years_from_game_start
					subtract = 5
					divide = 5 # In year 6 factions can begin forming, but at 20% weighting. This increases to 40% in year 7, 60% in year 8, etc., up to 100% in year 10 where things are then normal.
				}
			}
		}
	}

	county_join_score =  {
		base = 0

		compare_modifier = { # Non-nomads get it at -10 county opinion
			trigger = {
				scope:faction.faction_target = {
					NOT = { government_has_flag = government_is_nomadic }
				}
			}
			value = county_opinion
			multiplier = -2
			
			desc = "FACTION_REASON_COUNTY_OPINION"
		}
		
		compare_modifier = { # Nomads get it at roughly -30 county opinion
			trigger = {
				scope:faction.faction_target = {
					government_has_flag = government_is_nomadic
				}
			}
			value = county_opinion
			multiplier = -1
			
			desc = "FACTION_REASON_COUNTY_OPINION"
		}

		# If the targeted ruler isn't nomadic... time to rumble
		modifier = {
			add = 10
			scope:faction.faction_target = {
				NOR = {
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_herder
				}
			}
			desc = "FACTION_REASON_DISLIKE_NON_NOMADS"
		}

		legalism_virtue_and_sin_modifier = {
			TARGET = scope:faction.faction_target
			SCORE_PER_TRAIT = 10
		}

		# 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 = -25
			has_game_rule = higher_realm_stability
		}
		modifier = {
			desc = "FACTION_REASON_GAME_RULE_REALM_STABILITY_EXTREME"
			add = -50
			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 = -25
			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 = -50
			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 }
		}
	}

	county_power = county_horde_riders_to_raise_power

	ai_join_score = {
	}

	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 = {
				save_temporary_scope_as = destroyer_of_titles
				if = {
					limit = {
						has_variable = peasant_title
						exists = this.var:peasant_title.holder
					}
					destroy_title = this.var:peasant_title
				}
				if = {
					limit = {
						NOT = {
							has_character_flag = successful_nomad_revolt_leader
						}
					}
					every_held_title = {
						limit = {
							is_landless_type_title = yes
						}
						scope:destroyer_of_titles = { destroy_title = prev }
					}
					if = {
						limit = { # To make sure we aren't in debt
							gold > 0
						}
						remove_long_term_gold = gold # Zero out our wallet since the revolt is over and we're going back to being unlanded.
					}
				}
				if = {
					limit = {
						is_alive = yes # To prevent dead people from being checked for variables
					}
					if = {
						limit = {
							has_variable = rebel_leader_peasants
						}
						remove_variable = rebel_leader_peasants
					}
					remove_variable = peasant_title
					remove_character_flag = peasant_faction_random_peasant
					remove_character_flag = peasant_faction_claimant_without_title
					if = {
						limit = {
							has_character_flag = peasant_faction_random_peasant
							NOT = {	has_character_flag = peasant_revolt_do_not_kill }
						}
						death = {
							death_reason = death_vanished
						}
					}
				}
			}
		}
	}

	demand = {
		setup_nomadic_leader_effect = yes
		# Faction leader is unhappy if they reach the point of needing to issue an ultimatim.
		faction_leader = {
			add_opinion = {
				modifier = angry_opinion
				target = root.faction_target
				opinion = -50
			}
		}

		# Save scopes that will be used in the demand event.
		save_scope_as = faction
		special_character = {
			save_scope_as = peasant_leader
		}
		
		# Check for relevant vassals that owns any land targeted by the faction.
		save_temporary_scope_as = county_faction
		every_faction_county_member = {
			limit = {
				holder = {
					NOT = { this = scope:faction.faction_target }
					NOT = { is_in_list = nomadic_faction_vassal_targets }
				}
			}
			holder = {
				add_to_list = nomadic_faction_vassal_targets
			}
		}
		every_faction_county_member = {
			holder = {
				every_liege_or_above = {
					limit = {
						NOT = { this = scope:faction.faction_target }
						NOT = { is_in_list = nomadic_faction_vassal_targets }
					}
					add_to_list = nomadic_faction_vassal_targets
				}
			}
		}
		
		# Check if there is a player affected by the faction. AI rulers are blocked from accepting the faction demand if so.
		if = {
			limit = {
				any_in_list = {
					list = nomadic_faction_vassal_targets
					is_ai = no
				}
			}
			scope:faction = {
				set_variable = {
					name = faction_targets_player
					value = yes
				}
			}
		}
		
		# Fire the demand event. A follow up event will be sent to affected vassals.
		scope:faction.faction_target = {
			trigger_event = faction_demand.9998
		}
	}

	ai_demand_chance = {
		base = 100

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

	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 = {
				if = {
					limit = {
						NOT = {
							is_in_list = areas_of_rebellion
						}
					}
					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
					}
				}

				order_by = county_fertility

				title_province = {
					save_temporary_scope_as = local_center_of_rebellion
				}
			}

			# Spawn the faction troops in the designated location.
			root.faction_leader = {
				spawn_nomadic_revolt_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
				add_trait = nomadic_philosophy
			}
		}

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

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

	character_allow_create = no

	special_character_title = "FACTIONS_WINDOW_LEADER"

	inherit_membership = no

	multiple_targeting = no
}

