﻿create_cadet_branch_decision = {
	picture = {
		trigger = {
			government_is_japanese_trigger = yes
		}
		reference = "gfx/interface/illustrations/decisions/tgp_poet.dds"
	}
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_dynasty_house.dds"
	}

	decision_group_type = major
	ai_check_interval_by_tier = {
		barony = 0
		county = 6
		duchy = 6
		kingdom = 6
		empire = 6
		hegemony = 6
	}

	is_shown = {
		is_playable_character = yes
#		OR = {
#			is_landed_or_landless_administrative = yes
#			# any_held_title = { is_noble_family_title = yes }		# Seems to be redundant.
#			# government_has_flag = government_is_administrative	# Seems to be redundant.
#			AND = {
#				is_playable_character = yes # To include landless, but for House Tradition purposes its only Roman Aristocracy & Legitimist camps.
#				has_realm_law = camp_purpose_legitimists
#			}
#		}
		exists = house

#		exists = house.house_head
# This requirement makes no sense. Sometimes house head succession dies out and other branches cannot inherit it. No foreseable issues by not requiring house head to exist.
#  Commenting out to make possible for other branches of the dynasty to re-establish their status as a new house.
#
		is_house_head = no
		highest_held_title_tier > tier_barony
		NOR = {
			government_has_flag = government_is_holy_order
			government_has_flag = government_is_theocracy
			AND = {
				faith = { has_doctrine = doctrine_theocracy_temporal }
				has_council_position = councillor_court_chaplain
			}
			AND = {
				has_tgp_dlc_trigger = yes
				tgp_is_in_ceremonial_house_trigger = yes
			}
		}
	}

	is_valid = {
		is_adult = yes
		# government_allows = create_cadet_branches
		# Why should it matter if the house head somehow ended up unlanded, theocratic, or holy order? Currently blocks creation if house head is unlanded.
		# This appears intended to block tribal, but no good reason for tribals to be forced to identify as the same house.
		#
#		# Must have living children of same house
#		custom_tooltip = {
#			text = create_cadet_branch_decision_living_children
#			any_child = {
#				count >= 1
#				is_alive = yes
#				house ?= root.house
#			}
#		}
#		# No direct descendant is liege or above
#		custom_tooltip = {
#			text = create_cadet_branch_decision_liege_is_descendant
#			any_liege_or_above_is_descendant = no
#		}
		# If you are Clan, you cannot be in the same realm as your House Head. For simplicity, we'll just force you to be independent.
		trigger_if = {
			limit = {
				government_has_flag = government_is_clan
			}
			top_liege = this
		}
		# If you are Administrative, we let the player create a new House much easier, as to have easy access to their own Family Title and Estate.
		trigger_else_if = {
			limit = {
				government_allows = administrative
			}
			trigger_if = {
				limit = {
					is_ai = yes # We don't want the AI to create cadet branches willy nilly, so they have additional restrictions.
					top_liege = house.house_head.top_liege # But we do want the AI to create a cadet branch ASAP if the house head is in a different realm
				}
				NOT = { is_close_or_extended_family_of = house.house_head }
				custom_description = {
					text = create_cadet_branch_decision_succession_line
					house.house_head = {
						NOT = {
							any_held_title = {
								place_in_line_of_succession = {
									target = root
									value <=3
								}
							}
						}
					}
				}
			}
			# No triggers for players.
		}
		trigger_else_if = {
			limit = { is_landless_ruler = yes }
			has_realm_law = camp_purpose_legitimists
			trigger_if = {
				limit = {
					is_ai = yes # We don't want the AI to create cadet branches willy nilly, so they have additional restrictions.
					top_liege = house.house_head.top_liege # But we do want the AI to create a cadet branch ASAP if the house head is in a different realm
				}
				NOT = { is_close_or_extended_family_of = house.house_head }
				custom_description = {
					text = create_cadet_branch_decision_succession_line
					house.house_head = {
						NOT = {
							any_held_title = {
								place_in_line_of_succession = {
									target = root
									value <=3
								}
							}
						}
					}
				}
			}
			# No triggers for players.
		}
		# For everyone else, you cannot be too close in the line of succession.
		trigger_else = {
#			limit = { exists = house.house_head }
			custom_description = {
				text = create_cadet_branch_decision_succession_line
				house.house_head = {
					NOT = {
						any_held_title = {
							place_in_line_of_succession = {
								target = root
								value <= 3
							}
						}
					}
				}
			} # House Head succession check bloc
		}
		# Marriage restrictions
		trigger_if = {
			limit = { is_male = yes }
			trigger_if = {
				limit = {
					AND = { # Vanilla triggers on OR. Why? Non-dominant gender should only create cadets if they assert a dominant role.
						is_married = yes
						faith = { has_doctrine = doctrine_gender_female_dominated }
					}
				}
				patrilinear_marriage = yes
			}
		}
		trigger_else = {
			# Implied is_female = yes
			trigger_if = {
				limit = {
					AND = { # Mirroring Above
						is_married = yes
						faith = { has_doctrine = doctrine_gender_male_dominated }
					}
				}
				matrilinear_marriage = yes
			}
		}
#		trigger_if = { #Males of female-dominated faith must already be patrilineally married before taking this decision.
#			limit = {
#				is_married = no
#				is_female = no
#				faith = {
#					has_doctrine = doctrine_gender_female_dominated
#				}
#			}
#			patrilinear_marriage = yes
#		}
#		trigger_if = { #Females of male-dominated faith must already be matrilineally married before taking this decision.
#			limit = {
#				is_married = no
#				is_female = yes
#				faith = {
#					has_doctrine = doctrine_gender_male_dominated
#				}
#			}
#			matrilinear_marriage = yes
#		}
		# Monks need not apply
		trigger_if = {
			limit = { has_trait = devoted }
			NOT = { has_trait = devoted }
		}
		trigger_if = {
			limit = { has_trait = bastard }
			NOT = { has_trait = bastard }
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		is_playable_character = yes
	}

	effect = {
		house.house_head ?= { save_scope_as = old_head }
		if = {
			limit = {
				has_tgp_dlc_trigger = yes
				culture = {
					OR = {
						has_cultural_pillar = language_japonic
						has_name_list = name_list_yamato
					}
				}
			}
			add_character_flag = found_cadet_branch
			trigger_event = tgp_japan_decision.9001
			show_as_tooltip = {
				create_cadet_branch = { save_scope_as = new_house }
				if = {
					limit = { government_allows = create_cadet_branches }
					create_noble_family_effect = { GOVERNMENT_GIVER = scope:old_head }
				}
			}
		}
		else = {
			found_cadet_house_decision_effect = {
				CHARACTER = root
				PRESTIGE = major_prestige_gain
			}
			if = {
				limit = {
					government_allows = administrative
					highest_held_title_tier >= tier_duchy
				}
				create_noble_family_effect = { GOVERNMENT_GIVER = scope:old_head }
				# Changed/fixed from "create_noble_family_effect = yes" per Nomad's suggestion. Unsure the root cause; I believe I borrowed this code from admin noble family creation when RoP first came out.
				change_influence = major_influence_gain
			}
			if = {
				limit = { is_ai = yes }
				house = {
					add_house_modifier = {
						modifier = new_cadet_house_modifier
						years = 25
					}
				}
			}
			remove_character_modifier = blacksheep
		}
	}
	
	ai_potential = {
#		exists = house.house_head
		is_adult = yes
		highest_held_title_tier > tier_barony
		any_child = { is_alive = yes }
		#always = yes
		trigger_if = {
			# Revive the dynasty if somehow game glitches with no successors to the Dyansty Head but living members.
			limit = {
				NOT = {
					exists = dynasty.dynast
					# dynasty.dynast = {
					# 	is_alive = yes	# May create issues on succession
					# }
				}
			}
			always = yes
		}
		# Don't allow if too closely descended from a recent founder.
		trigger_else = {
			#Parents
			AND = {
				trigger_if = {
					limit = { exists = father }
					father = { is_house_founder = no }
				}
				trigger_if = {
					limit = { exists = mother }
					mother = { is_house_founder = no }
				}
				#Grandparents
				trigger_if = {
					limit = { exists = father.father }
					father.father = { is_house_founder = no }
				}
				trigger_if = {
					limit = { exists = father.mother }
					father.mother = { is_house_founder = no }
				} # Dad's parents
				trigger_if = {
					limit = { exists = mother.father }
					mother.father = { is_house_founder = no }
				}
				trigger_if = {
					limit = { exists = mother.mother }
					mother.mother = { is_house_founder = no }
				} # Mom's parents
			} # Parent/Grandparent House Founder Check

			# Don't allow creation of cadet branch if it threatens the survival of the parent house
			house = {
				any_house_member = { 		
					is_alive = yes 			
					count >= 25
				}
				any_house_member = { 		
					is_alive = yes
					is_male = yes
					NOT = { has_trait = bastard }
					age < 45
					count >= 8
				}
				any_house_member = { 		
					is_alive = yes
					is_male = yes
					NOT = { has_trait = bastard }
					age < 16
					count >= 4
				}
			}
		}

		trigger_if = {
			limit = {
				OR = {
					has_trait = crusader_king
					has_trait = augustus
					has_trait = saoshyant
					has_trait = chakravarti
					has_trait = greatest_of_khans
				}
			}
			always = yes
		}
		trigger_if = {
			limit = {
				primary_title.tier = tier_county
				is_independent_ruler = no
			}
			always = no
		}

		trigger_if = {
			limit = {
				has_government = tribal_government
			}
			is_independent_ruler = yes
			primary_title.tier >= tier_duchy
			opinion = {
				target = dynasty.dynast
				value <= 25
			}
			trait_compatibility = {
				target = dynasty.dynast
				value < neutral_trait_compatibility
			}
			# Tribes are more well, "tribal" and cohesive to the chief / top liege
			max_military_strength > 4000
			OR = {
				prestige_level >= 4
				piety_level >= 4
				AND = {
					prestige_level >= 3
					NOT = { # Too easy for them to satisfy, and Dynasty Head likely would have a firmer grasp over them.
						house = { has_house_modifier = trad_pride   }
						house = { has_house_modifier = trad_respect }
						house = { has_house_modifier = trad_power   }
					}
					OR = {
						has_character_modifier = blacksheep
						has_trait = ambitious
						has_trait = arrogant
						has_trait = pure_blooded
						has_any_nickname = yes
						NOT = {
							religion = dynasty.dynast.religion
							religion = house.house_head.religion
							culture = dynasty.dynast.culture
							culture = house.house_head.culture
							#Too easy for just one to be different.
						}
					}
				}
			}
		}

		trigger_if = {
			limit = {
				primary_title.tier = tier_county
				is_independent_ruler = yes
			}
			max_military_strength > 4000
			OR = {
				prestige_level >= 4
				piety_level >= 4
				AND = {
					prestige_level >= 3
					NOT = { # Too easy for them to satisfy, and Dynasty Head likely would have a firmer grasp over them.
						house = { has_house_modifier = trad_pride   }
						house = { has_house_modifier = trad_respect }
						house = { has_house_modifier = trad_power   }
					}
					OR = {
						has_character_modifier = blacksheep
						has_trait = ambitious
						has_trait = arrogant
						has_trait = pure_blooded
						has_any_nickname = yes
						NOT = {
							religion = dynasty.dynast.religion
							religion = house.house_head.religion
							culture = dynasty.dynast.culture
							culture = house.house_head.culture
							#Too easy for just one to be different.
						}
						# diplomacy >= 20
						# martial >= 20
						# stewardship >= 20
						# intrigue >= 20
						#learning >= 30	#Scholar focus characters tend to stack learning too easily
					}
				}
			}
		} # Count Trigger

		trigger_if = {
			limit = {
				primary_title.tier = tier_duchy
			}
			OR = {
				is_independent_ruler = yes
				NOT = {
					dynasty = liege.dynasty
					dynasty = top_liege.dynasty
				}
			}
			max_military_strength > 4000
			OR = {
				prestige_level >= 4
				piety_level >= 4
				AND = {
					prestige_level >= 3
					NOT = { # Too easy for them to satisfy, and Dynasty Head likely would have a firmer grasp over them.
						house = { has_house_modifier = trad_pride   }
						house = { has_house_modifier = trad_respect }
						house = { has_house_modifier = trad_power   }
					}
					OR = {
						has_character_modifier = blacksheep
						has_trait = ambitious
						has_trait = arrogant
						has_trait = pure_blooded
						has_any_nickname = yes
						NOT = {
							religion = dynasty.dynast.religion
							religion = house.house_head.religion
							culture = dynasty.dynast.culture
							culture = house.house_head.culture
							#Too easy for just one to be different.
						}
						# diplomacy >= 20
						# martial >= 20
						# stewardship >= 20
						# intrigue >= 20
						#learning > 30	#Scholar focus characters tend to stack learning too easily
					}
				}
			}
		} # Duke Trigger

		trigger_if = {
			limit = {
				primary_title.tier = tier_kingdom
				is_independent_ruler = no
			}
			NOT = {
				dynasty = liege.dynasty
				dynasty = top_liege.dynasty
			}
			max_military_strength > 4000
			OR = {
				prestige_level >= 4
				piety_level >= 4
				AND = {
					prestige_level >= 3
					OR = {
						has_character_modifier = blacksheep
						has_trait = ambitious
						has_trait = arrogant
						has_trait = pure_blooded
						has_any_nickname = yes
						NOT = {
							religion = dynasty.dynast.religion
							religion = house.house_head.religion
							culture = dynasty.dynast.culture
							culture = house.house_head.culture
							#Too easy for just one to be different.
						}
						# diplomacy >= 20
						# martial >= 20
						# stewardship >= 20
						# intrigue >= 20
						#learning > 30	#Scholar focus characters tend to stack learning too easily
					}
				}
			}
		} # Vassal King Trigger

		trigger_if = {
			limit = {
				primary_title.tier >= tier_kingdom
				is_independent_ruler = yes
			}
			always = yes
		} # Indie King/Emperor Trigger
	}

	ai_will_do = {
		base = 30
		
		modifier = {
			add = 30
			NOT = { exists = dynasty.dynast }
		}

		modifier = { # Dukes and above are more keen
			add = 70
			exists = primary_title
			primary_title.tier > tier_county
		}

		modifier = { # Prefer to stay in liege's dynasty
			add = -50
			government_is_japanese_trigger = no
			top_liege != this
			any_liege_or_above = {
				dynasty = root.dynasty
			}
		}
		modifier = {
			add = -50
			is_independent_ruler = no
			any_liege_or_above = {
				dynasty = root.dynasty
			}
		}
		
		modifier = { # Childless rulers should not found houses
			factor = 0
			any_child = {
				is_alive = yes
				NOT = { has_trait = bastard }
				count < 3
				OR = {					
					AND = {
						OR = {
							has_realm_law = male_only_law
							has_realm_law = male_preference_law
							liege_or_court_owner = { has_realm_law = male_only_law }
							liege_or_court_owner = { has_realm_law = male_preference_law }
						}
						is_female = no
					}
					AND = {
						OR = {
							has_realm_law = female_only_law
							has_realm_law = female_preference_law
							liege_or_court_owner = { has_realm_law = female_only_law }
							liege_or_court_owner = { has_realm_law = female_preference_law }
						}
						is_female = yes
					}
				} # Lineality Check
			}
		}
		
		modifier = {
			factor = 0
			OR = {
				has_trait = content
				has_trait = humble
				has_trait = shy
				has_trait = inbred
				has_trait = infertile
				has_trait = celibate
				has_trait = eunuch
			}
		}
		modifier = { # Stop stealing player's house members
			factor = 0
			any_player = {
				OR = {
					any_child = {
						OR = {
							is_child_of = root
							is_grandchild_of = root
							is_great_grandchild_of = root
						}
					}
					any_child = {
						even_if_dead = yes
						any_child = {
							OR = {
								is_child_of = root
								is_grandchild_of = root
								is_great_grandchild_of = root
							}
						}
					}
					any_child = {
						even_if_dead = yes
						any_child = {
							even_if_dead = yes
							any_child = {
								OR = {
									is_child_of = root
									is_grandchild_of = root
									is_great_grandchild_of = root
								}
							}
						}
					}
				}
			}
		}

		# we don't want AI to split from their House unless they dislike their house head
		modifier = {
			factor = 0
			government_has_flag = government_is_clan
			opinion = {
				target = house.house_head
				value >= -50
			}
		}
		
		# If an admin top liege ever ends up having their house head in a different realm, they should create a cadet branch ASAP
		modifier = {
			add = 1000
			government_allows = administrative
			top_liege = this
			this != house.house_head.top_liege
		}

		modifier = {
			add = -250
			years_from_game_start < 5
		}

		### TGP

		modifier = { # Prefer not to branch away from ruling close family
			government_is_japanese_trigger = yes
			top_liege.primary_title = {
				any_past_holder = { is_close_family_of = root }
			}
			add = -25
		}

		modifier = { # Prefer to branch away from leading house if distant to split up blocs
			government_is_japanese_trigger = yes
			is_landed = yes
			top_liege.house = root.house
			NOT = { is_close_or_extended_family_of = top_liege }
			add = 50
		}

		modifier = { # Prefer to branch away from house head
			government_is_japanese_trigger = yes
			NOT = { is_close_or_extended_family_of = house.house_head }
			add = 50
		}

		modifier = { # Ruler should not break family apart
			government_is_japanese_trigger = yes
			is_independent_ruler = yes
			add = -1000
		}

		modifier = { # Close and extended family prefer to stay with Ritsuryo house heads
			government_is_japanese_trigger = yes
			government_allows = administrative
			OR = {
				is_close_or_extended_family_of = house.house_head
				AND = {
					top_liege.house ?= house
					is_close_or_extended_family_of = top_liege
				}
				house.house_head = {
					any_held_title = {
						is_noble_family_title = yes
						any_past_holder = { is_close_or_extended_family_of = root }
					}
				}
			}
			add = -1000
		}
	}
}
