########################
# Rites in Local Shrine
########################
namespace = local_rites
#shrine_location = Where we perform the rites (AKA the destination)

scripted_trigger has_two_local_shrine_options_trigger = {
	# Stops after we've found 2 good places for a sacrifice (don't want to overload the player).
	calc_true_if = {
		amount >= 2
		exists = scope:hills_location
		exists = scope:mountains_location
		exists = scope:temple_holding_location
		exists = scope:forest_location
		exists = scope:point_of_interest_location
		exists = scope:mount_tai_location
	}
}

scripted_effect local_shrine_at_location_effect = {
	set_variable = {
		name = shrine_location
		value = scope:$SHRINE$_location
	}
	scope:$SHRINE$_location = { #to show the location name in the difficulty custom tooltip
		save_scope_as = tooltip_loc
	}
	start_travel_plan = {
		destination = var:shrine_location
		on_travel_planner_cancel_event = local_rites.10
		on_arrival_event = local_rites.2
		on_arrival_destinations = all_but_last
	}
	add_character_modifier = {
		modifier = visit_local_shrine_modifier
		days = local_shrine_duration
	}
	add_character_flag = {
		flag = local_shrine_rite
		years = 1 #This is a fallback, it is also cleared at location
	}
}

# If the player exits the travel planning UI, this event fires reseting the decision cooldown and removing variables
local_rites.10 = {
	hidden = yes
	trigger = {
		OR = {
			exists = var:shrine_location
			has_character_flag = local_shrine_rite
		}
	}
	immediate = {
		current_travel_plan ?= { cancel_travel_plan = yes }
		remove_decision_cooldown = visit_local_shrine_decision
		remove_variable = shrine_location
		remove_character_flag = local_shrine_rite
	}
}

