﻿#Education events for children

namespace = childhood_education

####################################################################

#0002: Acquire education points
#1000 = Religious and Cultural Conversion
#9001: Assign education focus
#9100: Remove guardian opinions when guardian/ward relation ends

###################################################################


#Notification event for educator about child switching focus
childhood_education.0004 = {
	type = character_event
	title = childhood_education.0004.t
	desc = {
		desc = childhood_education.0004.opening
		first_valid = {
			triggered_desc = {
				trigger= {
					has_character_flag = matching_education
				}
				desc = childhood_education.0004.desc1
			}
			triggered_desc = {
				trigger= {
					has_character_flag = previously_matching_education
				}
				desc = childhood_education.0004.desc2
			}
			desc = childhood_education.0004.desc_fallback
		}
	}
	theme = education
	left_portrait = scope:ward

	trigger = {
		# PARENTS ARE EXCLUDED FROM THIS NATURALLY, see 00_education_focuses.txt under on_change_from for more info
		scope:ward = {
			has_any_focus = yes # The guardian is not required to actually have one, as there are alternatives. But the ward is.
			OR = {
				has_character_flag = martial_previous_education
				has_character_flag = intrigue_previous_education
				has_character_flag = diplomacy_previous_education
				has_character_flag = learning_previous_education
				has_character_flag = stewardship_previous_education
			}
		}
	}
	on_trigger_fail = { # In case a stone was left unturned
		debug_log = "childhood_education.0004 failed to meet trigger requirements, there is probably a missing prev education flag!"
	}

	immediate = {
		if = {
			limit = {
				OR = {
					has_education_diplomacy_trigger = yes
					has_focus_diplomacy = yes
					diplomacy >= high_skill_rating
				}
				scope:ward = {
					has_focus = education_diplomacy
				}
			}
			add_character_flag = matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_martial_trigger = yes
					has_focus_martial = yes
					martial >= high_skill_rating
				}
				scope:ward = {
					has_focus = education_martial
				}
			}
			add_character_flag = matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_stewardship_trigger = yes
					has_focus_stewardship = yes
					stewardship >= high_skill_rating
				}
				scope:ward = {
					has_focus = education_stewardship
				}
			}
			add_character_flag = matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_intrigue_trigger = yes
					has_focus_intrigue = yes
					intrigue >= high_skill_rating
				}
				scope:ward = {
					has_focus = education_intrigue
				}
			}
			add_character_flag = matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_learning_trigger = yes
					has_focus_learning = yes
					learning >= high_skill_rating
				}
				scope:ward = {
					has_focus = education_learning
				}
			}
			add_character_flag = matching_education
		}
		if = {
			limit = {
				OR = {
					has_education_diplomacy_trigger = yes
					has_focus_diplomacy = yes
					diplomacy >= high_skill_rating
				}
				scope:ward = {
					has_character_flag = diplomacy_previous_education
				}
			}
			add_character_flag = previously_matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_martial_trigger = yes
					has_focus_martial = yes
					martial >= high_skill_rating
				}
				scope:ward = {
					has_character_flag = martial_previous_education
				}
			}
			add_character_flag = previously_matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_stewardship_trigger = yes
					has_focus_stewardship = yes
					stewardship >= high_skill_rating
				}
				scope:ward = {
					has_character_flag = stewardship_previous_education
				}
			}
			add_character_flag = previously_matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_intrigue_trigger = yes
					has_focus_intrigue = yes
					intrigue >= high_skill_rating
				}
				scope:ward = {
					has_character_flag = intrigue_previous_education
				}
			}
			add_character_flag = previously_matching_education
		}
		else_if = {
			limit = {
				OR = {
					has_education_learning_trigger = yes
					has_focus_learning = yes
					learning >= high_skill_rating
				}
				scope:ward = {
					has_character_flag = learning_previous_education
				}
			}
			add_character_flag = previously_matching_education
		}
	}

	option = {
		name = childhood_education.0004.a
	}

	after = {
		remove_character_flag = previously_matching_education
		remove_character_flag = matching_education
		scope:ward = {
			remove_character_flag = martial_previous_education
			remove_character_flag = intrigue_previous_education
			remove_character_flag = diplomacy_previous_education
			remove_character_flag = learning_previous_education
			remove_character_flag = stewardship_previous_education
		}
	}
}

