# Hall of Echoes
# Scope: country.
# CLASS  = scientist / commander / official
# AMOUNT = echoes to add (1 or 2)
# EVENT  = (optional) visible event to show the player when an echo is gained
#
# The echo count lives in the country variable apb_echoes_<CLASS> (0-10).
# The modifier is always rebuilt from that variable, so removing and re-adding
# it never loses stacks.
apb_hoe_add_echo = {
	if = {
		limit = {
			NOT = { is_variable_set = apb_echoes_$CLASS$ }
		}
		set_variable = {
			which = apb_echoes_$CLASS$
			value = 0
		}
	}
	if = {
		limit = {
			check_variable = {
				which = apb_echoes_$CLASS$
				value < 10
			}
		}
		change_variable = {
			which = apb_echoes_$CLASS$
			value = $AMOUNT$
		}
		if = {
			limit = {
				check_variable = {
					which = apb_echoes_$CLASS$
					value > 10
				}
			}
			set_variable = {
				which = apb_echoes_$CLASS$
				value = 10
			}
		}
		apb_hoe_refresh_echo = { CLASS = $CLASS$ }
		[[EVENT]
		if = {
			limit = { is_ai = no }
			country_event = { id = $EVENT$ }
		}
		]
	}
}

# Rebuilds the echo modifier for one class from its variable.
apb_hoe_refresh_echo = {
	if = {
		limit = { has_modifier = apb_echo_$CLASS$ }
		remove_modifier = apb_echo_$CLASS$
	}
	if = {
		limit = {
			is_variable_set = apb_echoes_$CLASS$
			check_variable = {
				which = apb_echoes_$CLASS$
				value > 0
			}
		}
		add_modifier = {
			modifier = apb_echo_$CLASS$
			multiplier = apb_echoes_$CLASS$
		}
	}
}

# Scope: country. FROM must be the dead leader (on_leader_death).
# Level 8+ counts as two echoes.
apb_hoe_echo_from_dead_leader = {
	if = {
		limit = { from = { has_base_skill >= 8 } }
		apb_hoe_add_echo = { CLASS = $CLASS$ AMOUNT = 2 EVENT = $EVENT$ }
	}
	else = {
		apb_hoe_add_echo = { CLASS = $CLASS$ AMOUNT = 1 EVENT = $EVENT$ }
	}
}
