using System; using System.Collections.Generic; using RynthNav.Routing; namespace RynthCore.Plugin.RynthNav; /// One recall the route planner may use: a spell to cast, or a command to type. internal sealed class RecallOption { public string Name = ""; /// The spell to cast (0 = a command). public int SpellId; /// The command to send (@lifestone, @house recall) when SpellId is 0. public string Command = ""; public double Ns, Ew; public double CastSeconds; /// Learned in game (RecallMemory), not fixed by the world data. public bool Learned; /// The planner's recall: its cost the flat way, or (TripTimes) the cast and portal space it takes. public RecallLink ToLink(TripTimes? times = null) => new(Ns, Ew, Name, times == null ? PortalRoute.RecallCostUnits(CastSeconds) : times.RecallUnits(SpellId != 0)); } /// /// Which recalls this character can use, and where they land: the fixed recall spells /// from the Atlas (only the ones in the character's spellbook), plus the personal ones /// RynthNav has learned (RecallMemory). A recall whose landing isn't known, or is off the /// map (a dungeon), is left out with a note saying why. No host calls: the offline tests /// compile this file. /// internal static class RecallPlanner { public const int PrimaryPortalTieRecall = 48; public const int PrimaryPortalTie = 47; public const int LifestoneRecall = 1635; public const int LifestoneTie = 2644; public const int PortalRecall = 2645; public const int SecondaryPortalTie = 2646; public const int SecondaryPortalTieRecall = 2647; /// A recall spell: the magic stance (and a wand) plus the cast. Estimated. public const double SpellCastSeconds = 5.0; /// @lifestone, @house recall and the like: a long recall animation. Estimated. public const double CommandRecallSeconds = 14.0; /// The character's spellbook; null while it hasn't been read. public static List Build(Atlas atlas, ISet? known, RecallMemory memory, string charKey, List? notes) { var list = new List(); // Fixed-destination recall spells (Aerlinthe Recall, Lyceum Recall, ...). if (known == null) { notes?.Add("Spellbook not read yet: recall spells left out."); } else { var seen = new HashSet(); foreach (AtlasRecall r in atlas.Recalls) { if (!known.Contains(r.SpellId) || !seen.Add(r.SpellId)) continue; if (!r.OnMap) { notes?.Add(r.Name + " lands " + (r.In.Length > 0 ? "in " + r.In : "in a dungeon") + ": not used for routes."); continue; } list.Add(new RecallOption { Name = r.Name, SpellId = r.SpellId, Ns = r.Ns, Ew = r.Ew, CastSeconds = SpellCastSeconds }); } } // Personal recall spells: the landing is the character's own. if (known != null) { AddLearnedSpell(list, notes, known, memory, charKey, LifestoneRecall, "Lifestone Recall", RecallSlot.Lifestone, "cast Lifestone Tie on a lifestone, cast Lifestone Recall once, or /rnav tie lifestone at the tied lifestone"); AddLearnedSpell(list, notes, known, memory, charKey, PortalRecall, "Portal Recall", RecallSlot.LastPortal, "walk through a portal"); AddLearnedSpell(list, notes, known, memory, charKey, PrimaryPortalTieRecall, "Primary Portal Tie Recall", RecallSlot.Tie1, "cast Primary Portal Tie on a portal, cast Primary Portal Tie Recall once, or /rnav tie 1 at the tied portal"); AddLearnedSpell(list, notes, known, memory, charKey, SecondaryPortalTieRecall, "Secondary Portal Tie Recall", RecallSlot.Tie2, "cast Secondary Portal Tie on a portal, cast Secondary Portal Tie Recall once, or /rnav tie 2 at the tied portal"); } // Command recalls (no spell): @lifestone always; house, mansion and hometown // once the character has used them (only then is it known that they have one). AddLearnedCommand(list, notes, memory, charKey, RecallSlot.Sanctuary, "Lifestone (@lifestone)", alwaysAvailable: true, "use a lifestone"); AddLearnedCommand(list, notes, memory, charKey, RecallSlot.House, "House recall", alwaysAvailable: false, ""); AddLearnedCommand(list, notes, memory, charKey, RecallSlot.Mansion, "Allegiance mansion recall", alwaysAvailable: false, ""); AddLearnedCommand(list, notes, memory, charKey, RecallSlot.Hometown, "Allegiance hometown recall", alwaysAvailable: false, ""); return list; } private static void AddLearnedSpell(List list, List? notes, ISet known, RecallMemory memory, string charKey, int spellId, string name, RecallSlot slot, string howToLearn) { if (!known.Contains(spellId)) return; RecallSpot? spot = memory.Get(charKey, slot); if (spot == null) { notes?.Add(name + ": where it lands isn't known yet (" + howToLearn + ")."); return; } if (!spot.OnMap) { notes?.Add(name + " lands " + Where(spot) + ": not used for routes."); return; } if (spot.Unconfirmed) { notes?.Add(name + ": the last portal you used (" + (spot.Label.Length > 0 ? spot.Label : "unknown") + ") isn't in RynthNav's portal list, " + "so whether the server lets Portal Recall go back there isn't known: not used for routes until you cast " + name + " once."); return; } list.Add(new RecallOption { Name = name, SpellId = spellId, Ns = spot.Ns, Ew = spot.Ew, CastSeconds = SpellCastSeconds, Learned = true, }); } /// What a portal walk does to Portal Recall's landing (RynthNavPlugin.Travel). internal enum LastPortalUpdate { Set, SetUnconfirmed, Keep } /// ACE's PortalBitmask.NoRecall: the server doesn't keep such a portal as your last portal. internal const int PortalNoRecall = 0x20; /// /// A teleport through a portal with this PortalBitmask (null: not in the portal list). ACE keeps /// a portal as your last portal only if it isn't NoRecall (Portal.ActOnUse), so Portal Recall then /// still goes to the one before (R-20261126-024033-5d60, Conquest: from the Dark Isle, which can't /// be recalled to, a goto cast Portal Recall and landed in a dungeon visited earlier). /// internal static LastPortalUpdate ForPortal(int? bitmask) => bitmask is not int b ? LastPortalUpdate.SetUnconfirmed : (b & PortalNoRecall) != 0 ? LastPortalUpdate.Keep : LastPortalUpdate.Set; private static void AddLearnedCommand(List list, List? notes, RecallMemory memory, string charKey, RecallSlot slot, string name, bool alwaysAvailable, string howToLearn) { RecallSpot? spot = memory.Get(charKey, slot); if (spot == null) { if (alwaysAvailable) notes?.Add(name + ": where it lands isn't known yet (" + howToLearn + ")."); return; } if (!spot.OnMap) { notes?.Add(name + " lands " + Where(spot) + ": not used for routes."); return; } list.Add(new RecallOption { Name = name, Command = spot.Command.Length > 0 ? spot.Command : RecallMemory.DefaultCommand(slot), Ns = spot.Ns, Ew = spot.Ew, CastSeconds = CommandRecallSeconds, Learned = true, }); } private static string Where(RecallSpot s) => s.Label.Length > 0 ? "in " + s.Label : "in a dungeon"; // ── Learning from a recall cast by hand ────────────────────────────────── /// Recall the Sanctuary (Lifestone Sending): lands where @lifestone does. public const int RecallTheSanctuary = 2023; /// WeenieError YouMustLinkToLifestoneToRecall / YouMustLinkToPortalToRecall. public const uint ErrorMustLinkLifestone = 0x049E, ErrorMustLinkPortal = 0x04A3; /// What a teleport right after your own spell words teaches. public enum HandCast { /// Nothing new (already known, a fixed recall, or can't tell); why says which. Nothing, /// The landing of slot. Learn, /// A portal tie recall, but not which: ask (/rnav tie 1|2). AskTie, } /// /// You spoke spell words, stood still and were teleported: a recall cast by hand. Which /// one, from where it landed: on a lifestone, Lifestone Recall; anywhere else, a portal tie /// recall (Portal Recall's landing is the last portal's, already known). A landing that /// matches a recall already known teaches nothing. When the spellbook leaves more than one /// tie recall (or Portal Recall with no last portal known), ask. No host calls. /// /// The landing is on a lifestone (the Atlas's). /// Two landings this close are the same place. public static HandCast GuessHandCast(RecallSpot landing, bool atLifestone, ISet? known, RecallMemory memory, string charKey, double nearUnits, out RecallSlot slot, out string why) { slot = RecallSlot.Lifestone; if (known == null) { why = "the spellbook isn't read yet"; return HandCast.Nothing; } if (atLifestone) { if (!known.Contains(LifestoneRecall)) { why = "on a lifestone, but Lifestone Recall isn't in the spellbook"; return HandCast.Nothing; } if (Same(memory.Get(charKey, RecallSlot.Lifestone), landing, nearUnits)) { why = "Lifestone Recall, already known"; return HandCast.Nothing; } if (known.Contains(RecallTheSanctuary) && Same(memory.Get(charKey, RecallSlot.Sanctuary), landing, nearUnits)) { why = "on your @lifestone lifestone: Lifestone Recall or Recall the Sanctuary, can't tell"; return HandCast.Nothing; } why = "on a lifestone: Lifestone Recall"; return HandCast.Learn; } if (Same(memory.Get(charKey, RecallSlot.LastPortal), landing, nearUnits)) { why = "where the last portal went: Portal Recall"; return HandCast.Nothing; } var ties = new List(2); if (known.Contains(PrimaryPortalTieRecall)) ties.Add(RecallSlot.Tie1); if (known.Contains(SecondaryPortalTieRecall)) ties.Add(RecallSlot.Tie2); foreach (RecallSlot t in ties) if (Same(memory.Get(charKey, t), landing, nearUnits)) { slot = t; why = RecallMemory.SlotName(t) + ", already known"; return HandCast.Nothing; } if (ties.Count == 0) { why = "no portal tie recall in the spellbook (a spell's teleport RynthNav doesn't know)"; return HandCast.Nothing; } bool maybePortalRecall = known.Contains(PortalRecall) && memory.Get(charKey, RecallSlot.LastPortal) == null; if (ties.Count == 1 && !maybePortalRecall) { slot = ties[0]; why = "the only portal tie recall in the spellbook"; return HandCast.Learn; } slot = ties[0]; why = maybePortalRecall ? "a portal tie recall or Portal Recall (no last portal known yet)" : "a portal tie recall, primary or secondary"; return HandCast.AskTie; } /// Two landings are the same place: on the map within ; off it, the same label. public static bool Same(RecallSpot? a, RecallSpot b, double nearUnits) { if (a == null || a.OnMap != b.OnMap) return false; if (a.OnMap) return NavCoords.Distance(a.Ns, a.Ew, b.Ns, b.Ew) < nearUnits; return a.Label.Equals(b.Label, StringComparison.OrdinalIgnoreCase); } /// /// The recall the server says isn't linked: "You must link to a lifestone to recall it" is /// always Lifestone Recall; "...to a portal..." is whichever portal recall was just cast /// (, 0 = not known: nothing). /// public static RecallSlot? SlotUnlinked(uint error, int castSpell) => error switch { ErrorMustLinkLifestone => RecallSlot.Lifestone, ErrorMustLinkPortal => castSpell switch { PrimaryPortalTieRecall => RecallSlot.Tie1, SecondaryPortalTieRecall => RecallSlot.Tie2, PortalRecall => RecallSlot.LastPortal, _ => null, }, _ => null, }; /// The recall spell whose landing a slot holds (0 for the command recalls). public static int SpellFor(RecallSlot slot) => slot switch { RecallSlot.Lifestone => LifestoneRecall, RecallSlot.LastPortal => PortalRecall, RecallSlot.Tie1 => PrimaryPortalTieRecall, RecallSlot.Tie2 => SecondaryPortalTieRecall, _ => 0, }; /// A slot's name for players ("Primary Portal Tie Recall", "@lifestone"). public static string Title(RecallSlot slot) => slot switch { RecallSlot.Sanctuary => "@lifestone", RecallSlot.Lifestone => "Lifestone Recall", RecallSlot.LastPortal => "Portal Recall", RecallSlot.Tie1 => "Primary Portal Tie Recall", RecallSlot.Tie2 => "Secondary Portal Tie Recall", RecallSlot.House => "House recall", RecallSlot.Mansion => "Allegiance mansion recall", _ => "Allegiance hometown recall", }; public static List Links(List options, TripTimes? times = null) { var links = new List(options.Count); foreach (var o in options) links.Add(o.ToLink(times)); return links; } }