﻿##################################################
# Landless Adventurer Camp Scripted Effects
##################################################

##################################################
# Misc Effects

apply_saharan_provisions_debuff_effect = {
	if = {
		limit = {
			geographical_region = custom_sahara_proper
			OR = {
				terrain = desert
				terrain = desert_mountains
			}
		}
		add_province_modifier = world_africa_sahara_provisions_usage_multiplier_modifier
	}
}

become_bandit_king_decision_effect = {
	add_character_modifier = bandit_king_modifier
	bandit_king_maa_spawn_effect = yes
	add_prestige_level = -3
	remove_trait = knight_errant
}

##################################################
# Flavourisation Effects

remove_camp_purpose_flavourisation_variables_effect = {
	remove_variable = flavourise_camp_purpose_mercenaries
	remove_variable = flavourise_camp_purpose_wanderers
	remove_variable = flavourise_camp_purpose_scholars
	remove_variable = flavourise_camp_purpose_explorers
	remove_variable = flavourise_camp_purpose_brigands
	remove_variable = flavourise_camp_purpose_legitimists
}

##################################################
# Contract Effects

populate_location_with_contracts_effect = {
	if = {
		limit = {
			# The AI have their own system for getting contracts.
			trigger_if = {
				limit = { is_landless_adventurer = yes }
				is_ai = no
			}
			lock_contracts_from_spawning_in_sahara_trigger = yes
		}
		# Flag our pre-existing contracts if necessary.
		if = {
			limit = { exists = scope:toggle_distance_ignore }
			every_character_task_contract = { add_to_list = pre_existing_contracts_list }
		}
		# Now populate.
		populate_task_contracts_for_area = {
			location = $AREA_CHAR$.capital_province
			amount = $AMOUNT$
			group = { 
				laamp_contracts_transport_group 
				laamp_contracts_criminal_group
				laamp_contracts_diplomacy_group
				laamp_contracts_hireling_group
				laamp_contracts_hunting_group
				laamp_contracts_intrigue_group
				laamp_contracts_learning_group
				laamp_contracts_martial_group
				laamp_contracts_stewardship_group
				laamp_contracts_justicar_group 
			}
		}
		# Then flag our new contracts (again, if necessary).
		if = {
			limit = { exists = scope:toggle_distance_ignore }
			every_character_task_contract = {
				limit = {
					NOT = { is_in_list = pre_existing_contracts_list }
				}
				set_variable = {
					name = retain_despite_distance
					value = yes
				}
			}
		}
	}
}

populate_location_with_special_contracts_effect = {
	if = {
		limit = {
			# The AI have their own system for getting contracts.
			trigger_if = {
				limit = { is_landless_adventurer = yes }
				is_ai = no
			}
			lock_contracts_from_spawning_in_sahara_trigger = yes
		}
		# Flag our pre-existing contracts if necessary.
		if = {
			limit = { exists = scope:toggle_distance_ignore }
			every_character_task_contract = { add_to_list = pre_existing_contracts_list }
		}
		populate_task_contracts_for_area = {
			location = $AREA_CHAR$.capital_province
			amount = $AMOUNT$
			group = { 
				laamp_contracts_transport_group 
				laamp_contracts_war_group 
				laamp_contracts_hireling_group
				laamp_contracts_intrigue_group
				laamp_contracts_legitimist_group
			}
		}
		# Then flag our new contracts (again, if necessary).
		if = {
			limit = { exists = scope:toggle_distance_ignore }
			every_character_task_contract = {
				limit = {
					NOT = { is_in_list = pre_existing_contracts_list }
				}
				set_variable = {
					name = retain_despite_distance
					value = yes
				}
			}
		}
	}
}

fire_generic_contract_scheme_on_going_events_effect = {
	save_scope_as = scheme
	if = {
		limit = {
			is_scheme_target_type = character
		}
		scheme_target_character = { save_scope_as = target }
	}
	else = {
		scheme_target_title.holder = { save_scope_as = target }
	}
	
	scheme_owner = {
		save_scope_as = owner
		trigger_event = { on_action = laamp_base_contract_scheme_on_going_events }
	}
}

