###########################
# The Tally (ap_the_tally)
###########################
namespace = apb_tally

# A colony was conquered: +1.
# Hook: on_colony_conquer. This = Colony scope, From = new owner, FromFrom = former owner.
carrier_event = {
	id = apb_tally.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		from = {
			has_ascension_perk = ap_the_tally
			NOT = { has_country_flag = apb_tally_full }
		}
	}

	immediate = {
		from = {
			apb_tally_add = { AMOUNT = 1 }
		}
	}
}

# Pops finished purging: +1 for every full 1,000.
# Hook: on_pop_purged. This = Colony scope, From = Country, FromFrom = Pop group,
# local_pop_amount = how many pops were purged (read the way forceofwill.500 reads it).
# The Synaptic Lathe resettlement is left out, as in vanilla's crisis.4030.
carrier_event = {
	id = apb_tally.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		from = {
			has_ascension_perk = ap_the_tally
			NOT = { has_country_flag = apb_tally_full }
		}
		fromfrom = {
			NOT = {
				has_purge_type = { country = from type = purge_cosmogenesis_lathe_resettle }
			}
		}
	}

	immediate = {
		from = {
			if = {
				limit = {
					NOT = { is_variable_set = apb_tally_purged }
				}
				set_variable = {
					which = apb_tally_purged
					value = 0
				}
			}
			change_variable = {
				which = apb_tally_purged
				value = root.local_pop_amount
			}
			while = {
				limit = {
					check_variable = {
						which = apb_tally_purged
						value >= 1000
					}
				}
				subtract_variable = {
					which = apb_tally_purged
					value = 1000
				}
				apb_tally_add = { AMOUNT = 1 }
			}
		}
	}
}

# An empire was destroyed: +10 for its destroyer.
# Hook: on_country_destroyed. This = destroyed country, From = destroyer (optional).
country_event = {
	id = apb_tally.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		from = {
			has_ascension_perk = ap_the_tally
			NOT = { has_country_flag = apb_tally_full }
		}
		OR = {
			is_country_type = default
			is_country_type = fallen_empire
			is_country_type = awakened_fallen_empire
		}
	}

	immediate = {
		from = {
			apb_tally_add = { AMOUNT = 10 }
		}
	}
}

# The yearly reading: shown to players in years when the count rose.
# Hook: on_yearly_pulse_country. This = country.
country_event = {
	id = apb_tally.5
	title = apb_tally.5.name
	desc = apb_tally.5.desc
	picture = GFX_evt_announcement
	show_sound = event_default
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_the_tally
		has_country_flag = apb_tally_changed
		is_ai = no
	}

	immediate = {
		remove_country_flag = apb_tally_changed
	}

	option = {
		name = apb_tally.5.a
	}
}

# A trophy at 10, 25, 50 and 100.
country_event = {
	id = apb_tally.10
	title = apb_tally.10.name
	desc = apb_tally.10.desc
	picture = GFX_evt_victorious_army
	show_sound = event_default
	is_triggered_only = yes

	immediate = {
		# Alloys: 20 for each year since the start, counting at least 50 years.
		export_trigger_value_to_variable = {
			trigger = years_passed
			variable = apb_tally_years
		}
		if = {
			limit = {
				check_variable = {
					which = apb_tally_years
					value < 50
				}
			}
			set_variable = {
				which = apb_tally_years
				value = 50
			}
		}
	}

	# A renowned commander
	option = {
		name = apb_tally.10.a
		custom_tooltip = apb_tally_trophy_commander_tt
		hidden_effect = {
			create_leader = {
				class = commander
				tier = leader_tier_renowned
				species = owner_main_species
				name = random
				skill = 5
				event_leader = no
				randomize_traits = yes
			}
		}
		ai_chance = {
			factor = 1
		}
	}

	# Naval capacity
	option = {
		name = apb_tally.10.b
		custom_tooltip = apb_tally_trophy_fleet_tt
		hidden_effect = {
			apb_tally_add_fleet_trophy = yes
		}
		ai_chance = {
			factor = 1
		}
	}

	# Alloys
	option = {
		name = apb_tally.10.c
		add_resource = {
			alloys = 20
			mult = apb_tally_years
		}
		ai_chance = {
			factor = 1
		}
	}
}
