﻿# Pilgrims and Preachers - scripted effects

# Rebuild the global list of heresiarchs that contracts may target.
# Called once a year (yearly_global_pulse) and at game start.
ppr_refresh_heresiarch_list_effect = {
	if = {
		limit = { has_global_variable_list = ppr_heresiarch_list }
		clear_global_variable_list = ppr_heresiarch_list
	}
	every_living_character = {
		limit = {
			has_trait = heresiarch
			is_ai = yes
			is_landed = no
		}
		add_to_global_variable_list = {
			name = ppr_heresiarch_list
			target = this
		}
	}
}

# Recover scope:task_contract for an active contract of type $TYPE$ when an event
# is reached through travel and the saved scope may have been lost.
# Character scope (the contract taker).
ppr_find_contract_effect = {
	if = {
		limit = { NOT = { exists = scope:task_contract } }
		random_character_active_contract = {
			limit = { has_task_contract_type = $TYPE$ }
			save_scope_as = task_contract
		}
	}
}

# Save the usual scopes from the contract's links and variables.
ppr_set_contract_scopes_effect = {
	scope:task_contract.task_contract_taker ?= { save_scope_as = task_contract_taker }
	scope:task_contract.task_contract_employer ?= { save_scope_as = employer }
	scope:task_contract.var:task_contract_destination ?= { save_scope_as = task_contract_destination }
	scope:task_contract.var:task_contract_target ?= { save_scope_as = task_contract_target }
	scope:task_contract.var:ppr_saint ?= { save_scope_as = ppr_saint }
}

# Character scope (any). Create a holy relic of $SAINT$ owned by $OWNER$.
# Mirrors pam_extract_relic_from_saint_effect, minus the relic charge, which the
# engine only lets the ruler of the tomb spend. Each relic type can still only be
# drawn once per tomb (pam_relic_drawn_* variables), which caps the supply.
ppr_grant_saint_relic_effect = {
	$SAINT$ = {
		save_scope_as = relic_creator_saint
		saint_burial_location ?= { save_scope_as = relic_site }
		save_scope_value_as = {
			name = quality
			value = pam_saint_relic_quality_value
		}
	}
	if = {
		limit = { exists = scope:relic_site }
		scope:relic_site = {
			set_variable = {
				name = ppr_relic_taken
				value = yes
				years = 30
			}
		}
		create_appropriate_relic_artifact_from_character_effect = {
			OWNER = $OWNER$
			CREATOR = scope:relic_creator_saint
		}
		if = {
			limit = { exists = scope:newly_created_artifact }
			hidden_effect_new_object = {
				scope:newly_created_artifact = {
					set_variable = {
						name = pam_saint_relic_saint
						value = scope:relic_creator_saint
					}
				}
			}
		}
	}
}

# Heresiarch scope. They recant publicly and return to $EMPLOYER$'s faith.
ppr_heresiarch_recants_effect = {
	remove_trait = heresiarch
	set_character_faith = $EMPLOYER$.faith
	add_piety = minor_piety_loss
	$EMPLOYER$.faith = {
		change_fervor = {
			value = minor_fervor_value
			desc = ppr_fervor_heresiarch_recanted
		}
	}
}

# Heresiarch scope. Publicly discredited: no longer a heresiarch, and shamed.
ppr_heresiarch_discredited_effect = {
	remove_trait = heresiarch
	add_character_modifier = {
		modifier = ppr_discredited_heresiarch_modifier
		years = 10
	}
}

# Taker scope. Relic secured: carry it back to the employer's capital.
# Expects scope:employer and scope:task_contract.
ppr_relic_head_home_effect = {
	scope:employer.capital_province ?= { save_scope_as = ppr_home }
	if = {
		limit = { exists = scope:ppr_home }
		if = {
			limit = { location = scope:ppr_home }
			trigger_event = ppr_contract_events.0003
		}
		else = {
			start_travel_plan = {
				destination = scope:ppr_home
				on_arrival_destinations = all
				on_arrival_event = ppr_contract_events.0003
				on_travel_planner_cancel_event = ppr_contract_events.0009
			}
		}
	}
	else = {
		scope:task_contract = { invalidate_contract = yes }
	}
}

# Taker scope. A sermon went well. Expects scope:task_contract, optional scope:ppr_tenet.
ppr_preach_success_effect = {
	custom_tooltip = ppr_contract_events.0010.progress_tt
	scope:task_contract = {
		increment_variable_effect = { VAR = ppr_score_tally VAL = 1 }
	}
	if = {
		limit = { exists = scope:ppr_tenet }
		pam_tenet_popularity_on_evangelized_tenet_effect = {
			TENET = scope:ppr_tenet
			CHANGE = minor_tenet_popularity_gain
		}
	}
	if = { limit = { has_pam_dlc_trigger = yes } change_spiritual_fulfillment = minor_spiritual_fulfillment_value }
}

# Taker scope. A sermon fell flat.
ppr_preach_failure_effect = {
	custom_tooltip = ppr_contract_events.0010.no_progress_tt
}

# Contract scope (reward effects). The Reliquary Wagon improves religious contract rewards (x1.5).
ppr_set_extra_reward_effect = {
	if = {
		limit = {
			task_contract_taker.domicile ?= { has_domicile_parameter = ppr_camp_improved_religious_contract_rewards }
		}
		save_scope_value_as = { name = extra_reward value = flag:yes }
	}
	else = {
		save_scope_value_as = { name = extra_reward value = flag:no }
	}
}
