﻿namespace = RE_house_customizer

# 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
		trigger = { always = yes }
		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. 继兄弟（同父异母 / 同母异父）
	# =========================
	option = {
		name = RE_house_customizer.0002.aa

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

		set_house = scope:hc_selection.house

		set_variable = {
			name = hc_anchor_is_father
			value = no
		}

		if = {
			limit = {
				scope:hc_selection.father = {
					exists = yes
					house = scope:hc_selection.house
				}
			}
			set_father = scope:hc_selection.father
			set_variable = {
				name = hc_anchor_is_father
				value = yes
			}
		}
		else_if = {
			limit = {
				scope:hc_selection.mother = {
					exists = yes
					house = scope:hc_selection.house
				}
			}
			set_mother = scope:hc_selection.mother
		}

		hidden_effect = {
			if = {
				limit = {
					check_variable = { which = hc_anchor_is_father value = yes }
					NOT = { exists = mother }
					has_character_flag = hc_fake_parents_mode
				}
				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
			}
			if = {
				limit = {
					check_variable = { which = hc_anchor_is_father value = no }
					NOT = { exists = father }
					has_character_flag = hc_fake_parents_mode
				}
				random_courtier = {
					limit = {
						is_male = yes
						is_adult = yes
						can_be_parent_of = root
						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
			}
			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. 孩子（合法子女）
	# =========================
	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

			# real模式：只查活着的当前配偶
			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
			}
			# fake模式：随机宫廷女性，找不到就生成
			else_if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					NOT = { exists = 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

			# real模式：只查活着的当前配偶
			if = {
				limit = {
					has_character_flag = hc_real_parents_mode
					scope:hc_selection.primary_spouse ?= {
						is_male = yes
						can_be_parent_of = root
					}
				}
				set_father = scope:hc_selection.primary_spouse
			}
			# fake模式：随机宫廷男性，找不到就生成
			else_if = {
				limit = {
					has_character_flag = hc_fake_parents_mode
					NOT = { exists = father }
				}
				random_courtier = {
					limit = {
						is_male = yes
						is_adult = yes
						can_be_parent_of = root
						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. 私生子
	# =========================
	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
			# fake模式才补全母亲
			if = {
				limit = {
					NOT = { exists = mother }
					has_character_flag = hc_fake_parents_mode
				}
				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
			# fake模式才补全父亲
			if = {
				limit = {
					NOT = { exists = father }
					has_character_flag = hc_fake_parents_mode
				}
				random_courtier = {
					limit = {
						is_male = yes
						is_adult = yes
						can_be_parent_of = root
						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 = legitimized_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. 堂亲 → 0003
	# =========================
	option = {
		name = RE_house_customizer.0002.c

		trigger = {
			scope:hc_selection = {
				any_close_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.0002.d

		trigger = {
			scope:hc_selection = {
				any_extended_family_member = {
					even_if_dead = yes
					is_adult = yes
					house = scope:hc_selection.house
					NOT = { is_close_family_of = scope:hc_selection }
				}
			}
		}
		hidden_effect = {
			clear_variable_list = possible_characters
			add_character_flag = distant_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
	}
}