﻿#############################
# Servitude Faction Effects #
#############################
# Effect which sends a notification to the Magister when a servitude faction
# is created.
#
# scope:faction = the faction in question
regula_faction_servitude_creation_notify_effect = {
	global_var:magister_character ?= {
		send_interface_message = {
			type = regula_servitude_faction_created
			title = regula_servitude_faction_created_title
			desc = regula_servitude_faction_created_desc
			right_icon = scope:faction.faction_target
		}
	}
}

# Effect which sends a notification to the Magister when a servitude faction
# declares war.
#
# scope:attacker = the attacker in the servitude war
# scope:defender = the defender in the servitude war
regula_faction_servitude_war_declared_notify_effect = {
	if = {
		limit = {
			scope:attacker = {
				regula_faction_is_servitude_faction_leader = yes
			}
		}
		scope:attacker = {
			save_scope_as = servitude_leader
		}
		scope:defender = {
			save_scope_as = servitude_target
		}
	}
	else = {
		scope:attacker = {
			save_scope_as = servitude_target
		}
		scope:defender = {
			save_scope_as = servitude_leader
		}
	}

	global_var:magister_character ?= {
		send_interface_message = {
			type = regula_servitude_war_declared
			title = regula_servitude_faction_declared_war_title
			desc = regula_servitude_faction_declared_war_desc
			left_icon = scope:servitude_leader
			right_icon = scope:servitude_target
		}
	}
}

