﻿### Found Military Order ###
create_holy_order_decision = {
	picture = {
		trigger = {
			culture = { has_graphical_east_asia_culture_group_trigger = yes }
		}
		reference = "gfx/interface/illustrations/holding_types/tgp_chinese_pagoda.dds"
	}
	picture = {
		trigger = {
			culture = { has_graphical_india_culture_group_trigger = yes }
		}
		reference = "gfx/interface/illustrations/holding_types/tgp_castle_se_asia.dds"
	}
	picture = {
		trigger = {
			OR = { religion = religion:eastern_orthodox_religion religion = religion:oriental_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion }
		}
		reference = "gfx/interface/illustrations/event_story/pam_the_christian_church_crusade.dds"
	}
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_major_religion.dds"
	}
	decision_group_type = major

	ai_goal = yes

	is_shown = {
		is_landed = yes
		OR = {
			highest_held_title_tier >= tier_kingdom
			any_held_county = {
				any_in_de_jure_hierarchy = {
					barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
				}
			}
			AND = {
				highest_held_title_tier >= tier_duchy
				any_character_struggle = {
					involvement = involved
					has_struggle_phase_parameter = holy_order_can_be_created_by_dukes
				}
			}
		}
		any_patroned_holy_order = {
			count = 0
			leader ?= {
				government_has_flag = government_is_military_holy_order
			}
		}

		character_can_create_holy_order_basic_trigger = yes

		# Jomsvikings should use their special decision if appropriate.
		trigger_if = {
			limit = { has_fp1_dlc_trigger = yes }
			faith = {
				fp1_valid_norse_faith_for_jomsvikings_trigger = no
			}
		}
	}

	widget = {
		controller = create_holy_order
		title_valid = {
			trigger_if = {
				limit = { exists = this }
				barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = scope:ruler }
			}
			trigger_else = {
				custom_description = {
					text = "has_holy_order_barony_selected"
					always = no
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		is_at_war = no
	}

	is_valid = {
		trigger_if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					has_struggle_phase_parameter = holy_order_can_be_created_by_dukes
				}
			}
			highest_held_title_tier >= tier_duchy
		}
		trigger_else = {
			custom_description = {
				text = "is_king_or_emperor"
				highest_held_title_tier >= tier_kingdom
			}
		}

		custom_description = {
			text = "has_two_castle_county"
			any_held_county = {
				any_in_de_jure_hierarchy = {
					barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
				}
			}
		}
		trigger_if = {
			limit = {
				exists = faith.religious_head
				root != root.faith.religious_head
			}
			faith.religious_head = {
				opinion = {
					target = root
					value >= {
						value = 0
						if = {
							limit = { is_ai = no }
					 		add = high_positive_opinion
						}
					}
				}
			}
		}
		trigger_if = {
			limit = {
				has_pam_dlc_trigger = yes
				NOT = { rite = { rite_has_parameter = lay_clergy } }
			}
			OR = {
				custom_tooltip = {
					text = government_is_theocracy
					government_has_flag = government_is_theocracy
				}
				custom_tooltip = {
					text = has_clerical_puppet_tt
					OR = {
						exists = root.puppet:theological_agent_puppet
						exists = root.puppet:external_theological_agent_puppet
					}
				}
			}
		}
		
		trigger_if = {
			limit = {
				is_ai = yes
			}
			piety_level >= 1
		}
		trigger_else = {
			piety_level >= 3
		}
	}
	cost = {
		gold = {
			value = 0
			if = {
				limit = {
					has_treasury = no
				}
				add = holy_order_gold_cost
			}
		}
		treasury = {
			value = 0
			if = {
				limit = {
					has_treasury = yes
				}
				add = holy_order_gold_cost
			}
		}
		piety = {
			value = holy_order_piety_cost
		}
	}

	effect = {
		custom_tooltip = create_holy_order_decision_effect_message

		if = { # If you have no valid barony this will throw errors when you open the decision - we add this check to avoid that 
			limit = {
				exists = scope:barony
			}
			hidden_effect = {
				if = {
					limit = { scope:barony.holder != root }
					create_title_and_vassal_change = {
						type = leased_out
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:barony = {
						change_title_holder_include_vassals = {
							holder = root
							change = scope:change
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}

			create_character = {
				template = holy_order_leader_character
				location = scope:barony.title_province
				save_scope_as = leader
			}
			hidden_effect = {
				scope:leader = { set_character_rite ?= root.rite }
			}

			create_holy_order_neutral_effect = {
				LEADER = scope:leader
				CAPITAL = scope:barony
				NEW_HO_SCOPE = new_holy_order
				FOUNDER = root
				ORDER_TYPE = military
			}
			# PAM - Catalysts
			if = {
				limit = {
					has_pam_dlc_trigger = yes
					OR = { religion = religion:eastern_orthodox_religion religion = religion:oriental_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion }
					situation:the_christian_church ?= {
						situation_top_has_catalyst = catalyst_the_christian_church_founded_military_order
					}
				}
				situation:the_christian_church = {
					trigger_situation_catalyst = catalyst_the_christian_church_founded_military_order
				}
			}
		}
		create_holy_order_accompanying_effect = yes
	}
	
	ai_potential = {
		highest_held_title_tier >= tier_kingdom
		is_playable_character = yes
		OR = {
			any_held_title = {
				title_tier = barony
				barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
			}
			any_vassal = {
				primary_title.tier = tier_barony
				any_held_title = {
					barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
				}
			}
		}
	}

	ai_will_do = {
		base = 200
		modifier = {
			add = {
				faith = {
					# Don't create more than 5 Military Orders for a faith
					every_faith_holy_order = {
						subtract = 40
					}
				}
			}
		}
		modifier = {
			government_has_flag = government_is_mandala
			add = -150
		}
	}
}

### Borrow Gold from Military Order ###
borrow_from_holy_order_decision = { #by Linnéa Thimrén
	picture = {
		trigger = { 
			culture = { has_graphical_india_culture_group_trigger = yes }
		}
		reference = "gfx/interface/illustrations/decisions/hindu_religious.dds"
	}
	decision_group_type = religious
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_spend_money.dds"
	}
	desc = borrow_from_holy_order_decision_desc
	selection_tooltip = borrow_from_holy_order_decision_tooltip

	ai_check_interval_by_tier = {
		barony = 0
		county = 72
		duchy = 72
		kingdom = 72
		empire = 72
		hegemony = 72
	}

	cost = {
		piety = 50
	}

	cooldown = { days = 5475 }

	is_shown = {
		highest_held_title_tier > tier_barony
		faith ?= {
			any_faith_holy_order = { #Your faith must have a Military Order
				leader = {
					government_has_flag = government_is_military_holy_order
				}
			}
		}
		is_playable_character = yes
		# cannot already have a loan - keep it simple
		NOR = {
			exists = var:loan_amount_owed
			exists = var:loan_holder
		}
	}

	is_valid_showing_failures_only = {
		is_available = yes
		custom_description = {
			text = borrow_from_holy_order_gold_condition
			faith = {
				any_faith_holy_order = {
					leader = {
						government_has_flag = government_is_military_holy_order
						gold >= root.holy_order_gold_value
						NOT = {
							has_any_bad_relationship_with_character_trigger = { CHARACTER = root }
						}
					}
				}
			}
		}
	}

	effect = {
		hidden_effect = {
			faith = {
				random_faith_holy_order = {
					limit = {
						leader = {
							gold >= root.holy_order_gold_value
							government_has_flag = government_is_military_holy_order
							NOT = {
								has_any_bad_relationship_with_character_trigger = { CHARACTER = root }
							}
						}
					}
					leader = { save_scope_as = recipient }
					save_scope_as = order
				}
			}
			trigger_event = holy_order.0200
		}
		custom_tooltip = borrow_from_holy_order_decision_effect_tooltip
	}
	
	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 50
		modifier = {
			factor = 0
			gold >= holy_order_gold_value
		}
		ai_value_modifier = {
			ai_zeal = -0.5
		}
		ai_value_modifier = {
			ai_greed = 0.5
		}
	}
}

### Revoke Military Order Lease ###
cancel_holy_order_lease_decision = {
	picture = {
		trigger = { 
			culture = { has_graphical_india_culture_group_trigger = yes }
		}
		reference = "gfx/interface/illustrations/decisions/hindu_soldiers.dds"
	}
	decision_group_type = religious
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_major_religion.dds"
	}
	desc = {
		first_valid = {
			triggered_desc = {
			    trigger = {
					faith = {
						faith_hostility_level = {
							target = scope:barony.lessee_title.holder.faith
							value >= faith_hostile_level
						}
					}
			    }
			    desc = cancel_holy_order_lease_hostile_decision_desc
			}
			desc = cancel_holy_order_lease_decision_desc
		}
	}

	ai_check_interval_by_tier = {
		barony = 0
		county = 36
		duchy = 36
		kingdom = 36
		empire = 36
		hegemony = 36
	}

	is_shown = {
		is_playable_character = yes
		any_sub_realm_barony = {
			barony_is_valid_for_holy_order_lease_cancellation_trigger = { ACTOR = root }
		}

		is_landless_adventurer = no
	}

	widget = {
		controller = revoke_holy_order_lease
		title_valid = {
			trigger_if = {
				limit = { exists = this }
				barony_is_valid_for_holy_order_lease_cancellation_trigger =  { ACTOR = scope:ruler }
			}
			trigger_else = {
				custom_description = {
					text = "has_holy_order_barony_selected"
					always = no
				}
			}
		}
	}

	cost = {
		piety = {
			if = {
				limit = {
					exists = scope:barony.lessee.faith
				}
				if = {
					limit = { faith = scope:barony.lessee.faith }
					add = cancel_holy_order_piety_cost
				}
				else_if = {
					limit = {
						faith = {
							faith_hostility_level = {
								target = scope:barony.lessee.faith
								value <= faith_astray_level
							}
						}
					}
					add = cancel_astray_holy_order_piety_cost
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available = yes
		age >= 12
		is_at_war = no
	}

	effect = {
		if = {
			limit = { exists = scope:barony.lessee }
			save_scope_as = cost_paid
			revoke_holy_order_lease_effect = {
				CHARACTER = root
				TARGET = scope:barony.lessee
				BARONY = scope:barony
			}
			# PAM - Catalysts
			if = {
				limit = {
					has_pam_dlc_trigger = yes
					OR = { religion = religion:eastern_orthodox_religion religion = religion:oriental_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion }
					situation:the_christian_church ?= {
						situation_top_has_catalyst = catalyst_the_christian_church_revoked_order_lease
					}
				}
				situation:the_christian_church = {
					trigger_situation_catalyst = catalyst_the_christian_church_revoked_order_lease
				}
			}
		}
		hidden_effect = {

			#Message is sent through the on_holy_order_destroyed on_action instead!
			#send_interface_toast = {
			#	type = holy_order_founded_message
			#	desc = i_created_holy_order_message
			#	left_icon = scope:leader
			#	right_icon = scope:new_holy_order.title
			#}
			#save_scope_as = holy_order_creator
			#every_ruler = {
			#	limit = {
			#		faith = scope:holy_order_creator.faith
			#		this != scope:holy_order_creator
			#	}
			#	send_interface_message = {
			#		type = holy_order_founded_message
			#		desc = someone_created_holy_order_message
			#		left_icon = scope:holy_order_creator
			#		right_icon = scope:new_holy_order.title
			#	}
			#}
			#every_neighboring_top_liege_realm_owner = {
			#	limit = {
			#		faith != scope:holy_order_creator.faith
			#	}
			#	send_interface_message = {
			#		type = enemy_holy_order_founded_message
			#		desc = other_faith_neighbor_created_holy_order_message
			#		left_icon = scope:holy_order_creator
			#		right_icon = scope:new_holy_order.title
			#	}
			#}
		}
	}
	
	ai_potential = {
		is_playable_character = yes
		is_available = yes
		age >= 12
		is_at_war = no
		highest_held_title_tier >= tier_county
		any_sub_realm_barony = {
			county.holder = { is_ai = yes }
			barony_is_valid_for_holy_order_lease_cancellation_trigger = { ACTOR = root }
		}
		years_from_game_start >= 1
	}

	ai_will_do = {
		base = 0
		# revoke titles leased to Military Orders of the wrong faith
		modifier = {
			add = 75
			exists = scope:barony.lessee
			scope:barony.lessee.faith != root.faith
		}
		modifier = {
			add = 25
			exists = scope:barony.lessee
			scope:barony.lessee.faith != root.faith
			root.faith = {
				faith_hostility_level = {
					target = scope:barony.lessee.faith
					value >= faith_hostile_level
				}
			}
		}
		# Revoke if cynical
		modifier = {
			add = 100
			ai_zeal <= -50
			exists = scope:barony.lessee
			NOT = {
				any_patroned_holy_order = {
					leader = {
						this = scope:barony.lessee
					}
				}
			}
		}
	}
}

# Found a Monastic Order
# by Jason Cantalini 
create_holy_order_monastic_decision = {
	picture = {
		trigger = {
			culture = { has_graphical_east_asia_culture_group_trigger = yes }
		}
		reference = "gfx/interface/illustrations/holding_types/tgp_chinese_pagoda.dds"
	}
	picture = {
		trigger = {
			culture = { has_graphical_india_culture_group_trigger = yes }
		}
		reference = "gfx/interface/illustrations/holding_types/tgp_castle_se_asia.dds"
	}
	picture = {
		reference = "gfx/interface/illustrations/decisions/pam_pre-romanesque_church_exterior.dds"
	}
	decision_group_type = major

	ai_goal = yes

	cooldown = { years = 10 }

	is_shown = {
		is_landed = yes
		highest_held_title_tier >= tier_duchy
		faith ?= {
			faith_has_monasticism_trigger = yes
			NOT = {
				any_faith_holy_order = {
					#Can be military patron and found monastic order
					leader ?= {
						government_has_flag = government_is_monastic_holy_order
					}
					holy_order_patron ?= root
				}
			}
			
		}
	}
	
	cost = {
		gold = {
			value = 0
			if = {
				limit = {
					has_treasury = no
				}
				add = monastic_order_gold_cost
			}
		}
		treasury = {
			value = 0
			if = {
				limit = {
					has_treasury = yes
				}
				add = monastic_order_gold_cost
			}
		}
		piety = {
			value = monastic_order_piety_cost
		}
	}

	widget = {
		controller = select_county_title_in_realm

		title_valid = {
			trigger_if = {
				limit = { exists = this }
				county_is_valid_for_monastic_lease_trigger = { CHARACTER = scope:ruler }
			}
			trigger_else = {
				custom_description = {
					text = "has_holy_order_county_selected"
					always = no
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		is_at_war = no
	}

	is_valid = {

		custom_tooltip = {
			text = has_temple_for_monastic_tt
			save_temporary_scope_as = root_temp
			any_held_county = {
				county_is_valid_for_monastic_lease_trigger = { CHARACTER = root }
			}
		}
		trigger_if = {
			limit = {
				has_pam_dlc_trigger = yes
			}
			OR = {
				custom_tooltip = {
					text = government_is_theocracy
					government_has_flag = government_is_theocracy
				}
				custom_tooltip = {
					text = has_clerical_puppet_tt
					OR = {
						exists = root.puppet:theological_agent_puppet
						exists = root.puppet:external_theological_agent_puppet
					}
				}
			}
		}
		
		trigger_if = {
			limit = {
				is_ai = yes
			}
			piety_level >= 1
		}
		trigger_else = {
			piety_level >= 3
		}
	}

	effect = {
		create_monastic_decision_effect = yes
	}
	
	ai_potential = {
		highest_held_title_tier >= tier_duchy
		is_playable_character = yes
		any_held_county = {
			county_is_valid_for_monastic_lease_trigger = { CHARACTER = root }
		}
	}

	ai_will_do = {
		base = 200
		modifier = {
			add = {
				faith = {
					# Don't create more than 20 monastic orders for a faith
					every_faith_holy_order = {
						subtract = 10
					}
				}
			}
		}
		modifier = {
			government_has_flag = government_is_mandala
			add = -150
		}
		modifier = {
			gold <= monastic_order_gold_cost
			piety <= monastic_order_piety_cost
			factor = 0
		}
	}
}

#Split Leases in your Realm Off into New Order
# by Lonesome Hermit Jason Cantalini
pam_monastic_order_new_movement_decision = {
	picture = {
		trigger = {
			culture = {
				OR = {
					has_graphical_east_asia_culture_group_trigger = yes
					has_graphical_india_culture_group_trigger = yes
				}
			}
		}
		reference = "gfx/interface/illustrations/holding_types/tgp_decision_mandala.dds"
	}
	picture = {
		reference = "gfx/interface/illustrations/decisions/pam_decision_reform_monastic_endowment.dds"
	}
	decision_group_type = major

	ai_goal = yes

	cooldown = { years = 10 }

	is_shown = {
		is_landed = yes
		highest_held_title_tier >= tier_duchy
		#Show when you have any order in your sub-realm you might want to replace
		any_sub_realm_barony = {
			barony_is_valid_to_be_transferred_by_monastic_movement_trigger = { CHARACTER = root }
		}
		faith = {
			faith_has_monasticism_trigger = yes
		}
	}

	widget = {
		controller = select_barony_title_in_realm

		title_valid = {
			trigger_if = {
				limit = { exists = this }
				county.holder ?= scope:ruler
				is_under_holy_order_lease = yes
				lessee ?= {
					government_has_flag = government_is_monastic_holy_order
					holy_order ?= {
						NOT = { holy_order_patron ?= scope:ruler }
					}
				}
			}
			trigger_else = {
				custom_description = {
					text = "pam_monastic_order_new_movement_decision_select_barony_tt"
					always = no
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		trigger_if = {
			limit = {
				monastic_movement_non_order_size_reqs_trigger = no
			}
			custom_tooltip = {
				text = monastic_movement_order_too_small_tt
				any_held_county = {
					any_county_province = {
						barony = { 
							barony_is_valid_to_be_transferred_by_monastic_movement_trigger = { CHARACTER = root }
							lessee.holy_order = {
								num_leased_titles >= 10
							}
						}
					}
				}
			}
		}
	}
	is_valid = {
		custom_tooltip = {
			text = has_monastic_movement_lease_tt
			any_held_county = {
				any_county_province = {
					barony = { 
						barony_is_valid_to_be_transferred_by_monastic_movement_trigger = { CHARACTER = root }
					}
				}
			}
		}
		trigger_if = {
			limit = {
				has_pam_dlc_trigger = yes
				NOT = {
					government_has_flag = government_is_theocracy
				}
			}
			custom_tooltip = {
				text = has_theological_agent_puppet_tt
				exists = root.puppet:theological_agent_puppet
			}
		}
		trigger_else = {
			OR = {
				AND = {
					has_trait = lifestyle_scholar
					has_trait_xp = {
						trait = lifestyle_scholar
						value >= 60
					}
				}
				has_trait = theologian
				has_trait = erudite
				has_trait = lifestyle_mystic
				has_trait = devoted
				has_trait = family_first
				has_trait = diplomat
				has_trait = schemer
			}
		}
		trigger_if = {
			limit = {
				has_pam_dlc_trigger = yes
			}
			custom_tooltip = {
				text = has_external_theological_agent_puppet_tt
				exists = root.puppet:external_theological_agent_puppet
			}
		}
		
		OR = {
			custom_tooltip = {
				text = domain_order_is_large_tt
				any_held_county = {
					any_county_province = {
						barony = { 
							barony_is_valid_to_be_transferred_by_monastic_movement_trigger = { CHARACTER = root }
							lessee.holy_order = {
								num_leased_titles >= 10
							}
						}
					}
				}
			}
			monastic_movement_non_order_size_reqs_trigger = yes
			
		}
		
		trigger_if = {
			limit = {
				is_ai = yes
			}
			piety_level >= 1
		}
		trigger_else = {
			piety_level >= 3
		}
	}

	effect = {
		if = {
			limit = {
				exists = scope:title
				exists = scope:title.lessee.holy_order
			}
			scope:title = { save_scope_as = barony }
			scope:title.lessee = { save_scope_as = target_lessee }
			scope:title.lessee.holy_order = { save_scope_as = target_order }
			scope:title.lessee.holy_order.order_tenet = { save_scope_as = target_order_tenet }
			scope:target_order = {
	
				every_leased_title = {
					limit = {
						county.holder ?= {
							this = root
						}
					}
					add_to_list = revoked_leases
				}
				leader ?= {
					add_opinion = {
						modifier = revoked_order_lease_opinion
						target = root
						opinion = {
							value = 0
							every_in_list = {
								list = revoked_leases
								add = -50
							}
						}
					}
				}
				holy_order_patron ?= {
					add_opinion = {
						modifier = revoked_order_lease_opinion
						target = root
						opinion = {
							value = 0
							every_in_list = {
								list = revoked_leases
								add = -25
							}
						}
					}
				}
				every_in_list = {
					list = revoked_leases
					revoke_lease = yes
				}
			}
			custom_tooltip = monastic_order_creation_breakdown_tooltip
			custom_tooltip = movement_order_gains_target_monasteries_tt
			custom_tooltip = movement_order_has_monaster_gaining_bonus_tt
			custom_tooltip = movement_order_tenet_popularity_tt
			faith = {
				if = {
					limit = { has_tenet_popularity_trigger = yes }
					if = {
						limit = {
							list_size = {
								name = revoked_leases
								value >= 5
							}
						}
						change_tenet_popularity = {
							target = scope:target_order_tenet
							change = {
								value = medium_tenet_popularity_loss
								multiply = 4
							}
						}
					}
					else_if = {
						limit = {
							list_size = {
								name = revoked_leases
								value >= 4
							}
						}
						change_tenet_popularity = {
							target = scope:target_order_tenet
							change = {
								value = medium_tenet_popularity_loss
								multiply = 3
							}
						}
					}
					else_if = {
						limit = {
							list_size = {
								name = revoked_leases
								value >= 3
							}
						}
						change_tenet_popularity = {
							target = scope:target_order_tenet
							change = {
								value = medium_tenet_popularity_loss
								multiply = 2
							}
						}
					}
					else_if = {
						limit = {
							list_size = {
								name = revoked_leases
								value >= 2
							}
						}
						change_tenet_popularity = {
							target = scope:target_order_tenet
							change = {
								value = medium_tenet_popularity_loss
								multiply = 1.5
							}
						}
					}
					else = {
						change_tenet_popularity = {
							target = scope:target_order_tenet
							change = {
								value = medium_tenet_popularity_loss
								multiply = 1
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				NOT = {
					has_trait = lifestyle_scholar
				}
				OR = {
					has_trait = education_learning
					has_lifestyle = learning_lifestyle
					government_has_flag = government_is_theocracy
				}
			}
			add_trait = lifestyle_scholar
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_learning
					has_lifestyle = learning_lifestyle
					government_has_flag = government_is_theocracy
				}
			}
			add_trait_xp = {
				trait = lifestyle_scholar
				value = 10
			}
		}
		show_as_tooltip = {
			create_monastic_order_founder_effect = yes
		}
		
		trigger_event = holy_order.2050
	}

	cost = {
		prestige = {
			value = 400
			if = {
				limit = {
					exists = scope:title
				}
				scope:title.lessee.holy_order ?= {
					every_leased_title = {
						limit = {
							county.holder ?= {
								this = root
							}
						}
						add = 200
					}
				}
			}
			if = {
				limit = {
					domicile ?= {
						has_domicile_parameter = estate_cloister_monastic_movement
					}
				}
				divide = 2
			}
			if = {
				limit = {
					is_ai = yes
				}
				divide = 2
			}
		}
		piety = {
			value = 400
			if = {
				limit = {
					exists = scope:title
				}
				scope:title.lessee.holy_order ?= {
					every_leased_title = {
						limit = {
							county.holder ?= {
								this = root
							}
						}
						add = 200
					}
				}
			}
			if = {
				limit = {
					domicile ?= {
						has_domicile_parameter = estate_cloister_monastic_movement
					}
				}
				divide = 2
			}
			if = {
				limit = {
					is_ai = yes
				}
				divide = 2
			}
		}
	}
	
	ai_potential = {
		highest_held_title_tier >= tier_duchy
		is_playable_character = yes
		any_held_county = {
			county_is_valid_for_monastic_lease_trigger = { CHARACTER = root }
		}
	}

	ai_will_do = {
		base = 200
		modifier = {
			add = {
				faith = {
					# Don't create more than 20 monastic orders for a faith
					every_faith_holy_order = {
						subtract = 10
					}
				}
			}
		}
		modifier = {
			government_has_flag = government_is_mandala
			add = -150
		}
		modifier = {
			add = ai_zeal
			ai_zeal < 0
		}
		modifier = {
			add = ai_boldness
			ai_boldness < 0
		}
		modifier = {
			factor = 2
			OR = {
				#Opinion of target abbot
				AND = {
					exists = scope:title.lessee
					has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:title.lessee }
				}
				#Opinion of target abbot liege
				AND = {
					exists = scope:title.lessee.holy_order
					has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:title.lessee.holy_order.holy_order_patron }
				}
				#Has tenet they want promoted
				AND = {
					OR = {
						has_trait = education_learning
						has_lifestyle = learning_lifestyle
						government_has_flag = government_is_theocracy
					}
					any_personal_tenet = {
						root.rite = {
							NOR = {
								has_tenet_status = {
									tenet = prev
									status = prohibited
								}
								has_tenet_status = {
									tenet = prev
									status = core
								}
							}
						}
					}
					exists = scope:title.lessee.holy_order.order_tenet
					any_personal_tenet = {
						this = scope:title.lessee.holy_order.order_tenet
					}
					rite = scope:title.lessee.rite
					NOT = {
						house ?= {
							any_house_member = {
								monastic_movement_potential_leader_trigger = yes
							}
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			OR = {
				#Opinion of target abbot
				AND = {
					exists = scope:title.lessee
					OR = {
						has_any_good_relationship_with_character_trigger = { CHARACTER = scope:title.lessee }
						scope:title.lessee.puppeteer ?= root
						AND = {
							exists = house
							scope:title.lessee.house ?= root.house
						}
					}
				}
				#Opinion of target abbot liege
				AND = {
					exists = scope:title.lessee.holy_order
					OR = {
						has_any_good_relationship_with_character_trigger = { CHARACTER = scope:title.lessee.holy_order.holy_order_patron }
						has_dread_level_towards = {
							target = scope:title.lessee.holy_order.holy_order_patron
							level >= 1
						}
						puppeteer ?= scope:title.lessee.holy_order.holy_order_patron
					}
				}
				#Supports order tenet 
				AND = {
					exists = scope:title.lessee.holy_order.order_tenet
					any_personal_tenet = {
						this = scope:title.lessee.holy_order.order_tenet
					}
					rite = scope:title.lessee.rite
					NOT = {
						house ?= {
							any_house_member = {
								monastic_movement_potential_leader_trigger = yes
							}
						}
					}
				}
			}
		}
	}
}
