#####
##### PROPAGANDA SYSTEM #####
#####
SOV_achievement_propaganda_counter_effect = {
	if = {
		limit = {
			check_variable = { var = propaganda_counter value = 1 compare = less_than }
		}
		set_variable = { var = propaganda_counter value = 1 } #setup variable
	}
	else = {
		add_to_variable = {
			propaganda_counter = 1
		}
	}
}
SOV_pick_propaganda_campaign_slot_effect = {
	hidden_effect = {
		if = {
			limit = {
				check_variable = { SOV_poster_left_icon_frame = SOV_propaganda_empty_slot_index }
			}
			set_variable = { SOV_poster_left_icon_frame = SOV_new_propaganda_campaign }
			#Ageing posters
			set_variable = { SOV_propaganda_left_poster_to_age = SOV_poster_left_icon_frame } #This is used to prevent the system to break if a propaganda campaign is cancelled
			hidden_effect = {
				country_event = { id = NSB_soviet_propaganda_capmaign_events.101 days = SOV_default_propaganda_campaign_old_days }
				#set_country_flag = { flag = SOV_propaganda_left_days_remaining_flag days = SOV_default_propaganda_campaign_duration } #Surprise! This doesnät work with vars :D  Maybe in the future
				if = { #Debug -> Faster time
					limit = {
						has_country_flag = SOV_debug_fast_propaganda_campaigns_flag
					}
					set_country_flag = { flag = SOV_propaganda_left_days_remaining_flag days = 5 }
				}
				else = { #Regular time
					set_country_flag = { flag = SOV_propaganda_left_days_remaining_flag days = 180 } #Need code support to use a var here
				}
			}
		}
		else_if = {
			limit = {
				check_variable = { SOV_poster_center_icon_frame = SOV_propaganda_empty_slot_index }
			}
			set_variable = { SOV_poster_center_icon_frame = SOV_new_propaganda_campaign }
			#Ageing posters
			set_variable = { SOV_propaganda_center_poster_to_age = SOV_poster_center_icon_frame } #This is used to prevent the system to break if a propaganda campaign is cancelled
			hidden_effect = {
				country_event = { id = NSB_soviet_propaganda_capmaign_events.102 days = SOV_default_propaganda_campaign_old_days }
				#set_country_flag = { flag = SOV_propaganda_center_days_remaining_flag days = SOV_default_propaganda_campaign_duration } #Surprise! This doesnät work with vars :D  Maybe in the future
				if = { #Debug -> Faster time
					limit = {
						has_country_flag = SOV_debug_fast_propaganda_campaigns_flag
					}
					set_country_flag = { flag = SOV_propaganda_center_days_remaining_flag days = 5 }
				}
				else = { #Regular time
					set_country_flag = { flag = SOV_propaganda_center_days_remaining_flag days = 180 } #Need code support to use a var here
				}
			}
		}
		else_if = {
			limit = {
				check_variable = { SOV_poster_right_icon_frame = SOV_propaganda_empty_slot_index }
			}
			set_variable = { SOV_poster_right_icon_frame = SOV_new_propaganda_campaign }
			#Ageing posters
			set_variable = { SOV_propaganda_right_poster_to_age = SOV_poster_right_icon_frame } #This is used to prevent the system to break if a propaganda campaign is cancelled
			hidden_effect = {
				country_event = { id = NSB_soviet_propaganda_capmaign_events.103 days = SOV_default_propaganda_campaign_old_days }
				#set_country_flag = { flag = SOV_propaganda_right_days_remaining_flag days = SOV_default_propaganda_campaign_duration } #Surprise! This doesnät work with vars :D  Maybe in the future
				if = { #Debug -> Faster time
					limit = {
						has_country_flag = SOV_debug_fast_propaganda_campaigns_flag
					}
					set_country_flag = { flag = SOV_propaganda_right_days_remaining_flag days = 5 }
				}
				else = { #Regular time
					set_country_flag = { flag = SOV_propaganda_right_days_remaining_flag days = 180 } #Need code support to use a var here
				}
			}
		}
		set_variable = { SOV_new_propaganda_campaign = 0 } # Reset var
		add_to_variable = { SOV_propaganda_campaigns_taken = 1 } #Update propaganda campaigns counter
	}
}

SOV_reset_propaganda_campaign_slot_effect = {
	hidden_effect = {
		if = {
			limit = {
				check_variable = { SOV_poster_left_icon_frame = SOV_propaganda_campaign_to_remove }
			}
			set_variable = { SOV_poster_left_icon_frame = SOV_propaganda_empty_slot_index }
			#Clear aged posters
			clear_variable = SOV_propaganda_left_poster_to_age
			clr_country_flag = SOV_propaganda_left_slot_is_old_flag
		}
		else_if = {
			limit = {
				check_variable = { SOV_poster_center_icon_frame = SOV_propaganda_campaign_to_remove }
			}
			set_variable = { SOV_poster_center_icon_frame = SOV_propaganda_empty_slot_index }
			#Clear aged posters
			clear_variable = SOV_propaganda_center_poster_to_age
			clr_country_flag = SOV_propaganda_center_slot_is_old_flag
		}
		else_if = { #This could be turned into an else, but leaving it like this to hopefully make it easier to see
			limit = {
				check_variable = { SOV_poster_right_icon_frame = SOV_propaganda_campaign_to_remove }
			}
			set_variable = { SOV_poster_right_icon_frame = SOV_propaganda_empty_slot_index }
			#Clear aged posters
			clear_variable = SOV_propaganda_right_poster_to_age
			clr_country_flag = SOV_propaganda_right_slot_is_old_flag
		}
		set_variable = { SOV_propaganda_campaign_to_remove = 0 } # Reset var
	}
}

SOV_unlock_propaganda_campaign_slot_effect = {
	hidden_effect = {
		if = {
			limit = {
				check_variable = { SOV_poster_left_icon_frame = SOV_propaganda_locked_slot_index }
			}
			set_variable = { SOV_poster_left_icon_frame = SOV_propaganda_empty_slot_index }
		}
		else_if = {
			limit = {
				check_variable = { SOV_poster_center_icon_frame = SOV_propaganda_locked_slot_index }
			}
			set_variable = { SOV_poster_center_icon_frame = SOV_propaganda_empty_slot_index }
		}
		else = {
			set_variable = { SOV_poster_right_icon_frame = SOV_propaganda_empty_slot_index }
		}
	}
}

SOV_increase_propaganda_campaign_cost_effect = {
	multiply_variable = { SOV_propaganda_cost = SOV_propaganda_cost_increase } #Using generic var SOV_propaganda_cost so that we only need to change this operation to tweak all propagandas' cost escalation
}


#####
##### PARANOIA SYSTEM #####
#####

SOV_paranoia_very_low_increase_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_very_low_increase_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_very_low_increase }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_low_increase_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_low_increase_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_low_increase }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_medium_increase_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_medium_increase_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_medium_increase }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_high_increase_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_high_increase_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_high_increase }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_very_high_increase_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_very_high_increase_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_very_high_increase }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_low_decrease_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_low_decrease_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_low_decrease }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_medium_decrease_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_medium_decrease_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_medium_decrease }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_high_decrease_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_high_decrease_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_high_decrease }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_very_high_decrease_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		custom_effect_tooltip = SOV_paranoia_very_high_decrease_effect_tt
		add_to_variable = { SOV_paranoia = SOV_paranoia_very_high_decrease }
		SOV_paranoia_clamp_and_update_ui_effect = yes
	}
}

SOV_paranoia_increase_based_on_unit_leader_level_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		#custom_effect_tooltip = SOV_paranoia_increase_based_on_unit_leader_level_effect_tt
		add_to_variable = { SOV_paranoia = SOV_unit_leader_level }
		SOV_paranoia_clamp_and_update_ui_effect = yes
		clear_variable = SOV_unit_leader_level
	}
}

SOV_paranoia_decrease_based_on_unit_leader_level_effect = {
	if = {
		limit = {
			has_country_flag = SOV_paranoia_system_active_flag
		}
		#custom_effect_tooltip = SOV_paranoia_decrease_based_on_unit_leader_level_effect_tt
		subtract_from_variable = { SOV_paranoia = SOV_unit_leader_level }
		SOV_paranoia_clamp_and_update_ui_effect = yes
		clear_variable = SOV_unit_leader_level
	}
}

SOV_paranoia_modifier_increase_effect = { #Called on_weekly to apply the modifier
	custom_effect_tooltip = SOV_paranoia_modifier_increase_effect_tt
	set_temp_variable = { SOV_paranoia_total_weekly_modifier_increase = SOV_paranoia_modifier_increase } #Get default weekly modifier increase value
	multiply_temp_variable = { SOV_paranoia_total_weekly_modifier_increase = SOV_paranoia_weekly_modifiers_amount } #Multiply default weekly modifier increase by the number of active weekly modifiers
	add_to_variable = { SOV_paranoia = SOV_paranoia_total_weekly_modifier_increase } #Apply the result and add to Paranoia
	SOV_paranoia_clamp_and_update_ui_effect = yes
}

SOV_paranoia_modifier_update_gui_tooltip = { #Only updates the tooltip in the decision GUI
	ROOT = { set_variable = { SOV_paranoia_weekly_modifier_ui = SOV_paranoia_modifier_increase } } #Get default weekly modifier increase value
	ROOT = { multiply_variable = { SOV_paranoia_weekly_modifier_ui = SOV_paranoia_weekly_modifiers_amount } } #Multiply default weekly modifier increase by the number of active weekly modifiers
}

SOV_paranoia_clamp_and_update_ui_effect = {
	clamp_variable = {
		var = SOV_paranoia
		min = 0
		max = 100
	}
	SOV_update_ui_effect = yes
}

SOV_paranoia_increase_purge_counter_regular = {
	add_to_variable = { SOV_regular_purge_counter = 1 }
	add_to_variable = { SOV_total_purge_counter = 1 }
}

SOV_paranoia_increase_purge_counter_great = {
	add_to_variable = { SOV_great_purge_counter = 1 }
	add_to_variable = { SOV_total_purge_counter = 1 }
}

SOV_initiate_paranoia_effect = { #PARANOIA SYSTEM STARTS, GOOD LUCK COMRADE
	set_country_flag = SOV_paranoia_system_active_flag #Uh oh...

	#Stalin gets paranoid
	add_country_leader_trait = stalin_political_paranoia

	#NKVD chiefs will make Political Paranoia worse
	remove_trait = {
		character = SOV_genrikh_yagoda
		slot = political_advisor
		trait = head_of_the_nkvd_yagoda_initial
	}
	add_trait = {
		character = SOV_genrikh_yagoda
		slot = political_advisor
		trait = head_of_the_nkvd_yagoda
	}

	remove_trait = {
		character = SOV_nikolay_yezhov
		slot = political_advisor
		trait = head_of_the_nkvd_yezhov_initial
	}
	add_trait = {
		character = SOV_nikolay_yezhov
		slot = political_advisor
		trait = head_of_the_nkvd_yezhov
	}

	remove_trait = {
		character = SOV_lavrenty_beriya
		slot = political_advisor
		trait = head_of_the_nkvd_beriya_initial
	}
	add_trait = {
		character = SOV_lavrenty_beriya
		slot = political_advisor
		trait = head_of_the_nkvd_beriya
	}
}

SOV_remove_paranoia_effect = { # NO MORE PARANOIA, COMRADES
	custom_effect_tooltip = SOV_stalins_cult_of_personality_tt
	set_variable = { SOV_paranoia = 0 }
	set_variable = { SOV_paranoia_modifier_increase = 0 }
	SOV_paranoia_clamp_and_update_ui_effect = yes
	clr_country_flag = SOV_paranoia_system_active_flag
	clr_country_flag = SOV_paranoia_modifier_active_flag
	if = {
		limit = {
			has_country_leader_with_trait = stalin_political_paranoia_advanced
		}
		remove_country_leader_trait = stalin_political_paranoia_advanced
	}
	else_if = {
		limit = {
			has_country_leader_with_trait = stalin_political_paranoia
		}
		remove_country_leader_trait = stalin_political_paranoia
	}

	#No more paranoia from NKVD chiefs
	hidden_effect = {
		if = {
			limit = {
				has_character = SOV_genrikh_yagoda
			}

			#Remove old trait
			if = {
				limit = {
					SOV_genrikh_yagoda = {
						has_trait = head_of_the_nkvd_yagoda_initial
					}
				}
				remove_trait = {
					character = SOV_genrikh_yagoda
					trait = head_of_the_nkvd_yagoda_initial
					slot = political_advisor
				}
			}
			else_if = {
				limit = {
					SOV_genrikh_yagoda = {
						has_trait = head_of_the_nkvd_yagoda
					}
				}
				remove_trait = {
					character = SOV_genrikh_yagoda
					trait = head_of_the_nkvd_yagoda
					slot = political_advisor
				}
			}

			#Add final trait
			add_trait = {
				character = SOV_genrikh_yagoda
				trait = head_of_the_nkvd_yagoda_no_paranoia
				slot = political_advisor
			}
			set_can_be_fired_in_advisor_role = {
				character = SOV_genrikh_yagoda 
				value = yes
			}
		}

		if = {
			limit = {
				has_character = SOV_nikolay_yezhov
			}

			#Remove old trait
			if = {
				limit = {
					SOV_nikolay_yezhov = {
						has_trait = head_of_the_nkvd_yezhov_initial
					}
				}
				remove_trait = {
					character = SOV_nikolay_yezhov
					trait = head_of_the_nkvd_yezhov_initial
					slot = political_advisor
				}
			}
			else_if = {
				limit = {
					SOV_nikolay_yezhov = {
						has_trait = head_of_the_nkvd_yezhov
					}
				}
				remove_trait = {
					character = SOV_nikolay_yezhov
					trait = head_of_the_nkvd_yezhov
					slot = political_advisor
				}
			}
			
			#Add final trait
			add_trait = {
				character = SOV_nikolay_yezhov
				trait = head_of_the_nkvd_yezhov_no_paranoia
				slot = political_advisor
			}
			set_can_be_fired_in_advisor_role = {
				character = SOV_nikolay_yezhov 
				value = yes
			}
		}

		if = {
			limit = {
				has_character = SOV_lavrenty_beriya
			}

			#Remove old trait
			if = {
				limit = {
					SOV_lavrenty_beriya = {
						has_trait = head_of_the_nkvd_beriya_initial
					}
				}
				remove_trait = {
					character = SOV_lavrenty_beriya
					trait = head_of_the_nkvd_beriya_initial
					slot = political_advisor
				}
			}
			else_if = {
				limit = {
					SOV_lavrenty_beriya = {
						has_trait = head_of_the_nkvd_beriya
					}
				}
				remove_trait = {
					character = SOV_lavrenty_beriya
					trait = head_of_the_nkvd_beriya
					slot = political_advisor
				}
			}
			
			#Add final trait
			add_trait = {
				character = SOV_lavrenty_beriya
				trait = head_of_the_nkvd_beriya_no_paranoia
				slot = political_advisor
			}
			set_can_be_fired_in_advisor_role = {
				character = SOV_lavrenty_beriya 
				value = yes
			}
		}
	}
}

#UI
SOV_update_ui_effect = {
	if = {
		limit = {
			check_variable = { SOV_paranoia = 0 }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 1 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 5 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 2 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 10 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 3 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 15 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 4 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 20 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 5 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 25 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 6 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 30 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 7 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 35 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 8 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 40 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 9 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 45 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 10 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 50 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 11 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 55 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 12 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 60 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 13 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 65 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 14 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 70 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 15 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 75 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 16 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 80 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 17 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 85 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 18 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 90 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 19 }
	}
	else_if = {
		limit = {
			check_variable = { SOV_paranoia = 95 compare = less_than_or_equals }
		}
		set_variable = { SOV_paranoia_main_paranoiameter = 20 }
	}
	else = { # paranoia > 95
		set_variable = { SOV_paranoia_main_paranoiameter = 21 }
	}
}

#EFFECTS FROM PURGES
SOV_paranoia_daunt_generals_effect = {
	set_temp_variable = { temp_targets_index = 0 }

	while_loop_effect = {
		limit = {
			check_variable = { temp_targets_index = 5 compare = less_than }
			any_army_leader = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_army }
			}
		}
		random_army_leader = {
			limit = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_army }
			}
			add_unit_leader_trait = trait_SOV_cowed_by_stalin_army
		}
		add_to_temp_variable = { temp_targets_index = 1 }
		#log = "LOOP ROUND [?temp_targets_index]"
	}

	custom_effect_tooltip = SOV_daunt_generals_tt
}

SOV_paranoia_daunt_generals_minor_effect = {
	set_temp_variable = { temp_targets_index = 0 }

	while_loop_effect = {
		limit = {
			check_variable = { temp_targets_index = 2 compare = less_than }
			any_army_leader = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_army }
			}
		}
		random_army_leader = {
			limit = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_army }
			}
			add_unit_leader_trait = trait_SOV_cowed_by_stalin_army
		}
		add_to_temp_variable = { temp_targets_index = 1 }
		#log = "LOOP ROUND [?temp_targets_index]"
	}

	custom_effect_tooltip = SOV_daunt_generals_minor_tt
}

SOV_paranoia_daunt_admirals_effect = {
	set_temp_variable = { temp_targets_index = 0 }

	while_loop_effect = {
		limit = {
			check_variable = { temp_targets_index = 5 compare = less_than }
			any_navy_leader = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_navy }
			}
		}
		random_navy_leader = {
			limit = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_navy }
			}
			add_unit_leader_trait = trait_SOV_cowed_by_stalin_navy
		}
		add_to_temp_variable = { temp_targets_index = 1 }
		#log = "LOOP ROUND [?temp_targets_index]"
	}

	custom_effect_tooltip = SOV_daunt_admirals_tt
}

SOV_paranoia_daunt_admirals_minor_effect = {
	set_temp_variable = { temp_targets_index = 0 }

	while_loop_effect = {
		limit = {
			check_variable = { temp_targets_index = 2 compare = less_than }
			any_navy_leader = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_navy }
			}
		}
		random_navy_leader = {
			limit = {
				NOT = { has_character_flag = SOV_targeted_for_purge_flag }
				NOT = { has_trait = trait_SOV_cowed_by_stalin_navy }
			}
			add_unit_leader_trait = trait_SOV_cowed_by_stalin_navy
		}
		add_to_temp_variable = { temp_targets_index = 1 }
		#log = "LOOP ROUND [?temp_targets_index]"
	}

	custom_effect_tooltip = SOV_daunt_admirals_minor_tt
}


SOV_purge_character_effect = { #Expected to be called in the character scope
	set_character_flag = SOV_character_purged_flag
	PREV = { retire_character = PREV }
}

SOV_paranoia_update_portraits_ui = { #Expected to be called in the country scope
	if = {
		limit = {
			NOT = { has_character = SOV_grigory_zinovyev }
			check_variable = { SOV.SOV_paranoia_photo_zinovyev = 8 compare = not_equals }
		}
		set_variable = { SOV.SOV_paranoia_photo_zinovyev = 8 }
		set_country_flag = SOV_added_purged_portrait_flag
	}
	if = {
		limit = {
			NOT = { has_character = SOV_karl_radek }
			check_variable = { SOV.SOV_paranoia_photo_radek = 9 compare = not_equals }
		}
		set_variable = { SOV.SOV_paranoia_photo_radek = 9 }
		set_country_flag = SOV_added_purged_portrait_flag
	}
	if = {
		limit = {
			NOT = { has_character = SOV_nikolay_bukharin }
			check_variable = { SOV.SOV_paranoia_photo_bukharin = 10 compare = not_equals }
		}
		set_variable = { SOV.SOV_paranoia_photo_bukharin = 10 }
		set_country_flag = SOV_added_purged_portrait_flag
	}
	if = {
		limit = {
			NOT = { has_character = SOV_lev_trotsky }
			check_variable = { SOV.SOV_paranoia_photo_trotsky = 11 compare = not_equals }
		}
		set_variable = { SOV.SOV_paranoia_photo_trotsky = 11 }
		set_country_flag = SOV_added_purged_portrait_flag
	}
	if = {
		limit = {
			NOT = { has_character = SOV_mikhail_tukhachevsky }
			check_variable = { SOV.SOV_paranoia_photo_tukhachevsky = 12 compare = not_equals }
		}
		set_variable = { SOV.SOV_paranoia_photo_tukhachevsky = 12 }
		set_country_flag = SOV_added_purged_portrait_flag
	}
	if = { #Generic
		limit = {
			NOT = { has_country_flag = SOV_added_purged_portrait_flag } #IMPORTANT: This flag needs to be cleared after the purge character effect in eevery event purging characters
		}
		set_temp_variable = { SOV_generic_portrait_frame = SOV_paranoia_portrait_index }
		if = { #There are 6 portraits and 12 slots, so second round of generic portraits
			limit = {
				check_variable = { SOV_paranoia_portrait_index = 6 compare = greater_than }
			}
			subtract_from_temp_variable = { SOV_generic_portrait_frame = 6 }
		}
		add_to_temp_variable = { SOV_generic_portrait_frame = 1 }

		add_to_array = {
			array = SOV_paranoia_generic_portraits_array
			value = SOV_generic_portrait_frame
			index = SOV_paranoia_portrait_index
		}
		
		add_to_variable = { SOV_paranoia_portrait_index = 1 }
	}
	clr_country_flag = SOV_added_purged_portrait_flag
}

SOV_clean_purge_targets_effect = {
	hidden_effect = {
		every_character = {
			limit = {
				has_character_flag = SOV_targeted_for_purge_flag
			}
			clr_character_flag = SOV_targeted_for_purge_flag
		}
	}
}

SOV_clear_unavoidable_purge_flags_effect = {
	hidden_effect = {
		if = {
			limit = {
				has_country_flag = SOV_purge_event_unavoidable_flag
			}
			clr_country_flag = SOV_purge_event_unavoidable_flag
		}
	}
}

SOV_add_random_character_to_purge_targets_effect = {
	if = {
		limit = {
			check_variable = { SOV_specific_random_purge_targets_override = 0 compare = less_than }
		}
		add_to_variable = { SOV_specific_random_purge_targets_override = 2 } #Var starts at -1 so it's necessary to take it into account
	}
	else = {
		add_to_variable = { SOV_specific_random_purge_targets_override = 1 }
	}
}

SOV_no_random_characters_for_this_purge_effect = {
	set_variable = { SOV_specific_random_purge_targets_override = 0 }
}

SOV_reset_specific_random_target_override_effect = {
	set_variable = { SOV_specific_random_purge_targets_override = -1 }
}


###############
### PHASE 1 ###
###############
# Check for a randomly triggered purge, and set the appropriate severity (regular purge/great purge) [RANDOM PURGE & SIZE]
SOV_check_for_a_new_purge_effect = { #Useless now. Leaving it just in case
	if = { # REGULAR PURGE
		limit = {
			SOV_paranoia_is_medium_level = yes
		}
		SOV_manage_a_regular_purge_effect = yes
	}
	else_if = { # GREAT PURGE
		limit = {
			SOV_paranoia_is_high_level = yes
		}
		SOV_manage_a_great_purge_effect = yes
	}
}


###############
### PHASE 2 ###
###############
#Set the Area to be purged (admin, army, navy, airforce) [AREA]

# REGULAR PURGE
SOV_manage_a_regular_purge_effect = { # Phase 2: Set the Area to be purged (admin, army, navy, airforce), then set the actual targets (people), then trigger event (Phase 3)
	# Randomly choose an area to purge
	SOV_paranoia_low_decrease_effect = yes
	random_list = {
		15 = { # ADMINISTRATION
			modifier = { #Don't repeat last purge please
				factor = 0.1
				check_variable = { SOV_last_regular_purge_area = 1 }
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					is_character_slot = political_advisor
					has_character_flag = SOV_potential_target_flag
				}
			}
			modifier = { 
				factor = 20
				has_global_flag = SOV_has_early_war
			}
			modifier = { #Yagoda is in the NKVD -> He shall be purged
				factor = 1.5
				has_idea = SOV_genrikh_yagoda
			}
			modifier = { #There are no purges available
				factor = 0
				NOT = {
					any_character = {
						is_character_slot = political_advisor
						SOV_character_can_be_purged = yes
					}
				}
				has_idea = SOV_understaffed_bureaucracy
				has_idea = SOV_lavrenty_beriya
			}
			SOV_pick_regular_purge_administration_type_effect = yes
		}
		25 = { # ARMY
			modifier = { #Don't repeat last purge please
				factor = 0.1
				check_variable = { SOV_last_regular_purge_area = 2 }
			}
			modifier = { 
				factor = 20
				has_war = yes
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					has_character_flag = SOV_potential_target_flag
					OR = {
						is_army_leader = yes
						has_army_ledger = yes
					}
				}
			}
			modifier = { #There are no purges available
				factor = 0
				NOT = {
					any_army_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
				has_idea = SOV_anti_soviet_military_thinking_banned_army
				has_idea = SOV_training_activities_curtailed
			}
			SOV_pick_regular_purge_army_type_effect = yes
		}
		30 = { # NAVY
			modifier = { #Don't repeat last purge please
				factor = 0.1
				check_variable = { SOV_last_regular_purge_area = 3 }
			}
			modifier = { 
				factor = 0.5
				has_global_flag = SOV_has_early_war
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_navy_leader = {
					has_character_flag = SOV_potential_target_flag
				}
			}
			modifier = { #There are no purges available
				factor = 0
				NOT = {
					any_navy_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
				has_idea = SOV_anti_soviet_military_thinking_banned_navy
				has_idea = SOV_shipyard_staff_purged
			}
			SOV_pick_regular_purge_navy_type_effect = yes
		}
		30 = { # AIRFORCE
			modifier = { #Don't repeat last purge please
				factor = 0.1
				check_variable = { SOV_last_regular_purge_area = 4 }
			}
			modifier = { 
				factor = 0.5
				has_global_flag = SOV_has_early_war
			}
			modifier = { #Someone is a potential targets
				factor = 2
				any_character = {
					has_air_ledger = yes
					has_character_flag = SOV_potential_target_flag
				}
			}
			modifier = { #There are no purges available
				factor = 0
				has_idea = SOV_anti_soviet_military_thinking_banned_airforce
				has_idea = SOV_ground_based_training
			}
			SOV_pick_regular_purge_airforce_type_effect = yes
		}
	}
}

# GREAT PURGE
SOV_manage_a_great_purge_effect = {
	# Randomly choose an area to purge
	random_list = {
		15 = { # ADMINISTRATION
			modifier = { #Don't repeat last purge please
				factor = 0
				check_variable = { SOV_last_great_purge_area = 1 }
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					is_character_slot = political_advisor
					has_character_flag = SOV_potential_target_flag
				}
			}
			modifier = { #There are no purges available
				factor = 0
				NOT = {
					any_character = {
						is_character_slot = political_advisor
						NOT = { #NKVDs have their own purge
							is_character = SOV_genrikh_yagoda
							is_character = SOV_nikolay_yezhov
							is_character = SOV_lavrenty_beriya
						}
						SOV_character_can_be_purged = yes
					}
				}
			}
			SOV_pick_great_purge_administration_type_effect = yes
		}
		60 = { # ARMY
			modifier = {
				factor = 0.2 #lmao, keep them dying please
				check_variable = { SOV_last_great_purge_area = 2 }
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					has_character_flag = SOV_potential_target_flag
					OR = {
						is_army_leader = yes
						has_army_ledger	= yes
					}
				}
			}
			modifier = { #There are no purges available
				factor = 0
				has_idea = SOV_purged_junior_army_officers_3
				NOT = {
					any_character = {
						OR = {
							is_army_leader = yes
							has_army_ledger = yes
						}
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
			}
			SOV_pick_great_purge_army_type_effect = yes
		}
		5 = { # NAVY
			modifier = { #Don't repeat last purge please
				factor = 0
				check_variable = { SOV_last_great_purge_area = 3 }
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					OR = {
						is_navy_leader = yes
						has_navy_ledger = yes
					}
					has_character_flag = SOV_potential_target_flag
				}
			}
			modifier = { #There are no purges available
				factor = 0
				has_idea = SOV_purged_junior_navy_officers_3
				NOT = {
					any_navy_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
			}
			SOV_pick_great_purge_navy_type_effect = yes
		}
		20 = { # AIRFORCE
			modifier = { #Don't repeat last purge please
				factor = 0
				check_variable = { SOV_last_great_purge_area = 4 }
			}
			modifier = { #Someone is a potential targets
				factor = 2
				OR = {
					NOT = { has_country_flag = SOV_purged_airforce_chiefs_flag } #This purge can only happen "randomly" once, unless some air advisors are forcefully targeted in a focus or something
					any_character = {
						has_air_ledger = yes
						has_character_flag = SOV_potential_target_flag
					}
				}
			}
			modifier = { #We have closed a lot of designers man
				factor = 0.5
				has_country_flag = SOV_purged_airforce_chiefs_flag  #This purge can only happen "randomly" once, unless some air advisors are forcefully targeted in a focus or something
				NOT = {
					any_character = {
						has_air_ledger = yes
						has_character_flag = SOV_potential_target_flag
					}
				}
				count_triggers = {
					amount = 2
					has_country_flag = SOV_mig_design_bureau_dissolved_flag
					has_country_flag = SOV_ilyushin_design_bureau_dissolved_flag
					has_country_flag = SOV_tupolev_design_bureau_dissolved_flag
					has_country_flag = SOV_yakovlev_design_bureau_dissolved_flag
				}
			}

			modifier = { #There are no purges available
				factor = 0
				has_country_flag = SOV_purged_airforce_chiefs_flag  #This purge can only happen "randomly" once, unless some air advisors are forcefully targeted in a focus or something
				NOT = {
					any_character = {
						has_air_ledger = yes
						has_character_flag = SOV_potential_target_flag
					}
				}
				has_country_flag = SOV_mig_design_bureau_dissolved_flag
				has_country_flag = SOV_ilyushin_design_bureau_dissolved_flag
				has_country_flag = SOV_tupolev_design_bureau_dissolved_flag
				has_country_flag = SOV_yakovlev_design_bureau_dissolved_flag
			}
			SOV_pick_great_purge_airforce_type_effect = yes
		}
	}
}


###############
### PHASE 3 ###
###############
# Set the purge type [TYPE]

