###########################
# Many Voices (ap_many_voices)
#
# Country variables: apb_voices_count (0-12, this year's count),
#                    apb_voices_peak (highest count so far).
# Species flags:     apb_voice_counted@<country> (counted this year),
#                    apb_voice_heard@<country> (has been welcomed as a voice).
###########################
namespace = apb_voices

# Yearly count.
# Hook: on_yearly_pulse_country. This = Country.
country_event = {
	id = apb_voices.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_many_voices
	}

	immediate = {
		country_event = { id = apb_voices.2 }
	}
}

# Count the voices. Also fired from the perk's on_enabled.
country_event = {
	id = apb_voices.2
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_owned_species = {
			limit = { has_species_flag = apb_voice_counted@root }
			remove_species_flag = apb_voice_counted@root
		}
		set_variable = { which = apb_voices_count value = 0 }

		# The cheap checks sit in the limit. The rest is in the body, so each
		# species sees the marks left by the ones counted before it and the
		# pop count is skipped once 12 voices are found.
		every_owned_species = {
			limit = {
				is_sapient = yes
				has_citizenship_type = { type = citizenship_full country = root }
			}
			save_event_target_as = apb_voice_candidate
			if = {
				limit = {
					root = {
						check_variable = { which = apb_voices_count value < 12 }
					}
					# Not the same species as, or a subspecies of, one already counted.
					NOT = {
						root = {
							any_owned_species = {
								has_species_flag = apb_voice_counted@root
								OR = {
									is_same_species = event_target:apb_voice_candidate
									is_subspecies = event_target:apb_voice_candidate
									event_target:apb_voice_candidate = { is_subspecies = prev }
								}
							}
						}
					}
					# At least 1,000 full-citizen pops of this species.
					root = {
						count_owned_pop_amount = {
							limit = {
								is_same_species = event_target:apb_voice_candidate
								has_citizenship_type = { type = citizenship_full country = root }
							}
							count >= 1000
						}
					}
				}
				set_species_flag = apb_voice_counted@root
				root = {
					change_variable = { which = apb_voices_count value = 1 }
				}
			}
		}

		# First count after taking the perk: no welcome events.
		if = {
			limit = {
				NOT = { is_variable_set = apb_voices_peak }
			}
			every_owned_species = {
				limit = { has_species_flag = apb_voice_counted@root }
				set_species_flag = apb_voice_heard@root
			}
			set_variable = { which = apb_voices_peak value = apb_voices_count }
		}
		# A new record: welcome every voice we have not heard before.
		else_if = {
			limit = {
				check_variable = { which = apb_voices_count value > apb_voices_peak }
			}
			every_owned_species = {
				limit = {
					has_species_flag = apb_voice_counted@root
					NOT = { has_species_flag = apb_voice_heard@root }
				}
				save_event_target_as = apb_voice_candidate
				if = {
					limit = {
						NOT = {
							root = {
								any_owned_species = {
									has_species_flag = apb_voice_heard@root
									OR = {
										is_same_species = event_target:apb_voice_candidate
										is_subspecies = event_target:apb_voice_candidate
										event_target:apb_voice_candidate = { is_subspecies = prev }
									}
								}
							}
						}
					}
					root = {
						country_event = { id = apb_voices.10 scopes = { from = event_target:apb_voice_candidate } }
					}
				}
				set_species_flag = apb_voice_heard@root
			}
			set_variable = { which = apb_voices_peak value = apb_voices_count }
		}

		# Rebuild the scaled modifier.
		if = {
			limit = { has_modifier = apb_many_voices }
			remove_modifier = apb_many_voices
		}
		if = {
			limit = {
				check_variable = { which = apb_voices_count value > 0 }
			}
			add_modifier = {
				modifier = apb_many_voices
				multiplier = apb_voices_count
			}
		}

		# Permanent extras at 4, 8 and 12 voices, once each.
		if = {
			limit = {
				check_variable = { which = apb_voices_count value >= 4 }
				NOT = { has_country_flag = apb_voices_quartet }
			}
			set_country_flag = apb_voices_quartet
			add_modifier = { modifier = apb_voices_quartet days = -1 }
			if = {
				limit = { is_ai = no }
				country_event = { id = apb_voices.11 }
			}
		}
		if = {
			limit = {
				check_variable = { which = apb_voices_count value >= 8 }
				NOT = { has_country_flag = apb_voices_chorus }
			}
			set_country_flag = apb_voices_chorus
			add_modifier = { modifier = apb_voices_chorus days = -1 }
			if = {
				limit = { is_ai = no }
				country_event = { id = apb_voices.12 }
			}
		}
		if = {
			limit = {
				check_variable = { which = apb_voices_count value >= 12 }
				NOT = { has_country_flag = apb_voices_symphony }
			}
			set_country_flag = apb_voices_symphony
			add_modifier = { modifier = apb_voices_symphony days = -1 }
			if = {
				limit = { is_ai = no }
				country_event = { id = apb_voices.13 }
			}
		}
	}
}

# A new voice. Root = Country, From = the species.
country_event = {
	id = apb_voices.10
	title = apb_voices.10.name
	desc = apb_voices.10.desc
	picture = GFX_evt_galactic_senate
	show_sound = event_conversation
	is_triggered_only = yes

	option = {
		name = apb_voices.10.a
		add_monthly_resource_mult = {
			resource = unity
			value = @tier2unityreward
			min = @tier2unitymin
			max = @tier2unitymax
		}
	}
}

# Four voices (player notice; the modifier was added in apb_voices.2).
country_event = {
	id = apb_voices.11
	title = apb_voices.11.name
	desc = apb_voices.11.desc
	picture = GFX_evt_galactic_senate
	show_sound = event_conversation
	is_triggered_only = yes

	option = {
		name = apb_voices.11.a
		custom_tooltip = apb_voices.11.tt
	}
}

# Eight voices.
country_event = {
	id = apb_voices.12
	title = apb_voices.12.name
	desc = apb_voices.12.desc
	picture = GFX_evt_galactic_senate
	show_sound = event_conversation
	is_triggered_only = yes

	option = {
		name = apb_voices.12.a
		custom_tooltip = apb_voices.12.tt
	}
}

# Twelve voices.
country_event = {
	id = apb_voices.13
	title = apb_voices.13.name
	desc = apb_voices.13.desc
	picture = GFX_evt_galactic_senate
	show_sound = event_celebration
	is_triggered_only = yes

	option = {
		name = apb_voices.13.a
		custom_tooltip = apb_voices.13.tt
	}
}
