﻿namespace = agot_hf_new_house_name_generation_events

agot_hf_new_house_name_generation_events.0001 = { #MODIFIED Badger's Custom Cadet Name Generator
	type = character_event

	hidden = yes

	immediate = {
		if = {
			limit = { has_dynasty = yes }

			agot_hf_random_name_flag = yes

			set_global_variable = {
				name = new_house_name_generation
				value = root
			}

			ROOT.house = {
				set_house_name = agot_generated_house_name

				if = {
					limit = {
						ROOT = {
							has_character_flag = has_personal_coa
						}
					}
					set_coa = ROOT.var:my_personal_coa.house
				}
			}

			ROOT.dynasty = {
				set_dynasty_name = agot_generated_house_name_again
			}

			remove_global_variable = new_house_name_generation

			add_character_flag = generated_name_given
		}
		if = {
			limit = { is_landed = yes }
			trigger_event = agot_coa_events.0001
		}
	}
}

agot_hf_new_house_name_generation_events.0002 = {
	type = character_event

	hidden = yes

	immediate = {
		if = {
			limit = {
				OR = {
					agothf_agot_dynasties = yes
					agothf_agot_houses = yes
				}
			}
			add_character_flag = generated_name_given
		}
		else_if = {
			limit = {
				NOT = { agot_has_bespoke_dynasty_trigger = yes }
				NOT = { is_from_ruler_designer = yes }
				NOT = { has_character_flag = generated_name_given }
			}
			trigger_event = agot_hf_new_house_name_generation_events.0001
		}
	}
}

