diff --git a/Source/Core/Builder.csproj b/Source/Core/Builder.csproj index 5b1ede1859222f2d8f315503968c3c9d62d45190..fc97a21057633ec48fa4bda05835ae0cbf5a6b9c 100644 --- a/Source/Core/Builder.csproj +++ b/Source/Core/Builder.csproj @@ -788,6 +788,7 @@ <Compile Include="Rendering\SurfaceEntryCollection.cs" /> <Compile Include="Rendering\SurfaceManager.cs" /> <Compile Include="Rendering\SurfaceUpdate.cs" /> + <Compile Include="Types\RandomIntegerHandler.cs" /> <Compile Include="Types\ThingClassHandler.cs" /> <Compile Include="Types\ThingTypeHandler.cs" /> <Compile Include="VisualModes\VisualVertex.cs" /> diff --git a/Source/Core/General/General.cs b/Source/Core/General/General.cs index a25a77963f75ef7e2d457c6cc850f8eb7f107afc..a3d8e89d3add4b0fe1dee8152d34ee6952219e49 100644 --- a/Source/Core/General/General.cs +++ b/Source/Core/General/General.cs @@ -187,6 +187,9 @@ namespace CodeImp.DoomBuilder private static bool delaymainwindow; private static bool nosettings; + //misc + private static Random random; //mxd + #endregion #region ================== Properties @@ -688,6 +691,8 @@ namespace CodeImp.DoomBuilder mainwindow.DisplayStatus(StatusType.Warning, "There were errors during program statup!"); if(!delaymainwindow && General.Settings.ShowErrorsWindow) mainwindow.ShowErrors(); } + + random = new Random(); //mxd // Run application from the main window Application.Run(mainwindow); @@ -1561,7 +1566,12 @@ namespace CodeImp.DoomBuilder //mxd. This clamps angle between 0 and 359 public static float ClampAngle(float angle) { return (angle + 360) % 360; - } + } + + //mxd + public static int Random(int min, int max) { + return random.Next(min, max); + } // This returns an element from a collection by index public static T GetByIndex<T>(ICollection<T> collection, int index) diff --git a/Source/Core/Types/RandomIntegerHandler.cs b/Source/Core/Types/RandomIntegerHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..c318f92fe93bffd6f14c8bc6ef6938493a0284a5 --- /dev/null +++ b/Source/Core/Types/RandomIntegerHandler.cs @@ -0,0 +1,107 @@ +#region ================== Namespaces + +using System; +using System.Globalization; +using CodeImp.DoomBuilder.Config; + +#endregion + +namespace CodeImp.DoomBuilder.Types +{ + [TypeHandler(UniversalType.RandomInteger, "Integer (Random)", true)] + internal class RandomIntegerHandler : TypeHandler + { + #region ================== Variables + + private int value; + private int defaultValue; + private bool randomValue; + private int min; + private int max; + + #endregion + + #region ================== Properties + + #endregion + + #region ================== Methods + + public override void SetupArgument(TypeHandlerAttribute attr, ArgumentInfo arginfo) { + defaultValue = (int)arginfo.DefaultValue; + base.SetupArgument(attr, arginfo); + + //mxd. We don't want to store this type + index = (int)UniversalType.Integer; + } + + public override void SetupField(TypeHandlerAttribute attr, UniversalFieldInfo fieldinfo) { + base.SetupField(attr, fieldinfo); + + //mxd. We don't want to store this type + index = (int)UniversalType.Integer; + } + + public override void SetValue(object value) { + int result; + + // Null? + if(value == null) { + this.value = 0; + } + // Compatible type? + else if((value is int) || (value is float) || (value is bool)) { + // Set directly + this.value = Convert.ToInt32(value); + } else { + // Try parsing as string + if(int.TryParse(value.ToString(), NumberStyles.Integer, 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(int.TryParse(parts[0], NumberStyles.Integer, CultureInfo.CurrentCulture, out min) && + int.TryParse(parts[1], NumberStyles.Integer, 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; + } + } + + if(forargument) { + this.value = General.Clamp(this.value, General.Map.FormatInterface.MinArgument, General.Map.FormatInterface.MaxArgument); + } + } + + //mxd + public override void SetDefaultValue() { + value = defaultValue; + } + + public override object GetValue() { + if(randomValue) return General.Random(min, max); //mxd + return this.value; + } + + public override int GetIntValue() { + if(randomValue) return General.Random(min, max); //mxd + return 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 a3006600772f4409079aaa8020dc81f5755e42f5..b073b84ce195edc21df87e9cb2ff83e3f4cfee78 100644 --- a/Source/Core/Types/UniversalType.cs +++ b/Source/Core/Types/UniversalType.cs @@ -41,6 +41,7 @@ namespace CodeImp.DoomBuilder.Types EnumStrings = 16, AngleDegreesFloat = 17, ThingType = 18, - ThingClass = 19 + ThingClass = 19, + RandomInteger = 20 //mxd } }