﻿
secret_disputed_heritage = {
	category = adultery

	is_valid = {
		secret_disputed_heritage_is_valid_trigger = {
			TARGET = scope:secret_target
		}
	}

	is_shunned = {	
		bastard_secret_is_shunned = {
			TARGET = scope:secret_target
			PARTICIPANT = scope:target
		}
	}

	is_criminal = {
		bastard_secret_is_criminal = {
			TARGET = scope:secret_target
			PARTICIPANT = scope:target
		}
	}

	on_owner_death = {
		save_scope_as = secret
		scope:secret_target = {
			save_scope_as = child
		}

		###SET NEW OWNERSHIP###
		#Should we set new secret owner(s)?
		if = {
			limit = {
				#Child knows about the secret
				is_known_by = scope:child
				scope:child = { is_alive = yes }
			}
			set_secret_owner = scope:child
		}
	}

	on_discover = {
		save_scope_as = secret
		scope:secret_target = {
			save_scope_as = child
			real_father = {
				save_scope_as = real_father
			}
			mother = {
				save_scope_as = mother
			}
			if =  {
				limit = {
					exists = father
				}
				father = {
					save_scope_as = father
				}
			}
			else = {
				debug_log = "secret_disputed_heritage was discovered for a child without a father!"
				debug_log_scopes = yes
			}
		}

		scope:child = { remove_inherited_descendent_traits_effect = yes }

		###Sends events###
		scope:discoverer = {
			if = {
				limit = { NOT = { has_character_flag = block_secret_disputed_heritage_discovery_event } } #Set by reveal_to_without_events_effect

				if = {
					limit = { is_ai = no } #Don't send any events to AI, they have no effect

					#CHILD#
					if = {
						limit = { this = scope:child }
						trigger_event = secrets.0501
					}
					#REAL FATHER#
					else_if = {
						limit = { this = scope:real_father }
						trigger_event = secrets.0502
					}
					#FATHER#
					else_if = {
						limit = {
							this = scope:father
							scope:mother = {
								NOT = { has_variable = borte_first_child_var }
							}
						}
						trigger_event = secrets.0500	
					}
					#CONSORT/LOVER OF CHILD (if we're now siblings!)
					else_if =  {
						limit = {
							any_sibling = {
								is_consort_of = scope:child
								has_relation_lover = scope:child
							}
						}
						save_my_known_father_effect = yes #Saves known_father for this character
						if = {
							limit = {
								scope:known_father = scope:real_father
								any_sibling = {
									father = scope:known_father
									OR = {
										is_consort_of = scope:child
										has_relation_lover = scope:child
									}
								}
							}

							random_sibling = {
								limit = {
									father = scope:known_father
									OR = {
										is_consort_of = scope:child
										has_relation_lover = scope:child
									}
								}
								save_scope_as = sibling_partner
							}
							trigger_event = secrets.0503
						}
					}		
				}
			}
			else = {
				remove_character_flag = block_secret_disputed_heritage_discovery_event
			}
		}

		#Set participants for this secret
		if = {
			limit = {
				OR = {
					scope:discoverer = { this = scope:real_father }
					scope:discoverer = { this = scope:child }
				}
			}
			add_secret_participant = scope:discoverer
		}
	}

	on_expose = {
		# Save scopes and set "knows"-flags for reference in events later on.
		save_scope_as = secret
		scope:secret_owner = { save_scope_as = owner }
		scope:secret_target = {
			#Child
			save_scope_as = child
			if = {
				limit = {
					scope:secret = {
						is_known_by = scope:child
					}
				}
				scope:child = {
					add_character_flag = child_knows
				}
			}

			#Mother
			mother = {
				save_scope_as = mother
			}

			#Real_father
			scope:child.real_father = {
				save_scope_as = real_father
				if = {
					limit = {
						scope:secret = {
							is_known_by = scope:real_father
						}
					}
					add_character_flag = real_father_knows
				}
			}

			#Father
			if = {
				limit = { exists = father }
				father = { save_scope_as = father }
				if = {
					limit = {
						scope:secret = {
							is_known_by = scope:father
						}
					}
					add_character_flag = father_knows
				}
			}
		}

		# Actual effects of exposing the secret
		hidden_effect = {
			scope:secret_owner = { secret_exposed_owner_effects_effect = { SECRET = scope:secret POV = scope:secret_exposer } } #Unop: Wrap in scope:secret_owner so the effect runs in character scope, matching how 00_secret_types.txt calls this effect
		}

		##############################
		# EXPOSURE EVENTS		  	 						
		#  These events exist for notification purposes — they only describe what has already happened, and do not trigger any actual gameplay effects
		##############################

		##Every secret knower & those who care about the child, mother and real_father (spouses/father are handled here)
		secret_exposed_notification_effect = yes

		#"Unique" targets
		scope:child = {
			remove_inherited_descendent_traits_effect = yes
			trigger_event = secret_bastard.2003
		}
		scope:mother = {
			trigger_event = {
				id = secret_bastard.2000
			}
		}
		scope:real_father = {
			trigger_event = {
				id = secret_bastard.2002
			}
		}

		if = {
			limit = {
				exists = scope:father
				scope:father = scope:secret_exposer
			}
			scope:father = {
				trigger_event = secret_bastard.2001
			}
		}
	}
}


