@ECO_trade_scaled_weight  = 0.32
@ECO_trade_penalty_weight = 0.16
# Gold Grams to Currency ratio, USD = 31.1035
@currency_conversion_factor = 31.1035

calculate_GDP = {
	set_variable = { country_GDP = 0 }
	set_variable = { country_resource_GDP = 0 }
	set_temp_variable = { temp_civ = 0 }
	set_temp_variable = { temp_mil = 0 }
	set_temp_variable = { temp_doc = num_of_naval_factories }

	set_temp_variable = { actual_core_manpower_k = 0 }
	set_temp_variable = { actual_noncore_manpower_k = 0 }
	clr_country_flag = gdp_menu

	# -----------------------
	# CATEGORY FACTOR SECTION
	# -----------------------
	every_country = {
		set_variable = { THIS.country_category_factor = 0 }
		set_variable = { THIS.country_num_states = THIS.num_core_states }
	}

	every_state = {
		IF = {
			limit = { is_owned_by = PREV }
			add_to_temp_variable = { actual_manpower_k = state_population_k }
			IF = {
				limit = { is_core_of = PREV }
				add_to_temp_variable = { PREV.actual_core_manpower_k = state_population_k }
			}
			ELSE = {
				add_to_temp_variable = { PREV.actual_noncore_manpower_k = state_population_k }
			}
		}
		IF = {
			limit = { is_core_of = PREV }
			set_variable = { state_category_factor = 1 }
			IF = { limit = { is_megalopolis_state = yes         } multiply_variable = { state_category_factor = 2.00 } }
			ELSE_IF = { limit = { is_metropolis_state = yes          } multiply_variable = { state_category_factor = 1.75 } }
			ELSE_IF = { limit = { is_large_city_state = yes          } multiply_variable = { state_category_factor = 1.50 } }
			ELSE_IF = { limit = { is_city_state = yes                } multiply_variable = { state_category_factor = 1.25 } }
			ELSE_IF = { limit = { is_large_town_state = yes          } multiply_variable = { state_category_factor = 1.00 } }
			ELSE_IF = { limit = { is_town_state = yes                } multiply_variable = { state_category_factor = 0.85 } }
			ELSE_IF = { limit = { is_rural_state = yes               } multiply_variable = { state_category_factor = 0.40 } }
			ELSE    = { multiply_variable = { state_category_factor = 0.20 } }
			OWNER   = { add_to_variable = { THIS.country_category_factor = Prev.state_category_factor } }
		}
	}

	IF = {
		LIMIT = { THIS = { tag = MRC } }
		log = "[THIS.GetTag] actual_core_manpower_k = [?actual_core_manpower_k]"
		log = "[THIS.GetTag] actual_noncore_manpower_k = [?actual_noncore_manpower_k]"
	}

	every_country = {
		clamp_variable = { var = THIS.country_num_states min = 1 } # Guard: no core/owned states edge case
		divide_variable = { THIS.country_category_factor = THIS.country_num_states }
		subtract_from_variable = { country_category_factor = 1 }
		divide_variable = { country_category_factor = 10 }
		set_variable = { country_category_factor_1 = 1 }
		add_to_variable = { country_category_factor_1 = country_category_factor }

		# -----------------------
		# INFRASTRUCTURE + CIV RESET (single pass)
		# -----------------------
		set_variable = { THIS.country_average_inf = 0 }
		set_variable = { THIS.country_num_states = THIS.num_owned_states }
		set_variable = { THIS.country_civ = 0 }
		set_variable = { THIS.country_civ_own = 0 }
		set_variable = { THIS.country_civ_core = 0 }
		set_variable = { THIS.country_agriculture_weight = 0 }
	}

	set_variable = { temp_8 = country_category_factor_1 }

	# -----------------------
	# STATE ACCUMULATIONS
	# -----------------------
	every_state = {
		# FAILSAFE: states with near-zero population (e.g. 1 person) produce 0 GDP
		# state_population_k is in thousands — 0.002 = 2 people minimum threshold
		if = {
			limit = { check_variable = { THIS.state_population_k < 0.002 } }
			set_variable = { state_GDP = 0 }
			set_variable = { state_GDP_per_capita = 0 }
			set_variable = { state_productivity_weight = 0 }
			set_variable = { state_labour_ratio = 0 }
		}
		else = {

		# STATE INF
		set_variable = { state_inf = 0 }
		add_to_variable = { state_inf = THIS.non_damaged_building_level@infrastructure }
		OWNER = { add_to_variable = { THIS.country_average_inf = Prev.state_inf } }

		# FACTORY PRODUCTIVITY WEIGHT
		# city = 1.00 baseline. Above: agglomeration bonus (capped ~2.40 at inf5). Below: harsh penalty.
		# inf_factor: base 0.80, +0.08 per level → inf0=0.80, inf5=1.20
		set_variable = { state_productivity_weight = 1.00 }
		IF = { limit = { is_megalopolis_state = yes  } set_variable = { state_productivity_weight = 2.25 } }
		ELSE_IF = { limit = { is_metropolis_state = yes   } set_variable = { state_productivity_weight = 1.95 } }
		ELSE_IF = { limit = { is_large_city_state = yes   } set_variable = { state_productivity_weight = 1.55 } }
		ELSE_IF = { limit = { is_city_state = yes         } set_variable = { state_productivity_weight = 1.25 } }
		ELSE_IF = { limit = { is_large_town_state = yes   } set_variable = { state_productivity_weight = 0.85 } }
		ELSE_IF = { limit = { is_town_state = yes         } set_variable = { state_productivity_weight = 0.55 } }
		ELSE_IF = { limit = { is_rural_state = yes        } set_variable = { state_productivity_weight = 0.30 } }
		ELSE    = { set_variable = { state_productivity_weight = 0.05 } }

		# Factory infrastructure factor: base 0.80, +0.08 per level (inf0=0.80, inf5=1.20)
		set_variable = { state_inf_factor = 0.80 }
		set_temp_variable = { temp_inf_bonus = THIS.non_damaged_building_level@infrastructure }
		multiply_temp_variable = { temp_inf_bonus = 0.08 }
		add_to_variable = { state_inf_factor = temp_inf_bonus }

		# Final factory productivity = category * inf
		multiply_variable = { state_productivity_weight = state_inf_factor }

		# -----------------------------------------------------------------------
		# POPULATION/FACTORY LABOUR RATIO
		# Labour supply is the binding constraint on industrial output.
		# One factory complex requires FACTORY_POP_REQUIREMENT thousand people
		# to operate at full capacity. Below that, output scales down.
		# Historical basis: ~400k people per factory (WWII industrial calibration)
		# TUNING: change the 0.4 value on the multiply line below (= 400k per factory)
		# FLOOR:  0.0001 = 0.01% minimum — even Petsamo contributes something
		# -----------------------------------------------------------------------
		set_temp_variable = { state_total_factories = THIS.non_damaged_building_level@industrial_complex }
		add_to_temp_variable = { state_total_factories = THIS.non_damaged_building_level@arms_factory }
		add_to_temp_variable = { state_total_factories = THIS.non_damaged_building_level@dockyard }
		clamp_temp_variable = { var = state_total_factories min = 0.0001 }
		set_temp_variable = { factory_pop_req = state_total_factories }
		multiply_temp_variable = { factory_pop_req = 300 } # TUNING: 300 = 300 people per factory (state_population_k is in thousands)
		set_temp_variable = { state_pop_factory_ratio = THIS.state_population_k }
		divide_temp_variable = { state_pop_factory_ratio = factory_pop_req }
		clamp_temp_variable = { var = state_pop_factory_ratio min = 0.0001 max = 1.0 }
		set_variable = { state_labour_ratio = state_pop_factory_ratio } # stored as state var for reuse

		# AGRICULTURE PRODUCTIVITY WEIGHT (bell curve)
		# Peaks at large_town: organized farming with market surplus
		# Falls below (subsistence) and above (urbanisation eats farmland)
		# inf_factor: base 0.90, +0.04 per level → inf0=0.90, inf5=1.10 (gentler — roads matter but less than for industry)
		set_variable = { state_agri_weight = 1.00 }
		IF = { limit = { is_megalopolis_state = yes  } set_variable = { state_agri_weight = 0.05 } }
		ELSE_IF = { limit = { is_metropolis_state = yes   } set_variable = { state_agri_weight = 0.20 } }
		ELSE_IF = { limit = { is_large_city_state = yes   } set_variable = { state_agri_weight = 0.45 } }
		ELSE_IF = { limit = { is_city_state = yes         } set_variable = { state_agri_weight = 0.75 } }
		ELSE_IF = { limit = { is_large_town_state = yes   } set_variable = { state_agri_weight = 1.10 } }
		ELSE_IF = { limit = { is_town_state = yes         } set_variable = { state_agri_weight = 1.05 } }
		ELSE_IF = { limit = { is_rural_state = yes        } set_variable = { state_agri_weight = 0.75 } }
		ELSE    = { set_variable = { state_agri_weight = 0.30 } }

		# Agriculture infrastructure factor: base 0.90, +0.04 per level (inf0=0.90, inf5=1.10)
		set_variable = { state_agri_inf_factor = 0.90 }
		set_temp_variable = { temp_agri_inf_bonus = THIS.non_damaged_building_level@infrastructure }
		multiply_temp_variable = { temp_agri_inf_bonus = 0.04 }
		add_to_variable = { state_agri_inf_factor = temp_agri_inf_bonus }

		# Final agriculture weight = category * inf
		multiply_variable = { state_agri_weight = state_agri_inf_factor }

		# Accumulate weighted population into owner for agriculture calculation
		set_temp_variable = { temp_state_pop = THIS.state_population_k }
		multiply_temp_variable = { temp_state_pop = state_agri_weight }
		OWNER = { add_to_variable = { THIS.country_agriculture_weight = temp_state_pop } }

		# STATE CIV (weighted × labour ratio)
		set_variable = { state_civ = 0 }
		add_to_variable = { state_civ = THIS.non_damaged_building_level@industrial_complex }
		multiply_variable = { state_civ = state_productivity_weight }
		multiply_variable = { state_civ = state_labour_ratio }
		controller = { add_to_variable = { THIS.country_civ = Prev.state_civ } }

		# STATE CIV OWN (weighted × labour ratio)
		set_variable = { state_civ_own = 0 }
		add_to_variable = { state_civ_own = THIS.non_damaged_building_level@industrial_complex }
		multiply_variable = { state_civ_own = state_productivity_weight }
		multiply_variable = { state_civ_own = state_labour_ratio }
		OWNER = { add_to_variable = { THIS.country_civ_own = Prev.state_civ_own } }

		IF = {
			limit = { is_core_of = PREV }
			set_variable = { state_civ_core = 0 }
			add_to_variable = { state_civ_core = THIS.non_damaged_building_level@industrial_complex }
			multiply_variable = { state_civ_core = state_productivity_weight }
			multiply_variable = { state_civ_core = state_labour_ratio }
			OWNER = { add_to_variable = { THIS.country_civ_core = Prev.state_civ_core } }
		}
		} # end else (population > 0.002 gate)
	}

	# -----------------------
	# FINAL NORMALIZATION
	# -----------------------
	every_country = {
		clamp_variable = { var = THIS.country_num_states min = 1 } # Guard: no owned states edge case
		divide_variable = { THIS.country_average_inf = THIS.country_num_states }
		# -----------------------
		# RESET ALL ARMS VARIABLES
		# -----------------------
		set_variable = { THIS.country_arms      = 0 }
		set_variable = { THIS.country_arms_own  = 0 }
		set_variable = { THIS.country_arms_core = 0 }
	}

	set_variable = { country_civ_con = 0 }
	add_to_variable = { country_civ_con = country_civ }

	set_variable = { country_civ_ocp_temp = country_civ }
	set_variable = { country_civ_coreown_temp = country_civ_core }
	set_variable = { country_civ_noncore_temp = country_civ_own }
	set_variable = { country_civ_core_temp = country_civ_core }
	subtract_from_variable = { country_civ_ocp_temp = country_civ_own }
	subtract_from_variable = { country_civ_noncore_temp  = country_civ_core_temp }
	subtract_from_variable = { country_civ_con  = country_civ_noncore_temp }

	subtract_from_variable = { country_civ_ocp_temp = country_civ_noncore_temp }
	clamp_variable = { var = country_civ_ocp_temp min = 0 }

	set_temp_variable = { temp_9 = country_civ_ocp_temp }

	subtract_from_variable = { country_civ_con = temp_9 }

	set_temp_variable = { temp_civ_con = country_civ_con }
	add_to_temp_variable = { temp_civ = temp_civ_con }



	# Construction
		set_temp_variable = { var = construction_modifier
		value = modifier@production_speed_buildings_factor }
		clamp_temp_variable = { var = construction_modifier min = -1 }

		set_temp_variable = { construction_bonus = 1.0 }
		add_to_temp_variable = { construction_bonus = construction_modifier }

		set_temp_variable = { temp_civ_bonus = temp_civ }
		set_temp_variable = { temp_civ_dif = temp_civ }
		multiply_temp_variable = { temp_civ_bonus = construction_bonus }
		subtract_from_variable = { temp_civ_bonus = temp_civ_dif }
		add_to_temp_variable = { temp_civ = temp_civ_bonus }
	# Construction

	# Inflation Impact on Civilian Value
		set_temp_variable = { temp_inflation_factor = global.ECO_inflation }
		divide_temp_variable = { temp_inflation_factor = 100 }
		add_to_temp_variable = { temp_inflation_factor = 1 }

		set_temp_variable = { real_value_mult = 1.0 }
		divide_temp_variable = { real_value_mult = temp_inflation_factor }

		multiply_temp_variable = { temp_civ = real_value_mult }
	# Inflation Impact on Civilian Value end



	add_to_variable = { country_GDP = temp_civ } # Initial civilian factories are included in GDP calculations

	set_variable = { temp_civ_all = num_of_civilian_factories }
	subtract_from_variable = { temp_civ_all = country_civ }
	clamp_variable = { var = temp_civ_all min = 0}

	set_temp_variable = { temp_2 = temp_civ_all }
	divide_temp_variable = { temp_2 = 1 }
	add_to_temp_variable = { temp_civ = temp_2 }

	set_temp_variable = { temp_4 = country_civ_ocp_temp }
	divide_temp_variable = { temp_4 = 10 }

	set_temp_variable = { temp_6 = country_civ_noncore_temp }
	divide_temp_variable = { temp_6 = 3.0 }

	add_to_temp_variable = { temp_civ = temp_4 } 
	add_to_temp_variable = { temp_civ = temp_6 }

	# -----------------------
	# STATE ACCUMULATIONS (general + owned)
	# -----------------------
	every_state = {
		# Arms factories use same state_productivity_weight computed in the civ loop above
		# state_productivity_weight is already set as a state variable at this point

		# Arms factories (controller, weighted × labour ratio)
		set_variable = { state_arms = 0 }
		add_to_variable = { state_arms = THIS.non_damaged_building_level@arms_factory }
		multiply_variable = { state_arms = state_productivity_weight }
		multiply_variable = { state_arms = state_labour_ratio }
		controller = { add_to_variable = { THIS.country_arms = Prev.state_arms } }

		# Arms factories (owner, weighted × labour ratio)
		set_variable = { state_arms_own = 0 }
		add_to_variable = { state_arms_own = THIS.non_damaged_building_level@arms_factory }
		multiply_variable = { state_arms_own = state_productivity_weight }
		multiply_variable = { state_arms_own = state_labour_ratio }
		OWNER = { add_to_variable = { THIS.country_arms_own = Prev.state_arms_own } }

		# -----------------------
		# STATE ACCUMULATION (core states only)
		# -----------------------
		IF = {
			limit = { is_core_of = PREV }
			set_variable = { state_arms_core = 0 }
			add_to_variable = { state_arms_core = THIS.non_damaged_building_level@arms_factory }
			multiply_variable = { state_arms_core = state_productivity_weight }
			multiply_variable = { state_arms_core = state_labour_ratio }
			OWNER = { add_to_variable = { THIS.country_arms_core = Prev.state_arms_core } }
		}
	}

	set_variable = { country_arms_con = 0 }
	add_to_variable = { country_arms_con = country_arms }

	set_variable = { country_arms_ocp_temp = country_arms }
	set_variable = { country_arms_coreown_temp = country_arms_core }
	set_variable = { country_arms_noncore_temp = country_arms_own }
	set_variable = { country_arms_core_temp = country_arms_core }

	subtract_from_variable = { country_arms_ocp_temp = country_arms_coreown_temp }

	subtract_from_variable = { country_arms_noncore_temp  = country_arms_core_temp }

	subtract_from_variable = { country_arms_con = country_arms_noncore_temp }

	subtract_from_variable = { country_arms_ocp_temp = country_arms_noncore_temp }

	clamp_variable = { var = country_arms_ocp_temp min = 0}
	set_temp_variable = { temp_10 = country_civ_ocp_temp }
	
	subtract_from_variable = { country_arms_con = temp_10 }

	set_temp_variable = { temp_arms_con = country_arms_con }
	add_to_temp_variable = { temp_mil = temp_arms_con }

	set_temp_variable = { temp_5 = country_arms_ocp_temp }
	divide_temp_variable = { temp_5 = 20 }

	set_temp_variable = { temp_7 = country_arms_noncore_temp }
	divide_temp_variable = { temp_7 = 5.0 }

	add_to_temp_variable = { temp_mil = temp_5 } #

	add_to_temp_variable = { temp_mil = temp_7 } #

	set_variable = { overlord_factor = 1 }
	set_variable = { overlord_factor_2 = modifier@cic_to_overlord_factor }
	multiply_variable = { overlord_factor_2 = 0.99 }
	subtract_from_variable = { overlord_factor = overlord_factor_2 }
	multiply_variable = { temp_civ = overlord_factor }

	set_variable = { m_overlord_factor = 1 }
	set_variable = { m_overlord_factor_2 = modifier@mic_to_overlord_factor }
	multiply_variable = { m_overlord_factor_2 = 0.99 }
	subtract_from_variable = { m_overlord_factor = m_overlord_factor_2 }
	multiply_variable = { temp_mil = m_overlord_factor }



	# Trade
		set_temp_variable = { var = trade_modifier value = modifier@min_export }
		clamp_temp_variable = { var = trade_modifier min = 0 max = 1 }

		set_temp_variable = { scaled_modifier = @ECO_trade_scaled_weight }
		multiply_temp_variable = { scaled_modifier = trade_modifier }

		set_temp_variable = { penalty_term = @ECO_trade_penalty_weight }
		multiply_temp_variable = { penalty_term = trade_modifier }
		multiply_temp_variable = { penalty_term = trade_modifier }

		set_temp_variable = { final_trade_modifier = scaled_modifier }
		subtract_from_temp_variable = { final_trade_modifier = penalty_term }

		set_temp_variable = { trade_bonus = 1.0 }
		add_to_temp_variable = { trade_bonus = final_trade_modifier }

		multiply_temp_variable = { temp_civ = trade_bonus }
		multiply_temp_variable = { temp_mil = trade_bonus }
		multiply_temp_variable = { temp_doc = trade_bonus }
	# Trade End



	# Conscription
		set_temp_variable = { conscription_modifier = modifier@conscription }
		set_temp_variable = { conscription_factor_modifier = modifier@conscription_factor }
		add_to_temp_variable = { conscription_factor_modifier = 1 }
		multiply_temp_variable = { conscription_modifier = conscription_factor_modifier }
		clamp_temp_variable = { var = conscription_modifier min = 0 max = 1 }

		set_temp_variable = { civ_conscript_penalty = 0.5 }
		multiply_temp_variable = { civ_conscript_penalty = conscription_modifier }
		set_temp_variable = { mil_conscript_penalty = 0.2 }
		multiply_temp_variable = { mil_conscript_penalty = conscription_modifier }

		set_temp_variable = { civ_multiplier = 1.0 }
		subtract_from_temp_variable = { civ_multiplier = civ_conscript_penalty }

		set_temp_variable = { mil_multiplier = 1.0 }
		subtract_from_temp_variable = { mil_multiplier = mil_conscript_penalty }

		multiply_temp_variable = { temp_civ = civ_multiplier }
		multiply_temp_variable = { temp_mil = mil_multiplier }
		multiply_temp_variable = { temp_doc = mil_multiplier }
	# Conscription End

	IF = {
		LIMIT = { THIS = { tag = MRC } }
		log = "[THIS.GetTag] trade_modifier = [?trade_modifier]"
		log = "[THIS.GetTag] conscription_modifier = [?conscription_modifier]"
	}



	set_temp_variable = { temp_0 = temp_mil }
	multiply_temp_variable = { temp_0 = 7.0 }

	set_temp_variable = { temp_1 = temp_doc }
	multiply_temp_variable = { temp_1 = 7.0 }

	add_to_variable = { country_GDP = temp_0 }
	add_to_variable = { country_GDP = temp_1 }

	set_variable = { population_modifier = 1 }
	set_variable = { population_modifier_civ = 1 }
	set_variable = { population_core_state = actual_core_manpower_k }
	set_variable = { population_core_state_1 = actual_core_manpower_k }

	set_variable = { population_modifier_1 = -1 }
	set_variable = { temp_nocore =  actual_noncore_manpower_k }
	divide_variable = { temp_nocore = 1.5 } #5M
	add_to_variable = { population_core_state = temp_nocore }

	divide_variable = { population_core_state = 3500 } #3.5M

	add_to_variable = { population_modifier_1 = population_core_state }

	if = {
		limit = {
			check_variable = { population_core_state > 0.999 }
		}
		divide_variable = { population_modifier_1 = 500 }
	}

	add_to_variable = { population_modifier = population_modifier_1 }
	divide_variable = { population_core_state_1 = 3500 }
	divide_variable = { population_core_state_1 = 100 }
	add_to_variable = { population_modifier_civ = population_core_state_1 }

	clamp_variable = { var = population_modifier min = 0  max = 1.25 }

	multiply_variable = { country_GDP = population_modifier }

	clamp_variable = { var = population_modifier_civ min = 0  max = 2.5 }
	multiply_temp_variable = { temp_civ = population_modifier_civ }

	# Infrastructure multiplier removed — infra is now baked into per-state factory productivity weights.
	# country_average_inf_1 kept at 1.0 (neutral) to avoid breaking any downstream references.
	set_variable = { country_average_inf_1 = 1 }

	# category_factor applied directly without population compounding
	# (population already applied once above at line 420)
	multiply_variable = { country_GDP = temp_8 }

	multiply_temp_variable = { temp_civ = 7.00 }
	multiply_temp_variable = { temp_mil = 4.00 }
	multiply_temp_variable = { temp_doc = 4.00 }

	set_temp_variable = { temp_civ_resource = 8 }
	set_temp_variable = { temp_civ_bonus = temp_civ }
	multiply_temp_variable = { temp_civ_bonus = 0.25 } 
	add_to_temp_variable = { temp_civ_resource = temp_civ_bonus }

	# Multipliers = [Physical Grams per Unit] / 1,000,000,000

	# Oil: 1,000,000 grams
	set_temp_variable = { res_val = resource_produced@oil }
	set_temp_variable = { fuel_gain_bonus = modifier@fuel_gain_factor }
	add_to_temp_variable = { fuel_gain_bonus = 1 }
	multiply_temp_variable = { res_val = fuel_gain_bonus }
	multiply_temp_variable = { res_val = 0.001 }
	add_to_variable = { country_resource_GDP = res_val }

	# Coal: 670,000 grams
	set_temp_variable = { res_val = resource_produced@coal }
	multiply_temp_variable = { res_val = 0.00067 }
	add_to_variable = { country_resource_GDP = res_val }

	# Steel: 70,000 grams
	set_temp_variable = { res_val = resource_produced@steel }
	multiply_temp_variable = { res_val = 0.00007 }
	add_to_variable = { country_resource_GDP = res_val }

	# Rubber: 10,000 grams
	set_temp_variable = { res_val = resource_produced@rubber }
	multiply_temp_variable = { res_val = 0.00001 }
	add_to_variable = { country_resource_GDP = res_val }

	# Chromium: 2,500 grams
	set_temp_variable = { res_val = resource_produced@chromium }
	multiply_temp_variable = { res_val = 0.0000025 }
	add_to_variable = { country_resource_GDP = res_val }

	# Aluminium: 500 grams
	set_temp_variable = { res_val = resource_produced@aluminium }
	multiply_temp_variable = { res_val = 0.0000005 }
	add_to_variable = { country_resource_GDP = res_val }

	# Tungsten: 150 grams
	set_temp_variable = { res_val = resource_produced@tungsten }
	multiply_temp_variable = { res_val = 0.00000015 }
	add_to_variable = { country_resource_GDP = res_val }
	
	# Inflation Impact on Resource Value
	
	set_temp_variable = { temp_inflation_factor = global.ECO_inflation }
	divide_temp_variable = { temp_inflation_factor = 100 }
	add_to_temp_variable = { temp_inflation_factor = 1 }

	set_temp_variable = { real_value_mult = 1.0 }
	divide_temp_variable = { real_value_mult = temp_inflation_factor }

	multiply_variable = { country_resource_GDP = real_value_mult }  # <-- was multiply_temp_variable
	
	# Inflation Impact on Resource Value end

	multiply_variable = { country_resource_GDP = temp_civ_resource }
	add_to_variable = { country_GDP = country_resource_GDP }



	clamp_temp_variable = { var = temp_civ min = 1 }



	set_variable = { factory_output = 1 }
	set_variable = { factory_output_2 = modifier@industrial_capacity_factory }
	multiply_variable = { factory_output_2 = 0.2 }
	add_to_variable = { factory_output = factory_output_2 }
	clamp_variable = { var = factory_output min = 0.5 max = 1.5 }
	multiply_variable = { country_GDP = factory_output }
	
	# Stability multiplier
	# Range: 0% stability = ×0.85, 100% stability = ×1.15
	# Politically stable nations produce more — instability tanks output.
	set_temp_variable = { stability_val = stability }
	multiply_temp_variable = { stability_val = 0.003 }
	set_temp_variable = { stability_gdp_factor = 0.85 }
	add_to_temp_variable = { stability_gdp_factor = stability_val }
	clamp_temp_variable = { var = stability_gdp_factor min = 0.70 max = 1.15 }
	multiply_variable = { country_GDP = stability_gdp_factor }
	# Stability multiplier end


	# Agriculture Labor (state-weighted bell curve)
		# Uses country_agriculture_weight accumulated per-state in the state loop above
		# Weight peaks at large_town (1.00), falls off toward wasteland and megalopolis
		set_temp_variable = { temp_ECO_efficiency = global.ECO_efficiency }
		divide_temp_variable = { temp_ECO_efficiency = 100 }
		add_to_temp_variable = { temp_ECO_efficiency = 2 } # Minor bump to maintain Agricultural GDP baseline

		# Convert weighted population (thousands) to millions, apply efficiency scalar
		set_variable = { country_agri_pop_m = country_agriculture_weight }
		divide_variable = { country_agri_pop_m = 1000 }
		multiply_variable = { country_agri_pop_m = temp_ECO_efficiency }
		
		# Deflate agricultural GDP by inflation — keeps real value consistent with industrial GDP
		set_temp_variable = { agri_inflation_factor = global.ECO_inflation }
		divide_temp_variable = { agri_inflation_factor = 100 }
		add_to_temp_variable = { agri_inflation_factor = 1 }
		divide_variable = { country_agri_pop_m = agri_inflation_factor }

		set_variable = { country_agriculture_GDP = country_agri_pop_m }
		add_to_variable = { country_GDP = country_agri_pop_m }
	# Agriculture Labor end



	divide_variable = { country_GDP = @currency_conversion_factor }
	divide_variable = { country_resource_GDP = @currency_conversion_factor }
	divide_variable = { country_agriculture_GDP = @currency_conversion_factor }

	set_variable = { percentage_of_GDP = country_GDP }
	multiply_variable = { percentage_of_GDP = 100 }
	if = {
		limit = { check_variable = { global.GDP_total > 0 } }
		divide_variable = { percentage_of_GDP = global.GDP_total }
	}
	clamp_variable = { var = percentage_of_GDP min = 0 }
	clamp_variable = { var = country_GDP min = 0 }

	set_variable = { country_GDP_per_capita = country_GDP }
	multiply_variable = { country_GDP_per_capita = 1000 }
	set_temp_variable = { actual_population_per_capita = actual_noncore_manpower_k }
	divide_temp_variable = { actual_population_per_capita = 3 }
	add_to_temp_variable = { actual_population_per_capita = actual_core_manpower_k }
	# FAILSAFE: if total population is near zero, skip division and set GDPPC to 0
	if = {
		limit = { check_variable = { actual_population_per_capita < 0.002 } }
		set_variable = { country_GDP_per_capita = 0 }
	}
	else = {
		divide_variable = { country_GDP_per_capita = actual_population_per_capita }
		multiply_variable = { country_GDP_per_capita = 1000 }
	}
	clamp_variable = { var = country_GDP_per_capita min = 0 }

	# -----------------------
	# STATE GDP COMPUTATION
	# -----------------------
	calculate_state_GDP = yes

	set_variable = { country_GDP_B = country_GDP }
	set_variable = { country_GDP_M = country_GDP }
	set_variable = { country_GDP_T = country_GDP }
	multiply_variable = { country_GDP_M = 1000 }
	multiply_variable = { country_GDP_T = 0.001 }

	set_variable = { country_resource_GDP_B = country_resource_GDP }
	set_variable = { country_resource_GDP_M = country_resource_GDP }
	set_variable = { country_resource_GDP_T = country_resource_GDP }
	multiply_variable = { country_resource_GDP_M = 1000 }
	multiply_variable = { country_resource_GDP_T = 0.001 }

	set_variable = { country_agriculture_GDP_B = country_agriculture_GDP }
	set_variable = { country_agriculture_GDP_M = country_agriculture_GDP }
	set_variable = { country_agriculture_GDP_T = country_agriculture_GDP }
	multiply_variable = { country_agriculture_GDP_M = 1000 }
	multiply_variable = { country_agriculture_GDP_T = 0.001 }
}


