extends Node

class_name World

var _data: Dictionary
var _heightmap: Array



func _init(width, height):
	_data["width"] = width
	_data["height"] = height
	
	for x in range(_data["width"]):
		_heightmap.append([])
		for y in range(_data["height"]):
			_heightmap[x].append(0)

func generate_world():
	randomize()
	var noise = FastNoiseLite.new()
	noise.seed = randi()
	
	for x in _data["width"]:
		for y in _data["width"]:
			var wavelength = 3
			var width = 2048
			var height = 2048
			var terraces = 20

			# var border = border_width + rng.randf_range(-20.0, 20.0)
			var elevation = noise.get_noise_2d(x / wavelength, y / wavelength)

			var nx = 2 * x / width - 1
			var ny = 2 * y / height - 1

			var radius = remap(elevation, -1, 1, 0.8, 0.9)
			var distance = 1 - (1-pow(nx, 2)) * (1-pow(ny,2))
			distance = sqrt(pow(nx, 2) + pow(ny, 2))
#			if distance > radius:
#				elevation = (elevation - remap(distance, radius, 1.0, 0.0, 1.0))

			elevation = max(elevation, -1)
					

			if elevation > 0.1:
				elevation = max(pow((elevation) * 1.2, 1.5), 0.1)
			
			elevation = min(elevation, 1)
				
			elevation = (elevation * terraces)
			
#			print(elevation)
			_heightmap[x][y] = floor(elevation)


#func blocs_generate():
#	for x in _data["width"] - 1:
#		for y in _data["width"] - 1:
#			var corners = bloc_get_corners(Vector2(x, y))
#			_blocs[x][y]["height"] = corners.max()
#			_blocs[x][y]["shape"] = blocs_get_configuration_by_corners(corners)
#
#func blocs_get_bloc(point: Vector2):
#	return _blocs[point.x][point.y]
#
#func bloc_get_corners(bloc: Vector2):
#	return [
#		corners_get_corner(Vector2(bloc.x, bloc.y)),
#		corners_get_corner(Vector2(bloc.x + 1, bloc.y)),
#		corners_get_corner(Vector2(bloc.x + 1, bloc.y + 1)),
#		corners_get_corner(Vector2(bloc.x, bloc.y + 1))
#	]
#
#func blocs_get_configuration_by_corners(corners: Array):
#	var max = corners.max()
#	var result: int = 0
#	if corners[0] == max:
#		result += 8
#	if corners[1] == max:
#		result += 4
#	if corners[2] == max:
#		result += 2
#	if corners[3] == max:
#		result += 1
#	if result == 8 and corners[0] - corners[2] >= 2:
#		result = 16
#	if result == 4 and corners[1] - corners[3] >= 2:
#		result = 17
#	if result == 2 and corners[2] - corners[0] >= 2:
#		result = 18
#	if result == 1 and corners[3] - corners[1] >= 2:
#		result = 19
#
#	return result

func get_neighbours_8_at_same_height(point: Vector2):
	var neighbours = 0
	var current_bloc = _heightmap[point.x][point.y]
	if _heightmap[point.x-1][point.y-1] == current_bloc:
		neighbours += Global.directions_8.TOP_LEFT
	if _heightmap[point.x][point.y-1] == current_bloc:
		neighbours += Global.directions_8.TOP
	if _heightmap[point.x+1][point.y-1] == current_bloc:
		neighbours += Global.directions_8.TOP_RIGHT
	if _heightmap[point.x+1][point.y] == current_bloc:
		neighbours += Global.directions_8.RIGHT
	if _heightmap[point.x+1][point.y+1] == current_bloc:
		neighbours += Global.directions_8.BOTTOM_RIGHT
	if _heightmap[point.x][point.y+1] == current_bloc:
		neighbours += Global.directions_8.BOTTOM
	if _heightmap[point.x-1][point.y+1] == current_bloc:
		neighbours += Global.directions_8.BOTTOM_LEFT
	if _heightmap[point.x-1][point.y] == current_bloc:
		neighbours += Global.directions_8.LEFT
		
	return neighbours
	
func get_neighbours_4_at_same_height(point: Vector2):
	var neighbours = 0
	var current_bloc = _heightmap[point.x][point.y]
	if _heightmap[point.x][point.y-1] == current_bloc:
		neighbours += Global.directions_4.TOP
	if _heightmap[point.x+1][point.y] == current_bloc:
		neighbours += Global.directions_4.RIGHT
	if _heightmap[point.x][point.y+1] == current_bloc:
		neighbours += Global.directions_4.BOTTOM
	if _heightmap[point.x-1][point.y] == current_bloc:
		neighbours += Global.directions_4.LEFT
		
	return neighbours
	

func find_elevation(point: Vector2):
	return _heightmap[point.x][point.y]
