Skip to content

Commit

Permalink
namePattern listName function
Browse files Browse the repository at this point in the history
  • Loading branch information
cadon committed Dec 26, 2023
1 parent 8fd4e6d commit 125036f
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 16 deletions.
10 changes: 10 additions & 0 deletions ARKBreedingStats/ARKBreedingStats.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Compile Include="library\CreatureSpawnCommand.cs" />
<Compile Include="library\DummyCreatures.cs" />
<Compile Include="multiplierTesting\CalculateMultipliers.cs" />
<Compile Include="NamePatterns\NameList.cs" />
<Compile Include="NamePatterns\NamePatternEntry.cs">
<SubType>Component</SubType>
</Compile>
Expand Down Expand Up @@ -666,6 +667,15 @@
<None Include="json\variantsDefaultUnselected.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="json\creatureNamesF.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="json\creatureNamesM.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="json\creatureNamesU.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="_manifest.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>_manifest.json</LastGenOutput>
Expand Down
70 changes: 70 additions & 0 deletions ARKBreedingStats/NamePatterns/NameList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ARKBreedingStats.NamePatterns
{
/// <summary>
/// Loads a list of names from a file.
/// </summary>
internal static class NameList
{
/// <summary>
/// Contains all name lists, key is the fileName suffix.
/// </summary>
private static readonly Dictionary<string, string[]> nameLists = new Dictionary<string, string[]>();
private static readonly Dictionary<string, DateTime> listFileCheckedAt = new Dictionary<string, DateTime>();

/// <summary>
/// Returns a name from a list. If the file wasn't checked recently, it's checked and reloaded.
/// </summary>
public static string GetName(int nameIndex = 0, string listSuffix = null)
{
if (nameIndex < 0) return null;
var nameList = GetNameList(listSuffix);
if (nameList == null || nameList.Length == 0) return null;

if (nameIndex >= nameList.Length)
nameIndex %= nameList.Length;
return nameList[nameIndex];
}

/// <summary>
/// Returns a name list.
/// </summary>
public static string[] GetNameList(string listSuffix = null)
{
if (listSuffix == null) listSuffix = string.Empty;
string[] list;
if (!listFileCheckedAt.TryGetValue(listSuffix, out var checkedAt)
|| (DateTime.Now - checkedAt).TotalSeconds > 10
|| !nameLists.TryGetValue(listSuffix, out list))
{
list = LoadList(listSuffix, checkedAt);
}
return list;
}

private static string[] LoadList(string listSuffix, DateTime checkedAt)
{
var filePath = FileService.GetJsonPath("creatureNames" + listSuffix + ".txt");

if (!File.Exists(filePath)) return null;
try
{
if (new FileInfo(filePath).LastWriteTime > checkedAt)
{
var list = File.ReadAllLines(filePath);
nameLists[listSuffix] = list;
}
listFileCheckedAt[listSuffix] = DateTime.Now;
return nameLists[listSuffix];
}
catch { }
return null;
}
}
}
11 changes: 10 additions & 1 deletion ARKBreedingStats/NamePatterns/NamePatternFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ private static string ParametersInvalid(string specificError, string expression,
{"color", FunctionColor},
{"colornew", FunctionColorNew},
{"indexof", FunctionIndexOf},
{"md5", FunctionMd5}
{"md5", FunctionMd5},
{"listname", FunctionListName }
};

private static string FunctionIf(Match m, NamePatternParameters p)
Expand Down Expand Up @@ -375,6 +376,14 @@ private static string FunctionMd5(Match m, NamePatternParameters p)
return sb.ToString();
}

private static string FunctionListName(Match m, NamePatternParameters p)
{
// parameter: 1: name index, 2: list suffix
if (!int.TryParse(m.Groups[2].Value, out var nameIndex)) return string.Empty;

return NameList.GetName(nameIndex, m.Groups[3].Value);
}

