namespace = regula_virus

regula_virus.1000 = {
	hidden = yes

	trigger = {
		has_trait = regula_virus
		is_imprisoned = no	
	}

	immediate = {
		save_scope_as = disease_spreader
		save_scope_as = last_patient
		if = {
			limit = {
				exists = court_owner
			}
			court_owner = {
				save_scope_as = contagion_court_owner
			}
		}

		if = {
			limit = { exists = scope:contagion_court_owner }

			scope:contagion_court_owner = {
				save_court_physician_as_effect = { SCOPE_NAME = physician }
			}
			#First, an available partner gets a shot at being infected.
			every_relation = {
				type = lover
				limit = {
					age >= 16
					is_male = no
					NOT = { has_trait = regula_virus }
				}
				add_to_temporary_list = potential_contagion_victims
			}
			every_relation = {
				type = soulmate
				limit = {
					age >= 16
					is_male = no
					NOT = { has_trait = regula_virus }
				}
				add_to_temporary_list = potential_contagion_victims
			}
			every_consort = {
				limit = {
					age >= 16
					is_male = no
					NOT = { has_trait = regula_virus }
				}
				add_to_temporary_list = potential_contagion_victims
			}
			if = {
				limit = {
					is_married = yes
				}
				scope:spouse = { # To account for polygamous societies.
					every_consort = {
						limit = {
							age >= 16
							is_male = no
							NOT = { has_trait = regula_virus }
						}
						add_to_temporary_list = potential_contagion_victims
					}
				}
			}
			### Regular Spread
			#Add everyone in the same court (ruler, prisoners, guests) and friendly scheme targets/owners to list
			create_regula_virus_list_effect = { SICK_CHARACTER = root CONTAGION_COURT_OWNER = scope:contagion_court_owner }
			
			random_list = {
				120 = { #Contagion successful
					random_in_list = {
						list = contagion_list
						weight = {
							base = 20

							modifier = {
								add = -5
								has_perk = wash_your_hands_perk
							}
							modifier = {
								add = -5
								has_trait = chaste
							}
							modifier = {
								add = -10
								has_trait = celibate
							}
							modifier = {
								add = -1000
								age < 16
							}
							modifier = { # Very high chance of landing on a female character. Most of the courts seem to be male.
								is_male = no
								add = 240
							}
							modifier = {
								is_imprisoned = yes
								add = -1000
							}
							modifier = { # Failsafe
								add = -1000
								has_trait = magister_trait_group
							}
							modifier = { # To stop the virus from looping between the converted.
								add = -1000
								faith = global_var:magister_character.faith
							}
						}
						contract_regula_virus_effect = yes
						save_scope_as = last_patient
						trigger_event = {
							id = regula_virus.1000
							days = { bubonic_plague_contagion_min bubonic_plague_contagion_max }
						}
					}
					remove_trait = regula_virus
				}
				0 = { #Contagion prevented by locking up the source
					modifier = {
						regula_virus_imprisonable_trigger = yes
						add = 10
					}
					modifier = {
						any_in_list = {
							list = contagion_list
							faith = global_var:magister_character.faith
						}
						add = -5
					}
					scope:contagion_court_owner = {
						imprison = {
							target = root
							type = dungeon
						}
					}
					save_scope_as = last_patient
					global_var:magister_character = {

						random_owned_story = {
							limit = {
								story_type = story_regula_infecta_chain
								var:patient_zero = scope:regula_patient_zero
							}
							save_scope_as = infecta_story
							hidden_effect = {
								set_variable = {
									name = infection_tally
									value = 0
								}
							}
							every_in_list = {
								variable = new_converts
								prev = {
									change_variable = {
										name = infection_tally
										add = 1
									}
								}
							}
						}
						send_interface_message = {  # Tells the player the outcome.
							type = event_spouse_task_good
							title = regula_virus_outcome_jail.t
							desc = regula_virus_outcome_jail.desc
							left_icon = scope:regula_patient_zero
							right_icon = scope:last_patient
						}
						random_owned_story = {
							limit = {
								story_type = story_regula_infecta_chain
								var:patient_zero = scope:regula_patient_zero
							}
							end_story = yes
						}
					}
					remove_trait = regula_virus
					#No contagion this time!
				}
				0 = { # Contagion doesn't occur.  This time.
					modifier = {
						regula_virus_imprisonable_trigger = no
						add = 40
					}
				}
				### Chain ends.
				40 = {
					remove_trait = regula_virus
					#Inform the player.
					global_var:magister_character = {
						random_owned_story = {
							limit = {
								story_type = story_regula_infecta_chain
								var:patient_zero = scope:regula_patient_zero
							}
							save_scope_as = infecta_story
							hidden_effect = {
								set_variable = {
									name = infection_tally
									value = 0
								}
							}
							every_in_list = {
								variable = new_converts
								prev = {
									change_variable = {
										name = infection_tally
										add = 1
									}
								}
							}
						}
						send_interface_message = {  # Tells the player the outcome.
							type = event_spouse_task_good
							title = regula_virus_outcome.t
							desc = regula_virus_outcome.desc
							left_icon = scope:regula_patient_zero
							right_icon = scope:last_patient
						}
						random_owned_story = {
							limit = {
								story_type = story_regula_infecta_chain
								var:patient_zero = scope:regula_patient_zero
							}
							end_story = yes
						}
					}
				}
			}
		}
		if = {
			limit = {
				is_imprisoned = no
			}
			random_list = {
				20 = { #Contagion hits relative/spouse.
					random_in_list = {
						list = potential_contagion_victims
						weight = {
							base = 50
							modifier = { #Who visits the captives, anyway?
								is_imprisoned = no
								add = 50
							}
							modifier = {
								add = -50
								has_perk = wash_your_hands_perk
							}
							modifier = {
								add = -200
								has_trait = chaste
							}
							modifier = {
								add = -1000
								has_trait = celibate
							}
							modifier = { # 80% chance of landing on a female character, if there is one.
								add = 400
								is_male = no
							}
							modifier = { # Failsafe
								add = -1000
								has_trait = magister_trait_group
							}
							modifier = { # To stop the virus from looping between the converted.
								add = -1000
								faith = global_var:magister_character.faith
							}
					}
					contract_regula_virus_relation_effect = yes
				}	
			}
		}
		if = {
			limit = {
				has_trait = regula_virus
			}
			trigger_event = {
				id = regula_virus.1000
				days = { 15 60 }
			}
		}
	}
}