Map sectors show again, clients get their own machines and the train's swath, machines stay out of the ground

- Map: the sectors are wound the screen's way (most were wound the other way and culled, leaving a blank chart),
  kept inside the chart, and drawn as ball, sectors and marks layers; the scale bar is no longer sized mid-draw.
- Players' save keys are replicated, so a client's garage finds its machines and their upgrades; two players under
  one key get separate ones.
- The grass train sends its mowing to the clients; moving the depot or resizing the planet relays its line.
- A hard floor under vehicle springs: hard landings, steep rises and hand-overs no longer sink a machine.
- Tests: -demosink, -demorep host|client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-12 01:33:03 +06:00
parent 86c8403c2a
commit d61167e22b
10 changed files with 474 additions and 33 deletions

View File

@ -0,0 +1,110 @@
using System.Collections;
using UnityEngine;
namespace GrassCutter
{
public partial class DemoBot
{
/// <summary>
/// -demorep host|client: what a client sees of the host's world. A grass train at Mk IV mowing its line (the
/// grass it takes must go on the client too, the train must stand the same), the client buying a machine of its
/// own and an upgrade for it, and driving it and hopping off (it must stay on the ground).
/// </summary>
IEnumerator ReplicaDemo(bool host)
{
string me = host ? "host" : "client";
yield return new WaitForSeconds(2f);
if (host) { TeamState.DeleteSave(); NetSession.Instance.HostLocal(); }
else { yield return new WaitForSeconds(10f); NetSession.Instance.JoinLocal("127.0.0.1"); }
while (!Me || !Ts) yield return null;
Quaternion Face(Vector3 p, Vector3 way) => Quaternion.LookRotation(Vector3.ProjectOnPlane(way, Planet.Up(p)), Planet.Up(p));
var sAt = Soil17(-40f, -70f);
if (host)
{
Clock(11f);
Ts.ServerSetMoney(500000d);
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);
piece.Tier.Value = GameDefs.MaxTier;
var tr = piece.GetComponent<Train>();
yield return new WaitForSeconds(0.5f);
// its first stretch laid: it goes home mowing out either side
tr.State = new[] { (float)tr.Target.Value, tr.LineLength, tr.LineLength, 1f, 0f };
}
float t0 = Time.time;
while (PlayerAgent.All.Count < 2 && Time.time - t0 < 60f) yield return null;
Log($"{me}: {PlayerAgent.All.Count} players");
if (PlayerAgent.All.Count < 2) { Application.Quit(); yield break; }
yield return new WaitForSeconds(4f);
var other = PlayerAgent.All.Find(p => p != Me);
Log($"{me}: my save key '{Me.SaveId}', the other's '{other.SaveId}'");
// the train mows: the same strip on both
var train = Train.All.Count > 0 ? Train.All[0] : null;
if (!train) { Log($"{me}: no train"); Application.Quit(); yield break; }
MowMap.Count();
float mown0 = MowMap.MownArea();
float at0 = train.At.Value;
yield return new WaitForSeconds(12f);
MowMap.Count();
Log($"{me}: train went {at0 - train.At.Value:0} m, {MowMap.MownArea() - mown0:0} m² mown meanwhile, {train.Hopper.Value:0} kg on board");
var lp = train.loco.position;
var lu = Planet.Up(lp);
Log($"{me}: loco at {lp.x:0.00},{lp.y:0.00},{lp.z:0.00} leaning {Vector3.Angle(train.loco.up, lu):0.0} deg, {Planet.Altitude(lp):0.00} m up; wagon 1 at {(train.wagons.Length > 0 && train.wagons[0] ? train.wagons[0].position.ToString("F2") : "-")}, {train.LineLength:0} m of line");
Time.timeScale = 1f;
yield return Look(host ? "rep_host_train" : "rep_client_train", lp + lu * 8f + Vector3.Cross(lu, train.loco.forward).normalized * 15f + train.loco.forward * 10f, lp - train.loco.forward * 10f, 0.4f);
// the client buys a machine, finds it is its own, and upgrades it
Vehicle mine = null;
if (!host)
{
Ts.BuyVehicleRpc((byte)VehicleKind.Tractor, 0UL);
yield return new WaitForSeconds(3f);
mine = Vehicle.Mine(VehicleKind.Tractor, Me.SaveId);
Log($"client: bought a tractor, the garage sees it as mine: {(mine ? "yes" : "NO")}");
if (mine)
{
int was = mine.Level(TuneId.Motors);
Ts.TuneVehicleRpc(mine.NetworkObjectId, (byte)TuneId.Motors);
yield return new WaitForSeconds(1.5f);
Log($"client: motors {was} -> {mine.Level(TuneId.Motors)}");
}
}
else yield return new WaitForSeconds(4.5f);
yield return new WaitForSeconds(1f);
if (host)
{
var theirs = Vehicle.Mine(VehicleKind.Tractor, other.SaveId);
Log($"host: the client's tractor {(theirs ? "is here" : "MISSING")}, mine {(Vehicle.Mine(VehicleKind.Tractor, Me.SaveId) ? "too" : "none (right)")}");
}
// the client drives it and hops off; both watch how it stands
var v = Vehicle.All.Find(x => x && x.kind == VehicleKind.Tractor);
if (!host && v)
{
Me.Teleport(v.transform.position + v.transform.right * 3f + Planet.Up(v.transform.position));
yield return new WaitForSeconds(0.5f);
v.Interact();
yield return new WaitForSeconds(1f);
Ctl.BotMove = new Vector2(0.3f, 1f);
yield return new WaitForSeconds(5f);
Ctl.BotMove = Vector2.zero;
yield return new WaitForSeconds(1f);
v.RequestExit();
}
else yield return new WaitForSeconds(7.5f);
float low = 0f;
if (v) for (float t = 0f; t < 4f; t += Time.deltaTime) { low = Mathf.Min(low, Lowest(v)); yield return null; }
if (v) Log($"{me}: after the client hopped off, deepest {low:0.00} m, now {Lowest(v):0.00} m, driver {(v.HasDriver ? "STILL" : "none")}, owned by {(v.IsOwnedByServer ? "the host" : "a client")}, sinks caught here {Vehicle.Sinks}");
if (v)
{
var vp = v.transform.position;
var vu = Planet.Up(vp);
yield return Look(host ? "rep_host_tractor" : "rep_client_tractor", vp + vu * 2.5f + v.transform.right * 8f, vp, 0.3f);
}
yield return new WaitForSeconds(3f);
Log($"{me}: rep done");
Application.Quit();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b0d9abdb827442aea18206094c4586b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,116 @@
using System.Collections;
using UnityEngine;
namespace GrassCutter
{
public partial class DemoBot
{
/// <summary>
/// -demosink: a tractor on the steepest bit of meadow round the base. Each round drives it up the slope, hops off,
/// and drops it from eight metres; the log says how far below the ground its lowest corner or its nose went. The
/// first round has the hard floor off (how it was), the second on.
/// </summary>
IEnumerator SinkDemo()
{
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(100000d);
// the steepest clear slope within 70 m
Vector3 steep = Soil17(60f, 40f), downhill = Vector3.forward;
float worst = -1f;
for (int a = 0; a < 48; a++)
for (float r = 30f; r <= 70f; r += 8f)
{
var q = Soil17(Mathf.Cos(a * 7.5f * Mathf.Deg2Rad) * r, Mathf.Sin(a * 7.5f * Mathf.Deg2Rad) * r);
var u = Planet.Up(q);
var e1 = Vector3.Cross(u, Vector3.forward).normalized;
var e2 = Vector3.Cross(u, e1);
float h1 = Planet.Altitude(q + e1 * 2f + u * 0f) - Planet.Altitude(q - e1 * 2f);
float h2 = Planet.Altitude(q + e2 * 2f) - Planet.Altitude(q - e2 * 2f);
float slope = Mathf.Atan2(new Vector2(h1, h2).magnitude, 4f) * Mathf.Rad2Deg;
if (slope > worst && !Ts.OnDeck(q) && !(Ground.Instance && Ground.Instance.RockAt(q)) && !Physics.CheckSphere(q + u * 3f, 9f, 1 << Layers.Default, QueryTriggerInteraction.Ignore))
{
worst = slope; steep = q;
// altitude grows the way the ground falls away under the probe, so "uphill" is against it
downhill = (e1 * h1 + e2 * h2).normalized;
}
}
var up = Planet.Up(steep);
var start = steep + downhill * 18f;
start = Planet.Surface(start);
var uphill = Vector3.ProjectOnPlane(-downhill, Planet.Up(start)).normalized;
Log($"slope {worst:0} deg at {steep}");
var v = Spawner.Instance.SpawnVehicle(VehicleKind.Tractor, Me.SaveId, start + Planet.Up(start) * 0.6f, Quaternion.LookRotation(uphill, Planet.Up(start)));
yield return new WaitForSeconds(1.5f);
if (!v) { Log("tractor NOT spawned"); Application.Quit(); yield break; }
for (int round = 0; round < 2; round++)
{
Vehicle.NoFloor = round == 0;
string tag = round == 0 ? "floor OFF" : "floor ON ";
v.ServerPut(start + Planet.Up(start) * 0.6f, Quaternion.LookRotation(uphill, Planet.Up(start)));
yield return new WaitForSeconds(2f);
int sinks = Vehicle.Sinks;
// up the slope, flat out
v.Interact();
yield return new WaitForSeconds(0.6f);
Ctl.BotMove = Vector2.up;
float climb = 0f;
for (float t = 0f; t < 5f; t += Time.fixedDeltaTime) { climb = Mathf.Min(climb, Lowest(v)); yield return new WaitForFixedUpdate(); }
Ctl.BotMove = Vector2.zero;
yield return new WaitForSeconds(1f);
if (round == 1) yield return Look("sink_climb", v.transform.position + Planet.Up(v.transform.position) * 1.5f + v.transform.right * 7f, v.transform.position, 0.3f);
// hop off and watch it stand
v.RequestExit();
float off = 0f;
for (float t = 0f; t < 4f; t += Time.fixedDeltaTime) { off = Mathf.Min(off, Lowest(v)); yield return new WaitForFixedUpdate(); }
float rest = Lowest(v);
// and dropped from eight metres
var p = v.transform.position;
v.ServerPut(p + Planet.Up(p) * 8f, v.transform.rotation);
float land = 0f;
for (float t = 0f; t < 5f; t += Time.fixedDeltaTime) { land = Mathf.Min(land, Lowest(v)); yield return new WaitForFixedUpdate(); }
Log($"{tag}: deepest on the climb {climb:0.00} m, after hopping off {off:0.00} m (at rest {rest:0.00}), landing {land:0.00} m, caught {Vehicle.Sinks - sinks} times, now {Planet.Altitude(v.transform.position):0.00} m up");
}
Vehicle.NoFloor = false;
// and the map, close in, with the sectors round here
Cheats.Run("tool all");
Me.Teleport(v.transform.position + v.transform.right * 4f + Planet.Up(v.transform.position));
yield return Sweep(Me.transform.position, new Vector3(0f, 0f, 1.6f), 3f);
UIRoot.I.OpenMap();
yield return new WaitForSeconds(1.5f);
BotCapture.From = null;
yield return Shot("sink_map");
Log($"map: {MapScreen.Drawn} sectors drawn close in, {MapScreen.Flipped} turned round");
UIRoot.I.CloseTopModal();
yield return new WaitForSeconds(0.3f);
UIRoot.I.OpenMap(1.6f);
yield return new WaitForSeconds(1.5f);
yield return Shot("sink_map_far");
Log("sink done");
Application.Quit();
}
/// <summary>The lowest of its four corners, nose and tail, against the ground (below it is negative).</summary>
static float Lowest(Vehicle v)
{
float low = float.MaxValue;
var t = v.transform;
for (int x = -1; x <= 1; x += 2)
for (int z = -1; z <= 1; z += 2)
low = Mathf.Min(low, Planet.Altitude(t.TransformPoint(new Vector3(x * 0.5f * v.width, 0f, z * 0.42f * v.length))));
low = Mathf.Min(low, Planet.Altitude(t.TransformPoint(new Vector3(0f, 0f, 0.5f * v.length))));
low = Mathf.Min(low, Planet.Altitude(t.TransformPoint(new Vector3(0f, 0f, -0.5f * v.length))));
return low;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 20733dbba1c04b8395fe845276378d8f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2957,6 +2957,11 @@ namespace GrassCutter
yield return TwoPlayers(role == "host");
yield break;
}
if (CommandLine.Get("-demorep") is string rep)
{
yield return ReplicaDemo(rep == "host");
yield break;
}
if (CommandLine.Get("-democrowd") is string crowd)
{
yield return Crowd(crowd == "host");
@ -3013,6 +3018,11 @@ namespace GrassCutter
yield return Perf(int.Parse(perfSlot));
yield break;
}
if (CommandLine.Has("-demosink"))
{
yield return SinkDemo();
yield break;
}
if (CommandLine.Has("-demotrain"))
{
yield return TrainDemo();