##################################################
# Laamps
##################################################

create_landless_adventurer_title_history_effect = {
	holder ?= {
		if = {
			limit = {
				NOT = { has_realm_law = landless_adventurer_succession_law }
			}
			add_realm_law = landless_adventurer_succession_law
		}
	}
}

create_landless_minority_title_history_effect = {
	holder ?= {
		if = {
			limit = {
				NOT = { has_realm_law = confederate_partition_succession_law }
			}
			add_realm_law = confederate_partition_succession_law
		}
	}
}
create_landless_adventurer_title_effect = {
	save_scope_as = new_landless_adventurer
	if = {
		limit = {
			exists = house
		}
		house = { save_scope_as = new_landless_adventurer_house }
	}
	if = {
		limit = {
			exists = location
		}
		location = { save_scope_as = new_landless_adventurer_location }
	}
	save_scope_value_as = {
		name = adventurer_creation_reason
		value = $REASON$
	}
	$FLAVOR_CHAR$ = { save_scope_as = adventurer_flavor_char }
	hidden_effect = {
		random_list = {
			1000 = {
				trigger = {
					scope:warrior_youth ?= scope:new_landless_adventurer
					age <= 20
					exists = scope:host.house
				}
				create_adventurer_title = {
					name = adventurer_name_youths
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				create_adventurer_title = {
					name = adventurer_name_001
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				trigger = {
					exists = scope:new_landless_adventurer_house
				}
				create_adventurer_title = {
					name = adventurer_name_002
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				create_adventurer_title = {
					name = adventurer_name_003
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				create_adventurer_title = {
					name = adventurer_name_004
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				trigger = {
					NOT = {
						scope:new_landless_adventurer.culture = culture:kurdish
					}
				}
				create_adventurer_title = {
					name = adventurer_name_005
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				create_adventurer_title = {
					name = adventurer_name_006
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				create_adventurer_title = {
					name = adventurer_name_007
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				trigger = {
					exists = scope:new_landless_adventurer_house
				}
				create_adventurer_title = {
					name = adventurer_name_008
					holder = scope:new_landless_adventurer
					save_scope_as = new_title
				}
			}
			10 = {
				create_adventurer_title = {
					name = adventurer_name_009
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					exists = scope:new_landless_adventurer_location
				}
				create_adventurer_title = {
					name = adventurer_name_010
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					exists = scope:new_landless_adventurer_location
				}
				create_adventurer_title = {
					name = adventurer_name_011
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					exists = scope:new_landless_adventurer_location
				}
				create_adventurer_title = {
					name = adventurer_name_012
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal > 0
				}
				create_adventurer_title = {
					name = adventurer_name_013
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal > 0
				}
				create_adventurer_title = {
					name = adventurer_name_014
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal > 0
				}
				create_adventurer_title = {
					name = adventurer_name_015
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					OR = {
						has_martial_lifestyle_trait_trigger = yes
						ai_boldness >= medium_positive_ai_value
						has_trait = education_martial_prowess_1
						has_trait = education_martial_prowess_2
						has_trait = education_martial_prowess_3
						has_trait = education_martial_prowess_4
						has_education_martial_trigger = yes
					}
				}
				create_adventurer_title = {
					name = adventurer_name_016
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					OR = {
						has_martial_lifestyle_trait_trigger = yes
						has_trait = lifestyle_blademaster
						has_trait = education_martial_prowess_1
						has_trait = education_martial_prowess_2
						has_trait = education_martial_prowess_3
						has_trait = education_martial_prowess_4
						has_education_martial_trigger = yes
					}
				}
				create_adventurer_title = {
					name = adventurer_name_017
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal > medium_negative_ai_value
					scope:new_landless_adventurer.ai_boldness >= 0
					NOT = {
						scope:new_landless_adventurer = {
							faith.religion = religion:west_african_bori_religion
						}
					}
				}
				create_adventurer_title = {
					name = adventurer_name_018
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal > medium_negative_ai_value
					scope:new_landless_adventurer.ai_greed >= 0
					NOT = {
						scope:new_landless_adventurer = {
							faith.religion = religion:west_african_bori_religion
						}
					}
				}
				create_adventurer_title = {
					name = adventurer_name_019
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal > medium_negative_ai_value
					scope:new_landless_adventurer.ai_compassion >= 0
					NOT = {
						scope:new_landless_adventurer = {
							faith.religion = religion:west_african_bori_religion
						}
					}
				}
				create_adventurer_title = {
					name = adventurer_name_020
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					exists = scope:new_landless_adventurer_location.county
				}
				scope:new_landless_adventurer_location.county = {
					save_scope_as = new_landless_adventurer_county
				}
				create_adventurer_title = {
					name = adventurer_name_021
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				create_adventurer_title = {
					name = adventurer_name_022
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				trigger = {
					exists = scope:new_landless_adventurer_house
				}
				create_adventurer_title = {
					name = adventurer_name_023
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal > medium_negative_ai_value
					scope:new_landless_adventurer.ai_rationality >= 0
					#Many names using Allah already
					NOT = {
						scope:new_landless_adventurer = {
							faith.religion = religion:islam_religion
						}
					}
				}
				create_adventurer_title = {
					name = adventurer_name_024
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			7 = {
				trigger = {
					scope:new_landless_adventurer.ai_zeal < low_negative_ai_value
					scope:new_landless_adventurer.ai_compassion < medium_negative_ai_value
				}
				create_adventurer_title = {
					name = adventurer_name_025
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
			10 = {
				create_adventurer_title = {
					name = adventurer_name_0026
					holder = scope:new_landless_adventurer
					save_scope_as = new_title
				}
			}
			5 = {
				trigger = {
					exists = scope:new_landless_adventurer.culture
					OR = {
						has_martial_lifestyle_trait_trigger = yes
						ai_boldness >= medium_positive_ai_value
						has_trait = education_martial_prowess_1
						has_trait = education_martial_prowess_2
						has_trait = education_martial_prowess_3
						has_trait = education_martial_prowess_4
						has_education_martial_trigger = yes
					}
				}
				create_adventurer_title = {
					name = adventurer_name_0027
					holder = scope:new_landless_adventurer
					article = DEFAULT_TITLE_NAME_ARTICLE
					save_scope_as = new_title
				}
			}
		}

		scope:new_title ?= {
			create_landless_adventurer_title_history_effect = yes
			set_variable = {
				name = adventurer_creation_reason
				value = scope:adventurer_creation_reason
			}
			set_variable = {
				name = adventurer_flavor_char
				value = scope:adventurer_flavor_char
			}
		}

		scope:new_title = { save_scope_as = new_landless_adventurer_title }
		scope:new_landless_adventurer = {
			if = {
				limit = {
					NOT = { has_government = landless_adventurer_government }
				}
				change_government = landless_adventurer_government
			}
			if = {
				limit = {
					NOT = { has_realm_law = landless_adventurer_succession_law }
				}
				add_realm_law = landless_adventurer_succession_law
			}
			if = {
				limit = {
					has_realm_law = crown_authority_0
				}
				remove_realm_law = crown_authority_0
			}
			domicile = { change_provisions = starting_provisions_value }
		}
		every_player = {
			limit = {
				NOR = {
					root ?= this
					this = scope:new_landless_adventurer
				}
				OR = {
					# Either they're important to us.
					is_close_or_extended_family_of = scope:new_landless_adventurer
					is_consort_of = scope:new_landless_adventurer
					has_important_relationship_with_character_trigger = { CHARACTER = scope:new_landless_adventurer }
					# Or they're just close enough.
					AND = {
						capital_province = { "squared_distance(scope:new_landless_adventurer.location)" <= squared_distance_medium }
						OR = {
							top_liege = root.top_liege
							any_neighboring_and_across_water_top_liege_realm_owner = { this = root.top_liege }
						}
					}
				}
			}
			send_interface_message = {
				type = msg_landless_adventurer_nearby
				title = nearby_landless_adventurer_created_message_title
				left_icon = scope:new_landless_adventurer
				right_icon = scope:adventurer_flavor_char
				switch = {
					trigger = scope:adventurer_creation_reason
					flag:conquest = { custom_tooltip = nearby_landless_adventurer_created_message_conquest_tt }
					flag:conquest_claim = { custom_tooltip = nearby_landless_adventurer_created_message_conquest_tt }
					flag:deposition = { custom_tooltip = nearby_landless_adventurer_created_message_deposition_tt }
					flag:succession = { custom_tooltip = nearby_landless_adventurer_created_message_succession_tt }
					flag:exile = { custom_tooltip = nearby_landless_adventurer_created_message_exile_tt }
					flag:voluntary = { custom_tooltip = nearby_landless_adventurer_created_message_voluntary_tt }
					flag:revocation = { custom_tooltip = nearby_landless_adventurer_created_message_revocation_tt }
					flag:runaway_allowed = { custom_tooltip = nearby_landless_adventurer_created_message_runaway_allowed_tt }
					flag:runaway_forbidden = { custom_tooltip = nearby_landless_adventurer_created_message_runaway_forbidden_tt }
					flag:wanderer = { custom_tooltip = nearby_landless_adventurer_created_message_wanderer_tt }
					flag:eradicate = { custom_tooltip = nearby_landless_adventurer_created_message_eradicate_tt }
					fallback = { custom_tooltip = nearby_landless_adventurer_created_message_fallback_tt }
				}
			}
		}
	}
	custom_tooltip = laamp_become_adventurer_tt
	if = {
		limit = {
			OR = {
				scope:adventurer_creation_reason = flag:deposition
				scope:adventurer_creation_reason = flag:conquest
				scope:adventurer_creation_reason = flag:conquest_claim
				scope:adventurer_creation_reason = flag:exile
			}
			NOT = { this = scope:adventurer_flavor_char }
		}
		add_opinion = {
			target = scope:adventurer_flavor_char
			modifier = demanded_eviction_opinion
		}
		remove_laamp_from_exiled_county_automatically_effect = {
			PROVINCE = scope:new_landless_adventurer.location
			LAAMP = scope:new_landless_adventurer
		}
		save_scope_value_as = {
			name = expelled
			value = yes
		}
	}
	hidden_effect = {
		if = {
			limit = {
				scope:new_landless_adventurer = {
					is_ai = no
				}
				NOT = { exists = scope:expelled }
			}
			populate_location_with_contracts_effect = {
				AREA_CHAR = scope:new_landless_adventurer
				AMOUNT = 3
			}
		}
		scope:new_landless_adventurer = {
			every_councillor = {
				if = {
					limit = {
						NOR = {
							has_council_position = councillor_court_chaplain
							has_council_position = councillor_spouse
						}
					}
					scope:new_landless_adventurer = {
						fire_councillor = prev
					}
					remove_opinion = {
						modifier = fired_from_council_opinion
						target = scope:new_landless_adventurer
					}
				}
				else_if = {
					limit = {
						NOR = {
							is_close_or_extended_family_of = scope:new_landless_adventurer
							has_relation_lover = scope:new_landless_adventurer
							has_relation_friend = scope:new_landless_adventurer
							is_consort_of = scope:new_landless_adventurer
							has_council_position = councillor_spouse
						}
					}
					death = {
						death_reason = death_vanished
					}
				}
			}
			every_courtier = {
				limit = {
					NOR = {
						is_close_or_extended_family_of = scope:new_landless_adventurer
						has_relation_lover = scope:new_landless_adventurer
						has_relation_friend = scope:new_landless_adventurer
						is_consort_of = scope:new_landless_adventurer
					}
				}
				move_to_pool = yes
			}
			# Give us a couple of starting chars if we're almost completely bereft.
			if = {
				limit = {
					any_courtier = { count <= 2 }
				}
				trigger_event = {
					id = ep3_laamps.1001
					weeks = { 2 6 }
				}
			}
		}
		trigger_event = { id = misc.0001 days = 1 }
		local_arbitration_boon_change_purpose_effect = yes
	}
	add_to_global_variable_list = {
		name = laamps_tally
		target = scope:new_landless_adventurer.primary_title
	}
	set_variable = flavourise_camp_purpose_wanderers
}

create_landless_minority_title_effect = {
	save_scope_as = new_landless_minority
	if = {
		limit = {
			NOT = { exists = dynasty }
		}
		create_dynasty = {
			spread_to_descendants = yes
		}
	}
	if = {
		limit = {
			exists = house
		}
		house = { save_scope_as = new_landless_minority_house }
	}
	if = {
		limit = {
			exists = location
		}
		location = { save_scope_as = new_landless_minority_location }
	}
	save_scope_value_as = {
		name = minority_creation_reason
		value = $REASON$
	}
	$FLAVOR_CHAR$ = { save_scope_as = minority_flavor_char }
	hidden_effect = {
		
		create_adventurer_title = {
			name = minority_name
			holder = scope:new_landless_minority
			article = DEFAULT_TITLE_NAME_ARTICLE
			government = landless_minority_government
			save_scope_as = new_title
		}
		scope:new_title ?= {
			create_landless_minority_title_history_effect = yes
			set_variable = {
				name = minority_creation_reason
				value = scope:minority_creation_reason
			}
			set_variable = {
				name = minority_flavor_char
				value = scope:minority_flavor_char
			}
		}

		scope:new_title = { save_scope_as = new_landless_minority_title }
		scope:new_landless_minority = {
			if = {
				limit = {
					NOT = { has_government = landless_minority_government }
				}
				change_government = landless_minority_government
			}
			if = {
				limit = {
					NOT = { has_realm_law = confederate_partition_succession_law }
				}
				add_realm_law = confederate_partition_succession_law
			}
			if = {
				limit = {
					has_realm_law = crown_authority_0
				}
				remove_realm_law = crown_authority_0
			}
			domicile = { change_provisions = starting_provisions_value }
		}
		every_player = {
			limit = {
				NOR = {
					root ?= this
					this = scope:new_landless_minority
				}
				OR = {
					# Either they're important to us.
					is_close_or_extended_family_of = scope:new_landless_minority
					is_consort_of = scope:new_landless_minority
					has_important_relationship_with_character_trigger = { CHARACTER = scope:new_landless_minority }
					# Or they're just close enough.
					AND = {
						capital_province = { "squared_distance(scope:new_landless_minority.location)" <= squared_distance_medium }
						OR = {
							top_liege = root.top_liege
							any_neighboring_and_across_water_top_liege_realm_owner = { this = root.top_liege }
						}
					}
				}
			}
			send_interface_message = {
				type = msg_landless_minority_nearby
				title = nearby_landless_minority_created_message_title
				left_icon = scope:new_landless_minority
				right_icon = scope:minority_flavor_char
				switch = {
					trigger = scope:minority_creation_reason
					flag:conquest = { custom_tooltip = nearby_landless_minority_created_message_conquest_tt }
					flag:conquest_claim = { custom_tooltip = nearby_landless_minority_created_message_conquest_tt }
					flag:deposition = { custom_tooltip = nearby_landless_minority_created_message_deposition_tt }
					flag:succession = { custom_tooltip = nearby_landless_minority_created_message_succession_tt }
					flag:exile = { custom_tooltip = nearby_landless_minority_created_message_exile_tt }
					flag:voluntary = { custom_tooltip = nearby_landless_minority_created_message_voluntary_tt }
					flag:revocation = { custom_tooltip = nearby_landless_minority_created_message_revocation_tt }
					flag:runaway_allowed = { custom_tooltip = nearby_landless_minority_created_message_runaway_allowed_tt }
					flag:runaway_forbidden = { custom_tooltip = nearby_landless_minority_created_message_runaway_forbidden_tt }
					flag:wanderer = { custom_tooltip = nearby_landless_minority_created_message_wanderer_tt }
					flag:eradicate = { custom_tooltip = nearby_landless_minority_created_message_eradicate_tt }
					fallback = { custom_tooltip = nearby_landless_minority_created_message_fallback_tt }
				}
			}
		}
	}
	custom_tooltip = laamp_become_minority_tt
	hidden_effect = {
		if = {
			limit = {
				scope:new_landless_minority = {
					is_ai = no
				}
				NOT = { exists = scope:expelled }
			}
			populate_location_with_contracts_effect = {
				AREA_CHAR = scope:new_landless_minority
				AMOUNT = 3
			}
		}
		scope:new_landless_minority = {
			every_courtier = {
				limit = {
					NOR = {
						is_close_or_extended_family_of = scope:new_landless_minority
						has_relation_lover = scope:new_landless_minority
						has_relation_friend = scope:new_landless_minority
						is_consort_of = scope:new_landless_minority
					}
				}
				move_to_pool = yes
			}
			# Give us a couple of starting chars if we're almost completely bereft.
			if = {
				limit = {
					is_ai = no
					any_courtier = { count <= 2 }
				}
				trigger_event = {
					id = ep3_laamps.1001
					weeks = { 2 6 }
				}
			}
			else = {
				create_character = {
					template = pool_repopulate_diplomacy
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = diplomacy_1
					diplomacy = {
						min_template_high_skill
						max_template_high_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_diplomacy
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = diplomacy_2
					diplomacy = {
						min_template_high_skill
						max_template_high_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_martial
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = martial_1
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_high_skill
						max_template_high_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_martial
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = martial_2
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_high_skill
						max_template_high_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_stewardship
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = stewardship_1
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_high_skill
						max_template_high_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_stewardship
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = stewardship_2
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_high_skill
						max_template_high_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_intrigue
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = intrigue_1
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_high_skill
						max_template_high_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_intrigue
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = intrigue_2
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_high_skill
						max_template_high_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_learning
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = learning_1
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_high_skill
						max_template_high_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_learning
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = learning_2
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_high_skill
						max_template_high_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_prowess
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = prowess_1
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_high_skill
						max_template_high_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_prowess
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = prowess_2
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_high_skill
						max_template_high_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_local_flavor
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = women_1
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_local_flavor
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = women_2
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				create_character = {
					template = pool_repopulate_local_flavor
					dynasty = none
					culture = scope:new_landless_minority.culture
					faith = scope:new_landless_minority.faith
					location = root.location
					save_scope_as = women_3
					diplomacy = {
						min_template_average_skill
						max_template_average_skill
					}
					martial = {
						min_template_average_skill
						max_template_average_skill
					}
					stewardship = {
						min_template_average_skill
						max_template_average_skill
					}
					intrigue = {
						min_template_average_skill
						max_template_average_skill
					}
					learning = {
						min_template_average_skill
						max_template_average_skill
					}
					prowess = {
						min_template_average_skill
						max_template_average_skill
					}
					after_creation = { add_to_list = spawned_chars_list }
				}
				add_courtier = scope:women_1
				add_courtier = scope:women_2
				add_courtier = scope:women_3
				add_courtier = scope:martial_1
				add_courtier = scope:martial_2
				add_courtier = scope:diplomacy_1
				add_courtier = scope:diplomacy_2
				add_courtier = scope:stewardship_1
				add_courtier = scope:stewardship_2
				add_courtier = scope:learning_1
				add_courtier = scope:learning_2
				add_courtier = scope:intrigue_1
				add_courtier = scope:intrigue_2
				add_courtier = scope:prowess_1
				add_courtier = scope:prowess_2
			}
		}
		trigger_event = { id = misc.0001 days = 1 }
		local_arbitration_boon_change_purpose_effect = yes
	}
	add_to_global_variable_list = {
		name = laamps_tally
		target = scope:new_landless_minority.primary_title
	}
	set_variable = flavourise_camp_purpose_wanderers
}

destroy_landless_title_no_dlc_effect = {
	if = {
		limit = {
			NOT = { has_dlc_feature = roads_to_power }
			game_start_date = $DATE$
		}
		holder = { destroy_title = prev }
	}
}

set_landless_government_effect = {
	if = {
		limit = {
			has_dlc_feature = roads_to_power
		}
		change_government = landless_adventurer_government
	}
}

disburse_knight_errant_decision_rewards_effect = {
	add_trait = knight_errant
	house = {
		add_house_modifier = {
			modifier = ep3_knight_of_the_swan_house_modifier
		}
	}
	custom_tooltip = knight_errant_decision_armor_tt
	if = {
		limit = { has_dlc_feature = legends }
		create_swan_knight_legend_seed_effect = yes
	}
	if = {
		limit = { has_character_modifier = bandit_king_modifier }
		remove_character_modifier = bandit_king_modifier
	}
}

laamp_game_over_option_effect = {
	add_internal_flag = dangerous
	custom_tooltip = laamp_game_over_tt
	if = {
		limit = { exists = scope:new_landless_adventurer_title }
		hidden_effect = { 
			destroy_title = scope:new_landless_adventurer_title 
			add_character_flag = { flag = declined_become_laamp days = 1 } # The flag is temporary to make the player get a game over if they had no landed heir to take over their lands
			# While also making it so that if they did have an heir to take over they can later play as the old character if they somehow inherit from the heir
		}
	}
}

destroy_laamp_effect = {
	$ADVENTURER$ = {
		save_scope_as = the_adventurer
		# If we weren't an adventurer before, make us one.
		if = {
			limit = {
				NOT = { has_trait = adventurer }
			}
			add_trait = adventurer
		}
		hidden_effect = {
			every_courtier = {
				set_variable = {
					name = former_camp_leader
					value = scope:the_adventurer
				}
				add_trait = adventurer_follower
			}
		}
		custom_tooltip = {
			text = destroy_laamp_effect.tt.domicile_liquidated
			laamp_clear_domicile_buildings_effect = yes
		}
		# Contacts have opinions™ about you.
		## Be they good.
		every_contact = {
			limit = {
				opinion = {
					target = scope:the_adventurer
					value >= 1
				}
			}
			custom = custom.every_contact_with_positive_opinion
			add_opinion = {
				target = scope:the_adventurer
				modifier = pleased_former_contact_opinion
				opinion = 30
			}
			hidden_effect = {
				if = {
					limit = {
						can_set_relation_potential_friend_trigger = { CHARACTER = scope:the_adventurer }
					}
					set_relation_potential_friend = scope:the_adventurer
				}
			}
		}
		## Or bad.
		every_contact = {
			limit = {
				opinion = {
					target = scope:the_adventurer
					value < 1
				}
			}
			# And neutral but _eh_.
			custom = custom.every_contact_with_negative_opinion
			add_opinion = {
				target = scope:the_adventurer
				modifier = frustrated_former_contact_opinion
				opinion = -30
			}
			hidden_effect = {
				if = {
					limit = {
						can_set_relation_potential_rival_trigger = { CHARACTER = scope:the_adventurer }
					}
					set_relation_potential_rival = scope:the_adventurer
				}
			}
		}
		# Destroy your main title.
		every_held_title = {
			limit = { has_variable = adventurer_creation_reason }
			save_scope_as = landless_adventurer_title
			scope:the_adventurer = { destroy_title = scope:landless_adventurer_title }
		}
		# Ironically, we only want to use this effect if you're _not_ admin — it'll reset you nicely to the best government for you, but if you're admin, you should stay admin.
		if = {
			limit = {
				NOR = {
					government_has_flag = government_is_administrative
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_steppe_admin
					government_has_flag = government_is_wanua
					government_has_flag = government_is_mandala
					government_has_flag = government_is_celestial
					government_has_flag = government_is_meritocratic
					government_has_flag = government_is_japan_feudal
				}
			}
			admin_government_reset_effect = yes #this only contains Clan, Tribal, Nomadic and Feudal
		}
		# Clean up story no-movement flags.
		remove_character_flag = laamp_stay_same_kingdom
	}
}

create_landless_adventurer_title_tooltip_effect = {
	show_as_tooltip = {
		custom_tooltip = laamp_become_adventurer_tt
		if = {
			limit = { exists = scope:new_landless_adventurer_title}
			show_as_tooltip = { get_title = scope:new_landless_adventurer_title }
		}
	}
}
create_landless_minority_title_tooltip_effect = {
	show_as_tooltip = {
		custom_tooltip = laamp_become_minority_tt
		if = {
			limit = { exists = scope:new_landless_minority_title}
			show_as_tooltip = { get_title = scope:new_landless_minority_title }
		}
	}
}

find_playable_relatives_effect = {
	if = {
		limit = {
			any_close_family_member = { playable_relative_trigger = yes }
		}
		ordered_close_family_member = {
			limit = { playable_relative_trigger = yes }
			order_by = highest_held_title_tier
			max = 3
			check_range_bounds = no
			if = {
				limit = { exists = scope:alt_2 }
				save_scope_as = alt_3
			}
			else_if = {
				limit = { exists = scope:alt_1 }
				save_scope_as = alt_2
			}
			else = { save_scope_as = alt_1 }
		}
	}
}

laamp_switch_playable_character_effect = {
	set_player_character = $NEW_CHARACTER$
	hidden_effect = {
		if = {
			limit = { exists = scope:new_landless_adventurer_title }
			destroy_title = scope:new_landless_adventurer_title
		}
	}
}

ep3_become_landed_save_liege_effect = {
	$TITLE_GIVER$ = { save_scope_as = ruler }
	save_scope_value_as = {
		name = override_independence_logic
		value = $ALWAYS_INDEPENDENT$
	}
	if = {
		limit = { scope:override_independence_logic = yes }
		# Do nothing, we're just here to block subsequent checks.
	}
	else_if = {
		limit = {
			scope:ruler = { top_liege != this }
			any_in_list = {
				list = $TITLE_LIST$
				tier = scope:ruler.highest_held_title_tier
			}
			NOT = {
				any_in_list = {
					list = $TITLE_LIST$
					tier > scope:ruler.highest_held_title_tier
				}
			}
		}
		scope:ruler.liege = { save_scope_as = new_liege }
	}
	else_if = {
		limit = {
			NOT = {
				any_in_list = {
					list = $TITLE_LIST$
					tier >= scope:ruler.highest_held_title_tier
				}
			}
		}
		scope:ruler = { save_scope_as = new_liege }
	}
}

ep3_become_landed_transfer_effect = {
	hidden_effect = {
		# Liquidate camp
		$TITLE_RECEIVER$ = { laamp_clear_domicile_buildings_liquidator_effect = yes }
	}
	$TITLE_RECEIVER$ = {
		if = {
			limit = { character:90028 ?= this }
			custom_tooltip = ep3_story_cycle_harrying.3053.b.tt
			add_character_flag = hereward_settled_flag
		}
		set_variable = {
			name = ascended_throne_reason
			value = $REASON$
			weeks = 1
		}
		save_scope_value_as = {
			name = ascended_throne_value
			value = $REASON$
		}
		set_variable = { # Used to prevent invalid NF creation
			name = laamp_invasion_flag
			value = scope:target
			days = 1
		}
	}
	create_title_and_vassal_change = {
		type = $TYPE$
		add_claim_on_loss = yes
		save_scope_as = title_change
	}
	scope:target.holder ?= { save_scope_as = old_holder }
	if = {
		limit = { NOT = { exists = scope:old_holder } }
		scope:target = {
			ordered_de_jure_county = {
				limit = { holder.top_liege = scope:defender.top_liege }
				order_by = development_level
				holder = { save_scope_as = old_holder }
			}
		}
	}
	scope:supporter ?= { save_scope_as = government_giver }
	if = {
		limit = { NOT = { exists = scope:government_giver } }
		scope:old_holder = { save_scope_as = government_giver }
	}
	scope:government_giver ?= {
		switch = {
			trigger = government_has_flag
			government_is_administrative = {
				save_scope_value_as = { name = old_government value = flag:admin }
			}
			government_is_clan = {
				save_scope_value_as = { name = old_government value = flag:clan }
			}
			government_is_tribal = {
				save_scope_value_as = { name = old_government value = flag:tribal }
			}
			government_is_wanua = {
				save_scope_value_as = { name = old_government value = flag:wanua }
			}
			government_is_nomadic = {
				save_scope_value_as = { name = old_government value = flag:nomad }
			}
			government_is_herder = {
				save_scope_value_as = { name = old_government value = flag:nomad }
			}
			government_is_celestial = {
				save_scope_value_as = { name = old_government value = flag:celestial }
			}
			government_is_steppe_admin = {
				save_scope_value_as = { name = old_government value = flag:steppe_admin }
			}
			government_is_meritocratic = {
				save_scope_value_as = { name = old_government value = flag:meritocratic }
			}
			government_is_japan_administrative = {
				save_scope_value_as = { name = old_government value = flag:japan_administrative }
			}
			government_is_japan_feudal = {
				save_scope_value_as = { name = old_government value = flag:japan_feudal }
			}
			government_is_mandala = {
				save_scope_value_as = { name = old_government value = flag:mandala }
			}
		}
	}
	if = {
		limit = {
			NOT = { exists = scope:new_capital }
		}
		ordered_in_list = {
			list = $TITLE_LIST$
			limit = { tier = tier_county }
			order_by = development_level
			save_scope_as = new_capital
		}
	}
	if = {
		limit = {
			NOT = { exists = scope:new_capital }
		}
		scope:new_primary_title ?= {
			ordered_de_jure_county = {
				limit = {
					holder = scope:defender
				}
				alternative_limit = {
					holder.top_liege = scope:defender.top_liege
				}
				order_by = development_level
				save_scope_as = new_capital
			}
		}
	}
	ordered_in_list = {
		list = $TITLE_LIST$
		order_by = tier
		save_scope_as = new_primary_title
	}
	$TITLE_RECEIVER$ = {
		if = {
			limit = {
				exists = scope:new_primary_title
				highest_held_title_tier > scope:new_primary_title.tier
				primary_title = { is_landless_type_title = no }
			}
			primary_title = { save_scope_as = new_primary_title }
		}
		else = {
			scope:new_primary_title ?= {
				change_title_holder_include_vassals = {
					holder = $TITLE_RECEIVER$
					change = scope:title_change
					take_baronies = no
				}
			}
		}
	}
	scope:new_primary_title ?= {
		every_de_jure_county = {
			limit = {
				NOT = { is_in_list = $TITLE_LIST$ }
				trigger_if = {
					limit = { exists = scope:defender }
					holder.top_liege ?= scope:defender.top_liege
				}
				trigger_if = {
					limit = { exists = scope:new_liege }
					holder.top_liege ?= scope:new_liege.top_liege
				}
			}
			change_title_holder_include_vassals = {
				holder = $TITLE_RECEIVER$
				change = scope:title_change
				take_baronies = no
			}
		}
	}
	every_in_list = {
		list = $TITLE_LIST$
		limit = {
			is_leased_out = no
			trigger_if = {
				limit = {
					NOT = { exists = var:ceremonial_title }
				}
				is_landless_type_title = no
			}
			is_head_of_faith = no
			tier >= tier_county
			trigger_if = {
				limit = { exists = scope:defender }
				holder.top_liege ?= scope:defender.top_liege
			}
			trigger_if = {
				limit = { exists = scope:new_liege }
				holder.top_liege ?= scope:new_liege.top_liege
			}
			NOT = { scope:new_primary_title ?= this }
		}
		change_title_holder_include_vassals = {
			holder = $TITLE_RECEIVER$
			change = scope:title_change
			take_baronies = no
		}
	}
	if = {
		limit = { has_character_flag = special_laamp_char }
		resolve_title_and_vassal_change = scope:title_change
		# Then we resolve vassalage separately, so that we can lock in tiers.
		create_title_and_vassal_change = {
			type = $TYPE$
			add_claim_on_loss = yes
			save_scope_as = title_change
		}
	}
	if = {
		limit = {
			exists = scope:new_liege
			trigger_if = {
				limit = { exists = scope:ascended_throne_value }
				NOR = {
					scope:ascended_throne_value = flag:conquest
					scope:ascended_throne_value = flag:conquest_claim
				}
			}
		}
		$TITLE_RECEIVER$ = {
			change_liege = {
				liege = scope:new_liege
				change = scope:title_change
			}
		}
	}
	else_if = {
		limit = { top_liege != this }
		becomes_independent = { change = scope:title_change }
	}
	resolve_title_and_vassal_change = scope:title_change
	# Because target may be either a title or a character, we check if a scope value has been set up in advance if this is being called from the Seize Realm scheme specifically
	if = {
		limit = {
			exists = scope:old_government_type
		}
		scope:old_government_type = {
			save_scope_as = old_government
		}
	}
	# Set government type
	if = {
		limit = {
			exists = scope:old_government
		}
		# We don't expect to actually trigger most of the below, as most are not valid targets to begin with, but all are included for completeness and mod support
		switch = {
			trigger = scope:old_government
			flag:feudal = {
				change_government = feudal_government
			}
			flag:republic = {
				change_government = republic_government
			}
			flag:theocracy = {
				change_government = theocracy_government
			}
			flag:clan = {
				change_government = clan_government
			}
			flag:tribal = {
				change_government = tribal_government
			}
			flag:wanua = {
				change_government = wanua_government
			}
			flag:holy_order = {
				change_government = holy_order_government
			}
			flag:administrative = {
				change_government = administrative_government
			}
			flag:nomad = {
				change_government = nomad_government
			}
			flag:herder = {
				change_government = herder_government
			}
			flag:celestial = {
				change_government = celestial_government
			}
			flag:mandala = {
				if = {
					limit = {
						faith = { has_doctrine = doctrine_polytheist }
					}
					change_government = mandala_government
					#Sort out the House shenanigans
					if = {
						limit = { is_house_head = no }
						house = { 
							if = {
								limit = {
									house_head = { 
										is_landed = no
										is_courtier_of = root
									}
								}
								set_house_head = root
							}
						}
					}
					else = {
						house ?= {
							set_house_aspiration = { type = no_aspect }
						}
					}
				}
				else = {
					if = {
						limit = { ep3_is_clan_inclined_trigger = yes }
						change_government = clan_government
					}
					else = { change_government = feudal_government }
				}
			}
			flag:steppe_admin = {
				change_government = steppe_admin_government
			}
			flag:meritocratic = {
				change_government = meritocratic_government
			}
			flag:japan_feudal = {
				change_government = japan_feudal_government
			}
			flag:japan_administrative = {
				if = {
					limit = { 
						scope:new_liege ?= { has_government = japan_administrative_government }
					}
					change_government = japan_administrative_government
				}
				else = { change_government = feudal_government }
			}
		}
	}
	# Otherwise we proceed as normal
	else = {
		$TITLE_RECEIVER$ = {
			# Change our government according to the government of our liege.
			## Nomad.
			if = {
				limit = {
					OR = {
						government_has_flag = government_is_nomadic
						scope:new_liege ?= { government_has_flag = government_is_nomadic }
						scope:old_government ?= flag:nomad
						scope:new_capital.title_province = {
							OR = {
								has_holding_type = nomad_holding
								has_holding_type = herder_holding
							}
						}
					}
				}
				hidden_effect = {
					every_held_title = {
						limit = {
							tier = tier_county
							title_province = { has_holding_type = herder_holding }
						}
						title_province = { set_holding_type = nomad_holding }
					}
				}
				change_government = nomad_government
			}
			## Soryo.
			else_if = {
				limit = {
					OR = {
						scope:new_liege ?= { government_has_flag = government_is_japan_feudal }
						scope:old_government ?= flag:japan_feudal
					}
				}
				change_government = japan_feudal_government
			}
			## Administrative.
			else_if = {
				limit = {
					OR = {
						scope:old_government ?= flag:admin
						scope:old_government ?= flag:celestial
						scope:old_government ?= flag:steppe_admin
						scope:old_government ?= flag:meritocratic
						scope:old_government ?= flag:japan_administrative
						scope:new_liege ?= { government_allows = administrative }
					}
				}
				change_to_administrative_effect = yes
			}
			## Clan.
			else_if = {
				limit = {
					OR = {
						scope:new_liege ?= { government_has_flag = government_is_clan }
						scope:old_government ?= flag:clan
						AND = {
							ep3_is_clan_inclined_trigger = yes
							top_liege = this
						}
					}
				}
				change_government = clan_government
			}
			## Tribal.
			else_if = {
				limit = {
					OR = {
						scope:new_liege ?= { government_has_flag = government_is_tribal }
						scope:old_government ?= flag:tribal
					}
				}
				change_government = tribal_government
			}
			## Wanua.
			else_if = {
				limit = {
					OR = {
						scope:new_liege ?= { government_has_flag = government_is_wanua }
						scope:old_government ?= flag:wanua
					}
				}
				change_government = wanua_government
			}
			## Mandala.
			else_if = {
				limit = {
					OR = {
						scope:new_liege ?= { government_has_flag = government_is_mandala }
						#scope:old_government ?= flag:mandala
						scope:target.holder = { government_has_flag = government_is_mandala }
					}
					faith = { has_doctrine = doctrine_polytheist }
				}
				change_government = mandala_government
				#Sort out the House shenanigans
				hidden_effect = {
					if = {
						limit = { is_house_head = no }
						house = { 
							if = {
								limit = {
									house_head = { 
										is_landed = no
										is_courtier_of = $TITLE_RECEIVER$
									}
								}
								set_house_head = $TITLE_RECEIVER$
							}
						}
					}
					else = {
						house ?= {
							set_house_aspiration = { type = no_aspect }
						}
					}
				}	
			}
			## Else feudal.
			else = {
				change_government = feudal_government
			}
		}
		# Ennobled adventurer penalties
		if = {
			limit = { $ENNOBLED_ADVENTURER$ = flag:yes }
			if = {
				limit = {
					calc_true_if = {
						amount = 3
						prestige_level <= 3
						scope:new_capital.culture ?= {
							NOR = {
								$TITLE_RECEIVER$.culture ?= this
								$TITLE_RECEIVER$.primary_spouse.culture ?= this
								$TITLE_RECEIVER$.primary_heir.culture ?= this
							}
						}
						scope:new_capital.faith != faith
					}
				}
				custom_tooltip = ep3_ennobled_adventurer_modifier_reason_3
				add_character_modifier = {
					modifier = ep3_ennobled_adventurer_modifier_3
					years = 30
				}
				custom_tooltip = ep3_ennobled_adventurer_modifier_rules
			}
			else_if = {
				limit = {
					calc_true_if = {
						amount = 2
						prestige_level <= 3
						scope:new_capital.culture ?= {
							NOR = {
								$TITLE_RECEIVER$.culture ?= this
								$TITLE_RECEIVER$.primary_spouse.culture ?= this
								$TITLE_RECEIVER$.primary_heir.culture ?= this
							}
						}
						scope:new_capital.faith != faith
					}
				}
				custom_tooltip = ep3_ennobled_adventurer_modifier_reason_2
				add_character_modifier = {
					modifier = ep3_ennobled_adventurer_modifier_2
					years = 20
				}
				custom_tooltip = ep3_ennobled_adventurer_modifier_rules
			}
			else_if = {
				limit = {
					calc_true_if = {
						amount = 1
						prestige_level <= 3
						scope:new_capital.culture ?= {
							NOR = {
								$TITLE_RECEIVER$.culture ?= this
								$TITLE_RECEIVER$.primary_spouse.culture ?= this
								$TITLE_RECEIVER$.primary_heir.culture ?= this
							}
						}
						scope:new_capital.faith != faith
					}
				}
				custom_tooltip = ep3_ennobled_adventurer_modifier_reason_1
				add_character_modifier = {
					modifier = ep3_ennobled_adventurer_modifier_1
					years = 10
				}
				custom_tooltip = ep3_ennobled_adventurer_modifier_rules
			}
		}
	}
	destroy_laamp_effect = { ADVENTURER = $TITLE_RECEIVER$ }
	# Setup NF if valid
	if = {
		limit = { government_allows = administrative }
		give_new_noble_family_title_effect = yes
	}
}

ep3_become_landed_cleanup_effect = {
	if = {
		limit = {
			exists = scope:supporter
			highest_held_title_tier < scope:supporter.highest_held_title_tier
		}
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = change
			add_claim_on_loss = no
		}
		change_liege = {
			liege = scope:supporter
			change = scope:change
		}
		resolve_title_and_vassal_change = scope:change
		# Message for nearby rulers
		create_landed_ruler_message_effect = {
			LANDED = scope:attacker
			FLAVOR_CHAR = scope:supporter
			FLAVOR_TITLE = scope:attacker.primary_title
			REASON = flag:conquest_support
		}
	}
	else = {
		if = {
			limit = {
				top_liege != this
				NOT = { exists = scope:new_liege }
			}
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
				add_claim_on_loss = no
			}
			becomes_independent = { change = scope:change }
			resolve_title_and_vassal_change = scope:change
		}
		# Message for nearby rulers
		create_landed_ruler_message_effect = {
			LANDED = scope:attacker
			FLAVOR_CHAR = scope:defender
			FLAVOR_TITLE = scope:attacker.primary_title
			REASON = flag:conquest
		}
	}
}

ep3_become_landed_warning_effect = {
	$TITLE$ = { save_scope_as = title }
	$TITLE_RECEIVER$ = { custom_tooltip = seize_realm_warning }
}

purchase_land_interaction_effect = {
	# scope:actor = adventurer
	# scope:recipient = seller
	# purchased_titles = titles

	hidden_effect = {
		scope:actor = {
			if = {
				limit = { character:90028 ?= this }
				add_character_flag = hereward_settled_flag
			}
			if = {
				limit = {
					NOT = { has_trait = adventurer }
				}
				add_trait = adventurer
			}
			every_courtier = {
				set_variable = {
					name = former_camp_leader
					value = scope:actor
				}
				add_trait = adventurer_follower
			}
		}
	}

	# Determine hierarchy
	ep3_become_landed_save_liege_effect = {
		TITLE_GIVER = scope:recipient
		ALWAYS_INDEPENDENT = no
		TITLE_LIST = purchased_titles
	}
	# Purchase land specific
	scope:actor = {
		show_as_tooltip = {
			pay_short_term_gold = {
				gold = scope:purchase_land_cost
				target = scope:recipient
			}
		}
	}
	# Resolve title, liege, government changes
	ep3_become_landed_transfer_effect = {
		TITLE_RECEIVER = scope:actor
		TITLE_LIST = purchased_titles
		TYPE = granted
		REASON = flag:purchased
		ENNOBLED_ADVENTURER = flag:yes
	}
	# Extra purchase land specific
	scope:actor = {
		stress_impact = {
			greedy = minor_stress_impact_gain
			lazy = minor_stress_impact_loss
		}
	}
	# Effects on land giver
	scope:recipient = { add_legitimacy = scope:purchase_land_legitimacy_cost }
	# Messages for nearby players
	create_landed_ruler_message_effect = {
		LANDED = scope:actor
		FLAVOR_CHAR = scope:recipient
		FLAVOR_TITLE = scope:target
		REASON = flag:purchased
	}
	# Hook
	if = {
		limit = { scope:offer_hook = yes }
		scope:recipient = {
			add_hook = {
				type = obligation_hook
				target = scope:actor
			}
		}
	}
	#Handle the government types
	if = {
		limit = {
			scope:recipient = { government_has_flag = government_is_mandala }
			#... we're lenient
			faith = { has_doctrine = doctrine_polytheist }
		}
		#Sort out the House shenanigans
		if = {
			limit = { is_house_head = no }
			house = { 
				if = {
					limit = {
						house_head = { 
							is_landed = no
							is_courtier_of = root
						}
					}
					set_house_head = root
				}
			}
		}
		else = {
			house ?= {
				set_house_aspiration = { type = no_aspect }
			}
		}	
	}
	#Otherwise go vanilla
	else = {
		if = {
			limit = { ep3_is_clan_inclined_trigger = yes }
			change_government = clan_government
		}
		else = { change_government = feudal_government }
	}
}

purchase_land_interaction_shortened_effect = {
	scope:actor = {
		if = {
			limit = { character:90028 ?= this }
			custom_tooltip = ep3_story_cycle_harrying.3053.b.tt
			add_character_flag = hereward_settled_flag
		}
		show_as_tooltip = {
			pay_short_term_gold = {
				gold = scope:purchase_land_cost
				target = scope:recipient
			}
		}
		hidden_effect = {
			scope:recipient = { add_gold = scope:purchase_land_cost }
		}
	}
	scope:actor = {
		stress_impact = {
			humble = minor_stress_impact_gain
			lazy = minor_stress_impact_loss
		}
		if = {
			limit = {
				scope:target.tier = tier_duchy
			}
			custom_tooltip = purchase_land_interaction_shortened_effect_duchy_desc
		}
		else = {
			custom_tooltip = purchase_land_interaction_shortened_effect_county_desc
		}
		if = {
			limit = {
				scope:target.tier = tier_duchy
			}
			scope:target.title_capital_county = {
				save_scope_as = new_primary_title
			}
		}
		else = {
			scope:target = {
				save_scope_as = new_primary_title
			}
		}
		if = {
			limit = {
				calc_true_if = {
					amount = 3
					prestige_level <= 3
					OR = {
						scope:new_primary_title.culture != culture
						primary_spouse ?= {
							culture != scope:new_primary_title.culture
						}
						primary_heir ?= {
							culture != scope:new_primary_title.culture
						}
					}
					scope:new_primary_title.faith != faith
				}
			}
			custom_tooltip = ep3_ennobled_adventurer_modifier_reason_3
			add_character_modifier = {
				modifier = ep3_ennobled_adventurer_modifier_3
				years = 30
			}
			custom_tooltip = ep3_ennobled_adventurer_modifier_rules
		}
		else_if = {
			limit = {
				calc_true_if = {
					amount = 2
					prestige_level <= 3
					OR = {
						scope:new_primary_title.culture != culture
						primary_spouse ?= {
							culture != scope:new_primary_title.culture
						}
						primary_heir ?= {
							culture != scope:new_primary_title.culture
						}
					}
					scope:new_primary_title.faith != faith

				}
			}
			custom_tooltip = ep3_ennobled_adventurer_modifier_reason_2
			add_character_modifier = {
				modifier = ep3_ennobled_adventurer_modifier_2
				years = 20
			}
			custom_tooltip = ep3_ennobled_adventurer_modifier_rules
		}
		else_if = {
			limit = {
				calc_true_if = {
					amount = 1
					prestige_level <= 3
					OR = {
						scope:new_primary_title.culture != culture
						primary_spouse ?= {
							culture != scope:new_primary_title.culture
						}
						primary_heir ?= {
							culture != scope:new_primary_title.culture
						}
					}
					scope:new_primary_title.faith != faith

				}
			}
			custom_tooltip = ep3_ennobled_adventurer_modifier_reason_1
			add_character_modifier = {
				modifier = ep3_ennobled_adventurer_modifier_1
				years = 10
			}
			custom_tooltip = ep3_ennobled_adventurer_modifier_rules
		}

	}
}

procure_estate_interaction_effect = {
	if = {
		limit = { scope:piety_option = yes }
		if = {
			limit = { faith != scope:recipient.faith }
			set_character_faith = scope:recipient.faith
			add_character_flag = {
				flag = procure_estate_conversion
				months = 1
			}
		}
		scope:recipient = {
			add_piety = {
				value = procure_estate_interaction_cost_base_piety
				multiply = 0.5
			}
		}
	}
	else_if = {
		limit = { scope:gold_option = yes }
		show_as_tooltip = {
			pay_short_term_gold = {
				target = scope:recipient
				gold = procure_estate_interaction_cost_base_gold
			}
		}
		hidden_effect = {
			scope:recipient = { add_gold = procure_estate_interaction_cost_base_gold }
		}
	}
	else_if = {
		limit = { scope:hook = yes }
		scope:recipient = {
			add_hook = {
				target = scope:actor
				type = favor_hook
			}
		}
	}
	else = {
		scope:recipient = {
			add_prestige = {
				value = procure_estate_interaction_cost_base_prestige
				multiply = 0.25
			}
		}
	}
	scope:recipient = { change_influence = medium_influence_loss }
	#Scope:actor becomes ladmin
	scope:actor = {
		hidden_effect = {
			# Liquidate camp
			laamp_clear_domicile_buildings_liquidator_effect = yes
		}
		if = {
			limit = {
				scope:actor.house ?= {
					any_house_member = {
						any_held_title = { is_noble_family_title = yes }
						top_liege = scope:recipient.top_liege
					}
				}
			}
			found_cadet_house_decision_effect = {
				CHARACTER = scope:actor
				PRESTIGE = major_prestige_gain
			}
		}
		# Note our courtiers so that they don't escape.
		every_courtier = { add_to_list = courtiers_list }
		# Change liege
		destroy_laamp_effect = { ADVENTURER = scope:actor }
		hidden_effect  = {
			scope:recipient.top_liege = {
				add_courtier = scope:actor
				return_to_court = yes
			}
		}
		create_noble_family_effect = { GOVERNMENT_GIVER = scope:recipient }
		hidden_effect = {
			# Return any escaped courtiers.
			every_in_list = {
				list = courtiers_list
				limit = {
					NOT = { liege_or_court_owner = scope:actor }
				}
				scope:actor = { add_courtier = prev }
			}
			# And boot any new autogenned arrivals.
			every_courtier = {
				limit = {
					is_lowborn = yes
					NOR = {
						is_in_list = courtiers_list
						is_close_or_extended_family_of = scope:actor
						is_consort_of = scope:actor
						has_important_relationship_trigger = { CHARACTER = scope:actor }
					}
				}
				silent_disappearance_effect = yes
			}
		}
		create_title_and_vassal_change = {
			type = swear_fealty
			add_claim_on_loss = no
			save_scope_as = title_change
		}
		change_liege = {
			liege = scope:recipient.top_liege
			change = scope:title_change
		}
		resolve_title_and_vassal_change = scope:title_change
		hidden_effect = {
			domicile ?= {
				if = {
					limit = {
						NOT = { domicile_location = scope:recipient.capital_province }
					}
					move_domicile = scope:recipient.capital_province
				}
			}
		}
		add_character_modifier = {
			modifier = ep3_administrative_outsider_modifier
			years = 15
		}
	}
	scope:recipient.top_liege = {
		if = {
			limit = {
				is_ai = no
			}
			send_interface_message = {
				type = msg_landless_adventurer_nearby
				title = procure_estate_message_title
				left_icon = scope:actor
				right_icon = scope:recipient
				custom_tooltip = procure_estate_message_tt
			}
		}
		every_vassal_or_below = {
			limit = {
				is_ai = no
				NOT = { this = scope:actor }
			}
			send_interface_message = {
				type = msg_landless_adventurer_nearby
				title = procure_estate_message_title
				left_icon = scope:actor
				right_icon = scope:recipient
				custom_tooltip = procure_estate_message_tt
			}
		}
	}
}

negotiate_settlement_interaction_effect = {
	# scope:actor = offerer
	# scope:recipient = adventurer
	# offered_titles = titles

	hidden_effect = {
		scope:recipient = {
			if = {
				limit = {
					NOT = { has_trait = adventurer }
				}
				add_trait = adventurer
			}
			every_courtier = {
				set_variable = {
					name = former_camp_leader
					value = scope:recipient
				}
				add_trait = adventurer_follower
			}
		}
	}

	# Determine hierarchy
	ep3_become_landed_save_liege_effect = {
		TITLE_GIVER = scope:actor
		ALWAYS_INDEPENDENT = no
		TITLE_LIST = offered_titles
	}
	# Negotiate settlement specific
	scope:recipient = {
		# End relevant wars
		every_character_war = {
			limit = {
				OR = {
					primary_attacker = scope:recipient
					primary_defender = scope:recipient
				}
			}
			end_war = white_peace
		}
		# Remove from relevant wars
		every_character_war = {
			limit = {
				NOR = {
					primary_attacker = scope:recipient
					primary_defender = scope:recipient
				}
				any_war_defender = {
					OR = {
						this = scope:actor
						this.top_liege = scope:actor.top_liege
					}
				}
			}
			remove_participant = scope:recipient
		}
	}
	# Resolve title, liege, government changes
	ep3_become_landed_transfer_effect = {
		TITLE_RECEIVER = scope:recipient
		TITLE_LIST = offered_titles
		TYPE = granted
		REASON = flag:negotiated
		ENNOBLED_ADVENTURER = flag:yes
	}
	# Effects on land giver
	scope:actor = {
		add_legitimacy = scope:negotiate_settlement_legitimacy_cost
		stress_impact = {
			greedy = minor_stress_impact_gain
			vengeful = minor_stress_impact_gain
		}
	}
	# Messages for nearby players
	create_landed_ruler_message_effect = {
		LANDED = scope:recipient
		FLAVOR_CHAR = scope:actor
		FLAVOR_TITLE = scope:new_primary_title
		REASON = flag:negotiated
	}
	# And, just in case it's failed, make sure that we've vassalised our new friend.
	hidden_effect = {
		if = {
			limit = {
				scope:recipient = {
					highest_held_title_tier < scope:actor.highest_held_title_tier
					top_liege = this
				}
			}
			create_title_and_vassal_change = {
				type = swear_fealty
				save_scope_as = change
			}
			scope:recipient = {
				change_liege = {
					liege = scope:actor
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
}

create_landed_ruler_message_effect = {
	hidden_effect = {
		$LANDED$ = { save_scope_as = landed }
		$FLAVOR_CHAR$ = { save_scope_as = landed_flavor_char }
		$FLAVOR_TITLE$ = { save_scope_as = landed_flavor_title }
		save_scope_value_as = {
			name = landed_creation_reason
			value = $REASON$
		}
		every_player = {
			limit = {
				NOR = {
					this = scope:landed
					this = scope:landed_flavor_char
				}
				top_liege = scope:landed.top_liege
			}
			send_interface_message = {
				type = msg_landless_adventurer_nearby
				title = nearby_landed_ruler_created_message_title
				left_icon = scope:landed
				right_icon = scope:landed_flavor_char
				switch = {
					trigger = scope:landed_creation_reason
					flag:purchased = { custom_tooltip = nearby_landed_ruler_created_message_purchased_tt }
					flag:negotiated = { custom_tooltip = nearby_landed_ruler_created_message_negotiated_tt }
					flag:seized_pastureland = { custom_tooltip = nearby_landed_ruler_created_message_seized_pastureland_tt }
					flag:conquest_supporter = { custom_tooltip = nearby_landed_ruler_created_message_conquest_support_tt }
					flag:conquest = { custom_tooltip = nearby_landed_ruler_created_message_conquest_tt }
					flag:seize_realm = { custom_tooltip = nearby_landed_ruler_created_message_seize_realm_tt }
					flag:culture_champion = { custom_tooltip = nearby_landed_ruler_created_message_culture_champion_tt }
					fallback = { custom_tooltip = nearby_landed_ruler_created_message_fallback_tt }
				}
			}
		}
	}
}

conquest_supporter_memory_variable_effect = {
	set_variable = {
		name = ascended_throne_reason
		value = flag:conquest_supporter
		weeks = 1
	}
	set_variable = {
		name = ascended_throne_extra
		value = scope:supporter
		weeks = 1
	}
}

ep3_laamp_supporter_invalidated_effect = {
	if = {
		limit = { exists = var:adventurer_invasion_supporter }
		save_scope_as = adventurer
		var:adventurer_invasion_supporter ?= { save_scope_as = supporter }
		var:adventurer_invasion_target ?= { save_scope_as = war_target }
	}
	else_if = {
		limit = { exists = var:adventurer_invasion_supportee }
		var:adventurer_invasion_supportee ?= { save_scope_as = adventurer }
		var:adventurer_invasion_target ?= { save_scope_as = target }
		save_scope_as = supporter
	}
	if = {
		limit = {
			exists = scope:adventurer
			exists = scope:supporter
			exists = scope:war_target
		}
		scope:adventurer ?= {
			send_interface_message = {
				type = event_landless_adventurer_neutral
				title = ep3_laamp_sponsor_invalidated_title
				left_icon = scope:supporter
				right_icon = scope:war_target
				remove_variable = adventurer_invasion_supporter
				remove_variable = adventurer_invasion_target
				custom_tooltip = ep3_laamp_sponsor_invalidated_adventurer
			}
		}
		scope:supporter ?= {
			send_interface_message = {
				type = event_landless_adventurer_neutral
				title = ep3_laamp_sponsor_invalidated_title
				left_icon = scope:attacker
				right_icon = scope:war_target
				remove_variable = adventurer_invasion_supportee
				remove_variable = adventurer_invasion_target
				custom_tooltip = ep3_laamp_sponsor_invalidated_supporter
				if = {
					limit = { exists = var:adventurer_invasion_join_war }
					set_variable = {
						name = adventurer_invasion_join_war
						value = scope:actor
						years = 5
					}
				}
				if = {
					limit = { exists = var:adventurer_invasion_request_soldiers }
					set_variable = {
						name = adventurer_invasion_request_soldiers
						value = scope:actor
						years = 5
					}
				}
			}
		}
	}
}

ep3_laamp_support_variable_cleanup_effect = {
	if = {
		limit = { exists = var:adventurer_invasion_supporter }
		save_scope_as = adventurer
		var:adventurer_invasion_supporter ?= { save_scope_as = supporter }
		var:adventurer_invasion_target ?= { save_scope_as = war_target }
	}
	else_if = {
		limit = { exists = var:adventurer_invasion_supportee }
		var:adventurer_invasion_supportee ?= { save_scope_as = adventurer }
		var:adventurer_invasion_target ?= { save_scope_as = war_target }
		save_scope_as = supporter
	}
	scope:adventurer ?= {
		remove_variable = adventurer_invasion_supporter
		remove_variable = adventurer_invasion_target
	}
	scope:supporter ?= {
		remove_variable = adventurer_invasion_supportee
		remove_variable = adventurer_invasion_target
	}
}

seize_realm_scheme_success_effect = {
	# scope:target = ruler
	# scope:owner = adventurer

	hidden_effect = {
		scope:owner = {
			if = {
				limit = {
					NOT = { has_trait = adventurer }
				}
				add_trait = adventurer
			}
			every_courtier = {
				set_variable = {
					name = former_camp_leader
					value = scope:owner
				}
				add_trait = adventurer_follower
			}
		}
	}

	scope:target = {
		save_scope_as = background_dungeon_scope
		save_scope_as = background_courtyard_scope
		save_scope_as = background_throne_room_scope
		every_held_title = {
			limit = {
				is_leased_out = no
				is_landless_type_title = no
				is_head_of_faith = no
			}
			add_to_list = seized_titles
		}
		ordered_in_list = {
			list = seized_titles
			order_by = tier
			save_scope_as = realm_title
		}
	}
	# Determine independent vassals
	adventurer_realm_destabilisation_list_effect = {
		TITLE_GIVER = scope:target
		TITLE_RECEIVER = scope:owner
		TITLE = scope:target.primary_title
	}
	# Determine hierarchy
	ep3_become_landed_save_liege_effect = {
		TITLE_GIVER = scope:target
		ALWAYS_INDEPENDENT = no
		TITLE_LIST = seized_titles
	}
	# Resolve title, liege, government changes
	ep3_become_landed_transfer_effect = {
		TITLE_RECEIVER = scope:owner
		TITLE_LIST = seized_titles
		TYPE = usurped
		REASON = flag:seize_realm
		ENNOBLED_ADVENTURER = flag:yes
	}
	# Old ruler's fate
	if = {
		limit = { exists = scope:death }
		scope:target = {
			death = { death_reason = death_fight }
		}
	}
	else_if = {
		limit = { exists = scope:imprison }
		scope:target = { add_character_flag = was_abducted_block_notification_event }
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:target
			IMPRISONER = scope:owner
		}
	}
	add_clan_unity_interaction_effect = {
		CHARACTER = scope:owner
		TARGET = scope:target
		VALUE = medium_unity_loss
		DESC = clan_unity_seize_realm_attempted.desc
		REVERSE_NON_HOUSE_TARGET = no
	}
	successful_seize_realm_opinion_effect = {
		VICTIM = scope:target
		PLOTTER = scope:owner
	}
	# Vassals break away
	adventurer_realm_destabilisation_transfer_effect = {
		TITLE_RECEIVER = scope:owner
		TITLE_GIVER = scope:target
		TITLE = scope:realm_title
		TITLE_LIST = seized_titles
	}
	# Messages for nearby players
	create_landed_ruler_message_effect = {
		LANDED = scope:owner
		FLAVOR_CHAR = scope:target
		FLAVOR_TITLE = scope:realm_title
		REASON = flag:seize_realm
	}
	scope:owner = {
		create_character_memory = {
			type = seized_realm_memory
			participants = { deposed = scope:target }
		}
		ordered_memory = {
			memory_type = seized_realm_memory
			order_by = memory_creation_date
			if = {
				limit = {
					NOT = { exists = var:realm }
				}
				set_variable = {
					name = realm
					value = scope:realm_title
				}
			}
		}
	}
	scope:target = {
		create_character_memory = {
			type = realm_seized_memory
			participants = { deposer = scope:owner }
		}
		ordered_memory = {
			memory_type = realm_seized_memory
			order_by = memory_creation_date
			if = {
				limit = {
					NOT = { exists = var:realm }
				}
				set_variable = {
					name = realm
					value = scope:realm_title
				}
			}
		}
	}
}

seize_realm_scheme_failure_effect = {
	scope:scheme = { end_scheme = yes }
 	#Add Watchful Modifier to the target
	scope:target = {
		save_scope_as = background_dungeon_scope
		save_scope_as = background_courtyard_scope
		save_scope_as = background_throne_room_scope
		ordered_held_title = {
			limit = {
				is_leased_out = no
				is_landless_type_title = no
				is_head_of_faith = no
			}
			order_by = tier
			save_scope_as = realm_title
		}
		show_as_tooltip = {
			add_character_modifier = {
				modifier = watchful_modifier
				days = watchful_modifier_duration
			}
		}
	}
	# Old ruler's fate
	if = {
		limit = { exists = scope:death }
		scope:owner = {
			death = { death_reason = death_fight }
		}
	}
	else = {
		scope:owner = {
			create_character_memory = {
				type = seized_realm_failed_memory
				participants = { deposed = scope:target }
			}
			ordered_memory = {
				memory_type = seized_realm_failed_memory
				order_by = memory_creation_date
				if = {
					limit = {
						NOT = { exists = var:realm }
					}
					set_variable = {
						name = realm
						value = scope:realm_title
					}
				}
			}
		}
		if = {
			limit = { exists = scope:imprison }
			scope:owner = { add_character_flag = was_abducted_block_notification_event }
			rightfully_imprison_character_less_verbose_effect = {
				TARGET = scope:owner
				IMPRISONER = scope:target
			}
			hidden_effect = { change_prison_type = dungeon }
		}
	}
	attempted_seize_realm_opinion_effect = {
		VICTIM = scope:target
		PLOTTER = scope:owner
	}
	# If we're a clan this affects unity
	add_clan_unity_interaction_effect = {
		CHARACTER = scope:owner
		TARGET = scope:target
		VALUE = medium_unity_loss
		DESC = clan_unity_seize_realm_attempted.desc
		REVERSE_NON_HOUSE_TARGET = no
	}
	scope:target = {
		create_character_memory = {
			type = realm_seized_failed_memory
			participants = { deposer = scope:owner }
		}
		ordered_memory = {
			memory_type = realm_seized_failed_memory
			order_by = memory_creation_date
			if = {
				limit = {
					NOT = { exists = var:realm }
				}
				set_variable = {
					name = realm
					value = scope:realm_title
				}
			}
		}
	}
}

successful_seize_realm_opinion_effect = {
	$VICTIM$ = {
		if = {
			limit = { is_alive = yes }
			add_opinion = {
				target = $PLOTTER$
				modifier = seized_my_realm_crime
			}
			set_relation_rival = {
				target = scope:owner
				reason = rival_seized_realm_reason
			}
		}
		every_spouse = {
			limit = {
				NOR = {
					this = $PLOTTER$
					is_spouse_of = $PLOTTER$
					is_close_family_of = $PLOTTER$
				}
			}
			add_to_temporary_list = close_family_seize_realm_opinion_list
		}
		every_close_family_member = {
			limit = {
				NOR = {
					this = $PLOTTER$
					is_spouse_of = $PLOTTER$
					is_close_family_of = $PLOTTER$
				}
			}
			add_to_temporary_list = close_family_seize_realm_opinion_list
		}
		every_in_list = {
			list = close_family_seize_realm_opinion_list
			custom = all_close_family_and_spouses
			add_opinion = {
				target = $PLOTTER$
				modifier = seized_close_family_realm_crime
			}
		}
	}
}

attempted_seize_realm_opinion_effect = {
	$VICTIM$ = {
		if = {
			limit = { is_alive = yes }
			add_opinion = {
				target = $PLOTTER$
				modifier = attempted_to_seize_my_realm_crime
			}
		}
		every_spouse = {
			limit = { NOT = { this = $PLOTTER$ } }
			add_to_temporary_list = close_family_seize_realm_opinion_list
		}
		every_close_family_member = {
			limit = { NOT = { this = $PLOTTER$ } }
			add_to_temporary_list = close_family_seize_realm_opinion_list
		}

		if = {
			limit = {
				any_in_list = { list = close_family_seize_realm_opinion_list }
			}
			every_in_list = {
				list = close_family_seize_realm_opinion_list
				custom = all_close_family_and_spouses
				add_opinion = {
					target = $PLOTTER$
					modifier = attempted_to_seize_realm_close_family_crime
				}
			}
		}

	}
}

adventurer_realm_destabilisation_list_effect = {
	$TITLE_RECEIVER$ = { save_scope_as = adventurer }
	$TITLE$ = { save_scope_as = title }
	$TITLE_GIVER$ = {
		save_scope_as = ruler
		# Determine vassal sentiment - calculated before ruler change
		hidden_effect = {
			every_vassal = {
				limit = {
					highest_held_title_tier >= tier_county
					save_temporary_scope_as = vassal_temp
					# Only vassals who will not *still* be de jure vassals of the title loser via other titles
					trigger_if = {
						limit = {
							$TITLE_GIVER$ = {
								any_held_title = {
									tier >= scope:title.tier
									this != scope:title
								}
							}
						}
						any_held_title = {
							target_is_de_jure_liege_or_above = scope:title
						}
					}
					trigger_if = {
						limit = {
							government_allows = administrative
						}
						scope:title = {
							OR = {
								AND = {
									scope:ruler.primary_title.tier = tier_kingdom
									tier < tier_kingdom
								}
								tier < tier_empire
							}
						}
					}
				}
				# Calculate how likely are they to not become your vassal
				random = {
					chance = 0
					### FAITH/CULTURE
					modifier = { # Cultural acceptance differences
						add = {
							value = "culture.cultural_acceptance(scope:adventurer.culture)"
							subtract = 100
							abs = yes
							divide = 5
						}
						culture = {
							this != scope:adventurer.culture
							cultural_acceptance = {
								target = scope:adventurer.culture
								value < 50
							}
							OR = {
								this = scope:ruler.culture
								"cultural_acceptance(scope:ruler.culture)" > "cultural_acceptance(scope:adventurer.culture)"
							}
						}
					}
					modifier = { # Zealous of other faiths more likely
						add = {
							value = ai_zeal
							multiply = 0.15
							multiply = "scope:vassal_temp.faith.faith_hostility_level(scope:adventurer.faith)"
							max = 25
						}
						ai_zeal > low_positive_zeal
						faith = {
							this != scope:adventurer.faith
							"faith_hostility_level(scope:adventurer.faith)" > 0
						}
					}
					modifier = { # Theocatic Vassals of different faith more likely
						factor = 2
						has_government = theocracy_government
						faith != scope:adventurer.faith
					}
					### DISTANCE
					modifier = { # Distant vassals more likely
						add = {
							value = 0
							add = scope:distance
							if = {
								limit = { scope:distance >= squared_distance_medium }
								add = scope:distance
								multiply = 0.0001
							}
							max = 25
						}
						"capital_county.squared_distance(scope:ruler.capital_county)" >= squared_distance_medium
						capital_county = {
							save_temporary_scope_value_as = {
								name = distance
								value = "squared_distance(scope:ruler.capital_county)"
							}
						}
					}
					### PERSONALITY
					ai_value_modifier = {
						ai_boldness = 0.15
						ai_energy = 0.15
						ai_greed = 0.15
						ai_honor = -0.15
					}
					### OPINION
					opinion_modifier = { # More likely if dislikes adventurer
						opinion_target = scope:adventurer
						multiplier = -0.25
					}
					opinion_modifier = { # More likely if liked old ruler
						opinion_target = scope:ruler
						multiplier = 0.1
					}
					### STRENGTH
					modifier = { # Small vassals less likely
						add = -15
						tier_difference = {
							target = scope:ruler
							value <= -2
						}
					}
					modifier = {
						add = -25
						realm_size < tiny_realm_size
					}
					modifier = { # Powerful vassals more likely
						factor = 1.25
						is_powerful_vassal = yes
					}
					modifier = { # Powerful vassals more likely
						add = {
							value = highest_held_title_tier
							multiply = 5
						}
					}
					### RELEVANCE
					modifier = { # Fringe vassals more likely
						factor = 1.25
						any_sub_realm_county = {
							any_neighboring_county = { holder.top_liege != scope:ruler }
						}
					}
					modifier = { # Core vassals less likely
						factor = 0.75
						scope:ruler.capital_county.kingdom = {
							any_de_jure_county = { this = scope:vassal_temp.capital_county }
						}
					}
					### FAMILY
					modifier = { # Relatives of deposed more likely
						factor = 1.1
						is_close_family_of = scope:ruler
						NOT = { is_close_family_of = scope:adventurer }
					}
					modifier = { # Dynasts of deposed more likely
						factor = 1.1
						dynasty ?= {
							this = scope:ruler.dynasty
							this != scope:adventurer.dynasty
						}
					}
					modifier = { # Relatives of adventurer less likely
						factor = 0.75
						is_close_family_of = scope:adventurer
						NOT = { is_close_family_of = scope:ruler }
					}
					modifier = { # Dynasts of adventurer less likely
						factor = 0.75
						dynasty ?= {
							this = scope:adventurer.dynasty
							this != scope:ruler.dynasty
						}
					}
					### HOOKS
					modifier = {
						factor = 0
						scope:adventurer = { has_hook = scope:vassal_temp }
					}
					modifier = {
						factor = 2
						scope:ruler = { has_hook = scope:vassal_temp }
					}
					### TIER
					modifier = {
						factor = 1.5
						scope:ruler.primary_title.tier = tier_empire
					}
					modifier = {
						factor = 1.25
						scope:ruler.primary_title.tier = tier_kingdom
					}
					max = 95
					add_to_list = independent_vassals
				}
			}
			# Save heirs for claimant wars
			scope:title = {
				every_title_heir = {
					limit = {
						save_temporary_scope_as = heir_temp
						scope:title = {
							place_in_line_of_succession = {
								target = scope:heir_temp
								value <= 5
							}
						}
					}
					add_to_list = heirs
				}
			}
		}
	}
}

adventurer_realm_destabilisation_civil_war_effect = {
	$TITLE_GIVER$ = { save_scope_as = ruler }
	$TITLE_RECEIVER$ = { save_scope_as = adventurer }
	$TITLE$ = { save_scope_as = title }
	if = {
		limit = {
			any_in_list = { list = independent_vassals }
		}
		scope:ruler = {
			if = {
				limit = { is_alive = yes }
				save_scope_as = claimant
			}
			else_if = {
				limit = {
					any_in_list = { list = heirs }
				}
				random_in_list = {
					list = heirs
					limit = { has_strong_claim_on = scope:realm }
					alternative_limit = { has_weak_claim_on = scope:realm }
					weight = {
						base = 1
						modifier = { add = age }
					}
					save_scope_as = claimant
				}
			}
			else_if = {
				limit = {
					scope:title = {
						any_claimant = {
							this != scope:adventurer
						}
					}
				}
				scope:title = {
					random_claimant = {
						limit = { has_strong_claim_on = scope:realm }
						alternative_limit = { has_weak_claim_on = scope:realm }
						weight = {
							base = 1
							modifier = { add = age }
						}
						save_scope_as = claimant
					}
				}
			}
			else = {
				ordered_in_list = {
					list = independent_vassals
					order_by = max_military_strength
					save_scope_as = claimant
				}
			}
		}
		# Vassals Breakaway
		ordered_in_list = {
			list = independent_vassals
			limit = {
				can_create_faction = {
					type = claimant_faction
					target = scope:adventurer
				}
			}
			order_by = max_military_strength
			save_scope_as = revolt_leader
			create_faction = {
				type = claimant_faction
				target = scope:adventurer
			}
			joined_faction = {
				save_scope_as = faction
				set_special_character = scope:claimant
				set_special_title = scope:realm
			}
		}
		every_in_list = {
			list = independent_vassals
			limit = {
				this != scope:revolt_leader
				can_join_faction = scope:faction
			}
			join_faction = scope:faction
		}
		scope:faction = {
			faction_start_war = { title = scope:realm }
		}
	}
}

adventurer_realm_destabilisation_transfer_effect = {
	$TITLE_RECEIVER$ = { save_scope_as = adventurer }
	$TITLE_GIVER$ = { save_scope_as = ruler }
	$TITLE$ = { save_scope_as = title }
	if = {
		limit = {
			any_in_list = { list = independent_vassals }
		}
		# Vassals Breakaway
		create_title_and_vassal_change = {
			type = independency
			add_claim_on_loss = yes
			save_scope_as = change
		}
		hidden_effect = {
			every_in_list = {
				list = independent_vassals
				save_temporary_scope_as = vassal_temp
				if = {
					limit = {
						scope:adventurer = { top_liege = this }
						scope:ruler = {
							NOT = {
								any_held_title = {
									this != scope:title
									any_in_de_jure_hierarchy = { this = scope:vassal_temp.primary_title }
								}
							}
						}
					}
					becomes_independent = { change = scope:change }
				}
				else_if = {
					limit = {
						scope:ruler = {
							NOT = {
								any_held_title = {
									this != scope:title
									any_in_de_jure_hierarchy = { this = scope:vassal_temp.primary_title }
								}
							}
						}
					}
					change_liege = {
						liege = scope:adventurer.liege
						change = scope:change
					}
				}
				add_opinion = {
					modifier = seized_lieges_realm_crime
					target = scope:adventurer
				}
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
	create_title_and_vassal_change = {
		type = conquest
		add_claim_on_loss = yes
		save_scope_as = change
	}
	hidden_effect = {
		every_in_list = {
			list = $TITLE_LIST$
			limit = { tier >= tier_county }
			custom_tooltip = CONQUEST_CB_TITLE
			every_in_de_jure_hierarchy = {
				continue = { tier >= tier_duchy }
				limit = {
					tier >= tier_county
					holder ?= {
						OR = {
							this = scope:ruler
							target_is_liege_or_above = scope:ruler
						}
					}
				}
				save_temporary_scope_as = title_temp
				if = {
					limit = {
						holder != scope:adventurer
						OR = {
							holder = scope:ruler
							holder.highest_held_title_tier >= scope:title.tier
							scope:adventurer = { has_claim_on = scope:title_temp }
						}
					}
					add_to_temporary_list = titles_taken
				}
				else = {
					holder = {
						if = {
							limit = {
								liege = scope:ruler
								NOT = { is_in_list = independent_vassals }
							}
							add_to_temporary_list = vassals_taken
						}
					}
				}
			}
		}
	}
	hidden_effect = {
		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:adventurer
				change = scope:change
			}
		}
	}
	resolve_title_and_vassal_change = scope:change
}

player_adventurer_contract_generation_on_domicile_moved_effect = {
	if = {
		limit = { player_adventurer_sufficient_distance_for_contract_spawn_trigger = yes }
		save_temporary_scope_as = char_temp
		if = { # Explorers
			limit = { has_realm_law = camp_purpose_explorers }
			populate_task_contracts_for_area = {
				location = scope:char_temp.location
				amount = 1
				group = { laamp_contracts_transport_group }
			}
		}
		else_if = { # Mercenaries
			limit = { has_realm_law = camp_purpose_mercenaries }
			populate_task_contracts_for_area = {
				location = scope:char_temp.location
				amount = 1
				group = { laamp_contracts_war_group }
			}
			ordered_independent_ruler = {
				limit = {
					in_diplomatic_range = scope:char_temp
					scope:char_temp = {
						can_create_task_contract = {
							type_name = laamp_join_war_contract
							employer = prev
						}
					}
				}
				order_by = {
					value = 0
					subtract = "capital_county.squared_distance(scope:char_temp.domicile.domicile_location)"
				}
				ordered_character_war = {
					limit = {
						ep3_adventurer_mercenary_war_trigger = {
							EMPLOYER = prev
							LAAMP = scope:char_temp
						}
					}
					order_by = war_total_participants_current_strength_value
					primary_defender ?= { save_scope_as = defender }
					primary_attacker ?= { save_scope_as = attacker }
					save_scope_as = war
					scope:char_temp = {
						spawn_join_war_contracts_effect = {
							WAR = scope:war
							LAAMP = scope:char_temp
							ATTACKER = scope:attacker
							DEFENDER = scope:defender
						}
					}
				}
			}
			debug_log = "WAR_1"
			debug_log_scopes = yes
		}
		populate_location_with_contracts_effect = {
			AREA_CHAR = scope:char_temp
			AMOUNT = 3
		}
		# Log that we visited this place.
		add_to_variable_list = {
			name = laamp_recently_visited_kingdoms
			target = scope:kingdom_temp
			years = 5
		}
	}
}

player_adventurer_contract_generation_on_wait_in_place_effect = {
	if = {
		limit = { player_adventurer_sufficient_distance_for_contract_spawn_trigger = yes }
		# Track where we are.
		set_variable = {
			name = contract_passive_spawn_location
			value = location.kingdom
		}
		# Reset the tally.
		set_variable = {
			name = contract_passive_spawn_tally
			value = 0
		}
		# Queue up an additional contract.
		trigger_event = {
			id = ep3_laamps.9981
			months = { 4 6 }
		}
	}
}

contract_passive_spawn_effect = {
	#count how many contracts you already have
	every_character_task_contract = {
		add_to_list = num_of_contracts_before
	}
	# G'is a contract.
	populate_location_with_contracts_effect = {
		AREA_CHAR = root
		AMOUNT = $SPAWN_CONTRACTS$
	}
	#count them again to see if the effect above spawned something
	every_character_task_contract = {
		add_to_list = num_of_contracts_after
	}
	set_local_variable = {
		name = current_value
		value = list_size:num_of_contracts_before
	}
	# Make sure we don't get too many of them just sitting in one place.
	while = {
		limit = {
			local_var:current_value < list_size:num_of_contracts_after
		}
		increment_variable_effect = {
			VAR = contract_passive_spawn_tally
			VAL = 1
		}
		change_local_variable = {
			name = current_value
			add = 1
		}
	}
}

#spawns a valid task contract for a landless adventurer
# root = landless adventurer
# TASK_CONTRACT_TYPE = type name of a task contract you want to spawn
# SPAWN_POINT = where you want the task contract to appear on the map
laamp_spawn_valid_task_contract = {
	$SPAWN_POINT$ = {
		save_scope_as = spawn_location
	}
	random_ruler = {
		limit = {
			root = {
				can_create_task_contract = {
					type_name = $TASK_CONTRACT_TYPE$
					employer = prev
				}
			}
		}
		save_scope_as = employer
	}
	create_task_contract = {
		task_contract_type = $TASK_CONTRACT_TYPE$
		task_contract_tier = scope:employer.task_contract_tier_value
		location = scope:spawn_location
		task_contract_employer = scope:employer
	}
}

points_of_interest_tracker_effect = {
	if = {
		limit = {
			government_has_flag = government_is_landless_adventurer
			location = {
				save_temporary_scope_as = province_temp
				OR = {
					# Building flags
					has_any_point_of_interest_flag = yes
					# Capitals
					AND = {
						province_owner.top_liege.capital_province ?= this
						province_owner.top_liege ?= { highest_held_title_tier >= tier_kingdom }
					}
					# Grand Cities
					this = province:4828 #Baghdad
					this = province:2575 #Rome
					this = province:496 #Constantinople
					# Points of interest
					this = province:747 # Halikarnassos
					this = province:6053 # Alexandria
					# Legend stuff
					any_legend = {
						legend_owner.capital_province ?= scope:province_temp
						OR = {
							legend_quality = illustrious
							legend_quality = mythical
						}
					}
				}
			}
		}
		if = {
			limit = {
				NOT = {
					has_variable = poi_visited
				}
			}
			set_variable = {
				name = poi_visited
				value = 1
			}
		}
		else = {
			change_variable = {
				name = poi_visited
				add = 1
			}
		}
	}
}

zealous_missionary_conversion_effect = {
	if = {
		limit = {
			exists = location.barony.holder
			government_has_flag = government_is_landless_adventurer
			has_character_flag = zealous_missionary_timer
			location.county ?= { hasan_evangelize_province_trigger = yes }
		}
		trigger_event = ep3_laamp_decision_event.1103
	}
}

war_contracts_completed_tracker_effect = {
	if = {
		limit = {
			government_has_flag = government_is_landless_adventurer
			has_variable = war_contract_tracker
		}
		change_variable = {
			name = war_contract_tracker
			add = 1
		}
	}
	else_if = {
		limit = {
			government_has_flag = government_is_landless_adventurer
		}
		set_variable = {
			name = war_contract_tracker
			value = 1
		}
	}
}

bandit_king_maa_spawn_effect = {

	save_scope_as = bandit_king
	if = {
		limit = {
			has_character_modifier = bandit_king_modifier
		}
		set_variable = {
			name = maa_number
			value = 1
		}
		if = {
			limit = {
				scope:extra_reward ?= flag:yes
			}
			change_variable = {
				name = maa_number
				add = 2
			}
		}
		if = {
			limit = {
				calc_true_if = {
					amount >= 4
					has_trait_xp = {
						trait = gallowsbait
						track = bandit
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = marauder
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = poacher
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = thief
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = trickster
						value >= 60
					}
				}
			}
			change_variable = {
				name = maa_number
				add = 2
			}
		}
		else_if = {
			limit = {
				calc_true_if = {
					amount >= 2
					has_trait_xp = {
						trait = gallowsbait
						track = bandit
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = marauder
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = poacher
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = thief
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = trickster
						value >= 60
					}
				}
			}
			change_variable = {
				name = maa_number
				add = 1
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait_xp = {
						trait = gallowsbait
						track = bandit
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = marauder
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = poacher
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = thief
						value >= 60
					}
					has_trait_xp = {
						trait = gallowsbait
						track = trickster
						value >= 60
					}
				}
			}
			change_variable = {
				name = maa_number
				add = 1
			}
		}
		custom_tooltip = bandit_king_maa_spawn_effect_desc #otherwise the tooltip looks crazy
		hidden_effect = {
			random_list = {
				1 = {
					spawn_army = {
						men_at_arms = {
							type = maa_poachers
							stacks = var:maa_number
						}
						inheritable = no
						uses_supply = yes
						location = scope:bandit_king.location
						name = ep3_poachers_army
					}
					spawn_army = {
						men_at_arms = {
							type = maa_thieves
							stacks = var:maa_number
						}
						inheritable = no
						uses_supply = yes
						location = scope:bandit_king.location
						name = ep3_bandits_army
					}
				}
				1 = {
					spawn_army = {
						men_at_arms = {
							type = maa_bandits
							stacks = var:maa_number
						}
						inheritable = no
						uses_supply = yes
						location = scope:bandit_king.location
						name = ep3_bandits_army
					}
					spawn_army = {
						men_at_arms = {
							type = maa_thieves
							stacks = var:maa_number
						}
						inheritable = no
						uses_supply = yes
						location = scope:bandit_king.location
						name = ep3_bandits_army
					}
				}
			}
		}
	}
}

absolve_crusaders_of_gallowsbait_effect = {
	if = {
		limit = {
			has_trait_xp = {
				trait = gallowsbait
				track = bandit
				value >= 1
			}
		}
		add_trait_xp = {
			trait = gallowsbait
			track = bandit
			value = crusader_absolution_xp_removed_per_track_value
		}
	}
	if = {
		limit = {
			has_trait_xp = {
				trait = gallowsbait
				track = trickster
				value >= 1
			}
		}
		add_trait_xp = {
			trait = gallowsbait
			track = trickster
			value = crusader_absolution_xp_removed_per_track_value
		}
	}
	if = {
		limit = {
			has_trait_xp = {
				trait = gallowsbait
				track = thief
				value >= 1
			}
		}
		add_trait_xp = {
			trait = gallowsbait
			track = thief
			value = crusader_absolution_xp_removed_per_track_value
		}
	}
	if = {
		limit = {
			has_trait_xp = {
				trait = gallowsbait
				track = poacher
				value >= 1
			}
		}
		add_trait_xp = {
			trait = gallowsbait
			track = poacher
			value = crusader_absolution_xp_removed_per_track_value
		}
	}
	if = {
		limit = {
			has_trait_xp = {
				trait = gallowsbait
				track = marauder
				value >= 1
			}
		}
		add_trait_xp = {
			trait = gallowsbait
			track = marauder
			value = crusader_absolution_xp_removed_per_track_value
		}
	}
	if = {
		limit = {
			has_trait_xp = {
				trait = gallowsbait
				track = bandit
				value <= crusader_absolution_min_xp_to_remove_trait_value
			}
			has_trait_xp = {
				trait = gallowsbait
				track = trickster
				value <= crusader_absolution_min_xp_to_remove_trait_value
			}
			has_trait_xp = {
				trait = gallowsbait
				track = thief
				value <= crusader_absolution_min_xp_to_remove_trait_value
			}
			has_trait_xp = {
				trait = gallowsbait
				track = poacher
				value <= crusader_absolution_min_xp_to_remove_trait_value
			}
			has_trait_xp = {
				trait = gallowsbait
				track = marauder
				value <= crusader_absolution_min_xp_to_remove_trait_value
			}
		}
		remove_trait = gallowsbait
	}
}

temporarily_lock_visit_settlement_effect = {
	set_variable = {
		name = ep3_laamp_decision_1000_is_visiting
		days = visit_settlement_lock_days
	}
}

##################################################
# Admin Government
##################################################

torture_grants_influence_effect = {
	if = {
		limit = {
			domicile ?= { has_domicile_parameter = estate_torture_grants_influence }
		}
		change_influence = medium_influence_gain
	}
}

create_noble_family_effect = {
	debug_log = create_noble_family_title
	save_scope_as = new_noble_family_holder
	$GOVERNMENT_GIVER$ = { save_scope_as = government_giver }
	hidden_effect = { # NF can only be held by house heads
		if = { # Start by checking that we are not the house head already
			limit = {
				exists = house.house_head
				NOT = { house.house_head = scope:new_noble_family_holder }
			}
			if = { # If not, let's see if it would be reasonable to become house head...
				limit = {
					house.house_head ?= {
						is_ai = yes # Don't steal house head from a player
						OR = {
							# Within same realm but less important
							AND = {
								top_liege = scope:new_noble_family_holder.top_liege
								OR = {
									is_ruler = no
									highest_held_title_tier < scope:new_noble_family_holder.highest_held_title_tier
								}
							}
							# Wanderer
							NOT = { exists = liege }
						}
					}
				}
				house = { set_house_head = scope:new_noble_family_holder }
			}
			else = { # ... Otherwise, we create a cadet branch
				create_cadet_branch = {}
			}
		}
	}
	scope:government_giver = {
		if = {
			limit = {
				scope:new_noble_family_holder = {
					NOT = {
						any_held_title = { is_noble_family_title = yes }
					}
				}
			}
			switch = {
				trigger = has_government
				japan_administrative_government = { # Japan has count level NFs
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = japan_administrative_government
							save_scope_as = new_title
						}
					}
				}
				japan_feudal_government = { # Japan has count level NFs
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = japan_feudal_government
							save_scope_as = new_title
						}
					}
				}
				celestial_government = { # China has different government
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = celestial_government
							save_scope_as = new_title
						}
						trigger_event = {
							id = tgp_china_career.0001
							days = { 2 5 }
						}
					}
				}
				meritocratic_government = { # Meritocratic has different government
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = meritocratic_government
							save_scope_as = new_title
						}
					}
				}
				steppe_admin_government = { # Steppe Admin has different government
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = county
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = steppe_admin_government
							save_scope_as = new_title
						}
					}
				}
				fallback = {
					scope:new_noble_family_holder = {
						give_noble_family_title = {
							name = noble_family_name
							tier = duchy
							article = DEFAULT_TITLE_NAME_ARTICLE
							government = administrative_government
							save_scope_as = new_title
						}
					}
				}
			}
		}
		scope:new_title.holder ?= {
			save_scope_as = noble_family_head
			scope:new_title = { set_coa = scope:noble_family_head.house }
		}
		custom_description = { text = create_noble_family_tt }

		#Notifications
		every_player = {
			limit = {
				top_liege = scope:new_noble_family_holder.top_liege
				this != scope:new_noble_family_holder
				# China has hundreds of Noble Families, it gets spammy
				NOT = { government_allows = merit }
			}
			trigger_event = {
				id = ep3_emperor_yearly.2410
				days = 1
			}
		}
	}

	debug_log_scopes = yes
}

increase_governance_effect = {
	# VALUE - The amount of XP added to the Governor trait/increased chance to gain the Governor trait
	save_scope_value_as = {
		name = governor_xp_gain
		value = {
			value = $VALUE$
			if = {
				limit = {
					exists = scope:task_contract
				}
				multiply = governance_task_contract_tier_value

				if = {
					limit = {
						$VALUE$ > 0
						domicile ?= { has_domicile_parameter = estate_governor_trait_xp_bonus_for_contracts }
					}
					multiply = {
						value = 1
						add = estate_governance_contract_bonus_value
					}
				}
			}
		}
	}
	# Double-check if you have the governor trait - If not, let's make sure you get it
	if = {
		limit = {
			NOT = { has_trait = governor }
			is_governor = yes
		}
		add_trait = governor
	}
	# If you have the trait, let's increase your XP for it
	else_if = {
		limit = {
			has_trait = governor
		}
		add_trait_xp = {
			trait = governor
			value = scope:governor_xp_gain
		}
	}
}

spawn_governance_contract_effect = {
	save_scope_as = governor
	# Select the Contract Tier (somewhat randomized, based on your experience as a governor)
	random_list = {
		30 = {
			save_scope_value_as = {
				name = governance_task_contract_tier
				value = define:NTaskContract|LOW_TASK_CONTRACT_TIER
			}
		}
		20 = {
			save_scope_value_as = {
				name = governance_task_contract_tier
				value = define:NTaskContract|MEDIUM_TASK_CONTRACT_TIER
			}

			modifier = {
				add = 10
				has_trait = governor
			}
			modifier = {
				add = 10
				has_trait = governor
				has_trait_xp = {
					trait = governor
					value >= 40
				}
			}
		}
		10 = {
			save_scope_value_as = {
				name = governance_task_contract_tier
				value = define:NTaskContract|HIGH_TASK_CONTRACT_TIER
			}

			modifier = {
				add = 10
				has_trait = governor
			}
			modifier = {
				add = 10
				has_trait = governor
				has_trait_xp = {
					trait = governor
					value >= 40
				}
			}
			modifier = {
				add = 20
				has_trait = governor
				has_trait_xp = {
					trait = governor
					value >= 80
				}
			}
		}
	}

	# Select which contract to create
	trigger_event = {
		on_action = spawn_admin_governance_contract_on_action
	}
	
	play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Byzantine_Flavor/ep3_mx_sting_byzantineflavor_become_governor"
}

manage_new_governance_contract_effect = {
	if = {
		limit = { exists = scope:target_province }
		if = {
			limit = {
				NOT = { has_character_flag = admin_governance_frequency_cooldown }
			}
			add_character_flag = { flag = admin_governance_frequency_cooldown years = 6 }
		}
		send_interface_message = {
			type = msg_governance_contracts
			title = msg_governance_contract_appear.t
			desc = msg_governance_contract_appear.desc
			right_icon = scope:target_province.county
		}
	}
	if = {
		limit = {
			is_ai = yes
			exists = scope:new_governance_issue
		}
		accept_task_contract = scope:new_governance_issue
	}
}

generate_governance_outcome_effect = {
	# INFO: We use this effect to randomize the options you get when doing a governor contract.
	# $OPTIONS$ is the number of options the event has - Never use less than four, and try to have six if possible.

	# Unlock a bonus option if you have enough exp in the Governor trait - We do this here (and not in the option) to avoid blocking the option due to exp decay (if game is unpaused).
	if = {
		limit = {
			has_trait = governor
			has_trait_xp = {
				trait = governor
				value >= 50
			}
		}
		save_scope_as = governance_option_a
	}

	# Check if you have the Governor trait
	if = {
		limit = { # If you have the trait with at least rank 1, we generate an extra option.
			has_trait = governor
			has_trait_xp = {
				trait = governor
				value >= 25
			}
		}
		save_scope_value_as = { name = governance_number_of_options value = 3 }
	}
	else = { # Otherwise, you only get two base options.
		save_scope_value_as = { name = governance_number_of_options value = 2 }
	}

	# Generate the options
	set_local_variable = {
		name = current_value
		value = 0
	}
	while = {
		limit = {
			local_var:current_value < scope:governance_number_of_options
		}
		random_list = {
			# There is no option A, as A should be unlocked for having the Governor trait at rank 2 (which is done in the corresponding event)
			20 = {
				trigger = {
					NOT = { exists = scope:governance_option_b }
				}
				save_scope_value_as = {
					name = governance_option_b
					value = yes
				}
				change_local_variable = {
					name = current_value
					add = 1
				}
			}
			20 = {
				trigger = {
					NOT = { exists = scope:governance_option_c }
				}
				save_scope_value_as = {
					name = governance_option_c
					value = yes
				}
				change_local_variable = {
					name = current_value
					add = 1
				}
			}
			20 = {
				trigger = {
					NOT = { exists = scope:governance_option_d }
				}
				save_scope_value_as = {
					name = governance_option_d
					value = yes
				}
				change_local_variable = {
					name = current_value
					add = 1
				}
			}
			20 = {
				trigger = {
					NOT = { exists = scope:governance_option_e }
					$OPTIONS$ >= 5
				}
				save_scope_value_as = {
					name = governance_option_e
					value = yes
				}
				change_local_variable = {
					name = current_value
					add = 1
				}
			}
			20 = {
				trigger = {
					NOT = { exists = scope:governance_option_f }
					$OPTIONS$ >= 6
				}
				save_scope_value_as = {
					name = governance_option_f
					value = yes
				}
				change_local_variable = {
					name = current_value
					add = 1
				}
			}
		}
	}
}

remove_other_eparch_modifiers_effect = {
	hidden_effect = { # No need to show this
		# Let's check for the county opinion modifier first
		scope:liege ?= {
			# We check and scope to all counties to make sure we remove any stray modifiers in case the capital is moved, etc.
			if = {
				limit = {
					any_realm_county = {
						any_county_province = {
							has_province_modifier = ep3_eparch_county_opinion_modifier
						}
					}
				}
				every_realm_county = {
					limit = {
						any_county_province = {
							has_province_modifier = ep3_eparch_county_opinion_modifier
						}
					}
					every_county_province = {
						limit = {
							has_province_modifier = ep3_eparch_county_opinion_modifier
						}
						remove_province_modifier = ep3_eparch_county_opinion_modifier
					}
				}
			}
		}
		
		# Next is the troop modifier
		scope:liege ?= {
			# We check and scope to all counties to make sure we remove any stray modifiers in case the capital is moved, etc.
			if = {
				limit = {
					any_realm_county = {
						any_county_province = {
							has_province_modifier = ep3_eparch_troops_modifier
						}
					}
				}
				every_realm_county = {
					limit = {
						any_county_province = {
							has_province_modifier = ep3_eparch_troops_modifier
						}
					}
					every_county_province = {
						limit = {
							has_province_modifier = ep3_eparch_troops_modifier
						}
						remove_province_modifier = ep3_eparch_troops_modifier
					}
				}
			}
		}
		
		# Next is the county control modifier
		scope:liege ?= {
			# We check and scope to all counties to make sure we remove any stray modifiers in case the capital is moved, etc.
			if = {
				limit = {
					any_realm_county = {
						any_county_province = {
							has_province_modifier = ep3_eparch_control_modifier
						}
					}
				}
				every_realm_county = {
					limit = {
						any_county_province = {
							has_province_modifier = ep3_eparch_control_modifier
						}
					}
					every_county_province = {
						limit = {
							has_province_modifier = ep3_eparch_control_modifier
						}
						remove_province_modifier = ep3_eparch_control_modifier
					}
				}
			}
		}
		
		# And finally, the development modifier
		scope:liege ?= {
			# We check and scope to all counties to make sure we remove any stray modifiers in case the capital is moved, etc.
			if = {
				limit = {
					any_realm_county = {
						any_county_province = {
							has_province_modifier = ep3_eparch_development_modifier
						}
					}
				}
				every_realm_county = {
					limit = {
						any_county_province = {
							has_province_modifier = ep3_eparch_development_modifier
						}
					}
					every_county_province = {
						limit = {
							has_province_modifier = ep3_eparch_development_modifier
						}
						remove_province_modifier = ep3_eparch_development_modifier
					}
				}
			}
		}
	}
}

eparch_neglecting_duty_effect = {
	custom_tooltip = {
		text = eparch_neglecting_duty_warning_tt
		
		# We check if you have neglected duties in the past
		if = {
			limit = {
				exists = var:eparch_neglecting_duty
			}
			# If you have, let's first increase the variable
			change_variable = { name = eparch_neglecting_duty add = 5 }
			
			# Then check if the liege notices and decide to take action
			random = {
				chance = {
					# Base value - Increases with each neglecting action
					value = var:eparch_neglecting_duty
					
					# Add the difference in intrigue skill
					add = {
						value = liege.intrigue
						subtract = intrigue
					}
					
					# Add the difference in influence level
					add = {
						value = liege.influence_level
						subtract = influence_level
						multiply = 5
					}
					
					# Lower the chance if you belong to a powerful or dominant family
					if = {
						limit = {
							house ?= { is_powerful_family = yes }
						}
						subtract = 10
					}
					if = {
						limit = {
							house ?= { is_dominant_family = yes }
						}
						subtract = 20
					}
					
					min = 5
					max = 90
				}
				
				# The liege have noticed - We reduce the variable slightly as a result (but never below 0)
				if = {
					limit = {
						exists = var:eparch_neglecting_duty
						var:eparch_neglecting_duty >= 20
					}
					change_variable = { name = eparch_neglecting_duty subtract = 20 }
				}
				else = {
					set_variable = { name = eparch_neglecting_duty value = 0 }
				}
				
				liege = {
					trigger_event = { id = ep3_eparch_event.9000 days = { 10 90 } }
				}
			}
		}
		else = { # If you haven't, we'll set the variable and give you a free pass (you only get it for your very first time though)
			set_variable = { name = eparch_neglecting_duty value = 0 }
		}
	}
}

add_influence_attacker_victory_effect = {
	scope:attacker = {
		if = {
			limit = { government_has_flag = government_has_influence }
			change_influence = major_influence_gain
		}
	}
}

add_influence_attacker_defeat_effect = {
	scope:defender = {
		if = {
			limit = { government_has_flag = government_has_influence }
			change_influence = medium_influence_gain
		}
	}
}

admin_change_government_effect = {
	# Make sure you only use this effect on characters you are sure should change government! Since we don't do any extended triggers here.
	# For a consistent behaviour, this effect is based on "convert_whole_realm_to_feudalism_effect"

	# First, let's see if the character only has their noble family title - then we don't bother changing their government
	# We only destroy the family title to make sure it doesn't linger
	if = {
		limit = {
			is_landed = no
			any_held_title = {
				is_noble_family_title = yes
			}
			NOT = {
				any_held_title = {
					is_noble_family_title = no
				}
			}
		}
		every_held_title = {
			limit = {
				is_noble_family_title = yes
			}
			save_temporary_scope_as = family_title
			prev = { destroy_title = scope:family_title }
		}
	}

	# If not, let's change the government type
	# If liege has clan - Become clan!
	else_if = {
		limit = {
			top_liege != this
			OR = {
				liege = { government_has_flag = government_is_clan }
				AND = {
					liege = { government_has_flag = government_is_tribal }
					religion = religion:islam_religion
				}
			}
		}
		change_government = clan_government
	}
	# If liege has japanese feudal - Become japanese feudal!
	else_if = {
		limit = {
			is_independent_ruler = no
			OR = {
				liege = { government_has_flag = government_is_japan_feudal }
				AND = {
					liege = { government_has_flag = government_is_japan_feudal }
					NOT = { religion = religion:islam_religion }
				}
			}
		}
		change_government = japan_feudal_government
	}
	# If liege has feudal - Become feudal!
	else_if = {
		limit = {
			top_liege != this
			OR = {
				liege = { government_has_flag = government_is_feudal }
				AND = {
					liege = { government_has_flag = government_is_tribal }
					NOT = { religion = religion:islam_religion }
				}
			}
		}
		change_government = feudal_government
	}
	# If you're an independent Muslim ruler - Become clan!
	else_if = {
		limit = {
			top_liege = this
			faith = { religion_tag = islam_religion }
		}
		change_government = clan_government
	}
	# If you're an independent ruler with anything else - Become feudal!
	else = { change_government = feudal_government }

	# Finally, let's double-check and see if there are any remaining family titles we should destroy
	if = {
		limit = {
			any_held_title = {
				is_noble_family_title = yes
			}
		}
		every_held_title = {
			limit = {
				is_noble_family_title = yes
			}
			save_temporary_scope_as = family_title
			prev = { destroy_title = scope:family_title }
		}
	}
}

##################################################
# Dynasty Legacy: Bureaucracy
##################################################

ep3_dynasty_legacy_add_legitimacy_effect = {
	scope:activity = {
		if = {
			limit = {
				scope:host = {
					government_allows = administrative
					OR = {
						dynasty = { has_dynasty_perk = ep3_administrative_legacy_3 }
						top_participant_group:dynastic_cycle ?= {
							participant_group_type = conservative_movement
						}
					}
				}
				# Does the activity location have a domicile owned by a house member
				# Does that domicile have the Reception Hall building
				activity_location = {
					any_province_domicile = {
						owner = { house ?= scope:host.house }
						OR = {
							has_domicile_building_or_higher = reception_hall_01
							has_domicile_building_or_higher = east_asian_estate_reception_hall_01
						}
					}
				}
			}
			activity_location = {
				random_province_domicile = {
					limit = {
						owner = { house ?= scope:host.house }
						OR = {
							has_domicile_building_or_higher = reception_hall_01
							has_domicile_building_or_higher = east_asian_estate_reception_hall_01
						}
					}
					owner = { save_scope_as = reception_hall_owner }
				}
			}
			scope:host = {
				house = {
					if = {
						limit = {
							NOT = {
								has_variable = ep3_legacy_legitimacy_counter
							}
						}
						# Set the initial value
						set_variable = {
							name = ep3_legacy_legitimacy_counter
							value = 10
						}
					}
					else = {
						change_variable = {
							name = ep3_legacy_legitimacy_counter
							add = 10
						}
					}
				}
				if = {
					limit = {
						primary_title.tier = tier_empire
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = ep3_dynasty_legacy_gain_legitimacy_effect_title
						left_icon = scope:host
						right_icon = scope:host.primary_title
						add_legitimacy = 10
					}
				}
				else = {
					house = {
						every_house_member = {
							limit = { is_ai = no }
							send_interface_message = {
								type = event_generic_good_text
								title = ep3_dynasty_legacy_add_legitimacy_effect_title
								desc = ep3_dynasty_legacy_add_legitimacy_effect_desc
								left_icon = scope:host
								right_icon = house
							}
						}
					}
				}
			}
		}
	}
}

##################################################
# Byzantium
##################################################

remove_a_criminal_trait_in_faith_effect = {
	$CHARACTER$ = {
		if = {
			limit = {
				has_trait = adulterer
				trait_is_criminal_in_faith_trigger = { TRAIT = adulterer FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = adulterer
		}
		else_if = {
			limit = {
				has_trait = fornicator
				trait_is_criminal_in_faith_trigger = { TRAIT = fornicator FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = fornicator
		}
		else_if = {
			limit = {
				has_trait = sodomite
				trait_is_criminal_in_faith_trigger = { TRAIT = sodomite FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = sodomite
		}
		else_if = {
			limit = {
				has_trait = deviant
				trait_is_criminal_in_faith_trigger = { TRAIT = deviant FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = deviant
		}
		else_if = {
			limit = {
				has_trait = kinslayer_3
				trait_is_criminal_in_faith_trigger = { TRAIT = kinslayer_3 FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = kinslayer_3
		}
		else_if = {
			limit = {
				has_trait = kinslayer_2
				trait_is_criminal_in_faith_trigger = { TRAIT = kinslayer_2 FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = kinslayer_2
		}
		else_if = {
			limit = {
				has_trait = kinslayer_1
				trait_is_criminal_in_faith_trigger = { TRAIT = kinslayer_1 FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = kinslayer_1
		}
		else_if = {
			limit = {
				has_trait = witch
				trait_is_criminal_in_faith_trigger = { TRAIT = witch FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = witch
		}
		else_if = {
			limit = {
				has_trait = cannibal
				trait_is_criminal_in_faith_trigger = { TRAIT = cannibal FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = cannibal
		}
		else_if = {
			limit = {
				has_trait = incestuous
				trait_is_criminal_in_faith_trigger = { TRAIT = incestuous FAITH = $FAITH$ GENDER_CHARACTER = $CHARACTER$ }
			}
			remove_trait = incestuous
		}
	}
}

##################################################
# Chariot Races
##################################################

gather_charioteers_effect = {
	scope:activity = {
		random_list = {
			50 = {
				set_variable = {
					name = num_blues
					value = 2
				}
			}
			50 = {
				set_variable = {
					name = num_blues
					value = 3
				}
			}
		}
		random_list = {
			50 = {
				set_variable = {
					name = num_greens
					value = 2
				}
			}
			50 = {
				set_variable = {
					name = num_greens
					value = 3
				}
			}
		}
		random_list = {
			50 = {
				set_variable = {
					name = num_whites
					value = 2
				}
			}
			50 = {
				set_variable = {
					name = num_whites
					value = 3
				}
			}
		}
		random_list = {
			50 = {
				set_variable = {
					name = num_reds
					value = 2
				}
			}
			50 = {
				set_variable = {
					name = num_reds
					value = 3
				}
			}
		}

		ordered_attending_character = {
			order_by = chariot_ordering_value
			max = 12
			check_range_bounds = no
			save_temporary_scope_as = temp_character
			if = {
				limit = {
					OR = {
						has_court_position = charioteer_court_position
						trigger_if = {
							limit = {
								available_charioteers_spots_trigger = { TEAM = green }
							}
							has_trait = charioteer_green
						}
						trigger_if = {
							limit = {
								available_charioteers_spots_trigger = { TEAM = blue }
							}
							has_trait = charioteer_blue
						}
						trigger_if = {
							limit = {
								available_charioteers_spots_trigger = { TEAM = red }
							}
							has_trait = charioteer_red
						}
						trigger_if = {
							limit = {
								available_charioteers_spots_trigger = { TEAM = white }
							}
							has_trait = charioteer_white
						}
					}
				}
				scope:activity = {
					add_to_guest_subset = {
						name = charioteers
						target = scope:temp_character
					}
				}
			}
		}

		# Finalize the list of charioteers
		chariot_racers_setup_effect = { TEAM = green }
		chariot_racers_setup_effect = { TEAM = blue }
		chariot_racers_setup_effect = { TEAM = red }
		chariot_racers_setup_effect = { TEAM = white }
		# And filter everyone remaining to spectators.
		every_attending_character = {
			limit = {
				NOT = {
					is_in_guest_subset = { name = charioteers }
				}
			}
			save_scope_as = temp_character
			scope:activity = {
				add_to_guest_subset = {
					name = spectators
					target = scope:temp_character
				}
			}
		}
	}
}

chariot_racers_setup_effect = { # If you need to create additional racers
	while = {
		count = 5
		limit = {
			available_charioteers_spots_trigger = { TEAM = $TEAM$ }
		}
		create_character = {
			template = charioteer_template
			employer = scope:activity.activity_host
			save_scope_as = temp_charioteer
		}
		scope:temp_charioteer = {
			add_trait = charioteer_$TEAM$
			add_trait_xp = {
				trait = charioteer_$TEAM$
				value = { 5 50 }
			}
			add_to_activity = scope:activity
		}
		scope:activity = {
			add_to_guest_subset = {
				name = charioteers
				target = scope:temp_charioteer
			}
		}
	}
}

# Each character is assigned Odds to Win, Odds to Place, and Odds to Show
# Win: come in first place
# Place: come in first or second place
# Show: come in first, second, or third place
calculate_chariot_race_odds_effect = {
	calculate_chariot_odds_to_win_effect = yes
	calculate_chariot_odds_to_place_effect = yes
	calculate_chariot_odds_to_show_effect = yes
}

# Odds are represented in the format A:B, where A is the number of ways/opportunities a character has to succeed, and B represents how many ways/opportunities they have to fail
# These same calculations are done for the teams overall
# Individual odds are saved on the individual character
# Team odds are saved on the activity scope
calculate_chariot_odds_to_win_effect = {
	scope:activity = {
		### INDIVIDUAL ODDS
		# Set odds to win
		set_variable = {
			name = total_chance_win
			value = 0
		}
		# Set odds_win_a (number of ways to win)
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = odds_win_a
				value = charioteer_chance_to_win
			}
			scope:activity = {
				change_variable = {
					name = total_chance_win
					add = {
						value = prev.charioteer_chance_to_win
						min = 1
					}
				}
			}
		}
		# set odds_win_b (number of ways to lose)
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = odds_win_b
				value = {
					value = scope:activity.var:total_chance_win
					subtract = var:odds_win_a
					min = 1
				}
			}
		}

		# Calc percent chance to win
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = percent_chance_to_win
				value = {
					add = var:odds_win_a
					divide = {
						value = 0
						add = var:odds_win_a
						add = var:odds_win_b
					}
					multiply = 100
					min = 1
					max = 99
				}
			}
		}
		##########

		### TEAM ODDS
		# For each of the 4 teams, we calculate their odds to win/place/show. These variables are saved on the activity
		# BLUES
		# Initialize variables
		set_variable = {
			name = blue_odds_win_a
			value = 0
		}
		set_variable = {
			name = blue_odds_win_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_blue }
			scope:activity = {
				change_variable = {
					name = blue_odds_win_a
					add = {
						value = prev.var:odds_win_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = blue_odds_win_b
			add = {
				value = var:total_chance_win
				subtract = var:blue_odds_win_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = blue_percent_chance_to_win
			value = {
				add = var:blue_odds_win_a
				divide = {
					value = 0
					add = var:blue_odds_win_a
					add = var:blue_odds_win_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# GREENS
		# Initialize variables
		set_variable = {
			name = green_odds_win_a
			value = 0
		}
		set_variable = {
			name = green_odds_win_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_green }
			scope:activity = {
				change_variable = {
					name = green_odds_win_a
					add = {
						value = prev.var:odds_win_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = green_odds_win_b
			add = {
				value = var:total_chance_win
				subtract = var:green_odds_win_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = green_percent_chance_to_win
			value = {
				add = var:green_odds_win_a
				divide = {
					value = 0
					add = var:green_odds_win_a
					add = var:green_odds_win_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# WHITES
		# Initialize variables
		set_variable = {
			name = white_odds_win_a
			value = 0
		}
		set_variable = {
			name = white_odds_win_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_white }
			scope:activity = {
				change_variable = {
					name = white_odds_win_a
					add = {
						value = prev.var:odds_win_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = white_odds_win_b
			add = {
				value = var:total_chance_win
				subtract = var:white_odds_win_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = white_percent_chance_to_win
			value = {
				add = var:white_odds_win_a
				divide = {
					value = 0
					add = var:white_odds_win_a
					add = var:white_odds_win_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# REDS
		# Initialize variables
		set_variable = {
			name = red_odds_win_a
			value = 0
		}
		set_variable = {
			name = red_odds_win_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_red }
			scope:activity = {
				change_variable = {
					name = red_odds_win_a
					add = {
						value = prev.var:odds_win_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = red_odds_win_b
			add = {
				value = var:total_chance_win
				subtract = var:red_odds_win_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = red_percent_chance_to_win
			value = {
				add = var:red_odds_win_a
				divide = {
					value = 0
					add = var:red_odds_win_a
					add = var:red_odds_win_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}
	}
}

calculate_chariot_odds_to_place_effect = {
	scope:activity = {
		### INDIVIDUAL ODDS
		# Set odds to place
		set_variable = {
			name = total_chance_place
			value = 0
		}
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = odds_place_a
				value = {
					value = charioteer_chance_to_win
					multiply = 2
				}
			}
			scope:activity = {
				change_variable = {
					name = total_chance_place
					add = {
						value = prev.charioteer_chance_to_win
						min = 1
					}
				}
			}
		}
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = odds_place_b
				value = {
					value = scope:activity.var:total_chance_place
					subtract = var:odds_place_a
					min = 1
				}
			}
		}

		# Calc percent chance to win
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = percent_chance_to_place
				value = {
					add = var:odds_place_a
					divide = {
						value = 0
						add = var:odds_place_a
						add = var:odds_place_b
					}
					multiply = 100
					min = 1
					max = 99
				}
			}
		}
		##########

		### TEAM ODDS
		# For each of the 4 teams, we calculate their odds to win/place/show. These variables are saved on the activity
		# Initialize variables
		set_variable = {
			name = blue_odds_place_a
			value = 0
		}
		set_variable = {
			name = blue_odds_place_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_blue }
			scope:activity = {
				change_variable = {
					name = blue_odds_place_a
					add = {
						value = prev.var:odds_place_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = blue_odds_place_b
			add = {
				value = var:total_chance_place
				subtract = var:blue_odds_place_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = blue_percent_chance_to_place
			value = {
				add = var:blue_odds_place_a
				divide = {
					value = 0
					add = var:blue_odds_place_a
					add = var:blue_odds_place_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# GREENS
		# Initialize variables
		set_variable = {
			name = green_odds_place_a
			value = 0
		}
		set_variable = {
			name = green_odds_place_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_green }
			scope:activity = {
				change_variable = {
					name = green_odds_place_a
					add = {
						value = prev.var:odds_place_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = green_odds_place_b
			add = {
				value = var:total_chance_place
				subtract = var:green_odds_place_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = green_percent_chance_to_place
			value = {
				add = var:green_odds_place_a
				divide = {
					value = 0
					add = var:green_odds_place_a
					add = var:green_odds_place_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# WHITES
		# Initialize variables
		set_variable = {
			name = white_odds_place_a
			value = 0
		}
		set_variable = {
			name = white_odds_place_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_white }
			scope:activity = {
				change_variable = {
					name = white_odds_place_a
					add = {
						value = prev.var:odds_place_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = white_odds_place_b
			add = {
				value = var:total_chance_place
				subtract = var:white_odds_place_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = white_percent_chance_to_place
			value = {
				add = var:white_odds_place_a
				divide = {
					value = 0
					add = var:white_odds_place_a
					add = var:white_odds_place_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}


		# REDS
		# Initialize variables
		set_variable = {
			name = red_odds_place_a
			value = 0
		}
		set_variable = {
			name = red_odds_place_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_red }
			scope:activity = {
				change_variable = {
					name = red_odds_place_a
					add = {
						value = prev.var:odds_place_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = red_odds_place_b
			add = {
				value = var:total_chance_place
				subtract = var:red_odds_place_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = red_percent_chance_to_place
			value = {
				add = var:red_odds_place_a
				divide = {
					value = 0
					add = var:red_odds_place_a
					add = var:red_odds_place_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}
	}
}

calculate_chariot_odds_to_show_effect = {
	scope:activity = {
		### INDIVIDUAL ODDS
		# Set odds to show
		set_variable = {
			name = total_chance_show
			value = 0
		}
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = odds_show_a
				value = {
					value = charioteer_chance_to_win
					multiply = 3
					min = 1
				}
			}
			scope:activity = {
				change_variable = {
					name = total_chance_show
					add = {
						value = prev.charioteer_chance_to_win
						min = 1
					}
				}
			}
		}
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = odds_show_b
				value = {
					value = scope:activity.var:total_chance_show
					subtract = var:odds_show_a
					min = 1
				}
			}
		}

		# Calc percent chance to show
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = percent_chance_to_show
				value = {
					add = var:odds_show_a
					divide = {
						value = var:odds_show_a
						add = scope:activity.var:total_chance_show
					}
					multiply = 100
					min = 1
					max = 99
				}
			}
		}
		##########

		### TEAM ODDS
		# For each of the 4 teams, we calculate their odds to win/place/show. These variables are saved on the activity
		# BLUES
		# Initialize variables
		set_variable = {
			name = blue_odds_show_a
			value = 0
		}
		set_variable = {
			name = blue_odds_show_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_blue }
			scope:activity = {
				change_variable = {
					name = blue_odds_show_a
					add = {
						value = prev.var:odds_show_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = blue_odds_show_b
			add = {
				value = var:total_chance_show
				subtract = var:blue_odds_show_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = blue_percent_chance_to_show
			value = {
				add = var:blue_odds_show_a
				divide = {
					value = 0
					add = var:blue_odds_show_a
					add = var:blue_odds_show_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# GREENS
		# Initialize variables
		set_variable = {
			name = green_odds_show_a
			value = 0
		}
		set_variable = {
			name = green_odds_show_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_green }
			scope:activity = {
				change_variable = {
					name = green_odds_show_a
					add = {
						value = prev.var:odds_show_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = green_odds_show_b
			add = {
				value = var:total_chance_show
				subtract = var:green_odds_show_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = green_percent_chance_to_show
			value = {
				add = var:green_odds_show_a
				divide = {
					value = 0
					add = var:green_odds_show_a
					add = var:green_odds_show_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# WHITES
		# Initialize variables
		set_variable = {
			name = white_odds_show_a
			value = 0
		}
		set_variable = {
			name = white_odds_show_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_white }
			scope:activity = {
				change_variable = {
					name = white_odds_show_a
					add = {
						value = prev.var:odds_show_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = white_odds_show_b
			add = {
				value = var:total_chance_show
				subtract = var:white_odds_show_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = white_percent_chance_to_show
			value = {
				add = var:white_odds_show_a
				divide = {
					value = 0
					add = var:white_odds_show_a
					add = var:white_odds_show_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}

		# REDS
		# Initialize variables
		set_variable = {
			name = red_odds_show_a
			value = 0
		}
		set_variable = {
			name = red_odds_show_b
			value = 0
		}

		# Account for each team member
		every_guest_subset = {
			name = charioteers
			limit = { has_trait = charioteer_red }
			scope:activity = {
				change_variable = {
					name = red_odds_show_a
					add = {
						value = prev.var:odds_show_a
						min = 1
					}
				}
			}
		}
		# Set the B variable based on the total amount of chance - the opporutnity for success
		change_variable = {
			name = red_odds_show_b
			add = {
				value = var:total_chance_show
				subtract = var:red_odds_show_a
				min = 1
			}
		}
		# Calculate the percentage chance of success for readability
		set_variable = {
			name = red_percent_chance_to_show
			value = {
				add = var:red_odds_show_a
				divide = {
					value = 0
					add = var:red_odds_show_a
					add = var:red_odds_show_b
				}
				multiply = 100
				min = 1
				max = 99
			}
		}
	}
}

calculate_chariot_payout_effect = {
	scope:activity = {
		every_guest_subset = {
			name = charioteers
			set_variable = {
				name = win_payout
				value = {
					add = var:odds_win_b
					subtract = {
						value = var:odds_win_b
						multiply = 0.3
					}
					save_temporary_value_as = temp_val
					subtract = {
						value = scope:temp_val
						multiply = bookkeeper_small_commission_value
						round = yes
					}
					divide = var:odds_win_a
					add = 1
					min = 1.1
				}
			}
			set_variable = {
				name = place_payout
				value = {
					add = var:odds_place_b
					subtract = {
						value = var:odds_place_b
						multiply = 0.3
					}
					save_temporary_value_as = temp_val
					subtract = {
						value = scope:temp_val
						multiply = bookkeeper_medium_commission_value
						round = yes
					}
					divide = var:odds_place_a
					add = 1
					min = 1.1
				}
			}
			set_variable = {
				name = show_payout
				value = {
					add = var:odds_show_b
					subtract = {
						value = var:odds_show_b
						multiply = 0.3
					}
					save_temporary_value_as = temp_val
					subtract = {
						value = scope:temp_val
						multiply = bookkeeper_large_commission_value
						round = yes
					}
					divide = var:odds_show_a
					add = 1
					min = 1.1
				}
			}
		}
		set_variable = {
			name = blue_win_payout
			value = {
				add = var:blue_odds_win_b
				subtract = {
					value = var:blue_odds_win_b
					multiply = bookkeeper_small_commission_value
					round = yes
				}
				divide = var:blue_odds_win_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = blue_place_payout
			value = {
				add = var:blue_odds_place_b
				subtract = {
					value = var:blue_odds_place_b
					multiply = bookkeeper_medium_commission_value
					round = yes
				}
				divide = var:blue_odds_place_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = blue_show_payout
			value = {
				add = var:blue_odds_show_b
				subtract = {
					value = var:blue_odds_show_b
					multiply = bookkeeper_large_commission_value
					round = yes
				}
				divide = var:blue_odds_show_a
				add = 1
				min = 1.1
			}
		}

		set_variable = {
			name = green_win_payout
			value = {
				add = var:green_odds_win_b
				subtract = {
					value = var:green_odds_win_b
					multiply = bookkeeper_small_commission_value
					round = yes
				}
				divide = var:green_odds_win_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = green_place_payout
			value = {
				add = var:green_odds_place_b
				subtract = {
					value = var:green_odds_place_b
					multiply = bookkeeper_medium_commission_value
					round = yes
				}
				divide = var:green_odds_place_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = green_show_payout
			value = {
				add = var:green_odds_show_b
				subtract = {
					value = var:green_odds_show_b
					multiply = bookkeeper_large_commission_value
					round = yes
				}
				divide = var:green_odds_show_a
				add = 1
				min = 1.1
			}
		}

		set_variable = {
			name = white_win_payout
			value = {
				add = var:white_odds_win_b
				subtract = {
					value = var:white_odds_win_b
					multiply = bookkeeper_small_commission_value
					round = yes
				}
				divide = var:white_odds_win_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = white_place_payout
			value = {
				add = var:white_odds_place_b
				subtract = {
					value = var:white_odds_place_b
					multiply = bookkeeper_medium_commission_value
					round = yes
				}
				divide = var:white_odds_place_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = white_show_payout
			value = {
				add = var:white_odds_show_b
				subtract = {
					value = var:white_odds_show_b
					multiply = bookkeeper_large_commission_value
					round = yes
				}
				divide = var:white_odds_show_a
				add = 1
				min = 1.1
			}
		}

		set_variable = {
			name = red_win_payout
			value = {
				add = var:red_odds_win_b
				subtract = {
					value = var:red_odds_win_b
					multiply = bookkeeper_small_commission_value
					round = yes
				}
				divide = var:red_odds_win_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = red_place_payout
			value = {
				add = var:red_odds_place_b
				subtract = {
					value = var:red_odds_place_b
					multiply = bookkeeper_medium_commission_value
					round = yes
				}
				divide = var:red_odds_place_a
				add = 1
				min = 1.1
			}
		}
		set_variable = {
			name = red_show_payout
			value = {
				add = var:red_odds_show_b
				subtract = {
					value = var:red_odds_show_b
					multiply = bookkeeper_large_commission_value
					round = yes
				}
				divide = var:red_odds_show_a
				add = 1
				min = 1.1
			}
		}

	}
}

set_or_increment_lucky_better_variable = {
	if = {
		limit = {
			NOT = { has_variable = lucky_bet_counter }
		}
		set_variable = {
			name = lucky_bet_counter
			value = 1
		}
	}
	else = {
		change_variable = {
			name = lucky_bet_counter
			add = 1
		}
		if = {
			limit = {
				var:lucky_bet_counter >= 5
				has_any_nickname = no
			}
			send_interface_toast = {
				# works for our purposes "New Nickname"
				title = sell_titles_nickname_gained
				left_icon = this
				give_nickname = nick_the_lucky
			}
		}
	}
}

disburse_chariot_race_activity_host_rewards = {
	scope:activity = {
		add_activity_log_entry = {
			key = chariot_race_host_rewards
			score = 100
			show_in_conclusion = yes
			character = root
			location = root.location

			root = {
				# Some base influence
				change_influence = medium_influence_gain
				# Extra influence rewards for having a successful wager
				if = {
					limit = {
						has_character_flag = successful_bet
						has_variable = wager_type
					}
					switch = {
						trigger = var:wager_type
						flag:win = {
							change_influence = medium_influence_gain
						}
						flag:place = {
							change_influence = minor_influence_gain
						}
						flag:show = {
							change_influence = miniscule_influence_gain
						}
					}
				}

				# Rewards for employing a charioteer who wins/places/shows
				if = {
					limit = { employs_court_position = charioteer_court_position }
					if = {
						limit = {
							any_court_position_holder = {
								type = charioteer_court_position
								scope:activity.var:first_place ?= this
							}
						}
						change_influence = monumental_influence_gain
					}
					else_if = {
						limit = {
							any_court_position_holder = {
								type = charioteer_court_position
								scope:activity.var:second_place ?= this
							}
						}
						change_influence = massive_influence_gain
					}
					else_if = {
						limit = {
							any_court_position_holder = {
								type = charioteer_court_position
								scope:activity.var:third_place ?= this
							}
						}
						change_influence = major_influence_gain
					}
				}

				#Legitimacy and popular opinion for how fancy your chariot races were
				if = {
					limit = {
						scope:activity = {
							has_activity_option = {
								category = chariot_option_entertainment
								option = chariot_entertainment_bad
							}
						}
					}
					if = {
						limit = {
							scope:host.capital_county = {
								county_opinion > 0
							}
						}
						add_legitimacy = miniscule_legitimacy_gain
						if = {
							limit = { has_activity_intent = reduce_stress_intent }
							stress_impact = {
								base = major_stress_loss
								athletic = miniscule_stress_loss
								gregarious = miniscule_stress_loss
							}
						}
						custom_tooltip = chariot_race_reward_high_county_opinion_tt
					}
					else_if = {
						limit = {
							scope:host.capital_county = {
								county_opinion <= -50
							}
						}
						add_legitimacy = 5
						if = {
							limit = { has_activity_intent = reduce_stress_intent }
							stress_impact = {
								base = medium_stress_loss
								athletic = miniscule_stress_loss
								gregarious = miniscule_stress_loss
							}
						}
						custom_tooltip = chariot_race_reward_low_county_opinion_tt
					}
					else = {
						add_legitimacy = 10
					}
				}
				else_if = {
					limit = {
						scope:activity = {
							has_activity_option = {
								category = chariot_option_entertainment
								option = chariot_entertainment_normal
							}
						}
					}
					if = {
						limit = {
							scope:host.capital_county = {
								county_opinion > 0
							}
						}
						add_legitimacy = minor_legitimacy_gain
						if = {
							limit = { has_activity_intent = reduce_stress_intent }
							stress_impact = {
								base = massive_stress_loss
								athletic = miniscule_stress_loss
								gregarious = miniscule_stress_loss
							}
						}
						capital_county = {
							change_county_control = miniscule_county_control_gain
						}

						custom_tooltip = chariot_race_reward_high_county_opinion_tt
					}
					else_if = {
						limit = {
							scope:host.capital_county = {
								county_opinion <= -50
							}
						}
						add_legitimacy = 10
						if = {
							limit = { has_activity_intent = reduce_stress_intent }
							stress_impact = {
								base = major_stress_loss
								athletic = miniscule_stress_loss
								gregarious = miniscule_stress_loss
							}
						}
						custom_tooltip = chariot_race_reward_low_county_opinion_tt
					}
					else = {
						add_legitimacy = miniscule_legitimacy_gain
					}
					scope:host.capital_county = {
						add_county_modifier = {
							modifier = ep3_chariot_race_weak_appeasement_modifier
							years = 10
						}
					}
				}
				else_if = {
					limit = {
						scope:activity = {
							has_activity_option = {
								category = chariot_option_entertainment
								option = chariot_entertainment_good
							}
						}
					}
					if = {
						limit = {
							scope:host.capital_county = {
								county_opinion > 0
							}
						}
						add_legitimacy = medium_legitimacy_gain
						if = {
							limit = { has_activity_intent = reduce_stress_intent }
							stress_impact = {
								base = monumental_stress_loss
								athletic = miniscule_stress_loss
								gregarious = miniscule_stress_loss
							}
						}
						capital_county = {
							change_county_control = minor_county_control_gain
						}
						custom_tooltip = chariot_race_reward_high_county_opinion_tt
					}
					else_if = {
						limit = {
							scope:host.capital_county = {
								county_opinion <= -50
							}
						}
						add_legitimacy = miniscule_legitimacy_gain
						if = {
							limit = { has_activity_intent = reduce_stress_intent }
							stress_impact = {
								base = miniscule_stress_loss
								athletic = massive_stress_loss
								gregarious = miniscule_stress_loss
							}
						}
						custom_tooltip = chariot_race_reward_low_county_opinion_tt
					}
					else = {
						add_legitimacy = minor_legitimacy_gain
					}
					scope:host.capital_county = {
						add_county_modifier = {
							modifier = ep3_chariot_race_strong_appeasement_modifier
							years = 10
						}
					}
				}
			}
		}
	}
}

disburse_chariot_race_activity_spectator_rewards = {
	# Some smaller amounts of Influence rewards for having a successful wager
	if = {
		limit = {
			has_character_flag = successful_bet
			has_variable = wager_type
		}
		switch = {
			trigger = var:wager_type
			flag:win = {
				change_influence = medium_influence_gain
			}
			flag:place = {
				change_influence = minor_influence_gain
			}
			flag:show = {
				change_influence = miniscule_influence_gain
			}
		}
	}

	# Rewards for employing a charioteer who wins/places/shows
	# You get more influence if your charioteer is successful
	if = {
		limit = {
			any_court_position_holder = {
				type = charioteer_court_position
				scope:activity.var:first_place ?= this
			}
		}
		change_influence = monumental_influence_gain
		if = {
			limit = { has_activity_intent = reduce_stress_intent }
			stress_impact = {
				base = monumental_stress_loss
				athletic = miniscule_stress_loss
				gregarious = miniscule_stress_loss
			}
		}
	}
	else_if = {
		limit = {
			any_court_position_holder = {
				type = charioteer_court_position
				scope:activity.var:second_place ?= this
			}
		}
		change_influence = massive_influence_gain
		if = {
			limit = { has_activity_intent = reduce_stress_intent }
			stress_impact = {
				base = massive_stress_loss
				athletic = miniscule_stress_loss
				gregarious = miniscule_stress_loss
			}
		}
	}
	else_if = {
		limit = {
			any_court_position_holder = {
				type = charioteer_court_position
				scope:activity.var:third_place ?= this
			}
		}
		change_influence = major_influence_gain
		if = {
			limit = { has_activity_intent = reduce_stress_intent }
			stress_impact = {
				base = massive_stress_loss
				athletic = miniscule_stress_loss
				gregarious = miniscule_stress_loss
			}
		}
	}
	else_if = {
		limit = { has_activity_intent = reduce_stress_intent }
		stress_impact = {
			base = miniscule_stress_loss
			athletic = major_stress_loss
			gregarious = miniscule_stress_loss
		}
	}
}

disburse_chariot_race_activity_charioteer_rewards = {
	# Give them some xp based on place finished
	if = {
		limit = { this = scope:activity.var:first_place }
		scope:activity = {
			add_activity_log_entry = {
				key = charioteer_gained_trait_xp_key
				score = 110
				character = scope:activity.var:first_place
				scope:activity.var:first_place = {
					distribute_charioteer_trait_xp_effect = { VALUE = 15 } }
			}
		}
	}
	else_if = {
		limit = { this = scope:activity.var:second_place }
		scope:activity = {
			add_activity_log_entry = {
				key = charioteer_gained_trait_xp_key
				score = 110
				character = scope:activity.var:second_place
				scope:activity.var:second_place = { distribute_charioteer_trait_xp_effect = { VALUE = 12 } }
			}
		}
	}
	else_if = {
		limit = { this = scope:activity.var:third_place }
		scope:activity = {
			add_activity_log_entry = {
				key = charioteer_gained_trait_xp_key
				score = 110
				character = scope:activity.var:third_place
				scope:activity.var:third_place = { distribute_charioteer_trait_xp_effect = { VALUE = 8 } }
			}
		}
	}
	else = {
		scope:activity = {
			add_activity_log_entry = {
				key = charioteer_gained_trait_xp_key
				score = 110
				character = prev
				prev = { distribute_charioteer_trait_xp_effect = { VALUE = 3 } }
			}
		}
	}
}

# Used to show how much a character gets from a successful wager in end of the race event chariot_race.0600
individual_chariot_race_payout_effect = {
	save_scope_as = current_gambler
	if = {
		limit = {
			var:wager_type ?= flag:win
			var:wager_target ?= scope:activity.var:first_place
		}
		add_gold = {
			value = scope:current_gambler.var:wager_value
			multiply = scope:activity.var:first_place.var:win_payout
			max = scope:current_gambler.monumental_gold_value
		}
	}
	else_if = {
		limit = {
			var:wager_type ?= flag:place
			OR = {
				var:wager_target ?= scope:activity.var:first_place
				var:wager_target ?= scope:activity.var:second_place
			}
		}
		add_gold = {
			value = scope:current_gambler.var:wager_value
			multiply = scope:activity.var:second_place.var:place_payout
			max = scope:current_gambler.monumental_gold_value
		}
	}
	else_if = {
		limit = {
			var:wager_type ?= flag:show
			OR = {
				var:wager_target ?= scope:activity.var:first_place
				var:wager_target ?= scope:activity.var:second_place
				var:wager_target ?= scope:activity.var:third_place
			}
		}
		add_gold = {
			value = scope:current_gambler.var:wager_value
			multiply = scope:activity.var:third_place.var:show_payout
			max = scope:current_gambler.monumental_gold_value
		}
	}
	# Handle team based bets
	else_if = {
		limit = {
			var:wager_type ?= flag:win
			var:wager_target ?= scope:current_gambler
		}
		switch = {
			trigger = var:wager_team
			flag:blues = {
				if = {
					limit = { scope:activity.var:first_place = { has_trait = charioteer_blue } }
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:blue_win_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:greens = {
				if = {
					limit = { scope:activity.var:first_place = { has_trait = charioteer_green } }
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:green_win_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:whites = {
				if = {
					limit = { scope:activity.var:first_place = { has_trait = charioteer_white } }
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:white_win_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:reds = {
				if = {
					limit = { scope:activity.var:first_place = { has_trait = charioteer_red } }
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:red_win_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			var:wager_type ?= flag:place
			var:wager_target ?= scope:current_gambler
		}
		switch = {
			trigger = var:wager_team
			flag:blues = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_blue }
							scope:activity.var:second_place = { has_trait = charioteer_blue }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:blue_place_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:greens = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_green }
							scope:activity.var:second_place = { has_trait = charioteer_green }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:green_place_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:whites = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_white }
							scope:activity.var:second_place = { has_trait = charioteer_white }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:white_place_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:reds = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_red }
							scope:activity.var:second_place = { has_trait = charioteer_red }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:red_place_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			var:wager_type ?= flag:show
			var:wager_target ?= scope:current_gambler
		}
		switch = {
			trigger = var:wager_team
			flag:blues = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_blue }
							scope:activity.var:second_place = { has_trait = charioteer_blue }
							scope:activity.var:third_place = { has_trait = charioteer_blue }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:blue_show_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:greens = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_green }
							scope:activity.var:second_place = { has_trait = charioteer_green }
							scope:activity.var:third_place = { has_trait = charioteer_green }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:green_show_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:whites = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_white }
							scope:activity.var:second_place = { has_trait = charioteer_white }
							scope:activity.var:third_place = { has_trait = charioteer_white }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:white_show_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
			flag:reds = {
				if = {
					limit = {
						OR = {
							scope:activity.var:first_place = { has_trait = charioteer_red }
							scope:activity.var:second_place = { has_trait = charioteer_red }
							scope:activity.var:third_place = { has_trait = charioteer_red }
						}
					}
					add_gold = {
						value = scope:current_gambler.var:wager_value
						multiply = scope:activity.var:red_show_payout
						max = scope:current_gambler.monumental_gold_value
					}
				}
			}
		}
	}
	else_if = {
		limit = { has_variable = wager_value }
		custom_tooltip = chariot_race_lost_bet_tt
	}
}

chariot_race_payout_effect = {
	scope:activity = {
		every_guest_subset = {
			name = spectators
			save_scope_as = current_gambler
			if = {
				limit = {
					var:wager_type ?= flag:win
					var:wager_target ?= scope:activity.var:first_place
				}
				set_or_increment_lucky_better_variable = yes
				add_character_flag = successful_bet
				scope:activity = {
					add_activity_log_entry = {
						key = chariot_race_betting_char_outcome_key
						score = 100
						show_in_conclusion = yes
						character = scope:current_gambler
						scope:current_gambler = {
							add_gold = {
								value = scope:current_gambler.var:wager_value
								multiply = scope:activity.var:first_place.var:win_payout
								max = scope:current_gambler.monumental_gold_value
							}
						}
						change_variable = {
							name = chariot_race_pot
							subtract = {
								value = scope:current_gambler.var:wager_value
								multiply = scope:activity.var:first_place.var:win_payout
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					var:wager_type ?= flag:place
					OR = {
						var:wager_target ?= scope:activity.var:first_place
						var:wager_target ?= scope:activity.var:second_place
					}
				}
				set_or_increment_lucky_better_variable = yes
				add_character_flag = successful_bet
				scope:activity = {
					add_activity_log_entry = {
						key = chariot_race_betting_char_outcome_key
						score = 100
						show_in_conclusion = yes
						character = scope:current_gambler
						scope:current_gambler = {
							add_gold = {
								value = scope:current_gambler.var:wager_value
								multiply = scope:activity.var:second_place.var:place_payout
								max = scope:current_gambler.monumental_gold_value
							}
						}
						change_variable = {
							name = chariot_race_pot
							subtract = {
								value = scope:current_gambler.var:wager_value
								multiply = scope:activity.var:second_place.var:place_payout
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					var:wager_type ?= flag:show
					OR = {
						var:wager_target ?= scope:activity.var:first_place
						var:wager_target ?= scope:activity.var:second_place
						var:wager_target ?= scope:activity.var:third_place
					}
				}
				set_or_increment_lucky_better_variable = yes
				add_character_flag = successful_bet
				scope:activity = {
					add_activity_log_entry = {
						key = chariot_race_betting_char_outcome_key
						score = 100
						show_in_conclusion = yes
						character = scope:current_gambler
						scope:current_gambler = {
							add_gold = {
								value = scope:current_gambler.var:wager_value
								multiply = scope:activity.var:third_place.var:show_payout
								max = scope:current_gambler.monumental_gold_value
							}
						}
						change_variable = {
							name = chariot_race_pot
							subtract = {
								value = scope:current_gambler.var:wager_value
								multiply = scope:activity.var:third_place.var:show_payout
							}
						}
					}
				}
			}
			# Handle team based bets
			else_if = {
				limit = {
					var:wager_type ?= flag:win
					var:wager_target ?= this
				}
				switch = {
					trigger = var:wager_team
					flag:blues = {
						if = {
							limit = { scope:activity.var:first_place = { has_trait = charioteer_blue } }
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:blue_win_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:blue_win_payout
										}
									}
								}
							}
						}
					}
					flag:greens = {
						if = {
							limit = { scope:activity.var:first_place = { has_trait = charioteer_green } }
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:green_win_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:green_win_payout
										}
									}
								}
							}
						}
					}
					flag:whites = {
						if = {
							limit = { scope:activity.var:first_place = { has_trait = charioteer_white } }
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:white_win_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:white_win_payout
										}
									}
								}
							}
						}
					}
					flag:reds = {
						if = {
							limit = { scope:activity.var:first_place = { has_trait = charioteer_red } }
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:red_win_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:red_win_payout
										}
									}
								}
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					var:wager_type ?= flag:place
					var:wager_target ?= this
				}
				switch = {
					trigger = var:wager_team
					flag:blues = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_blue }
									scope:activity.var:second_place = { has_trait = charioteer_blue }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:blue_place_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:blue_place_payout
										}
									}
								}
							}
						}
					}
					flag:greens = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_green }
									scope:activity.var:second_place = { has_trait = charioteer_green }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:green_place_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:green_place_payout
										}
									}
								}
							}
						}
					}
					flag:whites = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_white }
									scope:activity.var:second_place = { has_trait = charioteer_white }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:white_place_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:white_place_payout
										}
									}
								}
							}
						}
					}
					flag:reds = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_red }
									scope:activity.var:second_place = { has_trait = charioteer_red }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:red_place_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:red_place_payout
										}
									}
								}
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					var:wager_type ?= flag:show
					var:wager_target ?= this
				}
				switch = {
					trigger = var:wager_team
					flag:blues = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_blue }
									scope:activity.var:second_place = { has_trait = charioteer_blue }
									scope:activity.var:third_place = { has_trait = charioteer_blue }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:blue_show_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:blue_show_payout
										}
									}
								}
							}
						}
					}
					flag:greens = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_green }
									scope:activity.var:second_place = { has_trait = charioteer_green }
									scope:activity.var:third_place = { has_trait = charioteer_green }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:green_show_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:green_show_payout
										}
									}
								}
							}
						}
					}
					flag:whites = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_white }
									scope:activity.var:second_place = { has_trait = charioteer_white }
									scope:activity.var:third_place = { has_trait = charioteer_white }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:white_show_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:white_show_payout
										}
									}
								}
							}
						}
					}
					flag:reds = {
						if = {
							limit = {
								OR = {
									scope:activity.var:first_place = { has_trait = charioteer_red }
									scope:activity.var:second_place = { has_trait = charioteer_red }
									scope:activity.var:third_place = { has_trait = charioteer_red }
								}
							}
							set_or_increment_lucky_better_variable = yes
							add_character_flag = successful_bet
							scope:activity = {
								add_activity_log_entry = {
									key = chariot_race_betting_team_outcome_key
									score = 100
									show_in_conclusion = yes
									character = scope:current_gambler
									scope:current_gambler = {
										add_gold = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:red_show_payout
											max = scope:current_gambler.monumental_gold_value
										}
									}
									change_variable = {
										name = chariot_race_pot
										subtract = {
											value = scope:current_gambler.var:wager_value
											multiply = scope:activity.var:red_show_payout
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

clear_placement_variables_effect = {
	switch = {
		trigger = has_variable
		first_place = { remove_variable = first_place }
		second_place = { remove_variable = second_place }
		third_place = { remove_variable = third_place }
		fourth_place = { remove_variable = fourth_place }
		fifth_place = { remove_variable = fifth_place }
		sixth_place = { remove_variable = sixth_place }
		seventh_place = { remove_variable = seventh_place }
		eighth_place = { remove_variable = eighth_place }
		ninth_place = { remove_variable = ninth_place }
		tenth_place = { remove_variable = tenth_place }
		eleventh_place = { remove_variable = eleventh_place }
		twelfth_place = { remove_variable = twelfth_place }
	}
}

chariot_race_remove_variables_effect = {
	scope:activity = {
		# Clean up variables we used for calculations on both Charioteers and Spectators
		every_guest_subset = {
			name = charioteers
			remove_variable = odds_win_a
			remove_variable = odds_win_b
			remove_variable = odds_place_a
			remove_variable = odds_place_b
			remove_variable = odds_show_a
			remove_variable = odds_show_b
			remove_variable = percent_chance_to_win
			remove_variable = percent_chance_to_place
			remove_variable = percent_chance_to_show
			remove_variable = win_payout
			remove_variable = place_payout
			remove_variable = show_payout
			remove_variable = current_place_in_race
		}
		every_guest_subset = {
			name = spectators
			remove_variable = wager_team
			remove_variable = wager_type
			remove_variable = wager_target
			remove_variable = wager_value
			if = {
				limit = { has_character_flag = successful_bet }
				remove_character_flag = successful_bet
			}
		}
		# Really for error suppression only since variables saved on activities are destroyed when the activity is over
		switch = {
			trigger = has_variable
			blue_percent_chance_to_win = { remove_variable = blue_percent_chance_to_win }
			blue_percent_chance_to_show = { remove_variable = blue_percent_chance_to_show }
			blue_percent_chance_to_place = { remove_variable = blue_percent_chance_to_place }
			green_percent_chance_to_win = { remove_variable = green_percent_chance_to_win }
			green_percent_chance_to_place = { remove_variable = green_percent_chance_to_place }
			green_percent_chance_to_show = { remove_variable = green_percent_chance_to_show }
			white_percent_chance_to_place = { remove_variable = white_percent_chance_to_place }
			white_percent_chance_to_win = { remove_variable = white_percent_chance_to_win }
			white_percent_chance_to_show = { remove_variable = white_percent_chance_to_show }
			red_percent_chance_to_win = { remove_variable = red_percent_chance_to_win }
			red_percent_chance_to_place = { remove_variable = red_percent_chance_to_place }
			red_percent_chance_to_show = { remove_variable = red_percent_chance_to_show }
			dead_charioteers = { remove_variable = dead_charioteers }
		}
		clear_placement_variables_effect = yes
	}
}

advance_place_in_ranking_effect = {
	$NEW_LEADING_CHAR$ = { save_scope_as = char_advanced }
	$NEW_TRAILING_CHAR$ = { save_scope_as = char_passed }
	scope:char_advanced = {
		change_variable = {
			name = chariot_race_victory_score
			add = {
				value = scope:char_passed.var:chariot_race_victory_score
				subtract = scope:char_advanced.var:chariot_race_victory_score
				add = 1
			}
		}

	}
	scope:activity = { update_chariot_race_standings_effect = yes }
}

# Use this effect to update the current standings of the race based on each charioteer's victory score
update_chariot_race_standings_effect = {
	scope:activity = {
		# Ensure we have dead characters saved for the gui
		if = {
			limit = {
				has_variable = first_place
				var:first_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:first_place
			}
		}
		else_if = {
			limit = {
				has_variable = second_place
				var:second_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:second_place
			}
		}
		else_if = {
			limit = {
				has_variable = third_place
				var:third_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:third_place
			}
		}
		else_if = {
			limit = {
				has_variable = fourth_place
				var:fourth_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:fourth_place
			}
		}
		else_if = {
			limit = {
				has_variable = fifth_place
				var:fifth_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:fifth_place
			}
		}
		else_if = {
			limit = {
				has_variable = sixth_place
				var:sixth_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:sixth_place
			}
		}
		else_if = {
			limit = {
				has_variable = seventh_place
				var:seventh_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:seventh_place
			}
		}
		else_if = {
			limit = {
				has_variable = eighth_place
				var:eighth_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:eighth_place
			}
		}
		else_if = {
			limit = {
				has_variable = ninth_place
				var:ninth_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:ninth_place
			}
		}
		else_if = {
			limit = {
				has_variable = tenth_place
				var:tenth_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:tenth_place
			}
		}
		else_if = {
			limit = {
				has_variable = eleventh_place
				var:eleventh_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:eleventh_place
			}
		}
		else_if = {
			limit = {
				has_variable = twelfth_place
				var:twelfth_place = { is_alive = no }
			}
			add_to_variable_list = {
				name = dead_charioteers
				target = var:twelfth_place
			}
		}
		# Clear the variables so we get rid of any dead characters
		clear_placement_variables_effect = yes
		# Get a count of how many living charioteers we have
		set_variable = {
			name = num_charioteers
			value = 0
		}
		# Iterate over the list of charioteers and save each as a variable according to their place
		ordered_guest_subset = {
			name = charioteers
			order_by = charioteer_victory_score_value
			max = 12
			check_range_bounds = no
			limit = { is_alive = yes }
			save_temporary_scope_as = current_charioteer
			# increment variable
			scope:activity = {
				change_variable = {
					name = num_charioteers
					add = 1
				}
			}
			if = {
				limit = { has_variable = current_place_in_race }
				remove_variable = current_place_in_race
			}
			# scripted effect to set variable based on current count (num_charioteers)
			assign_charioteer_to_place_variable_effect = yes
		}
	}
}

# Used in update_chariot_race_standings_effect to set variables on both the activity and the individual charioteers
assign_charioteer_to_place_variable_effect = {
	scope:activity = {
		switch = {
			trigger = var:num_charioteers
			1 = {
				set_variable = {
					name = first_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 1
					}
				}
			}
			2 = {
				set_variable = {
					name = second_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 2
					}
				}
			}
			3 = {
				set_variable = {
					name = third_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 3
					}
				}
			}
			4 = {
				set_variable = {
					name = fourth_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 4
					}
				}
			}
			5 = {
				set_variable = {
					name = fifth_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 5
					}
				}
			}
			6 = {
				set_variable = {
					name = sixth_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 6
					}
				}
			}
			7 = {
				set_variable = {
					name = seventh_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 7
					}
				}
			}
			8 = {
				set_variable = {
					name = eighth_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 8
					}
				}
			}
			9 = {
				set_variable = {
					name = ninth_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 9
					}
				}
			}
			10 = {
				set_variable = {
					name = tenth_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 10
					}
				}
			}
			11 = {
				set_variable = {
					name = eleventh_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 11
					}
				}
			}
			12 = {
				set_variable = {
					name = twelfth_place
					value = scope:current_charioteer
				}
				scope:current_charioteer = {
					set_variable = {
						name = current_place_in_race
						value = 12
					}
				}
			}
		}
	}
}

distribute_charioteer_trait_xp_effect = {
	switch = {
		trigger = has_trait
		charioteer_blue = {
			if = {
				limit = { has_court_position = charioteer_court_position }
				employer = {
					send_interface_toast = {
						title = charioteer_trait_xp_gain_message
						left_icon = prev
						prev = {
							add_trait_xp = {
								trait = charioteer_blue
								value = $VALUE$
							}
						}
					}
				}
			}
			else = {
				add_trait_xp = {
					trait = charioteer_blue
					value = $VALUE$
				}
			}
		}
		charioteer_green = {
			if = {
				limit = { has_court_position = charioteer_court_position }
				employer = {
					send_interface_toast = {
						title = charioteer_trait_xp_gain_message
						left_icon = prev
						prev = {
							add_trait_xp = {
								trait = charioteer_green
								value = $VALUE$
							}
						}
					}
				}
			}
			else = {
				add_trait_xp = {
					trait = charioteer_green
					value = $VALUE$
				}
			}
		}
		charioteer_white = {
			if = {
				limit = { has_court_position = charioteer_court_position }
				employer = {
					send_interface_toast = {
						title = charioteer_trait_xp_gain_message
						left_icon = prev
						prev = {
							add_trait_xp = {
								trait = charioteer_white
								value = $VALUE$
							}
						}
					}
				}
			}
			else = {
				add_trait_xp = {
					trait = charioteer_white
					value = $VALUE$
				}
			}
		}
		charioteer_red = {
			if = {
				limit = { has_court_position = charioteer_court_position }
				employer = {
					send_interface_toast = {
						title = charioteer_trait_xp_gain_message
						left_icon = prev
						prev = {
							add_trait_xp = {
								trait = charioteer_red
								value = $VALUE$
							}
						}
					}
				}
			}
			else = {
				add_trait_xp = {
					trait = charioteer_red
					value = $VALUE$
				}
			}
		}
	}
}

remove_dead_charioteer_from_race = {
	scope:activity = {
		add_to_variable_list = {
			name = dead_charioteers
			target = prev
		}
	}
}

### END CHARIOT RACE

ep3_send_akolouthos_message_loyal = {
	send_interface_message = {
		type = msg_akolouthos_steels_varangians
		title = ep3_akolouthos_events.1000
		desc =  ep3_akolouthos_events.1000_loyal_notification_tooltip

		left_icon = scope:trait_getter
		scope:trait_getter = {
			add_trait = loyal
			add_opinion = {
				target = root
				modifier = loyalty_opinion
				opinion = 40
			}
		}
		custom_tooltip = akolouthos_effect_tooltip
	}
}

ep3_send_akolouthos_message_varangian = {
	send_interface_message = {
		type = msg_akolouthos_steels_varangians
		title = ep3_akolouthos_events.1000
		desc =  ep3_akolouthos_events.1000_varangian_notification_tooltip

		left_icon = scope:trait_getter
		scope:trait_getter = {
			add_trait = varangian
			add_opinion = {
				target = root
				modifier = loyalty_opinion
				opinion = 40
			}
		}
		custom_tooltip = akolouthos_effect_tooltip
	}
}

ep3_palace_looting_random_malus_effect = {
	if = {
		limit = {
			has_character_flag = malus_court_grandeur
			var:varangian_regiments >= 7
		}
		change_current_court_grandeur = massive_court_grandeur_loss
	}
	else_if = {
		limit = {
			has_character_flag = malus_court_grandeur
			var:varangian_regiments >= 4
		}
		change_current_court_grandeur = major_court_grandeur_loss
	}
	else_if = {
		limit = {
			has_character_flag = malus_court_grandeur
		}
		change_current_court_grandeur = medium_court_grandeur_loss
	}
	if = {
		limit = {
			has_character_flag = malus_broken_artifact
		}
		scope:broken_artifact = {
			add_durability = -40
		}
	}
	if = {
		limit = {
			has_character_flag = malus_stolen_artifact
		}
		destroy_artifact = scope:stolen_artifact
	}
}

ep3_child_castration_effect = {
	add_trait = beardless_eunuch
	torture_memory_effect = {
		VICTIM = scope:recipient
		TORTURER = scope:actor
		TYPE = castrated_beardless
	}
	#Eunuch bonus effects
	custom_tooltip = castration_increased_aptitude
	if = {
		limit = {
			scope:actor = {
				is_landed_or_landless_administrative = yes
			}
		}
		custom_tooltip = castration_governor_efficiency
	}
	#might get hurt
	custom_tooltip = castration_wounded_chance
	hidden_effect = {
		random_list = {
			5 = {
				add_character_modifier = {
					modifier = brutal_castration_modifier
					years = 5
				}
			}
			5 = {
				modifier = {
					add = 5
					employs_court_position = court_physician_court_position
				}
				modifier = {
					add = 5
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 1
						}
					}
				}
				modifier = {
					add = 10
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 2
						}
					}
				}
				modifier = {
					add = 10
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 3
						}
					}
				}
				modifier = {
					add = 10
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 4
						}
					}
				}
			}
		}
	}
}

ep3_youth_castration_effect = {
	add_trait = eunuch_1
	torture_memory_effect = {
		VICTIM = scope:recipient
		TORTURER = scope:actor
		TYPE = castrated
	}
	#eunuch bonus effects
	custom_tooltip = castration_increased_aptitude
	if = {
		limit = {
			scope:actor = {
				is_landed_or_landless_administrative = yes
			}
		}
		custom_tooltip = castration_governor_efficiency
	}
	#might get wounded
	custom_tooltip = castration_wounded_chance
	hidden_effect = {
		random_list = {
			10 = {
				add_character_modifier = {
					modifier = brutal_castration_modifier
					years = 5
				}
			}
			5 = {
				modifier = {
					add = 5
					employs_court_position = court_physician_court_position
				}
				modifier = {
					add = 5
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 1
						}
					}
				}
				modifier = {
					add = 10
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 2
						}
					}
				}
				modifier = {
					add = 10
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 3
						}
					}
				}
				modifier = {
					add = 10
					employs_court_position = court_physician_court_position
					court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 4
						}
					}
				}
			}
		}
	}
}

ep3_greek_fire_success_effect = {
	scope:invading_commander = {
		add_character_modifier = {
			modifier = ravaged_by_greek_fire_modifier
			years = 1
		}
		add_stress = major_stress_impact_gain
	}
	scope:army = {
		deplete_army_by_percentage = 0.9
	}
	scope:invading_commander = {
		custom_tooltip = two_knights_killed_tooltip
		hidden_effect = {
			scope:knight_1 ?= {
				death = {
					death_reason = death_greek_fire
					killer = scope:city_defender
				}
			}
			scope:knight_2 ?= {
				death = {
					death_reason = death_greek_fire
					killer = scope:city_defender
				}
			}
		}
		custom_tooltip = three_knights_killed_tooltip
		hidden_effect = {
			scope:knight_3 ?= {
				increase_wounds_effect = { REASON = greek_fire }
			}
			scope:knight_4 ?= {
				increase_wounds_effect = { REASON = greek_fire }
			}
			scope:knight_5 ?= {
				increase_wounds_effect = { REASON = greek_fire }
			}
		}
	}

}

ep3_greek_fire_fail_effect = {
	scope:invading_commander = {
		add_character_modifier = {
			modifier = assaulted_by_greek_fire_modifier
			years = 1
		}
		add_stress = medium_stress_impact_gain
	}
	scope:knight_1 ?= {
		death = {
			death_reason = death_greek_fire
			killer = scope:city_defender
		}
	}
	scope:army = {
		deplete_army_by_percentage = 0.4
	}
	scope:invading_commander = {
		custom_tooltip = two_knights_wounded_tooltip
		hidden_effect = {
			scope:knight_2 ?= {
				increase_wounds_effect = { REASON = greek_fire }
			}
			scope:knight_3 ?= {
				increase_wounds_effect = { REASON = greek_fire }
			}
		}
	}
}

ep3_greek_fire_crit_fail_effect = {
	scope:invading_commander = {
		add_character_modifier = {
			modifier = assaulted_by_greek_fire_modifier
			years = 1
		}
	}
	scope:army = {
		deplete_army_by_percentage = 0.1
	}
	scope:invading_commander = {
		custom_tooltip = two_knights_wounded_tooltip
		hidden_effect = {
			scope:knight_1 ?= {
				increase_wounds_effect = { REASON = greek_fire }
			}
			scope:knight_2 ?= {
				increase_wounds_effect = { REASON = greek_fire }
			}
		}
	}

	root.capital_county = {
		add_county_modifier = {
			modifier = defenders_immolated_modifier
			years = 10
		}
	}
	#You lose the greek fire fleet modifier
	title:c_byzantion = {
		remove_county_modifier = defensive_fire_dromons_modifier
	}
}

save_traveler_culture_effect = {
	random_culture_global = {
		limit = {
			exists = culture_head
			this != root.culture
		}
		save_scope_as = traveler_culture
	}
	#save second culture
	random_culture_global = {
		limit = {
			exists = culture_head
			this != root.culture
			OR = {
				AND = {
					culture_overlaps_geographical_region = world_europe
					scope:traveler_culture = { culture_overlaps_geographical_region = world_europe }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_minor
					scope:traveler_culture = { culture_overlaps_geographical_region = world_europe }
				}
				AND = {
					culture_overlaps_geographical_region = world_europe
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_minor }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_minor
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_minor }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_minor
					scope:traveler_culture = { culture_overlaps_geographical_region = world_middle_east }
				}
				AND = {
					culture_overlaps_geographical_region = world_middle_east
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_minor }
				}
				AND = {
					culture_overlaps_geographical_region = world_middle_east
					scope:traveler_culture = { culture_overlaps_geographical_region = world_middle_east }
				}
				AND = {
					culture_overlaps_geographical_region = world_africa
					scope:traveler_culture = { culture_overlaps_geographical_region = world_africa }
				}
				AND = {
					culture_overlaps_geographical_region = world_steppe
					scope:traveler_culture = { culture_overlaps_geographical_region = world_steppe }
				}
				AND = {
					culture_overlaps_geographical_region = world_tibet
					scope:traveler_culture = { culture_overlaps_geographical_region = world_tibet }
				}
				AND = {
					culture_overlaps_geographical_region = world_tibet
					scope:traveler_culture = { culture_overlaps_geographical_region = world_india}
				}
				AND = {
					culture_overlaps_geographical_region = world_india
					scope:traveler_culture = { culture_overlaps_geographical_region = world_tibet }
				}
				AND = {
					culture_overlaps_geographical_region = world_india
					scope:traveler_culture = { culture_overlaps_geographical_region = world_india }
				}
				AND = {
					culture_overlaps_geographical_region = world_burma
					scope:traveler_culture = { culture_overlaps_geographical_region = world_india }
				}
				AND = {
					culture_overlaps_geographical_region = world_india
					scope:traveler_culture = { culture_overlaps_geographical_region = world_burma }
				}
				AND = {
					culture_overlaps_geographical_region = world_tibet
					scope:traveler_culture = { culture_overlaps_geographical_region = world_burma }
				}
				AND = {
					culture_overlaps_geographical_region = world_burma
					scope:traveler_culture = { culture_overlaps_geographical_region = world_tibet }
				}
				AND = {
					culture_overlaps_geographical_region = world_tibet
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_china }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_china
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_china }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_china
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_southeast }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_china
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_korea }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_china
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_japan }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_korea
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_china }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_korea
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_japan }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_korea
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_korea }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_japan
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_japan }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_japan
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_china }
				}
				AND = {
					culture_overlaps_geographical_region = world_asia_japan
					scope:traveler_culture = { culture_overlaps_geographical_region = world_asia_korea }
				}
			}
			cultural_acceptance = {
				target = scope:traveler_culture
				value >= 5
			}
		}
		alternative_limit = {
			this = root.culture
		}
		save_scope_as = traveler_culture_2
	}
}
ep3_ai_castration_effect = {
	random = {
		chance = 50
		add_trait = beardless_eunuch
		random_list = {
			5 = {
				add_character_modifier = {
					modifier = brutal_castration_modifier
					years = 5
				}
			}
			5 = {}
			5 = {
				trigger = {
					liege ?= { employs_court_position = court_physician_court_position }
				}
				modifier = {
					add = 5
					liege.court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 1
						}
					}
				}
				modifier = {
					add = 10
					liege.court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 2
						}
					}
				}
				modifier = {
					add = 10
					liege.court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 3
						}
					}
				}
				modifier = {
					add = 10
					liege.court_position:court_physician_court_position = {
						aptitude = {
							court_position = court_physician_court_position
							value >= 4
						}
					}
				}
			}
		}
	}
}