SOV_pick_regular_purge_administration_type_effect = {
	set_temp_variable = { SOV_random_purge_test = 0 } #Testing purposes (to see if/when all those conditions fail)
	random_list = {
		33 = { # ADVISOR PURGED
			modifier = { #No pastia no comiercio
				factor = 0
				NOT = {
					any_character = {
						is_character_slot = political_advisor
						SOV_character_can_be_purged = yes
					}
				}
			}
			modifier = { #If someone is already suspicious, then it's more likely that a purge to get rid of him happens
				factor = 3
				any_character = {
					is_character_slot = political_advisor
					has_character_flag = SOV_potential_target_flag
				}
			}
			modifier = {
				factor = 0.5
				has_war = yes
			}
			SOV_trigger_regular_purge_advisor_accused_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		33 = { # CIVIL SERVANTS PURGED
			modifier = {
				factor = 0
				has_idea = SOV_understaffed_bureaucracy
			}
			modifier = {
				factor = 20
				has_global_flag = SOV_has_early_war
			}
			SOV_trigger_regular_purge_civil_servants_purged_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		33 = { # NKVD PURGE
			modifier = { #Beriya is huevo pocho
				factor = 0
				has_idea = SOV_lavrenty_beriya
			}
			modifier = {
				factor = 20
				has_global_flag = SOV_has_early_war
			}
			modifier = { #Yagoda is in the NKVD -> He shall be purged
				factor = 3
				has_idea = SOV_genrikh_yagoda
				date > 1938.1.1
			}
			modifier = { #Yezhov is in the NKVD -> He should be purged
				factor = 2
				has_idea = SOV_nikolay_yezhov
				date > 1939.1.1
			}
			SOV_trigger_regular_purge_nkvd_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
	}
	if = { #TEST
		limit = {
			check_variable = { SOV_random_purge_test = 0 }
		}
		#log = "RANDOM PURGE FAILED. NO VALID PURGE AVAILABLE"
	}
}

SOV_pick_great_purge_administration_type_effect = {
	#JUST IN CASE WE ADD AN EXTRA GREAT PURGE. COULD BE DELETED AND THE EXTRA STEP SKIPPED IF NOT
	if = {
		limit = { #As long as someone can be purged, go ahead
			any_character = {
				is_character_slot = political_advisor
				NOT = { #NKVDs have their own purge
					is_character = SOV_genrikh_yagoda
					is_character = SOV_nikolay_yezhov
					is_character = SOV_lavrenty_beriya
				}
				SOV_character_can_be_purged = yes
			}
		}
		SOV_trigger_great_purge_moscow_trial_effect = yes
	}
	else = {
		#log = "What are you doing the Great Purge of administration is not available you madman"
	}
}

SOV_pick_regular_purge_army_type_effect = {
	set_temp_variable = { SOV_random_purge_test = 0 } #Testing purposes (to see if/when all those conditions fail)
	random_list = {
		25 = { # GENERAL ACCUSED
			modifier = { #No pastia no comiercio
				factor = 0
				NOT = {
					any_army_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_army_leader = {
					is_corps_commander = yes
					has_character_flag = SOV_potential_target_flag
				}
			}
			modifier = { #Someone is a potential target
				factor = 20
				has_war = yes
			}
			SOV_regular_purge_general_accused_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		25 = { # CONSPIRACY IN THE ACADEMY - ARMY
			modifier = {
				factor = 0
				has_idea = SOV_anti_soviet_military_thinking_banned_army
			}
			SOV_regular_purge_conspiracy_in_the_academy_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		25 = { # ANTI-SABOTEUR CAMPAIGN - ARMY
			modifier = {
				factor = 0
				has_idea = SOV_training_activities_curtailed
			}
			SOV_regular_purge_anti_saboteur_campaign_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		25 = { # GENERAL ACCUSES GENERAL
			modifier = { #No pastia no comiercio
				factor = 0
				NOT = { #TODO_MANU: Need to check for 2 guys fulfilling these conditions here
					any_army_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					OR = {
						is_army_leader = yes
						has_army_ledger = yes
					}
					has_character_flag = SOV_potential_target_flag
				}
			}
			SOV_regular_purge_general_accuses_general_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
	}
	if = { #TEST
		limit = {
			check_variable = { SOV_random_purge_test = 0 }
		}
		#log = "RANDOM PURGE FAILED. NO VALID PURGE AVAILABLE"
	}
}

SOV_pick_great_purge_army_type_effect = {
#Pick from the different possibilities
	set_temp_variable = { SOV_random_purge_test = 0 } #Testing purposes (to see if/when all those conditions fail)
	random_list = {
		50 = { # JUNIOR ARMY OFFICERS PURGED
			modifier = {
				factor = 0
				has_idea = SOV_purged_junior_army_officers_3
			}
			SOV_great_purge_army_junior_officers_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		50 = { # TRIAL OF THE GENERALS
			modifier = { #No pastia no comiercio
				factor = 0
				NOT = {
					any_army_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					OR = {
						is_army_leader = yes
						has_army_ledger = yes
					}
					has_character_flag = SOV_potential_target_flag
				}
			}
			SOV_great_purge_army_trial_of_the_generals_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
	}
	if = { #TEST
		limit = {
			check_variable = { SOV_random_purge_test = 0 }
		}
		#log = "RANDOM PURGE FAILED. NO VALID PURGE AVAILABLE"
	}
}

SOV_pick_regular_purge_navy_type_effect = {
	set_temp_variable = { SOV_random_purge_test = 0 } #Testing purposes (to see if/when all those conditions fail)
	random_list = {
		33 = { # ADMIRAL ACCUSED
			modifier = { #No pastia no comiercio
				factor = 0
				NOT = {
					any_navy_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_navy_leader = {
					has_character_flag = SOV_potential_target_flag
				}
			}
			SOV_trigger_regular_purge_admiral_accused_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		33 = { # CONSPIRACY IN THE ACADEMY - NAVY
			modifier = {
				factor = 0
				has_idea = SOV_anti_soviet_military_thinking_banned_navy
			}
			SOV_trigger_regular_purge_naval_conspiracy_in_the_academy_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		33 = { # ANTI-SABOTEUR CAMPAIGN - NAVY
			modifier = {
				factor = 0
				has_idea = SOV_shipyard_staff_purged
			}
			SOV_trigger_regular_purge_naval_anti_saboteur_campaign_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
	}
	if = { #TEST
		limit = {
			check_variable = { SOV_random_purge_test = 0 }
		}
		#log = "RANDOM PURGE FAILED. NO VALID PURGE AVAILABLE"
	}
}

SOV_pick_great_purge_navy_type_effect = {
	set_temp_variable = { SOV_random_purge_test = 0 } #Testing purposes (to see if/when all those conditions fail)
	random_list = {
		50 = { # JUNIOR NAVY OFFICERS PURGED
			modifier = {
				factor = 0
				has_idea = SOV_purged_junior_navy_officers_3
			}
			SOV_trigger_great_purge_junior_naval_officers_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		50 = { # TRIAL OF THE ADMIRALS
			modifier = { #No pastia no comiercio
				factor = 0
				NOT = {
					any_navy_leader = {
						SOV_character_can_be_purged = yes
						NOT = { has_trait = trait_SOV_stalinist }
					}
				}
			}
			modifier = { #Someone is a potential target
				factor = 2
				any_character = {
					OR = {
						is_navy_leader = yes
						has_navy_ledger = yes
					}
					has_character_flag = SOV_potential_target_flag
				}
			}
			SOV_trigger_great_purge_trial_of_the_admirals_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
	}
	if = { #TEST
		limit = {
			check_variable = { SOV_random_purge_test = 0 }
		}
		#log = "RANDOM PURGE FAILED. NO VALID PURGE AVAILABLE"
	}
}

SOV_pick_regular_purge_airforce_type_effect = {
	set_temp_variable = { SOV_random_purge_test = 0 } #Testing purposes (to see if/when all those conditions fail)
	random_list = {
		25 = { # CONSPIRACY IN THE ACADEMY - AIRFORCE
			modifier = {
				factor = 0
				has_idea = SOV_anti_soviet_military_thinking_banned_airforce
			}
			SOV_trigger_regular_purge_airforce_conspiracy_in_the_academy_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
		25 = { # ANTI-SABOTEUR CAMPAIGN - AIRFORCE
			modifier = {
				factor = 0
				has_idea = SOV_ground_based_training
			}
			SOV_trigger_regular_purge_airforce_anti_saboteur_campaign_effect = yes
			add_to_temp_variable = { SOV_random_purge_test = 1 } #TEST
		}
	}
	if = { #TEST
		limit = {
			check_variable = { SOV_random_purge_test = 0 }
		}
		#log = "RANDOM PURGE FAILED. NO VALID PURGE AVAILABLE"
	}
}

SOV_pick_great_purge_airforce_type_effect = {
	random_list = {
		50 = { # DISSOLVE DESIGN BUREAU
			modifier = { #Nothing left to dissolve
				factor = 0
				has_country_flag = SOV_mig_design_bureau_dissolved_flag
				has_country_flag = SOV_ilyushin_design_bureau_dissolved_flag
				has_country_flag = SOV_tupolev_design_bureau_dissolved_flag
				has_country_flag = SOV_yakovlev_design_bureau_dissolved_flag
			}
			SOV_trigger_great_purge_design_bureau_dissolved_effect = yes
		}
		50 = { # PURGE ARIFORCE CHIEFS
			modifier = { #No pastia no comiercio
				factor = 0
				has_country_flag = SOV_purged_airforce_chiefs_flag  #This purge can only happen "randomly" once, unless some air advisors are forcefully targeted in a focus or something
				NOT = {
					any_character = {
						has_air_ledger = yes
						has_character_flag = SOV_potential_target_flag
					}
				}
			}
			modifier = { #Someone is a potential target
				factor = 2
				OR = {
					NOT = { has_country_flag = SOV_purged_airforce_chiefs_flag } #This purge can only happen "randomly" once, unless some air advisors are forcefully targeted in a focus or something
					any_character = {
						has_air_ledger = yes
						has_character_flag = SOV_potential_target_flag
					}
				}
			}
			SOV_trigger_great_purge_airforce_chiefs_effect = yes
		}
	}
}

###############
### PHASE 4 ###
###############
#Set the actual targets (people) [TARGETS] then trigger the appropriate event (Phase 5)

### ADVISOR ACCUSED OF TREASON ###
SOV_trigger_regular_purge_advisor_accused_effect = {
	hidden_effect = {
		if = { #PRIORITIZE FORCED TARGETS
			limit ={
				any_character = {
					is_character_slot = political_advisor
					SOV_character_can_be_purged = yes
					has_character_flag = SOV_potential_target_flag
				}
			}
			random_character = {
				limit = {
					is_character_slot = political_advisor
					SOV_character_can_be_purged = yes
					has_character_flag = SOV_potential_target_flag
				}
				set_character_flag = SOV_targeted_for_purge_flag
				clr_character_flag = SOV_potential_target_flag
				ROOT = { set_variable = { SOV_purged_character_for_loc = PREV } }
				#ROOT = { log = "I am [THIS.GetName] // var is [?ROOT.SOV_purged_character_for_loc.GetName]" } #Debug
			}
		}
		else_if = { #If no one to prio, then pick a random one
			limit = {
				any_character = {
					is_character_slot = political_advisor
					SOV_character_can_be_purged = yes
				}
			}
			random_character = {
				limit = {
					is_character_slot = political_advisor
					SOV_character_can_be_purged = yes
				}
				set_character_flag = SOV_targeted_for_purge_flag
				ROOT = { set_variable = { SOV_purged_character_for_loc = PREV } }
				#ROOT = { log = "This is [THIS.GetName] // PREV is [PREV.GetName] // ROOT is [ROOT.GetName] // VAR is [?ROOT.SOV_purged_character_for_loc.GetName]" } #Debug
			}
		}
		#log = "Person is [?SOV_purged_character_for_loc.GetName]" #Debug
	}

	country_event = NSB_soviet_paranoia_system_events.11 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 1 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

### CIVIL SERVANTS PURGED
SOV_trigger_regular_purge_civil_servants_purged_effect = {
	country_event = NSB_soviet_paranoia_system_events.12 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 1 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

### PURGE THE NKVD ###
SOV_trigger_regular_purge_nkvd_effect = {
	country_event = NSB_soviet_paranoia_system_events.13 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 1 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

### GREAT PURGE - MOSCOW TRIALS ###
SOV_trigger_great_purge_moscow_trial_effect = {
	hidden_effect = {
		set_temp_variable = { temp_targets_index = 0 } #Targeted chars counter

		#If someone is already TARGETED, prio them first.
		if = {
			limit = {
				any_character = {
					is_character_slot = political_advisor
					has_character_flag = SOV_targeted_for_purge_flag
					SOV_character_can_be_purged = yes
				}
			}
			#These guys will be automatically targeted in the event, so we increase the var and only fill in with random chars if necessary
			if = {
				limit = {
					check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
				}
				#log = "I will purge some random guys" #Debug
			}
			else = {
				#log = "I will use default people to purge" #Debug
				every_character = {
					limit = {
						is_character_slot = political_advisor
						has_character_flag = SOV_targeted_for_purge_flag
						SOV_character_can_be_purged = yes
					}
					PREV = { add_to_temp_variable = { temp_targets_index = 1 } }
				}
			}
		}

		set_temp_variable = { temp_people_to_purge = 0 } #Number of random people that can be purged

		#log = "VAR OVERRIDE is [?SOV_specific_random_purge_targets_override]" #Debug

		if = { #If a specific amount of random chars to be purged has been set, then use that number
			limit = {
				check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
			}
			set_temp_variable = { temp_people_to_purge = SOV_specific_random_purge_targets_override }
			SOV_reset_specific_random_target_override_effect = yes #Reset this var (value = -1) so that it is not used again until specified otherwise
			#log = "Using OVERRIDE" #Debug
		}
		else = { #If no specific amount of random chars to be purged is set, then use default value
			set_temp_variable = { temp_people_to_purge = SOV_paranoia_great_purge_administration_number_of_targets }
			#log = "Using DEFAULT" #Debug
		}

		#log = " PEOPLE TO PURGE IS [?temp_people_to_purge]" #Debug
		#log = " INDEX IS = [?temp_targets_index]" #Debug
		if = {
			limit = {
				check_variable = { temp_people_to_purge = 0 compare = greater_than } #Random people to purge is not 0
				check_variable = { temp_targets_index < temp_people_to_purge } #There is room for random advisors to be purged (for when there are pre-set targets)
			}
			#log = "Random people" #Debug
			while_loop_effect = {
				limit = {
					check_variable = { temp_targets_index = temp_people_to_purge compare = less_than }
				}

				if = { #PRIORITIZE POTENTIAL TARGETS
					limit ={
						any_character = {
							is_character_slot = political_advisor
							SOV_character_can_be_purged = yes
							has_character_flag = SOV_potential_target_flag
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
						}
					}
					#log ="PRIOING STUFF" #Debug
					random_character = {
						limit = {
							is_character_slot = political_advisor
							SOV_character_can_be_purged = yes
							has_character_flag = SOV_potential_target_flag
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
						}
						set_character_flag = SOV_targeted_for_purge_flag
						clr_character_flag = SOV_potential_target_flag

						#log = "TARGET FIXED: [THIS.GetName]" #Debug
					}

				}
				else_if = { #If no one to prio, then pick a random one
					limit = {
						any_character = {
							is_character_slot = political_advisor
							SOV_character_can_be_purged = yes
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
						}
					}
					#log = "RANDOM STUFF" #Debug
					random_character = {
						limit = {
							is_character_slot = political_advisor
							SOV_character_can_be_purged = yes
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
						}
						set_character_flag = SOV_targeted_for_purge_flag
						#log = "RANDOM ADVISOR PICK: [THIS.GetName]" #Debug
					}

				}
				else = {
					#log = "RAN OUT OF POLITICAL ADVISORS, DAMN!" #Debug
				}
				add_to_temp_variable = { temp_targets_index = 1 }
				#log = "LOOP: temp_targets_index = [?temp_targets_index]" #Debug
			}
		}
		else = {
			#log = "No Random People" #Debug
		}
	}

	country_event = NSB_soviet_paranoia_system_events.19 # TRIGGER EVENT

	set_variable = { SOV_last_great_purge_area = 1 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

### GENERAL ACCUSED OF TREASON ###
SOV_regular_purge_general_accused_effect = {
	hidden_effect = {
		if = { #PRIORITIZE FORCED TARGETS
			limit ={
				any_army_leader = {
					has_character_flag = SOV_potential_target_flag
					SOV_character_can_be_purged = yes
				}
			}
			random_army_leader = {
				limit = {
					has_character_flag = SOV_potential_target_flag
					SOV_character_can_be_purged = yes
				}
				set_character_flag = SOV_targeted_for_purge_flag
				clr_character_flag = SOV_potential_target_flag
				SOV = {
					set_variable = { SOV_accused_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accused_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_purged_character_for_loc = PREV } }
				ROOT = { log = "This is [THIS.GetName] // PREV is [PREV.GetName] // ROOT is [ROOT.GetName] // VAR is [?ROOT.SOV_purged_character_for_loc.GetName]" }
			}
		}
		else = {
			random_army_leader = {
				limit = {
					SOV_character_can_be_purged = yes
					NOT = { has_trait = trait_SOV_stalinist }
				}
				set_character_flag = SOV_targeted_for_purge_flag
				SOV = {
					set_variable = { SOV_accused_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accused_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_purged_character_for_loc = PREV } }
				ROOT = { log = "This is [THIS.GetName] // PREV is [PREV.GetName] // ROOT is [ROOT.GetName] // VAR is [?ROOT.SOV_purged_character_for_loc.GetName]" }
			}
		}
	}

	var:SOV_purged_character_for_loc = { unit_leader_event = NSB_soviet_paranoia_system_events.21 } # TRIGGER EVENT
	set_variable = { SOV_last_regular_purge_area = 2 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

### CONSPIRACY IN THE ACADEMY ###
SOV_regular_purge_conspiracy_in_the_academy_effect = {
	country_event = NSB_soviet_paranoia_system_events.22 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 2 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

### ANTI-SABOTEUR CAMPAIGN ###
SOV_regular_purge_anti_saboteur_campaign_effect = {
	country_event = NSB_soviet_paranoia_system_events.23 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 2 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

### GENERAL ACCUSES GENERAL ###
SOV_regular_purge_general_accuses_general_effect = {
	hidden_effect = {
		# ACCUSER
		if = { # PRIO stalinist or politically connected
			limit ={
				any_army_leader = {
					OR = {
						has_trait = trait_SOV_stalinist
						has_trait = politically_connected
					}
					skill < 4
				}
			}
			random_army_leader = {
				limit = {
					OR = {
						has_trait = trait_SOV_stalinist
						has_trait = politically_connected
					}
					skill < 4
				}
				set_character_flag = SOV_purge_accuser_flag
				SOV = {
					set_variable = { SOV_accuser_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accuser_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_accuser_character_for_loc = PREV } }
			}
		}
		else = {
			random_army_leader = {
				set_character_flag = SOV_purge_accuser_flag
				SOV = {
					set_variable = { SOV_accuser_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accuser_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_accuser_character_for_loc = PREV } }
			}
		}

		# ACCUSED
		if = { # PRIO more level
			limit ={
				any_army_leader = {
					SOV_character_can_be_purged = yes
					OR = {
						has_character_flag = SOV_potential_target_flag
						AND = {
							NOT = { has_trait = trait_SOV_stalinist }
							NOT = { has_trait = politically_connected }
						}
					}
					skill > 3
				}
			}
			random_army_leader = {
				limit = {
					SOV_character_can_be_purged = yes
					OR = {
						has_character_flag = SOV_potential_target_flag
						AND = {
							NOT = { has_trait = trait_SOV_stalinist }
							NOT = { has_trait = politically_connected }
						}
					}
					skill > 3
				}
				set_character_flag = SOV_purge_accused_flag
				SOV = {
					set_variable = { SOV_accused_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accused_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_accused_character_for_loc = PREV } }

			}
		}
		else = {
			random_army_leader = {
				set_character_flag = SOV_purge_accused_flag
				SOV = {
					set_variable = { SOV_accused_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accused_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_accused_character_for_loc = PREV } }
			}
		}

		set_variable = { SOV_combined_unit_leader_level = SOV_accuser_unit_leader_level }
		add_to_variable = { SOV_combined_unit_leader_level = SOV_accused_unit_leader_level }
	}

	var:SOV_accused_character_for_loc = { unit_leader_event = NSB_soviet_paranoia_system_events.24 } # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 2 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_great_purge_army_junior_officers_effect = {
	country_event = NSB_soviet_paranoia_system_events.28 # TRIGGER EVENT (PHASE 5)

	set_variable = { SOV_last_great_purge_area = 2 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}


### TRIAL OF THE GENERALS ###
SOV_great_purge_army_trial_of_the_generals_effect = {
	hidden_effect = {
		set_temp_variable = { temp_targets_index = 0 } #Targeted chars counter

		#If someone is already TARGETED, prio them first.
		if = {
			limit = {
				any_character = {
					OR = {
						is_army_leader = yes #Generals + Marshals
						has_army_ledger = yes
					}
					has_character_flag = SOV_targeted_for_purge_flag
					SOV_character_can_be_purged = yes
				}
			}
			#These guys will be automatically targeted in the event, so we increase the var and only fill in with random chars if necessary
			if = {
				limit = {
					check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
				}
				#log = "I will purge some random guys" #Debug
			}
			else = {
				#log = "I will use default people to purge" #Debug
				every_character = {
					limit = {
						OR = {
							is_army_leader = yes #Generals + Marshals
							has_army_ledger = yes
						}
						has_character_flag = SOV_targeted_for_purge_flag
						SOV_character_can_be_purged = yes
					}
					PREV = { add_to_temp_variable = { temp_targets_index = 1 } }
				}
			}
		}

		set_temp_variable = { temp_people_to_purge = 0 } #Number of random people that can be purged

		#log = "VAR OVERRIDE is [?SOV_specific_random_purge_targets_override]" #Debug

		if = { #If a specific amount of random chars to be purged has been set, then use that number
			limit = {
				check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
			}
			set_temp_variable = { temp_people_to_purge = SOV_specific_random_purge_targets_override }
			SOV_reset_specific_random_target_override_effect = yes #Reset this var (value = -1) so that it is not used again until specified otherwise
			#log = "Using OVERRIDE" #Debug
		}
		else = { #If no specific amount of random chars to be purged is set, then use default value
			set_temp_variable = { temp_people_to_purge = SOV_paranoia_great_purge_army_number_of_targets }
			#log = "Using DEFAULT" #Debug
		}

		# PICK RANDOM PEOPLE IF APPROPRIATE
		if = {
			limit = {
				check_variable = { temp_people_to_purge = 0 compare = greater_than } #Random people to purge is not 0
				check_variable = { temp_targets_index < temp_people_to_purge } #There is room for random advisors to be purged (for when there are pre-set targets)
			}

			# PICK 1 FIELD MARSHAL
			hidden_effect = {
				if = { # Pick an already-targeted Marshal, if any
					limit = {
						any_army_leader = {
							is_field_marshal = yes
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
					}
					random_army_leader = {
						limit = {
							is_field_marshal = yes
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
						set_character_flag = SOV_targeted_for_purge_flag
						clr_character_flag = SOV_potential_target_flag
						#ROOT = { log = "Chosen Marshal - Prioritized - [PREV.GetName]" } #Debug
					}
					add_to_temp_variable = { temp_targets_index = 1 }
				}
				else_if = { # Pick an already-targeted Army advisor, if any
					limit = {
						any_character = {
							has_army_ledger = yes
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
					}
					random_character = {
						limit = {
							has_army_ledger = yes
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
						set_character_flag = SOV_targeted_for_purge_flag
						clr_character_flag = SOV_potential_target_flag
						#ROOT = { log = "Chosen Advisor - Prioritized - [PREV.GetName]" } Debug
					}
					add_to_temp_variable = { temp_targets_index = 1 }
				}
				else_if = { # Pick a random marshal
					limit = {
						any_army_leader = {
							is_field_marshal = yes
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
					}
					random_army_leader = {
						limit = {
							is_field_marshal = yes
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
						set_character_flag = SOV_targeted_for_purge_flag
						#ROOT = { log = "Chosen Marshal - Random - [PREV.GetName]" } #Debug
					}
					add_to_temp_variable = { temp_targets_index = 1 }
				}
				else_if = { # Pick a random army advisor
					limit = {
						any_character = {
							has_army_ledger = yes
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
					}
					random_character = {
						limit = {
							has_army_ledger = yes
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
						set_character_flag = SOV_targeted_for_purge_flag
						#ROOT = { log = "Chosen Advisor - Random - [PREV.GetName]" } #Debug
					}
					add_to_temp_variable = { temp_targets_index = 1 }
				}

				while_loop_effect = {
					limit = {
						check_variable = { temp_targets_index = temp_people_to_purge compare = less_than }
					}
					if = { #PRIORITIZE FORCED TARGETS - GENERALS
						limit ={
							any_army_leader = {
								is_corps_commander = yes
								has_character_flag = SOV_potential_target_flag
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
						}
						#log = "Prioritized General" #Debug
						random_army_leader = {
							limit = {
								is_corps_commander = yes
								has_character_flag = SOV_potential_target_flag
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
							set_character_flag = SOV_targeted_for_purge_flag
							clr_character_flag = SOV_potential_target_flag

							#ROOT = { log = "Chosen General - Prioritized - [PREV.GetName]" } #Debug
						}
						#log = "TARGET FIXED - General" #Debug
					}
					else_if = { #PRIORITIZE FORCED TARGETS - MARSHALS + ARMY ADVISORS
						limit ={
							any_character = {
								OR = {
									is_field_marshal = yes
									has_army_ledger = yes
								}
								has_character_flag = SOV_potential_target_flag
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
						}
						random_character = {
							limit = {
								OR = {
									is_field_marshal = yes
									has_army_ledger = yes
								}
								has_character_flag = SOV_potential_target_flag
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
							set_character_flag = SOV_targeted_for_purge_flag
							clr_character_flag = SOV_potential_target_flag

							#ROOT = { log = "Chosen Marshal/Advisor - Prioritized - [PREV.GetName]" } #Debug
						}
						#log = "TARGET FIXED - Marshal/Advisor" #Debug
					}
					else_if = { #FILL TARGET WITH RANDOM GENERALS (Could have an extra step, checking for non-Stalinist ppl, then this one)
						limit = {
							any_army_leader = {
								is_corps_commander = yes
								NOT = { has_character_flag = SOV_targeted_for_purge_flag }
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
						}
						random_army_leader = {
							limit = {
								is_corps_commander = yes
								NOT = { has_character_flag = SOV_targeted_for_purge_flag }
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
							set_character_flag = SOV_targeted_for_purge_flag

							#ROOT = { log = "Chosen General - Random - [PREV.GetName]" } #Debug
						}
						#log = "RANDOM GENERAL PICK" #Debug
					}
					else_if = { #IF NOT ENOUGH GENERALS, THEN PICK SOME EXTRA MARSHALS/ADVISORS
						limit = {
							any_character = {
								OR = {
									is_field_marshal = yes
									has_army_ledger = yes
								}
								NOT = { has_character_flag = SOV_targeted_for_purge_flag }
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
						}
						random_army_leader = {
							limit = {
								OR = {
									is_field_marshal = yes
									has_army_ledger = yes
								}
								NOT = { has_character_flag = SOV_targeted_for_purge_flag }
								SOV_character_can_be_purged = yes
								NOT = { has_trait = trait_SOV_stalinist }
							}
							set_character_flag = SOV_targeted_for_purge_flag

							#ROOT = { log = "Chosen Marshal/Advisor - Random - [PREV.GetName]" } #Debug

						}
						#log = "RANDOM MARSHAL/ADVISOR PICK" #Debug
					}
					else = {
						#log = "RAN OUT OF ARMY LEADERS, DAMN!" #Debug
					}
					add_to_temp_variable = { temp_targets_index = 1 }
					#log = "LOOP: temp_targets_index = [?temp_targets_index]" #Debug
				}
			}
		}
	}

	country_event = NSB_soviet_paranoia_system_events.29 # TRIGGER EVENT (PHASE 5)

	set_variable = { SOV_last_great_purge_area = 2 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}


SOV_trigger_regular_purge_admiral_accused_effect = {
	hidden_effect = {
		if = { #PRIORITIZE FORCED TARGETS
			limit ={
				any_navy_leader = {
					has_character_flag = SOV_potential_target_flag
					SOV_character_can_be_purged = yes
				}
			}
			random_navy_leader = {
				limit = {
					has_character_flag = SOV_potential_target_flag
					SOV_character_can_be_purged = yes
				}
				set_character_flag = SOV_targeted_for_purge_flag
				clr_character_flag = SOV_potential_target_flag
				SOV = {
					set_variable = { SOV_accused_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accused_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_purged_character_for_loc = PREV } }	
			}
		}
		else = {
			random_navy_leader = {
				limit = {
					SOV_character_can_be_purged = yes
					NOT = { has_trait = trait_SOV_stalinist }
				}
				set_character_flag = SOV_targeted_for_purge_flag
				SOV = {
					set_variable = { SOV_accused_unit_leader_level = PREV.skill_level }
					multiply_variable = { SOV_accused_unit_leader_level = SOV_paranoia_based_on_level_multiplier }
				}
				ROOT = { set_variable = { SOV_purged_character_for_loc = PREV } }	
			}
		}
	}
	
	var:SOV_purged_character_for_loc = { unit_leader_event = NSB_soviet_paranoia_system_events.31 } # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 3 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_regular_purge_naval_conspiracy_in_the_academy_effect = {
	country_event = NSB_soviet_paranoia_system_events.32 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 3 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_regular_purge_naval_anti_saboteur_campaign_effect = {
	country_event = NSB_soviet_paranoia_system_events.33 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 3 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_great_purge_junior_naval_officers_effect = {
	country_event = NSB_soviet_paranoia_system_events.38 # TRIGGER EVENT

	set_variable = { SOV_last_great_purge_area = 3 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_great_purge_trial_of_the_admirals_effect = {
	hidden_effect = {
		set_temp_variable = { temp_targets_index = 0 } #Targeted chars counter

		#If someone is already TARGETED, prio them first.
		if = {
			limit = {
				any_character = {
					OR = {
						is_navy_leader = yes #Admirals
						has_navy_ledger = yes #Navy Advisors
					}
					has_character_flag = SOV_targeted_for_purge_flag
					SOV_character_can_be_purged = yes
				}
			}
			#These guys will be automatically targeted in the event, so we increase the var and only fill in with random chars if necessary
			if = {
				limit = {
					check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
				}
				#log = "I will purge some random guys" #Debug
			}
			else = {
				#log = "I will use default people to purge" #Debug
				every_character = {
					limit = {
						OR = {
							is_navy_leader = yes #Admirals
							has_navy_ledger = yes #Navy Advisors
						}
						has_character_flag = SOV_targeted_for_purge_flag
						SOV_character_can_be_purged = yes
					}
					PREV = { add_to_temp_variable = { temp_targets_index = 1 } }
				}
			}
		}

		set_temp_variable = { temp_people_to_purge = 0 } #Number of random people that can be purged

		#log = "VAR OVERRIDE is [?SOV_specific_random_purge_targets_override]" #Debug

		if = { #If a specific amount of random chars to be purged has been set, then use that number
			limit = {
				check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
			}
			set_temp_variable = { temp_people_to_purge = SOV_specific_random_purge_targets_override }
			SOV_reset_specific_random_target_override_effect = yes #Reset this var (value = -1) so that it is not used again until specified otherwise
			#log = "Using OVERRIDE" #Debug
		}
		else = { #If no specific amount of random chars to be purged is set, then use default value
			set_temp_variable = { temp_people_to_purge = SOV_paranoia_great_purge_navy_number_of_targets }
			#log = "Using DEFAULT" #Debug
		}

		# PICK RANDOM PEOPLE IF APPROPRIATE
		if = {
			limit = {
				check_variable = { temp_people_to_purge = 0 compare = greater_than } #Random people to purge is not 0
				check_variable = { temp_targets_index < temp_people_to_purge } #There is room for random advisors to be purged (for when there are pre-set targets)
			}

			while_loop_effect = {
				limit = {
					check_variable = { temp_targets_index = temp_people_to_purge compare = less_than }
				}
				if = { #PRIORITIZE FORCED TARGETS - ADMIRALS/ADVISORS
					limit ={
						any_character = {
							OR = {
								is_navy_leader = yes #Admirals
								has_navy_ledger = yes #Navy Advisors
							}
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
					}
					random_character = {
						limit = {
							OR = {
								is_navy_leader = yes #Admirals
								has_navy_ledger = yes #Navy Advisors
							}
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
						set_character_flag = SOV_targeted_for_purge_flag
						clr_character_flag = SOV_potential_target_flag
					}
					#log = "TARGET FIXED" #Debug
				}
				else_if = { #FILL TARGET WITH RANDOM ADMIRALS/ADVISORS (Could have an extra step, checking for non-Stalinist ppl, then this one)
					limit = {
						any_character = {
							OR = {
								is_navy_leader = yes #Admirals
								has_navy_ledger = yes #Navy Advisors
							}
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
					}
					random_character = {
						limit = {
							OR = {
								is_navy_leader = yes #Admirals
								has_navy_ledger = yes #Navy Advisors
							}
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
							SOV_character_can_be_purged = yes
							NOT = { has_trait = trait_SOV_stalinist }
						}
						set_character_flag = SOV_targeted_for_purge_flag
					}
					#log = "RANDOM ADMIRAL/ADVISOR PICK" #Debug
				}
				else = {
					#log = "RAN OUT OF NAVY LEADERS, DAMN!" #Debug
				}
				add_to_temp_variable = { temp_targets_index = 1 }
				#log = "LOOP: temp_targets_index = [?temp_targets_index]" #Debug
			}
		}
	}

	country_event = NSB_soviet_paranoia_system_events.39 # TRIGGER EVENT

	set_variable = { SOV_last_great_purge_area = 3 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_regular_purge_airforce_conspiracy_in_the_academy_effect = {
	country_event = NSB_soviet_paranoia_system_events.41 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 4 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_regular_purge_airforce_anti_saboteur_campaign_effect = {
	country_event = NSB_soviet_paranoia_system_events.42 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 4 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_great_purge_design_bureau_dissolved_effect = {
	hidden_effect = {
		set_variable = { SOV_random_bureau_to_dissolve = random } # THIS IS NOW PASSED TO THE EVENT, BUT NEEDS TO BE DONE HERE AND STORED IN VAR
		#log = "Random bureau to dissolve: [?SOV_random_bureau_to_dissolve]"
	}

	country_event = NSB_soviet_paranoia_system_events.48 # TRIGGER EVENT

	set_variable = { SOV_last_great_purge_area = 4 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}

SOV_trigger_great_purge_airforce_chiefs_effect = {
	hidden_effect = {
		set_temp_variable = { temp_targets_index = 0 } #Targeted chars counter
		#If someone is already TARGETED, prio them first.
		if = {
			limit = {
				any_character = {
					has_air_ledger = yes
					has_character_flag = SOV_targeted_for_purge_flag
					SOV_character_can_be_purged = yes
				}
			}
			#These guys will be automatically targeted in the event, so we increase the var and only fill in with random chars if necessary
			if = {
				limit = {
					check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
				}
				#log = "I will purge some random guys" #Debug
			}
			else = {
				#log = "I will use default people to purge" #Debug
				every_character = {
					limit = {
						has_air_ledger = yes
						has_character_flag = SOV_targeted_for_purge_flag
						SOV_character_can_be_purged = yes
					}
					PREV = { add_to_temp_variable = { temp_targets_index = 1 } }
				}
			}
		}
		else = { # If no one targeted via another effect, then pick historically purged people
			if = {
				limit = {
					has_character = SOV_sergey_khudyakov
					SOV_sergey_khudyakov = { SOV_character_can_be_purged = yes }
				}
				SOV_sergey_khudyakov = { #MHC
					set_character_flag = SOV_targeted_for_purge_flag
				}
				add_to_temp_variable = { temp_targets_index = 1 }
			}
			if = {
				limit = {
					has_character = SOV_yakov_smushkevich
					SOV_yakov_smushkevich = { SOV_character_can_be_purged = yes }
				}
				SOV_yakov_smushkevich = {#Chief
					set_character_flag = SOV_targeted_for_purge_flag
				}
				add_to_temp_variable = { temp_targets_index = 1 }
			}
			if = {
				limit = {
					has_character = SOV_pavel_rychagov
					SOV_pavel_rychagov = { SOV_character_can_be_purged = yes }
				}
				SOV_pavel_rychagov = { #Chief
					set_character_flag = SOV_targeted_for_purge_flag
				}
				add_to_temp_variable = { temp_targets_index = 1 }
			}
		}

		set_temp_variable = { temp_people_to_purge = 0 } #Number of random people that can be purged

		#log = "VAR OVERRIDE is [?SOV_specific_random_purge_targets_override]" #Debug

		if = { #If a specific amount of random chars to be purged has been set, then use that number
			limit = {
				check_variable = { SOV_specific_random_purge_targets_override = 0 compare = greater_than_or_equals }
			}
			set_temp_variable = { temp_people_to_purge = SOV_specific_random_purge_targets_override }
			SOV_reset_specific_random_target_override_effect = yes #Reset this var (value = -1) so that it is not used again until specified otherwise
			#log = "Using OVERRIDE" #Debug
		}
		else = { #If no specific amount of random chars to be purged is set, then use default value
			set_temp_variable = { temp_people_to_purge = SOV_paranoia_great_purge_air_number_of_targets }
			#log = "Using DEFAULT" #Debug
		}

		# PICK RANDOM PEOPLE IF APPROPRIATE
		if = {
			limit = {
				check_variable = { temp_people_to_purge = 0 compare = greater_than } #Random people to purge is not 0
				check_variable = { temp_targets_index < temp_people_to_purge } #There is room for random advisors to be purged (for when there are pre-set targets)
			}

			while_loop_effect = {
				limit = {
					check_variable = { temp_targets_index = temp_people_to_purge compare = less_than }
				}
				if = { #PRIORITIZE FORCED TARGETS
					limit ={
						any_character = {
							has_air_ledger = yes
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
						}
					}
					random_character = {
						limit = {
							has_air_ledger = yes
							has_character_flag = SOV_potential_target_flag
							SOV_character_can_be_purged = yes
						}
						set_character_flag = SOV_targeted_for_purge_flag
						clr_character_flag = SOV_potential_target_flag
					}
					#log = "TARGET FIXED" #Debug
				}
				else_if = { #FILL TARGET WITH RANDOM AIRFORCE ADVISORS (Could have an extra step, checking for non-Stalinist ppl, then this one)
					limit = {
						any_character = {
							has_air_ledger = yes
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
							SOV_character_can_be_purged = yes
						}
					}
					random_character = {
						limit = {
							has_air_ledger = yes
							NOT = { has_character_flag = SOV_targeted_for_purge_flag }
							SOV_character_can_be_purged = yes
						}
						set_character_flag = SOV_targeted_for_purge_flag
					}
					#log = "RANDOM ADMIRAL/ADVISOR PICK" #Debug
				}
				else = {
					#log = "RAN OUT OF NAVY LEADERS, DAMN!" #Debug
				}
				add_to_temp_variable = { temp_targets_index = 1 }
				#log = "LOOP: temp_targets_index = [?temp_targets_index]" #Debug
			}
		}
	}

	country_event = NSB_soviet_paranoia_system_events.49 # TRIGGER EVENT

	set_variable = { SOV_last_regular_purge_area = 4 } #Set area purged to prevent same area from being purged twice in a row
	set_country_flag = SOV_last_purge_flag
}




####
#### INDUSTRIAL STUFF ####
####

SOV_replace_industrial_designer = {
	custom_effect_tooltip = SOV_upgrade_industrial_designer_tt
	if = {
		limit = {
			has_idea = stalingrad_tractor_factory
		}
		swap_ideas = {
			remove_idea = stalingrad_tractor_factory
			add_idea = stalingrad_tractor_factory_improved
		}
	}
	else_if = {
		limit = {
			has_idea = leningrad_polytechnical_institute
		}
		swap_ideas = {
			remove_idea = leningrad_polytechnical_institute
			add_idea = leningrad_polytechnical_institute_improved
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_soviet_railways
		}
		swap_ideas = {
			remove_idea = SOV_soviet_railways
			add_idea = SOV_soviet_railways_improved
		}
	}
}



####
#### FOREIGN POLITICS ####
####

SOV_fortify_mongolian_border_effect = {
	330 = {
		add_building_construction = { #ULAAN BAATAR
			type = bunker
			level = 1
			instant_build = yes
			province = 4801
		}
	}
	if = {
		limit = {
			761 = { #HULUNBUIR
				controller = {
					SOV_is_threated_by_japan = yes
				}
			}
		}
		819 = {
			add_building_construction = { #BORDER
				type = bunker
				level = 2
				instant_build = yes
				province = {
					id = 4792
					id = 12557
					id = 1743
				}
			}
		}
		ROOT = { set_country_flag = SOV_mongolian_forts_1_flag }
	}
	if = {
		limit = {
			612 = { #CHAHAR
				controller = {
					SOV_is_threated_by_japan = yes
				}
			}
		}
		if = { #Do not repeat same province twice
			limit = {
				ROOT = { has_country_flag = SOV_mongolian_forts_1_flag }
			}
			819 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 12629
						id = 4873
					}
				}
			}
		}
		else = {
			819 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 1743
						id = 12629
						id = 4873
					}
				}
			}
		}
		817 = {
			add_building_construction = { #BORDER
				type = bunker
				level = 2
				instant_build = yes
				province = {
					id = 10575
					id = 4655
				}
			}
		}
		ROOT = { clr_country_flag = SOV_mongolian_forts_1_flag }
		ROOT = { set_country_flag = SOV_mongolian_forts_2_flag }
	}
	if = {
		limit = {
			621 = { #SUIYUAN
				controller = {
					SOV_is_threated_by_japan = yes
				}
			}
		}
		if = { #Do not repeat same province twice
			limit = {
				ROOT = { has_country_flag = SOV_mongolian_forts_2_flag }
			}
			817 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 1723
						id = 1853
						id = 12539
						id = 10558
					}
				}
			}
		}
		else = {
			817 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 4655
						id = 1723
						id = 1853
						id = 12539
						id = 10558
					}
				}
			}
		}
		ROOT = { clr_country_flag = SOV_mongolian_forts_2_flag }
		ROOT = { set_country_flag = SOV_mongolian_forts_3_flag }
	}
	if = {
		limit = {
			756 = { #JIUQUAN
				controller = {
					SOV_is_threated_by_japan = yes
				}
			}
		}
		if = { #Do not repeat same province twice
			limit = {
				ROOT = { has_country_flag = SOV_mongolian_forts_3_flag }
			}
			817 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 10689
						id = 4786
						id = 1781
					}
				}
			}
		}
		else = {
			817 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 10558
						id = 10689
						id = 4786
						id = 1781
					}
				}
			}
		}
		ROOT = { clr_country_flag = SOV_mongolian_forts_3_flag }
		ROOT = { set_country_flag = SOV_mongolian_forts_4_flag }
	}
	if = {
		limit = {
			760 = { #DABANCHENG
				controller = {
					SOV_is_threated_by_japan = yes
				}
			}
		}
		if = { #Do not repeat same province twice
			limit = {
				ROOT = { has_country_flag = SOV_mongolian_forts_4_flag }
			}
			817 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 10560
						id = 1855
					}
				}
			}
		}
		else = {
			817 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 1781
						id = 10560
						id = 1855
					}
				}
			}
		}
		817 = {
			add_building_construction = { #BORDER
				type = bunker
				level = 2
				instant_build = yes
				province = {
					id = 7808
				}
			}
		}
		ROOT = { clr_country_flag = SOV_mongolian_forts_4_flag }
		ROOT = { set_country_flag = SOV_mongolian_forts_5_flag }
	}
	if = {
		limit = {
			618 = { #DZUNGARIA
				controller = {
					SOV_is_threated_by_japan = yes
				}
			}
		}
		if = { #Do not repeat same province twice
			limit = {
				ROOT = { has_country_flag = SOV_mongolian_forts_5_flag }
			}
			818 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 7870
						id = 1774
						id = 10539
						id = 4751
					}
				}
			}
		}
		else = {
			818 = {
				add_building_construction = { #BORDER
					type = bunker
					level = 2
					instant_build = yes
					province = {
						id = 7808
						id = 7870
						id = 1774
						id = 10539
						id = 4751
					}
				}
			}
		}
		ROOT = { clr_country_flag = SOV_mongolian_forts_5_flag }
	}
}

SOV_clear_targeted_relation_modifiers_effect = {
	#log = "Effect Called"
	every_other_country = {
		limit = {
			capital_scope = { is_on_continent = europe }
			SOV = {
				has_relation_modifier = {
					target = PREV
					modifier = SOV_face_western_fascism_static
				}
			}
		}
		#log = "Removing shit in [THIS.GetName]"
		SOV = {
			remove_relation_modifier = {
				target = PREV
				modifier = SOV_face_western_fascism_static
			}
		}
	}
	#log = "------- SPACE -------"
	GER = {
		SOV = {
			remove_relation_modifier = {
				target = PREV
				modifier = SOV_face_western_fascism_static
			}
			#log = "[GER.getname] is now clean"
		}
	}
}

SOV_clear_hidden_targeted_ns_effect = {
	if = {
		limit = {
			has_idea = SOV_targeted_bonus_against_ENG
		}
		add_ideas = SOV_targeted_bonus_against_ENG
	}
	if = {
		limit = {
			has_idea = SOV_targeted_bonus_against_FRA
		}
		add_ideas = SOV_targeted_bonus_against_FRA
	}
	if = {
		limit = {
			has_idea = SOV_targeted_bonus_against_GER
		}
		add_ideas = SOV_targeted_bonus_against_GER
	}
	if = {
		limit = {
			has_idea = SOV_targeted_bonus_against_ITA
		}
		add_ideas = SOV_targeted_bonus_against_ITA
	}
	if = {
		limit = {
			has_idea = SOV_targeted_bonus_against_USA
		}
		add_ideas = SOV_targeted_bonus_against_USA
	}
	if = {
		limit = {
			has_idea = SOV_targeted_bonus_against_JAP
		}
		add_ideas = SOV_targeted_bonus_against_JAP
	}
}

SOV_foreign_politics_decision_cost_reduction_effect = { #Add more diplomacy vars here to divide and round them
	divide_variable = { SOV_pressure_country_government_cost = 2 }
	divide_variable = { SOV_promote_ideology_rallies_cost = 2 }
	divide_variable = { SOV_send_ultimatum_to_country_cost = 2 }
	divide_variable = { SOV_send_infantry_equipment_cost = 2 }
	divide_variable = { SOV_peace_deal_cost = 2 }
	divide_variable = { SOV_sinkiang_proposal_cost = 2 }

	round_variable = SOV_pressure_country_government_cost
	round_variable = SOV_promote_ideology_rallies_cost
	round_variable = SOV_send_ultimatum_to_country_cost
	round_variable = SOV_send_infantry_equipment_cost
	round_variable = SOV_peace_deal_cost
	round_variable = SOV_sinkiang_proposal_cost
}

SOV_approach_sinkiang_diplomacy_effect = {
	add_ai_strategy = {
		type = befriend
		id = SIK			
		value = 15
	}
	add_ai_strategy = {
		type = alliance
		id = SIK		
		value = 15
	}
	SIK = {
		add_ai_strategy = {
			type = befriend
			id = SOV			
			value = 10
		}
		add_ai_strategy = {
			type = alliance
			id = CHI		
			value = -5
		}
		add_ai_strategy = {
			type = alliance
			id = PRC		
			value = -5
		}
		add_ai_strategy = {
			type = alliance
			id = XSM		
			value = -5
		}
		add_ai_strategy = {
			type = alliance
			id = SHX		
			value = -5
		}
		add_ai_strategy = {
			type = alliance
			id = YUN		
			value = -5
		}
		add_ai_strategy = {
			type = alliance
			id = GXC		
			value = -5
		}
		add_ai_strategy = {
			type = alliance
			id = MEN		
			value = -5
		}
		add_ai_strategy = {
			type = alliance
			id = MAN		
			value = -5
		}
	}
}

SOV_set_appropriate_national_academy_of_sciences_flag_effect = {
	#log = "ROOT is [ROOT.GetName]"
	#log = "FROM is [FROM.GetName]"
	if = {
		limit = {
			FROM = { tag = BLR }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_BLR_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = UKR }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_UKR_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = KAZ }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_KAZ_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = GEO }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_GEO_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = ARM }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_ARM_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = AZR }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_AZR_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = UZB }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_UZB_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = TMS }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_TMS_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = KYR }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_KYR_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = TAJ }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_TAJ_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = TAN }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_TAN_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = DON }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_DON_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = KUB }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_KUB_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = EST }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_EST_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = LAT }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_LAT_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = LIT }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_LIT_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = POL }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_POL_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = FIN }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_FIN_flag }
	}
	else_if = {
		limit = {
			FROM = { tag = MON }
		}
		ROOT = { set_country_flag = SOV_national_academy_of_sciences_built_MON_flag }
	}

	# Add any new cosmetics/releasables here! (and also decisions for them) ((and also scripted triggers)) (((please just do not add any more cosmetics/releasables...)))
}

SOV_build_national_academy_of_sciences_effect = {
	if = {
		limit = {
			country_exists = FROM
		}
		custom_effect_tooltip = SOV_national_academy_of_sciences_construction_independent_tt
	}
	else = {
		custom_effect_tooltip = SOV_national_academy_of_sciences_construction_integrated_tt
	}
	ROOT = {
		custom_effect_tooltip = SOV_national_academy_of_sciences_research_tt
		add_to_variable = { SOV_academy_of_sciences_research_speed_factor = SOV_national_academy_of_sciences_research_increase }
	}
}


SOV_clear_hostile_diplomacy_in_both_countries = { #It assumes you want to clear claims and wargoals against each other in both ROOT and FROM
	#First clear ROOT
	if = {
		limit = {
			any_owned_state = {
				is_claimed_by = FROM
			}
		}
		every_owned_state = {
			limit = {
				is_claimed_by = FROM
			}
			remove_claim_by = FROM
		}
	}
	if = {
		limit = {
			FROM = { has_wargoal_against = ROOT }
		}
		FROM = {
			remove_wargoal = {
				type = all
				target = ROOT
			}
		}
	}

	#Now clear FROM
	if = {
		limit = {
			FROM = {
				any_owned_state = {
					is_claimed_by = ROOT
				}
			}
		}
		FROM = {
			every_owned_state = {
				limit = {
					is_claimed_by = ROOT
				}
				remove_claim_by = ROOT
			}
		}
	}
	if = {
		limit = {
			has_wargoal_against = FROM
		}
		remove_wargoal = {
			type = all
			target = FROM
		}
	}
}

SOV_clear_hostile_diplomacy_in_all_three_countries = { #It assumes you want to clear claims and wargoals against each other in ROOT, FROM and POL
	#First clear ROOT
	if = {
		limit = {
			any_owned_state = {
				is_claimed_by = FROM
			}
		}
		every_owned_state = {
			limit = {
				is_claimed_by = FROM
			}
			remove_claim_by = FROM
		}
	}
	if = {
		limit = {
			any_owned_state = {
				is_claimed_by = POL
			}
		}
		every_owned_state = {
			limit = {
				is_claimed_by = POL
			}
			remove_claim_by = POL
		}
	}
	if = {
		limit = {
			FROM = { has_wargoal_against = ROOT }
		}
		FROM = {
			remove_wargoal = {
				type = all
				target = ROOT
			}
		}
	}
	if = {
		limit = {
			POL = { has_wargoal_against = ROOT }
		}
		POL = {
			remove_wargoal = {
				type = all
				target = ROOT
			}
		}
	}

	#Now clear FROM
	if = {
		limit = {
			FROM = {
				any_owned_state = {
					is_claimed_by = ROOT
				}
			}
		}
		FROM = {
			every_owned_state = {
				limit = {
					is_claimed_by = ROOT
				}
				remove_claim_by = ROOT
			}
		}
	}
	if = {
		limit = {
			FROM = {
				any_owned_state = {
					is_claimed_by = POL
				}
			}
		}
		FROM = {
			every_owned_state = {
				limit = {
					is_claimed_by = POL
				}
				remove_claim_by = POL
			}
		}
	}
	if = {
		limit = {
			has_wargoal_against = FROM
		}
		remove_wargoal = {
			type = all
			target = FROM
		}
	}
	if = {
		limit = {
			POL = { has_wargoal_against = FROM }
		}
		POL = {
			remove_wargoal = {
				type = all
				target = FROM
			}
		}
	}

	#Finally, clear POL
	if = {
		limit = {
			POL = {
				any_owned_state = {
					is_claimed_by = ROOT
				}
			}
		}
		POL = {
			every_owned_state = {
				limit = {
					is_claimed_by = ROOT
				}
				remove_claim_by = ROOT
			}
		}
	}
	if = {
		limit = {
			POL = {
				any_owned_state = {
					is_claimed_by = FROM
				}
			}
		}
		POL = {
			every_owned_state = {
				limit = {
					is_claimed_by = FROM
				}
				remove_claim_by = FROM
			}
		}
	}
	if = {
		limit = {
			has_wargoal_against = POL
		}
		remove_wargoal = {
			type = all
			target = POL
		}
	}
	if = {
		limit = {
			FROM = { has_wargoal_against = POL }
		}
		FROM = {
			remove_wargoal = {
				type = all
				target = POL
			}
		}
	}
}

SOV_apply_collective_ageement_on_mutual_assistance_effects_basic = {
	SOV_clear_hostile_diplomacy_in_both_countries = yes #Clears wargoals and claims

	#FRA -> SOV
	FROM = {
		diplomatic_relation = {
			country = ROOT
			relation = non_aggression_pact
			active = yes
		}
		give_guarantee = ROOT
		add_opinion_modifier = {
			target = ROOT
			modifier = SOV_policies_of_collective_security_opinion
		}
	}

	#SOV -> FRA
	diplomatic_relation = {
		country = FROM
		relation = non_aggression_pact
		active = yes
	}
	give_guarantee = FROM
	add_opinion_modifier = {
		target = FROM
		modifier = SOV_policies_of_collective_security_opinion
	}
}


SOV_apply_collective_ageement_on_mutual_assistance_effects_poland_included = {
	SOV_clear_hostile_diplomacy_in_all_three_countries = yes #Clears wargoals and claims in all three countries

	#SOV -> FRA
	diplomatic_relation = {
		country = FROM
		relation = non_aggression_pact
		active = yes
	}
	give_guarantee = FROM
	add_opinion_modifier = {
		target = FROM
		modifier = SOV_policies_of_collective_security_opinion
	}

	#SOV -> POL
	diplomatic_relation = {
		country = POL
		relation = non_aggression_pact
		active = yes
	}
	give_guarantee = POL
	add_opinion_modifier = {
		target = POL
		modifier = SOV_policies_of_collective_security_opinion
	}

	#FRA -> SOV
	FROM = {
		diplomatic_relation = {
			country = ROOT
			relation = non_aggression_pact
			active = yes
		}
		give_guarantee = ROOT
		add_opinion_modifier = {
			target = ROOT
			modifier = SOV_policies_of_collective_security_opinion
		}
	}

	#FRA -> POL
	FROM = {
		diplomatic_relation = {
			country = POL
			relation = non_aggression_pact
			active = yes
		}
		give_guarantee = POL
		add_opinion_modifier = {
			target = POL
			modifier = SOV_policies_of_collective_security_opinion
		}
	}

	#POL -> SOV
	POL = {
		diplomatic_relation = {
			country = ROOT
			relation = non_aggression_pact
			active = yes
		}
		give_guarantee = ROOT
		add_opinion_modifier = {
			target = ROOT
			modifier = SOV_policies_of_collective_security_opinion
		}
	}

	#POL -> FRA
	POL = {
		diplomatic_relation = {
			country = FROM
			relation = non_aggression_pact
			active = yes
		}
		give_guarantee = FROM
		add_opinion_modifier = {
			target = FROM
			modifier = SOV_policies_of_collective_security_opinion
		}
	}
}

SOV_set_military_offensive_effect = {
	set_country_flag = SOV_military_offensive_in_progress_flag
	for_each_scope_loop = {
		array = SOV_military_offensive_states
		add_dynamic_modifier = {
			modifier = SOV_soviet_military_offensive
			days = 90
			scope = SOV
		}
	}
}

SOV_clean_military_offensive_effect = {
	clr_country_flag = SOV_military_offensive_in_progress_flag
	for_each_scope_loop = {
		array = SOV_military_offensive_states
		remove_dynamic_modifier = { modifier = SOV_soviet_military_offensive }
	}
	clear_array = SOV_military_offensive_states
	clear_variable = SOV_offensive_main_objective
}

SOV_spawn_penal_divisions_effect = {
	hidden_effect = {
		capital_scope = {
			create_unit = {
				division = "division_template = \"Shtrafnaya Chast\" start_experience_factor = 0.5 start_equipment_factor = 1.0"
				owner = ROOT
				count = 4
			}
		}
		set_country_flag = SOV_penal_units_raised_flag
	}
}

SOV_anglo_soviet_invasion_add_ai_strategies_effect = { #Soviet / Anglo-Soviet invasion of Iran
	if = {
		limit = {
			has_global_flag = SOV_agreed_anglo_soviet_invasion_of_iran_flag
		}
		SOV = {
			add_ai_strategy = {
				type = befriend
				id = ENG			
				value = 15
			}
		}
		ENG = {
			add_ai_strategy = {
				type = befriend
				id = SOV			
				value = 15
			}
		}
	}
	else = {
		SOV = {
			add_ai_strategy = {
				type = befriend
				id = ENG			
				value = -15
			}
		}
		ENG = {
			add_ai_strategy = {
				type = befriend
				id = SOV			
				value = -15
			}
		}
	}
	if = {
		limit = {
			PER = {
				is_in_faction = no
			}
		}
		PER = {
			add_timed_idea = {
				idea = SOV_shocked_by_surprise_attack
				days = var:SOV.SOV_operation_countenance_mission_days
			}
		}
	}
}

SOV_failed_anglo_soviet_invasion_ai_strategies_effect = {
	if = { #IF ENG is not involved, seek British aid
		limit = {
			NOT = { has_global_flag = SOV_agreed_anglo_soviet_invasion_of_iran_flag }
		}
		if = {
			limit = {
				country_exists = ENG
				NOT = { has_war_with = ENG }
				ENG = {
					is_faction_leader = yes
					NOT = {	is_in_faction_with = SOV }
				}
			}
			add_ai_strategy = {
				type = alliance
				id = ENG		
				value = 200
			}
			ENG = {
				add_ai_strategy = {
					type = alliance
					id = PER		
					value = 25
				}
			}
		}
	}
	else = {
		if = {
			limit = {
				country_exists = GER
				NOT = { has_war_with = GER }
				GER = {
					is_faction_leader = yes
					NOT = {
						is_in_faction_with = SOV
						AND = {
							has_global_flag = SOV_agreed_anglo_soviet_invasion_of_iran_flag
							is_in_faction_with = ENG
						}
					}
				}
			}
			add_ai_strategy = {
				type = alliance
				id = GER		
				value = 200
			}
			GER = {
				add_ai_strategy = {
					type = alliance
					id = PER		
					value = 25
				}
			}
		}
		else_if = {
			limit = {
				country_exists = ITA
				NOT = { has_war_with = ITA }
				ITA = {
					is_faction_leader = yes
					NOT = {
						is_in_faction_with = SOV
						AND = {
							has_global_flag = SOV_agreed_anglo_soviet_invasion_of_iran_flag
							is_in_faction_with = ENG
						}
					}
				}
			}
			add_ai_strategy = {
				type = alliance
				id = ITA		
				value = 200
			}
			ITA = {
				add_ai_strategy = {
					type = alliance
					id = PER		
					value = 25
				}
			}
		}
		else_if = {
			limit = {
				country_exists = JAP
				NOT = { has_war_with = JAP }
				JAP = {
					is_faction_leader = yes
					NOT = {
						is_in_faction_with = SOV
						AND = {
							has_global_flag = SOV_agreed_anglo_soviet_invasion_of_iran_flag
							is_in_faction_with = ENG
						}
					}
				}
			}
			add_ai_strategy = {
				type = alliance
				id = JAP		
				value = 200
			}
			JAP = {
				add_ai_strategy = {
					type = alliance
					id = PER		
					value = 25
				}
			}
		}
		else_if = {
			limit = {
				country_exists = FRA
				NOT = { has_war_with = FRA }
				FRA = {
					is_faction_leader = yes
					NOT = {
						is_in_faction_with = SOV
						AND = {
							has_global_flag = SOV_agreed_anglo_soviet_invasion_of_iran_flag
							is_in_faction_with = ENG
						}
					}
				}
			}
			add_ai_strategy = {
				type = alliance
				id = FRA		
				value = 200
			}
			FRA = {
				add_ai_strategy = {
					type = alliance
					id = PER		
					value = 25
				}
			}
		}
		else_if = {
			limit = {
				country_exists = USA
				NOT = { has_war_with = USA }
				USA = {
					is_faction_leader = yes
					NOT = {
						is_in_faction_with = SOV
						AND = {
							has_global_flag = SOV_agreed_anglo_soviet_invasion_of_iran_flag
							is_in_faction_with = ENG
						}
					}
				}
			}
			add_ai_strategy = {
				type = alliance
				id = USA		
				value = 200
			}
			USA = {
				add_ai_strategy = {
					type = alliance
					id = PER		
					value = 25
				}
			}
		}
	}
}

SOV_manage_spanish_generals_exiled_in_ussr_effect = {
	#log = "FROM is [FROM.GetName] // ROOT is [ROOT.GetName]" #Debug
	#Exiled SPD/SPC Generals
	if = { #Líster
		limit = {
			ROOT = { tag = SPD }
			SOV = {
				has_country_flag = SOV_spanish_refugee_generals_welcome_flag
				has_completed_focus = SOV_the_centre
			}
		}
		ROOT = { SPR_enrique_lister = { set_nationality = SOV } }
		SOV = {
			SPR_enrique_lister = {
				set_variable = { SOV.SOV_spanish_general = THIS }
				#log = "Líster is [?SOV.SOV_spanish_general.GetName]" #Debug
				#Líster joins the USSR as a Stalinist
				if = {
					limit = {
						has_trait = trait_SPR_anti_stalinist_loyalties
					}
					remove_unit_leader_trait = trait_SPR_anti_stalinist_loyalties
				}
				if = {
					limit = {
						has_trait = trait_SPR_republican_loyalties
					}
					remove_unit_leader_trait = trait_SPR_republican_loyalties
				}
				if = {
					limit = {
						NOT = { has_trait = trait_SPR_stalinist_loyalties }
					}
					add_unit_leader_trait = trait_SPR_stalinist_loyalties
				}
			}
			set_country_flag = SOV_go_go_general_flag
		}
	}
	else_if = { #El Campesino
		limit = {
			ROOT = { tag = SPD }
			SOV = {
				has_country_flag = SOV_spanish_refugee_generals_welcome_flag
				OR = {
					has_completed_focus = SOV_the_left_opposition
					AND = {
						has_completed_focus = SOV_the_right_opposition
						NOT = { has_completed_focus = SOV_covert_support_for_spanish_poum }
					}
				}
			}
		}
		ROOT = { SPR_valentin_gonzalez = { set_nationality = SOV } }
		SOV = {
			SPR_valentin_gonzalez = {
				set_variable = { SOV.SOV_spanish_general = THIS }
				#El Campesino joins the USSR as an Anti-Stalinist
				if = {
					limit = {
						has_trait = trait_SPR_republican_loyalties
					}
					remove_unit_leader_trait = trait_SPR_republican_loyalties
				}
				if = {
					limit = {
						has_trait = trait_SPR_stalinist_loyalties
					}
					remove_unit_leader_trait = trait_SPR_stalinist_loyalties
				}
				if = {
					limit = {
						NOT = { has_trait = trait_SPR_anti_stalinist_loyalties }
					}
					add_unit_leader_trait = trait_SPR_anti_stalinist_loyalties
				}
			}
			set_country_flag = SOV_go_go_general_flag
		}
	}
	else_if = { #José Rovira
		limit = {
			ROOT = { tag = SPC }
			SOV = {
				has_country_flag = SOV_spanish_refugee_generals_welcome_flag
				has_completed_focus = SOV_covert_support_for_spanish_poum
			}
		}
		ROOT = { SPR_jose_rovira = { set_nationality = SOV } }
		SOV = {
			SPR_jose_rovira = {
				set_variable = { SOV.SOV_spanish_general = THIS }
				#Rovira joins the USSR as an Anti-Stalinist
				if = {
					limit = {
						has_trait = trait_SPR_republican_loyalties
					}
					remove_unit_leader_trait = trait_SPR_republican_loyalties
				}
				if = {
					limit = {
						has_trait = trait_SPR_stalinist_loyalties
					}
					remove_unit_leader_trait = trait_SPR_stalinist_loyalties
				}
				if = {
					limit = {
						NOT = { has_trait = trait_SPR_anti_stalinist_loyalties }
					}
					add_unit_leader_trait = trait_SPR_anti_stalinist_loyalties
				}
			}
			set_country_flag = SOV_go_go_general_flag
		}
	}
	if = {
		limit = {
			SOV = { has_country_flag = SOV_go_go_general_flag }
		}
		var:SOV.SOV_spanish_general = {
			#log = "I am [THIS.GetName]" #Debug
			if = { # Won't be a marshal in the USSR
				limit = {
					is_field_marshal = yes
				}
				demote_leader = yes
			}
			if = { # Won't be an advisor in the USSR
				limit = {
					is_advisor = yes
				}
				if = {
					limit = {
						is_character_slot = theorist
					}
					remove_advisor_role = { slot = theorist } 
				}
				else_if = {
					limit = {
						is_character_slot = army_chief
					}
					remove_advisor_role = { slot = army_chief } 
				}
				if = {
					limit = {
						is_character_slot = high_command
					}
					remove_advisor_role = { slot = high_command } 
				}
			}
			unit_leader_event = { id = NSB_soviet_spanish_civil_war.1 days = 5 random_days = 2 }
		}
		SOV = { clr_country_flag = SOV_spanish_refugee_generals_welcome_flag }
	}
}

#POUM brigades come to aid in the Soviet Civil War
SOV_poum_brigades_to_soviet_civil_war_effect = {

	add_equipment_to_stockpile = {
		type = infantry_equipment
		amount = 6000
		producer = SPC
	}
	add_equipment_to_stockpile = {
		type = support_equipment
		amount = 600
		producer = SPC
	}

	division_template = {
		name = "International Marxist Brigades"
		is_locked = yes
		priority = 1
		template_counter = 14
		regiments = {
			infantry = { x = 0 y = 0 }
			infantry = { x = 0 y = 1 }
			infantry = { x = 1 y = 0 }
			infantry = { x = 1 y = 1 }
		}
	}

	division_template = {
		name = "POUM Special Brigades"
		is_locked = yes
		priority = 1
		template_counter = 02
		regiments = {
			infantry = { x = 0 y = 0 }
			infantry = { x = 0 y = 1 }
			infantry = { x = 0 y = 2 }
			infantry = { x = 1 y = 0 }
			infantry = { x = 1 y = 1 }
			infantry = { x = 1 y = 2 }
		}
		support = {
			engineer = { x = 0 y = 0 }
			recon = { x = 0 y = 1 }
			artillery = { x = 0 y = 2 }
			anti_tank = { x = 0 y = 3 }
		}
	}

	division_template = {
		name = "Brigada de las Juventudes del POUM"
		template_counter = 0
		division_names_group = SPD_INF_02
		is_locked = yes
		regiments = {
			infantry = { x = 0 y = 0 }
			infantry = { x = 0 y = 1 }
			infantry = { x = 0 y = 2 }
		}
		support = {
			engineer = { x = 0 y = 0 }
		}
	}

	var:SOV_civil_war_start_state = {
		create_unit = { #Int Marxist Brigades
			division = "name = \"International Marxist Brigade\" division_template = \"International Marxist Brigades\" start_experience_factor = 0.7 start_equipment_factor = 1.0" owner = SOV count = 12
		}
		create_unit = { #POUM Special Brigades
			division = "name = \"POUM Special Brigade\" division_template = \"POUM Special Brigades\" start_experience_factor = 1 start_equipment_factor = 1.0" owner = SOV count = 2
		}
		create_unit = { #Juventudes Sindicales
			division = "name = \"Brigada de las Juventudes del POUM\" division_template = \"Brigada de las Juventudes del POUM\" start_experience_factor = 0.6 start_equipment_factor = 1.0" owner = SOV count = 6
		}
	}

}

SOV_damage_relocated_military_factories_effect = {
	if = {
		limit = {
			FROM = {
				NOT = { is_fully_controlled_by = ROOT }
			}
		}
		damage_building = { type = arms_factory damage = 0.9 }
	}
	else = {
		damage_building = { type = arms_factory damage = 0.5 }
	}
}

SOV_damage_relocated_civilian_factories_effect = {
	if = {
		limit = {
			FROM = {
				NOT = { is_fully_controlled_by = ROOT }
			}
		}
		damage_building = { type = industrial_complex damage = 0.9 }
	}
	else = {
		damage_building = { type = industrial_complex damage = 0.5 }
	}
}

#RELOCATE INDUSTRY - ROOT = TAG // FROM = TARGETED STATE (to remove industry) // THIS = TARGETED STATE (to remove industry) [Effect assumed to be called inside FROM scope]
SOV_relocate_industry_in_appropriate_state_effect = {
	if = {
		limit = {
			OR = {
				has_state_flag = SOV_civs_removed_flag
				has_state_flag = SOV_mils_removed_flag
			}
		}
		ROOT = {
			if = { #Prio the Urals
				limit = {
					any_controlled_state = {
						SOV_is_industrial_relocation_destiny_state = yes
						free_building_slots = {
							building = arms_factory
							size > 1
							include_locked = yes
						}
					}
				}
				random_controlled_state = {
					limit = {
						SOV_is_industrial_relocation_destiny_state = yes
						free_building_slots = {
							building = arms_factory
							size > 1
							include_locked = yes
						}
					}
					#FIRST FACTORY - Prio Mil
					if = {
						limit = {
							FROM = { has_state_flag = SOV_mils_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = arms_factory
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_mils_removed_flag value = -1 } }
						SOV_damage_relocated_military_factories_effect = yes
					}
					else_if = {
						limit = {
							FROM = { has_state_flag = SOV_civs_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = industrial_complex
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_civs_removed_flag value = -1 } }
						SOV_damage_relocated_civilian_factories_effect = yes
					}
					#SECOND FACTORY - Prio Civ
					if = {
						limit = {
							FROM = { has_state_flag = SOV_civs_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = industrial_complex
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_civs_removed_flag value = -1 } }
						SOV_damage_relocated_civilian_factories_effect = yes
					}
					else_if = {
						limit = {
							FROM = { has_state_flag = SOV_mils_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = arms_factory
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_mils_removed_flag value = -1 } }
						SOV_damage_relocated_military_factories_effect = yes
					}
				}
			}
			else_if = { #A bunch of historical fallbacks
				limit = {
					any_controlled_state = {
						SOV_is_industrial_relocation_destiny_fallback_state = yes
						free_building_slots = {
							building = arms_factory
							size > 1
							include_locked = yes
						}
					}
				}
				random_controlled_state = {
					limit = {
						SOV_is_industrial_relocation_destiny_fallback_state = yes
						free_building_slots = {
							building = arms_factory
							size > 1
							include_locked = yes
						}
					}
					#FIRST FACTORY
					if = {
						limit = {
							FROM = { has_state_flag = SOV_mils_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = arms_factory
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_mils_removed_flag value = -1 } }
						SOV_damage_relocated_military_factories_effect = yes
					}
					else_if = {
						limit = {
							FROM = { has_state_flag = SOV_civs_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = industrial_complex
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_civs_removed_flag value = -1 } }
						SOV_damage_relocated_civilian_factories_effect = yes
					}
					#SECOND FACTORY
					if = {
						limit = {
							FROM = { has_state_flag = SOV_mils_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = arms_factory
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_mils_removed_flag value = -1 } }
						SOV_damage_relocated_military_factories_effect = yes
					}
					else_if = {
						limit = {
							FROM = { has_state_flag = SOV_civs_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = industrial_complex
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_civs_removed_flag value = -1 } }
						SOV_damage_relocated_civilian_factories_effect = yes
					}
				}
			}
			else_if = { #what the hell man? Your country is in flames you know?
				limit = {
					any_controlled_state = {
						SOV_is_industrial_relocation_destiny_state = no
						SOV_is_industrial_relocation_destiny_fallback_state = no
						is_on_continent = asia
						free_building_slots = {
							building = arms_factory
							size > 1
							include_locked = yes
						}
					}
				}
				random_controlled_state = {
					limit = {
						SOV_is_industrial_relocation_destiny_state = no
						SOV_is_industrial_relocation_destiny_fallback_state = no
						is_on_continent = asia
						free_building_slots = {
							building = arms_factory
							size > 1
							include_locked = yes
						}
					}
					#FIRST FACTORY
					if = {
						limit = {
							FROM = { has_state_flag = SOV_mils_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = arms_factory
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_mils_removed_flag value = -1 } }
						SOV_damage_relocated_military_factories_effect = yes
					}
					else_if = {
						limit = {
							FROM = { has_state_flag = SOV_civs_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = industrial_complex
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_civs_removed_flag value = -1 } }
						SOV_damage_relocated_civilian_factories_effect = yes
					}
					#SECOND FACTORY
					if = {
						limit = {
							FROM = { has_state_flag = SOV_mils_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = arms_factory
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_mils_removed_flag value = -1 } }
						SOV_damage_relocated_military_factories_effect = yes
					}
					else_if = {
						limit = {
							FROM = { has_state_flag = SOV_civs_removed_flag }
						}
						add_extra_state_shared_building_slots = 1
						add_building_construction = {
							type = industrial_complex
							level = 1
							instant_build = yes
						}
						FROM = { modify_state_flag = { flag = SOV_civs_removed_flag value = -1 } }
						SOV_damage_relocated_civilian_factories_effect = yes
					}
				}
			}
		}
	}
}

SOV_kill_trotsky_effect = { # Poor bwoy D: -> ROOT = SOV (or it doesn't work! :D)
	#log = "ROOT is [ROOT.GetName] // THIS is [THIS.GetName] // FROM is [FROM.GetName]" -> Everything is SOV but retire_character tooltip keeps saying "Retire char from [nothing]"
	set_global_flag = flag_trotsky_dead
	if = {
		limit = {
			has_idea = trotskyite_plot_nsb
		}
		swap_ideas = {
			remove_idea = trotskyite_plot_nsb
			add_idea = trotskyite_plot_purged
		}
	}
	SOV_lev_trotsky = {
		set_character_flag = SOV_character_purged_flag
		retire = yes
	}
	set_variable = { SOV.SOV_paranoia_photo_trotsky = 11 }
	MEX = {
		if = {
			limit = {
				has_country_leader = {
					character = MEX_leon_trotsky
				}
			}
			retire_ideology_leader = communism 
		}
		#retire_character = MEX_leon_trotsky #Mexico did show Trotsky as an unavailable advisor after being killed

		#THIS BELOW IS WHAT IS SCRIPTED TO HAPPEN IF TROTSKY RETURNS TO THE USSR TO RULE THERE
		#if = {
		#	limit = {
		#		has_country_leader = { character = MEX_leon_trotsky }
		#	}
		#	add_country_leader_role = {
		#		character = MEX_hernan_laborde
		#		country_leader = {
		#			expire = "1965.1.1"
		#			ideology = leninism
		#			traits = {
		#				devoted_trotskyist
		#			}
		#		}
		#		promote_leader = yes
		#	}
		#}
	}
}


########    ###    ##    ## ##    ##    ########  ########  ######  ####  ######   ##    ## ######## ########
   ##      ## ##   ###   ## ##   ##     ##     ## ##       ##    ##  ##  ##    ##  ###   ## ##       ##     ##
   ##     ##   ##  ####  ## ##  ##      ##     ## ##       ##        ##  ##        ####  ## ##       ##     ##
   ##    ##     ## ## ## ## #####       ##     ## ######    ######   ##  ##   #### ## ## ## ######   ########
   ##    ######### ##  #### ##  ##      ##     ## ##             ##  ##  ##    ##  ##  #### ##       ##   ##
   ##    ##     ## ##   ### ##   ##     ##     ## ##       ##    ##  ##  ##    ##  ##   ### ##       ##    ##
   ##    ##     ## ##    ## ##    ##    ########  ########  ######  ####  ######   ##    ## ######## ##     ##


 #######                                #
    #       ##    #    #  #    #       ##
    #      #  #   ##   #  #   #       # #
    #     #    #  # #  #  ####          #
    #     ######  #  # #  #  #          #
    #     #    #  #   ##  #   #         #
    #     #    #  #    #  #    #      #####

#Why do I do this to myself?! D:
SOV_upgrade_appropriate_tank_designer_1_effect = {
	#MOROZOV
	if = {
		limit = {
			has_idea = SOV_morozov_design_bureau
		}
		set_country_flag = SOV_merge_tank_morozov_1_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau
			add_idea = SOV_morozov_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_2
		}
		set_country_flag = SOV_merge_tank_morozov_1_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_2
			add_idea = SOV_morozov_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_3
		}
		set_country_flag = SOV_merge_tank_morozov_1_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_3
			add_idea = SOV_morozov_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_2_3
		}
		set_country_flag = SOV_merge_tank_morozov_1_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_2_3
			add_idea = SOV_morozov_design_bureau_final
		}
	}

	#ASTROV
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau
		}
		set_country_flag = SOV_merge_tank_astrov_1_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau
			add_idea = SOV_astrov_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_2
		}
		set_country_flag = SOV_merge_tank_astrov_1_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_2
			add_idea = SOV_astrov_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_3
		}
		set_country_flag = SOV_merge_tank_astrov_1_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_3
			add_idea = SOV_astrov_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_2_3
		}
		set_country_flag = SOV_merge_tank_astrov_1_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_2_3
			add_idea = SOV_astrov_design_bureau_final
		}
	}

	#OKMO
	else_if = {
		limit = {
			has_idea = SOV_okmo
		}
		set_country_flag = SOV_merge_tank_okmo_1_flag
		swap_ideas = {
			remove_idea = SOV_okmo
			add_idea = SOV_okmo_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_2
		}
		set_country_flag = SOV_merge_tank_okmo_1_flag
		swap_ideas = {
			remove_idea = SOV_okmo_2
			add_idea = SOV_okmo_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_3
		}
		set_country_flag = SOV_merge_tank_okmo_1_flag
		swap_ideas = {
			remove_idea = SOV_okmo_3
			add_idea = SOV_okmo_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_2_3
		}
		set_country_flag = SOV_merge_tank_okmo_1_flag
		swap_ideas = {
			remove_idea = SOV_okmo_2_3
			add_idea = SOV_okmo_final
		}
	}

	#mytishchy
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant
		}
		set_country_flag = SOV_merge_tank_mytishchy_1_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant
			add_idea = SOV_mytishchy_machine_building_plant_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_2
		}
		set_country_flag = SOV_merge_tank_mytishchy_1_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_2
			add_idea = SOV_mytishchy_machine_building_plant_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_3
		}
		set_country_flag = SOV_merge_tank_mytishchy_1_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_3
			add_idea = SOV_mytishchy_machine_building_plant_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_2_3
		}
		set_country_flag = SOV_merge_tank_mytishchy_1_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_2_3
			add_idea = SOV_mytishchy_machine_building_plant_final
		}
	}

	#KIROV
	else_if = {
		limit = {
			has_idea = SOV_kirov
		}
		set_country_flag = SOV_merge_tank_kirov_1_flag
		swap_ideas = {
			remove_idea = SOV_kirov
			add_idea = SOV_kirov_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_2
		}
		set_country_flag = SOV_merge_tank_kirov_1_flag
		swap_ideas = {
			remove_idea = SOV_kirov_2
			add_idea = SOV_kirov_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_3
		}
		set_country_flag = SOV_merge_tank_kirov_1_flag
		swap_ideas = {
			remove_idea = SOV_kirov_3
			add_idea = SOV_kirov_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_2_3
		}
		set_country_flag = SOV_merge_tank_kirov_1_flag
		swap_ideas = {
			remove_idea = SOV_kirov_2_3
			add_idea = SOV_kirov_final
		}
	}
}

 #######                               #####
    #       ##    #    #  #    #      #     #
    #      #  #   ##   #  #   #             #
    #     #    #  # #  #  ####         #####
    #     ######  #  # #  #  #        #
    #     #    #  #   ##  #   #       #
    #     #    #  #    #  #    #      #######