spawn_join_war_contracts_effect = {
	$WAR$ = { save_scope_as = war }
	$ATTACKER$ = { save_scope_as = attacker }
	$DEFENDER$ = { save_scope_as = defender }
	$LAAMP$ = {
		#spawn a defensive join war contract, if defender is weaker than attacker or in a civil war
		if = {
			limit = {
				OR = {
					task_contract_join_war_attacker_strength_ratio_value > 1
					scope:war = {
						using_civil_war_cb_trigger = yes
					}
				}
				can_create_task_contract = {
					type_name = laamp_join_war_contract
					employer = scope:defender
				}
			}
			send_interface_message = {
				type = msg_task_contract_created
				title = join_war_contract_spawn.t
				desc = join_war_contract_spawn.desc_defender
				left_icon = scope:attacker
				right_icon = scope:defender
				create_task_contract = {
					task_contract_employer = scope:defender
					task_contract_type = laamp_join_war_contract
					task_contract_tier = scope:defender.task_contract_tier_value
					location = scope:defender.location
					save_scope_as = new_contract_defender
				}
			}
			scope:new_contract_defender = {
				set_variable = {
					name = retain_despite_distance
					value = yes
				}
				set_variable = {
					name = task_contract_war
					value = scope:war
				}
				set_variable = {
					name = task_contract_employer
					value = scope:defender
				}
				set_variable = {
					name = task_contract_target
					value = scope:war.primary_attacker
				}
			}
		}
		#spawn an offensive join war contract for the same war, if attacker has +/- 60% the strength of defender
		if = {
			limit = {
				task_contract_join_war_attacker_strength_ratio_value >= 0.4
				task_contract_join_war_attacker_strength_ratio_value <= 1.6
				can_create_task_contract = {
					type_name = laamp_join_war_contract
					employer = scope:attacker
				}
			}
			send_interface_message = {
				type = msg_task_contract_created
				title = join_war_contract_spawn.t
				desc = join_war_contract_spawn.desc_attacker
				left_icon = scope:attacker
				right_icon = scope:defender
				create_task_contract = {
					task_contract_employer = scope:attacker
					task_contract_type = laamp_join_war_contract
					task_contract_tier = scope:attacker.task_contract_tier_value
					location = scope:attacker.location
					save_scope_as = new_contract_attacker
				}
			}
			scope:new_contract_attacker = {
				set_variable = {
					name = retain_despite_distance
					value = yes
				}
				set_variable = {
					name = task_contract_war
					value = scope:war
				}
				set_variable = {
					name = task_contract_employer
					value = scope:attacker
				}
				set_variable = {
					name = task_contract_target
					value = scope:war.primary_defender
				}
			}
		}
		#else spawn contracts for both sides if their strength difference is +/- 100%
		if = {
			limit = {
				NOR = {
					exists = scope:new_contract_defender
					exists = scope:new_contract_attacker
				}
				task_contract_join_war_attacker_strength_ratio_value >= 0
				task_contract_join_war_attacker_strength_ratio_value <= 2
			}
			if = {
				limit = {
					can_create_task_contract = {
						type_name = laamp_join_war_contract
						employer = scope:attacker
					}
				}
				send_interface_message = {
					type = msg_task_contract_created
					title = join_war_contract_spawn.t
					desc = join_war_contract_spawn.desc_attacker
					left_icon = scope:attacker
					right_icon = scope:defender
					create_task_contract = {
						task_contract_employer = scope:attacker
						task_contract_type = laamp_join_war_contract
						task_contract_tier = scope:attacker.task_contract_tier_value
						location = scope:attacker.location
						save_scope_as = new_contract_attacker
					}
				}
				scope:new_contract_attacker = {
					set_variable = {
						name = retain_despite_distance
						value = yes
					}
					set_variable = {
						name = task_contract_war
						value = scope:war
					}
					set_variable = {
						name = task_contract_employer
						value = scope:attacker
					}
					set_variable = {
						name = task_contract_target
						value = scope:war.primary_defender
					}
				}
			}
			if = {
				limit = {
					can_create_task_contract = {
						type_name = laamp_join_war_contract
						employer = scope:defender
					}
				}
				send_interface_message = {
					type = msg_task_contract_created
					title = join_war_contract_spawn.t
					desc = join_war_contract_spawn.desc_defender
					left_icon = scope:attacker
					right_icon = scope:defender
					create_task_contract = {
						task_contract_employer = scope:defender
						task_contract_type = laamp_join_war_contract
						task_contract_tier = scope:defender.task_contract_tier_value
						location = scope:defender.location
						save_scope_as = new_contract_defender
					}
				}
				scope:new_contract_defender = {
					set_variable = {
						name = retain_despite_distance
						value = yes
					}
					set_variable = {
						name = task_contract_war
						value = scope:war
					}
					set_variable = {
						name = task_contract_employer
						value = scope:defender
					}
					set_variable = {
						name = task_contract_target
						value = scope:war.primary_attacker
					}
				}
			}				
		}
	}
	debug_log = "spawn_join_war_contracts_effect"
	debug_log_scopes = yes
}

# used in task contracts triggered from council tasks, assumes you already saved root as task_contract
# COUNCILLOR_TYPE = councillor type name
# COUNCIL_TASK_TYPE = council task type name
assign_council_task_scopes_in_contract_effect = {
	if = {
		limit = {
			scope:task_contract.task_contract_employer.cp:$COUNCILLOR_TYPE$ ?= {
				is_performing_council_task = $COUNCIL_TASK_TYPE$
			}
		}
		set_task_contract_target = scope:task_contract.task_contract_employer.cp:$COUNCILLOR_TYPE$
		task_contract_target = {
			save_scope_as = task_contract_councillor
			liege = {
				save_scope_as = task_contract_councillor_liege
			}				
		}
	}
	else = {
		set_task_contract_target = scope:task_contract.task_contract_employer.liege
		scope:task_contract.task_contract_employer = {
			save_scope_as = task_contract_councillor
			liege = {
				save_scope_as = task_contract_councillor_liege
			}
		}
	}
	set_variable = {
		name = task_contract_target
		value = scope:task_contract.task_contract_target
	}
	set_variable = {
		name = task_contract_councillor
		value = scope:task_contract_councillor
	}
	set_variable = {
		name = task_contract_councillor_liege
		value = scope:task_contract_councillor_liege
	}	
}

# We have to sort our randomisation here because otherwise we get oodles of false positives from trying to do it inside the subsequent value.
grab_gold_fuzz_number_effect = {
	save_scope_value_as = {
		name = gold_fuzz
		value = {
			integer_range = {
				min = 0
				max = 9
			}
		}
	}
}

##################################################
# Reward Effects

