namespace = geneticPlus

#######################################
#
# CK2Plus Genetics Events
#
#######################################
#
# CATALOG
#
# 001 - 009: Setup Chain
# 010 - 019: Maintenance
#
#######################################

#######################################
#
# 001 - 009: Setup Chain
#
#######################################
#
# INDEX
#
# 001: Congenital Traits Control
# 002: Intelligence Check
# 003: Strength Check
# 004: Appearance Check
# 005: Fertility Check
# 006: Height Check
# 007: Masking Modifiers
# 008: Divine Blood Check
#
#######################################

# 001: Congenital Traits Control
character_event = {
	id = geneticPlus.001

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		character_event = { id = geneticPlus.002 } # 002: Intelligence Check
		character_event = { id = geneticPlus.003 } # 003: Strength Check
		character_event = { id = geneticPlus.004 } # 004: Appearance Check
		character_event = { id = geneticPlus.005 } # 005: Fertility Check
		character_event = { id = geneticPlus.006 } # 006: Height Check
		character_event = { id = geneticPlus.007 } # 007: Masking Modifiers
		character_event = { id = geneticPlus.008 } # 008: Divine Blood Check

		# Chance of random congenital trait
		if = {
			limit = { is_secret_congenital_trait_trigger = no }
			generate_potential_congenital_trait = yes
		}
	}
}