SOV_upgrade_appropriate_tank_designer_2_effect = {
	#MOROZOV
	if = {
		limit = {
			has_idea = SOV_morozov_design_bureau
		}
		set_country_flag = SOV_merge_tank_morozov_2_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau
			add_idea = SOV_morozov_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_1
		}
		set_country_flag = SOV_merge_tank_morozov_2_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_1
			add_idea = SOV_morozov_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_3
		}
		set_country_flag = SOV_merge_tank_morozov_2_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_3
			add_idea = SOV_morozov_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_1_3
		}
		set_country_flag = SOV_merge_tank_morozov_2_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_1_3
			add_idea = SOV_morozov_design_bureau_final
		}
	}

	#ASTROV
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau
		}
		set_country_flag = SOV_merge_tank_astrov_2_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau
			add_idea = SOV_astrov_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_1
		}
		set_country_flag = SOV_merge_tank_astrov_2_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_1
			add_idea = SOV_astrov_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_3
		}
		set_country_flag = SOV_merge_tank_astrov_2_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_3
			add_idea = SOV_astrov_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_1_3
		}
		set_country_flag = SOV_merge_tank_astrov_2_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_1_3
			add_idea = SOV_astrov_design_bureau_final
		}
	}

	#OKMO
	else_if = {
		limit = {
			has_idea = SOV_okmo
		}
		set_country_flag = SOV_merge_tank_okmo_2_flag
		swap_ideas = {
			remove_idea = SOV_okmo
			add_idea = SOV_okmo_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_1
		}
		set_country_flag = SOV_merge_tank_okmo_2_flag
		swap_ideas = {
			remove_idea = SOV_okmo_1
			add_idea = SOV_okmo_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_3
		}
		set_country_flag = SOV_merge_tank_okmo_2_flag
		swap_ideas = {
			remove_idea = SOV_okmo_3
			add_idea = SOV_okmo_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_1_3
		}
		set_country_flag = SOV_merge_tank_okmo_2_flag
		swap_ideas = {
			remove_idea = SOV_okmo_1_3
			add_idea = SOV_okmo_final
		}
	}

	#mytishchy
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant
		}
		set_country_flag = SOV_merge_tank_mytishchy_2_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant
			add_idea = SOV_mytishchy_machine_building_plant_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_1
		}
		set_country_flag = SOV_merge_tank_mytishchy_2_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_1
			add_idea = SOV_mytishchy_machine_building_plant_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_3
		}
		set_country_flag = SOV_merge_tank_mytishchy_2_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_3
			add_idea = SOV_mytishchy_machine_building_plant_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_1_3
		}
		set_country_flag = SOV_merge_tank_mytishchy_2_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_1_3
			add_idea = SOV_mytishchy_machine_building_plant_final
		}
	}

	#KIROV
	else_if = {
		limit = {
			has_idea = SOV_kirov
		}
		set_country_flag = SOV_merge_tank_kirov_2_flag
		swap_ideas = {
			remove_idea = SOV_kirov
			add_idea = SOV_kirov_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_1
		}
		set_country_flag = SOV_merge_tank_kirov_2_flag
		swap_ideas = {
			remove_idea = SOV_kirov_1
			add_idea = SOV_kirov_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_3
		}
		set_country_flag = SOV_merge_tank_kirov_2_flag
		swap_ideas = {
			remove_idea = SOV_kirov_3
			add_idea = SOV_kirov_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_1_3
		}
		set_country_flag = SOV_merge_tank_kirov_2_flag
		swap_ideas = {
			remove_idea = SOV_kirov_1_3
			add_idea = SOV_kirov_final
		}
	}
}

 #######                               #####
    #       ##    #    #  #    #      #     #
    #      #  #   ##   #  #   #             #
    #     #    #  # #  #  ####         #####
    #     ######  #  # #  #  #              #
    #     #    #  #   ##  #   #       #     #
    #     #    #  #    #  #    #       #####


