﻿# Interaction Costs
# Piety costs for interactions
regula_fascinare_cost = 150
regula_fascinare_rebate = 50
regula_mutare_corpus_interaction_piety_cost = 500
regula_mutare_corpus_cosmetic_interaction_piety_cost = 100
regula_mutare_corpus_interaction_childbirth_cost_negative = -250
regula_mutare_corpus_interaction_piety_cost_negative = -500

regula_sanctifica_serva_interaction_piety_cost = 750
regula_sanctifica_serva_interaction_prestige_cost = 500

regula_religious_proclamation_piety_cost = 10000
regula_change_birth_rate_piety_cost = 1000
regula_change_harem_tenet_piety_cost = 2500

regula_infecta_virus_min_days_event = 15
regula_infecta_virus_max_days_event = 45

# Prisoner charming costs.
regula_prisoner_charm_cost = {
	value = regula_fascinare_cost
	divide = 2
}
regula_prisoner_charm_cost_negated = {
	subtract = regula_prisoner_charm_cost
}

regula_prisoner_lover_charm_cost = {
	value = regula_fascinare_cost
	multiply = 3
	divide = 4
}
regula_prisoner_lover_charm_cost_negated = {
	subtract = regula_prisoner_lover_charm_cost
}

regula_prisoner_lover_charm_devoted_cost_negated = {
	subtract = regula_prisoner_lover_charm_cost
	add = regula_prisoner_charm_cost
}

regula_divorce_spouse_cost = {
	value = 100

	# If a ruler, add extra cost
	scope:recipient = {
		if = {
			limit = { is_ruler = yes }
			if = {
				limit = { primary_title.tier = tier_county }
				add = {
					value = 50
					desc = regula_beguile_cost_primary_title
				}
			}
			if = {
				limit = { primary_title.tier = tier_duchy }
				add = {
					value = 150
					desc = regula_beguile_cost_primary_title
				}
			}
			if = {
				limit = { primary_title.tier = tier_kingdom }
				add = {
					value = 400
					desc = regula_beguile_cost_primary_title
				}
			}
			if = {
				limit = { primary_title.tier = tier_empire }
				add = {
					value = 900
					desc = regula_beguile_cost_primary_title
				}
			}
		}
	}
}

# Computes the amount of piety required for the absorb vitality interaction.
#
# Scopes (All Required):
# scope:recipient = the target of the exhaurire vitale interaction.
regula_exhaurire_vitale_absorb_vitality_piety_cost = {
	value = 5000
	scope:recipient = {
		if = {
			limit = {
				has_trait = paelex
			}
			value = 4000
		}
		else_if = {
			limit = {
				is_regula_leader_devoted_trigger = yes
			}
			value = 3000
		}
	}
}

# Computes the amount of piety required for the absorb vitality interaction.
#
# Scopes (All Required):
# scope:actor = the actor running the exhaurire vitale interaction.
regula_exhaurire_vitale_absorb_vitality_piety_level_cost = {
	value = 1
	scope:actor = {
		if = {
			limit = {
				has_variable = absorb_vitality_piety_level_cost
			}
			value = var:absorb_vitality_piety_level_cost
			floor = yes
		}
	}
}

# Computes the amount of piety required for the absorb essence interaction.
#
# Scopes (All Required):
# scope:recipient = the target of the exhaurire vitale interaction.
regula_exhaurire_vitale_absorb_essence_piety_cost = {
	value = 3000
	scope:recipient = {
		if = {
			limit = {
				has_trait = paelex
			}
			value = 2000
		}
		else_if = {
			limit = {
				is_regula_leader_devoted_trigger = yes
			}
			value = 1500
		}
	}
}
regula_exhaurire_vitale_absorb_essence_piety_experience_cost = 1000

# Computes the amount of prestige required for the beguile interaction.
#
# Scopes (All Required):
# scope:recipient = the target of the beguile interaction.
# The cost is the primary title cost of the recipient
# We then half it if the recipient is charmed
regula_beguile_interaction_prestige_cost = {
	value = 0
	scope:recipient = {
		primary_title = {
			add = {
				value = regula_title_cost
				desc = regula_beguile_cost_primary_title
			}
		}

		if = {
			limit = {
				is_regula_devoted_trigger = yes
			}
			divide = {
				value = 2
				desc = regula_beguile_cost_devoted
			}
		}
	}
}

