#Court Maintenance events of various persuasions

namespace = court_maintenance

#Children should acquire personality trait events
court_maintenance.0003 = {
	hidden = yes

	trigger = {
		OR = {
			is_adult = no
			any_courtier_or_guest = {
				is_adult = no
			}
			any_prisoner = {
				is_adult = no
			}
		}
	}

	immediate = {
		if = {
			limit = {
				is_adult = no
				OR = {
					age = 9
					age = 11
					age = 13
					age >= 14 #Backup in case something has gone wrong
				}
				number_of_personality_traits < childhood_personality_trait_gain_limit
			}
			if = {
				limit = {
					age = 9
					has_variable = reincarnation_of
					can_become_reincarnation_trigger = yes
				}
				trigger_event = child_personality.9900
			}
			else = {
				trigger_event = {
					on_action = child_personality_gain
					days = { 1 360 }
				}
			}
		}
		every_courtier_or_guest = {
			limit = {
				is_adult = no
				OR = {
					age = 9
					age = 11
					age = 13
					age >= 14 #Backup in case something has gone wrong
				}
				number_of_personality_traits < childhood_personality_trait_gain_limit
			}
			trigger_event = {
				on_action = child_personality_gain
				days = { 1 360 }
			}
		}
		#Small random chance to acquire a fourth personality trait
		every_courtier_or_guest = {
			limit = {
				is_adult = no
				number_of_personality_traits = childhood_personality_trait_gain_limit
				number_of_personality_traits < personality_trait_limit
			}
			random = {
				chance = childhood_fourth_personality_trait_chance
				trigger_event = {
					on_action = child_personality_gain
					days = { 1 360 }
				}
			}
		}
		every_prisoner = {
			limit = {
				is_adult = no
				OR = {
					age = 9
					age = 11
					age = 13
					age >= 14 #Backup in case something has gone wrong
				}
				number_of_personality_traits < childhood_personality_trait_gain_limit
			}
			trigger_event = {
				on_action = child_personality_gain
				days = { 1 360 }
			}
		}
		#Small random chance to acquire a fourth personality trait
		every_prisoner = {
			limit = {
				is_adult = no
				number_of_personality_traits = childhood_personality_trait_gain_limit
				number_of_personality_traits < personality_trait_limit
			}
			random = {
				chance = childhood_fourth_personality_trait_chance
				trigger_event = {
					on_action = child_personality_gain
					days = { 1 360 }
				}
			}
		}
	}
}

court_maintenance.0005 = {
	hidden = yes

	trigger = {
		scope:new_employer = {
			government_has_flag = government_is_holy_order
		}
	}

	immediate = {
		add_trait = order_member
	}
}

#Absent from your court
court_maintenance.0010 = {
	theme = crown
	override_background = { reference = council_chamber }
	type = character_event
	title = court_maintenance.0010.t
	left_portrait = {
		character = scope:messenger
		animation = worry
	}

	cooldown = { years = 5 }

	trigger = {
		always = no # Disabled for now
		has_royal_court = yes
		has_dlc_feature = royal_court
		is_ai = no
		is_available_adult = yes
		is_at_war = no
	}

	desc = {	
		desc = court_maintenance.0010.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:great_cgv_court
				}
				desc = court_maintenance.0010.desc.seeking_gainful_employment
			}
			desc = court_maintenance.0010.desc.general_ending
		}
	}

	immediate = {
		ordered_independent_ruler = {
			order_by = court_grandeur_current
			limit = {
				faith.religion = root.faith.religion
				in_diplomatic_range = root
				this != root
			}
			alternative_limit = {
				this != root
				in_diplomatic_range = root
			}
			alternative_limit = {
				this != root
			}
			save_scope_as = great_cgv_court
		}
		random_courtier = {
			limit = {
				has_any_court_position = yes
			}
			alternative_limit = {
				is_available_adult = yes
			}
			save_scope_as = messenger
		}
	}

	option = {
		name = court_maintenance.0010.a
		#Open Royal Court
		custom_tooltip = court_maintenance.0010.a.tooltip
		open_view_data = {
			view = royal_court
			player = root
		}
	}

	option = {
		name = {
			text = court_maintenance.0010.b.paranoid
			trigger = { has_trait = paranoid }
		}
		name = {
			text = court_maintenance.0010.b.depressed
			trigger = { has_trait = depressed }
		}
		name = {
			text = court_maintenance.0010.b.brave
			trigger = { has_trait = brave }
		}
		name = {
			text = court_maintenance.0010.b
			trigger = { always = yes }
		}
		change_current_court_grandeur = miniscule_court_grandeur_loss
		if = {
			limit = { exists = scope:great_cgv_court }
			custom_tooltip = court_maintenance.0010.b.tooltip
		}
		hidden_effect = {
			if = {
				limit = { exists = scope:great_cgv_court }
				random_courtier = {
					limit = {
						has_any_court_position = no
						is_physically_able_ai_adult = yes
						is_councillor = no
						NOT = { is_vassal_of = root }
						NOT = { is_close_family_of = ROOT }
					}
					set_employer = scope:great_cgv_court
					return_to_court = yes
				}
			}
		}
	}
}

