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

			###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
				}
			}
			###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
			}
			
			modifier = {
				add = 50
				OR = {
					mpo_has_gok_mongol_empire_trigger = yes
					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
			}
			
			modifier = {
				add = 50
				OR = {
					mpo_has_gok_mongol_empire_trigger = yes
					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 }
			}
			
			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
			}
		}
		
		hidden_effect = {
		scope:peasant_leader = { ## Player leader handling
		   joined_faction = {
		    every_faction_member = {
		    remove_character_modifier = oppressed_modifier
			quit_job_effect = yes
			sell_property_effect = 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_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
			}
			
			modifier = {
				add = -5000
				OR = {
					mpo_has_gok_mongol_empire_trigger = yes
					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
			}
			modifier = {
				add = 5000
				OR = {
					mpo_has_gok_mongol_empire_trigger = yes
					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
			}
		}
	}
}

# Peasant Faction Demand
# by Sean Hughes

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"
		
		scope:peasant_leader = {
		   joined_faction = {
		    every_faction_member = {
		   remove_character_modifier = oppressed_modifier
		   remove_character_modifier = peasant_oppressed_modifier
		   send_interface_toast = {
				title = peasant_victory
				custom_tooltip = peasant_victory_tt
			}
		   dynasty = { add_dynasty_modifier = priviliged_peasants_modifier }
		   }		  
         }
		}

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