﻿# Effect which allows for a generically defined exhaurire vitale effect with
# default weights.
#
# Scopes (All Required):
# global_var:magister_character = The magister.
# scope:actor = The character doing the interaction - should be magister.
# scope:recipient = The character recieving the interaction.
#
# Parameters (All Required)
# BACKFIRE = The event to be fired on backfire
# BAD = The event to be fired on bad outcome
# GOOD = The event to be fired on good outcome
# GREAT = The event to be fired on great outcome
# FANTASTIC = The event to be fired on fantastic outcome
regula_exhaurire_vitale_generic_effect = {
	# Five possible outcomes:
	# Backfire, Bad, Good, Great, Fantastic
	# Use Piety level to modify chance of each effect
	random_list = {
		0 = { # Backfire
			modifier = {
				global_var:magister_character = {
					piety_level = 0
				}
				add = 30
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 1
				}
				add = 10
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = 5
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_backfire }
			trigger_event = $BACKFIRE$
		}
		0 = { # Bad
			modifier = {
				global_var:magister_character = {
					piety_level = 0
				}
				add = 50
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 1
				}
				add = 20
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = 10
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 3
				}
				add = 5
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_bad }
			trigger_event = $BAD$
		}
		0 = { # Good
			modifier = {
				global_var:magister_character = {
					piety_level = 0
				}
				add = 20
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 1
				}
				add = 60
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = 50
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 3
				}
				add = 40
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_good }
			trigger_event = $GOOD$
		}
		0 = { # Great
			modifier = {
				global_var:magister_character = {
					piety_level = 1
				}
				add = 10
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = 30
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 3
				}
				add = 40
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 4
				}
				add = 60
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 5
				}
				add = 20
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_great }
			trigger_event = $GREAT$
		}
		0 = { # Fantastic
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = 5
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 3
				}
				add = 15
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 4
				}
				add = 40
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 5
				}
				add = 80
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_fantastic }
			trigger_event = $FANTASTIC$
		}
	}
}

regula_exhaurire_vitale_drain_piety_effect = {

	# Three possible outcomes
	# Bad, Good and Great

	# Use Piety level to modify chance of each effect
	random_list = {
		25 = {
			# Bonus based on piety level
			modifier = {
				global_var:magister_character = {
					piety_level = 0
				}
				add = 25
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 1
				}
				add = 15
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = 5
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 3
				}
				add = -15
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 4
				}
				add = -20
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 5
				}
				add = -25
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_bad }

			trigger_event = {
				id = regula_exhaurire_vitale_event.0011
			}
		}
		60 = {
			# Bonus based on piety level
			modifier = {
				global_var:magister_character = {
					piety_level = 0
				}
				add = -25
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 1
				}
				add = -10
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = 5
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 3
				}
				add = 15
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 4
				}
				add = 20
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 5
				}
				add = 25
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_good }

			trigger_event = {
				id = regula_exhaurire_vitale_event.0012
			}
		}
		25 = {
			# Bonus based on piety level
			modifier = {
				global_var:magister_character = {
					piety_level = 0
				}
				add = -50
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 1
				}
				add = -25
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 2
				}
				add = -10
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 3
				}
				add = 10
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 4
				}
				add = 15
			}
			modifier = {
				global_var:magister_character = {
					piety_level = 5
				}
				add = 20
			}

			# Add learning stat as bonus for great
			modifier = {
				add = { value = global_var:magister_character.learning }
			}

			custom_description_no_bullet = { text = regula_exhaurire_vitale_outcome_great }

			trigger_event = {
				id = regula_exhaurire_vitale_event.0013
			}
		}
	}
}

regula_exhaurire_vitale_absorb_vitality_effect = {
	regula_exhaurire_vitale_generic_effect = {
		BACKFIRE = regula_exhaurire_vitale_event.0021
		BAD = regula_exhaurire_vitale_event.0022
		GOOD = regula_exhaurire_vitale_event.0023
		GREAT = regula_exhaurire_vitale_event.0024
		FANTASTIC = regula_exhaurire_vitale_event.0025
	}
}

regula_exhaurire_vitale_absorb_essence_effect = {
	regula_exhaurire_vitale_generic_effect = {
		BACKFIRE = regula_exhaurire_vitale_event.0031
		BAD = regula_exhaurire_vitale_event.0032
		GOOD = regula_exhaurire_vitale_event.0033
		GREAT = regula_exhaurire_vitale_event.0034
		FANTASTIC = regula_exhaurire_vitale_event.0035
	}
}

# Helper effects for Exhaurire Vitale
# exhaurire_vitale_drain_vitality_cost_update			- Updates the piety level cost of drain vitality.
# exhaurire_vitale_drain_vitality_effect				- Drains vitality (youth) from a target.
# exhaurire_vitale_drain_single_fixed_trait_effect		- Drains a single given unleveled trait.
# exhaurire_vitale_drain_single_leveled_trait_effect	- Drains a level of a single given trait.
# exhaurire_vitale_drain_intellect_trait_effect			- Helper wrapper to drain related intellect traits.
# exhaurire_vitale_drain_physique_trait_effect			- Helper wrapper to drain related physique traits.
# exhaurire_vitale_drain_any_trait_effect				- Drains a level of an unspecified trait.

