﻿# Determines if a character is allowed to be granted specific title by another character
# Root is the target character
# scope:liege is the granter
# scope:landed_title is the title in question
# scope:government_type the government type to be switched to (not set: default government type)
allowed_to_be_granted_titles_by = {
	trigger_if = {
		limit = {
			scope:landed_title = {
				tier <= tier_county
				title_province = { has_holding_type = church_holding }
			}
			scope:liege.rite = {
				NOT = { rite_has_parameter = lay_clergy }
			}
		}
		custom_tooltip = {
			text = grant_title_clerical_gender_tt
			can_be_granted_theocratic_titles_by = { RULER = scope:liege }
		}
	}
	trigger_else = {
		can_be_granted_titles_by = { RULER = scope:liege }
	}
}

# Rules for the Grant Title government type selection feature.
# These rules control which government types can be selected when granting a county or higher title
# to an unlanded character.

# Determines whether a specific government type appears in the picker in the Grant Titles interface.
# Called once per candidate government type. Recipient and county-tier checks are enforced in Code.
# Root is scope:actor (the granting ruler).
# scope:landed_title is the highest-tier title being granted.
# scope:government_type is the candidate government type being evaluated.
# scope:target_titles lists all titles being granted in this offer.
is_grant_government_shown = {
	# Can only grant theocratic government type if that is your faith's theocratic government type
	trigger_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_theocracy
			}
		}
		faith.faith_theocratic_goverment_type = scope:government_type
	}
	trigger_else_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_ecclesiastical
			}
		}
		faith.faith_theocratic_goverment_type = scope:government_type
	}
	# lock all DLC government types in case they are allowed in modded government
	trigger_else_if = {
		limit = {
			scope:government_type = {
				OR = {
					this = government_type:feudal_government
					this = government_type:tribal_government
				}
			}
		}
		OR = {
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_herder
			AND = {
				scope:government_type = { this = government_type:feudal_government }
				OR = {
					government_has_flag = government_is_administrative
					government_has_flag = government_is_theocracy
				}
				NOT = { 
					faith.religion = religion:islam_religion
				}
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:government_type = {
				this = government_type:clan_government
			}
		}
		OR = {
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_herder
			AND = {
				scope:government_type = { this = government_type:clan_government }
				OR = {
					government_has_flag = government_is_administrative
					government_has_flag = government_is_theocracy
				}
				faith.religion = religion:islam_religion
			}
		}
	}
	trigger_else_if = {
		limit = {
			OR = {
				scope:government_type = government_type:nomad_government
				scope:government_type = government_type:herder_government
			}
		}
		has_mpo_dlc_trigger = yes
	}
	trigger_else_if = {
		limit = {
			OR = {
				scope:government_type = government_type:wanua_government
				scope:government_type = government_type:celestial_government
				scope:government_type = government_type:mandala_government
				scope:government_type = government_type:steppe_admin_government
				scope:government_type = government_type:meritocratic_government
				scope:government_type = government_type:japan_administrative_government
				scope:government_type = government_type:japan_feudal_government
			}
		}
		has_tgp_dlc_trigger = yes
	}
	trigger_else = {
		always = no
	}
}

