Brute forcing elevation/rotation with four points of contact

I have come up with some pseudo-code to brute force leveling on uneven terrain between four height-adjusting landing legs. The problem is that the universe loves triangles and three points of contact, a limitation I refuse to bow to.
I can’t seem to think my way around how once the object is levelled, there is nothing stopping a single leg from hanging up in the air, and I’m struggling to think of a way around it. This is the flow as I currently have it.

increase = .1 per tick
tolerance = 5 degrees

function Level() runs multiple times until level == true
	Get the orientation vector, and get pitch and roll variables.

	if roll < tolerance
		leftLegs += increase
	else if roll > tolerance
		rightLegs += increase
	else
		rollCheck = true

	if pitch < tolerance
		frontLegs += increase
	else if pitch > tolerance
		backLegs += increase
	else
		pitchCheck = true

	if pitchCheck and rollCheck then
		level = true
		return

	Take the shortest leg and shorten it to the minimum length, then shorten all the others by an equal amount to ensure the legs stay as retracted as possible.

Please let me know if I’m missing something obvious here.

As part of a final if check could you not just fire a ray downwards from the end of each leg (or foot) if the landing legs have one and see what hits and how far?

I was hoping to solve this in a way that I can re-use it for robotics coding, but that is probably the best solution for when I do it on Roblox. Thanks for the help!