ep3_story_cycle_admin_eunuch_upgrade_modifier_effect = {
	save_scope_value_as = {
		name = modifier_type
		value = flag:$TYPE$
	}
	switch = {
		trigger = has_character_modifier
		admin_eunuch_$TYPE$_1_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_1_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_2_modifier
		}
		admin_eunuch_$TYPE$_2_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_2_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_3_modifier
		}
		admin_eunuch_$TYPE$_3_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_3_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_4_modifier
		}
		admin_eunuch_$TYPE$_4_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_4_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_5_modifier
		}
		admin_eunuch_$TYPE$_5_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_5_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_6_modifier
		}
		admin_eunuch_$TYPE$_6_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_6_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_7_modifier
		}
		admin_eunuch_$TYPE$_7_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_7_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_8_modifier
		}
		admin_eunuch_$TYPE$_8_modifier = {
			if = {
				limit = {
					scope:modifier_type = flag:eunuch
					NOT = { has_hook = top_liege }
				}
				add_hook = {
					type = influence_hook
					target = top_liege
				}
			}
			else_if = {
				limit = {
					scope:modifier_type = flag:eunuch
					NOT = { has_strong_hook = top_liege }
				}
				remove_hook = { target = top_liege }
				add_hook = {
					type = strong_influence_hook
					target = top_liege
				}
			}
			else = { change_influence = medium_influence_gain }
		}
		fallback = { add_character_modifier = admin_eunuch_$TYPE$_4_modifier }
	}
}

ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
	$FIRST$ = {
		switch = {
			trigger = has_character_modifier
			admin_eunuch_$FIRST_TYPE$_1_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_1_modifier }
			}
			admin_eunuch_$FIRST_TYPE$_2_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_2_modifier }
			}
			admin_eunuch_$FIRST_TYPE$_3_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_3_modifier }
			}
			admin_eunuch_$FIRST_TYPE$_4_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_4_modifier }
			}
			admin_eunuch_$FIRST_TYPE$_5_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_5_modifier }
			}
			admin_eunuch_$FIRST_TYPE$_6_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_6_modifier }
			}
			admin_eunuch_$FIRST_TYPE$_7_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_7_modifier }
			}
			admin_eunuch_$FIRST_TYPE$_8_modifier = {
				$SECOND$ = { add_character_modifier = admin_eunuch_$SECOND_TYPE$_8_modifier }
			}
		}
	}
}

