
# Scripted effects for PlusFaction, prefixed by 'pf_'

## FACTION MOOD INCREASE/DECREASE ##
# Handy quick-effects for basic faction mood plus/minus outcomes. These are
# primarily used by CBs, and they only cover convenient increments/decrements
# of faction mood (actual increase/decrease is noted in each effect's name).
# Ideally, all faction mood increase/decrease effects used in Plus and EMF can
# be centralized here eventually (for conformity, convenience, and also ease
# in changing tooltips or other implementation details).

# Court
pf_court_plus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_court } }
		custom_tooltip = { text = COURTFACTIONMOODPLUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = "faction_mood_increase" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_court_minus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_court } }
		custom_tooltip = { text = COURTFACTIONMOODMINUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = "faction_mood_decrease" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

# Prosperity
pf_prosperity_plus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_prosperity } }
		custom_tooltip = { text = PROSPERITYFACTIONMOODPLUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = "faction_mood_increase" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_prosperity_minus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_prosperity } }
		custom_tooltip = { text = PROSPERITYFACTIONMOODMINUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = "faction_mood_decrease" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_prosperity_plus4_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_prosperity } }
		custom_tooltip = { text = PROSPERITYFACTIONMOODPLUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = "faction_mood_increase" value = 4 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_prosperity_minus4_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_prosperity } }
		custom_tooltip = { text = PROSPERITYFACTIONMOODMINUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = "faction_mood_decrease" value = 4 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

# Glory
pf_glory_plus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_glory } }
		custom_tooltip = { text = GLORYFACTIONMOODPLUS_NONAME }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = "faction_mood_increase" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_glory_minus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_glory } }
		custom_tooltip = { text = GLORYFACTIONMOODMINUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = "faction_mood_decrease" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

# Traditon
pf_tradition_plus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_tradition } }
		custom_tooltip = { text = TRADITIONFACTIONMOODPLUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = "faction_mood_increase" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_tradition_plus4_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_tradition } }
		custom_tooltip = { text = TRADITIONFACTIONMOODPLUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = "faction_mood_increase" value = 4 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_tradition_minus2_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_tradition } }
		custom_tooltip = { text = TRADITIONFACTIONMOODMINUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = "faction_mood_decrease" value = 2 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}
pf_tradition_minus4_effect = {
	if = {
		limit = { any_vassal = { leads_faction = faction_tradition } }
		custom_tooltip = { text = TRADITIONFACTIONMOODMINUS }
		hidden_tooltip = {
			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = "faction_mood_decrease" value = 4 }
			}
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}


## FACTION INTERNALS ##
# These are mostly used internal to the PlusFaction code to reduce redundancy
# and centralize some logic for easier modification later.

pf_clear_supporter_flags_effect = {
	clr_character_flag = faction_court_supporter
	clr_character_flag = faction_prosperity_supporter
	clr_character_flag = faction_glory_supporter
	clr_character_flag = faction_tradition_supporter
}

pf_clear_succession_flags_effect = {
	if = {
		limit = { has_character_flag = demands_seniority }
		FROM = {
			random_demesne_title = {
				limit = { has_title_flag = change_seniority }
				clr_title_flag = change_seniority
			}
		}
	}
	if = {
		limit = { has_character_flag = demands_primogeniture }
		FROM = {
			random_demesne_title = {
				limit = { has_title_flag = change_primogeniture }
				clr_title_flag = change_primogeniture
			}
		}
	}
	if = {
		limit = { has_character_flag = demands_gavelkind }
		FROM = {
			random_demesne_title = {
				limit = { has_title_flag = change_gavelkind }
				clr_title_flag = change_gavelkind
			}
		}
	}
	if = {
		limit = { has_character_flag = demands_feudal_elective }
		FROM = {
			random_demesne_title = {
				limit = { has_title_flag = change_feudal_elective }
				clr_title_flag = change_feudal_elective
			}
		}
	}
}

pf_clear_war_flags_effect = {
	clr_character_flag = faction_court_war
	clr_character_flag = faction_prosperity_war
	clr_character_flag = faction_glory_war
	clr_character_flag = faction_tradition_war
	clr_character_flag = faction_pretender_war
	clr_character_flag = faction_separatist_war
	clr_character_flag = faction_religious_war
	clr_character_flag = faction_succession_war
	clr_character_flag = faction_authority_war
}

