###########################
# Wards of the Stars (ap_wards_of_the_stars)
#
# A "ward" is a pre-FTL country whose capital has an observation post owned by
# the perk's owner. Wards carry the country flag apb_ward_of@<watcher>, so the
# watcher can still be found after the post is dismantled (vanilla removes the
# post before the new empire is created).
###########################
namespace = apb_wards

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

	trigger = {
		has_ascension_perk = ap_wards_of_the_stars
	}

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

# Recount the wards, mark them and rebuild the influence modifier.
# Fired from the perk's on_enabled, the yearly pulse and the events below.
country_event = {
	id = apb_wards.2
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_variable = { which = apb_wards_count value = 0 }

		# Marks last two years and are renewed here while the post stands, so a ward
		# that loses its post on the way to the stars still counts when it graduates.
		every_country = {
			limit = {
				is_primitive = yes
				exists = capital_scope
				capital_scope = {
					has_observation_outpost = yes
					observation_outpost = { exists = owner }
					observation_outpost_owner = { is_same_value = root }
				}
			}
			remove_country_flag = apb_ward_of@root
			set_timed_country_flag = { flag = apb_ward_of@root days = 720 }
			if = {
				limit = { has_pre_ftl_age = early_space_age }
				set_country_flag = apb_wards_space_age_seen
			}
			root = {
				change_variable = { which = apb_wards_count value = 1 }
			}
		}

		# Influence: one stack per ward, four at most.
		set_variable = { which = apb_wards_stacks value = apb_wards_count }
		if = {
			limit = {
				check_variable = { which = apb_wards_stacks value > 4 }
			}
			set_variable = { which = apb_wards_stacks value = 4 }
		}
		if = {
			limit = { has_modifier = apb_wards_standing }
			remove_modifier = apb_wards_standing
		}
		if = {
			limit = {
				check_variable = { which = apb_wards_stacks value > 0 }
			}
			add_modifier = {
				modifier = apb_wards_standing
				multiplier = apb_wards_stacks
			}
		}
	}
}

# Five-year field reports: society research and unity from each ward.
# Months of income per ward: 2/1 (Stone to Iron), 4/2 (Late Medieval, Renaissance),
# 6/3 (Steam, Industrial), 8/4 (Machine, Atomic, Early Space).
# Hook: on_five_year_pulse_country. This = Country.
country_event = {
	id = apb_wards.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_wards_of_the_stars
	}

	immediate = {
		country_event = { id = apb_wards.2 }
		set_variable = { which = apb_wards_reporting value = 0 }

		every_country = {
			limit = {
				is_primitive = yes
				exists = capital_scope
				capital_scope = {
					has_observation_outpost = yes
					observation_outpost = { exists = owner }
					observation_outpost_owner = { is_same_value = root }
				}
			}
			root = {
				change_variable = { which = apb_wards_reporting value = 1 }
			}
			if = {
				limit = {
					OR = {
						has_pre_ftl_age = machine_age
						has_pre_ftl_age = atomic_age
						has_pre_ftl_age = early_space_age
					}
				}
				root = {
					add_monthly_resource_mult = {
						resource = society_research
						value = 8
						min = 200
						max = 100000
					}
					add_monthly_resource_mult = {
						resource = unity
						value = 4
						min = 100
						max = 100000
					}
				}
			}
			else_if = {
				limit = {
					OR = {
						has_pre_ftl_age = steam_age
						has_pre_ftl_age = industrial_age
					}
				}
				root = {
					add_monthly_resource_mult = {
						resource = society_research
						value = 6
						min = 150
						max = 100000
					}
					add_monthly_resource_mult = {
						resource = unity
						value = 3
						min = 75
						max = 100000
					}
				}
			}
			else_if = {
				limit = {
					OR = {
						has_pre_ftl_age = late_medieval_age
						has_pre_ftl_age = renaissance_age
					}
				}
				root = {
					add_monthly_resource_mult = {
						resource = society_research
						value = 4
						min = 100
						max = 100000
					}
					add_monthly_resource_mult = {
						resource = unity
						value = 2
						min = 50
						max = 100000
					}
				}
			}
			else = {
				root = {
					add_monthly_resource_mult = {
						resource = society_research
						value = 2
						min = 50
						max = 100000
					}
					add_monthly_resource_mult = {
						resource = unity
						value = 1
						min = 25
						max = 100000
					}
				}
			}
		}

		if = {
			limit = {
				is_ai = no
				check_variable = { which = apb_wards_reporting value > 0 }
			}
			country_event = { id = apb_wards.4 }
		}
	}
}

