Files
grass-cutters/Assets/Scripts/Gameplay/TeamState.Mow.cs
NiemandKG d1c3761e58 Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and
Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date.
Images, audio, models and binaries go through Git LFS (.gitattributes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:40 +06:00

46 lines
1.5 KiB
C#

using System.Collections;
using System.IO;
using Unity.Netcode;
using UnityEngine;
namespace GrassCutter
{
public partial class TeamState
{
const int MowChunk = 16000;
MemoryStream _mowIn;
/// <summary>Server: a player joined a game in progress. Hand them everything that has been cut.</summary>
public void ServerSendMow(ulong to)
{
if (to != NetworkManager.ServerClientId) StartCoroutine(SendMow(to));
}
IEnumerator SendMow(ulong to)
{
var data = MowMap.Serialize();
int count = Mathf.Max(1, Mathf.CeilToInt(data.Length / (float)MowChunk));
for (int i = 0; i < count; i++)
{
if (!NetworkManager.ConnectedClients.ContainsKey(to)) yield break;
int at = i * MowChunk, len = Mathf.Min(MowChunk, data.Length - at);
var chunk = new byte[len];
System.Buffer.BlockCopy(data, at, chunk, 0, len);
MowChunkRpc(chunk, i, count, RpcTarget.Single(to, RpcTargetUse.Temp));
yield return null;
}
}
[Rpc(SendTo.SpecifiedInParams)]
void MowChunkRpc(byte[] chunk, int index, int count, RpcParams p = default)
{
if (index == 0) _mowIn = new MemoryStream();
if (_mowIn == null) return;
_mowIn.Write(chunk, 0, chunk.Length);
if (index < count - 1) return;
MowMap.Merge(_mowIn.ToArray());
_mowIn = null;
}
}
}