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

@ -0,0 +1,110 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
public partial class DemoBot
{
/// <summary>
/// -demotrain: a fresh planet; a grass train at Mk IV (six wagons), its first stretch finished at once so it lays on
/// to the next station, a full tractor unloading in the first station's yard, the train picking that up and bringing
/// it home, and the host riding in the cab and driving it.
/// </summary>
IEnumerator TrainDemo()
{
TeamState.DeleteSave();
yield return new WaitForSeconds(2f);
NetSession.Instance.HostLocal();
while (!Me || !Ts) yield return null;
yield return new WaitForSeconds(3f);
Clock(11f);
Ts.ServerSetMoney(500000d);
Quaternion Face(Vector3 p, Vector3 way) => Quaternion.LookRotation(Vector3.ProjectOnPlane(way, Planet.Up(p)), Planet.Up(p));
var sAt = Soil17(-40f, -70f);
var piece = Spawner.Instance.SpawnPiece(PieceId.Train, sAt, Face(sAt, Vector3.ProjectOnPlane(sAt - Planet.FromBase(0f, 0f) * Planet.Radius, Planet.Up(sAt))), sAt, true);
yield return new WaitForSeconds(1f);
var train = piece ? piece.GetComponent<Train>() : null;
if (!train) { Log("train MISSING"); Application.Quit(); yield break; }
piece.Tier.Value = GameDefs.MaxTier;
yield return new WaitForSeconds(0.5f);
int wagonsOn = 0;
foreach (var w in train.wagons) if (w && w.gameObject.activeSelf) wagonsOn++;
Log($"train: Mk {piece.Tier.Value + 1}, {train.wagons.Length} wagons made, {wagonsOn} on the rails, line {train.LineLength:0} m to {train.Stops} station(s)");
// the first stretch done at once: laid, and the side cutters out as far as they go
float first = train.LineLength;
train.State = new[] { (float)train.Target.Value, first, first, 99f, 0f };
yield return new WaitForSeconds(1f);
Log($"extended: {train.Stops} stations, line {train.LineLength:0} m (was {first:0}), laying {train.Laid.Value:0} m, runs {train.Runs.Value}");
// and the second laid too: it starts mowing that stretch on the way home
float whole = train.LineLength;
train.State = new[] { (float)train.Target.Value, whole, whole, 1f, 0f, (float)(train.Legs.Value & 0xFFFFFF), (float)((train.Legs.Value >> 24) & 0xFFFFFF) };
yield return new WaitForSeconds(0.5f);
var yards = new List<(Vector3 at, string name)>();
Train.Yards(yards);
Log($"yards: {yards.Count} ({string.Join(", ", yards.ConvertAll(y => y.name))})");
if (yards.Count == 0) { Application.Quit(); yield break; }
// a full tractor unloads in the first yard
var yAt = yards[0].at;
var tractor = Spawner.Instance.SpawnVehicle(VehicleKind.Tractor, Me.SaveId, yAt + Planet.Up(yAt) * 1.2f, Face(yAt, Vector3.right));
yield return new WaitForSeconds(1f);
if (tractor)
{
tractor.Tank.Value = tractor.Capacity;
float full = tractor.Tank.Value;
yield return new WaitForSeconds(8f);
float waiting = 0f;
foreach (var b in train.Bins) waiting += b;
Log($"tractor unloaded {full - tractor.Tank.Value:0} kg in the yard (unloading {tractor.Unloading}), {waiting:0} kg waiting for the train");
}
else Log("tractor NOT spawned");
// where the line turns round the first station
var up0 = Planet.Up(yAt);
yield return Look("train_junction", yAt + up0 * 45f + Vector3.ProjectOnPlane(Vector3.forward, up0).normalized * -30f, yAt, 1f);
// the train comes home through the yard, picks it up, and ships it all at the base
double money = Ts.Money.Value;
Time.timeScale = 8f;
float picked = 0f;
for (int i = 0; i < 90 && train.At.Value > 40f; i++)
{
yield return new WaitForSecondsRealtime(1f);
picked = Mathf.Max(picked, train.Hopper.Value);
}
yield return new WaitForSecondsRealtime(3f);
Time.timeScale = 1f;
Log($"home: at {train.At.Value:0} m, most on board {picked:0} kg, now {train.Hopper.Value:0} kg, earned {Ts.Money.Value - money:0}");
var lp = train.loco.position;
var lu = Planet.Up(lp);
yield return Look("train_six_wagons", lp + lu * 7f + Vector3.Cross(lu, train.loco.forward).normalized * 16f - train.loco.forward * 14f, lp - train.loco.forward * 14f, 1f);
// the host gets in the cab and drives it up the line
train.ServerBoard(Unity.Netcode.NetworkManager.Singleton.LocalClientId);
yield return new WaitForSeconds(1f);
Log($"in the cab: {(Me.RidingTrain == train ? "yes" : "NO")}, {Vector3.Distance(Me.transform.position, train.CabFloor):0.00} m from the cab floor");
float from = train.At.Value;
train.DriveRpc(1);
yield return new WaitForSeconds(4f);
Log($"driven: {train.At.Value - from:0} m on in 4 s, rider still {Vector3.Distance(Me.transform.position, train.CabFloor):0.00} m from the floor");
BotCapture.From = null;
yield return Shot("train_cab");
train.DriveRpc(0);
yield return new WaitForSeconds(0.5f);
float stopped = train.At.Value;
yield return new WaitForSeconds(1f);
Log($"lever back: moved {train.At.Value - stopped:0.0} m in a second");
train.Leave();
yield return new WaitForSeconds(1f);
Log($"off: riding {(Me.RidingTrain ? "STILL" : "no")}, {Vector3.Distance(Me.transform.position, train.loco.position):0.0} m from the loco, altitude {Planet.Altitude(Me.transform.position):0.00} m, rider {(train.Ridden ? "STILL" : "none")}");
yield return new WaitForSeconds(3f);
Log($"on its own again: at {train.At.Value:0} m, runs {train.Runs.Value}");
Log("train done");
Application.Quit();
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 242ecd8155aa4caf865239337df2727d

View File

@ -3013,6 +3013,11 @@ namespace GrassCutter
yield return Perf(int.Parse(perfSlot));
yield break;
}
if (CommandLine.Has("-demotrain"))
{
yield return TrainDemo();
yield break;
}
if (CommandLine.Has("-demoshop"))
{
yield return ShopDemo(int.TryParse(CommandLine.Get("-demoshop"), out int shopSlot) ? shopSlot : 0);