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:
@ -25,7 +25,9 @@ namespace GrassCutter
|
||||
const float Strip = 3.4f, Arm = 3f;
|
||||
/// <summary>How far out the side cutters reach at Mk I, either side of the track.</summary>
|
||||
const float Reach = 18f;
|
||||
const float WaitAtEnd = 6f, Hold = 6000f, Gap = 5.2f, Gauge = 1.5f;
|
||||
const float WaitAtEnd = 6f, Hold = 6000f, Gap = 5.2f, Gauge = 1.62f;
|
||||
/// <summary>The top of the rails above the track line: the models' wheels stand on their origin.</summary>
|
||||
const float RailTop = 0.27f;
|
||||
|
||||
/// <summary>The station it is laying track to (255: not chosen yet).</summary>
|
||||
public readonly NetworkVariable<byte> Target = new NetworkVariable<byte>(255);
|
||||
@ -47,7 +49,7 @@ namespace GrassCutter
|
||||
float Mul => (Piece ? Piece.TierMul : 1f) * (Ts ? Ts.BotSpeedMul : 1f);
|
||||
float MaxOut => Reach * (Piece ? Piece.TierMul : 1f);
|
||||
/// <summary>Where the train stands at home: far enough along for every wagon to be on the rails.</summary>
|
||||
float HomeAt => Mathf.Min(_length, (wagons != null ? wagons.Length : 0) * Gap + 1f);
|
||||
float HomeAt => Mathf.Min(_length, (wagons != null ? wagons.Length : 0) * Gap + 2.5f);
|
||||
float Inner => Strip * 0.5f + (Runs.Value - 1) * Arm;
|
||||
bool Laying => Laid.Value < _length - 0.01f;
|
||||
bool RouteDone => !Laying && Runs.Value > 0 && Inner >= MaxOut;
|
||||
@ -72,6 +74,10 @@ namespace GrassCutter
|
||||
var mf = t.GetComponent<MeshFilter>();
|
||||
float rad = mf && mf.sharedMesh ? Mathf.Max(0.1f, mf.sharedMesh.bounds.extents.y) : 0.4f;
|
||||
_wheels.Add((t, t.localRotation, rad));
|
||||
// how far the axles reach fore and aft: the model sits on the rails at its end axles
|
||||
var lp = Vector3.Scale(root.InverseTransformPoint(t.position), root.localScale);
|
||||
float z = Mathf.Abs((root.localRotation * lp).z);
|
||||
_axles[root] = Mathf.Max(_axles.TryGetValue(root, out var had) ? had : 0f, z);
|
||||
}
|
||||
else if (t.name == "Reel") { _reel = t; _reelRest = t.localRotation; }
|
||||
}
|
||||
@ -151,6 +157,14 @@ namespace GrassCutter
|
||||
_length = _cum[_cum.Count - 1];
|
||||
}
|
||||
|
||||
/// <summary>The point this far along the track, carried on straight past either end.</summary>
|
||||
Vector3 PosOn(float d)
|
||||
{
|
||||
if (d < 0f) return Pos(0f) - Ahead(0f) * -d;
|
||||
if (d > _length) return Pos(_length) + Ahead(_length) * (d - _length);
|
||||
return Pos(d);
|
||||
}
|
||||
|
||||
/// <summary>The point this far along the track.</summary>
|
||||
Vector3 Pos(float d)
|
||||
{
|
||||
@ -276,15 +290,27 @@ 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>();
|
||||
|
||||
readonly Dictionary<Transform, float> _axles = new Dictionary<Transform, float>();
|
||||
|
||||
/// <summary>
|
||||
/// Stands a model on the rails: its front and back axles both on the track, so it leans up and down the slopes
|
||||
/// with the rails instead of standing level and hanging off one end.
|
||||
/// </summary>
|
||||
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) * rest);
|
||||
float half = _axles.TryGetValue(t, out var ax) && ax > 0.2f ? ax : 1.5f;
|
||||
var back = PosOn(d - half);
|
||||
var front = PosOn(d + half);
|
||||
var f = front - back;
|
||||
if (f.sqrMagnitude < 1e-4f) f = Ahead(d);
|
||||
f.Normalize();
|
||||
var mid = (back + front) * 0.5f;
|
||||
var up = Vector3.ProjectOnPlane(Planet.Up(mid), f);
|
||||
if (up.sqrMagnitude < 1e-6f) up = Planet.Up(mid);
|
||||
up.Normalize();
|
||||
t.SetPositionAndRotation(mid + up * (onRails ? RailTop : 0f), Quaternion.LookRotation(f, up) * rest);
|
||||
}
|
||||
|
||||
// ───────────────────────── the track ─────────────────────────
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -278,7 +278,7 @@ namespace GrassCutter
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (!IsSpawned || !IsOwner || Body.isKinematic) return;
|
||||
if (!IsSpawned || !IsOwner || Body.isKinematic) { if (_ghost) Ghost(false); return; }
|
||||
bool driving = LocalDriving;
|
||||
var input = driving ? Ctl.Move : Vector2.zero;
|
||||
// the host drives a machine on autopilot
|
||||
@ -295,6 +295,8 @@ 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
|
||||
Ghost(auto);
|
||||
|
||||
// raycast springs at four corners (soil, deck, ramp)
|
||||
int wet = 0;
|
||||
@ -306,7 +308,8 @@ 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 (Physics.Raycast(wp + up * 1.2f, -up, out var sh, 1.2f + ride + 1.5f, groundMask, QueryTriggerInteraction.Ignore))
|
||||
if (auto) 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;
|
||||
if (depth > 0f)
|
||||
|
||||
Reference in New Issue
Block a user