﻿namespace = court_events

##################################################
# # RANGES
#1000 - 1100 Jason Cantalini
# 
##################################################

#####################################
# The Very Grandest of All			#
# by Jason Cantalini				#
# 1000-1001							#
#####################################

# You reach #1 in global court grandeur
court_events.1000 = {
	type = court_event
	title = court_events.1000.t
	desc = court_events.1000.desc

	theme = court
	court_scene = {
		button_position_character = scope:guest
		roles = {
			scope:guest = {
				group = event_group
				animation = throne_room_bow_1
			}
			scope:guest_2 = {
				group = event_group
				animation = throne_room_curtsey_1
			}
			root = {
				group = event_group
				animation = war_over_win
			}
		}
	}

	cooldown = { years = 10 }

	trigger = {
		#just to acknowledge players
		is_ai = no
		is_available = yes
		#Is court grandeur at all good?
		has_royal_court = yes
		court_grandeur_current >= 30
		#Check your court grandeur is at least 10 higher than everyone else's
		NOT = {
			any_character_with_royal_court = {
				court_grandeur_current > root.court_grandeur_current_minus_ten_value
				this != root
			 }
		}
	}

	immediate = {
		#get our first fawning traveler
		#grab a guest with traveler or pilgrim trait
		if = {
			limit = {
				any_pool_guest = {
					is_available_ai_adult = yes
					age >= 25
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
				}
			}
			random_pool_guest = {
				limit = {
					is_available_ai_adult = yes
					age >= 25
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
				}
				save_scope_as = guest
			}
		}
		#grab any pool character
		else_if = {
			limit = {
				any_pool_character = {
					province = root.capital_province
					age >= 25
					is_available_ai_adult = yes
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
				}
			}
			random_pool_character = {
				province = root.capital_province
				limit = {
					age >= 25
					is_available_ai_adult = yes
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
				}
				save_scope_as = guest
			}
		}
		#create character
		else = {
			#will use to grab interesting cultures for travellers
			save_traveler_culture_effect = yes
			create_character = {
				template = traveler_character
				location = root.capital_province
				random_culture = {
					scope:traveler_culture = {
						trigger = {
							exists = scope:traveler_culture
						}
					}
				}
				gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
				save_scope_as = guest
			}
			scope:guest = { add_character_flag = created_character }
		}
		#get our second fawning traveler
		#grab a guest with traveler or pilgrim trait
		if = {
			limit = {
				any_pool_guest = {
					is_available_ai_adult = yes
					age >= 25
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
					this != scope:guest
				}
			}
			random_pool_guest = {
				limit = {
					is_available_ai_adult = yes
					age >= 25
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
					this != scope:guest
				}
				save_scope_as = guest_2
			}
		}
		#grab any pool character
		else_if = {
			limit = {
				any_pool_character = {
					province = root.capital_province
					age >= 25
					is_available_ai_adult = yes
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
					this != scope:guest
				}
			}
			random_pool_character = {
				province = root.capital_province
				limit = {
					age >= 25
					is_available_ai_adult = yes
					has_court_event_flag = no
					OR = {
						has_trait = lifestyle_traveler
						has_trait = pilgrim
					}
					this != scope:guest
				}
				save_scope_as = guest_2
			}
		}
		#create character
		else = {
			create_character = {
				template = traveler_character
				location = root.capital_province
				random_culture = {
					scope:traveler_culture_2 = {
						trigger = {
							exists = scope:traveler_culture_2
						}
					}
				}
				gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
				save_scope_as = guest_2
			}
			scope:guest_2 = { add_character_flag = created_character }
		}
		#grab your high almoner if you have one
		court_position:high_almoner_court_position ?= { save_scope_as = high_almoner }
		hidden_effect = {
			if = {
				limit = {
					scope:guest = {
						NOR = {
							has_relation_rival = scope:guest_2
							has_relation_nemesis = scope:guest_2
							has_character_flag = created_character
						}
					}
					scope:guest_2 = {
						NOT = {
							has_character_flag = created_character
						}
					}
				}
				scope:guest = {
					set_relation_friend = {
						reason = friend_random_fallback
						target = scope:guest_2
					}
				}
			}
			else = {
				scope:guest = {
					set_relation_best_friend = {
						reason = friend_random_fallback
						target = scope:guest_2
					}
				}
			}
			if = {
				limit = {
					scope:guest = { has_character_flag = created_character }
					scope:guest.culture = {
						exists = culture_head
						culture_head.faith != scope:guest.faith
					}
				}
				scope:guest = { set_character_faith = culture.culture_head.faith }
			}
			if = {
				limit = {
					scope:guest_2 = { has_character_flag = created_character }
					scope:guest_2.culture = {
						exists = culture_head
						culture_head.faith != scope:guest_2.faith
					}
				}
				scope:guest_2 = { set_character_faith = culture.culture_head.faith }
			}
			if = {
				limit = {
					NOT = {
						scope:guest = {
							knows_language_of_culture = scope:guest_2.culture
						}
					}
					scope:guest = { has_character_flag = created_character }
				}
				scope:guest = { learn_language_of_culture = scope:guest_2.culture }
			}
			if = {
				limit = {
					NOT = {
						scope:guest_2 = {
							knows_language_of_culture = scope:guest.culture
						}
					}
					scope:guest_2 = { has_character_flag = created_character }
				}
				scope:guest_2 = { learn_language_of_culture = scope:guest.culture }
			}
			if = {
				limit = {
					scope:guest = { has_character_flag = created_character }
				}
				scope:guest = {
					add_opinion = {
						target = root
						modifier = reputation_opinion
						opinion = 25
					}
				}
			}
			if = {
				limit = {
					scope:guest_2 = { has_character_flag = created_character }
				}
				scope:guest_2 = {
					add_opinion = {
						target = root
						modifier = reputation_opinion
						opinion = 25
					}
				}
			}
		}
		#get second place ruler so we can dunk on them in loc
		random_character_with_royal_court = {
			limit = {
				NOR = {
					any_character_with_royal_court = {
						count >= 2
						court_grandeur_current > prev.court_grandeur_current
					}
					this = root
				}
			}
			save_scope_as = second_court
		}
	}

	# Option A: self-effacingly use your high almoner to prove you are both grand and humble
	option = { 
		name = court_events.1000.a
		flavor = court_events.1000.a.flavor
		trigger = {
			exists = scope:high_almoner
			scope:high_almoner = {
				aptitude = {
					court_position = high_almoner_court_position
					value >= 2
				}
			}
		}
		reason = high_almoner
		add_piety = medium_piety_gain
		capital_county = {
			change_development_level = 1
		}
		remove_short_term_gold = medium_gold_value
		stress_impact = {
			generous = miniscule_stress_impact_loss
			arrogant = major_stress_impact_gain
			greedy = major_stress_impact_gain
			ambitious = minor_stress_impact_gain
			callous = medium_stress_impact_gain
			sadistic = medium_stress_impact_gain
			arbitrary = minor_stress_impact_gain
		}
		ai_chance = {
			base = 1
		}
	}

	#Option B: I'm the greatest of kings, yes. Remain here and tell everyone
	option = { 
		name = court_events.1000.b
		flavor = court_events.1000.b.flavor
		change_current_court_grandeur = 5
		add_courtier = scope:guest
		add_courtier = scope:guest_2
		
		random_list = {
			10 = {
				give_nickname = nick_the_glorious
			}
			10 = {
				give_nickname = nick_the_magnificent
			}
			10 = {
				give_nickname = nick_the_gracious
			}
			10 = {
				give_nickname = nick_the_stupor_mundi
			}
			20 = {}
		}
		
		
		custom_tooltip = traveling_friends_court_position_aptitude
		
		scope:guest = {
			add_character_flag = travelling_friend
		}
		scope:guest_2 = {
			add_character_flag = travelling_friend
		}
		stress_impact = {
			arrogant = minor_stress_impact_loss
			ambitious = miniscule_stress_impact_loss
			paranoid = minor_stress_impact_gain
			humble = minor_stress_impact_gain
			temperate = minor_stress_impact_gain
		}
		ai_chance = {
			base = 1
		}
	}

	#Option C: Let all speak of my grandness! Go, spread my legend!
	option = { 
		name = court_events.1000.c
		custom_tooltip = visiting_gives_opinion
		add_legitimacy = minor_legitimacy_gain
		change_current_court_grandeur = 5
		scope:guest = {
			add_character_flag = spreading_word_of_grandeur
			set_variable = {
				name = greatest_court_holder
				value = root
			}
		}
		scope:guest_2 = {
			add_character_flag = spreading_word_of_grandeur
			set_variable = {
				name = greatest_court_holder
				value = root
			}
		}
		stress_impact = {
			arrogant = minor_stress_impact_loss
			ambitious = miniscule_stress_impact_loss
			humble = minor_stress_impact_gain
			temperate = minor_stress_impact_gain
		}
		ai_chance = {
			base = 1
		}
	}

	
	after = {
		scope:guest = {
			clear_court_event_participation = yes
			remove_character_flag ?= created_character
		}
		scope:guest_2 = {
			clear_court_event_participation = yes
			remove_character_flag ?= created_character
		}
	}
}

