using System.Linq;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace GrassCutter
{
/// The stations the crew has found, travel between them, and who isn't welcome.
public partial class TeamState
{
/// Stations found, not counting the base.
public NetworkList Opened;
List _banned = new List();
public bool StationOpen(int i) => i == 0 || (Opened != null && Opened.Contains((byte)i));
[Rpc(SendTo.Server)]
public void OpenStationRpc(int i, RpcParams rpc = default)
{
var who = PlayerOf(rpc.Receive.SenderClientId);
if (!who || i <= 0 || i >= Stations.Count || StationOpen(i)) return;
if ((who.transform.position - Stations.Spot(i)).sqrMagnitude > 3600f) return;
Opened.Add((byte)i);
AnnounceRpc(Loc.Pack("{0} found {1} Station!", who.DisplayName, Stations.Names[i]));
Save();
}
[Rpc(SendTo.Server)]
public void TravelRpc(int i, RpcParams rpc = default)
{
var who = PlayerOf(rpc.Receive.SenderClientId);
if (!who || i < 0 || i >= Stations.Count) return;
if (who.Driving.Value || !StationOpen(i) || i == Stations.Nearest(who.transform.position) || Money.Value < GameDefs.TravelPrice)
{
DeniedRpc(RpcTarget.Single(rpc.Receive.SenderClientId, RpcTargetUse.Temp));
return;
}
Money.Value -= GameDefs.TravelPrice;
who.ArriveRpc(Stations.Arrival(i));
}
/// Host: send someone away, for good if they are banned.
public void ServerKick(ulong client, bool ban)
{
if (!IsServer || client == NetworkManager.ServerClientId) return;
var who = PlayerOf(client);
if (ban && who && who.ClaimedSave && !_banned.Contains(who.SaveId)) _banned.Add(who.SaveId);
NetworkManager.DisconnectClient(client, ban ? Loc.Pack("You were banned from this game") : Loc.Pack("The host asked you to leave"));
Save();
}
public bool Banned(string key) => _banned.Contains(key);
/// The depots of the far stations: a new game has them, and an older save gets them now.
void ServerSpawnStations()
{
for (int i = 1; i < Stations.Count; i++) ServerSpawnPiece(PieceId.Depot, Stations.Spot(i), Stations.Turn(i), Vector3.zero, -1, true);
}
///
/// Eight low intakes on the ground against the sides of the base deck, two on each side, so a belt from the meadow can
/// end at the base without climbing up top. They sit between the ladders, clear of the ramp, backs to the deck.
/// A new game has them, and an older save gets them now (the tall depots that stood there before go).
///
void ServerSpawnIntakes()
{
foreach (var spot in IntakeSpots)
{
// straight out from the side it stands on, back square to the deck
var outward = Mathf.Abs(spot.x) > Mathf.Abs(spot.z) ? new Vector3(Mathf.Sign(spot.x), 0f, 0f) : new Vector3(0f, 0f, Mathf.Sign(spot.z));
ServerSpawnPiece(PieceId.Intake, Planet.Surface(spot), Quaternion.LookRotation(outward, Vector3.up), Vector3.zero, -1, true);
}
}
/// A save from stage 18 has tall depots round the foot of the deck: they make way for the intakes.
void ServerSwapIntakes()
{
foreach (var p in BuildPiece.All.ToArray())
{
if (!p || p.piece != PieceId.Depot || Planet.Altitude(p.transform.position) > 0.6f) continue;
var flat = new Vector2(p.transform.position.x, p.transform.position.z);
foreach (var old in OldIntakeSpots)
if ((flat - new Vector2(old.x, old.z)).magnitude < 2.5f) { p.NetworkObject.Despawn(true); break; }
}
ServerSpawnIntakes();
}
/// Saves from the first stage-19 build have the intakes turned 45° off the deck: they are set again.
void ServerRespawnIntakes()
{
foreach (var p in BuildPiece.All.ToArray())
if (p && p.piece == PieceId.Intake) p.NetworkObject.Despawn(true);
ServerSpawnIntakes();
}
/// The deck runs ±9 m; an intake is 1.5 m deep, so its middle is 9.77 m out, 6 m either side of each edge's middle.
public static readonly Vector3[] IntakeSpots =
{
new Vector3(-6f, 0f, 9.77f), new Vector3(6f, 0f, 9.77f),
new Vector3(-6f, 0f, -9.77f), new Vector3(6f, 0f, -9.77f),
new Vector3(9.77f, 0f, -6f), new Vector3(9.77f, 0f, 6f),
new Vector3(-9.77f, 0f, -6f), new Vector3(-9.77f, 0f, 6f),
};
static readonly Vector3[] OldIntakeSpots =
{
new Vector3(-6f, 0f, 11f), new Vector3(6f, 0f, 11f),
new Vector3(-6f, 0f, -11f), new Vector3(6f, 0f, -11f),
new Vector3(11f, 0f, -6f), new Vector3(11f, 0f, 6f),
new Vector3(-11f, 0f, -6f), new Vector3(-11f, 0f, 6f),
};
}
}