﻿child_personality_gain = {
	trigger = {
		is_adult = no
		number_of_personality_traits < personality_trait_limit
		# AGOT Added
		is_human = yes
		NOT = { has_character_flag = agot_has_canon_traits }
	}
	random_events = {
		100 = child_personality.0001 # Dare Event
		100 = child_personality.0002 # Pillory Event
		100 = child_personality.0003 # Gift Event
		100 = child_personality.0004 # Lot Event
		100 = child_personality.0005 # Pushing Event
		100 = child_personality.0006 # Crush Event
		100 = child_personality.0007 # Wilderness Event
		100 = child_personality.0008 # Feast Event
		100 = child_personality.0009 # Preacher Event
		100 = child_personality.0010 # Riding Event
		100 = child_personality.0011 # Cook Event
		100 = child_personality.0012 # Worldway event
		100 = child_personality.0013 # Rules Event
		100 = child_personality.0014 # Friend's Crush event
		100 = child_personality.0015 # Scary stories event
		100 = child_personality.0016 # Scary stories event
		
		
		#100 = child_personality.7050 # just, greedy, callous
		#100 = child_personality.7060 # humble, cynical, content
		#100 = child_personality.7070 # vengeful, deceitful, calm
		#100 = child_personality.7080 # generous, fickle, arrogant
		#100 = child_personality.7090 # forgiving, trusting, patient
		#100 = child_personality.7100 # honest, arbitrary, impatient
		#100 = child_personality.7200 # brave, stubborn, wrathful
		
		# AGOT Added
		200 = agot_child_personality_events.2010 # Authoritative, Rude, Inquistive - Weighted double because its the only opportunity for any of these three traits

		#In prison
		100 = child_personality.0021 ##Gain lustful
		100 = child_personality.0022 ##Gain chaste
		100 = child_personality.0023 ##Gain gluttonous
		100 = child_personality.0024 ##Gain temperate
		100 = child_personality.0025 ##Gain greedy
		50 = child_personality.0026 ##Gain generous
		100 = child_personality.0027 ##Gain lazy
		100 = child_personality.0028 ##Gain diligent
		100 = child_personality.0029 ##Gain wrathful
		100 = child_personality.0030 ##Gain calm
		100 = child_personality.0031 ##Gain patient
		100 = child_personality.0032 ##Gain impatient
		100 = child_personality.0033 ##Gain arrogant
		100 = child_personality.0034 ##Gain humble
		100 = child_personality.0035 ##Gain deceitful
		50 = child_personality.0036 ##Gain honest
		100 = child_personality.0037 ##Gain craven
		100 = child_personality.0038 ##Gain brave
		100 = child_personality.0039 ##Gain shy
		100 = child_personality.0040 ##Gain gregarious
		100 = child_personality.0041 ##Gain ambitious
		100 = child_personality.0042 ##Gain content
		100 = child_personality.0043 ##Gain arbitrary
		100 = child_personality.0044 ##Gain just
		100 = child_personality.0045 ##Gain cynical
		100 = child_personality.0046 ##Gain zealous
		100 = child_personality.0047 ##Gain paranoid
		50 = child_personality.0048 ##Gain trusting
		50 = child_personality.0049 ##Gain compassionate
		100 = child_personality.0050 ##Gain callous
		100 = child_personality.0051 ##Gain sadistic
		100 = child_personality.0052 ##Gain stubborn
		100 = child_personality.0053 ##Gain fickle
		100 = child_personality.0054 ##Gain vengeful
		50 = child_personality.0055 ##Gain forgiving
	}
}