#Gained a Royal Court
court_maintenance.0011 = {
	theme = new_royal_court
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { tgp_is_ceremonial_regent_trigger = yes } # Not 'actual' ruler
				desc = court_maintenance.0011.t.regent
			}
			desc = court_maintenance.0011.t
		}
	}
	window = visit_settlement_window
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { tgp_is_ceremonial_regent_trigger = yes } # Not 'actual' ruler
				desc = court_maintenance.0011.desc.regent
			}
			triggered_desc = {
				trigger = { coronation_trigger = yes }
				desc = court_maintenance.0011.desc.ach
			}
			desc = court_maintenance.0011.desc
		}
	}

	left_portrait = {
		character = root
		animation = war_over_win
	}
	right_portrait = {
		character = scope:messenger
		animation = ecstasy
	}
	artifact = { # To display the dynasty banner in the event-window
	    target = scope:dynasty_banner
	    position = lower_center_portrait
	}
	artifact = { # To display the house banner in the event-window
	    target = scope:house_banner
	    position = lower_right_portrait
	}

	trigger = {
		is_ai = no
		is_landed = yes
		has_dlc_feature = royal_court
		# To avoid farming and duplication
		OR = {
			# No owned house banner
			NOT = {
				any_character_artifact = {
					has_variable = banner_house
					var:banner_house = root.house
				}
			}
			AND = {
				# Is Dynasty Head
				OR = {
					this = dynasty.dynast # Dynasty Head
					any_parent = { # Parent landless
						employer = prev
						dynasty = prev.dynasty
						this = dynasty.dynast
					}
				}
				# Dynasty banner does not exist
				NOT = {
					any_character_artifact = {
						has_variable = banner_dynasty
						var:banner_dynasty = root.dynasty
					}
				}
			}
		}
	}

	immediate = {
		tgp_save_realm_ceremonial_liege_effect = yes
		if = {
			limit = {
				coronation_trigger = yes
				NOT = { has_game_rule = coronation_laws_off }
			}
			coronation_reset_realm_law_effect = yes #we need to reset the crown laws immediately, not wait for the next tick
			set_variable = coronation_discount
			custom_tooltip = court_maintenance.0011.coronation_discount

			# Coronations law
			show_as_tooltip = {
				add_realm_law = uncrowned
			}

			# Artifact for Coronations setup
			create_proper_coronation_artifact_setup = yes
		}
		
		if = {
			limit = {
				exists = cp:councillor_chancellor
			}
			cp:councillor_chancellor = { save_scope_as = messenger }
		}
		else_if = {
			limit = {
				any_councillor = {
					is_kurultai_trigger = yes
				}
			}
			random_councillor = {
				limit = {
					is_kurultai_trigger = yes
				}
				save_scope_as = messenger
			}
		}
		else = {
			random_vassal = { save_scope_as = messenger }
		}
		#House banner
		if = {
			limit = {
				tgp_is_ceremonial_regent_trigger = no
				NOT = {
					any_character_artifact = {
						has_variable = banner_house
						var:banner_house = root.house
					}
				}
			}
			set_variable = banner_from_house
			create_artifact_wall_banner_effect = {
				OWNER = root
				CREATOR = scope:messenger
				TARGET = root.house
			}
			if = { # For artifact portraits
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { save_scope_as = house_banner }
			}
		}
		#Dynasty banner
		if = { 
			limit = {
				tgp_is_ceremonial_regent_trigger = no
				OR = {
					this = dynasty.dynast # Dynasty Head
					any_parent = { # Parent landless
						employer = root
						dynasty = root.dynasty
						this = dynasty.dynast
					}
				}
				NOT = {
					any_character_artifact = {
						has_variable = banner_dynasty
						var:banner_dynasty = root.dynasty
					}
				}
			}
			set_variable = banner_from_dynasty
			create_artifact_wall_banner_effect = {
				OWNER = root
				CREATOR = scope:messenger
				TARGET = root.dynasty
			}
			if = { # For artifact portraits
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { save_scope_as = dynasty_banner }
			}
		}
		#Both
		if = {
			limit = {
				exists = scope:house_banner
				exists = scope:dynasty_banner
			}
			custom_tooltip = court_maintenance.0011.banner_tooltip
		}
		#House
		else_if = {
			limit = { exists = scope:house_banner }
			custom_tooltip = court_maintenance.0011.banner_house_tooltip
		}
		#Dynasty
		else = { custom_tooltip = court_maintenance.0011.banner_dynasty_tooltip }

		# Add extra legitimacy, for the first time they gain the Kingdom title
		add_legitimacy_effect = { LEGITIMACY = major_legitimacy_gain }
	}

	option = {
		name = court_maintenance.0011.coronation
		custom_tooltip = court_maintenance.0011.coronation.tooltip
		trigger = {
			coronation_trigger = yes
			NOR = {
				has_character_flag = had_coronation_prompt
				has_game_rule = coronation_laws_off
				tgp_is_ceremonial_regent_trigger = yes
			}
		}
		if = { #ai should always go straight for it if they can
			limit = { is_ai = yes can_host_activity = activity_coronation }
			ai_attempt_to_host_activity = activity_coronation
		}
		else_if = { #if you already have a crown, a window opened for you sire
			limit = { coronation_has_proper_artifact_trigger = yes is_ai = no }
			open_view_data = {
				view = activity_list_detail_host_window
				data = activity_type:activity_coronation
				player = root
			}
		}
		else = { #Else we start creating a crown
			create_proper_coronation_artifact_creation = yes
		}

		custom_tooltip = court_maintenance.0011.b.tooltip

		ai_chance = { #AI should always start hosting coronations
			base = 100
		}
	}

	option = {
		name = {
			text = court_maintenance.0011.a
			trigger = { tgp_is_ceremonial_regent_trigger = no } # Not 'actual' ruler
		}
		name = {
			text = court_maintenance.0011.a.regent
			trigger = { tgp_is_ceremonial_regent_trigger = yes } # Not 'actual' ruler
		}
		#Open Royal Court
		custom_tooltip = court_maintenance.0011.a.tooltip
		if = {
			limit = {
				coronation_trigger = yes
				tgp_is_ceremonial_regent_trigger = no # Not 'actual' ruler
				NOR = {
					has_character_flag = had_coronation_prompt
					has_game_rule = coronation_laws_off
					government_has_flag = government_is_mandala
				}
			}
			custom_tooltip = court_maintenance.0011.b.tooltip.ach
		}
		open_view_data = {
			view = royal_court
			player = root
		}
	}
	option = {
		name = court_maintenance.0011.b
		custom_tooltip = court_maintenance.0011.b.tooltip
		if = {
			limit = {
				coronation_trigger = yes
				tgp_is_ceremonial_regent_trigger = no # Not 'actual' ruler
				NOR = {
					has_character_flag = had_coronation_prompt
					has_game_rule = coronation_laws_off
					government_has_flag = government_is_mandala
				}
			}
			custom_tooltip = court_maintenance.0011.b.tooltip.ach
		}
	}

	after = {
		add_character_flag = had_coronation_prompt
		if = {
			limit = {
				has_tgp_dlc_trigger = yes
				government_allows = merit
				NOT = { has_government = celestial_government } #Created from cycle
			}
			create_artifact_dynastic_imperial_seal_effect = {
				OWNER = root
				SMITH = root
			}
		}
	}
}

