798 lines
41 KiB
C#
798 lines
41 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace RynthCore.Plugin.RynthAi;
|
|
|
|
/// <summary>
|
|
/// Outdoor hunting (2026-10-23): walk from one monster to the next around obstacles. Pure: the plugin
|
|
/// (RynthAiPlugin.OutdoorHunt.cs) passes the character's position, the monsters combat would fight
|
|
/// (line of sight not needed: the walk goes round), a path function (RynthNav's navmesh, which holds
|
|
/// buildings, their insides, trees, rocks and slopes too steep to climb) and the clock, and installs the
|
|
/// route a <see cref="Decision"/> asks for. Combat takes over by itself once a monster is within its
|
|
/// range and in sight (the arbiter ranks it above navigation); this is asked again only when nothing
|
|
/// else wants the character.
|
|
///
|
|
/// The next monster is the one with the shortest walk (a higher Priority first), among the few nearest
|
|
/// in a straight line. A monster with no walkable way to it (on a cliff, in water, inside a closed
|
|
/// building) is left out for <see cref="SkipSec"/>. Monsters are looked for out to <see cref="RangeYd"/>
|
|
/// (Hunt Range); <see cref="RadiusYd"/> (Hunt Radius) keeps the picks within that far of where it started
|
|
/// (0 = anywhere). With nothing in range it waits where it is. That is <see cref="PickMode.Nearest"/>;
|
|
/// since 2026-11-08 the setting can go for the biggest pack or the most experience instead (PickPack).
|
|
///
|
|
/// Buildings (2026-10-25, Tom: "the entire radar range, also going inside houses"): RynthNav's map ends
|
|
/// at a building's walls, so a walk into one is RynthNav's way to the outside of one of its doors, then
|
|
/// the building's own rooms (<see cref="IBuildings"/>: BuildingMap on cell.dat) to the monster; the door
|
|
/// giving the shortest walk in all is taken. From inside a building: a monster in the same building
|
|
/// through its rooms, one outdoors (or in another building) out through a door first. A building with
|
|
/// no door known is walked out of the way it was walked into (<see cref="Indoors"/>, the breadcrumbs).
|
|
/// </summary>
|
|
internal sealed class OutdoorHunt
|
|
{
|
|
internal enum PathStatus { Ok, Partial, NoPath, NoMesh, Unavailable }
|
|
|
|
/// <summary>Why the hunt can't start here (None: it can). The Hunt button and Hunt On Login ask the same.</summary>
|
|
internal enum StartBlock { None, NoSettings, NoPosition, InDungeon, UnknownBuilding, NoRynthNav }
|
|
|
|
/// <summary>
|
|
/// The checks before a start, in order, each asked only when the ones before it passed (the later
|
|
/// ones read cell.dat and RynthNav): settings, a position, not in a dungeon, not in a building whose
|
|
/// doors it doesn't know, RynthNav's map there.
|
|
/// </summary>
|
|
internal static StartBlock WhyNoStart(bool hasSettings, bool hasPosition,
|
|
Func<bool> inDungeon, Func<bool> unknownBuilding, Func<bool> noRynthNav)
|
|
{
|
|
if (!hasSettings) return StartBlock.NoSettings;
|
|
if (!hasPosition) return StartBlock.NoPosition;
|
|
if (inDungeon()) return StartBlock.InDungeon;
|
|
if (unknownBuilding()) return StartBlock.UnknownBuilding;
|
|
if (noRynthNav()) return StartBlock.NoRynthNav;
|
|
return StartBlock.None;
|
|
}
|
|
|
|
/// <summary>What chat says for a <see cref="StartBlock"/> (after "[RynthAi] ").</summary>
|
|
internal static string StartBlockText(StartBlock b) => b switch
|
|
{
|
|
StartBlock.NoSettings => "Settings not ready.",
|
|
StartBlock.NoPosition => "Outdoor hunt: no position yet.",
|
|
StartBlock.InDungeon => "Outdoor hunt is for outdoors; in a dungeon the Dungeon Patrol hunts (Patrol).",
|
|
StartBlock.UnknownBuilding => "Outdoor hunt: start it outdoors (it doesn't know this building's doors; it finds its own way out of a building it went into).",
|
|
StartBlock.NoRynthNav => "Outdoor hunt needs RynthNav 2026.10.23 or newer (its map shows the way round obstacles).",
|
|
_ => "",
|
|
};
|
|
|
|
/// <summary>A planned walk: the corners after the start (the last is the goal), its length, and for a
|
|
/// partial path how far its end is from the goal.</summary>
|
|
internal readonly record struct PathResult(PathStatus Status, IReadOnlyList<Vec> Corners, double Length, double Gap);
|
|
|
|
/// <summary>A spot (world units: x east, y north, z up). <see cref="Door"/>: a point lined up on a
|
|
/// doorway (walked through straight, NavPoint.Doorway).</summary>
|
|
internal readonly record struct Vec(double X, double Y, double Z, bool Door = false)
|
|
{
|
|
public double DistanceTo(Vec o) => Math.Sqrt((X - o.X) * (X - o.X) + (Y - o.Y) * (Y - o.Y) + (Z - o.Z) * (Z - o.Z));
|
|
public double FlatDistanceTo(Vec o) => Math.Sqrt((X - o.X) * (X - o.X) + (Y - o.Y) * (Y - o.Y));
|
|
}
|
|
|
|
/// <summary>A monster combat would fight. <see cref="Cell"/>: its landcell (a building's cells are 0x0100 up).</summary>
|
|
internal readonly record struct Candidate(int Id, string Name, Vec Pos, double Distance, int Priority, uint Cell = 0);
|
|
|
|
/// <summary>
|
|
/// A building's door to the outside, in <see cref="Cell"/>: <see cref="At"/> its centre at floor level,
|
|
/// <see cref="Inner"/> a step inside it (in Cell), <see cref="Outside"/> a step out of it (on the
|
|
/// landscape, where RynthNav's way to or from the door ends or starts).
|
|
/// </summary>
|
|
internal readonly record struct Door(uint Cell, Vec Inner, Vec At, Vec Outside);
|
|
|
|
/// <summary>What the hunt knows of the buildings round it: BuildingMap (cell.dat) in the plugin, fakes in the tests.</summary>
|
|
internal interface IBuildings
|
|
{
|
|
/// <summary>The building holding <paramref name="cell"/> (any id but 0); 0 for a cell outdoors or one no known building holds.</summary>
|
|
int BuildingOf(uint cell);
|
|
/// <summary>The building's doors to the outside that can be walked through (none known: empty).</summary>
|
|
IReadOnlyList<Door> Doors(int building);
|
|
/// <summary>
|
|
/// The walk inside one building from <paramref name="from"/> (in fromCell) to <paramref name="to"/>
|
|
/// (in toCell), through its rooms and doorways and kept out of portals: corners after the start
|
|
/// (the last is the goal, or for Partial the nearest it gets), its length. NoPath when the two
|
|
/// don't join inside.
|
|
/// </summary>
|
|
PathResult Inside(uint fromCell, Vec from, uint toCell, Vec to);
|
|
/// <summary>A short name for log lines.</summary>
|
|
string Name(int building);
|
|
}
|
|
|
|
/// <summary>A building's (or a dungeon's) cell, not the landscape's.</summary>
|
|
internal static bool IsIndoorCell(uint cell) => (cell & 0xFFFF) >= 0x0100;
|
|
|
|
/// <summary>A walk planned by <see cref="PlanWalk"/>: the path, the door it leaves a building by and
|
|
/// the one it goes into a building by (null: none), the buildings (0: outdoors), why it failed.</summary>
|
|
internal readonly record struct HuntWalk(PathResult P, Door? Out = null, Door? In = null, int From = 0, int To = 0, string Why = "");
|
|
|
|
internal enum Act
|
|
{
|
|
/// <summary>Keep doing what the last decision said.</summary>
|
|
None,
|
|
/// <summary>Walk <see cref="Decision.Corners"/> (replacing the hunt's route).</summary>
|
|
Walk,
|
|
/// <summary>Stand (take the hunt's route off): nothing to hunt and nowhere to go, or already at the
|
|
/// end of the walk to <see cref="Decision.TargetId"/>.</summary>
|
|
Idle,
|
|
}
|
|
|
|
internal readonly record struct Decision(Act Act, IReadOnlyList<Vec>? Corners = null, int TargetId = 0, string Why = "");
|
|
|
|
// Settings (the plugin copies them in before each Tick).
|
|
/// <summary>Hunt Range: 192 by default since 2026-10-25, the radar's own default range (Tom: "the entire radar range").</summary>
|
|
public double RangeYd { get; set; } = DefaultRangeYd;
|
|
public const double DefaultRangeYd = 192;
|
|
public double RadiusYd { get; set; } = 0;
|
|
|
|
/// <summary>A partial path whose end is within this of the monster still counts (the last step is combat's).</summary>
|
|
public double ReachYd { get; set; } = 3;
|
|
/// <summary>
|
|
/// A walk whose end is more than this below or above the monster doesn't reach it (2026-11-12): RynthNav
|
|
/// finds the ground up to 64 yd under a goal, so a monster on a roof, a ledge or the floor above read as
|
|
/// reached from the spot under it, and the character stood there swinging up at it.
|
|
/// </summary>
|
|
public double ReachRiseYd { get; set; } = 3;
|
|
/// <summary>
|
|
/// A walk longer than this times Hunt Range is too far round: the first test hunt (10-23) chose a buff bot
|
|
/// 12 yd away inside a building, a 143 yd walk; the walk led her past Hunt Range from it, she gave up,
|
|
/// turned back, and chose it again, every second.
|
|
/// </summary>
|
|
public double MaxWalkFactor { get; set; } = 2.0;
|
|
/// <summary>
|
|
/// The plugin asks for monsters out to this times Hunt Range: new picks stay within Hunt Range, but the
|
|
/// one being walked to is kept while the walk round an obstacle takes the character further from it.
|
|
/// </summary>
|
|
public const double KeepFactor = 2.0;
|
|
/// <summary>A monster with no way to it is left out this long.</summary>
|
|
public double SkipSec { get; set; } = 120;
|
|
/// <summary>How many of the nearest monsters get a path asked for when choosing.</summary>
|
|
public int PickCandidates { get; set; } = 6;
|
|
public double PickEverySec { get; set; } = 1.0;
|
|
/// <summary>Plan again when the monster has moved this far from where the walk was planned to.</summary>
|
|
public double ReplanMovedYd { get; set; } = 4;
|
|
public double ReplanMinSec { get; set; } = 1.0;
|
|
/// <summary>Plan again at least this often while walking to a monster (it may have turned).</summary>
|
|
public double ReplanMaxSec { get; set; } = 8;
|
|
/// <summary>A walk this short: the character is already where the walk to the monster ends.</summary>
|
|
public double ArrivedYd { get; set; } = 2;
|
|
/// <summary>
|
|
/// At the end of the walk to a monster this long and combat hasn't taken it (the hunt is only asked
|
|
/// when nothing else wants the character): something stops combat, a wall or a fence between, and the
|
|
/// monster is left out for <see cref="SkipSec"/>. a test character (10-23) stood at a wall re-walking 1 yd
|
|
/// every half second to a monster on its far side: the walk's end, 1.3 yd away, counted as reached.
|
|
/// </summary>
|
|
public double NoFightSec { get; set; } = 2;
|
|
|
|
/// <summary>
|
|
/// What the next monster is chosen by (2026-11-08, Tom: "it should be a setting to go for nearest
|
|
/// spawn or most dense or highest experience", and "right now it does circles when only 1 mob has
|
|
/// respawned"): the setting OutdoorHuntPick, 0 / 1 / 2.
|
|
/// </summary>
|
|
internal enum PickMode
|
|
{
|
|
/// <summary>The shortest walk among the nearest few (the hunt as it was before the setting).</summary>
|
|
Nearest = 0,
|
|
/// <summary>The biggest pack: the monsters within <see cref="PackYd"/> of each other, counted.</summary>
|
|
Pack = 1,
|
|
/// <summary>The pack worth the most experience (the learned XP per kill, <see cref="XpLookup"/>).</summary>
|
|
Xp = 2,
|
|
}
|
|
|
|
public PickMode Pick { get; set; } = PickMode.Nearest;
|
|
/// <summary>Monsters this close to a monster are in its pack.</summary>
|
|
public double PackYd { get; set; } = 20;
|
|
/// <summary>
|
|
/// A pack's worth is divided by (1 + its walk / this): a pack this far away counts half, twice as far
|
|
/// a third, so a near pack wins unless a farther one is clearly bigger (a 5-pack 80 yd off beats a
|
|
/// lone monster 10 yd off; a 3-pack 180 yd off doesn't).
|
|
/// </summary>
|
|
public double PackFarYd { get; set; } = 60;
|
|
/// <summary>Most monsters looked at for packs (nearest first): the pack count is every pair.</summary>
|
|
public const int MaxPackCandidates = 300;
|
|
/// <summary>The learned XP per kill of a monster by name (null: not known), for <see cref="PickMode.Xp"/>.</summary>
|
|
public Func<string, double?>? XpLookup { get; set; }
|
|
|
|
public bool Active { get; private set; }
|
|
public Vec Anchor { get; private set; }
|
|
public int TargetId { get; private set; }
|
|
/// <summary>The last thing worth a log line (the plugin logs it when it changes).</summary>
|
|
public string Note
|
|
{
|
|
get => _note;
|
|
private set { _note = value; _notes.Add(value); }
|
|
}
|
|
private string _note = "";
|
|
private readonly List<string> _notes = new();
|
|
/// <summary>Every note since <see cref="ClearNotes"/>: one tick can have several (one left out, then the next chosen).</summary>
|
|
internal IReadOnlyList<string> PendingNotes => _notes;
|
|
internal void ClearNotes() => _notes.Clear();
|
|
|
|
private Vec _plannedTo;
|
|
private DateTime _lastPlan = DateTime.MinValue, _lastPick = DateTime.MinValue;
|
|
private DateTime _arrivedSince = DateTime.MinValue; // at the walk's end with no fight since (NoFightSec)
|
|
private readonly Dictionary<int, DateTime> _skip = new();
|
|
private readonly List<(Candidate C, HuntWalk W)> _viable = new();
|
|
private Func<Vec, Vec, PathResult> _path = (_, _) => new PathResult(PathStatus.Unavailable, Array.Empty<Vec>(), 0, 0);
|
|
private IBuildings? _buildings;
|
|
private int _meBuilding; // the building the character is in this tick (0: outdoors)
|
|
|
|
public void Start(DateTime now, Vec at)
|
|
{
|
|
Active = true;
|
|
Anchor = at;
|
|
TargetId = 0;
|
|
_skip.Clear();
|
|
_lastPlan = _lastPick = _arrivedSince = DateTime.MinValue;
|
|
Note = $"started at {at.X:0},{at.Y:0}";
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
Active = false;
|
|
TargetId = 0;
|
|
WalkingOut = false;
|
|
_outTries = 0;
|
|
}
|
|
|
|
/// <summary>Left out of the hunt for a while (no way to it, or combat gave up on it).</summary>
|
|
public void Skip(int id, DateTime now, double sec = -1) => _skip[id] = now.AddSeconds(sec < 0 ? SkipSec : sec);
|
|
|
|
public bool IsSkipped(int id, DateTime now)
|
|
{
|
|
if (!_skip.TryGetValue(id, out var until)) return false;
|
|
if (now < until) return true;
|
|
_skip.Remove(id);
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The next step. <paramref name="routeDone"/>: the last walk ran out (arrived, or navigation stopped it).
|
|
/// <paramref name="candidates"/>: sorted nearest first. Called only when nothing else wants the character.
|
|
/// </summary>
|
|
public Decision Tick(DateTime now, Vec me, bool routeDone, IReadOnlyList<Candidate> candidates, Func<Vec, Vec, PathResult> path)
|
|
=> Tick(now, me, 0, routeDone, candidates, path, null);
|
|
|
|
/// <summary>
|
|
/// The next step, with the character's cell <paramref name="meCell"/> and the buildings round it
|
|
/// (<paramref name="buildings"/>, null = none known: monsters in buildings are left alone). In a
|
|
/// building no door of which is known, the walk back out the way in (<see cref="Indoors"/>).
|
|
/// </summary>
|
|
public Decision Tick(DateTime now, Vec me, uint meCell, bool routeDone, IReadOnlyList<Candidate> candidates,
|
|
Func<Vec, Vec, PathResult> path, IBuildings? buildings)
|
|
{
|
|
if (!Active) return new Decision(Act.None);
|
|
_path = path;
|
|
_buildings = buildings;
|
|
_now = now;
|
|
_meBuilding = 0;
|
|
if (IsIndoorCell(meCell))
|
|
{
|
|
int inB = buildings?.BuildingOf(meCell) ?? 0;
|
|
if (inB == 0 || buildings!.Doors(inB).Count == 0) return Indoors(now, me, routeDone);
|
|
_meBuilding = inB;
|
|
WalkingOut = false; // the doors take it out from here
|
|
}
|
|
|
|
// Still walking to a monster that is still there: plan again when it moved, the walk ran out, or a while passed.
|
|
if (TargetId != 0)
|
|
{
|
|
Candidate? t = null;
|
|
foreach (var c in candidates)
|
|
if (c.Id == TargetId) { t = c; break; }
|
|
if (t is Candidate target && InArea(target.Pos) && !IsSkipped(target.Id, now) && Plannable(target))
|
|
{
|
|
double since = (now - _lastPlan).TotalSeconds;
|
|
bool moved = target.Pos.DistanceTo(_plannedTo) > ReplanMovedYd && since >= ReplanMinSec;
|
|
bool due = (routeDone && since >= 0.5) || since >= ReplanMaxSec;
|
|
if (!moved && !due) return new Decision(Act.None, TargetId: TargetId);
|
|
var w = PlanWalk(me, meCell, target.Pos, target.Cell);
|
|
var p = w.P;
|
|
if (Reaches(p, target.Pos))
|
|
{
|
|
_plannedTo = target.Pos;
|
|
_lastPlan = now;
|
|
if (p.Length > ArrivedYd)
|
|
{
|
|
_arrivedSince = DateTime.MinValue;
|
|
return new Decision(Act.Walk, p.Corners, target.Id, $"to '{target.Name}' again ({p.Length:0} yd)");
|
|
}
|
|
// Already at the walk's end: stand, no 1 yd walk (it ends at once and is planned
|
|
// again every half second); combat has NoFightSec to take the monster.
|
|
if (_arrivedSince == DateTime.MinValue) _arrivedSince = now;
|
|
if ((now - _arrivedSince).TotalSeconds < NoFightSec) return new Decision(Act.Idle, TargetId: target.Id);
|
|
Skip(target.Id, now);
|
|
Note = $"'{target.Name}': at the end of the walk to it for {NoFightSec:0} s and combat hasn't taken it "
|
|
+ $"(a wall or a fence between?): left out for {SkipSec:0} s";
|
|
}
|
|
else
|
|
{
|
|
Skip(target.Id, now);
|
|
Note = $"'{target.Name}' can't be reached any more ({Describe(p, w.Why, target.Pos)}): left out for {SkipSec:0} s";
|
|
}
|
|
}
|
|
TargetId = 0;
|
|
_arrivedSince = DateTime.MinValue;
|
|
}
|
|
|
|
// Choosing (at most once every PickEverySec): the few nearest, by the walk to them.
|
|
if ((now - _lastPick).TotalSeconds < PickEverySec)
|
|
return new Decision(Act.Idle);
|
|
_lastPick = now;
|
|
if (Pick != PickMode.Nearest) return PickPack(now, me, meCell, candidates);
|
|
_viable.Clear();
|
|
int asked = 0;
|
|
foreach (var c in candidates)
|
|
{
|
|
if (asked >= PickCandidates) break;
|
|
if (c.Distance > RangeYd) break; // nearest first
|
|
if (!InArea(c.Pos) || IsSkipped(c.Id, now) || !Plannable(c)) continue;
|
|
asked++;
|
|
var w = PlanWalk(me, meCell, c.Pos, c.Cell);
|
|
var p = w.P;
|
|
if (p.Status == PathStatus.Unavailable) return new Decision(Act.Idle, Why: "no pathfinder");
|
|
if (Reaches(p, c.Pos)) _viable.Add((c, w));
|
|
else
|
|
{
|
|
Skip(c.Id, now);
|
|
Note = $"'{c.Name}' left out for {SkipSec:0} s: {Describe(p, w.Why, c.Pos)}";
|
|
}
|
|
}
|
|
if (_viable.Count > 0)
|
|
{
|
|
var best = _viable[0];
|
|
for (int i = 1; i < _viable.Count; i++)
|
|
{
|
|
var v = _viable[i];
|
|
if (v.C.Priority > best.C.Priority || (v.C.Priority == best.C.Priority && v.W.P.Length < best.W.P.Length)) best = v;
|
|
}
|
|
return Go(now, best.C, best.W);
|
|
}
|
|
return Waiting();
|
|
}
|
|
|
|
/// <summary>The chosen monster becomes the target: walked to, or stood by when the walk's end is already here.</summary>
|
|
private Decision Go(DateTime now, Candidate c, HuntWalk w, string pack = "")
|
|
{
|
|
TargetId = c.Id;
|
|
_plannedTo = c.Pos;
|
|
_lastPlan = now;
|
|
Note = NextNote(c, w, pack);
|
|
if (w.P.Length > ArrivedYd)
|
|
{
|
|
_arrivedSince = DateTime.MinValue;
|
|
return new Decision(Act.Walk, w.P.Corners, c.Id, Note);
|
|
}
|
|
_arrivedSince = now; // it's right here and combat hasn't taken it: NoFightSec, then left out
|
|
return new Decision(Act.Idle, TargetId: c.Id, Why: Note);
|
|
}
|
|
|
|
/// <summary>Nothing to hunt: wait where it is (no walk back to the start: Tom 10-23, "it shouldn't bother").</summary>
|
|
private Decision Waiting()
|
|
{
|
|
string wait = $"nothing within Hunt Range ({RangeYd:0} yd){(RadiusYd > 0 ? $" inside Hunt Radius ({RadiusYd:0} yd)" : "")}: waiting here";
|
|
if (_note != wait) Note = wait;
|
|
return new Decision(Act.Idle);
|
|
}
|
|
|
|
// ── Biggest pack / Most XP (2026-11-08) ────────────────────────────────────────────────────────
|
|
// Every monster that could be chosen has a pack: itself and the others within PackYd of it. Its
|
|
// worth is the pack's count (Biggest pack) or the sum of the pack's XP per kill (Most XP: a name
|
|
// with no XP learned counts as the average of the known ones here, or 1 when none is known, so
|
|
// it falls back to Biggest pack). Score = worth / (1 + walk / PackFarYd). A higher Priority in the
|
|
// pack still comes first. The packs are ranked by the straight line first, paths are asked only
|
|
// for the top PickCandidates, and those are scored again by the real walk; the target is the
|
|
// pack's member with the shortest walk.
|
|
|
|
private readonly List<Candidate> _eligible = new();
|
|
|
|
private Decision PickPack(DateTime now, Vec me, uint meCell, IReadOnlyList<Candidate> candidates)
|
|
{
|
|
_eligible.Clear();
|
|
foreach (var c in candidates)
|
|
{
|
|
if (c.Distance > RangeYd) break; // nearest first
|
|
if (!InArea(c.Pos) || IsSkipped(c.Id, now) || !Plannable(c)) continue;
|
|
_eligible.Add(c);
|
|
if (_eligible.Count >= MaxPackCandidates) break;
|
|
}
|
|
int n = _eligible.Count;
|
|
if (n == 0) return Waiting();
|
|
|
|
// Each monster's worth: 1, or its XP per kill.
|
|
var worth = new double[n];
|
|
bool xpKnown = false;
|
|
if (Pick == PickMode.Xp && XpLookup != null)
|
|
{
|
|
double sum = 0;
|
|
int known = 0;
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
double? x = XpLookup(_eligible[i].Name);
|
|
if (x is double v && double.IsFinite(v) && v > 0) { worth[i] = v; sum += v; known++; }
|
|
else worth[i] = -1;
|
|
}
|
|
xpKnown = known > 0;
|
|
double unknown = known > 0 ? sum / known : 1;
|
|
for (int i = 0; i < n; i++) if (worth[i] < 0) worth[i] = unknown;
|
|
}
|
|
else
|
|
for (int i = 0; i < n; i++) worth[i] = 1;
|
|
|
|
// Each monster's pack: its worth, its size, its highest Priority.
|
|
var packWorth = new double[n];
|
|
var packCount = new int[n];
|
|
var packPri = new int[n];
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
int pri = int.MinValue;
|
|
for (int j = 0; j < n; j++)
|
|
{
|
|
if (i != j && _eligible[i].Pos.DistanceTo(_eligible[j].Pos) > PackYd) continue;
|
|
packWorth[i] += worth[j];
|
|
packCount[i]++;
|
|
if (_eligible[j].Priority > pri) pri = _eligible[j].Priority;
|
|
}
|
|
packPri[i] = pri;
|
|
}
|
|
|
|
// Ranked by the straight line; paths for the top few only.
|
|
var order = new List<int>(n);
|
|
for (int i = 0; i < n; i++) order.Add(i);
|
|
var straight = new double[n];
|
|
for (int i = 0; i < n; i++) straight[i] = packWorth[i] / (1 + _eligible[i].Distance / PackFarYd);
|
|
order.Sort((a, b) =>
|
|
{
|
|
if (packPri[a] != packPri[b]) return packPri[b].CompareTo(packPri[a]);
|
|
int s = straight[b].CompareTo(straight[a]);
|
|
return s != 0 ? s : _eligible[a].Distance.CompareTo(_eligible[b].Distance);
|
|
});
|
|
|
|
var walks = new Dictionary<int, HuntWalk>(); // eligible index -> its walk (the ones that reach it)
|
|
int asked = 0;
|
|
foreach (int i in order)
|
|
{
|
|
if (asked >= PickCandidates) break;
|
|
asked++;
|
|
var c = _eligible[i];
|
|
var w = PlanWalk(me, meCell, c.Pos, c.Cell);
|
|
if (w.P.Status == PathStatus.Unavailable) return new Decision(Act.Idle, Why: "no pathfinder");
|
|
if (Reaches(w.P, c.Pos)) walks[i] = w;
|
|
else
|
|
{
|
|
Skip(c.Id, now);
|
|
Note = $"'{c.Name}' left out for {SkipSec:0} s: {Describe(w.P, w.Why, c.Pos)}";
|
|
}
|
|
}
|
|
if (walks.Count == 0) return Waiting();
|
|
|
|
// Scored again by the real walk.
|
|
int best = -1;
|
|
double bestScore = 0;
|
|
foreach (var (i, w) in walks)
|
|
{
|
|
double score = packWorth[i] / (1 + w.P.Length / PackFarYd);
|
|
if (best < 0 || packPri[i] > packPri[best]
|
|
|| (packPri[i] == packPri[best] && (score > bestScore || (score == bestScore && w.P.Length < walks[best].P.Length))))
|
|
{
|
|
best = i;
|
|
bestScore = score;
|
|
}
|
|
}
|
|
|
|
// The target: the pack's member with the highest Priority, then the shortest walk (best itself if no other was planned).
|
|
int target = best;
|
|
foreach (var (i, w) in walks)
|
|
{
|
|
if (i != best && _eligible[i].Pos.DistanceTo(_eligible[best].Pos) > PackYd) continue;
|
|
var t = _eligible[target];
|
|
var c = _eligible[i];
|
|
if (c.Priority > t.Priority || (c.Priority == t.Priority && w.P.Length < walks[target].P.Length)) target = i;
|
|
}
|
|
|
|
int count = packCount[best];
|
|
string pack = count == 1 ? ", on its own" : $", pack of {count}";
|
|
if (Pick == PickMode.Xp) pack += xpKnown ? $" ({packWorth[best]:N0} XP)" : " (no XP learned yet: by size)";
|
|
return Go(now, _eligible[target], walks[target], pack);
|
|
}
|
|
|
|
// ── Indoors ──────────────────────────────────────────────────────────────────────────────────
|
|
// A building's rooms are off RynthNav's landscape map: it plans no walk from in there, and every
|
|
// monster came back "not on the map" (a test hunt 10-23, after combat chased one inside: the hunt left
|
|
// them all out and stood there). Combat still fights in there; for the walk back out, the way in.
|
|
// Since 2026-10-25 this is the fallback: Tick walks out through a door of a building it knows
|
|
// (IBuildings), and comes here only for one with no door known (or no building map at all).
|
|
|
|
private readonly List<Vec> _wayIn = new(); // [0] the last spot outdoors, then the steps inside
|
|
private bool _knowsOutdoors;
|
|
private Vec _lastOutdoors;
|
|
private DateTime _outPlannedAt = DateTime.MinValue;
|
|
private Vec _outEnd;
|
|
private int _outTries;
|
|
|
|
/// <summary>A step of the way in is kept every this far.</summary>
|
|
public double StepYd { get; set; } = 1.5;
|
|
/// <summary>The walk out ends this far past the last spot outdoors (out of the doorway).</summary>
|
|
public double OutPastYd { get; set; } = 3;
|
|
/// <summary>Walks out that ended still inside, before giving up (waiting for the character to come out).</summary>
|
|
public int OutTriesMax { get; set; } = 3;
|
|
internal const int MaxSteps = 256;
|
|
|
|
public bool WalkingOut { get; private set; }
|
|
internal int WayInSteps => _wayIn.Count;
|
|
|
|
/// <summary>Every tick, whatever the character is doing: where it is, and whether that is in a building.</summary>
|
|
public void Track(Vec me, bool indoors)
|
|
{
|
|
if (!indoors)
|
|
{
|
|
_wayIn.Clear();
|
|
_lastOutdoors = me;
|
|
_knowsOutdoors = true;
|
|
WalkingOut = false;
|
|
_outTries = 0;
|
|
return;
|
|
}
|
|
if (_wayIn.Count == 0)
|
|
{
|
|
if (!_knowsOutdoors) return;
|
|
_wayIn.Add(_lastOutdoors);
|
|
}
|
|
// Back near a step already on the way: the loop since is cut, so the way out stays the shortest known.
|
|
for (int i = 0; i < _wayIn.Count - 1; i++)
|
|
{
|
|
if (_wayIn[i].FlatDistanceTo(me) < StepYd)
|
|
{
|
|
_wayIn.RemoveRange(i + 1, _wayIn.Count - i - 1);
|
|
return;
|
|
}
|
|
}
|
|
if (_wayIn[^1].FlatDistanceTo(me) >= StepYd && _wayIn.Count < MaxSteps) _wayIn.Add(me);
|
|
}
|
|
|
|
/// <summary>The walk back out: the steps in, newest first, the last spot outdoors, and <see cref="OutPastYd"/> past it; null when no way in is known.</summary>
|
|
internal List<Vec>? WayOut(Vec me)
|
|
{
|
|
if (_wayIn.Count == 0) return null;
|
|
var way = new List<Vec>(_wayIn.Count + 1);
|
|
for (int i = _wayIn.Count - 1; i >= 1; i--)
|
|
if (_wayIn[i].FlatDistanceTo(me) >= 1.0) way.Add(_wayIn[i]);
|
|
Vec door = _wayIn[0];
|
|
way.Add(door);
|
|
Vec from = way.Count >= 2 ? way[^2] : me;
|
|
double dx = door.X - from.X, dy = door.Y - from.Y, len = Math.Sqrt(dx * dx + dy * dy);
|
|
if (len > 0.1) way.Add(new Vec(door.X + dx / len * OutPastYd, door.Y + dy / len * OutPastYd, door.Z));
|
|
return way;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The hunt's step while the character is in a building (instead of <see cref="Tick"/>, which plans on
|
|
/// the landscape): walk back out the way in. <paramref name="routeDone"/>: the last walk ran out.
|
|
/// </summary>
|
|
public Decision Indoors(DateTime now, Vec me, bool routeDone)
|
|
{
|
|
if (!Active) return new Decision(Act.None);
|
|
if (WalkingOut)
|
|
{
|
|
if (!routeDone || (now - _outPlannedAt).TotalSeconds < 1) return new Decision(Act.None);
|
|
// The walk ran out. At its end and still inside counts against OutTriesMax; anywhere else
|
|
// (a fight took the character elsewhere, the walk was cut short) it is just planned again.
|
|
if (me.FlatDistanceTo(_outEnd) <= 2) _outTries++;
|
|
}
|
|
var way = _outTries >= OutTriesMax ? null : WayOut(me);
|
|
if (way == null)
|
|
{
|
|
string wait = _outTries >= OutTriesMax
|
|
? $"inside a building: {OutTriesMax} walks out ended still inside: waiting"
|
|
: "inside a building and no way out known (the hunt never saw it outdoors): waiting";
|
|
if (Note != wait) Note = wait;
|
|
WalkingOut = false;
|
|
return new Decision(Act.Idle);
|
|
}
|
|
WalkingOut = true;
|
|
_outPlannedAt = now;
|
|
_outEnd = way[^1];
|
|
Note = $"inside a building: back out the way in ({way.Count} steps)";
|
|
return new Decision(Act.Walk, way, TargetId, Note);
|
|
}
|
|
|
|
private bool InArea(Vec p) => RadiusYd <= 0 || p.FlatDistanceTo(Anchor) <= RadiusYd;
|
|
|
|
private bool Reaches(PathResult p, Vec goal) =>
|
|
p.Corners.Count > 0 && (p.Status == PathStatus.Ok || (p.Status == PathStatus.Partial && p.Gap <= ReachYd))
|
|
&& p.Length <= RangeYd * MaxWalkFactor && Math.Abs(EndRise(p, goal)) <= ReachRiseYd;
|
|
|
|
/// <summary>How far the monster is above (+) or below (-) the walk's end.</summary>
|
|
private static double EndRise(PathResult p, Vec goal) => p.Corners.Count > 0 ? goal.Z - p.Corners[^1].Z : 0;
|
|
|
|
private string Describe(PathResult p, string why, Vec goal) => p.Status switch
|
|
{
|
|
_ when p.Corners.Count > 0 && Math.Abs(EndRise(p, goal)) > ReachRiseYd
|
|
&& (p.Status == PathStatus.Ok || p.Status == PathStatus.Partial)
|
|
=> EndRise(p, goal) > 0 ? $"{EndRise(p, goal):0} yd above the walk's end (on a ledge, a roof or the floor above)"
|
|
: $"{-EndRise(p, goal):0} yd below the walk's end",
|
|
_ when p.Corners.Count > 0 && p.Length > RangeYd * MaxWalkFactor
|
|
&& (p.Status == PathStatus.Ok || p.Status == PathStatus.Partial) => $"a {p.Length:0} yd walk, too far round",
|
|
_ when why.Length > 0 => why,
|
|
PathStatus.Partial => $"the walk ends {p.Gap:0} yd short",
|
|
PathStatus.NoPath => "no walkable way",
|
|
PathStatus.NoMesh => "not on the map (indoors, or no map there)",
|
|
_ => p.Status.ToString(),
|
|
};
|
|
|
|
/// <summary>A monster in a building is hunted only when that building is known: its rooms, and a door
|
|
/// into it (or the character is in it already).</summary>
|
|
private bool Plannable(Candidate c)
|
|
{
|
|
if (!IsIndoorCell(c.Cell)) return true;
|
|
int b = _buildings?.BuildingOf(c.Cell) ?? 0;
|
|
return b != 0 && (b == _meBuilding || _buildings!.Doors(b).Count > 0);
|
|
}
|
|
|
|
/// <summary>The log line for the choice; <paramref name="pack"/> (Biggest pack, Most XP): ", pack of 5 (4,200,000 XP)" after the name.</summary>
|
|
private string NextNote(Candidate c, HuntWalk w, string pack = "")
|
|
{
|
|
var b = _buildings;
|
|
string len = $"a {w.P.Length:0} yd walk";
|
|
string away = $"({c.Distance:0} yd away)";
|
|
string next = $"next: '{c.Name}'{pack}";
|
|
static string At(Door d) => $"{d.At.X:0},{d.At.Y:0}";
|
|
if (b == null || (w.From == 0 && w.To == 0)) return $"{next}, {len} {away}";
|
|
if (w.Out is Door o && w.In is Door i)
|
|
return $"{next}, inside {b.Name(w.To)} (cell {c.Cell:X8}), {len} out of {b.Name(w.From)} through the door at {At(o)}, "
|
|
+ $"then in through the door at {At(i)} {away}";
|
|
if (w.In is Door din) return $"{next}, inside {b.Name(w.To)} (cell {c.Cell:X8}), {len} through the door at {At(din)} {away}";
|
|
if (w.Out is Door dout) return $"{next}, {len} out of {b.Name(w.From)} through the door at {At(dout)} {away}";
|
|
return $"{next}, inside {b.Name(w.To)} (cell {c.Cell:X8}), {len} through its rooms {away}";
|
|
}
|
|
|
|
// ── Walks into, out of and between buildings ───────────────────────────────────────────────────
|
|
|
|
/// <summary>Most door pairs tried for one walk between two buildings (each a RynthNav path).</summary>
|
|
internal const int MaxDoorPairs = 16;
|
|
/// <summary>A door whose step out is this far above or below RynthNav's ground there doesn't open
|
|
/// onto the ground (a balcony, the top of a wall).</summary>
|
|
public double DoorstepRiseYd { get; set; } = 1.5;
|
|
/// <summary>How long a doorstep's answer is kept.</summary>
|
|
public double DoorstepKeepSec { get; set; } = 60;
|
|
private readonly Dictionary<(uint, long, long), (bool Ok, DateTime Until)> _doorsteps = new();
|
|
private DateTime _now;
|
|
|
|
/// <summary>
|
|
/// The door's step out is on RynthNav's ground: the map's nearest point to it (a path from it to
|
|
/// itself) within ReachYd across and DoorstepRiseYd in height. Asked once per DoorstepKeepSec.
|
|
/// </summary>
|
|
private bool OnGround(Door d)
|
|
{
|
|
var key = (d.Cell, (long)Math.Round(d.Outside.X * 10), (long)Math.Round(d.Outside.Y * 10));
|
|
if (_doorsteps.TryGetValue(key, out var known) && _now < known.Until) return known.Ok;
|
|
var p = _path(d.Outside, d.Outside);
|
|
if (p.Status == PathStatus.Unavailable) return true; // no pathfinder: the walk itself says so
|
|
bool ok = p.Status is PathStatus.Ok or PathStatus.Partial && p.Corners.Count > 0
|
|
&& p.Corners[^1].FlatDistanceTo(d.Outside) <= ReachYd
|
|
&& Math.Abs(p.Corners[^1].Z - d.Outside.Z) <= DoorstepRiseYd;
|
|
if (_doorsteps.Count > 512) _doorsteps.Clear();
|
|
_doorsteps[key] = (ok, _now.AddSeconds(DoorstepKeepSec));
|
|
return ok;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The walk from <paramref name="me"/> (in meCell) to <paramref name="goal"/> (in goalCell): on
|
|
/// RynthNav's map when both are outdoors; through the building's rooms when both are in one; else
|
|
/// out through a door of the building it is in and/or in through a door of the goal's, the door (or
|
|
/// pair) with the shortest walk in all. The corners run through each door's inner point, centre and
|
|
/// outer point (flagged Door). The walk's end (status, gap) is its last part's.
|
|
/// </summary>
|
|
internal HuntWalk PlanWalk(Vec me, uint meCell, Vec goal, uint goalCell)
|
|
{
|
|
var b = _buildings;
|
|
int from = b != null && IsIndoorCell(meCell) ? b.BuildingOf(meCell) : 0;
|
|
int to = b != null && IsIndoorCell(goalCell) ? b.BuildingOf(goalCell) : 0;
|
|
if (from == 0 && to == 0) return new HuntWalk(_path(me, goal));
|
|
string why = "";
|
|
if (from != 0 && from == to)
|
|
{
|
|
var inside = b!.Inside(meCell, me, goalCell, goal);
|
|
if (inside.Status is PathStatus.Ok or PathStatus.Partial && inside.Corners.Count > 0)
|
|
return new HuntWalk(inside, From: from, To: to);
|
|
why = $"no way to it through the rooms of {b.Name(from)}";
|
|
// Its parts don't join inside: out by one door and in by another, as two buildings.
|
|
}
|
|
|
|
// The ways out of the character's building (to each door's outer point) and into the goal's (from each door's outer point).
|
|
var outs = new List<(Door? D, PathResult P, double Cost)>();
|
|
var ins = new List<(Door? D, PathResult P, double Cost)>();
|
|
var none = new PathResult(PathStatus.Ok, Array.Empty<Vec>(), 0, 0);
|
|
if (from == 0) outs.Add((null, none, 0));
|
|
else
|
|
foreach (var d in b!.Doors(from))
|
|
{
|
|
var p = b.Inside(meCell, me, d.Cell, d.Inner);
|
|
if (p.Status != PathStatus.Ok || !OnGround(d)) continue;
|
|
outs.Add((d, p, p.Length + d.Inner.DistanceTo(d.At) + d.At.DistanceTo(d.Outside)));
|
|
}
|
|
if (to == 0) ins.Add((null, none, 0));
|
|
else
|
|
foreach (var d in b!.Doors(to))
|
|
{
|
|
var p = b.Inside(d.Cell, d.Inner, goalCell, goal);
|
|
if (p.Status is not (PathStatus.Ok or PathStatus.Partial) || p.Corners.Count == 0 || !OnGround(d)) continue;
|
|
ins.Add((d, p, d.Outside.DistanceTo(d.At) + d.At.DistanceTo(d.Inner) + p.Length));
|
|
}
|
|
if (outs.Count == 0 || ins.Count == 0)
|
|
{
|
|
if (why.Length == 0)
|
|
why = outs.Count == 0 ? $"no door of {b!.Name(from)} it can walk to" : $"no door of {b!.Name(to)} leads to it";
|
|
return new HuntWalk(new PathResult(PathStatus.NoPath, Array.Empty<Vec>(), 0, 0), From: from, To: to, Why: why);
|
|
}
|
|
|
|
// Door pairs by the least their walk can be (the outdoor part at least the straight line): a
|
|
// pair that can't beat the best so far isn't asked for (each one is a RynthNav path).
|
|
var pairs = new List<(int O, int I, double Bound)>();
|
|
for (int o = 0; o < outs.Count; o++)
|
|
for (int i = 0; i < ins.Count; i++)
|
|
{
|
|
Vec a = outs[o].D?.Outside ?? me, z = ins[i].D?.Outside ?? goal;
|
|
pairs.Add((o, i, outs[o].Cost + a.FlatDistanceTo(z) + ins[i].Cost));
|
|
}
|
|
pairs.Sort((x, y) => x.Bound.CompareTo(y.Bound));
|
|
|
|
HuntWalk? best = null;
|
|
double bestLen = double.MaxValue;
|
|
bool unavailable = false;
|
|
int tried = 0;
|
|
foreach (var (o, i, bound) in pairs)
|
|
{
|
|
if (bound >= bestLen || tried >= MaxDoorPairs) break;
|
|
tried++;
|
|
var (dOut, pOut, cOut) = outs[o];
|
|
var (dIn, pIn, cIn) = ins[i];
|
|
Vec a = dOut?.Outside ?? me, z = dIn?.Outside ?? goal;
|
|
var land = _path(a, z);
|
|
if (land.Status == PathStatus.Unavailable) { unavailable = true; break; }
|
|
if (land.Status is not (PathStatus.Ok or PathStatus.Partial) || land.Corners.Count == 0) continue;
|
|
// Into a door: the way there must end at its doorstep (a door above the ground, on a
|
|
// balcony, leaves it the height of the floor short).
|
|
if (dIn != null && land.Gap > ReachYd) continue;
|
|
double len = cOut + land.Length + cIn;
|
|
if (len >= bestLen) continue;
|
|
var corners = new List<Vec>(pOut.Corners.Count + land.Corners.Count + pIn.Corners.Count + 6);
|
|
if (dOut is Door x) { AddCorners(corners, pOut.Corners); AddCorner(corners, x.At); AddCorner(corners, x.Outside); }
|
|
AddCorners(corners, land.Corners);
|
|
PathResult whole;
|
|
if (dIn is Door y)
|
|
{
|
|
AddCorner(corners, y.Outside); AddCorner(corners, y.At); AddCorner(corners, y.Inner);
|
|
AddCorners(corners, pIn.Corners);
|
|
whole = new PathResult(pIn.Status, corners, len, pIn.Gap);
|
|
}
|
|
else whole = new PathResult(land.Status, corners, len, land.Gap);
|
|
best = new HuntWalk(whole, dOut, dIn, from, to);
|
|
bestLen = len;
|
|
}
|
|
if (best is HuntWalk found) return found;
|
|
if (unavailable) return new HuntWalk(new PathResult(PathStatus.Unavailable, Array.Empty<Vec>(), 0, 0));
|
|
if (why.Length == 0)
|
|
why = to != 0 ? $"no way to a door of {b!.Name(to)}" : $"no way to it from a door of {b!.Name(from)}";
|
|
return new HuntWalk(new PathResult(PathStatus.NoPath, Array.Empty<Vec>(), 0, 0), From: from, To: to, Why: why);
|
|
}
|
|
|
|
/// <summary>Corners closer than this to the one before are one (RynthNav's way to a doorstep ends on it).</summary>
|
|
private const double SameCornerYd = 0.3;
|
|
|
|
private static void AddCorner(List<Vec> corners, Vec v)
|
|
{
|
|
if (corners.Count > 0 && corners[^1].DistanceTo(v) < SameCornerYd)
|
|
{
|
|
if (v.Door && !corners[^1].Door) corners[^1] = corners[^1] with { Door = true };
|
|
return;
|
|
}
|
|
corners.Add(v);
|
|
}
|
|
|
|
private static void AddCorners(List<Vec> corners, IReadOnlyList<Vec> more)
|
|
{
|
|
foreach (var v in more) AddCorner(corners, v);
|
|
}
|
|
}
|