﻿# Faction started by the peasants of a county desiring to serve the Magister.
#
# Parallel but distinct from regula_servitude_faction which is started by
# a vassal of the target.
regula_peasant_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 = 11

	##############################
	### Basic Faction Settings ###
	##############################
	requires_county = yes
	requires_character = no
	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
		}

		# Peasant servitude faction can't coexist with a vassal servitude
		# faction. Vassal servitude factions always take precedence.
		faction_target = {
			NOT = {
				any_targeting_faction = {
					faction_is_type = regula_servitude_faction
				}
			}
		}
	}

	##############################
	###### 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 = no
	character_allow_join = yes
	county_allow_create = yes
	county_allow_join = yes

	can_character_create = {
		# Only counties can create peasant factions.
		always = no
	}

	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 = {
		# Faction must be valid.
		regula_faction_servitude_common_valid_trigger = {
			TARGET = scope:target
		}

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

	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 = {
		# Leader can only be the peasant leader.
		has_variable = peasant_leader_faction
		var:peasant_leader_faction = scope:faction
	}

	##############################
	## Member Behavior Scoring ###
	##############################
	ai_create_score = {
		# Characters can't create peasant factions.
		base = -2000
	}

	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 = {
		# Base reluctance value we must overcome to create a servitude faction.
		base = 0

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

		###################
		# 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
			}
		}
	}

	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
	}

	leader_leaves = {
		# If the peasant leader leaves in a peasant faction, disband the faction.
		# Should only trigger when the leader is captured or killed in battle.
		if = {
			limit = {
				special_character = {
					is_alive = no
				}
			}
			faction_war = {
				end_war = defender
			}
			destroy_faction = yes
		}
		if = {
			limit = {
				NOT = { has_variable = peasant_destroying }
				exists = faction_war
			}
			faction_war = {
				end_war = defender
			}
		}
	}
}

# Faction started by a regula heresy conspiracy desiring independence from the
# magister.
regula_heresy_faction = {
	name = regula_heresy_faction
	desc = regula_heresy_faction_desc
	short_effect_desc = regula_heresy_faction_short_effect_desc
	special_character_title = regula_heresy_faction_popular_leader

	casus_belli = regula_heresy_faction_war
	sort_order = 11

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

	discontent_progress = {
		base = 0

		# Discontent grows by the number of conspiracy members each tick.
		modifier = {
			add = {
				value = faction_target.regula_heresy_conspiracy_num_members
				multiply = regula_heresy_discontent_per_conspirator
			}
			desc = regula_heresy_faction_discontent_desc
		}
	}

	power_threshold = {
		# Heresy factions will always place their demand, regardless of power.
		# They've already committed heresy, what is left to lose?
		base = 0
	}

	is_valid = {
		faction_target = global_var:magister_character
		faction_target = {
			regula_heresy_ongoing_trigger = yes
		}
	}

	##############################
	###### Member Validity #######
	##############################
	is_character_valid = {
		regula_faction_heresy_valid_character_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	is_county_valid = {
		regula_faction_heresy_valid_county_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

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

	can_character_create = {
		# Always created by county member.
		always = no
	}

	can_character_join = {
		regula_faction_heresy_valid_character_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	can_county_create = {
		regula_faction_heresy_valid_county_trigger = {
			TARGET = scope:target
		}
	}

	can_county_join = {
		regula_faction_heresy_valid_county_trigger = {
			TARGET = scope:faction.faction_target
		}
	}

	can_character_become_leader = {
		# Normally, anyone can be leader.
		always = yes

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

		# Leader can only be landed or peasant leader during war.
		trigger_if = {
			limit = {
				exists = scope:faction.faction_war
			}

			OR = {
				is_landed = yes
				AND = {
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = scope:faction
				}
			}
		}
	}

	##############################
	## Member Behavior Scoring ###
	##############################
	ai_create_score = {
		# AI should never create this dynamically.
		base = -1000
	}

	ai_join_score = {
		# AI should never join this dynamically.
		base = -1000

		# But stay in if you are in the faction.
		modifier = {
			add = 5000
			joined_faction ?= {
				regula_faction_is_heresy_faction_trigger = yes
			}
		}
	}

	county_create_score = {
		# AI should never create this dynamically.
		base = -1000
	}

	county_join_score = {
		# AI should never join this dynamically.
		base = -1000

		# But stay in if you are in the faction.
		modifier = {
			add = 5000
			any_title_joined_faction = {
				regula_faction_is_heresy_faction_trigger = yes
			}
		}
	}

	ai_demand_chance = {
		# Heresy factions are likely to enforce demands shortly after reaching
		# max discontent.
		base = 70
	}

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

	on_destroy = {
		save_scope_as = faction
		regula_faction_heresy_peasant_leader_cleanup_effect = yes
		regula_heresy_faction_teardown = yes
	}

	demand = {
		save_scope_as = faction
		regula_faction_heresy_demand_effect = yes
	}

	on_war_start = {
		save_scope_as = faction
		regula_faction_heresy_spawn_troops_effect = yes
	}

	leader_leaves = {
		# If we are at war, and there are no more valid leaders, the faction is
		# defeated.
		save_scope_as = faction
		if = {
			limit = {
				exists = faction_war
				NOT = {
					any_faction_member = {
						OR = {
							is_landed = yes
							AND = {
								has_variable = peasant_leader_faction
								var:peasant_leader_faction = scope:faction
							}
						}
					}
				}
				NOT = {
					has_variable = peasant_destroying
				}
			}

			faction_war = {
				end_war = defender
			}
		}
	}
}
