﻿activity_grand_council = {

	is_shown = {
		has_character_flag = council_host_flag
	}	

	is_valid = {
		scope:host = {
			is_alive = yes
			is_imprisoned = no
			NOT = { is_incapable = yes }
			has_character_flag = council_host_flag
			NOT = { is_at_war_with = scope:activity.special_guest:the_pretender }
			NOT = { has_character_flag = recently_held_council }
		}
		scope:activity.special_guest:the_pretender = {
			can_arrive_in_time_to_activity_minimum = scope:activity
			is_eligible_for_succession_crisis = yes
		}
	}


	guest_subsets = {
		supporter
		detractor
	}

	activity_window_widgets = {
		sc_supporter_detractor_widget = "window_activity_contestants_right_bar_container"
		sc_special_type_progression = "activity_plugin_widgets_summary"
	}

	on_start = {
		activity_setup_special_type_progression_effect = yes
		scope:activity = {
			set_variable = {
				name = activity_special_type_progression
				value = 50
			}
			activity_location = {
				save_scope_as = location
			}
		}

		scope:host = {
			liege = {
				save_scope_as = current_ruler
				
				if = {
					limit = { has_character_flag = crisis_heir_flag }
					save_scope_as = crisis_heir
				}
				
				primary_title = {
					save_scope_as = crisis_title
				}
				
				every_vassal_or_below = {
					limit = {
						this != scope:activity.special_guest:the_pretender
					}
					trigger_event = succession_crisis_misc.0023
				}
				
				every_neighboring_and_across_water_top_liege_realm_owner = {
					limit = {
						this != scope:activity.special_guest:the_pretender
					}
					trigger_event = succession_crisis_misc.0023
				}
			}
		}

		scope:activity.special_guest:the_pretender = {
			save_scope_as = warleader

			if = {
				limit = { is_ai = yes }
				set_location = root.activity_location
			}

			trigger_event = succession_crisis_misc.0017
		}
		
		scope:current_ruler = {
			add_character_flag = council_host_flag
			set_variable = {
				name = grand_council_special_guest
				value = scope:activity.special_guest:the_pretender
			}
		}
		
		set_activity_host = scope:current_ruler
	}


	pulse_actions = {
		entries = {
			host_gains_diplo_xp
			host_gains_intrigue_xp
			hook_trusting_guest
			guest_friendship
			guest_insulted
			host_gains_diplo_xp
			host_gains_steward_xp
			host_gains_intrigue_xp
			council_sort_intent_subsets
			contact_network
			guest_chokes_on_bone
			guest_brawl
			council_heated_brawl
			council_political_rivalry
			council_forge_alliance
			council_pretender_sways_vassal
			council_impressed_by_host_rhetoric
			council_marriage_pact_alliance
			council_theological_debate
			council_prestige_clash
			courtiers_became_lovers
			courtier_friendship
			courtier_improved_skill
			courtier_learned_language
			courtiers_got_into_fight
			courtiers_became_rivals
			council_purchase_hook
		}
		chance_of_no_event = 5
	}

	on_invalidated = {
		# Handle death of the host during the council
		if = {
			limit = {
				scope:host = { is_alive = no }
			}
			# Ensure dead_predecessor exists for context
			if = {
				limit = { NOT = { exists = scope:dead_predecessor } }
				scope:host = { save_scope_as = dead_predecessor }
			}
			
			scope:host = {
				primary_heir = {
					save_scope_as = new_host
					# Set location if not already at the activity location
					if = {
						limit = {
							NOT = { location = scope:activity.activity_location }
						}
						set_location = scope:activity.activity_location
					}
					
					# Set as new host and notify
					scope:activity = {
						set_activity_host = scope:new_host
					}
					
					trigger_event = succession_crisis_heir_event.0009
				}
			}
		}

		scope:host = {
		}
		scope:activity.special_guest:the_pretender = {
			set_variable = {
				name = invalidated_host
				value = scope:host
			}
			trigger_event = succession_crisis_diplomacy_event.0013
		}

		if = {
			limit = {
				scope:host = { is_incapable = yes }
				# Ensure we don't double trigger if death was handled above
				NOT = { scope:host = { is_alive = no } }
			}
			scope:activity = {
				activity_type = { save_scope_as = activity_type }
			}
			scope:host = {
				save_scope_as = new_host
				
				trigger_event = activity_system.0330
			}
			scope:activity = {
				every_attending_character = {
					limit = { this != scope:host }
					trigger_event = activity_system.0331
				}
			}
		}
	}

	province_filter = top_realm
	ai_province_filter = top_realm

	is_location_valid = {
		this = scope:host.liege.capital_province
	}

	max_province_icons = 1
	allow_zero_guest_invites = yes
	open_invite = yes

	province_score = {
		add = 1000
	}

	cooldown = { years = 0 }

	phases = {
		council_arrival_phase = {
			is_predefined = yes
			order = 1

			on_enter_phase = {
				scope:host = {
					add_character_flag = council_host_flag
				}
			}

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

					scope:activity = {
						activity_location = { save_scope_as = location }
					}
					
					# Ensure Host is at the destination
					scope:host = {
						if = {
							limit = {
								NOT = { location = scope:location }
							}
							set_location = scope:location
						}
					}

					trigger_event = succession_crisis_misc.0008
					scope:activity.special_guest:the_pretender = {
						trigger_event = succession_crisis_misc.0008
					}
					
					scope:activity = {
						every_attending_character = {
							if = {
								limit = {
									has_activity_intent = council_support_host
								}
								add_to_list = supporter
							}
							else_if = {
								limit = {
									has_activity_intent = council_support_pretender
								}
								add_to_list = detractor
							}
						}
						every_in_list = {
							list = supporter
							scope:activity = {
								add_to_guest_subset = {
									name = supporter
									target = prev
								}
							}
						}
						every_in_list = {
							list = detractor
							scope:activity = {
								add_to_guest_subset = {
									name = detractor
									target = prev
								}
							}
						}
					}
				}
				scope:activity = {
					every_attending_character = {
						council_calculate_progression_effect = yes
					}
				}
				scope:activity = {
					progress_activity_phase_after = { days = 180 }
				}
			}

			on_end = {
				if = {
					limit = { this = scope:host }
				}
			}
		}
		
		council_round_one = {
			is_predefined = yes
			order = 2

			on_enter_phase = {
				scope:activity = {
					progress_activity_phase_after = { days = 1 }
				}
			}

			on_phase_active = {
				scope:activity = {
					every_attending_character = {
						council_calculate_progression_effect = yes
					}
				}
				if = {
					limit = { this = scope:host }
					scope:activity.special_guest:the_pretender = {
						trigger_event = succession_crisis_diplomacy_event.0001
					}
					scope:host = {
						trigger_event = succession_crisis_diplomacy_ruler.0002
					}
				}
				scope:activity = {
					progress_activity_phase_after = { days = { 120 240 } }
				}
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					add_prestige = 50
				}
				scope:activity.special_guest:the_pretender = {
					add_prestige = 50
				}
			}

			on_weekly_pulse = {
				trigger_event = { on_action = council_debate_events }
			}
		}
		
		council_round_two = {
			is_predefined = yes
			order = 3

			on_enter_phase = {
				scope:activity = {
					progress_activity_phase_after = { days = 1 }				
				}
			}

			on_phase_active = {
				scope:activity = {
					every_attending_character = {
						council_calculate_progression_effect = yes
					}
				}
				if = {
					limit = { this = scope:host }
					scope:activity.special_guest:the_pretender = {
						trigger_event = succession_crisis_diplomacy_event.0003
					}
					scope:host = {
						trigger_event = succession_crisis_diplomacy_ruler.0003
					}
				}
				if = {
					limit = { this = scope:host }
					trigger_event = { on_action = council_opening_on_action }
				}
				scope:activity = {
					progress_activity_phase_after = { days = { 120 240 } }
				}
			}

			on_weekly_pulse = {
				trigger_event = { on_action = council_debate_events }
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					add_prestige = 50
				}
				scope:activity.special_guest:the_pretender = {
					add_prestige = 50
				}
				scope:activity = {
					every_attending_character = {
						if = {
							limit = {
								is_ai = yes
							}
							debate_disburse_activity_ai_guest_rewards = yes
						}
					}
				}
				if = {
					limit = { this = scope:host }
					debate_disburse_activity_host_rewards = yes
				}
				if = {
					limit = {
						NOT = { this = scope:host }
						is_ai = no
					}
					debate_disburse_activity_guest_rewards = yes
				}
				if = {
					limit = {
						this != scope:host
						this != scope:activity.var:challenged_movement_leader
					}
					trigger_event = debate_event.5130
				}
				scope:activity = {
					every_attending_character = {
						debate_cleanup_effect = yes
					}
				}
			}
		}
		
		council_conclusion = {
			is_predefined = yes
			order = 4

			on_enter_phase = {
				scope:activity = {
					progress_activity_phase_after = { days = 1 }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					
					if = {
						limit = { NOT = { exists = scope:crisis_title } }
						var:crisis_title = { save_scope_as = crisis_title }
					}
					
					scope:crisis_title = {
						holder = { save_scope_as = current_ruler }
					}
					
					if = {
						limit = { has_character_flag = crisis_heir_flag }
						save_scope_as = crisis_heir
					}
					
					# Host wins: Progression > 66
					if = {
						limit = {
							scope:activity.var:activity_special_type_progression > 66
						}
						scope:activity.special_guest:the_pretender = {
							trigger_event = succession_crisis_diplomacy_event.0005
						}
						trigger_event = succession_crisis_diplomacy_ruler.0004
					}
					# Deadlock: 33 <= Progression <= 66
					else_if = {
						limit = {
							scope:activity.var:activity_special_type_progression >= 33
						}
						scope:activity.special_guest:the_pretender = {
							trigger_event = succession_crisis_diplomacy_event.0006
						}
						trigger_event = succession_crisis_diplomacy_ruler.0008
					}
					# Pretender wins: Progression < 33
					else = {
						scope:activity.special_guest:the_pretender = {
							trigger_event = succession_crisis_diplomacy_event.0004
						}
						trigger_event = succession_crisis_diplomacy_ruler.0005
					}
				}
				scope:activity = {
					progress_activity_phase_after = { days = 10 }
				}
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					
					# Setup pretender reference
					scope:activity.special_guest:the_pretender = { save_scope_as = pretender }
					
					# Imminent War check
					# Pretender logic
					if = {
						limit = { scope:pretender = { has_character_flag = imminent_war } }
						scope:pretender = { save_scope_as = warleader }
						if = {
							limit = { scope:warleader = { has_character_flag = crisis_heir_flag } }
							scope:host = { trigger_event = succession_crisis_claimant_event.0005 }
						}
						else = {
							scope:host = { trigger_event = succession_crisis_heir_event.0006 }
						}
					}
					
					# Host logic
					if = {
						limit = { scope:host = { has_character_flag = imminent_war } }
						scope:host = { save_scope_as = warleader }
						if = {
							limit = { scope:warleader = { has_character_flag = crisis_heir_flag } }
							scope:pretender = { trigger_event = succession_crisis_claimant_event.0005 }
						}
						else = {
							scope:pretender = { trigger_event = succession_crisis_heir_event.0006 }
						}
					}

					if = {
						limit = {
							OR = {
								scope:host = { has_character_flag = imminent_war }
								scope:pretender = { has_character_flag = imminent_war }
							}
						}
						scope:activity = {
							every_attending_character = {
								trigger_event = sc_council_events.0012
							}
						}
					}
					else = {
						scope:activity = {
							every_attending_character = {
								trigger_event = sc_council_events.0014
							}
						}
						# Cleanup flags if no imminent war
						scope:activity = {
							every_attending_character = {
								remove_character_flag = pledged_to_heir
								remove_character_flag = pledged_to_claimant
							}
						}
						scope:host = {
							remove_character_flag = active_crisis_rebel
							remove_character_flag = crisis_heir_flag
							remove_character_flag = crisis_special_character
						}
						scope:pretender = {
							remove_character_flag = active_crisis_rebel
							remove_character_flag = crisis_heir_flag
							remove_character_flag = crisis_special_character
						}
					}
					
					# Cleanup flags for all participants
					scope:activity = {
						every_attending_character = {
							if = {
								limit = { has_character_flag = debating }
								remove_character_flag = debating
							}
							if = {
								limit = { has_character_flag = in_debate }
								remove_character_flag = in_debate
							}
							if = {
								limit = { has_character_flag = council_host_flag }
								remove_character_flag = council_host_flag
							}
						}
					}
				}
			}
		}
	}

	options = {
		feast_option_food = {
			council_food_none = {
				default = yes

				ai_will_do = {
					value = 0
					add = activity_option_likes_cheap_expense_value
					add = activity_option_food_prefers_small_volume_value
				}
			}
		}
		feast_option_drink = {
			council_drinks_none = {
				default = yes

				ai_will_do = {
					value = 0
					add = 10
				}
			}
		}
	}

	max_guest_arrival_delay_time = { years = 1 }

	ai_will_do = {
		value = 1000
	}


	ai_check_interval = 60

	ai_will_select_province = {
		value = 10
	}

	host_intents = {
		default = council_defend_claim
		intents = { council_defend_claim }
	}

	guest_intents = {
		default = council_bear_witness
		intents = { council_press_claim council_support_host council_support_pretender council_opportunist council_bear_witness council_weaken_pretender }
	}

	max_guests = 200

	guest_invite_rules = {
		rules = {
			2 = activity_invite_rule_rivals
			2 = activity_invite_rule_extended_family
			2 = activity_invite_rule_lovers
			2 = activity_invite_rule_potential_lovers
			1 = activity_invite_rule_hof
			6 = activity_invite_mp
		}
		defaults = {
			2 = activity_invite_rule_friends
			2 = activity_invite_rule_potential_friends
			1 = activity_invite_rule_close_family
			1 = activity_invite_rule_tributaries
			2 = 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
			2 = activity_invite_rule_all_lieges
			2 = activity_invite_neighbouring_rulers
			1 = activity_invite_rule_landless_adventurers_restricted_range
		}
	}

	can_be_activity_guest = {
		is_adult = yes
		OR = {
			in_diplomatic_range = scope:host
			this = scope:host.var:grand_council_special_guest
		}
	}

	special_guests = {
		the_pretender = {
			is_required = no

			select_character = {
				var:grand_council_special_guest = {
					save_scope_as = character
					save_scope_as = the_pretender
				}
			}

			can_pick = {
				this = scope:host.var:grand_council_special_guest
			}

			ai_will_do = {
				value = 1000
			}
		}
	}

	travel_entourage_selection = {
		weight = standard_travel_entourage_additions
		max = 10
		invite_rule_order = 3
	}

	guest_join_chance = {
		base = 1000
		base_activity_modifier = yes

		activity_guest_shared_ai_accept_modifier = yes

		modifier = {
			primary_title.tier = tier_empire
			add = 100
		}
		modifier = {
			primary_title.tier = tier_kingdom
			add = 100
		}
		modifier = {
			primary_title.tier = tier_duchy
			add = 100
		}
		modifier = {
			primary_title.tier = tier_county
			add = 100
		}
		modifier = {
			primary_title.tier = tier_barony
			add = 100
		}
	}

	background = {	
		texture = "gfx/interface/illustrations/event_scenes/fp2_throneroom.dds"
		environment = "environment_event_fp2_throneroom"
		ambience = "event:/SFX/Events/Backgrounds/throneroom_western"
	}

	window_characters = {
		guest = {
			camera = camera_body_right

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

			scripted_animation = {
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { drink_goblet happiness flirtation throne_room_conversation_3 throne_room_conversation_1 instrument_idle alto_flute_active worry stress boredom fear disbelief paranoia eyeroll drink eavesdrop wedding_drunk instrument_active toast }
				}
			}
		}

		guest = {
			camera = camera_body

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
						any_attending_character = {
							this = scope:activity.special_guest:the_pretender
							location = scope:activity.activity_location
						}
					}
					scope:activity = {
						every_attending_character = {
							limit = {
								this = scope:activity.special_guest:the_pretender
								location = scope:activity.activity_location
								is_lowborn = no
							}
							add_to_list = characters
						}
					}
				}
				else = {
					scope:activity = {
						every_attending_character = {
							limit = {
								location = scope:activity.activity_location
								this != scope:host
								is_lowborn = no
							}
							add_to_list = characters
						}
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:character = scope:activity.special_guest:the_pretender
					}
					animation = { ecstasy wedding_priest chaplain personality_honorable personality_bold hero_flex laugh throne_room_conversation_2 }
				}
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { drink_goblet happiness flirtation throne_room_conversation_3 throne_room_conversation_1 instrument_idle alto_flute_active worry stress boredom fear disbelief paranoia eyeroll drink eavesdrop wedding_drunk instrument_active toast }
				}
			}
		}

		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 = {
						always = yes
					}
					animation = { scepter reading lute_active writing emotion_thinking_scepter page_flipping }
				}
			}
		}

		guest = {
			camera = camera_body

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

			scripted_animation = {
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { drink_goblet happiness flirtation throne_room_conversation_3 throne_room_conversation_1 instrument_idle alto_flute_active worry stress boredom fear disbelief paranoia eyeroll drink eavesdrop wedding_drunk instrument_active toast }
				}
			}
		}

		guest = {
			camera = camera_body_left

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

			scripted_animation = {
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { drink_goblet happiness flirtation throne_room_conversation_3 throne_room_conversation_1 instrument_idle alto_flute_active worry stress boredom fear disbelief paranoia eyeroll drink eavesdrop wedding_drunk instrument_active toast }
				}
			}
		}
	}
}