childhood_ongoing = {
	trigger = {
		is_ruler = yes
		is_available_child = yes
		# AGOT Added
		is_human = yes
	}
	random_events = {
		chance_to_happen = 50
		400 = 0
		100 = childhood.0001 #Child gets a crush
		100 = childhood.0003 #Child gets a friend
		100 = childhood.0005 #Child gets a bully
		100 = childhood.0007 #Child gets a victim
		100 = childhood.1000 #Your friendship can turn into a crush
		100 = childhood.1001 #You make your bully stop being such a bully
		100 = childhood.1002 #Parent is your educator, "supportive_parent" opinion_modifier
		100 = childhood.1004 #Guardian parent reduces stress
		100 = childhood.1005 #Guardian parent gives stress
		100 = childhood.1006 #Guardian parent (diligent) gives stress to lazy child
		100 = childhood.1007 #Bully gives you stress
		100 = childhood.1008 #To have a victim reduces stress
		100 = childhood.1009 #Your friend reduces your stress
		100 = childhood.1010 #Crush gives/reduces stress
		100 = childhood.1011 #Child might return an existing crush
		100 = childhood.1012 #Cruel guardian parent take child's toy away
		100 = childhood.1013 #You destroy your victims toy
		100 = childhood.1015 #child studies diplomacy with friend/crush
		100 = childhood.1016 #child studies martial with friend/crush
		100 = childhood.1017 #child studies stewardship with friend/crush
		100 = childhood.1018 #child studies intrigue with friend/crush
		100 = childhood.1019 #child studies learning with friend/crush
		50 = childhood.2001 #Get a pet cat!
		50 = childhood.2003 #Get a pet dog!
		100 = child_personality.0061 # Child may gain a propensity to drinking and lose their shyness by watching their guardian
		
		#BP1
		20 = childhood.7000 # Steal an adults clothes!
		5 = childhood.7100 # See another child get annihilated in a brutal but historically-appropriate fashion!
		100 = childhood.7200 # Try to learn the language of a Friend/crush
		100 = childhood.7300 # Beat up another child for a hook
		100 = bp1_yearly.5721 # Roast marshmallows over a fire with another child
		150 = childhood.7400 # Your bodyguard protects you from your bully
		100 = childhood.7500 # Your guardian tells you a story about one of their memories around the bonfire
		100 = bp1_yearly.9034 # You find a terrifying bog witch
		100 = bp1_yearly.9037 # You find a bog corpse
		100 = bp1_yearly.9041 # Your sibling is sad over the death of your parent
		40 = bp1_yearly.5001 # Your sibling tries to kill you

		#BP2
		100 = bp2_yearly.8100 # You develop feelings for another kid, is this a crush? 
		100 = bp2_yearly.5005 # A winsome foreign hostage
		100 = bp2_yearly.5009 # You promise?
	}
}

#AGOT Added
on_1st_birthday = {

	#AGOT Modified
	#trigger = { age = 1 }
	trigger = {
		age = 1
		is_human = yes
	}

	on_actions = {

		on_1st_birthday_sibling_betrothal

	}

}

on_3rd_birthday = {
	#AGOT Modified
	#trigger = { age = 3 }
	trigger = {
		age = 3
		NOT = { has_character_flag = agot_has_canon_traits }
		is_human = yes
	}
	events = {
		child_personality.3000 #Randomizes a childhood personality
		birth.3033 #lose the trait sickly
	}
}

on_6th_birthday = {
	#AGOT Modified
	#trigger = { age = 6 }
	trigger = {
		age = 6
		is_human = yes
	}

	#AGOT Added
	on_actions = {
		on_birthday_coin_flip
		on_6th_birthday_strong_seed
		on_6th_birthday_sibling_betrothal
		on_birthday_tomboy
	}
	
	events = {
		childhood_education.9001 #Assigns the education corresponding to their trait
	}
}

on_10th_birthday = {
	trigger = { 
		age = 10
		is_human = yes
	}

	#AGOT Added
	on_actions = {
		on_10th_birthday_sibling_betrothal
		on_10th_birthday_heir_betrothal
	}
	
	events = {
		child_personality.4000 #Assigns a sexuality to the child
		bp1_house_feud.9003
		bp1_house_feud.9004
	}
}

#AGOT Added

on_14th_birthday = {
	trigger = { 
		age = 14
		is_human = yes
	}
	
	on_actions = {
		on_birthday_sibling_or_dynasty_betrothal
	}

	events = {
		agot_events_generic.0005
		delay = { days = { 2 360 } }
		agot_events_generic.0007
	}
}

on_15th_birthday = {
	trigger = { 
		age = 15
		is_human = yes
	}

	events = {
		witch.1001
		secret_faith.1001
		bp2_yearly.2019 # Imaginary Friend - Eccentric Trait Event
		delay = { days = { 30 120 } }
		child_personality.7000 # Safety net - Charming
		child_personality.7010 # Safety net - Rowdy
		child_personality.7020 # Safety net - Bossy
		child_personality.7030 # Safety net - Pensive
		child_personality.7040 # Safety net - Curious
		agot_engagement_events.0001
	}
}

