
namespace = holy_order

# Holy Order land management events:
# 1000+

# Holy Order character events:
# 2000+


#Send character to a Holy Order
#by Linnéa Thimrén
holy_order.0001 = {
	type = character_event
	title = holy_order.0001.t
	desc = {
		desc = holy_order.0001.desc_opening
		first_valid = {
			triggered_desc = {
				trigger = {
					has_any_relation_trigger = { CHARACTER = scope:recipient }
				}
				desc = holy_order.0001.desc_relation
			}
			desc = holy_order.0001.desc
		}
	}
	theme = faith
	override_background = {
		reference = courtyard
	}
	left_portrait = scope:recipient

	immediate = {
		#Save holy order(s) scope
		scope:actor.faith = {
			random_faith_holy_order = {
				limit = {
					leader = { NOT = { has_relation_rival = scope:recipient } }
				}
				weight = {
					base = 1
					modifier = {
						add = 1
						holy_order_patron = root
					}
				}
				leader = {
					save_scope_as = grandmaster_1
				}
				save_scope_as = holy_order_1
			}
			if = {
				limit = {
					any_faith_holy_order = {
						leader = { NOT = { has_relation_rival = scope:recipient } }
						this != scope:holy_order_1
					}
				}
				random_faith_holy_order = {
					limit = {
						leader = { NOT = { has_relation_rival = scope:recipient } }
						this != scope:holy_order_1
					}
					leader = {
						save_scope_as = grandmaster_2
					}
					save_scope_as = holy_order_2
				}
			}
			if = {
				limit = {
					any_faith_holy_order = {
						leader = { NOT = { has_relation_rival = scope:recipient } }
						NOR = {
							this = scope:holy_order_1
							this = scope:holy_order_2
						}
					}
				}
				random_faith_holy_order = {
					limit = {
						leader = { NOT = { has_relation_rival = scope:recipient } }
						NOR = {
							this = scope:holy_order_1
							this = scope:holy_order_2
						}
					}
					leader = {
						save_scope_as = grandmaster_3
					}
					save_scope_as = holy_order_3
				}
			}
			if = {
				limit = {
					any_faith_holy_order = {
						leader = { NOT = { has_relation_rival = scope:recipient } }
						NOR = {
							this = scope:holy_order_1
							this = scope:holy_order_2
							this = scope:holy_order_3
						}
					}
				}
				random_faith_holy_order = {
					limit = {
						leader = { NOT = { has_relation_rival = scope:recipient } }
						NOR = {
							this = scope:holy_order_1
							this = scope:holy_order_2
							this = scope:holy_order_3
						}
					}
					leader = {
						save_scope_as = grandmaster_4
					}
					save_scope_as = holy_order_4
				} #this should be enough since it's VERY unlikely that you have this many holy orders lying around
			}
		}
	}

	option = {
		name = holy_order.0001.a
		send_child_to_holy_order_effect = {
			GRANDMASTER = grandmaster_1
		}
	}

	option = {
		name = holy_order.0001.b
		trigger = { exists = scope:holy_order_2 }
		send_child_to_holy_order_effect = {
			GRANDMASTER = grandmaster_2
		}
	}
	
	option = {
		name = holy_order.0001.c
		trigger = { exists = scope:holy_order_3 }
		send_child_to_holy_order_effect = {
			GRANDMASTER = grandmaster_3
		}
	}

	option = {
		name = holy_order.0001.d
		trigger = { exists = scope:holy_order_4 }
		send_child_to_holy_order_effect = {
			GRANDMASTER = grandmaster_4
		}
	}

	option = {
		name = holy_order.0001.e
	}
}

#Acceptance to clergy
#by Linnéa Thimrén
holy_order.0002 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = holy_order.0002.desc
		
	sender = scope:recipient

	immediate = {
		send_child_to_clergy_effect = yes
	}

	option = { #Send them on their way
		name = holy_order.0002.a
	}
}



#A member of a Holy Order has a random chance of gaining the pilgrim trait since non-landed characters can't go on a pilgrimage
#by Linnéa Thimrén
holy_order.0100 = {
	hidden = yes
	
	trigger = { #Is a member of a holy order
		NOT = { has_trait = pilgrim }
		has_trait = order_member
		faith = {
			any_faith_holy_order = { #Your faith must have a Holy Order
				leader = {
					this = root.liege
				}
			}
		}
	}

	immediate = {
		add_trait = pilgrim
	}
}


