﻿# Pilgrims and Preachers - preaching expeditions: scripted effects
# Root = the Pilgrims adventurer. Barter goods = provisions carried by the missionaries.
# Like raiders and barterers, the missionaries keep what they collect (alms = barter loot, and
# piety, kept on the army) until they are back in the county of their camp.
# IMPORTANT: every real change is made directly; the toasts only SHOW them (show_as_tooltip).
# Amounts are kept in (non-temporary) saved scope values.

# Load provisions from the camp up to what the army can carry. Needs scope:army.
ppr_load_mission_rations_effect = {
	save_scope_value_as = {
		name = ppr_rations_load
		value = ppr_mission_rations_to_load_value
	}
	if = {
		limit = { scope:ppr_rations_load > 0 }
		domicile ?= {
			change_provisions = {
				value = {
					value = scope:ppr_rations_load
					multiply = -1
				}
			}
		}
		add_barter_goods = scope:ppr_rations_load
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = ppr_missionaries_set_out_t
			left_icon = root
			show_as_tooltip = {
				domicile ?= {
					change_provisions = {
						value = {
							value = scope:ppr_rations_load
							multiply = -1
						}
					}
				}
			}
		}
	}
	add_character_flag = ppr_missionaries_supplied
	# Capacity shown in the army window (gui/window_army.gui, ppr_army_provisions_gauge).
	set_variable = {
		name = ppr_rations_cap
		value = ppr_mission_rations_capacity_value
	}
}

# No missionary army left (disbanded), or the purpose was left: carried provisions go back.
ppr_unload_mission_rations_effect = {
	if = {
		limit = { barter_goods > 0 }
		save_scope_value_as = {
			name = ppr_rations_back
			value = barter_goods
		}
		domicile ?= { change_provisions = scope:ppr_rations_back }
		remove_barter_goods = scope:ppr_rations_back
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = ppr_missionaries_provisions_back_t
			left_icon = root
			show_as_tooltip = {
				domicile ?= { change_provisions = scope:ppr_rations_back }
			}
		}
	}
	remove_character_flag ?= ppr_missionaries_supplied
	if = {
		limit = { has_variable = ppr_rations_cap }
		remove_variable = ppr_rations_cap
	}
}

# Pilgrims camp with no missionary army: keep a token provision set aside (ppr_mission_rations_reserve_value) so that the
# missionaries can be raised (the raise button needs barter goods).
ppr_keep_mission_rations_reserve_effect = {
	save_scope_value_as = {
		name = ppr_reserve_missing
		value = {
			value = ppr_mission_rations_reserve_value
			subtract = barter_goods
		}
	}
	save_scope_value_as = {
		name = ppr_reserve_excess
		value = {
			value = barter_goods
			subtract = ppr_mission_rations_reserve_value
		}
	}
	if = {
		limit = {
			exists = domicile
			scope:ppr_reserve_missing >= 1
		}
		domicile = {
			change_provisions = {
				value = {
					value = scope:ppr_reserve_missing
					multiply = -1
				}
			}
		}
		add_barter_goods = scope:ppr_reserve_missing
		# (no toast: only a token provision is set aside)
	}
	else_if = {
		limit = { scope:ppr_reserve_excess >= 1 }
		domicile ?= { change_provisions = scope:ppr_reserve_excess }
		remove_barter_goods = scope:ppr_reserve_excess
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = ppr_missionaries_provisions_back_t
			left_icon = root
			show_as_tooltip = {
				domicile ?= { change_provisions = scope:ppr_reserve_excess }
			}
		}
	}
	remove_character_flag ?= ppr_missionaries_supplied
	if = {
		limit = { has_variable = ppr_rations_cap }
		remove_variable = ppr_rations_cap
	}
}

# Back in the camp's county: the missionaries hand over what their sermons earned (gold in alms
# mode, piety in faith mode, kept on the army sermon after sermon), then take fresh provisions.
# Needs scope:army.
ppr_missionaries_home_effect = {
	save_scope_value_as = {
		name = ppr_gold
		value = {
			value = 0
			if = {
				limit = { scope:army = { has_variable = ppr_gold_carried } }
				add = scope:army.var:ppr_gold_carried
			}
		}
	}
	save_scope_value_as = {
		name = ppr_piety
		value = {
			value = 0
			if = {
				limit = { scope:army = { has_variable = ppr_piety_carried } }
				add = scope:army.var:ppr_piety_carried
			}
		}
	}
	if = {
		limit = {
			OR = {
				scope:ppr_gold > 0
				NOT = { scope:ppr_piety = 0 }
			}
		}
		if = {
			limit = { scope:ppr_gold > 0 }
			add_gold = scope:ppr_gold
		}
		if = {
			limit = { NOT = { scope:ppr_piety = 0 } }
			add_piety = scope:ppr_piety
		}
		send_interface_toast = {
			type = event_toast_effect_good
			title = ppr_missionaries_back_t
			left_icon = root
			show_as_tooltip = {
				if = {
					limit = { scope:ppr_gold > 0 }
					add_gold = scope:ppr_gold
				}
				if = {
					limit = { NOT = { scope:ppr_piety = 0 } }
					add_piety = scope:ppr_piety
				}
			}
		}
	}
	scope:army = {
		remove_variable ?= ppr_gold_carried
		remove_variable ?= ppr_piety_carried
	}
	# The missionaries' purse is empty again (the engine's own barter purse is ignored: it is never
	# handed over for a landless adventurer and cannot be emptied by script).
	ppr_refresh_missionary_purse_effect = yes
	# Refill the provisions for the next journey.
	ppr_load_mission_rations_effect = yes
}