# Field reports (player notice; the rewards were paid in apb_wards.3).
country_event = {
	id = apb_wards.4
	title = apb_wards.4.name
	desc = apb_wards.4.desc
	picture = GFX_evt_medieval_alien_civilization
	show_sound = event_radio_chatter
	is_triggered_only = yes

	option = {
		name = apb_wards.4.a
		custom_tooltip = apb_wards.4.tt
	}
}

# A pre-FTL civilisation entered a new age.
# Hook: on_ftl_society_enters_new_age. This = the pre-FTL country.
country_event = {
	id = apb_wards.10
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_primitive = yes
		NOT = { has_country_flag = apb_wards_space_age_seen }
		exists = capital_scope
		capital_scope = {
			has_observation_outpost = yes
			observation_outpost = { exists = owner }
			observation_outpost_owner = {
				has_ascension_perk = ap_wards_of_the_stars
			}
		}
	}

	immediate = {
		capital_scope = {
			observation_outpost_owner = {
				root = { set_timed_country_flag = { flag = apb_ward_of@prev days = 720 } }
				country_event = { id = apb_wards.11 }	# From = the ward
			}
		}
		# Vanilla fires this hook once more when an Early Space Age society achieves FTL;
		# that is graduation (apb_wards.20/.21), not a new age.
		if = {
			limit = { has_pre_ftl_age = early_space_age }
			set_country_flag = apb_wards_space_age_seen
		}
	}
}

# A ward comes of age: research or unity.
# Root = watcher, From = the ward.
country_event = {
	id = apb_wards.11
	title = apb_wards.11.name
	desc = apb_wards.11.desc
	picture = GFX_evt_medieval_alien_civilization
	show_sound = event_alien_nature
	location = from.capital_scope
	is_triggered_only = yes

	trigger = {
		exists = from
	}

	option = {
		name = apb_wards.11.a
		add_monthly_resource_mult = {
			resource = society_research
			value = @tier1researchreward
			min = @tier1researchmin
			max = @tier1researchmax
		}
	}

	option = {
		name = apb_wards.11.b
		add_monthly_resource_mult = {
			resource = unity
			value = @tier1unityreward
			min = @tier1unitymin
			max = @tier1unitymax
		}
	}
}

# A country changed type. If it is a former ward that is now a normal empire,
# tell every empire that watched over it.
# Hook: on_country_type_changed. This = Country.
country_event = {
	id = apb_wards.20
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = default
		NOT = { has_country_flag = apb_wards_graduated }
		any_country = {
			has_ascension_perk = ap_wards_of_the_stars
			root = { has_country_flag = apb_ward_of@prev }
		}
	}

	immediate = {
		set_country_flag = apb_wards_graduated
		every_country = {
			limit = {
				has_ascension_perk = ap_wards_of_the_stars
				root = { has_country_flag = apb_ward_of@prev }
			}
			# Delayed so vanilla's own first-flight events (and their subject offers) settle first.
			country_event = { id = apb_wards.21 days = 5 }	# From = the new empire
		}
	}
}

# A ward takes flight. Root = watcher, From = the new empire.
country_event = {
	id = apb_wards.21
	title = apb_wards.21.name
	desc = apb_wards.21.desc
	picture = GFX_evt_early_space_age
	show_sound = event_ship_thrusters
	location = from.capital_scope
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_wards_of_the_stars
		exists = from
		from = { is_country_type = default }
	}

	immediate = {
		from = {
			remove_country_flag = apb_ward_of@root
			establish_communications_no_message = root
			add_trust = {
				amount = 50
				who = root
			}
			add_opinion_modifier = {
				who = root
				modifier = apb_opinion_watched_over_us
			}
		}
		country_event = { id = apb_wards.2 }
	}

	# Take them under our protection.
	option = {
		name = apb_wards.21.a
		trigger = {
			is_subject = no
			from = {
				is_subject = no
				NOT = { has_country_flag = was_enlightened }
			}
		}
		custom_tooltip = apb_wards.21.a.tt
		hidden_effect = {
			from = {
				if = {
					limit = {
						root = { has_valid_civic = civic_inwards_perfection }
					}
					set_subject_of = {
						who = root
						preset = preset_tributary
						allow_instant_negotiation = yes
					}
				}
				else = {
					set_subject_of = {
						who = root
						preset = preset_protectorate
						allow_instant_negotiation = yes
					}
				}
			}
		}
		add_resource = { influence = 100 }
		ai_chance = { factor = 3 }
	}

	# Let them go their own way.
	option = {
		name = apb_wards.21.b
		trigger = {
			from = { NOT = { has_country_flag = was_enlightened } }
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier2unityreward
			min = @tier2unitymin
			max = @tier2unitymax
		}
		ai_chance = { factor = 1 }
	}

	# We already settled terms with them when we uplifted them.
	option = {
		name = apb_wards.21.c
		trigger = {
			from = { has_country_flag = was_enlightened }
		}
	}
}