local_rites.1 = {
	type = character_event
	title = local_rites.1.t
	desc = local_rites.1.desc
	theme = faith

	override_background = {
		reference = throne_room
	}

	left_portrait = {
		character = root
		animation = personality_rational
	}

	immediate = {
		# Stop characters from planning multiple activities at once.
		add_character_flag = {
			flag = planning_an_activity
			days = 30
		}

		capital_barony.title_province = {
			save_scope_as = capital_location
		}

		# Pick out possible locations
		# First any Special Building Shrines
		# If I am the Emperor of China I go to Mount Tai
		if = {
			limit = {
				#I am the emperor
				has_title = title:h_china
				#My dynasty did not perform this sacrifice before
				dynasty = {
					NOT = { has_variable = fengshan_performed }
				}
				#The Cycle is stable
				OR = {
					situation:dynastic_cycle = {
						situation_current_phase = situation_dynastic_cycle_phase_stability_expansion
					}
					situation:dynastic_cycle = {
						situation_current_phase = situation_dynastic_cycle_phase_stability_advancement
					}
				}
				title:b_xuchang = {
					this != root.capital_barony
					holder.top_liege = root
				}
			}
			title:b_xuchang = {
				title_province = { save_scope_as = mount_tai_location }
			}
		}
		# Everyone else
		if = {
			limit = {
				has_two_local_shrine_options_trigger = no
			}
			random_sub_realm_barony = {
				limit = {
					this != root.capital_barony
					title_province = {
						has_building_with_flag = travel_point_of_interest_religious
					}
				}
				title_province = { save_scope_as = point_of_interest_location }
			}
		}
		#Now we look for Mountains
		if = {
			limit = {
				has_two_local_shrine_options_trigger = no
			}
			random_sub_realm_barony = {
				limit = {
					this != root.capital_barony
					trigger_if = {
						limit = { exists = scope:point_of_interest_location }
						title_province != scope:point_of_interest_location
					}
					title_province = {
						OR = {
							terrain = mountains
							terrain = desert_mountains
						}
					}
				}
				title_province = { save_scope_as = mountains_location }
			}
		}
		#Now we look for any Temple Holding
		if = {
			limit = {
				has_two_local_shrine_options_trigger = no
			}
			random_sub_realm_barony = {
				limit = {
					this != root.capital_barony
					trigger_if = {
						limit = { exists = scope:point_of_interest_location }
						title_province != scope:point_of_interest_location
					}
					title_province = {
						has_holding_type = church_holding
					}
				}
				title_province = { save_scope_as = temple_holding_location }
			}
		}
		#Now we look for Hills
		if = {
			limit = {
				has_two_local_shrine_options_trigger = no
			}
			random_sub_realm_barony = {
				limit = {
					this != root.capital_barony
					trigger_if = {
						limit = { exists = scope:point_of_interest_location }
						title_province != scope:point_of_interest_location
					}
					title_province = {
						OR = {
							terrain = hills
							terrain = terraced_hills
						}
					}
				}
				title_province = { save_scope_as = hills_location }
			}
		}
		#Any Forests?
		if = {
			limit = {
				has_two_local_shrine_options_trigger = no
			}
			random_sub_realm_barony = {
				limit = {
					this != root.capital_barony
					trigger_if = {
						limit = { exists = scope:point_of_interest_location }
						title_province != scope:point_of_interest_location
					}
					title_province = {
						OR = {
							terrain = forest
							terrain = taiga
						}
					}
				}
				title_province = { save_scope_as = forest_location }
			}
		}
		#If we found nothing we look to the top realm
		if = {
			limit = {
				NOT = { exists = scope:point_of_interest_location } #Because this is the one we try to save.
				has_two_local_shrine_options_trigger = no
			}
			top_liege = {
				random_sub_realm_barony = {
					limit = {
						this != root.capital_barony
						this.kingdom = root.capital_barony.kingdom #If possible we should keep close to where we started
						title_province = {
							has_building_with_flag = travel_point_of_interest_religious
						}
					}
					alternative_limit = {
						this != root.capital_barony
						title_province = {
							has_building_with_flag = travel_point_of_interest_religious
						}
					}
					title_province = { save_scope_as = point_of_interest_location }
				}
			}
		}
		#We also try this a second time for landless vassals.
		if = {
			limit = {
				has_two_local_shrine_options_trigger = no
				NOT = { exists = scope:point_of_interest_location }
			}
			top_liege = {
				random_sub_realm_barony = {
					limit = {
						this != root.capital_barony
						this.kingdom = root.capital_barony.kingdom #If possible we should keep close to where we started
						title_province = {
							has_building_with_flag = travel_point_of_interest_religious
						}
					}
					alternative_limit = {
						this != root.capital_barony
						title_province = {
							has_building_with_flag = travel_point_of_interest_religious
						}
					}
					title_province = { save_scope_as = point_of_interest_location }
				}
			}
		}
		#And last but not least we try to save one for adventurers
		if = {
			limit = {
				has_two_local_shrine_options_trigger = no
				NOT = { exists = scope:point_of_interest_location }
				exists = location.county
			}
			location.county.holder.top_liege = {
				random_sub_realm_barony = {
					limit = {
						this != root.capital_barony
						this.kingdom = root.capital_barony.kingdom #If possible we should keep close to where we started
						title_province = {
							has_building_with_flag = travel_point_of_interest_religious
						}
					}
					alternative_limit = {
						this != root.capital_barony
						title_province = {
							has_building_with_flag = travel_point_of_interest_religious
						}
					}
					title_province = { save_scope_as = point_of_interest_location }
				}
			}
		}
	}

	# Option 1: Rituals at home - Low Impact
	option = { #This option does not set the flag for being unavailable
		name = local_rites.1.home
		set_variable = {
			name = shrine_location
			value = scope:capital_location
		}
		add_character_modifier = {
			modifier = visit_local_shrine_modifier
			days = local_shrine_duration
		}
		scope:capital_location = { #to show the location name in the difficulty custom tooltip
			save_scope_as = tooltip_loc			
		}
		custom_tooltip = local_rites.1.home.tt
		trigger_event = local_rites.3 #When you sacrifice at home this is instant.
	}

	# I am the Emperor Headed for Mt Tai
	option = {
		trigger = {
			exists = scope:mount_tai_location
			scope:mount_tai_location.barony = title:b_xuchang
		}
		name = local_rites.1.mount_tai
		local_shrine_at_location_effect = { SHRINE = mount_tai }
		custom_tooltip = local_rites.1.fengshan.tt
	}
	# Visit Famous Shrine - High Impact, High Offering Expectation
	option = {
		trigger = { exists = scope:point_of_interest_location }
		name = local_rites.1.major_shrine
		local_shrine_at_location_effect = { SHRINE = point_of_interest }
		custom_tooltip = local_rites.1.major_shrine.tt
	}

	# Shrine in the hills. Medium Impact
	option = {
		trigger = { exists = scope:hills_location }
		name = local_rites.1.hills
		local_shrine_at_location_effect = { SHRINE = hills }
	}

	# Shrine in the forest. Medium Impact
	option = {
		trigger = { exists = scope:forest_location }
		name = local_rites.1.forest
		local_shrine_at_location_effect = { SHRINE = forest }
	}

	# Shrine in the mountains. Medium Impact
	option = {
		trigger = { exists = scope:mountains_location }
		name = local_rites.1.mountains
		local_shrine_at_location_effect = { SHRINE = mountains }
	}

	# Shrine in the temple holding - High Impact, High Offerings.
	option = {
		trigger = { exists = scope:temple_holding_location }
		name = local_rites.1.temple_holding
		local_shrine_at_location_effect = { SHRINE = temple_holding }
		custom_tooltip = local_rites.1.major_shrine.tt
	}

	# Option 3: Opt-out
	option = {
		name = local_shrine.optout
		flavor = local_shrine.optout.flavor

		ai_chance = {
			base = 0
		}

		remove_decision_cooldown = visit_local_shrine_decision
	}

	after = {
		remove_character_flag = planning_an_activity
		trigger_event = local_rites.5
	}
}