SOV_upgrade_appropriate_tank_designer_3_effect = {
	#MOROZOV
	if = {
		limit = {
			has_idea = SOV_morozov_design_bureau
		}
		set_country_flag = SOV_merge_tank_morozov_3_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau
			add_idea = SOV_morozov_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_1
		}
		set_country_flag = SOV_merge_tank_morozov_3_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_1
			add_idea = SOV_morozov_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_2
		}
		set_country_flag = SOV_merge_tank_morozov_3_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_2
			add_idea = SOV_morozov_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_morozov_design_bureau_1_2
		}
		set_country_flag = SOV_merge_tank_morozov_3_flag
		swap_ideas = {
			remove_idea = SOV_morozov_design_bureau_1_2
			add_idea = SOV_morozov_design_bureau_final
		}
	}

	#ASTROV
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau
		}
		set_country_flag = SOV_merge_tank_astrov_3_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau
			add_idea = SOV_astrov_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_1
		}
		set_country_flag = SOV_merge_tank_astrov_3_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_1
			add_idea = SOV_astrov_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_2
		}
		set_country_flag = SOV_merge_tank_astrov_3_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_2
			add_idea = SOV_astrov_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_astrov_design_bureau_1_2
		}
		set_country_flag = SOV_merge_tank_astrov_3_flag
		swap_ideas = {
			remove_idea = SOV_astrov_design_bureau_1_2
			add_idea = SOV_astrov_design_bureau_final
		}
	}

	#OKMO
	else_if = {
		limit = {
			has_idea = SOV_okmo
		}
		set_country_flag = SOV_merge_tank_okmo_3_flag
		swap_ideas = {
			remove_idea = SOV_okmo
			add_idea = SOV_okmo_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_1
		}
		set_country_flag = SOV_merge_tank_okmo_3_flag
		swap_ideas = {
			remove_idea = SOV_okmo_1
			add_idea = SOV_okmo_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_2
		}
		set_country_flag = SOV_merge_tank_okmo_3_flag
		swap_ideas = {
			remove_idea = SOV_okmo_2
			add_idea = SOV_okmo_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_okmo_1_2
		}
		set_country_flag = SOV_merge_tank_okmo_3_flag
		swap_ideas = {
			remove_idea = SOV_okmo_1_2
			add_idea = SOV_okmo_final
		}
	}

	#mytishchy
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant
		}
		set_country_flag = SOV_merge_tank_mytishchy_3_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant
			add_idea = SOV_mytishchy_machine_building_plant_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_1
		}
		set_country_flag = SOV_merge_tank_mytishchy_3_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_1
			add_idea = SOV_mytishchy_machine_building_plant_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_2
		}
		set_country_flag = SOV_merge_tank_mytishchy_3_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_2
			add_idea = SOV_mytishchy_machine_building_plant_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mytishchy_machine_building_plant_1_2
		}
		set_country_flag = SOV_merge_tank_mytishchy_3_flag
		swap_ideas = {
			remove_idea = SOV_mytishchy_machine_building_plant_1_2
			add_idea = SOV_mytishchy_machine_building_plant_final
		}
	}

	#KIROV
	else_if = {
		limit = {
			has_idea = SOV_kirov
		}
		set_country_flag = SOV_merge_tank_kirov_3_flag
		swap_ideas = {
			remove_idea = SOV_kirov
			add_idea = SOV_kirov_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_1
		}
		set_country_flag = SOV_merge_tank_kirov_3_flag
		swap_ideas = {
			remove_idea = SOV_kirov_1
			add_idea = SOV_kirov_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_2
		}
		set_country_flag = SOV_merge_tank_kirov_3_flag
		swap_ideas = {
			remove_idea = SOV_kirov_2
			add_idea = SOV_kirov_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_kirov_1_2
		}
		set_country_flag = SOV_merge_tank_kirov_3_flag
		swap_ideas = {
			remove_idea = SOV_kirov_1_2
			add_idea = SOV_kirov_final
		}
	}
}


##    ##    ###    ##     ##    ###    ##          ########  ########  ######  ####  ######   ##    ## ######## ########
###   ##   ## ##   ##     ##   ## ##   ##          ##     ## ##       ##    ##  ##  ##    ##  ###   ## ##       ##     ##
####  ##  ##   ##  ##     ##  ##   ##  ##          ##     ## ##       ##        ##  ##        ####  ## ##       ##     ##
## ## ## ##     ## ##     ## ##     ## ##          ##     ## ######    ######   ##  ##   #### ## ## ## ######   ########
##  #### #########  ##   ##  ######### ##          ##     ## ##             ##  ##  ##    ##  ##  #### ##       ##   ##
##   ### ##     ##   ## ##   ##     ## ##          ##     ## ##       ##    ##  ##  ##    ##  ##   ### ##       ##    ##
##    ## ##     ##    ###    ##     ## ########    ########  ########  ######  ####  ######   ##    ## ######## ##     ##

 #     #                                        #        ######
 ##    #    ##    #    #    ##    #            ##        #     #    ##    #       #####  #   ####
 # #   #   #  #   #    #   #  #   #           # #        #     #   #  #   #         #    #  #    #
 #  #  #  #    #  #    #  #    #  #             #        ######   #    #  #         #    #  #
 #   # #  ######  #    #  ######  #             #        #     #  ######  #         #    #  #
 #    ##  #    #   #  #   #    #  #             #        #     #  #    #  #         #    #  #    #
 #     #  #    #    ##    #    #  ######      #####      ######   #    #  ######    #    #   ####


SOV_upgrade_appropriate_ship_designer_1_baltic_effect = {
	#NEVSKOYE DESIGN BUREAU
	if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau
			add_idea = SOV_nevskoye_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_2
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_2
			add_idea = SOV_nevskoye_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_3
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_3
			add_idea = SOV_nevskoye_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_4
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_4
			add_idea = SOV_nevskoye_design_bureau_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_2_4
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_2_4
			add_idea = SOV_nevskoye_design_bureau_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_3_4
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_3_4
			add_idea = SOV_nevskoye_design_bureau_final_3
		}
	}

	#RUBIN DESIGN BUREAU
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau
			add_idea = SOV_rubin_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_2
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_2
			add_idea = SOV_rubin_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_3
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_3
			add_idea = SOV_rubin_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_4
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_4
			add_idea = SOV_rubin_design_bureau_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_2_4
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_2_4
			add_idea = SOV_rubin_design_bureau_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_3_4
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_1_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_3_4
			add_idea = SOV_rubin_design_bureau_final_3
		}
	}
}

 #     #                                       #####       ######
 ##    #    ##    #    #    ##    #           #     #      #     #    ##    #       #####  #   ####
 # #   #   #  #   #    #   #  #   #                 #      #     #   #  #   #         #    #  #    #
 #  #  #  #    #  #    #  #    #  #            #####       ######   #    #  #         #    #  #
 #   # #  ######  #    #  ######  #           #            #     #  ######  #         #    #  #
 #    ##  #    #   #  #   #    #  #           #            #     #  #    #  #         #    #  #    #
 #     #  #    #    ##    #    #  ######      #######      ######   #    #  ######    #    #   ####


SOV_upgrade_appropriate_ship_designer_2_baltic_effect = {
	#NEVSKOYE DESIGN BUREAU
	if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau
			add_idea = SOV_nevskoye_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_1
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_1
			add_idea = SOV_nevskoye_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_4
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_4
			add_idea = SOV_nevskoye_design_bureau_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_1_4
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_1_4
			add_idea = SOV_nevskoye_design_bureau_final_2
		}
	}

	#RUBIN DESIGN BUREAU
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau
			add_idea = SOV_rubin_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_1
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_1
			add_idea = SOV_rubin_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_4
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_4
			add_idea = SOV_rubin_design_bureau_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_1_4
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_2_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_1_4
			add_idea = SOV_rubin_design_bureau_final_2
		}
	}
}

 #     #                                       #####       ######
 ##    #    ##    #    #    ##    #           #     #      #     #    ##    #       #####  #   ####
 # #   #   #  #   #    #   #  #   #                 #      #     #   #  #   #         #    #  #    #
 #  #  #  #    #  #    #  #    #  #            #####       ######   #    #  #         #    #  #
 #   # #  ######  #    #  ######  #                 #      #     #  ######  #         #    #  #
 #    ##  #    #   #  #   #    #  #           #     #      #     #  #    #  #         #    #  #    #
 #     #  #    #    ##    #    #  ######       #####       ######   #    #  ######    #    #   ####

SOV_upgrade_appropriate_ship_designer_3_baltic_effect = {
	#NEVSKOYE DESIGN BUREAU
	if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau
			add_idea = SOV_nevskoye_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_1
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_1
			add_idea = SOV_nevskoye_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_4
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_4
			add_idea = SOV_nevskoye_design_bureau_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_1_4
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_1_4
			add_idea = SOV_nevskoye_design_bureau_final_3
		}
	}

	#RUBIN DESIGN BUREAU
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau
			add_idea = SOV_rubin_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_1
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_1
			add_idea = SOV_rubin_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_4
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_4
			add_idea = SOV_rubin_design_bureau_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_1_4
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_3_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_1_4
			add_idea = SOV_rubin_design_bureau_final_3
		}
	}
}

 #     #                                      #            ######
 ##    #    ##    #    #    ##    #           #    #       #     #    ##    #       #####  #   ####
 # #   #   #  #   #    #   #  #   #           #    #       #     #   #  #   #         #    #  #    #
 #  #  #  #    #  #    #  #    #  #           #    #       ######   #    #  #         #    #  #
 #   # #  ######  #    #  ######  #           #######      #     #  ######  #         #    #  #
 #    ##  #    #   #  #   #    #  #                #       #     #  #    #  #         #    #  #    #
 #     #  #    #    ##    #    #  ######           #       ######   #    #  ######    #    #   ####


SOV_upgrade_appropriate_ship_designer_4_baltic_effect = {
	#NEVSKOYE DESIGN BUREAU
	if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau
			add_idea = SOV_nevskoye_design_bureau_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_1
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_1
			add_idea = SOV_nevskoye_design_bureau_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_2
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_2
			add_idea = SOV_nevskoye_design_bureau_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_3
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_3
			add_idea = SOV_nevskoye_design_bureau_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_1_2
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_1_2
			add_idea = SOV_nevskoye_design_bureau_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_nevskoye_design_bureau_1_3
		}
		set_country_flag = SOV_merge_ship_nevskoye_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_nevskoye_design_bureau_1_3
			add_idea = SOV_nevskoye_design_bureau_final_3
		}
	}

	#RUBIN DESIGN BUREAU
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau
			add_idea = SOV_rubin_design_bureau_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_1
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_1
			add_idea = SOV_rubin_design_bureau_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_2
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_2
			add_idea = SOV_rubin_design_bureau_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_3
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_3
			add_idea = SOV_rubin_design_bureau_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_1_2
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_1_2
			add_idea = SOV_rubin_design_bureau_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_rubin_design_bureau_1_3
		}
		set_country_flag = SOV_merge_ship_rubin_design_bureau_4_flag
		swap_ideas = {
			remove_idea = SOV_rubin_design_bureau_1_3
			add_idea = SOV_rubin_design_bureau_final_3
		}
	}
}

 #     #                                        #        ######                                        #####
 ##    #    ##    #    #    ##    #            ##        #     #  #         ##     ####   #    #      #     #  ######    ##
 # #   #   #  #   #    #   #  #   #           # #        #     #  #        #  #   #    #  #   #       #        #        #  #
 #  #  #  #    #  #    #  #    #  #             #        ######   #       #    #  #       ####         #####   #####   #    #
 #   # #  ######  #    #  ######  #             #        #     #  #       ######  #       #  #              #  #       ######
 #    ##  #    #   #  #   #    #  #             #        #     #  #       #    #  #    #  #   #       #     #  #       #    #
 #     #  #    #    ##    #    #  ######      #####      ######   ######  #    #   ####   #    #       #####   ######  #    #


