###############################
# Maintenance events
#
# misc_event.1 -> misc_event.13
###############################

namespace = misc_event

# Destroy Bagaude
character_event	= {
	id = misc_event.1

	hide_window = yes
	
	only_rulers = yes
	ai = yes
	
	trigger = {
		has_landed_title = d_bagaudae
		ROOT = {
			NOT = {
				primary_title = {
					title = d_bagaudae
				}
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		activate_title	= {
			title = d_bagaudae
			status = no
		}
	}
}

# Maintain Pope Nicene
character_event	= {
	id = misc_event.2

	hide_window = yes
	
	only_rulers = yes
	ai = yes
	
	trigger = {
		OR = {
			has_landed_title = d_papal_state
			has_landed_title = d_orthodox
		}
		NOT = { religion = nicene }
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		ROOT = {
			abandon_heresy = yes
			religion = nicene
		}
	}
}

# Maintain Senate as Republic
character_event	= {
	id = misc_event.3

	hide_window = yes
	
	only_rulers = yes
	
	trigger = {
		primary_title = {
			title = d_senate
		}
		NOT = { government = merchant_republic_government }
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		ROOT = {
			set_government_type = merchant_republic_government
		}
	}
}

# misc_event.4 -> misc_event.5 free

# Adds not recognised by Senate modifier to all new independent holders of the WRE or Italy
character_event = {
	id = misc_event.6
	
	hide_window = yes
	
	is_triggered_only = yes
	
	only_playable = yes
	only_rulers = yes
	only_independent = yes
	
	trigger = {
		OR = {
			has_landed_title = e_wre
			has_landed_title = k_italy
		}
		is_title_active	= d_senate
		NOT = { has_character_flag = had_senate_recognition_event }
	}
	
	immediate = {
		if = {
			limit = {
				has_landed_title = e_wre
			}
			e_wre = {
				set_title_flag = not_recognised_senate
			}
		}
		if = {
			limit = {
				has_landed_title = k_italy
			}
			k_italy = {
				set_title_flag = not_recognised_senate
			}
		}
	}
}

# Destroy Senate
character_event = {
	id = misc_event.7

	hide_window = yes
	
	only_rulers = yes

	trigger = {
		has_landed_title = d_senate
		NOT = { is_title_active = k_republic }
		ROOT = {
			NOT = {
				primary_title = {
					title = d_senate
				}
			}
		}
		NOT = { government = merchant_republic_government }
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		activate_title = {
			title = d_senate
			status = no
		}
	}
}

# Keep Bureaucratic government after switching primary title
character_event = {
	id = misc_event.8

	hide_window = yes
	
	only_playable = yes
	only_independent = yes

	trigger = {
		NAND = {
			controls_religion = yes
			NOT = {	primary_title = { is_landless_type_title = no }	}
		}
		NOT = { religion_group = muslim }
		NOT = { religion = zoroastrian }
		is_theocracy = no
		any_demesne_title = {
			higher_tier_than = duke
			sassanid_government_trigger = no
			bureaucratic_government_trigger = yes
		}
		higher_tier_than = duke
		NOT = { government = bureaucratic_government }
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		primary_title = {
			set_title_flag = adopted_bureaucratic # For bureaucratic government
		}
		set_government_type = bureaucratic_government
	}
}

# Nicene becomes heresy, disable Great Schism
character_event = {
	id = misc_event.9
	
	hide_window = yes
	
	is_triggered_only = yes # On-action
	
	trigger = {
		FROMFROM = { # temporary character with old orthodoxy religion
			religion = nicene
		}
	}
	
	immediate = {
		set_global_flag = nicene_heresy
	}
}

# Prevent Papal States title revocation
character_event = {
	id = misc_event.10
	
	hide_window = yes
	
	is_triggered_only = yes # On-action
	
	trigger = {
		FROM = { # FROM is the character recieving the event
			is_theocracy = yes
		}
		OR = {
			d_papal_state = {
				any_previous_holder	= {
					character = FROMFROM
				}
			}
			FROMFROM = { # FROMFROM is the person sending the event
				has_landed_title = d_papal_state
				is_theocracy = yes
			}
		}
	}
	
	immediate = {
		d_papal_state = {
			grant_title_no_opinion = FROMFROM
		}
	}
}

# Papal States should remain a theocracy
character_event = {
	id = misc_event.11
	
	hide_window = yes
	
	only_rulers = yes
	ai = yes
	
	trigger = {
		has_landed_title = d_papal_state
		is_theocracy = no
	}
	
	mean_time_to_happen = {
		days = 1
	}
		
	immediate = {
		create_character = {
			random_traits = yes
			dynasty = random
			religion = nicene
			culture = roman
		}
		new_character = {
			d_papal_state = {
				grant_title = PREV
			}
			set_government_type	= theocracy_government
		}
	}
}

# Give all provinces the effects of extreme weather events of 535-536 if game started between 536 and 546
character_event = {
	id = misc_event.12

	hide_window = yes
	
	only_playable = yes
	
	is_triggered_only = yes # On-action
	
	trigger = {
		has_global_flag = 535_536_weather_events
		start_date < 546.1.1
		owns = 496 #provinces are always held by someone
	}

	immediate = {
		trigger_switch = {
			on_trigger = year
			#the order is important here
			546 = { }
			545	= { any_province = { add_province_modifier = { name = failed_crops duration = 365 } } }
			544	= { any_province = { add_province_modifier = { name = failed_crops duration = 730 } } }
			543	= { any_province = { add_province_modifier = { name = failed_crops duration = 1095 } } }
			542	= { any_province = { add_province_modifier = { name = failed_crops duration = 1460 } } }
			541	= { any_province = { add_province_modifier = { name = failed_crops duration = 1825 } } }
			540	= { any_province = { add_province_modifier = { name = failed_crops duration = 2190 } } }
			539	= { any_province = { add_province_modifier = { name = failed_crops duration = 2555 } } }
			538	= { any_province = { add_province_modifier = { name = failed_crops duration = 2920 } } }
			537	= { any_province = { add_province_modifier = { name = failed_crops duration = 3285 } } }
			536	= { any_province = { add_province_modifier = { name = failed_crops duration = 3650 } } }
		}
	}
}

# War-torn province after siege
character_event = {
	id = misc_event.13
	
	hide_window = yes
	
	is_triggered_only = yes # On-action
	
	trigger = {
		FROM = {
			location = {
				NOT = {
					has_province_modifier = devastated_war
				}
			}
		}
	}
	
	immediate = {
		if = { # Has been sieged once and gets the war-torn modifier
			limit = {
				FROM = {
					location = {
						NOR = {
							has_province_modifier = war_torn
							has_province_modifier = devastated_war
						}
					}
				}
			}
			FROM = {
				location = {
					add_province_modifier = {
						name = war_torn
						duration = 3650 # 10 years
					}
				}
			}
		}
		if = { # Has been sieged once more within the year
			limit = {
				FROM = {
					location = {
						has_province_modifier = war_torn
						NOT = { has_province_modifier = devastated_war }
						NOT = {
							check_variable = { 
								which = sieged_variable 
								value = 1
							}
						}
					}
				}
			}
			FROM = {
				location = {
					set_variable = { 
						which = sieged_variable 
						value = 1
					}
				}
			}
		}
		if = { # Has been sieged once more and gets additional variable
			limit = {
				FROM = {
					location = {
						has_province_modifier = war_torn
						NOT = { has_province_modifier = devastated_war }
						check_variable = {
							which = sieged_variable 
							value = 1
						}
						NOT = {
							check_variable = { 
								which = sieged_variable 
								value = 9
							}
						}
					}
				}
			}
			FROM = {
				location = {
					change_variable = { 
						which = sieged_variable 
						value = 1 
					}
				}
			}
		}
		if = { # Has been sieged ten times and gets the devastated modifier
			limit = {
				FROM = {
					location = {
						has_province_modifier = war_torn
						NOT = { has_province_modifier = devastated_war }
						check_variable = {
							which = sieged_variable 
							value = 9
						}
					}
				}
			}
			FROM = {
				location = {
					add_province_modifier = {
						name = devastated_war
						duration = 9125 # 25 years
					}
					remove_province_modifier = war_torn
					set_variable = { 
						which = sieged_variable
						value = 0 
					}
				}
			}
		}
		if = { # Completely unrelated but effect here for performance reasons in testing build
			limit = {
				primary_title = {
					has_title_flag = sub_roman_government_flag
					NOT = { has_title_flag = landed_army } # Variable export no longer necessary once the event has fired once since it can not fire twice
				}
				government = sub_roman_government
			}
			ROOT = {
				export_to_variable = { # Amount of retinues
					which = retinue_size 
					value = retinue_points_max 
				}
			}
		}
	}
}