﻿# Pilgrims and Preachers - on_actions
# Everything is appended through on_actions = { } so no vanilla block is replaced.

# --- Pilgrims camp purpose upkeep -------------------------------------------

on_death = {
	on_actions = { ppr_on_death }
}

ppr_on_death = {
	effect = {
		# Inherit flavourisation, as vanilla does for its own purposes
		if = {
			limit = { has_variable = ppr_flavourise_camp_purpose_pilgrims }
			primary_heir ?= {
				set_variable = ppr_flavourise_camp_purpose_pilgrims
			}
		}
	}
}

quarterly_playable_pulse = {
	on_actions = { ppr_purpose_maintenance }
}

# Other purposes' on_pass don't know about Pilgrims, so tidy up after them,
# and set things right after inheritance or game start.
ppr_purpose_maintenance = {
	effect = {
		ppr_ensure_missionary_carts_effect = yes
		if = {
			limit = { has_realm_law = ppr_camp_purpose_pilgrims }
			if = {
				limit = { NOT = { has_variable = ppr_flavourise_camp_purpose_pilgrims } }
				set_variable = ppr_flavourise_camp_purpose_pilgrims
			}
			if = {
				limit = {
					has_perk = local_arbitration_perk
					NOT = { has_character_modifier = ppr_landless_local_arbitration_pilgrims_modifier }
				}
				local_arbitration_boon_set_purpose_effect = {
					MODIFIER = ppr_landless_local_arbitration_pilgrims_modifier
				}
			}
		}
		else = {
			if = {
				limit = { has_variable = ppr_flavourise_camp_purpose_pilgrims }
				remove_variable = ppr_flavourise_camp_purpose_pilgrims
			}
			if = {
				limit = { has_character_modifier = ppr_landless_local_arbitration_pilgrims_modifier }
				remove_character_modifier = ppr_landless_local_arbitration_pilgrims_modifier
			}
		}
	}
}

# --- Heresiarch registry ------------------------------------------------------
# Contracts need to find heresiarchs cheaply. Instead of scanning every character
# whenever contracts are generated, keep a small global list refreshed once a year.

yearly_global_pulse = {
	on_actions = { ppr_refresh_heresiarch_list }
}

on_game_start_after_lobby = {
	on_actions = { ppr_refresh_heresiarch_list }
}

ppr_refresh_heresiarch_list = {
	effect = {
		ppr_refresh_heresiarch_list_effect = yes
	}
}

# --- Events ------------------------------------------------------------------

travel_events_on_action = {
	on_actions = { ppr_travel_events }
}

ppr_travel_events = {
	trigger = {
		is_ai = no
		is_landless_adventurer = yes
		has_realm_law = ppr_camp_purpose_pilgrims
	}
	random_events = {
		chance_of_no_event = 90
		100 = ppr_flavor_events.0001 # The Mendicant Friar
		100 = ppr_flavor_events.0011 # A Wayside Shrine
		100 = ppr_camp_events.0006 # Flagellants on the Road
		100 = ppr_camp_events.0007 # The Hermit
	}
}

ep3_yearly_events_laamps = {
	on_actions = { ppr_yearly_camp_events }
}

ppr_yearly_camp_events = {
	trigger = {
		has_realm_law = ppr_camp_purpose_pilgrims
	}
	random_events = {
		300 = 0 # No event: roughly one Pilgrims event every year or two
		100 = ppr_flavor_events.0021 # Heretics at the Campfire
		100 = ppr_camp_events.0001 # Night Vigil
		150 = ppr_camp_events.0002 # A Miracle in Camp? (needs a sick follower)
		100 = ppr_camp_events.0003 # The Penitent
		100 = ppr_camp_events.0004 # Pious Quarrel
		100 = ppr_camp_events.0005 # The Relic Peddler
	}
}