childhood_education.0005 = {
	hidden = yes
	trigger = {
		OR = {
			has_focus = education_diplomacy
			has_focus = education_martial
			has_focus = education_stewardship
			has_focus = education_learning
			has_focus = education_intrigue
		}
	}
	immediate = {
		set_focus = no
	}
}

childhood_education.0008 = {
	hidden = yes
	immediate = {
		remove_character_flag = cp_0001
		remove_character_flag = cp_0002
		remove_character_flag = cp_0003
		remove_character_flag = cp_0004
		remove_character_flag = cp_0005
		remove_character_flag = cp_0006
		remove_character_flag = cp_0007
		remove_character_flag = cp_0008
		remove_character_flag = cp_0009
		remove_character_flag = cp_0010
		remove_character_flag = cp_0011
		remove_character_flag = cp_0012
		remove_character_flag = cp_0013
		remove_character_flag = cp_0014
		remove_character_flag = cp_0015
		remove_character_flag = cp_0016
		remove_character_flag = cp_700
	}
}

childhood_education.0006 = {
	hidden = yes
	trigger = {
		any_relation = {
			type = guardian
			count > 0
		}
	}
	immediate = {
		random_relation = {
			type = guardian
			save_scope_as = educator
		}
		wrap_up_education_effect = yes
	}
}

childhood_education.0007 = {
	hidden = yes
	trigger = {
		OR = {
			has_trait = curious
			has_trait = rowdy
			has_trait = pensive
			has_trait = charming
			has_trait = bossy
		}
	}	
	immediate = {
		remove_variables_and_trait_coming_of_age_effect = yes
		if = {
			limit = {
				NOR = {
					has_education_martial_trigger = yes
					has_education_diplomacy_trigger = yes
					has_education_learning_trigger = yes
					has_education_stewardship_trigger = yes
					has_education_intrigue_trigger = yes
				}
			}
			random_list = {
				10 = {
					add_trait = education_martial_1
				}
				10 = {
					add_trait = education_diplomacy_1
				}
				10 = {
					add_trait = education_learning_1
				}
				10 = {
					add_trait = education_stewardship_1
				}
				10 = {
					add_trait = education_intrigue_1
				}
			}
		}
	}
}



########################
# Religious and Cultural Conversion
# 1000+
# System by Petter Vilberg
########################

childhood_education.1001 = {
	hidden = yes

	trigger = {
		any_relation = {
			type = guardian
			OR = {
				AND = {
					has_relation_flag = {
						target = root
						relation = ward
						flag = convert_faith
					}
					NOT = { faith = root.faith }
				}
				AND = {
					has_relation_flag = {
						target = root
						relation = ward
						flag = convert_culture
					}
					NOT = { culture = root.culture }
				}
			}
		}
	}

	immediate = {
		random_relation = {
			type = guardian
			save_scope_as = guardian
		}
		if = {
			limit = {
				scope:guardian = {
					has_relation_flag = {
						target = root
						relation = ward
						flag = convert_faith
					}
				}
				NOT = { faith = scope:guardian.faith }
			}
			if = {
				limit = {
					has_variable = education_faith_conversion_chance
				}
				change_variable = {
					name = education_faith_conversion_chance
					add = 20
				}
				random = {
					chance = var:education_faith_conversion_chance
					save_temporary_scope_value_as = {
						name = convert_faith
						value = yes
					}
				}
			}
			else = {
				set_variable = {
					name = education_faith_conversion_chance
					value = 0
				}
			}
		}
		if = {
			limit = {
				scope:guardian = {
					has_relation_flag = {
						target = root
						relation = ward
						flag = convert_culture
					}
				}
				NOT = { culture = scope:guardian.culture }
			}
			if = {
				limit = {
					has_variable = education_culture_conversion_chance
				}
				change_variable = {
					name = education_culture_conversion_chance
					add = 10
				}
				random = {
					chance = var:education_culture_conversion_chance
					save_temporary_scope_value_as = {
						name = convert_culture
						value = yes
					}
				}
			}
			else = {
				set_variable = {
					name = education_culture_conversion_chance
					value = 0
				}
			}
		}

		#Check if a conversion event should be fired
		if = {
			limit = {
				exists = scope:convert_faith
				exists = scope:convert_culture
			}

			#Send event about converting both
			trigger_event = {
				id = childhood_education.1011
				days = { 1 100 }
			}
		}
		else_if = {
			limit = {
				exists = scope:convert_faith
			}

			#Send event about converting faith
			trigger_event = {
				id = childhood_education.1021
				days = { 1 100 }
			}
		}
		else_if = {
			limit = {
				exists = scope:convert_culture
			}

			#Send event about converting culture
			trigger_event = {
				id = childhood_education.1031
				days = { 1 100 }
			}
		}
	}
}


