﻿# Pilgrims and Preachers - Camp Retreat (ppr_activity_camp_retreat)
#
# A Scholars or Pilgrims camp goes on retreat to a monastery (temple holding of the leader's faith)
# or a holy site of that faith, close to where the camp stands. The camp relocates there on arrival.
# Modelled on vanilla camp_party.txt (adventurer camp activity), pilgrimage.txt (travel, conclusion)
# and funeral.txt (predefined multi-phase order).
#
# Phases:  1. ppr_camp_retreat_phase_arrival    (welcome event ppr_camp_retreat.0001, camp moves)
#          2. ppr_camp_retreat_phase_retreat    (4 or 10 weeks; pulse actions + mid events)
#          3. ppr_camp_retreat_phase_conclusion (outcome event ppr_camp_retreat.0009 + log entry)
# Score:   var:activity_special_type_progression on the activity (see ppr_camp_retreat_effects.txt).

ppr_activity_camp_retreat = {
	is_shown = {
		has_ep3_dlc_trigger = yes
		has_government = landless_adventurer_government
		is_adult = yes
		trigger_if = {
			limit = { is_ai = yes }
			ppr_camp_retreat_purpose_trigger = yes
		}
	}

	can_start_showing_failures_only = {
		NOT = { is_activity_type_on_cooldown = ppr_activity_camp_retreat }
		is_available = yes
		custom_tooltip = {
			text = ppr_camp_retreat_purpose_tt
			ppr_camp_retreat_purpose_trigger = yes
		}
		custom_tooltip = {
			text = ppr_camp_retreat_provisions_tt
			domicile ?= { provisions >= ppr_camp_retreat_provisions_needed_value }
		}
	}

	is_valid = {
		scope:host = {
			is_alive = yes
			is_imprisoned = no
			has_government = landless_adventurer_government
			is_incapable = no
		}
	}

	on_invalidated = {
		invalidation_incapable_effect = yes
		invalidation_imprisoned_effect = yes
		invalidation_host_isolated_effect = yes
	}

	on_host_death = {
		activity_try_use_new_host_effect = yes
	}

	###################
	# LOCATION
	###################

	is_single_location = yes

	province_filter = all
	ai_province_filter = same_faith
	max_province_icons = 5

	is_location_valid = {
		# Cheap distance check first (province_filter = all is slow).
		custom_tooltip = {
			text = ppr_camp_retreat_distance_tt
			squared_distance = { target = scope:host.location value < squared_distance_large }
		}
		custom_tooltip = {
			text = ppr_camp_retreat_location_tt
			OR = {
				# A monastery must be fairly close to the camp
				AND = {
					has_holding_type = church_holding
					county.faith ?= scope:host.faith
					squared_distance = { target = scope:host.location value < squared_distance_medium }
				}
				# A holy site is worth a longer road
				barony ?= { is_holy_site_of = scope:host.faith }
			}
		}
	}

	province_description = {
		desc = ppr_activity_camp_retreat_province_desc
		triggered_desc = {
			trigger = {
				barony ?= { is_holy_site_of = scope:host.faith }
			}
			desc = ppr_activity_camp_retreat_province_holy_site
		}
		triggered_desc = {
			trigger = {
				has_holding_type = church_holding
				county.faith ?= scope:host.faith
			}
			desc = ppr_activity_camp_retreat_province_monastery
		}
	}

	province_score = {
		add = ppr_camp_retreat_province_score_value
	}

	###################
	# COSTS
	###################

	cost = {
		gold = {
			add = {
				value = ppr_camp_retreat_base_cost_value
				desc = ppr_camp_retreat_cost_base
			}
		}
	}

	ui_predicted_cost = {
		gold = {
			value = ppr_camp_retreat_base_cost_value
			# Make it a multiple of 5 (rounded up)
			divide = 5
			ceiling = yes
			multiply = 5
		}
	}

	cooldown = { years = 5 }

	max_guest_arrival_delay_time = { weeks = 2 }

	###################
	# PHASES
	###################

	phases = {
		# The camp arrives and is welcomed by the community.
		ppr_camp_retreat_phase_arrival = {
			order = 1
			is_predefined = yes
			location_source = pickable

			ai_will_do = {
				value = 30
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = {
						activity_location = { save_scope_as = location }
					}
					# The camp (domicile) stays put: the host and followers travel as guests and entourage.
					# Moving a domicile while its owner is on a travel plan is untested in vanilla.
					# Welcome event.
					trigger_event = ppr_camp_retreat.0001
					scope:activity = { progress_activity_phase_after = { days = 5 } }
				}
			}
		}

		# The retreat itself: several weeks of prayer or study.
		ppr_camp_retreat_phase_retreat = {
			order = 2
			is_predefined = yes

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					# No passive state, go straight on.
					scope:activity = { progress_activity_phase_after = { days = 1 } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = {
						set_variable = {
							name = ppr_retreat_week
							value = 0
						}
						if = {
							limit = { ppr_camp_retreat_long_trigger = yes }
							progress_activity_phase_after = { weeks = 10 }
						}
						else = {
							progress_activity_phase_after = { weeks = 4 }
						}
					}
				}
			}

			on_weekly_pulse = {
				if = {
					limit = { this = scope:host }
					scope:activity = {
						change_variable = {
							name = ppr_retreat_week
							add = 1
						}
					}
					if = {
						limit = {
							scope:activity = { ppr_camp_retreat_mid_event_week_trigger = yes }
						}
						trigger_event = { on_action = ppr_camp_retreat_mid_events }
						# The Strict Rule wears everyone down.
						if = {
							limit = {
								scope:activity = { ppr_camp_retreat_strict_rule_trigger = yes }
							}
							scope:activity = {
								every_attending_character = {
									add_stress = miniscule_stress_gain
								}
							}
						}
					}
				}
			}
		}

		# Farewell to the community: outcome and conclusion.
		ppr_camp_retreat_phase_conclusion = {
			order = 3
			is_predefined = yes

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { days = 1 } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					# Conclusion event: the outcome is applied through its log entry.
					trigger_event = ppr_camp_retreat.0009
					scope:activity = { progress_activity_phase_after = { days = 3 } }
				}
			}
		}
	}

	###################
	# OPTIONS
	###################

	options = {
		# Rule of Life
		ppr_camp_retreat_option_rule = {
			ppr_camp_retreat_rule_gentle = {
				default = yes

				ai_will_do = {
					value = 100
				}
			}
			ppr_camp_retreat_rule_strict = {
				# Austerity costs piety, not gold, so vows of poverty never block it.
				cost = {
					piety = {
						add = {
							value = minor_piety_value
							desc = ppr_camp_retreat_rule_strict
						}
					}
				}

				ai_will_do = {
					value = 10
					if = {
						limit = { has_trait = zealous }
						add = 40
					}
					if = {
						limit = { has_trait = diligent }
						add = 30
					}
					if = {
						limit = {
							OR = {
								has_trait = lazy
								has_trait = gluttonous
								has_trait = impatient
							}
						}
						add = -50
					}
				}
			}
		}

		# Length
		ppr_camp_retreat_option_length = {
			ppr_camp_retreat_length_short = {
				default = yes

				ai_will_do = {
					value = 100
				}
			}
			ppr_camp_retreat_length_long = {
				is_valid = {
					custom_tooltip = {
						text = ppr_camp_retreat_long_provisions_tt
						domicile ?= { provisions >= ppr_camp_retreat_long_provisions_needed_value }
					}
				}

				on_start = {
					# Extra stores for the long stay.
					scope:host = {
						domicile ?= { change_provisions = minor_provisions_loss }
					}
				}

				ai_will_do = {
					value = 10
					if = {
						limit = { has_trait = patient }
						add = 40
					}
					if = {
						limit = {
							OR = {
								has_trait = zealous
								has_trait = erudite
								has_trait = theologian
							}
						}
						add = 20
					}
					if = {
						limit = { has_trait = impatient }
						add = -50
					}
				}
			}
		}
	}

	###################
	# START
	###################

	on_start = {
		scope:activity ?= {
			#Counter for the retreat score
			activity_setup_special_type_progression_effect = yes
			activity_special_type_progression_variable = { NUM = 15 }

			# Remember the camp purpose at departure, events and rewards branch on it.
			if = {
				limit = {
					scope:host = { has_realm_law = ppr_camp_purpose_pilgrims }
				}
				set_variable = {
					name = ppr_retreat_purpose
					value = flag:pilgrims
				}
			}
			else = {
				set_variable = {
					name = ppr_retreat_purpose
					value = flag:scholars
				}
			}

			add_activity_log_entry = {
				key = ppr_camp_retreat_departed_log
				score = 25
				tags = { departure }
				character = scope:activity.activity_host
			}
		}
		# Provisions for the road and the stay.
		scope:host = {
			domicile ?= { change_provisions = miniscule_provisions_loss }
		}
	}

	on_enter_passive_state = {
		# The host arrives: start right away instead of waiting for the planned date.
		if = {
			limit = { root = scope:host }
			scope:activity = { progress_activity_phase_after = { days = 1 } }
		}
	}

	###################
	# PULSES
	###################

	pulse_actions = {
		entries = {
			ppr_apa_retreat_choir_prayer
			ppr_apa_retreat_scriptorium
			ppr_apa_retreat_vespers_debate
			ppr_apa_retreat_cloister_garden
		}
		chance_of_no_event = 3
	}

	###################
	# AI VALUES
	###################

	# desc is only shown in debug AI watch window
	ai_will_do = {
		value = 30 # Above the AI threshold of 20: an ordinary Scholars or Pilgrims camp retreats now and then

		if = {
			limit = { has_trait = zealous }
			add = 10
		}
		if = {
			limit = {
				OR = {
					has_trait = erudite
					has_trait = theologian
					has_trait = pilgrim
					has_trait = lifestyle_mystic
				}
			}
			add = 10
		}
		if = {
			limit = { stress >= 50 }
			add = 5
		}
		if = {
			limit = {
				OR = {
					has_trait = lazy
					has_trait = cynical
				}
			}
			add = -15
		}

		#Can you actually afford this?
		if = {
			limit = {
				domicile.provisions <= 300
			}
			add = -75
		}

		add = activity_general_ai_will_do_value
	}

	ai_check_interval = 60

	ai_will_select_province = {
		value = scope:score
	}

	###################
	# GUEST HANDLING
	###################

	allow_zero_guest_invites = yes
	open_invite = no
	max_guests = 30

	host_intents = {
		intents = { reduce_stress_intent reflection_intent study_hard_intent }
		default = reduce_stress_intent
	}

	guest_intents = {
		intents = { reduce_stress_intent reflection_intent study_hard_intent }
		default = reduce_stress_intent
	}

	can_be_activity_guest = {
		is_ai = yes
		is_adult = yes
		OR = {
			is_courtier_of = scope:host
			current_travel_plan ?= { travel_plan_owner = scope:host }
		}
	}

	guest_invite_rules = {
		defaults = {
			1 = activity_invite_rule_followers
		}
	}

	travel_entourage_selection = {
		weight = {
			value = standard_travel_entourage_additions
			if = {
				limit = {
					is_courtier_of = scope:owner
					is_available_ai_adult = yes
					location = scope:owner.location
				}
				add = 50
			}
		}

		# Up to how many characters to select for a player
		max = 15

		# Up to how many characters to select for an AI
		ai_max = 6

		invite_rule_order = 1
	}

	guest_join_chance = {
		base = 50
		base_activity_modifier = yes

		# Scripted Modifiers
		activity_guest_shared_ai_accept_modifier = yes

		modifier = {
			is_courtier_of = scope:host
			add = 100
			desc = ACTIVITY_GUEST_ADVENTURER_REASON
		}
		modifier = {
			NOT = { faith = scope:host.faith }
			add = -40
			desc = ppr_camp_retreat_guest_other_faith_reason
		}
	}

	###################
	# GRAPHICS
	###################

	header_background = "gfx/interface/illustrations/event_scenes/temple.dds"

	background = { # At the retreat - Islam
		trigger = {
			scope:host.location = scope:activity.activity_location
			scope:host = { religion = religion:islam_religion }
		}
		texture = "gfx/interface/illustrations/event_scenes/mosque.dds"
		environment = "environment_event_mosque"
		ambience = "event:/SFX/Events/Backgrounds/temple_muslim"
	}
	background = { # At the retreat
		trigger = {
			scope:host.location = scope:activity.activity_location
		}
		texture = "gfx/interface/illustrations/event_scenes/church.dds"
		environment = "environment_event_church"
		ambience = "event:/SFX/Events/Backgrounds/temple_generic"
	}
	background = { # On the road
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_farm.dds"
		environment = "environment_travel_farmlands"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}

	window_characters = {
		guest = {
			camera = camera_body_right

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					every_attending_character = {
						limit = {
							location = scope:activity.activity_location
							this != scope:host
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:activity = { ppr_camp_retreat_scholars_trigger = yes }
					}
					animation = { reading thinking throne_room_writer }
				}
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { prayer chaplain personality_zealous }
				}
			}
		}

		host = {
			camera = camera_body

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					scope:host = {
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:activity = { ppr_camp_retreat_scholars_trigger = yes }
					}
					animation = { reading personality_rational }
				}
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { prayer personality_zealous }
				}
			}
		}

		guest = {
			camera = camera_body_left

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					every_attending_character = {
						limit = {
							location = scope:activity.activity_location
							this != scope:host
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:activity = { ppr_camp_retreat_scholars_trigger = yes }
					}
					animation = { debating interested thinking }
				}
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { prayer worry personality_content }
				}
			}
		}

		### On the road (host not yet at the retreat) ###

		# Pilgrims walk, staff in hand, as pilgrims should
		travel_host = {
			camera = camera_body

			effect = {
				if = {
					limit = {
						NOT = { scope:host.location = scope:activity.activity_location }
						scope:activity = { ppr_camp_retreat_scholars_trigger = no }
					}
					scope:host = { add_to_list = characters }
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = { scope:character.location = { is_sea_province = yes } }
					animation = { survey }
				}
				animation = holding_staff
			}
		}

		guest = {
			camera = camera_body_left

			effect = {
				if = {
					limit = {
						NOT = { scope:host.location = scope:activity.activity_location }
						scope:activity = { ppr_camp_retreat_scholars_trigger = no }
					}
					every_attending_character = {
						limit = {
							this != scope:host
							location = scope:host.location
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = { always = yes }
					animation = { survey_staff holding_staff }
				}
				animation = holding_staff
			}
		}

		guest = {
			camera = camera_body_right

			effect = {
				if = {
					limit = {
						NOT = { scope:host.location = scope:activity.activity_location }
						scope:activity = { ppr_camp_retreat_scholars_trigger = no }
					}
					every_attending_character = {
						limit = {
							this != scope:host
							location = scope:host.location
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = { scope:character = { has_trait = zealous } }
					animation = { holding_staff }
				}
				triggered_animation = {
					trigger = { always = yes }
					animation = { survey_staff worry personality_content }
				}
				animation = survey_staff
			}
		}

		# Scholars ride
		travel_host = {
			camera = camera_activity_horse

			effect = {
				if = {
					limit = {
						NOT = { scope:host.location = scope:activity.activity_location }
						scope:activity = { ppr_camp_retreat_scholars_trigger = yes }
					}
					scope:host = { add_to_list = characters }
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = { scope:character = { has_trait = impatient } }
					animation = { jockey_gallop }
				}
				animation = jockey_walk
			}
		}

		guest = {
			camera = camera_activity_horse

			effect = {
				if = {
					limit = {
						NOT = { scope:host.location = scope:activity.activity_location }
						scope:activity = { ppr_camp_retreat_scholars_trigger = yes }
					}
					every_attending_character = {
						limit = {
							this != scope:host
							location = scope:host.location
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				animation = jockey_walk
			}
		}

		guest = {
			camera = camera_activity_horse

			effect = {
				if = {
					limit = {
						NOT = { scope:host.location = scope:activity.activity_location }
						scope:activity = { ppr_camp_retreat_scholars_trigger = yes }
					}
					every_attending_character = {
						limit = {
							this != scope:host
							location = scope:host.location
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				animation = jockey_walk
			}
		}
	}

	# Recollection bar, like the Carousing bar of the camp party (label in zzz_ppr_activity_loc.txt)
	activity_window_widgets = {
		activity_special_type_progression = "activity_plugin_widgets_summary"
	}
}
