warvulture
This commit is contained in:
parent
3095198539
commit
1d66b00374
29 changed files with 7236 additions and 4 deletions
243
JotunnModStub/Sverm/SvermBiomass.cs
Normal file
243
JotunnModStub/Sverm/SvermBiomass.cs
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// The nest's tank, and the rule for what may go into it.
|
||||
///
|
||||
/// The number lives on the nest's own ZDO rather than in a field, so it survives a reload, a
|
||||
/// zone unload, and a different player walking up to the same nest.
|
||||
///
|
||||
/// WHAT COUNTS AS FUEL is the part worth reading. Anything the Sverm could eat, at half its
|
||||
/// nutritional sum, plus a flat rate for wood and resin - EXCEPT honey and royal jelly, which
|
||||
/// stopped being fuel the moment they became medicine. Feeding the colony its own healing
|
||||
/// supply would be the worst kind of bug: quiet, expensive, and entirely the mod's fault.
|
||||
/// </summary>
|
||||
public static class SvermBiomass
|
||||
{
|
||||
// ─── What burns ──────────────────────────────────────────────────────
|
||||
|
||||
public static bool IsFuel(ItemDrop.ItemData item)
|
||||
{
|
||||
if (item == null || item.m_shared == null) return false;
|
||||
|
||||
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : "";
|
||||
|
||||
// The medicine. Explicitly first, so no later rule can accidentally readmit it.
|
||||
if (prefabName == "Honey" || prefabName == SvermConfig.RoyalJellyPrefab) return false;
|
||||
|
||||
if (IsWood(prefabName)) return true;
|
||||
|
||||
return item.m_shared.m_food + item.m_shared.m_foodStamina + item.m_shared.m_foodEitr > 0f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wood and resin, at a flat rate regardless of biome. Ashwood is out.
|
||||
///
|
||||
/// Flat because a per-biome table for firewood is a lot of bookkeeping for a resource whose
|
||||
/// whole job is to be the bulk filler while food is the concentrate.
|
||||
/// </summary>
|
||||
private static bool IsWood(string prefabName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(prefabName)) return false;
|
||||
if (prefabName.ToLowerInvariant().Contains("ash")) return false; // ashwood excluded
|
||||
|
||||
return prefabName == "Wood"
|
||||
|| prefabName == "RoundLog"
|
||||
|| prefabName == "FineWood"
|
||||
|| prefabName == "ElderBark"
|
||||
|| prefabName == "YggdrasilWood"
|
||||
|| prefabName == "Resin";
|
||||
}
|
||||
|
||||
/// <summary>What one of this item is worth in the tank.</summary>
|
||||
public static float ValueOf(ItemDrop.ItemData item)
|
||||
{
|
||||
if (!IsFuel(item)) return 0f;
|
||||
|
||||
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : "";
|
||||
if (IsWood(prefabName)) return SvermConfig.WoodBiomassValue;
|
||||
|
||||
return (item.m_shared.m_food + item.m_shared.m_foodStamina + item.m_shared.m_foodEitr) * 0.5f;
|
||||
}
|
||||
|
||||
// ─── The tank ────────────────────────────────────────────────────────
|
||||
//
|
||||
// THREE NUMBERS, NOT ONE, and the middle one is the whole point of the shop. What the hive
|
||||
// has just been handed is not what the hive can spend: everything it eats lands in
|
||||
// DIGESTION and seeps into the BANK at a fixed rate, and only the bank ever pays for
|
||||
// anything. CAPACITY is the stage's ceiling and it covers both together, because a stomach
|
||||
// that is full is full whether or not the last meal has gone down yet.
|
||||
|
||||
// The bank keeps the key the tank has always had, so a hive standing in somebody's world
|
||||
// reads back with its biomass intact rather than at zero.
|
||||
private const string BankKey = "RacesOfValheim_SvermBiomass"; // spendable
|
||||
private const string DigestKey = "RacesOfValheim_SvermDigesting"; // eaten, not yet spendable
|
||||
private const string ClockKey = "RacesOfValheim_SvermDigestedAt"; // world second of the last digest
|
||||
|
||||
/// <summary>What the hive can actually spend.</summary>
|
||||
public static float Bank(Transform nest)
|
||||
{
|
||||
ZDO zdo = Zdo(nest);
|
||||
return zdo != null ? zdo.GetFloat(BankKey, 0f) : 0f;
|
||||
}
|
||||
|
||||
/// <summary>Eaten but not yet spendable.</summary>
|
||||
public static float Digesting(Transform nest)
|
||||
{
|
||||
ZDO zdo = Zdo(nest);
|
||||
return zdo != null ? zdo.GetFloat(DigestKey, 0f) : 0f;
|
||||
}
|
||||
|
||||
public static float Capacity(Transform nest)
|
||||
{
|
||||
return SvermHiveStage.Capacity(SvermHiveStage.Of(nest));
|
||||
}
|
||||
|
||||
/// <summary>How much more the hive can hold, digested or not.</summary>
|
||||
public static float Space(Transform nest)
|
||||
{
|
||||
return Mathf.Max(0f, Capacity(nest) - Bank(nest) - Digesting(nest));
|
||||
}
|
||||
|
||||
/// <summary>Adds one item's worth. Returns what actually fitted.</summary>
|
||||
public static float Deposit(Transform nest, ItemDrop.ItemData item)
|
||||
{
|
||||
return Swallow(nest, ValueOf(item));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts raw biomass into digestion, clamped to what is left of the tank. Returns what fitted.
|
||||
///
|
||||
/// EVERYTHING the hive eats comes through here - the shop's one button and the haulers'
|
||||
/// chest round alike. A meal that arrived by way of a chest is still a meal, and giving the
|
||||
/// haulers a shortcut straight into the bank would make the wait the shop imposes
|
||||
/// avoidable by anyone who owns a box.
|
||||
/// </summary>
|
||||
public static float Swallow(Transform nest, float amount)
|
||||
{
|
||||
if (amount <= 0f) return 0f;
|
||||
|
||||
ZNetView nview = Claim(nest);
|
||||
if (nview == null) return 0f;
|
||||
|
||||
float accepted = Mathf.Min(amount, Space(nest));
|
||||
if (accepted <= 0f) return 0f;
|
||||
|
||||
ZDO zdo = nview.GetZDO();
|
||||
|
||||
// The clock is started by the first mouthful and not before: an untouched hive has no
|
||||
// "last digested" second, and reading a missing key as zero would hand it every second
|
||||
// since the world began the moment it was finally fed.
|
||||
//
|
||||
// WHOLE SECONDS IN A LONG FIELD, deliberately and everywhere. ZDO.Set takes a long as
|
||||
// readily as a float, and a clock written through one overload and read through the
|
||||
// other is a key that always answers with its default - which here means an elapsed
|
||||
// time of zero, forever, and a hive that digests nothing at all.
|
||||
if (zdo.GetFloat(DigestKey, 0f) <= 0f) zdo.Set(ClockKey, WorldSecond());
|
||||
|
||||
zdo.Set(DigestKey, zdo.GetFloat(DigestKey, 0f) + accepted);
|
||||
return accepted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves what time has earned from digestion into the bank.
|
||||
///
|
||||
/// Measured against the WORLD clock rather than frames, so a hive nobody is standing next
|
||||
/// to still digests: come back to it in the morning and the meal has gone down. The owner
|
||||
/// calls this on a timer - see SvermHiveBehaviour.
|
||||
/// </summary>
|
||||
public static void Digest(Transform nest)
|
||||
{
|
||||
ZNetView nview = nest != null ? nest.GetComponent<ZNetView>() : null;
|
||||
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
|
||||
|
||||
ZDO zdo = nview.GetZDO();
|
||||
float digesting = zdo.GetFloat(DigestKey, 0f);
|
||||
if (digesting <= 0f) return;
|
||||
|
||||
long now = WorldSecond();
|
||||
long last = zdo.GetLong(ClockKey, now);
|
||||
long elapsed = now - last;
|
||||
|
||||
// Whole seconds only, and the clock only moves by what was actually consumed, so the
|
||||
// fraction left over is not quietly lost on every tick.
|
||||
if (elapsed <= 0L) return;
|
||||
|
||||
float moved = Mathf.Min(digesting, SvermConfigManagement.DigestRate * elapsed);
|
||||
zdo.Set(ClockKey, now);
|
||||
if (moved <= 0f) return;
|
||||
|
||||
zdo.Set(DigestKey, digesting - moved);
|
||||
zdo.Set(BankKey, zdo.GetFloat(BankKey, 0f) + moved);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts spent biomass straight back in the bank, past the digestion.
|
||||
///
|
||||
/// Only ever for a purchase that was paid for and then could not be delivered. It skips the
|
||||
/// digestion on purpose: the hive already digested this once, and making the player wait
|
||||
/// again for a thing they did not get would be a second punishment for our own race.
|
||||
/// </summary>
|
||||
public static void Refund(Transform nest, float amount)
|
||||
{
|
||||
if (amount <= 0f) return;
|
||||
|
||||
ZNetView nview = Claim(nest);
|
||||
if (nview == null) return;
|
||||
|
||||
ZDO zdo = nview.GetZDO();
|
||||
zdo.Set(BankKey, Mathf.Min(zdo.GetFloat(BankKey, 0f) + amount, Capacity(nest)));
|
||||
}
|
||||
|
||||
/// <summary>Takes from the bank if there is enough. All or nothing.</summary>
|
||||
public static bool Spend(Transform nest, float amount)
|
||||
{
|
||||
ZNetView nview = Claim(nest);
|
||||
if (nview == null) return false;
|
||||
|
||||
ZDO zdo = nview.GetZDO();
|
||||
float bank = zdo.GetFloat(BankKey, 0f);
|
||||
if (bank < amount) return false;
|
||||
|
||||
zdo.Set(BankKey, bank - amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ─── Plumbing ────────────────────────────────────────────────────────
|
||||
|
||||
private static ZDO Zdo(Transform nest)
|
||||
{
|
||||
ZNetView nview = nest != null ? nest.GetComponent<ZNetView>() : null;
|
||||
return nview != null && nview.IsValid() ? nview.GetZDO() : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The nest's view, owned by us before we write to it.
|
||||
///
|
||||
/// A write to a ZDO somebody else owns survives until their copy comes back over the wire
|
||||
/// and overwrites it - which reads, to the player who just sold half their larder, as the
|
||||
/// hive having eaten it and forgotten.
|
||||
/// </summary>
|
||||
private static ZNetView Claim(Transform nest)
|
||||
{
|
||||
ZNetView nview = nest != null ? nest.GetComponent<ZNetView>() : null;
|
||||
if (nview == null || !nview.IsValid()) return null;
|
||||
if (!nview.IsOwner()) nview.ClaimOwnership();
|
||||
return nview;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The world clock in whole seconds.
|
||||
///
|
||||
/// The world's own time and not Time.time, so the answer means the same thing to a hive
|
||||
/// that has just loaded as to one that has been standing there all evening. Zero when there
|
||||
/// is no net yet, which only happens before a world is up - and Digest reads a missing
|
||||
/// clock as "now", so nothing is ever handed a windfall.
|
||||
/// </summary>
|
||||
private static long WorldSecond()
|
||||
{
|
||||
return ZNet.instance != null ? (long)ZNet.instance.GetTimeSeconds() : 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue