﻿namespace = olympia_culling

olympia_culling.1000 = {	#Start of game after lobby one-time event
	hidden = yes
	scope = none

	immediate = {
		every_independent_ruler = {
			trigger_event = {
				id = olympia_culling.1009			#Check diplomatic ranges, go!
				days = 3							#Delay it to ensure all courts are fully populated
			}
		}
		
		every_independent_ruler = {
			trigger_event = {
				id = olympia_culling.1010			#Apply the trait and modifiers!
				days = 7							#Delay it to ensure all flags are in place
			}
		}
		
		#Trigger a protection event for already established dynasty members, if game rule has been enabled to protect them
		every_player = {
			dynasty = {
				every_dynasty_member = {
					trigger_event = {
						id = olympia_culling.1007
						days = 5
					}
				}
			}
			every_close_or_extended_family_member = {
				limit = {
					OR = {
						NOT = { dynasty = prev.dynasty }
						is_lowborn = yes
					}
				}
				
				trigger_event = {
					id = olympia_culling.1007
					days = 5
				}
			}
		}
		
		if = {
			limit = { has_game_rule = acotw_protect_legendary_dynasty_on }
			every_living_character = {
				limit = {
					has_dynasty = yes
					dynasty = {
						dynasty_prestige_level >= 8
					}
				}
				trigger_event = {
					id = olympia_culling.1007
					days = 10
				}
			}
		}
	}
}

#######################
#Yearly Pulse to Check#
#######################

olympia_culling.1001 = {
    hidden = yes
	scope = none

	immediate = {
		
		every_independent_ruler = {
			limit = { 
				OR = {
					AND = {
						any_player = { in_diplomatic_range = prev }
						has_character_flag = acotw_ruler_outside_diplomatic_range
					}
					AND = {
						NOT = { any_player = { in_diplomatic_range = prev } }
						NOT = { has_character_flag = acotw_ruler_outside_diplomatic_range }
					}
				}
			}
			trigger_event = {
				id = olympia_culling.1009
				days = 3
			}
		}
	}
}

##################
#Culling the Weak#
##################

olympia_culling.1002 = {	#Time for culling
    hidden = yes
	type = character_event
	
	trigger = {
		olympia_possible_cull_trigger = yes
	}
	
	immediate = {
		
		#Obscure their death if they have no relatives or spouse
		if = {
			limit = {
				is_adult = yes
				any_close_family_member = { 
					is_alive = no
				}
				is_married = no
			}
			death = { death_reason = death_obscured }
		}
		else_if = {
			limit = {
				is_adult = no
			}
			death = { death_reason = death_ill }
		}
		
		#If they had living family or a spouse then give them at least a plausible send-off
		else_if = {
			limit = { age >= 50 }
			death = { death_reason = death_natural_causes }
		}
		else = {
			death = { death_reason = death_accident }
		}
	}
}

########################
#Individual Ruler Check#
########################

olympia_culling.1003 = {
    hidden = yes
	type = character_event

	trigger = {
		is_ai = yes
	}

    immediate = {
		
		#If there is a player out of diplomatic range do nothing
		if = {
			limit = {
				any_player = {
					in_diplomatic_range = prev
				}
			}
			olympia_purge_obscured_from_realm = yes
		}
		
		else = {
            # No player in range -> add obscurity (trigger guarantees trait is absent here)
			olympia_add_obscured_to_realm = yes
        }
	}
}

##########################
#AI Ruler Modifier Checks#
##########################

olympia_culling.1004 = {
    hidden = yes
	type = character_event

	trigger = {
		is_ruler = yes
		is_ai = yes
		has_trait = acotw_obscured
		olympia_acotw_modifier_adjustments_needed = yes
	}

    immediate = {
		actow_olympia_add_health_modifiers = yes
		actow_olympia_add_ruler_modifiers = yes
	}
}

#########################
#Courtier & Guests Check#
#########################

olympia_culling.1005 = {	#Check to make sure your courtiers are all obscured or not
    hidden = yes
	type = character_event

	trigger = {
		is_ruler = yes	#Covers Barons up, plus Mercenary Captains, more flexible
		OR = {
			AND = {
				has_trait = acotw_obscured
				any_courtier_or_guest = {
					acotw_character_can_be_obscured = yes
				}
			}
			AND = {
				NOT = { has_trait = acotw_obscured }
				any_courtier_or_guest = {
					has_trait = acotw_obscured
				}
			}
		}
	}

    immediate = {
		if = {
			limit = {
				has_trait = acotw_obscured
			}
			every_courtier_or_guest = {
				limit = { acotw_character_can_be_obscured = yes }
				add_trait = acotw_obscured
				actow_olympia_add_health_modifiers = yes
			}
		}
		else_if = {
			limit = {
				NOT = { has_trait = acotw_obscured }
			}
			every_courtier_or_guest = {
				limit = { has_trait = acotw_obscured }
				actow_olympia_remove_culling_trait_and_modifiers = yes
			}
		}
	}
}

#########################
#General Character Check#
#########################

olympia_culling.1006 = {	#Checks to see if your new top liege has the obscured trait or not then add yours if necessary
    hidden = yes
	type = character_event

	trigger = {
		exists = top_liege
		NOT = { has_character_flag = acotw_cull_immunity }
		
		OR = {
		
			#Liege has trait character doesn't --> should add
			AND = {
				top_liege = { has_trait = acotw_obscured }
				acotw_character_can_be_obscured = yes
			}
			
			#Liege lacks trait character has it --> should remove
			AND = { 
				NOT = { top_liege = { has_trait = acotw_obscured } }
				has_trait = acotw_obscured
			}
			
			#Or you have mismatching modifiers as a ruler
			AND = {
				is_ruler = no
				OR = {
					has_character_modifier = acotw_olympia_ruler_domain_increase
					has_character_modifier = acotw_olympia_war_reduction
				}
				OR = {
					has_game_rule = acotw_health_adjustments_on
					NOT = { has_character_modifier = acotw_olympia_health_adjustments }
				}
			}
		}
	}

    immediate = {
		if = {
			limit = { top_liege = { has_trait = acotw_obscured } }
			add_trait = acotw_obscured
			actow_olympia_add_health_modifiers = yes
			actow_olympia_add_ruler_modifiers = yes
		}
		
		else_if = {
			limit = { has_trait = acotw_obscured }
			actow_olympia_remove_culling_trait_and_modifiers = yes
		}
		
		else_if = {
			limit = {
				is_ruler = no
			}
			remove_character_modifier = acotw_olympia_ruler_domain_increase
			remove_character_modifier = acotw_olympia_war_reduction
		}
	}
}

##################################
#Character Birth - Immunity Flags#
##################################

olympia_culling.1007 = {	#Adds immune flags to characters at birth to save on them ever being checked for culling later
    hidden = yes
	type = character_event

	trigger = {
		OR = {
			any_close_family_member = {									#High born child prevent from getting culled
				any_held_title = { tier >= tier_duchy }
			}
			any_close_or_extended_family_member = {						#Royal family members are always kept around
				any_held_title = { tier >= tier_kingdom }
			}
			any_player = {
				OR = {
					ROOT = { dynasty = prev.dynasty }
					is_close_or_extended_family_of = ROOT
				}
			}
			AND = {
				has_dynasty = yes
				dynasty = {
					dynasty_prestige_level >= 8	#Glorious, Fabled, and Legendary
				}
			}
		}
	}

    immediate = {
		
		#Flag lifetime immunity from culls
		if = {
			limit = {
				any_player = {
					OR = {
						ROOT = { dynasty = prev.dynasty }
						is_close_or_extended_family_of = ROOT
					}
				}
				NOT = { has_game_rule = acotw_protect_player_dynasty_off }
			}
			
			#Protect Player Dynasties or Houses depending on Game Rules
			if = {
				limit = {
					has_game_rule = acotw_protect_player_house_on
					any_player = {
						OR = {
							is_close_or_extended_family_of = prev				#Any close or extended family of a player is safe, should cover relatives who may not be part of the dynasty through marriage or adoption
							this.house = prev.house								#Player house members safe
						}
					}
				}
				add_character_flag = acotw_cull_immunity
				
				if = {
					limit = { has_character_modifier = acotw_olympia_health_adjustments }
					remove_character_modifier = acotw_olympia_health_adjustments
				}
			}
			else_if = {
				limit = {
					has_game_rule = acotw_protect_player_dynasty_on
					any_player = {
						OR = {
							is_close_or_extended_family_of = prev				#Any close or extended family of a player is safe, should cover relatives who may not be part of the dynasty through marriage or adoption
							this.dynasty = prev.dynasty							#Player dynasty members safe
						}
					}
				}
				add_character_flag = acotw_cull_immunity
				
				if = {
					limit = { has_character_modifier = acotw_olympia_health_adjustments }
					remove_character_modifier = acotw_olympia_health_adjustments
				}
			}
			else = {
				acotw_add_health_modifiers_baby = yes
				add_character_flag = acotw_cull_immunity
			}
		}
		
		else_if = {
			limit = { 
				has_game_rule = acotw_protect_legendary_dynasty_on
				has_dynasty = yes
				dynasty = {
					dynasty_prestige_level >= 8
				}
			}
			if = {
				limit = { has_character_modifier = acotw_olympia_health_adjustments }
				remove_character_modifier = acotw_olympia_health_adjustments
			}
			add_character_flag = acotw_cull_immunity
		}
		
		else_if = {
			limit = {
				OR = {
					any_close_family_member = {									#High born child prevent from getting culled
						any_held_title = { tier >= tier_duchy }
					}
					any_close_or_extended_family_member = {						#Royal family members are always kept around
						any_held_title = { tier >= tier_kingdom }
					}
				}
			}
			acotw_add_health_modifiers_baby = yes
			add_character_flag = acotw_cull_immunity
		}
	}
}

