﻿### EK NOTE: Removed references to vanilla title

# Faction demand events

namespace = faction_demand

##################################################
# GENERAL SCRIPTED TRIGGERS & EFFECTS

scripted_trigger faction_leader_would_consider_turning_down_co_emperorship_trigger = {
	OR = {
		is_ai = no
		# Won't change their mind.
		AND = {
			has_trait = stubborn
			NOR = {
				has_trait = arrogant
				has_trait = ambitious
			}
		}
		# Eyes on the prize.
		AND = {
			has_trait = humble
			NOT = { has_trait = ambitious }
		}
		# Not swayed by high office.
		AND = {
			has_trait = content
			NOT = { has_trait = arrogant }
		}
	}
}

scripted_trigger faction_target_can_attempt_to_bribe_with_co_emperorship_trigger = {
	may_appoint_co_emperors_trigger = yes
	has_active_diarchy = no
}

scripted_effect faction_demand_counter_offer_co_rule_start_diarchy_effect = {
	custom_tooltip = {
		text = faction_demand.0000.enter_co_rule_avoiding_worst_effects
		try_start_diarchy = co_emperorship
		set_diarch = scope:faction_leader
		change_diarchy_swing = 30
		reverse_add_opinion = {
			target = scope:faction_leader
			modifier = respect_opinion
			opinion = 50
		}
	}
}










##################################################
# EVENTS

##################################################
# Independence Faction
# by Joakim Andreasson & Ismael Serrano
# 0001 - 0010
##################################################

scripted_effect faction_demand_independence_counter_offer_corule_accepted_effect = {
	add_dread = medium_dread_loss
	# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
	faction_accept_demand_legitimacy_effect = yes
	custom_tooltip = {
		text = faction_demand.0000.truce_from_co_rule
		scope:faction = {
			every_faction_member = {
				save_scope_as = recipient # Ensuring it localizes correctly
				add_truce_both_ways = {
					character = scope:faction.faction_target
					years = faction_counter_offer_appointed_co_emperor_value
					name = ep3_truce_appointed_faction_leader_coruler
				}
			}
		}
	}
	add_prestige = {
		value = medium_prestige_value
		multiply = -1
	}
	scope:faction = { destroy_faction = yes }
}

scripted_effect faction_demand_independence_rejected_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_rejected }

	show_as_tooltip = {
		scope:faction = {
			faction_start_war = {}
			custom_tooltip = faction_starts_war.tt.replacement
		}
	}
	
	if = {
		limit = {
			scope:faction = {
				faction_is_type = independence_faction
			}
		}
		scope:faction = {
			every_faction_member = {
				save_scope_as = char_temp
				add_clan_unity_interaction_effect = {
					CHARACTER = scope:char_temp
					TARGET = root
					VALUE = medium_unity_loss
					DESC = clan_unity_joined_independence_faction.desc
					REVERSE_NON_HOUSE_TARGET = no
				}
			}
		}
	}
}

# Independence Faction Demand
# by Joakim Andreasson
faction_demand.0001 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_DESC"
	
	trigger = {
		# May have ceased to exist by the time this triggers
		exists = scope:faction
	}

	immediate = {
		# Grab our scope for loc.
		save_scope_as = faction_target
	}

	# Accept.
	option = {
		name = "FACTION_DEMAND_ACCEPT"

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }

		root = {
			save_scope_as = faction_target
			add_dread = medium_dread_loss
			# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
			faction_accept_demand_legitimacy_effect = yes
		}
		
		create_title_and_vassal_change = {
			type = independency
			save_scope_as = change
			add_claim_on_loss = no
		}

		scope:faction = {
			every_faction_member = {
				change_liege_or_become_independent = {
					CHANGE = scope:change
					VASSAL = this
				}
				
				hidden_effect = {
					save_scope_as = recipient # Ensuring it localizes correctly
					add_truce_both_ways = {
						character = root
						days = 1825
						name = TRUCE_GRANT_INDEPENDENCE
					}

					# Struggle Catalysts.
					if = {
						limit = {
							is_important_or_vip_struggle_character = yes
							any_character_struggle = {
								involvement = involved
								activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
									CATALYST = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
									CHAR = scope:recipient
								}
							}
						}
						every_character_struggle = {
							involvement = involved
							limit = {
								activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
									CATALYST = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
									CHAR = scope:recipient
								}
							}
							activate_struggle_catalyst = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
						}
					}
				}

				trigger_event = faction_demand.0002
			}
		}

		resolve_title_and_vassal_change = scope:change

		add_prestige = {
			value = medium_prestige_value
			multiply = -1
		}
	
		scope:faction_leader = { ek_remember_recent_conquest_victory_effect = yes } #EK EDIT

		ai_chance = {
			base = 1
			modifier = {
				add = 99
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = 25
			}
			modifier = {
				add = -50
				is_at_war = no
				primary_title.tier >= tier_kingdom
			}
			modifier = {
				add = -150
				is_at_war = no
				primary_title.tier >= tier_empire
			}
			### EK TODO: Maybe add our own?
			modifier = {
				add = -5000
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
		}
	}

	# Attempt to bribe.
	option = {
		name = "FACTION_DEMAND_BRIBE_CO_EMPEROR"
		show_as_unavailable = { has_active_diarchy = yes }
		trigger = { faction_target_can_attempt_to_bribe_with_co_emperorship_trigger = yes }

		# Ping-pong the choice back to the target.
		## Laying out the consequences.
		show_as_tooltip = {
			random_list = {
				# They accept!
				100 = {
					desc = faction_demand.0000.faction_leader_accepts
					show_chance = no
					faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
					faction_demand_independence_counter_offer_corule_accepted_effect = yes
				}
				# They decline.
				100 = {
					desc = faction_demand.0000.faction_leader_rejects
					show_chance = no
					faction_demand_independence_rejected_effect = yes
				}
			}
		}
		## And sorting the actual flow.
		scope:faction_leader = { trigger_event = faction_demand.0006 }

		ai_chance = { liege_offer_co_emperorship_to_faction_leader_modifier = yes }
	}

	# Reject.
	option = {
		name = "FACTION_DEMAND_REFUSE"

		faction_demand_independence_rejected_effect = yes
		scope:faction_leader = { trigger_event = faction_demand.0003 }

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 4.0
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
			compare_modifier = {
				value = debt_level
				multiplier = -25
			}
			### EK TODO: maybe add our own?
			modifier = {
				add = 5000
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
		}
	}
}

# Independence Faction Demand Accepted
# by Ismael Serrano
faction_demand.0002 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE_ACCEPTED"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_ACCEPTED_DESC"

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
		custom_tooltip = faction_demand.0002.tt
	}
}

# Independence Faction Demand Refused
# by Joakim Andreasson
faction_demand.0003 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			faction_start_war = {}
			
			every_faction_member = {
				limit = { NOT = { this = scope:faction.faction_leader } }
				trigger_event = faction_demand.0004
			}
		}
	}
}

# Independence Faction Demand Refused Member Notice
# by Joakim Andreasson
faction_demand.0004 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"	
	}
}

# Independence Faction Demand Send Notice
# by Joakim Andreasson
faction_demand.0005 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE_SEND_DEMAND_NOTIFICATION"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_SEND_DEMAND_NOTIFICATION_DESC"

	option = {
		name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"	
	}
}

# Co-emperor counter-offer.
faction_demand.0006 = {
	type = letter_event
	sender = scope:faction_target
	opening = FACTION_DEMAND_COUNTER_OFFER_CORULE
	desc = FACTION_DEMAND_COUNTER_OFFER_CORULE_DESC
	
	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_ACCEPT

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_start_diarchy
					title = faction_demand.0000.offer_accepted
					show_as_tooltip = { custom_tooltip = faction_demand.0000.enter_co_emperorship }
				}
			}
			scope:faction = {
				every_faction_member = {
					limit = {
						is_ai = no
						NOT = { this = scope:faction_leader }
					}
					trigger_event = faction_demand.9011
				}
			}
		}
		# Actual effects.
		scope:faction_target = {
			faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
			faction_demand_independence_counter_offer_corule_accepted_effect = yes
		}

		# Low weight: we control the AI accepting this by toggling whether they can access the refusal option at all.
		ai_chance = { base = 20 }
	}

	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_REJECT
		trigger = { faction_leader_would_consider_turning_down_co_emperorship_trigger = yes }

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_war_declared_on_me
					title = faction_demand.0000.offer_rejected
					show_as_tooltip = {
						scope:faction = { custom_tooltip = faction_starts_war.tt.replacement }
					}
				}
			}
		}
		# Actual effects.
		faction_demand_independence_rejected_effect = yes

		# If the AI can access this at all then they should strongly consider it.
		ai_chance = { base = 80 }
	}
}