#You get a loan from a Holy Order
#by Linnéa Thimrén
holy_order.0200 = {
	type = letter_event
	opening = {
		first_valid = {
			triggered_desc = {
				trigger = {
					piety_level < 1
				}
				desc = char_interaction.0001.opening_low_piety
			}
			desc = holy_order.0200.desc_opening
		}
	}
	desc = holy_order.0200.desc
	sender = scope:recipient

	option = {
		name = holy_order.0200.a
		show_as_tooltip = {
			scope:recipient = {
				pay_treasury_or_gold = {
					target = root
					value = root.holy_order_gold_value
				}
			}
		}
		set_up_loan_effect = {
			LENDER = scope:recipient
			AMOUNT = holy_order_gold_value
		}
		#Set up extra variable to track time
		scope:recipient = {
			set_variable = {
				name = years_since_loan
				value = 0
			}
		}
	}

	option = { #Your piety level is too low :( sinner!
		name = holy_order.0200.b
		trigger = {
			piety_level < 1
		}
		show_as_unavailable = { piety_level < 1 }
	}
}

#To handle inheritance of loans in a Holy Order
#by Linnéa Thimrén
holy_order.0201 = {
	hidden = yes

	immediate = {
		remove_variable = loan_holder
		scope:order ={
			leader = {
				save_scope_as = new_leader
			}
		}
		set_variable = {
			name = loan_holder
			value = scope:new_leader
		}
		scope:new_leader = {
			set_variable = {
				name = loan_amount_owed
				value = root.var:loan_amount_owed
			}
			set_variable = {
				name = owes_me_money
				value = root
			}
		}
	}
}

#To increment years
#by Linnéa Thimrén
holy_order.0206 = {
	hidden = yes

	trigger = {
		exists = var:owes_me_money
		exists = var:years_since_loan
	}

	immediate = {
		change_variable = {
			name = years_since_loan
			add = 1
		}
	}
}

scripted_trigger holy_order_request_child_trigger = {
	is_courtier_of = root
	NOT = { has_trait = devoted }
	is_adult = yes
	has_education_martial_trigger = yes
	can_be_warrior_trigger = { ARMY_OWNER = root }
}

#Holy order demands payment
#by Linnéa Thimrén
holy_order.0202 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:asked_for_time
				}
				desc = holy_order.0202.second_time
			}
			desc = holy_order.0202.opening
		}
		desc = holy_order.0202.desc
	}
	sender = scope:recipient

	trigger = {
		has_character_flag = borrow_from_holy_order
		exists = var:loan_holder
		var:loan_holder = {
			exists = var:years_since_loan
			var:years_since_loan >= 10
		}
	}

	immediate = {
		var:loan_holder = {
			save_scope_as = recipient
		}
		faith = {
			random_faith_holy_order = {
				limit = {
					leader = {
						this = scope:recipient
					}
				}
				save_scope_as = holy_order
			}
		}
	}

	option = { #Pay back
		name = holy_order.0202.a
		trigger = {
			can_execute_decision = repay_loan_decision
		}
		show_as_unavailable = { NOT = { can_execute_decision = repay_loan_decision } }
		flavor = { #To get warning if you don't have enough money
			triggered_desc = {
				trigger = {
					NOT = { can_execute_decision = repay_loan_decision }
				}
				desc = holy_order.0202.a._not.tt
			}
		}
		show_as_tooltip = {
			pay_treasury_or_gold = {
				value = var:loan_amount_owed
				target = scope:recipient
			}
		}
		execute_decision = repay_loan_decision
	}

	option = { #Pretty please give me more time?
		name = {
			trigger = {
				faith = { religion_tag = christianity_religion }
			}
			text = holy_order.0202.b_christian
		}
		name = {
			trigger = {
				NOT = { faith = { religion_tag = christianity_religion } }
			}
			text = holy_order.0202.b
		}
		trigger = {
			piety_level >= 3
		}
		show_as_unavailable = { piety_level < 3 }
		custom_tooltip = holy_order.0202.b.tt
		save_scope_value_as = {
			name = asked_for_time
			value = yes
		}
		trigger_event = {
			id = holy_order.0202
			years = 5
		}
	}

	option = { #no
		name = holy_order.0202.c
		flavor = holy_order.0202.c.tt
	}

	option = { #no
		name = holy_order.0202.d
		if = {
			limit = {
				exists = faith.religious_head
			}
			faith.religious_head = {
				add_opinion = {
					modifier = not_repay_loan_opinion
					target = root
				}
			}
		}
		scope:recipient = {
			add_opinion = {
				modifier = not_repay_loan_opinion
				target = root
			}
		}
		if = { #are we still of the same faith?
			limit = {
				scope:recipient.faith = root.faith
			}
			add_piety_level = -1
		}
		remove_character_flag = borrow_from_holy_order
	}

	after = { #Consequences for not paying back
		hidden_effect = {
			random_list = {
				50 = {
					trigger = {
						NOT = { exists = scope:asked_for_time }
						any_held_title = {
							title_tier = barony
							barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
						}
					}
					trigger_event = { #They ask for a castle
						id = holy_order.0203
						days = { 30 90 }
					}
				}
				50 = {
					trigger = { #Has a fitting child
						NOT = { exists = scope:asked_for_time }
						any_child = {
							holy_order_request_child_trigger = yes
						}
					}
					trigger_event = { #They ask for a child
						id = holy_order.0204
						days = { 30 90 }
					}
				}
			}
		}
	}
}