pf_clear_demands_flags_effect = {
	clr_character_flag = demands_lower_CA
	clr_character_flag = demands_lower_TO
	clr_character_flag = demands_surrender_title
	clr_character_flag = demands_share_wealth
	clr_character_flag = demands_revert_succession
	clr_character_flag = demands_council_life_terms
	clr_character_flag = demands_lower_feudal_obligations
	clr_character_flag = demands_lower_tribal_obligations
	clr_character_flag = demands_lower_city_obligations
	clr_character_flag = demands_lower_temple_obligations
	clr_character_flag = demands_seniority
	clr_character_flag = demands_primogeniture
	clr_character_flag = demands_gavelkind
	clr_character_flag = demands_feudal_elective
	
	liege = {
		any_vassal = {
			ROOT = { clr_character_flag = faction_demand_@PREV }
		}
		any_demesne_title = {
			ROOT = { clr_character_flag = faction_demand_@PREV }
		}
	}
}

pf_reset_maintenance_vars_effect = {
	set_variable = { which = "faction_votes_court" value = 0 }
	set_variable = { which = "faction_votes_prosperity" value = 0 }
	set_variable = { which = "faction_votes_glory" value = 0 }
	set_variable = { which = "faction_votes_tradition" value = 0 }
	set_variable = { which = "faction_mood_court" value = 0 }
	set_variable = { which = "faction_mood_prosperity" value = 0 }
	set_variable = { which = "faction_mood_glory" value = 0 }
	set_variable = { which = "faction_mood_tradition" value = 0 }
}

# called within faction leader scope, votes *in favor* for the leader himself
pf_leader_vote_effect = {
	hidden_tooltip = {
		if = {
			limit = { real_tier = BARON }
			change_variable = { which = "faction_votes" value = 1 }
		}
		if = {
			limit = { real_tier = COUNT }
			change_variable = { which = "faction_votes" value = 2 }
		}
		if = {
			limit = { real_tier = DUKE }
			change_variable = { which = "faction_votes" value = 4 }
		}
		if = {
			limit = { real_tier = KING }
			change_variable = { which = "faction_votes" value = 8 }
		}
	}
}

# PREV scope is faction leader scope, THIS is a faction backer, THIS votes *in favor*
pf_member_vote_effect = {
	hidden_tooltip = {
		if = {
			limit = { real_tier = BARON }
			PREV = { change_variable = { which = "faction_votes" value = 1 } }
		}
		if = {
			limit = { real_tier = COUNT }
			PREV = { change_variable = { which = "faction_votes" value = 2 } }
		}
		if = {
			limit = { real_tier = DUKE }
			PREV = { change_variable = { which = "faction_votes" value = 4 } }
		}
		if = {
			limit = { real_tier = KING }
			PREV = { change_variable = { which = "faction_votes" value = 8 } }
		}
		reverse_remove_opinion = {
			who = PREV
			modifier = opinion_opposed_in_meeting
		}
		reverse_remove_opinion = {
			who = PREV
			modifier = opinion_supported_in_meeting
		}
	}
}

# PREV scope is faction leader scope, THIS is a faction backer, THIS votes *against*
pf_member_vote_against_effect = {
	hidden_tooltip = {
		if = {
			limit = { real_tier = BARON }
			PREV = { change_variable = { which = "faction_votes" value = -1 } }
		}
		if = {
			limit = { real_tier = COUNT }
			PREV = { change_variable = { which = "faction_votes" value = -2 } }
		}
		if = {
			limit = { real_tier = DUKE }
			PREV = { change_variable = { which = "faction_votes" value = -4 } }
		}
		if = {
			limit = { real_tier = KING }
			PREV = { change_variable = { which = "faction_votes" value = -8 } }
		}
		reverse_remove_opinion = {
			who = PREV
			modifier = opinion_opposed_in_meeting
		}
		reverse_remove_opinion = {
			who = PREV
			modifier = opinion_supported_in_meeting
		}
	}
}

## OTHER EFFECTS ##

