﻿@weight_up_ai_select_province_small = 10
@weight_up_ai_select_province_medium = 50
@weight_up_ai_select_province_large = 250
@weight_up_ai_select_province_massive = 500

@funeral_cost_gold_low = tiny_gold_value
@funeral_cost_gold_medium = medium_gold_value
@funeral_cost_gold_high = major_gold_value

activity_funeral = {
	is_shown = {
		highest_held_title_tier > tier_barony
		is_landed = yes
		trigger_if = {
			limit = {
				is_ai = yes
			}
			is_at_war = no
			short_term_gold >= funeral_activity_cost
		}
		has_ce1_dlc_trigger = yes
		#Every other activity must be blocked until an uncrowned AI crowns themselves
		trigger_if = {
			limit = {
				is_ai = yes
			}
			NOT = {
				has_realm_law = uncrowned
			}
		}
	}

	can_start_showing_failures_only = {
		NOT = { is_activity_type_on_cooldown = activity_funeral }
		age >= 12
		is_available = yes
		trigger_if = {
			limit = {
				is_ai = no
			}
			custom_tooltip = {
				text = has_someone_selected_to_bury
				exists = scope:body_to_bury
			}
		}
		trigger_if = {
			limit = {
				is_ai = yes
			}
			has_available_funeral_target = yes
		}
	}

	can_plan = {
		custom_tooltip = {
			text = has_someone_to_bury_tt
			has_available_funeral_target = yes
		}
	}

	can_start = {
		custom_tooltip = {
			text = has_someone_to_bury_tt
			has_available_funeral_target = yes
		}
	}

	is_valid = {
		scope:host = {
			is_alive = yes
			is_imprisoned = no
			is_landed = yes
			NOT = { is_incapable = yes }
		}
		# If no one shows up
		trigger_if = {
			limit = {
				is_current_phase_active = yes
			}
			has_attending_activity_guests = yes
		}
	}
	
	on_host_death = {
		if = { # Transfer hostly duties to the heir if possible
			limit = {
				exists = scope:host.player_heir
				any_attending_character = { this = scope:host.player_heir }
			}
			scope:host.player_heir = { save_scope_as = new_host }
			#Also inherit the travel plan
			if = {
				limit = {
					scope:host.current_travel_plan = {
						any_entourage_character = { this = scope:new_host }
					}
				}
				scope:host.current_travel_plan = { set_travel_plan_owner = scope:new_host }
			}
			scope:activity = { set_activity_host = scope:new_host }
			scope:new_host = {
				primary_title = { save_scope_as = host_title }
				trigger_event = {
					id = funeral.0002
					days = 1
				}
			}
		}
		else = { # Otherwise, tell everyone to go home
			every_attending_character = {
				limit = { is_alive = yes }
				trigger_event = {
					id = funeral.0001
					days = 1 # So you don't get spammed
				}
			}
		}
	}

	###################
	# PARAMETERS
	###################

	is_single_location = yes

	province_filter = realm
	ai_province_filter = capital

	max_route_deviation_mult = 2.0

	is_location_valid = {
		has_holding = yes
	}

	province_description = {
		desc = activity_funeral_province_desc
		# +++ / triple plus good
		# Triple good / +++
		triggered_desc = { # Has a big cool temple of your faith
			trigger = {
				has_holy_building = yes
			}
			desc = activity_funeral_major_religious_building
		}
		triggered_desc = { # Has a tower of silence
			trigger = {
				has_building_or_higher = tower_of_silence_01
			}
			desc = activity_funeral_tos
		}
		triggered_desc = { #Wavel Cathedral
			trigger = {
				has_building_or_higher = wawel_cathedral_01
			}
			desc = activity_funeral_wavel
		}
		# Double good / ++
		triggered_desc = {
			trigger = {
				barony = {
					is_holy_site_of = scope:host.faith
				}
			}
			desc = activity_funeral_holy_site
		}
		# Things your religion values
		triggered_desc = {
			trigger = {
				scope:host.faith = { has_doctrine_parameter = sky_burials_active }
				is_mountainous_trigger = yes
			}
			desc = activity_funeral_mountain
		}
		# Good / +
		triggered_desc = {
			trigger = {
				has_building_or_higher = temple_01
				faith.religion = scope:host.faith.religion
			}
			desc = activity_funeral_temple
		}
	}

	max_province_icons = 5

	province_score = {
		value = 0
		# BUILDINGS
		# Triple good / +++
		if = { # Has a big cool temple of your faith
			limit = {
				has_holy_building = yes
			}
			add = 100
		}
		if = { # Wavel Cathedral
			limit = {
				has_building_or_higher = wawel_cathedral_01
			}
			add = 100 #Also a holy building so this means +200
		}
		if = { # Has a tower of silence
			limit = {
				has_building_or_higher = tower_of_silence_01
			}
			add = 100
		}
		# Double good / ++
		if = {
			limit = {
				barony = {
					is_holy_site_of = scope:host.faith
				}
			}
			add = 50
		}
		# Things your religion values
		if = {
			limit = {
				scope:host.faith = { has_doctrine_parameter = sky_burials_active }
				is_mountainous_trigger = yes
			}
			add = 50
		}
		# Good / +
		if = {
			limit = {
				has_building_or_higher = temple_01
				faith.religion = scope:host.faith.religion
			}
			add = 25
		}
	}

	cooldown = { years = standard_funeral_cooldown_time }

	# desc is only shown in debug AI watch window
	ai_will_do = {
		add = {
			value = 30
			desc = "Base"
		}
		add = {
			value = ai_greed
			multiply = -0.25
			desc = "ai_greed"
		}
		add = {
			value = ai_sociability
			multiply = 0.5
			desc = "ai_sociability"
		}
		if = {
			limit = {
				ai_energy < 0
			}
			add = {
				add = ai_energy
				desc = "ai_energy"
			}
		}
		if = {
			limit = { has_trait = gregarious }
			add = {
				value = 10
				desc = "Is gregarious"
			}
		}
		if = {
			limit = { has_trait = shy }
			add = {
				value = -30
				desc = "Is shy"
			}
		}
		if = {
			limit = { has_trait = inappetetic }
			add = {
				value = -30
				desc = "Is inappetetic"
			}
		}
		if = {
			limit = { has_trait = reclusive }
			add = {
				value = -30
				desc = "Is reclusive"
			}
		}
		if = {
			limit = {
				short_term_gold >= funeral_activity_cost
				short_term_gold >= major_gold_value
			}
			add = {
				value = 30
				desc = "Got gold to burn"
			}
		}
		if = {
			limit = { has_trait = lifestyle_reveler }
			add = {
				value = 20
				desc = "Is Reveler"
			}
		}
		if = {
			limit = {
				has_royal_court = yes 
				has_court_type = court_intrigue
			}
			add = {
				value = 10
				desc = "Intrigue court trait"
			}
		}
		if = {
			limit = { has_trait = comfort_eater }
			add = {
				value = 10
				desc = "Is Comfort Eater"
			}
		}
		if = {
			limit = { has_trait = drunkard }
			add = {
				value = 10
				desc = "Is Drunkard"
			}
		}
		if = {
			limit = { has_trait = gluttonous }
			add = {
				value = 10
				desc = "Is Gluttonous"
			}
		}
		if = {
			limit = { has_trait = hashishiyah }
			add = {
				value = 10
				desc = "Has Munchies"
			}
		}
		# Stress
		if = { # Low Stress
			limit = {
				stress >= lower_than_baseline_stress
				NOT = { has_trait = shy }
			}
			add = {
				value = 10
				desc = "lower_than_baseline_stress"
			}
		}
		if = { # High Stress
			limit = {
				stress >= higher_than_baseline_stress
				NOT = { has_trait = shy }
			}
			add = {
				value = 10
				desc = "higher_than_baseline_stress"
			}
		}
		if = { # Very High Stress
			limit = {
				stress >= high_stress
				NOT = { has_trait = shy }
			}
			add = {
				value = 10
				desc = "high_stress"
			}
		}
		if = {
			limit = {
				culture = {
					has_cultural_tradition = tradition_culinary_art
				}
			}
			add = {
				value = 20
				desc = "Host culture has Culinary Artists Cultural Tradition"
			}
		}
		if = {
			limit = {
				faith = { has_doctrine = tenet_ritual_celebrations }
			}
			add = {
				value = 35
				desc = "Host culture has Ritual Celebrations Cultural Tradition"
			}
		}
		if = {
			limit = {
				faith = { has_doctrine = tenet_hedonistic }
			}
			add = {
				value = 35
				desc = "Host faith has Hedonistic Tenet"
			}
		}
		if = {
			limit = {
				host.faith = {
					has_doctrine = tenet_asceticism
				}
			}
			add = {
				value = -20
				desc = "Host faith has Asceticism tenet"
			}
		}
		if = {
			limit = {
				top_liege != this
				has_vassal_stance = courtly
			}
			add = {
				value = 10
				desc = courtly
			}
		}
		if = {
			limit = { ai_has_warlike_personality = yes }
			add = {
				value = -30
				desc = "Warlike"
			}
		}
		if = {
			limit = {
				ai_has_warlike_personality = yes
				war_chest_gold < war_chest_gold_maximum
			}
			multiply = {
				value = 0.25
				desc = "Warlike"
			}
		}
		# Economic Archetype
		if = { # Too busy booming the economy
			limit = {
				OR = {
					ai_has_economical_boom_personality = yes
					has_character_flag = ai_boom
				}
			}
			add = {
				value = -200
				desc = "Builder"
			}
		}
		if = { # Funerals are good... and pious
			limit = { ai_has_pious_builder_personality = yes }
			add = {
				value = 50
				desc = "Pious Builder"
			}
		}
		# Legitimacy
		if = {
			limit = {
				is_valid_for_legitimacy_change = yes
			}
			if = { # Low Legitimacy
				limit = {
					OR = {
						legitimacy_level = 1
						legitimacy_level = 2
					}
				}
				add = {
					value = 20
					desc = "low_legitimacy"
				}
			}
			if = { # Very Low Legitimacy
				limit = {
					legitimacy_level = 0
				}
				add = {
					value = 30
					desc = "low_legitimacy"
				}
			}
		}
		# Plague
		if = {
        	limit = {
        	    any_held_title = {
					title_tier = county
        	        has_province_with_epidemic = { intensity < apocalyptic }
        	    }
        	    would_follow_social_distancing_value >= -1
        	}
        	add = {
        	    value = -60
        	    desc = "Plague in Domain"
        	}
    	}
    	else_if = {
    	    limit = {
    	        any_realm_county = {
    	            has_province_with_epidemic = { intensity < apocalyptic }
    	        }
    	        would_follow_social_distancing_value >= -1
    	    }
    	    add = {
    	        value = -20
    	        desc = "Plague in Top Realm"
    	    }
    	}
    	if = {
    	    limit = {
    	        any_held_title = {
					title_tier = county
    	            has_province_with_epidemic = { intensity = apocalyptic }
    	        }
    	    }
    	    add = {
    	        value = -200
    	        desc = "Apocalyptic Plague in Domain"
    	    }
    	}
    	else_if = {
    	    limit = {
    	        any_held_title = {
					title_tier = county
    	            has_province_with_epidemic = { intensity = apocalyptic }
    	        }
    	    }
    	    add = {
    	        value = -40
    	        desc = "Apocalyptic Plague in Top Realm"
    	    }
    	}
	}

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

	max_guests = 40

	guest_invite_rules = {
		rules = {
			# Relations.
			2 = activity_invite_rule_rivals_if_appropriate

			# Family.
			3 = activity_invite_rule_extended_family

			# Magnates.
			4 = activity_invite_neighbouring_rulers

			# Misc landless.
			5 = activity_invite_rule_knights

			# MP 
			6 = activity_invite_mp
		}
		defaults = {
			1 = activity_invite_rule_friends
			1 = activity_invite_rule_potential_friends
			2 = activity_invite_rule_lovers
			2 = activity_invite_rule_potential_lovers
			1 = activity_invite_rule_close_family
			1 = activity_invite_rule_liege
			1 = activity_invite_rule_suzerain
			1 = activity_invite_rule_tributaries
			1 = activity_invite_rule_vassals
			1 = activity_invite_rule_confederates
			2 = activity_invite_rule_fellow_vassals
			3 = activity_invite_rule_courtiers
			4 = activity_invite_rule_guests
			1 = activity_invite_spouses
			1 = activity_invite_rule_landless_adventurers_restricted_range
		}
	}

	can_be_activity_guest = {
		age >= 12
		is_healthy = yes
		in_diplomatic_range = scope:host
	}

	host_intents = {
		intents = { mourn_intent murder_attendee_intent woo_attendee_intent befriend_attendee_intent legitimacy_intent }
		default = mourn_intent
	}

	guest_intents = {
		intents = { mourn_intent murder_attendee_intent woo_attendee_intent befriend_attendee_intent legitimacy_intent }
		default = mourn_intent
	}

	guest_join_chance = {
		base = 10
		base_activity_modifier = yes

		# Scripted Modifiers
		activity_guest_shared_ai_accept_modifier = yes 
	}

	travel_entourage_selection = {
		weight = {
			value = standard_travel_entourage_additions
			if = {
				limit = {
					OR = {
						has_trait = gregarious
						has_trait = drunkard
						has_trait = hashishiyah
						has_trait = gluttonous
						has_trait = comfort_eater
					}
				}
				add = 10
			}
		}
		max = 18
		invite_rule_order = 3
	}

	cost = {
		gold = {
			add = {
				value = 0
				desc = funeral_activity_cost_desc
				add = {
					value = funeral_activity_cost
					desc = funeral_activity_cost_desc
				}
				add = {
					value = funeral_activity_cost
					multiply = activity_cost_scale_by_era
					subtract = funeral_activity_cost
					desc = activity_cost_scale_by_era_desc
				}
				if = {
					limit = {
						scope:province ?= {
							OR = {
								has_building_or_higher = holy_site_cathedral_01
								has_building_or_higher = holy_site_mosque_01
								has_building_or_higher = holy_site_pagan_grand_temple_01
								has_building_or_higher = holy_site_indian_grand_temple_01
								has_building_or_higher = holy_site_other_grand_temple_01
								has_building_or_higher = tower_of_silence_01
								# TGP
								has_building_or_higher = holy_site_buddhist_grand_temple_01
								has_building_or_higher = holy_site_japanese_temple_01
								has_building_or_higher = holy_site_chinese_temple_01
								has_building_or_higher = holy_site_se_asia_pagan_temple_01
							}
						}
					}
					add = {
						value = funeral_activity_cost
						multiply = funeral_activity_cost_increase_max
						subtract = funeral_activity_cost
						desc = funeral_cost_increase_great_temple
					}
				}
				if = {
					limit = {
						root.faith ?= { has_doctrine_parameter = mummification_funeral }
					}
					add = {
						value = funeral_activity_cost
						multiply = 2
						subtract = funeral_activity_cost
						desc = funeral_cost_increase_mummification
					}
				}
				if = {
					limit = {
						root.faith ?= { has_doctrine_parameter = cremation_funeral }
					}
					add = {
						value = funeral_activity_cost
						multiply = funeral_activity_cost_discount_max
						subtract = funeral_activity_cost
						desc = funeral_cost_discount_cremation
					}
				}
				if = {
					limit = {
						domicile ?= { has_domicile_parameter = estate_cheaper_funeral_cost }
					}
					subtract = {
						value = 0
						add = {
							value = funeral_activity_cost
							add = {
								value = feast_activity_cost
								multiply = activity_cost_scale_by_era
								subtract = feast_activity_cost
							}
						}
						multiply = estate_cheaper_funeral_cost_value
						desc = estate_cheaper_funeral_cost_desc
					}
				}
				if = {
					limit = {
						confederation ?= { has_cohesion_level_parameter = any_member_activity_cost_reduced }
					}
					multiply = {
						value = 0.9
						desc = CB_COHESION_PARAMETER_DISCOUNT
					}
				}
			}
			if = {
				limit = {
					has_title = title:e_minister_of_revenue
				}
				multiply = 0.85
			}
		}
	}

	ui_predicted_cost = {
		# All costs are balanced on County/Early Era starting point
		gold = {
			add = { # Base Cost, scaled by era
				value = funeral_activity_cost
				multiply = activity_cost_scale_by_era
			}
			add = { # Ceremony 
				value = 0 # Bad Option Cost
				add = 10 # Normal Option Cost
				add = 35 # Good Option Cost
				# Scale the sum of the options by era and tier
				add = {
					value = 45
					multiply = activity_cost_scale_by_tier
				}
				add = {
					value = 45
					multiply = activity_cost_scale_by_era
				}
				# Divide by 3 (num of levels for this option) so we get the average option cost
				divide = 3
			}
			save_temporary_value_as = pre_discount_funeral_cost_ui
			if = {
				limit = {
					dynasty ?= {
						has_dynasty_perk = law_legacy_1
					}
				}
				subtract = {
					value = scope:pre_discount_funeral_cost_ui
					multiply = law_legacy_cost_reduction_mult
				}
			}
			if = {
				limit = {
					confederation ?= { has_cohesion_level_parameter = any_member_activity_cost_reduced }
				}
				multiply = {
					value = 0.9
					desc = CB_COHESION_PARAMETER_DISCOUNT
				}
			}
			
			# Make it a multiple of 5 (rounded up)
			divide = 5
			ceiling = yes 
			multiply = 5
			if = {
				limit = {
					has_title = title:e_minister_of_revenue
				}
				multiply = 0.85
			}
		}
	}

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

	map_entity = {
		trigger = {
			activity_location.culture = {
				OR = {
					has_graphical_iranian_culture_group_trigger = yes
					has_graphical_mena_culture_group_trigger = yes
					has_graphical_steppe_culture_group_trigger = yes
					has_graphical_african_culture_group_trigger = yes
				}
			}
		}
		reference = "building_mena_feast_01_entity"
	}

	map_entity = {
		trigger = {
			activity_location.culture = {
				has_graphical_india_culture_group_trigger = yes
			}
		}
		reference = "building_indian_feast_01_entity"
	}
	
	map_entity = "building_western_feast_01_entity"

	# Travel
	background = { # Bridge
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				is_riverside_province = yes
				geographical_region = world_europe
				OR = {
					terrain = farmlands
					terrain = plains
					terrain = mountains
					terrain = hills
				}
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_bridge.dds"
		environment = "environment_travel_bridge"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}

	background = { #Rice Fields - If we have Paddy fields AND/or terraced hills
	#This goes before other terrain since it can override in many different terrains if built
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				OR = {
					terrain = terraced_hills
					has_building_or_higher = paddy_fields_01
				}
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/tgp_rice_fields.dds"
		environment = "environment_tgp_rice_fields"
		ambience = "event:/DLC/EP4/SFX/Events/Event_Backgrounds/tgp_rice_fields"
	}

	background = { #Farmlands - East Asia
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				culture = { has_graphical_east_asia_culture_group_trigger = yes }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/tgp_farm_asia.dds"
		environment = "environment_tgp_farm_asia"
		ambience = "event:/DLC/EP4/SFX/Events/Event_Backgrounds/tgp_farm_asia"
	}
	
	background = { # Farmland, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_farm.dds"
		environment = "environment_travel_farmlands"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}
	background = { # Farmland, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/farms.dds"
		environment = "environment_event_farms"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}

	background = { #Desert, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_desert_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_desert.dds"
		environment = "environment_travel_desert"
		ambience = "event:/SFX/Events/Backgrounds/desert_day"
	}
	background = { #Desert, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_desert_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/desert.dds"
		environment = "environment_event_bp1_desert"
		ambience = "event:/SFX/Events/Backgrounds/desert_day"
	}

	background = { #forest snowy
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location ?= { hunt_snowy_forest_trigger = yes }
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_hunt_snowy_forest.dds"
		environment = "environment_hunt_snowy_forest"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}

	background = { #forest_pine
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_forest_pine_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/forest_pine.dds"
		environment = "environment_event_forest_pine"
		ambience = "event:/SFX/Events/Backgrounds/coniferous_forest_day"
	}

	background = { #forest
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_forest_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/forest.dds"
		environment = "environment_event_forest"
		ambience = "event:/SFX/Events/Backgrounds/deciduous_forest_day"
	}

	background = { #mountains, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_mountains_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_mountains.dds"
		environment = "environment_travel_mountain_settlement"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}
	background = { #mountains, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_mountains_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/mountains.dds"
		environment = "environment_event_mountains"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}

	background = { #steppe, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_steppe_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_steppe.dds"
		environment = "environment_travel_steppe_settlement"
		ambience = "event:/SFX/Events/Backgrounds/steppe_day"
	}
	background = { #steppe, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_steppe_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/steppe.dds"
		environment = "environment_event_steppe"
		ambience = "event:/SFX/Events/Backgrounds/steppe_day"
	}

	background = { #drylands (formerly desert)
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_drylands_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/drylands.dds"
		environment = "environment_event_desert"
		ambience = "event:/SFX/Events/Backgrounds/desert_day"
	}

	background = { #wetlands
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_wetlands_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_wetlands.dds"
		environment = "environment_event_bp1_wetlands" 
		ambience = "event:/SFX/Events/Backgrounds/deciduous_forest_day"
	}

	background = { #jungle
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_jungle_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_jungle.dds"
		environment = "environment_event_bp1_jungle"
		ambience = "event:/SFX/Events/Backgrounds/deciduous_forest_day"
	}

	background = { #plains, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_plains_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_hills.dds"
		environment = "environment_travel_hills_settlement"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}
	background = { #plains, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_plains_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_plains.dds"
		environment = "environment_event_bp1_plains"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}

	background = { #hills, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_hills_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_hills.dds"
		environment = "environment_travel_hills_settlement"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}
	background = { #hills, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_hills_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_hills.dds"
		environment = "environment_event_bp1_hills"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}

	background = { #sea
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				is_sea_province = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/fp1_ocean.dds"
		environment = "environment_event_fp1_ocean"
		ambience = "event:/DLC/FP1/SFX/Events/event_ocean_longship"
	}
	# End Travel

	background = {
		trigger = {
			exists = scope:activity
			exists = scope:host
			scope:host = {
				faith = {
					has_doctrine_parameter = cremation_funeral
				}
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_bonfire.dds"
		environment = "environment_event_bp1_bonfire"
		ambience = "event:/SFX/Events/Backgrounds/burning_building"
	}
	background = {
		trigger = {
			exists = scope:activity
			exists = scope:host
			scope:host.culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/tgp_chinese_corridor_night.dds"
		environment = "environment_tgp_chinese_corridor_night"
		ambience = "event:/DLC/EP4/SFX/Events/Event_Backgrounds/tgp_chinese_corridor_night"
	}
	background = {
		texture = "gfx/interface/illustrations/event_scenes/fp4_catacombs.dds"
		environment = "environment_ce1_catacombs"
		ambience = "event:/SFX/Events/Backgrounds/FP4/fp4_condemned_village"
	}

	window_characters = {

		host = {
			camera = camera_event_center
			
			effect = {
				if = {
					limit = {
						OR = {
							scope:host.location = scope:activity.activity_location
							scope:host.location = { is_sea_province = yes }
						}
					}
					scope:host = {
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:host.location = { is_sea_province = yes }
					}
					animation = survey
				}
				triggered_animation = {
					trigger = {
						scope:activity.var:body_to_bury = {
							killer ?= scope:host
						}
					}
					animation = eccentric
				}				
				triggered_animation = {
					trigger = {
						scope:character.faith = {
							has_doctrine_parameter = bewailment_funeral
						}
					}
					animation = { wailing grief }
				}
				triggered_animation = {
					trigger = {
						scope:character.faith = {
							NOT = { has_doctrine_parameter = bewailment_funeral }
						}
					}
					animation = { grief sadness }
				}

				#Fallback
				animation = grief
			}
		}
		
		travel_host = {
			camera = camera_event_horse_left
			
			effect = {
				if = {
					limit = {
						NOR = {
							scope:host.location = scope:activity.activity_location
							scope:host.location = { is_sea_province = 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_event_left
			
			effect = {
				every_attending_character = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					add_to_list = characters
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:character.faith = {
							has_doctrine_parameter = bewailment_funeral
						}
					}
					animation = { sadness }
				}
				triggered_animation = {
					trigger = {
						scope:character.faith = {
							NOT = { has_doctrine_parameter = bewailment_funeral }
						}
					}
					animation = { wailing }
				}
				#Fallback
				animation = grief
			}
		}

		guest = {
			camera = camera_event_center
			
			effect = {
				every_attending_character = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					add_to_list = characters
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:character.faith = {
							has_doctrine_parameter = bewailment_funeral
						}
					}
					animation = { stress crying }
				}
				triggered_animation = {
					trigger = {
						scope:character.faith = {
							NOT = { has_doctrine_parameter = bewailment_funeral }
						}
					}
					animation = { stress crying }
				}
				#Fallback
				animation = grief
			}
		}
		
		body = {
			camera = camera_event_center
			
			effect = {
				if = {
					limit = {
						exists = scope:activity.var:body_to_bury
					}
					scope:activity.var:body_to_bury = {
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				animation = dead
			}
		}
	}

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

	options = {
		# How much do you spend on pomp and fancy stuff at the funeral?

		# How much do you spend on the religious ceremony?
		funeral_option_ceremony = {
			funeral_ceremony_bad = {
				is_shown = { has_dlc_feature = advanced_activities }

				ai_will_do = {
					value = 0
					# Financial preference.
					add = activity_option_likes_cheap_expense_value
					# Quantity preference.
					add = activity_option_food_prefers_small_volume_value
				}
			}
			funeral_ceremony_normal = {
				default = yes
	
				cost = {
					gold = {
						add = {
							value = 0
							desc = funeral_ceremony_normal
							add = {
								value = 10
								desc = funeral_ceremony_normal
							}
							add = {
								value = 10
								multiply = activity_cost_scale_by_tier
								subtract = 10
								desc = activity_cost_scale_by_tier_desc
							}
							add = {
								value = 10
								multiply = activity_cost_scale_by_era
								subtract = 10
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									dynasty ?= {
										has_dynasty_perk = law_legacy_1
									}
								}
								subtract = {
									value = 10
									add = {
										value = 10
										multiply = activity_cost_scale_by_tier
										subtract = 10
									}
									add = {
										value = 10
										multiply = activity_cost_scale_by_era
										subtract = 10
									}
									multiply = law_legacy_cost_reduction_mult
									desc = law_legacy_1_name
								}
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					# Financial preference.
					add = activity_option_likes_middling_expense_value
					# Quantity preference.
					## We just use slightly reduced quantity prefs here.
					add = {
						value = activity_option_prefers_large_volume_value
						multiply = 0.5
					}
				}
			}
			funeral_ceremony_good = {
				is_shown = { has_dlc_feature = advanced_activities }

				cost = {
					gold = {
						add = {
							value = 0
							desc = funeral_ceremony_good
							add = {
								value = 35
								desc = funeral_ceremony_good
							}
							add = {
								value = 35
								multiply = activity_cost_scale_by_tier
								subtract = 35
								desc = activity_cost_scale_by_tier_desc
							}
							add = {
								value = 35
								multiply = activity_cost_scale_by_era
								subtract = 35
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									dynasty ?= {
										has_dynasty_perk = law_legacy_1
									}
								}
								subtract = {
									value = 35
									add = {
										value = 35
										multiply = activity_cost_scale_by_tier
										subtract = 35
									}
									add = {
										value = 35
										multiply = activity_cost_scale_by_era
										subtract = 35
									}
									multiply = law_legacy_cost_reduction_mult
									desc = law_legacy_1_name
								}
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					# Financial preference.
					add = activity_option_likes_heavy_expense_value
					# Quantity preference.
					add = activity_option_prefers_large_volume_value
				}
			}
		}
	}

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

	max_guest_arrival_delay_time = { months = 8 }

	phases = {
		# Mingling and socialising
		funeral_phase_wake = {
			order = 1
			is_predefined = yes

			is_shown = { always = yes }

			on_phase_active = {
				# Sort some stuff on the host.
				if = {
					limit = { this = scope:host }

					scope:activity = { progress_activity_phase_after = { months = 3 } }
				}
			}
			
			on_weekly_pulse = {
				scope:activity.var:body_to_bury = { save_scope_as = deceased }
				root.involved_activity = { save_scope_as = activity }
				trigger_event = { on_action = funeral_wake_pulse }
			}
		}

		# The funeral ceremony itself
		funeral_phase_ceremony = {
			order = 2
			is_predefined = yes

			on_enter_phase = {
				if = {
					limit = { this = scope:host }

					# No passive state for the toast, just jump right into it
					scope:activity = { progress_activity_phase_after = { days = 5 } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { save_scope_as = activity }
					trigger_event = funeral.0100
					scope:activity = { progress_activity_phase_after = { weeks = 1 } }
				}
			}
		}
	}

	ai_check_interval_by_tier = {
		barony = 0
		county = 60
		duchy = 60
		kingdom = 60
		empire = 60
		hegemony = 60
	}

	ai_will_select_province = {
		# Base
		value = 0
		# BUILDINGS
		# Triple good / +++
		if = { # Has a big cool temple of your faith
			limit = {
				barony = {
					is_holy_site_of = scope:host.faith
				}
				has_holy_building = yes
			}
			add = {
				value = @weight_up_ai_select_province_large
				desc = activity_funeral_major_religious_building
			}
		}
		if = { # Has a big cool temple of your faith
			limit = {
				has_building_or_higher = wawel_cathedral_01
			}
			add = {
				value = @weight_up_ai_select_province_large
				desc = activity_funeral_wavel
			}
		}
		if = { # Has a tower of silence
			limit = {
				has_building_or_higher = tower_of_silence_01
			}
			add = {
				value = @weight_up_ai_select_province_large
				desc = activity_funeral_tos
			}
		}
		# Double good / ++
		if = {
			limit = {
				barony = {
					is_holy_site_of = scope:host.faith
				}
			}
			add = {
				value = @weight_up_ai_select_province_medium
				desc = activity_funeral_holy_site
			}
		}
		# Things your religion values
		if = {
			limit = {
				scope:host.faith = { has_doctrine_parameter = sky_burials_active }
				is_mountainous_trigger = yes
			}
			add = {
				value = @weight_up_ai_select_province_medium
				desc = activity_funeral_mountain
			}
		}
		# Good / +
		if = {
			limit = {
				has_building_or_higher = temple_01
				faith.religion = scope:host.faith.religion
			}
			add = {
				value = @weight_up_ai_select_province_small
				desc = activity_funeral_temple
			}
		}
	}

	###################
	# ACTIVITY-SPECIFIC PULSES
	###################
	
	pulse_actions = {
		entries = {
			spouse_skill_diplomacy_improves spouse_gives_host_prestige spouse_impresses_clergy spouse_gives_hook
			relation_gains_friend children_bonding hook_trusting_guest guest_tells_fantastic_joke
			excellent_food lauded_food guest_impressed_by_court_artifact friend_lauds_efforts courtly_vassals_appeased impressed_courtly_vassal
			guest_of_honor_praised
			guest_chokes_on_bone guest_brawl spouse_insults_host ridiculed_food
			host_gains_diplo_xp host_gains_steward_xp host_gains_intrigue_xp
			good_kitty good_puppy antiquarian_admires_artifact jester_entertained_guests apa_executioner
		}
		chance_of_no_event = 5
	}
	
	on_start = {
		if = {
			limit = {
				scope:host = {
					is_ai = yes
				}
			}
			scope:host = {
				funeral_add_funeral_targets_to_list = {
                    LIST_NAME = funeral_activity_targets
                }
				random_in_list = {
					variable = funeral_activity_targets
					save_scope_as = body_to_bury
				}
				clear_variable_list = funeral_activity_targets
			}
			set_variable = {
				name = body_to_bury
				value = scope:body_to_bury
			}
		}
		random = {
			chance = 5
			set_variable = {
				name = incorrupt_body
				value = yes
				years = 5
			}
		}
		scope:activity.var:body_to_bury = {
			set_dead_character_variable = {
				name = body_has_been_disposed
				value = yes
				years = 5
			}
		}
	}
	
	on_enter_passive_state = {
		trigger_event = funeral.0010
	}

	on_enter_active_state = {
		surveyor_no_stone_unturned_perk_effect = yes
	}
	# Effects run when the activity completes after its last phase
	# root = character in this phase
	# scope:activity = the activity
	# scope:host = host of the activity
	on_complete = {
		if = {
			limit = {
				is_ai = no
				promoted_legend ?= {
					legend_protagonist = scope:activity.var:body_to_bury
				}
			}
			add_achievement_global_variable_effect = {
				VARIABLE = ce1_f_achievement_unlocked
				VALUE = yes
			}
		}
		scope:host = {
			if = {
				limit = {
					has_character_flag = cancelled_coronation
				}
				remove_character_flag = cancelled_coronation
			}	
		}
	}

	activity_planner_widgets = {
		funeral_deceased_selection_button = "activity_options_plugins"
		funeral_deceased_selection_widget = "activity_plugin_center"
	}

	activity_window_widgets = {
		funeral_deceased_portrait = "activity_top_layer_event_widgets"
		funeral_deceased_portrait = "activity_top_layer_idle_widgets"
	}
}
