
namespace = PlusFaction

#reserved: PlusFaction.200 to PlusFaction.299

##################################
# FACTION MAINTENANCE EVENTS
# Original Faction System by Wiz
# Re-Written by Rylock
##################################

# PlusFaction.200 [Liege]
# On-demand faction maintenance event
character_event = {
	id = PlusFaction.200

	is_triggered_only = yes
	hide_window = yes
	
	only_rulers = yes

	trigger = {
		higher_tier_than = COUNT
		is_nomadic = no
		is_landed = yes
		any_vassal = { pf_member_trigger = yes }
	}

	immediate = {
		character_event = { id = PlusFaction.221 } # setup effects

		#make any one-time adjustments to faction opinions (fired from events & decisions)
		any_vassal = {
			limit = {
				pf_leader_trigger = yes
				check_variable = { which = "faction_mood_increase" value = 0.1 }
			}
			character_event = { id = PlusFaction.223 } #one-time faction opinion increase
			set_variable = { which = "faction_mood_increase" value = 0 }
		}
		any_vassal = {
			limit = {
				pf_leader_trigger = yes
				check_variable = { which = "faction_mood_decrease" value = 0.1 }
			}
			character_event = { id = PlusFaction.224 } #one-time faction opinion decrease
			set_variable = { which = "faction_mood_decrease" value = 0 }
		}

		character_event = { id = PlusFaction.222 } # completion effects
	}
}

# PlusFaction.201 [Liege & His Court Faction Members]
# Notification of court faction mood change
character_event = {
	id = PlusFaction.201
	picture = GFX_evt_council
	
	is_triggered_only = yes
	notification = yes
	hide_from = yes
	
	desc = {
		text = EVTDESC_PlusFaction_201_Happy
		trigger = {
			FROM = { has_character_flag = faction_court_happy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_201_Neutral
		trigger = {
			FROM = {
				NOR = {
					has_character_flag = faction_court_happy
					has_character_flag = faction_court_unhappy
					has_character_flag = faction_court_angry
				}
			}
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_201_Unhappy
		trigger = {
			FROM = { has_character_flag = faction_court_unhappy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_201_Angry
		trigger = {
			FROM = { has_character_flag = faction_court_angry }
		}
	}
	
	option = {
		name = "OK"
	}
}

# PlusFaction.202 [Liege & His Prosperity Faction Members]
# Notification of prosperity faction mood change
character_event = {
	id = PlusFaction.202
	picture = GFX_evt_council
	
	is_triggered_only = yes
	notification = yes
	hide_from = yes
	
	desc = {
		text = EVTDESC_PlusFaction_202_Happy
		trigger = {
			FROM = { has_character_flag = faction_prosperity_happy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_202_Neutral
		trigger = {
			FROM = {
				NOR = {
					has_character_flag = faction_prosperity_happy
					has_character_flag = faction_prosperity_unhappy
					has_character_flag = faction_prosperity_angry
				}
			}
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_202_Unhappy
		trigger = {
			FROM = { has_character_flag = faction_prosperity_unhappy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_202_Angry
		trigger = {
			FROM = { has_character_flag = faction_prosperity_angry }
		}
	}
	
	option = {
		name = "OK"
	}
}

# PlusFaction.203 [Liege & His Glory Faction Members]
# Notification of glory faction mood change
character_event = {
	id = PlusFaction.203
	picture = GFX_evt_council
	
	is_triggered_only = yes
	notification = yes
	hide_from = yes
	
	desc = {
		text = EVTDESC_PlusFaction_203_Happy
		trigger = {
			FROM = { has_character_flag = faction_glory_happy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_203_Neutral
		trigger = {
			FROM = {
				NOR = {
					has_character_flag = faction_glory_happy
					has_character_flag = faction_glory_unhappy
					has_character_flag = faction_glory_angry
				}
			}
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_203_Unhappy
		trigger = {
			FROM = { has_character_flag = faction_glory_unhappy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_203_Angry
		trigger = {
			FROM = { has_character_flag = faction_glory_angry }
		}
	}
	
	option = {
		name = "OK"
	}
}

# PlusFaction.204 [Liege & His Tradition Faction Members]
# Notification of tradition faction mood change
character_event = {
	id = PlusFaction.204
	picture = GFX_evt_council
	
	is_triggered_only = yes
	notification = yes
	hide_from = yes
	
	desc = {
		text = EVTDESC_PlusFaction_204_Happy
		trigger = {
			FROM = { has_character_flag = faction_tradition_happy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_204_Neutral
		trigger = {
			FROM = {
				NOR = {
					has_character_flag = faction_tradition_happy
					has_character_flag = faction_tradition_unhappy
					has_character_flag = faction_tradition_angry
				}
			}
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_204_Unhappy
		trigger = {
			FROM = { has_character_flag = faction_tradition_unhappy }
		}
	}
	desc = {
		text = EVTDESC_PlusFaction_204_Angry
		trigger = {
			FROM = { has_character_flag = faction_tradition_angry }
		}
	}
	
	option = {
		name = "OK"
	}
}

# PlusFaction.205 [Dying Liege and/or Revolter]
# Faction maintenance on character death
character_event = {
	id = PlusFaction.205

	is_triggered_only = yes
	hide_window = yes

	only_playable = yes

	trigger = {
		OR = {
			pf_war_trait_trigger = yes
			any_war = {
				defender = { character = ROOT }
				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
				}
			}
			any_vassal = { pf_member_trigger = yes }
		}
	}

	immediate = {
		if = {
			limit = { is_vice_royalty = yes }
			capital_scope = {
				county = {
					current_heir = {
						save_event_target_as = heir
					}
				}
			}
		}
		if = {
			limit = {
				is_vice_royalty = no
				current_heir = { is_alive = yes }
			}
			current_heir = {
				save_event_target_as = heir
			}
		}
		if = {
			limit = {
				is_vice_royalty = no
				NOT = { current_heir = { is_alive = yes } }
			}
			save_event_target_as = heir
		}
		
		if = {
			limit = {
				OR = {
					higher_real_tier_than = event_target:heir
					event_target:heir = { is_ruler = no }
				}
			}
			#reset heir's faction variables
			event_target:heir = {
				pf_reset_maintenance_vars_effect = yes
				clr_character_flag = faction_court_happy
				clr_character_flag = faction_court_unhappy
				clr_character_flag = faction_court_angry
				clr_character_flag = faction_prosperity_happy
				clr_character_flag = faction_prosperity_unhappy
				clr_character_flag = faction_prosperity_angry
				clr_character_flag = faction_glory_happy
				clr_character_flag = faction_glory_unhappy
				clr_character_flag = faction_glory_angry
				clr_character_flag = faction_tradition_happy
				clr_character_flag = faction_tradition_unhappy
				clr_character_flag = faction_tradition_angry
			}
			#pass on faction mood flags
			event_target:heir = {
				if = {
					limit = { ROOT = { has_character_flag = faction_court_happy } }
					set_character_flag = faction_court_happy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_court_unhappy } }
					set_character_flag = faction_court_unhappy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_court_angry } }
					set_character_flag = faction_court_angry
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_prosperity_happy } }
					set_character_flag = faction_prosperity_happy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_prosperity_unhappy } }
					set_character_flag = faction_prosperity_unhappy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_prosperity_angry } }
					set_character_flag = faction_prosperity_angry
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_glory_happy } }
					set_character_flag = faction_glory_happy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_glory_unhappy } }
					set_character_flag = faction_glory_unhappy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_glory_angry } }
					set_character_flag = faction_glory_angry
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_tradition_happy } }
					set_character_flag = faction_tradition_happy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_tradition_unhappy } }
					set_character_flag = faction_tradition_unhappy
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_tradition_angry } }
					set_character_flag = faction_tradition_angry
				}
			}
		}
		
		# If we have these, we don't want them to persist after death
		pf_teardown_loyalist_effect = yes
		pf_teardown_neutral_effect = yes
		
		if = {
			limit = {
				NOR = {
					any_war = {
						defender = { character = ROOT }
						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
						}
					}
					trait = pf_rebel
				}
			}
			break = yes
		}
		
		log = "REVOLT-RELATED DEATH: [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] (ID: [This.GetID]):"
		log = "<<<<<"
		
		event_target:heir = {
			if = {
				limit = { NOT = { character = ROOT } }
				if = {
					limit = { is_ruler = yes }
					log = "heir is already a ruler: [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] (ID: [This.GetID])"
				}
				if = {
					limit = { is_ruler = no }
					log = "heir is NOT yet a ruler: [This.GetTitledName] (ID: [This.GetID])"
				}
			}
			if = {
				limit = { character = ROOT }
				log = "dying ruler has no known heir"
			}
		}
		
		if = { # Are we a civil war liege (defender) that is allowed to pass on our revolt to someone?
			limit = {
				any_war = {
					defender = { character = ROOT }
 					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
 					}
				}
				# Our liege cannot also be our heir if the revolt is to continue, because the revolt leader's
				# temporary liege (i.e., NOT the liege_before_war) for the revolt cannot also be the target
				# of the revolt. This would otherwise violate the premises of the hard-coded revolt title
				# system, although if Paradox wanted, they could handle this more gracefully by elevating the
				# revolt title to a yet higher tier and revassalizing the revolt title to the liege
				# of the dying ruler's liege/heir (or independent if no such liege).
				NOR = {
					liege = { character = event_target:heir } # aforementioned case
					character = event_target:heir # unknown heir
					is_vice_royalty = yes # viceroys suck at everything
				}
			}
			
			log = "dying ruler is a civil war liege (defending against civil war)"
			
			if = {
				limit = {
					has_opinion_modifier = { who = event_target:heir modifier = revolting_against_me }
				}
				event_target:heir = {
					log = "heir was revolting against [Root.GetHerHim]... resetting [heir.GetTitledFirstName] AND will try vassalizing [heir.GetHerHim]"
					pf_teardown_rebel_effect = yes
					set_defacto_liege = ROOT
				}
			}
			
			event_target:heir = {
				if = {
					limit = { ROOT = { has_character_flag = faction_court_war } }
					set_character_flag = faction_court_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_prosperity_war } }
					set_character_flag = faction_prosperity_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_glory_war } }
					set_character_flag = faction_glory_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_tradition_war } }
					set_character_flag = faction_tradition_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_separatist_war } }
					set_character_flag = faction_separatist_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_pretender_war } }
					set_character_flag = faction_pretender_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_religious_war } }
					set_character_flag = faction_religious_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_succession_war } }
					set_character_flag = faction_succession_war
				}
				if = {
					limit = { ROOT = { has_character_flag = faction_authority_war } }
					set_character_flag = faction_authority_war
				}
			}
			
			# Note liege change for our civil wars
			any_war = {
				limit = {
					defender = { character = ROOT }
 					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
 					}
				}
				attacker = {
					if = {
						limit = { has_character_flag = original_war_liege }
						log = "clearing original liege status for civil war (rebel success will be less dangerous)"
					}
					clr_character_flag = original_war_liege
				}
			}
			
			# Transfer revolt TOMs
			any_opinion_modifier_target = { # Those revolting against me
				limit = {
					is_alive = yes
					reverse_has_opinion_modifier = { who = PREV modifier = revolting_against_me }
					NOR = {
						character = event_target:heir
						reverse_has_opinion_modifier = {
							who = event_target:heir
							modifier = revolting_against_me
						}
						has_opinion_modifier = {
							who = event_target:heir
							modifier = revolting_against
						}
					}
				}
				reverse_opinion = { who = event_target:heir modifier = revolting_against_me }
				opinion = { who = event_target:heir modifier = revolting_against }
				reverse_remove_opinion = { who = ROOT modifier = revolting_against_me }
				remove_opinion = { who = ROOT modifier = revolting_against }
			}
			
			# Transfer loyalist/neutral TOMs
			any_vassal = { # Those loyal to me
				limit = {
					trait = pf_loyalist
					NOR = {
						character = event_target:heir
						reverse_has_opinion_modifier = {
							who = event_target:heir
							modifier = pf_loyal_to_me
						}
						has_opinion_modifier = {
							who = event_target:heir
							modifier = pf_loyal_toward
						}
					}
				}
				reverse_opinion = { who = event_target:heir modifier = pf_loyal_to_me }
				opinion = { who = event_target:heir modifier = pf_loyal_toward }
				reverse_remove_opinion = { who = ROOT modifier = pf_loyal_to_me }
				remove_opinion = { who = ROOT modifier = pf_loyal_toward }
			}
			any_vassal = { # Those neutral to me
				limit = {
					trait = pf_neutral
					NOR = {
						character = event_target:heir
						reverse_has_opinion_modifier = {
							who = event_target:heir
							modifier = pf_neutral_to_me
						}
						has_opinion_modifier = {
							who = event_target:heir
							modifier = pf_neutral_toward
						}
					}
				}
				reverse_opinion = { who = event_target:heir modifier = pf_neutral_to_me }
				opinion = { who = event_target:heir modifier = pf_neutral_toward }
				reverse_remove_opinion = { who = ROOT modifier = pf_neutral_to_me }
				remove_opinion = { who = ROOT modifier = pf_neutral_toward }
			}
		}
		
		if = { # Are we a civil war liege (defender) that is NOT allowed to pass on our revolt to someone?
			limit = {
				any_war = {
					defender = { character = ROOT }
 					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
 					}
				}
				OR = {
					liege = { character = event_target:heir }
					character = event_target:heir
					is_vice_royalty = yes
				}
			}
			if = {
				limit = {
					liege = {
						character = event_target:heir
						NOT = { character = ROOT }
					}
				}
				log = "dying ruler is a civil war liege, but [Root.GetHerHis] heir is also [Root.GetHerHis] liege, so the revolt should invalidate."
			}
			if = {
				limit = { character = event_target:heir }
				log = "dying ruler is a civil war liege, but [Root.GetSheHe] has no known successor, so the revolt should invalidate."
			}
			if = {
				limit = { is_vice_royalty = yes }
				log = "dying ruler is a civil war liege, but [Root.GetSheHe] is also a viceroy, so the revolt should invalidate."
			}
			any_war = {
				limit = {
					defender = { character = ROOT }
 					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
 					}
				}
				end_war = invalid # civil war state will be cleaned-up gracefully, because the war defender is [barely] still alive.
			}
		}

		if = {
			limit = { NOT = { trait = pf_rebel } }
			log = ">>>>>"
			break = yes
		}
		
		# We're supposed to be involved in a PF revolt (as a revolter), if we made it here.
		
		log = "dying ruler is a revolter"
		
		random_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV modifier = revolting_against } }
			save_event_target_as = pf_war_liege
		}
		
		if = {
			limit = {
				event_target:pf_war_liege = {
					event_target:heir = {
						NOR = {
							character = PREV
							character = ROOT
							is_liege_of = ROOT
							ROOT = { is_vice_royalty = yes }
							AND = {
								trait = pf_rebel
								any_opinion_modifier_target = {
									is_alive = yes
									reverse_has_opinion_modifier = {
										who = PREV
										modifier = revolting_against
									}
									NOT = { character = PREVPREV }
								}
							}
							AND = {
								OR = {
									trait = pf_loyalist
									trait = pf_neutral
								}
								vassal_of = PREV
							}
						}
					}
				}
			}
			
			log = "dying revolter's heir SHOULD inherit [Root.GetHerHis] revolt"
			
			event_target:heir = {
				if = {
					limit = { trait = pf_loyalist }
					log = "clearing heir's unrelated loyalist status"
					remove_opinion = { who = LIEGE modifier = pf_loyal_toward }
					reverse_remove_opinion = { who = LIEGE modifier = pf_loyal_to_me }
				}
				if = {
					limit = { trait = pf_neutral }
					log = "clearing heir's unrelated neutral status"
					remove_opinion = { who = LIEGE modifier = pf_neutral_toward }
					reverse_remove_opinion = { who = LIEGE modifier = pf_neutral_to_me }
				}
				add_trait = pf_rebel
			}
			
			if = {
				limit = {
					any_war = {
						attacker = { character = ROOT }
						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
						}
					}
				}
				
				log = "dying revolter is a rebel leader"
				
				# Inherit original_war_liege if we have it.
				if = {
					limit = { has_character_flag = original_war_liege }
					event_target:heir = { set_character_flag = original_war_liege }
					log = "heir will inherit rebel leader's original_war_liege status (the proper liege hasn't changed during the war)"
				}
				
				# Inherit demands flags
				event_target:heir = {
					if = {
						limit = { ROOT = { has_character_flag = demands_lower_CA } }
						set_character_flag = demands_lower_CA
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_lower_TO } }
						set_character_flag = demands_lower_TO
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_surrender_title } }
						set_character_flag = demands_surrender_title
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_share_wealth } }
						set_character_flag = demands_share_wealth
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_revert_succession } }
						set_character_flag = demands_revert_succession
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_council_life_terms } }
						set_character_flag = demands_council_life_terms
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_lower_feudal_obligations } }
						set_character_flag = demands_lower_feudal_obligations
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_lower_tribal_obligations } }
						set_character_flag = demands_lower_tribal_obligations
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_lower_city_obligations } }
						set_character_flag = demands_lower_city_obligations
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_lower_temple_obligations } }
						set_character_flag = demands_lower_temple_obligations
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_seniority } }
						set_character_flag = demands_seniority
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_primogeniture } }
						set_character_flag = demands_primogeniture
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_gavelkind } }
						set_character_flag = demands_gavelkind
					}
					if = {
						limit = { ROOT = { has_character_flag = demands_feudal_elective } }
						set_character_flag = demands_feudal_elective
					}
				}
			}
			
			event_target:pf_war_liege = {
				if = {
					limit = {
						NOR = {
							reverse_has_opinion_modifier = {
								who = event_target:heir
								modifier = revolting_against
							}
							has_opinion_modifier = {
								who = event_target:heir
								modifier = revolting_against_me
							}
						}
					}
					reverse_opinion = { who = event_target:heir modifier = revolting_against }
					opinion = { who = event_target:heir modifier = revolting_against_me }
					reverse_remove_opinion = { who = ROOT modifier = revolting_against }
					remove_opinion = { who = ROOT modifier = revolting_against_me }
				}
			}
			
			log = ">>>>>"
			break = yes
		}
		
		# We should NOT pass-down the revolt, if we made it here.
		
		log = "dying rebel's heir should NOT inherit their revolt. reason(s):"
		
		if = {
			limit = { event_target:pf_war_liege = { character = event_target:heir } }
			log = "-> the dying rebel's proper liege is also his heir"
		}
		if = {
			limit = { character = event_target:heir }
			log = "-> the dying rebel's successor is unknown"
		}
		if = {
			limit = { liege = { character = event_target:heir } }
			log = "-> the dying rebel's current liege is also his heir"
		}
		if = {
			limit = { is_vice_royalty = yes }
			log = "-> the dying rebel is a viceroy, and viceroys can do nothing but drool incessantly"
		}
		if = {
			limit = {
				event_target:pf_war_liege = {
					event_target:heir = {
						trait = pf_rebel
						any_opinion_modifier_target = {
							is_alive = yes
							reverse_has_opinion_modifier = {
								who = PREV
								modifier = revolting_against
							}
							NOT = { character = PREVPREV }
						}
					}
				}
			}
			log = "-> heir is already a rebel and [heir.GetHerHis] proper liege is NOT the same as the dying rebel's"
		}
		if = {
			limit = {
				event_target:pf_war_liege = {
					event_target:heir = {
						OR = {
							trait = pf_loyalist
							trait = pf_neutral
						}
						vassal_of = PREV
					}
				}
			}
			log = "-> heir is already a loyalist/neutral AND [heir.GetHerHis] liege is the dying rebel's proper liege"
		}
		
		# If we're a revolt leader, then we can't pass down the revolt to anyone in this case, so invalidate our
		# rebellion.
		
		if = {
			limit = {
				any_war = {
					attacker = { character = ROOT }
					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
					}
				}
			}
			log = "dying rebel is a rebel leader. since heir shouldn't inherit the revolt, the war must be invalidated."
			log = ">>>>>"
			any_war = {
				limit = {
					attacker = { character = ROOT }
					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
					}
				}
				end_war = invalid
			}
			break = yes
		}
		
		# We're not a revolt leader, if we've made it here.
		
		log = "dying rebel is NOT a rebel leader"
		pf_teardown_rebel_effect = yes
		log = ">>>>>"
	}
}