##################################################
# Liberty Faction
# by Henrik Fåhraeus
# 0101 - 0110
##################################################

scripted_effect faction_demand_liberty_counter_offer_corule_accepted_effect = {
	add_dread = medium_dread_loss
	# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
	faction_accept_demand_legitimacy_effect = yes
	add_prestige = -200
	add_character_flag = {
		flag = recent_liberty_faction_war
		years = 10
	}
	custom_tooltip = {
		text = faction_demand.0000.truce_from_co_rule
		scope:faction = {
			every_faction_member = {
				save_scope_as = recipient # Ensuring it localizes correctly
				add_truce_both_ways = {
					character = scope:faction.faction_target
					years = faction_counter_offer_appointed_co_emperor_value
					name = ep3_truce_appointed_faction_leader_coruler
				}
			}
		}
	}
	scope:faction = { destroy_faction = yes }
	# This effect should always be placed here, _after_ everything except the ai_chance.
	trigger_event = { on_action = on_faction_demand_accepted }
}

scripted_effect faction_demand_liberty_rejected_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_rejected }

	show_as_tooltip = {
		scope:faction = {
			faction_start_war = {}
			custom_tooltip = faction_starts_war.tt.replacement
		}
	}
}

# Liberty Faction Demand
# by Henrik Fåhraeus
faction_demand.0101 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_LIBERTY"
	}
	desc = "FACTION_DEMAND_LIBERTY_DESC"
	
	trigger = {
		# May have ceased to exist by the time this triggers
		exists = scope:faction
	}

	immediate = {
		# Grab our scope for loc.
		save_scope_as = faction_target
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }

		add_dread = medium_dread_loss

		# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
		faction_accept_demand_legitimacy_effect = yes
		
		switch = {
			trigger = has_realm_law
			imperial_bureaucracy_1 = { add_realm_law = imperial_bureaucracy_0 }
			imperial_bureaucracy_2 = { add_realm_law = imperial_bureaucracy_1 }
			imperial_bureaucracy_3 = { add_realm_law = imperial_bureaucracy_2 }
			tribal_authority_1 = { add_realm_law = tribal_authority_0 }
			tribal_authority_2 = { add_realm_law = tribal_authority_1 }
			tribal_authority_3 = { add_realm_law = tribal_authority_2 }
			crown_authority_1 = { add_realm_law = crown_authority_0 }
			crown_authority_2 = { add_realm_law = crown_authority_1 }
			crown_authority_3 = { add_realm_law = crown_authority_2 }
		}

		add_prestige = -200

		add_character_flag = {
			flag = recent_liberty_faction_war
			years = 10
		}

		scope:faction_leader = {
			trigger_event = {
				id = faction_demand.0102
			}
		}

		scope:faction = {
			destroy_faction = yes
		}

		# This effect should always be placed here, _after_ everything except the ai_chance.
		trigger_event = { on_action = on_faction_demand_accepted }

		ai_chance = {
			base = 10
			modifier = {
				add = 90
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				add = 50
				any_character_war = {
					is_war_leader = root
					is_defender = root
				}
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_BRIBE_CO_EMPEROR"
		show_as_unavailable = { has_active_diarchy = yes }
		trigger = { faction_target_can_attempt_to_bribe_with_co_emperorship_trigger = yes }

		# Ping-pong the choice back to the target.
		## Laying out the consequences.
		show_as_tooltip = {
			random_list = {
				# They accept!
				100 = {
					desc = faction_demand.0000.faction_leader_accepts
					show_chance = no
					faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
					faction_demand_liberty_counter_offer_corule_accepted_effect = yes
				}
				# They decline.
				100 = {
					desc = faction_demand.0000.faction_leader_rejects
					show_chance = no
					faction_demand_liberty_rejected_effect = yes
				}
			}
		}
		## And sorting the actual flow.
		scope:faction_leader = { trigger_event = faction_demand.0106 }

		ai_chance = { liege_offer_co_emperorship_to_faction_leader_modifier = yes }
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"

		faction_demand_liberty_rejected_effect = yes

		scope:faction_leader = {
			trigger_event = {
				id = faction_demand.0103
				days = 1
			}
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 4.0
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
		}
	}
}

# Liberty Faction Demand Accepted
# by Henrik Fåhraeus
faction_demand.0102 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_LIBERTY_ACCEPTED"
	}
	desc = "FACTION_DEMAND_LIBERTY_ACCEPTED_DESC"

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
		custom_tooltip = faction_demand.0102.tt
	}
}

# Liberty Faction Demand Refused
# by Henrik Fåhraeus
faction_demand.0103 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_LIBERTY_REFUSED"
	}
	desc = "FACTION_DEMAND_LIBERTY_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			faction_start_war = {}
			
			every_faction_member = {
				limit = { NOT = { this = scope:faction.faction_leader } }
				trigger_event = faction_demand.0104
			}
		}
	}
}

# Liberty Faction Demand Refused Member Notice
# by Henrik Fåhraeus
faction_demand.0104 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_LIBERTY_REFUSED"
	}
	desc = "FACTION_DEMAND_LIBERTY_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"	
	}
}

# Liberty Faction Demand Send Notice
# by Henrik Fåhraeus
faction_demand.0105 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_LIBERTY_SEND_DEMAND_NOTIFICATION"
	}
	desc = "FACTION_DEMAND_LIBERTY_SEND_DEMAND_NOTIFICATION_DESC"

	option = {
		name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"	
	}
}

# Co-emperor counter-offer.
faction_demand.0106 = {
	type = letter_event
	sender = scope:faction_target
	opening = FACTION_DEMAND_COUNTER_OFFER_CORULE
	desc = FACTION_DEMAND_COUNTER_OFFER_CORULE_DESC
	
	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_ACCEPT

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_start_diarchy
					title = faction_demand.0000.offer_accepted
					show_as_tooltip = { custom_tooltip = faction_demand.0000.enter_co_emperorship }
				}
			}
			scope:faction = {
				every_faction_member = {
					limit = {
						is_ai = no
						NOT = { this = scope:faction_leader }
					}
					trigger_event = faction_demand.9011
				}
			}
		}
		# Actual effects.
		scope:faction_target = {
			faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
			faction_demand_liberty_counter_offer_corule_accepted_effect = yes
		}

		# Low weight: we control the AI accepting this by toggling whether they can access the refusal option at all.
		ai_chance = { base = 20 }
	}

	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_REJECT
		trigger = { faction_leader_would_consider_turning_down_co_emperorship_trigger = yes }

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_war_declared_on_me
					title = faction_demand.0000.offer_rejected
					show_as_tooltip = {
						scope:faction = { custom_tooltip = faction_starts_war.tt.replacement }
					}
				}
			}
		}
		# Actual effects.
		faction_demand_liberty_rejected_effect = yes

		# If the AI can access this at all then they should strongly consider it.
		ai_chance = { base = 80 }
	}
}

##################################################
# Populist Faction
# by Henrik Fåhraeus and Sean Hughes
# 1001, 1010 (but not the range between them), & 0099 too for some inexplicable reason
##################################################

