﻿namespace = RE_house_customizer

# =====================================================================
# 约定
#   root                     = 被修改的角色
#   scope:hc_selection       = 玩家选中的角色（来自 scope:recipient）
#   "合格家长" 判定统一写作：
#       OR = { can_be_parent_of = root  16 <= { value = age subtract = root.age } }
#   即：候选人 age - root.age >= 16（候选人比 root 年长）
# =====================================================================

# Event that fires on game start to tell you the instructions
RE_house_customizer.0001 = {
	type = character_event
	title = RE_house_customizer.0001.t
	desc = RE_house_customizer.0001.desc

	theme = family

	override_background = {
		reference = physicians_study
	}

	left_portrait = {
		character = root
		animation = throne_room_writer
	}

	immediate = {
		play_music_cue = "mx_cue_succession"
	}

	option = {
		name = RE_house_customizer.0001.b_real
		remove_character_flag = hc_fake_parents_mode
		add_character_flag = hc_real_parents_mode
		trigger_event = RE_house_customizer.0002
	}

	option = {
		name = RE_house_customizer.0001.b_fake
		remove_character_flag = hc_real_parents_mode
		add_character_flag = hc_fake_parents_mode
		trigger_event = RE_house_customizer.0002
	}

	option = {
		name = RE_house_customizer.back
		hidden_effect = {
			remove_character_flag = hc_real_parents_mode
			remove_character_flag = hc_fake_parents_mode
		}
	}
}


######################################################
### HOUSE CONVERSION AND CHARACTER RELATION EVENTS ###
######################################################

