﻿#
#Sets the correct "parent house" for a known bastard
set_parent_house_effect = {
	if = {
		limit = {
			age < bastard_legitimacy_change_cutoff_age
		}
		if = {
			limit = {
				should_mother_give_house_to_bastard_trigger = yes
			}
			set_house = mother.house
		}
		else_if = {
			limit = {
				exists = father
				exists = father.house
			}
			set_house = father.house
		}
		else = {
			set_to_lowborn = yes
		}
		# Family Matters Starts
		hidden_effect = {
			on_birth_child_patrilines_effect = yes
			on_birth_child_matrilines_effect = yes
			on_birth_child_dynastylines_effect = yes
			on_birth_child_familymarks_effect = yes
			on_birth_child_otherlines_effect = yes
		}
		# Family Matters Ends
	}
}

#Effect run in the legitimize bastard interactions
legitimize_bastard_interaction_opinions_effect = {
	hidden_effect = {
		$LEGITIMIZER$ = {
			save_scope_as = actor
		}
		if = {
			limit = {
				exists = $HOUSE_HEAD$
			}
			$HOUSE_HEAD$ = {
				save_scope_as = house_head
			}
		}

		if = {
			limit = {
				scope:actor = {
					is_parent_of = $BASTARD$
				}
			}
			scope:actor = {
				save_scope_as = parent
			}
		}
		else = {
			$BASTARD$ = {
				random_parent = {
					limit = {
						house = scope:actor.house
						is_playable_character = yes
					}
					alternative_limit = {
						house = scope:actor.house	
					}
					alternative_limit = {
						always = yes
					}
					weight = {
						base = 1
						compare_modifier = {
							value = highest_held_title_tier
							multiplier = 1000
						}
					}
					save_scope_as = parent
				}
			}
		}
	}
	# Reveal secret if you are the father
	if = {
		limit = {
			any_secret = {
				secret_type = secret_unmarried_illegitimate_child
				secret_target = {
					real_father = {
						this = scope:actor
					}
				}
			}
		}
		random_secret = {
			limit = {
				secret_type = secret_unmarried_illegitimate_child
				secret_target = {
					real_father = {
						this = root
					}
				}
			}
			expose_secret = scope:actor
		}
	}

	$BASTARD$ = {
		hidden_effect = { remove_trait = bastard }
		add_trait_force_tooltip = legitimized_bastard
		add_opinion = {
			target = scope:actor
			modifier = legitimized_me_opinion
		}
		scope:actor = {
			if = {
				limit = {
					is_parent_of = $BASTARD$
				}
				$BASTARD$ = {
					random_parent = {
						limit = { NOT = { this = scope:actor } }
						add_opinion = {
							target = scope:actor
							modifier = legitimized_my_child_opinion
						}
					}
				}
				every_spouse = {
					limit = {
						NOT = { is_parent_of = $BASTARD$ }
					}
					add_opinion = {
						target = scope:actor
						modifier = legitimized_a_bastard_opinion
					}
				}
				if = {
					limit = {
						any_child = {
							is_adult = yes
							NOT = { this = $BASTARD$ }
							NOR = {
								has_trait = bastard
								has_trait = legitimized_bastard
								opinion = {
									value >= 40
									target = $BASTARD$
								}
							}
						}
					}
					every_child = {
						limit = {
							is_adult = yes
							NOT = { this = $BASTARD$ }
							NOR = {
								has_trait = bastard
								has_trait = legitimized_bastard
								opinion = {
									value >= 40
									target = $BASTARD$
								}
							}
						}
						custom =  every_child_adult_non_bastard_dislike
						add_opinion = {
							target = scope:actor
							modifier = legitimized_a_bastard_opinion
						}
					}
				}
			}
		}
	}

	hidden_effect = {
		$BASTARD$ = {
			trigger_event = bastard_interaction.0001
			
			save_scope_as = legitimized_bastard

			if = {
				limit = {
					NOT = { exists = scope:newborn_legitimization }
				}
				every_parent = {
					limit = { NOT = { this = scope:actor } }
					trigger_event = bastard_interaction.0003
				}
			}
			# Family Matters Starts
			on_birth_child_patrilines_effect = yes
			on_birth_child_matrilines_effect = yes
			on_birth_child_dynastylines_effect = yes
			on_birth_child_familymarks_effect = yes
			on_birth_child_otherlines_effect = yes
			# Family Matters Ends
		}

		scope:actor = {
			if = {
				limit = {
					NOT = { exists = scope:newborn_legitimization }
					is_parent_of = $BASTARD$
				}
				trigger_event = bastard_interaction.0002
			}

			if = {
				limit = {
					is_parent_of = $BASTARD$
				}
				every_spouse = {
					limit = {
						NOT = { is_parent_of = $BASTARD$ }
						NOT = { this = scope:actor }
					}
					$BASTARD$ = { save_scope_as = legitimized_bastard }
					trigger_event = bastard_interaction.0004
				}
				every_child = {
					limit = {
						is_adult = yes
						NOT = { this = $BASTARD$ }
						NOT = { this = scope:actor }
					}
					trigger_event = bastard_interaction.0005
				}
			}
			else = {
				scope:parent = {
					if = {
						limit = {
							NOT = { exists = scope:newborn_legitimization }
						}
						trigger_event = bastard_interaction.0006
					}
				
					every_spouse = {
						limit = {
							NOT = { is_parent_of = $BASTARD$ }
							NOT = { this = scope:actor }
						}
						trigger_event = bastard_interaction.0007
					}

					every_child = {
						limit = {
							is_adult = yes
							NOT = { this = $BASTARD$ }
							NOT = { this = scope:actor }
						}
						trigger_event = bastard_interaction.0008
					}
				}
			}
		}
	}
}