# The Home Guard

# Scope: country.
# Recounts the wars in which this country is a defender (apb_homeguard_wars).
# If the count went up, the Home Guard musters. "Homeland Defended" is held
# for as long as the count is above zero. Safe to call at any time.
apb_homeguard_update = {
	save_event_target_as = apb_hg_country
	if = {
		limit = {
			NOT = { is_variable_set = apb_homeguard_wars }
		}
		set_variable = { which = apb_homeguard_wars value = 0 }
	}
	set_variable = { which = apb_homeguard_now value = 0 }
	every_war = {
		limit = {
			is_war_participant = { who = event_target:apb_hg_country side = defenders }
		}
		event_target:apb_hg_country = {
			change_variable = { which = apb_homeguard_now value = 1 }
		}
	}

	if = {
		limit = {
			check_variable = { which = apb_homeguard_now value > apb_homeguard_wars }
		}
		apb_homeguard_muster = yes
		if = {
			limit = { is_ai = no }
			country_event = { id = apb_homeguard.10 }
		}
	}
	set_variable = { which = apb_homeguard_wars value = apb_homeguard_now }

	if = {
		limit = {
			check_variable = { which = apb_homeguard_wars value > 0 }
		}
		if = {
			limit = {
				NOT = { has_modifier = apb_homeland_defended }
			}
			add_modifier = {
				modifier = apb_homeland_defended
				days = -1
			}
		}
	}
	else = {
		if = {
			limit = { has_modifier = apb_homeland_defended }
			remove_modifier = apb_homeland_defended
		}
		every_owned_planet = {
			limit = { has_modifier = apb_home_guard_capital }
			remove_modifier = apb_home_guard_capital
		}
	}
}

# Scope: country.
# The Home Guard musters: Homeland Defended gives every colony 2 more defence
# armies, and the capital gets 2 more on top. Defence armies are counted by the
# engine from planet_defense_armies_add, so armies created by script are culled
# at the next daily update (tested in 4.6); the modifiers are what holds.
apb_homeguard_muster = {
	every_owned_planet = {
		limit = { has_modifier = apb_home_guard_capital }
		remove_modifier = apb_home_guard_capital
	}
	capital_scope = {
		add_modifier = {
			modifier = apb_home_guard_capital
			days = -1
		}
	}
}
