Skip to content

Commit

Permalink
name pattern rand function
Browse files Browse the repository at this point in the history
  • Loading branch information
cadon committed Dec 26, 2023
1 parent 61b8add commit 8fd4e6d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 3 additions & 4 deletions ARKBreedingStats/NamePatterns/NamePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public static class NamePattern
/// </summary>
private const string PipeEscapeSequence = @"\pipe";

public static Random Random = new Random();

/// <summary>
/// Generate a creature name with the naming pattern.
/// </summary>
Expand Down Expand Up @@ -203,9 +205,6 @@ public static Dictionary<string, string> CreateTokenDictionary(Creature creature
double imp = creature.imprintingBonus * 100;
double eff = creature.tamingEff * 100;

Random rand = new Random(DateTime.Now.Millisecond);
string randStr = rand.Next(0, 999999).ToString("000000");

string effImp = "Z";
string prefix = string.Empty;
if (creature.isBred)
Expand Down Expand Up @@ -332,7 +331,7 @@ public static Dictionary<string, string> CreateTokenDictionary(Creature creature
{ "genn", (speciesCreatures?.Count(c=>c.generation==generation) ?? 0 + 1).ToString()},
{ "nr_in_gen", nrInGeneration.ToString()},
{ "nr_in_gen_sex", nrInGenerationAndSameSex.ToString()},
{ "rnd", randStr },
{ "rnd", Random.Next(0, 999999).ToString("000000")},
{ "ln", libraryCreatureCount.ToString()},
{ "tn", speciesCount.ToString()},
{ "sn", speciesSexCount.ToString()},
Expand Down
15 changes: 15 additions & 0 deletions ARKBreedingStats/NamePatterns/NamePatternFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static string ParametersInvalid(string specificError, string expression,
{"float_div", FunctionFloatDiv},
{"div", FunctionDiv},
{"casing", FunctionCasing},
{"rand", FunctionRand },
{"replace", FunctionReplace},
{"regexreplace", FunctionRegExReplace},
{"customreplace", FunctionCustomReplace},
Expand Down Expand Up @@ -247,6 +248,20 @@ private static string FunctionCasing(Match m, NamePatternParameters p)
return ParametersInvalid($"casing expects 'U', 'L' or 'T', given is '{m.Groups[3].Value}'", m.Groups[0].Value, p.DisplayError);
}

private static string FunctionRand(Match m, NamePatternParameters p)
{
// parameter: 1: to (if one parameter), from (if two parameters), 2: to
// to is exclusive
int.TryParse(m.Groups[2].Value, out var from);
if (!int.TryParse(m.Groups[3].Value, out var to))
{
to = from;
from = 0;
}
if (from < 0 || from >= to) return string.Empty;
return NamePattern.Random.Next(from, to).ToString();
}

private static string FunctionReplace(Match m, NamePatternParameters p)
{
// parameter: 1: replace, 2: text, 3: find, 4: replace
Expand Down
1 change: 1 addition & 0 deletions ARKBreedingStats/NamePatterns/PatternEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ private void InsertText(string text)
{"expr", "{{#expr: expression }}, simple calculation with two operands and one operator. Possible operators are +, -, *, /.\n{{#expr: {hp} * 2 }}" },
{"len", "{{#len: string }}, returns the length of the passed string.\n{{#len: {isTophp}{isTopdm}{isTopwe} }}" },
{"substring","{{#substring: text | start | length }}. Length can be omitted. If start is negative it takes the characters from the end. If length is negative it takes the characters until that position from the end\n{{#substring: {species} | 0 | 4 }}"},
{"rand","{{#rand: min | max (exclusive) }} or {{#rand: max (exclusive) }}\n{{#rand: 100 }}"},
{"replace","{{#replace: text | find | replaceBy }}\n{{#replace: {species} | Aberrant | Ab }}"},
{"regexreplace","{{#regexreplace: text | pattern | replaceBy }}\nUse &&lcub; instead {, &&vline; instead | and &&rcub; instead of }.\n{{#regexreplace: hp-st-we- | \\-$ | }}"},
{"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} }}"},
Expand Down

0 comments on commit 8fd4e6d

Please sign in to comment.