#	Example:
# 
#	example_effect = {
#		add_political_power = 66
#		add_popularity = {
#			ideology = fascist
#			popularity = 0.33
#		}
#	}
#
#
#	In a script file:
#
#	effect = {
#		example_effect = yes
#	}
#

gdp_init_calc = {	
	###############################################
	# FIXED ECONOMICS - DIMINISHING RETURNS + POPULATION SCALING
	###############################################
	
	# Get raw factory count
    set_temp_variable = { factory_count_temp = num_of_civilian_factories }
	set_temp_variable = { pop_k_temp = max_manpower_k }
	
	# STEP 1: Apply DIMINISHING RETURNS on factories using square root
	# Formula: sqrt(factories) instead of linear factories
	set_temp_variable = { math_sqrt_number = factory_count_temp }
	add_to_temp_variable = { math_sqrt_number = 0.01 } # Avoid zero
	math_square_root = yes
	set_temp_variable = { effective_factories = math_sqrt_output }
	
	# STEP 2: Calculate POPULATION SCALING FACTOR with diminishing returns
	# Different formulas based on population size to create realistic curves
	
	set_temp_variable = { pop_multiplier = 1 }
	
	# Very large populations (150M+) - Moderate diminishing returns
	if = {
	    limit = {
		    check_variable = { 
			    pop_k_temp > 150000 
			}
		}
	    # Use sqrt for diminishing returns: sqrt(pop/150000)
	    set_temp_variable = { pop_normalized = pop_k_temp }
	    divide_temp_variable = { pop_normalized = 150000 }
	    math_square_root = yes
	    set_temp_variable = { pop_multiplier = math_sqrt_output }
	    multiply_temp_variable = { pop_multiplier = 0.0009 }
	}
	
	# Large populations (90-150M) - Balanced scaling
	if = {
	    limit = {
		    check_variable = {
    			pop_k_temp < 150000 
				pop_k_temp > 90000
			}
		}
	    multiply_temp_variable = { pop_multiplier = 0.001 }
	    set_temp_variable = { pop_adj = pop_k_temp }
	    divide_temp_variable = { pop_adj = 120000 } # Normalize around mid-range
	    multiply_temp_variable = { pop_multiplier = pop_adj }
	} 
	
	# Medium populations (30-90M) - Linear-ish growth
	if = {
	    limit = {
		    check_variable = {
      			pop_k_temp < 90000 
				pop_k_temp > 30000 
			}
		}
	    multiply_temp_variable = { pop_multiplier = 0.002 }
	    set_temp_variable = { pop_adj = pop_k_temp }
	    divide_temp_variable = { pop_adj = 60000 }
	    multiply_temp_variable = { pop_multiplier = pop_adj }
	}
	
	# Small-Medium populations (15-30M) - Good growth potential
	if = {
	    limit = {
		    check_variable = { 
			    pop_k_temp < 30000 
			    pop_k_temp > 15000
			}
		}
	    multiply_temp_variable = { pop_multiplier = 0.004 }
	    set_temp_variable = { pop_adj = pop_k_temp }
	    divide_temp_variable = { pop_adj = 22500 }
	    multiply_temp_variable = { pop_multiplier = pop_adj }
	}
	
	# Small populations (<15M) - Penalty for lack of labor
	if = {
	    limit = {
		    check_variable = { pop_k_temp < 15000 }
		}
	    # Sqrt scaling with penalty: smaller nations face labor shortage
	    set_temp_variable = { pop_normalized = pop_k_temp }
	    divide_temp_variable = { pop_normalized = 10000 }
	    math_square_root = yes
	    set_temp_variable = { pop_multiplier = math_sqrt_output }
	    multiply_temp_variable = { pop_multiplier = 0.007 }
	}
	
	# STEP 3: Apply synergistic production function
	# GDP = sqrt(Factories) × Population_Factor
	# This creates realistic economics: Land + Labor + Capital all matter
	multiply_temp_variable = { effective_factories = pop_multiplier }
	
	# Set final GDP calculation
	set_variable = { gdp_calc = effective_factories }
	
	# Apply soft cap to prevent extreme outliers at game start
	if = {
		limit = {
			check_variable = { gdp_calc > 200 }
		}
		# If GDP calc exceeds 200, apply diminishing returns
		set_temp_variable = { excess = gdp_calc }
		subtract_from_temp_variable = { excess = 200 }
		divide_temp_variable = { excess = 2 } # Half the excess
		set_variable = { gdp_calc = 200 }
		add_to_variable = { gdp_calc = excess }
	}
}

gdp_growth_calc = {	
    every_country = {
    set_variable = { gdp_growth_calc = 0.01 }
	if = {
	    limit = {
		    has_idea = undisturbed_isolation
		}
	    add_to_variable = { gdp_growth_calc = -0.002 }
	}
	if = {
	    limit = {
		    has_idea = isolation
		}
	    add_to_variable = { gdp_growth_calc = -0.001 }
	}
	if = {
	    limit = {
		    has_idea = civilian_economy
		}
	    add_to_variable = { gdp_growth_calc = 0.005 }
	}
	if = {
	    limit = {
		    has_idea = low_economic_mobilisation
		}
	    add_to_variable = { gdp_growth_calc = 0.002 }
	}
	if = {
	    limit = {
		    has_idea = partial_economic_mobilisation
		}
	    add_to_variable = { gdp_growth_calc = 0.001 }
	}
	if = {
	    limit = {
		    has_idea = war_economy
		}
	    add_to_variable = { gdp_growth_calc = -0.002 }
	}
	if = {
	    limit = {
		    has_idea = tot_economic_mobilisation
		}
	    add_to_variable = { gdp_growth_calc = -0.004 }
	}
	if = {
	    limit = {
		    has_idea = free_trade
		}
	    add_to_variable = { gdp_growth_calc = 0.003 }
	}
	if = {
	    limit = {
		    has_idea = export_focus
		}
	    add_to_variable = { gdp_growth_calc = 0.004 }
	}
	if = {
	    limit = {
		    has_idea = limited_exports
		}
	    add_to_variable = { gdp_growth_calc = 0.001 }
	}
	if = {
	    limit = {
		    has_idea = closed_economy
		}
	    add_to_variable = { gdp_growth_calc = -0.001 }
	}

	set_temp_variable = { gdp_calc_temp = gdp_calc }
	multiply_temp_variable = { gdp_calc_temp = gdp_growth_calc }
	add_to_temp_variable = { gdp_calc = gdp_calc_temp }
	
	}
}