# Sums up all relevant skills.
regula_character_total_skills = {
	value = 0
	add = diplomacy
	add = martial
	add = stewardship
	add = intrigue
	add = learning
}

# Counts the number of powerful vassals that are crypto-famuli.
regula_num_covert_powerful_vassals = {
	value = 0
	every_vassal = {
		limit = {
			is_powerful_vassal_of = root
			any_secret = {
				secret_type = regula_covert_conversion
			}
		}
		add = 1
	}
}

# Counts number of children
regula_num_children = {
	value = 0
	every_child = {
		add = 1
	}
}

# Counts number of sons (in same dynasty!)
regula_num_sons_same_dynasty = {
	value = 0
	every_child = {
		limit = {
			dynasty = prev.dynasty	# We check against the dynasty of the character that got this value
			is_male = yes
		}
		add = 1
	}
}

# Also counts dead children ;_;
regula_num_children_plus_dead = {
	value = 0
	every_child = {
		even_if_dead = yes
		add = 1
	}
}

# Num of children divided by 1.5
regula_num_children_divided_1_5 = {
	value = 0
	every_child = {
		add = 1
	}
	divide = 1.5
}

# Num of children divided by 2
regula_num_children_divided_2 = {
	value = 0
	every_child = {
		add = 1
	}
	divide = 2
}

# Num of children divided by 3
regula_num_children_divided_3 = {
	value = 0
	every_child = {
		add = 1
	}
	divide = 3
}

# Num of children divided by 4
regula_num_children_divided_4 = {
	value = 0
	every_child = {
		add = 1
	}
	divide = 4
}

# Num of children divided by 5
regula_num_children_divided_5 = {
	value = 0
	every_child = {
		add = 1
	}
	divide = 5
}

# Counts the number of pregnant wives and concubines
regula_num_pregnant_consorts = {
	value = 0
	# Add wives and concubines
	every_consort = {
		limit = {
			is_pregnant = yes
		}
		add = 1
	}
}

# Calculates the number of "leader" spouses. Catches domina/familia paelex too.
# Used for some triggers.
regula_num_leader_spouses = {
	value = 0
	every_spouse = {
		limit = {
			is_regula_leader_devoted_trigger = yes
		}
		add = 1
	}
}

# Counts the number of "leader" spouses that have a kingdom title
regula_num_regina_paelex = {
	value = 0
	every_spouse = {
		limit = {
			is_regula_leader_devoted_trigger = yes
			highest_held_title_tier = tier_kingdom
		}
		add = 1
	}
	max = 5
}

# Counts the number of "leader" spouses that have a Duchy title
regula_num_ducissa_paelex = {
	value = 0
	every_spouse = {
		limit = {
			is_regula_leader_devoted_trigger = yes
			highest_held_title_tier = tier_duchy
		}
		add = 1
	}
	max = 10
}

# Counts the number of "leader" spouses that have a county title
regula_num_comitissa_paelex = {
	value = 0
	every_spouse = {
		limit = {
			is_regula_leader_devoted_trigger = yes
			highest_held_title_tier = tier_county
		}
		add = 1
	}
	max = 25
}

# Calculates the number of unlanded spouses.
# Note, having a Family Title or Adventurer Camp counts.
# Normally, A Paelex can only be created if they have a county tier title or above, but we allow barony tier Paelices without adding to total.
# Used to compute penalty for unlanded Paelex tier spouses.
# Applies `regula_paelex_unlanded_penalty` modifier.
regula_num_unlanded_spouses = {
	value = 0
	every_spouse = {
		limit = {
			highest_held_title_tier <= tier_barony # If a Paelex somehow gets downgraded to a barony, we allow it
			AND = {
				is_regula_leader_devoted_trigger = yes
				NOT = { has_trait = domina } # Domina does not have to be landed
			}
		}
		add = 1
	}
}

# Calculates the number of tropaeum concubines the magister has.
regula_num_tropaeum = {
	value = 0
	every_concubine = {
		limit = {
			has_trait = tropaeum
		}
		add = 1
	}
}

# Calculates the number of Bloodline traits this character has
regula_num_bloodline_traits = {
	value = 0

	if = {
		limit = { has_trait = regula_fascinare_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_domitans_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_obedience_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_contubernalis_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_domination_war_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_servitude_war_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_potestas_queen_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_multitasker_bloodline }
		add = 1
	}

	if = {
		limit = { has_trait = regula_bun_bloodline }
		add = 1
	}

}