# 002: Intelligence Check
character_event = {
	id = geneticPlus.002

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOR = {
			trait = genius
			trait = quick
			trait = slow
			trait = imbecile
		}

		OR = {
			mother_even_if_dead = {
				OR = {
					trait = genius
					trait = quick
					trait = slow
					trait = imbecile
				}
			}

			real_father_even_if_dead = {
				OR = {
					trait = genius
					trait = quick
					trait = slow
					trait = imbecile
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				OR = {
					mother_even_if_dead = {
						OR = {
							trait = genius
							trait = quick
						}
					}

					real_father_even_if_dead = {
						OR = {
							trait = genius
							trait = quick
						}
					}
				}

				# If only one parent is quick, the other can't be average or less
				NOR = {
					AND = {
						mother_even_if_dead = { trait = quick }

						real_father_even_if_dead = {
							NOR = {
								trait = quick
								trait = genius
							}
						}
					}

					AND = {
						real_father_even_if_dead = { trait = quick }

						real_father_even_if_dead = {
							NOR = {
								trait = quick
								trait = genius
							}
						}
					}
				}
			}

			# Genius
			random = {
				chance = 10

				mult_modifier = { # Both parents are Geniuses
					factor = 2

					mother_even_if_dead = { trait = genius }
					real_father_even_if_dead = { trait = genius }
				}

				mult_modifier = { # One parent is just quick
					factor = 1.5

					OR = {
						mother_even_if_dead = { trait = quick }
						real_father_even_if_dead = { trait = quick }
					}

					OR = {
						mother_even_if_dead = { trait = genius }
						real_father_even_if_dead = { trait = genius }
					}
				}

				mult_modifier = { # one parent is slow or an imbecile
					factor = 0.5

					OR = {
						mother_even_if_dead = {
							OR = {
								trait = slow
								trait = imbecile
							}
						}

						real_father_even_if_dead = {
							OR = {
								trait = slow
								trait = imbecile
							}
						}
					}
				}

				mult_modifier = { # Both parents are quick
					factor = 0.1

					mother_even_if_dead = { trait = quick }
					real_father_even_if_dead = { trait = quick }
				}

				add_trait = genius
			}
		}

		# Quick
		if = {
			limit = {
				NOT = { trait = genius }

				# Either parent is either quick or a genius
				OR = {
					mother_even_if_dead = {
						OR = {
							trait = genius
							trait = quick
						}
					}

					real_father_even_if_dead = {
						OR = {
							trait = genius
							trait = quick
						}
					}
				}
			}

			random = {
				chance = 10

				mult_modifier = { # Both parents are geniuses
					factor = 3

					mother_even_if_dead = { trait = genius }
					real_father_even_if_dead = { trait = genius }
				}

				mult_modifier = { # Both parents are quick
					factor = 2

					mother_even_if_dead = { trait = quick }
					real_father_even_if_dead = { trait = quick }
				}

				mult_modifier = { # One parent is a genius
					factor = 2

					OR = {
						mother_even_if_dead = { trait = genius }
						real_father_even_if_dead = { trait = genius }
					}

					OR = {
						mother_even_if_dead = { trait = quick }
						real_father_even_if_dead = { trait = quick }
					}
				}

				mult_modifier = { # One parent is a genius, the other is average or worse
					factor = 1.5

					OR = {
						mother_even_if_dead = { trait = genius }
						real_father_even_if_dead = { trait = genius }
					}

					mother_even_if_dead = {
						NOR = {
							trait = genius
							trait = quick
						}
					}

					real_father_even_if_dead = {
						NOR = {
							trait = genius
							trait = quick
						}
					}
				}

				mult_modifier = { # one parent is slow or an imbecile
					factor = 0.5

					OR = {
						mother_even_if_dead = {
							OR = {
								trait = slow
								trait = imbecile
							}
						}

						real_father_even_if_dead = {
							OR = {
								trait = slow
								trait = imbecile
							}
						}
					}
				}

				add_trait = quick
			}
		}

		# Imbecile
		if = {
			limit = {
				NOR = {
					trait = genius
					trait = quick
				}

				# Either parent is slow or an imbecile
				OR = {
					mother_even_if_dead = {
						OR = {
							trait = slow
							trait = imbecile
						}
					}

					real_father_even_if_dead = {
						OR = {
							trait = slow
							trait = imbecile
						}
					}
				}

				# If one parent is slow, the other can't be average (or more)
				NOR = {
					AND = {
						mother_even_if_dead = { trait = slow }

						real_father_even_if_dead = {
							NOR = {
								trait = slow
								trait = imbecile
							}
						}
					}

					AND = {
						real_father_even_if_dead = { trait = slow }

						mother_even_if_dead = {
							NOR = {
								trait = slow
								trait = imbecile
							}
						}
					}
				}
			}

			random = {
				chance = 10

				modifier = { # Both parents are imbeciles
					factor = 2

					mother_even_if_dead = { trait = imbecile }
					real_father_even_if_dead = { trait = imbecile }
				}

				modifier = { # One parent is just slow
					factor = 1.5

					OR = {
						mother_even_if_dead = { trait = slow }
						real_father_even_if_dead = { trait = slow }
					}

					OR = {
						mother_even_if_dead = { trait = imbecile }
						real_father_even_if_dead = { trait = imbecile }
					}
				}

				modifier = { # One parent is quick or a genius
					factor = 0.5

					OR = {
						mother_even_if_dead = { trait = genius }
						mother_even_if_dead = { trait = quick }
						real_father_even_if_dead = { trait = genius }
						real_father_even_if_dead = { trait = quick }
					}
				}

				modifier = { # Both parents are slow
					factor = 0.1

					mother_even_if_dead = { trait = slow }
					real_father_even_if_dead = { trait = slow }
				}

				add_trait = imbecile
			}
		}

		if = {
			limit = {
				NOR = {
					trait = genius
					trait = quick
					trait = imbecile
				}

				# Either parent is slow or an imbecile
				OR = {
					mother_even_if_dead = {
						OR = {
							trait = slow
							trait = imbecile
						}
					}

					real_father_even_if_dead = {
						OR = {
							trait = slow
							trait = imbecile
						}
					}
				}
			}

			# Slow
			random = {
				chance = 10

				modifier = { # both parents are imbeciles
					factor = 3
					mother_even_if_dead = { trait = imbecile }
					real_father_even_if_dead = { trait = imbecile }
				}

				modifier = { # Both parents are slow
					factor = 2
					mother_even_if_dead = { trait = slow }
					real_father_even_if_dead = { trait = slow }
				}

				modifier = { # One parent is an imbecile
					factor = 2

					OR = {
						mother_even_if_dead = { trait = imbecile }
						real_father_even_if_dead = { trait = imbecile }
					}

					OR = {
						mother_even_if_dead = { trait = slow }
						real_father_even_if_dead = { trait = slow }
					}
				}

				modifier = { # One parent is an imbecile, the other is average
					factor = 1.5

					OR = {
						mother_even_if_dead = { trait = imbecile }
						real_father_even_if_dead = { trait = imbecile }
					}

					NOR = {
						mother_even_if_dead = { trait = slow }
						mother_even_if_dead = { trait = imbecile }
						real_father_even_if_dead = { trait = slow }
						real_father_even_if_dead = { trait = imbecile }
					}
				}

				modifier = { # One parent is quick or a genius
					factor = 0.5

					OR = {
						mother_even_if_dead = { trait = genius }
						mother_even_if_dead = { trait = quick }
						real_father_even_if_dead = { trait = genius }
						real_father_even_if_dead = { trait = quick }
					}
				}

				add_trait = slow
			}
		}
	}
}