# Populist Faction Demand
# by Henrik Fåhraeus and Sean Hughes
faction_demand.1001 = {
	type = letter_event
	sender = scope:peasant_leader
	opening = {
		desc = "FACTION_DEMAND_POPULIST"
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					government_allows = state_faith
					primary_title.state_faith != scope:peasant_leader.faith
				}
				desc = FACTION_DEMAND_POPULIST_DESC_ADMIN
			}
			triggered_desc = {
				trigger = {
					NOT = { culture = scope:peasant_leader.culture }
					faith = scope:peasant_leader.faith
				}
				desc = "FACTION_DEMAND_POPULIST_DESC_CULTURE"
			}
			triggered_desc = {
				trigger = {
					NOT = { faith = scope:peasant_leader.faith }
					culture = scope:peasant_leader.culture
				}
				desc = "FACTION_DEMAND_POPULIST_DESC_FAITH"
			}
			desc = "FACTION_DEMAND_POPULIST_DESC"
		}
	}

	trigger = {
		exists = scope:faction
		exists = scope:peasant_leader
		scope:peasant_leader.joined_faction = scope:faction
	}

	on_trigger_fail = {
		debug_log = "Popular Faction tried to press claim (probably) without existing peasant_leader"
		debug_log_scopes = yes
		debug_log = "Popular Faction error reporting complete"
	}

	immediate = {
		every_realm_province = {
			county = { add_to_list = remaining_counties_list }
		}
		scope:faction = {
			every_faction_county_member = { remove_from_list = remaining_counties_list }
		}
	}

	# Option A: Convert to the faction's faith or culture.
	option = {
		name = "FACTION_DEMAND_POPULIST_ACCEPT_CONVERT"

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }

		trigger = {
			# If more than 30% of your realm belongs to the revolting culture or faith, you get the option to switch to it.
			OR = {
				culture = scope:peasant_leader.culture
				any_realm_province = {
					percent >= 0.30
					culture = scope:peasant_leader.culture
				}
			}
			OR = {
				faith = scope:peasant_leader.faith
				any_realm_province = {
					percent >= 0.30
					faith = scope:peasant_leader.faith
				}
			}
			
			# Heads of Faith can never convert.
			NOT = { faith.religious_head = root }

			# Option unavailable to realms with State Faith
			NOT = { government_allows = state_faith }
			
			# EK ADDITION: some restrictions must apply in EK to avoid goblin culture Altmer and Argonian culture Cyrodiilics
			culture = { has_same_culture_heritage = scope:peasant_leader.culture }
			# EK ADDITION END


			###Restrictions to stop the AI converting quite so freely
			trigger_if = {
				limit = {
					is_ai = yes
				}
				
				# May not convert to an unreformed faith from a reformed one.
				NAND = {
					scope:peasant_leader.faith = { has_doctrine_parameter = unreformed }
					faith = {
						NOT = { has_doctrine_parameter = unreformed }
					}
				}

				# Must be converting from a low-fervor faith to a higher-fervor faith.
				scope:peasant_leader.faith = { fervor >= 31 }
				faith = { fervor <= 30 }
				
				# Zealous rulers do not get the option to change faith.
				OR = {
					NOT = { has_trait = zealous }
					faith = scope:peasant_leader.faith
				}

				# Crusaders & Crusader-Monarchs should never accept this.
				NOR = {
					has_trait = faith_warrior
					has_trait = crusader_king
					# has_title = title:k_jerusalem #EK DISABLED: Jerusalem still doesn't exist
				}
			}
			###End AI restrictions.
		}

		if = {
			limit = {
				NOT = { culture = scope:peasant_leader.culture }
			}
			set_culture = scope:peasant_leader.culture
			add_prestige_level = -2
		}
		else = {
			add_prestige_level = -1
		}

		if = {
			limit = {
				NOT = { faith = scope:peasant_leader.faith }
			}
			set_character_faith = scope:peasant_leader.faith
			add_piety_level = -1
		}
		
		if = {
			limit = {
				dread > 0
			}
			add_dread = medium_dread_loss
		}

		# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
		faction_accept_demand_legitimacy_effect = yes

		# Struggle Catalysts.
		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:faction_leader
					}
				}
			}
			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:faction_leader
					}
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
					character = root
				}
			}
		}
		
		scope:faction = {
			hidden_effect = {
				clean_revolt_county_modifiers_from_faction_members_effect = yes
			}
			destroy_faction = yes # Show this to have it explicitly said in the tooltip that the faction actually disbands.
		}
		
		stress_impact = {
			zealous = massive_stress_gain
		}
		
		ai_chance = {
			base = 50
			ai_value_modifier = {
				trigger = {
					NOT = { faith = scope:peasant_leader.faith }
				}
				ai_zeal = -1
				min = 0
			}

			compare_modifier = { # Weaker factions increase the chance of an AI ruler refusing; at 80% army strength (minimum), this will be -20; at 200% army strength, this will max out at +50.
				value = {			
					value = scope:faction.faction_power
					subtract = 100
				}
				max = 50
			}
			### EK TODO: maybe add our own?
			modifier = {
				add = 50
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
		}
	}

	# Option D: Switch State Faith to the faction faith
	# (out of order so it appears correctly in the event. this option has been added subsequently)
	option = {
		name = FACTION_DEMAND_POPULIST_ACCEPT_CONVERT_STATE_FAITH

		trigger = { # Only available if ruler fulfills the following:
			government_allows = state_faith
			primary_title.state_faith != scope:peasant_leader.faith
			OR = { # You share religion OR faction leader's faith is the largest faith in the realm - This is to avoid flipping to a faith that doesn't make sense
				religion = scope:peasant_leader.religion
				any_realm_county = {
					percent >= 0.6
					faith = scope:peasant_leader.faith
				}
			}
			culture = scope:peasant_leader.culture
		}

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }

		primary_title = { set_state_faith = scope:peasant_leader.faith }
		if = {
			limit = { government_has_flag = government_has_influence }
			change_influence = monumental_influence_loss
		}
		else = {
			add_prestige_level = -1
		}
		add_piety_level = -1

		ai_chance = {
			base = 50
			ai_value_modifier = {
				trigger = {
					NOT = { faith = scope:peasant_leader.faith }
				}
				ai_zeal = -1
				min = 0
			}

			compare_modifier = { # Weaker factions increase the chance of an AI ruler refusing; at 80% army strength (minimum), this will be -20; at 200% army strength, this will max out at +50.
				value = {			
					value = scope:faction.faction_power
					subtract = 100
				}
				max = 50
			}
			#EK EDIT: no mongols
			modifier = {
				add = 50
				OR = {
					#has_title = title:e_mongol_empire
					#has_title = title:e_ilkhanate
					#has_title = title:e_golden_horde
					#has_title = title:e_chagatai
					#has_title = title:e_black_horde
					#has_title = title:e_white_horde
					#has_title = title:e_aarlud_khanate
					#has_title = title:e_togskol_khanate
					#has_title = title:e_baruun_khanate
					#has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
			
			modifier = { # Don't agree to change the state faith to another religion
				factor = 0
				NOT = { faith.religion = scope:peasant_leader.faith.religion }
			}
			#EK DISABLED: no Byzantium
			#modifier = { # Byzantium should try to avoid becoming Catholic
			#	factor = 0
			#	primary_title = title:e_byzantium
			#	primary_title.state_faith.religion = scope:peasant_leader.faith.religion
			#	scope:peasant_leader.faith = faith:catholic
			#}
		}
	}

	# Option B: Grant the faction independence.
	option = {
		name = "FACTION_DEMAND_POPULIST_ACCEPT_GRANT_INDEPENDENCE"
		trigger = { # AI should not give the faction independence if a player is targeted by the faction.
			NAND = {
				scope:faction = {
					has_variable = faction_targets_player
				}
				is_ai = yes
			}
			# Only allow this option for governments with state faith it's a cultural populist faction
			trigger_if = {
				limit = { government_allows = administrative }
				primary_title.state_faith = scope:peasant_leader.faith
				culture != scope:peasant_leader.culture
			}
		}

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }
		
		add_prestige_level = -1
		if = {
			limit = {
				dread > 0
			}
			add_dread = medium_dread_loss
		}

		successful_popular_revolt_outcome_effect = {
			FACTION_LEADER = scope:peasant_leader
			TARGET_TITLE = scope:target_title
			SOURCE_GOVERNMENT = root
		}

		# If the faction includes ALL of our counties, notify the player that it will leave them landless!
		if = {
			limit = {
				any_in_list = {
					list = remaining_counties_list
					count < 1
				}
			}
			custom_tooltip = FACTION_DEMAND_POPULIST_ACCEPT_GRANT_INDEPENCE_TT
		}

		# Struggle Catalysts.
		# Should rename the Catalysts?
		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:faction_leader
					}
				}
			}
			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:faction_leader
					}
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
					character = root
				}
			}
		}

		hidden_effect = {
			if = {
				limit = {
					exists = scope:faction
				}
				scope:faction = {
					clean_revolt_county_modifiers_from_faction_members_effect = yes
					destroy_faction = yes
				}
			}
		}

		ai_chance = {
			base = 0
			compare_modifier = { # Stronger factions increase the chance of an AI ruler capitulating; at 200% ruler army strength, this maxes out at +50% chance to accept demands.
				value = {
					value = scope:faction.faction_power
					subtract = 100
					multiply = 0.5
				}
				max = 50
			}
			compare_modifier = { # Craven rulers can get up to +37% chance to accept demands.
				value = ai_boldness
				multiplier = -0.5
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = 25
			}
			modifier = {
				add = -50
				is_at_war = no
				primary_title.tier >= tier_kingdom
			}
			modifier = {
				add = -150
				is_at_war = no
				primary_title.tier >= tier_empire
			}
			
			### EK TODO: Maybe add our own?
			modifier = {
				add = -5000
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
					government_allows = state_faith
				}
			}
		}
	}

	# Option C: Refuse the demand and start a war.
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							government_allows = state_faith
							primary_title.state_faith != scope:peasant_leader.faith
						}
						desc = FACTION_DEMAND_POPULIST_REFUSE_ADMIN
					}
					desc = FACTION_DEMAND_POPULIST_REFUSE
				}
			}
		}

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_rejected }

		scope:faction = {
			faction_start_war = {
				title = scope:target_title
			}
		}
		
		custom_tooltip = FACTION_DEMAND_POPULIST_REFUSE_TT
		
		if = { # Send follow up event to affected vassals.
			limit = {
				list_size = {
					name = popular_faction_vassal_targets
					value > 0
				}
			}
			every_in_list = {
				list = popular_faction_vassal_targets
				trigger_event = faction_demand.0099
			}
		}

		ai_chance = {
			base = 100
			compare_modifier = { # Weaker factions increase the chance of an AI ruler refusing; at 80% army strength (minimum), this will be +20; at 200% army strength, this will max out at -50.
				value = {			
					value = scope:faction.faction_power
					subtract = 100
					multiply = -1
				}
				min = -50
			}
			compare_modifier = { # Brave rulers get up to a 50% bonus chance to refuse.
				value = ai_boldness
				multiplier = -0.5
				min = -50
			}
			modifier = {
				factor = 1.5
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = -25
			}
			
			### EK TODO: maybe add our own?
			modifier = {
				add = 5000
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
		}
	}
	
	after = {
		if = {
			limit = {
				exists = scope:faction
				scope:faction = {
					has_variable = faction_targets_player
				}
			}
			scope:faction = {
				remove_variable = faction_targets_player
			}
		}
	}
}

