﻿# Convert character and continue
contract_regula_virus_effect = {
	# Note, we dont use fascinare_sucess_effect as that would trigger more Infecta stories!
	# Instead we just add traits and continue
	add_trait = mulsa
	add_trait = regula_virus
	convert_to_regula_openly_or_secretly_effect = {
		FAITH = global_var:magister_character.faith
	}
	create_memory_infected_by_virus_main = {
		LAST_PATIENT = root
		PATIENT_ZERO = scope:regula_patient_zero
	}

	# Keep track of infecta converts
	save_temporary_scope_as = newest_convert
	global_var:magister_character = {
		random_owned_story = {
			limit = {
				story_type = story_regula_infecta_chain
				var:patient_zero = scope:regula_patient_zero
			}
			add_to_variable_list = {
				name = new_converts
				target = scope:newest_convert
			}
		}
	}
}

# Build a list of characters to spread the Regula virus
# This consists of only characters the infecta would know
# Requires - INFECTA_CHARACTER
# Puts the possible characters into a list called potential_regula_virus_targets_list
create_regula_virus_list_effect = {
	$INFECTA_CHARACTER$ = {
		#First, an available partner gets a shot at being infected.
		# Our Lovers/Soulmates
		every_relation = {
			type = lover
			limit = {
				regula_virus_valid_infecta_target_trigger = yes
				NOT = { is_in_list = potential_regula_virus_targets_list }
			}
			add_to_temporary_list = potential_regula_virus_targets_list
		}
		# Our Friends
		every_relation = {
			type = friend
			limit = {
				regula_virus_valid_infecta_target_trigger = yes
				NOT = { is_in_list = potential_regula_virus_targets_list }
			}
			add_to_temporary_list = potential_regula_virus_targets_list
		}
		# Consorts
		every_consort = {
			limit = {
				regula_virus_valid_infecta_target_trigger = yes
				NOT = { is_in_list = potential_regula_virus_targets_list }
			}
			add_to_temporary_list = potential_regula_virus_targets_list
		}
		# Or husbands spouses/concubines
		if = {
			limit = {
				is_married = yes
			}
			primary_spouse = { # To account for polygamous societies.
				every_consort = {
					limit = {
						regula_virus_valid_infecta_target_trigger = yes
						NOT = { is_in_list = potential_regula_virus_targets_list }
					}
					add_to_temporary_list = potential_regula_virus_targets_list
				}
			}
		}
		# Our Family
		every_close_or_extended_family_member = {
			limit = {
				regula_virus_valid_infecta_target_trigger = yes
				NOT = { is_in_list = potential_regula_virus_targets_list }
			}
			add_to_temporary_list = potential_regula_virus_targets_list
		}
		# If I am a ruler add ruler contacts
		if = {
			limit = { is_ruler = yes }
			# My vassals
			every_vassal_or_below = {
				limit = {
					regula_virus_valid_infecta_target_trigger = yes
					NOT = { is_in_list = potential_regula_virus_targets_list }
				}
				add_to_temporary_list = potential_regula_virus_targets_list
			}
			# My courtiers/guests
			every_courtier_or_guest = {
				limit = {
					regula_virus_valid_infecta_target_trigger = yes
					NOT = { is_in_list = potential_regula_virus_targets_list }
				}
				add_to_temporary_list = potential_regula_virus_targets_list
			}
			# My liege
			liege ?= {
				if = {
					limit = {
						regula_virus_valid_infecta_target_trigger = yes
						NOT = { is_in_list = potential_regula_virus_targets_list }
					}
					add_to_temporary_list = potential_regula_virus_targets_list
				}
			}
		}
		# Friendly schemes
		every_scheme = {
			limit = {
				friendly_scheme_trigger = yes
				scheme_target_character = {
					NOT = { is_in_list = potential_regula_virus_targets_list }
					regula_virus_valid_infecta_target_trigger = yes
				}
			}
			scheme_target_character = { add_to_temporary_list = potential_regula_virus_targets_list }
		}
		every_targeting_scheme = {
			limit = {
				friendly_scheme_trigger = yes
				scheme_owner = {
					NOT = { is_in_list = potential_regula_virus_targets_list }
					regula_virus_valid_infecta_target_trigger = yes
				}
			}
			scheme_owner = { add_to_temporary_list = potential_regula_virus_targets_list }
		}
	}
}

