﻿# -----------------------------------------------------------------------------
# Proposal state
# -----------------------------------------------------------------------------

pde_clear_treaty_state_effect = {
	remove_variable = pde_treaty_war
	remove_variable = pde_treaty_opponent
	remove_variable = pde_offer_gold
	remove_variable = pde_demand_gold
	remove_variable = pde_offer_herd
	remove_variable = pde_demand_herd
	remove_variable = pde_offer_tribute
	remove_variable = pde_demand_tribute
	remove_variable = pde_offer_suzerainty
	remove_variable = pde_demand_suzerainty
	remove_variable = pde_offer_claims
	remove_variable = pde_demand_claims
	remove_variable = pde_offer_hostage
	remove_variable = pde_demand_hostage
	remove_variable = pde_offer_break_alliances
	remove_variable = pde_demand_break_alliances
	remove_variable = pde_offer_humiliation
	remove_variable = pde_demand_humiliation
	remove_variable = pde_offer_prisoner_release
	remove_variable = pde_demand_prisoner_release
	remove_variable = pde_offer_conversion
	remove_variable = pde_demand_conversion
	remove_variable = pde_offer_disarmament
	remove_variable = pde_demand_disarmament
	remove_variable = pde_forced_alliance
	remove_variable = pde_long_truce
	remove_variable = pde_extended_truce
	remove_variable = pde_treaty_locked
	remove_variable = pde_treaty_resolution_in_progress
	remove_variable = pde_land_selection_mode
	if = {
		limit = { has_variable_list = pde_offer_candidates }
		clear_variable_list = pde_offer_candidates
	}
	if = {
		limit = { has_variable_list = pde_demand_candidates }
		clear_variable_list = pde_demand_candidates
	}
	if = {
		limit = { has_variable_list = pde_offer_counties }
		clear_variable_list = pde_offer_counties
	}
	if = {
		limit = { has_variable_list = pde_demand_counties }
		clear_variable_list = pde_demand_counties
	}
}

# Remove an offer pointer whose proposer, opponent, or war no longer matches.
# ROOT is the recipient of the pending offer.
pde_clear_stale_pending_treaty_effect = {
	if = {
		limit = {
			has_variable = pde_pending_treaty_proposer
			NOT = {
				var:pde_pending_treaty_proposer = {
					is_alive = yes
					exists = var:pde_treaty_war
					exists = var:pde_treaty_opponent
					var:pde_treaty_opponent = root
					var:pde_treaty_war = {
						OR = {
							AND = { primary_attacker = prev primary_defender = root }
							AND = { primary_defender = prev primary_attacker = root }
						}
					}
					trigger_if = {
						limit = { is_ai = yes }
						var:pde_treaty_war = { has_variable = pde_ai_offer_pending }
					}
				}
			}
		}
		remove_variable = pde_pending_treaty_proposer
	}
}

# Safe to call whenever a character enters a new war or opens the treaty tab.
pde_clear_stale_treaty_state_effect = {
	pde_clear_stale_pending_treaty_effect = yes
	if = {
		limit = {
			has_variable = pde_treaty_war
			NOT = { exists = var:pde_treaty_war }
		}
		pde_clear_treaty_state_effect = yes
	}
}

# ROOT is one of the two primary leaders and scope:war is ending this tick.
pde_cleanup_ended_war_treaty_effect = {
	if = {
		limit = {
			has_variable = pde_pending_treaty_proposer
			var:pde_pending_treaty_proposer = {
				exists = var:pde_treaty_war
				var:pde_treaty_war = scope:war
			}
		}
		remove_variable = pde_pending_treaty_proposer
	}
	if = {
		limit = {
			has_variable = pde_treaty_war
			var:pde_treaty_war = scope:war
			NOT = { has_variable = pde_treaty_resolution_in_progress }
		}
		scope:war = { remove_variable = pde_ai_offer_pending }
		pde_clear_treaty_state_effect = yes
	}
}

