﻿namespace = fp3_scholarship

# ========================= Persian Scholarship Events =========================
# =   Events that relate to sponsoring scholars and innovation in your realm   =
# ==============================================================================
#																			   =
# 0000 – 0999: Hidden decision events										   =
# 1000 – 1999: Scholarship events that don't need a court scholar to trigger   =
# 2000 – 2999: Events that fire when you employ a court scholar				   =
# 8000 – 8999: Research Project events										   =
# 9000 – 9999: Research Project conclusion events							   =
#																			   =
# ==============================================================================

# Sponsor Persian Scholars
fp3_scholarship.0001 = {
	hidden = yes

	trigger = {
		exists = var:number_of_invite_scholars_countdown
	}

	immediate = {
		#Set up a list of provinces, one per duchy in the realm, find all relevant pool characters there
		every_realm_province = {
			if = {
				limit = {
					save_temporary_scope_as = potential_province
					NOT = {
						any_in_list = {
							list = pool_provinces
							county.duchy = scope:potential_province.county.duchy
						}
					}
				}
				add_to_temporary_list = pool_provinces
			}
		}

		every_in_list = {
			list = pool_provinces
			save_temporary_scope_as = pool_province

			every_pool_character = {
				province = scope:pool_province
				limit = {
					learning >= 10
					has_trait = education_learning
					NOT = { has_trait = intellect_bad }
				}
				add_to_temporary_list = potential_scholars
			}
		}

		#Prioritize good scholars
		random_in_list = {
			list = potential_scholars
			limit = { is_alive = yes }
			weight = {
				base = 1
				modifier = {
					add = {
						value = learning
						subtract = 12
						multiply = 5
					}
				}
			}
			save_scope_as = scholar
		}

		#If no one was found, make a new scholar
		if = {
			limit = { NOT = { exists = scope:scholar } }

			# Can be either anywhere from a great to mediocre scholar

			create_character = {
				location = root.capital_province
				age = { 20 70 }
				gender_female_chance = {
					if = {
						limit = {
							faith = { has_doctrine = doctrine_gender_male_dominated }
						}
						add = 0
					}
					else_if = {
						limit = {
							faith = { has_doctrine = doctrine_gender_female_dominated }
						}
						add = 100
					}
					else = {
						add = 50
					}
				}
				culture = root.culture
				faith = root.faith

				random_traits = yes
				diplomacy	= { 2 10 }
				martial		= { 0 8 }
				stewardship = { 2 10 }
				intrigue	= { 2 10 }
				learning	= { 10 18 }
				prowess		= { 0 8 }
				
				random_traits_list = {
					count = { 0 2 }
					scholar = {}
					theologian = {}
					lifestyle_physician = {}
				}

				dynasty = none

				after_creation = {
					remove_any_education_traits_effect = yes
					random_list = { 
						10 = { add_trait = education_learning_1 }
						35 = { add_trait = education_learning_2 }
						35 = { add_trait = education_learning_3 }
						20 = { add_trait = education_learning_4 }
					}
					random = {
						chance = 10
						add_trait = shrewd
					}
					random_list = {
						85 = {}
						20 = {
							trigger = {
								NOR = {
									has_trait = intellect_good_1
									has_trait = intellect_good_2
									has_trait = intellect_good_3
								}
							}
							add_trait = intellect_good_1
						}
						10 = {
							trigger = {
								NOR = {
									has_trait = intellect_good_1
									has_trait = intellect_good_2
									has_trait = intellect_good_3
								}
							}
							add_trait = intellect_good_2
						}
					}
					add_random_tiered_trait_xp_effect = {
						TRAIT = lifestyle_physician
						LEVEL_1 = yes
						LEVEL_3 = yes
					}
					random = {
						chance = 2
						add_trait = depressed_1
					}
					random = {
						chance = 2
						add_trait = reclusive
					}
					random = {
						chance = 2
						add_trait = journaller
					}
				}

				save_scope_as = scholar
			}
		}

		#Add scholar to court
		send_interface_message = {
			type = event_learning_good_with_text
			title = fp3_scholarship.0001.t
			desc = fp3_scholarship.0001.desc
			left_icon = scope:scholar
			add_visiting_courtier = scope:scholar
		}

		#Count down the number of extra scholars and either end the chain or resend this event
		change_variable = {
			name = number_of_invite_scholars_countdown
			subtract = 1
		}

		if = {
			limit = { var:number_of_invite_scholars_countdown = 0 }
			remove_variable = number_of_invite_scholars_countdown
		}
		else = {
			clear_saved_scope = scholar
			trigger_event = {
				id = fp3_scholarship.0001
				days = { 100 200 }
			}
		}
	}
}

##########################################
# 1000 - 1999: No Court Scholar Required #
##########################################

# A Second Aryabhata
# Ask a neighboring Indo-Aryan ruler to send you some scholars

scripted_trigger fp3_scholarship_1000_neighboring_ruler_is_valid = {
	this != root
	is_ai = yes
	culture = {
		OR = {
			has_cultural_pillar = heritage_indo_aryan
			has_cultural_pillar = heritage_dravidian
		}
		NOT = { has_cultural_pillar = ethos_bellicose }
	}
}