# Build a list of characters to spread the Regula virus
# Adds the owner of the court we are in and their courtiers/guests/vassals
# Requires - INFECTA_CHARACTER, CONTAGION_COURT_OWNER
create_regula_virus_court_list_effect = {
	$CONTAGION_COURT_OWNER$ = {
		if = {
			limit = {
				regula_virus_valid_infecta_target_trigger = yes
				NOT = { is_in_list = potential_regula_virus_targets_list }
			}
			add_to_temporary_list = potential_regula_virus_targets_list
		}
		every_courtier_or_guest = {
			limit = {
				regula_virus_valid_infecta_target_trigger = yes
				NOT = { is_in_list = potential_regula_virus_targets_list }
			}
			add_to_temporary_list = potential_regula_virus_targets_list
		}
		every_vassal = {
			limit = {
				regula_virus_valid_infecta_target_trigger = yes
				NOT = { is_in_list = potential_regula_virus_targets_list }
			}
			add_to_temporary_list = potential_regula_virus_targets_list
		}
	}
	# Then add the standard list from above
	create_regula_virus_list_effect = { INFECTA_CHARACTER = $INFECTA_CHARACTER$ }
}

# Try and infect targets in our list
# Four possible events
# 1. We find a new target to infect, the last_patient changes and the current person "passes on" the regula virus trait
# 2. Nothing happens, retrigger virus event
# 3. Chain finishes, we remove virus trait
# 4. The last_patient gets imprisoned and can no longer pass on the regula virus
regula_virus_try_infect_list_effect = {

	# Get how many current infections there are
	regula_get_number_of_converts = yes

	random_list = {
		# 1. We find a new target to infect, the last_patient changes and the current person "passes on" the regula virus trait
		100 = {
			modifier = {
				scope:infecta_story = {
					var:infection_tally >= 5
				}
				add = -100
			}
			random_in_list = {
				list = potential_regula_virus_targets_list
				weight = {
					base = 15

					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 = {
						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
						OR = {
							faith = global_var:magister_character.faith
							secret_faith ?= global_var:magister_character.faith
						}
					}
					modifier = {
						add = -1000
						this = scope:last_patient
					}
				}
				contract_regula_virus_effect = yes
				save_scope_as = last_patient
				trigger_event = {
					id = regula_virus.1000
					days = { regula_infecta_virus_min_days_event regula_infecta_virus_max_days_event }
				}
			}
			remove_trait = regula_virus
		}

		# 2. Nothing happens, retrigger virus event
		50 = {
			modifier = {
				scope:infecta_story = {
					var:infection_tally >= 5
				}
				add = -50
			}
			trigger_event = {
				id = regula_virus.1000
				days = { regula_infecta_virus_min_days_event regula_infecta_virus_max_days_event }
			}
		}

		# 3. Chain finishes, we remove virus trait
		0 = {
			modifier = {
				scope:infecta_story = {
					var:infection_tally > 1
				}
				add = 50
			}
			regula_virus_end_infect_default = yes
		}

		0 = { #Contagion prevented by locking up the source
			modifier = {
				regula_virus_imprisonable_trigger = yes
				add = 25
			}
			# Imprision this character
			scope:contagion_court_owner = {
				imprison = {
					target = root
					type = dungeon
				}
			}
			regula_virus_end_infect_imprision = yes
		}
	}
}

# End the infect story normally
regula_virus_end_infect_default = {

	# Inform the player.
	save_scope_as = last_patient
	global_var:magister_character = {
		# Find the relevent infecta story
		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
					}
				}
			}
		}

		# Tells the player the outcome.
		send_interface_message = {
			type = event_spouse_good
			title = regula_virus_outcome.t
			desc = regula_virus_outcome.desc
			left_icon = scope:regula_patient_zero
			right_icon = scope:last_patient
			scope:infecta_story = {
				every_in_list = {
					variable = new_converts
					custom_tooltip = regula_virus_outcome.infected.tt
				}
			}
		}
		random_owned_story = {
			limit = {
				story_type = story_regula_infecta_chain
				var:patient_zero = scope:regula_patient_zero
			}
			end_story = yes
		}
	}

	# Remove trait
	remove_trait = regula_virus
}

# End the infecta story because last patient was imprisoned
regula_virus_end_infect_imprision = {

	# Inform the player.
	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
					}
				}
			}
		}

		# Tells the player the outcome.
		send_interface_message = {
			type = event_captured_combatants_bad
			title = regula_virus_outcome_jail.t
			desc = regula_virus_outcome_jail.desc
			left_icon = scope:regula_patient_zero
			right_icon = scope:last_patient
			scope:infecta_story = {
				every_in_list = {
					variable = new_converts
					custom_tooltip = regula_virus_outcome.infected.tt
				}
			}
		}

		# End the story
		random_owned_story = {
			limit = {
				story_type = story_regula_infecta_chain
				var:patient_zero = scope:regula_patient_zero
			}
			end_story = yes
		}
	}

	# Remove trait
	remove_trait = regula_virus
}

# Returns how many infections there are using Magisters infecta story
# var:infection_tally via scope:infecta_story
regula_get_number_of_converts = {
	global_var:magister_character = {
		# Find the relevent infecta story
		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
					}
				}
			}
		}
	}
}