# Root = the Pilgrims adventurer. The county of the camp cannot be preached to (the missionaries do
# not preach to their own hosts): its provinces carry the vanilla recently_bartered_modifier, which
# the engine uses to block barter missions (and raids) there. Refreshed while the camp stays.
ppr_protect_camp_county_effect = {
	domicile ?= {
		domicile_location.county ?= {
			every_county_province = {
				add_province_modifier = {
					modifier = recently_bartered_modifier
					days = 100
				}
			}
		}
	}
}

# Root = the Pilgrims adventurer. The army window cannot read army variables, so the purse of the
# missionaries is mirrored on their owner (ppr_disp_gold, ppr_disp_piety; ppr_disp_carried = both, for the
# warnings), summed over all their
# missionary armies; read by the ppr_missionary_alms / ppr_missionary_piety gauges of gui/window_army.gui), and ppr_alms_on_the_road
# (read by the disband warnings, gui/window_army.gui, gui/hud.gui and gui/window_military.gui) is
# set while any of them carries something.
ppr_refresh_missionary_purse_effect = {
	save_scope_as = ppr_purse_owner
	set_variable = { name = ppr_disp_carried value = 0 }
	# Gold and piety are shown apart (gui/window_army.gui): changing the preaching mode mid-mission
	# leaves an army carrying both.
	set_variable = { name = ppr_disp_gold value = 0 }
	set_variable = { name = ppr_disp_piety value = 0 }
	every_army = {
		limit = { is_barter_army = yes }
		save_scope_as = ppr_purse_army
		scope:ppr_purse_owner = {
			change_variable = { name = ppr_disp_carried add = ppr_alms_carried_value }
			if = {
				limit = { scope:ppr_purse_army = { has_variable = ppr_gold_carried } }
				change_variable = { name = ppr_disp_gold add = scope:ppr_purse_army.var:ppr_gold_carried }
			}
			if = {
				limit = { scope:ppr_purse_army = { has_variable = ppr_piety_carried } }
				change_variable = { name = ppr_disp_piety add = scope:ppr_purse_army.var:ppr_piety_carried }
			}
		}
	}
	if = {
		limit = { var:ppr_disp_carried > 0 }
		set_variable = ppr_alms_on_the_road
	}
	else_if = {
		limit = { has_variable = ppr_alms_on_the_road }
		remove_variable = ppr_alms_on_the_road
	}
}

# Root = the missionary army, scope:barterer = its owner (ppr_on_preaching_completion).
# Like the Pillager (vanilla on_raid_action_completion): a Wandering Preacher in command gains
# experience with each sermon; another commander of the preacher's faith counts the sermons led
# and, from the third, may become one.
ppr_wandering_preacher_progress_effect = {
	army_commander ?= {
		if = {
			limit = {
				is_commanding_army = yes
				has_trait = ppr_wandering_preacher
			}
			add_trait_xp = {
				trait = ppr_wandering_preacher
				value = ppr_wandering_preacher_xp_value
			}
		}
		else_if = {
			limit = {
				is_commanding_army = yes
				is_adult = yes
				faith = scope:barterer.faith
			}
			if = {
				limit = { has_variable = ppr_sermons_led }
				change_variable = { name = ppr_sermons_led add = 1 }
			}
			else = {
				set_variable = { name = ppr_sermons_led value = 1 }
			}
			if = {
				limit = { var:ppr_sermons_led >= 3 }
				random = {
					chance = ppr_wandering_preacher_gain_chance_value
					save_scope_as = ppr_new_wandering_preacher
					add_trait = ppr_wandering_preacher
					remove_variable = ppr_sermons_led
					scope:barterer = {
						send_interface_message = {
							type = ppr_msg_wandering_preacher
							title = ppr_msg_wandering_preacher_title
							desc = ppr_msg_wandering_preacher_desc
							left_icon = scope:ppr_new_wandering_preacher
							show_as_tooltip = {
								scope:ppr_new_wandering_preacher = { add_trait = ppr_wandering_preacher }
							}
						}
					}
				}
			}
		}
	}
}

# Root = a character. Pilgrims camps carry ppr_missionary_carts_modifier, others never do. Called when the
# purpose is passed or left, quarterly (ppr_purpose_maintenance) and whenever missionaries move or pulse.
ppr_ensure_missionary_carts_effect = {
	if = {
		limit = {
			has_realm_law = ppr_camp_purpose_pilgrims
			NOT = { has_character_modifier = ppr_missionary_carts_modifier }
		}
		add_character_modifier = ppr_missionary_carts_modifier
	}
	else_if = {
		limit = {
			NOT = { has_realm_law = ppr_camp_purpose_pilgrims }
			has_character_modifier = ppr_missionary_carts_modifier
		}
		remove_character_modifier = ppr_missionary_carts_modifier
	}
}