ep3_story_cycle_admin_eunuch_downgrade_modifier_effect = {
	save_scope_value_as = {
		name = modifier_type
		value = flag:$TYPE$
	}
	switch = {
		trigger = has_character_modifier
		admin_eunuch_$TYPE$_1_modifier = {
			if = {
				limit = { scope:modifier_type = flag:eunuch }
				progress_towards_rival_effect = {
					REASON = rival_admin_eunuch
					CHARACTER = top_liege
					OPINION = default_rival_opinion
				}
			}
			else = { change_influence = medium_influence_loss }
		}
		admin_eunuch_$TYPE$_2_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_2_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_1_modifier
			if = { # Harder to say no the further you go
				limit = { scope:modifier_type = flag:liege }
				change_influence = miniscule_influence_loss
			}
		}
		admin_eunuch_$TYPE$_3_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_3_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_2_modifier
			if = { # Harder to say no the further you go
				limit = { scope:modifier_type = flag:liege }
				change_influence = miniscule_influence_loss
			}
		}
		admin_eunuch_$TYPE$_4_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_4_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_3_modifier
			if = { # Harder to say no the further you go
				limit = { scope:modifier_type = flag:liege }
				change_influence = minor_influence_loss
			}
		}
		admin_eunuch_$TYPE$_5_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_5_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_4_modifier
			if = { # Harder to say no the further you go
				limit = { scope:modifier_type = flag:liege }
				change_influence = minor_influence_loss
			}
		}
		admin_eunuch_$TYPE$_6_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_6_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_5_modifier
			if = { # Harder to say no the further you go
				limit = { scope:modifier_type = flag:liege }
				change_influence = medium_influence_loss
			}
		}
		admin_eunuch_$TYPE$_7_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_7_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_6_modifier
			if = { # Harder to say no the further you go
				limit = { scope:modifier_type = flag:liege }
				change_influence = medium_influence_loss
			}
		}
		admin_eunuch_$TYPE$_8_modifier = {
			hidden_effect = { remove_character_modifier = admin_eunuch_$TYPE$_8_modifier }
			add_character_modifier = admin_eunuch_$TYPE$_7_modifier
			if = { # Harder to say no the further you go
				limit = { scope:modifier_type = flag:liege }
				change_influence = major_influence_loss
			}
		}
		fallback = { add_character_modifier = admin_eunuch_$TYPE$_3_modifier }
	}
}