# Populist Faction Demand - Affected vassals choose their action
faction_demand.0099 = {
	type = letter_event
	sender = scope:peasant_leader
	opening = {
		desc = "FACTION_DEMAND_POPULIST"
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					culture = scope:peasant_leader.culture
					faith = scope:peasant_leader.faith
				}
				desc = "FACTION_DEMAND_POPULIST_VASSAL_DESC_SAME_CULTURE_AND_FAITH"
			}
			triggered_desc = {
				trigger = {
					NOT = { culture = scope:peasant_leader.culture }
					faith = scope:peasant_leader.faith
				}
				desc = "FACTION_DEMAND_POPULIST_VASSAL_DESC_CULTURE"
			}
			triggered_desc = {
				trigger = {
					NOT = { faith = scope:peasant_leader.faith }
					culture = scope:peasant_leader.culture
				}
				desc = "FACTION_DEMAND_POPULIST_VASSAL_DESC_FAITH"
			}
			desc = "FACTION_DEMAND_POPULIST_VASSAL_DESC"
		}
	}

	trigger = {
		exists = scope:faction
		exists = scope:peasant_leader
		scope:peasant_leader.joined_faction = scope:faction
	}
	
	immediate = {
		scope:faction.faction_war = {
			save_scope_as = populist_war
		}
	}
	
	# Option A: Convert to the faction's culture/faith and join the rebels.
	option = {
		name = {
			trigger = {
				culture = scope:peasant_leader.culture
				faith = scope:peasant_leader.faith
			}
			text = "FACTION_DEMAND_POPULIST_VASSAL_JOIN_REBELS"
		}
		name = {
			trigger = {
				NAND = {
					culture = scope:peasant_leader.culture
					faith = scope:peasant_leader.faith
				}
			}
			text = "FACTION_DEMAND_POPULIST_VASSAL_CONVERTS"
		}
		trigger = {
			# Have to be of the same faith as the faction, or the faith must have a large enough presence in the vassal's realm.
			OR = {
				faith = scope:peasant_leader.faith
				any_realm_province = {
					percent >= 0.20
					faith = scope:peasant_leader.faith
				}
			}
			
			# Heads of Faith can never convert.
			NOT = { faith.religious_head = root }
			
			# Restrictions to stop the AI converting quite so freely
			trigger_if = {
				limit = {
					is_ai = yes
				}
				
				# May not convert to an unreformed faith from a reformed one.
				NAND = {
					scope:peasant_leader.faith = { has_doctrine = unreformed_faith_doctrine }
					faith = {
						NOT = { has_doctrine = unreformed_faith_doctrine }
					}
				}
				
				# Must be converting from a low-fervor faith to a higher-fervor faith.
				scope:peasant_leader.faith = { fervor >= 31 }
				faith = { fervor <= 30 }
				
				# Zealous rulers do not get the option to change faith.
				OR = {
					NOT = { has_trait = zealous }
					faith = scope:peasant_leader.faith
				}
				
				# Crusaders & Crusader-Monarchs should never accept this.
				NOR = {
					has_trait = faith_warrior
					has_trait = crusader_king
					#has_title = title:k_jerusalem #EK DISABLED
				}
			}
			
			# You cannot join the attacking side if you already are at war with another participant.
			scope:faction.faction_war = {
				NOT = {
					any_war_participant = {
						is_at_war_with = root
					}
				}
			}
			
			# You cannot join the attacking side if it would put you against someone you are fighting another war with.
			scope:faction.faction_war = {
				NOT = {
					any_war_defender = {
						any_war_ally = {
							this = root
						}
					}
				}
			}
		}
		
		# Change culture/faith and apply penalties if needed.
		if = {
			limit = {
				NOT = { culture = scope:peasant_leader.culture }
			}
			set_culture = scope:peasant_leader.culture
			add_prestige_level = -1
		}
		if = {
			limit = {
				NOT = { faith = scope:peasant_leader.faith }
			}
			set_character_faith = scope:peasant_leader.faith
			add_piety_level = -1
		}
		
		# Join the war.
		scope:faction.faction_war = {
			add_attacker = root
		}
		custom_tooltip = FACTION_DEMAND_POPULIST_VASSAL_CONVERTS_TT
		
		stress_impact = {
			zealous = massive_stress_gain
		}
		
		ai_chance = {
			base = 0
			
			# Want to convert for cultural reasons
			modifier = {
				add = 10
				OR = {
					AND = {
						culture = scope:peasant_leader.culture
						NOT = { culture = scope:faction_target.culture }
					}
					AND = {
						any_realm_province = {
							percent >= 0.40
							culture = scope:peasant_leader.culture
						}
						NOT = { culture = scope:faction_target.culture }
					}
				}
			}
			modifier = {
				factor = 0
				culture = { has_cultural_tradition = tradition_staunch_traditionalists }
				NOT = { culture = scope:faction_target.culture }
			}
			
			# Want to convert for religious reasons
			modifier = {
				add = 20
				OR = {
					AND = {
						faith = scope:peasant_leader.faith
						NOT = { faith = scope:faction_target.faith }
					}
					AND = {
						any_realm_province = {
							percent >= 0.40
							faith = scope:peasant_leader.faith
						}
						NOT = { faith = scope:faction_target.faith }
					}
				}
			}
			modifier = {
				factor = 0
				culture = { has_cultural_tradition = tradition_zealous_people }
				NOT = { faith = scope:peasant_leader.faith }
			}
			modifier = {
				factor = 1.5
				culture = { has_cultural_tradition = tradition_religion_blending }
			}
			modifier = {
				factor = 2
				has_trait = zealous
				faith = scope:peasant_leader.faith
			}
			
			# General modifiers
			modifier = {
				factor = 1.25
				has_trait = fickle
			}
		}
	}
	
	# Option B: Fight alongside your liege.
	option = {
		name = FACTION_DEMAND_POPULIST_VASSAL_JOIN_WAR
		
		# You cannot join the defending side...
		trigger = {
			custom_tooltip = { # ... If you already are at war with another participant.
				text = FACTION_DEMAND_POPULIST_VASSAL_JOIN_WAR_BLOCK_TT
				scope:faction.faction_war = {
					NOT = {
						any_war_participant = {
							is_at_war_with = root
						}
					}
				}
			}
			custom_tooltip = { # ... If it would put you against someone you are fighting another war with.
				text = FACTION_DEMAND_POPULIST_VASSAL_JOIN_WAR_BLOCK_TT_2
				scope:faction.faction_war = {
					NOT = {
						any_war_attacker = {
							any_war_ally = {
								this = root
							}
						}
					}
				}
			}
		}
		show_as_unavailable = {
			OR = {
				scope:faction.faction_war = {
					any_war_participant = {
						is_at_war_with = root
					}
				}
				scope:faction.faction_war = {
					any_war_attacker = {
						any_war_ally = {
							this = root
						}
					}
				}
			}
		}
		
		# Join the war.
		scope:faction.faction_war = {
			add_defender = root
		}
		custom_tooltip = FACTION_DEMAND_POPULIST_VASSAL_JOIN_WAR_TT
		
		ai_chance = {
			base = 100
		}
	}
	
	# Option C: Do nothing.
	option = {
		name = FACTION_DEMAND_POPULIST_VASSAL_TAKE_NO_SIDE
		custom_tooltip = FACTION_DEMAND_POPULIST_VASSAL_TAKE_NO_SIDE_TT_A
		custom_tooltip = FACTION_DEMAND_POPULIST_VASSAL_TAKE_NO_SIDE_TT_B
		
		ai_chance = {
			base = 0
			
			modifier = {
				add = 20
				has_trait = craven
			}
		}
	}
}

