﻿create_noble_family_effect = {
	debug_log = create_noble_family_title
	save_scope_as = new_noble_family_holder
	$GOVERNMENT_GIVER$ = { save_scope_as = government_giver }
	hidden_effect = { # NF can only be held by house heads
		if = { # Start by checking that we are not the house head already
			limit = {
				house ?= { house_head != scope:new_noble_family_holder }
			}
			if = { # If not, let's see if it would be reasonable to become house head...
				limit = {
					house.house_head ?= {
						top_liege = scope:new_noble_family_holder.top_liege
						OR = {
							is_ruler = no
							primary_title.tier <= tier_county
						}
						is_ai = yes #  Don't steal house head from a player
					}
				}
				house = { set_house_head = scope:new_noble_family_holder }
			}
			else = { # ... Otherwise, we create a cadet branch
				create_cadet_branch = {}
			}
		}
	}
	scope:government_giver = {
		if = {
			limit = {
				scope:new_noble_family_holder = {
					NOT = {
						any_held_title = { is_noble_family_title = yes }
					}
				}
			}
			switch = {
				trigger = has_government
				japan_administrative_government = { # Japan has count level NFs
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = japan_administrative_government
							save_scope_as = new_title
						}
					}
				}
				japan_feudal_government = { # Japan has count level NFs
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = japan_feudal_government
							save_scope_as = new_title
						}
					}
				}
				feudal_government = { # Japan has count level NFs
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = feudal_government
							save_scope_as = new_title
						}
					}
				}
				celestial_government = { # China has different government
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = celestial_government
							save_scope_as = new_title
						}
						trigger_event = {
							id = tgp_china_career.0001
							days = { 2 5 }
						}
					}
				}
				meritocratic_government = { # Meritocratic has different government
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = meritocratic_government
							save_scope_as = new_title
						}
					}
				}
				steppe_admin_government = { # Steppe Admin has different government
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = steppe_admin_government
							save_scope_as = new_title
						}
					}
				}
				fallback = {
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = duchy
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = administrative_government
							save_scope_as = new_title
						}
					}
				}
			}
		}
		scope:new_title.holder ?= {
			save_scope_as = noble_family_head
			scope:new_title = { set_coa = scope:noble_family_head.house }
		}
		custom_description = { text = create_noble_family_tt }

		#Notifications
		every_player = {
			limit = {
				top_liege = scope:new_noble_family_holder.top_liege
				this != scope:new_noble_family_holder
				# China has hundreds of Noble Families, it gets spammy
				NOT = { government_allows = merit }
			}
			trigger_event = {
				id = ep3_emperor_yearly.2410
				days = 1
			}
		}
	}

	debug_log_scopes = yes
}

create_initial_noble_family_effect = {
    debug_log = "Creating initial noble family title for [This.GetName]"

    # Only continue if valid target
    if = {
        limit = {
            has_government = feudal_government
            is_house_head = yes
            NOT = { any_held_title = { is_noble_family_title = yes } }
        }

        give_noble_family_title = {
            name = noble_family_name
            tier = county
            article = DEFAULT_TITLE_NAME_ARTICLE
            government = feudal_government
            save_scope_as = new_title
        }

        # Give it proper coat of arms
        scope:new_title = {
            set_coa = root.house
        }
	}
    custom_description = { text = create_noble_family_tt }
	if = {
		limit = { has_government = feudal_government }
		switch = {
			trigger = primary_title.tier
			tier_county = { add_legitimacy = 100 }
			tier_duchy  = { add_legitimacy = 150 }
			tier_kingdom = { add_legitimacy = 250 }
			tier_empire  = { add_legitimacy = 300 }
		}
	}
}