ep3_story_cycle_admin_eunuch_clear_modifier_effect = {
	switch = {
		trigger = has_character_modifier
		admin_eunuch_$TYPE$_1_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_1_modifier }
		admin_eunuch_$TYPE$_2_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_2_modifier }
		admin_eunuch_$TYPE$_3_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_3_modifier }
		admin_eunuch_$TYPE$_4_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_4_modifier }
		admin_eunuch_$TYPE$_5_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_5_modifier }
		admin_eunuch_$TYPE$_6_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_6_modifier }
		admin_eunuch_$TYPE$_7_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_7_modifier }
		admin_eunuch_$TYPE$_8_modifier = { remove_character_modifier = admin_eunuch_$TYPE$_8_modifier }
	}
}

ep3_give_eunuch_effect = {
	$TAKER$ = {
		add_courtier = $EUNUCH$
	}
	$GIVER$ = {
		change_influence = {
			value = 100
			add = $EUNUCH$.sum_of_all_skills_value
			if = {
				limit = {
					$EUNUCH$ = { has_trait = beardless_eunuch }
				}
				multiply = 1.5
			}
		}
	}
}

##################################################
# Misc
##################################################

# Picks a random valid maa type for OWNER to create
ep3_pick_random_maa_regiment_effect = {
	save_temporary_scope_value_as = {
		name = include_basic_maa
		value = $INCLUDE_BASIC_MAA$
	}
	save_temporary_scope_value_as = {
		name = include_siege_maa
		value = $INCLUDE_SIEGE_MAA$
	}
	$OWNER$ = {
		random_list = {
			1 = {
				trigger = {
					scope:include_basic_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:light_footmen
				}
			}
			1 = {
				trigger = {
					scope:include_basic_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:bowmen
				}
			}
			1 = {
				trigger = {
					scope:include_basic_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:light_horsemen
				}
			}
			1 = {
				trigger = {
					scope:include_basic_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:pikemen_unit
				}
			}
			# TRIBAL
			1 = {
				trigger = {
					culture = { has_innovation = innovation_elephantry }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:war_elephant
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_war_camels }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:camel_rider
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_arched_saddle }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:armored_horsemen
				}
			}
			1 = {
				trigger = {
					culture = {
						has_innovation = innovation_catapult
						NOR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:onager
				}
			}
			1 = {
				trigger = {
					culture = {
						has_innovation = innovation_catapult
						OR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:ballista
				}
			}
			# EARLY
			1 = {
				trigger = {
					culture = { has_innovation = innovation_quilted_armor }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:armored_footmen
				}
			}
			1 = {
				trigger = {
					culture = {
						has_innovation = innovation_mangonel
						NOR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:mangonel
				}
			}
			1 = {
				trigger = {
					culture = {
						has_innovation = innovation_mangonel
						OR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:cloud_ladder
				}
			}
			# HIGH
			1 = {
				trigger = {
					culture = {
						has_innovation = innovation_trebuchet
						NOR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:trebuchet
				}
			}
			1 = {
				trigger = {
					culture = {
						has_innovation = innovation_trebuchet
						OR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:siege_tower
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_advanced_bowmaking }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:crossbowmen
				}
			}
			# LATE
			1 = {
				trigger = {
					culture = {
						has_cultural_parameter = unlock_late_medieval_gunpowder_units
						NOR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:bombard
				}
			}
			1 = {
				trigger = {
					culture = {
						has_cultural_parameter = unlock_late_medieval_gunpowder_units
						OR = {
							has_cultural_pillar = heritage_chinese
							has_cultural_pillar = heritage_korean
							has_cultural_pillar = heritage_japonic
						}
					}
					scope:include_siege_maa = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:cannon
				}
			}
			# CULTURAL INNOVATIONS
			1 = {
				trigger = {
					culture = { has_innovation = innovation_bamboo_bows }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:paiks
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_zweihanders }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:landsknecht
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_adaptive_militia }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:goedendag
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_valets }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:gendarme
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_rocket_cart }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:rocket_carts
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_pike_columns }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:picchieri
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_legionnaires }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:praetorian
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_desert_tactics }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:chasseur
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_caballeros }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:caballero
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_hobbies }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:hobelar
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_rectilinear_schiltron }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:schiltron
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_sahel_horsemen }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:sahel_horsemen
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_sarawit }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:sarawit
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_repeating_crossbow }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:shenbigong
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_pole_vault }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:guanch_vaulter
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_huscarls }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:huscarl
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_longbowmen }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:longbowmen
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_monaspa }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:monaspa
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_cataphract }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:cataphract
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_metsanvartija }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:metsanvartija
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_maturkan_warriors }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:maturkan_warriors
				}
			}
			1 = {
				trigger = {
					OR = {
						culture = { has_cultural_parameter = unlock_maa_horse_archers }
						mpo_can_recruit_nomad_maa_trigger = yes
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:horse_archers
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_ayyar }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:ayyar
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_mubarizun }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:mubarizun
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_mulaththamun }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:mulaththamun
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_archers_of_the_nile }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:archers_of_the_nile
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_druzhina }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:druzhina
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_hussar }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:hussar
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_horn_warrior }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:horn_warrior
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_bush_hunter }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:bush_hunter
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_shomer }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:shomer
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_garudas }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:garudas
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_khandayat }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:khandayat
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_mountaineer }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:mountaineer
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_guinea_warrior }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:guinea_warrior
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_abudrar }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:abudrar
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_zbrojnosh }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:zbrojnosh
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_palace_guards }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:palace_guards
				}
			}
			1 = {
				trigger = {
					dynasty ?= {
						has_dynasty_perk = fp3_khvarenah_legacy_4
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:asawira
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_tarkhan }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:tarkhan
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_zupin_warrior }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:zupin_warrior
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_tawashi }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:tawashi
				}
			}
			1 = {
				trigger = {
					dynasty ?= {
						has_dynasty_perk = warfare_legacy_5
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:house_guard
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:archer_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_archers
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:skirmisher_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_skirmishers
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:pike_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_pikes
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:outrider_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_outriders
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:vanguard_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_vanguards
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:lancer_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_lancers
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:crossbowmen_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_crossbowers
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:camelry_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_cameliers
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:elephantry_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_elephantiers
				}
			}
			1 = {
				trigger = {
					any_active_accolade = {
						primary_type = {
							this = accolade_type:horse_archer_attribute
						}
						accolade_rank >= 3
					}
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:accolade_maa_horse_archers
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_vigmen }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:vigmen
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_bondi }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:bondi
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_varangian_veterans }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:varangian_veterans
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_ayrudzi }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:ayrudzi
				}
			}
			1 = {
				trigger = {
					mpo_can_recruit_nomad_maa_trigger = yes
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:steppe_raiders
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_conrois }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:conrois
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_akritai }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:akritai
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_ballistrai }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:ballistrai
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_skoutatoi }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:skoutatoi
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_warrior_monk_units }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:warrior_monks
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_emishi_horse_archers_units }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:emishi_horse_archers
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_mounted_samurai_units }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:japanese_horse_archers
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_pesilat }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:pesilat_warriors
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_gakgung_archers }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:gakgung_archers
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_infantry_samurai_units }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:samurai
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_zhanmadao }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:zhanmadao_infantry
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_burenjia }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:burenjia
				}
			}
			1 = {
				trigger = {
					culture = { has_innovation = innovation_tiefutu }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:tiefutu
				}
			}
			1 = {
				trigger = {
					culture = { has_cultural_parameter = unlock_maa_black_armor_cavalry }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:black_armor_cavalry
				}
			}
			1 = {
				trigger = {
					exists = scope:title
					scope:title = title:e_byzantium
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:varangian_guards
				}
			}
			1 = {
				trigger = {
			   		domicile ?= { has_domicile_parameter = camp_unlocks_handpicked_faithful_maa_type }
				}
				save_scope_value_as = {
					name = maa_to_create
					value = flag:handpicked_faithful
				}
			}
		}
	}
	#in case you don't fullfil any criteria just pick a random default maa
	if = {
		limit = {
			NOT = { exists = scope:maa_to_create }
		}
		random_list = {
			1 = {
				save_scope_value_as = {
					name = maa_to_create
					value = flag:light_footmen
				}
			}
			1 = {
				save_scope_value_as = {
					name = maa_to_create
					value = flag:bowmen
				}
			}
			1 = {
				save_scope_value_as = {
					name = maa_to_create
					value = flag:light_horsemen
				}
			}
			1 = {
				save_scope_value_as = {
					name = maa_to_create
					value = flag:pikemen_unit
				}
			}
		}
	}
}