#Holy Order demands castle
#by Linnéa Thimrén
holy_order.0203 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = holy_order.0203.desc
	sender = scope:recipient

	trigger = {
		has_character_flag = borrow_from_holy_order
		any_held_title = {
			title_tier = barony
			barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
		}
	}

	on_trigger_fail = { #If no available children exists now
		if = {
			limit = {
				has_character_flag = borrow_from_holy_order
				any_child = {
					holy_order_request_child_trigger = yes
				}
			}
			trigger_event = holy_order.0204
		}
		else = { #No options left at all
			trigger_event = holy_order.0205
		}
	}

	immediate = {
		random_held_title = {
			title_tier = barony
			limit = {
				barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
			}
			save_scope_as = province
		}
	}

	option = {
		name = holy_order.0203.a
		custom_tooltip = holy_order.0204.a.tt
		remove_character_flag = borrow_from_holy_order
		scope:province = {
			lease_out_to = scope:holy_order
		}
	}

	option = { #refuse
		name = holy_order.0203.b
		if = {
			limit = {
				exists = scope:recipient.faith.religious_head
			}
			scope:recipient.faith.religious_head = {
				add_opinion = {
					modifier = not_repay_loan_opinion
					target = root
				}
			}
		}
		scope:recipient = {
			add_opinion = {
				modifier = not_repay_loan_opinion
				target = root
			}
		}

		if = { #are we still of the same faith?
			limit = {
				scope:recipient.faith = root.faith
			}
			add_piety_level = -1
			remove_character_flag = borrow_from_holy_order
		}
	}
}

#Holy Order demands child
#by Linnéa Thimrén
holy_order.0204 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = holy_order.0204.desc
	sender = scope:recipient

	trigger = {
		has_character_flag = borrow_from_holy_order
		any_child = {
			holy_order_request_child_trigger = yes
		}
	}

	on_trigger_fail = { #If no available children exists now
		if = {
			limit = {
				has_character_flag = borrow_from_holy_order
				any_held_title = {
					title_tier = barony
					barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
				}
			}
			trigger_event = holy_order.0203
		}
		else = { #No options left at all
			trigger_event = holy_order.0205
		}
	}

	immediate = {
		random_child = {
			limit = {
				holy_order_request_child_trigger = yes
			}
			save_scope_as = child
		}
	}

	option = {
		name = holy_order.0204.a
		custom_tooltip = holy_order.0204.a.tt
		remove_character_flag = borrow_from_holy_order
		scope:child = {
			add_trait = order_member
			add_piety_level = 1
			if = { # Ensure that your child is of the correct faith to join the holy order.
				limit = {
					faith != scope:recipient.faith
				}
				set_character_faith = scope:recipient.faith
			}
		}
		scope:recipient = {
			add_courtier = scope:child
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 20
			}
		}
		add_piety = medium_piety_gain
		dynasty = {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
	}

	option = { #refuse
		name = holy_order.0203.b
		scope:recipient.faith.religious_head = {
			add_opinion = {
				modifier = not_repay_loan_opinion
				target = root
			}
		}
		scope:recipient = {
			add_opinion = {
				modifier = not_repay_loan_opinion
				target = root
			}
		}
		if = { #are we still of the same faith?
			limit = {
				scope:recipient.faith = root.faith
			}
			add_piety_level = -1
			remove_character_flag = borrow_from_holy_order
		}
	}
}

#No options available, send punishment event
holy_order.0205 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = holy_order.0205.desc
	sender = scope:recipient

	option = {
		name = holy_order.0205.a
		if = {
			limit = {
				exists = faith.religious_head
			}
			faith.religious_head = {
				add_opinion = {
					modifier = not_repay_loan_opinion
					target = root
				}
			}
		}
		scope:recipient = {
			add_opinion = {
				modifier = not_repay_loan_opinion
				target = root
			}
		}
		add_piety_level = -1
		remove_character_flag = borrow_from_holy_order
	}
}




