fix(sverm): the nest lays brood eggs while its owner is around
Settle ran every second and restarted the egg clock at "now" each time. One second is 1/20 of an egg, which integer division rounds to none, so with the owner near the nest twenty seconds never went by and the clutch stayed empty. Every troop and the Blister Tick cost an egg, so the whole Hive Market shelf stayed grey. The clock now moves on only by the eggs actually laid, and is held at now while the clutch is full.
This commit is contained in:
parent
6c23d4e206
commit
ff4f79f452
1 changed files with 53 additions and 20 deletions
|
|
@ -281,24 +281,19 @@ namespace RacesOfValheim
|
|||
{
|
||||
ZNetView nview = View(nest);
|
||||
if (nview == null) return 0;
|
||||
|
||||
ZDO zdo = nview.GetZDO();
|
||||
int stored = zdo.GetInt(EggKey, 0);
|
||||
long clock = zdo.GetLong(ClockKey, 0L);
|
||||
|
||||
// A clutch that has never been settled has no elapsed time to claim. Reading a missing
|
||||
// clock as zero would hand a brand new nest every egg since the world began.
|
||||
if (clock <= 0L) return Mathf.Clamp(stored, 0, Cap(nest));
|
||||
|
||||
long elapsed = WorldSecond() - clock;
|
||||
int laid = elapsed > 0L ? (int)(elapsed / SvermConfig.EggInterval) : 0;
|
||||
|
||||
return Mathf.Clamp(stored + laid, 0, Cap(nest));
|
||||
return Tally(nview.GetZDO(), nest, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes down what time has laid and restarts the clock. The owner calls this on a timer so
|
||||
/// the number the other clients read is never far behind.
|
||||
/// Writes down what time has laid. The owner calls this every second so the number the
|
||||
/// other clients read is never far behind.
|
||||
///
|
||||
/// It used to restart the clock at "now" on every call. One second is 1/20 of an egg, which
|
||||
/// integer division rounds to none, so with the owner standing by the nest the clock was
|
||||
/// reset every second, twenty seconds never went by, and the clutch stayed empty for as
|
||||
/// long as anybody was there to see it. Every troop and the Blister Tick cost an egg, so the
|
||||
/// whole shelf stayed grey. The clock now moves on only by the eggs actually laid, the same
|
||||
/// rule SvermBiomass.Digest keeps.
|
||||
/// </summary>
|
||||
public static void Settle(Transform nest)
|
||||
{
|
||||
|
|
@ -306,10 +301,10 @@ namespace RacesOfValheim
|
|||
if (nview == null || !nview.IsOwner()) return;
|
||||
|
||||
ZDO zdo = nview.GetZDO();
|
||||
int now = Count(nest);
|
||||
int now = Tally(zdo, nest, out long clock);
|
||||
|
||||
zdo.Set(EggKey, now);
|
||||
zdo.Set(ClockKey, WorldSecond());
|
||||
zdo.Set(ClockKey, clock);
|
||||
}
|
||||
|
||||
/// <summary>Takes eggs out of the clutch. All or nothing.</summary>
|
||||
|
|
@ -318,15 +313,53 @@ namespace RacesOfValheim
|
|||
ZNetView nview = View(nest);
|
||||
if (nview == null) return false;
|
||||
|
||||
int have = Count(nest);
|
||||
ZDO zdo = nview.GetZDO();
|
||||
int have = Tally(zdo, nest, out long clock);
|
||||
if (have < howMany) return false;
|
||||
|
||||
if (!nview.IsOwner()) nview.ClaimOwnership();
|
||||
nview.GetZDO().Set(EggKey, have - howMany);
|
||||
nview.GetZDO().Set(ClockKey, WorldSecond());
|
||||
zdo.Set(EggKey, have - howMany);
|
||||
zdo.Set(ClockKey, clock);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The clutch as of now, and where its clock should stand afterwards.
|
||||
///
|
||||
/// The clock advances by whole eggs only, so the part of the twenty seconds already waited
|
||||
/// for the next one is kept. A full clutch lays nothing, so its clock is held at now: the
|
||||
/// first egg after one is taken comes twenty seconds later, not at once. A clutch that has
|
||||
/// never been settled has no elapsed time to claim - reading a missing clock as zero would
|
||||
/// hand a brand new nest every egg since the world began.
|
||||
/// </summary>
|
||||
private static int Tally(ZDO zdo, Transform nest, out long clock)
|
||||
{
|
||||
long now = WorldSecond();
|
||||
int cap = Cap(nest);
|
||||
int stored = zdo.GetInt(EggKey, 0);
|
||||
clock = zdo.GetLong(ClockKey, 0L);
|
||||
|
||||
if (clock <= 0L)
|
||||
{
|
||||
clock = now;
|
||||
return Mathf.Clamp(stored, 0, cap);
|
||||
}
|
||||
|
||||
long interval = (long)SvermConfig.EggInterval;
|
||||
long elapsed = now - clock;
|
||||
long laid = elapsed > 0L && interval > 0L ? elapsed / interval : 0L;
|
||||
|
||||
long eggs = stored + laid;
|
||||
if (eggs >= cap)
|
||||
{
|
||||
clock = now;
|
||||
return cap;
|
||||
}
|
||||
|
||||
clock += laid * interval;
|
||||
return Mathf.Max(0, (int)eggs);
|
||||
}
|
||||
|
||||
private static ZNetView View(Transform nest)
|
||||
{
|
||||
ZNetView nview = nest != null ? nest.GetComponent<ZNetView>() : null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue