﻿namespace = hold_court

# 7000-7999		-	James Beaumont
# 7000 - Fair Share
# 7010 - Two Faiths, One Family
# 7020 - The Shield of Faith

# Fair Share
# Low-dev counties want some money
# 7000
scripted_trigger hc_7000_valid_county = {
	hc_7000_low_dev = yes
	OR = {
		AND = {
			exists = holder
			holder != root
		}
		any_direct_de_facto_vassal_title = {
			exists = holder
			holder != root
		}
	}
}
scripted_trigger hc_7000_low_dev = {
	development_level <= {
		value = root.capital_county.development_level
		subtract = 10
	}
}
hold_court.7000 = {
	type = court_event
	title = hold_court.7000.t
	desc = hold_court.7000.desc
	theme = realm
	cooldown = { years = 15 }

	court_scene = {
		button_position_character = scope:local_leader
		court_event_force_open = yes
		show_timeout_info = no
		should_pause_time = yes
		roles = {
			scope:local_leader = {
				group = petitioners_group
				animation = beg
			}
		}
	}

	widget = {
		gui = "event_window_widget_event_chain_progress"
		container = "custom_widgets_container"
		controller = event_chain_progress
	}

	trigger = {
		any_sub_realm_county = {
			hc_7000_valid_county = yes
		}
	}

	weight_multiplier = {
		base = 1
		# Court weightings.
		ep1_weight_up_for_court_type_modifier = { COURT_TYPE = court_administrative }
	}

	immediate = {
		random_sub_realm_county = {
			limit = {
				hc_7000_valid_county = yes
			}
			save_scope_as = low_dev_county
			if = {
				limit = {
					exists = holder
					holder != root
				}
				holder = { save_scope_as = local_leader }
			}
			else = {
				random_direct_de_facto_vassal_title = {
					limit = {
						exists = holder
						holder != root
					}
					holder = { save_scope_as = local_leader }
				}
			}
		}
	}

	# Divert resources to the low counties
	option = {
		name = hold_court.7000.a
		every_sub_realm_county = {
			limit = {
				hc_7000_low_dev = yes
			}
			custom = every_county_10_dev_below_capital
			add_county_modifier = {
				modifier = hc_7000_dev_boost
				years = 15
			}
		}
		capital_county = {
			add_county_modifier = {
				modifier = hc_7000_dev_reduction
				years = 15
			}
		}
		remove_treasury_or_gold = medium_treasury_or_gold_value
		stress_impact = {
			callous = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_greed = -1
   				ai_compassion = 1
			}
			modifier = {
				factor = 0
				short_term_treasury_or_gold <= medium_treasury_or_gold_value
			}
		}
	}

	# Perhaps they should move to the capital ¯\_(ツ)_/¯
	option = {
		name = hold_court.7000.b
		every_sub_realm_county = {
			limit = {
				hc_7000_low_dev = yes
			}
			custom = every_county_10_dev_below_capital
			add_county_modifier = {
				modifier = hc_7000_dev_reduction
				years = 15
			}
		}
		capital_county = {
			add_county_modifier = {
				modifier = hc_7000_dev_boost
				years = 15
			}
		}
		remove_treasury_or_gold = minor_treasury_or_gold_value
		stress_impact = {
			compassionate = minor_stress_impact_gain
			humble = minor_stress_impact_gain
			just = minor_stress_impact_gain
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_greed = -1
   				ai_compassion = -1
			}
			modifier = {
				factor = 0
				short_term_treasury_or_gold <= minor_treasury_or_gold_value
			}
		}
	}

	# Man don't care
	option = {
		name = hold_court.7000.c
		stress_impact = {
			generous = minor_stress_impact_gain
			just = miniscule_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_greed = 1
   				ai_compassion = -1
			}
		}
	}
	after = {
		scope:local_leader = { clear_court_event_participation = yes }
		# Finish up the chain if relevant.
		hold_court_queue_post_event_effect = yes
	}
}