calculate_state_GDP = {
	# Reset global max trackers each run
	set_variable = { global.max_state_GDP = 0 }
	set_variable = { global.max_state_GDP_per_capita = 0 }

	# -----------------------------------------------------------------------
	# STATE GDP & STATE GDPPC — Informational / Map Mode
	# -----------------------------------------------------------------------
	# Computes a per-state GDP figure that reflects each state's productive
	# contribution. Country-level policy modifiers (trade, conscription,
	# inflation, stability, factory output, population, category factor,
	# overlord extraction) are combined into a single national_policy_scalar
	# and applied uniformly to every state — since those are national policies
	# that affect all states equally.
	#
	# state_GDP variables are purely informational. They do not feed back
	# into country_GDP in any way.
	# -----------------------------------------------------------------------

	# -----------------------
	# STEP 1: Compute national_policy_scalar at country scope
	# This is the combined product of all country-level modifiers
	# -----------------------

	# Construction bonus
	set_temp_variable = { s_construction = modifier@production_speed_buildings_factor }
	clamp_temp_variable = { var = s_construction min = -1 }
	add_to_temp_variable = { s_construction = 1 }

	# Inflation deflation
	set_temp_variable = { s_inflation = global.ECO_inflation }
	divide_temp_variable = { s_inflation = 100 }
	add_to_temp_variable = { s_inflation = 1 }
	set_temp_variable = { s_inflation_mult = 1.0 }
	divide_temp_variable = { s_inflation_mult = s_inflation }

	# Overlord extraction (civ)
	set_temp_variable = { s_overlord = modifier@cic_to_overlord_factor }
	multiply_temp_variable = { s_overlord = 0.99 }
	set_temp_variable = { s_overlord_mult = 1.0 }
	subtract_from_temp_variable = { s_overlord_mult = s_overlord }

	# Trade bonus
	set_temp_variable = { s_trade = modifier@min_export }
	clamp_temp_variable = { var = s_trade min = 0 max = 1 }
	set_temp_variable = { s_trade_scaled = @ECO_trade_scaled_weight }
	multiply_temp_variable = { s_trade_scaled = s_trade }
	set_temp_variable = { s_trade_penalty = @ECO_trade_penalty_weight }
	multiply_temp_variable = { s_trade_penalty = s_trade }
	multiply_temp_variable = { s_trade_penalty = s_trade }
	set_temp_variable = { s_trade_bonus = 1.0 }
	add_to_temp_variable = { s_trade_bonus = s_trade_scaled }
	subtract_from_temp_variable = { s_trade_bonus = s_trade_penalty }

	# Conscription penalty (civ)
	set_temp_variable = { s_conscript = modifier@conscription }
	set_temp_variable = { s_conscript_factor = modifier@conscription_factor }
	add_to_temp_variable = { s_conscript_factor = 1 }
	multiply_temp_variable = { s_conscript = s_conscript_factor }
	clamp_temp_variable = { var = s_conscript min = 0 max = 1 }
	set_temp_variable = { s_civ_conscript_penalty = 0.5 }
	multiply_temp_variable = { s_civ_conscript_penalty = s_conscript }
	set_temp_variable = { s_civ_mult = 1.0 }
	subtract_from_temp_variable = { s_civ_mult = s_civ_conscript_penalty }

	# Factory output efficiency
	set_temp_variable = { s_factory_output = modifier@industrial_capacity_factory }
	multiply_temp_variable = { s_factory_output = 0.2 }
	add_to_temp_variable = { s_factory_output = 1 }
	clamp_temp_variable = { var = s_factory_output min = 0.5 max = 1.5 }

	# Stability
	set_temp_variable = { s_stability = stability }
	multiply_temp_variable = { s_stability = 0.003 }
	add_to_temp_variable = { s_stability = 0.85 }
	clamp_temp_variable = { var = s_stability min = 0.70 max = 1.15 }

	# Population modifier
	set_temp_variable = { s_pop_mod = population_modifier }
	clamp_temp_variable = { var = s_pop_mod min = 0 max = 1.25 }

	# Category factor
	set_temp_variable = { s_cat_factor = temp_8 }

	# ECO efficiency for agriculture
	set_temp_variable = { s_eco_eff = global.ECO_efficiency }
	divide_temp_variable = { s_eco_eff = 100 }
	add_to_temp_variable = { s_eco_eff = 2 }

	# Combined national policy scalar
	# = construction * inflation_mult * overlord * trade * civ_conscript * factory_output * stability * pop * category
	set_variable = { national_policy_scalar = 1.0 }
	multiply_variable = { national_policy_scalar = s_construction }
	multiply_variable = { national_policy_scalar = s_inflation_mult }
	multiply_variable = { national_policy_scalar = s_overlord_mult }
	multiply_variable = { national_policy_scalar = s_trade_bonus }
	multiply_variable = { national_policy_scalar = s_civ_mult }
	multiply_variable = { national_policy_scalar = s_factory_output }
	multiply_variable = { national_policy_scalar = s_stability }
	multiply_variable = { national_policy_scalar = s_pop_mod }
	multiply_variable = { national_policy_scalar = s_cat_factor }

	# Resource scale (mirrors country-level temp_civ_resource = 8 + 0.25*temp_civ)
	# Use a fixed base of 8 for state scope since temp_civ is country-level
	set_variable = { state_resource_scale = 8 }

	# -----------------------
	# STEP 2: Compute state_GDP and state_GDP_per_capita per state
	# -----------------------
	every_state = {
		# Only compute for states that have an owner
		if = {
			limit = { has_owner = yes }

			# Weighted civs (× labour ratio — state_labour_ratio set in main calculate_GDP loop)
			set_temp_variable = { s_civs = THIS.non_damaged_building_level@industrial_complex }
			multiply_temp_variable = { s_civs = state_productivity_weight }
			multiply_temp_variable = { s_civs = state_labour_ratio }
			multiply_temp_variable = { s_civs = 7.0 }

			# Weighted arms (× labour ratio)
			set_temp_variable = { s_arms = THIS.non_damaged_building_level@arms_factory }
			multiply_temp_variable = { s_arms = state_productivity_weight }
			multiply_temp_variable = { s_arms = state_labour_ratio }
			multiply_temp_variable = { s_arms = 5.0 }

			# Weighted dockyards (× labour ratio)
			set_temp_variable = { s_docs = THIS.non_damaged_building_level@dockyard }
			multiply_temp_variable = { s_docs = state_productivity_weight }
			multiply_temp_variable = { s_docs = state_labour_ratio }
			multiply_temp_variable = { s_docs = 5.0 }

			# Resources (raw slots × gram values)
			set_temp_variable = { s_res = 0 }
			set_temp_variable = { s_res_val = resource@oil }
			multiply_temp_variable = { s_res_val = 0.001 }
			add_to_temp_variable = { s_res = s_res_val }
			set_temp_variable = { s_res_val = resource@coal }
			multiply_temp_variable = { s_res_val = 0.0006 }
			add_to_temp_variable = { s_res = s_res_val }
			set_temp_variable = { s_res_val = resource@steel }
			multiply_temp_variable = { s_res_val = 0.00005 }
			add_to_temp_variable = { s_res = s_res_val }
			set_temp_variable = { s_res_val = resource@rubber }
			multiply_temp_variable = { s_res_val = 0.00001 }
			add_to_temp_variable = { s_res = s_res_val }
			set_temp_variable = { s_res_val = resource@chromium }
			multiply_temp_variable = { s_res_val = 0.0000025 }
			add_to_temp_variable = { s_res = s_res_val }
			set_temp_variable = { s_res_val = resource@aluminium }
			multiply_temp_variable = { s_res_val = 0.0000005 }
			add_to_temp_variable = { s_res = s_res_val }
			set_temp_variable = { s_res_val = resource@tungsten }
			multiply_temp_variable = { s_res_val = 0.00000015 }
			add_to_temp_variable = { s_res = s_res_val }
			# Scale by state resource scale (mirrors country resource multiplier)
			OWNER = { multiply_temp_variable = { s_res = state_resource_scale } }

			# -----------------------------------------------------------------------
			# RESOURCE EXTRACTION POPULATION LABOUR RATIO
			# Extraction requires workforce proportional to output volume.
			# Each resource weighted by intensity. Base: 2k people per workforce-equivalent slot.
			# TUNING: per-resource weights and the ×2 base multiplier below.
			# FLOOR: 0.0001 (0.01%). Calibrated: Texas 1936 (6M pop, ~100 oil slots) ≈ 1.0 ratio.
			# -----------------------------------------------------------------------
			set_temp_variable = { s_res_equiv = resource@oil }
			multiply_temp_variable = { s_res_equiv = 15 }        # TUNING: Oil — most workforce-intensive
			set_temp_variable = { s_res_equiv_b = resource@coal }
			multiply_temp_variable = { s_res_equiv_b = 8 }       # TUNING: Coal — underground mining
			add_to_temp_variable = { s_res_equiv = s_res_equiv_b }
			set_temp_variable = { s_res_equiv_b = resource@steel }
			multiply_temp_variable = { s_res_equiv_b = 10 }      # TUNING: Steel — smelting + ore extraction
			add_to_temp_variable = { s_res_equiv = s_res_equiv_b }
			set_temp_variable = { s_res_equiv_b = resource@rubber }
			multiply_temp_variable = { s_res_equiv_b = 3 }       # TUNING: Rubber — plantation, labour-light
			add_to_temp_variable = { s_res_equiv = s_res_equiv_b }
			set_temp_variable = { s_res_equiv_b = resource@chromium }
			multiply_temp_variable = { s_res_equiv_b = 5 }       # TUNING: Chromium — open-cast mining
			add_to_temp_variable = { s_res_equiv = s_res_equiv_b }
			set_temp_variable = { s_res_equiv_b = resource@aluminium }
			multiply_temp_variable = { s_res_equiv_b = 5 }       # TUNING: Aluminium — bauxite + smelting
			add_to_temp_variable = { s_res_equiv = s_res_equiv_b }
			set_temp_variable = { s_res_equiv_b = resource@tungsten }
			multiply_temp_variable = { s_res_equiv_b = 4 }       # TUNING: Tungsten — precision mining
			add_to_temp_variable = { s_res_equiv = s_res_equiv_b }
			clamp_temp_variable = { var = s_res_equiv min = 0.0001 } # guard div/0 for resource-less states
			set_temp_variable = { s_res_pop_req = s_res_equiv }
			multiply_temp_variable = { s_res_pop_req = 2 }       # TUNING: 2 = 2k people per workforce-equivalent
			set_temp_variable = { s_res_labour_ratio = THIS.state_population_k }
			divide_temp_variable = { s_res_labour_ratio = s_res_pop_req }
			clamp_temp_variable = { var = s_res_labour_ratio min = 0.0001 max = 1.0 }
			multiply_temp_variable = { s_res = s_res_labour_ratio }

			# Agriculture (state population × agri_weight × eco_efficiency / inflation)
			set_temp_variable = { s_agri = THIS.state_population_k }
			multiply_temp_variable = { s_agri = state_agri_weight }
			divide_temp_variable = { s_agri = 1000 }
			OWNER = {
				multiply_temp_variable = { s_agri = s_eco_eff }
				divide_temp_variable = { s_agri = s_inflation }
			}

			# Raw state GDP (pre-policy, pre-currency)
			set_temp_variable = { s_raw_gdp = s_civs }
			add_to_temp_variable = { s_raw_gdp = s_arms }
			add_to_temp_variable = { s_raw_gdp = s_docs }
			add_to_temp_variable = { s_raw_gdp = s_res }
			add_to_temp_variable = { s_raw_gdp = s_agri }

			# Apply national policy scalar and currency conversion
			OWNER = { multiply_temp_variable = { s_raw_gdp = national_policy_scalar } }
			divide_temp_variable = { s_raw_gdp = @currency_conversion_factor }
			clamp_temp_variable = { var = s_raw_gdp min = 0 }

			# Store as state variable
			set_variable = { state_GDP = s_raw_gdp }

			# State GDPPC = state_GDP / state_population_k × 1000
			set_variable = { state_GDP_per_capita = state_GDP }
			multiply_variable = { state_GDP_per_capita = 1000 }
			set_temp_variable = { s_state_pop = THIS.state_population_k }
			clamp_temp_variable = { var = s_state_pop min = 1 }
			divide_variable = { state_GDP_per_capita = s_state_pop }
			multiply_variable = { state_GDP_per_capita = 1000 }
			clamp_variable = { var = state_GDP_per_capita min = 0 }
		}
	}

	# -----------------------
	# ISSUE 3 FIX: Correction factor — align sum of state GDPs to country GDP
	# Raw resource slots vs produced output causes ~10-20% overcounting.
	# Compute country_GDP / sum(state_GDPs) and apply to all states uniformly.
	# Sanity clamp: 0.5–2.0 to prevent runaway corrections.
	# -----------------------
	# -----------------------
	# CORRECTION + GDPPC RECOMPUTE + GLOBAL MAX — merged into one pass (Fix 2 optimisation)
	# Previously 3 separate every_state loops — now one
	# -----------------------
	set_variable = { state_gdp_sum = 0 }
	every_state = {
		if = {
			limit = { has_owner = yes }
			OWNER = { add_to_variable = { state_gdp_sum = PREV.state_GDP } }
		}
	}
	clamp_variable = { var = state_gdp_sum min = 0.0001 } # guard div/0
	set_variable = { state_gdp_correction = country_GDP }
	divide_variable = { state_gdp_correction = state_gdp_sum }
	clamp_variable = { var = state_gdp_correction min = 0.5 max = 2.0 }
	every_state = {
		if = {
			limit = { has_owner = yes }
			OWNER = {
				# Apply correction
				multiply_variable = { PREV.state_GDP = state_gdp_correction }
				# Recompute state GDPPC after correction
				set_variable = { PREV.state_GDP_per_capita = PREV.state_GDP }
				multiply_variable = { PREV.state_GDP_per_capita = 1000 }
				set_temp_variable = { s_pop_corr = PREV.state_population_k }
				clamp_temp_variable = { var = s_pop_corr min = 1 }
				divide_variable = { PREV.state_GDP_per_capita = s_pop_corr }
				multiply_variable = { PREV.state_GDP_per_capita = 1000 }
				clamp_variable = { var = PREV.state_GDP_per_capita min = 0 }
			}
		}
		# Track global max for map mode gradient
		if = {
			limit = { check_variable = { state_GDP > global.max_state_GDP } }
			set_variable = { global.max_state_GDP = state_GDP }
		}
		if = {
			limit = { check_variable = { state_GDP_per_capita > global.max_state_GDP_per_capita } }
			set_variable = { global.max_state_GDP_per_capita = state_GDP_per_capita }
		}
	}

	# -----------------------
	# PRE-COMPUTE SOL RATIO THRESHOLDS — done once here, not per-state
	# dyn_SoL_TYPE_NUM = global.average_GDP_per_capita × ratio
	# These are temp_variables readable inside every_state below
	# -----------------------
	# Pre-compute ratio thresholds per type from global average
	# Format: dyn_SoL_[type]_[tier_num]
	set_temp_variable = { dyn_SoL_metro_2 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_2 = 0.1716 }
	set_temp_variable = { dyn_SoL_urban_2 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_2 = 0.12 }
	set_temp_variable = { dyn_SoL_agrarian_2 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_2 = 0.0852 }
	set_temp_variable = { dyn_SoL_rural_2 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_2 = 0.06 }
	set_temp_variable = { dyn_SoL_wasteland_2 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_2 = 0.0372 }
	set_temp_variable = { dyn_SoL_metro_3 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_3 = 0.3146 }
	set_temp_variable = { dyn_SoL_urban_3 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_3 = 0.22 }
	set_temp_variable = { dyn_SoL_agrarian_3 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_3 = 0.1562 }
	set_temp_variable = { dyn_SoL_rural_3 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_3 = 0.11 }
	set_temp_variable = { dyn_SoL_wasteland_3 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_3 = 0.0682 }
	set_temp_variable = { dyn_SoL_metro_4 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_4 = 0.5005 }
	set_temp_variable = { dyn_SoL_urban_4 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_4 = 0.35 }
	set_temp_variable = { dyn_SoL_agrarian_4 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_4 = 0.2485 }
	set_temp_variable = { dyn_SoL_rural_4 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_4 = 0.175 }
	set_temp_variable = { dyn_SoL_wasteland_4 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_4 = 0.1085 }
	set_temp_variable = { dyn_SoL_metro_5 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_5 = 0.7436 }
	set_temp_variable = { dyn_SoL_urban_5 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_5 = 0.52 }
	set_temp_variable = { dyn_SoL_agrarian_5 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_5 = 0.3692 }
	set_temp_variable = { dyn_SoL_rural_5 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_5 = 0.26 }
	set_temp_variable = { dyn_SoL_wasteland_5 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_5 = 0.1612 }
	set_temp_variable = { dyn_SoL_metro_6 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_6 = 1.0725 }
	set_temp_variable = { dyn_SoL_urban_6 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_6 = 0.75 }
	set_temp_variable = { dyn_SoL_agrarian_6 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_6 = 0.5325 }
	set_temp_variable = { dyn_SoL_rural_6 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_6 = 0.375 }
	set_temp_variable = { dyn_SoL_wasteland_6 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_6 = 0.2325 }
	set_temp_variable = { dyn_SoL_metro_7 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_7 = 1.5015 }
	set_temp_variable = { dyn_SoL_urban_7 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_7 = 1.05 }
	set_temp_variable = { dyn_SoL_agrarian_7 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_7 = 0.7455 }
	set_temp_variable = { dyn_SoL_rural_7 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_7 = 0.525 }
	set_temp_variable = { dyn_SoL_wasteland_7 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_7 = 0.3255 }
	set_temp_variable = { dyn_SoL_metro_8 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_8 = 2.002 }
	set_temp_variable = { dyn_SoL_urban_8 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_8 = 1.4 }
	set_temp_variable = { dyn_SoL_agrarian_8 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_8 = 0.994 }
	set_temp_variable = { dyn_SoL_rural_8 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_8 = 0.7 }
	set_temp_variable = { dyn_SoL_wasteland_8 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_8 = 0.434 }
	set_temp_variable = { dyn_SoL_metro_9 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_9 = 2.6455 }
	set_temp_variable = { dyn_SoL_urban_9 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_9 = 1.85 }
	set_temp_variable = { dyn_SoL_agrarian_9 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_9 = 1.3135 }
	set_temp_variable = { dyn_SoL_rural_9 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_9 = 0.925 }
	set_temp_variable = { dyn_SoL_wasteland_9 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_9 = 0.5735 }
	set_temp_variable = { dyn_SoL_metro_10 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_10 = 3.575 }
	set_temp_variable = { dyn_SoL_urban_10 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_10 = 2.5 }
	set_temp_variable = { dyn_SoL_agrarian_10 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_10 = 1.775 }
	set_temp_variable = { dyn_SoL_rural_10 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_10 = 1.25 }
	set_temp_variable = { dyn_SoL_wasteland_10 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_10 = 0.775 }
	set_temp_variable = { dyn_SoL_metro_11 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_11 = 4.862 }
	set_temp_variable = { dyn_SoL_urban_11 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_11 = 3.4 }
	set_temp_variable = { dyn_SoL_agrarian_11 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_11 = 2.414 }
	set_temp_variable = { dyn_SoL_rural_11 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_11 = 1.7 }
	set_temp_variable = { dyn_SoL_wasteland_11 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_11 = 1.054 }
	set_temp_variable = { dyn_SoL_metro_12 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_12 = 6.435 }
	set_temp_variable = { dyn_SoL_urban_12 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_12 = 4.5 }
	set_temp_variable = { dyn_SoL_agrarian_12 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_12 = 3.195 }
	set_temp_variable = { dyn_SoL_rural_12 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_12 = 2.25 }
	set_temp_variable = { dyn_SoL_wasteland_12 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_12 = 1.395 }
	set_temp_variable = { dyn_SoL_metro_13 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_13 = 8.58 }
	set_temp_variable = { dyn_SoL_urban_13 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_13 = 6.0 }
	set_temp_variable = { dyn_SoL_agrarian_13 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_13 = 4.26 }
	set_temp_variable = { dyn_SoL_rural_13 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_13 = 3.0 }
	set_temp_variable = { dyn_SoL_wasteland_13 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_13 = 1.86 }
	set_temp_variable = { dyn_SoL_metro_14 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_14 = 10.725 }
	set_temp_variable = { dyn_SoL_urban_14 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_14 = 7.5 }
	set_temp_variable = { dyn_SoL_agrarian_14 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_14 = 5.325 }
	set_temp_variable = { dyn_SoL_rural_14 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_14 = 3.75 }
	set_temp_variable = { dyn_SoL_wasteland_14 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_14 = 2.325 }
	set_temp_variable = { dyn_SoL_metro_15 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_metro_15 = 12.87 }
	set_temp_variable = { dyn_SoL_urban_15 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_urban_15 = 9.0 }
	set_temp_variable = { dyn_SoL_agrarian_15 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_agrarian_15 = 6.39 }
	set_temp_variable = { dyn_SoL_rural_15 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_rural_15 = 4.5 }
	set_temp_variable = { dyn_SoL_wasteland_15 = global.average_GDP_per_capita }
	multiply_temp_variable = { dyn_SoL_wasteland_15 = 2.79 }

	# -----------------------
	# STATE LIVING STANDARD — hybrid dual-check system
	# Urban = baseline (matches country GDPPC ratios)
	# Each type scaled: metro x1.43 / urban x1.00 / agrarian x0.71 / rural x0.50 / wasteland x0.31
	# Both relative check (vs global avg) AND absolute floor must pass
	# Walk-up: starts at 1, each tier overwrites if both checks pass
	# -----------------------
	every_state = {
		if = {
			limit = { has_owner = yes }
			set_variable = { state_living_standard = 1 }

			# Tier 2: Squalid
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_2 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 21 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 2 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_2 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 15 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 2 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_2 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 11 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 2 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_2 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 8 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 2 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_2 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 5 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 2 }
			}

			# Tier 3: Hardscrabble
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_3 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 50 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 3 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_3 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 35 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 3 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_3 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 25 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 3 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_3 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 18 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 3 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_3 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 11 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 3 }
			}

			# Tier 4: Grimy
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_4 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 114 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 4 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_4 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 80 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 4 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_4 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 57 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 4 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_4 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 40 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 4 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_4 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 25 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 4 }
			}

			# Tier 5: Meager
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_5 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 214 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 5 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_5 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 150 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 5 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_5 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 106 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 5 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_5 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 75 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 5 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_5 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 46 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 5 }
			}

			# Tier 6: Decent
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_6 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 429 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 6 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_6 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 300 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 6 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_6 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 213 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 6 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_6 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 150 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 6 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_6 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 93 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 6 }
			}

			# Tier 7: Comfortable
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_7 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 715 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 7 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_7 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 500 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 7 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_7 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 355 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 7 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_7 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 250 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 7 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_7 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 155 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 7 }
			}

			# Tier 8: Robust
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_8 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1144 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 8 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_8 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 800 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 8 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_8 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 568 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 8 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_8 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 400 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 8 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_8 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 248 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 8 }
			}

			# Tier 9: Vibrant
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_9 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1716 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 9 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_9 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1200 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 9 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_9 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 852 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 9 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_9 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 600 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 9 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_9 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 372 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 9 }
			}

			# Tier 10: Prosperous
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_10 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 2574 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 10 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_10 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1800 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 10 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_10 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1278 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 10 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_10 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 900 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 10 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_10 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 558 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 10 }
			}

			# Tier 11: Opulent
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_11 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 3575 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 11 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_11 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 2500 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 11 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_11 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1775 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 11 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_11 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1250 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 11 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_11 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 775 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 11 }
			}

			# Tier 12: Gilded
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_12 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 5148 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 12 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_12 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 3600 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 12 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_12 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 2556 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 12 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_12 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1800 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 12 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_12 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1116 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 12 }
			}

			# Tier 13: Radiant
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_13 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 7150 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 13 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_13 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 5000 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 13 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_13 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 3550 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 13 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_13 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 2500 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 13 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_13 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 1550 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 13 }
			}

			# Tier 14: Transcendental
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_14 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 9295 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 14 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_14 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 6500 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 14 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_14 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 4615 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 14 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_14 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 3250 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 14 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_14 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 2015 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 14 }
			}

			# Tier 15: Utopian
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_metro_15 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 11726 compare = greater_than_or_equals }
					OR = {
						is_megalopolis_state = yes
						is_metropolis_state = yes
					}
				}
				set_variable = { state_living_standard = 15 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_urban_15 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 8200 compare = greater_than_or_equals }
					OR = {
						is_large_city_state = yes
						is_city_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
				}
				set_variable = { state_living_standard = 15 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_agrarian_15 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 5822 compare = greater_than_or_equals }
					is_large_town_state = yes
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
				}
				set_variable = { state_living_standard = 15 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_rural_15 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 4100 compare = greater_than_or_equals }
					OR = {
						is_town_state = yes
						is_rural_state = yes
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
				}
				set_variable = { state_living_standard = 15 }
			}
			if = {
				limit = {
					check_variable = { var = state_GDP_per_capita value = dyn_SoL_wasteland_15 compare = greater_than_or_equals }
					check_variable = { var = state_GDP_per_capita value = 2542 compare = greater_than_or_equals }
					OR = {
						has_state_category = wasteland
						has_state_category = pastoral
						has_state_category = small_island
						has_state_category = tiny_island
					}
					NOT = { is_megalopolis_state = yes }
					NOT = { is_metropolis_state = yes }
					NOT = { is_large_city_state = yes }
					NOT = { is_city_state = yes }
					NOT = { is_large_town_state = yes }
					NOT = { is_town_state = yes }
					NOT = { is_rural_state = yes }
				}
				set_variable = { state_living_standard = 15 }
			}

		}
	}
}