# General logic for giving out the appropriate amount of currencies to a laamp upon contract completion.
laamp_rewards_disburse_ordinary_currency_effect = {
	# Grab some scope for ease of access.
	save_scope_as = task_contract
	task_contract_employer = { save_scope_as = employer }
	task_contract_taker = { save_scope_as = taker }
	# Gold.
	scope:employer = {
		# Yes, this is necessary. We get boot errors if we compare a direct script value vs. zero: maths works out, it's just Jomini Magic doin' its thing.
		save_scope_value_as = {
			name = gold
			value = {
				value = $GOLD$
				if = {
					limit = { $EXTRA_REWARD$ = flag:yes }
					multiply = task_contract_extra_reward_multiplier_value
				}
				round = yes
			}
		}
		if = {
			limit = { scope:gold != 0 }
			# Directly add gold to taker without further conditions
			scope:taker = { add_gold = scope:gold }
		}
	}
	# Everything else.
	scope:taker = {
		# Prestige.
		if = {
			limit = { $PRESTIGE$ != 0 }
			add_prestige = {
				value = {
					value = $PRESTIGE$
					if = {
						limit = { $EXTRA_REWARD$ = flag:yes }
						multiply = task_contract_extra_reward_multiplier_value
					}
					round = yes
				}
			}
		}
		# Piety.
		if = {
			limit = { $PIETY$ != 0 }
			add_piety = {
				value = {
					value = $PIETY$
					if = {
						limit = { $EXTRA_REWARD$ = flag:yes }
						multiply = task_contract_extra_reward_multiplier_value
					}
					round = yes
				}
			}
		}
		# Provisions.
		if = {
			limit = {
				$PROVISIONS$ != 0
				exists = domicile
			}
			domicile = {
				change_provisions = {
					value = {
						value = $PROVISIONS$
						if = {
							limit = { $EXTRA_REWARD$ = flag:yes }
							multiply = task_contract_extra_reward_multiplier_value
						}
						round = yes
					}
				}
			}
		}
		# Opinion.
		if = {
			limit = { $OPINION$ != 0 }
			reverse_add_opinion = {
				modifier = $OPINION_TYPE$
				target = scope:employer
				opinion = $OPINION$
			}
		}
		# Contact.
		save_scope_value_as = {
			name = add_contact_toggle
			value = flag:$CONTACT$
		}
		save_scope_value_as = {
			name = add_contact_hook_toggle
			value =flag:$CONTACT_HOOK$
		}
		if = {
			limit = { scope:add_contact_toggle != flag:no }
			# Huck 'em into our contact list.
			add_contact = scope:employer
			# Give hook as well as contact?
			if = {
				limit = { scope:add_contact_hook_toggle = flag:yes }
				add_hook_if_possible_default_length_effect = {
					TARGET = scope:employer 
					TYPE = contact_list_weak_hook
				}
			}
		}
	}
}

# Dish out XP in the gallowsbait trait.
laamp_rewards_apply_criminal_xp_effect = {
	save_scope_as = xp_gainer
	save_scope_value_as = {
		name = xp_change_min
		value = $XP_MIN$
	}
	if = {
		limit = {
			NOT = { has_trait = gallowsbait }
		}
		add_trait = gallowsbait
	}
	add_trait_xp = {
		trait = gallowsbait
		track = $TRACK$
		value = {
			# Always use the gallowsbait_xp_minor_gain and related values for positive gains to ensure proper multipliers are applied!
			integer_range = {
				min = $XP_MIN$
				max = $XP_MAX$
			}
			if = {
				limit = {
					scope:xp_change_min > 0 # Do not multiply reductions
					scope:xp_gainer.domicile ?= { has_domicile_parameter = camp_reduced_criminal_contract_xp_gain }
				}
				multiply = reduced_criminal_contract_xp_gain_value
			}
			floor = yes # Round numbers down when modified
		}
	}
}

# Dish out XP in the Knight-Errant trait.
laamp_rewards_apply_knight_errant_xp_effect = {
	if = {
		limit = { has_trait = knight_errant }
		save_scope_as = xp_gainer
		save_scope_value_as = {
			name = xp_change_min
			value = $XP_MIN$
		}
		add_trait_xp = {
			trait = knight_errant
			value = {
				# Always use the gallowsbait_xp_minor_gain and related values for positive gains to ensure proper multipliers are applied!
				integer_range = {
					min = $XP_MIN$
					max = $XP_MAX$
				}
				floor = yes # Round numbers down when modified
			}
		}
	}
}

lower_gallowsbait_xp_effect = {
	if = {
		limit = { has_trait = gallowsbait }
		if = {
			limit = {
				has_trait = gallowsbait
				has_trait_xp = {
					trait = gallowsbait
					track = bandit
					value >= 1
				}
			}
			add_trait_xp = {
				trait = gallowsbait
				track = bandit
				VALUE = $VALUE$
			}
		}
		else_if = {
			limit = {
				has_trait = gallowsbait
				has_trait_xp = {
					trait = gallowsbait
					track = trickster
					value >= 1
				}
			}
			add_trait_xp = {
				trait = gallowsbait
				track = trickster
				VALUE = $VALUE$
			}
		}
		else_if = {
			limit = {
				has_trait = gallowsbait
				has_trait_xp = {
					trait = gallowsbait
					track = marauder
					value >= 1
				}
			}
			add_trait_xp = {
				trait = gallowsbait
				track = marauder
				VALUE = $VALUE$
			}
		}
		else_if = {
			limit = {
				has_trait = gallowsbait
				has_trait_xp = {
					trait = gallowsbait
					track = poacher
					value >= 1
				}
			}
			add_trait_xp = {
				trait = gallowsbait
				track = poacher
				VALUE = $VALUE$
			}
		}
		else = { # Thief
			add_trait_xp = {
				trait = gallowsbait
				track = thief
				VALUE = $VALUE$
			}
		}
	}
}