#Dynasty Banner improves
scripted_trigger dynasty_banner_improve_rarity_trigger = {
	trigger_if = {
		limit = { root.dynasty.dynasty_prestige_level = max_dynasty_prestige_level }
		NOT = { rarity = illustrious }
	}
	trigger_else_if = {
		limit = { root.dynasty.dynasty_prestige_level >= high_dynasty_prestige_level }
		NOR = {
			rarity = famed
			rarity = illustrious
		}
	}
	trigger_else = {
		root.dynasty.dynasty_prestige_level >= medium_dynasty_prestige_level
		NOR = {
			rarity = masterwork
			rarity = famed
			rarity = illustrious
		}
	}
}

scripted_effect dynasty_banner_improve_rarity_effect = {
	if = {
		limit = { scope:target.dynasty_prestige_level = max_dynasty_prestige_level }
		set_artifact_rarity = illustrious
	}
	else_if = {
		limit = { scope:target.dynasty_prestige_level >= high_dynasty_prestige_level }
		set_artifact_rarity = famed
	}
	else = { set_artifact_rarity = masterwork }
	hidden_effect = {
		set_artifact_description = artifact_wall_banner_dynasty_description
		clear_artifact_modifiers = yes
		add_scaled_artifact_modifier_grandeur_small_effect = yes
		add_scaled_artifact_modifier_dynasty_prestige_effect = yes
		add_scaled_artifact_modifier_prestige_effect = yes
		add_scaled_artifact_modifier_rulership_effect = yes
	}
}