# creates the maa type for OWNER of SIZE set by the above
ep3_create_random_maa_regiment_effect = {
	$OWNER$ = {
		switch = {
			trigger = scope:maa_to_create
			flag:light_footmen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = light_footmen
					SIZE = $SIZE$
				}
			}
			flag:bowmen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = bowmen
					SIZE = $SIZE$
				}
			}
			flag:light_horsemen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = light_horsemen
					SIZE = $SIZE$
				}
			}
			flag:pikemen_unit = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = pikemen_unit
					SIZE = $SIZE$
				}
			}
			flag:war_elephant = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = war_elephant
					SIZE = $SIZE$
				}
			}
			flag:camel_rider = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = camel_rider
					SIZE = $SIZE$
				}
			}
			flag:armored_horsemen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = armored_horsemen
					SIZE = $SIZE$
				}
			}
			flag:onager = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = onager
					SIZE = $SIZE$
				}
			}
			flag:armored_footmen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = armored_footmen
					SIZE = $SIZE$
				}
			}
			flag:mangonel = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = mangonel
					SIZE = $SIZE$
				}
			}
			flag:trebuchet = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = trebuchet
					SIZE = $SIZE$
				}
			}
			flag:crossbowmen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = crossbowmen
					SIZE = $SIZE$
				}
			}
			flag:bombard = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = bombard
					SIZE = $SIZE$
				}
			}
			flag:paiks = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = paiks
					SIZE = $SIZE$
				}
			}
			flag:landsknecht = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = landsknecht
					SIZE = $SIZE$
				}
			}
			flag:goedendag = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = goedendag
					SIZE = $SIZE$
				}
			}
			flag:gendarme = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = gendarme
					SIZE = $SIZE$
				}
			}
			flag:picchieri = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = picchieri
					SIZE = $SIZE$
				}
			}
			flag:praetorian = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = praetorian
					SIZE = $SIZE$
				}
			}
			flag:chasseur = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = chasseur
					SIZE = $SIZE$
				}
			}
			flag:caballero = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = caballero
					SIZE = $SIZE$
				}
			}
			flag:hobelar = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = hobelar
					SIZE = $SIZE$
				}
			}
			flag:schiltron = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = schiltron
					SIZE = $SIZE$
				}
			}
			flag:sahel_horsemen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = sahel_horsemen
					SIZE = $SIZE$
				}
			}
			flag:sarawit = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = sarawit
					SIZE = $SIZE$
				}
			}
			flag:shenbigong = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = shenbigong
					SIZE = $SIZE$
				}
			}
			flag:guanch_vaulter = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = guanch_vaulter
					SIZE = $SIZE$
				}
			}
			flag:huscarl = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = huscarl
					SIZE = $SIZE$
				}
			}
			flag:longbowmen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = longbowmen
					SIZE = $SIZE$
				}
			}
			flag:monaspa = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = monaspa
					SIZE = $SIZE$
				}
			}
			flag:cataphract = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = cataphract
					SIZE = $SIZE$
				}
			}
			flag:metsanvartija = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = metsanvartija
					SIZE = $SIZE$
				}
			}
			flag:maturkan_warriors = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = maturkan_warriors
					SIZE = $SIZE$
				}
			}
			flag:horse_archers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = horse_archers
					SIZE = $SIZE$
				}
			}
			flag:ayyar = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = ayyar
					SIZE = $SIZE$
				}
			}
			flag:mubarizun = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = mubarizun
					SIZE = $SIZE$
				}
			}
			flag:mulaththamun = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = mulaththamun
					SIZE = $SIZE$
				}
			}
			flag:archers_of_the_nile = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = archers_of_the_nile
					SIZE = $SIZE$
				}
			}
			flag:druzhina = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = druzhina
					SIZE = $SIZE$
				}
			}
			flag:hussar = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = hussar
					SIZE = $SIZE$
				}
			}
			flag:horn_warrior = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = horn_warrior
					SIZE = $SIZE$
				}
			}
			flag:bush_hunter = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = bush_hunter
					SIZE = $SIZE$
				}
			}
			flag:shomer = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = shomer
					SIZE = $SIZE$
				}
			}
			flag:garudas = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = garudas
					SIZE = $SIZE$
				}
			}
			flag:khandayat = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = khandayat
					SIZE = $SIZE$
				}
			}
			flag:mountaineer = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = mountaineer
					SIZE = $SIZE$
				}
			}
			flag:guinea_warrior = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = guinea_warrior
					SIZE = $SIZE$
				}
			}
			flag:abudrar = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = abudrar
					SIZE = $SIZE$
				}
			}
			flag:zbrojnosh = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = zbrojnosh
					SIZE = $SIZE$
				}
			}
			flag:palace_guards = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = palace_guards
					SIZE = $SIZE$
				}
			}
			flag:asawira = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = asawira
					SIZE = $SIZE$
				}
			}
			flag:tarkhan = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = tarkhan
					SIZE = $SIZE$
				}
			}
			flag:zupin_warrior = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = zupin_warrior
					SIZE = $SIZE$
				}
			}
			flag:tawashi = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = tawashi
					SIZE = $SIZE$
				}
			}
			flag:house_guard = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = house_guard
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_archers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_archers
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_skirmishers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_skirmishers
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_pikes = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_pikes
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_outriders = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_outriders
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_vanguards = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_vanguards
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_lancers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_lancers
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_crossbowers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_crossbowers
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_cameliers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_cameliers
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_elephantiers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_elephantiers
					SIZE = $SIZE$
				}
			}
			flag:accolade_maa_horse_archers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = accolade_maa_horse_archers
					SIZE = $SIZE$
				}
			}
			flag:vigmen = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = vigmen
					SIZE = $SIZE$
				}
			}
			flag:bondi = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = bondi
					SIZE = $SIZE$
				}
			}
			flag:varangian_veterans = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = varangian_veterans
					SIZE = $SIZE$
				}
			}
			flag:ayrudzi = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = ayrudzi
					SIZE = $SIZE$
				}
			}
			flag:steppe_raiders = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = steppe_raiders
					SIZE = $SIZE$
				}
			}
			flag:conrois = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = conrois
					SIZE = $SIZE$
				}
			}
			flag:akritai = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = akritai
					SIZE = $SIZE$
				}
			}
			flag:ballistrai = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = ballistrai
					SIZE = $SIZE$
				}
			}
			flag:skoutatoi = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = skoutatoi
					SIZE = $SIZE$
				}
			}
			flag:varangian_guards = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = varangian_guards
					SIZE = $SIZE$
				}
			}
			flag:handpicked_faithful = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = handpicked_faithful
					SIZE = $SIZE$
				}
			}
			flag:emishi_horse_archers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = emishi_horse_archers
					SIZE = $SIZE$
				}
			}
			flag:japanese_horse_archers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = japanese_horse_archers
					SIZE = $SIZE$
				}
			}
			flag:warrior_monks = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = warrior_monks
					SIZE = $SIZE$
				}
			}
			flag:gakgung_archers = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = gakgung_archers
					SIZE = $SIZE$
				}
			}
			flag:pesilat_warriors = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = pesilat_warriors
					SIZE = $SIZE$
				}
			}
			flag:samurai = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = samurai
					SIZE = $SIZE$
				}
			}
			flag:zhanmadao_infantry = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = zhanmadao_infantry
					SIZE = $SIZE$
				}
			}
			flag:burenjia = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = burenjia
					SIZE = $SIZE$
				}
			}
			flag:tiefutu = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = tiefutu
					SIZE = $SIZE$
				}
			}
			flag:black_armor_cavalry = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = black_armor_cavalry
					SIZE = $SIZE$
				}
			}
			flag:rocket_carts = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = rocket_carts
					SIZE = $SIZE$
				}
			}
			flag:ballista = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = ballista
					SIZE = $SIZE$
				}
			}
			flag:cloud_ladder = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = cloud_ladder
					SIZE = $SIZE$
				}
			}
			flag:siege_tower = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = siege_tower
					SIZE = $SIZE$
				}
			}
			flag:cannon = {
				create_maa_or_upgrade_regiment_effect = {
					TYPE = cannon
					SIZE = $SIZE$
				}
			}
		}
	}
}