# grants hook and patronage from the councillor or liege of the employer in council task contracts, assumes you saved root as task_contract before
gain_patron_and_hook_from_council_task_contract_effect = {
	task_contract_taker = {
		if = {
			limit = {
				scope:task_contract.task_contract_employer = scope:task_contract.var:task_contract_councillor
				scope:task_contract.var:task_contract_councillor_liege = { is_ai = yes }
			}
			add_contact = scope:task_contract.var:task_contract_councillor_liege
			add_hook_if_possible_default_length_effect = {
				TARGET = scope:task_contract.var:task_contract_councillor_liege
				TYPE = contact_list_weak_hook
			}
		}
		else = {
			add_contact = scope:task_contract.var:task_contract_councillor
			add_hook_if_possible_default_length_effect = {
				TARGET = scope:task_contract.var:task_contract_councillor
				TYPE = contact_list_weak_hook
			}
		}
	}		
}

task_contract_justicar_contract_tracker_effect = {
	custom_tooltip = task_contract_justicar_contract_tracker_effect.tt
	increment_variable_effect = {
		VAR = justicar_contracts_succeeded_counter
		VAL = 1
	}
}

task_successful_noncrim_contract_count_effect = {
	custom_tooltip = task_successful_noncrim_contract_count_effect_tt
	increment_variable_effect = {
		VAR = laamp_total_noncrim_contracts_successfully_completed
		VAL = 1
	}
}

##################################################
# Camp Effects

# Remove any inappropriate building upgrades on camp purpose changing.
laamp_clear_inappropriate_buildings_effect = {
	# Warn that purpose-specific buildings will be demolished.
	if = {
		limit = {
			domicile = {
				OR = {
					has_domicile_building = supply_tent_reserve_provisions
					has_domicile_building = supply_tent_reserve_water
					has_domicile_building = supply_tent_climbing_gear
					has_domicile_building = supply_tent_subdued_gear
					has_domicile_building = barber_tent_reference_corpus
					has_domicile_building = barber_tent_morticians_tools
					has_domicile_building = baggage_train_siege_engineers
					has_domicile_building = baggage_train_scribes
					has_domicile_building = baggage_train_proof_of_claims
					has_domicile_building = baggage_train_ransom_cages
					has_domicile_building = baggage_train_negotiators
					has_domicile_building = baggage_train_ascetics
					has_domicile_building = camp_fire_local_hangers_on
					has_domicile_building = camp_fire_future_dreams
					has_domicile_building = camp_fire_nightly_debates
					has_domicile_building = camp_fire_juicy_rumors
					has_domicile_building = proving_grounds_lockwagon
					has_domicile_building = proving_grounds_martial_study
					has_domicile_building = proving_grounds_the_stick_game
					has_domicile_building = proving_grounds_bodyguard_drills
					has_domicile_building = camp_perimeter_extra_watch
					has_domicile_building = camp_perimeter_palisade
					has_domicile_building = camp_perimeter_ditch
				}
			}
		}
		custom_tooltip = laamp_clear_inappropriate_buildings_effect.tt.buildings_may_be_lost
	}
	# Aaaand then queue our demolition.
	trigger_event = {
		id = ep3_laamps.1021
		delayed = yes
	}
}

