Wagons on the rails, buildings on the slope, an autopilot nothing stops
- The train's loco and wagons stand on their front and back axles, so they pitch with the track up and down hills instead of standing level and hanging off one end; wheels sit on the rail tops, gauge matched to the models. - A piece set down on the soil leans with the ground under its footprint (up to 22 degrees) and sinks until no corner hangs in the air; foundations stay level. - On autopilot a machine touches nothing: it rides on the soil and the base deck and drives straight through buildings, rocks, rails, people and machines. Wedged anyway, it hops a few metres on; it no longer gives up on the way to unload. - -demoauto puts a building in the way to the yard, sets one down on the steepest clear slope and reports any tip-over. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -29,13 +29,56 @@ namespace GrassCutter
|
||||
/// <summary>Strokes as pairs of points on the ground: start, end, start, end...</summary>
|
||||
readonly List<Vector3> _strokes = new List<Vector3>();
|
||||
int _next, _field;
|
||||
float _stuck, _back, _backSteer, _lastSteer;
|
||||
float _stuck, _lastSteer;
|
||||
int _stuckTimes;
|
||||
/// <summary>The closest it has come to where it's going, and when: circling round a point inside its turn, it gives up on it.</summary>
|
||||
float _bestDist, _bestAt;
|
||||
int _goalKey = -1;
|
||||
Vector3 _unloadAt;
|
||||
|
||||
/// <summary>An autopilot touches nothing: it rides on the soil and on the base deck, through everything else.</summary>
|
||||
const int GhostMask = ~0;
|
||||
bool _ghost;
|
||||
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>
|
||||
static float DeckClearance(Vector3 wp, Vector3 up, float reach)
|
||||
{
|
||||
int n = Physics.RaycastNonAlloc(wp + up * 1.2f, -up, s_deckHits, reach, 1 << Layers.Default, QueryTriggerInteraction.Ignore);
|
||||
float best = float.MaxValue;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (s_deckHits[i].collider is BoxCollider && s_deckHits[i].collider.GetComponent<DeckTileView>())
|
||||
best = Mathf.Min(best, s_deckHits[i].distance - 1.2f);
|
||||
return best;
|
||||
}
|
||||
|
||||
void Ghost(bool on)
|
||||
{
|
||||
if (_ghost == on) return;
|
||||
_ghost = on;
|
||||
Body.excludeLayers = on ? (LayerMask)GhostMask : (LayerMask)0;
|
||||
}
|
||||
|
||||
/// <summary>The last way out: picks the machine up and sets it down a few metres on, facing where it's going.</summary>
|
||||
void Hop(Vector3 goal, float metres)
|
||||
{
|
||||
var p = transform.position;
|
||||
var up = Planet.Up(p);
|
||||
var to = Vector3.ProjectOnPlane(goal - p, up);
|
||||
float d = to.magnitude;
|
||||
if (d < 0.5f) return;
|
||||
var dir = to / d;
|
||||
var at = Planet.Surface(p + dir * Mathf.Min(metres, d));
|
||||
var nu = Planet.Up(at);
|
||||
// onto the base deck, not under it
|
||||
if (Ts && Ts.OnDeck(at)) { float c = DeckClearance(at + nu * 4f, nu, 8f); if (c < 50f) at += nu * (4f - c); }
|
||||
Body.position = at + nu * 0.6f;
|
||||
Body.rotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(dir, nu).normalized, nu);
|
||||
Body.linearVelocity = Vector3.zero;
|
||||
Body.angularVelocity = Vector3.zero;
|
||||
_hasCut = false;
|
||||
}
|
||||
|
||||
static readonly List<Vector2Int> s_fields = new List<Vector2Int>();
|
||||
static readonly Collider[] s_block = new Collider[4];
|
||||
|
||||
@ -256,10 +299,11 @@ namespace GrassCutter
|
||||
else if (dist < _bestDist - 1f) { _bestDist = dist; _bestAt = Time.time; }
|
||||
else if (Time.time - _bestAt > 7f)
|
||||
{
|
||||
// round and round a point it can't turn into: on to the next one (or the next stroke)
|
||||
// round and round a point it can't turn into: on to the next one (or the next stroke);
|
||||
// on the way to unload it gets there anyway, a hop at a time
|
||||
_goalKey = -1;
|
||||
if (job == AutoJob.Mowing) _next++;
|
||||
else if (++_stuckTimes >= 5) Auto.Value = (byte)AutoJob.Done;
|
||||
else Hop(goal, Mathf.Min(dist, 8f));
|
||||
return new Vector2(_lastSteer, 0.4f);
|
||||
}
|
||||
|
||||
@ -267,13 +311,6 @@ namespace GrassCutter
|
||||
float ang = Vector3.SignedAngle(fwd, to, up);
|
||||
float speed = Vector3.ProjectOnPlane(Body.linearVelocity, up).magnitude;
|
||||
|
||||
// backing out of something it ran into
|
||||
if (_back > 0f)
|
||||
{
|
||||
_back -= dt;
|
||||
return new Vector2(_backSteer, -1f);
|
||||
}
|
||||
|
||||
float steer = Mathf.Clamp(ang / 30f, -1f, 1f);
|
||||
// mowing it keeps to a pace it can turn at; on the way somewhere far it goes faster
|
||||
float cap = job == AutoJob.Mowing ? 0.7f : dist > 20f ? 1f : 0.6f;
|
||||
@ -283,21 +320,14 @@ namespace GrassCutter
|
||||
if (Mathf.Abs(ang) > 50f && dist < 10f) thr = Mathf.Min(thr, 0.22f);
|
||||
_lastSteer = steer;
|
||||
|
||||
// it drives through what's in its way; still not moving (wedged on a slope, sunk in something), it hops on
|
||||
if (speed < 0.5f && thr > 0.2f)
|
||||
{
|
||||
_stuck += dt;
|
||||
if (_stuck > 2.2f)
|
||||
if (_stuck > 2f)
|
||||
{
|
||||
_stuck = 0f;
|
||||
_back = 1.5f;
|
||||
_backSteer = ang > 0f ? -1f : 1f;
|
||||
if (++_stuckTimes >= 4)
|
||||
{
|
||||
// can't get there: the next stroke, or another way home
|
||||
_stuckTimes = 0;
|
||||
if (job == AutoJob.Mowing) _next += (_next % 2 == 0) ? 2 : 1;
|
||||
else Auto.Value = (byte)AutoJob.Done;
|
||||
}
|
||||
Hop(goal, 4f);
|
||||
}
|
||||
}
|
||||
else _stuck = 0f;
|
||||
|
||||
Reference in New Issue
Block a user