# A Second Aryabhata
# Contemplate asking a neighboring Indo-Aryan ruler for some mathematician courtiers
fp3_scholarship.1000 = {
	type = character_event
	title = fp3_scholarship.1000.t
	desc = fp3_scholarship.1000.desc
	theme = education
	left_portrait = {
		character = root
		animation = personality_rational
	}
	lower_left_portrait = scope:nearby_indian_ruler

	cooldown = { years = 10 }

	trigger = {
		has_fp3_dlc_trigger = yes
		NOT = { employs_court_position = court_scholar_court_position }
		can_employ_court_position_type = court_scholar_court_position
		any_neighboring_top_liege_realm_owner = {
			fp3_scholarship_1000_neighboring_ruler_is_valid = yes
		}
	}

	# Find an appropriate neighboring Indian ruler
	immediate = {
		save_scope_as = actor # Required for gift_value
		random_neighboring_top_liege_realm_owner = {
			limit = {
				fp3_scholarship_1000_neighboring_ruler_is_valid = yes
			}
			save_scope_as = nearby_indian_ruler
			save_scope_as = recipient # Required for gift_value
			save_scope_value_as = {
				name = bribe_value
				value = {
					value = gift_value
					divide = 2
				}
			}
		}
	}

	option = { # Ask nicely
		name = fp3_scholarship.1000.a
		duel = {
			skill = diplomacy
			target = scope:nearby_indian_ruler
			50 = { 
				desc = fp3_scholarship.1000.duel_success
				min = 15
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				opinion_modifier = {
					who = scope:nearby_indian_ruler
					opinion_target = root
					multiplier = 1.5
					min = -49
				}
				send_interface_toast = {
					title = fp3_scholarship.1000.duel_success
					left_icon = scope:nearby_indian_ruler
					custom_tooltip = fp3_scholarship.1000.duel_success_tt
				}
				trigger_event = {
					id = fp3_scholarship.1001
					days = { 7 14 }
				}
				hidden_effect = {
					add_opinion = {
						target = scope:nearby_indian_ruler
						modifier = grateful_opinion
						opinion = 20
					}
				}
			}
			50 = {
				desc = fp3_scholarship.1000.duel_failure
				min = 15
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				opinion_modifier = {
					who = scope:nearby_indian_ruler
					opinion_target = root
					multiplier = -1.5
					min = -49
				}
				send_interface_toast = {
					title = fp3_scholarship.1000.duel_failure
					left_icon = scope:nearby_indian_ruler
					reverse_add_opinion = {
						target = scope:nearby_indian_ruler
						modifier = fp3_disrespected_me_opinion
					}
				}
				hidden_effect = {
					add_opinion = {
						target = scope:nearby_indian_ruler
						modifier = disappointed_opinion
						opinion = -10
					}
				}
			}
		}
		stress_impact = {
			paranoid = medium_stress_impact_gain
			arrogant = minor_stress_impact_gain
			scholar = minor_stress_impact_loss
		}
		ai_chance = {
			base = 10 #We don't want too many Indian mathematicians in Persia from this event
			modifier = {
				factor = 5
				has_trait = scholar
			}
		}
	}

	option = { # Offer some gold
		name = fp3_scholarship.1000.b
		stress_impact = {
			greedy = medium_stress_impact_gain
			arrogant = minor_stress_impact_gain
			paranoid = medium_stress_impact_gain
		}
		remove_short_term_gold = scope:bribe_value
		custom_tooltip = fp3_scholarship.1000.duel_success_tt
		reverse_add_opinion = {
			target = scope:nearby_indian_ruler
			modifier = grateful_opinion
			opinion = 20
		}
		trigger_event = {
			id = fp3_scholarship.1001
			days = { 7 14 }
		}
		stress_impact = {
			greedy = medium_stress_impact_gain
			arrogant = minor_stress_impact_gain
			scholar = minor_stress_impact_loss
		}
		ai_chance = {
			base = 10
			modifier = {
				factor = 0
				has_trait = greedy
			}
		}
	}

	option = { # Focus on improving local mathematics
		name = fp3_scholarship.1000.c
		remove_treasury_or_gold = medium_treasury_or_gold_value
		custom_tooltip = fp3_scholarship.1000.native_tt
		add_character_modifier = {
			modifier = fp3_promoted_persian_mathematics_modifier
			years = 10
		}
		hidden_effect = {
			create_character = {
				location = root.capital_province
				template = scholar_character
				save_scope_as = mathematician
			}
			
			send_interface_message = {
				type = event_learning_good_with_text
				title = fp3_scholarship.1001.t
				desc = fp3_scholarship.1001.native.desc
				left_icon = scope:mathematician
				add_courtier = scope:mathematician
			}
		}
		stress_impact = {
			greedy = medium_stress_impact_gain
			arrogant = minor_stress_impact_gain
			scholar = minor_stress_impact_loss
		}
		ai_chance = {
			base = 10
			modifier = {
				factor = 0
				has_trait = greedy
			}
		}
	}
	option = { # Complete Opt Out
		name = fp3_scholarship.1000.d
		stress_impact = {
			scholar = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
		}
	}
}

