Train and autopilot tried in the game: prefab, wagons upright, no circling

The train piece is in the scene's prefab list, its icon is made, and the
wagons keep the axis turn their FBX root brings. The autopilot gives up on a
point it has circled for seven seconds without getting closer, and takes sharp
turns close by slowly. -demoauto: on the easy planet the tractor unloads and
goes back to mowing, and the train lays 826 m of track to Timothy, closes 532
sectors and earns about 13.7k by the time its reach is mown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-10 12:17:41 +06:00
parent df55b5e417
commit 29485df9e1
19 changed files with 6111 additions and 4879 deletions

View File

@ -273,14 +273,18 @@ namespace GrassCutter
}
}
/// <summary>How each model stands in its own frame (an FBX with one root object brings its axis turn on that root).</summary>
readonly Dictionary<Transform, Quaternion> _rest = new Dictionary<Transform, Quaternion>();
void Place(Transform t, float d, bool onRails)
{
if (!t) return;
if (!_rest.TryGetValue(t, out var rest)) _rest[t] = rest = t.localRotation;
var p = Pos(d);
var up = Planet.Up(p);
var f = Vector3.ProjectOnPlane(Ahead(d), up);
if (f.sqrMagnitude < 1e-6f) f = Vector3.ProjectOnPlane(transform.forward, up);
t.SetPositionAndRotation(p + up * (onRails ? 0.22f : 0f), Quaternion.LookRotation(f.normalized, up));
t.SetPositionAndRotation(p + up * (onRails ? 0.22f : 0f), Quaternion.LookRotation(f.normalized, up) * rest);
}
// ───────────────────────── the track ─────────────────────────

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b769e94835e00e2489dd6274ee0ecb85

View File

@ -31,6 +31,9 @@ namespace GrassCutter
int _next, _field;
float _stuck, _back, _backSteer, _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;
static readonly List<Vector2Int> s_fields = new List<Vector2Int>();
@ -242,12 +245,23 @@ namespace GrassCutter
var to = Vector3.ProjectOnPlane(goal - p, up);
float dist = to.magnitude;
if (job == AutoJob.Mowing && dist < 1.6f)
if (job == AutoJob.Mowing && dist < 2.2f)
{
_next++;
_stuckTimes = 0;
return new Vector2(_lastSteer, 0.4f);
}
int key = (int)job * 100000 + (job == AutoJob.Mowing ? _next : 0);
if (key != _goalKey) { _goalKey = key; _bestDist = dist; _bestAt = Time.time; }
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)
_goalKey = -1;
if (job == AutoJob.Mowing) _next++;
else if (++_stuckTimes >= 5) Auto.Value = (byte)AutoJob.Done;
return new Vector2(_lastSteer, 0.4f);
}
var fwd = Vector3.ProjectOnPlane(transform.forward, up).normalized;
float ang = Vector3.SignedAngle(fwd, to, up);
@ -265,6 +279,8 @@ namespace GrassCutter
float cap = job == AutoJob.Mowing ? 0.7f : dist > 20f ? 1f : 0.6f;
float thr = cap * Mathf.Lerp(1f, 0.4f, Mathf.Clamp01(Mathf.Abs(ang) / 90f));
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);
_lastSteer = steer;
if (speed < 0.5f && thr > 0.2f)

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f48ca0b3d49f54140ba770685b721579