#Holy order looks for someone to ask for land from
# by Petter Vilberg
holy_order.1000 = {
	hidden = yes
	scope = faith

	trigger = {
		any_faith_holy_order = {
			holy_order_1000_holy_order_trigger = yes
		}
		any_faith_ruler = {
			holy_order_1000_request_target_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		#Reduced chance if there are no HOs under the soft threshold:
		modifier = {
			add = -0.8
			NOT = {
				any_faith_holy_order = {
					holy_order_1000_holy_order_trigger = yes
					num_leased_titles < holy_order_barony_number_soft_threshold
				}
			}
		}
	}

	immediate = {
		random_faith_holy_order = {
			limit = {
				holy_order_1000_holy_order_trigger = yes
			}
			weight = {
				base = 1
				modifier = { #HO is already past the soft threshold
					add = -0.9
					num_leased_titles >= holy_order_barony_number_soft_threshold
				}
				#More likely to pick Holy Orders that don't have a lot of territory
				compare_modifier = {
					value = num_leased_titles
					multiplier = -0.05
				}
			}
			save_scope_as = holy_order
			leader = {
				save_scope_as = grandmaster
			}
		}
		random_ruler = {
			limit = {
				holy_order_1000_request_target_trigger = yes
			}
			weight = {
				base = 1

				#More likely to ask their patron for more land
				modifier = {
					add = 5
					this = scope:holy_order.holy_order_patron
				}
				#Less likely to ask you the more you're already providing HOs:
				modifier = {
					add = -0.1
					any_held_title = {
						is_under_holy_order_lease = yes
						count >= 1
					}
				}
				modifier = {
					add = -0.2
					any_held_title = {
						is_under_holy_order_lease = yes
						count > 1
					}
				}
				modifier = {
					add = -0.3
					any_held_title = {
						is_under_holy_order_lease = yes
						count > 3
					}
				}
				modifier = {
					add = -0.3
					any_held_title = {
						is_under_holy_order_lease = yes
						count > 5
					}
				}
			}
			save_scope_as = request_target
			random_sub_realm_barony = {
				limit = {
					holy_order_1000_target_barony_trigger = { CHARACTER = scope:request_target }
				}
				weight = {
					base = 1
					#Way more likely to choose from the target's personal domain:
					modifier = {
						add = -0.8
						holder != scope:request_target
					}
				}
				save_scope_as = target_barony
			}
		}

		if = {
			#Chance of "discarding" a holy order's request if it's too big
			limit = {
				scope:holy_order = {
					num_leased_titles >= holy_order_barony_number_soft_threshold
				}
			}
			random = {
				chance = 50
				save_scope_value_as = {
					name = cancel_event
					value = yes
				}
				scope:holy_order.title = {
					set_variable = {
						name = discarded_for_being_too_big
						value = yes
						years = 5
					}
				}
			}
		}

		if = {
			limit = {
				NOT = {
					exists = scope:cancel_event
				}
			}
			
			scope:request_target = {
				trigger_event = holy_order.1001
			}
		}
	}
}


#Holy Order requests a Barony for Gold
holy_order.1001 = {
	type = letter_event
	opening = {
		desc = holy_order_letter.opening
	}
	desc = {
		desc = holy_order.1001.intro
		desc = holy_order.1001.end
	}
	sender = scope:grandmaster
	
	immediate = {
		save_scope_as = ruler
		add_character_flag = {
			flag = holy_order_recently_requested_land
			years = 3
		}
		if = {
			limit = {
				any_sub_realm_barony = {
					holy_order_1000_target_barony_trigger = { CHARACTER = root }
					this != scope:target_barony
					holy_order_barony_value_calculation <= scope:target_barony.holy_order_barony_value_calculation
				}
			}
			random_sub_realm_barony = {
				limit = {
					holy_order_1000_target_barony_trigger = { CHARACTER = root }
					this != scope:target_barony
					holy_order_barony_value_calculation <= scope:target_barony.holy_order_barony_value_calculation
				}
				weight = {
					base = 1
					# Jomsvikings prefer to ask for coastal locations if they can.
					modifier = {
						add = 1000
						exists = global_var:jomsvikings_title
						scope:grandmaster.primary_title = global_var:jomsvikings_title
						this.title_province = { is_coastal = yes }
					}
				}
				save_scope_as = alternative_barony
			}
		}
	}

	option = {
		name = holy_order.1001.a
		trigger = {
			# fixing rare edge case where a character has become a baron/unlanded and would attempt to give away their last title
			is_landed = yes
			highest_held_title_tier > tier_barony
			# fixing rare edge case where a title is leased out twice
			scope:target_barony = { is_leased_out = no }
		}

		add_treasury_or_gold = scope:target_barony.holy_order_barony_value_calculation
		
		scope:target_barony = {
			if = { #To make sure the effect works properly we have to have the title when we lease it out
				limit = {
					holder != root
				}
				root = { get_title = prev }
			}
			lease_out_to = scope:holy_order
		}
		scope:grandmaster = {
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 25
			}
		}
		hidden_effect = {
			scope:holy_order.title = {
				set_variable = {
					name = received_new_land
					value = yes
					years = 3
				}
			}	
			add_character_flag = {
				flag = holy_order_recently_received_land
				years = 10
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = -1
				ai_zeal = 1
			}
		}
	}

	option = {
		name = holy_order.1001.b
		trigger = {
			exists = scope:alternative_barony
		}

		add_treasury_or_gold = scope:alternative_barony.holy_order_barony_value_calculation

		scope:alternative_barony = {
			if = { #To make sure the effect works properly we have to have the title when we lease it out
				limit = {
					holder != root
				}
				root = { get_title = prev }
			}
			lease_out_to = scope:holy_order
		}
		scope:grandmaster = {
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 20
			}
		}
		hidden_effect = {
			scope:holy_order.title = {
				set_variable = {
					name = received_new_land
					value = yes
					years = 3
				}
			}	
			add_character_flag = {
				flag = holy_order_recently_received_land
				years = 10
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = -0.8
				ai_zeal = 0.5
			}
		}
	}
	
	option = {
		name = holy_order.1001.c
		scope:grandmaster = {
			add_opinion = {
				target = root
				modifier = disappointed_opinion
				opinion = -10
			}
		}
		ai_chance = {
			base = 100
			opinion_modifier = {
				opinion_target = scope:grandmaster
				multiplier = -0.9
			}
		}
	}
}
	


