function BitOps.getConnectivityForFace(H,e)local d=0 local s=BitOps.CONNECTIVITY_INDEXES_FOR_FACE[tostring(BitOps.normalizeVec3(e))]for e,s in ipairs(s)do d=bit32.lshift(d,1)d=bit32.bor(d,bit32.extract(H,s))end return d end function BitOps.getVerticalFaceFromOccupancy(H)local e for d,s in pairs(BitOps.FACES)do e=s.Y~=0 if not e and H==BitOps.OCCUPANCY_FACE_MASK[tostring(s)]then return s end end return false end function BitOps.applyFaceOccupancy(H,e,d,s)local m=bit32.extract(d,BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(s))])if m==1 then H=bit32.replace(H,m,BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(e))])end return H end function BitOps.removeFaceOccupancy(H,e,d,s)local m=bit32.extract(d,BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(s))])if m==1 then H=bit32.replace(H,0,BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(e))])end return H end function BitOps.applyFaceConnectivity(H,e,d,s)local m=BitOps.getConnectivityForFace(d,s)local c=BitOps.getConnectivityForFace(H,e)local B=bit32.bor(c,m)H=BitOps.setFaceConnectivity(H,e,B)return H end
-- TODO: Optimize for vertical faces
function BitOps.getConnectivityForFace(connectivity, face)
	local result = 0

	local bitIndexes = BitOps.CONNECTIVITY_INDEXES_FOR_FACE[tostring(BitOps.normalizeVec3(face))]
	for _, index in ipairs(bitIndexes) do
		result = bit32.lshift(result, 1)
		result = bit32.bor(result, bit32.extract(connectivity, index))
	end

	return result
end

function BitOps.getVerticalFaceFromOccupancy(occupancy)
	local horizontalFace
	for _, face in pairs(BitOps.FACES) do
		horizontalFace = face.Y ~= 0
		if not horizontalFace and occupancy == BitOps.OCCUPANCY_FACE_MASK[tostring(face)] then
			return face
		end
	end

	return false
end

function BitOps.applyFaceOccupancy(targetOccupancy, targetFace, appliedOccupancy, appliedFace)
	local appliedOccupancyFaceBit =
		bit32.extract(appliedOccupancy, BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(appliedFace))])
	if appliedOccupancyFaceBit == 1 then
		targetOccupancy = bit32.replace(
			targetOccupancy,
			appliedOccupancyFaceBit,
			BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(targetFace))]
		)
	end

	return targetOccupancy
end

function BitOps.removeFaceOccupancy(targetOccupancy, targetFace, appliedOccupancy, appliedFace)
	local appliedOccupancyFaceBit =
		bit32.extract(appliedOccupancy, BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(appliedFace))])
	if appliedOccupancyFaceBit == 1 then
		targetOccupancy = bit32.replace(
			targetOccupancy,
			0,
			BitOps.OCCUPANCY_INDEX_FOR_FACE[tostring(BitOps.normalizeVec3(targetFace))]
		)
	end

	return targetOccupancy
end

function BitOps.applyFaceConnectivity(targetConnectivity, targetFace, appliedConnectivity, appliedFace)
	local appliedConnectivityFaceBits = BitOps.getConnectivityForFace(appliedConnectivity, appliedFace)
	local targetConnectivityFaceBits = BitOps.getConnectivityForFace(targetConnectivity, targetFace)
	local newTargetConnectivityFaceBits = bit32.bor(targetConnectivityFaceBits, appliedConnectivityFaceBits)

	targetConnectivity = BitOps.setFaceConnectivity(targetConnectivity, targetFace, newTargetConnectivityFaceBits)

	return targetConnectivity
end