RE_house_customizer.0002 = {
	type = character_event
	title = RE_house_customizer.0002.t
	desc = RE_house_customizer.0002.desc

	theme = family

	override_background = { reference = study }

	left_portrait = {
		character = root
		animation = throne_room_writer
	}

	right_portrait = {
		character = scope:hc_selection
		animation = throne_room_writer
		trigger = { exists = scope:hc_selection }
	}

	immediate = {
		play_music_cue = "mx_cue_succession"
		scope:recipient = { save_scope_as = hc_selection }
	}

	# =========================
	# 1. 兄弟（同父母）
	# =========================
	option = {
		name = RE_house_customizer.0002.a

		trigger = {
			scope:hc_selection = { can_be_sibling_of = root }
		}

		set_house = scope:hc_selection.house

		if = {
			limit = { scope:hc_selection.father ?= { exists = yes } }
			set_father = scope:hc_selection.father
		}
		if = {
			limit = { scope:hc_selection.mother ?= { exists = yes } }
			set_mother = scope:hc_selection.mother
		}

		hidden_effect = {
			hc_sync_children_house_effect = yes
			hc_cleanup_parents_effect = yes
			remove_trait = re_house_customizer
			remove_character_flag = hc_real_parents_mode
			remove_character_flag = hc_fake_parents_mode
		}
	}

	# =========================
	# 2. 继兄弟（同父异母 / 同母异父）
	#    锚点 = 与 hc_selection 同家族的那位家长，另一位家长在 fake 模式下补全
	# =========================
	option = {
		name = RE_house_customizer.0002.aa

		trigger = {
			scope:hc_selection = { can_be_sibling_of = root }
		}

		set_house = scope:hc_selection.house

		# 原代码用 check_variable（CK3 无此 trigger，triggers.log 中不存在），
		# 改为用 flag 记录锚点是父亲还是母亲
		if = {
			limit = {
				scope:hc_selection.father ?= {
					exists = yes
					house = scope:hc_selection.house
				}
			}
			set_father = scope:hc_selection.father
			hidden_effect = { add_character_flag = hc_anchor_is_father }
		}
		else_if = {
			limit = {
				scope:hc_selection.mother ?= {
					exists = yes
					house = scope:hc_selection.house
				}
			}
			set_mother = scope:hc_selection.mother
			hidden_effect = { add_character_flag = hc_anchor_is_mother }
		}

		hidden_effect = {
			# 锚点是父亲 → 补母亲
			if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					has_character_flag = hc_anchor_is_father
					NOT = { exists = mother }
				}
				clear_saved_scope = hc_random_mother
				random_courtier = {
					limit = {
						is_female = yes
						is_adult = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
						NOT = { is_close_family_of = root }
					}
					save_scope_as = hc_random_mother
				}
				if = {
					limit = { NOT = { exists = scope:hc_random_mother } }
					create_character = {
						template = generic_female_peasant_character
						employer = root
						gender = female
						save_scope_as = hc_random_mother
					}
				}
				set_mother = scope:hc_random_mother
			}
			# 锚点是母亲 → 补父亲
			else_if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					has_character_flag = hc_anchor_is_mother
					NOT = { exists = father }
				}
				clear_saved_scope = hc_random_father
				random_courtier = {
					limit = {
						is_male = yes
						is_adult = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
						NOT = { is_close_family_of = root }
					}
					save_scope_as = hc_random_father
				}
				if = {
					limit = { NOT = { exists = scope:hc_random_father } }
					create_character = {
						template = generic_peasant_character
						employer = root
						gender = male
						save_scope_as = hc_random_father
					}
				}
				set_father = scope:hc_random_father
			}

			remove_character_flag = hc_anchor_is_father
			remove_character_flag = hc_anchor_is_mother
			hc_sync_children_house_effect = yes
			hc_cleanup_parents_effect = yes
			remove_trait = re_house_customizer
			remove_character_flag = hc_real_parents_mode
			remove_character_flag = hc_fake_parents_mode
		}
	}

	# =========================
	# 3. 孩子（合法子女）
	#    real 模式：取 hc_selection 的现任配偶；fake 模式：随机宫廷角色，找不到就生成
	# =========================
	option = {
		name = RE_house_customizer.0002.b

		trigger = {
			scope:hc_selection = {
				OR = {
					can_be_parent_of = root
					16 <= { value = age subtract = root.age }
				}
			}
		}

		set_house = scope:hc_selection.house

		if = {
			limit = { scope:hc_selection = { is_male = yes } }
			set_father = scope:hc_selection

			if = {
				limit = {
					has_character_flag = hc_real_parents_mode
					scope:hc_selection.primary_spouse ?= {
						is_female = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
					}
				}
				set_mother = scope:hc_selection.primary_spouse
			}
			else_if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					NOT = { exists = mother }
				}
				clear_saved_scope = hc_random_mother
				random_courtier = {
					limit = {
						is_female = yes
						is_adult = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
						NOT = { is_close_family_of = root }
					}
					save_scope_as = hc_random_mother
				}
				if = {
					limit = { NOT = { exists = scope:hc_random_mother } }
					create_character = {
						template = generic_female_peasant_character
						employer = root
						gender = female
						save_scope_as = hc_random_mother
					}
				}
				set_mother = scope:hc_random_mother
			}
		}
		else = {
			set_mother = scope:hc_selection

			if = {
				limit = {
					has_character_flag = hc_real_parents_mode
					scope:hc_selection.primary_spouse ?= {
						is_male = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
					}
				}
				set_father = scope:hc_selection.primary_spouse
			}
			else_if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					NOT = { exists = father }
				}
				clear_saved_scope = hc_random_father
				random_courtier = {
					limit = {
						is_male = yes
						is_adult = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
						NOT = { is_close_family_of = root }
					}
					save_scope_as = hc_random_father
				}
				if = {
					limit = { NOT = { exists = scope:hc_random_father } }
					create_character = {
						template = generic_peasant_character
						employer = root
						gender = male
						save_scope_as = hc_random_father
					}
				}
				set_father = scope:hc_random_father
			}
		}

		hidden_effect = {
			hc_sync_children_house_effect = yes
			hc_cleanup_parents_effect = yes
			remove_trait = re_house_customizer
			remove_character_flag = hc_real_parents_mode
			remove_character_flag = hc_fake_parents_mode
		}
	}

	# =========================
	# 4. 私生子（只在 fake 模式补全另一位家长）
	# =========================
	option = {
		name = RE_house_customizer.0002.bb

		trigger = {
			scope:hc_selection = {
				OR = {
					can_be_parent_of = root
					16 <= { value = age subtract = root.age }
				}
			}
		}

		set_house = scope:hc_selection.house

		if = {
			limit = { scope:hc_selection = { is_male = yes } }
			set_father = scope:hc_selection

			if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					NOT = { exists = mother }
				}
				clear_saved_scope = hc_random_mother
				random_courtier = {
					limit = {
						is_female = yes
						is_adult = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
						NOT = { is_close_family_of = root }
					}
					save_scope_as = hc_random_mother
				}
				if = {
					limit = { NOT = { exists = scope:hc_random_mother } }
					create_character = {
						template = generic_female_peasant_character
						employer = root
						gender = female
						save_scope_as = hc_random_mother
					}
				}
				set_mother = scope:hc_random_mother
			}
		}
		else = {
			set_mother = scope:hc_selection

			if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					NOT = { exists = father }
				}
				clear_saved_scope = hc_random_father
				random_courtier = {
					limit = {
						is_male = yes
						is_adult = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
						NOT = { is_close_family_of = root }
					}
					save_scope_as = hc_random_father
				}
				if = {
					limit = { NOT = { exists = scope:hc_random_father } }
					create_character = {
						template = generic_peasant_character
						employer = root
						gender = male
						save_scope_as = hc_random_father
					}
				}
				set_father = scope:hc_random_father
			}
		}

		add_trait = bastard
		hidden_effect = {
			hc_sync_children_house_effect = yes
			hc_cleanup_parents_effect = yes
			remove_trait = re_house_customizer
			remove_character_flag = hc_real_parents_mode
			remove_character_flag = hc_fake_parents_mode
		}
	}

	# =========================
	# 5. 亲属
	# =========================
	option = {
		name = RE_house_customizer.0002.c

		trigger = {
			scope:hc_selection = {
				every_close_or_extended_family_member = {
					even_if_dead = yes
					is_adult = yes
					house = scope:hc_selection.house
					NOT = { this = scope:hc_selection }
				}
			}
		}

		hidden_effect = {
			clear_variable_list = possible_characters
			add_character_flag = relative_pop_living
			hc_cleanup_parents_effect = yes
			remove_character_flag = hc_real_parents_mode
			remove_character_flag = hc_fake_parents_mode
			trigger_event = RE_house_customizer.0003
		}
	}

	# =========================
	# 返回
	# =========================
	option = {
		name = RE_house_customizer.back
		hidden_effect = {
			hc_cleanup_parents_effect = yes
			remove_character_flag = hc_real_parents_mode
			remove_character_flag = hc_fake_parents_mode
			trigger_event = RE_house_customizer.0001
		}
	}
}

