using System; using System.Collections.Generic; namespace RynthCore.Plugin.RynthAi; // ============================================================================ // Environment-impact throttle (2026-10-12): pure rules and bookkeeping. // // A missile or a war/void projectile that hits a wall, a pillar, the ceiling // or another object short of the target does no damage, and before this the bot // kept firing into it until the no-damage blacklist (several settle windows) or // the no-progress timeout gave up. Now, once a target has taken // EnvironmentImpactLimit such hits within EnvironmentImpactWindowSec, combat // stops attacking it and does what the player chose for that kind of combat: // move closer, pick another target, or move closer once and then pick another. // // The signals (CombatManager.EnvironmentImpact.cs): // missile ACE ProjectileCollisionHelper.OnCollideEnvironment sends the shooter // "Your missile attack hit the environment." (also when the arrow hits // an object that isn't its target). // magic ACE sends nothing (SpellProjectile.OnCollideEnvironment only // explodes the projectile), so the bot follows its own projectiles and // judges where they stopped (ProjectileImpactJudge). // // Everything here is pure (no host, no clock of its own) so the host tests can // drive it directly. // ============================================================================ /// What combat does once a target has taken too many environment hits. /// Stored as an int in the settings (MissileEnvironmentResponse / MagicEnvironmentResponse). public enum EnvironmentResponse { /// No detection; keep attacking (the behaviour before 2026-10-12). Off = 0, /// Move toward the target each time; once already close, pick another target. MoveCloser = 1, /// Skip the target for a while and pick another (moves closer when there is no other). NewTarget = 2, /// Move closer once; if the shots still hit the environment, pick another target. CloserThenNewTarget = 3, } /// The step combat takes for one throttle trip. public enum EnvironmentAction { None, Approach, NewTarget } /// Choices and the decision for one throttle trip. public static class EnvironmentImpactPlanner { /// Combo labels, in order (plugin and engine settings panels). public static readonly string[] ResponseNames = { "Off", "Move closer", "New target", "Move closer, then new target" }; /// Approaching is pointless within this much of the closest allowed distance. public const double ApproachSlackMeters = 0.5; /// A saved int as a response; anything out of range is Off. public static EnvironmentResponse Parse(int value) => value is >= 0 and <= 3 ? (EnvironmentResponse)value : EnvironmentResponse.Off; /// The label for a saved value (logs and the settings panels). public static string Name(int value) => ResponseNames[(int)Parse(value)]; /// /// The step for a throttle trip on a target metres away that /// has already been approached times for this reason. /// Never approaches nearer than . A new target needs another /// monster in reach: skipping the only one would leave it free to hit the bot while combat /// idles, so then the bot moves closer if it still can, or keeps attacking (None). /// public static EnvironmentAction Decide(EnvironmentResponse response, int approachesDone, double distance, double minDistance, bool hasAlternateTarget) { bool canApproach = double.IsFinite(distance) && distance > minDistance + ApproachSlackMeters; EnvironmentAction approachElseNone = canApproach ? EnvironmentAction.Approach : EnvironmentAction.None; EnvironmentAction newTargetElse = hasAlternateTarget ? EnvironmentAction.NewTarget : approachElseNone; return response switch { EnvironmentResponse.MoveCloser => canApproach ? EnvironmentAction.Approach : newTargetElse, EnvironmentResponse.NewTarget => newTargetElse, EnvironmentResponse.CloserThenNewTarget => approachesDone == 0 && canApproach ? EnvironmentAction.Approach : newTargetElse, _ => EnvironmentAction.None, }; } /// /// Where an approach stops: nearer than now, never nearer /// than . /// public static double ApproachGoal(double distance, double stepMeters, double minDistance) => Math.Max(Math.Max(0.5, minDistance), distance - Math.Max(1.0, stepMeters)); } /// /// Environment hits per target inside a sliding window, and how many times each target has /// been approached for them. Pump thread only. /// public sealed class EnvironmentImpactThrottle { private readonly Dictionary> _hits = new(); private readonly Dictionary _approaches = new(); // A long session meets thousands of monsters; ids are forgotten on delete, this is the backstop. private const int MaxTracked = 256; /// Records one hit on and returns the hits within the window. public int Record(int targetId, DateTime now, double windowSec) { if (targetId == 0) return 0; if (_hits.Count >= MaxTracked && !_hits.ContainsKey(targetId)) _hits.Clear(); if (!_hits.TryGetValue(targetId, out var list)) _hits[targetId] = list = new List(4); list.Add(now); Prune(list, now, windowSec); return list.Count; } /// Hits on within the window (no new hit). public int Count(int targetId, DateTime now, double windowSec) { if (!_hits.TryGetValue(targetId, out var list)) return 0; Prune(list, now, windowSec); return list.Count; } /// Starts the count again (after the bot acted on it). public void ResetHits(int targetId) => _hits.Remove(targetId); public int Approaches(int targetId) => _approaches.TryGetValue(targetId, out int n) ? n : 0; public void NoteApproach(int targetId) { if (_approaches.Count >= MaxTracked && !_approaches.ContainsKey(targetId)) _approaches.Clear(); _approaches[targetId] = Approaches(targetId) + 1; } /// The object is gone (deleted): a respawn reusing the id starts clean. public void Forget(int targetId) { _hits.Remove(targetId); _approaches.Remove(targetId); } public void Clear() { _hits.Clear(); _approaches.Clear(); } private static void Prune(List list, DateTime now, double windowSec) { double window = Math.Max(1.0, windowSec); list.RemoveAll(t => (now - t).TotalSeconds > window); } } /// /// Where a spell projectile stopped, against where the target is. A projectile that reaches /// the target stops at its surface; one that hits the environment stops short of it, toward /// the caster. Both conditions must hold, so a large monster (its surface a few metres from its /// centre) isn't read as a wall. The caller also requires that no hit, resist or evade line /// arrived for the cast: that is the final word for a large monster or an odd stop. /// public static class ProjectileImpactJudge { /// A stop farther than this from the target's centre didn't touch it. public const double MissRadiusMeters = 4.0; /// A stop at least this much nearer to the caster than the target is "short". public const double ShortByMeters = 2.0; /// A projectile that flew less than this never really left (or was never ours). public const double MinTravelMeters = 1.0; /// True when the projectile stopped short of the target and away from it. public static bool StoppedShort(double impactToTarget, double casterToImpact, double casterToTarget) { if (!double.IsFinite(impactToTarget) || !double.IsFinite(casterToImpact) || !double.IsFinite(casterToTarget)) return false; return impactToTarget > MissRadiusMeters && casterToImpact + ShortByMeters < casterToTarget; } }