RynthSuite/Tools/RynthNav.RouteTests/CustomPortalTests.cs

128 lines
9 KiB
C#

using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthNav;
using RynthNav.Routing;
namespace RynthNav.RouteTests;
// A custom server's own portals (CustomPortals.cs, 2026-11-27): written down by a scan, learned by taking them, per
// server, saved, and planned with on our own graph (PortalRoute) next to the world data's portals.
// Spotted by SilentKelpie (Forgejo rynth PR #12); re-implemented here.
internal static partial class Program
{
private static void RunCustomPortals()
{
Run(nameof(CustomScanAndLearn), CustomScanAndLearn);
Run(nameof(CustomKnownPortalNotRelearned), CustomKnownPortalNotRelearned);
Run(nameof(CustomPerServerForgetClear), CustomPerServerForgetClear);
Run(nameof(CustomSaveAndMerge), CustomSaveAndMerge);
Run(nameof(CustomPortalsRoute), CustomPortalsRoute);
}
private static readonly List<PortalLink> BuiltIn = new() { new(10.0, 10.0, -30.0, -30.0, "Portal to the World Data") };
private static readonly DateTime T0 = new(2026, 11, 27, 12, 0, 0, DateTimeKind.Utc);
private static void CustomScanAndLearn()
{
var s = new CustomPortalStore();
int v0 = s.Version;
Check(s.Saw("Test Server", "Portal to the Spire", 9001, 0xA9B40021, 12.5, 30.25, 40.0, 20.0, 30.0, BuiltIn, T0) == CustomSeen.Added, "a scan adds a new one");
Check(s.Version > v0, "and it's a change to save");
Check(s.Saw("Test Server", "Portal to the Spire", 9001, 0xA9B40021, 12.5, 30.25, 40.0, 20.0, 30.0, BuiltIn, T0) == CustomSeen.Refreshed, "seen again: refreshed");
Check(s.Saw("Test Server", "Portal to the World Data", 0, 0xA9B40022, 1, 1, 0, 10.02, 10.01, BuiltIn, T0) == CustomSeen.BuiltIn, "the world data has it: left alone");
Check(s.Saw("Test Server", "Some Other Portal", 0, 0xA9B40022, 1, 1, 0, 10.01, 10.0, BuiltIn, T0) == CustomSeen.BuiltIn, "any portal right on a world-data one: left alone");
var (saved, routable) = s.Counts("Test Server");
Check(saved == 1 && routable == 0, $"one saved, not routable until taken ({saved}, {routable})");
Check(s.Links("Test Server").Count == 0, "no link yet");
CustomLearn r = s.Learned("Test Server", "Portal to the Spire", 9001, 0xA9B40021, 12.5, 30.25, 40.0, 20.0, 30.0,
true, -5.5, 60.25, 0, BuiltIn, T0.AddMinutes(1), out CustomPortal? p);
Check(r == CustomLearn.New && p != null && p.Routable, "taking it learns the landing");
List<PortalLink> links = s.Links("Test Server");
Check(links.Count == 1 && links[0].Name == "Portal to the Spire" && links[0].DstNs == -5.5 && links[0].DstEw == 60.25, "a route link");
Check(links[0].HasSpot && links[0].Cell == 0xA9B40021 && links[0].Z == 40.0, "the link walks to where the object stood");
Check(!links[0].HasRules, "open to anyone (a refusal is learned as for any portal)");
// A portal never scanned: added when taken.
r = s.Learned("Test Server", "Hidden Portal", 0, 0xA9B40023, 2, 2, 0, 21.0, 31.0, true, 50, 50, 0, BuiltIn, T0, out p);
Check(r == CustomLearn.New && s.Counts("Test Server").Saved == 2, "an unscanned one is added when taken");
// Into a dungeon: kept, listed, not routed.
r = s.Learned("Test Server", "Pit Portal", 0, 0xA9B40024, 3, 3, 0, 22.0, 32.0, false, 0, 0, 0x01230105, BuiltIn, T0, out p);
Check(r == CustomLearn.New && p != null && !p.Routable && p.Where.Contains("0123"), "a dungeon landing: kept, not routed");
Check(s.Links("Test Server").Count == 2, "two map-to-map links");
// A world-data portal taken: nothing learned.
r = s.Learned("Test Server", "Portal to the World Data", 0, 0xA9B40022, 1, 1, 0, 10.0, 10.0, true, -30, -30, 0, BuiltIn, T0, out p);
Check(r == CustomLearn.BuiltIn && p == null, "a world-data portal isn't learned");
// Landing right beside it: not a teleport through it.
r = s.Learned("Test Server", "Fountain Portal", 0, 0xA9B40025, 1, 1, 0, 23.0, 33.0, true, 23.05, 33.05, 0, BuiltIn, T0, out p);
Check(r == CustomLearn.NotAPortal && s.Counts("Test Server").Saved == 3, "a landing beside it isn't learned");
}
private static void CustomKnownPortalNotRelearned()
{
var s = new CustomPortalStore();
s.Learned("S", "Portal A", 0, 0xA9B40021, 1, 1, 0, 20.0, 30.0, true, -5.5, 60.25, 0, null, T0, out _);
int v = s.Version;
CustomLearn r = s.Learned("S", "Portal A", 0, 0xA9B40021, 1, 1, 0, 20.02, 30.01, true, -5.52, 60.3, 0, null, T0.AddHours(1), out _);
Check(r == CustomLearn.AlreadyKnown && s.Version == v, "taken again, same landing: already known, nothing to write");
r = s.Learned("S", "Portal A", 0, 0xA9B40021, 1, 1, 0, 20.0, 30.0, true, 40.0, -40.0, 0, null, T0.AddHours(2), out CustomPortal? p);
Check(r == CustomLearn.LandingChanged && p!.DestNs == 40.0 && s.Version > v, "a new landing: updated");
}
private static void CustomPerServerForgetClear()
{
var s = new CustomPortalStore();
s.Learned("One", "Gate", 0, 0xA9B40021, 1, 1, 0, 20.0, 30.0, true, -5, 60, 0, null, T0, out _);
s.Learned("One", "Gate of Ash", 0, 0xA9B40022, 1, 1, 0, 25.0, 35.0, true, -15, 70, 0, null, T0, out _);
s.Learned("Two", "Gate", 0, 0xA9B40021, 1, 1, 0, 20.0, 30.0, true, 45, -60, 0, null, T0, out _);
Check(s.Links("One").Count == 2 && s.Links("two").Count == 1, "each server has its own (the name in any case)");
Check(s.Links("Three").Count == 0, "another server has none");
Check(s.Forget("One", "Gate") == 1 && s.Links("One").Count == 1, "forget: the exact name only, when there is one");
Check(s.Forget("One", "ash") == 1 && s.Links("One").Count == 0, "forget: else the names containing it");
Check(s.Forget("One", "Nothing") == 0, "forget: none");
Check(s.Clear("Two") == 1 && s.All.Count == 0, "clear: that server's all");
}
private static void CustomSaveAndMerge()
{
var s = new CustomPortalStore();
s.Saw("Test Server", "Scanned Only", 77, 0xA9B40021, 10.5, 20.25, 30.0, 20.0, 30.0, null, T0);
s.Learned("Test Server", "To Town", 78, 0xA9B40022, 1, 2, 3, 21.0, 31.0, true, -5.5, 60.25, 0, null, T0, out _);
s.Learned("Other", "Down", 0, 0xA9B40023, 1, 2, 3, 22.0, 32.0, false, 0, 0, 0x01230105, null, T0, out _);
string text = s.Serialize();
CustomPortalStore back = CustomPortalStore.Parse(text);
Check(back.All.Count == 3, $"three read back ({back.All.Count})");
CustomPortal? a = back.Find("Test Server", "Scanned Only", 20.0, 30.0), b = back.Find("Test Server", "To Town", 21.0, 31.0), c = back.Find("Other", "Down", 22.0, 32.0);
Check(a != null && !a.DestKnown && a.Wcid == 77 && a.Cell == 0xA9B40021 && a.X == 10.5 && a.Y == 20.25 && a.Z == 30.0, "a scanned one, with its spot");
Check(b != null && b.Routable && b.DestNs == -5.5 && b.DestEw == 60.25 && b.LearnedUtc == T0, "a learned one, with its landing");
Check(c != null && c.DestKnown && !c.DestOnMap && c.DestCell == 0x01230105, "a dungeon landing");
Check(CustomPortalStore.Parse("# notes\n\nbad line\nS\tN\tx\n").All.Count == 0, "notes and bad lines are skipped");
Check(CustomPortalStore.Clean("A\tB\nC ") == "A B C", "names are one field");
// Another client's file: its new portal and later landing are taken in, a portal this one forgot isn't.
var mine = CustomPortalStore.Parse(text);
mine.Forget("Test Server", "Scanned Only");
var disk = CustomPortalStore.Parse(text);
disk.Learned("Test Server", "To Town", 78, 0xA9B40022, 1, 2, 3, 21.0, 31.0, true, 44.0, 44.0, 0, null, T0.AddDays(1), out _);
disk.Learned("Test Server", "New Elsewhere", 0, 0xA9B40024, 1, 2, 3, 26.0, 36.0, true, -40, 40, 0, null, T0, out _);
mine.MergeFrom(disk);
Check(mine.Find("Test Server", "Scanned Only", 20.0, 30.0) == null, "what this client forgot stays forgotten");
Check(mine.Find("Test Server", "New Elsewhere", 26.0, 36.0) != null, "another client's new portal is taken in");
Check(mine.Find("Test Server", "To Town", 21.0, 31.0)!.DestNs == 44.0, "a landing learned later wins");
}
private static void CustomPortalsRoute()
{
// The world data has no way across; the server's own portal does: the plan takes it (our graph, PortalRoute).
var s = new CustomPortalStore();
s.Learned("S", "Server Portal", 0, 0xA9B40021, 1, 1, 0, 0.5, 0.5, true, 39.5, 39.5, 0, BuiltIn, T0, out _);
var portals = new List<PortalLink>(BuiltIn);
portals.AddRange(s.Links("S"));
var steps = PortalRoute.Plan(portals, null, 0, 0, 40, 40, out _, out int used);
Check(used == 1 && steps.Count == 2 && steps[0].UsePortal && steps[0].Label == "Server Portal", "the custom portal is taken");
Near(steps[0].LandNs, 39.5, 1e-9, "it lands where it was learned to");
// Without it (custom off, or another server): walk.
steps = PortalRoute.Plan(BuiltIn, null, 0, 0, 40, 40, out _, out used);
Check(used == 0, "without it: no portal");
}
}