convert_whole_realm_to_feudalism_effect = {
	#TOOLTIP INFO
	#PLAYER EFFECTS
	
	hidden_effect = {
		## Spawn holdings in tribal counties
		
		every_sub_realm_county = { # Spawn temples in your holy sites
			limit = {
				any_county_province = {
					has_holding_type = tribal_holding
				}
				NOT = {
					any_county_province = {
						has_holding_type = church_holding
					}
				}
				any_county_province = {
					has_holding = no
					barony = {
						is_holy_site_of = root.faith
					}
				}
			}
			random_county_province = {
				limit = {
					has_holding = no
					barony_cannot_construct_holding = no
					barony = {
						is_holy_site_of = root.faith
					}
				}
				set_holding_type = church_holding
			}
		}
		
		every_sub_realm_county = { # Spawn a City
			limit = {
				any_county_province = {
					has_holding_type = tribal_holding
				}
				any_county_province = {
					has_holding = no
				}
				NOT = {
					any_county_province = {
						has_holding_type = city_holding
					}
				}
			}
			random_county_province = {
				limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
						terrain = terraced_hills
					}
					is_coastal = yes
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
						terrain = terraced_hills
					}
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
						terrain = terraced_hills
					}
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					barony_cannot_construct_holding = no
				}
				random = {
					chance = 60
					modifier = {
						add = county.development_level
					}
					modifier = {
						add = 50
						this.county = root.capital_county
					}
					modifier = {
						add = 20
						OR = {
							terrain = floodplains
							terrain = oasis
							terrain = farmlands
							terrain = terraced_hills
						}
					}
					set_holding_type = city_holding
				}
			}
		}
		
		every_sub_realm_county = { # Spawn a Temple
			limit = {
				any_county_province = {
					has_holding_type = tribal_holding
				}
				any_county_province = {
					has_holding = no
				}
				NOT = {
					any_county_province = {
						has_holding_type = church_holding
					}
				}
			}
			random_county_province = {
				limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
						terrain = terraced_hills
					}
					is_coastal = yes
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
						terrain = terraced_hills
					}
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
						terrain = terraced_hills
					}
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					barony_cannot_construct_holding = no
				}
				random = {
					chance = 50
					modifier = {
						add = county.development_level
					}
					modifier = {
						add = 50
						this.county = root.capital_county
					}
					modifier = {
						add = 20
						OR = {
							terrain = floodplains	
							terrain = oasis
							terrain = farmlands
							terrain = terraced_hills
						}
					}
					set_holding_type = church_holding
				}
			}
		}

		##Sort out new buildings for liege and all vassals
		every_realm_province = {
			limit = {
				has_holding_type = tribal_holding
				NOT = { is_in_list = feudal_conversion }
			}
			add_to_list = feudal_conversion
		}
		every_vassal_or_below = {
			every_realm_province = {
				limit = {
					has_holding_type = tribal_holding
					NOT = { is_in_list = feudal_conversion }
				}
				add_to_list = feudal_conversion

			}
		}
		every_in_list = {
			list = feudal_conversion
			set_variable = {
				name = tribal_to_feudal_building_level
				value = combined_building_level
				days = 1
			}
			set_holding_type = castle_holding
			while = {
				limit = {
					var:tribal_to_feudal_building_level > 1
				}
				generate_building = yes
				change_variable = {
					name = tribal_to_feudal_building_level
					subtract = 1
				}
			}
			remove_variable = tribal_to_feudal_building_level
			remove_from_list = feudal_conversion
		}
	}
	
	##Liege has Soryo? Convert to Soryo!
	if = {
		limit = {
			has_tgp_dlc_trigger = yes
			top_liege != this
			liege = { government_has_flag = government_is_japan_feudal }
		}
		change_government = japan_feudal_government
		if = {
			limit = {
				is_house_head = yes
			}
			save_scope_as = new_head
			house = {
				set_house_name = japanese_dynasty_name
				save_scope_as = new_house
			}
			create_noble_family_effect = { GOVERNMENT_GIVER = this }
			scope:new_title = {
				#set_title_name_dynamic = japanese_noble_family_name
				set_coa = scope:new_house
			}
		}
	}
	##Liege has clan? Convert to clan!
	else_if = {
		limit = {
			top_liege != this
			OR = {
				liege = { government_has_flag = government_is_clan }
				AND = {
					liege = { government_has_flag = government_is_tribal }
					religion = religion:islam_religion
				}
			}
		}
		change_government = clan_government
	}
	##Liege has feudal? Convert to feudal!
	else_if = {
		limit = {
			top_liege != this
			OR = {
				liege = { government_has_flag = government_is_feudal }
				AND = {
					liege = { government_has_flag = government_is_tribal }
					NOT = { religion = religion:islam_religion }
				}
			}
		}
		change_government = feudal_government
		if = {
			limit = {
				is_house_head = yes
			}
			create_noble_family_effect = { GOVERNMENT_GIVER = this }
		}
	}
	##You're an independent Japanese Heritage Character? Convert to Soryo!
	else_if = {
		limit = {
			has_tgp_dlc_trigger = yes
			top_liege = this
			culture = { has_cultural_pillar = heritage_japonic }
		}
		change_government = japan_feudal_government
		if = {
			limit = {
				is_house_head = yes
			}
			save_scope_as = new_head
			house = {
				set_house_name = japanese_dynasty_name
				save_scope_as = new_house
			}
			create_noble_family_effect = { GOVERNMENT_GIVER = this }
			scope:new_title = {
				#set_title_name_dynamic = japanese_noble_family_name
				set_coa = scope:new_house
			}
		}
	}
	##You're an independent Muslim? Convert to clan!
	else_if = {
		limit = {
			top_liege = this
			faith = { religion_tag = islam_religion }
		}
		change_government = clan_government
	}
	##You're an independent anything else? Convert to feudal!
	else = { 
		change_government = feudal_government
		if = {
			limit = {
				is_house_head = yes
			}
			create_noble_family_effect = { GOVERNMENT_GIVER = this }
		}
	}
	if = {
		limit = {
			government_has_flag = government_is_tribal
		}
		##General
		custom_tooltip = all_holding_will_change
		##Liege has clan? Convert to clan!
		if = {
			limit = {
				top_liege != this
				liege = { government_has_flag = government_is_clan }
			}
			custom_tooltip = clan_liege_vassals_will_convert
		}
		#Liege has feudal? Convert to feudal!
		if = {
			limit = {
				top_liege != this
				liege = { government_has_flag = government_is_feudal }
			}
			custom_tooltip = feudal_liege_vassals_will_convert
		}
		##If I am Islamic, we will become clan.
		if = {
			limit = {
				top_liege = this
				faith = { religion_tag = islam_religion }
			}
			custom_tooltip = islamic_vassals_will_convert
		}
		##If I am not Islamic, we will become feudal.
		else_if = {
			limit = { top_liege = this }
			custom_tooltip = other_vassals_will_convert
		}
	}

	hidden_effect = {
		#Nuke tribal authorities.
		if = {
			limit = { has_realm_law = tribal_authority_0 }
			remove_realm_law = tribal_authority_0
		}
		if = {
			limit = { has_realm_law = tribal_authority_1 }
			remove_realm_law = tribal_authority_1
		}
		if = {
			limit = { has_realm_law = tribal_authority_2 }
			remove_realm_law = tribal_authority_2
		}
		if = {
			limit = { has_realm_law = tribal_authority_3 }
			remove_realm_law = tribal_authority_3
		}
		#Start off crown authorities.
		if = {
			limit = {
				NOT = { has_realm_law = crown_authority_0 }
			}
			add_realm_law_skip_effects = crown_authority_0
		}

		#Also convert every vassal.
		#Liege has taken soryo, so become soryo.
		if = {
			limit = {
				government_has_flag = government_is_japan_feudal
			}
			every_vassal_or_below = {
				limit = { government_has_flag = government_is_tribal }
				change_government = japan_feudal_government
				if = {
					limit = {
						is_house_head = yes
					}
					save_scope_as = new_head
					house = {
						set_house_name = japanese_dynasty_name
						save_scope_as = new_house
					}
					create_noble_family_effect = { GOVERNMENT_GIVER = this }
					scope:new_title = {
						#set_title_name_dynamic = japanese_noble_family_name
						set_coa = scope:new_house
					}
				}
				#Nuke tribal authorities.
				if = {
					limit = { has_realm_law = tribal_authority_0 }
					remove_realm_law = tribal_authority_0
				}
				if = {
					limit = { has_realm_law = tribal_authority_1 }
					remove_realm_law = tribal_authority_1
				}
				if = {
					limit = { has_realm_law = tribal_authority_2 }
					remove_realm_law = tribal_authority_2
				}
				if = {
					limit = { has_realm_law = tribal_authority_3 }
					remove_realm_law = tribal_authority_3
				}
				if = {
					limit = {
						NOT = { has_realm_law = japanese_bureaucracy_0 }
					}
					add_realm_law_skip_effects = japanese_bureaucracy_0
				}
			}
		}
		#Liege has taken clan, so become clan.
		else_if = {
			limit = {
				government_has_flag = government_is_clan
			}
			every_vassal_or_below = {
				limit = { government_has_flag = government_is_tribal }
				change_government = clan_government
				#Nuke tribal authorities.
				if = {
					limit = { has_realm_law = tribal_authority_0 }
					remove_realm_law = tribal_authority_0
				}
				if = {
					limit = { has_realm_law = tribal_authority_1 }
					remove_realm_law = tribal_authority_1
				}
				if = {
					limit = { has_realm_law = tribal_authority_2 }
					remove_realm_law = tribal_authority_2
				}
				if = {
					limit = { has_realm_law = tribal_authority_3 }
					remove_realm_law = tribal_authority_3
				}
				if = {
					limit = {
						NOT = { has_realm_law = crown_authority_0 }
					}
					add_realm_law_skip_effects = crown_authority_0
				}
			}
		}
		#Liege is not clan, become feudal.
		else = {
			every_vassal_or_below = {
				limit = { government_has_flag = government_is_tribal }
				change_government = feudal_government
				#Nuke tribal authorities.
				if = {
					limit = { has_realm_law = tribal_authority_0 }
					remove_realm_law = tribal_authority_0
				}
				if = {
					limit = { has_realm_law = tribal_authority_1 }
					remove_realm_law = tribal_authority_1
				}
				if = {
					limit = { has_realm_law = tribal_authority_2 }
					remove_realm_law = tribal_authority_2
				}
				if = {
					limit = { has_realm_law = tribal_authority_3 }
					remove_realm_law = tribal_authority_3
				}
				if = {
					limit = {
						NOT = { has_realm_law = crown_authority_0 }
					}
					add_realm_law_skip_effects = crown_authority_0
				}
				if = {
					limit = {
						is_house_head = yes
					}
					create_noble_family_effect = { GOVERNMENT_GIVER = this }
				}
			}
		}
	}
}