2f107db...5202af9
This commit is contained in:
@@ -36,7 +36,6 @@ namespace Barotrauma
|
||||
/// <summary>
|
||||
/// Constructor with provided 32 bit seed
|
||||
/// </summary>
|
||||
[CLSCompliant(false)]
|
||||
public MTRandom(int seed)
|
||||
{
|
||||
Initialize((uint)Math.Abs(seed));
|
||||
@@ -45,7 +44,6 @@ namespace Barotrauma
|
||||
/// <summary>
|
||||
/// (Re)initialize this instance with provided 32 bit seed
|
||||
/// </summary>
|
||||
[CLSCompliant(false)]
|
||||
private void Initialize(uint seed)
|
||||
{
|
||||
mt = new UInt32[N];
|
||||
@@ -59,7 +57,6 @@ namespace Barotrauma
|
||||
/// <summary>
|
||||
/// Generates a random value from UInt32.MinValue to UInt32.MaxValue, inclusively
|
||||
/// </summary>
|
||||
[CLSCompliant(false)]
|
||||
private uint NextUInt32()
|
||||
{
|
||||
UInt32 y;
|
||||
|
||||
@@ -200,6 +200,26 @@ namespace Barotrauma
|
||||
return to - from;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the angle between the two angles, where the direction matters.
|
||||
/// </summary>
|
||||
public static float GetMidAngle(float from, float to)
|
||||
{
|
||||
float max = MathHelper.Max(from, to);
|
||||
float min = MathHelper.Min(from, to);
|
||||
float diff = max - min;
|
||||
if (from < to)
|
||||
{
|
||||
// Clockwise
|
||||
return from + diff / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
// CCW
|
||||
return from - diff / 2;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// solves the angle opposite to side a (parameters: lengths of each side)
|
||||
/// </summary>
|
||||
|
||||
@@ -13,8 +13,8 @@ namespace Barotrauma
|
||||
ClientOnly = 1 //set to match between clients (used for misc elements that the server doesn't track, but clients want to match anyway)
|
||||
}
|
||||
|
||||
private static Random localRandom = new Random();
|
||||
private static Random[] syncedRandom = new MTRandom[] {
|
||||
private static readonly Random localRandom = new Random();
|
||||
private static readonly Random[] syncedRandom = new MTRandom[] {
|
||||
new MTRandom(), new MTRandom()
|
||||
};
|
||||
|
||||
@@ -29,22 +29,16 @@ namespace Barotrauma
|
||||
syncedRandom[(int)RandSync.ClientOnly] = new MTRandom(seed);
|
||||
}
|
||||
|
||||
private static void Assert(RandSync sync)
|
||||
{
|
||||
//TODO: REMOVE AFTER FINDING ALL WRONG RNG USAGE
|
||||
#if false
|
||||
string trace = Environment.StackTrace.ToString();
|
||||
if (sync != RandSync.Server) return;
|
||||
if (trace.ToLower().Contains("barotraumaclient\\source")) DebugConsole.NewMessage("WARNING: Client code using RandSync.Server\n"+trace,Color.Yellow);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static float Range(float minimum, float maximum, RandSync sync=RandSync.Unsynced)
|
||||
{
|
||||
Assert(sync);
|
||||
return (float)(sync == RandSync.Unsynced ? localRandom : (syncedRandom[(int)sync])).NextDouble() * (maximum - minimum) + minimum;
|
||||
}
|
||||
|
||||
public static double Range(double minimum, double maximum, RandSync sync = RandSync.Unsynced)
|
||||
{
|
||||
return (sync == RandSync.Unsynced ? localRandom : (syncedRandom[(int)sync])).NextDouble() * (maximum - minimum) + minimum;
|
||||
}
|
||||
|
||||
public static double Range(double minimum, double maximum, RandSync sync = RandSync.Unsynced)
|
||||
{
|
||||
Assert(sync);
|
||||
@@ -53,19 +47,16 @@ namespace Barotrauma
|
||||
|
||||
public static int Range(int minimum, int maximum, RandSync sync = RandSync.Unsynced)
|
||||
{
|
||||
Assert(sync);
|
||||
return (sync == RandSync.Unsynced ? localRandom : (syncedRandom[(int)sync])).Next(maximum - minimum) + minimum;
|
||||
}
|
||||
|
||||
public static int Int(int max, RandSync sync = RandSync.Unsynced)
|
||||
{
|
||||
Assert(sync);
|
||||
return (sync == RandSync.Unsynced ? localRandom : (syncedRandom[(int)sync])).Next(max);
|
||||
}
|
||||
|
||||
public static Vector2 Vector(float length, RandSync sync = RandSync.Unsynced)
|
||||
{
|
||||
Assert(sync);
|
||||
Vector2 randomVector = new Vector2(Range(-1.0f, 1.0f, sync), Range(-1.0f, 1.0f, sync));
|
||||
|
||||
if (randomVector.LengthSquared() < 0.001f) return new Vector2(0.0f, length);
|
||||
@@ -95,7 +86,6 @@ namespace Barotrauma
|
||||
|
||||
public static DoubleVector2 Vector(double length, RandSync sync = RandSync.Unsynced)
|
||||
{
|
||||
Assert(sync);
|
||||
double x = Range(-1.0, 1.0, sync);
|
||||
double y = Range(-1.0, 1.0, sync);
|
||||
|
||||
|
||||
@@ -180,11 +180,17 @@ namespace Barotrauma
|
||||
return files;
|
||||
}
|
||||
|
||||
public static string CreateSavePath(SaveType saveType, string fileName = "Save")
|
||||
public static string CreateSavePath(SaveType saveType, string fileName = "Save_Default")
|
||||
{
|
||||
string folder = saveType == SaveType.Singleplayer ? SaveFolder : MultiplayerSaveFolder;
|
||||
|
||||
if (!Directory.Exists(SaveFolder))
|
||||
if (fileName == "Save_Default")
|
||||
{
|
||||
fileName = TextManager.Get("SaveFile.DefaultName", true);
|
||||
if (fileName.Length == 0) fileName = "Save";
|
||||
}
|
||||
|
||||
if (!Directory.Exists(folder))
|
||||
{
|
||||
DebugConsole.ThrowError("Save folder \"" + folder + "\" not found. Created new folder");
|
||||
Directory.CreateDirectory(folder);
|
||||
|
||||
@@ -3,8 +3,10 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -36,6 +38,38 @@ namespace Barotrauma
|
||||
|
||||
public static partial class ToolBox
|
||||
{
|
||||
static internal class Epoch
|
||||
{
|
||||
private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current Unix Epoch
|
||||
/// </summary>
|
||||
public static int Now
|
||||
{
|
||||
get
|
||||
{
|
||||
return (int)(DateTime.UtcNow.Subtract(epoch).TotalSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert an epoch to a datetime
|
||||
/// </summary>
|
||||
public static DateTime ToDateTime(decimal unixTime)
|
||||
{
|
||||
return epoch.AddSeconds((long)unixTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a DateTime to a unix time
|
||||
/// </summary>
|
||||
public static uint FromDateTime(DateTime dt)
|
||||
{
|
||||
return (uint)(dt.Subtract(epoch).TotalSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsProperFilenameCase(string filename)
|
||||
{
|
||||
//File case only matters on Linux where the filesystem is case-sensitive, so we don't need these errors in release builds.
|
||||
@@ -129,6 +163,51 @@ namespace Barotrauma
|
||||
return inputType;
|
||||
}
|
||||
|
||||
// Convert an RGB value into an HLS value.
|
||||
public static Vector3 RgbToHLS(Vector3 color)
|
||||
{
|
||||
double h, l, s;
|
||||
|
||||
double double_r = color.X;
|
||||
double double_g = color.Y;
|
||||
double double_b = color.Z;
|
||||
|
||||
// Get the maximum and minimum RGB components.
|
||||
double max = double_r;
|
||||
if (max < double_g) max = double_g;
|
||||
if (max < double_b) max = double_b;
|
||||
|
||||
double min = double_r;
|
||||
if (min > double_g) min = double_g;
|
||||
if (min > double_b) min = double_b;
|
||||
|
||||
double diff = max - min;
|
||||
l = (max + min) / 2;
|
||||
if (Math.Abs(diff) < 0.00001)
|
||||
{
|
||||
s = 0;
|
||||
h = 0; // H is really undefined.
|
||||
}
|
||||
else
|
||||
{
|
||||
if (l <= 0.5) s = diff / (max + min);
|
||||
else s = diff / (2 - max - min);
|
||||
|
||||
double r_dist = (max - double_r) / diff;
|
||||
double g_dist = (max - double_g) / diff;
|
||||
double b_dist = (max - double_b) / diff;
|
||||
|
||||
if (double_r == max) h = b_dist - g_dist;
|
||||
else if (double_g == max) h = 2 + r_dist - b_dist;
|
||||
else h = 4 + g_dist - r_dist;
|
||||
|
||||
h = h * 60;
|
||||
if (h < 0) h += 360;
|
||||
}
|
||||
|
||||
return new Vector3((float)h, (float)l, (float)s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one string into the other
|
||||
/// </summary>
|
||||
@@ -252,6 +331,20 @@ namespace Barotrauma
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public static UInt32 StringToUInt32Hash(string str, MD5 md5)
|
||||
{
|
||||
//calculate key based on MD5 hash instead of string.GetHashCode
|
||||
//to ensure consistent results across platforms
|
||||
byte[] inputBytes = Encoding.ASCII.GetBytes(str);
|
||||
byte[] hash = md5.ComputeHash(inputBytes);
|
||||
|
||||
UInt32 key = (UInt32)((str.Length & 0xff) << 24); //could use more of the hash here instead?
|
||||
key |= (UInt32)(hash[hash.Length - 3] << 16);
|
||||
key |= (UInt32)(hash[hash.Length - 2] << 8);
|
||||
key |= (UInt32)(hash[hash.Length - 1]);
|
||||
|
||||
return key;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a new instance of the class with all properties and fields copied.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user