# The Tally (ap_the_tally)
#
# State (all on the country):
#   variable apb_tally          the count, 0-100
#   variable apb_tally_purged   purged pops not yet turned into a point (0-999)
#   variable apb_tally_fleets   times the naval capacity trophy was taken
#   flag     apb_tally_full     the count reached 100: the hooks stop early
#   flag     apb_tally_changed  the count rose since the last yearly reading
#   flags    apb_tally_trophy_10/25/50/100   trophy already offered

# Scope: country. AMOUNT = points to add.
# The largest single step is 10 and the trophy marks are at least 15 apart,
# so one call never passes two marks.
apb_tally_add = {
	if = {
		limit = {
			NOT = { is_variable_set = apb_tally }
		}
		set_variable = {
			which = apb_tally
			value = 0
		}
	}
	if = {
		limit = {
			check_variable = {
				which = apb_tally
				value < 100
			}
		}
		change_variable = {
			which = apb_tally
			value = $AMOUNT$
		}
		if = {
			limit = {
				check_variable = {
					which = apb_tally
					value >= 100
				}
			}
			set_variable = {
				which = apb_tally
				value = 100
			}
			set_country_flag = apb_tally_full
		}
		set_country_flag = apb_tally_changed
		apb_tally_refresh = yes
		apb_tally_check_trophy = { MARK = 10 }
		apb_tally_check_trophy = { MARK = 25 }
		apb_tally_check_trophy = { MARK = 50 }
		apb_tally_check_trophy = { MARK = 100 }
	}
}

# Scope: country. Rebuilds the stacking modifier from the count.
apb_tally_refresh = {
	if = {
		limit = { has_modifier = apb_the_tally }
		remove_modifier = apb_the_tally
	}
	if = {
		limit = {
			is_variable_set = apb_tally
			check_variable = {
				which = apb_tally
				value > 0
			}
		}
		add_modifier = {
			modifier = apb_the_tally
			multiplier = apb_tally
		}
	}
}

# Scope: country. Offers a trophy the first time the count reaches MARK.
apb_tally_check_trophy = {
	if = {
		limit = {
			NOT = { has_country_flag = apb_tally_trophy_$MARK$ }
			check_variable = {
				which = apb_tally
				value >= $MARK$
			}
		}
		set_country_flag = apb_tally_trophy_$MARK$
		country_event = { id = apb_tally.10 }
	}
}

# Scope: country. One more block of naval capacity from the trophy.
apb_tally_add_fleet_trophy = {
	if = {
		limit = {
			NOT = { is_variable_set = apb_tally_fleets }
		}
		set_variable = {
			which = apb_tally_fleets
			value = 0
		}
	}
	change_variable = {
		which = apb_tally_fleets
		value = 1
	}
	if = {
		limit = { has_modifier = apb_tally_war_fleet }
		remove_modifier = apb_tally_war_fleet
	}
	add_modifier = {
		modifier = apb_tally_war_fleet
		multiplier = apb_tally_fleets
	}
}
