using System.Collections;
using UnityEngine;
namespace GrassCutter
{
public partial class DemoBot
{
///
/// -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).
///
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();
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)")}");
}
// every other kind of purchase, from the client: a matrix perk, a tool, gear, a belt slot, a building's upgrade
BuildPiece robot = null, garage = null;
if (host)
{
var rAt = Soil17(30f, -30f);
robot = Spawner.Instance.SpawnPiece(PieceId.RobotI, rAt, Face(rAt, Vector3.forward), Vector3.zero, true);
var gAt = Soil17(45f, 20f);
garage = Spawner.Instance.SpawnPiece(PieceId.Garage, gAt, Face(gAt, Vector3.forward), Vector3.zero, true);
}
yield return new WaitForSeconds(2f);
robot = BuildPiece.All.Find(x => x && x.piece == PieceId.RobotI);
garage = BuildPiece.All.Find(x => x && x.piece == PieceId.Garage);
if (!host)
{
PerkDef perk = null;
foreach (var d in GameDefs.Perks) if (Ts.Level(d.id) == 0 && Ts.PerkAvailable(d)) { perk = d; break; }
int slots = Me.BeltSlots.Value, tier = robot ? robot.Tier.Value : -1;
double cash = Ts.Money.Value;
if (perk != null) Ts.BuyPerkRpc((int)perk.id);
Ts.BuyToolRpc((byte)ToolId.Sickle);
Ts.BuyGearRpc((byte)GearId.Jetpack);
Ts.BuyBeltSlotRpc();
if (robot) robot.UpgradeRpc();
yield return new WaitForSeconds(2f);
Log($"client buys: perk {(perk != null ? perk.id + " " + Ts.Level(perk.id) : "none available")}, sickle {(Me.Owns(ToolId.Sickle) ? "yes" : "NO")}, jetpack {(Me.HasGear(GearId.Jetpack) ? "yes" : "NO")}, belt slots {slots} -> {Me.BeltSlots.Value}, robot {(robot ? "Mk " + (tier + 1) + " -> Mk " + (robot.Tier.Value + 1) : "MISSING")}, spent {cash - Ts.Money.Value:0}");
// and the screens, as the client sees them
BotCapture.From = null;
if (garage) { UIRoot.I.OpenGarage(garage.GetComponent(), VehicleKind.Tractor); yield return new WaitForSeconds(1.2f); yield return Shot("rep_client_garage"); UIRoot.I.CloseTopModal(); yield return new WaitForSeconds(0.4f); }
if (robot) { UIRoot.I.OpenPiece(robot); yield return new WaitForSeconds(1.2f); yield return Shot("rep_client_robot"); UIRoot.I.CloseTopModal(); yield return new WaitForSeconds(0.4f); }
UIRoot.I.OpenShop(); yield return new WaitForSeconds(1.2f); yield return Shot("rep_client_shop"); UIRoot.I.CloseTopModal(); yield return new WaitForSeconds(0.4f);
UIRoot.I.OpenSupply(); yield return new WaitForSeconds(1.2f); yield return Shot("rep_client_supply"); UIRoot.I.CloseTopModal(); yield return new WaitForSeconds(0.4f);
}
else yield return new WaitForSeconds(12f);
yield return new WaitForSeconds(1f);
if (host) Log($"host sees: robot Mk {(robot ? robot.Tier.Value + 1 : 0)}, the client owns a sickle {(other.Owns(ToolId.Sickle) ? "yes" : "NO")}, perks bought {Ts.Installs - 1}");
// 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();
}
}
}