# PlusFaction.211 [Granted Title]
# GRANT_LANDED_TITLE_INTERACTION_ACCEPT_EVENT - updates faction response to a granted title
character_event = {
	id = PlusFaction.211

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		FROM = { character_event = { id = PlusFaction.212 days = 1 } }
	}
}

# PlusFaction.212 [Granter of Title]
# Faction pleased due to title granted to one of their members
#
# FROM = Granted Title
character_event = {
	id = PlusFaction.212

	is_triggered_only = yes
	hide_window = yes
	
	immediate = {
		#save the title and new holder as event targets
		FROM = {
			save_event_target_as = pf_new_title
			holder_scope = { save_event_target_as = pf_new_owner }
		}
		
		#first check to see if the AI shouldn't have been granted a county
		#this is for the anti-border gore events in CK2Plus only
		if = {
			limit = {
				#only applies to AI vassals with multiple counties granted a county by AI lieges
				ai = yes
				event_target:pf_new_owner = {
					ai = yes
					num_of_count_titles = 2
					NOT = { is_primary_heir = ROOT }
				}
				event_target:pf_new_title = {
					tier = COUNT
					NOR = {
						#not within two provinces of a province owned by either the new ruler or one of his vassals
						location = {
							any_neighbor_province = {
								OR = {
									owner = {
										OR = {
											character = event_target:pf_new_owner
											is_liege_or_above = event_target:pf_new_owner
										}
									}
									any_neighbor_province = {
										owner = {
											OR = {
												character = event_target:pf_new_owner
												is_liege_or_above = event_target:pf_new_owner
											}
										}
									}
								}
							}
						}
						#the de jure duchy is held by neither the new ruler or one of his vassals
						location = {
							duchy = {
								holder_scope = {
									OR = {
										character = event_target:pf_new_owner
										is_liege_or_above = event_target:pf_new_owner
									}
								}
							}
						}
						#the de jure kingdom is held by neither the new ruler or one of his vassals
						location = {
							kingdom = {
								holder_scope = {
									OR = {
										character = event_target:pf_new_owner
										is_liege_or_above = event_target:pf_new_owner
									}
								}
							}
						}
					}
				}
			}
			log = "[pf_new_owner.GetBestName] was granted the [pf_new_title.GetFullName] by [Root.GetBestName], but it will be re-assigned."
			reverse_remove_opinion = { who = event_target:pf_new_owner modifier = opinion_granted_county }
			event_target:new_title = {
				grant_title_no_opinion = ROOT
				remove_claim = event_target:pf_new_owner
			}
			character_event = { id = Plus.1755 } #re-assign title
			break = yes
		}

		#then check for any vassals who have pf TOM's
		any_vassal = {
			limit = { is_playable = yes }
			pf_liege_change_effect = yes
		}
		
		#claimants to the granted title who aren't close relatives of the new owner are displeased
		log = "[pf_new_owner.GetBestName] (ID:[pf_new_owner.GetID]) was granted the [pf_new_title.GetFullName] by [Root.GetBestName] (ID:[Root.GetID])"
		event_target:pf_new_title = {
			any_claimant = {
				limit = {
					NOR = {
						character = ROOT
						character = event_target:pf_new_owner
						is_close_relative = event_target:pf_new_owner
					}
					OR = {
						AND = {
							is_female = no
							ROOT = {
								succ_law_title = {
									NOT = { has_law = enatic_succession }
									NOT = { has_law = enatic_cognatic_succession }
								}
							}
						}
						AND = {
							is_female = yes
							ROOT = {
								succ_law_title = {
									NOT = { has_law = agnatic_succession }
									NOT = { has_law = cognatic_succession }
								}
							}
						}
					}
					any_liege = { character = ROOT }
				}
				if = {
					limit = { has_strong_claim = PREV }
					opinion = { who = ROOT modifier = faction_disapprove_title_grant multiplier = 4 years = 10 }
				}
				if = {
					limit = { has_weak_claim = PREV }
					opinion = { who = ROOT modifier = faction_disapprove_title_grant multiplier = 2 years = 10 }
				}
			}
		}
		#members of the same faction as the new holder are pleased, based on the tier of the title
		if = {
			limit = { event_target:pf_new_owner = { in_faction = faction_court } }
			ROOT = {
				any_vassal = {
					limit = {
						NOT = { character = event_target:pf_new_owner }
						in_faction = faction_court
					}
					if = {
						limit = { event_target:pf_new_title = { tier = BARON } }
						opinion = { who = ROOT modifier = faction_approve_title_grant years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = COUNT } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 2 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = DUKE } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 4 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = KING } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 6 years = 3 }
					}
				}
				character_event = { id = PlusFaction.200 days = 1 } #fire maintenance event to recalculate moods
			}
		}
		if = {
			limit = { event_target:pf_new_owner = { in_faction = faction_prosperity } }
			ROOT = {
				any_vassal = {
					limit = {
						NOT = { character = event_target:pf_new_owner }
						in_faction = faction_prosperity
					}
					if = {
						limit = { event_target:pf_new_title = { tier = BARON } }
						opinion = { who = ROOT modifier = faction_approve_title_grant years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = COUNT } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 2 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = DUKE } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 4 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = KING } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 6 years = 3 }
					}
				}
				character_event = { id = PlusFaction.200 days = 1 } #fire maintenance event to recalculate moods
			}
		}
		if = {
			limit = { event_target:pf_new_owner = { in_faction = faction_glory } }
			ROOT = {
				any_vassal = {
					limit = {
						NOT = { character = event_target:pf_new_owner }
						in_faction = faction_glory
					}
					if = {
						limit = { event_target:pf_new_title = { tier = BARON } }
						opinion = { who = ROOT modifier = faction_approve_title_grant years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = COUNT } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 2 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = DUKE } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 4 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = KING } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 6 years = 3 }
					}
				}
				character_event = { id = PlusFaction.200 days = 1 } #fire maintenance event to recalculate moods
			}
		}
		if = {
			limit = { event_target:pf_new_owner = { in_faction = faction_tradition } }
			ROOT = {
				any_vassal = {
					limit = {
						NOT = { character = event_target:pf_new_owner }
						in_faction = faction_tradition
					}
					if = {
						limit = { event_target:pf_new_title = { tier = BARON } }
						opinion = { who = ROOT modifier = faction_approve_title_grant years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = COUNT } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 2 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = DUKE } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 4 years = 3 }
					}
					if = {
						limit = { event_target:pf_new_title = { tier = KING } }
						opinion = { who = ROOT modifier = faction_approve_title_grant multiplier = 6 years = 3 }
					}
				}
				character_event = { id = PlusFaction.200 days = 1 } #fire maintenance event to recalculate moods
			}
		}
	}
}

