﻿#On actions directing prison-related events
#Note that prison stress events are handled through the Stress on-actions.


# Code on-action: character being imprisoned in root scope
# imprisoning character set as scope:imprisoner
on_imprison = {
	effect = { # You no longer make such decisions over your life :(
		# EP2: Cancel associated activity and travel plan if they exist
		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_flag = travel_option_added_character
			}
			remove_character_flag = travel_option_added_character
		}

		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 = {
					activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
						CATALYST = catalyst_imprison_supporter
						CHAR = scope:imprisoner
					}
				}
			}
			every_character_struggle = {
				limit = {
					activate_struggle_catalyst_secondary_character_involvement_either_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 = {
					activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
						CATALYST = catalyst_imprison_detractor
						CHAR = scope:imprisoner
					}
				}
			}
			every_character_struggle = {
				limit = {
					activate_struggle_catalyst_secondary_character_involvement_either_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 }
			}
		}
		if = {
			limit = {
				has_pam_dlc_trigger = yes
				any_held_title = { is_head_of_faith = yes }
				scope:imprisoner = {
					religion = religion:christianity_religion
					any_character_situation = {
						situation_type = the_christian_church
						situation_top_has_catalyst = catalyst_the_christian_church_ruler_imprisons_head_of_faith
					}
				}
			}
			situation:the_christian_church = {
				trigger_situation_catalyst = catalyst_the_christian_church_ruler_imprisons_head_of_faith
			}
		}
		#Guelphs & Ghibellines
		if = {
			limit = {
				OR = {
					is_guelph_leader_trigger = yes
					is_ghibelline_leader_trigger = yes
				}
			}
			gg_triggered_catalyst_effect = {
				ACTOR = scope:imprisoner
				RECIPIENT = root
				CATALYST = catalyst_gg_leader_imprisoned
			}
		}
		
		# 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 = {
					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 = {
						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
				}
			}
		}

		house ?= {
			change_house_relation_effect = {
				HOUSE = scope:imprisoner.house
				VALUE = house_relation_damage_medium_value
				REASON = imprisonment
				CHAR = scope:imprisoner
				TARGET_CHAR = root
				TITLE = scope:dummy_gender
			}
		}

		# Paiza
		if = {
			limit = {
				reverse_has_opinion_modifier = {
					target = scope:imprisoner
					modifier = gravely_abused_paiza_authority
				}
			}
			scope:imprisoner = {
				add_character_flag = {
					flag = paiza_abuse_punished
					years = 15
				}
			}
		}

		# Charter
		if = {
			limit = { government_has_flag = government_is_merchant }
			if = {
				limit = {
					reverse_has_opinion_modifier = {
						target = scope:imprisoner
						modifier = gravely_abused_charter_authority_opinion
					}
				}
				scope:imprisoner = {
					add_character_flag = {
						flag = merchant_charter_abuse_punished
						years = 50
					}
				}
			}
			if = {
				limit = {
					suzerain ?= scope:imprisoner
				}
				# Destroy charters of imprisoned merchants
				merchant_charter_destroy_held_charters_effect = {
					MERCHANT = this
					PATRON = scope:imprisoner
				}
			}
		}

		# Partner contracts
		if = {
			limit = { exists = var:partner_contract }
			trade_opportunity_partner_invalidated_effect = { PARTNER = this }
		}
	}
	events = {
		prison_notification.0001
		stewardship_duty.1066	# You imprison your client and prevent them from working for you.
		# NEOW 2555: event not loaded. bookmark.0003			# King Aella gets imprisoned by one of Ragnarr Lodbrok's sons
		martial_authority.2052	# Right-Hand Person Imprisoned (by you)
		martial_authority.2053	# Right-Hand Person Imprisoned (by someone else)
		diarchy.0001			# You're forced into a regency
		delay = {
			days = { 1 180 }
		}
		diarchy.8021			# The AI captures their co-emperor.
	}
}

