############################################################################################################
#	Expert AI mod - resource effects
############################################################################################################

# Take resource extraction decisions when AI can't produce enough resources for its lines
EAI_RESOURCE_extraction_decisions = {

	if = { # Minors should get all the resources possible
		limit = {
			OR = {
				has_political_power > 250
				AND = { is_major = no has_idea = free_trade }
			}
		}

		set_country_flag = { flag = EAI_extract_aluminium value = 1 days = 30 }
		set_country_flag = { flag = EAI_extract_rubber value = 1 days = 30 }
		set_country_flag = { flag = EAI_extract_tungsten value = 1 days = 30 }
		set_country_flag = { flag = EAI_extract_steel value = 1 days = 30 }
		set_country_flag = { flag = EAI_extract_chromium value = 1 days = 30 }
	}

	else = {

		if = { limit = { check_variable = { EAI_needs_aluminium = 3 } }

			set_country_flag = { flag = EAI_extract_aluminium value = 1 days = 30 }
		}

		if = { limit = { check_variable = { EAI_needs_rubber = 3 } }

			set_country_flag = { flag = EAI_extract_rubber value = 1 days = 30 }
		}

		if = { limit = { check_variable = { EAI_needs_tungsten = 3 } }

			set_country_flag = { flag = EAI_extract_tungsten value = 1 days = 30 }
		}

		if = { limit = { check_variable = { EAI_needs_steel = 3 } }

			set_country_flag = { flag = EAI_extract_steel value = 1 days = 30 }
		}

		if = { limit = { check_variable = { EAI_needs_chromium = 3 } }

			set_country_flag = { flag = EAI_extract_chromium value = 1 days = 30 }
		}
	}
}

### Checked every 15 days. Tracked over time because resource needs fluctuate due to various reasons and the AI may be able to solve it on its own before it needs more drastic measures.