#Double conversion
childhood_education.1011 = {
	type = character_event
	title = childhood_education.1011.t
	desc = childhood_education.1011.desc
	theme = education
	left_portrait = scope:guardian

	trigger = {
		scope:guardian = {
			is_alive = yes
		}
		has_relation_guardian = scope:guardian
	}

	immediate = {
		play_music_cue = "mx_cue_faith_conversion"
		save_scope_as = ward
	}

	option = {
		name = childhood_education.1011.a
		set_character_faith = scope:guardian.faith
		set_culture = scope:guardian.culture
		hidden_effect = {
			scope:guardian = {
				remove_relation_flag = {
					target = root
					relation = ward					
					flag = convert_culture
				}
			}
			scope:guardian = {
				trigger_event = childhood_education.1012
			}
		}
	}
}

#Message to Guardian
childhood_education.1012 = {
	type = character_event
	title = childhood_education.1012.t
	desc = childhood_education.1012.desc
	theme = education
	left_portrait = {
		character = scope:ward
		animation = reading
	}

	immediate = {
		play_music_cue = "mx_cue_faith_conversion"
		scope:ward = {
			set_character_faith = scope:guardian.faith
			set_culture = scope:guardian.culture
		}
		hidden_effect = {
			if = {
				limit = {
					exists = scope:ward.father
					scope:ward.father = {
						is_alive = yes
						NOT = { this = root }
					}
				}
				scope:ward.father = {
					save_scope_as = father
					send_interface_message = {
						type = event_childhood_neutral
						title = child_culture_and_faith_converted.parent.message
						left_icon = scope:ward
						right_icon = scope:educator

						show_as_tooltip = {
							scope:ward = {
								set_character_faith = scope:guardian.faith
								set_culture = scope:guardian.culture
							}
						}
					}
				}
			}
			if = {
				limit = {
					exists = scope:ward.mother
					scope:ward.mother = {
						is_alive = yes
						NOT = { this = root }
					}
				}
				scope:ward.mother = {
					save_scope_as = mother
					send_interface_message = {
						type = event_childhood_neutral
						title = child_culture_and_faith_converted.parent.message
						left_icon = scope:ward
						right_icon = scope:educator

						show_as_tooltip = {
							scope:ward = {
								set_character_faith = scope:guardian.faith
								set_culture = scope:guardian.culture
							}
						}
					}
				}
			}
		}
	}

	option = {
		name = childhood_education.1012.a
		trigger = {
			NOT = {
				is_parent_of = scope:ward
			}
		}
	}

	option = {
		name = childhood_education.1012.a
		trigger = {
			is_parent_of = scope:ward
		}
	}
}


#Convert Faith
childhood_education.1021 = {
	type = character_event
	title = childhood_education.1021.t
	desc = childhood_education.1021.desc
	theme = education
	left_portrait = scope:guardian

	trigger = {
		scope:guardian = {
			is_alive = yes
		}
		has_relation_guardian = scope:guardian
	}

	immediate = {
		play_music_cue = "mx_cue_faith_conversion"
		save_scope_as = ward
	}

	option = {
		name = childhood_education.1021.a
		set_character_faith = scope:guardian.faith
		hidden_effect = {
			scope:guardian = {
				trigger_event = childhood_education.1022
			}
		}
	}
}

