From 8de9ad6b247dbc4ccfd5e71b4c5e6dc18061e88e Mon Sep 17 00:00:00 2001 From: MaxED Date: Wed, 10 Jul 2013 13:40:18 +0000 Subject: [PATCH] Added RandomFloat Universal field type. To use it, change Filed type to "Decimal (random)" and enter a value as 'min max'. --- Source/Core/Builder.csproj | 1 + Source/Core/General/General.cs | 5 ++ Source/Core/Types/RandomFloatHandler.cs | 100 ++++++++++++++++++++++++ Source/Core/Types/UniversalType.cs | 3 +- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 Source/Core/Types/RandomFloatHandler.cs diff --git a/Source/Core/Builder.csproj b/Source/Core/Builder.csproj index fc97a210..454e3707 100644 --- a/Source/Core/Builder.csproj +++ b/Source/Core/Builder.csproj @@ -788,6 +788,7 @@ + diff --git a/Source/Core/General/General.cs b/Source/Core/General/General.cs index a3d8e89d..6db63401 100644 --- a/Source/Core/General/General.cs +++ b/Source/Core/General/General.cs @@ -1572,6 +1572,11 @@ namespace CodeImp.DoomBuilder public static int Random(int min, int max) { return random.Next(min, max); } + + //mxd + public static float Random(float min, float max) { + return (float)Math.Round(min + (max - min) * random.NextDouble(), 2); + } // This returns an element from a collection by index public static T GetByIndex(ICollection collection, int index) diff --git a/Source/Core/Types/RandomFloatHandler.cs b/Source/Core/Types/RandomFloatHandler.cs new file mode 100644 index 00000000..9f6b063b --- /dev/null +++ b/Source/Core/Types/RandomFloatHandler.cs @@ -0,0 +1,100 @@ +#region ================== Namespaces + +using System; +using System.Globalization; +using CodeImp.DoomBuilder.Config; + +#endregion + +namespace CodeImp.DoomBuilder.Types +{ + [TypeHandler(UniversalType.RandomFloat, "Decimal (Random)", true)] + internal class RandomFloatHandler : TypeHandler + { + #region ================== Constants + + #endregion + + #region ================== Variables + + private float value; + private bool randomValue; + private float min; + private float max; + + #endregion + + #region ================== Properties + + #endregion + + #region ================== Methods + + public override void SetupArgument(TypeHandlerAttribute attr, ArgumentInfo arginfo) { + base.SetupArgument(attr, arginfo); + + //mxd. We don't want to store this type + index = (int)UniversalType.Float; + } + + public override void SetupField(TypeHandlerAttribute attr, UniversalFieldInfo fieldinfo) { + base.SetupField(attr, fieldinfo); + + //mxd. We don't want to store this type + index = (int)UniversalType.Float; + } + + public override void SetValue(object value) { + float result; + + // Null? + if(value == null) { + this.value = 0.0f; + } + // Compatible type? + else if((value is int) || (value is float) || (value is bool)) { + // Set directly + this.value = Convert.ToSingle(value); + } else { + // Try parsing as string + if(float.TryParse(value.ToString(), NumberStyles.Float, CultureInfo.CurrentCulture, out result)) { + this.value = result; + } else { + //mxd. Try to parse value as random range + string[] parts = value.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + if(parts.Length == 2) { + if(float.TryParse(parts[0], NumberStyles.Float, CultureInfo.CurrentCulture, out min) && + float.TryParse(parts[1], NumberStyles.Float, CultureInfo.CurrentCulture, out max)) { + randomValue = (min != max); + + if(min == max) + this.value = min; + else if(min > max) + General.Swap(ref min, ref max); + } + } + + this.value = 0.0f; + } + } + } + + public override object GetValue() { + if(randomValue) return General.Random(min, max); //mxd + return this.value; + } + + public override int GetIntValue() { + if(randomValue) return (int)General.Random(min, max); //mxd + return (int)this.value; + } + + public override string GetStringValue() { + if(randomValue) return General.Random(min, max).ToString(); //mxd + return this.value.ToString(); + } + + #endregion + } +} diff --git a/Source/Core/Types/UniversalType.cs b/Source/Core/Types/UniversalType.cs index b073b84c..f9ab8851 100644 --- a/Source/Core/Types/UniversalType.cs +++ b/Source/Core/Types/UniversalType.cs @@ -42,6 +42,7 @@ namespace CodeImp.DoomBuilder.Types AngleDegreesFloat = 17, ThingType = 18, ThingClass = 19, - RandomInteger = 20 //mxd + RandomInteger = 20, //mxd + RandomFloat = 21 //mxd } }