scripted_effect fp3_scholarship_1001_create_indian_mathematician_effect = {
	create_character = {
		location = root.capital_province
		age = { 20 70 }
		gender_female_chance = {
			if = {
				limit = {
					scope:nearby_indian_ruler.faith = { has_doctrine = doctrine_gender_male_dominated }
				}
				add = 0
			}
			else_if = {
				limit = {
					scope:nearby_indian_ruler.faith = { has_doctrine = doctrine_gender_female_dominated }
				}
				add = 100
			}
			else = {
				add = 50
			}
		}
		culture = scope:nearby_indian_ruler.culture
		faith = scope:nearby_indian_ruler.faith

		random_traits_list = {
			count = 3
			chaste = {}
			content = {}
			ambitious = {}
			diligent = {}
			stubborn = {}
			forgiving = {}
			shy = {}
			just = {}
			patient = {}
			cynical = {}
			craven = {}
		}
		random_traits = no
		diplomacy	= { 2 10 }
		martial		= { 0 8 }
		stewardship = { 2 10 }
		intrigue	= { 0 8 }
		learning	= { 8 14 }
		prowess		= { 0 8 }

		dynasty = none

		after_creation = {
			remove_any_education_traits_effect = yes
			random_list = { 
				10 = { add_trait = education_learning_2 }
				30 = { add_trait = education_learning_3 }
				10 = { add_trait = education_learning_4 }
			}
			random = {
				chance = 25
				add_trait = scholar
			}
			random = {
				chance = 10
				add_trait = shrewd
			}
			random_list = {
				85 = {}
				20 = {
					trigger = {
						NOR = {
							has_trait = intellect_good_1
							has_trait = intellect_good_2
							has_trait = intellect_good_3
						}
					}
					add_trait = intellect_good_1
				}
				10 = {
					trigger = {
						NOR = {
							has_trait = intellect_good_1
							has_trait = intellect_good_2
							has_trait = intellect_good_3
						}
					}
					add_trait = intellect_good_2
				}
			}
			random = {
				chance = 2
				add_trait = depressed_1
			}
			random = {
				chance = 2
				add_trait = reclusive
			}
			random = {
				chance = 5
				add_trait = hashishiyah
			}
			random = {
				chance = 2
				add_trait = journaller
			}
		}

		save_temporary_scope_as = mathematician
	}
	send_interface_message = {
		type = event_learning_good_with_text
		title = fp3_scholarship.1001.t
		desc = fp3_scholarship.1001.desc
		left_icon = scope:mathematician
		add_courtier = scope:mathematician
	}
}

# 1-2 mathematician Indo-Aryan courtiers arrive
fp3_scholarship.1001 = {
	hidden = yes

	trigger = {
		exists = scope:nearby_indian_ruler # Sanity check
	}

	immediate = {
			fp3_scholarship_1001_create_indian_mathematician_effect = yes
			fp3_scholarship_1001_create_indian_mathematician_effect = yes
	}
}

scripted_trigger fp3_scholarship_1010_is_valid_magus_county = {
	county.holder = { #Holder must be ROOT or a direct vassal of ROOT in order for root to grant protection
		OR = {
			this = root
			liege ?= root
		}
	}
	faith = {
		religion_tag = zoroastrianism_religion
		faith_hostility_level = {
			target = root.faith
			value > faith_astray_level
		}
	}
	# Don't target repeat counties
	NOT = { has_variable = cannot_be_converted_by_value }
}

