﻿##################################################
# INFO
# For contracts that are lewd.
# Generally should weight towards recruits/piety as rewards over gold/prestige
# These contracts will only be available if you have read the book, so you should expect the player character to be willing to do lewd things.
# Being Magister should give bonus options, but contracts should be similar
##################################################
##################################################
# GROUPS (for populate_task_contracts_for_area)
# 
# laamp_contracts_regula_group
# 
##################################################
## Regula Contracts
# laamp_regula_0001 	Arbitrate Marriage Dispute - Based on laamp_base_2031
##################################################

# Arbitrate a marriage dispute
# Sort out a peasant marriage, or potentially yoink peasant girl for yourself
# Based on "Settle a local dispute" vanilla contract
laamp_regula_0001 = {
	group = laamp_contracts_regula_group
	icon = "gfx/interface/icons/scheme_types/icon_scheme_elope.dds"

	travel = yes
	use_diplomatic_range = no

	weight = {
		# Standard weights.
		value = task_contract_weight_default_value
		add = laamp_contracts_weight_up_regula_value
		# Employer weights.
		scope:employer = {
			add = {
				# Weight up.
				## Traits that are likely to run into these kinds of lusty/perverted troubles but not super able to deal with them.
				if = {
					limit = { has_trait = chaste }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = lustful }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = arbitrary }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = fickle }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = lazy }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = shy }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = forgiving }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				# Weight down.
				## Traits that would avoid or swiftly resolve the issue.
				if = {
					limit = { has_trait = deceitful }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = patient }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = just }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = trusting }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				# Restrictions.
				min = task_contract_weight_employer_associated_traits_min_value
				max = task_contract_weight_employer_associated_traits_max_value
			}
			# Don't weight down barons like we usually would, because we _only_ want barons here.
			# This weight is an override, so make sure it's placed at the bottom.
			add = task_contract_weight_employer_contact_list_value
		}
	}

	# Validity Triggers
	valid_to_create = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		employer_has_treasury_to_offer_job_trigger = yes
		valid_laamp_sensible_start_trigger = yes
		# Want to have characters who have courts (courtiers/guests to use as suitors) so anything above baron
		scope:employer.highest_held_title_tier > tier_barony
	}
	valid_to_accept = {
		# Standard triggers.
		valid_laamp_basic_accept_only_trigger = yes
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		scope:employer = { is_landed = yes }
		is_adult = yes
	}
	valid_to_continue = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		task_contract_employer = { is_landed = yes }
		task_contract_taker = { is_adult = yes }
	}
	valid_to_keep = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		task_contract_employer = { is_landed = yes }
		task_contract_taker = { is_adult = yes }
	}

	# On_actions
	on_accepted = {
		task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_accept_contract" }
		# For the intro event.
		save_scope_as = task_contract
		task_contract_employer = { save_scope_as = employer }
		# Then we roll our first event..
		task_contract_taker ?= { trigger_event = regula_laamp_marriage_dispute_contract.0001 }
	}
	on_create = {
		scope:contract = {
			# Make our gold rewards static.
			grab_gold_fuzz_number_effect = yes
			save_scope_as = task_contract
			set_variable = {
				name = gold_success_standard
				value = task_contract_taker.task_contract_success_gold_gain_half_value
			}
			# Make sure critical values are a bit higher.
			save_scope_value_as = { name = gold_safety_margin value = flag:yes }
			set_variable = {
				name = gold_success_critical
				value = task_contract_taker.task_contract_success_gold_gain_full_value
			}
		}
	}

	# Rewards
	task_contract_reward = {
		success_standard = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				# Camp building extra Stewardship Rewards: +50%.
				if = {
					limit = {
						task_contract_taker.domicile ?= { has_domicile_parameter = camp_improved_stewardship_contract_rewards }
					}
					save_scope_value_as = { name = extra_reward value = flag:yes }
				}
				else = {
					save_scope_value_as = { name = extra_reward value = flag:no }
				}
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_standard
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				task_contract_taker = { task_successful_noncrim_contract_count_effect = yes }
			}
		}
		success_recruitment = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				# Camp building extra Stewardship Rewards: +50%.
				if = {
					limit = {
						task_contract_taker.domicile ?= { has_domicile_parameter = camp_improved_stewardship_contract_rewards }
					}
					save_scope_value_as = { name = extra_reward value = flag:yes }
				}
				else = {
					save_scope_value_as = { name = extra_reward value = flag:no }
				}

				# Due to how scopes work, this actually takes place in the event, blurgh
				show_as_tooltip = {
					if = {
						limit = { exists = scope:bride }
						if = {
							limit = { 
								exists = scope:groom
								scope:bride = {
									exists = betrothed
									betrothed = scope:groom
								}
							}
							scope:bride = { break_betrothal = scope:groom }
						}
						task_contract_taker = {
							add_courtier = scope:bride
						}
					}
					else = {
						custom_description = {
							text = regula_female_follower_joins
						}
					}					
				}


				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = 0
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 50
					PROVISIONS = task_contract_success_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				task_contract_taker = { task_successful_noncrim_contract_count_effect = yes }
			}	
		}
		failure_standard = {
			positive = no
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_failure_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				# Standard Penalties.
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = 0
					PRESTIGE = task_contract_failure_prestige_loss_full_value
					PIETY = 0
					PROVISIONS = task_contract_failure_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_failure_value
					OPINION_TYPE = failed_task_contract_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
			}
		}
	}
}


