# Baby Portrait ( needs to be updated when new graphicalcultures are added )
assign_baby_portrait_effect = { # Automatically give correct trait based on skintone/gender
	if = {
		limit = {
			has_game_rule = {
				name = baby_portraits
				value = yes
			}

			practical_age < 2
		}

		# Just in case they already had the trait
		remove_trait = baby_boy_dark
		remove_trait = baby_boy_darker
		remove_trait = baby_boy_fair
		remove_trait = baby_boy_tanned
		remove_trait = baby_girl_dark
		remove_trait = baby_girl_darker
		remove_trait = baby_girl_fair
		remove_trait = baby_girl_tanned

		if = { # skin_tone = 0
			limit = {
				OR = { # Exhaustive list of every gfx_culture with fair skin (skin_tone = 0)
					graphical_culture = frankishgfx
					graphical_culture = englishgfx
					graphical_culture = germangfx
					graphical_culture = occitangfx
					graphical_culture = celticgfx
					graphical_culture = norsegfx
					graphical_culture = saxongfx
					graphical_culture = normangfx
					graphical_culture = italiangfx
					graphical_culture = easternslavicgfx
					graphical_culture = westernslavicgfx
					graphical_culture = ugricgfx
					graphical_culture = baltgfx
					graphical_culture = bretongfx
					graphical_culture = bohemiangfx
					graphical_culture = norsegaelgfx
					graphical_culture = hungariangfx
					graphical_culture = dutchgfx
					graphical_culture = pomeraniangfx
					graphical_culture = southernslavicgfx
					graphical_culture = basquegfx
					graphical_culture = lombardgfx
					graphical_culture = ashkenazigfx
					graphical_culture = easterngfx
					graphical_culture = westerngfx
				}
			}

			if = {
				limit = { is_female = yes }
				add_trait = baby_girl_fair
			}
			else = {
				add_trait = baby_boy_fair
			}
		}
		else_if = { # skin_tone = 1
			limit = {
				OR = { # Exhaustive list of every gfx_culture with tanned skin (skin_tone = 1)
					graphical_culture = southerngfx
					graphical_culture = byzantinegfx
					graphical_culture = mongolgfx
					graphical_culture = andalusiangfx
					graphical_culture = chinesegfx
					graphical_culture = bodpagfx
					graphical_culture = bulgargfx
					graphical_culture = centralasiangfx
					graphical_culture = steppegfx
					graphical_culture = siberiangfx
					graphical_culture = alangfx
					graphical_culture = georgiangfx
					graphical_culture = sogdiangfx
					graphical_culture = levantinegfx
					graphical_culture = siciliangfx
					graphical_culture = pecheneggfx
					graphical_culture = afghangfx
					graphical_culture = assamesegfx
					graphical_culture = khitangfx
					graphical_culture = semitegfx
					graphical_culture = mesoamericangfx
					graphical_culture = jewishgfx
				}
			}

			if = {
				limit = { is_female = yes }
				add_trait = baby_girl_tanned
			}
			else = {
				add_trait = baby_boy_tanned
			}
		}
		else_if = { # skin_tone = 2
			limit = {
				OR = { # Exhaustive list of every gfx_culture with dark skin (skin_tone = 2)
					graphical_culture = arabicgfx
					graphical_culture = copticgfx
					graphical_culture = turkishgfx
					graphical_culture = cumangfx
					graphical_culture = persiangfx
					graphical_culture = maghrebgfx
					graphical_culture = egyptiangfx
					graphical_culture = muslimgfx
					graphical_culture = indiangfx
				}
			}

			if = {
				limit = { is_female = yes }
				add_trait = baby_girl_dark
			}
			else = {
				add_trait = baby_boy_dark
			}
		}
		else_if = { # skin_tone = 3
			limit = {
				OR = { # Exhaustive list of every gfx_culture with darker skin (skin_tone = 3)
					graphical_culture = africangfx
					graphical_culture = westafricangfx
					graphical_culture = southindiangfx
					graphical_culture = berbergfx
					graphical_culture = centralindiangfx
					graphical_culture = somaligfx
				}
			}

			if = {
				limit = { is_female = yes }
				add_trait = baby_girl_darker
			}
			else = {
				add_trait = baby_boy_darker
			}
		}
		else = { # On error, default to Tanned
			if = {
				limit = { is_female = yes }
				add_trait = baby_girl_tanned
			}
			else = {
				add_trait = baby_boy_tanned
			}
		}
	}
}

