class_name GATFormat


## Byte Length: 4 [br]
## GRAT
var signature: String = "GRAT"

## Byte Type: u8 [br]
## Byte Length: 2
var version: Version

## Byte Type: i32 [br]
## Byte Length: 4
var map_width: int

## Byte Type: i32 [br]
## Byte Length: 4
var map_height: int

## Byte Length: [member map_width] * [member map_height]
var tiles: Array[Tile]


static func from_bytes(bytes: ByteStream) -> GATFormat:
	var gat_format = GATFormat.new()
	
	bytes.advance(gat_format.signature.length())
	
	@warning_ignore("shadowed_variable")
	var version = Version.new()
	version.major = bytes.decode_u8()
	version.minor = bytes.decode_u8()
	gat_format.version = version
	
	gat_format.map_width = bytes.decode_s32()
	gat_format.map_height = bytes.decode_s32()
	
	gat_format.tiles = [] as Array[Tile]
	for _n in gat_format.map_width * gat_format.map_height:
		gat_format.tiles.append(Tile.from_bytes(bytes))
	
	return gat_format


class Tile:
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	## Orignal Coordinates_ (0, 0)
	var bottom_left_altitude: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	## Orignal Coordinates_ (1, 0)
	var bottom_right_altitude: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	## Orignal Coordinates_ (0, 1)
	var top_left_altitude: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	## Orignal Coordinates_ (1, 1)
	var top_right_altitude: int
	
	## Byte Type: u32 ?[br]
	## Byte Length: 4 ?
	var terrain_type: int
	
	
	func get_height_map() -> Dictionary: # Dictionary[Vector2, int]
		return {
			Vector2(0, 0): top_left_altitude,
			Vector2(1, 0): top_right_altitude,
			Vector2(0, 1): bottom_left_altitude,
			Vector2(1, 1): bottom_right_altitude,
		}
	
	
	static func from_bytes(bytes: ByteStream) -> Tile:
		var tile = Tile.new()
		
		tile.bottom_left_altitude = bytes.decode_float()
		tile.bottom_right_altitude = bytes.decode_float()
		tile.top_left_altitude = bytes.decode_float()
		tile.top_right_altitude = bytes.decode_float()
		tile.terrain_type = bytes.decode_u32()
		
		return tile