using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
public partial class DemoBot
{
///
/// -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.
///
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() : 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();
}
}
}