GDP_growth_calculation = {
	set_variable = { gdp_growth = country_GDP }
	multiply_variable = { gdp_growth = 100 }
	divide_variable = { gdp_growth = gdp_last }
	IF = {
		LIMIT = {
		   OR = { 
			   check_variable = { var = gdp_growth value = 100 }
			   check_variable = { var = gdp_growth value = 0 }
		   }
		}
		subtract_from_variable = { gdp_growth = 100 }
	}
	IF = {
		LIMIT = {
			check_variable = { gdp_growth = -0.001 }
		}
		multiply_variable = { gdp_growth = 0 }
	}
	multiply_variable = { gdp_growth = 1 }
	set_variable = { gdp_last = country_GDP }
}
# Randomized Iterative Quicksort by Yard1
# Feel free to use in your mods, but give credits to Yard1 (both in code, with comments; and on your download page)
scoped_quicksort = { # I added a meta effect & scripted loc to take in array arguments - reimu
	meta_effect = {
		text = {
			set_variable = { high = [SORTARRAY]^num }
			subtract_from_variable = { high = 1 }
			set_variable = { low = 0 }

			set_temp_variable = { stack_size = high }
			subtract_from_temp_variable = { stack_size = low }
			add_to_temp_variable = { stack_size = 1 }

			resize_temp_array = {
				array = stack
				value = 0
				size = stack_size
			}

			set_temp_variable = { top = 0 }
			set_temp_variable = { stack^top = low }
			add_to_temp_variable = { top = 1 }
			set_temp_variable = { stack^top = high }

			while_loop_effect = {
				limit = { 
					check_variable = {
						var = top
						value = 0
						compare = greater_than_or_equals
					}
				}
				set_variable = { high = stack^top }
				subtract_from_temp_variable = { top = 1 }
				set_variable = { low = stack^top }
				subtract_from_temp_variable = { top = 1 }

				set_variable = { random_pivot = random }
				multiply_variable = { random_pivot = high }
				round_variable = random_pivot
				add_to_variable = { random_pivot = low }
				clamp_variable = {
					var = random_pivot
					min = low
					max = high
				}
				set_temp_variable = { swap = [SORTARRAY]^high }
				set_variable = { [SORTARRAY]^high = [SORTARRAY]^random_pivot }
				set_variable = { [SORTARRAY]^random_pivot = swap }

				set_temp_variable = { idx = low }
				subtract_from_temp_variable = { idx = 1 }

				var:[SORTARRAY]^high = {
					set_variable = { PREV.minimum = [SORTVALUE] }
				}

				for_loop_effect = {
					start = low
					end = high
					var:[SORTARRAY]^v = {
						set_variable = { PREV.tmp = [SORTVALUE] }
					}
					if = {
						limit = {
							check_variable = {
								var = tmp
								value = minimum
								compare = greater_than_or_equals
							}
						}
						add_to_temp_variable = { idx = 1 }
						set_temp_variable = { swap = [SORTARRAY]^idx }
						set_variable = { [SORTARRAY]^idx = [SORTARRAY]^v }
						set_variable = { [SORTARRAY]^v = swap }
					}
				}

				add_to_temp_variable = { idx = 1 }
				set_temp_variable = { swap = [SORTARRAY]^idx }
				set_variable = { [SORTARRAY]^idx = [SORTARRAY]^high }
				set_variable = { [SORTARRAY]^high = swap }

				set_temp_variable = { qs_partition_return = idx }

				set_temp_variable = { qs_partition_return_plus_1 = qs_partition_return }
				set_temp_variable = { qs_partition_return_minus_1 = qs_partition_return }
				add_to_temp_variable = { qs_partition_return_plus_1 = 1 } 
				subtract_from_temp_variable = { qs_partition_return_minus_1 = 1 } 

				if = {
					limit = { check_variable = { qs_partition_return_minus_1 > low } }
					add_to_temp_variable = { top = 1 }
					set_temp_variable = { stack^top = low }
					add_to_temp_variable = { top = 1 }
					set_temp_variable = { stack^top = qs_partition_return_minus_1 }
				}
				if = {
					limit = { check_variable = { qs_partition_return_plus_1 < high } }
					add_to_temp_variable = { top = 1 }
					set_temp_variable = { stack^top = qs_partition_return_plus_1 }
					add_to_temp_variable = { top = 1 }
					set_temp_variable = { stack^top = high }
				}
			}
			clear_variable = minimum
			clear_variable = tmp
			clear_variable = random_pivot
		}
		SORTARRAY = "[ROOT.GetSortArray]"
		SORTVALUE = "[ROOT.GetSortValue]"
	}
}
# Randomized Iterative Quicksort by Yard1
# Feel free to use in your mods, but give credits to Yard1 (both in code, with comments; and on your download page)
create_GDP_sorted_array = {
	IF = {
		LIMIT = {
			check_variable = { last_sort_var = sort_var }
		}
		set_variable = { middle = global.GDP_list_array^num }
		set_temp_variable = { len = global.GDP_list_array^num }
		subtract_from_temp_variable = { len = 1 }
		divide_variable = { middle = 2 }
		round_variable = middle
		for_loop_effect = {
			start = 0
			end = middle

			set_temp_variable = { reverse = len }
			subtract_from_temp_variable = { reverse = v }
			set_temp_variable = { swap = global.GDP_list_array^v }
			set_variable = { global.GDP_list_array^v = global.GDP_list_array^reverse }
			set_variable = { global.GDP_list_array^reverse = swap }
		}
		clear_variable = middle
		set_variable = { last_sort_var = 0 }
	}
	ELSE = {
		set_temp_variable = { temp_array_sort = 4 }
		set_temp_variable = { temp_value_sort = 2 }
		scoped_quicksort = yes
		set_variable = { last_sort_var = sort_var }
	}
}