# Spawn rebel reinforcements for popular revolts which successfully capture holdings.
faction_demand.1010 = {
	hidden = yes

	trigger = {
		has_variable = rebel_leader_peasants
		joined_faction = var:rebel_leader_peasants
		exists = scope:war
		scope:war = {
			primary_defender = scope:previous_controller
		}
		OR = {
			scope:county.culture = root.culture
			scope:county.religion = root.religion
		}
	}

	immediate = {
		# Determine how many rebel reinforcements we need to spawn, based on the total number of provinces captured.
		set_variable = {
			name = number_rebel_reinforcements
			value = {		
				add = {
					every_in_list = {
						list = occupied_baronies
						add = building_levies
					}
				}
				multiply = { # Only spawns between 25% and 50% of the total county's levies (otherwise could get too overwhelming).
					add = 0.25
					add = {
						scope:county = {
							add = development_level
							multiply = 0.0025
						}
					}	
				}
			}
		}

		# Spawn the army on the captured province.
		spawn_army = {
			name = event_troop_default_name
			levies = var:number_rebel_reinforcements
			location = scope:barony.title_province
			war = scope:war
		}

		# Additional loss of county control, can be up to -50 for large counties.
		every_in_list = {
			list = occupied_baronies
			scope:county = {
				change_county_control = -10
			}
		}
		
		remove_variable = number_rebel_reinforcements
	}
}

##################################################
# Peasant Faction
# by Sean Hughes
# 1101 - 1110
##################################################

faction_demand.1101 = {
	type = letter_event
	sender = scope:peasant_leader
	opening = "FACTION_DEMAND_PEASANT"
	desc = FACTION_DEMAND_PEASANT_DESC

	trigger = {
		exists = scope:faction
		exists = scope:peasant_leader
		scope:peasant_leader.joined_faction = scope:faction
	}

	on_trigger_fail = {
		debug_log = "Peasant Faction tried to press claim (probably) without existing peasant_leader"
		debug_log_scopes = yes
		debug_log = "Peasant Faction error reporting complete"
	}

	option = {
		name = "FACTION_DEMAND_PEASANT_ACCEPT"

		# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
		faction_accept_demand_legitimacy_effect = yes

		peasant_faction_demands_enforced = {FACTION = scope:faction}

		ai_chance = {
			base = 0
			ai_value_modifier = {
				ai_boldness = -4
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 110 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 120 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 130 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 140 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = -50
				scope:faction.faction_target = {
					is_at_war = no
					primary_title.tier >= tier_kingdom
				}
			}
			modifier = {
				add = -150
				scope:faction.faction_target = {
					is_at_war = no
					primary_title.tier >= tier_empire
				}
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_PEASANT_REFUSE"

		scope:faction = {
			faction_start_war = {}
			
			# This needs to run after 'faction_start_war' or the troops won't spawn correctly.
			faction_spawn_member_county_armies_effect = {
				FACTION = scope:faction
				ARMY_OWNER = scope:peasant_leader
				PEASANT_ARMY_NAME = peasant_faction_event_troops
			}	
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 4
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 90 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
		}
	}
}

faction_demand.1102 = {
	hidden = yes

	trigger = {
		is_imprisoned = no
	}

	immediate = {
		death = {
			death_reason = death_vanished
		}
	}
}

faction_demand.1110 = {
	hidden = yes

	trigger = {
		has_variable = rebel_leader_peasants
		joined_faction = var:rebel_leader_peasants
		exists = scope:war
		scope:war = {
			primary_defender = scope:previous_controller
		}
	}

	immediate = {
		# Determine how many rebel reinforcements we need to spawn, based on the total number of provinces captured.
		set_variable = {
			name = number_rebel_reinforcements
			value = {		
				add = {
					every_in_list = {
						list = occupied_baronies
						add = building_levies
					}
				}
				multiply = { # Spawns between 50% and 100% of the total county's levies
					add = 0.5
					add = {
						scope:county = {
							add = development_level
							multiply = 0.005
						}
					}	
				}
			}
		}

		# Spawn the army on the captured province.
		spawn_army = {
			name = event_troop_default_name
			levies = var:number_rebel_reinforcements
			location = scope:barony.title_province
			war = scope:war
		}

		# Additional loss of county control, can be up to -70 for large counties.
		every_in_list = {
			list = occupied_baronies
			scope:county = {
				change_county_control = -10
			}
		}
		# Lose an extra -10 county control.
		scope:county = {
			change_county_control = -10
		}
		
		remove_variable = number_rebel_reinforcements
	}
}

##################################################
# Claimant Faction
# by Ismael Serrano
# 2001 - 2010
##################################################

scripted_effect faction_demand_claimant_counter_offer_corule_accepted_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_accepted }

	add_dread = medium_dread_loss

	custom_tooltip = {
		text = faction_demand.0000.truce_from_co_rule
		scope:faction = {
			every_faction_member = {
				save_scope_as = recipient # Ensuring it localizes correctly
				add_truce_both_ways = {
					character = scope:faction.faction_target
					years = faction_counter_offer_appointed_co_emperor_value
					name = ep3_truce_appointed_faction_leader_coruler
				}
			}
		}
	}

	# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
	faction_accept_demand_legitimacy_effect = yes
	on_claimant_faction_war_win_common = {
		TARGET_TITLES = target_titles
		ATTACKER = scope:faction.faction_leader
		DEFENDER = root
		CLAIMANT = scope:faction.special_character
	}

	scope:faction = { destroy_faction = yes }
}

scripted_effect faction_demand_claimant_rejected_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_rejected }
	
	show_as_tooltip = {
		scope:faction = {
			faction_start_war = { title = scope:target_title }
			faction_spawn_member_county_armies_effect = {
				FACTION = scope:faction
				ARMY_OWNER = scope:faction_leader
				PEASANT_ARMY_NAME = claimant_faction_event_troops
			}
		}
	}
}