# Check for an invalidated succession law 
character_event = {
	id = PlusFaction.213
	
	is_triggered_only = yes
	hide_window = yes
	
	immediate = {
		FROM = {
			save_event_target_as = pf_new_title
		
			#check matrilineal marriage & viceroy status
			if = {
				limit = { is_primary_holder_title = yes }
				ROOT = {
					character_event = { id = PlusFaction.216 days = 1 }
					character_event = { id = Plus.3299 days = 1 }
				}
			}
		}
		
		# Check if title has been granted
		# NOTE: Inherited titles always work, and will reset the succession law to be readable even
		# If it's currently broken, so it doesn't matter if FROMFROM is dead and the title inherited
		# as even if it falls to the 'created titles' event below it will still fail at the trigger
		if = {
			limit = { FROMFROM = { is_alive = yes } }
			FROMFROM = { save_event_target_as = pf_old_holder }
			character_event = { id = PlusFaction.214 days = 1 }
			break = yes
		}
		
		# Failing that, fire the event for created titles
		character_event = { id = PlusFaction.215 days = 1 }
	}
}

# For granted titles
character_event = {
	id = PlusFaction.214
	
	is_triggered_only = yes
	hide_window = yes
	
	trigger = {
		event_target:pf_new_title = {
			emf_laws_has_readable_succession_law_trigger = no
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
		}
	}
	
	immediate = {
		# Test the new title vs. the previous holder
		event_target:pf_new_title = { current_inheritance_laws_effect = yes }
		log = "---> Received Title From: [pf_old_holder.GetBestName], Primary Title: [pf_old_holder.PrimaryTitle.GetID]"
		event_target:pf_old_holder = {
			primary_title = {
				if = {
					limit = { has_law = succ_gavelkind }
					log = "---> current succession law for [This.GetID]: Gavelkind"
				}
				if = {
					limit = { has_law = succ_elective_gavelkind }
					log = "---> current succession law for [This.GetID]: Elective Gavelkind"
				}
				if = {
					limit = { has_law = succ_seniority }
					log = "---> current succession law for [This.GetID]: Seniority"
				}
				if = {
					limit = { has_law = succ_primogeniture }
					log = "---> current succession law for [This.GetID]: Primogeniture"
				}
				if = {
					limit = { has_law = succ_feudal_elective }
					log = "---> current succession law for [This.GetID]: Feudal Elective"
				}
				if = {
					limit = { has_law = succ_tanistry }
					log = "---> current succession law for [This.GetID]: Tanistry"
				}
				if = {
					limit = { has_law = succ_ultimogeniture }
					log = "---> current succession law for [This.GetID]: Ultimogeniture"
				}
				if = {
					limit = { has_law = succ_turkish_succession }
					log = "---> current succession law for [This.GetID]: Turkish Succession"
				}
				if = {
					limit = { has_law = succ_papal_succession }
					log = "---> current succession law for [This.GetID]: Papal Succession"
				}
				if = {
					limit = { has_law = succ_appointment }
					log = "---> current succession law for [This.GetID]: Appointment"
				}
				if = {
					limit = { has_law = succ_open_elective }
					log = "---> current succession law for [This.GetID]: Open Elective"
				}
				if = {
					limit = { has_law = succ_catholic_bishopric }
					log = "---> current succession law for [This.GetID]: Bishopric"
				}
				if = {
					limit = { has_law = succ_patrician_elective }
					log = "---> current succession law for [This.GetID]: Patrician Elective"
				}
				if = {
					limit = { has_law = succ_nomad_succession }
					log = "---> current succession law for [This.GetID]: Nomadic"
				}
			}
		}
		# Before anything, take care of some high-priority cases:
		if = {
			limit = { event_target:pf_new_title = { is_law_potential = succ_patrician_elective } }
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Patrician Elective succession (due to base case)"
				add_law = {
					law = succ_patrician_elective
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = { event_target:pf_new_title = { is_law_potential = succ_nomad_succession } }
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Nomad succession (due to base case)"
				add_law = {
					law = succ_nomad_succession
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		# Then attempt to get the succession law from the granting liege
		if = {
			limit = {
				event_target:pf_old_holder = { primary_title = { has_law = succ_gavelkind } }
				event_target:pf_new_title = { is_law_potential = succ_institute_gavelkind }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Gavelkind succession"
				add_law = {
					law = succ_gavelkind
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_old_holder = { primary_title = { has_law = succ_elective_gavelkind } }
				event_target:pf_new_title = { is_law_potential = succ_institute_elective_gavelkind }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Elective Gavelkind succession"
				add_law = {
					law = succ_elective_gavelkind
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_old_holder = { primary_title = { has_law = succ_seniority } }
				event_target:pf_new_title = { is_law_potential = succ_institute_seniority }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Seniority succession"
				add_law = {
					law = succ_seniority
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_old_holder = { primary_title = { has_law = succ_primogeniture } }
				event_target:pf_new_title = { is_law_potential = succ_institute_primogeniture }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Primogeniture succession"
				add_law = {
					law = succ_primogeniture
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_old_holder = { primary_title = { has_law = succ_feudal_elective } }
				event_target:pf_new_title = {
					is_law_potential = succ_institute_feudal_elective
					OR = {
						higher_tier_than = DUKE
						AND = {
							is_primary_holder_title = yes
							tier = DUKE
						}
					}
				}
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Feudal Elective succession"
				add_law = {
					law = succ_feudal_elective
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_old_holder = { primary_title = { has_law = succ_tanistry } }
				event_target:pf_new_title = { is_law_potential = succ_institute_tanistry }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Tanistry succession"
				add_law = {
					law = succ_tanistry
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_old_holder = { primary_title = { has_law = succ_ultimogeniture } }
				event_target:pf_new_title = { is_law_potential = succ_institute_ultimogeniture }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Ultimogeniture succession"
				add_law = {
					law = succ_ultimogeniture
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		# Failing that, use the default
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_gavelkind }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Gavelkind succession (different from previous)"
				add_law = {
					law = succ_gavelkind
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_elective_gavelkind }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Elective Gavelkind succession (different from previous)"
				add_law = {
					law = succ_elective_gavelkind
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_seniority }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Seniority succession (different from previous)"
				add_law = {
					law = succ_seniority
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_primogeniture }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Primogeniture succession (different from previous)"
				add_law = {
					law = succ_primogeniture
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = {
					is_law_potential = succ_institute_feudal_elective
					OR = {
						higher_tier_than = DUKE
						AND = {
							is_primary_holder_title = yes
							tier = DUKE
						}
					}
				}
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Feudal Elective succession (different from previous)"
				add_law = {
					law = succ_feudal_elective
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_tanistry }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Tanistry succession (different from previous)"
				add_law = {
					law = succ_tanistry
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_ultimogeniture }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Ultimogeniture succession (different from previous)"
				add_law = {
					law = succ_ultimogeniture
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_turkish_succession }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Turkish succession (different from previous)"
				add_law = {
					law = succ_turkish_succession
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_papal_succession }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Papal succession (different from previous)"
				add_law = {
					law = succ_papal_succession
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_appointment }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Appointment succession (different from previous)"
				add_law = {
					law = succ_appointment
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_open_elective }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Open Elective succession (different from previous)"
				add_law = {
					law = succ_open_elective
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_catholic_bishopric }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Bishopric succession (different from previous)"
				add_law = {
					law = succ_catholic_bishopric
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		log = "---> ASSERT: Failed to find appropriate succession law for [pf_new_title.GetFullName] ([pf_new_title.GetID])!"
	}
}

# For created titles
character_event = {
	id = PlusFaction.215
	
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
		event_target:pf_new_title = {
			emf_laws_has_readable_succession_law_trigger = no
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
		}
	}
	
	immediate = {
		event_target:pf_new_title = {
			current_inheritance_laws_effect = yes
		}
		log = "---> [pf_new_title.GetID] is a new title created by [Root.GetBestName]"
		
		# Before anything, take care of some high-priority cases:
		if = {
			limit = { event_target:pf_new_title = { is_law_potential = succ_patrician_elective } }
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Patrician Elective succession (due to base case)"
				add_law = {
					law = succ_patrician_elective
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = { event_target:pf_new_title = { is_law_potential = succ_nomad_succession } }
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Nomad succession (due to base case)"
				add_law = {
					law = succ_nomad_succession
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		
		#check for another title that we can get the succession law from
		random_demesne_title = {
			limit = {
				is_primary_holder_title = no
				lower_tier_than = KING
				emf_laws_has_readable_succession_law_trigger = yes
			}
			log = "---> attempting to derive succession law from [This.GetID] (other held title)"
			if = {
				limit = {
					has_law = succ_gavelkind
					event_target:pf_new_title = { is_law_potential = succ_institute_gavelkind }
				}
				event_target:pf_new_title = {
					log = "---> INFO: [This.GetID] has to be manually corrected to Gavelkind succession"
					add_law = {
					law = succ_gavelkind
					cooldown = no
					opinion_effect = no
				}
				}
				break = yes
			}
			if = {
				limit = {
					has_law = succ_elective_gavelkind
					event_target:pf_new_title = { is_law_potential = succ_institute_elective_gavelkind }
				}
				event_target:pf_new_title = {
					log = "---> INFO: [This.GetID] has to be manually corrected to Elective Gavelkind succession"
					add_law = {
					law = succ_elective_gavelkind
					cooldown = no
					opinion_effect = no
				}
				}
				break = yes
			}
			if = {
				limit = {
					has_law = succ_seniority
					event_target:pf_new_title = { is_law_potential = succ_institute_seniority }
				}
				event_target:pf_new_title = {
					log = "---> INFO: [This.GetID] has to be manually corrected to Seniority succession"
					add_law = {
					law = succ_seniority
					cooldown = no
					opinion_effect = no
				}
				}
				break = yes
			}
			if = {
				limit = {
					has_law = succ_primogeniture
					event_target:pf_new_title = { is_law_potential = succ_institute_primogeniture }
				}
				event_target:pf_new_title = {
					log = "---> INFO: [This.GetID] has to be manually corrected to Primogeniture succession"
					add_law = {
					law = succ_primogeniture
					cooldown = no
					opinion_effect = no
				}
				}
				break = yes
			}
			if = {
				limit = {
					has_law = succ_feudal_elective
					event_target:pf_new_title = {
						is_law_potential = succ_institute_feudal_elective
						OR = {
							higher_tier_than = DUKE
							AND = {
								is_primary_holder_title = yes
								tier = DUKE
							}
						}
					}
				}
				event_target:pf_new_title = {
					log = "---> INFO: [This.GetID] has to be manually corrected to Feudal Elective succession"
					add_law = {
						law = succ_feudal_elective
						cooldown = no
						opinion_effect = no
					}
				}
				break = yes
			}
			if = {
				limit = {
					has_law = succ_tanistry
					event_target:pf_new_title = { is_law_potential = succ_institute_tanistry }
				}
				event_target:pf_new_title = {
					log = "---> INFO: [This.GetID] has to be manually corrected to Tanistry succession"
					add_law = {
						law = succ_tanistry
						cooldown = no
						opinion_effect = no
					}
				}
				break = yes
			}
			if = {
				limit = {
					has_law = succ_ultimogeniture
					event_target:pf_new_title = { is_law_potential = succ_institute_ultimogeniture }
				}
				event_target:pf_new_title = {
					log = "---> INFO: [This.GetID] has to be manually corrected to Ultimogeniture succession"
					add_law = {
						law = succ_ultimogeniture
						cooldown = no
						opinion_effect = no
					}
				}
				break = yes
			}
		}
		
		# Failing that, use the default
		log = "---> no title from which to derive succession law (using default)"
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_gavelkind }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Gavelkind succession"
				add_law = {
					law = succ_gavelkind
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_elective_gavelkind }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Elective Gavelkind succession"
				add_law = {
					law = succ_gavelkind
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_seniority }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Seniority succession"
				add_law = {
					law = succ_seniority
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_primogeniture }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Primogeniture succession"
				add_law = {
					law = succ_primogeniture
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_feudal_elective }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Feudal Elective succession"
				add_law = {
					law = succ_feudal_elective
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_tanistry }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Tanistry succession"
				add_law = {
					law = succ_tanistry
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_institute_ultimogeniture }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Ultimogeniture succession"
				add_law = {
					law = succ_ultimogeniture
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_turkish_succession }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Turkish succession"
				add_law = {
					law = succ_turkish_succession
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_papal_succession }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Papal succession"
				add_law = {
					law = succ_papal_succession
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_appointment }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Appointment succession"
				add_law = {
					law = succ_appointment
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_open_elective }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Open Elective succession"
				add_law = {
					law = succ_open_elective
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		if = {
			limit = {
				event_target:pf_new_title = { is_law_potential = succ_catholic_bishopric }
			}
			event_target:pf_new_title = {
				log = "---> INFO: [This.GetID] has to be manually corrected to Bishopric succession"
				add_law = {
					law = succ_catholic_bishopric
					cooldown = no
					opinion_effect = no
				}
			}
			break = yes
		}
		log = "---> ASSERT: Failed to find appropriate succession law for [pf_new_title.GetFullName] ([pf_new_title.GetID])!"
	}
}

# Update matrilineal marriage status for created titles
character_event = {
	id = PlusFaction.216
	
	is_triggered_only = yes
	hide_window = yes
	
	immediate = {	
		primary_title = {
			if = {
				limit = {
					has_law = agnatic_succession
					NOT = { has_law = female_council_law_2 }
				}
				set_allows_matrilineal_marriage = no
			}
			if = {
				limit = {
					OR = {
						NOT = { has_law = agnatic_succession }
						has_law = female_council_law_2
					}
				}
				set_allows_matrilineal_marriage = yes
			}
		}
	}
}

# PlusFaction.220 [Liege]
#
# Annual faction maintenance event <MTTH>

character_event = {
	id = PlusFaction.220

	hide_window = yes
	is_triggered_only = yes

	only_playable = yes
	only_rulers = yes
	
	trigger = {
		higher_tier_than = COUNT
		is_nomadic = no
		is_landed = yes
		any_vassal = { pf_member_trigger = yes }
	}

	immediate = {
		character_event = { id = PlusFaction.221 } # setup effects

		#remove any [invalid] faction_mood increase/decrease on non-faction-leaders
		if = {
			limit = {
				pf_leader_trigger = no
				OR = {
					check_variable = { which = "faction_mood_increase" value = 0.1 }
					check_variable = { which = "faction_mood_decrease" value = 0.1 }
				}
			}
			set_variable = { which = "faction_mood_increase" value = 0 }
			set_variable = { which = "faction_mood_decrease" value = 0 }
		}
		any_vassal = {
			limit = {
				pf_leader_trigger = no
				OR = {
					check_variable = { which = "faction_mood_increase" value = 0.1 }
					check_variable = { which = "faction_mood_decrease" value = 0.1 }
				}
			}
			set_variable = { which = "faction_mood_increase" value = 0 }
			set_variable = { which = "faction_mood_decrease" value = 0 }
		}

		# In the future, we will support execution of deferred faction mood impulse
		# maintenance here: ---->   <------

		## ANNUAL MAINTENANCE CORE ##

		# Crown Authority
		any_vassal = {
			# x2 to prevent all "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_approve_CA }
			remove_opinion = { who = ROOT modifier = faction_approve_CA }

			# x5 to prevent all "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_disapprove_CA }
			remove_opinion = { who = ROOT modifier = faction_disapprove_CA }
			remove_opinion = { who = ROOT modifier = faction_disapprove_CA }
			remove_opinion = { who = ROOT modifier = faction_disapprove_CA }
			remove_opinion = { who = ROOT modifier = faction_disapprove_CA }
		}
		if = {
			limit = {
				is_tribal = no
				is_nomadic = no # For completeness only
				any_demesne_title = { is_crown_law_title = yes }
			}
			#court faction disapproves the most of high CA, with court factions in the HRE being even more extreme
			any_vassal = {
				limit = { in_faction = faction_court }
				if = {
					limit = { crownlaw_title = { holder = ROOT has_law = crown_authority_0 } }
					opinion = { who = ROOT modifier = faction_approve_CA months = 14 }
				}
				if = {
					limit = {
						ROOT = { NOT = { has_landed_title = e_hre } }
						crownlaw_title = { holder = ROOT has_law = crown_authority_1 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA months = 14 }
				}
				if = {
					limit = {
						ROOT = { has_landed_title = e_hre }
						crownlaw_title = { holder = ROOT has_law = crown_authority_1 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 2 months = 14 }
				}
				if = {
					limit = {
						ROOT = { NOT = { has_landed_title = e_hre } }
						crownlaw_title = { holder = ROOT has_law = crown_authority_2 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA months = 14 }
				}
				if = {
					limit = {
						ROOT = { has_landed_title = e_hre }
						crownlaw_title = { holder = ROOT has_law = crown_authority_2 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 3 months = 14 }
				}
				if = {
					limit = {
						ROOT = { NOT = { has_landed_title = e_hre } }
						crownlaw_title = { holder = ROOT has_law = crown_authority_3 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 2 months = 14 }
				}
				if = {
					limit = {
						ROOT = { has_landed_title = e_hre }
						crownlaw_title = { holder = ROOT has_law = crown_authority_3 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 4 months = 14 }
				}
				if = {
					limit = {
						ROOT = { NOT = { has_landed_title = e_hre } }
						crownlaw_title = { holder = ROOT has_law = crown_authority_4 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 3 months = 14 }
				}
				if = {
					limit = {
						ROOT = { has_landed_title = e_hre }
						crownlaw_title = { holder = ROOT has_law = crown_authority_4 }
					}
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 5 months = 14 }
				}
			}
			#prosperity faction approves of high CA (more order in the kingdom)
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				if = {
					limit = { crownlaw_title = { holder = ROOT has_law = crown_authority_0 } }
					opinion = { who = ROOT modifier = faction_disapprove_CA months = 14 }
				}
				if = {
					limit = { crownlaw_title = { holder = ROOT has_law = crown_authority_3 } }
					opinion = { who = ROOT modifier = faction_approve_CA months = 14 }
				}
				if = {
					limit = { crownlaw_title = { holder = ROOT has_law = crown_authority_4 } }
					opinion = { who = ROOT modifier = faction_approve_CA multiplier = 2 months = 14 }
				}
			}
			#glory and tradition factions disapprove of high CA, but only in the HRE
			any_vassal = {
				limit = {
					ROOT = { has_landed_title = e_hre }
					OR = {
						in_faction = faction_glory
						in_faction = faction_tradition
					}
				}
				if = {
					limit = { crownlaw_title = { holder = ROOT has_law = crown_authority_2 } }
					opinion = { who = ROOT modifier = faction_disapprove_CA months = 14 }
				}
				if = {
					limit = { crownlaw_title = { holder = ROOT has_law = crown_authority_3 } }
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 2 months = 14 }
				}
				if = {
					limit = { crownlaw_title = { holder = ROOT has_law = crown_authority_4 } }
					opinion = { who = ROOT modifier = faction_disapprove_CA multiplier = 3 months = 14 }
				}
			}
		}

		#TRIBAL ORGANIZATION
		any_vassal = {
			# x2 to remove all possible "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_approve_TO }
			remove_opinion = { who = ROOT modifier = faction_approve_TO }

			# x3 to remove all possible "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_disapprove_TO }
			remove_opinion = { who = ROOT modifier = faction_disapprove_TO }
			remove_opinion = { who = ROOT modifier = faction_disapprove_TO }
		}
		if = {
			limit = { is_tribal = yes }
			#Tribal Organization 0
			if = {
				limit = { has_law = tribal_organization_0 }
				#court factions approve
				any_vassal = {
					limit = { in_faction = faction_court }
					opinion = { who = ROOT modifier = faction_approve_TO months = 14 }
				}
				#prosperity factions disapprove
				any_vassal = {
					limit = { in_faction = faction_prosperity }
					opinion = { who = ROOT modifier = faction_disapprove_TO months = 14 }
				}
			}
			#Tribal Organization 1
			if = {
				limit = { has_law = tribal_organization_1 }
				#court factions disapprove
				any_vassal = {
					limit = { in_faction = faction_court }
					opinion = { who = ROOT modifier = faction_disapprove_TO months = 14 }
				}
			}
			#Tribal Organization 2
			if = {
				limit = { has_law = tribal_organization_2 }
				# court factions disapprove
				any_vassal = {
					limit = { in_faction = faction_court }
					opinion = { who = ROOT modifier = faction_disapprove_TO months = 14 }
				}
			}
			#Tribal Organization 3
			if = {
				limit = { has_law = tribal_organization_3 }
				# court factions really disapprove
				any_vassal = {
					limit = { in_faction = faction_court }
					opinion = { who = ROOT modifier = faction_disapprove_TO multiplier = 2 months = 14 }
				}
				# tradition factions disapprove
				any_vassal = {
					limit = { in_faction = faction_tradition }
					opinion = { who = ROOT modifier = faction_disapprove_TO months = 14 }
				}
				# prosperity factions approve
				any_vassal = {
					limit = { in_faction = faction_prosperity }
					opinion = { who = ROOT modifier = faction_approve_TO months = 14 }
				}
			}
			#Tribal Organization 4
			if = {
				limit = { has_law = tribal_organization_4 }
				#court factions really disapprove
				any_vassal = {
					limit = { in_faction = faction_court }
					opinion = { who = ROOT modifier = faction_disapprove_TO multiplier = 3 months = 14 }
				}
				#tradition factions disapprove
				any_vassal = {
					limit = { in_faction = faction_tradition }
					opinion = { who = ROOT modifier = faction_disapprove_TO months = 14 }
				}
				#prosperity factions really approve
				any_vassal = {
					limit = { in_faction = faction_prosperity }
					opinion = { who = ROOT modifier = faction_approve_TO multiplier = 2 months = 14 }
				}
			}
		}

		#OBLIGATIONS
		any_vassal = {
			remove_opinion = { who = ROOT modifier = faction_approve_obligations }

			# Max. stacking for faction_disapprove_obligations:
			#
			# ================================================
			# FACTION | FEUDAL/IQTA | TEMPLE | CITY | TRIBAL |
			# --------+-------------+--------+------+--------+
			# court   |           3 |      0 |    0 |      3 | => 6
			# prosper |           1 |      1 |    3 |      1 | => 6
			# glory   |           2 |      0 |    0 |      2 | => 4
			# traditi |           0 |      3 |    0 |      0 | => 3
			# =================================================+
			#
			# Ergo, ergo court & prosperity can stack the highest with 6
			#
			# x6 to remove all possible "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_disapprove_obligations }
			remove_opinion = { who = ROOT modifier = faction_disapprove_obligations }
			remove_opinion = { who = ROOT modifier = faction_disapprove_obligations }
			remove_opinion = { who = ROOT modifier = faction_disapprove_obligations }
			remove_opinion = { who = ROOT modifier = faction_disapprove_obligations }
			remove_opinion = { who = ROOT modifier = faction_disapprove_obligations }
		}
		#Feudal Obligations 0
		if = {
			limit = { has_law = feudal_obligations_0 }
			any_vassal = {
				limit = {
					OR = {
						in_faction = faction_court
						in_faction = faction_glory
					}
				}
				opinion = { who = ROOT modifier = faction_approve_obligations months = 14 }
			}
		}
		#Feudal Obligations 2
		if = {
			limit = { has_law = feudal_obligations_2 }
			any_vassal = {
				limit = { in_faction = faction_court }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Feudal Obligations 3
		if = {
			limit = { has_law = feudal_obligations_3 }
			any_vassal = {
				limit = { in_faction = faction_court }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Feudal Obligations 4
		if = {
			limit = { has_law = feudal_obligations_4 }
			any_vassal = {
				limit = { in_faction = faction_court }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 3 months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}			
		#Temple Obligations 0
		if = {
			limit = { has_law = temple_obligations_0 }
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_approve_obligations months = 14 }
			}
		}
		#Temple Obligations 2
		if = {
			limit = { has_law = temple_obligations_2 }
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Temple Obligations 3
		if = {
			limit = { has_law = temple_obligations_3 }
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
		}
		#Temple Obligations 4
		if = {
			limit = { has_law = temple_obligations_4 }
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 3 months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#City Obligations 0
		if = {
			limit = { has_law = city_obligations_0 }
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_approve_obligations months = 14 }
			}
		}
		#City Obligations 2
		if = {
			limit = { has_law = city_obligations_2 }
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#City Obligations 3
		if = {
			limit = { has_law = city_obligations_3 }
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
		}
		#City Obligations 4
		if = {
			limit = { has_law = city_obligations_4 }
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 3 months = 14 }
			}
		}
		#Iqta Obligations 0
		if = {
			limit = { has_law = iqta_obligations_0 }
			any_vassal = {
				limit = {
					OR = {
						in_faction = faction_court
						in_faction = faction_glory
					}
				}
				opinion = { who = ROOT modifier = faction_approve_obligations months = 14 }
			}
		}
		#Iqta Obligations 2
		if = {
			limit = { has_law = iqta_obligations_2 }
			any_vassal = {
				limit = { in_faction = faction_court }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Iqta Obligations 3
		if = {
			limit = { has_law = iqta_obligations_3 }
			any_vassal = {
				limit = { in_faction = faction_court }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Iqta Obligations 4
		if = {
			limit = { has_law = iqta_obligations_4 }
			any_vassal = {
				limit = { in_faction = faction_court }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 3 months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Tribal Obligations 0
		if = {
			limit = { has_law = tribal_obligations_0 }
			any_vassal = {
				limit = {
					is_tribal = yes
					OR = {
						in_faction = faction_court
						in_faction = faction_glory
					}
				}
				opinion = { who = ROOT modifier = faction_approve_obligations months = 14 }
			}
		}
		#Tribal Obligations 2
		if = {
			limit = { has_law = tribal_obligations_2 }
			any_vassal = {
				limit = {
					is_tribal = yes
					in_faction = faction_court
				}
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Tribal Obligations 3
		if = {
			limit = { has_law = tribal_obligations_3 }
			any_vassal = {
				limit = {
					is_tribal = yes
					in_faction = faction_court
				}
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
			any_vassal = {
				limit = {
					is_tribal = yes
					in_faction = faction_glory
				}
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}
		#Tribal Obligations 4
		if = {
			limit = { has_law = tribal_obligations_4 }
			any_vassal = {
				limit = {
					is_tribal = yes
					in_faction = faction_court
				}
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 3 months = 14 }
			}
			any_vassal = {
				limit = {
					is_tribal = yes
					in_faction = faction_glory
				}
				opinion = { who = ROOT modifier = faction_disapprove_obligations multiplier = 2 months = 14 }
			}
			any_vassal = {
				limit = {
					is_tribal = yes
					in_faction = faction_prosperity
				}
				opinion = { who = ROOT modifier = faction_disapprove_obligations months = 14 }
			}
		}

		#COUNCIL TERMS
		any_vassal = {
			remove_opinion = { who = ROOT modifier = faction_approve_council_term }
		}
		if = {
			limit = { has_law = council_privileges_1 }
			job_chancellor = {
				remove_opinion = { who = ROOT modifier = grateful_council_term }
				opinion = { who = ROOT modifier = grateful_council_term months = 14 }
			}
			job_marshal = {
				remove_opinion = { who = ROOT modifier = grateful_council_term }
				opinion = { who = ROOT modifier = grateful_council_term months = 14 }
			}
			job_treasurer = {
				remove_opinion = { who = ROOT modifier = grateful_council_term }
				opinion = { who = ROOT modifier = grateful_council_term months = 14 }
			}
			job_spymaster = {
				remove_opinion = { who = ROOT modifier = grateful_council_term }
				opinion = { who = ROOT modifier = grateful_council_term months = 14 }
			}
			job_spiritual = {
				remove_opinion = { who = ROOT modifier = grateful_council_term }
				opinion = { who = ROOT modifier = grateful_council_term months = 14 }
			}
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_approve_council_term months = 14 }
			}
		}
		
		#COUNCILLORS
		any_vassal = {
			remove_opinion = { who = ROOT modifier = faction_approve_council }
			remove_opinion = { who = ROOT modifier = faction_disapprove_council }
		}
		#No Court Faction member on council
		if = {
			limit = { pf_councillor_court_trigger = no }
			any_vassal = {
				limit = { in_faction = faction_court }
				opinion = { who = ROOT modifier = faction_disapprove_council months = 14 }
			}
		}
		#Court Faction member on council
		if = {
			limit = { pf_councillor_court_trigger = yes }
			any_vassal = {
				limit = {
					is_voter = no
					in_faction = faction_court
				}
				opinion = { who = ROOT modifier = faction_approve_council months = 14 }
			}
		}
		#No Prosperity Faction member on council
		if = {
			limit = { pf_councillor_prosperity_trigger = no }
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_council months = 14 }
			}
		}
		#Prosperity Faction member on council
		if = {
			limit = { pf_councillor_prosperity_trigger = yes }
			any_vassal = {
				limit = {
					is_voter = no
					in_faction = faction_prosperity
				}
				opinion = { who = ROOT modifier = faction_approve_council months = 14 }
			}
		}
		#No Glory Faction member on council
		if = {
			limit = { pf_councillor_glory_trigger = no }
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_council months = 14 }
			}
		}
		#Glory Faction member on council
		if = {
			limit = { pf_councillor_glory_trigger = yes }
			any_vassal = {
				limit = {
					is_voter = no
					in_faction = faction_glory
				}
				opinion = { who = ROOT modifier = faction_approve_council months = 14 }
			}
		}
		#No Tradition Faction member on council
		if = {
			limit = { pf_councillor_tradition_trigger = no }
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_disapprove_council months = 14 }
			}
		}
		#Tradition Faction member on council
		if = {
			limit = { pf_councillor_tradition_trigger = yes }
			any_vassal = {
				limit = {
					is_voter = no
					in_faction = faction_tradition
				}
				opinion = { who = ROOT modifier = faction_approve_council months = 14 }
			}
		}

		#LIEGE PIETY
		any_vassal = {
			# x4 to prevent all "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_approve_piety }
			remove_opinion = { who = ROOT modifier = faction_approve_piety }
			remove_opinion = { who = ROOT modifier = faction_approve_piety }
			remove_opinion = { who = ROOT modifier = faction_approve_piety }
			# x4 to prevent all "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_disapprove_piety }
			remove_opinion = { who = ROOT modifier = faction_disapprove_piety }
			remove_opinion = { who = ROOT modifier = faction_disapprove_piety }
			remove_opinion = { who = ROOT modifier = faction_disapprove_piety }
		}
		#Liege has negative piety and NOT in a crusade or holy war
		if = {
			limit = {
				NOR = {
					piety = 0
					pf_tradition_approved_war_trigger = yes
				}
			}
			#Non-muslim Tradition Faction disapproves
			any_vassal = {
				limit = {
					NOT = { religion_group = muslim }
					in_faction = faction_tradition
				}
				opinion = { who = ROOT modifier = faction_disapprove_piety multiplier = 2 months = 14 }
			}
			#Muslim Tradition Faction disapproves more
			any_vassal = {
				limit = {
					religion_group = muslim
					in_faction = faction_tradition
				}
				opinion = { who = ROOT modifier = faction_disapprove_piety multiplier = 4 months = 14 }
			}
			#Muslim Court Faction disapproves
			any_vassal = {
				limit = {
					religion_group = muslim
					in_faction = faction_court
				}
				opinion = { who = ROOT modifier = faction_disapprove_piety multiplier = 2 months = 14 }
			}
			#Muslim Glory Faction disapproves less
			any_vassal = {
				limit = {
					religion_group = muslim
					in_faction = faction_glory
				}
				opinion = { who = ROOT modifier = faction_disapprove_piety months = 14 }
			}
		}
		#Liege has 500-999 Piety
		if = {
			limit = {
				piety = 500
				NOT = { piety = 1000 }
			}
			#Tradition Faction approves
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_approve_piety months = 14 }
			}
		}
		#Liege has 1000+ Piety
		if = {
			limit = { piety = 1000 }
			#Tradition Faction approves more
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_approve_piety multiplier = 2 months = 14 }
			}
		}
		#Liege is in a crusade or holy war
		if = {
			limit = { pf_tradition_approved_war_trigger = yes }
			#Tradition Faction approves more
			any_vassal = {
				limit = { in_faction = faction_tradition }
				opinion = { who = ROOT modifier = faction_approve_piety multiplier = 2 months = 14 }
			}
		}

		#LIEGE DEBT/WEALTH
		any_vassal = {
			remove_opinion = { who = ROOT modifier = faction_disapprove_debt }
			remove_opinion = { who = ROOT modifier = faction_approve_wealth }
		}
		#Liege has negative wealth or is in debt
		if = {
			limit = {
				OR = {
					NOT = { wealth = 0 }
					has_character_flag = loan_taken
					check_variable = { which = "loan_amount" value = 49.5 }
				}
			}
			#Prosperity faction disapproves
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_disapprove_debt multiplier = 2 months = 14 }
			}
		}
		#Liege has 2.5-4.9 scaled wealth, and no loan
		if = {
			limit = {
				scaled_wealth = 2.5
				NOT = { scaled_wealth = 5.0 }
				NOT = { has_character_flag = loan_taken }
				NOT = { check_variable = { which = "loan_amount" value = 49.5 } }
			}
			#Prosperity faction approves
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_approve_wealth months = 14 }
			}
		}
		#Liege has 5.0+ scaled wealth, and no loan
		if = {
			limit = {
				scaled_wealth = 5.0
				NOT = { has_character_flag = loan_taken }
				NOT = { check_variable = { which = "loan_amount" value = 49.5 } }
			}
			#Prosperity faction approves more
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_approve_wealth multiplier = 2 months = 14 }
			}
		}

		#LIEGE PRESTIGE
		any_vassal = {
			# x2 to prevent all "leftover" stacking
			remove_opinion = { who = ROOT modifier = faction_approve_prestige }
			remove_opinion = { who = ROOT modifier = faction_approve_prestige }
			remove_opinion = { who = ROOT modifier = faction_disapprove_prestige }
			remove_opinion = { who = ROOT modifier = faction_disapprove_prestige }
		}
		#Liege has negative prestige
		if = {
			limit = { NOT = { prestige = 0 } }
			#Glory Faction disapproves
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_prestige multiplier = 2 months = 14 }
			}
		}
		#Liege has 1000-2499 Prestige
		if = {
			limit = {
				prestige = 1000
				NOT = { prestige = 2500 }
			}
			#Glory Faction approves
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_approve_prestige months = 14 }
			}
		}
		#Liege has 2500 Prestige+
		if = {
			limit = {
				prestige = 2500
			}
			#Glory Faction approves more
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_approve_prestige multiplier = 2 months = 14 }
			}
		}

		#YEARS OF PEACE
		any_vassal = {
			# x4 to remove max. possible stacked modifiers
			remove_opinion = { who = ROOT modifier = faction_approve_peace }
			remove_opinion = { who = ROOT modifier = faction_approve_peace }
			remove_opinion = { who = ROOT modifier = faction_approve_peace }
			remove_opinion = { who = ROOT modifier = faction_approve_peace }
			remove_opinion = { who = ROOT modifier = faction_disapprove_peace }
			remove_opinion = { who = ROOT modifier = faction_disapprove_peace }
			remove_opinion = { who = ROOT modifier = faction_disapprove_peace }
			remove_opinion = { who = ROOT modifier = faction_disapprove_peace }
		}
		#5-10 years of consecutive peace
		if = {
			limit = {
				check_variable = { which = "years_of_peace" value = 5 }
				NOT = { check_variable = { which = "years_of_peace" value = 10 } }
			}
			#Prosperity Faction approves
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_approve_peace months = 14 }
			}
			#Glory Faction disapproves
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_peace months = 14 }
			}
		}
		#10-15 years of consecutive peace
		if = {
			limit = {
				check_variable = { which = "years_of_peace" value = 10 }
				NOT = { check_variable = { which = "years_of_peace" value = 15 } }
			}
			#Prosperity Faction approves more
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_approve_peace multiplier = 2 months = 14 }
			}
			#Glory Faction disapproves more
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_peace multiplier = 2 months = 14 }
			}
		}
		#15-20 years of consecutive peace
		if = {
			limit = {
				check_variable = { which = "years_of_peace" value = 15 }
				NOT = { check_variable = { which = "years_of_peace" value = 20 } }
			}
			#Prosperity Faction approves more & more
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_approve_peace multiplier = 3 months = 14 }
			}
			#Glory Faction disapproves more & more
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_peace multiplier = 3 months = 14 }
			}
		}
		#20+ years of consecutive peace
		if = {
			limit = {
				check_variable = { which = "years_of_peace" value = 20 }
			}
			#Prosperity Faction REALLY approves
			any_vassal = {
				limit = { in_faction = faction_prosperity }
				opinion = { who = ROOT modifier = faction_approve_peace multiplier = 4 months = 14 }
			}
			#Glory Faction REALLY disapproves
			any_vassal = {
				limit = { in_faction = faction_glory }
				opinion = { who = ROOT modifier = faction_disapprove_peace multiplier = 4 months = 14 }
			}
		}

		#Adjust moods of factions towards their own leaders, based on agreement over opinion of liege
		any_vassal = {
			limit = { pf_leader_trigger = yes }
			#First remove the old agree/disagree modifiers for all the liege's vassals
			ROOT = {
				any_vassal = {
					remove_opinion = { who = PREVPREV modifier = faction_agree_liege }
					# x3 to prevent all "leftover" stacking
					remove_opinion = { who = PREVPREV modifier = faction_disagree_liege }
					remove_opinion = { who = PREVPREV modifier = faction_disagree_liege }
					remove_opinion = { who = PREVPREV modifier = faction_disagree_liege }
				}
			}
			#Then check the faction leader's opinion range of the liege
			#and look at all fellow faction members, and see if they sit in the same range or NOT
			#the more extreme the difference, the larger the disapproval of the faction leader
			if = {
				limit = {
					NOT = { opinion = { who = ROOT value = -75 } }
				}
				any_faction_backer = {
					limit = { pf_my_faction_backer_trigger = yes }
					if = {
						limit = {
							NOT = { opinion = { who = ROOT value = -75 } }
						}
						opinion = { who = PREV modifier = faction_agree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = -25 }
							NOT = { opinion = { who = ROOT value = 25 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = 25 }
							NOT = { opinion = { who = ROOT value = 75 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege multiplier = 2 months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = 75 }
						}
						opinion = { who = PREV modifier = faction_disagree_liege multiplier = 3 months = 14 }
					}
				}
			}
			if = {
				limit = {
					opinion = { who = ROOT value = -75 }
					NOT = { opinion = { who = ROOT value = -25 } }
				}
				any_faction_backer = {
					limit = { pf_my_faction_backer_trigger = yes }
					if = {
						limit = {
							opinion = { who = ROOT value = -75 }
							NOT = { opinion = { who = ROOT value = -25 } }
						}
						opinion = { who = PREV modifier = faction_agree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = 25 }
							NOT = { opinion = { who = ROOT value = 75 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = 75 }
						}
						opinion = { who = PREV modifier = faction_disagree_liege multiplier = 2 months = 14 }
					}
				}
			}
			if = {
				limit = {
					opinion = { who = ROOT value = -25 }
					NOT = { opinion = { who = ROOT value = 25 } }
				}
				any_faction_backer = {
					limit = { pf_my_faction_backer_trigger = yes }
					if = {
						limit = {
							NOT = { opinion = { who = ROOT value = -75 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = -25 }
							NOT = { opinion = { who = ROOT value = 25 } }
						}
						opinion = { who = PREV modifier = faction_agree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = 75 }
						}
						opinion = { who = PREV modifier = faction_disagree_liege months = 14 }
					}
				}
			}
			if = {
				limit = {
					opinion = { who = ROOT value = 25 }
					NOT = { opinion = { who = ROOT value = 75 } }
				}
				any_faction_backer = {
					limit = { pf_my_faction_backer_trigger = yes }
					if = {
						limit = {
							NOT = { opinion = { who = ROOT value = -75 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege multiplier = 2 months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = -75 }
							NOT = { opinion = { who = ROOT value = -25 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = 25 }
							NOT = { opinion = { who = ROOT value = 75 } }
						}
						opinion = { who = PREV modifier = faction_agree_liege months = 14 }
					}
				}
			}
			if = {
				limit = {
					opinion = { who = ROOT value = 75 }
				}
				any_faction_backer = {
					limit = { pf_my_faction_backer_trigger = yes }
					if = {
						limit = {
							NOT = { opinion = { who = ROOT value = -75 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege multiplier = 3 months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = -75 }
							NOT = { opinion = { who = ROOT value = -25 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege multiplier = 2 months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = -25 }
							NOT = { opinion = { who = ROOT value = 25 } }
						}
						opinion = { who = PREV modifier = faction_disagree_liege months = 14 }
					}
					if = {
						limit = {
							opinion = { who = ROOT value = 75 }
						}
						opinion = { who = PREV modifier = faction_agree_liege months = 14 }
					}
				}
			}
		}

		character_event = { id = PlusFaction.222 } # completion effects
	}
}

# PlusFaction.221 [Liege]
# Faction maintenance setup effects
character_event = {
	id = PlusFaction.221

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		pf_reset_maintenance_vars_effect = yes

		#count the number of votes in each faction
		any_vassal = {
			limit = { in_faction = faction_court }
			if = {
				limit = { real_tier = BARON }
				ROOT = { change_variable = { which = "faction_votes_court" value = 1 } }
			}
			if = {
				limit = { real_tier = COUNT }
				ROOT = { change_variable = { which = "faction_votes_court" value = 2 } }
			}
			if = {
				limit = { real_tier = DUKE }
				ROOT = { change_variable = { which = "faction_votes_court" value = 4 } }
			}
			if = {
				limit = { real_tier = KING }
				ROOT = { change_variable = { which = "faction_votes_court" value = 8 } }
			}
		}
		any_vassal = {
			limit = { in_faction = faction_prosperity }
			if = {
				limit = { real_tier = BARON }
				ROOT = { change_variable = { which = "faction_votes_prosperity" value = 1 } }
			}
			if = {
				limit = { real_tier = COUNT }
				ROOT = { change_variable = { which = "faction_votes_prosperity" value = 2 } }
			}
			if = {
				limit = { real_tier = DUKE }
				ROOT = { change_variable = { which = "faction_votes_prosperity" value = 4 } }
			}
			if = {
				limit = { real_tier = KING }
				ROOT = { change_variable = { which = "faction_votes_prosperity" value = 8 } }
			}
		}
		any_vassal = {
			limit = { in_faction = faction_glory }
			if = {
				limit = { real_tier = BARON }
				ROOT = { change_variable = { which = "faction_votes_glory" value = 1 } }
			}
			if = {
				limit = { real_tier = COUNT }
				ROOT = { change_variable = { which = "faction_votes_glory" value = 2 } }
			}
			if = {
				limit = { real_tier = DUKE }
				ROOT = { change_variable = { which = "faction_votes_glory" value = 4 } }
			}
			if = {
				limit = { real_tier = KING }
				ROOT = { change_variable = { which = "faction_votes_glory" value = 8 } }
			}
		}
		any_vassal = {
			limit = { in_faction = faction_tradition }
			if = {
				limit = { real_tier = BARON }
				ROOT = { change_variable = { which = "faction_votes_tradition" value = 1 } }
			}
			if = {
				limit = { real_tier = COUNT }
				ROOT = { change_variable = { which = "faction_votes_tradition" value = 2 } }
			}
			if = {
				limit = { real_tier = DUKE }
				ROOT = { change_variable = { which = "faction_votes_tradition" value = 4 } }
			}
			if = {
				limit = { real_tier = KING }
				ROOT = { change_variable = { which = "faction_votes_tradition" value = 8 } }
			}
		}
	}
}

# PlusFaction.222 [Liege]
# Faction maintenance completion effects
character_event = {
	id = PlusFaction.222

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		#Now calculate each faction's mood, which is the  average of each faction member's
		#overall opinion of the liege, weighted by the number of votes they have in the faction
		if = {
			limit = { ai = yes }
			any_vassal = {
				limit = { pf_member_trigger = yes }

				# set a number based on their opinion of the liege (from 1 to 100, 100 being the best)
				if = {
					limit = {
						opinion = { who = ROOT value = 75 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 100 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 75 } }
						opinion = { who = ROOT value = 50 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 87.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 50 } }
						opinion = { who = ROOT value = 25 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 75 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 25 } }
						opinion = { who = ROOT value = 0 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 62.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 0 } }
						opinion = { who = ROOT value = -25 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 50 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = -25 } }
						opinion = { who = ROOT value = -50 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 37.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = -50 } }
						opinion = { who = ROOT value = -75 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 25 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = -75 } }
						opinion = { who = ROOT value = -99 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 12.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = -99 } }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 1 } }
				}

				# then multiply that number by the number of votes they have in the faction
				# NOTE: Barons would multiply x1, so no point in actually calculating that
				if = {
					limit = { real_tier = COUNT }
					ROOT = { multiply_variable = { which = "faction_member_approval" value = 2 } }
				}
				if = {
					limit = { real_tier = DUKE }
					ROOT = { multiply_variable = { which = "faction_member_approval" value = 4 } }
				}
				if = {
					limit = { real_tier = KING }
					ROOT = { multiply_variable = { which = "faction_member_approval" value = 8 } }
				}

				#then divide by the total number of votes in the faction and add that to the liege's mood variable
				if = {
					limit = { in_faction = faction_court }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_court" }
						change_variable = { which = "faction_mood_court" which = "faction_member_approval" }
					}
				}
				if = {
					limit = { in_faction = faction_prosperity }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_prosperity" }
						change_variable = { which = "faction_mood_prosperity" which = "faction_member_approval" }
					}
				}
				if = {
					limit = { in_faction = faction_glory }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_glory" }
						change_variable = { which = "faction_mood_glory" which = "faction_member_approval" }
					}
				}
				if = {
					limit = { in_faction = faction_tradition }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_tradition" }
						change_variable = { which = "faction_mood_tradition" which = "faction_member_approval" }
					}
				}
			}
		}

		if = {
			limit = { ai = no }
			any_vassal = {
				limit = { pf_member_trigger = yes }

				# set a number based on their opinion of the liege (from 1 to 100, 100 being the best)
				if = {
					limit = {
						opinion = { who = ROOT value = 90 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 100 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 90 } }
						opinion = { who = ROOT value = 70 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 87.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 70 } }
						opinion = { who = ROOT value = 50 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 75 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 50 } }
						opinion = { who = ROOT value = 30 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 62.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 30 } }
						opinion = { who = ROOT value = 10 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 50 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = 10 } }
						opinion = { who = ROOT value = -10 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 37.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = -10 } }
						opinion = { who = ROOT value = -30 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 25 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = -30 } }
						opinion = { who = ROOT value = -50 }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 12.5 } }
				}
				if = {
					limit = {
						NOT = { opinion = { who = ROOT value = -50 } }
					}
					ROOT = { set_variable = { which = "faction_member_approval" value = 1 } }
				}

				# then multiply that number by the number of votes they have in the faction
				# NOTE: Barons would multiply x1, so no point in actually calculating that
				if = {
					limit = { real_tier = COUNT }
					ROOT = { multiply_variable = { which = "faction_member_approval" value = 2 } }
				}
				if = {
					limit = { real_tier = DUKE }
					ROOT = { multiply_variable = { which = "faction_member_approval" value = 4 } }
				}
				if = {
					limit = { real_tier = KING }
					ROOT = { multiply_variable = { which = "faction_member_approval" value = 8 } }
				}

				#then divide by the total number of votes in the faction and add that to the liege's mood variable
				if = {
					limit = { in_faction = faction_court }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_court" }
						change_variable = { which = "faction_mood_court" which = "faction_member_approval" }
					}
				}
				if = {
					limit = { in_faction = faction_prosperity }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_prosperity" }
						change_variable = { which = "faction_mood_prosperity" which = "faction_member_approval" }
					}
				}
				if = {
					limit = { in_faction = faction_glory }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_glory" }
						change_variable = { which = "faction_mood_glory" which = "faction_member_approval" }
					}
				}
				if = {
					limit = { in_faction = faction_tradition }
					ROOT = {
						divide_variable = { which = "faction_member_approval" which = "faction_votes_tradition" }
						change_variable = { which = "faction_mood_tradition" which = "faction_member_approval" }
					}
				}
			}
		}

		#Now determine the overall mood category of the faction and set the flag if it's different than before
		#and notify the liege if it's actually changed (and only then, to avoid spam)
		if = {
			limit = { any_vassal = { in_faction = faction_court } }
			if = {
				limit = {
					check_variable = { which = "faction_mood_court" value = 75 }
					NOT = { has_character_flag = faction_court_happy }
				}
				set_character_flag = faction_court_happy
				clr_character_flag = faction_court_unhappy
				clr_character_flag = faction_court_angry
				character_event = { id = PlusFaction.201 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_court }
					character_event = { id = PlusFaction.201 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_court" value = 75 } }
					check_variable = { which = "faction_mood_court" value = 50 }
					OR = {
						has_character_flag = faction_court_happy
						has_character_flag = faction_court_unhappy
						has_character_flag = faction_court_angry
					}	
				}
				clr_character_flag = faction_court_happy
				clr_character_flag = faction_court_unhappy
				clr_character_flag = faction_court_angry
				character_event = { id = PlusFaction.201 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_court }
					character_event = { id = PlusFaction.201 days = 1 }
				}
			}				
			if = {
				limit = {
					check_variable = { which = "faction_mood_court" value = 37.5 }
					NOT = { check_variable = { which = "faction_mood_court" value = 50 } }
					NOT = { has_character_flag = faction_court_unhappy }
				}
				set_character_flag = faction_court_unhappy
				clr_character_flag = faction_court_happy
				clr_character_flag = faction_court_angry
				character_event = { id = PlusFaction.201 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_court }
					character_event = { id = PlusFaction.201 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_court" value = 37.5 } }
					NOT = { has_character_flag = faction_court_angry }
				}
				set_character_flag = faction_court_angry
				clr_character_flag = faction_court_unhappy
				clr_character_flag = faction_court_happy
				character_event = { id = PlusFaction.201 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_court }
					character_event = { id = PlusFaction.201 days = 1 }
				}
			}
		}
		if = {
			limit = { any_vassal = { in_faction = faction_prosperity } }
			if = {
				limit = {
					check_variable = { which = "faction_mood_prosperity" value = 75 }
					NOT = { has_character_flag = faction_prosperity_happy }
				}
				set_character_flag = faction_prosperity_happy
				clr_character_flag = faction_prosperity_unhappy
				clr_character_flag = faction_prosperity_angry
				character_event = { id = PlusFaction.202 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_prosperity }
					character_event = { id = PlusFaction.202 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_prosperity" value = 75 } }
					check_variable = { which = "faction_mood_prosperity" value = 50 }
					OR = {
						has_character_flag = faction_prosperity_happy
						has_character_flag = faction_prosperity_unhappy
						has_character_flag = faction_prosperity_angry
					}	
				}
				clr_character_flag = faction_prosperity_happy
				clr_character_flag = faction_prosperity_unhappy
				clr_character_flag = faction_prosperity_angry
				character_event = { id = PlusFaction.202 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_prosperity }
					character_event = { id = PlusFaction.202 days = 1 }
				}
			}
			if = {
				limit = {
					check_variable = { which = "faction_mood_prosperity" value = 37.5 }
					NOT = { check_variable = { which = "faction_mood_prosperity" value = 50 } }
					NOT = { has_character_flag = faction_prosperity_unhappy }
				}
				set_character_flag = faction_prosperity_unhappy
				clr_character_flag = faction_prosperity_happy
				clr_character_flag = faction_prosperity_angry
				character_event = { id = PlusFaction.202 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_prosperity }
					character_event = { id = PlusFaction.202 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_prosperity" value = 37.5 } }
					NOT = { has_character_flag = faction_prosperity_angry }
				}
				set_character_flag = faction_prosperity_angry
				clr_character_flag = faction_prosperity_unhappy
				clr_character_flag = faction_prosperity_happy
				character_event = { id = PlusFaction.202 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_prosperity }
					character_event = { id = PlusFaction.202 days = 1 }
				}
			}
		}
		if = {
			limit = { any_vassal = { in_faction = faction_glory } }
			if = {
				limit = {
					check_variable = { which = "faction_mood_glory" value = 75 }
					NOT = { has_character_flag = faction_glory_happy }
				}
				set_character_flag = faction_glory_happy
				clr_character_flag = faction_glory_unhappy
				clr_character_flag = faction_glory_angry
				character_event = { id = PlusFaction.203 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_glory }
					character_event = { id = PlusFaction.203 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_glory" value = 75 } }
					check_variable = { which = "faction_mood_glory" value = 50 }
					OR = {
						has_character_flag = faction_glory_happy
						has_character_flag = faction_glory_unhappy
						has_character_flag = faction_glory_angry
					}	
				}
				clr_character_flag = faction_glory_happy
				clr_character_flag = faction_glory_unhappy
				clr_character_flag = faction_glory_angry
				character_event = { id = PlusFaction.203 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_glory }
					character_event = { id = PlusFaction.203 days = 1 }
				}
			}
			if = {
				limit = {
					check_variable = { which = "faction_mood_glory" value = 37.5 }
					NOT = { check_variable = { which = "faction_mood_glory" value = 50 } }
					NOT = { has_character_flag = faction_glory_unhappy }
				}
				set_character_flag = faction_glory_unhappy
				clr_character_flag = faction_glory_happy
				clr_character_flag = faction_glory_angry
				character_event = { id = PlusFaction.203 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_glory }
					character_event = { id = PlusFaction.203 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_glory" value = 37.5 } }
					NOT = { has_character_flag = faction_glory_angry }
				}
				set_character_flag = faction_glory_angry
				clr_character_flag = faction_glory_unhappy
				clr_character_flag = faction_glory_happy
				character_event = { id = PlusFaction.203 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_glory }
					character_event = { id = PlusFaction.203 days = 1 }
				}
			}
		}
		if = {
			limit = { any_vassal = { in_faction = faction_tradition } }
			if = {
				limit = {
					check_variable = { which = "faction_mood_tradition" value = 75 }
					NOT = { has_character_flag = faction_tradition_happy }
				}
				set_character_flag = faction_tradition_happy
				clr_character_flag = faction_tradition_unhappy
				clr_character_flag = faction_tradition_angry
				character_event = { id = PlusFaction.204 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_tradition }
					character_event = { id = PlusFaction.204 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_tradition" value = 75 } }
					check_variable = { which = "faction_mood_tradition" value = 50 }
					OR = {
						has_character_flag = faction_tradition_happy
						has_character_flag = faction_tradition_unhappy
						has_character_flag = faction_tradition_angry
					}	
				}
				clr_character_flag = faction_tradition_happy
				clr_character_flag = faction_tradition_unhappy
				clr_character_flag = faction_tradition_angry
				character_event = { id = PlusFaction.204 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_tradition }
					character_event = { id = PlusFaction.204 days = 1 }
				}
			}
			if = {
				limit = {
					check_variable = { which = "faction_mood_tradition" value = 37.5 }
					NOT = { check_variable = { which = "faction_mood_tradition" value = 50 } }
					NOT = { has_character_flag = faction_tradition_unhappy }
				}
				set_character_flag = faction_tradition_unhappy
				clr_character_flag = faction_tradition_happy
				clr_character_flag = faction_tradition_angry
				character_event = { id = PlusFaction.204 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_tradition }
					character_event = { id = PlusFaction.204 days = 1 }
				}
			}
			if = {
				limit = {
					NOT = { check_variable = { which = "faction_mood_tradition" value = 37.5 } }
					NOT = { has_character_flag = faction_tradition_angry }
				}
				set_character_flag = faction_tradition_angry
				clr_character_flag = faction_tradition_unhappy
				clr_character_flag = faction_tradition_happy
				character_event = { id = PlusFaction.204 days = 1 }
				any_vassal = {
					limit = { in_faction = faction_tradition }
					character_event = { id = PlusFaction.204 days = 1 }
				}
			}
		}
	}
}