# Liquidate a camp to refund a portion of all its buildings.
laamp_clear_domicile_buildings_effect = {
	domicile ?= {
		if = {
			limit = { has_domicile_building = camp_main_04 }
			remove_domicile_building = camp_main_04
		}
		if = {
			limit = { has_domicile_building = camp_main_03 }
			remove_domicile_building = camp_main_03
		}
		if = {
			limit = { has_domicile_building = camp_main_02 }
			remove_domicile_building = camp_main_02
		}
		if = {
			limit = { has_domicile_building = camp_main_01 }
			remove_domicile_building = camp_main_01
		}
		if = {
			limit = { has_domicile_building = supply_tent_sutler }
			remove_domicile_building = supply_tent_sutler
		}
		if = {
			limit = { has_domicile_building = supply_tent_mender }
			remove_domicile_building = supply_tent_mender
		}
		if = {
			limit = { has_domicile_building = supply_tent_smithy }
			remove_domicile_building = supply_tent_smithy
		}
		if = {
			limit = { has_domicile_building = supply_tent_arsenal }
			remove_domicile_building = supply_tent_arsenal
		}
		if = {
			limit = { has_domicile_building = supply_tent_reserve_provisions }
			remove_domicile_building = supply_tent_reserve_provisions
		}
		if = {
			limit = { has_domicile_building = supply_tent_reserve_water }
			remove_domicile_building = supply_tent_reserve_water
		}
		if = {
			limit = { has_domicile_building = supply_tent_climbing_gear }
			remove_domicile_building = supply_tent_climbing_gear
		}
		if = {
			limit = { has_domicile_building = supply_tent_subdued_gear }
			remove_domicile_building = supply_tent_subdued_gear
		}
		if = {
			limit = { has_domicile_building = supply_tent_06 }
			remove_domicile_building = supply_tent_06
		}
		if = {
			limit = { has_domicile_building = supply_tent_05 }
			remove_domicile_building = supply_tent_05
		}
		if = {
			limit = { has_domicile_building = supply_tent_04 }
			remove_domicile_building = supply_tent_04
		}
		if = {
			limit = { has_domicile_building = supply_tent_03 }
			remove_domicile_building = supply_tent_03
		}
		if = {
			limit = { has_domicile_building = supply_tent_02 }
			remove_domicile_building = supply_tent_02
		}
		if = {
			limit = { has_domicile_building = supply_tent_01 }
			remove_domicile_building = supply_tent_01
		}
		if = {
			limit = { has_domicile_building = barber_tent_surgeons_tools }
			remove_domicile_building = barber_tent_surgeons_tools
		}
		if = {
			limit = { has_domicile_building = barber_tent_dentists_tools }
			remove_domicile_building = barber_tent_dentists_tools
		}
		if = {
			limit = { has_domicile_building = barber_tent_torturers_tools }
			remove_domicile_building = barber_tent_torturers_tools
		}
		if = {
			limit = { has_domicile_building = barber_tent_reference_corpus }
			remove_domicile_building = barber_tent_reference_corpus
		}
		if = {
			limit = { has_domicile_building = barber_tent_morticians_tools }
			remove_domicile_building = barber_tent_morticians_tools
		}
		if = {
			limit = { has_domicile_building = barber_tent_06 }
			remove_domicile_building = barber_tent_06
		}
		if = {
			limit = { has_domicile_building = barber_tent_05 }
			remove_domicile_building = barber_tent_05
		}
		if = {
			limit = { has_domicile_building = barber_tent_04 }
			remove_domicile_building = barber_tent_04
		}
		if = {
			limit = { has_domicile_building = barber_tent_03 }
			remove_domicile_building = barber_tent_03
		}
		if = {
			limit = { has_domicile_building = barber_tent_02 }
			remove_domicile_building = barber_tent_02
		}
		if = {
			limit = { has_domicile_building = barber_tent_01 }
			remove_domicile_building = barber_tent_01
		}
		if = {
			limit = { has_domicile_building = baggage_train_ample_steeds }
			remove_domicile_building = baggage_train_ample_steeds
		}
		if = {
			limit = { has_domicile_building = baggage_train_porters }
			remove_domicile_building = baggage_train_porters
		}
		if = {
			limit = { has_domicile_building = baggage_train_trackers }
			remove_domicile_building = baggage_train_trackers
		}
		if = {
			limit = { has_domicile_building = baggage_train_kennel }
			remove_domicile_building = baggage_train_kennel
		}
		if = {
			limit = { has_domicile_building = baggage_train_siege_engineers }
			remove_domicile_building = baggage_train_siege_engineers
		}
		if = {
			limit = { has_domicile_building = baggage_train_shrine }
			remove_domicile_building = baggage_train_shrine
		}
		if = {
			limit = { has_domicile_building = baggage_train_scribes }
			remove_domicile_building = baggage_train_scribes
		}
		if = {
			limit = { has_domicile_building = baggage_train_bartering_grounds }
			remove_domicile_building = baggage_train_bartering_grounds
		}
		if = {
			limit = { has_domicile_building = baggage_train_proof_of_claims }
			remove_domicile_building = baggage_train_proof_of_claims
		}
		if = {
			limit = { has_domicile_building = baggage_train_ransom_cages }
			remove_domicile_building = baggage_train_ransom_cages
		}
		if = {
			limit = { has_domicile_building = baggage_train_negotiators }
			remove_domicile_building = baggage_train_negotiators
		}
		if = {
			limit = { has_domicile_building = baggage_train_ascetics }
			remove_domicile_building = baggage_train_ascetics
		}
		if = {
			limit = { has_domicile_building = baggage_train_pleasure_tents }
			remove_domicile_building = baggage_train_pleasure_tents
		}
		if = {
			limit = { has_domicile_building = baggage_train_06 }
			remove_domicile_building = baggage_train_06
		}
		if = {
			limit = { has_domicile_building = baggage_train_05 }
			remove_domicile_building = baggage_train_05
		}
		if = {
			limit = { has_domicile_building = baggage_train_04 }
			remove_domicile_building = baggage_train_04
		}
		if = {
			limit = { has_domicile_building = baggage_train_03 }
			remove_domicile_building = baggage_train_03
		}
		if = {
			limit = { has_domicile_building = baggage_train_02 }
			remove_domicile_building = baggage_train_02
		}
		if = {
			limit = { has_domicile_building = baggage_train_01 }
			remove_domicile_building = baggage_train_01
		}
		if = {
			limit = { has_domicile_building = mess_tent_herbalists }
			remove_domicile_building = mess_tent_herbalists
		}
		if = {
			limit = { has_domicile_building = mess_tent_brewers }
			remove_domicile_building = mess_tent_brewers
		}
		if = {
			limit = { has_domicile_building = mess_tent_curers }
			remove_domicile_building = mess_tent_curers
		}
		if = {
			limit = { has_domicile_building = mess_tent_bakers }
			remove_domicile_building = mess_tent_bakers
		}
		if = {
			limit = { has_domicile_building = mess_tent_cooks }
			remove_domicile_building = mess_tent_cooks
		}
		if = {
			limit = { has_domicile_building = mess_tent_06 }
			remove_domicile_building = mess_tent_06
		}
		if = {
			limit = { has_domicile_building = mess_tent_05 }
			remove_domicile_building = mess_tent_05
		}
		if = {
			limit = { has_domicile_building = mess_tent_04 }
			remove_domicile_building = mess_tent_04
		}
		if = {
			limit = { has_domicile_building = mess_tent_03 }
			remove_domicile_building = mess_tent_03
		}
		if = {
			limit = { has_domicile_building = mess_tent_02 }
			remove_domicile_building = mess_tent_02
		}
		if = {
			limit = { has_domicile_building = mess_tent_01 }
			remove_domicile_building = mess_tent_01
		}
		if = {
			limit = { has_domicile_building = camp_fire_trailing_musicians }
			remove_domicile_building = camp_fire_trailing_musicians
		}
		if = {
			limit = { has_domicile_building = camp_fire_wandering_poets }
			remove_domicile_building = camp_fire_wandering_poets
		}
		if = {
			limit = { has_domicile_building = camp_fire_capering_fools }
			remove_domicile_building = camp_fire_capering_fools
		}
		if = {
			limit = { has_domicile_building = camp_fire_libations_for_the_lost }
			remove_domicile_building = camp_fire_libations_for_the_lost
		}
		if = {
			limit = { has_domicile_building = camp_fire_local_hangers_on }
			remove_domicile_building = camp_fire_local_hangers_on
		}
		if = {
			limit = { has_domicile_building = camp_fire_future_dreams }
			remove_domicile_building = camp_fire_future_dreams
		}
		if = {
			limit = { has_domicile_building = camp_fire_nightly_debates }
			remove_domicile_building = camp_fire_nightly_debates
		}
		if = {
			limit = { has_domicile_building = camp_fire_juicy_rumors }
			remove_domicile_building = camp_fire_juicy_rumors
		}
		if = {
			limit = { has_domicile_building = camp_fire_06 }
			remove_domicile_building = camp_fire_06
		}
		if = {
			limit = { has_domicile_building = camp_fire_05 }
			remove_domicile_building = camp_fire_05
		}
		if = {
			limit = { has_domicile_building = camp_fire_04 }
			remove_domicile_building = camp_fire_04
		}
		if = {
			limit = { has_domicile_building = camp_fire_03 }
			remove_domicile_building = camp_fire_03
		}
		if = {
			limit = { has_domicile_building = camp_fire_02 }
			remove_domicile_building = camp_fire_02
		}
		if = {
			limit = { has_domicile_building = camp_fire_01 }
			remove_domicile_building = camp_fire_01
		}
		if = {
			limit = { has_domicile_building = proving_grounds_horse_run }
			remove_domicile_building = proving_grounds_horse_run
		}
		if = {
			limit = { has_domicile_building = proving_grounds_camel_run }
			remove_domicile_building = proving_grounds_camel_run
		}
		if = {
			limit = { has_domicile_building = proving_grounds_elephantry_reserve }
			remove_domicile_building = proving_grounds_elephantry_reserve
		}
		if = {
			limit = { has_domicile_building = proving_grounds_nightly_barding_drills }
			remove_domicile_building = proving_grounds_nightly_barding_drills
		}
		if = {
			limit = { has_domicile_building = proving_grounds_life_in_the_saddle }
			remove_domicile_building = proving_grounds_life_in_the_saddle
		}
		if = {
			limit = { has_domicile_building = proving_grounds_the_stump }
			remove_domicile_building = proving_grounds_the_stump
		}
		if = {
			limit = { has_domicile_building = proving_grounds_personal_bouts }
			remove_domicile_building = proving_grounds_personal_bouts
		}
		if = {
			limit = { has_domicile_building = proving_grounds_training_circle }
			remove_domicile_building = proving_grounds_training_circle
		}
		if = {
			limit = { has_domicile_building = proving_grounds_mock_battle_drills }
			remove_domicile_building = proving_grounds_mock_battle_drills
		}
		if = {
			limit = { has_domicile_building = proving_grounds_lockwagon }
			remove_domicile_building = proving_grounds_lockwagon
		}
		if = {
			limit = { has_domicile_building = proving_grounds_martial_study }
			remove_domicile_building = proving_grounds_martial_study
		}
		if = {
			limit = { has_domicile_building = proving_grounds_the_stick_game }
			remove_domicile_building = proving_grounds_the_stick_game
		}
		if = {
			limit = { has_domicile_building = proving_grounds_bodyguard_drills }
			remove_domicile_building = proving_grounds_bodyguard_drills
		}
		if = {
			limit = { has_domicile_building = proving_grounds_06 }
			remove_domicile_building = proving_grounds_06
		}
		if = {
			limit = { has_domicile_building = proving_grounds_05 }
			remove_domicile_building = proving_grounds_05
		}
		if = {
			limit = { has_domicile_building = proving_grounds_04 }
			remove_domicile_building = proving_grounds_04
		}
		if = {
			limit = { has_domicile_building = proving_grounds_03 }
			remove_domicile_building = proving_grounds_03
		}
		if = {
			limit = { has_domicile_building = proving_grounds_02 }
			remove_domicile_building = proving_grounds_02
		}
		if = {
			limit = { has_domicile_building = proving_grounds_01 }
			remove_domicile_building = proving_grounds_01
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_good_siting }
			remove_domicile_building = camp_perimeter_good_siting
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_fixed_layout }
			remove_domicile_building = camp_perimeter_fixed_layout
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_extra_watch }
			remove_domicile_building = camp_perimeter_extra_watch
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_palisade }
			remove_domicile_building = camp_perimeter_palisade
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_ditch }
			remove_domicile_building = camp_perimeter_ditch
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_camp_hygiene }
			remove_domicile_building = camp_perimeter_camp_hygiene
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_06 }
			remove_domicile_building = camp_perimeter_06
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_05 }
			remove_domicile_building = camp_perimeter_05
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_04 }
			remove_domicile_building = camp_perimeter_04
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_03 }
			remove_domicile_building = camp_perimeter_03
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_02 }
			remove_domicile_building = camp_perimeter_02
		}
		if = {
			limit = { has_domicile_building = camp_perimeter_01 }
			remove_domicile_building = camp_perimeter_01
		}
	}
}