pf_save_old_gender_law_effect = {
	if = {
		limit = {
			higher_tier_than = count
		}
		clr_title_flag = former_cognatic_succession
		clr_title_flag = former_agnatic_succession
		clr_title_flag = former_true_cognatic_succession
		if = {
			limit = { has_law = cognatic_succession }
			set_title_flag = former_cognatic_succession
		}
		if = {
			limit = { has_law = agnatic_succession }
			set_title_flag = former_agnatic_succession
		}
		if = {
			limit = { has_law = true_cognatic_succession }
			set_title_flag = former_true_cognatic_succession
		}
	}
}

pf_save_old_succ_law_effect = {
	if = {
		limit = {
			higher_tier_than = count
		}
		clr_title_flag = former_gavelkind
		clr_title_flag = former_seniority
		clr_title_flag = former_primogeniture
		clr_title_flag = former_feudal_elective
		clr_title_flag = former_tanistry
		clr_title_flag = former_ultimogeniture
		if = {
			limit = { has_law = succ_gavelkind }
			set_title_flag = former_gavelkind
		}
		if = {
			limit = { has_law = succ_seniority }
			set_title_flag = former_seniority
		}
		if = {
			limit = { has_law = succ_primogeniture }
			set_title_flag = former_primogeniture
		}
		if = {
			limit = { has_law = succ_feudal_elective }
			set_title_flag = former_feudal_elective
		}
		if = {
			limit = { has_law = succ_tanistry }
			set_title_flag = former_tanistry
		}
		if = {
			limit = { has_law = succ_ultimogeniture }
			set_title_flag = former_ultimogeniture
		}
	}
}

## FACTION DEMAND EFFECTS ##

# THIS is the title upon which to revert succession (generally a primary_title)
pf_revert_succession_law_effect = {
	if = {
		limit = {
			has_title_flag = former_gavelkind
			NOT = { has_law = succ_gavelkind }
			OR = {
				is_law_potential = succ_institute_gavelkind
				is_law_potential = succ_elective_to_gavelkind
			}
		}
		add_law = succ_gavelkind
	}
	if = {
		limit = {
			has_title_flag = former_seniority
			NOT = { has_law = succ_seniority }
			OR = {
				is_law_potential = succ_institute_seniority
				is_law_potential = succ_elective_to_seniority
			}
		}
		add_law = succ_seniority
	}
	if = {
		limit = {
			has_title_flag = former_primogeniture
			NOT = { has_law = succ_primogeniture }
			OR = {
				is_law_potential = succ_institute_primogeniture
				is_law_potential = succ_elective_to_primogeniture
			}
		}
		add_law = succ_primogeniture
	}
	if = {
		limit = {
			has_title_flag = former_feudal_elective
			NOT = { has_law = succ_feudal_elective }
			OR = {
				is_law_potential = succ_institute_feudal_elective
				is_law_potential = succ_elective_to_feudal_elective
			}
		}
		add_law = succ_feudal_elective
	}
	if = {
		limit = {
			has_title_flag = former_tanistry
			NOT = { has_law = succ_tanistry }
			OR = {
				is_law_potential = succ_institute_tanistry
				is_law_potential = succ_elective_to_tanistry
			}
		}
		add_law = succ_tanistry
	}
	if = {
		limit = {
			has_title_flag = former_ultimogeniture
			NOT = { has_law = succ_ultimogeniture }
			is_law_potential = succ_institute_ultimogeniture
		}
		add_law = succ_ultimogeniture
	}
	if = {
		limit = {
			has_title_flag = former_cognatic_succession
			NOT = { has_law = cognatic_succession }
			is_law_potential = institute_cognatic_succession
		}
		add_law = cognatic_succession
	}
	if = {
		limit = {
			has_title_flag = former_agnatic_succession
			NOT = { has_law = agnatic_succession }
			is_law_potential = institute_agnatic_succession
		}
		add_law = agnatic_succession
	}
	if = {
		limit = {
			has_title_flag = former_true_cognatic_succession
			NOT = { has_law = true_cognatic_succession }
			is_law_potential = institute_true_cognatic_succession
		}
		add_law = true_cognatic_succession
	}
	clr_title_flag = former_gavelkind
	clr_title_flag = former_seniority
	clr_title_flag = former_primogeniture
	clr_title_flag = former_feudal_elective
	clr_title_flag = former_tanistry
	clr_title_flag = former_ultimogeniture
	clr_title_flag = former_cognatic_succession
	clr_title_flag = former_agnatic_succession
	clr_title_flag = former_true_cognatic_succession
}