# Claimant Faction Demand
# by Ismael Serrano
faction_demand.2001 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_CLAIMANT"
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:faction_claimant = scope:faction_leader }
				desc = "FACTION_DEMAND_CLAIMANT_DESC_ME"
			}
			desc = "FACTION_DEMAND_CLAIMANT_DESC"
		}
	}
	
	trigger = {
		# May have ceased to exist by the time this triggers
		exists = scope:faction
	}

	immediate = {
		# Grab our scope for loc.
		save_scope_as = faction_target
		set_variable = {
			name = claimant_faction_sent_demand
			value = scope:faction_claimant
		}
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }

		add_dread = medium_dread_loss

		# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
		faction_accept_demand_legitimacy_effect = yes

		scope:faction = {
			every_faction_member = {
				trigger_event = faction_demand.2002
			}

			special_title = {
				add_to_list = target_titles
			}
		}

		# Admin Emperors lose all their held titles
		if = {
			limit = { has_realm_law = acclamation_succession_law }
			every_held_title = {
				limit = {
					# But not their noble family title, so they can continue playing as LADMIN
					NOR = { has_title_law = noble_family_succession_law }
					# and not baronies
					tier >= tier_county
				}
				add_to_list = target_titles
			}
		}

		on_claimant_faction_war_win_common = {
			TARGET_TITLES = target_titles
			ATTACKER = scope:faction.faction_leader
			DEFENDER = root
			CLAIMANT = scope:faction.special_character
		#	ATTACKER_PRESTIGE = 0
		#	DEFENDER_PRESTIGE = 0
		}

		ai_chance = {
			base = 10
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				factor = 0
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			modifier = {
				add = -25
				is_at_war = no
				primary_title.tier >= tier_kingdom
			}
			modifier = {
				add = -50
				is_at_war = no
				primary_title.tier >= tier_empire
			}
			
			### EK TODO: maybe add our own?
			modifier = {
				add = -5000
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
			 		ai_has_conqueror_personality = yes
				}
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_BRIBE_CO_EMPEROR"
		show_as_unavailable = {
			OR = {
				has_active_diarchy = yes
				scope:faction_leader != scope:faction.special_character
			}
		}
		trigger = {
			faction_target_can_attempt_to_bribe_with_co_emperorship_trigger = yes
			custom_tooltip = {
				text = FACTION_DEMAND_BRIBE_CO_EMPEROR_REASON
				scope:faction_leader = scope:faction.special_character
			}
		}

		# Ping-pong the choice back to the target.
		## Laying out the consequences.
		show_as_tooltip = {
			random_list = {
				# They accept!
				100 = {
					desc = faction_demand.0000.faction_leader_accepts
					show_chance = no
					faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
					faction_demand_claimant_counter_offer_corule_accepted_effect = yes
				}
				# They decline.
				100 = {
					desc = faction_demand.0000.faction_leader_rejects
					show_chance = no
					faction_demand_claimant_rejected_effect = yes
				}
			}
		}
		## And sorting the actual flow.
		scope:faction_leader = { trigger_event = faction_demand.2006 }

		ai_chance = { liege_offer_co_emperorship_to_faction_leader_modifier = yes }
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"

		faction_demand_claimant_rejected_effect = yes
		
		scope:faction_leader = { trigger_event = faction_demand.2003 }

		ai_chance = {
			base = 100
			compare_modifier = {
				trigger = {
					ai_boldness > 0
				}
				value = ai_boldness
				multiplier = 3
			}
			compare_modifier = {
				trigger = {
					ai_boldness < 0
				}
				value = ai_boldness
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
			### EK TODO: maybe add our own?
			modifier = {
				add = 5000
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
		}
	}

	after = {
		remove_variable = claimant_faction_sent_demand
	}
}

# Claimant Faction Demand Accepted
# by Ismael Serrano
faction_demand.2002 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_CLAIMANT_ACCEPTED"
	}
	desc = "FACTION_DEMAND_CLAIMANT_ACCEPTED_DESC"

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
	}
}

# Claimant Faction Demand Refused
# by Ismael Serrano
faction_demand.2003 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_CLAIMANT_REFUSED"
	}
	desc = "FACTION_DEMAND_CLAIMANT_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			every_faction_member = {
				limit = {
					NOT = { this = root } # i.e. not faction leader, even when the war disbands the faction
				}
				trigger_event = faction_demand.2004
			}
			
			faction_start_war = {
				title = special_title
			}

			# This needs to occur after the war begins, or the troops can't spawn correctly.
			faction_spawn_member_county_armies_effect = {
				FACTION = scope:faction
				ARMY_OWNER = scope:faction_leader
				PEASANT_ARMY_NAME = claimant_faction_event_troops
			}
		}
	}
}

# Claimant Faction Demand Refused Member Notice
# by Ismael Serrano
faction_demand.2004 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_CLAIMANT_REFUSED"
	}
	desc = "FACTION_DEMAND_CLAIMANT_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"	
		custom_tooltip = faction_demand.2004.opt_tt
	}
}

# Claimant Faction Demand Send Notice
# by Ismael Serrano
faction_demand.2005 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_CLAIMANT_SEND_DEMAND_NOTIFICATION"
	}
	desc = "FACTION_DEMAND_CLAIMANT_SEND_DEMAND_NOTIFICATION_DESC"

	option = {
		name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"	
	}
}

# Co-emperor counter-offer.
faction_demand.2006 = {
	type = letter_event
	sender = scope:faction_target
	opening = FACTION_DEMAND_COUNTER_OFFER_CORULE
	desc = FACTION_DEMAND_COUNTER_OFFER_CORULE_DESC
	
	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_ACCEPT

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_start_diarchy
					title = faction_demand.0000.offer_accepted
					show_as_tooltip = { custom_tooltip = faction_demand.0000.enter_co_emperorship }
				}
			}
			scope:faction = {
				every_faction_member = {
					limit = {
						is_ai = no
						NOT = { this = scope:faction_leader }
					}
					trigger_event = faction_demand.9011
				}
			}
		}
		# Actual effects.
		scope:faction_target = {
			faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
			faction_demand_claimant_counter_offer_corule_accepted_effect = yes
		}

		# Low weight: we control the AI accepting this by toggling whether they can access the refusal option at all.
		ai_chance = { base = 20 }
	}

	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_REJECT
		trigger = { faction_leader_would_consider_turning_down_co_emperorship_trigger = yes }

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_war_declared_on_me
					title = faction_demand.0000.offer_rejected
					show_as_tooltip = {
						scope:faction = { custom_tooltip = faction_starts_war.tt.replacement }
					}
				}
			}
		}
		# Actual effects.
		faction_demand_claimant_rejected_effect = yes

		# If the AI can access this at all then they should strongly consider it.
		ai_chance = { base = 80 }
	}
}

##################################################
# Nation Fracturing Faction
# by Maxence Voleau
# 1000 - 1009 (but not 1001 or 1010)
##################################################

scripted_effect faction_demand_nation_fracturing_counter_offer_corule_accepted_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_accepted }

	add_dread = medium_dread_loss
	# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
	faction_accept_demand_legitimacy_effect = yes

	custom_tooltip = {
		text = faction_demand.0000.truce_from_co_rule
		scope:faction = {
			every_faction_member = {
				save_scope_as = recipient # Ensuring it localizes correctly
				add_truce_both_ways = {
					character = scope:faction.faction_target
					years = faction_counter_offer_appointed_co_emperor_value
					name = ep3_truce_appointed_faction_leader_coruler
				}
			}
		}
	}
	scope:faction = { destroy_faction = yes }
}

scripted_effect faction_demand_nation_fracturing_rejected_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_rejected }

	show_as_tooltip = {
		scope:faction = {
			faction_start_war = {}
			custom_tooltip = faction_starts_war.tt.replacement
		}
	}
}