# Based on laamp_transport_ward, laamp_transport_vip, laamp_transport_explorer
#
# Scenario:
# - A devoted mother wants to send her daughter on a pilgrimage to a holy site.
# - Player have to travel to the mother's court, pick up the daughter, escort her to the nearest Regula Leyline, and then return her to her mother.
#
# Possible forks:
# - The daughter gets charmed before the arrival
# - Player returns with uncharmed daughter
#
# Variables on the contract:
# task_contract_employer - a devoted mother.
# task_contract_object - the daughter.
# task_contract_destination - next point to visit to advance the contract (changes every time the contract is advanced).
# pickup_location - the location where the daughter is picked up (mother's court).
# journey_target_location - the location of the nearest Regula Leyline.
# visited_pickup_location - boolean flag - set when the daughter is picked up.
# visited_journey_target_location - boolean flag - set upon arrival to the Regula Leyline.
#
# Contract is advanced by regula_on_travel_contract_check_arrival effect.
laamp_regula_transport_daughter = {
	group = laamp_contracts_transport_group
	#icon = "gfx/interface/icons/character_interactions/icon_contract_escort.dds"
	icon = "gfx/interface/icons/faith/regula_faith.dds"
	travel = no
	use_diplomatic_range = yes

	# Validity Triggers
	valid_to_create = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		#employer_has_treasury_to_offer_job_trigger = yes
		valid_laamp_sensible_start_trigger = yes
		# Specific flavour triggers.
		scope:employer = {
			is_ai = yes
			#is_adult = yes
			has_trait = devoted_trait_group
			gold > 0
			OR = {
				is_landed = yes
				primary_spouse ?= {
					is_landed = yes
				}
			}

			any_child = {
				regula_valid_daugher_for_educational_travel = yes
				location = scope:employer.location
			}
		}
		magister_alive_trigger = yes
	}
	
	# Conditions for preserving created but not taken contract
	valid_to_keep = {
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		OR = {
			# Mother must remain in power
			task_contract_employer = { is_landed = yes }
			task_contract_employer.primary_spouse ?= { is_landed = yes }
		}

		task_contract_taker = { is_adult = yes }

		var:task_contract_object = {
			is_alive = yes
			age <= 30

			# While it would be logical to discard the contract as soon as daughter becomes devoted,
			# it would be a bit harsh to do so. We do not want to penalize the player for charming the daughter early.
			# is_regula_devoted_trigger = no 

			# "The princes" should remain in same "castle"
			location = root.var:pickup_location
		}
	}

	valid_to_accept = {
		# Standard triggers.
		valid_laamp_basic_accept_only_trigger = yes
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		has_trait = magister_trait_group
	}
	valid_to_continue = {
		var:task_contract_object ?= {
			is_alive = yes
		}
		var:task_contract_employer ?= {
			is_alive = yes
		}
	}

	weight = {
		# Standard weights.
		value = task_contract_weight_interesting_very_rare_value
		add = laamp_contracts_weight_up_transport_value
		add = task_contract_weight_employer_contact_list_value
		add = laamp_contracts_weight_up_regula_value
		scope:employer = { multiply = task_contract_weight_by_tier_value }
	}

	# On_Actions
	on_create = {
		scope:contract = {
			save_scope_as = task_contract

			task_contract_employer = {
				if = {
					limit = {
						NOT = { exists = scope:task_contract.var:task_contract_object }
					}
					random_child = {
						limit = {
							regula_valid_daugher_for_educational_travel = yes
							location = scope:task_contract.task_contract_employer.location
						}
						save_scope_as = task_contract_object
					}
				}
			}
			set_variable = {
				name = task_contract_object
				value = scope:task_contract_object
			}

			scope:task_contract_object = {
				location = {
					save_scope_as = pickup_location
				}
			}


			# Find all regula Leylines
			religion:regula_religion = {
				every_faith = {
					every_holy_site = {
						title_province = {
							if = {
								# Exclude provinces that are too close to the pickup_location 
								limit = {
									squared_distance = {
										target = scope:pickup_location
										value >= 10000
									}
								}

								add_to_temporary_list = layline_provinces
							}
						}
					}
				}
			}


			# Find nearest location 
			ordered_in_list = {
				list = layline_provinces
				order_by = {
					value = "squared_distance(scope:pickup_location)"
					multiply = -1
				}
				max = 1
				save_scope_as = journey_target_location
			}

			# Save scopes into contract
			set_variable = {
				name = pickup_location
				value = scope:pickup_location
			}
			set_variable = {
				name = task_contract_destination
				value = scope:pickup_location
			}
			set_variable = {
				name = journey_target_location
				value = scope:journey_target_location
			}
			set_variable = {
				name = task_contract_object
				value = scope:task_contract_object
			}

			# Make our gold rewards static.
			grab_gold_fuzz_number_effect = yes
			set_variable = {
				name = gold_success_critical
				value = task_contract_success_gold_gain_travel_value
			}
			set_variable = {
				name = gold_success_standard
				value = task_contract_success_gold_gain_half_travel_value
			}
			set_variable = {
				name = gold_success_qualified
				value = {
					value = task_contract_success_gold_gain_travel_value
					multiply = 0.10
				}
			}
		}
	}

	on_accepted = {
		task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_accept_contract" }
		save_scope_as = task_contract
		task_contract_taker = {
			trigger_event = regula_travel_contract_event.0020
		}
	}
	on_invalidated = {
		if = {
			limit = {
				root.task_contract_taker = {
					any_character_active_contract = {
						this = root
					}
				}
			}
			task_contract_employer = {
				save_scope_as = task_contract_employer
			}
			save_scope_value_as = {
				name = task_contract_tier
				value = root.task_contract_tier
			}
			var:task_contract_destination = {
				save_scope_as = task_contract_destination
			}
			var:task_contract_object = {
				save_scope_as = task_contract_object
			}

			task_contract_taker = {
				set_variable = {
					name = invalidated_task_contract_type
					value = flag:laamp_transport_ward
				}
			}
			if = {
				limit = {
					var:task_contract_object ?= {
						is_alive = no
					}
				}
				task_contract_taker = {
					save_scope_value_as = {
						name = task_contract_invalidation_reason
						value = flag:escortee_dead
					}
					trigger_event = regula_travel_contract_event.0029
				}
			}
			else_if = {
				limit = {
					task_contract_employer = {
						is_alive = no
					}
				}
				task_contract_employer.primary_title.holder ?= {
					save_scope_as = employer_heir
				}
				task_contract_taker = {
					save_scope_value_as = {
						name = task_contract_invalidation_reason
						value = flag:employer_dead
					}
					trigger_event = regula_travel_contract_event.0029

				}
			}
			#remove the ward/guardian variables
			if = {
				limit = {
					var:task_contract_object ?= {
						is_alive = yes
					}
				}
				var:task_contract_object = {
					return_to_court = yes
				}
			}
		}
	}

	# Rewards
	task_contract_reward = {
		success_critical = {

			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_critical
					PRESTIGE = task_contract_success_prestige_gain_full_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_major_value
					OPINION = task_contract_opinion_excelled_reward_value
					OPINION_TYPE = critically_succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = yes
					EXTRA_REWARD = scope:extra_reward
				}
			}
		}
		success_standard = {
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_standard
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_medium_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = yes
					EXTRA_REWARD = scope:extra_reward
				}
			}
		}
		success_qualified = {
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_qualified
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_medium_value
					OPINION = 0
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
			}
		}
		failure_standard = {
			positive = no
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_failure_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = 0
					PRESTIGE = task_contract_failure_prestige_loss_full_value
					PIETY = 0
					PROVISIONS = task_contract_failure_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_failure_value
					OPINION_TYPE = failed_task_contract_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
			}
		}
	}
}