# Calculates the power of the contubernalis the magister has. No bonus from hollowing out members of own dynasty.
regula_contubernalis_renown = {
	value = 0
	every_concubine = {
		limit = {
			has_trait = contubernalis
			NOT = { dynast = global_var:magister_character }
		}
		add = 1

		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 1 }
			}
			add = 1
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 2 }
			}
			add = 2
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 3 }
			}
			add = 3
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 4 }
			}
			add = 3
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 5 }
			}
			add = 4
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 6 }
			}
			add = 5
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 7 }
			}
			add = 7
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 8 }
			}
			add = 10
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 9 }
			}
			add = 15
		}
		
		if = {
			limit = {
				dynasty = { dynasty_prestige_level = 10 }
			}
			add = 20
		}
		
	}
}



# Calculates the number of spouses in your harem multiplied by 15
regula_num_harem_times_15 = {
	value = 0
	every_consort = {
		limit = {
			is_regula_leader_devoted_trigger = yes
		}
		add = 15
	}
}

# Adds the learning skills of your entire harem
regula_harem_learning_skill = {
	value = 0
	every_consort = {
		limit = {
			is_regula_leader_devoted_trigger = yes
		}
		add = learning
	}
}

regula_potestas_non_transfunde_cost = {
	value = 0
	every_held_title = {
		add = regula_title_cost
	}
	every_vassal_or_below = { # Vassals count for half.
		limit = {
			highest_held_title_tier = tier_duchy
		}
		add = 300
	}
	every_vassal_or_below = {
		limit = {
			highest_held_title_tier = tier_county
		}
		add = 150
	}

	if = {
		limit = {
			tier_difference = {
				target = scope:actor
				value = -2
			}
		}
		multiply = 0.5
	}
	if = {
		limit = {
			tier_difference = {
				target = scope:actor
				value = -3
			}
		}
		multiply = 0.25
	}
	if = {
		limit = {
			AND = {
				character_is_realm_neighbor = scope:actor
				NOT = {
					character_is_land_realm_neighbor = scope:actor
				}
			}
		}
		multiply = 1.25 # A little pricier if they're not nearby.
	}
	if = {
		limit = {
			NOT = {
				character_is_realm_neighbor = scope:actor
				character_is_land_realm_neighbor = scope:actor
			}
		}
		multiply = 2 # Much pricier if they arent near you
	}


	# if = {   ### UPDATE - has_trait triggers register as false, regardless of validity. Not restricted to devoted traits or recipient.
	# 	limit = {
	#				is_regula_leader_devoted_trigger = yes
	# 	}
	# 	multiply = 0.5 # You're also convincing the smallfolk and the minor lords. Having the target on your side isn't everything.
	# }
	#

	# We get to spend less depending on our piety level
	multiply = regula_piety_level_reduction_multiplier
}

regula_abice_maritus_cost = {
	value = 0
	scope:recipient.primary_spouse = {
		every_held_title = {
			add = regula_title_cost
		}
		every_vassal_or_below = { # Vassals count for half.
			limit = {
				highest_held_title_tier = tier_duchy
			}
			add = 300
		}
		every_vassal_or_below = {
			limit = {
				highest_held_title_tier = tier_county
			}
			add = 150
		}

		if = {
			limit = {
				tier_difference = {
					target = scope:actor
					value = -2
				}
			}
			multiply = 0.5
		}
		if = {
			limit = {
				tier_difference = {
					target = scope:actor
					value = -3
				}
			}
			multiply = 0.25
		}
		if = {
			limit = {
				AND = {
					character_is_realm_neighbor = scope:actor
					NOT = {
						character_is_land_realm_neighbor = scope:actor
					}
				}
			}
			multiply = 1.25 # A little pricier if they're not nearby.
		}
	}

	# We get to spend less depending on our piety level
	multiply = regula_piety_level_reduction_multiplier

	if = {
		limit = {
			global_var:magister_character.faith = {
				controls_holy_site_with_flag = 	regula_abice_maritus_active
			}
		}
		multiply = 0.5
	}
}

