namespace = maintenance
# All of these should be triggered on a yearly pulse, at least for now
# How many senators are necessary to pass a bill?
character_event = {
	id = maintenance.900
	is_triggered_only = yes
	hide_window = yes
	
	only_rulers = yes
	trigger = {
		higher_tier_than = count
	}
	immediate = {
		set_variable = { which = "var_senate_majority" value = 0 }
		any_realm_province = {
			limit = {
				owner = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
				OR = {
					empire = { holder_scope = { character = ROOT } }
					kingdom = { holder_scope = { character = ROOT } }
					duchy = { holder_scope = { character = ROOT } }
				}
			}
			ROOT = { change_variable = { which = "var_senate_majority" value = 1 } }
		}
		divide_variable = { which = "var_senate_majority" value = 2 }
		change_variable = { which = "var_senate_majority" value = 0.5 }
		
		# Calculate the representation of each faction in the Senate
		character_event = { id = maintenance.1000 }		# Pro-Centralization
		character_event = { id = maintenance.1050 }		
		character_event = { id = maintenance.1100 }		# Anti-Centralization
		character_event = { id = maintenance.1150 }
		character_event = { id = maintenance.1200 }		# Devolve Local Government
		character_event = { id = maintenance.1250 }
		character_event = { id = maintenance.1300 }		# This one doesn't exist apparently?
		character_event = { id = maintenance.1350 }
		character_event = { id = maintenance.1400 }		# Independence
		character_event = { id = maintenance.1450 }
		character_event = { id = maintenance.1500 }		# No Confidence
		character_event = { id = maintenance.1550 }
		character_event = { id = maintenance.1600 }		# Abolish Legislature
		character_event = { id = maintenance.1650 }
		character_event = { id = maintenance.1700 }		# Monarchist
		character_event = { id = maintenance.1750 }
		character_event = { id = maintenance.1800 }		# Republican
		character_event = { id = maintenance.1850 }
		character_event = { id = maintenance.1900 }		# Technocratic
		character_event = { id = maintenance.1950 }
	}
}
# Wipe senators for the pro-centralization faction
character_event = {
	id = maintenance.1000
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_pro_centralization } }
		NOT = { is_variable_equal = { which = "var_pro_centralization" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_pro_centralization" value = -1 } }
}
# Calculate senators for the pro-centralization faction
character_event = {
	id = maintenance.1050
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_pro_centralization }
	}
	
	immediate = {
		set_variable = { which = "var_pro_centralization" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_pro_centralization }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_pro_centralization" value = 1 } }
			}
		}
		subtract_variable = { which = "var_pro_centralization" which = "var_senate_majority" }
	}
}
# Wipe senators for the anti-centralization faction
character_event = {
	id = maintenance.1100
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_anti_centralization } }
		NOT = { is_variable_equal = { which = "var_anti_centralization" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_anti_centralization" value = -1 } }
}
# Calculate senators for the anti-centralization faction
character_event = {
	id = maintenance.1150
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_anti_centralization }
	}
	
	immediate = {
		set_variable = { which = "var_anti_centralization" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_anti_centralization }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_anti_centralization" value = 1 } }
			}
		}
		subtract_variable = { which = "var_anti_centralization" which = "var_senate_majority" }
	}
}
# Wipe senators for the devolve local government faction
character_event = {
	id = maintenance.1200
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_devolve_local_government } }
		NOT = { is_variable_equal = { which = "var_devolve_local_government" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_devolve_local_government" value = -1 } }
}
# Calculate senators for the devolve local government faction
character_event = {
	id = maintenance.1250
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_devolve_local_government }
	}
	
	immediate = {
		set_variable = { which = "var_devolve_local_government" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_devolve_local_government }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_devolve_local_government" value = 1 } }
			}
		}
		subtract_variable = { which = "var_devolve_local_government" which = "var_senate_majority" }
	}
}
# Wipe senators for the independence faction
character_event = {
	id = maintenance.1400
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_independence } }
		NOT = { is_variable_equal = { which = "var_independence" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_independence" value = -1 } }
}
# Calculate senators for the independence faction
character_event = {
	id = maintenance.1450
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_independence }
	}
	
	immediate = {
		set_variable = { which = "var_independence" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_independence }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_independence" value = 1 } }
			}
		}
		subtract_variable = { which = "var_independence" which = "var_senate_majority" }
	}
}
# Wipe senators for the no confidence faction
character_event = {
	id = maintenance.1500
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_no_confidence } }
		NOT = { is_variable_equal = { which = "var_no_confidence" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_no_confidence" value = -1 } }
}
# Calculate senators for the no confidence faction
character_event = {
	id = maintenance.1550
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_no_confidence }
	}
	
	immediate = {
		set_variable = { which = "var_no_confidence" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_no_confidence }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_no_confidence" value = 1 } }
			}
		}
		subtract_variable = { which = "var_no_confidence" which = "var_senate_majority" }
	}
}
# Wipe senators for the anti-legislature faction
character_event = {
	id = maintenance.1600
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_abolish_legislature } }
		NOT = { is_variable_equal = { which = "var_abolish_legislature" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_abolish_legislature" value = -1 } }
}
# Calculate senators for the anti-legislature faction
character_event = {
	id = maintenance.1650
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_abolish_legislature }
	}
	
	immediate = {
		set_variable = { which = "var_abolish_legislature" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_abolish_legislature }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_abolish_legislature" value = 1 } }
			}
		}
		subtract_variable = { which = "var_abolish_legislature" which = "var_senate_majority" }
	}
}

