# Needs $resource$ defined as a valid resource in game
# needs $ACCURACY$ defined as any positive integer
# gives out $resource$ as a variable
calc_resource_production = {
	set_variable = { which = $resource$ value = 0 }
	if = {
		limit = {
			planet_resource_compare = {
				resource = $resource$
				type = produces
				value > 0
			}
		}
		# arbitrary boundaries at 100k production
		set_variable = { which = low value = 0 }
		set_variable = { which = high value = 100000 }
		# 2 times 99% / 90% of the way is skipped by checking for 1k and 100
		if = {
			limit = {
				planet_resource_compare = {
					resource = $resource$
					type = produces
					value < 1000
				}
			}
			set_variable = { which = high value = 1000 }
		}
		if = {
			limit = {
				planet_resource_compare = {
					resource = $resource$
					type = produces
					value < 100
				}
			}
			set_variable = { which = high value = 100 }
		}
		# low_plus_x where x is the $ACCURACY$ we desire
		# lower $ACCURACY$ is more accurate
		# with x = 1 it takes 17 steps to arrive at a starting planets rough mineral production
		# with 100 steps accuracy to 2 decimal places can be achieved.
		set_variable = { which = low_plus_x value = low }
		change_variable = { which = low_plus_x value = 1 }
		#the counter just runs in the background to see how many calcs we had to made last time we checked
		# set_variable = { which = counter_$resource$ value = 0 } # used for debugging
		while = {
			limit = {
				check_variable = { which = high value > low_plus_x }
			}
			# mid = (low + high) / 2
			set_variable = { which = mid value = low }
			change_variable = { which = mid value = high }
			divide_variable = { which = mid value = 2 }
			# If produces >= mid -> narrow from below
			if = {
				limit = {
					planet_resource_compare = {
						resource = $resource$
						type = produces
						value >= mid
					}
				}
				set_variable = { which = low value = mid }
			}
			else = {
				set_variable = { which = high value = mid }
			}
			#set low_plus_x to new value and increment counter
			set_variable = { which = low_plus_x value = low }
			change_variable = { which = low_plus_x value = $ACCURACY$ }
			change_variable = { which = counter value = 1 }
		}
		# Final value is low
		set_variable = { which = $resource$ value = low }
		clear_variable = high
		clear_variable = mid
		clear_variable = low_plus_x
		clear_variable = low
		clear_variable = counter
	}
}
# Needs $resource$ defined as a valid resource in game
# needs $ACCURACY$ defined as any positive integer
# gives out $resource$ as a variable
calc_resource_upkeep = {
	if = {
		limit = {
			planet_resource_compare = {
				resource = $resource$
				type = upkeep
				value > 0
			}
		}
		# arbitrary boundaries at 100k upkeep
		set_variable = { which = low value = 0 }
		set_variable = { which = high value = 100000 }
		# low_plus_x where x is the $ACCURACY$ we desire
		# lower $ACCURACY$ is more accurate
		# with x = 1 it takes 17 steps to arrive at a starting planets rough mineral upkeep
		# with 100 steps accuracy to 2 decimal places can be achieved.
		set_variable = { which = low_plus_x value = low }
		change_variable = { which = low_plus_x value = 1 }
		#the counter just runs in the background to see how many calcs we had to made last time we checked
		# set_variable = { which = counter_$resource$ value = 0 } # used for debugging
		while = {
			limit = {
				check_variable = { which = high value > low_plus_x }
			}
			# mid = (low + high) / 2
			set_variable = { which = mid value = low }
			change_variable = { which = mid value = high }
			divide_variable = { which = mid value = 2 }
			# If upkeep >= mid -> narrow from below
			if = {
				limit = {
					planet_resource_compare = {
						resource = $resource$
						type = upkeep
						value >= mid
					}
				}
				set_variable = { which = low value = mid }
			}
			else = {
				set_variable = { which = high value = mid }
			}
			#set low_plus_x to new value and increment counter
			set_variable = { which = low_plus_x value = low }
			change_variable = { which = low_plus_x value = $ACCURACY$ }
			change_variable = { which = counter value = 1 }
		}
		# Final value is low
		set_variable = { which = $resource$ value = low }
		clear_variable = high
		clear_variable = mid
		clear_variable = low_plus_x
		clear_variable = low
		clear_variable = counter
	}
}
# Needs $resource$ defined as a valid resource in game
# needs $ACCURACY$ defined as any positive integer
# gives out $resource$ as a variable
calc_resource_balance = {
	if = {
		limit = {
			planet_resource_compare = {
				resource = $resource$
				type = balance
				value > 0
			}
		}
		# arbitrary boundaries at 100k balance
		set_variable = { which = low value = 0 }
		set_variable = { which = high value = 100000 }
		# low_plus_x where x is the $ACCURACY$ we desire
		# lower $ACCURACY$ is more accurate
		# with x = 1 it takes 17 steps to arrive at a starting planets rough mineral balance
		# with 100 steps accuracy to 2 decimal places can be achieved.
		set_variable = { which = low_plus_x value = low }
		change_variable = { which = low_plus_x value = 1 }
		#the counter just runs in the background to see how many calcs we had to made last time we checked
		# set_variable = { which = counter_$resource$ value = 0 } # used for debugging
		while = {
			limit = {
				check_variable = { which = high value > low_plus_x }
			}
			# mid = (low + high) / 2
			set_variable = { which = mid value = low }
			change_variable = { which = mid value = high }
			divide_variable = { which = mid value = 2 }
			# If balance >= mid -> narrow from below
			if = {
				limit = {
					planet_resource_compare = {
						resource = $resource$
						type = balance
						value >= mid
					}
				}
				set_variable = { which = low value = mid }
			}
			else = {
				set_variable = { which = high value = mid }
			}
			#set low_plus_x to new value and increment counter
			set_variable = { which = low_plus_x value = low }
			change_variable = { which = low_plus_x value = $ACCURACY$ }
			change_variable = { which = counter value = 1 }
		}
		# Final value is low
		set_variable = { which = $resource$ value = low }
		clear_variable = high
		clear_variable = mid
		clear_variable = low_plus_x
		clear_variable = low
		clear_variable = counter
	}
}