on_16th_birthday = {
	trigger = { 
		age = 16
		is_human = yes
	}

	events = {
		childhood_education.9103 #To make sure that every child gets a education
		child_personality.4011 #To make sure everyone gets a sexuality
		childhood.0900#victim transforms into opinion modifier on adulthood
		childhood.0910#bully transforms into opinion modifier on adulthood
		childhood.0998#crush transforms into opinion modifier on adulthood
		#AGOT Added
		agot_events_generic.0011
		coming_of_age.0001#Learning
		coming_of_age.0002#Stewardship
		coming_of_age.0003#Martial
		coming_of_age.0004#Intrigue
		coming_of_age.0005#Diplomacy
		delay = { days = 2 }
		childhood_education.0005 #To remove education focus
		childhood_education.0006 #To remove stray guardians
		childhood_education.0007 #To remove childhood traits and make sure any education trait is applied
		childhood_education.0008 #To remove all flags from childhood events
		coming_of_age.2000 #Sage Accolade
		coming_of_age.2001 #Sage Accolade high bonus
		coming_of_age.3000 #House Knight Accolade
	}

	effect = {
		if = {
			limit = {
				is_ai = yes
				# Shouldn't matter _but_, just in case, we double-check that you're actually in a suitable diarchy.
				has_diarchy_active_parameter = diarchy_type_is_entrenched_regency
			}
			trigger_event = {
				id = diarchy.0111
				months = { 6 24 }
			}
		}
	# If they had the Imaginary Friend story cycle, determine if we need to end the story or assign an adult persona to the imaginary friend 
		if = {
			limit = {
				any_owned_story = { story_type = story_cycle_imaginary_friend }
				NOT = { has_trait = eccentric }
			}
			random_owned_story = {
				limit = { story_type = story_cycle_imaginary_friend }
				end_story = yes
			}
			remove_character_modifier = imaginary_friend_modifier
		}
		else = {
			random_owned_story = {
				limit = { story_type = story_cycle_imaginary_friend }
				random_list = {
					50 = { # Give them a devilish imaginary friend in adulthood
						set_variable = {
							name = imaginary_friend_disposition
							value = flag:devil
						}
						modifier = {
							root = { has_trait = sadistic }
							add = 10
						}
						modifier = {
							root = { has_trait = vengeful }
							add = 10 
						}
						modifier = {
							root = { has_trait = deceitful }
							add = 10 
						}
						modifier = {
							root = { has_trait = callous }
							add = 10 
						}
						modifier = {
							root = { has_trait = paranoid }
							add = 10 
						}
					}
					50 = { # Give them an angelic imaginary friend in adulthood
						set_variable = {
							name = imaginary_friend_disposition
							value = flag:angel 
						}
						modifier = {
							root = { has_trait = compassionate }
							add = 10 
						}
						modifier = {
							root = { has_trait = forgiving }
							add = 10 
						}
						modifier = {
							root = { has_trait = generous }
							add = 10 
						}
						modifier = {
							root = { has_trait = calm }
							add = 10 
						}
						modifier = {
							root = { has_trait = content }
							add = 10 
						}
					}
				}
			}
		}
		# Hostage reaches adulthood
		if = {
			limit = { is_hostage = yes }
			save_scope_as = hostage
			warden ?= {
				set_variable = {
					name = hook_chance
					value = scope:hostage.bp2_hostage_loyalty_hook_chance_value
				}
				trigger_event = {
					id = bp2_hostage_system.0200
					days = 5
				}
			}
		}
	}
}

#To make sure everyone, including character from history, gets a sexuality
on_action_add_sexuality = {
	trigger = {
		age = 10
		is_human = yes
	}

	events = {
		delay = { days = { 14 30 } }
		child_personality.4000 #To make sure everyone gets a sexuality
	}
}

on_birthday_education_events = {
	trigger = {
		is_adult = no
		is_human = yes
		age >= childhood_education_start_age
	}

	events = {
		court_maintenance.0003 #All children should acquire Personality Traits
	}
	
	on_actions = {
		childhood_education_boost
		childhood_education
	}
}

#To check if character needs education boost or not
childhood_education_boost = {
	trigger = {
		NOT = { has_character_flag = natural_education_progression }
		NOT = { age = 16 } # Already fired in 'on_16th_birthday' on_action
		has_education_focus_trigger = yes
	}
	events = {
		childhood_education.9103 # Education catchup
	}
}

#To gain education points
childhood_education = {
	trigger = {
		is_adult = no
		age >= childhood_education_start_age
	}

	events = {
		childhood_education.9002
		childhood_education.1001
		childhood_education.9004
	}
	on_actions = {
		childhood_education_patronage
	}
}