regula_domination_war_cost = {
	value = 0
	scope:defender = {
		every_held_title = {
			add = regula_title_cost
		}
		every_vassal_or_below = { # Vassals count for half.
			limit = {
				highest_held_title_tier = tier_duchy
			}
			add = 300
		}
		every_vassal_or_below = {
			limit = {
				highest_held_title_tier = tier_county
			}
			add = 150
		}

		if = {
			limit = {
				tier_difference = {
					target = scope:attacker
					value = -2
				}
			}
			multiply = 0.5
		}
		if = {
			limit = {
				tier_difference = {
					target = scope:attacker
					value = -3
				}
			}
			multiply = 0.25
		}
		if = {
			limit = {
				AND = {
					character_is_realm_neighbor = scope:attacker
					NOT = {
						character_is_land_realm_neighbor = scope:attacker
					}
				}
			}
			multiply = 1.25 # A little pricier if they're not nearby.
		}
	}

	# We get to spend less depending on our piety level
	multiply = regula_piety_level_reduction_multiplier

	if = {
		limit = {
			global_var:magister_character.faith = {
				controls_holy_site_with_flag = 	regula_abice_maritus_active
			}
		}
		multiply = 0.5
	}

	# As this is a domination war, it costs less
	multiply = 0.5
}

regula_titulum_novis_cost = {
	value = 0

	# Add the piety cost of titles below the landed_title, eg any counties below duchy or duchys + counties below kingdom
	every_in_de_jure_hierarchy = {
		if = {
			limit = {
				holder = scope:recipient
			}
			add = regula_title_cost
		}
	}

	# If target is retired paelex, we set cost to 0
	if = {
		limit = {
			scope:recipient = {
				has_trait = retired_paelex
			}
		}
		max = 0
	}


	# We get to spend less depending on our piety level
	#multiply = regula_piety_level_reduction_multiplier

}
###################################################################################################
# Retire Famuli costs
# Base cost is decided by physical stats
# While discounts apply to the base cost, reducing it by a percentage for each type of trigger
# Expects scope:actor and scope:recipient
###################################################################################################
# Base cost, decided by physical stats
	# Age
	# Health
	# Congential Traits
	# Physical Traits
regula_famuli_retire_base_cost = {
	value = 0

	add = {
		value = 100
		desc = regula_retire_cost_base_desc
	}

	scope:recipient = {
		# Age
			if = {
				limit = {
					age >= 60
				}
				subtract = {
					value = 100
					desc = regula_retire_cost_old_desc
				}
			}
			else_if = {
				limit = {
					age < 60
					age >= 45
				}
				subtract = {
					value = 50
					desc = regula_retire_cost_aged_desc
				}
			}
			else_if = {
				limit = {
					age < 45
					age >= 30
				}
				add = {
					value = 50
					desc = regula_retire_cost_middle_aged_desc
				}
			}
			else_if = {
				limit = {
					age < 30
					age >= 20
				}
				add = {
					value = 150
					desc = regula_retire_cost_young_desc
				}
			}
			else_if = {
				limit = {
					age < 20
				}
				add = {
					value = 400
					desc = regula_retire_cost_very_young_desc
				}
			}
		# Health
			if = {
				limit = {
					health < 0
				}
				subtract = {
					value = 1000
					desc = regula_retire_cost_bad_health_desc
				}
			}
			else_if = {
				limit = {
					health < 1
					health >= 0
				}
				subtract = {
					value = 500
					desc = regula_retire_cost_bad_health_desc
				}
			}
			else_if = {
				limit = {
					health < 3
					health >= 1
				}
				subtract = {
					value = 250
					desc = regula_retire_cost_bad_health_desc
				}
			}
			else_if = {
				limit = {
					health < 5
					health >= 3
				}
				add = {
					value = 50
					desc = regula_retire_cost_good_health_desc
				}
			}
			else_if = {
				limit = {
					health < 7
					health >= 5
				}
				add = {
					value = 150
					desc = regula_retire_cost_good_health_desc
				}
			}
			else_if = {
				limit = {
					health >= 7
				}
				add = {
					value = 250
					desc = regula_retire_cost_good_health_desc
				}
			}
		# Congential Traits
		# Physical Traits
			if = {
				limit = {
					OR = {
						has_trait = incapable
						has_trait = infirm
						has_trait = lunatic_1
						has_trait = lunatic_genetic
						has_trait = possessed_1
						has_trait = possessed_genetic
					}
				}
				subtract = {
					value = 500
					desc = regula_retire_cost_incapable_desc
				}
			}
	}

	# The base cost can never be below 0
	min = 0
}

# Discount multiplier
	# Sinful/Virtuous Traits
	# Other Traits
	# Relationship
	# Primary Heir