# Called with ROOT = player and scope:war supplied by the war overview GUI.
pde_open_treaty_effect = {
	# Clear weak references and rejected/invalid offers left by an older war.
	pde_clear_stale_treaty_state_effect = yes
	if = {
		limit = { NOT = { exists = var:pde_treaty_war } }
		pde_clear_treaty_state_effect = yes
	}
	else_if = {
		limit = { var:pde_treaty_war != scope:war }
		pde_clear_treaty_state_effect = yes
	}

	set_variable = { name = pde_treaty_war value = scope:war }
	scope:war = {
		if = {
			limit = { primary_attacker = root }
			primary_defender = { save_scope_as = pde_opening_opponent }
		}
		else_if = {
			limit = { primary_defender = root }
			primary_attacker = { save_scope_as = pde_opening_opponent }
		}
	}
	if = {
		limit = { exists = scope:pde_opening_opponent }
		set_variable = {
			name = pde_treaty_opponent
			value = scope:pde_opening_opponent
		}
	}
	if = { limit = { NOT = { exists = var:pde_offer_gold } } set_variable = { name = pde_offer_gold value = 0 } }
	if = { limit = { NOT = { exists = var:pde_demand_gold } } set_variable = { name = pde_demand_gold value = 0 } }
	if = { limit = { NOT = { exists = var:pde_offer_herd } } set_variable = { name = pde_offer_herd value = 0 } }
	if = { limit = { NOT = { exists = var:pde_demand_herd } } set_variable = { name = pde_demand_herd value = 0 } }

	# Candidate lists are generated in localized alphabetical order. Each
	# supported total conversion overrides only the generated title sequence.
	if = {
		limit = { has_variable_list = pde_offer_candidates }
		clear_variable_list = pde_offer_candidates
	}
	if = {
		limit = { has_variable_list = pde_demand_candidates }
		clear_variable_list = pde_demand_candidates
	}
	pde_populate_alphabetical_treaty_candidates_effect = yes
}

# -----------------------------------------------------------------------------
# Gold controls. Values are clamped after every change.
# -----------------------------------------------------------------------------

pde_clamp_offer_gold_effect = {
	if = { limit = { var:pde_offer_gold < 0 } set_variable = { name = pde_offer_gold value = 0 } }
	if = { limit = { var:pde_offer_gold > pde_max_offer_gold_value } set_variable = { name = pde_offer_gold value = pde_max_offer_gold_value } }
}

pde_clamp_demand_gold_effect = {
	if = { limit = { var:pde_demand_gold < 0 } set_variable = { name = pde_demand_gold value = 0 } }
	if = { limit = { var:pde_demand_gold > pde_max_demand_gold_value } set_variable = { name = pde_demand_gold value = pde_max_demand_gold_value } }
}

pde_offer_gold_add_1_effect = { change_variable = { name = pde_offer_gold add = 1 } pde_clamp_offer_gold_effect = yes }
pde_offer_gold_add_10_effect = { change_variable = { name = pde_offer_gold add = 10 } pde_clamp_offer_gold_effect = yes }
pde_offer_gold_add_100_effect = { change_variable = { name = pde_offer_gold add = 100 } pde_clamp_offer_gold_effect = yes }
pde_offer_gold_subtract_1_effect = { change_variable = { name = pde_offer_gold subtract = 1 } pde_clamp_offer_gold_effect = yes }
pde_offer_gold_subtract_10_effect = { change_variable = { name = pde_offer_gold subtract = 10 } pde_clamp_offer_gold_effect = yes }
pde_offer_gold_subtract_100_effect = { change_variable = { name = pde_offer_gold subtract = 100 } pde_clamp_offer_gold_effect = yes }
pde_offer_gold_max_effect = { set_variable = { name = pde_offer_gold value = pde_max_offer_gold_value } }
pde_offer_gold_clear_effect = { set_variable = { name = pde_offer_gold value = 0 } }