pf_lower_feudal_obligations_effect = {
	primary_title = {
		if = {
			limit = { has_law = feudal_obligations_1 }
			add_law_w_cooldown = feudal_obligations_0
		}
		if = {
			limit = { has_law = feudal_obligations_2 }
			add_law_w_cooldown = feudal_obligations_1
		}
		if = {
			limit = { has_law = feudal_obligations_3 }
			add_law_w_cooldown = feudal_obligations_2
		}
		if = {
			limit = { has_law = feudal_obligations_4 }
			add_law_w_cooldown = feudal_obligations_3
		}
		if = {
			limit = { has_law = iqta_obligations_1 }
			add_law_w_cooldown = iqta_obligations_0
		}
		if = {
			limit = { has_law = iqta_obligations_2 }
			add_law_w_cooldown = iqta_obligations_1
		}
		if = {
			limit = { has_law = iqta_obligations_3 }
			add_law_w_cooldown = iqta_obligations_2
		}
		if = {
			limit = { has_law = iqta_obligations_4 }
			add_law_w_cooldown = iqta_obligations_3
		}
	}
}

pf_lower_tribal_obligations_effect = {
	primary_title = {
		if = {
			limit = { has_law = tribal_obligations_1 }
			add_law_w_cooldown = tribal_obligations_0
		}
		if = {
			limit = { has_law = tribal_obligations_2 }
			add_law_w_cooldown = tribal_obligations_1
		}
		if = {
			limit = { has_law = tribal_obligations_3 }
			add_law_w_cooldown = tribal_obligations_2
		}
		if = {
			limit = { has_law = tribal_obligations_4 }
			add_law_w_cooldown = tribal_obligations_3
		}
	}
}

pf_lower_city_obligations_effect = {
	primary_title = {
		if = {
			limit = { has_law = city_obligations_1 }
			add_law_w_cooldown = city_obligations_0
		}
		if = {
			limit = { has_law = city_obligations_2 }
			add_law_w_cooldown = city_obligations_1
		}
		if = {
			limit = { has_law = city_obligations_3 }
			add_law_w_cooldown = city_obligations_2
		}
		if = {
			limit = { has_law = city_obligations_4 }
			add_law_w_cooldown = city_obligations_3
		}
	}
}

pf_lower_temple_obligations_effect = {
	primary_title = {
		if = {
			limit = { has_law = temple_obligations_1 }
			add_law_w_cooldown = temple_obligations_0
		}
		if = {
			limit = { has_law = temple_obligations_2 }
			add_law_w_cooldown = temple_obligations_1
		}
		if = {
			limit = { has_law = temple_obligations_3 }
			add_law_w_cooldown = temple_obligations_2
		}
		if = {
			limit = { has_law = temple_obligations_4 }
			add_law_w_cooldown = temple_obligations_3
		}
	}
}

pf_lower_TO_effect = {
	primary_title = {
		if = {
			limit = {
				OR = {
					has_law = tribal_organization_1
					has_law = tribal_organization_2
					has_law = tribal_organization_3
					has_law = tribal_organization_4
				}
			}
			custom_tooltip = {
				text = TO_LOWERED_NO_NAME
				hidden_tooltip = {
					if = {
						limit = { has_law = tribal_organization_1 }
						add_law_w_cooldown = tribal_organization_0
					}
					if = {
						limit = { has_law = tribal_organization_2 }
						add_law_w_cooldown = tribal_organization_1
					}
					if = {
						limit = { has_law = tribal_organization_3 }
						add_law_w_cooldown = tribal_organization_2
					}
					if = {
						limit = { has_law = tribal_organization_4 }
						add_law_w_cooldown = tribal_organization_3
					}
				}
			}
		}
	}
}