SOV_upgrade_appropriate_ship_designer_1_black_sea_effect = {
	#BLACK SEA SHIPYARD
	if = {
		limit = {
			has_idea = SOV_black_sea_shipyard
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_1_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard
			add_idea = SOV_black_sea_shipyard_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_2
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_1_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_2
			add_idea = SOV_black_sea_shipyard_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_3
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_1_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_3
			add_idea = SOV_black_sea_shipyard_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_4
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_1_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_4
			add_idea = SOV_black_sea_shipyard_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_2_4
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_1_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_2_4
			add_idea = SOV_black_sea_shipyard_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_3_4
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_1_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_3_4
			add_idea = SOV_black_sea_shipyard_final_3
		}
	}

	#SEVASTOPOL MARINE PLANT
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_1_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant
			add_idea = SOV_sevastopol_marine_plant_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_2
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_1_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_2
			add_idea = SOV_sevastopol_marine_plant_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_3
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_1_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_3
			add_idea = SOV_sevastopol_marine_plant_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_4
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_1_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_4
			add_idea = SOV_sevastopol_marine_plant_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_2_4
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_1_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_2_4
			add_idea = SOV_sevastopol_marine_plant_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_3_4
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_1_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_3_4
			add_idea = SOV_sevastopol_marine_plant_final_3
		}
	}
}

 #     #                                       #####       ######                                        #####
 ##    #    ##    #    #    ##    #           #     #      #     #  #         ##     ####   #    #      #     #  ######    ##
 # #   #   #  #   #    #   #  #   #                 #      #     #  #        #  #   #    #  #   #       #        #        #  #
 #  #  #  #    #  #    #  #    #  #            #####       ######   #       #    #  #       ####         #####   #####   #    #
 #   # #  ######  #    #  ######  #           #            #     #  #       ######  #       #  #              #  #       ######
 #    ##  #    #   #  #   #    #  #           #            #     #  #       #    #  #    #  #   #       #     #  #       #    #
 #     #  #    #    ##    #    #  ######      #######      ######   ######  #    #   ####   #    #       #####   ######  #    #


SOV_upgrade_appropriate_ship_designer_2_black_sea_effect = {
	#BLACK SEA SHIPYARD
	if = {
		limit = {
			has_idea = SOV_black_sea_shipyard
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_2_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard
			add_idea = SOV_black_sea_shipyard_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_1
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_2_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_1
			add_idea = SOV_black_sea_shipyard_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_4
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_2_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_4
			add_idea = SOV_black_sea_shipyard_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_1_4
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_2_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_1_4
			add_idea = SOV_black_sea_shipyard_final_2
		}
	}

	#SEVASTOPOL MARINE PLANT
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_2_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant
			add_idea = SOV_sevastopol_marine_plant_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_1
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_2_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_1
			add_idea = SOV_sevastopol_marine_plant_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_4
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_2_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_4
			add_idea = SOV_sevastopol_marine_plant_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_1_4
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_2_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_1_4
			add_idea = SOV_sevastopol_marine_plant_final_2
		}
	}
}

 #     #                                       #####       ######                                        #####
 ##    #    ##    #    #    ##    #           #     #      #     #  #         ##     ####   #    #      #     #  ######    ##
 # #   #   #  #   #    #   #  #   #                 #      #     #  #        #  #   #    #  #   #       #        #        #  #
 #  #  #  #    #  #    #  #    #  #            #####       ######   #       #    #  #       ####         #####   #####   #    #
 #   # #  ######  #    #  ######  #                 #      #     #  #       ######  #       #  #              #  #       ######
 #    ##  #    #   #  #   #    #  #           #     #      #     #  #       #    #  #    #  #   #       #     #  #       #    #
 #     #  #    #    ##    #    #  ######       #####       ######   ######  #    #   ####   #    #       #####   ######  #    #


SOV_upgrade_appropriate_ship_designer_3_black_sea_effect = {
	#BLACK SEA SHIPYARD
	if = {
		limit = {
			has_idea = SOV_black_sea_shipyard
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_3_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard
			add_idea = SOV_black_sea_shipyard_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_1
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_3_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_1
			add_idea = SOV_black_sea_shipyard_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_4
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_3_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_4
			add_idea = SOV_black_sea_shipyard_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_1_4
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_3_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_1_4
			add_idea = SOV_black_sea_shipyard_final_3
		}
	}

	#SEVASTOPOL MARINE PLANT
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_3_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant
			add_idea = SOV_sevastopol_marine_plant_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_1
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_3_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_1
			add_idea = SOV_sevastopol_marine_plant_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_4
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_3_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_4
			add_idea = SOV_sevastopol_marine_plant_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_1_4
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_3_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_1_4
			add_idea = SOV_sevastopol_marine_plant_final_3
		}
	}
}

 #     #                                      #            ######                                        #####
 ##    #    ##    #    #    ##    #           #    #       #     #  #         ##     ####   #    #      #     #  ######    ##
 # #   #   #  #   #    #   #  #   #           #    #       #     #  #        #  #   #    #  #   #       #        #        #  #
 #  #  #  #    #  #    #  #    #  #           #    #       ######   #       #    #  #       ####         #####   #####   #    #
 #   # #  ######  #    #  ######  #           #######      #     #  #       ######  #       #  #              #  #       ######
 #    ##  #    #   #  #   #    #  #                #       #     #  #       #    #  #    #  #   #       #     #  #       #    #
 #     #  #    #    ##    #    #  ######           #       ######   ######  #    #   ####   #    #       #####   ######  #    #


SOV_upgrade_appropriate_ship_designer_4_black_sea_effect = {
	#BLACK SEA SHIPYARD
	if = {
		limit = {
			has_idea = SOV_black_sea_shipyard
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_4_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard
			add_idea = SOV_black_sea_shipyard_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_1
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_4_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_1
			add_idea = SOV_black_sea_shipyard_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_2
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_4_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_2
			add_idea = SOV_black_sea_shipyard_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_3
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_4_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_3
			add_idea = SOV_black_sea_shipyard_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_1_2
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_4_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_1_2
			add_idea = SOV_black_sea_shipyard_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_black_sea_shipyard_1_3
		}
		set_country_flag = SOV_merge_ship_black_sea_shipyard_4_flag
		swap_ideas = {
			remove_idea = SOV_black_sea_shipyard_1_3
			add_idea = SOV_black_sea_shipyard_final_3
		}
	}

	#SEVASTOPOL MARINE PLANT
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_4_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant
			add_idea = SOV_sevastopol_marine_plant_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_1
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_4_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_1
			add_idea = SOV_sevastopol_marine_plant_1_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_2
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_4_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_2
			add_idea = SOV_sevastopol_marine_plant_2_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_3
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_4_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_3
			add_idea = SOV_sevastopol_marine_plant_3_4
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_1_2
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_4_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_1_2
			add_idea = SOV_sevastopol_marine_plant_final_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_sevastopol_marine_plant_1_3
		}
		set_country_flag = SOV_merge_ship_sevastopol_marine_plant_4_flag
		swap_ideas = {
			remove_idea = SOV_sevastopol_marine_plant_1_3
			add_idea = SOV_sevastopol_marine_plant_final_3
		}
	}
}


   ###    #### ########   ######  ########     ###    ######## ########    ########  ########  ######  ####  ######   ##    ## ######## ########
  ## ##    ##  ##     ## ##    ## ##     ##   ## ##   ##          ##       ##     ## ##       ##    ##  ##  ##    ##  ###   ## ##       ##     ##
 ##   ##   ##  ##     ## ##       ##     ##  ##   ##  ##          ##       ##     ## ##       ##        ##  ##        ####  ## ##       ##     ##
##     ##  ##  ########  ##       ########  ##     ## ######      ##       ##     ## ######    ######   ##  ##   #### ## ## ## ######   ########
#########  ##  ##   ##   ##       ##   ##   ######### ##          ##       ##     ## ##             ##  ##  ##    ##  ##  #### ##       ##   ##
##     ##  ##  ##    ##  ##    ## ##    ##  ##     ## ##          ##       ##     ## ##       ##    ##  ##  ##    ##  ##   ### ##       ##    ##
##     ## #### ##     ##  ######  ##     ## ##     ## ##          ##       ########  ########  ######  ####  ######   ##    ## ######## ##     ##

    #                                                             #
   # #    #  #####    ####   #####     ##    ######  #####       ##
  #   #   #  #    #  #    #  #    #   #  #   #         #        # #
 #     #  #  #    #  #       #    #  #    #  #####     #          #
 #######  #  #####   #       #####   ######  #         #          #
 #     #  #  #   #   #    #  #   #   #    #  #         #          #
 #     #  #  #    #   ####   #    #  #    #  #         #        #####


SOV_upgrade_appropriate_aircraft_designer_1_effect = {
	#MIG
	if = {
		limit = {
			has_idea = SOV_mig_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_mig_1_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau
			add_idea = SOV_mig_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_mig_1_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_2
			add_idea = SOV_mig_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_mig_1_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_3
			add_idea = SOV_mig_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_2_3
		}
		set_country_flag = SOV_merge_aircraft_mig_1_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_2_3
			add_idea = SOV_mig_design_bureau_final
		}
	}

	#ILYUSHIN
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_1_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau
			add_idea = SOV_ilyushin_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_1_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_2
			add_idea = SOV_ilyushin_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_1_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_3
			add_idea = SOV_ilyushin_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_2_3
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_1_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_2_3
			add_idea = SOV_ilyushin_design_bureau_final
		}
	}

	#TUPOLEV
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_tupolev_1_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau
			add_idea = SOV_tupolev_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_tupolev_1_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_2
			add_idea = SOV_tupolev_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_tupolev_1_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_3
			add_idea = SOV_tupolev_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_2_3
		}
		set_country_flag = SOV_merge_aircraft_tupolev_1_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_2_3
			add_idea = SOV_tupolev_design_bureau_final
		}
	}

	#YAKOVLEV
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_1_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau
			add_idea = SOV_yakovlev_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_1_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_2
			add_idea = SOV_yakovlev_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_1_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_3
			add_idea = SOV_yakovlev_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_2_3
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_1_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_2_3
			add_idea = SOV_yakovlev_design_bureau_final
		}
	}
}

    #                                                            #####
   # #    #  #####    ####   #####     ##    ######  #####      #     #
  #   #   #  #    #  #    #  #    #   #  #   #         #              #
 #     #  #  #    #  #       #    #  #    #  #####     #         #####
 #######  #  #####   #       #####   ######  #         #        #
 #     #  #  #   #   #    #  #   #   #    #  #         #        #
 #     #  #  #    #   ####   #    #  #    #  #         #        #######


SOV_upgrade_appropriate_aircraft_designer_2_effect = {
	#MIG
	if = {
		limit = {
			has_idea = SOV_mig_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_mig_2_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau
			add_idea = SOV_mig_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_mig_2_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_1
			add_idea = SOV_mig_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_mig_2_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_3
			add_idea = SOV_mig_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_1_3
		}
		set_country_flag = SOV_merge_aircraft_mig_2_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_1_3
			add_idea = SOV_mig_design_bureau_final
		}
	}

	#ILYUSHIN
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_2_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau
			add_idea = SOV_ilyushin_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_2_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_1
			add_idea = SOV_ilyushin_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_2_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_3
			add_idea = SOV_ilyushin_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_1_3
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_2_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_1_3
			add_idea = SOV_ilyushin_design_bureau_final
		}
	}

	#TUPOLEV
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_tupolev_2_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau
			add_idea = SOV_tupolev_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_tupolev_2_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_1
			add_idea = SOV_tupolev_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_tupolev_2_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_3
			add_idea = SOV_tupolev_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_1_3
		}
		set_country_flag = SOV_merge_aircraft_tupolev_2_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_1_3
			add_idea = SOV_tupolev_design_bureau_final
		}
	}

	#YAKOVLEV
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_2_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau
			add_idea = SOV_yakovlev_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_2_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_1
			add_idea = SOV_yakovlev_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_3
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_2_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_3
			add_idea = SOV_yakovlev_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_1_3
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_2_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_1_3
			add_idea = SOV_yakovlev_design_bureau_final
		}
	}
}

    #                                                            #####
   # #    #  #####    ####   #####     ##    ######  #####      #     #
  #   #   #  #    #  #    #  #    #   #  #   #         #              #
 #     #  #  #    #  #       #    #  #    #  #####     #         #####
 #######  #  #####   #       #####   ######  #         #              #
 #     #  #  #   #   #    #  #   #   #    #  #         #        #     #
 #     #  #  #    #   ####   #    #  #    #  #         #         #####


SOV_upgrade_appropriate_aircraft_designer_3_effect = {
	#MIG
	if = {
		limit = {
			has_idea = SOV_mig_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_mig_3_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau
			add_idea = SOV_mig_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_mig_3_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_1
			add_idea = SOV_mig_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_mig_3_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_2
			add_idea = SOV_mig_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_mig_design_bureau_1_2
		}
		set_country_flag = SOV_merge_aircraft_mig_3_flag
		swap_ideas = {
			remove_idea = SOV_mig_design_bureau_1_2
			add_idea = SOV_mig_design_bureau_final
		}
	}

	#ILYUSHIN
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_3_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau
			add_idea = SOV_ilyushin_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_3_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_1
			add_idea = SOV_ilyushin_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_3_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_2
			add_idea = SOV_ilyushin_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_ilyushin_design_bureau_1_2
		}
		set_country_flag = SOV_merge_aircraft_ilyushin_3_flag
		swap_ideas = {
			remove_idea = SOV_ilyushin_design_bureau_1_2
			add_idea = SOV_ilyushin_design_bureau_final
		}
	}

	#TUPOLEV
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_tupolev_3_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau
			add_idea = SOV_tupolev_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_tupolev_3_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_1
			add_idea = SOV_tupolev_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_tupolev_3_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_2
			add_idea = SOV_tupolev_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tupolev_design_bureau_1_2
		}
		set_country_flag = SOV_merge_aircraft_tupolev_3_flag
		swap_ideas = {
			remove_idea = SOV_tupolev_design_bureau_1_2
			add_idea = SOV_tupolev_design_bureau_final
		}
	}

	#YAKOVLEV
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_3_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau
			add_idea = SOV_yakovlev_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_1
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_3_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_1
			add_idea = SOV_yakovlev_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_2
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_3_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_2
			add_idea = SOV_yakovlev_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_yakovlev_design_bureau_1_2
		}
		set_country_flag = SOV_merge_aircraft_yakovlev_3_flag
		swap_ideas = {
			remove_idea = SOV_yakovlev_design_bureau_1_2
			add_idea = SOV_yakovlev_design_bureau_final
		}
	}
}


##     ##    ###    ######## ######## ########  #### ######## ##          ########  ########  ######  ####  ######   ##    ## ######## ########
###   ###   ## ##      ##    ##       ##     ##  ##  ##       ##          ##     ## ##       ##    ##  ##  ##    ##  ###   ## ##       ##     ##
#### ####  ##   ##     ##    ##       ##     ##  ##  ##       ##          ##     ## ##       ##        ##  ##        ####  ## ##       ##     ##
## ### ## ##     ##    ##    ######   ########   ##  ######   ##          ##     ## ######    ######   ##  ##   #### ## ## ## ######   ########
##     ## #########    ##    ##       ##   ##    ##  ##       ##          ##     ## ##             ##  ##  ##    ##  ##  #### ##       ##   ##
##     ## ##     ##    ##    ##       ##    ##   ##  ##       ##          ##     ## ##       ##    ##  ##  ##    ##  ##   ### ##       ##    ##
##     ## ##     ##    ##    ######## ##     ## #### ######## ########    ########  ########  ######  ####  ######   ##    ## ######## ##     ##

 #     #                                                          #
 ##   ##    ##    #####  ######  #####   #  ######  #            ##
 # # # #   #  #     #    #       #    #  #  #       #           # #
 #  #  #  #    #    #    #####   #    #  #  #####   #             #
 #     #  ######    #    #       #####   #  #       #             #
 #     #  #    #    #    #       #   #   #  #       #             #
 #     #  #    #    #    ######  #    #  #  ######  ######      #####

SOV_upgrade_appropriate_materiel_designer_1_artillery_effect = {
	#GRABIN
	if = {
		limit = {
			has_idea = SOV_grabin_design_bureau
		}
		set_country_flag = SOV_merge_materiel_grabin_1_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau
			add_idea = SOV_grabin_design_bureau_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_2
		}
		set_country_flag = SOV_merge_materiel_grabin_1_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_2
			add_idea = SOV_grabin_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_3
		}
		set_country_flag = SOV_merge_materiel_grabin_1_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_3
			add_idea = SOV_grabin_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_2_3
		}
		set_country_flag = SOV_merge_materiel_grabin_1_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_2_3
			add_idea = SOV_grabin_design_bureau_final
		}
	}
}

SOV_upgrade_appropriate_materiel_designer_1_infantry_effect = {
	#TULA
	if = {
		limit = {
			has_idea = SOV_tula_arms_plant
		}
		set_country_flag = SOV_merge_materiel_tula_1_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant
			add_idea = SOV_tula_arms_plant_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_2
		}
		set_country_flag = SOV_merge_materiel_tula_1_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_2
			add_idea = SOV_tula_arms_plant_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_3
		}
		set_country_flag = SOV_merge_materiel_tula_1_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_3
			add_idea = SOV_tula_arms_plant_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_2_3
		}
		set_country_flag = SOV_merge_materiel_tula_1_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_2_3
			add_idea = SOV_tula_arms_plant_final
		}
	}
}

SOV_upgrade_appropriate_materiel_designer_1_motorized_effect = {
	#GAZ
	if = {
		limit = {
			has_idea = SOV_gaz
		}
		set_country_flag = SOV_merge_materiel_gaz_1_flag
		swap_ideas = {
			remove_idea = SOV_gaz
			add_idea = SOV_gaz_1
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_2
		}
		set_country_flag = SOV_merge_materiel_gaz_1_flag
		swap_ideas = {
			remove_idea = SOV_gaz_2
			add_idea = SOV_gaz_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_3
		}
		set_country_flag = SOV_merge_materiel_gaz_1_flag
		swap_ideas = {
			remove_idea = SOV_gaz_3
			add_idea = SOV_gaz_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_2_3
		}
		set_country_flag = SOV_merge_materiel_gaz_1_flag
		swap_ideas = {
			remove_idea = SOV_gaz_2_3
			add_idea = SOV_gaz_final
		}
	}
}

 #     #                                                         #####
 ##   ##    ##    #####  ######  #####   #  ######  #           #     #
 # # # #   #  #     #    #       #    #  #  #       #                 #
 #  #  #  #    #    #    #####   #    #  #  #####   #            #####
 #     #  ######    #    #       #####   #  #       #           #
 #     #  #    #    #    #       #   #   #  #       #           #
 #     #  #    #    #    ######  #    #  #  ######  ######      #######


SOV_upgrade_appropriate_materiel_designer_2_effect = {
	#GRABIN
	if = {
		limit = {
			has_idea = SOV_grabin_design_bureau
		}
		set_country_flag = SOV_merge_materiel_grabin_2_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau
			add_idea = SOV_grabin_design_bureau_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_1
		}
		set_country_flag = SOV_merge_materiel_grabin_2_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_1
			add_idea = SOV_grabin_design_bureau_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_3
		}
		set_country_flag = SOV_merge_materiel_grabin_2_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_3
			add_idea = SOV_grabin_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_1_3
		}
		set_country_flag = SOV_merge_materiel_grabin_2_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_1_3
			add_idea = SOV_grabin_design_bureau_final
		}
	}

	#TULA
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant
		}
		set_country_flag = SOV_merge_materiel_tula_2_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant
			add_idea = SOV_tula_arms_plant_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_1
		}
		set_country_flag = SOV_merge_materiel_tula_2_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_1
			add_idea = SOV_tula_arms_plant_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_3
		}
		set_country_flag = SOV_merge_materiel_tula_2_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_3
			add_idea = SOV_tula_arms_plant_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_1_3
		}
		set_country_flag = SOV_merge_materiel_tula_2_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_1_3
			add_idea = SOV_tula_arms_plant_final
		}
	}

	#GAZ
	else_if = {
		limit = {
			has_idea = SOV_gaz
		}
		set_country_flag = SOV_merge_materiel_gaz_2_flag
		swap_ideas = {
			remove_idea = SOV_gaz
			add_idea = SOV_gaz_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_1
		}
		set_country_flag = SOV_merge_materiel_gaz_2_flag
		swap_ideas = {
			remove_idea = SOV_gaz_1
			add_idea = SOV_gaz_1_2
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_3
		}
		set_country_flag = SOV_merge_materiel_gaz_2_flag
		swap_ideas = {
			remove_idea = SOV_gaz_3
			add_idea = SOV_gaz_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_1_3
		}
		set_country_flag = SOV_merge_materiel_gaz_2_flag
		swap_ideas = {
			remove_idea = SOV_gaz_1_3
			add_idea = SOV_gaz_final
		}
	}
}

 #     #                                                         #####
 ##   ##    ##    #####  ######  #####   #  ######  #           #     #
 # # # #   #  #     #    #       #    #  #  #       #                 #
 #  #  #  #    #    #    #####   #    #  #  #####   #            #####
 #     #  ######    #    #       #####   #  #       #                 #
 #     #  #    #    #    #       #   #   #  #       #           #     #
 #     #  #    #    #    ######  #    #  #  ######  ######       #####

SOV_upgrade_appropriate_materiel_designer_3_effect = {
	#GRABIN
	if = {
		limit = {
			has_idea = SOV_grabin_design_bureau
		}
		set_country_flag = SOV_merge_materiel_grabin_3_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau
			add_idea = SOV_grabin_design_bureau_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_1
		}
		set_country_flag = SOV_merge_materiel_grabin_3_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_1
			add_idea = SOV_grabin_design_bureau_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_2
		}
		set_country_flag = SOV_merge_materiel_grabin_3_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_2
			add_idea = SOV_grabin_design_bureau_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_grabin_design_bureau_1_2
		}
		set_country_flag = SOV_merge_materiel_grabin_3_flag
		swap_ideas = {
			remove_idea = SOV_grabin_design_bureau_1_2
			add_idea = SOV_grabin_design_bureau_final
		}
	}

	#TULA
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant
		}
		set_country_flag = SOV_merge_materiel_tula_3_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant
			add_idea = SOV_tula_arms_plant_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_1
		}
		set_country_flag = SOV_merge_materiel_tula_3_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_1
			add_idea = SOV_tula_arms_plant_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_2
		}
		set_country_flag = SOV_merge_materiel_tula_3_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_2
			add_idea = SOV_tula_arms_plant_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_tula_arms_plant_1_2
		}
		set_country_flag = SOV_merge_materiel_tula_3_flag
		swap_ideas = {
			remove_idea = SOV_tula_arms_plant_1_2
			add_idea = SOV_tula_arms_plant_final
		}
	}

	#GAZ
	else_if = {
		limit = {
			has_idea = SOV_gaz
		}
		set_country_flag = SOV_merge_materiel_gaz_3_flag
		swap_ideas = {
			remove_idea = SOV_gaz
			add_idea = SOV_gaz_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_1
		}
		set_country_flag = SOV_merge_materiel_gaz_3_flag
		swap_ideas = {
			remove_idea = SOV_gaz_1
			add_idea = SOV_gaz_1_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_2
		}
		set_country_flag = SOV_merge_materiel_gaz_3_flag
		swap_ideas = {
			remove_idea = SOV_gaz_2
			add_idea = SOV_gaz_2_3
		}
	}
	else_if = {
		limit = {
			has_idea = SOV_gaz_1_2
		}
		set_country_flag = SOV_merge_materiel_gaz_3_flag
		swap_ideas = {
			remove_idea = SOV_gaz_1_2
			add_idea = SOV_gaz_final
		}
	}
}

 ## #   #  ##  ###       ##  #  # #  # ###   ##  #  #      ## ###  ### ###  ### ###  ##
#   #   # #  # #  #     #  # #  # #  # #  # #  # #  #     #   #  #  #  #  #  #   #  #
 #  # # # #### ###      #    #### #  # ###  #    ####      #  ###   #  ###   #   #   #
  # # # # #  # #        #  # #  # #  # #  # #  # #  #       # #     #  #  #  #   #    #
##   # #  #  # #         ##  #  #  ##  #  #  ##  #  #     ##  #    ### #  # ###  #  ##


SOV_increase_church_power = {
	if = {
		limit = { has_idea = SOV_church_power_4 }
		add_political_power = 25
	}
	else_if = {
		limit = { has_idea = SOV_church_power_3 }
		swap_ideas = {
			remove_idea = SOV_church_power_3
			add_idea = SOV_church_power_4
		}
	}
	else_if = {
		limit = { has_idea = SOV_church_power_2 }
		swap_ideas = {
			remove_idea = SOV_church_power_2
			add_idea = SOV_church_power_3
		}
	}
	else_if = {
		limit = { has_idea = SOV_church_power_1 }
		swap_ideas = {
			remove_idea = SOV_church_power_1
			add_idea = SOV_church_power_2
		}
	}
	else = {
		effect_tooltip = {
			swap_ideas = {
				remove_idea = SOV_church_power_1
				add_idea = SOV_church_power_2
			}
		}
	}
}

SOV_decrease_church_power = {
	if = {
		limit = { has_idea = SOV_church_power_1 }
		add_political_power = -25
	}
	else_if = {
		limit = { has_idea = SOV_church_power_2 }
		swap_ideas = {
			remove_idea = SOV_church_power_2
			add_idea = SOV_church_power_1
		}
	}
	else_if = {
		limit = { has_idea = SOV_church_power_3 }
		swap_ideas = {
			remove_idea = SOV_church_power_3
			add_idea = SOV_church_power_2
		}
	}
	else_if = {
		limit = { has_idea = SOV_church_power_4 }
		swap_ideas = {
			remove_idea = SOV_church_power_4
			add_idea = SOV_church_power_3
		}
	}
	else = {
		effect_tooltip = {
			add_political_power = -25
		}
	}
}

###########################################################################################################################

 ##   ##  #   # #   # #  # #  # ###  ## ###      ##  ### #   # ### #       #   #  ##  ###      ### ### ### ###  ##  ###  ##
#  # #  # ## ## ## ## #  # ## #  #  #    #      #  #  #  #   #  #  #       #   # #  # #  #     #   #   #   #   #  #  #  #
#    #  # # # # # # # #  # # ##  #   #   #      #     #   # #   #  #       # # # #### ###      ##  ##  ##  ##  #     #   #
#  # #  # #   # #   # #  # #  #  #    #  #      #  #  #   # #   #  #       # # # #  # #  #     #   #   #   #   #  #  #    #
 ##   ##  #   # #   #  ##  #  # ### ##   #       ##  ###   #   ### ###      # #  #  # #  #     ### #   #   ###  ##   #  ##

###########################################################################################################################

