#	Example:
#
#	example_effect = {
#		add_political_power = 66
#		add_popularity = {
#			ideology = fascism
#			popularity = 0.33
#		}
#	}
#
#
#	In a script file:
#
#	effect = {
#		example_effect = yes
#	}
#
increase_trade_law = {
	if = {
		limit = {
			has_idea = closed_economy
		}
		swap_ideas = {
			remove_idea = closed_economy
			add_idea = limited_exports
		}
	}
	else_if = {
		limit = {
			has_idea = limited_exports
		}
		swap_ideas = {
			remove_idea = limited_exports
			add_idea = export_focus
		}
	}
	else_if = {
		limit = {
			has_idea = export_focus
		}
		swap_ideas = {
			remove_idea = export_focus
			add_idea = free_trade
		}
	}
	else_if = {
		limit = {
			has_idea = free_trade
		}
		add_political_power = 50
	}
}

decrease_trade_law = {
	if = {
		limit = {
			has_idea = free_trade
		}
		swap_ideas = {
			remove_idea = free_trade
			add_idea = export_focus
		}
	}
	else_if = {
		limit = {
			has_idea = export_focus
		}
		swap_ideas = {
			remove_idea = export_focus
			add_idea = limited_exports
		}
	}
	else_if = {
		limit = {
			has_idea = limited_exports
		}
		swap_ideas = {
			remove_idea = limited_exports
			add_idea = closed_economy
		}
	}
	else_if = {
		limit = {
			has_idea = closed_economy
		}
		add_stability = 0.01
	}
}

increase_voting_rights = {
	if = {
		limit = {
			has_idea = absolutism
		}
		swap_ideas = {
			remove_idea = absolutism
			add_idea = oligarchism
		}
	}
	else_if = {
		limit = {
			has_idea = oligarchism
		}
		swap_ideas = {
			remove_idea = oligarchism
			add_idea = parliamentary_suffrage
		}
	}
	else_if = {
		limit = {
			has_idea = parliamentary_suffrage
		}
		swap_ideas = {
			remove_idea = parliamentary_suffrage
			add_idea = property_owners_suffrage
		}
	}
	else_if = {
		limit = {
			has_idea = property_owners_suffrage
		}
		swap_ideas = {
			remove_idea = property_owners_suffrage
			add_idea = universal_male_suffrage
		}
	}
	else_if = {
		limit = {
			has_idea = universal_male_suffrage
		}
		swap_ideas = {
			remove_idea = universal_male_suffrage
			add_idea = universal_suffrage
		}
	}
	else = {
		add_political_power = -50
		add_stability = 0.01
	}
}

decrease_voting_rights = {
	if = {
		limit = {
			has_idea = universal_suffrage
		}
		swap_ideas = {
			remove_idea = universal_suffrage
			add_idea = universal_male_suffrage
		}
	}
	else_if = {
		limit = {
			has_idea = universal_male_suffrage
		}
		swap_ideas = {
			remove_idea = universal_male_suffrage
			add_idea = property_owners_suffrage
		}
	}
	else_if = {
		limit = {
			has_idea = property_owners_suffrage
		}
		swap_ideas = {
			remove_idea = property_owners_suffrage
			add_idea = parliamentary_suffrage
		}
	}
	else_if = {
		limit = {
			has_idea = parliamentary_suffrage
		}
		swap_ideas = {
			remove_idea = parliamentary_suffrage
			add_idea = oligarchism
		}
	}
	else_if = {
		limit = {
			has_idea = oligarchism
		}
		swap_ideas = {
			remove_idea = oligarchism
			add_idea = absolutism
		}
	}
	else = {
		add_political_power = 50
		add_stability = -0.01
	}
}

add_1_infra_random_state = {
	random_owned_controlled_state = {
		limit = {
			free_building_slots = {
				building = infrastructure
				size > 0
			}
			OR = {
				is_in_home_area = yes
				NOT = {
					owner = {
						any_owned_state = {
							free_building_slots = {
								building = infrastructure
								size > 0
							}
							is_in_home_area = yes
						}
					}
				}
			}
		}
		add_building_construction = {
			type = infrastructure
			level = 1
			instant_build = yes
		}
	}
}

