﻿# A regula-specific fork of vanilla `common_can_character_join_trigger`
# which excludes requirement that target must be immediate liege. Instead, we
# enforce liege or above.
#
# scope = the character being tested for join validity.
# FACTION_TARGET = the character being targetted by the faction.
regula_common_can_character_join_trigger = {
	NOR = { #No prince-bishop can ever join
		government_has_flag = government_is_theocracy
		trigger_if = {
			limit = { exists = cp:councillor_court_chaplain }
			this = cp:councillor_court_chaplain
		}
	}

	target_is_liege_or_above = $FACTION_TARGET$

	highest_held_title_tier > tier_barony

	#Not blocked through events
	custom_description = {
		text = character_blocked_from_joining
		NOT = {
			has_character_flag = joining_faction_block
		}
	}

	is_adult = yes
	NOT = { has_truce = $FACTION_TARGET$ }
	$FACTION_TARGET$ = {
		NOT = { has_strong_hook = root }
		NOT = { is_allied_to = root }
	}

	OR = {
		is_ai = no
		NOR = {
			has_relation_lover = $FACTION_TARGET$
			has_relation_friend = $FACTION_TARGET$
		}
	}

	$FACTION_TARGET$.highest_held_title_tier > tier_county

	custom_description = {
		text = character_has_faction_disabling_modifier
		character_has_faction_disabling_modifier_trigger = yes
	}
}

##############################
# Servitude Faction Triggers #
##############################
# Trigger which simply determines whether a faction is one of the servitude
# faction types.
#
# scope = the faction being tested.
regula_faction_is_servitude_faction_trigger = {
	OR = {
		faction_is_type = regula_servitude_faction
		faction_is_type = regula_peasant_servitude_faction
	}
}

# Trigger which simply determines whether a character is leading one of the
# servitude faction types.
#
# scope = the character being tested.
regula_faction_is_servitude_faction_leader = {
	OR = {
		is_leading_faction_type = regula_servitude_faction
		is_leading_faction_type = regula_peasant_servitude_faction
	}
}

# Common triggers which determine whether a servitude faction is valid.
#
# TARGET = the target of the faction.
regula_faction_servitude_common_valid_trigger = {
	# Servitude factions should only exist if the magister is alive & able to
	# take on vassals.
	magister_alive_trigger = yes
	global_var:magister_character = {
		highest_held_title_tier >= tier_duchy
	}

	$TARGET$ = {
		is_independent_ruler = yes
		NOT = { # No servitude faction target within the Magister's realm.
			target_is_same_character_or_above = global_var:magister_character
		}
		NOT = { # Can't have a Magister target.
			has_trait = magister_trait_group
		}
		NOT = { # Faction must not target a devoted ally of the magister.
			has_trait = devoted_trait_group
			any_ally = {
				has_trait = magister_trait_group
			}
		}
	}
}

# Determines whether the scoped character is valid to participate in a
# servitude faction.
#
# scope = the character in question.
# TARGET = the faction target to test.
# scope:faction = the faction if the faction already exists (join or valid context).
regula_faction_servitude_valid_character_trigger = {
	OR = {
		AND = { # Devoted Vassal Can Join or Create
			regula_common_can_character_join_trigger = {
				FACTION_TARGET = $TARGET$
			}

			# Should not join faction if any liege up the chain is already in a servitude faction.
			NOT = {
				any_liege_or_above = {
					exists = joined_faction
					joined_faction = {
						regula_faction_is_servitude_faction_trigger = yes
					}
				}
			}

			has_trait = devoted_trait_group
			NOT = { # Magister must be able to accept the vassal.
				highest_held_title_tier >= global_var:magister_character.highest_held_title_tier
			}
		}
		AND = { # Magister Can Join
			exists = scope:faction # But Can't Create
			regula_common_can_character_join_trigger = {
				FACTION_TARGET = $TARGET$
			}
			has_trait = magister_trait_group
		}
		AND = { # Peasant Leader Can Join By Construction
			exists = scope:faction # But Can't Create.
			has_variable = peasant_leader_faction
			var:peasant_leader_faction = scope:faction
		}
	}
}