SOV_set_up_nkvd = {
	random_owned_state = {
		limit = { is_capital = yes }

		create_unit = {
			division = "name = \"5ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"6ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"7ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"8ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"9ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"10ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"11ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"12ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"13ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"14ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"15ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"16ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"17ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
		create_unit = {
			division = "name = \"18ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
		}
	}

	IF = {
		limit = {
			OR = {
				AND = {
					has_idea = SOV_genrikh_yagoda
					SOV_genrikh_yagoda = { has_character_flag = SOV_aligned_right_opposition_flag }
				}
				AND = {
					has_idea = SOV_nikolay_yezhov
					SOV_nikolay_yezhov = { has_character_flag = SOV_aligned_right_opposition_flag }
				}
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"1ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
			}
			create_unit = {
				division = "name = \"2ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
			}
			create_unit = {
				division = "name = \"3ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
			}
			create_unit = {
				division = "name = \"4ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = PREV
			}
		}
	}
	IF = {
		limit = {}
		random_owned_controlled_state = {
			create_unit = {
				division = "division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = ROOT count = var:SOV.SOV_nkvd_divisions
			}

		}
	}
}

SOV_set_up_red_army = {

	delete_unit_template_and_units = {
	    division_template = "Strelkovaya Diviziya"
	    disband = yes
	}
	division_template = {
		name = "Strelkovaya Diviziya"
		division_names_group = SOV_INF_01

		regiments = {
			infantry = { x = 0 y = 0 }
			infantry = { x = 0 y = 1 }
			infantry = { x = 0 y = 2 }
			infantry = { x = 1 y = 0 }
			infantry = { x = 1 y = 1 }
			infantry = { x = 1 y = 2 }
			infantry = { x = 2 y = 0 }
			infantry = { x = 2 y = 1 }
			infantry = { x = 2 y = 2 }
		}
		support = {
			artillery = { x = 0 y = 0 }
		}
	}
	delete_unit_template_and_units = {
	    division_template = "Motostrelkovaya Diviziya"
	    disband = yes
	}

	division_template = {
		name = "Motostrelkovaya Diviziya"
		division_names_group = SOV_MOT_01
		regiments = {
			motorized = { x = 0 y = 0 }
			motorized = { x = 0 y = 1 }
			motorized = { x = 0 y = 2 }
			motorized = { x = 1 y = 0 }
			motorized = { x = 1 y = 1 }
			motorized = { x = 1 y = 2 }
		}
	}
	delete_unit_template_and_units = {
	    division_template = "Kavaleriyskaya Diviziya"
	    disband = yes
	}
	division_template = {
		name = "Kavaleriyskaya Diviziya" 	# Cavalry Division
		division_names_group = SOV_CAV_01

		regiments = {
			cavalry = { x = 0 y = 0 }
			cavalry = { x = 0 y = 1 }
			cavalry = { x = 1 y = 0 }
			cavalry = { x = 1 y = 1 }
		}
	}
	var:SOV.SOV_stalinist_civil_war_start_state = {

		create_unit = {
			division = "name = \"Strelkovaya Diviziya\" division_template = \"Strelkovaya Diviziya\" start_experience_factor = 0.1 start_equipment_factor = 1.0" owner = PREV count = var:PREV.civil_war_army_size
		}
	}
	random_owned_state = {
		limit = {
			NOT = { state = var:SOV.SOV_stalinist_civil_war_start_state  }
		}
		create_unit = {
			division = "name = \"Strelkovaya Diviziya\" division_template = \"Strelkovaya Diviziya\" start_experience_factor = 0.1 start_equipment_factor = 1.0" owner = PREV count = var:PREV.civil_war_army_size
		}
	}
	random_owned_state = {
		limit = {
			NOT = { state = var:SOV.SOV_stalinist_civil_war_start_state  }
			any_neighbor_state = { state = var:SOV.SOV_stalinist_civil_war_start_state  }
		}
		create_unit = {
			division = "name = \"Moskva Proletarskaya Motostrelkovaya Diviziya\" division_template = \"Motostrelkovaya Diviziya\" start_experience_factor = 0.1 start_equipment_factor = 1.0" owner = PREV count = 1
		}
	}
	random_owned_state = {
		limit = {
			any_neighbor_state = { state = var:SOV.SOV_stalinist_civil_war_start_state }
		}
		create_unit = {
			division = "name = \"Kavaleriyskaya Diviziya\" division_template = \"Kavaleriyskaya Diviziya\" start_experience_factor = 0.1 start_equipment_factor = 1.0" owner = PREV count = var:PREV.civil_war_army_size
		}
	}
	clear_variable = PREV.civil_war_army_size


}

SOV_set_up_left_opposition_militia = {
	division_template = {
	    name = "Trotskyist Workers' Militia"
	    is_locked = yes
	    division_names_group = SOV_INF_01
	    priority = 0
	    template_counter = 0
	    regiments = {
	        infantry = { x = 0 y = 0 }
	        infantry = { x = 0 y = 1 }
	        infantry = { x = 0 y = 2 }
	    }
	    is_locked = yes
	}
	division_template = {
	    name = "Trotskyist Peasant Militia"
	    is_locked = yes
	    division_names_group = SOV_INF_01
	    priority = 0
	    template_counter = 14
	    regiments = {
	        infantry = { x = 0 y = 0 }
	        infantry = { x = 0 y = 1 }
	        infantry = { x = 1 y = 0 }
	        infantry = { x = 1 y = 1 }
	    }
	    is_locked = yes
	}
	every_owned_state = {
		IF = {
			limit = {
				OR = {
					has_state_category = city
					has_state_category = large_city
					has_state_category = megalopolis
					has_state_category = metropolis
				}
			}
			create_unit = {
				division = "name = \"Workers' Miltia Brigade\" division_template = \"Trotskyist Workers' Militia\" start_experience_factor = 0.09 start_equipment_factor = 0.85" owner = ROOT
			}
		}
		ELSE_IF = {
			limit = {
				NOT = { has_state_category = enclave }
				NOT = { has_state_category = wasteland }
			}
			create_unit = {
				division = "name = \"Peasant Militia brigade\" division_template = \"Trotskyist Peasant Militia\" start_experience_factor = 0.01 start_equipment_factor = 0.55" owner = ROOT
			}
		}
	}
}

SOV_set_up_right_opposition_militia = {
	division_template = {
	    name = "Bukharinist Workers' Militia"
	    is_locked = yes
	    division_names_group = SOV_INF_01
	    priority = 0
	    template_counter = 0
	    regiments = {
	        infantry = { x = 0 y = 0 }
	        infantry = { x = 0 y = 1 }
	        infantry = { x = 0 y = 2 }
	    }
	    is_locked = yes
	}
	division_template = {
	    name = "Bukharinist Peasant Militia"
	    is_locked = yes
	    division_names_group = SOV_INF_01
	    priority = 0
	    template_counter = 14
	    regiments = {
	        infantry = { x = 0 y = 0 }
	        infantry = { x = 0 y = 1 }
	        infantry = { x = 1 y = 0 }
	        infantry = { x = 1 y = 1 }
	    }
	    is_locked = yes
	}
	every_owned_state = {
		IF = {
			limit = {
				OR = {
					has_state_category = city
					has_state_category = large_city
					has_state_category = megalopolis
					has_state_category = metropolis
				}
			}
			create_unit = {
				division = "name = \"Workers' Miltia Brigade\" division_template = \"Bukharinist Workers' Militia\" start_experience_factor = 0.09 start_equipment_factor = 0.85" owner = ROOT
			}
		}
		ELSE_IF = {
			limit = {
				NOT = { has_state_category = enclave }
				NOT = { has_state_category = wasteland }
			}
			create_unit = {
				division = "name = \"Peasant Militia brigade\" division_template = \"Bukharinist Peasant Militia\" start_experience_factor = 0.01 start_equipment_factor = 0.55" owner = ROOT
			}
		}
		# More Peasant Militia if collectivization has been reversed
		IF = {
			limit = {
				ROOT = { has_completed_focus = SOV_reverse_the_collectivization_process }
				NOT = { has_state_category = enclave }
				NOT = { has_state_category = wasteland }
			}
			create_unit = {
				division = "name = \"Peasant Militia brigade\" division_template = \"Bukharinist Peasant Militia\" start_experience_factor = 0.01 start_equipment_factor = 0.55" owner = ROOT
			}
		}
	}
}

SOV_set_up_extras_from_concessions = {
	# Extra Troops from Japan?
	IF = {
		limit = {
			has_country_flag = SOV_CSW_transfer_vladivostok_flag
			any_country= {
				original_tag = SOV
				owns_state = 408
			}
		}
		SOV_set_up_japanese_troops = yes
	}
	# Equipment from Japan in return for North Sakhalin
	IF = {
		limit = {
			has_country_flag = SOV_CSW_transfer_sakhalin_flag
			any_country= {
				original_tag = SOV
				owns_state = 655
			}
		}
		IF = {
			limit = { check_variable = { SOV.SOV_japanese_rifles > 0 } }
			add_equipment_to_stockpile = {
			    type = infantry_equipment
				amount = SOV.SOV_japanese_rifles
				producer = JAP
			}
		}
		IF = {
			limit = { check_variable = { SOV.SOV_japanese_guns > 0 } }
			add_equipment_to_stockpile = {
			    type = artillery_equipment
				amount = SOV.SOV_japanese_guns
				producer = JAP
			}
		}
		IF = {
			limit = { check_variable = { SOV.SOV_japanese_fighters > 0 } }
			add_equipment_to_stockpile = {
			    type = small_plane_airframe
				amount = SOV.SOV_japanese_fighters
				producer = JAP
			}
		}
	}

	# Advisors from Germany
	IF = {
		limit = {
			has_country_flag = SOV_CSW_german_advisors_flag
		}
		GER = {
			country_event = {
				id = NSB_soviet_civil_war_common.109
				hours = 1
			}
		}
	}

	# Equipment from Germany
	IF = { 
		limit = { 
			has_country_flag = SOV_CSW_baku_oil_flag 
		}
		IF = {
			limit = {
				NOT = {
					has_dlc = "No Step Back"
				}
			}
			add_equipment_to_stockpile = { type = light_tank_equipment_2 amount = 100 producer = GER }
		}
		ELSE = { 
			add_equipment_to_stockpile = { type = light_tank_chassis_2 amount = 100 producer = GER variant_name = "Panzer II Ausf. a" } 
		}
		add_equipment_to_stockpile = { type = infantry_equipment_0 amount = 200 producer = GER }
	}




	# German Ukrainian Insurrection
	IF = {
		limit = {
			has_country_flag = SOV_CSW_ukraine_germany_ready_flag
		}
		country_event = {
			id = NSB_soviet_civil_war_common.114
			hours = 1
		}
	}

	# Adding weapons for trotsky
	for_each_loop = {
		array = SOV.many_weapons_array
		value = weapons_country
		add_equipment_to_stockpile = {
			type = infantry_equipment
			amount = 200
			producer = var:weapons_country
		}
	}
	for_each_loop = {
		array = SOV.some_weapons_array
		value = weapons_country
		add_equipment_to_stockpile = {
			type = infantry_equipment
			amount = 50
			producer = var:weapons_country
		}
	}
}

#######################
### COUNTRY LEADERS ###
#######################

SOV_promote_to_country_leader_beriya = {
	if = {
		limit = {
			has_dlc = "La Resistance"
		}
		add_country_leader_role = {
			character = "SOV_lavrenty_beriya"
			country_leader = {
				ideology = stalinism
				traits = { unscrupulous_orchestrator }
				expire = "1953.12.23.1"
			}
			promote_leader = yes
		}
	}
	else = {
		add_country_leader_role = {
			character = "SOV_lavrenty_beriya"
			country_leader = {
				ideology = stalinism
				traits = { unscrupulous_orchestrator_no_lar }
				expire = "1953.12.23.1"
			}
			promote_leader = yes
		}
	}
	SOV = { set_country_flag = SOV_beriya_country_leader_flag }
}

SOV_promote_to_country_leader_trotsky = {
	add_country_leader_role = {
		character = "SOV_lev_trotsky"
		country_leader = {
			ideology = marxism
			traits = { permanent_revolutionary }
			expire = "1965.1.1.1"
		}
		promote_leader = yes
	}
	SOV = { set_country_flag = SOV_trotsky_country_leader_flag }
}

SOV_promote_to_country_leader_smirnov = {
	add_country_leader_role = {
		character = "SOV_ivan_smirnov"
		country_leader = {
			ideology = marxism
			traits = { the_siberian_lenin }
			expire = "1965.1.1.1"
		}
		promote_leader = yes
	}
	SOV = { set_country_flag = SOV_smirnov_country_leader_flag }
}

SOV_promote_to_country_leader_bukharin = {
	add_country_leader_role = {
		character = "SOV_nikolay_bukharin"
		country_leader = {
			ideology = marxism
			traits = { defender_of_the_peasantry }
			expire = "1965.1.1.1"
		}
		promote_leader = yes
	}
	SOV = { set_country_flag = SOV_bukharin_country_leader_flag }
}

SOV_promote_to_country_leader_rykov = {
	add_country_leader_role = {
		character = "SOV_aleksey_rykov"
		country_leader = {
			ideology = marxism
			traits = { labor_defense_organizer }
			expire = "1965.1.1.1"
		}
		promote_leader = yes
	}
	SOV = { set_country_flag = SOV_rykov_country_leader_flag }
}

SOV_promote_to_country_leader_zinovyev = {
	add_country_leader_role = {
		character = "SOV_grigory_zinovyev"
		country_leader = {
			ideology = marxism
			traits = { born_agitator }
			expire = "1965.1.1.1"
		}
		promote_leader = yes
	}
	SOV = { set_country_flag = SOV_zinovyev_country_leader_flag }
}

SOV_promote_to_country_leader_kamenev = {
	add_country_leader_role = {
		character = "SOV_lev_kamenev"
		country_leader = {
			ideology = marxism
			traits = { the_faded_star }
			expire = "1965.1.1.1"
		}
		promote_leader = yes
	}
	SOV = { set_country_flag = SOV_kamenev_country_leader_flag }
}

SOV_promote_to_country_leader_provisional = {
	SOV_provisional_government = {
		remove_trait = {
			ideology = despotism
			trait = emigre_connections
		}
		remove_trait = {
			ideology = despotism
			trait = desperate_gambit
		}
		remove_trait = {
			ideology = despotism
			trait = dysfunctional_assembly
		}
		add_trait = {
			ideology = despotism
			trait = internal_rivalry
		}
		promote_character = yes
	}
}
# japanese_expeditionary_leader_flag
SOV_set_up_japanese_troops = {
	division_template = {
	    name = "Japanese Expeditionary Force"
	    division_names_group = SOV_JAP_INF
	    is_locked = yes
	    priority = 1
	    template_counter = 3
	    regiments = {
	        infantry = { x = 0 y = 0 }
	        infantry = { x = 0 y = 1 }
	        infantry = { x = 0 y = 2 }
	        infantry = { x = 1 y = 0 }
	        infantry = { x = 1 y = 1 }
	        infantry = { x = 1 y = 2 }
	        artillery_brigade = { x= 2 y = 0}
	        artillery_brigade = { x= 2 y = 1}
	    }
	    is_locked = yes
	}
	JAP = {
		var:SOV.SOV_japanese_general = {
			unit_leader_event = {
				id = NSB_soviet_civil_war_common.129
				hours = 1
			}
		}
	}

	var:SOV_civil_war_start_state = {
		create_unit = {
			division = "division_template = \"Japanese Expeditionary Force\" start_experience_factor = 0.2 start_equipment_factor = 0.95" owner = ROOT count = 24
		}
		

	}

	# Add air support 

	add_equipment_to_stockpile = { type = small_plane_airframe  amount = 100 producer = JAP }
	add_equipment_to_stockpile = { type = medium_plane_airframe  amount = 50 producer = JAP }
	
	
}

# Start creating HQ for Civil war
SOV_start_setting_up_hq_state = {
	set_country_flag = { flag = SOV_align_state_in_process days = 5 value = 1 }
	FROM = { set_state_flag = SOV_hq_state_selected }
	set_country_flag = SOV_hq_state_selected_country_flag
}

# Define state as controlled for Civil War
SOV_set_civil_war_state = {
	custom_effect_tooltip = SOV_set_civil_war_state_tt

	IF = {
		limit = { SOV_is_left_opposition = yes }
		FROM = { set_state_flag = SOV_left_opposition_state }
		FROM = { clr_state_flag = SOV_is_aligning_left }
	}
	ELSE_IF = {
		limit = { SOV_is_right_opposition = yes }
		FROM = { set_state_flag = SOV_right_opposition_state }
		FROM = { clr_state_flag = SOV_is_aligning_right }
	}
	ELSE_IF = {
		limit = { SOV_is_exiles = yes }
		FROM = { set_state_flag = SOV_exiles_state }
		FROM = { clr_state_flag = SOV_is_aligning_exiles }
	}
}

# Start aligning state for Civil War
SOV_start_infiltrating_state = {
	custom_effect_tooltip = SOV_start_infiltrating_state_tt
	set_country_flag = { flag = SOV_align_state_in_process days = 14 value = 1 }
	IF = {
		limit = { SOV_is_left_opposition = yes }
		FROM = { set_state_flag = SOV_is_aligning_left }
	}
	ELSE_IF = {
		limit = { SOV_is_right_opposition = yes }
		FROM = { set_state_flag = SOV_is_aligning_right }
	}
	ELSE_IF = {
		limit = { SOV_is_exiles = yes }
		FROM = { set_state_flag = SOV_is_aligning_exiles }
	}
}

SOV_initiate_civil_war = {
	

		# Set War Flag
		set_global_flag = SOV_soviet_civil_war
		IF = {
			limit = {
				SOV_is_exiles = yes 
			}
			MAN = { give_military_access = SOP }
		}


		# Remove Paranoia
		SOV_remove_paranoia_effect = yes

		# Get Ideology Support
		set_variable = { SOV_communist_support_before_civil_war = party_popularity_100@communism }
		set_variable = { SOV_democratic_support_before_civil_war = party_popularity_100@democratic }
		set_variable = { SOV_fascist_support_before_civil_war = party_popularity_100@fascism }
		set_variable = { SOV_neutrality_support_before_civil_war = party_popularity_100@neutrality }

		# Disband the NKVD -  they will come back, no worries
		delete_units = {
			division_template = "NKVD Pogranichnaya Diviziya"
			disband = no
		}
}

SOV_set_up_civil_war_capital = {
	IF = {
		limit = {
			check_variable = { ROOT.SOV_civil_war_start_state = 0 }
		}
		# If we don't have a base: 
		random_owned_state = {
			limit = { 
				NOT = { state = 219 } 
				NOT = { region = 132 }
				NOT = { region = 133 }
			}
			set_variable = { ROOT.SOV_civil_war_start_state = THIS }
			IF = {
				limit = { SOV_is_left_opposition = yes }
				THIS = { set_state_flag = SOV_left_opposition_state }
			}
			ELSE_IF = {
				limit = { SOV_is_right_opposition = yes }
				THIS = { set_state_flag = SOV_right_opposition_state }
			}
			ELSE_IF = {
				limit = { SOV_is_exiles = yes }
				THIS = { set_state_flag = SOV_exiles_state }
			}
		}
	}
	set_capital = {
		state = var:ROOT.SOV_civil_war_start_state
		remember_old_capital = no
	}
	IF = {
		limit = {
			check_variable = { ROOT.SOV_stalinist_civil_war_start_state = 0 }
		}
		set_variable = { ROOT.SOV_stalinist_civil_war_start_state = 219 }
	}
	# Increase importance of Chita if capital, and add hub (for supply and surrender)
	IF = {
		limit = {
			var:ROOT.SOV_civil_war_start_state = { state = 563 }
		}
		add_victory_points = {
			province = 4705
			value = 4
		}

		563 = {
			add_building_construction = {
				type = supply_node 
				level = 1
				instant_build = yes
				province = 4705
			}
		}
	}
}

SOV_set_up_civil_war_army_fractions = {
	# Verify armed forces fractions
	IF = {
		limit = { SOV_is_right_opposition = yes }
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_right_opposition_army_support }
			}
			set_variable = { ROOT.SOV_right_opposition_army_support = 0.2 }
		}
		IF = { 
			limit = { 
				SOV = { is_ai = yes } 
				check_variable = { ROOT.SOV_right_opposition_army_support < 0.55 }
			}
			add_to_variable = { ROOT.SOV_right_opposition_army_support = 0.25 }
		}
		clamp_variable = {
			var = ROOT.SOV_right_opposition_army_support
			min = 0
			max = 0.9
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_right_opposition_navy_support }
			}
			set_variable = { ROOT.SOV_left_opposition_navy_support = 0.1 }
		}
		clamp_variable = {
			var = ROOT.SOV_right_opposition_navy_support
			min = 0
			max = 0.9
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_right_opposition_air_support }
			}
			set_variable = { ROOT.SOV_right_opposition_air_support = 0 }
		}
		clamp_variable = {
			var = ROOT.SOV_right_opposition_air_support
			min = 0
			max = 0.9
		}
		set_temp_variable = { temp_centre_army = 1 }
		subtract_from_temp_variable = { temp_centre_army = ROOT.SOV_right_opposition_army_support }
		set_temp_variable = { temp_centre_navy = 1 }
		subtract_from_temp_variable = { temp_centre_navy = ROOT.SOV_right_opposition_navy_support }
		set_temp_variable = { temp_centre_air = 1 }
		subtract_from_temp_variable = { temp_centre_air = ROOT.SOV_right_opposition_air_support }
	}
	ELSE_IF = {
		limit = {
			SOV_is_left_opposition = yes
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_left_opposition_army_support }
			}
			set_variable = { ROOT.SOV_left_opposition_army_support = 0.2 }
		}
		IF = { 
			limit = { 
				SOV = { is_ai = yes } 
				check_variable = { ROOT.SOV_left_opposition_army_support < 0.55 }
			}
			add_to_variable = { ROOT.SOV_left_opposition_army_support = 0.25 }
		}
		clamp_variable = {
			var = ROOT.SOV_left_opposition_army_support
			min = 0
			max = 0.9
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_left_opposition_navy_support }
			}
			set_variable = { ROOT.SOV_left_opposition_navy_support = 0.1 }
		}
		clamp_variable = {
			var = ROOT.SOV_left_opposition_navy_support
			min = 0
			max = 0.9
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_left_opposition_air_support }
			}
			set_variable = { ROOT.SOV_left_opposition_air_support = 0 }
		}
		clamp_variable = {
			var = ROOT.SOV_left_opposition_air_support
			min = 0
			max = 0.9
		}
		set_temp_variable = { temp_centre_army = 1 }
		subtract_from_temp_variable = { temp_centre_army = ROOT.SOV_left_opposition_army_support }
		set_temp_variable = { temp_centre_navy = 1 }
		subtract_from_temp_variable = { temp_centre_navy = ROOT.SOV_left_opposition_navy_support }
		set_temp_variable = { temp_centre_air = 1 }
		subtract_from_temp_variable = { temp_centre_air = ROOT.SOV_left_opposition_air_support }
	}
	ELSE_IF = {
		limit = {
			SOV_is_exiles = yes
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_exiles_army_support }
			}
			set_variable = { ROOT.SOV_exiles_army_support = 0.2 }
		}
		IF = { 
			limit = { 
				SOV = { is_ai = yes }
				check_variable = { ROOT.SOV_exiles_army_support < 0.55 }
			}
			add_to_variable = { ROOT.SOV_exiles_army_support = 0.25 }
		}
		clamp_variable = {
			var = ROOT.SOV_exiles_army_support
			min = 0
			max = 0.9
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_exiles_navy_support }
			}
			set_variable = { ROOT.SOV_exiles_navy_support = 0.1 }
		}
		clamp_variable = {
			var = ROOT.SOV_exiles_navy_support
			min = 0
			max = 0.9
		}
		IF = {
			limit = {
				NOT = { has_variable = ROOT.SOV_exiles_air_support }
			}
			set_variable = { ROOT.SOV_exiles_air_support = 0 }
		}
		IF = { 
			limit = { 
				SOV = { is_ai = yes } 
				check_variable = { ROOT.SOV_exiles_air_support < 0.55 }
			}
			add_to_variable = { ROOT.SOV_exiles_air_support = 0.25 }
		}
		clamp_variable = {
			var = ROOT.SOV_exiles_air_support
			min = 0
			max = 0.9
		}

		set_temp_variable = { temp_centre_army = 1 }
		subtract_from_temp_variable = { temp_centre_army = ROOT.SOV_exiles_army_support }
		set_temp_variable = { temp_centre_navy = 1 }
		subtract_from_temp_variable = { temp_centre_navy = ROOT.SOV_exiles_navy_support }
		set_temp_variable = { temp_centre_air = 1 }
		subtract_from_temp_variable = { temp_centre_air = ROOT.SOV_exiles_air_support }
	}

}

SOV_start_civil_war = {
	# Define the Player Country Ideology
	IF= {
		limit = { SOV_is_left_opposition = yes }
		set_temp_variable = { temp_ideology = token:communism }
	}
	ELSE_IF= {
		limit = { SOV_is_right_opposition = yes }
		set_temp_variable = { temp_ideology = token:communism }
	}
	ELSE_IF= {
		limit = { SOV_is_exiles = yes }
		set_temp_variable = { temp_ideology = token:neutrality }
	}

	# Lose border war with JAP 
	IF = {
		limit = {
			has_border_war_between = {
				attacker = 527
				defender = 408
			}
		}
		finalize_border_war = {
			attacker_win = yes
			attacker = 527
			defender = 408
		}
	}
	ELSE_IF = {
		limit = {
			has_border_war_between = {
				attacker = 408
				defender = 527
			}
		}
		finalize_border_war = {
			defender_win = yes
			attacker = 408
			defender = 527
		}
	}

	# Dismantle the comintern (if faction leader)
	IF = { 
		limit = {
			ROOT = { is_faction_leader = yes }
		}
		ROOT = { dismantle_faction = yes }
	}

	# Launch The Civil War
	start_civil_war = {
		ideology = communism
		ruling_party = var:temp_ideology
		capital = var:ROOT.SOV_stalinist_civil_war_start_state
		states = all
		states_filter = {
			IF= {
				limit = { SOV_is_left_opposition = yes }
				NOT = { has_state_flag = SOV_left_opposition_state }
				is_fully_controlled_by = ROOT
			}
			ELSE_IF= {
				limit = { SOV_is_right_opposition = yes }
				NOT = { has_state_flag = SOV_right_opposition_state }
				is_fully_controlled_by = ROOT
			}
			ELSE_IF= {
				limit = { SOV_is_exiles = yes }
				NOT = { has_state_flag = SOV_exiles_state }
				is_fully_controlled_by = ROOT
			}
		}

		size = 0.75
		army_ratio = var:temp_centre_army
		navy_ratio = var:temp_centre_navy
		air_ratio = var:temp_centre_air

		keep_political_leader = yes
		keep_unit_leaders_trigger = {
			IF = {
				limit = { SOV_is_left_opposition = yes }
				has_trait = trait_SOV_trotskyist
			}
			ELSE_IF = {
				limit = { SOV_is_right_opposition = yes }
				has_trait = trait_SOV_bukharinist
			}
			ELSE_IF = {
				limit = { SOV_is_exiles = yes }
				has_trait = trait_SOV_monarchist_sympathizer
			}
		}
	}

	# Set Up Stalinist Soviet Union ( tag = SOS )
	random_country = {
		limit = {
			original_tag = SOV
			has_war_with = ROOT
			has_government = communism
		}
		set_cosmetic_tag = stalinist_soviet_union
		set_popularities = {
			communism = SOV.SOV_communist_support_before_civil_war
			democratic = SOV.SOV_democratic_support_before_civil_war
			fascism = SOV.SOV_fascist_support_before_civil_war
			neutrality = SOV.SOV_neutrality_support_before_civil_war
		}
		# Set Stalin as Leader if alive, else set Beriya
		# recruit_character = SOV_iosif_stalin
		IF = { 
			limit = { SOV = { has_character = SOV_iosif_stalin }}
			SOV = { SOV_iosif_stalin = { set_nationality = SOS } }
			promote_character = { character = SOV_iosif_stalin  ideology = stalinism }
		}
		ELSE = {
			SOV = { SOV_lavrenty_beriya = { set_nationality = SOS } }
			promote_character = { character = SOV_lavrenty_beriya  ideology = stalinism }
		}

		# Remove ideas 
		remove_ideas = SOV_the_fourth_international_idea

		# Rally the Red Army!
		IF = {
			limit = {
				num_divisions < 5
			}
			multiply_temp_variable = { temp_centre_army = 20 }
			set_variable = { civil_war_army_size = temp_centre_army }
			SOV_set_up_red_army = yes
		}

		# Set up "extra" template (Stalin likes his basic infantry template, no messing with it)
		delete_unit_template_and_units = {
		    division_template = "Gvardia"
		    disband = yes
		}
		division_template = {
			name = "Gvardia"
			division_names_group = SOV_INF_01

			regiments = {
				infantry = { x = 0 y = 0 }
				infantry = { x = 0 y = 1 }
				infantry = { x = 0 y = 2 }
				infantry = { x = 1 y = 0 }
				infantry = { x = 1 y = 1 }
				infantry = { x = 1 y = 2 }
				infantry = { x = 2 y = 0 }
				infantry = { x = 2 y = 1 }
				infantry = { x = 2 y = 2 }
			}
			support = {
				artillery = { x = 0 y = 0 }
			}
		}
		random_owned_controlled_state = {
			limit = { is_capital = yes }
			create_unit = {
				division = "division_template = \"Gvardia\" start_experience_factor = 0.1 start_equipment_factor = 1.0" owner = PREV count = 2
			}
		}

		# The Support of the NKVD
		IF = {
			limit = {
				NOT = { any_other_country = { has_country_flag = SOV_nkvd_aligned_flag } }
			}
			SOV_set_up_nkvd = yes
		}
		ELSE_IF = {
			limit = {
				OR = {
					SOV = {
						has_idea = SOV_genrikh_yagoda
						NOT = { SOV_genrikh_yagoda = { has_character_flag = SOV_aligned_right_opposition_flag } }
					}
					SOV = {
						has_idea = SOV_nikolay_yezhov
						NOT = { SOV_nikolay_yezhov = { has_character_flag = SOV_aligned_right_opposition_flag } }
					}
					SOV = {
						NOT = {
							has_idea = SOV_nikolay_yezhov
							has_idea = SOV_genrikh_yagoda
						}
					}
				}
			}
			random_owned_controlled_state = {
				create_unit = {
					division = "name = \"1ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = SOS
				}
				create_unit = {
					division = "name = \"2ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = SOS
				}
				create_unit = {
					division = "name = \"3ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = SOS
				}
				create_unit = {
					division = "name = \"4ya NKVD Pogranichnaya Diviziya\" division_template = \"NKVD Pogranichnaya Diviziya\" start_experience_factor = 0.2 start_equipment_factor = 1.0" owner = SOS
				}
			}
		}

		# Load the Focus Tree
		load_focus_tree = { tree = soviet_focus keep_completed = yes }
		if = {
			limit = { has_completed_focus = SOV_the_left_opposition }
			uncomplete_national_focus = { # That was Trotzky's doing, not Stalin's
				focus = SOV_the_left_opposition
				uncomplete_children = yes
			}
		}
		if = {
			limit = { has_completed_focus = SOV_the_right_opposition }
			uncomplete_national_focus = { # Remove all traces of Bukharin's
				focus = SOV_the_right_opposition
				uncomplete_children = yes
			}
		}
		if = {
			limit = { has_completed_focus = SOV_beaten_but_not_defeated }
			uncomplete_national_focus = { # Remove all traces of exiles'
				focus = SOV_beaten_but_not_defeated
				uncomplete_children = yes
			}
		}

		complete_national_focus = SOV_the_path_of_marxism_leninism
		complete_national_focus = SOV_the_centre
		IF = {
			limit = { SOV = { has_country_flag = SOV_the_stalin_constitution_completed_flag } }
			complete_national_focus = SOV_the_stalin_constitution
		}

		IF = {
			limit = { SOV = { has_country_flag = SOV_the_zinovyevite_terrorist_center_completed_flag } }
			unlock_national_focus = SOV_the_zinovyevite_terrorist_center # effect already played out
		}
		IF = {
			limit = { SOV = { has_country_flag = SOV_the_anti_soviet_trotskyist_center_completed_flag } }
			unlock_national_focus = SOV_the_anti_soviet_trotskyist_center # effect already played out
		}
		IF = {
			limit = { SOV = { has_country_flag = SOV_the_workers_dictatorship_completed_flag }}
			complete_national_focus = SOV_the_workers_dictatorship
		}
		IF = {
			limit = { SOV = { has_country_flag = SOV_the_military_conspiracy_completed_flag } }
			unlock_national_focus = SOV_the_military_conspiracy # effect already played out
		}
		IF = {
			limit = { SOV = { has_country_flag = SOV_socialism_in_one_country_completed_flag } }
			complete_national_focus = SOV_socialism_in_one_country
		}
		IF = {
			limit = { SOV = { has_country_flag = SOV_secure_the_administration_completed_flag } }
			complete_national_focus = SOV_secure_the_administration
		}
		IF = {
			limit = { SOV = { has_country_flag = SOV_the_bloc_of_rights_and_trotskyites_completed_flag } }
			unlock_national_focus = SOV_the_bloc_of_rights_and_trotskyites # effect already played out
		}
		IF = {
			limit = { SOV = { has_country_flag = SOV_behead_the_snake_completed_flag } }
			unlock_national_focus = SOV_behead_the_snake # effect already played out
		}
		# Set the Surrender Limit Modifier
		set_variable = { SOV_surrender_modifier = SOV.SOV_opposition_popular_support }
		divide_variable = { SOV_surrender_modifier = 100 } # convert to fraction
		clamp_variable = {
			var = SOV_surrender_modifier
			min = 0
			max = 0.9
		}
		multiply_variable = { SOV_surrender_modifier = -1 } # make negative
		SOV_set_up_dynamic_modifiers_civil_war = yes # Add Modifiers and Copy variables

		# Ensure no Stalinist Leaders side with the Opposition
		SOV = {
			every_unit_leader = {
				limit = {
					has_trait = trait_SOV_stalinist
				}
				set_nationality = SOS
			}
		}

		# give germany Wargoal if needed
		IF = {
			limit = {
				GER = { 
					has_completed_focus = GER_war_with_the_ussr # has completed War with the USSR
					NOT = { has_war_with = SOV } # Not has started the war already
					has_wargoal_against = SOV # Still has the wargoal
				}
			}
			GER = {
				create_wargoal = {
					type = annex_everything
					target = SOS
					expire = 0
				}
			}
		}
	}
	### Opposition country - new SOV 
	IF = {
		limit = {
			has_template = "NKVD Pogranichnaya Diviziya"
		}
		set_division_template_lock = {
			division_template = "NKVD Pogranichnaya Diviziya"
			is_locked = no
		}
	}
	# Exiles don't get any NKVD templates unless they later desert
	IF = {
		limit = { SOV_is_exiles = yes }
		SOV = {
			delete_unit_template_and_units = {
				division_template = "NKVD Pogranichnaya Diviziya"
				disband = no
			}
		}
	}	
	# Stalinist military will no longer be a thing in this country 
	IF = {
		limit = { has_country_flag = SOV_stalinist_military_flag }
		clr_country_flag = SOV_stalinist_military_flag
	}
	# Return non-converted states
	IF = {
		limit = { SOV_is_left_opposition = yes }
		every_controlled_state = {
			limit = { #This is a bit silly, but necessary.
				NOT = { has_state_flag = SOV_left_opposition_state }
				is_fully_controlled_by = ROOT
				is_owned_by = ROOT
			}
			SOS = {
				transfer_state = PREV
			}
		}
	}
	ELSE_IF = {
		limit = { SOV_is_right_opposition = yes }
		every_controlled_state = {
			limit = { 
				NOT = { has_state_flag = SOV_right_opposition_state }
				is_fully_controlled_by = ROOT
				is_owned_by = ROOT
			}
			SOS = {
				transfer_state = PREV
			}
		}
	}
	ELSE_IF = {
		limit = { SOV_is_exiles = yes }
		every_controlled_state = {
			limit = { 
				NOT = { has_state_flag = SOV_exiles_state }
				is_fully_controlled_by = ROOT
				is_owned_by = ROOT
			}
			SOS = {
				transfer_state = PREV
			}
		}
	}

	## Return controlled states in other countries
	# Fully controlled states go to stalin 
	every_state = {
		limit = { 
			is_fully_controlled_by = ROOT 
			NOT = { is_owned_by = ROOT }
			NOT = { has_state_flag = SOV_left_opposition_state } 
			NOT = { has_state_flag = SOV_right_opposition_state }
			NOT = { has_state_flag = SOV_exiles_state }
		}
		set_state_controller_to = SOS
	}
	# controlled, but not fully go back to the owner, if they have anything bordering 
	every_state = {
		limit = { 
			is_controlled_by = ROOT 
			NOT = { is_owned_by = ROOT }
			NOT = { has_state_flag = SOV_left_opposition_state } 
			NOT = { has_state_flag = SOV_right_opposition_state }
			NOT = { has_state_flag = SOV_exiles_state }
			any_neighbor_state = {
				is_controlled_by = PREV.OWNER
			}
		}
		set_state_controller_to = THIS.OWNER
	}
	# rest will have to sort itself out



	# Bring exiled characters back:
	IF = {
		limit = { SOV_is_left_opposition = yes }
		every_character = {
			limit = {
				has_character_flag = SOV_exiled_flag
				has_character_flag = SOV_aligned_left_opposition_flag
			}
			clr_character_flag = SOV_exiled_flag
		}
	}
	ELSE_IF = {
		limit = { SOV_is_right_opposition = yes }
		every_character = {
			limit = {
				has_character_flag = SOV_exiled_flag
				has_character_flag = SOV_aligned_right_opposition_flag
			}
			clr_character_flag = SOV_exiled_flag
		}
	}
	ELSE_IF = { # Political Advisors handled individually in post-CW focuses
		limit = { SOV_is_exiles = yes }
		every_character = {
			limit = {
				has_character_flag = SOV_exiled_flag
				OR = {
					has_character_flag = SOV_exiled_army_leader
					has_character_flag = SOV_exiled_military_advisor_flag
				}
			}
			clr_character_flag = SOV_exiled_flag
		}
	}

	# Ensure no Communist Commanders end up with exiles
	IF = {
		limit = { SOV_is_exiles = yes }
		SOV = {
			every_unit_leader = {
				limit = {
					NOT = { has_trait = trait_SOV_monarchist_sympathizer}
					NOT = { has_character_flag = SOV_exiled_army_leader }
				}
				set_nationality = SOS
			}
		}
	}

	IF = {
		limit = { check_variable = { SOV_opposition_popular_support > 1 } }
		country_event = {
			id = NSB_soviet_civil_war_common.006
			days = 2
			random_days = 2
		}
	}

	# Remove home of the revolution idea
	SOV = { remove_ideas = home_of_revolution }

	# set non recognized country = no factions allowed 
	SOV = { set_rule = { can_join_factions = no }}
	
	# add Supply hub
	IF = {
		limit = { 
			has_country_flag = SOV_supply_hub_flag
		}
		every_owned_state = {
			limit = {
				supply_node < 1
				OR = {
					state = var:ROOT.SOV_civil_war_start_state
					any_neighbor_state = { state = var:ROOT.SOV_civil_war_start_state }
				}
			}
			add_building_construction = {
				type = supply_node
				province = {
					all_provinces = yes
					limit_to_victory_point = yes
				}
				level = 1
				instant_build = yes
			}
		}
		every_owned_state = {
			limit = {
				is_controlled_by = ROOT
				any_neighbor_state = { state = var:ROOT.SOV_civil_war_start_state }
				NOT = { state = var:ROOT.SOV_civil_war_start_state }
			}
			set_variable = { ROOT.temp_target_state = THIS.id }
			ROOT = {
				build_railway = {
					level = 1 # Defaults to 1
					start_state = var:ROOT.SOV_civil_war_start_state
					target_state = var:ROOT.temp_target_state
				}
			}
			build_railway = {
				level = 2
				start_province = 4705
				target_province = 7860
			}
		}
		
		clear_variable = ROOT.temp_target_state
	}

	# Anti Comintern Pact 
	IF = {
		limit = {
			GER = { has_completed_focus = GER_anti_comintern_pact }
		}
		# Add effects to SOS
		IF = {
			limit = {
				GER = { has_completed_focus = GER_anti_soviet_pact }
			}
			SOS = { add_opinion_modifier = { target = GER modifier = worse_pact_against_us } }
		}
		ELSE = {
			SOS = { add_opinion_modifier = { target = GER modifier = pact_against_us } }
		}
		every_country = {
			limit = {
				has_country_flag = anti_comintern_member
				NOT = { has_idea = anti_soviet_pact }
			}
			SOS = { add_opinion_modifier = { target = PREV modifier = pact_against_us } }
		}
		every_country = {
			limit = { has_idea = anti_soviet_pact }
			SOS = { add_opinion_modifier = { target = PREV modifier = worse_pact_against_us } }
		}
		# Clear effect for non-communist Russias
		IF = { 
			limit = { NOT = { has_government = communism } }
			every_country = {
				limit = {
					OR = {
						tag = GER
						has_idea = anti_soviet_pact
						has_country_flag = anti_comintern_member
					}
				}
				ROOT = { remove_opinion_modifier = { target = PREV modifier = pact_against_us } }
				ROOT = { remove_opinion_modifier = { target = PREV modifier = worse_pact_against_us  } }
			}
		}
	}

	# Let the world know
	news_event = { id = nsb_news.9 hours = 8 }
}