####################
#Cheap Player Check - Fires from Character Interactions
####################

olympia_culling.1008 = {
	hidden = yes
	type = character_event
	
	trigger = {
		is_ai = no
		OR = {
			any_independent_ruler = {
				has_trait = acotw_obscured
				any_player = { in_diplomatic_range = prev }
			}
			any_independent_ruler = {
				acotw_character_can_be_obscured = yes
				NOT = {
					any_player = { in_diplomatic_range = prev }
				}
			}
		}
	}
	
	immediate = {
		every_independent_ruler = {
			limit = {
				has_trait = acotw_obscured
				any_player = { in_diplomatic_range = prev }
			}
			olympia_purge_obscured_from_ruler = yes					#Remove obscured from rulers and vassals only
		}
		every_independent_ruler = {
			limit = {
				acotw_character_can_be_obscured = yes
				NOT = { any_player = { in_diplomatic_range = prev } }
			}
			olympia_add_obscured_to_ruler = yes						#Apply to rulers and vassals only
		}
	}
}

########################
#Diplomatic Range Check#
########################

#Splitting these events into two helped eliminate OOS issues in multiplayer
olympia_culling.1009 = {
    hidden = yes
    type = character_event

	trigger = {
		OR = {
			AND = {
				any_player = { in_diplomatic_range = ROOT }
				has_character_flag = acotw_ruler_outside_diplomatic_range
			}
			AND = {
				NOT = { any_player = { in_diplomatic_range = prev } }
				NOT = { has_character_flag = acotw_ruler_outside_diplomatic_range }
			}
		}
	}

	immediate = {
        if = {
			limit = {
				any_player = { in_diplomatic_range = ROOT }
				has_character_flag = acotw_ruler_outside_diplomatic_range
			}
			remove_character_flag = acotw_ruler_outside_diplomatic_range
		}
		else_if = {
			limit = {
				NOT = { any_player = { in_diplomatic_range = prev } }
				NOT = { has_character_flag = acotw_ruler_outside_diplomatic_range }
			}
			add_character_flag = acotw_ruler_outside_diplomatic_range
		}		
    }
}

olympia_culling.1010 = {
    hidden = yes
    type = character_event

	trigger = {
		is_independent_ruler = yes
		OR = {
			AND = {
				has_character_flag = acotw_ruler_outside_diplomatic_range
				NOT = { has_trait = acotw_obscured }
			}
			AND = {
				NOT = { has_character_flag = acotw_ruler_outside_diplomatic_range }
				has_trait = acotw_obscured
			}
		}
	}

    immediate = {
		if = {
			limit = {
				has_character_flag = acotw_ruler_outside_diplomatic_range
				NOT = { has_trait = acotw_obscured }
			}
			#olympia_add_obscured_to_ruler = yes
			olympia_add_obscured_to_realm = yes
		}
		else_if = {
			limit = {
				NOT = { has_character_flag = acotw_ruler_outside_diplomatic_range }
				has_trait = acotw_obscured
			}
			olympia_purge_obscured_from_realm = yes
		}
	}
}

###

olympia_culling.2000 = {
	hidden = yes
	scope = none
	
	immediate = {
		#trigger_event = {
		#	id = olympia_culling.2001
		#	days = 2
		#}
		
		if = {
			limit = { has_game_rule = acotw_early_game_culling_on }
			every_living_character = {
				limit = { olympia_early_game_possible_cull_trigger = yes }
				death = { death_reason = death_obscured }
			}
		}
		else_if = {
			limit = { has_game_rule = acotw_early_game_culling_extreme }
			every_living_character = {
				limit = { olympia_early_game_possible_extreme_cull_trigger = yes }
				death = { death_reason = death_obscured }
			}
		}
	}
}

olympia_culling.2001 = {
	hidden = yes
	scope = none
	
	immediate = {
		if = {
			limit = { has_game_rule = acotw_early_game_culling_on }
			every_living_character = {
				limit = { olympia_early_game_possible_cull_trigger = yes }
				death = { death_reason = death_obscured }
			}
		}
		else_if = {
			limit = { has_game_rule = acotw_early_game_culling_extreme }
			every_living_character = {
				limit = { olympia_early_game_possible_extreme_cull_trigger = yes }
				death = { death_reason = death_obscured }
			}
		}
	}
}