update_position = {
	clear_array = global.top_ten_GDP_array
	clear_variable = global.GDP_total
	clear_variable = global.GDP_per_capita_total
	
	set_temp_variable = { position = 0 }
	set_variable = { sort_var = 1 }
	set_variable = { last_sort_var = 0 }
	create_GDP_sorted_array = yes

	for_each_scope_loop = {
		array = global.GDP_list_array

		add_to_temp_variable = { position = 1 }
		set_variable = { THIS.GDP_position = position }

		if = {
			limit = {
				check_variable = { THIS.GDP_position < 11 }
			}
			add_to_array = { global.top_ten_GDP_array = THIS }
		}
		add_to_variable = { global.GDP_total = country_GDP }
		add_to_variable = { global.GDP_per_capita_total = country_GDP_per_capita }
	}

	set_variable = { global.average_GDP = global.GDP_total }
	set_temp_variable = { gdp_array_size = global.GDP_list_array^num }
	clamp_temp_variable = { var = gdp_array_size min = 1 } # Guard: empty array edge case
	divide_variable = { global.average_GDP = gdp_array_size }
	set_variable = { global.average_GDP_per_capita = global.GDP_per_capita_total }
	divide_variable = { global.average_GDP_per_capita = gdp_array_size }
}

create_GDP_per_capita_sorted_array = {
	if = {
		limit = {
			check_variable = { last_sort_var = sort_var }
		}
		set_variable = { middle = global.GDP_list_array^num }
		set_temp_variable = { len = global.GDP_list_array^num }
		subtract_from_temp_variable = { len = 1 }
		divide_variable = { middle = 2 }
		round_variable = middle
		for_loop_effect = {
			start = 0
			end = middle

			set_temp_variable = { reverse = len }
			subtract_from_temp_variable = { reverse = v }
			set_temp_variable = { swap = global.GDP_list_array^v }
			set_variable = { global.GDP_list_array^v = global.GDP_list_array^reverse }
			set_variable = { global.GDP_list_array^reverse = swap }
		}
		clear_variable = middle
		set_variable = { last_sort_var = 0 }
	}
	else = {
		set_temp_variable = { temp_array_sort = 4 }
		set_temp_variable = { temp_value_sort = 3 }
		scoped_quicksort = yes
		set_variable = { last_sort_var = sort_var }
	}
}