# Needs $resource$ defined as a valid resource in game
# needs $ACCURACY$ defined as any positive integer
# needs $type$ to be produces / upkeep / balance
# gives out $resource$ as a variable
calc_resource_type = {
	if = {
		limit = {
			planet_resource_compare = {
				resource = $resource$
				type = $type$
				value > 0
			}
		}
		# arbitrary boundaries at 100k balance
		set_variable = { which = low value = 0 }
		set_variable = { which = high value = 100000 }
		# low_plus_x where x is the $ACCURACY$ we desire
		# lower $ACCURACY$ is more accurate
		# with x = 1 it takes 17 steps to arrive at a starting planets rough mineral balance
		# with 100 steps accuracy to 2 decimal places can be achieved.
		set_variable = { which = low_plus_x value = low }
		change_variable = { which = low_plus_x value = 1 }
		#the counter just runs in the background to see how many calcs we had to made last time we checked
		# set_variable = { which = counter_$resource$ value = 0 } # used for debugging
		while = {
			limit = {
				check_variable = { which = high value > low_plus_x }
			}
			# mid = (low + high) / 2
			set_variable = { which = mid value = low }
			change_variable = { which = mid value = high }
			divide_variable = { which = mid value = 2 }
			# If balance >= mid -> narrow from below
			if = {
				limit = {
					planet_resource_compare = {
						resource = $resource$
						type = balance
						value >= mid
					}
				}
				set_variable = { which = low value = mid }
			}
			else = {
				set_variable = { which = high value = mid }
			}
			#set low_plus_x to new value and increment counter
			set_variable = { which = low_plus_x value = low }
			change_variable = { which = low_plus_x value = $ACCURACY$ }
			change_variable = { which = counter value = 1 }
		}
		# Final value is low
		set_variable = { which = $resource$ value = low }
		clear_variable = high
		clear_variable = mid
		clear_variable = low_plus_x
		clear_variable = low
		clear_variable = counter
	}
}