########################
## MAINTENANCE EVENTS ##
########################

RE_house_customizer.0003 = {
	type = character_event
	title = RE_house_customizer.0003.t
	desc = RE_house_customizer.0003.desc
	theme = family

	override_background = { reference = study }

	left_portrait = {
		character = root
		animation = throne_room_writer
	}

	right_portrait = scope:hc_selection

	widget = {
		is_shown = { is_ai = no }
		gui = "event_window_character_selection_tall"
		container = "custom_widgets_container"
	}

	immediate = {
		clear_variable_list = possible_characters
		scope:recipient = {
			save_scope_as = hc_selection
		}

		# =========================
		# 近亲
		# =========================
		if = {
			limit = { has_character_flag = relative_pop_living }

			scope:hc_selection = {
				every_close_family_member = {
					even_if_dead = yes
					limit = {
						is_adult = yes
						house = scope:hc_selection.house
						OR = {
							can_be_parent_of = root
							AND = {
								is_female = yes
								16 <= { value = root.age subtract = age }
							}
						}
					}
					root = {
						add_to_variable_list = {
							name = possible_characters
							target = prev
						}
					}
				}
			}
		}

		# =========================
		# 远亲（排除近亲）
		# =========================
		else_if = {
			limit = { has_character_flag = distant_relative_pop_living }

			scope:recipient = {
				save_scope_as = hc_selection
			}

			scope:hc_selection = {
				every_extended_family_member = {
					even_if_dead = yes
					limit = {
						is_adult = yes
						house = scope:hc_selection.house
						NOT = { is_close_family_of = scope:hc_selection }
						OR = {
							can_be_parent_of = root
							AND = {
								is_female = yes
								16 <= { value = root.age subtract = age }
							}
						}
					}
					root = {
						add_to_variable_list = {
							name = possible_characters
							target = prev
						}
					}
				}
			}
		}

		# =========================
		# 排序
		# =========================
		ordered_in_list = {
			variable = possible_characters
			order_by = age
			root = { set_variable = { name = si_selected_character value = prev } }
			prev = { save_scope_as = hc_random_relative_selection }
		}
	}

	# =========================
	# 选择角色
	# =========================
	option = {
		name = "RE_house_customizer.0003.li"

		trigger = {
			OR = {
				has_character_flag = relative_pop_living
				has_character_flag = distant_relative_pop_living
			}
		}

		# 维护变量
		var:si_selected_character = { save_scope_as = hc_random_relative_selection }
		clear_variable_list = possible_characters

		set_house = scope:hc_random_relative_selection.house

		# =========================
		# 父母设置：优先当前配偶，fallback已死亡配偶
		# =========================
		if = {
			limit = { scope:hc_random_relative_selection = { is_male = yes } }

			set_father = scope:hc_random_relative_selection

			if = {
				limit = {
					scope:hc_random_relative_selection.primary_spouse ?= {
						is_female = yes
						OR = {
							can_be_parent_of = root
							16 <= { value = age subtract = root.age }
						}
					}
				}
				set_mother = scope:hc_random_relative_selection.primary_spouse
			}
			else_if = {
				limit = {
					scope:hc_random_relative_selection = {
						any_spouse = {
							even_if_dead = yes
							is_female = yes
							OR = {
								can_be_parent_of = root
								16 <= { value = age subtract = root.age }
							}
						}
					}
				}
				scope:hc_random_relative_selection = {
					random_spouse = {
						limit = {
							even_if_dead = yes
							is_female = yes
							OR = {
								can_be_parent_of = root
								16 <= { value = age subtract = root.age }
							}
						}
						save_scope_as = hc_relative_spouse
					}
				}
				set_mother = scope:hc_relative_spouse
			}
		}
		else = {
			set_mother = scope:hc_random_relative_selection

			if = {
				limit = {
					scope:hc_random_relative_selection.primary_spouse ?= {
						is_male = yes
						can_be_parent_of = root
					}
				}
				set_father = scope:hc_random_relative_selection.primary_spouse
			}
			else_if = {
				limit = {
					scope:hc_random_relative_selection = {
						any_spouse = {
							even_if_dead = yes
							is_male = yes
							can_be_parent_of = root
						}
					}
				}
				scope:hc_random_relative_selection = {
					random_spouse = {
						limit = {
							even_if_dead = yes
							is_male = yes
							can_be_parent_of = root
						}
						save_scope_as = hc_relative_spouse
					}
				}
				set_father = scope:hc_relative_spouse
			}
		}

		# 子女同步家族
		if = {
			limit = { any_child = { NOT = { house = root.house } } }
			every_child = {
				limit = { NOT = { house = root.house } }
				set_house = root.house
			}
		}

		# 清理
		remove_character_flag = relative_pop_living
		remove_character_flag = distant_relative_pop_living

		hidden_effect = {
			hc_cleanup_parents_effect = yes
			remove_trait = re_house_customizer
		}
	}

	# =========================
	# 返回
	# =========================
	option = {
		name = RE_house_customizer.0003.back
		custom_tooltip = RE_house_customizer.0003.back.tt

		trigger = {
			OR = {
				has_character_flag = relative_pop_living
				has_character_flag = distant_relative_pop_living
			}
		}

		clear_variable_list = possible_characters
		remove_character_flag = relative_pop_living
		remove_character_flag = distant_relative_pop_living

		trigger_event = RE_house_customizer.0002
	}
}