warvulture

This commit is contained in:
Ferdinando 2026-08-24 19:17:47 +02:00
commit ee5a2e2e7b
12 changed files with 314 additions and 76 deletions

View file

@ -36,6 +36,7 @@ namespace RacesOfValheim
public float Price;
public int Stage; // the nest stage it becomes available at
public float Cooldown; // seconds between two of these
public float SpawnHeight = 0.5f; // how far above the nest it is put down
public string PrefabName => PrefabPrefix + Source;
public int Cap => CapOf(Id);
@ -53,7 +54,7 @@ namespace RacesOfValheim
new Kind { Id = "brood", Display = "Brood", Source = "SeekerBrood", Icon = "TrophySeeker", Price = 100f, Stage = 1, Cooldown = 60f },
new Kind { Id = "seeker", Display = "Seeker", Source = "Seeker", Icon = "TrophySeeker", Price = 250f, Stage = 2, Cooldown = 150f },
new Kind { Id = "soldier", Display = "Soldier", Source = "SeekerBrute", Icon = "TrophySeekerBrute", Price = 500f, Stage = 3, Cooldown = 300f },
new Kind { Id = "gjall", Display = "Gjall", Source = "Gjall", Icon = "TrophyGjall", Price = 1000f, Stage = 4, Cooldown = 600f },
new Kind { Id = "gjall", Display = "Gjall", Source = "Gjall", Icon = "TrophyGjall", Price = 1000f, Stage = 4, Cooldown = 600f, SpawnHeight = 10f },
};
private static int CapOf(string id)
@ -141,7 +142,7 @@ namespace RacesOfValheim
}
/// <summary>
/// Puts one on the ground beside the nest, bound to it and born at whatever star the colony
/// Puts one down beside the nest, bound to it and born at whatever star the colony
/// has bought.
/// </summary>
public static bool Hatch(SvermHiveBehaviour hive, Kind kind)
@ -156,9 +157,7 @@ namespace RacesOfValheim
return false;
}
Vector3 offset = Random.insideUnitSphere * 2f;
offset.y = 0f;
Vector3 where = hive.transform.position + offset + Vector3.up * 0.5f;
Vector3 where = SpawnSpot(hive, kind);
GameObject spawned = Object.Instantiate(prefab, where, Quaternion.Euler(0f, Random.Range(0, 360), 0f));
if (spawned == null) return false;
@ -179,6 +178,73 @@ namespace RacesOfValheim
return true;
}
/// <summary>How much open ground is left between the nest's own skirt and a newborn.</summary>
private const float SpawnClearance = 2.5f;
/// <summary>
/// Where the newborn is put down: on a ring OUTSIDE the nest, never in the middle of it.
///
/// Troops were hatching at the nest's own position and half a metre up, which is inside a
/// building several metres wide - so they came out wedged in its collider. This walks a ring
/// from a random angle, takes the first spot with nothing standing on it, and drops the
/// creature on the TERRAIN height there, so a nest on a slope does not bury anyone either.
/// The per-kind height goes on top, which is what keeps the Gjall in the air.
/// </summary>
private static Vector3 SpawnSpot(SvermHiveBehaviour hive, Kind kind)
{
Vector3 centre = hive.transform.position;
float radius = RingRadius(hive);
int obstacleMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "vehicle");
float start = Random.Range(0f, 360f);
for (int i = 0; i < 12; i++)
{
float angle = (start + i * (360f / 12f)) * Mathf.Deg2Rad;
Vector3 candidate = centre + new Vector3(Mathf.Cos(angle), 0f, Mathf.Sin(angle)) * radius;
if (ZoneSystem.instance != null) candidate.y = ZoneSystem.instance.GetGroundHeight(candidate);
if (Physics.CheckSphere(candidate + Vector3.up * 1.5f, 1f, obstacleMask)) continue;
return candidate + Vector3.up * kind.SpawnHeight;
}
// Twelve tries and everything is built up: due south on the ring anyway. Still outside
// the nest, which is the thing this is here to guarantee.
Vector3 fallback = centre + new Vector3(0f, 0f, -radius);
if (ZoneSystem.instance != null) fallback.y = ZoneSystem.instance.GetGroundHeight(fallback);
return fallback + Vector3.up * kind.SpawnHeight;
}
/// <summary>
/// How wide the ring is: the nest's own footprint plus a couple of metres.
///
/// MEASURED and not a number, because the nest grows with its stage - a stage four nest is
/// several times the width of the one that was placed, so any fixed radius that clears the
/// small one hatches troops inside the big one. Only solid meshes are measured; the hive's
/// particle effects have bounds of their own and would report a ring the size of a field.
/// </summary>
private static float RingRadius(SvermHiveBehaviour hive)
{
Vector3 centre = hive.transform.position;
float widest = 0f;
foreach (Renderer renderer in hive.GetComponentsInChildren<Renderer>(true))
{
if (renderer == null) continue;
if (!(renderer is MeshRenderer) && !(renderer is SkinnedMeshRenderer)) continue;
Bounds bounds = renderer.bounds; // world space, so the stage's growth is already in it
float reach = Mathf.Max(Mathf.Abs(bounds.center.x - centre.x) + bounds.extents.x,
Mathf.Abs(bounds.center.z - centre.z) + bounds.extents.z);
if (reach > widest) widest = reach;
}
// Nothing readable means nothing to be stuck in either, but the floor keeps a newborn off
// the nest's own doorstep, and the ceiling stops one measuring mistake spawning troops
// half a zone away.
return Mathf.Clamp(widest, 2f, 20f) + SpawnClearance;
}
}
/// <summary>