# Bloodlust
bloodlust_control_effect = {
	# Remove the trait
	if = {
		limit = {
			trait = bloodlust

			OR = {
				ai = no
				independent = no
				has_regent = yes

				trigger_if = {
					limit = { primary_title = { is_mongol_empire = yes } }

					NAND = {
						culture = mongol
						religion = tengri_pagan
					}
				}
				trigger_else_if = {
					limit = { primary_title = { title = e_aztec_empire } }

					NAND = {
						culture = nahua
						religion = aztec_pagan
					}
				}
				trigger_else_if = {
					limit = {
						capital_scope = {
							region = custom_hre_stem_duchies
						}
					}

					OR = {
						is_tribal = yes
						is_title_active = e_karling
						is_title_active = e_hre
						is_title_active = e_germania
						lower_real_tier_than = KING
						has_character_flag = former_carolingian_emperor
						completely_controls_region = custom_hre_stem_duchies
						martial < 14

						NOR = {
							trait = ambitious
							trait = zealous
						}
					}
				}
				trigger_else_if = {
					limit = { dynasty = 613 } # Seljuk

					OR = {
						realm_size < 200
						year < 1200
					}
				}
				trigger_else_if = {
					limit = { dynasty = 800 } # Timurids
					realm_size > 200
				}
			}
		}

		remove_trait = bloodlust
	}
	# Add the trait
	else_if = {
		limit = {
			NOT = { trait = bloodlust }
			ai = yes
			has_regent = no

			trigger_if = {
				limit = { primary_title = { is_mongol_empire = yes } }

				culture = mongol
				religion = tengri_pagan
			}
			trigger_else_if = {
				limit = { primary_title = { title = e_aztec_empire } }

				culture = nahua
				religion = aztec_pagan
			}
			trigger_else_if = {
				limit = {
					capital_scope = {
						region = custom_hre_stem_duchies
					}
					is_tribal = no # Tribes are already pretty aggressive
				}

				higher_real_tier_than = DUKE

				OR = {
					trait = ambitious
					trait = zealous
					martial >= 14
				}

				NOR = {
					is_title_active = e_karling
					is_title_active = e_hre
					is_title_active = e_germania
					has_character_flag = former_carolingian_emperor
					completely_controls_region = custom_hre_stem_duchies
				}
			}
			trigger_else_if = {
				limit = { dynasty = 613 } # Seljuk

				realm_size < 200
				year < 1200
			}
			trigger_else_if = {
				limit = { dynasty = 800 } # Timurids
				realm_size < 200
			}
			trigger_else = {
				always = no
			}
		}

		add_trait = bloodlust
	}
}

# Dishonorable Trait
dishonorable_trait_effect = {
	hidden_effect = {
		dishonorable_trait_remove_effect = yes
		dynastic_stain_trait_remove_effect = yes

		# Caps the variable
		if = {
			limit = {
				check_variable = {
					which = dishonorable
					value > 200
				}
			}

			set_variable = {
				which = dishonorable
				value = 200
			}
		}
		else_if = {
			limit = {
				check_variable = {
					which = dishonorable
					value < 0
				}
			}

			set_variable = {
				which = dishonorable
				value = 0
			}
		}
	}

	# Sets the trait
	if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 180
			}
		}

		add_trait = dishonorable10
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 160
			}
		}

		add_trait = dishonorable9
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 140
			}
		}

		add_trait = dishonorable8
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 120
			}
		}

		add_trait = dishonorable7
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 100
			}
		}

		add_trait = dishonorable6
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 80
			}
		}

		add_trait = dishonorable5
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 60
			}
		}

		add_trait = dishonorable4
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 40
			}
		}

		add_trait = dishonorable3
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 20
			}
		}

		add_trait = dishonorable2
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 0
			}
		}

		add_trait = dishonorable1
	}
}