# Nation Fracturing demands
faction_demand.1000 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_COLLAPSE"
	}
	desc = "FACTION_DEMAND_COLLAPSE_DESC"

	trigger = {
		# May have ceased to exist by the time this triggers
		exists = scope:faction
	}

	immediate = {
		# Grab our scope for loc.
		save_scope_as = faction_target
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }

		add_dread = medium_dread_loss
		# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
		faction_accept_demand_legitimacy_effect = yes

		# We are doing this before the create_title_and_vassal_change as the scope:change will be stored as a event_target for the faction_demand.1002 and cause an "OOS" - Title and vassal changes should be resolved in the same tick
		scope:faction = {
			every_faction_member = {
				trigger_event = faction_demand.1002
			}
		}

		create_title_and_vassal_change = {
			type = independency
			save_scope_as = change
			add_claim_on_loss = no
		}

		scope:faction = {
			every_faction_member = {
				change_liege_or_become_independent = {
					CHANGE = scope:change
					VASSAL = this
				}

				hidden_effect = {
					save_scope_as = recipient # Ensuring it localizes correctly
					add_truce_both_ways = {
						character = root
						days = 1825
						name = TRUCE_GRANT_INDEPENDENCE
					}

					# Struggle Catalysts.
					if = {
						limit = {
							catalyst_gave_independence_to_powerful_diff_faith_culture_vassal_preliminary_trigger = {
								CHAR1 = root
								CHAR2 = scope:recipient
							}
							any_character_struggle = {
								involvement = involved
								activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
									CATALYST = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
									CHAR = scope:recipient
								}
							}
						}
						every_character_struggle = {
							involvement = involved
							limit = {
								activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
									CATALYST = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
									CHAR = scope:recipient
								}
							}
							activate_struggle_catalyst = {
								catalyst = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
								character = root
							}
						}
					}
				}
			}
		}

		every_vassal = {
			limit = { is_ai = no }
			send_interface_toast = {
				type = event_faction_neutral_text
				title = liege_primary_title_dissolution
				desc = liege_primary_title_dissolution_desc
				left_icon = root
			}
		}

		resolve_title_and_vassal_change = scope:change

		every_held_title = {
			limit = {
				tier = prev.primary_title.tier
			}
			add_to_temporary_list = titles_to_destroy
		}

		every_in_list = {
			list = titles_to_destroy
			holder = {
				destroy_title = prev
			}
		}

		add_prestige = {
			value = medium_prestige_value
			multiply = -1
		}

		ai_chance = {
			base = 1
			modifier = {
				add = 99
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power >= 300 }
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = 25
			}
			modifier = {
				add = -50
				is_at_war = no
				primary_title.tier >= tier_kingdom
			}
			modifier = {
				add = -150
				is_at_war = no
				primary_title.tier >= tier_empire
			}
			### EK TODO: maybe add our own?
			#modifier = {
			#	add = -5000
			#	OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
			#	}
			#}
		}
	}

	option = {
		name = "FACTION_DEMAND_BRIBE_CO_EMPEROR"
		show_as_unavailable = { has_active_diarchy = yes }
		trigger = { faction_target_can_attempt_to_bribe_with_co_emperorship_trigger = yes }

		# Ping-pong the choice back to the target.
		## Laying out the consequences.
		show_as_tooltip = {
			random_list = {
				# They accept!
				100 = {
					desc = faction_demand.0000.faction_leader_accepts
					show_chance = no
					faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
					faction_demand_nation_fracturing_counter_offer_corule_accepted_effect = yes
				}
				# They decline.
				100 = {
					desc = faction_demand.0000.faction_leader_rejects
					show_chance = no
					faction_demand_nation_fracturing_rejected_effect = yes
				}
			}
		}
		## And sorting the actual flow.
		scope:faction_leader = { trigger_event = faction_demand.1006 }

		ai_chance = { liege_offer_co_emperorship_to_faction_leader_modifier = yes }
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"

		faction_demand_nation_fracturing_rejected_effect = yes
		
		scope:faction_leader = {
			trigger_event = faction_demand.1003
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 4.0
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
			compare_modifier = {
				value = debt_level
				multiplier = -25
			}
			### EK TODO: maybe add our own?
			modifier = {
				add = 5000
				OR = {
			#		has_title = title:e_mongol_empire
			#		has_title = title:e_ilkhanate
			#		has_title = title:e_golden_horde
			#		has_title = title:e_chagatai
			#		has_title = title:e_black_horde
			#		has_title = title:e_white_horde
			#		has_title = title:e_aarlud_khanate
			#		has_title = title:e_togskol_khanate
			#		has_title = title:e_baruun_khanate
			#		has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
		}
	}
}

# Nation Fracturing Demand Accepted
faction_demand.1002 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_COLLAPSE_ACCEPTED"
	}
	desc = "FACTION_DEMAND_COLLAPSE_ACCEPTED_DESC"

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
		custom_tooltip = faction_demand.1002.tt
	}
}

# Nation Fracturing Demand Refused
faction_demand.1003 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_COLLAPSE_REFUSED"
	}
	desc = "FACTION_DEMAND_COLLAPSE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			faction_start_war = {}

			every_faction_member = {
				limit = { NOT = { this = scope:faction.faction_leader } }
				trigger_event = faction_demand.1004
			}
		}
	}
}

# Independence Faction Demand Refused Member Notice
faction_demand.1004 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_COLLAPSE_REFUSED"
	}
	desc = "FACTION_DEMAND_COLLAPSE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
	}
}

# Independence Faction Demand Send Notice
faction_demand.1005 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_COLLAPSE_SEND_DEMAND_NOTIFICATION"
	}
	desc = "FACTION_DEMAND_COLLAPSE_SEND_DEMAND_NOTIFICATION_DESC"

	option = {
		name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"
	}
}

# Co-emperor counter-offer.
faction_demand.1006 = {
	type = letter_event
	sender = scope:faction_target
	opening = FACTION_DEMAND_COUNTER_OFFER_CORULE
	desc = FACTION_DEMAND_COUNTER_OFFER_CORULE_DESC
	
	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_ACCEPT

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_start_diarchy
					title = faction_demand.0000.offer_accepted
					show_as_tooltip = { custom_tooltip = faction_demand.0000.enter_co_emperorship }
				}
			}
			scope:faction = {
				every_faction_member = {
					limit = {
						is_ai = no
						NOT = { this = scope:faction_leader }
					}
					trigger_event = faction_demand.9011
				}
			}
		}
		# Actual effects.
		scope:faction_target = {
			faction_demand_counter_offer_co_rule_start_diarchy_effect = yes
			faction_demand_nation_fracturing_counter_offer_corule_accepted_effect = yes
		}

		# Low weight: we control the AI accepting this by toggling whether they can access the refusal option at all.
		ai_chance = { base = 20 }
	}

	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_CORULE_REJECT
		trigger = { faction_leader_would_consider_turning_down_co_emperorship_trigger = yes }

		# Inform the relevant parties.
		hidden_effect = {
			scope:faction_target = {
				send_interface_toast = {
					type = msg_war_declared_on_me
					title = faction_demand.0000.offer_rejected
					show_as_tooltip = {
						scope:faction = { custom_tooltip = faction_starts_war.tt.replacement }
					}
				}
			}
		}
		# Actual effects.
		faction_demand_nation_fracturing_rejected_effect = yes

		# If the AI can access this at all then they should strongly consider it.
		ai_chance = { base = 80 }
	}
}

##################################################
# Faction Edgecases
# by various
# 9001 - 9050
##################################################

# Faction declares a retaliatory war.
faction_demand.9001 = {
	type = character_event
	title = faction_demand.9001.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:recipient = {
						is_imprisoned = yes
						imprisoner = scope:actor
					}
				}
				desc = FACTION_SUCCESSFUL_IMPRISONMENT_RETALIATION
			}
			desc = FACTION_FAILED_IMPRISONMENT_RETALIATION
		}
		desc = FACTION_IMPRISONMENT_RETALIATION_END
	}
	theme = war
	override_background = {
		trigger = {
			scope:recipient = {
				is_imprisoned = yes
				imprisoner = scope:actor
			}
		}
		reference = dungeon
	}
	left_portrait = {
		character = scope:actor
		animation = schadenfreude
	}
	right_portrait = {
		character = scope:recipient
		animation = anger
	}
	
	trigger = {
		exists = scope:retaliating_faction
	}

	immediate = {
		show_as_tooltip = {
			scope:retaliating_faction = {
				faction_start_war = {}
			}
		}
	}

	option = {
		name = FACTION_IMPRISONMENT_RETALIATION_OPT

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_rejected }
	}
}

# Co-emperor counter-offer.
faction_demand.9011 = {
	type = letter_event
	sender = scope:faction_leader
	opening = FACTION_DEMAND_COUNTER_OFFER_CORULE_MEMBER_NOTIFICATION
	desc = FACTION_DEMAND_COUNTER_OFFER_CORULE_MEMBER_NOTIFICATION_DESC
	
	immediate = {
		show_as_tooltip = {
			scope:faction_target = { faction_demand_counter_offer_co_rule_start_diarchy_effect = yes }
			scope:faction = { destroy_faction = yes }
		}
	}

	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_MEMBER_NOTIFICATION.A

		# No need for AI chance or stress, just a notification event.
	}

	option = {
		name = FACTION_DEMAND_COUNTER_OFFER_MEMBER_NOTIFICATION.B
		trigger = { is_ai = no }

		cancel_truce_both_ways = scope:faction_target
		reverse_add_opinion = {
			target = scope:faction_target
			modifier = refusal_opinion
			opinion = -40
		}
		change_strife_opinion = major_strife_gain

		# Player-only choice.
	}
}