pde_demand_gold_add_1_effect = { change_variable = { name = pde_demand_gold add = 1 } pde_clamp_demand_gold_effect = yes }
pde_demand_gold_add_10_effect = { change_variable = { name = pde_demand_gold add = 10 } pde_clamp_demand_gold_effect = yes }
pde_demand_gold_add_100_effect = { change_variable = { name = pde_demand_gold add = 100 } pde_clamp_demand_gold_effect = yes }
pde_demand_gold_subtract_1_effect = { change_variable = { name = pde_demand_gold subtract = 1 } pde_clamp_demand_gold_effect = yes }
pde_demand_gold_subtract_10_effect = { change_variable = { name = pde_demand_gold subtract = 10 } pde_clamp_demand_gold_effect = yes }
pde_demand_gold_subtract_100_effect = { change_variable = { name = pde_demand_gold subtract = 100 } pde_clamp_demand_gold_effect = yes }
pde_demand_gold_max_effect = { set_variable = { name = pde_demand_gold value = pde_max_demand_gold_value } }
pde_demand_gold_clear_effect = { set_variable = { name = pde_demand_gold value = 0 } }

# -----------------------------------------------------------------------------
# Herd controls. Only nomadic rulers may exchange herd, and herd cannot be owed.
# -----------------------------------------------------------------------------

pde_clamp_offer_herd_effect = {
	if = { limit = { var:pde_offer_herd < 0 } set_variable = { name = pde_offer_herd value = 0 } }
	if = { limit = { var:pde_offer_herd > pde_max_offer_herd_value } set_variable = { name = pde_offer_herd value = pde_max_offer_herd_value } }
}

pde_clamp_demand_herd_effect = {
	if = { limit = { var:pde_demand_herd < 0 } set_variable = { name = pde_demand_herd value = 0 } }
	if = { limit = { var:pde_demand_herd > pde_max_demand_herd_value } set_variable = { name = pde_demand_herd value = pde_max_demand_herd_value } }
}

pde_offer_herd_add_10_effect = { change_variable = { name = pde_offer_herd add = 10 } pde_clamp_offer_herd_effect = yes }
pde_offer_herd_add_100_effect = { change_variable = { name = pde_offer_herd add = 100 } pde_clamp_offer_herd_effect = yes }
pde_offer_herd_subtract_10_effect = { change_variable = { name = pde_offer_herd subtract = 10 } pde_clamp_offer_herd_effect = yes }
pde_offer_herd_subtract_100_effect = { change_variable = { name = pde_offer_herd subtract = 100 } pde_clamp_offer_herd_effect = yes }
pde_offer_herd_max_effect = { set_variable = { name = pde_offer_herd value = pde_max_offer_herd_value } }
pde_offer_herd_clear_effect = { set_variable = { name = pde_offer_herd value = 0 } }
pde_toggle_offer_herd_effect = {
	if = { limit = { var:pde_offer_herd > 0 } pde_offer_herd_clear_effect = yes }
	else = { set_variable = { name = pde_offer_herd value = pde_default_offer_herd_value } pde_clamp_offer_herd_effect = yes }
}

pde_demand_herd_add_10_effect = { change_variable = { name = pde_demand_herd add = 10 } pde_clamp_demand_herd_effect = yes }
pde_demand_herd_add_100_effect = { change_variable = { name = pde_demand_herd add = 100 } pde_clamp_demand_herd_effect = yes }
pde_demand_herd_subtract_10_effect = { change_variable = { name = pde_demand_herd subtract = 10 } pde_clamp_demand_herd_effect = yes }
pde_demand_herd_subtract_100_effect = { change_variable = { name = pde_demand_herd subtract = 100 } pde_clamp_demand_herd_effect = yes }
pde_demand_herd_max_effect = { set_variable = { name = pde_demand_herd value = pde_max_demand_herd_value } }
pde_demand_herd_clear_effect = { set_variable = { name = pde_demand_herd value = 0 } }
pde_toggle_demand_herd_effect = {
	if = { limit = { var:pde_demand_herd > 0 } pde_demand_herd_clear_effect = yes }
	else = { set_variable = { name = pde_demand_herd value = pde_default_demand_herd_value } pde_clamp_demand_herd_effect = yes }
}
# -----------------------------------------------------------------------------
# Clause toggles
# -----------------------------------------------------------------------------

