﻿zz_adopt_to_house_effect = {
	$ADOPTER$ = {
		$CHILD$ = {
			hidden_effect = {
				set_house = $ADOPTER$.house
				found_cadet_house_decision_effect = {
					CHARACTER = scope:recipient
					PRESTIGE = 0
				}
			}
			custom_tooltip = adventurer_adoption_child_tt
			# Remove bastard traits
			if = {
				limit = {
					has_any_bastard_trait_trigger = yes
				}
				remove_all_bastard_traits = yes
			}
			# Remove disinherited if you're the house head.
			if = {
				limit = {
					$ADOPTER$.house.house_head = $ADOPTER$
					has_trait = disinherited
				}
				remove_trait = disinherited
			}
		}
	}
	#adopted child should travel to new parent
	$CHILD$ = {
		if = {
			limit = { exists = $ADOPTER$.capital_province } # Ward heads to landed Guardian
			$ADOPTER$.capital_province = { save_scope_as = destination_province }
		}
		else_if = { # Ward heads to Guardian's host
			limit = { exists = $ADOPTER$.host.capital_province }
			$ADOPTER$.host.capital_province = { save_scope_as = destination_province }
		}
		else = {
			$ADOPTER$.location = { save_scope_as = destination_province }
		}
		save_scope_as = adoptee
		$ADOPTER$ = {
			save_scope_as = adopter
		}
		if = {
			limit = {
				any_relation = {
					type = guardian
					NOT = { this = $ADOPTER$ }
				}
			}
			random_relation = {
				type = guardian
				limit = { NOT = { this = $ADOPTER$ } }
				save_scope_as = old_guardian
			}
			remove_guardian_effect = {
				GUARDIAN = scope:old_guardian
				WARD = scope:adoptee
				RETURN_WARD = no
				HIDE_OPINION = no
			}
		}
		if = {
			limit = { NOT = { scope:destination_province = $CHILD$.location } }
			set_variable = {
				name = adoptee_travelling_to_adopter
				years = 2
				value = scope:adopter
			}
			start_travel_plan = {
				destination = scope:destination_province
				on_start_on_action = on_adoptee_depart_for_adopter
				on_travel_planner_cancel_on_action = on_adoptee_depart_travel_planner_exit
				on_arrival_on_action = on_adoptee_arrive_at_adopter
				on_arrival_destinations = last
				return_trip = no # One way
			}
			custom_tooltip = adoptee_leaves_for_adopter_tt
		}
	}
}

create_noble_family_dragonrider_effect = {
	debug_log = create_noble_family_title

	save_scope_as = new_noble_family_holder
	hidden_effect = { # NF can only be held by house heads
		house ?= {
			if = {
				limit = { house_head != scope:new_noble_family_holder }
				set_house_head = scope:new_noble_family_holder
			}
		}
	}
	add_character_flag = new_government_is_freehold_admin
	tgp_domicile_conversion_when_changing_government_type = {
			NEW_DOMICILE_TYPE = dragonkeep
			NEW_GOVERNMENT_TYPE = freehold_government
		}
	change_government = freehold_government 
	remove_character_flag = new_government_is_freehold_admin

	custom_description = {
		text = create_noble_family_tt
	}

	#Notifications
	top_liege ?= {
		every_vassal = {
			limit = {
				is_ai = no
				NOT = { this = scope:new_noble_family_holder }
			}
			trigger_event = {
				id = ep3_emperor_yearly.2410
				days = 1
			}
		}
	}

	debug_log_scopes = yes
}

add_random_internal_dragonkeep_building = {
	random_list = {
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = servants_quarters_01 }
			}
			add_domicile_building = servants_quarters_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = servants_quarters_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = library_01 }
			}
			add_domicile_building = library_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = library_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = bath_01 }
			}
			add_domicile_building = bath_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = bath_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = courtyard_01 }
			}
			add_domicile_building = courtyard_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = courtyard_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = prison_01 }
			}
			add_domicile_building = prison_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = prison_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = living_quarters_01 }
			}
			add_domicile_building = living_quarters_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = living_quarters_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = trophy_room_01 }
			}
			add_domicile_building = trophy_room_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = trophy_room_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = office_01 }
			}
			add_domicile_building = office_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = office_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = guest_room_01 }
			}
			add_domicile_building = guest_room_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = guest_room_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = wine_cellar_01 }
			}
			add_domicile_building = wine_cellar_01
			if = {
				limit = { has_domicile_building = dragonkeep_main_03 }
				add_domicile_building = wine_cellar_02
			}
		}
	}
}