# 003: Strength Check
character_event = {
	id = geneticPlus.003

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOR = {
			trait = strong
			trait = weak
		}

		OR = {
			mother_even_if_dead = {
				OR = {
					trait = strong
					trait = weak
				}
			}

			real_father_even_if_dead = {
				OR = {
					trait = strong
					trait = weak
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				OR = {
					mother_even_if_dead = { trait = strong }
					real_father_even_if_dead = { trait = strong }
				}
			}

			# Strong
			random = {
				chance = 33

				mult_modifier = { # Both parents strong
					factor = 2

					mother_even_if_dead = { trait = strong }
					real_father_even_if_dead = { trait = strong }
				}

				mult_modifier = { # One parent weak
					factor = 0.5

					OR = {
						mother_even_if_dead = { trait = weak }
						real_father_even_if_dead = { trait = weak }
					}
				}

				add_trait = strong
			}
		}

		if = {
			limit = {
				NOT = { trait = strong }

				OR = {
					mother_even_if_dead = { trait = weak }
					real_father_even_if_dead = { trait = weak }
				}
			}

			# Weak
			random = {
				chance = 33

				mult_modifier = { # Both parents weak
					factor = 2

					mother_even_if_dead = { trait = weak }
					real_father_even_if_dead = { trait = weak }
				}

				mult_modifier = { # One parent strong
					factor = 0.5

					OR = {
						mother_even_if_dead = { trait = strong }
						real_father_even_if_dead = { trait = strong }
					}
				}

				add_trait = weak
			}
		}
	}
}

# 004: Appearance Check
character_event = {
	id = geneticPlus.004

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOR = {
			trait = attractive
			trait = ugly
		}

		OR = {
			mother_even_if_dead = {
				OR = {
					trait = attractive
					trait = ugly
				}
			}

			real_father_even_if_dead = {
				OR = {
					trait = attractive
					trait = ugly
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				OR = {
					mother_even_if_dead = { trait = attractive }
					real_father_even_if_dead = { trait = attractive }
				}
			}

			# Fair
			random = {
				chance = 33

				mult_modifier = { # Both parents are fair
					factor = 2

					mother_even_if_dead = { trait = attractive }
					real_father_even_if_dead = { trait = attractive }
				}

				mult_modifier = { # One parent ugly
					factor = 0.5

					OR = {
						mother_even_if_dead = { trait = ugly }
						real_father_even_if_dead = { trait = ugly }
					}
				}

				add_trait = attractive
			}
		}

		if = {
			limit = {
				NOT = { trait = attractive }

				OR = {
					mother_even_if_dead = { trait = ugly }
					real_father_even_if_dead = { trait = ugly }
				}
			}

			# Ugly
			random = {
				chance = 33

				mult_modifier = { # Both parents are ugly
					factor = 2
					mother_even_if_dead = { trait = ugly }
					real_father_even_if_dead = { trait = ugly }
				}

				mult_modifier = { # One parent fair
					factor = 0.5
					OR = {
						mother_even_if_dead = { trait = attractive }
						real_father_even_if_dead = { trait = attractive }
					}
				}

				add_trait = ugly
			}
		}
	}
}

# 005: Fertility Check
character_event = {
	id = geneticPlus.005

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		immortal = no

		NOR = {
			trait = fertile
			trait = infertile
			has_character_flag = is_barren
		}
	}

	immediate = {
		# Barren
		random = {
			chance = 3

			mult_modifier = { # Increased chance if inbred
				factor = 10
				trait = inbred
			}

			mult_modifier = { # Increased chance if weak
				factor = 5
				is_weak_trigger = yes
			}

			mult_modifier = { # Reduced chance if strong
				factor = 0.5
				is_strong_trigger = yes
			}

			add_trait = infertile
			set_character_flag = is_barren
		}

		if = {
			limit = { NOT = { trait = infertile } }

			# Infertile
			random = {
				chance = 12

				mult_modifier = { # Increased chance if inbred
					factor = 3
					trait = inbred
				}

				mult_modifier = { # Increased chance if weak
					factor = 2
					is_weak_trigger = yes
				}

				mult_modifier = { # Reduced chance if strong
					factor = 0.5
					is_strong_trigger = yes
				}

				add_trait = infertile
			}
		}
	}
}