#Patronage-focused / scholarly spouse may adds points
childhood_education_patronage = {
	trigger = {
		exists = host.cp:councillor_spouse
		host.cp:councillor_spouse = { save_temporary_scope_as = councillor }
		spouse_learning_tier_1_trigger = yes
		has_education_focus_trigger = yes
		trigger_if = {
			limit = {
				has_variable = patronage_spouse_tutor
			}
			var:patronage_spouse_tutor = scope:councillor
		}
	}
	events = {
		childhood_education.9003
	}
}

on_ward_depart_for_guardian = {}

on_ward_depart_travel_planner_exit = {}

on_ward_arrive_at_guardian = {
	effect = {
		if = {
			limit = {
				# Ward still valid
				is_alive = yes
				is_adult = no
				is_landed = no
				num_of_relation_guardian = 0
				# Guardian still valid
				var:ward_travelling_to_guardian ?= {
					is_alive = yes
					num_of_relation_ward <= 1
				}
				OR = {
					scope:guardian = { is_landed = yes }
					exists = scope:guardian.host
					exists = scope:guardian.liege
				}
			}
			# Move to court
			if = {
				limit = {
					scope:guardian = { is_landed = yes }
					NOT = { host = scope:guardian }
				}
				visit_court_of = scope:guardian
			}
			else_if = {
				limit = {
					exists = scope:guardian.host
					NOT = { host = scope:guardian.host }
				}
				visit_court_of = scope:guardian.host
			}
			else_if = {
				limit = {
					exists = scope:guardian.liege
					NOT = { host = scope:guardian.liege }
				}
				visit_court_of = scope:guardian.liege
			}
			# Set relation
			set_relation_guardian = scope:guardian
			# Opinion
			guardian_add_opinion_effect = {
				GUARDIAN = scope:guardian
				WARD_LIEGE = scope:ward_liege
				WARD = scope:ward
			}
			# Convert culture setup
			scope:ward = {
				if = {
					limit = { has_character_flag = convert_culture }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_culture
						}
					}
					remove_character_flag = convert_culture
				}
				# Convert faith setup
				if = {
					limit = { has_character_flag = convert_faith }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_faith
						}
					}
					remove_character_flag = convert_faith
				}
			}
			# Fire events
			scope:guardian ?= { trigger_event = char_interaction.0083 }
			if = {
				limit = {
					NOT = { scope:guardian_liege ?= scope:guardian }
				}
				scope:guardian_liege = { trigger_event = char_interaction.0083 }
			}
			scope:ward_liege ?= { trigger_event = char_interaction.0084 }
		}
		else = { # Invalidate travel
			scope:guardian = {
				send_interface_toast = {
					title = ward_invalidated_during_travel_title
					left_icon = scope:ward
					right_icon = scope:guardian
					show_as_tooltip = {
						scope:ward = { remove_relation_guardian = scope:guardian }
					}
				}
			}
			scope:ward_liege ?= {
				send_interface_toast = {
					title = ward_invalidated_during_travel_title
					left_icon = scope:ward
					right_icon = scope:guardian
					scope:ward = {
						show_as_tooltip = { remove_relation_guardian = scope:guardian }
						current_travel_plan ?= {
							if = {
								limit = { can_cancel = yes }
								cancel_travel_plan = yes
							}
						}
						set_location_to_default = yes
					}
				}
			}
			debug_log = "Invalidated ward travel on arrival"
			debug_log_scopes = yes
		}
		# Clean up variables
		remove_variable = ward_travelling_to_guardian
		remove_variable = character_making_education_request
		#AGOT ADDED - Dead People check
		# scope:guardian = { remove_variable = guardian_waiting_for_ward }
		if = {
			limit = {
				scope:guardian ?= { is_alive = yes }
			}
			scope:guardian = { remove_variable = guardian_waiting_for_ward }
		}
	}
}

on_ward_depart_for_home = {}

on_ward_arrive_at_home = {
	effect = {
		# Move to court
		save_scope_as = ward
		var:guardian_educated ?= { save_scope_as = guardian }
		var:guardian_educated_realm ?= { save_scope_as = realm }
		if = {
			limit = {
				exists = scope:guardian 
				exists = liege
			}
			liege = {
				send_interface_message = {
					type = event_generic_good_text
					title = ward_returned_home_title
					desc = ward_returned_home_desc_guardian
					left_icon = scope:ward
					right_icon = scope:guardian
					scope:ward = { return_to_court = yes }
				}
			}
		}
		else_if = {
			limit = { exists = liege }
			liege = {
				send_interface_message = {
					type = event_generic_good_text
					title = ward_returned_home_title
					desc = ward_returned_home_desc
					left_icon = scope:ward
					scope:ward = { return_to_court = yes }
				}
			}
		}
		remove_variable = guardian_educated
		remove_variable = guardian_educated_realm
	}
}