# PlusFaction.223 [Faction Leader w/ Mood Increase]
# Helper to PlusFaction.200 in order to take advantage of `break = yes` (and more)
character_event = {
	id = PlusFaction.223

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 1 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 2 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 2 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 2 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 3 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 3 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 3 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 4 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 4 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 4 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 5 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 5 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 5 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 6 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 6 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 6 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 7 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 7 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 7 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 8 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 8 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 8 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_increase" value = 9 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 9 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 9 }
			}
			break = yes
		}
		if = {
			limit = { check_variable = { which = "faction_mood_increase" value = 9.5 } }
			opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 10 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_pleased years = 3 multiplier = 10 }
			}
			break = yes
		}
	}
}

# PlusFaction.224 [Faction Leader w/ Mood Decrease]
# Helper to PlusFaction.200 in order to take advantage of `break = yes` (and more)
character_event = {
	id = PlusFaction.224

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 1 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 2 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 2 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 2 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 3 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 3 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 3 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 4 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 4 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 4 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 5 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 5 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 5 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 6 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 6 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 6 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 7 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 7 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 7 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 8 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 8 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 8 }
			}
			break = yes
		}
		if = {
			limit = { is_variable_equal = { which = "faction_mood_decrease" value = 9 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 9 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 9 }
			}
			break = yes
		}
		if = {
			limit = { check_variable = { which = "faction_mood_decrease" value = 9.5 } }
			opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 10 }
			any_faction_backer = {
				limit = { pf_my_faction_backer_trigger = yes }
				opinion = { who = FROM modifier = faction_displeased years = 6 multiplier = 10 }
			}
			break = yes
		}
	}
}