create_maa_or_upgrade_regiment_effect = {
	if = {
		limit = {
			any_maa_regiment = {
				is_maa_type = $TYPE$
				can_upgrade_maa = yes
			}
		}
		random_maa_regiment = {
			limit = {
				is_maa_type = $TYPE$
				can_upgrade_maa = yes
			}
			change_maa_regiment_size = $SIZE$
		}
	}
	else = {
		create_maa_regiment = {
			type = $TYPE$
			check_can_recruit = no
			size = $SIZE$
		}
	}
}

ep3_missionary_effect = {
	# Same religion or unreformed or Hasan, convert to the preacher's faith
	if = {
		limit = {
			OR = {
				$LOCATION$.religion = $PREACHER$.religion
				$LOCATION$.faith = { has_doctrine_parameter = unreformed }
				character:41702 ?= $PREACHER$
			}
		}
		$LOCATION$ = {
			set_county_faith = $PREACHER$.faith
		}
	}
	# Preacher is Gnostic, convert the locals to a regionally appropriate form of Gnosticism
	else_if = {
		limit = {
			$PREACHER$.faith = {
				OR = {
					has_doctrine = tenet_gnosticism
					has_doctrine = special_doctrine_is_gnostic_faith
				}
			}
			$LOCATION$.religion = {
				any_faith = {
					has_doctrine = tenet_gnosticism
				}
			}
		}
		$LOCATION$ = {
			religion = {
				random_faith = {
					limit = {
						OR = {
							has_doctrine = tenet_gnosticism
							has_doctrine = special_doctrine_is_gnostic_faith
						}
					}
					$LOCATION$ = { set_county_faith = prev }
				}
			}
		}
	}
	# Otherwise, reduce popular opinion
	else = {
		$LOCATION$ = {
			add_county_modifier = {
				modifier = ep3_religious_doubts
				years = 15
			}
		}
	}
}
ep3_hasan_missionary_effect = {
	ep3_hasan_advance_radicalism = yes
	ep3_missionary_effect = {
		PREACHER = $PREACHER$
		LOCATION = $LOCATION$
	}
}
ep3_hasan_advance_radicalism = {
	if = {
		limit = {
			any_owned_story = {
				type = story_hasan
			}
		}
		custom_tooltip = {
			text = advance_to_the_assassins_tt
			random_owned_story = {
				type = story_hasan
				if = {
					limit = {
						exists = var:radical_points
					}
					change_variable = {
						name = radical_points
						add = 1
					}
				}
				else = {
					set_variable = {
						name = radical_points
						value = 1
					}
				}
			}
		}
	}
}
hasan_sabbah_ignite_decision_effect = {
	if = {
		limit = {
			hasan_sabbah_event_troops_value <= 1000
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = ayyar
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = ayyar
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = ayyar
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
	}
	else_if = {
		limit = {
			hasan_sabbah_event_troops_value <= 2000
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 1
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
	}
	else_if = {
		limit = {
			hasan_sabbah_event_troops_value <= 3000
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 1
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
	}
	else_if = {
		limit = {
			hasan_sabbah_event_troops_value > 3000
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 2
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 2
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 2
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 2
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
		spawn_army = {
			name = hasan_fanatics
			men_at_arms = {
				type = asawira
				stacks = 2
			}
			men_at_arms = {
				type = ayyar
				stacks = 2
			}
			levies = hasan_sabbah_event_troops_value
			inheritable = yes
			location = root.domicile.domicile_location
			war_keep_on_attacker_victory = yes
		}
	}
	start_war = {
		cb = ep3_hasan_assassin_war
		target = root.location.county.holder.top_liege
		target_title = root.location.county.holder.top_liege.primary_title
	}
}
hasan_sabbah_end_war = {
	# Offer Hasan the opportunity to become landed
	hidden_effect = {
		if = {
			limit = {
				$SELJUK$ = {
					any_sub_realm_barony = {
						title_province = { has_building_or_higher = alamut_castle_01 }
					}
				}
			}
			$SELJUK$ = {
				random_sub_realm_barony = {
					limit = {
						title_province = { has_building_or_higher = alamut_castle_01 }
					}
					county = {
						save_scope_as = hasan_home
					}
				}
			}
		}
		else_if = {
			limit = {
				$SELJUK$ = {
					any_sub_realm_county = {
						faith = $HASAN$.faith
						holder = { is_ai = yes }
					}
				}
			}
			$SELJUK$ = {
				random_sub_realm_county = {
					limit = {
						faith = $HASAN$.faith
						holder = { is_ai = yes }
					}
					save_scope_as = hasan_home
				}
			}
		}
		else = {
			$SELJUK$ = {
				random_sub_realm_county = {
					limit = {
						holder = { is_ai = yes }
					}
					save_scope_as = hasan_home
				}
			}
		}
		$HASAN$ = {
			if = {
				limit = {
					any_owned_story = {
						type = story_hasan
						exists = var:preferred_caliph
					}
				}
				random_owned_story = {
					type = story_hasan
					var:preferred_caliph = { save_scope_as = caliph }
				}
			}
			trigger_event = {
				id = hasan_sabbah.1050
			}
			if = {
				limit = {
					is_ai = no
				}
				add_achievement_global_variable_effect = {
					VARIABLE = finished_the_old_man_of_the_mountain_achievement
					VALUE = yes
				}
			}
		}
	}
	create_title_and_vassal_change = {
		type = independency
		save_scope_as = change
	}
	$HASAN$ = {
		add_prestige = medium_prestige_value
	}
	$SELJUK$ = {
		add_prestige = {
			value = medium_prestige_value
			multiply = -1
		}

		every_vassal = {
			limit = {
				highest_held_title_tier > tier_barony
			}
			change_liege_or_become_independent = {
				CHANGE = scope:change
				VASSAL = this
			}
		}

		every_vassal = {
			limit = { is_ai = no }
			send_interface_toast = {
				type = msg_gained_independence
				title = liege_primary_title_dissolution
				desc = liege_primary_title_dissolution_desc
				left_icon = $SELJUK$
			}
		}
	}
	resolve_title_and_vassal_change = scope:change
	$SELJUK$ = {
		every_held_title = {
			limit = {
				tier >= tier_duchy
				is_head_of_faith = no
				NOT = { this = $SELJUK$.capital_county.duchy }
			}
			add_to_temporary_list = titles_to_destroy
		}
	}
	every_in_list = {
		list = titles_to_destroy
		$HASAN$ = {
			destroy_title = prev
		}
	}
	$SELJUK$ = {
		if = {
			limit = {
				government_has_flag = government_is_administrative
			}
			admin_government_reset_effect = yes
		}
	}
	# LEGITIMACY FROM LOSING FACTION WAR
	faction_war_end_defeat_legitimacy_effect = yes
	# End Hasan's story cycle
	hidden_effect = {
		$HASAN$ = {
			if = {
				limit = {
					any_owned_story = {
						type = story_hasan
					}
				}
				random_owned_story = {
					type = story_hasan
					end_story = yes
				}
			}
		}
	}
}

reinforce_soldiers_regiment_effect = {
	save_scope_as = laamp_temp
	# For the value.
	save_temporary_scope_as = actor
	custom_tooltip = {
		text = reinforce_soldiers_effect_tt
		every_maa_regiment = {
			limit = { maa_regiments_valid_for_cheap_reinforcement_trigger = yes }
			change_maa_troops_count = reinforce_soldiers_regiment_soldiers_value
		}
	}
}

refill_maa_with_provisions_effect = {
	save_scope_as = laamp_temp
	while = {
		limit = {
			scope:actor.domicile.provisions > 0
			any_maa_regiment = {
				maa_regiments_valid_to_refill_trigger = yes
			}
		}
		random_maa_regiment = {
			limit = {
				maa_regiments_valid_to_refill_trigger = yes
			}
			save_scope_as = maa_scope
			save_scope_value_as = {
				name = current_provisions_max_value_scope
				value = scope:actor.domicile.provisions
			}
			scope:actor.domicile ?= {
				change_provisions = {
					value = {
						add = scope:maa_scope.replenishable_troops_value
						divide = provision_refill_value
						max = scope:current_provisions_max_value_scope
						multiply = -1
					}
				}
			}
			change_maa_troops_count = replenishable_troops_with_provisions_value
		}
	}
}

refill_maa_with_gold_effect = {
	save_scope_as = laamp_temp
	while = {
		limit = {
			scope:actor.gold > 0
			any_maa_regiment = {
				maa_regiments_valid_to_refill_trigger = yes
			}
		}
		random_maa_regiment = {
			limit = {
				maa_regiments_valid_to_refill_trigger = yes
			}
			save_scope_as = maa_scope
			save_scope_value_as = {
				name = current_gold_max_value_scope
				value = scope:actor.gold
			}
			scope:actor = {
				remove_short_term_gold = {
					add = scope:maa_scope.replenishable_troops_value
					divide = gold_refill_value
					max = scope:current_gold_max_value_scope
				}
			}
			change_maa_troops_count = replenishable_troops_with_gold_value
		}
	}
}

admin_government_reset_effect = {
	if = {
		limit = { is_landed = yes } # error suppression
		### NOMADIC
		if = {
			limit = {
				OR = {
					government_has_flag = government_is_nomadic
					capital_province = {
						OR = {
							has_holding_type = nomad_holding
							has_holding_type = herder_holding
						}
					}
				}
			}
			change_government = nomad_government
		}
		### TRIBAL - are all of your holdings Tribal?
		if = {
			limit = {
				NOT = {
					any_held_title = {
						trigger_if = {
							limit = { tier <= tier_county }
							title_province ?= {
								NOT = { has_holding_type = tribal_holding }
							}
						}
						trigger_else = {
							title_capital_county.title_province ?= {
								NOT = { has_holding_type = tribal_holding }
							}
						}
					}
				}
			}
			change_government = tribal_government
		}
		### CLAN - are you a Muslim or from a Clan culture?
		else_if = {
			limit = {
				OR = {
					faith = { religion_tag = islam_religion }
					culture = {
						OR = {
							has_cultural_pillar = heritage_arabic
							has_cultural_pillar = heritage_iranian
							has_cultural_pillar = heritage_turkic
						}
					}
				}
			}
			change_government = clan_government
		}
		### FEUDAL - fallback playable case
		else = { change_government = feudal_government }
	}
}

create_basic_peasant_effect = {
	create_character = {
		template = peasant_character
		location = root.location
		dynasty = none
		culture = scope:new_landless_minority.culture
		faith = scope:new_landless_minority.faith
		gender_female_chance = 50
		after_creation = {
			set_to_lowborn = yes
		}
		save_scope_as = peasant
	}
}

fire_governor_long_war_response_events = {
	if = {
		limit = {
			scope:defender = {
				government_has_flag = government_is_administrative
				top_liege = this
				any_vassal = {
					is_governor = yes
					NOR = {
						this = scope:attacker
						is_allied_to = scope:attacker
					}
				}
			}
		}
		scope:defender = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:attacker
						is_allied_to = scope:attacker
					}
				}
				save_scope_as = def_governor_1
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 24
				}
			}
		}
		scope:defender = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:attacker
						is_allied_to = scope:attacker
						this = scope:def_governor_1
					}
				}
				save_scope_as = def_governor_2
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 28
				}
			}
		}
		scope:defender = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:attacker
						is_allied_to = scope:attacker
						this = scope:def_governor_1
						this = scope:def_governor_2
					}
				}
				save_scope_as = def_governor_3
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 32
				}
			}
		}
		scope:defender = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:attacker
						is_allied_to = scope:attacker
						this = scope:def_governor_1
						this = scope:def_governor_2
						this = scope:def_governor_3
					}
				}
				save_scope_as = def_governor_4
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 36
				}
			}
		}
		scope:defender = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:attacker
						is_allied_to = scope:attacker
						this = scope:def_governor_1
						this = scope:def_governor_2
						this = scope:def_governor_3
						this = scope:def_governor_4
					}
				}
				save_scope_as = def_governor_5
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 42
				}
			}
		}
		scope:defender = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:attacker
						is_allied_to = scope:attacker
						this = scope:def_governor_1
						this = scope:def_governor_2
						this = scope:def_governor_3
						this = scope:def_governor_4
						this = scope:def_governor_5
					}
				}
				save_scope_as = def_governor_6
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 54
				}
			}
		}
	}
	if = {
		limit = {
			scope:attacker = {
				government_has_flag = government_is_administrative
				top_liege = this
				any_vassal = {
					is_governor = yes
					NOR = {
						this = scope:defender
						is_allied_to = scope:defender
					}
				}
			}
		}
		scope:attacker = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:defender
						is_allied_to = scope:defender
					}
				}
				save_scope_as = att_governor_1
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 24
				}
			}
		}
		scope:attacker = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:defender
						is_allied_to = scope:defender
						this = scope:att_governor_1
					}
				}
				save_scope_as = att_governor_2
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 28
				}
			}
		}
		scope:attacker = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:defender
						is_allied_to = scope:defender
						this = scope:att_governor_1
						this = scope:att_governor_2
					}
				}
				save_scope_as = att_governor_3
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 32
				}
			}
		}
		scope:attacker = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:defender
						is_allied_to = scope:defender
						this = scope:att_governor_1
						this = scope:att_governor_2
						this = scope:att_governor_3
					}
				}
				save_scope_as = att_governor_4
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 36
				}
			}
		}
		scope:attacker = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:defender
						is_allied_to = scope:defender
						this = scope:att_governor_1
						this = scope:att_governor_2
						this = scope:att_governor_3
						this = scope:att_governor_4
					}
				}
				save_scope_as = att_governor_5
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 42
				}
			}
		}
		scope:attacker = {
			random_vassal = {
				limit = {
					is_governor = yes
					NOR = {
						this = scope:defender
						is_allied_to = scope:defender
						this = scope:att_governor_1
						this = scope:att_governor_2
						this = scope:att_governor_3
						this = scope:att_governor_4
						this = scope:att_governor_5
					}
				}
				save_scope_as = att_governor_6
				trigger_event = {
					id = ep3_governor_yearly.3010
					months = 54
				}
			}
		}
	}
}

ep3_governor_yearly_3010_war_aid_effect = {
	if = {
		limit = {
			scope:emperor = {
				is_defender_in_war = scope:war
			}
		}
		scope:war = {
			add_defender = scope:governor
		}
	}
	else = {
		scope:war = {
			add_attacker = scope:governor
		}

	}
	if = {
		limit = {
			root = scope:emperor
		}
		hidden_effect = {
			reverse_add_opinion = {
				target = scope:emperor
				opinion = 20
				modifier = grateful_opinion
			}
		}
	}
	else = {
		reverse_add_opinion = {
			target = scope:emperor
			opinion = 20
			modifier = grateful_opinion
		}
	}
	pay_short_term_gold = {
		target = scope:emperor
		gold = medium_gold_value
	}
	if = {
		limit = {
			can_add_hook = {
				target = scope:emperor
				type = favor_hook
			}
		}
		add_hook = {
			target = scope:emperor
			type = favor_hook
		}
	}
	else = {
		change_influence = major_influence_gain
	}
}
ep3_admin_war_aftermath_effect = {
	scope:war = {
		if = {
			limit = {
				any_war_participant = {
					is_landed = yes
					government_has_flag = government_is_administrative
				}
			}
			every_war_participant = {
				limit = {
					is_landed = yes
					government_has_flag = government_is_administrative
				}
				primary_title = {
					set_variable = {
						name = admin_war_aftermath_var
						years = 5
					}
				}
			}
		}
	}
}

state_faith_conversion_add_piety_effect = {
	if = {
		limit = {
			scope:actor = {
				top_liege = { government_allows = state_faith }
				faith = top_liege.primary_title.state_faith
			}
			scope:recipient.top_liege = { government_allows = state_faith }
		}
		scope:actor = {
			add_piety = medium_piety_gain
		}
	}
}

state_faith_refuse_conversion_add_piety_effect = {
	if = {
		limit = {
			scope:actor.top_liege = { government_allows = state_faith }
			scope:recipient = {
				top_liege = { government_allows = state_faith }
				faith = top_liege.primary_title.state_faith
			}
		}
		scope:recipient = { add_piety = medium_piety_gain }
	}
}

# EP3 Laamp possibility warning
ep3_war_loss_adventurer_tt_effect = {
	save_scope_as = war_loser
	if = {
		limit = {
			has_ep3_dlc_trigger = yes
			NOT = {
				any_held_title = {
					OR = {
						is_landless_type_title = yes
						is_noble_family_title = yes
					}
				}
			}
			trigger_if = {
				limit = { any_in_list = { list = target_titles } }
				OR = {
					# You are losing all your counties and have no vassals to steal from
					AND = {
						any_held_county = {
							count = all
							OR = {
								is_in_list = target_titles
								target_is_de_jure_liege_or_above = scope:war_loser.primary_title
							}
						}
						any_vassal = {
							count = 0
							highest_held_title_tier >= tier_county
						}
					}
					# You are losing all your held titles
					any_held_title = {
						count = all
						OR = {
							is_in_list = target_titles
							target_is_de_jure_liege_or_above = scope:war_loser.primary_title
						}
					}
				}
			}
		}
		custom_tooltip = laamp_may_become_adventurer_tt
	}
}