add_1_civfac_random_state = {
	random_owned_controlled_state = {
		limit = {
			free_building_slots = {
				building = industrial_complex
				size > 0
				include_locked = yes
			}
			OR = {
				is_in_home_area = yes
				NOT = {
					owner = {
						any_owned_state = {
							free_building_slots = {
								building = industrial_complex
								size > 0
								include_locked = yes
							}
							is_in_home_area = yes
						}
					}
				}
			}
		}
		add_extra_state_shared_building_slots = 1
		add_building_construction = {
			type = industrial_complex
			level = 1
			instant_build = yes
		}
	}
}

add_1_milfac_random_state = {
	random_owned_controlled_state = {
		limit = {
			free_building_slots = {
				building = arms_factory
				size > 0
				include_locked = yes
			}
			OR = {
				is_in_home_area = yes
				NOT = {
					owner = {
						any_owned_state = {
							free_building_slots = {
								building = arms_factory
								size > 0
								include_locked = yes
							}
							is_in_home_area = yes
						}
					}
				}
			}
		}
		add_extra_state_shared_building_slots = 1
		add_building_construction = {
			type = arms_factory
			level = 1
			instant_build = yes
		}
	}
}

replace_civ_with_arms_factories = {
	random_owned_controlled_state = {
		limit = {
			is_fully_controlled_by = ROOT
			industrial_complex > 0
		}
		remove_building = {
			type = industrial_complex
			level = 1
		}
		add_building_construction = {
			type = arms_factory
			level = 1
			instant_build = yes
		}
	}
	random_owned_controlled_state = {
		limit = {
			is_fully_controlled_by = ROOT
			industrial_complex > 0
		}
		remove_building = {
			type = industrial_complex
			level = 1
		}
		add_building_construction = {
			type = arms_factory
			level = 1
			instant_build = yes
		}
	}
	random_owned_controlled_state = {
		limit = {
			is_fully_controlled_by = ROOT
			industrial_complex > 0
		}
		remove_building = {
			type = industrial_complex
			level = 1
		}
		add_building_construction = {
			type = arms_factory
			level = 1
			instant_build = yes
		}
	}
	random_owned_controlled_state = {
		limit = {
			is_fully_controlled_by = ROOT
			industrial_complex > 0
		}
		remove_building = {
			type = industrial_complex
			level = 1
		}
		add_building_construction = {
			type = arms_factory
			level = 1
			instant_build = yes
		}
	}
	random_owned_controlled_state = {
		limit = {
			is_fully_controlled_by = ROOT
			industrial_complex > 0
		}
		remove_building = {
			type = industrial_complex
			level = 1
		}
		add_building_construction = {
			type = arms_factory
			level = 1
			instant_build = yes
		}
	}
}

#### Italy/Italian Peninsula
ITA_Division_of_italy_variable = {
	hidden_effect = {
		add_to_variable = {
			var = UCT.ITA_FRA_Divide_Italy_Variable
			value = 1
		}
		if = {
			limit = {
				UCT = {
					check_variable = {
						ITA_FRA_Divide_Italy_Variable = 6
					}
				}
			}
			FRA = {
				country_event = {
					id = ita_confederation.5
				}
			}
		}
	}
}