public static void Dispose()
{
_md5?.Dispose();
Expand Down
7 changes: 4 additions & 3 deletions ARKBreedingStats/NamePatterns/PatternEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,16 @@ private void InsertText(string text)
{"customreplace","{{#customreplace: text }}. Replaces the text with a value saved in the file customReplacings.json.\nIf a second parameter is given, that is returned if the key is not available.\n{{#customreplace: {species} }}"},
{"float divide by","{{#float_div: number | divisor | formatString }}, can be used to display stat-values in thousands, e.g. '{{#float_div: {hp_vb} | 1000 | F2 }}kHP'.\n{{#float_div: {hp_vb} | 1000 | F2 }}"},
{"divide by","{{#div: number | divisor }}, can be used to display stat-values in thousands, e.g. '{{#div: {hp_vb} | 1000 }}kHP'.\n{{#div: {hp_vb} | 1000 }}"},
{"padleft","{{#padleft: number | length | padding character }}\n{{#padleft: {hp_vb} | 8 | 0 }}"},
{"padright","{{#padright: number | length | padding character }}\n{{#padright: {hp_vb} | 8 | _ }}"},
{"listName","{{#listName: nameIndex | listSuffix }}, takes a name from a list in the file creatureNames[suffix].txt\n{{#listName: 0 | {sex_short} }}"},
{"padLeft","{{#padLeft: number | length | padding character }}\n{{#padLeft: {hp_vb} | 8 | 0 }}"},
{"padRight","{{#padRight: number | length | padding character }}\n{{#padRight: {hp_vb} | 8 | _ }}"},
{"casing","{{#casing: text | case (U, L, T) }}. U for UPPER, L for lower, T for Title.\n{{#casing: {species} | U }}"},
{"time","{{#time: formatString }}\n{{#time: yyyy-MM-dd_HH:mm }}"},
{"format","{{#format: number | formatString }}\n{{#format: {hp_vb} | 000000 }}"},
{"format_int","Like #format, but supports \"x\" in the format for hexadecimal representations. {{#format_int: number | formatString }}\n{{#format_int: {{#color: 0 }} | x2 }}"},
{"color","{{#color: regionId | return color name | return value even for unused regions }}. Returns the colorId of the region. If the second parameter is not empty, the color name will be returned. Unused regions will only return a value if the third value is not empty.\n{{#color: 0 | true }}"},
{"colorNew","{{#colorNew: regionId }}. Returns newInRegion if the region contains a color that is not yet available in that species. Returns newInSpecies if that color is not yet available in any region of that species.\n{{#colorNew: 0 }}"},
{"indexof","{{#indexof: source string | string to find }}. Returns the index of the second parameter in the first parameter. If the string is not contained, an empty string will be returned.\n{{#indexof: hello | ll }}"},
{"indexOf","{{#indexof: source string | string to find }}. Returns the index of the second parameter in the first parameter. If the string is not contained, an empty string will be returned.\n{{#indexof: hello | ll }}"},
{"md5", "{{#md5: string }}, returns the md5 hash of a given string\n{{#md5: {hp}{st}{we} }}"}
};

Expand Down
38 changes: 38 additions & 0 deletions ARKBreedingStats/json/creatureNamesF.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Aurora
Bess
Bones
Breeze
Casey
Casia
Catlin
Chromy
Chuckles
Cosmo
Cupcake
Danele
Daphne
Durva
Electra
Ellie
Elora
Flare
Ginger
Hope
Indigo
Jackie
Layka
Myst
Nectar
Oracle
Pandora
Peachy
Peanuts
Princess
Raye
Sabre
Shellbie
Shine
Tia
Vanity
Wilde
Zara
19 changes: 19 additions & 0 deletions ARKBreedingStats/json/creatureNamesM.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Austin
Bran
Cosmo
Dearborn
Eclipse
Fuzz
Gazoo
Hercules
Indy
Jiggles
Lightning
Marble
Noah
Pepper
Rancher
Sparkler
Tweeter
Whiskers
Zion
28 changes: 28 additions & 0 deletions ARKBreedingStats/json/creatureNamesU.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Acorn
Bailey
Blaze
Casey
Caramel
Dara
Echo
Fluffy
Goldy
Harper
Indie
Java
Kiwi
Lake
Marley
Max
Ninja
Olive
Onyx
Phoenix
Quinn
Riley
Sable
Scout
Smokey
Sunny
Tiny
Waffles
36 changes: 24 additions & 12 deletions ARKBreedingStats/library/DummyCreatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using ARKBreedingStats.BreedingPlanning;
using ARKBreedingStats.Library;
using ARKBreedingStats.NamePatterns;
using ARKBreedingStats.species;
using ARKBreedingStats.values;