add_randomised_double_moon_modifier_effect = {
	hidden_effect = {
		scope:double_moon_book = {
			random_list = {
				1 = {
					add_artifact_modifier = artifact_prowess_2_modifier
					set_artifact_description = double_moon_tome_desc_1
				}
				1 = {
					add_artifact_modifier = artifact_knight_effectiveness_4_modifier
					set_artifact_description = double_moon_tome_desc_2
				}
				1 = {
					add_artifact_modifier = artifact_stewardship_1_modifier
					set_artifact_description = double_moon_tome_desc_3
				}
				1 = {
					add_artifact_modifier = artifact_diplomacy_1_modifier
					set_artifact_description = double_moon_tome_desc_4
				}
				1 = {
					add_artifact_modifier = artifact_health_gain_1_modifier
					set_artifact_description = double_moon_tome_desc_5
				}
				1 = {
					add_artifact_modifier = artifact_monthly_minor_prestige_3_modifier
					set_artifact_description = double_moon_tome_desc_6
				}
				1 = {
					add_artifact_modifier = artifact_monthly_prestige_3_modifier
					set_artifact_description = double_moon_tome_desc_7
				}
				1 = {
					add_artifact_modifier = artifact_dynasty_opinion_2_modifier
					set_artifact_description = double_moon_tome_desc_8
				}
				1 = {
					add_artifact_modifier = artifact_learn_language_scheme_phase_duration_add_2_modifier
					set_artifact_description = double_moon_tome_desc_9
				}
				1 = {
					add_artifact_modifier = artifact_diplomacy_per_prestige_level_modifier
					set_artifact_description = double_moon_tome_desc_10
				}
				1 = {
					add_artifact_modifier = artifact_mercenary_hire_cost_mult_2_modifier
					set_artifact_description = double_moon_tome_desc_11
				}
				1 = {
					add_artifact_modifier = artifact_general_opinion_1_modifier
					set_artifact_description = double_moon_tome_desc_12
				}
				1 = {
					add_artifact_modifier = artifact_independent_ruler_opinion_2_modifier
					set_artifact_description = double_moon_tome_desc_13
				}
				1 = {
					add_artifact_modifier = artifact_clergy_opinion_2_modifier
					set_artifact_description = double_moon_tome_desc_14
				}
			}
		}
	}
}

random_rain_snow_chance_effect = {
	random_list = {
		10 = {	# Snow
			trigger = {
				location = {
					OR = {
						has_province_modifier = winter_mild_modifier
						has_province_modifier = winter_normal_modifier
						has_province_modifier = winter_harsh_modifier
					}
				}
			}
			save_scope_as = snow_day
		}
		10 = { # Rain
			trigger = {
				location = {
					NOR = {
						has_province_modifier = winter_mild_modifier
						has_province_modifier = winter_normal_modifier
						has_province_modifier = winter_harsh_modifier
					}
				}
			}
			modifier = {
				factor = 1.02
				location.county = { is_riverside_county = yes }
			}
			modifier = {
				factor = 0.05
				location = {
					OR = {
						terrain = desert
						terrain = drylands
						terrain = desert_mountains
						terrain = oasis

					}
				}
			}
			save_scope_as = rain_day
		}
		10 = {} # No rain
	}
}

governor_resignation_title_transfer_effect = {
	save_scope_as = resigner
	top_liege = { save_scope_as = emperor }
	force_step_down_landed_titles = yes
}

boost_governor_efficiency_success_effect = {
	scope:actor = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = boost_efficiency_success_desc
			left_icon = scope:recipient

			scope:recipient ?= {
				send_interface_toast = {
					type = event_toast_effect_good
					title = boost_efficiency_success_desc
					left_icon = scope:actor
					if = {
						limit = { has_character_modifier = ep3_damage_efficiency_modifier }
						remove_character_modifier = ep3_damage_efficiency_modifier
						if = {
							limit = { var:ep3_governor_efficiency_stack = 1 }
							remove_variable = ep3_governor_efficiency_stack
						}
						else = {
							change_variable = {
								name = ep3_governor_efficiency_stack
								add = -1
							}
						}
					}
					else = {
						add_character_modifier = {
							modifier = ep3_boost_efficiency_modifier
							years = 10
						}
						if = {
							limit = { has_variable = ep3_governor_efficiency_stack }
							set_variable = {
								name = ep3_governor_efficiency_stack
								value = {
									value = scope:recipient.var:ep3_governor_efficiency_stack
									add = 1
									max = 5
								}
								years = 10
							}
						}
						else = {
							set_variable = {
								name = ep3_governor_efficiency_stack
								value = 1
								years = 10
							}
						}
					}
					if = {
						limit = {
							opinion = {
								target = scope:actor
								value >= 15
							}
						}
						scope:actor = {
							progress_towards_friend_effect = {
								REASON = friend_boosted_gov_efficiency
								CHARACTER = scope:recipient
								OPINION = default_friend_opinion
							}
						}
					}
					else_if = {
						limit = { is_ai = yes }
						add_opinion = {
							target = scope:actor
							modifier = boosted_efficiency_opinion
						}
					}
				}
			}
			# Merit based realm gain some merit for improving others
			if = {
				limit = {
					government_allows = merit
				}
				change_merit = medium_merit_gain
			}
		}
	}
}

boost_governor_efficiency_duel_effect = {
	scope:actor = {
		duel = {
			skill = $SKILL$
			value = mediocre_skill_rating
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 1.5
					min = -49
				}
				min = 2
				desc = boost_efficiency_success_desc
				send_interface_toast = {
					type = event_toast_effect_good
					title = boost_efficiency_success_desc
					left_icon = scope:recipient
					boost_governor_efficiency_success_effect = yes
				}
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -1.5
					min = -49
				}
				min = 2
				desc = boost_efficiency_failure_desc
				send_interface_toast = {
					type = event_toast_text_bad
					title = boost_efficiency_failure_desc
					left_icon = scope:recipient
					desc = NO_EFFECT
				}
			}
		}
	}
}

damage_governor_efficiency_success_effect = {
	scope:recipient ?= {
		send_interface_toast = {
			type = event_toast_effect_bad
			title = damage_efficiency_success_desc
			left_icon = scope:actor
			if = {
				limit = { has_character_modifier = ep3_boost_efficiency_modifier }
				remove_character_modifier = ep3_boost_efficiency_modifier
				if = {
					limit = { var:ep3_governor_efficiency_stack = 1 }
					remove_variable = ep3_governor_efficiency_stack
				}
				else = {
					change_variable = {
						name = ep3_governor_efficiency_stack
						add = -1
					}
				}
			}
			else = {
				add_character_modifier = {
					modifier = ep3_damage_efficiency_modifier
					years = 10
				}
				if = {
					limit = { has_variable = ep3_governor_efficiency_stack }
					set_variable = {
						name = ep3_governor_efficiency_stack
						value = {
							value = scope:recipient.var:ep3_governor_efficiency_stack
							add = 1
							max = 5
						}
						years = 10
					}
				}
				else = {
					set_variable = {
						name = ep3_governor_efficiency_stack
						value = 1
						years = 10
					}
				}
			}
			if = {
				limit = { is_ai = yes }
				add_opinion = {
					target = scope:actor
					modifier = damaged_efficiency_opinion
				}
			}
		}
	}
}

damage_governor_efficiency_duel_effect = {
	scope:actor = {
		duel = {
			skill = $SKILL$
			value = mediocre_skill_rating
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 1.5
					min = -49
				}
				min = 2
				desc = damage_efficiency_success_desc
				send_interface_toast = {
					type = event_toast_effect_good
					title = damage_efficiency_success_desc
					left_icon = scope:recipient
					damage_governor_efficiency_success_effect = yes
				}
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -1.5
					min = -49
				}
				min = 2
				desc = damage_efficiency_failure_desc
				send_interface_toast = {
					type = event_toast_text_bad
					title = damage_efficiency_success_desc
					left_icon = scope:recipient
					desc = NO_EFFECT
				}
			}
		}
	}
}

# NF Adoption effect
noble_family_adoption_effect = {
	$ADOPTEE$ = {
		if = {
			limit = {
				exists = house.house_head
			}
			house.house_head = {  #We do this first, otherwise house head will already mean someone else.
				if = {
					limit = {
						$ADOPTER$.culture = { has_cultural_parameter = wider_adoption }
						$ADOPTEE$ = {
							culture = { has_cultural_parameter = wider_adoption }
							NOT = {
								any_parent = {
									is_alive = yes
									can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = liege }
								}
							}
						}
					}
					add_opinion = {
						modifier = adopted_my_house_member_opinion
						opinion = 25
						target = $ADOPTER$
					}
				}
				else = {
					add_opinion = {
						modifier = adopted_my_house_member_opinion
						opinion = -25
						target = $ADOPTER$
					}
				}
				if = {
					limit = {
						has_hook_of_type = {
							type = house_head_hook
							target = $ADOPTEE$
						}
					}
					remove_hook = {
						type = house_head_hook
						target = $ADOPTEE$
					}
				}
			}
		}
	}
	$ADOPTER$ = {
		if = {
			limit = { scope:gold = yes }
			pay_short_term_gold = {
				target = $ADOPTEE$
				gold = $ADOPTER$.minor_gold_value
			}
		}
		if = {
			limit = { scope:hook = yes }
			use_hook = $ADOPTEE$
		}
		$ADOPTEE$ = { set_house = $ADOPTER$.house }
		# Zealous vassals *hate* you adopting a heretic
		house = {
			every_house_member = {
				custom = every_zealot_house_member_tt
				limit = {
					ai_zeal >= medium_positive_ai_value
					faith = { faith_hostility_level = { target = $ADOPTEE$.faith value >= faith_astray_level } }
					NOT = { this = $ADOPTER$ }
				}
				add_opinion = {
					modifier = adopted_new_house_member_opinion
					opinion = -20
					target = $ADOPTER$
				}
			}
			every_house_member = {
				custom = every_close_family_house_member_tt
				limit = {
					NOT = { this = $ADOPTER$ }
					is_close_family_of = $ADOPTER$
					NAND = {
						ai_zeal >= medium_positive_ai_value
						faith = { faith_hostility_level = { target = $ADOPTEE$.faith value >= faith_astray_level } }
					}
					NAND = {
						$ADOPTER$.culture = { has_cultural_parameter = wider_adoption }
						$ADOPTEE$ = {
							culture = { has_cultural_parameter = wider_adoption }
							NOT = {
								any_parent = {
									is_alive = yes
									can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = liege }
								}
							}
						}
					}
				}
				add_opinion = {
					modifier = adopted_new_house_member_opinion
					opinion = -10
					target = $ADOPTER$
				}
			}
		}
	}
	$ADOPTEE$ = {
		#adopted should travel
		if = {
			limit = { exists = $ADOPTER$.capital_province }
			$ADOPTER$.capital_province = { save_scope_as = destination_province }
		}
		else_if = {
			limit = { exists = $ADOPTER$.host.capital_province }
			$ADOPTER$.host.capital_province = { save_scope_as = destination_province }
		}
		else_if = {
			limit = { exists = $ADOPTER$.domicile.domicile_location }
			$ADOPTER$.domicile.domicile_location = { save_scope_as = destination_province }
		}
		else = {
			$ADOPTER$.location = { save_scope_as = destination_province }
		}
		save_scope_as = adoptee
		$ADOPTER$ = { save_scope_as = adopter }
		if = {
			limit = {
				any_relation = {
					type = guardian
					NOT = { this = $ADOPTER$ }
				}
			}
			random_relation = {
				type = guardian
				limit = { NOT = { this = $ADOPTER$ } }
				save_scope_as = old_guardian
			}
			remove_guardian_effect = {
				GUARDIAN = scope:old_guardian
				WARD = scope:adoptee
				RETURN_WARD = no
				HIDE_OPINION = no
			}
		}
		if = {
			limit = { NOT = { scope:destination_province = $ADOPTEE$.location } }
			set_variable = {
				name = adoptee_travelling_to_adopter
				years = 2
				value = scope:adopter
			}
			start_travel_plan = {
				destination = scope:destination_province
				on_start_on_action = on_adoptee_depart_for_adopter
				on_travel_planner_cancel_on_action = on_adoptee_depart_travel_planner_exit
				on_arrival_on_action = on_adoptee_arrive_at_adopter
				on_arrival_destinations = last
				return_trip = no # One way
			}
			custom_tooltip = adoptee_leaves_for_adopter_tt
		}
		else_if = {
			limit = {
				$ADOPTER$ = {
					NOT = { is_in_the_same_court_as = $ADOPTEE$ }
				}
			}
			$ADOPTER$ = { add_courtier = $ADOPTEE$ }
		}
		#No final else, in this situation they are already in the right location and the right court.
	}
}

# Create icon artifacts
## Court Artifact - Large
create_artifact_court_icon_large_effect = {
	# Get the character the artifact is being made for.
	$OWNER$ = { save_scope_as = owner }
	$PAINTER$ = { save_scope_as = painter }

	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		# Create the artifact
		scope:owner = {
			if = {
				limit = {
					exists = scope:inspiration_owner
				}
				create_artifact = {
					name = artifact_wall_court_icon_large_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:painter
					visuals = icon_court_large
					type = wall_big
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
				}
			}
			else_if = {
				limit = {
					exists = scope:adventurer
				}
				create_artifact = {
					name = artifact_wall_court_icon_large_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:adventurer
					visuals = icon_court_large
					type = wall_big
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
					history = {
						location = scope:location
						actor = scope:adventurer
						recipient = root
						type = discovered
					}
				}
			}
			else = {
				create_artifact = {
					name = artifact_wall_court_icon_large_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:painter
					visuals = icon_court_large
					type = wall_big
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
				}
			}
		}
		scope:newly_created_artifact = {
			set_artifact_description = artifact_wall_court_icon_large_description
			# Set piety
			add_2_scaled_artifact_modifier_devotion_effect = yes

			if = {
				limit = {
					OR = {
						rarity = famed
						rarity = illustrious
					}
				}
				add_scaled_artifact_modifier_dynasty_prestige_effect = yes
			}
			remove_artifact_modifier = artifact_placeholder_modifier
		}
	}
}

## Court Artifact - Small
create_artifact_court_icon_small_effect = {
	# Get the character the artifact is being made for.
	$OWNER$ = { save_scope_as = owner }
	$PAINTER$ = { save_scope_as = painter }

	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		# Create the artifact
		scope:owner = {
			if = {
				limit = {
					exists = scope:inspiration_owner
				}
				create_artifact = {
					name = artifact_wall_court_icon_small_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:painter
					visuals = icon_court_small
					type = wall_small
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
				}
			}
			else_if = {
				limit = {
					exists = scope:adventurer
				}
				create_artifact = {
					name = artifact_wall_court_icon_small_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:adventurer
					visuals = icon_court_small
					type = wall_small
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
					history = {
						location = scope:location
						actor = scope:adventurer
						recipient = root
						type = discovered
					}
				}
			}
			else = {
				create_artifact = {
					name = artifact_wall_court_icon_small_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:painter
					visuals = icon_court_small
					type = wall_small
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
				}
			}
		}
		scope:newly_created_artifact = {
			set_artifact_description = artifact_wall_court_icon_small_description
			# Set piety
			add_2_scaled_artifact_modifier_devotion_effect = yes

			if = {
				limit = {
					OR = {
						rarity = famed
						rarity = illustrious
					}
				}
				add_scaled_artifact_modifier_dynasty_prestige_effect = yes
			}
			remove_artifact_modifier = artifact_placeholder_modifier
		}
	}
}

## Trinket Artifact
create_artifact_trinket_icon_effect = {
	# Get the character the artifact is being made for.
	$OWNER$ = { save_scope_as = owner }
	$PAINTER$ = { save_scope_as = painter }

	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		# Create the artifact
		scope:owner = {
			if = {
				limit = {
					exists = scope:inspiration_owner
				}
				create_artifact = {
					name = artifact_trinket_icon_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:painter
					visuals = trinket_icon
					type = miscellaneous
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
				}
			}
			else_if = {
				limit = {
					exists = scope:adventurer
				}
				create_artifact = {
					name = artifact_trinket_icon_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:adventurer
					visuals = icon_court_small
					type = miscellaneous
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
					history = {
						location = scope:location
						actor = scope:adventurer
						recipient = root
						type = discovered
					}
				}
			}
			else = {
				create_artifact = {
					name = artifact_trinket_icon_name
					description = placeholder # Will be regenerated in the post-creation effects
					creator = scope:painter
					visuals = trinket_icon
					type = miscellaneous
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = scope:wealth
					quality = scope:quality
				}
			}
		}
		scope:newly_created_artifact = {
			set_artifact_description = artifact_trinket_icon_description
			# Set piety
			add_scaled_artifact_modifier_piety_effect = yes

			if = {
				limit = {
					OR = {
						rarity = famed
						rarity = illustrious
					}
				}
				add_scaled_artifact_modifier_learning_lifestyle_xp_effect = yes
			}
			remove_artifact_modifier = artifact_placeholder_modifier
		}
	}
}

## Trinket Artifact
create_artifact_trinket_icon_ancient_effect = {
	# Get the character the artifact is being made for.
	$OWNER$ = { save_scope_as = owner }

	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		# Create the artifact
		scope:owner = {
			create_artifact = {
				name = artifact_trinket_icon_name
				description = placeholder # Will be regenerated in the post-creation effects
				visuals = trinket_icon
				type = miscellaneous
				modifier = artifact_placeholder_modifier
				save_scope_as = newly_created_artifact
				wealth = scope:wealth
				quality = scope:quality
				history = { type = created_before_history }
			}
		}
		scope:newly_created_artifact = {
			set_artifact_description = artifact_trinket_icon_description
			# Set piety
			add_scaled_artifact_modifier_piety_effect = yes

			if = {
				limit = {
					OR = {
						rarity = famed
						rarity = illustrious
					}
				}
				add_scaled_artifact_modifier_learning_lifestyle_xp_effect = yes
			}
			remove_artifact_modifier = artifact_placeholder_modifier
		}
	}
}
# Scheme bonuses from being the emperor, or member of a dominant/powerful family
ep3_actor_scheme_modifier_effect = {
	scope:new_scheme ?= {
		# Emperor enjoys a significant bonus to all admin related schemes
		if = {
			limit = { scope:actor = scope:actor.top_liege }
			add_scheme_modifier = { type = admin_emperor_scheme_modifier }
		}
		# Dominant and Powerful Families enjoys a decent bonus to all admin related schemes
		else_if = {
			limit = {
				scope:actor.house ?= { is_dominant_family = yes }
			}
			add_scheme_modifier = { type = admin_dominant_family_scheme_modifier }
		}
		else_if = {
			limit = {
				scope:actor.house ?= { is_powerful_family = yes }
			}
			add_scheme_modifier = { type = admin_powerful_family_scheme_modifier }
		}
	}
}

#Complete war group task contracts, used in war_on_actions
war_task_contracts_completion_effect = {
	#Join War Contract and Help Claimant Contract
	scope:war = {
		if = {
			limit = {
				any_war_participant = {
					any_character_active_contract = {
						OR = {
							AND = {
								has_task_contract_type = laamp_join_war_contract
								var:task_contract_war ?= scope:war
							}
							AND = {
								has_task_contract_type = laamp_help_claimant_contract
								task_contract_employer = scope:war.primary_attacker
							}
						}
						save_temporary_scope_as = relevant_contract
					}
				}
			}
			scope:relevant_contract = {
				if = {
					limit = {
						scope:war = {
							war_contribution = {
								target = scope:relevant_contract.task_contract_taker
								value > 0
							}
						}
					}
					complete_task_contract = success_standard
				}
				else = {
					complete_task_contract = failure_standard
				}
			}
		}
		if = {
			limit = {
				primary_attacker = {
					has_variable = valuable_prisoners
				}
			}
			primary_attacker = {
				remove_variable = valuable_prisoners
			}
		}
		if = {
			limit = {
				primary_defender = {
					has_variable = valuable_prisoners
				}
			}
			primary_defender = {
				remove_variable = valuable_prisoners
			}
		}
		if = {
			limit = {
				using_civil_war_cb_trigger = yes
				any_war_participant = {
					any_character_active_contract = {
						task_contract_type = laamp_join_faction_contract
						task_contract_employer = {
							is_participant_in_war = scope:war
						}
					}
				}
			}
			every_war_participant = {
				every_character_active_contract = {
					task_contract_type = laamp_join_faction_contract
					limit = {
						task_contract_employer = {
							is_participant_in_war = scope:war
						}
					}
					if = {
						limit = {
							task_contract_taker = scope:winner
						}
						complete_task_contract = success_standard
					}
					else = {
						complete_task_contract = failure_standard
					}
				}
			}
		}
	}
}

#If a landless adventurer was hired as a mercenary and contributed in a war they get paid, used in war_on_actions, relevant flag set by hire_laamp_mercenaries_interaction or ep3_interactions_events.0112
laamp_as_mercenary_payout_effect = {
	scope:war = {
		if = {
			limit = {
				any_war_participant = {
					has_character_flag = joined_as_mercenary
					is_attacker_in_war = scope:war
					NOT = { is_leader_in_war = scope:war }
					OR = {
						has_raised_armies = yes
						scope:war = {
							war_contribution = {
								target = prev
								value > 0
							}
						}
					}
				}
			}
			every_war_participant = {
				limit = {
					has_character_flag = joined_as_mercenary
					is_attacker_in_war = scope:war
					NOT = { is_leader_in_war = scope:war }
					OR = {
						has_raised_armies = yes
						scope:war = {
							war_contribution = {
								target = prev
								value > 0
							}
						}
					}
				}
				add_to_list = laamp_mercenaries
				remove_character_flag = joined_as_mercenary
			}
			primary_attacker = {
				save_scope_as = employer
				trigger_event = {
					id = ep3_interactions_events.0121
					days = { 3 7 }
				}
			}
			every_in_list = {
				list = laamp_mercenaries
				scope:employer = {
					remove_list_variable = {
					 	name = hired_mercenaries
					 	target = prev
					}
				}
			}
		}
		if = {
			limit = {
				any_war_participant = {
					has_character_flag = joined_as_mercenary
					is_defender_in_war = scope:war
					NOT = { is_leader_in_war = scope:war }
					OR = {
						has_raised_armies = yes
						scope:war = {
							war_contribution = {
								target = prev
								value > 0
							}
						}
					}
				}
			}
			every_war_participant = {
				limit = {
					has_character_flag = joined_as_mercenary
					is_defender_in_war = scope:war
					NOT = { is_leader_in_war = scope:war }
					OR = {
						has_raised_armies = yes
						scope:war = {
							war_contribution = {
								target = prev
								value > 0
							}
						}
					}
				}
				add_to_list = laamp_mercenaries
				remove_character_flag = joined_as_mercenary
			}
			primary_defender = {
				save_scope_as = employer
				trigger_event = {
					id = ep3_interactions_events.0121
					days = { 3 7 }
				}
			}
			every_in_list = {
				list = laamp_mercenaries
				scope:employer = {
					remove_list_variable = {
					 	name = hired_mercenaries
					 	target = prev
					}
				}
			}
		}
	}
}

# root - casus_belli
# war - war provided by the casus_belli
# attacker - primary attacker from the casus_belli
# defender - primary defender from the casus_belli
laamp_as_mercenary_payout_tooltip_effect = {
	show_as_tooltip = {
		scope:defender = {
			if = {
				limit = {
					exists = scope:war
					any_war_ally = {
						has_government = landless_adventurer_government
						OR = {
							any_character_active_contract = {
								has_task_contract_type = laamp_join_war_contract
								var:task_contract_war ?= scope:war
							}
							AND = {
								has_variable = owed_contract_assistance_gold
								var:owed_contract_assistance_war ?= scope:war
							}
							has_variable = joined_as_mercenary
						}
					}
				}
				every_war_ally = {
					if = {
						limit = {
							has_government = landless_adventurer_government
						}
						save_scope_as = laamp_temp
						save_scope_as = actor
						save_scope_value_as = {
							name = war_contribution_value
							value = "scope:war.war_contribution(scope:laamp_temp)"
						}
						if = {
							limit = {
								any_character_active_contract = {
									has_task_contract_type = laamp_join_war_contract
									var:task_contract_war = scope:war
								}
							}
							every_character_active_contract = {
								limit = {
									has_task_contract_type = laamp_join_war_contract
									var:task_contract_war = scope:war
								}
								show_as_tooltip = {
									task_contract_employer = {
										save_scope_as = employer
										custom_tooltip = ep3_join_war_payment_tt
										pay_short_term_gold = {
											target = scope:laamp_temp
											gold = ep3_hire_laamp_mercs_payment_value
										}
									}
								}
							}
						}
						if = {
							limit = {
								has_variable = joined_as_mercenary
								scope:defender = {
									is_target_in_variable_list = {
										name = hired_mercenaries
										target = scope:laamp_temp
									}
								}
							}
							scope:defender = {
								save_scope_as = employer
								custom_tooltip = ep3_hire_laamp_mercs_payment_tt
								pay_short_term_gold = {
									target = scope:laamp_temp
									gold = ep3_hire_laamp_mercs_payment_value
								}
							}
						}
						if = {
							limit = {
								has_variable = owed_contract_assistance_gold
								var:owed_contract_assistance_war ?= scope:war
							}
							save_scope_as = contract_assistance_helper
							scope:defender = {
								save_scope_as = recipient
								custom_tooltip = contract_assistance_interaction_condition_tt
								pay_short_term_gold = {
									target = scope:actor
									gold = scope:actor.var:owed_contract_assistance_gold
								}
							}
						}
					}
				}
			}
		}
		scope:attacker = {
			if = {
				limit = {
					exists = scope:war
					any_war_ally = {
						has_government = landless_adventurer_government
						OR = {
							any_character_active_contract = {
								has_task_contract_type = laamp_join_war_contract
								var:task_contract_war = scope:war
							}
							AND = {
								has_variable = owed_contract_assistance_gold
								var:owed_contract_assistance_war ?= scope:war
							}
							has_variable = joined_as_mercenary
						}
					}
				}
				every_war_ally = {
					if = {
						limit = {
							has_government = landless_adventurer_government
						}
						save_scope_as = laamp_temp
						save_scope_as = actor
						save_scope_value_as = {
							name = war_contribution_value
							value = "scope:war.war_contribution(scope:laamp_temp)"
						}
						if = {
							limit = {
								any_character_active_contract = {
									has_task_contract_type = laamp_join_war_contract
									var:task_contract_war = scope:war
								}
							}
							every_character_active_contract = {
								limit = {
									has_task_contract_type = laamp_join_war_contract
									var:task_contract_war = scope:war
								}
								show_as_tooltip = {
									task_contract_employer = {
										save_scope_as = employer
										custom_tooltip = ep3_hire_laamp_mercs_payment_tt
										pay_short_term_gold = {
											target = scope:laamp_temp
											gold = ep3_hire_laamp_mercs_payment_value
										}
									}
								}
							}
						}
						if = {
							limit = {
								has_variable = joined_as_mercenary
								scope:attacker = {
									is_target_in_variable_list = {
										name = hired_mercenaries
										target = scope:laamp_temp
									}
								}
							}
							scope:attacker = {
								save_scope_as = employer
								custom_tooltip = ep3_hire_laamp_mercs_payment_tt
								pay_short_term_gold = {
									target = scope:laamp_temp
									gold = ep3_hire_laamp_mercs_payment_value
								}
							}
						}
						if = {
							limit = {
								has_variable = owed_contract_assistance_gold
								var:owed_contract_assistance_war ?= scope:war
							}
							save_scope_as = contract_assistance_helper
							scope:attacker = {
								custom_tooltip = contract_assistance_interaction_condition_tt
								pay_short_term_gold = {
									target = scope:actor
									gold = scope:actor.var:owed_contract_assistance_gold
								}
							}
						}
					}
				}
			}
		}
	}
}

chariot_race_sow_discord_strong_response_effect = {
	if = {
		limit = {
			root = {
				this = scope:host
			}
		}
		scope:host = {
			capital_county = {
				change_county_control = {
					value = -3
					multiply = scope:activity.var:counter_chant_count
				}
			}
			custom_tooltip = counter_chants_strong_modifier_tooltip
			hidden_effect = {
				scope:activity = {
					every_in_list = {
						variable = counter_chanters
						scope:host = {
							capital_county = {
								add_county_modifier = {
									modifier = strong_sow_discord_modifier
									years = 10
								}
							}
						}
					}
				}
			}
		}
	}
	else = {
		scope:host = {
			capital_county = {
				add_county_modifier = {
					modifier = strong_sow_discord_modifier
					years = 10
				}
				change_county_control = -3
			}
		}
	}
}
chariot_race_sow_discord_weak_response_effect = {
	if = {
		limit = {
			root = {
				this = scope:host
			}
		}
		scope:host = {
			custom_tooltip = counter_chants_weak_modifier_tooltip
			hidden_effect = {
				scope:activity = {
					every_in_list = {
						variable = counter_chanters
						scope:host = {
							capital_county = {
								add_county_modifier = {
									modifier = weak_sow_discord_modifier
									years = 10
								}
							}
						}
					}
				}
			}
		}
	}
	else = {
		scope:host = {
			capital_county = {
				add_county_modifier = {
					modifier = weak_sow_discord_modifier
					years = 10
				}
			}
		}
	}
}
chariot_race_sow_discord_normal_response_effect = {
	if = {
		limit = {
			root = {
				this = scope:host
			}
		}
		scope:host = {
			capital_county = {
				change_county_control = {
					value = -1
					multiply = scope:activity.var:counter_chant_count
				}
			}
			custom_tooltip = counter_chants_normal_modifier_tooltip
			hidden_effect = {
				scope:activity = {
					every_in_list = {
						variable = counter_chanters
						scope:host = {
							capital_county = {
								add_county_modifier = {
									modifier = normal_sow_discord_modifier
									years = 10
								}
							}
						}
					}
				}
			}
		}
	}
	else = {
		scope:host = {
			capital_county = {
				add_county_modifier = {
					modifier = normal_sow_discord_modifier
					years = 10
				}
				custom_tooltip = modifier_stacking_chariot_tooltip
				change_county_control = -1
			}
		}
	}
}

#############
# Camp Party
#############

camp_party_completed_log_entry_effect = {
	involved_activity = {
		add_activity_log_entry = {
			key = camp_party_completed_log_entry_effect
			tags = { completed }
			score = 100
			show_in_conclusion = yes
			character = root
			#Effects
			root = {
				disburse_camp_party_reward_effect = yes
			}
		}
	}
}