ITA_Formation_of_Italy = {
	custom_effect_tooltip = ITA_Formation_of_Italy_Scripted
	hidden_effect = {
		#####For Piedmont
		### Flag Clearing
		PIE = {
			clr_country_flag = PIE_Central_States_Flag
			clr_country_flag = PIE_British_Diplomacy_yes
			clr_country_flag = PIE_RUS_Assistance_2IP
			clr_country_flag = PIE_Denied_French_Alliance
			clr_country_flag = PIE_Monaco_resolved
			clr_country_flag = FRA_Decline_To_Aid_Sardinia
			clr_country_flag = PIE_RUS_No_FRA
			clr_country_flag = PIE_Plombieres_complete
		}
		##Unification Event
		country_event = {
			id = ita.8
			hours = 1
		}
		###Sardinia Piedmont Historical and alternate conservative liberal path
		if = {
			limit = {
				AND = {
					tag = PIE
					has_completed_focus = PIE_Napoleon3_Help
				}
			}
			###Setting Cosmetic Tag and Flag for tag alias
			set_cosmetic_tag = PIE_focus_ITA
			set_country_flag = is_literally_italy
			####Country Events for Unification
			##Elections
			#country_event = {
			#	id = ita.41
			#	days = 25
			#}
			###Remove puppets
			if = {
				limit = {
					NSC = {
						exists = yes
						is_subject_of = ROOT
					}
				}
				annex_country = {
					target = NSC
					transfer_troops = yes
				}
			}
			###core land
			every_owned_state = {
				if = {
					limit = {
						NOT = {
							is_core_of = PIE
						}
					}
					add_core_of = PIE
				}
				remove_core_of = NSC
				remove_core_of = LVN
				remove_core_of = PRM
				remove_core_of = MOD
				remove_core_of = TUS
				remove_core_of = PAP
				remove_core_of = FRA
				remove_core_of = CBI
			}
			#hidden_effect = {
			#	remove_state_core = 812
			#	remove_state_core = 117
			#	remove_state_core = 997
			#	remove_state_core = 156
			#	remove_state_core = 115
			#	remove_state_core = 1380
			#}
			####Focus Tree
			load_focus_tree = italy_SPUnified
			###Politics and related things
			hidden_effect = {
				FRA = {
					mark_focus_tree_layout_dirty = yes
				}
				set_popularities = {
					anarchism = 6
					ideology_social_liberalism = 28
					ideology_radical_socialism = 1
					social_egalitarianism = 3
					centrism = 40
					traditional_conservatism = 12
					radical_democracy = 2
					chauvinist_populism = 1
					autocracy = 5
					fundamentalism = 2
				}
				set_politics = {
					ruling_party = centrism
				}
				add_ideas = {
					ITA_Unorganized_Army
					ITA_Unorganized_Navy
					ITA_Brigandage
					ITA_Different_Naval_Doctorines
					ITA_Reliance_on_Farming
				}
				remove_ideas = {
					PIE_Italian_National_Society
					ITA_AI_Boost_War
					PIE_Spearheading_The_Risorgimento
					PIE_Perfect_Fusion
					PIE_Sardinian_Question
					PIE_Dependency_On_Artisan_Industry
					#PIE_Baby_Boom
				}
				LVN = {
					remove_ideas = {
						LVN_National_Society_1
						LVN_National_Society_2
					}
				}
				#activate_mission = ITA_decision_deal_with_brigindage
			}
		}

		### Tuscany United Italy
		if = {
			limit = {
				tag = TUS
			}
			if = {
				limit = {
					has_completed_focus = TUS_focus_revivce_hopes_for_constitutional_monarchy
				}
				set_cosmetic_tag = TUSCM
			}
			if = {
				limit = {
					has_completed_focus = TUS_focus_revive_the_1848_goals
				}
				set_cosmetic_tag = TUSRR
			}
			
			#set_cosmetic_tag = PIE_focus_ITA   ## Tuscany Needs Two Different Cosmetic Tags, one for republican and one for radical
			set_country_flag = is_literally_italy
			####Country Events for Unification
			##Event about status of country
			
			###Remove puppets
			if = {
				limit = {
					NSC = {
						exists = yes
						is_subject_of = ROOT
					}
				}
				annex_country = {
					target = NSC
					transfer_troops = yes
				}
			}
			###core land
			every_owned_state = {
				if = {
					limit = {
						NOT = {
							is_core_of = TUS
						}
					}
					add_core_of = TUS
				}
				remove_core_of = NSC
				remove_core_of = LVN
				remove_core_of = PRM
				remove_core_of = MOD
				remove_core_of = TUS
				remove_core_of = PAP
				remove_core_of = FRA
				remove_core_of = CBI
			}
			#hidden_effect = {
			#	remove_state_core = 812
			#	remove_state_core = 117
			#	remove_state_core = 997
			#	remove_state_core = 156
			#	remove_state_core = 115
			#	remove_state_core = 1380
			#}
			####Focus Tree
			#load_focus_tree = italy_focus
			###Politics and related things
			hidden_effect = {
				FRA = {
					mark_focus_tree_layout_dirty = yes
				}
				#set_popularities = {
				#	anarchism = 8
				#	ideology_social_liberalism = 1
				#	ideology_radical_socialism = 1
				#	social_egalitarianism = 1
				#	centrism = 52
				#	traditional_conservatism = 25
				#	radical_democracy = 2
				#	chauvinist_populism = 0
				#	autocracy = 10
				#	fundamentalism = 0
				#}
				#set_politics = {
				#	ruling_party = centrism
				#}
				add_ideas = {
					ITA_Unorganized_Army
					ITA_Unorganized_Navy
					ITA_Brigandage
					ITA_Different_Naval_Doctorines
					ITA_Reliance_on_Farming
				}
				LVN = {
					remove_ideas = {
						LVN_National_Society_1
						LVN_National_Society_2
					}
				}
				#activate_mission = ITA_decision_deal_with_brigindage
			}
		}
	}
}