# PlusFaction.230 [New Holder]
#
# Civil war transfers to a new liege due to title gain
character_event = {
	id = PlusFaction.230

	is_triggered_only = yes
	hide_window = yes

	only_playable = yes

	trigger = {
		FROM = { higher_tier_than = COUNT }
		FROMFROM = { is_dying = no }
		any_war = { # We've inherited at least one of FROMFROM's civil wars
			defender = { character = ROOT }
			attacker = {
				has_opinion_modifier = { who = FROMFROM modifier = revolting_against }
			}
			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
			}
		}
		NOT = { has_global_flag = pf_teardown_lock }
	}

	immediate = {
		FROMFROM = {
			log = "INFO: ruler took-over defense against civil war due to gaining title '[From.GetFullBaseName]'. old liege's interim state pre-transfer:"
			pf_log_state_effect = yes
			
			# Transfer revolt TOMs
			any_opinion_modifier_target = { # Those that were revolting against FROMFROM
				limit = {
					is_alive = yes
					reverse_has_opinion_modifier = { who = PREV modifier = revolting_against_me }
				}
				reverse_remove_opinion = { who = PREV modifier = revolting_against_me }
				remove_opinion = { who = PREV modifier = revolting_against }
				if = {
					limit = {
						NOR = {
							reverse_has_opinion_modifier = {
								who = ROOT
								modifier = revolting_against_me
							}
							has_opinion_modifier = {
								who = ROOT
								modifier = revolting_against
							}
						}
					}
					reverse_opinion = { who = ROOT modifier = revolting_against_me }
					opinion = { who = ROOT modifier = revolting_against }
				}
			}
			
			# Tear-down loyalist/neutral state; we've got a new liege altogether now.
			# NOTE: We intentionally do not transfer neutral/loyalist to new liege
			
			any_opinion_modifier_target = { # Those that were loyal to FROMFROM
				limit = {
					is_alive = yes
					reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me }
				}
				reverse_remove_opinion = { who = PREV modifier = pf_loyal_to_me }
				remove_opinion = { who = PREV modifier = pf_loyal_toward }
				remove_trait = pf_loyalist
			}
			any_opinion_modifier_target = { # Those that were neutral in FROMFROM's civil war
				limit = {
					is_alive = yes
					reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me }
				}
				reverse_remove_opinion = { who = PREV modifier = pf_neutral_to_me }
				remove_opinion = { who = PREV modifier = pf_neutral_toward }
				remove_trait = pf_neutral
			}
			
			# Transfer war flags
			if = {
				limit = { has_character_flag = faction_court_war }
				ROOT = { set_character_flag = faction_court_war }
			}
			if = {
				limit = { has_character_flag = faction_prosperity_war }
				ROOT = { set_character_flag = faction_prosperity_war }
			}
			if = {
				limit = { has_character_flag = faction_glory_war }
				ROOT = { set_character_flag = faction_glory_war }
			}
			if = {
				limit = { has_character_flag = faction_tradition_war }
				ROOT = { set_character_flag = faction_tradition_war }
			}
			if = {
				limit = { has_character_flag = faction_pretender_war }
				ROOT = { set_character_flag = faction_pretender_war }
			}
			if = {
				limit = { has_character_flag = faction_separatist_war }
				ROOT = { set_character_flag = faction_separatist_war }
			}
			if = {
				limit = { has_character_flag = faction_religious_war }
				ROOT = { set_character_flag = faction_religious_war }
			}
			if = {
				limit = { has_character_flag = faction_succession_war }
				ROOT = { set_character_flag = faction_succession_war }
			}
			if = {
				limit = { has_character_flag = faction_authority_war }
				ROOT = { set_character_flag = faction_authority_war }
			}
			
			# NOTE: Only valid to clear all war flags due to the fact that loss
			# of a title leading to a new civil war liege altogether implies
			# that all of FROMFROM's civil wars were transferred to ROOT
			pf_clear_war_flags_effect = yes
			
			log = "... old liege's state post-transfer:"
			pf_log_state_effect = yes
		}
		
		# Note the liege change for transferred civil wars
		any_war = {
			limit = {
				defender = { character = ROOT }
				attacker = {
					has_opinion_modifier = { who = FROMFROM modifier = revolting_against }
				}
				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
				}
			}
			attacker = {
				if = {
					limit = { has_character_flag = original_war_liege }					
					log = "... clearing original liege status for civil war (rebel success will be less dangerous)"
				}
				clr_character_flag = original_war_liege
			}
		}
		
		log = "... new liege's state post-transfer:"
		pf_log_state_effect = yes
	}
}

