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>