# Dynastic Stain Trait
# Applies Dynastic Stain trait based on inherited Dishonorable variable
dynastic_stain_trait_effect = {
	if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 180
			}
		}

		add_trait = dyn_dishonorable10
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 160
			}
		}

		add_trait = dyn_dishonorable9
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 140
			}
		}

		add_trait = dyn_dishonorable8
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 120
			}
		}

		add_trait = dyn_dishonorable7
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 100
			}
		}

		add_trait = dyn_dishonorable6
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 80
			}
		}

		add_trait = dyn_dishonorable5
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 60
			}
		}

		add_trait = dyn_dishonorable4
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 40
			}
		}

		add_trait = dyn_dishonorable3
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 20
			}
		}

		add_trait = dyn_dishonorable2
	}
	else_if = {
		limit = {
			check_variable = {
				which = dishonorable
				value > 0
			}
		}

		add_trait = dyn_dishonorable1
	}
}

dishonorable_trait_remove_effect = {
	remove_trait = dishonorable1
	remove_trait = dishonorable2
	remove_trait = dishonorable3
	remove_trait = dishonorable4
	remove_trait = dishonorable5
	remove_trait = dishonorable6
	remove_trait = dishonorable7
	remove_trait = dishonorable8
	remove_trait = dishonorable9
	remove_trait = dishonorable10
}

dynastic_stain_trait_remove_effect = {
	remove_trait = dyn_dishonorable1
	remove_trait = dyn_dishonorable2
	remove_trait = dyn_dishonorable3
	remove_trait = dyn_dishonorable4
	remove_trait = dyn_dishonorable5
	remove_trait = dyn_dishonorable6
	remove_trait = dyn_dishonorable7
	remove_trait = dyn_dishonorable8
	remove_trait = dyn_dishonorable9
	remove_trait = dyn_dishonorable10
}

# Tyrant Trait
tyrant_trait_effect = {
	hidden_effect = {
		tyrant_trait_remove_effect = yes

		# Cap the variable
		if = {
			limit = {
				check_variable = {
					which = tyrant
					value >= 50
				}
			}

			set_variable = {
				which = tyrant
				value = 50
			}
		}
		else_if = {
			limit = {
				check_variable = {
					which = tyrant
					value < 0
				}
			}

			set_variable = {
				which = tyrant
				value = 0
			}
		}
	}

	# Set the Trait
	if = {
		limit = {
			check_variable = {
				which = tyrant
				value > 40
			}
		}
		add_trait = tyrant5
	}
	else_if = {
		limit = {
			check_variable = {
				which = tyrant
				value > 30
			}
		}
		add_trait = tyrant4
	}
	else_if = {
		limit = {
			check_variable = {
				which = tyrant
				value > 20
			}
		}
		add_trait = tyrant3
	}
	else_if = {
		limit = {
			check_variable = {
				which = tyrant
				value > 10
			}
		}
		add_trait = tyrant2
	}
	else_if = {
		limit = {
			check_variable = {
				which = tyrant
				value > 0
			}
		}
		add_trait = tyrant1
	}
}

tyrant_trait_remove_effect = {
	remove_trait = tyrant1
	remove_trait = tyrant2
	remove_trait = tyrant3
	remove_trait = tyrant4
	remove_trait = tyrant5
}

# Education
force_two_education_events_effect = {
	if = {
		limit = { NOT = { has_character_flag = had_first_education_event } }
		set_character_flag = had_first_education_event
	}
	else_if = {
		limit = { NOT = { has_character_flag = had_second_education_event } }
		set_character_flag = had_second_education_event
	}
}