View file

@ -111,7 +111,25 @@ namespace RacesOfValheim
wear.m_hitEffect = new EffectList();
}
if (prefab.GetComponent<ZNetView>() == null) prefab.AddComponent<ZNetView>();
// PERSISTENT, EXPLICITLY, and this is the whole reason a nest could vanish at world
// load. Every other station in the mod is cloned from a real building - a workbench, a
// barrel, a cartography table - and inherits a ZNetView that is already persistent. The
// nest is the one piece built out of SCENERY, and a CreepProp is not saved with the
// world: it belongs to the location that placed it and is put back by the location, so
// its ZNetView (or the bare one added here, which defaults to false) marks the ZDO as
// throwaway. The hive stood, worked, took biomass - and was not written to the save.
// Next load, no ZDO, no nest.
//
// m_syncInitialScale stays OFF on purpose. It looks like the obvious partner to this
// line and it would be a bug: the stage growth is applied to the transform at runtime,
// so a ZNetView that stored the scale would restore the GROWN size, Awake would read it
// back as builtScale, and the nest would multiply itself by its stage again every
// reload. The size is derived from the stage on the ZDO instead, by every client.
ZNetView view = prefab.GetComponent<ZNetView>() ?? prefab.AddComponent<ZNetView>();
view.m_persistent = true;
view.m_distant = false;
view.m_type = ZDO.ObjectType.Default;
prefab.AddComponent<SvermHiveBehaviour>();
MountMouths(prefab);

View file

@ -336,7 +336,13 @@ namespace RacesOfValheim
wear.m_hitEffect = new EffectList();
}
if (prefab.GetComponent<ZNetView>() == null) prefab.AddComponent<ZNetView>();
// The workbench this is cloned from brings a persistent ZNetView with it, so this is
// belt as well as braces - but it is the line the NEST turned out to need after players
// reported hives gone at world load, and a counter that saves only because of what
// it was copied from is one refactor away from not saving at all.
ZNetView view = prefab.GetComponent<ZNetView>() ?? prefab.AddComponent<ZNetView>();
view.m_persistent = true;
prefab.AddComponent<SvermHiveMarket>();
Jotunn.Managers.PieceManager.Instance.AddPiece(new Jotunn.Entities.CustomPiece(prefab, fixReference: false,