# Wipe senators for the monarchist faction
character_event = {
	id = maintenance.1700
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_monarchist } }
		NOT = { is_variable_equal = { which = "var_monarchist" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_monarchist" value = -1 } }
}
# Calculate senators for the monarchist faction
character_event = {
	id = maintenance.1750
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_monarchist }
	}
	
	immediate = {
		set_variable = { which = "var_monarchist" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_monarchist }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_monarchist" value = 1 } }
			}
		}
		subtract_variable = { which = "var_monarchist" which = "var_senate_majority" }
	}
}
# Wipe senators for the republican faction
character_event = {
	id = maintenance.1800
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_republican } }
		NOT = { is_variable_equal = { which = "var_republican" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_republican" value = -1 } }
}
# Calculate senators for the republican faction
character_event = {
	id = maintenance.1850
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_republican }
	}
	
	immediate = {
		set_variable = { which = "var_republican" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_republican }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_republican" value = 1 } }
			}
		}
		subtract_variable = { which = "var_republican" which = "var_senate_majority" }
	}
}
# Wipe senators for the technocratic faction
character_event = {
	id = maintenance.1900
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes
	
	trigger = {
		NOT = { any_vassal = { in_faction = faction_technocratic } }
		NOT = { is_variable_equal = { which = "var_technocratic" value = -1 } }
	}
	
	immediate = { set_variable = { which = "var_technocratic" value = -1 } }
}
# Calculate senators for the technocratic faction
character_event = {
	id = maintenance.1950
	hide_window = yes
	is_triggered_only = yes
	
	only_rulers = yes
	
	trigger = {
		higher_tier_than = count
		any_vassal = { in_faction = faction_technocratic }
	}
	
	immediate = {
		set_variable = { which = "var_technocratic" value = 0 }
		any_vassal = {
			limit = { in_faction = faction_technocratic }
			any_realm_province = {
				limit = {
					owner = {
						OR = {
							character = PREVPREV
							is_liege_or_above = PREVPREV
						}
					}
					OR = {
						empire = { holder_scope = { character = ROOT } }
						kingdom = { holder_scope = { character = ROOT } }
						duchy = { holder_scope = { character = ROOT } }
					}
				}
				ROOT = { change_variable = { which = "var_technocratic" value = 1 } }
			}
		}
		subtract_variable = { which = "var_technocratic" which = "var_senate_majority" }
	}
}