# Scripted triggers for PlusFaction, prefixed by 'pf_'

pf_leader_trigger = {
	OR = {
		leads_faction = faction_court
		leads_faction = faction_prosperity
		leads_faction = faction_glory
		leads_faction = faction_tradition
	}
}

pf_member_trigger = {
	OR = {
		in_faction = faction_court
		in_faction = faction_prosperity
		in_faction = faction_glory
		in_faction = faction_tradition
	}
}

pf_war_trait_trigger = {
	OR = {
		trait = pf_rebel
		trait = pf_loyalist
		trait = pf_neutral
	}
}

pf_war_flag_trigger = {
	OR = {
		has_character_flag = faction_court_war
		has_character_flag = faction_prosperity_war
		has_character_flag = faction_glory_war
		has_character_flag = faction_tradition_war
		has_character_flag = faction_pretender_war
		has_character_flag = faction_separatist_war
		has_character_flag = faction_religious_war
		has_character_flag = faction_succession_war
		has_character_flag = faction_authority_war
	}
}

pf_has_demands_trigger = {
	OR = {
		has_character_flag = demands_lower_CA
		has_character_flag = demands_lower_TO
		has_character_flag = demands_surrender_title
		has_character_flag = demands_share_wealth
		has_character_flag = demands_revert_succession
		has_character_flag = demands_council_life_terms
		has_character_flag = demands_lower_feudal_obligations
		has_character_flag = demands_lower_tribal_obligations
		has_character_flag = demands_lower_city_obligations
		has_character_flag = demands_lower_temple_obligations
		has_character_flag = demands_seniority
		has_character_flag = demands_primogeniture
		has_character_flag = demands_gavelkind
		has_character_flag = demands_feudal_elective
	}
}

pf_not_regular_faction_war_trigger = {
	OR = {
		has_character_flag = demands_seniority
		has_character_flag = demands_primogeniture
		has_character_flag = demands_gavelkind
		has_character_flag = demands_feudal_elective
		has_character_flag = preparing_claimant_faction_war
		has_character_flag = preparing_religious_faction_war
	}
}

# Not to be confused with "part of this revolt": this is the `in_revolt = yes`
# equivalent for PF revolts (only)
pf_in_revolt_trigger = {
	in_revolt = yes
	any_opinion_modifier_target = {
		has_opinion_modifier = { who = PREV modifier = revolting_against_me }
		any_war = {
			defender = { character = PREV }
			attacker = {
				OR = {
					character = PREVPREVPREV
					is_vassal_or_below = PREVPREVPREV
				}
			}
			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
			}
		}
	}
}

# used inside of an any_faction_backer scope (typically), PREV is assumed to be
# faction leader
pf_my_faction_backer_trigger = {
	NOT = { character = PREV }
	OR = {
		AND = {
			PREV = { leads_faction = faction_court }
			in_faction = faction_court
		}
		AND = {
			PREV = { leads_faction = faction_prosperity }
			in_faction = faction_prosperity
		}
		AND = {
			PREV = { leads_faction = faction_glory }
			in_faction = faction_glory
		}
		AND = {
			PREV = { leads_faction = faction_tradition }
			in_faction = faction_tradition
		}
	}
}

# used inside of an any_vassal scope (typically), ROOT is assumed to be
# faction leader. identical to pf_my_faction_backer_trigger except that
# ROOT is assumed to be the faction leader and not necessarily PREV.
# [ see, this is why it'd be great if scripted triggers and effects could
#   optionally take a scope / target as a right-hand-side parameter too! ]
pf_root_faction_backer_trigger = {
	NOT = { character = ROOT }
	OR = {
		AND = {
			ROOT = { leads_faction = faction_court }
			in_faction = faction_court
		}
		AND = {
			ROOT = { leads_faction = faction_prosperity }
			in_faction = faction_prosperity
		}
		AND = {
			ROOT = { leads_faction = faction_glory }
			in_faction = faction_glory
		}
		AND = {
			ROOT = { leads_faction = faction_tradition }
			in_faction = faction_tradition
		}
	}
}

