﻿on_imprison = {
	effect = { # You no longer make such decisions over your life :(
		# EP2: Cancel associated activity and travel plan if they exist
		sell_property_effect = yes
		quit_job_effect = yes
		if = {
			limit = {
				exists = involved_activity
				involved_activity.activity_host = root
			}
			invalidate_activity = root.involved_activity
		}
		else_if = {
			limit = {
				exists = involved_activity
			}
			remove_from_activity = involved_activity
		}

		current_travel_plan ?= {
			if = {
				limit = {
					travel_plan_owner = root
				}
				abort_travel_plan = yes
			}
			else = {
				remove_character = root
		 	}
		}

		if = {
			limit = { has_character_modifier = gaining_weight_modifier }
			remove_character_modifier = gaining_weight_modifier
		}
		if = {
			limit = { has_character_modifier = losing_weight_modifier }
			remove_character_modifier = losing_weight_modifier
		}
		if = { # from event hold_court.6140
			limit = {
				scope:imprisoner = {
					has_character_modifier = 6140_pet_modifier
					OR = {
						has_trait = torturer
						has_trait = callous
						has_trait = sadistic
					}
				}
			}
			add_character_modifier = 6140_animal_guardian_modifier
		}

		# Struggle Catalysts
		if = {
			limit = {
				is_important_or_vip_struggle_character = yes
				any_character_struggle = {
					involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_imprison_important
						CHAR = scope:imprisoner
					}
				}
			}
			every_character_struggle = {
				involvement = involved
				limit = {
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_imprison_important
						CHAR = scope:imprisoner
					}
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_imprison_important
					character = scope:imprisoner
				}
			}
		}
		if = {
			limit = {
				has_trait = fp3_struggle_supporter
				any_character_struggle = {
					involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_imprison_supporter
						CHAR = scope:imprisoner
					}
				}
			}
			every_character_struggle = {
				involvement = involved
				limit = {
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_imprison_supporter
						CHAR = scope:imprisoner
					}
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_imprison_supporter
					character = scope:imprisoner
				}
				log_debug_variable_for_persian_struggle_effect = { VAR = unrest_catalyst_imprison_supporter }
			}
		}
		if = {
			limit = {
				has_trait = fp3_struggle_detractor
				any_character_struggle = {
					involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_imprison_detractor
						CHAR = scope:imprisoner
					}
				}
			}
			every_character_struggle = {
				involvement = involved
				limit = {
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_imprison_detractor
						CHAR = scope:imprisoner
					}
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_imprison_detractor
					character = scope:imprisoner
				}
				log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_imprison_detractor }
			}
		}
		
		# Memory system
		create_character_memory = {
			type = imprisoned
			participants = { imprisoner = scope:imprisoner }
		}
		if = {
			limit = {
				is_adult = no
			}
			scope:new_memory = {
				set_variable = childhood_memory
			}
		}

		if = {
			limit = {
				has_bp2_dlc_trigger = yes
				is_adult = yes
				any_memory = {
					has_memory_type = imprisoned
					has_variable = childhood_memory
				}
			}
			trigger_event = {
				id = bp2_yearly.4031
				days = 10
			}
		} #You were imprisoned as a child, and guess what? You are imprisoned as an adult. Congratulations!

		if = {
			limit = {
				OR = {
					is_of_major_interest_trigger = { CHARACTER = scope:imprisoner }
					AND = {
						exists = scope:imprisoner.primary_title.tier
						highest_held_title_tier >= scope:imprisoner.primary_title.tier
					}
					highest_held_title_tier >= tier_kingdom
				}
			}
			root = { save_scope_as = prisoner_memory }
			scope:imprisoner = { # We must buffer this memory via events, otherwise the imprisonment shows up before tyranny/faction wars end
				trigger_event = error_suppression.0003
			}
		}
		if = { # Set up so you can't banish realm priests, and so that they transfer their gold to the next realm priest if possible
			limit = {
				is_theocratic_lessee = yes
			}
			add_character_flag = realm_priest
			save_scope_as = realm_priest_transfer
			if = {
				limit = {
					exists = liege
				}
				liege = {
					trigger_event = {
						days = 2 # Queued, so we can check the pulse in parallel
						id = prison.8000
					}
				}
			}
		}
		if = {
			limit = {
				has_bp2_dlc_trigger = yes
				scope:imprisoner = {
					any_memory = {
						has_memory_type = relative_died
						any_memory_participant = {
							exists = this.killer
							killer = {
								is_imprisoned_by = root
							}
						}
						has_variable = childhood_memory
					}
				}
			}
			scope:imprisoner = {
				trigger_event = {
					id = bp2_yearly.4001
					days = 1
				}
			}
		}
	}
	events = {
	}
}