local_rites.5 = {
	hidden = yes
	immediate = {
		hidden_effect = {
			random_courtier = { #We want to bring a priest if we can, if not we still want at least one character.
				limit = {
					is_adult = yes
					is_clergy = yes
					is_diarch = no
					is_travelling = no
				}
				alternative_limit = {
					is_adult = yes
					is_diarch = no
					is_travelling = no
				}
				save_scope_as = companion
			}
			root.current_travel_plan ?= {
				add_companion = scope:companion
			}
		}
	}
}

local_rites.2 = {
	hidden = yes
	immediate = {
		send_interface_toast = {
			title = local_rites.2.toast
			left_icon = root
			current_travel_plan = {
				delay_travel_plan = { days = local_shrine_duration }
			}
		}
		trigger_event = {
			id = local_rites.3
			days = local_shrine_duration
		}
	}
}

local_rites.3 = { #Destination - Making a Sacrifice
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					has_title = title:h_china
					var:shrine_location.barony = title:b_xuchang
				}
				desc = local_rites.3.t.fengshan #Fang Shan
			}
			triggered_desc = {
				trigger = {
					OR = {
						OR = {
							faith = { is_in_family = rf_sinitic }
							faith = faith:pundarika
							faith = faith:mantrayana
							faith = faith:dhyana
							faith = faith:sukhavati
							faith = faith:avatamsaka
							faith = faith:maitreya
							faith = faith:vinaya
							faith = faith:yogacara
							faith = faith:acharya
						}
					}
				}
				desc = local_rites.3.t #Sacrificing to Heaven
			}
			desc = local_rites.3.fallback.t #More Generic Version
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:at_home = yes }
				desc = local_rites.3.desc_at_home
			}
			triggered_desc = {
				trigger = {
					has_title = title:h_china
					var:shrine_location.barony = title:b_xuchang
				}
				desc = local_rites.3.desc_mt_tai_huangdi
			}
			triggered_desc = {
				trigger = {
					var:shrine_location = {
						has_building_with_flag = travel_point_of_interest_religious
					}
				}
				desc = local_rites.3.desc_important_shrine
			}
			triggered_desc = {
				trigger = {
					var:shrine_location = {
						has_holding_type = church_holding
					}
				}
				desc = local_rites.3.desc_holding_shrine
			}
			triggered_desc = {
				trigger = {
					var:shrine_location = {
						OR = {
							terrain = mountains
							terrain = desert_mountains
						}
					}
				}
				desc = local_rites.3.desc_mountain_shrine
			}
			triggered_desc = {
				trigger = {
					var:shrine_location = {
						OR = {
							terrain = forest
							terrain = taiga
						}
				}
				}
				desc = local_rites.3.desc_forest_shrine
			}
			triggered_desc = {
				trigger = {
					var:shrine_location = {
						terrain = hills
					}
				}
				desc = local_rites.3.desc_hills_shrine
			}
		}
		triggered_desc = {
			trigger = {
				has_trait = cynical
			}
			desc = local_rites.3.desc_cynical
		}
	}
	theme = faith
	override_background = { #Fengshan
		trigger = {
			has_title = title:h_china
			var:shrine_location.barony = title:b_xuchang
		}
		reference = mpo_steppe_evening
	}
	override_background = {
		trigger = {	scope:at_home = yes }
		reference = tgp_temple_asia
	}
	override_background = {
		trigger = {
			scope:at_home = no
			scope:shrine_location = {
				OR = {
					has_building_with_flag = travel_point_of_interest_religious
					has_holding_type = church_holding
				}
			}
		}
		reference = tgp_holysite_asia
	}
	override_background = {
		trigger = {
			scope:at_home = no
			scope:shrine_location = {
				NOR = {
					has_building_with_flag = travel_point_of_interest_religious
					has_holding_type = church_holding
				}
			}
		}
		reference = wilderness_scope
	}
	left_portrait = {
		character = root
		animation = personality_zealous
	}
	right_portrait = {
		character = scope:companion
		animation = pondering
	}

	immediate = {

		if = { #First we try to get a travel companion
			limit = {
				current_travel_plan ?= {
					any_entourage_character = {
						NOT = { this = root }
						is_adult = yes
					}
				}
			}
			current_travel_plan ?= {
				random_entourage_character = {
					limit = {
						NOT = { this = root }
						is_clergy = yes
						is_adult = yes
					}
					alternative_limit = {
						NOT = { this = root }
						is_adult = yes
					}
					save_scope_as = companion
				}
			}
		}
		else_if = { #If you are travelling without adult companions we get a courtier/follower.
			limit = {
				any_courtier = {
					is_adult = yes
				}
			}
			random_courtier = {
				limit = {
					is_adult = yes
					is_clergy = yes
				}
				alternative_limit = {
					is_adult = yes
				}
				save_scope_as = companion
			}
		}
		else = { #Your court is somehow also empty. If we get here it is going to be a bit weird. But we shouldn't.
			random_pool_character = {
				province = root.location
				limit = {
					is_adult = yes
				}
				save_scope_as = companion
			}
		}

		if = {
			limit = {
				NOT = {
					exists = var:shrine_location
				}
			}
			set_variable = {
				name = shrine_location
				value = root.location
			}
		}
		var:shrine_location = {
			save_scope_as = shrine_location
			save_scope_as = background_wilderness_scope
		}

		# Are we at home?
		if = {
			limit = {
				exists = capital_province
				scope:shrine_location = root.capital_province
			}
			save_scope_value_as = {
				name = at_home
				value = yes
			}
		}
		else = {
			save_scope_value_as = {
				name = at_home
				value = no
			}
		}

		#Are we at Mt Tai? Am I the Emperor?
		if = {
			limit = {
				has_title = title:h_china
				var:shrine_location.barony = title:b_xuchang
			}
			hidden_effect = {
				dynasty = { set_variable = fengshan_performed }
			}
		}
	}

	option = {
		name = fengshan_option
		trigger = {
			has_title = title:h_china
			var:shrine_location.barony = title:b_xuchang
		}
		add_treasury = {
			value = major_treasury_value
			multiply = -1
		}
		add_prestige = massive_prestige_loss
		add_legitimacy = massive_legitimacy_gain
		add_piety = major_piety_gain
	}
	option = {
		name = local_rites.3.pay_gold
		trigger = {
			scope:at_home = no
			NOR = {
				has_title = title:h_china
				var:shrine_location.barony = title:b_xuchang
			}
		}
		#Pay Gold - Get Piety
		switch = {
			trigger = exists
			scope:hills_location = {
				remove_short_term_gold = minor_gold_value
				add_piety = medium_piety_gain
			}
			scope:mountains_location = {
				remove_short_term_gold = minor_gold_value
				add_piety = medium_piety_gain
			}
			scope:temple_holding_location = {
				remove_short_term_gold = medium_gold_value
				add_piety = major_piety_gain
			}
			scope:forest_location = {
				remove_short_term_gold = minor_gold_value
				add_piety = medium_piety_gain
			}
			scope:shrine_location = {
				remove_short_term_gold = medium_gold_value
				add_piety = major_piety_gain
			}
		}
	}
	option = {
		name = local_rites.3.pay_prestige
		trigger = {
			scope:at_home = no
			NOR = {
				has_title = title:h_china
				var:shrine_location.barony = title:b_xuchang
			}
		}
		#Pay Prestige - Get Piety
		switch = {
			trigger = exists
			scope:hills_location = {
				add_prestige = minor_prestige_loss
				add_piety = medium_piety_gain
			}
			scope:mountains_location = {
				add_prestige = minor_prestige_loss
				add_piety = medium_piety_gain
			}
			scope:temple_holding_location = {
				add_prestige = medium_prestige_loss
				add_piety = major_piety_gain
			}
			scope:forest_location = {
				add_prestige = minor_prestige_loss
				add_piety = medium_piety_gain
			}
			scope:shrine_location = {
				add_prestige = medium_prestige_loss
				add_piety = major_piety_gain
			}
		}
	}
	option = {
		name = local_rites.3.pay_at_home #So be it
		trigger = {
			scope:at_home = yes
		}
		#Pay A little Gold - Get Piety
		remove_short_term_gold = miniscule_gold_value
		add_piety = minor_piety_gain
		remove_character_flag = local_shrine_rite
	}

	after = {
		stress_impact = { #Not the main purpose of the trip but the ritual comes with some stress relief
			base = minor_stress_impact_loss
			zealous = minor_stress_impact_loss
			cynical = minor_stress_impact_gain
		}

		# Run the clean-up event.
		if = {
			limit = {
				scope:at_home = no
			}
			trigger_event = {
				id = local_rites.1000
				days = 30
			}
		}
		else = {
			remove_variable = shrine_location
		}
	}
}

local_rites.1000 = {
	hidden = yes
	immediate = {
		send_interface_toast = {
			title = religious_decision.0291.toast
			left_icon = root
			current_travel_plan ?= {
				if = {
					limit = { is_paused = yes }
					resume_travel_plan = yes
				}
			}
		}
		remove_variable = shrine_location
		remove_character_flag = local_shrine_rite
	}
}
