###########################
# Crown World (ap_crown_world)
###########################
namespace = apb_crown

# An ascension perk was picked: recount a day later, once the new perk is in the total.
# Hook: on_ascension_perk_picked. This = Country.
country_event = {
	id = apb_crown.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_crown_world
	}

	immediate = {
		country_event = {
			id = apb_crown.2
			days = 1
		}
	}
}

# Recount the perks and rebuild the Crown World's modifier.
# An AI empire without a Crown World names its capital here.
# Fired a day after a perk pick (apb_crown.1) and from the perk's on_enabled.
country_event = {
	id = apb_crown.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_crown_world
	}

	immediate = {
		if = {
			limit = {
				is_ai = yes
				NOT = {
					any_owned_planet = { has_carrier_flag = apb_crown_world }
				}
				exists = capital_scope
				capital_scope = { is_colony = yes }
			}
			# apb_crown_name_world ends with a refresh.
			capital_scope = { apb_crown_name_world = yes }
		}
		else = {
			apb_crown_refresh = yes
		}
	}
}

# The Crown World changed hands.
# Hook: on_colony_transfer. This = Colony scope, From = new owner, FromFrom = previous owner (if any).
carrier_event = {
	id = apb_crown.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_carrier_flag = apb_crown_world
	}

	immediate = {
		apb_crown_strip_world = yes
		if = {
			limit = {
				exists = fromfrom
				fromfrom = { has_ascension_perk = ap_crown_world }
			}
			set_carrier_flag = apb_former_crown@fromfrom
			fromfrom = { apb_crown_go_into_exile = yes }
		}
	}
}

# The Crown World's colony was destroyed.
# Hook: on_colony_destroyed. Scope = Carrier, called just before owner and controller are cleared.
carrier_event = {
	id = apb_crown.4
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_carrier_flag = apb_crown_world
	}

	immediate = {
		apb_crown_strip_world = yes
		owner? = {
			if = {
				limit = { has_ascension_perk = ap_crown_world }
				apb_crown_go_into_exile = yes
			}
		}
	}
}

# A lost Crown World came back to its old owner.
# Hook: on_colony_transfer (after apb_crown.3). This = Colony scope, From = new owner.
carrier_event = {
	id = apb_crown.5
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		has_carrier_flag = apb_former_crown@from
	}

	immediate = {
		remove_carrier_flag = apb_former_crown@from
		if = {
			limit = {
				exists = owner
				owner = {
					is_same_value = root.from
					has_ascension_perk = ap_crown_world
					NOT = {
						any_owned_planet = { has_carrier_flag = apb_crown_world }
					}
				}
			}
			apb_crown_name_world = yes
			from = {
				if = {
					limit = { is_ai = no }
					country_event = { id = apb_crown.21 }
				}
			}
		}
	}
}

###########################
# Player notifications
###########################

# 5 perks
country_event = {
	id = apb_crown.10
	title = apb_crown.10.name
	desc = apb_crown.10.desc
	picture = GFX_evt_city_planet
	show_sound = event_default
	is_triggered_only = yes

	option = {
		name = apb_crown.10.a
		custom_tooltip = apb_crown_growth_tt
	}
}

# 10 perks
country_event = {
	id = apb_crown.11
	title = apb_crown.11.name
	desc = apb_crown.11.desc
	picture = GFX_evt_city_planet
	show_sound = event_default
	is_triggered_only = yes

	option = {
		name = apb_crown.11.a
		custom_tooltip = apb_crown_growth_tt
	}
}

# 15 perks
country_event = {
	id = apb_crown.12
	title = apb_crown.12.name
	desc = apb_crown.12.desc
	picture = GFX_evt_throne_room
	show_sound = event_default
	is_triggered_only = yes

	option = {
		name = apb_crown.12.a
		custom_tooltip = apb_crown_growth_tt
		custom_tooltip = apb_crown_seat_tt
	}
}

# Crown World lost
country_event = {
	id = apb_crown.20
	title = apb_crown.20.name
	desc = apb_crown.20.desc
	picture = GFX_evt_burning_city
	show_sound = event_default
	is_triggered_only = yes

	option = {
		name = apb_crown.20.a
		custom_tooltip = apb_crown_exile_tt
	}
}

# Crown World retaken
country_event = {
	id = apb_crown.21
	title = apb_crown.21.name
	desc = apb_crown.21.desc
	picture = GFX_evt_victorious_army
	show_sound = event_default
	is_triggered_only = yes

	option = {
		name = apb_crown.21.a
		custom_tooltip = apb_crown_retaken_tt
	}
}