##################################################
# Nomadic Faction
##################################################

# Nomadic Faction Demand
faction_demand.9998 = {
	type = letter_event
	sender = scope:peasant_leader
	opening = {
		desc = "FACTION_DEMAND_NOMADIC"
	}
	desc = FACTION_DEMAND_NOMADIC_DESC

	trigger = {
		exists = scope:faction
		exists = scope:peasant_leader
		scope:peasant_leader.joined_faction = scope:faction
	}

	immediate = {
		every_realm_province = {
			county = { add_to_list = remaining_counties_list }
		}
		scope:faction = {
			every_faction_county_member = {
				remove_from_list = remaining_counties_list
				duchy = {
					every_de_jure_county = {
						limit = {
							is_in_list = remaining_counties_list
						}
						remove_from_list = remaining_counties_list
					}
				}
			}
		}
		scope:peasant_county.duchy = { save_scope_as = nomad_main_duchy }
	}

	# Option A: Grant the faction independence.
	option = {
		name = "FACTION_DEMAND_NOMADIC_ACCEPT_GRANT_INDEPENDENCE"
		trigger = { # AI should not give the faction independence if a player is targeted by the faction.
			NAND = {
				scope:faction = {
					has_variable = faction_targets_player
				}
				is_ai = yes
			}
		}

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_accepted }
		
		add_prestige_level = -1
		if = {
			limit = {
				dread > 0
			}
			add_dread = medium_dread_loss
		}

		successful_nomadic_revolt_outcome_effect = { 
			FACTION_LEADER = scope:peasant_leader
		}

		# If the faction includes ALL of our counties, notify the player that it will leave them landless!
		if = {
			limit = {
				any_in_list = {
					list = remaining_counties_list
					count < 1
				}
			}
			custom_tooltip = FACTION_DEMAND_POPULIST_ACCEPT_GRANT_INDEPENCE_TT
		}

		hidden_effect = {
			if = {
				limit = {
					exists = scope:faction
				}
				scope:faction = {
					clean_revolt_county_modifiers_from_faction_members_effect = yes
					destroy_faction = yes
				}
			}
		}

		ai_chance = {
			base = 0
			compare_modifier = { # Stronger factions increase the chance of an AI ruler capitulating; at 200% ruler army strength, this maxes out at +50% chance to accept demands.
				value = {
					value = scope:faction.faction_power
					subtract = 100
					multiply = 0.5
				}
				max = 50
			}
			compare_modifier = { # Craven rulers can get up to +37% chance to accept demands.
				value = ai_boldness
				multiplier = -0.5
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = 25
			}
			modifier = {
				add = -50
				is_at_war = no
				primary_title.tier >= tier_kingdom
			}
			modifier = {
				add = -150
				is_at_war = no
				primary_title.tier >= tier_empire
			}
			
			modifier = {
				add = -5000
				OR = {
					#has_title = title:e_mongol_empire
					#has_title = title:e_ilkhanate
					#has_title = title:e_golden_horde
					#has_title = title:e_chagatai
					#has_title = title:e_black_horde
					#has_title = title:e_white_horde
					#has_title = title:e_aarlud_khanate
					#has_title = title:e_togskol_khanate
					#has_title = title:e_baruun_khanate
					#has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
					government_allows = state_faith
				}
			}
		}
	}

	# Option B: Refuse the demand and start a war.
	option = {
		name = "FACTION_DEMAND_POPULIST_REFUSE"

		# This effect should always be placed here, _before_ all the other effects in the block.
		trigger_event = { on_action = on_faction_demand_rejected }

		scope:faction = {
			faction_start_war = {
				title = scope:nomad_main_duchy
			}
		}
		
		custom_tooltip = FACTION_DEMAND_POPULIST_REFUSE_TT
		
		if = { # Send follow up event to affected vassals.
			limit = {
				list_size = {
					name = nomadic_faction_vassal_targets
					value > 0
				}
			}
			every_in_list = {
				list = nomadic_faction_vassal_targets
				limit = {
					is_ai = no
				}
				trigger_event = faction_demand.9999
			}
		}

		ai_chance = {
			base = 100
			compare_modifier = { # Weaker factions increase the chance of an AI ruler refusing; at 80% army strength (minimum), this will be +20; at 200% army strength, this will max out at -50.
				value = {			
					value = scope:faction.faction_power
					subtract = 100
					multiply = -1
				}
				min = -50
			}
			compare_modifier = { # Brave rulers get up to a 50% bonus chance to refuse.
				value = ai_boldness
				multiplier = -0.5
				min = -50
			}
			modifier = {
				factor = 1.5
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = -25
			}
			modifier = {
				add = 5000
				OR = {
					#has_title = title:e_mongol_empire
					#has_title = title:e_ilkhanate
					#has_title = title:e_golden_horde
					#has_title = title:e_chagatai
					#has_title = title:e_black_horde
					#has_title = title:e_white_horde
					#has_title = title:e_aarlud_khanate
					#has_title = title:e_togskol_khanate
					#has_title = title:e_baruun_khanate
					#has_title = title:e_byzantium
					ai_has_conqueror_personality = yes
				}
			}
		}
	}
	
	after = {
		if = {
			limit = {
				exists = scope:faction
				scope:faction = {
					has_variable = faction_targets_player
				}
			}
			scope:faction = {
				remove_variable = faction_targets_player
			}
		}
	}
}

# Nomadic Faction Demand - Affected vassals choose their action
faction_demand.9999 = {
	type = letter_event
	sender = scope:peasant_leader
	opening = {
		desc = "FACTION_DEMAND_NOMADIC"
	}
	desc = FACTION_DEMAND_NOMADIC_VASSAL_DESC

	trigger = {
		exists = scope:faction
		exists = scope:peasant_leader
		scope:peasant_leader.joined_faction = scope:faction
	}
	
	immediate = {
		scope:faction.faction_war = {
			save_scope_as = populist_war # Used for loc
		}
	}
	
	# Option A: Fight alongside your liege.
	option = {
		name = FACTION_DEMAND_NOMADIC_VASSAL_JOIN_WAR
		
		# You cannot join the defending side...
		trigger = {
			custom_tooltip = { # ... If you already are at war with another participant.
				text = FACTION_DEMAND_POPULIST_VASSAL_JOIN_WAR_BLOCK_TT
				scope:faction.faction_war = {
					NOT = {
						any_war_participant = {
							is_at_war_with = root
						}
					}
				}
			}
			custom_tooltip = { # ... If it would put you against someone you are fighting another war with.
				text = FACTION_DEMAND_POPULIST_VASSAL_JOIN_WAR_BLOCK_TT_2
				scope:faction.faction_war = {
					NOT = {
						any_war_attacker = {
							any_war_ally = {
								this = root
							}
						}
					}
				}
			}
		}
		show_as_unavailable = {
			OR = {
				scope:faction.faction_war = {
					any_war_participant = {
						is_at_war_with = root
					}
				}
				scope:faction.faction_war = {
					any_war_attacker = {
						any_war_ally = {
							this = root
						}
					}
				}
			}
		}
		
		# Join the war.
		scope:faction.faction_war = {
			add_defender = root
		}
		custom_tooltip = FACTION_DEMAND_POPULIST_VASSAL_JOIN_WAR_TT
		
		ai_chance = {
			base = 100
		}
	}
	
	# Option B: Do nothing.
	option = {
		name = FACTION_DEMAND_POPULIST_VASSAL_TAKE_NO_SIDE
		custom_tooltip = FACTION_DEMAND_POPULIST_VASSAL_TAKE_NO_SIDE_TT_A
		custom_tooltip = FACTION_DEMAND_POPULIST_VASSAL_TAKE_NO_SIDE_TT_B
		
		ai_chance = {
			base = 0
			
			modifier = {
				add = 20
				has_trait = craven
			}
		}
	}
}