# Determines whether a specific government type is a valid choice for the grant.
# Called once per candidate government type.
# Root is scope:actor (the granting ruler).
# scope:recipient is the character receiving the title.
# scope:government_type is the candidate government type being evaluated.
# scope:landed_title is the highest-tier title being granted.
# scope:target_titles lists all titles being granted in this offer.
is_grant_government_valid = {
	# Theocratic grants: faith doctrines, tier gate (AI county-only / player up to duchy),
	# realm theocratic-share cap, and shared cooldown.
	trigger_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_theocracy
			}
		}
		rite = {
			rite_has_doctrine = doctrine_theocracy_temporal
		}
		scope:recipient = {
			faith = root.faith
			rite = {
				rite_has_doctrine = doctrine_theocracy_temporal
			}
			is_adult = yes
			trigger_if = {
				limit = {
					is_clergy = no # If your child is somehow already clergy it's fine to grant them a Theocracy
					root = { is_clergy = no } # If dad is clergy he can grant his kiddos theocracies, yay nepotism
				}
				NOR = {
					is_child_of = root
					is_grandchild_of = root
					is_great_grandchild_of = root
				}
			}
		}
		# Tier gate: AI may only grant theocratic counties; players are capped at duchy.
		trigger_if = {
			limit = { is_ai = yes }
			scope:landed_title = { tier = tier_county }
		}
		trigger_else = {
			custom_description = {
				text = grant_government_kingdom_blocked_tt
				scope:landed_title = { tier <= tier_duchy }
			}
		}
		custom_description = {
			text = grant_government_too_many_theocratic_vassals_tt
			trigger_if = {
				limit = { is_ai = yes }
				realm_theocratic_county_share_ai_below_cap_trigger = yes
			}
			trigger_else = {
				realm_theocratic_county_share_player_below_cap_trigger = yes
			}
		}
		custom_description = {
			text = grant_government_cooldown_tt
			NOT = { has_variable = grant_vassal_government_cooldown }
		}
	}
	# holding type san check
	trigger_else_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_republic
			}
		}
		scope:recipient = {
			is_adult = yes
			NOR = {
				is_child_of = root
				is_grandchild_of = root
				is_great_grandchild_of = root
			}
		}
		any_in_list = {
			list = target_titles
			tier = tier_county
			title_province ?= {
				OR = {
					has_holding_type = castle_holding
					has_holding_type = city_holding
				}
			}
		}
		custom_description = {
			text = grant_government_cooldown_tt
			NOT = { has_variable = grant_vassal_government_cooldown }
		}
	}
	trigger_else_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_feudal
			}
		}
		any_in_list = {
			list = target_titles
			tier = tier_county
			title_province ?= {
				has_holding_type = castle_holding
			}
		}
		# cooldown if you can't use the holding
		trigger_if = {
			limit = {
				government_has_flag = government_is_settled
				NOT = { government_has_flag = government_is_tribal }
			}
			custom_description = {
				text = grant_government_cooldown_tt
				NOT = { has_variable = grant_vassal_government_cooldown }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_clan
			}
			ep3_is_clan_inclined_trigger = no
		}
		scope:recipient = { ep3_is_clan_inclined_trigger = yes }
		any_in_list = {
			list = target_titles
			tier = tier_county
			title_province ?= {
				has_holding_type = castle_holding
			}
		}
		# cooldown if you can't use the holding
		trigger_if = {
			limit = {
				government_has_flag = government_is_settled
				NOT = { government_has_flag = government_is_tribal }
			}
			custom_description = {
				text = grant_government_cooldown_tt
				NOT = { has_variable = grant_vassal_government_cooldown }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_tribal
			}
		}
		any_in_list = {
			list = target_titles
			tier = tier_county
			title_province ?= {
				has_holding_type = tribal_holding
			}
		}
		# cooldown if you can't use the holding
		trigger_if = {
			limit = { government_has_flag = government_is_tribal }
			custom_description = {
				text = grant_government_cooldown_tt
				NOT = { has_variable = grant_vassal_government_cooldown }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:government_type = {
				OR = {
					government_type_has_flag = government_is_nomadic
					government_type_has_flag = government_is_herder
				}
			}
		}
		NOR = {
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_herder
		}
		any_in_list = {
			list = target_titles
			tier = tier_county
			title_province ?= {
				OR = {
					has_holding_type = nomad_holding
					has_holding_type = herder_holding
				}
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:government_type = {
				government_type_has_flag = government_is_settled
			}
		}
		any_in_list = {
			list = target_titles
			tier = tier_county
			title_province ?= {
				OR = {
					has_holding_type = castle_holding
					has_holding_type = city_holding
					has_holding_type = church_holding
				}
			}
		}
		trigger_if = {
			limit = {
				government_has_flag = government_is_settled
				NOT = { government_has_flag = government_is_tribal }
			}
			custom_description = {
				text = grant_government_cooldown_tt
				NOT = { has_variable = grant_vassal_government_cooldown }
			}
		}
	}
	trigger_else = {
		custom_description = {
			text = grant_government_cooldown_tt
			NOT = { has_variable = grant_vassal_government_cooldown }
		}
	}
}