court_maintenance.0012 = {
	theme = new_royal_court
	type = character_event
	title = court_maintenance.0012.t
	desc = {
		desc = court_maintenance.0012.desc
		first_valid = {
			triggered_desc = {
				trigger = { scope:target.dynasty_prestige_level = max_dynasty_prestige_level }
				desc = court_maintenance.0012.desc_illustrious
			}
			triggered_desc = {
				trigger = { scope:target.dynasty_prestige_level >= high_dynasty_prestige_level }
				desc = court_maintenance.0012.desc_masterwork
			}
			desc = court_maintenance.0012.desc_famed
		}
	}
	left_portrait = {
		character = root
		animation = personality_honorable
	}
	artifact = { # To display the artifact in the event-window
	    target = scope:banner_scope
	    position = lower_right_portrait
	}

	trigger = {
		any_character_artifact = {
			trigger_if = { # To stop errors
				limit = { exists = var:banner_dynasty } # is a dynasty banner
				var:banner_dynasty = root.dynasty # of my dynasty
				dynasty_banner_improve_rarity_trigger = yes # and should be a higher level
			}
			trigger_else = { always = no }
		}
	}

	immediate = {
		dynasty = { save_scope_as = target } # for banner desc
		# save relevant banners (all just in case)
		every_character_artifact = {
			limit = {
				has_variable = banner_dynasty
				var:banner_dynasty = root.dynasty
				dynasty_banner_improve_rarity_trigger = yes
			}
			add_to_list = dynasty_banner_list
		}
		random_in_list = {
			list = dynasty_banner_list
			limit = { is_equipped = yes }
			alternative_limit = { always = yes }
			save_scope_as = banner_scope
			save_scope_as = newly_created_artifact
		}
		every_in_list = {
			list = dynasty_banner_list
			dynasty_banner_improve_rarity_effect = yes
		}
	}

	option = {
		name = court_maintenance.0012.a
	}
}

court_maintenance.0013 = {
	theme = new_royal_court
	type = character_event
	title = court_maintenance.0013.t
	desc = {
		desc = court_maintenance.0013.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:banner_scope.var:banner_commissioner
					scope:banner_scope.var:banner_commissioner != root
				}
				desc = court_maintenance.0013.desc_creator
			}
			desc = court_maintenance.0013.desc_fallback
		}
	}
	left_portrait = {
		character = root
		animation = personality_honorable
	}
	artifact = { # To display the artifact in the event-window
	    target = scope:banner_scope
	    position = lower_right_portrait
	}

	trigger = {
		any_character_artifact = { # Has more than one dynasty banner of own dynasty
			count > 1
			trigger_if = { # To stop errors
				limit = { exists = var:banner_dynasty }
				var:banner_dynasty = root.dynasty
			}
			trigger_else = { always = no }
		}
	}

	immediate = {
		every_character_artifact = { # Fetch all dynasty banners
			limit = {
				has_variable = banner_dynasty
				var:banner_dynasty = root.dynasty
			}
			add_to_list = dynasty_banner_list
		}
		ordered_in_list = { # Save oldest dynasty banner
			list = dynasty_banner_list
			order_by = artifact_age
			save_scope_as = banner_scope
		}
		every_in_list = { # Destroy all but oldest dynasty banner
			list = dynasty_banner_list
			limit = {
				this != scope:banner_scope
			}
			destroy_artifact = this
		}
	}

	option = {
		name = court_maintenance.0013.a
		custom_tooltip = court_maintenance.0013.tt
		
	}
}