ECO_generate_csv_data_per_country = {
	# Log CSV header
	log = "[GetDateText] Data:Country_Tag,Country_Population_K,Conscription_Rate,Num_Owned_States,Num_Controlled_States,Military_Factories,Civilian_Factories,Naval_Dockyards,Total_Factories,Oil_Units,Oil_Barrels,Aluminium_Units,Aluminium_Tons,Rubber_Units,Rubber_Tons,Tungsten_Units,Tungsten_Tons,Steel_Units,Steel_Tons,Chromium_Units,Chromium_Tons,Coal,Coal_ShortTons,Maximum_Potential_Exporting,Country_GDP_M,Resource_Sector_GDP_USD_M,Resource_Share_Pct,Agriculture_Sector_GDP_USD_M,Agriculture_Share_Pct,Country_GDP_per_C,Global_Inflation,Global_Efficiency"

	# Iterate through all countries
	every_country = {

		# Conscription
		set_temp_variable = { base_conscription_rate = modifier@conscription }
		set_temp_variable = { base_conscription_bonus_factor = modifier@conscription_factor }
		add_to_temp_variable = { base_conscription_bonus_factor = 1 }
		multiply_temp_variable = { base_conscription_rate = base_conscription_bonus_factor }

		# Collect factory data
		set_temp_variable = { mil_factories = num_of_military_factories }
		set_temp_variable = { civ_factories = num_of_civilian_factories }
		set_temp_variable = { naval_factories = num_of_naval_factories }
		set_temp_variable = { total_factories = num_of_factories }

		# Collect resource production (base units)
		set_temp_variable = { oil_base = resource_produced@oil }
		set_temp_variable = { aluminium_base = resource_produced@aluminium }
		set_temp_variable = { rubber_base = resource_produced@rubber }
		set_temp_variable = { tungsten_base = resource_produced@tungsten }
		set_temp_variable = { steel_base = resource_produced@steel }
		set_temp_variable = { chromium_base = resource_produced@chromium }
		set_temp_variable = { coal_base = resource_produced@coal }

		# Convert resources to real-world units
		# Oil: 1 unit = 1,000,000 barrels
		set_temp_variable = { oil_converted = oil_base }
		multiply_temp_variable = { oil_converted = 1000000 }

		# Aluminium: 1 unit = 500 metric tons
		set_temp_variable = { aluminium_converted = aluminium_base }
		multiply_temp_variable = { aluminium_converted = 500 }

		# Rubber: 1 unit = 10,000 metric tons
		set_temp_variable = { rubber_converted = rubber_base }
		multiply_temp_variable = { rubber_converted = 10000 }

		# Tungsten: 1 unit = 150 metric tons
		set_temp_variable = { tungsten_converted = tungsten_base }
		multiply_temp_variable = { tungsten_converted = 150 }

		# Steel: 1 unit = 70,000 metric tons
		set_temp_variable = { steel_converted = steel_base }
		multiply_temp_variable = { steel_converted = 70000 }

		# Chromium: 1 unit = 2,500 metric tons
		set_temp_variable = { chromium_converted = chromium_base }
		multiply_temp_variable = { chromium_converted = 2500 }

		# Coal: 1 unit = 670,000 short tons
		set_temp_variable = { coal_converted = coal_base }
		multiply_temp_variable = { coal_converted = 670000 }

		# Trade
		set_temp_variable = { var = base_total_export_percent value = modifier@min_export }


		# Collect economic data
		set_temp_variable = { country_gdp_based = country_GDP_M }
		set_temp_variable = { country_resource_gdp_based = country_resource_GDP_M }
		set_temp_variable = { country_agriculture_gdp_based = country_agriculture_GDP_M }

		set_temp_variable = { resource_share_pct = country_resource_GDP }
		divide_temp_variable = { resource_share_pct = country_GDP }

		set_temp_variable = { agriculture_share_pct = country_agriculture_GDP }
		divide_temp_variable = { agriculture_share_pct = country_GDP }

		set_temp_variable = { country_gdp_c_based = country_GDP_per_capita }
		set_temp_variable = { global_inflation_based = global.ECO_inflation }
		divide_temp_variable = { global_inflation_based = 100 }
		set_temp_variable = { global_efficiency_based = global.ECO_efficiency }
		divide_temp_variable = { global_efficiency_based = 100 }

		# Log CSV row with country tag and all data
		log = "[THIS.GetTag],[?max_manpower_k|0],[?base_conscription_rate|0%],[?num_owned_states|0],[?num_controlled_states|0],[?mil_factories|0],[?civ_factories|0],[?naval_factories|0],[?total_factories|0],[?oil_base|0],[?oil_converted|0],[?aluminium_base|0],[?aluminium_converted|0],[?rubber_base|0],[?rubber_converted|0],[?tungsten_base|0],[?tungsten_converted|0],[?steel_base|0],[?steel_converted|0],[?chromium_base|0],[?chromium_converted|0],[?coal_base|0],[?coal_converted|0],[?base_total_export_percent|0%],[?country_gdp_based|0],[?country_resource_gdp_based|0],[?resource_share_pct|0%],[?country_agriculture_gdp_based|0],[?agriculture_share_pct|0%],[?country_gdp_c_based|0],[?global_inflation_based|0%],[?global_efficiency_based|0%]"
	}
}