class_name AdtChunkCoordinate
extends RefCounted

## Immutable MCNK chunk indices inside one ADT tile.
## `chunk_x` grows east and `chunk_y` grows south, matching the ADT header and
## tile conversion contract rather than Godot's X/Z member names.

const MINIMUM_CHUNK_INDEX: int = 0
const MAXIMUM_CHUNK_INDEX: int = 15

var tile_coordinate: AdtTileCoordinate:
	get:
		return _tile_coordinate

var chunk_x: int:
	get:
		return _chunk_x

var chunk_y: int:
	get:
		return _chunk_y

var _tile_coordinate: AdtTileCoordinate
var _chunk_x: int
var _chunk_y: int


## Creates chunk indices without clamping so invalid parser input is visible.
func _init(tile_coordinate_value: AdtTileCoordinate, chunk_x_value: int, chunk_y_value: int) -> void:
	_tile_coordinate = tile_coordinate_value
	_chunk_x = chunk_x_value
	_chunk_y = chunk_y_value


## Returns whether the tile and both chunk indices are inside standard bounds.
func is_within_map_grid() -> bool:
	return (
		_tile_coordinate != null
		and _tile_coordinate.is_within_map_grid()
		and _chunk_x >= MINIMUM_CHUNK_INDEX
		and _chunk_x <= MAXIMUM_CHUNK_INDEX
		and _chunk_y >= MINIMUM_CHUNK_INDEX
		and _chunk_y <= MAXIMUM_CHUNK_INDEX
	)