regula_famuli_retire_cost_discount_cost = {
	value = 0

	scope:recipient = {
		# Sinful/Virtuous Traits - 5% per trait
		if = {
			limit = {
				num_sinful_traits >= 1
			}
			add = {
				value = regula_famuli_retire_base_cost
				if = {
					limit = { scope:revoke_titles = yes }
					add = regula_retire_famuli_revoke_titles_cost
				}
				multiply = num_sinful_traits
				multiply = 0.05
				desc = regula_retire_discount_sinful_desc
			}
		}
		if = {
			limit = {
				num_virtuous_traits >= 1
			}
			subtract = {
				value = regula_famuli_retire_base_cost
				if = {
					limit = { scope:revoke_titles = yes }
					add = regula_retire_famuli_revoke_titles_cost
				}
				multiply = num_virtuous_traits
				multiply = 0.05
				desc = regula_retire_discount_virtuous_desc
			}
		}
		# Other Traits
		# Relationship
		# Primary Heir - is heir a healthy adult?
		if = {
			limit = {
				primary_heir = {
					is_adult = yes
					is_healthy = yes
				}
			}
			subtract = {
				value = regula_famuli_retire_base_cost
				if = {
					limit = { scope:revoke_titles = yes }
					add = regula_retire_famuli_revoke_titles_cost
				}
				multiply = 0.05
				desc = regula_retire_discount_primary_heir_desc
			}
		}

		# If Leader Devoted (Domina/Paelex), reduce cost by 25%
		if = {
			limit = {
				is_regula_leader_devoted_trigger = yes
			}
			subtract = {
				value = regula_famuli_retire_base_cost
				if = {
					limit = { scope:revoke_titles = yes }
					add = regula_retire_famuli_revoke_titles_cost
				}
				multiply = 0.25
				desc = regula_retire_discount_devoted_desc
			}
		}

		# If Orba/Retired Paelex, no cost
		if = {
			limit = {
				OR = {
					has_trait = orba
					has_trait = retired_paelex
				}
			}
			subtract = {
				value = regula_famuli_retire_base_cost
				if = {
					limit = { scope:revoke_titles = yes }
					add = regula_retire_famuli_revoke_titles_cost
				}
				desc = regula_retire_discount_retire_desc
			}
		}
	}
}

regula_retire_famuli_revoke_titles_cost = {
	value = 0

	scope:recipient = {
		add = {
			value = 0
			every_held_title = {
				add = regula_title_cost
			}
			multiply = 0.5
			desc = regula_retire_cost_title_desc
		}
	}
}

###################################################################################################

regula_docere_cultura_characters_cost = {
	value = 0
	# Costs 10 prestiege per character
	# Compile a list of everyone who will flip to the new culture.
	# Check the character that is getting the request to change culture
	if = {
		limit = {
			NOT = { culture = global_var:magister_character.culture }
		}
		add = 10
	}

	# Check our courtiers
	every_courtier = {
		limit = {
			NOT = { culture = global_var:magister_character.culture }
		}
		add = 10
	}

	# Check each vassal (individualy)
	every_vassal_or_below = {
		limit = {
			NOT = { culture = global_var:magister_character.culture }
		}
		add = 10
	}

	# Then check the courtiers of each vassal
	every_vassal_or_below = {
		every_courtier = {
			limit = {
				NOT = { culture = global_var:magister_character.culture }
			}
			add = 10
		}
	}
}

regula_docere_cultura_titles_cost = {
	value = 0

	# Add the cost for each county that has a different culture to the Magister
	every_sub_realm_county = {
		if = {
			limit = {
				tier = tier_county
				NOT = { culture = global_var:magister_character.culture }
			}
			add = regula_title_cost	# As these are counties it should always be the county base cost
		}
	}
}

# Helper method for validation check
regula_docere_cultura_total_cost = {
	value = 0
	add = regula_docere_cultura_characters_cost
	add = regula_docere_cultura_titles_cost
}

# Get cost of title
regula_title_cost = {
	value = 0
	if = {
		limit = {
			tier = 1
		}
		add = 50
	}
	if = {
		limit = {
			tier = 2
		}
		add = 100
	}
	if = {
		limit = {
			tier = 3
		}
		add = 300
	}
	if = {
		limit = {
			tier = 4
		}
		add = 600
	}
	if = {
		limit = {
			tier = 5
		}
		add = 1200
	}
}