pf_lower_CA_effect = {
	any_demesne_title = {
		limit = {
			higher_tier_than = duke
			OR = {
				has_law = crown_authority_1
				has_law = crown_authority_2
				has_law = crown_authority_3
				has_law = crown_authority_4
			}
		}
		custom_tooltip = {
			text = CA_LOWERED_NO_NAME
			hidden_tooltip = {
				if = {
					limit = { has_law = crown_authority_1 }
					add_law = crown_authority_0
				}
				if = {
					limit = { has_law = crown_authority_2 }
					add_law = crown_authority_1
				}
				if = {
					limit = { has_law = crown_authority_3 }
					add_law = crown_authority_2
				}
				if = {
					limit = { has_law = crown_authority_4 }
					add_law = crown_authority_3
				}
			}
		}
	}
	hidden_tooltip = {
		if = {
			limit = { has_character_flag = byzantium_had_centr_three }
			clr_character_flag = byzantium_had_centr_three
		}
		if = {
			limit = { has_character_flag = byzantium_had_centr_four }
			set_character_flag = byzantium_had_centr_three
		}
	}
}


## CIVIL WAR SUPPORT CODE ##

pf_setup_rebel_effect = {
	add_trait = pf_rebel
	clr_character_flag = civil_war_faction_leader
	clr_character_flag = civil_war_faction_member
	clr_character_flag = will_join_civil_war
	hidden_tooltip = {
		# Remove prior loyalist/neutral TOMs, if any
		if = {
			limit = {
				has_opinion_modifier = { who = LIEGE modifier = pf_loyal_toward }
			}
			remove_opinion = { who = LIEGE modifier = pf_loyal_toward }
			reverse_remove_opinion = { who = LIEGE modifier = pf_loyal_to_me }
		}
		if = {
			limit = {
				has_opinion_modifier = { who = LIEGE modifier = pf_neutral_toward }
			}
			remove_opinion = { who = LIEGE modifier = pf_neutral_toward }
			reverse_remove_opinion = { who = LIEGE modifier = pf_neutral_to_me }
		}
		if = {
			limit = {
				NOT = { has_opinion_modifier = { who = LIEGE modifier = revolting_against } }
			}
			opinion = { who = LIEGE modifier = revolting_against }
		}
		if = {
			limit = {
				NOT = { reverse_has_opinion_modifier = { who = LIEGE modifier = revolting_against_me } }
			}
			reverse_opinion = { who = LIEGE modifier = revolting_against_me }
		}
	}
}

pf_teardown_rebel_effect = {
	remove_trait = pf_rebel
	hidden_tooltip = {
		any_opinion_modifier_target = { # Pre-revolt liege
			limit = { reverse_has_opinion_modifier = { who = PREV modifier = revolting_against } }
			reverse_remove_opinion = { who = PREV modifier = revolting_against }
			remove_opinion = { who = PREV modifier = revolting_against_me }
		}
	}
}

pf_teardown_loyalist_effect = {
	remove_trait = pf_loyalist
	hidden_tooltip = {
		any_opinion_modifier_target = { # Liege or previous liege
			limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_toward } }
			reverse_remove_opinion = { who = PREV modifier = pf_loyal_toward }
			remove_opinion = { who = PREV modifier = pf_loyal_to_me }
		}
	}
}

pf_teardown_neutral_effect = {
	remove_trait = pf_neutral
	hidden_tooltip = {
		any_opinion_modifier_target = { # Liege or previous liege
			limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_toward } }
			reverse_remove_opinion = { who = PREV modifier = pf_neutral_toward }
			remove_opinion = { who = PREV modifier = pf_neutral_to_me }
		}
	}
}