#Message to Guardian
childhood_education.1022 = {
	type = character_event
	title = childhood_education.1022.t
	desc = childhood_education.1022.desc
	theme = education
	left_portrait = {
		character = scope:ward
		animation = prayer
	}

	immediate = {
		play_music_cue = "mx_cue_faith_conversion"
		show_as_tooltip = {
			scope:ward = {
				set_character_faith = scope:guardian.faith
			}
		}
		hidden_effect = {
			if = {
				limit = {
					exists = scope:ward.father
					scope:ward.father = {
						is_alive = yes
						NOT = { this = root }
					}
				}
				scope:ward.father = {
					save_scope_as = father
					send_interface_message = {
						type = event_childhood_neutral
						title = child_faith_converted.parent.message
						left_icon = scope:ward
						right_icon = scope:educator

						show_as_tooltip = {
							scope:ward = {
								set_character_faith = scope:guardian.faith
							}
						}
					}
				}
			}
			if = {
				limit = {
					exists = scope:ward.mother
					scope:ward.mother = {
						is_alive = yes
						NOT = { this = root }
					}
				}
				scope:ward.mother = {
					save_scope_as = mother
					send_interface_message = {
						type = event_childhood_neutral
						title = child_faith_converted.parent.message
						left_icon = scope:ward
						right_icon = scope:educator

						show_as_tooltip = {
							scope:ward = {
								set_character_faith = scope:guardian.faith
							}
						}
					}
				}
			}
		}
	}

	option = {
		name = childhood_education.1022.a
	}
}


#Convert Culture
childhood_education.1031 = {
	type = character_event
	title = childhood_education.1031.t
	desc = childhood_education.1031.desc
	theme = education
	left_portrait = scope:guardian

	trigger = {
		scope:guardian = {
			is_alive = yes
		}
		has_relation_guardian = scope:guardian
	}

	immediate = {
		play_music_cue = "mx_cue_faith_conversion"
		save_scope_as = ward
	}

	option = {
		name = childhood_education.1031.a
		set_culture = scope:guardian.culture
		hidden_effect = {
			scope:guardian = {
				trigger_event = childhood_education.1032
			}
		}
	}
}

#Message to Guardian
childhood_education.1032 = {
	type = character_event
	title = childhood_education.1032.t
	desc = childhood_education.1032.desc
	theme = education
	left_portrait = {
		character = scope:ward
		animation = thinking
	}

	immediate = {
		play_music_cue = "mx_cue_faith_conversion"
		show_as_tooltip = {
			scope:ward = {
				set_culture = scope:guardian.culture
			}
		}
		hidden_effect = {
			if = {
				limit = {
					exists = scope:ward.father
					scope:ward.father = {
						is_alive = yes
						NOT = { this = root }
					}
				}
				scope:ward.father = {
					save_scope_as = father
					send_interface_message = {
						type = event_childhood_neutral
						title = child_culture_converted.parent.message
						left_icon = scope:ward
						right_icon = scope:educator

						show_as_tooltip = {
							scope:ward = {
								set_culture = scope:guardian.culture
							}
						}
					}
				}
			}
			if = {
				limit = {
					exists = scope:ward.mother
					scope:ward.mother = {
						is_alive = yes
						NOT = { this = root }
					}
				}
				scope:ward.mother = {
					save_scope_as = mother
					send_interface_message = {
						type = event_childhood_neutral
						title = child_culture_converted.parent.message
						left_icon = scope:ward
						right_icon = scope:educator

						show_as_tooltip = {
							scope:ward = {
								set_culture = scope:guardian.culture
							}
						}
					}
				}
			}
		}
	}

	option = {
		name = childhood_education.1032.a
	}
}


#########################
##
## EDUCATION MAINTENANCE
##
#########################

#Assign education focus
childhood_education.9001 = {
	hidden = yes
	immediate = {
		add_character_flag = natural_education_progression
		if = {
			limit = {
				has_education_focus_trigger = no
			}
			set_default_education = yes
		}
	}
}


#To gain education points
childhood_education.9002 = {
	hidden = yes

	immediate = {
		education_point_acquisition_effect = yes
	}
}