#########
# Holy Order Character Events
# 2000+
#########

##########
# Child wishes to join Holy Order
# by Petter Vilberg
#########
scripted_trigger holy_order_2001_candidate_trigger = {
	is_available_ai_adult = yes
	is_ruler = no
	#Not very likely to inherit a lot
	OR = {
		NOT = { is_heir_of = root }
		root.primary_title = {
			place_in_line_of_succession = {
				target = prev
				value > 2
			}
		}
	}
	faith = root.faith
	OR = {
		is_courtier_of = root
		AND = {
			root = { is_ai = no }
			court_owner ?= { is_ai = yes }
		}
	}
	is_married = no
	NOT = {
		exists = betrothed
	}
	NOT = { has_character_flag = holy_order_member_requested_return_home }
	NOT = {
		has_relation_rival = scope:holy_order.leader
	}
	can_be_warrior_trigger = { ARMY_OWNER = root }
}

scripted_trigger holy_order_2001_holy_order_trigger = {
	always = yes
}

holy_order.2001 = {
	type = character_event
	title = holy_order.2001.t
	desc = holy_order.2001.desc
	left_portrait = scope:child
	lower_right_portrait = scope:grandmaster
	theme = faith
	override_background = {
		reference = sitting_room
	}
	
	trigger = {
		NOT = { has_character_flag = had_event_holy_order_2001 }
		faith = {
			any_faith_holy_order = {
				holy_order_2001_holy_order_trigger = yes
				save_temporary_scope_as = holy_order
			}
		}
		any_child = {
			holy_order_2001_candidate_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.5
			faith = {
				any_faith_holy_order = {
					holy_order_2001_holy_order_trigger = yes
					save_temporary_scope_as = holy_order
				}
			}
			any_child = {
				holy_order_2001_candidate_trigger = yes
				OR = {
					NOT = { is_heir_of = root }
					root.primary_title = {
						place_in_line_of_succession = {
							target = prev
							value > 4
						}
					}
				}
			}
		}
		modifier = {
			add = -0.5
			any_child = {
				count < 4
			}
		}
		modifier = {
			add = 0.5
			faith = {
				any_faith_holy_order = {
					holy_order_2001_holy_order_trigger = yes
					save_temporary_scope_as = holy_order
				}
			}
			any_child = {
				holy_order_2001_candidate_trigger = yes
				has_trait = zealous
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_holy_order_2001
			years = 5
		}
		faith = {
			random_faith_holy_order = {
				limit = {
					holy_order_2001_holy_order_trigger = yes
				}
				weight = {
					base = 1
					modifier = {
						add = 1
						holy_order_patron = root
					}
				}
				save_scope_as = holy_order
				leader = {
					save_scope_as = grandmaster
				}
			}
		}
		random_child = {
			limit = {
				holy_order_2001_candidate_trigger = yes
			}
			weight = {
				base = 1
				modifier = {
					add = 1
					OR = {
						NOT = { is_heir_of = root }
						root.primary_title = {
							place_in_line_of_succession = {
								target = prev
								value >= 4
							}
						}
					}
				}
				modifier = {
					add = 0.5
					OR = {
						NOT = { is_heir_of = root }
						root.primary_title = {
							place_in_line_of_succession = {
								target = prev
								value >= 5
							}
						}
					}
				}
				modifier = {
					add = 1
					has_trait = zealous
				}
				modifier = {
					add = 0.25
					has_trait = education_martial
				}
			}
			save_scope_as = child
		}
	}

	#Yes, go
	option = {
		name = holy_order.2001.a
		scope:child = {
			add_trait = order_member
			add_piety_level = 1
			if = { # Ensure that your child is of the correct faith to join the holy order (if they convert between frames it causes issues otherwise)
				limit = {
					faith != scope:grandmaster.faith
				}
				set_character_faith = scope:grandmaster.faith
			}
		}
		scope:grandmaster = {
			add_courtier = scope:child
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 20
			}
		}
		add_piety = medium_piety_gain
		dynasty = {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
	}

	#I have other plans for you
	option = {
		name = holy_order.2001.b
		scope:child = {
			add_opinion = {
				target = root
				modifier = refusal_opinion
				opinion = -10
			}
		}
	}
}

#######
# Sibling wants to join a Holy Order
# by Petter Vilberg
#######
# Note: Intentionally almost identical to holy_order.2001

holy_order.2002 = {
	type = character_event
	title = holy_order.2001.t
	desc = holy_order.2002.desc
	left_portrait = scope:sibling
	lower_right_portrait = scope:grandmaster
	theme = faith
	override_background = {
		reference = sitting_room
	}
	
	trigger = {
		NOT = { has_character_flag = had_event_holy_order_2002 }
		faith = {
			any_faith_holy_order = {
				holy_order_2001_holy_order_trigger = yes
				save_temporary_scope_as = holy_order
			}
		}
		any_sibling = {
			holy_order_2001_candidate_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.5
			faith = {
				any_faith_holy_order = {
					holy_order_2001_holy_order_trigger = yes
					save_temporary_scope_as = holy_order
				}
			}
			any_sibling = {
				holy_order_2001_candidate_trigger = yes
				OR = {
					NOT = { is_heir_of = root }
					root.primary_title = {
						place_in_line_of_succession = {
							target = prev
							value > 4
						}
					}
				}
			}
		}
		modifier = {
			add = 0.5
			faith = {
				any_faith_holy_order = {
					holy_order_2001_holy_order_trigger = yes
					save_temporary_scope_as = holy_order
				}
			}
			any_sibling = {
				holy_order_2001_candidate_trigger = yes
				has_trait = zealous
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_holy_order_2002
			years = 5
		}
		faith = {
			random_faith_holy_order = {
				limit = {
					holy_order_2001_holy_order_trigger = yes
				}
				weight = {
					base = 1
					modifier = {
						add = 1
						holy_order_patron = root
					}
				}
				save_scope_as = holy_order
				leader = {
					save_scope_as = grandmaster
				}
			}
		}
		random_sibling = {
			limit = {
				holy_order_2001_candidate_trigger = yes
			}
			weight = {
				base = 1
				modifier = {
					add = 1
					OR = {
						NOT = { is_heir_of = root }
						root.primary_title = {
							place_in_line_of_succession = {
								target = prev
								value >= 4
							}
						}
					}
				}
				modifier = {
					add = 0.5
					OR = {
						NOT = { is_heir_of = root }
						root.primary_title = {
							place_in_line_of_succession = {
								target = prev
								value >= 5
							}
						}
					}
				}
				modifier = {
					add = 1
					has_trait = zealous
				}
				modifier = {
					add = 0.25
					has_trait = education_martial
				}
			}
			save_scope_as = sibling
		}
	}

	#Yes, go
	option = {
		name = holy_order.2002.a
		scope:sibling = {
			add_trait = order_member
			add_piety_level = 1
			if = { # Ensure that your child is of the correct faith to join the holy order (if they convert between frames it causes issues otherwise)
				limit = {
					faith != scope:grandmaster.faith
				}
				set_character_faith = scope:grandmaster.faith
			}
		}
		scope:grandmaster = {
			add_courtier = scope:sibling
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 20
			}
		}
		add_piety = medium_piety_gain
		dynasty = {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
	}

	#I have other plans for you
	option = {
		name = holy_order.2002.b
		scope:sibling = {
			add_opinion = {
				target = root
				modifier = refusal_opinion
				opinion = -20
			}
		}
	}
}



#################
# Holy Order suggests you send them a child
# by Petter Vilberg
#################

scripted_trigger holy_order_2003_candidate_trigger = {
	is_adult = no
	age < 15
	age > 7
	is_available = yes
	is_ruler = no
	is_ai = yes
	#Not very likely to inherit a lot
	OR = {
		NOT = { is_heir_of = root }
		root.primary_title = {
			place_in_line_of_succession = {
				target = prev
				value > 2
			}
		}
	}
	faith = root.faith
	OR = {
		is_courtier_of = root
		AND = {
			root = { is_ai = no }
			court_owner = { is_ai = yes }
		}
	}
	NOT = { has_character_flag = holy_order_member_requested_return_home }
	trigger_if = {
		limit = {
			root = { is_ai = yes }
			court_owner = { is_ai = no }
		}
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = court_owner }
	}
	trigger_else = {
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = root }
	}
}