# Depending on our piety level, we have a multiplier that reduces costs slightly
regula_piety_level_reduction_multiplier = {
	value = 1 # 100 percent

	# Poenitens (level 0) get an increase of 10%
	# Cenobite (level 1) get no reduction
	# Zelator (Level 2) get 10% reduction
	# Skeuophylax (level 3) get 20% reduction
	# Synkellos (level 4) get 35% reduction
	# Exarch (level 5) get 50& reduction

	if = {
		limit = {
			global_var:magister_character = {
				piety_level = 0
			}
		}
		multiply = 1.1
	}

	if = {
		limit = {
			global_var:magister_character = {
				piety_level = 2
			}
		}
		multiply = 0.9
	}

	if = {
		limit = {
			global_var:magister_character = {
				piety_level = 3
			}
		}
		multiply = 0.8
	}

	if = {
		limit = {
			global_var:magister_character = {
				piety_level = 4
			}
		}
		multiply = 0.65
	}

	if = {
		limit = {
			global_var:magister_character = {
				piety_level = 5
			}
		}
		multiply = 0.5
	}
}

# Counts up number of baronies Holy Orders of the magistrar's variant of regula has
regula_num_holy_order_counties = {
	value = 0
	global_var:magister_character.faith = {
		ordered_faith_holy_order = {
			every_leased_title = {
				limit = {
					is_under_holy_order_lease = yes
					tier = tier_barony
				}
				add = 1
			}
		}
	}
}

# Cost of corrupting this holy order, uses the leader (lessee) as scope
# Currently is number of baronies in your lands (that you can revoke)
# scope:barony.lessee
regula_corrupt_holy_order_cost = {
	value = 0
	save_temporary_scope_as = leader
	faith = {
		ordered_faith_holy_order = {
			limit = {
				leader = scope:leader
			}
			every_leased_title = {
				limit = {
					OR = {
						holder.top_liege = global_var:magister_character
						holder = global_var:magister_character
					}
					is_under_holy_order_lease = yes
					has_revokable_lease = yes
					tier = tier_barony
				}
				add = 1
			}
		}
	}

	# multiply by corruption value
	# 500 is amount we use in Vanilla to revoke the lease of a Holy Order
	multiply = 500
}

regula_claim_initiates_prestige_cost = {
	value = 0
	add = 150
	if = {
		limit = {
			regula_num_holy_order_counties >= 15
		}
		add = 150
	}
	if = {
		limit = {
			regula_num_holy_order_counties >= 30
		}
		add = 200
	}
}

# Returns the dynasty prestige you get from enslaving a ward who either has a title or is the primary heir to a title
# TODO, how do we find out the highest title a character is first in line for?
# Scope should be ward character
regula_ward_highest_title_to_dynasty_prestige = {
	value = 0
	# They are a ward that already has a primary title of this rank
	if = {
		limit = {
			primary_title.tier = 1
		}
		add = 25
	}
	if = {
		limit = {
			primary_title.tier = 2
		}
		add = 100
	}
	if = {
		limit = {
			primary_title.tier = 3
		}
		add = 300
	}
	if = {
		limit = {
			primary_title.tier = 4
		}
		add = 750
	}
	if = {
		limit = {
			primary_title.tier = 5
		}
		add = 1250
	}
}

# Returns a number that is the chance trigger a Regula raid event
# Starts at 20% then plus bonus based on Magisters trait rank
# Then plus 10% for having an Enslaver, and plus 10% for having Famuli Enslavers
# Also add player martial
regula_magister_raiding_bonus = {
	value = 15

	if = {
		# All of these checks require the Magister to exist
		limit = {
			exists = global_var:magister_character
		}

		# Add Magister Martial and Prowess skills
		add = {
			value = global_var:magister_character.martial
			divide = 2
		}

		add = {
			value = global_var:magister_character.prowess
			divide = 3
		}

		# Add bonus for magister piety level
		if = {
			limit = {
				global_var:magister_character = {
					piety_level = 0
				}
			}
			add = -20
		}

		if = {
			limit = {
				global_var:magister_character = {
					piety_level = 3
				}
			}
			add = 5
		}

		if = {
			limit = {
				global_var:magister_character = {
					piety_level = 4
				}
			}
			add = 10
		}

		if = {
			limit = {
				global_var:magister_character = {
					piety_level = 5
				}
			}
			add = 15
		}

		# Add bonus for having raid/combat related traits
		if = {
			limit = {
				global_var:magister_character = {
					OR = {
						has_trait = reaver
						has_trait = viking			# Despite being named "Viking", anyone can get this via raiding a lot
						has_trait = berserker
						has_trait = shieldmaiden
						has_trait = varangian
					}
				}
			}
			add = 5
		}

		# Do we have an Enslaver knight?
		# If so, add 10% extra
		if = {
			limit = {
				global_var:magister_character = {
					any_active_accolade = {
						has_accolade_parameter = accolade_regula_enslaver_increase_raid_chance
					}
				}
			}
			add = 10
		}

		# Do we have the Famuli Enslavers tradition?
		# If so, add 10% extra
		if = {
			limit = {
				global_var:magister_character.culture = {
					has_cultural_tradition = tradition_famuli_enslavers
				}
			}
			add = 10
		}

		# Do we have a bonus from modifier?
		if = {
			limit = {
				global_var:magister_character = {
					has_character_modifier = regula_enslavement_bonus_chance
				}
			}
			add = 25
		}
	}
	else = {
		# If there is no Magister, just return a 0%
		max = 0
	}

	# Max chance of a raid event is 100%!
	max = 100
}