##################################################
# Travel Effects

remove_laamp_from_exiled_county_automatically_effect = {
	$PROVINCE$ = { save_scope_as = province_effect }
	$LAAMP$ = { save_scope_as = laamp_effect }
	if = {
		limit = {
			exists = domicile.domicile_location.county.holder # Safety check
			is_laamp_exiled_from_province_trigger = {
				PROVINCE = scope:province_effect
				LAAMP = scope:laamp_effect
			}
		}
		scope:laamp_effect = {
			# Grab any valid destination to shift to.
			## Try somewhere more sensible if we're a player (which is less performant).
			domicile.domicile_location.county.holder = { # (is_laamp_exiled_from_province_trigger actually checks county holder)
				every_neighboring_and_across_water_top_liege_realm_owner = {
					limit = {
						NOT = { this = scope:laamp_effect }
					}
					every_realm_border_county = {
						limit = {
							exists = holder
							NOT = {
								holder = {
									is_laamp_exiled_from_holder_or_liege_trigger = { LAAMP = scope:laamp_effect }
								}
							}
						}
						add_to_list = reroute_counties_list
					}
				}
				ordered_in_list = {
					list = reroute_counties_list
					order_by = {
						value = "title_province.squared_distance(scope:laamp_effect.location)"
						multiply = -1
					}
					save_scope_as = exile_reroute
				}
			}
			## Otherwise, try to grab anywhere valid.
			if = {
				limit = {
					NOT = { exists = scope:exile_reroute }
				}
				domicile.domicile_location.county.holder = { # (is_laamp_exiled_from_province_trigger actually checks county holder)
					random_neighboring_and_across_water_top_liege_realm_owner = {
						random_realm_county = {
							limit = {
								exists = holder
								NOT = {
									holder = {
										is_laamp_exiled_from_holder_or_liege_trigger = { LAAMP = scope:laamp_effect }
									}
								}
							}
							save_scope_as = exile_reroute
						}
					}
				}
			}
			## If we got nothing (which is very unlikely), prep a backup.
			if = {
				limit = {
					NOT = { exists = scope:exile_reroute }
				}
				scope:province_effect = { save_scope_as = province_to_exit }
				ordered_empire = {
					limit = {
						any_de_jure_county = {
							exists = holder
							NOT = {
								holder = {
									is_laamp_exiled_from_holder_or_liege_trigger = { LAAMP = scope:laamp_effect }
								}
							}
						}
					}
					order_by = {
						title_capital_county = { add = "squared_distance(scope:province_to_exit)" }
						multiply = -1
					}
					random_de_jure_county = {
						limit = {
							exists = holder
							NOT = {
								holder = {
									is_laamp_exiled_from_holder_or_liege_trigger = { LAAMP = scope:laamp_effect }
								}
							}
						}
						save_scope_as = exile_reroute
					}
				}
			}
			# If we're an AI, give us some relief for our (likely) poor choice of destination.
			if = {
				limit = { is_ai = yes }
				add_character_modifier = laamp_provisions_forgiveness_travel_modifier
			}
			# Begin travelling.
			start_travel_plan = {
				destination = scope:exile_reroute.title_province
				travel_with_domicile = yes
				return_trip = no
				players_use_planner = no
			}
		}
	}
}

