
# 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_plus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODPLUS # Marginally

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_increase value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_court_plus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODPLUS # Modestly

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_increase value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_court_plus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODPLUS # Significantly

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_increase value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_court_plus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODPLUS # Greatly

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_increase value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_court_minus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_decrease value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_court_minus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_decrease value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_court_minus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_decrease value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_court_minus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_court } }

		custom_tooltip = {
			text = COURTFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_court }
				change_variable = { which = local_faction_mood_decrease value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

# Prosperity
pf_prosperity_plus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_increase value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_prosperity_plus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_increase value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_prosperity_plus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_increase value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_prosperity_plus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_increase value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_prosperity_minus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_decrease value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_prosperity_minus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_decrease value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_prosperity_minus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_decrease value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_prosperity_minus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_prosperity } }

		custom_tooltip = {
			text = PROSPERITYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_prosperity }
				change_variable = { which = local_faction_mood_decrease value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

# Glory
pf_glory_plus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_increase value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_glory_plus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_increase value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_glory_plus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_increase value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_glory_plus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_increase value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_glory_minus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_decrease value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_glory_minus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_decrease value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_glory_minus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_decrease value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_glory_minus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_glory } }

		custom_tooltip = {
			text = GLORYFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_glory }
				change_variable = { which = local_faction_mood_decrease value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

# Traditon
pf_tradition_plus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_increase value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_tradition_plus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_increase value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_tradition_plus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_increase value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_tradition_plus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODPLUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_increase value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_tradition_minus1_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_decrease value = 1 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_tradition_minus2_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_decrease value = 2 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_tradition_minus4_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_decrease value = 4 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			character_event = { id = PlusFaction.200 days = 1 }
		}
	}
}

