namespace = emf_faction

character_event = {
	id = emf_faction.1000

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = { is_ruler = yes }
			log = "PF STATE DUMP: [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID]):"
		}
		else = {
			log = "PF STATE DUMP: courtier: [This.GetTitledName] ([This.GetID]):"
		}

		log = "<<<<<"

		if = {
			limit = { pf_war_trait_trigger = no }

			if = {
				limit = { pf_in_revolt_trigger = yes }
				log = "no civil war trait but nevertheless in a PF civil war (likely WAD)"
			}
			else = {
				log = "no civil war trait (and not revolting in a PF civil war)"
			}
		}

		if = {
			limit = { trait = pf_rebel }

			if = {
				limit = { is_ruler = no }
				log = "WARNING: trait = pf_rebel but not a ruler!"
			}
			else = {
				if = {
					limit = { pf_in_revolt_trigger = yes }

					if = {
						limit = { has_any_opinion_modifier = revolting_against }
						log = "trait = pf_rebel"
					}
					else = {
						log = "WARNING: trait = pf_rebel but lacking revolting_against TOM!"
					}
				}
				else = {
					if = {
						limit = { has_any_opinion_modifier = revolting_against }
						log = "WARNING: trait = pf_rebel but not in revolt!"
					}
					else = {
						log = "WARNING: trait = pf_rebel but neither in revolt nor assigned a revolting_against TOM!"
					}
				}
			}
		}

		if = {
			limit = { trait = pf_loyalist }

			if = {
				limit = { is_ruler = no }
				log = "WARNING: trait = pf_loyalist but not a ruler!"
			}
			else = {
				if = {
					limit = {
						reverse_has_opinion_modifier = {
							name = pf_loyal_to_me
							who = liege
						}
					}

					if = {
						limit = {
							liege = {
								any_war = {
									defender = { character = PREVPREV }

									OR = {
										using_cb = cb_faction_overthrow_ruler
										using_cb = claim_on_liege_plot
										using_cb = other_claim_on_liege_plot
										using_cb = religious_overthrow_ruler
										using_cb = cb_faction_independence
									}
								}
							}
						}

						log = "trait = pf_loyalist"
					}
					else = {
						log = "WARNING: trait = pf_loyalist w/ pf_loyal_to_me TOM, BUT liege isn't defending against a civil war!"
					}
				}
				else = {
					if = {
						limit = {
							liege = {
								any_war = {
									defender = { character = PREVPREV }

									OR = {
										using_cb = cb_faction_overthrow_ruler
										using_cb = claim_on_liege_plot
										using_cb = other_claim_on_liege_plot
										using_cb = religious_overthrow_ruler
										using_cb = cb_faction_independence
									}
								}
							}
						}

						log = "WARNING: trait = pf_loyalist w/ liege defending against civil war BUT w/o pf_loyal_to_me TOM!"
					}

					else = {
						log = "WARNING: trait = pf_loyalist w/o liege defending against civil war AND w/o pf_loyal_to_me TOM!"
					}
				}
			}
		}

		if = {
			limit = { trait = pf_neutral }

			if = {
				limit = { is_ruler = no }
				log = "WARNING: trait = pf_neutral but not a ruler!"
			}

			else = {
				if = {
					limit = {
						reverse_has_opinion_modifier = {
							name = pf_loyal_to_me
							who = liege
						}
					}

					if = {
						limit = {
							liege = {
								any_war = {
									defender = { character = PREVPREV }

									OR = {
										using_cb = cb_faction_overthrow_ruler
										using_cb = claim_on_liege_plot
										using_cb = other_claim_on_liege_plot
										using_cb = religious_overthrow_ruler
										using_cb = cb_faction_independence
									}
								}
							}
						}

						log = "trait = pf_neutral"
					}
					else = {
						log = "WARNING: trait = pf_neutral w/ pf_neutral_to_me TOM, BUT liege isn't defending against a civil war!"
					}
				}
				else = {
					if = {
						limit = {
							liege = {
								any_war = {
									defender = { character = PREVPREV }

									OR = {
										using_cb = cb_faction_overthrow_ruler
										using_cb = claim_on_liege_plot
										using_cb = other_claim_on_liege_plot
										using_cb = religious_overthrow_ruler
										using_cb = cb_faction_independence
									}
								}
							}
						}
						log = "WARNING: trait = pf_neutral w/ liege defending against civil war BUT w/o pf_neutral_to_me TOM!"
					}
					else = {
						log = "WARNING: trait = pf_neutral w/o liege defending against civil war AND w/o pf_neutral_to_me TOM!"
					}
				}
			}
		}

		if = {
			limit = { in_revolt = yes }
			log = "in_revolt = yes"
		}
		else = {
			log = "in_revolt = no"
		}

		liege = {
			if = {
				limit = { character = PREV }
				log = "liege: <self>"
			}
			else = {
				log = "liege: [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}

			save_event_target_as = pf_log_liege
		}

		liege_before_war = {
			if = {
				limit = { character = event_target:pf_log_liege }
				log = "liege_before_war: <same as liege>"
			}
			else = {
				log = "liege_before_war: [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
		}

		clear_event_target = pf_log_liege

		if = {
			limit = {
				any_opinion_modifier_target = {
					count = 2

					reverse_has_opinion_modifier = {
						name = revolting_against
						who = PREV
					}
				}
			}

			log = "WARNING: revolting_against more than 1 character!"
		}

		any_opinion_modifier_target = {
			limit = {
				reverse_has_opinion_modifier = {
					name = revolting_against
					who = PREV
				}
			}

			if = {
				limit = {
					has_opinion_modifier = {
						name = revolting_against_me
						who = PREV
					}
				}

				log = "revolting_against -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
			else = {
				log = "WARNING: revolting_against w/o [reverse] revolting_against_me -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
		}

		any_opinion_modifier_target = {
			limit = {
				has_opinion_modifier = {
					name = revolting_against
					who = PREV
				}
			}

			if = {
				limit = {
					reverse_has_opinion_modifier = {
						name = revolting_against_me
						who = PREV
					}
				}

				log = "revolting_against_me -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
			else = {
				log = "WARNING: revolting_against_me w/o [reverse] revolting_against -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
		}

		if = {
			limit = {
				any_opinion_modifier_target = {
					count = 2

					reverse_has_opinion_modifier = {
						name = pf_loyal_toward
						who = PREV
					}
				}
			}

			log = "WARNING: pf_loyal_toward more than 1 character!"
		}

		any_opinion_modifier_target = {
			limit = {
				reverse_has_opinion_modifier = {
					name = pf_loyal_toward
					who = PREV
				}
			}

			if = {
				limit = {
					has_opinion_modifier = {
						name = pf_loyal_to_me
						who = PREV
					}
				}

				log = "pf_loyal_toward -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
			else = {
				log = "WARNING: pf_loyal_toward w/o [reverse] pf_loyal_to_me -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
		}

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

			if = {
				limit = {
					has_opinion_modifier = {
						name = pf_loyal_toward
						who = PREV
					}
				}

				log = "pf_loyal_to_me -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
			else = {
				log = "WARNING: pf_loyal_to_me w/o [reverse] pf_loyal_toward -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
		}

		if = {
			limit = {
				any_opinion_modifier_target = {
					count = 2

					reverse_has_opinion_modifier = {
						name = pf_neutral_toward
						who = PREV
					}
				}
			}

			log = "WARNING: pf_neutral_toward more than 1 character!"
		}

		any_opinion_modifier_target = {
			limit = {
				reverse_has_opinion_modifier = {
					name = pf_neutral_toward
					who = PREV
				}
			}

			if = {
				limit = {
					has_opinion_modifier = {
						name = pf_neutral_to_me
						who = PREV
					}
				}

				log = "pf_neutral_toward -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
			else = {
				log = "WARNING: pf_neutral_toward w/o [reverse] pf_neutral_to_me -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
		}

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

			if = {
				limit = {
					has_opinion_modifier = {
						name = pf_neutral_toward
						who = PREV
					}
				}

				log = "pf_neutral_to_me -> [This.GetTitledFirstName] of the [This.PrimaryTitle.GetFullBaseName] ([This.GetID])"
			}
		}

		if = {
			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
					}
				}
			}

			if = {
				limit = { has_character_flag = faction_court_war }
				log = "war flag: faction_court_war"
			}
			if = {
				limit = { has_character_flag = faction_prosperity_war }
				log = "war flag: faction_prosperity_war"
			}
			if = {
				limit = { has_character_flag = faction_glory_war }
				log = "war flag: faction_glory_war"
			}
			if = {
				limit = { has_character_flag = faction_tradition_war }
				log = "war flag: faction_tradition_war"
			}
			if = {
				limit = { has_character_flag = faction_pretender_war }
				log = "war flag: faction_pretender_war"
			}
			if = {
				limit = { has_character_flag = faction_separatist_war }
				log = "war flag: faction_separatist_war"
			}
			if = {
				limit = { has_character_flag = faction_religious_war }
				log = "war flag: faction_religious_war"
			}
			if = {
				limit = { has_character_flag = faction_succession_war }
				log = "war flag: faction_succession_war"
			}
			if = {
				limit = { has_character_flag = faction_authority_war }
				log = "war flag: faction_authority_war"
			}
		}
		else = {
			if = {
				limit = { has_character_flag = faction_court_war }
				log = "WARNING: stale war flag: faction_court_war"
			}
			if = {
				limit = { has_character_flag = faction_prosperity_war }
				log = "WARNING: stale war flag: faction_prosperity_war"
			}
			if = {
				limit = { has_character_flag = faction_glory_war }
				log = "WARNING: stale war flag: faction_glory_war"
			}
			if = {
				limit = { has_character_flag = faction_tradition_war }
				log = "WARNING: stale war flag: faction_tradition_war"
			}
			if = {
				limit = { has_character_flag = faction_pretender_war }
				log = "WARNING: stale war flag: faction_pretender_war"
			}
			if = {
				limit = { has_character_flag = faction_separatist_war }
				log = "WARNING: stale war flag: faction_separatist_war"
			}
			if = {
				limit = { has_character_flag = faction_religious_war }
				log = "WARNING: stale war flag: faction_religious_war"
			}
			if = {
				limit = { has_character_flag = faction_succession_war }
				log = "WARNING: stale war flag: faction_succession_war"
			}
			if = {
				limit = { has_character_flag = faction_authority_war }
				log = "WARNING: stale war flag: faction_authority_war"
			}
		}

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

			if = {
				limit = { has_character_flag = demands_lower_CA }
				log = "demands flag: demands_lower_CA"
			}
			if = {
				limit = { has_character_flag = demands_lower_TO }
				log = "demands flag: demands_lower_TO"
			}
			if = {
				limit = { has_character_flag = demands_surrender_title }
				log = "demands flag: demands_surrender_title"
			}
			if = {
				limit = { has_character_flag = demands_share_wealth }
				log = "demands flag: demands_share_wealth"
			}
			if = {
				limit = { has_character_flag = demands_revert_succession }
				log = "demands flag: demands_revert_succession"
			}
			if = {
				limit = { has_character_flag = demands_council_life_terms }
				log = "demands flag: demands_council_life_terms"
			}
			if = {
				limit = { has_character_flag = demands_lower_feudal_obligations }
				log = "demands flag: demands_lower_feudal_obligations"
			}
			if = {
				limit = { has_character_flag = demands_lower_tribal_obligations }
				log = "demands flag: demands_lower_tribal_obligations"
			}
			if = {
				limit = { has_character_flag = demands_lower_city_obligations }
				log = "demands flag: demands_lower_city_obligations"
			}
			if = {
				limit = { has_character_flag = demands_lower_temple_obligations }
				log = "demands flag: demands_lower_temple_obligations"
			}

			if = {
				limit = { has_character_flag = demands_seniority }

				log = "demands flag: demands_seniority"

				liege_before_war = {
					if = {
						limit = { any_demesne_title = { has_title_flag = change_seniority } }

						any_demesne_title = {
							limit = { has_title_flag = change_seniority }
							log = "disputed succession law title: [This.GetFullBaseName]"
						}
					}
					else = {
						log = "WARNING: demands a succession law change, but defender has no title marked for such a demand"
					}
				}
			}

			if = {
				limit = { has_character_flag = demands_primogeniture }

				log = "demands flag: demands_primogeniture"

				liege_before_war = {
					if = {
						limit = { any_demesne_title = { has_title_flag = change_primogeniture } }

						any_demesne_title = {
							limit = { has_title_flag = change_primogeniture }
							log = "disputed succession law title: [This.GetFullBaseName]"
						}
					}
					else = {
						log = "WARNING: demands a succession law change, but defender has no title marked for such a demand"
					}
				}
			}

			if = {
				limit = { has_character_flag = demands_gavelkind }

				log = "demands flag: demands_gavelkind"

				liege_before_war = {
					if = {
						limit = { any_demesne_title = { has_title_flag = change_gavelkind } }

						any_demesne_title = {
							limit = { has_title_flag = change_gavelkind }
							log = "disputed succession law title: [This.GetFullBaseName]"
						}
					}
					else = {
						log = "WARNING: demands a succession law change, but defender has no title marked for such a demand"
					}
				}
			}

			if = {
				limit = { has_character_flag = demands_feudal_elective }

				log = "demands flag: demands_feudal_elective"

				liege_before_war = {
					if = {
						limit = { any_demesne_title = { has_title_flag = change_feudal_elective } }

						any_demesne_title = {
							limit = { has_title_flag = change_feudal_elective }
							log = "disputed succession law title: [This.GetFullBaseName]"
						}
					}
					else = {
						log = "WARNING: demands a succession law change, but defender has no title marked for such a demand"
					}
				}
			}
		}
		else = {
			if = {
				limit = { has_character_flag = demands_lower_CA }
				log = "WARNING: stale demands flag: demands_lower_CA"
			}
			if = {
				limit = { has_character_flag = demands_lower_TO }
				log = "WARNING: stale demands flag: demands_lower_TO"
			}
			if = {
				limit = { has_character_flag = demands_surrender_title }
				log = "WARNING: stale demands flag: demands_surrender_title"
			}
			if = {
				limit = { has_character_flag = demands_share_wealth }
				log = "WARNING: stale demands flag: demands_share_wealth"
			}
			if = {
				limit = { has_character_flag = demands_revert_succession }
				log = "WARNING: stale demands flag: demands_revert_succession"
			}
			if = {
				limit = { has_character_flag = demands_council_life_terms }
				log = "WARNING: stale demands flag: demands_council_life_terms"
			}
			if = {
				limit = { has_character_flag = demands_lower_feudal_obligations }
				log = "WARNING: stale demands flag: demands_lower_feudal_obligations"
			}
			if = {
				limit = { has_character_flag = demands_lower_tribal_obligations }
				log = "WARNING: stale demands flag: demands_lower_tribal_obligations"
			}
			if = {
				limit = { has_character_flag = demands_lower_city_obligations }
				log = "WARNING: stale demands flag: demands_lower_city_obligations"
			}
			if = {
				limit = { has_character_flag = demands_lower_temple_obligations }
				log = "WARNING: stale demands flag: demands_lower_temple_obligations"
			}
			if = {
				limit = { has_character_flag = demands_seniority }
				log = "WARNING: stale demands flag: demands_seniority"
			}
			if = {
				limit = { has_character_flag = demands_primogeniture }
				log = "WARNING: stale demands flag: demands_primogeniture"
			}
			if = {
				limit = { has_character_flag = demands_gavelkind }
				log = "WARNING: stale demands flag: demands_gavelkind"
			}
			if = {
				limit = { has_character_flag = demands_feudal_elective }
				log = "WARNING: stale demands flag: demands_feudal_elective"
			}
		}

		log = ">>>>>"
	}
}


character_event = {
	id = emf_faction.1001

	hide_window = yes

	trigger = {
		OR = {
			AND = {
				OR = {
					trait = pf_rebel
					has_any_opinion_modifier = revolting_against
				}

				NAND = {
					is_ruler = yes
					pf_in_revolt_trigger = yes
				}
			}

			AND = {
				trait = pf_loyalist

				NAND = {
					is_ruler = yes
					has_opinion_modifier = { who = liege name = pf_loyal_toward }
					reverse_has_opinion_modifier = { who = liege name = pf_loyal_to_me }
				}
			}

			AND = {
				trait = pf_neutral

				NAND = {
					is_ruler = yes
					has_opinion_modifier = { who = liege name = pf_neutral_toward }
					reverse_has_opinion_modifier = { who = liege name = pf_neutral_to_me }
				}
			}

			AND = {
				OR = {
					trait = pf_neutral
					trait = pf_loyalist
					has_any_opinion_modifier = pf_loyal_toward
					has_any_opinion_modifier = pf_neutral_toward
				}

				liege = {
					OR = {
						character = PREV

						NOT = {
							any_war = {
								defender = { character = PREVPREV }

								OR = {
									using_cb = cb_faction_overthrow_ruler
									using_cb = claim_on_liege_plot
									using_cb = other_claim_on_liege_plot
									using_cb = religious_overthrow_ruler
									using_cb = cb_faction_independence
								}
							}
						}
					}
				}
			}
		}
	}

	mean_time_to_happen = {
		days = 1
	}

	immediate = {
		log = "ERROR: stray rebel/neutral/loyalist state (checked periodically, so more info needed); dump follows:"
		pf_log_state_effect = yes

		random_player = {
			limit = {
				is_multiplayer_host_character = yes
				has_global_flag = emf_debug
			}

			character_event = { id = emf_faction.1003 }
		}

		pf_teardown_rebel_effect = yes
		pf_teardown_neutral_effect = yes
		pf_teardown_loyalist_effect = yes
	}
}

character_event = {
	id = emf_faction.1002

	hide_window = yes

	only_rulers = yes

	trigger = {
		OR = {
			has_any_opinion_modifier = revolting_against_me
			has_any_opinion_modifier = pf_loyal_to_me
			has_any_opinion_modifier = pf_neutral_to_me
			pf_war_flag_trigger = yes
		}

		NOT = {
			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
				}
			}
		}
	}

	mean_time_to_happen = {
		days = 1
	}

	immediate = {
		log = "ERROR: stray civil war liege state (checked periodically, so more info needed); dump follows:"
		pf_log_state_effect = yes

		random_player = {
			limit = {
				is_multiplayer_host_character = yes
				has_global_flag = emf_debug
			}

			character_event = { id = emf_faction.1003 }
		}

		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV name = revolting_against_me } }
			reverse_remove_opinion = { who = PREV name = revolting_against_me }
			remove_opinion = { who = PREV name = revolting_against }
		}
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV name = pf_loyal_to_me } }
			reverse_remove_opinion = { who = PREV name = pf_loyal_to_me }
			remove_opinion = { who = PREV name = pf_loyal_toward }
		}
		any_opinion_modifier_target = {
			limit = { reverse_has_opinion_modifier = { who = PREV name = pf_neutral_to_me } }
			reverse_remove_opinion = { who = PREV name = pf_neutral_to_me }
			remove_opinion = { who = PREV name = pf_neutral_toward }
		}

		pf_clear_war_flags_effect = yes
	}
}


character_event = {
	id = emf_faction.1003
	picture = GFX_evt_bloody_man
	border = GFX_event_normal_frame_war

	desc = {
		text = emf_faction.1003.desc_courtier
		trigger = { FROM = { is_ruler = no } }
	}

	desc = {
		text = emf_faction.1003.desc_ruler
		trigger = { FROM = { is_ruler = yes } }
	}

	is_triggered_only = yes

	option = { name = ALAS }
}