# Targeted decisions are possible vs _all_ other characters and shown are in the Diplomacy View, not the Intrigue View. The taker is in the FROM scope.
#
# filter = [self/court/home_court/vassals/sub_realm/realm/dynasty/all] ('self' MUST be set for decisions targetting only the taker, the other filter types can be set to lessen CPU load)
# ai_target_filter = [self/court/home_court/vassals/sub_realm/realm/dynasty/all] (which characters for which the AI evaluates the decision.)
#	court: all characters in the AI's host court, including prisoners, and characters currently away (wards, prisoners, etc)
#	home_court: all characters in the AI's home court, including prisoners, and characters currently away (wards, prisoners, etc)
#	vassals: direct vassal rulers of the AI's employer
#	sub_realm: all characters below the AI's employer
#	realm: all characters in the same top realm as the AI
#	dynasty: all members of the same dynasty
#	rivals: your rivals plus any character you have an opinion modifier with 'crime = yes' set (the same set of characters the 'is_foe' trigger evaluates)
#	all: all living characters (Avoid if possible. VERY CPU-HEAVY!)
#

targeted_decisions = {

	commit_suicide = {
		filter = self

		ai_target_filter = self
		ai_check_interval = 12

		potential = {
			OR = {
				trait = depressed

				trigger_if = {
					limit = { has_dlc = "Reapers" }
					is_incapable = yes
				}
			}

			is_adult = yes
			immortal = no
			prisoner = no
			NOT = { has_character_flag = attempting_suicide }
		}

		allow = {
			custom_tooltip = { text = commit_suicide_allow_tooltip }

			custom_tooltip = {
				text = is_not_busy_trigger_tooltip

				trigger_if = {
					limit = { in_seclusion = no }
					is_inaccessible_trigger = no
				}
			}
		}

		effect = {
			if = {
				limit = { has_dlc = "Reapers" }
				set_character_flag = attempting_suicide

				if = {
					limit = { is_incapable = yes }
					character_event = { id = RIP.30210 }
				}
				else = {
					character_event = { id = RIP.30200 }
				}
			}
			else = {
				prestige = -200

				add_character_modifier = {
					name = suicide
					duration = -1
				}

				death = { death_reason = death_suicide }

				random = {
					chance = 35

					mult_modifier = {
						factor = 0.3
						has_nickname = yes
					}

					give_nickname = nick_the_suicide
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				NOT = { trait = zealous }
			}

			mult_modifier = {
				factor = 2
				wealth < -100
			}

			mult_modifier = {
				factor = 2.5
				wealth < -300
			}

			mult_modifier = {
				factor = 2
				trait = stressed
			}

			mult_modifier = {
				factor = 2 # Wrecked with Guilt
				is_kinslayer_trigger = yes
			}
		}
	}

	legitimize_bastard = {
		filter = close_relative

		ai_target_filter = close_relatives
		ai_check_interval = 30

		only_playable = yes

		from_potential = {
			religion_group = christian
			has_polygamy = no # CleanSlate: There is no reason to restrict this to non-polygamists.

			trigger_if = {
				limit = { is_female = yes }
				is_married_matrilineally = yes
			}

			OR = {
				any_child = {
					trait = bastard
					is_alive = yes
				}

				father_even_if_dead = {
					is_alive = no

					any_child = {
						trait = bastard
						is_alive = yes
						NOT = { character = FROM }
					}
				}

				mother_even_if_dead = {
					is_alive = no

					any_child = {
						trait = bastard
						is_alive = yes
						NOT = { character = FROM }
					}
				}
			}
		}

		potential = {
			trait = bastard

			OR = {
				is_child_of = FROM

				father_even_if_dead = {
					is_alive = no
					is_father = FROM
				}

				mother_even_if_dead = {
					is_alive = no
					is_mother = FROM
				}
			}

			OR = {
				is_vassal_or_below_of = FROM

				AND = {
					same_realm = FROM
					real_tier = FROM
				}
			}
		}

		allow = {
			FROM = {
				show_scope_change = no

				trigger_if = {
					limit = {
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = bloodline_legitimize
								bloodline_is_active_for = PREV
							}
						}
					}

					piety >= 50
				}
			}
		}

		effect = {
			if = {
				limit = {
					is_child_of = FROM
					FROM = { is_female = no }
				}

				set_father = FROM
			}

			set_real_father = 0

			hidden_effect = {
				any_child = {
					limit = { dynasty = ROOT }

					dynasty = FROM

					any_child = {
						limit = { dynasty = ROOT }

						dynasty = FROM
					}
				}
			}

			dynasty = FROM
			remove_trait = bastard
			add_trait = legit_bastard
			recalc_succession = yes

			# Take the piety price from the legitimizer
			FROM = {
				show_scope_change = no

				if = {
					limit = {
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = bloodline_legitimize
								bloodline_is_active_for = PREV
							}
						}
					}
					piety = -50
				}
			}

			# Release Bastard if imprisoned
			if = {
				limit = {
					prisoner = yes
					host = { character = FROM }
				}

				imprison = no
			}

			# Bastard's other parent is pleased
			if = {
				limit = {
					FROM = { is_female = yes }
				}

				father = {
					show_scope_change = no

					opinion = {
						name = legitimized_child
						who = FROM
						years = 10
					}
				}
			}
			else = {
				mother = {
					show_scope_change = no

					opinion = {
						name = legitimized_child
						who = FROM
						years = 10
					}
				}
			}

			FROM = {
				show_scope_change = no

				# Spouses are angry
				any_spouse = {
					show_scope_change = no

					limit = { NOT = { is_parent_of = ROOT } }

					opinion = {
						name = legitimized_bastard
						who = ROOT_FROM
						years = 20
					}
				}

				# Non-bastard children are angry
				if = {
					limit = {
						any_child = {
							dynasty = ROOT_FROM

							NOR = {
								character = ROOT
								trait = legit_bastard
							}
						}
					}

					custom_tooltip = {
						text = any_child_gets_angry_tt

						any_child = {
							show_scope_change = no

							limit = {
								dynasty = ROOT_FROM

								NOR = {
									character = ROOT
									trait = legit_bastard
								}
							}

							opinion = {
								name = legitimized_bastard
								who = ROOT_FROM
								years = 1
							}
						}
					}
				}
			}

			if = {
				limit = {
					OR = {
						father_even_if_dead = {
							is_alive = no
							is_father = FROM
						}

						mother_even_if_dead = {
							is_alive = no
							is_father = FROM
						}
					}
				}

				FROM = {
					show_scope_change  =no

					any_sibling = {
						show_scope_change = no

						limit = {
							dynasty = FROM

							NOR = {
								trait = legit_bastard
								character = ROOT
							}
						}

						opinion = {
							name = legitimized_bastard
							who = FROM
							years = 1
						}
					}

					any_child = {
						limit = {
							dynasty = FROM

							NOR = {
								trait = legit_bastard
								character = ROOT
							}
						}

						opinion = {
							name = legitimized_bastard
							who = FROM
							years = 5
						}
					}
				}
			}

		}

		ai_will_do = {
			factor = 1

			trigger = {
				has_character_flag = acknowledged_bastard

				FROM = {
					has_character_flag = will_acknowledge_bastards
				}

				father_even_if_dead = {
					is_alive = yes
				}
			}
		}
	}

	# Muslim ruler tries to convince a dynasty member to lose the 'Decadent' Trait
	convince_to_straighten_up = {
		filter = dynasty
		ai_target_filter = dynasty
		ai_check_interval = 6

		hide_in_decisions_list = yes # This decision is shown in the Religion View for each decadent character

		only_playable = yes

		from_potential = {
			uses_decadence = yes

			trigger_if = {
				limit = { independent = no }

				NOT = {
					any_liege = {
						dynasty = FROM
					}
				}
			}
		}

		potential = {
			trait = decadent
			lower_real_tier_than = FROM
			is_incapable = no
		}

		allow = {
			FROM = {
				OR = {
					custom_tooltip = {
						text = NON_RULER_PIETY_50

						ROOT = { is_ruler = no }
						piety >= 50
					}

					custom_tooltip = {
						text = IS_RULER_PIETY_100

						ROOT = { is_ruler = yes }
						piety >= 100
					}
				}

				NOT = { trait = decadent }
				has_regent = no

				custom_tooltip = {
					text = NOT_ALREADY_NEGOTIATED

					ROOT = {
						NOT = {
							has_opinion_modifier = {
								name = negotiated_to_drop_decadence
								who = FROM
							}
						}
					}
				}
			}

			ROOT = {
				has_regent = no
				NOT = { trait = on_hajj }
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				if = {
					limit = { ROOT = { is_ruler = yes } }
					piety = -100
				}
				else = {
					piety = -50
				}
			}

			hidden_effect = {
				set_character_flag = negotiated_to_drop_decadence

				opinion = {
					name = negotiated_to_drop_decadence
					who = FROM
					years = 100
				}

				character_event = { id = 91350 } # Try to convince to lose 'Decadent' trait
			}
		}

		ai_will_do = {
			factor = 0.1

			mult_modifier = {
				factor = 0.5

				reverse_opinion = {
					who = FROM
					value >= 25
				}
			}

			mult_modifier = {
				factor = 0.5

				reverse_opinion = {
					who = FROM
					value >= 50
				}
			}

			mult_modifier = {
				factor = 2

				reverse_opinion = {
					who = FROM
					value < 0
				}
			}

			mult_modifier = {
				factor = 2

				reverse_opinion = {
					who = FROM
					value < -25
				}
			}

			mult_modifier = {
				factor = 2

				reverse_opinion = {
					who = FROM
					value < -50
				}
			}

			mult_modifier = {
				factor = 0.5
				FROM = { trait = slothful }
			}

			mult_modifier = {
				factor = 0.5
				FROM = { trait = kind }
			}

			mult_modifier = {
				factor = 0.5
				FROM = { trait = patient }
			}

			mult_modifier = {
				factor = 0.5
				FROM = { trait = content }
			}

			mult_modifier = {
				factor = 2
				FROM = { trait = diligent }
			}

			mult_modifier = {
				factor = 2
				FROM = { trait = cruel }
			}

			mult_modifier = {
				factor = 2
				FROM = { trait = wroth }
			}

			mult_modifier = {
				factor = 5
				FROM = { trait = zealous }
			}
		}
	}

	# Unhardcoded decision
	order_to_take_vows = {
		filter = court
		ai_target_filter = court
		ai_check_interval = 120

		only_playable = yes

		from_potential = {
			has_dlc = "Sons of Abraham"
			is_adult = yes
			prisoner = no
			has_regent = no

			OR = {
				trigger_if = {
					limit = { is_alternate_start = no }

					OR = {
						religion_group = christian
						has_dharmic_religion_trigger = yes
						religion = manichaean
						religion_openly_bon_or_reformed_trigger = yes
						religion_openly_hellenic_or_reformed_trigger = yes
						religion_openly_roman_or_reformed_trigger = yes

						trigger_if = {
							limit = { culture = roman }

							OR = { # Somewhat compatible when mixed with Roman culture.
								religion_group = christian
								religion_group = jewish_group
								religion = bon_pagan
								religion = buddhist
							}
						}
					}
				}

				has_religion_feature = religion_monastic
				has_religion_feature = religion_feature_west_african
			}
		}

		potential = {
			host = { character = FROM }
			religion = FROM
			is_ruler = no
			is_adult = yes
			is_incapable = no
			is_not_ascetic_trigger = yes
		}

		allow = {
			trigger_if = {
				limit = {
					dynasty = FROM

					any_owned_bloodline = {
						has_bloodline_flag = bloodline_easy_monks
						bloodline_is_active_for = FROM
						bloodline_is_active_for = ROOT
					}
				}

				# Matilde's Bloodline
				custom_tooltip = {
					text = BLOODLINE_EASIER_VOWS_TOOLTIP
				}
			}
			trigger_else = {
				OR = {
					prisoner = yes

					AND = {
						is_heir = no
						is_married = no
					}
				}
			}

			FROM = {
				piety >= 50
			}
		}

		effect = {
			FROM = {
				show_scope_change = no
				piety = -50
			}

			add_ascetic_trait_effect = yes # Adds religion-based monk-traits

			# Prisoners are freed upon taking the vows
			if = {
				limit = { prisoner = yes }
				imprison = no
			}

			# The vows mean no marriage
			if = {
				limit = { is_married = yes }

				any_spouse = {
					show_scope_change = no

					remove_spouse = ROOT
				}
			}

			# And no consorts
			if = {
				limit = { is_senior_consort_party = yes }

				any_consort = {
					show_scope_change = no

					remove_consort = ROOT
				}
			}

			# Monks and Mystics actually join societies
			if = {
				limit = {
					has_dlc = "Mystics"
					is_in_society = no
					is_heretic = no
				}

				if = {
					limit = { is_catholic_branch = yes }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society

						random_list = {
							50 = {
								trigger = { religion = catholic }
								join_society = monastic_order_benedictine
							}
							50 = { join_society = monastic_order_dominican }
						}
					}
				}
				else_if = {
					limit = { is_orthodox_branch = yes }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_orthodox
					}
				}
				else_if = {
					limit = { is_nestorian_branch = yes }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_nestorian
					}
				}
				else_if = {
					limit = { is_miaphysite_branch = yes }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_monophysite
					}
				}
				else_if = {
					limit = { religion = hindu }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_hindu
					}
				}
				else_if = {
					limit = { religion = buddhist }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_buddhist
					}
				}
				else_if = {
					limit = { religion = jain }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_jain
					}
				}
				# CK2Plus
				else_if = {
					limit = { is_apostolic_branch = yes }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_apostolic
					}
				}
				else_if = {
					limit = { religion_openly_bon_or_reformed_trigger = yes }

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_bon
					}
				}
				else_if = {
					limit = {
						OR = {
							religion_openly_hellenic_or_reformed_trigger = yes
							religion_openly_roman_or_reformed_trigger = yes

							trigger_if = {
								limit = { culture = roman }

								OR = { # Somewhat compatible when mixed with Roman culture.
									religion_group = christian
									religion_group = jewish_group
									religion = bon_pagan
									religion = buddhist
								}
							}
						}
					}

					custom_tooltip = {
						text = tooltip_vows_monk_joins_society
						join_society = monastic_order_stoics
					}
				}
			}

			# Moves to Theocracy
			if = {
				limit = {
					any_realm_lord = {
						is_theocracy = yes
						religion = ROOT
					}
				}

				custom_tooltip = {
					text = tooltip_vows_monk_goes_to_theocracy

					random_realm_lord = {
						limit = {
							is_theocracy = yes
							religion = ROOT
						}

						preferred_limit = {
							reverse_opinion = {
								who = FROM
								value >= 20
							}
							vassal_of = FROM
							# The one they just joined
							same_society_as = ROOT
						}

						preferred_limit = {
							vassal_of = FROM
							# The one they just joined
							same_society_as = ROOT
						}

						preferred_limit = {
							# The one they just joined
							same_society_as = ROOT
						}

						preferred_limit = {
							vassal_of = FROM
						}

						ROOT = { move_character = PREV }
					}
				}
			}

			# People have opinions
			if = {
				limit = {
					dynasty = FROM
					NOT = { trait = zealous }
				}

				opinion = {
					name = opinion_cloistered_me_family
					who = FROM
					years = 20
				}
			}
			else_if = {
				limit = {
					NOR = {
						dynasty = FROM
						trait = zealous
					}
				}

				opinion = {
					name = opinion_cloistered_me
					who = FROM
					years = 20
				}
			}

			# Child reacts
			if = {
				limit = { is_child_of = FROM }

				FROM = {
					character_event = { id = HFP.23001 }
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				FROM = {
					opinion = {
						who = ROOT
						value < 50
					}

					# I'm not a good ruler or they are not a dynastic heir
					trigger_if = {
						limit = { is_benevolent_trigger = yes }

						opinion = {
							who = ROOT
							value < 0
						}
					}
					trigger_else_if = {
						limit = { is_parent_of = ROOT }
						NOT = { dynasty = ROOT }
					}

					# Don't hate you enough to send you off
					trigger_if = {
						limit = { is_benevolent_trigger = yes }

						ROOT = {
							OR = {
								is_child_of = FROM
								prisoner = no

								reverse_opinion = {
									who = FROM
									value >= -20
								}
							}
						}
					}
				}
			}

			# Must protect the line
			mult_modifier = {
				factor = 5

				ROOT = {
					is_child_of = FROM
					NOT = { dynasty = FROM }
				}
			}

			# Hate you
			mult_modifier = {
				factor = 5

				ROOT = {
					is_child_of = FROM
					is_rival = FROM
				}
			}

			# There are better spares
			mult_modifier = {
				factor = 20

				ROOT = {
					is_child_of = FROM

					OR = {
						has_negative_congenital_trigger = yes
						has_infertile_trait_trigger = yes
						has_severe_disability_trigger = yes
					}
				}

				FROM = {
					any_child = {
						is_alive = yes
						has_negative_congenital_trigger = no
						has_infertile_trait_trigger = no
						has_severe_disability_trigger = no
					}
				}
			}

			# Must keep the realm from falling to evil
			mult_modifier = {
				factor = 5

				ROOT = { is_evil_trigger = yes }

				FROM = {
					is_benevolent_trigger = yes

					any_child = {
						is_alive = yes
						is_evil_trigger = no
					}
				}
			}
		}
	}

	send_child_into_hiding = {
		filter = close_relative
		ai_target_filter = close_relatives
		ai_check_interval = 12

		is_high_prio = yes

		only_rulers = yes

		from_potential = {
			has_living_children = yes
			has_regent = no

			trigger_if = {
				limit = { ai = yes }
				has_education_intrigue_trigger = yes
			}
		}

		potential = {
			is_child_of = FROM
			host = { character = FROM }
			is_ruler = no
			prisoner = no

			NOR = {
				is_married = FROM
				has_character_modifier = went_out_of_hiding_timer

				# CleanSlate: Not if you're plotting against your own child (you monster)
				trigger_if = {
					limit = { is_plot_target_of = FROM }

					FROM = {
						has_plot = plot_kill_character
					}
				}
			}
		}

		allow = {
			custom_tooltip = {
				text = is_target_of_murder_plot_tooltip

				OR = {
					has_character_modifier = suspected_murder_plot

					FROM = {
						any_known_plotter = {
							target = ROOT

							OR = {
								has_plot = plot_kill_character
								has_plot = plot_kill_spouse
								has_plot = plot_take_revenge
							}
						}
					}
				}
			}

			custom_tooltip = {
				text = recently_went_out_of_hiding_tooltip

				NOT = { has_character_modifier = went_out_of_hiding_timer }
			}

			is_inaccessible_trigger = no
			has_job_title = no
			in_command_trigger = no
		}

		effect = {
			custom_tooltip = {
				text = go_into_hiding_info_tooltip

				add_trait = in_hiding
				set_character_flag = do_not_disturb
				set_character_flag = in_hiding_murder
			}

			hidden_effect = {
				any_plotter = {
					target = ROOT

					limit = {
						OR = {
							has_plot = plot_kill_character
							has_plot = plot_kill_spouse
							has_plot = plot_take_revenge
						}
					}

					if = {
						# CleanSlate: Prevent the AI from firing unneeded notification events
						limit = { ai = yes }
						# CleanSlate: Deactivate the plot directly instead of firing the event that deactivates it
						activate_plot = no
					}
					else = {
						# CleanSlate: Players can have notification events
						if = {
							limit = { is_plot_active = yes }
							character_event = { id = CM.6180 } # Notify, deactivate plot
						}
						else = {
							character_event = { id = CM.6004 } # Notify
						}
					}

					any_backed_character = {
						limit = { ai = no }
						character_event = { id = CM.6004 } # Notify
					}
				}

				any_player = {
					limit = {
						OR = {
							is_close_relative = ROOT
							is_friend = ROOT
							is_lover = ROOT
							is_rival = ROOT
						}

						NOR = {
							character = FROM

							trigger_if = {
								limit = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}
								}

								plot_target_char = {
									character = ROOT
								}
							}

							any_backed_character = {
								OR = {
									has_plot = plot_kill_character
									has_plot = plot_kill_spouse
								}

								plot_target_char = {
									character = ROOT
								}
							}
						}
					}

					character_event = { id = CM.6004 } # Notify
				}
			}
		}

		ai_will_do = {
			factor = 0.25

			trigger = {
				trigger_if = {
					limit = {
						NOT = { is_primary_heir = FROM }
					}

					trigger_if = {
						limit = { is_female = yes }

						OR = {
							has_law = true_cognatic_succession
							has_law = enatic_cognatic_succession
							has_law = enatic_succession
						}
					}
					trigger_else = {
						OR = {
							has_law = true_cognatic_succession
							has_law = cognatic_succession
							has_law = agnatic_succession
						}
					}
				}
			}

			mult_modifier = {
				factor = 0.5

				FROM = {
					opinion = {
						who = ROOT
						value < -25
					}
				}
			}

			mult_modifier = {
				factor = 1.5

				FROM = {
					opinion = {
						who = ROOT
						value >= 25
					}
				}
			}

			mult_modifier = {
				factor = 5
				FROM = { trait = in_hiding }
			}

			mult_modifier = {
				factor = 10
				FROM = { trait = paranoid }
			}
		}
	}

	take_child_out_of_hiding = {
		filter = close_relative
		ai_target_filter = close_relatives
		ai_check_interval = 12

		is_high_prio = yes

		only_rulers = yes

		from_potential = {
			has_living_children = yes
			has_regent = no
		}

		potential = {
			is_child_of = FROM
			has_character_flag = in_hiding_murder
			host = { character = FROM }
			is_ruler = no
			prisoner = no
			NOT = { is_married = FROM }
		}

		allow = {
			trait = in_hiding
		}

		effect = {
			custom_tooltip = {
				text = come_out_of_hiding_info_tooltip

				clr_character_flag = in_hiding_murder
				clr_character_flag = do_not_disturb
				remove_trait = in_hiding

				add_character_modifier = {
					name = went_out_of_hiding_timer
					months = 6
					hidden = yes
				}

				# Notify player close family and plotters
				any_player = {
					limit = {
						OR = {
							is_close_relative = ROOT
							is_friend = ROOT
							is_lover = ROOT
							is_rival = ROOT

							trigger_if = {
								limit = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}
								}

								plot_target_char = {
									character = ROOT
								}
							}
							trigger_else = {
								any_backed_character = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}

									plot_target_char = {
										character = ROOT
									}
								}
							}
						}

						NOT = { character = FROM }
					}

					character_event = { id = CM.6005 } # Notify
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				# Don't take plot targets out of hiding
				NOR = {
					has_character_modifier = suspected_murder_plot

					FROM = {
						any_known_plotter = {
							target = ROOT

							plot_power >= 0.5

							OR = {
								has_plot = plot_kill_character
								has_plot = plot_kill_spouse
								has_plot = plot_take_revenge
							}
						}
					}
				}
			}

			mult_modifier = {
				factor = 0.3
				FROM = { trait = paranoid }
			}
		}
	}

	go_into_hiding = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		is_high_prio = yes

		only_rulers = yes

		potential = {
			prisoner = no
		}

		allow = {
			custom_tooltip = {
				text = is_target_of_murder_plot_tooltip

				OR = {
					has_character_modifier = suspected_murder_plot

					any_known_plotter = {
						target = ROOT

						OR = {
							has_plot = plot_kill_character
							has_plot = plot_kill_spouse
							has_plot = plot_take_revenge
						}
					}
				}
			}

			custom_tooltip = {
				text = recently_went_out_of_hiding_tooltip

				NOT = { has_character_modifier = went_out_of_hiding_timer }
			}

			is_inaccessible_trigger = no
			has_job_title = no
			in_command_trigger = no
		}

		effect = {
			custom_tooltip = {
				text = go_into_hiding_info_tooltip

				add_trait = in_hiding
				set_character_flag = in_hiding_murder
				set_character_flag = do_not_disturb

				# Notify close family and plotters
				any_plotter = {
					target = ROOT

					limit = {
						OR = {
							has_plot = plot_kill_character
							has_plot = plot_kill_spouse
							has_plot = plot_take_revenge
						}
					}

					# CleanSlate: AI doesn't need notification events
					if = {
						limit = { ai = yes }
						# CleanSlate: Deactivate the plot directly instead of firing the event that deactivates it
						activate_plot = no
					}
					# CleanSlate: Players do
					else = {
						if = {
							limit = { is_plot_active = yes }
							character_event = { id = CM.6180 }
						}
						else = {
							character_event = { id = CM.6000 }
						}
					}

					any_backed_character = {
						limit = { ai = no }
						character_event = { id = CM.6000 }
					}
				}

				any_player = {
					limit = {
						OR = {
							is_close_relative = ROOT
							is_friend = ROOT
							is_lover = ROOT
							is_rival = ROOT
						}

						NOR = {
							character = ROOT

							trigger_if = {
								limit = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}
								}

								plot_target_char = {
									character = ROOT
								}
							}

							any_backed_character = {
								OR = {
									has_plot = plot_kill_character
									has_plot = plot_kill_spouse
								}

								plot_target_char = {
									character = ROOT
								}
							}
						}
					}

					character_event = { id = CM.6000 }
				}
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				OR = {
					has_character_modifier = suspected_murder_plot

					any_known_plotter = {
						target = ROOT

						plot_power >= 0.5

						OR = {
							has_plot = plot_kill_character
							has_plot = plot_kill_spouse
							has_plot = plot_take_revenge
						}
					}
				}
			}

			mult_modifier = {
				factor = 0.1
				practical_age >= 65
			}

			mult_modifier = {
				factor = 0.1
				trait = brave
			}

			mult_modifier = {
				factor = 2
				trait = depressed
			}

			mult_modifier = {
				factor = 5
				trait = craven
			}

			mult_modifier = {
				factor = 10
				trait = paranoid
			}
		}
	}

	come_out_of_hiding = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		is_high_prio = yes

		only_rulers = yes

		potential = {
			trait = in_hiding
			has_character_flag = in_hiding_murder
			NOT = { has_character_flag = in_religious_seclusion }
		}

		allow = {
			trigger_if = {
				limit = {
					OR = {
						is_adult = no
						is_incapable = yes
					}
				}

				regent = {
					NOR = {
						trait = deceitful
						trait = ambitious
						is_rival = ROOT
					}
				}
			}
		}

		effect = {
			custom_tooltip = {
				text = come_out_of_hiding_info_tooltip

				clr_character_flag = do_not_disturb
				clr_character_flag = in_hiding_murder
				remove_trait = in_hiding

				add_character_modifier = {
					name = went_out_of_hiding_timer
					months = 6
					hidden = yes
				}

				# Notify player close family and plotters
				any_player = {
					limit = {
						trigger_if = {
							limit = {
								OR = {
									is_close_relative = ROOT
									is_friend = ROOT
									is_lover = ROOT
									is_rival = ROOT
								}
							}

							NOR = {
								character = ROOT

								trigger_if = {
									limit = {
										OR = {
											has_plot = plot_kill_character
											has_plot = plot_kill_spouse
										}
									}

									plot_target_char = {
										character = ROOT
									}
								}

								any_backed_character = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}

									plot_target_char = {
										character = ROOT
									}
								}
							}
						}
						trigger_else = {
							trigger_if = {
								limit = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}
								}

								plot_target_char = {
									character = ROOT
								}
							}
							trigger_else = {
								any_backed_character = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}

									plot_target_char = {
										character = ROOT
									}
								}
							}
						}
					}

					character_event = { id = CM.6001 }
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				# Stay in hiding if still a (suspected) target of a plot
				NOR = {
					has_character_modifier = suspected_murder_plot

					any_known_plotter = {
						target = ROOT

						plot_power >= 0.5

						OR = {
							has_plot = plot_kill_character
							has_plot = plot_kill_spouse
							has_plot = plot_take_revenge
						}
					}
				}
			}

			mult_modifier = {
				factor = 0.3
				trait = paranoid
			}
		}
	}

	send_spouse_into_hiding = {
		filter = spouse
		ai_target_filter = spouse
		ai_check_interval = 12

		is_high_prio = yes

		only_rulers = yes

		from_potential = {
			is_married = yes
			has_regent = no
		}

		potential = {
			host = { character = FROM }
			is_ruler = no
			is_inaccessible_trigger = no

			# CleanSlate: Not if you're plotting against your spouse yourself (you monster)
			trigger_if = {
				limit = { is_plot_target_of = FROM }

				FROM = {
					NOT = { has_plot = plot_kill_spouse }
				}
			}
		}

		allow = {
			custom_tooltip = {
				text = is_target_of_murder_plot_tooltip

				OR = {
					has_character_modifier = suspected_murder_plot

					FROM = {
						any_known_plotter = {
							target = ROOT

							OR = {
								has_plot = plot_kill_character
								has_plot = plot_kill_spouse
								has_plot = plot_take_revenge
							}
						}
					}
				}
			}

			custom_tooltip = {
				text = recently_went_out_of_hiding_tooltip

				NOT = { has_character_modifier = went_out_of_hiding_timer }
			}

			has_job_title = no
			in_command_trigger = no
			prisoner = no
		}

		effect = {
			custom_tooltip = {
				text = go_into_hiding_info_tooltip

				add_trait = in_hiding
				set_character_flag = do_not_disturb
				set_character_flag = in_hiding_murder

				# Notify player and close family and plotters
				any_plotter = {
					target = ROOT

					limit = {
						OR = {
							has_plot = plot_kill_character
							has_plot = plot_kill_spouse
							has_plot = plot_take_revenge
						}
					}

					# CleanSlate: AI don't need notification events
					if = {
						limit = { ai = yes }
						# CleanSlate: Deactivate the plot directly instead of firing the event that deactivates it
						activate_plot = no
					}
					# CleanSlate: Players do
					else = {
						if = {
							limit = { is_plot_active = yes }
							character_event = { id = CM.6180 }
						}
						else = {
							character_event = { id = CM.6002 }
						}
					}

					any_backed_character = {
						limit = { ai = no }
						character_event = { id = CM.6002 }
					}
				}

				any_player = {
					limit = {
						OR = {
							is_close_relative = ROOT
							is_friend = ROOT
							is_lover = ROOT
							is_rival = ROOT
						}

						NOR = {
							character = FROM

							trigger_if = {
								limit = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}
								}

								plot_target_char = {
									character = ROOT
								}
							}

							any_backed_character = {
								OR = {
									has_plot = plot_kill_character
									has_plot = plot_kill_spouse
								}

								plot_target_char = {
									character = ROOT
								}
							}
						}
					}

					character_event = { id = CM.6002 }
				}
			}
		}

		ai_will_do = {
			factor = 0.25

			trigger = {
				OR = {
					has_character_modifier = suspected_murder_plot

					FROM = {
						any_known_plotter = {
							target = ROOT

							plot_power >= 0.5

							OR = {
								has_plot = plot_kill_character
								has_plot = plot_kill_spouse
								has_plot = plot_take_revenge
							}
						}
					}
				}
			}

			mult_modifier = {
				factor = 0.1
				practical_age >= 65
			}

			mult_modifier = {
				factor = 0.1

				OR = {
					FROM = {
						trait = pregnant
						father_of_unborn_known = yes
						father_of_unborn = { NOT = { character = ROOT } }
					}

					ROOT = {
						trait = pregnant
						father_of_unborn_known = yes
						father_of_unborn = { NOT = { character = FROM } }
					}
				}
			}

			mult_modifier = {
				factor = 10

				OR = {
					FROM = {
						trait = pregnant
						father_of_unborn_known = yes
						father_of_unborn = { character = ROOT }
					}

					ROOT = {
						trait = pregnant
						father_of_unborn_known = yes
						father_of_unborn = { character = FROM }
					}
				}
			}

			mult_modifier = {
				factor = 0.5

				FROM = {
					opinion = {
						who = ROOT
						value < -25
					}
				}
			}

			mult_modifier = {
				factor = 1.5

				FROM = {
					opinion = {
						who = ROOT
						value >= 25
					}
				}
			}

			mult_modifier = {
				factor = 5
				FROM = { trait = in_hiding }
			}

			mult_modifier = {
				factor = 10
				FROM = { trait = paranoid }
			}
		}
	}

	take_spouse_out_of_hiding = {
		filter = spouse
		ai_target_filter = spouse
		ai_check_interval = 12

		is_high_prio = yes

		only_rulers = yes

		from_potential = {
			is_married = yes
			has_regent = no
		}

		potential = {
			trait = in_hiding
			has_character_flag = in_hiding_murder
			host = { character = FROM }
			is_ruler = no
			prisoner = no
		}

		effect = {
			custom_tooltip = {
				text = come_out_of_hiding_info_tooltip

				remove_trait = in_hiding
				clr_character_flag = in_hiding_murder
				clr_character_flag = do_not_disturb

				add_character_modifier = {
					name = went_out_of_hiding_timer
					months = 6
					hidden = yes
				}

				# Notify player close family and plotters
				any_player = {
					limit = {
						trigger_if = {
							limit = {
								OR = {
									is_close_relative = ROOT
									is_friend = ROOT
									is_lover = ROOT
									is_rival = ROOT
								}
							}

							NOR = {
								character = FROM

								trigger_if = {
									limit = {
										OR = {
											has_plot = plot_kill_character
											has_plot = plot_kill_spouse
										}
									}

									plot_target_char = {
										character = ROOT
									}
								}

								any_backed_character = {
									plot_target_char = {
										character = ROOT
									}
								}
							}
						}
						trigger_else = {
							trigger_if = {
								limit = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}
								}

								plot_target_char = {
									character = ROOT
								}
							}
							trigger_else = {
								any_backed_character = {
									OR = {
										has_plot = plot_kill_character
										has_plot = plot_kill_spouse
									}

									plot_target_char = {
										character = ROOT
									}
								}
							}
						}
					}

					character_event = { id = CM.6003 }
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				# Only if no longer a (suspected) plot target
				NOR = {
					has_character_modifier = suspected_murder_plot

					FROM = {
						any_known_plotter = {
							target = ROOT

							plot_power >= 0.5

							OR = {
								has_plot = plot_kill_character
								has_plot = plot_kill_spouse
								has_plot = plot_take_revenge
							}
						}
					}
				}
			}

			mult_modifier = {
				factor = 0.1
				FROM = { trait = paranoid }
			}
		}
	}
}