# Should be used in the scope of the faction leader
pf_punish_deceitful_player_effect = {
	hidden_tooltip = {
		liege = {
			any_vassal = {
				limit = {
					ai = no
					#not part of my faction
					NOR = {
						AND = {
							ROOT = {
								leads_faction = faction_court
								pf_not_regular_faction_war_trigger = no
							}
							in_faction = faction_court
						}
						AND = {
							ROOT = {
								leads_faction = faction_prosperity
								pf_not_regular_faction_war_trigger = no
							}
							in_faction = faction_prosperity
						}
						AND = {
							ROOT = {
								leads_faction = faction_glory
								pf_not_regular_faction_war_trigger = no
							}
							in_faction = faction_glory
						}
						AND = {
							ROOT = {
								leads_faction = faction_tradition
								pf_not_regular_faction_war_trigger = no
							}
							in_faction = faction_tradition
						}
						AND = {
							ROOT = {
								leads_faction = faction_succ_seniority
								has_character_flag = demands_seniority
							}
							in_faction = faction_succ_seniority
						}
						AND = {
							ROOT = {
								leads_faction = faction_succ_primogeniture
								has_character_flag = demands_primogeniture
							}
							in_faction = faction_succ_primogeniture
						}
						AND = {
							ROOT = {
								leads_faction = faction_succ_gavelkind
								has_character_flag = demands_gavelkind
							}
							in_faction = faction_succ_gavelkind
						}
						AND = {
							ROOT = {
								leads_faction = faction_succ_feudal_elective
								has_character_flag = demands_feudal_elective
							}
							in_faction = faction_succ_feudal_elective
						}
						AND = {
							ROOT = {
								leads_faction = faction_pretender
								has_character_flag = preparing_claimant_faction_war
							}
							in_faction = faction_pretender
						}
						AND = {
							ROOT = {
								leads_faction = faction_religious
								has_character_flag = preparing_religious_faction_war
							}
							religion = ROOT
							in_faction = faction_religious
						}
					}
					#either promised to join the war or dropped out of faction
					OR = {
						has_character_flag = will_join_civil_war
						has_character_flag = civil_war_faction_member
					}
					#capable of joining the faction
					is_adult = yes
					prisoner = no
					NOR = {
						trait = incapable
						has_opinion_modifier = {
							who = PREV
							modifier = opinion_coerced_into_leaving_faction
						}
						has_opinion_modifier = { who = PREV modifier = opinion_subjugated }
						has_opinion_modifier = { who = PREV modifier = opinion_subjugated_other_rel }
					}
				}
				letter_event = { id = PlusFaction.416 }
			}
		}
	}
}

pf_civil_war_clear_old_opinions_effect = {
	hidden_tooltip = {
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV modifier = opinion_civil_war_loser } }
			reverse_remove_opinion = { who = PREV modifier = opinion_civil_war_loser }
		}
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV modifier = opinion_civil_war_supporter } }
			reverse_remove_opinion = { who = PREV modifier = opinion_civil_war_supporter }
		}
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV modifier = opinion_civil_war_neutral } }
			reverse_remove_opinion = { who = PREV modifier = opinion_civil_war_neutral }
		}
	}
}

# ROOT=THIS is the victorious side after a civil war
pf_civil_war_cleanup_effect = {
	hidden_tooltip = {
		FROM = {
			any_opinion_modifier_target = {
				limit = { pf_part_of_this_revolt_trigger = yes }
				remove_trait = pf_rebel
				reverse_remove_opinion = { who = PREV modifier = revolting_against_me }
				remove_opinion = { who = PREV modifier = revolting_against }
			}

			# Clear neutral/loyalist TOM's
			any_opinion_modifier_target = {
				limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me } }
				remove_trait = pf_loyalist
				reverse_remove_opinion = { who = PREV modifier = pf_loyal_to_me }
				remove_opinion = { who = PREV modifier = pf_loyal_toward }
			}
			any_opinion_modifier_target = {
				limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me } }
				remove_trait = pf_neutral
				reverse_remove_opinion = { who = PREV modifier = pf_neutral_to_me }
				remove_opinion = { who = PREV modifier = pf_neutral_toward }
			}
		}
		ROOT = {
			clr_character_flag = original_war_liege
			pf_clear_succession_flags_effect = yes
			pf_clear_demands_flags_effect = yes
		}
		FROM = {
			pf_clear_war_flags_effect = yes
			#hidden_tooltip = { disband_event_forces = faction_loyalists }
		}
		#hidden_tooltip = { any_attacker = { disband_event_forces = faction_revolters } }
	}
}

