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:
110
Assets/Scripts/Core/DemoBot.Replica.cs
Normal file
110
Assets/Scripts/Core/DemoBot.Replica.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user