pde_toggle_offer_tribute_effect = {
	if = { limit = { has_variable = pde_offer_tribute } remove_variable = pde_offer_tribute }
	else = { set_variable = { name = pde_offer_tribute value = 1 } remove_variable = pde_offer_suzerainty }
}
pde_toggle_demand_tribute_effect = {
	if = { limit = { has_variable = pde_demand_tribute } remove_variable = pde_demand_tribute }
	else = { set_variable = { name = pde_demand_tribute value = 1 } remove_variable = pde_demand_suzerainty }
}
pde_toggle_offer_suzerainty_effect = {
	if = { limit = { has_variable = pde_offer_suzerainty } remove_variable = pde_offer_suzerainty }
	else = { set_variable = { name = pde_offer_suzerainty value = 1 } remove_variable = pde_offer_tribute remove_variable = pde_demand_suzerainty }
}
pde_toggle_demand_suzerainty_effect = {
	if = { limit = { has_variable = pde_demand_suzerainty } remove_variable = pde_demand_suzerainty }
	else = { set_variable = { name = pde_demand_suzerainty value = 1 } remove_variable = pde_demand_tribute remove_variable = pde_offer_suzerainty }
}

pde_toggle_offer_claims_effect = { if = { limit = { has_variable = pde_offer_claims } remove_variable = pde_offer_claims } else = { set_variable = { name = pde_offer_claims value = 1 } } }
pde_toggle_demand_claims_effect = { if = { limit = { has_variable = pde_demand_claims } remove_variable = pde_demand_claims } else = { set_variable = { name = pde_demand_claims value = 1 } } }
pde_toggle_offer_hostage_effect = { if = { limit = { has_variable = pde_offer_hostage } remove_variable = pde_offer_hostage } else = { set_variable = { name = pde_offer_hostage value = 1 } } }
pde_toggle_demand_hostage_effect = { if = { limit = { has_variable = pde_demand_hostage } remove_variable = pde_demand_hostage } else = { set_variable = { name = pde_demand_hostage value = 1 } } }
pde_toggle_offer_break_alliances_effect = {
	if = { limit = { has_variable = pde_offer_break_alliances } remove_variable = pde_offer_break_alliances }
	else = {
		set_variable = { name = pde_offer_break_alliances value = 1 }
		remove_variable = pde_forced_alliance
	}
}
pde_toggle_demand_break_alliances_effect = {
	if = { limit = { has_variable = pde_demand_break_alliances } remove_variable = pde_demand_break_alliances }
	else = {
		set_variable = { name = pde_demand_break_alliances value = 1 }
		remove_variable = pde_forced_alliance
	}
}
pde_toggle_offer_humiliation_effect = { if = { limit = { has_variable = pde_offer_humiliation } remove_variable = pde_offer_humiliation } else = { set_variable = { name = pde_offer_humiliation value = 1 } } }
pde_toggle_demand_humiliation_effect = { if = { limit = { has_variable = pde_demand_humiliation } remove_variable = pde_demand_humiliation } else = { set_variable = { name = pde_demand_humiliation value = 1 } } }
pde_toggle_offer_prisoner_release_effect = { if = { limit = { has_variable = pde_offer_prisoner_release } remove_variable = pde_offer_prisoner_release } else = { set_variable = { name = pde_offer_prisoner_release value = 1 } } }
pde_toggle_demand_prisoner_release_effect = { if = { limit = { has_variable = pde_demand_prisoner_release } remove_variable = pde_demand_prisoner_release } else = { set_variable = { name = pde_demand_prisoner_release value = 1 } } }
pde_toggle_offer_conversion_effect = {
	if = { limit = { has_variable = pde_offer_conversion } remove_variable = pde_offer_conversion }
	else = {
		set_variable = { name = pde_offer_conversion value = 1 }
		remove_variable = pde_demand_conversion
	}
}
pde_toggle_demand_conversion_effect = {
	if = { limit = { has_variable = pde_demand_conversion } remove_variable = pde_demand_conversion }
	else = {
		set_variable = { name = pde_demand_conversion value = 1 }
		remove_variable = pde_offer_conversion
	}
}
pde_toggle_offer_disarmament_effect = { if = { limit = { has_variable = pde_offer_disarmament } remove_variable = pde_offer_disarmament } else = { set_variable = { name = pde_offer_disarmament value = 1 } } }
pde_toggle_demand_disarmament_effect = { if = { limit = { has_variable = pde_demand_disarmament } remove_variable = pde_demand_disarmament } else = { set_variable = { name = pde_demand_disarmament value = 1 } } }
pde_toggle_forced_alliance_effect = {
	if = { limit = { has_variable = pde_forced_alliance } remove_variable = pde_forced_alliance }
	else = {
		set_variable = { name = pde_forced_alliance value = 1 }
		remove_variable = pde_offer_break_alliances
		remove_variable = pde_demand_break_alliances
	}
}
pde_toggle_long_truce_effect = {
	if = { limit = { has_variable = pde_long_truce } remove_variable = pde_long_truce }
	else = {
		set_variable = { name = pde_long_truce value = 1 }
		remove_variable = pde_extended_truce
	}
}
pde_toggle_extended_truce_effect = {
	if = { limit = { has_variable = pde_extended_truce } remove_variable = pde_extended_truce }
	else = {
		set_variable = { name = pde_extended_truce value = 1 }
		remove_variable = pde_long_truce
	}
}

