###########################################
#
# Society decisions for monastic orders
#
###########################################

society_decisions = {

	monastic_orders_invite_ascetic = {
		ai_check_interval = 50

		only_playable = yes

		potential = {
			has_dlc = "Mystics"
			is_monastic_member_trigger = yes
			society_rank >= 3

			NOR = {
				has_character_flag = ascetic_arriving
				has_character_modifier = recently_invited_ascetic
			}
		}

		allow = {
			has_society_currency_medium_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_character_flag = ascetic_arriving
			detract_society_currency_medium_effect = yes

			custom_tooltip = {
				text = monastic_orders_invite_ascetic_tt

				character_event = {
					id = MNM.4434
					days = 7
				}
			}

			add_character_modifier = {
				name = recently_invited_ascetic
				years = 1
			}
		}

		ai_will_do = {
			factor = 0.05

			mult_modifier = {
				factor = 0.01
				job_spiritual = { learning >= 16 }
			}

			mult_modifier = {
				factor = 0.1
				any_courtier = { is_monastic_member_trigger = yes } # Less likely to invite if there is already someone else at court
			}
		}
	}

	monastic_order_penance = {
		ai_check_interval = 24

		potential = {
			is_christian_monastic_member_trigger = yes
			has_quest = quest_monastic_order_penance
			NOT = { has_character_modifier = doing_penance }
		}

		allow = {
			has_quest = quest_monastic_order_penance

			custom_tooltip = {
				text = monastic_order_penance_allow_tt

				trigger_if = {
					limit = { has_character_flag = holy_seclusion_timer }

					had_character_flag = {
						flag = holy_seclusion_timer
						days >= 1000
					}
				}
			}
		}

		effect = {
			add_character_modifier = {
				name = doing_penance
				duration = -1
			}

			character_event = { id = MNM.4403 }
		}

		ai_will_do = {
			factor = 0.1

			mult_modifier = {
				factor = 0.1
				war = yes
			}

			mult_modifier = {
				factor = 0.1

				OR = {
					trait = gregarious
					trait = socializer
				}
			}

			mult_modifier = {
				factor = 5
				is_virtous = yes
			}
		}
	}

	monastic_order_meditation = {
		ai_check_interval = 24

		potential = {
			has_dlc = "Mystics"

			OR = {
				society_member_of = monastic_order_stoics
				is_dharmic_monastic_member_trigger = yes
			}

			has_quest = quest_monastic_order_meditation
		}

		allow = {
			has_quest = quest_monastic_order_meditation

			custom_tooltip = {
				text = monastic_order_meditation_allow_tt

				trigger_if = {
					limit = { has_character_flag = holy_seclusion_timer }

					had_character_flag = {
						flag = holy_seclusion_timer
						days >= 1000
					}
				}
			}

			custom_tooltip = {
				text = in_meditation_cooldown_tt

				NOT = { has_character_modifier = in_meditation }
			}
		}

		effect = {
			add_character_modifier = {
				name = in_meditation
				duration = -1
			}

			character_event = { id = MNM.4403 }
		}

		ai_will_do = {
			factor = 0.6

			mult_modifier = {
				factor = 0.01
				war = yes
			}

			mult_modifier = {
				factor = 0.1

				OR = {
					trait = gregarious
					trait = socializer
				}
			}

			mult_modifier = {
				factor = 5
				is_virtous = yes
			}
		}
	}

	monastic_order_rejoin_world = {
		ai = no

		potential = {
			OR = {
				has_character_modifier = doing_penance
				has_character_modifier = in_meditation
			}
		}

		allow = {
			piety >= 100

			OR = {
				has_character_modifier = doing_penance
				has_character_modifier = in_meditation
			}
		}

		effect = {
			piety = -100

			remove_character_modifier = doing_penance
			remove_character_modifier = in_meditation

			clr_character_flag = success_seclusion_desc_A # Flags from 4426
			clr_character_flag = success_seclusion_desc_B
			clr_character_flag = success_seclusion_desc_C
		}

		ai_will_do = {
			factor = 0
		}
	}

	monastic_orders_go_celibate = {
		ai = no

		potential = {
			OR = {
				is_dharmic_monastic_member_trigger = yes
				is_christian_monastic_member_trigger = yes
				society_member_of = monastic_order_stoics
			}

			society_rank >= 2

			NOR = {
				has_character_modifier = promise_of_celibacy
				trait = celibate
			}
		}

		allow = {
			NOR = {
				trait = celibate
				trait = hedonist
				trait = master_seducer
				trait = master_seductress
			}
		}

		effect = {
			tooltip = {
				add_trait = celibate
			}

			character_event = { id = MNM.4090 }
		}

		ai_will_do = {
			factor = 0
		}
	}

	monastic_orders_stop_celibate = {
		ai = no

		potential = {
			trait = celibate

			OR = {
				is_christian_monastic_member_trigger = yes
				is_dharmic_monastic_member_trigger = yes
				society_member_of = monastic_order_stoics
			}

			society_rank >= 2

			# No weaseling
			NOT = { has_character_flag = flag_papal_celibacy }
		}

		allow = {
			trait = celibate
		}

		effect = {
			tooltip = {
				remove_trait = celibate
			}

			character_event = { id = MNM.4091 }
		}

		ai_will_do = {
			factor = 0
		}
	}

	monastic_orders_donate_to_charity = {
		ai_check_interval = 36

		only_playable = yes

		potential = {
			is_monastic_member_trigger = yes

			OR = {
				has_quest = quest_monastic_order_donate_to_charity
				NOT = { has_character_modifier = recently_donated_to_charity }
			}

			NOR = {
				society_member_of = monastic_order_stoics
				has_character_flag = donate_to_charity_cooldown
			}
		}

		allow = {
			wealth >= 100
		}

		effect = {
			set_character_flag = donate_to_charity_cooldown

			tooltip = {
				wealth = -100

				if = {
					limit = { has_quest = quest_monastic_order_donate_to_charity }

					if = {
						limit = {
							any_owned_bloodline = {
								has_bloodline_flag = monastic_saintly_bloodline
								bloodline_is_active_for = PREV
							}
						}

						add_society_currency_major_effect = yes
					}
					else = {
						add_society_currency_medium_effect = yes
					}
				}
				else = {
					if = {
						limit = {
							any_owned_bloodline = {
								has_bloodline_flag = monastic_saintly_bloodline
								bloodline_is_active_for = PREV
							}
						}

						add_society_currency_medium_effect = yes
					}
					else = {
						add_society_currency_minor_effect = yes
					}
				}

				add_character_modifier = {
					name = recently_donated_to_charity
					years = 1
				}

				if = {
					limit = { trait = greedy }

					random = {
						chance = 40
						remove_trait = greedy
					}
				}
				else_if = {
					limit = {
						NOT = { trait = charitable }
					}

					random = {
						chance = 30
						add_trait = charitable
					}
				}
			}

			hidden_effect = {
				if = {
					limit = { is_society_grandmaster = no }

					society = {
						leader = { save_event_target_as = monastic_leader }
					}

					letter_event = { id = MNM.4014 }
				}
				else_if = {
					limit = {
						society = {
							any_society_member = { is_society_grandmaster = no }
						}
					}

					society = {
						random_society_member = {
							limit = { is_society_grandmaster = no }
							save_event_target_as = monastic_leader
						}
					}

					letter_event = { id = MNM.4014 }
				}

				else = {
					wealth = -100
					clr_character_flag = donate_to_charity_cooldown

					if = {
						limit = { has_quest = quest_monastic_order_donate_to_charity }

						hidden_effect = {
							clr_quest = quest_monastic_order_donate_to_charity
							sound_effect = monastic_orders_donate_charity
							monastic_order_purity_effect = yes
						}

						if = {
							limit = {
								any_owned_bloodline = {
									has_bloodline_flag = monastic_saintly_bloodline
									bloodline_is_active_for = PREV
								}
							}

							add_society_currency_major_effect = yes
						}
						else = {
							add_society_currency_medium_effect = yes
						}
					}
					else = {
						if = {
							limit = {
								any_owned_bloodline = {
									has_bloodline_flag = monastic_saintly_bloodline
									bloodline_is_active_for = PREV
								}
							}

							add_society_currency_medium_effect = yes
						}
						else = {
							add_society_currency_minor_effect = yes
						}
					}

					add_character_modifier = {
						name = recently_donated_to_charity
						years = 1
					}

					if = {
						limit = { trait = greedy }

						random = {
							chance = 40
							remove_trait_greedy_effect = yes
						}
					}
					else = {
						random = {
							chance = 30
							add_trait_charitable_effect = yes
						}
					}
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

}

targeted_decisions = {

	### Monastic Order Rank 2 power : targeted decision to remove one of target's vices
	monastic_orders_spiritual_guidance = {
		ai_target_filter = realm
		ai_check_interval = 12

		is_in_society = yes

		from_potential = {
			has_dlc = "Mystics"
			society_rank >= 2
			is_monastic_member_trigger = yes
			is_incapable = no
		}

		potential = {
			religion = FROM
			is_incapable = no

			# AI will only target landed relatives in the same realm
			trigger_if = {
				limit = {
					FROM = { ai = yes }
				}

				is_close_relative = FROM

				OR = {
					is_landed = yes
					is_patrician = yes
				}

			}
			trigger_else_if = {
				limit = {
					NOT = { same_realm = FROM }
				}

				is_within_diplo_range = FROM
				same_society_as = FROM
			}
		}

		allow = {
			FROM = {
				show_only_failed_conditions = yes

				has_society_currency_medium_trigger = yes # Requires 300 Society Currency
				is_inaccessible_trigger = no
				prisoner = no
				in_command = no
			}

			OR = {
				is_landed = yes
				is_patrician = yes
				is_close_relative = FROM
			}

			is_inaccessible_trigger = no
			prisoner = no
			in_command = no

			NOT = {
				has_opinion_modifier = { # 10 year cooldown
					name = opinion_spiritual_guidance
					who = FROM
				}
			}

			custom_tooltip = {
				text = spiritual_guidance_tooltip

				OR = {
					AND = {
						trait = lustful
						FROM = { NOT = { trait = lustful } }
					}

					AND = {
						trait = gluttonous
						FROM = { NOT = { trait = gluttonous } }
					}

					AND = {
						trait = greedy
						FROM = { NOT = { trait = greedy } }
					}

					AND = {
						trait = slothful
						FROM = { NOT = { trait = slothful } }
					}

					AND = {
						trait = wroth
						FROM = { NOT = { trait = wroth } }
					}

					AND = {
						trait = envious
						FROM = { NOT = { trait = envious } }
					}

					AND = {
						trait = proud
						FROM = { NOT = { trait = proud } }
					}
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no
				detract_society_currency_medium_effect = yes # Lose 300 Society Currency
			}

			# Will randomly remove one sin that target has and you don't
			random_list = {
				10 = {
					trigger = {
						trait = lustful

						FROM = {
							NOT = { trait = lustful }
						}
					}

					set_character_flag = spiritual_guidance_lustful
					remove_trait = lustful
				}

				10 = {
					trigger = {
						trait = gluttonous

						FROM = {
							NOT = { trait = gluttonous }
						}
					}

					set_character_flag = spiritual_guidance_gluttonous
					remove_trait = gluttonous
				}

				10 = {
					trigger = {
						trait = greedy

						FROM = {
							NOT = { trait = greedy }
						}
					}

					set_character_flag = spiritual_guidance_greedy
					remove_trait = greedy
				}

				10 = {
					trigger = {
						trait = slothful

						FROM = {
							NOT = { trait = slothful }
						}
					}

					set_character_flag = spiritual_guidance_slothful
					remove_trait = slothful
				}

				10 = {
					trigger = {
						trait = wroth

						FROM = {
							NOT = { trait = wroth }
						}
					}

					set_character_flag = spiritual_guidance_wroth
					remove_trait = wroth
				}

				10 = {
					trigger = {
						trait = envious

						FROM = {
							NOT = { trait = envious }
						}
					}

					set_character_flag = spiritual_guidance_envious
					remove_trait = envious
				}

				10 = {
					trigger = {
						trait = proud

						FROM = {
							NOT = { trait = proud }
						}
					}

					set_character_flag = spiritual_guidance_proud
					remove_trait = proud
				}
			}

			# Opinion mostly serves as a 10 years cooldown ; +5 Opinion
			opinion = {
				name = opinion_spiritual_guidance
				who = FROM
				years = 10
			}

			FROM = { character_event = { id = MNM.4950 } }
			character_event = { id = MNM.4951 }
		}

		ai_will_do = {
			factor = 0.1
		}
	}

	### Monastic Order Rank 3 power : targeted decision to give target a virtue
	monastic_orders_teach_virtue = {
		ai_target_filter = realm
		ai_check_interval = 12

		is_in_society = yes

		from_potential = {
			has_dlc = "Mystics"
			is_monastic_member_trigger = yes
			society_rank >= 3
			is_incapable = no
			has_virtue_trigger = yes
		}

		potential = {
			religion = FROM
			is_incapable = no

			# AI will only target landed relatives in the same realm
			trigger_if = {
				limit = {
					FROM = { ai = yes }
				}

				is_close_relative = FROM

				OR = {
					is_landed = yes
					is_patrician = yes
				}

			}
			trigger_else_if = {
				limit = {
					NOT = { same_realm = FROM }
				}

				is_within_diplo_range = FROM
				same_society_as = FROM
			}
		}

		allow = {
			FROM = {
				show_only_failed_conditions = yes

				has_society_currency_major_trigger = yes	# Requires 500 Society Currency
				is_inaccessible_trigger = no
				prisoner = no
				in_command = no
			}

			OR = {
				is_landed = yes
				is_patrician = yes
				is_close_relative = FROM
			}

			is_inaccessible_trigger = no
			prisoner = no
			in_command = no

			NOT = {
				has_opinion_modifier = { # 10 year cooldown
					name = opinion_teach_virtue
					who = FROM
				}
			}

			custom_tooltip = {
				text = teach_virtue_tooltip

				OR = {
					AND = {
						NOT = { trait = chaste }
						FROM = { trait = chaste }
					}

					AND = {
						NOT = { trait = temperate }
						FROM = { trait = temperate }
					}

					AND = {
						NOT = { trait = charitable }
						FROM = { trait = charitable }
					}

					AND = {
						NOT = { trait = diligent }
						FROM = { trait = diligent }
					}

					AND = {
						NOT = { trait = patient }
						FROM = { trait = patient }
					}

					AND = {
						NOT = { trait = kind }
						FROM = { trait = kind }
					}

					AND = {
						NOT = { trait = humble }
						FROM = { trait = humble }
					}
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no
				detract_society_currency_major_effect = yes # Lose 500 Society Currency
			}

			# Will randomly add one virtue that you have and target doesn't
			random_list = {
				10 = {
					trigger = {
						NOT = { trait = chaste }
						FROM = { trait = chaste }
					}

					set_character_flag = teach_virtue_chaste
					add_trait = chaste
				}

				10 = {
					trigger = {
						NOT = { trait = temperate }
						FROM = { trait = temperate }
					}

					set_character_flag = teach_virtue_temperate
					add_trait = temperate
				}

				10 = {
					trigger = {
						NOT = { trait = charitable }
						FROM = { trait = charitable }
					}

					set_character_flag = teach_virtue_charitable
					add_trait = charitable
				}

				10 = {
					trigger = {
						NOT = { trait = diligent }
						FROM = { trait = diligent }
					}

					set_character_flag = teach_virtue_diligent
					add_trait = diligent
				}

				10 = {
					trigger = {
						NOT = { trait = patient }
						FROM = { trait = patient }
					}

					set_character_flag = teach_virtue_patient
					add_trait = patient
				}

				10 = {
					trigger = {
						NOT = { trait = kind }
						FROM = { trait = kind }
					}

					set_character_flag = teach_virtue_kind
					add_trait = kind
				}

				10 = {
					trigger = {
						NOT = { trait = humble }
						FROM = { trait = humble }
					}

					set_character_flag = teach_virtue_humble
					add_trait = humble
				}
			}

			# Opinion mostly serves as a 10 years cooldown ; +10 Opinion
			opinion = {
				name = opinion_teach_virtue
				who = FROM
				years = 10
			}

			FROM = { character_event = { id = MNM.4960 } }
			character_event = { id = MNM.4961 }
		}

		ai_will_do = {
			factor = 0.1
		}
	}
}

title_decisions = {
	# Monastic Order Rank 4 Power - Convert County

	monastic_order_convert_county = {
		ai_target_filter = sub_realm_owned
		ai_check_interval = 3

		is_in_society = yes
		only_playable = yes

		from_potential = {
			has_dlc = "Mystics"
			is_monastic_member_trigger = yes
			society_rank == 4
			NOT = { society_member_of = monastic_order_stoics }
		}

		potential = {
			real_tier = COUNT

			location = {
				NOT = { religion = FROM }
			}

			holder_scope = {
				OR = {
					character = FROM
					is_vassal_or_below_of = FROM
				}
			}
		}

		allow = {
			FROM = {
				society_currency >= 1000
			}

			location = {
				NOT = { religion = FROM }

				# Cannot convert counties of owners who have that religion
				trigger_if = {
					limit = {
						NOT = { owned_by = FROM }
					}

					owner = {
						NOT = { religion = PREV }
					}
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				change_society_currency = -1000
				character_event = { id = MNM.4990 }
			}

			location = {
				show_scope_change = no

				religion = FROM

				add_province_modifier = {
					name = religious_tension
					years = 2
				}
			}

			if = { # Notify county owner
				limit = {
					NOT = { holder = FROM }
				}

				holder_scope = {
					character_event = { id = MNM.4991 }
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

}