Train lines that grow, station yards, a cab to ride, a wagon per upgrade; autopilot unloads where it can

- The grass train lays on to the next station by itself once its newest stretch is mown as far as the cutters reach,
  rounding each station at the edge of its yard, until no station is left within reach; it runs three times as fast
  over the stretches already mown.
- Every station on a line has a yard at the end of the rails: machines (the autopilot too) unload there, the train
  picks it up as it passes and it is paid when the train gets home. With nothing left to cut it carries for the yards.
- E on the locomotive puts you in the cab: W on up the line, S back home, E to get off; it carries on by itself after.
- Three wagons, one more per upgrade (six at Mk IV), 2 t each; the heaps grow as they fill. The upgrade text says so.
- The autopilot only drives to places a machine can unload (garage pads, unload bays, the yard, train yards), not to
  drone pads, binders or catchers, where it used to stop and stand; at a spot that won't take it, it tries the next.
  The HUD unload markers skip those too.
- -demotrain checks the line growing, a yard filling and emptying, the cab ride and six wagons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-10 22:31:45 +06:00
parent e186a7e883
commit 86c8403c2a
22 changed files with 776 additions and 154 deletions

View File

@ -118,6 +118,12 @@ namespace GrassCutter
Vector3 _climbFrom, _climbTo;
float _jumpTapAt = -9f, _dashAt = -9f;
Vehicle _vehicle;
/// <summary>The train whose cab this player rides in, and the lever as last sent.</summary>
Train _train;
sbyte _lever;
public Train RidingTrain => _train;
/// <summary>In a seat of some kind: a machine's, or a train's cab.</summary>
bool Seated => _vehicle || _train;
public bool Flying { get; private set; }
public float CheatSpeed = 1f;
@ -126,7 +132,7 @@ namespace GrassCutter
public void SetFlying(bool on)
{
if (!IsOwner || _vehicle || Flying == on) return;
if (!IsOwner || Seated || Flying == on) return;
Flying = on;
Body.enabled = !on;
_vel = Vector3.zero;
@ -301,7 +307,7 @@ namespace GrassCutter
transform.rotation = _frame;
// everyone else sees a jump too, not a glide straight through the planet
if (_net && _net.IsSpawned && _settled) _net.Teleport(pos, _frame, transform.localScale);
Body.enabled = !Flying && !_vehicle;
Body.enabled = !Flying && !Seated;
_vel = Vector3.zero;
_climbT = -1f;
}
@ -383,15 +389,16 @@ namespace GrassCutter
}
Look();
if (_vehicle) UpdateRiding();
else if (_train) RideTrain();
else if (Flying) Fly();
else if (_climbT >= 0f) Climb();
else Move();
UpdateAim();
if (Ctl.InteractPressed) DoInteract();
// T on anything built opens its upgrades and switches (the hammer keeps T for moving pieces)
if (Ctl.MovePressed && !_vehicle && AimPiece && !(PlayerBuild.Local && PlayerBuild.Local.Active)) UIRoot.I?.OpenPiece(AimPiece);
if (Ctl.MovePressed && !Seated && AimPiece && !(PlayerBuild.Local && PlayerBuild.Local.Active)) UIRoot.I?.OpenPiece(AimPiece);
// never lost under the soil
if (!Flying && !_vehicle && Planet.Altitude(transform.position) < -3f) Teleport(Planet.Surface(transform.position) + Planet.Up(transform.position) * 0.1f);
if (!Flying && !Seated && Planet.Altitude(transform.position) < -3f) Teleport(Planet.Surface(transform.position) + Planet.Up(transform.position) * 0.1f);
if (Swimming.Value != _swimming) Swimming.Value = _swimming;
var cp = Tools.CutPoint;
var cut = Tools.IsCutting && Ts ? new Vector4(cp.x, cp.y, cp.z, Ts.ToolWidth(Tools.Current)) : Vector4.zero;
@ -405,14 +412,14 @@ namespace GrassCutter
var up = _frame * Vector3.up;
_frame = Quaternion.AngleAxis(l.x, up) * _frame;
_pitch = Mathf.Clamp(_pitch - l.y, -85f, 85f);
if (!_vehicle) transform.rotation = _frame;
if (!Seated) transform.rotation = _frame;
float flat = Vector3.ProjectOnPlane(_vel, up).magnitude;
float bob = (Grounded || _swimming) && !_vehicle ? Mathf.Sin(_bobPhase * 2f) * (_swimming ? 0.05f : 0.035f) * Mathf.Clamp01(flat / sprintSpeed + (_swimming ? 0.4f : 0f)) : 0f;
float bob = (Grounded || _swimming) && !Seated ? Mathf.Sin(_bobPhase * 2f) * (_swimming ? 0.05f : 0.035f) * Mathf.Clamp01(flat / sprintSpeed + (_swimming ? 0.4f : 0f)) : 0f;
float roll = -Ctl.Move.x * 1.5f;
if (_swimming) roll += Mathf.Sin(Time.time * 1.3f) * 2f;
_landDip = Mathf.Lerp(_landDip, 0f, Time.deltaTime * 8f);
head.localPosition = new Vector3(0, eyeHeight + bob - _landDip, 0);
if (_vehicle)
if (Seated)
{
// look is relative to the vehicle heading
head.rotation = _frame * Quaternion.Euler(_pitch, 0f, 0f);
@ -588,7 +595,7 @@ namespace GrassCutter
AimInteractable = null;
AimGrabbable = null;
HasAim = false;
if (!cam || _vehicle) return;
if (!cam || Seated) return;
var ray = new Ray(cam.transform.position, cam.transform.forward);
float tether = Ts ? Ts.Level(PerkId.BundleLine) * 1.5f : 0f;
if (Physics.Raycast(ray, out var hit, Reach + tether + (InGrass ? 0.6f : 0f), Layers.InteractMask, QueryTriggerInteraction.Ignore))
@ -606,6 +613,7 @@ namespace GrassCutter
void DoInteract()
{
if (_train) { _train.Leave(); return; }
if (_vehicle) { _vehicle.RequestExit(); return; }
if (Grab.Holding)
{
@ -639,9 +647,77 @@ namespace GrassCutter
}
else if (!_vehicle.LocalDriving) ExitVehicle();
if (_vehicle) FollowSeat();
// the train: the host says who is in its cab
if (!_train)
{
if (!_vehicle)
foreach (var t in Train.All)
if (t && t.IsSpawned && t.Rider.Value == OwnerClientId) { EnterTrain(t); break; }
}
else if (!_train.IsSpawned || _train.Rider.Value != OwnerClientId) ExitTrain();
if (_train) FollowCab();
Boom();
}
// ───────────────────────── the train's cab ─────────────────────────
void EnterTrain(Train t)
{
SetFlying(false);
_train = t;
_lever = 0;
Body.enabled = false;
_swimming = false;
_climbT = -1f;
Grab.Drop();
_heading = CabHeading(t);
_frame = _heading;
Sfx.Play("build_place", 0.5f, 1.3f);
Hud.Toast(Loc.T("In the cab: W forward · S back · E to get off"), Hud.ToastKind.Info);
}
void ExitTrain()
{
var t = _train;
_train = null;
_lever = 0;
var at = t ? t.StepOff : transform.position;
var up = Planet.Up(at);
var pos = Planet.Surface(at) + up * 0.1f;
if (Physics.Raycast(at + up * 2f, -up, out var hit, 6f, (1 << Layers.Default) | (1 << Layers.Build), QueryTriggerInteraction.Ignore)) pos = hit.point + up * 0.1f;
Teleport(pos);
}
/// <summary>The lever: W on up the line, S back toward home, neither to stop.</summary>
void RideTrain()
{
if (!_train) return;
var m = Ctl.Move;
sbyte lever = (sbyte)(m.y > 0.3f ? 1 : m.y < -0.3f ? -1 : 0);
if (lever != _lever)
{
_lever = lever;
_train.DriveRpc(lever);
}
CameraFx.FovTarget(lever != 0 ? 6f : 0f);
}
static Quaternion CabHeading(Train t)
{
var up = Planet.Up(t.CabFloor);
var f = Vector3.ProjectOnPlane(t.CabTurn * Vector3.forward, up);
return Quaternion.LookRotation(f.sqrMagnitude > 1e-6f ? f.normalized : Vector3.ProjectOnPlane(Vector3.forward, up).normalized, up);
}
void FollowCab()
{
transform.position = _train.CabFloor;
var heading = CabHeading(_train);
_frame = heading * Quaternion.Inverse(_heading) * _frame;
_heading = heading;
transform.rotation = heading;
}
/// <summary>
/// The chase view: the camera orbits the machine with the mouse, the wheel pulls it closer or further, and it never ends up
/// in the ground or behind a wall. Leaving the seat puts it back on the eyes.