Fix Sky Mower tumbling and the hitches on a big farm
- Mowbot keeps its heading apart from the drawn pose: a flyer's lean and sway were fed back each frame and rolled it over. - Drone pads looked through every loose bundle, testing every belt end, for each idle drone every frame (43 ms on a 1500-bundle farm): distance first, and a 1.5 s pause after finding nothing. - Saves are written, laid out and compressed off the main thread; backups go in the same queue (an autosave froze the game ~50 ms every 30 s). - Robots tick on staggered frames. - -demoperf N profiles save slot N; -demobots logs the Sky Mower's tilt. On the player's save: frames over 25 ms went from 144 to 0 in 40 s, worst frame 74 ms -> 22 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -215,16 +215,36 @@ namespace GrassCutter
|
||||
}
|
||||
|
||||
static void Pack(Mask m)
|
||||
{
|
||||
m.packed = Deflate(Raw(m));
|
||||
m.dirty = false;
|
||||
}
|
||||
|
||||
static byte[] Raw(Mask m)
|
||||
{
|
||||
var raw = new byte[Res * Res * 3];
|
||||
System.Buffer.BlockCopy(m.left, 0, raw, 0, Res * Res * 2);
|
||||
for (int i = 0; i < Res * Res; i++) raw[Res * Res * 2 + i] = m.rg[i * 2];
|
||||
return raw;
|
||||
}
|
||||
|
||||
static byte[] Deflate(byte[] raw)
|
||||
{
|
||||
using (var mem = new MemoryStream())
|
||||
{
|
||||
using (var z = new DeflateStream(mem, System.IO.Compression.CompressionLevel.Fastest, true)) z.Write(raw, 0, raw.Length);
|
||||
m.packed = mem.ToArray();
|
||||
return mem.ToArray();
|
||||
}
|
||||
m.dirty = false;
|
||||
}
|
||||
|
||||
/// <summary>A sector packed off the main thread for a save: its bytes go back on the mask if nothing changed since.</summary>
|
||||
sealed class Packing { public Mask m; public byte[] raw, packed; }
|
||||
static readonly System.Collections.Concurrent.ConcurrentQueue<Packing> s_packed = new System.Collections.Concurrent.ConcurrentQueue<Packing>();
|
||||
|
||||
static void TakePacked()
|
||||
{
|
||||
while (s_packed.TryDequeue(out var p))
|
||||
if (p.m.packed == null && !p.m.dirty && p.m.left != null) p.m.packed = p.packed;
|
||||
}
|
||||
|
||||
static byte[] Inflate(byte[] packed)
|
||||
@ -241,6 +261,7 @@ namespace GrassCutter
|
||||
/// <summary>Too many maps in memory: squeeze the ones furthest from here.</summary>
|
||||
public static void Trim(Vector3 dir)
|
||||
{
|
||||
TakePacked();
|
||||
int open = 0;
|
||||
foreach (var m in s_masks.Values) if (m.left != null) open++;
|
||||
if (open <= MaxOpen) return;
|
||||
@ -646,30 +667,60 @@ namespace GrassCutter
|
||||
const int Magic = 0x314D4347; // "GCM1"
|
||||
|
||||
/// <summary>Everything cut so far, as one block of bytes (the save file, and what a late joiner is sent).</summary>
|
||||
public static byte[] Serialize()
|
||||
public static byte[] Serialize() => SerializeLater()();
|
||||
|
||||
/// <summary>
|
||||
/// The mow map as it stands now, put into bytes later, off the main thread if wanted: only the sectors cut since the
|
||||
/// last time are packed here, the ten-odd megabytes are laid out by the function it hands back. A packed array is
|
||||
/// only ever replaced, never written into, so holding on to them is safe.
|
||||
/// </summary>
|
||||
public static System.Func<byte[]> SerializeLater()
|
||||
{
|
||||
using (var mem = new MemoryStream())
|
||||
using (var w = new BinaryWriter(mem))
|
||||
TakePacked();
|
||||
var rows = new List<(int id, float total, float mown, bool done, byte[] packed, Packing job)>(s_masks.Count);
|
||||
foreach (var m in s_masks.Values)
|
||||
{
|
||||
int count = 0;
|
||||
foreach (var m in s_masks.Values) if (m.keep) count++;
|
||||
w.Write(Magic);
|
||||
w.Write(count);
|
||||
foreach (var m in s_masks.Values)
|
||||
if (!m.keep) continue;
|
||||
if (!m.done && (m.dirty || m.packed == null) && m.left != null)
|
||||
{
|
||||
if (!m.keep) continue;
|
||||
if (!m.done && (m.dirty || m.packed == null) && m.left != null) Pack(m);
|
||||
w.Write(m.s.id);
|
||||
w.Write(m.total);
|
||||
w.Write(m.mown);
|
||||
w.Write(m.done);
|
||||
int len = m.done || m.packed == null ? 0 : m.packed.Length;
|
||||
w.Write(len);
|
||||
if (len > 0) w.Write(m.packed);
|
||||
// only the copy is made here, the squeezing happens with the rest of the save; till its bytes come
|
||||
// back the mask has none, so anything that needs them packs it again itself
|
||||
var job = new Packing { m = m, raw = Raw(m) };
|
||||
m.packed = null;
|
||||
m.dirty = false;
|
||||
rows.Add((m.s.id, m.total, m.mown, false, null, job));
|
||||
continue;
|
||||
}
|
||||
w.Flush();
|
||||
return mem.ToArray();
|
||||
rows.Add((m.s.id, m.total, m.mown, m.done, m.done ? null : m.packed, null));
|
||||
}
|
||||
return () =>
|
||||
{
|
||||
using (var mem = new MemoryStream())
|
||||
using (var w = new BinaryWriter(mem))
|
||||
{
|
||||
w.Write(Magic);
|
||||
w.Write(rows.Count);
|
||||
foreach (var (id, total, mown, done, had, job) in rows)
|
||||
{
|
||||
var packed = had;
|
||||
if (job != null)
|
||||
{
|
||||
packed = job.packed = Deflate(job.raw);
|
||||
job.raw = null;
|
||||
s_packed.Enqueue(job);
|
||||
}
|
||||
w.Write(id);
|
||||
w.Write(total);
|
||||
w.Write(mown);
|
||||
w.Write(done);
|
||||
int len = packed == null ? 0 : packed.Length;
|
||||
w.Write(len);
|
||||
if (len > 0) w.Write(packed);
|
||||
}
|
||||
w.Flush();
|
||||
return mem.ToArray();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Take in a saved game. What this peer has cut in the meantime stays cut.</summary>
|
||||
|
||||
Reference in New Issue
Block a user