# Updates the piety level cost of the drain vitality to new cost after
# current execution.
#
# scope = the actor to update the cost for.
exhaurire_vitale_drain_vitality_cost_update = {
	if = {
		limit = {
			has_variable = absorb_vitality_piety_level_cost
		}

		set_variable = {
			name = absorb_vitality_piety_level_cost
			value = {
				value = var:absorb_vitality_piety_level_cost
				add = 1
				max = 4
			}
		}
	}
	else = {
		set_variable = {
			name = absorb_vitality_piety_level_cost
			value = 2
		}
	}
}

# Applies an effect draining a configurable amount of `vitality` from a given target.
#
# Only applies age drains & health penalties. No wounds, death chances, or other effects.
#
# ACTOR = the character doing the vitality draining.
# TARGET = the character being drained.
# STACKS = the stacks of vitality drained applied - 10 years per stack.
exhaurire_vitale_drain_vitality_effect = {
	save_temporary_scope_value_as = {
		name = years_drained
		value = {
			value = 10
			multiply = $STACKS$
		}
	}

	custom_description = {
		text = regula_exhaurire_vitale_absorb_vitality_effect
		subject = $ACTOR$
		object = $TARGET$
		value = scope:years_drained

		while = {
			count = $STACKS$

			$TARGET$ = {
				add_character_modifier = regula_exhaurire_vitale_vitality_malus
				change_current_weight = -30
			}
			$ACTOR$ = {
				add_character_modifier = regula_exhaurire_vitale_vitality_bonus
			}
		}
	}
}

# Applies an effect draining a given TRAIT from the given TARGET to given
# ACTOR. Specifically, this applies for fixed / unleveled traits.
#
# Does not apply validity checks - those should be applied before selecting the
# given TRAIT.
#
# Parameters (All Required):
# ACTOR = the character doing the trait draining.
# TARGET = the character being drained.
# TRAIT = the trait or trait group being drained.
exhaurire_vitale_drain_single_fixed_trait_effect = {
	$ACTOR$ = {
		add_trait = $TRAIT$
	}

	$TARGET$ = {
		remove_trait = $TRAIT$
	}
}

# Applies an effect draining a given TRAIT from the given TARGET to given
# ACTOR. Specifically, this applies for leveled traits.
#
# Does not apply validity checks - those should be applied before selecting the
# given TRAIT.
#
# Parameters (All Required):
# ACTOR = the character doing the trait draining.
# TARGET = the character being drained.
# TRAIT = the trait or trait group being drained.
exhaurire_vitale_drain_single_leveled_trait_effect = {
	$ACTOR$ = {
		change_trait_rank = {
			trait = $TRAIT$
			rank = 1
		}
	}

	$TARGET$ = {
		change_trait_rank = {
			trait = $TRAIT$
			rank = -1
		}
	}
}

# Applies an effect draining a valid intellect trait from the given TARGET to
# given ACTOR.
#
# The trait drained will be selected at random from amongst the valid traits.
#
# Parameters:
# ACTOR = the character doing the trait draining.
# TARGET = the character being drained.
exhaurire_vitale_drain_intellect_trait_effect = {
	if = { # Remove negatives before giving any positive
		limit = {
			$ACTOR$ = {
				OR = {
					has_trait = intellect_bad
					has_trait = dull
				}
			}
		}

		# De-level a random negative intellect trait.
		$ACTOR$ = {
			random_list = {
				1 = {
					trigger = {
						has_trait = dull
					}
					remove_trait = dull
				}
				1 = {
					trigger = {
						has_trait = intellect_bad
					}
					change_trait_rank = {
						trait = intellect_bad
						rank = -1
					}
				}
			}
		}

		# De-level a random positive intellect trait from the target.
		$TARGET$ = {
			random_list = {
				1 = {
					trigger = {
						has_trait = shrewd
					}
					remove_trait = shrewd
				}
				1 = {
					trigger = {
						has_trait = intellect_good
					}
					change_trait_rank = {
						trait = intellect_good
						rank = -1
					}
				}
			}
		}
	}
	else = {
		random_list = {
			1 = {
				trigger = {
					exhaurire_vitale_can_drain_fixed_trait = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = shrewd
					}
				}

				exhaurire_vitale_drain_single_fixed_trait_effect = {
					ACTOR = $ACTOR$
					TARGET = $TARGET$
					TRAIT = shrewd
				}
			}
			1 = {
				trigger = {
					exhaurire_vitale_can_drain_leveled_trait = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = intellect_good
					}
				}

				exhaurire_vitale_drain_single_leveled_trait_effect = {
					ACTOR = $ACTOR$
					TARGET = $TARGET$
					TRAIT = intellect_good
				}
			}
		}
	}
}

