story_cycle_black_death = {
	# by Nick Meredith

	on_setup = { #check if the plague is close enough
		story_owner = {
			trigger_event = {
				id = epidemic_events.5998
				days = { 21 37 }
			}
		}
	}

	on_end = {
		debug_log = "Black Death story ended on:"
		debug_log_date = yes
	}

	on_owner_death = {
		# Should be inherited, so transfer to heir
		if = {
			limit = {
				exists = story_owner.player_heir
			}
			make_story_owner = story_owner.player_heir
		}
		# Or destroy
		else = {
			end_story = yes
		}
	}

	#check if the plague is close enough
	effect_group = {
		months = { 2 4 }

		trigger = {
			has_global_variable = black_death
			story_owner = {
				NOR = {
					has_character_flag = black_death_nearby
					has_character_flag = black_death_splash
				}
			}
		}

		first_valid = {
			triggered_effect = {
				trigger = { always = yes }
				effect = {
					story_owner = {
						trigger_event = {
							id = epidemic_events.5998
							days = { 2 4 }
						}
					}
				}
			}
		}
	}

	#IT IS GETTING CLOSER!
	effect_group = {
		months = { 4 8 }

		trigger = {
			has_global_variable = black_death
			story_owner = {
				has_character_flag = black_death_closer
				NOR = {
					has_character_flag = black_death_nearby
					has_character_flag = black_death_splash
				}
			}
		}

		first_valid = {
			triggered_effect = {
				trigger = { always = yes }
				effect = {
					save_scope_as = story_scope
					story_owner = {
						trigger_event = {
							on_action = ongoing_black_death_events
						}
					}
				}
			}
		}

	}

	#ALMOST THERE!
	effect_group = {
		months = { 1 2 }

		trigger = {
			has_global_variable = black_death
			story_owner = {
				has_character_flag = black_death_closer
				NOR = {
					has_character_flag = black_death_splash
					has_character_flag = black_death_nearby
				}
			}
		}

		first_valid = {
			triggered_effect = {
				trigger = { always = yes }
				effect = {
					save_scope_as = story_scope
					story_owner = {
						trigger_event = {
							id = epidemic_events.5998
							days = { 4 8 }
						}
					}
				}
			}
		}
	}

	#YIKES, IT IS NEAR
	effect_group = {
		days = { 21 37 }

		trigger = {
			has_global_variable = black_death
			story_owner = {
				has_character_flag = black_death_nearby
			}
		}

		first_valid = {
			triggered_effect = {
				trigger = { always = yes }
				effect = {
					save_scope_as = story_scope
					story_owner = {
						trigger_event = {
							on_action = ongoing_black_death_events
						}
					}
				}
			}
		}

	}

	# We can stop worrying about black plague coming to us, because...
	effect_group = {
		days = { 30 60 }

		trigger = {
			OR = {
				NOT = { #...the black plague is gone...
					has_global_variable = black_death
				}
				story_owner = {
					has_character_flag = black_death_splash #... IT IS HERE OH GOD OH HECK
				}
			}
		}

		first_valid = {
			triggered_effect = {
				trigger = { always = yes }
				effect = {
					end_story = yes
				}
			}
		}
	}


	#Maintenance group
	effect_group = {
		days = 1
		trigger = {
			story_owner = { is_landed = no }
		}
		triggered_effect = {
			trigger = { always = yes }
			effect = {
				#You became a landless adventurer/administrative! Ending story prematurely.
				story_owner = { save_scope_as = story_owner }
				debug_log = "Black Death invalidated"
				debug_log_scopes = yes
				end_story = yes
			}
		}
	}
}