RUS_PIE_Galicia_Conference_Pass = {
	hidden_effect = {
		RUS = {
			if = {
				limit = {
					### West Galicia + Krakow
					has_country_flag = RUS_GAL_WEST_BORDER_STATE_KRAKOW
					NOT = {
						has_country_flag = RUS_GAL_WEST_BORDER_STATE_KRAKOW2
					}
				}
				### West Galicia + Krakow
				transfer_state = 88
				transfer_state = 1321
				add_state_core = 88
				add_state_core = 1321
			}
			if = {
				limit = {
					has_country_flag = RUS_GAL_EAST_BORDER_STATE_KRAKOW
					NOT = {
						has_country_flag = RUS_GAL_EAST_BORDER_STATE_KRAKOW2
					}
				}
				### East Galicia + Krakow
				transfer_state = 1321
				add_state_core = 1321
				transfer_state = 91
				add_state_core = 91
			}
			if = {
				limit = {
					### West Galicia
					has_country_flag = RUS_GAL_WEST_BORDER_STATE
					NOT = {
						has_country_flag = RUS_GAL_WEST_BORDER_STATE2
					}
				}
				### West Galicia
				transfer_state = 88
				add_state_core = 88
				AUS = {
					country_event = {
						id = risorgimento.222
					}
				}
			}
			if = {
				limit = {
					### East Galicia
					has_country_flag = RUS_GAL_EAST_BORDER_STATE
					NOT = {
						has_country_flag = RUS_GAL_EAST_BORDER_STATE2
					}
				}
				### East Galicia
				transfer_state = 91
				add_state_core = 91
			}
			if = {
				limit = {
					### Full Annexation
					has_country_flag = RUS_GAL_FULL_ANNEXATION
					NOT = {
						has_country_flag = RUS_GAL_FULL_ANNEXATION2
					}
				}
				### Full Annexation
				transfer_state = 91
				add_state_core = 91
				transfer_state = 88
				add_state_core = 88
				transfer_state = 89
				add_state_core = 89
				transfer_state = 1321
				add_state_core = 1321
			}
			if = {
				limit = {
					### Autonomous
					has_country_flag = RUS_GAL_Autonomous
					NOT = {
						has_country_flag = RUS_GAL_Autonomous2
					}
				}
				### Autonomous 
			}
			#### Clear Country Flags
		}
	}
}

#Country scope
ROOT_inherit_current_scope_wars_effect = {
	custom_effect_tooltip = ROOT_inherit_current_scope_wars_effect
	hidden_effect = {
		every_country = {
			limit = {
				has_defensive_war_with = PREV
			}
			ROOT = {
				declare_war_on = {
					target = PREV
					type = annex_everything
				}
			}
		}
		every_country = {
			limit = {
				has_offensive_war_with = PREV
			}
			declare_war_on = {
				target = ROOT
				type = annex_everything
			}
		}
	}
}

apply_ruling_party_popularity_change = {
	if = {
		limit = { has_government = centrism }
		add_popularity = { ideology = centrism popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = traditional_conservatism }
		add_popularity = { ideology = traditional_conservatism popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = social_egalitarianism }
		add_popularity = { ideology = social_egalitarianism popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = ideology_radical_socialism }
		add_popularity = { ideology = ideology_radical_socialism popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = ideology_social_liberalism }
		add_popularity = { ideology = ideology_social_liberalism popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = autocracy }
		add_popularity = { ideology = autocracy popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = radical_democracy }
		add_popularity = { ideology = radical_democracy popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = chauvinist_populism }
		add_popularity = { ideology = chauvinist_populism popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = anarchism }
		add_popularity = { ideology = anarchism popularity = popularity_change_value }
	}
	else_if = {
		limit = { has_government = fundamentalism }
		add_popularity = { ideology = fundamentalism popularity = popularity_change_value }
	}
}