# 006: Height Check
character_event = {
	id = geneticPlus.006

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOR = {
			trait = giant
			trait = tall
			trait = dwarf
		}

		OR = {
			mother_even_if_dead = {
				OR = {
					trait = giant
					trait = tall
					trait = dwarf
				}
			}

			real_father_even_if_dead = {
				OR = {
					trait = giant
					trait = tall
					trait = dwarf
				}
			}
		}
	}

	immediate = {
		# Giant
		if = {
			limit = {
				OR = {
					mother_even_if_dead = { trait = giant }
					real_father_even_if_dead = { trait = giant }
				}
			}

			random = {
				chance = 33

				mult_modifier = { # Increased chance if both parents giants
					factor = 2

					mother_even_if_dead = { trait = giant }
					real_father_even_if_dead = { trait = giant }
				}

				add_trait = giant
			}
		}

		# Tall
		if = {
			limit = {
				NOT = { trait = giant }

				OR = {
					mother_even_if_dead = {
						OR = {
							trait = giant
							trait = tall
						}
					}

					real_father_even_if_dead = {
						OR = {
							trait = giant
							trait = tall
						}
					}
				}
			}

			random = {
				chance = 33

				mult_modifier = { # Increased chance if both parents tall
					factor = 2

					mother_even_if_dead = { trait = tall }
					real_father_even_if_dead = { trait = tall }
				}

				mult_modifier = { # Increased chance if one parent is a giant and the other is just tall
					factor = 2

					OR = {
						mother_even_if_dead = { trait = giant }
						real_father_even_if_dead = { trait = giant }
					}

					OR = {
						mother_even_if_dead = { trait = tall }
						real_father_even_if_dead = { trait = tall }
					}
				}

				add_trait = tall
			}
		}

		# Dwarf
		if = {
			limit = {
				NOR = {
					trait = giant
					trait = tall
				}

				OR = {
					mother_even_if_dead = { trait = dwarf }
					real_father_even_if_dead = { trait = dwarf }
				}
			}

			random = {
				chance = 33

				mult_modifier = { # Increased chance if both parents dwarves
					factor = 2

					mother_even_if_dead = { trait = dwarf }
					real_father_even_if_dead = { trait = dwarf }
				}

				add_trait = dwarf
			}
		}
	}
}

# 007: Masking Modifiers
character_event = {
	id = geneticPlus.007

	is_triggered_only = yes # geneticPlus.001
	hide_window = yes

	max_age = 6

	trigger = {
		has_game_rule = {
			name = hidden_character_traits_rule
			value = yes
		}
	}

	immediate = {
		if = {
			limit = { trait = genius }

			add_character_modifier = {
				name = hidden_genius
				years = 16
			}
		}

		if = {
			limit = { trait = quick }

			add_character_modifier = {
				name = hidden_quick
				years = 16
			}
		}

		if = {
			limit = { trait = slow }

			add_character_modifier = {
				name = hidden_slow
				years = 16
			}
		}

		if = {
			limit = { trait = imbecile }

			add_character_modifier = {
				name = hidden_imbecile
				years = 16
			}
		}

		if = {
			limit = { trait = strong }

			add_character_modifier = {
				name = hidden_strong
				years = 16
			}
		}

		if = {
			limit = { trait = weak }

			add_character_modifier = {
				name = hidden_weak
				years = 16
			}
		}

		if = {
			limit = { trait = attractive }

			add_character_modifier = {
				name = hidden_fair
				years = 16
			}
		}

		if = {
			limit = { trait = ugly }

			add_character_modifier = {
				name = hidden_ugly
				years = 16
			}
		}

		if = {
			limit = { trait = tall }

			add_character_modifier = {
				name = hidden_tall
				years = 16
			}
		}

		if = {
			limit = { trait = dwarf }

			add_character_modifier = {
				name = hidden_dwarf
				years = 16
			}
		}
	}
}

# 008: Divine Blood Check
character_event = {
	id = geneticPlus.008

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		OR = {
			mother_even_if_dead = { trait = divine_blood }
			real_father_even_if_dead = { trait = divine_blood }
			father_even_if_dead = { trait = divine_blood }
		}

		NOR = {
			trait = divine_blood
			trait = bastard
			trait = legit_bastard
		}
	}

	immediate = {
		add_trait = divine_blood
	}
}

#######################################
#
# 010 - 019: Setup Chain
#
#######################################
#
# INDEX
#
# 010: Remove Masking Modifiers
#
#######################################

# 010: Remove Masking Modifiers
character_event = {
	id = geneticPlus.010

	is_triggered_only = yes # on_adulthood
	hide_window = yes

	immediate = {
		remove_character_modifier = hidden_genius
		remove_character_modifier = hidden_quick
		remove_character_modifier = hidden_slow
		remove_character_modifier = hidden_imbecile
		remove_character_modifier = hidden_strong
		remove_character_modifier = hidden_weak
		remove_character_modifier = hidden_fair
		remove_character_modifier = hidden_ugly
		remove_character_modifier = hidden_tall
		remove_character_modifier = hidden_dwarf
	}
}