# The Lone Magus
# You encounter an extant fire temple. Do you ignore it, destroy it, or recruit the Magus?
#Travel Event
fp3_scholarship.1010 = {
	type = character_event
	title = fp3_scholarship.1010.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					# Consider the zoroastrian faith "dying" if it has no religious head of faith
					NOT = { exists = scope:magus_faith.religious_head }
				}
			desc = fp3_scholarship.1010.desc_dying_faith
			}
		desc = fp3_scholarship.1010.desc_living_faith
		}
	}
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:magus
		animation = beg
	}
	theme = travel
	override_background = {
		reference = fp3_zoroastrian_temple
	}

	cooldown = { years = 10 }

	trigger = {
		has_fp3_dlc_trigger = yes
		NOT = { employs_court_position = court_scholar_court_position }
		can_employ_court_position_type = court_scholar_court_position
		faith = {
			NOT = { religion_tag = zoroastrianism_religion }
		}
		location = { fp3_scholarship_1010_is_valid_magus_county = yes }
		NOT = {
			has_character_modifier = fp3_denied_religious_protection_modifier
		}
	}

	immediate = {
		# Grab scopes for the event
		location = {
			save_scope_as = magus_location
			county = { save_scope_as = magus_county }
			faith = { save_scope_as = magus_faith }
		}
		create_character = {
			location = scope:magus_location
			age = { 30 70 }
			gender_female_chance = {
				if = {
					limit = {
						scope:magus_faith = { has_doctrine = doctrine_clerical_gender_male_only }
					}
					add = 0
				}
				else_if = {
					limit = {
						scope:magus_faith = { has_doctrine = doctrine_clerical_gender_female_only }
					}
					add = 100
				}
				else = {
					add = 50
				}
			}
			culture = scope:magus_county.culture
			faith = scope:magus_county.faith

			trait = brave
			trait = devoted
			random_traits_list = {
				count = 2
				zealous = {}
				chaste = {}
				diligent = {}
				stubborn = {}
				just = {}
				patient = {}
				impatient = {}
				honest = {}
				temperate = {}
				humble = {}
				calm = {}
				wrathful = {}
				forgiving = {}
			}
			random_traits = no
			diplomacy	= { 2 10 }
			martial		= { 0 8 }
			stewardship = { 2 10 }
			intrigue	= { 0 8 }
			learning	= { 10 16 }
			prowess		= { 0 8 }
	
			dynasty = none
	
			after_creation = {
				remove_any_education_traits_effect = yes
				random_list = { 
					5 = { add_trait = education_learning_2 }
					30 = { add_trait = education_learning_3 }
					15 = { add_trait = education_learning_4 }
				}
				random = {
					chance = 15
					add_trait = scholar
				}
				random = {
					chance = 60
					add_trait = theologian
				}
				random = {
					chance = 10
					add_trait = shrewd
				}
				random_list = {
					85 = {}
					20 = {
						trigger = {
							NOR = {
								has_trait = intellect_good_1
								has_trait = intellect_good_2
								has_trait = intellect_good_3
							}
						}
						add_trait = intellect_good_1
					}
					10 = {
						trigger = {
							NOR = {
								has_trait = intellect_good_1
								has_trait = intellect_good_2
								has_trait = intellect_good_3
							}
						}
						add_trait = intellect_good_2
					}
				}
				random = {
					chance = 5
					add_trait = depressed_1
				}
				random = {
					chance = 90
					add_trait = celibate
				}
				random = {
					chance = 25
					add_trait = infertile
				}
				random = {
					chance = 2
					add_trait = contrite
				}
				random = {
					chance = 5
					add_trait = reclusive
				}
			}
	
			save_scope_as = magus
		}
	}
	option = { # Deny the request
		name = fp3_scholarship.1010.a
		add_character_modifier = {
			modifier = fp3_denied_religious_protection_modifier
			years = 10
		}
		scope:magus = {
			death = {
				death_reason = death_execution
				killer = root
			}
		}
		stress_impact = {
			compassionate = medium_stress_impact_gain
			just = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
			generous = minor_stress_impact_gain
			zealous = minor_stress_impact_loss
			callous = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.25
				has_trait = just
			}
			modifier = {
				factor = 0
				has_trait = compassionate
			}
		}
	}

	option = { # Gain the magus as a courtier
		name = fp3_scholarship.1010.b
		trigger = {
			is_ai = no
		}
		add_courtier = scope:magus
		custom_tooltip = fp3_scholarship.1010.b.tt
		scope:magus_county = {
			set_variable = {
				name = cannot_be_converted_by_value
				value = root
				years = 20
			}
		}
		scope:magus = {
			set_variable = {
				name = cannot_be_converted_by_value
				value = root
				years = 100
			}
			add_character_flag = {
				flag = blocked_from_leaving
				years = 10
			}
		}
		stress_impact = {
			zealous = medium_stress_impact_gain
			callous = minor_stress_impact_gain
			forgiving = minor_stress_impact_loss
			compassionate = minor_stress_impact_loss
		}
	}

	option = { # Just Leave
		name = fp3_scholarship.1010.c
		add_piety = minor_piety_loss
		scope:magus = {
			silent_disappearance_effect = yes
		}
		ai_chance = {
			base = 100
		}
	}
}

########################################
# 2000 - 2999: Court Scholar Required  #
########################################

# Worth Its Weight
# Requisition paper for your court scholar?
fp3_scholarship.2000 = {
	type = character_event
	title = fp3_scholarship.2000.t
	desc = fp3_scholarship.2000.desc
	theme = education
	left_portrait = {
		character = scope:court_scholar
		animation = reading
	}

	cooldown = { years = 5 }

	trigger = {
		# Don't give the court scholar multiple paper surpluses
		any_court_position_holder = {
			type = court_scholar_court_position
			NOT = { has_character_modifier = fp3_paper_surplus_modifier }
		}
		# Paper should be abundant by the late medieval era
		culture = { NOT = { has_cultural_era_or_later = culture_era_late_medieval } }
	}

	immediate = {
		random_court_position_holder = {
			type = court_scholar_court_position
			save_scope_as = court_scholar
		}
	}

	option = { # Requisition the paper
		name = fp3_scholarship.2000.a
		capital_county = {
			change_county_control = -25
		}
		scope:court_scholar = {
			add_character_modifier = {
				modifier = fp3_paper_surplus_modifier
				years = 10
			}
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 10
			}
		}
		stress_impact = {
			just = minor_stress_impact_gain
			compassionate = medium_stress_impact_gain
			generous = medium_stress_impact_gain
		}
		ai_chance = {
			base = 0
		}
	}

	option = { # Import the Paper
		name = fp3_scholarship.2000.b
		remove_treasury_or_gold = medium_treasury_or_gold_value
		scope:court_scholar = {
			add_character_modifier = {
				modifier = fp3_paper_surplus_modifier
			}
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 15
			}
		}
		stress_impact = {
			greedy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_greed = -1
			}
		}
	}

	option = { # We cannot afford it
		name = fp3_scholarship.2000.c
		scope:court_scholar = {
			add_character_modifier = {
				modifier = fp3_paper_shortage_modifier
				years = 10
			}
			add_opinion = {
				target = root
				modifier = disappointed_opinion
				opinion = -15
			}
		}
		stress_impact = {
			diligent = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_energy = -0.5
			}
		}
	}
}