# PlusFaction.231 [New Holder]
#
# State cleanup when rebel drops-out of a civil war due to losing a title
character_event = {
	id = PlusFaction.231

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		FROMFROM = {
			trait = pf_rebel
			OR = {
				is_ruler = no
				in_revolt = no
				NOT = {
					liege_before_war = {
						has_opinion_modifier = { who = PREV modifier = revolting_against_me }
					}
				}
			}
		}
		NOT = { has_global_flag = pf_teardown_lock }
	}

	immediate = {
		FROMFROM = {
			log = "INFO: rebel dropped-out of a civil war after losing a title ([From.GetFullBaseName]). interim state pre-cleanup:"
			pf_log_state_effect = yes
			pf_teardown_rebel_effect = yes
			log = "... ex-rebel's state post-cleanup:"
			pf_log_state_effect = yes
		}
	}
}

# PlusFaction.232 [New Holder]
#
# State cleanup when rebel drops-out of a civil war due to gaining a title
character_event = {
	id = PlusFaction.232

	is_triggered_only = yes
	hide_window = yes

	only_playable = yes # Due to nature of title gain and preexisting rebel state

	trigger = {
		FROM = {
			higher_tier_than = COUNT
			is_primary_holder_title = yes
			temporary = no
		}
		trait = pf_rebel
		any_opinion_modifier_target = { # Old civil war liege
			is_alive = yes
			reverse_has_opinion_modifier = { who = PREV modifier = revolting_against }
			in_revolt = yes
			any_war = {
				defender = { character = PREV }
				attacker = { # Old revolt leader
					NOR = {
						character = ROOT
						is_vassal_or_below = ROOT
					}
				}
				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
				}
			}
		}
		NOT = { has_global_flag = pf_teardown_lock }
	}

	immediate = {
		log = "INFO: rebel is dropping-out of civil war due to [This.GetHerHim] gaining title ([From.GetFullBaseName]): [This.GetHerHis] interim state follows..."
		pf_log_state_effect = yes
		pf_teardown_rebel_effect = yes
		log = "... ex-rebel's state post-cleanup:"
		pf_log_state_effect = yes
	}
}