agot_hf_new_house_name_generation_events.0003 = {
	type = character_event
	title = agot_hf_new_house_name_generation_events.0003.t
	desc = agot_hf_new_house_name_generation_events.0003.desc

	theme = court

	override_background = {
		reference = courtyard
	}

	right_portrait = {
		character = scope:new_ruler
		animation = war_defender
	}

	trigger = {
		NOT = { has_character_flag = agot_hf_new_house_name_generation_events_0003_sent }
		NOT = { has_game_rule = agot_hf_cadet_conversion_disabled }
		NOT = { has_game_rule = agot_cadet_none }
		exists = scope:new_ruler.house
	}

	immediate = {
		if = {
			limit = { NOT = { scope:new_ruler.culture = scope:new_ruler.capital_county.culture } }
			scope:new_ruler = {
				add_character_flag = {
					flag = different_culture
					days = 40
				}
			}
		}
	}

	#FULL CONVERSION
	option = {
		name = agot_hf_new_house_name_generation_events.0003.a
		trigger = { NOT = { has_character_flag = valyrian_cadet } }

		scope:new_ruler = {
			set_culture = scope:new_ruler.capital_county.culture
			if = {
				limit = { NOT = { scope:new_ruler.faith.religion = scope:new_ruler.capital_county.faith.religion } }
				set_character_faith = scope:new_ruler.capital_county.faith
			}

			if = {
				limit = { any_child = { count > 0 } }
				every_child = {
					set_culture = scope:new_ruler.capital_county.culture
					if = {
						limit = { NOT = { scope:new_ruler.faith.religion = scope:new_ruler.capital_county.faith.religion } }
						set_character_faith = scope:new_ruler.capital_county.faith
					}
				}
			}
			if = {
				limit = { house = { any_house_member = { is_grandchild_of = scope:new_ruler } } }
				house = {
					every_house_member = {
						limit = {
							is_grandchild_of = scope:new_ruler
						}
						set_culture = scope:new_ruler.capital_county.culture
						if = {
							limit = { NOT = { scope:new_ruler.faith.religion = scope:new_ruler.capital_county.faith.religion } }
							set_character_faith = scope:new_ruler.capital_county.faith
						}
					}
				}
			}

			if = {
				limit = {
					NOT = { scope:new_ruler = scope:new_ruler.dynasty.dynast }
					NOT = { scope:new_ruler = scope:new_ruler.house.house_head }
					scope:new_ruler = { has_character_flag = different_culture }
					trigger_if = {
						limit = { exists = scope:new_ruler.dynasty.dynast.primary_heir }
						NOT = { scope:new_ruler = scope:new_ruler.dynasty.dynast.primary_heir }
					}
					trigger_if = {
						limit = { exists = scope:new_ruler.house.house_head.primary_heir }
						NOT = { scope:new_ruler = scope:new_ruler.house.house_head.primary_heir }
					}
					trigger_if = {
						limit = {
							exists = scope:new_ruler.father
							exists = scope:new_ruler.father.house
							scope:new_ruler.house = scope:new_ruler.father.house
							scope:new_ruler.father = { is_alive = yes }
						}
						scope:new_ruler.father = { NOT = { is_playable_character = yes } }
						NOT = { scope:new_ruler.father = scope:new_ruler.dynasty.dynast }
						NOT = { scope:new_ruler.father = scope:new_ruler.house.house_head }
					}
					trigger_if = {
						limit = {
							exists = scope:new_ruler.mother
							exists = scope:new_ruler.mother.house
							scope:new_ruler.house = scope:new_ruler.mother.house
							scope:new_ruler.mother = { is_alive = yes }
						}
						scope:new_ruler.mother = { NOT = { is_playable_character = yes } }
						NOT = { scope:new_ruler.mother = scope:new_ruler.dynasty.dynast }
						NOT = { scope:new_ruler.mother = scope:new_ruler.house.house_head }
					}
					dynasty = {
						any_dynasty_member = {
							is_alive = yes
							is_playable_character = yes
							scope:new_ruler = { NOT = { is_primary_heir_of = prev } }
						}
					}
					scope:new_ruler.capital_county = {
						any_past_holder = {
							NOT = { dynasty = scope:new_ruler.dynasty }
							NOT = { is_great_grandparent_of = scope:new_ruler }
						}
					}
				}
				custom_tooltip = agot_hf_new_house_name_generation_events.0003.a.tt
				agot_hf_cadet_generation = yes
			}
			add_character_flag = {
				flag = agot_hf_new_house_name_generation_events_0003_sent
				years = 10
			}
		}

		ai_chance = {
			base = 100
		}
	}

	#ONLY CADET
	option = {
		name = agot_hf_new_house_name_generation_events.0003.b
		trigger = { has_character_flag = valyrian_cadet }

		scope:new_ruler = {
			if = {
				limit = {
					NOT = { scope:new_ruler = scope:new_ruler.dynasty.dynast }
					NOT = { scope:new_ruler = scope:new_ruler.house.house_head }
					trigger_if = {
						limit = { exists = scope:new_ruler.dynasty.dynast.primary_heir }
						NOT = { scope:new_ruler = scope:new_ruler.dynasty.dynast.primary_heir }
					}
					trigger_if = {
						limit = { exists = scope:new_ruler.house.house_head.primary_heir }
						NOT = { scope:new_ruler = scope:new_ruler.house.house_head.primary_heir }
					}
					trigger_if = {
						limit = {
							exists = scope:new_ruler.father
							exists = scope:new_ruler.father.house
							scope:new_ruler.house = scope:new_ruler.father.house
							scope:new_ruler.father = { is_alive = yes }
						}
						scope:new_ruler.father = { NOT = { is_playable_character = yes } }
						NOT = { scope:new_ruler.father = scope:new_ruler.dynasty.dynast }
						NOT = { scope:new_ruler.father = scope:new_ruler.house.house_head }
					}
					trigger_if = {
						limit = {
							exists = scope:new_ruler.mother
							exists = scope:new_ruler.mother.house
							scope:new_ruler.house = scope:new_ruler.mother.house
							scope:new_ruler.mother = { is_alive = yes }
						}
						scope:new_ruler.mother = { NOT = { is_playable_character = yes } }
						NOT = { scope:new_ruler.mother = scope:new_ruler.dynasty.dynast }
						NOT = { scope:new_ruler.mother = scope:new_ruler.house.house_head }
					}
					dynasty = {
						any_dynasty_member = {
							is_alive = yes
							is_playable_character = yes
							scope:new_ruler = { NOT = { is_primary_heir_of = prev } }
						}
					}
					scope:new_ruler.capital_county = {
						any_past_holder = {
							NOT = { dynasty = scope:new_ruler.dynasty }
							NOT = { is_great_grandparent_of = scope:new_ruler }
						}
					}
				}
				custom_tooltip = agot_hf_new_house_name_generation_events.0003.a.tt
				agot_hf_cadet_generation = yes
			}
			add_character_flag = {
				flag = agot_hf_new_house_name_generation_events_0003_sent
				years = 10
			}
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = agot_hf_new_house_name_generation_events.0003.c

		ai_chance = {
			base = 25
		}
	}
}

agot_hf_new_house_name_generation_events.0004 = {
	type = character_event
	title = agot_hf_new_house_name_generation_events.0004.t
	desc = agot_hf_new_house_name_generation_events.0004.desc

	theme = bastardy

	override_background = {
		reference = courtyard
	}

	right_portrait = {
		character = scope:father
		animation = thinking
	}

	left_portrait = {
		character = scope:my_father
		animation = idle
	}

	trigger = { NOT = { has_character_flag = agot_hf_new_house_name_generation_events_0004 } }

	immediate = {
		scope:father.father = { save_scope_as = my_father }
		add_character_flag = {
			flag = agot_hf_new_house_name_generation_events_0004
			days = 90
		}
	}

	#Bastard Cadet
	option = {
		name = agot_hf_new_house_name_generation_events.0004.a
		custom_tooltip = agot_hf_new_house_name_generation_events.0004.a.tt

		hidden_effect = {
			scope:father = {
				every_child = {
					limit = { house = scope:father.house }
					set_house = scope:father.father.house
				}
				set_house = scope:father.father.house
			}
			scope:father = { add_character_flag = agot_hf_last_bastrad_activated }
			scope:father = { agot_create_bastard_cadet_effect = yes }
		}

		ai_chance = {
			base = 50

			ai_value_modifier = {
				ai_rationality = 0.5
				ai_honor = 0.5
			}
		}
	}

	#New House
	option = {
		name = agot_hf_new_house_name_generation_events.0004.b

		ai_chance = {
			base = 1

			ai_value_modifier = {
				ai_rationality = -0.5
				ai_greed = -0.5
			}
		}
	}
}

agot_hf_new_house_name_generation_events.0010 = {
	type = character_event
	title = agot_hf_new_house_name_generation_events.0010.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:agot_hf_parent = { has_trait = legitimized_bastard }
					NOT = { exists = scope:house_head_cadet }
				}
				desc = agot_hf_new_house_name_generation_events.0010.desc.legit_bastard
			}
			triggered_desc = {
				trigger = {
					scope:agot_hf_parent = { has_trait = disinherited }
					NOT = { exists = scope:house_head_cadet }
				}
				desc = agot_hf_new_house_name_generation_events.0010.desc.disinherited
			}
			triggered_desc = {
				trigger = {
					scope:agot_hf_parent = { has_trait = legitimized_bastard }
					exists = scope:house_head_cadet
				}
				desc = agot_hf_new_house_name_generation_events.0010.desc.legit_bastard.head
			}
			triggered_desc = {
				trigger = {
					scope:agot_hf_parent = { has_trait = disinherited }
					exists = scope:house_head_cadet
				}
				desc = agot_hf_new_house_name_generation_events.0010.desc.disinherited.head
			}
		}
	}
	theme = bastardy

	right_portrait = {
		character = scope:child
	}

	lower_right_portrait = {
		character = scope:agot_hf_parent
	}

	immediate = {
		if = {
			limit = { exists = scope:cadet_from_father }
			scope:father = { save_scope_as = agot_hf_parent }
		}
		else_if = {
			limit = { exists = scope:cadet_from_mother }
			scope:mother = { save_scope_as = agot_hf_parent }
		}

		scope:agot_hf_parent.house = { save_scope_as = agot_hf_prev_house }
	}

	option = {
		name = {
			trigger = { NOT = { exists = scope:house_head_cadet } }
			text = agot_hf_new_house_name_generation_events.0010.a
		}
		name = {
			trigger = { exists = scope:house_head_cadet }
			text = agot_hf_new_house_name_generation_events.0010.a.head
		}
		if = {
			limit = { NOT = { exists = scope:house_head_cadet } }
			custom_tooltip = agot_hf_new_house_name_generation_events.0010.a.tt
		}
		else = { custom_tooltip = agot_hf_new_house_name_generation_events.0010.a.tt.head }

		if = {
			limit = { scope:agot_hf_parent = { is_alive = yes } }
			scope:agot_hf_parent = { agot_hf_cadet_generation = yes }
		}
		else = {
			scope:child = { agot_hf_cadet_generation = yes }
			hidden_effect = { scope:agot_hf_parent = { set_house = scope:child.house } }
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = agot_hf_new_house_name_generation_events.0010.b

		ai_chance = {
			base = 0

			ai_value_modifier = {
				ai_boldness = -0.5
				ai_honor = 0.5
			}
		}
	}
}

# agot_hf_new_house_name_generation_events.9999 = {
# 	type = character_event
# 	title = agot_hf_new_house_name_generation_events.9999.t
# 	desc = agot_hf_new_house_name_generation_events.9999.desc
# 	theme = bastardy
# 	orphan = yes

# 	right_portrait = {
# 		character = root
# 	}

# 	immediate = {
# 		agot_hf_random_house_sigil_effect = yes
# 	}

# 	option = {
# 		name = agot_hf_new_house_name_generation_events.9999.a

# 		if = {
# 			limit = { agot_hf_test_shit = yes }
# 			show_as_tooltip = { add_gold = 1 }
# 		}
# 	}
# }