pde_clear_offer_counties_effect = { if = { limit = { has_variable_list = pde_offer_counties } clear_variable_list = pde_offer_counties } }
pde_clear_demand_counties_effect = { if = { limit = { has_variable_list = pde_demand_counties } clear_variable_list = pde_demand_counties } }

# -----------------------------------------------------------------------------
# Treaty execution
# -----------------------------------------------------------------------------

pde_start_money_tribute_effect = {
	set_variable = { name = pde_tribute_receiver value = scope:pde_tribute_receiver }
	set_variable = { name = pde_tribute_years value = 10 }
	set_variable = { name = pde_tribute_annual_amount value = pde_actor_annual_tribute_value }
	add_character_modifier = { modifier = pde_paying_tribute_modifier years = 10 }
	trigger_event = { id = peace_deals_expanded.0100 years = 1 }
}

pde_end_money_tribute_effect = {
	remove_character_modifier = pde_paying_tribute_modifier
	remove_variable = pde_tribute_receiver
	remove_variable = pde_tribute_years
	remove_variable = pde_tribute_annual_amount
}

pde_apply_humiliation_effect = {
	save_scope_as = pde_humiliated_ruler
	add_prestige = -250
	if = { limit = { has_legitimacy = yes } add_legitimacy = -100 }
	add_stress = 40
	add_character_modifier = { modifier = pde_humiliated_modifier years = 5 }
	every_vassal = {
		add_opinion = { target = scope:pde_humiliated_ruler modifier = pde_humiliating_peace_opinion }
	}
}

