﻿on_birth_child = {
	on_actions = { mgr_sb_on_birth }
}

on_16th_birthday = {
	on_actions = { mgr_sb_reveal }
}

on_death = {
	on_actions = { mgr_sb_reveal }
}

mgr_sb_reveal = {
	effect = {
		if = {
			limit = {
				has_game_rule = reveal_secret_bastards
				exists = real_father
				OR = {
					NOT = { exists = father }
					NOT = { father = { this = root.real_father } }
				}
			}
			#Set father and house
			set_father = root.real_father
			set_parent_house_effect = yes
			if = {
				limit = {
					has_any_bastard_trait_trigger = no
				}
				add_bastard_trait_based_on_faith_effect = yes
			}
			# Biological parents, if alive, suffer penalties from adultery.
			if = {
				limit = {
					NOT = {
						real_father.faith = { has_doctrine = tenet_polyamory }
						mother.faith = { has_doctrine = tenet_polyamory }
					}
				}
				if = {
					limit = { mother = { is_alive = yes } }
					mother = {
						# Comprehensive effect which adds adulter/fornicator/incestuous traits, unfaithfulness & slept with wife opinions, and characters losing the appropriate number of piety level(s).
						expose_lover_secret_or_run_consequence_effect = {
							TARGET = root.mother
							EXPOSER = root.real_father
						}
					}
				}
				else_if = { limit = { real_father = { is_alive = yes } }
					# If the mother is dead, run this on the real_father instead (so it gets run somewhere). If the mother is alive, the real_father will have already suffered the penalties.
					real_father = {	
						expose_lover_secret_or_run_consequence_effect = {
							TARGET = root.real_father
							EXPOSER = root.mother
						}
					}
				}
			}
		}
	}
}

mgr_sb_on_birth = {
	effect = {
		#Secret Bastards
		if = {
			limit = {
				has_game_rule = no_secret_bastards
				OR = {
					scope:is_bastard = yes
					NOT = { scope:father = { this = scope:real_father } }
				}
			}
			if = {
				limit = {
					has_any_bastard_trait_trigger = no
				}
				add_bastard_trait_based_on_faith_effect = yes
			}

			if = {
				limit = {
					OR = {
						scope:mother = {
							has_character_flag = pregnancy_real_father_of_bastard_is_known_flag
						}
						has_game_rule = no_secret_bastards
					}
				}
				#Set father and house
				set_father = scope:real_father
				set_parent_house_effect = yes
			}

			#Set conditions for any twins
			scope:mother = {
				every_child = {
					limit = {
						is_twin_of = scope:child
					}
					add_bastard_trait_based_on_faith_effect = yes
					if = {
						limit = {
							OR = {
								scope:mother = {
									has_character_flag = pregnancy_real_father_of_bastard_is_known_flag
								}
								has_game_rule = no_secret_bastards
							}
						}
						#Set father and house
						set_father = scope:real_father
						set_parent_house_effect = yes
					}
				}
			}
			# Biological parents, if alive, suffer penalties from adultery.
			if = {
				limit = {
					NOT = {
						scope:real_father.faith = { has_doctrine = tenet_polyamory }
						scope:mother.faith = { has_doctrine = tenet_polyamory }
					}
				}
				if = {
					limit = { scope:mother = { is_alive = yes } }
					scope:mother = {
						# Comprehensive effect which adds adulter/fornicator/incestuous traits, unfaithfulness & slept with wife opinions, and characters losing the appropriate number of piety level(s).
						expose_lover_secret_or_run_consequence_effect = {
							TARGET = scope:real_father
							EXPOSER = scope:mother
						}
					}
				}
				else_if = { 
					limit = { scope:real_father = { is_alive = yes } }
					# If the mother is dead, run this on the real_father instead (so it gets run somewhere). If the mother is alive, the real_father will have already suffered the penalties.
					scope:real_father = {	
						expose_lover_secret_or_run_consequence_effect = {
							TARGET = scope:mother
							EXPOSER = scope:real_father
						}
					}
				}
			}
		}
	}
}