# Initializes a peasant leader as the leader for any county members of a
# servitude faction. Assumes that we have already verified that at least
# one county member exists.
#
# scope:faction = the faction to potentially create a leader for.
regula_faction_servitude_peasant_leader_setup_effect = {
	scope:faction = {
		random_faction_county_member = {
			save_scope_as = peasant_county
		}
	}

	create_character = {
		save_scope_as = peasant_leader
		template = regula_servitude_peasant_leader

		location = scope:peasant_county.title_province
		culture = scope:peasant_county.culture
		faith = scope:peasant_county.faith
	}

	# Create a title for the peasant leader.
	create_dynamic_title = {
		tier = duchy
		name = regula_servitude_revolt_title_name
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	scope:new_title = {
		set_capital_county = scope:peasant_county

		set_landless_title = yes
		set_destroy_on_succession = yes
		set_delete_on_destroy = yes
		set_no_automatic_claims = yes
		set_definitive_form = yes
		set_can_be_named_after_dynasty = no

		change_title_holder = {
			holder = scope:peasant_leader
			change = scope:change
			government_base = scope:peasant_county.holder
		}

		set_variable = {
			name = faction
			value = scope:faction
		}

		# Avoid unused variable error.
		var:faction = {}
	}
	resolve_title_and_vassal_change = scope:change

	scope:peasant_leader = {
		set_variable = {
			name = peasant_title
			value = scope:new_title
		}
	}
	scope:new_title = { generate_coa = factions }

	scope:peasant_leader = {
		set_variable = {
			name = peasant_leader_faction
			value = scope:faction
		}
		join_faction_skip_check = scope:faction
	}

	scope:faction = {
		set_special_character = scope:peasant_leader
	}
}

# Cleans up content related to a peasant faction leader if said leader exists.
#
# scope:faction = the faction to potentially cleanup a leader for
regula_faction_servitude_peasant_leader_cleanup_effect = {
	scope:faction = {
		set_variable = {
			name = peasant_destroying
			value = yes
		}

		if = {
			limit = {
				exists = special_character
				special_character = {
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = scope:faction
				}
			}

			special_character = {
				if = { # Destroy temp title.
					limit = {
						has_variable = peasant_title
					}
					destroy_title = var:peasant_title
				}

				if = {
					limit = {
						# To prevent mutations on dead actors.
						is_alive = yes
					}

					if = {
						limit = {
							has_variable = peasant_leader_faction
						}
						remove_variable = peasant_leader_faction
					}

					if = {
						limit = {
							has_variable = peasant_title
						}
						remove_variable = peasant_title
					}

					if = {
						limit = { # To make sure we aren't in debt
							gold > 0
							is_landed = no
						}

						# Zero out our wallet since the revolt is over and
						# we're still not landed.
						remove_long_term_gold = gold
					}

					if = {
						limit = {
							NOT = {
								has_character_flag = peasant_revolt_do_not_kill
							}
						}

						death = {
							death_reason = death_vanished
						}
					}
				}
			}
		}
	}
}

# Spawns peasant troops for all faction county members, and adds commanders
# if the faction leader is a peasant leader.
#
# scope:faction = the faction for which we are spawning troops
regula_faction_servitude_spawn_troops_effect = {
	scope:faction = {
		if = {
			limit = {
				exists = special_character
				any_faction_county_member = {
					exists = this
				}
				special_character = {
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = scope:faction
				}
			}

			# Peasant Leader Joins War
			faction_war = {
				if = {
					limit = {
						NOT = {
							is_participant = scope:faction.special_character
						}
					}

					if = {
						limit = {
							is_attacker = scope:faction.faction_leader
						}
						add_attacker = scope:faction.special_character
					}
					else = {
						add_defender = scope:faction.special_character
					}
				}
			}

			# Spawn peasant troops
			faction_spawn_member_county_armies_effect = {
				FACTION = scope:faction
				ARMY_OWNER = scope:faction.special_character
				PEASANT_ARMY_NAME = regula_servitude_faction_event_troops
			}

			# Give the peasant leader a commander for each member county
			every_faction_county_member = {
				save_temporary_scope_as = tmp_faction_member_county

				create_character = {
					template = new_commander_character

					gender = scope:faction.special_character

					location = scope:tmp_faction_member_county.title_province
					faith = scope:faction.special_character.faith
					culture = scope:faction.special_character.culture

					dynasty = none

					save_scope_as = new_servitude_commander
				}

				scope:new_servitude_commander = {
					set_employer = scope:faction.special_character
				}

				# Give the peasant leader a small purse for each county member
				# so that they don't go bankrupt due to peasant forces.
				scope:faction.special_character = {
					add_gold = 10
				}
			}
		}
		else = {
			# Peasant leader doesn't exist - just give the faction leader
			# troops.
			faction_spawn_member_county_armies_effect = {
				FACTION = scope:faction
				ARMY_OWNER = scope:faction.faction_leader
				PEASANT_ARMY_NAME = regula_servitude_faction_event_troops
			}

			# Give the faction leader a small purse for each county member so
			# that they don't go bankrupt due to peasant forces.
			every_faction_county_member = {
				scope:faction.faction_leader = {
					add_gold = 10
				}
			}
		}
	}
}

# Effect encapsulating all logic which should be performed when a regula
# servitude faction sends it's demands.
#
# scope:faction = the faction sending the demand
regula_faction_servitude_demand_effect = {
	scope:faction = {
		# If we have any county members, create the peasant leader, but don't
		# create if the peasant leader (special character) already exists.
		if = {
			limit = {
				any_faction_county_member = {
					exists = this
				}
				NOT = { exists = special_character }
			}
			regula_faction_servitude_peasant_leader_setup_effect = yes
		}

		faction_leader = {
			save_scope_as = faction_leader
		}

		faction_target = {
			save_scope_as = faction_target
		}

		global_var:magister_character = { # Allow the player to veto the demand.
			trigger_event = regula_faction_demand.0006
		}
	}
}

# Applies all liege and title ownership changes resulting from a successful
# regula servitude faction.
#
# FACTION = the faction to apply effects for.
# LEADER = the faction leader
# TARGET = the faction target
regula_faction_servitude_faction_success_effect = {
	create_title_and_vassal_change = {
		type = faction_demand
		save_scope_as = change
		add_claim_on_loss = no
	}

	$FACTION$ = {
		# Change owner of all of the county faction members.
		if = { # Peasant leader alive - they claim counties.
			limit = {
				exists = special_character
				special_character = {
					is_alive = yes
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = $FACTION$
				}
			}

			special_character = {
				add_character_flag = peasant_revolt_do_not_kill
			}

			every_faction_county_member = {
				change_title_holder = {
					holder = $FACTION$.special_character
					change = scope:change
				}
			}
		}
		else = { # Peasant leader missing - give titles directly to magister.
			every_faction_county_member = {
				change_title_holder = {
					holder = global_var:magister_character
					change = scope:change
				}
			}
		}

		# Change liege of all of the character faction members.
		every_faction_member = {
			if = { # Peasant leader needs to be handled separately.
				limit = {
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = $FACTION$
				}

				show_as_tooltip = {
					change_liege = {
						liege = global_var:magister_character
						change = scope:change
					}
				}

				# Set up government change parameter.
				if = {
					limit = {
						$TARGET$ = { government_has_flag = government_is_tribal }
					}

					set_variable = {
						name = change_government_tribal
						value = yes
					}
				}
				else_if = {
					limit = {
						$TARGET$ = { government_has_flag = government_is_clan }
					}

					set_variable = {
						name = change_government_clan
						value = yes
					}
				}

				trigger_event = {
					id = regula_faction_demand.0007
					days = 1
				}
			}
			else = {
				change_liege = {
					liege = global_var:magister_character
					change = scope:change
				}
			}
		}
	}

	resolve_title_and_vassal_change = scope:change

	# Increment the servitude war counter
	change_global_variable = {
		name = regula_servitude_war_tally
		add = 1
	}

	global_var:magister_character = {
		# Let the Magister know the war is won.
		send_interface_message = {
			type = regula_servitude_war_won
			title = regula_servitude_faction_victory_notification.t
			desc = regula_servitude_faction_victory_notification_desc
			right_icon = $LEADER$
			left_icon = $TARGET$

			add_prestige = major_prestige_value
		}
	}
}

##########################
# Heresy Faction Effects #
##########################

# Initializes a peasant leader as the leader for any county members of a
# heresy faction.
#
# This will be the faction leader if the faction leader is unlanded, and
# otherwise, will be an unlanded faction member at random.
#
# scope = the faction to potentially create a leader for.
# scope:faction = same as above
regula_faction_heresy_peasant_leader_setup_effect = {
	if = {
		limit = {
			any_faction_county_member = {
				holder = {
					regula_heresy_conspirator_character_trigger = no
				}
			}
			any_faction_member = {
				is_landed = no
			}
			NOT = { exists = special_character }
		}

		random_faction_county_member = {
			limit = {
				holder = {
					regula_heresy_conspirator_character_trigger = no
				}
			}

			save_scope_as = peasant_county
		}

		if = {
			limit = {
				faction_leader = {
					is_landed = no
				}
			}

			faction_leader = {
				save_scope_as = peasant_leader
			}
		}
		else = {
			random_faction_member = {
				limit = {
					is_landed = no
				}
				save_scope_as = peasant_leader
			}
		}

		# Create a title for the peasant leader.
		create_dynamic_title = {
			tier = duchy
			name = regula_heresy_revolt_title_name
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}

		scope:new_title = {
			set_capital_county = scope:peasant_county

			set_landless_title = yes
			set_destroy_on_succession = yes
			set_delete_on_destroy = yes
			set_no_automatic_claims = yes
			set_definitive_form = yes
			set_can_be_named_after_dynasty = no

			change_title_holder = {
				holder = scope:peasant_leader
				change = scope:change
				government_base = scope:peasant_county.holder
			}

			set_variable = {
				name = faction
				value = scope:faction
			}

			# Avoid unused variable error.
			var:faction = {}
		}
		resolve_title_and_vassal_change = scope:change

		scope:peasant_leader = {
			set_variable = {
				name = peasant_title
				value = scope:new_title
			}
		}

		scope:new_title = { generate_coa = factions }

		scope:peasant_leader = {
			set_variable = {
				name = peasant_leader_faction
				value = scope:faction
			}
		}

		set_special_character = scope:peasant_leader
	}
}

# Cleans up content related to a peasant faction leader if said leader exists.
#
# scope:faction = the faction to potentially cleanup a leader for
regula_faction_heresy_peasant_leader_cleanup_effect = {
	scope:faction = {
		set_variable = {
			name = peasant_destroying
			value = yes
		}

		if = {
			limit = {
				exists = special_character
				special_character = {
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = scope:faction
				}
			}

			special_character = {
				if = { # Destroy temp title.
					limit = {
						has_variable = peasant_title
					}
					destroy_title = var:peasant_title
				}

				if = {
					limit = {
						# To prevent mutations on dead actors.
						is_alive = yes
					}

					if = {
						limit = {
							has_variable = peasant_leader_faction
						}
						remove_variable = peasant_leader_faction
					}

					if = {
						limit = {
							has_variable = peasant_title
						}
						remove_variable = peasant_title
					}

					if = {
						limit = { # To make sure we aren't in debt
							gold > 0
							is_landed = no
						}

						# Zero out our wallet since the revolt is over and
						# we're still not landed.
						remove_long_term_gold = gold
					}
				}
			}
		}
	}
}

# Spawns peasant troops for all faction county members, and adds commanders
# if the faction leader is a peasant leader.
#
# scope:faction = the faction for which we are spawning troops
regula_faction_heresy_spawn_troops_effect = {
	scope:faction = {
		if = {
			limit = {
				exists = special_character
				any_faction_county_member = {
					holder = {
						regula_heresy_conspirator_character_trigger = no
					}
				}
				special_character = {
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = scope:faction
				}
			}

			# Peasant Leader Joins War
			faction_war = {
				if = {
					limit = {
						NOT = {
							is_participant = scope:faction.special_character
						}
					}

					if = {
						limit = {
							is_attacker = scope:faction.faction_leader
						}
						add_attacker = scope:faction.special_character
					}
					else = {
						add_defender = scope:faction.special_character
					}
				}
			}

			# Give the peasant leader a commander for each member county
			every_faction_county_member = {
				save_temporary_scope_as = tmp_faction_member_county

				limit = {
					holder = {
						regula_heresy_conspirator_character_trigger = no
					}
				}

				# Spawn peasant troops
				regula_faction_heresy_spawn_individual_county_troops_effect = {
					FACTION = scope:faction
					ARMY_OWNER = scope:faction.special_character
					PEASANT_ARMY_NAME = regula_heresy_faction_event_troops
				}

				create_character = {
					template = new_commander_character

					gender = scope:faction.special_character

					location = scope:tmp_faction_member_county.title_province
					faith = scope:faction.special_character.faith
					culture = scope:faction.special_character.culture

					dynasty = none

					save_scope_as = new_heresy_commander
				}

				scope:new_heresy_commander = {
					set_employer = scope:faction.special_character
				}

				# Give the peasant leader a small purse for each county member
				# so that they don't go bankrupt due to peasant forces.
				scope:faction.special_character = {
					add_gold = 10
				}
			}
		}
		else = {
			every_faction_county_member = {
				limit = {
					holder = {
						regula_heresy_conspirator_character_trigger = no
					}
				}

				# Peasant leader doesn't exist - just give the faction leader
				# troops.
				regula_faction_heresy_spawn_individual_county_troops_effect = {
					FACTION = scope:faction
					ARMY_OWNER = scope:faction.faction_leader
					PEASANT_ARMY_NAME = regula_heresy_faction_event_troops
				}

				# Give the faction leader a small purse for each county member so
				# that they don't go bankrupt due to peasant forces.
				scope:faction.faction_leader = {
					add_gold = 10
				}
			}
		}
	}
}

regula_faction_heresy_spawn_individual_county_troops_effect = {
	save_temporary_scope_as = county

	# Reduce county control in faction member counties
	change_county_control = peasant_war_starts_county_control_loss

	if = {
		limit = {
			regula_heresy_potential_heresy_county_directly_trigger = yes
		}

		# Directly heretical counties spawn more troops than normal.
		$ARMY_OWNER$ = {
			spawn_army = {
				levies = {
					value = scope:county.county_levies_to_raise
					multiply = regula_heresy_direct_heresy_troop_scale
				}
				location = scope:county.title_province
				war = $FACTION$.faction_war
				name = $PEASANT_ARMY_NAME$
			}
		}
	}
	else = {
		# Indirectly heretical counties spawn slightly fewer than normal
		# troops.
		$ARMY_OWNER$ = {
			spawn_army = {
				levies = {
					value = scope:county.county_levies_to_raise
					multiply = regula_heresy_indirect_heresy_troop_scale
				}
				location = scope:county.title_province
				war = $FACTION$.faction_war
				name = $PEASANT_ARMY_NAME$
			}
		}
	}
}

# Effect encapsulating all logic which should be performed when a regula
# heresy faction sends it's demands.
#
# scope:faction = the faction sending the demand
regula_faction_heresy_demand_effect = {
	scope:faction = {
		regula_faction_heresy_peasant_leader_setup_effect = yes

		faction_leader = {
			save_scope_as = faction_leader
		}

		faction_target = {
			save_scope_as = faction_target
			trigger_event = regula_faction_demand.0101
		}
	}
}

# Applies all liege and title ownership changes resulting from a successful
# regula heresy faction.
#
# Specifically, if alive the peasant leader gains all faction county members
# not held by landed faction members and then becomes independent.
#
# If the peasant leader is not present, then the county members are distributed
# among the landed faction members (preferring bordering sub-realms & same
# faith).
#
# Then, all remaining unlanded conspirators in the faction join the court of
# their preferred landed (or newly landed) faction members.
#
# FACTION = the faction to apply effects for.
# LEADER = the faction leader
# TARGET = the faction target
regula_faction_heresy_faction_success_effect = {
	create_title_and_vassal_change = {
		type = faction_demand
		save_scope_as = change
		add_claim_on_loss = no
	}

	$FACTION$ = {
		# Change owner of all of the county faction members.
		if = { # Peasant leader alive - they claim county members.
			limit = {
				exists = special_character
				special_character = {
					is_alive = yes
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = $FACTION$
				}
			}

			every_faction_county_member = {
				limit = {
					holder = {
						regula_heresy_conspirator_character_trigger = no
					}
				}

				change_title_holder = {
					holder = $FACTION$.special_character
					change = scope:change
				}

				regula_heresy_force_convert_county_effect = {
					CONVERTER = $FACTION$.special_character
				}
			}
		}
		else = { # Peasant leader missing - give titles directly to a landed faction member.
			every_faction_county_member = {
				limit = {
					holder = {
						regula_heresy_conspirator_character_trigger = no
					}
				}

				save_temporary_scope_as = county_to_rehome

				$FACTION$ = {
					random_faction_member = {
						limit = {
							is_landed = yes
						}

						weight = {
							base = 1

							# Prefer clustering counties.
							modifier = {
								factor = 1000
								any_sub_realm_county = {
									any_neighboring_county = {
										this = scope:county_to_rehome
									}
								}
							}

							# Otherwise, prefer matching county faith with
							# liege.
							modifier = {
								factor = 10
								faith = scope:county_to_rehome.faith
							}
						}

						change_title_holder = {
							holder = $LEADER$
							change = scope:change
						}

						save_temporary_scope_as = new_title_owner
						regula_heresy_force_convert_county_effect = {
							CONVERTER = scope:new_title_owner
						}
					}
				}
			}
		}

		# Change liege of all of the character faction members.
		every_faction_member = {
			if = { # Peasant Leader Gains County Members
				limit = {
					has_variable = peasant_leader_faction
					var:peasant_leader_faction = $FACTION$
				}

				show_as_tooltip = {
					becomes_independent = {
						change = scope:change
					}
				}

				# Set up government change parameter.
				if = {
					limit = {
						$TARGET$ = { government_has_flag = government_is_tribal }
					}

					set_variable = {
						name = change_government_tribal
						value = yes
					}
				}
				else_if = {
					limit = {
						$TARGET$ = { government_has_flag = government_is_clan }
					}

					set_variable = {
						name = change_government_clan
						value = yes
					}
				}

				trigger_event = {
					id = regula_faction_demand.0110
					days = 1
				}
			}
			else_if = { # Landed Members Become Independent
				limit = {
					is_landed = yes
				}
				becomes_independent = {
					change = scope:change
				}

				# All counties in sub realm are converted.
				every_sub_realm_county = {
					regula_heresy_force_convert_county_effect = {
						CONVERTER = scope:current_faction_member
					}
				}

				# All vassal or below and courtiers are converted.
				every_vassal_or_below = {
					regula_heresy_force_convert_character_effect = {
						CONVERTER = scope:current_faction_member
					}
				}
				every_courtier = {
					regula_heresy_force_convert_character_effect = {
						CONVERTER = scope:current_faction_member
					}
				}
			}
			else = { # Remaining landless members move to their preferred court.
				save_temporary_scope_as = member_to_rehome
				$FACTION$ = {
					random_faction_member = {
						limit = {
							OR = {
								is_landed = yes
								AND = {
									has_variable = peasant_leader_faction
									var:peasant_leader_faction = $FACTION$
								}
							}
						}

						weight = {
							base = 1

							modifier = {
								factor = 10
								faith = scope:member_to_rehome.faith
							}
						}

						add_courtier = scope:member_to_rehome
					}
				}
			}
		}
	}

	resolve_title_and_vassal_change = scope:change

	$FACTION$ = {
		regula_heresy_transition_to_faction_victory = yes
	}
}

# Applies all faction & opinion changes resultant from the white peace outcome
# of a heresy faction.
#
# FACTION = the faction to apply effects for.
# TARGET = the faction target
regula_faction_heresy_faction_white_peace_effect = {
	$FACTION$ ?= {
		regula_heresy_transition_to_faction_white_peace = yes

		save_scope_as = current_faction
		every_faction_county_member = {
			if = {
				limit = {
					holder = {
						regula_heresy_conspirator_character_trigger = no
					}
					is_regula_trigger = yes
					any_title_to_title_neighboring_and_across_water_county = {
						regula_heresy_potential_heresy_county_directly_trigger = yes
					}
				}

				random_title_to_title_neighboring_and_across_water_county = {
					limit = {
						regula_heresy_potential_heresy_county_directly_trigger = yes
					}
					save_temporary_scope_as = belief_county
				}
				regula_heresy_force_convert_county_effect = {
					CONVERTER = scope:belief_county
				}
			}
		}

		every_faction_member = {
			save_temporary_scope_as = current_faction_member

			if = {
				limit = {
					exists = scope:current_faction # Can get destroyed as we loop through
				}
				leave_faction_with_cooldown_effect = {
					FACTION = scope:current_faction
					YEARS = faction_war_white_peace_cooldown
				}
			}
			else = {
				add_faction_cooldown_effect = { YEARS = faction_war_white_peace_cooldown }
			}

			if = {
				limit = {
					is_landed = yes
				}

				# All directly held counties are converted.
				every_sub_realm_county = {
					limit = {
						holder = scope:current_faction_member
					}

					regula_heresy_force_convert_county_effect = {
						CONVERTER = scope:current_faction_member
					}
				}

				# All direct vassals and courtiers are converted.
				every_vassal = {
					regula_heresy_force_convert_character_effect = {
						CONVERTER = scope:current_faction_member
					}
				}
				every_courtier = {
					regula_heresy_force_convert_character_effect = {
						CONVERTER = scope:current_faction_member
					}
				}
			}

			# In a white peace, unlanded courtiers and guests in the magister's
			# court will probably want to leave.
			regula_heresy_conspirator_leave_court_effect = yes

			$TARGET$ = {
				add_opinion = {
					target = prev
					modifier = regula_heresy_conspirator_opinion
				}
			}
		}

		scope:current_faction ?= { add_faction_discontent = -200 }
	}
}

# Applies all faction & opinion changes resultant from the defeat outcome
# of a heresy faction.
#
# FACTION = the faction to apply effects for.
# TARGET = the faction target
regula_faction_heresy_faction_defeat_effect = {
	if = {
		limit = {
			exists = $FACTION$
		}

		$FACTION$ = {
			set_variable = {
				name = peasant_destroying
				value = yes
			}

			regula_heresy_transition_to_faction_defeat = yes

			every_faction_member = {
				if = {
					limit = {
						exists = $FACTION$ # Can get destroyed as we loop through
					}

					leave_faction_with_cooldown_effect = {
						FACTION = $FACTION$
						YEARS = faction_war_defeat_cooldown
					}
				}
				else = {
					add_faction_cooldown_effect = { YEARS = faction_war_defeat_cooldown }
				}

				save_temporary_scope_as = faction_member
				regula_heresy_imprison_target_effect = {
					TARGET = scope:faction_member
					IMPRISONER = $TARGET$
				}
			}
		}
	}
}
