# The Great Work (ap_great_work)

# Scope: country.
# WING = archive / forum / foundry / garden   (the counter variable)
# MOD  = the static modifier suffix (same as WING, except garden_gestalt)
#
# The counter apb_gw_wing_<WING> is 2 after the first pick and +1 for each
# repeat pick. The static modifier holds half a wing, so a first pick gives the
# full value and each repeat adds half of it. The modifier is always rebuilt
# from the variable.
apb_gw_build_wing = {
	if = {
		limit = {
			NOT = { is_variable_set = apb_gw_wing_$WING$ }
		}
		set_variable = {
			which = apb_gw_wing_$WING$
			value = 2
		}
	}
	else = {
		change_variable = {
			which = apb_gw_wing_$WING$
			value = 1
		}
	}
	if = {
		limit = { has_modifier = apb_great_work_wing_$MOD$ }
		remove_modifier = apb_great_work_wing_$MOD$
	}
	add_modifier = {
		modifier = apb_great_work_wing_$MOD$
		multiplier = apb_gw_wing_$WING$
	}
}

# Scope: country. Gestalts get the gestalt version of the Garden modifier.
apb_gw_build_garden = {
	if = {
		limit = { is_gestalt = yes }
		apb_gw_build_wing = { WING = garden MOD = garden_gestalt }
	}
	else = {
		apb_gw_build_wing = { WING = garden MOD = garden }
	}
}

# Scope: country. Called when a stage ends (stages 2-4 on_first_enter).
# The AI picks a wing at random; a player gets the choice event.
apb_gw_offer_wing = {
	if = {
		limit = { is_ai = yes }
		random_list = {
			1 = { apb_gw_build_wing = { WING = archive MOD = archive } }
			1 = { apb_gw_build_wing = { WING = forum MOD = forum } }
			1 = { apb_gw_build_wing = { WING = foundry MOD = foundry } }
			1 = { apb_gw_build_garden = yes }
		}
	}
	else = {
		country_event = { id = apb_greatwork.10 }
	}
}

# Scope: country. "Call the faithful" is active: the current capital, and only
# the capital, carries apb_great_work_faithful. The modifier is timed (40 days)
# and renewed every month by apb_greatwork.2, so a copy stranded on a planet
# we no longer own (capital conquered or ceded) runs out by itself.
# Removed before it is re-added, so it can never stack.
apb_gw_faithful_on = {
	every_owned_planet = {
		limit = {
			has_modifier = apb_great_work_faithful
		}
		remove_modifier = apb_great_work_faithful
	}
	capital_scope? = {
		add_modifier = {
			modifier = apb_great_work_faithful
			days = 40
		}
	}
}

# Scope: country. Another approach is active, or the situation is over.
apb_gw_faithful_off = {
	every_owned_planet = {
		limit = {
			has_modifier = apb_great_work_faithful
		}
		remove_modifier = apb_great_work_faithful
	}
}
