regula_servitude_faction = {
	casus_belli = regula_servitude_faction_war
	
	short_effect_desc = regula_servitude_faction_short_effect_desc

	sort_order = 10

	discontent_progress = {
		base = 0

		# We use a modifier as our base progress in order to take advantage of script values.
		modifier = {
			add = base_discontent_progress
			desc = "BASE_COLON"
		}

		# The higher above the power threshold the faction is, the faster discontent increases.
		modifier = {
			faction_power > faction_power_threshold
			add = faction_discontent_for_extra_power
			desc = "FACTION_DISCONTENT_POWER_ABOVE_THRESHOLD"
		}

		# Discontent slowly decays if power is under the threshold.
		modifier = {
			faction_power < faction_power_threshold
			add = {
				add = base_discontent_progress
				multiply = -2
			}
			desc = "FACTION_DISCONTENT_POWER_BELOW_THRESHOLD"
		}
	}

	power_threshold = {
		base = 80

		modifier = {
			add = 20
			faction_target = {
				has_perk = hard_rule_perk
			}
			desc = "FACTION_POWER_HARD_RULE"
		}
	}

	is_character_valid = {
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = cp:councillor_court_chaplain
				this = cp:councillor_court_chaplain
			}
		}

		scope:faction.faction_target = liege
		highest_held_title_tier > tier_barony

		has_trait = devoted_trait_group

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

	demand = {
		save_scope_as = faction

		faction_leader = {
			save_scope_as = faction_leader
		}

		faction_target = {
			save_scope_as = faction_target
		}

		# Let the human players in the faction know that the demand will be sent
		every_faction_member = {
			limit = {
				is_ai = no
				NOT = { this = scope:faction.faction_leader }
			}
			trigger_event = regula_faction_demand.0005
		}

		global_var:magister_character = { # Inform the player.
			trigger_event = regula_faction_demand.0006
		}

		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = regula_faction_demand.0001
				days = 5
			}
		}
	}

	ai_create_score = {
		base = -175 # Base reluctance value we must overcome to start an Independence Faction.

		############
		# BLOCKERS #

		# Refuses to make if there was a recent independence faction revolt.
		modifier = {
			add = -1000
			scope:target = {
				has_character_flag = recent_regula_servitude_faction_war
			}
		}
		# Can't fight this war if already serving the Magister.
		modifier = {
			add = -1000
			scope:target = {
				has_trait = magister_trait_group
				any_ally = {
					has_trait = magister_trait_group
				}
			}
		}

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -1000
			is_at_war = yes
		}

		# If a suitable Servitude Faction already exists, join it instead
		modifier = {
			add = -1000
			scope:target = {
				any_targeting_faction = {
	  				faction_is_type = regula_servitude_faction
				}
		  	}
		}

		#######################
		# Standard AI Weights #


		# Very high base create chance.
		modifier = {
			add = 1000
			has_trait = devoted_trait_group
		}

		# Can't create if title is higher than Magister's  
		modifier = {
			highest_held_title_tier >= global_var:magister_character.highest_held_title_tier
			add = -2000
		}

		# -150 create weight at +100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:target
			multiplier = -1.5
			max = 0
		}

		# for each character.religion != factionTarget.religion add X join-score
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = 25
			HOSTILE_BASE_VALUE = 50
			EVIL_BASE_VALUE = 100
			BASE_FAITH = faith
			TARGET_FAITH = scope:target.faith
		}

		# -100 join chance from intimidation, -1000 join chance from terrified
		intimidated_from_faction_modifier = {
			TARGET = scope:target
			POWER =  0
			THRESHOLD = 80
		}
	}

	ai_join_score = {
		base = -150 # Base reluctance value we must overcome to start an Independence Faction.

		############
		# BLOCKERS #

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -1000
			AND = {
				is_at_war = yes
				trigger_if = {
					limit = {
						exists = joined_faction
					}
					NOT = { joined_faction = scope:faction }
				}
			}
		}

		#######################
		# Standard AI Weights #

		# Very high base join chance.
		modifier = {
			add = 1000
			has_trait = devoted_trait_group
		}

		# Can't join if title is higher than Magister's  
		modifier = {
			highest_held_title_tier >= global_var:magister_character.highest_held_title_tier
			add = -2000
		}

		# Won't join if Magister is allied to target.
		modifier = {
			any_ally = {
				has_trait = magister_trait_group
			}
			add = -1000
		}

		# -150 join weight at +100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:faction.faction_target
			multiplier = -1.5
			max = 0
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:faction.faction_target
			POWER =  0
			THRESHOLD = 80
		}

		# for each character.religion != factionTarget.religion add X join-score
		pluralism_fundamentalism_modifier = {
			ASTRAY_BASE_VALUE = 25
			HOSTILE_BASE_VALUE = 50
			EVIL_BASE_VALUE = 100
			BASE_FAITH = faith
			TARGET_FAITH = scope:faction.faction_target.faith
		}


		# -100 join chance from intimidation, -1000 join chance from terrified
		intimidated_from_faction_modifier = {
			TARGET = scope:faction.faction_target
			POWER =  0
			THRESHOLD = 80
		}

		##########################
		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		modifier = {
			scope:faction.faction_power < scope:faction.faction_power_minimal
			liege = {
				any_targeting_faction = {
					OR = {
						faction_is_type = liberty_faction
						AND = {
							faction_is_type = claimant_faction
							special_character = {
								reverse_opinion = {
									target = root
									value >= -20
								}	
							}	
						}
						AND = {
							faction_is_type = populist_faction
							var:faction_faith = root.faith
						}
					}
					faction_power >= faction_power_minimal
				}
			}
			factor = faction_weight_factor_power_minimal_penalty
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_halfway_threshold
			factor = faction_weight_factor_power_halfway_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_pushing_threshold
			factor = faction_weight_factor_power_pushing_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_threshold
			factor = faction_weight_factor_power_exceeds_threshold
		}
	}

	ai_demand_chance = {
		base = 0

		# 40% base chance at minimum power (80%), increasing linearly
		compare_modifier = {
			value = faction_power
			multiplier = 0.5
		}

		# Once the faction has a good chance to win (10% stronger than liege) demand chance increases much more rapidly.
		compare_modifier = {
			trigger = {	faction_power > 110 }
			value = faction_power
			multiplier = 1
		}

		modifier = {
			add = 100
			faction_target = {
				is_at_war = yes # Independence Factions are opportunistic bastards!
			}
		}
	}

	can_character_join = {
		NOT = { is_allied_to = scope:faction.faction_target }
		scope:faction.faction_target = {
			NOT = { has_strong_hook = root }
		}

		has_trait = devoted_trait_group
		global_var:magister_character = {
			is_alive = yes
		}

		scope:faction.faction_target.highest_held_title_tier > tier_county

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	can_character_create = {
		NOR = { #No prince-bishop can ever join
			has_government = theocracy_government
			AND = {
				exists = liege.cp:councillor_court_chaplain
				this = liege.cp:councillor_court_chaplain
			}
		}
		liege = {
			is_independent_ruler = yes
			NOT = { has_trait = magister_trait_group }
		}
		AND = {
			exists = global_var:magister_character
			global_var:magister_character = { # Hilarious as a bunch of Paelices campaigning for a dead ruler is, it causes a few too many problems.
				is_alive = yes
			}
		}
		has_trait = devoted_trait_group
		NOT = { is_allied_to = scope:target }
		NOT = { has_truce = scope:target }
		scope:target = {
			NOT = { has_strong_hook = root }
		}

		scope:target.highest_held_title_tier > tier_county
		highest_held_title_tier > tier_barony

		####
		# BLOCKERS
		####
		# General Faction immunity
		custom_description = {
			text = character_is_immune_to_factions
			subject = scope:target
			NOT = { scope:target = { immune_to_factions_trigger = yes } }
		}

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}
	player_can_join = no
	inherit_membership = no
	county_allow_join = yes
	county_allow_create = yes
}