##################################################
# Event Effects

try_to_set_event_recurrer_relationship_effect = {
	if = {
		limit = {
			save_scope_as = liege_temp
			any_relation = {
				type = event_recurrer
				# Only count recurrers that are still useful: if they've been demoted or incapacitated, someone should fill the gap.
				is_valid_active_event_recurrer_trigger = { LIEGE = scope:liege_temp }
				# We don't want too few, don't want too many.
				count < 5
			}
		}
		set_relation_event_recurrer = $TARGET$
	}
}

laamp_contract_grab_suitable_rival_for_disposal_effect = {
	# Used in valid_laamp_contract_foe_trigger.
	save_scope_as = employer_ref
	# First, try to grab suitable rivals.
	ordered_relation = {
		type = rival
		limit = { valid_laamp_contract_foe_trigger = yes }
		order_by = "opinion(scope:employer_ref)"
		save_scope_as = target
	}
	# Then we try for potential rivals.
	## Non-if-else_if chaining here is deliberate: we iterate over each list once then look to see whether we came up with anything. If we didn't, we move on to the next list.
	### This saves us doubling up on checks for the same outcome.
	if = {
		limit = {
			NOT = { exists = scope:target }
		}
		ordered_relation = {
			type = potential_rival
			order_by = "opinion(scope:employer_ref)"
			limit = { valid_laamp_contract_foe_trigger = yes }
			save_scope_as = target
		}
	}
	# Then get peer vassals (if we're not independent).
	if = {
		limit = {
			NOT = { exists = scope:target }
			is_independent_ruler = no
		}
		liege = {
			ordered_vassal = {
				limit = { valid_laamp_contract_foe_trigger = yes }
				order_by = "opinion(scope:employer_ref)"
				save_scope_as = target
			}
		}
	}
	# Or neighbouring rulers otherwise.
	if = {
		limit = {
			NOT = { exists = scope:target }
		}
		ordered_neighboring_and_across_water_top_liege_realm_owner = {
			limit = {
				valid_laamp_contract_foe_trigger = yes
				# Make sure that we don't have barons putting out hits on emperors.
				highest_held_title_tier <= {
					value = scope:employer.highest_held_title_tier
					add = 2
				}
			}
			order_by = "opinion(scope:employer_ref)"
			save_scope_as = target
		}
	}
	# Then we just spawn someone.
	if = {
		limit = {
			NOT = { exists = scope:target }
		}
		save_scope_as = value_target
		create_character = {
			template = bandit_character_generic
			faith = scope:employer_ref.capital_county.faith
			culture = scope:employer_ref.capital_county.culture
			location = scope:employer_ref.capital_county.title_province
			gender_female_chance = scope_value_target_soldier_female_chance
			dynasty = none
			save_scope_as = target
			after_creation = {
				add_opinion = {
					target = scope:employer_ref
					modifier = hate_opinion
					opinion = -50
				}
				reverse_add_opinion = {
					target = scope:employer_ref
					modifier = hate_opinion
					opinion = -50
				}
			}
		}
	}
	# Plus, set up a grudge from our employer.
	scope:employer_ref = {
		set_random_rivalry_reason = {
			TARGET = scope:target
			RELATION = grudge
		}
	}
	# Finally, log them to the contract.
	scope:task_contract = {
		set_variable = {
			name = target
			value = scope:target
		}
	}
}