# Liquid Wisdom
# Your court scholar is drinking with a foreign scholar, do you stop them in the name of piety?
fp3_scholarship.2010 = {
	type = character_event
	title = fp3_scholarship.2010.t
	desc = fp3_scholarship.2010.desc
	theme = education
	override_background = {
		reference = tavern
	}
	left_portrait = {
		character = scope:court_scholar
		animation = thinking
	}
	right_portrait = {
		character = scope:foreign_scholar
		animation = toast
	}

	cooldown = { years = 10 }

	trigger = {
		faith = { trait_is_sin = drunkard }
		any_court_position_holder = {
			type = court_scholar_court_position
			NOT = { has_trait = zealous }
		}
	}

	immediate = {
		random_court_position_holder = {
			type = court_scholar_court_position
			save_scope_as = court_scholar
		}
		# Find a random faraway county to pull a character from
		random_county_in_region = {
			region = world_india
			limit = {
				faith = {
					NOT = { trait_is_sin = drunkard }
				}
			}
			save_scope_as = foreign_county
		}
		create_character = {
			age = { 35 50 }
			random_traits_list = {
				count = 1
				education_learning_2 = {}
				education_learning_3 = {}
				education_learning_4 = {}
			}
			random_traits = yes
			location = root.capital_province
			faith = scope:foreign_county.faith
			culture = scope:foreign_county.culture
			gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
			learning = {
				min_template_high_skill
				max_template_high_skill
			}
			save_scope_as = foreign_scholar
		}
	}

	option = { # Stop court scholar
		name = fp3_scholarship.2010.a
		add_piety = minor_piety_gain
		reverse_add_opinion = {
			target = scope:court_scholar
			modifier = fp3_killjoy_opinion
			opinion = -15
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_zeal = 1
			}
		}
	}

	option = { # Let them continue drinking
		name = fp3_scholarship.2010.b
		add_piety = minor_piety_loss
		random = {
			chance = 50
			scope:court_scholar = {
				add_learning_skill = 1
			}
		}
		scope:court_scholar = {
			add_character_modifier = {
				modifier = fp3_liquid_wisdom_modifier
				years = 5
			}
		}
		stress_impact = {
			zealous = medium_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_zeal = -1
			}
		}
	}

	after = {
		scope:foreign_scholar = { silent_disappearance_effect = yes }
	}
}

# Morbid Insights
# Give a prisoner to your court scholar to experiment on?
fp3_scholarship.2020 = {
	type = character_event
	title = fp3_scholarship.2020.t
	desc = fp3_scholarship.2020.desc
	theme = prison
	left_portrait = {
		character = scope:court_scholar
		animation = thinking
	}
	right_portrait = {
		character = scope:specimen
		animation = fear
	}

	cooldown = { years = 10 }

	trigger = {
		any_prisoner = {
			is_adult = yes
		}
		any_court_position_holder = {
			type = court_scholar_court_position
			ai_compassion < very_high_positive_ai_value # A compassionate person would not endanger another's life
		}
	}

	immediate = {
		random_court_position_holder = {
			type = court_scholar_court_position
			save_scope_as = court_scholar
			assign_quirk_effect = yes
		}
		random_prisoner = {
			limit = { is_adult = yes }
			# Try to pull an important person; this event is more interesting if the prisoner isn't Guy McNobody
			weight = {
				base = 1
				modifier = {
					exists = dynasty
					add = dynasty.dynasty_prestige_level
				}
			}
			save_scope_as = specimen
		}
	}

	option = { # Allow the experimentation
		name = fp3_scholarship.2020.a
		random_list = { # 40% chance of prisoner death
			3 = { add_learning_skill = 2 }
			2 = {
				scope:specimen = {
					death = {
						death_reason = death_treatment
						killer = scope:court_scholar
					}
				}
			}
		}
		scope:court_scholar = {
			add_character_modifier = {
				modifier = fp3_morbid_insights_modifier
				years = 20
			}
		}
		stress_impact = {
			compassionate = major_stress_impact_gain
			sadistic = minor_stress_impact_loss
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_compassion = -1
			}
			modifier = {
				NOT = { has_execute_reason = scope:specimen }
				ai_honor >= high_positive_ai_value
				add = -50
			}
		}
	}

	option = { # Forbid experimentation
		name = fp3_scholarship.2020.b
		reverse_add_opinion = {
			target = scope:court_scholar
			modifier = disappointed_opinion
			opinion = -15
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_compassion = 1
			}
		}
	}
}