# Determines whether a scoped county is a valid county to participate in a
# servitude faction.
#
# scope = the county in question
# TARGET = the faction target to test
regula_faction_servitude_valid_county_trigger = {
	# County must be unhappy.
	county_opinion < 0

	# Counties must have the magister's faith.
	faith = global_var:magister_character.faith

	# Counties can only be in a faction against their holder or an above liege, but
	# won't join one if the target is devoted or magister faith.
	holder = {
		target_is_same_character_or_above = $TARGET$
	}
	$TARGET$ = {
		NOR = {
			has_trait = devoted_trait_group
			faith = global_var:magister_character.faith
		}
	}

	# Counties will not join a servitude faction if their immediate holder is
	# devoted or has the magister's faith, regardless of actual target.
	holder = {
		NOR = {
			has_trait = devoted_trait_group
			faith = global_var:magister_character.faith
		}
	}

	# If a county's holder is in a servitude faction, then the county shouldn't
	# also be in a servitude faction. Same for liege or above of holder.
	holder = {
		NOR = {
			AND = {
				exists = joined_faction
				joined_faction = {
					regula_faction_is_servitude_faction_trigger = yes
				}
			}
			any_liege_or_above = {
				exists = joined_faction
				joined_faction = {
					regula_faction_is_servitude_faction_trigger = yes
				}
			}
		}
	}

	# Player capitals are blocked from joining servitude factions
	# (to avoid Game Overing a player due to a populist uprising against
	# their liege, which as a vassal they can't really prevent or fight).
	trigger_if = {
		limit = {
			holder = {
				is_ai = no
			}
		}
		NOT = { this = holder.capital_county }
	}
}

###########################
# Heresy Faction Triggers #
###########################
# Trigger which simply determines whether a faction is one of the heresy
# faction types.
#
# scope = the faction being tested.
regula_faction_is_heresy_faction_trigger = {
	faction_is_type = regula_heresy_faction
}

# Trigger which simply determines whether a character is leading one of the
# heresy faction types.
#
# scope = the character being tested.
regula_faction_is_heresy_faction_leader = {
	is_leading_faction_type = regula_heresy_faction
}

# Determines whether a scoped character is a valid character to participate in a
# heresy faction.
#
# scope = the county in question
# TARGET = the faction target to test
regula_faction_heresy_valid_character_trigger = {
	magister_alive_trigger = yes
	global_var:magister_character = $TARGET$

	# Can't be dead, imprisoned or incapable
	is_physically_able = yes

	regula_heresy_potential_heresy_character_trigger = yes

	# If some liege above me is in a heresy faction, I'll let them handle it.
	NOT = {
		any_liege_or_above = {
			joined_faction ?= {
				regula_faction_is_heresy_faction_trigger = yes
			}
		}
	}
}

# Determines whether a scoped county is a valid county to participate in a
# heresy faction.
#
# scope = the county in question
# TARGET = the faction target to test
regula_faction_heresy_valid_county_trigger = {
	magister_alive_trigger = yes
	global_var:magister_character = $TARGET$

	# A county that has the Regula faith will never rebel
	is_regula_trigger = no

	holder = {
		# Must be in the magister's realm.
		target_is_same_character_or_above = $TARGET$
	}

	# County must either be potential heresy county or it's holder must be a
	# conspirator.
	trigger_if = {
		# If our holder is a conspirator, then fine if this is their
		# capital county.
		limit = {
			holder = {
				regula_heresy_conspirator_character_trigger = yes
			}
		}
		this = holder.capital_county
	}
	trigger_else = {
		# Otherwise, if county is a conspiracy county, then allowed if no
		# holder or above joined the conspiracy.
		regula_heresy_potential_heresy_county_including_neighbors_trigger = yes
		holder = {
			NOT = {
				any_liege_or_above = {
					regula_heresy_conspirator_character_trigger = yes
				}
			}
		}
	}

	# Player capitals are blocked from joining heresy factions
	# (to avoid Game Over).
	trigger_if = {
		limit = {
			holder = {
				is_ai = no
			}
		}
		NOT = { this = holder.capital_county }
	}
}
