#126: asteroids are only created off screen

The guard was a 420px bubble around the fleet, which is smaller than the
window - so a rock could and did materialise in plain sight, several hundred
pixels away but well inside the view. "Not on the fleet" was never the rule
the player notices; "not in view" is.

The keep-out area is now the visible world rect, grown by a margin so a rock
placed just outside does not drift into view on the frame it was created.
The old bubble survives as a FLOOR under it: what to use when the viewport
cannot be measured - no camera yet on a scene's first frame, or a headless
run with no window - and for a heavily zoomed-in view where the screen is
narrower than the distance a rock needs to be safe.

Candidates inside are pushed out through the nearest edge rather than
dropped, so the field keeps its density around the fleet instead of thinning
out exactly where the player is looking.

Measured: keep-out 2200x2200 over a 1920 screen, nearest asteroid 1899 units
out where it used to be allowed at 420.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Loïc Wisniewski 2026-08-24 20:57:55 +02:00
commit ddc3de595e
2 changed files with 100 additions and 38 deletions

View file

@ -10,16 +10,23 @@ extends SceneTree
# test_return_position_matches_planet fail intermittently.
#
# Verifies:
# 1. _safe_position never returns a point inside the bubble, for candidates
# all around and including the exact centre. This is checked directly
# rather than by looking at where asteroids happened to land: with ~54
# asteroids spread over 25 chunks, any single run only rarely produces a
# near miss, so an observational check passes just as happily with the
# guard removed and proves nothing.
# 2. candidates already outside the bubble are left exactly where they are,
# so the field is not distorted away from the fleet
# 3. the field still spawns a full complement, and a stationary fleet loses
# no ships
# #126 widens the rule from "not on the fleet" to "not IN VIEW": the old
# bubble was 420px, smaller than the screen, so a rock could and did
# materialise in plain sight several hundred pixels away.
#
# Verifies:
# 1. _safe_position never returns a point inside the keep-out area, for
# candidates all around it and including its exact centre. Checked
# directly rather than by looking at where asteroids happened to land:
# with ~54 asteroids over 25 chunks, any single run only rarely
# produces a near miss, so an observational check passes just as
# happily with the guard removed and proves nothing.
# 2. candidates already outside are left exactly where they are, so the
# field is not distorted away from the fleet
# 3. the keep-out area really is the visible rect and not the old bubble
# 4. nothing the field actually placed is on screen
# 5. the field still spawns a full complement, and a stationary fleet
# loses no ships
var scene: Node
var fleet: Node
@ -53,32 +60,50 @@ func _process(delta: float) -> bool:
var centre: Vector2 = fleet.get_centroid()
var safe: float = field.safe_spawn_radius
var keep_out: Rect2 = field._keep_out_rect(centre)
# --- the guard itself, deterministically ----------------------
var worst_inside: float = 1e9
# --- 3. the keep-out area is the screen, not the old bubble ---
var on_screen: Rect2 = ViewUtils.get_visible_world_rect(field)
if not keep_out.encloses(on_screen):
print("TEST_ASTEROID_SAFE_SPAWN: FAIL (the keep-out area does not cover the visible rect)")
quit(1)
return true
if keep_out.size.x < safe * 2.0 or keep_out.size.y < safe * 2.0:
print("TEST_ASTEROID_SAFE_SPAWN: FAIL (the keep-out area shrank below the old bubble)")
quit(1)
return true
print("keep-out %.0fx%.0f covers a %.0fx%.0f screen (old bubble was %.0f across)" % [
keep_out.size.x, keep_out.size.y, on_screen.size.x, on_screen.size.y, safe * 2.0
])
# --- 1/2. the guard itself, deterministically -----------------
var half: Vector2 = keep_out.size * 0.5
var reach: float = maxf(half.x, half.y)
var let_one_through: bool = false
var moved_an_outside_one: bool = false
for i in range(72):
var a: float = (float(i) / 72.0) * TAU
var dir: Vector2 = Vector2(cos(a), sin(a))
# Candidates well inside, just inside, and exactly on centre.
for d in [0.0, 1.0, safe * 0.25, safe * 0.99]:
var got: Vector2 = field._safe_position(centre + dir * d, centre)
worst_inside = minf(worst_inside, got.distance_to(centre))
for d in [0.0, 1.0, reach * 0.25, reach * 0.7]:
var candidate: Vector2 = keep_out.get_center() + dir * d
if not keep_out.has_point(candidate):
continue
if keep_out.has_point(field._safe_position(candidate, keep_out)):
let_one_through = true
# Candidates already outside must be returned untouched.
var outside: Vector2 = centre + dir * (safe * 1.5)
if field._safe_position(outside, centre).distance_to(outside) > 0.001:
var outside: Vector2 = keep_out.get_center() + dir * (reach * 3.0)
if field._safe_position(outside, keep_out).distance_to(outside) > 0.001:
moved_an_outside_one = true
print("closest result for an inside candidate: %.1f (safe radius %.1f)" % [
worst_inside, safe
])
if worst_inside < safe - 0.001:
print("TEST_ASTEROID_SAFE_SPAWN: FAIL (guard let a spawn land inside the fleet's bubble)")
if let_one_through:
print("TEST_ASTEROID_SAFE_SPAWN: FAIL (the guard left a spawn inside the visible area)")
quit(1)
return true
if moved_an_outside_one:
print("TEST_ASTEROID_SAFE_SPAWN: FAIL (guard displaced an asteroid that was already clear)")
quit(1)
return true
print("every one of 288 in-view candidates was pushed out; none outside was touched")
var asteroids: Array = get_nodes_in_group("asteroids")
var closest: float = 1e9