﻿# This is an override for the Vanilla create holy order decision
# This is allowed for non Regula religion rulers
create_holy_order_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_major_religion.dds"
	}
	decision_group_type = major

	ai_goal = yes

	is_shown = {
		is_landed = yes
		is_regula_trigger = no # Regula characters should use the other decision
		NOT = {
			faith = {
				any_faith_holy_order = {
					holy_order_patron = root
					save_temporary_scope_as = order
				}
			}
		}
		# Jomsvikings should use their special decision if appropriate.
		trigger_if = {
			limit = { has_fp1_dlc_trigger = yes }
			faith = {
				NOT = { fp1_valid_norse_faith_for_jomsvikings_trigger = yes }
			}
		}

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

	widget = {
		controller = create_holy_order
		barony_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 = scope:ruler }
				}
			}
		}
		trigger_if = {
			limit = {
				exists = faith.religious_head
				NOT = { 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 = {
				is_ai = yes
			}
			piety_level >= 1
		}
		trigger_else = {
			piety_level >= 3
		}
	}
	cost = {
		gold = {
			value = 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 = { NOT = { 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
			}

			create_holy_order_neutral_effect = {
				LEADER = scope:leader
				CAPITAL = scope:barony
				NEW_HO_SCOPE = new_holy_order
				FOUNDER = root
			}
		}
		create_holy_order_effect = yes
	}
	
	ai_potential = {
		highest_held_title_tier >= tier_kingdom
		is_playable_character = yes
		OR = {
			any_held_title = {
				tier = 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 holy orders for a faith
					every_faith_holy_order = {
						subtract = 40
					}
				}
			}
		}

	}
}

# This is our customised Holy order creation decision
# Mostly the same, except it creates female order members
regula_create_holy_order_decision = {

	# Text
	title = regula_create_holy_order_decision.t
	desc = regula_create_holy_order_decision_desc
	selection_tooltip = regula_create_holy_order_decision_tooltip
	confirm_text = regula_create_holy_order_decision_confirm


	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_major_religion.dds"
	}
	decision_group_type = major

	ai_goal = yes

	is_shown = {
		is_landed = yes
		is_regula_trigger = yes # Regula characters should use this decision
		NOT = {
			faith = {
				any_faith_holy_order = {
					holy_order_patron = root
					save_temporary_scope_as = order
				}
			}
		}
		# Jomsvikings should use their special decision if appropriate.
		trigger_if = {
			limit = { has_fp1_dlc_trigger = yes }
			faith = {
				NOT = { fp1_valid_norse_faith_for_jomsvikings_trigger = yes }
			}
		}

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

	widget = {
		controller = create_holy_order
		barony_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 = scope:ruler }
				}
			}
		}
		trigger_if = {
			limit = {
				exists = faith.religious_head
				NOT = { 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 = {
				is_ai = yes
			}
			piety_level >= 1
		}
		trigger_else = {
			piety_level >= 3
		}
	}
	cost = {
		gold = {
			value = holy_order_gold_cost
		}
		piety = {
			value = holy_order_piety_cost
		}
	}

	effect = {
		custom_tooltip = create_holy_order_decision_effect_message

		# Discover Virgo innovation into Magister culture if its not already unlocked
		if = {
			limit = {
				culture = {
					culture_head ?= root
					NOT = { has_innovation = innovation_regula_virgo_training }
				}
			}
			culture = {
				add_innovation = innovation_regula_virgo_training
			}
		}

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

			create_holy_order_neutral_effect = {
				LEADER = scope:leader
				CAPITAL = scope:barony
				NEW_HO_SCOPE = new_holy_order
				FOUNDER = root
			}
		}
		create_holy_order_effect = yes

		# Customise our holy order for RM
		# Remove male knights and create some female order members instead
		# Also add mulsa traits and make leader lustful
		# create_holy_order_effect handles making the initial holy order and standard effects (plus messages)
		hidden_effect = {
			if = { # If you have no valid barony this will throw errors when you open the decision since the leader won't be saved - we add this check to avoid that
				limit = {
					exists = scope:leader
				}
				scope:leader = {
					# Customised creation process here
					add_trait = lustful
					add_trait = mulsa

					# Give some starting initiate solider characters
					while = {
						count = 2
						create_character = {
							template = regula_initiate_solider_noble_character
							employer = scope:leader
							culture = scope:leader.culture
							faith = scope:leader.faith
							gender = female
							age = { 25 30 }
						}
					}
					while = {
						count = 3
						create_character = {
							template = regula_initiate_solider_common_character
							employer = scope:leader
							culture = scope:leader.culture
							faith = scope:leader.faith
							gender = female
							age = { 18 25 }
						}
					}

					# Get rid of male knights then make female knights "order members"
					every_courtier = {
						limit = { is_female = no }
						death = { death_reason = death_vanished }
					}
					every_courtier = {
						limit = { is_female = yes }
						add_trait = order_member
						add_trait = mulsa
					}
				}

				# Messages
				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
				}
			}
		}
	}
	
	ai_potential = {
		highest_held_title_tier >= tier_kingdom
		is_playable_character = yes
		OR = {
			any_held_title = {
				tier = 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 holy orders for a faith
					every_faith_holy_order = {
						subtract = 40
					}
				}
			}
		}
	}
}

# If we have a holy order within the grounds of our lands we can corrupt them
# This takes a single hostile holy order and converts their leased titles into a regula holy order
regula_corrupt_holy_order_decision = {

	# Text
	title = regula_corrupt_holy_order_decision.t
	desc = regula_corrupt_holy_order_decision_desc
	selection_tooltip = regula_corrupt_holy_order_decision_tooltip
	confirm_text = regula_corrupt_holy_order_decision_confirm.a

	# Art
	picture = {
		reference = "gfx/interface/illustrations/decisions/regula_corrupt_holy_order_decision.dds"
	}

	# Grouping
	decision_group_type = regula
	sort_order = 70

	# AI
	# This is player only
	ai_check_interval = 0
	ai_potential = { always = no }
	ai_will_do = { base = 0 }

	# Triggers for showing
	is_shown = {
		is_ai = no
		is_ruler = yes
		has_trait = magister_trait_group
		age >= 16

		any_sub_realm_barony = {
			is_under_holy_order_lease = yes
			has_revokable_lease = yes
			lessee_title.holder = {
				NOT = {
					faith = global_var:magister_character.faith
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		custom_description = {
			text = magister_trait_6_required_trigger
			has_trait_rank = {
				trait = magister_trait_group
				rank >= 6
			}
		}
	}

	# Widget
	widget = {
		controller = revoke_holy_order_lease
		barony_valid = {
			trigger_if = {
				limit = {
					exists = this
					is_under_holy_order_lease = yes
					has_revokable_lease = yes
					lessee_title.holder = {
						NOT = {
							faith = global_var:magister_character.faith
						}
					}
				}
			}
			trigger_else = {
				custom_description = {
					text = "has_holy_order_barony_selected"
					always = no
				}
			}
		}
	}

	# Cost
	cost = {
		piety = {
			value = 0
			if = {
				limit = {
					exists = scope:barony
				}
				add = scope:barony.lessee.regula_corrupt_holy_order_cost
			}
		}
	}

	# Effect
	effect = {
		custom_tooltip = "regula_corrupt_holy_order_decision_tooltip_effect"
		trigger_event = {
			id = regula_corrupt_holy_order_event.0001
		}
	}
}
