###########################
# The Home Guard (ap_home_guard)
#
# apb_homeguard_wars (country variable) = number of wars in which we are a
# defender. It is recounted from the war list every time, so it cannot drift.
###########################
namespace = apb_homeguard

# A war began, or we joined one that was already under way.
# Hooks: on_war_beginning (Root = Country, From = War),
#        on_war_entered (This = Country, From = opponent war leader).
country_event = {
	id = apb_homeguard.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_home_guard
	}

	# Armies created in the same tick as on_war_beginning do not appear (tested in 4.6),
	# so the recount and muster run a day later.
	immediate = {
		country_event = { id = apb_homeguard.7 days = 1 }
	}
}

# We left a war in which we were a defender. The war is still on our list at
# this point, so the recount waits a day.
# Hook: on_war_exited. This = Country, From = War.
country_event = {
	id = apb_homeguard.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_home_guard
		exists = from
		from = {
			is_war_participant = { who = root side = defenders }
		}
	}

	immediate = {
		country_event = { id = apb_homeguard.3 days = 1 }
	}
}

# Recount after a war ended. A defender marked by apb_homeguard.4 or .6 gets
# Veterans' Peace for five years (1800 days).
country_event = {
	id = apb_homeguard.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_home_guard
	}

	immediate = {
		apb_homeguard_update = yes
		if = {
			limit = { has_country_flag = apb_homeguard_held }
			remove_country_flag = apb_homeguard_held
			if = {
				limit = { has_modifier = apb_veterans_peace }
				remove_modifier = apb_veterans_peace
			}
			add_modifier = {
				modifier = apb_veterans_peace
				days = 1800
			}
			if = {
				limit = { is_ai = no }
				country_event = { id = apb_homeguard.11 }
			}
		}
	}
}

# The defenders won.
# Hook: on_war_won. Root = Winner Warleader, From = Loser Warleader, FromFrom = War.
country_event = {
	id = apb_homeguard.4
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = fromfrom
		fromfrom = {
			is_war_participant = { who = root side = defenders }
		}
	}

	immediate = {
		fromfrom = {
			every_defender = {
				limit = { has_ascension_perk = ap_home_guard }
				set_timed_country_flag = {
					flag = apb_homeguard_held
					days = 10
				}
				country_event = { id = apb_homeguard.3 days = 2 }
			}
		}
	}
}

# The defenders lost: recount, no Veterans' Peace.
# Hook: on_war_lost. Root = Loser Warleader, From = Winner Warleader, FromFrom = War.
country_event = {
	id = apb_homeguard.5
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = fromfrom
		fromfrom = {
			is_war_participant = { who = root side = defenders }
		}
	}

	immediate = {
		fromfrom = {
			every_defender = {
				limit = { has_ascension_perk = ap_home_guard }
				country_event = { id = apb_homeguard.3 days = 2 }
			}
		}
	}
}

# Status quo.
# Hooks: on_status_quo, on_status_quo_forced. FromFromFromFrom = War.
country_event = {
	id = apb_homeguard.6
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = fromfromfromfrom
	}

	immediate = {
		fromfromfromfrom = {
			every_defender = {
				limit = { has_ascension_perk = ap_home_guard }
				set_timed_country_flag = {
					flag = apb_homeguard_held
					days = 10
				}
				country_event = { id = apb_homeguard.3 days = 2 }
			}
		}
	}
}

# Perk taken: muster at once if we are already defending. Also fired a day after
# a war begins or is joined (apb_homeguard.1).
country_event = {
	id = apb_homeguard.7
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_home_guard
	}

	immediate = {
		apb_homeguard_update = yes
	}
}

# Yearly safety net, in case a war ended without any of the hooks above.
# Hook: on_yearly_pulse_country. This = Country.
country_event = {
	id = apb_homeguard.8
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_home_guard
		OR = {
			has_modifier = apb_homeland_defended
			is_at_war = yes
		}
	}

	immediate = {
		apb_homeguard_update = yes
	}
}

# The Home Guard musters (player notice).
country_event = {
	id = apb_homeguard.10
	title = apb_homeguard.10.name
	desc = apb_homeguard.10.desc
	picture = GFX_evt_ground_combat
	show_sound = event_radio_chatter
	is_triggered_only = yes

	option = {
		name = apb_homeguard.10.a
		custom_tooltip = apb_homeguard.10.tt
	}
}

# Veterans' Peace (player notice).
country_event = {
	id = apb_homeguard.11
	title = apb_homeguard.11.name
	desc = apb_homeguard.11.desc
	picture = GFX_evt_victorious_army
	show_sound = event_celebration
	is_triggered_only = yes

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