#ruler hosting travellers gains opinion of #1 grand emperor
court_events.1001 = {
	hidden = yes
	trigger = {
		scope:guest = {
			has_character_flag = spreading_word_of_grandeur
			var:greatest_court_holder = {
				is_alive = yes
				has_royal_court = yes
			}
		}
	}
	immediate = {
		scope:guest = {
			var:greatest_court_holder = {
				reverse_add_opinion = {
					modifier = reputation_opinion
					target = host
					opinion = 20
				}
			}
		}
	}
}

scripted_trigger court_events_1010_players_nobody_trigger = {
	is_healthy = yes
	is_available_ai_adult = yes
	NOR = {
		is_close_family_of = liege
		has_relation_lover = liege
		has_relation_soulmate = liege
		has_relation_friend = liege
		has_relation_best_friend = liege
		has_relation_guardian = liege
		has_relation_bully = liege
		has_relation_victim = liege
		is_consort_of = root
		is_hostage_of = root
		has_trait = loyal
		ep3_story_cycle_admin_eunuch_involved_character = yes
	}
	ai_honor <= low_positive_ai_value
	has_any_court_position = no
	is_councillor = no
	is_acclaimed = no
	opinion = {
		target = liege
		value <= -30
	}
}
scripted_trigger court_events_1010_ais_courtier_trigger = {
	is_healthy = yes
	is_available_ai_adult = yes
	NOR = {
		is_heir_of = liege
		has_relation_lover = liege
		has_relation_soulmate = liege
		has_relation_friend = liege
		has_relation_best_friend = liege
		has_relation_guardian = liege
		has_relation_bully = liege
		has_relation_victim = liege
		is_consort_of = root
		is_hostage_of = root
		has_trait = loyal
		ep3_story_cycle_admin_eunuch_involved_character = yes
	}
	ai_honor <= low_positive_ai_value
	is_acclaimed = no
	opinion = {
		target = liege
		value <= -20
	}
}