scripted_trigger holy_order_2003_holy_order_trigger = {
	leader = {
		NOT = { has_relation_rival = root }
	}
}

holy_order.2003 = {
	type = letter_event
	opening = holy_order.2003.opening
	desc = holy_order.2003.desc
	sender = scope:grandmaster
	
	trigger = {
		NOT = { has_character_flag = had_holy_order_2003 }
		is_landed_or_landless_administrative = yes
		any_child = {
			holy_order_2003_candidate_trigger = yes
		}
		faith = {
			any_faith_holy_order = {
				holy_order_2003_holy_order_trigger = yes
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_holy_order_2003
			years = 10
		}
		random_child = {
			limit = {
				holy_order_2003_candidate_trigger = yes		
			}
			weight = {
				base = 1
				modifier = {
					add = 1
					OR = {
						NOT = { is_heir_of = root }
						root.primary_title = {
							place_in_line_of_succession = {
								target = prev
								value > 3
							}
						}
					}
				}
				modifier = {
					add = 0.5
					has_focus = education_martial
				}
				modifier = {
					add = 0.5
					has_trait = rowdy
				}
			}
			save_scope_as = child
		}
		faith = {
			random_faith_holy_order = {
				limit = {
					holy_order_2003_holy_order_trigger = yes
				}
				save_scope_as = holy_order_scope
				leader = {
					save_scope_as = grandmaster
				}
			}
		}
		if = {
			limit = {
				any_child = {
					holy_order_2003_candidate_trigger = yes
					this != scope:child
				}
			}
			random_child = {
				limit = {
					holy_order_2003_candidate_trigger = yes
					this != scope:child	
				}
				save_scope_as = alternative_child
			}
		}
	}

	#Send them away
	option = {
		name = holy_order.2003.a
		dynasty = {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
		add_piety = major_piety_gain
		hidden_effect = {
			scope:child = {
				add_trait = order_member
				if = {
					limit = {
						NOR = {
							has_focus = education_martial
							has_focus = education_learning
						}
					}
					set_focus = education_martial
				}
				add_piety_level = 1
				if = { # Ensure that your child is of the correct faith to join the holy order (if they convert between frames it causes issues otherwise)
					limit = {
						faith != scope:grandmaster.faith
					}
					set_character_faith = scope:grandmaster.faith
				}
			}
		}
		custom_tooltip = holy_order.2003.child_joins_order
		scope:grandmaster = {
			hidden_effect = {
				add_courtier = scope:child
			}
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 30
			}
		}
	}

	#Send an alternative candidate
	option = {
		name = holy_order.2003.b
		trigger = {
			exists = scope:alternative_child
		}
		dynasty = {
			add_dynasty_prestige = medium_dynasty_prestige_gain
		}
		add_piety = major_piety_gain
		hidden_effect = {
			scope:alternative_child = {
				add_trait = order_member
				add_piety_level = 1
				if = {
					limit = {
						NOR = {
							has_focus = education_martial
							has_focus = education_learning
						}
					}
					set_focus = education_martial
				}
				if = { # Ensure that your child is of the correct faith to join the holy order (if they convert between frames it causes issues otherwise)
					limit = {
						faith != scope:grandmaster.faith
					}
					set_character_faith = scope:grandmaster.faith
				}
			}
		}
		custom_tooltip = holy_order.2003.alternative_child_joins_order
		scope:grandmaster = {
			hidden_effect = {
				add_courtier = scope:alternative_child
			}
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 30
			}
		}
	}
	
	#No
	option = {
		name = holy_order.2003.c
		scope:grandmaster = {
			add_opinion = {
				target = root
				modifier = disappointed_opinion
				opinion = -10
			}
		}
	}
}