#Treatice Published (Your Scholar is levelling up)
#By Henrik Lohmander
fp3_scholarship.2030 = {
	type = character_event
	title = fp3_scholarship.2030.t
	desc = fp3_scholarship.2030.desc
	theme = education
	left_portrait = {
		character = scope:court_scholar
		animation = reading
	}
	override_background = {
		reference = bp2_study_indian
	}

	cooldown = { years = 10 } #This will keep happening with regular intervals once the cooldown is over, if you keep a scholar on for long enough they will be increasingly learned

	trigger = {
		any_court_position_holder = {
			type = court_scholar_court_position
			is_available_healthy_adult = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 1
			any_realm_province = {
				is_higher_studies_location = yes
			}
		}
	}

	immediate = {
		random_court_position_holder = {
			type = court_scholar_court_position
			save_scope_as = court_scholar
		}
		random_list = {
			33 = {
				trigger = {
					scope:court_scholar =  {
						NOT = { has_trait = scholar }
						learning > 15
					}
				}
				save_scope_value_as = {
					name = personal_development_time
					value = flag:scholar_trait
				}
			}
			33 = {
				trigger = {
					scope:court_scholar =  {
						NOT = { has_trait = theologian }
						learning > 20
					}
				}
				save_scope_value_as = {
					name = personal_development_time
					value = flag:theologian_trait
				}
			}
			33 = {
				save_scope_value_as = {
					name = personal_development_time
					value = flag:learning
				}
			}
			33 = {
				trigger = {
					any_realm_province = {
						is_higher_studies_location = yes
					}
					scope:court_scholar = {
						has_education_learning_trigger = yes
						NOT = { has_trait = education_learning_5 }
					}
				}
				save_scope_value_as = {
					name = personal_development_time
					value = flag:learning_education
				}
			}
		}
	}

	option = {
		name = fp3_scholarship.2030.a
		switch = {
			trigger = scope:personal_development_time
			flag:learning = {
				scope:court_scholar = {
					add_learning_skill = 2
				}
			}
			flag:theologian_trait = {
				scope:court_scholar = {
					add_trait = theologian
				}
			}
			flag:scholar_trait = {
				scope:court_scholar = {
					add_trait = scholar
				}
			}
			flag:learning_education = {
				custom_tooltip = fp3_scholarship.2030.tt
				scope:court_scholar = {
					switch = {
						trigger = has_trait
						education_learning_1 = {
							remove_trait = education_learning_1
							add_trait_force_tooltip = education_learning_2
						}
						education_learning_2 = { 
							remove_trait = education_learning_2
							add_trait_force_tooltip = education_learning_3
						}
						education_learning_3 = {
							remove_trait = education_learning_3
							add_trait_force_tooltip = education_learning_4
						}
						education_learning_4 = { 
							remove_trait = education_learning_4
							add_trait_force_tooltip = education_learning_5
						}
					}
				}
			}
		}
	}
}

##################################
# 8000 - 8999: Innovation Events #
##################################

# Determines whether the research project was a success or failure, given $SUCCESS_CHANCE$
scripted_effect fp3_research_completion_effect = {
	# For loc
	if = {
		limit = { NOT = { exists = scope:court_scholar } }
		random_court_position_holder = {
			type = court_scholar_court_position
			save_scope_as = court_scholar
		}
	}
	random_list = {
		65 = { # Failure
			modifier = {
				add = {
					value = 0
					subtract = $SUCCESS_CHANCE$
				} 
			}
			custom_tooltip = fp3_scholarship.8999.tt_failure
			trigger_event = fp3_scholarship.9000
		}
		34 = { # Regular Success
			modifier = { 
				add = {
					value = $SUCCESS_CHANCE$
					multiply = 0.75
				}
			}
			custom_tooltip = fp3_scholarship.8999.tt_success
			trigger_event = fp3_scholarship.9100
		}
		1 = { # Critical Success: Unlock Innovation
			trigger = {
				trigger_if = {
					limit = {
						NOT = { culture = { has_innovation = fp3_innovation_mural_sextant } }
					}
					culture = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				trigger_if = {
					limit = {
						NOT = { culture = { has_innovation = fp3_innovation_fritware } }
					}
					culture = { has_cultural_era_or_later = culture_era_late_medieval }
				}
			}
			modifier = {
				add = {
					value = $SUCCESS_CHANCE$
					multiply = 0.25
				}
			}
			custom_tooltip = fp3_scholarship.8999.tt_crit_success
			trigger_event = fp3_scholarship.9200
		}
	}
}

scripted_effect add_research_cooldown_effect = {
	add_character_flag = {
		flag = fund_research_inspiration_event_cooldown
		days = { 30 60 }
	}
}

scripted_trigger has_no_research_event_cooldown = {
	NOT = { has_character_flag = fund_research_inspiration_event_cooldown }
}

# Scholar announces initial plans to begin a research project
fp3_scholarship.8000 = {
	type = character_event
	title = fp3_scholarship.8000.t
	desc = fp3_scholarship.8000.desc
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:court_scholar
		animation = reading
	}
	theme = education
	override_background = {
		reference = bp2_university
	}

	cooldown = {
		years = 5
	}

	immediate = {
		random_court_position_holder = {
			type = court_scholar_court_position
			limit = {
				exists = inspiration
				is_available_healthy_adult = yes
			}
			save_scope_as = court_scholar
		}
	}

	option = {
		name = fp3_scholarship.8000.a
	}
}