# Two Faiths, One Family
# Diff-daith neighbour wants to be besties
# 7010
scripted_trigger hc_7010_valid_neighbour = {
	NOT = {
		has_trait = zealous
		is_allied_to = root
		has_relation_rival = root
		has_trait = holy_warrior
	}
	ai_compassion > ai_zeal
	faith = {
		faith_hostility_level = {
			target = root.faith
			value >= faith_hostile_level
		}
	}
	any_child ?= {
		save_temporary_scope_as = foreign_princess_temp
		hc_7010_valid_prince = yes
		is_courtier_of = prev
		root = {
			any_child = {
				hc_7010_valid_prince = yes
				is_courtier_of = root
				allowed_to_marry_character_gender_trigger = { CHARACTER = scope:foreign_princess_temp }
			}
		}
	}
}
scripted_trigger hc_7010_valid_prince = {
	is_married = no
	is_concubine = no
	is_betrothed = no
	is_adult = yes
	fertility >= medium_fertility
	can_marry_trigger = yes
}
hold_court.7010 = {
	type = court_event
	title = hold_court.7010.t
	desc = hold_court.7010.desc
	theme = realm
	cooldown = { years = 15 }

	court_scene = {
		button_position_character = scope:friendly_neighbour
		court_event_force_open = yes
		show_timeout_info = no
		should_pause_time = yes
		roles = {
			scope:friendly_neighbour = {
				group = petitioners_group
				animation = personality_compassionate
			}
		}
	}

	lower_left_portrait = scope:potential_prince
	lower_right_portrait = scope:foreign_princess

	widget = {
		gui = "event_window_widget_event_chain_progress"
		container = "custom_widgets_container"
		controller = event_chain_progress
	}

	trigger = {
		top_liege = this
		any_neighboring_and_across_water_top_liege_realm_owner = {
			faith = {
				faith_hostility_level = {
					target = root.faith
					value >= faith_hostile_level
				}
			}
			count >= 2
		}
		any_neighboring_and_across_water_top_liege_realm_owner = {
			hc_7010_valid_neighbour = yes
		}
	}

	weight_multiplier = {
		base = 1
		# Court weightings.
		ep1_weight_up_for_court_type_modifier = { COURT_TYPE = court_administrative }
	}

	immediate = {
		random_neighboring_and_across_water_top_liege_realm_owner = {
			limit = {
				hc_7010_valid_neighbour = yes
			}
			save_scope_as = friendly_neighbour
			random_child = {
				limit = {
					save_temporary_scope_as = foreign_princess_temp
					hc_7010_valid_prince = yes
					is_courtier_of = prev
					root = {
						any_child = {
							hc_7010_valid_prince = yes
							is_courtier_of = root
							allowed_to_marry_character_gender_trigger = { CHARACTER = scope:foreign_princess_temp }
						}
					}
				}
				save_scope_as = foreign_princess
				root = {
					random_child = {
						limit = {
							hc_7010_valid_prince = yes
							is_courtier_of = root
							allowed_to_marry_character_gender_trigger = { CHARACTER = scope:foreign_princess }
						}
						save_scope_as = potential_prince
					}
				}
			}
		}
	}

	# The two shall be wed at once!
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							OR = {
								religion = religion:judaism_religion
								scope:neighboring_ruler = { religion = religion:judaism_religion }
							}
						}
						desc = hold_court.7010.a.jewish # To life, to life, l'chaim. L'chaim, l'chaim, to life. ♪♪
					}
					desc = hold_court.7010.a
				}
			}
		}
		set_relation_friend = {
			target = scope:friendly_neighbour
			reason = friend_beyond_faith
		}
		marry_spouse_1_dominant = {
			SPOUSE_1 = scope:potential_prince
			SPOUSE_2 = scope:foreign_princess
		}
		add_truce_both_ways = {
			character = scope:friendly_neighbour
			years = 10
			name = truce_religious_tolerance
		}
		add_character_modifier = {
			modifier = hc_7010_tolerance
			years = 10
		}
		stress_impact = {
			callous = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
			sadistic = minor_stress_impact_gain
			brave = minor_stress_impact_gain
			zealous = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_compassion = 1
   				ai_vengefulness = -1
   				ai_zeal = -10
			}
		}
	}

	# See how this lunatic speaks of heresy! They must be destroyed!
	option = {
		name = hold_court.7010.b
		if = {
			limit = {
				NOT = {
					has_trait = holy_warrior
				}
			}
			add_trait = holy_warrior
		}
		else = {
			add_character_modifier = {
				modifier = hc_7010_holy_war
				years = 10
			}
		}
		progress_towards_rival_effect = {
			REASON = rival_planning_holy_war
			CHARACTER = scope:friendly_neighbour
			OPINION = default_rival_opinion
		}
		stress_impact = {
			compassionate = medium_stress_impact_gain
			cynical = minor_stress_impact_gain
			just = minor_stress_impact_gain
			arbitrary = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_compassion = -1
   				ai_vengefulness = 1
   				ai_zeal = 2.5
			}
		}
	}

	# I don't wanna do this
	option = {
		name = hold_court.7010.c
		reverse_add_opinion = {
			modifier = disappointed_opinion
			opinion = -10
			target = scope:friendly_neighbour
		}
		stress_impact = {
			compassionate = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
			just = minor_stress_impact_gain
			zealous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 0
		}
	}
	after = {
		scope:friendly_neighbour = { clear_court_event_participation = yes }
		# Finish up the chain if relevant.
		hold_court_queue_post_event_effect = yes
	}
}

