Not yet tried in the game: Unity could not get a licence (Unity Hub isn't running), so this was only type-checked with Roslyn. - Planets by area: the hard one half the old whole planet, normal a third of that, easy a sixth. Saves keep the size set they were made on (sizeGen 0). - A sector closes at 93% mown. - Mid and late prices go up (perks, machines, vehicles, tunes); the early game stays as it was. - AUTOMATION perk tree: Long Leash (robot reach), Overdrive Motors, Bigger Hoppers, Long-Range Drones and Hive Mind. - Autopilot, a garage tune for mowers: C hands the machine over. It mows the fields around in strokes, unloads at the nearest pad or the base yard and comes back. Getting in takes it back; it carries on after a load. - Grass Train: lays its own track to a station next door, mowing under it, then runs back and forth cutting wider each run. Models in gc_train.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
8.0 KiB
C#
173 lines
8.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>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.</summary>
|
|
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;
|
|
/// <summary>On autopilot (<see cref="Vehicle.AutoJob"/>, 0 off), and the spot and way it was started from.</summary>
|
|
public int auto;
|
|
public Vector3 home, fwd;
|
|
}
|
|
|
|
/// <summary>Where a garage puts a machine: its pad, or if it's gone, at the base along the ramp in the first free spot.</summary>
|
|
Vector3 PadFor(ulong garage, out Quaternion rot)
|
|
{
|
|
if (NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(garage, out var no) && no.TryGetComponent<Garage>(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;
|
|
}
|
|
|
|
/// <summary>The shed a machine comes out of: the garage asked, or the base shed.</summary>
|
|
VehicleShed ShedFor(ulong garage)
|
|
{
|
|
if (NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(garage, out var no) && no.TryGetComponent<Garage>(out var g))
|
|
return g.GetComponent<VehicleShed>();
|
|
return VehicleShed.Base;
|
|
}
|
|
|
|
/// <summary>Server: the machine waits inside the shed, the door goes up and it drives out onto a free spot on the pad.</summary>
|
|
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<Vehicle>(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 || 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();
|
|
}
|
|
|
|
/// <summary>Any garage can bring your machine to its pad, wherever it was left.</summary>
|
|
[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<Vehicle>();
|
|
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<Vehicle>();
|
|
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) { DeniedRpc(sender); return; }
|
|
Money.Value -= price;
|
|
v.Tuned[tune] = (byte)(level + 1);
|
|
Save();
|
|
}
|
|
|
|
List<VehicleSave> PackVehicles()
|
|
{
|
|
var list = new List<VehicleSave>();
|
|
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<VehicleSave> 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);
|
|
}
|
|
}
|
|
}
|
|
}
|