namespace = legend_events

############################
## Legend Spawn Events
## 0001-1000
## by James Beaumont
############################

# legend_events.0001 - Chronicler gives you a legend seed
# legend_events.0010 - Chronicler convinces someone to promote your legend
# legend_events.0020 - Hold Court presentation of a legend seed

# Chronicler gives you a legend seed :D
legend_events.0001 = {
	type = character_event
	title = legend_events.0001.t
	desc = {
		desc = legend_events.0001.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:slew_dragon
				}
				desc = legend_events.0001.desc.mid.slew_dragon
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:ancestral_saint
				}
				desc = legend_events.0001.desc.mid.ancestral_saint
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:old_godly_descent
				}
				desc = legend_events.0001.desc.mid.old_godly_descent
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:raised_by_animals
				}
				desc = legend_events.0001.desc.mid.raised_by_animals
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:auspicious_stars
				}
				desc = legend_events.0001.desc.mid.auspicious_stars
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:virgin_birth
				}
				desc = legend_events.0001.desc.mid.virgin_birth
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:spoke_to_angels
				}
				desc = legend_events.0001.desc.mid.spoke_to_angels
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:fought_the_devil
				}
				desc = legend_events.0001.desc.mid.fought_the_devil
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:exotic_backstory
				}
				desc = legend_events.0001.desc.mid.exotic_backstory
			}
			triggered_desc = {
				trigger = {
					var:legend_seed_to_give = flag:ancient_people
				}
				desc = legend_events.0001.desc.mid.ancient_people
			}
		}
		desc = legend_events.0001.desc.outro
	}
	theme = legend
	left_portrait = {
		character = root
		animation = personality_honorable
	}
	right_portrait = {
		character = scope:chronicler
		animation = chancellor
	}
	lower_center_portrait = {
		trigger = {
			exists = scope:legend_character
		}
		character = scope:legend_character
	}

	# The Court Chronicler needs to be alive
	trigger = {
		scope:chronicler = {
			is_alive = yes
		}
	}

	immediate = {
		random_list = { # Determine the seed to hand out
			10 = { # Ancestor killed a dragon
				trigger = {
					any_ancestor = {
						even_if_dead = yes
						is_adult = yes
						is_alive = no
					}
					NOT = { has_game_rule = historical_legends_only }
				}
				random_ancestor = {
					even_if_dead = yes
					limit = {
						is_adult = yes
						is_alive = no
					}
					weight = {
						base = 1
						modifier = {
							add = prowess
						}
						modifier = {
							add = martial
						}
					}
					save_scope_as = legend_character
				}
				set_variable = {
					name = legend_seed_to_give
					value = flag:slew_dragon
				}
			}
			10 = { # Ancestor was/should be a saint
				trigger = {
					any_ancestor = {
						even_if_dead = yes
						is_adult = yes
						is_alive = no
					}
					NOT = { has_game_rule = historical_legends_only }
				}
				random_ancestor = {
					even_if_dead = yes
					limit = {
						is_adult = yes
						is_alive = no
					}
					weight = {
						base = 1
						modifier = {
							add = learning
						}
						modifier = {
							factor = 3
							OR = {
								num_virtuous_traits = {
									target = root.faith
									value >= 1
								}
								has_trait = saint
							}
						}
						modifier = {
							factor = 0.1
							OR = {
								num_sinful_traits = {
									target = root.faith
									value >= 1
								}
								has_trait = excommunicated
							}
						}
					}
					save_scope_as = legend_character
				}
				set_variable = {
					name = legend_seed_to_give
					value = flag:ancestral_saint
				}
			}
			10 = { # I am descended from an old God
				trigger = {
					NOT = { religion = { is_in_family = rf_pagan } }
					any_ancestor = {
						even_if_dead = yes
						is_alive = no
						religion = { is_in_family = rf_pagan }
					}
					NOT = { has_game_rule = historical_legends_only }
				}
				random_ancestor = {
					even_if_dead = yes
					limit = {
						religion = { is_in_family = rf_pagan }
						is_alive = no
					}
					weight = {
						base = 1
						modifier = {
							add = learning
						}
						modifier = {
							factor = 3
							OR = {
								num_virtuous_traits = {
									target = root.faith
									value >= 1
								}
								has_trait = saint
							}
						}
						modifier = {
							factor = 0.1
							OR = {
								num_sinful_traits = {
									target = root.faith
									value >= 1
								}
								has_trait = excommunicated
							}
						}
					}
					save_scope_as = legend_character
				}
				set_variable = {
					name = legend_seed_to_give
					value = flag:old_godly_descent
				}
			}
			10 = { # I was raised by dangerous animals
				trigger = {
					NOT = { has_game_rule = historical_legends_only }
				}
				select_local_animal_effect = { TYPE = dangerous }
				set_variable = {
					name = legend_seed_to_give
					value = flag:raised_by_animals
				}
			}
			10 = { # Born under auspicious stars
				trigger = {
					NOT = { has_game_rule = historical_legends_only }
				}
				set_variable = {
					name = legend_seed_to_give
					value = flag:auspicious_stars
				}
			}
			10 = { # Virgin birth
				trigger = {
					has_any_bastard_trait_trigger = yes
					NOT = { has_game_rule = historical_legends_only }
				}
				set_variable = {
					name = legend_seed_to_give
					value = flag:virgin_birth
				}
			}
			10 = { # Spoke to an angel
				trigger = {
					religion_has_angels_trigger = yes
					NOT = { has_game_rule = historical_legends_only }
				}
				set_variable = {
					name = legend_seed_to_give
					value = flag:spoke_to_angels
				}
			}
			10 = { # Fought a demon
				trigger = {
					NOT = { has_game_rule = historical_legends_only }
				}
				set_variable = {
					name = legend_seed_to_give
					value = flag:fought_the_devil
				}
			}
			10 = { # Exotic backstory
				get_appropriate_exotic_location = yes
				set_variable = {
					name = legend_seed_to_give
					value = flag:exotic_backstory
				}
			}
			10 = { # Ancient people
				trigger = {
					# The ancient cultures we have for now aren't appropriate for these regions
					root.culture = {
						NOR = {
							culture_overlaps_geographical_region = world_burma
							culture_overlaps_geographical_region = custom_arakan_mountains
							culture_overlaps_geographical_region = world_tibet
						}
					}
				}
				get_appropriate_ancient_people = yes
				set_variable = {
					name = legend_seed_to_give
					value = flag:ancient_people
				}
			}
		}
	}

	option = { # Wowie zowie, really?
		name = legend_events.0001.a
		if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:slew_dragon
			}
			create_legend_seed = {
				type = heroic
				quality = famed
				chronicle = beast_slayer
				properties = {
					beast = flag:dragon
					location = root.location
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:ancestral_saint
			}
			create_legend_seed = {
				type = holy
				quality = famed
				chronicle = saintly_deed
				properties = {
					ancestor = scope:legend_character
					religion = root.religion
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:old_godly_descent
			}
			generate_religion_descent_effect = {
				RELIGION = scope:legend_character.religion
				GOD = high_god
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:raised_by_animals
			}
			create_legend_seed = {
				type = heroic
				quality = famed
				chronicle = raised_by_animals
				properties = {
					beast = var:animal_type
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:auspicious_stars
			}
			create_legend_seed = {
				type = holy
				quality = famed
				chronicle = sacred_birth
				properties = {
					reason = flag:auspicious
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:virgin_birth
			}
			create_legend_seed = {
				type = holy
				quality = famed
				chronicle = sacred_birth
				properties = {
					reason = flag:virgin
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:spoke_to_angels
			}
			create_legend_seed = {
				type = holy
				quality = famed
				chronicle = divine_intervention
				properties = {
					god = flag:the_angels
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:fought_the_devil
			}
			create_legend_seed = {
				type = holy
				quality = famed
				chronicle = fought_devil
				properties = {
					faith = root.faith
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:exotic_backstory
			}
			create_legend_seed = {
				type = heroic
				quality = famed
				chronicle = exotic_backstory
				properties = {
					location = scope:exotic_location
				}
			}
		}
		else_if = {
			limit = {
				exists = var:legend_seed_to_give
				var:legend_seed_to_give = flag:ancient_people
			}
			create_legend_seed = {
				type = legitimizing
				quality = famed
				chronicle = ancient_people
				properties = {
					culture = scope:ancient_culture
					title = root.primary_title
					original_region = var:original_region
				}
			}
		}
		ai_chance = {
			base = 100
		}
	}

	option = { # I don't want this legend
		name = legend_events.0001.b
		ai_chance = {
			base = 0
		}
	}
}

# Chronicler convinces someone to promote your legend
legend_events.0010 = {
	type = character_event
	title = legend_events.0010.t
	desc = legend_events.0010.desc
	theme = legend
	left_portrait = {
		character = root
		animation = personality_honorable
	}
	right_portrait = {
		character = scope:chronicler
		animation = chancellor
	}
	lower_center_portrait = scope:potential_promoter

	trigger = {
		OR = {
			scope:chronicler ?= {
				is_alive = yes
				has_court_position = chronicler_court_position
				employer = root
			}
			employs_court_position = chronicler_court_position
		}
		OR = {
			# Vassals
			any_vassal_or_below = {
				# No barons
				highest_held_title_tier >= tier_county
				# Not promoting their own legend
				promoted_legend.legend_owner != root
				promoted_legend != scope:liege.promoted_legend
				is_ai = yes
			}
			# Neighbourinos
			any_neighboring_and_across_water_top_liege_realm_owner = {
				# Not promoting their own legend
				promoted_legend.legend_owner != root
				promoted_legend != scope:liege.promoted_legend
				is_ai = yes
			}
		}
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					scope:chronicler ?= {
						is_alive = yes
						has_court_position = chronicler_court_position
						employer = root
					}
				}
			}
			court_position:chronicler_court_position ?= { save_scope_as = chronicler }
		}
		random_list = {
			# Vassals
			75 = {
				trigger = {
					any_vassal_or_below = {
						highest_held_title_tier >= tier_county
						promoted_legend.legend_owner != root
						promoted_legend != scope:liege.promoted_legend
						is_ai = yes
					}
				}
				random_vassal_or_below = {
					limit = {
						highest_held_title_tier >= tier_county
						promoted_legend.legend_owner != root
						promoted_legend != scope:liege.promoted_legend
						is_ai = yes
					}
					save_scope_as = potential_promoter
				}
			}
			# Neighbourinos
			25 = {
				trigger = {
					any_neighboring_and_across_water_top_liege_realm_owner = {
						promoted_legend.legend_owner != root
						promoted_legend != scope:liege.promoted_legend
						is_ai = yes
					}
				}
				random_neighboring_and_across_water_top_liege_realm_owner = {
					limit = {
						promoted_legend.legend_owner != root
						promoted_legend != scope:liege.promoted_legend
						is_ai = yes
					}
					save_scope_as = potential_promoter
				}
			}
		}

	}

	option = { # Diplo duel option
		name = legend_events.0010.a
		duel = {
			skill = diplomacy
			target = scope:potential_promoter
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = { # Reluctant to drop their existing legend
					scope:potential_promoter = { exists = promoted_legend }
					factor = 0.5
				}
				min = 10
				send_interface_toast = {
					title = legend_events.0010.a.success
					right_icon = scope:potential_promoter
					scope:potential_promoter = {
						set_promoted_legend = root.promoted_legend
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = { # Reluctant to drop their existing legend
					scope:potential_promoter = { exists = promoted_legend }
					factor = 2
				}
				min = 10
				send_interface_toast = {
					title = legend_events.0010.a.failure
					right_icon = scope:potential_promoter
					add_prestige = minor_prestige_loss
				}
			}
		}
		ai_chance = {
			base = 100
			modifier = { # I'd have better odds with option B
				intrigue > diplomacy
				factor = 0
			}
		}
	}

	option = { # Lie
		name = legend_events.0010.b
		duel = {
			skill = intrigue
			target = scope:potential_promoter
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = { # Reluctant to drop their existing legend
					scope:potential_promoter = { exists = promoted_legend }
					factor = 0.5
				}
				min = 10
				send_interface_toast = {
					title = legend_events.0010.a.success
					right_icon = scope:potential_promoter
					scope:potential_promoter = {
						set_promoted_legend = root.promoted_legend
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = { # Reluctant to drop their existing legend
					scope:potential_promoter = { exists = promoted_legend }
					factor = 2
				}
				min = 10
				send_interface_toast = {
					title = legend_events.0010.a.failure
					right_icon = scope:potential_promoter
					add_prestige = minor_prestige_loss
				}
			}
		}
		stress_impact = {
			honest = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = { # I'd have better odds with option A
				intrigue < diplomacy
				factor = 0
			}
		}
	}

	option = { # Nah not interested, go away.
		name = legend_events.0010.c
		ai_chance = {
			base = 0
		}
	}
}


legend_events.0020 = { #hold court presentation of a potential seed
	type = court_event
	title = legend_events.0020.t
	desc = legend_events.0020.desc

	theme = legend
	court_scene = {
		button_position_character = scope:seed_presenter
		court_event_force_open = yes
		show_timeout_info = no
		should_pause_time = yes
		roles = {
			scope:seed_presenter = {
				group = petitioners_group
				animation = personality_rational
			}
		}
	}

	widget = {
		gui = "event_window_widget_event_chain_progress"
		container = "custom_widgets_container"
		controller = event_chain_progress
	}

	weight_multiplier = {
		base = 1
		modifier = {
			factor = 2
			court_grandeur_current_level > court_grandeur_minimum_expected_level
		}
		# Court weightings.
		ep1_weight_up_for_court_type_modifier = { COURT_TYPE = court_diplomatic }
	}

	trigger = {
		any_pool_guest = {
			count >= 1
			is_available_adult = yes
			has_court_event_flag = no
			culture != root.culture
			opinion = {
				target = root
				value >= high_positive_opinion
			}
		}
		NOR = {
			has_personal_legend_seed = heroic
			has_personal_legend_seed = holy
			has_personal_legend_seed = legitimizing
		}
	}

	immediate = {
		random_pool_guest = {
			limit = {
				is_available_adult = yes
				has_court_event_flag = no
				culture != root.culture
				opinion = {
					target = root
					value >= high_positive_opinion
				}
			}
			save_scope_as = seed_presenter
			court_event_character_flag_effect = yes
		}

		hold_court_queue_next_event_effect = yes
	}

	option = { #yes, it is my about my dynasty and I shall prove it!
		name = legend_events.0020.a
		legend_seed_great_deed_dynasty_effect = yes
		culture = {
			change_cultural_acceptance = {
				target = scope:seed_presenter.culture
				value = miniscule_positive_culture_acceptance
				desc = cultural_acceptance_gain_hold_court_event_outcome
			}
		}
		stress_impact = {
			ambitious = minor_stress_loss
		}
		ai_chance = { #we want AI to have seeds
			base = 100
		}
	}

	option = { #yes, it is about me ruling the title and I shall prove it!
		name = legend_events.0020.b
		legend_seed_great_deed_title_effect = {
			TITLE = root.primary_title
		}
		culture = {
			change_cultural_acceptance = {
				target = scope:seed_presenter.culture
				value = miniscule_positive_culture_acceptance
				desc = cultural_acceptance_gain_hold_court_event_outcome
			}
		}
		stress_impact = {
			ambitious = minor_stress_loss
		}
		ai_chance = { #we want AI to have seeds
			base = 100
		}
	}

	option = { #no, I don't need to humor your culture fairy tales
		name = legend_events.0020.c
		add_prestige = medium_prestige_gain
		dynasty = {
			add_dynasty_prestige = minor_dynasty_prestige_gain
		}
		culture = {
			change_cultural_acceptance = {
				target = scope:seed_presenter.culture
				value = low_negative_culture_acceptance
				desc = cultural_acceptance_gain_hold_court_event_outcome
			}
		}

		stress_impact = {
			ambitious = minor_stress_gain
			compassionate = minor_stress_gain
			callous = medium_stress_loss
			sadistic = medium_stress_loss
		}
		ai_chance = { #we want AI to have seeds
			base = 0
		}
	}

	after = {
		scope:seed_presenter = {
			clear_court_event_participation = yes
		}
		# Finish up the chain if relevant.
		hold_court_queue_post_event_effect = yes
	}
}

legend_events.1000 = {
	type = letter_event
	opening = { desc = legend_events.1000.t }
	desc = legend_events.1000.desc
	sender = scope:legend_owner

	immediate = {
		scope:legend = {
			legend_owner = { save_scope_as = legend_owner }
		}
	}

	option = { #I did a silly
		name = legend_events.1000.a
		pay_short_term_gold = {
			target = scope:legend_owner
			gold = medium_gold_value
		}
		stress_impact = {
			deceitful = major_stress_impact_loss
			honest = medium_stress_impact_gain
			just = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
		}
	}

	option = { #If you have really high Intrigue you get away with paying just half
		name = legend_events.1000.b
		trigger = {
			intrigue >= very_high_skill_rating
		}
		#trait = deceitful
		pay_short_term_gold = {
			target = scope:legend_owner
			gold = {
				value = medium_gold_value
				multiply = 0.5
			}
		}
		stress_impact = {
			deceitful = massive_stress_impact_loss
			honest = major_stress_impact_gain
			just = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
		}
	}

	after = {
		remove_variable = accepted_promote_legend_var
	}
}
