class_name BiomeDataClass
extends Node

var tree_collection = preload("res://Entities/Tree/Resources/TreeData.tres") as TreeDataCollection

# Calculate spawn probability for each tree type
func calculate_tree_probabilities(x: int, z: int) -> Dictionary:
	if Global.biome_data == null:
		Log.pr("Biome data not initialized. Call generate_environment_maps first.")
		return {}

	if x > Global.map_data.size() and x <= 0:
		if z > Global.map_data[x].size() and z <= 0:
			Log.pr("Coordinates out of bounds: (%d, %d)" % [x, z])
			return {}

	var moisture = Global.biome_data.moisture.get_pixel(x, z).r
	var temperature = Global.biome_data.temperature.get_pixel(x, z).r
	var elevation = Global.biome_data.elevation.get_pixel(x, z).r

	var probabilities = {}

	for tree in tree_collection.trees:
			# Calculate suitability for each environmental factor
			var moisture_suit = calculate_suitability(moisture, tree.moisture_range[0], tree.moisture_range[1])
			var temp_suit = calculate_suitability(temperature, tree.temperature_range[0], tree.temperature_range[1])
			var elev_suit = calculate_suitability(elevation, tree.elevation_range[0], tree.elevation_range[1])
			
			# Combined probability
			probabilities[tree.tree_name] = {}
			probabilities[tree.tree_name]['chance'] = moisture_suit * temp_suit * elev_suit
			probabilities[tree.tree_name]['resource'] = tree

	return probabilities

func calculate_suitability(value: float, min_pref: float, max_pref: float) -> float:
	if value >= min_pref and value <= max_pref:
			return 1.0
	else:
			var distance_outside = 0.0
			if value < min_pref:
					distance_outside = min_pref - value
			else:
					distance_outside = value - max_pref
			
			# Exponential decay - drops very quickly
			return max(0.01, exp(-distance_outside * 10.0))
