﻿# Pilgrims and Preachers - preaching expeditions
# Appended to vanilla on_actions with on_actions = { }, nothing is replaced.
# See common/script_values/ppr_preaching_values.txt for how provisions, alms and piety flow.
# Preaching mode (army window button, common/scripted_guis/ppr_preaching_mode_guis.txt):
#   for alms (default)  -> each sermon earns gold, paid when the army is back at the camp
#   for the faith       -> each sermon earns piety instead (character variable ppr_preach_for_piety)

# --- Missionaries on the move ----------------------------------------------------
# root = owner of the army, scope:army = the army
on_army_enter_province = {
	on_actions = { ppr_missionaries_enter_province }
}

ppr_missionaries_enter_province = {
	trigger = {
		is_ai = no
		is_landless_adventurer = yes
		has_realm_law = ppr_camp_purpose_pilgrims
		scope:army = { is_barter_army = yes }
	}
	effect = {
		if = {
			# Back at the camp (its very barony): hand over what was earned, take fresh provisions.
			limit = { ppr_missionaries_at_camp_trigger = yes }
			ppr_protect_camp_county_effect = yes
			ppr_missionaries_home_effect = yes
		}
		else_if = {
			# Raised away from the camp: take provisions once, when they set out.
			limit = { NOT = { has_character_flag = ppr_missionaries_supplied } }
			ppr_load_mission_rations_effect = yes
		}
		# Purse shown in the army window (gui/window_army.gui)
		ppr_refresh_missionary_purse_effect = yes
		ppr_ensure_missionary_carts_effect = yes
	}
}

# Monthly: the same, for missionaries standing still (e.g. just raised at the camp).
on_army_monthly = {
	on_actions = { ppr_missionaries_monthly }
}

ppr_missionaries_monthly = {
	trigger = {
		is_ai = no
		is_landless_adventurer = yes
		has_realm_law = ppr_camp_purpose_pilgrims
		scope:army = { is_barter_army = yes }
	}
	effect = {
		if = {
			limit = { ppr_missionaries_at_camp_trigger = yes }
			ppr_protect_camp_county_effect = yes
			ppr_missionaries_home_effect = yes
		}
		else_if = {
			limit = { NOT = { has_character_flag = ppr_missionaries_supplied } }
			ppr_load_mission_rations_effect = yes
		}
		# Purse shown in the army window (gui/window_army.gui)
		ppr_refresh_missionary_purse_effect = yes
		ppr_ensure_missionary_carts_effect = yes
	}
}

# Quarterly: protects the camp's county from being preached to; keeps a small reserve of
# provisions for raising the missionaries when none exist; gives everything back when the
# Pilgrims purpose is left (or for the AI, which never preaches).
quarterly_playable_pulse = {
	on_actions = { ppr_missionaries_quarterly }
}

ppr_missionaries_quarterly = {
	trigger = {
		is_landless_adventurer = yes
		OR = {
			barter_goods > 0
			has_variable = ppr_rations_cap
			has_variable = ppr_alms_on_the_road
			AND = {
				is_ai = no
				has_realm_law = ppr_camp_purpose_pilgrims
			}
		}
	}
	effect = {
		if = {
			limit = {
				is_ai = no
				has_realm_law = ppr_camp_purpose_pilgrims
			}
			ppr_protect_camp_county_effect = yes
			ppr_refresh_missionary_purse_effect = yes
			if = {
				limit = { NOT = { any_army = { is_barter_army = yes } } }
				ppr_keep_mission_rations_reserve_effect = yes
			}
		}
		else = {
			remove_variable ?= ppr_alms_on_the_road
			remove_variable ?= ppr_disp_carried
			ppr_unload_mission_rations_effect = yes
		}
	}
}

# --- End of a sermon (barter action completed) ---------------------------------
on_barter_action_completion = {
	on_actions = { ppr_on_preaching_completion }
}