# The Shield of Faith
# Same-faith ruler offers to sponsor a reformation
# 7020
scripted_trigger hc_7020_valid_same_faith_friend = {
	top_liege = this
	this != root
	NOT = {
		has_relation_rival = root # I would never work with you!
		has_trait = zealous # I would never change the old ways!
	}
	any_sub_realm_barony = {
		is_holy_site_of = root.faith
		holder.faith ?= root.faith
	}
}
scripted_trigger hc_7020_valid_threat = {
	# You see them as a threat to your religion
	NOT = { faith = { has_doctrine_parameter = unreformed } }
	religion != root.religion
	faith = {
		faith_hostility_level = {
			target = root.faith
			value >= faith_hostile_level
		}
	}
	# They are encroaching on your religion in some way
	OR = {
		# They control a large amount of your faith's lands
		any_sub_realm_county = {
			count >= 4
			faith = root.faith
			# Religiously protected people within their realm should not count towards this
			holder.faith != root.faith
		}
		# They recently attacked you
		any_memory = {
			memory_type = war_won
			memory_participant:loser ?= root
		}
		# They control one of your holy sites
		any_sub_realm_barony = {
			is_holy_site_of = root.faith
			# The holy site is not held by a vassal of your faith
			holder.faith != root.faith
		}
	}
}
hold_court.7020 = {
	type = court_event
	title = hold_court.7020.t
	desc = {
		desc = hold_court.7020.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:threatening_ruler = {
						any_sub_realm_county = {
							count >= 4
							faith = root.faith
							holder.faith != root.faith
						}
					}
				}
				desc = hold_court.7020.desc.mid.oppression
			}
			triggered_desc = {
				trigger = {
					scope:threatening_ruler = {
						any_memory = {
							memory_type = war_won
							memory_participant:loser ?= root
						}
					}
				}
				desc = hold_court.7020.desc.mid.recent_war
			}
			triggered_desc = {
				trigger = {
					scope:threatening_ruler = {
						any_sub_realm_barony = {
							is_holy_site_of = root.faith
							holder.faith != root.faith
						}
					}
				}
				desc = hold_court.7020.desc.mid.holy_site
			}
		}
		desc = hold_court.7020.desc.outro
	}
	theme = realm
	cooldown = { years = 15 }

	court_scene = {
		button_position_character = scope:same_faith_friend
		court_event_force_open = yes
		show_timeout_info = no
		should_pause_time = yes
		roles = {
			scope:same_faith_friend = {
				group = petitioners_group
				animation = personality_compassionate
			}
		}
	}

	widget = {
		gui = "event_window_widget_event_chain_progress"
		container = "custom_widgets_container"
		controller = event_chain_progress
	}

	trigger = {
		is_ai = no # AI reformation should always follow the rules
		piety_level >= medium_piety_level
		faith = { has_doctrine_parameter = unreformed }
		any_sub_realm_barony = {
			is_holy_site_of = root.faith
			holder.faith ?= root.faith
		}
		faith = {
			holy_sites_controlled >= 3
			any_faith_character = {
				hc_7020_valid_same_faith_friend = yes
			}
		}
		any_neighboring_and_across_water_top_liege_realm_owner = {
			hc_7020_valid_threat = yes
		}
	}

	weight_multiplier = {
		base = 1
		# Court weightings.
		ep1_weight_up_for_court_type_modifier = { COURT_TYPE = court_administrative }
	}

	immediate = {
		random_sub_realm_barony = {
			limit = {
				is_holy_site_of = root.faith
				holder.faith ?= root.faith
			}
			save_scope_as = holy_site
		}
		faith = {
			random_faith_character = {
				limit = { hc_7020_valid_same_faith_friend = yes }
				save_scope_as = same_faith_friend
			}
		}
		random_neighboring_and_across_water_top_liege_realm_owner = {
			limit = { hc_7020_valid_threat = yes }
			save_scope_as = threatening_ruler
		}
	}

	# The reformation is neccessary
	option = {
		name = hold_court.7020.a
		remove_treasury_or_gold = major_treasury_or_gold_value
		progress_towards_friend_effect = {
			REASON = friend_united_by_faith
			CHARACTER = scope:same_faith_friend
			OPINION = default_friend_opinion
		}
		custom_tooltip = {
			text = ignore_3_site_rule_tt
			add_character_flag = ignore_3_holy_site_rule
		}
		custom_tooltip = {
			text = friend_will_convert_tt
			scope:same_faith_friend = {
				add_character_flag = forced_conversion_on_reform
			}
		}
		stress_impact = {
			zealous = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
			shy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_boldness = 1
   				ai_vengefulness = 1
   				ai_rationality = 1
   				ai_zeal = -2
			}
			modifier = {
				factor = 0
				short_term_treasury_or_gold < major_treasury_or_gold_value
			}
		}
	}

	# Stay strong, brother. We must not sacrifice our ways
	option = {
		name = hold_court.7020.b
		add_piety = medium_piety_gain
		add_character_modifier = {
			modifier = hc_7020_guardian_of_the_old_ways
			years = 15
		}
		scope:same_faith_friend = {
			add_character_modifier = {
				modifier = hc_7020_guardian_of_the_old_ways
				years = 15
			}
		}
		stress_impact = {
			cynical = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_boldness = -1
   				ai_vengefulness = 1
   				ai_zeal = 2.5
			}
		}
	}

	# We must join together in faith
	option = {
		name = hold_court.7020.c
		trigger = {
			NOT = { is_allied_to = scope:same_faith_friend }
		}
		create_alliance = {
			target = scope:same_faith_friend
			allied_through_owner = root
			allied_through_target = scope:same_faith_friend
		}
		stress_impact = {
			shy = minor_stress_impact_gain
			callous = minor_stress_impact_gain
			sadistic = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
   				ai_compassion = 1
   				ai_sociability = 1
   				ai_boldness = -1
   				ai_vengefulness = 1
   				ai_zeal = 2.5
			}
		}
	}
	after = {
		scope:same_faith_friend = { clear_court_event_participation = yes }
		# Finish up the chain if relevant.
		hold_court_queue_post_event_effect = yes
	}
}