#Patronage-focused / scholarly spouse may add points
childhood_education.9003 = {
	hidden = yes

	immediate = {
		add_patronage_spouse_education_points_effect = yes
	}
}

# Pedagogy effect
childhood_education.9004 = {
	hidden = yes

	trigger = {
		age >= 8
		any_relation = {
			type = guardian
			has_perk = pedagogy_perk
		}
	}

	immediate = {
		save_scope_as = child
		random_relation = {
			type = guardian
			limit = {
				has_perk = pedagogy_perk
			}
			save_scope_as = guardian
		}
		if = {
			limit = {
				age >= 10
				NOR = {
					has_relation_friend = scope:guardian
					has_relation_best_friend = scope:guardian
					has_relation_rival = scope:guardian
					has_relation_nemesis = scope:guardian
				}
			}
			random = {
				chance = 20
				compare_modifier = {
					trigger = {
						age >= 14
					}
					value = age
					multiplier = 2
				}
				save_scope_value_as = {
					name = set_friendship
					value = yes
				}
			}
		}
		random_list = {
			80 = { }
			4 = { # Gain Diplomacy
				send_interface_toast = {
					title = childhood_education.9004.child_message
					left_icon = scope:guardian
					
					add_diplomacy_skill = 1
					if = {
						limit = {
							exists = scope:set_friendship
						}
						set_relation_friend = { reason = friend_pedagogy target = scope:guardian }
					}
				}
				scope:guardian = {
					send_interface_message = {
						type = event_diplomacy_good
						title = childhood_education.9004.guardian_message
						left_icon = scope:child

						show_as_tooltip = {
							scope:child = {
								add_diplomacy_skill = 1
							}
							if = {
								limit = {
									exists = scope:set_friendship
								}
								set_relation_friend = { reason = friend_pedagogy target = scope:child }
							}
						}
					}
				}
			}
			4 = { # Gain Martial
				send_interface_toast = {
					title = childhood_education.9004.child_message
					left_icon = scope:guardian

					add_martial_skill = 1
					if = {
						limit = {
							exists = scope:set_friendship
						}
						set_relation_friend = { reason = friend_pedagogy target = scope:guardian }
					}
				}
				scope:guardian = {
					send_interface_message = {
						type = event_martial_good
						title = childhood_education.9004.guardian_message
						left_icon = scope:child

						show_as_tooltip = {
							scope:child = {
								add_martial_skill = 1
							}
							if = {
								limit = {
									exists = scope:set_friendship
								}
								set_relation_friend = { reason = friend_pedagogy target = scope:child }
							}
						}
					}
				}
			}
			4 = { # Gain Learning
				send_interface_toast = {
					title = childhood_education.9004.child_message
					left_icon = scope:guardian

					add_learning_skill = 1
					if = {
						limit = {
							exists = scope:set_friendship
						}
						set_relation_friend = { reason = friend_pedagogy target = scope:guardian }
					}
				}
				scope:guardian = {
					send_interface_message = {
						type = event_learning_good
						title = childhood_education.9004.guardian_message
						left_icon = scope:child

						show_as_tooltip = {
							scope:child = {
								add_learning_skill = 1
							}
							if = {
								limit = {
									exists = scope:set_friendship
								}
								set_relation_friend = { reason = friend_pedagogy target = scope:child }
							}
						}
					}
				}
			}
			4 = { # Gain Intrigue
				send_interface_toast = {
					title = childhood_education.9004.child_message
					left_icon = scope:guardian

					add_intrigue_skill = 1
					if = {
						limit = {
							exists = scope:set_friendship
						}
						set_relation_friend = { reason = friend_pedagogy target = scope:guardian }
					}
				}
				scope:guardian = {
					send_interface_message = {
						type = event_intrigue_good
						title = childhood_education.9004.guardian_message
						left_icon = scope:child

						show_as_tooltip = {
							scope:child = {
								add_intrigue_skill = 1
							}
							if = {
								limit = {
									exists = scope:set_friendship
								}
								set_relation_friend = { reason = friend_pedagogy target = scope:child }
							}
						}
					}
				}
			}
			4 = { # Gain Stewardship
				send_interface_toast = {
					title = childhood_education.9004.child_message
					left_icon = scope:guardian

					add_stewardship_skill = 1
					if = {
						limit = {
							exists = scope:set_friendship
						}
						set_relation_friend = { reason = friend_pedagogy target = scope:guardian }
					}
				}
				scope:guardian = {
					send_interface_message = {
						type = event_stewardship_good
						title = childhood_education.9004.guardian_message
						left_icon = scope:child

						show_as_tooltip = {
							scope:child = {
								add_stewardship_skill = 1
							}
							if = {
								limit = {
									exists = scope:set_friendship
								}
								set_relation_friend = { reason = friend_pedagogy target = scope:child }
							}
						}
					}
				}
			}
			2 = { # Gain Prowess
				send_interface_toast = {
					title = childhood_education.9004.child_message
					left_icon = scope:guardian
					
					add_prowess_skill = 1
					if = {
						limit = {
							exists = scope:set_friendship
						}
						set_relation_friend = { reason = friend_pedagogy target = scope:guardian }
					}
				}
				scope:guardian = {
					send_interface_message = {
						type = event_prowess_good
						title = childhood_education.9004.guardian_message
						left_icon = scope:child

						show_as_tooltip = {
							scope:child = {
								add_prowess_skill = 1
							}
							if = {
								limit = {
									exists = scope:set_friendship
								}
								set_relation_friend = { reason = friend_pedagogy target = scope:child }
							}
						}
					}
				}
			}
		}
	}
}

