Autopilot steers round what's in its way

A machine on autopilot looks a few metres ahead and swerves to the clearer side of a building, rock, person or
machine, so it goes round things instead of through them; it still touches nothing, so what it can't get round it
passes through, and it can't be wedged or tipped. It rolls over the deck rails. -demoauto counts swerves and hops.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-10 20:25:01 +06:00
parent 0db89bf7cb
commit e186a7e883
3 changed files with 58 additions and 10 deletions

View File

@ -36,9 +36,49 @@ namespace GrassCutter
int _goalKey = -1;
Vector3 _unloadAt;
/// <summary>An autopilot touches nothing: it rides on the soil and on the base deck, through everything else.</summary>
/// <summary>
/// An autopilot touches nothing, so nothing can wedge it, climb it or tip it: it rides on the soil and on the base
/// deck, steers round what stands in its way and goes through what it can't get round.
/// </summary>
const int GhostMask = ~0;
bool _ghost;
/// <summary>What it steers round (not the deck rails: it rolls over those).</summary>
const int SolidMask = (1 << Layers.Default) | (1 << Layers.Build) | (1 << Layers.Vehicle) | (1 << Layers.Player) | (1 << Layers.LocalPlayer);
bool _ghost, _avoiding;
/// <summary>For the demo: how often an autopilot steered round something, went through it, hopped.</summary>
public static int Swerves, Hops;
static readonly RaycastHit[] s_ahead = new RaycastHit[8];
bool Solid(Collider c) => c && !c.transform.IsChildOf(transform) && !(c is BoxCollider && c.GetComponent<DeckTileView>());
/// <summary>How far ahead the way is blocked this way (MaxValue: it's clear), not counting what's near the goal.</summary>
float Blocked(Vector3 from, Vector3 dir, float radius, float reach, Vector3 goal)
{
int n = Physics.SphereCastNonAlloc(from, radius, dir, s_ahead, reach, SolidMask, QueryTriggerInteraction.Ignore);
float best = float.MaxValue;
for (int i = 0; i < n; i++)
{
var h = s_ahead[i];
if (!Solid(h.collider) || h.distance <= 0f) continue;
if ((h.point - goal).sqrMagnitude < 36f) continue;
best = Mathf.Min(best, h.distance);
}
return best;
}
/// <summary>Steering to take it round what stands ahead: toward the clearer side, harder the closer it is.</summary>
float Avoid(Vector3 p, Vector3 up, Vector3 fwd, float dist, Vector3 goal)
{
float reach = Mathf.Min(9f, dist - 1.5f);
if (reach < 2f) return 0f;
var from = p + up * 1.1f;
float r = Mathf.Max(0.6f, width * 0.5f);
float ahead = Blocked(from, fwd, r, reach, goal);
if (ahead == float.MaxValue) return 0f;
float left = Blocked(from, Quaternion.AngleAxis(-40f, up) * fwd, r, reach, goal);
float right = Blocked(from, Quaternion.AngleAxis(40f, up) * fwd, r, reach, goal);
float side = left > right ? -1f : left < right ? 1f : (Vector3.SignedAngle(fwd, goal - p, up) < 0f ? -1f : 1f);
return side * Mathf.Lerp(1.4f, 0.5f, ahead / reach);
}
static readonly RaycastHit[] s_deckHits = new RaycastHit[8];
/// <summary>How high this wheel stands over the base deck's slab under it, if there is one (it keeps to the deck, not to what's on it).</summary>
@ -68,6 +108,7 @@ namespace GrassCutter
float d = to.magnitude;
if (d < 0.5f) return;
var dir = to / d;
Hops++;
var at = Planet.Surface(p + dir * Mathf.Min(metres, d));
var nu = Planet.Up(at);
// onto the base deck, not under it
@ -318,17 +359,23 @@ namespace GrassCutter
if (dist < 6f) thr = Mathf.Min(thr, 0.3f + dist * 0.08f);
// a sharp turn close by is taken slowly: the slower it goes, the tighter it turns
if (Mathf.Abs(ang) > 50f && dist < 10f) thr = Mathf.Min(thr, 0.22f);
// round what stands in the way; what it can't get round it goes through
float avoid = Avoid(p, up, fwd, dist, goal);
if (avoid == 0f) _avoiding = false;
else
{
if (!_avoiding) Swerves++;
_avoiding = true;
steer = Mathf.Clamp(steer + avoid, -1f, 1f);
thr = Mathf.Min(thr, 0.5f);
}
_lastSteer = steer;
// it drives through what's in its way; still not moving (wedged on a slope, sunk in something), it hops on
// nothing stops it, so not moving means wedged on the ground somehow: it hops on
if (speed < 0.5f && thr > 0.2f)
{
_stuck += dt;
if (_stuck > 2f)
{
_stuck = 0f;
Hop(goal, 4f);
}
if (_stuck > 3f) { _stuck = 0f; Hop(goal, 4f); }
}
else _stuck = 0f;
return new Vector2(steer, thr);

View File

@ -295,7 +295,7 @@ namespace GrassCutter
var up = Planet.Up(transform.position);
Body.AddForce(-up * GameDefs.LowGravity, ForceMode.Acceleration);
// on autopilot nothing stops it: it drives through buildings, rocks, props, people and machines, on the soil
// on autopilot it touches nothing: it steers round things and goes through what it can't get round
Ghost(auto);
// raycast springs at four corners (soil, deck, ramp)
@ -308,7 +308,7 @@ namespace GrassCutter
var local = new Vector3(Probes[i].x * width, 0f, Probes[i].y * length);
var wp = transform.TransformPoint(local);
float clearance = Planet.Altitude(wp);
if (auto) clearance = Mathf.Min(clearance, DeckClearance(wp, up, 1.2f + ride + 1.5f));
if (_ghost) clearance = Mathf.Min(clearance, DeckClearance(wp, up, 1.2f + ride + 1.5f));
else if (Physics.Raycast(wp + up * 1.2f, -up, out var sh, 1.2f + ride + 1.5f, groundMask, QueryTriggerInteraction.Ignore))
clearance = Mathf.Min(clearance, sh.distance - 1.2f);
float depth = ride - clearance;