pf_liege_change_effect = {
	hidden_tooltip = {
		if = {
			limit = {
				trait = pf_rebel
				OR = {
					is_ruler = no
					pf_in_revolt_trigger = no
				}
			}
			pf_teardown_rebel_effect = yes
		}
		if = {
			limit = {
				trait = pf_loyalist
				OR = {
					is_ruler = no
					NOT = { has_opinion_modifier = { who = LIEGE modifier = pf_loyal_toward } }
				}
			}
			pf_teardown_loyalist_effect = yes
		}
		if = {
			limit = {
				trait = pf_neutral
				OR = {
					is_ruler = no
					NOT = { has_opinion_modifier = { who = LIEGE modifier = pf_neutral_toward } }
				}
			}
			pf_teardown_neutral_effect = yes
		}
	}
}

# Careful! Don't put this convenience wrapper under a nested FROM!
pf_liege_change_under_title_begin_effect = {
	FROM = { save_event_target_as = pf_lc_enemy }
	pf_liege_change_under_title_ext_begin_effect = yes
	clear_event_target = pf_lc_enemy
}

pf_liege_change_under_title_ext_begin_effect = {
	hidden_tooltip = {
		any_de_jure_vassal = { # All rulers with land under this de jure title
			limit = {
				# That have civil war state
				is_playable = yes
				pf_war_trait_trigger = yes
				# That are in pf_lc_enemy's subrealm under the title PREV
				OR = {
					character = event_target:pf_lc_enemy
					is_liege_or_above = event_target:pf_lc_enemy
				}
			}
			# Add temporary TOM from Coptic Pope to tracked ruler
			d_miaphysite = {
				holder_scope = {
					if = {
						limit = {
							NOT = {
								has_opinion_modifier = {
									who = PREVPREV
									modifier = pf_lcut_ruler
								}
							}
						}
						opinion = { who = PREVPREV modifier = pf_lcut_ruler }
					}
				}
			}
		}
		# And now to also add any folks PF-revolting against folks under this title+subrealm...
		any_de_jure_vassal = { # All rulers with land under this de jure title
			limit = {
				# That have civil war defender state
				is_playable = yes
				any_war = {
					defender = { character = PREV }
					OR = {
						using_cb = cb_faction_overthrow_ruler
						using_cb = claim_on_liege_plot
						using_cb = other_claim_on_liege_plot
						using_cb = religious_overthrow_ruler
						using_cb = cb_faction_independence
					}
				}
				# That are in pf_lc_enemy's subrealm under the title PREV
				OR = {
					character = event_target:pf_lc_enemy
					is_liege_or_above = event_target:pf_lc_enemy
				}
			}
			any_opinion_modifier_target = { # Revolters
				limit = { reverse_has_opinion_modifier = { who = PREV modifier = revolting_against_me } }
				# Add temporary TOM from Coptic Pope to tracked ruler
				d_miaphysite = {
					holder_scope = {
						if = {
							limit = {
								NOT = {
									has_opinion_modifier = {
										who = PREVPREV
										modifier = pf_lcut_ruler
									}
								}
							}
							opinion = { who = PREVPREV modifier = pf_lcut_ruler }
						}
					}
				}
			}
			any_opinion_modifier_target = { # Neutrals
				limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me } }
				# Add temporary TOM from Coptic Pope to tracked ruler
				d_miaphysite = {
					holder_scope = {
						if = {
							limit = {
								NOT = {
									has_opinion_modifier = {
										who = PREVPREV
										modifier = pf_lcut_ruler
									}
								}
							}
							opinion = { who = PREVPREV modifier = pf_lcut_ruler }
						}
					}
				}
			}
			any_opinion_modifier_target = { # Loyalists
				limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me } }
				# Add temporary TOM from Coptic Pope to tracked ruler
				d_miaphysite = {
					holder_scope = {
						if = {
							limit = {
								NOT = {
									has_opinion_modifier = {
										who = PREVPREV
										modifier = pf_lcut_ruler
									}
								}
							}
							opinion = { who = PREVPREV modifier = pf_lcut_ruler }
						}
					}
				}
			}
		}
	}
}

pf_liege_change_under_title_end_effect = {
	hidden_tooltip = {
		d_miaphysite = {
			holder_scope = {
				any_opinion_modifier_target = { # Tracked rulers
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_lcut_ruler } }
					reverse_remove_opinion = { who = PREV modifier = pf_lcut_ruler }
					pf_liege_change_effect = yes
				}
			}
		}
	}
}

pf_log_state_effect = {
	hidden_tooltip = { character_event = { id = emf_faction.1000 } }
}