# Scholar wants to begin an alchemy inspiration
fp3_scholarship.8500 = {
	type = character_event
	title = fp3_scholarship.8500.t
	desc = fp3_scholarship.8500.desc
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:court_scholar
		animation = reading
	}
	theme = education
	override_background = {
		reference = bp2_university
	}

	cooldown = {
		years = 5
	}

	immediate = {
		random_court_position_holder = {
			type = court_scholar_court_position
			limit = {
				NOT = { exists = inspiration }
				is_available_healthy_adult = yes
			}
			save_scope_as = court_scholar
		}
		scope:court_scholar = {
			save_scope_as = recipient
		}
		save_scope_as = actor
	}

	trigger = {
		has_fp3_dlc_trigger = yes
		has_royal_court = yes #Yes, for this you need both fp3 and Royal Court
		any_court_position_holder = {
			type = court_scholar_court_position
			NOT = { exists = inspiration }
			is_available_healthy_adult = yes
			has_completed_inspiration = no
			learning > 20
		}
	}

	option = {
		name = fp3_scholarship.8000.a
		remove_short_term_gold = fp3_research_base_cost
		custom_tooltip = fp3_scholarship.8500.a.tt
		hidden_effect = {
			scope:court_scholar = {
				create_inspiration = alchemy_inspiration
				fund_inspiration_effect = yes
			}
		}
	}

	option = {
		name = fp3_scholarship.8000.b
		add_prestige = minor_prestige_loss
		reverse_add_opinion = {
			target = scope:court_scholar
			modifier = disappointed_opinion
			opinion = -10
		}
	}
}

# Scholar wants to begin a book inspiration
fp3_scholarship.8600 = {
	type = character_event
	title = fp3_scholarship.8600.t
	desc = fp3_scholarship.8600.desc
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:court_scholar
		animation = throne_room_writer
	}
	theme = education
	override_background = {
		reference = study
	}

	trigger = {
		has_fp3_dlc_trigger = yes
		has_royal_court = yes #Yes, for this you need both fp3 and Royal Court
		any_court_position_holder = {
			type = court_scholar_court_position
			NOT = { exists = inspiration }
			has_completed_inspiration = no
			is_available_healthy_adult = yes
			learning > 20
		}
	}

	cooldown = {
		years = 5
	}

	immediate = {
		random_court_position_holder = {
			type = court_scholar_court_position
			limit = {
				NOT = { exists = inspiration }
				is_available_healthy_adult = yes
			}
			save_scope_as = court_scholar
		}
		scope:court_scholar = {
			save_scope_as = recipient
		}
		save_scope_as = actor
	}

	option = {
		name = fp3_scholarship.8000.a
		show_as_tooltip = { remove_short_term_gold = medium_gold_value } # gold is removed by sponsor_inspiration effect (inside fund_inspiration_effect)
		custom_tooltip = fp3_scholarship.8600.a.tt
		hidden_effect = {
			scope:court_scholar = {
				create_inspiration = {
					type = book_inspiration
					gold = root.medium_gold_value
				}
				fund_inspiration_effect = yes
			}
		}
	}

	option = {
		name = fp3_scholarship.8000.b
		add_prestige = minor_prestige_loss
		reverse_add_opinion = {
			target = scope:court_scholar
			modifier = disappointed_opinion
			opinion = -10
		}
	}
}

# Scholar asks for more texts
fp3_scholarship.8100 = {
	type = character_event
	title = fp3_scholarship.8100.t
	desc = fp3_scholarship.8100.desc
	theme = education
	left_portrait = scope:inspiration_owner

	cooldown = { years = 2 }

	trigger = {
		exists = scope:inspiration
		scope:inspiration_owner = {
			is_available_ai_adult = yes
			has_no_research_event_cooldown = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.5
			scope:inspiration_owner = {
				ai_energy >= medium_positive_ai_value
			}
		}
	}

	immediate = {
		scope:inspiration_owner = {
			add_research_cooldown_effect = yes
		}
	}

	option = { # Furnish the library
		name = fp3_scholarship.8100.a
		if = {
			limit = { exists = scope:inspiration }
			scope:inspiration = {
				invest_gold = 50
			}
		}
		scope:inspiration_owner = {
			change_artifact_quality_effect = { AMOUNT = 6 }
		}
		stress_impact = {
			arrogant = medium_stress_impact_gain
			greedy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 50
			modifier = {
				add = -50
				short_term_gold < 50
			}
		}
	}

	option = { # The library is fine as-is
		name = fp3_scholarship.8100.b
		scope:inspiration_owner = {
			change_artifact_quality_effect = { AMOUNT = -2 }
			add_opinion = {
				target = root
				modifier = refusal_opinion
				opinion = -10
			}
		}
		stress_impact = {
			arrogant = minor_stress_impact_loss
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_greed = 0.75
			}
		}
	}
}