# Base tyranny gain for turning a character into a contubernalis, without
# trait, religion, and age adjustments.
#
# scope = the character creating a contubernalis.
# scope:actor = same as scope.
# scope:recipient = the character becoming a contubernalis.
regula_base_contubernalis_tyranny_gain = {
	# Used in `execution_tyranny_gain`
	save_temporary_scope_as = victim
	value = execution_tyranny_gain

	if = {
		limit = {
			NOR = {
				has_banish_reason = scope:recipient
				has_imprisonment_reason = scope:recipient
			}
		}
		multiply = 2
	}
}

# Tyranny gain for turning a character into a contubernalis, including
# trait, religion, and age adjustments.
#
# scope = the character creating a contubernalis.
# scope:actor = same as scope.
# scope:recipient = the character becoming a contubernalis.
regula_contubernalis_tyranny_gain = {
	value = regula_base_contubernalis_tyranny_gain

	# Add factors which increase tyranny.
	add = {
		value = 0

		# Double up on devoted leaders - you are rending soul of
		# outwardly most devoted characters.
		if = {
			limit = {
				scope:recipient = {
					is_regula_leader_devoted_trigger = yes
				}
			}
			add = regula_base_contubernalis_tyranny_gain
		}

		# Rending someone who is visibly pregnant is pretty bad
		if = {
			limit = {
				scope:recipient = {
					has_trait = pregnant	# Only if visible pregnant, so check trait
				}
			}
			add = {
				value = regula_base_contubernalis_tyranny_gain
				multiply = 0.5
			}
		}

		# Virtuous traits increase the percieved tyranny.
		if = {
			limit = {
				scope:recipient = {
					num_virtuous_traits = {
						value > 0
						target = scope:actor.faith
					}
				}
			}
			add = {
				value = regula_base_contubernalis_tyranny_gain
				multiply = 0.2
			}
		}

		# Rending soul of a younger person looks worse.
		if = {
			limit = {
				scope:recipient = {
					age <= 30
				}
			}
			add = {
				value = regula_base_contubernalis_tyranny_gain
				multiply = 0.1
			}
		}
	}

	# Subtract factors which decrease tyranny.
	subtract = {
		value = 0

		# Non regula faith makes it a bit more understandable.
		if = {
			limit = {
				scope:recipient = {
					is_regula_trigger = no
				}
			}
			add = {
				value = regula_base_contubernalis_tyranny_gain
				multiply = 0.15
			}
		}

		# Sinners may deserve to be contubernalis.
		if = {
			limit = {
				scope:recipient = {
					num_sinful_traits = {
						value > 0
						target = scope:actor.faith
					}
				}
			}
			add = {
				value = regula_base_contubernalis_tyranny_gain
				multiply = 0.4
			}
		}

		# Rending the soul of an elderly person is a blessing...right?
		if = {
			limit = {
				scope:recipient = {
					age >= 60
				}
			}
			add = {
				value = regula_base_contubernalis_tyranny_gain
				multiply = 0.2
			}
		}

		# If sacrifice is a tenet this causes very little tyranny.
		if = {
			limit = {
				scope:actor.faith = {
					has_doctrine_parameter = contubernalis_realm_benefits
				}
			}
			add = {
				value = regula_base_contubernalis_tyranny_gain
				multiply = 0.8
			}
		}
	}

	min = 0
	round = yes
}