# used inside of an any_vassal scope (typically), event_target:faction_leader is assumed to be
# faction leader (and must be saved first). identical to pf_my_faction_backer_trigger except that
# event_target:faction_leader is assumed to be the faction leader and not necessarily PREV.
# [ see, this is why it'd be great if scripted triggers and effects could
#   optionally take a scope / target as a right-hand-side parameter too! ]
pf_event_target_faction_backer_trigger = {
	NOT = { character = event_target:faction_leader }
	OR = {
		AND = {
			event_target:faction_leader = { leads_faction = faction_court }
			in_faction = faction_court
		}
		AND = {
			event_target:faction_leader = { leads_faction = faction_prosperity }
			in_faction = faction_prosperity
		}
		AND = {
			event_target:faction_leader = { leads_faction = faction_glory }
			in_faction = faction_glory
		}
		AND = {
			event_target:faction_leader = { leads_faction = faction_tradition }
			in_faction = faction_tradition
		}
	}
}

# see above (redundancy, be gone!)
pf_from_faction_backer_trigger = {
	OR = {
		AND = {
			FROM = { leads_faction = faction_court }
			in_faction = faction_court
		}
		AND = {
			FROM = { leads_faction = faction_prosperity }
			in_faction = faction_prosperity
		}
		AND = {
			FROM = { leads_faction = faction_glory }
			in_faction = faction_glory
		}
		AND = {
			FROM = { leads_faction = faction_tradition }
			in_faction = faction_tradition
		}
	}
}


pf_my_faction_angry_trigger = {
	OR = {
		AND = {
			leads_faction = faction_court
			liege = { has_character_flag = faction_court_angry }
		}
		AND = {
			leads_faction = faction_prosperity
			liege = { has_character_flag = faction_prosperity_angry }
		}
		AND = {
			leads_faction = faction_glory
			liege = { has_character_flag = faction_glory_angry }
		}
		AND = {
			leads_faction = faction_tradition
			liege = { has_character_flag = faction_tradition_angry }
		}
	}
}


pf_councillor_court_trigger = {
	any_voter = { in_faction = faction_court }
}

pf_councillor_prosperity_trigger = {
	any_voter = { in_faction = faction_prosperity }
}

pf_councillor_glory_trigger = {
	any_voter = { in_faction = faction_glory }
}

pf_councillor_tradition_trigger = {
	any_voter = { in_faction = faction_tradition }
}


pf_tradition_approved_war_trigger = {
	any_war = {
		OR = {
			AND = {
				any_attacker = { character = PREVPREV }
				OR = {
					using_cb = crusade
					using_cb = holy_war
					using_cb = excommunicated_ruler
					using_cb = depose_antipope
				}
			}
			AND = {
				any_defender = { character = PREVPREV }
				attacker = { NOT = { religion = PREVPREV } }
				OR = {
					using_cb = tribal_invasion
					using_cb = crusade
					using_cb = holy_war
					using_cb = viking_invasion
					using_cb = pagan_county_conquest
					using_cb = shia_caliphate_rising
					using_cb = manifest_destiny_invasion
					using_cb = tributary_cb
					using_cb = nomad_invasion
				}
			}
		}
	}
}

# NOTE: This trigger can't support multiple simultaneous PF civil wars coherently until an extra layer of semantic relation
# tracking is added to PF. Ergo, one should make sure that there cannot be multiple such wars until such is (re-)added.
pf_part_of_this_revolt_trigger = {
	reverse_has_opinion_modifier = { who = PREV modifier = revolting_against_me }
}

pf_pending_civil_war_trigger = {
	liege = {
		any_vassal = {
			has_character_flag = civil_war_faction_leader
		}
	}
}
