﻿historical_figure_initialize_all_effect = {

	set_global_variable = { name = historical_figure_mod_version value = 3.10 }

	random_living_character = {
		historical_figure_birth_check_existence_all_effect = yes

		if = {
			limit = {
				NOT = { has_game_rule = historical_figure_birth_disabled }
			}

			if = {
				limit = {
					historical_figure_is_first_day_of_year_trigger = yes
				}
				# avoid character duplication
			}
			else = {
				historical_figure_birth_check_all_effect = yes
				historical_figure_check_change_first_name_all_effect = yes
			}
		}
	}

}


historical_figure_initialize_effect = {

	# Prepare scopes

	save_scope_value_as = { name = figure     value = $FIGURE$ }
	save_scope_value_as = { name = desc       value = $DESC$ }

	character:$CHAR$ = { save_scope_as = target }

	scope:target = {

		# Add Special Trait

		historical_figure_add_special_trait_effect = yes

		# Create template character

		if = {
			limit = {
				is_alive = yes
				age < adulthood_start_age
			}

			random_province = {
				save_scope_as = birthplace
			}

			create_character = {
				age = adulthood_start_age
				gender = scope:target
				faith = scope:target.faith
				culture = scope:target.culture
				location = scope:birthplace
				template = historical_figure_$CHAR$
				save_scope_as = template
			}

			create_character = {
				age = adulthood_start_age
				gender = scope:target
				faith = scope:target.faith
				culture = scope:target.culture
				location = scope:birthplace
				template = historical_figure_$CHAR$
				save_scope_as = template2
			}

			create_character = {
				age = adulthood_start_age
				gender = scope:target
				faith = scope:target.faith
				culture = scope:target.culture
				location = scope:birthplace
				template = historical_figure_$CHAR$
				save_scope_as = template3
			}
		}

		# Initialization

		trigger_event = historical_figure_birth.4001
	}

}


historical_figure_birth_from_father_effect = {

	# Check current date

	if = {
		limit = {
			game_start_date >= $DATE$
		}

		# do nothing if the character was already born
	}
	else = {

		# Prepare scopes

		faith:$FAITH$     = { save_scope_as = faith }
		culture:$CULTURE$ = { save_scope_as = culture }

		save_scope_value_as = { name = figure     value = $FIGURE$ }
		save_scope_value_as = { name = desc       value = $DESC$ }
		save_scope_value_as = { name = priority   value = flag:$PRIORITY$ }

		# Create template character

		random_province = {
			save_scope_as = birthplace
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template2
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template3
		}

		# Add Special Trait

		scope:template = {
			save_scope_as = target
			historical_figure_add_special_trait_effect = yes
		}

		# Give birth

		trigger_event = {
			id = historical_figure_birth.1001
			days = 0
		}
	}

}


historical_figure_birth_from_mother_effect = {

	# Check current date

	if = {
		limit = {
			game_start_date >= $DATE$
		}

		# do nothing if the character was already born
	}
	else = {

		# Prepare scopes

		faith:$FAITH$     = { save_scope_as = faith }
		culture:$CULTURE$ = { save_scope_as = culture }

		save_scope_value_as = { name = figure     value = $FIGURE$ }
		save_scope_value_as = { name = desc       value = $DESC$ }
		save_scope_value_as = { name = priority   value = flag:$PRIORITY$ }

		# Create template character

		random_province = {
			save_scope_as = birthplace
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template2
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template3
		}

		# Add Special Trait

		scope:template = {
			save_scope_as = target
			historical_figure_add_special_trait_effect = yes
		}

		# Give birth

		trigger_event = {
			id = historical_figure_birth.2001
			days = 0
		}

	}

}


historical_figure_birth_from_parentless_effect = {

	# Check current date

	if = {
		limit = {
			game_start_date >= $DATE$
		}

		# do nothing if the character was already born
	}
	else = {

		# Prepare scopes

		faith:$FAITH$     = { save_scope_as = faith }
		culture:$CULTURE$ = { save_scope_as = culture }

		save_scope_value_as = { name = figure     value = $FIGURE$ }
		save_scope_value_as = { name = desc       value = $DESC$ }

		# Create template character

		random_province = {
			save_scope_as = birthplace
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template2
		}

		create_character = {
			age = adulthood_start_age
			name = $NAME$
			gender_female_chance = $GENDER$
			faith = scope:faith
			culture = scope:culture
			location = scope:birthplace
			template = historical_figure_$CHAR$
			save_scope_as = template3
		}

		# Add Special Trait

		scope:template = {
			save_scope_as = target
			historical_figure_add_special_trait_effect = yes
		}

		# Give birth

		trigger_event = {
			id = historical_figure_birth.3001
			days = 0
		}

	}

}


historical_figure_delete_template_character_effect = {

	scope:template = {
		if = {
			limit = { is_alive = yes }
			death = { death_reason = death_vanished }
		}
	}
	scope:template2 = {
		if = {
			limit = { is_alive = yes }
			death = { death_reason = death_vanished }
		}
	}
	scope:template3 = {
		if = {
			limit = { is_alive = yes }
			death = { death_reason = death_vanished }
		}
	}

}


historical_figure_set_dynasty_house_father_effect = {

	if = {
		limit = {
			exists = scope:father.house
		}
		scope:father.house = { save_scope_as = house }
	}
	else_if = {
		limit = {
			exists = scope:father.dynasty
		}
		scope:father.dynasty = { save_scope_as = dynasty }
	}

}


historical_figure_set_dynasty_house_mother_effect = {

	if = {
		limit = {
			exists = scope:mother.house
		}
		scope:mother.house = { save_scope_as = house }
	}
	else_if = {
		limit = {
			exists = scope:mother.dynasty
		}
		scope:mother.dynasty = { save_scope_as = dynasty }
	}

}


historical_figure_find_birthplace_effect = {

	if = {
		limit = {
			any_province = {
				this.faith   = $FAITH$
				this.culture = $CULTURE$
			}
		}
		random_province = {
			limit = {
				this.faith   = $FAITH$
				this.culture = $CULTURE$
			}
			save_scope_as = birthplace
		}
	}
	else_if = {
		limit = {
			any_province = {
				this.culture = $CULTURE$
			}
		}
		random_province = {
			limit = {
				this.culture = $CULTURE$
			}
			save_scope_as = birthplace
		}
	}
	else_if = {
		limit = {
			any_province = {
				this.faith = $FAITH$
			}
		}
		random_province = {
			limit = {
				this.faith = $FAITH$
			}
			save_scope_as = birthplace
		}
	}
	else = {
		random_province = {
			save_scope_as = birthplace
		}
	}
}


historical_figure_add_special_trait_effect = {

	if = {
		limit = {
			scope:desc ?= 1
		}
		add_trait = historical_figure_famous
	}
	else = {
		add_trait = historical_figure
	}

}


historical_figure_add_information_effect = {

	if = {
		limit = {
			is_alive = yes
		}

		set_variable = {
			name = historical_figure_id
			value = scope:figure
		}

		add_to_global_variable_list = {
			name = historical_figure_list
			target = this
		}

		add_to_global_variable_list = {
			name = historical_figure_id_list
			target = scope:figure
		}
	}

}