Expand All @@ -15,6 +16,9 @@ public static class DummyCreatures
{
public static DummyCreatureCreationSettings LastSettings;

private static string[] _namesFemale;
private static string[] _namesMale;

/// <summary>
/// Creates a list of random creatures.
/// </summary>
Expand Down Expand Up @@ -147,18 +151,29 @@ public static Creature CreateCreature(Species species, double difficulty = 5, bo
string name = null;
if (doTame)
{
if (_namesFemale == null)
_namesFemale = NameList.GetNameList("F");
if (_namesMale == null)
_namesMale = NameList.GetNameList("M");
var names = sex == Sex.Female ? _namesFemale : _namesMale;
name = names[rand.Next(names.Length)];
if (nameCounter != null)
if (names == null)
{
if (nameCounter.TryGetValue(name, out var nameCount))
{
nameCounter[name]++;
name += $" {nameCount + 1}";
}
else
name = "?";
}
else
{
name = names[rand.Next(names.Length)];
if (nameCounter != null)
{
nameCounter.Add(name, 1);
if (nameCounter.TryGetValue(name, out var nameCount))
{
nameCounter[name]++;
name += $" {nameCount + 1}";
}
else
{
nameCounter.Add(name, 1);
}
}
}
}
Expand Down Expand Up @@ -377,9 +392,6 @@ private static List<Creature> BreedCreatures(Creature[] creatures, Species speci

}

private static readonly string[] _namesFemale = { "Aurora", "Bess", "Bones", "Breeze", "Casey", "Casia", "Catlin", "Chromy", "Chuckles", "Cosmo", "Cupcake", "Danele", "Daphne", "Durva", "Electra", "Ellie", "Elora", "Flare", "Ginger", "Hope", "Indigo", "Jackie", "Layka", "Myst", "Nectar", "Oracle", "Pandora", "Peachy", "Peanuts", "Princess", "Raye", "Sabre", "Shellbie", "Shine", "Tia", "Vanity", "Wilde", "Zara" };
private static readonly string[] _namesMale = { "Austin", "Bran", "Cosmo", "Dearborn", "Eclipse", "Fuzz", "Gazoo", "Hercules", "Indy", "Jiggles", "Lightning", "Marble", "Noah", "Pepper", "Rancher", "Sparkler", "Tweeter", "Whiskers", "Zion" };

#region Binomial distributed levels

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions setup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Source: "{#ReleaseDir}\tr\*"; DestDir: "{app}\tr\"; Excludes: "*.pdb,*.xml"; Fla
Source: "{#ReleaseDir}\zh\*"; DestDir: "{app}\zh\"; Excludes: "*.pdb,*.xml"; Flags: ignoreversion skipifsourcedoesntexist
Source: "{#ReleaseDir}\_manifest.json"; DestDir: "{localappdata}\{#AppName}\"; Flags: ignoreversion
Source: "{#ReleaseDir}\json\*.json"; DestDir: "{localappdata}\{#AppName}\json\"; Flags: ignoreversion
Source: "{#ReleaseDir}\json\*.txt"; DestDir: "{localappdata}\{#AppName}\json\"; Flags: ignoreversion
Source: "{#ReleaseDir}\json\values\values.json"; DestDir: "{localappdata}\{#AppName}\json\values\"; Flags: ignoreversion
Source: "{#ReleaseDir}\json\values\ASA-values.json"; DestDir: "{localappdata}\{#AppName}\json\values\"; Flags: ignoreversion
Source: "{#ReleaseDir}\json\values\_manifest.json"; DestDir: "{localappdata}\{#AppName}\json\values\"; Flags: ignoreversion
Expand Down

0 comments on commit 125036f

Please sign in to comment.