select_thriving_predator_animal_effect = {
	random_list = {
		# Boar
		20 = {
			trigger = {
				$LOCATION$ = { hunt_animal_boar_trigger = yes }
			}
			# We don't mind hunting them for pest control, regardless of faith.
			set_variable = {
				name = animal_type_event
				value = flag:boar
			}
		}
		# Wolf, Hyena, Lynx
		10 = {
			random_list = {
				# Wolf
				10 = {
					trigger = {
						$LOCATION$ = {
							NAND = {
								geographical_region = world_africa_west
								OR = {
									terrain = hills
									terrain = forest
									terrain = jungle
									terrain = mountains
								}
							}
						}
					}
					set_variable = {
						name = animal_type_event
						value = flag:wolf
					}
				}
				# Hyena
				20 = {
					trigger = {
						$LOCATION$ = { hunt_animal_hyena_trigger = yes }
					}
					set_variable = {
						name = animal_type_event
						value = flag:hyena
					}
				}
				# Lynx
				5 = {
					trigger = {
						$LOCATION$ = { hunt_animal_lynx_trigger = yes }
					}
					set_variable = {
						name = animal_type_event
						value = flag:lynx
					}
				}
			}
		}
		# Bear
		5 = {
			trigger = {
				$LOCATION$ = { hunt_animal_bear_trigger = yes }
			}
			set_variable = {
				name = animal_type_event
				value = flag:bear
			}
		}
		# Big Cat
		5 = {
			trigger = {
				$LOCATION$ = {
					hunt_animal_big_cat_trigger = yes
					OR = {
						hunt_animal_lion_trigger = yes
						hunt_animal_tiger_trigger = yes
						hunt_animal_leopard_trigger = yes
					}
				}
			}
			random_list = {
				# Lion
				5 = {
					trigger = {
						$LOCATION$ = { hunt_animal_lion_trigger = yes }
					}
					modifier = {
						factor = 5
						$LOCATION$ = {
							OR = {
								terrain = desert
								terrain = forest
							}
						}
					}
					set_variable = {
						name = animal_type_event
						value = flag:lion
					}
				}
				# Tiger
				5 = {
					trigger = {
						$LOCATION$ = { hunt_animal_tiger_trigger = yes }
					}
					modifier = {
						factor = 5
						$LOCATION$ = { terrain = jungle }
					}
					set_variable = {
						name = animal_type_event
						value = flag:tiger
					}
				}
				# Leopard
				5 = {
					trigger = {
						$LOCATION$ = { hunt_animal_leopard_trigger = yes }
					}
					modifier = {
						factor = 0.5
						$LOCATION$ = {
							OR = {
								terrain = desert
								terrain = mountains
							}
						}
					}
					set_variable = {
						name = animal_type_event
						value = flag:leopard
					}
				}
			}
		}
	}
	if = {
		limit = {
			NOT = { has_variable = animal_type_event }
		}
		set_variable = {
			name = animal_type_event
			value = flag:dog
		}
	}
}

relation_increase_harrying_stats_effect = {
	# Harrying logic.
	if = {
		limit = {
			character_is_valid_for_harrying_of_the_north_trigger = yes
			scope:target = {
				character_is_valid_for_harrying_of_the_north_trigger = yes
				is_important_or_vip_struggle_character = yes
			}
			culture != scope:target.culture
		}
		global_var:harrying_of_the_north = { save_scope_as = story }
		if = {
			limit = {
				OR = {
					AND = {
						character_is_valid_norman_for_harrying_of_the_north_trigger = yes
						scope:target = { character_is_valid_anglo_saxon_for_harrying_of_the_north_trigger = yes }
					}
					AND = {
						character_is_valid_anglo_saxon_for_harrying_of_the_north_trigger = yes
						scope:target = { character_is_valid_norman_for_harrying_of_the_north_trigger = yes }
					}
				}
			}
			ep3_increase_$STAT$_effect = { AMOUNT = harrying_miniscule_increase_value }
		}
	}
}

###########################
# City Construction Effects
laamp_city_builder_construction_effect = {
	if = {
		limit = {
			has_character_flag = laamp_city_builder_$FLAG$_upgrade
		}
		scope:city_location = {
			add_building = $BUILDING$_03
		}
	}
	else_if = {
		limit = {
			has_character_flag = laamp_city_builder_$FLAG$
		}
		scope:city_location = {
			add_building = $BUILDING$_02
		}
	}
}