# Code on-action: character released from prison in root scope. Does not fire when "released" due to dying
# imprisoning character set as scope:imprisoner
on_release_from_prison = {
	effect = {
		# We give you a cool chapter if you escaped from prison lol
		if = {
			limit = {
				has_legend_chapter_trigger = { CHAPTER = extra_deed }	
				NOT = {
					promoted_legend ?= {
						has_legend_chapter = {
							name = extra_deed
							localization_key = legend_chapter_extra_deed_recent_imprisonment
						}
					}
				}				
			}
			random = {
				chance = 75
				send_interface_toast = {
					title = legend_chapter_change_toast
					left_icon = root
					promoted_legend = {
						set_legend_chapter = {
							name = extra_deed
							localization_key = legend_chapter_extra_deed_recent_imprisonment
						}
					}
				}
			}
		}
		if = {
			limit = { # Uses flag to stop escape + release memories being created at same time
				NOT = { has_character_flag = escaped_from_prison_flag }
			}
			create_character_memory = {
				type = released_from_prison_memory
				
				participants = {
					imprisoner = scope:imprisoner
				}
			}
		}
		# No prison, no problem.
		if = {
			limit = { has_character_modifier = gruesome_festivals_designated_sacrifice_fp1_modifier }
			remove_character_modifier = gruesome_festivals_designated_sacrifice_fp1_modifier
		}
		if = {		#from event hold_court.6140
			limit = { has_character_modifier = 6140_animal_guardian_modifier }
			remove_character_modifier = 6140_animal_guardian_modifier
		}
		if = {
			limit = { has_character_flag = realm_priest }
			remove_character_flag = realm_priest
		}
		if = {
			limit = { has_character_flag = rival_on_exit_prison }
			remove_character_flag = rival_on_exit_prison

			# imprisoner has to still be valid and alive
			if = {
				limit = {
					scope:imprisoner ?= { is_alive = yes }
				}
				set_relation_rival_effect = {
					TARGET = scope:imprisoner
					REASON = rival_was_kept_in_jail
				}
			}
		}
		if = {
			limit = { has_variable = imprisoned_by_diarch }
			remove_variable = imprisoned_by_diarch
		}
		if = {
			limit = { has_character_flag = being_prisonbroken_by_laamp }
			remove_character_flag = being_prisonbroken_by_laamp
		}
		scope:imprisoner ?= {
			gdp_clear_indicted_character_effect = { TARGET = root }
		}
		# Struggle bonus
	}

	events = {
		prison.0001					# Remove imprisoned_me modifier, consume imprisonment reasons the imprisoner had
		prison_notification.2001
		intrigue_dread.4010			# Interrupts this event chain for the imprisoner when their prisoner is freed.
		human_sacrifice.0003		# Is the character marked for human sacrifice?
		martial_authority.2054 		# Right-hand wo/man released 
	}
}



################
# MAINTENANCE
################
quarterly_prison_maintenance_pulse = {
	trigger = {
		any_prisoner = { }
	}
	effect = { #Fire the correct events for all prisoners
		every_prisoner = {
			limit = {
				is_in_prison_type = house_arrest
			}
			trigger_event = {
				days = 1 # Queued, so we can check the pulse in parallel
				on_action = house_arrest_ongoing
			}
		}
		every_prisoner = {
			limit = {
				is_in_prison_type = dungeon
			}
			trigger_event = {
				days = 1 # Queued, so we can check the pulse in parallel
				on_action = dungeon_ongoing
			}
		}
	}
}

########
#Dungeon on_actions
########
dungeon_ongoing = {
	trigger = {
		exists = imprisoner #Don't fire if we've been released since the on_action was called.
	}

	random_events = {
		chance_to_happen = 50
		500 = 0
		100 = dungeon_ongoing.0001 #Crazy neighbor bothers you
		200 = dungeon_ongoing.0002 #Wait patiently - or not
		100 = dungeon_ongoing.0003 #Guards threaten you with violence
		200 = dungeon_ongoing.0004 #A wound becomes infected
		50 = dungeon_ongoing.0005 #Infection passes
		200 = dungeon_ongoing.0006 #Prison has low standards - unless you are Content
		100 = dungeon_ongoing.0007 #It is terrible to be Ambitious and in prison
		100 = dungeon_ongoing.0008 #Impatient, and have stayed in the dungeon "forever"
		100 = dungeon_ongoing.0009 #You prison pet is there to comfort you
		100 = dungeon_ongoing.0010 #Write to a relative asking for help
		100 = dungeon_ongoing.0015 #A friend or relative pays your ransom
		100 = dungeon_ongoing.9006 #You can't afford ransom and barter for release

		#Long-term imprisonment events:
		500 = dungeon_ongoing.9001 #Become ill
		500 = dungeon_ongoing.9002 #Become pneumonic
		500 = dungeon_ongoing.9003 #Gain stress
		500 = dungeon_ongoing.9004 #Gain stress
		500 = dungeon_ongoing.9005 #Prison Pet Dies
	}
}


#######
# House Arrest on_actions
#######
house_arrest_ongoing = {
	trigger = {
		exists = imprisoner #Don't fire if we've been released since the on_action was called.
	}

	random_events = {
		100 = house_arrest_ongoing.0001 #To be ambitious is terrible
		100 = house_arrest_ongoing.0002 #To be arrogant is terrible
		100 = house_arrest_ongoing.0003 #Missing love ones
		100 = house_arrest_ongoing.0004 #Friend at court helps you
		100 = house_arrest_ongoing.0005 #Rival at court is a nuisance
		100 = dungeon_ongoing.0010 #Write to a relative asking for help
		100 = dungeon_ongoing.0015 #A friend or relative pays your ransom
	}
}

#########
# Escape from prison (attempt)
#########
escape_from_prison = {
	events = {
		prison.9000
	}
}

escape_from_prison_success = {
	random_events = {
		1 = prison.9001 #You escaped!
		100 = prison.9002 #You escaped thanks to your prison pet
		100 = prison.9003 #You escape with friend or close family member
	}
}

escape_from_prison_failure = {
	random_events = {
		10 = prison.9501 #You're still stuck, buttercup... :(
		100 = prison.9502 #You and your prison pet failed
		100 = prison.9503 #Your friend or close family member leave you behind
		200 = fp1_shieldmaiden.0011 #Your imprisoner's shieldmaiden stymies your escape.
		50 = prison.9504 #Rival sabotages escape
		5 = prison.9505 #Become wounded
	}
}
