# Crown World (ap_crown_world)
#
# State:
#   carrier flag  apb_crown_world            the planet holding the title
#   carrier flag  apb_former_crown@<country> a lost Crown World, remembered for its old owner
#   variable      apb_crown_perks            perks counted (0-20), on the country and copied to the planet
#   country flags apb_crown_milestone_1/2/3  growth at 5, 10 and 15 perks, once per empire
#   country flag  apb_crown_feature_earned   the Seat of the Crown follows the title

# Scope: country.
# Recounts the perks, rebuilds the scaled modifier on the Crown World and hands out
# any milestone that is due. Does nothing visible while no planet holds the title.
apb_crown_refresh = {
	export_trigger_value_to_variable = {
		trigger = num_ascension_perks
		variable = apb_crown_perks
	}
	if = {
		limit = {
			check_variable = {
				which = apb_crown_perks
				value > 20
			}
		}
		set_variable = {
			which = apb_crown_perks
			value = 20
		}
	}
	every_owned_planet = {
		limit = { has_carrier_flag = apb_crown_world }
		set_variable = {
			which = apb_crown_perks
			value = owner.apb_crown_perks
		}
		if = {
			limit = { has_modifier = apb_crown_world }
			remove_modifier = apb_crown_world
		}
		if = {
			limit = {
				check_variable = {
					which = apb_crown_perks
					value > 0
				}
			}
			add_modifier = {
				modifier = apb_crown_world
				multiplier = apb_crown_perks
			}
		}
	}
	if = {
		limit = {
			any_owned_planet = { has_carrier_flag = apb_crown_world }
		}
		if = {
			limit = {
				NOT = { has_country_flag = apb_crown_milestone_1 }
				check_variable = {
					which = apb_crown_perks
					value >= 5
				}
			}
			set_country_flag = apb_crown_milestone_1
			every_owned_planet = {
				limit = { has_carrier_flag = apb_crown_world }
				apb_crown_grow_world = yes
			}
			if = {
				limit = { is_ai = no }
				country_event = { id = apb_crown.10 }
			}
		}
		if = {
			limit = {
				NOT = { has_country_flag = apb_crown_milestone_2 }
				check_variable = {
					which = apb_crown_perks
					value >= 10
				}
			}
			set_country_flag = apb_crown_milestone_2
			every_owned_planet = {
				limit = { has_carrier_flag = apb_crown_world }
				apb_crown_grow_world = yes
			}
			if = {
				limit = { is_ai = no }
				country_event = { id = apb_crown.11 }
			}
		}
		if = {
			limit = {
				NOT = { has_country_flag = apb_crown_milestone_3 }
				check_variable = {
					which = apb_crown_perks
					value >= 15
				}
			}
			set_country_flag = apb_crown_milestone_3
			set_country_flag = apb_crown_feature_earned
			every_owned_planet = {
				limit = { has_carrier_flag = apb_crown_world }
				apb_crown_grow_world = yes
				if = {
					limit = {
						NOT = { has_modifier = apb_crown_seat }
					}
					add_modifier = {
						modifier = apb_crown_seat
						days = -1
					}
				}
			}
			if = {
				limit = { is_ai = no }
				country_event = { id = apb_crown.12 }
			}
		}
	}
}

# Scope: planet. +2 planet size, except on habitats and ring worlds.
apb_crown_grow_world = {
	if = {
		limit = {
			NOR = {
				is_planet_class = pc_habitat
				is_planet_class = pc_ringworld_habitable
				is_planet_class = pc_shattered_ring_habitable
			}
		}
		change_planet_size = 2
	}
}

# Scope: planet (must have an owner with the perk). Gives the planet the title.
apb_crown_name_world = {
	set_carrier_flag = apb_crown_world
	if = {
		limit = {
			owner = { has_country_flag = apb_crown_feature_earned }
			NOT = { has_modifier = apb_crown_seat }
		}
		add_modifier = {
			modifier = apb_crown_seat
			days = -1
		}
	}
	owner = {
		if = {
			limit = { has_modifier = apb_crown_in_exile }
			remove_modifier = apb_crown_in_exile
		}
		apb_crown_refresh = yes
	}
}

# Scope: planet. Takes the title away again. Size already gained stays.
apb_crown_strip_world = {
	remove_carrier_flag = apb_crown_world
	if = {
		limit = { has_modifier = apb_crown_world }
		remove_modifier = apb_crown_world
	}
	if = {
		limit = { has_modifier = apb_crown_seat }
		remove_modifier = apb_crown_seat
	}
}

# Scope: country. The Crown World was lost: 20 years of Crown in Exile.
apb_crown_go_into_exile = {
	if = {
		limit = { has_modifier = apb_crown_in_exile }
		remove_modifier = apb_crown_in_exile
	}
	add_modifier = {
		modifier = apb_crown_in_exile
		days = 7200
	}
	if = {
		limit = { is_ai = no }
		country_event = { id = apb_crown.20 }
	}
}