# Applies an effect draining a valid physique trait from the given TARGET to
# given ACTOR.
#
# The trait drained will be selected at random from amongst the valid traits.
#
# Parameters:
# ACTOR = the character doing the trait draining.
# TARGET = the character being drained.
exhaurire_vitale_drain_physique_trait_effect = {
	if = { # Remove negatives before giving any positive
		limit = {
			$ACTOR$ = {
				OR = {
					has_trait = physique_bad
					has_trait = weak
					has_trait = spindly
				}
			}
		}

		# De-level a random negative physique trait.
		$ACTOR$ = {
			random_list = {
				1 = {
					trigger = {
						has_trait = weak
					}
					remove_trait = weak
				}
				1 = {
					trigger = {
						has_trait = spindly
					}
					remove_trait = spindly
				}
				1 = {
					trigger = {
						has_trait = physique_bad
					}
					change_trait_rank = {
						trait = physique_bad
						rank = -1
					}
				}
			}
		}

		# De-level a random positive physique trait from the target.
		$TARGET$ = {
			random_list = {
				1 = {
					trigger = {
						has_trait = strong
					}
					remove_trait = strong
				}
				1 = {
					trigger = {
						has_trait = physique_good
					}
					change_trait_rank = {
						trait = physique_good
						rank = -1
					}
				}
			}
		}
	}
	else = {
		random_list = {
			1 = {
				trigger = {
					exhaurire_vitale_can_drain_fixed_trait = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = strong
					}
				}

				exhaurire_vitale_drain_single_fixed_trait_effect = {
					ACTOR = $ACTOR$
					TARGET = $TARGET$
					TRAIT = strong
				}
			}
			1 = {
				trigger = {
					exhaurire_vitale_can_drain_leveled_trait = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = physique_good
					}
				}

				exhaurire_vitale_drain_single_leveled_trait_effect = {
					ACTOR = $ACTOR$
					TARGET = $TARGET$
					TRAIT = physique_good
				}
			}
		}
	}
}

# Applies an effect draining a valid trait from the given TARGET to given
# ACTOR.
#
# The trait drained will be selected at random from amongst the valid traits.
#
# Note: List here should be kept in sync with parallel list in
# regula_exhaurire_vitale_triggers.txt.
#
# Parameters:
# ACTOR = the character doing the trait draining.
# TARGET = the character being drained.
exhaurire_vitale_drain_any_trait_effect = {
	custom_description = {
		text = regula_exhaurire_vitale_absorb_essence_drain_effect
		subject = $ACTOR$
		object = $TARGET$

		random_list = {
			1 = { # pure_blooded
				trigger = {
					exhaurire_vitale_can_drain_fixed_trait = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = pure_blooded
					}
				}

				exhaurire_vitale_drain_single_fixed_trait_effect = {
					ACTOR = $ACTOR$
					TARGET = $TARGET$
					TRAIT = pure_blooded
				}
			}
			1 = { # fecund
				trigger = {
					exhaurire_vitale_can_drain_fixed_trait = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = fecund
					}
				}

				if = {
					limit = {
						$ACTOR$ = {
							has_trait = infertile
						}
					}

					$ACTOR$ = {
						remove_trait = infertile
					}

					$TARGET$ = {
						remove_trait = fecund
					}
				}
				else = {
					exhaurire_vitale_drain_single_fixed_trait_effect = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = fecund
					}
				}
			}
			1 = { # beauty_good
				trigger = {
					exhaurire_vitale_can_drain_leveled_trait = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = beauty_good
					}
				}

				if = {
					limit = {
						$ACTOR$ = {
							has_trait = beauty_bad
						}
					}

					$ACTOR$ = {
						change_trait_rank = {
							trait = beauty_bad
							rank = -1
						}
					}

					$TARGET$ = {
						change_trait_rank = {
							trait = beauty_good
							rank = -1
						}
					}
				}
				else = {
					exhaurire_vitale_drain_single_leveled_trait_effect = {
						ACTOR = $ACTOR$
						TARGET = $TARGET$
						TRAIT = beauty_good
					}
				}
			}
			1 = { # intellect_good & shrewd
				trigger = {
					OR = {
						exhaurire_vitale_can_drain_fixed_trait = {
							ACTOR = $ACTOR$
							TARGET = $TARGET$
							TRAIT = shrewd
						}
						exhaurire_vitale_can_drain_leveled_trait = {
							ACTOR = $ACTOR$
							TARGET = $TARGET$
							TRAIT = intellect_good
						}
					}
				}

				exhaurire_vitale_drain_intellect_trait_effect = {
					ACTOR = $ACTOR$
					TARGET = $TARGET$
				}
			}
			1 = { # physique_good & strong
				trigger = {
					OR = {
						exhaurire_vitale_can_drain_fixed_trait = {
							ACTOR = $ACTOR$
							TARGET = $TARGET$
							TRAIT = strong
						}
						exhaurire_vitale_can_drain_leveled_trait = {
							ACTOR = $ACTOR$
							TARGET = $TARGET$
							TRAIT = physique_good
						}
					}
				}

				exhaurire_vitale_drain_physique_trait_effect = {
					ACTOR = $ACTOR$
					TARGET = $TARGET$
				}
			}
		}
	}
}
