OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / test / grid_snap_gate.gd

79 lines · gd ref:
extends Node
## HEADLESS GATE — a cut lands on the lattice the MAP was authored on.
##
## The claim I made and then had to correct: "snapping goes through the world origin
## while the map is recentred, so cuts land off-lattice and get refused". Measured on
## START.MAP, map_center is (736, -88, -1704) — the importer snaps it to Quake's
## 8-unit grid for DC voxel alignment — so grids of 1, 2, 4 and 8 units were ALREADY
## right. Only 16, 32 and 64 drift, and only on the axes that do not divide.
##
## This asserts both halves of that: the fine grids are unchanged (so the fix is not
## silently moving cuts that were already correct), and the coarse ones now land on
## the map lattice where naive snapping did not.
##
## Run: "$HYPER_TPS_GODOT" --headless res://scenes/test/grid_snap_gate.tscn

const MapDCLevel := preload("res://scenes/maps/map_dc_level.gd")
const WORLD_SCALE := 0.028
## START.MAP, measured.
const MAP_CENTRE := Vector3(736, -88, -1704)

var fails := 0


func _ok(what: String, cond: bool) -> void:
	if not cond:
		fails += 1
	print("  %-58s %s" % [what, "OK" if cond else "**FAIL**"])


## Where does this world offset sit on the map's lattice, in Quake units?
func _quake_units(d: float, n: Vector3) -> float:
	return d / WORLD_SCALE + n.dot(MAP_CENTRE)


func _ready() -> void:
	print("=== Grid snap gate ===\n")
	var axes := {"x": Vector3.RIGHT, "y": Vector3.UP, "z": Vector3.BACK}

	print("-- every grid size lands on an integer multiple of itself, in QUAKE units --")
	for units: int in [1, 2, 4, 8, 16, 32, 64]:
		var worst := 0.0
		for name: String in axes:
			var n: Vector3 = axes[name]
			# A few arbitrary aim points, none of them on a lattice line.
			for raw: float in [0.137, -2.611, 13.4499, -37.02]:
				var d: float = MapDCLevel.snap_to_map_grid(raw, n, units, MAP_CENTRE, Vector3.ZERO)
				var q: float = _quake_units(d, n)
				worst = maxf(worst, absf(q - roundf(q / float(units)) * float(units)))
		_ok("grid %d units snaps onto the map lattice (err %.6f u)" % [units, worst], worst < 1e-3)

	print("\n-- the fine grids were ALREADY right, so the fix must not move them --")
	for units: int in [1, 2, 4, 8]:
		var same := true
		for name: String in axes:
			var n: Vector3 = axes[name]
			for raw: float in [0.137, -2.611, 13.4499]:
				var step: float = float(units) * WORLD_SCALE
				var naive: float = roundf(raw / step) * step
				var fixed: float = MapDCLevel.snap_to_map_grid(raw, n, units, MAP_CENTRE, Vector3.ZERO)
				if absf(naive - fixed) > 1e-4:
					same = false
		_ok("grid %d units is unchanged by the fix" % units, same)

	print("\n-- and the coarse grids REALLY were wrong (this is the bug) --")
	# map_center.y = -88: a multiple of 8, not of 16. So on Y the naive snap is half a
	# step off the map lattice at 16 units, and worse at 32 and 64.
	for units: int in [16, 32, 64]:
		var n := Vector3.UP
		var step: float = float(units) * WORLD_SCALE
		var naive: float = roundf(13.4499 / step) * step
		var fixed: float = MapDCLevel.snap_to_map_grid(13.4499, n, units, MAP_CENTRE, Vector3.ZERO)
		var naive_q: float = _quake_units(naive, n)
		var off: float = absf(naive_q - roundf(naive_q / float(units)) * float(units))
		print("      grid %2d u on Y: naive is %.1f units off the map lattice" % [units, off])
		_ok("grid %d units: the fix differs from naive" % units, absf(naive - fixed) > 1e-4)

	print("\n%s" % ("FAILED" if fails else "PASS"))
	get_tree().quit(1 if fails else 0)