SOV_left_opposition_civil_war_start = {
	# Rename the USSR
	set_cosmetic_tag = SOV_left_opposition # tag = SOT

	# Set party Popularity
	set_popularities = {
		communism = SOV_communist_support_before_civil_war
		democratic = SOV_democratic_support_before_civil_war
		fascism = SOV_fascist_support_before_civil_war
		neutrality = SOV_neutrality_support_before_civil_war
	}

	# Modify Stability
	IF = {
		limit = {
			has_completed_focus = SOV_gain_support_from_party_members
		}
		add_stability = 0.1
	}

	### Set up special units
	# The Support of the NKVD
	IF = {
		limit = {
			has_country_flag = SOV_nkvd_aligned_flag
		}
		SOV_set_up_nkvd = yes
	}
	# Extras from Concessions Kick in if not already
	SOV_set_up_extras_from_concessions = yes

	# Spawn Left Opposition Militia Units
	SOV_set_up_left_opposition_militia = yes

	# Initiate Wreckers
	SOV_launch_wreckers = yes

	# Check Foreign Funds
	IF = {
		limit = { has_completed_focus = SOV_funds_for_the_cause }
		add_timed_idea = {
			idea = SOV_foreign_funds_idea
			days = 365
		}
	}

}

SOV_right_opposition_civil_war_start = {
	# Rename the USSR
	set_cosmetic_tag = SOV_right_opposition # tag = SOB

	# Set party Popularity
	set_popularities = {
		communism = SOV_communist_support_before_civil_war
		democratic = SOV_democratic_support_before_civil_war
		fascism = SOV_fascist_support_before_civil_war
		neutrality = SOV_neutrality_support_before_civil_war
	}

	# Modify Stability
	IF = {
		limit = {
			has_completed_focus = SOV_gain_support_from_party_members
		}
		add_stability = 0.1
	}

	# Reverse the collectivisation process
	IF = {
		limit = { has_completed_focus = SOV_reverse_the_collectivization_process }
		add_timed_idea = {
			idea = SOV_support_from_farmers
			days = 365
		}
	}

	### Set up special units
	# The Support of the NKVD
	IF = {
		limit = {
			has_country_flag = SOV_nkvd_aligned_flag
		}
		SOV_set_up_nkvd = yes
	}


	# Spawn Right Opposition Militia Units
	SOV_set_up_right_opposition_militia = yes

	# Extras from Concessions Kick in if not already
	SOV_set_up_extras_from_concessions = yes

	# Initiate Wreckers
	SOV_launch_wreckers = yes

	# POUM brigades come to help (if has supported POUM and POUM won the SCW)
	if = {
		limit = {
			has_country_flag = SOV_poum_brigades_ready_to_help_flag
		}
		SOV_poum_brigades_to_soviet_civil_war_effect = yes
	}
}

SOV_exiles_civil_war_start = {
	set_popularities = {
		communism = SOV_communist_support_before_civil_war
		democratic = SOV_democratic_support_before_civil_war
		fascism = SOV_fascist_support_before_civil_war
		neutrality = SOV_neutrality_support_before_civil_war
	}

	# Modify Stability
	add_stability = -0.15
	set_cosmetic_tag = SOV_PROVISIONAL
	IF = {
		limit = {
			democratic > 0.25
		}
		add_popularity = {
			ideology = democratic
			popularity = 0.15
		}
	}

	### Set up special units
	# From The Eyes Fear Fokus

	IF = {
		limit = {
			has_country_flag = Sov_the_eyes_fear_flag
		}
		country_event = {
			id = NSB_Soviet_fascist_civil_war.002
			hours = 24
			random_hours = 24
		}
	}

	hidden_effect = {
		set_technology = { blackshirt_assault_battalion_tech = 1 }
	}

	# From Fascist Youth Focus
	if = {
		limit = {
			has_country_flag = SOV_fascist_youth_union_flag
		}
		division_template = {
			name = "Rebyata"			#
			division_names_group = SOV_MEC_01
			regiments = {
				motorized = { x = 0 y = 0 }
				motorized = { x = 0 y = 1 }
				motorized = { x = 0 y = 2 }
				infantry = { x = 1 y = 0 }
				infantry = { x = 1 y = 1 }
				infantry = { x = 1 y = 2 }
				infantry = { x = 2 y = 0 }
				infantry = { x = 2 y = 1 }
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"2 Rebyata\"division_template = \"Rebyata\" start_experience_factor = 0.2"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"1 Rebyata\"division_template = \"Rebyata\" start_experience_factor = 0.2"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"3 Rebyata\"division_template = \"Rebyata\" start_experience_factor = 0.2"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"4 Rebyata\"division_template = \"Rebyata\" start_experience_factor = 0.2"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"5 Rebyata\"division_template = \"Rebyata\" start_experience_factor = 0.2"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
	}

	# Stariki
	IF = {
		limit = {
			has_country_flag = SOV_muster_the_old_guard_flag
		}
		division_template = {
			name = "Stariki"			#
			division_names_group = SOV_CAV_01

			regiments = {
				cavalry = { x = 0 y = 0 }
				cavalry = { x = 0 y = 1 }
				cavalry = { x = 1 y = 0 }
				infantry = { x = 1 y = 1 }
				infantry = { x = 1 y = 2 }
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"3 Stariki\"division_template = \"Stariki\" start_experience_factor = 0.8"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"2 Stariki\"division_template = \"Stariki\" start_experience_factor = 0.8"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
		random_owned_controlled_state = {
			create_unit = {
				division = "name = \"1 Stariki\"division_template = \"Stariki\" start_experience_factor = 0.8"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
		}
	}

	# Cavalry
	IF = {
		limit = {
			has_completed_focus = SOV_approach_semyonov
		}
		MAN = {
			country_event = {
				id = NSB_Soviet_fascist_civil_war.004 # transfer Semyonov from MAN to SOV
				days = 0
			}
		}
	}
	IF = {
		limit = {
			any_state = { has_state_flag = SOV_exiles_cavalry_flag }
		}
		division_template = {
			name = "Kavalerijskie Raskolniki"
			division_names_group = SOV_CAV_01

			regiments = {
				cavalry = { x = 0 y = 0 }
				cavalry = { x = 0 y = 1 }
				cavalry = { x = 1 y = 0 }
			}
		}
		every_state = {
			limit = { has_state_flag = SOV_exiles_cavalry_flag }
			create_unit = {
				division = "name = \"Kavalerijskie Raskolniki\"division_template = \"Kavalerijskie Raskolniki\" 	start_experience_factor = 0.02"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}



		}
		IF = {
			limit = { check_variable = { SOV_exiles_manchurian_cavalry > 0 } }
			division_template = {
				name = "Manchurian Cavalry"
				priority = 1
			    template_counter = 21
				regiments = {
					cavalry = { x = 0 y = 0 }
					cavalry = { x = 0 y = 1 }
					cavalry = { x = 1 y = 0 }
					cavalry = { x = 1 y = 1 }
				}
				is_locked = no
			}
			var:SOV_civil_war_start_state = {
				create_unit = {
					division = "name = \"Manchurian Cavalry Brigade\"division_template = 	\"Manchurian Cavalry\" 	start_experience_factor = 0.25"
					owner = SOV
					allow_spawning_on_enemy_provs = yes
					count = var:SOV.SOV_exiles_manchurian_cavalry
				}
			}
		}
	}

	# National Unification
	if = {
		limit = { has_country_flag = SOV_national_unification_flag }

		random_owned_controlled_state = {
			limit = { is_capital = yes }
			add_extra_state_shared_building_slots = 1
			add_building_construction = {
				type = arms_factory
				level = 1
				instant_build = yes
			}
			set_state_flag = SOV_national_unificaton_factory_bonus_flag
		}
		random_owned_controlled_state = {
			limit = { is_capital = yes }
			add_extra_state_shared_building_slots = 1
			add_building_construction = {
				type = arms_factory
				level = 1
				instant_build = yes
			}
		}
	}
	# The Women's Fascist Movement
	if = {
		limit = { has_country_flag = SOV_fascist_womens_movement_flag }
		add_ideas = SOV_fascist_womens_movement_idea
	}

	# The True Tsars
	if = {
		limit = { has_country_flag = SOV_the_true_tsars_flag }
		add_ideas = SOV_the_true_tsars_idea
	}

	if = {
		limit = { has_country_flag = SOV_dismissed_the_counter_revolution_flag }

		add_ideas = SOV_uncertain_counter_revolution_idea
	}

	# Tolkachi
	IF = {
		limit = { has_country_flag = SOV_tolkachi_flag }
		add_equipment_to_stockpile = {
			type = infantry_equipment
			amount = 2000
			producer = SOV
		}
		add_equipment_to_stockpile = {
			type = support_equipment
			amount = 500
			producer = SOV
		}
		add_equipment_to_stockpile = {
			type = artillery_equipment
			amount = 500
			producer = SOV
		}

	}

	# Extras from Concessions Kick in if not already
	SOV_set_up_extras_from_concessions = yes

	# Initiate Wreckers
	SOV_launch_wreckers = yes

	IF = {
		limit = {
			NOT = {
				OR = {
					has_global_flag = SOV_flug_and_shteifon_went_to_germany_flag
					has_global_flag = SOV_flug_and_shteifon_went_to_russia_flag
					has_global_flag = SOV_flug_and_shteifon_decided_to_fight_for_yugoslavia_flag
				}
			}
		}

		set_country_flag = SOV_flug_and_shteifon_are_ready_to_serve_flag
		set_global_flag = SOV_flug_and_shteifon_went_to_russia_flag

		YUG = {
			SOV_vasily_flug = {
				set_nationality = SOV
			}
			SOV_boris_shteifon = {
				set_nationality = SOV
			}
		}

	}

	# Fyodor Konovalov
	IF = {
		limit = {
			ETH = {
				OR = {
					exists = no
					has_capitulated = yes
					has_country_flag = ETH_selassie_left_flag
				}
			}
		}

		set_country_flag = ETH_feodor_konovalov_went_to_rejoin_the_russian_whites_flag

		ETH = {
			ETH_feodor_konovalov = {
				set_nationality = SOV
			}
		}

	}

	# Vladimir Sidorin
	IF = {
		limit = {
			DON = {
				exists = no
			}
		}

		set_country_flag = DON_vladimir_sidorin_went_to_rejoin_the_russian_whites_flag

		DON = {
			DON_vladimir_sidorin = {
				demote_leader = yes
				remove_country_leader_role = {
				    ideology = fascism_ideology
				}
				remove_country_leader_role = {
				    ideology = despotism
				}
				set_nationality = SOV
			}
		}

	}

	# Ivanis Vasily Nikolaevich
	IF = {
		limit = {
			KUB = {
				exists = no
			}
		}

		set_country_flag = KUB_ivanis_vasily_nikolaevich_went_to_rejoin_the_russian_whites_flag

		KUB = {
			KUB_ivanis_vasily_nikolaevich = {
				demote_leader = yes
				remove_country_leader_role = {
				    ideology = fascism_ideology
				}
				remove_country_leader_role = {
				    ideology = despotism
				}
				set_nationality = SOV
			}
		}

	}
	
}


SOV_launch_wreckers = {
	IF = {
		limit = {
			has_country_flag = SOV_organize_wreckers_flag
		}
		SOS = {
			add_timed_idea = {
				idea = SOV_organized_wreckers_idea
				days = 365
			}
		}
	}
}

SOV_the_eyes_fear_units = {
	# Don't try to cheat ;)
	hidden_effect = {
		delete_unit_template_and_units = {
			division_template = "Ruki"
			disband = yes
		}
	}
	division_template = {
		name = "Ruki"			#
		division_names_group = SOV_INF_03
		regiments = {
			infantry = { x = 0 y = 0 }
			infantry = { x = 0 y = 1 }
			infantry = { x = 0 y = 2 }
			infantry = { x = 1 y = 0 }
			infantry = { x = 1 y = 1 }
			infantry = { x = 1 y = 2 }
		}
	}
	set_temp_variable = { temp_units_siberia_index = 0 }
	# Add units in Siberia
	while_loop_effect = {
		limit = { check_variable = { temp_units_siberia_index < 8 }}
		random_state = {
			limit = {
				OR = {
					is_owned_by = SOS
					is_owned_by = SOV
				}
				OR  = {
					region = 149 # Eastern Siberia
					region = 256
					region = 257
					region = 258
					region = 259
					region = 260
					region = 151 # Western Siberia
					region = 261 
					region = 262 
				}
				NOT = { has_state_flag = SOV_ruki_flag } # Only one per state
			}
			create_unit = {
				division = "division_template = \"Ruki\" start_experience_factor = 0.2"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
			add_to_temp_variable = { temp_units_siberia_index = 1}
			set_state_flag = SOV_ruki_flag
		}
	}

	# Add units in the Ukraine
	set_temp_variable = { temp_units_ukraine_index = 0 }
	while_loop_effect = {
		limit = { check_variable = { temp_units_ukraine_index < 4 } }
		random_state = {
			limit = {
				OR = {
					is_owned_by = SOS
					is_owned_by = SOV
				}
				OR  = {
					region = 130 # Ukraine
					region = 135 # Kuban
				}
				NOT = { has_state_flag = SOV_ruki_flag }  # Only one per state
			}
			create_unit = {
				division = "division_template = \"Ruki\" start_experience_factor = 0.2"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
			add_to_temp_variable = { temp_units_ukraine_index = 1 }
			set_state_flag = SOV_ruki_flag
		}
	}


}

SOV_popular_uprising_units = {
	# Don't try to cheat ;)
	hidden_effect = {
		delete_unit_template_and_units = {
			division_template = "Opposition Partisan Brigade"
			disband = yes
		}
	}
	division_template = {
		name = "Opposition Partisan Brigade"			#
		# division_names_group = SOV_INF_04
		priority = 0
	    template_counter = 5
		regiments = {
			infantry = { x = 0 y = 0 }
			infantry = { x = 0 y = 1 }
			infantry = { x = 0 y = 2 }
		}
		is_locked = yes
	}
	set_temp_variable = { temp_partisan_units_index = 0 }
	set_temp_variable = { temp_partisan_units_number = SOV_opposition_popular_support }
	divide_temp_variable = { temp_partisan_units_number = 5 }
	add_to_temp_variable = { temp_partisan_units_number = 1 }
	# Add units in Prepped states
	# LOG = "Support: [?SOV_opposition_popular_support]"
	every_state = {
		limit = {
			OR = {
				is_owned_by = SOV
				is_owned_by = SOS
			}
			has_state_flag = SOV_opposition_partisans_flag
		}
		create_unit = {
			division = "name = \"Partisans\" division_template = \"Opposition Partisan Brigade\" start_experience_factor = 0.02"
			owner = SOV
			allow_spawning_on_enemy_provs = yes
			count = 1
		}
	}

	while_loop_effect = {
		limit = { check_variable = { temp_partisan_units_index < temp_partisan_units_number }}
		random_state = {
			limit = {
				is_owned_by = SOS
				NOT = { region = 132 } # Leningrad
				NOT  = { region = 133 } # Central Russia
				NOT  = { region = 131 } # Western Russia
				NOT = { has_state_flag = SOV_opposition_partisans_flag } # Only one per state
			}
			create_unit = {
				division = "name = \"Partisans\" division_template = \"Opposition Partisan Brigade\" start_experience_factor = 0.02"
				owner = SOV
				allow_spawning_on_enemy_provs = yes
				count = 1
			}
			add_to_temp_variable = { temp_partisan_units_index = 1 }
			set_state_flag = SOV_opposition_partisans_flag
		}
	}
}


SOV_opposition_support_very_low_increase_effect = {
	custom_effect_tooltip = SOV_opposition_support_very_low_increase_effect_tt
	add_to_variable = { SOV_opposition_popular_support = 1 }
	clamp_variable = {
		var = SOV_opposition_popular_support
		max = 60
		min = 0
	}
}
SOV_opposition_support_low_increase_effect = {
	custom_effect_tooltip = SOV_opposition_support_low_increase_effect_tt
	add_to_variable = { SOV_opposition_popular_support = 2 }
	clamp_variable = {
		var = SOV_opposition_popular_support
		max = 60
		min = 0
	}
}
SOV_opposition_support_medium_increase_effect = {
	custom_effect_tooltip = SOV_opposition_support_medium_increase_effect_tt
	add_to_variable = { SOV_opposition_popular_support = 3 }
	clamp_variable = {
		var = SOV_opposition_popular_support
		max = 60
		min = 0
	}
}
SOV_opposition_support_high_increase_effect = {
	custom_effect_tooltip = SOV_opposition_support_high_increase_effect_tt
	add_to_variable = { SOV_opposition_popular_support = 5 }
	clamp_variable = {
		var = SOV_opposition_popular_support
		max = 60
		min = 0
	}
}
SOV_opposition_support_low_decrease_effect = {
	custom_effect_tooltip = SOV_opposition_support_low_decrease_effect_tt
	subtract_from_variable = { SOV_opposition_popular_support = 1 }
	clamp_variable = {
		var = SOV_opposition_popular_support
		max = 60
		min = 0
	}
}

SOV_launch_stalinist_counter_measure = {
	IF = {
		limit = {
			NOT = { has_global_flag = SOV_soviet_civil_war_over } # Sanity check, in order not to activate more missions once the war has started
		}
		country_event = {
			id = NSB_soviet_civil_war_common.004
			days = 45 # simulating a cooldown
			random_days = 10
		}
	}
	IF = {
		limit = { NOT = { has_country_flag = SOV_stalinist_focuses_started } } # Only first time
		set_country_flag = SOV_stalinist_focuses_started
		country_event = {
			id = NSB_soviet_civil_war_common.005
			days = 70
		}
	}
}
SOV_get_centre_military_support = {
	IF = {
		limit = { SOV_is_left_opposition = yes }
		set_variable = { SOV.SOV_centre_army_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_army_support = SOV.SOV_left_opposition_army_support }
		set_variable = { SOV.SOV_centre_navy_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_navy_support = SOV.SOV_left_opposition_navy_support }
		set_variable = { SOV.SOV_centre_air_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_air_support = SOV.SOV_left_opposition_air_support }
	}
	ELSE_IF = {
		limit = { SOV_is_right_opposition = yes }
		set_variable = { SOV.SOV_centre_army_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_army_support = SOV.SOV_right_opposition_army_support }
		set_variable = { SOV.SOV_centre_navy_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_navy_support = SOV.SOV_right_opposition_navy_support }
		set_variable = { SOV.SOV_centre_air_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_air_support = SOV.SOV_right_opposition_air_support }
	}
	ELSE_IF = {
		limit = { SOV_is_exiles = yes }
		set_variable = { SOV.SOV_centre_army_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_army_support = SOV.SOV_exiles_army_support }
		set_variable = { SOV.SOV_centre_navy_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_navy_support = SOV.SOV_exiles_navy_support }
		set_variable = { SOV.SOV_centre_air_support = 1 }
		subtract_from_variable = { SOV.SOV_centre_air_support = SOV.SOV_exiles_air_support }
	}

}

SOV_increase_opposition_army_support = {
	custom_effect_tooltip = SOV_increase_opposition_army_support_tt
	IF = {
		limit = { SOV_is_left_opposition = yes }
		add_to_variable = { SOV.SOV_left_opposition_army_support = 0.1 }
		clamp_variable = {
			var= SOV.SOV_left_opposition_army_support
			max = 0.9
			min = 0.1
		}
	}
	ELSE_IF = {
		limit = { SOV_is_right_opposition = yes }
		add_to_variable = { SOV.SOV_right_opposition_army_support = 0.1 }
		clamp_variable = {
			var= SOV.SOV_right_opposition_army_support
			max = 0.9
			min = 0.1
		}
	}
	ELSE_IF = {
		limit = { SOV_is_exiles = yes }
		add_to_variable = { SOV.SOV_exiles_army_support = 0.1 }
		clamp_variable = {
			var = SOV.SOV_exiles_army_support
			max = 0.9
			min = 0.1
		}
	}
	SOV_get_centre_military_support = yes 
}
SOV_increase_opposition_army_support_small = {
	IF = {
		limit = {
			SOV_is_opposition = yes 
		}
		custom_effect_tooltip = SOV_increase_opposition_army_support_small_tt
		hidden_effect = {
			random_list = {
				25 = {}
				50 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_army_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_army_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_right_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_army_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_exiles_army_support
							max = 0.9
							min = 0.1
						}
					}
				}
				25 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_army_support = 0.05 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_army_support = 0.05 }
						clamp_variable = {
							var = SOV.SOV_right_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_army_support = 0.05 }
						clamp_variable = {
							var = SOV.SOV_exiles_army_support
							max = 0.9
							min = 0.1
						}
					}
				}
			}
		}
	}
	SOV_get_centre_military_support = yes 
}

SOV_increase_opposition_navy_support_small = {
	IF = {
		limit = {
			SOV_is_opposition = yes 
		}
		custom_effect_tooltip = SOV_increase_opposition_navy_support_small_tt
		hidden_effect = {
			random_list = {
				25 = {}
				50 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_navy_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_navy_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_right_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_navy_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_exiles_navy_support
							max = 0.9
							min = 0.1
						}
					}
				}
				25 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_navy_support = 0.05 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_navy_support = 0.05 }
						clamp_variable = {
							var = SOV.SOV_right_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_navy_support = 0.05 }
						clamp_variable = {
							var = SOV.SOV_exiles_navy_support
							max = 0.9
							min = 0.1
						}
					}
				}
			}
		}
	}
	SOV_get_centre_military_support = yes 
}

SOV_add_armed_forces_support_state = {
	IF = {
		limit = {
			SOV_is_opposition = yes 
		}
		custom_effect_tooltip = SOV_add_armed_forces_support_state_tt
		hidden_effect = {
			random_list = {
				25 = {}
				50 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_army_support = 0.01 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_army_support = 0.01}
						clamp_variable = {
							var = SOV.SOV_right_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_army_support = 0.01 }
						clamp_variable = {
							var = SOV.SOV_exiles_army_support
							max = 0.9
							min = 0.1
						}
					}
				}
				25 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_army_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_army_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_right_opposition_army_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_army_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_exiles_army_support
							max = 0.9
							min = 0.1
						}
					}
				}
			}
			random_list = {
				25 = {}
				50 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_navy_support = 0.01 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_navy_support = 0.01}
						clamp_variable = {
							var = SOV.SOV_right_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_navy_support = 0.01 }
						clamp_variable = {
							var = SOV.SOV_exiles_navy_support
							max = 0.9
							min = 0.1
						}
					}
				}
				25 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_navy_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_navy_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_right_opposition_navy_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_navy_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_exiles_navy_support
							max = 0.9
							min = 0.1
						}
					}
				}
			}
			random_list = {
				25 = {}
				50 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_air_support = 0.01 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_air_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_air_support = 0.01}
						clamp_variable = {
							var = SOV.SOV_right_opposition_air_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_air_support = 0.01 }
						clamp_variable = {
							var = SOV.SOV_exiles_air_support
							max = 0.9
							min = 0.1
						}
					}
				}
				25 = {
					IF = {
						limit = { SOV_is_left_opposition = yes }
						add_to_variable = { SOV.SOV_left_opposition_air_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_left_opposition_air_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_right_opposition = yes }
						add_to_variable = { SOV.SOV_right_opposition_air_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_right_opposition_air_support
							max = 0.9
							min = 0.1
						}
					}
					ELSE_IF = {
						limit = { SOV_is_exiles = yes }
						add_to_variable = { SOV.SOV_exiles_air_support = 0.02 }
						clamp_variable = {
							var = SOV.SOV_exiles_air_support
							max = 0.9
							min = 0.1
						}
					}
				}
			}
			SOV_get_centre_military_support = yes 
		}

	}
	
}

SOV_decrease_all_opposition_army = {
	custom_effect_tooltip = SOV_decrease_all_opposition_army_tt
	subtract_from_variable = { SOV.SOV_left_opposition_army_support = 0.02 }
	subtract_from_variable = { SOV.SOV_right_opposition_army_support = 0.02 }
	subtract_from_variable = { SOV.SOV_exiles_army_support = 0.02 }
	clamp_variable = {
		var = SOV.SOV_left_opposition_army_support
		max = 0.9
		min = 0.1
	}
	clamp_variable = {
		var = SOV.SOV_right_opposition_army_support
		max = 0.9
		min = 0.1
	}
	clamp_variable = {
		var = SOV.SOV_exiles_army_support
		max = 0.9
		min = 0.1
	}
	SOV_get_centre_military_support = yes 
}
SOV_decrease_all_opposition_navy = {
	custom_effect_tooltip = SOV_decrease_all_opposition_navy_tt
	subtract_from_variable = { SOV.SOV_left_opposition_navy_support = 0.02 }
	subtract_from_variable = { SOV.SOV_right_opposition_navy_support = 0.02 }
	subtract_from_variable = { SOV.SOV_exiles_navy_support = 0.02 }
	clamp_variable = {
		var = SOV.SOV_left_opposition_navy_support
		max = 0.9
		min = 0.1
	}
	clamp_variable = {
		var = SOV.SOV_right_opposition_navy_support
		max = 0.9
		min = 0.1
	}
	clamp_variable = {
		var = SOV.SOV_exiles_navy_support
		max = 0.9
		min = 0.1
	}
	SOV_get_centre_military_support = yes 
}
SOV_decrease_all_opposition_air = {
	custom_effect_tooltip = SOV_decrease_all_opposition_air_tt
	subtract_from_variable = { SOV.SOV_left_opposition_air_support = 0.02 }
	subtract_from_variable = { SOV.SOV_right_opposition_air_support = 0.02 }
	subtract_from_variable = { SOV.SOV_exiles_air_support = 0.02 }
	clamp_variable = {
		var = SOV.SOV_left_opposition_air_support
		max = 0.9
		min = 0.1
	}
	clamp_variable = {
		var = SOV.SOV_right_opposition_air_support
		max = 0.9
		min = 0.1
	}
	clamp_variable = {
		var = SOV.SOV_exiles_air_support
		max = 0.9
		min = 0.1
	}
	SOV_get_centre_military_support = yes 
}

