package ru.dbotthepony.kstarbound.defs.`object`

import ru.dbotthepony.kommons.vector.Vector2i
import ru.dbotthepony.kstarbound.Registry
import ru.dbotthepony.kstarbound.defs.tile.TileDefinition
import ru.dbotthepony.kstarbound.world.World

data class Anchor(val isBackground: Boolean, val position: Vector2i, val isTilled: Boolean, val isSoil: Boolean, val anchorMaterial: Registry.Ref<TileDefinition>?) {
	fun isValidPlacement(world: World<*, *>, position: Vector2i): Boolean {
		val cell = world.chunkMap.getCell(position + this.position)

		if (!cell.isConnectible(false, isBackground))
			return false

		if (isTilled && !cell.tile(isBackground).modifier.value.tilled)
			return false

		if (isSoil && !cell.tile(isBackground).material.value.soil)
			return false

		if (anchorMaterial?.entry != null && cell.tile(isBackground).material != anchorMaterial.entry)
			return false

		return true
	}
}
