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

# --- Pilgrims-only camp buildings: quarterly safety net --------------------
# The law's on_revoke already cleans up. This catches anything else
# (inheritance, purpose changed by script, parent building lost...).

quarterly_playable_pulse = {
	on_actions = { ppr_building_maintenance }
}

ppr_building_maintenance = {
	trigger = {
		is_landless_adventurer = yes
		OR = {
			AND = {
				domicile ?= {
					OR = {
						has_domicile_building = ppr_baggage_train_reliquary
						has_domicile_construction = ppr_baggage_train_reliquary
					}
				}
				OR = {
					NOT = { has_realm_law_flag = ppr_unlocks_baggage_train_reliquary }
					domicile = {
						NOT = { has_domicile_building_or_higher = baggage_train_01 }
					}
				}
			}
			AND = {
				domicile ?= {
					OR = {
						has_domicile_building = ppr_barber_tent_hospice
						has_domicile_construction = ppr_barber_tent_hospice
					}
				}
				OR = {
					NOT = { has_realm_law_flag = ppr_unlocks_barber_tent_hospice }
					domicile = {
						NOT = { has_domicile_building_or_higher = barber_tent_01 }
					}
				}
			}
			AND = {
				domicile ?= {
					OR = {
						has_domicile_building = ppr_camp_fire_vespers
						has_domicile_construction = ppr_camp_fire_vespers
					}
				}
				OR = {
					NOT = { has_realm_law_flag = ppr_unlocks_camp_fire_vespers }
					domicile = {
						NOT = { has_domicile_building_or_higher = camp_fire_01 }
					}
				}
			}
			AND = {
				domicile ?= {
					OR = {
						has_domicile_building = ppr_supply_tent_alms_chest
						has_domicile_construction = ppr_supply_tent_alms_chest
					}
				}
				OR = {
					NOT = { has_realm_law_flag = ppr_unlocks_supply_tent_alms_chest }
					domicile = {
						NOT = { has_domicile_building_or_higher = supply_tent_01 }
					}
				}
			}
			AND = {
				domicile ?= {
					OR = {
						has_domicile_building = ppr_camp_perimeter_palmers_watch
						has_domicile_construction = ppr_camp_perimeter_palmers_watch
					}
				}
				OR = {
					NOT = { has_realm_law_flag = ppr_unlocks_camp_perimeter_palmers_watch }
					domicile = {
						NOT = { has_domicile_building_or_higher = camp_perimeter_01 }
					}
				}
			}
		}
	}
	effect = {
		trigger_event = ppr_building_events.0001
	}
}

# --- Camp party: Sermon by the Fire / Tales of Saints ---------------------
# Pulse actions are defined by the activity type itself, so instead of overriding
# activity_camp_party we hook the host's monthly destination on_action.
# Modelled on vanilla apa_camp_party_lecture / apa_camp_party_tales_of_piety.

camp_party_destination_events = {
	on_actions = { ppr_camp_party_pilgrims_pulse }
}

ppr_camp_party_pilgrims_pulse = {
	trigger = {
		has_realm_law = ppr_camp_purpose_pilgrims
		involved_activity ?= {
			has_activity_type = activity_camp_party
			any_attending_character = {
				count >= 2
				is_physically_able_ai_adult = yes
			}
		}
	}
	effect = {
		random = {
			chance = 35
			involved_activity = {
				save_scope_as = ppr_party
				# The preacher: a pious soul among the attendees, or the host.
				random_attending_character = {
					limit = {
						is_physically_able_ai_adult = yes
						OR = {
							has_trait = pilgrim
							has_trait = zealous
							has_trait = theologian
							is_clergy = yes
							learning >= 12
						}
					}
					weight = {
						base = 1
						modifier = {
							add = 2
							has_trait = pilgrim
						}
						modifier = {
							add = 1
							OR = {
								has_trait = zealous
								has_trait = theologian
								is_clergy = yes
							}
						}
					}
					save_scope_as = ppr_preacher
				}
				if = {
					limit = { NOT = { exists = scope:ppr_preacher } }
					root = { save_scope_as = ppr_preacher }
				}
				# The listener: someone sharing the preacher's faith.
				random_attending_character = {
					limit = {
						is_physically_able_ai_adult = yes
						this != scope:ppr_preacher
						faith = scope:ppr_preacher.faith
					}
					weight = {
						base = 1
						modifier = {
							add = 2
							has_trait = zealous
						}
					}
					save_scope_as = ppr_listener
				}
				if = {
					limit = { exists = scope:ppr_listener }
					if = {
						limit = {
							scope:ppr_preacher = { has_trait = pilgrim }
						}
						add_activity_log_entry = {
							key = ppr_camp_party_tales_of_saints
							tags = { pulse_action }
							character = scope:ppr_preacher
							target = scope:ppr_listener

							scope:ppr_party = { activity_special_type_progression_variable = { NUM = 5 } }
							custom_tooltip = camp_party_tt_positive_tiny

							scope:ppr_listener = {
								add_piety = minor_piety_gain
								if = {
									limit = { has_trait = zealous }
									add_stress = minor_stress_loss
								}
							}
							scope:ppr_preacher = {
								add_piety = miniscule_piety_gain
							}
						}
					}
					else = {
						add_activity_log_entry = {
							key = ppr_camp_party_sermon_by_the_fire
							tags = { pulse_action }
							character = scope:ppr_preacher
							target = scope:ppr_listener

							scope:ppr_party = { activity_special_type_progression_variable = { NUM = 5 } }
							custom_tooltip = camp_party_tt_positive_tiny

							scope:ppr_listener = {
								add_piety = minor_piety_gain
								if = {
									limit = { has_trait = zealous }
									add_stress = minor_stress_loss
								}
							}
						}
					}
				}
			}
		}
	}
}