disburse_camp_party_reward_effect = {
	involved_activity ?= {
		###Non-progression linked
		#Drinks
		scope:host = {
			#For highest level drinks
			if = {
				limit = {
					involved_activity = {
						has_activity_option = {
							category = camp_party_option_drinks
							option = camp_party_drinks_abundant
						}
					}
				}
				add_character_modifier = {
					modifier = ep3_master_of_ceremonies
					years = 3
				}
			}

			#For any level of drinks
			#Opinion
			if = {
				limit = {
					involved_activity = {
						OR = {
							has_activity_option = {
								category = camp_party_option_drinks
								option = camp_party_drinks_abundant
							}
							has_activity_option = {
								category = camp_party_option_drinks
								option = camp_party_drinks_adequate
							}
						}
					}
				}
				involved_activity = {
					every_attending_character = {
						limit = {
							this != scope:host
						}
						custom = every_attending_guest
						add_opinion = {
							modifier = feast_life_of_the_party_opinion
							target = scope:host
							opinion = 35
						}
					}
				}
			}

			# Reveler Trait
			if = {
				limit = {
					involved_activity = {
						OR = {
							has_activity_option = {
								category = camp_party_option_drinks
								option = camp_party_drinks_abundant
							}
							has_activity_option = {
								category = camp_party_option_drinks
								option = camp_party_drinks_adequate
							}
						}
					}
				}
				if = {
					limit = {
						NOT = {
							has_trait = lifestyle_reveler
						}
					}
					add_trait = lifestyle_reveler
				}
				add_trait_xp = {
					trait = lifestyle_reveler
					value = {
						integer_range = {
							min = small_lifestyle_random_xp_low
							max = small_lifestyle_random_xp_high
						}
					}
				}
			}
		}

		#100 Carousing
		if = {
			limit = { var:activity_special_type_progression >= 100 }

			#Stress Loss
			every_attending_character = {
				custom = every_attending_guest
				add_stress = -150
			}

			#Host effects
			scope:host = {
				hidden_effect = {
					domicile = {
						random_list = {
							1 = {
								trigger = {
									has_domicile_building_or_higher = camp_main_01
									NOT = { has_domicile_building = camp_main_04 }
								}
								switch = {
									trigger = has_domicile_building
									camp_main_01 = { add_domicile_building = camp_main_02 }
									camp_main_02 = { add_domicile_building = camp_main_03 }
									camp_main_03 = { add_domicile_building = camp_main_04 }
								}
							}
							1 = {
								trigger = {
									has_domicile_building_or_higher = supply_tent_01
									NOT = { has_domicile_building = supply_tent_06 }
								}
								switch = {
									trigger = has_domicile_building
									supply_tent_01 = { add_domicile_building = supply_tent_02 }
									supply_tent_02 = { add_domicile_building = supply_tent_03 }
									supply_tent_03 = { add_domicile_building = supply_tent_04 }
									supply_tent_04 = { add_domicile_building = supply_tent_05 }
									supply_tent_05 = { add_domicile_building = supply_tent_06 }
								}
							}
							1 = {
								trigger = {
									has_domicile_building_or_higher = barber_tent_01
									NOT = { has_domicile_building = barber_tent_06 }
								}
								switch = {
									trigger = has_domicile_building
									barber_tent_01 = { add_domicile_building = barber_tent_02 }
									barber_tent_02 = { add_domicile_building = barber_tent_03 }
									barber_tent_03 = { add_domicile_building = barber_tent_04 }
									barber_tent_04 = { add_domicile_building = barber_tent_05 }
									barber_tent_05 = { add_domicile_building = barber_tent_06 }
								}
							}
							1 = {
								trigger = {
									has_domicile_building_or_higher = baggage_train_01
									NOT = { has_domicile_building = baggage_train_06 }
								}
								switch = {
									trigger = has_domicile_building
									baggage_train_01 = { add_domicile_building = baggage_train_02 }
									baggage_train_02 = { add_domicile_building = baggage_train_03 }
									baggage_train_03 = { add_domicile_building = baggage_train_04 }
									baggage_train_04 = { add_domicile_building = baggage_train_05 }
									baggage_train_05 = { add_domicile_building = baggage_train_06 }
								}
							}
							1 = {
								trigger = {
									has_domicile_building_or_higher = camp_fire_01
									NOT = { has_domicile_building = camp_fire_06 }
								}
								switch = {
									trigger = has_domicile_building
									camp_fire_01 = { add_domicile_building = camp_fire_02 }
									camp_fire_02 = { add_domicile_building = camp_fire_03 }
									camp_fire_03 = { add_domicile_building = camp_fire_04 }
									camp_fire_04 = { add_domicile_building = camp_fire_05 }
									camp_fire_05 = { add_domicile_building = camp_fire_06 }
								}
							}
							1 = {
								trigger = {
									has_domicile_building_or_higher = proving_grounds_01
									NOT = { has_domicile_building = proving_grounds_06 }
								}
								switch = {
									trigger = has_domicile_building
									proving_grounds_01 = { add_domicile_building = proving_grounds_02 }
									proving_grounds_02 = { add_domicile_building = proving_grounds_03 }
									proving_grounds_03 = { add_domicile_building = proving_grounds_04 }
									proving_grounds_04 = { add_domicile_building = proving_grounds_05 }
									proving_grounds_05 = { add_domicile_building = proving_grounds_06 }
								}
							}
							1 = {
								trigger = {
									has_domicile_building_or_higher = camp_perimeter_01
									NOT = { has_domicile_building = camp_perimeter_06 }
								}
								switch = {
									trigger = has_domicile_building
									camp_perimeter_01 = { add_domicile_building = camp_perimeter_02 }
									camp_perimeter_02 = { add_domicile_building = camp_perimeter_03 }
									camp_perimeter_03 = { add_domicile_building = camp_perimeter_04 }
									camp_perimeter_04 = { add_domicile_building = camp_perimeter_05 }
									camp_perimeter_05 = { add_domicile_building = camp_perimeter_06 }
								}
							}
						}
					}
				}

				custom_tooltip = activity_camp_party_reward_upgrade_building

				custom_tooltip = camp_party_100_contracts_get

				hidden_effect = {
					contract_passive_spawn_effect = {
						SPAWN_CONTRACTS = 2
					}
				}

				#Provisions
				domicile = {
					change_provisions = massive_provisions_gain
				}

				#Lifestyle XP
				bp2_lifestyle_xp_gain_per_type_effect = { VALUE = major }

				add_character_flag = {
					flag = better_recruitment_visit_holding_flag
					years = 1
				}
			}
		}

		#75+ Carousing
		else_if = {
			limit = {
				var:activity_special_type_progression < 100
				var:activity_special_type_progression >= 75
			}

			#Stress Loss
			every_attending_character = {
				custom = every_attending_guest
				add_stress = -100
			}

			#Contract spawn & flag assign
			scope:host = {
				custom_tooltip = camp_party_75_contracts_get

				hidden_effect = {
					contract_passive_spawn_effect = {
						SPAWN_CONTRACTS = 1
					}
				}

				#Provisions
				domicile = {
					change_provisions = major_provisions_gain
				}

				#Lifestyle XP
				bp2_lifestyle_xp_gain_per_type_effect = { VALUE = medium }

				add_character_flag = {
					flag = better_recruitment_visit_holding_flag
					years = 1
				}
			}
		}
		#50+ Carousing
		else_if = {
			limit = {
				var:activity_special_type_progression < 75
				var:activity_special_type_progression >= 50
			}

			#Stress Loss
			every_attending_character = {
				custom = every_attending_guest
				add_stress = -75
			}

			#Contract spawn and flag assign
			scope:host = {
				#Provisions
				domicile = {
					change_provisions = medium_provisions_gain
				}

				#Lifestyle XP
				bp2_lifestyle_xp_gain_per_type_effect = { VALUE = minor }

				add_character_flag = {
					flag = better_recruitment_visit_holding_flag
					years = 1
				}
			}
		}
		#25+ Carousing
		else = {
			#Stress Loss
			every_attending_character = {
				custom = every_attending_guest
				add_stress = -50
			}

			scope:host = {
				add_character_flag = {
					flag = cheaper_provisions_visit_holding_flag
					years = 1
				}
			}
		}

		scope:host = {
			if = {
				limit = {
					has_activity_intent = fireside_chat_intent
				}
				hidden_effect = {
					remove_character_modifier = ep3_fireside_chat_modifier_3
					remove_character_modifier = ep3_fireside_chat_modifier_2
					remove_character_modifier = ep3_fireside_chat_modifier_1
				}
				if = {
					limit = {
						involved_activity = {
							any_attending_character = {
								count >= 4
								OR = {
									has_character_modifier = ep3_fireside_chat_diplomacy
									has_character_modifier = ep3_fireside_chat_learning
									has_character_modifier = ep3_fireside_chat_intrigue
									has_character_modifier = ep3_fireside_chat_stewardship
									has_character_modifier = ep3_fireside_chat_martial
								}
							}
						}
					}
					add_character_modifier = {
						modifier = ep3_fireside_chat_modifier_3
						years = 10
					}
				}
				else_if = {
					limit = {
						involved_activity = {
							any_attending_character = {
								count >= 2
								OR = {
									has_character_modifier = ep3_fireside_chat_diplomacy
									has_character_modifier = ep3_fireside_chat_learning
									has_character_modifier = ep3_fireside_chat_intrigue
									has_character_modifier = ep3_fireside_chat_stewardship
									has_character_modifier = ep3_fireside_chat_martial
								}
							}
						}
					}
					add_character_modifier = {
						modifier = ep3_fireside_chat_modifier_2
						years = 10
					}
				}
				else = {
					add_character_modifier = {
						modifier = ep3_fireside_chat_modifier_1
						years = 10
					}
				}
			}
		}
	}
}

ep3_camp_party_visit_holding_skills_boost = {
	add_diplomacy_skill = { 1 3 }
	add_martial_skill = { 1 3 }
	add_stewardship_skill = { 1 3 }
	add_intrigue_skill = { 1 3 }
	add_learning_skill = { 1 3 }
}

#Setting up the Fens as a pseudo-custom region
ep3_the_fens_list_effect = {
	province:1525 = { add_to_list = the_fens } #b_lynn
	province:1534 = { add_to_list = the_fens } #b_cambridgeshire
	province:1535 = { add_to_list = the_fens } #b_radfield
	province:1536 = { add_to_list = the_fens } #b_papworth
	province:1537 = { add_to_list = the_fens } #b_ely
	province:1541 = { add_to_list = the_fens } #b_hurstingstone
	province:1542 = { add_to_list = the_fens } #b_norman_cross
	province:1584 = { add_to_list = the_fens } #b_stamford
	province:1585 = { add_to_list = the_fens } #b_boston
	province:1601 = { add_to_list = the_fens } #b_peterborough
}

#Used for setting up the Harrying of the North widget for events
#Requires use of ep3_harrying_widget_clear_effect in the 'after' as well
ep3_harrying_widget_setup_effect = {
	global_var:harrying_of_the_north = { save_scope_as = story }
	## First, identify our widget character.
	save_scope_as = scope_character
	## Then transfer over some suitable variables for the UI.
	set_variable = {
		name = pacification_gui
		value = scope:story.var:pacification
	}
	set_variable = {
		name = resistance_gui
		value = scope:story.var:resistance
	}
}

#Increase pacification
ep3_increase_pacification_effect = {
	save_scope_as = origin_char
	scope:story = {
		change_variable = {
			name = pacification
			add = $AMOUNT$
		}
	}
	set_variable = {
		name = pacification_gui
		value = scope:story.var:pacification
	}
	if = { #Display in red if Anglo-Saxon
		limit = {
			culture = culture:anglo_saxon
		}
		custom_tooltip = ep3_increase_pacification_effect_tt_negative
	}
	else = { #Otherwise in green
		custom_tooltip = ep3_increase_pacification_effect_tt_positive
	}

	if = {
		limit = {
			exists = scope:story.var:ruler_england
		}
		scope:story.var:ruler_england = {
			send_interface_message = {
				type = msg_pacification_rise
				title = harrying.notification_pacification_rise
				desc = harrying_pacification_rise
			}
		}
	}

	if = {
		limit = {
			character:90028 = {
				is_alive = yes
				NOT = {
					has_character_flag = hereward_settled_flag
				}
			}
		}
		character:90028 = {
			send_interface_message = {
				type = msg_pacification_rise
				title = harrying.notification_pacification_rise
				desc = harrying_pacification_rise
			}
		}
	}

	if = {
		limit = {
			scope:story.var:ruler_england = {
				any_vassal = {
					culture = culture:anglo_saxon
				}
			}
		}
		scope:story.var:ruler_england = {
			every_vassal = {
				limit = {
					culture = culture:anglo_saxon
				}
				send_interface_message = {
					type = msg_pacification_rise
					title = harrying.notification_pacification_rise
					desc = harrying_pacification_rise
				}
			}
		}
	}
}

#Increase pacification - large
ep3_increase_pacification_large_effect = {
	save_scope_as = origin_char
	scope:story = {
		change_variable = {
			name = pacification
			add = 2
		}
	}
	set_variable = {
		name = pacification_gui
		value = scope:story.var:pacification
	}
	if = { #Display in red if Anglo-Saxon
		limit = {
			culture = culture:anglo_saxon
		}
		custom_tooltip = ep3_increase_pacification_effect_tt_negative_large
	}
	else = { #Otherwise in green
		custom_tooltip = ep3_increase_pacification_effect_tt_positive_large
	}

	if = {
		limit = {
			exists = scope:story.var:ruler_england
		}
		scope:story.var:ruler_england = {
			send_interface_message = {
				type = msg_pacification_rise
				title = harrying.notification_pacification_rise
				desc = harrying_pacification_rise
			}
		}
	}

	if = {
		limit = {
			character:90028 = {
				is_alive = yes
				NOT = {
					has_character_flag = hereward_settled_flag
				}
			}
		}
		character:90028 = {
			send_interface_message = {
				type = msg_pacification_rise
				title = harrying.notification_pacification_rise
				desc = harrying_pacification_rise
			}
		}
	}

	if = {
		limit = {
			scope:story.var:ruler_england = {
				any_vassal = {
					culture = culture:anglo_saxon
				}
			}
		}
		scope:story.var:ruler_england = {
			every_vassal = {
				limit = {
					culture = culture:anglo_saxon
				}
				send_interface_message = {
					type = msg_pacification_rise
					title = harrying.notification_pacification_rise
					desc = harrying_pacification_rise
				}
			}
		}
	}
}

#Increase resistance
ep3_increase_resistance_effect = {
	save_scope_as = origin_char
	scope:story = {
		change_variable = {
			name = resistance
			add = $AMOUNT$
		}
	}
	set_variable = {
		name = resistance_gui
		value = scope:story.var:resistance
	}
	if = { #Display in green if Anglo-Saxon
		limit = {
			culture = culture:anglo_saxon
		}
		custom_tooltip = ep3_increase_resistance_effect_tt_positive
	}
	else = { #Otherwise in green
		custom_tooltip = ep3_increase_resistance_effect_tt_negative
	}

	if = {
		limit = {
			exists = scope:story.var:ruler_england
		}
		scope:story.var:ruler_england = {
			send_interface_message = {
				type = msg_resistance_rise
				title = harrying.notification_resistance_rise
				desc = harrying_resistance_rise
			}
		}
	}

	if = {
		limit = {
			character:90028 = {
				is_alive = yes
				NOT = {
					has_character_flag = hereward_settled_flag
				}
			}
		}
		character:90028 = {
			send_interface_message = {
				type = msg_resistance_rise
				title = harrying.notification_resistance_rise
				desc = harrying_resistance_rise
			}
		}
	}

	if = {
		limit = {
			scope:story.var:ruler_england = {
				any_vassal = {
					culture = culture:anglo_saxon
				}
			}
		}
		scope:story.var:ruler_england = {
			every_vassal = {
				limit = {
					culture = culture:anglo_saxon
				}
				send_interface_message = {
					type = msg_resistance_rise
					title = harrying.notification_resistance_rise
					desc = harrying_resistance_rise
				}
			}
		}
	}
}

#Increase resistance - large
ep3_increase_resistance_large_effect = {
	save_scope_as = origin_char
	scope:story = {
		change_variable = {
			name = resistance
			add = 2
		}
	}
	set_variable = {
		name = resistance_gui
		value = scope:story.var:resistance
	}
	if = { #Display in green if Anglo-Saxon
		limit = {
			culture = culture:anglo_saxon
		}
		custom_tooltip = ep3_increase_resistance_effect_tt_positive_large
	}
	else = { #Otherwise in green
		custom_tooltip = ep3_increase_resistance_effect_tt_negative_large
	}

	if = {
		limit = {
			exists = scope:story.var:ruler_england
		}
		scope:story.var:ruler_england = {
			send_interface_message = {
				type = msg_resistance_rise
				title = harrying.notification_resistance_rise
				desc = harrying_resistance_rise
			}
		}
	}

	if = {
		limit = {
			character:90028 = {
				is_alive = yes
				NOT = {
					has_character_flag = hereward_settled_flag
				}
			}
		}
		character:90028 = {
			send_interface_message = {
				type = msg_resistance_rise
				title = harrying.notification_resistance_rise
				desc = harrying_resistance_rise
			}
		}
	}

	if = {
		limit = {
			scope:story.var:ruler_england = {
				any_vassal = {
					culture = culture:anglo_saxon
				}
			}
		}
		scope:story.var:ruler_england = {
			random_vassal = {
				limit = {
					culture = culture:anglo_saxon
				}
				send_interface_message = {
					type = msg_resistance_rise
					title = harrying.notification_resistance_rise
					desc = harrying_resistance_rise
				}
			}
		}
	}
}

#Decrease pacification
ep3_decrease_pacification_effect = {
	save_scope_as = origin_char
	if = {
		limit = { scope:story.var:pacification != 0 }
		scope:story = {
			change_variable = {
				name = pacification
				add = -1
			}
		}
		set_variable = {
			name = pacification_gui
			value = scope:story.var:pacification
		}
		if = { #Display in green if Anglo-Saxon
			limit = {
				culture = culture:anglo_saxon
			}
			custom_tooltip = ep3_decrease_pacification_effect_tt_positive
		}
		else = { #Otherwise in green
			custom_tooltip = ep3_decrease_pacification_effect_tt_negative
		}

		if = {
			limit = {
				exists = scope:story.var:ruler_england
			}
			scope:story.var:ruler_england = {
				send_interface_message = {
					type = msg_pacification_falls
					title = harrying.notification_pacification_fall
					desc = harrying_pacification_fall
				}
			}
		}

		if = {
			limit = {
				character:90028 = {
					is_alive = yes
					NOT = {
						has_character_flag = hereward_settled_flag
					}
				}
			}
			character:90028 = {
				send_interface_message = {
					type = msg_pacification_falls
					title = harrying.notification_pacification_fall
					desc = harrying_pacification_fall
				}
			}
		}

		if = {
			limit = {
				scope:story.var:ruler_england = {
					any_vassal = {
						culture = culture:anglo_saxon
					}
				}
			}
			scope:story.var:ruler_england = {
				every_vassal = {
					limit = {
						culture = culture:anglo_saxon
					}
					send_interface_message = {
						type = msg_pacification_falls
						title = harrying.notification_pacification_fall
						desc = harrying_pacification_fall
					}
				}
			}
		}
	}
}

#Decrease resistance
ep3_decrease_resistance_effect = {
	save_scope_as = origin_char
	if = {
		limit = { scope:story.var:pacification != 0 }
		scope:story = {
			change_variable = {
				name = resistance
				add = -1
			}
		}
		set_variable = {
			name = resistance_gui
			value = scope:story.var:resistance
		}
		if = { #Display in red if Anglo-Saxon
			limit = {
				culture = culture:anglo_saxon
			}
			custom_tooltip = ep3_decrease_resistance_effect_tt_negative
		}
		else = { #Otherwise in green
			custom_tooltip = ep3_decrease_resistance_effect_tt_positive
		}

		if = {
			limit = {
				exists = scope:story.var:ruler_england
			}
			scope:story.var:ruler_england = {
				send_interface_message = {
					type = msg_resistance_falls
					title = harrying.notification_resistance_fall
					desc = harrying_resistance_fall
				}
			}
		}

		if = {
			limit = {
				character:90028 = {
					is_alive = yes
					NOT = {
						has_character_flag = hereward_settled_flag
					}
				}
			}
			character:90028 = {
				send_interface_message = {
					type = msg_resistance_falls
					title = harrying.notification_resistance_fall
					desc = harrying_resistance_fall
				}
			}
		}

		if = {
			limit = {
				scope:story.var:ruler_england = {
					any_vassal = {
						culture = culture:anglo_saxon
					}
				}
			}
			scope:story.var:ruler_england = {
				every_vassal = {
					limit = {
						culture = culture:anglo_saxon
					}
					send_interface_message = {
						type = msg_resistance_falls
						title = harrying.notification_resistance_fall
						desc = harrying_resistance_fall
					}
				}
			}
		}
	}
}

#Used for clearing widget data from the participants
ep3_harrying_widget_clear_effect = {
	remove_variable = pacification_gui
	remove_variable = resistance_gui
}

# Effect for handling players and AI continuing as landless if deposed
# Should be used over the standard effect in most cases (except barons/unlanded AI etc.)
# DEPOSER should be whoever has caused the deposal - faction leader, ultimatum giver, etc.
depose_effect = {
	show_as_tooltip = { depose = yes }
	save_scope_as = allow_imprisoned_landless
	if = {
		limit = { is_valid_for_laampdom = yes }
		custom_tooltip = laamp_may_become_adventurer_tt
	}
	set_variable = {
		name = deposed_primary_title
		value = primary_title
		months = 1
	}
	# Maintenance scopes
	save_scope_as = defender
	$DEPOSER$ = {
		save_scope_as = attacker
		save_scope_as = winner
	}
	# ACH crown laws reset
	if = {
		limit = {
			has_variable = crowned_king_var
		}
		remove_variable = crowned_king_var
		if = {
			limit = {
				NOT = { has_game_rule = coronation_laws_off }
				OR = {
					has_realm_law = crowned_king
					has_realm_law = crowned_emperor
				}
			}
			add_realm_law_skip_effects = uncrowned
		}
	}
	if = {
		limit = {
			has_variable = crowned_emperor_var
		}
		remove_variable = crowned_emperor_var
		if = {
			limit = {
				NOT = { has_game_rule = coronation_laws_off }
				OR = {
					has_realm_law = crowned_king
					has_realm_law = crowned_emperor
				}
			}
			add_realm_law_skip_effects = uncrowned
		}
	}
	every_held_title = { add_to_list = target_titles }
	trigger_event = ep3_laamps.0002
}

# Effect for handling players and AI continuing as landless if banished
# Should be used over the standard effect in most cases (except barons/unlanded AI etc.)
# BANISHER should be whoever has caused the banishment - imprisoner, ultimatum giver, etc.
banish_effect = {
	show_as_tooltip = { banish = yes }
	save_scope_as = allow_imprisoned_landless
	if = {
		limit = { is_valid_for_laampdom = yes }
		custom_tooltip = laamp_may_become_adventurer_tt
	}
	$BANISHER$ = { save_scope_as = banisher }
	trigger_event = ep3_laamps.0021
}

#Used for setting up the Peasant Leader widget for events
#Requires use of ep3_peasant_leader_widget_clear_effect in the 'after' as well
ep3_peasant_leader_widget_setup_effect = {
	set_variable = {
		name = support_gui
		value = scope:task_contract.var:support
	}
	set_variable = {
		name = control_gui
		value = scope:task_contract.var:control
	}
	set_variable = {
		name = num_of_corrupt_counties_gui
		value = scope:task_contract.var:num_of_corrupt_counties
	}
}
#Increase support
ep3_increase_peasant_support_effect = {
	scope:task_contract = {
		change_variable = {
			name = support
			add = 1
		}
		add_to_variable_list = {
			name = supported_counties
			target = scope:corrupt_province.county
		}
	}
	custom_tooltip = ep3_support_peasants_effect_tt
	scope:task_contract.var:task_contract_councillor_liege ?= {
		send_interface_message = {
			type = msg_peasant_supported
			title = msg_peasant_supported.title
			desc = msg_peasant_supported.desc
		}
	}
	scope:task_contract.var:task_contract_councillor ?= {
		send_interface_message = {
			type = msg_peasant_supported
			title = msg_peasant_supported.title
			desc = msg_peasant_supported.desc
		}
	}
}
#Increase control
ep3_increase_peasant_control_effect = {
	scope:task_contract = {
		change_variable = {
			name = control
			add = 1
		}
	}
	custom_tooltip = ep3_control_peasants_effect_tt
	scope:task_contract.var:task_contract_councillor_liege ?= {
		send_interface_message = {
			type = msg_peasant_controlled
			title = msg_peasant_controlled.title
			desc = msg_peasant_controlled.desc
		}
	}
	scope:task_contract.var:task_contract_councillor ?= {
		send_interface_message = {
			type = msg_peasant_controlled
			title = msg_peasant_controlled.title
			desc = msg_peasant_controlled.desc
		}
	}
}
#Used for clearing widget data from the participants
ep3_peasant_leader_widget_clear_effect = {
	remove_variable = support_gui
	remove_variable = control_gui
	remove_variable = num_of_corrupt_counties_gui
}

ep3_join_faction_war_effect = {
	# Use hook/pay gold
	scope:actor = {
		if = {
			limit = {
				scope:hook = yes
				has_hook = scope:recipient
			}
			use_hook = scope:recipient
		}
		if = {
			limit = { scope:gold = yes }
			pay_short_term_gold = {
				gold = scope:actor.medium_gold_value
				target = scope:recipient
			}
		}
	}
	# Gain some influence
	scope:recipient = {
		change_influence = 75
		# Remove from existing faction if swapping
		if = {
			limit = { exists = joined_faction }
			leave_faction = joined_faction
		}
		if = {
			limit = { target_is_liege_or_above = scope:secondary_recipient }
			add_faction_cooldown_effect = { YEARS = 5 }
		}
		remove_character_flag = under_offer_join_faction_war_flag
	}
	scope:actor.joined_faction.faction_war = {
		if = {
			limit = { is_defender = scope:actor }
			add_defender = scope:recipient
		}
		else = { add_attacker = scope:recipient }
	}
}

ep3_switch_faction_war_sides_effect = {
	scope:actor = {
		if = {
			limit = {
				scope:hook = yes
				has_hook = scope:recipient
			}
			use_hook = scope:recipient
		}
		if = {
			limit = { scope:gold = yes }
			pay_short_term_gold = {
				gold = scope:actor.medium_gold_value
				target = scope:recipient
			}
		}
	}
	scope:recipient = {
		change_influence = 150
		# Remove from existing faction if swapping
		if = {
			limit = { exists = joined_faction }
			leave_faction = joined_faction
		}
		if = {
			limit = { target_is_liege_or_above = scope:secondary_recipient }
			add_faction_cooldown_effect = { YEARS = 5 }
		}
	}
	scope:actor = {
		every_character_war = {
			limit = { ep3_switch_faction_war_sides_war_trigger = yes }
			hidden_effect = { remove_participant = scope:recipient }
			if = {
				limit = { is_defender = scope:actor }
				add_defender = scope:recipient
				hidden_effect = {
					primary_attacker = {
						add_opinion = {
							target = scope:recipient
							modifier = betrayed_me_opinion
							opinion = -25
						}
					}
				}
			}
			else = {
				add_attacker = scope:recipient
				primary_defender = {
					hidden_effect = {
						add_opinion = {
							target = scope:recipient
							modifier = betrayed_me_opinion
							opinion = -25
						}
					}
				}
			}
		}
	}
	scope:recipient = { custom_tooltip = ep3_switch_faction_war_sides_cooldown_effect_tt }
}

ep3_raid_insight_effect = {
	add_character_modifier = {
		modifier = raid_insight_$TYPE$
		years = 5
	}
}

# Set up titles in the new realm.
# Determine potential breakaways
ep3_landless_invasion_titles_taken_effect = {
	# Determine independent vassals
	adventurer_realm_destabilisation_list_effect = {
		TITLE_RECEIVER = $TITLE_RECEIVER$
		TITLE_GIVER = $TITLE_GIVER$
		TITLE = $TITLE$
	}
	# Determine hierarchy
	ep3_become_landed_save_liege_effect = {
		TITLE_GIVER = $TITLE_GIVER$
		ALWAYS_INDEPENDENT = yes
		TITLE_LIST = $TITLE_LIST$
	}
	# Resolve title, liege, government changes
	if = {
		limit = { exists = scope:claimant }
		ep3_become_landed_transfer_effect = {
			TITLE_RECEIVER = $TITLE_RECEIVER$
			TITLE_LIST = $TITLE_LIST$
			TYPE = conquest_claim
			REASON = flag:conquest_claim
			ENNOBLED_ADVENTURER = flag:yes
		}
	}
	else = {
		ep3_become_landed_transfer_effect = {
			TITLE_RECEIVER = $TITLE_RECEIVER$
			TITLE_LIST = $TITLE_LIST$
			TYPE = conquest
			REASON = flag:conquest
			ENNOBLED_ADVENTURER = flag:yes
		}
	}
	# Vassals break away
	adventurer_realm_destabilisation_transfer_effect = {
		TITLE = $TITLE$
		TITLE_RECEIVER = $TITLE_RECEIVER$
		TITLE_GIVER = $TITLE_GIVER$
		TITLE_LIST = $TITLE_LIST$
	}
}

governor_contract_invalidated_message_effect = {
	task_contract_taker ?= {
		save_scope_as = task_contract_character
		send_interface_message = {
			type = event_generic_bad_with_text
			title = admin_contract_invalidated_title
			left_icon = scope:task_contract_character
			desc = admin_contract_invalidated_desc

			current_travel_plan ?= { 
				if = {
					limit = { can_cancel = yes }
					cancel_travel_plan = yes
				}
			}
		}
	}
}

assign_favourite_child_effect = {
	$ACTOR$ = {
		random_relation = {
			type = favorite_child
			save_scope_as = current_favorite
		}
		send_interface_toast = {
			title = choose_favorite_interaction_notification
			left_icon = $ACTOR$
			right_icon = $RECIPIENT$
			if = {
				limit = {
					exists = scope:current_favorite
				}
				remove_relation_favorite_child = scope:current_favorite
				scope:current_favorite = {
					add_opinion = {
						target = $ACTOR$
						modifier = no_more_favorite_opinion
					}
				}
			}
			set_relation_favorite_child = { reason = favorite_child_chosen target = $RECIPIENT$ }
			$RECIPIENT$ = {
				add_opinion = {
					target = $ACTOR$
					modifier = made_favorite_opinion
				}
			}
			custom_tooltip = choose_favorite_interaction_notification.tt
		}
		hidden_effect = {
			every_close_or_extended_family_member = {
				limit = {
					NOT = { this = $RECIPIENT$ }
					OR = {
						is_child_of = $ACTOR$
						is_grandchild_of = $ACTOR$
						is_great_grandchild_of = $ACTOR$
					}
				}
				add_opinion = {
					target = $ACTOR$
					modifier = has_favorite_opinion
				}
				add_opinion = {
					target = $RECIPIENT$
					modifier = is_favorite_opinion
				}
			}
		}
	}
}

add_random_internal_estate_building = {
	random_list = {
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = servants_quarters_01 }
			}
			add_domicile_building = servants_quarters_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = servants_quarters_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = library_01 }
			}
			add_domicile_building = library_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = library_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = bath_01 }
			}
			add_domicile_building = bath_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = bath_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = courtyard_01 }
			}
			add_domicile_building = courtyard_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = courtyard_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = prison_01 }
			}
			add_domicile_building = prison_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = prison_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = living_quarters_01 }
			}
			add_domicile_building = living_quarters_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = living_quarters_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = trophy_room_01 }
			}
			add_domicile_building = trophy_room_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = trophy_room_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = office_01 }
			}
			add_domicile_building = office_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = office_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = guest_room_01 }
			}
			add_domicile_building = guest_room_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = guest_room_02
			}
		}
		10 = {
			trigger = {
				NOT = { has_domicile_building_or_higher = wine_cellar_01 }
			}
			add_domicile_building = wine_cellar_01
			if = {
				limit = { has_domicile_building = estate_main_03 }
				add_domicile_building = wine_cellar_02
			}
		}
	}
}

fill_external_estate_building_effect = {
	switch = {
		trigger = has_domicile_building_or_higher
		estate_main_05 = {
			while = {
				limit = { free_external_domicile_building_slots >= 1 }
				add_random_external_estate_building = yes
			}
		}
		estate_main_04 = {
			while = {
				limit = { free_external_domicile_building_slots >= 2 }
				add_random_external_estate_building = yes
			}
		}
		estate_main_03 = {
			while = {
				limit = { free_external_domicile_building_slots >= 3 }
				add_random_external_estate_building = yes
			}
		}
		estate_main_02 = {
			while = {
				limit = { free_external_domicile_building_slots >= 4 }
				add_random_external_estate_building = yes
			}
		}
		estate_main_01 = {
			while = {
				limit = { free_external_domicile_building_slots >= 5 }
				add_random_external_estate_building = yes
			}
		}
	}
}

add_random_external_estate_building = {
	if = {
		limit = {
			free_external_domicile_building_slots >= 1
		}
		random_list = {
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = temple_small_01
						owner = {
							government_has_flag = government_is_celestial 
						}
					}
				}
				add_domicile_building = temple_small_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = temple_small_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = barracks_01 }
				}
				add_domicile_building = barracks_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = barracks_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = watchtower_01 }
				}
				add_domicile_building = watchtower_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = watchtower_02
				}
			}
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = vineyard_01 
						owner.culture ?= {
							has_innovation = innovation_champa_rice
						}
					}
				}
				add_domicile_building = vineyard_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = vineyard_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = guardhouse_01 }
				}
				add_domicile_building = guardhouse_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = guardhouse_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = garden_01 }
				}
				add_domicile_building = garden_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = garden_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = storage_01 }
				}
				add_domicile_building = storage_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = storage_02
				}
			}
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = grain_field_01
						owner.culture ?= {
							has_innovation = innovation_champa_rice
						}
					}
				}
				add_domicile_building = grain_field_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = grain_field_02
				}
			}
			10 = {
				trigger = {
					NOT = { 
						has_domicile_building_or_higher = rice_field_01
					}
					owner.culture ?= {
						has_innovation = innovation_champa_rice
					}
				}
				add_domicile_building = rice_field_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = rice_field_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = stable_01 }
				}
				add_domicile_building = stable_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = stable_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = workshop_01 }
				}
				add_domicile_building = workshop_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = workshop_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = market_01 }
				}
				add_domicile_building = market_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = market_02
				}
			}
			10 = {
				trigger = {
					NOT = { has_domicile_building_or_higher = grazing_land_01 }
				}
				add_domicile_building = grazing_land_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = grazing_land_02
				}
			}
			10 = {
				trigger = {
					NOR = { 
						has_domicile_building_or_higher = olive_01 
						owner.culture ?= {
							has_innovation = innovation_champa_rice
						}
					}
				}
				add_domicile_building = olive_01
				if = {
					limit = { has_domicile_building = estate_main_03 }
					add_domicile_building = olive_02
				}
			}
		}
	}
}

estate_newly_built_grain_field_effect = {
	scope:owner = {
		if = {
			limit = {
				is_alive = yes
				years_from_game_start >= 1
			}
			random = {
				chance = 75
				add_character_flag = {
					flag = domicile_new_built_grain_field
					months = 1
				}
				trigger_event = {
					id = ep3_governor_yearly.3001
					days = 5
				}
			}
		}
	}
}

demand_pastureland_adventurer_interaction_effect = {
	# scope:actor = adventurer
	# scope:recipient = herder
	# seized_pastureland = titles
	scope:target = { add_to_list = seized_pastureland }
	scope:recipient = { save_scope_as = defender }

	hidden_effect = {
		scope:actor = {
			if = {
				limit = { character:90028 ?= this }
				add_character_flag = hereward_settled_flag
			}
			if = {
				limit = {
					NOT = { has_trait = adventurer }
				}
				add_trait = adventurer
			}
			every_courtier = {
				set_variable = {
					name = former_camp_leader
					value = scope:actor
				}
				add_trait = adventurer_follower
			}
		}
	}

	# Determine hierarchy
	ep3_become_landed_save_liege_effect = {
		TITLE_GIVER = scope:recipient
		ALWAYS_INDEPENDENT = no
		TITLE_LIST = seized_pastureland
	}
	# Resolve title, liege, government changes
	ep3_become_landed_transfer_effect = {
		TITLE_RECEIVER = scope:actor
		TITLE_LIST = seized_pastureland
		TYPE = granted
		REASON = flag:seized_pastureland
		ENNOBLED_ADVENTURER = flag:yes
	}
	# Messages for nearby players
	create_landed_ruler_message_effect = {
		LANDED = scope:actor
		FLAVOR_CHAR = scope:recipient
		FLAVOR_TITLE = scope:target
		REASON = flag:seized_pastureland
	}
}