# Negative popularity changes
ruling_party_lose_1_popularity = {
	set_variable = { popularity_change_value = -0.01 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_lose_2_popularity = {
	set_variable = { popularity_change_value = -0.02 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_lose_3_popularity = {
	set_variable = { popularity_change_value = -0.03 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_lose_4_popularity = {
	set_variable = { popularity_change_value = -0.04 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_lose_5_popularity = {
	set_variable = { popularity_change_value = -0.05 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_lose_10_popularity = {
	set_variable = { popularity_change_value = -0.1 }
	apply_ruling_party_popularity_change = yes
}

# Positive popularity changes
ruling_party_gain_1_popularity = {
	set_variable = { popularity_change_value = 0.01 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_gain_2_popularity = {
	set_variable = { popularity_change_value = 0.02 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_gain_3_popularity = {
	set_variable = { popularity_change_value = 0.03 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_gain_4_popularity = {
	set_variable = { popularity_change_value = 0.04 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_gain_5_popularity = {
	set_variable = { popularity_change_value = 0.05 }
	apply_ruling_party_popularity_change = yes
}

ruling_party_gain_10_popularity = {
	set_variable = { popularity_change_value = 0.1 }
	apply_ruling_party_popularity_change = yes
}

clear_sabotaged_resources_if_necesary = {
	if = {
		limit = {
			not = {
				has_dynamic_modifier = {
					modifier = sabotaged_resources
				}
			}
		}
		clear_variable = sabotaged_oil
		clear_variable = sabotaged_aluminium
		clear_variable = sabotaged_rubber
		clear_variable = sabotaged_tungsten
		clear_variable = sabotaged_steel
		clear_variable = sabotaged_chromium
	}
}

#expectes a temp variable country_to_initiate which should contain original_tag to instantiate as a collabration government
instantiate_collaboration_government = {
	custom_effect_tooltip = compliance_80_effect_tooltip
	hidden_effect = {
		set_variable = {
			collaboration_formed@var:country_to_initiate = 1
		}
		if = {
			# if country_to_initiate does not exist, simply release it as a puppet
			limit = {
				var:country_to_initiate = {
					exists = no
				}
			}
			var:country_to_initiate = {
				set_variable = {
					collaboration_formed_by = PREV
				}
			}
			release_autonomy = {
				target = country_to_initiate
				autonomy_state = autonomy_collaboration_government
				freedom_level = 0.5
			}
		}
		else = {
			# if country_to_initiate exists, create a dynamic country as our new puppet
			create_dynamic_country = {
				original_tag = country_to_initiate
				set_variable = {
					collaboration_formed_by = PREV
				}
				set_temp_variable = {
					new_country = this
				}
				PREV = {
					every_controlled_state = {
						limit = {
							occupied_country_tag = country_to_initiate
						}
						var:new_country = {
							transfer_state = PREV
						}
					}
					puppet = new_country
					set_autonomy = {
						target = new_country
						autonomy_state = autonomy_collaboration_government
						freedom_level = 0.5
					}
				}
			}
		}
	}
}

abandon_colony_tag = {
	if = {
		limit = {
			is_subject = yes
		}
		if = {
			limit = {
				original_tag = CAN
			}
			else = {
				set_cosmetic_tag = CAN_UK
			}
		}
		else_if = {
			limit = {
				original_tag = RAJ
			}
			set_cosmetic_tag = RAJ_UK
		}
		else_if = {
			limit = {
				original_tag = MAL
			}
			set_cosmetic_tag = MAL_UK
		}
		else_if = {
			limit = {
				original_tag = INS
			}
			set_cosmetic_tag = INS_HOL
		}
	}
	else = {
		if = {
			limit = {
				original_tag = RAJ
				NOT = {
					any_country = {
						has_cosmetic_tag = RAJ_FREE
					}
				}
			}
			set_cosmetic_tag = RAJ_FREE
		}
		else_if = {
			limit = {
				original_tag = CAN
				NOT = {
					any_country = {
						has_cosmetic_tag = CAN_FREE
					}
				}
			}
			set_cosmetic_tag = CAN_FREE
		}
		else_if = {
			limit = {
				original_tag = INS
				NOT = {
					has_cosmetic_tag = MAJ_UNIFIED
				}
				NOT = {
					any_country = {
						has_cosmetic_tag = INS_FREE
					}
				}
			}
			set_cosmetic_tag = INS_FREE
		}
		else_if = {
			limit = {
				OR = {
					has_cosmetic_tag = united_polynesia
					has_cosmetic_tag = MAJ_UNIFIED
				}
			}
		}
		else = {
			drop_cosmetic_tag = yes
		}
	}
}

print_variable_log_effect = {
	print_variables = {
		file = variables_yug
		append = yes
	}
}