# PlusFaction.233 [New Holder]
#
# Civil war state cleanup when loyalist/neutral's liege changes due to gaining a title
character_event = {
	id = PlusFaction.233

	is_triggered_only = yes
	hide_window = yes

	only_playable = yes # Due to nature of title gain and preexisting loyalist/neutral state

	trigger = {
		primary_title = { title = FROM }
		OR = {
			AND = {
				trait = pf_neutral
				NOT = { has_opinion_modifier = { who = LIEGE modifier = pf_neutral_toward } }
			}
			AND = {
				trait = pf_loyalist
				NOT = { has_opinion_modifier = { who = LIEGE modifier = pf_loyal_toward } }
			}
		}
		NOT = { has_global_flag = pf_teardown_lock }
	}

	immediate = {
		if = {
			limit = { trait = pf_loyalist }
			log = "INFO: loyalist's liege changed due to [This.GetHerHim] gaining a title ([From.GetFullBaseName]): [This.GetHerHis] interim state follows..."
			pf_log_state_effect = yes
			pf_teardown_loyalist_effect = yes
			log = "... ex-loyalist's PF state dump post-cleanup:"
			pf_log_state_effect = yes
		}
		if = {
			limit = { trait = pf_neutral }
			log = "INFO: neutral's liege changed due to [This.GetHerHim] gaining a title ([From.GetFullBaseName]): [This.GetHerHis] interim state follows..."
			pf_log_state_effect = yes
			pf_teardown_neutral_effect = yes
			log = "... ex-neutral's PF state dump post-cleanup:"
			pf_log_state_effect = yes
		}
	}
}