# End the war according to the net value of the negotiated clauses. Engine-level
# victory also enforces the original casus belli; near-balanced treaties remain
# white peace. ROOT is the proposer.
pde_end_negotiated_war_effect = {
	# End-of-war on_actions must not erase these clauses before they are applied.
	set_variable = { name = pde_treaty_resolution_in_progress value = 1 }
	if = {
		limit = { pde_treaty_net_value >= 3 }
		var:pde_treaty_war = {
			if = { limit = { primary_attacker = root } end_war = attacker }
			else = { end_war = defender }
		}
	}
	else_if = {
		limit = { pde_treaty_net_value <= -3 }
		var:pde_treaty_war = {
			if = { limit = { primary_attacker = root } end_war = defender }
			else = { end_war = attacker }
		}
	}
	else = {
		var:pde_treaty_war = { end_war = white_peace }
	}
}
# ROOT = proposer. The opposing war leader and every selected clause are read
# from ROOT's variables. The war is ended before this effect is called.
pde_apply_negotiated_treaty_effect = {
	save_scope_as = pde_actor
	var:pde_treaty_opponent = { save_scope_as = pde_opponent }

	# Territorial transfers in both directions.
	if = {
		limit = { has_variable_list = pde_demand_counties }
		create_title_and_vassal_change = { type = conquest save_scope_as = pde_demand_title_change add_claim_on_loss = yes }
		every_in_list = {
			variable = pde_demand_counties
			change_title_holder = { holder = scope:pde_actor change = scope:pde_demand_title_change }
		}
		resolve_title_and_vassal_change = scope:pde_demand_title_change
	}
	if = {
		limit = { has_variable_list = pde_offer_counties }
		create_title_and_vassal_change = { type = conquest save_scope_as = pde_offer_title_change add_claim_on_loss = yes }
		every_in_list = {
			variable = pde_offer_counties
			change_title_holder = { holder = scope:pde_opponent change = scope:pde_offer_title_change }
		}
		resolve_title_and_vassal_change = scope:pde_offer_title_change
	}

	# Immediate money transfers. pay_short_term_gold permits the negotiated debt.
	if = {
		limit = { var:pde_offer_gold > 0 }
		pay_short_term_gold = { target = scope:pde_opponent gold = var:pde_offer_gold }
	}
	if = {
		limit = { var:pde_demand_gold > 0 }
		scope:pde_opponent = { pay_short_term_gold = { target = scope:pde_actor gold = root.var:pde_demand_gold } }
	}

	# Immediate herd transfer. Both rulers must still be nomadic at signature.
	if = {
		limit = {
			var:pde_offer_herd > 0
			pde_nomadic_herd_exchange_available_trigger = yes
		}
		pay_herd = { target = scope:pde_opponent value = var:pde_offer_herd }
	}
	if = {
		limit = {
			var:pde_demand_herd > 0
			pde_nomadic_herd_exchange_available_trigger = yes
		}
		scope:pde_opponent = { pay_herd = { target = scope:pde_actor value = root.var:pde_demand_herd } }
	}
	# Fixed annual cash tribute for ten years.
	if = {
		limit = { has_variable = pde_offer_tribute }
		save_scope_as = pde_tribute_payer
		scope:pde_opponent = { save_scope_as = pde_tribute_receiver }
		scope:pde_tribute_payer = { pde_start_money_tribute_effect = yes }
	}
	if = {
		limit = { has_variable = pde_demand_tribute }
		scope:pde_opponent = { save_scope_as = pde_tribute_payer }
		save_scope_as = pde_tribute_receiver
		scope:pde_tribute_payer = { pde_start_money_tribute_effect = yes }
	}

	# Tributary contracts use the game's subjugated-tributary relationship.
	if = {
		limit = { has_variable = pde_offer_suzerainty pde_can_become_tributary_of_opponent_trigger = yes }
		save_scope_value_as = { name = high_obligations value = yes }
		start_tributary_interaction_effect = { TRIBUTARY = scope:pde_actor SUZERAIN = scope:pde_opponent }
	}
	if = {
		limit = { has_variable = pde_demand_suzerainty pde_opponent_can_become_tributary_trigger = yes }
		save_scope_value_as = { name = high_obligations value = yes }
		start_tributary_interaction_effect = { TRIBUTARY = scope:pde_opponent SUZERAIN = scope:pde_actor }
	}

	# Claim renunciation concerns all explicit claims against the other realm.
	if = {
		limit = { has_variable = pde_offer_claims }
		every_claim = {
			explicit = yes
			limit = { holder = { top_liege = scope:pde_opponent } }
			scope:pde_actor = { remove_claim = prev }
		}
	}
	if = {
		limit = { has_variable = pde_demand_claims }
		scope:pde_opponent = {
			every_claim = {
				explicit = yes
				limit = { holder = { top_liege = scope:pde_actor } }
				scope:pde_opponent = { remove_claim = prev }
			}
		}
	}

	# One eligible close relative is selected from the side providing the hostage.
	if = {
		limit = { has_variable = pde_offer_hostage }
		random_close_family_member = {
			limit = { bp2_valid_to_offer_as_hostage_trigger = { SENDER = scope:pde_actor } }
			save_scope_as = pde_selected_hostage
		}
		if = { limit = { exists = scope:pde_selected_hostage } scope:pde_opponent = { take_hostage = scope:pde_selected_hostage } }
	}
	if = {
		limit = { has_variable = pde_demand_hostage }
		scope:pde_opponent = {
			random_close_family_member = {
				limit = { bp2_valid_to_offer_as_hostage_trigger = { SENDER = scope:pde_opponent } }
				save_scope_as = pde_selected_hostage
			}
		}
		if = { limit = { exists = scope:pde_selected_hostage } scope:pde_actor = { take_hostage = scope:pde_selected_hostage } }
	}

	if = { limit = { has_variable = pde_offer_break_alliances } every_ally = { break_alliance = scope:pde_actor } }
	if = { limit = { has_variable = pde_demand_break_alliances } scope:pde_opponent = { every_ally = { break_alliance = scope:pde_opponent } } }

	if = { limit = { has_variable = pde_offer_humiliation } pde_apply_humiliation_effect = yes scope:pde_opponent = { add_prestige = 100 } }
	if = { limit = { has_variable = pde_demand_humiliation } scope:pde_opponent = { pde_apply_humiliation_effect = yes } add_prestige = 100 }

	# Close relatives held by the conceding side are returned without ransom.
	if = {
		limit = { has_variable = pde_offer_prisoner_release }
		every_prisoner = {
			limit = { is_close_family_of = scope:pde_opponent }
			release_from_prison = yes
		}
	}
	if = {
		limit = { has_variable = pde_demand_prisoner_release }
		scope:pde_opponent = {
			every_prisoner = {
				limit = { is_close_family_of = scope:pde_actor }
				release_from_prison = yes
			}
		}
	}

	# A ruler may accept the other ruler's faith as a personal peace term.
	if = {
		limit = { has_variable = pde_offer_conversion }
		set_character_faith = scope:pde_opponent.faith
	}
	if = {
		limit = { has_variable = pde_demand_conversion }
		scope:pde_opponent = { set_character_faith = scope:pde_actor.faith }
	}

	# Military restrictions are represented by a ten-year realm-wide modifier.
	if = {
		limit = { has_variable = pde_offer_disarmament }
		add_character_modifier = { modifier = pde_disarmed_by_treaty_modifier years = 10 }
	}
	if = {
		limit = { has_variable = pde_demand_disarmament }
		scope:pde_opponent = { add_character_modifier = { modifier = pde_disarmed_by_treaty_modifier years = 10 } }
	}

	# War cleanup can invalidate an alliance created on the same day. Formation is
	# therefore delayed by one day; the ten-year timer starts when it truly exists.
	if = {
		limit = {
			has_variable = pde_forced_alliance
			NOT = { is_allied_to = scope:pde_opponent }
		}
		trigger_event = { id = peace_deals_expanded.0301 days = 1 }
	}

	if = {
		limit = { has_variable = pde_long_truce }
		add_truce_both_ways = { character = scope:pde_opponent years = 10 name = pde_long_truce }
	}
	if = {
		limit = { has_variable = pde_extended_truce }
		add_truce_both_ways = { character = scope:pde_opponent years = 20 name = pde_extended_truce }
	}

	# Both sides remember coercive clauses; the opinion effect is directional.
	if = {
		limit = { pde_demand_cost_value > 0 }
		scope:pde_opponent = { add_opinion = { target = scope:pde_actor modifier = pde_treaty_grudge_opinion } }
	}
	if = {
		limit = { pde_offer_cost_value > 0 }
		add_opinion = { target = scope:pde_opponent modifier = pde_treaty_grudge_opinion }
	}

	pde_clear_treaty_state_effect = yes
}