# root = the missionary (barter) army; scope:barterer, scope:barony, scope:county, scope:value = alms
# The engine has already turned the provisions handed out (barter goods) into its purse (barter
# loot). What the sermon earns (gold OR piety, by preaching mode) is kept on the army until it is
# back at the camp (its very barony).
ppr_on_preaching_completion = {
	trigger = {
		scope:barterer = { has_realm_law = ppr_camp_purpose_pilgrims }
	}
	effect = {
		# Preaching events (events/ppr_preaching_events.txt): the scopes are saved first, while
		# everything is still valid, and carried into the events, which fire a few days later.
		scope:barterer = { save_scope_as = preacher }
		scope:county = { save_scope_as = preach_county }
		scope:barony = { save_scope_as = preach_barony }
		scope:county.holder ?= { save_scope_as = preach_holder }
		# A Wandering Preacher in command (common/traits/ppr_traits.txt): richer sermons, more good events.
		army_commander ?= {
			if = {
				limit = {
					has_trait = ppr_wandering_preacher
					is_commanding_army = yes
				}
				save_scope_as = preach_holy_commander
			}
		}
		# Friendly county: 40% positive event, 12% risk event; hostile county: 30% hostile-land event.
		# One event per county every 5 years, and one per preacher every 6 months.
		scope:barterer = { ppr_preaching_roll_event_effect = yes }
		# Feed message icons: the commander of the missionaries (or the preacher) and the county's banner.
		if = {
			limit = { exists = army_commander }
			army_commander = { save_scope_as = ppr_sermon_commander }
		}
		else = {
			scope:barterer = { save_scope_as = ppr_sermon_commander }
		}
		# What the sermon earns goes into the missionaries' own purse (army variables
		# ppr_gold_carried / ppr_piety_carried). The engine's barter purse is NOT used: it is never
		# handed over for a landless adventurer, and script cannot empty it (its limit is raised instead:
		# hidden-effect modifier ppr_missionary_carts_modifier). No purse limit: the provisions carried already limit what a mission can earn.
		save_scope_as = ppr_purse_army
		if = {
			limit = { NOT = { has_variable = ppr_gold_carried } }
			set_variable = { name = ppr_gold_carried value = 0 }
		}
		if = {
			limit = { NOT = { has_variable = ppr_piety_carried } }
			set_variable = { name = ppr_piety_carried value = 0 }
		}
		if = {
			limit = { scope:barterer = { has_variable = ppr_preach_for_piety } }
			# Preaching for the faith: piety, no gold. In a land of hostile faith, only half as much (as the
			# alms): few listen, but each soul reached counts. The risk lies in the hostile-land events.
			save_scope_value_as = {
				name = ppr_sermon_gain
				value = {
					value = scope:value
					multiply = ppr_preaching_piety_per_loot_value
					if = {
						limit = { ppr_preaching_county_hostile_trigger = yes }
						multiply = ppr_preaching_hostile_piety_mult_value
					}
					multiply = {
						value = 1
						add = ppr_wandering_preacher_bonus_value
					}
					floor = yes
				}
			}
			change_variable = { name = ppr_piety_carried add = scope:ppr_sermon_gain }
			scope:barterer = {
				send_interface_message = {
					type = ppr_msg_sermon_done
					title = ppr_msg_sermon_done_title
					desc = ppr_msg_sermon_done_piety_desc
					left_icon = scope:ppr_sermon_commander
					right_icon = scope:county
				}
			}
		}
		else = {
			# Preaching for alms: gold, no piety. The hostile faithful still give, but half as much.
			save_scope_value_as = {
				name = ppr_sermon_gain
				value = {
					value = scope:value
					multiply = ppr_preaching_gold_per_loot_value
					if = {
						limit = { ppr_preaching_county_hostile_trigger = yes }
						multiply = ppr_preaching_hostile_alms_mult_value
					}
					multiply = {
						value = 1
						add = ppr_wandering_preacher_bonus_value
					}
					floor = yes
				}
			}
			change_variable = { name = ppr_gold_carried add = scope:ppr_sermon_gain }
			scope:barterer = {
				send_interface_message = {
					type = ppr_msg_sermon_done
					title = ppr_msg_sermon_done_title
					desc = ppr_msg_sermon_done_alms_desc
					left_icon = scope:ppr_sermon_commander
					right_icon = scope:county
				}
			}
		}
		# Purse shown in the army window, and the "Disband All" warning (gui/hud.gui)
		scope:barterer = { ppr_refresh_missionary_purse_effect = yes }
		# The commander's Wandering Preacher trait: experience, or a chance to gain it.
		ppr_wandering_preacher_progress_effect = yes
	}
}
