using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace GrassCutter
{
/// Machines on the server: each player buys their own, one of each kind, calls it to any garage and tunes it. Paid from the crew wallet.
public partial class TeamState
{
[Serializable]
class VehicleSave
{
public int kind;
public string key;
public Vector3 at;
public Quaternion turn;
public float tank;
public int[] tune;
public Vector2[] bed;
/// On autopilot (, 0 off), and the spot and way it was started from.
public int auto;
public Vector3 home, fwd;
}
/// Where a garage puts a machine: its pad, or if it's gone, at the base along the ramp in the first free spot.
Vector3 PadFor(ulong garage, out Quaternion rot)
{
if (NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(garage, out var no) && no.TryGetComponent(out var g))
{
rot = g.transform.rotation;
return g.PadSpot;
}
var dock = Spawner.Instance ? Spawner.Instance.dock : null;
var at = dock ? dock.position : new Vector3(0f, GameDefs.WaterY, -14f);
var side = dock ? dock.right : Vector3.right;
rot = Quaternion.LookRotation(dock ? dock.forward : Vector3.back);
at.y = GameDefs.WaterY + 0.6f;
for (int i = 0; i < 6; i++)
{
var spot = at + side * (i % 2 == 0 ? i * 2f + 3f : -i * 2f - 3f);
if (!Physics.CheckSphere(spot + Vector3.up, 1.6f, 1 << Layers.Vehicle, QueryTriggerInteraction.Ignore)) return spot;
}
return at;
}
/// The shed a machine comes out of: the garage asked, or the base shed.
VehicleShed ShedFor(ulong garage)
{
if (NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(garage, out var no) && no.TryGetComponent(out var g))
return g.GetComponent();
return VehicleShed.Base;
}
/// Server: the machine waits inside the shed, the door goes up and it drives out onto a free spot on the pad.
bool ServerRollOut(Vehicle v, ulong garage)
{
var shed = ShedFor(garage);
if (!shed || !v) return false;
var rot = shed.Facing;
// the machine is turned to face out before its length is measured
v.ServerPut(v.transform.position, rot);
v.Extent(out float tail, out float top, out float bottom);
var lift = shed.transform.up * Mathf.Max(0.03f, 0.03f - bottom);
var outside = shed.OutsideFor(tail) + lift;
var to = outside;
var side = shed.transform.right;
for (int i = 0; i < 5; i++)
{
var spot = outside + side * (i == 0 ? 0f : (i % 2 == 1 ? 1f : -1f) * ((i + 1) / 2) * 3.4f);
if (!Physics.CheckSphere(spot + shed.transform.up * 0.8f, 1.5f, 1 << Layers.Vehicle, QueryTriggerInteraction.Ignore)) { to = spot; break; }
}
v.ServerRollOut(shed.InsideFor(tail, top) + lift, to, rot);
ShedDoorRpc(garage, v.NetworkObjectId);
return true;
}
[Rpc(SendTo.Everyone)]
void ShedDoorRpc(ulong garage, ulong vehicle)
{
var shed = ShedFor(garage);
if (shed) shed.Open();
// it waits inside out of sight until the door is most of the way up
if (NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(vehicle, out var no) && no.TryGetComponent(out var v))
v.HideFor(VehicleShed.Wait * 0.8f);
}
[Rpc(SendTo.Server)]
public void BuyVehicleRpc(byte kind, ulong garage, RpcParams rpc = default)
{
var sender = RpcTarget.Single(rpc.Receive.SenderClientId, RpcTargetUse.Temp);
var who = PlayerOf(rpc.Receive.SenderClientId);
if (!who || !who.ClaimedSave || kind >= (byte)VehicleKind.Count) return;
var d = GameDefs.Vehicle((VehicleKind)kind);
if (Money.Value < d.price || Demo.Over || Vehicle.Mine(d.kind, who.SaveId)) { DeniedRpc(sender); return; }
Money.Value -= d.price;
var shed = ShedFor(garage);
Vector3 at;
Quaternion rot;
if (shed) { at = shed.Inside; rot = shed.Facing; }
else at = PadFor(garage, out rot);
var made = Spawner.Instance.SpawnVehicle(d.kind, who.SaveId, at, rot);
if (made) ServerRollOut(made, garage);
AnnounceRpc(Loc.Pack("A new {0} is waiting at the garage!", Loc.Key(d.nameEn)));
Save();
}
/// Any garage can bring your machine to its pad, wherever it was left.
[Rpc(SendTo.Server)]
public void CallVehicleRpc(ulong vehicle, ulong garage, RpcParams rpc = default)
{
var who = PlayerOf(rpc.Receive.SenderClientId);
if (!who || !NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(vehicle, out var no)) return;
var v = no.GetComponent();
if (!v || v.HasDriver || v.Crew.Value.ToString() != who.SaveId) { DeniedRpc(RpcTarget.Single(rpc.Receive.SenderClientId, RpcTargetUse.Temp)); return; }
if (v.RollingOut) return;
if (ServerRollOut(v, garage)) return;
var at = PadFor(garage, out var rot);
v.ServerPut(at, rot);
}
[Rpc(SendTo.Server)]
public void TuneVehicleRpc(ulong vehicle, byte tune, RpcParams rpc = default)
{
var sender = RpcTarget.Single(rpc.Receive.SenderClientId, RpcTargetUse.Temp);
var who = PlayerOf(rpc.Receive.SenderClientId);
if (!who || tune >= (byte)TuneId.Count || !NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(vehicle, out var no)) return;
var v = no.GetComponent();
if (!v || v.Crew.Value.ToString() != who.SaveId) { DeniedRpc(sender); return; }
var t = (TuneId)tune;
int level = v.Level(t);
if (level >= GameDefs.Tune(t).prices.Length) { DeniedRpc(sender); return; }
double price = GameDefs.TunePrice(v.kind, t, level);
if (Money.Value < price || Demo.Over) { DeniedRpc(sender); return; }
Money.Value -= price;
v.Tuned[tune] = (byte)(level + 1);
Save();
}
List PackVehicles()
{
var list = new List();
foreach (var v in Vehicle.All)
{
if (!v || !v.IsSpawned) continue;
var s = new VehicleSave { kind = (int)v.kind, key = v.Crew.Value.ToString(), at = v.transform.position, turn = v.transform.rotation, tank = v.Tank.Value };
if (!v.HasDriver) v.AutoState(out s.auto, out s.home, out s.fwd);
s.tune = new int[v.Tuned.Count];
for (int i = 0; i < s.tune.Length; i++) s.tune[i] = v.Tuned[i];
s.bed = new Vector2[v.Bed.Count];
for (int i = 0; i < s.bed.Length; i++) s.bed[i] = v.Bed[i];
list.Add(s);
}
return list;
}
void UnpackVehicles(List list)
{
if (list == null || !Spawner.Instance) return;
foreach (var s in list)
{
if (s.kind < 0 || s.kind >= (int)VehicleKind.Count) continue;
var v = Spawner.Instance.SpawnVehicle((VehicleKind)s.kind, s.key, s.at, s.turn);
if (!v) continue;
v.Tank.Value = s.tank;
if (s.tune != null) for (int i = 0; i < s.tune.Length && i < v.Tuned.Count; i++) v.Tuned[i] = (byte)s.tune[i];
if (s.bed != null) foreach (var b in s.bed) v.Bed.Add(b);
if (s.auto != 0) v.ServerResumeAuto(s.home, s.fwd);
}
}
}
}