fill_external_dragonkeep_building_effect = {
	switch = {
		trigger = has_domicile_building_or_higher
		estate_main_05 = {
			while = {
				limit = { free_external_domicile_building_slots >= 1 }
				add_random_external_dragonkeep_building = yes
			}
		}
		estate_main_04 = {
			while = {
				limit = { free_external_domicile_building_slots >= 2 }
				add_random_external_dragonkeep_building = yes
			}
		}
		estate_main_03 = {
			while = {
				limit = { free_external_domicile_building_slots >= 3 }
				add_random_external_dragonkeep_building = yes
			}
		}
		estate_main_02 = {
			while = {
				limit = { free_external_domicile_building_slots >= 4 }
				add_random_external_dragonkeep_building = yes
			}
		}
		estate_main_01 = {
			while = {
				limit = { free_external_domicile_building_slots >= 5 }
				add_random_external_dragonkeep_building = yes
			}
		}
	}
}

add_random_external_dragonkeep_building = {
	if = {
		limit = {
			free_external_domicile_building_slots >= 1
		}
		random_list = {
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = temple_small_01
						owner = {
							government_has_flag = government_is_celestial 
						}
					}
				}
				add_domicile_building = temple_small_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = temple_small_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = barracks_01 }
				}
				add_domicile_building = barracks_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = barracks_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = watchtower_01 }
				}
				add_domicile_building = watchtower_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = watchtower_02
				}
			}
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = vineyard_01 
						owner.culture ?= {
							has_innovation = innovation_champa_rice
						}
					}
				}
				add_domicile_building = vineyard_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = vineyard_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = guardhouse_01 }
				}
				add_domicile_building = guardhouse_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = guardhouse_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = garden_01 }
				}
				add_domicile_building = garden_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = garden_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = storage_01 }
				}
				add_domicile_building = storage_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = storage_02
				}
			}
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = grain_field_01
						owner.culture ?= {
							has_innovation = innovation_champa_rice
						}
					}
				}
				add_domicile_building = grain_field_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = grain_field_02
				}
			}
			10 = {
				trigger = {
					NOT = { 
						has_domicile_building_or_higher = rice_field_01
					}
					owner.culture ?= {
						has_innovation = innovation_champa_rice
					}
				}
				add_domicile_building = rice_field_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = rice_field_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = stable_01 }
				}
				add_domicile_building = stable_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = stable_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = workshop_01 }
				}
				add_domicile_building = workshop_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = workshop_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = market_01 }
				}
				add_domicile_building = market_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = market_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = grazing_land_01 }
				}
				add_domicile_building = grazing_land_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = grazing_land_02
				}
			}
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = olive_01 
						owner.culture ?= {
							has_innovation = innovation_champa_rice
						}
					}
				}
				add_domicile_building = olive_01
				if = {
					limit = { has_domicile_building = dragonkeep_main_03 }
					add_domicile_building = olive_02
				}
			}
		}
	}
}



spawn_freehold_governance_contract_effect = {
	save_scope_as = governor
	# Select the Contract Tier (somewhat randomized, based on your experience as a governor)
	random_list = {
		30 = {
			save_scope_value_as = {
				name = governance_task_contract_tier
				value = define:NTaskContract|LOW_TASK_CONTRACT_TIER
			}
		}
		20 = {
			save_scope_value_as = {
				name = governance_task_contract_tier
				value = define:NTaskContract|MEDIUM_TASK_CONTRACT_TIER
			}

			modifier = {
				add = 10
				has_trait = governor
			}
			modifier = {
				add = 10
				has_trait = governor
				has_trait_xp = {
					trait = governor
					value >= 40
				}
			}
		}
		10 = {
			save_scope_value_as = {
				name = governance_task_contract_tier
				value = define:NTaskContract|HIGH_TASK_CONTRACT_TIER
			}

			modifier = {
				add = 10
				has_trait = governor
			}
			modifier = {
				add = 10
				has_trait = governor
				has_trait_xp = {
					trait = governor
					value >= 40
				}
			}
			modifier = {
				add = 20
				has_trait = governor
				has_trait_xp = {
					trait = governor
					value >= 80
				}
			}
		}
	}

	# Select which contract to create
	trigger_event = {
		on_action = spawn_freehold_governance_contract_on_action
	}
	
	play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Byzantine_Flavor/ep3_mx_sting_byzantineflavor_become_governor"
}



freehold_governor_contract_travel_or_progress_effect = {
	if = {
		limit = { location != $DESTINATION$ }
		start_travel_plan = {
			destination = $DESTINATION$
			on_arrival_event = $ARRIVAL_EVENT$
			on_travel_planner_cancel_event = freehold_governor_contract_event.1012
			on_arrival_destinations = all_but_last
		}
	}
	else = { trigger_event = $ARRIVAL_EVENT$ }
}
