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:
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user