reduce_education_effect = {
	custom_tooltip = {
		text = CURRENT_EDUCATION_REDUCED_TT

		trigger_switch = {
			on_trigger = trait

			### Intrigue ###
			amateurish_plotter = {
				remove_trait = amateurish_plotter
			}
			flamboyant_schemer = {
				remove_trait = flamboyant_schemer
				add_trait = amateurish_plotter
			}
			intricate_webweaver = {
				remove_trait = intricate_webweaver
				add_trait = flamboyant_schemer
			}
			elusive_shadow = {
				remove_trait = elusive_shadow
				add_trait = intricate_webweaver
			}

			### Diplomacy ###
			naive_appeaser = {
				remove_trait = naive_appeaser
			}
			underhanded_rogue = {
				remove_trait = underhanded_rogue
				add_trait = naive_appeaser
			}
			charismatic_negotiator = {
				remove_trait = charismatic_negotiator
				add_trait = underhanded_rogue
			}
			grey_eminence = {
				remove_trait = grey_eminence
				add_trait = charismatic_negotiator
			}

			### Stewardship ###
			indulgent_wastrel = {
				remove_trait = indulgent_wastrel
			}
			thrifty_clerk = {
				remove_trait = thrifty_clerk
				add_trait = indulgent_wastrel
			}
			fortune_builder = {
				remove_trait = fortune_builder
				add_trait = thrifty_clerk
			}
			midas_touched = {
				remove_trait = midas_touched
				add_trait = fortune_builder
			}

			### Martial ###
			misguided_warrior = {
				remove_trait = misguided_warrior
			}
			tough_soldier = {
				remove_trait = tough_soldier
				add_trait = misguided_warrior
			}
			skilled_tactician = {
				remove_trait = skilled_tactician
				add_trait = tough_soldier
			}
			brilliant_strategist = {
				remove_trait = brilliant_strategist
				add_trait = skilled_tactician
			}

			### Learning ###
			detached_priest = {
				remove_trait = detached_priest
			}
			dutiful_cleric = {
				remove_trait = dutiful_cleric
				add_trait = detached_priest
			}
			scholarly_theologian = {
				remove_trait = scholarly_theologian
				add_trait = dutiful_cleric
			}
			mastermind_theologian = {
				remove_trait = mastermind_theologian
				add_trait = scholarly_theologian
			}
		}
	}
}

reeducate_to_tier_1_effect = {
	custom_tooltip = {
		text = REEDUCATED_TO_TIER_1_TT

		trigger_switch = {
			on_trigger = has_character_flag

			reeducates_to_diplomacy = { add_trait = naive_appeaser }
			reeducates_to_martial = { add_trait = misguided_warrior }
			reeducates_to_stewardship = { add_trait = indulgent_wastrel }
			reeducates_to_intrigue = { add_trait = amateurish_plotter }
			reeducates_to_learning = { add_trait = detached_priest }
		}
	}
}

reeducate_to_tier_2_effect = {
	custom_tooltip = {
		text = REEDUCATED_TO_TIER_2_TT

		trigger_switch = {
			on_trigger = has_character_flag

			reeducates_to_diplomacy = { add_trait = underhanded_rogue }
			reeducates_to_martial = { add_trait = tough_soldier }
			reeducates_to_stewardship = { add_trait = thrifty_clerk }
			reeducates_to_intrigue = { add_trait = flamboyant_schemer }
			reeducates_to_learning = { add_trait = dutiful_cleric }
		}
	}
}

reeducate_to_tier_3_effect = {
	custom_tooltip = {
		text = REEDUCATED_TO_TIER_3_TT

		trigger_switch = {
			on_trigger = has_character_flag

			reeducates_to_diplomacy = { add_trait = charismatic_negotiator }
			reeducates_to_martial = { add_trait = skilled_tactician }
			reeducates_to_stewardship = { add_trait = fortune_builder }
			reeducates_to_intrigue = { add_trait = intricate_webweaver }
			reeducates_to_learning = { add_trait = scholarly_theologian }
		}
	}
}

reeducate_to_tier_4_effect = {
	custom_tooltip = {
		text = REEDUCATED_TO_TIER_4_TT

		trigger_switch = {
			on_trigger = has_character_flag

			reeducates_to_diplomacy = { add_trait = grey_eminence }
			reeducates_to_martial = { add_trait = brilliant_strategist }
			reeducates_to_stewardship = { add_trait = midas_touched }
			reeducates_to_intrigue = { add_trait = elusive_shadow }
			reeducates_to_learning = { add_trait = mastermind_theologian }
		}
	}
}

