###For industries going bankrupt, relocating &c.### 

##Remember to Set Reduction Factor!!##
##Reduction factor is the percentage to remove, so for 10% reduction factor is 0.1
##This doesnt produce a tooltip so a scripted one is used.
##Tooltip: "Lose <reduction_factor>% of civilian factories."

remove_civilian_industry_effect = {
	#log = "ecovent100 eff: Lose §R[?reduction_factor|%0]§! of civilian factories."
	#log = "Ind: [?reduction_factor] [?reduction_factor|%0]"
	custom_effect_tooltip = remove_civilian_industry_effect_tt
	hidden_effect = {
		set_variable = { ind_factories = num_of_civilian_factories }
		log = "Removing Industry, Factories: [?ind_factories], reduction factor: [?reduction_factor]"
		multiply_variable = {
			var = ind_factories
			value = reduction_factor
		}
		set_variable = { iterations = 0 }
		civ_factory_remove_loop_effect = yes
	}
}

##HoI doesn't have while = { } loops so this'll do for now.##
##Must set iterations before calling this effect.
civ_factory_remove_loop_effect = {
	hidden_effect = {
		log = "Starting Removal Loop"
		if = {
			log = "Removal Loop. Iteration: [?iterations]"
			limit = {
				check_variable = {
					var = iterations
					value = ind_factories
					compare = less_than
				}
			}
			random_owned_state = {
				remove_building = {
					type = industrial_complex
					level = 1
				}
				log = "Removing industry in [This.GetName], [Root.GetName]"
			}
			add_to_variable = {
				var = iterations
				value = 1
			}
			civ_factory_remove_loop_effect = yes
		}
	}
}

###Industry Relocates###
##Tooltip: "<reduction_factor>% of civilian factories move to <relocation_location>."
migrate_industry_to_random_effect = {
	hidden_effect = {
		random_country = {
			limit = {
				NOT = {
					OR = {
						has_idea = depression
						has_idea = recession
						has_idea = stagnation
					}
				}
				has_war = no
				#num_of_controlled_states > ROOT #Should stop one-state country getting 50 factories. #Doesn't appear to work with scope reference
			}
			save_event_target_as = relocation_location
		}
	}
	custom_effect_tooltip = migrate_industry_to_random_effect_tt
	hidden_effect = {
		set_variable = { ind_factories = num_of_civilian_factories }
		log = "Relocating Industry, Factories: [?ind_factories], reduction factor: [?reduction_factor]"
		multiply_variable = {
			var = ind_factories
			value = reduction_factor
		}
		set_variable = { iterations = 0 }
		civ_factory_remove_loop_effect = yes
		event_target:relocation_location = {
			country_event = {
				id = econvent.102
				days = 1
				random = 24
			}
		}
	}
}

# Remember to save_event_target_as = relocation_location before effect
migrate_industry_to_relocation_location_effect = {
	custom_effect_tooltip = migrate_industry_to_random_effect_tt
	hidden_effect = {
		set_variable = { ind_factories = num_of_civilian_factories }
		log = "Relocating Industry, Factories: [?ind_factories], reduction factor: [?reduction_factor]"
		multiply_variable = {
			var = ind_factories
			value = reduction_factor
		}
		set_variable = { iterations = 0 }
		civ_factory_remove_loop_effect = yes
		event_target:relocation_location = {
			country_event = {
				id = econvent.102
				days = 1
				random = 24
			}
		}
	}
}

##HoI doesn't have while = { } loops so this'll do for now.##
##Must set iterations before calling this effect.
civ_factory_relocate_from_from_build_loop_effect = {
	hidden_effect = {
		log = "Starting Construction Loop"
		if = {
			log = "Construction Loop. Iteration: [?From.iterations]"
			limit = {
				FROM = { #To get ind_factories var
					check_variable = {
						var = iterations
						value = ind_factories
						compare = less_than
					}
				}
			}
			random_owned_state = {
				if = {
					limit = {
						free_building_slots = {
							building = industrial_complex
							size > 1
							include_locked = no
						}
					}
					add_building_construction = {
						type = industrial_complex
						level = 1
						instant_build = yes
					}
				}
				else = {
					add_extra_state_shared_building_slots = 1
					add_building_construction = {
						type = industrial_complex
						level = 1
						instant_build = yes
					}
				}
				log = "Constructing industry in [This.GetName], [Root.GetName]"
			}
			FROM = {
				add_to_variable = {
					var = iterations
					value = 1
				}
			}
			civ_factory_relocate_from_from_build_loop_effect = yes
		}
	}
}


### update state on init

2017_state_init_setup = {
	every_owned_state = {
		if = {
			limit = { 
				has_state_category = state_00
				state_population_k > 300
			}
			set_state_category = state_01
		}
		if = {
			limit = {
				has_state_category = state_01
				state_population_k > 800
			}
			set_state_category = state_02
		}
		if = {
			limit = {
				has_state_category = state_02
				state_population_k > 1500
			}
			set_state_category = state_03
		}
		if = {
			limit = {
				has_state_category = state_03
				state_population_k > 2500
			}
			set_state_category = state_04
		}
		if = {
			limit = {
				has_state_category = state_04
				state_population_k > 3800
			}
			set_state_category = state_05
		}
		if = {
			limit = {
				has_state_category = state_05
				state_population_k > 5200
			}
			set_state_category = state_06
		}
		if = {
			limit = {
				has_state_category = state_06
				state_population_k > 6800
			}
			set_state_category = state_07
		}
		if = {
			limit = {
				has_state_category = state_07
				state_population_k > 8500
			}
			set_state_category = state_08
		}
		if = {
			limit = {
				has_state_category = state_08
				state_population_k > 10500
			}
			set_state_category = state_09
		}
		if = {
			limit = {
				has_state_category = state_09
				state_population_k > 13000
			}
			set_state_category = state_10
		}
		if = {
			limit = {
				has_state_category = state_10
				state_population_k > 16000
			}
			set_state_category = state_11
		}
		if = {
			limit = {
				has_state_category = state_11
				state_population_k > 19000
			}
			set_state_category = state_12
		}
		if = {
			limit = {
				has_state_category = state_12
				state_population_k > 23000
			}
			set_state_category = state_13
		}
		if = {
			limit = {
				has_state_category = state_13
				state_population_k > 27000
			}
			set_state_category = state_14
		}
		if = {
			limit = {
				has_state_category = state_14
				state_population_k > 34000
			}
			set_state_category = state_15
		}
		if = {
			limit = {
				has_state_category = state_15
				state_population_k > 42000
			}
			set_state_category = state_16
		}
		if = {
			limit = {
				has_state_category = state_16
				state_population_k > 52000
			}
			set_state_category = state_17
		}
		if = {
			limit = {
				has_state_category = state_17
				state_population_k > 65000
			}
			set_state_category = state_18
		}
		log = "[GetDateText]: [This.GetName]: state upgrades executed"
	}
}
