Modding:Random Functions

From Caves of Qud Wiki
Revision as of 16:02, 24 June 2019 by Teamtoto (talk | contribs) (→‎Randoms: Added GetSeededRandomGenerator. it rules)
Jump to navigation Jump to search
This information is reliable as of patch 2.0.182.1. If this is no longer the current patch, you can help by updating it.
As of Patch This information is reliable as of patch 2.0.182.1.
This article is a stub. You can help Caves of Qud Wiki by expanding it.
This article is a stub. You can help Caves of Qud Wiki by expanding it.

If your mod has some element of randomness, you're gonna be looking for a function to call. However, calling the built in Next() function in the Random Class built into C# will net you some compilation errors. The best practice is to use the random functions inside XRL/Rules/Stat.cs. Below are the different random functions inside Caves of Qud and what they are used for.

Randoms

These are the methods that return the Random class. These are all hashed from

      Stat.Rand = new Random(Hash.String("Seed0" + (object) Seed));
      Stat.Rnd = new Random(Hash.String("Seed1" + (object) Seed));
      Stat.Rnd2 = new Random(Hash.String("Seed2" + (object) Seed));
      if (includeLifetimeSeeds)
        Stat.LevelUpRandom = new Random(Hash.String("Seed3" + (object) Seed));
      Stat.Rnd4 = new Random(Hash.String("Seed4" + (object) Seed));
      Stat.Rnd5 = new Random(Hash.String("Seed5" + (object) Seed));

Rnd()

This is the main function that randomizes and determines things such as sultan artifacts, villages, and basically everything important that is based on world seed. All effects and mutations use this function. Don't use this function in your mod if you don't want to ruin world seeds.

Rnd2(), Rnd4(), Rnd5()

These functions deals with the smaller events you find in Qud.

LevelUpRandom()

The Random used to determine randomized things at level up.

GetSeededRandomGenerator(string Seed)

If you don't want to use the game's seeds in fear of altering worldseed too much, there is a helper function that returns a new random that hashes a new random. This allows your mod to be affected by world seed but does not alter the base game's random number calls.

public static Random GetSeededRandomGenerator(string Seed)
        {
            if (XRLCore.Core.Game == null)
            {
                return new Random();
            }
            return new Random(Hash.String(XRLCore.Core.Game.GetWorldSeed(null) + Seed));
        }

The Seed here should follow best practice conventions, so YourName_YourMod to avoid overlapping conflicts as much as possible.

Returners

These use a specific random class to return a multitude of things.

Random(int low, int high)

Calls Stat.Rnd().


TinkerRandom(int Low, int High)

Calls Stat.Rnd4().

GaussianRandom(float Mean, float StandardDeviation)

double num = Math.Sqrt(-2.0 * Math.Log(Stat.Rnd.NextDouble())) * Math.Sin(2.0 * Math.PI * Stat.Rnd.NextDouble());
      return (double) Mean + (double) StandardDeviation * num;

Returns a random float around the mean with a probability density of a normal distribution.

RandomCosmetic(int low, int high)

Used most often when getting random angles for particles and other cosmetic effects. Calls Stat.Rnd2().

SeededRandom(string Seed, int Low, int High)

Returns an int based on Seed, in the range of [Low, High].