set_autonomy_cancel = {
	scope = country
	type = scripted_gui
	saved_scopes = {
        player
		subject
    }

	is_shown = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}
	
	is_valid = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}

	effect = {
		scope:subject = {
			#s#ave_scope_as = autonomy_marked_nation
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_annexed
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_puppet
						}
						OR = {
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_protectorate
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			remove_variable = turning_into_protectorate
			remove_variable = turning_into_dominion
			remove_variable = turning_into_colony
			remove_variable = turning_into_puppet
			remove_variable = turning_into_annexed
			set_variable = { name = autonomy_progress value = 0 }
			remove_variable = marked_nation
		}
		scope:player = {
			remove_modifier = subject_autonomy_being_lowered_overlord_modifier
			remove_modifier = subject_autonomy_being_increased_overlord_modifier
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_increased_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_increased_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_lowered_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_lowered_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			
			remove_variable = autonomy_origin_nation
		}
	}
}

set_autonomy_annexed = {
	scope = country
	type = scripted_gui
	saved_scopes = {
        player
		subject
    }

	is_shown = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}
	
	is_valid = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}

	effect = {
		scope:subject = {
			scope:player = {
				if = {
					limit = {
						NOT = { has_variable = autonomy_progress_reduction_rate }
					}
					set_variable = {
						name = autonomy_progress_reduction_rate
						value = 3
					}
				}
				set_local_variable = {
					name = overlord_autonomy_progress_reduction_rate
					value = var:autonomy_progress_reduction_rate
				}
			}
			remove_variable = turning_into_protectorate
			remove_variable = turning_into_dominion
			remove_variable = turning_into_colony
			remove_variable = turning_into_puppet
			remove_variable = turning_into_annexed
			remove_variable = autonomy_progress
			#s#ave_scope_as = autonomy_marked_nation
			remove_variable = marked_nation
			set_local_variable = {
				name = autonomy_change_resistance_modifiers
				value = 0
			}
			if = {
				limit = {
					always = yes
					#NOT = { has_variable = autonomy_change_resistance }
				}
				set_variable = {
					name = autonomy_change_resistance
					value = 0
				}
			}
			if = {
				limit = {
					country_rank >= rank_value:major_power
				}
				change_local_variable = {
					name = autonomy_change_resistance_modifiers
					add = 1
				}
			}
			change_variable = {
				name = autonomy_change_resistance
				add = local_var:autonomy_change_resistance_modifiers
			}
			change_local_variable = {
				name = overlord_autonomy_progress_reduction_rate
				subtract = var:autonomy_change_resistance
			}
			set_variable = {
				name = autonomy_progress_reduction_rate
				value = local_var:overlord_autonomy_progress_reduction_rate
			}
			if = {
				limit = {
					NOT = {
						OR = {
							has_variable = turning_into_annexed
							has_variable = turning_into_puppet
							has_variable = turning_into_colony
							has_variable = turning_into_dominion
							has_variable = turning_into_protectorate
						}
					}
				}
				set_variable = { name = autonomy_progress value = 100 }
			}
			
			set_variable = turning_into_annexed
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_annexed
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_puppet
						}
						OR = {
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_protectorate
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			###
			if = {
				limit = {
					scope:subject = {
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				add_modifier = {
					name = subject_autonomy_being_lowered_modifier
					days = -1 # permanent until removed
				}
			}
			else = {
				add_modifier = {
					name = subject_autonomy_being_increased_modifier
					days = -1 # permanent until removed
				}
			}
		}
		scope:player = {
			remove_modifier = subject_autonomy_being_lowered_overlord_modifier
			remove_modifier = subject_autonomy_being_increased_overlord_modifier
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_increased_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_increased_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_lowered_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_lowered_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			remove_variable = autonomy_origin_nation
		}
	}
}

set_autonomy_puppet = {
	scope = country
	type = scripted_gui
	saved_scopes = {
        player
		subject
    }

	is_shown = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}
	
	is_valid = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}

	effect = {
		scope:subject = {
			scope:player = {
				if = {
					limit = {
						NOT = { has_variable = autonomy_progress_reduction_rate }
					}
					set_variable = {
						name = autonomy_progress_reduction_rate
						value = 3
					}
				}
				set_local_variable = {
					name = overlord_autonomy_progress_reduction_rate
					value = var:autonomy_progress_reduction_rate
				}
			}
			#s#ave_scope_as = autonomy_marked_nation
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_annexed
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_puppet
						}
						OR = {
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_protectorate
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			###
			remove_variable = turning_into_protectorate
			remove_variable = turning_into_dominion
			remove_variable = turning_into_colony
			remove_variable = turning_into_puppet
			remove_variable = turning_into_annexed
			remove_variable = autonomy_progress
			#s#ave_scope_as = autonomy_marked_nation
			remove_variable = marked_nation
			
			set_local_variable = {
				name = autonomy_change_resistance_modifiers
				value = 0
			}
			if = {
				limit = {
					always = yes
					#NOT = { has_variable = autonomy_change_resistance }
				}
				set_variable = {
					name = autonomy_change_resistance
					value = 0
				}
			}
			if = {
				limit = {
					country_rank >= rank_value:major_power
				}
				change_local_variable = {
					name = autonomy_change_resistance_modifiers
					add = 1
				}
			}
			change_variable = {
				name = autonomy_change_resistance
				add = local_var:autonomy_change_resistance_modifiers
			}
			change_local_variable = {
				name = overlord_autonomy_progress_reduction_rate
				subtract = var:autonomy_change_resistance
			}
			set_variable = {
				name = autonomy_progress_reduction_rate
				value = local_var:overlord_autonomy_progress_reduction_rate
			}
			if = {
				limit = {
					NOT = {
						OR = {
							has_variable = turning_into_annexed
							has_variable = turning_into_puppet
							has_variable = turning_into_colony
							has_variable = turning_into_dominion
							has_variable = turning_into_protectorate
						}
					}
				}
				set_variable = { name = autonomy_progress value = 100 }
			}
			set_variable = turning_into_puppet
			if = {
				limit = {
					scope:subject = {
						OR = {
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				add_modifier = {
					name = subject_autonomy_being_lowered_modifier
					days = -1 # permanent until removed
				}
			}
			else = {
				add_modifier = {
					name = subject_autonomy_being_increased_modifier
					days = -1 # permanent until removed
				}
			}
		}
		scope:player = {
			remove_modifier = subject_autonomy_being_lowered_overlord_modifier
			remove_modifier = subject_autonomy_being_increased_overlord_modifier
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_increased_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_increased_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_lowered_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_lowered_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			remove_variable = autonomy_origin_nation
		}
	}
}

set_autonomy_colony = {
	scope = country
	type = scripted_gui
	saved_scopes = {
        player
		subject
    }

	is_shown = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject_type = subject_type_colony }
		}
	}
	
	is_valid = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject_type = subject_type_colony }
		}
	}

	effect = {
		scope:subject = {
			scope:player = {
				if = {
					limit = {
						NOT = { has_variable = autonomy_progress_reduction_rate }
					}
					set_variable = {
						name = autonomy_progress_reduction_rate
						value = 3
					}
				}
				set_local_variable = {
					name = overlord_autonomy_progress_reduction_rate
					value = var:autonomy_progress_reduction_rate
				}
			}
			#s#ave_scope_as = autonomy_marked_nation
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_annexed
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_puppet
						}
						OR = {
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_protectorate
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			###
			remove_variable = turning_into_protectorate
			remove_variable = turning_into_dominion
			remove_variable = turning_into_colony
			remove_variable = turning_into_puppet
			remove_variable = turning_into_annexed
			remove_variable = autonomy_progress
			#s#ave_scope_as = autonomy_marked_nation
			remove_variable = marked_nation
			
			set_local_variable = {
				name = autonomy_change_resistance_modifiers
				value = 0
			}
			if = {
				limit = {
					always = yes
					#NOT = { has_variable = autonomy_change_resistance }
				}
				set_variable = {
					name = autonomy_change_resistance
					value = 0
				}
			}
			if = {
				limit = {
					country_rank >= rank_value:major_power
				}
				change_local_variable = {
					name = autonomy_change_resistance_modifiers
					add = 1
				}
			}
			change_variable = {
				name = autonomy_change_resistance
				add = local_var:autonomy_change_resistance_modifiers
			}
			change_local_variable = {
				name = overlord_autonomy_progress_reduction_rate
				subtract = var:autonomy_change_resistance
			}
			set_variable = {
				name = autonomy_progress_reduction_rate
				value = local_var:overlord_autonomy_progress_reduction_rate
			}
			if = {
				limit = {
					NOT = {
						OR = {
							has_variable = turning_into_annexed
							has_variable = turning_into_puppet
							has_variable = turning_into_colony
							has_variable = turning_into_dominion
							has_variable = turning_into_protectorate
						}
					}
				}
				set_variable = { name = autonomy_progress value = 100 }
			}
			set_variable = turning_into_colony
			if = {
				limit = {
					scope:subject = {
						OR = {
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				add_modifier = {
					name = subject_autonomy_being_lowered_modifier
					days = -1 # permanent until removed
				}
			}
			else = {
				add_modifier = {
					name = subject_autonomy_being_increased_modifier
					days = -1 # permanent until removed
				}
			}
		}
		scope:player = {
			remove_modifier = subject_autonomy_being_lowered_overlord_modifier
			remove_modifier = subject_autonomy_being_increased_overlord_modifier
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_increased_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_increased_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_lowered_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_lowered_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			remove_variable = autonomy_origin_nation
		}
	}
}

set_autonomy_dominion = {
	scope = country
	type = scripted_gui
	saved_scopes = {
        player
		subject
    }

	is_shown = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}
	
	is_valid = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}

	effect = {
		scope:subject = {
			scope:player = {
				if = {
					limit = {
						NOT = { has_variable = autonomy_progress_reduction_rate }
					}
					set_variable = {
						name = autonomy_progress_reduction_rate
						value = 3
					}
				}
				set_local_variable = {
					name = overlord_autonomy_progress_reduction_rate
					value = var:autonomy_progress_reduction_rate
				}
			}
			#s#ave_scope_as = autonomy_marked_nation
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_annexed
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_puppet
						}
						OR = {
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_protectorate
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			###
			remove_variable = turning_into_protectorate
			remove_variable = turning_into_dominion
			remove_variable = turning_into_colony
			remove_variable = turning_into_puppet
			remove_variable = turning_into_annexed
			remove_variable = autonomy_progress
			#s#ave_scope_as = autonomy_marked_nation
			remove_variable = marked_nation
			
			set_local_variable = {
				name = autonomy_change_resistance_modifiers
				value = 0
			}
			if = {
				limit = {
					always = yes
					#NOT = { has_variable = autonomy_change_resistance }
				}
				set_variable = {
					name = autonomy_change_resistance
					value = 0
				}
			}
			if = {
				limit = {
					country_rank >= rank_value:major_power
				}
				change_local_variable = {
					name = autonomy_change_resistance_modifiers
					add = 1
				}
			}
			change_variable = {
				name = autonomy_change_resistance
				add = local_var:autonomy_change_resistance_modifiers
			}
			change_local_variable = {
				name = overlord_autonomy_progress_reduction_rate
				subtract = var:autonomy_change_resistance
			}
			set_variable = {
				name = autonomy_progress_reduction_rate
				value = local_var:overlord_autonomy_progress_reduction_rate
			}
			if = {
				limit = {
					NOT = {
						OR = {
							has_variable = turning_into_annexed
							has_variable = turning_into_puppet
							has_variable = turning_into_colony
							has_variable = turning_into_dominion
							has_variable = turning_into_protectorate
						}
					}
				}
				set_variable = { name = autonomy_progress value = 100 }
			}
			set_variable = turning_into_dominion
			if = {
				limit = {
					scope:subject = {
						OR = {
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				add_modifier = {
					name = subject_autonomy_being_lowered_modifier
					days = -1 # permanent until removed
				}
			}
			else = {
				add_modifier = {
					name = subject_autonomy_being_increased_modifier
					days = -1 # permanent until removed
				}
			}
		}
		scope:player = {
			remove_modifier = subject_autonomy_being_lowered_overlord_modifier
			remove_modifier = subject_autonomy_being_increased_overlord_modifier
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_increased_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_increased_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_lowered_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_lowered_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			remove_variable = autonomy_origin_nation
		}
	}
}

set_autonomy_protectorate = {
	scope = country
	type = scripted_gui
	saved_scopes = {
        player
		subject
    }

	is_shown = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}
	
	is_valid = {
		any_country = {
			limit = {
				has_variable = marked_nation
			}
			NOT = { is_subject = no }
		}
	}

	effect = {
		scope:subject = {
			scope:player = {
				if = {
					limit = {
						NOT = { has_variable = autonomy_progress_reduction_rate }
					}
					set_variable = {
						name = autonomy_progress_reduction_rate
						value = 3
					}
				}
				set_local_variable = {
					name = overlord_autonomy_progress_reduction_rate
					value = var:autonomy_progress_reduction_rate
				}
			}
			#s#ave_scope_as = autonomy_marked_nation
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_annexed
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_puppet
						}
						OR = {
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_dominion
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_protectorate
							is_subject_type = subject_type_tributary
						}
					}
				}
				remove_modifier = subject_autonomy_being_lowered_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_colony
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_dominion
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			if = {
				limit = {
					scope:subject = {
						OR = {
							has_variable = turning_into_protectorate
						}
						OR = {
							is_subject_type = subject_type_puppet
							is_subject_type = subject_type_vassal
							is_subject_type = subject_type_personal_union
							is_subject_type = subject_type_colony
							is_subject_type = subject_type_dominion
						}
					}
				}
				remove_modifier = subject_autonomy_being_increased_modifier
			}
			###
			remove_variable = turning_into_protectorate
			remove_variable = turning_into_dominion
			remove_variable = turning_into_colony
			remove_variable = turning_into_puppet
			remove_variable = turning_into_annexed
			remove_variable = autonomy_progress
			#s#ave_scope_as = autonomy_marked_nation
			remove_variable = marked_nation
			
			set_local_variable = {
				name = autonomy_change_resistance_modifiers
				value = 0
			}
			if = {
				limit = {
					always = yes
					#NOT = { has_variable = autonomy_change_resistance }
				}
				set_variable = {
					name = autonomy_change_resistance
					value = 0
				}
			}
			if = {
				limit = {
					country_rank >= rank_value:major_power
				}
				change_local_variable = {
					name = autonomy_change_resistance_modifiers
					add = 1
				}
			}
			change_variable = {
				name = autonomy_change_resistance
				add = local_var:autonomy_change_resistance_modifiers
			}
			change_local_variable = {
				name = overlord_autonomy_progress_reduction_rate
				subtract = var:autonomy_change_resistance
			}
			set_variable = {
				name = autonomy_progress_reduction_rate
				value = local_var:overlord_autonomy_progress_reduction_rate
			}
			if = {
				limit = {
					NOT = {
						OR = {
							has_variable = turning_into_annexed
							has_variable = turning_into_puppet
							has_variable = turning_into_colony
							has_variable = turning_into_dominion
							has_variable = turning_into_protectorate
						}
					}
				}
				set_variable = { name = autonomy_progress value = 100 }
			}
			set_variable = turning_into_protectorate
			add_modifier = {
				name = subject_autonomy_being_increased_modifier
				days = -1 # permanent until removed
			}
		}
		scope:player = {
			remove_modifier = subject_autonomy_being_lowered_overlord_modifier
			remove_modifier = subject_autonomy_being_increased_overlord_modifier
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_increased_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_increased_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			every_subject_or_below = {
				limit = {
					has_modifier = subject_autonomy_being_lowered_modifier
				}
				overlord = {
					add_modifier = {
						name = subject_autonomy_being_lowered_overlord_modifier
						days = -1 # permanent until removed
					}
				}
			}
			remove_variable = autonomy_origin_nation
		}
	}
}