# Does the AI have to trade to fulfill its production line needs?
# Use decisions, build refineries, infrastructure etc. to fix
EAI_RESOURCE_check_resource_needs = {

	### aluminium

	if = {
		limit = {
			OR = {
				check_variable = { resource@aluminium < -20 }
				check_variable = { resource_imported@aluminium > 20 }
			}
		}

		add_to_variable = { EAI_needs_aluminium = 1 }
	}
	else = { subtract_from_variable = { EAI_needs_aluminium = 1 } }
	clamp_variable = { var = EAI_needs_aluminium min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_needs_aluminium = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: ALUMINIUM needed, resource@aluminium = [?resource@aluminium], resource_imported@aluminium = [?resource_imported@aluminium]" }

	### rubber

	if = {
		limit = {
			OR = {
				check_variable = { resource@rubber < -20 }
				check_variable = { resource_imported@rubber > 20 }
			}
		}

		add_to_variable = { EAI_needs_rubber = 1 }
	}
	else = { subtract_from_variable = { EAI_needs_rubber = 1 } }
	clamp_variable = { var = EAI_needs_rubber min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_needs_rubber = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: RUBBER needed, resource@rubber = [?resource@rubber], resource_imported@rubber = [?resource_imported@rubber]" }

	### steel

	if = {
		limit = {
			OR = {
				check_variable = { resource@steel < -20 }
				check_variable = { resource_imported@steel > 20 }
			}
		}

		add_to_variable = { EAI_needs_steel = 1 }
	}
	else = { subtract_from_variable = { EAI_needs_steel = 1 } }
	clamp_variable = { var = EAI_needs_steel min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_needs_steel = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: STEEL needed, resource@steel = [?resource@steel], resource_imported@steel = [?resource_imported@steel]" }

	### tungsten

	if = {
		limit = {
			OR = {
				check_variable = { resource@tungsten < -20 }
				check_variable = { resource_imported@tungsten > 20 }
			}
		}

		add_to_variable = { EAI_needs_tungsten = 1 }
	}
	else = { subtract_from_variable = { EAI_needs_tungsten = 1 } }
	clamp_variable = { var = EAI_needs_tungsten min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_needs_tungsten = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: TUNGSTEN needed, resource@tungsten = [?resource@tungsten], resource_imported@tungsten = [?resource_imported@tungsten]" }

	### chromium

	if = {
		limit = {
			OR = {
				check_variable = { resource@chromium < -20 }
				check_variable = { resource_imported@chromium > 20 }
			}
		}

		add_to_variable = { EAI_needs_chromium = 1 }
	}
	else = { subtract_from_variable = { EAI_needs_chromium = 1 } }
	clamp_variable = { var = EAI_needs_chromium min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_needs_chromium = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: CHROMIUM needed, resource@chromium = [?resource@chromium], resource_imported@chromium = [?resource_imported@chromium]" }

	### coal

	if = {
		limit = {
			OR = {
				check_variable = { resource@coal < -20 }
				check_variable = { resource_imported@coal > 20 }
			}
		}

		add_to_variable = { EAI_needs_coal = 1 }
	}
	else = { subtract_from_variable = { EAI_needs_coal = 1 } }
	clamp_variable = { var = EAI_needs_coal min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_needs_coal = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: coal needed, resource@coal = [?resource@coal], resource_imported@coal = [?resource_imported@coal]" }

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

	### allied resource needs

	for_each_scope_loop = { array = allies

		if = { limit = { check_variable = { resource@aluminium < -20 } } 	ROOT = { set_country_flag = { flag = EAI_allies_need_aluminium value = 1 days = 30 } } }
		if = { limit = { check_variable = { resource@rubber < -20 } } 		ROOT = { set_country_flag = { flag = EAI_allies_need_rubber value = 1 days = 30 } } }
		if = { limit = { check_variable = { resource@tungsten < -20 } } 	ROOT = { set_country_flag = { flag = EAI_allies_need_tungsten value = 1 days = 30 } } }
		if = { limit = { check_variable = { resource@steel < -20 } } 		ROOT = { set_country_flag = { flag = EAI_allies_need_steel value = 1 days = 30 } } }
		if = { limit = { check_variable = { resource@chromium < -20 } } 	ROOT = { set_country_flag = { flag = EAI_allies_need_chromium value = 1 days = 30 } } }
	}
}

# Does the AI have significant resource deficits compared to its production line needs? 
# More drastic measures; more decisions, refineries, more willing to change trade law, use forced labor
EAI_RESOURCE_check_resource_shortages = {

	@resource_shortage_limit = 0.15
	@resource_shortage_min_need = -25

	### FUEL

	if = {
		limit = {
			has_war = yes

			fuel_ratio < 0.25
		}

		add_to_variable = { EAI_fuel_resource_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_fuel_resource_shortage = 1 } }
	clamp_variable = { var = EAI_fuel_resource_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_fuel_resource_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: FUEL shortage, fuel_ratio = [?fuel_ratio]" }


	### ALUMINIUM

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@aluminium < 0 }
			
			# total needed for production
			set_temp_variable = { aluminium_needed = resource_consumed@aluminium }
			multiply_temp_variable = { aluminium_needed = -1 }
			add_to_temp_variable = { aluminium_needed = resource@aluminium }

			# need is at least this much
			check_variable = { aluminium_needed < @resource_shortage_min_need }

			# deficit is >25% of total need
			set_temp_variable = { aluminium_deficit = resource@aluminium }
			divide_temp_variable = { aluminium_deficit = aluminium_needed }
			check_variable = { aluminium_deficit > @resource_shortage_limit }
		}

		add_to_variable = { EAI_aluminium_resource_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_aluminium_resource_shortage = 1 } }
	clamp_variable = { var = EAI_aluminium_resource_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_aluminium_resource_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: ALUMINIUM shortage, deficit : need = [?aluminium_deficit|%0]" }

	### RUBBER

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@rubber < 0 }
			
			# total needed for production
			set_temp_variable = { rubber_needed = resource_consumed@rubber }
			multiply_temp_variable = { rubber_needed = -1 }
			add_to_temp_variable = { rubber_needed = resource@rubber }

			# need is at least this much
			check_variable = { rubber_needed < @resource_shortage_min_need }

			# deficit is >25% of total need
			set_temp_variable = { rubber_deficit = resource@rubber }
			divide_temp_variable = { rubber_deficit = rubber_needed }
			check_variable = { rubber_deficit > @resource_shortage_limit }
		}

		add_to_variable = { EAI_rubber_resource_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_rubber_resource_shortage = 1 } }
	clamp_variable = { var = EAI_rubber_resource_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_rubber_resource_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: RUBBER shortage, deficit : need = [?rubber_deficit|%0]" }

	### TUNGSTEN

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@tungsten < 0 }
			
			# total needed for production
			set_temp_variable = { tungsten_needed = resource_consumed@tungsten }
			multiply_temp_variable = { tungsten_needed = -1 }
			add_to_temp_variable = { tungsten_needed = resource@tungsten }

			# need is at least this much
			check_variable = { tungsten_needed < @resource_shortage_min_need }

			# deficit is >25% of total need
			set_temp_variable = { tungsten_deficit = resource@tungsten }
			divide_temp_variable = { tungsten_deficit = tungsten_needed }
			check_variable = { tungsten_deficit > @resource_shortage_limit }
		}

		add_to_variable = { EAI_tungsten_resource_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_tungsten_resource_shortage = 1 } }
	clamp_variable = { var = EAI_tungsten_resource_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_tungsten_resource_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: TUNGSTEN shortage, deficit : need = [?tungsten_deficit|%0]" }

	### STEEL

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@steel < 0 }
			
			# total needed for production
			set_temp_variable = { steel_needed = resource_consumed@steel }
			multiply_temp_variable = { steel_needed = -1 }
			add_to_temp_variable = { steel_needed = resource@steel }

			# need is at least this much
			check_variable = { steel_needed < @resource_shortage_min_need }

			# deficit is >25% of total need
			set_temp_variable = { steel_deficit = resource@steel }
			divide_temp_variable = { steel_deficit = steel_needed }
			check_variable = { steel_deficit > @resource_shortage_limit }
		}

		add_to_variable = { EAI_steel_resource_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_steel_resource_shortage = 1 } }
	clamp_variable = { var = EAI_steel_resource_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_steel_resource_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: STEEL shortage, deficit : need = [?steel_deficit|%0]" }

	### CHROMIUM

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@chromium < 0 }
			
			# total needed for production
			set_temp_variable = { chromium_needed = resource_consumed@chromium }
			multiply_temp_variable = { chromium_needed = -1 }
			add_to_temp_variable = { chromium_needed = resource@chromium }

			# need is at least this much
			check_variable = { chromium_needed < @resource_shortage_min_need }

			# deficit is >25% of total need
			set_temp_variable = { chromium_deficit = resource@chromium }
			divide_temp_variable = { chromium_deficit = chromium_needed }
			check_variable = { chromium_deficit > @resource_shortage_limit }
		}

		add_to_variable = { EAI_chromium_resource_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_chromium_resource_shortage = 1 } }
	clamp_variable = { var = EAI_chromium_resource_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_chromium_resource_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: CHROMIUM shortage, deficit : need = [?chromium_deficit|%0]" }

	### coal

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@coal < 0 }
			
			# total needed for production
			set_temp_variable = { coal_needed = resource_consumed@coal }
			multiply_temp_variable = { coal_needed = -1 }
			add_to_temp_variable = { coal_needed = resource@coal }

			# need is at least this much
			check_variable = { coal_needed < @resource_shortage_min_need }

			# deficit is >25% of total need
			set_temp_variable = { coal_deficit = resource@coal }
			divide_temp_variable = { coal_deficit = coal_needed }
			check_variable = { coal_deficit > @resource_shortage_limit }
		}

		add_to_variable = { EAI_coal_resource_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_coal_resource_shortage = 1 } }
	clamp_variable = { var = EAI_coal_resource_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_coal_resource_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: coal shortage, deficit : need = [?coal_deficit|%0]" }

	##############################################################################################################################
	#	Massive shortage
	##############################################################################################################################

	@resource_massive_shortage_min_need = -100
	@resource_massive_shortage_limit = 0.50

	### ALUMINIUM

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@aluminium < @resource_massive_shortage_min_need }
		}

		add_to_variable = { EAI_aluminium_resource_massive_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_aluminium_resource_massive_shortage = 1 } }
	clamp_variable = { var = EAI_aluminium_resource_massive_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_aluminium_resource_massive_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: ALUMINIUM shortage, deficit : need = [?aluminium_deficit|%0]" }

	### RUBBER

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@rubber < @resource_massive_shortage_min_need }
		}

		add_to_variable = { EAI_rubber_resource_massive_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_rubber_resource_massive_shortage = 1 } }
	clamp_variable = { var = EAI_rubber_resource_massive_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_rubber_resource_massive_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: RUBBER shortage, deficit : need = [?rubber_deficit|%0]" }

	### TUNGSTEN

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@tungsten < @resource_massive_shortage_min_need }
		}

		add_to_variable = { EAI_tungsten_resource_massive_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_tungsten_resource_massive_shortage = 1 } }
	clamp_variable = { var = EAI_tungsten_resource_massive_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_tungsten_resource_massive_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: TUNGSTEN shortage, deficit : need = [?tungsten_deficit|%0]" }

	### STEEL

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@steel < @resource_massive_shortage_min_need }
		}

		add_to_variable = { EAI_steel_resource_massive_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_steel_resource_massive_shortage = 1 } }
	clamp_variable = { var = EAI_steel_resource_massive_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_steel_resource_massive_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: STEEL shortage, deficit : need = [?steel_deficit|%0]" }

	### CHROMIUM

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@chromium < @resource_massive_shortage_min_need }
		}

		add_to_variable = { EAI_chromium_resource_massive_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_chromium_resource_massive_shortage = 1 } }
	clamp_variable = { var = EAI_chromium_resource_massive_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_chromium_resource_massive_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: CHROMIUM shortage, deficit : need = [?chromium_deficit|%0]" }

	### coal

	if = {
		limit = {
			# there is a deficit
			check_variable = { resource@coal < @resource_massive_shortage_min_need }
		}

		add_to_variable = { EAI_coal_resource_massive_shortage = 1 }
	}
	else = { subtract_from_variable = { EAI_coal_resource_massive_shortage = 1 } }
	clamp_variable = { var = EAI_coal_resource_massive_shortage min = 0 max = 3 }
	if = { limit = { has_country_flag = EAI_resources_logging check_variable = { EAI_coal_resource_massive_shortage = 3 } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCES: coal shortage, deficit : need = [?coal_deficit|%0]" }
}

###

# Determine if the trade law should be changed based on resources
EAI_RESOURCE_calculate_resource_need = {

	if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | EAI_RESOURCE_calculate_resource_need" }

	### Trade laws

		@EAI_RESOURCE_min_deficit_pct_diff_for_lower_law = -0.25
		@EAI_RESOURCE_max_deficit_pct_diff_for_higher_law = 0.05

		@EAI_RESOURCE_downgrade_trade_law_threshold = 10
		@EAI_RESOURCE_upgrade_trade_law_threshold = 10

		@EAI_RESOURCE_min_total_need_to_care_about_resource = 25

		@free_trade = 0
		@export_focus = 1
		@limited_exports = 2
		@closed_economy = 3

		@free_trade_min_export = 0.8
		@export_focus_min_export = 0.5
		@limited_exports_min_export = 0.25
		@closed_economy_min_export = 0

		set_temp_variable = { tmp = 1 }
		subtract_from_temp_variable = { tmp = @free_trade_min_export }
		set_temp_variable = { law_available_resources^0 = tmp }
		set_temp_variable = { tmp = 1 }
		subtract_from_temp_variable = { tmp = @export_focus_min_export }
		set_temp_variable = { law_available_resources^1 = tmp }
		set_temp_variable = { tmp = 1 }
		subtract_from_temp_variable = { tmp = @limited_exports_min_export }
		set_temp_variable = { law_available_resources^2 = tmp }
		set_temp_variable = { tmp = 1 }
		subtract_from_temp_variable = { tmp = @closed_economy_min_export }
		set_temp_variable = { law_available_resources^3 = tmp }

		###	Current trade law

			if = { limit = { has_idea = free_trade } set_temp_variable = { current_law = @free_trade } set_temp_variable = { prev_law = -1 } set_temp_variable = { next_law = @export_focus } }
			else_if = { limit = { has_idea = export_focus } set_temp_variable = { current_law = @export_focus } set_temp_variable = { prev_law = @free_trade } set_temp_variable = { next_law = @limited_exports } }
			else_if = { limit = { has_idea = limited_exports} set_temp_variable = { current_law = @limited_exports } set_temp_variable = { prev_law = @export_focus } set_temp_variable = { next_law = @closed_economy } }
			else_if = { limit = { has_idea = closed_economy } set_temp_variable = { current_law = @closed_economy } set_temp_variable = { prev_law = @closed_economy } set_temp_variable = { next_law = -1 } }

		###

			if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "current_law=[?current_law]" }
			if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "prev_law=[?prev_law]" }
			if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "next_law=[?next_law]" }

		###

	### Resource vars

		clear_temp_array = produced_resources
		clear_temp_array = imported_resources
		clear_temp_array = consumed_resources
		clear_temp_array = need_resources
		clear_temp_array = resources
		
		add_to_temp_array = { produced_resources = resource_produced@aluminium }
		add_to_temp_array = { produced_resources = resource_produced@rubber }
		add_to_temp_array = { produced_resources = resource_produced@tungsten }
		add_to_temp_array = { produced_resources = resource_produced@steel }
		add_to_temp_array = { produced_resources = resource_produced@chromium }

		add_to_temp_array = { imported_resources = resource_imported@aluminium }
		add_to_temp_array = { imported_resources = resource_imported@rubber }
		add_to_temp_array = { imported_resources = resource_imported@tungsten }
		add_to_temp_array = { imported_resources = resource_imported@steel }
		add_to_temp_array = { imported_resources = resource_imported@chromium }

		add_to_temp_array = { consumed_resources = resource_consumed@aluminium }
		add_to_temp_array = { consumed_resources = resource_consumed@rubber }
		add_to_temp_array = { consumed_resources = resource_consumed@tungsten }
		add_to_temp_array = { consumed_resources = resource_consumed@steel }
		add_to_temp_array = { consumed_resources = resource_consumed@chromium }

		add_to_temp_array = { resources = resource@aluminium }
		add_to_temp_array = { resources = resource@rubber }
		add_to_temp_array = { resources = resource@tungsten }
		add_to_temp_array = { resources = resource@steel }
		add_to_temp_array = { resources = resource@chromium }

	### Current and next law deficit % per resource

		clear_temp_array = deficit_pcts
		clear_temp_array = next_deficit_pcts
		clear_temp_array = prev_deficit_pcts
		for_each_loop = { array = resources

			if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = ">>>> resource=[?i]" }

			### Current deficit pct

				# Total need
				set_temp_variable = { total_need = consumed_resources^i }
				if = { limit = { check_variable = { resources^i < 0 } }
					set_temp_variable = { deficit = resources^i }
					multiply_temp_variable = { deficit = -1 }
					add_to_temp_variable = { total_need = deficit }
				}
				else = { set_temp_variable = { deficit = 0 } }
				add_to_temp_array = { need_resources = total_need }
				if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "total_need=[?total_need]" }
				if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "deficit=[?deficit]" }
		
				# Deficit %
				set_temp_variable = { deficit_pct = deficit }
				divide_temp_variable = { deficit_pct = total_need }	
				add_to_temp_array = { deficit_pcts = deficit_pct }
				if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "deficit_pct=[?deficit_pct]" }

				# Current available resources
				set_temp_variable = { cur_res = produced_resources^i }
				multiply_temp_variable = { cur_res = law_available_resources^current_law }
				add_to_temp_variable = { cur_res = imported_resources^i }

			### Next deficit pct

				if = { limit = { NOT = { check_variable = { next_law = -1 } } }

					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "deficit change from downgrade" }
				
					# Available res on new law
					set_temp_variable = { new_res = produced_resources^i }
					multiply_temp_variable = { new_res = law_available_resources^next_law }
					add_to_temp_variable = { new_res = imported_resources^i }
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "new_res=[?new_res]" }

					# New deficit
					set_temp_variable = { new_deficit = new_res }
					subtract_from_temp_variable = { new_deficit = total_need }
					if = { limit = { check_variable = { new_deficit < 0 } }
						multiply_temp_variable = { new_deficit = -1 }
					}
					else = { set_temp_variable = { new_deficit = 0 } }
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "new_deficit=[?new_deficit]" }

					# New deficit %
					set_temp_variable = { new_deficit_pct = new_deficit }
					divide_temp_variable = { new_deficit_pct = total_need }
					add_to_temp_array = { next_deficit_pcts = new_deficit_pct }
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "new_deficit_pct=[?new_deficit_pct]" }
				}

			### Prev deficit pct

				if = { limit = { NOT = { check_variable = { prev_law = -1 } } }

					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "deficit change from upgrade" }
				
					# Available res on new law
					set_temp_variable = { new_res = produced_resources^i }
					multiply_temp_variable = { new_res = law_available_resources^prev_law }
					add_to_temp_variable = { new_res = imported_resources^i }
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "new_res=[?new_res]" }

					# New deficit
					set_temp_variable = { new_deficit = new_res }
					subtract_from_temp_variable = { new_deficit = total_need }
					if = { limit = { check_variable = { new_deficit < 0 } }
						multiply_temp_variable = { new_deficit = -1 }
					}
					else = { set_temp_variable = { new_deficit = 0 } }
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "new_deficit=[?new_deficit]" }

					# New deficit %
					set_temp_variable = { new_deficit_pct = new_deficit }
					divide_temp_variable = { new_deficit_pct = total_need }
					add_to_temp_array = { prev_deficit_pcts = new_deficit_pct }
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "new_deficit_pct=[?new_deficit_pct]" }
				}

			###
		}

	### Worth changing law?

		### Track need over time

			### Lower

				if = { limit = {  NOT = { check_variable = { next_law = -1 } } }
				
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "checking need to lower trade" }
	
					if = {
						limit = {
							any_of = { array = resources
								NOT = { check_variable = { need_resources^i < @EAI_RESOURCE_min_total_need_to_care_about_resource } }
								set_temp_variable = { deficit_pct_diff = next_deficit_pcts^i }
								subtract_from_temp_variable = { deficit_pct_diff = deficit_pcts^i }
								if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "deficit_pct_diff=[?deficit_pct_diff]" }
								NOT = { check_variable = { deficit_pct_diff > @EAI_RESOURCE_min_deficit_pct_diff_for_lower_law } }
							}
						}
					
						add_to_variable = { EAI_RESOURCE_downgrade_trade_law_demand = 1 }
						if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = ">>>>>>>>> worth lowering law demand=[?EAI_RESOURCE_downgrade_trade_law_demand]" }
					}
					else = { subtract_from_variable = { EAI_RESOURCE_downgrade_trade_law_demand = 1 } }
					clamp_variable = { var = EAI_RESOURCE_downgrade_trade_law_demand min = 0 max = @EAI_RESOURCE_downgrade_trade_law_threshold }
				}
				else = {
					clear_variable = EAI_RESOURCE_downgrade_trade_law_demand
				}

			### Higher

				if = { limit = { NOT = { check_variable = { prev_law = -1 } } }
				
					if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "checking need to raise trade" }
	
					if = {
						limit = {
							NOT = {
								any_of = { array = resources
									NOT = { check_variable = { need_resources^i < @EAI_RESOURCE_min_total_need_to_care_about_resource } }
									set_temp_variable = { deficit_pct_diff = prev_deficit_pcts^i }
									subtract_from_temp_variable = { deficit_pct_diff = deficit_pcts^i }
									if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = "deficit_pct_diff=[?deficit_pct_diff]" }
									check_variable = { deficit_pct_diff > @EAI_RESOURCE_max_deficit_pct_diff_for_higher_law }
								}
							}
						}
					
						add_to_variable = { EAI_RESOURCE_upgrade_trade_law_demand = 1 }
						if = { limit = { has_country_flag = EAI_detailed_resources_logging } log = ">>>>>>>>> worth raising law demand=[?EAI_RESOURCE_upgrade_trade_law_demand]" }
					}
					else = { subtract_from_variable = { EAI_RESOURCE_upgrade_trade_law_demand = 1 } }
					clamp_variable = { var = EAI_RESOURCE_upgrade_trade_law_demand min = 0 max = @EAI_RESOURCE_upgrade_trade_law_threshold }
				}
				else = {
					clear_variable = EAI_RESOURCE_upgrade_trade_law_demand
				}

			###

		### Upgrade law if exceeded demand threshold

			if = { limit = { NOT = { check_variable = { EAI_RESOURCE_downgrade_trade_law_demand < @EAI_RESOURCE_downgrade_trade_law_threshold } } } 
				if = { limit = { has_country_flag = EAI_resources_logging NOT = { check_variable = { EAI_LAW_get_trade_law = next_law } } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCE: over downgrade threshold EAI_LAW_get_trade_law=[?EAI_LAW_get_trade_law]" }
				set_variable = { EAI_LAW_get_trade_law = next_law }
			}
			else_if = { limit = { NOT = { check_variable = { EAI_RESOURCE_upgrade_trade_law_demand < @EAI_RESOURCE_upgrade_trade_law_threshold } } } 
				if = { limit = { has_country_flag = EAI_resources_logging NOT = { check_variable = { EAI_LAW_get_trade_law = prev_law } } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | RESOURCE: over upgrade threshold EAI_LAW_get_trade_law=[?EAI_LAW_get_trade_law]" }
				set_variable = { EAI_LAW_get_trade_law = prev_law }
			}
			else = { set_variable = { EAI_LAW_get_trade_law  = -1 } }

		###

	###
}

# Estimate max fuel consumption based on its armor, air and navy
EAI_RESOURCE_calculate_fuel_need = {

	### air

	set_temp_variable = { air_force_size = num_deployed_planes }

	multiply_temp_variable = { air_force_size = 1.8 } # fuel consumption estimate

	### land

	set_temp_variable = { armor_size = 0 }

	set_temp_variable = { armor_size = num_battalions_with_type@medium_armor }
	add_to_temp_variable = { armor_size = num_battalions_with_type@medium_sp_anti_air_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@medium_sp_artillery_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@medium_tank_destroyer_brigade }

	add_to_temp_variable = { armor_size = num_battalions_with_type@heavy_armor }
	add_to_temp_variable = { armor_size = num_battalions_with_type@heavy_sp_anti_air_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@heavy_sp_artillery_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@heavy_tank_destroyer_brigade }

	add_to_temp_variable = { armor_size = num_battalions_with_type@modern_armor }
	add_to_temp_variable = { armor_size = num_battalions_with_type@modern_sp_anti_air_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@modern_sp_artillery_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@modern_tank_destroyer_brigade }

	add_to_temp_variable = { armor_size = num_battalions_with_type@light_armor }
	add_to_temp_variable = { armor_size = num_battalions_with_type@light_sp_anti_air_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@light_sp_artillery_brigade }
	add_to_temp_variable = { armor_size = num_battalions_with_type@light_tank_destroyer_brigade }

	multiply_temp_variable = { armor_size = 50 } # fuel consumption estimate

	### navy

	set_temp_variable = { navy_size = num_ships }

	multiply_temp_variable = { navy_size = 60 } # fuel consumption estimate

	### total

	set_variable = { EAI_max_fuel_consumption_estimate = air_force_size }
	add_to_variable = { EAI_max_fuel_consumption_estimate = armor_size }
	add_to_variable = { EAI_max_fuel_consumption_estimate = navy_size }
}