upgrade_education_diplomacy_effect = {
	trigger_switch = {
		on_trigger = trait

		### Diplomacy ###
		naive_appeaser = {
			remove_trait = naive_appeaser
			add_trait = underhanded_rogue
		}
		underhanded_rogue = {
			remove_trait = underhanded_rogue
			add_trait = charismatic_negotiator
		}
		charismatic_negotiator = {
			remove_trait = charismatic_negotiator
			add_trait = grey_eminence
		}
		grey_eminence = {
			# already maxed
		}
	}
}
upgrade_education_intrigue_effect = {
	trigger_switch = {
		on_trigger = trait

		### Intrigue ###
		amateurish_plotter = {
			remove_trait = amateurish_plotter
			add_trait = flamboyant_schemer
		}
		flamboyant_schemer = {
			remove_trait = flamboyant_schemer
			add_trait = intricate_webweaver
		}
		intricate_webweaver = {
			remove_trait = intricate_webweaver
			add_trait = elusive_shadow
		}
		elusive_shadow = {
			# already maxed
		}
	}
}

upgrade_education_learning_effect = {
	trigger_switch = {
		on_trigger = trait

		### Learning ###
		detached_priest = {
			remove_trait = detached_priest
			add_trait = dutiful_cleric
		}
		dutiful_cleric = {
			remove_trait = dutiful_cleric
			add_trait = scholarly_theologian
		}
		scholarly_theologian = {
			remove_trait = scholarly_theologian
			add_trait = mastermind_theologian
		}
		mastermind_theologian = {
			# already maxed
		}
	}
}

upgrade_education_martial_effect = {
	trigger_switch = {
		on_trigger = trait

		### Martial ###
		misguided_warrior = {
			remove_trait = misguided_warrior
			add_trait = tough_soldier
		}
		tough_soldier = {
			remove_trait = tough_soldier
			add_trait = skilled_tactician
		}
		skilled_tactician = {
			remove_trait = skilled_tactician
			add_trait = brilliant_strategist
		}
		brilliant_strategist = {
			# already maxed
		}
	}
}

upgrade_education_stewarship_effect = {
	trigger_switch = {
		on_trigger = trait

		### Stewardship ###
		indulgent_wastrel = {
			remove_trait = indulgent_wastrel
			add_trait = thrifty_clerk
		}
		thrifty_clerk = {
			remove_trait = thrifty_clerk
			add_trait = fortune_builder
		}
		fortune_builder = {
			remove_trait = fortune_builder
			add_trait = midas_touched
		}
		midas_touched = {
			# already maxed
		}
	}
}

# Duel System
battle_skill_increased = {
	trigger_switch = {
		on_trigger = trait

		poor_warrior = { add_trait = trained_warrior }
		trained_warrior = { add_trait = skilled_warrior }
		skilled_warrior = { add_trait = master_warrior }
		master_warrior = {} # Already the best
		fallback = { add_trait = poor_warrior }
	}
}

battle_skill_decreased = {
	trigger_switch = {
		on_trigger = trait

		poor_warrior = { remove_trait = poor_warrior }
		trained_warrior = {
			remove_trait = trained_warrior
			add_trait = poor_warrior
		}
		skilled_warrior = {
			remove_trait = skilled_warrior
			add_trait = trained_warrior
		}
		master_warrior = {
			remove_trait = master_warrior
			add_trait = skilled_warrior
		}
	}
}

women_scared_beauty_effect = {
	if = {
		limit = {
			is_female = yes

			OR = {
				trait = scarred

				AND = {
					trait = beauty
					practical_age >= 45
				}
			}
		}

		if = {
			limit = { trait = scarred }
			remove_trait = scarred
			add_trait = scarred_female
		}

		if = {
			limit = {
				trait = beauty
				practical_age >= 45
			}
			remove_trait = beauty
		}
	}
}

no_longer_should_be_hiding_effect = {
	if = {
		limit = {
			is_inaccessible_trigger = yes
			is_ruler = no
			in_siege = no
			in_seclusion = no
			liege = { war = no }

			NOR = {
				AND = {
					has_character_flag = in_hiding_murder

					liege = {
						OR = {
							is_married = ROOT
							is_parent_of = ROOT
						}
					}
				}

				AND = {
					has_character_flag = in_hiding_siege
					liege = { is_close_relative = ROOT }
				}

			}
		}
		end_inaccessibility_effect = yes
		clr_character_flag = in_hiding_murder
		clr_character_flag = in_hiding_siege
	}
}
