﻿# Faction started by a vassal of a county desiring to serve the Magister.
#
# Parallel but distinct from regula_peasant_servitude_faction which is started
# by the peasants of a county where the target is top liege.
regula_servitude_faction = {
	name = regula_servitude_faction
	desc = regula_servitude_faction_desc
	short_effect_desc = regula_servitude_faction_short_effect_desc
	special_character_title = regula_servitude_faction_peasant_leader

	casus_belli = regula_servitude_faction_war
	sort_order = 10

	##############################
	### Basic Faction Settings ###
	##############################
	requires_county = no
	requires_character = yes
	requires_leader = no
	inherit_membership = no
	leaders_allowed_to_leave = no
	multiple_targeting = no
	county_can_switch_to_other_faction = no

	county_power = county_levies_to_raise

	discontent_progress = {
		base = 0

		# Discontent defaults to same as vanilla: Entirely faction power based.
		# Though with lower power thresholds and some base discontent gain.
		common_discontent_progress_modifier = yes

		# Regula modifiers
		regula_faction_servitude_discontent_modifier = yes
	}

	power_threshold = {
		base = 70

		# Common & Regula Modifiers
		regula_faction_servitude_power_threshold_modifier = yes
	}

	is_valid = {
		save_temporary_scope_as  = faction

		# Faction must be valid.
		regula_faction_servitude_common_valid_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	##############################
	###### Member Validity #######
	##############################
	is_character_valid = {
		# Faction must be valid.
		regula_faction_servitude_common_valid_trigger = {
			TARGET = scope:faction.faction_target
		}

		# Character must be allowed to be in a servitude faction.
		regula_faction_servitude_valid_character_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	is_county_valid = {
		# Faction must be valid.
		regula_faction_servitude_common_valid_trigger = {
			TARGET = scope:faction.faction_target
		}

		# County must be allowed to be in a servitude faction.
		regula_faction_servitude_valid_county_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	##############################
	##### Member Join Rules ######
	##############################
	player_can_join = yes
	character_allow_create = yes
	character_allow_join = yes
	county_allow_create = no
	county_allow_join = yes

	can_character_create = {
		# Faction must be valid.
		regula_faction_servitude_common_valid_trigger = {
			TARGET = scope:target
		}

		# Character must be allowed to be in a servitude faction.
		regula_faction_servitude_valid_character_trigger = {
			TARGET = scope:target
		}

		# Character must be allowed to create factions.
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
	}

	can_character_join = {
		# Faction must be valid.
		regula_faction_servitude_common_valid_trigger = {
			TARGET = scope:faction.faction_target
		}

		# Character must be allowed to be in a servitude faction.
		regula_faction_servitude_valid_character_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	can_county_create = {
		# Only characters can create vassal factions.
		always = no
	}

	can_county_join = {
		# Faction must be valid.
		regula_faction_servitude_common_valid_trigger = {
			TARGET = scope:faction.faction_target
		}

		# County must be allowed to be in a servitude faction.
		regula_faction_servitude_valid_county_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	can_character_become_leader = {
		# Any vassal is a valid leader.
		liege = scope:faction.faction_target
	}

	##############################
	## Member Behavior Scoring ###
	##############################
	ai_create_score = {
		# Base reluctance value we must overcome to create a servitude faction.
		base = 0

		# Base faction favorability for character.
		regula_faction_servitude_character_faction_score = {
			TARGET = scope:target
			FACTION_POWER = 0
			FACTION_POWER_THRESHOLD = 70
		}

		###################
		# CREATE BLOCKERS #
		###################
		# If a suitable servitude faction against the target already exists side with it instead.
		modifier = {
			add = -2000
			scope:target = {
				any_targeting_faction = {
					regula_faction_is_servitude_faction_trigger = yes
				}
			}
		}

		# Refuses to make if there was a recent servitude faction revolt.
		modifier = {
			add = -2000
			scope:target = {
				has_character_flag = recent_regula_servitude_faction_war
			}
		}
	}

	ai_join_score = {
		# Base reluctance value we must overcome to join a servitude faction.
		base = 0

		# Base faction favorability for character.
		regula_faction_servitude_character_faction_score = {
			TARGET = scope:faction.faction_target
			FACTION_POWER = scope:faction.faction_power
			FACTION_POWER_THRESHOLD = scope:faction.faction_power_threshold
		}

		##############################
		# Faction 'Stacking' Factors #
		##############################
		# Attempts to cluster faction members into several powerful factions instead
		# of many weak ones.
		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
		}
	}

	county_create_score = {
		# Counties can't create vassal factions.
		base = -2000
	}

	county_join_score = {
		# Base reluctance value we must overcome to join a servitude faction.
		base = 0

		# Base faction favorability for county.
		regula_faction_servitude_county_faction_score = {
			TARGET = scope:faction.faction_target
		}

		##############################
		# Faction 'Stacking' Factors #
		##############################
		# Attempts to cluster faction members into several powerful factions instead
		# of many weak ones.
		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
		regula_faction_servitude_demand_score = yes
	}

	##############################
	###### Faction Effects #######
	##############################
	on_creation = {
		save_scope_as = faction
		regula_faction_servitude_creation_notify_effect = yes
	}

	on_destroy = {
		save_scope_as = faction
		regula_faction_servitude_peasant_leader_cleanup_effect = yes
	}

	demand = {
		save_scope_as = faction
		regula_faction_servitude_demand_effect = yes
	}

	on_war_start = {
		save_scope_as = faction
		regula_faction_servitude_spawn_troops_effect = yes
	}
}