# One More Hill effect
childhood_education.9010 = {
	hidden = yes
	cooldown = { years = 1 }

	trigger = {
		NOT = { has_variable = childhood_education_9010_cooldown }
		age >= 8
		any_parent = { has_perk = just_one_more_hill_perk }
	}

	immediate = {
		save_scope_as = child
		random_parent = {
			limit = {
				has_perk = just_one_more_hill_perk	
			}
			save_scope_as = parent
		}
		random_list = {
			25 = { }
			25 = { # Just Prowess
				scope:parent = {
					send_interface_toast = {
						type = event_prowess_good
						title = childhood_education.9010.parent_message
						left_icon = scope:child
						
						show_as_tooltip = {
							scope:child = {
								add_prowess_skill = 1
							}
						}
					}
				}
				send_interface_toast = {
					type = event_prowess_good
					title = childhood_education.9010.child_message
					left_icon = scope:parent
					
					add_prowess_skill = 1
				}
				set_variable = {
					name = childhood_education_9010_cooldown
					years = 2
				}
			}	
			50 = { # Prowess and Traveler XP
				scope:parent = {
					send_interface_toast = {
						type = event_prowess_good
						title = childhood_education.9010.parent_message
						left_icon = scope:child
						
						show_as_tooltip = {
							scope:child = {
								add_prowess_skill = 1
								if = {
									limit = {
										NOT = { has_trait = lifestyle_traveler }
									}
									add_trait = lifestyle_traveler
								}
								else = {
									add_trait_xp = {
										trait = lifestyle_traveler
										track = travel
										value = 3
									}
								}
							}
						}
					}
				}
				send_interface_toast = {
					type = event_prowess_good
					title = childhood_education.9010.child_message
					left_icon = scope:parent
					
					add_prowess_skill = 1
					if = {
						limit = {
							NOT = { has_trait = lifestyle_traveler }
						}
						add_trait = lifestyle_traveler
					}
					else = {
						add_trait_xp = {
							trait = lifestyle_traveler
							track = travel
							value = 3
						}
					}
				}
				set_variable = {
					name = childhood_education_9010_cooldown
					years = 2
				}
			}
		}
	}
}


#To check if character needs education boost or not
childhood_education.9103 = {
	hidden = yes

	trigger = {
		NOT = { has_character_flag = natural_education_progression }
	}

	immediate = {
		set_variable = {
			name = years_missing_from_education
			value = root.age
		}
		while = {
			limit = {
				var:years_missing_from_education > 6
			}
			education_point_acquisition_effect = yes
			change_variable = {
				name = years_missing_from_education
				add = -1
			}
		}
		add_character_flag = natural_education_progression
		remove_variable = years_missing_from_education
	}
}
