

should_mother_give_house_to_bastard_trigger = {
	#Also governs the "should_mother_give_house_to_bastard" scripted rule
	trigger_if = {
		limit = { NOT = { exists = mother } }
		always = no
	}
	trigger_else_if = {
		limit = { NOT = { exists = mother.house } }
		always = no
	}
	trigger_else_if = {
		limit = { mother = { matri_marriage_allowed = no } }
		always = no
	}
	trigger_else_if = {
		limit = { NOT = { exists = father } }
		always = yes
	}
	trigger_else_if = {
		limit = {
			real_father = { is_ruler = yes }
			mother = { is_ruler = no }
			exists = real_father.house
		}
		always = no
	}
	trigger_else_if = {
		limit = {
			real_father = { is_ruler = no }
			mother = { is_ruler = yes }
			exists = mother.house
		}
		always = yes
	}
	trigger_else_if = {
		limit = {
			mother = { NOT = { exists = house } }
			exists = real_father.house
		}
		always = no
	}
	trigger_else_if = {
		limit = {
			real_father = { NOT = { exists = house } }
			exists = mother.house
		}
		always = yes
	}
	# making the default behavior explicit if no other trigger_(else_)ifs are valid
	trigger_else = {
		always = yes
	}
}

secret_unmarried_illegitimate_child_is_valid_trigger = {
	$TARGET$ = {
		is_alive = yes
		exists = real_father
		exists = mother
		NOT = { has_trait = child_of_concubine }
		save_temporary_scope_as = temp_child
		trigger_if = {
			limit = {
				exists = father
				exists = scope:temp_child.real_father
			}
			NOT = { father = scope:temp_child.real_father }
		}
	}
}

secret_disputed_heritage_is_valid_trigger = {
	$TARGET$ = { # Child
		is_alive = yes
		exists = real_father
		exists = father
		exists = mother
		NOT = { real_father = father }
		NOR = {
			has_trait = bastard
			has_trait = wild_oat
			has_trait = legitimized_bastard
		}
	}
}

bastard_secret_is_criminal = {
	$TARGET$ = { save_temporary_scope_as = criminal_bastard }
	$PARTICIPANT$ = { save_temporary_scope_as = criminal_participant }
	OR = {
		#Mother if female adultery is criminal
		scope:criminal_participant = {
			scope:criminal_bastard = { mother = scope:criminal_participant }
			OR = {
				faith = { has_doctrine_parameter = adultery_female_crime }
				any_liege_or_above = { faith = { has_doctrine_parameter = adultery_female_crime } }
			}
		}
		#Real father is male adultery is criminal
		scope:criminal_participant = {
			scope:criminal_bastard = { real_father = scope:criminal_participant }
			OR = {
				faith = { has_doctrine_parameter = adultery_male_crime }
				any_liege_or_above = { faith = { has_doctrine_parameter = adultery_male_crime } }
			}
		}
	}
}


bastard_secret_is_shunned = {
	# FOR DEBUG PURPOSES -- REMOVE ONCE RESOLVED TODO_CD
	assert_if = {
		limit = { NOT = { exists = $TARGET$ } }
		text = "Empty TARGET was passed to bastard_secret_is_shunned!"
	}
	assert_if = {
		limit = { NOT = { exists = $TARGET$ } }
		text = "Empty PARTICIPANT was passed to bastard_secret_is_shunned!"
	}

	#Saving bastard & other parent scope
	$TARGET$ = { save_temporary_scope_as = bastard }
	$PARTICIPANT$ = { save_temporary_scope_as = participant }

	assert_if = {
		limit = { NOT = { exists = scope:bastard } }
		text = "scope:bastard does not exist in bastard_secret_is_shunned!"
	}
	assert_if = {
		limit = { NOT = { exists = scope:participant } }
		text = "scope:participant does not exist in bastard_secret_is_shunned!"
	}
	assert_if = {
		limit = { NOT = { exists = scope:bastard.mother } }
		text = "scope:bastard.mother does not exist in bastard_secret_is_shunned!"
	}
	assert_if = {
		limit = { NOT = { exists = scope:bastard.real_father } }
		text = "scope:bastard.real_father does not exist in bastard_secret_is_shunned!"
	}

	OR = {
		#Bastard child: always shunned, never criminal
		scope:participant = scope:bastard
		#Mother if female adultery is shunned
		scope:participant = {
			exists = scope:bastard.mother
			this = scope:bastard.mother
			OR = {
				faith = { has_doctrine_parameter = adultery_female_shunned }
				any_liege_or_above = { faith = { has_doctrine_parameter = adultery_female_shunned } }
			}
		}
		#Real father is male adultery is shunned
		scope:participant = {
			exists = scope:bastard.real_father
			this = scope:bastard.real_father
			OR = {
				faith = { has_doctrine_parameter = adultery_male_shunned }
				any_liege_or_above = { faith = { has_doctrine_parameter = adultery_male_shunned } }
			}
		}
		#Either parent if incest is shunned
		AND = {
			# Can't have incest with only one parent!
			exists = scope:bastard.real_father
			exists = scope:bastard.mother

			# Also the secret participant has to be one of the parents
			scope:participant = {
				OR = {
					this = scope:bastard.mother
					this = scope:bastard.real_father
				}
				trigger_if = {
					limit = { this = scope:bastard.mother }
					scope:bastard.real_father = { save_temporary_scope_as = other_parent }
				}
				trigger_else = {
					scope:bastard.mother = { save_temporary_scope_as = other_parent }
				}
					
				# Check if the relation would be incestuous in my faith or my liege's faith
				relation_with_character_is_incestuous_in_faith_trigger = {
					CHARACTER = scope:other_parent
					FAITH = scope:participant.faith
				}
				any_liege_or_above = {
					save_temporary_scope_as = liege_or_above
					scope:participant = {
						relation_with_character_is_incestuous_in_faith_trigger = {
							CHARACTER = scope:other_parent
							FAITH = scope:liege_or_above.faith
						}
					}
				}
			}
		}
	}
	NOT = {
		bastard_secret_is_criminal = {
			TARGET = scope:bastard
			PARTICIPANT = scope:participant
		}
	}
}