secret_unmarried_illegitimate_child = {
	category = adultery
	
	is_valid = {
		secret_unmarried_illegitimate_child_is_valid_trigger = {
			TARGET = scope:secret_target
		}
	}

	is_shunned = {	
		bastard_secret_is_shunned = {
			TARGET = scope:secret_target
			PARTICIPANT = scope:target
		}
	}

	is_criminal = {
		bastard_secret_is_criminal = {
			TARGET = scope:secret_target
			PARTICIPANT = scope:target
		}
	}

	on_owner_death = {
		save_scope_as = secret
		scope:secret_target = {
			save_scope_as = child
		}

		###SET NEW OWNERSHIP###
		#Should we set new secret owner?
		if = {
			limit = {
				#Child knows about the secret
				scope:secret = { is_known_by = scope:child }
				scope:child = { is_alive = yes }
			}
			set_secret_owner = scope:child
		}
	}

	on_discover = {
		save_scope_as = secret
		scope:secret_target = {
			save_scope_as = child
			real_father = {
				save_scope_as = real_father
			}
			mother = {
				save_scope_as = mother
			}
		}

		###Sends events###
		scope:discoverer = {
			if = {
				limit = { NOT = { has_character_flag = block_secret_unmarried_illegitimate_child_discovery_event } } #Set by reveal_to_without_events_effect

				if = {
					limit = { is_ai = no } #Do not send any events to AI, they have no script effects (they exist for informational purposes only).

					#CHILD#
					if = {
						limit = { this = scope:child }
						trigger_event = secrets.0501
					}
					#REAL FATHER#
					else_if = {
						limit = { this = scope:real_father }
						trigger_event = secrets.0502
					}
					#CONSORT/LOVER OF CHILD (if we're now siblings!)
					else_if =  {
						limit = {
							any_sibling = {
								is_consort_of = scope:child
								has_relation_lover = scope:child
							}
						}
						save_my_known_father_effect = yes #Saves known_father for this character
						if = {
							limit = {
								scope:known_father = scope:real_father
								any_sibling = {
									father = scope:known_father
									OR = {
										is_consort_of = scope:child
										has_relation_lover = scope:child
									}
								}
							}

							random_sibling = {
								limit = {
									father = scope:known_father
									OR = {
										is_consort_of = scope:child
										has_relation_lover = scope:child
									}
								}
								save_scope_as = sibling_partner
							}
							trigger_event = secrets.0503
						}
					}		
				}
			}
			else = {
				remove_character_flag = block_secret_unmarried_illegitimate_child_discovery_event
			}
		}

		#Set participants for this secret
		if = {
			limit = {
				OR = {
					scope:discoverer = { this = scope:real_father }
					scope:discoverer = { this = scope:child }
				}
			}
			add_secret_participant = scope:discoverer
		}
	}

	on_expose = {
		#Save scopes and set "knows"-flags for reference in events later on.
		save_scope_as = secret
		scope:secret_owner = { save_scope_as = owner }
		scope:secret_target = {
			#Child
			save_scope_as = child
			if = {
				limit = {
					scope:secret = {
						is_known_by = scope:child
					}
				}
				scope:child = {
					add_character_flag = child_knows
				}
			}

			#Mother
			mother = {
				save_scope_as = mother
			}

			#Real_father
			scope:child.real_father = {
				save_scope_as = real_father
				if = {
					limit = {
						scope:secret = {
							is_known_by = scope:real_father
						}
					}
					add_character_flag = real_father_knows
				}
			}
		}


		# Actual effects of exposing the secret
		hidden_effect = {
			scope:secret_owner = { secret_exposed_owner_effects_effect = { SECRET = scope:secret POV = scope:secret_exposer } } #Unop: Wrap in scope:secret_owner so the effect runs in character scope, matching how 00_secret_types.txt calls this effect
		}
		if = { #Unop: Skip if the child has been adopted; the adoption has rebound them to a new family
			limit = { scope:child = { NOT = { has_character_flag = unop_was_adopted } } }
			scope:child = {
				set_father = scope:real_father
				set_parent_house_effect = yes
				remove_inherited_descendent_traits_effect = yes
			}
		}


		##############################
		# EXPOSURE EVENTS		  	 						
		#  These events exist for notification purposes — they only describe what has already happened, and do not trigger any actual gameplay effects
		##############################

		#Every secret knower & those who care about child(ren), mother and real_father (spouses are handled here)
		secret_exposed_notification_effect = yes

		#"Unique" targets
		scope:child = {
			if = {
				limit = {
					is_hostage = yes
					dynasty = warden.dynasty
				}
				save_scope_as = unhostaged_child
				warden = { save_scope_as = warden }
			}
			trigger_event = secret_bastard.3001
		}
		scope:mother = {
			trigger_event = secret_bastard.2000
		}
		scope:real_father = {
			if = {
				limit = {
					is_alive = yes
					NOT = { has_variable = do_not_notify_me_of_bastard_secret_reveal }
				}
				trigger_event = secret_bastard.3000
			}
		}
		scope:child = {
			if = {
				limit = { exists = scope:unhostaged_child }
				return_hostage = yes
				scope:warden = { add_courtier = scope:unhostaged_child } #Unop: root is the secret in on_expose, but add_courtier needs the child character
			}
		}
	}
}