on_guardian_depart_for_ward = {}

on_guardian_depart_travel_planner_exit = {}

on_guardian_arrive_at_ward = {
	effect = {
		if = {
			limit = {
				# Guardian still valid
				is_alive = yes
				num_of_relation_ward <= 1
				# Ward still valid
				var:guardian_travelling_to_ward ?= {
					is_alive = yes
					is_adult = no
					is_landed = yes
					num_of_relation_guardian = 0
				}
			}
			# Move to court
			visit_court_of = scope:ward
			# Set relation
			set_relation_ward = scope:ward
			# Opinion
			guardian_add_opinion_effect = {
				GUARDIAN = scope:guardian
				WARD_LIEGE = scope:ward_liege
				WARD = scope:ward
			}
			# Convert culture setup
			scope:ward = {
				if = {
					limit = { has_character_flag = convert_culture }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_culture
						}
					}
					remove_character_flag = convert_culture
				}
				# Convert faith setup
				if = {
					limit = { has_character_flag = convert_faith }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_faith
						}
					}
					remove_character_flag = convert_faith
				}
			}
			# Fire events
			scope:ward = { trigger_event = char_interaction.0082 }
			if = {
				limit = {
					NOT = { scope:ward_liege = scope:ward }
				}
				scope:ward_liege = { trigger_event = char_interaction.0082 }
			}
		}
		else = { # Invalidate travel
			scope:ward = {
				send_interface_toast = {
					title = guardian_invalidated_during_travel_title
					left_icon = scope:guardian
					show_as_tooltip = {
						scope:guardian = { remove_relation_ward = scope:ward }
					}
				}
			}
			scope:ward_liege ?= {
				send_interface_toast = {
					title = guardian_invalidated_during_travel_title
					left_icon = scope:ward
					right_icon = scope:guardian
					scope:guardian = {
						show_as_tooltip = { remove_relation_ward = scope:ward }
						current_travel_plan = {
							if = {
								limit = { can_cancel = yes }
								cancel_travel_plan = yes
							}
						}
						set_location_to_default = yes
					}
				}
			}
			debug_log = "Invalidated guardian travel on arrival"
			debug_log_scopes = yes
		}
		# Clean up variables
		var:guardian_travelling_to_ward ?= {
			remove_variable = character_making_education_request
			remove_variable = ward_waiting_for_guardian
		}
		remove_variable = guardian_travelling_to_ward
	}
}

on_guardian_depart_for_home = {}

on_guardian_arrive_at_home = {
	effect = {
		# Move to court
		save_scope_as = guardian
		var:ward_educated ?= { save_scope_as = ward }
		var:ward_educated_realm = { save_scope_as = realm }
		if = {
			limit = { exists = scope:ward }
			liege = {
				send_interface_message = {
					type = event_generic_good_text
					title = guardian_returned_home_title
					desc = guardian_returned_home_desc_ward
					left_icon = scope:guardian
					right_icon = scope:ward
					scope:guardian = { return_to_court = yes }
				}
			}
		}
		else = {
			liege = {
				send_interface_message = {
					type = event_generic_good_text
					title = guardian_returned_home_title
					desc = guardian_returned_home_desc
					left_icon = scope:guardian
					scope:guardian = { return_to_court = yes }
				}
			}
		}
		remove_variable = ward_educated
		remove_variable = ward_educated_realm
	}
}

on_adoptee_depart_for_adopter = {
	effect = {
		scope:adopter = {
			send_interface_toast = {
				title = adoptee_departs_title
				left_icon = scope:adoptee
				right_icon = scope:adopter
				custom_tooltip = adoptee_departs_tt
			}
		}
	}
}
on_adoptee_depart_travel_planner_exit = {}
on_adoptee_arrive_at_adopter = {
	effect = {
		if = {
			limit = { exists = scope:adopter }
			scope:adopter = {
				send_interface_message = {
					type = event_generic_good_text
					title = adoptee_returned_home_title
					desc = adoptee_returned_home_desc
					left_icon = scope:adoptee
					add_courtier  = scope:adoptee
				}
			}
			remove_variable = adoptee_travelling_to_adopter
		}
	}
}