SOV_set_up_dynamic_modifiers_civil_war = { #Used for Stalinist side in the CW
	### Set up modifiers based on which ones have been added

	# opposition Support modifier. New, appearing at the start of the Civil War
	add_dynamic_modifier = {
		modifier = SOV_opposition_popular_support
	}
	# Five Year Plan
	IF = {
		limit = {
			SOV = { has_dynamic_modifier = { modifier = SOV_second_five_year_plan_dynamic_modifier }}
		}
		add_dynamic_modifier = { modifier = SOV_second_five_year_plan_dynamic_modifier }
	}
	ELSE_IF = {
		limit = {
			SOV = { has_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier }}
		}
		add_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier }
	}
	ELSE_IF = {
		limit = {
			SOV = { has_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier }}
		}
		add_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier }
	}
	ELSE_IF = {
		limit = {
			SOV = { has_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier }}
		}
		add_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier }
	}
	ELSE_IF = {
		limit = {
			SOV = { has_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier }}
		}
		add_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier }
	}

	# Politburo
	# Don't add, that will be managed by Stalin's focus completion

	# Army modifier, always present
	add_dynamic_modifier = {
		modifier = SOV_the_red_army_dynamic_modifier
	}
	# AirForce, always present as well
	add_dynamic_modifier = {
		modifier = SOV_soviet_airforce_dynamic_modifier
	}



	### Set variables
	# Copy Red Army Dynamic Modifier
	set_variable = { SOS.SOV_the_red_army_mobilization_laws_cost_factor = SOV.SOV_the_red_army_mobilization_laws_cost_factor }
	set_variable = { SOS.SOV_the_red_army_army_org_factor = SOV.SOV_the_red_army_army_org_factor }
	set_variable = { SOS.SOV_the_red_army_experience_gain_army_factor = SOV.SOV_the_red_army_experience_gain_army_factor }
	set_variable = { SOS.SOV_the_red_army_experience_loss_factor = SOV.SOV_the_red_army_experience_loss_factor }
	set_variable = { SOS.SOV_the_red_army_mobilization_speed = SOV.SOV_the_red_army_mobilization_speed }
	set_variable = { SOS.SOV_the_red_army_conscription_factor = SOV.SOV_the_red_army_conscription_factor }
	set_variable = { SOS.SOV_the_red_army_modifier_army_sub_unit_cavalry_speed_factor = SOV.SOV_the_red_army_modifier_army_sub_unit_cavalry_speed_factor }
	set_variable = { SOS.SOV_the_red_army_cavalry_attack_factor = SOV.SOV_the_red_army_cavalry_attack_factor }
	set_variable = { SOS.SOV_the_red_army_training_time_factor = SOV.SOV_the_red_army_training_time_factor }
	set_variable = { SOS.SOV_the_red_army_winter_attrition_factor = SOV.SOV_the_red_army_winter_attrition_factor }
	set_variable = { SOS.SOV_the_red_army_heat_attrition_factor = SOV.SOV_the_red_army_heat_attrition_factor }
	set_variable = { SOS.SOV_the_red_army_acclimatization_cold_climate_gain_factor = SOV.SOV_the_red_army_acclimatization_cold_climate_gain_factor }
	set_variable = { SOS.SOV_the_red_army_acclimatization_hot_climate_gain_factor = SOV.SOV_the_red_army_acclimatization_hot_climate_gain_factor }
	set_variable = { SOS.SOV_the_red_army_terrain_trait_xp_gain_factor = SOV.SOV_the_red_army_terrain_trait_xp_gain_factor }
	set_variable = { SOS.SOV_the_red_army_unit_infantry_design_cost_factor = SOV.SOV_the_red_army_unit_infantry_design_cost_factor }
	set_variable = { SOS.SOV_the_red_army_unit_artillery_brigade_design_cost_factor = SOV.SOV_the_red_army_unit_artillery_brigade_design_cost_factor }
	set_variable = { SOS.SOV_the_red_army_army_org_regain = SOV.SOV_the_red_army_army_org_regain }
	set_variable = { SOS.SOV_the_red_army_special_forces_cap = SOV.SOV_the_red_army_special_forces_cap }
	set_variable = { SOS.SOV_the_red_army_land_doctrine_cost_factor = SOV.SOV_the_red_army_land_doctrine_cost_factor }
	set_variable = { SOS.SOV_the_red_army_cas_damage_reduction = SOV.SOV_the_red_army_cas_damage_reduction }
	set_variable = { SOS.SOV_the_red_army_max_dig_in = SOV.SOV_the_red_army_max_dig_in }
	set_variable = { SOS.SOV_the_red_army_dig_in_speed_factor = SOV.SOV_the_red_army_dig_in_speed_factor }
	set_variable = { SOS.SOV_the_red_army_operative_slot = SOV.SOV_the_red_army_operative_slot }
	set_variable = { SOS.SOV_the_red_army_max_command_power_mult = SOV.SOV_the_red_army_max_command_power_mult }

	# Copy Air Force Dynamic Modifiers
	set_variable = { SOS.SOV_soviet_airforce_air_accidents_factor = SOV.SOV_soviet_airforce_air_accidents_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_night_penalty = SOV.SOV_soviet_airforce_air_night_penalty }
	set_variable = { SOS.SOV_soviet_airforce_air_weather_penalty = SOV.SOV_yyy }
	set_variable = { SOS.SOV_soviet_airforce_air_wing_xp_loss_when_killed_factor = SOV.SOV_soviet_airforce_air_wing_xp_loss_when_killed_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_mission_xp_gain_factor = SOV.SOV_soviet_airforce_air_mission_xp_gain_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_ace_generation_chance_factor = SOV.SOV_soviet_airforce_air_ace_generation_chance_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_training_xp_gain_factor = SOV.SOV_soviet_airforce_air_training_xp_gain_factor }
	set_variable = { SOS.SOV_soviet_airforce_aircraft_manufacturer_cost_factor = SOV.SOV_soviet_airforce_aircraft_manufacturer_cost_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_range_factor = SOV.SOV_soviet_airforce_air_range_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_fuel_consumption_factor = SOV.SOV_soviet_airforce_air_fuel_consumption_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_agility_factor = SOV.SOV_soviet_airforce_air_agility_factor }
	set_variable = { SOS.SOV_soviet_airforce_air_strategic_bomber_night_penalty = SOV.SOV_soviet_airforce_air_strategic_bomber_night_penalty }
	set_variable = { SOS.SOV_soviet_airforce_air_cas_present_factor = SOV.SOV_soviet_airforce_air_cas_present_factor }
	set_variable = { SOS.SOV_soviet_airforce_strategic_bomb_visibility = SOV.SOV_soviet_airforce_strategic_bomb_visibility }
	set_variable = { SOS.SOV_soviet_airforce_air_mission_efficiency = SOV.SOV_soviet_airforce_air_mission_efficiency }
	set_variable = { SOS.SOV_soviet_airforce_air_nav_efficiency = SOV.SOV_soviet_airforce_air_nav_efficiency }

	# Copy 5YP variables
	set_variable = { SOS.SOV_fyp_consumer_goods_factor = SOV.SOV_fyp_consumer_goods_factor }
	set_variable = { SOS.SOV_fyp_fuel_gain_factor_from_states = SOV.SOV_fyp_fuel_gain_factor_from_states }
	set_variable = { SOS.SOV_fyp_industrial_capacity_dockyard = SOV.SOV_fyp_industrial_capacity_dockyard }
	set_variable = { SOS.SOV_fyp_industrial_capacity_factory = SOV.SOV_fyp_industrial_capacity_factory }
	set_variable = { SOS.SOV_fyp_industrial_concern_cost_factor = SOV.SOV_fyp_industrial_concern_cost_factor }
	set_variable = { SOS.SOV_fyp_industry_repair_factor = SOV.SOV_fyp_industry_repair_factor }
	set_variable = { SOS.SOV_fyp_line_change_production_efficiency_factor = SOV.SOV_fyp_line_change_production_efficiency_factor }
	set_variable = { SOS.SOV_fyp_local_resources_factor = SOV.SOV_fyp_local_resources_factor }
	set_variable = { SOS.SOV_fyp_production_factory_efficiency_gain_factor = SOV.SOV_fyp_production_factory_efficiency_gain_factor }
	set_variable = { SOS.SOV_fyp_production_factory_max_efficiency_factor = SOV.SOV_fyp_production_factory_max_efficiency_factor }
	set_variable = { SOS.SOV_fyp_production_factory_start_efficiency_factor = SOV.SOV_fyp_production_factory_start_efficiency_factor }
	set_variable = { SOS.SOV_fyp_production_oil_factor = SOV.SOV_fyp_production_oil_factor }
	set_variable = { SOS.SOV_fyp_production_speed_arms_factory_factor = SOV.SOV_fyp_production_speed_arms_factory_factor }
	set_variable = { SOS.SOV_fyp_production_speed_industrial_complex_factor = SOV.SOV_fyp_production_speed_industrial_complex_factor }

	### PROPAGANDA ###
	set_variable = { SOS.SOV_new_propaganda_campaign = SOV.SOV_new_propaganda_campaign } #Used to pick a poster slot
	set_variable = { SOS.SOV_propaganda_campaign_to_remove = SOV.SOV_propaganda_campaign_to_remove } #Used to reset a poster slot
	set_variable = { SOS.SOV_propaganda_locked_slot_index = SOV.SOV_propaganda_locked_slot_index } #### === INSERT LOCKED SLOT INDEX IN STRIPE HERE === ####
	set_variable = { SOS.SOV_propaganda_empty_slot_index = SOV.SOV_propaganda_empty_slot_index } #### === INSERT EMPTY SLOT INDEX IN STRIPE HERE === ####
	set_variable = { SOS.SOV_poster_left_icon_frame = SOV_propaganda_locked_slot_index } #Set up the appropriate empty slot GFX
	set_variable = { SOS.SOV_poster_center_icon_frame = SOV_propaganda_locked_slot_index } #Set up the appropriate locked slot GFX
	set_variable = { SOS.SOV_poster_right_icon_frame = SOV_propaganda_locked_slot_index } #Set up the appropriate locked slot GFX

	set_variable = { SOS.SOV_default_propaganda_campaign_duration = SOV.SOV_default_propaganda_campaign_duration } #Duration days for propaganda campaigns
	set_variable = { SOS.SOV_default_propaganda_campaign_cooldown = SOV.SOV_default_propaganda_campaign_cooldown } #Days of cooldown after a Propaganda Campaign has been finished.
	set_variable = { SOS.SOV_default_propaganda_campaign_old_days = SOV.SOV_default_propaganda_campaign_old_days } #A propaganda poster will age (change GFX) after this amount of days have passed since the campaign was activated
	set_variable = { SOS.SOV_propaganda_cost = SOV.SOV_propaganda_cost } #Used not only to set the rest of propaganda campaign costs here, but also used to increase each campaign cost after being activated
	set_variable = { SOS.SOV_propaganda_cost_increase = SOV.SOV_propaganda_cost_increase } #This will multiply the regular propaganda_cost each time a propaganda campaign is activated, so that it's cost is increased
	set_variable = { SOS.SOV_propaganda_motherland_calls_cost = SOV_propaganda_cost } # ID = 2
	set_variable = { SOS.SOV_propaganda_victory_at_hand_cost = SOV_propaganda_cost } # ID = 3
	set_variable = { SOS.SOV_propaganda_defend_moscow_cost = SOV_propaganda_cost } # ID = 4
	set_variable = { SOS.SOV_propaganda_fight_to_the_last_cost = SOV_propaganda_cost } # ID = 5
	set_variable = { SOS.SOV_propaganda_hit_of_hammer_cost = SOV_propaganda_cost } # ID = 6
	set_variable = { SOS.SOV_propaganda_transport_cost = SOV_propaganda_cost } # ID = 7
	set_variable = { SOS.SOV_propaganda_oil_for_the_motherland_cost = SOV_propaganda_cost } # ID = 8
	set_variable = { SOS.SOV_propaganda_more_metal_cost = SOV_propaganda_cost } # ID = 9
	set_variable = { SOS.SOV_propaganda_high_yield_cost = SOV_propaganda_cost } # ID = 10
	set_variable = { SOS.SOV_propaganda_for_the_motherland_cost = SOV_propaganda_cost } # ID = 11
	set_variable = { SOS.SOV_propaganda_heroes_forward_cost = SOV_propaganda_cost } # ID = 12
	set_variable = { SOS.SOV_propaganda_anti_capitalism_cost = SOV_propaganda_cost } # ID = 13
	set_variable = { SOS.SOV_propaganda_anti_fascism_cost = SOV_propaganda_cost } # ID = 14
	set_variable = { SOS.SOV_propaganda_treacherous_enemy_cost = SOV_propaganda_cost } # ID = 15
	set_variable = { SOS.SOV_propaganda_do_not_blab_cost = SOV_propaganda_cost } # ID = 16
	set_variable = { SOS.SOV_propaganda_sweep_scum_out_cost = SOV_propaganda_cost } # ID = 17
	set_variable = { SOS.SOV_propaganda_knowledge_cost = SOV_propaganda_cost } # ID = 18
	set_variable = { SOS.SOV_propaganda_builders_of_communism_cost = SOV_propaganda_cost } # ID = 19
	set_variable = { SOS.SOV_propaganda_glory_to_partisans_cost = SOV_propaganda_cost } # ID = 20
	set_variable = { SOS.SOV_propaganda_death_to_invaders_cost = SOV_propaganda_cost } # ID = 21
	set_variable = { SOS.SOV_propaganda_stalin_cost = SOV_propaganda_cost } # ID = 22
	set_variable = { SOS.SOV_propaganda_rebuild_to_glory_cost = SOV_propaganda_cost } # ID = 23
	set_variable = { SOS.SOV_propaganda_peace_cost = SOV_propaganda_cost } # ID = 24
	set_variable = { SOS.SOV_propaganda_happy_life_cost = SOV_propaganda_cost } # ID = 25

	set_variable = { SOS.SOV_propaganda_campaigns_taken = SOV.SOV_propaganda_campaigns_taken } #Used as a requirement for certain focuses

	#GOSPROYEKTSTROY
	set_variable = { SOS.SOV_gosproyektstroy_decision_cost = SOV.SOV_gosproyektstroy_decision_cost }
	set_variable = { SOS.SOV_gosproyektstroy_decision_time = SOV.SOV_gosproyektstroy_decision_time }

	#USSR ACADEMY OF SCIENCES
	set_variable = { SOS.SOV_national_academy_of_sciences_research_increase = SOV.SOV_national_academy_of_sciences_research_increase } #Research speed increment for each Academy of Sciences built
	set_variable = { SOS.SOV_academy_of_sciences_research_speed_factor = SOV.SOV_academy_of_sciences_research_speed_factor } #Var used by the dynamic modifier to apply the research bonus
	set_variable = { SOS.SOV_national_academy_of_sciences_construction_cost = SOV.SOV_national_academy_of_sciences_construction_cost } #The cost for activating the decision to build an academy.
	set_variable = { SOS.SOV_national_academy_of_sciences_construction_time = SOV.SOV_national_academy_of_sciences_construction_time } #The time it takes for the decision to finish and build an academy.
	set_variable = { SOS.SOV_academies_required_for_atomic_bomb_project = SOV.SOV_academies_required_for_atomic_bomb_project } #Number of academies of sciences required to pick focus Soviet Atomic Bomb Project

	#THIRD FIVE YEAR PLAN FACTORY REQUIREMENTS
	set_variable = { SOS.SOV_fyp_factory_requirement_tier_1 = SOV.SOV_fyp_factory_requirement_tier_1 }
	set_variable = { SOS.SOV_fyp_factory_requirement_tier_2 = SOV.SOV_fyp_factory_requirement_tier_2 }
	set_variable = { SOS.SOV_fyp_factory_requirement_tier_3 = SOV.SOV_fyp_factory_requirement_tier_3 }
	set_variable = { SOS.SOV_fyp_factory_requirement_tier_4 = SOV.SOV_fyp_factory_requirement_tier_4 }
	set_variable = { SOS.SOV_fyp_factory_requirement_tier_5 = SOV.SOV_fyp_factory_requirement_tier_5 }

	#THE RED FLEET NATIONAL SPIRIT VARS
	set_variable = { SOS.SOV_red_fleet_navy_refit_ic_cost = SOV.SOV_red_fleet_navy_refit_ic_cost }
	set_variable = { SOS.SOV_red_fleet_repair_speed_factor = SOV.SOV_red_fleet_repair_speed_factor }

	set_variable = { SOS.SOV_red_fleet_naval_coordination = SOV.SOV_red_fleet_naval_coordination } #-> Advanced Maneuvers
	set_variable = { SOS.SOV_red_fleet_navy_visibility = SOV.SOV_red_fleet_navy_visibility } #-> Advanced Maneuvers
	set_variable = { SOS.SOV_red_fleet_positioning = SOV.SOV_red_fleet_positioning } #-> Advanced Maneuvers
	set_variable = { SOS.SOV_red_fleet_screening_efficiency = SOV.SOV_red_fleet_screening_efficiency } #-> Advanced Maneuvers
	set_variable = { SOS.SOV_red_fleet_naval_invasion_capacity = SOV.SOV_red_fleet_naval_invasion_capacity } #-> Marines
	set_variable = { SOS.SOV_red_fleet_invasion_preparation = SOV.SOV_red_fleet_invasion_preparation } #-> Marines
	set_variable = { SOS.SOV_red_fleet_special_forces_cap = SOV.SOV_red_fleet_special_forces_cap } #-> Marines

	#DEFENSE INDUSTRY NATIONAL SPIRIT VARS
	set_variable = { SOS.SOV_defense_industry_conversion_cost_civ_to_mil_factor = SOV.SOV_defense_industry_conversion_cost_civ_to_mil_factor }
	set_variable = { SOS.SOV_defense_industry_production_factory_max_efficiency_factor = SOV.SOV_defense_industry_production_factory_max_efficiency_factor }
	set_variable = { SOS.SOV_defense_industry_production_factory_efficiency_gain_factor = SOV.SOV_defense_industry_production_factory_efficiency_gain_factor }
	set_variable = { SOS.SOV_defense_industry_line_change_production_efficiency_factor = SOV.SOV_defense_industry_line_change_production_efficiency_factor }

	set_variable = { SOS.SOV_defense_industry_tank_manufacturer_cost_factor = SOV.SOV_defense_industry_tank_manufacturer_cost_factor } #-> Tank Designer focus
	set_variable = { SOS.SOV_defense_industry_land_equipment_upgrade_xp_cost = SOV.SOV_defense_industry_land_equipment_upgrade_xp_cost } #-> Tankograd
	set_variable = { SOS.SOV_defense_industry_materiel_manufacturer_cost_factor = SOV.SOV_defense_industry_materiel_manufacturer_cost_factor } #-> Equipment Designers focus
	set_variable = { SOS.SOV_defense_industry_breakthrough_factor = SOV.SOV_defense_industry_breakthrough_factor } #-> PC of Mortar Armament
	set_variable = { SOS.SOV_defense_industry_production_speed_coastal_bunker_factor = SOV.SOV_defense_industry_production_speed_coastal_bunker_factor } #-> Military Engineering University
	set_variable = { SOS.SOV_defense_industry_production_speed_bunker_factor = SOV.SOV_defense_industry_production_speed_bunker_factor } #-> Military Engineering University
	set_variable = { SOS.SOV_defense_industry_max_dig_in_factor = SOV.SOV_defense_industry_max_dig_in_factor } #-> Military Engineering University
	set_variable = { SOS.SOV_defense_industry_army_artillery_defence_factor = SOV.SOV_defense_industry_army_artillery_defence_factor } #-> Military Engineering University

	#THE POLITBURO SPIRIT VARS
	set_variable = { SOS.SOV_the_politburo_communism_drift = SOV.SOV_the_politburo_communism_drift }

	set_variable = { SOS.SOV_the_politburo_defensive_war_stability_factor = SOV.SOV_the_politburo_defensive_war_stability_factor } # -> Socialism in One Country
	set_variable = { SOS.SOV_the_politburo_drift_defence_factor = SOV.SOV_the_politburo_drift_defence_factor } # -> Socialism in One Country
	set_variable = { SOS.SOV_the_politburo_political_power_factor = SOV.SOV_the_politburo_political_power_factor } #0.05 -> The Stalin Constitution
	set_variable = { SOS.SOV_the_politburo_economy_cost_factor = SOV.SOV_the_politburo_economy_cost_factor } # -> Secure the Administration
	set_variable = { SOS.SOV_the_politburo_trade_laws_cost_factor = SOV.SOV_the_politburo_trade_laws_cost_factor } # -> Secure the Administration
	set_variable = { SOS.SOV_the_politburo_mobilization_laws_cost_factor = SOV.SOV_the_politburo_mobilization_laws_cost_factor } # -> Secure the Administration
	set_variable = { SOS.SOV_the_politburo_good_consumer_goods_factor = SOV.SOV_the_politburo_good_consumer_goods_factor } # -> Collectivization Process
	set_variable = { SOS.SOV_the_politburo_production_speed_buildings_factor = SOV.SOV_the_politburo_production_speed_buildings_factor } # -> Collectivization Process
	set_variable = { SOS.SOV_the_politburo_global_building_slots_factor = SOV.SOV_the_politburo_global_building_slots_factor } # -> Collectivization Process
	set_variable = { SOS.SOV_the_politburo_max_surrender_limit_offset = SOV.SOV_the_politburo_max_surrender_limit_offset } # -> The Workers Dictatorship
	set_variable = { SOS.SOV_the_politburo_max_command_power = SOV.SOV_the_politburo_max_command_power } # -> Organization, Centralization, Discipline
	set_variable = { SOS.SOV_the_politburo_mobilization_speed = SOV.SOV_the_politburo_mobilization_speed } # -> Organization, Centralization, Discipline
	set_variable = { SOS.SOV_the_politburo_operative_slot = SOV.SOV_the_politburo_operative_slot } # -> Behead the Snake
	set_variable = { SOS.SOV_the_politburo_political_advisor_cost_factor = SOV.SOV_the_politburo_political_advisor_cost_factor } # -> Freedom of Debate, Unity of Action
	set_variable = { SOS.SOV_the_politburo_opinion_gain_monthly_same_ideology_factor = SOV.SOV_the_politburo_opinion_gain_monthly_same_ideology_factor } # -> The Construction of Communism

	#MERGE DESIGNERS VARS
	set_variable = { SOS.SOV_merge_designers_cost = SOV.SOV_merge_designers_cost } #Default cost for merge designer decisions
	set_variable = { SOS.SOV_merge_designers_time = SOV.SOV_merge_designers_time } #The time to complete merge designer decisions

	#INDUSTRIAL RELOCATION
	set_variable = { SOS.SOV_industrial_relocation_max_factories = SOV.SOV_industrial_relocation_max_factories } #Max amount of factories that will be relocated by a decision
	set_variable = { SOS.SOV_industrial_relocation_days = SOV.SOV_industrial_relocation_days } #Decision duration (can be halved by focus)
	set_variable = { SOS.SOV_industrial_relocation_trains_focus = SOV.SOV_industrial_relocation_trains_focus } #Trains to be used by the focus (20 trains / factory)
	set_variable = { SOS.SOV_industrial_relocation_trains_focus_negative = SOV.SOV_industrial_relocation_trains_focus_negative } #Trains to be used by the focus (20 trains / factory)
	set_variable = { SOS.SOV_industrial_relocation_trains_decision = SOV.SOV_industrial_relocation_trains_decision } #Trains to be used by the generic decision (20 trains / factory)
	set_variable = { SOS.SOV_industrial_relocation_trains_decision_negative = SOV.SOV_industrial_relocation_trains_decision_negative } #Trains to be used by the generic decision (20 trains / factory)
	set_variable = { SOS.SOV_industrial_relocation_trains_decision_aluminium = SOV.SOV_industrial_relocation_trains_decision_aluminium } #Trains to be used by the aluminium decision (20 trains / factory)
	set_variable = { SOS.SOV_industrial_relocation_trains_decision_aluminium_negative = SOV.SOV_industrial_relocation_trains_decision_aluminium_negative } #Trains to be used by the aluminium decision (20 trains / factory)

	#DECISIONS COST - For cost reductions
	set_variable = { SOS.SOV_pressure_country_government_cost = SOV.SOV_pressure_country_government_cost }
	set_variable = { SOS.SOV_promote_ideology_rallies_cost = SOV.SOV_promote_ideology_rallies_cost }
	set_variable = { SOS.SOV_send_ultimatum_to_country_cost = SOV.SOV_send_ultimatum_to_country_cost }
	set_variable = { SOS.SOV_send_infantry_equipment_cost = SOV.SOV_send_infantry_equipment_cost }
	set_variable = { SOS.SOV_peace_deal_cost = SOV.SOV_peace_deal_cost }
	set_variable = { SOS.SOV_sinkiang_proposal_cost = SOV.SOV_sinkiang_proposal_cost }
	set_variable = { SOS.SOV_sinkiang_puppeting_cost = SOV.SOV_sinkiang_puppeting_cost } #This will increase for each state controlled by SIK excluding the ones owned at game start.
	set_variable = { SOS.SOV_sinkiang_puppeting_cost_increase_per_state = SOV.SOV_sinkiang_puppeting_cost_increase_per_state }

	#DECISIONS DAYS
	set_variable = { SOS.SOV_sinkiang_mineral_prospection_days = SOV.SOV_sinkiang_mineral_prospection_days }
	set_variable = { SOS.SOV_sinkiang_oil_prospection_days = SOV.SOV_sinkiang_oil_prospection_days }
	set_variable = { SOS.SOV_sinkiang_military_aid_days = SOV.SOV_sinkiang_military_aid_days }
	set_variable = { SOS.SOV_sinkiang_support_expansion_days = SOV.SOV_sinkiang_support_expansion_days }

	#DECISIONS DAYS
	set_variable = { SOS.SOV_pressure_government_days = SOV.SOV_pressure_government_days }
	set_variable = { SOS.SOV_pressure_government_cd = SOV.SOV_pressure_government_cd }
	set_variable = { SOS.SOV_ideological_rallies_days = SOV.SOV_ideological_rallies_days }
	set_variable = { SOS.SOV_ultimatum_days = SOV.SOV_ultimatum_days }
	set_variable = { SOS.SOV_send_infantry_equipment_cd = SOV.SOV_send_infantry_equipment_cd }
	set_variable = { SOS.SOV_operation_countenance_preparation_days = SOV.SOV_operation_countenance_preparation_days }
	set_variable = { SOS.SOV_operation_countenance_mission_days = SOV.SOV_operation_countenance_mission_days }

	set_variable = { SOS.SOV_permanent_revolution_uprising_days = SOV.SOV_permanent_revolution_uprising_days }

	# set_variable = { SOS.xxx = SOV.yyy }

}

SOV_permanent_revolution_spawn_fifth_columnists_effect = {
	if = {
		limit = {
			FROM = {
				any_controlled_state = {
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
			}
		}
		FROM = {
			random_controlled_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
	}
	else_if = {
		limit = {
			FROM = {
				any_controlled_state = {
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 1500
						}
					}
				}
			}
		}
		random_state = {
			limit = {
				is_controlled_by = FROM
				is_in_home_area = yes
				is_capital = no
				NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
				any_state = {
					is_controlled_by = ROOT
					distance_to = {
						target = PREV
						value < 1500
					}
				}
			}
			set_state_flag = SOV_permanent_revolution_uprising_flag
			create_unit = {
				division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
				owner = ROOT
				allow_spawning_on_enemy_provs = yes
			}
			create_unit = {
				division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
				owner = ROOT
				allow_spawning_on_enemy_provs = yes
			}
			create_unit = {
				division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
				owner = ROOT
				allow_spawning_on_enemy_provs = yes
			}
		}
	}
	else = {
		random_state = {
			limit = {
				is_controlled_by = FROM
				is_in_home_area = yes
				is_capital = no
				NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
			}
			set_state_flag = SOV_permanent_revolution_uprising_flag
			create_unit = {
				division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
				owner = ROOT
				allow_spawning_on_enemy_provs = yes
			}
			create_unit = {
				division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
				owner = ROOT
				allow_spawning_on_enemy_provs = yes
			}
			create_unit = {
				division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
				owner = ROOT
				allow_spawning_on_enemy_provs = yes
			}
		}
	}

	if = {
		limit = {
			FROM = {
				num_of_controlled_states > 4
				communism > 0.1
			}
		}
		if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							distance_to = {
								target = PREV
								value < 750
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else_if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							distance_to = {
								target = PREV
								value < 1500
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 1500
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else = {
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
	}
	if = {
		limit = {
			FROM = {
				num_of_controlled_states > 6
				communism > 0.2
			}
		}
		if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
							distance_to = {
								target = PREV
								value < 750
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else_if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
							distance_to = {
								target = PREV
								value < 1500
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 1500
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else = {
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
	}
	if = {
		limit = {
			FROM = {
				num_of_controlled_states > 8
				communism > 0.3
			}
		}
		if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							distance_to = {
								target = PREV
								value < 750
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else_if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							distance_to = {
								target = PREV
								value < 1500
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 1500
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 1500
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else = {
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
	}
	if = {
		limit = {
			FROM = {
				num_of_controlled_states > 10
				communism > 0.4
			}
		}
		if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							distance_to = {
								target = PREV
								value < 750
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 750
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else_if = {
			limit = {
				FROM = {
					any_controlled_state = {
						is_in_home_area = yes
						is_capital = no
						NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
						any_state = {
							is_controlled_by = ROOT
							distance_to = {
								target = PREV
								value < 1500
							}
						}
					}
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 1500
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
					any_state = {
						is_controlled_by = ROOT
						distance_to = {
							target = PREV
							value < 1500
						}
					}
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
		else = {
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
			random_state = {
				limit = {
					is_controlled_by = FROM
					is_in_home_area = yes
					is_capital = no
					NOT = { has_state_flag = SOV_permanent_revolution_uprising_flag }
				}
				set_state_flag = SOV_permanent_revolution_uprising_flag
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
				create_unit = {
					division = "division_template = \"Trotskyist Revolutionary Militias\" start_experience_factor = 0.1"
					owner = ROOT
					allow_spawning_on_enemy_provs = yes
				}
			}
		}
	}
}

####### Change stuff based on ideology #######

SOV_replace_stuff_based_on_ideology_effect = {
	### FIVE YEAR PLANS
	if = {
		limit = {
			NOT = { has_government = communism }
		}
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier_alt }
		}
	}
	else = {
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_third_five_year_plan_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_third_five_year_plan_civilian_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_third_five_year_plan_military_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_fourth_five_year_plan_dynamic_modifier }
		}
	}

	### USSR ACADEMIES OF SCIENCES
	if = { #IS NOT COMMIE -> ADD ALT STUFF
		limit = {
			NOT = { has_government = communism }
		}
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_ussr_academy_of_sciences_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_ussr_academy_of_sciences_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_ussr_academy_of_sciences_dynamic_modifier_alt }
		}
	}
	else = { #IS COMMIE -> ADD REGULAR STUFF
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_ussr_academy_of_sciences_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_ussr_academy_of_sciences_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_ussr_academy_of_sciences_dynamic_modifier }
		}
	}

	### SOVIET AIRFORCE
	if = { #IS NOT COMMIE -> ADD ALT STUFF
		limit = {
			NOT = { has_government = communism }
		}
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_soviet_airforce_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_soviet_airforce_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_soviet_airforce_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_soviet_airforce_aircraft_production_focus_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_soviet_airforce_aircraft_production_focus_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_soviet_airforce_aircraft_production_focus_dynamic_modifier_alt }
		}
	}
	else = { #IS COMMIE -> ADD REGULAR STUFF
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_soviet_airforce_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_soviet_airforce_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_soviet_airforce_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_soviet_airforce_aircraft_production_focus_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_soviet_airforce_aircraft_production_focus_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_soviet_airforce_aircraft_production_focus_dynamic_modifier }
		}
	}

	### THE RED FLEET
	if = { #IS NOT COMMIE -> ADD ALT STUFF
		limit = {
			NOT = { has_government = communism }
		}
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_1_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_1_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_1_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_2_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_2_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_2_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_1_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_1_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_1_dynamic_modifier_alt }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_2_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_2_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_2_dynamic_modifier_alt }
		}
	}
	else = { #IS COMMIE -> ADD REGULAR STUFF
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_1_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_1_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_1_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_2_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_2_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_surface_2_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_1_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_1_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_1_dynamic_modifier }
		}
		else_if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_2_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_2_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_the_red_fleet_submarine_2_dynamic_modifier }
		}
	}

	### THE RED ARMY
	if = { #IS NOT COMMIE -> ADD ALT STUFF
		limit = {
			NOT = { has_government = communism }
		}
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_army_dynamic_modifier }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_army_dynamic_modifier }
			add_dynamic_modifier = { modifier = SOV_the_red_army_dynamic_modifier_alt }
		}
	}
	else = { #IS COMMIE -> ADD REGULAR STUFF
		if = {
			limit = {
				has_dynamic_modifier = { modifier = SOV_the_red_army_dynamic_modifier_alt }
			}
			remove_dynamic_modifier = { modifier = SOV_the_red_army_dynamic_modifier_alt }
			add_dynamic_modifier = { modifier = SOV_the_red_army_dynamic_modifier }
		}
	}
}