#####################################
# Maybe I should steal servants?	#
# by Jason Cantalini				#
# 01010								#
#####################################

# You meet with some disgruntled functionaries who work for a great ruler
court_events.1010 = {
	type = court_event
	title = court_events.1010.t
	desc = court_events.1010.desc

	theme = court
	court_scene = {
		button_position_character = scope:defector
		roles = {
			scope:defector = {
				group = event_group
				animation = obsequious_bow
			}
			root = {
				group = event_group
				triggered_animation = {
					trigger = { OR = { religion = religion:eastern_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion } }
					animation = acknowledging # anim uses crucifix scepter
				}
				animation = war_over_tie
			}
		}
	}
	lower_right_portrait = scope:grander_ruler
	lower_left_portrait = scope:court_chaplain

	cooldown = { years = 10 }

	trigger = {
		
		has_royal_court = yes
		#Is court grandeur at all good?
		court_grandeur_current >= 10
		is_available = yes
		#Check someone's court grandeur is at least 10 higher than yours
		any_character_with_royal_court = {
			court_grandeur_current >= root.court_grandeur_current_plus_ten_value
			this != root
			in_diplomatic_range = root
			NOR = {
				is_at_war_with = root
				has_character_flag = had_court_events_1000
			}
			#Get a shitty character from a player or good one from ai
			OR = {
				AND = {
					is_ai = no
					any_courtier = {
						court_events_1010_players_nobody_trigger = yes
					}
				}
				AND = {
					is_ai = yes
					any_courtier = {
						court_events_1010_ais_courtier_trigger = yes
					}
				}
			}
		}
		exists = cp:councillor_court_chaplain
	}

	weight_multiplier = {
		base = 1
		modifier = {
			factor = 0.33
			#It will be annoying if AI steal lots of courtiers
			#And foreigners being thrown around might look weird
			is_ai = yes
		}
	}

	immediate = {
		#Save the court owner being targeted
		random_character_with_royal_court = {
			limit = {
				court_grandeur_current >= root.court_grandeur_current_plus_ten_value
				this != root
				in_diplomatic_range = root
				NOR = {
					is_at_war_with = root
					has_character_flag = had_court_events_1000
				}
				#Get a shitty character from a player or good one from ai
				OR = {
					AND = {
						is_ai = no
						any_courtier = {
							court_events_1010_players_nobody_trigger = yes
						}
					}
					AND = {
						is_ai = yes
						any_courtier = {
							court_events_1010_ais_courtier_trigger = yes
						}
					}
				}
			}
			save_scope_as = grander_ruler
		}
		scope:grander_ruler = {
			if = {
				limit = {
					is_ai = no
					any_courtier = {
						court_events_1010_players_nobody_trigger = yes
						opinion = {
							target = scope:grander_ruler
							value <= -75
						}
					}
				}
				random_courtier = {
					limit = {
						court_events_1010_players_nobody_trigger = yes
						opinion = {
							target = scope:grander_ruler
							value <= -75
						}
					}
					save_scope_as = defector
				}
			}
			else_if = {
				limit = {
					is_ai = yes
					any_courtier = {
						court_events_1010_ais_courtier_trigger = yes
					}
					opinion = {
						target = scope:grander_ruler
						value <= -75
					}
				}
				random_courtier = {
					limit = {
						court_events_1010_ais_courtier_trigger = yes
						opinion = {
							target = scope:grander_ruler
							value <= -75
						}
					}
					save_scope_as = defector
				}
			}
			else_if = {
				limit = {
					is_ai = no
				}
				random_courtier = {
					limit = {
						court_events_1010_players_nobody_trigger = yes
					}
					save_scope_as = defector
				}
			}
			else = {
				random_courtier = {
					limit = {
						court_events_1010_ais_courtier_trigger = yes
					}
					save_scope_as = defector
				}
			}
		}
		#This will be a relevant fella
		cp:councillor_court_chaplain = {
			save_scope_as = court_chaplain
		}

		#grab your antiquarian if you have one
		court_position:antiquarian_court_position ?= { save_scope_as = antiquarian }
		save_scope_as = root_scope
		#Save artifact to potentially steal
		if = {
			limit = {
				scope:grander_ruler = {
					any_character_artifact = {
						ep1_artifact_is_court_artifact_trigger = yes
						NOR = {
							has_variable = banner_house
							has_variable = banner_dynasty
						}
					}
				}
			}
			scope:grander_ruler = {
				random_character_artifact = {
					limit = {
						ep1_artifact_is_court_artifact_trigger = yes
						NOR = {
							has_variable = banner_house
							has_variable = banner_dynasty
						}
					}
					save_scope_as = coveted_artifact
				}
			}
		}
		scope:grander_ruler = {
			add_character_flag = {
				flag = had_court_events_1000
				years = 5
			}
		}
	}

	# Option A: just courtiers? What about stealing a fine artifact too...?
	option = { 
		name = court_events.1010.a
		flavor = court_events.1010.a.flavor
		trigger = {
			exists = scope:antiquarian
			exists = scope:coveted_artifact
			scope:antiquarian = {
				aptitude = {
					court_position = antiquarian_court_position
					value >= 3
				}
			}
			scope:grander_ruler = { is_ai = yes }
		}
		reason = antiquarian
		scope:defector = {
			duel = {
				skill = intrigue
				target = scope:grander_ruler
				70 = { # Your new courtier steals the artifact
					desc = court_events.1010.a.success
					compare_modifier = {
						value = scope:duel_value
						multiplier = 3.5
						min = -69
					}
					root = {
						send_interface_toast = {
							type = event_toast_effect_good
							title = court_events.1010.a.success
							left_icon = scope:defector
							right_icon = scope:grander_ruler
							scope:coveted_artifact = {
								set_owner = {
									target = root
									history = {
										location = root.capital_province
										actor = scope:grander_ruler
										recipient = root
										type = stolen
									}
								}
							}
							set_relation_rival = {
								target = scope:grander_ruler
								reason = rival_stole_artifact
							}
							change_current_court_grandeur = 25
							scope:grander_ruler = { change_current_court_grandeur = -15 }
							add_courtier = scope:defector
						}
					}
				}
				30 = { # The fella frigs up
					desc = court_events.1010.a.failure
					compare_modifier = {
						value = scope:duel_value
						multiplier = -3.5
						min = -29
					}
					root = {
						send_interface_toast = {
							type = event_toast_effect_bad
							title = court_events.1010.a.failure
							left_icon = scope:defector
							right_icon = scope:grander_ruler
							
							scope:defector = {
								death = {
									death_reason = death_execution
									killer = scope:grander_ruler
								}
							}
						}
					}
				}
			}
		}
		add_prestige = medium_prestige_loss
		stress_impact = {
			greedy = minor_stress_impact_loss
			eccentric = miniscule_stress_impact_loss
			arbitrary = minor_stress_impact_loss
			honest = major_stress_impact_gain
			just = major_stress_impact_gain
			generous = medium_stress_impact_gain
			compassionate = minor_stress_impact_gain
			content = minor_stress_impact_gain
			trusting = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = 1
				ai_honor = -1
			}
			modifier = {
				add = -75
				OR = {
					has_trait = compassionate
					has_trait = content
					has_trait = trusting
				}
			}
			modifier = {
				factor = 0
				OR = {
					prestige <= major_prestige_value
					has_trait = honest
					has_trait = just
					has_trait = generous
				}
			}
		}
	}

	#Option B: Come. Serve with honor here
	option = { 
		name = court_events.1010.b
		
		
		if = {
			limit = {
				scope:grander_ruler = { is_ai = no }
			}
			show_as_tooltip = {
				change_current_court_grandeur = 20
				scope:grander_ruler = { change_current_court_grandeur = -10 }
				add_courtier = scope:defector
			}
			
			scope:grander_ruler = {
				trigger_event = court_events.1011
			}
		}
		else = {
			change_current_court_grandeur = 20
			scope:grander_ruler = { change_current_court_grandeur = -10 }
			add_courtier = scope:defector
		}
		reverse_add_opinion = {
			target = scope:defector
			modifier = loyalty_opinion
			opinion = 20
		}
		add_prestige = major_prestige_loss
		

		stress_impact = {
			trusting = miniscule_stress_impact_loss
			honest = medium_stress_impact_gain
			just = medium_stress_impact_gain
			paranoid = medium_stress_impact_gain
			content = minor_stress_impact_gain
			humble = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
				ai_honor = -1
			}
			modifier = {
				add = -75
				OR = {
					has_trait = humble
					has_trait = content
				}
			}
			modifier = {
				factor = 0
				OR = {
					prestige <= major_prestige_value
					has_trait = honest
					has_trait = just
					has_trait = paranoid
				}
			}
		}
	}

	#If I need functionaries, why not some priests?
	option = { 
		name = court_events.1010.c
		flavor = court_events.1010.c.flavor
		
		change_current_court_grandeur = 10
		scope:court_chaplain = {
			add_learning_skill = 2
			add_opinion = {
				modifier = pleased_opinion
				target = root
				opinion = 20
			}
		}
		add_piety = medium_piety_loss

		stress_impact = {
			cynical = major_stress_impact_gain
			arrogant = minor_stress_impact_gain
			gregarious = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 1
				ai_rationality = 0.5
			}
			modifier = {
				add = -75
				OR = {
					has_trait = arrogant
					has_trait = gregarious
				}
			}
			modifier = {
				factor = 0
				OR = {
					piety <= medium_piety_value
					has_trait = cynical
				}
			}
		}
	}
	#Disloyal people? Go serve, and feel shame
	option = { 
		name = court_events.1010.d
		
		every_vassal = {
			vassal_stance = parochial
			custom = every_parochial_vassal
			add_opinion = {
				modifier = respect_opinion
				opinion = 10
				target = root
			}
		}
		reverse_add_opinion = {
			modifier = grateful_opinion
			target = scope:grander_ruler
			opinion = 20
		}
		
		add_prestige = minor_prestige_gain

		stress_impact = {
			arbitrary = minor_stress_impact_gain
			deceitful = medium_stress_impact_gain
			ambitious = medium_stress_impact_gain
			generous = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = -1
				ai_honor = 1
			}
			modifier = {
				add = -75
				OR = {
					has_trait = arbitrary
					has_trait = generous
				}
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = ambitious
					has_trait = deceitful
				}
			}
		}
	}
}

court_events.1011 = {
	type = letter_event
	opening = {
		desc = court_events.1011.opening
	}
	desc = court_events.1011.desc
	sender = scope:defector

	immediate = {
		change_current_court_grandeur = -10
		scope:root_scope = {
			change_current_court_grandeur = 20
			add_courtier = scope:defector
		}
	}

	option = {
		name = court_events.1011.a
	}
}
