comment pass
This commit is contained in:
@@ -2,9 +2,11 @@ using System;
|
||||
|
||||
// ReSharper disable ReturnTypeCanBeEnumerable.Global
|
||||
// ReSharper disable ParameterTypeCanBeEnumerable.Local
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// The backtrack algorithm can calculate the subset of a set of samples with the sum closest to a target value.
|
||||
/// </summary>
|
||||
public struct Backtrack
|
||||
{
|
||||
private int[] indices;
|
||||
@@ -95,6 +97,9 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
return Sum(leave) < Sum(pick) ? leave : pick;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combine the two brute force algorithms to calculate the optimal subset.
|
||||
/// </summary>
|
||||
public int[] BruteForce()
|
||||
{
|
||||
var lower = BruteForceLower();
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace EscapeRoomEngine.Desert.Runtime
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// This component disables a certain game object from being selectable in the scene view.
|
||||
/// </summary>
|
||||
public class DisablePicking : MonoBehaviour
|
||||
{
|
||||
public GameObject objectToDisable;
|
||||
|
||||
@@ -3,6 +3,9 @@ using UnityEngine;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores both the hdr and ldr version of a colour.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public struct DynamicColor
|
||||
{
|
||||
|
||||
@@ -19,25 +19,24 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
|
||||
public class Logger : MonoBehaviour
|
||||
{
|
||||
public static Logger DefaultLogger
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_foundLogger == null)
|
||||
{
|
||||
_foundLogger = FindObjectOfType<Logger>();
|
||||
}
|
||||
return _foundLogger;
|
||||
}
|
||||
}
|
||||
private static Logger _foundLogger;
|
||||
/// <summary>
|
||||
/// The active instance of the logger.
|
||||
/// </summary>
|
||||
private static Logger Instance { get; set; }
|
||||
|
||||
[Tooltip("Toggle logging on or off globally.")]
|
||||
public bool loggingEnabled;
|
||||
[Tooltip("This list determines what types of log messages should be displayed.")]
|
||||
public List<LogType> typeFilter;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public static void Log(string message, LogType type)
|
||||
{
|
||||
if (DefaultLogger.loggingEnabled && DefaultLogger.typeFilter.Contains(type))
|
||||
if (Instance.loggingEnabled && Instance.typeFilter.Contains(type))
|
||||
{
|
||||
Debug.Log($"<b>[{type}]</b> {message}");
|
||||
}
|
||||
|
||||
@@ -6,12 +6,25 @@ using Random = System.Random;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// The representation of a normal distribution with a certain mean μ and standard deviation σ.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public struct NormalDistribution
|
||||
{
|
||||
public float μ, σ;
|
||||
/// <summary>
|
||||
/// The mean of this distribution.
|
||||
/// </summary>
|
||||
public float μ;
|
||||
/// <summary>
|
||||
/// The standard deviation of this distribution.
|
||||
/// </summary>
|
||||
public float σ;
|
||||
|
||||
public static NormalDistribution Standard => new NormalDistribution { μ = 0, σ = 1 };
|
||||
/// <summary>
|
||||
/// Generate a standard normal distribution.
|
||||
/// </summary>
|
||||
public static NormalDistribution Standard => new() { μ = 0, σ = 1 };
|
||||
|
||||
public NormalDistribution(float[] samples) : this()
|
||||
{
|
||||
@@ -19,13 +32,25 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
σ = Probability.StandardDeviation(samples, μ);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sample a random value from this distribution.
|
||||
/// </summary>
|
||||
public float Sample() => σ * Probability.Normal() + μ;
|
||||
|
||||
/// <summary>
|
||||
/// Sample the CDF of this distribution.
|
||||
/// </summary>
|
||||
public float Cumulative(float x) => (float)new Normal(μ, σ).CumulativeDistribution(x);
|
||||
|
||||
/// <summary>
|
||||
/// Sample the inverse CDF of this distribution.
|
||||
/// </summary>
|
||||
public float InverseCumulative(float x) => (float)new Normal(μ, σ).InverseCumulativeDistribution(x);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This class is used for probability calculations.
|
||||
/// </summary>
|
||||
public static class Probability
|
||||
{
|
||||
private static readonly Random _random = new();
|
||||
@@ -50,6 +75,9 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
return u1 * Mathf.Sqrt(-2 * Mathf.Log(square) / square);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the mean of a list of samples.
|
||||
/// </summary>
|
||||
public static float Mean(float[] samples)
|
||||
{
|
||||
if (samples.Length == 0)
|
||||
@@ -60,7 +88,13 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
return samples.Sum() / samples.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the standard deviation of a list of samples.
|
||||
/// </summary>
|
||||
public static float StandardDeviation(float[] samples) => StandardDeviation(samples, Mean(samples));
|
||||
/// <summary>
|
||||
/// Calculate the standard deviation of a list of samples without recalculating the mean.
|
||||
/// </summary>
|
||||
public static float StandardDeviation(float[] samples, float mean)
|
||||
{
|
||||
var deviations = new float[samples.Length];
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// This struct represents an integer range.
|
||||
/// </summary>
|
||||
public struct Range
|
||||
{
|
||||
public int min, max;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the range, excluding the maximum value.
|
||||
/// </summary>
|
||||
public int Length => max - min;
|
||||
|
||||
public Range(int min, int max)
|
||||
@@ -12,6 +18,9 @@
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an array of every value in this range.
|
||||
/// </summary>
|
||||
public int[] ToArray(bool includeMax = false)
|
||||
{
|
||||
var count = includeMax ? Length + 1 : Length;
|
||||
|
||||
@@ -23,6 +23,9 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
|
||||
#region Randomness
|
||||
|
||||
/// <summary>
|
||||
/// Return a random value in a range, including the last value.
|
||||
/// </summary>
|
||||
public static int RandomInclusive(int from, int to) => Random.Range(from, to + 1);
|
||||
|
||||
#endregion
|
||||
@@ -41,6 +44,9 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
return element;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a random value from a list.
|
||||
/// </summary>
|
||||
public static T RandomElement<T>(this List<T> list) => list[Random.Range(0, list.Count)];
|
||||
|
||||
/// <summary>
|
||||
@@ -59,8 +65,14 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
|
||||
public static class Vector2IntExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Project a 2D vector onto the floor in 3D space.
|
||||
/// </summary>
|
||||
public static Vector3Int ProjectAtFloor(this Vector2Int vector) => vector.ProjectAtHeight(0);
|
||||
|
||||
/// <summary>
|
||||
/// Project a 2D vector onto a specific height in 3D space.
|
||||
/// </summary>
|
||||
public static Vector3Int ProjectAtHeight(this Vector2Int vector, int height) =>
|
||||
new Vector3Int(vector.x, height, vector.y);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user