pf_tradition_minus8_effect = {
	if = {
		limit = { faction_exists = { faction = faction_tradition } }

		custom_tooltip = {
			text = TRADITIONFACTIONMOODMINUS

			random_vassal = {
				limit = { leads_faction = faction_tradition }
				change_variable = { which = local_faction_mood_decrease value = 8 }
			}

			clear_delayed_event = { id = PlusFaction.200 } # In case more than one of these effects is called at the same time
			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_leave_faction_effect = {
	trigger_switch = {
		on_trigger = in_faction

		faction_court	   = { leave_faction = faction_court }
		faction_prosperity = { leave_faction = faction_prosperity }
		faction_glory	   = { leave_faction = faction_glory }
		faction_tradition  = { leave_faction = faction_tradition }
	}
}

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

# Called before doing at the start of a faction maintenance pulse
pf_faction_vote_count = {
	# Count the number of votes in each faction
	any_vassal = {
		trigger_switch = {
			on_trigger = in_faction

			faction_court = {
				trigger_switch = {
					on_trigger = real_tier

					BARON = { change_variable = { which = local_faction_votes_court value = 1 } }
					COUNT = { change_variable = { which = local_faction_votes_court value = 2 } }
					DUKE  = { change_variable = { which = local_faction_votes_court value = 4 } }
					KING  = { change_variable = { which = local_faction_votes_court value = 8 } }
				}
			}

			faction_prosperity = {
				trigger_switch = {
					on_trigger = real_tier

					BARON = { change_variable = { which = local_faction_votes_prosperity value = 1 } }
					COUNT = { change_variable = { which = local_faction_votes_prosperity value = 2 } }
					DUKE  = { change_variable = { which = local_faction_votes_prosperity value = 4 } }
					KING  = { change_variable = { which = local_faction_votes_prosperity value = 8 } }
				}
			}

			faction_glory = {
				trigger_switch = {
					on_trigger = real_tier

					BARON = { change_variable = { which = local_faction_votes_glory value = 1 } }
					COUNT = { change_variable = { which = local_faction_votes_glory value = 2 } }
					DUKE  = { change_variable = { which = local_faction_votes_glory value = 4 } }
					KING  = { change_variable = { which = local_faction_votes_glory value = 8 } }
				}
			}

			faction_tradition = {
				trigger_switch = {
					on_trigger = real_tier

					BARON = { change_variable = { which = local_faction_votes_tradition value = 1 } }
					COUNT = { change_variable = { which = local_faction_votes_tradition value = 2 } }
					DUKE  = { change_variable = { which = local_faction_votes_tradition value = 4 } }
					KING  = { change_variable = { which = local_faction_votes_tradition value = 8 } }
				}
			}
		}
	}
}

# called within faction leader scope, votes *in favor* for the leader himself
pf_leader_vote_effect = {
	hidden_effect = {
		trigger_switch = {
			on_trigger = real_tier

			BARON = { change_variable = { which = faction_votes value = 1 } }
			COUNT = { change_variable = { which = faction_votes value = 2 } }
			DUKE  = { change_variable = { which = faction_votes value = 4 } }
			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_effect = {
		trigger_switch = {
			on_trigger = real_tier

			BARON = { PREV = { change_variable = { which = faction_votes value = 1 } } }
			COUNT = { PREV = { change_variable = { which = faction_votes value = 2 } } }
			DUKE  = { PREV = { change_variable = { which = faction_votes value = 4 } } }
			KING  = { PREV = { change_variable = { which = faction_votes value = 8 } } }
		}

		reverse_remove_opinion = {
			name = opinion_opposed_in_meeting
			who = PREV
		}

		reverse_remove_opinion = {
			name = opinion_supported_in_meeting
			who = PREV
		}
	}
}

# PREV scope is faction leader scope, THIS is a faction backer, THIS votes *against*
pf_member_vote_against_effect = {
	hidden_effect = {
		trigger_switch = {
			on_trigger = real_tier

			BARON = { PREV = { change_variable = { which = faction_votes value = -1 } } }
			COUNT = { PREV = { change_variable = { which = faction_votes value = -2 } } }
			DUKE  = { PREV = { change_variable = { which = faction_votes value = -4 } } }
			KING  = { PREV = { change_variable = { which = faction_votes value = -8 } } }
		}

		reverse_remove_opinion = {
			name = opinion_opposed_in_meeting
			who = PREV
		}

		reverse_remove_opinion = {
			name = opinion_supported_in_meeting
			who = PREV
		}
	}
}

## OTHER EFFECTS ##

pf_save_old_gender_law_effect = {
	if = {
		limit = { higher_real_tier_than = COUNT }

		clr_title_flag = former_cognatic_succession
		clr_title_flag = former_agnatic_succession
		clr_title_flag = former_true_cognatic_succession
		clr_title_flag = former_enatic_cognatic_succession
		clr_title_flag = former_enatic_succession

		trigger_switch = {
			on_trigger = has_law

			cognatic_succession = { set_title_flag = former_cognatic_succession }
			agnatic_succession = { set_title_flag = former_agnatic_succession }
			true_cognatic_succession = { set_title_flag = former_true_cognatic_succession }
			enatic_cognatic_succession = { set_title_flag = former_enatic_cognatic_succession }
			agnatic_succession = { set_title_flag = former_agnatic_succession }
		}
	}
}

pf_save_old_succ_law_effect = {
	if = {
		limit = { higher_real_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_hre_elective
		clr_title_flag = former_byzantine_elective
		clr_title_flag = former_tanistry
		clr_title_flag = former_eldership
		clr_title_flag = former_ultimogeniture

		trigger_switch = {
			on_trigger = has_law

			succ_gavelkind = { set_title_flag = former_gavelkind }
			succ_seniority = { set_title_flag = former_seniority }
			succ_primogeniture = { set_title_flag = former_primogeniture }
			succ_feudal_elective = { set_title_flag = former_feudal_elective }
			succ_hre_elective = { set_title_flag = former_hre_elective }
			succ_byzantine_elective = { set_title_flag = former_byzantine_elective }
			succ_tanistry = { set_title_flag = former_tanistry }
			succ_eldership = { set_title_flag = former_eldership }
			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 = {
	set_title_flag = non_manual_law_change # No prestige loss

	if = {
		limit = {
			has_title_flag = former_gavelkind
			NOT = { has_law = succ_gavelkind }
			is_law_potential = succ_gavelkind
		}
		add_law = succ_gavelkind
	}
	else_if = {
		limit = {
			has_title_flag = former_seniority
			NOT = { has_law = succ_seniority }
			is_law_potential = succ_seniority
		}
		add_law = succ_seniority
	}
	else_if = {
		limit = {
			has_title_flag = former_primogeniture
			NOT = { has_law = succ_primogeniture }
			is_law_potential = succ_primogeniture
		}
		add_law = succ_primogeniture
	}
	else_if = {
		limit = {
			has_title_flag = former_feudal_elective
			NOT = { has_law = succ_feudal_elective }
			is_law_potential = succ_feudal_elective
		}
		add_law = succ_feudal_elective
	}
	else_if = {
		limit = {
			has_title_flag = former_hre_elective
			NOT = { has_law = succ_hre_elective }
			is_law_potential = succ_feudal_elective
		}
		add_law = succ_hre_elective
	}
	else_if = {
		limit = {
			has_title_flag = former_byzantine_elective
			NOT = { has_law = succ_byzantine_elective }
			is_law_potential = succ_feudal_elective
		}
		add_law = succ_byzantine_elective
	}
	else_if = {
		limit = {
			has_title_flag = former_tanistry
			NOT = { has_law = succ_tanistry }
			is_law_potential = succ_tanistry
		}
		add_law = succ_tanistry
	}
	else_if = {
		limit = {
			has_title_flag = former_eldership
			NOT = { has_law = succ_eldership }
			is_law_potential = succ_tanistry
		}
		add_law = succ_eldership
	}
	else_if = {
		limit = {
			has_title_flag = former_ultimogeniture
			NOT = { has_law = succ_ultimogeniture }
			is_law_potential = succ_ultimogeniture
		}
		add_law = succ_ultimogeniture
	}
	else_if = {
		limit = {
			has_title_flag = former_agnatic_succession
			NOT = { has_law = agnatic_succession }
			is_law_potential = agnatic_succession
		}
		add_law = agnatic_succession
	}
	else_if = {
		limit = {
			has_title_flag = former_cognatic_succession
			NOT = { has_law = cognatic_succession }
			is_law_potential = cognatic_succession
		}
		add_law = cognatic_succession
	}
	else_if = {
		limit = {
			has_title_flag = former_true_cognatic_succession
			NOT = { has_law = true_cognatic_succession }
			is_law_potential = true_cognatic_succession
		}
		add_law = true_cognatic_succession
	}
	else_if = {
		limit = {
			has_title_flag = former_enatic_cognatic_succession
			NOT = { has_law = enatic_cognatic_succession }
			is_law_potential = enatic_cognatic_succession
		}
		add_law = enatic_cognatic_succession
	}
	else_if = {
		limit = {
			has_title_flag = former_enatic_succession
			NOT = { has_law = enatic_succession }
			is_law_potential = enatic_succession
		}
		add_law = enatic_succession
	}

	clr_title_flag = non_manual_law_change # No prestige loss

	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_agnatic_succession
	clr_title_flag = former_cognatic_succession
	clr_title_flag = former_true_cognatic_succession
	clr_title_flag = former_enatic_cognatic_succession
	clr_title_flag = former_enatic_succession
}

pf_lower_feudal_obligations_effect = {
	primary_title = {
		show_scope_change = no

		set_title_flag = non_manual_law_change # No prestige loss

		trigger_switch = {
			on_trigger = has_law

			feudal_obligations_1 = { add_law_w_cooldown = feudal_obligations_0 }
			feudal_obligations_2 = { add_law_w_cooldown = feudal_obligations_1 }
			feudal_obligations_3 = { add_law_w_cooldown = feudal_obligations_2 }
			feudal_obligations_4 = { add_law_w_cooldown = feudal_obligations_3 }
			iqta_obligations_1 = { add_law_w_cooldown = iqta_obligations_0 }
			iqta_obligations_2 = { add_law_w_cooldown = iqta_obligations_1 }
			iqta_obligations_3 = { add_law_w_cooldown = iqta_obligations_2 }
			iqta_obligations_4 = { add_law_w_cooldown = iqta_obligations_3 }
		}

		clr_title_flag = non_manual_law_change # No prestige loss
	}
}

pf_lower_tribal_obligations_effect = {
	primary_title = {
		show_scope_change = no

		set_title_flag = non_manual_law_change # No prestige loss

		trigger_switch = {
			on_trigger = has_law

			tribal_obligations_1 = { add_law_w_cooldown = tribal_obligations_0 }
			tribal_obligations_2 = { add_law_w_cooldown = tribal_obligations_1 }
			tribal_obligations_3 = { add_law_w_cooldown = tribal_obligations_2 }
			tribal_obligations_4 = { add_law_w_cooldown = tribal_obligations_3 }
		}

		clr_title_flag = non_manual_law_change # No prestige loss
	}
}

pf_lower_city_obligations_effect = {
	primary_title = {
		show_scope_change = no

		set_title_flag = non_manual_law_change # No prestige loss

		trigger_switch = {
			on_trigger = has_law

			city_obligations_1 = { add_law_w_cooldown = city_obligations_0 }
			city_obligations_2 = { add_law_w_cooldown = city_obligations_1 }
			city_obligations_3 = { add_law_w_cooldown = city_obligations_2 }
			city_obligations_4 = { add_law_w_cooldown = city_obligations_3 }
		}

		clr_title_flag = non_manual_law_change # No prestige loss
	}
}

pf_lower_temple_obligations_effect = {
	primary_title = {
		show_scope_change = no

		set_title_flag = non_manual_law_change # No prestige loss

		trigger_switch = {
			on_trigger = has_law

			temple_obligations_1 = { add_law_w_cooldown = temple_obligations_0 }
			temple_obligations_2 = { add_law_w_cooldown = temple_obligations_1 }
			temple_obligations_3 = { add_law_w_cooldown = temple_obligations_2 }
			temple_obligations_4 = { add_law_w_cooldown = temple_obligations_3 }
		}

		clr_title_flag = non_manual_law_change # No prestige loss
	}
}

pf_lower_TO_effect = {
	primary_title = {
		show_scope_change = no

		set_title_flag = non_manual_law_change # No prestige loss

		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

				trigger_switch = {
					on_trigger = has_law

					tribal_organization_1 = { add_law_w_cooldown = tribal_organization_0 }
					tribal_organization_2 = { add_law_w_cooldown = tribal_organization_1 }
					tribal_organization_3 = { add_law_w_cooldown = tribal_organization_2 }
					tribal_organization_4 = { add_law_w_cooldown = tribal_organization_3 }
				}
			}
		}

		clr_title_flag = non_manual_law_change # No prestige loss
	}
}

pf_lower_CA_effect = {
	any_demesne_title = {
		show_scope_change = no

		limit = {
			higher_real_tier_than = DUKE

			OR = {
				has_law = crown_authority_1
				has_law = crown_authority_2
				has_law = crown_authority_3
				has_law = crown_authority_4
			}
		}

		set_title_flag = non_manual_law_change # No prestige loss

		custom_tooltip = {
			text = CA_LOWERED

			trigger_switch = {
				on_trigger = has_law

				crown_authority_1 = { add_law = crown_authority_0 }
				crown_authority_2 = { add_law = crown_authority_1 }
				crown_authority_3 = { add_law = crown_authority_2 }
				crown_authority_4 = { add_law = crown_authority_3 }
			}
		}

		clr_title_flag = non_manual_law_change # No prestige loss
	}

	hidden_effect = {
		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_effect = {
		# Remove prior loyalist/neutral TOMs, if any
		if = {
			limit = { has_opinion_modifier = { who = liege name = pf_loyal_toward } }
			remove_opinion = { who = liege name = pf_loyal_toward }
			reverse_remove_opinion = { who = liege name = pf_loyal_to_me }
		}

		if = {
			limit = { has_opinion_modifier = { who = liege name = pf_neutral_toward } }
			remove_opinion = { who = liege name = pf_neutral_toward }
			reverse_remove_opinion = { who = liege name = pf_neutral_to_me }
		}

		if = {
			limit = { NOT = { has_opinion_modifier = { who = liege name = revolting_against } } }
			opinion = { who = liege name = revolting_against }
		}

		if = {
			limit = { NOT = { reverse_has_opinion_modifier = { who = liege name = revolting_against_me } } }
			reverse_opinion = { who = liege name = revolting_against_me }
		}
	}
}

pf_teardown_rebel_effect = {
	remove_trait = pf_rebel

	hidden_effect = {
		any_opinion_modifier_target = { # Pre-revolt liege
			limit = { reverse_has_opinion_modifier = { who = PREV name = revolting_against } }
			reverse_remove_opinion = { who = PREV name = revolting_against }
			remove_opinion = { who = PREV name = revolting_against_me }
		}
	}
}

pf_teardown_loyalist_effect = {
	remove_trait = pf_loyalist

	hidden_effect = {
		any_opinion_modifier_target = { # Liege or previous liege
			limit = { reverse_has_opinion_modifier = { who = PREV name = pf_loyal_toward } }
			reverse_remove_opinion = { who = PREV name = pf_loyal_toward }
			remove_opinion = { who = PREV name = pf_loyal_to_me }
		}
	}
}

pf_teardown_neutral_effect = {
	remove_trait = pf_neutral

	hidden_effect = {
		any_opinion_modifier_target = { # Liege or previous liege
			limit = { reverse_has_opinion_modifier = { who = PREV name = pf_neutral_toward } }
			reverse_remove_opinion = { who = PREV name = pf_neutral_toward }
			remove_opinion = { who = PREV name = pf_neutral_to_me }
		}
	}
}

# Should be used in the scope of the faction leader
pf_punish_deceitful_player_effect = {
	hidden_effect = {
		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
					is_incapable = no

					NOT = {
						has_opinion_modifier = { who = PREV name = opinion_coerced_into_leaving_faction }
					}
				}

				letter_event = { id = PlusFaction.416 }
			}
		}
	}
}

pf_civil_war_clear_old_opinions_effect = {
	hidden_effect = {
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV name = opinion_civil_war_loser } }
			reverse_remove_opinion = { who = PREV name = opinion_civil_war_loser }
		}
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV name = opinion_civil_war_supporter } }
			reverse_remove_opinion = { who = PREV name = opinion_civil_war_supporter }
		}
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV name = opinion_civil_war_neutral } }
			reverse_remove_opinion = { who = PREV name = opinion_civil_war_neutral }
		}
	}
}

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

			# Clear neutral/loyalist TOM's
			any_opinion_modifier_target = {
				limit = { reverse_has_opinion_modifier = { who = PREV name = pf_loyal_to_me } }
				remove_trait = pf_loyalist
				reverse_remove_opinion = { who = PREV name = pf_loyal_to_me }
				remove_opinion = { who = PREV name = pf_loyal_toward }
			}
			any_opinion_modifier_target = {
				limit = { reverse_has_opinion_modifier = { who = PREV name = pf_neutral_to_me } }
				remove_trait = pf_neutral
				reverse_remove_opinion = { who = PREV name = pf_neutral_to_me }
				remove_opinion = { who = PREV name = 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
			# disband_event_forces = faction_loyalists
		}
		# any_attacker = { disband_event_forces = faction_revolters }
	}
}

