﻿#AGOTCCH Overridden

agot_canon_children_after_birth_effect = {
	if = {
		limit = { is_male = yes }
		change_first_name = $NAME_MALE$
	}
	else_if = {
		limit = { is_female = yes }
		change_first_name = $NAME_FEMALE$
	}

	#add_character_flag = $FLAG$
	make_trait_inactive = $TRAIT$
	copy_inheritable_appearance_from = character:$DNA$

	if = {
		limit = { has_game_rule = agotcch_custom_dna_disabled }
		copy_inheritable_appearance_from = character:$DNA$
	}
	#AGOTCCH Added
	else = {
		if = {
			limit = { exists = character:$DNA$_agotcch }
			copy_inheritable_appearance_from = character:$DNA$_agotcch
		}
		else = {
			copy_inheritable_appearance_from = character:$DNA$
		}
	}

	add_to_global_variable_list = { # For retrieving scopes (scopes become unavailable on death)
		name = agot_born_canon_children_scopes
		target = this
	}
}

agot_canon_children_setup_mother_effect = {
	$FATHER$ = { save_scope_as = canon_father }

	if = { # Mother has historical ID
		limit = {
			$MOTHER$ ?= {
				exists = yes
				is_alive = yes
			}
		}
		$MOTHER$ = { save_scope_as = canon_mother }
	}
	else = { # Mother is canon child
		agot_canon_children_get_canon_child_scope_effect = {
			TRAIT = $MOTHER_TRAIT$
			SCOPE = canon_mother
		}
	}

	scope:canon_mother ?= { # Setup mother
		if = {
			limit = { is_pregnant = yes }
			end_pregnancy = yes
		}

		if = {
			limit = { NOT = { has_inactive_trait = $MOTHER_TRAIT$ } }
			make_trait_inactive = $MOTHER_TRAIT$
		}

		if = {
			limit = {
				always = $PREVENT_PREGNANCY$

				#AGOTCCH Added
				OR = {
					has_game_rule = agotcch_block_non_canon_children_enabled
					AND = {
						has_game_rule = agotcch_block_non_canon_children_player_disabled
						is_ai = yes
						scope:real_father = { is_ai = yes }
						pregnancy_assumed_father = { is_ai = yes }
					}
				}
			}

			add_character_flag = { # For renewing the prevent pregnancy flag
				flag = canon_children_should_prevent_pregnancy
			}

			add_character_flag = {
				flag = canon_children_prevent_pregnancy
				months = agot_canon_children_pregnancy_modifier_duration
			}
		}

		scope:canon_father = {
			add_to_variable_list = {
				name = canon_mothers
				target = scope:canon_mother
			}
		}
	}
}


#AGOTCCH Added

agotcch_populate_canon_spouse_effect = {
	if = {
		limit = {
			has_variable = agot_canon_spouse
		}

		var:agot_canon_spouse = {
			if = {
				limit = {
					is_alive = yes
					NOT = { is_in_list = characters }
				}
			}
			add_to_list = characters
		}
	}	
}

agotcch_setup_spouse_effect = {
	if = {
		limit = {
			has_game_rule = agotcch_canon_marriage_enabled
		}

		$SPOUSE_1$ ?= {
			save_temporary_scope_as = actor
	
			agot_canon_children_get_character_scope_effect = {
				TARGET = $SPOUSE_2$
				TRAIT = $SPOUSE_2_TRAIT$
				SCOPE = spouse
			}
	
			if = {
				limit = { exists = scope:spouse }
	
				set_variable = {
					name = agot_canon_spouse
					value = scope:spouse
				}
	
				scope:spouse = {
					set_variable = {
						name = agot_canon_spouse
						value = scope:actor
					}
				}
			}
		}
	}
}

agotcch_marriage_effect = {
	trigger_event = {
		on_action = agotcch_attempt_canon_marriage # TODO: Currently only implemented for AI
	}
}