# Roll for project outcome
fp3_scholarship.8999 = {
	hidden = yes

	# Roll for success
	immediate = {
		if = {
			limit = { exists = scope:inspiration_owner }
			scope:inspiration_owner = {
				if = {
					limit = { has_variable = research_success_chance }
					scope:inspiration_sponsor = {
						fp3_research_completion_effect = { SUCCESS_CHANCE = prev.var:research_success_chance }
					}
				}
			}
		}
	}
 }

# Research project was a failure
fp3_scholarship.9000 = {
	type = character_event
	title = fp3_scholarship.9000.t
	desc = fp3_scholarship.9000.desc
	theme = education
	left_portrait = {
		character = scope:inspiration_owner
		animation = worry
	}

	immediate = {
		scope:inspiration_owner = {
			add_character_flag = {
				flag = research_invalidated
				days = 1
			}
		}	
	}

	option = { # These things happen
		name = fp3_scholarship.9000.a
		add_prestige = minor_prestige_loss
	}
}

# Research project minor success
scripted_effect fp3_scholarship_9100_reward_effect = {
	house = {
		add_house_modifier = {
			modifier = $MODIFIER$
			years = 30
		}
	}
}

fp3_scholarship.9100 = {
	type = character_event
	title = fp3_scholarship.9100.t
	desc = fp3_scholarship.9100.desc
	theme = education
	left_portrait = {
		character = scope:inspiration_owner
		animation = personality_honorable
	}

	immediate = {
		hidden_effect = {
			random_list = {
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = civil_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_development_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_development_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = civil_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_building_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_building_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = civil_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_control_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_control_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = civil_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_mathematics_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_mathematics_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = civil_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_taxation_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_taxation_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = military_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_light_warfare_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_light_warfare_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = military_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_heavy_warfare_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_heavy_warfare_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = military_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_cavalry_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_cavalry_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = military_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_siege_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_siege_modifier
					}
				}
				30 = {
					trigger = {
						scope:inspiration_owner = { has_character_flag = military_research }
						house = {
							NOT = { has_house_modifier = fp3_treatise_on_strategy_modifier }
						}
					}
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:fp3_treatise_on_strategy_modifier
					}
				}
				1 = {
					save_scope_value_as = {
						name = researched_house_modifier
						value = flag:prestige
					}
				}
			}
		}
	}

	option = {
		name = fp3_scholarship.9100.a
		ai_chance = {
			base = 100
		}
		switch = {
			trigger = scope:researched_house_modifier
			flag:fp3_treatise_on_development_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_development_modifier }
			}
			flag:fp3_treatise_on_building_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_building_modifier }
			}
			flag:fp3_treatise_on_control_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_control_modifier }
			}
			flag:fp3_treatise_on_mathematics_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_mathematics_modifier }
			}
			flag:fp3_treatise_on_taxation_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_taxation_modifier }
			}
			flag:fp3_treatise_on_light_warfare_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_light_warfare_modifier }
			}
			flag:fp3_treatise_on_heavy_warfare_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_heavy_warfare_modifier }
			}
			flag:fp3_treatise_on_cavalry_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_cavalry_modifier }
			}
			flag:fp3_treatise_on_siege_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_siege_modifier }
			}
			flag:fp3_treatise_on_strategy_modifier = {
				fp3_scholarship_9100_reward_effect = { MODIFIER = fp3_treatise_on_strategy_modifier }
			}
			flag:prestige = {
				custom_tooltip = patron_of_the_sciences
				add_prestige = massive_prestige_value
			}
		}
	}
}

# Research project critical success! New innovation discovered
fp3_scholarship.9200 = {
	type = character_event
	title = fp3_scholarship.9200.t
	desc = fp3_scholarship.9200.desc
	theme = education
	left_portrait = {
		character = scope:inspiration_owner
		animation = happiness
	}

	immediate = {
		play_music_cue = "mx_cue_epic_sacral_moment"
		hidden_effect = {
			random_list = {
				30 = {
					trigger = {
						culture = {
							NOT = { has_innovation = fp3_innovation_mural_sextant }
							has_cultural_era_or_later = culture_era_early_medieval
						}
					}
					save_scope_value_as = {
						name = research_cultural_innovation
						value = flag:fp3_innovation_mural_sextant
					}
				}
				30 = {
					trigger = {
						culture = {
							NOT = { has_innovation = fp3_innovation_fritware }
							has_cultural_era_or_later = culture_era_late_medieval
						}
					}
					save_scope_value_as = {
						name = research_cultural_innovation
						value = flag:fp3_innovation_fritware
					}
				}
			}
		}
	}

	option = {
		name = fp3_scholarship.9200.a
		switch = {
			trigger = scope:research_cultural_innovation
			flag:fp3_innovation_mural_sextant = {
				culture = { add_innovation = fp3_innovation_mural_sextant }
			}
			flag:fp3_innovation_fritware = {
				culture = { add_innovation = fp3_innovation_fritware }
			}
		}
	}
}