#############
# Child returns to pick up the mantle
# by Petter Vilberg
#############

scripted_trigger holy_order_2004_child_trigger = {
	has_trait = order_member
	is_ai = yes
	is_adult = yes
	NOT = {
		has_character_flag = holy_order_member_requested_return_home
	}
}

holy_order.2004 = {
	type = letter_event
	opening = holy_order.2004.opening
	desc = holy_order.2004.desc
	sender = scope:child
	
	trigger = {
		any_child = {
			holy_order_2004_child_trigger = yes
		}
		OR = {
			AND = {
				#Must have either single-heir succession & no heir...
				has_realm_law_flag = advanced_succession_law
				NOT = { exists = player_heir }
			}
			AND = {
				#... Or partition & just the one.
				has_realm_law_flag = partition_succession_law
				any_heir = {
					is_child_of = root
					count < 2
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 2
			is_ai = no
		}
	}

	immediate = {
		random_child = {
			limit = {
				holy_order_2004_child_trigger = yes
			}
			save_scope_as = child
			add_character_flag = holy_order_member_requested_return_home
			court_owner = {
				save_scope_as = grandmaster
			}
		}
	}

	#Welcome home
	option = {
		name = holy_order.2004.a
		if = {
			limit = {
				scope:child = {
					NOT = {
						is_courtier_of = prev
					}
				}
			}
			add_courtier = scope:child
		}
		scope:child = {
			remove_trait = order_member
			add_character_modifier = {
				modifier = holy_order_experience_modifier
			}
		}
		ai_chance = {
			base = 100
		}
	}

	#There is no place for you here
	option = {
		name = holy_order.2004.b
		scope:child = {
			add_opinion = {
				target = root
				modifier = refusal_opinion
				opinion = -40
			}
		}
		ai_chance = {
			base = 10
		}
	}
}



###############
# A - Possibly Undesirable - Child Returns Home
# by Petter Vilberg
###############

scripted_trigger holy_order_2005_child_trigger = {
	has_trait = order_member
	is_adult = yes
	is_ai = yes
	NOT = {
		has_character_flag = holy_order_member_requested_return_home
	}
	num_sinful_traits > 0
}


holy_order.2005 = {
	type = character_event
	title = holy_order.2005.t
	desc = holy_order.2005.desc

	theme = faith
	override_background = {
		reference = throne_room
	}
	left_portrait = scope:child
	lower_right_portrait = scope:grandmaster
	
	trigger = {
		NOT = { has_character_flag = had_holy_order_2005 }
		any_child = {
			holy_order_2005_child_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 1
			any_child = {
				holy_order_2005_child_trigger = yes
				calc_true_if = {
					amount >= 2
					martial < average_skill_rating
					intrigue < average_skill_rating
					diplomacy < average_skill_rating
					stewardship < average_skill_rating
					learning < average_skill_rating
				}
			}
		}
		modifier = {
			add = 1
			any_child = {
				holy_order_2005_child_trigger = yes
				calc_true_if = {
					amount >= 3
					martial < average_skill_rating
					intrigue < average_skill_rating
					diplomacy < average_skill_rating
					stewardship < average_skill_rating
					learning < average_skill_rating
				}
			}
		}
		modifier = {
			add = 1
			any_child = {
				holy_order_2005_child_trigger = yes
				martial < average_skill_rating
				intrigue < average_skill_rating
				diplomacy < average_skill_rating
				stewardship < average_skill_rating
				learning < average_skill_rating
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_holy_order_2005
			years = 15
		}
		random_child = {
			limit = {
				holy_order_2005_child_trigger = yes
			}
			weight = {
				base = 1
				modifier = {
					add = 3
					calc_true_if = {
						amount = 3
						martial < average_skill_rating
						intrigue < average_skill_rating
						diplomacy < average_skill_rating
						stewardship < average_skill_rating
						learning < average_skill_rating
					}
				}
				modifier = {
					add = 5
					calc_true_if = {
						amount = 4
						martial < average_skill_rating
						intrigue < average_skill_rating
						diplomacy < average_skill_rating
						stewardship < average_skill_rating
						learning < average_skill_rating
					}
				}
				modifier = {
					add = 10
					martial < average_skill_rating
					intrigue < average_skill_rating
					diplomacy < average_skill_rating
					stewardship < average_skill_rating
					learning < average_skill_rating
				}
			}
			save_scope_as = child
			add_character_flag = holy_order_member_requested_return_home
			court_owner = {
				save_scope_as = grandmaster
			}
		}
	}

	#Uh-oh
	option = {
		name = holy_order.2005.a
		add_courtier = scope:child
		scope:child = {
			remove_trait = order_member
			add_character_modifier = {
				modifier = holy_order_experience_modifier
			}
		}
	}
}
