diff --git a/CHANGELOG.md b/CHANGELOG.md index daa2e0d..6148e04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ All notable changes to RandomNameGeneratorLibrary. The format follows [Keep a Ch ### Added +- `FantasyNameGenerator` (with `IFantasyNameGenerator` and the `Landform` enum): names for fantasy settings from small word grammars, seeded like the other generators (`new FantasyNameGenerator(42)`). Place names (adjective + structure, sometimes "of the" + a two-word feature: "Broken Tower of the Ash Hollow"), place names that fit a landform (hill, river, forest, marsh, coast, mountain, plain), faction names ("the Grey Heron Clan"), war-band compounds ("Splitskull"), trade surnames with older spellings ("Smith", "Smyth", "Tailor", via the public `ApplySpellingVariant`), a census first name with a byname ("Anna the Bold") and keeper epithets ("Bram the Honest"). The word lists were written for this library. The ideas behind the grammars were inspired by Matthew J. Finch's *Tome of Adventure Design*, *Points of Light*, Gygax's *Living Fantasy* and *World Builder*, and the *Judges Guild Guidebook to Shops*; nothing is copied from them. Existing generators and their seeded output are unchanged. + - A Unity package, `com.m4bwav.random-name-generator` (OpenUPM), in `Packages/com.m4bwav.random-name-generator`. It carries the published 2.3.0 `netstandard2.0` assembly byte for byte, so the NuGet package is unchanged. `tools/UnityPackage/sync.py` copies each new release's assembly from nuget.org; OpenUPM builds from `upm/` tags. Web builds need Player Settings > Use Embedded Resources; an editor script in the package logs an error when a Web build has it off. ## [2.3.0] - 2026-09-28 diff --git a/RandomNameGeneratorLibrary/FantasyNameGenerator.cs b/RandomNameGeneratorLibrary/FantasyNameGenerator.cs new file mode 100644 index 0000000..b31d7a1 --- /dev/null +++ b/RandomNameGeneratorLibrary/FantasyNameGenerator.cs @@ -0,0 +1,260 @@ +using System; +using System.Collections.Generic; + +namespace RandomNameGeneratorLibrary +{ + /// + /// Generates names for fantasy settings from small word grammars: places, landform places, factions, + /// war-bands, trade surnames, bynames and keeper epithets. The word lists are written for this library and + /// built into the code, so nothing is read from disk. + /// + /// + /// A generator created with the default constructor is safe to share between threads. A generator created + /// around a caller-supplied is only as thread-safe as that instance, which for + /// means not at all: use one generator per thread. The same seed gives the same names + /// within a version of the library; a later version that adds words may give different ones. + /// + public class FantasyNameGenerator : BaseNameGenerator, IFantasyNameGenerator + { + private static readonly string[] PlaceAdjectives = + { + "Broken", "Sunken", "Hollow", "Silent", "Burning", "Forgotten", "Crooked", "Gilded", "Shattered", "Weeping", + "Iron", "Pale", "Crimson", "Drowned", "Lonely", "Thorned", "Ashen", "Golden", "Black", "White", + "Old", "High", "Cold", "Bitter", "Starlit", "Rusted", "Sleeping", "Howling", "Veiled", "Last", + }; + + private static readonly string[] Structures = + { + "Tower", "Keep", "Abbey", "Hall", "Gate", "Bridge", "Temple", "Spire", "Citadel", "Mill", + "Chapel", "Vault", "Crypt", "Fort", "Lighthouse", "Barrow", "Manor", "Shrine", "Library", "Well", + "Market", "Watchpost", "Inn", "Forge", "Garden", "Throne", "Labyrinth", "Arch", "Halls", "Stair", + }; + + private static readonly string[] FeatureFirst = + { + "Ash", "Raven", "Salt", "Moon", "Thorn", "Frost", "Ember", "Bone", "Iron", "Mist", + "Stone", "Wolf", "Gloam", "Copper", "Silver", "Storm", "Willow", "Hawk", "Cinder", "Amber", + }; + + private static readonly string[] FeatureSecond = + { + "Hollow", "Vale", "Fen", "Crag", "Hills", "Waste", "Reach", "Wood", "Shore", "Deep", + "Moor", "Peaks", "Mere", "Glen", "Fields", "Falls", "March", "Downs", "Sands", "Isles", + }; + + private static readonly string[] HillPrefixes = { "Windy", "Green", "Bald", "Lark", "Bramble", "Stony", "Sheep", "Beacon", "Gorse", "Hare" }; + private static readonly string[] HillWords = { "Hill", "Tor", "Knoll", "Down", "Rise", "Ridge", "Barrow", "Brow", "Mound", "Heights" }; + private static readonly string[] RiverPrefixes = { "Swift", "Otter", "Silver", "Reed", "Mill", "Cold", "Alder", "Trout", "Willow", "Muddy" }; + private static readonly string[] RiverWords = { "Ford", "Bend", "Water", "Brook", "Run", "Rapids", "Crossing", "Falls", "Weir", "Banks" }; + private static readonly string[] ForestPrefixes = { "Oak", "Elder", "Shadow", "Bracken", "Moss", "Owl", "Hazel", "Deep", "Pine", "Antler" }; + private static readonly string[] ForestWords = { "Wood", "Grove", "Weald", "Thicket", "Holt", "Glade", "Copse", "Shaw", "Greenwood", "Tangle" }; + private static readonly string[] MarshPrefixes = { "Bog", "Black", "Rush", "Murk", "Heron", "Sedge", "Eel", "Toad", "Grey", "Sour" }; + private static readonly string[] MarshWords = { "Fen", "Mire", "Bog", "Marsh", "Slough", "Carr", "Moss", "Wallow", "Sump", "Flats" }; + private static readonly string[] CoastPrefixes = { "Saltmere", "Gull", "Wrack", "Gale", "Seal", "Driftwood", "Shell", "Tern", "Spray", "Pearl" }; + private static readonly string[] CoastWords = { "Strand", "Cove", "Bay", "Point", "Head", "Haven", "Sound", "Shore", "Cliffs", "Harbor" }; + private static readonly string[] MountainPrefixes = { "Eagle", "Frost", "Thunder", "Grim", "Snow", "Cloud", "Giant", "Iron", "Wind", "Goat" }; + private static readonly string[] MountainWords = { "Peak", "Crag", "Pass", "Spire", "Horn", "Summit", "Scarp", "Fang", "Tooth", "Col" }; + private static readonly string[] PlainPrefixes = { "Long", "Barley", "Gold", "Wide", "Thistle", "Wild", "Lark", "Dry", "Bison", "Sun" }; + private static readonly string[] PlainWords = { "Field", "Lea", "Moor", "Heath", "Steppe", "Meadow", "Plain", "Prairie", "Flat", "Acre" }; + + private static readonly string[] TotemColors = { "Grey", "Red", "White", "Black", "Golden", "Silver", "Spotted", "Blue", "Pale", "Dun" }; + + private static readonly string[] TotemAnimals = + { + "Heron", "Wolf", "Bear", "Elk", "Otter", "Raven", "Boar", "Hawk", "Badger", "Stag", + "Lynx", "Salmon", "Owl", "Fox", "Bull", "Crane", "Viper", "Hare", "Ram", "Toad", + }; + + private static readonly string[] GroupNouns = + { + "Clan", "Tribe", "Band", "Kin", "Lodge", "Brotherhood", "Sisterhood", "Host", "Circle", "Company", + "Fellowship", "Pack", "Council", "Hearth", "Sept", + }; + + private static readonly string[] WarVerbs = + { + "Split", "Break", "Crack", "Rend", "Gnaw", "Smash", "Bite", "Cleave", "Gouge", "Rip", + "Crush", "Snap", "Bleed", "Burn", "Tear", "Grind", "Hack", "Pierce", "Gut", "Blind", + }; + + private static readonly string[] BodyParts = + { + "skull", "fang", "bone", "eye", "jaw", "hand", "spine", "throat", "heart", "tooth", + "claw", "neck", "rib", "back", "knee", "horn", "tongue", "hide", "fist", "brow", + }; + + private static readonly string[] Occupations = + { + "Smith", "Taylor", "Baker", "Miller", "Cooper", "Fletcher", "Thatcher", "Mason", "Carter", "Turner", + "Weaver", "Fisher", "Tanner", "Potter", "Brewer", "Butcher", "Chandler", "Sawyer", "Wright", "Glover", + "Dyer", "Fuller", "Archer", "Bowyer", "Shepherd", "Hunter", "Porter", "Webster", "Barber", "Draper", + "Mercer", "Slater", "Thresher", "Wainwright", "Carpenter", "Painter", "Spinner", "Skinner", "Cook", "Clark", + }; + + // Older or regional spellings, tried in order; the first whose pattern occurs after the first letter is + // applied to its last occurrence. + private static readonly string[][] SpellingRules = + { + new[] { "ith", "yth" }, // Smith to Smyth + new[] { "ayl", "ail" }, // Taylor to Tailor + new[] { "ark", "arke" }, // Clark to Clarke + new[] { "ook", "ooke" }, // Cook to Cooke + new[] { "ll", "l" }, // Miller to Miler + new[] { "er", "ar" }, // Turner to Turnar + }; + + private static readonly string[] Bynames = + { + "Bold", "Fair", "Tall", "Short", "Red", "Grim", "Wise", "Young", "Elder", "Quiet", + "Loud", "Swift", "Stout", "Lame", "Lucky", "Proud", "Gentle", "Fierce", "Black", "Fat", + "Thin", "Merry", "Stern", "Clever", "Unready", + }; + + private static readonly string[] KeeperTraits = + { + "Honest", "Kind", "Watchful", "Patient", "Thrifty", "Generous", "Sober", "Cheerful", "Shrewd", "Careful", + "Faithful", "Sharp-Eyed", "Tight-Fisted", "Soft-Spoken", "Steady", + }; + + private static readonly string[] KeeperDeeds = + { + "Lantern-Bearer", "Gate-Keeper", "Ale-Pourer", "Coin-Counter", "Bridge-Mender", "Well-Digger", "Wolf-Slayer", + "Debt-Forgiver", "Map-Maker", "Bell-Ringer", "Road-Warden", "Bread-Giver", "Oath-Keeper", "Fire-Tender", "Stranger-Friend", + }; + + private readonly PersonNameGenerator _personNames; + + /// Creates a generator with a fresh, well-seeded random number generator. + public FantasyNameGenerator() + { + _personNames = new PersonNameGenerator(RandGen); + } + + /// Creates a generator that draws from . + /// The random number generator to draw from. A seeded instance gives reproducible names. + /// is null. + public FantasyNameGenerator(Random randGen) : base(randGen) + { + _personNames = new PersonNameGenerator(RandGen); + } + + /// Creates a generator that produces the same sequence of names for the same . + /// The seed for the underlying . + public FantasyNameGenerator(int seed) : this(new Random(seed)) + { + } + + /// The occupations draws from, in their common spelling. + public static IReadOnlyList TradeSurnames => Occupations; + + /// + public string GenerateRandomFantasyPlaceName() + { + var name = Pick(PlaceAdjectives) + " " + Pick(Structures); + + // About half the names get a feature: "of the Ash Hollow". + if (RandGen.Next(0, 2) == 0) + name += " of the " + Pick(FeatureFirst) + " " + Pick(FeatureSecond); + + return name; + } + + /// + public string GenerateRandomLandformPlaceName(Landform landform) + { + string[] prefixes; + string[] words; + + switch (landform) + { + case Landform.Hill: prefixes = HillPrefixes; words = HillWords; break; + case Landform.River: prefixes = RiverPrefixes; words = RiverWords; break; + case Landform.Forest: prefixes = ForestPrefixes; words = ForestWords; break; + case Landform.Marsh: prefixes = MarshPrefixes; words = MarshWords; break; + case Landform.Coast: prefixes = CoastPrefixes; words = CoastWords; break; + case Landform.Mountain: prefixes = MountainPrefixes; words = MountainWords; break; + case Landform.Plain: prefixes = PlainPrefixes; words = PlainWords; break; + default: throw new ArgumentOutOfRangeException(nameof(landform), landform, "Unknown landform."); + } + + return Pick(prefixes) + " " + Pick(words); + } + + /// + public string GenerateRandomFactionName() + { + return "the " + Pick(TotemColors) + " " + Pick(TotemAnimals) + " " + Pick(GroupNouns); + } + + /// + public string GenerateRandomWarBandName() + { + return Pick(WarVerbs) + Pick(BodyParts); + } + + /// + public string GenerateRandomTradeSurname() + { + var surname = Pick(Occupations); + + // One name in three takes an older spelling when a rule applies to it. + return RandGen.Next(0, 3) == 0 ? ApplySpellingVariant(surname) : surname; + } + + /// + public string GenerateRandomBynameName() + { + return _personNames.GenerateRandomFirstName() + " the " + Pick(Bynames); + } + + /// + public string GenerateRandomKeeperEpithet(string name) + { + if (name == null || name.Trim().Length == 0) + throw new ArgumentException("The name cannot be null or white space.", nameof(name)); + + var epithet = RandGen.Next(0, 2) == 0 ? Pick(KeeperTraits) : Pick(KeeperDeeds); + + return name.Trim() + " the " + epithet; + } + + /// + public IEnumerable GenerateMultipleFantasyPlaceNames(int numberOfNames) + { + if (numberOfNames < 0) throw new ArgumentOutOfRangeException(nameof(numberOfNames), numberOfNames, "The number of names to generate cannot be negative."); + + var list = new List(numberOfNames); + + for (var index = 0; index < numberOfNames; ++index) + list.Add(GenerateRandomFantasyPlaceName()); + + return list; + } + + /// + /// Rewrites with the first spelling rule that applies to it, for example + /// Smith to Smyth or Taylor to Tailor; a name no rule applies to comes back unchanged. + /// + /// The surname to respell. + /// The respelled surname. + /// is null. + public static string ApplySpellingVariant(string surname) + { + if (surname == null) throw new ArgumentNullException(nameof(surname)); + + foreach (var rule in SpellingRules) + { + var at = surname.LastIndexOf(rule[0], StringComparison.Ordinal); + if (at > 0) + return surname.Substring(0, at) + rule[1] + surname.Substring(at + rule[0].Length); + } + + return surname; + } + + private string Pick(string[] words) + { + return words[RandGen.Next(0, words.Length)]; + } + } +} diff --git a/RandomNameGeneratorLibrary/IFantasyNameGenerator.cs b/RandomNameGeneratorLibrary/IFantasyNameGenerator.cs new file mode 100644 index 0000000..8530798 --- /dev/null +++ b/RandomNameGeneratorLibrary/IFantasyNameGenerator.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; + +namespace RandomNameGeneratorLibrary +{ + /// + /// Generates names for fantasy settings from word grammars: places, factions, war-bands, trade surnames, + /// bynames and epithets. Implemented by ; register it with a dependency + /// injection container or mock it in tests. + /// + public interface IFantasyNameGenerator + { + /// + /// Generates a place name: an adjective and a structure, sometimes followed by "of the" and a two-word + /// feature, for example Broken Tower of the Ash Hollow or Sunken Abbey. + /// + string GenerateRandomFantasyPlaceName(); + + /// + /// Generates a place name that fits , for example Windy Tor for + /// or Saltmere Strand for . + /// + /// The kind of land the place stands on. + /// is not a defined value. + string GenerateRandomLandformPlaceName(Landform landform); + + /// + /// Generates a faction or sub-tribe name: a totem animal and a group noun, for example + /// the Grey Heron Clan. + /// + string GenerateRandomFactionName(); + + /// + /// Generates a war-band name compounded from a verb and a body part, for example Splitskull or + /// Breakfang. + /// + string GenerateRandomWarBandName(); + + /// + /// Generates a surname from an occupation, sometimes in an older spelling, for example Smith, + /// Smyth, Taylor or Tailor. + /// + string GenerateRandomTradeSurname(); + + /// + /// Generates a census first name followed by a byname, for example Anna the Bold. + /// + string GenerateRandomBynameName(); + + /// + /// Generates a keeper's epithet for : the name, "the" and a trait or deed, for + /// example Bram the Honest or Bram the Lantern-Bearer. + /// + /// The name to give an epithet. + /// is null or white space. + string GenerateRandomKeeperEpithet(string name); + + /// Generates place names with . + /// How many names to generate; zero gives an empty sequence. + /// is negative. + IEnumerable GenerateMultipleFantasyPlaceNames(int numberOfNames); + } +} diff --git a/RandomNameGeneratorLibrary/Landform.cs b/RandomNameGeneratorLibrary/Landform.cs new file mode 100644 index 0000000..5615b09 --- /dev/null +++ b/RandomNameGeneratorLibrary/Landform.cs @@ -0,0 +1,30 @@ +namespace RandomNameGeneratorLibrary +{ + /// + /// The kind of land a place stands on, which + /// uses to pick fitting words. + /// + public enum Landform + { + /// Hills, downs and ridges. + Hill, + + /// Rivers, streams and fords. + River, + + /// Forests, woods and groves. + Forest, + + /// Marshes, fens and bogs. + Marsh, + + /// Coasts, bays and headlands. + Coast, + + /// Mountains, peaks and passes. + Mountain, + + /// Plains, fields and moors. + Plain, + } +} diff --git a/RandomNameGeneratorUnitTests/FantasyNameBehavior.cs b/RandomNameGeneratorUnitTests/FantasyNameBehavior.cs new file mode 100644 index 0000000..70a91c8 --- /dev/null +++ b/RandomNameGeneratorUnitTests/FantasyNameBehavior.cs @@ -0,0 +1,182 @@ +using System; +using System.Linq; +using System.Text.RegularExpressions; +using RandomNameGeneratorLibrary; +using Xunit; + +namespace RandomNameGeneratorUnitTests +{ + public class FantasyNameBehavior + { + private const int Draws = 200; + + private static void AssertSeeded(Func draw) + { + var a = new FantasyNameGenerator(42); + var b = new FantasyNameGenerator(new Random(42)); + var first = Enumerable.Range(0, 20).Select(_ => draw(a)).ToList(); + var second = Enumerable.Range(0, 20).Select(_ => draw(b)).ToList(); + + Assert.Equal(first, second); + Assert.True(first.Distinct().Count() > 1, "a seeded generator should not repeat one name"); + } + + private static void AssertAllMatch(Func draw, string pattern) + { + var generator = new FantasyNameGenerator(7); + for (var i = 0; i < Draws; i++) + { + var name = draw(generator); + Assert.False(string.IsNullOrWhiteSpace(name)); + Assert.Matches(pattern, name); + } + } + + [Fact] + public void DefaultConstructorGeneratesNames() + { + var generator = new FantasyNameGenerator(); + + Assert.False(string.IsNullOrWhiteSpace(generator.GenerateRandomFantasyPlaceName())); + Assert.False(string.IsNullOrWhiteSpace(generator.GenerateRandomBynameName())); + } + + [Fact] + public void NullRandomIsRejected() + { + Assert.Throws(() => new FantasyNameGenerator(null!)); + } + + [Fact] + public void FantasyPlaceNameIsSeededAndShaped() + { + AssertSeeded(g => g.GenerateRandomFantasyPlaceName()); + AssertAllMatch(g => g.GenerateRandomFantasyPlaceName(), @"^[A-Z][a-z]+ [A-Z][a-z]+( of the [A-Z][a-z]+ [A-Z][a-z]+)?$"); + } + + [Fact] + public void FantasyPlaceNamesSometimesHaveAFeatureAndSometimesNot() + { + var names = new FantasyNameGenerator(1).GenerateMultipleFantasyPlaceNames(Draws).ToList(); + + Assert.Contains(names, n => n.Contains(" of the ")); + Assert.Contains(names, n => !n.Contains(" of the ")); + } + + [Fact] + public void GenerateMultipleFantasyPlaceNamesReturnsTheCountAndMatchesSingleDraws() + { + var many = new FantasyNameGenerator(5).GenerateMultipleFantasyPlaceNames(10).ToList(); + var single = new FantasyNameGenerator(5); + + Assert.Equal(10, many.Count); + Assert.Equal(many, Enumerable.Range(0, 10).Select(_ => single.GenerateRandomFantasyPlaceName())); + Assert.Empty(new FantasyNameGenerator(5).GenerateMultipleFantasyPlaceNames(0)); + Assert.Throws(() => new FantasyNameGenerator(5).GenerateMultipleFantasyPlaceNames(-1)); + } + + [Theory] + [InlineData(Landform.Hill, "Hill|Tor|Knoll|Down|Rise|Ridge|Barrow|Brow|Mound|Heights")] + [InlineData(Landform.River, "Ford|Bend|Water|Brook|Run|Rapids|Crossing|Falls|Weir|Banks")] + [InlineData(Landform.Forest, "Wood|Grove|Weald|Thicket|Holt|Glade|Copse|Shaw|Greenwood|Tangle")] + [InlineData(Landform.Marsh, "Fen|Mire|Bog|Marsh|Slough|Carr|Moss|Wallow|Sump|Flats")] + [InlineData(Landform.Coast, "Strand|Cove|Bay|Point|Head|Haven|Sound|Shore|Cliffs|Harbor")] + [InlineData(Landform.Mountain, "Peak|Crag|Pass|Spire|Horn|Summit|Scarp|Fang|Tooth|Col")] + [InlineData(Landform.Plain, "Field|Lea|Moor|Heath|Steppe|Meadow|Plain|Prairie|Flat|Acre")] + public void LandformPlaceNameEndsInAFittingWord(Landform landform, string words) + { + AssertSeeded(g => g.GenerateRandomLandformPlaceName(landform)); + AssertAllMatch(g => g.GenerateRandomLandformPlaceName(landform), "^[A-Z][a-z]+ (" + words + ")$"); + } + + [Fact] + public void UnknownLandformIsRejected() + { + Assert.Throws(() => new FantasyNameGenerator(1).GenerateRandomLandformPlaceName((Landform)99)); + } + + [Fact] + public void FactionNameIsSeededAndShaped() + { + AssertSeeded(g => g.GenerateRandomFactionName()); + AssertAllMatch(g => g.GenerateRandomFactionName(), "^the [A-Z][a-z]+ [A-Z][a-z]+ [A-Z][a-z]+$"); + } + + [Fact] + public void WarBandNameIsOneCapitalizedCompound() + { + AssertSeeded(g => g.GenerateRandomWarBandName()); + AssertAllMatch(g => g.GenerateRandomWarBandName(), "^[A-Z][a-z]+[a-z]+$"); + } + + [Fact] + public void TradeSurnameIsSeededAndIsAnOccupationOrItsVariant() + { + AssertSeeded(g => g.GenerateRandomTradeSurname()); + var allowed = FantasyNameGenerator.TradeSurnames.Concat(FantasyNameGenerator.TradeSurnames.Select(FantasyNameGenerator.ApplySpellingVariant)).ToList(); + var generator = new FantasyNameGenerator(3); + var names = Enumerable.Range(0, Draws).Select(_ => generator.GenerateRandomTradeSurname()).ToList(); + + Assert.All(names, n => Assert.Contains(n, allowed)); + Assert.Contains(names, n => !FantasyNameGenerator.TradeSurnames.Contains(n)); + } + + [Theory] + [InlineData("Smith", "Smyth")] + [InlineData("Taylor", "Tailor")] + [InlineData("Clark", "Clarke")] + [InlineData("Cook", "Cooke")] + [InlineData("Miller", "Miler")] + [InlineData("Turner", "Turnar")] + [InlineData("Mason", "Mason")] + public void ApplySpellingVariantFollowsTheRules(string surname, string expected) + { + Assert.Equal(expected, FantasyNameGenerator.ApplySpellingVariant(surname)); + } + + [Fact] + public void ApplySpellingVariantRejectsNull() + { + Assert.Throws(() => FantasyNameGenerator.ApplySpellingVariant(null!)); + } + + [Fact] + public void BynameNameIsACensusFirstNameAndAByname() + { + AssertSeeded(g => g.GenerateRandomBynameName()); + var generator = new FantasyNameGenerator(11); + for (var i = 0; i < Draws; i++) + { + var name = generator.GenerateRandomBynameName(); + var match = Regex.Match(name, "^(.+) the [A-Z][a-z]+$"); + Assert.True(match.Success, name); + var given = match.Groups[1].Value; + Assert.True(PersonNameGenerator.MaleFirstNames.Contains(given) || PersonNameGenerator.FemaleFirstNames.Contains(given), given); + } + } + + [Fact] + public void KeeperEpithetIsSeededAndShaped() + { + AssertSeeded(g => g.GenerateRandomKeeperEpithet("Bram")); + AssertAllMatch(g => g.GenerateRandomKeeperEpithet(" Bram "), "^Bram the [A-Z][a-z]+(-[A-Z][a-z]+)?$"); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public void KeeperEpithetRejectsAMissingName(string? name) + { + Assert.Throws(() => new FantasyNameGenerator(1).GenerateRandomKeeperEpithet(name!)); + } + + [Fact] + public void TheInterfaceExposesTheGenerator() + { + IFantasyNameGenerator generator = new FantasyNameGenerator(2); + + Assert.False(string.IsNullOrWhiteSpace(generator.GenerateRandomWarBandName())); + } + } +} diff --git a/RandomNameGeneratorUnitTests/PublicApi-additions.txt b/RandomNameGeneratorUnitTests/PublicApi-additions.txt new file mode 100644 index 0000000..ff59cc8 --- /dev/null +++ b/RandomNameGeneratorUnitTests/PublicApi-additions.txt @@ -0,0 +1,40 @@ +# Public API added since 2.2.0, each line ruled in ai-docs/decisions (see 2026-11-27-fantasy-name-grammars.md). +RandomNameGeneratorLibrary.FantasyNameGenerator type class +RandomNameGeneratorLibrary.FantasyNameGenerator base RandomNameGeneratorLibrary.BaseNameGenerator +RandomNameGeneratorLibrary.FantasyNameGenerator implements RandomNameGeneratorLibrary.IFantasyNameGenerator +RandomNameGeneratorLibrary.FantasyNameGenerator constructor .ctor() +RandomNameGeneratorLibrary.FantasyNameGenerator constructor .ctor(System.Random randGen) +RandomNameGeneratorLibrary.FantasyNameGenerator constructor .ctor(System.Int32 seed) +RandomNameGeneratorLibrary.FantasyNameGenerator method System.String GenerateRandomFantasyPlaceName() +RandomNameGeneratorLibrary.FantasyNameGenerator method System.String GenerateRandomLandformPlaceName(RandomNameGeneratorLibrary.Landform landform) +RandomNameGeneratorLibrary.FantasyNameGenerator method System.String GenerateRandomFactionName() +RandomNameGeneratorLibrary.FantasyNameGenerator method System.String GenerateRandomWarBandName() +RandomNameGeneratorLibrary.FantasyNameGenerator method System.String GenerateRandomTradeSurname() +RandomNameGeneratorLibrary.FantasyNameGenerator method System.String GenerateRandomBynameName() +RandomNameGeneratorLibrary.FantasyNameGenerator method System.String GenerateRandomKeeperEpithet(System.String name) +RandomNameGeneratorLibrary.FantasyNameGenerator method System.Collections.Generic.IEnumerable GenerateMultipleFantasyPlaceNames(System.Int32 numberOfNames) +RandomNameGeneratorLibrary.FantasyNameGenerator method static System.String ApplySpellingVariant(System.String surname) +RandomNameGeneratorLibrary.FantasyNameGenerator property System.Collections.Generic.IReadOnlyList TradeSurnames { get; } +RandomNameGeneratorLibrary.IFantasyNameGenerator type interface +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.String GenerateRandomFantasyPlaceName() +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.String GenerateRandomLandformPlaceName(RandomNameGeneratorLibrary.Landform landform) +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.String GenerateRandomFactionName() +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.String GenerateRandomWarBandName() +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.String GenerateRandomTradeSurname() +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.String GenerateRandomBynameName() +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.String GenerateRandomKeeperEpithet(System.String name) +RandomNameGeneratorLibrary.IFantasyNameGenerator method System.Collections.Generic.IEnumerable GenerateMultipleFantasyPlaceNames(System.Int32 numberOfNames) +RandomNameGeneratorLibrary.Landform type enum +RandomNameGeneratorLibrary.Landform base System.Enum +RandomNameGeneratorLibrary.Landform implements System.IComparable +RandomNameGeneratorLibrary.Landform implements System.ISpanFormattable +RandomNameGeneratorLibrary.Landform implements System.IFormattable +RandomNameGeneratorLibrary.Landform implements System.IConvertible +RandomNameGeneratorLibrary.Landform field System.Int32 value__ +RandomNameGeneratorLibrary.Landform field static RandomNameGeneratorLibrary.Landform Hill +RandomNameGeneratorLibrary.Landform field static RandomNameGeneratorLibrary.Landform River +RandomNameGeneratorLibrary.Landform field static RandomNameGeneratorLibrary.Landform Forest +RandomNameGeneratorLibrary.Landform field static RandomNameGeneratorLibrary.Landform Marsh +RandomNameGeneratorLibrary.Landform field static RandomNameGeneratorLibrary.Landform Coast +RandomNameGeneratorLibrary.Landform field static RandomNameGeneratorLibrary.Landform Mountain +RandomNameGeneratorLibrary.Landform field static RandomNameGeneratorLibrary.Landform Plain diff --git a/RandomNameGeneratorUnitTests/PublicApiBehavior.cs b/RandomNameGeneratorUnitTests/PublicApiBehavior.cs index 1d15704..c19fd8c 100644 --- a/RandomNameGeneratorUnitTests/PublicApiBehavior.cs +++ b/RandomNameGeneratorUnitTests/PublicApiBehavior.cs @@ -27,23 +27,26 @@ public void Every2_2_0ApiLineStillExists() [Fact] public void NoPublicMemberWasAddedWithoutAPlanDecision() { - var baseline = new HashSet(Baseline(), StringComparer.Ordinal); + // Additions ruled in a plan decision are listed in RandomNameGeneratorUnitTests/PublicApi-additions.txt. + var baseline = new HashSet(Baseline().Concat(Listed("RandomNameGeneratorUnitTests", "PublicApi-additions.txt")), StringComparer.Ordinal); var added = List(typeof(PersonNameGenerator).Assembly).Where(l => !baseline.Contains(l)).ToList(); Assert.True(added.Count == 0, "added: " + string.Join(Environment.NewLine, added)); } - private static IEnumerable Baseline() + private static IEnumerable Baseline() => Listed("tests", "Golden", "PublicApi-2.2.0.txt"); + + private static IEnumerable Listed(params string[] relativePath) { for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir != null; dir = dir.Parent) { - var candidate = Path.Combine(dir.FullName, "tests", "Golden", "PublicApi-2.2.0.txt"); + var candidate = Path.Combine(dir.FullName, Path.Combine(relativePath)); if (File.Exists(candidate)) { return File.ReadAllLines(candidate).Where(l => l.Length > 0 && !l.StartsWith("#", StringComparison.Ordinal)).ToList(); } } - throw new FileNotFoundException("tests/Golden/PublicApi-2.2.0.txt was not found above " + AppContext.BaseDirectory); + throw new FileNotFoundException(string.Join("/", relativePath) + " was not found above " + AppContext.BaseDirectory); } private static List List(Assembly asm) diff --git a/ai-docs/INDEX.md b/ai-docs/INDEX.md index 08a5633..449ac7a 100644 --- a/ai-docs/INDEX.md +++ b/ai-docs/INDEX.md @@ -5,6 +5,7 @@ Generated by `everlast.py index`; do not hand-edit (edit the entries' frontmatte ## decisions - 2026-09-28 [Retrofit 2.2.0 to the package-modernize standard without changing library code](decisions/2026-09-28-retrofit-without-code-changes.md) (accepted) `retrofit,golden,place-data,release,2.2.1`: why the retrofit adds a golden contract, workflows and settings but no library code, why the place-list truncation waits for 3.0, and why 2.2.1 ships anyway; read before changing a name list or releasing +- 2026-11-27 [FantasyNameGenerator, a new public type of word-grammar names](decisions/2026-11-27-fantasy-name-grammars.md) (proposed) `api,fantasy,grammars,minor-release,public-api`: why FantasyNameGenerator, IFantasyNameGenerator and Landform were added as new types instead of new members on the existing generators, how they are seeded, and how the public API test allows them; read before adding public API ## notes diff --git a/ai-docs/decisions/2026-11-27-fantasy-name-grammars.md b/ai-docs/decisions/2026-11-27-fantasy-name-grammars.md new file mode 100644 index 0000000..14a5a94 --- /dev/null +++ b/ai-docs/decisions/2026-11-27-fantasy-name-grammars.md @@ -0,0 +1,28 @@ +--- +title: FantasyNameGenerator, a new public type of word-grammar names +kind: decision +status: proposed +date: 2026-11-27 +verified: 2026-11-27 +stale_after: never +tags: [api, fantasy, grammars, minor-release, public-api] +summary: "why FantasyNameGenerator, IFantasyNameGenerator and Landform were added as new types instead of new members on the existing generators, how they are seeded, and how the public API test allows them; read before adding public API" +--- + +# FantasyNameGenerator + +## Context + +A request for fantasy names (places, landform places, factions, war-bands, trade surnames, bynames, keeper epithets) built from word grammars, with the library's own word lists. Technique ideas came from Finch's Tome of Adventure Design, Points of Light, Gygax's Living Fantasy and World Builder, and the Judges Guild Guidebook to Shops; no list is copied from them. + +## Decision (awaiting the maintainer's review in the PR) + +- New types only (`FantasyNameGenerator : BaseNameGenerator`, `IFantasyNameGenerator`, `Landform`); no existing member, list or `Random` call order changes, so every golden recording still replays. +- Seeding follows the other generators: `()`, `(Random)`, `(int seed)`. Bynames draw the first name from the census lists through a `PersonNameGenerator` sharing the same `Random`. +- Word lists live in code, not as embedded resources: they are short and hand written, not generated data. +- The seeded output of the new type is promised only within a version (stated in its remarks), so word lists can grow in minor releases without a golden decision. +- `PublicApiBehavior.NoPublicMemberWasAddedWithoutAPlanDecision` now also accepts lines in `RandomNameGeneratorUnitTests/PublicApi-additions.txt`; each block there must point at a decision entry. The 2.2.0 baseline file stays untouched. + +## Not done here + +- No version bump, no release, no Unity sync. A minor release (2.4.0) would carry it; bump `PackageValidationBaselineVersion` after release as usual. diff --git a/ai-docs/log.md b/ai-docs/log.md index fc1fc97..2a738d0 100644 --- a/ai-docs/log.md +++ b/ai-docs/log.md @@ -102,3 +102,5 @@ Append-only. One line per operation: `## [YYYY-MM-DD] op | title` where op is on ## [2026-10-11] index | rebuilt (8 entries) ## [2026-11-15] release | upm/2.3.0 tagged on 1b2b910 (PR #22 merge, ci green, sync.py --check OK); GitHub release https://github.com/m4bwav/DotNetRandomNameGenerator/releases/tag/upm/2.3.0 (not latest). OpenUPM listing PR still waits on Mark. - 2026-11-16: CI broke on NU1004 (runner SDK 10.0.15x moved the implicit Microsoft.NET.ILLink.Tasks to 10.0.15 vs lock 10.0.12). Pinned global.json to SDK 10.0.401 with rollForward disable so lock files only change on a deliberate SDK bump (bump global.json, then dotnet restore --force-evaluate). +- 2026-11-27: FantasyNameGenerator (word-grammar fantasy names) added on branch name-grammars; public API additions are allowed through RandomNameGeneratorUnitTests/PublicApi-additions.txt (decisions/2026-11-27-fantasy-name-grammars.md). The OneDrive folder D:/m4bwa/OneDrive/Code/DotNetRandomNameGenerator is not a git clone; work from a fresh clone. +## [2026-11-27] index | rebuilt (9 entries)