﻿# Ambitions - yearly and death hooks
# Appends to the vanilla hooks instead of overwriting them.

yearly_playable_pulse = {
	on_actions = { amb_yearly_pulse amb_vow_yearly_pulse amb_vow_ai_pulse }
}

quarterly_playable_pulse = {
	on_actions = { amb_vow_quarterly_pulse }
}

on_death = {
	on_actions = { amb_on_death }
}

amb_yearly_pulse = {
	trigger = {
		is_adult = yes
	}
	effect = {
		if = {
			limit = { has_variable = amb_active }
			amb_check_progress_effect = yes
			# Mid-ambition events for players (new in 1.2): at most one per ambition
			if = {
				limit = {
					is_ai = no
					has_variable = amb_timer
					NOT = { has_variable = amb_mid_done }
					NOT = { amb_goal_met_trigger = yes }
				}
				random = {
					chance = 12
					set_variable = {
						name = amb_mid_done
						value = 1
					}
					random_list = {
						40 = { trigger_event = amb.0005 }
						30 = { trigger_event = amb.0006 }
						30 = { trigger_event = amb.0007 }
					}
				}
			}
		}
		else_if = {
			limit = {
				is_ai = no
				has_variable = amb_legacy_pending
			}
			trigger_event = amb.0008
		}
		else_if = {
			limit = {
				amb_can_get_new_ambition_trigger = yes
				amb_any_ambition_valid_trigger = yes
			}
			if = {
				limit = { is_ai = no }
				# Frequency set by the Ambition Settings event (new in 1.2)
				if = {
					limit = { global_var:amb_rate = flag:rare }
					random = {
						chance = 10
						trigger_event = amb.0001
					}
				}
				else_if = {
					limit = { global_var:amb_rate = flag:often }
					random = {
						chance = 40
						trigger_event = amb.0001
					}
				}
				else = {
					random = {
						chance = 25
						trigger_event = amb.0001
					}
				}
			}
			else = {
				if = {
					limit = { NOT = { has_global_variable = amb_ai_off } }
					random = {
						chance = 10
						trigger_event = amb.0001
					}
				}
			}
		}
	}
}

# Inheritance (new in 1.2): an unfinished ambition passes to the heir
amb_on_death = {
	effect = {
		# The Judgment (new in 1.4): nickname for the dead, legacy for the heir
		amb_judgment_effect = yes
		if = {
			limit = {
				has_variable = amb_active
				exists = primary_heir
			}
			primary_heir = {
				if = {
					limit = { is_ai = no }
					set_variable = {
						name = amb_legacy_pending
						value = 1
					}
					trigger_event = {
						id = amb.0008
						days = 30
					}
				}
			}
		}
		# House of Achievers passes to the heir
		if = {
			limit = {
				exists = dynasty
				exists = primary_heir
				dynasty = { var:amb_fulfilled_count >= 5 }
			}
			primary_heir = {
				add_character_modifier = { modifier = amb_house_of_achievers_modifier }
			}
		}
	}
}

# Vows (new in 1.3): yearly check of a sworn vow
amb_vow_yearly_pulse = {
	trigger = {
		has_variable = amb_vow_active
	}
	effect = {
		amb_vow_check_effect = yes
	}
}

# Vows (new in 1.3): a broken vow is caught each quarter, not just once a year
amb_vow_quarterly_pulse = {
	trigger = {
		has_variable = amb_vow_active
		NOT = { has_variable = amb_vow_breaking }
		amb_vow_broken_trigger = yes
	}
	effect = {
		set_variable = {
			name = amb_vow_breaking
			value = 1
			days = 60
		}
		trigger_event = amb_vow.0004
	}
}

# Vows (new in 1.3): AI rulers swear vows too
amb_vow_ai_pulse = {
	trigger = {
		is_ai = yes
		NOT = { has_global_variable = amb_ai_off }
		amb_vow_can_swear_trigger = yes
		amb_vow_any_valid_trigger = yes
	}
	effect = {
		random = {
			chance = 5
			trigger_event = amb_vow.0001
		}
	}
}
