warvulture

This commit is contained in:
Ferdinando 2026-08-09 21:54:56 +02:00
commit e9f41a86e3
11 changed files with 2318 additions and 7 deletions

View file

@ -35,6 +35,20 @@ namespace RacesOfValheim
private static readonly Dictionary<string, Limit> Limits = new Dictionary<string, Limit>();
/// <summary>
/// Extra placement rules, one per piece, for the chains whose cap cannot be expressed as
/// "how many of THIS piece stand nearby".
///
/// The Skadi's golem is the case that forced this: the piece it builds removes itself a frame
/// later and leaves a creature behind, so counting the piece counts nothing, and one of its two
/// caps is per-stonecutter rather than per-area. A gate is handed the same player and the same
/// placement point the simple limit gets, says yes or no, and is responsible for its own
/// refusal message - which is the whole extension: the counting is the chain's business, being
/// the ONE Player.PlacePiece prefix in the mod is still this class's.
/// </summary>
private static readonly Dictionary<string, System.Func<Player, Vector3, bool>> Gates =
new Dictionary<string, System.Func<Player, Vector3, bool>>();
/// <summary>
/// The radius of a circle with the given area, so a limit can be written the way it is
/// actually discussed - "five per 1.6 square kilometres" - instead of as a magic distance.
@ -61,6 +75,45 @@ namespace RacesOfValheim
Jotunn.Logger.LogInfo($"[Density] {piecePrefabName}: at most {max} within {radius:0} m.");
}
/// <summary>
/// Adds a rule of the chain's own to a piece. Same call-once-from-Register() contract as
/// <see cref="Register"/>, and a second call replaces the first rather than stacking.
/// </summary>
public static void RegisterGate(string piecePrefabName, System.Func<Player, Vector3, bool> gate)
{
if (string.IsNullOrEmpty(piecePrefabName) || gate == null)
{
Jotunn.Logger.LogWarning($"[Density] Refusing a nonsense gate for '{piecePrefabName}'.");
return;
}
Gates[piecePrefabName] = gate;
Jotunn.Logger.LogInfo($"[Density] {piecePrefabName}: a rule of its own runs at placement.");
}
/// <summary>
/// Every ZDO in the world carrying that prefab. False when the world cannot be asked right
/// now, which callers must read as "no information" rather than as "none".
///
/// ZDOs and not loaded GameObjects, for the reason at the top of this file, and public
/// because the Skadi's golem counts by an anchor written INTO each ZDO rather than by
/// distance - so it needs the ZDOs themselves and not a tally of them.
/// </summary>
public static bool Gather(string prefabName, List<ZDO> into)
{
if (ZDOMan.instance == null || into == null) return false;
int index = 0;
// The iterative sweep hands back a chunk per call and reports true when it is done. The
// guard is so a pathological world cannot spin here forever.
for (int guard = 0; guard < 1000; guard++)
{
if (ZDOMan.instance.GetAllZDOsWithPrefabIterative(prefabName, into, ref index)) break;
}
return true;
}
/// <summary>
/// How many of that piece already stand within the radius of a point, or -1 if the world
/// cannot be asked right now - in which case callers must let the placement through rather
@ -68,16 +121,8 @@ namespace RacesOfValheim
/// </summary>
public static int Count(string piecePrefabName, Vector3 position, float radius)
{
if (ZDOMan.instance == null) return -1;
List<ZDO> zdos = new List<ZDO>();
int index = 0;
// The iterative sweep hands back a chunk per call and reports true when it is done. The
// guard is so a pathological world cannot spin here forever.
for (int guard = 0; guard < 1000; guard++)
{
if (ZDOMan.instance.GetAllZDOsWithPrefabIterative(piecePrefabName, zdos, ref index)) break;
}
if (!Gather(piecePrefabName, zdos)) return -1;
float rangeSq = radius * radius;
int count = 0;
@ -96,6 +141,14 @@ namespace RacesOfValheim
/// </summary>
public static bool Allow(Player player, string piecePrefabName, Vector3 position)
{
// The chain's own rule first: it is the more specific of the two, so it should be the one
// that gets to explain itself when both would refuse.
if (Gates.TryGetValue(piecePrefabName, out System.Func<Player, Vector3, bool> gate) &&
!gate(player, position))
{
return false;
}
if (!Limits.TryGetValue(piecePrefabName, out Limit limit)) return true;
int nearby = Count(piecePrefabName, position, limit.Radius);