# PlusFaction.234 [Unlanded Character]
#
# Civil war state cleanup when loyalist/neutral is unlanded
# NOTE: Unlanded rebels are already handled by PlusFaction.231
character_event = {
	id = PlusFaction.234

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		is_dying = no # This is handled directly on_death by PlusFaction.205
		OR = {
			trait = pf_loyalist
			trait = pf_neutral
		}
		has_game_started = yes
		NOT = { has_global_flag = pf_teardown_lock }
	}

	immediate = {
		if = {
			limit = { trait = pf_loyalist }
			log = "INFO: loyalist unlanded. interim state pre-cleanup:"
			pf_log_state_effect = yes
			pf_teardown_loyalist_effect = yes
			log = "... ex-loyalist's PF state dump post-cleanup:"
			pf_log_state_effect = yes
		}
		if = {
			limit = { trait = pf_neutral }
			log = "INFO: neutral unlanded. interim state pre-cleanup:"
			pf_log_state_effect = yes
			pf_teardown_neutral_effect = yes
			log = "... ex-neutral's PF state dump post-cleanup:"
			pf_log_state_effect = yes
		}
	}
}


# Main Annual faction maintenance event
character_event = {
	id = PlusFaction.250
	
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	religion = nestorian
	
	trigger = {
		has_landed_title = d_nestorian
	}
	
	immediate = {
		log = "Annual faction maintenance pulse begins"
		
		#clean up flags
		any_character = {
			limit = { is_ruler = yes }
			clr_character_flag = pf_pulse
			if = {
				limit = { war = no }
				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
			}
			if = {
				limit = {
					pf_leader_trigger = no
					OR = {
						has_character_flag = faction_meeting
						has_character_flag = faction_meeting_called
						has_character_flag = faction_meeting_begun
						has_character_flag = faction_meeting_postponed
						has_character_flag = faction_leadership_challenge
					}
				}
				clr_character_flag = faction_meeting
				clr_character_flag = can_make_demand
				clr_character_flag = faction_meeting_called
				clr_character_flag = faction_meeting_begun
				clr_character_flag = faction_meeting_postponed
				clr_character_flag = faction_leadership_challenge
				clr_character_flag = faction_is_happy
				clr_character_flag = faction_is_content
				clr_character_flag = faction_is_unhappy
				clr_character_flag = faction_is_angry
				clr_character_flag = made_great_speech
				clr_character_flag = made_good_speech
				clr_character_flag = made_fair_speech
				clr_character_flag = made_poor_speech
				clr_character_flag = made_terrible_speech
				clr_character_flag = liege_opinion_increased
				clr_character_flag = liege_opinion_slightly_increased
				clr_character_flag = liege_opinion_slightly_decreased
				clr_character_flag = liege_opinion_decreased
				clr_character_flag = demand_money_liege
				clr_character_flag = demand_support_liege
				clr_character_flag = faction_demand_refused
			}
			if = {
				limit = {
					OR = {
						had_character_flag = { flag = faction_meeting_called days = 365 }
						had_character_flag = { flag = faction_meeting_begun days = 365 }
						had_character_flag = { flag = faction_leadership_challenge days = 365 }
					}
				}
				clr_character_flag = can_make_demand
				clr_character_flag = faction_meeting_called
				clr_character_flag = faction_meeting_begun
				clr_character_flag = faction_meeting_postponed
				clr_character_flag = faction_leadership_challenge
				clr_character_flag = faction_is_happy
				clr_character_flag = faction_is_content
				clr_character_flag = faction_is_unhappy
				clr_character_flag = faction_is_angry
				clr_character_flag = made_great_speech
				clr_character_flag = made_good_speech
				clr_character_flag = made_fair_speech
				clr_character_flag = made_poor_speech
				clr_character_flag = made_terrible_speech
				clr_character_flag = liege_opinion_increased
				clr_character_flag = liege_opinion_slightly_increased
				clr_character_flag = liege_opinion_slightly_decreased
				clr_character_flag = liege_opinion_decreased
				clr_character_flag = demand_money_liege
				clr_character_flag = demand_support_liege
				clr_character_flag = faction_demand_refused
			}
			if = {
				limit = { had_character_flag = { flag = former_faction_leader days = 7 } }
				clr_character_flag = former_faction_leader
			}
			if = {
				limit = { had_character_flag = { flag = faction_reboot days = 7 } }
				clr_character_flag = faction_reboot
			}
		}
	}
}