pf_liege_change_effect = {
	hidden_effect = {
		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 name = pf_loyal_toward } }
				}
			}
			pf_teardown_loyalist_effect = yes
		}

		if = {
			limit = {
				trait = pf_neutral

				OR = {
					is_ruler = no
					NOT = { has_opinion_modifier = { who = liege name = 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_effect = {
		any_de_jure_vassal = { # All rulers with land under this de jure title
			limit = {
				# Who have civil war state
				is_playable = yes
				pf_war_trait_trigger = yes

				# Who are in pf_lc_enemy's subrealm under the title PREV
				OR = {
					character = event_target:pf_lc_enemy
					is_vassal_or_below_of = event_target:pf_lc_enemy
				}
			}

			# Add temporary TOM from pulse province owner to tracked ruler
			event_target:pulse_province = {
				owner = {
					if = {
						limit = {
							NOT = {
								has_opinion_modifier = {
									name = pf_lcut_ruler
									who = PREVPREV
								}
							}
						}

						opinion = {
							name = pf_lcut_ruler
							who = PREVPREV
						}
					}
				}
			}
		}

		# 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 = {
				# Who have civil war defender state
				is_playable = yes

				any_war = {
					defender = { character = PREVPREV }

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

				# Who are in pf_lc_enemy's subrealm under the title PREV
				OR = {
					character = event_target:pf_lc_enemy
					is_vassal_or_below_of = event_target:pf_lc_enemy
				}
			}

			any_opinion_modifier_target = { # Revolters
				limit = {
					reverse_has_opinion_modifier = {
						name = revolting_against_me
						who = PREV
					}
				}

				# Add temporary TOM from pulse province owner to tracked ruler
				event_target:pulse_province = {
					owner = {
						if = {
							limit = {
								NOT = {
									has_opinion_modifier = {
										name = pf_lcut_ruler
										who = PREVPREV
									}
								}
							}

							opinion = {
								name = pf_lcut_ruler
								who = PREVPREV
							}
						}
					}
				}
			}

			any_opinion_modifier_target = { # Neutrals
				limit = {
					reverse_has_opinion_modifier = {
						name = pf_neutral_to_me
						who = PREV
					}
				}

				# Add temporary TOM from pulse province owner to tracked ruler
				event_target:pulse_province = {
					owner = {
						if = {
							limit = {
								NOT = {
									has_opinion_modifier = {
										name = pf_lcut_ruler
										who = PREVPREV
									}
								}
							}

							opinion = {
								name = pf_lcut_ruler
								who = PREVPREV
							}
						}
					}
				}
			}

			any_opinion_modifier_target = { # Loyalists
				limit = {
					reverse_has_opinion_modifier = {
						name = pf_loyal_to_me
						who = PREV
					}
				}

				# Add temporary TOM from pulse province owner to tracked ruler
				event_target:pulse_province = {
					owner = {
						if = {
							limit = {
								NOT = {
									has_opinion_modifier = {
										name = pf_lcut_ruler
										who = PREVPREV
									}
								}
							}

							opinion = {
								name = pf_lcut_ruler
								who = PREVPREV
							}
						}
					}
				}
			}
		}
	}
}

pf_liege_change_under_title_end_effect = {
	hidden_effect = {
		event_target:pulse_province = {
			owner = {
				any_opinion_modifier_target = { # Tracked rulers
					limit = {
						reverse_has_opinion_modifier = {
							name = pf_lcut_ruler
							who = PREV
						}
					}

					reverse_remove_opinion = {
						name = pf_lcut_ruler
						who = PREV
					}

					pf_liege_change_effect = yes
				}
			}
		}
	}
}

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

pf_join_leader_effect = {
	if = {
		limit = { event_target:pf_leader = { leads_faction = faction_court } }

		join_faction = {
			faction = faction_court
			faction_leader = event_target:pf_leader
			ignore_requirements = yes
		}
	}
	else_if = {
		limit = { event_target:pf_leader = { leads_faction = faction_glory } }

		join_faction = {
			faction = faction_glory
			faction_leader = event_target:pf_leader
			ignore_requirements = yes
		}
	}
	else_if = {
		limit = { event_target:pf_leader = { leads_faction = faction_prosperity } }

		join_faction = {
			faction = faction_prosperity
			faction_leader = event_target:pf_leader
			ignore_requirements = yes
		}
	}
	else_if = {
		limit = { event_target:pf_leader = { leads_faction = faction_tradition } }

		join_faction = {
			faction = faction_tradition
			faction_leader = event_target:pf_leader
			ignore_requirements = yes
		}
	}
}